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"
47 #include "debugline.h"
49 #include "Architectures.hpp"
51 #include "macho_relocatable_file.h"
55 extern void throwf(const char* format
, ...) __attribute__ ((noreturn
,format(printf
, 1, 2)));
56 extern void warning(const char* format
, ...) __attribute__((format(printf
, 1, 2)));
59 namespace relocatable
{
63 template <typename A
> class Parser
;
64 template <typename A
> class Atom
;
65 template <typename A
> class Section
;
66 template <typename A
> class CFISection
;
67 template <typename A
> class CUSection
;
70 class File
: public ld::relocatable::File
73 File(const char* p
, time_t mTime
, const uint8_t* content
, ld::File::Ordinal ord
) :
74 ld::relocatable::File(p
,mTime
,ord
), _fileContent(content
),
75 _sectionsArray(NULL
), _atomsArray(NULL
),
76 _sectionsArrayCount(0), _atomsArrayCount(0),
77 _debugInfoKind(ld::relocatable::File::kDebugInfoNone
),
78 _dwarfTranslationUnitPath(NULL
),
79 _dwarfDebugInfoSect(NULL
), _dwarfDebugAbbrevSect(NULL
),
80 _dwarfDebugLineSect(NULL
), _dwarfDebugStringSect(NULL
),
81 _objConstraint(ld::File::objcConstraintNone
),
83 _canScatterAtoms(false) {}
86 // overrides of ld::File
87 virtual bool forEachAtom(ld::File::AtomHandler
&) const;
88 virtual bool justInTimeforEachAtom(const char* name
, ld::File::AtomHandler
&) const
91 // overrides of ld::relocatable::File
92 virtual ObjcConstraint
objCConstraint() const { return _objConstraint
; }
93 virtual uint32_t cpuSubType() const { return _cpuSubType
; }
94 virtual DebugInfoKind
debugInfo() const { return _debugInfoKind
; }
95 virtual const std::vector
<ld::relocatable::File::Stab
>* stabs() const { return &_stabs
; }
96 virtual bool canScatterAtoms() const { return _canScatterAtoms
; }
97 virtual const char* translationUnitSource() const;
99 const uint8_t* fileContent() { return _fileContent
; }
101 friend class Atom
<A
>;
102 friend class Section
<A
>;
103 friend class Parser
<A
>;
104 friend class CFISection
<A
>::OAS
;
106 typedef typename
A::P P
;
108 const uint8_t* _fileContent
;
109 Section
<A
>** _sectionsArray
;
110 uint8_t* _atomsArray
;
111 uint32_t _sectionsArrayCount
;
112 uint32_t _atomsArrayCount
;
113 std::vector
<ld::Fixup
> _fixups
;
114 std::vector
<ld::Atom::UnwindInfo
> _unwindInfos
;
115 std::vector
<ld::Atom::LineInfo
> _lineInfos
;
116 std::vector
<ld::relocatable::File::Stab
>_stabs
;
117 ld::relocatable::File::DebugInfoKind _debugInfoKind
;
118 const char* _dwarfTranslationUnitPath
;
119 const macho_section
<P
>* _dwarfDebugInfoSect
;
120 const macho_section
<P
>* _dwarfDebugAbbrevSect
;
121 const macho_section
<P
>* _dwarfDebugLineSect
;
122 const macho_section
<P
>* _dwarfDebugStringSect
;
123 ld::File::ObjcConstraint _objConstraint
;
124 uint32_t _cpuSubType
;
125 bool _canScatterAtoms
;
129 template <typename A
>
130 class Section
: public ld::Section
133 typedef typename
A::P::uint_t pint_t
;
134 typedef typename
A::P P
;
135 typedef typename
A::P::E E
;
137 virtual ~Section() { }
138 class File
<A
>& file() const { return _file
; }
139 const macho_section
<P
>* machoSection() const { return _machOSection
; }
140 uint32_t sectionNum(class Parser
<A
>&) const;
141 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
);
142 virtual ld::Atom::ContentType
contentType() { return ld::Atom::typeUnclassified
; }
143 virtual bool dontDeadStrip() { return (this->_machOSection
->flags() & S_ATTR_NO_DEAD_STRIP
); }
144 virtual Atom
<A
>* findAtomByAddress(pint_t addr
) { return this->findContentAtomByAddress(addr
, this->_beginAtoms
, this->_endAtoms
); }
145 virtual bool addFollowOnFixups() const { return ! _file
.canScatterAtoms(); }
146 virtual uint32_t appendAtoms(class Parser
<A
>& parser
, uint8_t* buffer
,
147 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
148 const struct Parser
<A
>::CFI_CU_InfoArrays
&) = 0;
149 virtual uint32_t computeAtomCount(class Parser
<A
>& parser
,
150 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
151 const struct Parser
<A
>::CFI_CU_InfoArrays
&) = 0;
152 virtual void makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
&);
153 virtual bool addRelocFixup(class Parser
<A
>& parser
, const macho_relocation_info
<P
>*);
154 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const { return 0; }
155 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
156 const ld::IndirectBindingTable
& ind
) const { return false; }
157 static const char* makeSectionName(const macho_section
<typename
A::P
>* s
);
160 Section(File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
161 : ld::Section(makeSegmentName(s
), makeSectionName(s
), sectionType(s
)),
162 _file(f
), _machOSection(s
), _beginAtoms(NULL
), _endAtoms(NULL
), _hasAliases(false) { }
163 Section(File
<A
>& f
, const char* segName
, const char* sectName
, ld::Section::Type t
, bool hidden
=false)
164 : ld::Section(segName
, sectName
, t
, hidden
), _file(f
), _machOSection(NULL
),
165 _beginAtoms(NULL
), _endAtoms(NULL
), _hasAliases(false) { }
168 Atom
<A
>* findContentAtomByAddress(pint_t addr
, class Atom
<A
>* start
, class Atom
<A
>* end
);
169 uint32_t x86_64PcRelOffset(uint8_t r_type
);
170 static const char* makeSegmentName(const macho_section
<typename
A::P
>* s
);
171 static bool readable(const macho_section
<typename
A::P
>* s
);
172 static bool writable(const macho_section
<typename
A::P
>* s
);
173 static bool exectuable(const macho_section
<typename
A::P
>* s
);
174 static ld::Section::Type
sectionType(const macho_section
<typename
A::P
>* s
);
177 const macho_section
<P
>* _machOSection
;
178 class Atom
<A
>* _beginAtoms
;
179 class Atom
<A
>* _endAtoms
;
184 template <typename A
>
185 class CFISection
: public Section
<A
>
188 CFISection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
189 : Section
<A
>(f
, s
) { }
192 virtual ld::Atom::ContentType
contentType() { return ld::Atom::typeCFI
; }
193 virtual uint32_t computeAtomCount(class Parser
<A
>& parser
, struct Parser
<A
>::LabelAndCFIBreakIterator
& it
, const struct Parser
<A
>::CFI_CU_InfoArrays
&);
194 virtual uint32_t appendAtoms(class Parser
<A
>& parser
, uint8_t* buffer
, struct Parser
<A
>::LabelAndCFIBreakIterator
& it
, const struct Parser
<A
>::CFI_CU_InfoArrays
&);
195 virtual void makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
&);
196 virtual bool addFollowOnFixups() const { return false; }
200 /// ObjectFileAddressSpace is used as a template parameter to UnwindCursor for parsing
201 /// dwarf CFI information in an object file.
206 typedef typename
A::P::uint_t pint_t
;
207 typedef typename
A::P P
;
208 typedef typename
A::P::E E
;
209 typedef typename
A::P::uint_t sint_t
;
211 OAS(CFISection
<A
>& ehFrameSection
, const uint8_t* ehFrameBuffer
) :
212 _ehFrameSection(ehFrameSection
),
213 _ehFrameContent(ehFrameBuffer
),
214 _ehFrameStartAddr(ehFrameSection
.machoSection()->addr()),
215 _ehFrameEndAddr(ehFrameSection
.machoSection()->addr()+ehFrameSection
.machoSection()->size()) {}
217 uint8_t get8(pint_t addr
) { return *((uint8_t*)mappedAddress(addr
)); }
218 uint16_t get16(pint_t addr
) { return E::get16(*((uint16_t*)mappedAddress(addr
))); }
219 uint32_t get32(pint_t addr
) { return E::get32(*((uint32_t*)mappedAddress(addr
))); }
220 uint64_t get64(pint_t addr
) { return E::get64(*((uint64_t*)mappedAddress(addr
))); }
221 pint_t
getP(pint_t addr
) { return P::getP(*((pint_t
*)mappedAddress(addr
))); }
222 uint64_t getULEB128(pint_t
& addr
, pint_t end
);
223 int64_t getSLEB128(pint_t
& addr
, pint_t end
);
224 pint_t
getEncodedP(pint_t
& addr
, pint_t end
, uint8_t encoding
);
226 const void* mappedAddress(pint_t addr
);
228 CFISection
<A
>& _ehFrameSection
;
229 const uint8_t* _ehFrameContent
;
230 pint_t _ehFrameStartAddr
;
231 pint_t _ehFrameEndAddr
;
235 typedef typename
A::P::uint_t pint_t
;
236 typedef libunwind::CFI_Atom_Info
<OAS
> CFI_Atom_Info
;
238 void cfiParse(class Parser
<A
>& parser
, uint8_t* buffer
, CFI_Atom_Info cfiArray
[], uint32_t cfiCount
);
239 bool needsRelocating();
241 static bool bigEndian();
243 void addCiePersonalityFixups(class Parser
<A
>& parser
, const CFI_Atom_Info
* cieInfo
);
244 static void warnFunc(void* ref
, uint64_t funcAddr
, const char* msg
);
248 template <typename A
>
249 class CUSection
: public Section
<A
>
252 CUSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
253 : Section
<A
>(f
, s
) { }
255 typedef typename
A::P::uint_t pint_t
;
256 typedef typename
A::P P
;
257 typedef typename
A::P::E E
;
259 virtual uint32_t computeAtomCount(class Parser
<A
>& parser
, struct Parser
<A
>::LabelAndCFIBreakIterator
& it
, const struct Parser
<A
>::CFI_CU_InfoArrays
&) { return 0; }
260 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; }
261 virtual void makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
&);
262 virtual bool addFollowOnFixups() const { return false; }
265 pint_t functionStartAddress
;
266 uint32_t functionSymbolIndex
;
267 uint32_t rangeLength
;
268 uint32_t compactUnwindInfo
;
269 const char* personality
;
276 void parse(class Parser
<A
>& parser
, uint32_t cnt
, Info array
[]);
281 const char* personalityName(class Parser
<A
>& parser
, const macho_relocation_info
<P
>* reloc
);
283 static int infoSorter(const void* l
, const void* r
);
288 template <typename A
>
289 class TentativeDefinitionSection
: public Section
<A
>
292 TentativeDefinitionSection(Parser
<A
>& parser
, File
<A
>& f
)
293 : Section
<A
>(f
, "__DATA", "__comm/tent", ld::Section::typeTentativeDefs
) {}
295 virtual ld::Atom::ContentType
contentType() { return ld::Atom::typeZeroFill
; }
296 virtual bool addFollowOnFixups() const { return false; }
297 virtual Atom
<A
>* findAtomByAddress(typename
A::P::uint_t addr
) { throw "TentativeDefinitionSection::findAtomByAddress() should never be called"; }
298 virtual uint32_t computeAtomCount(class Parser
<A
>& parser
, struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
299 const struct Parser
<A
>::CFI_CU_InfoArrays
&);
300 virtual uint32_t appendAtoms(class Parser
<A
>& parser
, uint8_t* buffer
,
301 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
302 const struct Parser
<A
>::CFI_CU_InfoArrays
&);
303 virtual void makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
&) {}
305 typedef typename
A::P::uint_t pint_t
;
306 typedef typename
A::P P
;
310 template <typename A
>
311 class AbsoluteSymbolSection
: public Section
<A
>
314 AbsoluteSymbolSection(Parser
<A
>& parser
, File
<A
>& f
)
315 : Section
<A
>(f
, "__DATA", "__abs", ld::Section::typeAbsoluteSymbols
, true) {}
317 virtual ld::Atom::ContentType
contentType() { return ld::Atom::typeUnclassified
; }
318 virtual bool dontDeadStrip() { return false; }
319 virtual ld::Atom::Alignment
alignmentForAddress(typename
A::P::uint_t addr
) { return ld::Atom::Alignment(0); }
320 virtual bool addFollowOnFixups() const { return false; }
321 virtual Atom
<A
>* findAtomByAddress(typename
A::P::uint_t addr
) { throw "AbsoluteSymbolSection::findAtomByAddress() should never be called"; }
322 virtual uint32_t computeAtomCount(class Parser
<A
>& parser
, struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
323 const struct Parser
<A
>::CFI_CU_InfoArrays
&);
324 virtual uint32_t appendAtoms(class Parser
<A
>& parser
, uint8_t* buffer
,
325 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
326 const struct Parser
<A
>::CFI_CU_InfoArrays
&);
327 virtual void makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
&) {}
328 virtual Atom
<A
>* findAbsAtomForValue(typename
A::P::uint_t
);
331 typedef typename
A::P::uint_t pint_t
;
332 typedef typename
A::P P
;
336 template <typename A
>
337 class SymboledSection
: public Section
<A
>
340 SymboledSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
);
341 virtual ld::Atom::ContentType
contentType() { return _type
; }
342 virtual bool dontDeadStrip();
343 virtual uint32_t computeAtomCount(class Parser
<A
>& parser
, struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
344 const struct Parser
<A
>::CFI_CU_InfoArrays
&);
345 virtual uint32_t appendAtoms(class Parser
<A
>& parser
, uint8_t* buffer
,
346 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
347 const struct Parser
<A
>::CFI_CU_InfoArrays
&);
349 typedef typename
A::P::uint_t pint_t
;
350 typedef typename
A::P P
;
352 ld::Atom::ContentType _type
;
356 template <typename A
>
357 class TLVDefsSection
: public SymboledSection
<A
>
360 TLVDefsSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
) :
361 SymboledSection
<A
>(parser
, f
, s
) { }
368 template <typename A
>
369 class ImplicitSizeSection
: public Section
<A
>
372 ImplicitSizeSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
373 : Section
<A
>(f
, s
) { }
374 virtual uint32_t computeAtomCount(class Parser
<A
>& parser
, struct Parser
<A
>::LabelAndCFIBreakIterator
& it
, const struct Parser
<A
>::CFI_CU_InfoArrays
&);
375 virtual uint32_t appendAtoms(class Parser
<A
>& parser
, uint8_t* buffer
, struct Parser
<A
>::LabelAndCFIBreakIterator
& it
, const struct Parser
<A
>::CFI_CU_InfoArrays
&);
377 typedef typename
A::P::uint_t pint_t
;
378 typedef typename
A::P P
;
380 virtual bool addFollowOnFixups() const { return false; }
381 virtual const char* unlabeledAtomName(Parser
<A
>& parser
, pint_t addr
) = 0;
382 virtual ld::Atom::SymbolTableInclusion
symbolTableInclusion() { return ld::Atom::symbolTableNotIn
; }
383 virtual pint_t
elementSizeAtAddress(pint_t addr
) = 0;
384 virtual ld::Atom::Scope
scopeAtAddress(Parser
<A
>& parser
, pint_t addr
) { return ld::Atom::scopeLinkageUnit
; }
385 virtual bool useElementAt(Parser
<A
>& parser
,
386 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
, pint_t addr
) = 0;
387 virtual ld::Atom::Definition
definition() { return ld::Atom::definitionRegular
; }
388 virtual ld::Atom::Combine
combine(Parser
<A
>& parser
, pint_t addr
) = 0;
389 virtual bool ignoreLabel(const char* label
) { return (label
[0] == 'L'); }
392 template <typename A
>
393 class FixedSizeSection
: public ImplicitSizeSection
<A
>
396 FixedSizeSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
397 : ImplicitSizeSection
<A
>(parser
, f
, s
) { }
399 typedef typename
A::P::uint_t pint_t
;
400 typedef typename
A::P P
;
401 typedef typename
A::P::E E
;
403 virtual bool useElementAt(Parser
<A
>& parser
,
404 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
, pint_t addr
)
409 template <typename A
>
410 class Literal4Section
: public FixedSizeSection
<A
>
413 Literal4Section(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
414 : FixedSizeSection
<A
>(parser
, f
, s
) {}
416 typedef typename
A::P::uint_t pint_t
;
417 typedef typename
A::P P
;
419 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
) { return ld::Atom::Alignment(2); }
420 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "4-byte-literal"; }
421 virtual pint_t
elementSizeAtAddress(pint_t addr
) { return 4; }
422 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
) { return ld::Atom::combineByNameAndContent
; }
423 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
424 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
425 const ld::IndirectBindingTable
& ind
) const;
428 template <typename A
>
429 class Literal8Section
: public FixedSizeSection
<A
>
432 Literal8Section(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
433 : FixedSizeSection
<A
>(parser
, f
, s
) {}
435 typedef typename
A::P::uint_t pint_t
;
436 typedef typename
A::P P
;
438 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
) { return ld::Atom::Alignment(3); }
439 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "8-byte-literal"; }
440 virtual pint_t
elementSizeAtAddress(pint_t addr
) { return 8; }
441 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
) { return ld::Atom::combineByNameAndContent
; }
442 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
443 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
444 const ld::IndirectBindingTable
& ind
) const;
447 template <typename A
>
448 class Literal16Section
: public FixedSizeSection
<A
>
451 Literal16Section(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
452 : FixedSizeSection
<A
>(parser
, f
, s
) {}
454 typedef typename
A::P::uint_t pint_t
;
455 typedef typename
A::P P
;
457 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
) { return ld::Atom::Alignment(4); }
458 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "16-byte-literal"; }
459 virtual pint_t
elementSizeAtAddress(pint_t addr
) { return 16; }
460 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
) { return ld::Atom::combineByNameAndContent
; }
461 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
462 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
463 const ld::IndirectBindingTable
& ind
) const;
467 template <typename A
>
468 class NonLazyPointerSection
: public FixedSizeSection
<A
>
471 NonLazyPointerSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
472 : FixedSizeSection
<A
>(parser
, f
, s
) {}
474 typedef typename
A::P::uint_t pint_t
;
475 typedef typename
A::P P
;
477 virtual void makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
&);
478 virtual ld::Atom::ContentType
contentType() { return ld::Atom::typeNonLazyPointer
; }
479 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
) { return ld::Atom::Alignment(log2(sizeof(pint_t
))); }
480 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "non_lazy_ptr"; }
481 virtual pint_t
elementSizeAtAddress(pint_t addr
) { return sizeof(pint_t
); }
482 virtual ld::Atom::Scope
scopeAtAddress(Parser
<A
>& parser
, pint_t addr
);
483 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
);
484 virtual bool ignoreLabel(const char* label
) { return true; }
485 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
486 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
487 const ld::IndirectBindingTable
& ind
) const;
490 static const char* targetName(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
);
491 static ld::Fixup::Kind
fixupKind();
495 template <typename A
>
496 class CFStringSection
: public FixedSizeSection
<A
>
499 CFStringSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
500 : FixedSizeSection
<A
>(parser
, f
, s
) {}
502 typedef typename
A::P::uint_t pint_t
;
504 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
) { return ld::Atom::Alignment(log2(sizeof(pint_t
))); }
505 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "CFString"; }
506 virtual pint_t
elementSizeAtAddress(pint_t addr
) { return 4*sizeof(pint_t
); }
507 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
) { return ld::Atom::combineByNameAndReferences
; }
508 virtual bool ignoreLabel(const char* label
) { return true; }
509 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
510 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
511 const ld::IndirectBindingTable
& ind
) const;
513 enum ContentType
{ contentUTF8
, contentUTF16
, contentUnknown
};
514 static const uint8_t* targetContent(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
,
515 ContentType
* ct
, unsigned int* count
);
519 template <typename A
>
520 class ObjC1ClassSection
: public FixedSizeSection
<A
>
523 ObjC1ClassSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
524 : FixedSizeSection
<A
>(parser
, f
, s
) {}
526 typedef typename
A::P::uint_t pint_t
;
527 typedef typename
A::P P
;
528 typedef typename
A::P::E E
;
530 virtual ld::Atom::Scope
scopeAtAddress(Parser
<A
>& , pint_t
) { return ld::Atom::scopeGlobal
; }
531 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
) { return ld::Atom::Alignment(2); }
532 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
);
533 virtual ld::Atom::SymbolTableInclusion
symbolTableInclusion() { return ld::Atom::symbolTableIn
; }
534 virtual pint_t
elementSizeAtAddress(pint_t addr
);
535 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
) { return ld::Atom::combineNever
; }
536 virtual bool ignoreLabel(const char* label
) { return true; }
537 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
539 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
540 const ld::IndirectBindingTable
& ind
) const { return false; }
541 virtual bool addRelocFixup(class Parser
<A
>& parser
, const macho_relocation_info
<P
>*);
545 template <typename A
>
546 class ObjC2ClassRefsSection
: public FixedSizeSection
<A
>
549 ObjC2ClassRefsSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
550 : FixedSizeSection
<A
>(parser
, f
, s
) {}
552 typedef typename
A::P::uint_t pint_t
;
554 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
) { return ld::Atom::Alignment(log2(sizeof(pint_t
))); }
555 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "objc-class-ref"; }
556 virtual pint_t
elementSizeAtAddress(pint_t addr
) { return sizeof(pint_t
); }
557 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
) { return ld::Atom::combineByNameAndReferences
; }
558 virtual bool ignoreLabel(const char* label
) { return true; }
559 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
560 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
561 const ld::IndirectBindingTable
& ind
) const;
563 const char* targetClassName(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
567 template <typename A
>
568 class ObjC2CategoryListSection
: public FixedSizeSection
<A
>
571 ObjC2CategoryListSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
572 : FixedSizeSection
<A
>(parser
, f
, s
) {}
574 typedef typename
A::P::uint_t pint_t
;
576 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
) { return ld::Atom::Alignment(log2(sizeof(pint_t
))); }
577 virtual ld::Atom::Scope
scopeAtAddress(Parser
<A
>& parser
, pint_t addr
) { return ld::Atom::scopeTranslationUnit
; }
578 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "objc-cat-list"; }
579 virtual pint_t
elementSizeAtAddress(pint_t addr
) { return sizeof(pint_t
); }
580 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
) { return ld::Atom::combineNever
; }
581 virtual bool ignoreLabel(const char* label
) { return true; }
583 const char* targetClassName(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
587 template <typename A
>
588 class PointerToCStringSection
: public FixedSizeSection
<A
>
591 PointerToCStringSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
592 : FixedSizeSection
<A
>(parser
, f
, s
) {}
594 typedef typename
A::P::uint_t pint_t
;
596 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
) { return ld::Atom::Alignment(log2(sizeof(pint_t
))); }
597 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "pointer-to-literal-cstring"; }
598 virtual pint_t
elementSizeAtAddress(pint_t addr
) { return sizeof(pint_t
); }
599 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
) { return ld::Atom::combineByNameAndReferences
; }
600 virtual bool ignoreLabel(const char* label
) { return true; }
601 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
602 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
603 const ld::IndirectBindingTable
& ind
) const;
604 virtual const char* targetCString(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
608 template <typename A
>
609 class Objc1ClassReferences
: public PointerToCStringSection
<A
>
612 Objc1ClassReferences(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
613 : PointerToCStringSection
<A
>(parser
, f
, s
) {}
615 typedef typename
A::P::uint_t pint_t
;
616 typedef typename
A::P P
;
618 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "pointer-to-literal-objc-class-name"; }
619 virtual bool addRelocFixup(class Parser
<A
>& parser
, const macho_relocation_info
<P
>*);
620 virtual const char* targetCString(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
624 template <typename A
>
625 class CStringSection
: public ImplicitSizeSection
<A
>
628 CStringSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
629 : ImplicitSizeSection
<A
>(parser
, f
, s
) {}
631 typedef typename
A::P::uint_t pint_t
;
632 typedef typename
A::P P
;
634 virtual ld::Atom::ContentType
contentType() { return ld::Atom::typeCString
; }
635 virtual Atom
<A
>* findAtomByAddress(pint_t addr
);
636 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "cstring"; }
637 virtual pint_t
elementSizeAtAddress(pint_t addr
);
638 virtual bool ignoreLabel(const char* label
);
639 virtual bool useElementAt(Parser
<A
>& parser
,
640 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
, pint_t addr
);
641 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
) { return ld::Atom::combineByNameAndContent
; }
642 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
643 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
644 const ld::IndirectBindingTable
& ind
) const;
649 template <typename A
>
650 class UTF16StringSection
: public SymboledSection
<A
>
653 UTF16StringSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
654 : SymboledSection
<A
>(parser
, f
, s
) {}
656 typedef typename
A::P::uint_t pint_t
;
657 typedef typename
A::P P
;
659 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
) { return ld::Atom::combineByNameAndContent
; }
660 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
661 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
662 const ld::IndirectBindingTable
& ind
) const;
667 // Atoms in mach-o files
669 template <typename A
>
670 class Atom
: public ld::Atom
673 // overrides of ld::Atom
674 virtual ld::File
* file() const { return §().file(); }
675 virtual const char* translationUnitSource() const
676 { return sect().file().translationUnitSource(); }
677 virtual const char* name() const { return _name
; }
678 virtual uint64_t size() const { return _size
; }
679 virtual uint64_t objectAddress() const { return _objAddress
; }
680 virtual void copyRawContent(uint8_t buffer
[]) const;
681 virtual const uint8_t* rawContentPointer() const { return contentPointer(); }
682 virtual unsigned long contentHash(const ld::IndirectBindingTable
& ind
) const
683 { if ( _hash
== 0 ) _hash
= sect().contentHash(this, ind
); return _hash
; }
684 virtual bool canCoalesceWith(const ld::Atom
& rhs
, const ld::IndirectBindingTable
& ind
) const
685 { return sect().canCoalesceWith(this, rhs
, ind
); }
686 virtual ld::Fixup::iterator
fixupsBegin() const { return &machofile()._fixups
[_fixupsStartIndex
]; }
687 virtual ld::Fixup::iterator
fixupsEnd() const { return &machofile()._fixups
[_fixupsStartIndex
+_fixupsCount
]; }
688 virtual ld::Atom::UnwindInfo::iterator
beginUnwind() const { return &machofile()._unwindInfos
[_unwindInfoStartIndex
]; }
689 virtual ld::Atom::UnwindInfo::iterator
endUnwind() const { return &machofile()._unwindInfos
[_unwindInfoStartIndex
+_unwindInfoCount
]; }
690 virtual ld::Atom::LineInfo::iterator
beginLineInfo() const{ return &machofile()._lineInfos
[_lineInfoStartIndex
]; }
691 virtual ld::Atom::LineInfo::iterator
endLineInfo() const { return &machofile()._lineInfos
[_lineInfoStartIndex
+_lineInfoCount
]; }
695 enum { kFixupStartIndexBits
= 32,
696 kLineInfoStartIndexBits
= 32,
697 kUnwindInfoStartIndexBits
= 24,
698 kFixupCountBits
= 24,
699 kLineInfoCountBits
= 12,
700 kUnwindInfoCountBits
= 4
701 }; // must sum to 128
704 // methods for all atoms from mach-o object file
705 Section
<A
>& sect() const { return (Section
<A
>&)section(); }
706 File
<A
>& machofile() const { return ((Section
<A
>*)(this->_section
))->file(); }
707 void setFixupsRange(uint32_t s
, uint32_t c
);
708 void setUnwindInfoRange(uint32_t s
, uint32_t c
);
709 void extendUnwindInfoRange();
710 void setLineInfoRange(uint32_t s
, uint32_t c
);
711 bool roomForMoreLineInfoCount() { return (_lineInfoCount
< ((1<<kLineInfoCountBits
)-1)); }
712 void incrementLineInfoCount() { assert(roomForMoreLineInfoCount()); ++_lineInfoCount
; }
713 void incrementFixupCount() { if (_fixupsCount
== ((1 << kFixupCountBits
)-1))
714 throwf("too may fixups in %s", name()); ++_fixupsCount
; }
715 const uint8_t* contentPointer() const;
716 uint32_t fixupCount() const { return _fixupsCount
; }
717 void verifyAlignment() const;
719 typedef typename
A::P P
;
720 typedef typename
A::P::E E
;
721 typedef typename
A::P::uint_t pint_t
;
722 // constuct via all attributes
723 Atom(Section
<A
>& sct
, const char* nm
, pint_t addr
, uint64_t sz
,
724 ld::Atom::Definition d
, ld::Atom::Combine c
, ld::Atom::Scope s
,
725 ld::Atom::ContentType ct
, ld::Atom::SymbolTableInclusion i
,
726 bool dds
, bool thumb
, bool al
, ld::Atom::Alignment a
)
727 : ld::Atom((ld::Section
&)sct
, d
, c
, s
, ct
, i
, dds
, thumb
, al
, a
),
728 _size(sz
), _objAddress(addr
), _name(nm
), _hash(0),
729 _fixupsStartIndex(0), _lineInfoStartIndex(0),
730 _unwindInfoStartIndex(0), _fixupsCount(0),
731 _lineInfoCount(0), _unwindInfoCount(0) { }
732 // construct via symbol table entry
733 Atom(Section
<A
>& sct
, Parser
<A
>& parser
, const macho_nlist
<P
>& sym
,
734 uint64_t sz
, bool alias
=false)
735 : ld::Atom((ld::Section
&)sct
, parser
.definitionFromSymbol(sym
),
736 parser
.combineFromSymbol(sym
), parser
.scopeFromSymbol(sym
),
737 parser
.resolverFromSymbol(sym
) ? ld::Atom::typeResolver
: sct
.contentType(),
738 parser
.inclusionFromSymbol(sym
),
739 parser
.dontDeadStripFromSymbol(sym
) || sct
.dontDeadStrip(),
740 parser
.isThumbFromSymbol(sym
), alias
,
741 sct
.alignmentForAddress(sym
.n_value())),
742 _size(sz
), _objAddress(sym
.n_value()),
743 _name(parser
.nameFromSymbol(sym
)), _hash(0),
744 _fixupsStartIndex(0), _lineInfoStartIndex(0),
745 _unwindInfoStartIndex(0), _fixupsCount(0),
746 _lineInfoCount(0), _unwindInfoCount(0) {
747 // <rdar://problem/6783167> support auto-hidden weak symbols
748 if ( _scope
== ld::Atom::scopeGlobal
&&
749 (sym
.n_desc() & (N_WEAK_DEF
|N_WEAK_REF
)) == (N_WEAK_DEF
|N_WEAK_REF
) )
751 this->verifyAlignment();
755 friend class Parser
<A
>;
756 friend class Section
<A
>;
757 friend class CStringSection
<A
>;
758 friend class AbsoluteSymbolSection
<A
>;
763 mutable unsigned long _hash
;
765 uint64_t _fixupsStartIndex
: kFixupStartIndexBits
,
766 _lineInfoStartIndex
: kLineInfoStartIndexBits
,
767 _unwindInfoStartIndex
: kUnwindInfoStartIndexBits
,
768 _fixupsCount
: kFixupCountBits
,
769 _lineInfoCount
: kLineInfoCountBits
,
770 _unwindInfoCount
: kUnwindInfoCountBits
;
776 template <typename A
>
777 void Atom
<A
>::setFixupsRange(uint32_t startIndex
, uint32_t count
)
779 if ( count
>= (1 << kFixupCountBits
) )
780 throwf("too many fixups in function %s", this->name());
781 if ( startIndex
>= (1 << kFixupStartIndexBits
) )
782 throwf("too many fixups in file");
783 assert(((startIndex
+count
) <= sect().file()._fixups
.size()) && "fixup index out of range");
784 _fixupsStartIndex
= startIndex
;
785 _fixupsCount
= count
;
788 template <typename A
>
789 void Atom
<A
>::setUnwindInfoRange(uint32_t startIndex
, uint32_t count
)
791 if ( count
>= (1 << kUnwindInfoCountBits
) )
792 throwf("too many compact unwind infos in function %s", this->name());
793 if ( startIndex
>= (1 << kUnwindInfoStartIndexBits
) )
794 throwf("too many compact unwind infos (%d) in file", startIndex
);
795 assert((startIndex
+count
) <= sect().file()._unwindInfos
.size() && "unwindinfo index out of range");
796 _unwindInfoStartIndex
= startIndex
;
797 _unwindInfoCount
= count
;
800 template <typename A
>
801 void Atom
<A
>::extendUnwindInfoRange()
803 if ( _unwindInfoCount
+1 >= (1 << kUnwindInfoCountBits
) )
804 throwf("too many compact unwind infos in function %s", this->name());
805 _unwindInfoCount
+= 1;
808 template <typename A
>
809 void Atom
<A
>::setLineInfoRange(uint32_t startIndex
, uint32_t count
)
811 assert((count
< (1 << kLineInfoCountBits
)) && "too many line infos");
812 assert((startIndex
+count
) < sect().file()._lineInfos
.size() && "line info index out of range");
813 _lineInfoStartIndex
= startIndex
;
814 _lineInfoCount
= count
;
817 template <typename A
>
818 const uint8_t* Atom
<A
>::contentPointer() const
820 const macho_section
<P
>* sct
= this->sect().machoSection();
821 if ( this->_objAddress
> sct
->addr() + sct
->size() )
822 throwf("malformed .o file, symbol has address 0x%0llX which is outside range of its section", (uint64_t)this->_objAddress
);
823 uint32_t fileOffset
= sct
->offset() - sct
->addr() + this->_objAddress
;
824 return this->sect().file().fileContent()+fileOffset
;
828 template <typename A
>
829 void Atom
<A
>::copyRawContent(uint8_t buffer
[]) const
832 if ( this->contentType() == ld::Atom::typeZeroFill
) {
833 bzero(buffer
, _size
);
835 else if ( _size
!= 0 ) {
836 memcpy(buffer
, this->contentPointer(), _size
);
841 void Atom
<arm
>::verifyAlignment() const
843 if ( (this->section().type() == ld::Section::typeCode
) && ! isThumb() ) {
844 if ( ((_objAddress
% 4) != 0) || (this->alignment().powerOf2
< 2) )
845 warning("ARM function not 4-byte aligned: %s from %s", this->name(), this->file()->path());
849 template <typename A
>
850 void Atom
<A
>::verifyAlignment() const
855 template <typename A
>
859 static bool validFile(const uint8_t* fileContent
, bool subtypeMustMatch
=false,
860 cpu_subtype_t subtype
=0);
861 static const char* fileKind(const uint8_t* fileContent
);
862 static bool hasObjC2Categories(const uint8_t* fileContent
);
863 static bool hasObjC1Categories(const uint8_t* fileContent
);
864 static ld::relocatable::File
* parse(const uint8_t* fileContent
, uint64_t fileLength
,
865 const char* path
, time_t modTime
, ld::File::Ordinal ordinal
,
866 const ParserOptions
& opts
) {
867 Parser
p(fileContent
, fileLength
, path
, modTime
,
868 ordinal
, opts
.convertUnwindInfo
);
869 return p
.parse(opts
);
872 typedef typename
A::P P
;
873 typedef typename
A::P::E E
;
874 typedef typename
A::P::uint_t pint_t
;
876 struct SourceLocation
{
878 SourceLocation(Atom
<A
>* a
, uint32_t o
) : atom(a
), offsetInAtom(o
) {}
880 uint32_t offsetInAtom
;
885 const char* name
; // only used if targetAtom is NULL
887 bool weakImport
; // only used if targetAtom is NULL
891 FixupInAtom(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, Atom
<A
>* target
) :
892 fixup(src
.offsetInAtom
, c
, k
, target
), atom(src
.atom
) { src
.atom
->incrementFixupCount(); }
894 FixupInAtom(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, ld::Fixup::TargetBinding b
, Atom
<A
>* target
) :
895 fixup(src
.offsetInAtom
, c
, k
, b
, target
), atom(src
.atom
) { src
.atom
->incrementFixupCount(); }
897 FixupInAtom(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, bool wi
, const char* name
) :
898 fixup(src
.offsetInAtom
, c
, k
, wi
, name
), atom(src
.atom
) { src
.atom
->incrementFixupCount(); }
900 FixupInAtom(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, ld::Fixup::TargetBinding b
, const char* name
) :
901 fixup(src
.offsetInAtom
, c
, k
, b
, name
), atom(src
.atom
) { src
.atom
->incrementFixupCount(); }
903 FixupInAtom(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, uint64_t addend
) :
904 fixup(src
.offsetInAtom
, c
, k
, addend
), atom(src
.atom
) { src
.atom
->incrementFixupCount(); }
906 FixupInAtom(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
) :
907 fixup(src
.offsetInAtom
, c
, k
, (uint64_t)0), atom(src
.atom
) { src
.atom
->incrementFixupCount(); }
913 void addFixup(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, Atom
<A
>* target
) {
914 _allFixups
.push_back(FixupInAtom(src
, c
, k
, target
));
917 void addFixup(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, ld::Fixup::TargetBinding b
, Atom
<A
>* target
) {
918 _allFixups
.push_back(FixupInAtom(src
, c
, k
, b
, target
));
921 void addFixup(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, bool wi
, const char* name
) {
922 _allFixups
.push_back(FixupInAtom(src
, c
, k
, wi
, name
));
925 void addFixup(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, ld::Fixup::TargetBinding b
, const char* name
) {
926 _allFixups
.push_back(FixupInAtom(src
, c
, k
, b
, name
));
929 void addFixup(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, uint64_t addend
) {
930 _allFixups
.push_back(FixupInAtom(src
, c
, k
, addend
));
933 void addFixup(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
) {
934 _allFixups
.push_back(FixupInAtom(src
, c
, k
));
938 uint32_t symbolCount() { return _symbolCount
; }
939 uint32_t indirectSymbol(uint32_t indirectIndex
);
940 const macho_nlist
<P
>& symbolFromIndex(uint32_t index
);
941 const char* nameFromSymbol(const macho_nlist
<P
>& sym
);
942 ld::Atom::Scope
scopeFromSymbol(const macho_nlist
<P
>& sym
);
943 static ld::Atom::Definition
definitionFromSymbol(const macho_nlist
<P
>& sym
);
944 static ld::Atom::Combine
combineFromSymbol(const macho_nlist
<P
>& sym
);
945 ld::Atom::SymbolTableInclusion
inclusionFromSymbol(const macho_nlist
<P
>& sym
);
946 static bool dontDeadStripFromSymbol(const macho_nlist
<P
>& sym
);
947 static bool isThumbFromSymbol(const macho_nlist
<P
>& sym
);
948 static bool weakImportFromSymbol(const macho_nlist
<P
>& sym
);
949 static bool resolverFromSymbol(const macho_nlist
<P
>& sym
);
950 uint32_t symbolIndexFromIndirectSectionAddress(pint_t
,const macho_section
<P
>*);
951 const macho_section
<P
>* firstMachOSection() { return _sectionsStart
; }
952 const macho_section
<P
>* machOSectionFromSectionIndex(uint32_t index
);
953 uint32_t machOSectionCount() { return _machOSectionsCount
; }
954 uint32_t undefinedStartIndex() { return _undefinedStartIndex
; }
955 uint32_t undefinedEndIndex() { return _undefinedEndIndex
; }
956 void addFixup(FixupInAtom f
) { _allFixups
.push_back(f
); }
957 Section
<A
>* sectionForNum(unsigned int sectNum
);
958 Section
<A
>* sectionForAddress(pint_t addr
);
959 Atom
<A
>* findAtomByAddress(pint_t addr
);
960 Atom
<A
>* findAtomByAddressOrNullIfStub(pint_t addr
);
961 Atom
<A
>* findAtomByAddressOrLocalTargetOfStub(pint_t addr
, uint32_t* offsetInAtom
);
962 Atom
<A
>* findAtomByName(const char* name
); // slow!
963 void findTargetFromAddress(pint_t addr
, TargetDesc
& target
);
964 void findTargetFromAddress(pint_t baseAddr
, pint_t addr
, TargetDesc
& target
);
965 void findTargetFromAddressAndSectionNum(pint_t addr
, unsigned int sectNum
,
967 uint32_t tentativeDefinitionCount() { return _tentativeDefinitionCount
; }
968 uint32_t absoluteSymbolCount() { return _absoluteSymbolCount
; }
970 bool hasStubsSection() { return (_stubsSectionNum
!= 0); }
971 unsigned int stubsSectionNum() { return _stubsSectionNum
; }
972 void addDtraceExtraInfos(const SourceLocation
& src
, const char* provider
);
973 const char* scanSymbolTableForAddress(uint64_t addr
);
974 bool convertUnwindInfo() { return _convertUnwindInfo
; }
975 bool hasDataInCodeLabels() { return _hasDataInCodeLabels
; }
977 macho_data_in_code_entry
<P
>* dataInCodeStart() { return _dataInCodeStart
; }
978 macho_data_in_code_entry
<P
>* dataInCodeEnd() { return _dataInCodeEnd
; }
980 void addFixups(const SourceLocation
& src
, ld::Fixup::Kind kind
, const TargetDesc
& target
);
981 void addFixups(const SourceLocation
& src
, ld::Fixup::Kind kind
, const TargetDesc
& target
, const TargetDesc
& picBase
);
985 struct LabelAndCFIBreakIterator
{
986 typedef typename CFISection
<A
>::CFI_Atom_Info CFI_Atom_Info
;
987 LabelAndCFIBreakIterator(const uint32_t* ssa
, uint32_t ssc
, const pint_t
* cfisa
,
988 uint32_t cfisc
, bool ols
)
989 : sortedSymbolIndexes(ssa
), sortedSymbolCount(ssc
), cfiStartsArray(cfisa
),
990 cfiStartsCount(cfisc
), fileHasOverlappingSymbols(ols
),
991 newSection(false), cfiIndex(0), symIndex(0) {}
992 bool next(Parser
<A
>& parser
, uint32_t sectNum
, pint_t startAddr
, pint_t endAddr
,
993 pint_t
* addr
, pint_t
* size
, const macho_nlist
<P
>** sym
);
994 pint_t
peek(Parser
<A
>& parser
, pint_t startAddr
, pint_t endAddr
);
995 void beginSection() { newSection
= true; symIndex
= 0; }
997 const uint32_t* const sortedSymbolIndexes
;
998 const uint32_t sortedSymbolCount
;
999 const pint_t
* cfiStartsArray
;
1000 const uint32_t cfiStartsCount
;
1001 const bool fileHasOverlappingSymbols
;
1007 struct CFI_CU_InfoArrays
{
1008 typedef typename CFISection
<A
>::CFI_Atom_Info CFI_Atom_Info
;
1009 typedef typename CUSection
<A
>::Info CU_Info
;
1010 CFI_CU_InfoArrays(const CFI_Atom_Info
* cfiAr
, uint32_t cfiC
, CU_Info
* cuAr
, uint32_t cuC
)
1011 : cfiArray(cfiAr
), cuArray(cuAr
), cfiCount(cfiC
), cuCount(cuC
) {}
1012 const CFI_Atom_Info
* const cfiArray
;
1013 CU_Info
* const cuArray
;
1014 const uint32_t cfiCount
;
1015 const uint32_t cuCount
;
1021 friend class Section
<A
>;
1023 enum SectionType
{ sectionTypeIgnore
, sectionTypeLiteral4
, sectionTypeLiteral8
, sectionTypeLiteral16
,
1024 sectionTypeNonLazy
, sectionTypeCFI
, sectionTypeCString
, sectionTypeCStringPointer
,
1025 sectionTypeUTF16Strings
, sectionTypeCFString
, sectionTypeObjC2ClassRefs
, typeObjC2CategoryList
,
1026 sectionTypeObjC1Classes
, sectionTypeSymboled
, sectionTypeObjC1ClassRefs
,
1027 sectionTypeTentativeDefinitions
, sectionTypeAbsoluteSymbols
, sectionTypeTLVDefs
,
1028 sectionTypeCompactUnwind
};
1030 template <typename P
>
1031 struct MachOSectionAndSectionClass
1033 const macho_section
<P
>* sect
;
1036 static int sorter(const void* l
, const void* r
) {
1037 const MachOSectionAndSectionClass
<P
>* left
= (MachOSectionAndSectionClass
<P
>*)l
;
1038 const MachOSectionAndSectionClass
<P
>* right
= (MachOSectionAndSectionClass
<P
>*)r
;
1039 int64_t diff
= left
->sect
->addr() - right
->sect
->addr();
1049 struct ParserAndSectionsArray
{ Parser
* parser
; const uint32_t* sortedSectionsArray
; };
1052 Parser(const uint8_t* fileContent
, uint64_t fileLength
,
1053 const char* path
, time_t modTime
,
1054 ld::File::Ordinal ordinal
, bool convertUnwindInfo
);
1055 ld::relocatable::File
* parse(const ParserOptions
& opts
);
1056 uint8_t loadCommandSizeMask();
1057 bool parseLoadCommands();
1058 void makeSections();
1059 void prescanSymbolTable();
1060 void makeSortedSymbolsArray(uint32_t symArray
[], const uint32_t sectionArray
[]);
1061 void makeSortedSectionsArray(uint32_t array
[]);
1062 static int pointerSorter(const void* l
, const void* r
);
1063 static int symbolIndexSorter(void* extra
, const void* l
, const void* r
);
1064 static int sectionIndexSorter(void* extra
, const void* l
, const void* r
);
1066 void parseDebugInfo();
1068 static bool isConstFunStabs(const char *stabStr
);
1069 bool read_comp_unit(const char ** name
, const char ** comp_dir
,
1070 uint64_t *stmt_list
);
1071 const char* getDwarfString(uint64_t form
, const uint8_t* p
);
1072 bool skip_form(const uint8_t ** offset
, const uint8_t * end
,
1073 uint64_t form
, uint8_t addr_size
, bool dwarf64
);
1076 // filled in by constructor
1077 const uint8_t* _fileContent
;
1078 uint32_t _fileLength
;
1081 ld::File::Ordinal _ordinal
;
1083 // filled in by parseLoadCommands()
1085 const macho_nlist
<P
>* _symbols
;
1086 uint32_t _symbolCount
;
1087 const char* _strings
;
1088 uint32_t _stringsSize
;
1089 const uint32_t* _indirectTable
;
1090 uint32_t _indirectTableCount
;
1091 uint32_t _undefinedStartIndex
;
1092 uint32_t _undefinedEndIndex
;
1093 const macho_section
<P
>* _sectionsStart
;
1094 uint32_t _machOSectionsCount
;
1096 macho_data_in_code_entry
<P
>* _dataInCodeStart
;
1097 macho_data_in_code_entry
<P
>* _dataInCodeEnd
;
1099 // filled in by parse()
1100 CFISection
<A
>* _EHFrameSection
;
1101 CUSection
<A
>* _compactUnwindSection
;
1102 AbsoluteSymbolSection
<A
>* _absoluteSection
;
1103 uint32_t _tentativeDefinitionCount
;
1104 uint32_t _absoluteSymbolCount
;
1105 uint32_t _symbolsInSections
;
1106 bool _hasLongBranchStubs
;
1107 bool _AppleObjc
; // FSF has objc that uses different data layout
1108 bool _overlappingSymbols
;
1109 bool _convertUnwindInfo
;
1110 bool _hasDataInCodeLabels
;
1111 unsigned int _stubsSectionNum
;
1112 const macho_section
<P
>* _stubsMachOSection
;
1113 std::vector
<const char*> _dtraceProviderInfo
;
1114 std::vector
<FixupInAtom
> _allFixups
;
1119 template <typename A
>
1120 Parser
<A
>::Parser(const uint8_t* fileContent
, uint64_t fileLength
, const char* path
, time_t modTime
,
1121 ld::File::Ordinal ordinal
, bool convertDUI
)
1122 : _fileContent(fileContent
), _fileLength(fileLength
), _path(path
), _modTime(modTime
),
1123 _ordinal(ordinal
), _file(NULL
),
1124 _symbols(NULL
), _symbolCount(0), _strings(NULL
), _stringsSize(0),
1125 _indirectTable(NULL
), _indirectTableCount(0),
1126 _undefinedStartIndex(0), _undefinedEndIndex(0),
1127 _sectionsStart(NULL
), _machOSectionsCount(0), _hasUUID(false),
1128 _dataInCodeStart(NULL
), _dataInCodeEnd(NULL
),
1129 _EHFrameSection(NULL
), _compactUnwindSection(NULL
), _absoluteSection(NULL
),
1130 _tentativeDefinitionCount(0), _absoluteSymbolCount(0),
1131 _symbolsInSections(0), _hasLongBranchStubs(false), _AppleObjc(false),
1132 _overlappingSymbols(false), _convertUnwindInfo(convertDUI
), _hasDataInCodeLabels(false),
1133 _stubsSectionNum(0), _stubsMachOSection(NULL
)
1139 bool Parser
<x86
>::validFile(const uint8_t* fileContent
, bool, cpu_subtype_t
)
1141 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1142 if ( header
->magic() != MH_MAGIC
)
1144 if ( header
->cputype() != CPU_TYPE_I386
)
1146 if ( header
->filetype() != MH_OBJECT
)
1152 bool Parser
<x86_64
>::validFile(const uint8_t* fileContent
, bool, cpu_subtype_t
)
1154 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1155 if ( header
->magic() != MH_MAGIC_64
)
1157 if ( header
->cputype() != CPU_TYPE_X86_64
)
1159 if ( header
->filetype() != MH_OBJECT
)
1165 bool Parser
<arm
>::validFile(const uint8_t* fileContent
, bool subtypeMustMatch
, cpu_subtype_t subtype
)
1167 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1168 if ( header
->magic() != MH_MAGIC
)
1170 if ( header
->cputype() != CPU_TYPE_ARM
)
1172 if ( header
->filetype() != MH_OBJECT
)
1174 if ( subtypeMustMatch
) {
1175 if ( (cpu_subtype_t
)header
->cpusubtype() == subtype
)
1177 // hack until libcc_kext.a is made fat
1178 if ( header
->cpusubtype() == CPU_SUBTYPE_ARM_ALL
)
1188 const char* Parser
<x86
>::fileKind(const uint8_t* fileContent
)
1190 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1191 if ( header
->magic() != MH_MAGIC
)
1193 if ( header
->cputype() != CPU_TYPE_I386
)
1199 const char* Parser
<x86_64
>::fileKind(const uint8_t* fileContent
)
1201 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1202 if ( header
->magic() != MH_MAGIC
)
1204 if ( header
->cputype() != CPU_TYPE_X86_64
)
1210 const char* Parser
<arm
>::fileKind(const uint8_t* fileContent
)
1212 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1213 if ( header
->magic() != MH_MAGIC
)
1215 if ( header
->cputype() != CPU_TYPE_ARM
)
1217 for (const ArchInfo
* t
=archInfoArray
; t
->archName
!= NULL
; ++t
) {
1218 if ( (t
->cpuType
== CPU_TYPE_ARM
) && ((cpu_subtype_t
)header
->cpusubtype() == t
->cpuSubType
) ) {
1226 template <typename A
>
1227 bool Parser
<A
>::hasObjC2Categories(const uint8_t* fileContent
)
1229 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1230 const uint32_t cmd_count
= header
->ncmds();
1231 const macho_load_command
<P
>* const cmds
= (macho_load_command
<P
>*)((char*)header
+ sizeof(macho_header
<P
>));
1232 const macho_load_command
<P
>* const cmdsEnd
= (macho_load_command
<P
>*)((char*)header
+ sizeof(macho_header
<P
>) + header
->sizeofcmds());
1233 const macho_load_command
<P
>* cmd
= cmds
;
1234 for (uint32_t i
= 0; i
< cmd_count
; ++i
) {
1235 if ( cmd
->cmd() == macho_segment_command
<P
>::CMD
) {
1236 const macho_segment_command
<P
>* segment
= (macho_segment_command
<P
>*)cmd
;
1237 const macho_section
<P
>* sectionsStart
= (macho_section
<P
>*)((char*)segment
+ sizeof(macho_segment_command
<P
>));
1238 for (uint32_t si
=0; si
< segment
->nsects(); ++si
) {
1239 const macho_section
<P
>* sect
= §ionsStart
[si
];
1240 if ( (sect
->size() > 0)
1241 && (strcmp(sect
->sectname(), "__objc_catlist") == 0)
1242 && (strcmp(sect
->segname(), "__DATA") == 0) ) {
1247 cmd
= (const macho_load_command
<P
>*)(((char*)cmd
)+cmd
->cmdsize());
1248 if ( cmd
> cmdsEnd
)
1249 throwf("malformed mach-o file, load command #%d is outside size of load commands", i
);
1255 template <typename A
>
1256 bool Parser
<A
>::hasObjC1Categories(const uint8_t* fileContent
)
1258 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1259 const uint32_t cmd_count
= header
->ncmds();
1260 const macho_load_command
<P
>* const cmds
= (macho_load_command
<P
>*)((char*)header
+ sizeof(macho_header
<P
>));
1261 const macho_load_command
<P
>* const cmdsEnd
= (macho_load_command
<P
>*)((char*)header
+ sizeof(macho_header
<P
>) + header
->sizeofcmds());
1262 const macho_load_command
<P
>* cmd
= cmds
;
1263 for (uint32_t i
= 0; i
< cmd_count
; ++i
) {
1264 if ( cmd
->cmd() == macho_segment_command
<P
>::CMD
) {
1265 const macho_segment_command
<P
>* segment
= (macho_segment_command
<P
>*)cmd
;
1266 const macho_section
<P
>* sectionsStart
= (macho_section
<P
>*)((char*)segment
+ sizeof(macho_segment_command
<P
>));
1267 for (uint32_t si
=0; si
< segment
->nsects(); ++si
) {
1268 const macho_section
<P
>* sect
= §ionsStart
[si
];
1269 if ( (sect
->size() > 0)
1270 && (strcmp(sect
->sectname(), "__category") == 0)
1271 && (strcmp(sect
->segname(), "__OBJC") == 0) ) {
1276 cmd
= (const macho_load_command
<P
>*)(((char*)cmd
)+cmd
->cmdsize());
1277 if ( cmd
> cmdsEnd
)
1278 throwf("malformed mach-o file, load command #%d is outside size of load commands", i
);
1283 template <typename A
>
1284 int Parser
<A
>::pointerSorter(const void* l
, const void* r
)
1286 // sort references by address
1287 const pint_t
* left
= (pint_t
*)l
;
1288 const pint_t
* right
= (pint_t
*)r
;
1289 return (*left
- *right
);
1292 template <typename A
>
1293 typename
A::P::uint_t Parser
<A
>::LabelAndCFIBreakIterator::peek(Parser
<A
>& parser
, pint_t startAddr
, pint_t endAddr
)
1296 if ( symIndex
< sortedSymbolCount
)
1297 symbolAddr
= parser
.symbolFromIndex(sortedSymbolIndexes
[symIndex
]).n_value();
1299 symbolAddr
= endAddr
;
1301 if ( cfiIndex
< cfiStartsCount
)
1302 cfiAddr
= cfiStartsArray
[cfiIndex
];
1305 if ( (cfiAddr
< symbolAddr
) && (cfiAddr
>= startAddr
) ) {
1306 if ( cfiAddr
< endAddr
)
1312 if ( symbolAddr
< endAddr
)
1320 // Parses up a section into chunks based on labels and CFI information.
1321 // Each call returns the next chunk address and size, and (if the break
1322 // was becuase of a label, the symbol). Returns false when no more chunks.
1324 template <typename A
>
1325 bool Parser
<A
>::LabelAndCFIBreakIterator::next(Parser
<A
>& parser
, uint32_t sectNum
, pint_t startAddr
, pint_t endAddr
,
1326 pint_t
* addr
, pint_t
* size
, const macho_nlist
<P
>** symbol
)
1328 // may not be a label on start of section, but need atom demarcation there
1331 // advance symIndex until we get to the first label at or past the start of this section
1332 while ( symIndex
< sortedSymbolCount
) {
1333 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(sortedSymbolIndexes
[symIndex
]);
1334 pint_t nextSymbolAddr
= sym
.n_value();
1335 //fprintf(stderr, "sectNum=%d, nextSymbolAddr=0x%08llX, name=%s\n", sectNum, (uint64_t)nextSymbolAddr, parser.nameFromSymbol(sym));
1336 if ( (nextSymbolAddr
> startAddr
) || ((nextSymbolAddr
== startAddr
) && (sym
.n_sect() == sectNum
)) )
1340 if ( symIndex
< sortedSymbolCount
) {
1341 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(sortedSymbolIndexes
[symIndex
]);
1342 pint_t nextSymbolAddr
= sym
.n_value();
1343 // if next symbol found is not in this section
1344 if ( sym
.n_sect() != sectNum
) {
1345 // check for CFI break instead of symbol break
1346 if ( cfiIndex
< cfiStartsCount
) {
1347 pint_t nextCfiAddr
= cfiStartsArray
[cfiIndex
];
1348 if ( nextCfiAddr
< endAddr
) {
1351 *addr
= nextCfiAddr
;
1352 *size
= peek(parser
, startAddr
, endAddr
) - nextCfiAddr
;
1358 *size
= endAddr
- startAddr
;
1360 if ( startAddr
== endAddr
)
1361 return false; // zero size section
1363 return true; // whole section is one atom with no label
1365 // if also CFI break here, eat it
1366 if ( cfiIndex
< cfiStartsCount
) {
1367 if ( cfiStartsArray
[cfiIndex
] == nextSymbolAddr
)
1370 if ( nextSymbolAddr
== startAddr
) {
1371 // label at start of section, return it as chunk
1374 *size
= peek(parser
, startAddr
, endAddr
) - startAddr
;
1378 // return chunk before first symbol
1380 *size
= nextSymbolAddr
- startAddr
;
1384 // no symbols left in whole file, so entire section is one chunk
1386 *size
= endAddr
- startAddr
;
1388 if ( startAddr
== endAddr
)
1389 return false; // zero size section
1391 return true; // whole section is one atom with no label
1394 while ( (symIndex
< sortedSymbolCount
) && (cfiIndex
< cfiStartsCount
) ) {
1395 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(sortedSymbolIndexes
[symIndex
]);
1396 pint_t nextSymbolAddr
= sym
.n_value();
1397 pint_t nextCfiAddr
= cfiStartsArray
[cfiIndex
];
1398 if ( nextSymbolAddr
< nextCfiAddr
) {
1399 if ( nextSymbolAddr
>= endAddr
)
1402 if ( nextSymbolAddr
< startAddr
)
1404 *addr
= nextSymbolAddr
;
1405 *size
= peek(parser
, startAddr
, endAddr
) - nextSymbolAddr
;
1409 else if ( nextCfiAddr
< nextSymbolAddr
) {
1410 if ( nextCfiAddr
>= endAddr
)
1413 if ( nextCfiAddr
< startAddr
)
1415 *addr
= nextCfiAddr
;
1416 *size
= peek(parser
, startAddr
, endAddr
) - nextCfiAddr
;
1421 if ( nextCfiAddr
>= endAddr
)
1425 if ( nextCfiAddr
< startAddr
)
1427 *addr
= nextCfiAddr
;
1428 *size
= peek(parser
, startAddr
, endAddr
) - nextCfiAddr
;
1433 while ( symIndex
< sortedSymbolCount
) {
1434 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(sortedSymbolIndexes
[symIndex
]);
1435 pint_t nextSymbolAddr
= sym
.n_value();
1436 // if next symbol found is not in this section, then done with iteration
1437 if ( sym
.n_sect() != sectNum
)
1440 if ( nextSymbolAddr
< startAddr
)
1442 *addr
= nextSymbolAddr
;
1443 *size
= peek(parser
, startAddr
, endAddr
) - nextSymbolAddr
;
1447 while ( cfiIndex
< cfiStartsCount
) {
1448 pint_t nextCfiAddr
= cfiStartsArray
[cfiIndex
];
1449 if ( nextCfiAddr
>= endAddr
)
1452 if ( nextCfiAddr
< startAddr
)
1454 *addr
= nextCfiAddr
;
1455 *size
= peek(parser
, startAddr
, endAddr
) - nextCfiAddr
;
1464 template <typename A
>
1465 ld::relocatable::File
* Parser
<A
>::parse(const ParserOptions
& opts
)
1467 // create file object
1468 _file
= new File
<A
>(_path
, _modTime
, _fileContent
, _ordinal
);
1470 // respond to -t option
1471 if ( opts
.logAllFiles
)
1472 printf("%s\n", _path
);
1474 // parse start of mach-o file
1475 if ( ! parseLoadCommands() )
1479 uint32_t sortedSectionIndexes
[_machOSectionsCount
];
1480 this->makeSortedSectionsArray(sortedSectionIndexes
);
1482 // make symbol table sorted by address
1483 this->prescanSymbolTable();
1484 uint32_t sortedSymbolIndexes
[_symbolsInSections
];
1485 this->makeSortedSymbolsArray(sortedSymbolIndexes
, sortedSectionIndexes
);
1487 // allocate Section<A> object for each mach-o section
1490 // if it exists, do special early parsing of __compact_unwind section
1491 uint32_t countOfCUs
= 0;
1492 if ( _compactUnwindSection
!= NULL
)
1493 countOfCUs
= _compactUnwindSection
->count();
1494 uint8_t cuInfoBuffer
[sizeof(typename CUSection
<A
>::Info
) * countOfCUs
];
1495 typename CUSection
<A
>::Info
* cuInfoArray
= (typename CUSection
<A
>::Info
*)cuInfoBuffer
;
1496 if ( countOfCUs
!= 0 )
1497 _compactUnwindSection
->parse(*this, countOfCUs
, cuInfoArray
);
1499 // if it exists, do special early parsing of __eh_frame section
1500 // stack allocate array of CFI_Atom_Info
1501 uint32_t countOfCFIs
= 0;
1502 if ( _EHFrameSection
!= NULL
)
1503 countOfCFIs
= _EHFrameSection
->cfiCount();
1504 typename CFISection
<A
>::CFI_Atom_Info cfiArray
[countOfCFIs
];
1505 // stack allocate (if not too large) a copy of __eh_frame to apply relocations to
1506 uint8_t* ehBuffer
= NULL
;
1507 uint32_t stackAllocSize
= 0;
1508 if ( (countOfCFIs
!= 0) && _EHFrameSection
->needsRelocating() ) {
1509 uint32_t sectSize
= _EHFrameSection
->machoSection()->size();
1510 if ( sectSize
> 50*1024 )
1511 ehBuffer
= (uint8_t*)malloc(sectSize
);
1513 stackAllocSize
= sectSize
;
1515 uint32_t ehStackBuffer
[1+stackAllocSize
/4]; // make 4-byte aligned stack bufffer
1516 if ( ehBuffer
== NULL
)
1517 ehBuffer
= (uint8_t*)&ehStackBuffer
;
1518 uint32_t cfiStartsCount
= 0;
1519 if ( countOfCFIs
!= 0 ) {
1520 _EHFrameSection
->cfiParse(*this, ehBuffer
, cfiArray
, countOfCFIs
);
1521 // count functions and lsdas
1522 for(uint32_t i
=0; i
< countOfCFIs
; ++i
) {
1523 if ( cfiArray
[i
].isCIE
)
1525 //fprintf(stderr, "cfiArray[i].func = 0x%08llX, cfiArray[i].lsda = 0x%08llX, encoding=0x%08X\n",
1526 // (uint64_t)cfiArray[i].u.fdeInfo.function.targetAddress,
1527 // (uint64_t)cfiArray[i].u.fdeInfo.lsda.targetAddress,
1528 // cfiArray[i].u.fdeInfo.compactUnwindInfo);
1529 if ( cfiArray
[i
].u
.fdeInfo
.function
.targetAddress
!= CFI_INVALID_ADDRESS
)
1531 if ( cfiArray
[i
].u
.fdeInfo
.lsda
.targetAddress
!= CFI_INVALID_ADDRESS
)
1535 CFI_CU_InfoArrays
cfis(cfiArray
, countOfCFIs
, cuInfoArray
, countOfCUs
);
1537 // create sorted array of function starts and lsda starts
1538 pint_t cfiStartsArray
[cfiStartsCount
];
1539 uint32_t countOfFDEs
= 0;
1540 if ( countOfCFIs
!= 0 ) {
1542 for(uint32_t i
=0; i
< countOfCFIs
; ++i
) {
1543 if ( cfiArray
[i
].isCIE
)
1545 if ( cfiArray
[i
].u
.fdeInfo
.function
.targetAddress
!= CFI_INVALID_ADDRESS
)
1546 cfiStartsArray
[index
++] = cfiArray
[i
].u
.fdeInfo
.function
.targetAddress
;
1547 if ( cfiArray
[i
].u
.fdeInfo
.lsda
.targetAddress
!= CFI_INVALID_ADDRESS
)
1548 cfiStartsArray
[index
++] = cfiArray
[i
].u
.fdeInfo
.lsda
.targetAddress
;
1551 ::qsort(cfiStartsArray
, cfiStartsCount
, sizeof(pint_t
), pointerSorter
);
1553 // scan for FDEs claming the same function
1554 for(int i
=1; i
< index
; ++i
) {
1555 assert( cfiStartsArray
[i
] != cfiStartsArray
[i
-1] );
1560 Section
<A
>** sections
= _file
->_sectionsArray
;
1561 uint32_t sectionsCount
= _file
->_sectionsArrayCount
;
1563 // figure out how many atoms will be allocated and allocate
1564 LabelAndCFIBreakIterator
breakIterator(sortedSymbolIndexes
, _symbolsInSections
, cfiStartsArray
,
1565 cfiStartsCount
, _overlappingSymbols
);
1566 uint32_t computedAtomCount
= 0;
1567 for (uint32_t i
=0; i
< sectionsCount
; ++i
) {
1568 breakIterator
.beginSection();
1569 uint32_t count
= sections
[i
]->computeAtomCount(*this, breakIterator
, cfis
);
1570 //const macho_section<P>* sect = sections[i]->machoSection();
1571 //fprintf(stderr, "computed count=%u for section %s size=%llu\n", count, sect->sectname(), (sect != NULL) ? sect->size() : 0);
1572 computedAtomCount
+= count
;
1574 //fprintf(stderr, "allocating %d atoms * sizeof(Atom<A>)=%ld, sizeof(ld::Atom)=%ld\n", computedAtomCount, sizeof(Atom<A>), sizeof(ld::Atom));
1575 _file
->_atomsArray
= new uint8_t[computedAtomCount
*sizeof(Atom
<A
>)];
1576 _file
->_atomsArrayCount
= 0;
1578 // have each section append atoms to _atomsArray
1579 LabelAndCFIBreakIterator
breakIterator2(sortedSymbolIndexes
, _symbolsInSections
, cfiStartsArray
,
1580 cfiStartsCount
, _overlappingSymbols
);
1581 for (uint32_t i
=0; i
< sectionsCount
; ++i
) {
1582 uint8_t* atoms
= _file
->_atomsArray
+ _file
->_atomsArrayCount
*sizeof(Atom
<A
>);
1583 breakIterator2
.beginSection();
1584 uint32_t count
= sections
[i
]->appendAtoms(*this, atoms
, breakIterator2
, cfis
);
1585 //fprintf(stderr, "append count=%u for section %s/%s\n", count, sections[i]->machoSection()->segname(), sections[i]->machoSection()->sectname());
1586 _file
->_atomsArrayCount
+= count
;
1588 assert( _file
->_atomsArrayCount
== computedAtomCount
&& "more atoms allocated than expected");
1591 // have each section add all fix-ups for its atoms
1592 _allFixups
.reserve(computedAtomCount
*5);
1593 for (uint32_t i
=0; i
< sectionsCount
; ++i
)
1594 sections
[i
]->makeFixups(*this, cfis
);
1596 // assign fixups start offset for each atom
1597 uint8_t* p
= _file
->_atomsArray
;
1598 uint32_t fixupOffset
= 0;
1599 for(int i
=_file
->_atomsArrayCount
; i
> 0; --i
) {
1600 Atom
<A
>* atom
= (Atom
<A
>*)p
;
1601 atom
->_fixupsStartIndex
= fixupOffset
;
1602 fixupOffset
+= atom
->_fixupsCount
;
1603 atom
->_fixupsCount
= 0;
1604 p
+= sizeof(Atom
<A
>);
1606 assert(fixupOffset
== _allFixups
.size());
1607 _file
->_fixups
.reserve(fixupOffset
);
1609 // copy each fixup for each atom
1610 for(typename
std::vector
<FixupInAtom
>::iterator it
=_allFixups
.begin(); it
!= _allFixups
.end(); ++it
) {
1611 uint32_t slot
= it
->atom
->_fixupsStartIndex
+ it
->atom
->_fixupsCount
;
1612 _file
->_fixups
[slot
] = it
->fixup
;
1613 it
->atom
->_fixupsCount
++;
1616 // done with temp vector
1620 _file
->_unwindInfos
.reserve(countOfFDEs
+countOfCUs
);
1621 for(uint32_t i
=0; i
< countOfCFIs
; ++i
) {
1622 if ( cfiArray
[i
].isCIE
)
1624 if ( cfiArray
[i
].u
.fdeInfo
.function
.targetAddress
!= CFI_INVALID_ADDRESS
) {
1625 ld::Atom::UnwindInfo info
;
1626 info
.startOffset
= 0;
1627 info
.unwindInfo
= cfiArray
[i
].u
.fdeInfo
.compactUnwindInfo
;
1628 _file
->_unwindInfos
.push_back(info
);
1629 Atom
<A
>* func
= findAtomByAddress(cfiArray
[i
].u
.fdeInfo
.function
.targetAddress
);
1630 func
->setUnwindInfoRange(_file
->_unwindInfos
.size()-1, 1);
1633 // apply compact infos in __LD,__compact_unwind section to each function
1634 // if function also has dwarf unwind, CU will override it
1635 Atom
<A
>* lastFunc
= NULL
;
1636 uint32_t lastEnd
= 0;
1637 for(uint32_t i
=0; i
< countOfCUs
; ++i
) {
1638 typename CUSection
<A
>::Info
* info
= &cuInfoArray
[i
];
1639 assert(info
->function
!= NULL
);
1640 ld::Atom::UnwindInfo ui
;
1641 ui
.startOffset
= info
->functionStartAddress
- info
->function
->objectAddress();
1642 ui
.unwindInfo
= info
->compactUnwindInfo
;
1643 _file
->_unwindInfos
.push_back(ui
);
1644 // if previous is for same function, extend range
1645 if ( info
->function
== lastFunc
) {
1646 if ( lastEnd
!= ui
.startOffset
) {
1647 if ( lastEnd
< ui
.startOffset
)
1648 warning("__LD,__compact_unwind entries for %s have a gap at offset 0x%0X", info
->function
->name(), lastEnd
);
1650 warning("__LD,__compact_unwind entries for %s overlap at offset 0x%0X", info
->function
->name(), lastEnd
);
1652 lastFunc
->extendUnwindInfoRange();
1655 info
->function
->setUnwindInfoRange(_file
->_unwindInfos
.size()-1, 1);
1656 lastFunc
= info
->function
;
1657 lastEnd
= ui
.startOffset
+ info
->rangeLength
;
1660 // parse dwarf debug info to get line info
1661 this->parseDebugInfo();
1668 template <> uint8_t Parser
<x86
>::loadCommandSizeMask() { return 0x03; }
1669 template <> uint8_t Parser
<x86_64
>::loadCommandSizeMask() { return 0x07; }
1670 template <> uint8_t Parser
<arm
>::loadCommandSizeMask() { return 0x03; }
1672 template <typename A
>
1673 bool Parser
<A
>::parseLoadCommands()
1675 const macho_header
<P
>* header
= (const macho_header
<P
>*)_fileContent
;
1677 // set File attributes
1678 _file
->_canScatterAtoms
= (header
->flags() & MH_SUBSECTIONS_VIA_SYMBOLS
);
1679 _file
->_cpuSubType
= header
->cpusubtype();
1681 const macho_segment_command
<P
>* segment
= NULL
;
1682 const uint8_t* const endOfFile
= _fileContent
+ _fileLength
;
1683 const uint32_t cmd_count
= header
->ncmds();
1684 // <rdar://problem/5394172> an empty .o file with zero load commands will crash linker
1685 if ( cmd_count
== 0 )
1687 const macho_load_command
<P
>* const cmds
= (macho_load_command
<P
>*)((char*)header
+ sizeof(macho_header
<P
>));
1688 const macho_load_command
<P
>* const cmdsEnd
= (macho_load_command
<P
>*)((char*)header
+ sizeof(macho_header
<P
>) + header
->sizeofcmds());
1689 const macho_load_command
<P
>* cmd
= cmds
;
1690 for (uint32_t i
= 0; i
< cmd_count
; ++i
) {
1691 uint32_t size
= cmd
->cmdsize();
1692 if ( (size
& this->loadCommandSizeMask()) != 0 )
1693 throwf("load command #%d has a unaligned size", i
);
1694 const uint8_t* endOfCmd
= ((uint8_t*)cmd
)+cmd
->cmdsize();
1695 if ( endOfCmd
> (uint8_t*)cmdsEnd
)
1696 throwf("load command #%d extends beyond the end of the load commands", i
);
1697 if ( endOfCmd
> endOfFile
)
1698 throwf("load command #%d extends beyond the end of the file", i
);
1699 switch (cmd
->cmd()) {
1702 const macho_symtab_command
<P
>* symtab
= (macho_symtab_command
<P
>*)cmd
;
1703 _symbolCount
= symtab
->nsyms();
1704 _symbols
= (const macho_nlist
<P
>*)(_fileContent
+ symtab
->symoff());
1705 _strings
= (char*)_fileContent
+ symtab
->stroff();
1706 _stringsSize
= symtab
->strsize();
1707 if ( (symtab
->symoff() + _symbolCount
*sizeof(macho_nlist
<P
>)) > _fileLength
)
1708 throw "mach-o symbol table extends beyond end of file";
1709 if ( (_strings
+ _stringsSize
) > (char*)endOfFile
)
1710 throw "mach-o string pool extends beyond end of file";
1711 if ( _indirectTable
== NULL
) {
1712 if ( _undefinedEndIndex
== 0 ) {
1713 _undefinedStartIndex
= 0;
1714 _undefinedEndIndex
= symtab
->nsyms();
1721 const macho_dysymtab_command
<P
>* dsymtab
= (macho_dysymtab_command
<P
>*)cmd
;
1722 _indirectTable
= (uint32_t*)(_fileContent
+ dsymtab
->indirectsymoff());
1723 _indirectTableCount
= dsymtab
->nindirectsyms();
1724 if ( &_indirectTable
[_indirectTableCount
] > (uint32_t*)endOfFile
)
1725 throw "indirect symbol table extends beyond end of file";
1726 _undefinedStartIndex
= dsymtab
->iundefsym();
1727 _undefinedEndIndex
= _undefinedStartIndex
+ dsymtab
->nundefsym();
1733 case LC_DATA_IN_CODE
:
1735 const macho_linkedit_data_command
<P
>* dc
= (macho_linkedit_data_command
<P
>*)cmd
;
1736 _dataInCodeStart
= (macho_data_in_code_entry
<P
>*)(_fileContent
+ dc
->dataoff());
1737 _dataInCodeEnd
= (macho_data_in_code_entry
<P
>*)(_fileContent
+ dc
->dataoff() + dc
->datasize());
1738 if ( _dataInCodeEnd
> (macho_data_in_code_entry
<P
>*)endOfFile
)
1739 throw "LC_DATA_IN_CODE table extends beyond end of file";
1742 if ( cmd
->cmd() == macho_segment_command
<P
>::CMD
) {
1743 if ( segment
!= NULL
)
1744 throw "more than one LC_SEGMENT found in object file";
1745 segment
= (macho_segment_command
<P
>*)cmd
;
1749 cmd
= (const macho_load_command
<P
>*)(((char*)cmd
)+cmd
->cmdsize());
1750 if ( cmd
> cmdsEnd
)
1751 throwf("malformed mach-o file, load command #%d is outside size of load commands", i
);
1754 // record range of sections
1755 if ( segment
== NULL
)
1756 throw "missing LC_SEGMENT";
1757 _sectionsStart
= (macho_section
<P
>*)((char*)segment
+ sizeof(macho_segment_command
<P
>));
1758 _machOSectionsCount
= segment
->nsects();
1764 template <typename A
>
1765 void Parser
<A
>::prescanSymbolTable()
1767 _tentativeDefinitionCount
= 0;
1768 _absoluteSymbolCount
= 0;
1769 _symbolsInSections
= 0;
1770 _hasDataInCodeLabels
= false;
1771 for (uint32_t i
=0; i
< this->_symbolCount
; ++i
) {
1772 const macho_nlist
<P
>& sym
= symbolFromIndex(i
);
1774 if ( (sym
.n_type() & N_STAB
) != 0 )
1777 // look at undefines
1778 const char* symbolName
= this->nameFromSymbol(sym
);
1779 if ( (sym
.n_type() & N_TYPE
) == N_UNDF
) {
1780 if ( sym
.n_value() != 0 ) {
1781 // count tentative definitions
1782 ++_tentativeDefinitionCount
;
1784 else if ( strncmp(symbolName
, "___dtrace_", 10) == 0 ) {
1785 // any undefined starting with __dtrace_*$ that is not ___dtrace_probe$* or ___dtrace_isenabled$*
1786 // is extra provider info
1787 if ( (strncmp(&symbolName
[10], "probe$", 6) != 0) && (strncmp(&symbolName
[10], "isenabled$", 10) != 0) ) {
1788 _dtraceProviderInfo
.push_back(symbolName
);
1794 // count absolute symbols
1795 if ( (sym
.n_type() & N_TYPE
) == N_ABS
) {
1796 const char* absName
= this->nameFromSymbol(sym
);
1797 // ignore .objc_class_name_* symbols
1798 if ( strncmp(absName
, ".objc_class_name_", 17) == 0 ) {
1802 // ignore .objc_class_name_* symbols
1803 if ( strncmp(absName
, ".objc_category_name_", 20) == 0 )
1805 // ignore empty *.eh symbols
1806 if ( strcmp(&absName
[strlen(absName
)-3], ".eh") == 0 )
1808 ++_absoluteSymbolCount
;
1811 // only look at definitions
1812 if ( (sym
.n_type() & N_TYPE
) != N_SECT
)
1815 // 'L' labels do not denote atom breaks
1816 if ( symbolName
[0] == 'L' ) {
1817 // <rdar://problem/9218847> Formalize data in code with L$start$ labels
1818 if ( strncmp(symbolName
, "L$start$", 8) == 0 )
1819 _hasDataInCodeLabels
= true;
1822 // how many def syms in each section
1823 if ( sym
.n_sect() > _machOSectionsCount
)
1824 throw "bad n_sect in symbol table";
1826 _symbolsInSections
++;
1830 template <typename A
>
1831 int Parser
<A
>::sectionIndexSorter(void* extra
, const void* l
, const void* r
)
1833 Parser
<A
>* parser
= (Parser
<A
>*)extra
;
1834 const uint32_t* left
= (uint32_t*)l
;
1835 const uint32_t* right
= (uint32_t*)r
;
1836 const macho_section
<P
>* leftSect
= parser
->machOSectionFromSectionIndex(*left
);
1837 const macho_section
<P
>* rightSect
= parser
->machOSectionFromSectionIndex(*right
);
1839 // can't just return difference because 64-bit diff does not fit in 32-bit return type
1840 int64_t result
= leftSect
->addr() - rightSect
->addr();
1841 if ( result
== 0 ) {
1842 // two sections with same start address
1843 // one with zero size goes first
1844 bool leftEmpty
= ( leftSect
->size() == 0 );
1845 bool rightEmpty
= ( rightSect
->size() == 0 );
1846 if ( leftEmpty
!= rightEmpty
) {
1847 return ( rightEmpty
? 1 : -1 );
1849 if ( !leftEmpty
&& !rightEmpty
)
1850 throwf("overlapping sections");
1851 // both empty, so chose file order
1852 return ( rightSect
- leftSect
);
1854 else if ( result
< 0 )
1860 template <typename A
>
1861 void Parser
<A
>::makeSortedSectionsArray(uint32_t array
[])
1863 const bool log
= false;
1866 fprintf(stderr
, "unsorted sections:\n");
1867 for(unsigned int i
=0; i
< _machOSectionsCount
; ++i
)
1868 fprintf(stderr
, "0x%08llX %s %s\n", _sectionsStart
[i
].addr(), _sectionsStart
[i
].segname(), _sectionsStart
[i
].sectname());
1871 // sort by symbol table address
1872 for (uint32_t i
=0; i
< _machOSectionsCount
; ++i
)
1874 ::qsort_r(array
, _machOSectionsCount
, sizeof(uint32_t), this, §ionIndexSorter
);
1877 fprintf(stderr
, "sorted sections:\n");
1878 for(unsigned int i
=0; i
< _machOSectionsCount
; ++i
)
1879 fprintf(stderr
, "0x%08llX %s %s\n", _sectionsStart
[array
[i
]].addr(), _sectionsStart
[array
[i
]].segname(), _sectionsStart
[array
[i
]].sectname());
1885 template <typename A
>
1886 int Parser
<A
>::symbolIndexSorter(void* extra
, const void* l
, const void* r
)
1888 ParserAndSectionsArray
* extraInfo
= (ParserAndSectionsArray
*)extra
;
1889 Parser
<A
>* parser
= extraInfo
->parser
;
1890 const uint32_t* sortedSectionsArray
= extraInfo
->sortedSectionsArray
;
1891 const uint32_t* left
= (uint32_t*)l
;
1892 const uint32_t* right
= (uint32_t*)r
;
1893 const macho_nlist
<P
>& leftSym
= parser
->symbolFromIndex(*left
);
1894 const macho_nlist
<P
>& rightSym
= parser
->symbolFromIndex(*right
);
1895 // can't just return difference because 64-bit diff does not fit in 32-bit return type
1896 int64_t result
= leftSym
.n_value() - rightSym
.n_value();
1897 if ( result
== 0 ) {
1898 // two symbols with same address
1899 // if in different sections, sort earlier section first
1900 if ( leftSym
.n_sect() != rightSym
.n_sect() ) {
1901 for (uint32_t i
=0; i
< parser
->machOSectionCount(); ++i
) {
1902 if ( sortedSectionsArray
[i
]+1 == leftSym
.n_sect() )
1904 if ( sortedSectionsArray
[i
]+1 == rightSym
.n_sect() )
1908 // two symbols in same section, means one is an alias
1909 // if one is ltmp*, make it an alias (sort first)
1910 const char* leftName
= parser
->nameFromSymbol(leftSym
);
1911 const char* rightName
= parser
->nameFromSymbol(rightSym
);
1912 bool leftIsTmp
= strncmp(leftName
, "ltmp", 4);
1913 bool rightIsTmp
= strncmp(rightName
, "ltmp", 4);
1914 if ( leftIsTmp
!= rightIsTmp
) {
1915 return (rightIsTmp
? -1 : 1);
1918 // if only one is global, make the other an alias (sort first)
1919 if ( (leftSym
.n_type() & N_EXT
) != (rightSym
.n_type() & N_EXT
) ) {
1920 if ( (rightSym
.n_type() & N_EXT
) != 0 )
1925 // if both are global, sort alphabetically. earlier one will be the alias
1926 return ( strcmp(rightName
, leftName
) );
1928 else if ( result
< 0 )
1935 template <typename A
>
1936 void Parser
<A
>::makeSortedSymbolsArray(uint32_t array
[], const uint32_t sectionArray
[])
1938 const bool log
= false;
1940 uint32_t* p
= array
;
1941 for (uint32_t i
=0; i
< this->_symbolCount
; ++i
) {
1942 const macho_nlist
<P
>& sym
= symbolFromIndex(i
);
1944 if ( (sym
.n_type() & N_STAB
) != 0 )
1947 // only look at definitions
1948 if ( (sym
.n_type() & N_TYPE
) != N_SECT
)
1951 // 'L' labels do not denote atom breaks
1952 const char* symbolName
= this->nameFromSymbol(sym
);
1953 if ( symbolName
[0] == 'L' )
1956 // how many def syms in each section
1957 if ( sym
.n_sect() > _machOSectionsCount
)
1958 throw "bad n_sect in symbol table";
1963 assert(p
== &array
[_symbolsInSections
] && "second pass over symbol table yield a different number of symbols");
1965 // sort by symbol table address
1966 ParserAndSectionsArray extra
= { this, sectionArray
};
1967 ::qsort_r(array
, _symbolsInSections
, sizeof(uint32_t), &extra
, &symbolIndexSorter
);
1970 // look for two symbols at same address
1971 _overlappingSymbols
= false;
1972 for (unsigned int i
=1; i
< _symbolsInSections
; ++i
) {
1973 if ( symbolFromIndex(array
[i
-1]).n_value() == symbolFromIndex(array
[i
]).n_value() ) {
1974 //fprintf(stderr, "overlapping symbols at 0x%08llX\n", symbolFromIndex(array[i-1]).n_value());
1975 _overlappingSymbols
= true;
1981 fprintf(stderr
, "sorted symbols:\n");
1982 for(unsigned int i
=0; i
< _symbolsInSections
; ++i
)
1983 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
])) );
1988 template <typename A
>
1989 void Parser
<A
>::makeSections()
1991 // classify each section by type
1992 // compute how many Section objects will be needed and total size for all
1993 unsigned int totalSectionsSize
= 0;
1994 uint8_t machOSectsStorage
[sizeof(MachOSectionAndSectionClass
<P
>)*(_machOSectionsCount
+2)]; // also room for tentative-defs and absolute symbols
1995 // allocate raw storage for all section objects on stack
1996 MachOSectionAndSectionClass
<P
>* machOSects
= (MachOSectionAndSectionClass
<P
>*)machOSectsStorage
;
1997 unsigned int count
= 0;
1998 for (uint32_t i
=0; i
< _machOSectionsCount
; ++i
) {
1999 const macho_section
<P
>* sect
= &_sectionsStart
[i
];
2000 if ( (sect
->flags() & S_ATTR_DEBUG
) != 0 ) {
2001 if ( strcmp(sect
->segname(), "__DWARF") == 0 ) {
2002 // note that .o file has dwarf
2003 _file
->_debugInfoKind
= ld::relocatable::File::kDebugInfoDwarf
;
2004 // save off iteresting dwarf sections
2005 if ( strcmp(sect
->sectname(), "__debug_info") == 0 )
2006 _file
->_dwarfDebugInfoSect
= sect
;
2007 else if ( strcmp(sect
->sectname(), "__debug_abbrev") == 0 )
2008 _file
->_dwarfDebugAbbrevSect
= sect
;
2009 else if ( strcmp(sect
->sectname(), "__debug_line") == 0 )
2010 _file
->_dwarfDebugLineSect
= sect
;
2011 else if ( strcmp(sect
->sectname(), "__debug_str") == 0 )
2012 _file
->_dwarfDebugStringSect
= sect
;
2013 // linker does not propagate dwarf sections to output file
2016 else if ( strcmp(sect
->segname(), "__LD") == 0 ) {
2017 if ( strncmp(sect
->sectname(), "__compact_unwind", 16) == 0 ) {
2018 machOSects
[count
].sect
= sect
;
2019 totalSectionsSize
+= sizeof(CUSection
<A
>);
2020 machOSects
[count
++].type
= sectionTypeCompactUnwind
;
2025 // ignore empty __OBJC sections
2026 if ( (sect
->size() == 0) && (strcmp(sect
->segname(), "__OBJC") == 0) )
2028 // objc image info section is really attributes and not content
2029 if ( ((strcmp(sect
->sectname(), "__image_info") == 0) && (strcmp(sect
->segname(), "__OBJC") == 0))
2030 || ((strncmp(sect
->sectname(), "__objc_imageinfo", 16) == 0) && (strcmp(sect
->segname(), "__DATA") == 0)) ) {
2031 // struct objc_image_info {
2032 // uint32_t version; // initially 0
2035 // #define OBJC_IMAGE_SUPPORTS_GC 2
2036 // #define OBJC_IMAGE_GC_ONLY 4
2038 const uint32_t* contents
= (uint32_t*)(_file
->fileContent()+sect
->offset());
2039 if ( (sect
->size() >= 8) && (contents
[0] == 0) ) {
2040 uint32_t flags
= E::get32(contents
[1]);
2041 if ( (flags
& 4) == 4 )
2042 _file
->_objConstraint
= ld::File::objcConstraintGC
;
2043 else if ( (flags
& 2) == 2 )
2044 _file
->_objConstraint
= ld::File::objcConstraintRetainReleaseOrGC
;
2046 _file
->_objConstraint
= ld::File::objcConstraintRetainRelease
;
2047 if ( sect
->size() > 8 ) {
2048 warning("section %s/%s has unexpectedly large size %llu in %s",
2049 sect
->segname(), Section
<A
>::makeSectionName(sect
), sect
->size(), _file
->path());
2053 warning("can't parse %s/%s section in %s", sect
->segname(), Section
<A
>::makeSectionName(sect
), _file
->path());
2057 machOSects
[count
].sect
= sect
;
2058 switch ( sect
->flags() & SECTION_TYPE
) {
2059 case S_SYMBOL_STUBS
:
2060 if ( _stubsSectionNum
== 0 ) {
2061 _stubsSectionNum
= i
+1;
2062 _stubsMachOSection
= sect
;
2065 assert(1 && "multiple S_SYMBOL_STUBS sections");
2066 case S_LAZY_SYMBOL_POINTERS
:
2068 case S_4BYTE_LITERALS
:
2069 totalSectionsSize
+= sizeof(Literal4Section
<A
>);
2070 machOSects
[count
++].type
= sectionTypeLiteral4
;
2072 case S_8BYTE_LITERALS
:
2073 totalSectionsSize
+= sizeof(Literal8Section
<A
>);
2074 machOSects
[count
++].type
= sectionTypeLiteral8
;
2076 case S_16BYTE_LITERALS
:
2077 totalSectionsSize
+= sizeof(Literal16Section
<A
>);
2078 machOSects
[count
++].type
= sectionTypeLiteral16
;
2080 case S_NON_LAZY_SYMBOL_POINTERS
:
2081 totalSectionsSize
+= sizeof(NonLazyPointerSection
<A
>);
2082 machOSects
[count
++].type
= sectionTypeNonLazy
;
2084 case S_LITERAL_POINTERS
:
2085 if ( (strcmp(sect
->segname(), "__OBJC") == 0) && (strcmp(sect
->sectname(), "__cls_refs") == 0) ) {
2086 totalSectionsSize
+= sizeof(Objc1ClassReferences
<A
>);
2087 machOSects
[count
++].type
= sectionTypeObjC1ClassRefs
;
2090 totalSectionsSize
+= sizeof(PointerToCStringSection
<A
>);
2091 machOSects
[count
++].type
= sectionTypeCStringPointer
;
2094 case S_CSTRING_LITERALS
:
2095 totalSectionsSize
+= sizeof(CStringSection
<A
>);
2096 machOSects
[count
++].type
= sectionTypeCString
;
2098 case S_MOD_INIT_FUNC_POINTERS
:
2099 case S_MOD_TERM_FUNC_POINTERS
:
2100 case S_THREAD_LOCAL_INIT_FUNCTION_POINTERS
:
2105 case S_THREAD_LOCAL_REGULAR
:
2106 case S_THREAD_LOCAL_ZEROFILL
:
2107 if ( (strcmp(sect
->segname(), "__TEXT") == 0) && (strcmp(sect
->sectname(), "__eh_frame") == 0) ) {
2108 totalSectionsSize
+= sizeof(CFISection
<A
>);
2109 machOSects
[count
++].type
= sectionTypeCFI
;
2111 else if ( (strcmp(sect
->segname(), "__DATA") == 0) && (strcmp(sect
->sectname(), "__cfstring") == 0) ) {
2112 totalSectionsSize
+= sizeof(CFStringSection
<A
>);
2113 machOSects
[count
++].type
= sectionTypeCFString
;
2115 else if ( (strcmp(sect
->segname(), "__TEXT") == 0) && (strcmp(sect
->sectname(), "__ustring") == 0) ) {
2116 totalSectionsSize
+= sizeof(UTF16StringSection
<A
>);
2117 machOSects
[count
++].type
= sectionTypeUTF16Strings
;
2119 else if ( (strcmp(sect
->segname(), "__DATA") == 0) && (strncmp(sect
->sectname(), "__objc_classrefs", 16) == 0) ) {
2120 totalSectionsSize
+= sizeof(ObjC2ClassRefsSection
<A
>);
2121 machOSects
[count
++].type
= sectionTypeObjC2ClassRefs
;
2123 else if ( (strcmp(sect
->segname(), "__DATA") == 0) && (strcmp(sect
->sectname(), "__objc_catlist") == 0) ) {
2124 totalSectionsSize
+= sizeof(ObjC2CategoryListSection
<A
>);
2125 machOSects
[count
++].type
= typeObjC2CategoryList
;
2127 else if ( _AppleObjc
&& (strcmp(sect
->segname(), "__OBJC") == 0) && (strcmp(sect
->sectname(), "__class") == 0) ) {
2128 totalSectionsSize
+= sizeof(ObjC1ClassSection
<A
>);
2129 machOSects
[count
++].type
= sectionTypeObjC1Classes
;
2132 totalSectionsSize
+= sizeof(SymboledSection
<A
>);
2133 machOSects
[count
++].type
= sectionTypeSymboled
;
2136 case S_THREAD_LOCAL_VARIABLES
:
2137 totalSectionsSize
+= sizeof(TLVDefsSection
<A
>);
2138 machOSects
[count
++].type
= sectionTypeTLVDefs
;
2140 case S_THREAD_LOCAL_VARIABLE_POINTERS
:
2142 throwf("unknown section type %d", sect
->flags() & SECTION_TYPE
);
2146 // sort by address (mach-o object files don't aways have sections sorted)
2147 ::qsort(machOSects
, count
, sizeof(MachOSectionAndSectionClass
<P
>), MachOSectionAndSectionClass
<P
>::sorter
);
2149 // we will synthesize a dummy Section<A> object for tentative definitions
2150 if ( _tentativeDefinitionCount
> 0 ) {
2151 totalSectionsSize
+= sizeof(TentativeDefinitionSection
<A
>);
2152 machOSects
[count
++].type
= sectionTypeTentativeDefinitions
;
2155 // we will synthesize a dummy Section<A> object for Absolute symbols
2156 if ( _absoluteSymbolCount
> 0 ) {
2157 totalSectionsSize
+= sizeof(AbsoluteSymbolSection
<A
>);
2158 machOSects
[count
++].type
= sectionTypeAbsoluteSymbols
;
2161 // allocate one block for all Section objects as well as pointers to each
2162 uint8_t* space
= new uint8_t[totalSectionsSize
+count
*sizeof(Section
<A
>*)];
2163 _file
->_sectionsArray
= (Section
<A
>**)space
;
2164 _file
->_sectionsArrayCount
= count
;
2165 Section
<A
>** objects
= _file
->_sectionsArray
;
2166 space
+= count
*sizeof(Section
<A
>*);
2167 for (uint32_t i
=0; i
< count
; ++i
) {
2168 switch ( machOSects
[i
].type
) {
2169 case sectionTypeIgnore
:
2171 case sectionTypeLiteral4
:
2172 *objects
++ = new (space
) Literal4Section
<A
>(*this, *_file
, machOSects
[i
].sect
);
2173 space
+= sizeof(Literal4Section
<A
>);
2175 case sectionTypeLiteral8
:
2176 *objects
++ = new (space
) Literal8Section
<A
>(*this, *_file
, machOSects
[i
].sect
);
2177 space
+= sizeof(Literal8Section
<A
>);
2179 case sectionTypeLiteral16
:
2180 *objects
++ = new (space
) Literal16Section
<A
>(*this, *_file
, machOSects
[i
].sect
);
2181 space
+= sizeof(Literal16Section
<A
>);
2183 case sectionTypeNonLazy
:
2184 *objects
++ = new (space
) NonLazyPointerSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2185 space
+= sizeof(NonLazyPointerSection
<A
>);
2187 case sectionTypeCFI
:
2188 _EHFrameSection
= new (space
) CFISection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2189 *objects
++ = _EHFrameSection
;
2190 space
+= sizeof(CFISection
<A
>);
2192 case sectionTypeCString
:
2193 *objects
++ = new (space
) CStringSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2194 space
+= sizeof(CStringSection
<A
>);
2196 case sectionTypeCStringPointer
:
2197 *objects
++ = new (space
) PointerToCStringSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2198 space
+= sizeof(PointerToCStringSection
<A
>);
2200 case sectionTypeObjC1ClassRefs
:
2201 *objects
++ = new (space
) Objc1ClassReferences
<A
>(*this, *_file
, machOSects
[i
].sect
);
2202 space
+= sizeof(Objc1ClassReferences
<A
>);
2204 case sectionTypeUTF16Strings
:
2205 *objects
++ = new (space
) UTF16StringSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2206 space
+= sizeof(UTF16StringSection
<A
>);
2208 case sectionTypeCFString
:
2209 *objects
++ = new (space
) CFStringSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2210 space
+= sizeof(CFStringSection
<A
>);
2212 case sectionTypeObjC2ClassRefs
:
2213 *objects
++ = new (space
) ObjC2ClassRefsSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2214 space
+= sizeof(ObjC2ClassRefsSection
<A
>);
2216 case typeObjC2CategoryList
:
2217 *objects
++ = new (space
) ObjC2CategoryListSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2218 space
+= sizeof(ObjC2CategoryListSection
<A
>);
2220 case sectionTypeObjC1Classes
:
2221 *objects
++ = new (space
) ObjC1ClassSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2222 space
+= sizeof(ObjC1ClassSection
<A
>);
2224 case sectionTypeSymboled
:
2225 *objects
++ = new (space
) SymboledSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2226 space
+= sizeof(SymboledSection
<A
>);
2228 case sectionTypeTLVDefs
:
2229 *objects
++ = new (space
) TLVDefsSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2230 space
+= sizeof(TLVDefsSection
<A
>);
2232 case sectionTypeCompactUnwind
:
2233 _compactUnwindSection
= new (space
) CUSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2234 *objects
++ = _compactUnwindSection
;
2235 space
+= sizeof(CUSection
<A
>);
2237 case sectionTypeTentativeDefinitions
:
2238 *objects
++ = new (space
) TentativeDefinitionSection
<A
>(*this, *_file
);
2239 space
+= sizeof(TentativeDefinitionSection
<A
>);
2241 case sectionTypeAbsoluteSymbols
:
2242 _absoluteSection
= new (space
) AbsoluteSymbolSection
<A
>(*this, *_file
);
2243 *objects
++ = _absoluteSection
;
2244 space
+= sizeof(AbsoluteSymbolSection
<A
>);
2247 throw "internal error uknown SectionType";
2253 template <typename A
>
2254 Section
<A
>* Parser
<A
>::sectionForAddress(typename
A::P::uint_t addr
)
2256 for (uint32_t i
=0; i
< _file
->_sectionsArrayCount
; ++i
) {
2257 const macho_section
<typename
A::P
>* sect
= _file
->_sectionsArray
[i
]->machoSection();
2258 // TentativeDefinitionSection and AbsoluteSymbolSection have no mach-o section
2259 if ( sect
!= NULL
) {
2260 if ( (sect
->addr() <= addr
) && (addr
< (sect
->addr()+sect
->size())) ) {
2261 return _file
->_sectionsArray
[i
];
2265 // not strictly in any section
2266 // may be in a zero length section
2267 for (uint32_t i
=0; i
< _file
->_sectionsArrayCount
; ++i
) {
2268 const macho_section
<typename
A::P
>* sect
= _file
->_sectionsArray
[i
]->machoSection();
2269 // TentativeDefinitionSection and AbsoluteSymbolSection have no mach-o section
2270 if ( sect
!= NULL
) {
2271 if ( (sect
->addr() == addr
) && (sect
->size() == 0) ) {
2272 return _file
->_sectionsArray
[i
];
2277 throwf("sectionForAddress(0x%llX) address not in any section", (uint64_t)addr
);
2280 template <typename A
>
2281 Section
<A
>* Parser
<A
>::sectionForNum(unsigned int num
)
2283 for (uint32_t i
=0; i
< _file
->_sectionsArrayCount
; ++i
) {
2284 const macho_section
<typename
A::P
>* sect
= _file
->_sectionsArray
[i
]->machoSection();
2285 // TentativeDefinitionSection and AbsoluteSymbolSection have no mach-o section
2286 if ( sect
!= NULL
) {
2287 if ( num
== (unsigned int)((sect
- _sectionsStart
)+1) )
2288 return _file
->_sectionsArray
[i
];
2291 throwf("sectionForNum(%u) section number not for any section", num
);
2294 template <typename A
>
2295 Atom
<A
>* Parser
<A
>::findAtomByAddress(pint_t addr
)
2297 Section
<A
>* section
= this->sectionForAddress(addr
);
2298 return section
->findAtomByAddress(addr
);
2301 template <typename A
>
2302 Atom
<A
>* Parser
<A
>::findAtomByAddressOrNullIfStub(pint_t addr
)
2304 if ( hasStubsSection() && (_stubsMachOSection
->addr() <= addr
) && (addr
< (_stubsMachOSection
->addr()+_stubsMachOSection
->size())) )
2306 return findAtomByAddress(addr
);
2309 template <typename A
>
2310 Atom
<A
>* Parser
<A
>::findAtomByAddressOrLocalTargetOfStub(pint_t addr
, uint32_t* offsetInAtom
)
2312 if ( hasStubsSection() && (_stubsMachOSection
->addr() <= addr
) && (addr
< (_stubsMachOSection
->addr()+_stubsMachOSection
->size())) ) {
2313 // target is a stub, remove indirection
2314 uint32_t symbolIndex
= this->symbolIndexFromIndirectSectionAddress(addr
, _stubsMachOSection
);
2315 assert(symbolIndex
!= INDIRECT_SYMBOL_LOCAL
);
2316 const macho_nlist
<P
>& sym
= this->symbolFromIndex(symbolIndex
);
2317 // can't be to external weak symbol
2318 assert( (this->combineFromSymbol(sym
) != ld::Atom::combineByName
) || (this->scopeFromSymbol(sym
) != ld::Atom::scopeGlobal
) );
2320 return this->findAtomByName(this->nameFromSymbol(sym
));
2322 Atom
<A
>* target
= this->findAtomByAddress(addr
);
2323 *offsetInAtom
= addr
- target
->_objAddress
;
2327 template <typename A
>
2328 Atom
<A
>* Parser
<A
>::findAtomByName(const char* name
)
2330 uint8_t* p
= _file
->_atomsArray
;
2331 for(int i
=_file
->_atomsArrayCount
; i
> 0; --i
) {
2332 Atom
<A
>* atom
= (Atom
<A
>*)p
;
2333 if ( strcmp(name
, atom
->name()) == 0 )
2335 p
+= sizeof(Atom
<A
>);
2340 template <typename A
>
2341 void Parser
<A
>::findTargetFromAddress(pint_t addr
, TargetDesc
& target
)
2343 if ( hasStubsSection() && (_stubsMachOSection
->addr() <= addr
) && (addr
< (_stubsMachOSection
->addr()+_stubsMachOSection
->size())) ) {
2344 // target is a stub, remove indirection
2345 uint32_t symbolIndex
= this->symbolIndexFromIndirectSectionAddress(addr
, _stubsMachOSection
);
2346 assert(symbolIndex
!= INDIRECT_SYMBOL_LOCAL
);
2347 const macho_nlist
<P
>& sym
= this->symbolFromIndex(symbolIndex
);
2349 target
.name
= this->nameFromSymbol(sym
);
2350 target
.weakImport
= this->weakImportFromSymbol(sym
);
2354 Section
<A
>* section
= this->sectionForAddress(addr
);
2355 target
.atom
= section
->findAtomByAddress(addr
);
2356 target
.addend
= addr
- target
.atom
->_objAddress
;
2357 target
.weakImport
= false;
2361 template <typename A
>
2362 void Parser
<A
>::findTargetFromAddress(pint_t baseAddr
, pint_t addr
, TargetDesc
& target
)
2364 findTargetFromAddress(baseAddr
, target
);
2365 target
.addend
= addr
- target
.atom
->_objAddress
;
2368 template <typename A
>
2369 void Parser
<A
>::findTargetFromAddressAndSectionNum(pint_t addr
, unsigned int sectNum
, TargetDesc
& target
)
2371 if ( sectNum
== R_ABS
) {
2372 // target is absolute symbol that corresponds to addr
2373 if ( _absoluteSection
!= NULL
) {
2374 target
.atom
= _absoluteSection
->findAbsAtomForValue(addr
);
2375 if ( target
.atom
!= NULL
) {
2377 target
.weakImport
= false;
2382 throwf("R_ABS reloc but no absolute symbol at target address");
2385 if ( hasStubsSection() && (stubsSectionNum() == sectNum
) ) {
2386 // target is a stub, remove indirection
2387 uint32_t symbolIndex
= this->symbolIndexFromIndirectSectionAddress(addr
, _stubsMachOSection
);
2388 assert(symbolIndex
!= INDIRECT_SYMBOL_LOCAL
);
2389 const macho_nlist
<P
>& sym
= this->symbolFromIndex(symbolIndex
);
2390 // use direct reference when stub is to a static function
2391 if ( ((sym
.n_type() & N_TYPE
) == N_SECT
) && (((sym
.n_type() & N_EXT
) == 0) || (this->nameFromSymbol(sym
)[0] == 'L')) ) {
2392 this->findTargetFromAddressAndSectionNum(sym
.n_value(), sym
.n_sect(), target
);
2396 target
.name
= this->nameFromSymbol(sym
);
2397 target
.weakImport
= this->weakImportFromSymbol(sym
);
2402 Section
<A
>* section
= this->sectionForNum(sectNum
);
2403 target
.atom
= section
->findAtomByAddress(addr
);
2404 if ( target
.atom
== NULL
) {
2405 typedef typename
A::P::sint_t sint_t
;
2406 sint_t a
= (sint_t
)addr
;
2407 sint_t sectStart
= (sint_t
)(section
->machoSection()->addr());
2408 sint_t sectEnd
= sectStart
+ section
->machoSection()->size();
2409 if ( a
< sectStart
) {
2410 // target address is before start of section, so must be negative addend
2411 target
.atom
= section
->findAtomByAddress(sectStart
);
2412 target
.addend
= a
- sectStart
;
2413 target
.weakImport
= false;
2417 else if ( a
>= sectEnd
) {
2418 target
.atom
= section
->findAtomByAddress(sectEnd
-1);
2419 target
.addend
= a
- sectEnd
;
2420 target
.weakImport
= false;
2425 assert(target
.atom
!= NULL
);
2426 target
.addend
= addr
- target
.atom
->_objAddress
;
2427 target
.weakImport
= false;
2431 template <typename A
>
2432 void Parser
<A
>::addDtraceExtraInfos(const SourceLocation
& src
, const char* providerName
)
2434 // for every ___dtrace_stability$* and ___dtrace_typedefs$* undefine with
2435 // a matching provider name, add a by-name kDtraceTypeReference at probe site
2436 const char* dollar
= strchr(providerName
, '$');
2437 if ( dollar
!= NULL
) {
2438 int providerNameLen
= dollar
-providerName
+1;
2439 for ( std::vector
<const char*>::iterator it
= _dtraceProviderInfo
.begin(); it
!= _dtraceProviderInfo
.end(); ++it
) {
2440 const char* typeDollar
= strchr(*it
, '$');
2441 if ( typeDollar
!= NULL
) {
2442 if ( strncmp(typeDollar
+1, providerName
, providerNameLen
) == 0 ) {
2443 addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindDtraceExtra
,false, *it
);
2450 template <typename A
>
2451 const char* Parser
<A
>::scanSymbolTableForAddress(uint64_t addr
)
2453 uint64_t closestSymAddr
= 0;
2454 const char* closestSymName
= NULL
;
2455 for (uint32_t i
=0; i
< this->_symbolCount
; ++i
) {
2456 const macho_nlist
<P
>& sym
= symbolFromIndex(i
);
2458 if ( (sym
.n_type() & N_STAB
) != 0 )
2461 // only look at definitions
2462 if ( (sym
.n_type() & N_TYPE
) != N_SECT
)
2465 // return with exact match
2466 if ( sym
.n_value() == addr
)
2467 return nameFromSymbol(sym
);
2469 // record closest seen so far
2470 if ( (sym
.n_value() < addr
) && ((sym
.n_value() > closestSymAddr
) || (closestSymName
== NULL
)) )
2471 closestSymName
= nameFromSymbol(sym
);
2474 return (closestSymName
!= NULL
) ? closestSymName
: "unknown";
2478 template <typename A
>
2479 void Parser
<A
>::addFixups(const SourceLocation
& src
, ld::Fixup::Kind setKind
, const TargetDesc
& target
)
2481 // some fixup pairs can be combined
2482 ld::Fixup::Cluster cl
= ld::Fixup::k1of3
;
2483 ld::Fixup::Kind firstKind
= ld::Fixup::kindSetTargetAddress
;
2484 bool combined
= false;
2485 if ( target
.addend
== 0 ) {
2486 cl
= ld::Fixup::k1of1
;
2488 switch ( setKind
) {
2489 case ld::Fixup::kindStoreLittleEndian32
:
2490 firstKind
= ld::Fixup::kindStoreTargetAddressLittleEndian32
;
2492 case ld::Fixup::kindStoreLittleEndian64
:
2493 firstKind
= ld::Fixup::kindStoreTargetAddressLittleEndian64
;
2495 case ld::Fixup::kindStoreBigEndian32
:
2496 firstKind
= ld::Fixup::kindStoreTargetAddressBigEndian32
;
2498 case ld::Fixup::kindStoreBigEndian64
:
2499 firstKind
= ld::Fixup::kindStoreTargetAddressBigEndian64
;
2501 case ld::Fixup::kindStoreX86BranchPCRel32
:
2502 firstKind
= ld::Fixup::kindStoreTargetAddressX86BranchPCRel32
;
2504 case ld::Fixup::kindStoreX86PCRel32
:
2505 firstKind
= ld::Fixup::kindStoreTargetAddressX86PCRel32
;
2507 case ld::Fixup::kindStoreX86PCRel32GOTLoad
:
2508 firstKind
= ld::Fixup::kindStoreTargetAddressX86PCRel32GOTLoad
;
2510 case ld::Fixup::kindStoreX86PCRel32TLVLoad
:
2511 firstKind
= ld::Fixup::kindStoreTargetAddressX86PCRel32TLVLoad
;
2513 case ld::Fixup::kindStoreX86Abs32TLVLoad
:
2514 firstKind
= ld::Fixup::kindStoreTargetAddressX86Abs32TLVLoad
;
2516 case ld::Fixup::kindStoreARMBranch24
:
2517 firstKind
= ld::Fixup::kindStoreTargetAddressARMBranch24
;
2519 case ld::Fixup::kindStoreThumbBranch22
:
2520 firstKind
= ld::Fixup::kindStoreTargetAddressThumbBranch22
;
2524 cl
= ld::Fixup::k1of2
;
2529 if ( target
.atom
!= NULL
) {
2530 if ( target
.atom
->scope() == ld::Atom::scopeTranslationUnit
) {
2531 addFixup(src
, cl
, firstKind
, target
.atom
);
2533 else if ( (target
.atom
->combine() == ld::Atom::combineByNameAndContent
) || (target
.atom
->combine() == ld::Atom::combineByNameAndReferences
) ) {
2534 addFixup(src
, cl
, firstKind
, ld::Fixup::bindingByContentBound
, target
.atom
);
2536 else if ( (src
.atom
->section().type() == ld::Section::typeCFString
) && (src
.offsetInAtom
!= 0) ) {
2537 // backing string in CFStrings should always be direct
2538 addFixup(src
, cl
, firstKind
, target
.atom
);
2541 // change direct fixup to by-name fixup
2542 addFixup(src
, cl
, firstKind
, false, target
.atom
->name());
2546 addFixup(src
, cl
, firstKind
, target
.weakImport
, target
.name
);
2548 if ( target
.addend
== 0 ) {
2550 addFixup(src
, ld::Fixup::k2of2
, setKind
);
2553 addFixup(src
, ld::Fixup::k2of3
, ld::Fixup::kindAddAddend
, target
.addend
);
2554 addFixup(src
, ld::Fixup::k3of3
, setKind
);
2558 template <typename A
>
2559 void Parser
<A
>::addFixups(const SourceLocation
& src
, ld::Fixup::Kind kind
, const TargetDesc
& target
, const TargetDesc
& picBase
)
2561 ld::Fixup::Cluster cl
= (target
.addend
== 0) ? ld::Fixup::k1of4
: ld::Fixup::k1of5
;
2562 if ( target
.atom
!= NULL
) {
2563 if ( target
.atom
->scope() == ld::Atom::scopeTranslationUnit
) {
2564 addFixup(src
, cl
, ld::Fixup::kindSetTargetAddress
, target
.atom
);
2566 else if ( (target
.atom
->combine() == ld::Atom::combineByNameAndContent
) || (target
.atom
->combine() == ld::Atom::combineByNameAndReferences
) ) {
2567 addFixup(src
, cl
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, target
.atom
);
2570 addFixup(src
, cl
, ld::Fixup::kindSetTargetAddress
, false, target
.atom
->name());
2574 addFixup(src
, cl
, ld::Fixup::kindSetTargetAddress
, target
.weakImport
, target
.name
);
2576 if ( target
.addend
== 0 ) {
2577 assert(picBase
.atom
!= NULL
);
2578 addFixup(src
, ld::Fixup::k2of4
, ld::Fixup::kindSubtractTargetAddress
, picBase
.atom
);
2579 addFixup(src
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractAddend
, picBase
.addend
);
2580 addFixup(src
, ld::Fixup::k4of4
, kind
);
2583 addFixup(src
, ld::Fixup::k2of5
, ld::Fixup::kindAddAddend
, target
.addend
);
2584 addFixup(src
, ld::Fixup::k3of5
, ld::Fixup::kindSubtractTargetAddress
, picBase
.atom
);
2585 addFixup(src
, ld::Fixup::k4of5
, ld::Fixup::kindSubtractAddend
, picBase
.addend
);
2586 addFixup(src
, ld::Fixup::k5of5
, kind
);
2592 template <typename A
>
2593 uint32_t TentativeDefinitionSection
<A
>::computeAtomCount(class Parser
<A
>& parser
,
2594 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
2595 const struct Parser
<A
>::CFI_CU_InfoArrays
&)
2597 return parser
.tentativeDefinitionCount();
2600 template <typename A
>
2601 uint32_t TentativeDefinitionSection
<A
>::appendAtoms(class Parser
<A
>& parser
, uint8_t* p
,
2602 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
2603 const struct Parser
<A
>::CFI_CU_InfoArrays
&)
2605 this->_beginAtoms
= (Atom
<A
>*)p
;
2607 for (uint32_t i
=parser
.undefinedStartIndex(); i
< parser
.undefinedEndIndex(); ++i
) {
2608 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(i
);
2609 if ( ((sym
.n_type() & N_TYPE
) == N_UNDF
) && (sym
.n_value() != 0) ) {
2610 uint64_t size
= sym
.n_value();
2611 uint8_t alignP2
= GET_COMM_ALIGN(sym
.n_desc());
2612 if ( alignP2
== 0 ) {
2613 // common symbols align to their size
2614 // that is, a 4-byte common aligns to 4-bytes
2615 // if this size is not a power of two,
2616 // then round up to the next power of two
2617 alignP2
= 63 - (uint8_t)__builtin_clzll(size
);
2618 if ( size
!= (1ULL << alignP2
) )
2621 // limit alignment of extremely large commons to 2^15 bytes (8-page)
2624 Atom
<A
>* allocatedSpace
= (Atom
<A
>*)p
;
2625 new (allocatedSpace
) Atom
<A
>(*this, parser
.nameFromSymbol(sym
), (pint_t
)ULLONG_MAX
, size
,
2626 ld::Atom::definitionTentative
, ld::Atom::combineByName
,
2627 parser
.scopeFromSymbol(sym
), ld::Atom::typeZeroFill
, ld::Atom::symbolTableIn
,
2628 parser
.dontDeadStripFromSymbol(sym
), false, false, ld::Atom::Alignment(alignP2
) );
2629 p
+= sizeof(Atom
<A
>);
2633 this->_endAtoms
= (Atom
<A
>*)p
;
2638 template <typename A
>
2639 uint32_t AbsoluteSymbolSection
<A
>::computeAtomCount(class Parser
<A
>& parser
,
2640 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
2641 const struct Parser
<A
>::CFI_CU_InfoArrays
&)
2643 return parser
.absoluteSymbolCount();
2646 template <typename A
>
2647 uint32_t AbsoluteSymbolSection
<A
>::appendAtoms(class Parser
<A
>& parser
, uint8_t* p
,
2648 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
2649 const struct Parser
<A
>::CFI_CU_InfoArrays
&)
2651 this->_beginAtoms
= (Atom
<A
>*)p
;
2653 for (uint32_t i
=0; i
< parser
.symbolCount(); ++i
) {
2654 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(i
);
2655 if ( (sym
.n_type() & N_TYPE
) != N_ABS
)
2657 const char* absName
= parser
.nameFromSymbol(sym
);
2658 // ignore .objc_class_name_* symbols
2659 if ( strncmp(absName
, ".objc_class_name_", 17) == 0 )
2661 // ignore .objc_class_name_* symbols
2662 if ( strncmp(absName
, ".objc_category_name_", 20) == 0 )
2664 // ignore empty *.eh symbols
2665 if ( strcmp(&absName
[strlen(absName
)-3], ".eh") == 0 )
2668 Atom
<A
>* allocatedSpace
= (Atom
<A
>*)p
;
2669 new (allocatedSpace
) Atom
<A
>(*this, parser
, sym
, 0);
2670 p
+= sizeof(Atom
<A
>);
2673 this->_endAtoms
= (Atom
<A
>*)p
;
2677 template <typename A
>
2678 Atom
<A
>* AbsoluteSymbolSection
<A
>::findAbsAtomForValue(typename
A::P::uint_t value
)
2680 Atom
<A
>* end
= this->_endAtoms
;
2681 for(Atom
<A
>* p
= this->_beginAtoms
; p
< end
; ++p
) {
2682 if ( p
->_objAddress
== value
)
2689 template <typename A
>
2690 uint32_t Parser
<A
>::indirectSymbol(uint32_t indirectIndex
)
2692 if ( indirectIndex
>= _indirectTableCount
)
2693 throw "indirect symbol index out of range";
2694 return E::get32(_indirectTable
[indirectIndex
]);
2697 template <typename A
>
2698 const macho_nlist
<typename
A::P
>& Parser
<A
>::symbolFromIndex(uint32_t index
)
2700 if ( index
> _symbolCount
)
2701 throw "symbol index out of range";
2702 return _symbols
[index
];
2705 template <typename A
>
2706 const macho_section
<typename
A::P
>* Parser
<A
>::machOSectionFromSectionIndex(uint32_t index
)
2708 if ( index
>= _machOSectionsCount
)
2709 throw "section index out of range";
2710 return &_sectionsStart
[index
];
2713 template <typename A
>
2714 uint32_t Parser
<A
>::symbolIndexFromIndirectSectionAddress(pint_t addr
, const macho_section
<P
>* sect
)
2716 uint32_t elementSize
= 0;
2717 switch ( sect
->flags() & SECTION_TYPE
) {
2718 case S_SYMBOL_STUBS
:
2719 elementSize
= sect
->reserved2();
2721 case S_LAZY_SYMBOL_POINTERS
:
2722 case S_NON_LAZY_SYMBOL_POINTERS
:
2723 elementSize
= sizeof(pint_t
);
2726 throw "section does not use inirect symbol table";
2728 uint32_t indexInSection
= (addr
- sect
->addr()) / elementSize
;
2729 uint32_t indexIntoIndirectTable
= sect
->reserved1() + indexInSection
;
2730 return this->indirectSymbol(indexIntoIndirectTable
);
2735 template <typename A
>
2736 const char* Parser
<A
>::nameFromSymbol(const macho_nlist
<P
>& sym
)
2738 return &_strings
[sym
.n_strx()];
2741 template <typename A
>
2742 ld::Atom::Scope Parser
<A
>::scopeFromSymbol(const macho_nlist
<P
>& sym
)
2744 if ( (sym
.n_type() & N_EXT
) == 0 )
2745 return ld::Atom::scopeTranslationUnit
;
2746 else if ( (sym
.n_type() & N_PEXT
) != 0 )
2747 return ld::Atom::scopeLinkageUnit
;
2748 else if ( this->nameFromSymbol(sym
)[0] == 'l' ) // since all 'l' symbols will be remove, don't make them global
2749 return ld::Atom::scopeLinkageUnit
;
2751 return ld::Atom::scopeGlobal
;
2754 template <typename A
>
2755 ld::Atom::Definition Parser
<A
>::definitionFromSymbol(const macho_nlist
<P
>& sym
)
2757 switch ( sym
.n_type() & N_TYPE
) {
2759 return ld::Atom::definitionAbsolute
;
2761 return ld::Atom::definitionRegular
;
2763 if ( sym
.n_value() != 0 )
2764 return ld::Atom::definitionTentative
;
2766 throw "definitionFromSymbol() bad symbol";
2769 template <typename A
>
2770 ld::Atom::Combine Parser
<A
>::combineFromSymbol(const macho_nlist
<P
>& sym
)
2772 if ( sym
.n_desc() & N_WEAK_DEF
)
2773 return ld::Atom::combineByName
;
2775 return ld::Atom::combineNever
;
2779 template <typename A
>
2780 ld::Atom::SymbolTableInclusion Parser
<A
>::inclusionFromSymbol(const macho_nlist
<P
>& sym
)
2782 const char* symbolName
= nameFromSymbol(sym
);
2783 // labels beginning with 'l' (lowercase ell) are automatically removed in final linked images <rdar://problem/4571042>
2784 // labels beginning with 'L' should have been stripped by the assembler, so are stripped now
2785 if ( sym
.n_desc() & REFERENCED_DYNAMICALLY
)
2786 return ld::Atom::symbolTableInAndNeverStrip
;
2787 else if ( symbolName
[0] == 'l' )
2788 return ld::Atom::symbolTableNotInFinalLinkedImages
;
2789 else if ( symbolName
[0] == 'L' )
2790 return ld::Atom::symbolTableNotIn
;
2792 return ld::Atom::symbolTableIn
;
2795 template <typename A
>
2796 bool Parser
<A
>::dontDeadStripFromSymbol(const macho_nlist
<P
>& sym
)
2798 return ( (sym
.n_desc() & (N_NO_DEAD_STRIP
|REFERENCED_DYNAMICALLY
)) != 0 );
2801 template <typename A
>
2802 bool Parser
<A
>::isThumbFromSymbol(const macho_nlist
<P
>& sym
)
2804 return ( sym
.n_desc() & N_ARM_THUMB_DEF
);
2807 template <typename A
>
2808 bool Parser
<A
>::weakImportFromSymbol(const macho_nlist
<P
>& sym
)
2810 return ( ((sym
.n_type() & N_TYPE
) == N_UNDF
) && ((sym
.n_desc() & N_WEAK_REF
) != 0) );
2813 template <typename A
>
2814 bool Parser
<A
>::resolverFromSymbol(const macho_nlist
<P
>& sym
)
2816 return ( sym
.n_desc() & N_SYMBOL_RESOLVER
);
2820 /* Skip over a LEB128 value (signed or unsigned). */
2822 skip_leb128 (const uint8_t ** offset
, const uint8_t * end
)
2824 while (*offset
!= end
&& **offset
>= 0x80)
2830 /* Read a ULEB128 into a 64-bit word. Return (uint64_t)-1 on overflow
2831 or error. On overflow, skip past the rest of the uleb128. */
2833 read_uleb128 (const uint8_t ** offset
, const uint8_t * end
)
2835 uint64_t result
= 0;
2842 return (uint64_t) -1;
2844 b
= **offset
& 0x7f;
2846 if (bit
>= 64 || b
<< bit
>> bit
!= b
)
2847 result
= (uint64_t) -1;
2849 result
|= b
<< bit
, bit
+= 7;
2850 } while (*(*offset
)++ >= 0x80);
2855 /* Skip over a DWARF attribute of form FORM. */
2856 template <typename A
>
2857 bool Parser
<A
>::skip_form(const uint8_t ** offset
, const uint8_t * end
, uint64_t form
,
2858 uint8_t addr_size
, bool dwarf64
)
2868 case DW_FORM_block2
:
2869 if (end
- *offset
< 2)
2871 sz
= 2 + A::P::E::get16(*(uint16_t*)offset
);
2874 case DW_FORM_block4
:
2875 if (end
- *offset
< 4)
2877 sz
= 2 + A::P::E::get32(*(uint32_t*)offset
);
2895 case DW_FORM_string
:
2896 while (*offset
!= end
&& **offset
)
2905 sz
= read_uleb128 (offset
, end
);
2908 case DW_FORM_block1
:
2916 case DW_FORM_ref_udata
:
2917 skip_leb128 (offset
, end
);
2921 case DW_FORM_ref_addr
:
2928 if (end
- *offset
< sz
)
2935 template <typename A
>
2936 const char* Parser
<A
>::getDwarfString(uint64_t form
, const uint8_t* p
)
2938 if ( form
== DW_FORM_string
)
2939 return (const char*)p
;
2940 else if ( form
== DW_FORM_strp
) {
2941 uint32_t offset
= E::get32(*((uint32_t*)p
));
2942 const char* dwarfStrings
= (char*)_file
->fileContent() + _file
->_dwarfDebugStringSect
->offset();
2943 if ( offset
> _file
->_dwarfDebugStringSect
->size() ) {
2944 warning("unknown dwarf DW_FORM_strp (offset=0x%08X) is too big in %s\n", offset
, this->_path
);
2947 return &dwarfStrings
[offset
];
2949 warning("unknown dwarf string encoding (form=%lld) in %s\n", form
, this->_path
);
2954 template <typename A
>
2955 struct AtomAndLineInfo
{
2957 ld::Atom::LineInfo info
;
2961 // <rdar://problem/5591394> Add support to ld64 for N_FUN stabs when used for symbolic constants
2962 // Returns whether a stabStr belonging to an N_FUN stab represents a
2963 // symbolic constant rather than a function
2964 template <typename A
>
2965 bool Parser
<A
>::isConstFunStabs(const char *stabStr
)
2968 // N_FUN can be used for both constants and for functions. In case it's a constant,
2969 // the format of the stabs string is "symname:c=<value>;"
2970 // ':' cannot appear in the symbol name, except if it's an Objective-C method
2971 // (in which case the symbol name starts with + or -, and then it's definitely
2973 return (stabStr
!= NULL
) && (stabStr
[0] != '+') && (stabStr
[0] != '-')
2974 && ((colon
= strchr(stabStr
, ':')) != NULL
)
2975 && (colon
[1] == 'c') && (colon
[2] == '=');
2979 template <typename A
>
2980 void Parser
<A
>::parseDebugInfo()
2982 // check for dwarf __debug_info section
2983 if ( _file
->_dwarfDebugInfoSect
== NULL
) {
2984 // if no DWARF debug info, look for stabs
2988 if ( _file
->_dwarfDebugInfoSect
->size() == 0 )
2994 if ( !read_comp_unit(&tuName
, &tuDir
, &stmtList
) ) {
2995 // if can't parse dwarf, warn and give up
2996 _file
->_dwarfTranslationUnitPath
= NULL
;
2997 warning("can't parse dwarf compilation unit info in %s", _path
);
2998 _file
->_debugInfoKind
= ld::relocatable::File::kDebugInfoNone
;
3001 if ( (tuName
!= NULL
) && (tuName
[0] == '/') ) {
3002 _file
->_dwarfTranslationUnitPath
= tuName
;
3004 else if ( (tuDir
!= NULL
) && (tuName
!= NULL
) ) {
3005 asprintf((char**)&(_file
->_dwarfTranslationUnitPath
), "%s/%s", tuDir
, tuName
);
3007 else if ( tuDir
== NULL
) {
3008 _file
->_dwarfTranslationUnitPath
= tuName
;
3011 _file
->_dwarfTranslationUnitPath
= NULL
;
3014 // add line number info to atoms from dwarf
3015 std::vector
<AtomAndLineInfo
<A
> > entries
;
3016 entries
.reserve(64);
3017 if ( _file
->_debugInfoKind
== ld::relocatable::File::kDebugInfoDwarf
) {
3018 // file with just data will have no __debug_line info
3019 if ( (_file
->_dwarfDebugLineSect
!= NULL
) && (_file
->_dwarfDebugLineSect
->size() != 0) ) {
3020 // validate stmt_list
3021 if ( (stmtList
!= (uint64_t)-1) && (stmtList
< _file
->_dwarfDebugLineSect
->size()) ) {
3022 const uint8_t* debug_line
= (uint8_t*)_file
->fileContent() + _file
->_dwarfDebugLineSect
->offset();
3023 struct line_reader_data
* lines
= line_open(&debug_line
[stmtList
],
3024 _file
->_dwarfDebugLineSect
->size() - stmtList
, E::little_endian
);
3025 struct line_info result
;
3026 Atom
<A
>* curAtom
= NULL
;
3027 uint32_t curAtomOffset
= 0;
3028 uint32_t curAtomAddress
= 0;
3029 uint32_t curAtomSize
= 0;
3030 std::map
<uint32_t,const char*> dwarfIndexToFile
;
3031 if ( lines
!= NULL
) {
3032 while ( line_next(lines
, &result
, line_stop_pc
) ) {
3033 //fprintf(stderr, "curAtom=%p, result.pc=0x%llX, result.line=%llu, result.end_of_sequence=%d,"
3034 // " curAtomAddress=0x%X, curAtomSize=0x%X\n",
3035 // curAtom, result.pc, result.line, result.end_of_sequence, curAtomAddress, curAtomSize);
3036 // work around weird debug line table compiler generates if no functions in __text section
3037 if ( (curAtom
== NULL
) && (result
.pc
== 0) && result
.end_of_sequence
&& (result
.file
== 1))
3039 // for performance, see if in next pc is in current atom
3040 if ( (curAtom
!= NULL
) && (curAtomAddress
<= result
.pc
) && (result
.pc
< (curAtomAddress
+curAtomSize
)) ) {
3041 curAtomOffset
= result
.pc
- curAtomAddress
;
3043 // or pc at end of current atom
3044 else if ( result
.end_of_sequence
&& (curAtom
!= NULL
) && (result
.pc
== (curAtomAddress
+curAtomSize
)) ) {
3045 curAtomOffset
= result
.pc
- curAtomAddress
;
3047 // or only one function that is a one line function
3048 else if ( result
.end_of_sequence
&& (curAtom
== NULL
) && (this->findAtomByAddress(0) != NULL
) && (result
.pc
== this->findAtomByAddress(0)->size()) ) {
3049 curAtom
= this->findAtomByAddress(0);
3050 curAtomOffset
= result
.pc
- curAtom
->objectAddress();
3051 curAtomAddress
= curAtom
->objectAddress();
3052 curAtomSize
= curAtom
->size();
3055 // do slow look up of atom by address
3057 curAtom
= this->findAtomByAddress(result
.pc
);
3060 // in case of bug in debug info, don't abort link, just limp on
3063 if ( curAtom
== NULL
)
3064 break; // file has line info but no functions
3065 if ( result
.end_of_sequence
&& (curAtomAddress
+curAtomSize
< result
.pc
) ) {
3066 // a one line function can be returned by line_next() as one entry with pc at end of blob
3067 // look for alt atom starting at end of previous atom
3068 uint32_t previousEnd
= curAtomAddress
+curAtomSize
;
3069 Atom
<A
>* alt
= this->findAtomByAddressOrNullIfStub(previousEnd
);
3071 continue; // ignore spurious debug info for stubs
3072 if ( result
.pc
<= alt
->objectAddress() + alt
->size() ) {
3074 curAtomOffset
= result
.pc
- alt
->objectAddress();
3075 curAtomAddress
= alt
->objectAddress();
3076 curAtomSize
= alt
->size();
3079 curAtomOffset
= result
.pc
- curAtom
->objectAddress();
3080 curAtomAddress
= curAtom
->objectAddress();
3081 curAtomSize
= curAtom
->size();
3085 curAtomOffset
= result
.pc
- curAtom
->objectAddress();
3086 curAtomAddress
= curAtom
->objectAddress();
3087 curAtomSize
= curAtom
->size();
3090 const char* filename
;
3091 std::map
<uint32_t,const char*>::iterator pos
= dwarfIndexToFile
.find(result
.file
);
3092 if ( pos
== dwarfIndexToFile
.end() ) {
3093 filename
= line_file(lines
, result
.file
);
3094 dwarfIndexToFile
[result
.file
] = filename
;
3097 filename
= pos
->second
;
3099 // only record for ~8000 line info records per function
3100 if ( curAtom
->roomForMoreLineInfoCount() ) {
3101 AtomAndLineInfo
<A
> entry
;
3102 entry
.atom
= curAtom
;
3103 entry
.info
.atomOffset
= curAtomOffset
;
3104 entry
.info
.fileName
= filename
;
3105 entry
.info
.lineNumber
= result
.line
;
3106 //fprintf(stderr, "addr=0x%08llX, line=%lld, file=%s, atom=%s, atom.size=0x%X, end=%d\n",
3107 // result.pc, result.line, filename, curAtom->name(), curAtomSize, result.end_of_sequence);
3108 entries
.push_back(entry
);
3109 curAtom
->incrementLineInfoCount();
3111 if ( result
.end_of_sequence
) {
3121 // assign line info start offset for each atom
3122 uint8_t* p
= _file
->_atomsArray
;
3123 uint32_t liOffset
= 0;
3124 for(int i
=_file
->_atomsArrayCount
; i
> 0; --i
) {
3125 Atom
<A
>* atom
= (Atom
<A
>*)p
;
3126 atom
->_lineInfoStartIndex
= liOffset
;
3127 liOffset
+= atom
->_lineInfoCount
;
3128 atom
->_lineInfoCount
= 0;
3129 p
+= sizeof(Atom
<A
>);
3131 assert(liOffset
== entries
.size());
3132 _file
->_lineInfos
.reserve(liOffset
);
3134 // copy each line info for each atom
3135 for (typename
std::vector
<AtomAndLineInfo
<A
> >::iterator it
= entries
.begin(); it
!= entries
.end(); ++it
) {
3136 uint32_t slot
= it
->atom
->_lineInfoStartIndex
+ it
->atom
->_lineInfoCount
;
3137 _file
->_lineInfos
[slot
] = it
->info
;
3138 it
->atom
->_lineInfoCount
++;
3141 // done with temp vector
3145 template <typename A
>
3146 void Parser
<A
>::parseStabs()
3148 // scan symbol table for stabs entries
3149 Atom
<A
>* currentAtom
= NULL
;
3150 pint_t currentAtomAddress
= 0;
3151 enum { start
, inBeginEnd
, inFun
} state
= start
;
3152 for (uint32_t symbolIndex
= 0; symbolIndex
< _symbolCount
; ++symbolIndex
) {
3153 const macho_nlist
<P
>& sym
= this->symbolFromIndex(symbolIndex
);
3154 bool useStab
= true;
3155 uint8_t type
= sym
.n_type();
3156 const char* symString
= (sym
.n_strx() != 0) ? this->nameFromSymbol(sym
) : NULL
;
3157 if ( (type
& N_STAB
) != 0 ) {
3158 _file
->_debugInfoKind
= (_hasUUID
? ld::relocatable::File::kDebugInfoStabsUUID
: ld::relocatable::File::kDebugInfoStabs
);
3159 ld::relocatable::File::Stab stab
;
3162 stab
.other
= sym
.n_sect();
3163 stab
.desc
= sym
.n_desc();
3164 stab
.value
= sym
.n_value();
3170 // beginning of function block
3172 // fall into case to lookup atom by addresss
3175 currentAtomAddress
= sym
.n_value();
3176 currentAtom
= this->findAtomByAddress(currentAtomAddress
);
3177 if ( currentAtom
!= NULL
) {
3178 stab
.atom
= currentAtom
;
3179 stab
.string
= symString
;
3182 fprintf(stderr
, "can't find atom for stabs BNSYM at %08llX in %s",
3183 (uint64_t)sym
.n_value(), _path
);
3192 // not associated with an atom, just copy
3193 stab
.string
= symString
;
3197 // n_value field is NOT atom address ;-(
3198 // need to find atom by name match
3199 const char* colon
= strchr(symString
, ':');
3200 if ( colon
!= NULL
) {
3201 // build underscore leading name
3202 int nameLen
= colon
- symString
;
3203 char symName
[nameLen
+2];
3204 strlcpy(&symName
[1], symString
, nameLen
+1);
3206 symName
[nameLen
+1] = '\0';
3207 currentAtom
= this->findAtomByName(symName
);
3208 if ( currentAtom
!= NULL
) {
3209 stab
.atom
= currentAtom
;
3210 stab
.string
= symString
;
3214 // might be a debug-note without trailing :G()
3215 currentAtom
= this->findAtomByName(symString
);
3216 if ( currentAtom
!= NULL
) {
3217 stab
.atom
= currentAtom
;
3218 stab
.string
= symString
;
3221 if ( stab
.atom
== NULL
) {
3222 // ld_classic added bogus GSYM stabs for old style dtrace probes
3223 if ( (strncmp(symString
, "__dtrace_probe$", 15) != 0) )
3224 warning("can't find atom for N_GSYM stabs %s in %s", symString
, _path
);
3230 if ( isConstFunStabs(symString
) ) {
3231 // constant not associated with a function
3232 stab
.string
= symString
;
3235 // old style stabs without BNSYM
3237 currentAtomAddress
= sym
.n_value();
3238 currentAtom
= this->findAtomByAddress(currentAtomAddress
);
3239 if ( currentAtom
!= NULL
) {
3240 stab
.atom
= currentAtom
;
3241 stab
.string
= symString
;
3244 warning("can't find atom for stabs FUN at %08llX in %s",
3245 (uint64_t)currentAtomAddress
, _path
);
3251 stab
.string
= symString
;
3257 stab
.string
= symString
;
3258 // -gfull built .o file
3261 warning("unknown stabs type 0x%X in %s", type
, _path
);
3265 stab
.atom
= currentAtom
;
3274 Atom
<A
>* nestedAtom
= this->findAtomByAddress(sym
.n_value());
3275 if ( nestedAtom
!= NULL
) {
3276 stab
.atom
= nestedAtom
;
3277 stab
.string
= symString
;
3280 warning("can't find atom for stabs 0x%X at %08llX in %s",
3281 type
, (uint64_t)sym
.n_value(), _path
);
3288 // adjust value to be offset in atom
3289 stab
.value
-= currentAtomAddress
;
3291 stab
.string
= symString
;
3298 if ( isConstFunStabs(symString
) ) {
3299 stab
.atom
= currentAtom
;
3300 stab
.string
= symString
;
3303 if ( sym
.n_sect() != 0 ) {
3304 // found another start stab, must be really old stabs...
3305 currentAtomAddress
= sym
.n_value();
3306 currentAtom
= this->findAtomByAddress(currentAtomAddress
);
3307 if ( currentAtom
!= NULL
) {
3308 stab
.atom
= currentAtom
;
3309 stab
.string
= symString
;
3312 warning("can't find atom for stabs FUN at %08llX in %s",
3313 (uint64_t)currentAtomAddress
, _path
);
3317 // found ending stab, switch back to start state
3318 stab
.string
= symString
;
3319 stab
.atom
= currentAtom
;
3328 // adjust value to be offset in atom
3329 stab
.value
-= currentAtomAddress
;
3330 stab
.atom
= currentAtom
;
3333 stab
.string
= symString
;
3337 stab
.atom
= currentAtom
;
3338 stab
.string
= symString
;
3343 // add to list of stabs for this .o file
3345 _file
->_stabs
.push_back(stab
);
3352 // Look at the compilation unit DIE and determine
3353 // its NAME, compilation directory (in COMP_DIR) and its
3354 // line number information offset (in STMT_LIST). NAME and COMP_DIR
3355 // may be NULL (especially COMP_DIR) if they are not in the .o file;
3356 // STMT_LIST will be (uint64_t) -1.
3358 // At present this assumes that there's only one compilation unit DIE.
3360 template <typename A
>
3361 bool Parser
<A
>::read_comp_unit(const char ** name
, const char ** comp_dir
,
3362 uint64_t *stmt_list
)
3364 const uint8_t * debug_info
;
3365 const uint8_t * debug_abbrev
;
3368 const uint8_t * end
;
3369 const uint8_t * enda
;
3372 uint64_t abbrev_base
;
3374 uint8_t address_size
;
3379 *stmt_list
= (uint64_t) -1;
3381 if ( (_file
->_dwarfDebugInfoSect
== NULL
) || (_file
->_dwarfDebugAbbrevSect
== NULL
) )
3384 debug_info
= (uint8_t*)_file
->fileContent() + _file
->_dwarfDebugInfoSect
->offset();
3385 debug_abbrev
= (uint8_t*)_file
->fileContent() + _file
->_dwarfDebugAbbrevSect
->offset();
3388 if (_file
->_dwarfDebugInfoSect
->size() < 12)
3389 /* Too small to be a real debug_info section. */
3391 sz
= A::P::E::get32(*(uint32_t*)di
);
3393 dwarf64
= sz
== 0xffffffff;
3395 sz
= A::P::E::get64(*(uint64_t*)di
), di
+= 8;
3396 else if (sz
> 0xffffff00)
3397 /* Unknown dwarf format. */
3400 /* Verify claimed size. */
3401 if (sz
+ (di
- debug_info
) > _file
->_dwarfDebugInfoSect
->size() || sz
<= (dwarf64
? 23 : 11))
3404 vers
= A::P::E::get16(*(uint16_t*)di
);
3405 if (vers
< 2 || vers
> 3)
3406 /* DWARF version wrong for this code.
3407 Chances are we could continue anyway, but we don't know for sure. */
3411 /* Find the debug_abbrev section. */
3412 abbrev_base
= dwarf64
? A::P::E::get64(*(uint64_t*)di
) : A::P::E::get32(*(uint32_t*)di
);
3413 di
+= dwarf64
? 8 : 4;
3415 if (abbrev_base
> _file
->_dwarfDebugAbbrevSect
->size())
3417 da
= debug_abbrev
+ abbrev_base
;
3418 enda
= debug_abbrev
+ _file
->_dwarfDebugAbbrevSect
->size();
3420 address_size
= *di
++;
3422 /* Find the abbrev number we're looking for. */
3424 abbrev
= read_uleb128 (&di
, end
);
3425 if (abbrev
== (uint64_t) -1)
3428 /* Skip through the debug_abbrev section looking for that abbrev. */
3431 uint64_t this_abbrev
= read_uleb128 (&da
, enda
);
3434 if (this_abbrev
== abbrev
)
3435 /* This is almost always taken. */
3437 skip_leb128 (&da
, enda
); /* Skip the tag. */
3440 da
++; /* Skip the DW_CHILDREN_* value. */
3443 attr
= read_uleb128 (&da
, enda
);
3444 skip_leb128 (&da
, enda
);
3445 } while (attr
!= 0 && attr
!= (uint64_t) -1);
3450 /* Check that the abbrev is one for a DW_TAG_compile_unit. */
3451 if (read_uleb128 (&da
, enda
) != DW_TAG_compile_unit
)
3455 da
++; /* Skip the DW_CHILDREN_* value. */
3457 /* Now, go through the DIE looking for DW_AT_name,
3458 DW_AT_comp_dir, and DW_AT_stmt_list. */
3461 uint64_t attr
= read_uleb128 (&da
, enda
);
3462 uint64_t form
= read_uleb128 (&da
, enda
);
3464 if (attr
== (uint64_t) -1)
3469 if (form
== DW_FORM_indirect
)
3470 form
= read_uleb128 (&di
, end
);
3472 if (attr
== DW_AT_name
)
3473 *name
= getDwarfString(form
, di
);
3474 else if (attr
== DW_AT_comp_dir
)
3475 *comp_dir
= getDwarfString(form
, di
);
3476 else if (attr
== DW_AT_stmt_list
&& form
== DW_FORM_data4
)
3477 *stmt_list
= A::P::E::get32(*(uint32_t*)di
);
3478 else if (attr
== DW_AT_stmt_list
&& form
== DW_FORM_data8
)
3479 *stmt_list
= A::P::E::get64(*(uint64_t*)di
);
3480 if (! skip_form (&di
, end
, form
, address_size
, dwarf64
))
3487 template <typename A
>
3490 free(_sectionsArray
);
3494 template <typename A
>
3495 const char* File
<A
>::translationUnitSource() const
3497 return _dwarfTranslationUnitPath
;
3502 template <typename A
>
3503 bool File
<A
>::forEachAtom(ld::File::AtomHandler
& handler
) const
3505 handler
.doFile(*this);
3506 uint8_t* p
= _atomsArray
;
3507 for(int i
=_atomsArrayCount
; i
> 0; --i
) {
3508 handler
.doAtom(*((Atom
<A
>*)p
));
3509 p
+= sizeof(Atom
<A
>);
3511 return (_atomsArrayCount
!= 0);
3514 template <typename A
>
3515 const char* Section
<A
>::makeSegmentName(const macho_section
<typename
A::P
>* sect
)
3517 // mach-o section record only has room for 16-byte seg/sect names
3518 // so a 16-byte name has no trailing zero
3519 const char* name
= sect
->segname();
3520 if ( strlen(name
) < 16 )
3522 char* tmp
= new char[17];
3523 strlcpy(tmp
, name
, 17);
3527 template <typename A
>
3528 const char* Section
<A
>::makeSectionName(const macho_section
<typename
A::P
>* sect
)
3530 const char* name
= sect
->sectname();
3531 if ( strlen(name
) < 16 )
3534 // special case common long section names so we don't have to malloc
3535 if ( strncmp(sect
->sectname(), "__objc_classrefs", 16) == 0 )
3536 return "__objc_classrefs";
3537 if ( strncmp(sect
->sectname(), "__objc_classlist", 16) == 0 )
3538 return "__objc_classlist";
3539 if ( strncmp(sect
->sectname(), "__objc_nlclslist", 16) == 0 )
3540 return "__objc_nlclslist";
3541 if ( strncmp(sect
->sectname(), "__objc_nlcatlist", 16) == 0 )
3542 return "__objc_nlcatlist";
3543 if ( strncmp(sect
->sectname(), "__objc_protolist", 16) == 0 )
3544 return "__objc_protolist";
3545 if ( strncmp(sect
->sectname(), "__objc_protorefs", 16) == 0 )
3546 return "__objc_protorefs";
3547 if ( strncmp(sect
->sectname(), "__objc_superrefs", 16) == 0 )
3548 return "__objc_superrefs";
3549 if ( strncmp(sect
->sectname(), "__objc_imageinfo", 16) == 0 )
3550 return "__objc_imageinfo";
3551 if ( strncmp(sect
->sectname(), "__objc_stringobj", 16) == 0 )
3552 return "__objc_stringobj";
3553 if ( strncmp(sect
->sectname(), "__gcc_except_tab", 16) == 0 )
3554 return "__gcc_except_tab";
3556 char* tmp
= new char[17];
3557 strlcpy(tmp
, name
, 17);
3561 template <typename A
>
3562 bool Section
<A
>::readable(const macho_section
<typename
A::P
>* sect
)
3567 template <typename A
>
3568 bool Section
<A
>::writable(const macho_section
<typename
A::P
>* sect
)
3570 // mach-o .o files do not contain segment permissions
3571 // we just know TEXT is special
3572 return ( strcmp(sect
->segname(), "__TEXT") != 0 );
3575 template <typename A
>
3576 bool Section
<A
>::exectuable(const macho_section
<typename
A::P
>* sect
)
3578 // mach-o .o files do not contain segment permissions
3579 // we just know TEXT is special
3580 return ( strcmp(sect
->segname(), "__TEXT") == 0 );
3584 template <typename A
>
3585 ld::Section::Type Section
<A
>::sectionType(const macho_section
<typename
A::P
>* sect
)
3587 switch ( sect
->flags() & SECTION_TYPE
) {
3589 return ld::Section::typeZeroFill
;
3590 case S_CSTRING_LITERALS
:
3591 if ( (strcmp(sect
->sectname(), "__cstring") == 0) && (strcmp(sect
->segname(), "__TEXT") == 0) )
3592 return ld::Section::typeCString
;
3594 return ld::Section::typeNonStdCString
;
3595 case S_4BYTE_LITERALS
:
3596 return ld::Section::typeLiteral4
;
3597 case S_8BYTE_LITERALS
:
3598 return ld::Section::typeLiteral8
;
3599 case S_LITERAL_POINTERS
:
3600 return ld::Section::typeCStringPointer
;
3601 case S_NON_LAZY_SYMBOL_POINTERS
:
3602 return ld::Section::typeNonLazyPointer
;
3603 case S_LAZY_SYMBOL_POINTERS
:
3604 return ld::Section::typeLazyPointer
;
3605 case S_SYMBOL_STUBS
:
3606 return ld::Section::typeStub
;
3607 case S_MOD_INIT_FUNC_POINTERS
:
3608 return ld::Section::typeInitializerPointers
;
3609 case S_MOD_TERM_FUNC_POINTERS
:
3610 return ld::Section::typeTerminatorPointers
;
3612 return ld::Section::typeUnclassified
;
3613 case S_16BYTE_LITERALS
:
3614 return ld::Section::typeLiteral16
;
3617 if ( sect
->flags() & S_ATTR_PURE_INSTRUCTIONS
) {
3618 return ld::Section::typeCode
;
3620 else if ( strcmp(sect
->segname(), "__TEXT") == 0 ) {
3621 if ( strcmp(sect
->sectname(), "__eh_frame") == 0 )
3622 return ld::Section::typeCFI
;
3623 else if ( strcmp(sect
->sectname(), "__ustring") == 0 )
3624 return ld::Section::typeUTF16Strings
;
3625 else if ( strcmp(sect
->sectname(), "__textcoal_nt") == 0 )
3626 return ld::Section::typeCode
;
3627 else if ( strcmp(sect
->sectname(), "__StaticInit") == 0 )
3628 return ld::Section::typeCode
;
3629 else if ( strcmp(sect
->sectname(), "__constructor") == 0 )
3630 return ld::Section::typeInitializerPointers
;
3632 else if ( strcmp(sect
->segname(), "__DATA") == 0 ) {
3633 if ( strcmp(sect
->sectname(), "__cfstring") == 0 )
3634 return ld::Section::typeCFString
;
3635 else if ( strcmp(sect
->sectname(), "__dyld") == 0 )
3636 return ld::Section::typeDyldInfo
;
3637 else if ( strcmp(sect
->sectname(), "__program_vars") == 0 )
3638 return ld::Section::typeDyldInfo
;
3639 else if ( strncmp(sect
->sectname(), "__objc_classrefs", 16) == 0 )
3640 return ld::Section::typeObjCClassRefs
;
3641 else if ( strcmp(sect
->sectname(), "__objc_catlist") == 0 )
3642 return ld::Section::typeObjC2CategoryList
;
3644 else if ( strcmp(sect
->segname(), "__OBJC") == 0 ) {
3645 if ( strcmp(sect
->sectname(), "__class") == 0 )
3646 return ld::Section::typeObjC1Classes
;
3649 case S_THREAD_LOCAL_REGULAR
:
3650 return ld::Section::typeTLVInitialValues
;
3651 case S_THREAD_LOCAL_ZEROFILL
:
3652 return ld::Section::typeTLVZeroFill
;
3653 case S_THREAD_LOCAL_VARIABLES
:
3654 return ld::Section::typeTLVDefs
;
3655 case S_THREAD_LOCAL_INIT_FUNCTION_POINTERS
:
3656 return ld::Section::typeTLVInitializerPointers
;
3658 return ld::Section::typeUnclassified
;
3662 template <typename A
>
3663 Atom
<A
>* Section
<A
>::findContentAtomByAddress(pint_t addr
, class Atom
<A
>* start
, class Atom
<A
>* end
)
3665 // do a binary search of atom array
3666 uint32_t atomCount
= end
- start
;
3667 Atom
<A
>* base
= start
;
3668 for (uint32_t n
= atomCount
; n
> 0; n
/= 2) {
3669 Atom
<A
>* pivot
= &base
[n
/2];
3670 pint_t atomStartAddr
= pivot
->_objAddress
;
3671 pint_t atomEndAddr
= atomStartAddr
+ pivot
->_size
;
3672 if ( atomStartAddr
<= addr
) {
3673 // address in normal atom
3674 if (addr
< atomEndAddr
)
3676 // address in "end" label (but not in alias)
3677 if ( (pivot
->_size
== 0) && (addr
== atomEndAddr
) && !pivot
->isAlias() )
3680 if ( addr
>= atomEndAddr
) {
3682 // move base to atom after pivot
3694 template <typename A
>
3695 ld::Atom::Alignment Section
<A
>::alignmentForAddress(pint_t addr
)
3697 const uint32_t sectionAlignment
= this->_machOSection
->align();
3698 return ld::Atom::Alignment(sectionAlignment
, (addr
% (1 << sectionAlignment
)));
3701 template <typename A
>
3702 uint32_t Section
<A
>::sectionNum(class Parser
<A
>& parser
) const
3704 if ( _machOSection
== NULL
)
3707 return 1 + (this->_machOSection
- parser
.firstMachOSection());
3710 // arm does not have zero cost exceptions
3711 template <> uint32_t CFISection
<arm
>::cfiCount() { return 0; }
3713 template <typename A
>
3714 uint32_t CFISection
<A
>::cfiCount()
3716 // create ObjectAddressSpace object for use by libunwind
3717 OAS
oas(*this, (uint8_t*)this->file().fileContent()+this->_machOSection
->offset());
3718 return libunwind::CFI_Parser
<OAS
>::getCFICount(oas
,
3719 this->_machOSection
->addr(), this->_machOSection
->size());
3722 template <typename A
>
3723 void CFISection
<A
>::warnFunc(void* ref
, uint64_t funcAddr
, const char* msg
)
3725 Parser
<A
>* parser
= (Parser
<A
>*)ref
;
3726 if ( ! parser
->convertUnwindInfo() )
3728 if ( funcAddr
!= CFI_INVALID_ADDRESS
) {
3729 // atoms are not constructed yet, so scan symbol table for labels
3730 const char* name
= parser
->scanSymbolTableForAddress(funcAddr
);
3731 warning("could not create compact unwind for %s: %s", name
, msg
);
3734 warning("could not create compact unwind: %s", msg
);
3739 bool CFISection
<x86_64
>::needsRelocating()
3744 template <typename A
>
3745 bool CFISection
<A
>::needsRelocating()
3751 void CFISection
<x86_64
>::cfiParse(class Parser
<x86_64
>& parser
, uint8_t* buffer
,
3752 libunwind::CFI_Atom_Info
<CFISection
<x86_64
>::OAS
>::CFI_Atom_Info cfiArray
[],
3755 // copy __eh_frame data to buffer
3756 memcpy(buffer
, file().fileContent() + this->_machOSection
->offset(), this->_machOSection
->size());
3758 // and apply relocations
3759 const macho_relocation_info
<P
>* relocs
= (macho_relocation_info
<P
>*)(file().fileContent() + this->_machOSection
->reloff());
3760 const macho_relocation_info
<P
>* relocsEnd
= &relocs
[this->_machOSection
->nreloc()];
3761 for (const macho_relocation_info
<P
>* reloc
= relocs
; reloc
< relocsEnd
; ++reloc
) {
3763 switch ( reloc
->r_type() ) {
3764 case X86_64_RELOC_SUBTRACTOR
:
3765 value
= 0 - parser
.symbolFromIndex(reloc
->r_symbolnum()).n_value();
3767 if ( reloc
->r_extern() )
3768 value
+= parser
.symbolFromIndex(reloc
->r_symbolnum()).n_value();
3770 case X86_64_RELOC_UNSIGNED
:
3771 value
= parser
.symbolFromIndex(reloc
->r_symbolnum()).n_value();
3773 case X86_64_RELOC_GOT
:
3774 // this is used for the reference to the personality function in CIEs
3775 // store the symbol number of the personality function for later use as a Fixup
3776 value
= reloc
->r_symbolnum();
3779 fprintf(stderr
, "CFISection::cfiParse() unexpected relocation type at r_address=0x%08X\n", reloc
->r_address());
3784 switch ( reloc
->r_length() ) {
3786 p64
= (uint64_t*)&buffer
[reloc
->r_address()];
3787 E::set64(*p64
, value
+ E::get64(*p64
));
3790 p32
= (uint32_t*)&buffer
[reloc
->r_address()];
3791 E::set32(*p32
, value
+ E::get32(*p32
));
3794 fprintf(stderr
, "CFISection::cfiParse() unexpected relocation size at r_address=0x%08X\n", reloc
->r_address());
3800 // create ObjectAddressSpace object for use by libunwind
3801 OAS
oas(*this, buffer
);
3803 // use libuwind to parse __eh_frame data into array of CFI_Atom_Info
3805 msg
= libunwind::DwarfInstructions
<OAS
, libunwind::Registers_x86_64
>::parseCFIs(
3806 oas
, this->_machOSection
->addr(), this->_machOSection
->size(),
3807 cfiArray
, count
, (void*)&parser
, warnFunc
);
3809 throwf("malformed __eh_frame section: %s", msg
);
3813 void CFISection
<x86
>::cfiParse(class Parser
<x86
>& parser
, uint8_t* buffer
,
3814 libunwind::CFI_Atom_Info
<CFISection
<x86
>::OAS
>::CFI_Atom_Info cfiArray
[],
3817 // create ObjectAddressSpace object for use by libunwind
3818 OAS
oas(*this, (uint8_t*)this->file().fileContent()+this->_machOSection
->offset());
3820 // use libuwind to parse __eh_frame data into array of CFI_Atom_Info
3822 msg
= libunwind::DwarfInstructions
<OAS
, libunwind::Registers_x86
>::parseCFIs(
3823 oas
, this->_machOSection
->addr(), this->_machOSection
->size(),
3824 cfiArray
, count
, (void*)&parser
, warnFunc
);
3826 throwf("malformed __eh_frame section: %s", msg
);
3833 void CFISection
<arm
>::cfiParse(class Parser
<arm
>& parser
, uint8_t* buffer
,
3834 libunwind::CFI_Atom_Info
<CFISection
<arm
>::OAS
>::CFI_Atom_Info cfiArray
[],
3837 // arm does not use zero cost exceptions
3843 template <typename A
>
3844 uint32_t CFISection
<A
>::computeAtomCount(class Parser
<A
>& parser
,
3845 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
3846 const struct Parser
<A
>::CFI_CU_InfoArrays
& cfis
)
3848 return cfis
.cfiCount
;
3853 template <typename A
>
3854 uint32_t CFISection
<A
>::appendAtoms(class Parser
<A
>& parser
, uint8_t* p
,
3855 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
3856 const struct Parser
<A
>::CFI_CU_InfoArrays
& cfis
)
3858 this->_beginAtoms
= (Atom
<A
>*)p
;
3859 // walk CFI_Atom_Info array and create atom for each entry
3860 const CFI_Atom_Info
* start
= &cfis
.cfiArray
[0];
3861 const CFI_Atom_Info
* end
= &cfis
.cfiArray
[cfis
.cfiCount
];
3862 for(const CFI_Atom_Info
* a
=start
; a
< end
; ++a
) {
3863 Atom
<A
>* space
= (Atom
<A
>*)p
;
3864 new (space
) Atom
<A
>(*this, (a
->isCIE
? "CIE" : "FDE"), a
->address
, a
->size
,
3865 ld::Atom::definitionRegular
, ld::Atom::combineNever
, ld::Atom::scopeTranslationUnit
,
3866 ld::Atom::typeCFI
, ld::Atom::symbolTableNotInFinalLinkedImages
,
3867 false, false, false, ld::Atom::Alignment(0));
3868 p
+= sizeof(Atom
<A
>);
3870 this->_endAtoms
= (Atom
<A
>*)p
;
3871 return cfis
.cfiCount
;
3875 template <> bool CFISection
<x86_64
>::bigEndian() { return false; }
3876 template <> bool CFISection
<x86
>::bigEndian() { return false; }
3877 template <> bool CFISection
<arm
>::bigEndian() { return false; }
3881 void CFISection
<x86_64
>::addCiePersonalityFixups(class Parser
<x86_64
>& parser
, const CFI_Atom_Info
* cieInfo
)
3883 uint8_t personalityEncoding
= cieInfo
->u
.cieInfo
.personality
.encodingOfTargetAddress
;
3884 if ( personalityEncoding
== 0x9B ) {
3885 // compiler always produces X86_64_RELOC_GOT with addend of 4 to personality function
3886 // CFISection<x86_64>::cfiParse() set targetAddress to be symbolIndex + 4 + addressInCIE
3887 uint32_t symbolIndex
= cieInfo
->u
.cieInfo
.personality
.targetAddress
- 4
3888 - cieInfo
->address
- cieInfo
->u
.cieInfo
.personality
.offsetInCFI
;
3889 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(symbolIndex
);
3890 const char* personalityName
= parser
.nameFromSymbol(sym
);
3892 Atom
<x86_64
>* cieAtom
= this->findAtomByAddress(cieInfo
->address
);
3893 Parser
<x86_64
>::SourceLocation
src(cieAtom
, cieInfo
->u
.cieInfo
.personality
.offsetInCFI
);
3894 parser
.addFixup(src
, ld::Fixup::k1of3
, ld::Fixup::kindSetTargetAddress
, false, personalityName
);
3895 parser
.addFixup(src
, ld::Fixup::k2of3
, ld::Fixup::kindAddAddend
, 4);
3896 parser
.addFixup(src
, ld::Fixup::k3of3
, ld::Fixup::kindStoreX86PCRel32GOT
);
3898 else if ( personalityEncoding
!= 0 ) {
3899 throwf("unsupported address encoding (%02X) of personality function in CIE",
3900 personalityEncoding
);
3905 void CFISection
<x86
>::addCiePersonalityFixups(class Parser
<x86
>& parser
, const CFI_Atom_Info
* cieInfo
)
3907 uint8_t personalityEncoding
= cieInfo
->u
.cieInfo
.personality
.encodingOfTargetAddress
;
3908 if ( (personalityEncoding
== 0x9B) || (personalityEncoding
== 0x90) ) {
3909 uint32_t offsetInCFI
= cieInfo
->u
.cieInfo
.personality
.offsetInCFI
;
3910 uint32_t nlpAddr
= cieInfo
->u
.cieInfo
.personality
.targetAddress
;
3911 Atom
<x86
>* cieAtom
= this->findAtomByAddress(cieInfo
->address
);
3912 Atom
<x86
>* nlpAtom
= parser
.findAtomByAddress(nlpAddr
);
3913 assert(nlpAtom
->contentType() == ld::Atom::typeNonLazyPointer
);
3914 Parser
<x86
>::SourceLocation
src(cieAtom
, cieInfo
->u
.cieInfo
.personality
.offsetInCFI
);
3916 parser
.addFixup(src
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, nlpAtom
);
3917 parser
.addFixup(src
, ld::Fixup::k2of4
, ld::Fixup::kindSubtractTargetAddress
, cieAtom
);
3918 parser
.addFixup(src
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractAddend
, offsetInCFI
);
3919 parser
.addFixup(src
, ld::Fixup::k4of4
, ld::Fixup::kindStoreLittleEndian32
);
3921 else if ( personalityEncoding
!= 0 ) {
3922 throwf("unsupported address encoding (%02X) of personality function in CIE", personalityEncoding
);
3927 template <typename A
>
3928 void CFISection
<A
>::addCiePersonalityFixups(class Parser
<A
>& parser
, const CFI_Atom_Info
* cieInfo
)
3934 template <typename A
>
3935 void CFISection
<A
>::makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
& cfis
)
3937 ld::Fixup::Kind store32
= bigEndian() ? ld::Fixup::kindStoreBigEndian32
: ld::Fixup::kindStoreLittleEndian32
;
3938 ld::Fixup::Kind store64
= bigEndian() ? ld::Fixup::kindStoreBigEndian64
: ld::Fixup::kindStoreLittleEndian64
;
3940 // add all references for FDEs, including implicit group references
3941 const CFI_Atom_Info
* end
= &cfis
.cfiArray
[cfis
.cfiCount
];
3942 for(const CFI_Atom_Info
* p
= &cfis
.cfiArray
[0]; p
< end
; ++p
) {
3944 // add reference to personality function if used
3945 if ( p
->u
.cieInfo
.personality
.targetAddress
!= CFI_INVALID_ADDRESS
) {
3946 this->addCiePersonalityFixups(parser
, p
);
3951 Atom
<A
>* fdeAtom
= this->findAtomByAddress(p
->address
);
3952 // find function Atom
3953 Atom
<A
>* functionAtom
= parser
.findAtomByAddress(p
->u
.fdeInfo
.function
.targetAddress
);
3955 Atom
<A
>* cieAtom
= this->findAtomByAddress(p
->u
.fdeInfo
.cie
.targetAddress
);
3957 Atom
<A
>* lsdaAtom
= NULL
;
3958 if ( p
->u
.fdeInfo
.lsda
.targetAddress
!= CFI_INVALID_ADDRESS
) {
3959 lsdaAtom
= parser
.findAtomByAddress(p
->u
.fdeInfo
.lsda
.targetAddress
);
3961 // add reference from FDE to CIE (always 32-bit pc-rel)
3962 typename Parser
<A
>::SourceLocation
fdeToCieSrc(fdeAtom
, p
->u
.fdeInfo
.cie
.offsetInCFI
);
3963 parser
.addFixup(fdeToCieSrc
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, fdeAtom
);
3964 parser
.addFixup(fdeToCieSrc
, ld::Fixup::k2of4
, ld::Fixup::kindAddAddend
, p
->u
.fdeInfo
.cie
.offsetInCFI
);
3965 parser
.addFixup(fdeToCieSrc
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractTargetAddress
, cieAtom
);
3966 parser
.addFixup(fdeToCieSrc
, ld::Fixup::k4of4
, store32
, cieAtom
);
3968 // add reference from FDE to function
3969 typename Parser
<A
>::SourceLocation
fdeToFuncSrc(fdeAtom
, p
->u
.fdeInfo
.function
.offsetInCFI
);
3970 switch (p
->u
.fdeInfo
.function
.encodingOfTargetAddress
) {
3971 case DW_EH_PE_pcrel
|DW_EH_PE_ptr
:
3972 if ( sizeof(typename
A::P::uint_t
) == 8 ) {
3973 parser
.addFixup(fdeToFuncSrc
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, functionAtom
);
3974 parser
.addFixup(fdeToFuncSrc
, ld::Fixup::k2of4
, ld::Fixup::kindSubtractTargetAddress
, fdeAtom
);
3975 parser
.addFixup(fdeToFuncSrc
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractAddend
, p
->u
.fdeInfo
.function
.offsetInCFI
);
3976 parser
.addFixup(fdeToFuncSrc
, ld::Fixup::k4of4
, store64
);
3979 // else fall into 32-bit case
3980 case DW_EH_PE_pcrel
|DW_EH_PE_sdata4
:
3981 parser
.addFixup(fdeToFuncSrc
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, functionAtom
);
3982 parser
.addFixup(fdeToFuncSrc
, ld::Fixup::k2of4
, ld::Fixup::kindSubtractTargetAddress
, fdeAtom
);
3983 parser
.addFixup(fdeToFuncSrc
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractAddend
, p
->u
.fdeInfo
.function
.offsetInCFI
);
3984 parser
.addFixup(fdeToFuncSrc
, ld::Fixup::k4of4
, store32
);
3987 throw "unsupported encoding in FDE of pointer to function";
3990 // add reference from FDE to LSDA
3991 typename Parser
<A
>::SourceLocation
fdeToLsdaSrc(fdeAtom
, p
->u
.fdeInfo
.lsda
.offsetInCFI
);
3992 if ( lsdaAtom
!= NULL
) {
3993 switch (p
->u
.fdeInfo
.lsda
.encodingOfTargetAddress
) {
3994 case DW_EH_PE_pcrel
|DW_EH_PE_ptr
:
3995 if ( sizeof(typename
A::P::uint_t
) == 8 ) {
3996 parser
.addFixup(fdeToLsdaSrc
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, lsdaAtom
);
3997 parser
.addFixup(fdeToLsdaSrc
, ld::Fixup::k2of4
, ld::Fixup::kindSubtractTargetAddress
, fdeAtom
);
3998 parser
.addFixup(fdeToLsdaSrc
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractAddend
, p
->u
.fdeInfo
.lsda
.offsetInCFI
);
3999 parser
.addFixup(fdeToLsdaSrc
, ld::Fixup::k4of4
, store64
);
4002 // else fall into 32-bit case
4003 case DW_EH_PE_pcrel
|DW_EH_PE_sdata4
:
4004 parser
.addFixup(fdeToLsdaSrc
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, lsdaAtom
);
4005 parser
.addFixup(fdeToLsdaSrc
, ld::Fixup::k2of4
, ld::Fixup::kindSubtractTargetAddress
, fdeAtom
);
4006 parser
.addFixup(fdeToLsdaSrc
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractAddend
, p
->u
.fdeInfo
.lsda
.offsetInCFI
);
4007 parser
.addFixup(fdeToLsdaSrc
, ld::Fixup::k4of4
, store32
);
4010 throw "unsupported encoding in FDE of pointer to LSDA";
4014 // FDE is in group lead by function atom
4015 typename Parser
<A
>::SourceLocation
fdeSrc(functionAtom
,0);
4016 parser
.addFixup(fdeSrc
, ld::Fixup::k1of1
, ld::Fixup::kindNoneGroupSubordinateFDE
, fdeAtom
);
4018 // LSDA is in group lead by function atom
4019 if ( lsdaAtom
!= NULL
) {
4020 parser
.addFixup(fdeSrc
, ld::Fixup::k1of1
, ld::Fixup::kindNoneGroupSubordinateLSDA
, lsdaAtom
);
4029 template <typename A
>
4030 const void* CFISection
<A
>::OAS::mappedAddress(pint_t addr
)
4032 if ( (_ehFrameStartAddr
<= addr
) && (addr
< _ehFrameEndAddr
) )
4033 return &_ehFrameContent
[addr
-_ehFrameStartAddr
];
4035 // requested bytes are not in __eh_frame section
4036 // this can occur when examining the instruction bytes in the __text
4037 File
<A
>& file
= _ehFrameSection
.file();
4038 for (uint32_t i
=0; i
< file
._sectionsArrayCount
; ++i
) {
4039 const macho_section
<typename
A::P
>* sect
= file
._sectionsArray
[i
]->machoSection();
4040 // TentativeDefinitionSection and AbsoluteSymbolSection have no mach-o section
4041 if ( sect
!= NULL
) {
4042 if ( (sect
->addr() <= addr
) && (addr
< (sect
->addr()+sect
->size())) ) {
4043 return file
.fileContent() + sect
->offset() + addr
- sect
->addr();
4047 throwf("__eh_frame parsing problem. Can't find target of reference to address 0x%08llX", (uint64_t)addr
);
4052 template <typename A
>
4053 uint64_t CFISection
<A
>::OAS::getULEB128(pint_t
& logicalAddr
, pint_t end
)
4055 uintptr_t size
= (end
- logicalAddr
);
4056 libunwind::LocalAddressSpace::pint_t laddr
= (libunwind::LocalAddressSpace::pint_t
)mappedAddress(logicalAddr
);
4057 libunwind::LocalAddressSpace::pint_t sladdr
= laddr
;
4058 uint64_t result
= libunwind::LocalAddressSpace::getULEB128(laddr
, laddr
+size
);
4059 logicalAddr
+= (laddr
-sladdr
);
4063 template <typename A
>
4064 int64_t CFISection
<A
>::OAS::getSLEB128(pint_t
& logicalAddr
, pint_t end
)
4066 uintptr_t size
= (end
- logicalAddr
);
4067 libunwind::LocalAddressSpace::pint_t laddr
= (libunwind::LocalAddressSpace::pint_t
)mappedAddress(logicalAddr
);
4068 libunwind::LocalAddressSpace::pint_t sladdr
= laddr
;
4069 int64_t result
= libunwind::LocalAddressSpace::getSLEB128(laddr
, laddr
+size
);
4070 logicalAddr
+= (laddr
-sladdr
);
4074 template <typename A
>
4075 typename
A::P::uint_t CFISection
<A
>::OAS::getEncodedP(pint_t
& addr
, pint_t end
, uint8_t encoding
)
4077 pint_t startAddr
= addr
;
4082 switch (encoding
& 0x0F) {
4084 result
= getP(addr
);
4085 p
+= sizeof(pint_t
);
4088 case DW_EH_PE_uleb128
:
4089 result
= getULEB128(addr
, end
);
4091 case DW_EH_PE_udata2
:
4092 result
= get16(addr
);
4096 case DW_EH_PE_udata4
:
4097 result
= get32(addr
);
4101 case DW_EH_PE_udata8
:
4102 result
= get64(addr
);
4106 case DW_EH_PE_sleb128
:
4107 result
= getSLEB128(addr
, end
);
4109 case DW_EH_PE_sdata2
:
4110 result
= (int16_t)get16(addr
);
4114 case DW_EH_PE_sdata4
:
4115 result
= (int32_t)get32(addr
);
4119 case DW_EH_PE_sdata8
:
4120 result
= get64(addr
);
4125 throwf("ObjectFileAddressSpace<A>::getEncodedP() encoding 0x%08X not supported", encoding
);
4128 // then add relative offset
4129 switch ( encoding
& 0x70 ) {
4130 case DW_EH_PE_absptr
:
4133 case DW_EH_PE_pcrel
:
4134 result
+= startAddr
;
4136 case DW_EH_PE_textrel
:
4137 throw "DW_EH_PE_textrel pointer encoding not supported";
4139 case DW_EH_PE_datarel
:
4140 throw "DW_EH_PE_datarel pointer encoding not supported";
4142 case DW_EH_PE_funcrel
:
4143 throw "DW_EH_PE_funcrel pointer encoding not supported";
4145 case DW_EH_PE_aligned
:
4146 throw "DW_EH_PE_aligned pointer encoding not supported";
4149 throwf("ObjectFileAddressSpace<A>::getEncodedP() encoding 0x%08X not supported", encoding
);
4153 // Note: DW_EH_PE_indirect is only used in CIEs to refernce the personality pointer
4154 // When parsing .o files that pointer contains zero, so we don't to return that.
4155 // Instead we skip the dereference and return the address of the pointer.
4156 // if ( encoding & DW_EH_PE_indirect )
4157 // result = getP(result);
4163 const char* CUSection
<x86_64
>::personalityName(class Parser
<x86_64
>& parser
, const macho_relocation_info
<x86_64::P
>* reloc
)
4165 assert(reloc
->r_extern() && "reloc not extern on personality column in __compact_unwind section");
4166 assert((reloc
->r_type() == X86_64_RELOC_UNSIGNED
) && "wrong reloc type on personality column in __compact_unwind section");
4167 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(reloc
->r_symbolnum());
4168 return parser
.nameFromSymbol(sym
);
4172 const char* CUSection
<x86
>::personalityName(class Parser
<x86
>& parser
, const macho_relocation_info
<x86::P
>* reloc
)
4174 assert(reloc
->r_extern() && "reloc not extern on personality column in __compact_unwind section");
4175 assert((reloc
->r_type() == GENERIC_RELOC_VANILLA
) && "wrong reloc type on personality column in __compact_unwind section");
4176 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(reloc
->r_symbolnum());
4177 return parser
.nameFromSymbol(sym
);
4180 template <typename A
>
4181 const char* CUSection
<A
>::personalityName(class Parser
<A
>& parser
, const macho_relocation_info
<P
>* reloc
)
4187 template <typename A
>
4188 int CUSection
<A
>::infoSorter(const void* l
, const void* r
)
4190 // sort references by symbol index, then address
4191 const Info
* left
= (Info
*)l
;
4192 const Info
* right
= (Info
*)r
;
4193 if ( left
->functionSymbolIndex
== right
->functionSymbolIndex
)
4194 return (left
->functionStartAddress
- right
->functionStartAddress
);
4196 return (left
->functionSymbolIndex
- right
->functionSymbolIndex
);
4199 template <typename A
>
4200 void CUSection
<A
>::parse(class Parser
<A
>& parser
, uint32_t cnt
, Info array
[])
4202 // walk section content and copy to Info array
4203 const macho_compact_unwind_entry
<P
>* const entries
= (macho_compact_unwind_entry
<P
>*)(this->file().fileContent() + this->_machOSection
->offset());
4204 for (uint32_t i
=0; i
< cnt
; ++i
) {
4205 Info
* info
= &array
[i
];
4206 const macho_compact_unwind_entry
<P
>* entry
= &entries
[i
];
4207 info
->functionStartAddress
= entry
->codeStart();
4208 info
->functionSymbolIndex
= 0xFFFFFFFF;
4209 info
->rangeLength
= entry
->codeLen();
4210 info
->compactUnwindInfo
= entry
->compactUnwindInfo();
4211 info
->personality
= NULL
;
4212 info
->lsdaAddress
= entry
->lsda();
4213 info
->function
= NULL
;
4215 if ( (info
->compactUnwindInfo
& UNWIND_PERSONALITY_MASK
) != 0 )
4216 warning("no bits should be set in UNWIND_PERSONALITY_MASK of compact unwind encoding in __LD,__compact_unwind section");
4217 if ( info
->lsdaAddress
!= 0 ) {
4218 info
->compactUnwindInfo
|= UNWIND_HAS_LSDA
;
4222 // scan relocs, local relocs are useless - ignore them
4223 // extern relocs are needed for personality references (possibly for function/lsda refs??)
4224 const macho_relocation_info
<P
>* relocs
= (macho_relocation_info
<P
>*)(this->file().fileContent() + this->_machOSection
->reloff());
4225 const macho_relocation_info
<P
>* relocsEnd
= &relocs
[this->_machOSection
->nreloc()];
4226 for (const macho_relocation_info
<P
>* reloc
= relocs
; reloc
< relocsEnd
; ++reloc
) {
4227 if ( reloc
->r_extern() ) {
4228 // only expect external relocs on some colummns
4229 if ( (reloc
->r_address() % sizeof(macho_compact_unwind_entry
<P
>)) == macho_compact_unwind_entry
<P
>::personalityFieldOffset() ) {
4230 uint32_t entryIndex
= reloc
->r_address() / sizeof(macho_compact_unwind_entry
<P
>);
4231 array
[entryIndex
].personality
= this->personalityName(parser
, reloc
);
4233 else if ( (reloc
->r_address() % sizeof(macho_compact_unwind_entry
<P
>)) == macho_compact_unwind_entry
<P
>::lsdaFieldOffset() ) {
4234 uint32_t entryIndex
= reloc
->r_address() / sizeof(macho_compact_unwind_entry
<P
>);
4235 const macho_nlist
<P
>& lsdaSym
= parser
.symbolFromIndex(reloc
->r_symbolnum());
4236 if ( (lsdaSym
.n_type() & N_TYPE
) == N_SECT
)
4237 array
[entryIndex
].lsdaAddress
= lsdaSym
.n_value();
4239 warning("unexpected extern relocation to lsda in __compact_unwind section");
4241 else if ( (reloc
->r_address() % sizeof(macho_compact_unwind_entry
<P
>)) == macho_compact_unwind_entry
<P
>::codeStartFieldOffset() ) {
4242 uint32_t entryIndex
= reloc
->r_address() / sizeof(macho_compact_unwind_entry
<P
>);
4243 array
[entryIndex
].functionSymbolIndex
= reloc
->r_symbolnum();
4246 warning("unexpected extern relocation in __compact_unwind section");
4251 // sort array by function start address so unwind infos will be contiguous for a given function
4252 ::qsort(array
, cnt
, sizeof(Info
), infoSorter
);
4255 template <typename A
>
4256 uint32_t CUSection
<A
>::count()
4258 const macho_section
<P
>* machoSect
= this->machoSection();
4259 if ( (machoSect
->size() % sizeof(macho_compact_unwind_entry
<P
>)) != 0 )
4260 throw "malformed __LD,__compact_unwind section, bad length";
4262 return machoSect
->size() / sizeof(macho_compact_unwind_entry
<P
>);
4265 template <typename A
>
4266 void CUSection
<A
>::makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
& cus
)
4268 Info
* const arrayStart
= cus
.cuArray
;
4269 Info
* const arrayEnd
= &cus
.cuArray
[cus
.cuCount
];
4270 for (Info
* info
=arrayStart
; info
< arrayEnd
; ++info
) {
4271 // if external reloc was used, real address is symbol n_value + addend
4272 if ( info
->functionSymbolIndex
!= 0xFFFFFFFF )
4273 info
->functionStartAddress
+= parser
.symbolFromIndex(info
->functionSymbolIndex
).n_value();
4274 // find function atom from address
4275 info
->function
= parser
.findAtomByAddress(info
->functionStartAddress
);
4276 // find lsda atom from address
4277 if ( info
->lsdaAddress
!= 0 ) {
4278 info
->lsda
= parser
.findAtomByAddress(info
->lsdaAddress
);
4279 // add lsda subordinate
4280 typename Parser
<A
>::SourceLocation
src(info
->function
, info
->functionStartAddress
- info
->function
->objectAddress());
4281 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindNoneGroupSubordinateLSDA
, info
->lsda
);
4283 if ( info
->personality
!= NULL
) {
4284 // add personality subordinate
4285 typename Parser
<A
>::SourceLocation
src(info
->function
, info
->functionStartAddress
- info
->function
->objectAddress());
4286 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindNoneGroupSubordinatePersonality
, false, info
->personality
);
4292 template <typename A
>
4293 SymboledSection
<A
>::SymboledSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
4294 : Section
<A
>(f
, s
), _type(ld::Atom::typeUnclassified
)
4296 switch ( s
->flags() & SECTION_TYPE
) {
4298 _type
= ld::Atom::typeZeroFill
;
4300 case S_MOD_INIT_FUNC_POINTERS
:
4301 _type
= ld::Atom::typeInitializerPointers
;
4303 case S_MOD_TERM_FUNC_POINTERS
:
4304 _type
= ld::Atom::typeTerminatorPointers
;
4306 case S_THREAD_LOCAL_VARIABLES
:
4307 _type
= ld::Atom::typeTLV
;
4309 case S_THREAD_LOCAL_ZEROFILL
:
4310 _type
= ld::Atom::typeTLVZeroFill
;
4312 case S_THREAD_LOCAL_REGULAR
:
4313 _type
= ld::Atom::typeTLVInitialValue
;
4315 case S_THREAD_LOCAL_INIT_FUNCTION_POINTERS
:
4316 _type
= ld::Atom::typeTLVInitializerPointers
;
4319 if ( strncmp(s
->sectname(), "__gcc_except_tab", 16) == 0 )
4320 _type
= ld::Atom::typeLSDA
;
4321 else if ( this->type() == ld::Section::typeInitializerPointers
)
4322 _type
= ld::Atom::typeInitializerPointers
;
4328 template <typename A
>
4329 bool SymboledSection
<A
>::dontDeadStrip()
4332 case ld::Atom::typeInitializerPointers
:
4333 case ld::Atom::typeTerminatorPointers
:
4336 // model an object file without MH_SUBSECTIONS_VIA_SYMBOLS as one in which nothing can be dead stripped
4337 if ( ! this->_file
.canScatterAtoms() )
4340 return Section
<A
>::dontDeadStrip();
4346 template <typename A
>
4347 uint32_t SymboledSection
<A
>::computeAtomCount(class Parser
<A
>& parser
,
4348 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
4349 const struct Parser
<A
>::CFI_CU_InfoArrays
&)
4351 const pint_t startAddr
= this->_machOSection
->addr();
4352 const pint_t endAddr
= startAddr
+ this->_machOSection
->size();
4353 const uint32_t sectNum
= this->sectionNum(parser
);
4358 const macho_nlist
<P
>* sym
;
4359 while ( it
.next(parser
, sectNum
, startAddr
, endAddr
, &addr
, &size
, &sym
) ) {
4362 //fprintf(stderr, "computeAtomCount(%s,%s) => %d\n", this->segmentName(), this->sectionName(), count);
4366 template <typename A
>
4367 uint32_t SymboledSection
<A
>::appendAtoms(class Parser
<A
>& parser
, uint8_t* p
,
4368 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
4369 const struct Parser
<A
>::CFI_CU_InfoArrays
&)
4371 this->_beginAtoms
= (Atom
<A
>*)p
;
4373 //fprintf(stderr, "SymboledSection::appendAtoms() in section %s\n", this->_machOSection->sectname());
4374 const pint_t startAddr
= this->_machOSection
->addr();
4375 const pint_t endAddr
= startAddr
+ this->_machOSection
->size();
4376 const uint32_t sectNum
= this->sectionNum(parser
);
4381 const macho_nlist
<P
>* label
;
4382 while ( it
.next(parser
, sectNum
, startAddr
, endAddr
, &addr
, &size
, &label
) ) {
4383 Atom
<A
>* allocatedSpace
= (Atom
<A
>*)p
;
4384 // is break because of label or CFI?
4385 if ( label
!= NULL
) {
4386 // The size is computed based on the address of the next label (or the end of the section for the last label)
4387 // If there are two labels at the same address, we want them one to be an alias of the other.
4388 // If the label is at the end of a section, it is has zero size, but is not an alias
4389 const bool isAlias
= ( (size
== 0) && (addr
< endAddr
) );
4390 new (allocatedSpace
) Atom
<A
>(*this, parser
, *label
, size
, isAlias
);
4392 this->_hasAliases
= true;
4395 ld::Atom::SymbolTableInclusion inclusion
= ld::Atom::symbolTableNotIn
;
4396 ld::Atom::ContentType ctype
= this->contentType();
4397 if ( ctype
== ld::Atom::typeLSDA
)
4398 inclusion
= ld::Atom::symbolTableInWithRandomAutoStripLabel
;
4399 new (allocatedSpace
) Atom
<A
>(*this, "anon", addr
, size
, ld::Atom::definitionRegular
, ld::Atom::combineNever
,
4400 ld::Atom::scopeTranslationUnit
, ctype
, inclusion
,
4401 this->dontDeadStrip(), false, false, this->alignmentForAddress(addr
));
4403 p
+= sizeof(Atom
<A
>);
4407 this->_endAtoms
= (Atom
<A
>*)p
;
4412 template <typename A
>
4413 uint32_t ImplicitSizeSection
<A
>::computeAtomCount(class Parser
<A
>& parser
,
4414 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
4415 const struct Parser
<A
>::CFI_CU_InfoArrays
&)
4418 const macho_section
<P
>* sect
= this->machoSection();
4419 const pint_t startAddr
= sect
->addr();
4420 const pint_t endAddr
= startAddr
+ sect
->size();
4421 for (pint_t addr
= startAddr
; addr
< endAddr
; addr
+= elementSizeAtAddress(addr
) ) {
4422 if ( useElementAt(parser
, it
, addr
) )
4425 if ( it
.fileHasOverlappingSymbols
&& (sect
->size() != 0) && (this->combine(parser
, startAddr
) == ld::Atom::combineByNameAndContent
) ) {
4426 // if there are multiple labels in this section for the same address, then clone them into multi atoms
4427 pint_t prevSymbolAddr
= (pint_t
)(-1);
4428 uint8_t prevSymbolSectNum
= 0;
4429 for(uint32_t i
=0; i
< it
.sortedSymbolCount
; ++i
) {
4430 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(it
.sortedSymbolIndexes
[i
]);
4431 const pint_t symbolAddr
= sym
.n_value();
4432 const pint_t symbolSectNum
= sym
.n_sect();
4433 if ( (symbolAddr
== prevSymbolAddr
) && (prevSymbolSectNum
== symbolSectNum
) && (symbolSectNum
== this->sectionNum(parser
)) ) {
4436 prevSymbolAddr
= symbolAddr
;
4437 prevSymbolSectNum
= symbolSectNum
;
4443 template <typename A
>
4444 uint32_t ImplicitSizeSection
<A
>::appendAtoms(class Parser
<A
>& parser
, uint8_t* p
,
4445 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
4446 const struct Parser
<A
>::CFI_CU_InfoArrays
&)
4448 this->_beginAtoms
= (Atom
<A
>*)p
;
4450 const macho_section
<P
>* sect
= this->machoSection();
4451 const pint_t startAddr
= sect
->addr();
4452 const pint_t endAddr
= startAddr
+ sect
->size();
4453 const uint32_t sectNum
= this->sectionNum(parser
);
4454 //fprintf(stderr, "ImplicitSizeSection::appendAtoms() in section %s\n", sect->sectname());
4458 const macho_nlist
<P
>* foundLabel
;
4459 Atom
<A
>* allocatedSpace
;
4460 while ( it
.next(parser
, sectNum
, startAddr
, endAddr
, &foundAddr
, &size
, &foundLabel
) ) {
4461 if ( foundLabel
!= NULL
) {
4462 pint_t labeledAtomSize
= this->elementSizeAtAddress(foundAddr
);
4463 allocatedSpace
= (Atom
<A
>*)p
;
4464 if ( this->ignoreLabel(parser
.nameFromSymbol(*foundLabel
)) ) {
4465 //fprintf(stderr, " 0x%08llX make annon\n", (uint64_t)foundAddr);
4466 new (allocatedSpace
) Atom
<A
>(*this, this->unlabeledAtomName(parser
, foundAddr
), foundAddr
,
4467 this->elementSizeAtAddress(foundAddr
), this->definition(),
4468 this->combine(parser
, foundAddr
), this->scopeAtAddress(parser
, foundAddr
),
4469 this->contentType(), this->symbolTableInclusion(),
4470 this->dontDeadStrip(), false, false, this->alignmentForAddress(foundAddr
));
4473 // make named atom for label
4474 //fprintf(stderr, " 0x%08llX make labeled\n", (uint64_t)foundAddr);
4475 new (allocatedSpace
) Atom
<A
>(*this, parser
, *foundLabel
, labeledAtomSize
);
4478 p
+= sizeof(Atom
<A
>);
4479 foundAddr
+= labeledAtomSize
;
4480 size
-= labeledAtomSize
;
4482 // some number of anonymous atoms
4483 for (pint_t addr
= foundAddr
; addr
< (foundAddr
+size
); addr
+= elementSizeAtAddress(addr
) ) {
4484 // make anon atoms for area before label
4485 if ( this->useElementAt(parser
, it
, addr
) ) {
4486 //fprintf(stderr, " 0x%08llX make annon\n", (uint64_t)addr);
4487 allocatedSpace
= (Atom
<A
>*)p
;
4488 new (allocatedSpace
) Atom
<A
>(*this, this->unlabeledAtomName(parser
, addr
), addr
, this->elementSizeAtAddress(addr
),
4489 this->definition(), this->combine(parser
, addr
), this->scopeAtAddress(parser
, addr
),
4490 this->contentType(), this->symbolTableInclusion(),
4491 this->dontDeadStrip(), false, false, this->alignmentForAddress(addr
));
4493 p
+= sizeof(Atom
<A
>);
4498 this->_endAtoms
= (Atom
<A
>*)p
;
4504 template <typename A
>
4505 unsigned long Literal4Section
<A
>::contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
4507 const uint32_t* literalContent
= (uint32_t*)atom
->contentPointer();
4508 return *literalContent
;
4511 template <typename A
>
4512 bool Literal4Section
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
4513 const ld::IndirectBindingTable
& ind
) const
4515 assert(this->type() == rhs
.section().type());
4516 const uint32_t* literalContent
= (uint32_t*)atom
->contentPointer();
4518 const Atom
<A
>* rhsAtom
= dynamic_cast<const Atom
<A
>*>(&rhs
);
4519 assert(rhsAtom
!= NULL
);
4520 if ( rhsAtom
!= NULL
) {
4521 const uint32_t* rhsLiteralContent
= (uint32_t*)rhsAtom
->contentPointer();
4522 return (*literalContent
== *rhsLiteralContent
);
4528 template <typename A
>
4529 unsigned long Literal8Section
<A
>::contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
4532 const uint64_t* literalContent
= (uint64_t*)atom
->contentPointer();
4533 return *literalContent
;
4535 unsigned long hash
= 5381;
4536 const uint8_t* byteContent
= atom
->contentPointer();
4537 for (int i
=0; i
< 8; ++i
) {
4538 hash
= hash
* 33 + byteContent
[i
];
4544 template <typename A
>
4545 bool Literal8Section
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
4546 const ld::IndirectBindingTable
& ind
) const
4548 if ( rhs
.section().type() != ld::Section::typeLiteral8
)
4550 assert(this->type() == rhs
.section().type());
4551 const uint64_t* literalContent
= (uint64_t*)atom
->contentPointer();
4553 const Atom
<A
>* rhsAtom
= dynamic_cast<const Atom
<A
>*>(&rhs
);
4554 assert(rhsAtom
!= NULL
);
4555 if ( rhsAtom
!= NULL
) {
4556 const uint64_t* rhsLiteralContent
= (uint64_t*)rhsAtom
->contentPointer();
4557 return (*literalContent
== *rhsLiteralContent
);
4563 template <typename A
>
4564 unsigned long Literal16Section
<A
>::contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
4566 unsigned long hash
= 5381;
4567 const uint8_t* byteContent
= atom
->contentPointer();
4568 for (int i
=0; i
< 16; ++i
) {
4569 hash
= hash
* 33 + byteContent
[i
];
4574 template <typename A
>
4575 bool Literal16Section
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
4576 const ld::IndirectBindingTable
& ind
) const
4578 if ( rhs
.section().type() != ld::Section::typeLiteral16
)
4580 assert(this->type() == rhs
.section().type());
4581 const uint64_t* literalContent
= (uint64_t*)atom
->contentPointer();
4583 const Atom
<A
>* rhsAtom
= dynamic_cast<const Atom
<A
>*>(&rhs
);
4584 assert(rhsAtom
!= NULL
);
4585 if ( rhsAtom
!= NULL
) {
4586 const uint64_t* rhsLiteralContent
= (uint64_t*)rhsAtom
->contentPointer();
4587 return ((literalContent
[0] == rhsLiteralContent
[0]) && (literalContent
[1] == rhsLiteralContent
[1]));
4594 template <typename A
>
4595 typename
A::P::uint_t CStringSection
<A
>::elementSizeAtAddress(pint_t addr
)
4597 const macho_section
<P
>* sect
= this->machoSection();
4598 const char* stringContent
= (char*)(this->file().fileContent() + sect
->offset() + addr
- sect
->addr());
4599 return strlen(stringContent
) + 1;
4602 template <typename A
>
4603 bool CStringSection
<A
>::useElementAt(Parser
<A
>& parser
, struct Parser
<A
>::LabelAndCFIBreakIterator
& it
, pint_t addr
)
4608 template <typename A
>
4609 bool CStringSection
<A
>::ignoreLabel(const char* label
)
4611 return (label
[0] == 'L') || (label
[0] == 'l');
4614 template <typename A
>
4615 Atom
<A
>* CStringSection
<A
>::findAtomByAddress(pint_t addr
)
4617 Atom
<A
>* result
= this->findContentAtomByAddress(addr
, this->_beginAtoms
, this->_endAtoms
);
4621 template <typename A
>
4622 unsigned long CStringSection
<A
>::contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
4624 unsigned long hash
= 5381;
4625 const char* stringContent
= (char*)atom
->contentPointer();
4626 for (const char* s
= stringContent
; *s
!= '\0'; ++s
) {
4627 hash
= hash
* 33 + *s
;
4633 template <typename A
>
4634 bool CStringSection
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
4635 const ld::IndirectBindingTable
& ind
) const
4637 if ( rhs
.section().type() != ld::Section::typeCString
)
4639 assert(this->type() == rhs
.section().type());
4640 assert(strcmp(this->sectionName(), rhs
.section().sectionName())== 0);
4641 assert(strcmp(this->segmentName(), rhs
.section().segmentName())== 0);
4642 const char* stringContent
= (char*)atom
->contentPointer();
4644 const Atom
<A
>* rhsAtom
= dynamic_cast<const Atom
<A
>*>(&rhs
);
4645 assert(rhsAtom
!= NULL
);
4646 if ( rhsAtom
!= NULL
) {
4647 if ( atom
->_size
!= rhsAtom
->_size
)
4649 const char* rhsStringContent
= (char*)rhsAtom
->contentPointer();
4650 return (strcmp(stringContent
, rhsStringContent
) == 0);
4657 ld::Fixup::Kind NonLazyPointerSection
<x86
>::fixupKind()
4659 return ld::Fixup::kindStoreLittleEndian32
;
4663 ld::Fixup::Kind NonLazyPointerSection
<arm
>::fixupKind()
4665 return ld::Fixup::kindStoreLittleEndian32
;
4670 void NonLazyPointerSection
<x86_64
>::makeFixups(class Parser
<x86_64
>& parser
, const struct Parser
<x86_64
>::CFI_CU_InfoArrays
&)
4672 assert(0 && "x86_64 should not have non-lazy-pointer sections in .o files");
4675 template <typename A
>
4676 void NonLazyPointerSection
<A
>::makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
&)
4678 // add references for each NLP atom based on indirect symbol table
4679 const macho_section
<P
>* sect
= this->machoSection();
4680 const pint_t endAddr
= sect
->addr() + sect
->size();
4681 for( pint_t addr
= sect
->addr(); addr
< endAddr
; addr
+= sizeof(pint_t
)) {
4682 typename Parser
<A
>::SourceLocation src
;
4683 typename Parser
<A
>::TargetDesc target
;
4684 src
.atom
= this->findAtomByAddress(addr
);
4685 src
.offsetInAtom
= 0;
4686 uint32_t symIndex
= parser
.symbolIndexFromIndirectSectionAddress(addr
, sect
);
4689 target
.weakImport
= false;
4691 if ( symIndex
== INDIRECT_SYMBOL_LOCAL
) {
4692 // use direct reference for local symbols
4693 const pint_t
* nlpContent
= (pint_t
*)(this->file().fileContent() + sect
->offset() + addr
- sect
->addr());
4694 pint_t targetAddr
= P::getP(*nlpContent
);
4695 target
.atom
= parser
.findAtomByAddress(targetAddr
);
4696 target
.weakImport
= false;
4697 target
.addend
= (targetAddr
- target
.atom
->objectAddress());
4698 // <rdar://problem/8385011> if pointer to thumb function, mask of thumb bit (not an addend of +1)
4699 if ( target
.atom
->isThumb() )
4700 target
.addend
&= (-2);
4701 assert(src
.atom
->combine() == ld::Atom::combineNever
);
4704 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(symIndex
);
4705 // use direct reference for local symbols
4706 if ( ((sym
.n_type() & N_TYPE
) == N_SECT
) && ((sym
.n_type() & N_EXT
) == 0) ) {
4707 parser
.findTargetFromAddressAndSectionNum(sym
.n_value(), sym
.n_sect(), target
);
4708 assert(src
.atom
->combine() == ld::Atom::combineNever
);
4711 target
.name
= parser
.nameFromSymbol(sym
);
4712 target
.weakImport
= parser
.weakImportFromSymbol(sym
);
4713 assert(src
.atom
->combine() == ld::Atom::combineByNameAndReferences
);
4716 parser
.addFixups(src
, this->fixupKind(), target
);
4720 template <typename A
>
4721 ld::Atom::Combine NonLazyPointerSection
<A
>::combine(Parser
<A
>& parser
, pint_t addr
)
4723 const macho_section
<P
>* sect
= this->machoSection();
4724 uint32_t symIndex
= parser
.symbolIndexFromIndirectSectionAddress(addr
, sect
);
4725 if ( symIndex
== INDIRECT_SYMBOL_LOCAL
)
4726 return ld::Atom::combineNever
;
4728 // don't coalesce non-lazy-pointers to local symbols
4729 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(symIndex
);
4730 if ( ((sym
.n_type() & N_TYPE
) == N_SECT
) && ((sym
.n_type() & N_EXT
) == 0) )
4731 return ld::Atom::combineNever
;
4733 return ld::Atom::combineByNameAndReferences
;
4736 template <typename A
>
4737 const char* NonLazyPointerSection
<A
>::targetName(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
)
4739 assert(atom
->combine() == ld::Atom::combineByNameAndReferences
);
4740 assert(atom
->fixupCount() == 1);
4741 ld::Fixup::iterator fit
= atom
->fixupsBegin();
4742 const char* name
= NULL
;
4743 switch ( fit
->binding
) {
4744 case ld::Fixup::bindingByNameUnbound
:
4747 case ld::Fixup::bindingByContentBound
:
4748 name
= fit
->u
.target
->name();
4750 case ld::Fixup::bindingsIndirectlyBound
:
4751 name
= ind
.indirectName(fit
->u
.bindingIndex
);
4756 assert(name
!= NULL
);
4760 template <typename A
>
4761 unsigned long NonLazyPointerSection
<A
>::contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
4763 assert(atom
->combine() == ld::Atom::combineByNameAndReferences
);
4764 unsigned long hash
= 9508;
4765 for (const char* s
= this->targetName(atom
, ind
); *s
!= '\0'; ++s
) {
4766 hash
= hash
* 33 + *s
;
4771 template <typename A
>
4772 bool NonLazyPointerSection
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
4773 const ld::IndirectBindingTable
& indirectBindingTable
) const
4775 if ( rhs
.section().type() != ld::Section::typeNonLazyPointer
)
4777 assert(this->type() == rhs
.section().type());
4778 // there can be many non-lazy pointer in different section names
4779 // we only want to coalesce in same section name
4780 if ( *this != rhs
.section() )
4782 const Atom
<A
>* rhsAtom
= dynamic_cast<const Atom
<A
>*>(&rhs
);
4783 assert(rhsAtom
!= NULL
);
4784 const char* thisName
= this->targetName(atom
, indirectBindingTable
);
4785 const char* rhsName
= this->targetName(rhsAtom
, indirectBindingTable
);
4786 return (strcmp(thisName
, rhsName
) == 0);
4789 template <typename A
>
4790 ld::Atom::Scope NonLazyPointerSection
<A
>::scopeAtAddress(Parser
<A
>& parser
, pint_t addr
)
4792 const macho_section
<P
>* sect
= this->machoSection();
4793 uint32_t symIndex
= parser
.symbolIndexFromIndirectSectionAddress(addr
, sect
);
4794 if ( symIndex
== INDIRECT_SYMBOL_LOCAL
)
4795 return ld::Atom::scopeTranslationUnit
;
4797 return ld::Atom::scopeLinkageUnit
;
4801 template <typename A
>
4802 const uint8_t* CFStringSection
<A
>::targetContent(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
,
4803 ContentType
* ct
, unsigned int* count
)
4805 *ct
= contentUnknown
;
4806 for (ld::Fixup::iterator fit
=atom
->fixupsBegin(), end
=atom
->fixupsEnd(); fit
!= end
; ++fit
) {
4807 const ld::Atom
* targetAtom
= NULL
;
4808 switch ( fit
->binding
) {
4809 case ld::Fixup::bindingByNameUnbound
:
4810 // ignore reference to ___CFConstantStringClassReference
4811 // we are just looking for reference to backing string data
4812 assert(fit
->offsetInAtom
== 0);
4813 assert(strcmp(fit
->u
.name
, "___CFConstantStringClassReference") == 0);
4815 case ld::Fixup::bindingDirectlyBound
:
4816 case ld::Fixup::bindingByContentBound
:
4817 targetAtom
= fit
->u
.target
;
4819 case ld::Fixup::bindingsIndirectlyBound
:
4820 targetAtom
= ind
.indirectAtom(fit
->u
.bindingIndex
);
4823 assert(0 && "bad binding type");
4825 assert(targetAtom
!= NULL
);
4826 const Atom
<A
>* target
= dynamic_cast<const Atom
<A
>*>(targetAtom
);
4827 if ( targetAtom
->section().type() == ld::Section::typeCString
) {
4829 *count
= targetAtom
->size();
4831 else if ( targetAtom
->section().type() == ld::Section::typeUTF16Strings
) {
4833 *count
= (targetAtom
->size()+1)/2; // round up incase of buggy compiler that has only one trailing zero byte
4835 assert(target
!= NULL
);
4836 return target
->contentPointer();
4842 template <typename A
>
4843 unsigned long CFStringSection
<A
>::contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
4845 // base hash of CFString on hash of cstring it wraps
4848 unsigned int charCount
;
4849 const uint8_t* content
= this->targetContent(atom
, ind
, &cType
, &charCount
);
4853 for (const char* s
= (char*)content
; *s
!= '\0'; ++s
) {
4854 hash
= hash
* 33 + *s
;
4859 --charCount
; // don't add last 0x0000 to hash because some buggy compilers only have trailing single byte
4860 for (const uint16_t* s
= (uint16_t*)content
; charCount
> 0; ++s
, --charCount
) {
4861 hash
= hash
* 1025 + *s
;
4864 case contentUnknown
:
4871 template <typename A
>
4872 bool CFStringSection
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
4873 const ld::IndirectBindingTable
& indirectBindingTable
) const
4877 if ( rhs
.section().type() != ld::Section::typeCFString
)
4879 assert(this->type() == rhs
.section().type());
4880 assert(strcmp(this->sectionName(), "__cfstring") == 0);
4882 ContentType thisType
;
4883 unsigned int charCount
;
4884 const uint8_t* cstringContent
= this->targetContent(atom
, indirectBindingTable
, &thisType
, &charCount
);
4885 ContentType rhsType
;
4886 const Atom
<A
>* rhsAtom
= dynamic_cast<const Atom
<A
>*>(&rhs
);
4887 assert(rhsAtom
!= NULL
);
4888 unsigned int rhsCharCount
;
4889 const uint8_t* rhsStringContent
= this->targetContent(rhsAtom
, indirectBindingTable
, &rhsType
, &rhsCharCount
);
4891 if ( thisType
!= rhsType
)
4894 // no need to compare content of pointers are already the same
4895 if ( cstringContent
== rhsStringContent
)
4898 // no need to compare content if size is different
4899 if ( charCount
!= rhsCharCount
)
4902 switch ( thisType
) {
4904 return (strcmp((char*)cstringContent
, (char*)rhsStringContent
) == 0);
4907 const uint16_t* cstringContent16
= (uint16_t*)cstringContent
;
4908 const uint16_t* rhsStringContent16
= (uint16_t*)rhsStringContent
;
4909 for (unsigned int i
= 0; i
< charCount
; ++i
) {
4910 if ( cstringContent16
[i
] != rhsStringContent16
[i
] )
4915 case contentUnknown
:
4922 template <typename A
>
4923 typename
A::P::uint_t ObjC1ClassSection
<A
>::elementSizeAtAddress(pint_t addr
)
4925 // nominal size for each class is 48 bytes, but sometimes the compiler
4926 // over aligns and there is padding after class data
4927 const macho_section
<P
>* sct
= this->machoSection();
4928 uint32_t align
= 1 << sct
->align();
4929 uint32_t size
= ((12 * sizeof(pint_t
)) + align
-1) & (-align
);
4933 template <typename A
>
4934 const char* ObjC1ClassSection
<A
>::unlabeledAtomName(Parser
<A
>& parser
, pint_t addr
)
4936 // 8-bytes into class object is pointer to class name
4937 const macho_section
<P
>* sct
= this->machoSection();
4938 uint32_t classObjcFileOffset
= sct
->offset() - sct
->addr() + addr
;
4939 const uint8_t* mappedFileContent
= this->file().fileContent();
4940 pint_t nameAddr
= P::getP(*((pint_t
*)(mappedFileContent
+classObjcFileOffset
+2*sizeof(pint_t
))));
4942 // find section containing string address to get string bytes
4943 const macho_section
<P
>* const sections
= parser
.firstMachOSection();
4944 const uint32_t sectionCount
= parser
.machOSectionCount();
4945 for (uint32_t i
=0; i
< sectionCount
; ++i
) {
4946 const macho_section
<P
>* aSect
= §ions
[i
];
4947 if ( (aSect
->addr() <= nameAddr
) && (nameAddr
< (aSect
->addr()+aSect
->size())) ) {
4948 assert((aSect
->flags() & SECTION_TYPE
) == S_CSTRING_LITERALS
);
4949 uint32_t nameFileOffset
= aSect
->offset() - aSect
->addr() + nameAddr
;
4950 const char* name
= (char*)mappedFileContent
+ nameFileOffset
;
4951 // spin through symbol table to find absolute symbol corresponding to this class
4952 for (uint32_t s
=0; s
< parser
.symbolCount(); ++s
) {
4953 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(s
);
4954 if ( (sym
.n_type() & N_TYPE
) != N_ABS
)
4956 const char* absName
= parser
.nameFromSymbol(sym
);
4957 if ( strncmp(absName
, ".objc_class_name_", 17) == 0 ) {
4958 if ( strcmp(&absName
[17], name
) == 0 )
4962 assert(0 && "obj class name not found in symbol table");
4965 assert(0 && "obj class name not found");
4966 return "unknown objc class";
4970 template <typename A
>
4971 const char* ObjC2ClassRefsSection
<A
>::targetClassName(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
4973 assert(atom
->fixupCount() == 1);
4974 ld::Fixup::iterator fit
= atom
->fixupsBegin();
4975 const char* className
= NULL
;
4976 switch ( fit
->binding
) {
4977 case ld::Fixup::bindingByNameUnbound
:
4978 className
= fit
->u
.name
;
4980 case ld::Fixup::bindingDirectlyBound
:
4981 case ld::Fixup::bindingByContentBound
:
4982 className
= fit
->u
.target
->name();
4984 case ld::Fixup::bindingsIndirectlyBound
:
4985 className
= ind
.indirectName(fit
->u
.bindingIndex
);
4988 assert(0 && "unsupported binding in objc2 class ref section");
4990 assert(className
!= NULL
);
4995 template <typename A
>
4996 unsigned long ObjC2ClassRefsSection
<A
>::contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
4998 unsigned long hash
= 978;
4999 for (const char* s
= targetClassName(atom
, ind
); *s
!= '\0'; ++s
) {
5000 hash
= hash
* 33 + *s
;
5005 template <typename A
>
5006 bool ObjC2ClassRefsSection
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
5007 const ld::IndirectBindingTable
& indirectBindingTable
) const
5009 assert(this->type() == rhs
.section().type());
5010 const Atom
<A
>* rhsAtom
= dynamic_cast<const Atom
<A
>*>(&rhs
);
5011 assert(rhsAtom
!= NULL
);
5012 const char* thisClassName
= targetClassName(atom
, indirectBindingTable
);
5013 const char* rhsClassName
= targetClassName(rhsAtom
, indirectBindingTable
);
5014 return (strcmp(thisClassName
, rhsClassName
) == 0);
5018 template <typename A
>
5019 const char* Objc1ClassReferences
<A
>::targetCString(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
5021 assert(atom
->fixupCount() == 2);
5022 ld::Fixup::iterator fit
= atom
->fixupsBegin();
5023 if ( fit
->kind
== ld::Fixup::kindSetTargetAddress
)
5025 const ld::Atom
* targetAtom
= NULL
;
5026 switch ( fit
->binding
) {
5027 case ld::Fixup::bindingByContentBound
:
5028 targetAtom
= fit
->u
.target
;
5030 case ld::Fixup::bindingsIndirectlyBound
:
5031 targetAtom
= ind
.indirectAtom(fit
->u
.bindingIndex
);
5032 if ( targetAtom
== NULL
) {
5033 fprintf(stderr
, "missing target named %s\n", ind
.indirectName(fit
->u
.bindingIndex
));
5039 assert(targetAtom
!= NULL
);
5040 const Atom
<A
>* target
= dynamic_cast<const Atom
<A
>*>(targetAtom
);
5041 assert(target
!= NULL
);
5042 return (char*)target
->contentPointer();
5046 template <typename A
>
5047 const char* PointerToCStringSection
<A
>::targetCString(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
5049 assert(atom
->fixupCount() == 1);
5050 ld::Fixup::iterator fit
= atom
->fixupsBegin();
5051 const ld::Atom
* targetAtom
= NULL
;
5052 switch ( fit
->binding
) {
5053 case ld::Fixup::bindingByContentBound
:
5054 targetAtom
= fit
->u
.target
;
5056 case ld::Fixup::bindingsIndirectlyBound
:
5057 targetAtom
= ind
.indirectAtom(fit
->u
.bindingIndex
);
5062 assert(targetAtom
!= NULL
);
5063 const Atom
<A
>* target
= dynamic_cast<const Atom
<A
>*>(targetAtom
);
5064 assert(target
!= NULL
);
5065 return (char*)target
->contentPointer();
5068 template <typename A
>
5069 unsigned long PointerToCStringSection
<A
>::contentHash(const class Atom
<A
>* atom
,
5070 const ld::IndirectBindingTable
& indirectBindingTable
) const
5072 // make hash from section name and target cstring name
5073 unsigned long hash
= 123;
5074 for (const char* s
= this->sectionName(); *s
!= '\0'; ++s
) {
5075 hash
= hash
* 33 + *s
;
5077 for (const char* s
= this->targetCString(atom
, indirectBindingTable
); *s
!= '\0'; ++s
) {
5078 hash
= hash
* 33 + *s
;
5083 template <typename A
>
5084 bool PointerToCStringSection
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
5085 const ld::IndirectBindingTable
& indirectBindingTable
) const
5087 assert(this->type() == rhs
.section().type());
5088 // there can be pointers-to-cstrings in different section names
5089 // we only want to coalesce in same section name
5090 if ( *this != rhs
.section() )
5093 // get string content for this
5094 const char* cstringContent
= this->targetCString(atom
, indirectBindingTable
);
5095 const Atom
<A
>* rhsAtom
= dynamic_cast<const Atom
<A
>*>(&rhs
);
5096 assert(rhsAtom
!= NULL
);
5097 const char* rhsCstringContent
= this->targetCString(rhsAtom
, indirectBindingTable
);
5099 assert(cstringContent
!= NULL
);
5100 assert(rhsCstringContent
!= NULL
);
5101 return (strcmp(cstringContent
, rhsCstringContent
) == 0);
5106 template <typename A
>
5107 unsigned long UTF16StringSection
<A
>::contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
5109 unsigned long hash
= 5381;
5110 const uint16_t* stringContent
= (uint16_t*)atom
->contentPointer();
5111 // some buggy compilers end utf16 data with single byte, so don't use last word in hash computation
5112 unsigned int count
= (atom
->size()/2) - 1;
5113 for (const uint16_t* s
= stringContent
; count
> 0; ++s
, --count
) {
5114 hash
= hash
* 33 + *s
;
5119 template <typename A
>
5120 bool UTF16StringSection
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
5121 const ld::IndirectBindingTable
& ind
) const
5123 if ( rhs
.section().type() != ld::Section::typeUTF16Strings
)
5136 uint32_t Section
<x86_64
>::x86_64PcRelOffset(uint8_t r_type
)
5139 case X86_64_RELOC_SIGNED
:
5141 case X86_64_RELOC_SIGNED_1
:
5143 case X86_64_RELOC_SIGNED_2
:
5145 case X86_64_RELOC_SIGNED_4
:
5153 bool Section
<x86_64
>::addRelocFixup(class Parser
<x86_64
>& parser
, const macho_relocation_info
<P
>* reloc
)
5155 const macho_section
<P
>* sect
= this->machoSection();
5156 uint64_t srcAddr
= sect
->addr() + reloc
->r_address();
5157 Parser
<x86_64
>::SourceLocation src
;
5158 Parser
<x86_64
>::TargetDesc target
;
5159 Parser
<x86_64
>::TargetDesc toTarget
;
5160 src
.atom
= this->findAtomByAddress(srcAddr
);
5161 src
.offsetInAtom
= srcAddr
- src
.atom
->_objAddress
;
5162 const uint8_t* fixUpPtr
= file().fileContent() + sect
->offset() + reloc
->r_address();
5163 uint64_t contentValue
= 0;
5164 const macho_relocation_info
<x86_64::P
>* nextReloc
= &reloc
[1];
5165 bool result
= false;
5166 bool useDirectBinding
;
5167 switch ( reloc
->r_length() ) {
5169 contentValue
= *fixUpPtr
;
5172 contentValue
= (int64_t)(int16_t)E::get16(*((uint16_t*)fixUpPtr
));
5175 contentValue
= (int64_t)(int32_t)E::get32(*((uint32_t*)fixUpPtr
));
5178 contentValue
= E::get64(*((uint64_t*)fixUpPtr
));
5183 target
.weakImport
= false;
5185 if ( reloc
->r_extern() ) {
5186 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(reloc
->r_symbolnum());
5187 // use direct reference for local symbols
5188 if ( ((sym
.n_type() & N_TYPE
) == N_SECT
) && (((sym
.n_type() & N_EXT
) == 0) || (parser
.nameFromSymbol(sym
)[0] == 'L')) ) {
5189 parser
.findTargetFromAddressAndSectionNum(sym
.n_value(), sym
.n_sect(), target
);
5190 target
.addend
+= contentValue
;
5193 target
.name
= parser
.nameFromSymbol(sym
);
5194 target
.weakImport
= parser
.weakImportFromSymbol(sym
);
5195 target
.addend
= contentValue
;
5197 // cfstrings should always use direct reference to backing store
5198 if ( (this->type() == ld::Section::typeCFString
) && (src
.offsetInAtom
!= 0) ) {
5199 parser
.findTargetFromAddressAndSectionNum(sym
.n_value(), sym
.n_sect(), target
);
5200 target
.addend
= contentValue
;
5204 if ( reloc
->r_pcrel() )
5205 contentValue
+= srcAddr
+ x86_64PcRelOffset(reloc
->r_type());
5206 parser
.findTargetFromAddressAndSectionNum(contentValue
, reloc
->r_symbolnum(), target
);
5208 switch ( reloc
->r_type() ) {
5209 case X86_64_RELOC_UNSIGNED
:
5210 if ( reloc
->r_pcrel() )
5211 throw "pcrel and X86_64_RELOC_UNSIGNED not supported";
5212 switch ( reloc
->r_length() ) {
5215 throw "length < 2 and X86_64_RELOC_UNSIGNED not supported";
5217 parser
.addFixups(src
, ld::Fixup::kindStoreLittleEndian32
, target
);
5220 parser
.addFixups(src
, ld::Fixup::kindStoreLittleEndian64
, target
);
5224 case X86_64_RELOC_SIGNED
:
5225 case X86_64_RELOC_SIGNED_1
:
5226 case X86_64_RELOC_SIGNED_2
:
5227 case X86_64_RELOC_SIGNED_4
:
5228 if ( ! reloc
->r_pcrel() )
5229 throw "not pcrel and X86_64_RELOC_SIGNED* not supported";
5230 if ( reloc
->r_length() != 2 )
5231 throw "length != 2 and X86_64_RELOC_SIGNED* not supported";
5232 switch ( reloc
->r_type() ) {
5233 case X86_64_RELOC_SIGNED
:
5234 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel32
, target
);
5236 case X86_64_RELOC_SIGNED_1
:
5237 if ( reloc
->r_extern() )
5239 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel32_1
, target
);
5241 case X86_64_RELOC_SIGNED_2
:
5242 if ( reloc
->r_extern() )
5244 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel32_2
, target
);
5246 case X86_64_RELOC_SIGNED_4
:
5247 if ( reloc
->r_extern() )
5249 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel32_4
, target
);
5253 case X86_64_RELOC_BRANCH
:
5254 if ( ! reloc
->r_pcrel() )
5255 throw "not pcrel and X86_64_RELOC_BRANCH not supported";
5256 switch ( reloc
->r_length() ) {
5258 if ( (target
.name
!= NULL
) && (strncmp(target
.name
, "___dtrace_probe$", 16) == 0) ) {
5259 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindStoreX86DtraceCallSiteNop
, false, target
.name
);
5260 parser
.addDtraceExtraInfos(src
, &target
.name
[16]);
5262 else if ( (target
.name
!= NULL
) && (strncmp(target
.name
, "___dtrace_isenabled$", 20) == 0) ) {
5263 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindStoreX86DtraceIsEnableSiteClear
, false, target
.name
);
5264 parser
.addDtraceExtraInfos(src
, &target
.name
[20]);
5267 parser
.addFixups(src
, ld::Fixup::kindStoreX86BranchPCRel32
, target
);
5271 parser
.addFixups(src
, ld::Fixup::kindStoreX86BranchPCRel8
, target
);
5274 throwf("length=%d and X86_64_RELOC_BRANCH not supported", reloc
->r_length());
5277 case X86_64_RELOC_GOT
:
5278 if ( ! reloc
->r_extern() )
5279 throw "not extern and X86_64_RELOC_GOT not supported";
5280 if ( ! reloc
->r_pcrel() )
5281 throw "not pcrel and X86_64_RELOC_GOT not supported";
5282 if ( reloc
->r_length() != 2 )
5283 throw "length != 2 and X86_64_RELOC_GOT not supported";
5284 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel32GOT
, target
);
5286 case X86_64_RELOC_GOT_LOAD
:
5287 if ( ! reloc
->r_extern() )
5288 throw "not extern and X86_64_RELOC_GOT_LOAD not supported";
5289 if ( ! reloc
->r_pcrel() )
5290 throw "not pcrel and X86_64_RELOC_GOT_LOAD not supported";
5291 if ( reloc
->r_length() != 2 )
5292 throw "length != 2 and X86_64_RELOC_GOT_LOAD not supported";
5293 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel32GOTLoad
, target
);
5295 case X86_64_RELOC_SUBTRACTOR
:
5296 if ( reloc
->r_pcrel() )
5297 throw "X86_64_RELOC_SUBTRACTOR cannot be pc-relative";
5298 if ( reloc
->r_length() < 2 )
5299 throw "X86_64_RELOC_SUBTRACTOR must have r_length of 2 or 3";
5300 if ( !reloc
->r_extern() )
5301 throw "X86_64_RELOC_SUBTRACTOR must have r_extern=1";
5302 if ( nextReloc
->r_type() != X86_64_RELOC_UNSIGNED
)
5303 throw "X86_64_RELOC_SUBTRACTOR must be followed by X86_64_RELOC_UNSIGNED";
5305 if ( nextReloc
->r_pcrel() )
5306 throw "X86_64_RELOC_UNSIGNED following a X86_64_RELOC_SUBTRACTOR cannot be pc-relative";
5307 if ( nextReloc
->r_length() != reloc
->r_length() )
5308 throw "X86_64_RELOC_UNSIGNED following a X86_64_RELOC_SUBTRACTOR must have same r_length";
5309 if ( nextReloc
->r_extern() ) {
5310 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(nextReloc
->r_symbolnum());
5311 // use direct reference for local symbols
5312 if ( ((sym
.n_type() & N_TYPE
) == N_SECT
) && (((sym
.n_type() & N_EXT
) == 0) || (parser
.nameFromSymbol(sym
)[0] == 'L')) ) {
5313 parser
.findTargetFromAddressAndSectionNum(sym
.n_value(), sym
.n_sect(), toTarget
);
5314 toTarget
.addend
= contentValue
;
5315 useDirectBinding
= true;
5318 toTarget
.name
= parser
.nameFromSymbol(sym
);
5319 toTarget
.weakImport
= parser
.weakImportFromSymbol(sym
);
5320 toTarget
.addend
= contentValue
;
5321 useDirectBinding
= false;
5325 parser
.findTargetFromAddressAndSectionNum(contentValue
, nextReloc
->r_symbolnum(), toTarget
);
5326 useDirectBinding
= (toTarget
.atom
->scope() == ld::Atom::scopeTranslationUnit
);
5328 if ( useDirectBinding
)
5329 parser
.addFixup(src
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, toTarget
.atom
);
5331 parser
.addFixup(src
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, toTarget
.weakImport
, toTarget
.name
);
5332 parser
.addFixup(src
, ld::Fixup::k2of4
, ld::Fixup::kindAddAddend
, toTarget
.addend
);
5333 if ( target
.atom
== NULL
)
5334 parser
.addFixup(src
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractTargetAddress
, false, target
.name
);
5336 parser
.addFixup(src
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractTargetAddress
, target
.atom
);
5337 if ( reloc
->r_length() == 2 )
5338 parser
.addFixup(src
, ld::Fixup::k4of4
, ld::Fixup::kindStoreLittleEndian32
);
5340 parser
.addFixup(src
, ld::Fixup::k4of4
, ld::Fixup::kindStoreLittleEndian64
);
5342 case X86_64_RELOC_TLV
:
5343 if ( ! reloc
->r_extern() )
5344 throw "not extern and X86_64_RELOC_TLV not supported";
5345 if ( ! reloc
->r_pcrel() )
5346 throw "not pcrel and X86_64_RELOC_TLV not supported";
5347 if ( reloc
->r_length() != 2 )
5348 throw "length != 2 and X86_64_RELOC_TLV not supported";
5349 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel32TLVLoad
, target
);
5352 throwf("unknown relocation type %d", reloc
->r_type());
5360 bool Section
<x86
>::addRelocFixup(class Parser
<x86
>& parser
, const macho_relocation_info
<P
>* reloc
)
5362 const macho_section
<P
>* sect
= this->machoSection();
5364 const uint8_t* fixUpPtr
;
5365 uint32_t contentValue
= 0;
5366 ld::Fixup::Kind kind
= ld::Fixup::kindNone
;
5367 Parser
<x86
>::SourceLocation src
;
5368 Parser
<x86
>::TargetDesc target
;
5370 if ( (reloc
->r_address() & R_SCATTERED
) == 0 ) {
5371 srcAddr
= sect
->addr() + reloc
->r_address();
5372 src
.atom
= this->findAtomByAddress(srcAddr
);
5373 src
.offsetInAtom
= srcAddr
- src
.atom
->_objAddress
;
5374 fixUpPtr
= file().fileContent() + sect
->offset() + reloc
->r_address();
5375 switch ( reloc
->r_type() ) {
5376 case GENERIC_RELOC_VANILLA
:
5377 switch ( reloc
->r_length() ) {
5379 contentValue
= (int32_t)(int8_t)*fixUpPtr
;
5380 if ( reloc
->r_pcrel() ) {
5381 kind
= ld::Fixup::kindStoreX86BranchPCRel8
;
5382 contentValue
+= srcAddr
+ sizeof(uint8_t);
5385 throw "r_length=0 and r_pcrel=0 not supported";
5388 contentValue
= (int32_t)(int16_t)E::get16(*((uint16_t*)fixUpPtr
));
5389 if ( reloc
->r_pcrel() ) {
5390 kind
= ld::Fixup::kindStoreX86PCRel16
;
5391 contentValue
+= srcAddr
+ sizeof(uint16_t);
5394 kind
= ld::Fixup::kindStoreLittleEndian16
;
5397 contentValue
= E::get32(*((uint32_t*)fixUpPtr
));
5398 if ( reloc
->r_pcrel() ) {
5399 kind
= ld::Fixup::kindStoreX86BranchPCRel32
;
5400 contentValue
+= srcAddr
+ sizeof(uint32_t);
5403 kind
= ld::Fixup::kindStoreLittleEndian32
;
5406 throw "r_length=3 not supported";
5408 if ( reloc
->r_extern() ) {
5410 const macho_nlist
<P
>& targetSymbol
= parser
.symbolFromIndex(reloc
->r_symbolnum());
5411 target
.name
= parser
.nameFromSymbol(targetSymbol
);
5412 target
.weakImport
= parser
.weakImportFromSymbol(targetSymbol
);
5413 target
.addend
= (int32_t)contentValue
;
5416 parser
.findTargetFromAddressAndSectionNum(contentValue
, reloc
->r_symbolnum(), target
);
5418 if ( (kind
== ld::Fixup::kindStoreX86BranchPCRel32
) && (target
.name
!= NULL
) ) {
5419 if ( strncmp(target
.name
, "___dtrace_probe$", 16) == 0 ) {
5420 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindStoreX86DtraceCallSiteNop
, false, target
.name
);
5421 parser
.addDtraceExtraInfos(src
, &target
.name
[16]);
5424 else if ( strncmp(target
.name
, "___dtrace_isenabled$", 20) == 0 ) {
5425 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindStoreX86DtraceIsEnableSiteClear
, false, target
.name
);
5426 parser
.addDtraceExtraInfos(src
, &target
.name
[20]);
5430 parser
.addFixups(src
, kind
, target
);
5433 case GENERIC_RLEOC_TLV
:
5435 if ( !reloc
->r_extern() )
5436 throw "r_extern=0 and r_type=GENERIC_RLEOC_TLV not supported";
5437 if ( reloc
->r_length() != 2 )
5438 throw "r_length!=2 and r_type=GENERIC_RLEOC_TLV not supported";
5439 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(reloc
->r_symbolnum());
5440 // use direct reference for local symbols
5441 if ( ((sym
.n_type() & N_TYPE
) == N_SECT
) && ((sym
.n_type() & N_EXT
) == 0) ) {
5442 parser
.findTargetFromAddressAndSectionNum(sym
.n_value(), sym
.n_sect(), target
);
5446 target
.name
= parser
.nameFromSymbol(sym
);
5447 target
.weakImport
= parser
.weakImportFromSymbol(sym
);
5449 target
.addend
= (int64_t)(int32_t)E::get32(*((uint32_t*)fixUpPtr
));
5450 if ( reloc
->r_pcrel() ) {
5451 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel32TLVLoad
, target
);
5454 parser
.addFixups(src
, ld::Fixup::kindStoreX86Abs32TLVLoad
, target
);
5460 throwf("unsupported i386 relocation type (%d)", reloc
->r_type());
5464 // scattered relocation
5465 const macho_scattered_relocation_info
<P
>* sreloc
= (macho_scattered_relocation_info
<P
>*)reloc
;
5466 srcAddr
= sect
->addr() + sreloc
->r_address();
5467 src
.atom
= this->findAtomByAddress(srcAddr
);
5468 assert(src
.atom
!= NULL
);
5469 src
.offsetInAtom
= srcAddr
- src
.atom
->_objAddress
;
5470 fixUpPtr
= file().fileContent() + sect
->offset() + sreloc
->r_address();
5471 uint32_t relocValue
= sreloc
->r_value();
5472 bool result
= false;
5473 // file format allows pair to be scattered or not
5474 const macho_scattered_relocation_info
<P
>* nextSReloc
= &sreloc
[1];
5475 const macho_relocation_info
<P
>* nextReloc
= &reloc
[1];
5476 bool nextRelocIsPair
= false;
5477 uint32_t nextRelocAddress
= 0;
5478 uint32_t nextRelocValue
= 0;
5479 if ( (nextReloc
->r_address() & R_SCATTERED
) == 0 ) {
5480 if ( nextReloc
->r_type() == GENERIC_RELOC_PAIR
) {
5481 nextRelocIsPair
= true;
5482 nextRelocAddress
= nextReloc
->r_address();
5483 result
= true; // iterator should skip next reloc, since we've consumed it here
5487 if ( nextSReloc
->r_type() == GENERIC_RELOC_PAIR
) {
5488 nextRelocIsPair
= true;
5489 nextRelocAddress
= nextSReloc
->r_address();
5490 nextRelocValue
= nextSReloc
->r_value();
5493 switch (sreloc
->r_type()) {
5494 case GENERIC_RELOC_VANILLA
:
5495 // with a scattered relocation we get both the target (sreloc->r_value()) and the target+offset (*fixUpPtr)
5496 target
.atom
= parser
.findAtomByAddress(relocValue
);
5497 if ( sreloc
->r_pcrel() ) {
5498 switch ( sreloc
->r_length() ) {
5500 contentValue
= srcAddr
+ 1 + *fixUpPtr
;
5501 target
.addend
= (int32_t)contentValue
- (int32_t)relocValue
;
5502 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel8
, target
);
5505 contentValue
= srcAddr
+ 2 + LittleEndian::get16(*((uint16_t*)fixUpPtr
));
5506 target
.addend
= (int32_t)contentValue
- (int32_t)relocValue
;
5507 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel16
, target
);
5510 contentValue
= srcAddr
+ 4 + LittleEndian::get32(*((uint32_t*)fixUpPtr
));
5511 target
.addend
= (int32_t)contentValue
- (int32_t)relocValue
;
5512 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel32
, target
);
5515 throw "unsupported r_length=3 for scattered pc-rel vanilla reloc";
5520 if ( sreloc
->r_length() != 2 )
5521 throwf("unsupported r_length=%d for scattered vanilla reloc", sreloc
->r_length());
5522 contentValue
= LittleEndian::get32(*((uint32_t*)fixUpPtr
));
5523 target
.addend
= (int32_t)contentValue
- (int32_t)(target
.atom
->objectAddress());
5524 parser
.addFixups(src
, ld::Fixup::kindStoreLittleEndian32
, target
);
5527 case GENERIC_RELOC_SECTDIFF
:
5528 case GENERIC_RELOC_LOCAL_SECTDIFF
:
5530 if ( !nextRelocIsPair
)
5531 throw "GENERIC_RELOC_SECTDIFF missing following pair";
5532 switch ( sreloc
->r_length() ) {
5535 throw "bad length for GENERIC_RELOC_SECTDIFF";
5537 contentValue
= (int32_t)(int16_t)LittleEndian::get16(*((uint16_t*)fixUpPtr
));
5538 kind
= ld::Fixup::kindStoreLittleEndian16
;
5541 contentValue
= LittleEndian::get32(*((uint32_t*)fixUpPtr
));
5542 kind
= ld::Fixup::kindStoreLittleEndian32
;
5545 Atom
<x86
>* fromAtom
= parser
.findAtomByAddress(nextRelocValue
);
5546 uint32_t offsetInFrom
= nextRelocValue
- fromAtom
->_objAddress
;
5547 parser
.findTargetFromAddress(sreloc
->r_value(), target
);
5548 // check for addend encoded in the section content
5549 int64_t addend
= (int32_t)contentValue
- (int32_t)(sreloc
->r_value() - nextRelocValue
);
5551 // switch binding base on coalescing
5552 if ( target
.atom
== NULL
) {
5553 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, false, target
.name
);
5555 else if ( target
.atom
->scope() == ld::Atom::scopeTranslationUnit
) {
5556 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, target
.atom
);
5558 else if ( (target
.atom
->combine() == ld::Atom::combineByNameAndContent
) || (target
.atom
->combine() == ld::Atom::combineByNameAndReferences
) ) {
5559 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, target
.atom
);
5562 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, false, target
.atom
->name());
5564 parser
.addFixup(src
, ld::Fixup::k2of5
, ld::Fixup::kindAddAddend
, target
.addend
);
5565 parser
.addFixup(src
, ld::Fixup::k3of5
, ld::Fixup::kindSubtractTargetAddress
, fromAtom
);
5566 parser
.addFixup(src
, ld::Fixup::k4of5
, ld::Fixup::kindSubtractAddend
, offsetInFrom
-addend
);
5567 parser
.addFixup(src
, ld::Fixup::k5of5
, kind
);
5570 // switch binding base on coalescing
5571 if ( target
.atom
== NULL
) {
5572 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, false, target
.name
);
5574 else if ( target
.atom
->scope() == ld::Atom::scopeTranslationUnit
) {
5575 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, target
.atom
);
5577 else if ( (target
.atom
->combine() == ld::Atom::combineByNameAndContent
) || (target
.atom
->combine() == ld::Atom::combineByNameAndReferences
) ) {
5578 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, target
.atom
);
5581 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, false, target
.atom
->name());
5583 parser
.addFixup(src
, ld::Fixup::k2of5
, ld::Fixup::kindAddAddend
, target
.addend
+addend
);
5584 parser
.addFixup(src
, ld::Fixup::k3of5
, ld::Fixup::kindSubtractTargetAddress
, fromAtom
);
5585 parser
.addFixup(src
, ld::Fixup::k4of5
, ld::Fixup::kindSubtractAddend
, offsetInFrom
);
5586 parser
.addFixup(src
, ld::Fixup::k5of5
, kind
);
5599 #if SUPPORT_ARCH_arm_any
5601 bool Section
<arm
>::addRelocFixup(class Parser
<arm
>& parser
, const macho_relocation_info
<P
>* reloc
)
5603 const macho_section
<P
>* sect
= this->machoSection();
5604 bool result
= false;
5608 int32_t displacement
= 0;
5609 uint32_t instruction
= 0;
5610 pint_t contentValue
= 0;
5611 Parser
<arm
>::SourceLocation src
;
5612 Parser
<arm
>::TargetDesc target
;
5613 const macho_relocation_info
<P
>* nextReloc
;
5615 if ( (reloc
->r_address() & R_SCATTERED
) == 0 ) {
5616 bool externSymbolIsThumbDef
= false;
5617 srcAddr
= sect
->addr() + reloc
->r_address();
5618 src
.atom
= this->findAtomByAddress(srcAddr
);
5619 src
.offsetInAtom
= srcAddr
- src
.atom
->_objAddress
;
5620 fixUpPtr
= (uint32_t*)(file().fileContent() + sect
->offset() + reloc
->r_address());
5621 if ( reloc
->r_type() != ARM_RELOC_PAIR
)
5622 instruction
= LittleEndian::get32(*fixUpPtr
);
5623 if ( reloc
->r_extern() ) {
5624 const macho_nlist
<P
>& targetSymbol
= parser
.symbolFromIndex(reloc
->r_symbolnum());
5625 // use direct reference for local symbols
5626 if ( ((targetSymbol
.n_type() & N_TYPE
) == N_SECT
) && (((targetSymbol
.n_type() & N_EXT
) == 0) || (parser
.nameFromSymbol(targetSymbol
)[0] == 'L')) ) {
5627 parser
.findTargetFromAddressAndSectionNum(targetSymbol
.n_value(), targetSymbol
.n_sect(), target
);
5631 target
.name
= parser
.nameFromSymbol(targetSymbol
);
5632 target
.weakImport
= parser
.weakImportFromSymbol(targetSymbol
);
5633 if ( ((targetSymbol
.n_type() & N_TYPE
) == N_SECT
) && (targetSymbol
.n_desc() & N_ARM_THUMB_DEF
) )
5634 externSymbolIsThumbDef
= true;
5637 switch ( reloc
->r_type() ) {
5638 case ARM_RELOC_BR24
:
5639 // Sign-extend displacement
5640 displacement
= (instruction
& 0x00FFFFFF) << 2;
5641 if ( (displacement
& 0x02000000) != 0 )
5642 displacement
|= 0xFC000000;
5643 // The pc added will be +8 from the pc
5645 // If this is BLX add H << 1
5646 if ((instruction
& 0xFE000000) == 0xFA000000)
5647 displacement
+= ((instruction
& 0x01000000) >> 23);
5648 if ( reloc
->r_extern() ) {
5649 target
.addend
= srcAddr
+ displacement
;
5650 if ( externSymbolIsThumbDef
)
5651 target
.addend
&= -2; // remove thumb bit
5654 dstAddr
= srcAddr
+ displacement
;
5655 parser
.findTargetFromAddressAndSectionNum(dstAddr
, reloc
->r_symbolnum(), target
);
5657 // special case "calls" for dtrace
5658 if ( (target
.name
!= NULL
) && (strncmp(target
.name
, "___dtrace_probe$", 16) == 0) ) {
5659 parser
.addFixup(src
, ld::Fixup::k1of1
,
5660 ld::Fixup::kindStoreARMDtraceCallSiteNop
, false, target
.name
);
5661 parser
.addDtraceExtraInfos(src
, &target
.name
[16]);
5663 else if ( (target
.name
!= NULL
) && (strncmp(target
.name
, "___dtrace_isenabled$", 20) == 0) ) {
5664 parser
.addFixup(src
, ld::Fixup::k1of1
,
5665 ld::Fixup::kindStoreARMDtraceIsEnableSiteClear
, false, target
.name
);
5666 parser
.addDtraceExtraInfos(src
, &target
.name
[20]);
5669 parser
.addFixups(src
, ld::Fixup::kindStoreARMBranch24
, target
);
5672 case ARM_THUMB_RELOC_BR22
:
5673 // thumb2 added two more bits to displacement, complicating the displacement decoding
5675 uint32_t s
= (instruction
>> 10) & 0x1;
5676 uint32_t j1
= (instruction
>> 29) & 0x1;
5677 uint32_t j2
= (instruction
>> 27) & 0x1;
5678 uint32_t imm10
= instruction
& 0x3FF;
5679 uint32_t imm11
= (instruction
>> 16) & 0x7FF;
5680 uint32_t i1
= (j1
== s
);
5681 uint32_t i2
= (j2
== s
);
5682 uint32_t dis
= (s
<< 24) | (i1
<< 23) | (i2
<< 22) | (imm10
<< 12) | (imm11
<< 1);
5686 displacement
= sdis
;
5688 // The pc added will be +4 from the pc
5690 // If the instruction was blx, force the low 2 bits to be clear
5691 dstAddr
= srcAddr
+ displacement
;
5692 if ((instruction
& 0xD0000000) == 0xC0000000)
5693 dstAddr
&= 0xFFFFFFFC;
5695 if ( reloc
->r_extern() ) {
5696 target
.addend
= dstAddr
;
5699 parser
.findTargetFromAddressAndSectionNum(dstAddr
, reloc
->r_symbolnum(), target
);
5701 // special case "calls" for dtrace
5702 if ( (target
.name
!= NULL
) && (strncmp(target
.name
, "___dtrace_probe$", 16) == 0) ) {
5703 parser
.addFixup(src
, ld::Fixup::k1of1
,
5704 ld::Fixup::kindStoreThumbDtraceCallSiteNop
, false, target
.name
);
5705 parser
.addDtraceExtraInfos(src
, &target
.name
[16]);
5707 else if ( (target
.name
!= NULL
) && (strncmp(target
.name
, "___dtrace_isenabled$", 20) == 0) ) {
5708 parser
.addFixup(src
, ld::Fixup::k1of1
,
5709 ld::Fixup::kindStoreThumbDtraceIsEnableSiteClear
, false, target
.name
);
5710 parser
.addDtraceExtraInfos(src
, &target
.name
[20]);
5713 parser
.addFixups(src
, ld::Fixup::kindStoreThumbBranch22
, target
);
5716 case ARM_RELOC_VANILLA
:
5717 if ( reloc
->r_length() != 2 )
5718 throw "bad length for ARM_RELOC_VANILLA";
5719 contentValue
= LittleEndian::get32(*fixUpPtr
);
5720 if ( reloc
->r_extern() ) {
5721 target
.addend
= (int32_t)contentValue
;
5722 if ( externSymbolIsThumbDef
)
5723 target
.addend
&= -2; // remove thumb bit
5726 parser
.findTargetFromAddressAndSectionNum(contentValue
, reloc
->r_symbolnum(), target
);
5727 // possible non-extern relocation turned into by-name ref because target is a weak-def
5728 if ( target
.atom
!= NULL
) {
5729 if ( target
.atom
->isThumb() )
5730 target
.addend
&= -2; // remove thumb bit
5731 // if reference to LSDA, add group subordinate fixup
5732 if ( target
.atom
->contentType() == ld::Atom::typeLSDA
) {
5733 Parser
<arm
>::SourceLocation src2
;
5734 src2
.atom
= src
.atom
;
5735 src2
.offsetInAtom
= 0;
5736 parser
.addFixup(src2
, ld::Fixup::k1of1
, ld::Fixup::kindNoneGroupSubordinateLSDA
, target
.atom
);
5740 parser
.addFixups(src
, ld::Fixup::kindStoreLittleEndian32
, target
);
5742 case ARM_THUMB_32BIT_BRANCH
:
5743 // silently ignore old unnecessary reloc
5745 case ARM_RELOC_HALF
:
5746 nextReloc
= &reloc
[1];
5747 if ( nextReloc
->r_type() == ARM_RELOC_PAIR
) {
5748 uint32_t instruction16
;
5749 uint32_t other16
= (nextReloc
->r_address() & 0xFFFF);
5751 if ( reloc
->r_length() & 2 ) {
5753 uint32_t i
= ((instruction
& 0x00000400) >> 10);
5754 uint32_t imm4
= (instruction
& 0x0000000F);
5755 uint32_t imm3
= ((instruction
& 0x70000000) >> 28);
5756 uint32_t imm8
= ((instruction
& 0x00FF0000) >> 16);
5757 instruction16
= (imm4
<< 12) | (i
<< 11) | (imm3
<< 8) | imm8
;
5761 uint32_t imm4
= ((instruction
& 0x000F0000) >> 16);
5762 uint32_t imm12
= (instruction
& 0x00000FFF);
5763 instruction16
= (imm4
<< 12) | imm12
;
5765 if ( reloc
->r_length() & 1 ) {
5767 dstAddr
= ((instruction16
<< 16) | other16
);
5768 if ( reloc
->r_extern() ) {
5769 target
.addend
= dstAddr
;
5770 if ( externSymbolIsThumbDef
)
5771 target
.addend
&= -2; // remove thumb bit
5774 parser
.findTargetFromAddress(dstAddr
, target
);
5775 if ( target
.atom
->isThumb() )
5776 target
.addend
&= (-2); // remove thumb bit
5778 parser
.addFixups(src
, (isThumb
? ld::Fixup::kindStoreThumbHigh16
: ld::Fixup::kindStoreARMHigh16
), target
);
5782 dstAddr
= (other16
<< 16) | instruction16
;
5783 if ( reloc
->r_extern() ) {
5784 target
.addend
= dstAddr
;
5785 if ( externSymbolIsThumbDef
)
5786 target
.addend
&= -2; // remove thumb bit
5789 parser
.findTargetFromAddress(dstAddr
, target
);
5790 if ( target
.atom
->isThumb() )
5791 target
.addend
&= (-2); // remove thumb bit
5793 parser
.addFixups(src
, (isThumb
? ld::Fixup::kindStoreThumbLow16
: ld::Fixup::kindStoreARMLow16
), target
);
5798 throw "for ARM_RELOC_HALF, next reloc is not ARM_RELOC_PAIR";
5801 throwf("unknown relocation type %d", reloc
->r_type());
5806 const macho_scattered_relocation_info
<P
>* sreloc
= (macho_scattered_relocation_info
<P
>*)reloc
;
5807 // file format allows pair to be scattered or not
5808 const macho_scattered_relocation_info
<P
>* nextSReloc
= &sreloc
[1];
5809 nextReloc
= &reloc
[1];
5810 srcAddr
= sect
->addr() + sreloc
->r_address();
5811 dstAddr
= sreloc
->r_value();
5812 fixUpPtr
= (uint32_t*)(file().fileContent() + sect
->offset() + sreloc
->r_address());
5813 instruction
= LittleEndian::get32(*fixUpPtr
);
5814 src
.atom
= this->findAtomByAddress(srcAddr
);
5815 src
.offsetInAtom
= srcAddr
- src
.atom
->_objAddress
;
5816 bool nextRelocIsPair
= false;
5817 uint32_t nextRelocAddress
= 0;
5818 uint32_t nextRelocValue
= 0;
5819 if ( (nextReloc
->r_address() & R_SCATTERED
) == 0 ) {
5820 if ( nextReloc
->r_type() == ARM_RELOC_PAIR
) {
5821 nextRelocIsPair
= true;
5822 nextRelocAddress
= nextReloc
->r_address();
5827 if ( nextSReloc
->r_type() == ARM_RELOC_PAIR
) {
5828 nextRelocIsPair
= true;
5829 nextRelocAddress
= nextSReloc
->r_address();
5830 nextRelocValue
= nextSReloc
->r_value();
5834 switch ( sreloc
->r_type() ) {
5835 case ARM_RELOC_VANILLA
:
5836 // with a scattered relocation we get both the target (sreloc->r_value()) and the target+offset (*fixUpPtr)
5837 if ( sreloc
->r_length() != 2 )
5838 throw "bad length for ARM_RELOC_VANILLA";
5839 target
.atom
= parser
.findAtomByAddress(sreloc
->r_value());
5840 if ( target
.atom
== NULL
)
5841 throwf("bad r_value (0x%08X) for ARM_RELOC_VANILLA\n", sreloc
->r_value());
5842 contentValue
= LittleEndian::get32(*fixUpPtr
);
5843 target
.addend
= contentValue
- target
.atom
->_objAddress
;
5844 if ( target
.atom
->isThumb() )
5845 target
.addend
&= -2; // remove thumb bit
5846 parser
.addFixups(src
, ld::Fixup::kindStoreLittleEndian32
, target
);
5848 case ARM_RELOC_BR24
:
5849 // Sign-extend displacement
5850 displacement
= (instruction
& 0x00FFFFFF) << 2;
5851 if ( (displacement
& 0x02000000) != 0 )
5852 displacement
|= 0xFC000000;
5853 // The pc added will be +8 from the pc
5855 // If this is BLX add H << 1
5856 if ((instruction
& 0xFE000000) == 0xFA000000)
5857 displacement
+= ((instruction
& 0x01000000) >> 23);
5858 target
.atom
= parser
.findAtomByAddress(sreloc
->r_value());
5859 target
.addend
= (int64_t)(srcAddr
+ displacement
) - (int64_t)(target
.atom
->_objAddress
);
5860 parser
.addFixups(src
, ld::Fixup::kindStoreARMBranch24
, target
);
5862 case ARM_THUMB_RELOC_BR22
:
5863 // thumb2 added two more bits to displacement, complicating the displacement decoding
5865 uint32_t s
= (instruction
>> 10) & 0x1;
5866 uint32_t j1
= (instruction
>> 29) & 0x1;
5867 uint32_t j2
= (instruction
>> 27) & 0x1;
5868 uint32_t imm10
= instruction
& 0x3FF;
5869 uint32_t imm11
= (instruction
>> 16) & 0x7FF;
5870 uint32_t i1
= (j1
== s
);
5871 uint32_t i2
= (j2
== s
);
5872 uint32_t dis
= (s
<< 24) | (i1
<< 23) | (i2
<< 22) | (imm10
<< 12) | (imm11
<< 1);
5876 displacement
= sdis
;
5878 // The pc added will be +4 from the pc
5880 dstAddr
= srcAddr
+displacement
;
5881 // If the instruction was blx, force the low 2 bits to be clear
5882 if ((instruction
& 0xF8000000) == 0xE8000000)
5883 dstAddr
&= 0xFFFFFFFC;
5884 target
.atom
= parser
.findAtomByAddress(sreloc
->r_value());
5885 target
.addend
= dstAddr
- target
.atom
->_objAddress
;
5886 parser
.addFixups(src
, ld::Fixup::kindStoreThumbBranch22
, target
);
5888 case ARM_RELOC_SECTDIFF
:
5889 case ARM_RELOC_LOCAL_SECTDIFF
:
5891 if ( ! nextRelocIsPair
)
5892 throw "ARM_RELOC_SECTDIFF missing following pair";
5893 if ( sreloc
->r_length() != 2 )
5894 throw "bad length for ARM_RELOC_SECTDIFF";
5895 contentValue
= LittleEndian::get32(*fixUpPtr
);
5896 Atom
<arm
>* fromAtom
= parser
.findAtomByAddress(nextRelocValue
);
5897 uint32_t offsetInFrom
= nextRelocValue
- fromAtom
->_objAddress
;
5898 uint32_t offsetInTarget
;
5899 Atom
<arm
>* targetAtom
= parser
.findAtomByAddressOrLocalTargetOfStub(sreloc
->r_value(), &offsetInTarget
);
5900 // check for addend encoded in the section content
5901 int64_t addend
= (int32_t)contentValue
- (int32_t)(sreloc
->r_value() - nextRelocValue
);
5902 if ( targetAtom
->isThumb() )
5903 addend
&= -2; // remove thumb bit
5904 // if reference to LSDA, add group subordinate fixup
5905 if ( targetAtom
->contentType() == ld::Atom::typeLSDA
) {
5906 Parser
<arm
>::SourceLocation src2
;
5907 src2
.atom
= src
.atom
;
5908 src2
.offsetInAtom
= 0;
5909 parser
.addFixup(src2
, ld::Fixup::k1of1
, ld::Fixup::kindNoneGroupSubordinateLSDA
, targetAtom
);
5912 // switch binding base on coalescing
5913 if ( targetAtom
->scope() == ld::Atom::scopeTranslationUnit
) {
5914 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, targetAtom
);
5916 else if ( (targetAtom
->combine() == ld::Atom::combineByNameAndContent
) || (targetAtom
->combine() == ld::Atom::combineByNameAndReferences
) ) {
5917 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, targetAtom
);
5920 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, false, targetAtom
->name());
5922 parser
.addFixup(src
, ld::Fixup::k2of5
, ld::Fixup::kindAddAddend
, offsetInTarget
);
5923 parser
.addFixup(src
, ld::Fixup::k3of5
, ld::Fixup::kindSubtractTargetAddress
, fromAtom
);
5924 parser
.addFixup(src
, ld::Fixup::k4of5
, ld::Fixup::kindSubtractAddend
, offsetInFrom
-addend
);
5925 parser
.addFixup(src
, ld::Fixup::k5of5
, ld::Fixup::kindStoreLittleEndian32
);
5928 if ( targetAtom
->scope() == ld::Atom::scopeTranslationUnit
) {
5929 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, targetAtom
);
5931 else if ( (targetAtom
->combine() == ld::Atom::combineByNameAndContent
) || (targetAtom
->combine() == ld::Atom::combineByNameAndReferences
) ) {
5932 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, targetAtom
);
5935 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, false, targetAtom
->name());
5937 parser
.addFixup(src
, ld::Fixup::k2of5
, ld::Fixup::kindAddAddend
, (uint32_t)(offsetInTarget
+addend
));
5938 parser
.addFixup(src
, ld::Fixup::k3of5
, ld::Fixup::kindSubtractTargetAddress
, fromAtom
);
5939 parser
.addFixup(src
, ld::Fixup::k4of5
, ld::Fixup::kindSubtractAddend
, offsetInFrom
);
5940 parser
.addFixup(src
, ld::Fixup::k5of5
, ld::Fixup::kindStoreLittleEndian32
);
5944 case ARM_RELOC_HALF_SECTDIFF
:
5945 if ( nextRelocIsPair
) {
5946 instruction
= LittleEndian::get32(*fixUpPtr
);
5947 Atom
<arm
>* fromAtom
= parser
.findAtomByAddress(nextRelocValue
);
5948 uint32_t offsetInFrom
= nextRelocValue
- fromAtom
->_objAddress
;
5949 Atom
<arm
>* targetAtom
= parser
.findAtomByAddress(sreloc
->r_value());
5950 uint32_t offsetInTarget
= sreloc
->r_value() - targetAtom
->_objAddress
;
5951 uint32_t instruction16
;
5952 uint32_t other16
= (nextRelocAddress
& 0xFFFF);
5954 if ( sreloc
->r_length() & 2 ) {
5956 uint32_t i
= ((instruction
& 0x00000400) >> 10);
5957 uint32_t imm4
= (instruction
& 0x0000000F);
5958 uint32_t imm3
= ((instruction
& 0x70000000) >> 28);
5959 uint32_t imm8
= ((instruction
& 0x00FF0000) >> 16);
5960 instruction16
= (imm4
<< 12) | (i
<< 11) | (imm3
<< 8) | imm8
;
5964 uint32_t imm4
= ((instruction
& 0x000F0000) >> 16);
5965 uint32_t imm12
= (instruction
& 0x00000FFF);
5966 instruction16
= (imm4
<< 12) | imm12
;
5968 if ( sreloc
->r_length() & 1 )
5969 dstAddr
= ((instruction16
<< 16) | other16
);
5971 dstAddr
= (other16
<< 16) | instruction16
;
5972 if ( targetAtom
->isThumb() )
5973 dstAddr
&= (-2); // remove thumb bit
5974 int32_t addend
= dstAddr
- (sreloc
->r_value() - nextRelocValue
);
5975 if ( targetAtom
->scope() == ld::Atom::scopeTranslationUnit
) {
5976 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, targetAtom
);
5978 else if ( (targetAtom
->combine() == ld::Atom::combineByNameAndContent
) || (targetAtom
->combine() == ld::Atom::combineByNameAndReferences
) ) {
5979 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, targetAtom
);
5982 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, false, targetAtom
->name());
5984 parser
.addFixup(src
, ld::Fixup::k2of5
, ld::Fixup::kindAddAddend
, (uint32_t)offsetInTarget
+addend
);
5985 parser
.addFixup(src
, ld::Fixup::k3of5
, ld::Fixup::kindSubtractTargetAddress
, fromAtom
);
5986 parser
.addFixup(src
, ld::Fixup::k4of5
, ld::Fixup::kindSubtractAddend
, offsetInFrom
);
5987 if ( sreloc
->r_length() & 1 ) {
5989 parser
.addFixup(src
, ld::Fixup::k5of5
, (isThumb
? ld::Fixup::kindStoreThumbHigh16
: ld::Fixup::kindStoreARMHigh16
));
5993 parser
.addFixup(src
, ld::Fixup::k5of5
, (isThumb
? ld::Fixup::kindStoreThumbLow16
: ld::Fixup::kindStoreARMLow16
));
5998 throw "ARM_RELOC_HALF_SECTDIFF reloc missing following pair";
6000 case ARM_RELOC_HALF
:
6001 if ( nextRelocIsPair
) {
6002 instruction
= LittleEndian::get32(*fixUpPtr
);
6003 Atom
<arm
>* targetAtom
= parser
.findAtomByAddress(sreloc
->r_value());
6004 uint32_t instruction16
;
6005 uint32_t other16
= (nextRelocAddress
& 0xFFFF);
6007 if ( sreloc
->r_length() & 2 ) {
6009 uint32_t i
= ((instruction
& 0x00000400) >> 10);
6010 uint32_t imm4
= (instruction
& 0x0000000F);
6011 uint32_t imm3
= ((instruction
& 0x70000000) >> 28);
6012 uint32_t imm8
= ((instruction
& 0x00FF0000) >> 16);
6013 instruction16
= (imm4
<< 12) | (i
<< 11) | (imm3
<< 8) | imm8
;
6017 uint32_t imm4
= ((instruction
& 0x000F0000) >> 16);
6018 uint32_t imm12
= (instruction
& 0x00000FFF);
6019 instruction16
= (imm4
<< 12) | imm12
;
6021 if ( sreloc
->r_length() & 1 )
6022 dstAddr
= ((instruction16
<< 16) | other16
);
6024 dstAddr
= (other16
<< 16) | instruction16
;
6025 if ( targetAtom
->scope() == ld::Atom::scopeTranslationUnit
) {
6026 parser
.addFixup(src
, ld::Fixup::k1of3
, ld::Fixup::kindSetTargetAddress
, targetAtom
);
6028 else if ( (targetAtom
->combine() == ld::Atom::combineByNameAndContent
) || (targetAtom
->combine() == ld::Atom::combineByNameAndReferences
) ) {
6029 parser
.addFixup(src
, ld::Fixup::k1of3
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, targetAtom
);
6032 parser
.addFixup(src
, ld::Fixup::k1of3
, ld::Fixup::kindSetTargetAddress
, false, targetAtom
->name());
6034 parser
.addFixup(src
, ld::Fixup::k2of3
, ld::Fixup::kindAddAddend
, dstAddr
- targetAtom
->_objAddress
);
6035 if ( sreloc
->r_length() & 1 ) {
6037 parser
.addFixup(src
, ld::Fixup::k3of3
, (isThumb
? ld::Fixup::kindStoreThumbHigh16
: ld::Fixup::kindStoreARMHigh16
));
6041 parser
.addFixup(src
, ld::Fixup::k3of3
, (isThumb
? ld::Fixup::kindStoreThumbLow16
: ld::Fixup::kindStoreARMLow16
));
6046 throw "scattered ARM_RELOC_HALF reloc missing following pair";
6049 throwf("unknown ARM scattered relocation type %d", sreloc
->r_type());
6060 template <typename A
>
6061 bool ObjC1ClassSection
<A
>::addRelocFixup(class Parser
<A
>& parser
, const macho_relocation_info
<P
>* reloc
)
6064 FixedSizeSection
<A
>::addRelocFixup(parser
, reloc
);
6066 assert(0 && "needs template specialization");
6071 bool ObjC1ClassSection
<x86
>::addRelocFixup(class Parser
<x86
>& parser
, const macho_relocation_info
<x86::P
>* reloc
)
6073 // if this is the reloc for the super class name string, add implicit reference to super class
6074 if ( ((reloc
->r_address() & R_SCATTERED
) == 0) && (reloc
->r_type() == GENERIC_RELOC_VANILLA
) ) {
6075 assert( reloc
->r_length() == 2 );
6076 assert( ! reloc
->r_pcrel() );
6078 const macho_section
<P
>* sect
= this->machoSection();
6079 Parser
<x86
>::SourceLocation src
;
6080 uint32_t srcAddr
= sect
->addr() + reloc
->r_address();
6081 src
.atom
= this->findAtomByAddress(srcAddr
);
6082 src
.offsetInAtom
= srcAddr
- src
.atom
->objectAddress();
6083 if ( src
.offsetInAtom
== 4 ) {
6084 Parser
<x86
>::TargetDesc stringTarget
;
6085 const uint8_t* fixUpPtr
= file().fileContent() + sect
->offset() + reloc
->r_address();
6086 uint32_t contentValue
= LittleEndian::get32(*((uint32_t*)fixUpPtr
));
6087 parser
.findTargetFromAddressAndSectionNum(contentValue
, reloc
->r_symbolnum(), stringTarget
);
6089 assert(stringTarget
.atom
!= NULL
);
6090 assert(stringTarget
.atom
->contentType() == ld::Atom::typeCString
);
6091 const char* superClassBaseName
= (char*)stringTarget
.atom
->rawContentPointer();
6092 char* superClassName
= new char[strlen(superClassBaseName
) + 20];
6093 strcpy(superClassName
, ".objc_class_name_");
6094 strcat(superClassName
, superClassBaseName
);
6096 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindSetTargetAddress
, false, superClassName
);
6100 return FixedSizeSection
<x86
>::addRelocFixup(parser
, reloc
);
6105 template <typename A
>
6106 bool Objc1ClassReferences
<A
>::addRelocFixup(class Parser
<A
>& parser
, const macho_relocation_info
<P
>* reloc
)
6109 PointerToCStringSection
<A
>::addRelocFixup(parser
, reloc
);
6111 assert(0 && "needs template specialization");
6118 bool Objc1ClassReferences
<x86
>::addRelocFixup(class Parser
<x86
>& parser
, const macho_relocation_info
<x86::P
>* reloc
)
6120 // add implict class refs, fixups not usable yet, so look at relocations
6121 assert( (reloc
->r_address() & R_SCATTERED
) == 0 );
6122 assert( reloc
->r_type() == GENERIC_RELOC_VANILLA
);
6123 assert( reloc
->r_length() == 2 );
6124 assert( ! reloc
->r_pcrel() );
6126 const macho_section
<P
>* sect
= this->machoSection();
6127 Parser
<x86
>::SourceLocation src
;
6128 uint32_t srcAddr
= sect
->addr() + reloc
->r_address();
6129 src
.atom
= this->findAtomByAddress(srcAddr
);
6130 src
.offsetInAtom
= srcAddr
- src
.atom
->objectAddress();
6131 Parser
<x86
>::TargetDesc stringTarget
;
6132 const uint8_t* fixUpPtr
= file().fileContent() + sect
->offset() + reloc
->r_address();
6133 uint32_t contentValue
= LittleEndian::get32(*((uint32_t*)fixUpPtr
));
6134 parser
.findTargetFromAddressAndSectionNum(contentValue
, reloc
->r_symbolnum(), stringTarget
);
6136 assert(stringTarget
.atom
!= NULL
);
6137 assert(stringTarget
.atom
->contentType() == ld::Atom::typeCString
);
6138 const char* baseClassName
= (char*)stringTarget
.atom
->rawContentPointer();
6139 char* objcClassName
= new char[strlen(baseClassName
) + 20];
6140 strcpy(objcClassName
, ".objc_class_name_");
6141 strcat(objcClassName
, baseClassName
);
6143 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindSetTargetAddress
, false, objcClassName
);
6146 return PointerToCStringSection
<x86
>::addRelocFixup(parser
, reloc
);
6150 template <typename A
>
6151 void Section
<A
>::makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
&)
6153 const macho_section
<P
>* sect
= this->machoSection();
6154 const macho_relocation_info
<P
>* relocs
= (macho_relocation_info
<P
>*)(file().fileContent() + sect
->reloff());
6155 const uint32_t relocCount
= sect
->nreloc();
6156 for (uint32_t r
= 0; r
< relocCount
; ++r
) {
6158 if ( this->addRelocFixup(parser
, &relocs
[r
]) )
6161 catch (const char* msg
) {
6162 throwf("in section %s,%s reloc %u: %s", sect
->segname(), Section
<A
>::makeSectionName(sect
), r
, msg
);
6166 // add follow-on fixups if .o file is missing .subsections_via_symbols
6167 if ( this->addFollowOnFixups() ) {
6168 Atom
<A
>* end
= &_endAtoms
[-1];
6169 for(Atom
<A
>* p
= _beginAtoms
; p
< end
; ++p
) {
6170 typename Parser
<A
>::SourceLocation
src(p
, 0);
6171 Atom
<A
>* nextAtom
= &p
[1];
6172 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindNoneFollowOn
, nextAtom
);
6175 else if ( this->type() == ld::Section::typeCode
) {
6176 // if FDE broke text not at a symbol, use followOn to keep code together
6177 Atom
<A
>* end
= &_endAtoms
[-1];
6178 for(Atom
<A
>* p
= _beginAtoms
; p
< end
; ++p
) {
6179 typename Parser
<A
>::SourceLocation
src(p
, 0);
6180 Atom
<A
>* nextAtom
= &p
[1];
6181 if ( (p
->symbolTableInclusion() == ld::Atom::symbolTableIn
) && (nextAtom
->symbolTableInclusion() == ld::Atom::symbolTableNotIn
) ) {
6182 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindNoneFollowOn
, nextAtom
);
6187 // <rdar://problem/9218847> track data-in-code
6188 if ( parser
.hasDataInCodeLabels() && (this->type() == ld::Section::typeCode
) ) {
6189 for (uint32_t i
=0; i
< parser
.symbolCount(); ++i
) {
6190 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(i
);
6192 if ( (sym
.n_type() & N_STAB
) != 0 )
6194 // ignore non-definitions
6195 if ( (sym
.n_type() & N_TYPE
) != N_SECT
)
6198 // 'L' labels do not denote atom breaks
6199 const char* symbolName
= parser
.nameFromSymbol(sym
);
6200 if ( symbolName
[0] == 'L' ) {
6201 if ( strncmp(symbolName
, "L$start$", 8) == 0 ) {
6202 ld::Fixup::Kind kind
= ld::Fixup::kindNone
;
6203 if ( strncmp(&symbolName
[8], "data$", 5) == 0 )
6204 kind
= ld::Fixup::kindDataInCodeStartData
;
6205 else if ( strncmp(&symbolName
[8], "code$", 5) == 0 )
6206 kind
= ld::Fixup::kindDataInCodeEnd
;
6207 else if ( strncmp(&symbolName
[8], "jt8$", 4) == 0 )
6208 kind
= ld::Fixup::kindDataInCodeStartJT8
;
6209 else if ( strncmp(&symbolName
[8], "jt16$", 4) == 0 )
6210 kind
= ld::Fixup::kindDataInCodeStartJT16
;
6211 else if ( strncmp(&symbolName
[8], "jt32$", 4) == 0 )
6212 kind
= ld::Fixup::kindDataInCodeStartJT32
;
6213 else if ( strncmp(&symbolName
[8], "jta32$", 4) == 0 )
6214 kind
= ld::Fixup::kindDataInCodeStartJTA32
;
6216 warning("unknown L$start$ label %s in file %s", symbolName
, this->file().path());
6217 if ( kind
!= ld::Fixup::kindNone
) {
6218 Atom
<A
>* inAtom
= parser
.findAtomByAddress(sym
.n_value());
6219 typename Parser
<A
>::SourceLocation
src(inAtom
, sym
.n_value() - inAtom
->objectAddress());
6220 parser
.addFixup(src
, ld::Fixup::k1of1
, kind
);
6227 // <rdar://problem/11150575> Handle LC_DATA_IN_CODE in object files
6228 if ( this->type() == ld::Section::typeCode
) {
6229 const pint_t startAddr
= this->_machOSection
->addr();
6230 const pint_t endAddr
= startAddr
+ this->_machOSection
->size();
6231 for ( const macho_data_in_code_entry
<P
>* p
= parser
.dataInCodeStart(); p
!= parser
.dataInCodeEnd(); ++p
) {
6232 if ( (p
->offset() >= startAddr
) && (p
->offset() < endAddr
) ) {
6233 ld::Fixup::Kind kind
= ld::Fixup::kindNone
;
6234 switch ( p
->kind() ) {
6235 case DICE_KIND_DATA
:
6236 kind
= ld::Fixup::kindDataInCodeStartData
;
6238 case DICE_KIND_JUMP_TABLE8
:
6239 kind
= ld::Fixup::kindDataInCodeStartJT8
;
6241 case DICE_KIND_JUMP_TABLE16
:
6242 kind
= ld::Fixup::kindDataInCodeStartJT16
;
6244 case DICE_KIND_JUMP_TABLE32
:
6245 kind
= ld::Fixup::kindDataInCodeStartJT32
;
6247 case DICE_KIND_ABS_JUMP_TABLE32
:
6248 kind
= ld::Fixup::kindDataInCodeStartJTA32
;
6251 kind
= ld::Fixup::kindDataInCodeStartData
;
6252 warning("uknown LC_DATA_IN_CODE kind (%d) at offset 0x%08X", p
->kind(), p
->offset());
6255 Atom
<A
>* inAtom
= parser
.findAtomByAddress(p
->offset());
6256 typename Parser
<A
>::SourceLocation
srcStart(inAtom
, p
->offset() - inAtom
->objectAddress());
6257 parser
.addFixup(srcStart
, ld::Fixup::k1of1
, kind
);
6258 typename Parser
<A
>::SourceLocation
srcEnd(inAtom
, p
->offset() + p
->length() - inAtom
->objectAddress());
6259 parser
.addFixup(srcEnd
, ld::Fixup::k1of1
, ld::Fixup::kindDataInCodeEnd
);
6265 // add follow-on fixups for aliases
6266 if ( _hasAliases
) {
6267 for(Atom
<A
>* p
= _beginAtoms
; p
< _endAtoms
; ++p
) {
6268 if ( p
->isAlias() && ! this->addFollowOnFixups() ) {
6269 Atom
<A
>* targetOfAlias
= &p
[1];
6270 assert(p
< &_endAtoms
[-1]);
6271 assert(p
->_objAddress
== targetOfAlias
->_objAddress
);
6272 typename Parser
<A
>::SourceLocation
src(p
, 0);
6273 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindNoneFollowOn
, targetOfAlias
);
6282 // main function used by linker to instantiate ld::Files
6284 ld::relocatable::File
* parse(const uint8_t* fileContent
, uint64_t fileLength
,
6285 const char* path
, time_t modTime
, ld::File::Ordinal ordinal
, const ParserOptions
& opts
)
6287 switch ( opts
.architecture
) {
6288 #if SUPPORT_ARCH_x86_64
6289 case CPU_TYPE_X86_64
:
6290 if ( mach_o::relocatable::Parser
<x86_64
>::validFile(fileContent
) )
6291 return mach_o::relocatable::Parser
<x86_64
>::parse(fileContent
, fileLength
, path
, modTime
, ordinal
, opts
);
6294 #if SUPPORT_ARCH_i386
6296 if ( mach_o::relocatable::Parser
<x86
>::validFile(fileContent
) )
6297 return mach_o::relocatable::Parser
<x86
>::parse(fileContent
, fileLength
, path
, modTime
, ordinal
, opts
);
6300 #if SUPPORT_ARCH_arm_any
6302 if ( mach_o::relocatable::Parser
<arm
>::validFile(fileContent
, opts
.objSubtypeMustMatch
, opts
.subType
) )
6303 return mach_o::relocatable::Parser
<arm
>::parse(fileContent
, fileLength
, path
, modTime
, ordinal
, opts
);
6311 // used by archive reader to validate member object file
6313 bool isObjectFile(const uint8_t* fileContent
, uint64_t fileLength
, const ParserOptions
& opts
)
6315 switch ( opts
.architecture
) {
6316 case CPU_TYPE_X86_64
:
6317 return ( mach_o::relocatable::Parser
<x86_64
>::validFile(fileContent
) );
6319 return ( mach_o::relocatable::Parser
<x86
>::validFile(fileContent
) );
6321 return ( mach_o::relocatable::Parser
<arm
>::validFile(fileContent
, opts
.objSubtypeMustMatch
, opts
.subType
) );
6327 // used by linker to infer architecture when no -arch is on command line
6329 bool isObjectFile(const uint8_t* fileContent
, cpu_type_t
* result
, cpu_subtype_t
* subResult
)
6331 if ( mach_o::relocatable::Parser
<x86_64
>::validFile(fileContent
) ) {
6332 *result
= CPU_TYPE_X86_64
;
6333 *subResult
= CPU_SUBTYPE_X86_64_ALL
;
6336 if ( mach_o::relocatable::Parser
<x86
>::validFile(fileContent
) ) {
6337 *result
= CPU_TYPE_I386
;
6338 *subResult
= CPU_SUBTYPE_X86_ALL
;
6341 if ( mach_o::relocatable::Parser
<arm
>::validFile(fileContent
, false, 0) ) {
6342 *result
= CPU_TYPE_ARM
;
6343 const macho_header
<Pointer32
<LittleEndian
> >* header
= (const macho_header
<Pointer32
<LittleEndian
> >*)fileContent
;
6344 *subResult
= header
->cpusubtype();
6351 // used by linker is error messages to describe bad .o file
6353 const char* archName(const uint8_t* fileContent
)
6355 if ( mach_o::relocatable::Parser
<x86_64
>::validFile(fileContent
) ) {
6356 return mach_o::relocatable::Parser
<x86_64
>::fileKind(fileContent
);
6358 if ( mach_o::relocatable::Parser
<x86
>::validFile(fileContent
) ) {
6359 return mach_o::relocatable::Parser
<x86
>::fileKind(fileContent
);
6361 if ( mach_o::relocatable::Parser
<arm
>::validFile(fileContent
, false, 0) ) {
6362 return mach_o::relocatable::Parser
<arm
>::fileKind(fileContent
);
6368 // Used by archive reader when -ObjC option is specified
6370 bool hasObjC2Categories(const uint8_t* fileContent
)
6372 if ( mach_o::relocatable::Parser
<x86_64
>::validFile(fileContent
) ) {
6373 return mach_o::relocatable::Parser
<x86_64
>::hasObjC2Categories(fileContent
);
6375 else if ( mach_o::relocatable::Parser
<arm
>::validFile(fileContent
, false, 0) ) {
6376 return mach_o::relocatable::Parser
<arm
>::hasObjC2Categories(fileContent
);
6378 else if ( mach_o::relocatable::Parser
<x86
>::validFile(fileContent
, false, 0) ) {
6379 return mach_o::relocatable::Parser
<x86
>::hasObjC2Categories(fileContent
);
6385 // Used by archive reader when -ObjC option is specified
6387 bool hasObjC1Categories(const uint8_t* fileContent
)
6389 if ( mach_o::relocatable::Parser
<x86
>::validFile(fileContent
, false, 0) ) {
6390 return mach_o::relocatable::Parser
<x86
>::hasObjC1Categories(fileContent
);
6397 } // namespace relocatable
6398 } // namespace mach_o