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
, uint32_t 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 _dwarfTranslationUnitDir(NULL
), _dwarfTranslationUnitFile(NULL
),
79 _dwarfDebugInfoSect(NULL
), _dwarfDebugAbbrevSect(NULL
),
80 _dwarfDebugLineSect(NULL
), _dwarfDebugStringSect(NULL
),
81 _objConstraint(ld::File::objcConstraintNone
),
83 _ojcReplacmentClass(false), _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 bool objcReplacementClasses() const { return _ojcReplacmentClass
; }
93 virtual ObjcConstraint
objCConstraint() const { return _objConstraint
; }
94 virtual uint32_t cpuSubType() const { return _cpuSubType
; }
95 virtual DebugInfoKind
debugInfo() const { return _debugInfoKind
; }
96 virtual const std::vector
<ld::relocatable::File::Stab
>* stabs() const { return &_stabs
; }
97 virtual bool canScatterAtoms() const { return _canScatterAtoms
; }
98 bool translationUnitSource(const char** dir
, const char** name
) const;
100 const uint8_t* fileContent() { return _fileContent
; }
102 friend class Atom
<A
>;
103 friend class Section
<A
>;
104 friend class Parser
<A
>;
105 friend class CFISection
<A
>::OAS
;
107 typedef typename
A::P P
;
109 const uint8_t* _fileContent
;
110 Section
<A
>** _sectionsArray
;
111 uint8_t* _atomsArray
;
112 uint32_t _sectionsArrayCount
;
113 uint32_t _atomsArrayCount
;
114 std::vector
<ld::Fixup
> _fixups
;
115 std::vector
<ld::Atom::UnwindInfo
> _unwindInfos
;
116 std::vector
<ld::Atom::LineInfo
> _lineInfos
;
117 std::vector
<ld::relocatable::File::Stab
>_stabs
;
118 ld::relocatable::File::DebugInfoKind _debugInfoKind
;
119 const char* _dwarfTranslationUnitDir
;
120 const char* _dwarfTranslationUnitFile
;
121 const macho_section
<P
>* _dwarfDebugInfoSect
;
122 const macho_section
<P
>* _dwarfDebugAbbrevSect
;
123 const macho_section
<P
>* _dwarfDebugLineSect
;
124 const macho_section
<P
>* _dwarfDebugStringSect
;
125 ld::File::ObjcConstraint _objConstraint
;
126 uint32_t _cpuSubType
;
127 bool _ojcReplacmentClass
;
128 bool _canScatterAtoms
;
132 template <typename A
>
133 class Section
: public ld::Section
136 typedef typename
A::P::uint_t pint_t
;
137 typedef typename
A::P P
;
138 typedef typename
A::P::E E
;
140 virtual ~Section() { }
141 class File
<A
>& file() const { return _file
; }
142 const macho_section
<P
>* machoSection() const { return _machOSection
; }
143 uint32_t sectionNum(class Parser
<A
>&) const;
144 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
);
145 virtual ld::Atom::ContentType
contentType() { return ld::Atom::typeUnclassified
; }
146 virtual bool dontDeadStrip() { return (this->_machOSection
->flags() & S_ATTR_NO_DEAD_STRIP
); }
147 virtual Atom
<A
>* findAtomByAddress(pint_t addr
) { return this->findContentAtomByAddress(addr
, this->_beginAtoms
, this->_endAtoms
); }
148 virtual bool addFollowOnFixups() const { return ! _file
.canScatterAtoms(); }
149 virtual uint32_t appendAtoms(class Parser
<A
>& parser
, uint8_t* buffer
,
150 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
151 const struct Parser
<A
>::CFI_CU_InfoArrays
&) = 0;
152 virtual uint32_t computeAtomCount(class Parser
<A
>& parser
,
153 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
154 const struct Parser
<A
>::CFI_CU_InfoArrays
&) = 0;
155 virtual void makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
&);
156 virtual bool addRelocFixup(class Parser
<A
>& parser
, const macho_relocation_info
<P
>*);
157 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const { return 0; }
158 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
159 const ld::IndirectBindingTable
& ind
) const { return false; }
160 static const char* makeSectionName(const macho_section
<typename
A::P
>* s
);
163 Section(File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
164 : ld::Section(makeSegmentName(s
), makeSectionName(s
), sectionType(s
)),
165 _file(f
), _machOSection(s
), _beginAtoms(NULL
), _endAtoms(NULL
), _hasAliases(false) { }
166 Section(File
<A
>& f
, const char* segName
, const char* sectName
, ld::Section::Type t
, bool hidden
=false)
167 : ld::Section(segName
, sectName
, t
, hidden
), _file(f
), _machOSection(NULL
),
168 _beginAtoms(NULL
), _endAtoms(NULL
), _hasAliases(false) { }
171 bool addRelocFixup_powerpc(class Parser
<A
>& parser
,const macho_relocation_info
<typename
A::P
>* reloc
);
172 Atom
<A
>* findContentAtomByAddress(pint_t addr
, class Atom
<A
>* start
, class Atom
<A
>* end
);
173 uint32_t x86_64PcRelOffset(uint8_t r_type
);
174 static const char* makeSegmentName(const macho_section
<typename
A::P
>* s
);
175 static bool readable(const macho_section
<typename
A::P
>* s
);
176 static bool writable(const macho_section
<typename
A::P
>* s
);
177 static bool exectuable(const macho_section
<typename
A::P
>* s
);
178 static ld::Section::Type
sectionType(const macho_section
<typename
A::P
>* s
);
181 const macho_section
<P
>* _machOSection
;
182 class Atom
<A
>* _beginAtoms
;
183 class Atom
<A
>* _endAtoms
;
188 template <typename A
>
189 class CFISection
: public Section
<A
>
192 CFISection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
193 : Section
<A
>(f
, s
) { }
196 virtual ld::Atom::ContentType
contentType() { return ld::Atom::typeCFI
; }
197 virtual uint32_t computeAtomCount(class Parser
<A
>& parser
, struct Parser
<A
>::LabelAndCFIBreakIterator
& it
, const struct Parser
<A
>::CFI_CU_InfoArrays
&);
198 virtual uint32_t appendAtoms(class Parser
<A
>& parser
, uint8_t* buffer
, struct Parser
<A
>::LabelAndCFIBreakIterator
& it
, const struct Parser
<A
>::CFI_CU_InfoArrays
&);
199 virtual void makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
&);
200 virtual bool addFollowOnFixups() const { return false; }
204 /// ObjectFileAddressSpace is used as a template parameter to UnwindCursor for parsing
205 /// dwarf CFI information in an object file.
210 typedef typename
A::P::uint_t pint_t
;
211 typedef typename
A::P P
;
212 typedef typename
A::P::E E
;
213 typedef typename
A::P::uint_t sint_t
;
215 OAS(CFISection
<A
>& ehFrameSection
, const uint8_t* ehFrameBuffer
) :
216 _ehFrameSection(ehFrameSection
),
217 _ehFrameContent(ehFrameBuffer
),
218 _ehFrameStartAddr(ehFrameSection
.machoSection()->addr()),
219 _ehFrameEndAddr(ehFrameSection
.machoSection()->addr()+ehFrameSection
.machoSection()->size()) {}
221 uint8_t get8(pint_t addr
) { return *((uint8_t*)mappedAddress(addr
)); }
222 uint16_t get16(pint_t addr
) { return E::get16(*((uint16_t*)mappedAddress(addr
))); }
223 uint32_t get32(pint_t addr
) { return E::get32(*((uint32_t*)mappedAddress(addr
))); }
224 uint64_t get64(pint_t addr
) { return E::get64(*((uint64_t*)mappedAddress(addr
))); }
225 pint_t
getP(pint_t addr
) { return P::getP(*((pint_t
*)mappedAddress(addr
))); }
226 uint64_t getULEB128(pint_t
& addr
, pint_t end
);
227 int64_t getSLEB128(pint_t
& addr
, pint_t end
);
228 pint_t
getEncodedP(pint_t
& addr
, pint_t end
, uint8_t encoding
);
230 const void* mappedAddress(pint_t addr
);
232 CFISection
<A
>& _ehFrameSection
;
233 const uint8_t* _ehFrameContent
;
234 pint_t _ehFrameStartAddr
;
235 pint_t _ehFrameEndAddr
;
239 typedef typename
A::P::uint_t pint_t
;
240 typedef libunwind::CFI_Atom_Info
<OAS
> CFI_Atom_Info
;
242 void cfiParse(class Parser
<A
>& parser
, uint8_t* buffer
, CFI_Atom_Info cfiArray
[], uint32_t cfiCount
);
243 bool needsRelocating();
245 static bool bigEndian();
247 void addCiePersonalityFixups(class Parser
<A
>& parser
, const CFI_Atom_Info
* cieInfo
);
248 static void warnFunc(void* ref
, uint64_t funcAddr
, const char* msg
);
252 template <typename A
>
253 class CUSection
: public Section
<A
>
256 CUSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
257 : Section
<A
>(f
, s
) { }
259 typedef typename
A::P::uint_t pint_t
;
260 typedef typename
A::P P
;
261 typedef typename
A::P::E E
;
263 virtual uint32_t computeAtomCount(class Parser
<A
>& parser
, struct Parser
<A
>::LabelAndCFIBreakIterator
& it
, const struct Parser
<A
>::CFI_CU_InfoArrays
&) { return 0; }
264 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; }
265 virtual void makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
&);
266 virtual bool addFollowOnFixups() const { return false; }
269 pint_t functionStartAddress
;
270 uint32_t functionSymbolIndex
;
271 uint32_t rangeLength
;
272 uint32_t compactUnwindInfo
;
273 const char* personality
;
280 void parse(class Parser
<A
>& parser
, uint32_t cnt
, Info array
[]);
285 const char* personalityName(class Parser
<A
>& parser
, const macho_relocation_info
<P
>* reloc
);
287 static int infoSorter(const void* l
, const void* r
);
292 template <typename A
>
293 class TentativeDefinitionSection
: public Section
<A
>
296 TentativeDefinitionSection(Parser
<A
>& parser
, File
<A
>& f
)
297 : Section
<A
>(f
, "__DATA", "__comm/tent", ld::Section::typeTentativeDefs
) {}
299 virtual ld::Atom::ContentType
contentType() { return ld::Atom::typeZeroFill
; }
300 virtual bool addFollowOnFixups() const { return false; }
301 virtual Atom
<A
>* findAtomByAddress(typename
A::P::uint_t addr
) { throw "TentativeDefinitionSection::findAtomByAddress() should never be called"; }
302 virtual uint32_t computeAtomCount(class Parser
<A
>& parser
, struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
303 const struct Parser
<A
>::CFI_CU_InfoArrays
&);
304 virtual uint32_t appendAtoms(class Parser
<A
>& parser
, uint8_t* buffer
,
305 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
306 const struct Parser
<A
>::CFI_CU_InfoArrays
&);
307 virtual void makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
&) {}
309 typedef typename
A::P::uint_t pint_t
;
310 typedef typename
A::P P
;
314 template <typename A
>
315 class AbsoluteSymbolSection
: public Section
<A
>
318 AbsoluteSymbolSection(Parser
<A
>& parser
, File
<A
>& f
)
319 : Section
<A
>(f
, "__DATA", "__abs", ld::Section::typeAbsoluteSymbols
, true) {}
321 virtual ld::Atom::ContentType
contentType() { return ld::Atom::typeUnclassified
; }
322 virtual bool dontDeadStrip() { return false; }
323 virtual ld::Atom::Alignment
alignmentForAddress(typename
A::P::uint_t addr
) { return ld::Atom::Alignment(0); }
324 virtual bool addFollowOnFixups() const { return false; }
325 virtual Atom
<A
>* findAtomByAddress(typename
A::P::uint_t addr
) { throw "AbsoluteSymbolSection::findAtomByAddress() should never be called"; }
326 virtual uint32_t computeAtomCount(class Parser
<A
>& parser
, struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
327 const struct Parser
<A
>::CFI_CU_InfoArrays
&);
328 virtual uint32_t appendAtoms(class Parser
<A
>& parser
, uint8_t* buffer
,
329 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
330 const struct Parser
<A
>::CFI_CU_InfoArrays
&);
331 virtual void makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
&) {}
332 virtual Atom
<A
>* findAbsAtomForValue(typename
A::P::uint_t
);
335 typedef typename
A::P::uint_t pint_t
;
336 typedef typename
A::P P
;
340 template <typename A
>
341 class SymboledSection
: public Section
<A
>
344 SymboledSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
);
345 virtual ld::Atom::ContentType
contentType() { return _type
; }
346 virtual bool dontDeadStrip();
347 virtual uint32_t computeAtomCount(class Parser
<A
>& parser
, struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
348 const struct Parser
<A
>::CFI_CU_InfoArrays
&);
349 virtual uint32_t appendAtoms(class Parser
<A
>& parser
, uint8_t* buffer
,
350 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
351 const struct Parser
<A
>::CFI_CU_InfoArrays
&);
353 typedef typename
A::P::uint_t pint_t
;
354 typedef typename
A::P P
;
356 ld::Atom::ContentType _type
;
360 template <typename A
>
361 class TLVDefsSection
: public SymboledSection
<A
>
364 TLVDefsSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
) :
365 SymboledSection
<A
>(parser
, f
, s
) { }
372 template <typename A
>
373 class ImplicitSizeSection
: public Section
<A
>
376 ImplicitSizeSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
377 : Section
<A
>(f
, s
) { }
378 virtual uint32_t computeAtomCount(class Parser
<A
>& parser
, struct Parser
<A
>::LabelAndCFIBreakIterator
& it
, const struct Parser
<A
>::CFI_CU_InfoArrays
&);
379 virtual uint32_t appendAtoms(class Parser
<A
>& parser
, uint8_t* buffer
, struct Parser
<A
>::LabelAndCFIBreakIterator
& it
, const struct Parser
<A
>::CFI_CU_InfoArrays
&);
381 typedef typename
A::P::uint_t pint_t
;
382 typedef typename
A::P P
;
384 virtual bool addFollowOnFixups() const { return false; }
385 virtual const char* unlabeledAtomName(Parser
<A
>& parser
, pint_t addr
) = 0;
386 virtual ld::Atom::SymbolTableInclusion
symbolTableInclusion() { return ld::Atom::symbolTableNotIn
; }
387 virtual pint_t
elementSizeAtAddress(pint_t addr
) = 0;
388 virtual ld::Atom::Scope
scopeAtAddress(Parser
<A
>& parser
, pint_t addr
) { return ld::Atom::scopeLinkageUnit
; }
389 virtual bool useElementAt(Parser
<A
>& parser
,
390 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
, pint_t addr
) = 0;
391 virtual ld::Atom::Definition
definition() { return ld::Atom::definitionRegular
; }
392 virtual ld::Atom::Combine
combine(Parser
<A
>& parser
, pint_t addr
) = 0;
393 virtual bool ignoreLabel(const char* label
) { return (label
[0] == 'L'); }
396 template <typename A
>
397 class FixedSizeSection
: public ImplicitSizeSection
<A
>
400 FixedSizeSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
401 : ImplicitSizeSection
<A
>(parser
, f
, s
) { }
403 typedef typename
A::P::uint_t pint_t
;
404 typedef typename
A::P P
;
405 typedef typename
A::P::E E
;
407 virtual bool useElementAt(Parser
<A
>& parser
,
408 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
, pint_t addr
)
413 template <typename A
>
414 class Literal4Section
: public FixedSizeSection
<A
>
417 Literal4Section(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
418 : FixedSizeSection
<A
>(parser
, f
, s
) {}
420 typedef typename
A::P::uint_t pint_t
;
421 typedef typename
A::P P
;
423 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
) { return ld::Atom::Alignment(2); }
424 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "4-byte-literal"; }
425 virtual pint_t
elementSizeAtAddress(pint_t addr
) { return 4; }
426 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
) { return ld::Atom::combineByNameAndContent
; }
427 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
428 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
429 const ld::IndirectBindingTable
& ind
) const;
432 template <typename A
>
433 class Literal8Section
: public FixedSizeSection
<A
>
436 Literal8Section(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
437 : FixedSizeSection
<A
>(parser
, f
, s
) {}
439 typedef typename
A::P::uint_t pint_t
;
440 typedef typename
A::P P
;
442 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
) { return ld::Atom::Alignment(3); }
443 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "8-byte-literal"; }
444 virtual pint_t
elementSizeAtAddress(pint_t addr
) { return 8; }
445 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
) { return ld::Atom::combineByNameAndContent
; }
446 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
447 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
448 const ld::IndirectBindingTable
& ind
) const;
451 template <typename A
>
452 class Literal16Section
: public FixedSizeSection
<A
>
455 Literal16Section(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
456 : FixedSizeSection
<A
>(parser
, f
, s
) {}
458 typedef typename
A::P::uint_t pint_t
;
459 typedef typename
A::P P
;
461 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
) { return ld::Atom::Alignment(4); }
462 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "16-byte-literal"; }
463 virtual pint_t
elementSizeAtAddress(pint_t addr
) { return 16; }
464 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
) { return ld::Atom::combineByNameAndContent
; }
465 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
466 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
467 const ld::IndirectBindingTable
& ind
) const;
471 template <typename A
>
472 class NonLazyPointerSection
: public FixedSizeSection
<A
>
475 NonLazyPointerSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
476 : FixedSizeSection
<A
>(parser
, f
, s
) {}
478 typedef typename
A::P::uint_t pint_t
;
479 typedef typename
A::P P
;
481 virtual void makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
&);
482 virtual ld::Atom::ContentType
contentType() { return ld::Atom::typeNonLazyPointer
; }
483 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
) { return ld::Atom::Alignment(log2(sizeof(pint_t
))); }
484 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "non_lazy_ptr"; }
485 virtual pint_t
elementSizeAtAddress(pint_t addr
) { return sizeof(pint_t
); }
486 virtual ld::Atom::Scope
scopeAtAddress(Parser
<A
>& parser
, pint_t addr
);
487 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
);
488 virtual bool ignoreLabel(const char* label
) { return true; }
489 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
490 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
491 const ld::IndirectBindingTable
& ind
) const;
494 static const char* targetName(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
);
495 static ld::Fixup::Kind
fixupKind();
499 template <typename A
>
500 class CFStringSection
: public FixedSizeSection
<A
>
503 CFStringSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
504 : FixedSizeSection
<A
>(parser
, f
, s
) {}
506 typedef typename
A::P::uint_t pint_t
;
508 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
) { return ld::Atom::Alignment(log2(sizeof(pint_t
))); }
509 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "CFString"; }
510 virtual pint_t
elementSizeAtAddress(pint_t addr
) { return 4*sizeof(pint_t
); }
511 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
) { return ld::Atom::combineByNameAndReferences
; }
512 virtual bool ignoreLabel(const char* label
) { return true; }
513 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
514 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
515 const ld::IndirectBindingTable
& ind
) const;
517 enum ContentType
{ contentUTF8
, contentUTF16
, contentUnknown
};
518 static const uint8_t* targetContent(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
,
519 ContentType
* ct
, unsigned int* count
);
523 template <typename A
>
524 class ObjC1ClassSection
: public FixedSizeSection
<A
>
527 ObjC1ClassSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
528 : FixedSizeSection
<A
>(parser
, f
, s
) {}
530 typedef typename
A::P::uint_t pint_t
;
531 typedef typename
A::P P
;
532 typedef typename
A::P::E E
;
534 virtual ld::Atom::Scope
scopeAtAddress(Parser
<A
>& , pint_t
) { return ld::Atom::scopeGlobal
; }
535 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
) { return ld::Atom::Alignment(2); }
536 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
);
537 virtual ld::Atom::SymbolTableInclusion
symbolTableInclusion() { return ld::Atom::symbolTableIn
; }
538 virtual pint_t
elementSizeAtAddress(pint_t addr
);
539 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
) { return ld::Atom::combineNever
; }
540 virtual bool ignoreLabel(const char* label
) { return true; }
541 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
543 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
544 const ld::IndirectBindingTable
& ind
) const { return false; }
545 virtual bool addRelocFixup(class Parser
<A
>& parser
, const macho_relocation_info
<P
>*);
549 template <typename A
>
550 class ObjC2ClassRefsSection
: public FixedSizeSection
<A
>
553 ObjC2ClassRefsSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
554 : FixedSizeSection
<A
>(parser
, f
, s
) {}
556 typedef typename
A::P::uint_t pint_t
;
558 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
) { return ld::Atom::Alignment(log2(sizeof(pint_t
))); }
559 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "objc-class-ref"; }
560 virtual pint_t
elementSizeAtAddress(pint_t addr
) { return sizeof(pint_t
); }
561 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
) { return ld::Atom::combineByNameAndReferences
; }
562 virtual bool ignoreLabel(const char* label
) { return true; }
563 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
564 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
565 const ld::IndirectBindingTable
& ind
) const;
567 const char* targetClassName(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
571 template <typename A
>
572 class ObjC2CategoryListSection
: public FixedSizeSection
<A
>
575 ObjC2CategoryListSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
576 : FixedSizeSection
<A
>(parser
, f
, s
) {}
578 typedef typename
A::P::uint_t pint_t
;
580 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
) { return ld::Atom::Alignment(log2(sizeof(pint_t
))); }
581 virtual ld::Atom::Scope
scopeAtAddress(Parser
<A
>& parser
, pint_t addr
) { return ld::Atom::scopeTranslationUnit
; }
582 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "objc-cat-list"; }
583 virtual pint_t
elementSizeAtAddress(pint_t addr
) { return sizeof(pint_t
); }
584 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
) { return ld::Atom::combineNever
; }
585 virtual bool ignoreLabel(const char* label
) { return true; }
587 const char* targetClassName(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
591 template <typename A
>
592 class PointerToCStringSection
: public FixedSizeSection
<A
>
595 PointerToCStringSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
596 : FixedSizeSection
<A
>(parser
, f
, s
) {}
598 typedef typename
A::P::uint_t pint_t
;
600 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
) { return ld::Atom::Alignment(log2(sizeof(pint_t
))); }
601 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "pointer-to-literal-cstring"; }
602 virtual pint_t
elementSizeAtAddress(pint_t addr
) { return sizeof(pint_t
); }
603 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
) { return ld::Atom::combineByNameAndReferences
; }
604 virtual bool ignoreLabel(const char* label
) { return true; }
605 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
606 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
607 const ld::IndirectBindingTable
& ind
) const;
608 virtual const char* targetCString(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
612 template <typename A
>
613 class Objc1ClassReferences
: public PointerToCStringSection
<A
>
616 Objc1ClassReferences(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
617 : PointerToCStringSection
<A
>(parser
, f
, s
) {}
619 typedef typename
A::P::uint_t pint_t
;
620 typedef typename
A::P P
;
622 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "pointer-to-literal-objc-class-name"; }
623 virtual bool addRelocFixup(class Parser
<A
>& parser
, const macho_relocation_info
<P
>*);
624 virtual const char* targetCString(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
628 template <typename A
>
629 class CStringSection
: public ImplicitSizeSection
<A
>
632 CStringSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
633 : ImplicitSizeSection
<A
>(parser
, f
, s
) {}
635 typedef typename
A::P::uint_t pint_t
;
636 typedef typename
A::P P
;
638 virtual ld::Atom::ContentType
contentType() { return ld::Atom::typeCString
; }
639 virtual Atom
<A
>* findAtomByAddress(pint_t addr
);
640 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "cstring"; }
641 virtual pint_t
elementSizeAtAddress(pint_t addr
);
642 virtual bool ignoreLabel(const char* label
);
643 virtual bool useElementAt(Parser
<A
>& parser
,
644 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
, pint_t addr
);
645 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
) { return ld::Atom::combineByNameAndContent
; }
646 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
647 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
648 const ld::IndirectBindingTable
& ind
) const;
653 template <typename A
>
654 class UTF16StringSection
: public SymboledSection
<A
>
657 UTF16StringSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
658 : SymboledSection
<A
>(parser
, f
, s
) {}
660 typedef typename
A::P::uint_t pint_t
;
661 typedef typename
A::P P
;
663 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
) { return ld::Atom::combineByNameAndContent
; }
664 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
665 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
666 const ld::IndirectBindingTable
& ind
) const;
671 // Atoms in mach-o files
673 template <typename A
>
674 class Atom
: public ld::Atom
677 // overrides of ld::Atom
678 virtual ld::File
* file() const { return §().file(); }
679 virtual bool translationUnitSource(const char** dir
, const char** nm
) const
680 { return sect().file().translationUnitSource(dir
, nm
); }
681 virtual const char* name() const { return _name
; }
682 virtual uint64_t size() const { return _size
; }
683 virtual uint64_t objectAddress() const { return _objAddress
; }
684 virtual void copyRawContent(uint8_t buffer
[]) const;
685 virtual const uint8_t* rawContentPointer() const { return contentPointer(); }
686 virtual unsigned long contentHash(const ld::IndirectBindingTable
& ind
) const
687 { if ( _hash
== 0 ) _hash
= sect().contentHash(this, ind
); return _hash
; }
688 virtual bool canCoalesceWith(const ld::Atom
& rhs
, const ld::IndirectBindingTable
& ind
) const
689 { return sect().canCoalesceWith(this, rhs
, ind
); }
690 virtual ld::Fixup::iterator
fixupsBegin() const { return &machofile()._fixups
[_fixupsStartIndex
]; }
691 virtual ld::Fixup::iterator
fixupsEnd() const { return &machofile()._fixups
[_fixupsStartIndex
+_fixupsCount
]; }
692 virtual ld::Atom::UnwindInfo::iterator
beginUnwind() const { return &machofile()._unwindInfos
[_unwindInfoStartIndex
]; }
693 virtual ld::Atom::UnwindInfo::iterator
endUnwind() const { return &machofile()._unwindInfos
[_unwindInfoStartIndex
+_unwindInfoCount
]; }
694 virtual ld::Atom::LineInfo::iterator
beginLineInfo() const{ return &machofile()._lineInfos
[_lineInfoStartIndex
]; }
695 virtual ld::Atom::LineInfo::iterator
endLineInfo() const { return &machofile()._lineInfos
[_lineInfoStartIndex
+_lineInfoCount
]; }
699 enum { kFixupStartIndexBits
= 32,
700 kLineInfoStartIndexBits
= 32,
701 kUnwindInfoStartIndexBits
= 24,
702 kFixupCountBits
= 24,
703 kLineInfoCountBits
= 12,
704 kUnwindInfoCountBits
= 4
705 }; // must sum to 128
708 // methods for all atoms from mach-o object file
709 Section
<A
>& sect() const { return (Section
<A
>&)section(); }
710 File
<A
>& machofile() const { return ((Section
<A
>*)(this->_section
))->file(); }
711 void setFixupsRange(uint32_t s
, uint32_t c
);
712 void setUnwindInfoRange(uint32_t s
, uint32_t c
);
713 void extendUnwindInfoRange();
714 void setLineInfoRange(uint32_t s
, uint32_t c
);
715 bool roomForMoreLineInfoCount() { return (_lineInfoCount
< ((1<<kLineInfoCountBits
)-1)); }
716 void incrementLineInfoCount() { assert(roomForMoreLineInfoCount()); ++_lineInfoCount
; }
717 void incrementFixupCount() { if (_fixupsCount
== ((1 << kFixupCountBits
)-1))
718 throwf("too may fixups in %s", name()); ++_fixupsCount
; }
719 const uint8_t* contentPointer() const;
720 uint32_t fixupCount() const { return _fixupsCount
; }
721 void verifyAlignment() const;
723 typedef typename
A::P P
;
724 typedef typename
A::P::E E
;
725 typedef typename
A::P::uint_t pint_t
;
726 // constuct via all attributes
727 Atom(Section
<A
>& sct
, const char* nm
, pint_t addr
, uint64_t sz
,
728 ld::Atom::Definition d
, ld::Atom::Combine c
, ld::Atom::Scope s
,
729 ld::Atom::ContentType ct
, ld::Atom::SymbolTableInclusion i
,
730 bool dds
, bool thumb
, bool al
, ld::Atom::Alignment a
)
731 : ld::Atom((ld::Section
&)sct
, d
, c
, s
, ct
, i
, dds
, thumb
, al
, a
),
732 _size(sz
), _objAddress(addr
), _name(nm
), _hash(0),
733 _fixupsStartIndex(0), _lineInfoStartIndex(0),
734 _unwindInfoStartIndex(0), _fixupsCount(0),
735 _lineInfoCount(0), _unwindInfoCount(0) { }
736 // construct via symbol table entry
737 Atom(Section
<A
>& sct
, Parser
<A
>& parser
, const macho_nlist
<P
>& sym
,
738 uint64_t sz
, bool alias
=false)
739 : ld::Atom((ld::Section
&)sct
, parser
.definitionFromSymbol(sym
),
740 parser
.combineFromSymbol(sym
), parser
.scopeFromSymbol(sym
),
741 parser
.resolverFromSymbol(sym
) ? ld::Atom::typeResolver
: sct
.contentType(),
742 parser
.inclusionFromSymbol(sym
),
743 parser
.dontDeadStripFromSymbol(sym
) || sct
.dontDeadStrip(),
744 parser
.isThumbFromSymbol(sym
), alias
,
745 sct
.alignmentForAddress(sym
.n_value())),
746 _size(sz
), _objAddress(sym
.n_value()),
747 _name(parser
.nameFromSymbol(sym
)), _hash(0),
748 _fixupsStartIndex(0), _lineInfoStartIndex(0),
749 _unwindInfoStartIndex(0), _fixupsCount(0),
750 _lineInfoCount(0), _unwindInfoCount(0) {
751 // <rdar://problem/6783167> support auto-hidden weak symbols
752 if ( _scope
== ld::Atom::scopeGlobal
&&
753 (sym
.n_desc() & (N_WEAK_DEF
|N_WEAK_REF
)) == (N_WEAK_DEF
|N_WEAK_REF
) )
755 this->verifyAlignment();
759 friend class Parser
<A
>;
760 friend class Section
<A
>;
761 friend class CStringSection
<A
>;
762 friend class AbsoluteSymbolSection
<A
>;
767 mutable unsigned long _hash
;
769 uint64_t _fixupsStartIndex
: kFixupStartIndexBits
,
770 _lineInfoStartIndex
: kLineInfoStartIndexBits
,
771 _unwindInfoStartIndex
: kUnwindInfoStartIndexBits
,
772 _fixupsCount
: kFixupCountBits
,
773 _lineInfoCount
: kLineInfoCountBits
,
774 _unwindInfoCount
: kUnwindInfoCountBits
;
780 template <typename A
>
781 void Atom
<A
>::setFixupsRange(uint32_t startIndex
, uint32_t count
)
783 if ( count
>= (1 << kFixupCountBits
) )
784 throwf("too many fixups in function %s", this->name());
785 if ( startIndex
>= (1 << kFixupStartIndexBits
) )
786 throwf("too many fixups in file");
787 assert(((startIndex
+count
) <= sect().file()._fixups
.size()) && "fixup index out of range");
788 _fixupsStartIndex
= startIndex
;
789 _fixupsCount
= count
;
792 template <typename A
>
793 void Atom
<A
>::setUnwindInfoRange(uint32_t startIndex
, uint32_t count
)
795 if ( count
>= (1 << kUnwindInfoCountBits
) )
796 throwf("too many compact unwind infos in function %s", this->name());
797 if ( startIndex
>= (1 << kUnwindInfoStartIndexBits
) )
798 throwf("too many compact unwind infos (%d) in file", startIndex
);
799 assert((startIndex
+count
) <= sect().file()._unwindInfos
.size() && "unwindinfo index out of range");
800 _unwindInfoStartIndex
= startIndex
;
801 _unwindInfoCount
= count
;
804 template <typename A
>
805 void Atom
<A
>::extendUnwindInfoRange()
807 if ( _unwindInfoCount
+1 >= (1 << kUnwindInfoCountBits
) )
808 throwf("too many compact unwind infos in function %s", this->name());
809 _unwindInfoCount
+= 1;
812 template <typename A
>
813 void Atom
<A
>::setLineInfoRange(uint32_t startIndex
, uint32_t count
)
815 assert((count
< (1 << kLineInfoCountBits
)) && "too many line infos");
816 assert((startIndex
+count
) < sect().file()._lineInfos
.size() && "line info index out of range");
817 _lineInfoStartIndex
= startIndex
;
818 _lineInfoCount
= count
;
821 template <typename A
>
822 const uint8_t* Atom
<A
>::contentPointer() const
824 const macho_section
<P
>* sct
= this->sect().machoSection();
825 uint32_t fileOffset
= sct
->offset() - sct
->addr() + this->_objAddress
;
826 return this->sect().file().fileContent()+fileOffset
;
830 template <typename A
>
831 void Atom
<A
>::copyRawContent(uint8_t buffer
[]) const
834 if ( this->contentType() == ld::Atom::typeZeroFill
) {
835 bzero(buffer
, _size
);
837 else if ( _size
!= 0 ) {
838 memcpy(buffer
, this->contentPointer(), _size
);
843 void Atom
<arm
>::verifyAlignment() const
845 if ( (this->section().type() == ld::Section::typeCode
) && ! isThumb() ) {
846 if ( ((_objAddress
% 4) != 0) || (this->alignment().powerOf2
< 2) )
847 warning("ARM function not 4-byte aligned: %s from %s", this->name(), this->file()->path());
851 template <typename A
>
852 void Atom
<A
>::verifyAlignment() const
857 template <typename A
>
861 static bool validFile(const uint8_t* fileContent
, bool subtypeMustMatch
=false,
862 cpu_subtype_t subtype
=0);
863 static const char* fileKind(const uint8_t* fileContent
);
864 static bool hasObjC2Categories(const uint8_t* fileContent
);
865 static ld::relocatable::File
* parse(const uint8_t* fileContent
, uint64_t fileLength
,
866 const char* path
, time_t modTime
, uint32_t ordinal
,
867 const ParserOptions
& opts
) {
868 Parser
p(fileContent
, fileLength
, path
, modTime
,
869 ordinal
, opts
.convertUnwindInfo
);
870 return p
.parse(opts
);
873 typedef typename
A::P P
;
874 typedef typename
A::P::E E
;
875 typedef typename
A::P::uint_t pint_t
;
877 struct SourceLocation
{
879 SourceLocation(Atom
<A
>* a
, uint32_t o
) : atom(a
), offsetInAtom(o
) {}
881 uint32_t offsetInAtom
;
886 const char* name
; // only used if targetAtom is NULL
888 bool weakImport
; // only used if targetAtom is NULL
892 FixupInAtom(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, Atom
<A
>* target
) :
893 fixup(src
.offsetInAtom
, c
, k
, target
), atom(src
.atom
) { src
.atom
->incrementFixupCount(); }
895 FixupInAtom(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, ld::Fixup::TargetBinding b
, Atom
<A
>* target
) :
896 fixup(src
.offsetInAtom
, c
, k
, b
, target
), atom(src
.atom
) { src
.atom
->incrementFixupCount(); }
898 FixupInAtom(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, bool wi
, const char* name
) :
899 fixup(src
.offsetInAtom
, c
, k
, wi
, name
), atom(src
.atom
) { src
.atom
->incrementFixupCount(); }
901 FixupInAtom(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, ld::Fixup::TargetBinding b
, const char* name
) :
902 fixup(src
.offsetInAtom
, c
, k
, b
, name
), atom(src
.atom
) { src
.atom
->incrementFixupCount(); }
904 FixupInAtom(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, uint64_t addend
) :
905 fixup(src
.offsetInAtom
, c
, k
, addend
), atom(src
.atom
) { src
.atom
->incrementFixupCount(); }
907 FixupInAtom(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
) :
908 fixup(src
.offsetInAtom
, c
, k
, (uint64_t)0), atom(src
.atom
) { src
.atom
->incrementFixupCount(); }
914 void addFixup(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, Atom
<A
>* target
) {
915 _allFixups
.push_back(FixupInAtom(src
, c
, k
, target
));
918 void addFixup(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, ld::Fixup::TargetBinding b
, Atom
<A
>* target
) {
919 _allFixups
.push_back(FixupInAtom(src
, c
, k
, b
, target
));
922 void addFixup(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, bool wi
, const char* name
) {
923 _allFixups
.push_back(FixupInAtom(src
, c
, k
, wi
, name
));
926 void addFixup(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, ld::Fixup::TargetBinding b
, const char* name
) {
927 _allFixups
.push_back(FixupInAtom(src
, c
, k
, b
, name
));
930 void addFixup(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, uint64_t addend
) {
931 _allFixups
.push_back(FixupInAtom(src
, c
, k
, addend
));
934 void addFixup(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
) {
935 _allFixups
.push_back(FixupInAtom(src
, c
, k
));
939 uint32_t symbolCount() { return _symbolCount
; }
940 uint32_t indirectSymbol(uint32_t indirectIndex
);
941 const macho_nlist
<P
>& symbolFromIndex(uint32_t index
);
942 const char* nameFromSymbol(const macho_nlist
<P
>& sym
);
943 ld::Atom::Scope
scopeFromSymbol(const macho_nlist
<P
>& sym
);
944 static ld::Atom::Definition
definitionFromSymbol(const macho_nlist
<P
>& sym
);
945 static ld::Atom::Combine
combineFromSymbol(const macho_nlist
<P
>& sym
);
946 ld::Atom::SymbolTableInclusion
inclusionFromSymbol(const macho_nlist
<P
>& sym
);
947 static bool dontDeadStripFromSymbol(const macho_nlist
<P
>& sym
);
948 static bool isThumbFromSymbol(const macho_nlist
<P
>& sym
);
949 static bool weakImportFromSymbol(const macho_nlist
<P
>& sym
);
950 static bool resolverFromSymbol(const macho_nlist
<P
>& sym
);
951 uint32_t symbolIndexFromIndirectSectionAddress(pint_t
,const macho_section
<P
>*);
952 const macho_section
<P
>* firstMachOSection() { return _sectionsStart
; }
953 const macho_section
<P
>* machOSectionFromSectionIndex(uint32_t index
);
954 uint32_t machOSectionCount() { return _machOSectionsCount
; }
955 uint32_t undefinedStartIndex() { return _undefinedStartIndex
; }
956 uint32_t undefinedEndIndex() { return _undefinedEndIndex
; }
957 void addFixup(FixupInAtom f
) { _allFixups
.push_back(f
); }
958 Section
<A
>* sectionForNum(unsigned int sectNum
);
959 Section
<A
>* sectionForAddress(pint_t addr
);
960 Atom
<A
>* findAtomByAddress(pint_t addr
);
961 Atom
<A
>* findAtomByAddressOrNullIfStub(pint_t addr
);
962 Atom
<A
>* findAtomByAddressOrLocalTargetOfStub(pint_t addr
, uint32_t* offsetInAtom
);
963 Atom
<A
>* findAtomByName(const char* name
); // slow!
964 void findTargetFromAddress(pint_t addr
, TargetDesc
& target
);
965 void findTargetFromAddress(pint_t baseAddr
, pint_t addr
, TargetDesc
& target
);
966 void findTargetFromAddressAndSectionNum(pint_t addr
, unsigned int sectNum
,
968 uint32_t tentativeDefinitionCount() { return _tentativeDefinitionCount
; }
969 uint32_t absoluteSymbolCount() { return _absoluteSymbolCount
; }
971 bool hasStubsSection() { return (_stubsSectionNum
!= 0); }
972 unsigned int stubsSectionNum() { return _stubsSectionNum
; }
973 void addDtraceExtraInfos(const SourceLocation
& src
, const char* provider
);
974 const char* scanSymbolTableForAddress(uint64_t addr
);
975 bool convertUnwindInfo() { return _convertUnwindInfo
; }
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 uint32_t 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
;
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
;
1095 // filled in by parse()
1096 CFISection
<A
>* _EHFrameSection
;
1097 CUSection
<A
>* _compactUnwindSection
;
1098 AbsoluteSymbolSection
<A
>* _absoluteSection
;
1099 uint32_t _tentativeDefinitionCount
;
1100 uint32_t _absoluteSymbolCount
;
1101 uint32_t _symbolsInSections
;
1102 bool _hasLongBranchStubs
;
1103 bool _AppleObjc
; // FSF has objc that uses different data layout
1104 bool _overlappingSymbols
;
1105 bool _convertUnwindInfo
;
1106 unsigned int _stubsSectionNum
;
1107 const macho_section
<P
>* _stubsMachOSection
;
1108 std::vector
<const char*> _dtraceProviderInfo
;
1109 std::vector
<FixupInAtom
> _allFixups
;
1114 template <typename A
>
1115 Parser
<A
>::Parser(const uint8_t* fileContent
, uint64_t fileLength
, const char* path
, time_t modTime
,
1116 uint32_t ordinal
, bool convertDUI
)
1117 : _fileContent(fileContent
), _fileLength(fileLength
), _path(path
), _modTime(modTime
),
1118 _ordinal(ordinal
), _file(NULL
),
1119 _symbols(NULL
), _symbolCount(0), _strings(NULL
), _stringsSize(0),
1120 _indirectTable(NULL
), _indirectTableCount(0),
1121 _undefinedStartIndex(0), _undefinedEndIndex(0),
1122 _sectionsStart(NULL
), _machOSectionsCount(0), _hasUUID(false),
1123 _EHFrameSection(NULL
), _compactUnwindSection(NULL
), _absoluteSection(NULL
),
1124 _tentativeDefinitionCount(0), _absoluteSymbolCount(0),
1125 _symbolsInSections(0), _hasLongBranchStubs(false), _AppleObjc(false),
1126 _overlappingSymbols(false), _convertUnwindInfo(convertDUI
),
1127 _stubsSectionNum(0), _stubsMachOSection(NULL
)
1132 bool Parser
<ppc
>::validFile(const uint8_t* fileContent
, bool, cpu_subtype_t
)
1134 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1135 if ( header
->magic() != MH_MAGIC
)
1137 if ( header
->cputype() != CPU_TYPE_POWERPC
)
1139 if ( header
->filetype() != MH_OBJECT
)
1145 bool Parser
<ppc64
>::validFile(const uint8_t* fileContent
, bool, cpu_subtype_t
)
1147 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1148 if ( header
->magic() != MH_MAGIC_64
)
1150 if ( header
->cputype() != CPU_TYPE_POWERPC64
)
1152 if ( header
->filetype() != MH_OBJECT
)
1158 bool Parser
<x86
>::validFile(const uint8_t* fileContent
, bool, cpu_subtype_t
)
1160 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1161 if ( header
->magic() != MH_MAGIC
)
1163 if ( header
->cputype() != CPU_TYPE_I386
)
1165 if ( header
->filetype() != MH_OBJECT
)
1171 bool Parser
<x86_64
>::validFile(const uint8_t* fileContent
, bool, cpu_subtype_t
)
1173 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1174 if ( header
->magic() != MH_MAGIC_64
)
1176 if ( header
->cputype() != CPU_TYPE_X86_64
)
1178 if ( header
->filetype() != MH_OBJECT
)
1184 bool Parser
<arm
>::validFile(const uint8_t* fileContent
, bool subtypeMustMatch
, cpu_subtype_t subtype
)
1186 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1187 if ( header
->magic() != MH_MAGIC
)
1189 if ( header
->cputype() != CPU_TYPE_ARM
)
1191 if ( header
->filetype() != MH_OBJECT
)
1193 if ( subtypeMustMatch
) {
1194 if ( (cpu_subtype_t
)header
->cpusubtype() == subtype
)
1196 // hack until libcc_kext.a is made fat
1197 if ( header
->cpusubtype() == CPU_SUBTYPE_ARM_ALL
)
1206 const char* Parser
<ppc
>::fileKind(const uint8_t* fileContent
)
1208 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1209 if ( header
->magic() != MH_MAGIC
)
1211 if ( header
->cputype() != CPU_TYPE_POWERPC
)
1213 switch ( header
->cpusubtype() ) {
1214 case CPU_SUBTYPE_POWERPC_750
:
1216 case CPU_SUBTYPE_POWERPC_7400
:
1218 case CPU_SUBTYPE_POWERPC_7450
:
1220 case CPU_SUBTYPE_POWERPC_970
:
1222 case CPU_SUBTYPE_POWERPC_ALL
:
1229 const char* Parser
<ppc64
>::fileKind(const uint8_t* fileContent
)
1231 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1232 if ( header
->magic() != MH_MAGIC
)
1234 if ( header
->cputype() != CPU_TYPE_POWERPC64
)
1240 const char* Parser
<x86
>::fileKind(const uint8_t* fileContent
)
1242 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1243 if ( header
->magic() != MH_MAGIC
)
1245 if ( header
->cputype() != CPU_TYPE_I386
)
1251 const char* Parser
<x86_64
>::fileKind(const uint8_t* fileContent
)
1253 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1254 if ( header
->magic() != MH_MAGIC
)
1256 if ( header
->cputype() != CPU_TYPE_X86_64
)
1262 const char* Parser
<arm
>::fileKind(const uint8_t* fileContent
)
1264 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1265 if ( header
->magic() != MH_MAGIC
)
1267 if ( header
->cputype() != CPU_TYPE_ARM
)
1269 for (const ARMSubType
* t
=ARMSubTypes
; t
->subTypeName
!= NULL
; ++t
) {
1270 if ( t
->subType
== (cpu_subtype_t
)header
->cpusubtype() ) {
1271 return t
->subTypeName
;
1278 template <typename A
>
1279 bool Parser
<A
>::hasObjC2Categories(const uint8_t* fileContent
)
1281 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1282 const uint32_t cmd_count
= header
->ncmds();
1283 const macho_load_command
<P
>* const cmds
= (macho_load_command
<P
>*)((char*)header
+ sizeof(macho_header
<P
>));
1284 const macho_load_command
<P
>* const cmdsEnd
= (macho_load_command
<P
>*)((char*)header
+ sizeof(macho_header
<P
>) + header
->sizeofcmds());
1285 const macho_load_command
<P
>* cmd
= cmds
;
1286 for (uint32_t i
= 0; i
< cmd_count
; ++i
) {
1287 if ( cmd
->cmd() == macho_segment_command
<P
>::CMD
) {
1288 const macho_segment_command
<P
>* segment
= (macho_segment_command
<P
>*)cmd
;
1289 const macho_section
<P
>* sectionsStart
= (macho_section
<P
>*)((char*)segment
+ sizeof(macho_segment_command
<P
>));
1290 for (uint32_t si
=0; si
< segment
->nsects(); ++si
) {
1291 const macho_section
<P
>* sect
= §ionsStart
[si
];
1292 if ( (sect
->size() > 0)
1293 && (strcmp(sect
->sectname(), "__objc_catlist") == 0)
1294 && (strcmp(sect
->segname(), "__DATA") == 0) ) {
1299 cmd
= (const macho_load_command
<P
>*)(((char*)cmd
)+cmd
->cmdsize());
1300 if ( cmd
> cmdsEnd
)
1301 throwf("malformed mach-o file, load command #%d is outside size of load commands", i
);
1306 template <typename A
>
1307 int Parser
<A
>::pointerSorter(const void* l
, const void* r
)
1309 // sort references by address
1310 const pint_t
* left
= (pint_t
*)l
;
1311 const pint_t
* right
= (pint_t
*)r
;
1312 return (*left
- *right
);
1315 template <typename A
>
1316 typename
A::P::uint_t Parser
<A
>::LabelAndCFIBreakIterator::peek(Parser
<A
>& parser
, pint_t startAddr
, pint_t endAddr
)
1319 if ( symIndex
< sortedSymbolCount
)
1320 symbolAddr
= parser
.symbolFromIndex(sortedSymbolIndexes
[symIndex
]).n_value();
1322 symbolAddr
= endAddr
;
1324 if ( cfiIndex
< cfiStartsCount
)
1325 cfiAddr
= cfiStartsArray
[cfiIndex
];
1328 if ( (cfiAddr
< symbolAddr
) && (cfiAddr
>= startAddr
) ) {
1329 if ( cfiAddr
< endAddr
)
1335 if ( symbolAddr
< endAddr
)
1343 // Parses up a section into chunks based on labels and CFI information.
1344 // Each call returns the next chunk address and size, and (if the break
1345 // was becuase of a label, the symbol). Returns false when no more chunks.
1347 template <typename A
>
1348 bool Parser
<A
>::LabelAndCFIBreakIterator::next(Parser
<A
>& parser
, uint32_t sectNum
, pint_t startAddr
, pint_t endAddr
,
1349 pint_t
* addr
, pint_t
* size
, const macho_nlist
<P
>** symbol
)
1351 // may not be a label on start of section, but need atom demarcation there
1354 // advance symIndex until we get to the first label at or past the start of this section
1355 while ( symIndex
< sortedSymbolCount
) {
1356 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(sortedSymbolIndexes
[symIndex
]);
1357 pint_t nextSymbolAddr
= sym
.n_value();
1358 //fprintf(stderr, "sectNum=%d, nextSymbolAddr=0x%08llX, name=%s\n", sectNum, (uint64_t)nextSymbolAddr, parser.nameFromSymbol(sym));
1359 if ( (nextSymbolAddr
> startAddr
) || ((nextSymbolAddr
== startAddr
) && (sym
.n_sect() == sectNum
)) )
1363 if ( symIndex
< sortedSymbolCount
) {
1364 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(sortedSymbolIndexes
[symIndex
]);
1365 pint_t nextSymbolAddr
= sym
.n_value();
1366 // if next symbol found is not in this section
1367 if ( sym
.n_sect() != sectNum
) {
1368 // check for CFI break instead of symbol break
1369 if ( cfiIndex
< cfiStartsCount
) {
1370 pint_t nextCfiAddr
= cfiStartsArray
[cfiIndex
];
1371 if ( nextCfiAddr
< endAddr
) {
1374 *addr
= nextCfiAddr
;
1375 *size
= peek(parser
, startAddr
, endAddr
) - nextCfiAddr
;
1381 *size
= endAddr
- startAddr
;
1383 if ( startAddr
== endAddr
)
1384 return false; // zero size section
1386 return true; // whole section is one atom with no label
1388 // if also CFI break here, eat it
1389 if ( cfiIndex
< cfiStartsCount
) {
1390 if ( cfiStartsArray
[cfiIndex
] == nextSymbolAddr
)
1393 if ( nextSymbolAddr
== startAddr
) {
1394 // label at start of section, return it as chunk
1397 *size
= peek(parser
, startAddr
, endAddr
) - startAddr
;
1401 // return chunk before first symbol
1403 *size
= nextSymbolAddr
- startAddr
;
1407 // no symbols left in whole file, so entire section is one chunk
1409 *size
= endAddr
- startAddr
;
1411 if ( startAddr
== endAddr
)
1412 return false; // zero size section
1414 return true; // whole section is one atom with no label
1417 while ( (symIndex
< sortedSymbolCount
) && (cfiIndex
< cfiStartsCount
) ) {
1418 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(sortedSymbolIndexes
[symIndex
]);
1419 pint_t nextSymbolAddr
= sym
.n_value();
1420 pint_t nextCfiAddr
= cfiStartsArray
[cfiIndex
];
1421 if ( nextSymbolAddr
< nextCfiAddr
) {
1422 if ( nextSymbolAddr
>= endAddr
)
1425 if ( nextSymbolAddr
< startAddr
)
1427 *addr
= nextSymbolAddr
;
1428 *size
= peek(parser
, startAddr
, endAddr
) - nextSymbolAddr
;
1432 else if ( nextCfiAddr
< nextSymbolAddr
) {
1433 if ( nextCfiAddr
>= endAddr
)
1436 if ( nextCfiAddr
< startAddr
)
1438 *addr
= nextCfiAddr
;
1439 *size
= peek(parser
, startAddr
, endAddr
) - nextCfiAddr
;
1444 if ( nextCfiAddr
>= endAddr
)
1448 if ( nextCfiAddr
< startAddr
)
1450 *addr
= nextCfiAddr
;
1451 *size
= peek(parser
, startAddr
, endAddr
) - nextCfiAddr
;
1456 while ( symIndex
< sortedSymbolCount
) {
1457 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(sortedSymbolIndexes
[symIndex
]);
1458 pint_t nextSymbolAddr
= sym
.n_value();
1459 // if next symbol found is not in this section, then done with iteration
1460 if ( sym
.n_sect() != sectNum
)
1463 if ( nextSymbolAddr
< startAddr
)
1465 *addr
= nextSymbolAddr
;
1466 *size
= peek(parser
, startAddr
, endAddr
) - nextSymbolAddr
;
1470 while ( cfiIndex
< cfiStartsCount
) {
1471 pint_t nextCfiAddr
= cfiStartsArray
[cfiIndex
];
1472 if ( nextCfiAddr
>= endAddr
)
1475 if ( nextCfiAddr
< startAddr
)
1477 *addr
= nextCfiAddr
;
1478 *size
= peek(parser
, startAddr
, endAddr
) - nextCfiAddr
;
1487 template <typename A
>
1488 ld::relocatable::File
* Parser
<A
>::parse(const ParserOptions
& opts
)
1490 // create file object
1491 _file
= new File
<A
>(_path
, _modTime
, _fileContent
, _ordinal
);
1493 // respond to -t option
1494 if ( opts
.logAllFiles
)
1495 printf("%s\n", _path
);
1497 // parse start of mach-o file
1498 if ( ! parseLoadCommands() )
1502 uint32_t sortedSectionIndexes
[_machOSectionsCount
];
1503 this->makeSortedSectionsArray(sortedSectionIndexes
);
1505 // make symbol table sorted by address
1506 this->prescanSymbolTable();
1507 uint32_t sortedSymbolIndexes
[_symbolsInSections
];
1508 this->makeSortedSymbolsArray(sortedSymbolIndexes
, sortedSectionIndexes
);
1510 // allocate Section<A> object for each mach-o section
1513 // if it exists, do special early parsing of __compact_unwind section
1514 uint32_t countOfCUs
= 0;
1515 if ( _compactUnwindSection
!= NULL
)
1516 countOfCUs
= _compactUnwindSection
->count();
1517 uint8_t cuInfoBuffer
[sizeof(typename CUSection
<A
>::Info
) * countOfCUs
];
1518 typename CUSection
<A
>::Info
* cuInfoArray
= (typename CUSection
<A
>::Info
*)cuInfoBuffer
;
1519 if ( countOfCUs
!= 0 )
1520 _compactUnwindSection
->parse(*this, countOfCUs
, cuInfoArray
);
1522 // if it exists, do special early parsing of __eh_frame section
1523 // stack allocate array of CFI_Atom_Info
1524 uint32_t countOfCFIs
= 0;
1525 if ( _EHFrameSection
!= NULL
)
1526 countOfCFIs
= _EHFrameSection
->cfiCount();
1527 typename CFISection
<A
>::CFI_Atom_Info cfiArray
[countOfCFIs
];
1528 // stack allocate (if not too large) a copy of __eh_frame to apply relocations to
1529 uint8_t* ehBuffer
= NULL
;
1530 uint32_t stackAllocSize
= 0;
1531 if ( (countOfCFIs
!= 0) && _EHFrameSection
->needsRelocating() ) {
1532 uint32_t sectSize
= _EHFrameSection
->machoSection()->size();
1533 if ( sectSize
> 50*1024 )
1534 ehBuffer
= (uint8_t*)malloc(sectSize
);
1536 stackAllocSize
= sectSize
;
1538 uint32_t ehStackBuffer
[1+stackAllocSize
/4]; // make 4-byte aligned stack bufffer
1539 if ( ehBuffer
== NULL
)
1540 ehBuffer
= (uint8_t*)&ehStackBuffer
;
1541 uint32_t cfiStartsCount
= 0;
1542 if ( countOfCFIs
!= 0 ) {
1543 _EHFrameSection
->cfiParse(*this, ehBuffer
, cfiArray
, countOfCFIs
);
1544 // count functions and lsdas
1545 for(uint32_t i
=0; i
< countOfCFIs
; ++i
) {
1546 if ( cfiArray
[i
].isCIE
)
1548 //fprintf(stderr, "cfiArray[i].func = 0x%08llX, cfiArray[i].lsda = 0x%08llX, encoding=0x%08X\n",
1549 // (uint64_t)cfiArray[i].u.fdeInfo.function.targetAddress,
1550 // (uint64_t)cfiArray[i].u.fdeInfo.lsda.targetAddress,
1551 // cfiArray[i].u.fdeInfo.compactUnwindInfo);
1552 if ( cfiArray
[i
].u
.fdeInfo
.function
.targetAddress
!= CFI_INVALID_ADDRESS
)
1554 if ( cfiArray
[i
].u
.fdeInfo
.lsda
.targetAddress
!= CFI_INVALID_ADDRESS
)
1558 CFI_CU_InfoArrays
cfis(cfiArray
, countOfCFIs
, cuInfoArray
, countOfCUs
);
1560 // create sorted array of function starts and lsda starts
1561 pint_t cfiStartsArray
[cfiStartsCount
];
1562 uint32_t countOfFDEs
= 0;
1563 if ( countOfCFIs
!= 0 ) {
1565 for(uint32_t i
=0; i
< countOfCFIs
; ++i
) {
1566 if ( cfiArray
[i
].isCIE
)
1568 if ( cfiArray
[i
].u
.fdeInfo
.function
.targetAddress
!= CFI_INVALID_ADDRESS
)
1569 cfiStartsArray
[index
++] = cfiArray
[i
].u
.fdeInfo
.function
.targetAddress
;
1570 if ( cfiArray
[i
].u
.fdeInfo
.lsda
.targetAddress
!= CFI_INVALID_ADDRESS
)
1571 cfiStartsArray
[index
++] = cfiArray
[i
].u
.fdeInfo
.lsda
.targetAddress
;
1574 ::qsort(cfiStartsArray
, cfiStartsCount
, sizeof(pint_t
), pointerSorter
);
1576 // scan for FDEs claming the same function
1577 for(int i
=1; i
< index
; ++i
) {
1578 assert( cfiStartsArray
[i
] != cfiStartsArray
[i
-1] );
1583 Section
<A
>** sections
= _file
->_sectionsArray
;
1584 uint32_t sectionsCount
= _file
->_sectionsArrayCount
;
1586 // figure out how many atoms will be allocated and allocate
1587 LabelAndCFIBreakIterator
breakIterator(sortedSymbolIndexes
, _symbolsInSections
, cfiStartsArray
,
1588 cfiStartsCount
, _overlappingSymbols
);
1589 uint32_t computedAtomCount
= 0;
1590 for (uint32_t i
=0; i
< sectionsCount
; ++i
) {
1591 breakIterator
.beginSection();
1592 uint32_t count
= sections
[i
]->computeAtomCount(*this, breakIterator
, cfis
);
1593 //const macho_section<P>* sect = sections[i]->machoSection();
1594 //fprintf(stderr, "computed count=%u for section %s size=%llu\n", count, sect->sectname(), (sect != NULL) ? sect->size() : 0);
1595 computedAtomCount
+= count
;
1597 //fprintf(stderr, "allocating %d atoms * sizeof(Atom<A>)=%ld, sizeof(ld::Atom)=%ld\n", computedAtomCount, sizeof(Atom<A>), sizeof(ld::Atom));
1598 _file
->_atomsArray
= new uint8_t[computedAtomCount
*sizeof(Atom
<A
>)];
1599 _file
->_atomsArrayCount
= 0;
1601 // have each section append atoms to _atomsArray
1602 LabelAndCFIBreakIterator
breakIterator2(sortedSymbolIndexes
, _symbolsInSections
, cfiStartsArray
,
1603 cfiStartsCount
, _overlappingSymbols
);
1604 for (uint32_t i
=0; i
< sectionsCount
; ++i
) {
1605 uint8_t* atoms
= _file
->_atomsArray
+ _file
->_atomsArrayCount
*sizeof(Atom
<A
>);
1606 breakIterator2
.beginSection();
1607 uint32_t count
= sections
[i
]->appendAtoms(*this, atoms
, breakIterator2
, cfis
);
1608 //fprintf(stderr, "append count=%u for section %s/%s\n", count, sections[i]->machoSection()->segname(), sections[i]->machoSection()->sectname());
1609 _file
->_atomsArrayCount
+= count
;
1611 assert( _file
->_atomsArrayCount
== computedAtomCount
&& "more atoms allocated than expected");
1614 // have each section add all fix-ups for its atoms
1615 _allFixups
.reserve(computedAtomCount
*5);
1616 for (uint32_t i
=0; i
< sectionsCount
; ++i
)
1617 sections
[i
]->makeFixups(*this, cfis
);
1619 // assign fixups start offset for each atom
1620 uint8_t* p
= _file
->_atomsArray
;
1621 uint32_t fixupOffset
= 0;
1622 for(int i
=_file
->_atomsArrayCount
; i
> 0; --i
) {
1623 Atom
<A
>* atom
= (Atom
<A
>*)p
;
1624 atom
->_fixupsStartIndex
= fixupOffset
;
1625 fixupOffset
+= atom
->_fixupsCount
;
1626 atom
->_fixupsCount
= 0;
1627 p
+= sizeof(Atom
<A
>);
1629 assert(fixupOffset
== _allFixups
.size());
1630 _file
->_fixups
.reserve(fixupOffset
);
1632 // copy each fixup for each atom
1633 for(typename
std::vector
<FixupInAtom
>::iterator it
=_allFixups
.begin(); it
!= _allFixups
.end(); ++it
) {
1634 uint32_t slot
= it
->atom
->_fixupsStartIndex
+ it
->atom
->_fixupsCount
;
1635 _file
->_fixups
[slot
] = it
->fixup
;
1636 it
->atom
->_fixupsCount
++;
1639 // done with temp vector
1643 _file
->_unwindInfos
.reserve(countOfFDEs
+countOfCUs
);
1644 for(uint32_t i
=0; i
< countOfCFIs
; ++i
) {
1645 if ( cfiArray
[i
].isCIE
)
1647 if ( cfiArray
[i
].u
.fdeInfo
.function
.targetAddress
!= CFI_INVALID_ADDRESS
) {
1648 ld::Atom::UnwindInfo info
;
1649 info
.startOffset
= 0;
1650 info
.unwindInfo
= cfiArray
[i
].u
.fdeInfo
.compactUnwindInfo
;
1651 _file
->_unwindInfos
.push_back(info
);
1652 Atom
<A
>* func
= findAtomByAddress(cfiArray
[i
].u
.fdeInfo
.function
.targetAddress
);
1653 func
->setUnwindInfoRange(_file
->_unwindInfos
.size()-1, 1);
1656 // apply compact infos in __LD,__compact_unwind section to each function
1657 // if function also has dwarf unwind, CU will override it
1658 Atom
<A
>* lastFunc
= NULL
;
1659 uint32_t lastEnd
= 0;
1660 for(uint32_t i
=0; i
< countOfCUs
; ++i
) {
1661 typename CUSection
<A
>::Info
* info
= &cuInfoArray
[i
];
1662 assert(info
->function
!= NULL
);
1663 ld::Atom::UnwindInfo ui
;
1664 ui
.startOffset
= info
->functionStartAddress
- info
->function
->objectAddress();
1665 ui
.unwindInfo
= info
->compactUnwindInfo
;
1666 _file
->_unwindInfos
.push_back(ui
);
1667 // if previous is for same function, extend range
1668 if ( info
->function
== lastFunc
) {
1669 if ( lastEnd
!= ui
.startOffset
) {
1670 if ( lastEnd
< ui
.startOffset
)
1671 warning("__LD,__compact_unwind entries for %s have a gap at offset 0x%0X", info
->function
->name(), lastEnd
);
1673 warning("__LD,__compact_unwind entries for %s overlap at offset 0x%0X", info
->function
->name(), lastEnd
);
1675 lastFunc
->extendUnwindInfoRange();
1678 info
->function
->setUnwindInfoRange(_file
->_unwindInfos
.size()-1, 1);
1679 lastFunc
= info
->function
;
1680 lastEnd
= ui
.startOffset
+ info
->rangeLength
;
1683 // parse dwarf debug info to get line info
1684 this->parseDebugInfo();
1691 template <> uint8_t Parser
<ppc
>::loadCommandSizeMask() { return 0x03; }
1692 template <> uint8_t Parser
<ppc64
>::loadCommandSizeMask() { return 0x07; }
1693 template <> uint8_t Parser
<x86
>::loadCommandSizeMask() { return 0x03; }
1694 template <> uint8_t Parser
<x86_64
>::loadCommandSizeMask() { return 0x07; }
1695 template <> uint8_t Parser
<arm
>::loadCommandSizeMask() { return 0x03; }
1697 template <typename A
>
1698 bool Parser
<A
>::parseLoadCommands()
1700 const macho_header
<P
>* header
= (const macho_header
<P
>*)_fileContent
;
1702 // set File attributes
1703 _file
->_canScatterAtoms
= (header
->flags() & MH_SUBSECTIONS_VIA_SYMBOLS
);
1704 _file
->_cpuSubType
= header
->cpusubtype();
1706 const macho_segment_command
<P
>* segment
= NULL
;
1707 const uint8_t* const endOfFile
= _fileContent
+ _fileLength
;
1708 const uint32_t cmd_count
= header
->ncmds();
1709 // <rdar://problem/5394172> an empty .o file with zero load commands will crash linker
1710 if ( cmd_count
== 0 )
1712 const macho_load_command
<P
>* const cmds
= (macho_load_command
<P
>*)((char*)header
+ sizeof(macho_header
<P
>));
1713 const macho_load_command
<P
>* const cmdsEnd
= (macho_load_command
<P
>*)((char*)header
+ sizeof(macho_header
<P
>) + header
->sizeofcmds());
1714 const macho_load_command
<P
>* cmd
= cmds
;
1715 for (uint32_t i
= 0; i
< cmd_count
; ++i
) {
1716 uint32_t size
= cmd
->cmdsize();
1717 if ( (size
& this->loadCommandSizeMask()) != 0 )
1718 throwf("load command #%d has a unaligned size", i
);
1719 const uint8_t* endOfCmd
= ((uint8_t*)cmd
)+cmd
->cmdsize();
1720 if ( endOfCmd
> (uint8_t*)cmdsEnd
)
1721 throwf("load command #%d extends beyond the end of the load commands", i
);
1722 if ( endOfCmd
> endOfFile
)
1723 throwf("load command #%d extends beyond the end of the file", i
);
1724 switch (cmd
->cmd()) {
1727 const macho_symtab_command
<P
>* symtab
= (macho_symtab_command
<P
>*)cmd
;
1728 _symbolCount
= symtab
->nsyms();
1729 _symbols
= (const macho_nlist
<P
>*)(_fileContent
+ symtab
->symoff());
1730 _strings
= (char*)_fileContent
+ symtab
->stroff();
1731 _stringsSize
= symtab
->strsize();
1732 if ( (symtab
->symoff() + _symbolCount
*sizeof(macho_nlist
<P
>)) > _fileLength
)
1733 throw "mach-o symbol table extends beyond end of file";
1734 if ( (_strings
+ _stringsSize
) > (char*)endOfFile
)
1735 throw "mach-o string pool extends beyond end of file";
1736 if ( _indirectTable
== NULL
) {
1737 if ( _undefinedEndIndex
== 0 ) {
1738 _undefinedStartIndex
= 0;
1739 _undefinedEndIndex
= symtab
->nsyms();
1746 const macho_dysymtab_command
<P
>* dsymtab
= (macho_dysymtab_command
<P
>*)cmd
;
1747 _indirectTable
= (uint32_t*)(_fileContent
+ dsymtab
->indirectsymoff());
1748 _indirectTableCount
= dsymtab
->nindirectsyms();
1749 if ( &_indirectTable
[_indirectTableCount
] > (uint32_t*)endOfFile
)
1750 throw "indirect symbol table extends beyond end of file";
1751 _undefinedStartIndex
= dsymtab
->iundefsym();
1752 _undefinedEndIndex
= _undefinedStartIndex
+ dsymtab
->nundefsym();
1760 if ( cmd
->cmd() == macho_segment_command
<P
>::CMD
) {
1761 if ( segment
!= NULL
)
1762 throw "more than one LC_SEGMENT found in object file";
1763 segment
= (macho_segment_command
<P
>*)cmd
;
1767 cmd
= (const macho_load_command
<P
>*)(((char*)cmd
)+cmd
->cmdsize());
1768 if ( cmd
> cmdsEnd
)
1769 throwf("malformed mach-o file, load command #%d is outside size of load commands", i
);
1772 // record range of sections
1773 if ( segment
== NULL
)
1774 throw "missing LC_SEGMENT";
1775 _sectionsStart
= (macho_section
<P
>*)((char*)segment
+ sizeof(macho_segment_command
<P
>));
1776 _machOSectionsCount
= segment
->nsects();
1782 template <typename A
>
1783 void Parser
<A
>::prescanSymbolTable()
1785 _tentativeDefinitionCount
= 0;
1786 _absoluteSymbolCount
= 0;
1787 _symbolsInSections
= 0;
1788 for (uint32_t i
=0; i
< this->_symbolCount
; ++i
) {
1789 const macho_nlist
<P
>& sym
= symbolFromIndex(i
);
1791 if ( (sym
.n_type() & N_STAB
) != 0 )
1794 // look at undefines
1795 const char* symbolName
= this->nameFromSymbol(sym
);
1796 if ( (sym
.n_type() & N_TYPE
) == N_UNDF
) {
1797 if ( sym
.n_value() != 0 ) {
1798 // count tentative definitions
1799 ++_tentativeDefinitionCount
;
1801 else if ( strncmp(symbolName
, "___dtrace_", 10) == 0 ) {
1802 // any undefined starting with __dtrace_*$ that is not ___dtrace_probe$* or ___dtrace_isenabled$*
1803 // is extra provider info
1804 if ( (strncmp(&symbolName
[10], "probe$", 6) != 0) && (strncmp(&symbolName
[10], "isenabled$", 10) != 0) ) {
1805 _dtraceProviderInfo
.push_back(symbolName
);
1811 // count absolute symbols
1812 if ( (sym
.n_type() & N_TYPE
) == N_ABS
) {
1813 const char* absName
= this->nameFromSymbol(sym
);
1814 // ignore .objc_class_name_* symbols
1815 if ( strncmp(absName
, ".objc_class_name_", 17) == 0 ) {
1819 // ignore .objc_class_name_* symbols
1820 if ( strncmp(absName
, ".objc_category_name_", 20) == 0 )
1822 // ignore empty *.eh symbols
1823 if ( strcmp(&absName
[strlen(absName
)-3], ".eh") == 0 )
1825 ++_absoluteSymbolCount
;
1828 // only look at definitions
1829 if ( (sym
.n_type() & N_TYPE
) != N_SECT
)
1832 // 'L' labels do not denote atom breaks
1833 if ( symbolName
[0] == 'L' )
1836 // how many def syms in each section
1837 if ( sym
.n_sect() > _machOSectionsCount
)
1838 throw "bad n_sect in symbol table";
1840 _symbolsInSections
++;
1844 template <typename A
>
1845 int Parser
<A
>::sectionIndexSorter(void* extra
, const void* l
, const void* r
)
1847 Parser
<A
>* parser
= (Parser
<A
>*)extra
;
1848 const uint32_t* left
= (uint32_t*)l
;
1849 const uint32_t* right
= (uint32_t*)r
;
1850 const macho_section
<P
>* leftSect
= parser
->machOSectionFromSectionIndex(*left
);
1851 const macho_section
<P
>* rightSect
= parser
->machOSectionFromSectionIndex(*right
);
1853 // can't just return difference because 64-bit diff does not fit in 32-bit return type
1854 int64_t result
= leftSect
->addr() - rightSect
->addr();
1855 if ( result
== 0 ) {
1856 // two sections with same start address
1857 // one with zero size goes first
1858 bool leftEmpty
= ( leftSect
->size() == 0 );
1859 bool rightEmpty
= ( rightSect
->size() == 0 );
1860 if ( leftEmpty
!= rightEmpty
) {
1861 return ( rightEmpty
? 1 : -1 );
1863 if ( !leftEmpty
&& !rightEmpty
)
1864 throwf("overlapping sections");
1865 // both empty, so chose file order
1866 return ( rightSect
- leftSect
);
1868 else if ( result
< 0 )
1874 template <typename A
>
1875 void Parser
<A
>::makeSortedSectionsArray(uint32_t array
[])
1877 const bool log
= false;
1880 fprintf(stderr
, "unsorted sections:\n");
1881 for(unsigned int i
=0; i
< _machOSectionsCount
; ++i
)
1882 fprintf(stderr
, "0x%08llX %s %s\n", _sectionsStart
[i
].addr(), _sectionsStart
[i
].segname(), _sectionsStart
[i
].sectname());
1885 // sort by symbol table address
1886 for (uint32_t i
=0; i
< _machOSectionsCount
; ++i
)
1888 ::qsort_r(array
, _machOSectionsCount
, sizeof(uint32_t), this, §ionIndexSorter
);
1891 fprintf(stderr
, "sorted sections:\n");
1892 for(unsigned int i
=0; i
< _machOSectionsCount
; ++i
)
1893 fprintf(stderr
, "0x%08llX %s %s\n", _sectionsStart
[array
[i
]].addr(), _sectionsStart
[array
[i
]].segname(), _sectionsStart
[array
[i
]].sectname());
1899 template <typename A
>
1900 int Parser
<A
>::symbolIndexSorter(void* extra
, const void* l
, const void* r
)
1902 ParserAndSectionsArray
* extraInfo
= (ParserAndSectionsArray
*)extra
;
1903 Parser
<A
>* parser
= extraInfo
->parser
;
1904 const uint32_t* sortedSectionsArray
= extraInfo
->sortedSectionsArray
;
1905 const uint32_t* left
= (uint32_t*)l
;
1906 const uint32_t* right
= (uint32_t*)r
;
1907 const macho_nlist
<P
>& leftSym
= parser
->symbolFromIndex(*left
);
1908 const macho_nlist
<P
>& rightSym
= parser
->symbolFromIndex(*right
);
1909 // can't just return difference because 64-bit diff does not fit in 32-bit return type
1910 int64_t result
= leftSym
.n_value() - rightSym
.n_value();
1911 if ( result
== 0 ) {
1912 // two symbols with same address
1913 // if in different sections, sort earlier section first
1914 if ( leftSym
.n_sect() != rightSym
.n_sect() ) {
1915 for (uint32_t i
=0; i
< parser
->machOSectionCount(); ++i
) {
1916 if ( sortedSectionsArray
[i
]+1 == leftSym
.n_sect() )
1918 if ( sortedSectionsArray
[i
]+1 == rightSym
.n_sect() )
1922 // two symbols in same section, means one is an alias
1923 // if only one is global, make the other an alias (sort first)
1924 if ( (leftSym
.n_type() & N_EXT
) != (rightSym
.n_type() & N_EXT
) ) {
1925 if ( (rightSym
.n_type() & N_EXT
) != 0 )
1930 // if both are global, make alphabetically last one be the alias
1931 return ( strcmp(parser
->nameFromSymbol(rightSym
), parser
->nameFromSymbol(leftSym
)) );
1933 else if ( result
< 0 )
1940 template <typename A
>
1941 void Parser
<A
>::makeSortedSymbolsArray(uint32_t array
[], const uint32_t sectionArray
[])
1943 const bool log
= false;
1945 uint32_t* p
= array
;
1946 for (uint32_t i
=0; i
< this->_symbolCount
; ++i
) {
1947 const macho_nlist
<P
>& sym
= symbolFromIndex(i
);
1949 if ( (sym
.n_type() & N_STAB
) != 0 )
1952 // only look at definitions
1953 if ( (sym
.n_type() & N_TYPE
) != N_SECT
)
1956 // 'L' labels do not denote atom breaks
1957 const char* symbolName
= this->nameFromSymbol(sym
);
1958 if ( symbolName
[0] == 'L' )
1961 // how many def syms in each section
1962 if ( sym
.n_sect() > _machOSectionsCount
)
1963 throw "bad n_sect in symbol table";
1968 assert(p
== &array
[_symbolsInSections
] && "second pass over symbol table yield a different number of symbols");
1970 // sort by symbol table address
1971 ParserAndSectionsArray extra
= { this, sectionArray
};
1972 ::qsort_r(array
, _symbolsInSections
, sizeof(uint32_t), &extra
, &symbolIndexSorter
);
1974 // look for two symbols at same address
1975 _overlappingSymbols
= false;
1976 for (unsigned int i
=1; i
< _symbolsInSections
; ++i
) {
1977 if ( symbolFromIndex(array
[i
-1]).n_value() == symbolFromIndex(array
[i
]).n_value() ) {
1978 //fprintf(stderr, "overlapping symbols at 0x%08llX\n", symbolFromIndex(array[i-1]).n_value());
1979 _overlappingSymbols
= true;
1984 fprintf(stderr
, "sorted symbols:\n");
1985 for(unsigned int i
=0; i
< _symbolsInSections
; ++i
)
1986 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
])) );
1991 template <typename A
>
1992 void Parser
<A
>::makeSections()
1994 // classify each section by type
1995 // compute how many Section objects will be needed and total size for all
1996 unsigned int totalSectionsSize
= 0;
1997 uint8_t machOSectsStorage
[sizeof(MachOSectionAndSectionClass
<P
>)*(_machOSectionsCount
+2)]; // also room for tentative-defs and absolute symbols
1998 // allocate raw storage for all section objects on stack
1999 MachOSectionAndSectionClass
<P
>* machOSects
= (MachOSectionAndSectionClass
<P
>*)machOSectsStorage
;
2000 unsigned int count
= 0;
2001 for (uint32_t i
=0; i
< _machOSectionsCount
; ++i
) {
2002 const macho_section
<P
>* sect
= &_sectionsStart
[i
];
2003 if ( (sect
->flags() & S_ATTR_DEBUG
) != 0 ) {
2004 if ( strcmp(sect
->segname(), "__DWARF") == 0 ) {
2005 // note that .o file has dwarf
2006 _file
->_debugInfoKind
= ld::relocatable::File::kDebugInfoDwarf
;
2007 // save off iteresting dwarf sections
2008 if ( strcmp(sect
->sectname(), "__debug_info") == 0 )
2009 _file
->_dwarfDebugInfoSect
= sect
;
2010 else if ( strcmp(sect
->sectname(), "__debug_abbrev") == 0 )
2011 _file
->_dwarfDebugAbbrevSect
= sect
;
2012 else if ( strcmp(sect
->sectname(), "__debug_line") == 0 )
2013 _file
->_dwarfDebugLineSect
= sect
;
2014 else if ( strcmp(sect
->sectname(), "__debug_str") == 0 )
2015 _file
->_dwarfDebugStringSect
= sect
;
2016 // linker does not propagate dwarf sections to output file
2019 else if ( strcmp(sect
->segname(), "__LD") == 0 ) {
2020 if ( strncmp(sect
->sectname(), "__compact_unwind", 16) == 0 ) {
2021 machOSects
[count
].sect
= sect
;
2022 totalSectionsSize
+= sizeof(CUSection
<A
>);
2023 machOSects
[count
++].type
= sectionTypeCompactUnwind
;
2028 // ignore empty __OBJC sections
2029 if ( (sect
->size() == 0) && (strcmp(sect
->segname(), "__OBJC") == 0) )
2031 // objc image info section is really attributes and not content
2032 if ( ((strcmp(sect
->sectname(), "__image_info") == 0) && (strcmp(sect
->segname(), "__OBJC") == 0))
2033 || ((strncmp(sect
->sectname(), "__objc_imageinfo", 16) == 0) && (strcmp(sect
->segname(), "__DATA") == 0)) ) {
2034 // struct objc_image_info {
2035 // uint32_t version; // initially 0
2038 // #define OBJC_IMAGE_SUPPORTS_GC 2
2039 // #define OBJC_IMAGE_GC_ONLY 4
2041 const uint32_t* contents
= (uint32_t*)(_file
->fileContent()+sect
->offset());
2042 if ( (sect
->size() >= 8) && (contents
[0] == 0) ) {
2043 uint32_t flags
= E::get32(contents
[1]);
2044 if ( (flags
& 4) == 4 )
2045 _file
->_objConstraint
= ld::File::objcConstraintGC
;
2046 else if ( (flags
& 2) == 2 )
2047 _file
->_objConstraint
= ld::File::objcConstraintRetainReleaseOrGC
;
2049 _file
->_objConstraint
= ld::File::objcConstraintRetainRelease
;
2050 if ( (flags
& 1) == 1 )
2051 _file
->_ojcReplacmentClass
= true;
2052 if ( sect
->size() > 8 ) {
2053 warning("section %s/%s has unexpectedly large size %llu in %s",
2054 sect
->segname(), Section
<A
>::makeSectionName(sect
), sect
->size(), _file
->path());
2058 warning("can't parse %s/%s section in %s", sect
->segname(), Section
<A
>::makeSectionName(sect
), _file
->path());
2062 machOSects
[count
].sect
= sect
;
2063 switch ( sect
->flags() & SECTION_TYPE
) {
2064 case S_SYMBOL_STUBS
:
2065 if ( _stubsSectionNum
== 0 ) {
2066 _stubsSectionNum
= i
+1;
2067 _stubsMachOSection
= sect
;
2070 assert(1 && "multiple S_SYMBOL_STUBS sections");
2071 case S_LAZY_SYMBOL_POINTERS
:
2073 case S_4BYTE_LITERALS
:
2074 totalSectionsSize
+= sizeof(Literal4Section
<A
>);
2075 machOSects
[count
++].type
= sectionTypeLiteral4
;
2077 case S_8BYTE_LITERALS
:
2078 totalSectionsSize
+= sizeof(Literal8Section
<A
>);
2079 machOSects
[count
++].type
= sectionTypeLiteral8
;
2081 case S_16BYTE_LITERALS
:
2082 totalSectionsSize
+= sizeof(Literal16Section
<A
>);
2083 machOSects
[count
++].type
= sectionTypeLiteral16
;
2085 case S_NON_LAZY_SYMBOL_POINTERS
:
2086 totalSectionsSize
+= sizeof(NonLazyPointerSection
<A
>);
2087 machOSects
[count
++].type
= sectionTypeNonLazy
;
2089 case S_LITERAL_POINTERS
:
2090 if ( (strcmp(sect
->segname(), "__OBJC") == 0) && (strcmp(sect
->sectname(), "__cls_refs") == 0) ) {
2091 totalSectionsSize
+= sizeof(Objc1ClassReferences
<A
>);
2092 machOSects
[count
++].type
= sectionTypeObjC1ClassRefs
;
2095 totalSectionsSize
+= sizeof(PointerToCStringSection
<A
>);
2096 machOSects
[count
++].type
= sectionTypeCStringPointer
;
2099 case S_CSTRING_LITERALS
:
2100 totalSectionsSize
+= sizeof(CStringSection
<A
>);
2101 machOSects
[count
++].type
= sectionTypeCString
;
2103 case S_MOD_INIT_FUNC_POINTERS
:
2104 case S_MOD_TERM_FUNC_POINTERS
:
2105 case S_THREAD_LOCAL_INIT_FUNCTION_POINTERS
:
2110 case S_THREAD_LOCAL_REGULAR
:
2111 case S_THREAD_LOCAL_ZEROFILL
:
2112 if ( (strcmp(sect
->segname(), "__TEXT") == 0) && (strcmp(sect
->sectname(), "__eh_frame") == 0) ) {
2113 totalSectionsSize
+= sizeof(CFISection
<A
>);
2114 machOSects
[count
++].type
= sectionTypeCFI
;
2116 else if ( (strcmp(sect
->segname(), "__DATA") == 0) && (strcmp(sect
->sectname(), "__cfstring") == 0) ) {
2117 totalSectionsSize
+= sizeof(CFStringSection
<A
>);
2118 machOSects
[count
++].type
= sectionTypeCFString
;
2120 else if ( (strcmp(sect
->segname(), "__TEXT") == 0) && (strcmp(sect
->sectname(), "__ustring") == 0) ) {
2121 totalSectionsSize
+= sizeof(UTF16StringSection
<A
>);
2122 machOSects
[count
++].type
= sectionTypeUTF16Strings
;
2124 else if ( (strcmp(sect
->segname(), "__DATA") == 0) && (strncmp(sect
->sectname(), "__objc_classrefs", 16) == 0) ) {
2125 totalSectionsSize
+= sizeof(ObjC2ClassRefsSection
<A
>);
2126 machOSects
[count
++].type
= sectionTypeObjC2ClassRefs
;
2128 else if ( (strcmp(sect
->segname(), "__DATA") == 0) && (strcmp(sect
->sectname(), "__objc_catlist") == 0) ) {
2129 totalSectionsSize
+= sizeof(ObjC2CategoryListSection
<A
>);
2130 machOSects
[count
++].type
= typeObjC2CategoryList
;
2132 else if ( _AppleObjc
&& (strcmp(sect
->segname(), "__OBJC") == 0) && (strcmp(sect
->sectname(), "__class") == 0) ) {
2133 totalSectionsSize
+= sizeof(ObjC1ClassSection
<A
>);
2134 machOSects
[count
++].type
= sectionTypeObjC1Classes
;
2137 totalSectionsSize
+= sizeof(SymboledSection
<A
>);
2138 machOSects
[count
++].type
= sectionTypeSymboled
;
2141 case S_THREAD_LOCAL_VARIABLES
:
2142 totalSectionsSize
+= sizeof(TLVDefsSection
<A
>);
2143 machOSects
[count
++].type
= sectionTypeTLVDefs
;
2145 case S_THREAD_LOCAL_VARIABLE_POINTERS
:
2147 throwf("unknown section type %d", sect
->flags() & SECTION_TYPE
);
2151 // sort by address (mach-o object files don't aways have sections sorted)
2152 ::qsort(machOSects
, count
, sizeof(MachOSectionAndSectionClass
<P
>), MachOSectionAndSectionClass
<P
>::sorter
);
2154 // we will synthesize a dummy Section<A> object for tentative definitions
2155 if ( _tentativeDefinitionCount
> 0 ) {
2156 totalSectionsSize
+= sizeof(TentativeDefinitionSection
<A
>);
2157 machOSects
[count
++].type
= sectionTypeTentativeDefinitions
;
2160 // we will synthesize a dummy Section<A> object for Absolute symbols
2161 if ( _absoluteSymbolCount
> 0 ) {
2162 totalSectionsSize
+= sizeof(AbsoluteSymbolSection
<A
>);
2163 machOSects
[count
++].type
= sectionTypeAbsoluteSymbols
;
2166 // allocate one block for all Section objects as well as pointers to each
2167 uint8_t* space
= new uint8_t[totalSectionsSize
+count
*sizeof(Section
<A
>*)];
2168 _file
->_sectionsArray
= (Section
<A
>**)space
;
2169 _file
->_sectionsArrayCount
= count
;
2170 Section
<A
>** objects
= _file
->_sectionsArray
;
2171 space
+= count
*sizeof(Section
<A
>*);
2172 for (uint32_t i
=0; i
< count
; ++i
) {
2173 switch ( machOSects
[i
].type
) {
2174 case sectionTypeIgnore
:
2176 case sectionTypeLiteral4
:
2177 *objects
++ = new (space
) Literal4Section
<A
>(*this, *_file
, machOSects
[i
].sect
);
2178 space
+= sizeof(Literal4Section
<A
>);
2180 case sectionTypeLiteral8
:
2181 *objects
++ = new (space
) Literal8Section
<A
>(*this, *_file
, machOSects
[i
].sect
);
2182 space
+= sizeof(Literal8Section
<A
>);
2184 case sectionTypeLiteral16
:
2185 *objects
++ = new (space
) Literal16Section
<A
>(*this, *_file
, machOSects
[i
].sect
);
2186 space
+= sizeof(Literal16Section
<A
>);
2188 case sectionTypeNonLazy
:
2189 *objects
++ = new (space
) NonLazyPointerSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2190 space
+= sizeof(NonLazyPointerSection
<A
>);
2192 case sectionTypeCFI
:
2193 _EHFrameSection
= new (space
) CFISection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2194 *objects
++ = _EHFrameSection
;
2195 space
+= sizeof(CFISection
<A
>);
2197 case sectionTypeCString
:
2198 *objects
++ = new (space
) CStringSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2199 space
+= sizeof(CStringSection
<A
>);
2201 case sectionTypeCStringPointer
:
2202 *objects
++ = new (space
) PointerToCStringSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2203 space
+= sizeof(PointerToCStringSection
<A
>);
2205 case sectionTypeObjC1ClassRefs
:
2206 *objects
++ = new (space
) Objc1ClassReferences
<A
>(*this, *_file
, machOSects
[i
].sect
);
2207 space
+= sizeof(Objc1ClassReferences
<A
>);
2209 case sectionTypeUTF16Strings
:
2210 *objects
++ = new (space
) UTF16StringSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2211 space
+= sizeof(UTF16StringSection
<A
>);
2213 case sectionTypeCFString
:
2214 *objects
++ = new (space
) CFStringSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2215 space
+= sizeof(CFStringSection
<A
>);
2217 case sectionTypeObjC2ClassRefs
:
2218 *objects
++ = new (space
) ObjC2ClassRefsSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2219 space
+= sizeof(ObjC2ClassRefsSection
<A
>);
2221 case typeObjC2CategoryList
:
2222 *objects
++ = new (space
) ObjC2CategoryListSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2223 space
+= sizeof(ObjC2CategoryListSection
<A
>);
2225 case sectionTypeObjC1Classes
:
2226 *objects
++ = new (space
) ObjC1ClassSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2227 space
+= sizeof(ObjC1ClassSection
<A
>);
2229 case sectionTypeSymboled
:
2230 *objects
++ = new (space
) SymboledSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2231 space
+= sizeof(SymboledSection
<A
>);
2233 case sectionTypeTLVDefs
:
2234 *objects
++ = new (space
) TLVDefsSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2235 space
+= sizeof(TLVDefsSection
<A
>);
2237 case sectionTypeCompactUnwind
:
2238 _compactUnwindSection
= new (space
) CUSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2239 *objects
++ = _compactUnwindSection
;
2240 space
+= sizeof(CUSection
<A
>);
2242 case sectionTypeTentativeDefinitions
:
2243 *objects
++ = new (space
) TentativeDefinitionSection
<A
>(*this, *_file
);
2244 space
+= sizeof(TentativeDefinitionSection
<A
>);
2246 case sectionTypeAbsoluteSymbols
:
2247 _absoluteSection
= new (space
) AbsoluteSymbolSection
<A
>(*this, *_file
);
2248 *objects
++ = _absoluteSection
;
2249 space
+= sizeof(AbsoluteSymbolSection
<A
>);
2252 throw "internal error uknown SectionType";
2258 template <typename A
>
2259 Section
<A
>* Parser
<A
>::sectionForAddress(typename
A::P::uint_t addr
)
2261 for (uint32_t i
=0; i
< _file
->_sectionsArrayCount
; ++i
) {
2262 const macho_section
<typename
A::P
>* sect
= _file
->_sectionsArray
[i
]->machoSection();
2263 // TentativeDefinitionSection and AbsoluteSymbolSection have no mach-o section
2264 if ( sect
!= NULL
) {
2265 if ( (sect
->addr() <= addr
) && (addr
< (sect
->addr()+sect
->size())) ) {
2266 return _file
->_sectionsArray
[i
];
2270 // not strictly in any section
2271 // may be in a zero length section
2272 for (uint32_t i
=0; i
< _file
->_sectionsArrayCount
; ++i
) {
2273 const macho_section
<typename
A::P
>* sect
= _file
->_sectionsArray
[i
]->machoSection();
2274 // TentativeDefinitionSection and AbsoluteSymbolSection have no mach-o section
2275 if ( sect
!= NULL
) {
2276 if ( (sect
->addr() == addr
) && (sect
->size() == 0) ) {
2277 return _file
->_sectionsArray
[i
];
2282 throwf("sectionForAddress(0x%llX) address not in any section", (uint64_t)addr
);
2285 template <typename A
>
2286 Section
<A
>* Parser
<A
>::sectionForNum(unsigned int num
)
2288 for (uint32_t i
=0; i
< _file
->_sectionsArrayCount
; ++i
) {
2289 const macho_section
<typename
A::P
>* sect
= _file
->_sectionsArray
[i
]->machoSection();
2290 // TentativeDefinitionSection and AbsoluteSymbolSection have no mach-o section
2291 if ( sect
!= NULL
) {
2292 if ( num
== (unsigned int)((sect
- _sectionsStart
)+1) )
2293 return _file
->_sectionsArray
[i
];
2296 throwf("sectionForNum(%u) section number not for any section", num
);
2299 template <typename A
>
2300 Atom
<A
>* Parser
<A
>::findAtomByAddress(pint_t addr
)
2302 Section
<A
>* section
= this->sectionForAddress(addr
);
2303 return section
->findAtomByAddress(addr
);
2306 template <typename A
>
2307 Atom
<A
>* Parser
<A
>::findAtomByAddressOrNullIfStub(pint_t addr
)
2309 if ( hasStubsSection() && (_stubsMachOSection
->addr() <= addr
) && (addr
< (_stubsMachOSection
->addr()+_stubsMachOSection
->size())) )
2311 return findAtomByAddress(addr
);
2314 template <typename A
>
2315 Atom
<A
>* Parser
<A
>::findAtomByAddressOrLocalTargetOfStub(pint_t addr
, uint32_t* offsetInAtom
)
2317 if ( hasStubsSection() && (_stubsMachOSection
->addr() <= addr
) && (addr
< (_stubsMachOSection
->addr()+_stubsMachOSection
->size())) ) {
2318 // target is a stub, remove indirection
2319 uint32_t symbolIndex
= this->symbolIndexFromIndirectSectionAddress(addr
, _stubsMachOSection
);
2320 assert(symbolIndex
!= INDIRECT_SYMBOL_LOCAL
);
2321 const macho_nlist
<P
>& sym
= this->symbolFromIndex(symbolIndex
);
2322 // can't be to external weak symbol
2323 assert( (this->combineFromSymbol(sym
) != ld::Atom::combineByName
) || (this->scopeFromSymbol(sym
) != ld::Atom::scopeGlobal
) );
2325 return this->findAtomByName(this->nameFromSymbol(sym
));
2327 Atom
<A
>* target
= this->findAtomByAddress(addr
);
2328 *offsetInAtom
= addr
- target
->_objAddress
;
2332 template <typename A
>
2333 Atom
<A
>* Parser
<A
>::findAtomByName(const char* name
)
2335 uint8_t* p
= _file
->_atomsArray
;
2336 for(int i
=_file
->_atomsArrayCount
; i
> 0; --i
) {
2337 Atom
<A
>* atom
= (Atom
<A
>*)p
;
2338 if ( strcmp(name
, atom
->name()) == 0 )
2340 p
+= sizeof(Atom
<A
>);
2345 template <typename A
>
2346 void Parser
<A
>::findTargetFromAddress(pint_t addr
, TargetDesc
& target
)
2348 if ( hasStubsSection() && (_stubsMachOSection
->addr() <= addr
) && (addr
< (_stubsMachOSection
->addr()+_stubsMachOSection
->size())) ) {
2349 // target is a stub, remove indirection
2350 uint32_t symbolIndex
= this->symbolIndexFromIndirectSectionAddress(addr
, _stubsMachOSection
);
2351 assert(symbolIndex
!= INDIRECT_SYMBOL_LOCAL
);
2352 const macho_nlist
<P
>& sym
= this->symbolFromIndex(symbolIndex
);
2354 target
.name
= this->nameFromSymbol(sym
);
2355 target
.weakImport
= this->weakImportFromSymbol(sym
);
2359 Section
<A
>* section
= this->sectionForAddress(addr
);
2360 target
.atom
= section
->findAtomByAddress(addr
);
2361 target
.addend
= addr
- target
.atom
->_objAddress
;
2362 target
.weakImport
= false;
2366 template <typename A
>
2367 void Parser
<A
>::findTargetFromAddress(pint_t baseAddr
, pint_t addr
, TargetDesc
& target
)
2369 findTargetFromAddress(baseAddr
, target
);
2370 target
.addend
= addr
- target
.atom
->_objAddress
;
2373 template <typename A
>
2374 void Parser
<A
>::findTargetFromAddressAndSectionNum(pint_t addr
, unsigned int sectNum
, TargetDesc
& target
)
2376 if ( sectNum
== R_ABS
) {
2377 // target is absolute symbol that corresponds to addr
2378 if ( _absoluteSection
!= NULL
) {
2379 target
.atom
= _absoluteSection
->findAbsAtomForValue(addr
);
2380 if ( target
.atom
!= NULL
) {
2382 target
.weakImport
= false;
2387 throwf("R_ABS reloc but no absolute symbol at target address");
2390 if ( hasStubsSection() && (stubsSectionNum() == sectNum
) ) {
2391 // target is a stub, remove indirection
2392 uint32_t symbolIndex
= this->symbolIndexFromIndirectSectionAddress(addr
, _stubsMachOSection
);
2393 assert(symbolIndex
!= INDIRECT_SYMBOL_LOCAL
);
2394 const macho_nlist
<P
>& sym
= this->symbolFromIndex(symbolIndex
);
2395 // use direct reference when stub is to a static function
2396 if ( ((sym
.n_type() & N_TYPE
) == N_SECT
) && (((sym
.n_type() & N_EXT
) == 0) || (this->nameFromSymbol(sym
)[0] == 'L')) ) {
2397 this->findTargetFromAddressAndSectionNum(sym
.n_value(), sym
.n_sect(), target
);
2401 target
.name
= this->nameFromSymbol(sym
);
2402 target
.weakImport
= this->weakImportFromSymbol(sym
);
2407 Section
<A
>* section
= this->sectionForNum(sectNum
);
2408 target
.atom
= section
->findAtomByAddress(addr
);
2409 if ( target
.atom
== NULL
) {
2410 typedef typename
A::P::sint_t sint_t
;
2411 sint_t a
= (sint_t
)addr
;
2412 sint_t sectStart
= (sint_t
)(section
->machoSection()->addr());
2413 sint_t sectEnd
= sectStart
+ section
->machoSection()->size();
2414 if ( a
< sectStart
) {
2415 // target address is before start of section, so must be negative addend
2416 target
.atom
= section
->findAtomByAddress(sectStart
);
2417 target
.addend
= a
- sectStart
;
2418 target
.weakImport
= false;
2422 else if ( a
>= sectEnd
) {
2423 target
.atom
= section
->findAtomByAddress(sectEnd
-1);
2424 target
.addend
= a
- sectEnd
;
2425 target
.weakImport
= false;
2430 assert(target
.atom
!= NULL
);
2431 target
.addend
= addr
- target
.atom
->_objAddress
;
2432 target
.weakImport
= false;
2436 template <typename A
>
2437 void Parser
<A
>::addDtraceExtraInfos(const SourceLocation
& src
, const char* providerName
)
2439 // for every ___dtrace_stability$* and ___dtrace_typedefs$* undefine with
2440 // a matching provider name, add a by-name kDtraceTypeReference at probe site
2441 const char* dollar
= strchr(providerName
, '$');
2442 if ( dollar
!= NULL
) {
2443 int providerNameLen
= dollar
-providerName
+1;
2444 for ( std::vector
<const char*>::iterator it
= _dtraceProviderInfo
.begin(); it
!= _dtraceProviderInfo
.end(); ++it
) {
2445 const char* typeDollar
= strchr(*it
, '$');
2446 if ( typeDollar
!= NULL
) {
2447 if ( strncmp(typeDollar
+1, providerName
, providerNameLen
) == 0 ) {
2448 addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindDtraceExtra
,false, *it
);
2455 template <typename A
>
2456 const char* Parser
<A
>::scanSymbolTableForAddress(uint64_t addr
)
2458 uint64_t closestSymAddr
= 0;
2459 const char* closestSymName
= NULL
;
2460 for (uint32_t i
=0; i
< this->_symbolCount
; ++i
) {
2461 const macho_nlist
<P
>& sym
= symbolFromIndex(i
);
2463 if ( (sym
.n_type() & N_STAB
) != 0 )
2466 // only look at definitions
2467 if ( (sym
.n_type() & N_TYPE
) != N_SECT
)
2470 // return with exact match
2471 if ( sym
.n_value() == addr
)
2472 return nameFromSymbol(sym
);
2474 // record closest seen so far
2475 if ( (sym
.n_value() < addr
) && ((sym
.n_value() > closestSymAddr
) || (closestSymName
== NULL
)) )
2476 closestSymName
= nameFromSymbol(sym
);
2479 return (closestSymName
!= NULL
) ? closestSymName
: "unknown";
2483 template <typename A
>
2484 void Parser
<A
>::addFixups(const SourceLocation
& src
, ld::Fixup::Kind setKind
, const TargetDesc
& target
)
2486 // some fixup pairs can be combined
2487 ld::Fixup::Cluster cl
= ld::Fixup::k1of3
;
2488 ld::Fixup::Kind firstKind
= ld::Fixup::kindSetTargetAddress
;
2489 bool combined
= false;
2490 if ( target
.addend
== 0 ) {
2491 cl
= ld::Fixup::k1of1
;
2493 switch ( setKind
) {
2494 case ld::Fixup::kindStoreLittleEndian32
:
2495 firstKind
= ld::Fixup::kindStoreTargetAddressLittleEndian32
;
2497 case ld::Fixup::kindStoreLittleEndian64
:
2498 firstKind
= ld::Fixup::kindStoreTargetAddressLittleEndian64
;
2500 case ld::Fixup::kindStoreBigEndian32
:
2501 firstKind
= ld::Fixup::kindStoreTargetAddressBigEndian32
;
2503 case ld::Fixup::kindStoreBigEndian64
:
2504 firstKind
= ld::Fixup::kindStoreTargetAddressBigEndian64
;
2506 case ld::Fixup::kindStoreX86BranchPCRel32
:
2507 firstKind
= ld::Fixup::kindStoreTargetAddressX86BranchPCRel32
;
2509 case ld::Fixup::kindStoreX86PCRel32
:
2510 firstKind
= ld::Fixup::kindStoreTargetAddressX86PCRel32
;
2512 case ld::Fixup::kindStoreX86PCRel32GOTLoad
:
2513 firstKind
= ld::Fixup::kindStoreTargetAddressX86PCRel32GOTLoad
;
2515 case ld::Fixup::kindStoreX86PCRel32TLVLoad
:
2516 firstKind
= ld::Fixup::kindStoreTargetAddressX86PCRel32TLVLoad
;
2518 case ld::Fixup::kindStoreX86Abs32TLVLoad
:
2519 firstKind
= ld::Fixup::kindStoreTargetAddressX86Abs32TLVLoad
;
2521 case ld::Fixup::kindStoreARMBranch24
:
2522 firstKind
= ld::Fixup::kindStoreTargetAddressARMBranch24
;
2524 case ld::Fixup::kindStoreThumbBranch22
:
2525 firstKind
= ld::Fixup::kindStoreTargetAddressThumbBranch22
;
2527 case ld::Fixup::kindStorePPCBranch24
:
2528 firstKind
= ld::Fixup::kindStoreTargetAddressPPCBranch24
;
2532 cl
= ld::Fixup::k1of2
;
2537 if ( target
.atom
!= NULL
) {
2538 if ( target
.atom
->scope() == ld::Atom::scopeTranslationUnit
) {
2539 addFixup(src
, cl
, firstKind
, target
.atom
);
2541 else if ( (target
.atom
->combine() == ld::Atom::combineByNameAndContent
) || (target
.atom
->combine() == ld::Atom::combineByNameAndReferences
) ) {
2542 addFixup(src
, cl
, firstKind
, ld::Fixup::bindingByContentBound
, target
.atom
);
2544 else if ( (src
.atom
->section().type() == ld::Section::typeCFString
) && (src
.offsetInAtom
!= 0) ) {
2545 // backing string in CFStrings should always be direct
2546 addFixup(src
, cl
, firstKind
, target
.atom
);
2549 // change direct fixup to by-name fixup
2550 addFixup(src
, cl
, firstKind
, false, target
.atom
->name());
2554 addFixup(src
, cl
, firstKind
, target
.weakImport
, target
.name
);
2556 if ( target
.addend
== 0 ) {
2558 addFixup(src
, ld::Fixup::k2of2
, setKind
);
2561 addFixup(src
, ld::Fixup::k2of3
, ld::Fixup::kindAddAddend
, target
.addend
);
2562 addFixup(src
, ld::Fixup::k3of3
, setKind
);
2566 template <typename A
>
2567 void Parser
<A
>::addFixups(const SourceLocation
& src
, ld::Fixup::Kind kind
, const TargetDesc
& target
, const TargetDesc
& picBase
)
2569 ld::Fixup::Cluster cl
= (target
.addend
== 0) ? ld::Fixup::k1of4
: ld::Fixup::k1of5
;
2570 if ( target
.atom
!= NULL
) {
2571 if ( target
.atom
->scope() == ld::Atom::scopeTranslationUnit
) {
2572 addFixup(src
, cl
, ld::Fixup::kindSetTargetAddress
, target
.atom
);
2574 else if ( (target
.atom
->combine() == ld::Atom::combineByNameAndContent
) || (target
.atom
->combine() == ld::Atom::combineByNameAndReferences
) ) {
2575 addFixup(src
, cl
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, target
.atom
);
2578 addFixup(src
, cl
, ld::Fixup::kindSetTargetAddress
, false, target
.atom
->name());
2582 addFixup(src
, cl
, ld::Fixup::kindSetTargetAddress
, target
.weakImport
, target
.name
);
2584 if ( target
.addend
== 0 ) {
2585 assert(picBase
.atom
!= NULL
);
2586 addFixup(src
, ld::Fixup::k2of4
, ld::Fixup::kindSubtractTargetAddress
, picBase
.atom
);
2587 addFixup(src
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractAddend
, picBase
.addend
);
2588 addFixup(src
, ld::Fixup::k4of4
, kind
);
2591 addFixup(src
, ld::Fixup::k2of5
, ld::Fixup::kindAddAddend
, target
.addend
);
2592 addFixup(src
, ld::Fixup::k3of5
, ld::Fixup::kindSubtractTargetAddress
, picBase
.atom
);
2593 addFixup(src
, ld::Fixup::k4of5
, ld::Fixup::kindSubtractAddend
, picBase
.addend
);
2594 addFixup(src
, ld::Fixup::k5of5
, kind
);
2600 template <typename A
>
2601 uint32_t TentativeDefinitionSection
<A
>::computeAtomCount(class Parser
<A
>& parser
,
2602 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
2603 const struct Parser
<A
>::CFI_CU_InfoArrays
&)
2605 return parser
.tentativeDefinitionCount();
2608 template <typename A
>
2609 uint32_t TentativeDefinitionSection
<A
>::appendAtoms(class Parser
<A
>& parser
, uint8_t* p
,
2610 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
2611 const struct Parser
<A
>::CFI_CU_InfoArrays
&)
2613 this->_beginAtoms
= (Atom
<A
>*)p
;
2615 for (uint32_t i
=parser
.undefinedStartIndex(); i
< parser
.undefinedEndIndex(); ++i
) {
2616 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(i
);
2617 if ( ((sym
.n_type() & N_TYPE
) == N_UNDF
) && (sym
.n_value() != 0) ) {
2618 uint64_t size
= sym
.n_value();
2619 uint8_t alignP2
= GET_COMM_ALIGN(sym
.n_desc());
2620 if ( alignP2
== 0 ) {
2621 // common symbols align to their size
2622 // that is, a 4-byte common aligns to 4-bytes
2623 // if this size is not a power of two,
2624 // then round up to the next power of two
2625 alignP2
= 63 - (uint8_t)__builtin_clzll(size
);
2626 if ( size
!= (1ULL << alignP2
) )
2629 // limit alignment of extremely large commons to 2^15 bytes (8-page)
2632 Atom
<A
>* allocatedSpace
= (Atom
<A
>*)p
;
2633 new (allocatedSpace
) Atom
<A
>(*this, parser
.nameFromSymbol(sym
), (pint_t
)ULLONG_MAX
, size
,
2634 ld::Atom::definitionTentative
, ld::Atom::combineByName
,
2635 parser
.scopeFromSymbol(sym
), ld::Atom::typeZeroFill
, ld::Atom::symbolTableIn
,
2636 parser
.dontDeadStripFromSymbol(sym
), false, false, ld::Atom::Alignment(alignP2
) );
2637 p
+= sizeof(Atom
<A
>);
2641 this->_endAtoms
= (Atom
<A
>*)p
;
2646 template <typename A
>
2647 uint32_t AbsoluteSymbolSection
<A
>::computeAtomCount(class Parser
<A
>& parser
,
2648 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
2649 const struct Parser
<A
>::CFI_CU_InfoArrays
&)
2651 return parser
.absoluteSymbolCount();
2654 template <typename A
>
2655 uint32_t AbsoluteSymbolSection
<A
>::appendAtoms(class Parser
<A
>& parser
, uint8_t* p
,
2656 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
2657 const struct Parser
<A
>::CFI_CU_InfoArrays
&)
2659 this->_beginAtoms
= (Atom
<A
>*)p
;
2661 for (uint32_t i
=0; i
< parser
.symbolCount(); ++i
) {
2662 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(i
);
2663 if ( (sym
.n_type() & N_TYPE
) != N_ABS
)
2665 const char* absName
= parser
.nameFromSymbol(sym
);
2666 // ignore .objc_class_name_* symbols
2667 if ( strncmp(absName
, ".objc_class_name_", 17) == 0 )
2669 // ignore .objc_class_name_* symbols
2670 if ( strncmp(absName
, ".objc_category_name_", 20) == 0 )
2672 // ignore empty *.eh symbols
2673 if ( strcmp(&absName
[strlen(absName
)-3], ".eh") == 0 )
2676 Atom
<A
>* allocatedSpace
= (Atom
<A
>*)p
;
2677 new (allocatedSpace
) Atom
<A
>(*this, parser
, sym
, 0);
2678 p
+= sizeof(Atom
<A
>);
2681 this->_endAtoms
= (Atom
<A
>*)p
;
2685 template <typename A
>
2686 Atom
<A
>* AbsoluteSymbolSection
<A
>::findAbsAtomForValue(typename
A::P::uint_t value
)
2688 Atom
<A
>* end
= this->_endAtoms
;
2689 for(Atom
<A
>* p
= this->_beginAtoms
; p
< end
; ++p
) {
2690 if ( p
->_objAddress
== value
)
2697 template <typename A
>
2698 uint32_t Parser
<A
>::indirectSymbol(uint32_t indirectIndex
)
2700 if ( indirectIndex
>= _indirectTableCount
)
2701 throw "indirect symbol index out of range";
2702 return E::get32(_indirectTable
[indirectIndex
]);
2705 template <typename A
>
2706 const macho_nlist
<typename
A::P
>& Parser
<A
>::symbolFromIndex(uint32_t index
)
2708 if ( index
> _symbolCount
)
2709 throw "symbol index out of range";
2710 return _symbols
[index
];
2713 template <typename A
>
2714 const macho_section
<typename
A::P
>* Parser
<A
>::machOSectionFromSectionIndex(uint32_t index
)
2716 if ( index
>= _machOSectionsCount
)
2717 throw "section index out of range";
2718 return &_sectionsStart
[index
];
2721 template <typename A
>
2722 uint32_t Parser
<A
>::symbolIndexFromIndirectSectionAddress(pint_t addr
, const macho_section
<P
>* sect
)
2724 uint32_t elementSize
= 0;
2725 switch ( sect
->flags() & SECTION_TYPE
) {
2726 case S_SYMBOL_STUBS
:
2727 elementSize
= sect
->reserved2();
2729 case S_LAZY_SYMBOL_POINTERS
:
2730 case S_NON_LAZY_SYMBOL_POINTERS
:
2731 elementSize
= sizeof(pint_t
);
2734 throw "section does not use inirect symbol table";
2736 uint32_t indexInSection
= (addr
- sect
->addr()) / elementSize
;
2737 uint32_t indexIntoIndirectTable
= sect
->reserved1() + indexInSection
;
2738 return this->indirectSymbol(indexIntoIndirectTable
);
2743 template <typename A
>
2744 const char* Parser
<A
>::nameFromSymbol(const macho_nlist
<P
>& sym
)
2746 return &_strings
[sym
.n_strx()];
2749 template <typename A
>
2750 ld::Atom::Scope Parser
<A
>::scopeFromSymbol(const macho_nlist
<P
>& sym
)
2752 if ( (sym
.n_type() & N_EXT
) == 0 )
2753 return ld::Atom::scopeTranslationUnit
;
2754 else if ( (sym
.n_type() & N_PEXT
) != 0 )
2755 return ld::Atom::scopeLinkageUnit
;
2756 else if ( this->nameFromSymbol(sym
)[0] == 'l' ) // since all 'l' symbols will be remove, don't make them global
2757 return ld::Atom::scopeLinkageUnit
;
2759 return ld::Atom::scopeGlobal
;
2762 template <typename A
>
2763 ld::Atom::Definition Parser
<A
>::definitionFromSymbol(const macho_nlist
<P
>& sym
)
2765 switch ( sym
.n_type() & N_TYPE
) {
2767 return ld::Atom::definitionAbsolute
;
2769 return ld::Atom::definitionRegular
;
2771 if ( sym
.n_value() != 0 )
2772 return ld::Atom::definitionTentative
;
2774 throw "definitionFromSymbol() bad symbol";
2777 template <typename A
>
2778 ld::Atom::Combine Parser
<A
>::combineFromSymbol(const macho_nlist
<P
>& sym
)
2780 if ( sym
.n_desc() & N_WEAK_DEF
)
2781 return ld::Atom::combineByName
;
2783 return ld::Atom::combineNever
;
2787 template <typename A
>
2788 ld::Atom::SymbolTableInclusion Parser
<A
>::inclusionFromSymbol(const macho_nlist
<P
>& sym
)
2790 const char* symbolName
= nameFromSymbol(sym
);
2791 // labels beginning with 'l' (lowercase ell) are automatically removed in final linked images <rdar://problem/4571042>
2792 // labels beginning with 'L' should have been stripped by the assembler, so are stripped now
2793 if ( sym
.n_desc() & REFERENCED_DYNAMICALLY
)
2794 return ld::Atom::symbolTableInAndNeverStrip
;
2795 else if ( symbolName
[0] == 'l' )
2796 return ld::Atom::symbolTableNotInFinalLinkedImages
;
2797 else if ( symbolName
[0] == 'L' )
2798 return ld::Atom::symbolTableNotIn
;
2800 return ld::Atom::symbolTableIn
;
2803 template <typename A
>
2804 bool Parser
<A
>::dontDeadStripFromSymbol(const macho_nlist
<P
>& sym
)
2806 return ( (sym
.n_desc() & (N_NO_DEAD_STRIP
|REFERENCED_DYNAMICALLY
)) != 0 );
2809 template <typename A
>
2810 bool Parser
<A
>::isThumbFromSymbol(const macho_nlist
<P
>& sym
)
2812 return ( sym
.n_desc() & N_ARM_THUMB_DEF
);
2815 template <typename A
>
2816 bool Parser
<A
>::weakImportFromSymbol(const macho_nlist
<P
>& sym
)
2818 return ( ((sym
.n_type() & N_TYPE
) == N_UNDF
) && ((sym
.n_desc() & N_WEAK_REF
) != 0) );
2821 template <typename A
>
2822 bool Parser
<A
>::resolverFromSymbol(const macho_nlist
<P
>& sym
)
2824 return ( sym
.n_desc() & N_SYMBOL_RESOLVER
);
2828 /* Skip over a LEB128 value (signed or unsigned). */
2830 skip_leb128 (const uint8_t ** offset
, const uint8_t * end
)
2832 while (*offset
!= end
&& **offset
>= 0x80)
2838 /* Read a ULEB128 into a 64-bit word. Return (uint64_t)-1 on overflow
2839 or error. On overflow, skip past the rest of the uleb128. */
2841 read_uleb128 (const uint8_t ** offset
, const uint8_t * end
)
2843 uint64_t result
= 0;
2850 return (uint64_t) -1;
2852 b
= **offset
& 0x7f;
2854 if (bit
>= 64 || b
<< bit
>> bit
!= b
)
2855 result
= (uint64_t) -1;
2857 result
|= b
<< bit
, bit
+= 7;
2858 } while (*(*offset
)++ >= 0x80);
2863 /* Skip over a DWARF attribute of form FORM. */
2864 template <typename A
>
2865 bool Parser
<A
>::skip_form(const uint8_t ** offset
, const uint8_t * end
, uint64_t form
,
2866 uint8_t addr_size
, bool dwarf64
)
2876 case DW_FORM_block2
:
2877 if (end
- *offset
< 2)
2879 sz
= 2 + A::P::E::get16(*(uint16_t*)offset
);
2882 case DW_FORM_block4
:
2883 if (end
- *offset
< 4)
2885 sz
= 2 + A::P::E::get32(*(uint32_t*)offset
);
2903 case DW_FORM_string
:
2904 while (*offset
!= end
&& **offset
)
2913 sz
= read_uleb128 (offset
, end
);
2916 case DW_FORM_block1
:
2924 case DW_FORM_ref_udata
:
2925 skip_leb128 (offset
, end
);
2929 case DW_FORM_ref_addr
:
2936 if (end
- *offset
< sz
)
2943 template <typename A
>
2944 const char* Parser
<A
>::getDwarfString(uint64_t form
, const uint8_t* p
)
2946 if ( form
== DW_FORM_string
)
2947 return (const char*)p
;
2948 else if ( form
== DW_FORM_strp
) {
2949 uint32_t offset
= E::get32(*((uint32_t*)p
));
2950 const char* dwarfStrings
= (char*)_file
->fileContent() + _file
->_dwarfDebugStringSect
->offset();
2951 if ( offset
> _file
->_dwarfDebugStringSect
->size() ) {
2952 warning("unknown dwarf DW_FORM_strp (offset=0x%08X) is too big in %s\n", offset
, this->_path
);
2955 return &dwarfStrings
[offset
];
2957 warning("unknown dwarf string encoding (form=%lld) in %s\n", form
, this->_path
);
2962 template <typename A
>
2963 struct AtomAndLineInfo
{
2965 ld::Atom::LineInfo info
;
2969 // <rdar://problem/5591394> Add support to ld64 for N_FUN stabs when used for symbolic constants
2970 // Returns whether a stabStr belonging to an N_FUN stab represents a
2971 // symbolic constant rather than a function
2972 template <typename A
>
2973 bool Parser
<A
>::isConstFunStabs(const char *stabStr
)
2976 // N_FUN can be used for both constants and for functions. In case it's a constant,
2977 // the format of the stabs string is "symname:c=<value>;"
2978 // ':' cannot appear in the symbol name, except if it's an Objective-C method
2979 // (in which case the symbol name starts with + or -, and then it's definitely
2981 return (stabStr
!= NULL
) && (stabStr
[0] != '+') && (stabStr
[0] != '-')
2982 && ((colon
= strchr(stabStr
, ':')) != NULL
)
2983 && (colon
[1] == 'c') && (colon
[2] == '=');
2987 template <typename A
>
2988 void Parser
<A
>::parseDebugInfo()
2990 // check for dwarf __debug_info section
2991 if ( _file
->_dwarfDebugInfoSect
== NULL
) {
2992 // if no DWARF debug info, look for stabs
2996 if ( _file
->_dwarfDebugInfoSect
->size() == 0 )
3000 if ( !read_comp_unit(&_file
->_dwarfTranslationUnitFile
, &_file
->_dwarfTranslationUnitDir
, &stmtList
) ) {
3001 // if can't parse dwarf, warn and give up
3002 _file
->_dwarfTranslationUnitFile
= NULL
;
3003 _file
->_dwarfTranslationUnitDir
= NULL
;
3004 warning("can't parse dwarf compilation unit info in %s", _path
);
3005 _file
->_debugInfoKind
= ld::relocatable::File::kDebugInfoNone
;
3009 // add line number info to atoms from dwarf
3010 std::vector
<AtomAndLineInfo
<A
> > entries
;
3011 entries
.reserve(64);
3012 if ( _file
->_debugInfoKind
== ld::relocatable::File::kDebugInfoDwarf
) {
3013 // file with just data will have no __debug_line info
3014 if ( (_file
->_dwarfDebugLineSect
!= NULL
) && (_file
->_dwarfDebugLineSect
->size() != 0) ) {
3015 // validate stmt_list
3016 if ( (stmtList
!= (uint64_t)-1) && (stmtList
< _file
->_dwarfDebugLineSect
->size()) ) {
3017 const uint8_t* debug_line
= (uint8_t*)_file
->fileContent() + _file
->_dwarfDebugLineSect
->offset();
3018 struct line_reader_data
* lines
= line_open(&debug_line
[stmtList
],
3019 _file
->_dwarfDebugLineSect
->size() - stmtList
, E::little_endian
);
3020 struct line_info result
;
3021 Atom
<A
>* curAtom
= NULL
;
3022 uint32_t curAtomOffset
= 0;
3023 uint32_t curAtomAddress
= 0;
3024 uint32_t curAtomSize
= 0;
3025 std::map
<uint32_t,const char*> dwarfIndexToFile
;
3026 if ( lines
!= NULL
) {
3027 while ( line_next(lines
, &result
, line_stop_pc
) ) {
3028 //fprintf(stderr, "curAtom=%p, result.pc=0x%llX, result.line=%llu, result.end_of_sequence=%d,"
3029 // " curAtomAddress=0x%X, curAtomSize=0x%X\n",
3030 // curAtom, result.pc, result.line, result.end_of_sequence, curAtomAddress, curAtomSize);
3031 // work around weird debug line table compiler generates if no functions in __text section
3032 if ( (curAtom
== NULL
) && (result
.pc
== 0) && result
.end_of_sequence
&& (result
.file
== 1))
3034 // for performance, see if in next pc is in current atom
3035 if ( (curAtom
!= NULL
) && (curAtomAddress
<= result
.pc
) && (result
.pc
< (curAtomAddress
+curAtomSize
)) ) {
3036 curAtomOffset
= result
.pc
- curAtomAddress
;
3038 // or pc at end of current atom
3039 else if ( result
.end_of_sequence
&& (curAtom
!= NULL
) && (result
.pc
== (curAtomAddress
+curAtomSize
)) ) {
3040 curAtomOffset
= result
.pc
- curAtomAddress
;
3042 // or only one function that is a one line function
3043 else if ( result
.end_of_sequence
&& (curAtom
== NULL
) && (this->findAtomByAddress(0) != NULL
) && (result
.pc
== this->findAtomByAddress(0)->size()) ) {
3044 curAtom
= this->findAtomByAddress(0);
3045 curAtomOffset
= result
.pc
- curAtom
->objectAddress();
3046 curAtomAddress
= curAtom
->objectAddress();
3047 curAtomSize
= curAtom
->size();
3050 // do slow look up of atom by address
3052 curAtom
= this->findAtomByAddress(result
.pc
);
3055 // in case of bug in debug info, don't abort link, just limp on
3058 if ( curAtom
== NULL
)
3059 break; // file has line info but no functions
3060 if ( result
.end_of_sequence
&& (curAtomAddress
+curAtomSize
< result
.pc
) ) {
3061 // a one line function can be returned by line_next() as one entry with pc at end of blob
3062 // look for alt atom starting at end of previous atom
3063 uint32_t previousEnd
= curAtomAddress
+curAtomSize
;
3064 Atom
<A
>* alt
= this->findAtomByAddressOrNullIfStub(previousEnd
);
3066 continue; // ignore spurious debug info for stubs
3067 if ( result
.pc
<= alt
->objectAddress() + alt
->size() ) {
3069 curAtomOffset
= result
.pc
- alt
->objectAddress();
3070 curAtomAddress
= alt
->objectAddress();
3071 curAtomSize
= alt
->size();
3074 curAtomOffset
= result
.pc
- curAtom
->objectAddress();
3075 curAtomAddress
= curAtom
->objectAddress();
3076 curAtomSize
= curAtom
->size();
3080 curAtomOffset
= result
.pc
- curAtom
->objectAddress();
3081 curAtomAddress
= curAtom
->objectAddress();
3082 curAtomSize
= curAtom
->size();
3085 const char* filename
;
3086 std::map
<uint32_t,const char*>::iterator pos
= dwarfIndexToFile
.find(result
.file
);
3087 if ( pos
== dwarfIndexToFile
.end() ) {
3088 filename
= line_file(lines
, result
.file
);
3089 dwarfIndexToFile
[result
.file
] = filename
;
3092 filename
= pos
->second
;
3094 // only record for ~8000 line info records per function
3095 if ( curAtom
->roomForMoreLineInfoCount() ) {
3096 AtomAndLineInfo
<A
> entry
;
3097 entry
.atom
= curAtom
;
3098 entry
.info
.atomOffset
= curAtomOffset
;
3099 entry
.info
.fileName
= filename
;
3100 entry
.info
.lineNumber
= result
.line
;
3101 //fprintf(stderr, "addr=0x%08llX, line=%lld, file=%s, atom=%s, atom.size=0x%X, end=%d\n",
3102 // result.pc, result.line, filename, curAtom->name(), curAtomSize, result.end_of_sequence);
3103 entries
.push_back(entry
);
3104 curAtom
->incrementLineInfoCount();
3106 if ( result
.end_of_sequence
) {
3116 // assign line info start offset for each atom
3117 uint8_t* p
= _file
->_atomsArray
;
3118 uint32_t liOffset
= 0;
3119 for(int i
=_file
->_atomsArrayCount
; i
> 0; --i
) {
3120 Atom
<A
>* atom
= (Atom
<A
>*)p
;
3121 atom
->_lineInfoStartIndex
= liOffset
;
3122 liOffset
+= atom
->_lineInfoCount
;
3123 atom
->_lineInfoCount
= 0;
3124 p
+= sizeof(Atom
<A
>);
3126 assert(liOffset
== entries
.size());
3127 _file
->_lineInfos
.reserve(liOffset
);
3129 // copy each line info for each atom
3130 for (typename
std::vector
<AtomAndLineInfo
<A
> >::iterator it
= entries
.begin(); it
!= entries
.end(); ++it
) {
3131 uint32_t slot
= it
->atom
->_lineInfoStartIndex
+ it
->atom
->_lineInfoCount
;
3132 _file
->_lineInfos
[slot
] = it
->info
;
3133 it
->atom
->_lineInfoCount
++;
3136 // done with temp vector
3140 template <typename A
>
3141 void Parser
<A
>::parseStabs()
3143 // scan symbol table for stabs entries
3144 Atom
<A
>* currentAtom
= NULL
;
3145 pint_t currentAtomAddress
= 0;
3146 enum { start
, inBeginEnd
, inFun
} state
= start
;
3147 for (uint32_t symbolIndex
= 0; symbolIndex
< _symbolCount
; ++symbolIndex
) {
3148 const macho_nlist
<P
>& sym
= this->symbolFromIndex(symbolIndex
);
3149 bool useStab
= true;
3150 uint8_t type
= sym
.n_type();
3151 const char* symString
= (sym
.n_strx() != 0) ? this->nameFromSymbol(sym
) : NULL
;
3152 if ( (type
& N_STAB
) != 0 ) {
3153 _file
->_debugInfoKind
= (_hasUUID
? ld::relocatable::File::kDebugInfoStabsUUID
: ld::relocatable::File::kDebugInfoStabs
);
3154 ld::relocatable::File::Stab stab
;
3157 stab
.other
= sym
.n_sect();
3158 stab
.desc
= sym
.n_desc();
3159 stab
.value
= sym
.n_value();
3165 // beginning of function block
3167 // fall into case to lookup atom by addresss
3170 currentAtomAddress
= sym
.n_value();
3171 currentAtom
= this->findAtomByAddress(currentAtomAddress
);
3172 if ( currentAtom
!= NULL
) {
3173 stab
.atom
= currentAtom
;
3174 stab
.string
= symString
;
3177 fprintf(stderr
, "can't find atom for stabs BNSYM at %08llX in %s",
3178 (uint64_t)sym
.n_value(), _path
);
3187 // not associated with an atom, just copy
3188 stab
.string
= symString
;
3192 // n_value field is NOT atom address ;-(
3193 // need to find atom by name match
3194 const char* colon
= strchr(symString
, ':');
3195 if ( colon
!= NULL
) {
3196 // build underscore leading name
3197 int nameLen
= colon
- symString
;
3198 char symName
[nameLen
+2];
3199 strlcpy(&symName
[1], symString
, nameLen
+1);
3201 symName
[nameLen
+1] = '\0';
3202 currentAtom
= this->findAtomByName(symName
);
3203 if ( currentAtom
!= NULL
) {
3204 stab
.atom
= currentAtom
;
3205 stab
.string
= symString
;
3209 // might be a debug-note without trailing :G()
3210 currentAtom
= this->findAtomByName(symString
);
3211 if ( currentAtom
!= NULL
) {
3212 stab
.atom
= currentAtom
;
3213 stab
.string
= symString
;
3216 if ( stab
.atom
== NULL
) {
3217 // ld_classic added bogus GSYM stabs for old style dtrace probes
3218 if ( (strncmp(symString
, "__dtrace_probe$", 15) != 0) )
3219 warning("can't find atom for N_GSYM stabs %s in %s", symString
, _path
);
3225 if ( isConstFunStabs(symString
) ) {
3226 // constant not associated with a function
3227 stab
.string
= symString
;
3230 // old style stabs without BNSYM
3232 currentAtomAddress
= sym
.n_value();
3233 currentAtom
= this->findAtomByAddress(currentAtomAddress
);
3234 if ( currentAtom
!= NULL
) {
3235 stab
.atom
= currentAtom
;
3236 stab
.string
= symString
;
3239 warning("can't find atom for stabs FUN at %08llX in %s",
3240 (uint64_t)currentAtomAddress
, _path
);
3246 stab
.string
= symString
;
3252 stab
.string
= symString
;
3253 // -gfull built .o file
3256 warning("unknown stabs type 0x%X in %s", type
, _path
);
3260 stab
.atom
= currentAtom
;
3269 Atom
<A
>* nestedAtom
= this->findAtomByAddress(sym
.n_value());
3270 if ( nestedAtom
!= NULL
) {
3271 stab
.atom
= nestedAtom
;
3272 stab
.string
= symString
;
3275 warning("can't find atom for stabs 0x%X at %08llX in %s",
3276 type
, (uint64_t)sym
.n_value(), _path
);
3283 // adjust value to be offset in atom
3284 stab
.value
-= currentAtomAddress
;
3286 stab
.string
= symString
;
3293 if ( isConstFunStabs(symString
) ) {
3294 stab
.atom
= currentAtom
;
3295 stab
.string
= symString
;
3298 if ( sym
.n_sect() != 0 ) {
3299 // found another start stab, must be really old stabs...
3300 currentAtomAddress
= sym
.n_value();
3301 currentAtom
= this->findAtomByAddress(currentAtomAddress
);
3302 if ( currentAtom
!= NULL
) {
3303 stab
.atom
= currentAtom
;
3304 stab
.string
= symString
;
3307 warning("can't find atom for stabs FUN at %08llX in %s",
3308 (uint64_t)currentAtomAddress
, _path
);
3312 // found ending stab, switch back to start state
3313 stab
.string
= symString
;
3314 stab
.atom
= currentAtom
;
3323 // adjust value to be offset in atom
3324 stab
.value
-= currentAtomAddress
;
3325 stab
.atom
= currentAtom
;
3328 stab
.string
= symString
;
3332 stab
.atom
= currentAtom
;
3333 stab
.string
= symString
;
3338 // add to list of stabs for this .o file
3340 _file
->_stabs
.push_back(stab
);
3347 // Look at the compilation unit DIE and determine
3348 // its NAME, compilation directory (in COMP_DIR) and its
3349 // line number information offset (in STMT_LIST). NAME and COMP_DIR
3350 // may be NULL (especially COMP_DIR) if they are not in the .o file;
3351 // STMT_LIST will be (uint64_t) -1.
3353 // At present this assumes that there's only one compilation unit DIE.
3355 template <typename A
>
3356 bool Parser
<A
>::read_comp_unit(const char ** name
, const char ** comp_dir
,
3357 uint64_t *stmt_list
)
3359 const uint8_t * debug_info
;
3360 const uint8_t * debug_abbrev
;
3363 const uint8_t * end
;
3364 const uint8_t * enda
;
3367 uint64_t abbrev_base
;
3369 uint8_t address_size
;
3374 *stmt_list
= (uint64_t) -1;
3376 if ( (_file
->_dwarfDebugInfoSect
== NULL
) || (_file
->_dwarfDebugAbbrevSect
== NULL
) )
3379 debug_info
= (uint8_t*)_file
->fileContent() + _file
->_dwarfDebugInfoSect
->offset();
3380 debug_abbrev
= (uint8_t*)_file
->fileContent() + _file
->_dwarfDebugAbbrevSect
->offset();
3383 if (_file
->_dwarfDebugInfoSect
->size() < 12)
3384 /* Too small to be a real debug_info section. */
3386 sz
= A::P::E::get32(*(uint32_t*)di
);
3388 dwarf64
= sz
== 0xffffffff;
3390 sz
= A::P::E::get64(*(uint64_t*)di
), di
+= 8;
3391 else if (sz
> 0xffffff00)
3392 /* Unknown dwarf format. */
3395 /* Verify claimed size. */
3396 if (sz
+ (di
- debug_info
) > _file
->_dwarfDebugInfoSect
->size() || sz
<= (dwarf64
? 23 : 11))
3399 vers
= A::P::E::get16(*(uint16_t*)di
);
3400 if (vers
< 2 || vers
> 3)
3401 /* DWARF version wrong for this code.
3402 Chances are we could continue anyway, but we don't know for sure. */
3406 /* Find the debug_abbrev section. */
3407 abbrev_base
= dwarf64
? A::P::E::get64(*(uint64_t*)di
) : A::P::E::get32(*(uint32_t*)di
);
3408 di
+= dwarf64
? 8 : 4;
3410 if (abbrev_base
> _file
->_dwarfDebugAbbrevSect
->size())
3412 da
= debug_abbrev
+ abbrev_base
;
3413 enda
= debug_abbrev
+ _file
->_dwarfDebugAbbrevSect
->size();
3415 address_size
= *di
++;
3417 /* Find the abbrev number we're looking for. */
3419 abbrev
= read_uleb128 (&di
, end
);
3420 if (abbrev
== (uint64_t) -1)
3423 /* Skip through the debug_abbrev section looking for that abbrev. */
3426 uint64_t this_abbrev
= read_uleb128 (&da
, enda
);
3429 if (this_abbrev
== abbrev
)
3430 /* This is almost always taken. */
3432 skip_leb128 (&da
, enda
); /* Skip the tag. */
3435 da
++; /* Skip the DW_CHILDREN_* value. */
3438 attr
= read_uleb128 (&da
, enda
);
3439 skip_leb128 (&da
, enda
);
3440 } while (attr
!= 0 && attr
!= (uint64_t) -1);
3445 /* Check that the abbrev is one for a DW_TAG_compile_unit. */
3446 if (read_uleb128 (&da
, enda
) != DW_TAG_compile_unit
)
3450 da
++; /* Skip the DW_CHILDREN_* value. */
3452 /* Now, go through the DIE looking for DW_AT_name,
3453 DW_AT_comp_dir, and DW_AT_stmt_list. */
3456 uint64_t attr
= read_uleb128 (&da
, enda
);
3457 uint64_t form
= read_uleb128 (&da
, enda
);
3459 if (attr
== (uint64_t) -1)
3464 if (form
== DW_FORM_indirect
)
3465 form
= read_uleb128 (&di
, end
);
3467 if (attr
== DW_AT_name
)
3468 *name
= getDwarfString(form
, di
);
3469 else if (attr
== DW_AT_comp_dir
)
3470 *comp_dir
= getDwarfString(form
, di
);
3471 else if (attr
== DW_AT_stmt_list
&& form
== DW_FORM_data4
)
3472 *stmt_list
= A::P::E::get32(*(uint32_t*)di
);
3473 else if (attr
== DW_AT_stmt_list
&& form
== DW_FORM_data8
)
3474 *stmt_list
= A::P::E::get64(*(uint64_t*)di
);
3475 if (! skip_form (&di
, end
, form
, address_size
, dwarf64
))
3482 template <typename A
>
3485 free(_sectionsArray
);
3489 template <typename A
>
3490 bool File
<A
>::translationUnitSource(const char** dir
, const char** name
) const
3492 if ( _debugInfoKind
== ld::relocatable::File::kDebugInfoDwarf
) {
3493 *dir
= _dwarfTranslationUnitDir
;
3494 *name
= _dwarfTranslationUnitFile
;
3495 return (_dwarfTranslationUnitFile
!= NULL
);
3502 template <typename A
>
3503 bool File
<A
>::forEachAtom(ld::File::AtomHandler
& handler
) const
3505 handler
.doFile(*this);
3506 uint8_t* p
= _atomsArray
;
3507 for(int i
=_atomsArrayCount
; i
> 0; --i
) {
3508 handler
.doAtom(*((Atom
<A
>*)p
));
3509 p
+= sizeof(Atom
<A
>);
3511 return (_atomsArrayCount
!= 0);
3514 template <typename A
>
3515 const char* Section
<A
>::makeSegmentName(const macho_section
<typename
A::P
>* sect
)
3517 // mach-o section record only has room for 16-byte seg/sect names
3518 // so a 16-byte name has no trailing zero
3519 const char* name
= sect
->segname();
3520 if ( strlen(name
) < 16 )
3522 char* tmp
= new char[17];
3523 strlcpy(tmp
, name
, 17);
3527 template <typename A
>
3528 const char* Section
<A
>::makeSectionName(const macho_section
<typename
A::P
>* sect
)
3530 const char* name
= sect
->sectname();
3531 if ( strlen(name
) < 16 )
3534 // special case common long section names so we don't have to malloc
3535 if ( strncmp(sect
->sectname(), "__objc_classrefs", 16) == 0 )
3536 return "__objc_classrefs";
3537 if ( strncmp(sect
->sectname(), "__objc_classlist", 16) == 0 )
3538 return "__objc_classlist";
3539 if ( strncmp(sect
->sectname(), "__objc_nlclslist", 16) == 0 )
3540 return "__objc_nlclslist";
3541 if ( strncmp(sect
->sectname(), "__objc_nlcatlist", 16) == 0 )
3542 return "__objc_nlcatlist";
3543 if ( strncmp(sect
->sectname(), "__objc_protolist", 16) == 0 )
3544 return "__objc_protolist";
3545 if ( strncmp(sect
->sectname(), "__objc_protorefs", 16) == 0 )
3546 return "__objc_protorefs";
3547 if ( strncmp(sect
->sectname(), "__objc_superrefs", 16) == 0 )
3548 return "__objc_superrefs";
3549 if ( strncmp(sect
->sectname(), "__objc_imageinfo", 16) == 0 )
3550 return "__objc_imageinfo";
3551 if ( strncmp(sect
->sectname(), "__objc_stringobj", 16) == 0 )
3552 return "__objc_stringobj";
3553 if ( strncmp(sect
->sectname(), "__gcc_except_tab", 16) == 0 )
3554 return "__gcc_except_tab";
3556 char* tmp
= new char[17];
3557 strlcpy(tmp
, name
, 17);
3561 template <typename A
>
3562 bool Section
<A
>::readable(const macho_section
<typename
A::P
>* sect
)
3567 template <typename A
>
3568 bool Section
<A
>::writable(const macho_section
<typename
A::P
>* sect
)
3570 // mach-o .o files do not contain segment permissions
3571 // we just know TEXT is special
3572 return ( strcmp(sect
->segname(), "__TEXT") != 0 );
3575 template <typename A
>
3576 bool Section
<A
>::exectuable(const macho_section
<typename
A::P
>* sect
)
3578 // mach-o .o files do not contain segment permissions
3579 // we just know TEXT is special
3580 return ( strcmp(sect
->segname(), "__TEXT") == 0 );
3584 template <typename A
>
3585 ld::Section::Type Section
<A
>::sectionType(const macho_section
<typename
A::P
>* sect
)
3587 switch ( sect
->flags() & SECTION_TYPE
) {
3589 return ld::Section::typeZeroFill
;
3590 case S_CSTRING_LITERALS
:
3591 if ( (strcmp(sect
->sectname(), "__cstring") == 0) && (strcmp(sect
->segname(), "__TEXT") == 0) )
3592 return ld::Section::typeCString
;
3594 return ld::Section::typeNonStdCString
;
3595 case S_4BYTE_LITERALS
:
3596 return ld::Section::typeLiteral4
;
3597 case S_8BYTE_LITERALS
:
3598 return ld::Section::typeLiteral8
;
3599 case S_LITERAL_POINTERS
:
3600 return ld::Section::typeCStringPointer
;
3601 case S_NON_LAZY_SYMBOL_POINTERS
:
3602 return ld::Section::typeNonLazyPointer
;
3603 case S_LAZY_SYMBOL_POINTERS
:
3604 return ld::Section::typeLazyPointer
;
3605 case S_SYMBOL_STUBS
:
3606 return ld::Section::typeStub
;
3607 case S_MOD_INIT_FUNC_POINTERS
:
3608 return ld::Section::typeInitializerPointers
;
3609 case S_MOD_TERM_FUNC_POINTERS
:
3610 return ld::Section::typeTerminatorPointers
;
3612 return ld::Section::typeUnclassified
;
3613 case S_16BYTE_LITERALS
:
3614 return ld::Section::typeLiteral16
;
3617 if ( sect
->flags() & S_ATTR_PURE_INSTRUCTIONS
) {
3618 return ld::Section::typeCode
;
3620 else if ( strcmp(sect
->segname(), "__TEXT") == 0 ) {
3621 if ( strcmp(sect
->sectname(), "__eh_frame") == 0 )
3622 return ld::Section::typeCFI
;
3623 else if ( strcmp(sect
->sectname(), "__ustring") == 0 )
3624 return ld::Section::typeUTF16Strings
;
3625 else if ( strcmp(sect
->sectname(), "__textcoal_nt") == 0 )
3626 return ld::Section::typeCode
;
3627 else if ( strcmp(sect
->sectname(), "__StaticInit") == 0 )
3628 return ld::Section::typeCode
;
3630 else if ( strcmp(sect
->segname(), "__DATA") == 0 ) {
3631 if ( strcmp(sect
->sectname(), "__cfstring") == 0 )
3632 return ld::Section::typeCFString
;
3633 else if ( strcmp(sect
->sectname(), "__dyld") == 0 )
3634 return ld::Section::typeDyldInfo
;
3635 else if ( strcmp(sect
->sectname(), "__program_vars") == 0 )
3636 return ld::Section::typeDyldInfo
;
3637 else if ( strncmp(sect
->sectname(), "__objc_classrefs", 16) == 0 )
3638 return ld::Section::typeObjCClassRefs
;
3639 else if ( strcmp(sect
->sectname(), "__objc_catlist") == 0 )
3640 return ld::Section::typeObjC2CategoryList
;
3642 else if ( strcmp(sect
->segname(), "__OBJC") == 0 ) {
3643 if ( strcmp(sect
->sectname(), "__class") == 0 )
3644 return ld::Section::typeObjC1Classes
;
3647 case S_THREAD_LOCAL_REGULAR
:
3648 return ld::Section::typeTLVInitialValues
;
3649 case S_THREAD_LOCAL_ZEROFILL
:
3650 return ld::Section::typeTLVZeroFill
;
3651 case S_THREAD_LOCAL_VARIABLES
:
3652 return ld::Section::typeTLVDefs
;
3653 case S_THREAD_LOCAL_INIT_FUNCTION_POINTERS
:
3654 return ld::Section::typeTLVInitializerPointers
;
3656 return ld::Section::typeUnclassified
;
3660 template <typename A
>
3661 Atom
<A
>* Section
<A
>::findContentAtomByAddress(pint_t addr
, class Atom
<A
>* start
, class Atom
<A
>* end
)
3663 // do a binary search of atom array
3664 uint32_t atomCount
= end
- start
;
3665 Atom
<A
>* base
= start
;
3666 for (uint32_t n
= atomCount
; n
> 0; n
/= 2) {
3667 Atom
<A
>* pivot
= &base
[n
/2];
3668 pint_t atomStartAddr
= pivot
->_objAddress
;
3669 pint_t atomEndAddr
= atomStartAddr
+ pivot
->_size
;
3670 if ( atomStartAddr
<= addr
) {
3671 // address in normal atom
3672 if (addr
< atomEndAddr
)
3674 // address in "end" label (but not in alias)
3675 if ( (pivot
->_size
== 0) && (addr
== atomEndAddr
) && !pivot
->isAlias() )
3678 if ( addr
>= atomEndAddr
) {
3680 // move base to atom after pivot
3692 template <typename A
>
3693 ld::Atom::Alignment Section
<A
>::alignmentForAddress(pint_t addr
)
3695 const uint32_t sectionAlignment
= this->_machOSection
->align();
3696 return ld::Atom::Alignment(sectionAlignment
, (addr
% (1 << sectionAlignment
)));
3699 template <typename A
>
3700 uint32_t Section
<A
>::sectionNum(class Parser
<A
>& parser
) const
3702 if ( _machOSection
== NULL
)
3705 return 1 + (this->_machOSection
- parser
.firstMachOSection());
3708 // libunwind does not support ppc64
3709 template <> uint32_t CFISection
<ppc64
>::cfiCount() { return 0; }
3710 // arm does not have zero cost exceptions
3711 template <> uint32_t CFISection
<arm
>::cfiCount() { return 0; }
3713 template <typename A
>
3714 uint32_t CFISection
<A
>::cfiCount()
3716 // create ObjectAddressSpace object for use by libunwind
3717 OAS
oas(*this, (uint8_t*)this->file().fileContent()+this->_machOSection
->offset());
3718 return libunwind::CFI_Parser
<OAS
>::getCFICount(oas
,
3719 this->_machOSection
->addr(), this->_machOSection
->size());
3722 template <typename A
>
3723 void CFISection
<A
>::warnFunc(void* ref
, uint64_t funcAddr
, const char* msg
)
3725 Parser
<A
>* parser
= (Parser
<A
>*)ref
;
3726 if ( ! parser
->convertUnwindInfo() )
3728 if ( funcAddr
!= CFI_INVALID_ADDRESS
) {
3729 // atoms are not constructed yet, so scan symbol table for labels
3730 const char* name
= parser
->scanSymbolTableForAddress(funcAddr
);
3731 warning("could not create compact unwind for %s: %s", name
, msg
);
3734 warning("could not create compact unwind: %s", msg
);
3739 bool CFISection
<x86_64
>::needsRelocating()
3744 template <typename A
>
3745 bool CFISection
<A
>::needsRelocating()
3751 void CFISection
<x86_64
>::cfiParse(class Parser
<x86_64
>& parser
, uint8_t* buffer
,
3752 libunwind::CFI_Atom_Info
<CFISection
<x86_64
>::OAS
>::CFI_Atom_Info cfiArray
[],
3755 // copy __eh_frame data to buffer
3756 memcpy(buffer
, file().fileContent() + this->_machOSection
->offset(), this->_machOSection
->size());
3758 // and apply relocations
3759 const macho_relocation_info
<P
>* relocs
= (macho_relocation_info
<P
>*)(file().fileContent() + this->_machOSection
->reloff());
3760 const macho_relocation_info
<P
>* relocsEnd
= &relocs
[this->_machOSection
->nreloc()];
3761 for (const macho_relocation_info
<P
>* reloc
= relocs
; reloc
< relocsEnd
; ++reloc
) {
3763 switch ( reloc
->r_type() ) {
3764 case X86_64_RELOC_SUBTRACTOR
:
3765 value
= 0 - parser
.symbolFromIndex(reloc
->r_symbolnum()).n_value();
3767 if ( reloc
->r_extern() )
3768 value
+= parser
.symbolFromIndex(reloc
->r_symbolnum()).n_value();
3770 case X86_64_RELOC_UNSIGNED
:
3771 value
= parser
.symbolFromIndex(reloc
->r_symbolnum()).n_value();
3773 case X86_64_RELOC_GOT
:
3774 // this is used for the reference to the personality function in CIEs
3775 // store the symbol number of the personality function for later use as a Fixup
3776 value
= reloc
->r_symbolnum();
3779 fprintf(stderr
, "CFISection::cfiParse() unexpected relocation type at r_address=0x%08X\n", reloc
->r_address());
3784 switch ( reloc
->r_length() ) {
3786 p64
= (uint64_t*)&buffer
[reloc
->r_address()];
3787 E::set64(*p64
, value
+ E::get64(*p64
));
3790 p32
= (uint32_t*)&buffer
[reloc
->r_address()];
3791 E::set32(*p32
, value
+ E::get32(*p32
));
3794 fprintf(stderr
, "CFISection::cfiParse() unexpected relocation size at r_address=0x%08X\n", reloc
->r_address());
3800 // create ObjectAddressSpace object for use by libunwind
3801 OAS
oas(*this, buffer
);
3803 // use libuwind to parse __eh_frame data into array of CFI_Atom_Info
3805 msg
= libunwind::DwarfInstructions
<OAS
, libunwind::Registers_x86_64
>::parseCFIs(
3806 oas
, this->_machOSection
->addr(), this->_machOSection
->size(),
3807 cfiArray
, count
, (void*)&parser
, warnFunc
);
3809 throwf("malformed __eh_frame section: %s", msg
);
3813 void CFISection
<x86
>::cfiParse(class Parser
<x86
>& parser
, uint8_t* buffer
,
3814 libunwind::CFI_Atom_Info
<CFISection
<x86
>::OAS
>::CFI_Atom_Info cfiArray
[],
3817 // create ObjectAddressSpace object for use by libunwind
3818 OAS
oas(*this, (uint8_t*)this->file().fileContent()+this->_machOSection
->offset());
3820 // use libuwind to parse __eh_frame data into array of CFI_Atom_Info
3822 msg
= libunwind::DwarfInstructions
<OAS
, libunwind::Registers_x86
>::parseCFIs(
3823 oas
, this->_machOSection
->addr(), this->_machOSection
->size(),
3824 cfiArray
, count
, (void*)&parser
, warnFunc
);
3826 throwf("malformed __eh_frame section: %s", msg
);
3830 // need to change libunwind parseCFIs() to work for ppc
3832 void CFISection
<ppc
>::cfiParse(class Parser
<ppc
>& parser
, uint8_t* buffer
,
3833 libunwind::CFI_Atom_Info
<CFISection
<ppc
>::OAS
>::CFI_Atom_Info cfiArray
[],
3836 // create ObjectAddressSpace object for use by libunwind
3837 OAS
oas(*this, (uint8_t*)this->file().fileContent()+this->_machOSection
->offset());
3839 // use libuwind to parse __eh_frame data into array of CFI_Atom_Info
3841 msg
= libunwind::DwarfInstructions
<OAS
, libunwind::Registers_ppc
>::parseCFIs(
3842 oas
, this->_machOSection
->addr(), this->_machOSection
->size(),
3843 cfiArray
, count
, (void*)&parser
, warnFunc
);
3845 throwf("malformed __eh_frame section: %s", msg
);
3849 void CFISection
<ppc64
>::cfiParse(class Parser
<ppc64
>& parser
, uint8_t* buffer
,
3850 libunwind::CFI_Atom_Info
<CFISection
<ppc64
>::OAS
>::CFI_Atom_Info cfiArray
[],
3853 // libunwind does not support ppc64
3858 void CFISection
<arm
>::cfiParse(class Parser
<arm
>& parser
, uint8_t* buffer
,
3859 libunwind::CFI_Atom_Info
<CFISection
<arm
>::OAS
>::CFI_Atom_Info cfiArray
[],
3862 // arm does not use zero cost exceptions
3868 template <typename A
>
3869 uint32_t CFISection
<A
>::computeAtomCount(class Parser
<A
>& parser
,
3870 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
3871 const struct Parser
<A
>::CFI_CU_InfoArrays
& cfis
)
3873 return cfis
.cfiCount
;
3878 template <typename A
>
3879 uint32_t CFISection
<A
>::appendAtoms(class Parser
<A
>& parser
, uint8_t* p
,
3880 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
3881 const struct Parser
<A
>::CFI_CU_InfoArrays
& cfis
)
3883 this->_beginAtoms
= (Atom
<A
>*)p
;
3884 // walk CFI_Atom_Info array and create atom for each entry
3885 const CFI_Atom_Info
* start
= &cfis
.cfiArray
[0];
3886 const CFI_Atom_Info
* end
= &cfis
.cfiArray
[cfis
.cfiCount
];
3887 for(const CFI_Atom_Info
* a
=start
; a
< end
; ++a
) {
3888 Atom
<A
>* space
= (Atom
<A
>*)p
;
3889 new (space
) Atom
<A
>(*this, (a
->isCIE
? "CIE" : "FDE"), a
->address
, a
->size
,
3890 ld::Atom::definitionRegular
, ld::Atom::combineNever
, ld::Atom::scopeTranslationUnit
,
3891 ld::Atom::typeCFI
, ld::Atom::symbolTableNotInFinalLinkedImages
,
3892 false, false, false, ld::Atom::Alignment(0));
3893 p
+= sizeof(Atom
<A
>);
3895 this->_endAtoms
= (Atom
<A
>*)p
;
3896 return cfis
.cfiCount
;
3900 template <> bool CFISection
<x86_64
>::bigEndian() { return false; }
3901 template <> bool CFISection
<x86
>::bigEndian() { return false; }
3902 template <> bool CFISection
<arm
>::bigEndian() { return false; }
3903 template <> bool CFISection
<ppc
>::bigEndian() { return true; }
3904 template <> bool CFISection
<ppc64
>::bigEndian() { return true; }
3908 void CFISection
<x86_64
>::addCiePersonalityFixups(class Parser
<x86_64
>& parser
, const CFI_Atom_Info
* cieInfo
)
3910 uint8_t personalityEncoding
= cieInfo
->u
.cieInfo
.personality
.encodingOfTargetAddress
;
3911 if ( personalityEncoding
== 0x9B ) {
3912 // compiler always produces X86_64_RELOC_GOT with addend of 4 to personality function
3913 // CFISection<x86_64>::cfiParse() set targetAddress to be symbolIndex + 4 + addressInCIE
3914 uint32_t symbolIndex
= cieInfo
->u
.cieInfo
.personality
.targetAddress
- 4
3915 - cieInfo
->address
- cieInfo
->u
.cieInfo
.personality
.offsetInCFI
;
3916 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(symbolIndex
);
3917 const char* personalityName
= parser
.nameFromSymbol(sym
);
3919 Atom
<x86_64
>* cieAtom
= this->findAtomByAddress(cieInfo
->address
);
3920 Parser
<x86_64
>::SourceLocation
src(cieAtom
, cieInfo
->u
.cieInfo
.personality
.offsetInCFI
);
3921 parser
.addFixup(src
, ld::Fixup::k1of3
, ld::Fixup::kindSetTargetAddress
, false, personalityName
);
3922 parser
.addFixup(src
, ld::Fixup::k2of3
, ld::Fixup::kindAddAddend
, 4);
3923 parser
.addFixup(src
, ld::Fixup::k3of3
, ld::Fixup::kindStoreX86PCRel32GOT
);
3925 else if ( personalityEncoding
!= 0 ) {
3926 throwf("unsupported address encoding (%02X) of personality function in CIE",
3927 personalityEncoding
);
3932 void CFISection
<x86
>::addCiePersonalityFixups(class Parser
<x86
>& parser
, const CFI_Atom_Info
* cieInfo
)
3934 uint8_t personalityEncoding
= cieInfo
->u
.cieInfo
.personality
.encodingOfTargetAddress
;
3935 if ( (personalityEncoding
== 0x9B) || (personalityEncoding
== 0x90) ) {
3936 uint32_t offsetInCFI
= cieInfo
->u
.cieInfo
.personality
.offsetInCFI
;
3937 uint32_t nlpAddr
= cieInfo
->u
.cieInfo
.personality
.targetAddress
;
3938 Atom
<x86
>* cieAtom
= this->findAtomByAddress(cieInfo
->address
);
3939 Atom
<x86
>* nlpAtom
= parser
.findAtomByAddress(nlpAddr
);
3940 assert(nlpAtom
->contentType() == ld::Atom::typeNonLazyPointer
);
3941 Parser
<x86
>::SourceLocation
src(cieAtom
, cieInfo
->u
.cieInfo
.personality
.offsetInCFI
);
3943 parser
.addFixup(src
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, nlpAtom
);
3944 parser
.addFixup(src
, ld::Fixup::k2of4
, ld::Fixup::kindSubtractTargetAddress
, cieAtom
);
3945 parser
.addFixup(src
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractAddend
, offsetInCFI
);
3946 parser
.addFixup(src
, ld::Fixup::k4of4
, ld::Fixup::kindStoreLittleEndian32
);
3948 else if ( personalityEncoding
!= 0 ) {
3949 throwf("unsupported address encoding (%02X) of personality function in CIE", personalityEncoding
);
3955 void CFISection
<ppc
>::addCiePersonalityFixups(class Parser
<ppc
>& parser
, const CFI_Atom_Info
* cieInfo
)
3957 uint8_t personalityEncoding
= cieInfo
->u
.cieInfo
.personality
.encodingOfTargetAddress
;
3958 if ( (personalityEncoding
== 0x9B) || (personalityEncoding
== 0x90) ) {
3959 uint32_t offsetInCFI
= cieInfo
->u
.cieInfo
.personality
.offsetInCFI
;
3960 uint32_t nlpAddr
= cieInfo
->u
.cieInfo
.personality
.targetAddress
;
3961 Atom
<ppc
>* cieAtom
= this->findAtomByAddress(cieInfo
->address
);
3962 Atom
<ppc
>* nlpAtom
= parser
.findAtomByAddress(nlpAddr
);
3963 assert(nlpAtom
->contentType() == ld::Atom::typeNonLazyPointer
);
3964 Parser
<ppc
>::SourceLocation
src(cieAtom
, cieInfo
->u
.cieInfo
.personality
.offsetInCFI
);
3966 parser
.addFixup(src
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, nlpAtom
);
3967 parser
.addFixup(src
, ld::Fixup::k2of4
, ld::Fixup::kindSubtractTargetAddress
, cieAtom
);
3968 parser
.addFixup(src
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractAddend
, offsetInCFI
);
3969 parser
.addFixup(src
, ld::Fixup::k4of4
, ld::Fixup::kindStoreBigEndian32
);
3971 else if ( personalityEncoding
!= 0 ) {
3972 throwf("unsupported address encoding (%02X) of personality function in CIE",
3973 personalityEncoding
);
3978 template <typename A
>
3979 void CFISection
<A
>::addCiePersonalityFixups(class Parser
<A
>& parser
, const CFI_Atom_Info
* cieInfo
)
3985 template <typename A
>
3986 void CFISection
<A
>::makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
& cfis
)
3988 ld::Fixup::Kind store32
= bigEndian() ? ld::Fixup::kindStoreBigEndian32
: ld::Fixup::kindStoreLittleEndian32
;
3989 ld::Fixup::Kind store64
= bigEndian() ? ld::Fixup::kindStoreBigEndian64
: ld::Fixup::kindStoreLittleEndian64
;
3991 // add all references for FDEs, including implicit group references
3992 const CFI_Atom_Info
* end
= &cfis
.cfiArray
[cfis
.cfiCount
];
3993 for(const CFI_Atom_Info
* p
= &cfis
.cfiArray
[0]; p
< end
; ++p
) {
3995 // add reference to personality function if used
3996 if ( p
->u
.cieInfo
.personality
.targetAddress
!= CFI_INVALID_ADDRESS
) {
3997 this->addCiePersonalityFixups(parser
, p
);
4002 Atom
<A
>* fdeAtom
= this->findAtomByAddress(p
->address
);
4003 // find function Atom
4004 Atom
<A
>* functionAtom
= parser
.findAtomByAddress(p
->u
.fdeInfo
.function
.targetAddress
);
4006 Atom
<A
>* cieAtom
= this->findAtomByAddress(p
->u
.fdeInfo
.cie
.targetAddress
);
4008 Atom
<A
>* lsdaAtom
= NULL
;
4009 if ( p
->u
.fdeInfo
.lsda
.targetAddress
!= CFI_INVALID_ADDRESS
) {
4010 lsdaAtom
= parser
.findAtomByAddress(p
->u
.fdeInfo
.lsda
.targetAddress
);
4012 // add reference from FDE to CIE (always 32-bit pc-rel)
4013 typename Parser
<A
>::SourceLocation
fdeToCieSrc(fdeAtom
, p
->u
.fdeInfo
.cie
.offsetInCFI
);
4014 parser
.addFixup(fdeToCieSrc
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, fdeAtom
);
4015 parser
.addFixup(fdeToCieSrc
, ld::Fixup::k2of4
, ld::Fixup::kindAddAddend
, p
->u
.fdeInfo
.cie
.offsetInCFI
);
4016 parser
.addFixup(fdeToCieSrc
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractTargetAddress
, cieAtom
);
4017 parser
.addFixup(fdeToCieSrc
, ld::Fixup::k4of4
, store32
, cieAtom
);
4019 // add reference from FDE to function
4020 typename Parser
<A
>::SourceLocation
fdeToFuncSrc(fdeAtom
, p
->u
.fdeInfo
.function
.offsetInCFI
);
4021 switch (p
->u
.fdeInfo
.function
.encodingOfTargetAddress
) {
4022 case DW_EH_PE_pcrel
|DW_EH_PE_ptr
:
4023 if ( sizeof(typename
A::P::uint_t
) == 8 ) {
4024 parser
.addFixup(fdeToFuncSrc
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, functionAtom
);
4025 parser
.addFixup(fdeToFuncSrc
, ld::Fixup::k2of4
, ld::Fixup::kindSubtractTargetAddress
, fdeAtom
);
4026 parser
.addFixup(fdeToFuncSrc
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractAddend
, p
->u
.fdeInfo
.function
.offsetInCFI
);
4027 parser
.addFixup(fdeToFuncSrc
, ld::Fixup::k4of4
, store64
);
4030 // else fall into 32-bit case
4031 case DW_EH_PE_pcrel
|DW_EH_PE_sdata4
:
4032 parser
.addFixup(fdeToFuncSrc
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, functionAtom
);
4033 parser
.addFixup(fdeToFuncSrc
, ld::Fixup::k2of4
, ld::Fixup::kindSubtractTargetAddress
, fdeAtom
);
4034 parser
.addFixup(fdeToFuncSrc
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractAddend
, p
->u
.fdeInfo
.function
.offsetInCFI
);
4035 parser
.addFixup(fdeToFuncSrc
, ld::Fixup::k4of4
, store32
);
4038 throw "unsupported encoding in FDE of pointer to function";
4041 // add reference from FDE to LSDA
4042 typename Parser
<A
>::SourceLocation
fdeToLsdaSrc(fdeAtom
, p
->u
.fdeInfo
.lsda
.offsetInCFI
);
4043 if ( lsdaAtom
!= NULL
) {
4044 switch (p
->u
.fdeInfo
.lsda
.encodingOfTargetAddress
) {
4045 case DW_EH_PE_pcrel
|DW_EH_PE_ptr
:
4046 if ( sizeof(typename
A::P::uint_t
) == 8 ) {
4047 parser
.addFixup(fdeToLsdaSrc
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, lsdaAtom
);
4048 parser
.addFixup(fdeToLsdaSrc
, ld::Fixup::k2of4
, ld::Fixup::kindSubtractTargetAddress
, fdeAtom
);
4049 parser
.addFixup(fdeToLsdaSrc
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractAddend
, p
->u
.fdeInfo
.lsda
.offsetInCFI
);
4050 parser
.addFixup(fdeToLsdaSrc
, ld::Fixup::k4of4
, store64
);
4053 // else fall into 32-bit case
4054 case DW_EH_PE_pcrel
|DW_EH_PE_sdata4
:
4055 parser
.addFixup(fdeToLsdaSrc
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, lsdaAtom
);
4056 parser
.addFixup(fdeToLsdaSrc
, ld::Fixup::k2of4
, ld::Fixup::kindSubtractTargetAddress
, fdeAtom
);
4057 parser
.addFixup(fdeToLsdaSrc
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractAddend
, p
->u
.fdeInfo
.lsda
.offsetInCFI
);
4058 parser
.addFixup(fdeToLsdaSrc
, ld::Fixup::k4of4
, store32
);
4061 throw "unsupported encoding in FDE of pointer to LSDA";
4065 // FDE is in group lead by function atom
4066 typename Parser
<A
>::SourceLocation
fdeSrc(functionAtom
,0);
4067 parser
.addFixup(fdeSrc
, ld::Fixup::k1of1
, ld::Fixup::kindNoneGroupSubordinateFDE
, fdeAtom
);
4069 // LSDA is in group lead by function atom
4070 if ( lsdaAtom
!= NULL
) {
4071 parser
.addFixup(fdeSrc
, ld::Fixup::k1of1
, ld::Fixup::kindNoneGroupSubordinateLSDA
, lsdaAtom
);
4080 template <typename A
>
4081 const void* CFISection
<A
>::OAS::mappedAddress(pint_t addr
)
4083 if ( (_ehFrameStartAddr
<= addr
) && (addr
< _ehFrameEndAddr
) )
4084 return &_ehFrameContent
[addr
-_ehFrameStartAddr
];
4086 // requested bytes are not in __eh_frame section
4087 // this can occur when examining the instruction bytes in the __text
4088 File
<A
>& file
= _ehFrameSection
.file();
4089 for (uint32_t i
=0; i
< file
._sectionsArrayCount
; ++i
) {
4090 const macho_section
<typename
A::P
>* sect
= file
._sectionsArray
[i
]->machoSection();
4091 // TentativeDefinitionSection and AbsoluteSymbolSection have no mach-o section
4092 if ( sect
!= NULL
) {
4093 if ( (sect
->addr() <= addr
) && (addr
< (sect
->addr()+sect
->size())) ) {
4094 return file
.fileContent() + sect
->offset() + addr
- sect
->addr();
4098 throwf("__eh_frame parsing problem. Can't find target of reference to address 0x%08llX", (uint64_t)addr
);
4103 template <typename A
>
4104 uint64_t CFISection
<A
>::OAS::getULEB128(pint_t
& logicalAddr
, pint_t end
)
4106 uintptr_t size
= (end
- logicalAddr
);
4107 libunwind::LocalAddressSpace::pint_t laddr
= (libunwind::LocalAddressSpace::pint_t
)mappedAddress(logicalAddr
);
4108 libunwind::LocalAddressSpace::pint_t sladdr
= laddr
;
4109 uint64_t result
= libunwind::LocalAddressSpace::getULEB128(laddr
, laddr
+size
);
4110 logicalAddr
+= (laddr
-sladdr
);
4114 template <typename A
>
4115 int64_t CFISection
<A
>::OAS::getSLEB128(pint_t
& logicalAddr
, pint_t end
)
4117 uintptr_t size
= (end
- logicalAddr
);
4118 libunwind::LocalAddressSpace::pint_t laddr
= (libunwind::LocalAddressSpace::pint_t
)mappedAddress(logicalAddr
);
4119 libunwind::LocalAddressSpace::pint_t sladdr
= laddr
;
4120 int64_t result
= libunwind::LocalAddressSpace::getSLEB128(laddr
, laddr
+size
);
4121 logicalAddr
+= (laddr
-sladdr
);
4125 template <typename A
>
4126 typename
A::P::uint_t CFISection
<A
>::OAS::getEncodedP(pint_t
& addr
, pint_t end
, uint8_t encoding
)
4128 pint_t startAddr
= addr
;
4133 switch (encoding
& 0x0F) {
4135 result
= getP(addr
);
4136 p
+= sizeof(pint_t
);
4139 case DW_EH_PE_uleb128
:
4140 result
= getULEB128(addr
, end
);
4142 case DW_EH_PE_udata2
:
4143 result
= get16(addr
);
4147 case DW_EH_PE_udata4
:
4148 result
= get32(addr
);
4152 case DW_EH_PE_udata8
:
4153 result
= get64(addr
);
4157 case DW_EH_PE_sleb128
:
4158 result
= getSLEB128(addr
, end
);
4160 case DW_EH_PE_sdata2
:
4161 result
= (int16_t)get16(addr
);
4165 case DW_EH_PE_sdata4
:
4166 result
= (int32_t)get32(addr
);
4170 case DW_EH_PE_sdata8
:
4171 result
= get64(addr
);
4176 throwf("ObjectFileAddressSpace<A>::getEncodedP() encoding 0x%08X not supported", encoding
);
4179 // then add relative offset
4180 switch ( encoding
& 0x70 ) {
4181 case DW_EH_PE_absptr
:
4184 case DW_EH_PE_pcrel
:
4185 result
+= startAddr
;
4187 case DW_EH_PE_textrel
:
4188 throw "DW_EH_PE_textrel pointer encoding not supported";
4190 case DW_EH_PE_datarel
:
4191 throw "DW_EH_PE_datarel pointer encoding not supported";
4193 case DW_EH_PE_funcrel
:
4194 throw "DW_EH_PE_funcrel pointer encoding not supported";
4196 case DW_EH_PE_aligned
:
4197 throw "DW_EH_PE_aligned pointer encoding not supported";
4200 throwf("ObjectFileAddressSpace<A>::getEncodedP() encoding 0x%08X not supported", encoding
);
4204 // Note: DW_EH_PE_indirect is only used in CIEs to refernce the personality pointer
4205 // When parsing .o files that pointer contains zero, so we don't to return that.
4206 // Instead we skip the dereference and return the address of the pointer.
4207 // if ( encoding & DW_EH_PE_indirect )
4208 // result = getP(result);
4214 const char* CUSection
<x86_64
>::personalityName(class Parser
<x86_64
>& parser
, const macho_relocation_info
<x86_64::P
>* reloc
)
4216 assert(reloc
->r_extern() && "reloc not extern on personality column in __compact_unwind section");
4217 assert((reloc
->r_type() == X86_64_RELOC_UNSIGNED
) && "wrong reloc type on personality column in __compact_unwind section");
4218 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(reloc
->r_symbolnum());
4219 return parser
.nameFromSymbol(sym
);
4223 const char* CUSection
<x86
>::personalityName(class Parser
<x86
>& parser
, const macho_relocation_info
<x86::P
>* reloc
)
4225 assert(reloc
->r_extern() && "reloc not extern on personality column in __compact_unwind section");
4226 assert((reloc
->r_type() == GENERIC_RELOC_VANILLA
) && "wrong reloc type on personality column in __compact_unwind section");
4227 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(reloc
->r_symbolnum());
4228 return parser
.nameFromSymbol(sym
);
4231 template <typename A
>
4232 const char* CUSection
<A
>::personalityName(class Parser
<A
>& parser
, const macho_relocation_info
<P
>* reloc
)
4238 template <typename A
>
4239 int CUSection
<A
>::infoSorter(const void* l
, const void* r
)
4241 // sort references by symbol index, then address
4242 const Info
* left
= (Info
*)l
;
4243 const Info
* right
= (Info
*)r
;
4244 if ( left
->functionSymbolIndex
== right
->functionSymbolIndex
)
4245 return (left
->functionStartAddress
- right
->functionStartAddress
);
4247 return (left
->functionSymbolIndex
- right
->functionSymbolIndex
);
4250 template <typename A
>
4251 void CUSection
<A
>::parse(class Parser
<A
>& parser
, uint32_t cnt
, Info array
[])
4253 // walk section content and copy to Info array
4254 const macho_compact_unwind_entry
<P
>* const entries
= (macho_compact_unwind_entry
<P
>*)(this->file().fileContent() + this->_machOSection
->offset());
4255 for (uint32_t i
=0; i
< cnt
; ++i
) {
4256 Info
* info
= &array
[i
];
4257 const macho_compact_unwind_entry
<P
>* entry
= &entries
[i
];
4258 info
->functionStartAddress
= entry
->codeStart();
4259 info
->functionSymbolIndex
= 0xFFFFFFFF;
4260 info
->rangeLength
= entry
->codeLen();
4261 info
->compactUnwindInfo
= entry
->compactUnwindInfo();
4262 info
->personality
= NULL
;
4263 info
->lsdaAddress
= entry
->lsda();
4264 info
->function
= NULL
;
4266 if ( (info
->compactUnwindInfo
& UNWIND_PERSONALITY_MASK
) != 0 )
4267 warning("no bits should be set in UNWIND_PERSONALITY_MASK of compact unwind encoding in __LD,__compact_unwind section");
4268 if ( info
->lsdaAddress
!= 0 ) {
4269 info
->compactUnwindInfo
|= UNWIND_HAS_LSDA
;
4273 // scan relocs, local relocs are useless - ignore them
4274 // extern relocs are needed for personality references (possibly for function/lsda refs??)
4275 const macho_relocation_info
<P
>* relocs
= (macho_relocation_info
<P
>*)(this->file().fileContent() + this->_machOSection
->reloff());
4276 const macho_relocation_info
<P
>* relocsEnd
= &relocs
[this->_machOSection
->nreloc()];
4277 for (const macho_relocation_info
<P
>* reloc
= relocs
; reloc
< relocsEnd
; ++reloc
) {
4278 if ( reloc
->r_extern() ) {
4279 // only expect external relocs on some colummns
4280 if ( (reloc
->r_address() % sizeof(macho_compact_unwind_entry
<P
>)) == macho_compact_unwind_entry
<P
>::personalityFieldOffset() ) {
4281 uint32_t entryIndex
= reloc
->r_address() / sizeof(macho_compact_unwind_entry
<P
>);
4282 array
[entryIndex
].personality
= this->personalityName(parser
, reloc
);
4284 else if ( (reloc
->r_address() % sizeof(macho_compact_unwind_entry
<P
>)) == macho_compact_unwind_entry
<P
>::lsdaFieldOffset() ) {
4285 uint32_t entryIndex
= reloc
->r_address() / sizeof(macho_compact_unwind_entry
<P
>);
4286 const macho_nlist
<P
>& lsdaSym
= parser
.symbolFromIndex(reloc
->r_symbolnum());
4287 if ( (lsdaSym
.n_type() & N_TYPE
) == N_SECT
)
4288 array
[entryIndex
].lsdaAddress
= lsdaSym
.n_value();
4290 warning("unexpected extern relocation to lsda in __compact_unwind section");
4292 else if ( (reloc
->r_address() % sizeof(macho_compact_unwind_entry
<P
>)) == macho_compact_unwind_entry
<P
>::codeStartFieldOffset() ) {
4293 uint32_t entryIndex
= reloc
->r_address() / sizeof(macho_compact_unwind_entry
<P
>);
4294 array
[entryIndex
].functionSymbolIndex
= reloc
->r_symbolnum();
4297 warning("unexpected extern relocation in __compact_unwind section");
4302 // sort array by function start address so unwind infos will be contiguous for a given function
4303 ::qsort(array
, cnt
, sizeof(Info
), infoSorter
);
4306 template <typename A
>
4307 uint32_t CUSection
<A
>::count()
4309 const macho_section
<P
>* machoSect
= this->machoSection();
4310 if ( (machoSect
->size() % sizeof(macho_compact_unwind_entry
<P
>)) != 0 )
4311 throw "malformed __LD,__compact_unwind section, bad length";
4313 return machoSect
->size() / sizeof(macho_compact_unwind_entry
<P
>);
4316 template <typename A
>
4317 void CUSection
<A
>::makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
& cus
)
4319 Info
* const arrayStart
= cus
.cuArray
;
4320 Info
* const arrayEnd
= &cus
.cuArray
[cus
.cuCount
];
4321 for (Info
* info
=arrayStart
; info
< arrayEnd
; ++info
) {
4322 // if external reloc was used, real address is symbol n_value + addend
4323 if ( info
->functionSymbolIndex
!= 0xFFFFFFFF )
4324 info
->functionStartAddress
+= parser
.symbolFromIndex(info
->functionSymbolIndex
).n_value();
4325 // find function atom from address
4326 info
->function
= parser
.findAtomByAddress(info
->functionStartAddress
);
4327 // find lsda atom from address
4328 if ( info
->lsdaAddress
!= 0 ) {
4329 info
->lsda
= parser
.findAtomByAddress(info
->lsdaAddress
);
4330 // add lsda subordinate
4331 typename Parser
<A
>::SourceLocation
src(info
->function
, info
->functionStartAddress
- info
->function
->objectAddress());
4332 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindNoneGroupSubordinateLSDA
, info
->lsda
);
4334 if ( info
->personality
!= NULL
) {
4335 // add personality subordinate
4336 typename Parser
<A
>::SourceLocation
src(info
->function
, info
->functionStartAddress
- info
->function
->objectAddress());
4337 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindNoneGroupSubordinatePersonality
, false, info
->personality
);
4343 template <typename A
>
4344 SymboledSection
<A
>::SymboledSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
4345 : Section
<A
>(f
, s
), _type(ld::Atom::typeUnclassified
)
4347 switch ( s
->flags() & SECTION_TYPE
) {
4349 _type
= ld::Atom::typeZeroFill
;
4351 case S_MOD_INIT_FUNC_POINTERS
:
4352 _type
= ld::Atom::typeInitializerPointers
;
4354 case S_MOD_TERM_FUNC_POINTERS
:
4355 _type
= ld::Atom::typeTerminatorPointers
;
4357 case S_THREAD_LOCAL_VARIABLES
:
4358 _type
= ld::Atom::typeTLV
;
4360 case S_THREAD_LOCAL_ZEROFILL
:
4361 _type
= ld::Atom::typeTLVZeroFill
;
4363 case S_THREAD_LOCAL_REGULAR
:
4364 _type
= ld::Atom::typeTLVInitialValue
;
4366 case S_THREAD_LOCAL_INIT_FUNCTION_POINTERS
:
4367 _type
= ld::Atom::typeTLVInitializerPointers
;
4370 if ( strncmp(s
->sectname(), "__gcc_except_tab", 16) == 0 )
4371 _type
= ld::Atom::typeLSDA
;
4377 template <typename A
>
4378 bool SymboledSection
<A
>::dontDeadStrip()
4381 case ld::Atom::typeInitializerPointers
:
4382 case ld::Atom::typeTerminatorPointers
:
4385 // model an object file without MH_SUBSECTIONS_VIA_SYMBOLS as one in which nothing can be dead stripped
4386 if ( ! this->_file
.canScatterAtoms() )
4389 return Section
<A
>::dontDeadStrip();
4395 template <typename A
>
4396 uint32_t SymboledSection
<A
>::computeAtomCount(class Parser
<A
>& parser
,
4397 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
4398 const struct Parser
<A
>::CFI_CU_InfoArrays
&)
4400 const pint_t startAddr
= this->_machOSection
->addr();
4401 const pint_t endAddr
= startAddr
+ this->_machOSection
->size();
4402 const uint32_t sectNum
= this->sectionNum(parser
);
4407 const macho_nlist
<P
>* sym
;
4408 while ( it
.next(parser
, sectNum
, startAddr
, endAddr
, &addr
, &size
, &sym
) ) {
4411 //fprintf(stderr, "computeAtomCount(%s,%s) => %d\n", this->segmentName(), this->sectionName(), count);
4415 template <typename A
>
4416 uint32_t SymboledSection
<A
>::appendAtoms(class Parser
<A
>& parser
, uint8_t* p
,
4417 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
4418 const struct Parser
<A
>::CFI_CU_InfoArrays
&)
4420 this->_beginAtoms
= (Atom
<A
>*)p
;
4422 //fprintf(stderr, "SymboledSection::appendAtoms() in section %s\n", this->_machOSection->sectname());
4423 const pint_t startAddr
= this->_machOSection
->addr();
4424 const pint_t endAddr
= startAddr
+ this->_machOSection
->size();
4425 const uint32_t sectNum
= this->sectionNum(parser
);
4430 const macho_nlist
<P
>* label
;
4431 while ( it
.next(parser
, sectNum
, startAddr
, endAddr
, &addr
, &size
, &label
) ) {
4432 Atom
<A
>* allocatedSpace
= (Atom
<A
>*)p
;
4433 // is break because of label or CFI?
4434 if ( label
!= NULL
) {
4435 // The size is computed based on the address of the next label (or the end of the section for the last label)
4436 // If there are two labels at the same address, we want them one to be an alias of the other.
4437 // If the label is at the end of a section, it is has zero size, but is not an alias
4438 const bool isAlias
= ( (size
== 0) && (addr
< endAddr
) );
4439 new (allocatedSpace
) Atom
<A
>(*this, parser
, *label
, size
, isAlias
);
4441 this->_hasAliases
= true;
4444 ld::Atom::SymbolTableInclusion inclusion
= ld::Atom::symbolTableNotIn
;
4445 ld::Atom::ContentType ctype
= this->contentType();
4446 if ( ctype
== ld::Atom::typeLSDA
)
4447 inclusion
= ld::Atom::symbolTableInWithRandomAutoStripLabel
;
4448 new (allocatedSpace
) Atom
<A
>(*this, "anon", addr
, size
, ld::Atom::definitionRegular
, ld::Atom::combineNever
,
4449 ld::Atom::scopeTranslationUnit
, ctype
, inclusion
,
4450 this->dontDeadStrip(), false, false, this->alignmentForAddress(addr
));
4452 p
+= sizeof(Atom
<A
>);
4456 this->_endAtoms
= (Atom
<A
>*)p
;
4461 template <typename A
>
4462 uint32_t ImplicitSizeSection
<A
>::computeAtomCount(class Parser
<A
>& parser
,
4463 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
4464 const struct Parser
<A
>::CFI_CU_InfoArrays
&)
4467 const macho_section
<P
>* sect
= this->machoSection();
4468 const pint_t startAddr
= sect
->addr();
4469 const pint_t endAddr
= startAddr
+ sect
->size();
4470 for (pint_t addr
= startAddr
; addr
< endAddr
; addr
+= elementSizeAtAddress(addr
) ) {
4471 if ( useElementAt(parser
, it
, addr
) )
4474 if ( it
.fileHasOverlappingSymbols
&& (sect
->size() != 0) && (this->combine(parser
, startAddr
) == ld::Atom::combineByNameAndContent
) ) {
4475 // if there are multiple labels in this section for the same address, then clone them into multi atoms
4476 pint_t prevSymbolAddr
= (pint_t
)(-1);
4477 uint8_t prevSymbolSectNum
= 0;
4478 for(uint32_t i
=0; i
< it
.sortedSymbolCount
; ++i
) {
4479 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(it
.sortedSymbolIndexes
[i
]);
4480 const pint_t symbolAddr
= sym
.n_value();
4481 const pint_t symbolSectNum
= sym
.n_sect();
4482 if ( (symbolAddr
== prevSymbolAddr
) && (prevSymbolSectNum
== symbolSectNum
) && (symbolSectNum
== this->sectionNum(parser
)) ) {
4485 prevSymbolAddr
= symbolAddr
;
4486 prevSymbolSectNum
= symbolSectNum
;
4492 template <typename A
>
4493 uint32_t ImplicitSizeSection
<A
>::appendAtoms(class Parser
<A
>& parser
, uint8_t* p
,
4494 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
4495 const struct Parser
<A
>::CFI_CU_InfoArrays
&)
4497 this->_beginAtoms
= (Atom
<A
>*)p
;
4499 const macho_section
<P
>* sect
= this->machoSection();
4500 const pint_t startAddr
= sect
->addr();
4501 const pint_t endAddr
= startAddr
+ sect
->size();
4502 const uint32_t sectNum
= this->sectionNum(parser
);
4503 //fprintf(stderr, "ImplicitSizeSection::appendAtoms() in section %s\n", sect->sectname());
4507 const macho_nlist
<P
>* foundLabel
;
4508 Atom
<A
>* allocatedSpace
;
4509 while ( it
.next(parser
, sectNum
, startAddr
, endAddr
, &foundAddr
, &size
, &foundLabel
) ) {
4510 if ( foundLabel
!= NULL
) {
4511 pint_t labeledAtomSize
= this->elementSizeAtAddress(foundAddr
);
4512 allocatedSpace
= (Atom
<A
>*)p
;
4513 if ( this->ignoreLabel(parser
.nameFromSymbol(*foundLabel
)) ) {
4514 //fprintf(stderr, " 0x%08llX make annon\n", (uint64_t)foundAddr);
4515 new (allocatedSpace
) Atom
<A
>(*this, this->unlabeledAtomName(parser
, foundAddr
), foundAddr
,
4516 this->elementSizeAtAddress(foundAddr
), this->definition(),
4517 this->combine(parser
, foundAddr
), this->scopeAtAddress(parser
, foundAddr
),
4518 this->contentType(), this->symbolTableInclusion(),
4519 this->dontDeadStrip(), false, false, this->alignmentForAddress(foundAddr
));
4522 // make named atom for label
4523 //fprintf(stderr, " 0x%08llX make labeled\n", (uint64_t)foundAddr);
4524 new (allocatedSpace
) Atom
<A
>(*this, parser
, *foundLabel
, labeledAtomSize
);
4527 p
+= sizeof(Atom
<A
>);
4528 foundAddr
+= labeledAtomSize
;
4529 size
-= labeledAtomSize
;
4531 // some number of anonymous atoms
4532 for (pint_t addr
= foundAddr
; addr
< (foundAddr
+size
); addr
+= elementSizeAtAddress(addr
) ) {
4533 // make anon atoms for area before label
4534 if ( this->useElementAt(parser
, it
, addr
) ) {
4535 //fprintf(stderr, " 0x%08llX make annon\n", (uint64_t)addr);
4536 allocatedSpace
= (Atom
<A
>*)p
;
4537 new (allocatedSpace
) Atom
<A
>(*this, this->unlabeledAtomName(parser
, addr
), addr
, this->elementSizeAtAddress(addr
),
4538 this->definition(), this->combine(parser
, addr
), this->scopeAtAddress(parser
, addr
),
4539 this->contentType(), this->symbolTableInclusion(),
4540 this->dontDeadStrip(), false, false, this->alignmentForAddress(addr
));
4542 p
+= sizeof(Atom
<A
>);
4547 this->_endAtoms
= (Atom
<A
>*)p
;
4553 template <typename A
>
4554 unsigned long Literal4Section
<A
>::contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
4556 const uint32_t* literalContent
= (uint32_t*)atom
->contentPointer();
4557 return *literalContent
;
4560 template <typename A
>
4561 bool Literal4Section
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
4562 const ld::IndirectBindingTable
& ind
) const
4564 assert(this->type() == rhs
.section().type());
4565 const uint32_t* literalContent
= (uint32_t*)atom
->contentPointer();
4567 const Atom
<A
>* rhsAtom
= dynamic_cast<const Atom
<A
>*>(&rhs
);
4568 assert(rhsAtom
!= NULL
);
4569 if ( rhsAtom
!= NULL
) {
4570 const uint32_t* rhsLiteralContent
= (uint32_t*)rhsAtom
->contentPointer();
4571 return (*literalContent
== *rhsLiteralContent
);
4577 template <typename A
>
4578 unsigned long Literal8Section
<A
>::contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
4581 const uint64_t* literalContent
= (uint64_t*)atom
->contentPointer();
4582 return *literalContent
;
4584 unsigned long hash
= 5381;
4585 const uint8_t* byteContent
= atom
->contentPointer();
4586 for (int i
=0; i
< 8; ++i
) {
4587 hash
= hash
* 33 + byteContent
[i
];
4593 template <typename A
>
4594 bool Literal8Section
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
4595 const ld::IndirectBindingTable
& ind
) const
4597 if ( rhs
.section().type() != ld::Section::typeLiteral8
)
4599 assert(this->type() == rhs
.section().type());
4600 const uint64_t* literalContent
= (uint64_t*)atom
->contentPointer();
4602 const Atom
<A
>* rhsAtom
= dynamic_cast<const Atom
<A
>*>(&rhs
);
4603 assert(rhsAtom
!= NULL
);
4604 if ( rhsAtom
!= NULL
) {
4605 const uint64_t* rhsLiteralContent
= (uint64_t*)rhsAtom
->contentPointer();
4606 return (*literalContent
== *rhsLiteralContent
);
4612 template <typename A
>
4613 unsigned long Literal16Section
<A
>::contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
4615 unsigned long hash
= 5381;
4616 const uint8_t* byteContent
= atom
->contentPointer();
4617 for (int i
=0; i
< 16; ++i
) {
4618 hash
= hash
* 33 + byteContent
[i
];
4623 template <typename A
>
4624 bool Literal16Section
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
4625 const ld::IndirectBindingTable
& ind
) const
4627 if ( rhs
.section().type() != ld::Section::typeLiteral16
)
4629 assert(this->type() == rhs
.section().type());
4630 const uint64_t* literalContent
= (uint64_t*)atom
->contentPointer();
4632 const Atom
<A
>* rhsAtom
= dynamic_cast<const Atom
<A
>*>(&rhs
);
4633 assert(rhsAtom
!= NULL
);
4634 if ( rhsAtom
!= NULL
) {
4635 const uint64_t* rhsLiteralContent
= (uint64_t*)rhsAtom
->contentPointer();
4636 return ((literalContent
[0] == rhsLiteralContent
[0]) && (literalContent
[1] == rhsLiteralContent
[1]));
4643 template <typename A
>
4644 typename
A::P::uint_t CStringSection
<A
>::elementSizeAtAddress(pint_t addr
)
4646 const macho_section
<P
>* sect
= this->machoSection();
4647 const char* stringContent
= (char*)(this->file().fileContent() + sect
->offset() + addr
- sect
->addr());
4648 return strlen(stringContent
) + 1;
4651 template <typename A
>
4652 bool CStringSection
<A
>::useElementAt(Parser
<A
>& parser
, struct Parser
<A
>::LabelAndCFIBreakIterator
& it
, pint_t addr
)
4657 template <typename A
>
4658 bool CStringSection
<A
>::ignoreLabel(const char* label
)
4660 return (label
[0] == 'L') || (label
[0] == 'l');
4663 template <typename A
>
4664 Atom
<A
>* CStringSection
<A
>::findAtomByAddress(pint_t addr
)
4666 Atom
<A
>* result
= this->findContentAtomByAddress(addr
, this->_beginAtoms
, this->_endAtoms
);
4670 template <typename A
>
4671 unsigned long CStringSection
<A
>::contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
4673 unsigned long hash
= 5381;
4674 const char* stringContent
= (char*)atom
->contentPointer();
4675 for (const char* s
= stringContent
; *s
!= '\0'; ++s
) {
4676 hash
= hash
* 33 + *s
;
4682 template <typename A
>
4683 bool CStringSection
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
4684 const ld::IndirectBindingTable
& ind
) const
4686 if ( rhs
.section().type() != ld::Section::typeCString
)
4688 assert(this->type() == rhs
.section().type());
4689 assert(strcmp(this->sectionName(), rhs
.section().sectionName())== 0);
4690 assert(strcmp(this->segmentName(), rhs
.section().segmentName())== 0);
4691 const char* stringContent
= (char*)atom
->contentPointer();
4693 const Atom
<A
>* rhsAtom
= dynamic_cast<const Atom
<A
>*>(&rhs
);
4694 assert(rhsAtom
!= NULL
);
4695 if ( rhsAtom
!= NULL
) {
4696 if ( atom
->_size
!= rhsAtom
->_size
)
4698 const char* rhsStringContent
= (char*)rhsAtom
->contentPointer();
4699 return (strcmp(stringContent
, rhsStringContent
) == 0);
4706 ld::Fixup::Kind NonLazyPointerSection
<x86
>::fixupKind()
4708 return ld::Fixup::kindStoreLittleEndian32
;
4712 ld::Fixup::Kind NonLazyPointerSection
<arm
>::fixupKind()
4714 return ld::Fixup::kindStoreLittleEndian32
;
4718 ld::Fixup::Kind NonLazyPointerSection
<ppc
>::fixupKind()
4720 return ld::Fixup::kindStoreBigEndian32
;
4724 ld::Fixup::Kind NonLazyPointerSection
<ppc64
>::fixupKind()
4726 return ld::Fixup::kindStoreBigEndian64
;
4730 void NonLazyPointerSection
<x86_64
>::makeFixups(class Parser
<x86_64
>& parser
, const struct Parser
<x86_64
>::CFI_CU_InfoArrays
&)
4732 assert(0 && "x86_64 should not have non-lazy-pointer sections in .o files");
4735 template <typename A
>
4736 void NonLazyPointerSection
<A
>::makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
&)
4738 // add references for each NLP atom based on indirect symbol table
4739 const macho_section
<P
>* sect
= this->machoSection();
4740 const pint_t endAddr
= sect
->addr() + sect
->size();
4741 for( pint_t addr
= sect
->addr(); addr
< endAddr
; addr
+= sizeof(pint_t
)) {
4742 typename Parser
<A
>::SourceLocation src
;
4743 typename Parser
<A
>::TargetDesc target
;
4744 src
.atom
= this->findAtomByAddress(addr
);
4745 src
.offsetInAtom
= 0;
4746 uint32_t symIndex
= parser
.symbolIndexFromIndirectSectionAddress(addr
, sect
);
4749 target
.weakImport
= false;
4751 if ( symIndex
== INDIRECT_SYMBOL_LOCAL
) {
4752 // use direct reference for local symbols
4753 const pint_t
* nlpContent
= (pint_t
*)(this->file().fileContent() + sect
->offset() + addr
- sect
->addr());
4754 pint_t targetAddr
= P::getP(*nlpContent
);
4755 target
.atom
= parser
.findAtomByAddress(targetAddr
);
4756 target
.weakImport
= false;
4757 target
.addend
= (targetAddr
- target
.atom
->objectAddress());
4758 // <rdar://problem/8385011> if pointer to thumb function, mask of thumb bit (not an addend of +1)
4759 if ( target
.atom
->isThumb() )
4760 target
.addend
&= (-2);
4761 assert(src
.atom
->combine() == ld::Atom::combineNever
);
4764 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(symIndex
);
4765 // use direct reference for local symbols
4766 if ( ((sym
.n_type() & N_TYPE
) == N_SECT
) && ((sym
.n_type() & N_EXT
) == 0) ) {
4767 parser
.findTargetFromAddressAndSectionNum(sym
.n_value(), sym
.n_sect(), target
);
4768 assert(src
.atom
->combine() == ld::Atom::combineNever
);
4771 target
.name
= parser
.nameFromSymbol(sym
);
4772 target
.weakImport
= parser
.weakImportFromSymbol(sym
);
4773 assert(src
.atom
->combine() == ld::Atom::combineByNameAndReferences
);
4776 parser
.addFixups(src
, this->fixupKind(), target
);
4780 template <typename A
>
4781 ld::Atom::Combine NonLazyPointerSection
<A
>::combine(Parser
<A
>& parser
, pint_t addr
)
4783 const macho_section
<P
>* sect
= this->machoSection();
4784 uint32_t symIndex
= parser
.symbolIndexFromIndirectSectionAddress(addr
, sect
);
4785 if ( symIndex
== INDIRECT_SYMBOL_LOCAL
)
4786 return ld::Atom::combineNever
;
4788 // don't coalesce non-lazy-pointers to local symbols
4789 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(symIndex
);
4790 if ( ((sym
.n_type() & N_TYPE
) == N_SECT
) && ((sym
.n_type() & N_EXT
) == 0) )
4791 return ld::Atom::combineNever
;
4793 return ld::Atom::combineByNameAndReferences
;
4796 template <typename A
>
4797 const char* NonLazyPointerSection
<A
>::targetName(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
)
4799 assert(atom
->combine() == ld::Atom::combineByNameAndReferences
);
4800 assert(atom
->fixupCount() == 1);
4801 ld::Fixup::iterator fit
= atom
->fixupsBegin();
4802 const char* name
= NULL
;
4803 switch ( fit
->binding
) {
4804 case ld::Fixup::bindingByNameUnbound
:
4807 case ld::Fixup::bindingByContentBound
:
4808 name
= fit
->u
.target
->name();
4810 case ld::Fixup::bindingsIndirectlyBound
:
4811 name
= ind
.indirectName(fit
->u
.bindingIndex
);
4816 assert(name
!= NULL
);
4820 template <typename A
>
4821 unsigned long NonLazyPointerSection
<A
>::contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
4823 assert(atom
->combine() == ld::Atom::combineByNameAndReferences
);
4824 unsigned long hash
= 9508;
4825 for (const char* s
= this->targetName(atom
, ind
); *s
!= '\0'; ++s
) {
4826 hash
= hash
* 33 + *s
;
4831 template <typename A
>
4832 bool NonLazyPointerSection
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
4833 const ld::IndirectBindingTable
& indirectBindingTable
) const
4835 if ( rhs
.section().type() != ld::Section::typeNonLazyPointer
)
4837 assert(this->type() == rhs
.section().type());
4838 // there can be many non-lazy pointer in different section names
4839 // we only want to coalesce in same section name
4840 if ( *this != rhs
.section() )
4842 const Atom
<A
>* rhsAtom
= dynamic_cast<const Atom
<A
>*>(&rhs
);
4843 assert(rhsAtom
!= NULL
);
4844 const char* thisName
= this->targetName(atom
, indirectBindingTable
);
4845 const char* rhsName
= this->targetName(rhsAtom
, indirectBindingTable
);
4846 return (strcmp(thisName
, rhsName
) == 0);
4849 template <typename A
>
4850 ld::Atom::Scope NonLazyPointerSection
<A
>::scopeAtAddress(Parser
<A
>& parser
, pint_t addr
)
4852 const macho_section
<P
>* sect
= this->machoSection();
4853 uint32_t symIndex
= parser
.symbolIndexFromIndirectSectionAddress(addr
, sect
);
4854 if ( symIndex
== INDIRECT_SYMBOL_LOCAL
)
4855 return ld::Atom::scopeTranslationUnit
;
4857 return ld::Atom::scopeLinkageUnit
;
4861 template <typename A
>
4862 const uint8_t* CFStringSection
<A
>::targetContent(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
,
4863 ContentType
* ct
, unsigned int* count
)
4865 *ct
= contentUnknown
;
4866 for (ld::Fixup::iterator fit
=atom
->fixupsBegin(), end
=atom
->fixupsEnd(); fit
!= end
; ++fit
) {
4867 const ld::Atom
* targetAtom
= NULL
;
4868 switch ( fit
->binding
) {
4869 case ld::Fixup::bindingByNameUnbound
:
4870 // ignore reference to ___CFConstantStringClassReference
4871 // we are just looking for reference to backing string data
4872 assert(fit
->offsetInAtom
== 0);
4873 assert(strcmp(fit
->u
.name
, "___CFConstantStringClassReference") == 0);
4875 case ld::Fixup::bindingDirectlyBound
:
4876 case ld::Fixup::bindingByContentBound
:
4877 targetAtom
= fit
->u
.target
;
4879 case ld::Fixup::bindingsIndirectlyBound
:
4880 targetAtom
= ind
.indirectAtom(fit
->u
.bindingIndex
);
4883 assert(0 && "bad binding type");
4885 assert(targetAtom
!= NULL
);
4886 const Atom
<A
>* target
= dynamic_cast<const Atom
<A
>*>(targetAtom
);
4887 if ( targetAtom
->section().type() == ld::Section::typeCString
) {
4889 *count
= targetAtom
->size();
4891 else if ( targetAtom
->section().type() == ld::Section::typeUTF16Strings
) {
4893 *count
= (targetAtom
->size()+1)/2; // round up incase of buggy compiler that has only one trailing zero byte
4895 assert(target
!= NULL
);
4896 return target
->contentPointer();
4902 template <typename A
>
4903 unsigned long CFStringSection
<A
>::contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
4905 // base hash of CFString on hash of cstring it wraps
4908 unsigned int charCount
;
4909 const uint8_t* content
= this->targetContent(atom
, ind
, &cType
, &charCount
);
4913 for (const char* s
= (char*)content
; *s
!= '\0'; ++s
) {
4914 hash
= hash
* 33 + *s
;
4919 --charCount
; // don't add last 0x0000 to hash because some buggy compilers only have trailing single byte
4920 for (const uint16_t* s
= (uint16_t*)content
; charCount
> 0; ++s
, --charCount
) {
4921 hash
= hash
* 1025 + *s
;
4924 case contentUnknown
:
4931 template <typename A
>
4932 bool CFStringSection
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
4933 const ld::IndirectBindingTable
& indirectBindingTable
) const
4937 if ( rhs
.section().type() != ld::Section::typeCFString
)
4939 assert(this->type() == rhs
.section().type());
4940 assert(strcmp(this->sectionName(), "__cfstring") == 0);
4942 ContentType thisType
;
4943 unsigned int charCount
;
4944 const uint8_t* cstringContent
= this->targetContent(atom
, indirectBindingTable
, &thisType
, &charCount
);
4945 ContentType rhsType
;
4946 const Atom
<A
>* rhsAtom
= dynamic_cast<const Atom
<A
>*>(&rhs
);
4947 assert(rhsAtom
!= NULL
);
4948 unsigned int rhsCharCount
;
4949 const uint8_t* rhsStringContent
= this->targetContent(rhsAtom
, indirectBindingTable
, &rhsType
, &rhsCharCount
);
4951 if ( thisType
!= rhsType
)
4954 // no need to compare content of pointers are already the same
4955 if ( cstringContent
== rhsStringContent
)
4958 // no need to compare content if size is different
4959 if ( charCount
!= rhsCharCount
)
4962 switch ( thisType
) {
4964 return (strcmp((char*)cstringContent
, (char*)rhsStringContent
) == 0);
4967 const uint16_t* cstringContent16
= (uint16_t*)cstringContent
;
4968 const uint16_t* rhsStringContent16
= (uint16_t*)rhsStringContent
;
4969 for (unsigned int i
= 0; i
< charCount
; ++i
) {
4970 if ( cstringContent16
[i
] != rhsStringContent16
[i
] )
4975 case contentUnknown
:
4982 template <typename A
>
4983 typename
A::P::uint_t ObjC1ClassSection
<A
>::elementSizeAtAddress(pint_t addr
)
4985 // nominal size for each class is 48 bytes, but sometimes the compiler
4986 // over aligns and there is padding after class data
4987 const macho_section
<P
>* sct
= this->machoSection();
4988 uint32_t align
= 1 << sct
->align();
4989 uint32_t size
= ((12 * sizeof(pint_t
)) + align
-1) & (-align
);
4993 template <typename A
>
4994 const char* ObjC1ClassSection
<A
>::unlabeledAtomName(Parser
<A
>& parser
, pint_t addr
)
4996 // 8-bytes into class object is pointer to class name
4997 const macho_section
<P
>* sct
= this->machoSection();
4998 uint32_t classObjcFileOffset
= sct
->offset() - sct
->addr() + addr
;
4999 const uint8_t* mappedFileContent
= this->file().fileContent();
5000 pint_t nameAddr
= P::getP(*((pint_t
*)(mappedFileContent
+classObjcFileOffset
+2*sizeof(pint_t
))));
5002 // find section containing string address to get string bytes
5003 const macho_section
<P
>* const sections
= parser
.firstMachOSection();
5004 const uint32_t sectionCount
= parser
.machOSectionCount();
5005 for (uint32_t i
=0; i
< sectionCount
; ++i
) {
5006 const macho_section
<P
>* aSect
= §ions
[i
];
5007 if ( (aSect
->addr() <= nameAddr
) && (nameAddr
< (aSect
->addr()+aSect
->size())) ) {
5008 assert((aSect
->flags() & SECTION_TYPE
) == S_CSTRING_LITERALS
);
5009 uint32_t nameFileOffset
= aSect
->offset() - aSect
->addr() + nameAddr
;
5010 const char* name
= (char*)mappedFileContent
+ nameFileOffset
;
5011 // spin through symbol table to find absolute symbol corresponding to this class
5012 for (uint32_t s
=0; s
< parser
.symbolCount(); ++s
) {
5013 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(s
);
5014 if ( (sym
.n_type() & N_TYPE
) != N_ABS
)
5016 const char* absName
= parser
.nameFromSymbol(sym
);
5017 if ( strncmp(absName
, ".objc_class_name_", 17) == 0 ) {
5018 if ( strcmp(&absName
[17], name
) == 0 )
5022 assert(0 && "obj class name not found in symbol table");
5025 assert(0 && "obj class name not found");
5026 return "unknown objc class";
5030 template <typename A
>
5031 const char* ObjC2ClassRefsSection
<A
>::targetClassName(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
5033 assert(atom
->fixupCount() == 1);
5034 ld::Fixup::iterator fit
= atom
->fixupsBegin();
5035 const char* className
= NULL
;
5036 switch ( fit
->binding
) {
5037 case ld::Fixup::bindingByNameUnbound
:
5038 className
= fit
->u
.name
;
5040 case ld::Fixup::bindingDirectlyBound
:
5041 case ld::Fixup::bindingByContentBound
:
5042 className
= fit
->u
.target
->name();
5044 case ld::Fixup::bindingsIndirectlyBound
:
5045 className
= ind
.indirectName(fit
->u
.bindingIndex
);
5048 assert(0 && "unsupported binding in objc2 class ref section");
5050 assert(className
!= NULL
);
5055 template <typename A
>
5056 unsigned long ObjC2ClassRefsSection
<A
>::contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
5058 unsigned long hash
= 978;
5059 for (const char* s
= targetClassName(atom
, ind
); *s
!= '\0'; ++s
) {
5060 hash
= hash
* 33 + *s
;
5065 template <typename A
>
5066 bool ObjC2ClassRefsSection
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
5067 const ld::IndirectBindingTable
& indirectBindingTable
) const
5069 assert(this->type() == rhs
.section().type());
5070 const Atom
<A
>* rhsAtom
= dynamic_cast<const Atom
<A
>*>(&rhs
);
5071 assert(rhsAtom
!= NULL
);
5072 const char* thisClassName
= targetClassName(atom
, indirectBindingTable
);
5073 const char* rhsClassName
= targetClassName(rhsAtom
, indirectBindingTable
);
5074 return (strcmp(thisClassName
, rhsClassName
) == 0);
5078 template <typename A
>
5079 const char* Objc1ClassReferences
<A
>::targetCString(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
5081 assert(atom
->fixupCount() == 2);
5082 ld::Fixup::iterator fit
= atom
->fixupsBegin();
5083 if ( fit
->kind
== ld::Fixup::kindSetTargetAddress
)
5085 const ld::Atom
* targetAtom
= NULL
;
5086 switch ( fit
->binding
) {
5087 case ld::Fixup::bindingByContentBound
:
5088 targetAtom
= fit
->u
.target
;
5090 case ld::Fixup::bindingsIndirectlyBound
:
5091 targetAtom
= ind
.indirectAtom(fit
->u
.bindingIndex
);
5092 if ( targetAtom
== NULL
) {
5093 fprintf(stderr
, "missing target named %s\n", ind
.indirectName(fit
->u
.bindingIndex
));
5099 assert(targetAtom
!= NULL
);
5100 const Atom
<A
>* target
= dynamic_cast<const Atom
<A
>*>(targetAtom
);
5101 assert(target
!= NULL
);
5102 return (char*)target
->contentPointer();
5106 template <typename A
>
5107 const char* PointerToCStringSection
<A
>::targetCString(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
5109 assert(atom
->fixupCount() == 1);
5110 ld::Fixup::iterator fit
= atom
->fixupsBegin();
5111 const ld::Atom
* targetAtom
= NULL
;
5112 switch ( fit
->binding
) {
5113 case ld::Fixup::bindingByContentBound
:
5114 targetAtom
= fit
->u
.target
;
5116 case ld::Fixup::bindingsIndirectlyBound
:
5117 targetAtom
= ind
.indirectAtom(fit
->u
.bindingIndex
);
5122 assert(targetAtom
!= NULL
);
5123 const Atom
<A
>* target
= dynamic_cast<const Atom
<A
>*>(targetAtom
);
5124 assert(target
!= NULL
);
5125 return (char*)target
->contentPointer();
5128 template <typename A
>
5129 unsigned long PointerToCStringSection
<A
>::contentHash(const class Atom
<A
>* atom
,
5130 const ld::IndirectBindingTable
& indirectBindingTable
) const
5132 // make hash from section name and target cstring name
5133 unsigned long hash
= 123;
5134 for (const char* s
= this->sectionName(); *s
!= '\0'; ++s
) {
5135 hash
= hash
* 33 + *s
;
5137 for (const char* s
= this->targetCString(atom
, indirectBindingTable
); *s
!= '\0'; ++s
) {
5138 hash
= hash
* 33 + *s
;
5143 template <typename A
>
5144 bool PointerToCStringSection
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
5145 const ld::IndirectBindingTable
& indirectBindingTable
) const
5147 assert(this->type() == rhs
.section().type());
5148 // there can be pointers-to-cstrings in different section names
5149 // we only want to coalesce in same section name
5150 if ( *this != rhs
.section() )
5153 // get string content for this
5154 const char* cstringContent
= this->targetCString(atom
, indirectBindingTable
);
5155 const Atom
<A
>* rhsAtom
= dynamic_cast<const Atom
<A
>*>(&rhs
);
5156 assert(rhsAtom
!= NULL
);
5157 const char* rhsCstringContent
= this->targetCString(rhsAtom
, indirectBindingTable
);
5159 assert(cstringContent
!= NULL
);
5160 assert(rhsCstringContent
!= NULL
);
5161 return (strcmp(cstringContent
, rhsCstringContent
) == 0);
5166 template <typename A
>
5167 unsigned long UTF16StringSection
<A
>::contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
5169 unsigned long hash
= 5381;
5170 const uint16_t* stringContent
= (uint16_t*)atom
->contentPointer();
5171 // some buggy compilers end utf16 data with single byte, so don't use last word in hash computation
5172 unsigned int count
= (atom
->size()/2) - 1;
5173 for (const uint16_t* s
= stringContent
; count
> 0; ++s
, --count
) {
5174 hash
= hash
* 33 + *s
;
5179 template <typename A
>
5180 bool UTF16StringSection
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
5181 const ld::IndirectBindingTable
& ind
) const
5183 if ( rhs
.section().type() != ld::Section::typeUTF16Strings
)
5196 uint32_t Section
<x86_64
>::x86_64PcRelOffset(uint8_t r_type
)
5199 case X86_64_RELOC_SIGNED
:
5201 case X86_64_RELOC_SIGNED_1
:
5203 case X86_64_RELOC_SIGNED_2
:
5205 case X86_64_RELOC_SIGNED_4
:
5213 bool Section
<x86_64
>::addRelocFixup(class Parser
<x86_64
>& parser
, const macho_relocation_info
<P
>* reloc
)
5215 const macho_section
<P
>* sect
= this->machoSection();
5216 uint64_t srcAddr
= sect
->addr() + reloc
->r_address();
5217 Parser
<x86_64
>::SourceLocation src
;
5218 Parser
<x86_64
>::TargetDesc target
;
5219 Parser
<x86_64
>::TargetDesc toTarget
;
5220 src
.atom
= this->findAtomByAddress(srcAddr
);
5221 src
.offsetInAtom
= srcAddr
- src
.atom
->_objAddress
;
5222 const uint8_t* fixUpPtr
= file().fileContent() + sect
->offset() + reloc
->r_address();
5223 uint64_t contentValue
= 0;
5224 const macho_relocation_info
<x86_64::P
>* nextReloc
= &reloc
[1];
5225 bool result
= false;
5226 bool useDirectBinding
;
5227 switch ( reloc
->r_length() ) {
5229 contentValue
= *fixUpPtr
;
5232 contentValue
= (int64_t)(int16_t)E::get16(*((uint16_t*)fixUpPtr
));
5235 contentValue
= (int64_t)(int32_t)E::get32(*((uint32_t*)fixUpPtr
));
5238 contentValue
= E::get64(*((uint64_t*)fixUpPtr
));
5243 target
.weakImport
= false;
5245 if ( reloc
->r_extern() ) {
5246 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(reloc
->r_symbolnum());
5247 // use direct reference for local symbols
5248 if ( ((sym
.n_type() & N_TYPE
) == N_SECT
) && (((sym
.n_type() & N_EXT
) == 0) || (parser
.nameFromSymbol(sym
)[0] == 'L')) ) {
5249 parser
.findTargetFromAddressAndSectionNum(sym
.n_value(), sym
.n_sect(), target
);
5250 target
.addend
+= contentValue
;
5253 target
.name
= parser
.nameFromSymbol(sym
);
5254 target
.weakImport
= parser
.weakImportFromSymbol(sym
);
5255 target
.addend
= contentValue
;
5257 // cfstrings should always use direct reference to backing store
5258 if ( (this->type() == ld::Section::typeCFString
) && (src
.offsetInAtom
!= 0) ) {
5259 parser
.findTargetFromAddressAndSectionNum(sym
.n_value(), sym
.n_sect(), target
);
5260 target
.addend
= contentValue
;
5264 if ( reloc
->r_pcrel() )
5265 contentValue
+= srcAddr
+ x86_64PcRelOffset(reloc
->r_type());
5266 parser
.findTargetFromAddressAndSectionNum(contentValue
, reloc
->r_symbolnum(), target
);
5268 switch ( reloc
->r_type() ) {
5269 case X86_64_RELOC_UNSIGNED
:
5270 if ( reloc
->r_pcrel() )
5271 throw "pcrel and X86_64_RELOC_UNSIGNED not supported";
5272 switch ( reloc
->r_length() ) {
5275 throw "length < 2 and X86_64_RELOC_UNSIGNED not supported";
5277 parser
.addFixups(src
, ld::Fixup::kindStoreLittleEndian32
, target
);
5280 parser
.addFixups(src
, ld::Fixup::kindStoreLittleEndian64
, target
);
5284 case X86_64_RELOC_SIGNED
:
5285 case X86_64_RELOC_SIGNED_1
:
5286 case X86_64_RELOC_SIGNED_2
:
5287 case X86_64_RELOC_SIGNED_4
:
5288 if ( ! reloc
->r_pcrel() )
5289 throw "not pcrel and X86_64_RELOC_SIGNED* not supported";
5290 if ( reloc
->r_length() != 2 )
5291 throw "length != 2 and X86_64_RELOC_SIGNED* not supported";
5292 switch ( reloc
->r_type() ) {
5293 case X86_64_RELOC_SIGNED
:
5294 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel32
, target
);
5296 case X86_64_RELOC_SIGNED_1
:
5297 if ( reloc
->r_extern() )
5299 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel32_1
, target
);
5301 case X86_64_RELOC_SIGNED_2
:
5302 if ( reloc
->r_extern() )
5304 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel32_2
, target
);
5306 case X86_64_RELOC_SIGNED_4
:
5307 if ( reloc
->r_extern() )
5309 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel32_4
, target
);
5313 case X86_64_RELOC_BRANCH
:
5314 if ( ! reloc
->r_pcrel() )
5315 throw "not pcrel and X86_64_RELOC_BRANCH not supported";
5316 switch ( reloc
->r_length() ) {
5318 if ( (target
.name
!= NULL
) && (strncmp(target
.name
, "___dtrace_probe$", 16) == 0) ) {
5319 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindStoreX86DtraceCallSiteNop
, false, target
.name
);
5320 parser
.addDtraceExtraInfos(src
, &target
.name
[16]);
5322 else if ( (target
.name
!= NULL
) && (strncmp(target
.name
, "___dtrace_isenabled$", 20) == 0) ) {
5323 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindStoreX86DtraceIsEnableSiteClear
, false, target
.name
);
5324 parser
.addDtraceExtraInfos(src
, &target
.name
[20]);
5327 parser
.addFixups(src
, ld::Fixup::kindStoreX86BranchPCRel32
, target
);
5331 parser
.addFixups(src
, ld::Fixup::kindStoreX86BranchPCRel8
, target
);
5334 throwf("length=%d and X86_64_RELOC_BRANCH not supported", reloc
->r_length());
5337 case X86_64_RELOC_GOT
:
5338 if ( ! reloc
->r_extern() )
5339 throw "not extern and X86_64_RELOC_GOT not supported";
5340 if ( ! reloc
->r_pcrel() )
5341 throw "not pcrel and X86_64_RELOC_GOT not supported";
5342 if ( reloc
->r_length() != 2 )
5343 throw "length != 2 and X86_64_RELOC_GOT not supported";
5344 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel32GOT
, target
);
5346 case X86_64_RELOC_GOT_LOAD
:
5347 if ( ! reloc
->r_extern() )
5348 throw "not extern and X86_64_RELOC_GOT_LOAD not supported";
5349 if ( ! reloc
->r_pcrel() )
5350 throw "not pcrel and X86_64_RELOC_GOT_LOAD not supported";
5351 if ( reloc
->r_length() != 2 )
5352 throw "length != 2 and X86_64_RELOC_GOT_LOAD not supported";
5353 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel32GOTLoad
, target
);
5355 case X86_64_RELOC_SUBTRACTOR
:
5356 if ( reloc
->r_pcrel() )
5357 throw "X86_64_RELOC_SUBTRACTOR cannot be pc-relative";
5358 if ( reloc
->r_length() < 2 )
5359 throw "X86_64_RELOC_SUBTRACTOR must have r_length of 2 or 3";
5360 if ( !reloc
->r_extern() )
5361 throw "X86_64_RELOC_SUBTRACTOR must have r_extern=1";
5362 if ( nextReloc
->r_type() != X86_64_RELOC_UNSIGNED
)
5363 throw "X86_64_RELOC_SUBTRACTOR must be followed by X86_64_RELOC_UNSIGNED";
5365 if ( nextReloc
->r_pcrel() )
5366 throw "X86_64_RELOC_UNSIGNED following a X86_64_RELOC_SUBTRACTOR cannot be pc-relative";
5367 if ( nextReloc
->r_length() != reloc
->r_length() )
5368 throw "X86_64_RELOC_UNSIGNED following a X86_64_RELOC_SUBTRACTOR must have same r_length";
5369 if ( nextReloc
->r_extern() ) {
5370 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(nextReloc
->r_symbolnum());
5371 // use direct reference for local symbols
5372 if ( ((sym
.n_type() & N_TYPE
) == N_SECT
) && (((sym
.n_type() & N_EXT
) == 0) || (parser
.nameFromSymbol(sym
)[0] == 'L')) ) {
5373 parser
.findTargetFromAddressAndSectionNum(sym
.n_value(), sym
.n_sect(), toTarget
);
5374 toTarget
.addend
= contentValue
;
5375 useDirectBinding
= true;
5378 toTarget
.name
= parser
.nameFromSymbol(sym
);
5379 toTarget
.weakImport
= parser
.weakImportFromSymbol(sym
);
5380 toTarget
.addend
= contentValue
;
5381 useDirectBinding
= false;
5385 parser
.findTargetFromAddressAndSectionNum(contentValue
, nextReloc
->r_symbolnum(), toTarget
);
5386 useDirectBinding
= (toTarget
.atom
->scope() == ld::Atom::scopeTranslationUnit
);
5388 if ( useDirectBinding
)
5389 parser
.addFixup(src
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, toTarget
.atom
);
5391 parser
.addFixup(src
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, toTarget
.weakImport
, toTarget
.name
);
5392 parser
.addFixup(src
, ld::Fixup::k2of4
, ld::Fixup::kindAddAddend
, toTarget
.addend
);
5393 if ( target
.atom
== NULL
)
5394 parser
.addFixup(src
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractTargetAddress
, false, target
.name
);
5396 parser
.addFixup(src
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractTargetAddress
, target
.atom
);
5397 if ( reloc
->r_length() == 2 )
5398 parser
.addFixup(src
, ld::Fixup::k4of4
, ld::Fixup::kindStoreLittleEndian32
);
5400 parser
.addFixup(src
, ld::Fixup::k4of4
, ld::Fixup::kindStoreLittleEndian64
);
5402 case X86_64_RELOC_TLV
:
5403 if ( ! reloc
->r_extern() )
5404 throw "not extern and X86_64_RELOC_TLV not supported";
5405 if ( ! reloc
->r_pcrel() )
5406 throw "not pcrel and X86_64_RELOC_TLV not supported";
5407 if ( reloc
->r_length() != 2 )
5408 throw "length != 2 and X86_64_RELOC_TLV not supported";
5409 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel32TLVLoad
, target
);
5412 throwf("unknown relocation type %d", reloc
->r_type());
5420 bool Section
<x86
>::addRelocFixup(class Parser
<x86
>& parser
, const macho_relocation_info
<P
>* reloc
)
5422 const macho_section
<P
>* sect
= this->machoSection();
5424 const uint8_t* fixUpPtr
;
5425 uint32_t contentValue
= 0;
5426 ld::Fixup::Kind kind
= ld::Fixup::kindNone
;
5427 Parser
<x86
>::SourceLocation src
;
5428 Parser
<x86
>::TargetDesc target
;
5430 if ( (reloc
->r_address() & R_SCATTERED
) == 0 ) {
5431 srcAddr
= sect
->addr() + reloc
->r_address();
5432 src
.atom
= this->findAtomByAddress(srcAddr
);
5433 src
.offsetInAtom
= srcAddr
- src
.atom
->_objAddress
;
5434 fixUpPtr
= file().fileContent() + sect
->offset() + reloc
->r_address();
5435 switch ( reloc
->r_type() ) {
5436 case GENERIC_RELOC_VANILLA
:
5437 switch ( reloc
->r_length() ) {
5439 contentValue
= (int32_t)(int8_t)*fixUpPtr
;
5440 if ( reloc
->r_pcrel() ) {
5441 kind
= ld::Fixup::kindStoreX86BranchPCRel8
;
5442 contentValue
+= srcAddr
+ sizeof(uint8_t);
5445 throw "r_length=0 and r_pcrel=0 not supported";
5448 contentValue
= (int32_t)(int16_t)E::get16(*((uint16_t*)fixUpPtr
));
5449 if ( reloc
->r_pcrel() ) {
5450 kind
= ld::Fixup::kindStoreX86PCRel16
;
5451 contentValue
+= srcAddr
+ sizeof(uint16_t);
5454 kind
= ld::Fixup::kindStoreLittleEndian16
;
5457 contentValue
= E::get32(*((uint32_t*)fixUpPtr
));
5458 if ( reloc
->r_pcrel() ) {
5459 kind
= ld::Fixup::kindStoreX86BranchPCRel32
;
5460 contentValue
+= srcAddr
+ sizeof(uint32_t);
5463 kind
= ld::Fixup::kindStoreLittleEndian32
;
5466 throw "r_length=3 not supported";
5468 if ( reloc
->r_extern() ) {
5470 const macho_nlist
<P
>& targetSymbol
= parser
.symbolFromIndex(reloc
->r_symbolnum());
5471 target
.name
= parser
.nameFromSymbol(targetSymbol
);
5472 target
.weakImport
= parser
.weakImportFromSymbol(targetSymbol
);
5473 target
.addend
= (int32_t)contentValue
;
5476 parser
.findTargetFromAddressAndSectionNum(contentValue
, reloc
->r_symbolnum(), target
);
5478 if ( (kind
== ld::Fixup::kindStoreX86BranchPCRel32
) && (target
.name
!= NULL
) ) {
5479 if ( strncmp(target
.name
, "___dtrace_probe$", 16) == 0 ) {
5480 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindStoreX86DtraceCallSiteNop
, false, target
.name
);
5481 parser
.addDtraceExtraInfos(src
, &target
.name
[16]);
5484 else if ( strncmp(target
.name
, "___dtrace_isenabled$", 20) == 0 ) {
5485 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindStoreX86DtraceIsEnableSiteClear
, false, target
.name
);
5486 parser
.addDtraceExtraInfos(src
, &target
.name
[20]);
5490 parser
.addFixups(src
, kind
, target
);
5493 case GENERIC_RLEOC_TLV
:
5495 if ( !reloc
->r_extern() )
5496 throw "r_extern=0 and r_type=GENERIC_RLEOC_TLV not supported";
5497 if ( reloc
->r_length() != 2 )
5498 throw "r_length!=2 and r_type=GENERIC_RLEOC_TLV not supported";
5499 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(reloc
->r_symbolnum());
5500 // use direct reference for local symbols
5501 if ( ((sym
.n_type() & N_TYPE
) == N_SECT
) && ((sym
.n_type() & N_EXT
) == 0) ) {
5502 parser
.findTargetFromAddressAndSectionNum(sym
.n_value(), sym
.n_sect(), target
);
5506 target
.name
= parser
.nameFromSymbol(sym
);
5507 target
.weakImport
= parser
.weakImportFromSymbol(sym
);
5509 target
.addend
= (int64_t)(int32_t)E::get32(*((uint32_t*)fixUpPtr
));
5510 if ( reloc
->r_pcrel() ) {
5511 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel32TLVLoad
, target
);
5514 parser
.addFixups(src
, ld::Fixup::kindStoreX86Abs32TLVLoad
, target
);
5520 throwf("unsupported i386 relocation type (%d)", reloc
->r_type());
5524 // scattered relocation
5525 const macho_scattered_relocation_info
<P
>* sreloc
= (macho_scattered_relocation_info
<P
>*)reloc
;
5526 srcAddr
= sect
->addr() + sreloc
->r_address();
5527 src
.atom
= this->findAtomByAddress(srcAddr
);
5528 assert(src
.atom
!= NULL
);
5529 src
.offsetInAtom
= srcAddr
- src
.atom
->_objAddress
;
5530 fixUpPtr
= file().fileContent() + sect
->offset() + sreloc
->r_address();
5531 uint32_t relocValue
= sreloc
->r_value();
5532 bool result
= false;
5533 // file format allows pair to be scattered or not
5534 const macho_scattered_relocation_info
<P
>* nextSReloc
= &sreloc
[1];
5535 const macho_relocation_info
<P
>* nextReloc
= &reloc
[1];
5536 bool nextRelocIsPair
= false;
5537 uint32_t nextRelocAddress
= 0;
5538 uint32_t nextRelocValue
= 0;
5539 if ( (nextReloc
->r_address() & R_SCATTERED
) == 0 ) {
5540 if ( nextReloc
->r_type() == GENERIC_RELOC_PAIR
) {
5541 nextRelocIsPair
= true;
5542 nextRelocAddress
= nextReloc
->r_address();
5543 result
= true; // iterator should skip next reloc, since we've consumed it here
5547 if ( nextSReloc
->r_type() == GENERIC_RELOC_PAIR
) {
5548 nextRelocIsPair
= true;
5549 nextRelocAddress
= nextSReloc
->r_address();
5550 nextRelocValue
= nextSReloc
->r_value();
5553 switch (sreloc
->r_type()) {
5554 case GENERIC_RELOC_VANILLA
:
5555 // with a scattered relocation we get both the target (sreloc->r_value()) and the target+offset (*fixUpPtr)
5556 target
.atom
= parser
.findAtomByAddress(relocValue
);
5557 if ( sreloc
->r_pcrel() ) {
5558 switch ( sreloc
->r_length() ) {
5560 contentValue
= srcAddr
+ 1 + *fixUpPtr
;
5561 target
.addend
= (int32_t)contentValue
- (int32_t)relocValue
;
5562 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel8
, target
);
5565 contentValue
= srcAddr
+ 2 + LittleEndian::get16(*((uint16_t*)fixUpPtr
));
5566 target
.addend
= (int32_t)contentValue
- (int32_t)relocValue
;
5567 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel16
, target
);
5570 contentValue
= srcAddr
+ 4 + LittleEndian::get32(*((uint32_t*)fixUpPtr
));
5571 target
.addend
= (int32_t)contentValue
- (int32_t)relocValue
;
5572 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel32
, target
);
5575 throw "unsupported r_length=3 for scattered pc-rel vanilla reloc";
5580 if ( sreloc
->r_length() != 2 )
5581 throwf("unsupported r_length=%d for scattered vanilla reloc", sreloc
->r_length());
5582 contentValue
= LittleEndian::get32(*((uint32_t*)fixUpPtr
));
5583 target
.addend
= (int32_t)contentValue
- (int32_t)(target
.atom
->objectAddress());
5584 parser
.addFixups(src
, ld::Fixup::kindStoreLittleEndian32
, target
);
5587 case GENERIC_RELOC_SECTDIFF
:
5588 case GENERIC_RELOC_LOCAL_SECTDIFF
:
5590 if ( !nextRelocIsPair
)
5591 throw "GENERIC_RELOC_SECTDIFF missing following pair";
5592 switch ( sreloc
->r_length() ) {
5595 throw "bad length for GENERIC_RELOC_SECTDIFF";
5597 contentValue
= (int32_t)(int16_t)LittleEndian::get16(*((uint16_t*)fixUpPtr
));
5598 kind
= ld::Fixup::kindStoreLittleEndian16
;
5601 contentValue
= LittleEndian::get32(*((uint32_t*)fixUpPtr
));
5602 kind
= ld::Fixup::kindStoreLittleEndian32
;
5605 Atom
<x86
>* fromAtom
= parser
.findAtomByAddress(nextRelocValue
);
5606 uint32_t offsetInFrom
= nextRelocValue
- fromAtom
->_objAddress
;
5607 parser
.findTargetFromAddress(sreloc
->r_value(), target
);
5608 // check for addend encoded in the section content
5609 int64_t addend
= (int32_t)contentValue
- (int32_t)(sreloc
->r_value() - nextRelocValue
);
5611 // switch binding base on coalescing
5612 if ( target
.atom
== NULL
) {
5613 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, false, target
.name
);
5615 else if ( target
.atom
->scope() == ld::Atom::scopeTranslationUnit
) {
5616 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, target
.atom
);
5618 else if ( (target
.atom
->combine() == ld::Atom::combineByNameAndContent
) || (target
.atom
->combine() == ld::Atom::combineByNameAndReferences
) ) {
5619 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, target
.atom
);
5622 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, false, target
.atom
->name());
5624 parser
.addFixup(src
, ld::Fixup::k2of5
, ld::Fixup::kindAddAddend
, target
.addend
);
5625 parser
.addFixup(src
, ld::Fixup::k3of5
, ld::Fixup::kindSubtractTargetAddress
, fromAtom
);
5626 parser
.addFixup(src
, ld::Fixup::k4of5
, ld::Fixup::kindSubtractAddend
, offsetInFrom
-addend
);
5627 parser
.addFixup(src
, ld::Fixup::k5of5
, kind
);
5630 // switch binding base on coalescing
5631 if ( target
.atom
== NULL
) {
5632 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, false, target
.name
);
5634 else if ( target
.atom
->scope() == ld::Atom::scopeTranslationUnit
) {
5635 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, target
.atom
);
5637 else if ( (target
.atom
->combine() == ld::Atom::combineByNameAndContent
) || (target
.atom
->combine() == ld::Atom::combineByNameAndReferences
) ) {
5638 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, target
.atom
);
5641 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, false, target
.atom
->name());
5643 parser
.addFixup(src
, ld::Fixup::k2of5
, ld::Fixup::kindAddAddend
, target
.addend
+addend
);
5644 parser
.addFixup(src
, ld::Fixup::k3of5
, ld::Fixup::kindSubtractTargetAddress
, fromAtom
);
5645 parser
.addFixup(src
, ld::Fixup::k4of5
, ld::Fixup::kindSubtractAddend
, offsetInFrom
);
5646 parser
.addFixup(src
, ld::Fixup::k5of5
, kind
);
5658 // ppc and ppc64 both use the same relocations, so process them in one common routine
5660 template <typename A
>
5661 bool Section
<A
>::addRelocFixup_powerpc(class Parser
<A
>& parser
,
5662 const macho_relocation_info
<typename
A::P
>* reloc
)
5664 const macho_section
<P
>* sect
= this->machoSection();
5665 bool result
= false;
5669 int32_t displacement
= 0;
5670 uint32_t instruction
= 0;
5672 pint_t contentValue
= 0;
5673 typename Parser
<A
>::SourceLocation src
;
5674 typename Parser
<A
>::TargetDesc target
;
5676 if ( (reloc
->r_address() & R_SCATTERED
) == 0 ) {
5677 srcAddr
= sect
->addr() + reloc
->r_address();
5678 src
.atom
= this->findAtomByAddress(srcAddr
);
5679 src
.offsetInAtom
= srcAddr
- src
.atom
->_objAddress
;
5680 const macho_relocation_info
<P
>* nextReloc
= &reloc
[1];
5681 fixUpPtr
= (uint32_t*)(file().fileContent() + sect
->offset() + reloc
->r_address());
5682 if ( reloc
->r_type() != PPC_RELOC_PAIR
)
5683 instruction
= BigEndian::get32(*fixUpPtr
);
5684 if ( reloc
->r_extern() ) {
5686 const macho_nlist
<P
>& targetSymbol
= parser
.symbolFromIndex(reloc
->r_symbolnum());
5687 target
.name
= parser
.nameFromSymbol(targetSymbol
);
5688 target
.weakImport
= parser
.weakImportFromSymbol(targetSymbol
);
5690 switch ( reloc
->r_type() ) {
5691 case PPC_RELOC_BR24
:
5692 assert((instruction
& 0x4C000000) == 0x48000000);
5693 displacement
= (instruction
& 0x03FFFFFC);
5694 if ( (displacement
& 0x02000000) != 0 )
5695 displacement
|= 0xFC000000;
5696 if ( reloc
->r_extern() ) {
5697 target
.addend
= srcAddr
+ displacement
;
5700 dstAddr
= srcAddr
+ displacement
;
5701 parser
.findTargetFromAddressAndSectionNum(dstAddr
, reloc
->r_symbolnum(), target
);
5703 // special case "calls" for dtrace
5704 if ( (target
.name
!= NULL
) && (strncmp(target
.name
, "___dtrace_probe$", 16) == 0) ) {
5705 parser
.addFixup(src
, ld::Fixup::k1of1
,
5706 ld::Fixup::kindStorePPCDtraceCallSiteNop
, false, target
.name
);
5707 parser
.addDtraceExtraInfos(src
, &target
.name
[16]);
5709 else if ( (target
.name
!= NULL
) && (strncmp(target
.name
, "___dtrace_isenabled$", 20) == 0) ) {
5710 parser
.addFixup(src
, ld::Fixup::k1of1
,
5711 ld::Fixup::kindStorePPCDtraceIsEnableSiteClear
, false, target
.name
);
5712 parser
.addDtraceExtraInfos(src
, &target
.name
[20]);
5715 parser
.addFixups(src
, ld::Fixup::kindStorePPCBranch24
, target
);
5718 case PPC_RELOC_BR14
:
5719 displacement
= (instruction
& 0x0000FFFC);
5720 if ( (displacement
& 0x00008000) != 0 )
5721 displacement
|= 0xFFFF0000;
5722 if ( reloc
->r_extern() ) {
5723 target
.addend
= srcAddr
+ displacement
;
5726 dstAddr
= srcAddr
+ displacement
;
5727 parser
.findTargetFromAddressAndSectionNum(dstAddr
, reloc
->r_symbolnum(), target
);
5729 parser
.addFixups(src
, ld::Fixup::kindStorePPCBranch14
, target
);
5731 case PPC_RELOC_PAIR
:
5732 // skip, processed by a previous look ahead
5734 case PPC_RELOC_LO16
:
5735 if ( nextReloc
->r_type() != PPC_RELOC_PAIR
)
5736 throw "PPC_RELOC_LO16 missing following pair";
5738 lowBits
= (instruction
& 0x0000FFFF);
5739 dstAddr
= (nextReloc
->r_address() << 16) + ((uint32_t)lowBits
& 0x0000FFFF);
5740 if ( reloc
->r_extern() ) {
5741 target
.addend
= dstAddr
;
5744 parser
.findTargetFromAddressAndSectionNum(dstAddr
, reloc
->r_symbolnum(), target
);
5746 parser
.addFixups(src
, ld::Fixup::kindStorePPCAbsLow16
, target
);
5748 case PPC_RELOC_LO14
:
5749 if ( nextReloc
->r_type() != PPC_RELOC_PAIR
)
5750 throw "PPC_RELOC_LO14 missing following pair";
5752 lowBits
= (instruction
& 0xFFFC);
5753 dstAddr
= (nextReloc
->r_address() << 16) + ((uint32_t)lowBits
& 0x0000FFFF);
5754 if ( reloc
->r_extern() ) {
5755 target
.addend
= dstAddr
;
5758 parser
.findTargetFromAddressAndSectionNum(dstAddr
, reloc
->r_symbolnum(), target
);
5760 parser
.addFixups(src
, ld::Fixup::kindStorePPCAbsLow14
, target
);
5762 case PPC_RELOC_HI16
:
5763 if ( nextReloc
->r_type() != PPC_RELOC_PAIR
)
5764 throw "PPC_RELOC_HI16 missing following pair";
5766 lowBits
= (nextReloc
->r_address() & 0xFFFF);
5767 dstAddr
= ((instruction
& 0xFFFF) << 16) | (lowBits
& 0x0000FFFF);
5768 if ( reloc
->r_extern() ) {
5769 target
.addend
= dstAddr
;
5772 parser
.findTargetFromAddressAndSectionNum(dstAddr
, reloc
->r_symbolnum(), target
);
5774 parser
.addFixups(src
, ld::Fixup::kindStorePPCAbsHigh16
, target
);
5776 case PPC_RELOC_HA16
:
5777 if ( nextReloc
->r_type() != PPC_RELOC_PAIR
)
5778 throw "PPC_RELOC_HA16 missing following pair";
5780 lowBits
= (nextReloc
->r_address() & 0x0000FFFF);
5781 dstAddr
= ((instruction
& 0xFFFF) << 16) + (int32_t)lowBits
;
5782 if ( reloc
->r_extern() ) {
5783 target
.addend
= dstAddr
;
5786 parser
.findTargetFromAddressAndSectionNum(dstAddr
, reloc
->r_symbolnum(), target
);
5788 parser
.addFixups(src
, ld::Fixup::kindStorePPCAbsHigh16AddLow
, target
);
5790 case PPC_RELOC_VANILLA
:
5791 contentValue
= P::getP(*((pint_t
*)fixUpPtr
));
5792 if ( reloc
->r_extern() ) {
5793 target
.addend
= contentValue
;
5796 parser
.findTargetFromAddressAndSectionNum(contentValue
, reloc
->r_symbolnum(), target
);
5798 switch ( reloc
->r_length() ) {
5801 throw "bad r_length in PPC_RELOC_VANILLA";
5803 parser
.addFixups(src
, ld::Fixup::kindStoreBigEndian32
, target
);
5806 parser
.addFixups(src
, ld::Fixup::kindStoreBigEndian64
, target
);
5810 case PPC_RELOC_JBSR
:
5811 // this is from -mlong-branch codegen. We ignore the jump island and make reference to the real target
5812 if ( nextReloc
->r_type() != PPC_RELOC_PAIR
)
5813 throw "PPC_RELOC_JBSR missing following pair";
5814 if ( !parser
._hasLongBranchStubs
)
5815 warning("object file compiled with -mlong-branch which is no longer needed. "
5816 "To remove this warning, recompile without -mlong-branch: %s", parser
._path
);
5817 parser
._hasLongBranchStubs
= true;
5819 if ( reloc
->r_extern() ) {
5820 throw "PPC_RELOC_JBSR should not be using an external relocation";
5822 parser
.findTargetFromAddressAndSectionNum(nextReloc
->r_address(), reloc
->r_symbolnum(), target
);
5823 parser
.addFixups(src
, ld::Fixup::kindStorePPCBranch24
, target
);
5826 warning("unknown relocation type %d", reloc
->r_type());
5830 const macho_scattered_relocation_info
<P
>* sreloc
= (macho_scattered_relocation_info
<P
>*)reloc
;
5831 // file format allows pair to be scattered or not
5832 const macho_scattered_relocation_info
<P
>* nextSReloc
= &sreloc
[1];
5833 const macho_relocation_info
<P
>* nextReloc
= &reloc
[1];
5834 srcAddr
= sect
->addr() + sreloc
->r_address();
5835 dstAddr
= sreloc
->r_value();
5836 fixUpPtr
= (uint32_t*)(file().fileContent() + sect
->offset() + sreloc
->r_address());
5837 instruction
= BigEndian::get32(*fixUpPtr
);
5838 src
.atom
= this->findAtomByAddress(srcAddr
);
5839 src
.offsetInAtom
= srcAddr
- src
.atom
->_objAddress
;
5840 typename Parser
<A
>::TargetDesc picBase
;
5841 bool nextRelocIsPair
= false;
5842 uint32_t nextRelocAddress
= 0;
5843 uint32_t nextRelocValue
= 0;
5844 if ( (nextReloc
->r_address() & R_SCATTERED
) == 0 ) {
5845 if ( nextReloc
->r_type() == PPC_RELOC_PAIR
) {
5846 nextRelocIsPair
= true;
5847 nextRelocAddress
= nextReloc
->r_address();
5852 if ( nextSReloc
->r_type() == PPC_RELOC_PAIR
) {
5853 nextRelocIsPair
= true;
5854 nextRelocAddress
= nextSReloc
->r_address();
5855 nextRelocValue
= nextSReloc
->r_value();
5859 switch ( sreloc
->r_type() ) {
5860 case PPC_RELOC_VANILLA
:
5861 // with a scattered relocation we get both the target (sreloc->r_value()) and the target+offset (*fixUpPtr)
5862 target
.atom
= parser
.findAtomByAddress(sreloc
->r_value());
5863 switch ( sreloc
->r_length() ) {
5866 throw "unsuppored r_length < 2 for scattered PPC_RELOC_VANILLA";
5868 contentValue
= BigEndian::get32(*(uint32_t*)fixUpPtr
);
5869 target
.addend
= contentValue
- target
.atom
->_objAddress
;
5870 parser
.addFixups(src
, ld::Fixup::kindStoreBigEndian32
, target
);
5873 contentValue
= BigEndian::get64(*(uint64_t*)fixUpPtr
);
5874 target
.addend
= contentValue
- target
.atom
->_objAddress
;
5875 parser
.addFixups(src
, ld::Fixup::kindStoreBigEndian64
, target
);
5879 case PPC_RELOC_BR14
:
5880 displacement
= (instruction
& 0x0000FFFC);
5881 if ( (displacement
& 0x00008000) != 0 )
5882 displacement
|= 0xFFFF0000;
5883 target
.atom
= parser
.findAtomByAddress(sreloc
->r_value());
5884 target
.addend
= (srcAddr
+ displacement
) - target
.atom
->_objAddress
;
5885 parser
.addFixups(src
, ld::Fixup::kindStorePPCBranch14
, target
);
5887 case PPC_RELOC_BR24
:
5888 assert((instruction
& 0x4C000000) == 0x48000000);
5889 displacement
= (instruction
& 0x03FFFFFC);
5890 if ( (displacement
& 0x02000000) != 0 )
5891 displacement
|= 0xFC000000;
5892 target
.atom
= parser
.findAtomByAddress(sreloc
->r_value());
5893 target
.addend
= (srcAddr
+ displacement
) - target
.atom
->_objAddress
;
5894 parser
.addFixups(src
, ld::Fixup::kindStorePPCBranch24
, target
);
5896 case PPC_RELOC_LO16_SECTDIFF
:
5897 if ( ! nextRelocIsPair
)
5898 throw "PPC_RELOC_LO16_SECTDIFF missing following pair";
5899 lowBits
= (instruction
& 0xFFFF);
5900 dstAddr
= nextRelocValue
+ ((nextRelocAddress
<< 16) | ((uint32_t)lowBits
& 0x0000FFFF));
5901 parser
.findTargetFromAddress(sreloc
->r_value(), target
);
5902 if ( target
.atom
!= NULL
)
5903 target
.addend
= dstAddr
- target
.atom
->_objAddress
;
5904 picBase
.atom
= parser
.findAtomByAddress(nextRelocValue
);
5905 picBase
.addend
= nextRelocValue
- picBase
.atom
->_objAddress
;
5906 picBase
.weakImport
= false;
5907 picBase
.name
= NULL
;
5908 parser
.addFixups(src
, ld::Fixup::kindStorePPCPicLow16
, target
, picBase
);
5910 case PPC_RELOC_LO14_SECTDIFF
:
5911 if ( ! nextRelocIsPair
)
5912 throw "PPC_RELOC_LO14_SECTDIFF missing following pair";
5913 lowBits
= (instruction
& 0xFFFC);
5914 dstAddr
= nextRelocValue
+ ((nextRelocAddress
<< 16) | ((uint32_t)lowBits
& 0x0000FFFF));
5915 parser
.findTargetFromAddress(sreloc
->r_value(), target
);
5916 if ( target
.atom
!= NULL
)
5917 target
.addend
= dstAddr
- target
.atom
->_objAddress
;
5918 picBase
.atom
= parser
.findAtomByAddress(nextRelocValue
);
5919 picBase
.addend
= nextRelocValue
- picBase
.atom
->_objAddress
;
5920 picBase
.weakImport
= false;
5921 picBase
.name
= NULL
;
5922 parser
.addFixups(src
, ld::Fixup::kindStorePPCPicLow14
, target
, picBase
);
5924 case PPC_RELOC_HA16_SECTDIFF
:
5925 if ( ! nextRelocIsPair
)
5926 throw "PPC_RELOC_HA16_SECTDIFF missing following pair";
5927 lowBits
= (nextRelocAddress
& 0x0000FFFF);
5928 dstAddr
= nextRelocValue
+ (((instruction
& 0x0000FFFF) << 16) + (int32_t)lowBits
);
5929 parser
.findTargetFromAddress(sreloc
->r_value(), target
);
5930 if ( target
.atom
!= NULL
)
5931 target
.addend
= dstAddr
- target
.atom
->_objAddress
;
5932 picBase
.atom
= parser
.findAtomByAddress(nextRelocValue
);
5933 picBase
.addend
= nextRelocValue
- picBase
.atom
->_objAddress
;
5934 picBase
.weakImport
= false;
5935 picBase
.name
= NULL
;
5936 parser
.addFixups(src
, ld::Fixup::kindStorePPCPicHigh16AddLow
, target
, picBase
);
5938 case PPC_RELOC_LO14
:
5939 if ( ! nextRelocIsPair
)
5940 throw "PPC_RELOC_LO14 missing following pair";
5941 lowBits
= (instruction
& 0xFFFC);
5942 dstAddr
= ((nextRelocAddress
<< 16) + ((uint32_t)lowBits
& 0x0000FFFF));
5943 parser
.findTargetFromAddress(sreloc
->r_value(), dstAddr
, target
);
5944 parser
.addFixups(src
, ld::Fixup::kindStorePPCAbsLow14
, target
);
5946 case PPC_RELOC_LO16
:
5947 if ( ! nextRelocIsPair
)
5948 throw "PPC_RELOC_LO16 missing following pair";
5949 lowBits
= (instruction
& 0xFFFF);
5950 dstAddr
= ((nextRelocAddress
<< 16) + ((uint32_t)lowBits
& 0x0000FFFF));
5951 parser
.findTargetFromAddress(sreloc
->r_value(), dstAddr
, target
);
5952 parser
.addFixups(src
, ld::Fixup::kindStorePPCAbsLow16
, target
);
5954 case PPC_RELOC_HA16
:
5955 if ( ! nextRelocIsPair
)
5956 throw "PPC_RELOC_HA16 missing following pair";
5957 lowBits
= (nextRelocAddress
& 0xFFFF);
5958 dstAddr
= (((instruction
& 0xFFFF) << 16) + (int32_t)lowBits
);
5959 parser
.findTargetFromAddress(sreloc
->r_value(), dstAddr
, target
);
5960 parser
.addFixups(src
, ld::Fixup::kindStorePPCAbsHigh16AddLow
, target
);
5962 case PPC_RELOC_HI16
:
5963 if ( ! nextRelocIsPair
)
5964 throw "PPC_RELOC_HI16 missing following pair";
5965 lowBits
= (nextRelocAddress
& 0xFFFF);
5966 dstAddr
= ((instruction
& 0xFFFF) << 16) | (lowBits
& 0x0000FFFF);
5967 parser
.findTargetFromAddress(sreloc
->r_value(), dstAddr
, target
);
5968 parser
.addFixups(src
, ld::Fixup::kindStorePPCAbsHigh16
, target
);
5970 case PPC_RELOC_SECTDIFF
:
5971 case PPC_RELOC_LOCAL_SECTDIFF
:
5973 if ( ! nextRelocIsPair
)
5974 throw "PPC_RELOC_SECTDIFF missing following pair";
5975 ld::Fixup::Kind kind
= ld::Fixup::kindNone
;
5976 switch ( sreloc
->r_length() ) {
5978 throw "bad length for PPC_RELOC_SECTDIFF";
5980 contentValue
= (int32_t)(int16_t)BigEndian::get16(*((uint16_t*)fixUpPtr
));
5981 kind
= ld::Fixup::kindStoreBigEndian16
;
5984 contentValue
= BigEndian::get32(*((uint32_t*)fixUpPtr
));
5985 kind
= ld::Fixup::kindStoreBigEndian32
;
5988 contentValue
= BigEndian::get64(*((uint64_t*)fixUpPtr
));
5989 kind
= ld::Fixup::kindStoreBigEndian64
;
5993 Atom
<A
>* fromAtom
= parser
.findAtomByAddress(nextRelocValue
);
5994 Atom
<A
>* targetAtom
= parser
.findAtomByAddress(sreloc
->r_value());
5995 uint32_t offsetInFrom
= nextRelocValue
- fromAtom
->_objAddress
;
5996 uint32_t offsetInTarget
= sreloc
->r_value() - targetAtom
->_objAddress
;
5997 // check for addend encoded in the section content
5998 int32_t addend
= contentValue
- (sreloc
->r_value() - nextRelocValue
);
6000 if ( targetAtom
->scope() == ld::Atom::scopeTranslationUnit
) {
6001 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, targetAtom
);
6003 else if ( (targetAtom
->combine() == ld::Atom::combineByNameAndContent
) || (targetAtom
->combine() == ld::Atom::combineByNameAndReferences
) ) {
6004 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, targetAtom
);
6007 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, false, targetAtom
->name());
6009 parser
.addFixup(src
, ld::Fixup::k2of5
, ld::Fixup::kindAddAddend
, offsetInTarget
);
6010 parser
.addFixup(src
, ld::Fixup::k3of5
, ld::Fixup::kindSubtractTargetAddress
, fromAtom
);
6011 parser
.addFixup(src
, ld::Fixup::k4of5
, ld::Fixup::kindSubtractAddend
, offsetInFrom
-addend
);
6012 parser
.addFixup(src
, ld::Fixup::k5of5
, kind
);
6015 if ( targetAtom
->scope() == ld::Atom::scopeTranslationUnit
) {
6016 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, targetAtom
);
6018 else if ( (targetAtom
->combine() == ld::Atom::combineByNameAndContent
) || (targetAtom
->combine() == ld::Atom::combineByNameAndReferences
) ) {
6019 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, targetAtom
);
6022 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, false, targetAtom
->name());
6024 parser
.addFixup(src
, ld::Fixup::k2of5
, ld::Fixup::kindAddAddend
, offsetInTarget
+addend
);
6025 parser
.addFixup(src
, ld::Fixup::k3of5
, ld::Fixup::kindSubtractTargetAddress
, fromAtom
);
6026 parser
.addFixup(src
, ld::Fixup::k4of5
, ld::Fixup::kindSubtractAddend
, offsetInFrom
);
6027 parser
.addFixup(src
, ld::Fixup::k5of5
, kind
);
6031 case PPC_RELOC_PAIR
:
6033 case PPC_RELOC_HI16_SECTDIFF
:
6034 warning("unexpected scattered relocation type PPC_RELOC_HI16_SECTDIFF");
6037 warning("unknown scattered relocation type %d", sreloc
->r_type());
6045 bool Section
<ppc
>::addRelocFixup(class Parser
<ppc
>& parser
, const macho_relocation_info
<P
>* reloc
)
6047 return addRelocFixup_powerpc(parser
, reloc
);
6052 bool Section
<ppc64
>::addRelocFixup(class Parser
<ppc64
>& parser
, const macho_relocation_info
<P
>* reloc
)
6054 return addRelocFixup_powerpc(parser
, reloc
);
6060 bool Section
<arm
>::addRelocFixup(class Parser
<arm
>& parser
, const macho_relocation_info
<P
>* reloc
)
6062 const macho_section
<P
>* sect
= this->machoSection();
6063 bool result
= false;
6067 int32_t displacement
= 0;
6068 uint32_t instruction
= 0;
6069 pint_t contentValue
= 0;
6070 Parser
<arm
>::SourceLocation src
;
6071 Parser
<arm
>::TargetDesc target
;
6072 const macho_relocation_info
<P
>* nextReloc
;
6074 if ( (reloc
->r_address() & R_SCATTERED
) == 0 ) {
6075 bool externSymbolIsThumbDef
= false;
6076 srcAddr
= sect
->addr() + reloc
->r_address();
6077 src
.atom
= this->findAtomByAddress(srcAddr
);
6078 src
.offsetInAtom
= srcAddr
- src
.atom
->_objAddress
;
6079 fixUpPtr
= (uint32_t*)(file().fileContent() + sect
->offset() + reloc
->r_address());
6080 if ( reloc
->r_type() != ARM_RELOC_PAIR
)
6081 instruction
= LittleEndian::get32(*fixUpPtr
);
6082 if ( reloc
->r_extern() ) {
6083 const macho_nlist
<P
>& targetSymbol
= parser
.symbolFromIndex(reloc
->r_symbolnum());
6084 // use direct reference for local symbols
6085 if ( ((targetSymbol
.n_type() & N_TYPE
) == N_SECT
) && (((targetSymbol
.n_type() & N_EXT
) == 0) || (parser
.nameFromSymbol(targetSymbol
)[0] == 'L')) ) {
6086 parser
.findTargetFromAddressAndSectionNum(targetSymbol
.n_value(), targetSymbol
.n_sect(), target
);
6090 target
.name
= parser
.nameFromSymbol(targetSymbol
);
6091 target
.weakImport
= parser
.weakImportFromSymbol(targetSymbol
);
6092 if ( ((targetSymbol
.n_type() & N_TYPE
) == N_SECT
) && (targetSymbol
.n_desc() & N_ARM_THUMB_DEF
) )
6093 externSymbolIsThumbDef
= true;
6096 switch ( reloc
->r_type() ) {
6097 case ARM_RELOC_BR24
:
6098 // Sign-extend displacement
6099 displacement
= (instruction
& 0x00FFFFFF) << 2;
6100 if ( (displacement
& 0x02000000) != 0 )
6101 displacement
|= 0xFC000000;
6102 // The pc added will be +8 from the pc
6104 // If this is BLX add H << 1
6105 if ((instruction
& 0xFE000000) == 0xFA000000)
6106 displacement
+= ((instruction
& 0x01000000) >> 23);
6107 if ( reloc
->r_extern() ) {
6108 target
.addend
= srcAddr
+ displacement
;
6109 if ( externSymbolIsThumbDef
)
6110 target
.addend
&= -2; // remove thumb bit
6113 dstAddr
= srcAddr
+ displacement
;
6114 parser
.findTargetFromAddressAndSectionNum(dstAddr
, reloc
->r_symbolnum(), target
);
6116 // special case "calls" for dtrace
6117 if ( (target
.name
!= NULL
) && (strncmp(target
.name
, "___dtrace_probe$", 16) == 0) ) {
6118 parser
.addFixup(src
, ld::Fixup::k1of1
,
6119 ld::Fixup::kindStoreARMDtraceCallSiteNop
, false, target
.name
);
6120 parser
.addDtraceExtraInfos(src
, &target
.name
[16]);
6122 else if ( (target
.name
!= NULL
) && (strncmp(target
.name
, "___dtrace_isenabled$", 20) == 0) ) {
6123 parser
.addFixup(src
, ld::Fixup::k1of1
,
6124 ld::Fixup::kindStoreARMDtraceIsEnableSiteClear
, false, target
.name
);
6125 parser
.addDtraceExtraInfos(src
, &target
.name
[20]);
6128 parser
.addFixups(src
, ld::Fixup::kindStoreARMBranch24
, target
);
6131 case ARM_THUMB_RELOC_BR22
:
6132 // thumb2 added two more bits to displacement, complicating the displacement decoding
6134 uint32_t s
= (instruction
>> 10) & 0x1;
6135 uint32_t j1
= (instruction
>> 29) & 0x1;
6136 uint32_t j2
= (instruction
>> 27) & 0x1;
6137 uint32_t imm10
= instruction
& 0x3FF;
6138 uint32_t imm11
= (instruction
>> 16) & 0x7FF;
6139 uint32_t i1
= (j1
== s
);
6140 uint32_t i2
= (j2
== s
);
6141 uint32_t dis
= (s
<< 24) | (i1
<< 23) | (i2
<< 22) | (imm10
<< 12) | (imm11
<< 1);
6145 displacement
= sdis
;
6147 // The pc added will be +4 from the pc
6149 // If the instruction was blx, force the low 2 bits to be clear
6150 dstAddr
= srcAddr
+ displacement
;
6151 if ((instruction
& 0xF8000000) == 0xE8000000)
6152 dstAddr
&= 0xFFFFFFFC;
6154 if ( reloc
->r_extern() ) {
6155 target
.addend
= dstAddr
;
6158 parser
.findTargetFromAddressAndSectionNum(dstAddr
, reloc
->r_symbolnum(), target
);
6160 // special case "calls" for dtrace
6161 if ( (target
.name
!= NULL
) && (strncmp(target
.name
, "___dtrace_probe$", 16) == 0) ) {
6162 parser
.addFixup(src
, ld::Fixup::k1of1
,
6163 ld::Fixup::kindStoreThumbDtraceCallSiteNop
, false, target
.name
);
6164 parser
.addDtraceExtraInfos(src
, &target
.name
[16]);
6166 else if ( (target
.name
!= NULL
) && (strncmp(target
.name
, "___dtrace_isenabled$", 20) == 0) ) {
6167 parser
.addFixup(src
, ld::Fixup::k1of1
,
6168 ld::Fixup::kindStoreThumbDtraceIsEnableSiteClear
, false, target
.name
);
6169 parser
.addDtraceExtraInfos(src
, &target
.name
[20]);
6172 parser
.addFixups(src
, ld::Fixup::kindStoreThumbBranch22
, target
);
6175 case ARM_RELOC_VANILLA
:
6176 if ( reloc
->r_length() != 2 )
6177 throw "bad length for ARM_RELOC_VANILLA";
6178 contentValue
= LittleEndian::get32(*fixUpPtr
);
6179 if ( reloc
->r_extern() ) {
6180 target
.addend
= (int32_t)contentValue
;
6181 if ( externSymbolIsThumbDef
)
6182 target
.addend
&= -2; // remove thumb bit
6185 parser
.findTargetFromAddressAndSectionNum(contentValue
, reloc
->r_symbolnum(), target
);
6186 // possible non-extern relocation turned into by-name ref because target is a weak-def
6187 if ( target
.atom
!= NULL
) {
6188 if ( target
.atom
->isThumb() )
6189 target
.addend
&= -2; // remove thumb bit
6190 // if reference to LSDA, add group subordinate fixup
6191 if ( target
.atom
->contentType() == ld::Atom::typeLSDA
) {
6192 Parser
<arm
>::SourceLocation src2
;
6193 src2
.atom
= src
.atom
;
6194 src2
.offsetInAtom
= 0;
6195 parser
.addFixup(src2
, ld::Fixup::k1of1
, ld::Fixup::kindNoneGroupSubordinateLSDA
, target
.atom
);
6199 parser
.addFixups(src
, ld::Fixup::kindStoreLittleEndian32
, target
);
6201 case ARM_THUMB_32BIT_BRANCH
:
6202 // silently ignore old unnecessary reloc
6204 case ARM_RELOC_HALF
:
6205 nextReloc
= &reloc
[1];
6206 if ( nextReloc
->r_type() == ARM_RELOC_PAIR
) {
6207 uint32_t instruction16
;
6208 uint32_t other16
= (nextReloc
->r_address() & 0xFFFF);
6210 if ( reloc
->r_length() & 2 ) {
6212 uint32_t i
= ((instruction
& 0x00000400) >> 10);
6213 uint32_t imm4
= (instruction
& 0x0000000F);
6214 uint32_t imm3
= ((instruction
& 0x70000000) >> 28);
6215 uint32_t imm8
= ((instruction
& 0x00FF0000) >> 16);
6216 instruction16
= (imm4
<< 12) | (i
<< 11) | (imm3
<< 8) | imm8
;
6220 uint32_t imm4
= ((instruction
& 0x000F0000) >> 16);
6221 uint32_t imm12
= (instruction
& 0x00000FFF);
6222 instruction16
= (imm4
<< 12) | imm12
;
6224 if ( reloc
->r_length() & 1 ) {
6226 dstAddr
= ((instruction16
<< 16) | other16
);
6227 if ( reloc
->r_extern() ) {
6228 target
.addend
= dstAddr
;
6231 parser
.findTargetFromAddress(dstAddr
, target
);
6232 if ( target
.atom
->isThumb() )
6233 target
.addend
&= (-2); // remove thumb bit
6235 parser
.addFixups(src
, (isThumb
? ld::Fixup::kindStoreThumbHigh16
: ld::Fixup::kindStoreARMHigh16
), target
);
6239 dstAddr
= (other16
<< 16) | instruction16
;
6240 if ( reloc
->r_extern() ) {
6241 target
.addend
= dstAddr
;
6244 parser
.findTargetFromAddress(dstAddr
, target
);
6245 if ( target
.atom
->isThumb() )
6246 target
.addend
&= (-2); // remove thumb bit
6248 parser
.addFixups(src
, (isThumb
? ld::Fixup::kindStoreThumbLow16
: ld::Fixup::kindStoreARMLow16
), target
);
6253 throw "for ARM_RELOC_HALF, next reloc is not ARM_RELOC_PAIR";
6256 throwf("unknown relocation type %d", reloc
->r_type());
6261 const macho_scattered_relocation_info
<P
>* sreloc
= (macho_scattered_relocation_info
<P
>*)reloc
;
6262 // file format allows pair to be scattered or not
6263 const macho_scattered_relocation_info
<P
>* nextSReloc
= &sreloc
[1];
6264 nextReloc
= &reloc
[1];
6265 srcAddr
= sect
->addr() + sreloc
->r_address();
6266 dstAddr
= sreloc
->r_value();
6267 fixUpPtr
= (uint32_t*)(file().fileContent() + sect
->offset() + sreloc
->r_address());
6268 instruction
= LittleEndian::get32(*fixUpPtr
);
6269 src
.atom
= this->findAtomByAddress(srcAddr
);
6270 src
.offsetInAtom
= srcAddr
- src
.atom
->_objAddress
;
6271 bool nextRelocIsPair
= false;
6272 uint32_t nextRelocAddress
= 0;
6273 uint32_t nextRelocValue
= 0;
6274 if ( (nextReloc
->r_address() & R_SCATTERED
) == 0 ) {
6275 if ( nextReloc
->r_type() == ARM_RELOC_PAIR
) {
6276 nextRelocIsPair
= true;
6277 nextRelocAddress
= nextReloc
->r_address();
6282 if ( nextSReloc
->r_type() == ARM_RELOC_PAIR
) {
6283 nextRelocIsPair
= true;
6284 nextRelocAddress
= nextSReloc
->r_address();
6285 nextRelocValue
= nextSReloc
->r_value();
6289 switch ( sreloc
->r_type() ) {
6290 case ARM_RELOC_VANILLA
:
6291 // with a scattered relocation we get both the target (sreloc->r_value()) and the target+offset (*fixUpPtr)
6292 if ( sreloc
->r_length() != 2 )
6293 throw "bad length for ARM_RELOC_VANILLA";
6294 target
.atom
= parser
.findAtomByAddress(sreloc
->r_value());
6295 contentValue
= LittleEndian::get32(*fixUpPtr
);
6296 target
.addend
= contentValue
- target
.atom
->_objAddress
;
6297 if ( target
.atom
->isThumb() )
6298 target
.addend
&= -2; // remove thumb bit
6299 parser
.addFixups(src
, ld::Fixup::kindStoreLittleEndian32
, target
);
6301 case ARM_RELOC_BR24
:
6302 // Sign-extend displacement
6303 displacement
= (instruction
& 0x00FFFFFF) << 2;
6304 if ( (displacement
& 0x02000000) != 0 )
6305 displacement
|= 0xFC000000;
6306 // The pc added will be +8 from the pc
6308 // If this is BLX add H << 1
6309 if ((instruction
& 0xFE000000) == 0xFA000000)
6310 displacement
+= ((instruction
& 0x01000000) >> 23);
6311 target
.atom
= parser
.findAtomByAddress(sreloc
->r_value());
6312 target
.addend
= (int64_t)(srcAddr
+ displacement
) - (int64_t)(target
.atom
->_objAddress
);
6313 parser
.addFixups(src
, ld::Fixup::kindStoreARMBranch24
, target
);
6315 case ARM_THUMB_RELOC_BR22
:
6316 // thumb2 added two more bits to displacement, complicating the displacement decoding
6318 uint32_t s
= (instruction
>> 10) & 0x1;
6319 uint32_t j1
= (instruction
>> 29) & 0x1;
6320 uint32_t j2
= (instruction
>> 27) & 0x1;
6321 uint32_t imm10
= instruction
& 0x3FF;
6322 uint32_t imm11
= (instruction
>> 16) & 0x7FF;
6323 uint32_t i1
= (j1
== s
);
6324 uint32_t i2
= (j2
== s
);
6325 uint32_t dis
= (s
<< 24) | (i1
<< 23) | (i2
<< 22) | (imm10
<< 12) | (imm11
<< 1);
6329 displacement
= sdis
;
6331 // The pc added will be +4 from the pc
6333 dstAddr
= srcAddr
+displacement
;
6334 // If the instruction was blx, force the low 2 bits to be clear
6335 if ((instruction
& 0xF8000000) == 0xE8000000)
6336 dstAddr
&= 0xFFFFFFFC;
6337 target
.atom
= parser
.findAtomByAddress(sreloc
->r_value());
6338 target
.addend
= dstAddr
- target
.atom
->_objAddress
;
6339 parser
.addFixups(src
, ld::Fixup::kindStoreThumbBranch22
, target
);
6341 case ARM_RELOC_SECTDIFF
:
6342 case ARM_RELOC_LOCAL_SECTDIFF
:
6344 if ( ! nextRelocIsPair
)
6345 throw "ARM_RELOC_SECTDIFF missing following pair";
6346 if ( sreloc
->r_length() != 2 )
6347 throw "bad length for ARM_RELOC_SECTDIFF";
6348 contentValue
= LittleEndian::get32(*fixUpPtr
);
6349 Atom
<arm
>* fromAtom
= parser
.findAtomByAddress(nextRelocValue
);
6350 uint32_t offsetInFrom
= nextRelocValue
- fromAtom
->_objAddress
;
6351 uint32_t offsetInTarget
;
6352 Atom
<arm
>* targetAtom
= parser
.findAtomByAddressOrLocalTargetOfStub(sreloc
->r_value(), &offsetInTarget
);
6353 // check for addend encoded in the section content
6354 int64_t addend
= (int32_t)contentValue
- (int32_t)(sreloc
->r_value() - nextRelocValue
);
6355 if ( targetAtom
->isThumb() )
6356 addend
&= -2; // remove thumb bit
6357 // if reference to LSDA, add group subordinate fixup
6358 if ( targetAtom
->contentType() == ld::Atom::typeLSDA
) {
6359 Parser
<arm
>::SourceLocation src2
;
6360 src2
.atom
= src
.atom
;
6361 src2
.offsetInAtom
= 0;
6362 parser
.addFixup(src2
, ld::Fixup::k1of1
, ld::Fixup::kindNoneGroupSubordinateLSDA
, targetAtom
);
6365 // switch binding base on coalescing
6366 if ( targetAtom
->scope() == ld::Atom::scopeTranslationUnit
) {
6367 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, targetAtom
);
6369 else if ( (targetAtom
->combine() == ld::Atom::combineByNameAndContent
) || (targetAtom
->combine() == ld::Atom::combineByNameAndReferences
) ) {
6370 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, targetAtom
);
6373 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, false, targetAtom
->name());
6375 parser
.addFixup(src
, ld::Fixup::k2of5
, ld::Fixup::kindAddAddend
, offsetInTarget
);
6376 parser
.addFixup(src
, ld::Fixup::k3of5
, ld::Fixup::kindSubtractTargetAddress
, fromAtom
);
6377 parser
.addFixup(src
, ld::Fixup::k4of5
, ld::Fixup::kindSubtractAddend
, offsetInFrom
-addend
);
6378 parser
.addFixup(src
, ld::Fixup::k5of5
, ld::Fixup::kindStoreLittleEndian32
);
6381 if ( targetAtom
->scope() == ld::Atom::scopeTranslationUnit
) {
6382 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, targetAtom
);
6384 else if ( (targetAtom
->combine() == ld::Atom::combineByNameAndContent
) || (targetAtom
->combine() == ld::Atom::combineByNameAndReferences
) ) {
6385 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, targetAtom
);
6388 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, false, targetAtom
->name());
6390 parser
.addFixup(src
, ld::Fixup::k2of5
, ld::Fixup::kindAddAddend
, (uint32_t)(offsetInTarget
+addend
));
6391 parser
.addFixup(src
, ld::Fixup::k3of5
, ld::Fixup::kindSubtractTargetAddress
, fromAtom
);
6392 parser
.addFixup(src
, ld::Fixup::k4of5
, ld::Fixup::kindSubtractAddend
, offsetInFrom
);
6393 parser
.addFixup(src
, ld::Fixup::k5of5
, ld::Fixup::kindStoreLittleEndian32
);
6397 case ARM_RELOC_HALF_SECTDIFF
:
6398 if ( nextRelocIsPair
) {
6399 instruction
= LittleEndian::get32(*fixUpPtr
);
6400 Atom
<arm
>* fromAtom
= parser
.findAtomByAddress(nextRelocValue
);
6401 uint32_t offsetInFrom
= nextRelocValue
- fromAtom
->_objAddress
;
6402 Atom
<arm
>* targetAtom
= parser
.findAtomByAddress(sreloc
->r_value());
6403 uint32_t offsetInTarget
= sreloc
->r_value() - targetAtom
->_objAddress
;
6404 uint32_t instruction16
;
6405 uint32_t other16
= (nextRelocAddress
& 0xFFFF);
6407 if ( sreloc
->r_length() & 2 ) {
6409 uint32_t i
= ((instruction
& 0x00000400) >> 10);
6410 uint32_t imm4
= (instruction
& 0x0000000F);
6411 uint32_t imm3
= ((instruction
& 0x70000000) >> 28);
6412 uint32_t imm8
= ((instruction
& 0x00FF0000) >> 16);
6413 instruction16
= (imm4
<< 12) | (i
<< 11) | (imm3
<< 8) | imm8
;
6417 uint32_t imm4
= ((instruction
& 0x000F0000) >> 16);
6418 uint32_t imm12
= (instruction
& 0x00000FFF);
6419 instruction16
= (imm4
<< 12) | imm12
;
6421 if ( sreloc
->r_length() & 1 )
6422 dstAddr
= ((instruction16
<< 16) | other16
);
6424 dstAddr
= (other16
<< 16) | instruction16
;
6425 if ( targetAtom
->isThumb() )
6426 dstAddr
&= (-2); // remove thumb bit
6427 int32_t addend
= dstAddr
- (sreloc
->r_value() - nextRelocValue
);
6428 if ( targetAtom
->scope() == ld::Atom::scopeTranslationUnit
) {
6429 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, targetAtom
);
6431 else if ( (targetAtom
->combine() == ld::Atom::combineByNameAndContent
) || (targetAtom
->combine() == ld::Atom::combineByNameAndReferences
) ) {
6432 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, targetAtom
);
6435 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, false, targetAtom
->name());
6437 parser
.addFixup(src
, ld::Fixup::k2of5
, ld::Fixup::kindAddAddend
, (uint32_t)offsetInTarget
+addend
);
6438 parser
.addFixup(src
, ld::Fixup::k3of5
, ld::Fixup::kindSubtractTargetAddress
, fromAtom
);
6439 parser
.addFixup(src
, ld::Fixup::k4of5
, ld::Fixup::kindSubtractAddend
, offsetInFrom
);
6440 if ( sreloc
->r_length() & 1 ) {
6442 parser
.addFixup(src
, ld::Fixup::k5of5
, (isThumb
? ld::Fixup::kindStoreThumbHigh16
: ld::Fixup::kindStoreARMHigh16
));
6446 parser
.addFixup(src
, ld::Fixup::k5of5
, (isThumb
? ld::Fixup::kindStoreThumbLow16
: ld::Fixup::kindStoreARMLow16
));
6451 throw "ARM_RELOC_HALF_SECTDIFF reloc missing following pair";
6453 case ARM_RELOC_HALF
:
6454 if ( nextRelocIsPair
) {
6455 instruction
= LittleEndian::get32(*fixUpPtr
);
6456 Atom
<arm
>* targetAtom
= parser
.findAtomByAddress(sreloc
->r_value());
6457 uint32_t instruction16
;
6458 uint32_t other16
= (nextRelocAddress
& 0xFFFF);
6460 if ( sreloc
->r_length() & 2 ) {
6462 uint32_t i
= ((instruction
& 0x00000400) >> 10);
6463 uint32_t imm4
= (instruction
& 0x0000000F);
6464 uint32_t imm3
= ((instruction
& 0x70000000) >> 28);
6465 uint32_t imm8
= ((instruction
& 0x00FF0000) >> 16);
6466 instruction16
= (imm4
<< 12) | (i
<< 11) | (imm3
<< 8) | imm8
;
6470 uint32_t imm4
= ((instruction
& 0x000F0000) >> 16);
6471 uint32_t imm12
= (instruction
& 0x00000FFF);
6472 instruction16
= (imm4
<< 12) | imm12
;
6474 if ( sreloc
->r_length() & 1 )
6475 dstAddr
= ((instruction16
<< 16) | other16
);
6477 dstAddr
= (other16
<< 16) | instruction16
;
6478 if ( targetAtom
->scope() == ld::Atom::scopeTranslationUnit
) {
6479 parser
.addFixup(src
, ld::Fixup::k1of3
, ld::Fixup::kindSetTargetAddress
, targetAtom
);
6481 else if ( (targetAtom
->combine() == ld::Atom::combineByNameAndContent
) || (targetAtom
->combine() == ld::Atom::combineByNameAndReferences
) ) {
6482 parser
.addFixup(src
, ld::Fixup::k1of3
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, targetAtom
);
6485 parser
.addFixup(src
, ld::Fixup::k1of3
, ld::Fixup::kindSetTargetAddress
, false, targetAtom
->name());
6487 parser
.addFixup(src
, ld::Fixup::k2of3
, ld::Fixup::kindAddAddend
, dstAddr
- targetAtom
->_objAddress
);
6488 if ( sreloc
->r_length() & 1 ) {
6490 parser
.addFixup(src
, ld::Fixup::k3of3
, (isThumb
? ld::Fixup::kindStoreThumbHigh16
: ld::Fixup::kindStoreARMHigh16
));
6494 parser
.addFixup(src
, ld::Fixup::k3of3
, (isThumb
? ld::Fixup::kindStoreThumbLow16
: ld::Fixup::kindStoreARMLow16
));
6499 throw "scattered ARM_RELOC_HALF reloc missing following pair";
6502 throwf("unknown ARM scattered relocation type %d", sreloc
->r_type());
6512 template <typename A
>
6513 bool ObjC1ClassSection
<A
>::addRelocFixup(class Parser
<A
>& parser
, const macho_relocation_info
<P
>* reloc
)
6516 FixedSizeSection
<A
>::addRelocFixup(parser
, reloc
);
6518 assert(0 && "needs template specialization");
6523 bool ObjC1ClassSection
<x86
>::addRelocFixup(class Parser
<x86
>& parser
, const macho_relocation_info
<x86::P
>* reloc
)
6525 // if this is the reloc for the super class name string, add implicit reference to super class
6526 if ( ((reloc
->r_address() & R_SCATTERED
) == 0) && (reloc
->r_type() == GENERIC_RELOC_VANILLA
) ) {
6527 assert( reloc
->r_length() == 2 );
6528 assert( ! reloc
->r_pcrel() );
6530 const macho_section
<P
>* sect
= this->machoSection();
6531 Parser
<x86
>::SourceLocation src
;
6532 uint32_t srcAddr
= sect
->addr() + reloc
->r_address();
6533 src
.atom
= this->findAtomByAddress(srcAddr
);
6534 src
.offsetInAtom
= srcAddr
- src
.atom
->objectAddress();
6535 if ( src
.offsetInAtom
== 4 ) {
6536 Parser
<x86
>::TargetDesc stringTarget
;
6537 const uint8_t* fixUpPtr
= file().fileContent() + sect
->offset() + reloc
->r_address();
6538 uint32_t contentValue
= LittleEndian::get32(*((uint32_t*)fixUpPtr
));
6539 parser
.findTargetFromAddressAndSectionNum(contentValue
, reloc
->r_symbolnum(), stringTarget
);
6541 assert(stringTarget
.atom
!= NULL
);
6542 assert(stringTarget
.atom
->contentType() == ld::Atom::typeCString
);
6543 const char* superClassBaseName
= (char*)stringTarget
.atom
->rawContentPointer();
6544 char* superClassName
= new char[strlen(superClassBaseName
) + 20];
6545 strcpy(superClassName
, ".objc_class_name_");
6546 strcat(superClassName
, superClassBaseName
);
6548 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindSetTargetAddress
, false, superClassName
);
6552 return FixedSizeSection
<x86
>::addRelocFixup(parser
, reloc
);
6556 bool ObjC1ClassSection
<ppc
>::addRelocFixup(class Parser
<ppc
>& parser
, const macho_relocation_info
<ppc::P
>* reloc
)
6558 // if this is the reloc for the super class name string, add implicit reference to super class
6559 if ( ((reloc
->r_address() & R_SCATTERED
) == 0) && (reloc
->r_type() == PPC_RELOC_VANILLA
) ) {
6560 assert( reloc
->r_length() == 2 );
6561 assert( ! reloc
->r_pcrel() );
6563 const macho_section
<P
>* sect
= this->machoSection();
6564 Parser
<ppc
>::SourceLocation src
;
6565 uint32_t srcAddr
= sect
->addr() + reloc
->r_address();
6566 src
.atom
= this->findAtomByAddress(srcAddr
);
6567 src
.offsetInAtom
= srcAddr
- src
.atom
->objectAddress();
6568 if ( src
.offsetInAtom
== 4 ) {
6569 Parser
<ppc
>::TargetDesc stringTarget
;
6570 const uint8_t* fixUpPtr
= file().fileContent() + sect
->offset() + reloc
->r_address();
6571 uint32_t contentValue
= BigEndian::get32(*((uint32_t*)fixUpPtr
));
6572 parser
.findTargetFromAddressAndSectionNum(contentValue
, reloc
->r_symbolnum(), stringTarget
);
6574 assert(stringTarget
.atom
!= NULL
);
6575 assert(stringTarget
.atom
->contentType() == ld::Atom::typeCString
);
6576 const char* superClassBaseName
= (char*)stringTarget
.atom
->rawContentPointer();
6577 char* superClassName
= new char[strlen(superClassBaseName
) + 20];
6578 strcpy(superClassName
, ".objc_class_name_");
6579 strcat(superClassName
, superClassBaseName
);
6581 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindSetTargetAddress
, false, superClassName
);
6586 return FixedSizeSection
<ppc
>::addRelocFixup(parser
, reloc
);
6592 template <typename A
>
6593 bool Objc1ClassReferences
<A
>::addRelocFixup(class Parser
<A
>& parser
, const macho_relocation_info
<P
>* reloc
)
6596 PointerToCStringSection
<A
>::addRelocFixup(parser
, reloc
);
6598 assert(0 && "needs template specialization");
6604 bool Objc1ClassReferences
<ppc
>::addRelocFixup(class Parser
<ppc
>& parser
, const macho_relocation_info
<ppc::P
>* reloc
)
6606 // add implict class refs, fixups not usable yet, so look at relocations
6607 assert( (reloc
->r_address() & R_SCATTERED
) == 0 );
6608 assert( reloc
->r_type() == PPC_RELOC_VANILLA
);
6609 assert( reloc
->r_length() == 2 );
6610 assert( ! reloc
->r_pcrel() );
6612 const macho_section
<P
>* sect
= this->machoSection();
6613 Parser
<ppc
>::SourceLocation src
;
6614 uint32_t srcAddr
= sect
->addr() + reloc
->r_address();
6615 src
.atom
= this->findAtomByAddress(srcAddr
);
6616 src
.offsetInAtom
= srcAddr
- src
.atom
->objectAddress();
6617 Parser
<ppc
>::TargetDesc stringTarget
;
6618 const uint8_t* fixUpPtr
= file().fileContent() + sect
->offset() + reloc
->r_address();
6619 uint32_t contentValue
= BigEndian::get32(*((uint32_t*)fixUpPtr
));
6620 parser
.findTargetFromAddressAndSectionNum(contentValue
, reloc
->r_symbolnum(), stringTarget
);
6622 assert(stringTarget
.atom
!= NULL
);
6623 assert(stringTarget
.atom
->contentType() == ld::Atom::typeCString
);
6624 const char* baseClassName
= (char*)stringTarget
.atom
->rawContentPointer();
6625 char* objcClassName
= new char[strlen(baseClassName
) + 20];
6626 strcpy(objcClassName
, ".objc_class_name_");
6627 strcat(objcClassName
, baseClassName
);
6629 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindSetTargetAddress
, false, objcClassName
);
6632 return PointerToCStringSection
<ppc
>::addRelocFixup(parser
, reloc
);
6637 bool Objc1ClassReferences
<x86
>::addRelocFixup(class Parser
<x86
>& parser
, const macho_relocation_info
<x86::P
>* reloc
)
6639 // add implict class refs, fixups not usable yet, so look at relocations
6640 assert( (reloc
->r_address() & R_SCATTERED
) == 0 );
6641 assert( reloc
->r_type() == GENERIC_RELOC_VANILLA
);
6642 assert( reloc
->r_length() == 2 );
6643 assert( ! reloc
->r_pcrel() );
6645 const macho_section
<P
>* sect
= this->machoSection();
6646 Parser
<x86
>::SourceLocation src
;
6647 uint32_t srcAddr
= sect
->addr() + reloc
->r_address();
6648 src
.atom
= this->findAtomByAddress(srcAddr
);
6649 src
.offsetInAtom
= srcAddr
- src
.atom
->objectAddress();
6650 Parser
<x86
>::TargetDesc stringTarget
;
6651 const uint8_t* fixUpPtr
= file().fileContent() + sect
->offset() + reloc
->r_address();
6652 uint32_t contentValue
= LittleEndian::get32(*((uint32_t*)fixUpPtr
));
6653 parser
.findTargetFromAddressAndSectionNum(contentValue
, reloc
->r_symbolnum(), stringTarget
);
6655 assert(stringTarget
.atom
!= NULL
);
6656 assert(stringTarget
.atom
->contentType() == ld::Atom::typeCString
);
6657 const char* baseClassName
= (char*)stringTarget
.atom
->rawContentPointer();
6658 char* objcClassName
= new char[strlen(baseClassName
) + 20];
6659 strcpy(objcClassName
, ".objc_class_name_");
6660 strcat(objcClassName
, baseClassName
);
6662 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindSetTargetAddress
, false, objcClassName
);
6665 return PointerToCStringSection
<x86
>::addRelocFixup(parser
, reloc
);
6669 template <typename A
>
6670 void Section
<A
>::makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
&)
6672 const macho_section
<P
>* sect
= this->machoSection();
6673 const macho_relocation_info
<P
>* relocs
= (macho_relocation_info
<P
>*)(file().fileContent() + sect
->reloff());
6674 const uint32_t relocCount
= sect
->nreloc();
6675 for (uint32_t r
= 0; r
< relocCount
; ++r
) {
6677 if ( this->addRelocFixup(parser
, &relocs
[r
]) )
6680 catch (const char* msg
) {
6681 throwf("in section %s,%s reloc %u: %s", sect
->segname(), Section
<A
>::makeSectionName(sect
), r
, msg
);
6685 // add follow-on fixups if .o file is missing .subsections_via_symbols
6686 if ( this->addFollowOnFixups() ) {
6687 Atom
<A
>* end
= &_endAtoms
[-1];
6688 for(Atom
<A
>* p
= _beginAtoms
; p
< end
; ++p
) {
6689 typename Parser
<A
>::SourceLocation
src(p
, 0);
6690 Atom
<A
>* nextAtom
= &p
[1];
6691 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindNoneFollowOn
, nextAtom
);
6694 else if ( this->type() == ld::Section::typeCode
) {
6695 // if FDE broke text not at a symbol, use followOn to keep code together
6696 Atom
<A
>* end
= &_endAtoms
[-1];
6697 for(Atom
<A
>* p
= _beginAtoms
; p
< end
; ++p
) {
6698 typename Parser
<A
>::SourceLocation
src(p
, 0);
6699 Atom
<A
>* nextAtom
= &p
[1];
6700 if ( (p
->symbolTableInclusion() == ld::Atom::symbolTableIn
) && (nextAtom
->symbolTableInclusion() == ld::Atom::symbolTableNotIn
) ) {
6701 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindNoneFollowOn
, nextAtom
);
6706 // add follow-on fixups for aliases
6707 if ( _hasAliases
) {
6708 for(Atom
<A
>* p
= _beginAtoms
; p
< _endAtoms
; ++p
) {
6709 if ( p
->isAlias() && ! this->addFollowOnFixups() ) {
6710 Atom
<A
>* targetOfAlias
= &p
[1];
6711 assert(p
< &_endAtoms
[-1]);
6712 assert(p
->_objAddress
== targetOfAlias
->_objAddress
);
6713 typename Parser
<A
>::SourceLocation
src(p
, 0);
6714 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindNoneFollowOn
, targetOfAlias
);
6723 // main function used by linker to instantiate ld::Files
6725 ld::relocatable::File
* parse(const uint8_t* fileContent
, uint64_t fileLength
,
6726 const char* path
, time_t modTime
, uint32_t ordinal
, const ParserOptions
& opts
)
6728 switch ( opts
.architecture
) {
6729 case CPU_TYPE_X86_64
:
6730 if ( mach_o::relocatable::Parser
<x86_64
>::validFile(fileContent
) )
6731 return mach_o::relocatable::Parser
<x86_64
>::parse(fileContent
, fileLength
, path
, modTime
, ordinal
, opts
);
6734 if ( mach_o::relocatable::Parser
<x86
>::validFile(fileContent
) )
6735 return mach_o::relocatable::Parser
<x86
>::parse(fileContent
, fileLength
, path
, modTime
, ordinal
, opts
);
6738 if ( mach_o::relocatable::Parser
<arm
>::validFile(fileContent
, opts
.objSubtypeMustMatch
, opts
.subType
) )
6739 return mach_o::relocatable::Parser
<arm
>::parse(fileContent
, fileLength
, path
, modTime
, ordinal
, opts
);
6741 case CPU_TYPE_POWERPC
:
6742 if ( mach_o::relocatable::Parser
<ppc
>::validFile(fileContent
) )
6743 return mach_o::relocatable::Parser
<ppc
>::parse(fileContent
, fileLength
, path
, modTime
, ordinal
, opts
);
6745 case CPU_TYPE_POWERPC64
:
6746 if ( mach_o::relocatable::Parser
<ppc64
>::validFile(fileContent
) )
6747 return mach_o::relocatable::Parser
<ppc64
>::parse(fileContent
, fileLength
, path
, modTime
, ordinal
, opts
);
6754 // used by archive reader to validate member object file
6756 bool isObjectFile(const uint8_t* fileContent
, uint64_t fileLength
, const ParserOptions
& opts
)
6758 switch ( opts
.architecture
) {
6759 case CPU_TYPE_X86_64
:
6760 return ( mach_o::relocatable::Parser
<x86_64
>::validFile(fileContent
) );
6762 return ( mach_o::relocatable::Parser
<x86
>::validFile(fileContent
) );
6764 return ( mach_o::relocatable::Parser
<arm
>::validFile(fileContent
, opts
.objSubtypeMustMatch
, opts
.subType
) );
6765 case CPU_TYPE_POWERPC
:
6766 return ( mach_o::relocatable::Parser
<ppc
>::validFile(fileContent
) );
6767 case CPU_TYPE_POWERPC64
:
6768 return ( mach_o::relocatable::Parser
<ppc64
>::validFile(fileContent
) );
6774 // used by linker to infer architecture when no -arch is on command line
6776 bool isObjectFile(const uint8_t* fileContent
, cpu_type_t
* result
, cpu_subtype_t
* subResult
)
6778 if ( mach_o::relocatable::Parser
<x86_64
>::validFile(fileContent
) ) {
6779 *result
= CPU_TYPE_X86_64
;
6780 *subResult
= CPU_SUBTYPE_X86_64_ALL
;
6783 if ( mach_o::relocatable::Parser
<x86
>::validFile(fileContent
) ) {
6784 *result
= CPU_TYPE_I386
;
6785 *subResult
= CPU_SUBTYPE_X86_ALL
;
6788 if ( mach_o::relocatable::Parser
<arm
>::validFile(fileContent
, false, 0) ) {
6789 *result
= CPU_TYPE_ARM
;
6790 const macho_header
<Pointer32
<LittleEndian
> >* header
= (const macho_header
<Pointer32
<LittleEndian
> >*)fileContent
;
6791 *subResult
= header
->cpusubtype();
6794 if ( mach_o::relocatable::Parser
<ppc
>::validFile(fileContent
) ) {
6795 *result
= CPU_TYPE_POWERPC
;
6796 const macho_header
<Pointer32
<BigEndian
> >* header
= (const macho_header
<Pointer32
<BigEndian
> >*)fileContent
;
6797 *subResult
= header
->cpusubtype();
6800 if ( mach_o::relocatable::Parser
<ppc64
>::validFile(fileContent
) ) {
6801 *result
= CPU_TYPE_POWERPC64
;
6802 *subResult
= CPU_SUBTYPE_POWERPC_ALL
;
6809 // used by linker is error messages to describe bad .o file
6811 const char* archName(const uint8_t* fileContent
)
6813 if ( mach_o::relocatable::Parser
<x86_64
>::validFile(fileContent
) ) {
6814 return mach_o::relocatable::Parser
<x86_64
>::fileKind(fileContent
);
6816 if ( mach_o::relocatable::Parser
<x86
>::validFile(fileContent
) ) {
6817 return mach_o::relocatable::Parser
<x86
>::fileKind(fileContent
);
6819 if ( mach_o::relocatable::Parser
<arm
>::validFile(fileContent
, false, 0) ) {
6820 return mach_o::relocatable::Parser
<arm
>::fileKind(fileContent
);
6822 if ( mach_o::relocatable::Parser
<ppc
>::validFile(fileContent
) ) {
6823 return mach_o::relocatable::Parser
<ppc
>::fileKind(fileContent
);
6825 if ( mach_o::relocatable::Parser
<ppc64
>::validFile(fileContent
) ) {
6826 return mach_o::relocatable::Parser
<ppc64
>::fileKind(fileContent
);
6832 // Used by archive reader when -ObjC option is specified
6834 bool hasObjC2Categories(const uint8_t* fileContent
)
6836 if ( mach_o::relocatable::Parser
<x86_64
>::validFile(fileContent
) ) {
6837 return mach_o::relocatable::Parser
<x86_64
>::hasObjC2Categories(fileContent
);
6839 else if ( mach_o::relocatable::Parser
<arm
>::validFile(fileContent
, false, 0) ) {
6840 return mach_o::relocatable::Parser
<arm
>::hasObjC2Categories(fileContent
);
6842 else if ( mach_o::relocatable::Parser
<x86
>::validFile(fileContent
, false, 0) ) {
6843 return mach_o::relocatable::Parser
<x86
>::hasObjC2Categories(fileContent
);
6850 } // namespace relocatable
6851 } // namespace mach_o