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 uint32_t fileOffset
= sct
->offset() - sct
->addr() + this->_objAddress
;
822 return this->sect().file().fileContent()+fileOffset
;
826 template <typename A
>
827 void Atom
<A
>::copyRawContent(uint8_t buffer
[]) const
830 if ( this->contentType() == ld::Atom::typeZeroFill
) {
831 bzero(buffer
, _size
);
833 else if ( _size
!= 0 ) {
834 memcpy(buffer
, this->contentPointer(), _size
);
839 void Atom
<arm
>::verifyAlignment() const
841 if ( (this->section().type() == ld::Section::typeCode
) && ! isThumb() ) {
842 if ( ((_objAddress
% 4) != 0) || (this->alignment().powerOf2
< 2) )
843 warning("ARM function not 4-byte aligned: %s from %s", this->name(), this->file()->path());
847 template <typename A
>
848 void Atom
<A
>::verifyAlignment() const
853 template <typename A
>
857 static bool validFile(const uint8_t* fileContent
, bool subtypeMustMatch
=false,
858 cpu_subtype_t subtype
=0);
859 static const char* fileKind(const uint8_t* fileContent
);
860 static bool hasObjC2Categories(const uint8_t* fileContent
);
861 static bool hasObjC1Categories(const uint8_t* fileContent
);
862 static ld::relocatable::File
* parse(const uint8_t* fileContent
, uint64_t fileLength
,
863 const char* path
, time_t modTime
, ld::File::Ordinal ordinal
,
864 const ParserOptions
& opts
) {
865 Parser
p(fileContent
, fileLength
, path
, modTime
,
866 ordinal
, opts
.convertUnwindInfo
);
867 return p
.parse(opts
);
870 typedef typename
A::P P
;
871 typedef typename
A::P::E E
;
872 typedef typename
A::P::uint_t pint_t
;
874 struct SourceLocation
{
876 SourceLocation(Atom
<A
>* a
, uint32_t o
) : atom(a
), offsetInAtom(o
) {}
878 uint32_t offsetInAtom
;
883 const char* name
; // only used if targetAtom is NULL
885 bool weakImport
; // only used if targetAtom is NULL
889 FixupInAtom(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, Atom
<A
>* target
) :
890 fixup(src
.offsetInAtom
, c
, k
, target
), atom(src
.atom
) { src
.atom
->incrementFixupCount(); }
892 FixupInAtom(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, ld::Fixup::TargetBinding b
, Atom
<A
>* target
) :
893 fixup(src
.offsetInAtom
, c
, k
, b
, target
), atom(src
.atom
) { src
.atom
->incrementFixupCount(); }
895 FixupInAtom(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, bool wi
, const char* name
) :
896 fixup(src
.offsetInAtom
, c
, k
, wi
, name
), atom(src
.atom
) { src
.atom
->incrementFixupCount(); }
898 FixupInAtom(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, ld::Fixup::TargetBinding b
, const char* name
) :
899 fixup(src
.offsetInAtom
, c
, k
, b
, name
), atom(src
.atom
) { src
.atom
->incrementFixupCount(); }
901 FixupInAtom(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, uint64_t addend
) :
902 fixup(src
.offsetInAtom
, c
, k
, addend
), atom(src
.atom
) { src
.atom
->incrementFixupCount(); }
904 FixupInAtom(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
) :
905 fixup(src
.offsetInAtom
, c
, k
, (uint64_t)0), atom(src
.atom
) { src
.atom
->incrementFixupCount(); }
911 void addFixup(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, Atom
<A
>* target
) {
912 _allFixups
.push_back(FixupInAtom(src
, c
, k
, target
));
915 void addFixup(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, ld::Fixup::TargetBinding b
, Atom
<A
>* target
) {
916 _allFixups
.push_back(FixupInAtom(src
, c
, k
, b
, target
));
919 void addFixup(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, bool wi
, const char* name
) {
920 _allFixups
.push_back(FixupInAtom(src
, c
, k
, wi
, name
));
923 void addFixup(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, ld::Fixup::TargetBinding b
, const char* name
) {
924 _allFixups
.push_back(FixupInAtom(src
, c
, k
, b
, name
));
927 void addFixup(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, uint64_t addend
) {
928 _allFixups
.push_back(FixupInAtom(src
, c
, k
, addend
));
931 void addFixup(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
) {
932 _allFixups
.push_back(FixupInAtom(src
, c
, k
));
936 uint32_t symbolCount() { return _symbolCount
; }
937 uint32_t indirectSymbol(uint32_t indirectIndex
);
938 const macho_nlist
<P
>& symbolFromIndex(uint32_t index
);
939 const char* nameFromSymbol(const macho_nlist
<P
>& sym
);
940 ld::Atom::Scope
scopeFromSymbol(const macho_nlist
<P
>& sym
);
941 static ld::Atom::Definition
definitionFromSymbol(const macho_nlist
<P
>& sym
);
942 static ld::Atom::Combine
combineFromSymbol(const macho_nlist
<P
>& sym
);
943 ld::Atom::SymbolTableInclusion
inclusionFromSymbol(const macho_nlist
<P
>& sym
);
944 static bool dontDeadStripFromSymbol(const macho_nlist
<P
>& sym
);
945 static bool isThumbFromSymbol(const macho_nlist
<P
>& sym
);
946 static bool weakImportFromSymbol(const macho_nlist
<P
>& sym
);
947 static bool resolverFromSymbol(const macho_nlist
<P
>& sym
);
948 uint32_t symbolIndexFromIndirectSectionAddress(pint_t
,const macho_section
<P
>*);
949 const macho_section
<P
>* firstMachOSection() { return _sectionsStart
; }
950 const macho_section
<P
>* machOSectionFromSectionIndex(uint32_t index
);
951 uint32_t machOSectionCount() { return _machOSectionsCount
; }
952 uint32_t undefinedStartIndex() { return _undefinedStartIndex
; }
953 uint32_t undefinedEndIndex() { return _undefinedEndIndex
; }
954 void addFixup(FixupInAtom f
) { _allFixups
.push_back(f
); }
955 Section
<A
>* sectionForNum(unsigned int sectNum
);
956 Section
<A
>* sectionForAddress(pint_t addr
);
957 Atom
<A
>* findAtomByAddress(pint_t addr
);
958 Atom
<A
>* findAtomByAddressOrNullIfStub(pint_t addr
);
959 Atom
<A
>* findAtomByAddressOrLocalTargetOfStub(pint_t addr
, uint32_t* offsetInAtom
);
960 Atom
<A
>* findAtomByName(const char* name
); // slow!
961 void findTargetFromAddress(pint_t addr
, TargetDesc
& target
);
962 void findTargetFromAddress(pint_t baseAddr
, pint_t addr
, TargetDesc
& target
);
963 void findTargetFromAddressAndSectionNum(pint_t addr
, unsigned int sectNum
,
965 uint32_t tentativeDefinitionCount() { return _tentativeDefinitionCount
; }
966 uint32_t absoluteSymbolCount() { return _absoluteSymbolCount
; }
968 bool hasStubsSection() { return (_stubsSectionNum
!= 0); }
969 unsigned int stubsSectionNum() { return _stubsSectionNum
; }
970 void addDtraceExtraInfos(const SourceLocation
& src
, const char* provider
);
971 const char* scanSymbolTableForAddress(uint64_t addr
);
972 bool convertUnwindInfo() { return _convertUnwindInfo
; }
973 bool hasDataInCodeLabels() { return _hasDataInCodeLabels
; }
975 macho_data_in_code_entry
<P
>* dataInCodeStart() { return _dataInCodeStart
; }
976 macho_data_in_code_entry
<P
>* dataInCodeEnd() { return _dataInCodeEnd
; }
978 void addFixups(const SourceLocation
& src
, ld::Fixup::Kind kind
, const TargetDesc
& target
);
979 void addFixups(const SourceLocation
& src
, ld::Fixup::Kind kind
, const TargetDesc
& target
, const TargetDesc
& picBase
);
983 struct LabelAndCFIBreakIterator
{
984 typedef typename CFISection
<A
>::CFI_Atom_Info CFI_Atom_Info
;
985 LabelAndCFIBreakIterator(const uint32_t* ssa
, uint32_t ssc
, const pint_t
* cfisa
,
986 uint32_t cfisc
, bool ols
)
987 : sortedSymbolIndexes(ssa
), sortedSymbolCount(ssc
), cfiStartsArray(cfisa
),
988 cfiStartsCount(cfisc
), fileHasOverlappingSymbols(ols
),
989 newSection(false), cfiIndex(0), symIndex(0) {}
990 bool next(Parser
<A
>& parser
, uint32_t sectNum
, pint_t startAddr
, pint_t endAddr
,
991 pint_t
* addr
, pint_t
* size
, const macho_nlist
<P
>** sym
);
992 pint_t
peek(Parser
<A
>& parser
, pint_t startAddr
, pint_t endAddr
);
993 void beginSection() { newSection
= true; symIndex
= 0; }
995 const uint32_t* const sortedSymbolIndexes
;
996 const uint32_t sortedSymbolCount
;
997 const pint_t
* cfiStartsArray
;
998 const uint32_t cfiStartsCount
;
999 const bool fileHasOverlappingSymbols
;
1005 struct CFI_CU_InfoArrays
{
1006 typedef typename CFISection
<A
>::CFI_Atom_Info CFI_Atom_Info
;
1007 typedef typename CUSection
<A
>::Info CU_Info
;
1008 CFI_CU_InfoArrays(const CFI_Atom_Info
* cfiAr
, uint32_t cfiC
, CU_Info
* cuAr
, uint32_t cuC
)
1009 : cfiArray(cfiAr
), cuArray(cuAr
), cfiCount(cfiC
), cuCount(cuC
) {}
1010 const CFI_Atom_Info
* const cfiArray
;
1011 CU_Info
* const cuArray
;
1012 const uint32_t cfiCount
;
1013 const uint32_t cuCount
;
1019 friend class Section
<A
>;
1021 enum SectionType
{ sectionTypeIgnore
, sectionTypeLiteral4
, sectionTypeLiteral8
, sectionTypeLiteral16
,
1022 sectionTypeNonLazy
, sectionTypeCFI
, sectionTypeCString
, sectionTypeCStringPointer
,
1023 sectionTypeUTF16Strings
, sectionTypeCFString
, sectionTypeObjC2ClassRefs
, typeObjC2CategoryList
,
1024 sectionTypeObjC1Classes
, sectionTypeSymboled
, sectionTypeObjC1ClassRefs
,
1025 sectionTypeTentativeDefinitions
, sectionTypeAbsoluteSymbols
, sectionTypeTLVDefs
,
1026 sectionTypeCompactUnwind
};
1028 template <typename P
>
1029 struct MachOSectionAndSectionClass
1031 const macho_section
<P
>* sect
;
1034 static int sorter(const void* l
, const void* r
) {
1035 const MachOSectionAndSectionClass
<P
>* left
= (MachOSectionAndSectionClass
<P
>*)l
;
1036 const MachOSectionAndSectionClass
<P
>* right
= (MachOSectionAndSectionClass
<P
>*)r
;
1037 int64_t diff
= left
->sect
->addr() - right
->sect
->addr();
1047 struct ParserAndSectionsArray
{ Parser
* parser
; const uint32_t* sortedSectionsArray
; };
1050 Parser(const uint8_t* fileContent
, uint64_t fileLength
,
1051 const char* path
, time_t modTime
,
1052 ld::File::Ordinal ordinal
, bool convertUnwindInfo
);
1053 ld::relocatable::File
* parse(const ParserOptions
& opts
);
1054 uint8_t loadCommandSizeMask();
1055 bool parseLoadCommands();
1056 void makeSections();
1057 void prescanSymbolTable();
1058 void makeSortedSymbolsArray(uint32_t symArray
[], const uint32_t sectionArray
[]);
1059 void makeSortedSectionsArray(uint32_t array
[]);
1060 static int pointerSorter(const void* l
, const void* r
);
1061 static int symbolIndexSorter(void* extra
, const void* l
, const void* r
);
1062 static int sectionIndexSorter(void* extra
, const void* l
, const void* r
);
1064 void parseDebugInfo();
1066 static bool isConstFunStabs(const char *stabStr
);
1067 bool read_comp_unit(const char ** name
, const char ** comp_dir
,
1068 uint64_t *stmt_list
);
1069 const char* getDwarfString(uint64_t form
, const uint8_t* p
);
1070 bool skip_form(const uint8_t ** offset
, const uint8_t * end
,
1071 uint64_t form
, uint8_t addr_size
, bool dwarf64
);
1074 // filled in by constructor
1075 const uint8_t* _fileContent
;
1076 uint32_t _fileLength
;
1079 ld::File::Ordinal _ordinal
;
1081 // filled in by parseLoadCommands()
1083 const macho_nlist
<P
>* _symbols
;
1084 uint32_t _symbolCount
;
1085 const char* _strings
;
1086 uint32_t _stringsSize
;
1087 const uint32_t* _indirectTable
;
1088 uint32_t _indirectTableCount
;
1089 uint32_t _undefinedStartIndex
;
1090 uint32_t _undefinedEndIndex
;
1091 const macho_section
<P
>* _sectionsStart
;
1092 uint32_t _machOSectionsCount
;
1094 macho_data_in_code_entry
<P
>* _dataInCodeStart
;
1095 macho_data_in_code_entry
<P
>* _dataInCodeEnd
;
1097 // filled in by parse()
1098 CFISection
<A
>* _EHFrameSection
;
1099 CUSection
<A
>* _compactUnwindSection
;
1100 AbsoluteSymbolSection
<A
>* _absoluteSection
;
1101 uint32_t _tentativeDefinitionCount
;
1102 uint32_t _absoluteSymbolCount
;
1103 uint32_t _symbolsInSections
;
1104 bool _hasLongBranchStubs
;
1105 bool _AppleObjc
; // FSF has objc that uses different data layout
1106 bool _overlappingSymbols
;
1107 bool _convertUnwindInfo
;
1108 bool _hasDataInCodeLabels
;
1109 unsigned int _stubsSectionNum
;
1110 const macho_section
<P
>* _stubsMachOSection
;
1111 std::vector
<const char*> _dtraceProviderInfo
;
1112 std::vector
<FixupInAtom
> _allFixups
;
1117 template <typename A
>
1118 Parser
<A
>::Parser(const uint8_t* fileContent
, uint64_t fileLength
, const char* path
, time_t modTime
,
1119 ld::File::Ordinal ordinal
, bool convertDUI
)
1120 : _fileContent(fileContent
), _fileLength(fileLength
), _path(path
), _modTime(modTime
),
1121 _ordinal(ordinal
), _file(NULL
),
1122 _symbols(NULL
), _symbolCount(0), _strings(NULL
), _stringsSize(0),
1123 _indirectTable(NULL
), _indirectTableCount(0),
1124 _undefinedStartIndex(0), _undefinedEndIndex(0),
1125 _sectionsStart(NULL
), _machOSectionsCount(0), _hasUUID(false),
1126 _dataInCodeStart(NULL
), _dataInCodeEnd(NULL
),
1127 _EHFrameSection(NULL
), _compactUnwindSection(NULL
), _absoluteSection(NULL
),
1128 _tentativeDefinitionCount(0), _absoluteSymbolCount(0),
1129 _symbolsInSections(0), _hasLongBranchStubs(false), _AppleObjc(false),
1130 _overlappingSymbols(false), _convertUnwindInfo(convertDUI
), _hasDataInCodeLabels(false),
1131 _stubsSectionNum(0), _stubsMachOSection(NULL
)
1137 bool Parser
<x86
>::validFile(const uint8_t* fileContent
, bool, cpu_subtype_t
)
1139 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1140 if ( header
->magic() != MH_MAGIC
)
1142 if ( header
->cputype() != CPU_TYPE_I386
)
1144 if ( header
->filetype() != MH_OBJECT
)
1150 bool Parser
<x86_64
>::validFile(const uint8_t* fileContent
, bool, cpu_subtype_t
)
1152 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1153 if ( header
->magic() != MH_MAGIC_64
)
1155 if ( header
->cputype() != CPU_TYPE_X86_64
)
1157 if ( header
->filetype() != MH_OBJECT
)
1163 bool Parser
<arm
>::validFile(const uint8_t* fileContent
, bool subtypeMustMatch
, cpu_subtype_t subtype
)
1165 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1166 if ( header
->magic() != MH_MAGIC
)
1168 if ( header
->cputype() != CPU_TYPE_ARM
)
1170 if ( header
->filetype() != MH_OBJECT
)
1172 if ( subtypeMustMatch
) {
1173 if ( (cpu_subtype_t
)header
->cpusubtype() == subtype
)
1175 // hack until libcc_kext.a is made fat
1176 if ( header
->cpusubtype() == CPU_SUBTYPE_ARM_ALL
)
1186 const char* Parser
<x86
>::fileKind(const uint8_t* fileContent
)
1188 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1189 if ( header
->magic() != MH_MAGIC
)
1191 if ( header
->cputype() != CPU_TYPE_I386
)
1197 const char* Parser
<x86_64
>::fileKind(const uint8_t* fileContent
)
1199 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1200 if ( header
->magic() != MH_MAGIC
)
1202 if ( header
->cputype() != CPU_TYPE_X86_64
)
1208 const char* Parser
<arm
>::fileKind(const uint8_t* fileContent
)
1210 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1211 if ( header
->magic() != MH_MAGIC
)
1213 if ( header
->cputype() != CPU_TYPE_ARM
)
1215 for (const ArchInfo
* t
=archInfoArray
; t
->archName
!= NULL
; ++t
) {
1216 if ( (t
->cpuType
== CPU_TYPE_ARM
) && ((cpu_subtype_t
)header
->cpusubtype() == t
->cpuSubType
) ) {
1224 template <typename A
>
1225 bool Parser
<A
>::hasObjC2Categories(const uint8_t* fileContent
)
1227 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1228 const uint32_t cmd_count
= header
->ncmds();
1229 const macho_load_command
<P
>* const cmds
= (macho_load_command
<P
>*)((char*)header
+ sizeof(macho_header
<P
>));
1230 const macho_load_command
<P
>* const cmdsEnd
= (macho_load_command
<P
>*)((char*)header
+ sizeof(macho_header
<P
>) + header
->sizeofcmds());
1231 const macho_load_command
<P
>* cmd
= cmds
;
1232 for (uint32_t i
= 0; i
< cmd_count
; ++i
) {
1233 if ( cmd
->cmd() == macho_segment_command
<P
>::CMD
) {
1234 const macho_segment_command
<P
>* segment
= (macho_segment_command
<P
>*)cmd
;
1235 const macho_section
<P
>* sectionsStart
= (macho_section
<P
>*)((char*)segment
+ sizeof(macho_segment_command
<P
>));
1236 for (uint32_t si
=0; si
< segment
->nsects(); ++si
) {
1237 const macho_section
<P
>* sect
= §ionsStart
[si
];
1238 if ( (sect
->size() > 0)
1239 && (strcmp(sect
->sectname(), "__objc_catlist") == 0)
1240 && (strcmp(sect
->segname(), "__DATA") == 0) ) {
1245 cmd
= (const macho_load_command
<P
>*)(((char*)cmd
)+cmd
->cmdsize());
1246 if ( cmd
> cmdsEnd
)
1247 throwf("malformed mach-o file, load command #%d is outside size of load commands", i
);
1253 template <typename A
>
1254 bool Parser
<A
>::hasObjC1Categories(const uint8_t* fileContent
)
1256 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1257 const uint32_t cmd_count
= header
->ncmds();
1258 const macho_load_command
<P
>* const cmds
= (macho_load_command
<P
>*)((char*)header
+ sizeof(macho_header
<P
>));
1259 const macho_load_command
<P
>* const cmdsEnd
= (macho_load_command
<P
>*)((char*)header
+ sizeof(macho_header
<P
>) + header
->sizeofcmds());
1260 const macho_load_command
<P
>* cmd
= cmds
;
1261 for (uint32_t i
= 0; i
< cmd_count
; ++i
) {
1262 if ( cmd
->cmd() == macho_segment_command
<P
>::CMD
) {
1263 const macho_segment_command
<P
>* segment
= (macho_segment_command
<P
>*)cmd
;
1264 const macho_section
<P
>* sectionsStart
= (macho_section
<P
>*)((char*)segment
+ sizeof(macho_segment_command
<P
>));
1265 for (uint32_t si
=0; si
< segment
->nsects(); ++si
) {
1266 const macho_section
<P
>* sect
= §ionsStart
[si
];
1267 if ( (sect
->size() > 0)
1268 && (strcmp(sect
->sectname(), "__category") == 0)
1269 && (strcmp(sect
->segname(), "__OBJC") == 0) ) {
1274 cmd
= (const macho_load_command
<P
>*)(((char*)cmd
)+cmd
->cmdsize());
1275 if ( cmd
> cmdsEnd
)
1276 throwf("malformed mach-o file, load command #%d is outside size of load commands", i
);
1281 template <typename A
>
1282 int Parser
<A
>::pointerSorter(const void* l
, const void* r
)
1284 // sort references by address
1285 const pint_t
* left
= (pint_t
*)l
;
1286 const pint_t
* right
= (pint_t
*)r
;
1287 return (*left
- *right
);
1290 template <typename A
>
1291 typename
A::P::uint_t Parser
<A
>::LabelAndCFIBreakIterator::peek(Parser
<A
>& parser
, pint_t startAddr
, pint_t endAddr
)
1294 if ( symIndex
< sortedSymbolCount
)
1295 symbolAddr
= parser
.symbolFromIndex(sortedSymbolIndexes
[symIndex
]).n_value();
1297 symbolAddr
= endAddr
;
1299 if ( cfiIndex
< cfiStartsCount
)
1300 cfiAddr
= cfiStartsArray
[cfiIndex
];
1303 if ( (cfiAddr
< symbolAddr
) && (cfiAddr
>= startAddr
) ) {
1304 if ( cfiAddr
< endAddr
)
1310 if ( symbolAddr
< endAddr
)
1318 // Parses up a section into chunks based on labels and CFI information.
1319 // Each call returns the next chunk address and size, and (if the break
1320 // was becuase of a label, the symbol). Returns false when no more chunks.
1322 template <typename A
>
1323 bool Parser
<A
>::LabelAndCFIBreakIterator::next(Parser
<A
>& parser
, uint32_t sectNum
, pint_t startAddr
, pint_t endAddr
,
1324 pint_t
* addr
, pint_t
* size
, const macho_nlist
<P
>** symbol
)
1326 // may not be a label on start of section, but need atom demarcation there
1329 // advance symIndex until we get to the first label at or past the start of this section
1330 while ( symIndex
< sortedSymbolCount
) {
1331 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(sortedSymbolIndexes
[symIndex
]);
1332 pint_t nextSymbolAddr
= sym
.n_value();
1333 //fprintf(stderr, "sectNum=%d, nextSymbolAddr=0x%08llX, name=%s\n", sectNum, (uint64_t)nextSymbolAddr, parser.nameFromSymbol(sym));
1334 if ( (nextSymbolAddr
> startAddr
) || ((nextSymbolAddr
== startAddr
) && (sym
.n_sect() == sectNum
)) )
1338 if ( symIndex
< sortedSymbolCount
) {
1339 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(sortedSymbolIndexes
[symIndex
]);
1340 pint_t nextSymbolAddr
= sym
.n_value();
1341 // if next symbol found is not in this section
1342 if ( sym
.n_sect() != sectNum
) {
1343 // check for CFI break instead of symbol break
1344 if ( cfiIndex
< cfiStartsCount
) {
1345 pint_t nextCfiAddr
= cfiStartsArray
[cfiIndex
];
1346 if ( nextCfiAddr
< endAddr
) {
1349 *addr
= nextCfiAddr
;
1350 *size
= peek(parser
, startAddr
, endAddr
) - nextCfiAddr
;
1356 *size
= endAddr
- startAddr
;
1358 if ( startAddr
== endAddr
)
1359 return false; // zero size section
1361 return true; // whole section is one atom with no label
1363 // if also CFI break here, eat it
1364 if ( cfiIndex
< cfiStartsCount
) {
1365 if ( cfiStartsArray
[cfiIndex
] == nextSymbolAddr
)
1368 if ( nextSymbolAddr
== startAddr
) {
1369 // label at start of section, return it as chunk
1372 *size
= peek(parser
, startAddr
, endAddr
) - startAddr
;
1376 // return chunk before first symbol
1378 *size
= nextSymbolAddr
- startAddr
;
1382 // no symbols left in whole file, so entire section is one chunk
1384 *size
= endAddr
- startAddr
;
1386 if ( startAddr
== endAddr
)
1387 return false; // zero size section
1389 return true; // whole section is one atom with no label
1392 while ( (symIndex
< sortedSymbolCount
) && (cfiIndex
< cfiStartsCount
) ) {
1393 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(sortedSymbolIndexes
[symIndex
]);
1394 pint_t nextSymbolAddr
= sym
.n_value();
1395 pint_t nextCfiAddr
= cfiStartsArray
[cfiIndex
];
1396 if ( nextSymbolAddr
< nextCfiAddr
) {
1397 if ( nextSymbolAddr
>= endAddr
)
1400 if ( nextSymbolAddr
< startAddr
)
1402 *addr
= nextSymbolAddr
;
1403 *size
= peek(parser
, startAddr
, endAddr
) - nextSymbolAddr
;
1407 else if ( nextCfiAddr
< nextSymbolAddr
) {
1408 if ( nextCfiAddr
>= endAddr
)
1411 if ( nextCfiAddr
< startAddr
)
1413 *addr
= nextCfiAddr
;
1414 *size
= peek(parser
, startAddr
, endAddr
) - nextCfiAddr
;
1419 if ( nextCfiAddr
>= endAddr
)
1423 if ( nextCfiAddr
< startAddr
)
1425 *addr
= nextCfiAddr
;
1426 *size
= peek(parser
, startAddr
, endAddr
) - nextCfiAddr
;
1431 while ( symIndex
< sortedSymbolCount
) {
1432 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(sortedSymbolIndexes
[symIndex
]);
1433 pint_t nextSymbolAddr
= sym
.n_value();
1434 // if next symbol found is not in this section, then done with iteration
1435 if ( sym
.n_sect() != sectNum
)
1438 if ( nextSymbolAddr
< startAddr
)
1440 *addr
= nextSymbolAddr
;
1441 *size
= peek(parser
, startAddr
, endAddr
) - nextSymbolAddr
;
1445 while ( cfiIndex
< cfiStartsCount
) {
1446 pint_t nextCfiAddr
= cfiStartsArray
[cfiIndex
];
1447 if ( nextCfiAddr
>= endAddr
)
1450 if ( nextCfiAddr
< startAddr
)
1452 *addr
= nextCfiAddr
;
1453 *size
= peek(parser
, startAddr
, endAddr
) - nextCfiAddr
;
1462 template <typename A
>
1463 ld::relocatable::File
* Parser
<A
>::parse(const ParserOptions
& opts
)
1465 // create file object
1466 _file
= new File
<A
>(_path
, _modTime
, _fileContent
, _ordinal
);
1468 // respond to -t option
1469 if ( opts
.logAllFiles
)
1470 printf("%s\n", _path
);
1472 // parse start of mach-o file
1473 if ( ! parseLoadCommands() )
1477 uint32_t sortedSectionIndexes
[_machOSectionsCount
];
1478 this->makeSortedSectionsArray(sortedSectionIndexes
);
1480 // make symbol table sorted by address
1481 this->prescanSymbolTable();
1482 uint32_t sortedSymbolIndexes
[_symbolsInSections
];
1483 this->makeSortedSymbolsArray(sortedSymbolIndexes
, sortedSectionIndexes
);
1485 // allocate Section<A> object for each mach-o section
1488 // if it exists, do special early parsing of __compact_unwind section
1489 uint32_t countOfCUs
= 0;
1490 if ( _compactUnwindSection
!= NULL
)
1491 countOfCUs
= _compactUnwindSection
->count();
1492 uint8_t cuInfoBuffer
[sizeof(typename CUSection
<A
>::Info
) * countOfCUs
];
1493 typename CUSection
<A
>::Info
* cuInfoArray
= (typename CUSection
<A
>::Info
*)cuInfoBuffer
;
1494 if ( countOfCUs
!= 0 )
1495 _compactUnwindSection
->parse(*this, countOfCUs
, cuInfoArray
);
1497 // if it exists, do special early parsing of __eh_frame section
1498 // stack allocate array of CFI_Atom_Info
1499 uint32_t countOfCFIs
= 0;
1500 if ( _EHFrameSection
!= NULL
)
1501 countOfCFIs
= _EHFrameSection
->cfiCount();
1502 typename CFISection
<A
>::CFI_Atom_Info cfiArray
[countOfCFIs
];
1503 // stack allocate (if not too large) a copy of __eh_frame to apply relocations to
1504 uint8_t* ehBuffer
= NULL
;
1505 uint32_t stackAllocSize
= 0;
1506 if ( (countOfCFIs
!= 0) && _EHFrameSection
->needsRelocating() ) {
1507 uint32_t sectSize
= _EHFrameSection
->machoSection()->size();
1508 if ( sectSize
> 50*1024 )
1509 ehBuffer
= (uint8_t*)malloc(sectSize
);
1511 stackAllocSize
= sectSize
;
1513 uint32_t ehStackBuffer
[1+stackAllocSize
/4]; // make 4-byte aligned stack bufffer
1514 if ( ehBuffer
== NULL
)
1515 ehBuffer
= (uint8_t*)&ehStackBuffer
;
1516 uint32_t cfiStartsCount
= 0;
1517 if ( countOfCFIs
!= 0 ) {
1518 _EHFrameSection
->cfiParse(*this, ehBuffer
, cfiArray
, countOfCFIs
);
1519 // count functions and lsdas
1520 for(uint32_t i
=0; i
< countOfCFIs
; ++i
) {
1521 if ( cfiArray
[i
].isCIE
)
1523 //fprintf(stderr, "cfiArray[i].func = 0x%08llX, cfiArray[i].lsda = 0x%08llX, encoding=0x%08X\n",
1524 // (uint64_t)cfiArray[i].u.fdeInfo.function.targetAddress,
1525 // (uint64_t)cfiArray[i].u.fdeInfo.lsda.targetAddress,
1526 // cfiArray[i].u.fdeInfo.compactUnwindInfo);
1527 if ( cfiArray
[i
].u
.fdeInfo
.function
.targetAddress
!= CFI_INVALID_ADDRESS
)
1529 if ( cfiArray
[i
].u
.fdeInfo
.lsda
.targetAddress
!= CFI_INVALID_ADDRESS
)
1533 CFI_CU_InfoArrays
cfis(cfiArray
, countOfCFIs
, cuInfoArray
, countOfCUs
);
1535 // create sorted array of function starts and lsda starts
1536 pint_t cfiStartsArray
[cfiStartsCount
];
1537 uint32_t countOfFDEs
= 0;
1538 if ( countOfCFIs
!= 0 ) {
1540 for(uint32_t i
=0; i
< countOfCFIs
; ++i
) {
1541 if ( cfiArray
[i
].isCIE
)
1543 if ( cfiArray
[i
].u
.fdeInfo
.function
.targetAddress
!= CFI_INVALID_ADDRESS
)
1544 cfiStartsArray
[index
++] = cfiArray
[i
].u
.fdeInfo
.function
.targetAddress
;
1545 if ( cfiArray
[i
].u
.fdeInfo
.lsda
.targetAddress
!= CFI_INVALID_ADDRESS
)
1546 cfiStartsArray
[index
++] = cfiArray
[i
].u
.fdeInfo
.lsda
.targetAddress
;
1549 ::qsort(cfiStartsArray
, cfiStartsCount
, sizeof(pint_t
), pointerSorter
);
1551 // scan for FDEs claming the same function
1552 for(int i
=1; i
< index
; ++i
) {
1553 assert( cfiStartsArray
[i
] != cfiStartsArray
[i
-1] );
1558 Section
<A
>** sections
= _file
->_sectionsArray
;
1559 uint32_t sectionsCount
= _file
->_sectionsArrayCount
;
1561 // figure out how many atoms will be allocated and allocate
1562 LabelAndCFIBreakIterator
breakIterator(sortedSymbolIndexes
, _symbolsInSections
, cfiStartsArray
,
1563 cfiStartsCount
, _overlappingSymbols
);
1564 uint32_t computedAtomCount
= 0;
1565 for (uint32_t i
=0; i
< sectionsCount
; ++i
) {
1566 breakIterator
.beginSection();
1567 uint32_t count
= sections
[i
]->computeAtomCount(*this, breakIterator
, cfis
);
1568 //const macho_section<P>* sect = sections[i]->machoSection();
1569 //fprintf(stderr, "computed count=%u for section %s size=%llu\n", count, sect->sectname(), (sect != NULL) ? sect->size() : 0);
1570 computedAtomCount
+= count
;
1572 //fprintf(stderr, "allocating %d atoms * sizeof(Atom<A>)=%ld, sizeof(ld::Atom)=%ld\n", computedAtomCount, sizeof(Atom<A>), sizeof(ld::Atom));
1573 _file
->_atomsArray
= new uint8_t[computedAtomCount
*sizeof(Atom
<A
>)];
1574 _file
->_atomsArrayCount
= 0;
1576 // have each section append atoms to _atomsArray
1577 LabelAndCFIBreakIterator
breakIterator2(sortedSymbolIndexes
, _symbolsInSections
, cfiStartsArray
,
1578 cfiStartsCount
, _overlappingSymbols
);
1579 for (uint32_t i
=0; i
< sectionsCount
; ++i
) {
1580 uint8_t* atoms
= _file
->_atomsArray
+ _file
->_atomsArrayCount
*sizeof(Atom
<A
>);
1581 breakIterator2
.beginSection();
1582 uint32_t count
= sections
[i
]->appendAtoms(*this, atoms
, breakIterator2
, cfis
);
1583 //fprintf(stderr, "append count=%u for section %s/%s\n", count, sections[i]->machoSection()->segname(), sections[i]->machoSection()->sectname());
1584 _file
->_atomsArrayCount
+= count
;
1586 assert( _file
->_atomsArrayCount
== computedAtomCount
&& "more atoms allocated than expected");
1589 // have each section add all fix-ups for its atoms
1590 _allFixups
.reserve(computedAtomCount
*5);
1591 for (uint32_t i
=0; i
< sectionsCount
; ++i
)
1592 sections
[i
]->makeFixups(*this, cfis
);
1594 // assign fixups start offset for each atom
1595 uint8_t* p
= _file
->_atomsArray
;
1596 uint32_t fixupOffset
= 0;
1597 for(int i
=_file
->_atomsArrayCount
; i
> 0; --i
) {
1598 Atom
<A
>* atom
= (Atom
<A
>*)p
;
1599 atom
->_fixupsStartIndex
= fixupOffset
;
1600 fixupOffset
+= atom
->_fixupsCount
;
1601 atom
->_fixupsCount
= 0;
1602 p
+= sizeof(Atom
<A
>);
1604 assert(fixupOffset
== _allFixups
.size());
1605 _file
->_fixups
.reserve(fixupOffset
);
1607 // copy each fixup for each atom
1608 for(typename
std::vector
<FixupInAtom
>::iterator it
=_allFixups
.begin(); it
!= _allFixups
.end(); ++it
) {
1609 uint32_t slot
= it
->atom
->_fixupsStartIndex
+ it
->atom
->_fixupsCount
;
1610 _file
->_fixups
[slot
] = it
->fixup
;
1611 it
->atom
->_fixupsCount
++;
1614 // done with temp vector
1618 _file
->_unwindInfos
.reserve(countOfFDEs
+countOfCUs
);
1619 for(uint32_t i
=0; i
< countOfCFIs
; ++i
) {
1620 if ( cfiArray
[i
].isCIE
)
1622 if ( cfiArray
[i
].u
.fdeInfo
.function
.targetAddress
!= CFI_INVALID_ADDRESS
) {
1623 ld::Atom::UnwindInfo info
;
1624 info
.startOffset
= 0;
1625 info
.unwindInfo
= cfiArray
[i
].u
.fdeInfo
.compactUnwindInfo
;
1626 _file
->_unwindInfos
.push_back(info
);
1627 Atom
<A
>* func
= findAtomByAddress(cfiArray
[i
].u
.fdeInfo
.function
.targetAddress
);
1628 func
->setUnwindInfoRange(_file
->_unwindInfos
.size()-1, 1);
1631 // apply compact infos in __LD,__compact_unwind section to each function
1632 // if function also has dwarf unwind, CU will override it
1633 Atom
<A
>* lastFunc
= NULL
;
1634 uint32_t lastEnd
= 0;
1635 for(uint32_t i
=0; i
< countOfCUs
; ++i
) {
1636 typename CUSection
<A
>::Info
* info
= &cuInfoArray
[i
];
1637 assert(info
->function
!= NULL
);
1638 ld::Atom::UnwindInfo ui
;
1639 ui
.startOffset
= info
->functionStartAddress
- info
->function
->objectAddress();
1640 ui
.unwindInfo
= info
->compactUnwindInfo
;
1641 _file
->_unwindInfos
.push_back(ui
);
1642 // if previous is for same function, extend range
1643 if ( info
->function
== lastFunc
) {
1644 if ( lastEnd
!= ui
.startOffset
) {
1645 if ( lastEnd
< ui
.startOffset
)
1646 warning("__LD,__compact_unwind entries for %s have a gap at offset 0x%0X", info
->function
->name(), lastEnd
);
1648 warning("__LD,__compact_unwind entries for %s overlap at offset 0x%0X", info
->function
->name(), lastEnd
);
1650 lastFunc
->extendUnwindInfoRange();
1653 info
->function
->setUnwindInfoRange(_file
->_unwindInfos
.size()-1, 1);
1654 lastFunc
= info
->function
;
1655 lastEnd
= ui
.startOffset
+ info
->rangeLength
;
1658 // parse dwarf debug info to get line info
1659 this->parseDebugInfo();
1666 template <> uint8_t Parser
<x86
>::loadCommandSizeMask() { return 0x03; }
1667 template <> uint8_t Parser
<x86_64
>::loadCommandSizeMask() { return 0x07; }
1668 template <> uint8_t Parser
<arm
>::loadCommandSizeMask() { return 0x03; }
1670 template <typename A
>
1671 bool Parser
<A
>::parseLoadCommands()
1673 const macho_header
<P
>* header
= (const macho_header
<P
>*)_fileContent
;
1675 // set File attributes
1676 _file
->_canScatterAtoms
= (header
->flags() & MH_SUBSECTIONS_VIA_SYMBOLS
);
1677 _file
->_cpuSubType
= header
->cpusubtype();
1679 const macho_segment_command
<P
>* segment
= NULL
;
1680 const uint8_t* const endOfFile
= _fileContent
+ _fileLength
;
1681 const uint32_t cmd_count
= header
->ncmds();
1682 // <rdar://problem/5394172> an empty .o file with zero load commands will crash linker
1683 if ( cmd_count
== 0 )
1685 const macho_load_command
<P
>* const cmds
= (macho_load_command
<P
>*)((char*)header
+ sizeof(macho_header
<P
>));
1686 const macho_load_command
<P
>* const cmdsEnd
= (macho_load_command
<P
>*)((char*)header
+ sizeof(macho_header
<P
>) + header
->sizeofcmds());
1687 const macho_load_command
<P
>* cmd
= cmds
;
1688 for (uint32_t i
= 0; i
< cmd_count
; ++i
) {
1689 uint32_t size
= cmd
->cmdsize();
1690 if ( (size
& this->loadCommandSizeMask()) != 0 )
1691 throwf("load command #%d has a unaligned size", i
);
1692 const uint8_t* endOfCmd
= ((uint8_t*)cmd
)+cmd
->cmdsize();
1693 if ( endOfCmd
> (uint8_t*)cmdsEnd
)
1694 throwf("load command #%d extends beyond the end of the load commands", i
);
1695 if ( endOfCmd
> endOfFile
)
1696 throwf("load command #%d extends beyond the end of the file", i
);
1697 switch (cmd
->cmd()) {
1700 const macho_symtab_command
<P
>* symtab
= (macho_symtab_command
<P
>*)cmd
;
1701 _symbolCount
= symtab
->nsyms();
1702 _symbols
= (const macho_nlist
<P
>*)(_fileContent
+ symtab
->symoff());
1703 _strings
= (char*)_fileContent
+ symtab
->stroff();
1704 _stringsSize
= symtab
->strsize();
1705 if ( (symtab
->symoff() + _symbolCount
*sizeof(macho_nlist
<P
>)) > _fileLength
)
1706 throw "mach-o symbol table extends beyond end of file";
1707 if ( (_strings
+ _stringsSize
) > (char*)endOfFile
)
1708 throw "mach-o string pool extends beyond end of file";
1709 if ( _indirectTable
== NULL
) {
1710 if ( _undefinedEndIndex
== 0 ) {
1711 _undefinedStartIndex
= 0;
1712 _undefinedEndIndex
= symtab
->nsyms();
1719 const macho_dysymtab_command
<P
>* dsymtab
= (macho_dysymtab_command
<P
>*)cmd
;
1720 _indirectTable
= (uint32_t*)(_fileContent
+ dsymtab
->indirectsymoff());
1721 _indirectTableCount
= dsymtab
->nindirectsyms();
1722 if ( &_indirectTable
[_indirectTableCount
] > (uint32_t*)endOfFile
)
1723 throw "indirect symbol table extends beyond end of file";
1724 _undefinedStartIndex
= dsymtab
->iundefsym();
1725 _undefinedEndIndex
= _undefinedStartIndex
+ dsymtab
->nundefsym();
1731 case LC_DATA_IN_CODE
:
1733 const macho_linkedit_data_command
<P
>* dc
= (macho_linkedit_data_command
<P
>*)cmd
;
1734 _dataInCodeStart
= (macho_data_in_code_entry
<P
>*)(_fileContent
+ dc
->dataoff());
1735 _dataInCodeEnd
= (macho_data_in_code_entry
<P
>*)(_fileContent
+ dc
->dataoff() + dc
->datasize());
1736 if ( _dataInCodeEnd
> (macho_data_in_code_entry
<P
>*)endOfFile
)
1737 throw "LC_DATA_IN_CODE table extends beyond end of file";
1740 if ( cmd
->cmd() == macho_segment_command
<P
>::CMD
) {
1741 if ( segment
!= NULL
)
1742 throw "more than one LC_SEGMENT found in object file";
1743 segment
= (macho_segment_command
<P
>*)cmd
;
1747 cmd
= (const macho_load_command
<P
>*)(((char*)cmd
)+cmd
->cmdsize());
1748 if ( cmd
> cmdsEnd
)
1749 throwf("malformed mach-o file, load command #%d is outside size of load commands", i
);
1752 // record range of sections
1753 if ( segment
== NULL
)
1754 throw "missing LC_SEGMENT";
1755 _sectionsStart
= (macho_section
<P
>*)((char*)segment
+ sizeof(macho_segment_command
<P
>));
1756 _machOSectionsCount
= segment
->nsects();
1762 template <typename A
>
1763 void Parser
<A
>::prescanSymbolTable()
1765 _tentativeDefinitionCount
= 0;
1766 _absoluteSymbolCount
= 0;
1767 _symbolsInSections
= 0;
1768 _hasDataInCodeLabels
= false;
1769 for (uint32_t i
=0; i
< this->_symbolCount
; ++i
) {
1770 const macho_nlist
<P
>& sym
= symbolFromIndex(i
);
1772 if ( (sym
.n_type() & N_STAB
) != 0 )
1775 // look at undefines
1776 const char* symbolName
= this->nameFromSymbol(sym
);
1777 if ( (sym
.n_type() & N_TYPE
) == N_UNDF
) {
1778 if ( sym
.n_value() != 0 ) {
1779 // count tentative definitions
1780 ++_tentativeDefinitionCount
;
1782 else if ( strncmp(symbolName
, "___dtrace_", 10) == 0 ) {
1783 // any undefined starting with __dtrace_*$ that is not ___dtrace_probe$* or ___dtrace_isenabled$*
1784 // is extra provider info
1785 if ( (strncmp(&symbolName
[10], "probe$", 6) != 0) && (strncmp(&symbolName
[10], "isenabled$", 10) != 0) ) {
1786 _dtraceProviderInfo
.push_back(symbolName
);
1792 // count absolute symbols
1793 if ( (sym
.n_type() & N_TYPE
) == N_ABS
) {
1794 const char* absName
= this->nameFromSymbol(sym
);
1795 // ignore .objc_class_name_* symbols
1796 if ( strncmp(absName
, ".objc_class_name_", 17) == 0 ) {
1800 // ignore .objc_class_name_* symbols
1801 if ( strncmp(absName
, ".objc_category_name_", 20) == 0 )
1803 // ignore empty *.eh symbols
1804 if ( strcmp(&absName
[strlen(absName
)-3], ".eh") == 0 )
1806 ++_absoluteSymbolCount
;
1809 // only look at definitions
1810 if ( (sym
.n_type() & N_TYPE
) != N_SECT
)
1813 // 'L' labels do not denote atom breaks
1814 if ( symbolName
[0] == 'L' ) {
1815 // <rdar://problem/9218847> Formalize data in code with L$start$ labels
1816 if ( strncmp(symbolName
, "L$start$", 8) == 0 )
1817 _hasDataInCodeLabels
= true;
1820 // how many def syms in each section
1821 if ( sym
.n_sect() > _machOSectionsCount
)
1822 throw "bad n_sect in symbol table";
1824 _symbolsInSections
++;
1828 template <typename A
>
1829 int Parser
<A
>::sectionIndexSorter(void* extra
, const void* l
, const void* r
)
1831 Parser
<A
>* parser
= (Parser
<A
>*)extra
;
1832 const uint32_t* left
= (uint32_t*)l
;
1833 const uint32_t* right
= (uint32_t*)r
;
1834 const macho_section
<P
>* leftSect
= parser
->machOSectionFromSectionIndex(*left
);
1835 const macho_section
<P
>* rightSect
= parser
->machOSectionFromSectionIndex(*right
);
1837 // can't just return difference because 64-bit diff does not fit in 32-bit return type
1838 int64_t result
= leftSect
->addr() - rightSect
->addr();
1839 if ( result
== 0 ) {
1840 // two sections with same start address
1841 // one with zero size goes first
1842 bool leftEmpty
= ( leftSect
->size() == 0 );
1843 bool rightEmpty
= ( rightSect
->size() == 0 );
1844 if ( leftEmpty
!= rightEmpty
) {
1845 return ( rightEmpty
? 1 : -1 );
1847 if ( !leftEmpty
&& !rightEmpty
)
1848 throwf("overlapping sections");
1849 // both empty, so chose file order
1850 return ( rightSect
- leftSect
);
1852 else if ( result
< 0 )
1858 template <typename A
>
1859 void Parser
<A
>::makeSortedSectionsArray(uint32_t array
[])
1861 const bool log
= false;
1864 fprintf(stderr
, "unsorted sections:\n");
1865 for(unsigned int i
=0; i
< _machOSectionsCount
; ++i
)
1866 fprintf(stderr
, "0x%08llX %s %s\n", _sectionsStart
[i
].addr(), _sectionsStart
[i
].segname(), _sectionsStart
[i
].sectname());
1869 // sort by symbol table address
1870 for (uint32_t i
=0; i
< _machOSectionsCount
; ++i
)
1872 ::qsort_r(array
, _machOSectionsCount
, sizeof(uint32_t), this, §ionIndexSorter
);
1875 fprintf(stderr
, "sorted sections:\n");
1876 for(unsigned int i
=0; i
< _machOSectionsCount
; ++i
)
1877 fprintf(stderr
, "0x%08llX %s %s\n", _sectionsStart
[array
[i
]].addr(), _sectionsStart
[array
[i
]].segname(), _sectionsStart
[array
[i
]].sectname());
1883 template <typename A
>
1884 int Parser
<A
>::symbolIndexSorter(void* extra
, const void* l
, const void* r
)
1886 ParserAndSectionsArray
* extraInfo
= (ParserAndSectionsArray
*)extra
;
1887 Parser
<A
>* parser
= extraInfo
->parser
;
1888 const uint32_t* sortedSectionsArray
= extraInfo
->sortedSectionsArray
;
1889 const uint32_t* left
= (uint32_t*)l
;
1890 const uint32_t* right
= (uint32_t*)r
;
1891 const macho_nlist
<P
>& leftSym
= parser
->symbolFromIndex(*left
);
1892 const macho_nlist
<P
>& rightSym
= parser
->symbolFromIndex(*right
);
1893 // can't just return difference because 64-bit diff does not fit in 32-bit return type
1894 int64_t result
= leftSym
.n_value() - rightSym
.n_value();
1895 if ( result
== 0 ) {
1896 // two symbols with same address
1897 // if in different sections, sort earlier section first
1898 if ( leftSym
.n_sect() != rightSym
.n_sect() ) {
1899 for (uint32_t i
=0; i
< parser
->machOSectionCount(); ++i
) {
1900 if ( sortedSectionsArray
[i
]+1 == leftSym
.n_sect() )
1902 if ( sortedSectionsArray
[i
]+1 == rightSym
.n_sect() )
1906 // two symbols in same section, means one is an alias
1907 // if only one is global, make the other an alias (sort first)
1908 if ( (leftSym
.n_type() & N_EXT
) != (rightSym
.n_type() & N_EXT
) ) {
1909 if ( (rightSym
.n_type() & N_EXT
) != 0 )
1914 // if both are global, make alphabetically last one be the alias
1915 return ( strcmp(parser
->nameFromSymbol(rightSym
), parser
->nameFromSymbol(leftSym
)) );
1917 else if ( result
< 0 )
1924 template <typename A
>
1925 void Parser
<A
>::makeSortedSymbolsArray(uint32_t array
[], const uint32_t sectionArray
[])
1927 const bool log
= false;
1929 uint32_t* p
= array
;
1930 for (uint32_t i
=0; i
< this->_symbolCount
; ++i
) {
1931 const macho_nlist
<P
>& sym
= symbolFromIndex(i
);
1933 if ( (sym
.n_type() & N_STAB
) != 0 )
1936 // only look at definitions
1937 if ( (sym
.n_type() & N_TYPE
) != N_SECT
)
1940 // 'L' labels do not denote atom breaks
1941 const char* symbolName
= this->nameFromSymbol(sym
);
1942 if ( symbolName
[0] == 'L' )
1945 // how many def syms in each section
1946 if ( sym
.n_sect() > _machOSectionsCount
)
1947 throw "bad n_sect in symbol table";
1952 assert(p
== &array
[_symbolsInSections
] && "second pass over symbol table yield a different number of symbols");
1954 // sort by symbol table address
1955 ParserAndSectionsArray extra
= { this, sectionArray
};
1956 ::qsort_r(array
, _symbolsInSections
, sizeof(uint32_t), &extra
, &symbolIndexSorter
);
1958 // look for two symbols at same address
1959 _overlappingSymbols
= false;
1960 for (unsigned int i
=1; i
< _symbolsInSections
; ++i
) {
1961 if ( symbolFromIndex(array
[i
-1]).n_value() == symbolFromIndex(array
[i
]).n_value() ) {
1962 //fprintf(stderr, "overlapping symbols at 0x%08llX\n", symbolFromIndex(array[i-1]).n_value());
1963 _overlappingSymbols
= true;
1968 fprintf(stderr
, "sorted symbols:\n");
1969 for(unsigned int i
=0; i
< _symbolsInSections
; ++i
)
1970 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
])) );
1975 template <typename A
>
1976 void Parser
<A
>::makeSections()
1978 // classify each section by type
1979 // compute how many Section objects will be needed and total size for all
1980 unsigned int totalSectionsSize
= 0;
1981 uint8_t machOSectsStorage
[sizeof(MachOSectionAndSectionClass
<P
>)*(_machOSectionsCount
+2)]; // also room for tentative-defs and absolute symbols
1982 // allocate raw storage for all section objects on stack
1983 MachOSectionAndSectionClass
<P
>* machOSects
= (MachOSectionAndSectionClass
<P
>*)machOSectsStorage
;
1984 unsigned int count
= 0;
1985 for (uint32_t i
=0; i
< _machOSectionsCount
; ++i
) {
1986 const macho_section
<P
>* sect
= &_sectionsStart
[i
];
1987 if ( (sect
->flags() & S_ATTR_DEBUG
) != 0 ) {
1988 if ( strcmp(sect
->segname(), "__DWARF") == 0 ) {
1989 // note that .o file has dwarf
1990 _file
->_debugInfoKind
= ld::relocatable::File::kDebugInfoDwarf
;
1991 // save off iteresting dwarf sections
1992 if ( strcmp(sect
->sectname(), "__debug_info") == 0 )
1993 _file
->_dwarfDebugInfoSect
= sect
;
1994 else if ( strcmp(sect
->sectname(), "__debug_abbrev") == 0 )
1995 _file
->_dwarfDebugAbbrevSect
= sect
;
1996 else if ( strcmp(sect
->sectname(), "__debug_line") == 0 )
1997 _file
->_dwarfDebugLineSect
= sect
;
1998 else if ( strcmp(sect
->sectname(), "__debug_str") == 0 )
1999 _file
->_dwarfDebugStringSect
= sect
;
2000 // linker does not propagate dwarf sections to output file
2003 else if ( strcmp(sect
->segname(), "__LD") == 0 ) {
2004 if ( strncmp(sect
->sectname(), "__compact_unwind", 16) == 0 ) {
2005 machOSects
[count
].sect
= sect
;
2006 totalSectionsSize
+= sizeof(CUSection
<A
>);
2007 machOSects
[count
++].type
= sectionTypeCompactUnwind
;
2012 // ignore empty __OBJC sections
2013 if ( (sect
->size() == 0) && (strcmp(sect
->segname(), "__OBJC") == 0) )
2015 // objc image info section is really attributes and not content
2016 if ( ((strcmp(sect
->sectname(), "__image_info") == 0) && (strcmp(sect
->segname(), "__OBJC") == 0))
2017 || ((strncmp(sect
->sectname(), "__objc_imageinfo", 16) == 0) && (strcmp(sect
->segname(), "__DATA") == 0)) ) {
2018 // struct objc_image_info {
2019 // uint32_t version; // initially 0
2022 // #define OBJC_IMAGE_SUPPORTS_GC 2
2023 // #define OBJC_IMAGE_GC_ONLY 4
2025 const uint32_t* contents
= (uint32_t*)(_file
->fileContent()+sect
->offset());
2026 if ( (sect
->size() >= 8) && (contents
[0] == 0) ) {
2027 uint32_t flags
= E::get32(contents
[1]);
2028 if ( (flags
& 4) == 4 )
2029 _file
->_objConstraint
= ld::File::objcConstraintGC
;
2030 else if ( (flags
& 2) == 2 )
2031 _file
->_objConstraint
= ld::File::objcConstraintRetainReleaseOrGC
;
2033 _file
->_objConstraint
= ld::File::objcConstraintRetainRelease
;
2034 if ( sect
->size() > 8 ) {
2035 warning("section %s/%s has unexpectedly large size %llu in %s",
2036 sect
->segname(), Section
<A
>::makeSectionName(sect
), sect
->size(), _file
->path());
2040 warning("can't parse %s/%s section in %s", sect
->segname(), Section
<A
>::makeSectionName(sect
), _file
->path());
2044 machOSects
[count
].sect
= sect
;
2045 switch ( sect
->flags() & SECTION_TYPE
) {
2046 case S_SYMBOL_STUBS
:
2047 if ( _stubsSectionNum
== 0 ) {
2048 _stubsSectionNum
= i
+1;
2049 _stubsMachOSection
= sect
;
2052 assert(1 && "multiple S_SYMBOL_STUBS sections");
2053 case S_LAZY_SYMBOL_POINTERS
:
2055 case S_4BYTE_LITERALS
:
2056 totalSectionsSize
+= sizeof(Literal4Section
<A
>);
2057 machOSects
[count
++].type
= sectionTypeLiteral4
;
2059 case S_8BYTE_LITERALS
:
2060 totalSectionsSize
+= sizeof(Literal8Section
<A
>);
2061 machOSects
[count
++].type
= sectionTypeLiteral8
;
2063 case S_16BYTE_LITERALS
:
2064 totalSectionsSize
+= sizeof(Literal16Section
<A
>);
2065 machOSects
[count
++].type
= sectionTypeLiteral16
;
2067 case S_NON_LAZY_SYMBOL_POINTERS
:
2068 totalSectionsSize
+= sizeof(NonLazyPointerSection
<A
>);
2069 machOSects
[count
++].type
= sectionTypeNonLazy
;
2071 case S_LITERAL_POINTERS
:
2072 if ( (strcmp(sect
->segname(), "__OBJC") == 0) && (strcmp(sect
->sectname(), "__cls_refs") == 0) ) {
2073 totalSectionsSize
+= sizeof(Objc1ClassReferences
<A
>);
2074 machOSects
[count
++].type
= sectionTypeObjC1ClassRefs
;
2077 totalSectionsSize
+= sizeof(PointerToCStringSection
<A
>);
2078 machOSects
[count
++].type
= sectionTypeCStringPointer
;
2081 case S_CSTRING_LITERALS
:
2082 totalSectionsSize
+= sizeof(CStringSection
<A
>);
2083 machOSects
[count
++].type
= sectionTypeCString
;
2085 case S_MOD_INIT_FUNC_POINTERS
:
2086 case S_MOD_TERM_FUNC_POINTERS
:
2087 case S_THREAD_LOCAL_INIT_FUNCTION_POINTERS
:
2092 case S_THREAD_LOCAL_REGULAR
:
2093 case S_THREAD_LOCAL_ZEROFILL
:
2094 if ( (strcmp(sect
->segname(), "__TEXT") == 0) && (strcmp(sect
->sectname(), "__eh_frame") == 0) ) {
2095 totalSectionsSize
+= sizeof(CFISection
<A
>);
2096 machOSects
[count
++].type
= sectionTypeCFI
;
2098 else if ( (strcmp(sect
->segname(), "__DATA") == 0) && (strcmp(sect
->sectname(), "__cfstring") == 0) ) {
2099 totalSectionsSize
+= sizeof(CFStringSection
<A
>);
2100 machOSects
[count
++].type
= sectionTypeCFString
;
2102 else if ( (strcmp(sect
->segname(), "__TEXT") == 0) && (strcmp(sect
->sectname(), "__ustring") == 0) ) {
2103 totalSectionsSize
+= sizeof(UTF16StringSection
<A
>);
2104 machOSects
[count
++].type
= sectionTypeUTF16Strings
;
2106 else if ( (strcmp(sect
->segname(), "__DATA") == 0) && (strncmp(sect
->sectname(), "__objc_classrefs", 16) == 0) ) {
2107 totalSectionsSize
+= sizeof(ObjC2ClassRefsSection
<A
>);
2108 machOSects
[count
++].type
= sectionTypeObjC2ClassRefs
;
2110 else if ( (strcmp(sect
->segname(), "__DATA") == 0) && (strcmp(sect
->sectname(), "__objc_catlist") == 0) ) {
2111 totalSectionsSize
+= sizeof(ObjC2CategoryListSection
<A
>);
2112 machOSects
[count
++].type
= typeObjC2CategoryList
;
2114 else if ( _AppleObjc
&& (strcmp(sect
->segname(), "__OBJC") == 0) && (strcmp(sect
->sectname(), "__class") == 0) ) {
2115 totalSectionsSize
+= sizeof(ObjC1ClassSection
<A
>);
2116 machOSects
[count
++].type
= sectionTypeObjC1Classes
;
2119 totalSectionsSize
+= sizeof(SymboledSection
<A
>);
2120 machOSects
[count
++].type
= sectionTypeSymboled
;
2123 case S_THREAD_LOCAL_VARIABLES
:
2124 totalSectionsSize
+= sizeof(TLVDefsSection
<A
>);
2125 machOSects
[count
++].type
= sectionTypeTLVDefs
;
2127 case S_THREAD_LOCAL_VARIABLE_POINTERS
:
2129 throwf("unknown section type %d", sect
->flags() & SECTION_TYPE
);
2133 // sort by address (mach-o object files don't aways have sections sorted)
2134 ::qsort(machOSects
, count
, sizeof(MachOSectionAndSectionClass
<P
>), MachOSectionAndSectionClass
<P
>::sorter
);
2136 // we will synthesize a dummy Section<A> object for tentative definitions
2137 if ( _tentativeDefinitionCount
> 0 ) {
2138 totalSectionsSize
+= sizeof(TentativeDefinitionSection
<A
>);
2139 machOSects
[count
++].type
= sectionTypeTentativeDefinitions
;
2142 // we will synthesize a dummy Section<A> object for Absolute symbols
2143 if ( _absoluteSymbolCount
> 0 ) {
2144 totalSectionsSize
+= sizeof(AbsoluteSymbolSection
<A
>);
2145 machOSects
[count
++].type
= sectionTypeAbsoluteSymbols
;
2148 // allocate one block for all Section objects as well as pointers to each
2149 uint8_t* space
= new uint8_t[totalSectionsSize
+count
*sizeof(Section
<A
>*)];
2150 _file
->_sectionsArray
= (Section
<A
>**)space
;
2151 _file
->_sectionsArrayCount
= count
;
2152 Section
<A
>** objects
= _file
->_sectionsArray
;
2153 space
+= count
*sizeof(Section
<A
>*);
2154 for (uint32_t i
=0; i
< count
; ++i
) {
2155 switch ( machOSects
[i
].type
) {
2156 case sectionTypeIgnore
:
2158 case sectionTypeLiteral4
:
2159 *objects
++ = new (space
) Literal4Section
<A
>(*this, *_file
, machOSects
[i
].sect
);
2160 space
+= sizeof(Literal4Section
<A
>);
2162 case sectionTypeLiteral8
:
2163 *objects
++ = new (space
) Literal8Section
<A
>(*this, *_file
, machOSects
[i
].sect
);
2164 space
+= sizeof(Literal8Section
<A
>);
2166 case sectionTypeLiteral16
:
2167 *objects
++ = new (space
) Literal16Section
<A
>(*this, *_file
, machOSects
[i
].sect
);
2168 space
+= sizeof(Literal16Section
<A
>);
2170 case sectionTypeNonLazy
:
2171 *objects
++ = new (space
) NonLazyPointerSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2172 space
+= sizeof(NonLazyPointerSection
<A
>);
2174 case sectionTypeCFI
:
2175 _EHFrameSection
= new (space
) CFISection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2176 *objects
++ = _EHFrameSection
;
2177 space
+= sizeof(CFISection
<A
>);
2179 case sectionTypeCString
:
2180 *objects
++ = new (space
) CStringSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2181 space
+= sizeof(CStringSection
<A
>);
2183 case sectionTypeCStringPointer
:
2184 *objects
++ = new (space
) PointerToCStringSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2185 space
+= sizeof(PointerToCStringSection
<A
>);
2187 case sectionTypeObjC1ClassRefs
:
2188 *objects
++ = new (space
) Objc1ClassReferences
<A
>(*this, *_file
, machOSects
[i
].sect
);
2189 space
+= sizeof(Objc1ClassReferences
<A
>);
2191 case sectionTypeUTF16Strings
:
2192 *objects
++ = new (space
) UTF16StringSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2193 space
+= sizeof(UTF16StringSection
<A
>);
2195 case sectionTypeCFString
:
2196 *objects
++ = new (space
) CFStringSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2197 space
+= sizeof(CFStringSection
<A
>);
2199 case sectionTypeObjC2ClassRefs
:
2200 *objects
++ = new (space
) ObjC2ClassRefsSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2201 space
+= sizeof(ObjC2ClassRefsSection
<A
>);
2203 case typeObjC2CategoryList
:
2204 *objects
++ = new (space
) ObjC2CategoryListSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2205 space
+= sizeof(ObjC2CategoryListSection
<A
>);
2207 case sectionTypeObjC1Classes
:
2208 *objects
++ = new (space
) ObjC1ClassSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2209 space
+= sizeof(ObjC1ClassSection
<A
>);
2211 case sectionTypeSymboled
:
2212 *objects
++ = new (space
) SymboledSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2213 space
+= sizeof(SymboledSection
<A
>);
2215 case sectionTypeTLVDefs
:
2216 *objects
++ = new (space
) TLVDefsSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2217 space
+= sizeof(TLVDefsSection
<A
>);
2219 case sectionTypeCompactUnwind
:
2220 _compactUnwindSection
= new (space
) CUSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2221 *objects
++ = _compactUnwindSection
;
2222 space
+= sizeof(CUSection
<A
>);
2224 case sectionTypeTentativeDefinitions
:
2225 *objects
++ = new (space
) TentativeDefinitionSection
<A
>(*this, *_file
);
2226 space
+= sizeof(TentativeDefinitionSection
<A
>);
2228 case sectionTypeAbsoluteSymbols
:
2229 _absoluteSection
= new (space
) AbsoluteSymbolSection
<A
>(*this, *_file
);
2230 *objects
++ = _absoluteSection
;
2231 space
+= sizeof(AbsoluteSymbolSection
<A
>);
2234 throw "internal error uknown SectionType";
2240 template <typename A
>
2241 Section
<A
>* Parser
<A
>::sectionForAddress(typename
A::P::uint_t addr
)
2243 for (uint32_t i
=0; i
< _file
->_sectionsArrayCount
; ++i
) {
2244 const macho_section
<typename
A::P
>* sect
= _file
->_sectionsArray
[i
]->machoSection();
2245 // TentativeDefinitionSection and AbsoluteSymbolSection have no mach-o section
2246 if ( sect
!= NULL
) {
2247 if ( (sect
->addr() <= addr
) && (addr
< (sect
->addr()+sect
->size())) ) {
2248 return _file
->_sectionsArray
[i
];
2252 // not strictly in any section
2253 // may be in a zero length section
2254 for (uint32_t i
=0; i
< _file
->_sectionsArrayCount
; ++i
) {
2255 const macho_section
<typename
A::P
>* sect
= _file
->_sectionsArray
[i
]->machoSection();
2256 // TentativeDefinitionSection and AbsoluteSymbolSection have no mach-o section
2257 if ( sect
!= NULL
) {
2258 if ( (sect
->addr() == addr
) && (sect
->size() == 0) ) {
2259 return _file
->_sectionsArray
[i
];
2264 throwf("sectionForAddress(0x%llX) address not in any section", (uint64_t)addr
);
2267 template <typename A
>
2268 Section
<A
>* Parser
<A
>::sectionForNum(unsigned int num
)
2270 for (uint32_t i
=0; i
< _file
->_sectionsArrayCount
; ++i
) {
2271 const macho_section
<typename
A::P
>* sect
= _file
->_sectionsArray
[i
]->machoSection();
2272 // TentativeDefinitionSection and AbsoluteSymbolSection have no mach-o section
2273 if ( sect
!= NULL
) {
2274 if ( num
== (unsigned int)((sect
- _sectionsStart
)+1) )
2275 return _file
->_sectionsArray
[i
];
2278 throwf("sectionForNum(%u) section number not for any section", num
);
2281 template <typename A
>
2282 Atom
<A
>* Parser
<A
>::findAtomByAddress(pint_t addr
)
2284 Section
<A
>* section
= this->sectionForAddress(addr
);
2285 return section
->findAtomByAddress(addr
);
2288 template <typename A
>
2289 Atom
<A
>* Parser
<A
>::findAtomByAddressOrNullIfStub(pint_t addr
)
2291 if ( hasStubsSection() && (_stubsMachOSection
->addr() <= addr
) && (addr
< (_stubsMachOSection
->addr()+_stubsMachOSection
->size())) )
2293 return findAtomByAddress(addr
);
2296 template <typename A
>
2297 Atom
<A
>* Parser
<A
>::findAtomByAddressOrLocalTargetOfStub(pint_t addr
, uint32_t* offsetInAtom
)
2299 if ( hasStubsSection() && (_stubsMachOSection
->addr() <= addr
) && (addr
< (_stubsMachOSection
->addr()+_stubsMachOSection
->size())) ) {
2300 // target is a stub, remove indirection
2301 uint32_t symbolIndex
= this->symbolIndexFromIndirectSectionAddress(addr
, _stubsMachOSection
);
2302 assert(symbolIndex
!= INDIRECT_SYMBOL_LOCAL
);
2303 const macho_nlist
<P
>& sym
= this->symbolFromIndex(symbolIndex
);
2304 // can't be to external weak symbol
2305 assert( (this->combineFromSymbol(sym
) != ld::Atom::combineByName
) || (this->scopeFromSymbol(sym
) != ld::Atom::scopeGlobal
) );
2307 return this->findAtomByName(this->nameFromSymbol(sym
));
2309 Atom
<A
>* target
= this->findAtomByAddress(addr
);
2310 *offsetInAtom
= addr
- target
->_objAddress
;
2314 template <typename A
>
2315 Atom
<A
>* Parser
<A
>::findAtomByName(const char* name
)
2317 uint8_t* p
= _file
->_atomsArray
;
2318 for(int i
=_file
->_atomsArrayCount
; i
> 0; --i
) {
2319 Atom
<A
>* atom
= (Atom
<A
>*)p
;
2320 if ( strcmp(name
, atom
->name()) == 0 )
2322 p
+= sizeof(Atom
<A
>);
2327 template <typename A
>
2328 void Parser
<A
>::findTargetFromAddress(pint_t addr
, TargetDesc
& target
)
2330 if ( hasStubsSection() && (_stubsMachOSection
->addr() <= addr
) && (addr
< (_stubsMachOSection
->addr()+_stubsMachOSection
->size())) ) {
2331 // target is a stub, remove indirection
2332 uint32_t symbolIndex
= this->symbolIndexFromIndirectSectionAddress(addr
, _stubsMachOSection
);
2333 assert(symbolIndex
!= INDIRECT_SYMBOL_LOCAL
);
2334 const macho_nlist
<P
>& sym
= this->symbolFromIndex(symbolIndex
);
2336 target
.name
= this->nameFromSymbol(sym
);
2337 target
.weakImport
= this->weakImportFromSymbol(sym
);
2341 Section
<A
>* section
= this->sectionForAddress(addr
);
2342 target
.atom
= section
->findAtomByAddress(addr
);
2343 target
.addend
= addr
- target
.atom
->_objAddress
;
2344 target
.weakImport
= false;
2348 template <typename A
>
2349 void Parser
<A
>::findTargetFromAddress(pint_t baseAddr
, pint_t addr
, TargetDesc
& target
)
2351 findTargetFromAddress(baseAddr
, target
);
2352 target
.addend
= addr
- target
.atom
->_objAddress
;
2355 template <typename A
>
2356 void Parser
<A
>::findTargetFromAddressAndSectionNum(pint_t addr
, unsigned int sectNum
, TargetDesc
& target
)
2358 if ( sectNum
== R_ABS
) {
2359 // target is absolute symbol that corresponds to addr
2360 if ( _absoluteSection
!= NULL
) {
2361 target
.atom
= _absoluteSection
->findAbsAtomForValue(addr
);
2362 if ( target
.atom
!= NULL
) {
2364 target
.weakImport
= false;
2369 throwf("R_ABS reloc but no absolute symbol at target address");
2372 if ( hasStubsSection() && (stubsSectionNum() == sectNum
) ) {
2373 // target is a stub, remove indirection
2374 uint32_t symbolIndex
= this->symbolIndexFromIndirectSectionAddress(addr
, _stubsMachOSection
);
2375 assert(symbolIndex
!= INDIRECT_SYMBOL_LOCAL
);
2376 const macho_nlist
<P
>& sym
= this->symbolFromIndex(symbolIndex
);
2377 // use direct reference when stub is to a static function
2378 if ( ((sym
.n_type() & N_TYPE
) == N_SECT
) && (((sym
.n_type() & N_EXT
) == 0) || (this->nameFromSymbol(sym
)[0] == 'L')) ) {
2379 this->findTargetFromAddressAndSectionNum(sym
.n_value(), sym
.n_sect(), target
);
2383 target
.name
= this->nameFromSymbol(sym
);
2384 target
.weakImport
= this->weakImportFromSymbol(sym
);
2389 Section
<A
>* section
= this->sectionForNum(sectNum
);
2390 target
.atom
= section
->findAtomByAddress(addr
);
2391 if ( target
.atom
== NULL
) {
2392 typedef typename
A::P::sint_t sint_t
;
2393 sint_t a
= (sint_t
)addr
;
2394 sint_t sectStart
= (sint_t
)(section
->machoSection()->addr());
2395 sint_t sectEnd
= sectStart
+ section
->machoSection()->size();
2396 if ( a
< sectStart
) {
2397 // target address is before start of section, so must be negative addend
2398 target
.atom
= section
->findAtomByAddress(sectStart
);
2399 target
.addend
= a
- sectStart
;
2400 target
.weakImport
= false;
2404 else if ( a
>= sectEnd
) {
2405 target
.atom
= section
->findAtomByAddress(sectEnd
-1);
2406 target
.addend
= a
- sectEnd
;
2407 target
.weakImport
= false;
2412 assert(target
.atom
!= NULL
);
2413 target
.addend
= addr
- target
.atom
->_objAddress
;
2414 target
.weakImport
= false;
2418 template <typename A
>
2419 void Parser
<A
>::addDtraceExtraInfos(const SourceLocation
& src
, const char* providerName
)
2421 // for every ___dtrace_stability$* and ___dtrace_typedefs$* undefine with
2422 // a matching provider name, add a by-name kDtraceTypeReference at probe site
2423 const char* dollar
= strchr(providerName
, '$');
2424 if ( dollar
!= NULL
) {
2425 int providerNameLen
= dollar
-providerName
+1;
2426 for ( std::vector
<const char*>::iterator it
= _dtraceProviderInfo
.begin(); it
!= _dtraceProviderInfo
.end(); ++it
) {
2427 const char* typeDollar
= strchr(*it
, '$');
2428 if ( typeDollar
!= NULL
) {
2429 if ( strncmp(typeDollar
+1, providerName
, providerNameLen
) == 0 ) {
2430 addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindDtraceExtra
,false, *it
);
2437 template <typename A
>
2438 const char* Parser
<A
>::scanSymbolTableForAddress(uint64_t addr
)
2440 uint64_t closestSymAddr
= 0;
2441 const char* closestSymName
= NULL
;
2442 for (uint32_t i
=0; i
< this->_symbolCount
; ++i
) {
2443 const macho_nlist
<P
>& sym
= symbolFromIndex(i
);
2445 if ( (sym
.n_type() & N_STAB
) != 0 )
2448 // only look at definitions
2449 if ( (sym
.n_type() & N_TYPE
) != N_SECT
)
2452 // return with exact match
2453 if ( sym
.n_value() == addr
)
2454 return nameFromSymbol(sym
);
2456 // record closest seen so far
2457 if ( (sym
.n_value() < addr
) && ((sym
.n_value() > closestSymAddr
) || (closestSymName
== NULL
)) )
2458 closestSymName
= nameFromSymbol(sym
);
2461 return (closestSymName
!= NULL
) ? closestSymName
: "unknown";
2465 template <typename A
>
2466 void Parser
<A
>::addFixups(const SourceLocation
& src
, ld::Fixup::Kind setKind
, const TargetDesc
& target
)
2468 // some fixup pairs can be combined
2469 ld::Fixup::Cluster cl
= ld::Fixup::k1of3
;
2470 ld::Fixup::Kind firstKind
= ld::Fixup::kindSetTargetAddress
;
2471 bool combined
= false;
2472 if ( target
.addend
== 0 ) {
2473 cl
= ld::Fixup::k1of1
;
2475 switch ( setKind
) {
2476 case ld::Fixup::kindStoreLittleEndian32
:
2477 firstKind
= ld::Fixup::kindStoreTargetAddressLittleEndian32
;
2479 case ld::Fixup::kindStoreLittleEndian64
:
2480 firstKind
= ld::Fixup::kindStoreTargetAddressLittleEndian64
;
2482 case ld::Fixup::kindStoreBigEndian32
:
2483 firstKind
= ld::Fixup::kindStoreTargetAddressBigEndian32
;
2485 case ld::Fixup::kindStoreBigEndian64
:
2486 firstKind
= ld::Fixup::kindStoreTargetAddressBigEndian64
;
2488 case ld::Fixup::kindStoreX86BranchPCRel32
:
2489 firstKind
= ld::Fixup::kindStoreTargetAddressX86BranchPCRel32
;
2491 case ld::Fixup::kindStoreX86PCRel32
:
2492 firstKind
= ld::Fixup::kindStoreTargetAddressX86PCRel32
;
2494 case ld::Fixup::kindStoreX86PCRel32GOTLoad
:
2495 firstKind
= ld::Fixup::kindStoreTargetAddressX86PCRel32GOTLoad
;
2497 case ld::Fixup::kindStoreX86PCRel32TLVLoad
:
2498 firstKind
= ld::Fixup::kindStoreTargetAddressX86PCRel32TLVLoad
;
2500 case ld::Fixup::kindStoreX86Abs32TLVLoad
:
2501 firstKind
= ld::Fixup::kindStoreTargetAddressX86Abs32TLVLoad
;
2503 case ld::Fixup::kindStoreARMBranch24
:
2504 firstKind
= ld::Fixup::kindStoreTargetAddressARMBranch24
;
2506 case ld::Fixup::kindStoreThumbBranch22
:
2507 firstKind
= ld::Fixup::kindStoreTargetAddressThumbBranch22
;
2511 cl
= ld::Fixup::k1of2
;
2516 if ( target
.atom
!= NULL
) {
2517 if ( target
.atom
->scope() == ld::Atom::scopeTranslationUnit
) {
2518 addFixup(src
, cl
, firstKind
, target
.atom
);
2520 else if ( (target
.atom
->combine() == ld::Atom::combineByNameAndContent
) || (target
.atom
->combine() == ld::Atom::combineByNameAndReferences
) ) {
2521 addFixup(src
, cl
, firstKind
, ld::Fixup::bindingByContentBound
, target
.atom
);
2523 else if ( (src
.atom
->section().type() == ld::Section::typeCFString
) && (src
.offsetInAtom
!= 0) ) {
2524 // backing string in CFStrings should always be direct
2525 addFixup(src
, cl
, firstKind
, target
.atom
);
2528 // change direct fixup to by-name fixup
2529 addFixup(src
, cl
, firstKind
, false, target
.atom
->name());
2533 addFixup(src
, cl
, firstKind
, target
.weakImport
, target
.name
);
2535 if ( target
.addend
== 0 ) {
2537 addFixup(src
, ld::Fixup::k2of2
, setKind
);
2540 addFixup(src
, ld::Fixup::k2of3
, ld::Fixup::kindAddAddend
, target
.addend
);
2541 addFixup(src
, ld::Fixup::k3of3
, setKind
);
2545 template <typename A
>
2546 void Parser
<A
>::addFixups(const SourceLocation
& src
, ld::Fixup::Kind kind
, const TargetDesc
& target
, const TargetDesc
& picBase
)
2548 ld::Fixup::Cluster cl
= (target
.addend
== 0) ? ld::Fixup::k1of4
: ld::Fixup::k1of5
;
2549 if ( target
.atom
!= NULL
) {
2550 if ( target
.atom
->scope() == ld::Atom::scopeTranslationUnit
) {
2551 addFixup(src
, cl
, ld::Fixup::kindSetTargetAddress
, target
.atom
);
2553 else if ( (target
.atom
->combine() == ld::Atom::combineByNameAndContent
) || (target
.atom
->combine() == ld::Atom::combineByNameAndReferences
) ) {
2554 addFixup(src
, cl
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, target
.atom
);
2557 addFixup(src
, cl
, ld::Fixup::kindSetTargetAddress
, false, target
.atom
->name());
2561 addFixup(src
, cl
, ld::Fixup::kindSetTargetAddress
, target
.weakImport
, target
.name
);
2563 if ( target
.addend
== 0 ) {
2564 assert(picBase
.atom
!= NULL
);
2565 addFixup(src
, ld::Fixup::k2of4
, ld::Fixup::kindSubtractTargetAddress
, picBase
.atom
);
2566 addFixup(src
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractAddend
, picBase
.addend
);
2567 addFixup(src
, ld::Fixup::k4of4
, kind
);
2570 addFixup(src
, ld::Fixup::k2of5
, ld::Fixup::kindAddAddend
, target
.addend
);
2571 addFixup(src
, ld::Fixup::k3of5
, ld::Fixup::kindSubtractTargetAddress
, picBase
.atom
);
2572 addFixup(src
, ld::Fixup::k4of5
, ld::Fixup::kindSubtractAddend
, picBase
.addend
);
2573 addFixup(src
, ld::Fixup::k5of5
, kind
);
2579 template <typename A
>
2580 uint32_t TentativeDefinitionSection
<A
>::computeAtomCount(class Parser
<A
>& parser
,
2581 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
2582 const struct Parser
<A
>::CFI_CU_InfoArrays
&)
2584 return parser
.tentativeDefinitionCount();
2587 template <typename A
>
2588 uint32_t TentativeDefinitionSection
<A
>::appendAtoms(class Parser
<A
>& parser
, uint8_t* p
,
2589 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
2590 const struct Parser
<A
>::CFI_CU_InfoArrays
&)
2592 this->_beginAtoms
= (Atom
<A
>*)p
;
2594 for (uint32_t i
=parser
.undefinedStartIndex(); i
< parser
.undefinedEndIndex(); ++i
) {
2595 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(i
);
2596 if ( ((sym
.n_type() & N_TYPE
) == N_UNDF
) && (sym
.n_value() != 0) ) {
2597 uint64_t size
= sym
.n_value();
2598 uint8_t alignP2
= GET_COMM_ALIGN(sym
.n_desc());
2599 if ( alignP2
== 0 ) {
2600 // common symbols align to their size
2601 // that is, a 4-byte common aligns to 4-bytes
2602 // if this size is not a power of two,
2603 // then round up to the next power of two
2604 alignP2
= 63 - (uint8_t)__builtin_clzll(size
);
2605 if ( size
!= (1ULL << alignP2
) )
2608 // limit alignment of extremely large commons to 2^15 bytes (8-page)
2611 Atom
<A
>* allocatedSpace
= (Atom
<A
>*)p
;
2612 new (allocatedSpace
) Atom
<A
>(*this, parser
.nameFromSymbol(sym
), (pint_t
)ULLONG_MAX
, size
,
2613 ld::Atom::definitionTentative
, ld::Atom::combineByName
,
2614 parser
.scopeFromSymbol(sym
), ld::Atom::typeZeroFill
, ld::Atom::symbolTableIn
,
2615 parser
.dontDeadStripFromSymbol(sym
), false, false, ld::Atom::Alignment(alignP2
) );
2616 p
+= sizeof(Atom
<A
>);
2620 this->_endAtoms
= (Atom
<A
>*)p
;
2625 template <typename A
>
2626 uint32_t AbsoluteSymbolSection
<A
>::computeAtomCount(class Parser
<A
>& parser
,
2627 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
2628 const struct Parser
<A
>::CFI_CU_InfoArrays
&)
2630 return parser
.absoluteSymbolCount();
2633 template <typename A
>
2634 uint32_t AbsoluteSymbolSection
<A
>::appendAtoms(class Parser
<A
>& parser
, uint8_t* p
,
2635 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
2636 const struct Parser
<A
>::CFI_CU_InfoArrays
&)
2638 this->_beginAtoms
= (Atom
<A
>*)p
;
2640 for (uint32_t i
=0; i
< parser
.symbolCount(); ++i
) {
2641 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(i
);
2642 if ( (sym
.n_type() & N_TYPE
) != N_ABS
)
2644 const char* absName
= parser
.nameFromSymbol(sym
);
2645 // ignore .objc_class_name_* symbols
2646 if ( strncmp(absName
, ".objc_class_name_", 17) == 0 )
2648 // ignore .objc_class_name_* symbols
2649 if ( strncmp(absName
, ".objc_category_name_", 20) == 0 )
2651 // ignore empty *.eh symbols
2652 if ( strcmp(&absName
[strlen(absName
)-3], ".eh") == 0 )
2655 Atom
<A
>* allocatedSpace
= (Atom
<A
>*)p
;
2656 new (allocatedSpace
) Atom
<A
>(*this, parser
, sym
, 0);
2657 p
+= sizeof(Atom
<A
>);
2660 this->_endAtoms
= (Atom
<A
>*)p
;
2664 template <typename A
>
2665 Atom
<A
>* AbsoluteSymbolSection
<A
>::findAbsAtomForValue(typename
A::P::uint_t value
)
2667 Atom
<A
>* end
= this->_endAtoms
;
2668 for(Atom
<A
>* p
= this->_beginAtoms
; p
< end
; ++p
) {
2669 if ( p
->_objAddress
== value
)
2676 template <typename A
>
2677 uint32_t Parser
<A
>::indirectSymbol(uint32_t indirectIndex
)
2679 if ( indirectIndex
>= _indirectTableCount
)
2680 throw "indirect symbol index out of range";
2681 return E::get32(_indirectTable
[indirectIndex
]);
2684 template <typename A
>
2685 const macho_nlist
<typename
A::P
>& Parser
<A
>::symbolFromIndex(uint32_t index
)
2687 if ( index
> _symbolCount
)
2688 throw "symbol index out of range";
2689 return _symbols
[index
];
2692 template <typename A
>
2693 const macho_section
<typename
A::P
>* Parser
<A
>::machOSectionFromSectionIndex(uint32_t index
)
2695 if ( index
>= _machOSectionsCount
)
2696 throw "section index out of range";
2697 return &_sectionsStart
[index
];
2700 template <typename A
>
2701 uint32_t Parser
<A
>::symbolIndexFromIndirectSectionAddress(pint_t addr
, const macho_section
<P
>* sect
)
2703 uint32_t elementSize
= 0;
2704 switch ( sect
->flags() & SECTION_TYPE
) {
2705 case S_SYMBOL_STUBS
:
2706 elementSize
= sect
->reserved2();
2708 case S_LAZY_SYMBOL_POINTERS
:
2709 case S_NON_LAZY_SYMBOL_POINTERS
:
2710 elementSize
= sizeof(pint_t
);
2713 throw "section does not use inirect symbol table";
2715 uint32_t indexInSection
= (addr
- sect
->addr()) / elementSize
;
2716 uint32_t indexIntoIndirectTable
= sect
->reserved1() + indexInSection
;
2717 return this->indirectSymbol(indexIntoIndirectTable
);
2722 template <typename A
>
2723 const char* Parser
<A
>::nameFromSymbol(const macho_nlist
<P
>& sym
)
2725 return &_strings
[sym
.n_strx()];
2728 template <typename A
>
2729 ld::Atom::Scope Parser
<A
>::scopeFromSymbol(const macho_nlist
<P
>& sym
)
2731 if ( (sym
.n_type() & N_EXT
) == 0 )
2732 return ld::Atom::scopeTranslationUnit
;
2733 else if ( (sym
.n_type() & N_PEXT
) != 0 )
2734 return ld::Atom::scopeLinkageUnit
;
2735 else if ( this->nameFromSymbol(sym
)[0] == 'l' ) // since all 'l' symbols will be remove, don't make them global
2736 return ld::Atom::scopeLinkageUnit
;
2738 return ld::Atom::scopeGlobal
;
2741 template <typename A
>
2742 ld::Atom::Definition Parser
<A
>::definitionFromSymbol(const macho_nlist
<P
>& sym
)
2744 switch ( sym
.n_type() & N_TYPE
) {
2746 return ld::Atom::definitionAbsolute
;
2748 return ld::Atom::definitionRegular
;
2750 if ( sym
.n_value() != 0 )
2751 return ld::Atom::definitionTentative
;
2753 throw "definitionFromSymbol() bad symbol";
2756 template <typename A
>
2757 ld::Atom::Combine Parser
<A
>::combineFromSymbol(const macho_nlist
<P
>& sym
)
2759 if ( sym
.n_desc() & N_WEAK_DEF
)
2760 return ld::Atom::combineByName
;
2762 return ld::Atom::combineNever
;
2766 template <typename A
>
2767 ld::Atom::SymbolTableInclusion Parser
<A
>::inclusionFromSymbol(const macho_nlist
<P
>& sym
)
2769 const char* symbolName
= nameFromSymbol(sym
);
2770 // labels beginning with 'l' (lowercase ell) are automatically removed in final linked images <rdar://problem/4571042>
2771 // labels beginning with 'L' should have been stripped by the assembler, so are stripped now
2772 if ( sym
.n_desc() & REFERENCED_DYNAMICALLY
)
2773 return ld::Atom::symbolTableInAndNeverStrip
;
2774 else if ( symbolName
[0] == 'l' )
2775 return ld::Atom::symbolTableNotInFinalLinkedImages
;
2776 else if ( symbolName
[0] == 'L' )
2777 return ld::Atom::symbolTableNotIn
;
2779 return ld::Atom::symbolTableIn
;
2782 template <typename A
>
2783 bool Parser
<A
>::dontDeadStripFromSymbol(const macho_nlist
<P
>& sym
)
2785 return ( (sym
.n_desc() & (N_NO_DEAD_STRIP
|REFERENCED_DYNAMICALLY
)) != 0 );
2788 template <typename A
>
2789 bool Parser
<A
>::isThumbFromSymbol(const macho_nlist
<P
>& sym
)
2791 return ( sym
.n_desc() & N_ARM_THUMB_DEF
);
2794 template <typename A
>
2795 bool Parser
<A
>::weakImportFromSymbol(const macho_nlist
<P
>& sym
)
2797 return ( ((sym
.n_type() & N_TYPE
) == N_UNDF
) && ((sym
.n_desc() & N_WEAK_REF
) != 0) );
2800 template <typename A
>
2801 bool Parser
<A
>::resolverFromSymbol(const macho_nlist
<P
>& sym
)
2803 return ( sym
.n_desc() & N_SYMBOL_RESOLVER
);
2807 /* Skip over a LEB128 value (signed or unsigned). */
2809 skip_leb128 (const uint8_t ** offset
, const uint8_t * end
)
2811 while (*offset
!= end
&& **offset
>= 0x80)
2817 /* Read a ULEB128 into a 64-bit word. Return (uint64_t)-1 on overflow
2818 or error. On overflow, skip past the rest of the uleb128. */
2820 read_uleb128 (const uint8_t ** offset
, const uint8_t * end
)
2822 uint64_t result
= 0;
2829 return (uint64_t) -1;
2831 b
= **offset
& 0x7f;
2833 if (bit
>= 64 || b
<< bit
>> bit
!= b
)
2834 result
= (uint64_t) -1;
2836 result
|= b
<< bit
, bit
+= 7;
2837 } while (*(*offset
)++ >= 0x80);
2842 /* Skip over a DWARF attribute of form FORM. */
2843 template <typename A
>
2844 bool Parser
<A
>::skip_form(const uint8_t ** offset
, const uint8_t * end
, uint64_t form
,
2845 uint8_t addr_size
, bool dwarf64
)
2855 case DW_FORM_block2
:
2856 if (end
- *offset
< 2)
2858 sz
= 2 + A::P::E::get16(*(uint16_t*)offset
);
2861 case DW_FORM_block4
:
2862 if (end
- *offset
< 4)
2864 sz
= 2 + A::P::E::get32(*(uint32_t*)offset
);
2882 case DW_FORM_string
:
2883 while (*offset
!= end
&& **offset
)
2892 sz
= read_uleb128 (offset
, end
);
2895 case DW_FORM_block1
:
2903 case DW_FORM_ref_udata
:
2904 skip_leb128 (offset
, end
);
2908 case DW_FORM_ref_addr
:
2915 if (end
- *offset
< sz
)
2922 template <typename A
>
2923 const char* Parser
<A
>::getDwarfString(uint64_t form
, const uint8_t* p
)
2925 if ( form
== DW_FORM_string
)
2926 return (const char*)p
;
2927 else if ( form
== DW_FORM_strp
) {
2928 uint32_t offset
= E::get32(*((uint32_t*)p
));
2929 const char* dwarfStrings
= (char*)_file
->fileContent() + _file
->_dwarfDebugStringSect
->offset();
2930 if ( offset
> _file
->_dwarfDebugStringSect
->size() ) {
2931 warning("unknown dwarf DW_FORM_strp (offset=0x%08X) is too big in %s\n", offset
, this->_path
);
2934 return &dwarfStrings
[offset
];
2936 warning("unknown dwarf string encoding (form=%lld) in %s\n", form
, this->_path
);
2941 template <typename A
>
2942 struct AtomAndLineInfo
{
2944 ld::Atom::LineInfo info
;
2948 // <rdar://problem/5591394> Add support to ld64 for N_FUN stabs when used for symbolic constants
2949 // Returns whether a stabStr belonging to an N_FUN stab represents a
2950 // symbolic constant rather than a function
2951 template <typename A
>
2952 bool Parser
<A
>::isConstFunStabs(const char *stabStr
)
2955 // N_FUN can be used for both constants and for functions. In case it's a constant,
2956 // the format of the stabs string is "symname:c=<value>;"
2957 // ':' cannot appear in the symbol name, except if it's an Objective-C method
2958 // (in which case the symbol name starts with + or -, and then it's definitely
2960 return (stabStr
!= NULL
) && (stabStr
[0] != '+') && (stabStr
[0] != '-')
2961 && ((colon
= strchr(stabStr
, ':')) != NULL
)
2962 && (colon
[1] == 'c') && (colon
[2] == '=');
2966 template <typename A
>
2967 void Parser
<A
>::parseDebugInfo()
2969 // check for dwarf __debug_info section
2970 if ( _file
->_dwarfDebugInfoSect
== NULL
) {
2971 // if no DWARF debug info, look for stabs
2975 if ( _file
->_dwarfDebugInfoSect
->size() == 0 )
2981 if ( !read_comp_unit(&tuName
, &tuDir
, &stmtList
) ) {
2982 // if can't parse dwarf, warn and give up
2983 _file
->_dwarfTranslationUnitPath
= NULL
;
2984 warning("can't parse dwarf compilation unit info in %s", _path
);
2985 _file
->_debugInfoKind
= ld::relocatable::File::kDebugInfoNone
;
2988 if ( (tuName
!= NULL
) && (tuName
[1] == '/') ) {
2989 _file
->_dwarfTranslationUnitPath
= tuName
;
2991 else if ( (tuDir
!= NULL
) && (tuName
!= NULL
) ) {
2992 asprintf((char**)&(_file
->_dwarfTranslationUnitPath
), "%s/%s", tuDir
, tuName
);
2994 else if ( tuDir
== NULL
) {
2995 _file
->_dwarfTranslationUnitPath
= tuName
;
2998 _file
->_dwarfTranslationUnitPath
= NULL
;
3001 // add line number info to atoms from dwarf
3002 std::vector
<AtomAndLineInfo
<A
> > entries
;
3003 entries
.reserve(64);
3004 if ( _file
->_debugInfoKind
== ld::relocatable::File::kDebugInfoDwarf
) {
3005 // file with just data will have no __debug_line info
3006 if ( (_file
->_dwarfDebugLineSect
!= NULL
) && (_file
->_dwarfDebugLineSect
->size() != 0) ) {
3007 // validate stmt_list
3008 if ( (stmtList
!= (uint64_t)-1) && (stmtList
< _file
->_dwarfDebugLineSect
->size()) ) {
3009 const uint8_t* debug_line
= (uint8_t*)_file
->fileContent() + _file
->_dwarfDebugLineSect
->offset();
3010 struct line_reader_data
* lines
= line_open(&debug_line
[stmtList
],
3011 _file
->_dwarfDebugLineSect
->size() - stmtList
, E::little_endian
);
3012 struct line_info result
;
3013 Atom
<A
>* curAtom
= NULL
;
3014 uint32_t curAtomOffset
= 0;
3015 uint32_t curAtomAddress
= 0;
3016 uint32_t curAtomSize
= 0;
3017 std::map
<uint32_t,const char*> dwarfIndexToFile
;
3018 if ( lines
!= NULL
) {
3019 while ( line_next(lines
, &result
, line_stop_pc
) ) {
3020 //fprintf(stderr, "curAtom=%p, result.pc=0x%llX, result.line=%llu, result.end_of_sequence=%d,"
3021 // " curAtomAddress=0x%X, curAtomSize=0x%X\n",
3022 // curAtom, result.pc, result.line, result.end_of_sequence, curAtomAddress, curAtomSize);
3023 // work around weird debug line table compiler generates if no functions in __text section
3024 if ( (curAtom
== NULL
) && (result
.pc
== 0) && result
.end_of_sequence
&& (result
.file
== 1))
3026 // for performance, see if in next pc is in current atom
3027 if ( (curAtom
!= NULL
) && (curAtomAddress
<= result
.pc
) && (result
.pc
< (curAtomAddress
+curAtomSize
)) ) {
3028 curAtomOffset
= result
.pc
- curAtomAddress
;
3030 // or pc at end of current atom
3031 else if ( result
.end_of_sequence
&& (curAtom
!= NULL
) && (result
.pc
== (curAtomAddress
+curAtomSize
)) ) {
3032 curAtomOffset
= result
.pc
- curAtomAddress
;
3034 // or only one function that is a one line function
3035 else if ( result
.end_of_sequence
&& (curAtom
== NULL
) && (this->findAtomByAddress(0) != NULL
) && (result
.pc
== this->findAtomByAddress(0)->size()) ) {
3036 curAtom
= this->findAtomByAddress(0);
3037 curAtomOffset
= result
.pc
- curAtom
->objectAddress();
3038 curAtomAddress
= curAtom
->objectAddress();
3039 curAtomSize
= curAtom
->size();
3042 // do slow look up of atom by address
3044 curAtom
= this->findAtomByAddress(result
.pc
);
3047 // in case of bug in debug info, don't abort link, just limp on
3050 if ( curAtom
== NULL
)
3051 break; // file has line info but no functions
3052 if ( result
.end_of_sequence
&& (curAtomAddress
+curAtomSize
< result
.pc
) ) {
3053 // a one line function can be returned by line_next() as one entry with pc at end of blob
3054 // look for alt atom starting at end of previous atom
3055 uint32_t previousEnd
= curAtomAddress
+curAtomSize
;
3056 Atom
<A
>* alt
= this->findAtomByAddressOrNullIfStub(previousEnd
);
3058 continue; // ignore spurious debug info for stubs
3059 if ( result
.pc
<= alt
->objectAddress() + alt
->size() ) {
3061 curAtomOffset
= result
.pc
- alt
->objectAddress();
3062 curAtomAddress
= alt
->objectAddress();
3063 curAtomSize
= alt
->size();
3066 curAtomOffset
= result
.pc
- curAtom
->objectAddress();
3067 curAtomAddress
= curAtom
->objectAddress();
3068 curAtomSize
= curAtom
->size();
3072 curAtomOffset
= result
.pc
- curAtom
->objectAddress();
3073 curAtomAddress
= curAtom
->objectAddress();
3074 curAtomSize
= curAtom
->size();
3077 const char* filename
;
3078 std::map
<uint32_t,const char*>::iterator pos
= dwarfIndexToFile
.find(result
.file
);
3079 if ( pos
== dwarfIndexToFile
.end() ) {
3080 filename
= line_file(lines
, result
.file
);
3081 dwarfIndexToFile
[result
.file
] = filename
;
3084 filename
= pos
->second
;
3086 // only record for ~8000 line info records per function
3087 if ( curAtom
->roomForMoreLineInfoCount() ) {
3088 AtomAndLineInfo
<A
> entry
;
3089 entry
.atom
= curAtom
;
3090 entry
.info
.atomOffset
= curAtomOffset
;
3091 entry
.info
.fileName
= filename
;
3092 entry
.info
.lineNumber
= result
.line
;
3093 //fprintf(stderr, "addr=0x%08llX, line=%lld, file=%s, atom=%s, atom.size=0x%X, end=%d\n",
3094 // result.pc, result.line, filename, curAtom->name(), curAtomSize, result.end_of_sequence);
3095 entries
.push_back(entry
);
3096 curAtom
->incrementLineInfoCount();
3098 if ( result
.end_of_sequence
) {
3108 // assign line info start offset for each atom
3109 uint8_t* p
= _file
->_atomsArray
;
3110 uint32_t liOffset
= 0;
3111 for(int i
=_file
->_atomsArrayCount
; i
> 0; --i
) {
3112 Atom
<A
>* atom
= (Atom
<A
>*)p
;
3113 atom
->_lineInfoStartIndex
= liOffset
;
3114 liOffset
+= atom
->_lineInfoCount
;
3115 atom
->_lineInfoCount
= 0;
3116 p
+= sizeof(Atom
<A
>);
3118 assert(liOffset
== entries
.size());
3119 _file
->_lineInfos
.reserve(liOffset
);
3121 // copy each line info for each atom
3122 for (typename
std::vector
<AtomAndLineInfo
<A
> >::iterator it
= entries
.begin(); it
!= entries
.end(); ++it
) {
3123 uint32_t slot
= it
->atom
->_lineInfoStartIndex
+ it
->atom
->_lineInfoCount
;
3124 _file
->_lineInfos
[slot
] = it
->info
;
3125 it
->atom
->_lineInfoCount
++;
3128 // done with temp vector
3132 template <typename A
>
3133 void Parser
<A
>::parseStabs()
3135 // scan symbol table for stabs entries
3136 Atom
<A
>* currentAtom
= NULL
;
3137 pint_t currentAtomAddress
= 0;
3138 enum { start
, inBeginEnd
, inFun
} state
= start
;
3139 for (uint32_t symbolIndex
= 0; symbolIndex
< _symbolCount
; ++symbolIndex
) {
3140 const macho_nlist
<P
>& sym
= this->symbolFromIndex(symbolIndex
);
3141 bool useStab
= true;
3142 uint8_t type
= sym
.n_type();
3143 const char* symString
= (sym
.n_strx() != 0) ? this->nameFromSymbol(sym
) : NULL
;
3144 if ( (type
& N_STAB
) != 0 ) {
3145 _file
->_debugInfoKind
= (_hasUUID
? ld::relocatable::File::kDebugInfoStabsUUID
: ld::relocatable::File::kDebugInfoStabs
);
3146 ld::relocatable::File::Stab stab
;
3149 stab
.other
= sym
.n_sect();
3150 stab
.desc
= sym
.n_desc();
3151 stab
.value
= sym
.n_value();
3157 // beginning of function block
3159 // fall into case to lookup atom by addresss
3162 currentAtomAddress
= sym
.n_value();
3163 currentAtom
= this->findAtomByAddress(currentAtomAddress
);
3164 if ( currentAtom
!= NULL
) {
3165 stab
.atom
= currentAtom
;
3166 stab
.string
= symString
;
3169 fprintf(stderr
, "can't find atom for stabs BNSYM at %08llX in %s",
3170 (uint64_t)sym
.n_value(), _path
);
3179 // not associated with an atom, just copy
3180 stab
.string
= symString
;
3184 // n_value field is NOT atom address ;-(
3185 // need to find atom by name match
3186 const char* colon
= strchr(symString
, ':');
3187 if ( colon
!= NULL
) {
3188 // build underscore leading name
3189 int nameLen
= colon
- symString
;
3190 char symName
[nameLen
+2];
3191 strlcpy(&symName
[1], symString
, nameLen
+1);
3193 symName
[nameLen
+1] = '\0';
3194 currentAtom
= this->findAtomByName(symName
);
3195 if ( currentAtom
!= NULL
) {
3196 stab
.atom
= currentAtom
;
3197 stab
.string
= symString
;
3201 // might be a debug-note without trailing :G()
3202 currentAtom
= this->findAtomByName(symString
);
3203 if ( currentAtom
!= NULL
) {
3204 stab
.atom
= currentAtom
;
3205 stab
.string
= symString
;
3208 if ( stab
.atom
== NULL
) {
3209 // ld_classic added bogus GSYM stabs for old style dtrace probes
3210 if ( (strncmp(symString
, "__dtrace_probe$", 15) != 0) )
3211 warning("can't find atom for N_GSYM stabs %s in %s", symString
, _path
);
3217 if ( isConstFunStabs(symString
) ) {
3218 // constant not associated with a function
3219 stab
.string
= symString
;
3222 // old style stabs without BNSYM
3224 currentAtomAddress
= sym
.n_value();
3225 currentAtom
= this->findAtomByAddress(currentAtomAddress
);
3226 if ( currentAtom
!= NULL
) {
3227 stab
.atom
= currentAtom
;
3228 stab
.string
= symString
;
3231 warning("can't find atom for stabs FUN at %08llX in %s",
3232 (uint64_t)currentAtomAddress
, _path
);
3238 stab
.string
= symString
;
3244 stab
.string
= symString
;
3245 // -gfull built .o file
3248 warning("unknown stabs type 0x%X in %s", type
, _path
);
3252 stab
.atom
= currentAtom
;
3261 Atom
<A
>* nestedAtom
= this->findAtomByAddress(sym
.n_value());
3262 if ( nestedAtom
!= NULL
) {
3263 stab
.atom
= nestedAtom
;
3264 stab
.string
= symString
;
3267 warning("can't find atom for stabs 0x%X at %08llX in %s",
3268 type
, (uint64_t)sym
.n_value(), _path
);
3275 // adjust value to be offset in atom
3276 stab
.value
-= currentAtomAddress
;
3278 stab
.string
= symString
;
3285 if ( isConstFunStabs(symString
) ) {
3286 stab
.atom
= currentAtom
;
3287 stab
.string
= symString
;
3290 if ( sym
.n_sect() != 0 ) {
3291 // found another start stab, must be really old stabs...
3292 currentAtomAddress
= sym
.n_value();
3293 currentAtom
= this->findAtomByAddress(currentAtomAddress
);
3294 if ( currentAtom
!= NULL
) {
3295 stab
.atom
= currentAtom
;
3296 stab
.string
= symString
;
3299 warning("can't find atom for stabs FUN at %08llX in %s",
3300 (uint64_t)currentAtomAddress
, _path
);
3304 // found ending stab, switch back to start state
3305 stab
.string
= symString
;
3306 stab
.atom
= currentAtom
;
3315 // adjust value to be offset in atom
3316 stab
.value
-= currentAtomAddress
;
3317 stab
.atom
= currentAtom
;
3320 stab
.string
= symString
;
3324 stab
.atom
= currentAtom
;
3325 stab
.string
= symString
;
3330 // add to list of stabs for this .o file
3332 _file
->_stabs
.push_back(stab
);
3339 // Look at the compilation unit DIE and determine
3340 // its NAME, compilation directory (in COMP_DIR) and its
3341 // line number information offset (in STMT_LIST). NAME and COMP_DIR
3342 // may be NULL (especially COMP_DIR) if they are not in the .o file;
3343 // STMT_LIST will be (uint64_t) -1.
3345 // At present this assumes that there's only one compilation unit DIE.
3347 template <typename A
>
3348 bool Parser
<A
>::read_comp_unit(const char ** name
, const char ** comp_dir
,
3349 uint64_t *stmt_list
)
3351 const uint8_t * debug_info
;
3352 const uint8_t * debug_abbrev
;
3355 const uint8_t * end
;
3356 const uint8_t * enda
;
3359 uint64_t abbrev_base
;
3361 uint8_t address_size
;
3366 *stmt_list
= (uint64_t) -1;
3368 if ( (_file
->_dwarfDebugInfoSect
== NULL
) || (_file
->_dwarfDebugAbbrevSect
== NULL
) )
3371 debug_info
= (uint8_t*)_file
->fileContent() + _file
->_dwarfDebugInfoSect
->offset();
3372 debug_abbrev
= (uint8_t*)_file
->fileContent() + _file
->_dwarfDebugAbbrevSect
->offset();
3375 if (_file
->_dwarfDebugInfoSect
->size() < 12)
3376 /* Too small to be a real debug_info section. */
3378 sz
= A::P::E::get32(*(uint32_t*)di
);
3380 dwarf64
= sz
== 0xffffffff;
3382 sz
= A::P::E::get64(*(uint64_t*)di
), di
+= 8;
3383 else if (sz
> 0xffffff00)
3384 /* Unknown dwarf format. */
3387 /* Verify claimed size. */
3388 if (sz
+ (di
- debug_info
) > _file
->_dwarfDebugInfoSect
->size() || sz
<= (dwarf64
? 23 : 11))
3391 vers
= A::P::E::get16(*(uint16_t*)di
);
3392 if (vers
< 2 || vers
> 3)
3393 /* DWARF version wrong for this code.
3394 Chances are we could continue anyway, but we don't know for sure. */
3398 /* Find the debug_abbrev section. */
3399 abbrev_base
= dwarf64
? A::P::E::get64(*(uint64_t*)di
) : A::P::E::get32(*(uint32_t*)di
);
3400 di
+= dwarf64
? 8 : 4;
3402 if (abbrev_base
> _file
->_dwarfDebugAbbrevSect
->size())
3404 da
= debug_abbrev
+ abbrev_base
;
3405 enda
= debug_abbrev
+ _file
->_dwarfDebugAbbrevSect
->size();
3407 address_size
= *di
++;
3409 /* Find the abbrev number we're looking for. */
3411 abbrev
= read_uleb128 (&di
, end
);
3412 if (abbrev
== (uint64_t) -1)
3415 /* Skip through the debug_abbrev section looking for that abbrev. */
3418 uint64_t this_abbrev
= read_uleb128 (&da
, enda
);
3421 if (this_abbrev
== abbrev
)
3422 /* This is almost always taken. */
3424 skip_leb128 (&da
, enda
); /* Skip the tag. */
3427 da
++; /* Skip the DW_CHILDREN_* value. */
3430 attr
= read_uleb128 (&da
, enda
);
3431 skip_leb128 (&da
, enda
);
3432 } while (attr
!= 0 && attr
!= (uint64_t) -1);
3437 /* Check that the abbrev is one for a DW_TAG_compile_unit. */
3438 if (read_uleb128 (&da
, enda
) != DW_TAG_compile_unit
)
3442 da
++; /* Skip the DW_CHILDREN_* value. */
3444 /* Now, go through the DIE looking for DW_AT_name,
3445 DW_AT_comp_dir, and DW_AT_stmt_list. */
3448 uint64_t attr
= read_uleb128 (&da
, enda
);
3449 uint64_t form
= read_uleb128 (&da
, enda
);
3451 if (attr
== (uint64_t) -1)
3456 if (form
== DW_FORM_indirect
)
3457 form
= read_uleb128 (&di
, end
);
3459 if (attr
== DW_AT_name
)
3460 *name
= getDwarfString(form
, di
);
3461 else if (attr
== DW_AT_comp_dir
)
3462 *comp_dir
= getDwarfString(form
, di
);
3463 else if (attr
== DW_AT_stmt_list
&& form
== DW_FORM_data4
)
3464 *stmt_list
= A::P::E::get32(*(uint32_t*)di
);
3465 else if (attr
== DW_AT_stmt_list
&& form
== DW_FORM_data8
)
3466 *stmt_list
= A::P::E::get64(*(uint64_t*)di
);
3467 if (! skip_form (&di
, end
, form
, address_size
, dwarf64
))
3474 template <typename A
>
3477 free(_sectionsArray
);
3481 template <typename A
>
3482 const char* File
<A
>::translationUnitSource() const
3484 return _dwarfTranslationUnitPath
;
3489 template <typename A
>
3490 bool File
<A
>::forEachAtom(ld::File::AtomHandler
& handler
) const
3492 handler
.doFile(*this);
3493 uint8_t* p
= _atomsArray
;
3494 for(int i
=_atomsArrayCount
; i
> 0; --i
) {
3495 handler
.doAtom(*((Atom
<A
>*)p
));
3496 p
+= sizeof(Atom
<A
>);
3498 return (_atomsArrayCount
!= 0);
3501 template <typename A
>
3502 const char* Section
<A
>::makeSegmentName(const macho_section
<typename
A::P
>* sect
)
3504 // mach-o section record only has room for 16-byte seg/sect names
3505 // so a 16-byte name has no trailing zero
3506 const char* name
= sect
->segname();
3507 if ( strlen(name
) < 16 )
3509 char* tmp
= new char[17];
3510 strlcpy(tmp
, name
, 17);
3514 template <typename A
>
3515 const char* Section
<A
>::makeSectionName(const macho_section
<typename
A::P
>* sect
)
3517 const char* name
= sect
->sectname();
3518 if ( strlen(name
) < 16 )
3521 // special case common long section names so we don't have to malloc
3522 if ( strncmp(sect
->sectname(), "__objc_classrefs", 16) == 0 )
3523 return "__objc_classrefs";
3524 if ( strncmp(sect
->sectname(), "__objc_classlist", 16) == 0 )
3525 return "__objc_classlist";
3526 if ( strncmp(sect
->sectname(), "__objc_nlclslist", 16) == 0 )
3527 return "__objc_nlclslist";
3528 if ( strncmp(sect
->sectname(), "__objc_nlcatlist", 16) == 0 )
3529 return "__objc_nlcatlist";
3530 if ( strncmp(sect
->sectname(), "__objc_protolist", 16) == 0 )
3531 return "__objc_protolist";
3532 if ( strncmp(sect
->sectname(), "__objc_protorefs", 16) == 0 )
3533 return "__objc_protorefs";
3534 if ( strncmp(sect
->sectname(), "__objc_superrefs", 16) == 0 )
3535 return "__objc_superrefs";
3536 if ( strncmp(sect
->sectname(), "__objc_imageinfo", 16) == 0 )
3537 return "__objc_imageinfo";
3538 if ( strncmp(sect
->sectname(), "__objc_stringobj", 16) == 0 )
3539 return "__objc_stringobj";
3540 if ( strncmp(sect
->sectname(), "__gcc_except_tab", 16) == 0 )
3541 return "__gcc_except_tab";
3543 char* tmp
= new char[17];
3544 strlcpy(tmp
, name
, 17);
3548 template <typename A
>
3549 bool Section
<A
>::readable(const macho_section
<typename
A::P
>* sect
)
3554 template <typename A
>
3555 bool Section
<A
>::writable(const macho_section
<typename
A::P
>* sect
)
3557 // mach-o .o files do not contain segment permissions
3558 // we just know TEXT is special
3559 return ( strcmp(sect
->segname(), "__TEXT") != 0 );
3562 template <typename A
>
3563 bool Section
<A
>::exectuable(const macho_section
<typename
A::P
>* sect
)
3565 // mach-o .o files do not contain segment permissions
3566 // we just know TEXT is special
3567 return ( strcmp(sect
->segname(), "__TEXT") == 0 );
3571 template <typename A
>
3572 ld::Section::Type Section
<A
>::sectionType(const macho_section
<typename
A::P
>* sect
)
3574 switch ( sect
->flags() & SECTION_TYPE
) {
3576 return ld::Section::typeZeroFill
;
3577 case S_CSTRING_LITERALS
:
3578 if ( (strcmp(sect
->sectname(), "__cstring") == 0) && (strcmp(sect
->segname(), "__TEXT") == 0) )
3579 return ld::Section::typeCString
;
3581 return ld::Section::typeNonStdCString
;
3582 case S_4BYTE_LITERALS
:
3583 return ld::Section::typeLiteral4
;
3584 case S_8BYTE_LITERALS
:
3585 return ld::Section::typeLiteral8
;
3586 case S_LITERAL_POINTERS
:
3587 return ld::Section::typeCStringPointer
;
3588 case S_NON_LAZY_SYMBOL_POINTERS
:
3589 return ld::Section::typeNonLazyPointer
;
3590 case S_LAZY_SYMBOL_POINTERS
:
3591 return ld::Section::typeLazyPointer
;
3592 case S_SYMBOL_STUBS
:
3593 return ld::Section::typeStub
;
3594 case S_MOD_INIT_FUNC_POINTERS
:
3595 return ld::Section::typeInitializerPointers
;
3596 case S_MOD_TERM_FUNC_POINTERS
:
3597 return ld::Section::typeTerminatorPointers
;
3599 return ld::Section::typeUnclassified
;
3600 case S_16BYTE_LITERALS
:
3601 return ld::Section::typeLiteral16
;
3604 if ( sect
->flags() & S_ATTR_PURE_INSTRUCTIONS
) {
3605 return ld::Section::typeCode
;
3607 else if ( strcmp(sect
->segname(), "__TEXT") == 0 ) {
3608 if ( strcmp(sect
->sectname(), "__eh_frame") == 0 )
3609 return ld::Section::typeCFI
;
3610 else if ( strcmp(sect
->sectname(), "__ustring") == 0 )
3611 return ld::Section::typeUTF16Strings
;
3612 else if ( strcmp(sect
->sectname(), "__textcoal_nt") == 0 )
3613 return ld::Section::typeCode
;
3614 else if ( strcmp(sect
->sectname(), "__StaticInit") == 0 )
3615 return ld::Section::typeCode
;
3616 else if ( strcmp(sect
->sectname(), "__constructor") == 0 )
3617 return ld::Section::typeInitializerPointers
;
3619 else if ( strcmp(sect
->segname(), "__DATA") == 0 ) {
3620 if ( strcmp(sect
->sectname(), "__cfstring") == 0 )
3621 return ld::Section::typeCFString
;
3622 else if ( strcmp(sect
->sectname(), "__dyld") == 0 )
3623 return ld::Section::typeDyldInfo
;
3624 else if ( strcmp(sect
->sectname(), "__program_vars") == 0 )
3625 return ld::Section::typeDyldInfo
;
3626 else if ( strncmp(sect
->sectname(), "__objc_classrefs", 16) == 0 )
3627 return ld::Section::typeObjCClassRefs
;
3628 else if ( strcmp(sect
->sectname(), "__objc_catlist") == 0 )
3629 return ld::Section::typeObjC2CategoryList
;
3631 else if ( strcmp(sect
->segname(), "__OBJC") == 0 ) {
3632 if ( strcmp(sect
->sectname(), "__class") == 0 )
3633 return ld::Section::typeObjC1Classes
;
3636 case S_THREAD_LOCAL_REGULAR
:
3637 return ld::Section::typeTLVInitialValues
;
3638 case S_THREAD_LOCAL_ZEROFILL
:
3639 return ld::Section::typeTLVZeroFill
;
3640 case S_THREAD_LOCAL_VARIABLES
:
3641 return ld::Section::typeTLVDefs
;
3642 case S_THREAD_LOCAL_INIT_FUNCTION_POINTERS
:
3643 return ld::Section::typeTLVInitializerPointers
;
3645 return ld::Section::typeUnclassified
;
3649 template <typename A
>
3650 Atom
<A
>* Section
<A
>::findContentAtomByAddress(pint_t addr
, class Atom
<A
>* start
, class Atom
<A
>* end
)
3652 // do a binary search of atom array
3653 uint32_t atomCount
= end
- start
;
3654 Atom
<A
>* base
= start
;
3655 for (uint32_t n
= atomCount
; n
> 0; n
/= 2) {
3656 Atom
<A
>* pivot
= &base
[n
/2];
3657 pint_t atomStartAddr
= pivot
->_objAddress
;
3658 pint_t atomEndAddr
= atomStartAddr
+ pivot
->_size
;
3659 if ( atomStartAddr
<= addr
) {
3660 // address in normal atom
3661 if (addr
< atomEndAddr
)
3663 // address in "end" label (but not in alias)
3664 if ( (pivot
->_size
== 0) && (addr
== atomEndAddr
) && !pivot
->isAlias() )
3667 if ( addr
>= atomEndAddr
) {
3669 // move base to atom after pivot
3681 template <typename A
>
3682 ld::Atom::Alignment Section
<A
>::alignmentForAddress(pint_t addr
)
3684 const uint32_t sectionAlignment
= this->_machOSection
->align();
3685 return ld::Atom::Alignment(sectionAlignment
, (addr
% (1 << sectionAlignment
)));
3688 template <typename A
>
3689 uint32_t Section
<A
>::sectionNum(class Parser
<A
>& parser
) const
3691 if ( _machOSection
== NULL
)
3694 return 1 + (this->_machOSection
- parser
.firstMachOSection());
3697 // arm does not have zero cost exceptions
3698 template <> uint32_t CFISection
<arm
>::cfiCount() { return 0; }
3700 template <typename A
>
3701 uint32_t CFISection
<A
>::cfiCount()
3703 // create ObjectAddressSpace object for use by libunwind
3704 OAS
oas(*this, (uint8_t*)this->file().fileContent()+this->_machOSection
->offset());
3705 return libunwind::CFI_Parser
<OAS
>::getCFICount(oas
,
3706 this->_machOSection
->addr(), this->_machOSection
->size());
3709 template <typename A
>
3710 void CFISection
<A
>::warnFunc(void* ref
, uint64_t funcAddr
, const char* msg
)
3712 Parser
<A
>* parser
= (Parser
<A
>*)ref
;
3713 if ( ! parser
->convertUnwindInfo() )
3715 if ( funcAddr
!= CFI_INVALID_ADDRESS
) {
3716 // atoms are not constructed yet, so scan symbol table for labels
3717 const char* name
= parser
->scanSymbolTableForAddress(funcAddr
);
3718 warning("could not create compact unwind for %s: %s", name
, msg
);
3721 warning("could not create compact unwind: %s", msg
);
3726 bool CFISection
<x86_64
>::needsRelocating()
3731 template <typename A
>
3732 bool CFISection
<A
>::needsRelocating()
3738 void CFISection
<x86_64
>::cfiParse(class Parser
<x86_64
>& parser
, uint8_t* buffer
,
3739 libunwind::CFI_Atom_Info
<CFISection
<x86_64
>::OAS
>::CFI_Atom_Info cfiArray
[],
3742 // copy __eh_frame data to buffer
3743 memcpy(buffer
, file().fileContent() + this->_machOSection
->offset(), this->_machOSection
->size());
3745 // and apply relocations
3746 const macho_relocation_info
<P
>* relocs
= (macho_relocation_info
<P
>*)(file().fileContent() + this->_machOSection
->reloff());
3747 const macho_relocation_info
<P
>* relocsEnd
= &relocs
[this->_machOSection
->nreloc()];
3748 for (const macho_relocation_info
<P
>* reloc
= relocs
; reloc
< relocsEnd
; ++reloc
) {
3750 switch ( reloc
->r_type() ) {
3751 case X86_64_RELOC_SUBTRACTOR
:
3752 value
= 0 - parser
.symbolFromIndex(reloc
->r_symbolnum()).n_value();
3754 if ( reloc
->r_extern() )
3755 value
+= parser
.symbolFromIndex(reloc
->r_symbolnum()).n_value();
3757 case X86_64_RELOC_UNSIGNED
:
3758 value
= parser
.symbolFromIndex(reloc
->r_symbolnum()).n_value();
3760 case X86_64_RELOC_GOT
:
3761 // this is used for the reference to the personality function in CIEs
3762 // store the symbol number of the personality function for later use as a Fixup
3763 value
= reloc
->r_symbolnum();
3766 fprintf(stderr
, "CFISection::cfiParse() unexpected relocation type at r_address=0x%08X\n", reloc
->r_address());
3771 switch ( reloc
->r_length() ) {
3773 p64
= (uint64_t*)&buffer
[reloc
->r_address()];
3774 E::set64(*p64
, value
+ E::get64(*p64
));
3777 p32
= (uint32_t*)&buffer
[reloc
->r_address()];
3778 E::set32(*p32
, value
+ E::get32(*p32
));
3781 fprintf(stderr
, "CFISection::cfiParse() unexpected relocation size at r_address=0x%08X\n", reloc
->r_address());
3787 // create ObjectAddressSpace object for use by libunwind
3788 OAS
oas(*this, buffer
);
3790 // use libuwind to parse __eh_frame data into array of CFI_Atom_Info
3792 msg
= libunwind::DwarfInstructions
<OAS
, libunwind::Registers_x86_64
>::parseCFIs(
3793 oas
, this->_machOSection
->addr(), this->_machOSection
->size(),
3794 cfiArray
, count
, (void*)&parser
, warnFunc
);
3796 throwf("malformed __eh_frame section: %s", msg
);
3800 void CFISection
<x86
>::cfiParse(class Parser
<x86
>& parser
, uint8_t* buffer
,
3801 libunwind::CFI_Atom_Info
<CFISection
<x86
>::OAS
>::CFI_Atom_Info cfiArray
[],
3804 // create ObjectAddressSpace object for use by libunwind
3805 OAS
oas(*this, (uint8_t*)this->file().fileContent()+this->_machOSection
->offset());
3807 // use libuwind to parse __eh_frame data into array of CFI_Atom_Info
3809 msg
= libunwind::DwarfInstructions
<OAS
, libunwind::Registers_x86
>::parseCFIs(
3810 oas
, this->_machOSection
->addr(), this->_machOSection
->size(),
3811 cfiArray
, count
, (void*)&parser
, warnFunc
);
3813 throwf("malformed __eh_frame section: %s", msg
);
3820 void CFISection
<arm
>::cfiParse(class Parser
<arm
>& parser
, uint8_t* buffer
,
3821 libunwind::CFI_Atom_Info
<CFISection
<arm
>::OAS
>::CFI_Atom_Info cfiArray
[],
3824 // arm does not use zero cost exceptions
3830 template <typename A
>
3831 uint32_t CFISection
<A
>::computeAtomCount(class Parser
<A
>& parser
,
3832 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
3833 const struct Parser
<A
>::CFI_CU_InfoArrays
& cfis
)
3835 return cfis
.cfiCount
;
3840 template <typename A
>
3841 uint32_t CFISection
<A
>::appendAtoms(class Parser
<A
>& parser
, uint8_t* p
,
3842 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
3843 const struct Parser
<A
>::CFI_CU_InfoArrays
& cfis
)
3845 this->_beginAtoms
= (Atom
<A
>*)p
;
3846 // walk CFI_Atom_Info array and create atom for each entry
3847 const CFI_Atom_Info
* start
= &cfis
.cfiArray
[0];
3848 const CFI_Atom_Info
* end
= &cfis
.cfiArray
[cfis
.cfiCount
];
3849 for(const CFI_Atom_Info
* a
=start
; a
< end
; ++a
) {
3850 Atom
<A
>* space
= (Atom
<A
>*)p
;
3851 new (space
) Atom
<A
>(*this, (a
->isCIE
? "CIE" : "FDE"), a
->address
, a
->size
,
3852 ld::Atom::definitionRegular
, ld::Atom::combineNever
, ld::Atom::scopeTranslationUnit
,
3853 ld::Atom::typeCFI
, ld::Atom::symbolTableNotInFinalLinkedImages
,
3854 false, false, false, ld::Atom::Alignment(0));
3855 p
+= sizeof(Atom
<A
>);
3857 this->_endAtoms
= (Atom
<A
>*)p
;
3858 return cfis
.cfiCount
;
3862 template <> bool CFISection
<x86_64
>::bigEndian() { return false; }
3863 template <> bool CFISection
<x86
>::bigEndian() { return false; }
3864 template <> bool CFISection
<arm
>::bigEndian() { return false; }
3868 void CFISection
<x86_64
>::addCiePersonalityFixups(class Parser
<x86_64
>& parser
, const CFI_Atom_Info
* cieInfo
)
3870 uint8_t personalityEncoding
= cieInfo
->u
.cieInfo
.personality
.encodingOfTargetAddress
;
3871 if ( personalityEncoding
== 0x9B ) {
3872 // compiler always produces X86_64_RELOC_GOT with addend of 4 to personality function
3873 // CFISection<x86_64>::cfiParse() set targetAddress to be symbolIndex + 4 + addressInCIE
3874 uint32_t symbolIndex
= cieInfo
->u
.cieInfo
.personality
.targetAddress
- 4
3875 - cieInfo
->address
- cieInfo
->u
.cieInfo
.personality
.offsetInCFI
;
3876 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(symbolIndex
);
3877 const char* personalityName
= parser
.nameFromSymbol(sym
);
3879 Atom
<x86_64
>* cieAtom
= this->findAtomByAddress(cieInfo
->address
);
3880 Parser
<x86_64
>::SourceLocation
src(cieAtom
, cieInfo
->u
.cieInfo
.personality
.offsetInCFI
);
3881 parser
.addFixup(src
, ld::Fixup::k1of3
, ld::Fixup::kindSetTargetAddress
, false, personalityName
);
3882 parser
.addFixup(src
, ld::Fixup::k2of3
, ld::Fixup::kindAddAddend
, 4);
3883 parser
.addFixup(src
, ld::Fixup::k3of3
, ld::Fixup::kindStoreX86PCRel32GOT
);
3885 else if ( personalityEncoding
!= 0 ) {
3886 throwf("unsupported address encoding (%02X) of personality function in CIE",
3887 personalityEncoding
);
3892 void CFISection
<x86
>::addCiePersonalityFixups(class Parser
<x86
>& parser
, const CFI_Atom_Info
* cieInfo
)
3894 uint8_t personalityEncoding
= cieInfo
->u
.cieInfo
.personality
.encodingOfTargetAddress
;
3895 if ( (personalityEncoding
== 0x9B) || (personalityEncoding
== 0x90) ) {
3896 uint32_t offsetInCFI
= cieInfo
->u
.cieInfo
.personality
.offsetInCFI
;
3897 uint32_t nlpAddr
= cieInfo
->u
.cieInfo
.personality
.targetAddress
;
3898 Atom
<x86
>* cieAtom
= this->findAtomByAddress(cieInfo
->address
);
3899 Atom
<x86
>* nlpAtom
= parser
.findAtomByAddress(nlpAddr
);
3900 assert(nlpAtom
->contentType() == ld::Atom::typeNonLazyPointer
);
3901 Parser
<x86
>::SourceLocation
src(cieAtom
, cieInfo
->u
.cieInfo
.personality
.offsetInCFI
);
3903 parser
.addFixup(src
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, nlpAtom
);
3904 parser
.addFixup(src
, ld::Fixup::k2of4
, ld::Fixup::kindSubtractTargetAddress
, cieAtom
);
3905 parser
.addFixup(src
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractAddend
, offsetInCFI
);
3906 parser
.addFixup(src
, ld::Fixup::k4of4
, ld::Fixup::kindStoreLittleEndian32
);
3908 else if ( personalityEncoding
!= 0 ) {
3909 throwf("unsupported address encoding (%02X) of personality function in CIE", personalityEncoding
);
3914 template <typename A
>
3915 void CFISection
<A
>::addCiePersonalityFixups(class Parser
<A
>& parser
, const CFI_Atom_Info
* cieInfo
)
3921 template <typename A
>
3922 void CFISection
<A
>::makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
& cfis
)
3924 ld::Fixup::Kind store32
= bigEndian() ? ld::Fixup::kindStoreBigEndian32
: ld::Fixup::kindStoreLittleEndian32
;
3925 ld::Fixup::Kind store64
= bigEndian() ? ld::Fixup::kindStoreBigEndian64
: ld::Fixup::kindStoreLittleEndian64
;
3927 // add all references for FDEs, including implicit group references
3928 const CFI_Atom_Info
* end
= &cfis
.cfiArray
[cfis
.cfiCount
];
3929 for(const CFI_Atom_Info
* p
= &cfis
.cfiArray
[0]; p
< end
; ++p
) {
3931 // add reference to personality function if used
3932 if ( p
->u
.cieInfo
.personality
.targetAddress
!= CFI_INVALID_ADDRESS
) {
3933 this->addCiePersonalityFixups(parser
, p
);
3938 Atom
<A
>* fdeAtom
= this->findAtomByAddress(p
->address
);
3939 // find function Atom
3940 Atom
<A
>* functionAtom
= parser
.findAtomByAddress(p
->u
.fdeInfo
.function
.targetAddress
);
3942 Atom
<A
>* cieAtom
= this->findAtomByAddress(p
->u
.fdeInfo
.cie
.targetAddress
);
3944 Atom
<A
>* lsdaAtom
= NULL
;
3945 if ( p
->u
.fdeInfo
.lsda
.targetAddress
!= CFI_INVALID_ADDRESS
) {
3946 lsdaAtom
= parser
.findAtomByAddress(p
->u
.fdeInfo
.lsda
.targetAddress
);
3948 // add reference from FDE to CIE (always 32-bit pc-rel)
3949 typename Parser
<A
>::SourceLocation
fdeToCieSrc(fdeAtom
, p
->u
.fdeInfo
.cie
.offsetInCFI
);
3950 parser
.addFixup(fdeToCieSrc
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, fdeAtom
);
3951 parser
.addFixup(fdeToCieSrc
, ld::Fixup::k2of4
, ld::Fixup::kindAddAddend
, p
->u
.fdeInfo
.cie
.offsetInCFI
);
3952 parser
.addFixup(fdeToCieSrc
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractTargetAddress
, cieAtom
);
3953 parser
.addFixup(fdeToCieSrc
, ld::Fixup::k4of4
, store32
, cieAtom
);
3955 // add reference from FDE to function
3956 typename Parser
<A
>::SourceLocation
fdeToFuncSrc(fdeAtom
, p
->u
.fdeInfo
.function
.offsetInCFI
);
3957 switch (p
->u
.fdeInfo
.function
.encodingOfTargetAddress
) {
3958 case DW_EH_PE_pcrel
|DW_EH_PE_ptr
:
3959 if ( sizeof(typename
A::P::uint_t
) == 8 ) {
3960 parser
.addFixup(fdeToFuncSrc
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, functionAtom
);
3961 parser
.addFixup(fdeToFuncSrc
, ld::Fixup::k2of4
, ld::Fixup::kindSubtractTargetAddress
, fdeAtom
);
3962 parser
.addFixup(fdeToFuncSrc
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractAddend
, p
->u
.fdeInfo
.function
.offsetInCFI
);
3963 parser
.addFixup(fdeToFuncSrc
, ld::Fixup::k4of4
, store64
);
3966 // else fall into 32-bit case
3967 case DW_EH_PE_pcrel
|DW_EH_PE_sdata4
:
3968 parser
.addFixup(fdeToFuncSrc
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, functionAtom
);
3969 parser
.addFixup(fdeToFuncSrc
, ld::Fixup::k2of4
, ld::Fixup::kindSubtractTargetAddress
, fdeAtom
);
3970 parser
.addFixup(fdeToFuncSrc
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractAddend
, p
->u
.fdeInfo
.function
.offsetInCFI
);
3971 parser
.addFixup(fdeToFuncSrc
, ld::Fixup::k4of4
, store32
);
3974 throw "unsupported encoding in FDE of pointer to function";
3977 // add reference from FDE to LSDA
3978 typename Parser
<A
>::SourceLocation
fdeToLsdaSrc(fdeAtom
, p
->u
.fdeInfo
.lsda
.offsetInCFI
);
3979 if ( lsdaAtom
!= NULL
) {
3980 switch (p
->u
.fdeInfo
.lsda
.encodingOfTargetAddress
) {
3981 case DW_EH_PE_pcrel
|DW_EH_PE_ptr
:
3982 if ( sizeof(typename
A::P::uint_t
) == 8 ) {
3983 parser
.addFixup(fdeToLsdaSrc
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, lsdaAtom
);
3984 parser
.addFixup(fdeToLsdaSrc
, ld::Fixup::k2of4
, ld::Fixup::kindSubtractTargetAddress
, fdeAtom
);
3985 parser
.addFixup(fdeToLsdaSrc
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractAddend
, p
->u
.fdeInfo
.lsda
.offsetInCFI
);
3986 parser
.addFixup(fdeToLsdaSrc
, ld::Fixup::k4of4
, store64
);
3989 // else fall into 32-bit case
3990 case DW_EH_PE_pcrel
|DW_EH_PE_sdata4
:
3991 parser
.addFixup(fdeToLsdaSrc
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, lsdaAtom
);
3992 parser
.addFixup(fdeToLsdaSrc
, ld::Fixup::k2of4
, ld::Fixup::kindSubtractTargetAddress
, fdeAtom
);
3993 parser
.addFixup(fdeToLsdaSrc
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractAddend
, p
->u
.fdeInfo
.lsda
.offsetInCFI
);
3994 parser
.addFixup(fdeToLsdaSrc
, ld::Fixup::k4of4
, store32
);
3997 throw "unsupported encoding in FDE of pointer to LSDA";
4001 // FDE is in group lead by function atom
4002 typename Parser
<A
>::SourceLocation
fdeSrc(functionAtom
,0);
4003 parser
.addFixup(fdeSrc
, ld::Fixup::k1of1
, ld::Fixup::kindNoneGroupSubordinateFDE
, fdeAtom
);
4005 // LSDA is in group lead by function atom
4006 if ( lsdaAtom
!= NULL
) {
4007 parser
.addFixup(fdeSrc
, ld::Fixup::k1of1
, ld::Fixup::kindNoneGroupSubordinateLSDA
, lsdaAtom
);
4016 template <typename A
>
4017 const void* CFISection
<A
>::OAS::mappedAddress(pint_t addr
)
4019 if ( (_ehFrameStartAddr
<= addr
) && (addr
< _ehFrameEndAddr
) )
4020 return &_ehFrameContent
[addr
-_ehFrameStartAddr
];
4022 // requested bytes are not in __eh_frame section
4023 // this can occur when examining the instruction bytes in the __text
4024 File
<A
>& file
= _ehFrameSection
.file();
4025 for (uint32_t i
=0; i
< file
._sectionsArrayCount
; ++i
) {
4026 const macho_section
<typename
A::P
>* sect
= file
._sectionsArray
[i
]->machoSection();
4027 // TentativeDefinitionSection and AbsoluteSymbolSection have no mach-o section
4028 if ( sect
!= NULL
) {
4029 if ( (sect
->addr() <= addr
) && (addr
< (sect
->addr()+sect
->size())) ) {
4030 return file
.fileContent() + sect
->offset() + addr
- sect
->addr();
4034 throwf("__eh_frame parsing problem. Can't find target of reference to address 0x%08llX", (uint64_t)addr
);
4039 template <typename A
>
4040 uint64_t CFISection
<A
>::OAS::getULEB128(pint_t
& logicalAddr
, pint_t end
)
4042 uintptr_t size
= (end
- logicalAddr
);
4043 libunwind::LocalAddressSpace::pint_t laddr
= (libunwind::LocalAddressSpace::pint_t
)mappedAddress(logicalAddr
);
4044 libunwind::LocalAddressSpace::pint_t sladdr
= laddr
;
4045 uint64_t result
= libunwind::LocalAddressSpace::getULEB128(laddr
, laddr
+size
);
4046 logicalAddr
+= (laddr
-sladdr
);
4050 template <typename A
>
4051 int64_t CFISection
<A
>::OAS::getSLEB128(pint_t
& logicalAddr
, pint_t end
)
4053 uintptr_t size
= (end
- logicalAddr
);
4054 libunwind::LocalAddressSpace::pint_t laddr
= (libunwind::LocalAddressSpace::pint_t
)mappedAddress(logicalAddr
);
4055 libunwind::LocalAddressSpace::pint_t sladdr
= laddr
;
4056 int64_t result
= libunwind::LocalAddressSpace::getSLEB128(laddr
, laddr
+size
);
4057 logicalAddr
+= (laddr
-sladdr
);
4061 template <typename A
>
4062 typename
A::P::uint_t CFISection
<A
>::OAS::getEncodedP(pint_t
& addr
, pint_t end
, uint8_t encoding
)
4064 pint_t startAddr
= addr
;
4069 switch (encoding
& 0x0F) {
4071 result
= getP(addr
);
4072 p
+= sizeof(pint_t
);
4075 case DW_EH_PE_uleb128
:
4076 result
= getULEB128(addr
, end
);
4078 case DW_EH_PE_udata2
:
4079 result
= get16(addr
);
4083 case DW_EH_PE_udata4
:
4084 result
= get32(addr
);
4088 case DW_EH_PE_udata8
:
4089 result
= get64(addr
);
4093 case DW_EH_PE_sleb128
:
4094 result
= getSLEB128(addr
, end
);
4096 case DW_EH_PE_sdata2
:
4097 result
= (int16_t)get16(addr
);
4101 case DW_EH_PE_sdata4
:
4102 result
= (int32_t)get32(addr
);
4106 case DW_EH_PE_sdata8
:
4107 result
= get64(addr
);
4112 throwf("ObjectFileAddressSpace<A>::getEncodedP() encoding 0x%08X not supported", encoding
);
4115 // then add relative offset
4116 switch ( encoding
& 0x70 ) {
4117 case DW_EH_PE_absptr
:
4120 case DW_EH_PE_pcrel
:
4121 result
+= startAddr
;
4123 case DW_EH_PE_textrel
:
4124 throw "DW_EH_PE_textrel pointer encoding not supported";
4126 case DW_EH_PE_datarel
:
4127 throw "DW_EH_PE_datarel pointer encoding not supported";
4129 case DW_EH_PE_funcrel
:
4130 throw "DW_EH_PE_funcrel pointer encoding not supported";
4132 case DW_EH_PE_aligned
:
4133 throw "DW_EH_PE_aligned pointer encoding not supported";
4136 throwf("ObjectFileAddressSpace<A>::getEncodedP() encoding 0x%08X not supported", encoding
);
4140 // Note: DW_EH_PE_indirect is only used in CIEs to refernce the personality pointer
4141 // When parsing .o files that pointer contains zero, so we don't to return that.
4142 // Instead we skip the dereference and return the address of the pointer.
4143 // if ( encoding & DW_EH_PE_indirect )
4144 // result = getP(result);
4150 const char* CUSection
<x86_64
>::personalityName(class Parser
<x86_64
>& parser
, const macho_relocation_info
<x86_64::P
>* reloc
)
4152 assert(reloc
->r_extern() && "reloc not extern on personality column in __compact_unwind section");
4153 assert((reloc
->r_type() == X86_64_RELOC_UNSIGNED
) && "wrong reloc type on personality column in __compact_unwind section");
4154 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(reloc
->r_symbolnum());
4155 return parser
.nameFromSymbol(sym
);
4159 const char* CUSection
<x86
>::personalityName(class Parser
<x86
>& parser
, const macho_relocation_info
<x86::P
>* reloc
)
4161 assert(reloc
->r_extern() && "reloc not extern on personality column in __compact_unwind section");
4162 assert((reloc
->r_type() == GENERIC_RELOC_VANILLA
) && "wrong reloc type on personality column in __compact_unwind section");
4163 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(reloc
->r_symbolnum());
4164 return parser
.nameFromSymbol(sym
);
4167 template <typename A
>
4168 const char* CUSection
<A
>::personalityName(class Parser
<A
>& parser
, const macho_relocation_info
<P
>* reloc
)
4174 template <typename A
>
4175 int CUSection
<A
>::infoSorter(const void* l
, const void* r
)
4177 // sort references by symbol index, then address
4178 const Info
* left
= (Info
*)l
;
4179 const Info
* right
= (Info
*)r
;
4180 if ( left
->functionSymbolIndex
== right
->functionSymbolIndex
)
4181 return (left
->functionStartAddress
- right
->functionStartAddress
);
4183 return (left
->functionSymbolIndex
- right
->functionSymbolIndex
);
4186 template <typename A
>
4187 void CUSection
<A
>::parse(class Parser
<A
>& parser
, uint32_t cnt
, Info array
[])
4189 // walk section content and copy to Info array
4190 const macho_compact_unwind_entry
<P
>* const entries
= (macho_compact_unwind_entry
<P
>*)(this->file().fileContent() + this->_machOSection
->offset());
4191 for (uint32_t i
=0; i
< cnt
; ++i
) {
4192 Info
* info
= &array
[i
];
4193 const macho_compact_unwind_entry
<P
>* entry
= &entries
[i
];
4194 info
->functionStartAddress
= entry
->codeStart();
4195 info
->functionSymbolIndex
= 0xFFFFFFFF;
4196 info
->rangeLength
= entry
->codeLen();
4197 info
->compactUnwindInfo
= entry
->compactUnwindInfo();
4198 info
->personality
= NULL
;
4199 info
->lsdaAddress
= entry
->lsda();
4200 info
->function
= NULL
;
4202 if ( (info
->compactUnwindInfo
& UNWIND_PERSONALITY_MASK
) != 0 )
4203 warning("no bits should be set in UNWIND_PERSONALITY_MASK of compact unwind encoding in __LD,__compact_unwind section");
4204 if ( info
->lsdaAddress
!= 0 ) {
4205 info
->compactUnwindInfo
|= UNWIND_HAS_LSDA
;
4209 // scan relocs, local relocs are useless - ignore them
4210 // extern relocs are needed for personality references (possibly for function/lsda refs??)
4211 const macho_relocation_info
<P
>* relocs
= (macho_relocation_info
<P
>*)(this->file().fileContent() + this->_machOSection
->reloff());
4212 const macho_relocation_info
<P
>* relocsEnd
= &relocs
[this->_machOSection
->nreloc()];
4213 for (const macho_relocation_info
<P
>* reloc
= relocs
; reloc
< relocsEnd
; ++reloc
) {
4214 if ( reloc
->r_extern() ) {
4215 // only expect external relocs on some colummns
4216 if ( (reloc
->r_address() % sizeof(macho_compact_unwind_entry
<P
>)) == macho_compact_unwind_entry
<P
>::personalityFieldOffset() ) {
4217 uint32_t entryIndex
= reloc
->r_address() / sizeof(macho_compact_unwind_entry
<P
>);
4218 array
[entryIndex
].personality
= this->personalityName(parser
, reloc
);
4220 else if ( (reloc
->r_address() % sizeof(macho_compact_unwind_entry
<P
>)) == macho_compact_unwind_entry
<P
>::lsdaFieldOffset() ) {
4221 uint32_t entryIndex
= reloc
->r_address() / sizeof(macho_compact_unwind_entry
<P
>);
4222 const macho_nlist
<P
>& lsdaSym
= parser
.symbolFromIndex(reloc
->r_symbolnum());
4223 if ( (lsdaSym
.n_type() & N_TYPE
) == N_SECT
)
4224 array
[entryIndex
].lsdaAddress
= lsdaSym
.n_value();
4226 warning("unexpected extern relocation to lsda in __compact_unwind section");
4228 else if ( (reloc
->r_address() % sizeof(macho_compact_unwind_entry
<P
>)) == macho_compact_unwind_entry
<P
>::codeStartFieldOffset() ) {
4229 uint32_t entryIndex
= reloc
->r_address() / sizeof(macho_compact_unwind_entry
<P
>);
4230 array
[entryIndex
].functionSymbolIndex
= reloc
->r_symbolnum();
4233 warning("unexpected extern relocation in __compact_unwind section");
4238 // sort array by function start address so unwind infos will be contiguous for a given function
4239 ::qsort(array
, cnt
, sizeof(Info
), infoSorter
);
4242 template <typename A
>
4243 uint32_t CUSection
<A
>::count()
4245 const macho_section
<P
>* machoSect
= this->machoSection();
4246 if ( (machoSect
->size() % sizeof(macho_compact_unwind_entry
<P
>)) != 0 )
4247 throw "malformed __LD,__compact_unwind section, bad length";
4249 return machoSect
->size() / sizeof(macho_compact_unwind_entry
<P
>);
4252 template <typename A
>
4253 void CUSection
<A
>::makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
& cus
)
4255 Info
* const arrayStart
= cus
.cuArray
;
4256 Info
* const arrayEnd
= &cus
.cuArray
[cus
.cuCount
];
4257 for (Info
* info
=arrayStart
; info
< arrayEnd
; ++info
) {
4258 // if external reloc was used, real address is symbol n_value + addend
4259 if ( info
->functionSymbolIndex
!= 0xFFFFFFFF )
4260 info
->functionStartAddress
+= parser
.symbolFromIndex(info
->functionSymbolIndex
).n_value();
4261 // find function atom from address
4262 info
->function
= parser
.findAtomByAddress(info
->functionStartAddress
);
4263 // find lsda atom from address
4264 if ( info
->lsdaAddress
!= 0 ) {
4265 info
->lsda
= parser
.findAtomByAddress(info
->lsdaAddress
);
4266 // add lsda subordinate
4267 typename Parser
<A
>::SourceLocation
src(info
->function
, info
->functionStartAddress
- info
->function
->objectAddress());
4268 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindNoneGroupSubordinateLSDA
, info
->lsda
);
4270 if ( info
->personality
!= NULL
) {
4271 // add personality subordinate
4272 typename Parser
<A
>::SourceLocation
src(info
->function
, info
->functionStartAddress
- info
->function
->objectAddress());
4273 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindNoneGroupSubordinatePersonality
, false, info
->personality
);
4279 template <typename A
>
4280 SymboledSection
<A
>::SymboledSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
4281 : Section
<A
>(f
, s
), _type(ld::Atom::typeUnclassified
)
4283 switch ( s
->flags() & SECTION_TYPE
) {
4285 _type
= ld::Atom::typeZeroFill
;
4287 case S_MOD_INIT_FUNC_POINTERS
:
4288 _type
= ld::Atom::typeInitializerPointers
;
4290 case S_MOD_TERM_FUNC_POINTERS
:
4291 _type
= ld::Atom::typeTerminatorPointers
;
4293 case S_THREAD_LOCAL_VARIABLES
:
4294 _type
= ld::Atom::typeTLV
;
4296 case S_THREAD_LOCAL_ZEROFILL
:
4297 _type
= ld::Atom::typeTLVZeroFill
;
4299 case S_THREAD_LOCAL_REGULAR
:
4300 _type
= ld::Atom::typeTLVInitialValue
;
4302 case S_THREAD_LOCAL_INIT_FUNCTION_POINTERS
:
4303 _type
= ld::Atom::typeTLVInitializerPointers
;
4306 if ( strncmp(s
->sectname(), "__gcc_except_tab", 16) == 0 )
4307 _type
= ld::Atom::typeLSDA
;
4308 else if ( this->type() == ld::Section::typeInitializerPointers
)
4309 _type
= ld::Atom::typeInitializerPointers
;
4315 template <typename A
>
4316 bool SymboledSection
<A
>::dontDeadStrip()
4319 case ld::Atom::typeInitializerPointers
:
4320 case ld::Atom::typeTerminatorPointers
:
4323 // model an object file without MH_SUBSECTIONS_VIA_SYMBOLS as one in which nothing can be dead stripped
4324 if ( ! this->_file
.canScatterAtoms() )
4327 return Section
<A
>::dontDeadStrip();
4333 template <typename A
>
4334 uint32_t SymboledSection
<A
>::computeAtomCount(class Parser
<A
>& parser
,
4335 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
4336 const struct Parser
<A
>::CFI_CU_InfoArrays
&)
4338 const pint_t startAddr
= this->_machOSection
->addr();
4339 const pint_t endAddr
= startAddr
+ this->_machOSection
->size();
4340 const uint32_t sectNum
= this->sectionNum(parser
);
4345 const macho_nlist
<P
>* sym
;
4346 while ( it
.next(parser
, sectNum
, startAddr
, endAddr
, &addr
, &size
, &sym
) ) {
4349 //fprintf(stderr, "computeAtomCount(%s,%s) => %d\n", this->segmentName(), this->sectionName(), count);
4353 template <typename A
>
4354 uint32_t SymboledSection
<A
>::appendAtoms(class Parser
<A
>& parser
, uint8_t* p
,
4355 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
4356 const struct Parser
<A
>::CFI_CU_InfoArrays
&)
4358 this->_beginAtoms
= (Atom
<A
>*)p
;
4360 //fprintf(stderr, "SymboledSection::appendAtoms() in section %s\n", this->_machOSection->sectname());
4361 const pint_t startAddr
= this->_machOSection
->addr();
4362 const pint_t endAddr
= startAddr
+ this->_machOSection
->size();
4363 const uint32_t sectNum
= this->sectionNum(parser
);
4368 const macho_nlist
<P
>* label
;
4369 while ( it
.next(parser
, sectNum
, startAddr
, endAddr
, &addr
, &size
, &label
) ) {
4370 Atom
<A
>* allocatedSpace
= (Atom
<A
>*)p
;
4371 // is break because of label or CFI?
4372 if ( label
!= NULL
) {
4373 // The size is computed based on the address of the next label (or the end of the section for the last label)
4374 // If there are two labels at the same address, we want them one to be an alias of the other.
4375 // If the label is at the end of a section, it is has zero size, but is not an alias
4376 const bool isAlias
= ( (size
== 0) && (addr
< endAddr
) );
4377 new (allocatedSpace
) Atom
<A
>(*this, parser
, *label
, size
, isAlias
);
4379 this->_hasAliases
= true;
4382 ld::Atom::SymbolTableInclusion inclusion
= ld::Atom::symbolTableNotIn
;
4383 ld::Atom::ContentType ctype
= this->contentType();
4384 if ( ctype
== ld::Atom::typeLSDA
)
4385 inclusion
= ld::Atom::symbolTableInWithRandomAutoStripLabel
;
4386 new (allocatedSpace
) Atom
<A
>(*this, "anon", addr
, size
, ld::Atom::definitionRegular
, ld::Atom::combineNever
,
4387 ld::Atom::scopeTranslationUnit
, ctype
, inclusion
,
4388 this->dontDeadStrip(), false, false, this->alignmentForAddress(addr
));
4390 p
+= sizeof(Atom
<A
>);
4394 this->_endAtoms
= (Atom
<A
>*)p
;
4399 template <typename A
>
4400 uint32_t ImplicitSizeSection
<A
>::computeAtomCount(class Parser
<A
>& parser
,
4401 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
4402 const struct Parser
<A
>::CFI_CU_InfoArrays
&)
4405 const macho_section
<P
>* sect
= this->machoSection();
4406 const pint_t startAddr
= sect
->addr();
4407 const pint_t endAddr
= startAddr
+ sect
->size();
4408 for (pint_t addr
= startAddr
; addr
< endAddr
; addr
+= elementSizeAtAddress(addr
) ) {
4409 if ( useElementAt(parser
, it
, addr
) )
4412 if ( it
.fileHasOverlappingSymbols
&& (sect
->size() != 0) && (this->combine(parser
, startAddr
) == ld::Atom::combineByNameAndContent
) ) {
4413 // if there are multiple labels in this section for the same address, then clone them into multi atoms
4414 pint_t prevSymbolAddr
= (pint_t
)(-1);
4415 uint8_t prevSymbolSectNum
= 0;
4416 for(uint32_t i
=0; i
< it
.sortedSymbolCount
; ++i
) {
4417 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(it
.sortedSymbolIndexes
[i
]);
4418 const pint_t symbolAddr
= sym
.n_value();
4419 const pint_t symbolSectNum
= sym
.n_sect();
4420 if ( (symbolAddr
== prevSymbolAddr
) && (prevSymbolSectNum
== symbolSectNum
) && (symbolSectNum
== this->sectionNum(parser
)) ) {
4423 prevSymbolAddr
= symbolAddr
;
4424 prevSymbolSectNum
= symbolSectNum
;
4430 template <typename A
>
4431 uint32_t ImplicitSizeSection
<A
>::appendAtoms(class Parser
<A
>& parser
, uint8_t* p
,
4432 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
4433 const struct Parser
<A
>::CFI_CU_InfoArrays
&)
4435 this->_beginAtoms
= (Atom
<A
>*)p
;
4437 const macho_section
<P
>* sect
= this->machoSection();
4438 const pint_t startAddr
= sect
->addr();
4439 const pint_t endAddr
= startAddr
+ sect
->size();
4440 const uint32_t sectNum
= this->sectionNum(parser
);
4441 //fprintf(stderr, "ImplicitSizeSection::appendAtoms() in section %s\n", sect->sectname());
4445 const macho_nlist
<P
>* foundLabel
;
4446 Atom
<A
>* allocatedSpace
;
4447 while ( it
.next(parser
, sectNum
, startAddr
, endAddr
, &foundAddr
, &size
, &foundLabel
) ) {
4448 if ( foundLabel
!= NULL
) {
4449 pint_t labeledAtomSize
= this->elementSizeAtAddress(foundAddr
);
4450 allocatedSpace
= (Atom
<A
>*)p
;
4451 if ( this->ignoreLabel(parser
.nameFromSymbol(*foundLabel
)) ) {
4452 //fprintf(stderr, " 0x%08llX make annon\n", (uint64_t)foundAddr);
4453 new (allocatedSpace
) Atom
<A
>(*this, this->unlabeledAtomName(parser
, foundAddr
), foundAddr
,
4454 this->elementSizeAtAddress(foundAddr
), this->definition(),
4455 this->combine(parser
, foundAddr
), this->scopeAtAddress(parser
, foundAddr
),
4456 this->contentType(), this->symbolTableInclusion(),
4457 this->dontDeadStrip(), false, false, this->alignmentForAddress(foundAddr
));
4460 // make named atom for label
4461 //fprintf(stderr, " 0x%08llX make labeled\n", (uint64_t)foundAddr);
4462 new (allocatedSpace
) Atom
<A
>(*this, parser
, *foundLabel
, labeledAtomSize
);
4465 p
+= sizeof(Atom
<A
>);
4466 foundAddr
+= labeledAtomSize
;
4467 size
-= labeledAtomSize
;
4469 // some number of anonymous atoms
4470 for (pint_t addr
= foundAddr
; addr
< (foundAddr
+size
); addr
+= elementSizeAtAddress(addr
) ) {
4471 // make anon atoms for area before label
4472 if ( this->useElementAt(parser
, it
, addr
) ) {
4473 //fprintf(stderr, " 0x%08llX make annon\n", (uint64_t)addr);
4474 allocatedSpace
= (Atom
<A
>*)p
;
4475 new (allocatedSpace
) Atom
<A
>(*this, this->unlabeledAtomName(parser
, addr
), addr
, this->elementSizeAtAddress(addr
),
4476 this->definition(), this->combine(parser
, addr
), this->scopeAtAddress(parser
, addr
),
4477 this->contentType(), this->symbolTableInclusion(),
4478 this->dontDeadStrip(), false, false, this->alignmentForAddress(addr
));
4480 p
+= sizeof(Atom
<A
>);
4485 this->_endAtoms
= (Atom
<A
>*)p
;
4491 template <typename A
>
4492 unsigned long Literal4Section
<A
>::contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
4494 const uint32_t* literalContent
= (uint32_t*)atom
->contentPointer();
4495 return *literalContent
;
4498 template <typename A
>
4499 bool Literal4Section
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
4500 const ld::IndirectBindingTable
& ind
) const
4502 assert(this->type() == rhs
.section().type());
4503 const uint32_t* literalContent
= (uint32_t*)atom
->contentPointer();
4505 const Atom
<A
>* rhsAtom
= dynamic_cast<const Atom
<A
>*>(&rhs
);
4506 assert(rhsAtom
!= NULL
);
4507 if ( rhsAtom
!= NULL
) {
4508 const uint32_t* rhsLiteralContent
= (uint32_t*)rhsAtom
->contentPointer();
4509 return (*literalContent
== *rhsLiteralContent
);
4515 template <typename A
>
4516 unsigned long Literal8Section
<A
>::contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
4519 const uint64_t* literalContent
= (uint64_t*)atom
->contentPointer();
4520 return *literalContent
;
4522 unsigned long hash
= 5381;
4523 const uint8_t* byteContent
= atom
->contentPointer();
4524 for (int i
=0; i
< 8; ++i
) {
4525 hash
= hash
* 33 + byteContent
[i
];
4531 template <typename A
>
4532 bool Literal8Section
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
4533 const ld::IndirectBindingTable
& ind
) const
4535 if ( rhs
.section().type() != ld::Section::typeLiteral8
)
4537 assert(this->type() == rhs
.section().type());
4538 const uint64_t* literalContent
= (uint64_t*)atom
->contentPointer();
4540 const Atom
<A
>* rhsAtom
= dynamic_cast<const Atom
<A
>*>(&rhs
);
4541 assert(rhsAtom
!= NULL
);
4542 if ( rhsAtom
!= NULL
) {
4543 const uint64_t* rhsLiteralContent
= (uint64_t*)rhsAtom
->contentPointer();
4544 return (*literalContent
== *rhsLiteralContent
);
4550 template <typename A
>
4551 unsigned long Literal16Section
<A
>::contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
4553 unsigned long hash
= 5381;
4554 const uint8_t* byteContent
= atom
->contentPointer();
4555 for (int i
=0; i
< 16; ++i
) {
4556 hash
= hash
* 33 + byteContent
[i
];
4561 template <typename A
>
4562 bool Literal16Section
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
4563 const ld::IndirectBindingTable
& ind
) const
4565 if ( rhs
.section().type() != ld::Section::typeLiteral16
)
4567 assert(this->type() == rhs
.section().type());
4568 const uint64_t* literalContent
= (uint64_t*)atom
->contentPointer();
4570 const Atom
<A
>* rhsAtom
= dynamic_cast<const Atom
<A
>*>(&rhs
);
4571 assert(rhsAtom
!= NULL
);
4572 if ( rhsAtom
!= NULL
) {
4573 const uint64_t* rhsLiteralContent
= (uint64_t*)rhsAtom
->contentPointer();
4574 return ((literalContent
[0] == rhsLiteralContent
[0]) && (literalContent
[1] == rhsLiteralContent
[1]));
4581 template <typename A
>
4582 typename
A::P::uint_t CStringSection
<A
>::elementSizeAtAddress(pint_t addr
)
4584 const macho_section
<P
>* sect
= this->machoSection();
4585 const char* stringContent
= (char*)(this->file().fileContent() + sect
->offset() + addr
- sect
->addr());
4586 return strlen(stringContent
) + 1;
4589 template <typename A
>
4590 bool CStringSection
<A
>::useElementAt(Parser
<A
>& parser
, struct Parser
<A
>::LabelAndCFIBreakIterator
& it
, pint_t addr
)
4595 template <typename A
>
4596 bool CStringSection
<A
>::ignoreLabel(const char* label
)
4598 return (label
[0] == 'L') || (label
[0] == 'l');
4601 template <typename A
>
4602 Atom
<A
>* CStringSection
<A
>::findAtomByAddress(pint_t addr
)
4604 Atom
<A
>* result
= this->findContentAtomByAddress(addr
, this->_beginAtoms
, this->_endAtoms
);
4608 template <typename A
>
4609 unsigned long CStringSection
<A
>::contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
4611 unsigned long hash
= 5381;
4612 const char* stringContent
= (char*)atom
->contentPointer();
4613 for (const char* s
= stringContent
; *s
!= '\0'; ++s
) {
4614 hash
= hash
* 33 + *s
;
4620 template <typename A
>
4621 bool CStringSection
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
4622 const ld::IndirectBindingTable
& ind
) const
4624 if ( rhs
.section().type() != ld::Section::typeCString
)
4626 assert(this->type() == rhs
.section().type());
4627 assert(strcmp(this->sectionName(), rhs
.section().sectionName())== 0);
4628 assert(strcmp(this->segmentName(), rhs
.section().segmentName())== 0);
4629 const char* stringContent
= (char*)atom
->contentPointer();
4631 const Atom
<A
>* rhsAtom
= dynamic_cast<const Atom
<A
>*>(&rhs
);
4632 assert(rhsAtom
!= NULL
);
4633 if ( rhsAtom
!= NULL
) {
4634 if ( atom
->_size
!= rhsAtom
->_size
)
4636 const char* rhsStringContent
= (char*)rhsAtom
->contentPointer();
4637 return (strcmp(stringContent
, rhsStringContent
) == 0);
4644 ld::Fixup::Kind NonLazyPointerSection
<x86
>::fixupKind()
4646 return ld::Fixup::kindStoreLittleEndian32
;
4650 ld::Fixup::Kind NonLazyPointerSection
<arm
>::fixupKind()
4652 return ld::Fixup::kindStoreLittleEndian32
;
4657 void NonLazyPointerSection
<x86_64
>::makeFixups(class Parser
<x86_64
>& parser
, const struct Parser
<x86_64
>::CFI_CU_InfoArrays
&)
4659 assert(0 && "x86_64 should not have non-lazy-pointer sections in .o files");
4662 template <typename A
>
4663 void NonLazyPointerSection
<A
>::makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
&)
4665 // add references for each NLP atom based on indirect symbol table
4666 const macho_section
<P
>* sect
= this->machoSection();
4667 const pint_t endAddr
= sect
->addr() + sect
->size();
4668 for( pint_t addr
= sect
->addr(); addr
< endAddr
; addr
+= sizeof(pint_t
)) {
4669 typename Parser
<A
>::SourceLocation src
;
4670 typename Parser
<A
>::TargetDesc target
;
4671 src
.atom
= this->findAtomByAddress(addr
);
4672 src
.offsetInAtom
= 0;
4673 uint32_t symIndex
= parser
.symbolIndexFromIndirectSectionAddress(addr
, sect
);
4676 target
.weakImport
= false;
4678 if ( symIndex
== INDIRECT_SYMBOL_LOCAL
) {
4679 // use direct reference for local symbols
4680 const pint_t
* nlpContent
= (pint_t
*)(this->file().fileContent() + sect
->offset() + addr
- sect
->addr());
4681 pint_t targetAddr
= P::getP(*nlpContent
);
4682 target
.atom
= parser
.findAtomByAddress(targetAddr
);
4683 target
.weakImport
= false;
4684 target
.addend
= (targetAddr
- target
.atom
->objectAddress());
4685 // <rdar://problem/8385011> if pointer to thumb function, mask of thumb bit (not an addend of +1)
4686 if ( target
.atom
->isThumb() )
4687 target
.addend
&= (-2);
4688 assert(src
.atom
->combine() == ld::Atom::combineNever
);
4691 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(symIndex
);
4692 // use direct reference for local symbols
4693 if ( ((sym
.n_type() & N_TYPE
) == N_SECT
) && ((sym
.n_type() & N_EXT
) == 0) ) {
4694 parser
.findTargetFromAddressAndSectionNum(sym
.n_value(), sym
.n_sect(), target
);
4695 assert(src
.atom
->combine() == ld::Atom::combineNever
);
4698 target
.name
= parser
.nameFromSymbol(sym
);
4699 target
.weakImport
= parser
.weakImportFromSymbol(sym
);
4700 assert(src
.atom
->combine() == ld::Atom::combineByNameAndReferences
);
4703 parser
.addFixups(src
, this->fixupKind(), target
);
4707 template <typename A
>
4708 ld::Atom::Combine NonLazyPointerSection
<A
>::combine(Parser
<A
>& parser
, pint_t addr
)
4710 const macho_section
<P
>* sect
= this->machoSection();
4711 uint32_t symIndex
= parser
.symbolIndexFromIndirectSectionAddress(addr
, sect
);
4712 if ( symIndex
== INDIRECT_SYMBOL_LOCAL
)
4713 return ld::Atom::combineNever
;
4715 // don't coalesce non-lazy-pointers to local symbols
4716 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(symIndex
);
4717 if ( ((sym
.n_type() & N_TYPE
) == N_SECT
) && ((sym
.n_type() & N_EXT
) == 0) )
4718 return ld::Atom::combineNever
;
4720 return ld::Atom::combineByNameAndReferences
;
4723 template <typename A
>
4724 const char* NonLazyPointerSection
<A
>::targetName(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
)
4726 assert(atom
->combine() == ld::Atom::combineByNameAndReferences
);
4727 assert(atom
->fixupCount() == 1);
4728 ld::Fixup::iterator fit
= atom
->fixupsBegin();
4729 const char* name
= NULL
;
4730 switch ( fit
->binding
) {
4731 case ld::Fixup::bindingByNameUnbound
:
4734 case ld::Fixup::bindingByContentBound
:
4735 name
= fit
->u
.target
->name();
4737 case ld::Fixup::bindingsIndirectlyBound
:
4738 name
= ind
.indirectName(fit
->u
.bindingIndex
);
4743 assert(name
!= NULL
);
4747 template <typename A
>
4748 unsigned long NonLazyPointerSection
<A
>::contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
4750 assert(atom
->combine() == ld::Atom::combineByNameAndReferences
);
4751 unsigned long hash
= 9508;
4752 for (const char* s
= this->targetName(atom
, ind
); *s
!= '\0'; ++s
) {
4753 hash
= hash
* 33 + *s
;
4758 template <typename A
>
4759 bool NonLazyPointerSection
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
4760 const ld::IndirectBindingTable
& indirectBindingTable
) const
4762 if ( rhs
.section().type() != ld::Section::typeNonLazyPointer
)
4764 assert(this->type() == rhs
.section().type());
4765 // there can be many non-lazy pointer in different section names
4766 // we only want to coalesce in same section name
4767 if ( *this != rhs
.section() )
4769 const Atom
<A
>* rhsAtom
= dynamic_cast<const Atom
<A
>*>(&rhs
);
4770 assert(rhsAtom
!= NULL
);
4771 const char* thisName
= this->targetName(atom
, indirectBindingTable
);
4772 const char* rhsName
= this->targetName(rhsAtom
, indirectBindingTable
);
4773 return (strcmp(thisName
, rhsName
) == 0);
4776 template <typename A
>
4777 ld::Atom::Scope NonLazyPointerSection
<A
>::scopeAtAddress(Parser
<A
>& parser
, pint_t addr
)
4779 const macho_section
<P
>* sect
= this->machoSection();
4780 uint32_t symIndex
= parser
.symbolIndexFromIndirectSectionAddress(addr
, sect
);
4781 if ( symIndex
== INDIRECT_SYMBOL_LOCAL
)
4782 return ld::Atom::scopeTranslationUnit
;
4784 return ld::Atom::scopeLinkageUnit
;
4788 template <typename A
>
4789 const uint8_t* CFStringSection
<A
>::targetContent(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
,
4790 ContentType
* ct
, unsigned int* count
)
4792 *ct
= contentUnknown
;
4793 for (ld::Fixup::iterator fit
=atom
->fixupsBegin(), end
=atom
->fixupsEnd(); fit
!= end
; ++fit
) {
4794 const ld::Atom
* targetAtom
= NULL
;
4795 switch ( fit
->binding
) {
4796 case ld::Fixup::bindingByNameUnbound
:
4797 // ignore reference to ___CFConstantStringClassReference
4798 // we are just looking for reference to backing string data
4799 assert(fit
->offsetInAtom
== 0);
4800 assert(strcmp(fit
->u
.name
, "___CFConstantStringClassReference") == 0);
4802 case ld::Fixup::bindingDirectlyBound
:
4803 case ld::Fixup::bindingByContentBound
:
4804 targetAtom
= fit
->u
.target
;
4806 case ld::Fixup::bindingsIndirectlyBound
:
4807 targetAtom
= ind
.indirectAtom(fit
->u
.bindingIndex
);
4810 assert(0 && "bad binding type");
4812 assert(targetAtom
!= NULL
);
4813 const Atom
<A
>* target
= dynamic_cast<const Atom
<A
>*>(targetAtom
);
4814 if ( targetAtom
->section().type() == ld::Section::typeCString
) {
4816 *count
= targetAtom
->size();
4818 else if ( targetAtom
->section().type() == ld::Section::typeUTF16Strings
) {
4820 *count
= (targetAtom
->size()+1)/2; // round up incase of buggy compiler that has only one trailing zero byte
4822 assert(target
!= NULL
);
4823 return target
->contentPointer();
4829 template <typename A
>
4830 unsigned long CFStringSection
<A
>::contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
4832 // base hash of CFString on hash of cstring it wraps
4835 unsigned int charCount
;
4836 const uint8_t* content
= this->targetContent(atom
, ind
, &cType
, &charCount
);
4840 for (const char* s
= (char*)content
; *s
!= '\0'; ++s
) {
4841 hash
= hash
* 33 + *s
;
4846 --charCount
; // don't add last 0x0000 to hash because some buggy compilers only have trailing single byte
4847 for (const uint16_t* s
= (uint16_t*)content
; charCount
> 0; ++s
, --charCount
) {
4848 hash
= hash
* 1025 + *s
;
4851 case contentUnknown
:
4858 template <typename A
>
4859 bool CFStringSection
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
4860 const ld::IndirectBindingTable
& indirectBindingTable
) const
4864 if ( rhs
.section().type() != ld::Section::typeCFString
)
4866 assert(this->type() == rhs
.section().type());
4867 assert(strcmp(this->sectionName(), "__cfstring") == 0);
4869 ContentType thisType
;
4870 unsigned int charCount
;
4871 const uint8_t* cstringContent
= this->targetContent(atom
, indirectBindingTable
, &thisType
, &charCount
);
4872 ContentType rhsType
;
4873 const Atom
<A
>* rhsAtom
= dynamic_cast<const Atom
<A
>*>(&rhs
);
4874 assert(rhsAtom
!= NULL
);
4875 unsigned int rhsCharCount
;
4876 const uint8_t* rhsStringContent
= this->targetContent(rhsAtom
, indirectBindingTable
, &rhsType
, &rhsCharCount
);
4878 if ( thisType
!= rhsType
)
4881 // no need to compare content of pointers are already the same
4882 if ( cstringContent
== rhsStringContent
)
4885 // no need to compare content if size is different
4886 if ( charCount
!= rhsCharCount
)
4889 switch ( thisType
) {
4891 return (strcmp((char*)cstringContent
, (char*)rhsStringContent
) == 0);
4894 const uint16_t* cstringContent16
= (uint16_t*)cstringContent
;
4895 const uint16_t* rhsStringContent16
= (uint16_t*)rhsStringContent
;
4896 for (unsigned int i
= 0; i
< charCount
; ++i
) {
4897 if ( cstringContent16
[i
] != rhsStringContent16
[i
] )
4902 case contentUnknown
:
4909 template <typename A
>
4910 typename
A::P::uint_t ObjC1ClassSection
<A
>::elementSizeAtAddress(pint_t addr
)
4912 // nominal size for each class is 48 bytes, but sometimes the compiler
4913 // over aligns and there is padding after class data
4914 const macho_section
<P
>* sct
= this->machoSection();
4915 uint32_t align
= 1 << sct
->align();
4916 uint32_t size
= ((12 * sizeof(pint_t
)) + align
-1) & (-align
);
4920 template <typename A
>
4921 const char* ObjC1ClassSection
<A
>::unlabeledAtomName(Parser
<A
>& parser
, pint_t addr
)
4923 // 8-bytes into class object is pointer to class name
4924 const macho_section
<P
>* sct
= this->machoSection();
4925 uint32_t classObjcFileOffset
= sct
->offset() - sct
->addr() + addr
;
4926 const uint8_t* mappedFileContent
= this->file().fileContent();
4927 pint_t nameAddr
= P::getP(*((pint_t
*)(mappedFileContent
+classObjcFileOffset
+2*sizeof(pint_t
))));
4929 // find section containing string address to get string bytes
4930 const macho_section
<P
>* const sections
= parser
.firstMachOSection();
4931 const uint32_t sectionCount
= parser
.machOSectionCount();
4932 for (uint32_t i
=0; i
< sectionCount
; ++i
) {
4933 const macho_section
<P
>* aSect
= §ions
[i
];
4934 if ( (aSect
->addr() <= nameAddr
) && (nameAddr
< (aSect
->addr()+aSect
->size())) ) {
4935 assert((aSect
->flags() & SECTION_TYPE
) == S_CSTRING_LITERALS
);
4936 uint32_t nameFileOffset
= aSect
->offset() - aSect
->addr() + nameAddr
;
4937 const char* name
= (char*)mappedFileContent
+ nameFileOffset
;
4938 // spin through symbol table to find absolute symbol corresponding to this class
4939 for (uint32_t s
=0; s
< parser
.symbolCount(); ++s
) {
4940 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(s
);
4941 if ( (sym
.n_type() & N_TYPE
) != N_ABS
)
4943 const char* absName
= parser
.nameFromSymbol(sym
);
4944 if ( strncmp(absName
, ".objc_class_name_", 17) == 0 ) {
4945 if ( strcmp(&absName
[17], name
) == 0 )
4949 assert(0 && "obj class name not found in symbol table");
4952 assert(0 && "obj class name not found");
4953 return "unknown objc class";
4957 template <typename A
>
4958 const char* ObjC2ClassRefsSection
<A
>::targetClassName(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
4960 assert(atom
->fixupCount() == 1);
4961 ld::Fixup::iterator fit
= atom
->fixupsBegin();
4962 const char* className
= NULL
;
4963 switch ( fit
->binding
) {
4964 case ld::Fixup::bindingByNameUnbound
:
4965 className
= fit
->u
.name
;
4967 case ld::Fixup::bindingDirectlyBound
:
4968 case ld::Fixup::bindingByContentBound
:
4969 className
= fit
->u
.target
->name();
4971 case ld::Fixup::bindingsIndirectlyBound
:
4972 className
= ind
.indirectName(fit
->u
.bindingIndex
);
4975 assert(0 && "unsupported binding in objc2 class ref section");
4977 assert(className
!= NULL
);
4982 template <typename A
>
4983 unsigned long ObjC2ClassRefsSection
<A
>::contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
4985 unsigned long hash
= 978;
4986 for (const char* s
= targetClassName(atom
, ind
); *s
!= '\0'; ++s
) {
4987 hash
= hash
* 33 + *s
;
4992 template <typename A
>
4993 bool ObjC2ClassRefsSection
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
4994 const ld::IndirectBindingTable
& indirectBindingTable
) const
4996 assert(this->type() == rhs
.section().type());
4997 const Atom
<A
>* rhsAtom
= dynamic_cast<const Atom
<A
>*>(&rhs
);
4998 assert(rhsAtom
!= NULL
);
4999 const char* thisClassName
= targetClassName(atom
, indirectBindingTable
);
5000 const char* rhsClassName
= targetClassName(rhsAtom
, indirectBindingTable
);
5001 return (strcmp(thisClassName
, rhsClassName
) == 0);
5005 template <typename A
>
5006 const char* Objc1ClassReferences
<A
>::targetCString(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
5008 assert(atom
->fixupCount() == 2);
5009 ld::Fixup::iterator fit
= atom
->fixupsBegin();
5010 if ( fit
->kind
== ld::Fixup::kindSetTargetAddress
)
5012 const ld::Atom
* targetAtom
= NULL
;
5013 switch ( fit
->binding
) {
5014 case ld::Fixup::bindingByContentBound
:
5015 targetAtom
= fit
->u
.target
;
5017 case ld::Fixup::bindingsIndirectlyBound
:
5018 targetAtom
= ind
.indirectAtom(fit
->u
.bindingIndex
);
5019 if ( targetAtom
== NULL
) {
5020 fprintf(stderr
, "missing target named %s\n", ind
.indirectName(fit
->u
.bindingIndex
));
5026 assert(targetAtom
!= NULL
);
5027 const Atom
<A
>* target
= dynamic_cast<const Atom
<A
>*>(targetAtom
);
5028 assert(target
!= NULL
);
5029 return (char*)target
->contentPointer();
5033 template <typename A
>
5034 const char* PointerToCStringSection
<A
>::targetCString(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
5036 assert(atom
->fixupCount() == 1);
5037 ld::Fixup::iterator fit
= atom
->fixupsBegin();
5038 const ld::Atom
* targetAtom
= NULL
;
5039 switch ( fit
->binding
) {
5040 case ld::Fixup::bindingByContentBound
:
5041 targetAtom
= fit
->u
.target
;
5043 case ld::Fixup::bindingsIndirectlyBound
:
5044 targetAtom
= ind
.indirectAtom(fit
->u
.bindingIndex
);
5049 assert(targetAtom
!= NULL
);
5050 const Atom
<A
>* target
= dynamic_cast<const Atom
<A
>*>(targetAtom
);
5051 assert(target
!= NULL
);
5052 return (char*)target
->contentPointer();
5055 template <typename A
>
5056 unsigned long PointerToCStringSection
<A
>::contentHash(const class Atom
<A
>* atom
,
5057 const ld::IndirectBindingTable
& indirectBindingTable
) const
5059 // make hash from section name and target cstring name
5060 unsigned long hash
= 123;
5061 for (const char* s
= this->sectionName(); *s
!= '\0'; ++s
) {
5062 hash
= hash
* 33 + *s
;
5064 for (const char* s
= this->targetCString(atom
, indirectBindingTable
); *s
!= '\0'; ++s
) {
5065 hash
= hash
* 33 + *s
;
5070 template <typename A
>
5071 bool PointerToCStringSection
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
5072 const ld::IndirectBindingTable
& indirectBindingTable
) const
5074 assert(this->type() == rhs
.section().type());
5075 // there can be pointers-to-cstrings in different section names
5076 // we only want to coalesce in same section name
5077 if ( *this != rhs
.section() )
5080 // get string content for this
5081 const char* cstringContent
= this->targetCString(atom
, indirectBindingTable
);
5082 const Atom
<A
>* rhsAtom
= dynamic_cast<const Atom
<A
>*>(&rhs
);
5083 assert(rhsAtom
!= NULL
);
5084 const char* rhsCstringContent
= this->targetCString(rhsAtom
, indirectBindingTable
);
5086 assert(cstringContent
!= NULL
);
5087 assert(rhsCstringContent
!= NULL
);
5088 return (strcmp(cstringContent
, rhsCstringContent
) == 0);
5093 template <typename A
>
5094 unsigned long UTF16StringSection
<A
>::contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
5096 unsigned long hash
= 5381;
5097 const uint16_t* stringContent
= (uint16_t*)atom
->contentPointer();
5098 // some buggy compilers end utf16 data with single byte, so don't use last word in hash computation
5099 unsigned int count
= (atom
->size()/2) - 1;
5100 for (const uint16_t* s
= stringContent
; count
> 0; ++s
, --count
) {
5101 hash
= hash
* 33 + *s
;
5106 template <typename A
>
5107 bool UTF16StringSection
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
5108 const ld::IndirectBindingTable
& ind
) const
5110 if ( rhs
.section().type() != ld::Section::typeUTF16Strings
)
5123 uint32_t Section
<x86_64
>::x86_64PcRelOffset(uint8_t r_type
)
5126 case X86_64_RELOC_SIGNED
:
5128 case X86_64_RELOC_SIGNED_1
:
5130 case X86_64_RELOC_SIGNED_2
:
5132 case X86_64_RELOC_SIGNED_4
:
5140 bool Section
<x86_64
>::addRelocFixup(class Parser
<x86_64
>& parser
, const macho_relocation_info
<P
>* reloc
)
5142 const macho_section
<P
>* sect
= this->machoSection();
5143 uint64_t srcAddr
= sect
->addr() + reloc
->r_address();
5144 Parser
<x86_64
>::SourceLocation src
;
5145 Parser
<x86_64
>::TargetDesc target
;
5146 Parser
<x86_64
>::TargetDesc toTarget
;
5147 src
.atom
= this->findAtomByAddress(srcAddr
);
5148 src
.offsetInAtom
= srcAddr
- src
.atom
->_objAddress
;
5149 const uint8_t* fixUpPtr
= file().fileContent() + sect
->offset() + reloc
->r_address();
5150 uint64_t contentValue
= 0;
5151 const macho_relocation_info
<x86_64::P
>* nextReloc
= &reloc
[1];
5152 bool result
= false;
5153 bool useDirectBinding
;
5154 switch ( reloc
->r_length() ) {
5156 contentValue
= *fixUpPtr
;
5159 contentValue
= (int64_t)(int16_t)E::get16(*((uint16_t*)fixUpPtr
));
5162 contentValue
= (int64_t)(int32_t)E::get32(*((uint32_t*)fixUpPtr
));
5165 contentValue
= E::get64(*((uint64_t*)fixUpPtr
));
5170 target
.weakImport
= false;
5172 if ( reloc
->r_extern() ) {
5173 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(reloc
->r_symbolnum());
5174 // use direct reference for local symbols
5175 if ( ((sym
.n_type() & N_TYPE
) == N_SECT
) && (((sym
.n_type() & N_EXT
) == 0) || (parser
.nameFromSymbol(sym
)[0] == 'L')) ) {
5176 parser
.findTargetFromAddressAndSectionNum(sym
.n_value(), sym
.n_sect(), target
);
5177 target
.addend
+= contentValue
;
5180 target
.name
= parser
.nameFromSymbol(sym
);
5181 target
.weakImport
= parser
.weakImportFromSymbol(sym
);
5182 target
.addend
= contentValue
;
5184 // cfstrings should always use direct reference to backing store
5185 if ( (this->type() == ld::Section::typeCFString
) && (src
.offsetInAtom
!= 0) ) {
5186 parser
.findTargetFromAddressAndSectionNum(sym
.n_value(), sym
.n_sect(), target
);
5187 target
.addend
= contentValue
;
5191 if ( reloc
->r_pcrel() )
5192 contentValue
+= srcAddr
+ x86_64PcRelOffset(reloc
->r_type());
5193 parser
.findTargetFromAddressAndSectionNum(contentValue
, reloc
->r_symbolnum(), target
);
5195 switch ( reloc
->r_type() ) {
5196 case X86_64_RELOC_UNSIGNED
:
5197 if ( reloc
->r_pcrel() )
5198 throw "pcrel and X86_64_RELOC_UNSIGNED not supported";
5199 switch ( reloc
->r_length() ) {
5202 throw "length < 2 and X86_64_RELOC_UNSIGNED not supported";
5204 parser
.addFixups(src
, ld::Fixup::kindStoreLittleEndian32
, target
);
5207 parser
.addFixups(src
, ld::Fixup::kindStoreLittleEndian64
, target
);
5211 case X86_64_RELOC_SIGNED
:
5212 case X86_64_RELOC_SIGNED_1
:
5213 case X86_64_RELOC_SIGNED_2
:
5214 case X86_64_RELOC_SIGNED_4
:
5215 if ( ! reloc
->r_pcrel() )
5216 throw "not pcrel and X86_64_RELOC_SIGNED* not supported";
5217 if ( reloc
->r_length() != 2 )
5218 throw "length != 2 and X86_64_RELOC_SIGNED* not supported";
5219 switch ( reloc
->r_type() ) {
5220 case X86_64_RELOC_SIGNED
:
5221 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel32
, target
);
5223 case X86_64_RELOC_SIGNED_1
:
5224 if ( reloc
->r_extern() )
5226 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel32_1
, target
);
5228 case X86_64_RELOC_SIGNED_2
:
5229 if ( reloc
->r_extern() )
5231 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel32_2
, target
);
5233 case X86_64_RELOC_SIGNED_4
:
5234 if ( reloc
->r_extern() )
5236 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel32_4
, target
);
5240 case X86_64_RELOC_BRANCH
:
5241 if ( ! reloc
->r_pcrel() )
5242 throw "not pcrel and X86_64_RELOC_BRANCH not supported";
5243 switch ( reloc
->r_length() ) {
5245 if ( (target
.name
!= NULL
) && (strncmp(target
.name
, "___dtrace_probe$", 16) == 0) ) {
5246 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindStoreX86DtraceCallSiteNop
, false, target
.name
);
5247 parser
.addDtraceExtraInfos(src
, &target
.name
[16]);
5249 else if ( (target
.name
!= NULL
) && (strncmp(target
.name
, "___dtrace_isenabled$", 20) == 0) ) {
5250 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindStoreX86DtraceIsEnableSiteClear
, false, target
.name
);
5251 parser
.addDtraceExtraInfos(src
, &target
.name
[20]);
5254 parser
.addFixups(src
, ld::Fixup::kindStoreX86BranchPCRel32
, target
);
5258 parser
.addFixups(src
, ld::Fixup::kindStoreX86BranchPCRel8
, target
);
5261 throwf("length=%d and X86_64_RELOC_BRANCH not supported", reloc
->r_length());
5264 case X86_64_RELOC_GOT
:
5265 if ( ! reloc
->r_extern() )
5266 throw "not extern and X86_64_RELOC_GOT not supported";
5267 if ( ! reloc
->r_pcrel() )
5268 throw "not pcrel and X86_64_RELOC_GOT not supported";
5269 if ( reloc
->r_length() != 2 )
5270 throw "length != 2 and X86_64_RELOC_GOT not supported";
5271 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel32GOT
, target
);
5273 case X86_64_RELOC_GOT_LOAD
:
5274 if ( ! reloc
->r_extern() )
5275 throw "not extern and X86_64_RELOC_GOT_LOAD not supported";
5276 if ( ! reloc
->r_pcrel() )
5277 throw "not pcrel and X86_64_RELOC_GOT_LOAD not supported";
5278 if ( reloc
->r_length() != 2 )
5279 throw "length != 2 and X86_64_RELOC_GOT_LOAD not supported";
5280 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel32GOTLoad
, target
);
5282 case X86_64_RELOC_SUBTRACTOR
:
5283 if ( reloc
->r_pcrel() )
5284 throw "X86_64_RELOC_SUBTRACTOR cannot be pc-relative";
5285 if ( reloc
->r_length() < 2 )
5286 throw "X86_64_RELOC_SUBTRACTOR must have r_length of 2 or 3";
5287 if ( !reloc
->r_extern() )
5288 throw "X86_64_RELOC_SUBTRACTOR must have r_extern=1";
5289 if ( nextReloc
->r_type() != X86_64_RELOC_UNSIGNED
)
5290 throw "X86_64_RELOC_SUBTRACTOR must be followed by X86_64_RELOC_UNSIGNED";
5292 if ( nextReloc
->r_pcrel() )
5293 throw "X86_64_RELOC_UNSIGNED following a X86_64_RELOC_SUBTRACTOR cannot be pc-relative";
5294 if ( nextReloc
->r_length() != reloc
->r_length() )
5295 throw "X86_64_RELOC_UNSIGNED following a X86_64_RELOC_SUBTRACTOR must have same r_length";
5296 if ( nextReloc
->r_extern() ) {
5297 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(nextReloc
->r_symbolnum());
5298 // use direct reference for local symbols
5299 if ( ((sym
.n_type() & N_TYPE
) == N_SECT
) && (((sym
.n_type() & N_EXT
) == 0) || (parser
.nameFromSymbol(sym
)[0] == 'L')) ) {
5300 parser
.findTargetFromAddressAndSectionNum(sym
.n_value(), sym
.n_sect(), toTarget
);
5301 toTarget
.addend
= contentValue
;
5302 useDirectBinding
= true;
5305 toTarget
.name
= parser
.nameFromSymbol(sym
);
5306 toTarget
.weakImport
= parser
.weakImportFromSymbol(sym
);
5307 toTarget
.addend
= contentValue
;
5308 useDirectBinding
= false;
5312 parser
.findTargetFromAddressAndSectionNum(contentValue
, nextReloc
->r_symbolnum(), toTarget
);
5313 useDirectBinding
= (toTarget
.atom
->scope() == ld::Atom::scopeTranslationUnit
);
5315 if ( useDirectBinding
)
5316 parser
.addFixup(src
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, toTarget
.atom
);
5318 parser
.addFixup(src
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, toTarget
.weakImport
, toTarget
.name
);
5319 parser
.addFixup(src
, ld::Fixup::k2of4
, ld::Fixup::kindAddAddend
, toTarget
.addend
);
5320 if ( target
.atom
== NULL
)
5321 parser
.addFixup(src
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractTargetAddress
, false, target
.name
);
5323 parser
.addFixup(src
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractTargetAddress
, target
.atom
);
5324 if ( reloc
->r_length() == 2 )
5325 parser
.addFixup(src
, ld::Fixup::k4of4
, ld::Fixup::kindStoreLittleEndian32
);
5327 parser
.addFixup(src
, ld::Fixup::k4of4
, ld::Fixup::kindStoreLittleEndian64
);
5329 case X86_64_RELOC_TLV
:
5330 if ( ! reloc
->r_extern() )
5331 throw "not extern and X86_64_RELOC_TLV not supported";
5332 if ( ! reloc
->r_pcrel() )
5333 throw "not pcrel and X86_64_RELOC_TLV not supported";
5334 if ( reloc
->r_length() != 2 )
5335 throw "length != 2 and X86_64_RELOC_TLV not supported";
5336 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel32TLVLoad
, target
);
5339 throwf("unknown relocation type %d", reloc
->r_type());
5347 bool Section
<x86
>::addRelocFixup(class Parser
<x86
>& parser
, const macho_relocation_info
<P
>* reloc
)
5349 const macho_section
<P
>* sect
= this->machoSection();
5351 const uint8_t* fixUpPtr
;
5352 uint32_t contentValue
= 0;
5353 ld::Fixup::Kind kind
= ld::Fixup::kindNone
;
5354 Parser
<x86
>::SourceLocation src
;
5355 Parser
<x86
>::TargetDesc target
;
5357 if ( (reloc
->r_address() & R_SCATTERED
) == 0 ) {
5358 srcAddr
= sect
->addr() + reloc
->r_address();
5359 src
.atom
= this->findAtomByAddress(srcAddr
);
5360 src
.offsetInAtom
= srcAddr
- src
.atom
->_objAddress
;
5361 fixUpPtr
= file().fileContent() + sect
->offset() + reloc
->r_address();
5362 switch ( reloc
->r_type() ) {
5363 case GENERIC_RELOC_VANILLA
:
5364 switch ( reloc
->r_length() ) {
5366 contentValue
= (int32_t)(int8_t)*fixUpPtr
;
5367 if ( reloc
->r_pcrel() ) {
5368 kind
= ld::Fixup::kindStoreX86BranchPCRel8
;
5369 contentValue
+= srcAddr
+ sizeof(uint8_t);
5372 throw "r_length=0 and r_pcrel=0 not supported";
5375 contentValue
= (int32_t)(int16_t)E::get16(*((uint16_t*)fixUpPtr
));
5376 if ( reloc
->r_pcrel() ) {
5377 kind
= ld::Fixup::kindStoreX86PCRel16
;
5378 contentValue
+= srcAddr
+ sizeof(uint16_t);
5381 kind
= ld::Fixup::kindStoreLittleEndian16
;
5384 contentValue
= E::get32(*((uint32_t*)fixUpPtr
));
5385 if ( reloc
->r_pcrel() ) {
5386 kind
= ld::Fixup::kindStoreX86BranchPCRel32
;
5387 contentValue
+= srcAddr
+ sizeof(uint32_t);
5390 kind
= ld::Fixup::kindStoreLittleEndian32
;
5393 throw "r_length=3 not supported";
5395 if ( reloc
->r_extern() ) {
5397 const macho_nlist
<P
>& targetSymbol
= parser
.symbolFromIndex(reloc
->r_symbolnum());
5398 target
.name
= parser
.nameFromSymbol(targetSymbol
);
5399 target
.weakImport
= parser
.weakImportFromSymbol(targetSymbol
);
5400 target
.addend
= (int32_t)contentValue
;
5403 parser
.findTargetFromAddressAndSectionNum(contentValue
, reloc
->r_symbolnum(), target
);
5405 if ( (kind
== ld::Fixup::kindStoreX86BranchPCRel32
) && (target
.name
!= NULL
) ) {
5406 if ( strncmp(target
.name
, "___dtrace_probe$", 16) == 0 ) {
5407 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindStoreX86DtraceCallSiteNop
, false, target
.name
);
5408 parser
.addDtraceExtraInfos(src
, &target
.name
[16]);
5411 else if ( strncmp(target
.name
, "___dtrace_isenabled$", 20) == 0 ) {
5412 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindStoreX86DtraceIsEnableSiteClear
, false, target
.name
);
5413 parser
.addDtraceExtraInfos(src
, &target
.name
[20]);
5417 parser
.addFixups(src
, kind
, target
);
5420 case GENERIC_RLEOC_TLV
:
5422 if ( !reloc
->r_extern() )
5423 throw "r_extern=0 and r_type=GENERIC_RLEOC_TLV not supported";
5424 if ( reloc
->r_length() != 2 )
5425 throw "r_length!=2 and r_type=GENERIC_RLEOC_TLV not supported";
5426 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(reloc
->r_symbolnum());
5427 // use direct reference for local symbols
5428 if ( ((sym
.n_type() & N_TYPE
) == N_SECT
) && ((sym
.n_type() & N_EXT
) == 0) ) {
5429 parser
.findTargetFromAddressAndSectionNum(sym
.n_value(), sym
.n_sect(), target
);
5433 target
.name
= parser
.nameFromSymbol(sym
);
5434 target
.weakImport
= parser
.weakImportFromSymbol(sym
);
5436 target
.addend
= (int64_t)(int32_t)E::get32(*((uint32_t*)fixUpPtr
));
5437 if ( reloc
->r_pcrel() ) {
5438 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel32TLVLoad
, target
);
5441 parser
.addFixups(src
, ld::Fixup::kindStoreX86Abs32TLVLoad
, target
);
5447 throwf("unsupported i386 relocation type (%d)", reloc
->r_type());
5451 // scattered relocation
5452 const macho_scattered_relocation_info
<P
>* sreloc
= (macho_scattered_relocation_info
<P
>*)reloc
;
5453 srcAddr
= sect
->addr() + sreloc
->r_address();
5454 src
.atom
= this->findAtomByAddress(srcAddr
);
5455 assert(src
.atom
!= NULL
);
5456 src
.offsetInAtom
= srcAddr
- src
.atom
->_objAddress
;
5457 fixUpPtr
= file().fileContent() + sect
->offset() + sreloc
->r_address();
5458 uint32_t relocValue
= sreloc
->r_value();
5459 bool result
= false;
5460 // file format allows pair to be scattered or not
5461 const macho_scattered_relocation_info
<P
>* nextSReloc
= &sreloc
[1];
5462 const macho_relocation_info
<P
>* nextReloc
= &reloc
[1];
5463 bool nextRelocIsPair
= false;
5464 uint32_t nextRelocAddress
= 0;
5465 uint32_t nextRelocValue
= 0;
5466 if ( (nextReloc
->r_address() & R_SCATTERED
) == 0 ) {
5467 if ( nextReloc
->r_type() == GENERIC_RELOC_PAIR
) {
5468 nextRelocIsPair
= true;
5469 nextRelocAddress
= nextReloc
->r_address();
5470 result
= true; // iterator should skip next reloc, since we've consumed it here
5474 if ( nextSReloc
->r_type() == GENERIC_RELOC_PAIR
) {
5475 nextRelocIsPair
= true;
5476 nextRelocAddress
= nextSReloc
->r_address();
5477 nextRelocValue
= nextSReloc
->r_value();
5480 switch (sreloc
->r_type()) {
5481 case GENERIC_RELOC_VANILLA
:
5482 // with a scattered relocation we get both the target (sreloc->r_value()) and the target+offset (*fixUpPtr)
5483 target
.atom
= parser
.findAtomByAddress(relocValue
);
5484 if ( sreloc
->r_pcrel() ) {
5485 switch ( sreloc
->r_length() ) {
5487 contentValue
= srcAddr
+ 1 + *fixUpPtr
;
5488 target
.addend
= (int32_t)contentValue
- (int32_t)relocValue
;
5489 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel8
, target
);
5492 contentValue
= srcAddr
+ 2 + LittleEndian::get16(*((uint16_t*)fixUpPtr
));
5493 target
.addend
= (int32_t)contentValue
- (int32_t)relocValue
;
5494 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel16
, target
);
5497 contentValue
= srcAddr
+ 4 + LittleEndian::get32(*((uint32_t*)fixUpPtr
));
5498 target
.addend
= (int32_t)contentValue
- (int32_t)relocValue
;
5499 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel32
, target
);
5502 throw "unsupported r_length=3 for scattered pc-rel vanilla reloc";
5507 if ( sreloc
->r_length() != 2 )
5508 throwf("unsupported r_length=%d for scattered vanilla reloc", sreloc
->r_length());
5509 contentValue
= LittleEndian::get32(*((uint32_t*)fixUpPtr
));
5510 target
.addend
= (int32_t)contentValue
- (int32_t)(target
.atom
->objectAddress());
5511 parser
.addFixups(src
, ld::Fixup::kindStoreLittleEndian32
, target
);
5514 case GENERIC_RELOC_SECTDIFF
:
5515 case GENERIC_RELOC_LOCAL_SECTDIFF
:
5517 if ( !nextRelocIsPair
)
5518 throw "GENERIC_RELOC_SECTDIFF missing following pair";
5519 switch ( sreloc
->r_length() ) {
5522 throw "bad length for GENERIC_RELOC_SECTDIFF";
5524 contentValue
= (int32_t)(int16_t)LittleEndian::get16(*((uint16_t*)fixUpPtr
));
5525 kind
= ld::Fixup::kindStoreLittleEndian16
;
5528 contentValue
= LittleEndian::get32(*((uint32_t*)fixUpPtr
));
5529 kind
= ld::Fixup::kindStoreLittleEndian32
;
5532 Atom
<x86
>* fromAtom
= parser
.findAtomByAddress(nextRelocValue
);
5533 uint32_t offsetInFrom
= nextRelocValue
- fromAtom
->_objAddress
;
5534 parser
.findTargetFromAddress(sreloc
->r_value(), target
);
5535 // check for addend encoded in the section content
5536 int64_t addend
= (int32_t)contentValue
- (int32_t)(sreloc
->r_value() - nextRelocValue
);
5538 // switch binding base on coalescing
5539 if ( target
.atom
== NULL
) {
5540 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, false, target
.name
);
5542 else if ( target
.atom
->scope() == ld::Atom::scopeTranslationUnit
) {
5543 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, target
.atom
);
5545 else if ( (target
.atom
->combine() == ld::Atom::combineByNameAndContent
) || (target
.atom
->combine() == ld::Atom::combineByNameAndReferences
) ) {
5546 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, target
.atom
);
5549 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, false, target
.atom
->name());
5551 parser
.addFixup(src
, ld::Fixup::k2of5
, ld::Fixup::kindAddAddend
, target
.addend
);
5552 parser
.addFixup(src
, ld::Fixup::k3of5
, ld::Fixup::kindSubtractTargetAddress
, fromAtom
);
5553 parser
.addFixup(src
, ld::Fixup::k4of5
, ld::Fixup::kindSubtractAddend
, offsetInFrom
-addend
);
5554 parser
.addFixup(src
, ld::Fixup::k5of5
, kind
);
5557 // switch binding base on coalescing
5558 if ( target
.atom
== NULL
) {
5559 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, false, target
.name
);
5561 else if ( target
.atom
->scope() == ld::Atom::scopeTranslationUnit
) {
5562 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, target
.atom
);
5564 else if ( (target
.atom
->combine() == ld::Atom::combineByNameAndContent
) || (target
.atom
->combine() == ld::Atom::combineByNameAndReferences
) ) {
5565 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, target
.atom
);
5568 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, false, target
.atom
->name());
5570 parser
.addFixup(src
, ld::Fixup::k2of5
, ld::Fixup::kindAddAddend
, target
.addend
+addend
);
5571 parser
.addFixup(src
, ld::Fixup::k3of5
, ld::Fixup::kindSubtractTargetAddress
, fromAtom
);
5572 parser
.addFixup(src
, ld::Fixup::k4of5
, ld::Fixup::kindSubtractAddend
, offsetInFrom
);
5573 parser
.addFixup(src
, ld::Fixup::k5of5
, kind
);
5586 #if SUPPORT_ARCH_arm_any
5588 bool Section
<arm
>::addRelocFixup(class Parser
<arm
>& parser
, const macho_relocation_info
<P
>* reloc
)
5590 const macho_section
<P
>* sect
= this->machoSection();
5591 bool result
= false;
5595 int32_t displacement
= 0;
5596 uint32_t instruction
= 0;
5597 pint_t contentValue
= 0;
5598 Parser
<arm
>::SourceLocation src
;
5599 Parser
<arm
>::TargetDesc target
;
5600 const macho_relocation_info
<P
>* nextReloc
;
5602 if ( (reloc
->r_address() & R_SCATTERED
) == 0 ) {
5603 bool externSymbolIsThumbDef
= false;
5604 srcAddr
= sect
->addr() + reloc
->r_address();
5605 src
.atom
= this->findAtomByAddress(srcAddr
);
5606 src
.offsetInAtom
= srcAddr
- src
.atom
->_objAddress
;
5607 fixUpPtr
= (uint32_t*)(file().fileContent() + sect
->offset() + reloc
->r_address());
5608 if ( reloc
->r_type() != ARM_RELOC_PAIR
)
5609 instruction
= LittleEndian::get32(*fixUpPtr
);
5610 if ( reloc
->r_extern() ) {
5611 const macho_nlist
<P
>& targetSymbol
= parser
.symbolFromIndex(reloc
->r_symbolnum());
5612 // use direct reference for local symbols
5613 if ( ((targetSymbol
.n_type() & N_TYPE
) == N_SECT
) && (((targetSymbol
.n_type() & N_EXT
) == 0) || (parser
.nameFromSymbol(targetSymbol
)[0] == 'L')) ) {
5614 parser
.findTargetFromAddressAndSectionNum(targetSymbol
.n_value(), targetSymbol
.n_sect(), target
);
5618 target
.name
= parser
.nameFromSymbol(targetSymbol
);
5619 target
.weakImport
= parser
.weakImportFromSymbol(targetSymbol
);
5620 if ( ((targetSymbol
.n_type() & N_TYPE
) == N_SECT
) && (targetSymbol
.n_desc() & N_ARM_THUMB_DEF
) )
5621 externSymbolIsThumbDef
= true;
5624 switch ( reloc
->r_type() ) {
5625 case ARM_RELOC_BR24
:
5626 // Sign-extend displacement
5627 displacement
= (instruction
& 0x00FFFFFF) << 2;
5628 if ( (displacement
& 0x02000000) != 0 )
5629 displacement
|= 0xFC000000;
5630 // The pc added will be +8 from the pc
5632 // If this is BLX add H << 1
5633 if ((instruction
& 0xFE000000) == 0xFA000000)
5634 displacement
+= ((instruction
& 0x01000000) >> 23);
5635 if ( reloc
->r_extern() ) {
5636 target
.addend
= srcAddr
+ displacement
;
5637 if ( externSymbolIsThumbDef
)
5638 target
.addend
&= -2; // remove thumb bit
5641 dstAddr
= srcAddr
+ displacement
;
5642 parser
.findTargetFromAddressAndSectionNum(dstAddr
, reloc
->r_symbolnum(), target
);
5644 // special case "calls" for dtrace
5645 if ( (target
.name
!= NULL
) && (strncmp(target
.name
, "___dtrace_probe$", 16) == 0) ) {
5646 parser
.addFixup(src
, ld::Fixup::k1of1
,
5647 ld::Fixup::kindStoreARMDtraceCallSiteNop
, false, target
.name
);
5648 parser
.addDtraceExtraInfos(src
, &target
.name
[16]);
5650 else if ( (target
.name
!= NULL
) && (strncmp(target
.name
, "___dtrace_isenabled$", 20) == 0) ) {
5651 parser
.addFixup(src
, ld::Fixup::k1of1
,
5652 ld::Fixup::kindStoreARMDtraceIsEnableSiteClear
, false, target
.name
);
5653 parser
.addDtraceExtraInfos(src
, &target
.name
[20]);
5656 parser
.addFixups(src
, ld::Fixup::kindStoreARMBranch24
, target
);
5659 case ARM_THUMB_RELOC_BR22
:
5660 // thumb2 added two more bits to displacement, complicating the displacement decoding
5662 uint32_t s
= (instruction
>> 10) & 0x1;
5663 uint32_t j1
= (instruction
>> 29) & 0x1;
5664 uint32_t j2
= (instruction
>> 27) & 0x1;
5665 uint32_t imm10
= instruction
& 0x3FF;
5666 uint32_t imm11
= (instruction
>> 16) & 0x7FF;
5667 uint32_t i1
= (j1
== s
);
5668 uint32_t i2
= (j2
== s
);
5669 uint32_t dis
= (s
<< 24) | (i1
<< 23) | (i2
<< 22) | (imm10
<< 12) | (imm11
<< 1);
5673 displacement
= sdis
;
5675 // The pc added will be +4 from the pc
5677 // If the instruction was blx, force the low 2 bits to be clear
5678 dstAddr
= srcAddr
+ displacement
;
5679 if ((instruction
& 0xF8000000) == 0xE8000000)
5680 dstAddr
&= 0xFFFFFFFC;
5682 if ( reloc
->r_extern() ) {
5683 target
.addend
= dstAddr
;
5686 parser
.findTargetFromAddressAndSectionNum(dstAddr
, reloc
->r_symbolnum(), target
);
5688 // special case "calls" for dtrace
5689 if ( (target
.name
!= NULL
) && (strncmp(target
.name
, "___dtrace_probe$", 16) == 0) ) {
5690 parser
.addFixup(src
, ld::Fixup::k1of1
,
5691 ld::Fixup::kindStoreThumbDtraceCallSiteNop
, false, target
.name
);
5692 parser
.addDtraceExtraInfos(src
, &target
.name
[16]);
5694 else if ( (target
.name
!= NULL
) && (strncmp(target
.name
, "___dtrace_isenabled$", 20) == 0) ) {
5695 parser
.addFixup(src
, ld::Fixup::k1of1
,
5696 ld::Fixup::kindStoreThumbDtraceIsEnableSiteClear
, false, target
.name
);
5697 parser
.addDtraceExtraInfos(src
, &target
.name
[20]);
5700 parser
.addFixups(src
, ld::Fixup::kindStoreThumbBranch22
, target
);
5703 case ARM_RELOC_VANILLA
:
5704 if ( reloc
->r_length() != 2 )
5705 throw "bad length for ARM_RELOC_VANILLA";
5706 contentValue
= LittleEndian::get32(*fixUpPtr
);
5707 if ( reloc
->r_extern() ) {
5708 target
.addend
= (int32_t)contentValue
;
5709 if ( externSymbolIsThumbDef
)
5710 target
.addend
&= -2; // remove thumb bit
5713 parser
.findTargetFromAddressAndSectionNum(contentValue
, reloc
->r_symbolnum(), target
);
5714 // possible non-extern relocation turned into by-name ref because target is a weak-def
5715 if ( target
.atom
!= NULL
) {
5716 if ( target
.atom
->isThumb() )
5717 target
.addend
&= -2; // remove thumb bit
5718 // if reference to LSDA, add group subordinate fixup
5719 if ( target
.atom
->contentType() == ld::Atom::typeLSDA
) {
5720 Parser
<arm
>::SourceLocation src2
;
5721 src2
.atom
= src
.atom
;
5722 src2
.offsetInAtom
= 0;
5723 parser
.addFixup(src2
, ld::Fixup::k1of1
, ld::Fixup::kindNoneGroupSubordinateLSDA
, target
.atom
);
5727 parser
.addFixups(src
, ld::Fixup::kindStoreLittleEndian32
, target
);
5729 case ARM_THUMB_32BIT_BRANCH
:
5730 // silently ignore old unnecessary reloc
5732 case ARM_RELOC_HALF
:
5733 nextReloc
= &reloc
[1];
5734 if ( nextReloc
->r_type() == ARM_RELOC_PAIR
) {
5735 uint32_t instruction16
;
5736 uint32_t other16
= (nextReloc
->r_address() & 0xFFFF);
5738 if ( reloc
->r_length() & 2 ) {
5740 uint32_t i
= ((instruction
& 0x00000400) >> 10);
5741 uint32_t imm4
= (instruction
& 0x0000000F);
5742 uint32_t imm3
= ((instruction
& 0x70000000) >> 28);
5743 uint32_t imm8
= ((instruction
& 0x00FF0000) >> 16);
5744 instruction16
= (imm4
<< 12) | (i
<< 11) | (imm3
<< 8) | imm8
;
5748 uint32_t imm4
= ((instruction
& 0x000F0000) >> 16);
5749 uint32_t imm12
= (instruction
& 0x00000FFF);
5750 instruction16
= (imm4
<< 12) | imm12
;
5752 if ( reloc
->r_length() & 1 ) {
5754 dstAddr
= ((instruction16
<< 16) | other16
);
5755 if ( reloc
->r_extern() ) {
5756 target
.addend
= dstAddr
;
5757 if ( externSymbolIsThumbDef
)
5758 target
.addend
&= -2; // remove thumb bit
5761 parser
.findTargetFromAddress(dstAddr
, target
);
5762 if ( target
.atom
->isThumb() )
5763 target
.addend
&= (-2); // remove thumb bit
5765 parser
.addFixups(src
, (isThumb
? ld::Fixup::kindStoreThumbHigh16
: ld::Fixup::kindStoreARMHigh16
), target
);
5769 dstAddr
= (other16
<< 16) | instruction16
;
5770 if ( reloc
->r_extern() ) {
5771 target
.addend
= dstAddr
;
5772 if ( externSymbolIsThumbDef
)
5773 target
.addend
&= -2; // remove thumb bit
5776 parser
.findTargetFromAddress(dstAddr
, target
);
5777 if ( target
.atom
->isThumb() )
5778 target
.addend
&= (-2); // remove thumb bit
5780 parser
.addFixups(src
, (isThumb
? ld::Fixup::kindStoreThumbLow16
: ld::Fixup::kindStoreARMLow16
), target
);
5785 throw "for ARM_RELOC_HALF, next reloc is not ARM_RELOC_PAIR";
5788 throwf("unknown relocation type %d", reloc
->r_type());
5793 const macho_scattered_relocation_info
<P
>* sreloc
= (macho_scattered_relocation_info
<P
>*)reloc
;
5794 // file format allows pair to be scattered or not
5795 const macho_scattered_relocation_info
<P
>* nextSReloc
= &sreloc
[1];
5796 nextReloc
= &reloc
[1];
5797 srcAddr
= sect
->addr() + sreloc
->r_address();
5798 dstAddr
= sreloc
->r_value();
5799 fixUpPtr
= (uint32_t*)(file().fileContent() + sect
->offset() + sreloc
->r_address());
5800 instruction
= LittleEndian::get32(*fixUpPtr
);
5801 src
.atom
= this->findAtomByAddress(srcAddr
);
5802 src
.offsetInAtom
= srcAddr
- src
.atom
->_objAddress
;
5803 bool nextRelocIsPair
= false;
5804 uint32_t nextRelocAddress
= 0;
5805 uint32_t nextRelocValue
= 0;
5806 if ( (nextReloc
->r_address() & R_SCATTERED
) == 0 ) {
5807 if ( nextReloc
->r_type() == ARM_RELOC_PAIR
) {
5808 nextRelocIsPair
= true;
5809 nextRelocAddress
= nextReloc
->r_address();
5814 if ( nextSReloc
->r_type() == ARM_RELOC_PAIR
) {
5815 nextRelocIsPair
= true;
5816 nextRelocAddress
= nextSReloc
->r_address();
5817 nextRelocValue
= nextSReloc
->r_value();
5821 switch ( sreloc
->r_type() ) {
5822 case ARM_RELOC_VANILLA
:
5823 // with a scattered relocation we get both the target (sreloc->r_value()) and the target+offset (*fixUpPtr)
5824 if ( sreloc
->r_length() != 2 )
5825 throw "bad length for ARM_RELOC_VANILLA";
5826 target
.atom
= parser
.findAtomByAddress(sreloc
->r_value());
5827 contentValue
= LittleEndian::get32(*fixUpPtr
);
5828 target
.addend
= contentValue
- target
.atom
->_objAddress
;
5829 if ( target
.atom
->isThumb() )
5830 target
.addend
&= -2; // remove thumb bit
5831 parser
.addFixups(src
, ld::Fixup::kindStoreLittleEndian32
, target
);
5833 case ARM_RELOC_BR24
:
5834 // Sign-extend displacement
5835 displacement
= (instruction
& 0x00FFFFFF) << 2;
5836 if ( (displacement
& 0x02000000) != 0 )
5837 displacement
|= 0xFC000000;
5838 // The pc added will be +8 from the pc
5840 // If this is BLX add H << 1
5841 if ((instruction
& 0xFE000000) == 0xFA000000)
5842 displacement
+= ((instruction
& 0x01000000) >> 23);
5843 target
.atom
= parser
.findAtomByAddress(sreloc
->r_value());
5844 target
.addend
= (int64_t)(srcAddr
+ displacement
) - (int64_t)(target
.atom
->_objAddress
);
5845 parser
.addFixups(src
, ld::Fixup::kindStoreARMBranch24
, target
);
5847 case ARM_THUMB_RELOC_BR22
:
5848 // thumb2 added two more bits to displacement, complicating the displacement decoding
5850 uint32_t s
= (instruction
>> 10) & 0x1;
5851 uint32_t j1
= (instruction
>> 29) & 0x1;
5852 uint32_t j2
= (instruction
>> 27) & 0x1;
5853 uint32_t imm10
= instruction
& 0x3FF;
5854 uint32_t imm11
= (instruction
>> 16) & 0x7FF;
5855 uint32_t i1
= (j1
== s
);
5856 uint32_t i2
= (j2
== s
);
5857 uint32_t dis
= (s
<< 24) | (i1
<< 23) | (i2
<< 22) | (imm10
<< 12) | (imm11
<< 1);
5861 displacement
= sdis
;
5863 // The pc added will be +4 from the pc
5865 dstAddr
= srcAddr
+displacement
;
5866 // If the instruction was blx, force the low 2 bits to be clear
5867 if ((instruction
& 0xF8000000) == 0xE8000000)
5868 dstAddr
&= 0xFFFFFFFC;
5869 target
.atom
= parser
.findAtomByAddress(sreloc
->r_value());
5870 target
.addend
= dstAddr
- target
.atom
->_objAddress
;
5871 parser
.addFixups(src
, ld::Fixup::kindStoreThumbBranch22
, target
);
5873 case ARM_RELOC_SECTDIFF
:
5874 case ARM_RELOC_LOCAL_SECTDIFF
:
5876 if ( ! nextRelocIsPair
)
5877 throw "ARM_RELOC_SECTDIFF missing following pair";
5878 if ( sreloc
->r_length() != 2 )
5879 throw "bad length for ARM_RELOC_SECTDIFF";
5880 contentValue
= LittleEndian::get32(*fixUpPtr
);
5881 Atom
<arm
>* fromAtom
= parser
.findAtomByAddress(nextRelocValue
);
5882 uint32_t offsetInFrom
= nextRelocValue
- fromAtom
->_objAddress
;
5883 uint32_t offsetInTarget
;
5884 Atom
<arm
>* targetAtom
= parser
.findAtomByAddressOrLocalTargetOfStub(sreloc
->r_value(), &offsetInTarget
);
5885 // check for addend encoded in the section content
5886 int64_t addend
= (int32_t)contentValue
- (int32_t)(sreloc
->r_value() - nextRelocValue
);
5887 if ( targetAtom
->isThumb() )
5888 addend
&= -2; // remove thumb bit
5889 // if reference to LSDA, add group subordinate fixup
5890 if ( targetAtom
->contentType() == ld::Atom::typeLSDA
) {
5891 Parser
<arm
>::SourceLocation src2
;
5892 src2
.atom
= src
.atom
;
5893 src2
.offsetInAtom
= 0;
5894 parser
.addFixup(src2
, ld::Fixup::k1of1
, ld::Fixup::kindNoneGroupSubordinateLSDA
, targetAtom
);
5897 // switch binding base on coalescing
5898 if ( targetAtom
->scope() == ld::Atom::scopeTranslationUnit
) {
5899 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, targetAtom
);
5901 else if ( (targetAtom
->combine() == ld::Atom::combineByNameAndContent
) || (targetAtom
->combine() == ld::Atom::combineByNameAndReferences
) ) {
5902 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, targetAtom
);
5905 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, false, targetAtom
->name());
5907 parser
.addFixup(src
, ld::Fixup::k2of5
, ld::Fixup::kindAddAddend
, offsetInTarget
);
5908 parser
.addFixup(src
, ld::Fixup::k3of5
, ld::Fixup::kindSubtractTargetAddress
, fromAtom
);
5909 parser
.addFixup(src
, ld::Fixup::k4of5
, ld::Fixup::kindSubtractAddend
, offsetInFrom
-addend
);
5910 parser
.addFixup(src
, ld::Fixup::k5of5
, ld::Fixup::kindStoreLittleEndian32
);
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
, (uint32_t)(offsetInTarget
+addend
));
5923 parser
.addFixup(src
, ld::Fixup::k3of5
, ld::Fixup::kindSubtractTargetAddress
, fromAtom
);
5924 parser
.addFixup(src
, ld::Fixup::k4of5
, ld::Fixup::kindSubtractAddend
, offsetInFrom
);
5925 parser
.addFixup(src
, ld::Fixup::k5of5
, ld::Fixup::kindStoreLittleEndian32
);
5929 case ARM_RELOC_HALF_SECTDIFF
:
5930 if ( nextRelocIsPair
) {
5931 instruction
= LittleEndian::get32(*fixUpPtr
);
5932 Atom
<arm
>* fromAtom
= parser
.findAtomByAddress(nextRelocValue
);
5933 uint32_t offsetInFrom
= nextRelocValue
- fromAtom
->_objAddress
;
5934 Atom
<arm
>* targetAtom
= parser
.findAtomByAddress(sreloc
->r_value());
5935 uint32_t offsetInTarget
= sreloc
->r_value() - targetAtom
->_objAddress
;
5936 uint32_t instruction16
;
5937 uint32_t other16
= (nextRelocAddress
& 0xFFFF);
5939 if ( sreloc
->r_length() & 2 ) {
5941 uint32_t i
= ((instruction
& 0x00000400) >> 10);
5942 uint32_t imm4
= (instruction
& 0x0000000F);
5943 uint32_t imm3
= ((instruction
& 0x70000000) >> 28);
5944 uint32_t imm8
= ((instruction
& 0x00FF0000) >> 16);
5945 instruction16
= (imm4
<< 12) | (i
<< 11) | (imm3
<< 8) | imm8
;
5949 uint32_t imm4
= ((instruction
& 0x000F0000) >> 16);
5950 uint32_t imm12
= (instruction
& 0x00000FFF);
5951 instruction16
= (imm4
<< 12) | imm12
;
5953 if ( sreloc
->r_length() & 1 )
5954 dstAddr
= ((instruction16
<< 16) | other16
);
5956 dstAddr
= (other16
<< 16) | instruction16
;
5957 if ( targetAtom
->isThumb() )
5958 dstAddr
&= (-2); // remove thumb bit
5959 int32_t addend
= dstAddr
- (sreloc
->r_value() - nextRelocValue
);
5960 if ( targetAtom
->scope() == ld::Atom::scopeTranslationUnit
) {
5961 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, targetAtom
);
5963 else if ( (targetAtom
->combine() == ld::Atom::combineByNameAndContent
) || (targetAtom
->combine() == ld::Atom::combineByNameAndReferences
) ) {
5964 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, targetAtom
);
5967 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, false, targetAtom
->name());
5969 parser
.addFixup(src
, ld::Fixup::k2of5
, ld::Fixup::kindAddAddend
, (uint32_t)offsetInTarget
+addend
);
5970 parser
.addFixup(src
, ld::Fixup::k3of5
, ld::Fixup::kindSubtractTargetAddress
, fromAtom
);
5971 parser
.addFixup(src
, ld::Fixup::k4of5
, ld::Fixup::kindSubtractAddend
, offsetInFrom
);
5972 if ( sreloc
->r_length() & 1 ) {
5974 parser
.addFixup(src
, ld::Fixup::k5of5
, (isThumb
? ld::Fixup::kindStoreThumbHigh16
: ld::Fixup::kindStoreARMHigh16
));
5978 parser
.addFixup(src
, ld::Fixup::k5of5
, (isThumb
? ld::Fixup::kindStoreThumbLow16
: ld::Fixup::kindStoreARMLow16
));
5983 throw "ARM_RELOC_HALF_SECTDIFF reloc missing following pair";
5985 case ARM_RELOC_HALF
:
5986 if ( nextRelocIsPair
) {
5987 instruction
= LittleEndian::get32(*fixUpPtr
);
5988 Atom
<arm
>* targetAtom
= parser
.findAtomByAddress(sreloc
->r_value());
5989 uint32_t instruction16
;
5990 uint32_t other16
= (nextRelocAddress
& 0xFFFF);
5992 if ( sreloc
->r_length() & 2 ) {
5994 uint32_t i
= ((instruction
& 0x00000400) >> 10);
5995 uint32_t imm4
= (instruction
& 0x0000000F);
5996 uint32_t imm3
= ((instruction
& 0x70000000) >> 28);
5997 uint32_t imm8
= ((instruction
& 0x00FF0000) >> 16);
5998 instruction16
= (imm4
<< 12) | (i
<< 11) | (imm3
<< 8) | imm8
;
6002 uint32_t imm4
= ((instruction
& 0x000F0000) >> 16);
6003 uint32_t imm12
= (instruction
& 0x00000FFF);
6004 instruction16
= (imm4
<< 12) | imm12
;
6006 if ( sreloc
->r_length() & 1 )
6007 dstAddr
= ((instruction16
<< 16) | other16
);
6009 dstAddr
= (other16
<< 16) | instruction16
;
6010 if ( targetAtom
->scope() == ld::Atom::scopeTranslationUnit
) {
6011 parser
.addFixup(src
, ld::Fixup::k1of3
, ld::Fixup::kindSetTargetAddress
, targetAtom
);
6013 else if ( (targetAtom
->combine() == ld::Atom::combineByNameAndContent
) || (targetAtom
->combine() == ld::Atom::combineByNameAndReferences
) ) {
6014 parser
.addFixup(src
, ld::Fixup::k1of3
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, targetAtom
);
6017 parser
.addFixup(src
, ld::Fixup::k1of3
, ld::Fixup::kindSetTargetAddress
, false, targetAtom
->name());
6019 parser
.addFixup(src
, ld::Fixup::k2of3
, ld::Fixup::kindAddAddend
, dstAddr
- targetAtom
->_objAddress
);
6020 if ( sreloc
->r_length() & 1 ) {
6022 parser
.addFixup(src
, ld::Fixup::k3of3
, (isThumb
? ld::Fixup::kindStoreThumbHigh16
: ld::Fixup::kindStoreARMHigh16
));
6026 parser
.addFixup(src
, ld::Fixup::k3of3
, (isThumb
? ld::Fixup::kindStoreThumbLow16
: ld::Fixup::kindStoreARMLow16
));
6031 throw "scattered ARM_RELOC_HALF reloc missing following pair";
6034 throwf("unknown ARM scattered relocation type %d", sreloc
->r_type());
6045 template <typename A
>
6046 bool ObjC1ClassSection
<A
>::addRelocFixup(class Parser
<A
>& parser
, const macho_relocation_info
<P
>* reloc
)
6049 FixedSizeSection
<A
>::addRelocFixup(parser
, reloc
);
6051 assert(0 && "needs template specialization");
6056 bool ObjC1ClassSection
<x86
>::addRelocFixup(class Parser
<x86
>& parser
, const macho_relocation_info
<x86::P
>* reloc
)
6058 // if this is the reloc for the super class name string, add implicit reference to super class
6059 if ( ((reloc
->r_address() & R_SCATTERED
) == 0) && (reloc
->r_type() == GENERIC_RELOC_VANILLA
) ) {
6060 assert( reloc
->r_length() == 2 );
6061 assert( ! reloc
->r_pcrel() );
6063 const macho_section
<P
>* sect
= this->machoSection();
6064 Parser
<x86
>::SourceLocation src
;
6065 uint32_t srcAddr
= sect
->addr() + reloc
->r_address();
6066 src
.atom
= this->findAtomByAddress(srcAddr
);
6067 src
.offsetInAtom
= srcAddr
- src
.atom
->objectAddress();
6068 if ( src
.offsetInAtom
== 4 ) {
6069 Parser
<x86
>::TargetDesc stringTarget
;
6070 const uint8_t* fixUpPtr
= file().fileContent() + sect
->offset() + reloc
->r_address();
6071 uint32_t contentValue
= LittleEndian::get32(*((uint32_t*)fixUpPtr
));
6072 parser
.findTargetFromAddressAndSectionNum(contentValue
, reloc
->r_symbolnum(), stringTarget
);
6074 assert(stringTarget
.atom
!= NULL
);
6075 assert(stringTarget
.atom
->contentType() == ld::Atom::typeCString
);
6076 const char* superClassBaseName
= (char*)stringTarget
.atom
->rawContentPointer();
6077 char* superClassName
= new char[strlen(superClassBaseName
) + 20];
6078 strcpy(superClassName
, ".objc_class_name_");
6079 strcat(superClassName
, superClassBaseName
);
6081 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindSetTargetAddress
, false, superClassName
);
6085 return FixedSizeSection
<x86
>::addRelocFixup(parser
, reloc
);
6090 template <typename A
>
6091 bool Objc1ClassReferences
<A
>::addRelocFixup(class Parser
<A
>& parser
, const macho_relocation_info
<P
>* reloc
)
6094 PointerToCStringSection
<A
>::addRelocFixup(parser
, reloc
);
6096 assert(0 && "needs template specialization");
6103 bool Objc1ClassReferences
<x86
>::addRelocFixup(class Parser
<x86
>& parser
, const macho_relocation_info
<x86::P
>* reloc
)
6105 // add implict class refs, fixups not usable yet, so look at relocations
6106 assert( (reloc
->r_address() & R_SCATTERED
) == 0 );
6107 assert( reloc
->r_type() == GENERIC_RELOC_VANILLA
);
6108 assert( reloc
->r_length() == 2 );
6109 assert( ! reloc
->r_pcrel() );
6111 const macho_section
<P
>* sect
= this->machoSection();
6112 Parser
<x86
>::SourceLocation src
;
6113 uint32_t srcAddr
= sect
->addr() + reloc
->r_address();
6114 src
.atom
= this->findAtomByAddress(srcAddr
);
6115 src
.offsetInAtom
= srcAddr
- src
.atom
->objectAddress();
6116 Parser
<x86
>::TargetDesc stringTarget
;
6117 const uint8_t* fixUpPtr
= file().fileContent() + sect
->offset() + reloc
->r_address();
6118 uint32_t contentValue
= LittleEndian::get32(*((uint32_t*)fixUpPtr
));
6119 parser
.findTargetFromAddressAndSectionNum(contentValue
, reloc
->r_symbolnum(), stringTarget
);
6121 assert(stringTarget
.atom
!= NULL
);
6122 assert(stringTarget
.atom
->contentType() == ld::Atom::typeCString
);
6123 const char* baseClassName
= (char*)stringTarget
.atom
->rawContentPointer();
6124 char* objcClassName
= new char[strlen(baseClassName
) + 20];
6125 strcpy(objcClassName
, ".objc_class_name_");
6126 strcat(objcClassName
, baseClassName
);
6128 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindSetTargetAddress
, false, objcClassName
);
6131 return PointerToCStringSection
<x86
>::addRelocFixup(parser
, reloc
);
6135 template <typename A
>
6136 void Section
<A
>::makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
&)
6138 const macho_section
<P
>* sect
= this->machoSection();
6139 const macho_relocation_info
<P
>* relocs
= (macho_relocation_info
<P
>*)(file().fileContent() + sect
->reloff());
6140 const uint32_t relocCount
= sect
->nreloc();
6141 for (uint32_t r
= 0; r
< relocCount
; ++r
) {
6143 if ( this->addRelocFixup(parser
, &relocs
[r
]) )
6146 catch (const char* msg
) {
6147 throwf("in section %s,%s reloc %u: %s", sect
->segname(), Section
<A
>::makeSectionName(sect
), r
, msg
);
6151 // add follow-on fixups if .o file is missing .subsections_via_symbols
6152 if ( this->addFollowOnFixups() ) {
6153 Atom
<A
>* end
= &_endAtoms
[-1];
6154 for(Atom
<A
>* p
= _beginAtoms
; p
< end
; ++p
) {
6155 typename Parser
<A
>::SourceLocation
src(p
, 0);
6156 Atom
<A
>* nextAtom
= &p
[1];
6157 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindNoneFollowOn
, nextAtom
);
6160 else if ( this->type() == ld::Section::typeCode
) {
6161 // if FDE broke text not at a symbol, use followOn to keep code together
6162 Atom
<A
>* end
= &_endAtoms
[-1];
6163 for(Atom
<A
>* p
= _beginAtoms
; p
< end
; ++p
) {
6164 typename Parser
<A
>::SourceLocation
src(p
, 0);
6165 Atom
<A
>* nextAtom
= &p
[1];
6166 if ( (p
->symbolTableInclusion() == ld::Atom::symbolTableIn
) && (nextAtom
->symbolTableInclusion() == ld::Atom::symbolTableNotIn
) ) {
6167 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindNoneFollowOn
, nextAtom
);
6172 // <rdar://problem/9218847> track data-in-code
6173 if ( parser
.hasDataInCodeLabels() && (this->type() == ld::Section::typeCode
) ) {
6174 for (uint32_t i
=0; i
< parser
.symbolCount(); ++i
) {
6175 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(i
);
6177 if ( (sym
.n_type() & N_STAB
) != 0 )
6179 // ignore non-definitions
6180 if ( (sym
.n_type() & N_TYPE
) != N_SECT
)
6183 // 'L' labels do not denote atom breaks
6184 const char* symbolName
= parser
.nameFromSymbol(sym
);
6185 if ( symbolName
[0] == 'L' ) {
6186 if ( strncmp(symbolName
, "L$start$", 8) == 0 ) {
6187 ld::Fixup::Kind kind
= ld::Fixup::kindNone
;
6188 if ( strncmp(&symbolName
[8], "data$", 5) == 0 )
6189 kind
= ld::Fixup::kindDataInCodeStartData
;
6190 else if ( strncmp(&symbolName
[8], "code$", 5) == 0 )
6191 kind
= ld::Fixup::kindDataInCodeEnd
;
6192 else if ( strncmp(&symbolName
[8], "jt8$", 4) == 0 )
6193 kind
= ld::Fixup::kindDataInCodeStartJT8
;
6194 else if ( strncmp(&symbolName
[8], "jt16$", 4) == 0 )
6195 kind
= ld::Fixup::kindDataInCodeStartJT16
;
6196 else if ( strncmp(&symbolName
[8], "jt32$", 4) == 0 )
6197 kind
= ld::Fixup::kindDataInCodeStartJT32
;
6198 else if ( strncmp(&symbolName
[8], "jta32$", 4) == 0 )
6199 kind
= ld::Fixup::kindDataInCodeStartJTA32
;
6201 warning("unknown L$start$ label %s in file %s", symbolName
, this->file().path());
6202 if ( kind
!= ld::Fixup::kindNone
) {
6203 Atom
<A
>* inAtom
= parser
.findAtomByAddress(sym
.n_value());
6204 typename Parser
<A
>::SourceLocation
src(inAtom
, sym
.n_value() - inAtom
->objectAddress());
6205 parser
.addFixup(src
, ld::Fixup::k1of1
, kind
);
6212 // <rdar://problem/11150575> Handle LC_DATA_IN_CODE in object files
6213 if ( this->type() == ld::Section::typeCode
) {
6214 const pint_t startAddr
= this->_machOSection
->addr();
6215 const pint_t endAddr
= startAddr
+ this->_machOSection
->size();
6216 for ( const macho_data_in_code_entry
<P
>* p
= parser
.dataInCodeStart(); p
!= parser
.dataInCodeEnd(); ++p
) {
6217 if ( (p
->offset() >= startAddr
) && (p
->offset() < endAddr
) ) {
6218 ld::Fixup::Kind kind
= ld::Fixup::kindNone
;
6219 switch ( p
->kind() ) {
6220 case DICE_KIND_DATA
:
6221 kind
= ld::Fixup::kindDataInCodeStartData
;
6223 case DICE_KIND_JUMP_TABLE8
:
6224 kind
= ld::Fixup::kindDataInCodeStartJT8
;
6226 case DICE_KIND_JUMP_TABLE16
:
6227 kind
= ld::Fixup::kindDataInCodeStartJT16
;
6229 case DICE_KIND_JUMP_TABLE32
:
6230 kind
= ld::Fixup::kindDataInCodeStartJT32
;
6232 case DICE_KIND_ABS_JUMP_TABLE32
:
6233 kind
= ld::Fixup::kindDataInCodeStartJTA32
;
6236 kind
= ld::Fixup::kindDataInCodeStartData
;
6237 warning("uknown LC_DATA_IN_CODE kind (%d) at offset 0x%08X", p
->kind(), p
->offset());
6240 Atom
<A
>* inAtom
= parser
.findAtomByAddress(p
->offset());
6241 typename Parser
<A
>::SourceLocation
srcStart(inAtom
, p
->offset() - inAtom
->objectAddress());
6242 parser
.addFixup(srcStart
, ld::Fixup::k1of1
, kind
);
6243 typename Parser
<A
>::SourceLocation
srcEnd(inAtom
, p
->offset() + p
->length() - inAtom
->objectAddress());
6244 parser
.addFixup(srcEnd
, ld::Fixup::k1of1
, ld::Fixup::kindDataInCodeEnd
);
6250 // add follow-on fixups for aliases
6251 if ( _hasAliases
) {
6252 for(Atom
<A
>* p
= _beginAtoms
; p
< _endAtoms
; ++p
) {
6253 if ( p
->isAlias() && ! this->addFollowOnFixups() ) {
6254 Atom
<A
>* targetOfAlias
= &p
[1];
6255 assert(p
< &_endAtoms
[-1]);
6256 assert(p
->_objAddress
== targetOfAlias
->_objAddress
);
6257 typename Parser
<A
>::SourceLocation
src(p
, 0);
6258 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindNoneFollowOn
, targetOfAlias
);
6267 // main function used by linker to instantiate ld::Files
6269 ld::relocatable::File
* parse(const uint8_t* fileContent
, uint64_t fileLength
,
6270 const char* path
, time_t modTime
, ld::File::Ordinal ordinal
, const ParserOptions
& opts
)
6272 switch ( opts
.architecture
) {
6273 #if SUPPORT_ARCH_x86_64
6274 case CPU_TYPE_X86_64
:
6275 if ( mach_o::relocatable::Parser
<x86_64
>::validFile(fileContent
) )
6276 return mach_o::relocatable::Parser
<x86_64
>::parse(fileContent
, fileLength
, path
, modTime
, ordinal
, opts
);
6279 #if SUPPORT_ARCH_i386
6281 if ( mach_o::relocatable::Parser
<x86
>::validFile(fileContent
) )
6282 return mach_o::relocatable::Parser
<x86
>::parse(fileContent
, fileLength
, path
, modTime
, ordinal
, opts
);
6285 #if SUPPORT_ARCH_arm_any
6287 if ( mach_o::relocatable::Parser
<arm
>::validFile(fileContent
, opts
.objSubtypeMustMatch
, opts
.subType
) )
6288 return mach_o::relocatable::Parser
<arm
>::parse(fileContent
, fileLength
, path
, modTime
, ordinal
, opts
);
6296 // used by archive reader to validate member object file
6298 bool isObjectFile(const uint8_t* fileContent
, uint64_t fileLength
, const ParserOptions
& opts
)
6300 switch ( opts
.architecture
) {
6301 case CPU_TYPE_X86_64
:
6302 return ( mach_o::relocatable::Parser
<x86_64
>::validFile(fileContent
) );
6304 return ( mach_o::relocatable::Parser
<x86
>::validFile(fileContent
) );
6306 return ( mach_o::relocatable::Parser
<arm
>::validFile(fileContent
, opts
.objSubtypeMustMatch
, opts
.subType
) );
6312 // used by linker to infer architecture when no -arch is on command line
6314 bool isObjectFile(const uint8_t* fileContent
, cpu_type_t
* result
, cpu_subtype_t
* subResult
)
6316 if ( mach_o::relocatable::Parser
<x86_64
>::validFile(fileContent
) ) {
6317 *result
= CPU_TYPE_X86_64
;
6318 *subResult
= CPU_SUBTYPE_X86_64_ALL
;
6321 if ( mach_o::relocatable::Parser
<x86
>::validFile(fileContent
) ) {
6322 *result
= CPU_TYPE_I386
;
6323 *subResult
= CPU_SUBTYPE_X86_ALL
;
6326 if ( mach_o::relocatable::Parser
<arm
>::validFile(fileContent
, false, 0) ) {
6327 *result
= CPU_TYPE_ARM
;
6328 const macho_header
<Pointer32
<LittleEndian
> >* header
= (const macho_header
<Pointer32
<LittleEndian
> >*)fileContent
;
6329 *subResult
= header
->cpusubtype();
6336 // used by linker is error messages to describe bad .o file
6338 const char* archName(const uint8_t* fileContent
)
6340 if ( mach_o::relocatable::Parser
<x86_64
>::validFile(fileContent
) ) {
6341 return mach_o::relocatable::Parser
<x86_64
>::fileKind(fileContent
);
6343 if ( mach_o::relocatable::Parser
<x86
>::validFile(fileContent
) ) {
6344 return mach_o::relocatable::Parser
<x86
>::fileKind(fileContent
);
6346 if ( mach_o::relocatable::Parser
<arm
>::validFile(fileContent
, false, 0) ) {
6347 return mach_o::relocatable::Parser
<arm
>::fileKind(fileContent
);
6353 // Used by archive reader when -ObjC option is specified
6355 bool hasObjC2Categories(const uint8_t* fileContent
)
6357 if ( mach_o::relocatable::Parser
<x86_64
>::validFile(fileContent
) ) {
6358 return mach_o::relocatable::Parser
<x86_64
>::hasObjC2Categories(fileContent
);
6360 else if ( mach_o::relocatable::Parser
<arm
>::validFile(fileContent
, false, 0) ) {
6361 return mach_o::relocatable::Parser
<arm
>::hasObjC2Categories(fileContent
);
6363 else if ( mach_o::relocatable::Parser
<x86
>::validFile(fileContent
, false, 0) ) {
6364 return mach_o::relocatable::Parser
<x86
>::hasObjC2Categories(fileContent
);
6370 // Used by archive reader when -ObjC option is specified
6372 bool hasObjC1Categories(const uint8_t* fileContent
)
6374 if ( mach_o::relocatable::Parser
<x86
>::validFile(fileContent
, false, 0) ) {
6375 return mach_o::relocatable::Parser
<x86
>::hasObjC1Categories(fileContent
);
6382 } // namespace relocatable
6383 } // namespace mach_o