1 /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-
3 * Copyright (c) 2009-2010 Apple Inc. All rights reserved.
5 * @APPLE_LICENSE_HEADER_START@
7 * This file contains Original Code and/or Modifications of Original Code
8 * as defined in and that are subject to the Apple Public Source License
9 * Version 2.0 (the 'License'). You may not use this file except in
10 * compliance with the License. Please obtain a copy of the License at
11 * http://www.opensource.apple.com/apsl/ and read it before using this
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
19 * Please see the License for the specific language governing rights and
20 * limitations under the License.
22 * @APPLE_LICENSE_HEADER_END@
31 #include <sys/param.h>
35 #include "MachOFileAbstraction.hpp"
37 #include "libunwind/DwarfInstructions.hpp"
38 #include "libunwind/AddressSpace.hpp"
39 #include "libunwind/Registers.hpp"
45 #include <type_traits>
48 #include "debugline.h"
50 #include "Architectures.hpp"
51 #include "Bitcode.hpp"
53 #include "macho_relocatable_file.h"
57 extern void throwf(const char* format
, ...) __attribute__ ((noreturn
,format(printf
, 1, 2)));
58 extern void warning(const char* format
, ...) __attribute__((format(printf
, 1, 2)));
61 namespace relocatable
{
65 template <typename A
> class Parser
;
66 template <typename A
> class Atom
;
67 template <typename A
> class Section
;
68 template <typename A
> class CFISection
;
69 template <typename A
> class CUSection
;
72 class File
: public ld::relocatable::File
75 File(const char* p
, time_t mTime
, const uint8_t* content
, ld::File::Ordinal ord
) :
76 ld::relocatable::File(p
,mTime
,ord
), _fileContent(content
),
77 _sectionsArray(NULL
), _atomsArray(NULL
),
78 _sectionsArrayCount(0), _atomsArrayCount(0), _aliasAtomsArrayCount(0),
79 _debugInfoKind(ld::relocatable::File::kDebugInfoNone
),
80 _dwarfTranslationUnitPath(NULL
),
81 _dwarfDebugInfoSect(NULL
), _dwarfDebugAbbrevSect(NULL
),
82 _dwarfDebugLineSect(NULL
), _dwarfDebugStringSect(NULL
),
83 _objConstraint(ld::File::objcConstraintNone
),
88 _canScatterAtoms(false),
89 _srcKind(kSourceUnknown
) {}
92 // overrides of ld::File
93 virtual bool forEachAtom(ld::File::AtomHandler
&) const;
94 virtual bool justInTimeforEachAtom(const char* name
, ld::File::AtomHandler
&) const
96 virtual uint32_t minOSVersion() const { return _minOSVersion
; }
97 virtual uint32_t platformLoadCommand() const { return _platform
; }
99 // overrides of ld::relocatable::File
100 virtual ObjcConstraint
objCConstraint() const { return _objConstraint
; }
101 virtual uint32_t cpuSubType() const { return _cpuSubType
; }
102 virtual DebugInfoKind
debugInfo() const { return _debugInfoKind
; }
103 virtual const std::vector
<ld::relocatable::File::Stab
>* stabs() const { return &_stabs
; }
104 virtual bool canScatterAtoms() const { return _canScatterAtoms
; }
105 virtual const char* translationUnitSource() const;
106 virtual LinkerOptionsList
* linkerOptions() const { return &_linkerOptions
; }
107 virtual uint8_t swiftVersion() const { return _swiftVersion
; }
108 virtual ld::Bitcode
* getBitcode() const { return _bitcode
.get(); }
109 virtual SourceKind
sourceKind() const { return _srcKind
; }
111 const uint8_t* fileContent() { return _fileContent
; }
113 friend class Atom
<A
>;
114 friend class Section
<A
>;
115 friend class Parser
<A
>;
116 friend class CFISection
<A
>::OAS
;
118 typedef typename
A::P P
;
120 const uint8_t* _fileContent
;
121 Section
<A
>** _sectionsArray
;
122 uint8_t* _atomsArray
;
123 uint8_t* _aliasAtomsArray
;
124 uint32_t _sectionsArrayCount
;
125 uint32_t _atomsArrayCount
;
126 uint32_t _aliasAtomsArrayCount
;
127 std::vector
<ld::Fixup
> _fixups
;
128 std::vector
<ld::Atom::UnwindInfo
> _unwindInfos
;
129 std::vector
<ld::Atom::LineInfo
> _lineInfos
;
130 std::vector
<ld::relocatable::File::Stab
>_stabs
;
131 ld::relocatable::File::DebugInfoKind _debugInfoKind
;
132 const char* _dwarfTranslationUnitPath
;
133 const macho_section
<P
>* _dwarfDebugInfoSect
;
134 const macho_section
<P
>* _dwarfDebugAbbrevSect
;
135 const macho_section
<P
>* _dwarfDebugLineSect
;
136 const macho_section
<P
>* _dwarfDebugStringSect
;
137 ld::File::ObjcConstraint _objConstraint
;
138 uint8_t _swiftVersion
;
139 uint32_t _cpuSubType
;
140 uint32_t _minOSVersion
;
142 bool _canScatterAtoms
;
143 std::vector
<std::vector
<const char*> > _linkerOptions
;
144 std::unique_ptr
<ld::Bitcode
> _bitcode
;
149 template <typename A
>
150 class Section
: public ld::Section
153 typedef typename
A::P::uint_t pint_t
;
154 typedef typename
A::P P
;
155 typedef typename
A::P::E E
;
157 virtual ~Section() { }
158 class File
<A
>& file() const { return _file
; }
159 const macho_section
<P
>* machoSection() const { return _machOSection
; }
160 uint32_t sectionNum(class Parser
<A
>&) const;
161 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
);
162 virtual ld::Atom::ContentType
contentType() { return ld::Atom::typeUnclassified
; }
163 virtual bool dontDeadStrip() { return (this->_machOSection
->flags() & S_ATTR_NO_DEAD_STRIP
); }
164 virtual bool dontDeadStripIfReferencesLive() { return ( (this->_machOSection
!= NULL
) && (this->_machOSection
->flags() & S_ATTR_LIVE_SUPPORT
) ); }
165 virtual Atom
<A
>* findAtomByAddress(pint_t addr
) { return this->findContentAtomByAddress(addr
, this->_beginAtoms
, this->_endAtoms
); }
166 virtual bool addFollowOnFixups() const { return ! _file
.canScatterAtoms(); }
167 virtual uint32_t appendAtoms(class Parser
<A
>& parser
, uint8_t* buffer
,
168 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
169 const struct Parser
<A
>::CFI_CU_InfoArrays
&) = 0;
170 virtual uint32_t computeAtomCount(class Parser
<A
>& parser
,
171 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
172 const struct Parser
<A
>::CFI_CU_InfoArrays
&) = 0;
173 virtual void makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
&);
174 virtual bool addRelocFixup(class Parser
<A
>& parser
, const macho_relocation_info
<P
>*);
175 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const { return 0; }
176 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
177 const ld::IndirectBindingTable
& ind
) const { return false; }
178 virtual bool ignoreLabel(const char* label
) const { return false; }
179 static const char* makeSectionName(const macho_section
<typename
A::P
>* s
);
182 Section(File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
183 : ld::Section(makeSegmentName(s
), makeSectionName(s
), sectionType(s
)),
184 _file(f
), _machOSection(s
), _beginAtoms(NULL
), _endAtoms(NULL
), _hasAliases(false) { }
185 Section(File
<A
>& f
, const char* segName
, const char* sectName
, ld::Section::Type t
, bool hidden
=false)
186 : ld::Section(segName
, sectName
, t
, hidden
), _file(f
), _machOSection(NULL
),
187 _beginAtoms(NULL
), _endAtoms(NULL
), _hasAliases(false) { }
190 Atom
<A
>* findContentAtomByAddress(pint_t addr
, class Atom
<A
>* start
, class Atom
<A
>* end
);
191 uint32_t x86_64PcRelOffset(uint8_t r_type
);
192 void addLOH(class Parser
<A
>& parser
, int kind
, int count
, const uint64_t addrs
[]);
193 static const char* makeSegmentName(const macho_section
<typename
A::P
>* s
);
194 static bool readable(const macho_section
<typename
A::P
>* s
);
195 static bool writable(const macho_section
<typename
A::P
>* s
);
196 static bool exectuable(const macho_section
<typename
A::P
>* s
);
197 static ld::Section::Type
sectionType(const macho_section
<typename
A::P
>* s
);
200 const macho_section
<P
>* _machOSection
;
201 class Atom
<A
>* _beginAtoms
;
202 class Atom
<A
>* _endAtoms
;
204 std::set
<const class Atom
<A
>*> _altEntries
;
208 template <typename A
>
209 class CFISection
: public Section
<A
>
212 CFISection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
213 : Section
<A
>(f
, s
) { }
214 uint32_t cfiCount(Parser
<A
>& parser
);
216 virtual ld::Atom::ContentType
contentType() { return ld::Atom::typeCFI
; }
217 virtual uint32_t computeAtomCount(class Parser
<A
>& parser
, struct Parser
<A
>::LabelAndCFIBreakIterator
& it
, const struct Parser
<A
>::CFI_CU_InfoArrays
&);
218 virtual uint32_t appendAtoms(class Parser
<A
>& parser
, uint8_t* buffer
, struct Parser
<A
>::LabelAndCFIBreakIterator
& it
, const struct Parser
<A
>::CFI_CU_InfoArrays
&);
219 virtual void makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
&);
220 virtual bool addFollowOnFixups() const { return false; }
224 /// ObjectFileAddressSpace is used as a template parameter to UnwindCursor for parsing
225 /// dwarf CFI information in an object file.
230 typedef typename
A::P::uint_t pint_t
;
231 typedef typename
A::P P
;
232 typedef typename
A::P::E E
;
233 typedef typename
A::P::uint_t sint_t
;
235 OAS(CFISection
<A
>& ehFrameSection
, const uint8_t* ehFrameBuffer
) :
236 _ehFrameSection(ehFrameSection
),
237 _ehFrameContent(ehFrameBuffer
),
238 _ehFrameStartAddr(ehFrameSection
.machoSection()->addr()),
239 _ehFrameEndAddr(ehFrameSection
.machoSection()->addr()+ehFrameSection
.machoSection()->size()) {}
241 uint8_t get8(pint_t addr
) { return *((uint8_t*)mappedAddress(addr
)); }
242 uint16_t get16(pint_t addr
) { return E::get16(*((uint16_t*)mappedAddress(addr
))); }
243 uint32_t get32(pint_t addr
) { return E::get32(*((uint32_t*)mappedAddress(addr
))); }
244 uint64_t get64(pint_t addr
) { return E::get64(*((uint64_t*)mappedAddress(addr
))); }
245 pint_t
getP(pint_t addr
) { return P::getP(*((pint_t
*)mappedAddress(addr
))); }
246 uint64_t getULEB128(pint_t
& addr
, pint_t end
);
247 int64_t getSLEB128(pint_t
& addr
, pint_t end
);
248 pint_t
getEncodedP(pint_t
& addr
, pint_t end
, uint8_t encoding
);
250 const void* mappedAddress(pint_t addr
);
252 CFISection
<A
>& _ehFrameSection
;
253 const uint8_t* _ehFrameContent
;
254 pint_t _ehFrameStartAddr
;
255 pint_t _ehFrameEndAddr
;
259 typedef typename
A::P::uint_t pint_t
;
260 typedef libunwind::CFI_Atom_Info
<OAS
> CFI_Atom_Info
;
262 void cfiParse(class Parser
<A
>& parser
, uint8_t* buffer
, CFI_Atom_Info cfiArray
[], uint32_t& cfiCount
, const pint_t cuStarts
[], uint32_t cuCount
);
263 bool needsRelocating();
265 static bool bigEndian();
267 void addCiePersonalityFixups(class Parser
<A
>& parser
, const CFI_Atom_Info
* cieInfo
);
268 static void warnFunc(void* ref
, uint64_t funcAddr
, const char* msg
);
272 template <typename A
>
273 class CUSection
: public Section
<A
>
276 CUSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
277 : Section
<A
>(f
, s
) { }
279 typedef typename
A::P::uint_t pint_t
;
280 typedef typename
A::P P
;
281 typedef typename
A::P::E E
;
283 virtual uint32_t computeAtomCount(class Parser
<A
>& parser
, struct Parser
<A
>::LabelAndCFIBreakIterator
& it
, const struct Parser
<A
>::CFI_CU_InfoArrays
&) { return 0; }
284 virtual uint32_t appendAtoms(class Parser
<A
>& parser
, uint8_t* buffer
, struct Parser
<A
>::LabelAndCFIBreakIterator
& it
, const struct Parser
<A
>::CFI_CU_InfoArrays
&) { return 0; }
285 virtual void makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
&);
286 virtual bool addFollowOnFixups() const { return false; }
289 pint_t functionStartAddress
;
290 uint32_t functionSymbolIndex
;
291 uint32_t rangeLength
;
292 uint32_t compactUnwindInfo
;
293 const char* personality
;
300 void parse(class Parser
<A
>& parser
, uint32_t cnt
, Info array
[]);
301 static bool encodingMeansUseDwarf(compact_unwind_encoding_t enc
);
306 const char* personalityName(class Parser
<A
>& parser
, const macho_relocation_info
<P
>* reloc
);
308 static int infoSorter(const void* l
, const void* r
);
313 template <typename A
>
314 class TentativeDefinitionSection
: public Section
<A
>
317 TentativeDefinitionSection(Parser
<A
>& parser
, File
<A
>& f
)
318 : Section
<A
>(f
, "__DATA", "__comm/tent", ld::Section::typeTentativeDefs
) {}
320 virtual ld::Atom::ContentType
contentType() { return ld::Atom::typeZeroFill
; }
321 virtual bool addFollowOnFixups() const { return false; }
322 virtual Atom
<A
>* findAtomByAddress(typename
A::P::uint_t addr
) { throw "TentativeDefinitionSection::findAtomByAddress() should never be called"; }
323 virtual uint32_t computeAtomCount(class Parser
<A
>& parser
, struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
324 const struct Parser
<A
>::CFI_CU_InfoArrays
&);
325 virtual uint32_t appendAtoms(class Parser
<A
>& parser
, uint8_t* buffer
,
326 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
327 const struct Parser
<A
>::CFI_CU_InfoArrays
&);
328 virtual void makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
&) {}
330 typedef typename
A::P::uint_t pint_t
;
331 typedef typename
A::P P
;
335 template <typename A
>
336 class AbsoluteSymbolSection
: public Section
<A
>
339 AbsoluteSymbolSection(Parser
<A
>& parser
, File
<A
>& f
)
340 : Section
<A
>(f
, "__DATA", "__abs", ld::Section::typeAbsoluteSymbols
, true) {}
342 virtual ld::Atom::ContentType
contentType() { return ld::Atom::typeUnclassified
; }
343 virtual bool dontDeadStrip() { return false; }
344 virtual ld::Atom::Alignment
alignmentForAddress(typename
A::P::uint_t addr
) { return ld::Atom::Alignment(0); }
345 virtual bool addFollowOnFixups() const { return false; }
346 virtual Atom
<A
>* findAtomByAddress(typename
A::P::uint_t addr
) { throw "AbsoluteSymbolSection::findAtomByAddress() should never be called"; }
347 virtual uint32_t computeAtomCount(class Parser
<A
>& parser
, struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
348 const struct Parser
<A
>::CFI_CU_InfoArrays
&);
349 virtual uint32_t appendAtoms(class Parser
<A
>& parser
, uint8_t* buffer
,
350 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
351 const struct Parser
<A
>::CFI_CU_InfoArrays
&);
352 virtual void makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
&) {}
353 virtual Atom
<A
>* findAbsAtomForValue(typename
A::P::uint_t
);
356 typedef typename
A::P::uint_t pint_t
;
357 typedef typename
A::P P
;
361 template <typename A
>
362 class SymboledSection
: public Section
<A
>
365 SymboledSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
);
366 virtual ld::Atom::ContentType
contentType() { return _type
; }
367 virtual bool dontDeadStrip();
368 virtual uint32_t computeAtomCount(class Parser
<A
>& parser
, struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
369 const struct Parser
<A
>::CFI_CU_InfoArrays
&);
370 virtual uint32_t appendAtoms(class Parser
<A
>& parser
, uint8_t* buffer
,
371 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
372 const struct Parser
<A
>::CFI_CU_InfoArrays
&);
374 typedef typename
A::P::uint_t pint_t
;
375 typedef typename
A::P P
;
377 ld::Atom::ContentType _type
;
381 template <typename A
>
382 class TLVDefsSection
: public SymboledSection
<A
>
385 TLVDefsSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
) :
386 SymboledSection
<A
>(parser
, f
, s
) { }
393 template <typename A
>
394 class ImplicitSizeSection
: public Section
<A
>
397 ImplicitSizeSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
398 : Section
<A
>(f
, s
) { }
399 virtual uint32_t computeAtomCount(class Parser
<A
>& parser
, struct Parser
<A
>::LabelAndCFIBreakIterator
& it
, const struct Parser
<A
>::CFI_CU_InfoArrays
&);
400 virtual uint32_t appendAtoms(class Parser
<A
>& parser
, uint8_t* buffer
, struct Parser
<A
>::LabelAndCFIBreakIterator
& it
, const struct Parser
<A
>::CFI_CU_InfoArrays
&);
402 typedef typename
A::P::uint_t pint_t
;
403 typedef typename
A::P P
;
405 virtual bool addFollowOnFixups() const { return false; }
406 virtual const char* unlabeledAtomName(Parser
<A
>& parser
, pint_t addr
) = 0;
407 virtual ld::Atom::SymbolTableInclusion
symbolTableInclusion();
408 virtual pint_t
elementSizeAtAddress(pint_t addr
) = 0;
409 virtual ld::Atom::Scope
scopeAtAddress(Parser
<A
>& parser
, pint_t addr
) { return ld::Atom::scopeLinkageUnit
; }
410 virtual bool useElementAt(Parser
<A
>& parser
,
411 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
, pint_t addr
) = 0;
412 virtual ld::Atom::Definition
definition() { return ld::Atom::definitionRegular
; }
413 virtual ld::Atom::Combine
combine(Parser
<A
>& parser
, pint_t addr
) = 0;
414 virtual bool ignoreLabel(const char* label
) const { return (label
[0] == 'L'); }
418 template <typename A
>
419 class FixedSizeSection
: public ImplicitSizeSection
<A
>
422 FixedSizeSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
423 : ImplicitSizeSection
<A
>(parser
, f
, s
) { }
425 typedef typename
A::P::uint_t pint_t
;
426 typedef typename
A::P P
;
427 typedef typename
A::P::E E
;
429 virtual bool useElementAt(Parser
<A
>& parser
,
430 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
, pint_t addr
)
435 template <typename A
>
436 class Literal4Section
: public FixedSizeSection
<A
>
439 Literal4Section(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
440 : FixedSizeSection
<A
>(parser
, f
, s
) {}
442 typedef typename
A::P::uint_t pint_t
;
443 typedef typename
A::P P
;
445 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
) { return ld::Atom::Alignment(2); }
446 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "4-byte-literal"; }
447 virtual pint_t
elementSizeAtAddress(pint_t addr
) { return 4; }
448 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
) { return ld::Atom::combineByNameAndContent
; }
449 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
450 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
451 const ld::IndirectBindingTable
& ind
) const;
452 virtual bool ignoreLabel(const char* label
) const;
455 template <typename A
>
456 class Literal8Section
: public FixedSizeSection
<A
>
459 Literal8Section(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
460 : FixedSizeSection
<A
>(parser
, f
, s
) {}
462 typedef typename
A::P::uint_t pint_t
;
463 typedef typename
A::P P
;
465 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
) { return ld::Atom::Alignment(3); }
466 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "8-byte-literal"; }
467 virtual pint_t
elementSizeAtAddress(pint_t addr
) { return 8; }
468 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
) { return ld::Atom::combineByNameAndContent
; }
469 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
470 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
471 const ld::IndirectBindingTable
& ind
) const;
472 virtual bool ignoreLabel(const char* label
) const;
475 template <typename A
>
476 class Literal16Section
: public FixedSizeSection
<A
>
479 Literal16Section(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
480 : FixedSizeSection
<A
>(parser
, f
, s
) {}
482 typedef typename
A::P::uint_t pint_t
;
483 typedef typename
A::P P
;
485 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
) { return ld::Atom::Alignment(4); }
486 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "16-byte-literal"; }
487 virtual pint_t
elementSizeAtAddress(pint_t addr
) { return 16; }
488 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
) { return ld::Atom::combineByNameAndContent
; }
489 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
490 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
491 const ld::IndirectBindingTable
& ind
) const;
492 virtual bool ignoreLabel(const char* label
) const;
496 template <typename A
>
497 class NonLazyPointerSection
: public FixedSizeSection
<A
>
500 NonLazyPointerSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
501 : FixedSizeSection
<A
>(parser
, f
, s
) {}
503 typedef typename
A::P::uint_t pint_t
;
504 typedef typename
A::P P
;
506 virtual void makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
&);
507 virtual ld::Atom::ContentType
contentType() { return ld::Atom::typeNonLazyPointer
; }
508 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
) { return ld::Atom::Alignment(log2(sizeof(pint_t
))); }
509 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "non_lazy_ptr"; }
510 virtual pint_t
elementSizeAtAddress(pint_t addr
) { return sizeof(pint_t
); }
511 virtual ld::Atom::Scope
scopeAtAddress(Parser
<A
>& parser
, pint_t addr
);
512 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
);
513 virtual bool ignoreLabel(const char* label
) const { return true; }
514 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
515 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
516 const ld::IndirectBindingTable
& ind
) const;
519 static const char* targetName(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
);
520 static ld::Fixup::Kind
fixupKind();
523 template <typename A
>
524 class TLVPointerSection
: public FixedSizeSection
<A
>
527 TLVPointerSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
528 : FixedSizeSection
<A
>(parser
, f
, s
) {}
530 typedef typename
A::P::uint_t pint_t
;
531 typedef typename
A::P P
;
533 virtual ld::Atom::ContentType
contentType() { return ld::Atom::typeTLVPointer
; }
534 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
) { return ld::Atom::Alignment(log2(sizeof(pint_t
))); }
535 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "tlv_lazy_ptr"; }
536 virtual pint_t
elementSizeAtAddress(pint_t addr
) { return sizeof(pint_t
); }
537 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
);
538 virtual bool ignoreLabel(const char* label
) const { return true; }
539 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
540 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
541 const ld::IndirectBindingTable
& ind
) const;
544 static const char* targetName(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
, bool* isStatic
);
548 template <typename A
>
549 class CFStringSection
: public FixedSizeSection
<A
>
552 CFStringSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
553 : FixedSizeSection
<A
>(parser
, f
, s
) {}
555 typedef typename
A::P::uint_t pint_t
;
557 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
) { return ld::Atom::Alignment(log2(sizeof(pint_t
))); }
558 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "CFString"; }
559 virtual pint_t
elementSizeAtAddress(pint_t addr
) { return 4*sizeof(pint_t
); }
560 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
) { return ld::Atom::combineByNameAndReferences
; }
561 virtual bool ignoreLabel(const char* label
) const { return true; }
562 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
563 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
564 const ld::IndirectBindingTable
& ind
) const;
566 enum ContentType
{ contentUTF8
, contentUTF16
, contentUnknown
};
567 static const uint8_t* targetContent(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
,
568 ContentType
* ct
, unsigned int* count
);
572 template <typename A
>
573 class ObjC1ClassSection
: public FixedSizeSection
<A
>
576 ObjC1ClassSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
577 : FixedSizeSection
<A
>(parser
, f
, s
) {}
579 typedef typename
A::P::uint_t pint_t
;
580 typedef typename
A::P P
;
581 typedef typename
A::P::E E
;
583 virtual ld::Atom::Scope
scopeAtAddress(Parser
<A
>& , pint_t
) { return ld::Atom::scopeGlobal
; }
584 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
) { return ld::Atom::Alignment(2); }
585 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
);
586 virtual ld::Atom::SymbolTableInclusion
symbolTableInclusion() { return ld::Atom::symbolTableIn
; }
587 virtual pint_t
elementSizeAtAddress(pint_t addr
);
588 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
) { return ld::Atom::combineNever
; }
589 virtual bool ignoreLabel(const char* label
) const { return true; }
590 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
592 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
593 const ld::IndirectBindingTable
& ind
) const { return false; }
594 virtual bool addRelocFixup(class Parser
<A
>& parser
, const macho_relocation_info
<P
>*);
598 template <typename A
>
599 class ObjC2ClassRefsSection
: public FixedSizeSection
<A
>
602 ObjC2ClassRefsSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
603 : FixedSizeSection
<A
>(parser
, f
, s
) {}
605 typedef typename
A::P::uint_t pint_t
;
607 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
) { return ld::Atom::Alignment(log2(sizeof(pint_t
))); }
608 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "objc-class-ref"; }
609 virtual pint_t
elementSizeAtAddress(pint_t addr
) { return sizeof(pint_t
); }
610 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
) { return ld::Atom::combineByNameAndReferences
; }
611 virtual bool ignoreLabel(const char* label
) const { return true; }
612 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
613 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
614 const ld::IndirectBindingTable
& ind
) const;
616 const char* targetClassName(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
620 template <typename A
>
621 class ObjC2CategoryListSection
: public FixedSizeSection
<A
>
624 ObjC2CategoryListSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
625 : FixedSizeSection
<A
>(parser
, f
, s
) {}
627 typedef typename
A::P::uint_t pint_t
;
629 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
) { return ld::Atom::Alignment(log2(sizeof(pint_t
))); }
630 virtual ld::Atom::Scope
scopeAtAddress(Parser
<A
>& parser
, pint_t addr
) { return ld::Atom::scopeTranslationUnit
; }
631 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "objc-cat-list"; }
632 virtual pint_t
elementSizeAtAddress(pint_t addr
) { return sizeof(pint_t
); }
633 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
) { return ld::Atom::combineNever
; }
634 virtual bool ignoreLabel(const char* label
) const { return true; }
636 const char* targetClassName(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
640 template <typename A
>
641 class PointerToCStringSection
: public FixedSizeSection
<A
>
644 PointerToCStringSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
645 : FixedSizeSection
<A
>(parser
, f
, s
) {}
647 typedef typename
A::P::uint_t pint_t
;
649 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
) { return ld::Atom::Alignment(log2(sizeof(pint_t
))); }
650 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "pointer-to-literal-cstring"; }
651 virtual pint_t
elementSizeAtAddress(pint_t addr
) { return sizeof(pint_t
); }
652 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
) { return ld::Atom::combineByNameAndReferences
; }
653 virtual bool ignoreLabel(const char* label
) const { return true; }
654 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
655 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
656 const ld::IndirectBindingTable
& ind
) const;
657 virtual const char* targetCString(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
661 template <typename A
>
662 class Objc1ClassReferences
: public PointerToCStringSection
<A
>
665 Objc1ClassReferences(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
666 : PointerToCStringSection
<A
>(parser
, f
, s
) {}
668 typedef typename
A::P::uint_t pint_t
;
669 typedef typename
A::P P
;
671 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "pointer-to-literal-objc-class-name"; }
672 virtual bool addRelocFixup(class Parser
<A
>& parser
, const macho_relocation_info
<P
>*);
673 virtual const char* targetCString(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
677 template <typename A
>
678 class CStringSection
: public ImplicitSizeSection
<A
>
681 CStringSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
682 : ImplicitSizeSection
<A
>(parser
, f
, s
) {}
684 typedef typename
A::P::uint_t pint_t
;
685 typedef typename
A::P P
;
687 virtual ld::Atom::ContentType
contentType() { return ld::Atom::typeCString
; }
688 virtual Atom
<A
>* findAtomByAddress(pint_t addr
);
689 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "cstring"; }
690 virtual pint_t
elementSizeAtAddress(pint_t addr
);
691 virtual bool ignoreLabel(const char* label
) const;
692 virtual bool useElementAt(Parser
<A
>& parser
,
693 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
, pint_t addr
);
694 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
) { return ld::Atom::combineByNameAndContent
; }
695 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
696 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
697 const ld::IndirectBindingTable
& ind
) const;
702 template <typename A
>
703 class UTF16StringSection
: public SymboledSection
<A
>
706 UTF16StringSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
707 : SymboledSection
<A
>(parser
, f
, s
) {}
709 typedef typename
A::P::uint_t pint_t
;
710 typedef typename
A::P P
;
712 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
) { return ld::Atom::combineByNameAndContent
; }
713 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
714 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
715 const ld::IndirectBindingTable
& ind
) const;
720 // Atoms in mach-o files
722 template <typename A
>
723 class Atom
: public ld::Atom
726 // overrides of ld::Atom
727 virtual const ld::File
* file() const;
728 virtual const char* translationUnitSource() const
729 { return sect().file().translationUnitSource(); }
730 virtual const char* name() const { return _name
; }
731 virtual uint64_t size() const { return _size
; }
732 virtual uint64_t objectAddress() const { return _objAddress
; }
733 virtual void copyRawContent(uint8_t buffer
[]) const;
734 virtual const uint8_t* rawContentPointer() const { return contentPointer(); }
735 virtual unsigned long contentHash(const ld::IndirectBindingTable
& ind
) const
736 { if ( _hash
== 0 ) _hash
= sect().contentHash(this, ind
); return _hash
; }
737 virtual bool canCoalesceWith(const ld::Atom
& rhs
, const ld::IndirectBindingTable
& ind
) const
738 { return sect().canCoalesceWith(this, rhs
, ind
); }
739 virtual ld::Fixup::iterator
fixupsBegin() const { return &machofile()._fixups
[_fixupsStartIndex
]; }
740 virtual ld::Fixup::iterator
fixupsEnd() const { return &machofile()._fixups
[_fixupsStartIndex
+_fixupsCount
]; }
741 virtual ld::Atom::UnwindInfo::iterator
beginUnwind() const { return &machofile()._unwindInfos
[_unwindInfoStartIndex
]; }
742 virtual ld::Atom::UnwindInfo::iterator
endUnwind() const { return &machofile()._unwindInfos
[_unwindInfoStartIndex
+_unwindInfoCount
]; }
743 virtual ld::Atom::LineInfo::iterator
beginLineInfo() const{ return &machofile()._lineInfos
[_lineInfoStartIndex
]; }
744 virtual ld::Atom::LineInfo::iterator
endLineInfo() const { return &machofile()._lineInfos
[_lineInfoStartIndex
+_lineInfoCount
]; }
745 virtual void setFile(const ld::File
* f
);
749 enum { kFixupStartIndexBits
= 32,
750 kLineInfoStartIndexBits
= 32,
751 kUnwindInfoStartIndexBits
= 24,
752 kFixupCountBits
= 24,
753 kLineInfoCountBits
= 12,
754 kUnwindInfoCountBits
= 4
755 }; // must sum to 128
758 // methods for all atoms from mach-o object file
759 Section
<A
>& sect() const { return (Section
<A
>&)section(); }
760 File
<A
>& machofile() const { return ((Section
<A
>*)(this->_section
))->file(); }
761 void setFixupsRange(uint32_t s
, uint32_t c
);
762 void setUnwindInfoRange(uint32_t s
, uint32_t c
);
763 void extendUnwindInfoRange();
764 void setLineInfoRange(uint32_t s
, uint32_t c
);
765 bool roomForMoreLineInfoCount() { return (_lineInfoCount
< ((1<<kLineInfoCountBits
)-1)); }
766 void incrementLineInfoCount() { assert(roomForMoreLineInfoCount()); ++_lineInfoCount
; }
767 void incrementFixupCount() { if (_fixupsCount
== ((1 << kFixupCountBits
)-1))
768 throwf("too may fixups in %s", name()); ++_fixupsCount
; }
769 const uint8_t* contentPointer() const;
770 uint32_t fixupCount() const { return _fixupsCount
; }
771 void verifyAlignment(const macho_section
<typename
A::P
>&) const;
773 typedef typename
A::P P
;
774 typedef typename
A::P::E E
;
775 typedef typename
A::P::uint_t pint_t
;
776 // constuct via all attributes
777 Atom(Section
<A
>& sct
, const char* nm
, pint_t addr
, uint64_t sz
,
778 ld::Atom::Definition d
, ld::Atom::Combine c
, ld::Atom::Scope s
,
779 ld::Atom::ContentType ct
, ld::Atom::SymbolTableInclusion i
,
780 bool dds
, bool thumb
, bool al
, ld::Atom::Alignment a
)
781 : ld::Atom((ld::Section
&)sct
, d
, c
, s
, ct
, i
, dds
, thumb
, al
, a
),
782 _size(sz
), _objAddress(addr
), _name(nm
), _hash(0),
783 _fixupsStartIndex(0), _lineInfoStartIndex(0),
784 _unwindInfoStartIndex(0), _fixupsCount(0),
785 _lineInfoCount(0), _unwindInfoCount(0) { }
786 // construct via symbol table entry
787 Atom(Section
<A
>& sct
, Parser
<A
>& parser
, const macho_nlist
<P
>& sym
,
788 uint64_t sz
, bool alias
=false)
789 : ld::Atom((ld::Section
&)sct
, parser
.definitionFromSymbol(sym
),
790 parser
.combineFromSymbol(sym
), parser
.scopeFromSymbol(sym
),
791 parser
.resolverFromSymbol(sym
) ? ld::Atom::typeResolver
: sct
.contentType(),
792 parser
.inclusionFromSymbol(sym
),
793 (parser
.dontDeadStripFromSymbol(sym
) && !sct
.dontDeadStripIfReferencesLive()) || sct
.dontDeadStrip(),
794 parser
.isThumbFromSymbol(sym
), alias
,
795 sct
.alignmentForAddress(sym
.n_value())),
796 _size(sz
), _objAddress(sym
.n_value()),
797 _name(parser
.nameFromSymbol(sym
)), _hash(0),
798 _fixupsStartIndex(0), _lineInfoStartIndex(0),
799 _unwindInfoStartIndex(0), _fixupsCount(0),
800 _lineInfoCount(0), _unwindInfoCount(0) {
801 // <rdar://problem/6783167> support auto-hidden weak symbols
802 if ( _scope
== ld::Atom::scopeGlobal
&&
803 (sym
.n_desc() & (N_WEAK_DEF
|N_WEAK_REF
)) == (N_WEAK_DEF
|N_WEAK_REF
) )
805 this->verifyAlignment(*sct
.machoSection());
806 if ( sct
.dontDeadStripIfReferencesLive() )
807 this->setDontDeadStripIfReferencesLive();
811 friend class Parser
<A
>;
812 friend class Section
<A
>;
813 friend class CStringSection
<A
>;
814 friend class AbsoluteSymbolSection
<A
>;
819 mutable unsigned long _hash
;
821 uint64_t _fixupsStartIndex
: kFixupStartIndexBits
,
822 _lineInfoStartIndex
: kLineInfoStartIndexBits
,
823 _unwindInfoStartIndex
: kUnwindInfoStartIndexBits
,
824 _fixupsCount
: kFixupCountBits
,
825 _lineInfoCount
: kLineInfoCountBits
,
826 _unwindInfoCount
: kUnwindInfoCountBits
;
828 static std::map
<const ld::Atom
*, const ld::File
*> _s_fileOverride
;
831 template <typename A
>
832 std::map
<const ld::Atom
*, const ld::File
*> Atom
<A
>::_s_fileOverride
;
834 template <typename A
>
835 void Atom
<A
>::setFile(const ld::File
* f
) {
836 _s_fileOverride
[this] = f
;
839 template <typename A
>
840 const ld::File
* Atom
<A
>::file() const
842 std::map
<const ld::Atom
*, const ld::File
*>::iterator pos
= _s_fileOverride
.find(this);
843 if ( pos
!= _s_fileOverride
.end() )
846 return §().file();
849 template <typename A
>
850 void Atom
<A
>::setFixupsRange(uint32_t startIndex
, uint32_t count
)
852 if ( count
>= (1 << kFixupCountBits
) )
853 throwf("too many fixups in function %s", this->name());
854 if ( startIndex
>= (1 << kFixupStartIndexBits
) )
855 throwf("too many fixups in file");
856 assert(((startIndex
+count
) <= sect().file()._fixups
.size()) && "fixup index out of range");
857 _fixupsStartIndex
= startIndex
;
858 _fixupsCount
= count
;
861 template <typename A
>
862 void Atom
<A
>::setUnwindInfoRange(uint32_t startIndex
, uint32_t count
)
864 if ( count
>= (1 << kUnwindInfoCountBits
) )
865 throwf("too many compact unwind infos in function %s", this->name());
866 if ( startIndex
>= (1 << kUnwindInfoStartIndexBits
) )
867 throwf("too many compact unwind infos (%d) in file", startIndex
);
868 assert((startIndex
+count
) <= sect().file()._unwindInfos
.size() && "unwindinfo index out of range");
869 _unwindInfoStartIndex
= startIndex
;
870 _unwindInfoCount
= count
;
873 template <typename A
>
874 void Atom
<A
>::extendUnwindInfoRange()
876 if ( _unwindInfoCount
+1 >= (1 << kUnwindInfoCountBits
) )
877 throwf("too many compact unwind infos in function %s", this->name());
878 _unwindInfoCount
+= 1;
881 template <typename A
>
882 void Atom
<A
>::setLineInfoRange(uint32_t startIndex
, uint32_t count
)
884 assert((count
< (1 << kLineInfoCountBits
)) && "too many line infos");
885 assert((startIndex
+count
) < sect().file()._lineInfos
.size() && "line info index out of range");
886 _lineInfoStartIndex
= startIndex
;
887 _lineInfoCount
= count
;
890 template <typename A
>
891 const uint8_t* Atom
<A
>::contentPointer() const
893 const macho_section
<P
>* sct
= this->sect().machoSection();
894 if ( this->_objAddress
> sct
->addr() + sct
->size() )
895 throwf("malformed .o file, symbol has address 0x%0llX which is outside range of its section", (uint64_t)this->_objAddress
);
896 uint32_t fileOffset
= sct
->offset() - sct
->addr() + this->_objAddress
;
897 return this->sect().file().fileContent()+fileOffset
;
901 template <typename A
>
902 void Atom
<A
>::copyRawContent(uint8_t buffer
[]) const
905 if ( this->contentType() == ld::Atom::typeZeroFill
) {
906 bzero(buffer
, _size
);
908 else if ( _size
!= 0 ) {
909 memcpy(buffer
, this->contentPointer(), _size
);
914 void Atom
<arm
>::verifyAlignment(const macho_section
<P
>&) const
916 if ( (this->section().type() == ld::Section::typeCode
) && ! isThumb() ) {
917 if ( ((_objAddress
% 4) != 0) || (this->alignment().powerOf2
< 2) )
918 warning("ARM function not 4-byte aligned: %s from %s", this->name(), this->file()->path());
922 #if SUPPORT_ARCH_arm64
924 void Atom
<arm64
>::verifyAlignment(const macho_section
<P
>& sect
) const
926 if ( (this->section().type() == ld::Section::typeCode
) && (sect
.size() != 0) ) {
927 if ( ((_objAddress
% 4) != 0) || (this->alignment().powerOf2
< 2) )
928 warning("arm64 function not 4-byte aligned: %s from %s", this->name(), this->file()->path());
933 template <typename A
>
934 void Atom
<A
>::verifyAlignment(const macho_section
<P
>&) const
939 class AliasAtom
: public ld::Atom
942 AliasAtom(const char* name
, bool hidden
, const ld::File
* file
, const char* aliasOfName
) :
943 ld::Atom(_s_section
, ld::Atom::definitionRegular
, ld::Atom::combineNever
,
944 (hidden
? ld::Atom::scopeLinkageUnit
: ld::Atom::scopeGlobal
),
945 ld::Atom::typeUnclassified
, ld::Atom::symbolTableIn
,
946 false, false, true, 0),
949 _fixup(0, ld::Fixup::k1of1
, ld::Fixup::kindNoneFollowOn
, ld::Fixup::bindingByNameUnbound
, aliasOfName
) { }
951 virtual const ld::File
* file() const { return _file
; }
952 virtual const char* translationUnitSource() const
954 virtual const char* name() const { return _name
; }
955 virtual uint64_t size() const { return 0; }
956 virtual uint64_t objectAddress() const { return 0; }
957 virtual void copyRawContent(uint8_t buffer
[]) const { }
958 virtual ld::Fixup::iterator
fixupsBegin() const { return &((ld::Fixup
*)&_fixup
)[0]; }
959 virtual ld::Fixup::iterator
fixupsEnd() const { return &((ld::Fixup
*)&_fixup
)[1]; }
962 static ld::Section _s_section
;
964 const ld::File
* _file
;
969 ld::Section
AliasAtom::_s_section("__LD", "__aliases", ld::Section::typeTempAlias
, true);
972 template <typename A
>
976 static bool validFile(const uint8_t* fileContent
, bool subtypeMustMatch
=false,
977 cpu_subtype_t subtype
=0);
978 static const char* fileKind(const uint8_t* fileContent
);
979 static Options::Platform
findPlatform(const macho_header
<typename
A::P
>* header
);
980 static bool hasObjC2Categories(const uint8_t* fileContent
);
981 static bool hasObjC1Categories(const uint8_t* fileContent
);
982 static bool getNonLocalSymbols(const uint8_t* fileContnet
, std::vector
<const char*> &syms
);
983 static ld::relocatable::File
* parse(const uint8_t* fileContent
, uint64_t fileLength
,
984 const char* path
, time_t modTime
, ld::File::Ordinal ordinal
,
985 const ParserOptions
& opts
) {
986 Parser
p(fileContent
, fileLength
, path
, modTime
,
987 ordinal
, opts
.warnUnwindConversionProblems
,
988 opts
.keepDwarfUnwind
, opts
.forceDwarfConversion
,
989 opts
.neverConvertDwarf
, opts
.verboseOptimizationHints
,
990 opts
.ignoreMismatchPlatform
);
991 return p
.parse(opts
);
994 typedef typename
A::P P
;
995 typedef typename
A::P::E E
;
996 typedef typename
A::P::uint_t pint_t
;
998 struct SourceLocation
{
1000 SourceLocation(Atom
<A
>* a
, uint32_t o
) : atom(a
), offsetInAtom(o
) {}
1002 uint32_t offsetInAtom
;
1007 const char* name
; // only used if targetAtom is NULL
1009 bool weakImport
; // only used if targetAtom is NULL
1012 struct FixupInAtom
{
1013 FixupInAtom(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, Atom
<A
>* target
) :
1014 fixup(src
.offsetInAtom
, c
, k
, target
), atom(src
.atom
) { src
.atom
->incrementFixupCount(); }
1016 FixupInAtom(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, ld::Fixup::TargetBinding b
, Atom
<A
>* target
) :
1017 fixup(src
.offsetInAtom
, c
, k
, b
, target
), atom(src
.atom
) { src
.atom
->incrementFixupCount(); }
1019 FixupInAtom(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, bool wi
, const char* name
) :
1020 fixup(src
.offsetInAtom
, c
, k
, wi
, name
), atom(src
.atom
) { src
.atom
->incrementFixupCount(); }
1022 FixupInAtom(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, ld::Fixup::TargetBinding b
, const char* name
) :
1023 fixup(src
.offsetInAtom
, c
, k
, b
, name
), atom(src
.atom
) { src
.atom
->incrementFixupCount(); }
1025 FixupInAtom(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, uint64_t addend
) :
1026 fixup(src
.offsetInAtom
, c
, k
, addend
), atom(src
.atom
) { src
.atom
->incrementFixupCount(); }
1028 FixupInAtom(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
) :
1029 fixup(src
.offsetInAtom
, c
, k
, (uint64_t)0), atom(src
.atom
) { src
.atom
->incrementFixupCount(); }
1035 void addFixup(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, Atom
<A
>* target
) {
1036 _allFixups
.push_back(FixupInAtom(src
, c
, k
, target
));
1039 void addFixup(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, ld::Fixup::TargetBinding b
, Atom
<A
>* target
) {
1040 _allFixups
.push_back(FixupInAtom(src
, c
, k
, b
, target
));
1043 void addFixup(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, bool wi
, const char* name
) {
1044 _allFixups
.push_back(FixupInAtom(src
, c
, k
, wi
, name
));
1047 void addFixup(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, ld::Fixup::TargetBinding b
, const char* name
) {
1048 _allFixups
.push_back(FixupInAtom(src
, c
, k
, b
, name
));
1051 void addFixup(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, uint64_t addend
) {
1052 _allFixups
.push_back(FixupInAtom(src
, c
, k
, addend
));
1055 void addFixup(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
) {
1056 _allFixups
.push_back(FixupInAtom(src
, c
, k
));
1059 const char* path() { return _path
; }
1060 uint32_t symbolCount() { return _symbolCount
; }
1061 uint32_t indirectSymbol(uint32_t indirectIndex
);
1062 const macho_nlist
<P
>& symbolFromIndex(uint32_t index
);
1063 const char* nameFromSymbol(const macho_nlist
<P
>& sym
);
1064 ld::Atom::Scope
scopeFromSymbol(const macho_nlist
<P
>& sym
);
1065 static ld::Atom::Definition
definitionFromSymbol(const macho_nlist
<P
>& sym
);
1066 static ld::Atom::Combine
combineFromSymbol(const macho_nlist
<P
>& sym
);
1067 ld::Atom::SymbolTableInclusion
inclusionFromSymbol(const macho_nlist
<P
>& sym
);
1068 static bool dontDeadStripFromSymbol(const macho_nlist
<P
>& sym
);
1069 static bool isThumbFromSymbol(const macho_nlist
<P
>& sym
);
1070 static bool weakImportFromSymbol(const macho_nlist
<P
>& sym
);
1071 static bool resolverFromSymbol(const macho_nlist
<P
>& sym
);
1072 static bool altEntryFromSymbol(const macho_nlist
<P
>& sym
);
1073 uint32_t symbolIndexFromIndirectSectionAddress(pint_t
,const macho_section
<P
>*);
1074 const macho_section
<P
>* firstMachOSection() { return _sectionsStart
; }
1075 const macho_section
<P
>* machOSectionFromSectionIndex(uint32_t index
);
1076 uint32_t machOSectionCount() { return _machOSectionsCount
; }
1077 uint32_t undefinedStartIndex() { return _undefinedStartIndex
; }
1078 uint32_t undefinedEndIndex() { return _undefinedEndIndex
; }
1079 void addFixup(FixupInAtom f
) { _allFixups
.push_back(f
); }
1080 Section
<A
>* sectionForNum(unsigned int sectNum
);
1081 Section
<A
>* sectionForAddress(pint_t addr
);
1082 Atom
<A
>* findAtomByAddress(pint_t addr
);
1083 Atom
<A
>* findAtomByAddressOrNullIfStub(pint_t addr
);
1084 Atom
<A
>* findAtomByAddressOrLocalTargetOfStub(pint_t addr
, uint32_t* offsetInAtom
);
1085 Atom
<A
>* findAtomByName(const char* name
); // slow!
1086 void findTargetFromAddress(pint_t addr
, TargetDesc
& target
);
1087 void findTargetFromAddress(pint_t baseAddr
, pint_t addr
, TargetDesc
& target
);
1088 void findTargetFromAddressAndSectionNum(pint_t addr
, unsigned int sectNum
,
1089 TargetDesc
& target
);
1090 uint32_t tentativeDefinitionCount() { return _tentativeDefinitionCount
; }
1091 uint32_t absoluteSymbolCount() { return _absoluteSymbolCount
; }
1093 bool hasStubsSection() { return (_stubsSectionNum
!= 0); }
1094 unsigned int stubsSectionNum() { return _stubsSectionNum
; }
1095 void addDtraceExtraInfos(const SourceLocation
& src
, const char* provider
);
1096 const char* scanSymbolTableForAddress(uint64_t addr
);
1097 bool warnUnwindConversionProblems() { return _warnUnwindConversionProblems
; }
1098 bool hasDataInCodeLabels() { return _hasDataInCodeLabels
; }
1099 bool keepDwarfUnwind() { return _keepDwarfUnwind
; }
1100 bool forceDwarfConversion() { return _forceDwarfConversion
; }
1101 bool verboseOptimizationHints() { return _verboseOptimizationHints
; }
1102 bool neverConvertDwarf() { return _neverConvertDwarf
; }
1103 bool armUsesZeroCostExceptions() { return _armUsesZeroCostExceptions
; }
1105 macho_data_in_code_entry
<P
>* dataInCodeStart() { return _dataInCodeStart
; }
1106 macho_data_in_code_entry
<P
>* dataInCodeEnd() { return _dataInCodeEnd
; }
1107 const uint8_t* optimizationHintsStart() { return _lohStart
; }
1108 const uint8_t* optimizationHintsEnd() { return _lohEnd
; }
1109 bool hasOptimizationHints() { return _lohStart
!= _lohEnd
; }
1112 void addFixups(const SourceLocation
& src
, ld::Fixup::Kind kind
, const TargetDesc
& target
);
1113 void addFixups(const SourceLocation
& src
, ld::Fixup::Kind kind
, const TargetDesc
& target
, const TargetDesc
& picBase
);
1117 struct LabelAndCFIBreakIterator
{
1118 typedef typename CFISection
<A
>::CFI_Atom_Info CFI_Atom_Info
;
1119 LabelAndCFIBreakIterator(const uint32_t* ssa
, uint32_t ssc
, const pint_t
* cfisa
,
1120 uint32_t cfisc
, bool ols
)
1121 : sortedSymbolIndexes(ssa
), sortedSymbolCount(ssc
), cfiStartsArray(cfisa
),
1122 cfiStartsCount(cfisc
), fileHasOverlappingSymbols(ols
),
1123 newSection(false), cfiIndex(0), symIndex(0) {}
1124 bool next(Parser
<A
>& parser
, const Section
<A
>& sect
, uint32_t sectNum
, pint_t startAddr
, pint_t endAddr
,
1125 pint_t
* addr
, pint_t
* size
, const macho_nlist
<P
>** sym
);
1126 pint_t
peek(Parser
<A
>& parser
, pint_t startAddr
, pint_t endAddr
);
1127 void beginSection() { newSection
= true; symIndex
= 0; }
1129 const uint32_t* const sortedSymbolIndexes
;
1130 const uint32_t sortedSymbolCount
;
1131 const pint_t
* cfiStartsArray
;
1132 const uint32_t cfiStartsCount
;
1133 const bool fileHasOverlappingSymbols
;
1139 struct CFI_CU_InfoArrays
{
1140 typedef typename CFISection
<A
>::CFI_Atom_Info CFI_Atom_Info
;
1141 typedef typename CUSection
<A
>::Info CU_Info
;
1142 CFI_CU_InfoArrays(const CFI_Atom_Info
* cfiAr
, uint32_t cfiC
, CU_Info
* cuAr
, uint32_t cuC
)
1143 : cfiArray(cfiAr
), cuArray(cuAr
), cfiCount(cfiC
), cuCount(cuC
) {}
1144 const CFI_Atom_Info
* const cfiArray
;
1145 CU_Info
* const cuArray
;
1146 const uint32_t cfiCount
;
1147 const uint32_t cuCount
;
1153 friend class Section
<A
>;
1155 enum SectionType
{ sectionTypeIgnore
, sectionTypeLiteral4
, sectionTypeLiteral8
, sectionTypeLiteral16
,
1156 sectionTypeNonLazy
, sectionTypeCFI
, sectionTypeCString
, sectionTypeCStringPointer
,
1157 sectionTypeUTF16Strings
, sectionTypeCFString
, sectionTypeObjC2ClassRefs
, typeObjC2CategoryList
,
1158 sectionTypeObjC1Classes
, sectionTypeSymboled
, sectionTypeObjC1ClassRefs
,
1159 sectionTypeTentativeDefinitions
, sectionTypeAbsoluteSymbols
, sectionTypeTLVDefs
,
1160 sectionTypeCompactUnwind
, sectionTypeTLVPointers
};
1162 template <typename P
>
1163 struct MachOSectionAndSectionClass
1165 const macho_section
<P
>* sect
;
1168 static int sorter(const void* l
, const void* r
) {
1169 const MachOSectionAndSectionClass
<P
>* left
= (MachOSectionAndSectionClass
<P
>*)l
;
1170 const MachOSectionAndSectionClass
<P
>* right
= (MachOSectionAndSectionClass
<P
>*)r
;
1171 int64_t diff
= left
->sect
->addr() - right
->sect
->addr();
1181 struct ParserAndSectionsArray
{ Parser
* parser
; const uint32_t* sortedSectionsArray
; };
1184 Parser(const uint8_t* fileContent
, uint64_t fileLength
,
1185 const char* path
, time_t modTime
, ld::File::Ordinal ordinal
,
1186 bool warnUnwindConversionProblems
, bool keepDwarfUnwind
,
1187 bool forceDwarfConversion
, bool neverConvertDwarf
,
1188 bool verboseOptimizationHints
, bool ignoreMismatchPlatform
);
1189 ld::relocatable::File
* parse(const ParserOptions
& opts
);
1190 static uint8_t loadCommandSizeMask();
1191 bool parseLoadCommands(Options::Platform platform
, uint32_t minOSVersion
, bool simulator
, bool ignoreMismatchPlatform
);
1192 void makeSections();
1193 void prescanSymbolTable();
1194 void makeSortedSymbolsArray(uint32_t symArray
[], const uint32_t sectionArray
[]);
1195 void makeSortedSectionsArray(uint32_t array
[]);
1196 static int pointerSorter(const void* l
, const void* r
);
1197 static int symbolIndexSorter(void* extra
, const void* l
, const void* r
);
1198 static int sectionIndexSorter(void* extra
, const void* l
, const void* r
);
1200 void parseDebugInfo();
1202 void appendAliasAtoms(uint8_t* atomBuffer
);
1203 static bool isConstFunStabs(const char *stabStr
);
1204 bool read_comp_unit(const char ** name
, const char ** comp_dir
,
1205 uint64_t *stmt_list
);
1206 pint_t
realAddr(pint_t addr
);
1207 const char* getDwarfString(uint64_t form
, const uint8_t*& p
);
1208 uint64_t getDwarfOffset(uint64_t form
, const uint8_t*& di
, bool dwarf64
);
1209 bool skip_form(const uint8_t ** offset
, const uint8_t * end
,
1210 uint64_t form
, uint8_t addr_size
, bool dwarf64
);
1213 // filled in by constructor
1214 const uint8_t* _fileContent
;
1215 uint32_t _fileLength
;
1218 ld::File::Ordinal _ordinal
;
1220 // filled in by parseLoadCommands()
1222 const macho_nlist
<P
>* _symbols
;
1223 uint32_t _symbolCount
;
1224 uint32_t _indirectSymbolCount
;
1225 const char* _strings
;
1226 uint32_t _stringsSize
;
1227 const uint32_t* _indirectTable
;
1228 uint32_t _indirectTableCount
;
1229 uint32_t _undefinedStartIndex
;
1230 uint32_t _undefinedEndIndex
;
1231 const macho_section
<P
>* _sectionsStart
;
1232 uint32_t _machOSectionsCount
;
1234 macho_data_in_code_entry
<P
>* _dataInCodeStart
;
1235 macho_data_in_code_entry
<P
>* _dataInCodeEnd
;
1236 const uint8_t* _lohStart
;
1237 const uint8_t* _lohEnd
;
1239 // filled in by parse()
1240 CFISection
<A
>* _EHFrameSection
;
1241 CUSection
<A
>* _compactUnwindSection
;
1242 AbsoluteSymbolSection
<A
>* _absoluteSection
;
1243 uint32_t _tentativeDefinitionCount
;
1244 uint32_t _absoluteSymbolCount
;
1245 uint32_t _symbolsInSections
;
1246 bool _hasLongBranchStubs
;
1247 bool _AppleObjc
; // FSF has objc that uses different data layout
1248 bool _overlappingSymbols
;
1249 bool _warnUnwindConversionProblems
;
1250 bool _hasDataInCodeLabels
;
1251 bool _keepDwarfUnwind
;
1252 bool _forceDwarfConversion
;
1253 bool _neverConvertDwarf
;
1254 bool _verboseOptimizationHints
;
1255 bool _armUsesZeroCostExceptions
;
1256 bool _ignoreMismatchPlatform
;
1257 bool _treateBitcodeAsData
;
1259 unsigned int _stubsSectionNum
;
1260 const macho_section
<P
>* _stubsMachOSection
;
1261 std::vector
<const char*> _dtraceProviderInfo
;
1262 std::vector
<FixupInAtom
> _allFixups
;
1267 template <typename A
>
1268 Parser
<A
>::Parser(const uint8_t* fileContent
, uint64_t fileLength
, const char* path
, time_t modTime
,
1269 ld::File::Ordinal ordinal
, bool convertDUI
, bool keepDwarfUnwind
, bool forceDwarfConversion
,
1270 bool neverConvertDwarf
, bool verboseOptimizationHints
, bool ignoreMismatchPlatform
)
1271 : _fileContent(fileContent
), _fileLength(fileLength
), _path(path
), _modTime(modTime
),
1272 _ordinal(ordinal
), _file(NULL
),
1273 _symbols(NULL
), _symbolCount(0), _indirectSymbolCount(0), _strings(NULL
), _stringsSize(0),
1274 _indirectTable(NULL
), _indirectTableCount(0),
1275 _undefinedStartIndex(0), _undefinedEndIndex(0),
1276 _sectionsStart(NULL
), _machOSectionsCount(0), _hasUUID(false),
1277 _dataInCodeStart(NULL
), _dataInCodeEnd(NULL
),
1278 _lohStart(NULL
), _lohEnd(NULL
),
1279 _EHFrameSection(NULL
), _compactUnwindSection(NULL
), _absoluteSection(NULL
),
1280 _tentativeDefinitionCount(0), _absoluteSymbolCount(0),
1281 _symbolsInSections(0), _hasLongBranchStubs(false), _AppleObjc(false),
1282 _overlappingSymbols(false), _warnUnwindConversionProblems(convertDUI
), _hasDataInCodeLabels(false),
1283 _keepDwarfUnwind(keepDwarfUnwind
), _forceDwarfConversion(forceDwarfConversion
),
1284 _neverConvertDwarf(neverConvertDwarf
),
1285 _verboseOptimizationHints(verboseOptimizationHints
),
1286 _ignoreMismatchPlatform(ignoreMismatchPlatform
),
1287 _stubsSectionNum(0), _stubsMachOSection(NULL
)
1293 bool Parser
<x86
>::validFile(const uint8_t* fileContent
, bool, cpu_subtype_t
)
1295 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1296 if ( header
->magic() != MH_MAGIC
)
1298 if ( header
->cputype() != CPU_TYPE_I386
)
1300 if ( header
->filetype() != MH_OBJECT
)
1306 bool Parser
<x86_64
>::validFile(const uint8_t* fileContent
, bool, cpu_subtype_t
)
1308 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1309 if ( header
->magic() != MH_MAGIC_64
)
1311 if ( header
->cputype() != CPU_TYPE_X86_64
)
1313 if ( header
->filetype() != MH_OBJECT
)
1319 bool Parser
<arm
>::validFile(const uint8_t* fileContent
, bool subtypeMustMatch
, cpu_subtype_t subtype
)
1321 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1322 if ( header
->magic() != MH_MAGIC
)
1324 if ( header
->cputype() != CPU_TYPE_ARM
)
1326 if ( header
->filetype() != MH_OBJECT
)
1328 if ( subtypeMustMatch
) {
1329 if ( (cpu_subtype_t
)header
->cpusubtype() == subtype
)
1331 // hack until libcc_kext.a is made fat
1332 if ( header
->cpusubtype() == CPU_SUBTYPE_ARM_ALL
)
1341 bool Parser
<arm64
>::validFile(const uint8_t* fileContent
, bool subtypeMustMatch
, cpu_subtype_t subtype
)
1343 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1344 if ( header
->magic() != MH_MAGIC_64
)
1346 if ( header
->cputype() != CPU_TYPE_ARM64
)
1348 if ( header
->filetype() != MH_OBJECT
)
1355 const char* Parser
<x86
>::fileKind(const uint8_t* fileContent
)
1357 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1358 if ( header
->magic() != MH_MAGIC
)
1360 if ( header
->cputype() != CPU_TYPE_I386
)
1366 const char* Parser
<x86_64
>::fileKind(const uint8_t* fileContent
)
1368 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1369 if ( header
->magic() != MH_MAGIC_64
)
1371 if ( header
->cputype() != CPU_TYPE_X86_64
)
1377 const char* Parser
<arm
>::fileKind(const uint8_t* fileContent
)
1379 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1380 if ( header
->magic() != MH_MAGIC
)
1382 if ( header
->cputype() != CPU_TYPE_ARM
)
1384 for (const ArchInfo
* t
=archInfoArray
; t
->archName
!= NULL
; ++t
) {
1385 if ( (t
->cpuType
== CPU_TYPE_ARM
) && ((cpu_subtype_t
)header
->cpusubtype() == t
->cpuSubType
) ) {
1392 #if SUPPORT_ARCH_arm64
1394 const char* Parser
<arm64
>::fileKind(const uint8_t* fileContent
)
1396 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1397 if ( header
->magic() != MH_MAGIC_64
)
1399 if ( header
->cputype() != CPU_TYPE_ARM64
)
1405 template <typename A
>
1406 bool Parser
<A
>::hasObjC2Categories(const uint8_t* fileContent
)
1408 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1409 const uint32_t cmd_count
= header
->ncmds();
1410 const macho_load_command
<P
>* const cmds
= (macho_load_command
<P
>*)((char*)header
+ sizeof(macho_header
<P
>));
1411 const macho_load_command
<P
>* const cmdsEnd
= (macho_load_command
<P
>*)((char*)header
+ sizeof(macho_header
<P
>) + header
->sizeofcmds());
1412 const macho_load_command
<P
>* cmd
= cmds
;
1413 for (uint32_t i
= 0; i
< cmd_count
; ++i
) {
1414 if ( cmd
->cmd() == macho_segment_command
<P
>::CMD
) {
1415 const macho_segment_command
<P
>* segment
= (macho_segment_command
<P
>*)cmd
;
1416 const macho_section
<P
>* sectionsStart
= (macho_section
<P
>*)((char*)segment
+ sizeof(macho_segment_command
<P
>));
1417 for (uint32_t si
=0; si
< segment
->nsects(); ++si
) {
1418 const macho_section
<P
>* sect
= §ionsStart
[si
];
1419 if ( (sect
->size() > 0)
1420 && (strcmp(sect
->sectname(), "__objc_catlist") == 0)
1421 && (strcmp(sect
->segname(), "__DATA") == 0) ) {
1426 cmd
= (const macho_load_command
<P
>*)(((char*)cmd
)+cmd
->cmdsize());
1427 if ( cmd
> cmdsEnd
)
1428 throwf("malformed mach-o file, load command #%d is outside size of load commands", i
);
1434 template <typename A
>
1435 bool Parser
<A
>::hasObjC1Categories(const uint8_t* fileContent
)
1437 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1438 const uint32_t cmd_count
= header
->ncmds();
1439 const macho_load_command
<P
>* const cmds
= (macho_load_command
<P
>*)((char*)header
+ sizeof(macho_header
<P
>));
1440 const macho_load_command
<P
>* const cmdsEnd
= (macho_load_command
<P
>*)((char*)header
+ sizeof(macho_header
<P
>) + header
->sizeofcmds());
1441 const macho_load_command
<P
>* cmd
= cmds
;
1442 for (uint32_t i
= 0; i
< cmd_count
; ++i
) {
1443 if ( cmd
->cmd() == macho_segment_command
<P
>::CMD
) {
1444 const macho_segment_command
<P
>* segment
= (macho_segment_command
<P
>*)cmd
;
1445 const macho_section
<P
>* sectionsStart
= (macho_section
<P
>*)((char*)segment
+ sizeof(macho_segment_command
<P
>));
1446 for (uint32_t si
=0; si
< segment
->nsects(); ++si
) {
1447 const macho_section
<P
>* sect
= §ionsStart
[si
];
1448 if ( (sect
->size() > 0)
1449 && (strcmp(sect
->sectname(), "__category") == 0)
1450 && (strcmp(sect
->segname(), "__OBJC") == 0) ) {
1455 cmd
= (const macho_load_command
<P
>*)(((char*)cmd
)+cmd
->cmdsize());
1456 if ( cmd
> cmdsEnd
)
1457 throwf("malformed mach-o file, load command #%d is outside size of load commands", i
);
1463 template <typename A
>
1464 bool Parser
<A
>::getNonLocalSymbols(const uint8_t* fileContent
, std::vector
<const char*> &syms
)
1466 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1467 const uint32_t cmd_count
= header
->ncmds();
1468 const macho_load_command
<P
>* const cmds
= (macho_load_command
<P
>*)((char*)header
+ sizeof(macho_header
<P
>));
1469 const macho_load_command
<P
>* const cmdsEnd
= (macho_load_command
<P
>*)((char*)header
+ sizeof(macho_header
<P
>) + header
->sizeofcmds());
1470 const macho_load_command
<P
>* cmd
= cmds
;
1471 for (uint32_t i
= 0; i
< cmd_count
; ++i
) {
1472 if ( cmd
->cmd() == LC_SYMTAB
) {
1473 const macho_symtab_command
<P
>* symtab
= (macho_symtab_command
<P
>*)cmd
;
1474 uint32_t symbolCount
= symtab
->nsyms();
1475 const macho_nlist
<P
>* symbols
= (const macho_nlist
<P
>*)(fileContent
+ symtab
->symoff());
1476 const char* strings
= (char*)fileContent
+ symtab
->stroff();
1477 for (uint32_t i
= 0; i
< symbolCount
; ++i
) {
1478 // ignore stabs and count only ext symbols
1479 if ( (symbols
[i
].n_type() & N_STAB
) == 0 &&
1480 (symbols
[i
].n_type() & N_EXT
) != 0 ) {
1481 const char* symName
= &strings
[symbols
[i
].n_strx()];
1482 syms
.push_back(symName
);
1487 cmd
= (const macho_load_command
<P
>*)(((char*)cmd
)+cmd
->cmdsize());
1488 if ( cmd
> cmdsEnd
)
1489 throwf("malformed mach-o file, load command #%d is outside size of load commands", i
);
1495 template <typename A
>
1496 int Parser
<A
>::pointerSorter(const void* l
, const void* r
)
1498 // sort references by address
1499 const pint_t
* left
= (pint_t
*)l
;
1500 const pint_t
* right
= (pint_t
*)r
;
1501 return (*left
- *right
);
1504 template <typename A
>
1505 typename
A::P::uint_t Parser
<A
>::LabelAndCFIBreakIterator::peek(Parser
<A
>& parser
, pint_t startAddr
, pint_t endAddr
)
1508 if ( symIndex
< sortedSymbolCount
)
1509 symbolAddr
= parser
.symbolFromIndex(sortedSymbolIndexes
[symIndex
]).n_value();
1511 symbolAddr
= endAddr
;
1513 if ( cfiIndex
< cfiStartsCount
)
1514 cfiAddr
= cfiStartsArray
[cfiIndex
];
1517 if ( (cfiAddr
< symbolAddr
) && (cfiAddr
>= startAddr
) ) {
1518 if ( cfiAddr
< endAddr
)
1524 if ( symbolAddr
< endAddr
)
1532 // Parses up a section into chunks based on labels and CFI information.
1533 // Each call returns the next chunk address and size, and (if the break
1534 // was becuase of a label, the symbol). Returns false when no more chunks.
1536 template <typename A
>
1537 bool Parser
<A
>::LabelAndCFIBreakIterator::next(Parser
<A
>& parser
, const Section
<A
>& sect
, uint32_t sectNum
, pint_t startAddr
, pint_t endAddr
,
1538 pint_t
* addr
, pint_t
* size
, const macho_nlist
<P
>** symbol
)
1540 // may not be a label on start of section, but need atom demarcation there
1543 // advance symIndex until we get to the first label at or past the start of this section
1544 while ( symIndex
< sortedSymbolCount
) {
1545 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(sortedSymbolIndexes
[symIndex
]);
1546 if ( ! sect
.ignoreLabel(parser
.nameFromSymbol(sym
)) ) {
1547 pint_t nextSymbolAddr
= sym
.n_value();
1548 //fprintf(stderr, "sectNum=%d, nextSymbolAddr=0x%08llX, name=%s\n", sectNum, (uint64_t)nextSymbolAddr, parser.nameFromSymbol(sym));
1549 if ( (nextSymbolAddr
> startAddr
) || ((nextSymbolAddr
== startAddr
) && (sym
.n_sect() == sectNum
)) )
1554 if ( symIndex
< sortedSymbolCount
) {
1555 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(sortedSymbolIndexes
[symIndex
]);
1556 pint_t nextSymbolAddr
= sym
.n_value();
1557 // if next symbol found is not in this section
1558 if ( sym
.n_sect() != sectNum
) {
1559 // check for CFI break instead of symbol break
1560 if ( cfiIndex
< cfiStartsCount
) {
1561 pint_t nextCfiAddr
= cfiStartsArray
[cfiIndex
];
1562 if ( nextCfiAddr
< endAddr
) {
1565 *addr
= nextCfiAddr
;
1566 *size
= peek(parser
, startAddr
, endAddr
) - nextCfiAddr
;
1572 *size
= endAddr
- startAddr
;
1574 if ( startAddr
== endAddr
)
1575 return false; // zero size section
1577 return true; // whole section is one atom with no label
1579 // if also CFI break here, eat it
1580 if ( cfiIndex
< cfiStartsCount
) {
1581 if ( cfiStartsArray
[cfiIndex
] == nextSymbolAddr
)
1584 if ( nextSymbolAddr
== startAddr
) {
1585 // label at start of section, return it as chunk
1588 *size
= peek(parser
, startAddr
, endAddr
) - startAddr
;
1592 // return chunk before first symbol
1594 *size
= nextSymbolAddr
- startAddr
;
1598 // no symbols in section, check CFI
1599 if ( cfiIndex
< cfiStartsCount
) {
1600 pint_t nextCfiAddr
= cfiStartsArray
[cfiIndex
];
1601 if ( nextCfiAddr
< endAddr
) {
1604 *addr
= nextCfiAddr
;
1605 *size
= peek(parser
, startAddr
, endAddr
) - nextCfiAddr
;
1610 // no cfi, so whole section is one chunk
1612 *size
= endAddr
- startAddr
;
1614 if ( startAddr
== endAddr
)
1615 return false; // zero size section
1617 return true; // whole section is one atom with no label
1620 while ( (symIndex
< sortedSymbolCount
) && (cfiIndex
< cfiStartsCount
) ) {
1621 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(sortedSymbolIndexes
[symIndex
]);
1622 pint_t nextSymbolAddr
= sym
.n_value();
1623 pint_t nextCfiAddr
= cfiStartsArray
[cfiIndex
];
1624 if ( nextSymbolAddr
< nextCfiAddr
) {
1625 if ( nextSymbolAddr
>= endAddr
)
1628 if ( nextSymbolAddr
< startAddr
)
1630 *addr
= nextSymbolAddr
;
1631 *size
= peek(parser
, startAddr
, endAddr
) - nextSymbolAddr
;
1635 else if ( nextCfiAddr
< nextSymbolAddr
) {
1636 if ( nextCfiAddr
>= endAddr
)
1639 if ( nextCfiAddr
< startAddr
)
1641 *addr
= nextCfiAddr
;
1642 *size
= peek(parser
, startAddr
, endAddr
) - nextCfiAddr
;
1647 if ( nextCfiAddr
>= endAddr
)
1651 if ( nextCfiAddr
< startAddr
)
1653 *addr
= nextCfiAddr
;
1654 *size
= peek(parser
, startAddr
, endAddr
) - nextCfiAddr
;
1659 while ( symIndex
< sortedSymbolCount
) {
1660 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(sortedSymbolIndexes
[symIndex
]);
1661 pint_t nextSymbolAddr
= sym
.n_value();
1662 // if next symbol found is not in this section, then done with iteration
1663 if ( sym
.n_sect() != sectNum
)
1666 if ( nextSymbolAddr
< startAddr
)
1668 *addr
= nextSymbolAddr
;
1669 *size
= peek(parser
, startAddr
, endAddr
) - nextSymbolAddr
;
1673 while ( cfiIndex
< cfiStartsCount
) {
1674 pint_t nextCfiAddr
= cfiStartsArray
[cfiIndex
];
1675 if ( nextCfiAddr
>= endAddr
)
1678 if ( nextCfiAddr
< startAddr
)
1680 *addr
= nextCfiAddr
;
1681 *size
= peek(parser
, startAddr
, endAddr
) - nextCfiAddr
;
1689 typename
arm::P::uint_t Parser
<arm
>::realAddr(typename
arm::P::uint_t addr
)
1694 template <typename A
>
1695 typename
A::P::uint_t Parser
<A
>::realAddr(typename
A::P::uint_t addr
)
1700 #define STACK_ALLOC_IF_SMALL(_type, _name, _actual_count, _maxCount) \
1701 _type* _name = NULL; \
1702 uint32_t _name##_count = 1; \
1703 if ( _actual_count > _maxCount ) \
1704 _name = (_type*)malloc(sizeof(_type) * _actual_count); \
1706 _name##_count = _actual_count; \
1707 _type _name##_buffer[_name##_count]; \
1708 if ( _name == NULL ) \
1709 _name = _name##_buffer;
1712 template <typename A
>
1713 ld::relocatable::File
* Parser
<A
>::parse(const ParserOptions
& opts
)
1715 // create file object
1716 _file
= new File
<A
>(_path
, _modTime
, _fileContent
, _ordinal
);
1719 _file
->_srcKind
= opts
.srcKind
;
1720 // set treatBitcodeAsData
1721 _treateBitcodeAsData
= opts
.treateBitcodeAsData
;
1722 _usingBitcode
= opts
.usingBitcode
;
1724 // respond to -t option
1725 if ( opts
.logAllFiles
)
1726 printf("%s\n", _path
);
1728 _armUsesZeroCostExceptions
= opts
.armUsesZeroCostExceptions
;
1730 // parse start of mach-o file
1731 if ( ! parseLoadCommands(opts
.platform
, opts
.minOSVersion
, opts
.simulator
, opts
.ignoreMismatchPlatform
) )
1735 uint32_t sortedSectionIndexes
[_machOSectionsCount
];
1736 this->makeSortedSectionsArray(sortedSectionIndexes
);
1738 // make symbol table sorted by address
1739 this->prescanSymbolTable();
1740 uint32_t sortedSymbolIndexes
[_symbolsInSections
];
1741 this->makeSortedSymbolsArray(sortedSymbolIndexes
, sortedSectionIndexes
);
1743 // allocate Section<A> object for each mach-o section
1746 // if it exists, do special early parsing of __compact_unwind section
1747 uint32_t countOfCUs
= 0;
1748 if ( _compactUnwindSection
!= NULL
)
1749 countOfCUs
= _compactUnwindSection
->count();
1750 // stack allocate (if not too large) cuInfoBuffer
1751 STACK_ALLOC_IF_SMALL(typename CUSection
<A
>::Info
, cuInfoArray
, countOfCUs
, 1024);
1752 if ( countOfCUs
!= 0 )
1753 _compactUnwindSection
->parse(*this, countOfCUs
, cuInfoArray
);
1755 // create lists of address that already have compact unwind and thus don't need the dwarf parsed
1756 unsigned cuLsdaCount
= 0;
1757 pint_t cuStarts
[countOfCUs
];
1758 for (uint32_t i
=0; i
< countOfCUs
; ++i
) {
1759 if ( CUSection
<A
>::encodingMeansUseDwarf(cuInfoArray
[i
].compactUnwindInfo
) )
1762 cuStarts
[i
] = cuInfoArray
[i
].functionStartAddress
;
1763 if ( cuInfoArray
[i
].lsdaAddress
!= 0 )
1768 // if it exists, do special early parsing of __eh_frame section
1769 // stack allocate (if not too large) array of CFI_Atom_Info
1770 uint32_t countOfCFIs
= 0;
1771 if ( _EHFrameSection
!= NULL
)
1772 countOfCFIs
= _EHFrameSection
->cfiCount(*this);
1773 STACK_ALLOC_IF_SMALL(typename CFISection
<A
>::CFI_Atom_Info
, cfiArray
, countOfCFIs
, 1024);
1775 // stack allocate (if not too large) a copy of __eh_frame to apply relocations to
1776 uint32_t sectSize
= 4;
1777 if ( (countOfCFIs
!= 0) && _EHFrameSection
->needsRelocating() )
1778 sectSize
= _EHFrameSection
->machoSection()->size()+4;
1779 STACK_ALLOC_IF_SMALL(uint8_t, ehBuffer
, sectSize
, 50*1024);
1780 uint32_t cfiStartsCount
= 0;
1781 if ( countOfCFIs
!= 0 ) {
1782 _EHFrameSection
->cfiParse(*this, ehBuffer
, cfiArray
, countOfCFIs
, cuStarts
, countOfCUs
);
1783 // count functions and lsdas
1784 for(uint32_t i
=0; i
< countOfCFIs
; ++i
) {
1785 if ( cfiArray
[i
].isCIE
)
1787 //fprintf(stderr, "cfiArray[i].func = 0x%08llX, cfiArray[i].lsda = 0x%08llX, encoding=0x%08X\n",
1788 // (uint64_t)cfiArray[i].u.fdeInfo.function.targetAddress,
1789 // (uint64_t)cfiArray[i].u.fdeInfo.lsda.targetAddress,
1790 // cfiArray[i].u.fdeInfo.compactUnwindInfo);
1791 if ( cfiArray
[i
].u
.fdeInfo
.function
.targetAddress
!= CFI_INVALID_ADDRESS
)
1793 if ( cfiArray
[i
].u
.fdeInfo
.lsda
.targetAddress
!= CFI_INVALID_ADDRESS
)
1797 CFI_CU_InfoArrays
cfis(cfiArray
, countOfCFIs
, cuInfoArray
, countOfCUs
);
1799 // create sorted array of function starts and lsda starts
1800 pint_t cfiStartsArray
[cfiStartsCount
+cuLsdaCount
];
1801 uint32_t countOfFDEs
= 0;
1802 uint32_t cfiStartsArrayCount
= 0;
1803 if ( countOfCFIs
!= 0 ) {
1804 for(uint32_t i
=0; i
< countOfCFIs
; ++i
) {
1805 if ( cfiArray
[i
].isCIE
)
1807 if ( cfiArray
[i
].u
.fdeInfo
.function
.targetAddress
!= CFI_INVALID_ADDRESS
)
1808 cfiStartsArray
[cfiStartsArrayCount
++] = realAddr(cfiArray
[i
].u
.fdeInfo
.function
.targetAddress
);
1809 if ( cfiArray
[i
].u
.fdeInfo
.lsda
.targetAddress
!= CFI_INVALID_ADDRESS
)
1810 cfiStartsArray
[cfiStartsArrayCount
++] = cfiArray
[i
].u
.fdeInfo
.lsda
.targetAddress
;
1814 if ( cuLsdaCount
!= 0 ) {
1815 // merge in an lsda info from compact unwind
1816 for (uint32_t i
=0; i
< countOfCUs
; ++i
) {
1817 if ( cuInfoArray
[i
].lsdaAddress
== 0 )
1819 // append to cfiStartsArray if not already in that list
1821 for(uint32_t j
=0; j
< cfiStartsArrayCount
; ++j
) {
1822 if ( cfiStartsArray
[j
] == cuInfoArray
[i
].lsdaAddress
)
1826 cfiStartsArray
[cfiStartsArrayCount
++] = cuInfoArray
[i
].lsdaAddress
;
1830 if ( cfiStartsArrayCount
!= 0 ) {
1831 ::qsort(cfiStartsArray
, cfiStartsArrayCount
, sizeof(pint_t
), pointerSorter
);
1833 // scan for FDEs claming the same function
1834 for(uint32_t i
=1; i
< cfiStartsArrayCount
; ++i
) {
1835 assert( cfiStartsArray
[i
] != cfiStartsArray
[i
-1] );
1840 Section
<A
>** sections
= _file
->_sectionsArray
;
1841 uint32_t sectionsCount
= _file
->_sectionsArrayCount
;
1843 // figure out how many atoms will be allocated and allocate
1844 LabelAndCFIBreakIterator
breakIterator(sortedSymbolIndexes
, _symbolsInSections
, cfiStartsArray
,
1845 cfiStartsArrayCount
, _overlappingSymbols
);
1846 uint32_t computedAtomCount
= 0;
1847 for (uint32_t i
=0; i
< sectionsCount
; ++i
) {
1848 breakIterator
.beginSection();
1849 uint32_t count
= sections
[i
]->computeAtomCount(*this, breakIterator
, cfis
);
1850 //const macho_section<P>* sect = sections[i]->machoSection();
1851 //fprintf(stderr, "computed count=%u for section %s size=%llu\n", count, sect->sectname(), (sect != NULL) ? sect->size() : 0);
1852 computedAtomCount
+= count
;
1854 //fprintf(stderr, "allocating %d atoms * sizeof(Atom<A>)=%ld, sizeof(ld::Atom)=%ld\n", computedAtomCount, sizeof(Atom<A>), sizeof(ld::Atom));
1855 _file
->_atomsArray
= new uint8_t[computedAtomCount
*sizeof(Atom
<A
>)];
1856 _file
->_atomsArrayCount
= 0;
1858 // have each section append atoms to _atomsArray
1859 LabelAndCFIBreakIterator
breakIterator2(sortedSymbolIndexes
, _symbolsInSections
, cfiStartsArray
,
1860 cfiStartsArrayCount
, _overlappingSymbols
);
1861 for (uint32_t i
=0; i
< sectionsCount
; ++i
) {
1862 uint8_t* atoms
= _file
->_atomsArray
+ _file
->_atomsArrayCount
*sizeof(Atom
<A
>);
1863 breakIterator2
.beginSection();
1864 uint32_t count
= sections
[i
]->appendAtoms(*this, atoms
, breakIterator2
, cfis
);
1865 //fprintf(stderr, "append count=%u for section %s/%s\n", count, sections[i]->machoSection()->segname(), sections[i]->machoSection()->sectname());
1866 _file
->_atomsArrayCount
+= count
;
1868 assert( _file
->_atomsArrayCount
== computedAtomCount
&& "more atoms allocated than expected");
1871 // have each section add all fix-ups for its atoms
1872 _allFixups
.reserve(computedAtomCount
*5);
1873 for (uint32_t i
=0; i
< sectionsCount
; ++i
)
1874 sections
[i
]->makeFixups(*this, cfis
);
1876 // assign fixups start offset for each atom
1877 uint8_t* p
= _file
->_atomsArray
;
1878 uint32_t fixupOffset
= 0;
1879 for(int i
=_file
->_atomsArrayCount
; i
> 0; --i
) {
1880 Atom
<A
>* atom
= (Atom
<A
>*)p
;
1881 atom
->_fixupsStartIndex
= fixupOffset
;
1882 fixupOffset
+= atom
->_fixupsCount
;
1883 atom
->_fixupsCount
= 0;
1884 p
+= sizeof(Atom
<A
>);
1886 assert(fixupOffset
== _allFixups
.size());
1887 _file
->_fixups
.resize(fixupOffset
);
1889 // copy each fixup for each atom
1890 for(typename
std::vector
<FixupInAtom
>::iterator it
=_allFixups
.begin(); it
!= _allFixups
.end(); ++it
) {
1891 uint32_t slot
= it
->atom
->_fixupsStartIndex
+ it
->atom
->_fixupsCount
;
1892 _file
->_fixups
[slot
] = it
->fixup
;
1893 it
->atom
->_fixupsCount
++;
1896 // done with temp vector
1900 _file
->_unwindInfos
.reserve(countOfFDEs
+countOfCUs
);
1901 for(uint32_t i
=0; i
< countOfCFIs
; ++i
) {
1902 if ( cfiArray
[i
].isCIE
)
1904 if ( cfiArray
[i
].u
.fdeInfo
.function
.targetAddress
!= CFI_INVALID_ADDRESS
) {
1905 ld::Atom::UnwindInfo info
;
1906 info
.startOffset
= 0;
1907 info
.unwindInfo
= cfiArray
[i
].u
.fdeInfo
.compactUnwindInfo
;
1908 _file
->_unwindInfos
.push_back(info
);
1909 Atom
<A
>* func
= findAtomByAddress(cfiArray
[i
].u
.fdeInfo
.function
.targetAddress
);
1910 func
->setUnwindInfoRange(_file
->_unwindInfos
.size()-1, 1);
1911 //fprintf(stderr, "cu from dwarf =0x%08X, atom=%s\n", info.unwindInfo, func->name());
1914 // apply compact infos in __LD,__compact_unwind section to each function
1915 // if function also has dwarf unwind, CU will override it
1916 Atom
<A
>* lastFunc
= NULL
;
1917 uint32_t lastEnd
= 0;
1918 for(uint32_t i
=0; i
< countOfCUs
; ++i
) {
1919 typename CUSection
<A
>::Info
* info
= &cuInfoArray
[i
];
1920 assert(info
->function
!= NULL
);
1921 ld::Atom::UnwindInfo ui
;
1922 ui
.startOffset
= info
->functionStartAddress
- info
->function
->objectAddress();
1923 ui
.unwindInfo
= info
->compactUnwindInfo
;
1924 _file
->_unwindInfos
.push_back(ui
);
1925 // don't override with converted cu with "use dwarf" cu, if forcing dwarf conversion
1926 if ( !_forceDwarfConversion
|| !CUSection
<A
>::encodingMeansUseDwarf(info
->compactUnwindInfo
) ) {
1927 //fprintf(stderr, "cu=0x%08X, atom=%s\n", ui.unwindInfo, info->function->name());
1928 // if previous is for same function, extend range
1929 if ( info
->function
== lastFunc
) {
1930 if ( lastEnd
!= ui
.startOffset
) {
1931 if ( lastEnd
< ui
.startOffset
)
1932 warning("__LD,__compact_unwind entries for %s have a gap at offset 0x%0X", info
->function
->name(), lastEnd
);
1934 warning("__LD,__compact_unwind entries for %s overlap at offset 0x%0X", info
->function
->name(), lastEnd
);
1936 lastFunc
->extendUnwindInfoRange();
1939 info
->function
->setUnwindInfoRange(_file
->_unwindInfos
.size()-1, 1);
1940 lastFunc
= info
->function
;
1941 lastEnd
= ui
.startOffset
+ info
->rangeLength
;
1945 // process indirect symbols which become AliasAtoms
1946 _file
->_aliasAtomsArray
= NULL
;
1947 _file
->_aliasAtomsArrayCount
= 0;
1948 if ( _indirectSymbolCount
!= 0 ) {
1949 _file
->_aliasAtomsArrayCount
= _indirectSymbolCount
;
1950 _file
->_aliasAtomsArray
= new uint8_t[_file
->_aliasAtomsArrayCount
*sizeof(AliasAtom
)];
1951 this->appendAliasAtoms(_file
->_aliasAtomsArray
);
1955 // parse dwarf debug info to get line info
1956 this->parseDebugInfo();
1961 static void versionToString(uint32_t value
, char buffer
[32])
1964 sprintf(buffer
, "%d.%d.%d", value
>> 16, (value
>> 8) & 0xFF, value
& 0xFF);
1966 sprintf(buffer
, "%d.%d", value
>> 16, (value
>> 8) & 0xFF);
1969 template <> uint8_t Parser
<x86
>::loadCommandSizeMask() { return 0x03; }
1970 template <> uint8_t Parser
<x86_64
>::loadCommandSizeMask() { return 0x07; }
1971 template <> uint8_t Parser
<arm
>::loadCommandSizeMask() { return 0x03; }
1972 template <> uint8_t Parser
<arm64
>::loadCommandSizeMask() { return 0x07; }
1974 template <typename A
>
1975 bool Parser
<A
>::parseLoadCommands(Options::Platform platform
, uint32_t linkMinOSVersion
, bool simulator
, bool ignoreMismatchPlatform
)
1977 const macho_header
<P
>* header
= (const macho_header
<P
>*)_fileContent
;
1979 // set File attributes
1980 _file
->_canScatterAtoms
= (header
->flags() & MH_SUBSECTIONS_VIA_SYMBOLS
);
1981 _file
->_cpuSubType
= header
->cpusubtype();
1983 const macho_segment_command
<P
>* segment
= NULL
;
1984 const uint8_t* const endOfFile
= _fileContent
+ _fileLength
;
1985 const uint32_t cmd_count
= header
->ncmds();
1986 // <rdar://problem/5394172> an empty .o file with zero load commands will crash linker
1987 if ( cmd_count
== 0 )
1989 Options::Platform lcPlatform
= Options::kPlatformUnknown
;
1990 const macho_load_command
<P
>* const cmds
= (macho_load_command
<P
>*)((char*)header
+ sizeof(macho_header
<P
>));
1991 const macho_load_command
<P
>* const cmdsEnd
= (macho_load_command
<P
>*)((char*)header
+ sizeof(macho_header
<P
>) + header
->sizeofcmds());
1992 const macho_load_command
<P
>* cmd
= cmds
;
1993 for (uint32_t i
= 0; i
< cmd_count
; ++i
) {
1994 uint32_t size
= cmd
->cmdsize();
1995 if ( (size
& this->loadCommandSizeMask()) != 0 )
1996 throwf("load command #%d has a unaligned size", i
);
1997 const uint8_t* endOfCmd
= ((uint8_t*)cmd
)+cmd
->cmdsize();
1998 if ( endOfCmd
> (uint8_t*)cmdsEnd
)
1999 throwf("load command #%d extends beyond the end of the load commands", i
);
2000 if ( endOfCmd
> endOfFile
)
2001 throwf("load command #%d extends beyond the end of the file", i
);
2002 switch (cmd
->cmd()) {
2005 const macho_symtab_command
<P
>* symtab
= (macho_symtab_command
<P
>*)cmd
;
2006 _symbolCount
= symtab
->nsyms();
2007 _symbols
= (const macho_nlist
<P
>*)(_fileContent
+ symtab
->symoff());
2008 _strings
= (char*)_fileContent
+ symtab
->stroff();
2009 _stringsSize
= symtab
->strsize();
2010 if ( (symtab
->symoff() + _symbolCount
*sizeof(macho_nlist
<P
>)) > _fileLength
)
2011 throw "mach-o symbol table extends beyond end of file";
2012 if ( (_strings
+ _stringsSize
) > (char*)endOfFile
)
2013 throw "mach-o string pool extends beyond end of file";
2014 if ( _indirectTable
== NULL
) {
2015 if ( _undefinedEndIndex
== 0 ) {
2016 _undefinedStartIndex
= 0;
2017 _undefinedEndIndex
= symtab
->nsyms();
2024 const macho_dysymtab_command
<P
>* dsymtab
= (macho_dysymtab_command
<P
>*)cmd
;
2025 _indirectTable
= (uint32_t*)(_fileContent
+ dsymtab
->indirectsymoff());
2026 _indirectTableCount
= dsymtab
->nindirectsyms();
2027 if ( &_indirectTable
[_indirectTableCount
] > (uint32_t*)endOfFile
)
2028 throw "indirect symbol table extends beyond end of file";
2029 _undefinedStartIndex
= dsymtab
->iundefsym();
2030 _undefinedEndIndex
= _undefinedStartIndex
+ dsymtab
->nundefsym();
2036 case LC_DATA_IN_CODE
:
2038 const macho_linkedit_data_command
<P
>* dc
= (macho_linkedit_data_command
<P
>*)cmd
;
2039 _dataInCodeStart
= (macho_data_in_code_entry
<P
>*)(_fileContent
+ dc
->dataoff());
2040 _dataInCodeEnd
= (macho_data_in_code_entry
<P
>*)(_fileContent
+ dc
->dataoff() + dc
->datasize());
2041 if ( _dataInCodeEnd
> (macho_data_in_code_entry
<P
>*)endOfFile
)
2042 throw "LC_DATA_IN_CODE table extends beyond end of file";
2045 case LC_LINKER_OPTION
:
2047 const macho_linker_option_command
<P
>* loc
= (macho_linker_option_command
<P
>*)cmd
;
2048 const char* buffer
= loc
->buffer();
2049 _file
->_linkerOptions
.resize(_file
->_linkerOptions
.size() + 1);
2050 std::vector
<const char*>& vec
= _file
->_linkerOptions
.back();
2051 for (uint32_t j
=0; j
< loc
->count(); ++j
) {
2052 vec
.push_back(buffer
);
2053 buffer
+= strlen(buffer
) + 1;
2055 if ( buffer
> ((char*)cmd
+ loc
->cmdsize()) )
2056 throw "malformed LC_LINKER_OPTION";
2059 case LC_LINKER_OPTIMIZATION_HINTS
:
2061 const macho_linkedit_data_command
<P
>* loh
= (macho_linkedit_data_command
<P
>*)cmd
;
2062 _lohStart
= _fileContent
+ loh
->dataoff();
2063 _lohEnd
= _fileContent
+ loh
->dataoff() + loh
->datasize();
2064 if ( _lohEnd
> endOfFile
)
2065 throw "LC_LINKER_OPTIMIZATION_HINTS table extends beyond end of file";
2068 case LC_VERSION_MIN_MACOSX
:
2069 case LC_VERSION_MIN_IPHONEOS
:
2070 case LC_VERSION_MIN_WATCHOS
:
2071 #if SUPPORT_APPLE_TV
2072 case LC_VERSION_MIN_TVOS
:
2074 if ( ignoreMismatchPlatform
)
2076 _file
->_platform
= cmd
->cmd();
2077 lcPlatform
= Options::platformForLoadCommand(cmd
->cmd());
2078 _file
->_minOSVersion
= ((macho_version_min_command
<P
>*)cmd
)->version();
2081 if ( cmd
->cmd() == macho_segment_command
<P
>::CMD
) {
2082 if ( segment
!= NULL
)
2083 throw "more than one LC_SEGMENT found in object file";
2084 segment
= (macho_segment_command
<P
>*)cmd
;
2088 cmd
= (const macho_load_command
<P
>*)(((char*)cmd
)+cmd
->cmdsize());
2089 if ( cmd
> cmdsEnd
)
2090 throwf("malformed mach-o file, load command #%d is outside size of load commands", i
);
2092 // arm/arm64 objects are default to ios platform if not set.
2093 // rdar://problem/21746314
2094 if (lcPlatform
== Options::kPlatformUnknown
&&
2095 (std::is_same
<A
, arm
>::value
|| std::is_same
<A
, arm64
>::value
))
2096 lcPlatform
= Options::kPlatformiOS
;
2098 // Check platform cross-linking.
2099 if ( !ignoreMismatchPlatform
) {
2100 if ( lcPlatform
!= platform
) {
2102 case Options::kPlatformOSX
:
2103 case Options::kPlatformiOS
:
2104 if ( lcPlatform
== Options::kPlatformUnknown
)
2106 // fall through if the Platform is not Unknown
2107 case Options::kPlatformWatchOS
:
2108 // WatchOS errors on cross-linking all the time.
2109 throwf("building for %s%s, but linking in object file built for %s,",
2110 Options::platformName(platform
), (simulator
? " simulator" : ""),
2111 Options::platformName(lcPlatform
));
2113 #if SUPPORT_APPLE_TV
2114 case Options::kPlatform_tvOS
:
2115 // Error when using bitcocde, warning otherwise.
2117 throwf("building for %s%s, but linking in object file built for %s,",
2118 Options::platformName(platform
), (simulator
? " simulator" : ""),
2119 Options::platformName(lcPlatform
));
2121 warning("URGENT: building for %s%s, but linking in object file (%s) built for %s. "
2122 "Note: This will be an error in the future.",
2123 Options::platformName(platform
), (simulator
? " simulator" : ""), path(),
2124 Options::platformName(lcPlatform
));
2127 case Options::kPlatformUnknown
:
2128 // skip if the target platform is unknown
2132 if ( linkMinOSVersion
&& (_file
->_minOSVersion
> linkMinOSVersion
) ) {
2135 versionToString(_file
->_minOSVersion
, t1
);
2136 versionToString(linkMinOSVersion
, t2
);
2137 warning("object file (%s) was built for newer %s version (%s) than being linked (%s)",
2138 _path
, Options::platformName(lcPlatform
), t1
, t2
);
2143 // record range of sections
2144 if ( segment
== NULL
)
2145 throw "missing LC_SEGMENT";
2146 _sectionsStart
= (macho_section
<P
>*)((char*)segment
+ sizeof(macho_segment_command
<P
>));
2147 _machOSectionsCount
= segment
->nsects();
2152 template <typename A
>
2153 Options::Platform Parser
<A
>::findPlatform(const macho_header
<P
>* header
)
2155 const uint32_t cmd_count
= header
->ncmds();
2156 if ( cmd_count
== 0 )
2157 return Options::kPlatformUnknown
;
2158 const macho_load_command
<P
>* const cmds
= (macho_load_command
<P
>*)((char*)header
+ sizeof(macho_header
<P
>));
2159 const macho_load_command
<P
>* const cmdsEnd
= (macho_load_command
<P
>*)((char*)header
+ sizeof(macho_header
<P
>) + header
->sizeofcmds());
2160 const macho_load_command
<P
>* cmd
= cmds
;
2161 for (uint32_t i
= 0; i
< cmd_count
; ++i
) {
2162 uint32_t size
= cmd
->cmdsize();
2163 if ( (size
& loadCommandSizeMask()) != 0 )
2164 throwf("load command #%d has a unaligned size", i
);
2165 const uint8_t* endOfCmd
= ((uint8_t*)cmd
)+cmd
->cmdsize();
2166 if ( endOfCmd
> (uint8_t*)cmdsEnd
)
2167 throwf("load command #%d extends beyond the end of the load commands", i
);
2168 switch (cmd
->cmd()) {
2169 case LC_VERSION_MIN_MACOSX
:
2170 return Options::kPlatformOSX
;
2171 case LC_VERSION_MIN_IPHONEOS
:
2172 return Options::kPlatformiOS
;
2174 cmd
= (const macho_load_command
<P
>*)(((char*)cmd
)+cmd
->cmdsize());
2175 if ( cmd
> cmdsEnd
)
2176 throwf("malformed mach-o file, load command #%d is outside size of load commands", i
);
2178 return Options::kPlatformUnknown
;
2182 template <typename A
>
2183 void Parser
<A
>::prescanSymbolTable()
2185 _tentativeDefinitionCount
= 0;
2186 _absoluteSymbolCount
= 0;
2187 _symbolsInSections
= 0;
2188 _hasDataInCodeLabels
= false;
2189 for (uint32_t i
=0; i
< this->_symbolCount
; ++i
) {
2190 const macho_nlist
<P
>& sym
= symbolFromIndex(i
);
2192 if ( (sym
.n_type() & N_STAB
) != 0 )
2195 // look at undefines
2196 const char* symbolName
= this->nameFromSymbol(sym
);
2197 if ( (sym
.n_type() & N_TYPE
) == N_UNDF
) {
2198 if ( sym
.n_value() != 0 ) {
2199 // count tentative definitions
2200 ++_tentativeDefinitionCount
;
2202 else if ( strncmp(symbolName
, "___dtrace_", 10) == 0 ) {
2203 // any undefined starting with __dtrace_*$ that is not ___dtrace_probe$* or ___dtrace_isenabled$*
2204 // is extra provider info
2205 if ( (strncmp(&symbolName
[10], "probe$", 6) != 0) && (strncmp(&symbolName
[10], "isenabled$", 10) != 0) ) {
2206 _dtraceProviderInfo
.push_back(symbolName
);
2211 else if ( ((sym
.n_type() & N_TYPE
) == N_INDR
) && ((sym
.n_type() & N_EXT
) != 0) ) {
2212 _indirectSymbolCount
++;
2216 // count absolute symbols
2217 if ( (sym
.n_type() & N_TYPE
) == N_ABS
) {
2218 const char* absName
= this->nameFromSymbol(sym
);
2219 // ignore .objc_class_name_* symbols
2220 if ( strncmp(absName
, ".objc_class_name_", 17) == 0 ) {
2224 // ignore .objc_class_name_* symbols
2225 if ( strncmp(absName
, ".objc_category_name_", 20) == 0 )
2227 // ignore empty *.eh symbols
2228 if ( strcmp(&absName
[strlen(absName
)-3], ".eh") == 0 )
2230 ++_absoluteSymbolCount
;
2233 // only look at definitions
2234 if ( (sym
.n_type() & N_TYPE
) != N_SECT
)
2237 // 'L' labels do not denote atom breaks
2238 if ( symbolName
[0] == 'L' ) {
2239 // <rdar://problem/9218847> Formalize data in code with L$start$ labels
2240 if ( strncmp(symbolName
, "L$start$", 8) == 0 )
2241 _hasDataInCodeLabels
= true;
2244 // how many def syms in each section
2245 if ( sym
.n_sect() > _machOSectionsCount
)
2246 throw "bad n_sect in symbol table";
2248 _symbolsInSections
++;
2252 template <typename A
>
2253 void Parser
<A
>::appendAliasAtoms(uint8_t* p
)
2255 for (uint32_t i
=0; i
< this->_symbolCount
; ++i
) {
2256 const macho_nlist
<P
>& sym
= symbolFromIndex(i
);
2258 if ( (sym
.n_type() & N_STAB
) != 0 )
2261 // only look at N_INDR symbols
2262 if ( (sym
.n_type() & N_TYPE
) != N_INDR
)
2265 // skip non-external aliases
2266 if ( (sym
.n_type() & N_EXT
) == 0 )
2269 const char* symbolName
= this->nameFromSymbol(sym
);
2270 const char* aliasOfName
= &_strings
[sym
.n_value()];
2271 bool isHiddenVisibility
= (sym
.n_type() & N_PEXT
);
2272 AliasAtom
* allocatedSpace
= (AliasAtom
*)p
;
2273 new (allocatedSpace
) AliasAtom(symbolName
, isHiddenVisibility
, _file
, aliasOfName
);
2274 p
+= sizeof(AliasAtom
);
2280 template <typename A
>
2281 int Parser
<A
>::sectionIndexSorter(void* extra
, const void* l
, const void* r
)
2283 Parser
<A
>* parser
= (Parser
<A
>*)extra
;
2284 const uint32_t* left
= (uint32_t*)l
;
2285 const uint32_t* right
= (uint32_t*)r
;
2286 const macho_section
<P
>* leftSect
= parser
->machOSectionFromSectionIndex(*left
);
2287 const macho_section
<P
>* rightSect
= parser
->machOSectionFromSectionIndex(*right
);
2289 // can't just return difference because 64-bit diff does not fit in 32-bit return type
2290 int64_t result
= leftSect
->addr() - rightSect
->addr();
2291 if ( result
== 0 ) {
2292 // two sections with same start address
2293 // one with zero size goes first
2294 bool leftEmpty
= ( leftSect
->size() == 0 );
2295 bool rightEmpty
= ( rightSect
->size() == 0 );
2296 if ( leftEmpty
!= rightEmpty
) {
2297 return ( rightEmpty
? 1 : -1 );
2299 if ( !leftEmpty
&& !rightEmpty
)
2300 throwf("overlapping sections");
2301 // both empty, so chose file order
2302 return ( rightSect
- leftSect
);
2304 else if ( result
< 0 )
2310 template <typename A
>
2311 void Parser
<A
>::makeSortedSectionsArray(uint32_t array
[])
2313 const bool log
= false;
2316 fprintf(stderr
, "unsorted sections:\n");
2317 for(unsigned int i
=0; i
< _machOSectionsCount
; ++i
)
2318 fprintf(stderr
, "0x%08llX %s %s\n", _sectionsStart
[i
].addr(), _sectionsStart
[i
].segname(), _sectionsStart
[i
].sectname());
2321 // sort by symbol table address
2322 for (uint32_t i
=0; i
< _machOSectionsCount
; ++i
)
2324 ::qsort_r(array
, _machOSectionsCount
, sizeof(uint32_t), this, §ionIndexSorter
);
2327 fprintf(stderr
, "sorted sections:\n");
2328 for(unsigned int i
=0; i
< _machOSectionsCount
; ++i
)
2329 fprintf(stderr
, "0x%08llX %s %s\n", _sectionsStart
[array
[i
]].addr(), _sectionsStart
[array
[i
]].segname(), _sectionsStart
[array
[i
]].sectname());
2335 template <typename A
>
2336 int Parser
<A
>::symbolIndexSorter(void* extra
, const void* l
, const void* r
)
2338 ParserAndSectionsArray
* extraInfo
= (ParserAndSectionsArray
*)extra
;
2339 Parser
<A
>* parser
= extraInfo
->parser
;
2340 const uint32_t* sortedSectionsArray
= extraInfo
->sortedSectionsArray
;
2341 const uint32_t* left
= (uint32_t*)l
;
2342 const uint32_t* right
= (uint32_t*)r
;
2343 const macho_nlist
<P
>& leftSym
= parser
->symbolFromIndex(*left
);
2344 const macho_nlist
<P
>& rightSym
= parser
->symbolFromIndex(*right
);
2345 // can't just return difference because 64-bit diff does not fit in 32-bit return type
2346 int64_t result
= leftSym
.n_value() - rightSym
.n_value();
2347 if ( result
== 0 ) {
2348 // two symbols with same address
2349 // if in different sections, sort earlier section first
2350 if ( leftSym
.n_sect() != rightSym
.n_sect() ) {
2351 for (uint32_t i
=0; i
< parser
->machOSectionCount(); ++i
) {
2352 if ( sortedSectionsArray
[i
]+1 == leftSym
.n_sect() )
2354 if ( sortedSectionsArray
[i
]+1 == rightSym
.n_sect() )
2358 // two symbols in same section, means one is an alias
2359 // if one is ltmp*, make it an alias (sort first)
2360 const char* leftName
= parser
->nameFromSymbol(leftSym
);
2361 const char* rightName
= parser
->nameFromSymbol(rightSym
);
2362 bool leftIsTmp
= strncmp(leftName
, "ltmp", 4);
2363 bool rightIsTmp
= strncmp(rightName
, "ltmp", 4);
2364 if ( leftIsTmp
!= rightIsTmp
) {
2365 return (rightIsTmp
? -1 : 1);
2368 // if only one is global, make the other an alias (sort first)
2369 if ( (leftSym
.n_type() & N_EXT
) != (rightSym
.n_type() & N_EXT
) ) {
2370 if ( (rightSym
.n_type() & N_EXT
) != 0 )
2375 // if both are global, sort alphabetically. earlier one will be the alias
2376 return ( strcmp(rightName
, leftName
) );
2378 else if ( result
< 0 )
2385 template <typename A
>
2386 void Parser
<A
>::makeSortedSymbolsArray(uint32_t array
[], const uint32_t sectionArray
[])
2388 const bool log
= false;
2390 uint32_t* p
= array
;
2391 for (uint32_t i
=0; i
< this->_symbolCount
; ++i
) {
2392 const macho_nlist
<P
>& sym
= symbolFromIndex(i
);
2394 if ( (sym
.n_type() & N_STAB
) != 0 )
2397 // only look at definitions
2398 if ( (sym
.n_type() & N_TYPE
) != N_SECT
)
2401 // 'L' labels do not denote atom breaks
2402 const char* symbolName
= this->nameFromSymbol(sym
);
2403 if ( symbolName
[0] == 'L' )
2406 // how many def syms in each section
2407 if ( sym
.n_sect() > _machOSectionsCount
)
2408 throw "bad n_sect in symbol table";
2413 assert(p
== &array
[_symbolsInSections
] && "second pass over symbol table yield a different number of symbols");
2415 // sort by symbol table address
2416 ParserAndSectionsArray extra
= { this, sectionArray
};
2417 ::qsort_r(array
, _symbolsInSections
, sizeof(uint32_t), &extra
, &symbolIndexSorter
);
2420 // look for two symbols at same address
2421 _overlappingSymbols
= false;
2422 for (unsigned int i
=1; i
< _symbolsInSections
; ++i
) {
2423 if ( symbolFromIndex(array
[i
-1]).n_value() == symbolFromIndex(array
[i
]).n_value() ) {
2424 //fprintf(stderr, "overlapping symbols at 0x%08llX\n", symbolFromIndex(array[i-1]).n_value());
2425 _overlappingSymbols
= true;
2431 fprintf(stderr
, "sorted symbols:\n");
2432 for(unsigned int i
=0; i
< _symbolsInSections
; ++i
)
2433 fprintf(stderr
, "0x%09llX symIndex=%d sectNum=%2d, %s\n", symbolFromIndex(array
[i
]).n_value(), array
[i
], symbolFromIndex(array
[i
]).n_sect(), nameFromSymbol(symbolFromIndex(array
[i
])) );
2437 template <typename A
>
2438 void Parser
<A
>::makeSections()
2440 // classify each section by type
2441 // compute how many Section objects will be needed and total size for all
2442 unsigned int totalSectionsSize
= 0;
2443 uint8_t machOSectsStorage
[sizeof(MachOSectionAndSectionClass
<P
>)*(_machOSectionsCount
+2)]; // also room for tentative-defs and absolute symbols
2444 // allocate raw storage for all section objects on stack
2445 MachOSectionAndSectionClass
<P
>* machOSects
= (MachOSectionAndSectionClass
<P
>*)machOSectsStorage
;
2446 unsigned int count
= 0;
2447 // local variable for bitcode parsing
2448 const macho_section
<P
>* bitcodeSect
= NULL
;
2449 const macho_section
<P
>* cmdlineSect
= NULL
;
2450 const macho_section
<P
>* swiftCmdlineSect
= NULL
;
2451 const macho_section
<P
>* bundleSect
= NULL
;
2452 bool bitcodeAsm
= false;
2454 for (uint32_t i
=0; i
< _machOSectionsCount
; ++i
) {
2455 const macho_section
<P
>* sect
= &_sectionsStart
[i
];
2456 if ( (sect
->flags() & S_ATTR_DEBUG
) != 0 ) {
2457 if ( strcmp(sect
->segname(), "__DWARF") == 0 ) {
2458 // note that .o file has dwarf
2459 _file
->_debugInfoKind
= ld::relocatable::File::kDebugInfoDwarf
;
2460 // save off iteresting dwarf sections
2461 if ( strcmp(sect
->sectname(), "__debug_info") == 0 )
2462 _file
->_dwarfDebugInfoSect
= sect
;
2463 else if ( strcmp(sect
->sectname(), "__debug_abbrev") == 0 )
2464 _file
->_dwarfDebugAbbrevSect
= sect
;
2465 else if ( strcmp(sect
->sectname(), "__debug_line") == 0 )
2466 _file
->_dwarfDebugLineSect
= sect
;
2467 else if ( strcmp(sect
->sectname(), "__debug_str") == 0 )
2468 _file
->_dwarfDebugStringSect
= sect
;
2469 // linker does not propagate dwarf sections to output file
2472 else if ( strcmp(sect
->segname(), "__LD") == 0 ) {
2473 if ( strncmp(sect
->sectname(), "__compact_unwind", 16) == 0 ) {
2474 machOSects
[count
].sect
= sect
;
2475 totalSectionsSize
+= sizeof(CUSection
<A
>);
2476 machOSects
[count
++].type
= sectionTypeCompactUnwind
;
2481 if ( strcmp(sect
->segname(), "__LLVM") == 0 ) {
2482 // Process bitcode segement
2483 if ( strncmp(sect
->sectname(), "__bitcode", 9) == 0 ) {
2485 } else if ( strncmp(sect
->sectname(), "__cmdline", 9) == 0 ) {
2487 } else if ( strncmp(sect
->sectname(), "__swift_cmdline", 15) == 0 ) {
2488 swiftCmdlineSect
= sect
;
2489 } else if ( strncmp(sect
->sectname(), "__bundle", 8) == 0 ) {
2491 } else if ( strncmp(sect
->sectname(), "__asm", 5) == 0 ) {
2494 // If treat the bitcode as data, continue to parse as a normal section.
2495 if ( !_treateBitcodeAsData
)
2498 // ignore empty __OBJC sections
2499 if ( (sect
->size() == 0) && (strcmp(sect
->segname(), "__OBJC") == 0) )
2501 // objc image info section is really attributes and not content
2502 if ( ((strcmp(sect
->sectname(), "__image_info") == 0) && (strcmp(sect
->segname(), "__OBJC") == 0))
2503 || ((strncmp(sect
->sectname(), "__objc_imageinfo", 16) == 0) && (strcmp(sect
->segname(), "__DATA") == 0)) ) {
2504 // struct objc_image_info {
2505 // uint32_t version; // initially 0
2508 // #define OBJC_IMAGE_SUPPORTS_GC 2
2509 // #define OBJC_IMAGE_GC_ONLY 4
2510 // #define OBJC_IMAGE_IS_SIMULATED 32
2512 const uint32_t* contents
= (uint32_t*)(_file
->fileContent()+sect
->offset());
2513 if ( (sect
->size() >= 8) && (contents
[0] == 0) ) {
2514 uint32_t flags
= E::get32(contents
[1]);
2515 if ( (flags
& 4) == 4 )
2516 _file
->_objConstraint
= ld::File::objcConstraintGC
;
2517 else if ( (flags
& 2) == 2 )
2518 _file
->_objConstraint
= ld::File::objcConstraintRetainReleaseOrGC
;
2519 else if ( (flags
& 32) == 32 )
2520 _file
->_objConstraint
= ld::File::objcConstraintRetainReleaseForSimulator
;
2522 _file
->_objConstraint
= ld::File::objcConstraintRetainRelease
;
2523 _file
->_swiftVersion
= ((flags
>> 8) & 0xFF);
2524 if ( sect
->size() > 8 ) {
2525 warning("section %s/%s has unexpectedly large size %llu in %s",
2526 sect
->segname(), Section
<A
>::makeSectionName(sect
), sect
->size(), _file
->path());
2530 warning("can't parse %s/%s section in %s", sect
->segname(), Section
<A
>::makeSectionName(sect
), _file
->path());
2534 machOSects
[count
].sect
= sect
;
2535 switch ( sect
->flags() & SECTION_TYPE
) {
2536 case S_SYMBOL_STUBS
:
2537 if ( _stubsSectionNum
== 0 ) {
2538 _stubsSectionNum
= i
+1;
2539 _stubsMachOSection
= sect
;
2542 assert(1 && "multiple S_SYMBOL_STUBS sections");
2543 case S_LAZY_SYMBOL_POINTERS
:
2545 case S_4BYTE_LITERALS
:
2546 totalSectionsSize
+= sizeof(Literal4Section
<A
>);
2547 machOSects
[count
++].type
= sectionTypeLiteral4
;
2549 case S_8BYTE_LITERALS
:
2550 totalSectionsSize
+= sizeof(Literal8Section
<A
>);
2551 machOSects
[count
++].type
= sectionTypeLiteral8
;
2553 case S_16BYTE_LITERALS
:
2554 totalSectionsSize
+= sizeof(Literal16Section
<A
>);
2555 machOSects
[count
++].type
= sectionTypeLiteral16
;
2557 case S_NON_LAZY_SYMBOL_POINTERS
:
2558 totalSectionsSize
+= sizeof(NonLazyPointerSection
<A
>);
2559 machOSects
[count
++].type
= sectionTypeNonLazy
;
2561 case S_THREAD_LOCAL_VARIABLE_POINTERS
:
2562 totalSectionsSize
+= sizeof(TLVPointerSection
<A
>);
2563 machOSects
[count
++].type
= sectionTypeTLVPointers
;
2565 case S_LITERAL_POINTERS
:
2566 if ( (strcmp(sect
->segname(), "__OBJC") == 0) && (strcmp(sect
->sectname(), "__cls_refs") == 0) ) {
2567 totalSectionsSize
+= sizeof(Objc1ClassReferences
<A
>);
2568 machOSects
[count
++].type
= sectionTypeObjC1ClassRefs
;
2571 totalSectionsSize
+= sizeof(PointerToCStringSection
<A
>);
2572 machOSects
[count
++].type
= sectionTypeCStringPointer
;
2575 case S_CSTRING_LITERALS
:
2576 totalSectionsSize
+= sizeof(CStringSection
<A
>);
2577 machOSects
[count
++].type
= sectionTypeCString
;
2579 case S_MOD_INIT_FUNC_POINTERS
:
2580 case S_MOD_TERM_FUNC_POINTERS
:
2581 case S_THREAD_LOCAL_INIT_FUNCTION_POINTERS
:
2586 case S_THREAD_LOCAL_REGULAR
:
2587 case S_THREAD_LOCAL_ZEROFILL
:
2588 if ( (strcmp(sect
->segname(), "__TEXT") == 0) && (strcmp(sect
->sectname(), "__eh_frame") == 0) ) {
2589 totalSectionsSize
+= sizeof(CFISection
<A
>);
2590 machOSects
[count
++].type
= sectionTypeCFI
;
2592 else if ( (strcmp(sect
->segname(), "__DATA") == 0) && (strcmp(sect
->sectname(), "__cfstring") == 0) ) {
2593 totalSectionsSize
+= sizeof(CFStringSection
<A
>);
2594 machOSects
[count
++].type
= sectionTypeCFString
;
2596 else if ( (strcmp(sect
->segname(), "__TEXT") == 0) && (strcmp(sect
->sectname(), "__ustring") == 0) ) {
2597 totalSectionsSize
+= sizeof(UTF16StringSection
<A
>);
2598 machOSects
[count
++].type
= sectionTypeUTF16Strings
;
2600 else if ( (strcmp(sect
->segname(), "__DATA") == 0) && (strncmp(sect
->sectname(), "__objc_classrefs", 16) == 0) ) {
2601 totalSectionsSize
+= sizeof(ObjC2ClassRefsSection
<A
>);
2602 machOSects
[count
++].type
= sectionTypeObjC2ClassRefs
;
2604 else if ( (strcmp(sect
->segname(), "__DATA") == 0) && (strcmp(sect
->sectname(), "__objc_catlist") == 0) ) {
2605 totalSectionsSize
+= sizeof(ObjC2CategoryListSection
<A
>);
2606 machOSects
[count
++].type
= typeObjC2CategoryList
;
2608 else if ( _AppleObjc
&& (strcmp(sect
->segname(), "__OBJC") == 0) && (strcmp(sect
->sectname(), "__class") == 0) ) {
2609 totalSectionsSize
+= sizeof(ObjC1ClassSection
<A
>);
2610 machOSects
[count
++].type
= sectionTypeObjC1Classes
;
2613 totalSectionsSize
+= sizeof(SymboledSection
<A
>);
2614 machOSects
[count
++].type
= sectionTypeSymboled
;
2617 case S_THREAD_LOCAL_VARIABLES
:
2618 totalSectionsSize
+= sizeof(TLVDefsSection
<A
>);
2619 machOSects
[count
++].type
= sectionTypeTLVDefs
;
2622 throwf("unknown section type %d", sect
->flags() & SECTION_TYPE
);
2627 if ( bitcodeSect
!= NULL
) {
2628 if ( cmdlineSect
!= NULL
)
2629 _file
->_bitcode
= std::unique_ptr
<ld::Bitcode
>(new ld::ClangBitcode(&_fileContent
[bitcodeSect
->offset()], bitcodeSect
->size(),
2630 &_fileContent
[cmdlineSect
->offset()], cmdlineSect
->size()));
2631 else if ( swiftCmdlineSect
!= NULL
)
2632 _file
->_bitcode
= std::unique_ptr
<ld::Bitcode
>(new ld::SwiftBitcode(&_fileContent
[bitcodeSect
->offset()], bitcodeSect
->size(),
2633 &_fileContent
[swiftCmdlineSect
->offset()], swiftCmdlineSect
->size()));
2635 throwf("Object file with bitcode missing cmdline options: %s", _file
->path());
2637 else if ( bundleSect
!= NULL
)
2638 _file
->_bitcode
= std::unique_ptr
<ld::Bitcode
>(new ld::BundleBitcode(&_fileContent
[bundleSect
->offset()], bundleSect
->size()));
2639 else if ( bitcodeAsm
)
2640 _file
->_bitcode
= std::unique_ptr
<ld::Bitcode
>(new ld::AsmBitcode(_fileContent
, _fileLength
));
2642 // sort by address (mach-o object files don't aways have sections sorted)
2643 ::qsort(machOSects
, count
, sizeof(MachOSectionAndSectionClass
<P
>), MachOSectionAndSectionClass
<P
>::sorter
);
2645 // we will synthesize a dummy Section<A> object for tentative definitions
2646 if ( _tentativeDefinitionCount
> 0 ) {
2647 totalSectionsSize
+= sizeof(TentativeDefinitionSection
<A
>);
2648 machOSects
[count
++].type
= sectionTypeTentativeDefinitions
;
2651 // we will synthesize a dummy Section<A> object for Absolute symbols
2652 if ( _absoluteSymbolCount
> 0 ) {
2653 totalSectionsSize
+= sizeof(AbsoluteSymbolSection
<A
>);
2654 machOSects
[count
++].type
= sectionTypeAbsoluteSymbols
;
2657 // allocate one block for all Section objects as well as pointers to each
2658 uint8_t* space
= new uint8_t[totalSectionsSize
+count
*sizeof(Section
<A
>*)];
2659 _file
->_sectionsArray
= (Section
<A
>**)space
;
2660 _file
->_sectionsArrayCount
= count
;
2661 Section
<A
>** objects
= _file
->_sectionsArray
;
2662 space
+= count
*sizeof(Section
<A
>*);
2663 for (uint32_t i
=0; i
< count
; ++i
) {
2664 switch ( machOSects
[i
].type
) {
2665 case sectionTypeIgnore
:
2667 case sectionTypeLiteral4
:
2668 *objects
++ = new (space
) Literal4Section
<A
>(*this, *_file
, machOSects
[i
].sect
);
2669 space
+= sizeof(Literal4Section
<A
>);
2671 case sectionTypeLiteral8
:
2672 *objects
++ = new (space
) Literal8Section
<A
>(*this, *_file
, machOSects
[i
].sect
);
2673 space
+= sizeof(Literal8Section
<A
>);
2675 case sectionTypeLiteral16
:
2676 *objects
++ = new (space
) Literal16Section
<A
>(*this, *_file
, machOSects
[i
].sect
);
2677 space
+= sizeof(Literal16Section
<A
>);
2679 case sectionTypeNonLazy
:
2680 *objects
++ = new (space
) NonLazyPointerSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2681 space
+= sizeof(NonLazyPointerSection
<A
>);
2683 case sectionTypeTLVPointers
:
2684 *objects
++ = new (space
) TLVPointerSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2685 space
+= sizeof(TLVPointerSection
<A
>);
2687 case sectionTypeCFI
:
2688 _EHFrameSection
= new (space
) CFISection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2689 *objects
++ = _EHFrameSection
;
2690 space
+= sizeof(CFISection
<A
>);
2692 case sectionTypeCString
:
2693 *objects
++ = new (space
) CStringSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2694 space
+= sizeof(CStringSection
<A
>);
2696 case sectionTypeCStringPointer
:
2697 *objects
++ = new (space
) PointerToCStringSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2698 space
+= sizeof(PointerToCStringSection
<A
>);
2700 case sectionTypeObjC1ClassRefs
:
2701 *objects
++ = new (space
) Objc1ClassReferences
<A
>(*this, *_file
, machOSects
[i
].sect
);
2702 space
+= sizeof(Objc1ClassReferences
<A
>);
2704 case sectionTypeUTF16Strings
:
2705 *objects
++ = new (space
) UTF16StringSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2706 space
+= sizeof(UTF16StringSection
<A
>);
2708 case sectionTypeCFString
:
2709 *objects
++ = new (space
) CFStringSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2710 space
+= sizeof(CFStringSection
<A
>);
2712 case sectionTypeObjC2ClassRefs
:
2713 *objects
++ = new (space
) ObjC2ClassRefsSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2714 space
+= sizeof(ObjC2ClassRefsSection
<A
>);
2716 case typeObjC2CategoryList
:
2717 *objects
++ = new (space
) ObjC2CategoryListSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2718 space
+= sizeof(ObjC2CategoryListSection
<A
>);
2720 case sectionTypeObjC1Classes
:
2721 *objects
++ = new (space
) ObjC1ClassSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2722 space
+= sizeof(ObjC1ClassSection
<A
>);
2724 case sectionTypeSymboled
:
2725 *objects
++ = new (space
) SymboledSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2726 space
+= sizeof(SymboledSection
<A
>);
2728 case sectionTypeTLVDefs
:
2729 *objects
++ = new (space
) TLVDefsSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2730 space
+= sizeof(TLVDefsSection
<A
>);
2732 case sectionTypeCompactUnwind
:
2733 _compactUnwindSection
= new (space
) CUSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2734 *objects
++ = _compactUnwindSection
;
2735 space
+= sizeof(CUSection
<A
>);
2737 case sectionTypeTentativeDefinitions
:
2738 *objects
++ = new (space
) TentativeDefinitionSection
<A
>(*this, *_file
);
2739 space
+= sizeof(TentativeDefinitionSection
<A
>);
2741 case sectionTypeAbsoluteSymbols
:
2742 _absoluteSection
= new (space
) AbsoluteSymbolSection
<A
>(*this, *_file
);
2743 *objects
++ = _absoluteSection
;
2744 space
+= sizeof(AbsoluteSymbolSection
<A
>);
2747 throw "internal error uknown SectionType";
2753 template <typename A
>
2754 Section
<A
>* Parser
<A
>::sectionForAddress(typename
A::P::uint_t addr
)
2756 for (uint32_t i
=0; i
< _file
->_sectionsArrayCount
; ++i
) {
2757 const macho_section
<typename
A::P
>* sect
= _file
->_sectionsArray
[i
]->machoSection();
2758 // TentativeDefinitionSection and AbsoluteSymbolSection have no mach-o section
2759 if ( sect
!= NULL
) {
2760 if ( (sect
->addr() <= addr
) && (addr
< (sect
->addr()+sect
->size())) ) {
2761 return _file
->_sectionsArray
[i
];
2765 // not strictly in any section
2766 // may be in a zero length section
2767 for (uint32_t i
=0; i
< _file
->_sectionsArrayCount
; ++i
) {
2768 const macho_section
<typename
A::P
>* sect
= _file
->_sectionsArray
[i
]->machoSection();
2769 // TentativeDefinitionSection and AbsoluteSymbolSection have no mach-o section
2770 if ( sect
!= NULL
) {
2771 if ( (sect
->addr() == addr
) && (sect
->size() == 0) ) {
2772 return _file
->_sectionsArray
[i
];
2777 throwf("sectionForAddress(0x%llX) address not in any section", (uint64_t)addr
);
2780 template <typename A
>
2781 Section
<A
>* Parser
<A
>::sectionForNum(unsigned int num
)
2783 for (uint32_t i
=0; i
< _file
->_sectionsArrayCount
; ++i
) {
2784 const macho_section
<typename
A::P
>* sect
= _file
->_sectionsArray
[i
]->machoSection();
2785 // TentativeDefinitionSection and AbsoluteSymbolSection have no mach-o section
2786 if ( sect
!= NULL
) {
2787 if ( num
== (unsigned int)((sect
- _sectionsStart
)+1) )
2788 return _file
->_sectionsArray
[i
];
2791 throwf("sectionForNum(%u) section number not for any section", num
);
2794 template <typename A
>
2795 Atom
<A
>* Parser
<A
>::findAtomByAddress(pint_t addr
)
2797 Section
<A
>* section
= this->sectionForAddress(addr
);
2798 return section
->findAtomByAddress(addr
);
2801 template <typename A
>
2802 Atom
<A
>* Parser
<A
>::findAtomByAddressOrNullIfStub(pint_t addr
)
2804 if ( hasStubsSection() && (_stubsMachOSection
->addr() <= addr
) && (addr
< (_stubsMachOSection
->addr()+_stubsMachOSection
->size())) )
2806 return findAtomByAddress(addr
);
2809 template <typename A
>
2810 Atom
<A
>* Parser
<A
>::findAtomByAddressOrLocalTargetOfStub(pint_t addr
, uint32_t* offsetInAtom
)
2812 if ( hasStubsSection() && (_stubsMachOSection
->addr() <= addr
) && (addr
< (_stubsMachOSection
->addr()+_stubsMachOSection
->size())) ) {
2813 // target is a stub, remove indirection
2814 uint32_t symbolIndex
= this->symbolIndexFromIndirectSectionAddress(addr
, _stubsMachOSection
);
2815 assert(symbolIndex
!= INDIRECT_SYMBOL_LOCAL
);
2816 const macho_nlist
<P
>& sym
= this->symbolFromIndex(symbolIndex
);
2817 // can't be to external weak symbol
2818 assert( (this->combineFromSymbol(sym
) != ld::Atom::combineByName
) || (this->scopeFromSymbol(sym
) != ld::Atom::scopeGlobal
) );
2820 return this->findAtomByName(this->nameFromSymbol(sym
));
2822 Atom
<A
>* target
= this->findAtomByAddress(addr
);
2823 *offsetInAtom
= addr
- target
->_objAddress
;
2827 template <typename A
>
2828 Atom
<A
>* Parser
<A
>::findAtomByName(const char* name
)
2830 uint8_t* p
= _file
->_atomsArray
;
2831 for(int i
=_file
->_atomsArrayCount
; i
> 0; --i
) {
2832 Atom
<A
>* atom
= (Atom
<A
>*)p
;
2833 if ( strcmp(name
, atom
->name()) == 0 )
2835 p
+= sizeof(Atom
<A
>);
2840 template <typename A
>
2841 void Parser
<A
>::findTargetFromAddress(pint_t addr
, TargetDesc
& target
)
2843 if ( hasStubsSection() && (_stubsMachOSection
->addr() <= addr
) && (addr
< (_stubsMachOSection
->addr()+_stubsMachOSection
->size())) ) {
2844 // target is a stub, remove indirection
2845 uint32_t symbolIndex
= this->symbolIndexFromIndirectSectionAddress(addr
, _stubsMachOSection
);
2846 assert(symbolIndex
!= INDIRECT_SYMBOL_LOCAL
);
2847 const macho_nlist
<P
>& sym
= this->symbolFromIndex(symbolIndex
);
2849 target
.name
= this->nameFromSymbol(sym
);
2850 target
.weakImport
= this->weakImportFromSymbol(sym
);
2854 Section
<A
>* section
= this->sectionForAddress(addr
);
2855 target
.atom
= section
->findAtomByAddress(addr
);
2856 target
.addend
= addr
- target
.atom
->_objAddress
;
2857 target
.weakImport
= false;
2861 template <typename A
>
2862 void Parser
<A
>::findTargetFromAddress(pint_t baseAddr
, pint_t addr
, TargetDesc
& target
)
2864 findTargetFromAddress(baseAddr
, target
);
2865 target
.addend
= addr
- target
.atom
->_objAddress
;
2868 template <typename A
>
2869 void Parser
<A
>::findTargetFromAddressAndSectionNum(pint_t addr
, unsigned int sectNum
, TargetDesc
& target
)
2871 if ( sectNum
== R_ABS
) {
2872 // target is absolute symbol that corresponds to addr
2873 if ( _absoluteSection
!= NULL
) {
2874 target
.atom
= _absoluteSection
->findAbsAtomForValue(addr
);
2875 if ( target
.atom
!= NULL
) {
2877 target
.weakImport
= false;
2882 throwf("R_ABS reloc but no absolute symbol at target address");
2885 if ( hasStubsSection() && (stubsSectionNum() == sectNum
) ) {
2886 // target is a stub, remove indirection
2887 uint32_t symbolIndex
= this->symbolIndexFromIndirectSectionAddress(addr
, _stubsMachOSection
);
2888 assert(symbolIndex
!= INDIRECT_SYMBOL_LOCAL
);
2889 const macho_nlist
<P
>& sym
= this->symbolFromIndex(symbolIndex
);
2890 // use direct reference when stub is to a static function
2891 if ( ((sym
.n_type() & N_TYPE
) == N_SECT
) && (((sym
.n_type() & N_EXT
) == 0) || (this->nameFromSymbol(sym
)[0] == 'L')) ) {
2892 this->findTargetFromAddressAndSectionNum(sym
.n_value(), sym
.n_sect(), target
);
2896 target
.name
= this->nameFromSymbol(sym
);
2897 target
.weakImport
= this->weakImportFromSymbol(sym
);
2902 Section
<A
>* section
= this->sectionForNum(sectNum
);
2903 target
.atom
= section
->findAtomByAddress(addr
);
2904 if ( target
.atom
== NULL
) {
2905 typedef typename
A::P::sint_t sint_t
;
2906 sint_t a
= (sint_t
)addr
;
2907 sint_t sectStart
= (sint_t
)(section
->machoSection()->addr());
2908 sint_t sectEnd
= sectStart
+ section
->machoSection()->size();
2909 if ( a
< sectStart
) {
2910 // target address is before start of section, so must be negative addend
2911 target
.atom
= section
->findAtomByAddress(sectStart
);
2912 target
.addend
= a
- sectStart
;
2913 target
.weakImport
= false;
2917 else if ( a
>= sectEnd
) {
2918 target
.atom
= section
->findAtomByAddress(sectEnd
-1);
2919 target
.addend
= a
- sectEnd
;
2920 target
.weakImport
= false;
2925 assert(target
.atom
!= NULL
);
2926 target
.addend
= addr
- target
.atom
->_objAddress
;
2927 target
.weakImport
= false;
2931 template <typename A
>
2932 void Parser
<A
>::addDtraceExtraInfos(const SourceLocation
& src
, const char* providerName
)
2934 // for every ___dtrace_stability$* and ___dtrace_typedefs$* undefine with
2935 // a matching provider name, add a by-name kDtraceTypeReference at probe site
2936 const char* dollar
= strchr(providerName
, '$');
2937 if ( dollar
!= NULL
) {
2938 int providerNameLen
= dollar
-providerName
+1;
2939 for ( std::vector
<const char*>::iterator it
= _dtraceProviderInfo
.begin(); it
!= _dtraceProviderInfo
.end(); ++it
) {
2940 const char* typeDollar
= strchr(*it
, '$');
2941 if ( typeDollar
!= NULL
) {
2942 if ( strncmp(typeDollar
+1, providerName
, providerNameLen
) == 0 ) {
2943 addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindDtraceExtra
,false, *it
);
2950 template <typename A
>
2951 const char* Parser
<A
>::scanSymbolTableForAddress(uint64_t addr
)
2953 uint64_t closestSymAddr
= 0;
2954 const char* closestSymName
= NULL
;
2955 for (uint32_t i
=0; i
< this->_symbolCount
; ++i
) {
2956 const macho_nlist
<P
>& sym
= symbolFromIndex(i
);
2958 if ( (sym
.n_type() & N_STAB
) != 0 )
2961 // only look at definitions
2962 if ( (sym
.n_type() & N_TYPE
) != N_SECT
)
2965 // return with exact match
2966 if ( sym
.n_value() == addr
) {
2967 const char* name
= nameFromSymbol(sym
);
2968 if ( strncmp(name
, "ltmp", 4) != 0 )
2970 // treat 'ltmp*' labels as close match
2971 closestSymAddr
= sym
.n_value();
2972 closestSymName
= name
;
2975 // record closest seen so far
2976 if ( (sym
.n_value() < addr
) && ((sym
.n_value() > closestSymAddr
) || (closestSymName
== NULL
)) )
2977 closestSymName
= nameFromSymbol(sym
);
2980 return (closestSymName
!= NULL
) ? closestSymName
: "unknown";
2984 template <typename A
>
2985 void Parser
<A
>::addFixups(const SourceLocation
& src
, ld::Fixup::Kind setKind
, const TargetDesc
& target
)
2987 // some fixup pairs can be combined
2988 ld::Fixup::Cluster cl
= ld::Fixup::k1of3
;
2989 ld::Fixup::Kind firstKind
= ld::Fixup::kindSetTargetAddress
;
2990 bool combined
= false;
2991 if ( target
.addend
== 0 ) {
2992 cl
= ld::Fixup::k1of1
;
2994 switch ( setKind
) {
2995 case ld::Fixup::kindStoreLittleEndian32
:
2996 firstKind
= ld::Fixup::kindStoreTargetAddressLittleEndian32
;
2998 case ld::Fixup::kindStoreLittleEndian64
:
2999 firstKind
= ld::Fixup::kindStoreTargetAddressLittleEndian64
;
3001 case ld::Fixup::kindStoreBigEndian32
:
3002 firstKind
= ld::Fixup::kindStoreTargetAddressBigEndian32
;
3004 case ld::Fixup::kindStoreBigEndian64
:
3005 firstKind
= ld::Fixup::kindStoreTargetAddressBigEndian64
;
3007 case ld::Fixup::kindStoreX86BranchPCRel32
:
3008 firstKind
= ld::Fixup::kindStoreTargetAddressX86BranchPCRel32
;
3010 case ld::Fixup::kindStoreX86PCRel32
:
3011 firstKind
= ld::Fixup::kindStoreTargetAddressX86PCRel32
;
3013 case ld::Fixup::kindStoreX86PCRel32GOTLoad
:
3014 firstKind
= ld::Fixup::kindStoreTargetAddressX86PCRel32GOTLoad
;
3016 case ld::Fixup::kindStoreX86PCRel32TLVLoad
:
3017 firstKind
= ld::Fixup::kindStoreTargetAddressX86PCRel32TLVLoad
;
3019 case ld::Fixup::kindStoreX86Abs32TLVLoad
:
3020 firstKind
= ld::Fixup::kindStoreTargetAddressX86Abs32TLVLoad
;
3022 case ld::Fixup::kindStoreARMBranch24
:
3023 firstKind
= ld::Fixup::kindStoreTargetAddressARMBranch24
;
3025 case ld::Fixup::kindStoreThumbBranch22
:
3026 firstKind
= ld::Fixup::kindStoreTargetAddressThumbBranch22
;
3028 #if SUPPORT_ARCH_arm64
3029 case ld::Fixup::kindStoreARM64Branch26
:
3030 firstKind
= ld::Fixup::kindStoreTargetAddressARM64Branch26
;
3032 case ld::Fixup::kindStoreARM64Page21
:
3033 firstKind
= ld::Fixup::kindStoreTargetAddressARM64Page21
;
3035 case ld::Fixup::kindStoreARM64PageOff12
:
3036 firstKind
= ld::Fixup::kindStoreTargetAddressARM64PageOff12
;
3038 case ld::Fixup::kindStoreARM64GOTLoadPage21
:
3039 firstKind
= ld::Fixup::kindStoreTargetAddressARM64GOTLoadPage21
;
3041 case ld::Fixup::kindStoreARM64GOTLoadPageOff12
:
3042 firstKind
= ld::Fixup::kindStoreTargetAddressARM64GOTLoadPageOff12
;
3044 case ld::Fixup::kindStoreARM64TLVPLoadPage21
:
3045 firstKind
= ld::Fixup::kindStoreTargetAddressARM64TLVPLoadPage21
;
3047 case ld::Fixup::kindStoreARM64TLVPLoadPageOff12
:
3048 firstKind
= ld::Fixup::kindStoreTargetAddressARM64TLVPLoadPageOff12
;
3053 cl
= ld::Fixup::k1of2
;
3058 if ( target
.atom
!= NULL
) {
3059 if ( target
.atom
->scope() == ld::Atom::scopeTranslationUnit
) {
3060 addFixup(src
, cl
, firstKind
, target
.atom
);
3062 else if ( (target
.atom
->combine() == ld::Atom::combineByNameAndContent
) || (target
.atom
->combine() == ld::Atom::combineByNameAndReferences
) ) {
3063 addFixup(src
, cl
, firstKind
, ld::Fixup::bindingByContentBound
, target
.atom
);
3065 else if ( (src
.atom
->section().type() == ld::Section::typeCFString
) && (src
.offsetInAtom
!= 0) ) {
3066 // backing string in CFStrings should always be direct
3067 addFixup(src
, cl
, firstKind
, target
.atom
);
3069 else if ( (src
.atom
== target
.atom
) && (target
.atom
->combine() == ld::Atom::combineByName
) ) {
3070 // reference to self should always be direct
3071 addFixup(src
, cl
, firstKind
, target
.atom
);
3074 // change direct fixup to by-name fixup
3075 addFixup(src
, cl
, firstKind
, false, target
.atom
->name());
3079 addFixup(src
, cl
, firstKind
, target
.weakImport
, target
.name
);
3081 if ( target
.addend
== 0 ) {
3083 addFixup(src
, ld::Fixup::k2of2
, setKind
);
3086 addFixup(src
, ld::Fixup::k2of3
, ld::Fixup::kindAddAddend
, target
.addend
);
3087 addFixup(src
, ld::Fixup::k3of3
, setKind
);
3091 template <typename A
>
3092 void Parser
<A
>::addFixups(const SourceLocation
& src
, ld::Fixup::Kind kind
, const TargetDesc
& target
, const TargetDesc
& picBase
)
3094 ld::Fixup::Cluster cl
= (target
.addend
== 0) ? ld::Fixup::k1of4
: ld::Fixup::k1of5
;
3095 if ( target
.atom
!= NULL
) {
3096 if ( target
.atom
->scope() == ld::Atom::scopeTranslationUnit
) {
3097 addFixup(src
, cl
, ld::Fixup::kindSetTargetAddress
, target
.atom
);
3099 else if ( (target
.atom
->combine() == ld::Atom::combineByNameAndContent
) || (target
.atom
->combine() == ld::Atom::combineByNameAndReferences
) ) {
3100 addFixup(src
, cl
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, target
.atom
);
3103 addFixup(src
, cl
, ld::Fixup::kindSetTargetAddress
, false, target
.atom
->name());
3107 addFixup(src
, cl
, ld::Fixup::kindSetTargetAddress
, target
.weakImport
, target
.name
);
3109 if ( target
.addend
== 0 ) {
3110 assert(picBase
.atom
!= NULL
);
3111 addFixup(src
, ld::Fixup::k2of4
, ld::Fixup::kindSubtractTargetAddress
, picBase
.atom
);
3112 addFixup(src
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractAddend
, picBase
.addend
);
3113 addFixup(src
, ld::Fixup::k4of4
, kind
);
3116 addFixup(src
, ld::Fixup::k2of5
, ld::Fixup::kindAddAddend
, target
.addend
);
3117 addFixup(src
, ld::Fixup::k3of5
, ld::Fixup::kindSubtractTargetAddress
, picBase
.atom
);
3118 addFixup(src
, ld::Fixup::k4of5
, ld::Fixup::kindSubtractAddend
, picBase
.addend
);
3119 addFixup(src
, ld::Fixup::k5of5
, kind
);
3125 template <typename A
>
3126 uint32_t TentativeDefinitionSection
<A
>::computeAtomCount(class Parser
<A
>& parser
,
3127 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
3128 const struct Parser
<A
>::CFI_CU_InfoArrays
&)
3130 return parser
.tentativeDefinitionCount();
3133 template <typename A
>
3134 uint32_t TentativeDefinitionSection
<A
>::appendAtoms(class Parser
<A
>& parser
, uint8_t* p
,
3135 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
3136 const struct Parser
<A
>::CFI_CU_InfoArrays
&)
3138 this->_beginAtoms
= (Atom
<A
>*)p
;
3140 for (uint32_t i
=parser
.undefinedStartIndex(); i
< parser
.undefinedEndIndex(); ++i
) {
3141 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(i
);
3142 if ( ((sym
.n_type() & N_TYPE
) == N_UNDF
) && (sym
.n_value() != 0) ) {
3143 uint64_t size
= sym
.n_value();
3144 uint8_t alignP2
= GET_COMM_ALIGN(sym
.n_desc());
3145 if ( alignP2
== 0 ) {
3146 // common symbols align to their size
3147 // that is, a 4-byte common aligns to 4-bytes
3148 // if this size is not a power of two,
3149 // then round up to the next power of two
3150 alignP2
= 63 - (uint8_t)__builtin_clzll(size
);
3151 if ( size
!= (1ULL << alignP2
) )
3154 // limit alignment of extremely large commons to 2^15 bytes (8-page)
3157 Atom
<A
>* allocatedSpace
= (Atom
<A
>*)p
;
3158 new (allocatedSpace
) Atom
<A
>(*this, parser
.nameFromSymbol(sym
), (pint_t
)ULLONG_MAX
, size
,
3159 ld::Atom::definitionTentative
, ld::Atom::combineByName
,
3160 parser
.scopeFromSymbol(sym
), ld::Atom::typeZeroFill
, ld::Atom::symbolTableIn
,
3161 parser
.dontDeadStripFromSymbol(sym
), false, false, ld::Atom::Alignment(alignP2
) );
3162 p
+= sizeof(Atom
<A
>);
3166 this->_endAtoms
= (Atom
<A
>*)p
;
3171 template <typename A
>
3172 uint32_t AbsoluteSymbolSection
<A
>::computeAtomCount(class Parser
<A
>& parser
,
3173 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
3174 const struct Parser
<A
>::CFI_CU_InfoArrays
&)
3176 return parser
.absoluteSymbolCount();
3179 template <typename A
>
3180 uint32_t AbsoluteSymbolSection
<A
>::appendAtoms(class Parser
<A
>& parser
, uint8_t* p
,
3181 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
3182 const struct Parser
<A
>::CFI_CU_InfoArrays
&)
3184 this->_beginAtoms
= (Atom
<A
>*)p
;
3186 for (uint32_t i
=0; i
< parser
.symbolCount(); ++i
) {
3187 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(i
);
3188 if ( (sym
.n_type() & N_TYPE
) != N_ABS
)
3190 const char* absName
= parser
.nameFromSymbol(sym
);
3191 // ignore .objc_class_name_* symbols
3192 if ( strncmp(absName
, ".objc_class_name_", 17) == 0 )
3194 // ignore .objc_class_name_* symbols
3195 if ( strncmp(absName
, ".objc_category_name_", 20) == 0 )
3197 // ignore empty *.eh symbols
3198 if ( strcmp(&absName
[strlen(absName
)-3], ".eh") == 0 )
3201 Atom
<A
>* allocatedSpace
= (Atom
<A
>*)p
;
3202 new (allocatedSpace
) Atom
<A
>(*this, parser
, sym
, 0);
3203 p
+= sizeof(Atom
<A
>);
3206 this->_endAtoms
= (Atom
<A
>*)p
;
3210 template <typename A
>
3211 Atom
<A
>* AbsoluteSymbolSection
<A
>::findAbsAtomForValue(typename
A::P::uint_t value
)
3213 Atom
<A
>* end
= this->_endAtoms
;
3214 for(Atom
<A
>* p
= this->_beginAtoms
; p
< end
; ++p
) {
3215 if ( p
->_objAddress
== value
)
3222 template <typename A
>
3223 uint32_t Parser
<A
>::indirectSymbol(uint32_t indirectIndex
)
3225 if ( indirectIndex
>= _indirectTableCount
)
3226 throw "indirect symbol index out of range";
3227 return E::get32(_indirectTable
[indirectIndex
]);
3230 template <typename A
>
3231 const macho_nlist
<typename
A::P
>& Parser
<A
>::symbolFromIndex(uint32_t index
)
3233 if ( index
> _symbolCount
)
3234 throw "symbol index out of range";
3235 return _symbols
[index
];
3238 template <typename A
>
3239 const macho_section
<typename
A::P
>* Parser
<A
>::machOSectionFromSectionIndex(uint32_t index
)
3241 if ( index
>= _machOSectionsCount
)
3242 throw "section index out of range";
3243 return &_sectionsStart
[index
];
3246 template <typename A
>
3247 uint32_t Parser
<A
>::symbolIndexFromIndirectSectionAddress(pint_t addr
, const macho_section
<P
>* sect
)
3249 uint32_t elementSize
= 0;
3250 switch ( sect
->flags() & SECTION_TYPE
) {
3251 case S_SYMBOL_STUBS
:
3252 elementSize
= sect
->reserved2();
3254 case S_LAZY_SYMBOL_POINTERS
:
3255 case S_NON_LAZY_SYMBOL_POINTERS
:
3256 elementSize
= sizeof(pint_t
);
3259 throw "section does not use inirect symbol table";
3261 uint32_t indexInSection
= (addr
- sect
->addr()) / elementSize
;
3262 uint32_t indexIntoIndirectTable
= sect
->reserved1() + indexInSection
;
3263 return this->indirectSymbol(indexIntoIndirectTable
);
3268 template <typename A
>
3269 const char* Parser
<A
>::nameFromSymbol(const macho_nlist
<P
>& sym
)
3271 return &_strings
[sym
.n_strx()];
3274 template <typename A
>
3275 ld::Atom::Scope Parser
<A
>::scopeFromSymbol(const macho_nlist
<P
>& sym
)
3277 if ( (sym
.n_type() & N_EXT
) == 0 )
3278 return ld::Atom::scopeTranslationUnit
;
3279 else if ( (sym
.n_type() & N_PEXT
) != 0 )
3280 return ld::Atom::scopeLinkageUnit
;
3281 else if ( this->nameFromSymbol(sym
)[0] == 'l' ) // since all 'l' symbols will be remove, don't make them global
3282 return ld::Atom::scopeLinkageUnit
;
3284 return ld::Atom::scopeGlobal
;
3287 template <typename A
>
3288 ld::Atom::Definition Parser
<A
>::definitionFromSymbol(const macho_nlist
<P
>& sym
)
3290 switch ( sym
.n_type() & N_TYPE
) {
3292 return ld::Atom::definitionAbsolute
;
3294 return ld::Atom::definitionRegular
;
3296 if ( sym
.n_value() != 0 )
3297 return ld::Atom::definitionTentative
;
3299 throw "definitionFromSymbol() bad symbol";
3302 template <typename A
>
3303 ld::Atom::Combine Parser
<A
>::combineFromSymbol(const macho_nlist
<P
>& sym
)
3305 if ( sym
.n_desc() & N_WEAK_DEF
)
3306 return ld::Atom::combineByName
;
3308 return ld::Atom::combineNever
;
3312 template <typename A
>
3313 ld::Atom::SymbolTableInclusion Parser
<A
>::inclusionFromSymbol(const macho_nlist
<P
>& sym
)
3315 const char* symbolName
= nameFromSymbol(sym
);
3316 // labels beginning with 'l' (lowercase ell) are automatically removed in final linked images <rdar://problem/4571042>
3317 // labels beginning with 'L' should have been stripped by the assembler, so are stripped now
3318 if ( sym
.n_desc() & REFERENCED_DYNAMICALLY
)
3319 return ld::Atom::symbolTableInAndNeverStrip
;
3320 else if ( symbolName
[0] == 'l' )
3321 return ld::Atom::symbolTableNotInFinalLinkedImages
;
3322 else if ( symbolName
[0] == 'L' )
3323 return ld::Atom::symbolTableNotIn
;
3325 return ld::Atom::symbolTableIn
;
3328 template <typename A
>
3329 bool Parser
<A
>::dontDeadStripFromSymbol(const macho_nlist
<P
>& sym
)
3331 return ( (sym
.n_desc() & (N_NO_DEAD_STRIP
|REFERENCED_DYNAMICALLY
)) != 0 );
3334 template <typename A
>
3335 bool Parser
<A
>::isThumbFromSymbol(const macho_nlist
<P
>& sym
)
3337 return ( sym
.n_desc() & N_ARM_THUMB_DEF
);
3340 template <typename A
>
3341 bool Parser
<A
>::weakImportFromSymbol(const macho_nlist
<P
>& sym
)
3343 return ( ((sym
.n_type() & N_TYPE
) == N_UNDF
) && ((sym
.n_desc() & N_WEAK_REF
) != 0) );
3346 template <typename A
>
3347 bool Parser
<A
>::resolverFromSymbol(const macho_nlist
<P
>& sym
)
3349 return ( sym
.n_desc() & N_SYMBOL_RESOLVER
);
3352 template <typename A
>
3353 bool Parser
<A
>::altEntryFromSymbol(const macho_nlist
<P
>& sym
)
3355 return ( sym
.n_desc() & N_ALT_ENTRY
);
3359 /* Skip over a LEB128 value (signed or unsigned). */
3361 skip_leb128 (const uint8_t ** offset
, const uint8_t * end
)
3363 while (*offset
!= end
&& **offset
>= 0x80)
3369 /* Read a ULEB128 into a 64-bit word. Return (uint64_t)-1 on overflow
3370 or error. On overflow, skip past the rest of the uleb128. */
3372 read_uleb128 (const uint8_t ** offset
, const uint8_t * end
)
3374 uint64_t result
= 0;
3381 return (uint64_t) -1;
3383 b
= **offset
& 0x7f;
3385 if (bit
>= 64 || b
<< bit
>> bit
!= b
)
3386 result
= (uint64_t) -1;
3388 result
|= b
<< bit
, bit
+= 7;
3389 } while (*(*offset
)++ >= 0x80);
3394 /* Skip over a DWARF attribute of form FORM. */
3395 template <typename A
>
3396 bool Parser
<A
>::skip_form(const uint8_t ** offset
, const uint8_t * end
, uint64_t form
,
3397 uint8_t addr_size
, bool dwarf64
)
3407 case DW_FORM_block2
:
3408 if (end
- *offset
< 2)
3410 sz
= 2 + A::P::E::get16(*(uint16_t*)offset
);
3413 case DW_FORM_block4
:
3414 if (end
- *offset
< 4)
3416 sz
= 2 + A::P::E::get32(*(uint32_t*)offset
);
3434 case DW_FORM_string
:
3435 while (*offset
!= end
&& **offset
)
3444 sz
= read_uleb128 (offset
, end
);
3447 case DW_FORM_block1
:
3455 case DW_FORM_ref_udata
:
3456 skip_leb128 (offset
, end
);
3460 case DW_FORM_ref_addr
:
3464 case DW_FORM_sec_offset
:
3465 sz
= sizeof(typename
A::P::uint_t
);
3468 case DW_FORM_exprloc
:
3469 sz
= read_uleb128 (offset
, end
);
3472 case DW_FORM_flag_present
:
3476 case DW_FORM_ref_sig8
:
3483 if (end
- *offset
< sz
)
3490 template <typename A
>
3491 const char* Parser
<A
>::getDwarfString(uint64_t form
, const uint8_t*& di
)
3494 const char* dwarfStrings
;
3495 const char* result
= NULL
;
3497 case DW_FORM_string
:
3498 result
= (const char*)di
;
3499 di
+= strlen(result
) + 1;
3502 offset
= E::get32(*((uint32_t*)di
));
3503 dwarfStrings
= (char*)_file
->fileContent() + _file
->_dwarfDebugStringSect
->offset();
3504 if ( offset
< _file
->_dwarfDebugStringSect
->size() )
3505 result
= &dwarfStrings
[offset
];
3507 warning("dwarf DW_FORM_strp (offset=0x%08X) is too big in %s", offset
, this->_path
);
3511 warning("unknown dwarf string encoding (form=%lld) in %s", form
, this->_path
);
3517 template <typename A
>
3518 uint64_t Parser
<A
>::getDwarfOffset(uint64_t form
, const uint8_t*& di
, bool dwarf64
)
3520 if ( form
== DW_FORM_sec_offset
)
3521 form
= (dwarf64
? DW_FORM_data8
: DW_FORM_data4
);
3522 uint64_t result
= -1;
3525 result
= A::P::E::get32(*(uint32_t*)di
);
3529 result
= A::P::E::get64(*(uint64_t*)di
);
3533 warning("unknown dwarf DW_FORM_ for DW_AT_stmt_list in %s", this->_path
);
3539 template <typename A
>
3540 struct AtomAndLineInfo
{
3542 ld::Atom::LineInfo info
;
3546 // <rdar://problem/5591394> Add support to ld64 for N_FUN stabs when used for symbolic constants
3547 // Returns whether a stabStr belonging to an N_FUN stab represents a
3548 // symbolic constant rather than a function
3549 template <typename A
>
3550 bool Parser
<A
>::isConstFunStabs(const char *stabStr
)
3553 // N_FUN can be used for both constants and for functions. In case it's a constant,
3554 // the format of the stabs string is "symname:c=<value>;"
3555 // ':' cannot appear in the symbol name, except if it's an Objective-C method
3556 // (in which case the symbol name starts with + or -, and then it's definitely
3558 return (stabStr
!= NULL
) && (stabStr
[0] != '+') && (stabStr
[0] != '-')
3559 && ((colon
= strchr(stabStr
, ':')) != NULL
)
3560 && (colon
[1] == 'c') && (colon
[2] == '=');
3564 template <typename A
>
3565 void Parser
<A
>::parseDebugInfo()
3567 // check for dwarf __debug_info section
3568 if ( _file
->_dwarfDebugInfoSect
== NULL
) {
3569 // if no DWARF debug info, look for stabs
3573 if ( _file
->_dwarfDebugInfoSect
->size() == 0 )
3579 if ( !read_comp_unit(&tuName
, &tuDir
, &stmtList
) ) {
3580 // if can't parse dwarf, warn and give up
3581 _file
->_dwarfTranslationUnitPath
= NULL
;
3582 warning("can't parse dwarf compilation unit info in %s", _path
);
3583 _file
->_debugInfoKind
= ld::relocatable::File::kDebugInfoNone
;
3586 if ( (tuName
!= NULL
) && (tuName
[0] == '/') ) {
3587 _file
->_dwarfTranslationUnitPath
= tuName
;
3589 else if ( (tuDir
!= NULL
) && (tuName
!= NULL
) ) {
3590 asprintf((char**)&(_file
->_dwarfTranslationUnitPath
), "%s/%s", tuDir
, tuName
);
3592 else if ( tuDir
== NULL
) {
3593 _file
->_dwarfTranslationUnitPath
= tuName
;
3596 _file
->_dwarfTranslationUnitPath
= NULL
;
3599 // add line number info to atoms from dwarf
3600 std::vector
<AtomAndLineInfo
<A
> > entries
;
3601 entries
.reserve(64);
3602 if ( _file
->_debugInfoKind
== ld::relocatable::File::kDebugInfoDwarf
) {
3603 // file with just data will have no __debug_line info
3604 if ( (_file
->_dwarfDebugLineSect
!= NULL
) && (_file
->_dwarfDebugLineSect
->size() != 0) ) {
3605 // validate stmt_list
3606 if ( (stmtList
!= (uint64_t)-1) && (stmtList
< _file
->_dwarfDebugLineSect
->size()) ) {
3607 const uint8_t* debug_line
= (uint8_t*)_file
->fileContent() + _file
->_dwarfDebugLineSect
->offset();
3608 struct line_reader_data
* lines
= line_open(&debug_line
[stmtList
],
3609 _file
->_dwarfDebugLineSect
->size() - stmtList
, E::little_endian
);
3610 struct line_info result
;
3611 Atom
<A
>* curAtom
= NULL
;
3612 uint32_t curAtomOffset
= 0;
3613 uint32_t curAtomAddress
= 0;
3614 uint32_t curAtomSize
= 0;
3615 std::map
<uint32_t,const char*> dwarfIndexToFile
;
3616 if ( lines
!= NULL
) {
3617 while ( line_next(lines
, &result
, line_stop_pc
) ) {
3618 //fprintf(stderr, "curAtom=%p, result.pc=0x%llX, result.line=%llu, result.end_of_sequence=%d,"
3619 // " curAtomAddress=0x%X, curAtomSize=0x%X\n",
3620 // curAtom, result.pc, result.line, result.end_of_sequence, curAtomAddress, curAtomSize);
3621 // work around weird debug line table compiler generates if no functions in __text section
3622 if ( (curAtom
== NULL
) && (result
.pc
== 0) && result
.end_of_sequence
&& (result
.file
== 1))
3624 // for performance, see if in next pc is in current atom
3625 if ( (curAtom
!= NULL
) && (curAtomAddress
<= result
.pc
) && (result
.pc
< (curAtomAddress
+curAtomSize
)) ) {
3626 curAtomOffset
= result
.pc
- curAtomAddress
;
3628 // or pc at end of current atom
3629 else if ( result
.end_of_sequence
&& (curAtom
!= NULL
) && (result
.pc
== (curAtomAddress
+curAtomSize
)) ) {
3630 curAtomOffset
= result
.pc
- curAtomAddress
;
3632 // or only one function that is a one line function
3633 else if ( result
.end_of_sequence
&& (curAtom
== NULL
) && (this->findAtomByAddress(0) != NULL
) && (result
.pc
== this->findAtomByAddress(0)->size()) ) {
3634 curAtom
= this->findAtomByAddress(0);
3635 curAtomOffset
= result
.pc
- curAtom
->objectAddress();
3636 curAtomAddress
= curAtom
->objectAddress();
3637 curAtomSize
= curAtom
->size();
3640 // do slow look up of atom by address
3642 curAtom
= this->findAtomByAddress(result
.pc
);
3645 // in case of bug in debug info, don't abort link, just limp on
3648 if ( curAtom
== NULL
)
3649 break; // file has line info but no functions
3650 if ( result
.end_of_sequence
&& (curAtomAddress
+curAtomSize
< result
.pc
) ) {
3651 // a one line function can be returned by line_next() as one entry with pc at end of blob
3652 // look for alt atom starting at end of previous atom
3653 uint32_t previousEnd
= curAtomAddress
+curAtomSize
;
3654 Atom
<A
>* alt
= this->findAtomByAddressOrNullIfStub(previousEnd
);
3656 continue; // ignore spurious debug info for stubs
3657 if ( result
.pc
<= alt
->objectAddress() + alt
->size() ) {
3659 curAtomOffset
= result
.pc
- alt
->objectAddress();
3660 curAtomAddress
= alt
->objectAddress();
3661 curAtomSize
= alt
->size();
3664 curAtomOffset
= result
.pc
- curAtom
->objectAddress();
3665 curAtomAddress
= curAtom
->objectAddress();
3666 curAtomSize
= curAtom
->size();
3670 curAtomOffset
= result
.pc
- curAtom
->objectAddress();
3671 curAtomAddress
= curAtom
->objectAddress();
3672 curAtomSize
= curAtom
->size();
3675 const char* filename
;
3676 std::map
<uint32_t,const char*>::iterator pos
= dwarfIndexToFile
.find(result
.file
);
3677 if ( pos
== dwarfIndexToFile
.end() ) {
3678 filename
= line_file(lines
, result
.file
);
3679 dwarfIndexToFile
[result
.file
] = filename
;
3682 filename
= pos
->second
;
3684 // only record for ~8000 line info records per function
3685 if ( curAtom
->roomForMoreLineInfoCount() ) {
3686 AtomAndLineInfo
<A
> entry
;
3687 entry
.atom
= curAtom
;
3688 entry
.info
.atomOffset
= curAtomOffset
;
3689 entry
.info
.fileName
= filename
;
3690 entry
.info
.lineNumber
= result
.line
;
3691 //fprintf(stderr, "addr=0x%08llX, line=%lld, file=%s, atom=%s, atom.size=0x%X, end=%d\n",
3692 // result.pc, result.line, filename, curAtom->name(), curAtomSize, result.end_of_sequence);
3693 entries
.push_back(entry
);
3694 curAtom
->incrementLineInfoCount();
3696 if ( result
.end_of_sequence
) {
3706 // assign line info start offset for each atom
3707 uint8_t* p
= _file
->_atomsArray
;
3708 uint32_t liOffset
= 0;
3709 for(int i
=_file
->_atomsArrayCount
; i
> 0; --i
) {
3710 Atom
<A
>* atom
= (Atom
<A
>*)p
;
3711 atom
->_lineInfoStartIndex
= liOffset
;
3712 liOffset
+= atom
->_lineInfoCount
;
3713 atom
->_lineInfoCount
= 0;
3714 p
+= sizeof(Atom
<A
>);
3716 assert(liOffset
== entries
.size());
3717 _file
->_lineInfos
.resize(liOffset
);
3719 // copy each line info for each atom
3720 for (typename
std::vector
<AtomAndLineInfo
<A
> >::iterator it
= entries
.begin(); it
!= entries
.end(); ++it
) {
3721 uint32_t slot
= it
->atom
->_lineInfoStartIndex
+ it
->atom
->_lineInfoCount
;
3722 _file
->_lineInfos
[slot
] = it
->info
;
3723 it
->atom
->_lineInfoCount
++;
3726 // done with temp vector
3730 template <typename A
>
3731 void Parser
<A
>::parseStabs()
3733 // scan symbol table for stabs entries
3734 Atom
<A
>* currentAtom
= NULL
;
3735 pint_t currentAtomAddress
= 0;
3736 enum { start
, inBeginEnd
, inFun
} state
= start
;
3737 for (uint32_t symbolIndex
= 0; symbolIndex
< _symbolCount
; ++symbolIndex
) {
3738 const macho_nlist
<P
>& sym
= this->symbolFromIndex(symbolIndex
);
3739 bool useStab
= true;
3740 uint8_t type
= sym
.n_type();
3741 const char* symString
= (sym
.n_strx() != 0) ? this->nameFromSymbol(sym
) : NULL
;
3742 if ( (type
& N_STAB
) != 0 ) {
3743 _file
->_debugInfoKind
= (_hasUUID
? ld::relocatable::File::kDebugInfoStabsUUID
: ld::relocatable::File::kDebugInfoStabs
);
3744 ld::relocatable::File::Stab stab
;
3747 stab
.other
= sym
.n_sect();
3748 stab
.desc
= sym
.n_desc();
3749 stab
.value
= sym
.n_value();
3755 // beginning of function block
3757 // fall into case to lookup atom by addresss
3760 currentAtomAddress
= sym
.n_value();
3761 currentAtom
= this->findAtomByAddress(currentAtomAddress
);
3762 if ( currentAtom
!= NULL
) {
3763 stab
.atom
= currentAtom
;
3764 stab
.string
= symString
;
3767 fprintf(stderr
, "can't find atom for stabs BNSYM at %08llX in %s",
3768 (uint64_t)sym
.n_value(), _path
);
3778 // not associated with an atom, just copy
3779 stab
.string
= symString
;
3783 // n_value field is NOT atom address ;-(
3784 // need to find atom by name match
3785 const char* colon
= strchr(symString
, ':');
3786 if ( colon
!= NULL
) {
3787 // build underscore leading name
3788 int nameLen
= colon
- symString
;
3789 char symName
[nameLen
+2];
3790 strlcpy(&symName
[1], symString
, nameLen
+1);
3792 symName
[nameLen
+1] = '\0';
3793 currentAtom
= this->findAtomByName(symName
);
3794 if ( currentAtom
!= NULL
) {
3795 stab
.atom
= currentAtom
;
3796 stab
.string
= symString
;
3800 // might be a debug-note without trailing :G()
3801 currentAtom
= this->findAtomByName(symString
);
3802 if ( currentAtom
!= NULL
) {
3803 stab
.atom
= currentAtom
;
3804 stab
.string
= symString
;
3807 if ( stab
.atom
== NULL
) {
3808 // ld_classic added bogus GSYM stabs for old style dtrace probes
3809 if ( (strncmp(symString
, "__dtrace_probe$", 15) != 0) )
3810 warning("can't find atom for N_GSYM stabs %s in %s", symString
, _path
);
3816 if ( isConstFunStabs(symString
) ) {
3817 // constant not associated with a function
3818 stab
.string
= symString
;
3821 // old style stabs without BNSYM
3823 currentAtomAddress
= sym
.n_value();
3824 currentAtom
= this->findAtomByAddress(currentAtomAddress
);
3825 if ( currentAtom
!= NULL
) {
3826 stab
.atom
= currentAtom
;
3827 stab
.string
= symString
;
3830 warning("can't find atom for stabs FUN at %08llX in %s",
3831 (uint64_t)currentAtomAddress
, _path
);
3837 stab
.string
= symString
;
3843 stab
.string
= symString
;
3844 // -gfull built .o file
3847 warning("unknown stabs type 0x%X in %s", type
, _path
);
3851 stab
.atom
= currentAtom
;
3860 Atom
<A
>* nestedAtom
= this->findAtomByAddress(sym
.n_value());
3861 if ( nestedAtom
!= NULL
) {
3862 stab
.atom
= nestedAtom
;
3863 stab
.string
= symString
;
3866 warning("can't find atom for stabs 0x%X at %08llX in %s",
3867 type
, (uint64_t)sym
.n_value(), _path
);
3874 // adjust value to be offset in atom
3875 stab
.value
-= currentAtomAddress
;
3877 stab
.string
= symString
;
3884 if ( isConstFunStabs(symString
) ) {
3885 stab
.atom
= currentAtom
;
3886 stab
.string
= symString
;
3889 if ( sym
.n_sect() != 0 ) {
3890 // found another start stab, must be really old stabs...
3891 currentAtomAddress
= sym
.n_value();
3892 currentAtom
= this->findAtomByAddress(currentAtomAddress
);
3893 if ( currentAtom
!= NULL
) {
3894 stab
.atom
= currentAtom
;
3895 stab
.string
= symString
;
3898 warning("can't find atom for stabs FUN at %08llX in %s",
3899 (uint64_t)currentAtomAddress
, _path
);
3903 // found ending stab, switch back to start state
3904 stab
.string
= symString
;
3905 stab
.atom
= currentAtom
;
3914 // adjust value to be offset in atom
3915 stab
.value
-= currentAtomAddress
;
3916 stab
.atom
= currentAtom
;
3919 stab
.string
= symString
;
3923 stab
.atom
= currentAtom
;
3924 stab
.string
= symString
;
3929 // add to list of stabs for this .o file
3931 _file
->_stabs
.push_back(stab
);
3938 // Look at the compilation unit DIE and determine
3939 // its NAME, compilation directory (in COMP_DIR) and its
3940 // line number information offset (in STMT_LIST). NAME and COMP_DIR
3941 // may be NULL (especially COMP_DIR) if they are not in the .o file;
3942 // STMT_LIST will be (uint64_t) -1.
3944 // At present this assumes that there's only one compilation unit DIE.
3946 template <typename A
>
3947 bool Parser
<A
>::read_comp_unit(const char ** name
, const char ** comp_dir
,
3948 uint64_t *stmt_list
)
3950 const uint8_t * debug_info
;
3951 const uint8_t * debug_abbrev
;
3954 const uint8_t * end
;
3955 const uint8_t * enda
;
3958 uint64_t abbrev_base
;
3960 uint8_t address_size
;
3965 *stmt_list
= (uint64_t) -1;
3967 if ( (_file
->_dwarfDebugInfoSect
== NULL
) || (_file
->_dwarfDebugAbbrevSect
== NULL
) )
3970 debug_info
= (uint8_t*)_file
->fileContent() + _file
->_dwarfDebugInfoSect
->offset();
3971 debug_abbrev
= (uint8_t*)_file
->fileContent() + _file
->_dwarfDebugAbbrevSect
->offset();
3974 if (_file
->_dwarfDebugInfoSect
->size() < 12)
3975 /* Too small to be a real debug_info section. */
3977 sz
= A::P::E::get32(*(uint32_t*)di
);
3979 dwarf64
= sz
== 0xffffffff;
3981 sz
= A::P::E::get64(*(uint64_t*)di
), di
+= 8;
3982 else if (sz
> 0xffffff00)
3983 /* Unknown dwarf format. */
3986 /* Verify claimed size. */
3987 if (sz
+ (di
- debug_info
) > _file
->_dwarfDebugInfoSect
->size() || sz
<= (dwarf64
? 23 : 11))
3990 vers
= A::P::E::get16(*(uint16_t*)di
);
3991 if (vers
< 2 || vers
> 4)
3992 /* DWARF version wrong for this code.
3993 Chances are we could continue anyway, but we don't know for sure. */
3997 /* Find the debug_abbrev section. */
3998 abbrev_base
= dwarf64
? A::P::E::get64(*(uint64_t*)di
) : A::P::E::get32(*(uint32_t*)di
);
3999 di
+= dwarf64
? 8 : 4;
4001 if (abbrev_base
> _file
->_dwarfDebugAbbrevSect
->size())
4003 da
= debug_abbrev
+ abbrev_base
;
4004 enda
= debug_abbrev
+ _file
->_dwarfDebugAbbrevSect
->size();
4006 address_size
= *di
++;
4008 /* Find the abbrev number we're looking for. */
4010 abbrev
= read_uleb128 (&di
, end
);
4011 if (abbrev
== (uint64_t) -1)
4014 /* Skip through the debug_abbrev section looking for that abbrev. */
4017 uint64_t this_abbrev
= read_uleb128 (&da
, enda
);
4020 if (this_abbrev
== abbrev
)
4021 /* This is almost always taken. */
4023 skip_leb128 (&da
, enda
); /* Skip the tag. */
4026 da
++; /* Skip the DW_CHILDREN_* value. */
4029 attr
= read_uleb128 (&da
, enda
);
4030 skip_leb128 (&da
, enda
);
4031 } while (attr
!= 0 && attr
!= (uint64_t) -1);
4036 /* Check that the abbrev is one for a DW_TAG_compile_unit. */
4037 if (read_uleb128 (&da
, enda
) != DW_TAG_compile_unit
)
4041 da
++; /* Skip the DW_CHILDREN_* value. */
4043 /* Now, go through the DIE looking for DW_AT_name,
4044 DW_AT_comp_dir, and DW_AT_stmt_list. */
4047 uint64_t attr
= read_uleb128 (&da
, enda
);
4048 uint64_t form
= read_uleb128 (&da
, enda
);
4050 if (attr
== (uint64_t) -1)
4054 if (form
== DW_FORM_indirect
)
4055 form
= read_uleb128 (&di
, end
);
4059 *name
= getDwarfString(form
, di
);
4061 case DW_AT_comp_dir
:
4062 *comp_dir
= getDwarfString(form
, di
);
4064 case DW_AT_stmt_list
:
4065 *stmt_list
= getDwarfOffset(form
, di
, dwarf64
);
4068 if (! skip_form (&di
, end
, form
, address_size
, dwarf64
))
4076 template <typename A
>
4079 free(_sectionsArray
);
4083 template <typename A
>
4084 const char* File
<A
>::translationUnitSource() const
4086 return _dwarfTranslationUnitPath
;
4089 template <typename A
>
4090 bool File
<A
>::forEachAtom(ld::File::AtomHandler
& handler
) const
4092 handler
.doFile(*this);
4093 uint8_t* p
= _atomsArray
;
4094 for(int i
=_atomsArrayCount
; i
> 0; --i
) {
4095 handler
.doAtom(*((Atom
<A
>*)p
));
4096 p
+= sizeof(Atom
<A
>);
4098 p
= _aliasAtomsArray
;
4099 for(int i
=_aliasAtomsArrayCount
; i
> 0; --i
) {
4100 handler
.doAtom(*((AliasAtom
*)p
));
4101 p
+= sizeof(AliasAtom
);
4104 return (_atomsArrayCount
!= 0) || (_aliasAtomsArrayCount
!= 0);
4107 template <typename A
>
4108 const char* Section
<A
>::makeSegmentName(const macho_section
<typename
A::P
>* sect
)
4110 // mach-o section record only has room for 16-byte seg/sect names
4111 // so a 16-byte name has no trailing zero
4112 const char* name
= sect
->segname();
4113 if ( strlen(name
) < 16 )
4115 char* tmp
= new char[17];
4116 strlcpy(tmp
, name
, 17);
4120 template <typename A
>
4121 const char* Section
<A
>::makeSectionName(const macho_section
<typename
A::P
>* sect
)
4123 const char* name
= sect
->sectname();
4124 if ( strlen(name
) < 16 )
4127 // special case common long section names so we don't have to malloc
4128 if ( strncmp(sect
->sectname(), "__objc_classrefs", 16) == 0 )
4129 return "__objc_classrefs";
4130 if ( strncmp(sect
->sectname(), "__objc_classlist", 16) == 0 )
4131 return "__objc_classlist";
4132 if ( strncmp(sect
->sectname(), "__objc_nlclslist", 16) == 0 )
4133 return "__objc_nlclslist";
4134 if ( strncmp(sect
->sectname(), "__objc_nlcatlist", 16) == 0 )
4135 return "__objc_nlcatlist";
4136 if ( strncmp(sect
->sectname(), "__objc_protolist", 16) == 0 )
4137 return "__objc_protolist";
4138 if ( strncmp(sect
->sectname(), "__objc_protorefs", 16) == 0 )
4139 return "__objc_protorefs";
4140 if ( strncmp(sect
->sectname(), "__objc_superrefs", 16) == 0 )
4141 return "__objc_superrefs";
4142 if ( strncmp(sect
->sectname(), "__objc_imageinfo", 16) == 0 )
4143 return "__objc_imageinfo";
4144 if ( strncmp(sect
->sectname(), "__objc_stringobj", 16) == 0 )
4145 return "__objc_stringobj";
4146 if ( strncmp(sect
->sectname(), "__gcc_except_tab", 16) == 0 )
4147 return "__gcc_except_tab";
4149 char* tmp
= new char[17];
4150 strlcpy(tmp
, name
, 17);
4154 template <typename A
>
4155 bool Section
<A
>::readable(const macho_section
<typename
A::P
>* sect
)
4160 template <typename A
>
4161 bool Section
<A
>::writable(const macho_section
<typename
A::P
>* sect
)
4163 // mach-o .o files do not contain segment permissions
4164 // we just know TEXT is special
4165 return ( strcmp(sect
->segname(), "__TEXT") != 0 );
4168 template <typename A
>
4169 bool Section
<A
>::exectuable(const macho_section
<typename
A::P
>* sect
)
4171 // mach-o .o files do not contain segment permissions
4172 // we just know TEXT is special
4173 return ( strcmp(sect
->segname(), "__TEXT") == 0 );
4177 template <typename A
>
4178 ld::Section::Type Section
<A
>::sectionType(const macho_section
<typename
A::P
>* sect
)
4180 switch ( sect
->flags() & SECTION_TYPE
) {
4182 return ld::Section::typeZeroFill
;
4183 case S_CSTRING_LITERALS
:
4184 if ( (strcmp(sect
->sectname(), "__cstring") == 0) && (strcmp(sect
->segname(), "__TEXT") == 0) )
4185 return ld::Section::typeCString
;
4187 return ld::Section::typeNonStdCString
;
4188 case S_4BYTE_LITERALS
:
4189 return ld::Section::typeLiteral4
;
4190 case S_8BYTE_LITERALS
:
4191 return ld::Section::typeLiteral8
;
4192 case S_LITERAL_POINTERS
:
4193 return ld::Section::typeCStringPointer
;
4194 case S_NON_LAZY_SYMBOL_POINTERS
:
4195 return ld::Section::typeNonLazyPointer
;
4196 case S_LAZY_SYMBOL_POINTERS
:
4197 return ld::Section::typeLazyPointer
;
4198 case S_SYMBOL_STUBS
:
4199 return ld::Section::typeStub
;
4200 case S_MOD_INIT_FUNC_POINTERS
:
4201 return ld::Section::typeInitializerPointers
;
4202 case S_MOD_TERM_FUNC_POINTERS
:
4203 return ld::Section::typeTerminatorPointers
;
4205 return ld::Section::typeUnclassified
;
4206 case S_16BYTE_LITERALS
:
4207 return ld::Section::typeLiteral16
;
4210 if ( sect
->flags() & S_ATTR_PURE_INSTRUCTIONS
) {
4211 return ld::Section::typeCode
;
4213 else if ( strcmp(sect
->segname(), "__TEXT") == 0 ) {
4214 if ( strcmp(sect
->sectname(), "__eh_frame") == 0 )
4215 return ld::Section::typeCFI
;
4216 else if ( strcmp(sect
->sectname(), "__ustring") == 0 )
4217 return ld::Section::typeUTF16Strings
;
4218 else if ( strcmp(sect
->sectname(), "__textcoal_nt") == 0 )
4219 return ld::Section::typeCode
;
4220 else if ( strcmp(sect
->sectname(), "__StaticInit") == 0 )
4221 return ld::Section::typeCode
;
4222 else if ( strcmp(sect
->sectname(), "__constructor") == 0 )
4223 return ld::Section::typeInitializerPointers
;
4225 else if ( strcmp(sect
->segname(), "__DATA") == 0 ) {
4226 if ( strcmp(sect
->sectname(), "__cfstring") == 0 )
4227 return ld::Section::typeCFString
;
4228 else if ( strcmp(sect
->sectname(), "__dyld") == 0 )
4229 return ld::Section::typeDyldInfo
;
4230 else if ( strcmp(sect
->sectname(), "__program_vars") == 0 )
4231 return ld::Section::typeDyldInfo
;
4232 else if ( strncmp(sect
->sectname(), "__objc_classrefs", 16) == 0 )
4233 return ld::Section::typeObjCClassRefs
;
4234 else if ( strcmp(sect
->sectname(), "__objc_catlist") == 0 )
4235 return ld::Section::typeObjC2CategoryList
;
4237 else if ( strcmp(sect
->segname(), "__OBJC") == 0 ) {
4238 if ( strcmp(sect
->sectname(), "__class") == 0 )
4239 return ld::Section::typeObjC1Classes
;
4242 case S_THREAD_LOCAL_REGULAR
:
4243 return ld::Section::typeTLVInitialValues
;
4244 case S_THREAD_LOCAL_ZEROFILL
:
4245 return ld::Section::typeTLVZeroFill
;
4246 case S_THREAD_LOCAL_VARIABLES
:
4247 return ld::Section::typeTLVDefs
;
4248 case S_THREAD_LOCAL_VARIABLE_POINTERS
:
4249 return ld::Section::typeTLVPointers
;
4250 case S_THREAD_LOCAL_INIT_FUNCTION_POINTERS
:
4251 return ld::Section::typeTLVInitializerPointers
;
4253 return ld::Section::typeUnclassified
;
4257 template <typename A
>
4258 Atom
<A
>* Section
<A
>::findContentAtomByAddress(pint_t addr
, class Atom
<A
>* start
, class Atom
<A
>* end
)
4260 // do a binary search of atom array
4261 uint32_t atomCount
= end
- start
;
4262 Atom
<A
>* base
= start
;
4263 for (uint32_t n
= atomCount
; n
> 0; n
/= 2) {
4264 Atom
<A
>* pivot
= &base
[n
/2];
4265 pint_t atomStartAddr
= pivot
->_objAddress
;
4266 pint_t atomEndAddr
= atomStartAddr
+ pivot
->_size
;
4267 if ( atomStartAddr
<= addr
) {
4268 // address in normal atom
4269 if (addr
< atomEndAddr
)
4271 // address in "end" label (but not in alias)
4272 if ( (pivot
->_size
== 0) && (addr
== atomEndAddr
) && !pivot
->isAlias() )
4275 if ( addr
>= atomEndAddr
) {
4277 // move base to atom after pivot
4289 template <typename A
>
4290 ld::Atom::Alignment Section
<A
>::alignmentForAddress(pint_t addr
)
4292 const uint32_t sectionAlignment
= this->_machOSection
->align();
4293 uint32_t modulus
= (addr
% (1 << sectionAlignment
));
4294 if ( modulus
> 0xFFFF )
4295 warning("alignment for symbol at address 0x%08llX in %s exceeds 2^16", (uint64_t)addr
, this->file().path());
4296 return ld::Atom::Alignment(sectionAlignment
, modulus
);
4299 template <typename A
>
4300 uint32_t Section
<A
>::sectionNum(class Parser
<A
>& parser
) const
4302 if ( _machOSection
== NULL
)
4305 return 1 + (this->_machOSection
- parser
.firstMachOSection());
4308 // arm does not have zero cost exceptions
4310 uint32_t CFISection
<arm
>::cfiCount(Parser
<arm
>& parser
)
4312 if ( parser
.armUsesZeroCostExceptions() ) {
4313 // create ObjectAddressSpace object for use by libunwind
4314 OAS
oas(*this, (uint8_t*)this->file().fileContent()+this->_machOSection
->offset());
4315 return libunwind::CFI_Parser
<OAS
>::getCFICount(oas
,
4316 this->_machOSection
->addr(), this->_machOSection
->size());
4321 template <typename A
>
4322 uint32_t CFISection
<A
>::cfiCount(Parser
<A
>& parser
)
4324 // create ObjectAddressSpace object for use by libunwind
4325 OAS
oas(*this, (uint8_t*)this->file().fileContent()+this->_machOSection
->offset());
4326 return libunwind::CFI_Parser
<OAS
>::getCFICount(oas
,
4327 this->_machOSection
->addr(), this->_machOSection
->size());
4330 template <typename A
>
4331 void CFISection
<A
>::warnFunc(void* ref
, uint64_t funcAddr
, const char* msg
)
4333 Parser
<A
>* parser
= (Parser
<A
>*)ref
;
4334 if ( ! parser
->warnUnwindConversionProblems() )
4336 if ( funcAddr
!= CFI_INVALID_ADDRESS
) {
4337 // atoms are not constructed yet, so scan symbol table for labels
4338 const char* name
= parser
->scanSymbolTableForAddress(funcAddr
);
4339 warning("could not create compact unwind for %s: %s", name
, msg
);
4342 warning("could not create compact unwind: %s", msg
);
4347 bool CFISection
<x86_64
>::needsRelocating()
4353 bool CFISection
<arm64
>::needsRelocating()
4358 template <typename A
>
4359 bool CFISection
<A
>::needsRelocating()
4365 void CFISection
<x86_64
>::cfiParse(class Parser
<x86_64
>& parser
, uint8_t* buffer
,
4366 libunwind::CFI_Atom_Info
<CFISection
<x86_64
>::OAS
>::CFI_Atom_Info cfiArray
[],
4367 uint32_t& count
, const pint_t cuStarts
[], uint32_t cuCount
)
4369 // copy __eh_frame data to buffer
4370 memcpy(buffer
, file().fileContent() + this->_machOSection
->offset(), this->_machOSection
->size());
4372 // and apply relocations
4373 const macho_relocation_info
<P
>* relocs
= (macho_relocation_info
<P
>*)(file().fileContent() + this->_machOSection
->reloff());
4374 const macho_relocation_info
<P
>* relocsEnd
= &relocs
[this->_machOSection
->nreloc()];
4375 for (const macho_relocation_info
<P
>* reloc
= relocs
; reloc
< relocsEnd
; ++reloc
) {
4377 switch ( reloc
->r_type() ) {
4378 case X86_64_RELOC_SUBTRACTOR
:
4379 value
= 0 - parser
.symbolFromIndex(reloc
->r_symbolnum()).n_value();
4381 if ( reloc
->r_extern() )
4382 value
+= parser
.symbolFromIndex(reloc
->r_symbolnum()).n_value();
4384 case X86_64_RELOC_UNSIGNED
:
4385 value
= parser
.symbolFromIndex(reloc
->r_symbolnum()).n_value();
4387 case X86_64_RELOC_GOT
:
4388 // this is used for the reference to the personality function in CIEs
4389 // store the symbol number of the personality function for later use as a Fixup
4390 value
= reloc
->r_symbolnum();
4393 fprintf(stderr
, "CFISection::cfiParse() unexpected relocation type at r_address=0x%08X\n", reloc
->r_address());
4398 switch ( reloc
->r_length() ) {
4400 p64
= (uint64_t*)&buffer
[reloc
->r_address()];
4401 E::set64(*p64
, value
+ E::get64(*p64
));
4404 p32
= (uint32_t*)&buffer
[reloc
->r_address()];
4405 E::set32(*p32
, value
+ E::get32(*p32
));
4408 fprintf(stderr
, "CFISection::cfiParse() unexpected relocation size at r_address=0x%08X\n", reloc
->r_address());
4413 // create ObjectAddressSpace object for use by libunwind
4414 OAS
oas(*this, buffer
);
4416 // use libuwind to parse __eh_frame data into array of CFI_Atom_Info
4418 msg
= libunwind::DwarfInstructions
<OAS
, libunwind::Registers_x86_64
>::parseCFIs(
4419 oas
, this->_machOSection
->addr(), this->_machOSection
->size(),
4420 cuStarts
, cuCount
, parser
.keepDwarfUnwind(), parser
.forceDwarfConversion(), parser
.neverConvertDwarf(),
4421 cfiArray
, count
, (void*)&parser
, warnFunc
);
4423 throwf("malformed __eh_frame section: %s", msg
);
4427 void CFISection
<x86
>::cfiParse(class Parser
<x86
>& parser
, uint8_t* buffer
,
4428 libunwind::CFI_Atom_Info
<CFISection
<x86
>::OAS
>::CFI_Atom_Info cfiArray
[],
4429 uint32_t& count
, const pint_t cuStarts
[], uint32_t cuCount
)
4431 // create ObjectAddressSpace object for use by libunwind
4432 OAS
oas(*this, (uint8_t*)this->file().fileContent()+this->_machOSection
->offset());
4434 // use libuwind to parse __eh_frame data into array of CFI_Atom_Info
4436 msg
= libunwind::DwarfInstructions
<OAS
, libunwind::Registers_x86
>::parseCFIs(
4437 oas
, this->_machOSection
->addr(), this->_machOSection
->size(),
4438 cuStarts
, cuCount
, parser
.keepDwarfUnwind(), parser
.forceDwarfConversion(), parser
.neverConvertDwarf(),
4439 cfiArray
, count
, (void*)&parser
, warnFunc
);
4441 throwf("malformed __eh_frame section: %s", msg
);
4448 void CFISection
<arm
>::cfiParse(class Parser
<arm
>& parser
, uint8_t* buffer
,
4449 libunwind::CFI_Atom_Info
<CFISection
<arm
>::OAS
>::CFI_Atom_Info cfiArray
[],
4450 uint32_t& count
, const pint_t cuStarts
[], uint32_t cuCount
)
4452 if ( !parser
.armUsesZeroCostExceptions() ) {
4453 // most arm do not use zero cost exceptions
4457 // create ObjectAddressSpace object for use by libunwind
4458 OAS
oas(*this, (uint8_t*)this->file().fileContent()+this->_machOSection
->offset());
4460 // use libuwind to parse __eh_frame data into array of CFI_Atom_Info
4462 msg
= libunwind::DwarfInstructions
<OAS
, libunwind::Registers_arm
>::parseCFIs(
4463 oas
, this->_machOSection
->addr(), this->_machOSection
->size(),
4464 cuStarts
, cuCount
, parser
.keepDwarfUnwind(), parser
.forceDwarfConversion(), parser
.neverConvertDwarf(),
4465 cfiArray
, count
, (void*)&parser
, warnFunc
);
4467 throwf("malformed __eh_frame section: %s", msg
);
4474 void CFISection
<arm64
>::cfiParse(class Parser
<arm64
>& parser
, uint8_t* buffer
,
4475 libunwind::CFI_Atom_Info
<CFISection
<arm64
>::OAS
>::CFI_Atom_Info cfiArray
[],
4476 uint32_t& count
, const pint_t cuStarts
[], uint32_t cuCount
)
4478 // copy __eh_frame data to buffer
4479 memcpy(buffer
, file().fileContent() + this->_machOSection
->offset(), this->_machOSection
->size());
4481 // and apply relocations
4482 const macho_relocation_info
<P
>* relocs
= (macho_relocation_info
<P
>*)(file().fileContent() + this->_machOSection
->reloff());
4483 const macho_relocation_info
<P
>* relocsEnd
= &relocs
[this->_machOSection
->nreloc()];
4484 for (const macho_relocation_info
<P
>* reloc
= relocs
; reloc
< relocsEnd
; ++reloc
) {
4485 uint64_t* p64
= (uint64_t*)&buffer
[reloc
->r_address()];
4486 uint32_t* p32
= (uint32_t*)&buffer
[reloc
->r_address()];
4487 uint32_t addend32
= E::get32(*p32
);
4488 uint64_t addend64
= E::get64(*p64
);
4490 switch ( reloc
->r_type() ) {
4491 case ARM64_RELOC_SUBTRACTOR
:
4492 value
= 0 - parser
.symbolFromIndex(reloc
->r_symbolnum()).n_value();
4494 if ( reloc
->r_extern() )
4495 value
+= parser
.symbolFromIndex(reloc
->r_symbolnum()).n_value();
4497 case ARM64_RELOC_UNSIGNED
:
4498 value
= parser
.symbolFromIndex(reloc
->r_symbolnum()).n_value();
4500 case ARM64_RELOC_POINTER_TO_GOT
:
4501 // this is used for the reference to the personality function in CIEs
4502 // store the symbol number of the personality function for later use as a Fixup
4503 value
= reloc
->r_symbolnum();
4508 fprintf(stderr
, "CFISection::cfiParse() unexpected relocation type at r_address=0x%08X\n", reloc
->r_address());
4511 switch ( reloc
->r_length() ) {
4513 E::set64(*p64
, value
+ addend64
);
4516 E::set32(*p32
, value
+ addend32
);
4519 fprintf(stderr
, "CFISection::cfiParse() unexpected relocation size at r_address=0x%08X\n", reloc
->r_address());
4525 // create ObjectAddressSpace object for use by libunwind
4526 OAS
oas(*this, buffer
);
4528 // use libuwind to parse __eh_frame data into array of CFI_Atom_Info
4530 msg
= libunwind::DwarfInstructions
<OAS
, libunwind::Registers_arm64
>::parseCFIs(
4531 oas
, this->_machOSection
->addr(), this->_machOSection
->size(),
4532 cuStarts
, cuCount
, parser
.keepDwarfUnwind(), parser
.forceDwarfConversion(), parser
.neverConvertDwarf(),
4533 cfiArray
, count
, (void*)&parser
, warnFunc
);
4535 throwf("malformed __eh_frame section: %s", msg
);
4539 template <typename A
>
4540 uint32_t CFISection
<A
>::computeAtomCount(class Parser
<A
>& parser
,
4541 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
4542 const struct Parser
<A
>::CFI_CU_InfoArrays
& cfis
)
4544 return cfis
.cfiCount
;
4549 template <typename A
>
4550 uint32_t CFISection
<A
>::appendAtoms(class Parser
<A
>& parser
, uint8_t* p
,
4551 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
4552 const struct Parser
<A
>::CFI_CU_InfoArrays
& cfis
)
4554 this->_beginAtoms
= (Atom
<A
>*)p
;
4555 // walk CFI_Atom_Info array and create atom for each entry
4556 const CFI_Atom_Info
* start
= &cfis
.cfiArray
[0];
4557 const CFI_Atom_Info
* end
= &cfis
.cfiArray
[cfis
.cfiCount
];
4558 for(const CFI_Atom_Info
* a
=start
; a
< end
; ++a
) {
4559 Atom
<A
>* space
= (Atom
<A
>*)p
;
4560 new (space
) Atom
<A
>(*this, (a
->isCIE
? "CIE" : "FDE"), a
->address
, a
->size
,
4561 ld::Atom::definitionRegular
, ld::Atom::combineNever
, ld::Atom::scopeTranslationUnit
,
4562 ld::Atom::typeCFI
, ld::Atom::symbolTableNotInFinalLinkedImages
,
4563 false, false, false, ld::Atom::Alignment(0));
4564 p
+= sizeof(Atom
<A
>);
4566 this->_endAtoms
= (Atom
<A
>*)p
;
4567 return cfis
.cfiCount
;
4571 template <> bool CFISection
<x86_64
>::bigEndian() { return false; }
4572 template <> bool CFISection
<x86
>::bigEndian() { return false; }
4573 template <> bool CFISection
<arm
>::bigEndian() { return false; }
4574 template <> bool CFISection
<arm64
>::bigEndian() { return false; }
4578 void CFISection
<x86_64
>::addCiePersonalityFixups(class Parser
<x86_64
>& parser
, const CFI_Atom_Info
* cieInfo
)
4580 uint8_t personalityEncoding
= cieInfo
->u
.cieInfo
.personality
.encodingOfTargetAddress
;
4581 if ( personalityEncoding
== 0x9B ) {
4582 // compiler always produces X86_64_RELOC_GOT with addend of 4 to personality function
4583 // CFISection<x86_64>::cfiParse() set targetAddress to be symbolIndex + 4 + addressInCIE
4584 uint32_t symbolIndex
= cieInfo
->u
.cieInfo
.personality
.targetAddress
- 4
4585 - cieInfo
->address
- cieInfo
->u
.cieInfo
.personality
.offsetInCFI
;
4586 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(symbolIndex
);
4587 const char* personalityName
= parser
.nameFromSymbol(sym
);
4589 Atom
<x86_64
>* cieAtom
= this->findAtomByAddress(cieInfo
->address
);
4590 Parser
<x86_64
>::SourceLocation
src(cieAtom
, cieInfo
->u
.cieInfo
.personality
.offsetInCFI
);
4591 parser
.addFixup(src
, ld::Fixup::k1of3
, ld::Fixup::kindSetTargetAddress
, false, personalityName
);
4592 parser
.addFixup(src
, ld::Fixup::k2of3
, ld::Fixup::kindAddAddend
, 4);
4593 parser
.addFixup(src
, ld::Fixup::k3of3
, ld::Fixup::kindStoreX86PCRel32GOT
);
4595 else if ( personalityEncoding
!= 0 ) {
4596 throwf("unsupported address encoding (%02X) of personality function in CIE",
4597 personalityEncoding
);
4602 void CFISection
<x86
>::addCiePersonalityFixups(class Parser
<x86
>& parser
, const CFI_Atom_Info
* cieInfo
)
4604 uint8_t personalityEncoding
= cieInfo
->u
.cieInfo
.personality
.encodingOfTargetAddress
;
4605 if ( (personalityEncoding
== 0x9B) || (personalityEncoding
== 0x90) ) {
4606 uint32_t offsetInCFI
= cieInfo
->u
.cieInfo
.personality
.offsetInCFI
;
4607 uint32_t nlpAddr
= cieInfo
->u
.cieInfo
.personality
.targetAddress
;
4608 Atom
<x86
>* cieAtom
= this->findAtomByAddress(cieInfo
->address
);
4609 Atom
<x86
>* nlpAtom
= parser
.findAtomByAddress(nlpAddr
);
4610 assert(nlpAtom
->contentType() == ld::Atom::typeNonLazyPointer
);
4611 Parser
<x86
>::SourceLocation
src(cieAtom
, cieInfo
->u
.cieInfo
.personality
.offsetInCFI
);
4613 parser
.addFixup(src
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, nlpAtom
);
4614 parser
.addFixup(src
, ld::Fixup::k2of4
, ld::Fixup::kindSubtractTargetAddress
, cieAtom
);
4615 parser
.addFixup(src
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractAddend
, offsetInCFI
);
4616 parser
.addFixup(src
, ld::Fixup::k4of4
, ld::Fixup::kindStoreLittleEndian32
);
4618 else if ( personalityEncoding
!= 0 ) {
4619 throwf("unsupported address encoding (%02X) of personality function in CIE", personalityEncoding
);
4623 #if SUPPORT_ARCH_arm64
4625 void CFISection
<arm64
>::addCiePersonalityFixups(class Parser
<arm64
>& parser
, const CFI_Atom_Info
* cieInfo
)
4627 uint8_t personalityEncoding
= cieInfo
->u
.cieInfo
.personality
.encodingOfTargetAddress
;
4628 if ( personalityEncoding
== 0x9B ) {
4629 // compiler always produces ARM64_RELOC_GOT r_pcrel=1 to personality function
4630 // CFISection<arm64>::cfiParse() set targetAddress to be symbolIndex + addressInCIE
4631 uint32_t symbolIndex
= cieInfo
->u
.cieInfo
.personality
.targetAddress
4632 - cieInfo
->address
- cieInfo
->u
.cieInfo
.personality
.offsetInCFI
;
4633 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(symbolIndex
);
4634 const char* personalityName
= parser
.nameFromSymbol(sym
);
4636 Atom
<arm64
>* cieAtom
= this->findAtomByAddress(cieInfo
->address
);
4637 Parser
<arm64
>::SourceLocation
src(cieAtom
, cieInfo
->u
.cieInfo
.personality
.offsetInCFI
);
4638 parser
.addFixup(src
, ld::Fixup::k1of2
, ld::Fixup::kindSetTargetAddress
, false, personalityName
);
4639 parser
.addFixup(src
, ld::Fixup::k2of2
, ld::Fixup::kindStoreARM64PCRelToGOT
);
4641 else if ( personalityEncoding
!= 0 ) {
4642 throwf("unsupported address encoding (%02X) of personality function in CIE",
4643 personalityEncoding
);
4649 void CFISection
<arm
>::addCiePersonalityFixups(class Parser
<arm
>& parser
, const CFI_Atom_Info
* cieInfo
)
4651 uint8_t personalityEncoding
= cieInfo
->u
.cieInfo
.personality
.encodingOfTargetAddress
;
4652 if ( (personalityEncoding
== 0x9B) || (personalityEncoding
== 0x90) ) {
4653 uint32_t offsetInCFI
= cieInfo
->u
.cieInfo
.personality
.offsetInCFI
;
4654 uint32_t nlpAddr
= cieInfo
->u
.cieInfo
.personality
.targetAddress
;
4655 Atom
<arm
>* cieAtom
= this->findAtomByAddress(cieInfo
->address
);
4656 Atom
<arm
>* nlpAtom
= parser
.findAtomByAddress(nlpAddr
);
4657 assert(nlpAtom
->contentType() == ld::Atom::typeNonLazyPointer
);
4658 Parser
<arm
>::SourceLocation
src(cieAtom
, cieInfo
->u
.cieInfo
.personality
.offsetInCFI
);
4660 parser
.addFixup(src
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, nlpAtom
);
4661 parser
.addFixup(src
, ld::Fixup::k2of4
, ld::Fixup::kindSubtractTargetAddress
, cieAtom
);
4662 parser
.addFixup(src
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractAddend
, offsetInCFI
);
4663 parser
.addFixup(src
, ld::Fixup::k4of4
, ld::Fixup::kindStoreLittleEndian32
);
4665 else if ( personalityEncoding
!= 0 ) {
4666 throwf("unsupported address encoding (%02X) of personality function in CIE", personalityEncoding
);
4672 template <typename A
>
4673 void CFISection
<A
>::addCiePersonalityFixups(class Parser
<A
>& parser
, const CFI_Atom_Info
* cieInfo
)
4675 assert(0 && "addCiePersonalityFixups() not implemented for arch");
4678 template <typename A
>
4679 void CFISection
<A
>::makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
& cfis
)
4681 ld::Fixup::Kind store32
= bigEndian() ? ld::Fixup::kindStoreBigEndian32
: ld::Fixup::kindStoreLittleEndian32
;
4682 ld::Fixup::Kind store64
= bigEndian() ? ld::Fixup::kindStoreBigEndian64
: ld::Fixup::kindStoreLittleEndian64
;
4684 // add all references for FDEs, including implicit group references
4685 const CFI_Atom_Info
* end
= &cfis
.cfiArray
[cfis
.cfiCount
];
4686 for(const CFI_Atom_Info
* p
= &cfis
.cfiArray
[0]; p
< end
; ++p
) {
4688 // add reference to personality function if used
4689 if ( p
->u
.cieInfo
.personality
.targetAddress
!= CFI_INVALID_ADDRESS
) {
4690 this->addCiePersonalityFixups(parser
, p
);
4695 Atom
<A
>* fdeAtom
= this->findAtomByAddress(p
->address
);
4696 // find function Atom
4697 Atom
<A
>* functionAtom
= parser
.findAtomByAddress(p
->u
.fdeInfo
.function
.targetAddress
);
4699 Atom
<A
>* cieAtom
= this->findAtomByAddress(p
->u
.fdeInfo
.cie
.targetAddress
);
4701 Atom
<A
>* lsdaAtom
= NULL
;
4702 if ( p
->u
.fdeInfo
.lsda
.targetAddress
!= CFI_INVALID_ADDRESS
) {
4703 lsdaAtom
= parser
.findAtomByAddress(p
->u
.fdeInfo
.lsda
.targetAddress
);
4705 // add reference from FDE to CIE (always 32-bit pc-rel)
4706 typename Parser
<A
>::SourceLocation
fdeToCieSrc(fdeAtom
, p
->u
.fdeInfo
.cie
.offsetInCFI
);
4707 parser
.addFixup(fdeToCieSrc
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, fdeAtom
);
4708 parser
.addFixup(fdeToCieSrc
, ld::Fixup::k2of4
, ld::Fixup::kindAddAddend
, p
->u
.fdeInfo
.cie
.offsetInCFI
);
4709 parser
.addFixup(fdeToCieSrc
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractTargetAddress
, cieAtom
);
4710 parser
.addFixup(fdeToCieSrc
, ld::Fixup::k4of4
, store32
, cieAtom
);
4712 // add reference from FDE to function
4713 typename Parser
<A
>::SourceLocation
fdeToFuncSrc(fdeAtom
, p
->u
.fdeInfo
.function
.offsetInCFI
);
4714 switch (p
->u
.fdeInfo
.function
.encodingOfTargetAddress
) {
4715 case DW_EH_PE_pcrel
|DW_EH_PE_ptr
:
4716 if ( sizeof(typename
A::P::uint_t
) == 8 ) {
4717 parser
.addFixup(fdeToFuncSrc
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, functionAtom
);
4718 parser
.addFixup(fdeToFuncSrc
, ld::Fixup::k2of4
, ld::Fixup::kindSubtractTargetAddress
, fdeAtom
);
4719 parser
.addFixup(fdeToFuncSrc
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractAddend
, p
->u
.fdeInfo
.function
.offsetInCFI
);
4720 parser
.addFixup(fdeToFuncSrc
, ld::Fixup::k4of4
, store64
);
4723 // else fall into 32-bit case
4724 case DW_EH_PE_pcrel
|DW_EH_PE_sdata4
:
4725 parser
.addFixup(fdeToFuncSrc
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, functionAtom
);
4726 parser
.addFixup(fdeToFuncSrc
, ld::Fixup::k2of4
, ld::Fixup::kindSubtractTargetAddress
, fdeAtom
);
4727 parser
.addFixup(fdeToFuncSrc
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractAddend
, p
->u
.fdeInfo
.function
.offsetInCFI
);
4728 parser
.addFixup(fdeToFuncSrc
, ld::Fixup::k4of4
, store32
);
4731 throw "unsupported encoding in FDE of pointer to function";
4734 // add reference from FDE to LSDA
4735 typename Parser
<A
>::SourceLocation
fdeToLsdaSrc(fdeAtom
, p
->u
.fdeInfo
.lsda
.offsetInCFI
);
4736 if ( lsdaAtom
!= NULL
) {
4737 switch (p
->u
.fdeInfo
.lsda
.encodingOfTargetAddress
) {
4738 case DW_EH_PE_pcrel
|DW_EH_PE_ptr
:
4739 if ( sizeof(typename
A::P::uint_t
) == 8 ) {
4740 parser
.addFixup(fdeToLsdaSrc
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, lsdaAtom
);
4741 parser
.addFixup(fdeToLsdaSrc
, ld::Fixup::k2of4
, ld::Fixup::kindSubtractTargetAddress
, fdeAtom
);
4742 parser
.addFixup(fdeToLsdaSrc
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractAddend
, p
->u
.fdeInfo
.lsda
.offsetInCFI
);
4743 parser
.addFixup(fdeToLsdaSrc
, ld::Fixup::k4of4
, store64
);
4746 // else fall into 32-bit case
4747 case DW_EH_PE_pcrel
|DW_EH_PE_sdata4
:
4748 parser
.addFixup(fdeToLsdaSrc
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, lsdaAtom
);
4749 parser
.addFixup(fdeToLsdaSrc
, ld::Fixup::k2of4
, ld::Fixup::kindSubtractTargetAddress
, fdeAtom
);
4750 parser
.addFixup(fdeToLsdaSrc
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractAddend
, p
->u
.fdeInfo
.lsda
.offsetInCFI
);
4751 parser
.addFixup(fdeToLsdaSrc
, ld::Fixup::k4of4
, store32
);
4754 throw "unsupported encoding in FDE of pointer to LSDA";
4758 // FDE is in group lead by function atom
4759 typename Parser
<A
>::SourceLocation
fdeSrc(functionAtom
,0);
4760 parser
.addFixup(fdeSrc
, ld::Fixup::k1of1
, ld::Fixup::kindNoneGroupSubordinateFDE
, fdeAtom
);
4762 // LSDA is in group lead by function atom
4763 if ( lsdaAtom
!= NULL
) {
4764 parser
.addFixup(fdeSrc
, ld::Fixup::k1of1
, ld::Fixup::kindNoneGroupSubordinateLSDA
, lsdaAtom
);
4773 template <typename A
>
4774 const void* CFISection
<A
>::OAS::mappedAddress(pint_t addr
)
4776 if ( (_ehFrameStartAddr
<= addr
) && (addr
< _ehFrameEndAddr
) )
4777 return &_ehFrameContent
[addr
-_ehFrameStartAddr
];
4779 // requested bytes are not in __eh_frame section
4780 // this can occur when examining the instruction bytes in the __text
4781 File
<A
>& file
= _ehFrameSection
.file();
4782 for (uint32_t i
=0; i
< file
._sectionsArrayCount
; ++i
) {
4783 const macho_section
<typename
A::P
>* sect
= file
._sectionsArray
[i
]->machoSection();
4784 // TentativeDefinitionSection and AbsoluteSymbolSection have no mach-o section
4785 if ( sect
!= NULL
) {
4786 if ( (sect
->addr() <= addr
) && (addr
< (sect
->addr()+sect
->size())) ) {
4787 return file
.fileContent() + sect
->offset() + addr
- sect
->addr();
4791 throwf("__eh_frame parsing problem. Can't find target of reference to address 0x%08llX", (uint64_t)addr
);
4796 template <typename A
>
4797 uint64_t CFISection
<A
>::OAS::getULEB128(pint_t
& logicalAddr
, pint_t end
)
4799 uintptr_t size
= (end
- logicalAddr
);
4800 libunwind::LocalAddressSpace::pint_t laddr
= (libunwind::LocalAddressSpace::pint_t
)mappedAddress(logicalAddr
);
4801 libunwind::LocalAddressSpace::pint_t sladdr
= laddr
;
4802 uint64_t result
= libunwind::LocalAddressSpace::getULEB128(laddr
, laddr
+size
);
4803 logicalAddr
+= (laddr
-sladdr
);
4807 template <typename A
>
4808 int64_t CFISection
<A
>::OAS::getSLEB128(pint_t
& logicalAddr
, pint_t end
)
4810 uintptr_t size
= (end
- logicalAddr
);
4811 libunwind::LocalAddressSpace::pint_t laddr
= (libunwind::LocalAddressSpace::pint_t
)mappedAddress(logicalAddr
);
4812 libunwind::LocalAddressSpace::pint_t sladdr
= laddr
;
4813 int64_t result
= libunwind::LocalAddressSpace::getSLEB128(laddr
, laddr
+size
);
4814 logicalAddr
+= (laddr
-sladdr
);
4818 template <typename A
>
4819 typename
A::P::uint_t CFISection
<A
>::OAS::getEncodedP(pint_t
& addr
, pint_t end
, uint8_t encoding
)
4821 pint_t startAddr
= addr
;
4826 switch (encoding
& 0x0F) {
4828 result
= getP(addr
);
4829 p
+= sizeof(pint_t
);
4832 case DW_EH_PE_uleb128
:
4833 result
= getULEB128(addr
, end
);
4835 case DW_EH_PE_udata2
:
4836 result
= get16(addr
);
4840 case DW_EH_PE_udata4
:
4841 result
= get32(addr
);
4845 case DW_EH_PE_udata8
:
4846 result
= get64(addr
);
4850 case DW_EH_PE_sleb128
:
4851 result
= getSLEB128(addr
, end
);
4853 case DW_EH_PE_sdata2
:
4854 result
= (int16_t)get16(addr
);
4858 case DW_EH_PE_sdata4
:
4859 result
= (int32_t)get32(addr
);
4863 case DW_EH_PE_sdata8
:
4864 result
= get64(addr
);
4869 throwf("ObjectFileAddressSpace<A>::getEncodedP() encoding 0x%08X not supported", encoding
);
4872 // then add relative offset
4873 switch ( encoding
& 0x70 ) {
4874 case DW_EH_PE_absptr
:
4877 case DW_EH_PE_pcrel
:
4878 result
+= startAddr
;
4880 case DW_EH_PE_textrel
:
4881 throw "DW_EH_PE_textrel pointer encoding not supported";
4883 case DW_EH_PE_datarel
:
4884 throw "DW_EH_PE_datarel pointer encoding not supported";
4886 case DW_EH_PE_funcrel
:
4887 throw "DW_EH_PE_funcrel pointer encoding not supported";
4889 case DW_EH_PE_aligned
:
4890 throw "DW_EH_PE_aligned pointer encoding not supported";
4893 throwf("ObjectFileAddressSpace<A>::getEncodedP() encoding 0x%08X not supported", encoding
);
4897 // Note: DW_EH_PE_indirect is only used in CIEs to refernce the personality pointer
4898 // When parsing .o files that pointer contains zero, so we don't to return that.
4899 // Instead we skip the dereference and return the address of the pointer.
4900 // if ( encoding & DW_EH_PE_indirect )
4901 // result = getP(result);
4907 const char* CUSection
<x86_64
>::personalityName(class Parser
<x86_64
>& parser
, const macho_relocation_info
<x86_64::P
>* reloc
)
4909 if ( reloc
->r_extern() ) {
4910 assert((reloc
->r_type() == X86_64_RELOC_UNSIGNED
) && "wrong reloc type on personality column in __compact_unwind section");
4911 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(reloc
->r_symbolnum());
4912 return parser
.nameFromSymbol(sym
);
4915 const pint_t
* content
= (pint_t
*)(this->file().fileContent() + this->_machOSection
->offset() + reloc
->r_address());
4916 pint_t personalityAddr
= *content
;
4917 assert((parser
.sectionForAddress(personalityAddr
)->type() == ld::Section::typeCode
) && "personality column in __compact_unwind section is not pointer to function");
4918 // atoms may not be constructed yet, so scan symbol table for labels
4919 const char* name
= parser
.scanSymbolTableForAddress(personalityAddr
);
4925 const char* CUSection
<x86
>::personalityName(class Parser
<x86
>& parser
, const macho_relocation_info
<x86::P
>* reloc
)
4927 if ( reloc
->r_extern() ) {
4928 assert((reloc
->r_type() == GENERIC_RELOC_VANILLA
) && "wrong reloc type on personality column in __compact_unwind section");
4929 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(reloc
->r_symbolnum());
4930 return parser
.nameFromSymbol(sym
);
4933 // support __LD, __compact_unwind personality entries which are pointer to personality non-lazy pointer
4934 const pint_t
* content
= (pint_t
*)(this->file().fileContent() + this->_machOSection
->offset() + reloc
->r_address());
4935 pint_t nlPointerAddr
= *content
;
4936 Section
<x86
>* nlSection
= parser
.sectionForAddress(nlPointerAddr
);
4937 if ( nlSection
->type() == ld::Section::typeCode
) {
4938 // personality function is defined in this .o file, so this is a direct reference to it
4939 // atoms may not be constructed yet, so scan symbol table for labels
4940 const char* name
= parser
.scanSymbolTableForAddress(nlPointerAddr
);
4944 uint32_t symIndex
= parser
.symbolIndexFromIndirectSectionAddress(nlPointerAddr
, nlSection
->machoSection());
4945 const macho_nlist
<P
>& nlSymbol
= parser
.symbolFromIndex(symIndex
);
4946 return parser
.nameFromSymbol(nlSymbol
);
4951 #if SUPPORT_ARCH_arm64
4953 const char* CUSection
<arm64
>::personalityName(class Parser
<arm64
>& parser
, const macho_relocation_info
<arm64::P
>* reloc
)
4955 if ( reloc
->r_extern() ) {
4956 assert((reloc
->r_type() == ARM64_RELOC_UNSIGNED
) && "wrong reloc type on personality column in __compact_unwind section");
4957 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(reloc
->r_symbolnum());
4958 return parser
.nameFromSymbol(sym
);
4961 const pint_t
* content
= (pint_t
*)(this->file().fileContent() + this->_machOSection
->offset() + reloc
->r_address());
4962 pint_t personalityAddr
= *content
;
4963 Section
<arm64
>* personalitySection
= parser
.sectionForAddress(personalityAddr
);
4964 (void)personalitySection
;
4965 assert((personalitySection
->type() == ld::Section::typeCode
) && "personality column in __compact_unwind section is not pointer to function");
4966 // atoms may not be constructed yet, so scan symbol table for labels
4967 const char* name
= parser
.scanSymbolTableForAddress(personalityAddr
);
4973 #if SUPPORT_ARCH_arm_any
4975 const char* CUSection
<arm
>::personalityName(class Parser
<arm
>& parser
, const macho_relocation_info
<arm::P
>* reloc
)
4977 if ( reloc
->r_extern() ) {
4978 assert((reloc
->r_type() == ARM_RELOC_VANILLA
) && "wrong reloc type on personality column in __compact_unwind section");
4979 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(reloc
->r_symbolnum());
4980 return parser
.nameFromSymbol(sym
);
4983 // support __LD, __compact_unwind personality entries which are pointer to personality non-lazy pointer
4984 const pint_t
* content
= (pint_t
*)(this->file().fileContent() + this->_machOSection
->offset() + reloc
->r_address());
4985 pint_t nlPointerAddr
= *content
;
4986 Section
<arm
>* nlSection
= parser
.sectionForAddress(nlPointerAddr
);
4987 if ( nlSection
->type() == ld::Section::typeCode
) {
4988 // personality function is defined in this .o file, so this is a direct reference to it
4989 // atoms may not be constructed yet, so scan symbol table for labels
4990 const char* name
= parser
.scanSymbolTableForAddress(nlPointerAddr
);
4994 uint32_t symIndex
= parser
.symbolIndexFromIndirectSectionAddress(nlPointerAddr
, nlSection
->machoSection());
4995 const macho_nlist
<P
>& nlSymbol
= parser
.symbolFromIndex(symIndex
);
4996 return parser
.nameFromSymbol(nlSymbol
);
5003 template <typename A
>
5004 const char* CUSection
<A
>::personalityName(class Parser
<A
>& parser
, const macho_relocation_info
<P
>* reloc
)
5010 bool CUSection
<x86
>::encodingMeansUseDwarf(compact_unwind_encoding_t enc
)
5012 return ((enc
& UNWIND_X86_MODE_MASK
) == UNWIND_X86_MODE_DWARF
);
5016 bool CUSection
<x86_64
>::encodingMeansUseDwarf(compact_unwind_encoding_t enc
)
5018 return ((enc
& UNWIND_X86_64_MODE_MASK
) == UNWIND_X86_64_MODE_DWARF
);
5021 #if SUPPORT_ARCH_arm_any
5023 bool CUSection
<arm
>::encodingMeansUseDwarf(compact_unwind_encoding_t enc
)
5025 return ((enc
& UNWIND_ARM_MODE_MASK
) == UNWIND_ARM_MODE_DWARF
);
5029 #if SUPPORT_ARCH_arm64
5031 bool CUSection
<arm64
>::encodingMeansUseDwarf(compact_unwind_encoding_t enc
)
5033 return ((enc
& UNWIND_ARM64_MODE_MASK
) == UNWIND_ARM64_MODE_DWARF
);
5037 template <typename A
>
5038 int CUSection
<A
>::infoSorter(const void* l
, const void* r
)
5040 // sort references by symbol index, then address
5041 const Info
* left
= (Info
*)l
;
5042 const Info
* right
= (Info
*)r
;
5043 if ( left
->functionSymbolIndex
== right
->functionSymbolIndex
)
5044 return (left
->functionStartAddress
- right
->functionStartAddress
);
5046 return (left
->functionSymbolIndex
- right
->functionSymbolIndex
);
5049 template <typename A
>
5050 void CUSection
<A
>::parse(class Parser
<A
>& parser
, uint32_t cnt
, Info array
[])
5052 // walk section content and copy to Info array
5053 const macho_compact_unwind_entry
<P
>* const entries
= (macho_compact_unwind_entry
<P
>*)(this->file().fileContent() + this->_machOSection
->offset());
5054 for (uint32_t i
=0; i
< cnt
; ++i
) {
5055 Info
* info
= &array
[i
];
5056 const macho_compact_unwind_entry
<P
>* entry
= &entries
[i
];
5057 info
->functionStartAddress
= entry
->codeStart();
5058 info
->functionSymbolIndex
= 0xFFFFFFFF;
5059 info
->rangeLength
= entry
->codeLen();
5060 info
->compactUnwindInfo
= entry
->compactUnwindInfo();
5061 info
->personality
= NULL
;
5062 info
->lsdaAddress
= entry
->lsda();
5063 info
->function
= NULL
;
5065 if ( (info
->compactUnwindInfo
& UNWIND_PERSONALITY_MASK
) != 0 )
5066 warning("no bits should be set in UNWIND_PERSONALITY_MASK of compact unwind encoding in __LD,__compact_unwind section");
5067 if ( info
->lsdaAddress
!= 0 ) {
5068 info
->compactUnwindInfo
|= UNWIND_HAS_LSDA
;
5072 // scan relocs, extern relocs are needed for personality references (possibly for function/lsda refs??)
5073 const macho_relocation_info
<P
>* relocs
= (macho_relocation_info
<P
>*)(this->file().fileContent() + this->_machOSection
->reloff());
5074 const macho_relocation_info
<P
>* relocsEnd
= &relocs
[this->_machOSection
->nreloc()];
5075 for (const macho_relocation_info
<P
>* reloc
= relocs
; reloc
< relocsEnd
; ++reloc
) {
5076 if ( reloc
->r_extern() ) {
5077 // only expect external relocs on some colummns
5078 if ( (reloc
->r_address() % sizeof(macho_compact_unwind_entry
<P
>)) == macho_compact_unwind_entry
<P
>::personalityFieldOffset() ) {
5079 uint32_t entryIndex
= reloc
->r_address() / sizeof(macho_compact_unwind_entry
<P
>);
5080 array
[entryIndex
].personality
= this->personalityName(parser
, reloc
);
5082 else if ( (reloc
->r_address() % sizeof(macho_compact_unwind_entry
<P
>)) == macho_compact_unwind_entry
<P
>::lsdaFieldOffset() ) {
5083 uint32_t entryIndex
= reloc
->r_address() / sizeof(macho_compact_unwind_entry
<P
>);
5084 const macho_nlist
<P
>& lsdaSym
= parser
.symbolFromIndex(reloc
->r_symbolnum());
5085 if ( (lsdaSym
.n_type() & N_TYPE
) == N_SECT
)
5086 array
[entryIndex
].lsdaAddress
= lsdaSym
.n_value();
5088 warning("unexpected extern relocation to lsda in __compact_unwind section");
5090 else if ( (reloc
->r_address() % sizeof(macho_compact_unwind_entry
<P
>)) == macho_compact_unwind_entry
<P
>::codeStartFieldOffset() ) {
5091 uint32_t entryIndex
= reloc
->r_address() / sizeof(macho_compact_unwind_entry
<P
>);
5092 array
[entryIndex
].functionSymbolIndex
= reloc
->r_symbolnum();
5093 array
[entryIndex
].functionStartAddress
+= parser
.symbolFromIndex(reloc
->r_symbolnum()).n_value();
5096 warning("unexpected extern relocation in __compact_unwind section");
5100 if ( (reloc
->r_address() % sizeof(macho_compact_unwind_entry
<P
>)) == macho_compact_unwind_entry
<P
>::personalityFieldOffset() ) {
5101 uint32_t entryIndex
= reloc
->r_address() / sizeof(macho_compact_unwind_entry
<P
>);
5102 array
[entryIndex
].personality
= this->personalityName(parser
, reloc
);
5107 // sort array by function start address so unwind infos will be contiguous for a given function
5108 ::qsort(array
, cnt
, sizeof(Info
), infoSorter
);
5111 template <typename A
>
5112 uint32_t CUSection
<A
>::count()
5114 const macho_section
<P
>* machoSect
= this->machoSection();
5115 if ( (machoSect
->size() % sizeof(macho_compact_unwind_entry
<P
>)) != 0 )
5116 throw "malformed __LD,__compact_unwind section, bad length";
5118 return machoSect
->size() / sizeof(macho_compact_unwind_entry
<P
>);
5121 template <typename A
>
5122 void CUSection
<A
>::makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
& cus
)
5124 Info
* const arrayStart
= cus
.cuArray
;
5125 Info
* const arrayEnd
= &cus
.cuArray
[cus
.cuCount
];
5126 for (Info
* info
=arrayStart
; info
< arrayEnd
; ++info
) {
5127 // find function atom from address
5128 info
->function
= parser
.findAtomByAddress(info
->functionStartAddress
);
5129 // find lsda atom from address
5130 if ( info
->lsdaAddress
!= 0 ) {
5131 info
->lsda
= parser
.findAtomByAddress(info
->lsdaAddress
);
5132 // add lsda subordinate
5133 typename Parser
<A
>::SourceLocation
src(info
->function
, info
->functionStartAddress
- info
->function
->objectAddress());
5134 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindNoneGroupSubordinateLSDA
, info
->lsda
);
5136 if ( info
->personality
!= NULL
) {
5137 // add personality subordinate
5138 typename Parser
<A
>::SourceLocation
src(info
->function
, info
->functionStartAddress
- info
->function
->objectAddress());
5139 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindNoneGroupSubordinatePersonality
, false, info
->personality
);
5145 template <typename A
>
5146 SymboledSection
<A
>::SymboledSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
5147 : Section
<A
>(f
, s
), _type(ld::Atom::typeUnclassified
)
5149 switch ( s
->flags() & SECTION_TYPE
) {
5151 _type
= ld::Atom::typeZeroFill
;
5153 case S_MOD_INIT_FUNC_POINTERS
:
5154 _type
= ld::Atom::typeInitializerPointers
;
5156 case S_MOD_TERM_FUNC_POINTERS
:
5157 _type
= ld::Atom::typeTerminatorPointers
;
5159 case S_THREAD_LOCAL_VARIABLES
:
5160 _type
= ld::Atom::typeTLV
;
5162 case S_THREAD_LOCAL_ZEROFILL
:
5163 _type
= ld::Atom::typeTLVZeroFill
;
5165 case S_THREAD_LOCAL_REGULAR
:
5166 _type
= ld::Atom::typeTLVInitialValue
;
5168 case S_THREAD_LOCAL_INIT_FUNCTION_POINTERS
:
5169 _type
= ld::Atom::typeTLVInitializerPointers
;
5172 if ( strncmp(s
->sectname(), "__gcc_except_tab", 16) == 0 )
5173 _type
= ld::Atom::typeLSDA
;
5174 else if ( this->type() == ld::Section::typeInitializerPointers
)
5175 _type
= ld::Atom::typeInitializerPointers
;
5181 template <typename A
>
5182 bool SymboledSection
<A
>::dontDeadStrip()
5185 case ld::Atom::typeInitializerPointers
:
5186 case ld::Atom::typeTerminatorPointers
:
5189 // model an object file without MH_SUBSECTIONS_VIA_SYMBOLS as one in which nothing can be dead stripped
5190 if ( ! this->_file
.canScatterAtoms() )
5193 return Section
<A
>::dontDeadStrip();
5199 template <typename A
>
5200 uint32_t SymboledSection
<A
>::computeAtomCount(class Parser
<A
>& parser
,
5201 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
5202 const struct Parser
<A
>::CFI_CU_InfoArrays
&)
5204 const pint_t startAddr
= this->_machOSection
->addr();
5205 const pint_t endAddr
= startAddr
+ this->_machOSection
->size();
5206 const uint32_t sectNum
= this->sectionNum(parser
);
5211 const macho_nlist
<P
>* sym
;
5212 while ( it
.next(parser
, *this, sectNum
, startAddr
, endAddr
, &addr
, &size
, &sym
) ) {
5215 //fprintf(stderr, "computeAtomCount(%s,%s) => %d\n", this->segmentName(), this->sectionName(), count);
5219 template <typename A
>
5220 uint32_t SymboledSection
<A
>::appendAtoms(class Parser
<A
>& parser
, uint8_t* p
,
5221 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
5222 const struct Parser
<A
>::CFI_CU_InfoArrays
&)
5224 this->_beginAtoms
= (Atom
<A
>*)p
;
5226 //fprintf(stderr, "SymboledSection::appendAtoms() in section %s\n", this->_machOSection->sectname());
5227 const pint_t startAddr
= this->_machOSection
->addr();
5228 const pint_t endAddr
= startAddr
+ this->_machOSection
->size();
5229 const uint32_t sectNum
= this->sectionNum(parser
);
5234 const macho_nlist
<P
>* label
;
5235 while ( it
.next(parser
, *this, sectNum
, startAddr
, endAddr
, &addr
, &size
, &label
) ) {
5236 Atom
<A
>* allocatedSpace
= (Atom
<A
>*)p
;
5237 // is break because of label or CFI?
5238 if ( label
!= NULL
) {
5239 // The size is computed based on the address of the next label (or the end of the section for the last label)
5240 // If there are two labels at the same address, we want them one to be an alias of the other.
5241 // If the label is at the end of a section, it is has zero size, but is not an alias
5242 const bool isAlias
= ( (size
== 0) && (addr
< endAddr
) );
5243 new (allocatedSpace
) Atom
<A
>(*this, parser
, *label
, size
, isAlias
);
5245 this->_hasAliases
= true;
5246 if ( parser
.altEntryFromSymbol(*label
) )
5247 this->_altEntries
.insert(allocatedSpace
);
5250 ld::Atom::SymbolTableInclusion inclusion
= ld::Atom::symbolTableNotIn
;
5251 ld::Atom::ContentType ctype
= this->contentType();
5252 if ( ctype
== ld::Atom::typeLSDA
)
5253 inclusion
= ld::Atom::symbolTableInWithRandomAutoStripLabel
;
5254 new (allocatedSpace
) Atom
<A
>(*this, "anon", addr
, size
, ld::Atom::definitionRegular
, ld::Atom::combineNever
,
5255 ld::Atom::scopeTranslationUnit
, ctype
, inclusion
,
5256 this->dontDeadStrip(), false, false, this->alignmentForAddress(addr
));
5258 p
+= sizeof(Atom
<A
>);
5262 this->_endAtoms
= (Atom
<A
>*)p
;
5268 ld::Atom::SymbolTableInclusion ImplicitSizeSection
<arm64
>::symbolTableInclusion()
5270 return ld::Atom::symbolTableInWithRandomAutoStripLabel
;
5273 template <typename A
>
5274 ld::Atom::SymbolTableInclusion ImplicitSizeSection
<A
>::symbolTableInclusion()
5276 return ld::Atom::symbolTableNotIn
;
5280 template <typename A
>
5281 uint32_t ImplicitSizeSection
<A
>::computeAtomCount(class Parser
<A
>& parser
,
5282 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
5283 const struct Parser
<A
>::CFI_CU_InfoArrays
&)
5286 const macho_section
<P
>* sect
= this->machoSection();
5287 const pint_t startAddr
= sect
->addr();
5288 const pint_t endAddr
= startAddr
+ sect
->size();
5289 for (pint_t addr
= startAddr
; addr
< endAddr
; addr
+= elementSizeAtAddress(addr
) ) {
5290 if ( useElementAt(parser
, it
, addr
) )
5293 if ( it
.fileHasOverlappingSymbols
&& (sect
->size() != 0) && (this->combine(parser
, startAddr
) == ld::Atom::combineByNameAndContent
) ) {
5294 // if there are multiple labels in this section for the same address, then clone them into multi atoms
5295 pint_t prevSymbolAddr
= (pint_t
)(-1);
5296 uint8_t prevSymbolSectNum
= 0;
5297 bool prevIgnore
= false;
5298 for(uint32_t i
=0; i
< it
.sortedSymbolCount
; ++i
) {
5299 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(it
.sortedSymbolIndexes
[i
]);
5300 const pint_t symbolAddr
= sym
.n_value();
5301 const uint8_t symbolSectNum
= sym
.n_sect();
5302 const bool ignore
= this->ignoreLabel(parser
.nameFromSymbol(sym
));
5303 if ( !ignore
&& !prevIgnore
&& (symbolAddr
== prevSymbolAddr
) && (prevSymbolSectNum
== symbolSectNum
) && (symbolSectNum
== this->sectionNum(parser
)) ) {
5306 prevSymbolAddr
= symbolAddr
;
5307 prevSymbolSectNum
= symbolSectNum
;
5308 prevIgnore
= ignore
;
5314 template <typename A
>
5315 uint32_t ImplicitSizeSection
<A
>::appendAtoms(class Parser
<A
>& parser
, uint8_t* p
,
5316 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
5317 const struct Parser
<A
>::CFI_CU_InfoArrays
&)
5319 this->_beginAtoms
= (Atom
<A
>*)p
;
5321 const macho_section
<P
>* sect
= this->machoSection();
5322 const pint_t startAddr
= sect
->addr();
5323 const pint_t endAddr
= startAddr
+ sect
->size();
5324 const uint32_t sectNum
= this->sectionNum(parser
);
5325 //fprintf(stderr, "ImplicitSizeSection::appendAtoms() in section %s\n", sect->sectname());
5329 const macho_nlist
<P
>* foundLabel
;
5330 Atom
<A
>* allocatedSpace
;
5331 while ( it
.next(parser
, *this, sectNum
, startAddr
, endAddr
, &foundAddr
, &size
, &foundLabel
) ) {
5332 if ( foundLabel
!= NULL
) {
5334 pint_t labeledAtomSize
= this->elementSizeAtAddress(foundAddr
);
5335 allocatedSpace
= (Atom
<A
>*)p
;
5336 if ( this->ignoreLabel(parser
.nameFromSymbol(*foundLabel
)) ) {
5338 // <rdar://problem/10018737>
5339 // a size of zero means there is another label at same location
5340 // and we are supposed to ignore this label
5344 //fprintf(stderr, " 0x%08llX make annon, size=%lld\n", (uint64_t)foundAddr, (uint64_t)size);
5345 new (allocatedSpace
) Atom
<A
>(*this, this->unlabeledAtomName(parser
, foundAddr
), foundAddr
,
5346 this->elementSizeAtAddress(foundAddr
), this->definition(),
5347 this->combine(parser
, foundAddr
), this->scopeAtAddress(parser
, foundAddr
),
5348 this->contentType(), this->symbolTableInclusion(),
5349 this->dontDeadStrip(), false, false, this->alignmentForAddress(foundAddr
));
5353 // make named atom for label
5354 //fprintf(stderr, " 0x%08llX make labeled\n", (uint64_t)foundAddr);
5355 new (allocatedSpace
) Atom
<A
>(*this, parser
, *foundLabel
, labeledAtomSize
);
5359 p
+= sizeof(Atom
<A
>);
5360 foundAddr
+= labeledAtomSize
;
5361 size
-= labeledAtomSize
;
5364 // some number of anonymous atoms
5365 for (pint_t addr
= foundAddr
; addr
< (foundAddr
+size
); addr
+= elementSizeAtAddress(addr
) ) {
5366 // make anon atoms for area before label
5367 if ( this->useElementAt(parser
, it
, addr
) ) {
5368 //fprintf(stderr, " 0x%08llX make annon, size=%lld\n", (uint64_t)addr, (uint64_t)elementSizeAtAddress(addr));
5369 allocatedSpace
= (Atom
<A
>*)p
;
5370 new (allocatedSpace
) Atom
<A
>(*this, this->unlabeledAtomName(parser
, addr
), addr
, this->elementSizeAtAddress(addr
),
5371 this->definition(), this->combine(parser
, addr
), this->scopeAtAddress(parser
, addr
),
5372 this->contentType(), this->symbolTableInclusion(),
5373 this->dontDeadStrip(), false, false, this->alignmentForAddress(addr
));
5375 p
+= sizeof(Atom
<A
>);
5380 this->_endAtoms
= (Atom
<A
>*)p
;
5385 template <typename A
>
5386 bool Literal4Section
<A
>::ignoreLabel(const char* label
) const
5388 return (label
[0] == 'L') || (label
[0] == 'l');
5391 template <typename A
>
5392 unsigned long Literal4Section
<A
>::contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
5394 const uint32_t* literalContent
= (uint32_t*)atom
->contentPointer();
5395 return *literalContent
;
5398 template <typename A
>
5399 bool Literal4Section
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
5400 const ld::IndirectBindingTable
& ind
) const
5402 assert(this->type() == rhs
.section().type());
5403 const uint32_t* literalContent
= (uint32_t*)atom
->contentPointer();
5405 const Atom
<A
>* rhsAtom
= dynamic_cast<const Atom
<A
>*>(&rhs
);
5406 assert(rhsAtom
!= NULL
);
5407 if ( rhsAtom
!= NULL
) {
5408 const uint32_t* rhsLiteralContent
= (uint32_t*)rhsAtom
->contentPointer();
5409 return (*literalContent
== *rhsLiteralContent
);
5415 template <typename A
>
5416 bool Literal8Section
<A
>::ignoreLabel(const char* label
) const
5418 return (label
[0] == 'L') || (label
[0] == 'l');
5421 template <typename A
>
5422 unsigned long Literal8Section
<A
>::contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
5425 const uint64_t* literalContent
= (uint64_t*)atom
->contentPointer();
5426 return *literalContent
;
5428 unsigned long hash
= 5381;
5429 const uint8_t* byteContent
= atom
->contentPointer();
5430 for (int i
=0; i
< 8; ++i
) {
5431 hash
= hash
* 33 + byteContent
[i
];
5437 template <typename A
>
5438 bool Literal8Section
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
5439 const ld::IndirectBindingTable
& ind
) const
5441 if ( rhs
.section().type() != ld::Section::typeLiteral8
)
5443 assert(this->type() == rhs
.section().type());
5444 const uint64_t* literalContent
= (uint64_t*)atom
->contentPointer();
5446 const Atom
<A
>* rhsAtom
= dynamic_cast<const Atom
<A
>*>(&rhs
);
5447 assert(rhsAtom
!= NULL
);
5448 if ( rhsAtom
!= NULL
) {
5449 const uint64_t* rhsLiteralContent
= (uint64_t*)rhsAtom
->contentPointer();
5450 return (*literalContent
== *rhsLiteralContent
);
5455 template <typename A
>
5456 bool Literal16Section
<A
>::ignoreLabel(const char* label
) const
5458 return (label
[0] == 'L') || (label
[0] == 'l');
5461 template <typename A
>
5462 unsigned long Literal16Section
<A
>::contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
5464 unsigned long hash
= 5381;
5465 const uint8_t* byteContent
= atom
->contentPointer();
5466 for (int i
=0; i
< 16; ++i
) {
5467 hash
= hash
* 33 + byteContent
[i
];
5472 template <typename A
>
5473 bool Literal16Section
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
5474 const ld::IndirectBindingTable
& ind
) const
5476 if ( rhs
.section().type() != ld::Section::typeLiteral16
)
5478 assert(this->type() == rhs
.section().type());
5479 const uint64_t* literalContent
= (uint64_t*)atom
->contentPointer();
5481 const Atom
<A
>* rhsAtom
= dynamic_cast<const Atom
<A
>*>(&rhs
);
5482 assert(rhsAtom
!= NULL
);
5483 if ( rhsAtom
!= NULL
) {
5484 const uint64_t* rhsLiteralContent
= (uint64_t*)rhsAtom
->contentPointer();
5485 return ((literalContent
[0] == rhsLiteralContent
[0]) && (literalContent
[1] == rhsLiteralContent
[1]));
5492 template <typename A
>
5493 typename
A::P::uint_t CStringSection
<A
>::elementSizeAtAddress(pint_t addr
)
5495 const macho_section
<P
>* sect
= this->machoSection();
5496 const char* stringContent
= (char*)(this->file().fileContent() + sect
->offset() + addr
- sect
->addr());
5497 return strlen(stringContent
) + 1;
5500 template <typename A
>
5501 bool CStringSection
<A
>::useElementAt(Parser
<A
>& parser
, struct Parser
<A
>::LabelAndCFIBreakIterator
& it
, pint_t addr
)
5506 template <typename A
>
5507 bool CStringSection
<A
>::ignoreLabel(const char* label
) const
5509 return (label
[0] == 'L') || (label
[0] == 'l');
5513 template <typename A
>
5514 Atom
<A
>* CStringSection
<A
>::findAtomByAddress(pint_t addr
)
5516 Atom
<A
>* result
= this->findContentAtomByAddress(addr
, this->_beginAtoms
, this->_endAtoms
);
5520 template <typename A
>
5521 unsigned long CStringSection
<A
>::contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
5523 unsigned long hash
= 5381;
5524 const char* stringContent
= (char*)atom
->contentPointer();
5525 for (const char* s
= stringContent
; *s
!= '\0'; ++s
) {
5526 hash
= hash
* 33 + *s
;
5532 template <typename A
>
5533 bool CStringSection
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
5534 const ld::IndirectBindingTable
& ind
) const
5536 if ( rhs
.section().type() != ld::Section::typeCString
)
5538 assert(this->type() == rhs
.section().type());
5539 assert(strcmp(this->sectionName(), rhs
.section().sectionName())== 0);
5540 assert(strcmp(this->segmentName(), rhs
.section().segmentName())== 0);
5541 const char* stringContent
= (char*)atom
->contentPointer();
5543 const Atom
<A
>* rhsAtom
= dynamic_cast<const Atom
<A
>*>(&rhs
);
5544 assert(rhsAtom
!= NULL
);
5545 if ( rhsAtom
!= NULL
) {
5546 if ( atom
->_size
!= rhsAtom
->_size
)
5548 const char* rhsStringContent
= (char*)rhsAtom
->contentPointer();
5549 return (strcmp(stringContent
, rhsStringContent
) == 0);
5556 ld::Fixup::Kind NonLazyPointerSection
<x86
>::fixupKind()
5558 return ld::Fixup::kindStoreLittleEndian32
;
5562 ld::Fixup::Kind NonLazyPointerSection
<arm
>::fixupKind()
5564 return ld::Fixup::kindStoreLittleEndian32
;
5568 ld::Fixup::Kind NonLazyPointerSection
<arm64
>::fixupKind()
5570 return ld::Fixup::kindStoreLittleEndian64
;
5575 void NonLazyPointerSection
<x86_64
>::makeFixups(class Parser
<x86_64
>& parser
, const struct Parser
<x86_64
>::CFI_CU_InfoArrays
&)
5577 assert(0 && "x86_64 should not have non-lazy-pointer sections in .o files");
5580 template <typename A
>
5581 void NonLazyPointerSection
<A
>::makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
&)
5583 // add references for each NLP atom based on indirect symbol table
5584 const macho_section
<P
>* sect
= this->machoSection();
5585 const pint_t endAddr
= sect
->addr() + sect
->size();
5586 for( pint_t addr
= sect
->addr(); addr
< endAddr
; addr
+= sizeof(pint_t
)) {
5587 typename Parser
<A
>::SourceLocation src
;
5588 typename Parser
<A
>::TargetDesc target
;
5589 src
.atom
= this->findAtomByAddress(addr
);
5590 src
.offsetInAtom
= 0;
5591 uint32_t symIndex
= parser
.symbolIndexFromIndirectSectionAddress(addr
, sect
);
5594 target
.weakImport
= false;
5596 if ( symIndex
== INDIRECT_SYMBOL_LOCAL
) {
5597 // use direct reference for local symbols
5598 const pint_t
* nlpContent
= (pint_t
*)(this->file().fileContent() + sect
->offset() + addr
- sect
->addr());
5599 pint_t targetAddr
= P::getP(*nlpContent
);
5600 target
.atom
= parser
.findAtomByAddress(targetAddr
);
5601 target
.weakImport
= false;
5602 target
.addend
= (targetAddr
- target
.atom
->objectAddress());
5603 // <rdar://problem/8385011> if pointer to thumb function, mask of thumb bit (not an addend of +1)
5604 if ( target
.atom
->isThumb() )
5605 target
.addend
&= (-2);
5606 assert(src
.atom
->combine() == ld::Atom::combineNever
);
5609 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(symIndex
);
5610 // use direct reference for local symbols
5611 if ( ((sym
.n_type() & N_TYPE
) == N_SECT
) && ((sym
.n_type() & N_EXT
) == 0) ) {
5612 parser
.findTargetFromAddressAndSectionNum(sym
.n_value(), sym
.n_sect(), target
);
5613 assert(src
.atom
->combine() == ld::Atom::combineNever
);
5616 target
.name
= parser
.nameFromSymbol(sym
);
5617 target
.weakImport
= parser
.weakImportFromSymbol(sym
);
5618 assert(src
.atom
->combine() == ld::Atom::combineByNameAndReferences
);
5621 parser
.addFixups(src
, this->fixupKind(), target
);
5625 template <typename A
>
5626 ld::Atom::Combine NonLazyPointerSection
<A
>::combine(Parser
<A
>& parser
, pint_t addr
)
5628 const macho_section
<P
>* sect
= this->machoSection();
5629 uint32_t symIndex
= parser
.symbolIndexFromIndirectSectionAddress(addr
, sect
);
5630 if ( symIndex
== INDIRECT_SYMBOL_LOCAL
)
5631 return ld::Atom::combineNever
;
5633 // don't coalesce non-lazy-pointers to local symbols
5634 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(symIndex
);
5635 if ( ((sym
.n_type() & N_TYPE
) == N_SECT
) && ((sym
.n_type() & N_EXT
) == 0) )
5636 return ld::Atom::combineNever
;
5638 return ld::Atom::combineByNameAndReferences
;
5641 template <typename A
>
5642 const char* NonLazyPointerSection
<A
>::targetName(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
)
5644 assert(atom
->combine() == ld::Atom::combineByNameAndReferences
);
5645 assert(atom
->fixupCount() == 1);
5646 ld::Fixup::iterator fit
= atom
->fixupsBegin();
5647 const char* name
= NULL
;
5648 switch ( fit
->binding
) {
5649 case ld::Fixup::bindingByNameUnbound
:
5652 case ld::Fixup::bindingByContentBound
:
5653 name
= fit
->u
.target
->name();
5655 case ld::Fixup::bindingsIndirectlyBound
:
5656 name
= ind
.indirectName(fit
->u
.bindingIndex
);
5661 assert(name
!= NULL
);
5665 template <typename A
>
5666 unsigned long NonLazyPointerSection
<A
>::contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
5668 assert(atom
->combine() == ld::Atom::combineByNameAndReferences
);
5669 unsigned long hash
= 9508;
5670 for (const char* s
= this->targetName(atom
, ind
); *s
!= '\0'; ++s
) {
5671 hash
= hash
* 33 + *s
;
5676 template <typename A
>
5677 bool NonLazyPointerSection
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
5678 const ld::IndirectBindingTable
& indirectBindingTable
) const
5680 if ( rhs
.section().type() != ld::Section::typeNonLazyPointer
)
5682 assert(this->type() == rhs
.section().type());
5683 // there can be many non-lazy pointer in different section names
5684 // we only want to coalesce in same section name
5685 if ( *this != rhs
.section() )
5687 const Atom
<A
>* rhsAtom
= dynamic_cast<const Atom
<A
>*>(&rhs
);
5688 assert(rhsAtom
!= NULL
);
5689 const char* thisName
= this->targetName(atom
, indirectBindingTable
);
5690 const char* rhsName
= this->targetName(rhsAtom
, indirectBindingTable
);
5691 return (strcmp(thisName
, rhsName
) == 0);
5694 template <typename A
>
5695 ld::Atom::Scope NonLazyPointerSection
<A
>::scopeAtAddress(Parser
<A
>& parser
, pint_t addr
)
5697 const macho_section
<P
>* sect
= this->machoSection();
5698 uint32_t symIndex
= parser
.symbolIndexFromIndirectSectionAddress(addr
, sect
);
5699 if ( symIndex
== INDIRECT_SYMBOL_LOCAL
)
5700 return ld::Atom::scopeTranslationUnit
;
5702 return ld::Atom::scopeLinkageUnit
;
5707 template <typename A
>
5708 ld::Atom::Combine TLVPointerSection
<A
>::combine(Parser
<A
>& parser
, pint_t addr
)
5710 return ld::Atom::combineByNameAndReferences
;
5714 template <typename A
>
5715 const char* TLVPointerSection
<A
>::targetName(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
, bool* isStatic
)
5717 assert(atom
->combine() == ld::Atom::combineByNameAndReferences
);
5718 assert(atom
->fixupCount() == 1);
5720 ld::Fixup::iterator fit
= atom
->fixupsBegin();
5721 const char* name
= NULL
;
5722 switch ( fit
->binding
) {
5723 case ld::Fixup::bindingByNameUnbound
:
5726 case ld::Fixup::bindingByContentBound
:
5727 name
= fit
->u
.target
->name();
5729 case ld::Fixup::bindingsIndirectlyBound
:
5730 name
= ind
.indirectName(fit
->u
.bindingIndex
);
5732 case ld::Fixup::bindingDirectlyBound
:
5733 name
= fit
->u
.target
->name();
5734 *isStatic
= (fit
->u
.target
->scope() == ld::Atom::scopeTranslationUnit
);
5739 assert(name
!= NULL
);
5743 template <typename A
>
5744 unsigned long TLVPointerSection
<A
>::contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
5746 assert(atom
->combine() == ld::Atom::combineByNameAndReferences
);
5747 unsigned long hash
= 9508;
5749 for (const char* s
= this->targetName(atom
, ind
, &isStatic
); *s
!= '\0'; ++s
) {
5750 hash
= hash
* 33 + *s
;
5755 template <typename A
>
5756 bool TLVPointerSection
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
5757 const ld::IndirectBindingTable
& indirectBindingTable
) const
5759 if ( rhs
.section().type() != ld::Section::typeTLVPointers
)
5761 assert(this->type() == rhs
.section().type());
5762 const Atom
<A
>* rhsAtom
= dynamic_cast<const Atom
<A
>*>(&rhs
);
5763 assert(rhsAtom
!= NULL
);
5766 const char* thisName
= this->targetName(atom
, indirectBindingTable
, &thisIsStatic
);
5767 const char* rhsName
= this->targetName(rhsAtom
, indirectBindingTable
, &rhsIsStatic
);
5768 return !thisIsStatic
&& !rhsIsStatic
&& (strcmp(thisName
, rhsName
) == 0);
5772 template <typename A
>
5773 const uint8_t* CFStringSection
<A
>::targetContent(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
,
5774 ContentType
* ct
, unsigned int* count
)
5776 *ct
= contentUnknown
;
5777 for (ld::Fixup::iterator fit
=atom
->fixupsBegin(), end
=atom
->fixupsEnd(); fit
!= end
; ++fit
) {
5778 const ld::Atom
* targetAtom
= NULL
;
5779 switch ( fit
->binding
) {
5780 case ld::Fixup::bindingByNameUnbound
:
5781 // ignore reference to ___CFConstantStringClassReference
5782 // we are just looking for reference to backing string data
5783 assert(fit
->offsetInAtom
== 0);
5784 assert(strcmp(fit
->u
.name
, "___CFConstantStringClassReference") == 0);
5786 case ld::Fixup::bindingDirectlyBound
:
5787 case ld::Fixup::bindingByContentBound
:
5788 targetAtom
= fit
->u
.target
;
5790 case ld::Fixup::bindingsIndirectlyBound
:
5791 targetAtom
= ind
.indirectAtom(fit
->u
.bindingIndex
);
5794 assert(0 && "bad binding type");
5796 assert(targetAtom
!= NULL
);
5797 const Atom
<A
>* target
= dynamic_cast<const Atom
<A
>*>(targetAtom
);
5798 if ( targetAtom
->section().type() == ld::Section::typeCString
) {
5800 *count
= targetAtom
->size();
5802 else if ( targetAtom
->section().type() == ld::Section::typeUTF16Strings
) {
5804 *count
= (targetAtom
->size()+1)/2; // round up incase of buggy compiler that has only one trailing zero byte
5807 *ct
= contentUnknown
;
5811 return target
->contentPointer();
5817 template <typename A
>
5818 unsigned long CFStringSection
<A
>::contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
5820 // base hash of CFString on hash of cstring it wraps
5823 unsigned int charCount
;
5824 const uint8_t* content
= this->targetContent(atom
, ind
, &cType
, &charCount
);
5828 for (const char* s
= (char*)content
; *s
!= '\0'; ++s
) {
5829 hash
= hash
* 33 + *s
;
5834 --charCount
; // don't add last 0x0000 to hash because some buggy compilers only have trailing single byte
5835 for (const uint16_t* s
= (uint16_t*)content
; charCount
> 0; ++s
, --charCount
) {
5836 hash
= hash
* 1025 + *s
;
5839 case contentUnknown
:
5840 // <rdar://problem/14134211> For malformed CFStrings, hash to address of atom so they have unique hashes
5841 return ULONG_MAX
- (unsigned long)(atom
);
5847 template <typename A
>
5848 bool CFStringSection
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
5849 const ld::IndirectBindingTable
& indirectBindingTable
) const
5853 if ( rhs
.section().type() != ld::Section::typeCFString
)
5855 assert(this->type() == rhs
.section().type());
5856 assert(strcmp(this->sectionName(), "__cfstring") == 0);
5858 ContentType thisType
;
5859 unsigned int charCount
;
5860 const uint8_t* cstringContent
= this->targetContent(atom
, indirectBindingTable
, &thisType
, &charCount
);
5861 ContentType rhsType
;
5862 const Atom
<A
>* rhsAtom
= dynamic_cast<const Atom
<A
>*>(&rhs
);
5863 assert(rhsAtom
!= NULL
);
5864 unsigned int rhsCharCount
;
5865 const uint8_t* rhsStringContent
= this->targetContent(rhsAtom
, indirectBindingTable
, &rhsType
, &rhsCharCount
);
5867 if ( thisType
!= rhsType
)
5870 if ( thisType
== contentUnknown
)
5873 if ( rhsType
== contentUnknown
)
5876 // no need to compare content of pointers are already the same
5877 if ( cstringContent
== rhsStringContent
)
5880 // no need to compare content if size is different
5881 if ( charCount
!= rhsCharCount
)
5884 switch ( thisType
) {
5886 return (strcmp((char*)cstringContent
, (char*)rhsStringContent
) == 0);
5889 const uint16_t* cstringContent16
= (uint16_t*)cstringContent
;
5890 const uint16_t* rhsStringContent16
= (uint16_t*)rhsStringContent
;
5891 for (unsigned int i
= 0; i
< charCount
; ++i
) {
5892 if ( cstringContent16
[i
] != rhsStringContent16
[i
] )
5897 case contentUnknown
:
5904 template <typename A
>
5905 typename
A::P::uint_t ObjC1ClassSection
<A
>::elementSizeAtAddress(pint_t addr
)
5907 // nominal size for each class is 48 bytes, but sometimes the compiler
5908 // over aligns and there is padding after class data
5909 const macho_section
<P
>* sct
= this->machoSection();
5910 uint32_t align
= 1 << sct
->align();
5911 uint32_t size
= ((12 * sizeof(pint_t
)) + align
-1) & (-align
);
5915 template <typename A
>
5916 const char* ObjC1ClassSection
<A
>::unlabeledAtomName(Parser
<A
>& parser
, pint_t addr
)
5918 // 8-bytes into class object is pointer to class name
5919 const macho_section
<P
>* sct
= this->machoSection();
5920 uint32_t classObjcFileOffset
= sct
->offset() - sct
->addr() + addr
;
5921 const uint8_t* mappedFileContent
= this->file().fileContent();
5922 pint_t nameAddr
= P::getP(*((pint_t
*)(mappedFileContent
+classObjcFileOffset
+2*sizeof(pint_t
))));
5924 // find section containing string address to get string bytes
5925 const macho_section
<P
>* const sections
= parser
.firstMachOSection();
5926 const uint32_t sectionCount
= parser
.machOSectionCount();
5927 for (uint32_t i
=0; i
< sectionCount
; ++i
) {
5928 const macho_section
<P
>* aSect
= §ions
[i
];
5929 if ( (aSect
->addr() <= nameAddr
) && (nameAddr
< (aSect
->addr()+aSect
->size())) ) {
5930 assert((aSect
->flags() & SECTION_TYPE
) == S_CSTRING_LITERALS
);
5931 uint32_t nameFileOffset
= aSect
->offset() - aSect
->addr() + nameAddr
;
5932 const char* name
= (char*)mappedFileContent
+ nameFileOffset
;
5933 // spin through symbol table to find absolute symbol corresponding to this class
5934 for (uint32_t s
=0; s
< parser
.symbolCount(); ++s
) {
5935 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(s
);
5936 if ( (sym
.n_type() & N_TYPE
) != N_ABS
)
5938 const char* absName
= parser
.nameFromSymbol(sym
);
5939 if ( strncmp(absName
, ".objc_class_name_", 17) == 0 ) {
5940 if ( strcmp(&absName
[17], name
) == 0 )
5944 assert(0 && "obj class name not found in symbol table");
5947 assert(0 && "obj class name not found");
5948 return "unknown objc class";
5952 template <typename A
>
5953 const char* ObjC2ClassRefsSection
<A
>::targetClassName(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
5955 assert(atom
->fixupCount() == 1);
5956 ld::Fixup::iterator fit
= atom
->fixupsBegin();
5957 const char* className
= NULL
;
5958 switch ( fit
->binding
) {
5959 case ld::Fixup::bindingByNameUnbound
:
5960 className
= fit
->u
.name
;
5962 case ld::Fixup::bindingDirectlyBound
:
5963 case ld::Fixup::bindingByContentBound
:
5964 className
= fit
->u
.target
->name();
5966 case ld::Fixup::bindingsIndirectlyBound
:
5967 className
= ind
.indirectName(fit
->u
.bindingIndex
);
5970 assert(0 && "unsupported binding in objc2 class ref section");
5972 assert(className
!= NULL
);
5977 template <typename A
>
5978 unsigned long ObjC2ClassRefsSection
<A
>::contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
5980 unsigned long hash
= 978;
5981 for (const char* s
= targetClassName(atom
, ind
); *s
!= '\0'; ++s
) {
5982 hash
= hash
* 33 + *s
;
5987 template <typename A
>
5988 bool ObjC2ClassRefsSection
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
5989 const ld::IndirectBindingTable
& indirectBindingTable
) const
5991 assert(this->type() == rhs
.section().type());
5992 const Atom
<A
>* rhsAtom
= dynamic_cast<const Atom
<A
>*>(&rhs
);
5993 assert(rhsAtom
!= NULL
);
5994 const char* thisClassName
= targetClassName(atom
, indirectBindingTable
);
5995 const char* rhsClassName
= targetClassName(rhsAtom
, indirectBindingTable
);
5996 return (strcmp(thisClassName
, rhsClassName
) == 0);
6000 template <typename A
>
6001 const char* Objc1ClassReferences
<A
>::targetCString(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
6003 assert(atom
->fixupCount() == 2);
6004 ld::Fixup::iterator fit
= atom
->fixupsBegin();
6005 if ( fit
->kind
== ld::Fixup::kindSetTargetAddress
)
6007 const ld::Atom
* targetAtom
= NULL
;
6008 switch ( fit
->binding
) {
6009 case ld::Fixup::bindingByContentBound
:
6010 targetAtom
= fit
->u
.target
;
6012 case ld::Fixup::bindingsIndirectlyBound
:
6013 targetAtom
= ind
.indirectAtom(fit
->u
.bindingIndex
);
6014 if ( targetAtom
== NULL
) {
6015 fprintf(stderr
, "missing target named %s\n", ind
.indirectName(fit
->u
.bindingIndex
));
6021 assert(targetAtom
!= NULL
);
6022 const Atom
<A
>* target
= dynamic_cast<const Atom
<A
>*>(targetAtom
);
6023 assert(target
!= NULL
);
6024 return (char*)target
->contentPointer();
6028 template <typename A
>
6029 const char* PointerToCStringSection
<A
>::targetCString(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
6031 assert(atom
->fixupCount() == 1);
6032 ld::Fixup::iterator fit
= atom
->fixupsBegin();
6033 const ld::Atom
* targetAtom
= NULL
;
6034 switch ( fit
->binding
) {
6035 case ld::Fixup::bindingByContentBound
:
6036 targetAtom
= fit
->u
.target
;
6038 case ld::Fixup::bindingsIndirectlyBound
:
6039 targetAtom
= ind
.indirectAtom(fit
->u
.bindingIndex
);
6041 case ld::Fixup::bindingDirectlyBound
:
6042 targetAtom
= fit
->u
.target
;
6045 assert(0 && "unsupported reference to selector");
6047 assert(targetAtom
!= NULL
);
6048 const Atom
<A
>* target
= dynamic_cast<const Atom
<A
>*>(targetAtom
);
6049 assert(target
!= NULL
);
6050 assert(target
->contentType() == ld::Atom::typeCString
);
6051 return (char*)target
->contentPointer();
6054 template <typename A
>
6055 unsigned long PointerToCStringSection
<A
>::contentHash(const class Atom
<A
>* atom
,
6056 const ld::IndirectBindingTable
& indirectBindingTable
) const
6058 // make hash from section name and target cstring name
6059 unsigned long hash
= 123;
6060 for (const char* s
= this->sectionName(); *s
!= '\0'; ++s
) {
6061 hash
= hash
* 33 + *s
;
6063 for (const char* s
= this->targetCString(atom
, indirectBindingTable
); *s
!= '\0'; ++s
) {
6064 hash
= hash
* 33 + *s
;
6069 template <typename A
>
6070 bool PointerToCStringSection
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
6071 const ld::IndirectBindingTable
& indirectBindingTable
) const
6073 assert(this->type() == rhs
.section().type());
6074 // there can be pointers-to-cstrings in different section names
6075 // we only want to coalesce in same section name
6076 if ( *this != rhs
.section() )
6079 // get string content for this
6080 const char* cstringContent
= this->targetCString(atom
, indirectBindingTable
);
6081 const Atom
<A
>* rhsAtom
= dynamic_cast<const Atom
<A
>*>(&rhs
);
6082 assert(rhsAtom
!= NULL
);
6083 const char* rhsCstringContent
= this->targetCString(rhsAtom
, indirectBindingTable
);
6085 assert(cstringContent
!= NULL
);
6086 assert(rhsCstringContent
!= NULL
);
6087 return (strcmp(cstringContent
, rhsCstringContent
) == 0);
6092 template <typename A
>
6093 unsigned long UTF16StringSection
<A
>::contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
6095 unsigned long hash
= 5381;
6096 const uint16_t* stringContent
= (uint16_t*)atom
->contentPointer();
6097 // some buggy compilers end utf16 data with single byte, so don't use last word in hash computation
6098 unsigned int count
= (atom
->size()/2) - 1;
6099 for (const uint16_t* s
= stringContent
; count
> 0; ++s
, --count
) {
6100 hash
= hash
* 33 + *s
;
6105 template <typename A
>
6106 bool UTF16StringSection
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
6107 const ld::IndirectBindingTable
& ind
) const
6109 if ( rhs
.section().type() != ld::Section::typeUTF16Strings
)
6122 uint32_t Section
<x86_64
>::x86_64PcRelOffset(uint8_t r_type
)
6125 case X86_64_RELOC_SIGNED
:
6127 case X86_64_RELOC_SIGNED_1
:
6129 case X86_64_RELOC_SIGNED_2
:
6131 case X86_64_RELOC_SIGNED_4
:
6139 bool Section
<x86_64
>::addRelocFixup(class Parser
<x86_64
>& parser
, const macho_relocation_info
<P
>* reloc
)
6141 const macho_section
<P
>* sect
= this->machoSection();
6142 uint64_t srcAddr
= sect
->addr() + reloc
->r_address();
6143 Parser
<x86_64
>::SourceLocation src
;
6144 Parser
<x86_64
>::TargetDesc target
;
6145 Parser
<x86_64
>::TargetDesc toTarget
;
6146 src
.atom
= this->findAtomByAddress(srcAddr
);
6147 src
.offsetInAtom
= srcAddr
- src
.atom
->_objAddress
;
6148 const uint8_t* fixUpPtr
= file().fileContent() + sect
->offset() + reloc
->r_address();
6149 uint64_t contentValue
= 0;
6150 const macho_relocation_info
<x86_64::P
>* nextReloc
= &reloc
[1];
6151 bool result
= false;
6152 bool useDirectBinding
;
6153 switch ( reloc
->r_length() ) {
6155 contentValue
= *fixUpPtr
;
6158 contentValue
= (int64_t)(int16_t)E::get16(*((uint16_t*)fixUpPtr
));
6161 contentValue
= (int64_t)(int32_t)E::get32(*((uint32_t*)fixUpPtr
));
6164 contentValue
= E::get64(*((uint64_t*)fixUpPtr
));
6169 target
.weakImport
= false;
6171 if ( reloc
->r_extern() ) {
6172 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(reloc
->r_symbolnum());
6173 // use direct reference for local symbols
6174 if ( ((sym
.n_type() & N_TYPE
) == N_SECT
) && (((sym
.n_type() & N_EXT
) == 0) || (parser
.nameFromSymbol(sym
)[0] == 'L')) ) {
6175 parser
.findTargetFromAddressAndSectionNum(sym
.n_value(), sym
.n_sect(), target
);
6176 target
.addend
+= contentValue
;
6179 target
.name
= parser
.nameFromSymbol(sym
);
6180 target
.weakImport
= parser
.weakImportFromSymbol(sym
);
6181 target
.addend
= contentValue
;
6183 // cfstrings should always use direct reference to backing store
6184 if ( (this->type() == ld::Section::typeCFString
) && (src
.offsetInAtom
!= 0) ) {
6185 parser
.findTargetFromAddressAndSectionNum(sym
.n_value(), sym
.n_sect(), target
);
6186 target
.addend
= contentValue
;
6190 if ( reloc
->r_pcrel() )
6191 contentValue
+= srcAddr
+ x86_64PcRelOffset(reloc
->r_type());
6192 parser
.findTargetFromAddressAndSectionNum(contentValue
, reloc
->r_symbolnum(), target
);
6194 switch ( reloc
->r_type() ) {
6195 case X86_64_RELOC_UNSIGNED
:
6196 if ( reloc
->r_pcrel() )
6197 throw "pcrel and X86_64_RELOC_UNSIGNED not supported";
6198 switch ( reloc
->r_length() ) {
6201 throw "length < 2 and X86_64_RELOC_UNSIGNED not supported";
6203 parser
.addFixups(src
, ld::Fixup::kindStoreLittleEndian32
, target
);
6206 parser
.addFixups(src
, ld::Fixup::kindStoreLittleEndian64
, target
);
6210 case X86_64_RELOC_SIGNED
:
6211 case X86_64_RELOC_SIGNED_1
:
6212 case X86_64_RELOC_SIGNED_2
:
6213 case X86_64_RELOC_SIGNED_4
:
6214 if ( ! reloc
->r_pcrel() )
6215 throw "not pcrel and X86_64_RELOC_SIGNED* not supported";
6216 if ( reloc
->r_length() != 2 )
6217 throw "length != 2 and X86_64_RELOC_SIGNED* not supported";
6218 switch ( reloc
->r_type() ) {
6219 case X86_64_RELOC_SIGNED
:
6220 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel32
, target
);
6222 case X86_64_RELOC_SIGNED_1
:
6223 if ( reloc
->r_extern() )
6225 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel32_1
, target
);
6227 case X86_64_RELOC_SIGNED_2
:
6228 if ( reloc
->r_extern() )
6230 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel32_2
, target
);
6232 case X86_64_RELOC_SIGNED_4
:
6233 if ( reloc
->r_extern() )
6235 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel32_4
, target
);
6239 case X86_64_RELOC_BRANCH
:
6240 if ( ! reloc
->r_pcrel() )
6241 throw "not pcrel and X86_64_RELOC_BRANCH not supported";
6242 switch ( reloc
->r_length() ) {
6244 if ( (target
.name
!= NULL
) && (strncmp(target
.name
, "___dtrace_probe$", 16) == 0) ) {
6245 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindStoreX86DtraceCallSiteNop
, false, target
.name
);
6246 parser
.addDtraceExtraInfos(src
, &target
.name
[16]);
6248 else if ( (target
.name
!= NULL
) && (strncmp(target
.name
, "___dtrace_isenabled$", 20) == 0) ) {
6249 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindStoreX86DtraceIsEnableSiteClear
, false, target
.name
);
6250 parser
.addDtraceExtraInfos(src
, &target
.name
[20]);
6253 parser
.addFixups(src
, ld::Fixup::kindStoreX86BranchPCRel32
, target
);
6257 parser
.addFixups(src
, ld::Fixup::kindStoreX86BranchPCRel8
, target
);
6260 throwf("length=%d and X86_64_RELOC_BRANCH not supported", reloc
->r_length());
6263 case X86_64_RELOC_GOT
:
6264 if ( ! reloc
->r_extern() )
6265 throw "not extern and X86_64_RELOC_GOT not supported";
6266 if ( ! reloc
->r_pcrel() )
6267 throw "not pcrel and X86_64_RELOC_GOT not supported";
6268 if ( reloc
->r_length() != 2 )
6269 throw "length != 2 and X86_64_RELOC_GOT not supported";
6270 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel32GOT
, target
);
6272 case X86_64_RELOC_GOT_LOAD
:
6273 if ( ! reloc
->r_extern() )
6274 throw "not extern and X86_64_RELOC_GOT_LOAD not supported";
6275 if ( ! reloc
->r_pcrel() )
6276 throw "not pcrel and X86_64_RELOC_GOT_LOAD not supported";
6277 if ( reloc
->r_length() != 2 )
6278 throw "length != 2 and X86_64_RELOC_GOT_LOAD not supported";
6279 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel32GOTLoad
, target
);
6281 case X86_64_RELOC_SUBTRACTOR
:
6282 if ( reloc
->r_pcrel() )
6283 throw "X86_64_RELOC_SUBTRACTOR cannot be pc-relative";
6284 if ( reloc
->r_length() < 2 )
6285 throw "X86_64_RELOC_SUBTRACTOR must have r_length of 2 or 3";
6286 if ( !reloc
->r_extern() )
6287 throw "X86_64_RELOC_SUBTRACTOR must have r_extern=1";
6288 if ( nextReloc
->r_type() != X86_64_RELOC_UNSIGNED
)
6289 throw "X86_64_RELOC_SUBTRACTOR must be followed by X86_64_RELOC_UNSIGNED";
6291 if ( nextReloc
->r_pcrel() )
6292 throw "X86_64_RELOC_UNSIGNED following a X86_64_RELOC_SUBTRACTOR cannot be pc-relative";
6293 if ( nextReloc
->r_length() != reloc
->r_length() )
6294 throw "X86_64_RELOC_UNSIGNED following a X86_64_RELOC_SUBTRACTOR must have same r_length";
6295 if ( nextReloc
->r_extern() ) {
6296 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(nextReloc
->r_symbolnum());
6297 // use direct reference for local symbols
6298 if ( ((sym
.n_type() & N_TYPE
) == N_SECT
) && (((sym
.n_type() & N_EXT
) == 0) || (parser
.nameFromSymbol(sym
)[0] == 'L')) ) {
6299 parser
.findTargetFromAddressAndSectionNum(sym
.n_value(), sym
.n_sect(), toTarget
);
6300 toTarget
.addend
= contentValue
;
6301 useDirectBinding
= true;
6304 toTarget
.name
= parser
.nameFromSymbol(sym
);
6305 toTarget
.weakImport
= parser
.weakImportFromSymbol(sym
);
6306 toTarget
.addend
= contentValue
;
6307 useDirectBinding
= false;
6311 parser
.findTargetFromAddressAndSectionNum(contentValue
, nextReloc
->r_symbolnum(), toTarget
);
6312 useDirectBinding
= (toTarget
.atom
->scope() == ld::Atom::scopeTranslationUnit
);
6314 if ( useDirectBinding
)
6315 parser
.addFixup(src
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, toTarget
.atom
);
6317 parser
.addFixup(src
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, toTarget
.weakImport
, toTarget
.name
);
6318 parser
.addFixup(src
, ld::Fixup::k2of4
, ld::Fixup::kindAddAddend
, toTarget
.addend
);
6319 if ( target
.atom
== NULL
)
6320 parser
.addFixup(src
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractTargetAddress
, false, target
.name
);
6322 parser
.addFixup(src
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractTargetAddress
, target
.atom
);
6323 if ( reloc
->r_length() == 2 )
6324 parser
.addFixup(src
, ld::Fixup::k4of4
, ld::Fixup::kindStoreLittleEndian32
);
6326 parser
.addFixup(src
, ld::Fixup::k4of4
, ld::Fixup::kindStoreLittleEndian64
);
6328 case X86_64_RELOC_TLV
:
6329 if ( ! reloc
->r_extern() )
6330 throw "not extern and X86_64_RELOC_TLV not supported";
6331 if ( ! reloc
->r_pcrel() )
6332 throw "not pcrel and X86_64_RELOC_TLV not supported";
6333 if ( reloc
->r_length() != 2 )
6334 throw "length != 2 and X86_64_RELOC_TLV not supported";
6335 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel32TLVLoad
, target
);
6338 throwf("unknown relocation type %d", reloc
->r_type());
6346 bool Section
<x86
>::addRelocFixup(class Parser
<x86
>& parser
, const macho_relocation_info
<P
>* reloc
)
6348 const macho_section
<P
>* sect
= this->machoSection();
6350 const uint8_t* fixUpPtr
;
6351 uint32_t contentValue
= 0;
6352 ld::Fixup::Kind kind
= ld::Fixup::kindNone
;
6353 Parser
<x86
>::SourceLocation src
;
6354 Parser
<x86
>::TargetDesc target
;
6356 if ( (reloc
->r_address() & R_SCATTERED
) == 0 ) {
6357 srcAddr
= sect
->addr() + reloc
->r_address();
6358 src
.atom
= this->findAtomByAddress(srcAddr
);
6359 src
.offsetInAtom
= srcAddr
- src
.atom
->_objAddress
;
6360 fixUpPtr
= file().fileContent() + sect
->offset() + reloc
->r_address();
6361 switch ( reloc
->r_type() ) {
6362 case GENERIC_RELOC_VANILLA
:
6363 switch ( reloc
->r_length() ) {
6365 contentValue
= (int32_t)(int8_t)*fixUpPtr
;
6366 if ( reloc
->r_pcrel() ) {
6367 kind
= ld::Fixup::kindStoreX86BranchPCRel8
;
6368 contentValue
+= srcAddr
+ sizeof(uint8_t);
6371 throw "r_length=0 and r_pcrel=0 not supported";
6374 contentValue
= (int32_t)(int16_t)E::get16(*((uint16_t*)fixUpPtr
));
6375 if ( reloc
->r_pcrel() ) {
6376 kind
= ld::Fixup::kindStoreX86PCRel16
;
6377 contentValue
+= srcAddr
+ sizeof(uint16_t);
6380 kind
= ld::Fixup::kindStoreLittleEndian16
;
6383 contentValue
= E::get32(*((uint32_t*)fixUpPtr
));
6384 if ( reloc
->r_pcrel() ) {
6385 kind
= ld::Fixup::kindStoreX86BranchPCRel32
;
6386 contentValue
+= srcAddr
+ sizeof(uint32_t);
6389 kind
= ld::Fixup::kindStoreLittleEndian32
;
6392 throw "r_length=3 not supported";
6394 if ( reloc
->r_extern() ) {
6396 const macho_nlist
<P
>& targetSymbol
= parser
.symbolFromIndex(reloc
->r_symbolnum());
6397 target
.name
= parser
.nameFromSymbol(targetSymbol
);
6398 target
.weakImport
= parser
.weakImportFromSymbol(targetSymbol
);
6399 target
.addend
= (int32_t)contentValue
;
6402 parser
.findTargetFromAddressAndSectionNum(contentValue
, reloc
->r_symbolnum(), target
);
6404 if ( (kind
== ld::Fixup::kindStoreX86BranchPCRel32
) && (target
.name
!= NULL
) ) {
6405 if ( strncmp(target
.name
, "___dtrace_probe$", 16) == 0 ) {
6406 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindStoreX86DtraceCallSiteNop
, false, target
.name
);
6407 parser
.addDtraceExtraInfos(src
, &target
.name
[16]);
6410 else if ( strncmp(target
.name
, "___dtrace_isenabled$", 20) == 0 ) {
6411 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindStoreX86DtraceIsEnableSiteClear
, false, target
.name
);
6412 parser
.addDtraceExtraInfos(src
, &target
.name
[20]);
6416 parser
.addFixups(src
, kind
, target
);
6419 case GENERIC_RLEOC_TLV
:
6421 if ( !reloc
->r_extern() )
6422 throw "r_extern=0 and r_type=GENERIC_RLEOC_TLV not supported";
6423 if ( reloc
->r_length() != 2 )
6424 throw "r_length!=2 and r_type=GENERIC_RLEOC_TLV not supported";
6425 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(reloc
->r_symbolnum());
6426 // use direct reference for local symbols
6427 if ( ((sym
.n_type() & N_TYPE
) == N_SECT
) && ((sym
.n_type() & N_EXT
) == 0) ) {
6428 parser
.findTargetFromAddressAndSectionNum(sym
.n_value(), sym
.n_sect(), target
);
6432 target
.name
= parser
.nameFromSymbol(sym
);
6433 target
.weakImport
= parser
.weakImportFromSymbol(sym
);
6435 target
.addend
= (int64_t)(int32_t)E::get32(*((uint32_t*)fixUpPtr
));
6436 if ( reloc
->r_pcrel() ) {
6437 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel32TLVLoad
, target
);
6440 parser
.addFixups(src
, ld::Fixup::kindStoreX86Abs32TLVLoad
, target
);
6446 throwf("unsupported i386 relocation type (%d)", reloc
->r_type());
6450 // scattered relocation
6451 const macho_scattered_relocation_info
<P
>* sreloc
= (macho_scattered_relocation_info
<P
>*)reloc
;
6452 srcAddr
= sect
->addr() + sreloc
->r_address();
6453 src
.atom
= this->findAtomByAddress(srcAddr
);
6454 assert(src
.atom
!= NULL
);
6455 src
.offsetInAtom
= srcAddr
- src
.atom
->_objAddress
;
6456 fixUpPtr
= file().fileContent() + sect
->offset() + sreloc
->r_address();
6457 uint32_t relocValue
= sreloc
->r_value();
6458 bool result
= false;
6459 // file format allows pair to be scattered or not
6460 const macho_scattered_relocation_info
<P
>* nextSReloc
= &sreloc
[1];
6461 const macho_relocation_info
<P
>* nextReloc
= &reloc
[1];
6462 bool nextRelocIsPair
= false;
6463 uint32_t nextRelocAddress
= 0;
6464 uint32_t nextRelocValue
= 0;
6465 if ( (nextReloc
->r_address() & R_SCATTERED
) == 0 ) {
6466 if ( nextReloc
->r_type() == GENERIC_RELOC_PAIR
) {
6467 nextRelocIsPair
= true;
6468 nextRelocAddress
= nextReloc
->r_address();
6469 result
= true; // iterator should skip next reloc, since we've consumed it here
6473 if ( nextSReloc
->r_type() == GENERIC_RELOC_PAIR
) {
6474 nextRelocIsPair
= true;
6475 nextRelocAddress
= nextSReloc
->r_address();
6476 nextRelocValue
= nextSReloc
->r_value();
6479 switch (sreloc
->r_type()) {
6480 case GENERIC_RELOC_VANILLA
:
6481 // with a scattered relocation we get both the target (sreloc->r_value()) and the target+offset (*fixUpPtr)
6482 target
.atom
= parser
.findAtomByAddress(relocValue
);
6483 if ( sreloc
->r_pcrel() ) {
6484 switch ( sreloc
->r_length() ) {
6486 contentValue
= srcAddr
+ 1 + *fixUpPtr
;
6487 target
.addend
= (int32_t)contentValue
- (int32_t)relocValue
;
6488 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel8
, target
);
6491 contentValue
= srcAddr
+ 2 + LittleEndian::get16(*((uint16_t*)fixUpPtr
));
6492 target
.addend
= (int32_t)contentValue
- (int32_t)relocValue
;
6493 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel16
, target
);
6496 contentValue
= srcAddr
+ 4 + LittleEndian::get32(*((uint32_t*)fixUpPtr
));
6497 target
.addend
= (int32_t)contentValue
- (int32_t)relocValue
;
6498 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel32
, target
);
6501 throw "unsupported r_length=3 for scattered pc-rel vanilla reloc";
6506 if ( sreloc
->r_length() != 2 )
6507 throwf("unsupported r_length=%d for scattered vanilla reloc", sreloc
->r_length());
6508 contentValue
= LittleEndian::get32(*((uint32_t*)fixUpPtr
));
6509 target
.addend
= (int32_t)contentValue
- (int32_t)(target
.atom
->objectAddress());
6510 parser
.addFixups(src
, ld::Fixup::kindStoreLittleEndian32
, target
);
6513 case GENERIC_RELOC_SECTDIFF
:
6514 case GENERIC_RELOC_LOCAL_SECTDIFF
:
6516 if ( !nextRelocIsPair
)
6517 throw "GENERIC_RELOC_SECTDIFF missing following pair";
6518 switch ( sreloc
->r_length() ) {
6521 throw "bad length for GENERIC_RELOC_SECTDIFF";
6523 contentValue
= (int32_t)(int16_t)LittleEndian::get16(*((uint16_t*)fixUpPtr
));
6524 kind
= ld::Fixup::kindStoreLittleEndian16
;
6527 contentValue
= LittleEndian::get32(*((uint32_t*)fixUpPtr
));
6528 kind
= ld::Fixup::kindStoreLittleEndian32
;
6531 Atom
<x86
>* fromAtom
= parser
.findAtomByAddress(nextRelocValue
);
6532 uint32_t offsetInFrom
= nextRelocValue
- fromAtom
->_objAddress
;
6533 parser
.findTargetFromAddress(sreloc
->r_value(), target
);
6534 // check for addend encoded in the section content
6535 int64_t addend
= (int32_t)contentValue
- (int32_t)(sreloc
->r_value() - nextRelocValue
);
6537 // switch binding base on coalescing
6538 if ( target
.atom
== NULL
) {
6539 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, false, target
.name
);
6541 else if ( target
.atom
->scope() == ld::Atom::scopeTranslationUnit
) {
6542 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, target
.atom
);
6544 else if ( (target
.atom
->combine() == ld::Atom::combineByNameAndContent
) || (target
.atom
->combine() == ld::Atom::combineByNameAndReferences
) ) {
6545 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, target
.atom
);
6548 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, false, target
.atom
->name());
6550 parser
.addFixup(src
, ld::Fixup::k2of5
, ld::Fixup::kindAddAddend
, target
.addend
);
6551 parser
.addFixup(src
, ld::Fixup::k3of5
, ld::Fixup::kindSubtractTargetAddress
, fromAtom
);
6552 parser
.addFixup(src
, ld::Fixup::k4of5
, ld::Fixup::kindSubtractAddend
, offsetInFrom
-addend
);
6553 parser
.addFixup(src
, ld::Fixup::k5of5
, kind
);
6556 // switch binding base on coalescing
6557 if ( target
.atom
== NULL
) {
6558 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, false, target
.name
);
6560 else if ( target
.atom
->scope() == ld::Atom::scopeTranslationUnit
) {
6561 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, target
.atom
);
6563 else if ( (target
.atom
->combine() == ld::Atom::combineByNameAndContent
) || (target
.atom
->combine() == ld::Atom::combineByNameAndReferences
) ) {
6564 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, target
.atom
);
6567 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, false, target
.atom
->name());
6569 parser
.addFixup(src
, ld::Fixup::k2of5
, ld::Fixup::kindAddAddend
, target
.addend
+addend
);
6570 parser
.addFixup(src
, ld::Fixup::k3of5
, ld::Fixup::kindSubtractTargetAddress
, fromAtom
);
6571 parser
.addFixup(src
, ld::Fixup::k4of5
, ld::Fixup::kindSubtractAddend
, offsetInFrom
);
6572 parser
.addFixup(src
, ld::Fixup::k5of5
, kind
);
6585 #if SUPPORT_ARCH_arm_any
6587 bool Section
<arm
>::addRelocFixup(class Parser
<arm
>& parser
, const macho_relocation_info
<P
>* reloc
)
6589 const macho_section
<P
>* sect
= this->machoSection();
6590 bool result
= false;
6594 int32_t displacement
= 0;
6595 uint32_t instruction
= 0;
6596 pint_t contentValue
= 0;
6597 Parser
<arm
>::SourceLocation src
;
6598 Parser
<arm
>::TargetDesc target
;
6599 const macho_relocation_info
<P
>* nextReloc
;
6601 if ( (reloc
->r_address() & R_SCATTERED
) == 0 ) {
6602 bool externSymbolIsThumbDef
= false;
6603 srcAddr
= sect
->addr() + reloc
->r_address();
6604 src
.atom
= this->findAtomByAddress(srcAddr
);
6605 src
.offsetInAtom
= srcAddr
- src
.atom
->_objAddress
;
6606 fixUpPtr
= (uint32_t*)(file().fileContent() + sect
->offset() + reloc
->r_address());
6607 if ( reloc
->r_type() != ARM_RELOC_PAIR
)
6608 instruction
= LittleEndian::get32(*fixUpPtr
);
6609 if ( reloc
->r_extern() ) {
6610 const macho_nlist
<P
>& targetSymbol
= parser
.symbolFromIndex(reloc
->r_symbolnum());
6611 // use direct reference for local symbols
6612 if ( ((targetSymbol
.n_type() & N_TYPE
) == N_SECT
) && (((targetSymbol
.n_type() & N_EXT
) == 0) || (parser
.nameFromSymbol(targetSymbol
)[0] == 'L')) ) {
6613 parser
.findTargetFromAddressAndSectionNum(targetSymbol
.n_value(), targetSymbol
.n_sect(), target
);
6617 target
.name
= parser
.nameFromSymbol(targetSymbol
);
6618 target
.weakImport
= parser
.weakImportFromSymbol(targetSymbol
);
6619 if ( ((targetSymbol
.n_type() & N_TYPE
) == N_SECT
) && (targetSymbol
.n_desc() & N_ARM_THUMB_DEF
) )
6620 externSymbolIsThumbDef
= true;
6623 switch ( reloc
->r_type() ) {
6624 case ARM_RELOC_BR24
:
6625 // Sign-extend displacement
6626 displacement
= (instruction
& 0x00FFFFFF) << 2;
6627 if ( (displacement
& 0x02000000) != 0 )
6628 displacement
|= 0xFC000000;
6629 // The pc added will be +8 from the pc
6631 // If this is BLX add H << 1
6632 if ((instruction
& 0xFE000000) == 0xFA000000)
6633 displacement
+= ((instruction
& 0x01000000) >> 23);
6634 if ( reloc
->r_extern() ) {
6635 dstAddr
= srcAddr
+ displacement
;
6636 // <rdar://problem/16652542> support large .o files
6637 if ( srcAddr
> 0x2000000 ) {
6638 dstAddr
-= ((srcAddr
+ 0x1FFFFFF) & 0xFC000000);
6640 target
.addend
= dstAddr
;
6641 if ( externSymbolIsThumbDef
)
6642 target
.addend
&= -2; // remove thumb bit
6645 dstAddr
= srcAddr
+ displacement
;
6646 parser
.findTargetFromAddressAndSectionNum(dstAddr
, reloc
->r_symbolnum(), target
);
6648 // special case "calls" for dtrace
6649 if ( (target
.name
!= NULL
) && (strncmp(target
.name
, "___dtrace_probe$", 16) == 0) ) {
6650 parser
.addFixup(src
, ld::Fixup::k1of1
,
6651 ld::Fixup::kindStoreARMDtraceCallSiteNop
, false, target
.name
);
6652 parser
.addDtraceExtraInfos(src
, &target
.name
[16]);
6654 else if ( (target
.name
!= NULL
) && (strncmp(target
.name
, "___dtrace_isenabled$", 20) == 0) ) {
6655 parser
.addFixup(src
, ld::Fixup::k1of1
,
6656 ld::Fixup::kindStoreARMDtraceIsEnableSiteClear
, false, target
.name
);
6657 parser
.addDtraceExtraInfos(src
, &target
.name
[20]);
6660 parser
.addFixups(src
, ld::Fixup::kindStoreARMBranch24
, target
);
6663 case ARM_THUMB_RELOC_BR22
:
6664 // thumb2 added two more bits to displacement, complicating the displacement decoding
6666 uint32_t s
= (instruction
>> 10) & 0x1;
6667 uint32_t j1
= (instruction
>> 29) & 0x1;
6668 uint32_t j2
= (instruction
>> 27) & 0x1;
6669 uint32_t imm10
= instruction
& 0x3FF;
6670 uint32_t imm11
= (instruction
>> 16) & 0x7FF;
6671 uint32_t i1
= (j1
== s
);
6672 uint32_t i2
= (j2
== s
);
6673 uint32_t dis
= (s
<< 24) | (i1
<< 23) | (i2
<< 22) | (imm10
<< 12) | (imm11
<< 1);
6677 displacement
= sdis
;
6679 // The pc added will be +4 from the pc
6681 // If the instruction was blx, force the low 2 bits to be clear
6682 dstAddr
= srcAddr
+ displacement
;
6683 if ((instruction
& 0xD0000000) == 0xC0000000)
6684 dstAddr
&= 0xFFFFFFFC;
6686 if ( reloc
->r_extern() ) {
6687 // <rdar://problem/16652542> support large .o files
6688 if ( srcAddr
> 0x1000000 ) {
6689 dstAddr
-= ((srcAddr
+ 0xFFFFFF) & 0xFE000000);
6691 target
.addend
= (int64_t)(int32_t)dstAddr
;
6694 parser
.findTargetFromAddressAndSectionNum(dstAddr
, reloc
->r_symbolnum(), target
);
6696 // special case "calls" for dtrace
6697 if ( (target
.name
!= NULL
) && (strncmp(target
.name
, "___dtrace_probe$", 16) == 0) ) {
6698 parser
.addFixup(src
, ld::Fixup::k1of1
,
6699 ld::Fixup::kindStoreThumbDtraceCallSiteNop
, false, target
.name
);
6700 parser
.addDtraceExtraInfos(src
, &target
.name
[16]);
6702 else if ( (target
.name
!= NULL
) && (strncmp(target
.name
, "___dtrace_isenabled$", 20) == 0) ) {
6703 parser
.addFixup(src
, ld::Fixup::k1of1
,
6704 ld::Fixup::kindStoreThumbDtraceIsEnableSiteClear
, false, target
.name
);
6705 parser
.addDtraceExtraInfos(src
, &target
.name
[20]);
6708 parser
.addFixups(src
, ld::Fixup::kindStoreThumbBranch22
, target
);
6711 case ARM_RELOC_VANILLA
:
6712 if ( reloc
->r_length() != 2 )
6713 throw "bad length for ARM_RELOC_VANILLA";
6714 contentValue
= LittleEndian::get32(*fixUpPtr
);
6715 if ( reloc
->r_extern() ) {
6716 target
.addend
= (int32_t)contentValue
;
6717 if ( externSymbolIsThumbDef
)
6718 target
.addend
&= -2; // remove thumb bit
6721 parser
.findTargetFromAddressAndSectionNum(contentValue
, reloc
->r_symbolnum(), target
);
6722 // possible non-extern relocation turned into by-name ref because target is a weak-def
6723 if ( target
.atom
!= NULL
) {
6724 if ( target
.atom
->isThumb() )
6725 target
.addend
&= -2; // remove thumb bit
6726 // if reference to LSDA, add group subordinate fixup
6727 if ( target
.atom
->contentType() == ld::Atom::typeLSDA
) {
6728 Parser
<arm
>::SourceLocation src2
;
6729 src2
.atom
= src
.atom
;
6730 src2
.offsetInAtom
= 0;
6731 parser
.addFixup(src2
, ld::Fixup::k1of1
, ld::Fixup::kindNoneGroupSubordinateLSDA
, target
.atom
);
6735 parser
.addFixups(src
, ld::Fixup::kindStoreLittleEndian32
, target
);
6737 case ARM_THUMB_32BIT_BRANCH
:
6738 // silently ignore old unnecessary reloc
6740 case ARM_RELOC_HALF
:
6741 nextReloc
= &reloc
[1];
6742 if ( nextReloc
->r_type() == ARM_RELOC_PAIR
) {
6743 uint32_t instruction16
;
6744 uint32_t other16
= (nextReloc
->r_address() & 0xFFFF);
6746 if ( reloc
->r_length() & 2 ) {
6748 uint32_t i
= ((instruction
& 0x00000400) >> 10);
6749 uint32_t imm4
= (instruction
& 0x0000000F);
6750 uint32_t imm3
= ((instruction
& 0x70000000) >> 28);
6751 uint32_t imm8
= ((instruction
& 0x00FF0000) >> 16);
6752 instruction16
= (imm4
<< 12) | (i
<< 11) | (imm3
<< 8) | imm8
;
6756 uint32_t imm4
= ((instruction
& 0x000F0000) >> 16);
6757 uint32_t imm12
= (instruction
& 0x00000FFF);
6758 instruction16
= (imm4
<< 12) | imm12
;
6760 if ( reloc
->r_length() & 1 ) {
6762 dstAddr
= ((instruction16
<< 16) | other16
);
6763 if ( reloc
->r_extern() ) {
6764 target
.addend
= dstAddr
;
6765 if ( externSymbolIsThumbDef
)
6766 target
.addend
&= -2; // remove thumb bit
6769 parser
.findTargetFromAddress(dstAddr
, target
);
6770 if ( target
.atom
->isThumb() )
6771 target
.addend
&= (-2); // remove thumb bit
6773 parser
.addFixups(src
, (isThumb
? ld::Fixup::kindStoreThumbHigh16
: ld::Fixup::kindStoreARMHigh16
), target
);
6777 dstAddr
= (other16
<< 16) | instruction16
;
6778 if ( reloc
->r_extern() ) {
6779 target
.addend
= dstAddr
;
6780 if ( externSymbolIsThumbDef
)
6781 target
.addend
&= -2; // remove thumb bit
6784 parser
.findTargetFromAddress(dstAddr
, target
);
6785 if ( target
.atom
->isThumb() )
6786 target
.addend
&= (-2); // remove thumb bit
6788 parser
.addFixups(src
, (isThumb
? ld::Fixup::kindStoreThumbLow16
: ld::Fixup::kindStoreARMLow16
), target
);
6793 throw "for ARM_RELOC_HALF, next reloc is not ARM_RELOC_PAIR";
6796 throwf("unknown relocation type %d", reloc
->r_type());
6801 const macho_scattered_relocation_info
<P
>* sreloc
= (macho_scattered_relocation_info
<P
>*)reloc
;
6802 // file format allows pair to be scattered or not
6803 const macho_scattered_relocation_info
<P
>* nextSReloc
= &sreloc
[1];
6804 nextReloc
= &reloc
[1];
6805 srcAddr
= sect
->addr() + sreloc
->r_address();
6806 dstAddr
= sreloc
->r_value();
6807 fixUpPtr
= (uint32_t*)(file().fileContent() + sect
->offset() + sreloc
->r_address());
6808 instruction
= LittleEndian::get32(*fixUpPtr
);
6809 src
.atom
= this->findAtomByAddress(srcAddr
);
6810 src
.offsetInAtom
= srcAddr
- src
.atom
->_objAddress
;
6811 bool nextRelocIsPair
= false;
6812 uint32_t nextRelocAddress
= 0;
6813 uint32_t nextRelocValue
= 0;
6814 if ( (nextReloc
->r_address() & R_SCATTERED
) == 0 ) {
6815 if ( nextReloc
->r_type() == ARM_RELOC_PAIR
) {
6816 nextRelocIsPair
= true;
6817 nextRelocAddress
= nextReloc
->r_address();
6822 if ( nextSReloc
->r_type() == ARM_RELOC_PAIR
) {
6823 nextRelocIsPair
= true;
6824 nextRelocAddress
= nextSReloc
->r_address();
6825 nextRelocValue
= nextSReloc
->r_value();
6829 switch ( sreloc
->r_type() ) {
6830 case ARM_RELOC_VANILLA
:
6831 // with a scattered relocation we get both the target (sreloc->r_value()) and the target+offset (*fixUpPtr)
6832 if ( sreloc
->r_length() != 2 )
6833 throw "bad length for ARM_RELOC_VANILLA";
6834 target
.atom
= parser
.findAtomByAddress(sreloc
->r_value());
6835 if ( target
.atom
== NULL
)
6836 throwf("bad r_value (0x%08X) for ARM_RELOC_VANILLA\n", sreloc
->r_value());
6837 contentValue
= LittleEndian::get32(*fixUpPtr
);
6838 target
.addend
= contentValue
- target
.atom
->_objAddress
;
6839 if ( target
.atom
->isThumb() )
6840 target
.addend
&= -2; // remove thumb bit
6841 parser
.addFixups(src
, ld::Fixup::kindStoreLittleEndian32
, target
);
6843 case ARM_RELOC_BR24
:
6844 // Sign-extend displacement
6845 displacement
= (instruction
& 0x00FFFFFF) << 2;
6846 if ( (displacement
& 0x02000000) != 0 )
6847 displacement
|= 0xFC000000;
6848 // The pc added will be +8 from the pc
6850 // If this is BLX add H << 1
6851 if ((instruction
& 0xFE000000) == 0xFA000000)
6852 displacement
+= ((instruction
& 0x01000000) >> 23);
6853 target
.atom
= parser
.findAtomByAddress(sreloc
->r_value());
6854 target
.addend
= (int64_t)(srcAddr
+ displacement
) - (int64_t)(target
.atom
->_objAddress
);
6855 parser
.addFixups(src
, ld::Fixup::kindStoreARMBranch24
, target
);
6857 case ARM_THUMB_RELOC_BR22
:
6858 // thumb2 added two more bits to displacement, complicating the displacement decoding
6860 uint32_t s
= (instruction
>> 10) & 0x1;
6861 uint32_t j1
= (instruction
>> 29) & 0x1;
6862 uint32_t j2
= (instruction
>> 27) & 0x1;
6863 uint32_t imm10
= instruction
& 0x3FF;
6864 uint32_t imm11
= (instruction
>> 16) & 0x7FF;
6865 uint32_t i1
= (j1
== s
);
6866 uint32_t i2
= (j2
== s
);
6867 uint32_t dis
= (s
<< 24) | (i1
<< 23) | (i2
<< 22) | (imm10
<< 12) | (imm11
<< 1);
6871 displacement
= sdis
;
6873 // The pc added will be +4 from the pc
6875 dstAddr
= srcAddr
+displacement
;
6876 // If the instruction was blx, force the low 2 bits to be clear
6877 if ((instruction
& 0xF8000000) == 0xE8000000)
6878 dstAddr
&= 0xFFFFFFFC;
6879 target
.atom
= parser
.findAtomByAddress(sreloc
->r_value());
6880 target
.addend
= dstAddr
- target
.atom
->_objAddress
;
6881 parser
.addFixups(src
, ld::Fixup::kindStoreThumbBranch22
, target
);
6883 case ARM_RELOC_SECTDIFF
:
6884 case ARM_RELOC_LOCAL_SECTDIFF
:
6886 if ( ! nextRelocIsPair
)
6887 throw "ARM_RELOC_SECTDIFF missing following pair";
6888 if ( sreloc
->r_length() != 2 )
6889 throw "bad length for ARM_RELOC_SECTDIFF";
6890 contentValue
= LittleEndian::get32(*fixUpPtr
);
6891 Atom
<arm
>* fromAtom
= parser
.findAtomByAddress(nextRelocValue
);
6892 uint32_t offsetInFrom
= nextRelocValue
- fromAtom
->_objAddress
;
6893 uint32_t offsetInTarget
;
6894 Atom
<arm
>* targetAtom
= parser
.findAtomByAddressOrLocalTargetOfStub(sreloc
->r_value(), &offsetInTarget
);
6895 // check for addend encoded in the section content
6896 int64_t addend
= (int32_t)contentValue
- (int32_t)(sreloc
->r_value() - nextRelocValue
);
6897 if ( targetAtom
->isThumb() )
6898 addend
&= -2; // remove thumb bit
6899 // if reference to LSDA, add group subordinate fixup
6900 if ( targetAtom
->contentType() == ld::Atom::typeLSDA
) {
6901 Parser
<arm
>::SourceLocation src2
;
6902 src2
.atom
= src
.atom
;
6903 src2
.offsetInAtom
= 0;
6904 parser
.addFixup(src2
, ld::Fixup::k1of1
, ld::Fixup::kindNoneGroupSubordinateLSDA
, targetAtom
);
6907 // switch binding base on coalescing
6908 if ( targetAtom
->scope() == ld::Atom::scopeTranslationUnit
) {
6909 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, targetAtom
);
6911 else if ( (targetAtom
->combine() == ld::Atom::combineByNameAndContent
) || (targetAtom
->combine() == ld::Atom::combineByNameAndReferences
) ) {
6912 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, targetAtom
);
6915 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, false, targetAtom
->name());
6917 parser
.addFixup(src
, ld::Fixup::k2of5
, ld::Fixup::kindAddAddend
, offsetInTarget
);
6918 parser
.addFixup(src
, ld::Fixup::k3of5
, ld::Fixup::kindSubtractTargetAddress
, fromAtom
);
6919 parser
.addFixup(src
, ld::Fixup::k4of5
, ld::Fixup::kindSubtractAddend
, offsetInFrom
-addend
);
6920 parser
.addFixup(src
, ld::Fixup::k5of5
, ld::Fixup::kindStoreLittleEndian32
);
6923 if ( targetAtom
->scope() == ld::Atom::scopeTranslationUnit
) {
6924 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, targetAtom
);
6926 else if ( (targetAtom
->combine() == ld::Atom::combineByNameAndContent
) || (targetAtom
->combine() == ld::Atom::combineByNameAndReferences
) ) {
6927 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, targetAtom
);
6930 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, false, targetAtom
->name());
6932 parser
.addFixup(src
, ld::Fixup::k2of5
, ld::Fixup::kindAddAddend
, (uint32_t)(offsetInTarget
+addend
));
6933 parser
.addFixup(src
, ld::Fixup::k3of5
, ld::Fixup::kindSubtractTargetAddress
, fromAtom
);
6934 parser
.addFixup(src
, ld::Fixup::k4of5
, ld::Fixup::kindSubtractAddend
, offsetInFrom
);
6935 parser
.addFixup(src
, ld::Fixup::k5of5
, ld::Fixup::kindStoreLittleEndian32
);
6939 case ARM_RELOC_HALF_SECTDIFF
:
6940 if ( nextRelocIsPair
) {
6941 instruction
= LittleEndian::get32(*fixUpPtr
);
6942 Atom
<arm
>* fromAtom
= parser
.findAtomByAddress(nextRelocValue
);
6943 uint32_t offsetInFrom
= nextRelocValue
- fromAtom
->_objAddress
;
6944 Atom
<arm
>* targetAtom
= parser
.findAtomByAddress(sreloc
->r_value());
6945 uint32_t offsetInTarget
= sreloc
->r_value() - targetAtom
->_objAddress
;
6946 uint32_t instruction16
;
6947 uint32_t other16
= (nextRelocAddress
& 0xFFFF);
6949 if ( sreloc
->r_length() & 2 ) {
6951 uint32_t i
= ((instruction
& 0x00000400) >> 10);
6952 uint32_t imm4
= (instruction
& 0x0000000F);
6953 uint32_t imm3
= ((instruction
& 0x70000000) >> 28);
6954 uint32_t imm8
= ((instruction
& 0x00FF0000) >> 16);
6955 instruction16
= (imm4
<< 12) | (i
<< 11) | (imm3
<< 8) | imm8
;
6959 uint32_t imm4
= ((instruction
& 0x000F0000) >> 16);
6960 uint32_t imm12
= (instruction
& 0x00000FFF);
6961 instruction16
= (imm4
<< 12) | imm12
;
6963 if ( sreloc
->r_length() & 1 )
6964 dstAddr
= ((instruction16
<< 16) | other16
);
6966 dstAddr
= (other16
<< 16) | instruction16
;
6967 if ( targetAtom
->isThumb() )
6968 dstAddr
&= (-2); // remove thumb bit
6969 int32_t addend
= dstAddr
- (sreloc
->r_value() - nextRelocValue
);
6970 if ( targetAtom
->scope() == ld::Atom::scopeTranslationUnit
) {
6971 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, targetAtom
);
6973 else if ( (targetAtom
->combine() == ld::Atom::combineByNameAndContent
) || (targetAtom
->combine() == ld::Atom::combineByNameAndReferences
) ) {
6974 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, targetAtom
);
6977 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, false, targetAtom
->name());
6979 parser
.addFixup(src
, ld::Fixup::k2of5
, ld::Fixup::kindAddAddend
, (uint32_t)offsetInTarget
+addend
);
6980 parser
.addFixup(src
, ld::Fixup::k3of5
, ld::Fixup::kindSubtractTargetAddress
, fromAtom
);
6981 parser
.addFixup(src
, ld::Fixup::k4of5
, ld::Fixup::kindSubtractAddend
, offsetInFrom
);
6982 if ( sreloc
->r_length() & 1 ) {
6984 parser
.addFixup(src
, ld::Fixup::k5of5
, (isThumb
? ld::Fixup::kindStoreThumbHigh16
: ld::Fixup::kindStoreARMHigh16
));
6988 parser
.addFixup(src
, ld::Fixup::k5of5
, (isThumb
? ld::Fixup::kindStoreThumbLow16
: ld::Fixup::kindStoreARMLow16
));
6993 throw "ARM_RELOC_HALF_SECTDIFF reloc missing following pair";
6995 case ARM_RELOC_HALF
:
6996 if ( nextRelocIsPair
) {
6997 instruction
= LittleEndian::get32(*fixUpPtr
);
6998 Atom
<arm
>* targetAtom
= parser
.findAtomByAddress(sreloc
->r_value());
6999 uint32_t instruction16
;
7000 uint32_t other16
= (nextRelocAddress
& 0xFFFF);
7002 if ( sreloc
->r_length() & 2 ) {
7004 uint32_t i
= ((instruction
& 0x00000400) >> 10);
7005 uint32_t imm4
= (instruction
& 0x0000000F);
7006 uint32_t imm3
= ((instruction
& 0x70000000) >> 28);
7007 uint32_t imm8
= ((instruction
& 0x00FF0000) >> 16);
7008 instruction16
= (imm4
<< 12) | (i
<< 11) | (imm3
<< 8) | imm8
;
7012 uint32_t imm4
= ((instruction
& 0x000F0000) >> 16);
7013 uint32_t imm12
= (instruction
& 0x00000FFF);
7014 instruction16
= (imm4
<< 12) | imm12
;
7016 if ( sreloc
->r_length() & 1 )
7017 dstAddr
= ((instruction16
<< 16) | other16
);
7019 dstAddr
= (other16
<< 16) | instruction16
;
7020 if ( targetAtom
->scope() == ld::Atom::scopeTranslationUnit
) {
7021 parser
.addFixup(src
, ld::Fixup::k1of3
, ld::Fixup::kindSetTargetAddress
, targetAtom
);
7023 else if ( (targetAtom
->combine() == ld::Atom::combineByNameAndContent
) || (targetAtom
->combine() == ld::Atom::combineByNameAndReferences
) ) {
7024 parser
.addFixup(src
, ld::Fixup::k1of3
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, targetAtom
);
7027 parser
.addFixup(src
, ld::Fixup::k1of3
, ld::Fixup::kindSetTargetAddress
, false, targetAtom
->name());
7029 parser
.addFixup(src
, ld::Fixup::k2of3
, ld::Fixup::kindAddAddend
, dstAddr
- targetAtom
->_objAddress
);
7030 if ( sreloc
->r_length() & 1 ) {
7032 parser
.addFixup(src
, ld::Fixup::k3of3
, (isThumb
? ld::Fixup::kindStoreThumbHigh16
: ld::Fixup::kindStoreARMHigh16
));
7036 parser
.addFixup(src
, ld::Fixup::k3of3
, (isThumb
? ld::Fixup::kindStoreThumbLow16
: ld::Fixup::kindStoreARMLow16
));
7041 throw "scattered ARM_RELOC_HALF reloc missing following pair";
7044 throwf("unknown ARM scattered relocation type %d", sreloc
->r_type());
7052 #if SUPPORT_ARCH_arm64
7054 bool Section
<arm64
>::addRelocFixup(class Parser
<arm64
>& parser
, const macho_relocation_info
<P
>* reloc
)
7056 bool result
= false;
7057 Parser
<arm64
>::SourceLocation src
;
7058 Parser
<arm64
>::TargetDesc target
= { NULL
, NULL
, false, 0 };
7059 Parser
<arm64
>::TargetDesc toTarget
;
7060 int32_t prefixRelocAddend
= 0;
7061 if ( reloc
->r_type() == ARM64_RELOC_ADDEND
) {
7062 uint32_t rawAddend
= reloc
->r_symbolnum();
7063 prefixRelocAddend
= rawAddend
;
7064 if ( rawAddend
& 0x00800000 )
7065 prefixRelocAddend
|= 0xFF000000; // sign extend 24-bit signed int to 32-bits
7066 uint32_t addendAddress
= reloc
->r_address();
7067 ++reloc
; //advance to next reloc record
7069 if ( reloc
->r_address() != addendAddress
)
7070 throw "ARM64_RELOC_ADDEND r_address does not match next reloc's r_address";
7072 const macho_section
<P
>* sect
= this->machoSection();
7073 uint64_t srcAddr
= sect
->addr() + reloc
->r_address();
7074 src
.atom
= this->findAtomByAddress(srcAddr
);
7075 src
.offsetInAtom
= srcAddr
- src
.atom
->_objAddress
;
7076 const uint8_t* fixUpPtr
= file().fileContent() + sect
->offset() + reloc
->r_address();
7077 uint64_t contentValue
= 0;
7078 const macho_relocation_info
<arm64::P
>* nextReloc
= &reloc
[1];
7079 bool useDirectBinding
;
7080 uint32_t instruction
;
7081 uint32_t encodedAddend
;
7082 switch ( reloc
->r_length() ) {
7084 contentValue
= *fixUpPtr
;
7087 contentValue
= (int64_t)(int16_t)E::get16(*((uint16_t*)fixUpPtr
));
7090 contentValue
= (int64_t)(int32_t)E::get32(*((uint32_t*)fixUpPtr
));
7093 contentValue
= E::get64(*((uint64_t*)fixUpPtr
));
7096 if ( reloc
->r_extern() ) {
7097 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(reloc
->r_symbolnum());
7098 const char* symbolName
= parser
.nameFromSymbol(sym
);
7099 if ( ((sym
.n_type() & N_TYPE
) == N_SECT
) && (((sym
.n_type() & N_EXT
) == 0) || (symbolName
[0] == 'L') || (symbolName
[0] == 'l')) ) {
7100 // use direct reference for local symbols
7101 parser
.findTargetFromAddressAndSectionNum(sym
.n_value(), sym
.n_sect(), target
);
7102 //target.addend += contentValue;
7104 else if ( ((sym
.n_type() & N_TYPE
) == N_SECT
) && (src
.atom
->_objAddress
<= sym
.n_value()) && (sym
.n_value() < (src
.atom
->_objAddress
+src
.atom
->size())) ) {
7105 // <rdar://problem/13700961> spurious warning when weak function has reference to itself
7106 // use direct reference when atom targets itself
7107 target
.atom
= src
.atom
;
7111 target
.name
= symbolName
;
7112 target
.weakImport
= parser
.weakImportFromSymbol(sym
);
7113 //target.addend = contentValue;
7115 // cfstrings should always use direct reference to backing store
7116 if ( (this->type() == ld::Section::typeCFString
) && (src
.offsetInAtom
!= 0) ) {
7117 parser
.findTargetFromAddressAndSectionNum(sym
.n_value(), sym
.n_sect(), target
);
7118 //target.addend = contentValue;
7122 if ( reloc
->r_pcrel() )
7123 contentValue
+= srcAddr
;
7124 parser
.findTargetFromAddressAndSectionNum(contentValue
, reloc
->r_symbolnum(), target
);
7126 switch ( reloc
->r_type() ) {
7127 case ARM64_RELOC_UNSIGNED
:
7128 if ( reloc
->r_pcrel() )
7129 throw "pcrel and ARM64_RELOC_UNSIGNED not supported";
7130 if ( reloc
->r_extern() )
7131 target
.addend
= contentValue
;
7132 switch ( reloc
->r_length() ) {
7135 throw "length < 2 and ARM64_RELOC_UNSIGNED not supported";
7137 parser
.addFixups(src
, ld::Fixup::kindStoreLittleEndian32
, target
);
7140 parser
.addFixups(src
, ld::Fixup::kindStoreLittleEndian64
, target
);
7144 case ARM64_RELOC_BRANCH26
:
7145 if ( ! reloc
->r_pcrel() )
7146 throw "not pcrel and ARM64_RELOC_BRANCH26 not supported";
7147 if ( ! reloc
->r_extern() )
7148 throw "r_extern == 0 and ARM64_RELOC_BRANCH26 not supported";
7149 if ( reloc
->r_length() != 2 )
7150 throw "r_length != 2 and ARM64_RELOC_BRANCH26 not supported";
7151 if ( (target
.name
!= NULL
) && (strncmp(target
.name
, "___dtrace_probe$", 16) == 0) ) {
7152 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindStoreARM64DtraceCallSiteNop
, false, target
.name
);
7153 parser
.addDtraceExtraInfos(src
, &target
.name
[16]);
7155 else if ( (target
.name
!= NULL
) && (strncmp(target
.name
, "___dtrace_isenabled$", 20) == 0) ) {
7156 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindStoreARM64DtraceIsEnableSiteClear
, false, target
.name
);
7157 parser
.addDtraceExtraInfos(src
, &target
.name
[20]);
7160 target
.addend
= prefixRelocAddend
;
7161 instruction
= contentValue
;
7162 encodedAddend
= (instruction
& 0x03FFFFFF) << 2;
7163 if ( encodedAddend
!= 0 ) {
7164 if ( prefixRelocAddend
== 0 ) {
7165 warning("branch26 instruction at 0x%08X has embedded addend. ARM64_RELOC_ADDEND should be used instead", reloc
->r_address());
7166 target
.addend
= encodedAddend
;
7169 throwf("branch26 instruction at 0x%08X has embedded addend and ARM64_RELOC_ADDEND also used", reloc
->r_address());
7172 parser
.addFixups(src
, ld::Fixup::kindStoreARM64Branch26
, target
);
7175 case ARM64_RELOC_PAGE21
:
7176 if ( ! reloc
->r_pcrel() )
7177 throw "not pcrel and ARM64_RELOC_PAGE21 not supported";
7178 if ( ! reloc
->r_extern() )
7179 throw "r_extern == 0 and ARM64_RELOC_PAGE21 not supported";
7180 if ( reloc
->r_length() != 2 )
7181 throw "length != 2 and ARM64_RELOC_PAGE21 not supported";
7182 target
.addend
= prefixRelocAddend
;
7183 instruction
= contentValue
;
7184 encodedAddend
= ((instruction
& 0x60000000) >> 29) | ((instruction
& 0x01FFFFE0) >> 3);
7185 encodedAddend
*= 4096; // internally addend is in bytes, so scale
7186 if ( encodedAddend
!= 0 ) {
7187 if ( prefixRelocAddend
== 0 ) {
7188 warning("adrp instruction at 0x%08X has embedded addend. ARM64_RELOC_ADDEND should be used instead", reloc
->r_address());
7189 target
.addend
= encodedAddend
;
7192 throwf("adrp instruction at 0x%08X has embedded addend and ARM64_RELOC_ADDEND also used", reloc
->r_address());
7195 parser
.addFixups(src
, ld::Fixup::kindStoreARM64Page21
, target
);
7197 case ARM64_RELOC_PAGEOFF12
:
7198 if ( reloc
->r_pcrel() )
7199 throw "pcrel and ARM64_RELOC_PAGEOFF12 not supported";
7200 if ( ! reloc
->r_extern() )
7201 throw "r_extern == 0 and ARM64_RELOC_PAGEOFF12 not supported";
7202 if ( reloc
->r_length() != 2 )
7203 throw "length != 2 and ARM64_RELOC_PAGEOFF12 not supported";
7204 target
.addend
= prefixRelocAddend
;
7205 instruction
= contentValue
;
7206 encodedAddend
= ((instruction
& 0x003FFC00) >> 10);
7207 // internally addend is in bytes. Some instructions have an implicit scale factor
7208 if ( (instruction
& 0x3B000000) == 0x39000000 ) {
7209 switch ( instruction
& 0xC0000000 ) {
7223 if ( encodedAddend
!= 0 ) {
7224 if ( prefixRelocAddend
== 0 ) {
7225 warning("pageoff12 instruction at 0x%08X has embedded addend. ARM64_RELOC_ADDEND should be used instead", reloc
->r_address());
7226 target
.addend
= encodedAddend
;
7229 throwf("pageoff12 instruction at 0x%08X has embedded addend and ARM64_RELOC_ADDEND also used", reloc
->r_address());
7232 parser
.addFixups(src
, ld::Fixup::kindStoreARM64PageOff12
, target
);
7234 case ARM64_RELOC_GOT_LOAD_PAGE21
:
7235 if ( ! reloc
->r_pcrel() )
7236 throw "not pcrel and ARM64_RELOC_GOT_LOAD_PAGE21 not supported";
7237 if ( ! reloc
->r_extern() )
7238 throw "r_extern == 0 and ARM64_RELOC_GOT_LOAD_PAGE21 not supported";
7239 if ( reloc
->r_length() != 2 )
7240 throw "length != 2 and ARM64_RELOC_GOT_LOAD_PAGE21 not supported";
7241 if ( prefixRelocAddend
!= 0 )
7242 throw "ARM64_RELOC_ADDEND followed by ARM64_RELOC_GOT_LOAD_PAGE21 not supported";
7243 instruction
= contentValue
;
7244 target
.addend
= ((instruction
& 0x60000000) >> 29) | ((instruction
& 0x01FFFFE0) >> 3);
7245 if ( target
.addend
!= 0 )
7246 throw "non-zero addend with ARM64_RELOC_GOT_LOAD_PAGE21 is not supported";
7247 parser
.addFixups(src
, ld::Fixup::kindStoreARM64GOTLoadPage21
, target
);
7249 case ARM64_RELOC_GOT_LOAD_PAGEOFF12
:
7250 if ( reloc
->r_pcrel() )
7251 throw "pcrel and ARM64_RELOC_GOT_LOAD_PAGEOFF12 not supported";
7252 if ( ! reloc
->r_extern() )
7253 throw "r_extern == 0 and ARM64_RELOC_GOT_LOAD_PAGEOFF12 not supported";
7254 if ( reloc
->r_length() != 2 )
7255 throw "length != 2 and ARM64_RELOC_GOT_LOAD_PAGEOFF12 not supported";
7256 if ( prefixRelocAddend
!= 0 )
7257 throw "ARM64_RELOC_ADDEND followed by ARM64_RELOC_GOT_LOAD_PAGEOFF12 not supported";
7258 instruction
= contentValue
;
7259 target
.addend
= ((instruction
& 0x003FFC00) >> 10);
7260 parser
.addFixups(src
, ld::Fixup::kindStoreARM64GOTLoadPageOff12
, target
);
7262 case ARM64_RELOC_TLVP_LOAD_PAGE21
:
7263 if ( ! reloc
->r_pcrel() )
7264 throw "not pcrel and ARM64_RELOC_TLVP_LOAD_PAGE21 not supported";
7265 if ( ! reloc
->r_extern() )
7266 throw "r_extern == 0 and ARM64_RELOC_TLVP_LOAD_PAGE21 not supported";
7267 if ( reloc
->r_length() != 2 )
7268 throw "length != 2 and ARM64_RELOC_TLVP_LOAD_PAGE21 not supported";
7269 if ( prefixRelocAddend
!= 0 )
7270 throw "ARM64_RELOC_ADDEND followed by ARM64_RELOC_TLVP_LOAD_PAGE21 not supported";
7271 instruction
= contentValue
;
7272 target
.addend
= ((instruction
& 0x60000000) >> 29) | ((instruction
& 0x01FFFFE0) >> 3);
7273 if ( target
.addend
!= 0 )
7274 throw "non-zero addend with ARM64_RELOC_GOT_LOAD_PAGE21 is not supported";
7275 parser
.addFixups(src
, ld::Fixup::kindStoreARM64TLVPLoadPage21
, target
);
7277 case ARM64_RELOC_TLVP_LOAD_PAGEOFF12
:
7278 if ( reloc
->r_pcrel() )
7279 throw "pcrel and ARM64_RELOC_TLVP_LOAD_PAGEOFF12 not supported";
7280 if ( ! reloc
->r_extern() )
7281 throw "r_extern == 0 and ARM64_RELOC_TLVP_LOAD_PAGEOFF12 not supported";
7282 if ( reloc
->r_length() != 2 )
7283 throw "length != 2 and ARM64_RELOC_TLVP_LOAD_PAGEOFF12 not supported";
7284 if ( prefixRelocAddend
!= 0 )
7285 throw "ARM64_RELOC_ADDEND followed by ARM64_RELOC_TLVP_LOAD_PAGEOFF12 not supported";
7286 instruction
= contentValue
;
7287 target
.addend
= ((instruction
& 0x003FFC00) >> 10);
7288 parser
.addFixups(src
, ld::Fixup::kindStoreARM64TLVPLoadPageOff12
, target
);
7290 case ARM64_RELOC_SUBTRACTOR
:
7291 if ( reloc
->r_pcrel() )
7292 throw "ARM64_RELOC_SUBTRACTOR cannot be pc-relative";
7293 if ( reloc
->r_length() < 2 )
7294 throw "ARM64_RELOC_SUBTRACTOR must have r_length of 2 or 3";
7295 if ( !reloc
->r_extern() )
7296 throw "ARM64_RELOC_SUBTRACTOR must have r_extern=1";
7297 if ( nextReloc
->r_type() != ARM64_RELOC_UNSIGNED
)
7298 throw "ARM64_RELOC_SUBTRACTOR must be followed by ARM64_RELOC_UNSIGNED";
7299 if ( prefixRelocAddend
!= 0 )
7300 throw "ARM64_RELOC_ADDEND followed by ARM64_RELOC_SUBTRACTOR not supported";
7302 if ( nextReloc
->r_pcrel() )
7303 throw "ARM64_RELOC_UNSIGNED following a ARM64_RELOC_SUBTRACTOR cannot be pc-relative";
7304 if ( nextReloc
->r_length() != reloc
->r_length() )
7305 throw "ARM64_RELOC_UNSIGNED following a ARM64_RELOC_SUBTRACTOR must have same r_length";
7306 if ( nextReloc
->r_extern() ) {
7307 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(nextReloc
->r_symbolnum());
7308 // use direct reference for local symbols
7309 if ( ((sym
.n_type() & N_TYPE
) == N_SECT
) && (((sym
.n_type() & N_EXT
) == 0) || (parser
.nameFromSymbol(sym
)[0] == 'L')) ) {
7310 parser
.findTargetFromAddressAndSectionNum(sym
.n_value(), sym
.n_sect(), toTarget
);
7311 toTarget
.addend
= contentValue
;
7312 useDirectBinding
= true;
7315 toTarget
.name
= parser
.nameFromSymbol(sym
);
7316 toTarget
.weakImport
= parser
.weakImportFromSymbol(sym
);
7317 toTarget
.addend
= contentValue
;
7318 useDirectBinding
= false;
7322 parser
.findTargetFromAddressAndSectionNum(contentValue
, nextReloc
->r_symbolnum(), toTarget
);
7323 useDirectBinding
= (toTarget
.atom
->scope() == ld::Atom::scopeTranslationUnit
);
7325 if ( useDirectBinding
)
7326 parser
.addFixup(src
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, toTarget
.atom
);
7328 parser
.addFixup(src
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, toTarget
.weakImport
, toTarget
.name
);
7329 parser
.addFixup(src
, ld::Fixup::k2of4
, ld::Fixup::kindAddAddend
, toTarget
.addend
);
7330 if ( target
.atom
== NULL
)
7331 parser
.addFixup(src
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractTargetAddress
, false, target
.name
);
7333 parser
.addFixup(src
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractTargetAddress
, target
.atom
);
7334 if ( reloc
->r_length() == 2 )
7335 parser
.addFixup(src
, ld::Fixup::k4of4
, ld::Fixup::kindStoreLittleEndian32
);
7337 parser
.addFixup(src
, ld::Fixup::k4of4
, ld::Fixup::kindStoreLittleEndian64
);
7339 case ARM64_RELOC_POINTER_TO_GOT
:
7340 if ( ! reloc
->r_extern() )
7341 throw "r_extern == 0 and ARM64_RELOC_POINTER_TO_GOT not supported";
7342 if ( prefixRelocAddend
!= 0 )
7343 throw "ARM64_RELOC_ADDEND followed by ARM64_RELOC_POINTER_TO_GOT not supported";
7344 if ( reloc
->r_pcrel() ) {
7345 if ( reloc
->r_length() != 2 )
7346 throw "r_length != 2 and r_extern = 1 and ARM64_RELOC_POINTER_TO_GOT not supported";
7347 parser
.addFixups(src
, ld::Fixup::kindStoreARM64PCRelToGOT
, target
);
7350 if ( reloc
->r_length() != 3 )
7351 throw "r_length != 3 and r_extern = 0 and ARM64_RELOC_POINTER_TO_GOT not supported";
7352 parser
.addFixups(src
, ld::Fixup::kindStoreARM64PointerToGOT
, target
);
7356 throwf("unknown relocation type %d", reloc
->r_type());
7362 template <typename A
>
7363 bool ObjC1ClassSection
<A
>::addRelocFixup(class Parser
<A
>& parser
, const macho_relocation_info
<P
>* reloc
)
7366 FixedSizeSection
<A
>::addRelocFixup(parser
, reloc
);
7368 assert(0 && "needs template specialization");
7373 bool ObjC1ClassSection
<x86
>::addRelocFixup(class Parser
<x86
>& parser
, const macho_relocation_info
<x86::P
>* reloc
)
7375 // if this is the reloc for the super class name string, add implicit reference to super class
7376 if ( ((reloc
->r_address() & R_SCATTERED
) == 0) && (reloc
->r_type() == GENERIC_RELOC_VANILLA
) ) {
7377 assert( reloc
->r_length() == 2 );
7378 assert( ! reloc
->r_pcrel() );
7380 const macho_section
<P
>* sect
= this->machoSection();
7381 Parser
<x86
>::SourceLocation src
;
7382 uint32_t srcAddr
= sect
->addr() + reloc
->r_address();
7383 src
.atom
= this->findAtomByAddress(srcAddr
);
7384 src
.offsetInAtom
= srcAddr
- src
.atom
->objectAddress();
7385 if ( src
.offsetInAtom
== 4 ) {
7386 Parser
<x86
>::TargetDesc stringTarget
;
7387 const uint8_t* fixUpPtr
= file().fileContent() + sect
->offset() + reloc
->r_address();
7388 uint32_t contentValue
= LittleEndian::get32(*((uint32_t*)fixUpPtr
));
7389 parser
.findTargetFromAddressAndSectionNum(contentValue
, reloc
->r_symbolnum(), stringTarget
);
7391 assert(stringTarget
.atom
!= NULL
);
7392 assert(stringTarget
.atom
->contentType() == ld::Atom::typeCString
);
7393 const char* superClassBaseName
= (char*)stringTarget
.atom
->rawContentPointer();
7394 char* superClassName
= new char[strlen(superClassBaseName
) + 20];
7395 strcpy(superClassName
, ".objc_class_name_");
7396 strcat(superClassName
, superClassBaseName
);
7398 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindSetTargetAddress
, false, superClassName
);
7402 return FixedSizeSection
<x86
>::addRelocFixup(parser
, reloc
);
7407 template <typename A
>
7408 bool Objc1ClassReferences
<A
>::addRelocFixup(class Parser
<A
>& parser
, const macho_relocation_info
<P
>* reloc
)
7411 PointerToCStringSection
<A
>::addRelocFixup(parser
, reloc
);
7413 assert(0 && "needs template specialization");
7420 bool Objc1ClassReferences
<x86
>::addRelocFixup(class Parser
<x86
>& parser
, const macho_relocation_info
<x86::P
>* reloc
)
7422 // add implict class refs, fixups not usable yet, so look at relocations
7423 assert( (reloc
->r_address() & R_SCATTERED
) == 0 );
7424 assert( reloc
->r_type() == GENERIC_RELOC_VANILLA
);
7425 assert( reloc
->r_length() == 2 );
7426 assert( ! reloc
->r_pcrel() );
7428 const macho_section
<P
>* sect
= this->machoSection();
7429 Parser
<x86
>::SourceLocation src
;
7430 uint32_t srcAddr
= sect
->addr() + reloc
->r_address();
7431 src
.atom
= this->findAtomByAddress(srcAddr
);
7432 src
.offsetInAtom
= srcAddr
- src
.atom
->objectAddress();
7433 Parser
<x86
>::TargetDesc stringTarget
;
7434 const uint8_t* fixUpPtr
= file().fileContent() + sect
->offset() + reloc
->r_address();
7435 uint32_t contentValue
= LittleEndian::get32(*((uint32_t*)fixUpPtr
));
7436 parser
.findTargetFromAddressAndSectionNum(contentValue
, reloc
->r_symbolnum(), stringTarget
);
7438 assert(stringTarget
.atom
!= NULL
);
7439 assert(stringTarget
.atom
->contentType() == ld::Atom::typeCString
);
7440 const char* baseClassName
= (char*)stringTarget
.atom
->rawContentPointer();
7441 char* objcClassName
= new char[strlen(baseClassName
) + 20];
7442 strcpy(objcClassName
, ".objc_class_name_");
7443 strcat(objcClassName
, baseClassName
);
7445 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindSetTargetAddress
, false, objcClassName
);
7448 return PointerToCStringSection
<x86
>::addRelocFixup(parser
, reloc
);
7451 #if SUPPORT_ARCH_arm64
7453 void Section
<arm64
>::addLOH(class Parser
<arm64
>& parser
, int kind
, int count
, const uint64_t addrs
[]) {
7455 case LOH_ARM64_ADRP_ADRP
:
7456 case LOH_ARM64_ADRP_LDR
:
7457 case LOH_ARM64_ADRP_ADD
:
7458 case LOH_ARM64_ADRP_LDR_GOT
:
7460 warning("arm64 Linker Optimiztion Hint %d has wrong number of arguments", kind
);
7462 case LOH_ARM64_ADRP_ADD_LDR
:
7463 case LOH_ARM64_ADRP_LDR_GOT_LDR
:
7464 case LOH_ARM64_ADRP_ADD_STR
:
7465 case LOH_ARM64_ADRP_LDR_GOT_STR
:
7467 warning("arm64 Linker Optimiztion Hint %d has wrong number of arguments", kind
);
7470 // pick lowest address in tuple for use as offsetInAtom
7471 uint64_t lowestAddress
= addrs
[0];
7472 for(int i
=1; i
< count
; ++i
) {
7473 if ( addrs
[i
] < lowestAddress
)
7474 lowestAddress
= addrs
[i
];
7476 // verify all other address are in same atom
7477 Atom
<arm64
>* inAtom
= parser
.findAtomByAddress(lowestAddress
);
7478 const uint64_t atomStartAddr
= inAtom
->objectAddress();
7479 const uint64_t atomEndAddr
= atomStartAddr
+ inAtom
->size();
7480 for(int i
=0; i
< count
; ++i
) {
7481 if ( (addrs
[i
] < atomStartAddr
) || (addrs
[i
] >= atomEndAddr
) ) {
7482 warning("arm64 Linker Optimiztion Hint addresses are not in same atom: 0x%08llX and 0x%08llX",
7483 lowestAddress
, addrs
[i
]);
7484 return; // skip this LOH
7486 if ( (addrs
[i
] & 0x3) != 0 ) {
7487 warning("arm64 Linker Optimiztion Hint address is not 4-byte aligned: 0x%08llX", addrs
[i
]);
7488 return; // skip this LOH
7490 if ( (addrs
[i
] - lowestAddress
) > 0xFFFF ) {
7491 if ( parser
.verboseOptimizationHints() ) {
7492 warning("arm64 Linker Optimiztion Hint addresses are too far apart: 0x%08llX and 0x%08llX",
7493 lowestAddress
, addrs
[i
]);
7495 return; // skip this LOH
7499 // encoded kind, count, and address deltas in 64-bit addend
7500 ld::Fixup::LOH_arm64 extra
;
7502 extra
.info
.kind
= kind
;
7503 extra
.info
.count
= count
-1;
7504 extra
.info
.delta1
= (addrs
[0] - lowestAddress
) >> 2;
7505 extra
.info
.delta2
= (count
> 1) ? ((addrs
[1] - lowestAddress
) >> 2) : 0;
7506 extra
.info
.delta3
= (count
> 2) ? ((addrs
[2] - lowestAddress
) >> 2) : 0;
7507 extra
.info
.delta4
= (count
> 3) ? ((addrs
[3] - lowestAddress
) >> 2) : 0;
7508 typename Parser
<arm64
>::SourceLocation
src(inAtom
, lowestAddress
- inAtom
->objectAddress());
7509 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindLinkerOptimizationHint
, extra
.addend
);
7513 template <typename A
>
7514 void Section
<A
>::addLOH(class Parser
<A
>& parser
, int kind
, int count
, const uint64_t addrs
[]) {
7518 template <typename A
>
7519 void Section
<A
>::makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
&)
7521 const macho_section
<P
>* sect
= this->machoSection();
7522 const macho_relocation_info
<P
>* relocs
= (macho_relocation_info
<P
>*)(file().fileContent() + sect
->reloff());
7523 const uint32_t relocCount
= sect
->nreloc();
7524 for (uint32_t r
= 0; r
< relocCount
; ++r
) {
7526 if ( this->addRelocFixup(parser
, &relocs
[r
]) )
7529 catch (const char* msg
) {
7530 throwf("in section %s,%s reloc %u: %s", sect
->segname(), Section
<A
>::makeSectionName(sect
), r
, msg
);
7534 // add follow-on fixups if .o file is missing .subsections_via_symbols
7535 if ( this->addFollowOnFixups() ) {
7536 Atom
<A
>* end
= &_endAtoms
[-1];
7537 for(Atom
<A
>* p
= _beginAtoms
; p
< end
; ++p
) {
7538 typename Parser
<A
>::SourceLocation
src(p
, 0);
7539 Atom
<A
>* nextAtom
= &p
[1];
7540 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindNoneFollowOn
, nextAtom
);
7543 else if ( this->type() == ld::Section::typeCode
) {
7544 // if FDE broke text not at a symbol, use followOn to keep code together
7545 Atom
<A
>* end
= &_endAtoms
[-1];
7546 for(Atom
<A
>* p
= _beginAtoms
; p
< end
; ++p
) {
7547 typename Parser
<A
>::SourceLocation
src(p
, 0);
7548 Atom
<A
>* nextAtom
= &p
[1];
7549 if ( (p
->symbolTableInclusion() == ld::Atom::symbolTableIn
) && (nextAtom
->symbolTableInclusion() == ld::Atom::symbolTableNotIn
) ) {
7550 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindNoneFollowOn
, nextAtom
);
7554 if ( !this->_altEntries
.empty() && !this->addFollowOnFixups() ) {
7555 if ( _altEntries
.count(_beginAtoms
) != 0 )
7556 warning("N_ALT_ENTRY bit set on first atom in section %s/%s", sect
->segname(), Section
<A
>::makeSectionName(sect
));
7558 Atom
<A
>* end
= &_endAtoms
[-1];
7559 for(Atom
<A
>* p
= _beginAtoms
; p
< end
; ++p
) {
7560 Atom
<A
>* nextAtom
= &p
[1];
7561 if ( _altEntries
.count(nextAtom
) != 0 ) {
7562 typename Parser
<A
>::SourceLocation
src(p
, 0);
7563 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindNoneFollowOn
, nextAtom
);
7564 typename Parser
<A
>::SourceLocation
src2(nextAtom
, 0);
7565 parser
.addFixup(src2
, ld::Fixup::k1of1
, ld::Fixup::kindNoneGroupSubordinate
, p
);
7570 // <rdar://problem/9218847> track data-in-code
7571 if ( parser
.hasDataInCodeLabels() && (this->type() == ld::Section::typeCode
) ) {
7572 for (uint32_t i
=0; i
< parser
.symbolCount(); ++i
) {
7573 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(i
);
7575 if ( (sym
.n_type() & N_STAB
) != 0 )
7577 // ignore non-definitions
7578 if ( (sym
.n_type() & N_TYPE
) != N_SECT
)
7581 // 'L' labels do not denote atom breaks
7582 const char* symbolName
= parser
.nameFromSymbol(sym
);
7583 if ( symbolName
[0] == 'L' ) {
7584 if ( strncmp(symbolName
, "L$start$", 8) == 0 ) {
7585 ld::Fixup::Kind kind
= ld::Fixup::kindNone
;
7586 if ( strncmp(&symbolName
[8], "data$", 5) == 0 )
7587 kind
= ld::Fixup::kindDataInCodeStartData
;
7588 else if ( strncmp(&symbolName
[8], "code$", 5) == 0 )
7589 kind
= ld::Fixup::kindDataInCodeEnd
;
7590 else if ( strncmp(&symbolName
[8], "jt8$", 4) == 0 )
7591 kind
= ld::Fixup::kindDataInCodeStartJT8
;
7592 else if ( strncmp(&symbolName
[8], "jt16$", 4) == 0 )
7593 kind
= ld::Fixup::kindDataInCodeStartJT16
;
7594 else if ( strncmp(&symbolName
[8], "jt32$", 4) == 0 )
7595 kind
= ld::Fixup::kindDataInCodeStartJT32
;
7596 else if ( strncmp(&symbolName
[8], "jta32$", 4) == 0 )
7597 kind
= ld::Fixup::kindDataInCodeStartJTA32
;
7599 warning("unknown L$start$ label %s in file %s", symbolName
, this->file().path());
7600 if ( kind
!= ld::Fixup::kindNone
) {
7601 Atom
<A
>* inAtom
= parser
.findAtomByAddress(sym
.n_value());
7602 typename Parser
<A
>::SourceLocation
src(inAtom
, sym
.n_value() - inAtom
->objectAddress());
7603 parser
.addFixup(src
, ld::Fixup::k1of1
, kind
);
7610 // <rdar://problem/11150575> Handle LC_DATA_IN_CODE in object files
7611 if ( this->type() == ld::Section::typeCode
) {
7612 const pint_t startAddr
= this->_machOSection
->addr();
7613 const pint_t endAddr
= startAddr
+ this->_machOSection
->size();
7614 for ( const macho_data_in_code_entry
<P
>* p
= parser
.dataInCodeStart(); p
!= parser
.dataInCodeEnd(); ++p
) {
7615 if ( (p
->offset() >= startAddr
) && (p
->offset() < endAddr
) ) {
7616 ld::Fixup::Kind kind
= ld::Fixup::kindNone
;
7617 switch ( p
->kind() ) {
7618 case DICE_KIND_DATA
:
7619 kind
= ld::Fixup::kindDataInCodeStartData
;
7621 case DICE_KIND_JUMP_TABLE8
:
7622 kind
= ld::Fixup::kindDataInCodeStartJT8
;
7624 case DICE_KIND_JUMP_TABLE16
:
7625 kind
= ld::Fixup::kindDataInCodeStartJT16
;
7627 case DICE_KIND_JUMP_TABLE32
:
7628 kind
= ld::Fixup::kindDataInCodeStartJT32
;
7630 case DICE_KIND_ABS_JUMP_TABLE32
:
7631 kind
= ld::Fixup::kindDataInCodeStartJTA32
;
7634 kind
= ld::Fixup::kindDataInCodeStartData
;
7635 warning("uknown LC_DATA_IN_CODE kind (%d) at offset 0x%08X", p
->kind(), p
->offset());
7638 Atom
<A
>* inAtom
= parser
.findAtomByAddress(p
->offset());
7639 typename Parser
<A
>::SourceLocation
srcStart(inAtom
, p
->offset() - inAtom
->objectAddress());
7640 parser
.addFixup(srcStart
, ld::Fixup::k1of1
, kind
);
7641 typename Parser
<A
>::SourceLocation
srcEnd(inAtom
, p
->offset() + p
->length() - inAtom
->objectAddress());
7642 parser
.addFixup(srcEnd
, ld::Fixup::k1of1
, ld::Fixup::kindDataInCodeEnd
);
7647 // <rdar://problem/11945700> convert linker optimization hints into internal format
7648 if ( this->type() == ld::Section::typeCode
&& parser
.hasOptimizationHints() ) {
7649 const pint_t startAddr
= this->_machOSection
->addr();
7650 const pint_t endAddr
= startAddr
+ this->_machOSection
->size();
7651 for (const uint8_t* p
= parser
.optimizationHintsStart(); p
< parser
.optimizationHintsEnd(); ) {
7653 int32_t kind
= read_uleb128(&p
, parser
.optimizationHintsEnd());
7654 if ( kind
== 0 ) // padding at end of loh buffer
7657 warning("malformed uleb128 kind in LC_LINKER_OPTIMIZATION_HINTS");
7660 int32_t count
= read_uleb128(&p
, parser
.optimizationHintsEnd());
7661 if ( count
== -1 ) {
7662 warning("malformed uleb128 count in LC_LINKER_OPTIMIZATION_HINTS");
7666 warning("address count > 3 in LC_LINKER_OPTIMIZATION_HINTS");
7669 for (int32_t i
=0; i
< count
; ++i
) {
7670 addrs
[i
] = read_uleb128(&p
, parser
.optimizationHintsEnd());
7672 if ( (startAddr
<= addrs
[0]) && (addrs
[0] < endAddr
) ) {
7673 this->addLOH(parser
, kind
, count
, addrs
);
7674 //fprintf(stderr, "kind=%d", kind);
7675 //for (int32_t i=0; i < count; ++i) {
7676 // fprintf(stderr, ", addr=0x%08llX", addrs[i]);
7678 //fprintf(stderr, "\n");
7684 // add follow-on fixups for aliases
7685 if ( _hasAliases
) {
7686 for(Atom
<A
>* p
= _beginAtoms
; p
< _endAtoms
; ++p
) {
7687 if ( p
->isAlias() && ! this->addFollowOnFixups() ) {
7688 Atom
<A
>* targetOfAlias
= &p
[1];
7689 assert(p
< &_endAtoms
[-1]);
7690 assert(p
->_objAddress
== targetOfAlias
->_objAddress
);
7691 typename Parser
<A
>::SourceLocation
src(p
, 0);
7692 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindNoneFollowOn
, targetOfAlias
);
7701 // main function used by linker to instantiate ld::Files
7703 ld::relocatable::File
* parse(const uint8_t* fileContent
, uint64_t fileLength
,
7704 const char* path
, time_t modTime
, ld::File::Ordinal ordinal
, const ParserOptions
& opts
)
7706 switch ( opts
.architecture
) {
7707 #if SUPPORT_ARCH_x86_64
7708 case CPU_TYPE_X86_64
:
7709 if ( mach_o::relocatable::Parser
<x86_64
>::validFile(fileContent
) )
7710 return mach_o::relocatable::Parser
<x86_64
>::parse(fileContent
, fileLength
, path
, modTime
, ordinal
, opts
);
7713 #if SUPPORT_ARCH_i386
7715 if ( mach_o::relocatable::Parser
<x86
>::validFile(fileContent
) )
7716 return mach_o::relocatable::Parser
<x86
>::parse(fileContent
, fileLength
, path
, modTime
, ordinal
, opts
);
7719 #if SUPPORT_ARCH_arm_any
7721 if ( mach_o::relocatable::Parser
<arm
>::validFile(fileContent
, opts
.objSubtypeMustMatch
, opts
.subType
) )
7722 return mach_o::relocatable::Parser
<arm
>::parse(fileContent
, fileLength
, path
, modTime
, ordinal
, opts
);
7725 #if SUPPORT_ARCH_arm64
7726 case CPU_TYPE_ARM64
:
7727 if ( mach_o::relocatable::Parser
<arm64
>::validFile(fileContent
, opts
.objSubtypeMustMatch
, opts
.subType
) )
7728 return mach_o::relocatable::Parser
<arm64
>::parse(fileContent
, fileLength
, path
, modTime
, ordinal
, opts
);
7736 // used by archive reader to validate member object file
7738 bool isObjectFile(const uint8_t* fileContent
, uint64_t fileLength
, const ParserOptions
& opts
)
7740 switch ( opts
.architecture
) {
7741 case CPU_TYPE_X86_64
:
7742 return ( mach_o::relocatable::Parser
<x86_64
>::validFile(fileContent
) );
7744 return ( mach_o::relocatable::Parser
<x86
>::validFile(fileContent
) );
7746 return ( mach_o::relocatable::Parser
<arm
>::validFile(fileContent
, opts
.objSubtypeMustMatch
, opts
.subType
) );
7747 case CPU_TYPE_ARM64
:
7748 return ( mach_o::relocatable::Parser
<arm64
>::validFile(fileContent
, opts
.objSubtypeMustMatch
, opts
.subType
) );
7754 // used by linker to infer architecture when no -arch is on command line
7756 bool isObjectFile(const uint8_t* fileContent
, cpu_type_t
* result
, cpu_subtype_t
* subResult
, Options::Platform
* platform
)
7758 if ( mach_o::relocatable::Parser
<x86_64
>::validFile(fileContent
) ) {
7759 *result
= CPU_TYPE_X86_64
;
7760 const macho_header
<Pointer64
<LittleEndian
> >* header
= (const macho_header
<Pointer64
<LittleEndian
> >*)fileContent
;
7761 *subResult
= header
->cpusubtype();
7762 *platform
= Parser
<x86_64
>::findPlatform(header
);
7765 if ( mach_o::relocatable::Parser
<x86
>::validFile(fileContent
) ) {
7766 const macho_header
<Pointer32
<LittleEndian
> >* header
= (const macho_header
<Pointer32
<LittleEndian
> >*)fileContent
;
7767 *result
= CPU_TYPE_I386
;
7768 *subResult
= CPU_SUBTYPE_X86_ALL
;
7769 *platform
= Parser
<x86
>::findPlatform(header
);
7772 if ( mach_o::relocatable::Parser
<arm
>::validFile(fileContent
, false, 0) ) {
7773 const macho_header
<Pointer32
<LittleEndian
> >* header
= (const macho_header
<Pointer32
<LittleEndian
> >*)fileContent
;
7774 *result
= CPU_TYPE_ARM
;
7775 *subResult
= header
->cpusubtype();
7776 *platform
= Parser
<arm
>::findPlatform(header
);
7779 if ( mach_o::relocatable::Parser
<arm64
>::validFile(fileContent
, false, 0) ) {
7780 const macho_header
<Pointer64
<LittleEndian
> >* header
= (const macho_header
<Pointer64
<LittleEndian
> >*)fileContent
;
7781 *result
= CPU_TYPE_ARM64
;
7782 *subResult
= CPU_SUBTYPE_ARM64_ALL
;
7783 *platform
= Parser
<arm64
>::findPlatform(header
);
7790 // used by linker is error messages to describe bad .o file
7792 const char* archName(const uint8_t* fileContent
)
7794 if ( mach_o::relocatable::Parser
<x86_64
>::validFile(fileContent
) ) {
7795 return mach_o::relocatable::Parser
<x86_64
>::fileKind(fileContent
);
7797 if ( mach_o::relocatable::Parser
<x86
>::validFile(fileContent
) ) {
7798 return mach_o::relocatable::Parser
<x86
>::fileKind(fileContent
);
7800 if ( mach_o::relocatable::Parser
<arm
>::validFile(fileContent
, false, 0) ) {
7801 return mach_o::relocatable::Parser
<arm
>::fileKind(fileContent
);
7807 // Used by archive reader when -ObjC option is specified
7809 bool hasObjC2Categories(const uint8_t* fileContent
)
7811 if ( mach_o::relocatable::Parser
<x86_64
>::validFile(fileContent
) ) {
7812 return mach_o::relocatable::Parser
<x86_64
>::hasObjC2Categories(fileContent
);
7814 else if ( mach_o::relocatable::Parser
<arm
>::validFile(fileContent
, false, 0) ) {
7815 return mach_o::relocatable::Parser
<arm
>::hasObjC2Categories(fileContent
);
7817 else if ( mach_o::relocatable::Parser
<x86
>::validFile(fileContent
, false, 0) ) {
7818 return mach_o::relocatable::Parser
<x86
>::hasObjC2Categories(fileContent
);
7820 #if SUPPORT_ARCH_arm64
7821 else if ( mach_o::relocatable::Parser
<arm64
>::validFile(fileContent
, false, 0) ) {
7822 return mach_o::relocatable::Parser
<arm64
>::hasObjC2Categories(fileContent
);
7829 // Used by archive reader when -ObjC option is specified
7831 bool hasObjC1Categories(const uint8_t* fileContent
)
7833 if ( mach_o::relocatable::Parser
<x86
>::validFile(fileContent
, false, 0) ) {
7834 return mach_o::relocatable::Parser
<x86
>::hasObjC1Categories(fileContent
);
7840 // Used by bitcode obfuscator to get a list of non local symbols from object file
7842 bool getNonLocalSymbols(const uint8_t* fileContent
, std::vector
<const char*> &syms
)
7844 if ( mach_o::relocatable::Parser
<x86_64
>::validFile(fileContent
) ) {
7845 return mach_o::relocatable::Parser
<x86_64
>::getNonLocalSymbols(fileContent
, syms
);
7847 else if ( mach_o::relocatable::Parser
<arm
>::validFile(fileContent
, false, 0) ) {
7848 return mach_o::relocatable::Parser
<arm
>::getNonLocalSymbols(fileContent
, syms
);
7850 else if ( mach_o::relocatable::Parser
<x86
>::validFile(fileContent
, false, 0) ) {
7851 return mach_o::relocatable::Parser
<x86
>::getNonLocalSymbols(fileContent
, syms
);
7853 else if ( mach_o::relocatable::Parser
<arm64
>::validFile(fileContent
, false, 0) ) {
7854 return mach_o::relocatable::Parser
<arm64
>::getNonLocalSymbols(fileContent
, syms
);
7861 } // namespace relocatable
7862 } // namespace mach_o