1 /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-
3 * Copyright (c) 2009-2010 Apple Inc. All rights reserved.
5 * @APPLE_LICENSE_HEADER_START@
7 * This file contains Original Code and/or Modifications of Original Code
8 * as defined in and that are subject to the Apple Public Source License
9 * Version 2.0 (the 'License'). You may not use this file except in
10 * compliance with the License. Please obtain a copy of the License at
11 * http://www.opensource.apple.com/apsl/ and read it before using this
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
19 * Please see the License for the specific language governing rights and
20 * limitations under the License.
22 * @APPLE_LICENSE_HEADER_END@
31 #include <sys/param.h>
35 #include "MachOFileAbstraction.hpp"
37 #include "libunwind/DwarfInstructions.hpp"
38 #include "libunwind/AddressSpace.hpp"
39 #include "libunwind/Registers.hpp"
47 #include "debugline.h"
49 #include "Architectures.hpp"
51 #include "macho_relocatable_file.h"
55 extern void throwf(const char* format
, ...) __attribute__ ((noreturn
,format(printf
, 1, 2)));
56 extern void warning(const char* format
, ...) __attribute__((format(printf
, 1, 2)));
59 namespace relocatable
{
63 template <typename A
> class Parser
;
64 template <typename A
> class Atom
;
65 template <typename A
> class Section
;
66 template <typename A
> class CFISection
;
67 template <typename A
> class CUSection
;
70 class File
: public ld::relocatable::File
73 File(const char* p
, time_t mTime
, const uint8_t* content
, ld::File::Ordinal ord
) :
74 ld::relocatable::File(p
,mTime
,ord
), _fileContent(content
),
75 _sectionsArray(NULL
), _atomsArray(NULL
),
76 _sectionsArrayCount(0), _atomsArrayCount(0),
77 _debugInfoKind(ld::relocatable::File::kDebugInfoNone
),
78 _dwarfTranslationUnitPath(NULL
),
79 _dwarfDebugInfoSect(NULL
), _dwarfDebugAbbrevSect(NULL
),
80 _dwarfDebugLineSect(NULL
), _dwarfDebugStringSect(NULL
),
81 _objConstraint(ld::File::objcConstraintNone
),
83 _canScatterAtoms(false) {}
86 // overrides of ld::File
87 virtual bool forEachAtom(ld::File::AtomHandler
&) const;
88 virtual bool justInTimeforEachAtom(const char* name
, ld::File::AtomHandler
&) const
91 // overrides of ld::relocatable::File
92 virtual ObjcConstraint
objCConstraint() const { return _objConstraint
; }
93 virtual uint32_t cpuSubType() const { return _cpuSubType
; }
94 virtual DebugInfoKind
debugInfo() const { return _debugInfoKind
; }
95 virtual const std::vector
<ld::relocatable::File::Stab
>* stabs() const { return &_stabs
; }
96 virtual bool canScatterAtoms() const { return _canScatterAtoms
; }
97 virtual const char* translationUnitSource() const;
98 virtual LinkerOptionsList
* linkerOptions() const { return &_linkerOptions
; }
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* _dwarfTranslationUnitPath
;
120 const macho_section
<P
>* _dwarfDebugInfoSect
;
121 const macho_section
<P
>* _dwarfDebugAbbrevSect
;
122 const macho_section
<P
>* _dwarfDebugLineSect
;
123 const macho_section
<P
>* _dwarfDebugStringSect
;
124 ld::File::ObjcConstraint _objConstraint
;
125 uint32_t _cpuSubType
;
126 bool _canScatterAtoms
;
127 std::vector
<std::vector
<const char*> > _linkerOptions
;
131 template <typename A
>
132 class Section
: public ld::Section
135 typedef typename
A::P::uint_t pint_t
;
136 typedef typename
A::P P
;
137 typedef typename
A::P::E E
;
139 virtual ~Section() { }
140 class File
<A
>& file() const { return _file
; }
141 const macho_section
<P
>* machoSection() const { return _machOSection
; }
142 uint32_t sectionNum(class Parser
<A
>&) const;
143 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
);
144 virtual ld::Atom::ContentType
contentType() { return ld::Atom::typeUnclassified
; }
145 virtual bool dontDeadStrip() { return (this->_machOSection
->flags() & S_ATTR_NO_DEAD_STRIP
); }
146 virtual Atom
<A
>* findAtomByAddress(pint_t addr
) { return this->findContentAtomByAddress(addr
, this->_beginAtoms
, this->_endAtoms
); }
147 virtual bool addFollowOnFixups() const { return ! _file
.canScatterAtoms(); }
148 virtual uint32_t appendAtoms(class Parser
<A
>& parser
, uint8_t* buffer
,
149 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
150 const struct Parser
<A
>::CFI_CU_InfoArrays
&) = 0;
151 virtual uint32_t computeAtomCount(class Parser
<A
>& parser
,
152 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
153 const struct Parser
<A
>::CFI_CU_InfoArrays
&) = 0;
154 virtual void makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
&);
155 virtual bool addRelocFixup(class Parser
<A
>& parser
, const macho_relocation_info
<P
>*);
156 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const { return 0; }
157 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
158 const ld::IndirectBindingTable
& ind
) const { return false; }
159 virtual bool ignoreLabel(const char* label
) 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 Atom
<A
>* findContentAtomByAddress(pint_t addr
, class Atom
<A
>* start
, class Atom
<A
>* end
);
172 uint32_t x86_64PcRelOffset(uint8_t r_type
);
173 static const char* makeSegmentName(const macho_section
<typename
A::P
>* s
);
174 static bool readable(const macho_section
<typename
A::P
>* s
);
175 static bool writable(const macho_section
<typename
A::P
>* s
);
176 static bool exectuable(const macho_section
<typename
A::P
>* s
);
177 static ld::Section::Type
sectionType(const macho_section
<typename
A::P
>* s
);
180 const macho_section
<P
>* _machOSection
;
181 class Atom
<A
>* _beginAtoms
;
182 class Atom
<A
>* _endAtoms
;
187 template <typename A
>
188 class CFISection
: public Section
<A
>
191 CFISection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
192 : Section
<A
>(f
, s
) { }
195 virtual ld::Atom::ContentType
contentType() { return ld::Atom::typeCFI
; }
196 virtual uint32_t computeAtomCount(class Parser
<A
>& parser
, struct Parser
<A
>::LabelAndCFIBreakIterator
& it
, const struct Parser
<A
>::CFI_CU_InfoArrays
&);
197 virtual uint32_t appendAtoms(class Parser
<A
>& parser
, uint8_t* buffer
, struct Parser
<A
>::LabelAndCFIBreakIterator
& it
, const struct Parser
<A
>::CFI_CU_InfoArrays
&);
198 virtual void makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
&);
199 virtual bool addFollowOnFixups() const { return false; }
203 /// ObjectFileAddressSpace is used as a template parameter to UnwindCursor for parsing
204 /// dwarf CFI information in an object file.
209 typedef typename
A::P::uint_t pint_t
;
210 typedef typename
A::P P
;
211 typedef typename
A::P::E E
;
212 typedef typename
A::P::uint_t sint_t
;
214 OAS(CFISection
<A
>& ehFrameSection
, const uint8_t* ehFrameBuffer
) :
215 _ehFrameSection(ehFrameSection
),
216 _ehFrameContent(ehFrameBuffer
),
217 _ehFrameStartAddr(ehFrameSection
.machoSection()->addr()),
218 _ehFrameEndAddr(ehFrameSection
.machoSection()->addr()+ehFrameSection
.machoSection()->size()) {}
220 uint8_t get8(pint_t addr
) { return *((uint8_t*)mappedAddress(addr
)); }
221 uint16_t get16(pint_t addr
) { return E::get16(*((uint16_t*)mappedAddress(addr
))); }
222 uint32_t get32(pint_t addr
) { return E::get32(*((uint32_t*)mappedAddress(addr
))); }
223 uint64_t get64(pint_t addr
) { return E::get64(*((uint64_t*)mappedAddress(addr
))); }
224 pint_t
getP(pint_t addr
) { return P::getP(*((pint_t
*)mappedAddress(addr
))); }
225 uint64_t getULEB128(pint_t
& addr
, pint_t end
);
226 int64_t getSLEB128(pint_t
& addr
, pint_t end
);
227 pint_t
getEncodedP(pint_t
& addr
, pint_t end
, uint8_t encoding
);
229 const void* mappedAddress(pint_t addr
);
231 CFISection
<A
>& _ehFrameSection
;
232 const uint8_t* _ehFrameContent
;
233 pint_t _ehFrameStartAddr
;
234 pint_t _ehFrameEndAddr
;
238 typedef typename
A::P::uint_t pint_t
;
239 typedef libunwind::CFI_Atom_Info
<OAS
> CFI_Atom_Info
;
241 void cfiParse(class Parser
<A
>& parser
, uint8_t* buffer
, CFI_Atom_Info cfiArray
[], uint32_t& cfiCount
, const pint_t cuStarts
[], uint32_t cuCount
);
242 bool needsRelocating();
244 static bool bigEndian();
246 void addCiePersonalityFixups(class Parser
<A
>& parser
, const CFI_Atom_Info
* cieInfo
);
247 static void warnFunc(void* ref
, uint64_t funcAddr
, const char* msg
);
251 template <typename A
>
252 class CUSection
: public Section
<A
>
255 CUSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
256 : Section
<A
>(f
, s
) { }
258 typedef typename
A::P::uint_t pint_t
;
259 typedef typename
A::P P
;
260 typedef typename
A::P::E E
;
262 virtual uint32_t computeAtomCount(class Parser
<A
>& parser
, struct Parser
<A
>::LabelAndCFIBreakIterator
& it
, const struct Parser
<A
>::CFI_CU_InfoArrays
&) { return 0; }
263 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; }
264 virtual void makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
&);
265 virtual bool addFollowOnFixups() const { return false; }
268 pint_t functionStartAddress
;
269 uint32_t functionSymbolIndex
;
270 uint32_t rangeLength
;
271 uint32_t compactUnwindInfo
;
272 const char* personality
;
279 void parse(class Parser
<A
>& parser
, uint32_t cnt
, Info array
[]);
280 static bool encodingMeansUseDwarf(compact_unwind_encoding_t enc
);
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();
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
) const { return (label
[0] == 'L'); }
397 template <typename A
>
398 class FixedSizeSection
: public ImplicitSizeSection
<A
>
401 FixedSizeSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
402 : ImplicitSizeSection
<A
>(parser
, f
, s
) { }
404 typedef typename
A::P::uint_t pint_t
;
405 typedef typename
A::P P
;
406 typedef typename
A::P::E E
;
408 virtual bool useElementAt(Parser
<A
>& parser
,
409 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
, pint_t addr
)
414 template <typename A
>
415 class Literal4Section
: public FixedSizeSection
<A
>
418 Literal4Section(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
419 : FixedSizeSection
<A
>(parser
, f
, s
) {}
421 typedef typename
A::P::uint_t pint_t
;
422 typedef typename
A::P P
;
424 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
) { return ld::Atom::Alignment(2); }
425 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "4-byte-literal"; }
426 virtual pint_t
elementSizeAtAddress(pint_t addr
) { return 4; }
427 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
) { return ld::Atom::combineByNameAndContent
; }
428 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
429 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
430 const ld::IndirectBindingTable
& ind
) const;
433 template <typename A
>
434 class Literal8Section
: public FixedSizeSection
<A
>
437 Literal8Section(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
438 : FixedSizeSection
<A
>(parser
, f
, s
) {}
440 typedef typename
A::P::uint_t pint_t
;
441 typedef typename
A::P P
;
443 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
) { return ld::Atom::Alignment(3); }
444 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "8-byte-literal"; }
445 virtual pint_t
elementSizeAtAddress(pint_t addr
) { return 8; }
446 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
) { return ld::Atom::combineByNameAndContent
; }
447 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
448 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
449 const ld::IndirectBindingTable
& ind
) const;
452 template <typename A
>
453 class Literal16Section
: public FixedSizeSection
<A
>
456 Literal16Section(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
457 : FixedSizeSection
<A
>(parser
, f
, s
) {}
459 typedef typename
A::P::uint_t pint_t
;
460 typedef typename
A::P P
;
462 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
) { return ld::Atom::Alignment(4); }
463 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "16-byte-literal"; }
464 virtual pint_t
elementSizeAtAddress(pint_t addr
) { return 16; }
465 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
) { return ld::Atom::combineByNameAndContent
; }
466 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
467 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
468 const ld::IndirectBindingTable
& ind
) const;
472 template <typename A
>
473 class NonLazyPointerSection
: public FixedSizeSection
<A
>
476 NonLazyPointerSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
477 : FixedSizeSection
<A
>(parser
, f
, s
) {}
479 typedef typename
A::P::uint_t pint_t
;
480 typedef typename
A::P P
;
482 virtual void makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
&);
483 virtual ld::Atom::ContentType
contentType() { return ld::Atom::typeNonLazyPointer
; }
484 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
) { return ld::Atom::Alignment(log2(sizeof(pint_t
))); }
485 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "non_lazy_ptr"; }
486 virtual pint_t
elementSizeAtAddress(pint_t addr
) { return sizeof(pint_t
); }
487 virtual ld::Atom::Scope
scopeAtAddress(Parser
<A
>& parser
, pint_t addr
);
488 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
);
489 virtual bool ignoreLabel(const char* label
) const { return true; }
490 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
491 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
492 const ld::IndirectBindingTable
& ind
) const;
495 static const char* targetName(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
);
496 static ld::Fixup::Kind
fixupKind();
500 template <typename A
>
501 class CFStringSection
: public FixedSizeSection
<A
>
504 CFStringSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
505 : FixedSizeSection
<A
>(parser
, f
, s
) {}
507 typedef typename
A::P::uint_t pint_t
;
509 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
) { return ld::Atom::Alignment(log2(sizeof(pint_t
))); }
510 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "CFString"; }
511 virtual pint_t
elementSizeAtAddress(pint_t addr
) { return 4*sizeof(pint_t
); }
512 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
) { return ld::Atom::combineByNameAndReferences
; }
513 virtual bool ignoreLabel(const char* label
) const { return true; }
514 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
515 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
516 const ld::IndirectBindingTable
& ind
) const;
518 enum ContentType
{ contentUTF8
, contentUTF16
, contentUnknown
};
519 static const uint8_t* targetContent(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
,
520 ContentType
* ct
, unsigned int* count
);
524 template <typename A
>
525 class ObjC1ClassSection
: public FixedSizeSection
<A
>
528 ObjC1ClassSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
529 : FixedSizeSection
<A
>(parser
, f
, s
) {}
531 typedef typename
A::P::uint_t pint_t
;
532 typedef typename
A::P P
;
533 typedef typename
A::P::E E
;
535 virtual ld::Atom::Scope
scopeAtAddress(Parser
<A
>& , pint_t
) { return ld::Atom::scopeGlobal
; }
536 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
) { return ld::Atom::Alignment(2); }
537 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
);
538 virtual ld::Atom::SymbolTableInclusion
symbolTableInclusion() { return ld::Atom::symbolTableIn
; }
539 virtual pint_t
elementSizeAtAddress(pint_t addr
);
540 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
) { return ld::Atom::combineNever
; }
541 virtual bool ignoreLabel(const char* label
) const { return true; }
542 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
544 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
545 const ld::IndirectBindingTable
& ind
) const { return false; }
546 virtual bool addRelocFixup(class Parser
<A
>& parser
, const macho_relocation_info
<P
>*);
550 template <typename A
>
551 class ObjC2ClassRefsSection
: public FixedSizeSection
<A
>
554 ObjC2ClassRefsSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
555 : FixedSizeSection
<A
>(parser
, f
, s
) {}
557 typedef typename
A::P::uint_t pint_t
;
559 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
) { return ld::Atom::Alignment(log2(sizeof(pint_t
))); }
560 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "objc-class-ref"; }
561 virtual pint_t
elementSizeAtAddress(pint_t addr
) { return sizeof(pint_t
); }
562 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
) { return ld::Atom::combineByNameAndReferences
; }
563 virtual bool ignoreLabel(const char* label
) const { return true; }
564 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
565 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
566 const ld::IndirectBindingTable
& ind
) const;
568 const char* targetClassName(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
572 template <typename A
>
573 class ObjC2CategoryListSection
: public FixedSizeSection
<A
>
576 ObjC2CategoryListSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
577 : FixedSizeSection
<A
>(parser
, f
, s
) {}
579 typedef typename
A::P::uint_t pint_t
;
581 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
) { return ld::Atom::Alignment(log2(sizeof(pint_t
))); }
582 virtual ld::Atom::Scope
scopeAtAddress(Parser
<A
>& parser
, pint_t addr
) { return ld::Atom::scopeTranslationUnit
; }
583 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "objc-cat-list"; }
584 virtual pint_t
elementSizeAtAddress(pint_t addr
) { return sizeof(pint_t
); }
585 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
) { return ld::Atom::combineNever
; }
586 virtual bool ignoreLabel(const char* label
) const { return true; }
588 const char* targetClassName(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
592 template <typename A
>
593 class PointerToCStringSection
: public FixedSizeSection
<A
>
596 PointerToCStringSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
597 : FixedSizeSection
<A
>(parser
, f
, s
) {}
599 typedef typename
A::P::uint_t pint_t
;
601 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
) { return ld::Atom::Alignment(log2(sizeof(pint_t
))); }
602 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "pointer-to-literal-cstring"; }
603 virtual pint_t
elementSizeAtAddress(pint_t addr
) { return sizeof(pint_t
); }
604 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
) { return ld::Atom::combineByNameAndReferences
; }
605 virtual bool ignoreLabel(const char* label
) const { return true; }
606 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
607 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
608 const ld::IndirectBindingTable
& ind
) const;
609 virtual const char* targetCString(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
613 template <typename A
>
614 class Objc1ClassReferences
: public PointerToCStringSection
<A
>
617 Objc1ClassReferences(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
618 : PointerToCStringSection
<A
>(parser
, f
, s
) {}
620 typedef typename
A::P::uint_t pint_t
;
621 typedef typename
A::P P
;
623 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "pointer-to-literal-objc-class-name"; }
624 virtual bool addRelocFixup(class Parser
<A
>& parser
, const macho_relocation_info
<P
>*);
625 virtual const char* targetCString(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
629 template <typename A
>
630 class CStringSection
: public ImplicitSizeSection
<A
>
633 CStringSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
634 : ImplicitSizeSection
<A
>(parser
, f
, s
) {}
636 typedef typename
A::P::uint_t pint_t
;
637 typedef typename
A::P P
;
639 virtual ld::Atom::ContentType
contentType() { return ld::Atom::typeCString
; }
640 virtual Atom
<A
>* findAtomByAddress(pint_t addr
);
641 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "cstring"; }
642 virtual pint_t
elementSizeAtAddress(pint_t addr
);
643 virtual bool ignoreLabel(const char* label
) const;
644 virtual bool useElementAt(Parser
<A
>& parser
,
645 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
, pint_t addr
);
646 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
) { return ld::Atom::combineByNameAndContent
; }
647 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
648 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
649 const ld::IndirectBindingTable
& ind
) const;
654 template <typename A
>
655 class UTF16StringSection
: public SymboledSection
<A
>
658 UTF16StringSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
659 : SymboledSection
<A
>(parser
, f
, s
) {}
661 typedef typename
A::P::uint_t pint_t
;
662 typedef typename
A::P P
;
664 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
) { return ld::Atom::combineByNameAndContent
; }
665 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
666 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
667 const ld::IndirectBindingTable
& ind
) const;
672 // Atoms in mach-o files
674 template <typename A
>
675 class Atom
: public ld::Atom
678 // overrides of ld::Atom
679 virtual ld::File
* file() const { return §().file(); }
680 virtual const char* translationUnitSource() const
681 { return sect().file().translationUnitSource(); }
682 virtual const char* name() const { return _name
; }
683 virtual uint64_t size() const { return _size
; }
684 virtual uint64_t objectAddress() const { return _objAddress
; }
685 virtual void copyRawContent(uint8_t buffer
[]) const;
686 virtual const uint8_t* rawContentPointer() const { return contentPointer(); }
687 virtual unsigned long contentHash(const ld::IndirectBindingTable
& ind
) const
688 { if ( _hash
== 0 ) _hash
= sect().contentHash(this, ind
); return _hash
; }
689 virtual bool canCoalesceWith(const ld::Atom
& rhs
, const ld::IndirectBindingTable
& ind
) const
690 { return sect().canCoalesceWith(this, rhs
, ind
); }
691 virtual ld::Fixup::iterator
fixupsBegin() const { return &machofile()._fixups
[_fixupsStartIndex
]; }
692 virtual ld::Fixup::iterator
fixupsEnd() const { return &machofile()._fixups
[_fixupsStartIndex
+_fixupsCount
]; }
693 virtual ld::Atom::UnwindInfo::iterator
beginUnwind() const { return &machofile()._unwindInfos
[_unwindInfoStartIndex
]; }
694 virtual ld::Atom::UnwindInfo::iterator
endUnwind() const { return &machofile()._unwindInfos
[_unwindInfoStartIndex
+_unwindInfoCount
]; }
695 virtual ld::Atom::LineInfo::iterator
beginLineInfo() const{ return &machofile()._lineInfos
[_lineInfoStartIndex
]; }
696 virtual ld::Atom::LineInfo::iterator
endLineInfo() const { return &machofile()._lineInfos
[_lineInfoStartIndex
+_lineInfoCount
]; }
700 enum { kFixupStartIndexBits
= 32,
701 kLineInfoStartIndexBits
= 32,
702 kUnwindInfoStartIndexBits
= 24,
703 kFixupCountBits
= 24,
704 kLineInfoCountBits
= 12,
705 kUnwindInfoCountBits
= 4
706 }; // must sum to 128
709 // methods for all atoms from mach-o object file
710 Section
<A
>& sect() const { return (Section
<A
>&)section(); }
711 File
<A
>& machofile() const { return ((Section
<A
>*)(this->_section
))->file(); }
712 void setFixupsRange(uint32_t s
, uint32_t c
);
713 void setUnwindInfoRange(uint32_t s
, uint32_t c
);
714 void extendUnwindInfoRange();
715 void setLineInfoRange(uint32_t s
, uint32_t c
);
716 bool roomForMoreLineInfoCount() { return (_lineInfoCount
< ((1<<kLineInfoCountBits
)-1)); }
717 void incrementLineInfoCount() { assert(roomForMoreLineInfoCount()); ++_lineInfoCount
; }
718 void incrementFixupCount() { if (_fixupsCount
== ((1 << kFixupCountBits
)-1))
719 throwf("too may fixups in %s", name()); ++_fixupsCount
; }
720 const uint8_t* contentPointer() const;
721 uint32_t fixupCount() const { return _fixupsCount
; }
722 void verifyAlignment() const;
724 typedef typename
A::P P
;
725 typedef typename
A::P::E E
;
726 typedef typename
A::P::uint_t pint_t
;
727 // constuct via all attributes
728 Atom(Section
<A
>& sct
, const char* nm
, pint_t addr
, uint64_t sz
,
729 ld::Atom::Definition d
, ld::Atom::Combine c
, ld::Atom::Scope s
,
730 ld::Atom::ContentType ct
, ld::Atom::SymbolTableInclusion i
,
731 bool dds
, bool thumb
, bool al
, ld::Atom::Alignment a
)
732 : ld::Atom((ld::Section
&)sct
, d
, c
, s
, ct
, i
, dds
, thumb
, al
, a
),
733 _size(sz
), _objAddress(addr
), _name(nm
), _hash(0),
734 _fixupsStartIndex(0), _lineInfoStartIndex(0),
735 _unwindInfoStartIndex(0), _fixupsCount(0),
736 _lineInfoCount(0), _unwindInfoCount(0) { }
737 // construct via symbol table entry
738 Atom(Section
<A
>& sct
, Parser
<A
>& parser
, const macho_nlist
<P
>& sym
,
739 uint64_t sz
, bool alias
=false)
740 : ld::Atom((ld::Section
&)sct
, parser
.definitionFromSymbol(sym
),
741 parser
.combineFromSymbol(sym
), parser
.scopeFromSymbol(sym
),
742 parser
.resolverFromSymbol(sym
) ? ld::Atom::typeResolver
: sct
.contentType(),
743 parser
.inclusionFromSymbol(sym
),
744 parser
.dontDeadStripFromSymbol(sym
) || sct
.dontDeadStrip(),
745 parser
.isThumbFromSymbol(sym
), alias
,
746 sct
.alignmentForAddress(sym
.n_value())),
747 _size(sz
), _objAddress(sym
.n_value()),
748 _name(parser
.nameFromSymbol(sym
)), _hash(0),
749 _fixupsStartIndex(0), _lineInfoStartIndex(0),
750 _unwindInfoStartIndex(0), _fixupsCount(0),
751 _lineInfoCount(0), _unwindInfoCount(0) {
752 // <rdar://problem/6783167> support auto-hidden weak symbols
753 if ( _scope
== ld::Atom::scopeGlobal
&&
754 (sym
.n_desc() & (N_WEAK_DEF
|N_WEAK_REF
)) == (N_WEAK_DEF
|N_WEAK_REF
) )
756 this->verifyAlignment();
760 friend class Parser
<A
>;
761 friend class Section
<A
>;
762 friend class CStringSection
<A
>;
763 friend class AbsoluteSymbolSection
<A
>;
768 mutable unsigned long _hash
;
770 uint64_t _fixupsStartIndex
: kFixupStartIndexBits
,
771 _lineInfoStartIndex
: kLineInfoStartIndexBits
,
772 _unwindInfoStartIndex
: kUnwindInfoStartIndexBits
,
773 _fixupsCount
: kFixupCountBits
,
774 _lineInfoCount
: kLineInfoCountBits
,
775 _unwindInfoCount
: kUnwindInfoCountBits
;
781 template <typename A
>
782 void Atom
<A
>::setFixupsRange(uint32_t startIndex
, uint32_t count
)
784 if ( count
>= (1 << kFixupCountBits
) )
785 throwf("too many fixups in function %s", this->name());
786 if ( startIndex
>= (1 << kFixupStartIndexBits
) )
787 throwf("too many fixups in file");
788 assert(((startIndex
+count
) <= sect().file()._fixups
.size()) && "fixup index out of range");
789 _fixupsStartIndex
= startIndex
;
790 _fixupsCount
= count
;
793 template <typename A
>
794 void Atom
<A
>::setUnwindInfoRange(uint32_t startIndex
, uint32_t count
)
796 if ( count
>= (1 << kUnwindInfoCountBits
) )
797 throwf("too many compact unwind infos in function %s", this->name());
798 if ( startIndex
>= (1 << kUnwindInfoStartIndexBits
) )
799 throwf("too many compact unwind infos (%d) in file", startIndex
);
800 assert((startIndex
+count
) <= sect().file()._unwindInfos
.size() && "unwindinfo index out of range");
801 _unwindInfoStartIndex
= startIndex
;
802 _unwindInfoCount
= count
;
805 template <typename A
>
806 void Atom
<A
>::extendUnwindInfoRange()
808 if ( _unwindInfoCount
+1 >= (1 << kUnwindInfoCountBits
) )
809 throwf("too many compact unwind infos in function %s", this->name());
810 _unwindInfoCount
+= 1;
813 template <typename A
>
814 void Atom
<A
>::setLineInfoRange(uint32_t startIndex
, uint32_t count
)
816 assert((count
< (1 << kLineInfoCountBits
)) && "too many line infos");
817 assert((startIndex
+count
) < sect().file()._lineInfos
.size() && "line info index out of range");
818 _lineInfoStartIndex
= startIndex
;
819 _lineInfoCount
= count
;
822 template <typename A
>
823 const uint8_t* Atom
<A
>::contentPointer() const
825 const macho_section
<P
>* sct
= this->sect().machoSection();
826 if ( this->_objAddress
> sct
->addr() + sct
->size() )
827 throwf("malformed .o file, symbol has address 0x%0llX which is outside range of its section", (uint64_t)this->_objAddress
);
828 uint32_t fileOffset
= sct
->offset() - sct
->addr() + this->_objAddress
;
829 return this->sect().file().fileContent()+fileOffset
;
833 template <typename A
>
834 void Atom
<A
>::copyRawContent(uint8_t buffer
[]) const
837 if ( this->contentType() == ld::Atom::typeZeroFill
) {
838 bzero(buffer
, _size
);
840 else if ( _size
!= 0 ) {
841 memcpy(buffer
, this->contentPointer(), _size
);
846 void Atom
<arm
>::verifyAlignment() const
848 if ( (this->section().type() == ld::Section::typeCode
) && ! isThumb() ) {
849 if ( ((_objAddress
% 4) != 0) || (this->alignment().powerOf2
< 2) )
850 warning("ARM function not 4-byte aligned: %s from %s", this->name(), this->file()->path());
854 template <typename A
>
855 void Atom
<A
>::verifyAlignment() const
860 template <typename A
>
864 static bool validFile(const uint8_t* fileContent
, bool subtypeMustMatch
=false,
865 cpu_subtype_t subtype
=0);
866 static const char* fileKind(const uint8_t* fileContent
);
867 static bool hasObjC2Categories(const uint8_t* fileContent
);
868 static bool hasObjC1Categories(const uint8_t* fileContent
);
869 static ld::relocatable::File
* parse(const uint8_t* fileContent
, uint64_t fileLength
,
870 const char* path
, time_t modTime
, ld::File::Ordinal ordinal
,
871 const ParserOptions
& opts
) {
872 Parser
p(fileContent
, fileLength
, path
, modTime
,
873 ordinal
, opts
.warnUnwindConversionProblems
,
874 opts
.keepDwarfUnwind
, opts
.forceDwarfConversion
);
875 return p
.parse(opts
);
878 typedef typename
A::P P
;
879 typedef typename
A::P::E E
;
880 typedef typename
A::P::uint_t pint_t
;
882 struct SourceLocation
{
884 SourceLocation(Atom
<A
>* a
, uint32_t o
) : atom(a
), offsetInAtom(o
) {}
886 uint32_t offsetInAtom
;
891 const char* name
; // only used if targetAtom is NULL
893 bool weakImport
; // only used if targetAtom is NULL
897 FixupInAtom(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, Atom
<A
>* target
) :
898 fixup(src
.offsetInAtom
, c
, k
, target
), atom(src
.atom
) { src
.atom
->incrementFixupCount(); }
900 FixupInAtom(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, ld::Fixup::TargetBinding b
, Atom
<A
>* target
) :
901 fixup(src
.offsetInAtom
, c
, k
, b
, target
), atom(src
.atom
) { src
.atom
->incrementFixupCount(); }
903 FixupInAtom(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, bool wi
, const char* name
) :
904 fixup(src
.offsetInAtom
, c
, k
, wi
, name
), atom(src
.atom
) { src
.atom
->incrementFixupCount(); }
906 FixupInAtom(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, ld::Fixup::TargetBinding b
, const char* name
) :
907 fixup(src
.offsetInAtom
, c
, k
, b
, name
), atom(src
.atom
) { src
.atom
->incrementFixupCount(); }
909 FixupInAtom(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, uint64_t addend
) :
910 fixup(src
.offsetInAtom
, c
, k
, addend
), atom(src
.atom
) { src
.atom
->incrementFixupCount(); }
912 FixupInAtom(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
) :
913 fixup(src
.offsetInAtom
, c
, k
, (uint64_t)0), atom(src
.atom
) { src
.atom
->incrementFixupCount(); }
919 void addFixup(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, Atom
<A
>* target
) {
920 _allFixups
.push_back(FixupInAtom(src
, c
, k
, target
));
923 void addFixup(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, ld::Fixup::TargetBinding b
, Atom
<A
>* target
) {
924 _allFixups
.push_back(FixupInAtom(src
, c
, k
, b
, target
));
927 void addFixup(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, bool wi
, const char* name
) {
928 _allFixups
.push_back(FixupInAtom(src
, c
, k
, wi
, name
));
931 void addFixup(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, ld::Fixup::TargetBinding b
, const char* name
) {
932 _allFixups
.push_back(FixupInAtom(src
, c
, k
, b
, name
));
935 void addFixup(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, uint64_t addend
) {
936 _allFixups
.push_back(FixupInAtom(src
, c
, k
, addend
));
939 void addFixup(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
) {
940 _allFixups
.push_back(FixupInAtom(src
, c
, k
));
943 const char* path() { return _path
; }
944 uint32_t symbolCount() { return _symbolCount
; }
945 uint32_t indirectSymbol(uint32_t indirectIndex
);
946 const macho_nlist
<P
>& symbolFromIndex(uint32_t index
);
947 const char* nameFromSymbol(const macho_nlist
<P
>& sym
);
948 ld::Atom::Scope
scopeFromSymbol(const macho_nlist
<P
>& sym
);
949 static ld::Atom::Definition
definitionFromSymbol(const macho_nlist
<P
>& sym
);
950 static ld::Atom::Combine
combineFromSymbol(const macho_nlist
<P
>& sym
);
951 ld::Atom::SymbolTableInclusion
inclusionFromSymbol(const macho_nlist
<P
>& sym
);
952 static bool dontDeadStripFromSymbol(const macho_nlist
<P
>& sym
);
953 static bool isThumbFromSymbol(const macho_nlist
<P
>& sym
);
954 static bool weakImportFromSymbol(const macho_nlist
<P
>& sym
);
955 static bool resolverFromSymbol(const macho_nlist
<P
>& sym
);
956 uint32_t symbolIndexFromIndirectSectionAddress(pint_t
,const macho_section
<P
>*);
957 const macho_section
<P
>* firstMachOSection() { return _sectionsStart
; }
958 const macho_section
<P
>* machOSectionFromSectionIndex(uint32_t index
);
959 uint32_t machOSectionCount() { return _machOSectionsCount
; }
960 uint32_t undefinedStartIndex() { return _undefinedStartIndex
; }
961 uint32_t undefinedEndIndex() { return _undefinedEndIndex
; }
962 void addFixup(FixupInAtom f
) { _allFixups
.push_back(f
); }
963 Section
<A
>* sectionForNum(unsigned int sectNum
);
964 Section
<A
>* sectionForAddress(pint_t addr
);
965 Atom
<A
>* findAtomByAddress(pint_t addr
);
966 Atom
<A
>* findAtomByAddressOrNullIfStub(pint_t addr
);
967 Atom
<A
>* findAtomByAddressOrLocalTargetOfStub(pint_t addr
, uint32_t* offsetInAtom
);
968 Atom
<A
>* findAtomByName(const char* name
); // slow!
969 void findTargetFromAddress(pint_t addr
, TargetDesc
& target
);
970 void findTargetFromAddress(pint_t baseAddr
, pint_t addr
, TargetDesc
& target
);
971 void findTargetFromAddressAndSectionNum(pint_t addr
, unsigned int sectNum
,
973 uint32_t tentativeDefinitionCount() { return _tentativeDefinitionCount
; }
974 uint32_t absoluteSymbolCount() { return _absoluteSymbolCount
; }
976 bool hasStubsSection() { return (_stubsSectionNum
!= 0); }
977 unsigned int stubsSectionNum() { return _stubsSectionNum
; }
978 void addDtraceExtraInfos(const SourceLocation
& src
, const char* provider
);
979 const char* scanSymbolTableForAddress(uint64_t addr
);
980 bool warnUnwindConversionProblems() { return _warnUnwindConversionProblems
; }
981 bool hasDataInCodeLabels() { return _hasDataInCodeLabels
; }
982 bool keepDwarfUnwind() { return _keepDwarfUnwind
; }
983 bool forceDwarfConversion() { return _forceDwarfConversion
; }
985 macho_data_in_code_entry
<P
>* dataInCodeStart() { return _dataInCodeStart
; }
986 macho_data_in_code_entry
<P
>* dataInCodeEnd() { return _dataInCodeEnd
; }
988 void addFixups(const SourceLocation
& src
, ld::Fixup::Kind kind
, const TargetDesc
& target
);
989 void addFixups(const SourceLocation
& src
, ld::Fixup::Kind kind
, const TargetDesc
& target
, const TargetDesc
& picBase
);
993 struct LabelAndCFIBreakIterator
{
994 typedef typename CFISection
<A
>::CFI_Atom_Info CFI_Atom_Info
;
995 LabelAndCFIBreakIterator(const uint32_t* ssa
, uint32_t ssc
, const pint_t
* cfisa
,
996 uint32_t cfisc
, bool ols
)
997 : sortedSymbolIndexes(ssa
), sortedSymbolCount(ssc
), cfiStartsArray(cfisa
),
998 cfiStartsCount(cfisc
), fileHasOverlappingSymbols(ols
),
999 newSection(false), cfiIndex(0), symIndex(0) {}
1000 bool next(Parser
<A
>& parser
, const Section
<A
>& sect
, uint32_t sectNum
, pint_t startAddr
, pint_t endAddr
,
1001 pint_t
* addr
, pint_t
* size
, const macho_nlist
<P
>** sym
);
1002 pint_t
peek(Parser
<A
>& parser
, pint_t startAddr
, pint_t endAddr
);
1003 void beginSection() { newSection
= true; symIndex
= 0; }
1005 const uint32_t* const sortedSymbolIndexes
;
1006 const uint32_t sortedSymbolCount
;
1007 const pint_t
* cfiStartsArray
;
1008 const uint32_t cfiStartsCount
;
1009 const bool fileHasOverlappingSymbols
;
1015 struct CFI_CU_InfoArrays
{
1016 typedef typename CFISection
<A
>::CFI_Atom_Info CFI_Atom_Info
;
1017 typedef typename CUSection
<A
>::Info CU_Info
;
1018 CFI_CU_InfoArrays(const CFI_Atom_Info
* cfiAr
, uint32_t cfiC
, CU_Info
* cuAr
, uint32_t cuC
)
1019 : cfiArray(cfiAr
), cuArray(cuAr
), cfiCount(cfiC
), cuCount(cuC
) {}
1020 const CFI_Atom_Info
* const cfiArray
;
1021 CU_Info
* const cuArray
;
1022 const uint32_t cfiCount
;
1023 const uint32_t cuCount
;
1029 friend class Section
<A
>;
1031 enum SectionType
{ sectionTypeIgnore
, sectionTypeLiteral4
, sectionTypeLiteral8
, sectionTypeLiteral16
,
1032 sectionTypeNonLazy
, sectionTypeCFI
, sectionTypeCString
, sectionTypeCStringPointer
,
1033 sectionTypeUTF16Strings
, sectionTypeCFString
, sectionTypeObjC2ClassRefs
, typeObjC2CategoryList
,
1034 sectionTypeObjC1Classes
, sectionTypeSymboled
, sectionTypeObjC1ClassRefs
,
1035 sectionTypeTentativeDefinitions
, sectionTypeAbsoluteSymbols
, sectionTypeTLVDefs
,
1036 sectionTypeCompactUnwind
};
1038 template <typename P
>
1039 struct MachOSectionAndSectionClass
1041 const macho_section
<P
>* sect
;
1044 static int sorter(const void* l
, const void* r
) {
1045 const MachOSectionAndSectionClass
<P
>* left
= (MachOSectionAndSectionClass
<P
>*)l
;
1046 const MachOSectionAndSectionClass
<P
>* right
= (MachOSectionAndSectionClass
<P
>*)r
;
1047 int64_t diff
= left
->sect
->addr() - right
->sect
->addr();
1057 struct ParserAndSectionsArray
{ Parser
* parser
; const uint32_t* sortedSectionsArray
; };
1060 Parser(const uint8_t* fileContent
, uint64_t fileLength
,
1061 const char* path
, time_t modTime
, ld::File::Ordinal ordinal
,
1062 bool warnUnwindConversionProblems
, bool keepDwarfUnwind
, bool forceDwarfConversion
);
1063 ld::relocatable::File
* parse(const ParserOptions
& opts
);
1064 uint8_t loadCommandSizeMask();
1065 bool parseLoadCommands();
1066 void makeSections();
1067 void prescanSymbolTable();
1068 void makeSortedSymbolsArray(uint32_t symArray
[], const uint32_t sectionArray
[]);
1069 void makeSortedSectionsArray(uint32_t array
[]);
1070 static int pointerSorter(const void* l
, const void* r
);
1071 static int symbolIndexSorter(void* extra
, const void* l
, const void* r
);
1072 static int sectionIndexSorter(void* extra
, const void* l
, const void* r
);
1074 void parseDebugInfo();
1076 static bool isConstFunStabs(const char *stabStr
);
1077 bool read_comp_unit(const char ** name
, const char ** comp_dir
,
1078 uint64_t *stmt_list
);
1079 const char* getDwarfString(uint64_t form
, const uint8_t* p
);
1080 bool skip_form(const uint8_t ** offset
, const uint8_t * end
,
1081 uint64_t form
, uint8_t addr_size
, bool dwarf64
);
1084 // filled in by constructor
1085 const uint8_t* _fileContent
;
1086 uint32_t _fileLength
;
1089 ld::File::Ordinal _ordinal
;
1091 // filled in by parseLoadCommands()
1093 const macho_nlist
<P
>* _symbols
;
1094 uint32_t _symbolCount
;
1095 const char* _strings
;
1096 uint32_t _stringsSize
;
1097 const uint32_t* _indirectTable
;
1098 uint32_t _indirectTableCount
;
1099 uint32_t _undefinedStartIndex
;
1100 uint32_t _undefinedEndIndex
;
1101 const macho_section
<P
>* _sectionsStart
;
1102 uint32_t _machOSectionsCount
;
1104 macho_data_in_code_entry
<P
>* _dataInCodeStart
;
1105 macho_data_in_code_entry
<P
>* _dataInCodeEnd
;
1107 // filled in by parse()
1108 CFISection
<A
>* _EHFrameSection
;
1109 CUSection
<A
>* _compactUnwindSection
;
1110 AbsoluteSymbolSection
<A
>* _absoluteSection
;
1111 uint32_t _tentativeDefinitionCount
;
1112 uint32_t _absoluteSymbolCount
;
1113 uint32_t _symbolsInSections
;
1114 bool _hasLongBranchStubs
;
1115 bool _AppleObjc
; // FSF has objc that uses different data layout
1116 bool _overlappingSymbols
;
1117 bool _warnUnwindConversionProblems
;
1118 bool _hasDataInCodeLabels
;
1119 bool _keepDwarfUnwind
;
1120 bool _forceDwarfConversion
;
1121 unsigned int _stubsSectionNum
;
1122 const macho_section
<P
>* _stubsMachOSection
;
1123 std::vector
<const char*> _dtraceProviderInfo
;
1124 std::vector
<FixupInAtom
> _allFixups
;
1129 template <typename A
>
1130 Parser
<A
>::Parser(const uint8_t* fileContent
, uint64_t fileLength
, const char* path
, time_t modTime
,
1131 ld::File::Ordinal ordinal
, bool convertDUI
, bool keepDwarfUnwind
, bool forceDwarfConversion
)
1132 : _fileContent(fileContent
), _fileLength(fileLength
), _path(path
), _modTime(modTime
),
1133 _ordinal(ordinal
), _file(NULL
),
1134 _symbols(NULL
), _symbolCount(0), _strings(NULL
), _stringsSize(0),
1135 _indirectTable(NULL
), _indirectTableCount(0),
1136 _undefinedStartIndex(0), _undefinedEndIndex(0),
1137 _sectionsStart(NULL
), _machOSectionsCount(0), _hasUUID(false),
1138 _dataInCodeStart(NULL
), _dataInCodeEnd(NULL
),
1139 _EHFrameSection(NULL
), _compactUnwindSection(NULL
), _absoluteSection(NULL
),
1140 _tentativeDefinitionCount(0), _absoluteSymbolCount(0),
1141 _symbolsInSections(0), _hasLongBranchStubs(false), _AppleObjc(false),
1142 _overlappingSymbols(false), _warnUnwindConversionProblems(convertDUI
), _hasDataInCodeLabels(false),
1143 _keepDwarfUnwind(keepDwarfUnwind
), _forceDwarfConversion(forceDwarfConversion
),
1144 _stubsSectionNum(0), _stubsMachOSection(NULL
)
1150 bool Parser
<x86
>::validFile(const uint8_t* fileContent
, bool, cpu_subtype_t
)
1152 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1153 if ( header
->magic() != MH_MAGIC
)
1155 if ( header
->cputype() != CPU_TYPE_I386
)
1157 if ( header
->filetype() != MH_OBJECT
)
1163 bool Parser
<x86_64
>::validFile(const uint8_t* fileContent
, bool, cpu_subtype_t
)
1165 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1166 if ( header
->magic() != MH_MAGIC_64
)
1168 if ( header
->cputype() != CPU_TYPE_X86_64
)
1170 if ( header
->filetype() != MH_OBJECT
)
1176 bool Parser
<arm
>::validFile(const uint8_t* fileContent
, bool subtypeMustMatch
, cpu_subtype_t subtype
)
1178 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1179 if ( header
->magic() != MH_MAGIC
)
1181 if ( header
->cputype() != CPU_TYPE_ARM
)
1183 if ( header
->filetype() != MH_OBJECT
)
1185 if ( subtypeMustMatch
) {
1186 if ( (cpu_subtype_t
)header
->cpusubtype() == subtype
)
1188 // hack until libcc_kext.a is made fat
1189 if ( header
->cpusubtype() == CPU_SUBTYPE_ARM_ALL
)
1198 bool Parser
<arm64
>::validFile(const uint8_t* fileContent
, bool subtypeMustMatch
, cpu_subtype_t subtype
)
1200 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1201 if ( header
->magic() != MH_MAGIC_64
)
1203 if ( header
->cputype() != CPU_TYPE_ARM64
)
1205 if ( header
->filetype() != MH_OBJECT
)
1212 const char* Parser
<x86
>::fileKind(const uint8_t* fileContent
)
1214 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1215 if ( header
->magic() != MH_MAGIC
)
1217 if ( header
->cputype() != CPU_TYPE_I386
)
1223 const char* Parser
<x86_64
>::fileKind(const uint8_t* fileContent
)
1225 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1226 if ( header
->magic() != MH_MAGIC
)
1228 if ( header
->cputype() != CPU_TYPE_X86_64
)
1234 const char* Parser
<arm
>::fileKind(const uint8_t* fileContent
)
1236 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1237 if ( header
->magic() != MH_MAGIC
)
1239 if ( header
->cputype() != CPU_TYPE_ARM
)
1241 for (const ArchInfo
* t
=archInfoArray
; t
->archName
!= NULL
; ++t
) {
1242 if ( (t
->cpuType
== CPU_TYPE_ARM
) && ((cpu_subtype_t
)header
->cpusubtype() == t
->cpuSubType
) ) {
1249 #if SUPPORT_ARCH_arm64
1251 const char* Parser
<arm64
>::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_ARM64
)
1262 template <typename A
>
1263 bool Parser
<A
>::hasObjC2Categories(const uint8_t* fileContent
)
1265 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1266 const uint32_t cmd_count
= header
->ncmds();
1267 const macho_load_command
<P
>* const cmds
= (macho_load_command
<P
>*)((char*)header
+ sizeof(macho_header
<P
>));
1268 const macho_load_command
<P
>* const cmdsEnd
= (macho_load_command
<P
>*)((char*)header
+ sizeof(macho_header
<P
>) + header
->sizeofcmds());
1269 const macho_load_command
<P
>* cmd
= cmds
;
1270 for (uint32_t i
= 0; i
< cmd_count
; ++i
) {
1271 if ( cmd
->cmd() == macho_segment_command
<P
>::CMD
) {
1272 const macho_segment_command
<P
>* segment
= (macho_segment_command
<P
>*)cmd
;
1273 const macho_section
<P
>* sectionsStart
= (macho_section
<P
>*)((char*)segment
+ sizeof(macho_segment_command
<P
>));
1274 for (uint32_t si
=0; si
< segment
->nsects(); ++si
) {
1275 const macho_section
<P
>* sect
= §ionsStart
[si
];
1276 if ( (sect
->size() > 0)
1277 && (strcmp(sect
->sectname(), "__objc_catlist") == 0)
1278 && (strcmp(sect
->segname(), "__DATA") == 0) ) {
1283 cmd
= (const macho_load_command
<P
>*)(((char*)cmd
)+cmd
->cmdsize());
1284 if ( cmd
> cmdsEnd
)
1285 throwf("malformed mach-o file, load command #%d is outside size of load commands", i
);
1291 template <typename A
>
1292 bool Parser
<A
>::hasObjC1Categories(const uint8_t* fileContent
)
1294 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1295 const uint32_t cmd_count
= header
->ncmds();
1296 const macho_load_command
<P
>* const cmds
= (macho_load_command
<P
>*)((char*)header
+ sizeof(macho_header
<P
>));
1297 const macho_load_command
<P
>* const cmdsEnd
= (macho_load_command
<P
>*)((char*)header
+ sizeof(macho_header
<P
>) + header
->sizeofcmds());
1298 const macho_load_command
<P
>* cmd
= cmds
;
1299 for (uint32_t i
= 0; i
< cmd_count
; ++i
) {
1300 if ( cmd
->cmd() == macho_segment_command
<P
>::CMD
) {
1301 const macho_segment_command
<P
>* segment
= (macho_segment_command
<P
>*)cmd
;
1302 const macho_section
<P
>* sectionsStart
= (macho_section
<P
>*)((char*)segment
+ sizeof(macho_segment_command
<P
>));
1303 for (uint32_t si
=0; si
< segment
->nsects(); ++si
) {
1304 const macho_section
<P
>* sect
= §ionsStart
[si
];
1305 if ( (sect
->size() > 0)
1306 && (strcmp(sect
->sectname(), "__category") == 0)
1307 && (strcmp(sect
->segname(), "__OBJC") == 0) ) {
1312 cmd
= (const macho_load_command
<P
>*)(((char*)cmd
)+cmd
->cmdsize());
1313 if ( cmd
> cmdsEnd
)
1314 throwf("malformed mach-o file, load command #%d is outside size of load commands", i
);
1319 template <typename A
>
1320 int Parser
<A
>::pointerSorter(const void* l
, const void* r
)
1322 // sort references by address
1323 const pint_t
* left
= (pint_t
*)l
;
1324 const pint_t
* right
= (pint_t
*)r
;
1325 return (*left
- *right
);
1328 template <typename A
>
1329 typename
A::P::uint_t Parser
<A
>::LabelAndCFIBreakIterator::peek(Parser
<A
>& parser
, pint_t startAddr
, pint_t endAddr
)
1332 if ( symIndex
< sortedSymbolCount
)
1333 symbolAddr
= parser
.symbolFromIndex(sortedSymbolIndexes
[symIndex
]).n_value();
1335 symbolAddr
= endAddr
;
1337 if ( cfiIndex
< cfiStartsCount
)
1338 cfiAddr
= cfiStartsArray
[cfiIndex
];
1341 if ( (cfiAddr
< symbolAddr
) && (cfiAddr
>= startAddr
) ) {
1342 if ( cfiAddr
< endAddr
)
1348 if ( symbolAddr
< endAddr
)
1356 // Parses up a section into chunks based on labels and CFI information.
1357 // Each call returns the next chunk address and size, and (if the break
1358 // was becuase of a label, the symbol). Returns false when no more chunks.
1360 template <typename A
>
1361 bool Parser
<A
>::LabelAndCFIBreakIterator::next(Parser
<A
>& parser
, const Section
<A
>& sect
, uint32_t sectNum
, pint_t startAddr
, pint_t endAddr
,
1362 pint_t
* addr
, pint_t
* size
, const macho_nlist
<P
>** symbol
)
1364 // may not be a label on start of section, but need atom demarcation there
1367 // advance symIndex until we get to the first label at or past the start of this section
1368 while ( symIndex
< sortedSymbolCount
) {
1369 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(sortedSymbolIndexes
[symIndex
]);
1370 if ( ! sect
.ignoreLabel(parser
.nameFromSymbol(sym
)) ) {
1371 pint_t nextSymbolAddr
= sym
.n_value();
1372 //fprintf(stderr, "sectNum=%d, nextSymbolAddr=0x%08llX, name=%s\n", sectNum, (uint64_t)nextSymbolAddr, parser.nameFromSymbol(sym));
1373 if ( (nextSymbolAddr
> startAddr
) || ((nextSymbolAddr
== startAddr
) && (sym
.n_sect() == sectNum
)) )
1378 if ( symIndex
< sortedSymbolCount
) {
1379 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(sortedSymbolIndexes
[symIndex
]);
1380 pint_t nextSymbolAddr
= sym
.n_value();
1381 // if next symbol found is not in this section
1382 if ( sym
.n_sect() != sectNum
) {
1383 // check for CFI break instead of symbol break
1384 if ( cfiIndex
< cfiStartsCount
) {
1385 pint_t nextCfiAddr
= cfiStartsArray
[cfiIndex
];
1386 if ( nextCfiAddr
< endAddr
) {
1389 *addr
= nextCfiAddr
;
1390 *size
= peek(parser
, startAddr
, endAddr
) - nextCfiAddr
;
1396 *size
= endAddr
- startAddr
;
1398 if ( startAddr
== endAddr
)
1399 return false; // zero size section
1401 return true; // whole section is one atom with no label
1403 // if also CFI break here, eat it
1404 if ( cfiIndex
< cfiStartsCount
) {
1405 if ( cfiStartsArray
[cfiIndex
] == nextSymbolAddr
)
1408 if ( nextSymbolAddr
== startAddr
) {
1409 // label at start of section, return it as chunk
1412 *size
= peek(parser
, startAddr
, endAddr
) - startAddr
;
1416 // return chunk before first symbol
1418 *size
= nextSymbolAddr
- startAddr
;
1422 // no symbols in section, check CFI
1423 if ( cfiIndex
< cfiStartsCount
) {
1424 pint_t nextCfiAddr
= cfiStartsArray
[cfiIndex
];
1425 if ( nextCfiAddr
< endAddr
) {
1428 *addr
= nextCfiAddr
;
1429 *size
= peek(parser
, startAddr
, endAddr
) - nextCfiAddr
;
1434 // no cfi, so whole section is one chunk
1436 *size
= endAddr
- startAddr
;
1438 if ( startAddr
== endAddr
)
1439 return false; // zero size section
1441 return true; // whole section is one atom with no label
1444 while ( (symIndex
< sortedSymbolCount
) && (cfiIndex
< cfiStartsCount
) ) {
1445 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(sortedSymbolIndexes
[symIndex
]);
1446 pint_t nextSymbolAddr
= sym
.n_value();
1447 pint_t nextCfiAddr
= cfiStartsArray
[cfiIndex
];
1448 if ( nextSymbolAddr
< nextCfiAddr
) {
1449 if ( nextSymbolAddr
>= endAddr
)
1452 if ( nextSymbolAddr
< startAddr
)
1454 *addr
= nextSymbolAddr
;
1455 *size
= peek(parser
, startAddr
, endAddr
) - nextSymbolAddr
;
1459 else if ( nextCfiAddr
< nextSymbolAddr
) {
1460 if ( nextCfiAddr
>= endAddr
)
1463 if ( nextCfiAddr
< startAddr
)
1465 *addr
= nextCfiAddr
;
1466 *size
= peek(parser
, startAddr
, endAddr
) - nextCfiAddr
;
1471 if ( nextCfiAddr
>= endAddr
)
1475 if ( nextCfiAddr
< startAddr
)
1477 *addr
= nextCfiAddr
;
1478 *size
= peek(parser
, startAddr
, endAddr
) - nextCfiAddr
;
1483 while ( symIndex
< sortedSymbolCount
) {
1484 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(sortedSymbolIndexes
[symIndex
]);
1485 pint_t nextSymbolAddr
= sym
.n_value();
1486 // if next symbol found is not in this section, then done with iteration
1487 if ( sym
.n_sect() != sectNum
)
1490 if ( nextSymbolAddr
< startAddr
)
1492 *addr
= nextSymbolAddr
;
1493 *size
= peek(parser
, startAddr
, endAddr
) - nextSymbolAddr
;
1497 while ( cfiIndex
< cfiStartsCount
) {
1498 pint_t nextCfiAddr
= cfiStartsArray
[cfiIndex
];
1499 if ( nextCfiAddr
>= endAddr
)
1502 if ( nextCfiAddr
< startAddr
)
1504 *addr
= nextCfiAddr
;
1505 *size
= peek(parser
, startAddr
, endAddr
) - nextCfiAddr
;
1512 #define STACK_ALLOC_IF_SMALL(_type, _name, _actual_count, _maxCount) \
1513 _type* _name = NULL; \
1514 uint32_t _name##_count = 1; \
1515 if ( _actual_count > _maxCount ) \
1516 _name = (_type*)malloc(sizeof(_type) * _actual_count); \
1518 _name##_count = _actual_count; \
1519 _type _name##_buffer[_name##_count]; \
1520 if ( _name == NULL ) \
1521 _name = _name##_buffer;
1524 template <typename A
>
1525 ld::relocatable::File
* Parser
<A
>::parse(const ParserOptions
& opts
)
1527 // create file object
1528 _file
= new File
<A
>(_path
, _modTime
, _fileContent
, _ordinal
);
1530 // respond to -t option
1531 if ( opts
.logAllFiles
)
1532 printf("%s\n", _path
);
1534 // parse start of mach-o file
1535 if ( ! parseLoadCommands() )
1539 uint32_t sortedSectionIndexes
[_machOSectionsCount
];
1540 this->makeSortedSectionsArray(sortedSectionIndexes
);
1542 // make symbol table sorted by address
1543 this->prescanSymbolTable();
1544 uint32_t sortedSymbolIndexes
[_symbolsInSections
];
1545 this->makeSortedSymbolsArray(sortedSymbolIndexes
, sortedSectionIndexes
);
1547 // allocate Section<A> object for each mach-o section
1550 // if it exists, do special early parsing of __compact_unwind section
1551 uint32_t countOfCUs
= 0;
1552 if ( _compactUnwindSection
!= NULL
)
1553 countOfCUs
= _compactUnwindSection
->count();
1554 // stack allocate (if not too large) cuInfoBuffer
1555 STACK_ALLOC_IF_SMALL(typename CUSection
<A
>::Info
, cuInfoArray
, countOfCUs
, 1024);
1556 if ( countOfCUs
!= 0 )
1557 _compactUnwindSection
->parse(*this, countOfCUs
, cuInfoArray
);
1559 // create lists of address that already have compact unwind and thus don't need the dwarf parsed
1560 unsigned cuLsdaCount
= 0;
1561 pint_t cuStarts
[countOfCUs
];
1562 for (uint32_t i
=0; i
< countOfCUs
; ++i
) {
1563 if ( CUSection
<A
>::encodingMeansUseDwarf(cuInfoArray
[i
].compactUnwindInfo
) )
1566 cuStarts
[i
] = cuInfoArray
[i
].functionStartAddress
;
1567 if ( cuInfoArray
[i
].lsdaAddress
!= 0 )
1572 // if it exists, do special early parsing of __eh_frame section
1573 // stack allocate (if not too large) array of CFI_Atom_Info
1574 uint32_t countOfCFIs
= 0;
1575 if ( _EHFrameSection
!= NULL
)
1576 countOfCFIs
= _EHFrameSection
->cfiCount();
1577 STACK_ALLOC_IF_SMALL(typename CFISection
<A
>::CFI_Atom_Info
, cfiArray
, countOfCFIs
, 1024);
1579 // stack allocate (if not too large) a copy of __eh_frame to apply relocations to
1580 uint32_t sectSize
= 4;
1581 if ( (countOfCFIs
!= 0) && _EHFrameSection
->needsRelocating() )
1582 sectSize
= _EHFrameSection
->machoSection()->size()+4;
1583 STACK_ALLOC_IF_SMALL(uint8_t, ehBuffer
, sectSize
, 50*1024);
1584 uint32_t cfiStartsCount
= 0;
1585 if ( countOfCFIs
!= 0 ) {
1586 _EHFrameSection
->cfiParse(*this, ehBuffer
, cfiArray
, countOfCFIs
, cuStarts
, countOfCUs
);
1587 // count functions and lsdas
1588 for(uint32_t i
=0; i
< countOfCFIs
; ++i
) {
1589 if ( cfiArray
[i
].isCIE
)
1591 //fprintf(stderr, "cfiArray[i].func = 0x%08llX, cfiArray[i].lsda = 0x%08llX, encoding=0x%08X\n",
1592 // (uint64_t)cfiArray[i].u.fdeInfo.function.targetAddress,
1593 // (uint64_t)cfiArray[i].u.fdeInfo.lsda.targetAddress,
1594 // cfiArray[i].u.fdeInfo.compactUnwindInfo);
1595 if ( cfiArray
[i
].u
.fdeInfo
.function
.targetAddress
!= CFI_INVALID_ADDRESS
)
1597 if ( cfiArray
[i
].u
.fdeInfo
.lsda
.targetAddress
!= CFI_INVALID_ADDRESS
)
1601 CFI_CU_InfoArrays
cfis(cfiArray
, countOfCFIs
, cuInfoArray
, countOfCUs
);
1603 // create sorted array of function starts and lsda starts
1604 pint_t cfiStartsArray
[cfiStartsCount
+cuLsdaCount
];
1605 uint32_t countOfFDEs
= 0;
1606 uint32_t cfiStartsArrayCount
= 0;
1607 if ( countOfCFIs
!= 0 ) {
1608 for(uint32_t i
=0; i
< countOfCFIs
; ++i
) {
1609 if ( cfiArray
[i
].isCIE
)
1611 if ( cfiArray
[i
].u
.fdeInfo
.function
.targetAddress
!= CFI_INVALID_ADDRESS
)
1612 cfiStartsArray
[cfiStartsArrayCount
++] = cfiArray
[i
].u
.fdeInfo
.function
.targetAddress
;
1613 if ( cfiArray
[i
].u
.fdeInfo
.lsda
.targetAddress
!= CFI_INVALID_ADDRESS
)
1614 cfiStartsArray
[cfiStartsArrayCount
++] = cfiArray
[i
].u
.fdeInfo
.lsda
.targetAddress
;
1618 if ( cuLsdaCount
!= 0 ) {
1619 // merge in an lsda info from compact unwind
1620 for (uint32_t i
=0; i
< countOfCUs
; ++i
) {
1621 if ( cuInfoArray
[i
].lsdaAddress
== 0 )
1623 // append to cfiStartsArray if not already in that list
1625 for(uint32_t j
=0; j
< cfiStartsArrayCount
; ++j
) {
1626 if ( cfiStartsArray
[j
] == cuInfoArray
[i
].lsdaAddress
)
1630 cfiStartsArray
[cfiStartsArrayCount
++] = cuInfoArray
[i
].lsdaAddress
;
1634 if ( cfiStartsArrayCount
!= 0 ) {
1635 ::qsort(cfiStartsArray
, cfiStartsArrayCount
, sizeof(pint_t
), pointerSorter
);
1637 // scan for FDEs claming the same function
1638 for(uint32_t i
=1; i
< cfiStartsArrayCount
; ++i
) {
1639 assert( cfiStartsArray
[i
] != cfiStartsArray
[i
-1] );
1644 Section
<A
>** sections
= _file
->_sectionsArray
;
1645 uint32_t sectionsCount
= _file
->_sectionsArrayCount
;
1647 // figure out how many atoms will be allocated and allocate
1648 LabelAndCFIBreakIterator
breakIterator(sortedSymbolIndexes
, _symbolsInSections
, cfiStartsArray
,
1649 cfiStartsArrayCount
, _overlappingSymbols
);
1650 uint32_t computedAtomCount
= 0;
1651 for (uint32_t i
=0; i
< sectionsCount
; ++i
) {
1652 breakIterator
.beginSection();
1653 uint32_t count
= sections
[i
]->computeAtomCount(*this, breakIterator
, cfis
);
1654 //const macho_section<P>* sect = sections[i]->machoSection();
1655 //fprintf(stderr, "computed count=%u for section %s size=%llu\n", count, sect->sectname(), (sect != NULL) ? sect->size() : 0);
1656 computedAtomCount
+= count
;
1658 //fprintf(stderr, "allocating %d atoms * sizeof(Atom<A>)=%ld, sizeof(ld::Atom)=%ld\n", computedAtomCount, sizeof(Atom<A>), sizeof(ld::Atom));
1659 _file
->_atomsArray
= new uint8_t[computedAtomCount
*sizeof(Atom
<A
>)];
1660 _file
->_atomsArrayCount
= 0;
1662 // have each section append atoms to _atomsArray
1663 LabelAndCFIBreakIterator
breakIterator2(sortedSymbolIndexes
, _symbolsInSections
, cfiStartsArray
,
1664 cfiStartsArrayCount
, _overlappingSymbols
);
1665 for (uint32_t i
=0; i
< sectionsCount
; ++i
) {
1666 uint8_t* atoms
= _file
->_atomsArray
+ _file
->_atomsArrayCount
*sizeof(Atom
<A
>);
1667 breakIterator2
.beginSection();
1668 uint32_t count
= sections
[i
]->appendAtoms(*this, atoms
, breakIterator2
, cfis
);
1669 //fprintf(stderr, "append count=%u for section %s/%s\n", count, sections[i]->machoSection()->segname(), sections[i]->machoSection()->sectname());
1670 _file
->_atomsArrayCount
+= count
;
1672 assert( _file
->_atomsArrayCount
== computedAtomCount
&& "more atoms allocated than expected");
1675 // have each section add all fix-ups for its atoms
1676 _allFixups
.reserve(computedAtomCount
*5);
1677 for (uint32_t i
=0; i
< sectionsCount
; ++i
)
1678 sections
[i
]->makeFixups(*this, cfis
);
1680 // assign fixups start offset for each atom
1681 uint8_t* p
= _file
->_atomsArray
;
1682 uint32_t fixupOffset
= 0;
1683 for(int i
=_file
->_atomsArrayCount
; i
> 0; --i
) {
1684 Atom
<A
>* atom
= (Atom
<A
>*)p
;
1685 atom
->_fixupsStartIndex
= fixupOffset
;
1686 fixupOffset
+= atom
->_fixupsCount
;
1687 atom
->_fixupsCount
= 0;
1688 p
+= sizeof(Atom
<A
>);
1690 assert(fixupOffset
== _allFixups
.size());
1691 _file
->_fixups
.reserve(fixupOffset
);
1693 // copy each fixup for each atom
1694 for(typename
std::vector
<FixupInAtom
>::iterator it
=_allFixups
.begin(); it
!= _allFixups
.end(); ++it
) {
1695 uint32_t slot
= it
->atom
->_fixupsStartIndex
+ it
->atom
->_fixupsCount
;
1696 _file
->_fixups
[slot
] = it
->fixup
;
1697 it
->atom
->_fixupsCount
++;
1700 // done with temp vector
1704 _file
->_unwindInfos
.reserve(countOfFDEs
+countOfCUs
);
1705 for(uint32_t i
=0; i
< countOfCFIs
; ++i
) {
1706 if ( cfiArray
[i
].isCIE
)
1708 if ( cfiArray
[i
].u
.fdeInfo
.function
.targetAddress
!= CFI_INVALID_ADDRESS
) {
1709 ld::Atom::UnwindInfo info
;
1710 info
.startOffset
= 0;
1711 info
.unwindInfo
= cfiArray
[i
].u
.fdeInfo
.compactUnwindInfo
;
1712 _file
->_unwindInfos
.push_back(info
);
1713 Atom
<A
>* func
= findAtomByAddress(cfiArray
[i
].u
.fdeInfo
.function
.targetAddress
);
1714 func
->setUnwindInfoRange(_file
->_unwindInfos
.size()-1, 1);
1715 //fprintf(stderr, "cu from dwarf =0x%08X, atom=%s\n", info.unwindInfo, func->name());
1718 // apply compact infos in __LD,__compact_unwind section to each function
1719 // if function also has dwarf unwind, CU will override it
1720 Atom
<A
>* lastFunc
= NULL
;
1721 uint32_t lastEnd
= 0;
1722 for(uint32_t i
=0; i
< countOfCUs
; ++i
) {
1723 typename CUSection
<A
>::Info
* info
= &cuInfoArray
[i
];
1724 assert(info
->function
!= NULL
);
1725 ld::Atom::UnwindInfo ui
;
1726 ui
.startOffset
= info
->functionStartAddress
- info
->function
->objectAddress();
1727 ui
.unwindInfo
= info
->compactUnwindInfo
;
1728 _file
->_unwindInfos
.push_back(ui
);
1729 // don't override with converted cu with "use dwarf" cu, if forcing dwarf conversion
1730 if ( !_forceDwarfConversion
|| !CUSection
<A
>::encodingMeansUseDwarf(info
->compactUnwindInfo
) ) {
1731 //fprintf(stderr, "cu=0x%08X, atom=%s\n", ui.unwindInfo, info->function->name());
1732 // if previous is for same function, extend range
1733 if ( info
->function
== lastFunc
) {
1734 if ( lastEnd
!= ui
.startOffset
) {
1735 if ( lastEnd
< ui
.startOffset
)
1736 warning("__LD,__compact_unwind entries for %s have a gap at offset 0x%0X", info
->function
->name(), lastEnd
);
1738 warning("__LD,__compact_unwind entries for %s overlap at offset 0x%0X", info
->function
->name(), lastEnd
);
1740 lastFunc
->extendUnwindInfoRange();
1743 info
->function
->setUnwindInfoRange(_file
->_unwindInfos
.size()-1, 1);
1744 lastFunc
= info
->function
;
1745 lastEnd
= ui
.startOffset
+ info
->rangeLength
;
1749 // parse dwarf debug info to get line info
1750 this->parseDebugInfo();
1757 template <> uint8_t Parser
<x86
>::loadCommandSizeMask() { return 0x03; }
1758 template <> uint8_t Parser
<x86_64
>::loadCommandSizeMask() { return 0x07; }
1759 template <> uint8_t Parser
<arm
>::loadCommandSizeMask() { return 0x03; }
1760 template <> uint8_t Parser
<arm64
>::loadCommandSizeMask() { return 0x07; }
1762 template <typename A
>
1763 bool Parser
<A
>::parseLoadCommands()
1765 const macho_header
<P
>* header
= (const macho_header
<P
>*)_fileContent
;
1767 // set File attributes
1768 _file
->_canScatterAtoms
= (header
->flags() & MH_SUBSECTIONS_VIA_SYMBOLS
);
1769 _file
->_cpuSubType
= header
->cpusubtype();
1771 const macho_segment_command
<P
>* segment
= NULL
;
1772 const uint8_t* const endOfFile
= _fileContent
+ _fileLength
;
1773 const uint32_t cmd_count
= header
->ncmds();
1774 // <rdar://problem/5394172> an empty .o file with zero load commands will crash linker
1775 if ( cmd_count
== 0 )
1777 const macho_load_command
<P
>* const cmds
= (macho_load_command
<P
>*)((char*)header
+ sizeof(macho_header
<P
>));
1778 const macho_load_command
<P
>* const cmdsEnd
= (macho_load_command
<P
>*)((char*)header
+ sizeof(macho_header
<P
>) + header
->sizeofcmds());
1779 const macho_load_command
<P
>* cmd
= cmds
;
1780 for (uint32_t i
= 0; i
< cmd_count
; ++i
) {
1781 uint32_t size
= cmd
->cmdsize();
1782 if ( (size
& this->loadCommandSizeMask()) != 0 )
1783 throwf("load command #%d has a unaligned size", i
);
1784 const uint8_t* endOfCmd
= ((uint8_t*)cmd
)+cmd
->cmdsize();
1785 if ( endOfCmd
> (uint8_t*)cmdsEnd
)
1786 throwf("load command #%d extends beyond the end of the load commands", i
);
1787 if ( endOfCmd
> endOfFile
)
1788 throwf("load command #%d extends beyond the end of the file", i
);
1789 switch (cmd
->cmd()) {
1792 const macho_symtab_command
<P
>* symtab
= (macho_symtab_command
<P
>*)cmd
;
1793 _symbolCount
= symtab
->nsyms();
1794 _symbols
= (const macho_nlist
<P
>*)(_fileContent
+ symtab
->symoff());
1795 _strings
= (char*)_fileContent
+ symtab
->stroff();
1796 _stringsSize
= symtab
->strsize();
1797 if ( (symtab
->symoff() + _symbolCount
*sizeof(macho_nlist
<P
>)) > _fileLength
)
1798 throw "mach-o symbol table extends beyond end of file";
1799 if ( (_strings
+ _stringsSize
) > (char*)endOfFile
)
1800 throw "mach-o string pool extends beyond end of file";
1801 if ( _indirectTable
== NULL
) {
1802 if ( _undefinedEndIndex
== 0 ) {
1803 _undefinedStartIndex
= 0;
1804 _undefinedEndIndex
= symtab
->nsyms();
1811 const macho_dysymtab_command
<P
>* dsymtab
= (macho_dysymtab_command
<P
>*)cmd
;
1812 _indirectTable
= (uint32_t*)(_fileContent
+ dsymtab
->indirectsymoff());
1813 _indirectTableCount
= dsymtab
->nindirectsyms();
1814 if ( &_indirectTable
[_indirectTableCount
] > (uint32_t*)endOfFile
)
1815 throw "indirect symbol table extends beyond end of file";
1816 _undefinedStartIndex
= dsymtab
->iundefsym();
1817 _undefinedEndIndex
= _undefinedStartIndex
+ dsymtab
->nundefsym();
1823 case LC_DATA_IN_CODE
:
1825 const macho_linkedit_data_command
<P
>* dc
= (macho_linkedit_data_command
<P
>*)cmd
;
1826 _dataInCodeStart
= (macho_data_in_code_entry
<P
>*)(_fileContent
+ dc
->dataoff());
1827 _dataInCodeEnd
= (macho_data_in_code_entry
<P
>*)(_fileContent
+ dc
->dataoff() + dc
->datasize());
1828 if ( _dataInCodeEnd
> (macho_data_in_code_entry
<P
>*)endOfFile
)
1829 throw "LC_DATA_IN_CODE table extends beyond end of file";
1832 case LC_LINKER_OPTION
:
1834 const macho_linker_option_command
<P
>* loc
= (macho_linker_option_command
<P
>*)cmd
;
1835 const char* buffer
= loc
->buffer();
1836 _file
->_linkerOptions
.resize(_file
->_linkerOptions
.size() + 1);
1837 std::vector
<const char*>& vec
= _file
->_linkerOptions
.back();
1838 for (uint32_t j
=0; j
< loc
->count(); ++j
) {
1839 vec
.push_back(buffer
);
1840 buffer
+= strlen(buffer
) + 1;
1842 if ( buffer
> ((char*)cmd
+ loc
->cmdsize()) )
1843 throw "malformed LC_LINKER_OPTION";
1847 if ( cmd
->cmd() == macho_segment_command
<P
>::CMD
) {
1848 if ( segment
!= NULL
)
1849 throw "more than one LC_SEGMENT found in object file";
1850 segment
= (macho_segment_command
<P
>*)cmd
;
1854 cmd
= (const macho_load_command
<P
>*)(((char*)cmd
)+cmd
->cmdsize());
1855 if ( cmd
> cmdsEnd
)
1856 throwf("malformed mach-o file, load command #%d is outside size of load commands", i
);
1859 // record range of sections
1860 if ( segment
== NULL
)
1861 throw "missing LC_SEGMENT";
1862 _sectionsStart
= (macho_section
<P
>*)((char*)segment
+ sizeof(macho_segment_command
<P
>));
1863 _machOSectionsCount
= segment
->nsects();
1869 template <typename A
>
1870 void Parser
<A
>::prescanSymbolTable()
1872 _tentativeDefinitionCount
= 0;
1873 _absoluteSymbolCount
= 0;
1874 _symbolsInSections
= 0;
1875 _hasDataInCodeLabels
= false;
1876 for (uint32_t i
=0; i
< this->_symbolCount
; ++i
) {
1877 const macho_nlist
<P
>& sym
= symbolFromIndex(i
);
1879 if ( (sym
.n_type() & N_STAB
) != 0 )
1882 // look at undefines
1883 const char* symbolName
= this->nameFromSymbol(sym
);
1884 if ( (sym
.n_type() & N_TYPE
) == N_UNDF
) {
1885 if ( sym
.n_value() != 0 ) {
1886 // count tentative definitions
1887 ++_tentativeDefinitionCount
;
1889 else if ( strncmp(symbolName
, "___dtrace_", 10) == 0 ) {
1890 // any undefined starting with __dtrace_*$ that is not ___dtrace_probe$* or ___dtrace_isenabled$*
1891 // is extra provider info
1892 if ( (strncmp(&symbolName
[10], "probe$", 6) != 0) && (strncmp(&symbolName
[10], "isenabled$", 10) != 0) ) {
1893 _dtraceProviderInfo
.push_back(symbolName
);
1899 // count absolute symbols
1900 if ( (sym
.n_type() & N_TYPE
) == N_ABS
) {
1901 const char* absName
= this->nameFromSymbol(sym
);
1902 // ignore .objc_class_name_* symbols
1903 if ( strncmp(absName
, ".objc_class_name_", 17) == 0 ) {
1907 // ignore .objc_class_name_* symbols
1908 if ( strncmp(absName
, ".objc_category_name_", 20) == 0 )
1910 // ignore empty *.eh symbols
1911 if ( strcmp(&absName
[strlen(absName
)-3], ".eh") == 0 )
1913 ++_absoluteSymbolCount
;
1916 // only look at definitions
1917 if ( (sym
.n_type() & N_TYPE
) != N_SECT
)
1920 // 'L' labels do not denote atom breaks
1921 if ( symbolName
[0] == 'L' ) {
1922 // <rdar://problem/9218847> Formalize data in code with L$start$ labels
1923 if ( strncmp(symbolName
, "L$start$", 8) == 0 )
1924 _hasDataInCodeLabels
= true;
1927 // how many def syms in each section
1928 if ( sym
.n_sect() > _machOSectionsCount
)
1929 throw "bad n_sect in symbol table";
1931 _symbolsInSections
++;
1935 template <typename A
>
1936 int Parser
<A
>::sectionIndexSorter(void* extra
, const void* l
, const void* r
)
1938 Parser
<A
>* parser
= (Parser
<A
>*)extra
;
1939 const uint32_t* left
= (uint32_t*)l
;
1940 const uint32_t* right
= (uint32_t*)r
;
1941 const macho_section
<P
>* leftSect
= parser
->machOSectionFromSectionIndex(*left
);
1942 const macho_section
<P
>* rightSect
= parser
->machOSectionFromSectionIndex(*right
);
1944 // can't just return difference because 64-bit diff does not fit in 32-bit return type
1945 int64_t result
= leftSect
->addr() - rightSect
->addr();
1946 if ( result
== 0 ) {
1947 // two sections with same start address
1948 // one with zero size goes first
1949 bool leftEmpty
= ( leftSect
->size() == 0 );
1950 bool rightEmpty
= ( rightSect
->size() == 0 );
1951 if ( leftEmpty
!= rightEmpty
) {
1952 return ( rightEmpty
? 1 : -1 );
1954 if ( !leftEmpty
&& !rightEmpty
)
1955 throwf("overlapping sections");
1956 // both empty, so chose file order
1957 return ( rightSect
- leftSect
);
1959 else if ( result
< 0 )
1965 template <typename A
>
1966 void Parser
<A
>::makeSortedSectionsArray(uint32_t array
[])
1968 const bool log
= false;
1971 fprintf(stderr
, "unsorted sections:\n");
1972 for(unsigned int i
=0; i
< _machOSectionsCount
; ++i
)
1973 fprintf(stderr
, "0x%08llX %s %s\n", _sectionsStart
[i
].addr(), _sectionsStart
[i
].segname(), _sectionsStart
[i
].sectname());
1976 // sort by symbol table address
1977 for (uint32_t i
=0; i
< _machOSectionsCount
; ++i
)
1979 ::qsort_r(array
, _machOSectionsCount
, sizeof(uint32_t), this, §ionIndexSorter
);
1982 fprintf(stderr
, "sorted sections:\n");
1983 for(unsigned int i
=0; i
< _machOSectionsCount
; ++i
)
1984 fprintf(stderr
, "0x%08llX %s %s\n", _sectionsStart
[array
[i
]].addr(), _sectionsStart
[array
[i
]].segname(), _sectionsStart
[array
[i
]].sectname());
1990 template <typename A
>
1991 int Parser
<A
>::symbolIndexSorter(void* extra
, const void* l
, const void* r
)
1993 ParserAndSectionsArray
* extraInfo
= (ParserAndSectionsArray
*)extra
;
1994 Parser
<A
>* parser
= extraInfo
->parser
;
1995 const uint32_t* sortedSectionsArray
= extraInfo
->sortedSectionsArray
;
1996 const uint32_t* left
= (uint32_t*)l
;
1997 const uint32_t* right
= (uint32_t*)r
;
1998 const macho_nlist
<P
>& leftSym
= parser
->symbolFromIndex(*left
);
1999 const macho_nlist
<P
>& rightSym
= parser
->symbolFromIndex(*right
);
2000 // can't just return difference because 64-bit diff does not fit in 32-bit return type
2001 int64_t result
= leftSym
.n_value() - rightSym
.n_value();
2002 if ( result
== 0 ) {
2003 // two symbols with same address
2004 // if in different sections, sort earlier section first
2005 if ( leftSym
.n_sect() != rightSym
.n_sect() ) {
2006 for (uint32_t i
=0; i
< parser
->machOSectionCount(); ++i
) {
2007 if ( sortedSectionsArray
[i
]+1 == leftSym
.n_sect() )
2009 if ( sortedSectionsArray
[i
]+1 == rightSym
.n_sect() )
2013 // two symbols in same section, means one is an alias
2014 // if one is ltmp*, make it an alias (sort first)
2015 const char* leftName
= parser
->nameFromSymbol(leftSym
);
2016 const char* rightName
= parser
->nameFromSymbol(rightSym
);
2017 bool leftIsTmp
= strncmp(leftName
, "ltmp", 4);
2018 bool rightIsTmp
= strncmp(rightName
, "ltmp", 4);
2019 if ( leftIsTmp
!= rightIsTmp
) {
2020 return (rightIsTmp
? -1 : 1);
2023 // if only one is global, make the other an alias (sort first)
2024 if ( (leftSym
.n_type() & N_EXT
) != (rightSym
.n_type() & N_EXT
) ) {
2025 if ( (rightSym
.n_type() & N_EXT
) != 0 )
2030 // if both are global, sort alphabetically. earlier one will be the alias
2031 return ( strcmp(rightName
, leftName
) );
2033 else if ( result
< 0 )
2040 template <typename A
>
2041 void Parser
<A
>::makeSortedSymbolsArray(uint32_t array
[], const uint32_t sectionArray
[])
2043 const bool log
= false;
2045 uint32_t* p
= array
;
2046 for (uint32_t i
=0; i
< this->_symbolCount
; ++i
) {
2047 const macho_nlist
<P
>& sym
= symbolFromIndex(i
);
2049 if ( (sym
.n_type() & N_STAB
) != 0 )
2052 // only look at definitions
2053 if ( (sym
.n_type() & N_TYPE
) != N_SECT
)
2056 // 'L' labels do not denote atom breaks
2057 const char* symbolName
= this->nameFromSymbol(sym
);
2058 if ( symbolName
[0] == 'L' )
2061 // how many def syms in each section
2062 if ( sym
.n_sect() > _machOSectionsCount
)
2063 throw "bad n_sect in symbol table";
2068 assert(p
== &array
[_symbolsInSections
] && "second pass over symbol table yield a different number of symbols");
2070 // sort by symbol table address
2071 ParserAndSectionsArray extra
= { this, sectionArray
};
2072 ::qsort_r(array
, _symbolsInSections
, sizeof(uint32_t), &extra
, &symbolIndexSorter
);
2075 // look for two symbols at same address
2076 _overlappingSymbols
= false;
2077 for (unsigned int i
=1; i
< _symbolsInSections
; ++i
) {
2078 if ( symbolFromIndex(array
[i
-1]).n_value() == symbolFromIndex(array
[i
]).n_value() ) {
2079 //fprintf(stderr, "overlapping symbols at 0x%08llX\n", symbolFromIndex(array[i-1]).n_value());
2080 _overlappingSymbols
= true;
2086 fprintf(stderr
, "sorted symbols:\n");
2087 for(unsigned int i
=0; i
< _symbolsInSections
; ++i
)
2088 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
])) );
2092 template <typename A
>
2093 void Parser
<A
>::makeSections()
2095 // classify each section by type
2096 // compute how many Section objects will be needed and total size for all
2097 unsigned int totalSectionsSize
= 0;
2098 uint8_t machOSectsStorage
[sizeof(MachOSectionAndSectionClass
<P
>)*(_machOSectionsCount
+2)]; // also room for tentative-defs and absolute symbols
2099 // allocate raw storage for all section objects on stack
2100 MachOSectionAndSectionClass
<P
>* machOSects
= (MachOSectionAndSectionClass
<P
>*)machOSectsStorage
;
2101 unsigned int count
= 0;
2102 for (uint32_t i
=0; i
< _machOSectionsCount
; ++i
) {
2103 const macho_section
<P
>* sect
= &_sectionsStart
[i
];
2104 if ( (sect
->flags() & S_ATTR_DEBUG
) != 0 ) {
2105 if ( strcmp(sect
->segname(), "__DWARF") == 0 ) {
2106 // note that .o file has dwarf
2107 _file
->_debugInfoKind
= ld::relocatable::File::kDebugInfoDwarf
;
2108 // save off iteresting dwarf sections
2109 if ( strcmp(sect
->sectname(), "__debug_info") == 0 )
2110 _file
->_dwarfDebugInfoSect
= sect
;
2111 else if ( strcmp(sect
->sectname(), "__debug_abbrev") == 0 )
2112 _file
->_dwarfDebugAbbrevSect
= sect
;
2113 else if ( strcmp(sect
->sectname(), "__debug_line") == 0 )
2114 _file
->_dwarfDebugLineSect
= sect
;
2115 else if ( strcmp(sect
->sectname(), "__debug_str") == 0 )
2116 _file
->_dwarfDebugStringSect
= sect
;
2117 // linker does not propagate dwarf sections to output file
2120 else if ( strcmp(sect
->segname(), "__LD") == 0 ) {
2121 if ( strncmp(sect
->sectname(), "__compact_unwind", 16) == 0 ) {
2122 machOSects
[count
].sect
= sect
;
2123 totalSectionsSize
+= sizeof(CUSection
<A
>);
2124 machOSects
[count
++].type
= sectionTypeCompactUnwind
;
2129 // ignore empty __OBJC sections
2130 if ( (sect
->size() == 0) && (strcmp(sect
->segname(), "__OBJC") == 0) )
2132 // objc image info section is really attributes and not content
2133 if ( ((strcmp(sect
->sectname(), "__image_info") == 0) && (strcmp(sect
->segname(), "__OBJC") == 0))
2134 || ((strncmp(sect
->sectname(), "__objc_imageinfo", 16) == 0) && (strcmp(sect
->segname(), "__DATA") == 0)) ) {
2135 // struct objc_image_info {
2136 // uint32_t version; // initially 0
2139 // #define OBJC_IMAGE_SUPPORTS_GC 2
2140 // #define OBJC_IMAGE_GC_ONLY 4
2141 // #define OBJC_IMAGE_IS_SIMULATED 32
2143 const uint32_t* contents
= (uint32_t*)(_file
->fileContent()+sect
->offset());
2144 if ( (sect
->size() >= 8) && (contents
[0] == 0) ) {
2145 uint32_t flags
= E::get32(contents
[1]);
2146 if ( (flags
& 4) == 4 )
2147 _file
->_objConstraint
= ld::File::objcConstraintGC
;
2148 else if ( (flags
& 2) == 2 )
2149 _file
->_objConstraint
= ld::File::objcConstraintRetainReleaseOrGC
;
2150 else if ( (flags
& 32) == 32 )
2151 _file
->_objConstraint
= ld::File::objcConstraintRetainReleaseForSimulator
;
2153 _file
->_objConstraint
= ld::File::objcConstraintRetainRelease
;
2154 if ( sect
->size() > 8 ) {
2155 warning("section %s/%s has unexpectedly large size %llu in %s",
2156 sect
->segname(), Section
<A
>::makeSectionName(sect
), sect
->size(), _file
->path());
2160 warning("can't parse %s/%s section in %s", sect
->segname(), Section
<A
>::makeSectionName(sect
), _file
->path());
2164 machOSects
[count
].sect
= sect
;
2165 switch ( sect
->flags() & SECTION_TYPE
) {
2166 case S_SYMBOL_STUBS
:
2167 if ( _stubsSectionNum
== 0 ) {
2168 _stubsSectionNum
= i
+1;
2169 _stubsMachOSection
= sect
;
2172 assert(1 && "multiple S_SYMBOL_STUBS sections");
2173 case S_LAZY_SYMBOL_POINTERS
:
2175 case S_4BYTE_LITERALS
:
2176 totalSectionsSize
+= sizeof(Literal4Section
<A
>);
2177 machOSects
[count
++].type
= sectionTypeLiteral4
;
2179 case S_8BYTE_LITERALS
:
2180 totalSectionsSize
+= sizeof(Literal8Section
<A
>);
2181 machOSects
[count
++].type
= sectionTypeLiteral8
;
2183 case S_16BYTE_LITERALS
:
2184 totalSectionsSize
+= sizeof(Literal16Section
<A
>);
2185 machOSects
[count
++].type
= sectionTypeLiteral16
;
2187 case S_NON_LAZY_SYMBOL_POINTERS
:
2188 totalSectionsSize
+= sizeof(NonLazyPointerSection
<A
>);
2189 machOSects
[count
++].type
= sectionTypeNonLazy
;
2191 case S_LITERAL_POINTERS
:
2192 if ( (strcmp(sect
->segname(), "__OBJC") == 0) && (strcmp(sect
->sectname(), "__cls_refs") == 0) ) {
2193 totalSectionsSize
+= sizeof(Objc1ClassReferences
<A
>);
2194 machOSects
[count
++].type
= sectionTypeObjC1ClassRefs
;
2197 totalSectionsSize
+= sizeof(PointerToCStringSection
<A
>);
2198 machOSects
[count
++].type
= sectionTypeCStringPointer
;
2201 case S_CSTRING_LITERALS
:
2202 totalSectionsSize
+= sizeof(CStringSection
<A
>);
2203 machOSects
[count
++].type
= sectionTypeCString
;
2205 case S_MOD_INIT_FUNC_POINTERS
:
2206 case S_MOD_TERM_FUNC_POINTERS
:
2207 case S_THREAD_LOCAL_INIT_FUNCTION_POINTERS
:
2212 case S_THREAD_LOCAL_REGULAR
:
2213 case S_THREAD_LOCAL_ZEROFILL
:
2214 if ( (strcmp(sect
->segname(), "__TEXT") == 0) && (strcmp(sect
->sectname(), "__eh_frame") == 0) ) {
2215 totalSectionsSize
+= sizeof(CFISection
<A
>);
2216 machOSects
[count
++].type
= sectionTypeCFI
;
2218 else if ( (strcmp(sect
->segname(), "__DATA") == 0) && (strcmp(sect
->sectname(), "__cfstring") == 0) ) {
2219 totalSectionsSize
+= sizeof(CFStringSection
<A
>);
2220 machOSects
[count
++].type
= sectionTypeCFString
;
2222 else if ( (strcmp(sect
->segname(), "__TEXT") == 0) && (strcmp(sect
->sectname(), "__ustring") == 0) ) {
2223 totalSectionsSize
+= sizeof(UTF16StringSection
<A
>);
2224 machOSects
[count
++].type
= sectionTypeUTF16Strings
;
2226 else if ( (strcmp(sect
->segname(), "__DATA") == 0) && (strncmp(sect
->sectname(), "__objc_classrefs", 16) == 0) ) {
2227 totalSectionsSize
+= sizeof(ObjC2ClassRefsSection
<A
>);
2228 machOSects
[count
++].type
= sectionTypeObjC2ClassRefs
;
2230 else if ( (strcmp(sect
->segname(), "__DATA") == 0) && (strcmp(sect
->sectname(), "__objc_catlist") == 0) ) {
2231 totalSectionsSize
+= sizeof(ObjC2CategoryListSection
<A
>);
2232 machOSects
[count
++].type
= typeObjC2CategoryList
;
2234 else if ( _AppleObjc
&& (strcmp(sect
->segname(), "__OBJC") == 0) && (strcmp(sect
->sectname(), "__class") == 0) ) {
2235 totalSectionsSize
+= sizeof(ObjC1ClassSection
<A
>);
2236 machOSects
[count
++].type
= sectionTypeObjC1Classes
;
2239 totalSectionsSize
+= sizeof(SymboledSection
<A
>);
2240 machOSects
[count
++].type
= sectionTypeSymboled
;
2243 case S_THREAD_LOCAL_VARIABLES
:
2244 totalSectionsSize
+= sizeof(TLVDefsSection
<A
>);
2245 machOSects
[count
++].type
= sectionTypeTLVDefs
;
2247 case S_THREAD_LOCAL_VARIABLE_POINTERS
:
2249 throwf("unknown section type %d", sect
->flags() & SECTION_TYPE
);
2253 // sort by address (mach-o object files don't aways have sections sorted)
2254 ::qsort(machOSects
, count
, sizeof(MachOSectionAndSectionClass
<P
>), MachOSectionAndSectionClass
<P
>::sorter
);
2256 // we will synthesize a dummy Section<A> object for tentative definitions
2257 if ( _tentativeDefinitionCount
> 0 ) {
2258 totalSectionsSize
+= sizeof(TentativeDefinitionSection
<A
>);
2259 machOSects
[count
++].type
= sectionTypeTentativeDefinitions
;
2262 // we will synthesize a dummy Section<A> object for Absolute symbols
2263 if ( _absoluteSymbolCount
> 0 ) {
2264 totalSectionsSize
+= sizeof(AbsoluteSymbolSection
<A
>);
2265 machOSects
[count
++].type
= sectionTypeAbsoluteSymbols
;
2268 // allocate one block for all Section objects as well as pointers to each
2269 uint8_t* space
= new uint8_t[totalSectionsSize
+count
*sizeof(Section
<A
>*)];
2270 _file
->_sectionsArray
= (Section
<A
>**)space
;
2271 _file
->_sectionsArrayCount
= count
;
2272 Section
<A
>** objects
= _file
->_sectionsArray
;
2273 space
+= count
*sizeof(Section
<A
>*);
2274 for (uint32_t i
=0; i
< count
; ++i
) {
2275 switch ( machOSects
[i
].type
) {
2276 case sectionTypeIgnore
:
2278 case sectionTypeLiteral4
:
2279 *objects
++ = new (space
) Literal4Section
<A
>(*this, *_file
, machOSects
[i
].sect
);
2280 space
+= sizeof(Literal4Section
<A
>);
2282 case sectionTypeLiteral8
:
2283 *objects
++ = new (space
) Literal8Section
<A
>(*this, *_file
, machOSects
[i
].sect
);
2284 space
+= sizeof(Literal8Section
<A
>);
2286 case sectionTypeLiteral16
:
2287 *objects
++ = new (space
) Literal16Section
<A
>(*this, *_file
, machOSects
[i
].sect
);
2288 space
+= sizeof(Literal16Section
<A
>);
2290 case sectionTypeNonLazy
:
2291 *objects
++ = new (space
) NonLazyPointerSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2292 space
+= sizeof(NonLazyPointerSection
<A
>);
2294 case sectionTypeCFI
:
2295 _EHFrameSection
= new (space
) CFISection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2296 *objects
++ = _EHFrameSection
;
2297 space
+= sizeof(CFISection
<A
>);
2299 case sectionTypeCString
:
2300 *objects
++ = new (space
) CStringSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2301 space
+= sizeof(CStringSection
<A
>);
2303 case sectionTypeCStringPointer
:
2304 *objects
++ = new (space
) PointerToCStringSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2305 space
+= sizeof(PointerToCStringSection
<A
>);
2307 case sectionTypeObjC1ClassRefs
:
2308 *objects
++ = new (space
) Objc1ClassReferences
<A
>(*this, *_file
, machOSects
[i
].sect
);
2309 space
+= sizeof(Objc1ClassReferences
<A
>);
2311 case sectionTypeUTF16Strings
:
2312 *objects
++ = new (space
) UTF16StringSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2313 space
+= sizeof(UTF16StringSection
<A
>);
2315 case sectionTypeCFString
:
2316 *objects
++ = new (space
) CFStringSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2317 space
+= sizeof(CFStringSection
<A
>);
2319 case sectionTypeObjC2ClassRefs
:
2320 *objects
++ = new (space
) ObjC2ClassRefsSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2321 space
+= sizeof(ObjC2ClassRefsSection
<A
>);
2323 case typeObjC2CategoryList
:
2324 *objects
++ = new (space
) ObjC2CategoryListSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2325 space
+= sizeof(ObjC2CategoryListSection
<A
>);
2327 case sectionTypeObjC1Classes
:
2328 *objects
++ = new (space
) ObjC1ClassSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2329 space
+= sizeof(ObjC1ClassSection
<A
>);
2331 case sectionTypeSymboled
:
2332 *objects
++ = new (space
) SymboledSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2333 space
+= sizeof(SymboledSection
<A
>);
2335 case sectionTypeTLVDefs
:
2336 *objects
++ = new (space
) TLVDefsSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2337 space
+= sizeof(TLVDefsSection
<A
>);
2339 case sectionTypeCompactUnwind
:
2340 _compactUnwindSection
= new (space
) CUSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2341 *objects
++ = _compactUnwindSection
;
2342 space
+= sizeof(CUSection
<A
>);
2344 case sectionTypeTentativeDefinitions
:
2345 *objects
++ = new (space
) TentativeDefinitionSection
<A
>(*this, *_file
);
2346 space
+= sizeof(TentativeDefinitionSection
<A
>);
2348 case sectionTypeAbsoluteSymbols
:
2349 _absoluteSection
= new (space
) AbsoluteSymbolSection
<A
>(*this, *_file
);
2350 *objects
++ = _absoluteSection
;
2351 space
+= sizeof(AbsoluteSymbolSection
<A
>);
2354 throw "internal error uknown SectionType";
2360 template <typename A
>
2361 Section
<A
>* Parser
<A
>::sectionForAddress(typename
A::P::uint_t addr
)
2363 for (uint32_t i
=0; i
< _file
->_sectionsArrayCount
; ++i
) {
2364 const macho_section
<typename
A::P
>* sect
= _file
->_sectionsArray
[i
]->machoSection();
2365 // TentativeDefinitionSection and AbsoluteSymbolSection have no mach-o section
2366 if ( sect
!= NULL
) {
2367 if ( (sect
->addr() <= addr
) && (addr
< (sect
->addr()+sect
->size())) ) {
2368 return _file
->_sectionsArray
[i
];
2372 // not strictly in any section
2373 // may be in a zero length section
2374 for (uint32_t i
=0; i
< _file
->_sectionsArrayCount
; ++i
) {
2375 const macho_section
<typename
A::P
>* sect
= _file
->_sectionsArray
[i
]->machoSection();
2376 // TentativeDefinitionSection and AbsoluteSymbolSection have no mach-o section
2377 if ( sect
!= NULL
) {
2378 if ( (sect
->addr() == addr
) && (sect
->size() == 0) ) {
2379 return _file
->_sectionsArray
[i
];
2384 throwf("sectionForAddress(0x%llX) address not in any section", (uint64_t)addr
);
2387 template <typename A
>
2388 Section
<A
>* Parser
<A
>::sectionForNum(unsigned int num
)
2390 for (uint32_t i
=0; i
< _file
->_sectionsArrayCount
; ++i
) {
2391 const macho_section
<typename
A::P
>* sect
= _file
->_sectionsArray
[i
]->machoSection();
2392 // TentativeDefinitionSection and AbsoluteSymbolSection have no mach-o section
2393 if ( sect
!= NULL
) {
2394 if ( num
== (unsigned int)((sect
- _sectionsStart
)+1) )
2395 return _file
->_sectionsArray
[i
];
2398 throwf("sectionForNum(%u) section number not for any section", num
);
2401 template <typename A
>
2402 Atom
<A
>* Parser
<A
>::findAtomByAddress(pint_t addr
)
2404 Section
<A
>* section
= this->sectionForAddress(addr
);
2405 return section
->findAtomByAddress(addr
);
2408 template <typename A
>
2409 Atom
<A
>* Parser
<A
>::findAtomByAddressOrNullIfStub(pint_t addr
)
2411 if ( hasStubsSection() && (_stubsMachOSection
->addr() <= addr
) && (addr
< (_stubsMachOSection
->addr()+_stubsMachOSection
->size())) )
2413 return findAtomByAddress(addr
);
2416 template <typename A
>
2417 Atom
<A
>* Parser
<A
>::findAtomByAddressOrLocalTargetOfStub(pint_t addr
, uint32_t* offsetInAtom
)
2419 if ( hasStubsSection() && (_stubsMachOSection
->addr() <= addr
) && (addr
< (_stubsMachOSection
->addr()+_stubsMachOSection
->size())) ) {
2420 // target is a stub, remove indirection
2421 uint32_t symbolIndex
= this->symbolIndexFromIndirectSectionAddress(addr
, _stubsMachOSection
);
2422 assert(symbolIndex
!= INDIRECT_SYMBOL_LOCAL
);
2423 const macho_nlist
<P
>& sym
= this->symbolFromIndex(symbolIndex
);
2424 // can't be to external weak symbol
2425 assert( (this->combineFromSymbol(sym
) != ld::Atom::combineByName
) || (this->scopeFromSymbol(sym
) != ld::Atom::scopeGlobal
) );
2427 return this->findAtomByName(this->nameFromSymbol(sym
));
2429 Atom
<A
>* target
= this->findAtomByAddress(addr
);
2430 *offsetInAtom
= addr
- target
->_objAddress
;
2434 template <typename A
>
2435 Atom
<A
>* Parser
<A
>::findAtomByName(const char* name
)
2437 uint8_t* p
= _file
->_atomsArray
;
2438 for(int i
=_file
->_atomsArrayCount
; i
> 0; --i
) {
2439 Atom
<A
>* atom
= (Atom
<A
>*)p
;
2440 if ( strcmp(name
, atom
->name()) == 0 )
2442 p
+= sizeof(Atom
<A
>);
2447 template <typename A
>
2448 void Parser
<A
>::findTargetFromAddress(pint_t addr
, TargetDesc
& target
)
2450 if ( hasStubsSection() && (_stubsMachOSection
->addr() <= addr
) && (addr
< (_stubsMachOSection
->addr()+_stubsMachOSection
->size())) ) {
2451 // target is a stub, remove indirection
2452 uint32_t symbolIndex
= this->symbolIndexFromIndirectSectionAddress(addr
, _stubsMachOSection
);
2453 assert(symbolIndex
!= INDIRECT_SYMBOL_LOCAL
);
2454 const macho_nlist
<P
>& sym
= this->symbolFromIndex(symbolIndex
);
2456 target
.name
= this->nameFromSymbol(sym
);
2457 target
.weakImport
= this->weakImportFromSymbol(sym
);
2461 Section
<A
>* section
= this->sectionForAddress(addr
);
2462 target
.atom
= section
->findAtomByAddress(addr
);
2463 target
.addend
= addr
- target
.atom
->_objAddress
;
2464 target
.weakImport
= false;
2468 template <typename A
>
2469 void Parser
<A
>::findTargetFromAddress(pint_t baseAddr
, pint_t addr
, TargetDesc
& target
)
2471 findTargetFromAddress(baseAddr
, target
);
2472 target
.addend
= addr
- target
.atom
->_objAddress
;
2475 template <typename A
>
2476 void Parser
<A
>::findTargetFromAddressAndSectionNum(pint_t addr
, unsigned int sectNum
, TargetDesc
& target
)
2478 if ( sectNum
== R_ABS
) {
2479 // target is absolute symbol that corresponds to addr
2480 if ( _absoluteSection
!= NULL
) {
2481 target
.atom
= _absoluteSection
->findAbsAtomForValue(addr
);
2482 if ( target
.atom
!= NULL
) {
2484 target
.weakImport
= false;
2489 throwf("R_ABS reloc but no absolute symbol at target address");
2492 if ( hasStubsSection() && (stubsSectionNum() == sectNum
) ) {
2493 // target is a stub, remove indirection
2494 uint32_t symbolIndex
= this->symbolIndexFromIndirectSectionAddress(addr
, _stubsMachOSection
);
2495 assert(symbolIndex
!= INDIRECT_SYMBOL_LOCAL
);
2496 const macho_nlist
<P
>& sym
= this->symbolFromIndex(symbolIndex
);
2497 // use direct reference when stub is to a static function
2498 if ( ((sym
.n_type() & N_TYPE
) == N_SECT
) && (((sym
.n_type() & N_EXT
) == 0) || (this->nameFromSymbol(sym
)[0] == 'L')) ) {
2499 this->findTargetFromAddressAndSectionNum(sym
.n_value(), sym
.n_sect(), target
);
2503 target
.name
= this->nameFromSymbol(sym
);
2504 target
.weakImport
= this->weakImportFromSymbol(sym
);
2509 Section
<A
>* section
= this->sectionForNum(sectNum
);
2510 target
.atom
= section
->findAtomByAddress(addr
);
2511 if ( target
.atom
== NULL
) {
2512 typedef typename
A::P::sint_t sint_t
;
2513 sint_t a
= (sint_t
)addr
;
2514 sint_t sectStart
= (sint_t
)(section
->machoSection()->addr());
2515 sint_t sectEnd
= sectStart
+ section
->machoSection()->size();
2516 if ( a
< sectStart
) {
2517 // target address is before start of section, so must be negative addend
2518 target
.atom
= section
->findAtomByAddress(sectStart
);
2519 target
.addend
= a
- sectStart
;
2520 target
.weakImport
= false;
2524 else if ( a
>= sectEnd
) {
2525 target
.atom
= section
->findAtomByAddress(sectEnd
-1);
2526 target
.addend
= a
- sectEnd
;
2527 target
.weakImport
= false;
2532 assert(target
.atom
!= NULL
);
2533 target
.addend
= addr
- target
.atom
->_objAddress
;
2534 target
.weakImport
= false;
2538 template <typename A
>
2539 void Parser
<A
>::addDtraceExtraInfos(const SourceLocation
& src
, const char* providerName
)
2541 // for every ___dtrace_stability$* and ___dtrace_typedefs$* undefine with
2542 // a matching provider name, add a by-name kDtraceTypeReference at probe site
2543 const char* dollar
= strchr(providerName
, '$');
2544 if ( dollar
!= NULL
) {
2545 int providerNameLen
= dollar
-providerName
+1;
2546 for ( std::vector
<const char*>::iterator it
= _dtraceProviderInfo
.begin(); it
!= _dtraceProviderInfo
.end(); ++it
) {
2547 const char* typeDollar
= strchr(*it
, '$');
2548 if ( typeDollar
!= NULL
) {
2549 if ( strncmp(typeDollar
+1, providerName
, providerNameLen
) == 0 ) {
2550 addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindDtraceExtra
,false, *it
);
2557 template <typename A
>
2558 const char* Parser
<A
>::scanSymbolTableForAddress(uint64_t addr
)
2560 uint64_t closestSymAddr
= 0;
2561 const char* closestSymName
= NULL
;
2562 for (uint32_t i
=0; i
< this->_symbolCount
; ++i
) {
2563 const macho_nlist
<P
>& sym
= symbolFromIndex(i
);
2565 if ( (sym
.n_type() & N_STAB
) != 0 )
2568 // only look at definitions
2569 if ( (sym
.n_type() & N_TYPE
) != N_SECT
)
2572 // return with exact match
2573 if ( sym
.n_value() == addr
) {
2574 const char* name
= nameFromSymbol(sym
);
2575 if ( strncmp(name
, "ltmp", 4) != 0 )
2577 // treat 'ltmp*' labels as close match
2578 closestSymAddr
= sym
.n_value();
2579 closestSymName
= name
;
2582 // record closest seen so far
2583 if ( (sym
.n_value() < addr
) && ((sym
.n_value() > closestSymAddr
) || (closestSymName
== NULL
)) )
2584 closestSymName
= nameFromSymbol(sym
);
2587 return (closestSymName
!= NULL
) ? closestSymName
: "unknown";
2591 template <typename A
>
2592 void Parser
<A
>::addFixups(const SourceLocation
& src
, ld::Fixup::Kind setKind
, const TargetDesc
& target
)
2594 // some fixup pairs can be combined
2595 ld::Fixup::Cluster cl
= ld::Fixup::k1of3
;
2596 ld::Fixup::Kind firstKind
= ld::Fixup::kindSetTargetAddress
;
2597 bool combined
= false;
2598 if ( target
.addend
== 0 ) {
2599 cl
= ld::Fixup::k1of1
;
2601 switch ( setKind
) {
2602 case ld::Fixup::kindStoreLittleEndian32
:
2603 firstKind
= ld::Fixup::kindStoreTargetAddressLittleEndian32
;
2605 case ld::Fixup::kindStoreLittleEndian64
:
2606 firstKind
= ld::Fixup::kindStoreTargetAddressLittleEndian64
;
2608 case ld::Fixup::kindStoreBigEndian32
:
2609 firstKind
= ld::Fixup::kindStoreTargetAddressBigEndian32
;
2611 case ld::Fixup::kindStoreBigEndian64
:
2612 firstKind
= ld::Fixup::kindStoreTargetAddressBigEndian64
;
2614 case ld::Fixup::kindStoreX86BranchPCRel32
:
2615 firstKind
= ld::Fixup::kindStoreTargetAddressX86BranchPCRel32
;
2617 case ld::Fixup::kindStoreX86PCRel32
:
2618 firstKind
= ld::Fixup::kindStoreTargetAddressX86PCRel32
;
2620 case ld::Fixup::kindStoreX86PCRel32GOTLoad
:
2621 firstKind
= ld::Fixup::kindStoreTargetAddressX86PCRel32GOTLoad
;
2623 case ld::Fixup::kindStoreX86PCRel32TLVLoad
:
2624 firstKind
= ld::Fixup::kindStoreTargetAddressX86PCRel32TLVLoad
;
2626 case ld::Fixup::kindStoreX86Abs32TLVLoad
:
2627 firstKind
= ld::Fixup::kindStoreTargetAddressX86Abs32TLVLoad
;
2629 case ld::Fixup::kindStoreARMBranch24
:
2630 firstKind
= ld::Fixup::kindStoreTargetAddressARMBranch24
;
2632 case ld::Fixup::kindStoreThumbBranch22
:
2633 firstKind
= ld::Fixup::kindStoreTargetAddressThumbBranch22
;
2635 #if SUPPORT_ARCH_arm64
2636 case ld::Fixup::kindStoreARM64Branch26
:
2637 firstKind
= ld::Fixup::kindStoreTargetAddressARM64Branch26
;
2639 case ld::Fixup::kindStoreARM64Page21
:
2640 firstKind
= ld::Fixup::kindStoreTargetAddressARM64Page21
;
2642 case ld::Fixup::kindStoreARM64PageOff12
:
2643 firstKind
= ld::Fixup::kindStoreTargetAddressARM64PageOff12
;
2645 case ld::Fixup::kindStoreARM64GOTLoadPage21
:
2646 firstKind
= ld::Fixup::kindStoreTargetAddressARM64GOTLoadPage21
;
2648 case ld::Fixup::kindStoreARM64GOTLoadPageOff12
:
2649 firstKind
= ld::Fixup::kindStoreTargetAddressARM64GOTLoadPageOff12
;
2654 cl
= ld::Fixup::k1of2
;
2659 if ( target
.atom
!= NULL
) {
2660 if ( target
.atom
->scope() == ld::Atom::scopeTranslationUnit
) {
2661 addFixup(src
, cl
, firstKind
, target
.atom
);
2663 else if ( (target
.atom
->combine() == ld::Atom::combineByNameAndContent
) || (target
.atom
->combine() == ld::Atom::combineByNameAndReferences
) ) {
2664 addFixup(src
, cl
, firstKind
, ld::Fixup::bindingByContentBound
, target
.atom
);
2666 else if ( (src
.atom
->section().type() == ld::Section::typeCFString
) && (src
.offsetInAtom
!= 0) ) {
2667 // backing string in CFStrings should always be direct
2668 addFixup(src
, cl
, firstKind
, target
.atom
);
2670 else if ( (src
.atom
== target
.atom
) && (target
.atom
->combine() == ld::Atom::combineByName
) ) {
2671 // reference to self should always be direct
2672 addFixup(src
, cl
, firstKind
, target
.atom
);
2675 // change direct fixup to by-name fixup
2676 addFixup(src
, cl
, firstKind
, false, target
.atom
->name());
2680 addFixup(src
, cl
, firstKind
, target
.weakImport
, target
.name
);
2682 if ( target
.addend
== 0 ) {
2684 addFixup(src
, ld::Fixup::k2of2
, setKind
);
2687 addFixup(src
, ld::Fixup::k2of3
, ld::Fixup::kindAddAddend
, target
.addend
);
2688 addFixup(src
, ld::Fixup::k3of3
, setKind
);
2692 template <typename A
>
2693 void Parser
<A
>::addFixups(const SourceLocation
& src
, ld::Fixup::Kind kind
, const TargetDesc
& target
, const TargetDesc
& picBase
)
2695 ld::Fixup::Cluster cl
= (target
.addend
== 0) ? ld::Fixup::k1of4
: ld::Fixup::k1of5
;
2696 if ( target
.atom
!= NULL
) {
2697 if ( target
.atom
->scope() == ld::Atom::scopeTranslationUnit
) {
2698 addFixup(src
, cl
, ld::Fixup::kindSetTargetAddress
, target
.atom
);
2700 else if ( (target
.atom
->combine() == ld::Atom::combineByNameAndContent
) || (target
.atom
->combine() == ld::Atom::combineByNameAndReferences
) ) {
2701 addFixup(src
, cl
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, target
.atom
);
2704 addFixup(src
, cl
, ld::Fixup::kindSetTargetAddress
, false, target
.atom
->name());
2708 addFixup(src
, cl
, ld::Fixup::kindSetTargetAddress
, target
.weakImport
, target
.name
);
2710 if ( target
.addend
== 0 ) {
2711 assert(picBase
.atom
!= NULL
);
2712 addFixup(src
, ld::Fixup::k2of4
, ld::Fixup::kindSubtractTargetAddress
, picBase
.atom
);
2713 addFixup(src
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractAddend
, picBase
.addend
);
2714 addFixup(src
, ld::Fixup::k4of4
, kind
);
2717 addFixup(src
, ld::Fixup::k2of5
, ld::Fixup::kindAddAddend
, target
.addend
);
2718 addFixup(src
, ld::Fixup::k3of5
, ld::Fixup::kindSubtractTargetAddress
, picBase
.atom
);
2719 addFixup(src
, ld::Fixup::k4of5
, ld::Fixup::kindSubtractAddend
, picBase
.addend
);
2720 addFixup(src
, ld::Fixup::k5of5
, kind
);
2726 template <typename A
>
2727 uint32_t TentativeDefinitionSection
<A
>::computeAtomCount(class Parser
<A
>& parser
,
2728 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
2729 const struct Parser
<A
>::CFI_CU_InfoArrays
&)
2731 return parser
.tentativeDefinitionCount();
2734 template <typename A
>
2735 uint32_t TentativeDefinitionSection
<A
>::appendAtoms(class Parser
<A
>& parser
, uint8_t* p
,
2736 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
2737 const struct Parser
<A
>::CFI_CU_InfoArrays
&)
2739 this->_beginAtoms
= (Atom
<A
>*)p
;
2741 for (uint32_t i
=parser
.undefinedStartIndex(); i
< parser
.undefinedEndIndex(); ++i
) {
2742 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(i
);
2743 if ( ((sym
.n_type() & N_TYPE
) == N_UNDF
) && (sym
.n_value() != 0) ) {
2744 uint64_t size
= sym
.n_value();
2745 uint8_t alignP2
= GET_COMM_ALIGN(sym
.n_desc());
2746 if ( alignP2
== 0 ) {
2747 // common symbols align to their size
2748 // that is, a 4-byte common aligns to 4-bytes
2749 // if this size is not a power of two,
2750 // then round up to the next power of two
2751 alignP2
= 63 - (uint8_t)__builtin_clzll(size
);
2752 if ( size
!= (1ULL << alignP2
) )
2755 // limit alignment of extremely large commons to 2^15 bytes (8-page)
2758 Atom
<A
>* allocatedSpace
= (Atom
<A
>*)p
;
2759 new (allocatedSpace
) Atom
<A
>(*this, parser
.nameFromSymbol(sym
), (pint_t
)ULLONG_MAX
, size
,
2760 ld::Atom::definitionTentative
, ld::Atom::combineByName
,
2761 parser
.scopeFromSymbol(sym
), ld::Atom::typeZeroFill
, ld::Atom::symbolTableIn
,
2762 parser
.dontDeadStripFromSymbol(sym
), false, false, ld::Atom::Alignment(alignP2
) );
2763 p
+= sizeof(Atom
<A
>);
2767 this->_endAtoms
= (Atom
<A
>*)p
;
2772 template <typename A
>
2773 uint32_t AbsoluteSymbolSection
<A
>::computeAtomCount(class Parser
<A
>& parser
,
2774 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
2775 const struct Parser
<A
>::CFI_CU_InfoArrays
&)
2777 return parser
.absoluteSymbolCount();
2780 template <typename A
>
2781 uint32_t AbsoluteSymbolSection
<A
>::appendAtoms(class Parser
<A
>& parser
, uint8_t* p
,
2782 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
2783 const struct Parser
<A
>::CFI_CU_InfoArrays
&)
2785 this->_beginAtoms
= (Atom
<A
>*)p
;
2787 for (uint32_t i
=0; i
< parser
.symbolCount(); ++i
) {
2788 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(i
);
2789 if ( (sym
.n_type() & N_TYPE
) != N_ABS
)
2791 const char* absName
= parser
.nameFromSymbol(sym
);
2792 // ignore .objc_class_name_* symbols
2793 if ( strncmp(absName
, ".objc_class_name_", 17) == 0 )
2795 // ignore .objc_class_name_* symbols
2796 if ( strncmp(absName
, ".objc_category_name_", 20) == 0 )
2798 // ignore empty *.eh symbols
2799 if ( strcmp(&absName
[strlen(absName
)-3], ".eh") == 0 )
2802 Atom
<A
>* allocatedSpace
= (Atom
<A
>*)p
;
2803 new (allocatedSpace
) Atom
<A
>(*this, parser
, sym
, 0);
2804 p
+= sizeof(Atom
<A
>);
2807 this->_endAtoms
= (Atom
<A
>*)p
;
2811 template <typename A
>
2812 Atom
<A
>* AbsoluteSymbolSection
<A
>::findAbsAtomForValue(typename
A::P::uint_t value
)
2814 Atom
<A
>* end
= this->_endAtoms
;
2815 for(Atom
<A
>* p
= this->_beginAtoms
; p
< end
; ++p
) {
2816 if ( p
->_objAddress
== value
)
2823 template <typename A
>
2824 uint32_t Parser
<A
>::indirectSymbol(uint32_t indirectIndex
)
2826 if ( indirectIndex
>= _indirectTableCount
)
2827 throw "indirect symbol index out of range";
2828 return E::get32(_indirectTable
[indirectIndex
]);
2831 template <typename A
>
2832 const macho_nlist
<typename
A::P
>& Parser
<A
>::symbolFromIndex(uint32_t index
)
2834 if ( index
> _symbolCount
)
2835 throw "symbol index out of range";
2836 return _symbols
[index
];
2839 template <typename A
>
2840 const macho_section
<typename
A::P
>* Parser
<A
>::machOSectionFromSectionIndex(uint32_t index
)
2842 if ( index
>= _machOSectionsCount
)
2843 throw "section index out of range";
2844 return &_sectionsStart
[index
];
2847 template <typename A
>
2848 uint32_t Parser
<A
>::symbolIndexFromIndirectSectionAddress(pint_t addr
, const macho_section
<P
>* sect
)
2850 uint32_t elementSize
= 0;
2851 switch ( sect
->flags() & SECTION_TYPE
) {
2852 case S_SYMBOL_STUBS
:
2853 elementSize
= sect
->reserved2();
2855 case S_LAZY_SYMBOL_POINTERS
:
2856 case S_NON_LAZY_SYMBOL_POINTERS
:
2857 elementSize
= sizeof(pint_t
);
2860 throw "section does not use inirect symbol table";
2862 uint32_t indexInSection
= (addr
- sect
->addr()) / elementSize
;
2863 uint32_t indexIntoIndirectTable
= sect
->reserved1() + indexInSection
;
2864 return this->indirectSymbol(indexIntoIndirectTable
);
2869 template <typename A
>
2870 const char* Parser
<A
>::nameFromSymbol(const macho_nlist
<P
>& sym
)
2872 return &_strings
[sym
.n_strx()];
2875 template <typename A
>
2876 ld::Atom::Scope Parser
<A
>::scopeFromSymbol(const macho_nlist
<P
>& sym
)
2878 if ( (sym
.n_type() & N_EXT
) == 0 )
2879 return ld::Atom::scopeTranslationUnit
;
2880 else if ( (sym
.n_type() & N_PEXT
) != 0 )
2881 return ld::Atom::scopeLinkageUnit
;
2882 else if ( this->nameFromSymbol(sym
)[0] == 'l' ) // since all 'l' symbols will be remove, don't make them global
2883 return ld::Atom::scopeLinkageUnit
;
2885 return ld::Atom::scopeGlobal
;
2888 template <typename A
>
2889 ld::Atom::Definition Parser
<A
>::definitionFromSymbol(const macho_nlist
<P
>& sym
)
2891 switch ( sym
.n_type() & N_TYPE
) {
2893 return ld::Atom::definitionAbsolute
;
2895 return ld::Atom::definitionRegular
;
2897 if ( sym
.n_value() != 0 )
2898 return ld::Atom::definitionTentative
;
2900 throw "definitionFromSymbol() bad symbol";
2903 template <typename A
>
2904 ld::Atom::Combine Parser
<A
>::combineFromSymbol(const macho_nlist
<P
>& sym
)
2906 if ( sym
.n_desc() & N_WEAK_DEF
)
2907 return ld::Atom::combineByName
;
2909 return ld::Atom::combineNever
;
2913 template <typename A
>
2914 ld::Atom::SymbolTableInclusion Parser
<A
>::inclusionFromSymbol(const macho_nlist
<P
>& sym
)
2916 const char* symbolName
= nameFromSymbol(sym
);
2917 // labels beginning with 'l' (lowercase ell) are automatically removed in final linked images <rdar://problem/4571042>
2918 // labels beginning with 'L' should have been stripped by the assembler, so are stripped now
2919 if ( sym
.n_desc() & REFERENCED_DYNAMICALLY
)
2920 return ld::Atom::symbolTableInAndNeverStrip
;
2921 else if ( symbolName
[0] == 'l' )
2922 return ld::Atom::symbolTableNotInFinalLinkedImages
;
2923 else if ( symbolName
[0] == 'L' )
2924 return ld::Atom::symbolTableNotIn
;
2926 return ld::Atom::symbolTableIn
;
2929 template <typename A
>
2930 bool Parser
<A
>::dontDeadStripFromSymbol(const macho_nlist
<P
>& sym
)
2932 return ( (sym
.n_desc() & (N_NO_DEAD_STRIP
|REFERENCED_DYNAMICALLY
)) != 0 );
2935 template <typename A
>
2936 bool Parser
<A
>::isThumbFromSymbol(const macho_nlist
<P
>& sym
)
2938 return ( sym
.n_desc() & N_ARM_THUMB_DEF
);
2941 template <typename A
>
2942 bool Parser
<A
>::weakImportFromSymbol(const macho_nlist
<P
>& sym
)
2944 return ( ((sym
.n_type() & N_TYPE
) == N_UNDF
) && ((sym
.n_desc() & N_WEAK_REF
) != 0) );
2947 template <typename A
>
2948 bool Parser
<A
>::resolverFromSymbol(const macho_nlist
<P
>& sym
)
2950 return ( sym
.n_desc() & N_SYMBOL_RESOLVER
);
2954 /* Skip over a LEB128 value (signed or unsigned). */
2956 skip_leb128 (const uint8_t ** offset
, const uint8_t * end
)
2958 while (*offset
!= end
&& **offset
>= 0x80)
2964 /* Read a ULEB128 into a 64-bit word. Return (uint64_t)-1 on overflow
2965 or error. On overflow, skip past the rest of the uleb128. */
2967 read_uleb128 (const uint8_t ** offset
, const uint8_t * end
)
2969 uint64_t result
= 0;
2976 return (uint64_t) -1;
2978 b
= **offset
& 0x7f;
2980 if (bit
>= 64 || b
<< bit
>> bit
!= b
)
2981 result
= (uint64_t) -1;
2983 result
|= b
<< bit
, bit
+= 7;
2984 } while (*(*offset
)++ >= 0x80);
2989 /* Skip over a DWARF attribute of form FORM. */
2990 template <typename A
>
2991 bool Parser
<A
>::skip_form(const uint8_t ** offset
, const uint8_t * end
, uint64_t form
,
2992 uint8_t addr_size
, bool dwarf64
)
3002 case DW_FORM_block2
:
3003 if (end
- *offset
< 2)
3005 sz
= 2 + A::P::E::get16(*(uint16_t*)offset
);
3008 case DW_FORM_block4
:
3009 if (end
- *offset
< 4)
3011 sz
= 2 + A::P::E::get32(*(uint32_t*)offset
);
3029 case DW_FORM_string
:
3030 while (*offset
!= end
&& **offset
)
3039 sz
= read_uleb128 (offset
, end
);
3042 case DW_FORM_block1
:
3050 case DW_FORM_ref_udata
:
3051 skip_leb128 (offset
, end
);
3055 case DW_FORM_ref_addr
:
3059 case DW_FORM_sec_offset
:
3060 sz
= sizeof(typename
A::P::uint_t
);
3063 case DW_FORM_exprloc
:
3064 sz
= read_uleb128 (offset
, end
);
3067 case DW_FORM_flag_present
:
3071 case DW_FORM_ref_sig8
:
3078 if (end
- *offset
< sz
)
3085 template <typename A
>
3086 const char* Parser
<A
>::getDwarfString(uint64_t form
, const uint8_t* p
)
3088 if ( form
== DW_FORM_string
)
3089 return (const char*)p
;
3090 else if ( form
== DW_FORM_strp
) {
3091 uint32_t offset
= E::get32(*((uint32_t*)p
));
3092 const char* dwarfStrings
= (char*)_file
->fileContent() + _file
->_dwarfDebugStringSect
->offset();
3093 if ( offset
> _file
->_dwarfDebugStringSect
->size() ) {
3094 warning("unknown dwarf DW_FORM_strp (offset=0x%08X) is too big in %s\n", offset
, this->_path
);
3097 return &dwarfStrings
[offset
];
3099 warning("unknown dwarf string encoding (form=%lld) in %s\n", form
, this->_path
);
3104 template <typename A
>
3105 struct AtomAndLineInfo
{
3107 ld::Atom::LineInfo info
;
3111 // <rdar://problem/5591394> Add support to ld64 for N_FUN stabs when used for symbolic constants
3112 // Returns whether a stabStr belonging to an N_FUN stab represents a
3113 // symbolic constant rather than a function
3114 template <typename A
>
3115 bool Parser
<A
>::isConstFunStabs(const char *stabStr
)
3118 // N_FUN can be used for both constants and for functions. In case it's a constant,
3119 // the format of the stabs string is "symname:c=<value>;"
3120 // ':' cannot appear in the symbol name, except if it's an Objective-C method
3121 // (in which case the symbol name starts with + or -, and then it's definitely
3123 return (stabStr
!= NULL
) && (stabStr
[0] != '+') && (stabStr
[0] != '-')
3124 && ((colon
= strchr(stabStr
, ':')) != NULL
)
3125 && (colon
[1] == 'c') && (colon
[2] == '=');
3129 template <typename A
>
3130 void Parser
<A
>::parseDebugInfo()
3132 // check for dwarf __debug_info section
3133 if ( _file
->_dwarfDebugInfoSect
== NULL
) {
3134 // if no DWARF debug info, look for stabs
3138 if ( _file
->_dwarfDebugInfoSect
->size() == 0 )
3144 if ( !read_comp_unit(&tuName
, &tuDir
, &stmtList
) ) {
3145 // if can't parse dwarf, warn and give up
3146 _file
->_dwarfTranslationUnitPath
= NULL
;
3147 warning("can't parse dwarf compilation unit info in %s", _path
);
3148 _file
->_debugInfoKind
= ld::relocatable::File::kDebugInfoNone
;
3151 if ( (tuName
!= NULL
) && (tuName
[0] == '/') ) {
3152 _file
->_dwarfTranslationUnitPath
= tuName
;
3154 else if ( (tuDir
!= NULL
) && (tuName
!= NULL
) ) {
3155 asprintf((char**)&(_file
->_dwarfTranslationUnitPath
), "%s/%s", tuDir
, tuName
);
3157 else if ( tuDir
== NULL
) {
3158 _file
->_dwarfTranslationUnitPath
= tuName
;
3161 _file
->_dwarfTranslationUnitPath
= NULL
;
3164 // add line number info to atoms from dwarf
3165 std::vector
<AtomAndLineInfo
<A
> > entries
;
3166 entries
.reserve(64);
3167 if ( _file
->_debugInfoKind
== ld::relocatable::File::kDebugInfoDwarf
) {
3168 // file with just data will have no __debug_line info
3169 if ( (_file
->_dwarfDebugLineSect
!= NULL
) && (_file
->_dwarfDebugLineSect
->size() != 0) ) {
3170 // validate stmt_list
3171 if ( (stmtList
!= (uint64_t)-1) && (stmtList
< _file
->_dwarfDebugLineSect
->size()) ) {
3172 const uint8_t* debug_line
= (uint8_t*)_file
->fileContent() + _file
->_dwarfDebugLineSect
->offset();
3173 struct line_reader_data
* lines
= line_open(&debug_line
[stmtList
],
3174 _file
->_dwarfDebugLineSect
->size() - stmtList
, E::little_endian
);
3175 struct line_info result
;
3176 Atom
<A
>* curAtom
= NULL
;
3177 uint32_t curAtomOffset
= 0;
3178 uint32_t curAtomAddress
= 0;
3179 uint32_t curAtomSize
= 0;
3180 std::map
<uint32_t,const char*> dwarfIndexToFile
;
3181 if ( lines
!= NULL
) {
3182 while ( line_next(lines
, &result
, line_stop_pc
) ) {
3183 //fprintf(stderr, "curAtom=%p, result.pc=0x%llX, result.line=%llu, result.end_of_sequence=%d,"
3184 // " curAtomAddress=0x%X, curAtomSize=0x%X\n",
3185 // curAtom, result.pc, result.line, result.end_of_sequence, curAtomAddress, curAtomSize);
3186 // work around weird debug line table compiler generates if no functions in __text section
3187 if ( (curAtom
== NULL
) && (result
.pc
== 0) && result
.end_of_sequence
&& (result
.file
== 1))
3189 // for performance, see if in next pc is in current atom
3190 if ( (curAtom
!= NULL
) && (curAtomAddress
<= result
.pc
) && (result
.pc
< (curAtomAddress
+curAtomSize
)) ) {
3191 curAtomOffset
= result
.pc
- curAtomAddress
;
3193 // or pc at end of current atom
3194 else if ( result
.end_of_sequence
&& (curAtom
!= NULL
) && (result
.pc
== (curAtomAddress
+curAtomSize
)) ) {
3195 curAtomOffset
= result
.pc
- curAtomAddress
;
3197 // or only one function that is a one line function
3198 else if ( result
.end_of_sequence
&& (curAtom
== NULL
) && (this->findAtomByAddress(0) != NULL
) && (result
.pc
== this->findAtomByAddress(0)->size()) ) {
3199 curAtom
= this->findAtomByAddress(0);
3200 curAtomOffset
= result
.pc
- curAtom
->objectAddress();
3201 curAtomAddress
= curAtom
->objectAddress();
3202 curAtomSize
= curAtom
->size();
3205 // do slow look up of atom by address
3207 curAtom
= this->findAtomByAddress(result
.pc
);
3210 // in case of bug in debug info, don't abort link, just limp on
3213 if ( curAtom
== NULL
)
3214 break; // file has line info but no functions
3215 if ( result
.end_of_sequence
&& (curAtomAddress
+curAtomSize
< result
.pc
) ) {
3216 // a one line function can be returned by line_next() as one entry with pc at end of blob
3217 // look for alt atom starting at end of previous atom
3218 uint32_t previousEnd
= curAtomAddress
+curAtomSize
;
3219 Atom
<A
>* alt
= this->findAtomByAddressOrNullIfStub(previousEnd
);
3221 continue; // ignore spurious debug info for stubs
3222 if ( result
.pc
<= alt
->objectAddress() + alt
->size() ) {
3224 curAtomOffset
= result
.pc
- alt
->objectAddress();
3225 curAtomAddress
= alt
->objectAddress();
3226 curAtomSize
= alt
->size();
3229 curAtomOffset
= result
.pc
- curAtom
->objectAddress();
3230 curAtomAddress
= curAtom
->objectAddress();
3231 curAtomSize
= curAtom
->size();
3235 curAtomOffset
= result
.pc
- curAtom
->objectAddress();
3236 curAtomAddress
= curAtom
->objectAddress();
3237 curAtomSize
= curAtom
->size();
3240 const char* filename
;
3241 std::map
<uint32_t,const char*>::iterator pos
= dwarfIndexToFile
.find(result
.file
);
3242 if ( pos
== dwarfIndexToFile
.end() ) {
3243 filename
= line_file(lines
, result
.file
);
3244 dwarfIndexToFile
[result
.file
] = filename
;
3247 filename
= pos
->second
;
3249 // only record for ~8000 line info records per function
3250 if ( curAtom
->roomForMoreLineInfoCount() ) {
3251 AtomAndLineInfo
<A
> entry
;
3252 entry
.atom
= curAtom
;
3253 entry
.info
.atomOffset
= curAtomOffset
;
3254 entry
.info
.fileName
= filename
;
3255 entry
.info
.lineNumber
= result
.line
;
3256 //fprintf(stderr, "addr=0x%08llX, line=%lld, file=%s, atom=%s, atom.size=0x%X, end=%d\n",
3257 // result.pc, result.line, filename, curAtom->name(), curAtomSize, result.end_of_sequence);
3258 entries
.push_back(entry
);
3259 curAtom
->incrementLineInfoCount();
3261 if ( result
.end_of_sequence
) {
3271 // assign line info start offset for each atom
3272 uint8_t* p
= _file
->_atomsArray
;
3273 uint32_t liOffset
= 0;
3274 for(int i
=_file
->_atomsArrayCount
; i
> 0; --i
) {
3275 Atom
<A
>* atom
= (Atom
<A
>*)p
;
3276 atom
->_lineInfoStartIndex
= liOffset
;
3277 liOffset
+= atom
->_lineInfoCount
;
3278 atom
->_lineInfoCount
= 0;
3279 p
+= sizeof(Atom
<A
>);
3281 assert(liOffset
== entries
.size());
3282 _file
->_lineInfos
.reserve(liOffset
);
3284 // copy each line info for each atom
3285 for (typename
std::vector
<AtomAndLineInfo
<A
> >::iterator it
= entries
.begin(); it
!= entries
.end(); ++it
) {
3286 uint32_t slot
= it
->atom
->_lineInfoStartIndex
+ it
->atom
->_lineInfoCount
;
3287 _file
->_lineInfos
[slot
] = it
->info
;
3288 it
->atom
->_lineInfoCount
++;
3291 // done with temp vector
3295 template <typename A
>
3296 void Parser
<A
>::parseStabs()
3298 // scan symbol table for stabs entries
3299 Atom
<A
>* currentAtom
= NULL
;
3300 pint_t currentAtomAddress
= 0;
3301 enum { start
, inBeginEnd
, inFun
} state
= start
;
3302 for (uint32_t symbolIndex
= 0; symbolIndex
< _symbolCount
; ++symbolIndex
) {
3303 const macho_nlist
<P
>& sym
= this->symbolFromIndex(symbolIndex
);
3304 bool useStab
= true;
3305 uint8_t type
= sym
.n_type();
3306 const char* symString
= (sym
.n_strx() != 0) ? this->nameFromSymbol(sym
) : NULL
;
3307 if ( (type
& N_STAB
) != 0 ) {
3308 _file
->_debugInfoKind
= (_hasUUID
? ld::relocatable::File::kDebugInfoStabsUUID
: ld::relocatable::File::kDebugInfoStabs
);
3309 ld::relocatable::File::Stab stab
;
3312 stab
.other
= sym
.n_sect();
3313 stab
.desc
= sym
.n_desc();
3314 stab
.value
= sym
.n_value();
3320 // beginning of function block
3322 // fall into case to lookup atom by addresss
3325 currentAtomAddress
= sym
.n_value();
3326 currentAtom
= this->findAtomByAddress(currentAtomAddress
);
3327 if ( currentAtom
!= NULL
) {
3328 stab
.atom
= currentAtom
;
3329 stab
.string
= symString
;
3332 fprintf(stderr
, "can't find atom for stabs BNSYM at %08llX in %s",
3333 (uint64_t)sym
.n_value(), _path
);
3342 // not associated with an atom, just copy
3343 stab
.string
= symString
;
3347 // n_value field is NOT atom address ;-(
3348 // need to find atom by name match
3349 const char* colon
= strchr(symString
, ':');
3350 if ( colon
!= NULL
) {
3351 // build underscore leading name
3352 int nameLen
= colon
- symString
;
3353 char symName
[nameLen
+2];
3354 strlcpy(&symName
[1], symString
, nameLen
+1);
3356 symName
[nameLen
+1] = '\0';
3357 currentAtom
= this->findAtomByName(symName
);
3358 if ( currentAtom
!= NULL
) {
3359 stab
.atom
= currentAtom
;
3360 stab
.string
= symString
;
3364 // might be a debug-note without trailing :G()
3365 currentAtom
= this->findAtomByName(symString
);
3366 if ( currentAtom
!= NULL
) {
3367 stab
.atom
= currentAtom
;
3368 stab
.string
= symString
;
3371 if ( stab
.atom
== NULL
) {
3372 // ld_classic added bogus GSYM stabs for old style dtrace probes
3373 if ( (strncmp(symString
, "__dtrace_probe$", 15) != 0) )
3374 warning("can't find atom for N_GSYM stabs %s in %s", symString
, _path
);
3380 if ( isConstFunStabs(symString
) ) {
3381 // constant not associated with a function
3382 stab
.string
= symString
;
3385 // old style stabs without BNSYM
3387 currentAtomAddress
= sym
.n_value();
3388 currentAtom
= this->findAtomByAddress(currentAtomAddress
);
3389 if ( currentAtom
!= NULL
) {
3390 stab
.atom
= currentAtom
;
3391 stab
.string
= symString
;
3394 warning("can't find atom for stabs FUN at %08llX in %s",
3395 (uint64_t)currentAtomAddress
, _path
);
3401 stab
.string
= symString
;
3407 stab
.string
= symString
;
3408 // -gfull built .o file
3411 warning("unknown stabs type 0x%X in %s", type
, _path
);
3415 stab
.atom
= currentAtom
;
3424 Atom
<A
>* nestedAtom
= this->findAtomByAddress(sym
.n_value());
3425 if ( nestedAtom
!= NULL
) {
3426 stab
.atom
= nestedAtom
;
3427 stab
.string
= symString
;
3430 warning("can't find atom for stabs 0x%X at %08llX in %s",
3431 type
, (uint64_t)sym
.n_value(), _path
);
3438 // adjust value to be offset in atom
3439 stab
.value
-= currentAtomAddress
;
3441 stab
.string
= symString
;
3448 if ( isConstFunStabs(symString
) ) {
3449 stab
.atom
= currentAtom
;
3450 stab
.string
= symString
;
3453 if ( sym
.n_sect() != 0 ) {
3454 // found another start stab, must be really old stabs...
3455 currentAtomAddress
= sym
.n_value();
3456 currentAtom
= this->findAtomByAddress(currentAtomAddress
);
3457 if ( currentAtom
!= NULL
) {
3458 stab
.atom
= currentAtom
;
3459 stab
.string
= symString
;
3462 warning("can't find atom for stabs FUN at %08llX in %s",
3463 (uint64_t)currentAtomAddress
, _path
);
3467 // found ending stab, switch back to start state
3468 stab
.string
= symString
;
3469 stab
.atom
= currentAtom
;
3478 // adjust value to be offset in atom
3479 stab
.value
-= currentAtomAddress
;
3480 stab
.atom
= currentAtom
;
3483 stab
.string
= symString
;
3487 stab
.atom
= currentAtom
;
3488 stab
.string
= symString
;
3493 // add to list of stabs for this .o file
3495 _file
->_stabs
.push_back(stab
);
3502 // Look at the compilation unit DIE and determine
3503 // its NAME, compilation directory (in COMP_DIR) and its
3504 // line number information offset (in STMT_LIST). NAME and COMP_DIR
3505 // may be NULL (especially COMP_DIR) if they are not in the .o file;
3506 // STMT_LIST will be (uint64_t) -1.
3508 // At present this assumes that there's only one compilation unit DIE.
3510 template <typename A
>
3511 bool Parser
<A
>::read_comp_unit(const char ** name
, const char ** comp_dir
,
3512 uint64_t *stmt_list
)
3514 const uint8_t * debug_info
;
3515 const uint8_t * debug_abbrev
;
3518 const uint8_t * end
;
3519 const uint8_t * enda
;
3522 uint64_t abbrev_base
;
3524 uint8_t address_size
;
3529 *stmt_list
= (uint64_t) -1;
3531 if ( (_file
->_dwarfDebugInfoSect
== NULL
) || (_file
->_dwarfDebugAbbrevSect
== NULL
) )
3534 debug_info
= (uint8_t*)_file
->fileContent() + _file
->_dwarfDebugInfoSect
->offset();
3535 debug_abbrev
= (uint8_t*)_file
->fileContent() + _file
->_dwarfDebugAbbrevSect
->offset();
3538 if (_file
->_dwarfDebugInfoSect
->size() < 12)
3539 /* Too small to be a real debug_info section. */
3541 sz
= A::P::E::get32(*(uint32_t*)di
);
3543 dwarf64
= sz
== 0xffffffff;
3545 sz
= A::P::E::get64(*(uint64_t*)di
), di
+= 8;
3546 else if (sz
> 0xffffff00)
3547 /* Unknown dwarf format. */
3550 /* Verify claimed size. */
3551 if (sz
+ (di
- debug_info
) > _file
->_dwarfDebugInfoSect
->size() || sz
<= (dwarf64
? 23 : 11))
3554 vers
= A::P::E::get16(*(uint16_t*)di
);
3555 if (vers
< 2 || vers
> 3)
3556 /* DWARF version wrong for this code.
3557 Chances are we could continue anyway, but we don't know for sure. */
3561 /* Find the debug_abbrev section. */
3562 abbrev_base
= dwarf64
? A::P::E::get64(*(uint64_t*)di
) : A::P::E::get32(*(uint32_t*)di
);
3563 di
+= dwarf64
? 8 : 4;
3565 if (abbrev_base
> _file
->_dwarfDebugAbbrevSect
->size())
3567 da
= debug_abbrev
+ abbrev_base
;
3568 enda
= debug_abbrev
+ _file
->_dwarfDebugAbbrevSect
->size();
3570 address_size
= *di
++;
3572 /* Find the abbrev number we're looking for. */
3574 abbrev
= read_uleb128 (&di
, end
);
3575 if (abbrev
== (uint64_t) -1)
3578 /* Skip through the debug_abbrev section looking for that abbrev. */
3581 uint64_t this_abbrev
= read_uleb128 (&da
, enda
);
3584 if (this_abbrev
== abbrev
)
3585 /* This is almost always taken. */
3587 skip_leb128 (&da
, enda
); /* Skip the tag. */
3590 da
++; /* Skip the DW_CHILDREN_* value. */
3593 attr
= read_uleb128 (&da
, enda
);
3594 skip_leb128 (&da
, enda
);
3595 } while (attr
!= 0 && attr
!= (uint64_t) -1);
3600 /* Check that the abbrev is one for a DW_TAG_compile_unit. */
3601 if (read_uleb128 (&da
, enda
) != DW_TAG_compile_unit
)
3605 da
++; /* Skip the DW_CHILDREN_* value. */
3607 /* Now, go through the DIE looking for DW_AT_name,
3608 DW_AT_comp_dir, and DW_AT_stmt_list. */
3611 uint64_t attr
= read_uleb128 (&da
, enda
);
3612 uint64_t form
= read_uleb128 (&da
, enda
);
3614 if (attr
== (uint64_t) -1)
3619 if (form
== DW_FORM_indirect
)
3620 form
= read_uleb128 (&di
, end
);
3622 if (attr
== DW_AT_name
)
3623 *name
= getDwarfString(form
, di
);
3624 else if (attr
== DW_AT_comp_dir
)
3625 *comp_dir
= getDwarfString(form
, di
);
3626 else if (attr
== DW_AT_stmt_list
&& form
== DW_FORM_data4
)
3627 *stmt_list
= A::P::E::get32(*(uint32_t*)di
);
3628 else if (attr
== DW_AT_stmt_list
&& form
== DW_FORM_data8
)
3629 *stmt_list
= A::P::E::get64(*(uint64_t*)di
);
3630 if (! skip_form (&di
, end
, form
, address_size
, dwarf64
))
3637 template <typename A
>
3640 free(_sectionsArray
);
3644 template <typename A
>
3645 const char* File
<A
>::translationUnitSource() const
3647 return _dwarfTranslationUnitPath
;
3652 template <typename A
>
3653 bool File
<A
>::forEachAtom(ld::File::AtomHandler
& handler
) const
3655 handler
.doFile(*this);
3656 uint8_t* p
= _atomsArray
;
3657 for(int i
=_atomsArrayCount
; i
> 0; --i
) {
3658 handler
.doAtom(*((Atom
<A
>*)p
));
3659 p
+= sizeof(Atom
<A
>);
3661 return (_atomsArrayCount
!= 0);
3664 template <typename A
>
3665 const char* Section
<A
>::makeSegmentName(const macho_section
<typename
A::P
>* sect
)
3667 // mach-o section record only has room for 16-byte seg/sect names
3668 // so a 16-byte name has no trailing zero
3669 const char* name
= sect
->segname();
3670 if ( strlen(name
) < 16 )
3672 char* tmp
= new char[17];
3673 strlcpy(tmp
, name
, 17);
3677 template <typename A
>
3678 const char* Section
<A
>::makeSectionName(const macho_section
<typename
A::P
>* sect
)
3680 const char* name
= sect
->sectname();
3681 if ( strlen(name
) < 16 )
3684 // special case common long section names so we don't have to malloc
3685 if ( strncmp(sect
->sectname(), "__objc_classrefs", 16) == 0 )
3686 return "__objc_classrefs";
3687 if ( strncmp(sect
->sectname(), "__objc_classlist", 16) == 0 )
3688 return "__objc_classlist";
3689 if ( strncmp(sect
->sectname(), "__objc_nlclslist", 16) == 0 )
3690 return "__objc_nlclslist";
3691 if ( strncmp(sect
->sectname(), "__objc_nlcatlist", 16) == 0 )
3692 return "__objc_nlcatlist";
3693 if ( strncmp(sect
->sectname(), "__objc_protolist", 16) == 0 )
3694 return "__objc_protolist";
3695 if ( strncmp(sect
->sectname(), "__objc_protorefs", 16) == 0 )
3696 return "__objc_protorefs";
3697 if ( strncmp(sect
->sectname(), "__objc_superrefs", 16) == 0 )
3698 return "__objc_superrefs";
3699 if ( strncmp(sect
->sectname(), "__objc_imageinfo", 16) == 0 )
3700 return "__objc_imageinfo";
3701 if ( strncmp(sect
->sectname(), "__objc_stringobj", 16) == 0 )
3702 return "__objc_stringobj";
3703 if ( strncmp(sect
->sectname(), "__gcc_except_tab", 16) == 0 )
3704 return "__gcc_except_tab";
3706 char* tmp
= new char[17];
3707 strlcpy(tmp
, name
, 17);
3711 template <typename A
>
3712 bool Section
<A
>::readable(const macho_section
<typename
A::P
>* sect
)
3717 template <typename A
>
3718 bool Section
<A
>::writable(const macho_section
<typename
A::P
>* sect
)
3720 // mach-o .o files do not contain segment permissions
3721 // we just know TEXT is special
3722 return ( strcmp(sect
->segname(), "__TEXT") != 0 );
3725 template <typename A
>
3726 bool Section
<A
>::exectuable(const macho_section
<typename
A::P
>* sect
)
3728 // mach-o .o files do not contain segment permissions
3729 // we just know TEXT is special
3730 return ( strcmp(sect
->segname(), "__TEXT") == 0 );
3734 template <typename A
>
3735 ld::Section::Type Section
<A
>::sectionType(const macho_section
<typename
A::P
>* sect
)
3737 switch ( sect
->flags() & SECTION_TYPE
) {
3739 return ld::Section::typeZeroFill
;
3740 case S_CSTRING_LITERALS
:
3741 if ( (strcmp(sect
->sectname(), "__cstring") == 0) && (strcmp(sect
->segname(), "__TEXT") == 0) )
3742 return ld::Section::typeCString
;
3744 return ld::Section::typeNonStdCString
;
3745 case S_4BYTE_LITERALS
:
3746 return ld::Section::typeLiteral4
;
3747 case S_8BYTE_LITERALS
:
3748 return ld::Section::typeLiteral8
;
3749 case S_LITERAL_POINTERS
:
3750 return ld::Section::typeCStringPointer
;
3751 case S_NON_LAZY_SYMBOL_POINTERS
:
3752 return ld::Section::typeNonLazyPointer
;
3753 case S_LAZY_SYMBOL_POINTERS
:
3754 return ld::Section::typeLazyPointer
;
3755 case S_SYMBOL_STUBS
:
3756 return ld::Section::typeStub
;
3757 case S_MOD_INIT_FUNC_POINTERS
:
3758 return ld::Section::typeInitializerPointers
;
3759 case S_MOD_TERM_FUNC_POINTERS
:
3760 return ld::Section::typeTerminatorPointers
;
3762 return ld::Section::typeUnclassified
;
3763 case S_16BYTE_LITERALS
:
3764 return ld::Section::typeLiteral16
;
3767 if ( sect
->flags() & S_ATTR_PURE_INSTRUCTIONS
) {
3768 return ld::Section::typeCode
;
3770 else if ( strcmp(sect
->segname(), "__TEXT") == 0 ) {
3771 if ( strcmp(sect
->sectname(), "__eh_frame") == 0 )
3772 return ld::Section::typeCFI
;
3773 else if ( strcmp(sect
->sectname(), "__ustring") == 0 )
3774 return ld::Section::typeUTF16Strings
;
3775 else if ( strcmp(sect
->sectname(), "__textcoal_nt") == 0 )
3776 return ld::Section::typeCode
;
3777 else if ( strcmp(sect
->sectname(), "__StaticInit") == 0 )
3778 return ld::Section::typeCode
;
3779 else if ( strcmp(sect
->sectname(), "__constructor") == 0 )
3780 return ld::Section::typeInitializerPointers
;
3782 else if ( strcmp(sect
->segname(), "__DATA") == 0 ) {
3783 if ( strcmp(sect
->sectname(), "__cfstring") == 0 )
3784 return ld::Section::typeCFString
;
3785 else if ( strcmp(sect
->sectname(), "__dyld") == 0 )
3786 return ld::Section::typeDyldInfo
;
3787 else if ( strcmp(sect
->sectname(), "__program_vars") == 0 )
3788 return ld::Section::typeDyldInfo
;
3789 else if ( strncmp(sect
->sectname(), "__objc_classrefs", 16) == 0 )
3790 return ld::Section::typeObjCClassRefs
;
3791 else if ( strcmp(sect
->sectname(), "__objc_catlist") == 0 )
3792 return ld::Section::typeObjC2CategoryList
;
3794 else if ( strcmp(sect
->segname(), "__OBJC") == 0 ) {
3795 if ( strcmp(sect
->sectname(), "__class") == 0 )
3796 return ld::Section::typeObjC1Classes
;
3799 case S_THREAD_LOCAL_REGULAR
:
3800 return ld::Section::typeTLVInitialValues
;
3801 case S_THREAD_LOCAL_ZEROFILL
:
3802 return ld::Section::typeTLVZeroFill
;
3803 case S_THREAD_LOCAL_VARIABLES
:
3804 return ld::Section::typeTLVDefs
;
3805 case S_THREAD_LOCAL_INIT_FUNCTION_POINTERS
:
3806 return ld::Section::typeTLVInitializerPointers
;
3808 return ld::Section::typeUnclassified
;
3812 template <typename A
>
3813 Atom
<A
>* Section
<A
>::findContentAtomByAddress(pint_t addr
, class Atom
<A
>* start
, class Atom
<A
>* end
)
3815 // do a binary search of atom array
3816 uint32_t atomCount
= end
- start
;
3817 Atom
<A
>* base
= start
;
3818 for (uint32_t n
= atomCount
; n
> 0; n
/= 2) {
3819 Atom
<A
>* pivot
= &base
[n
/2];
3820 pint_t atomStartAddr
= pivot
->_objAddress
;
3821 pint_t atomEndAddr
= atomStartAddr
+ pivot
->_size
;
3822 if ( atomStartAddr
<= addr
) {
3823 // address in normal atom
3824 if (addr
< atomEndAddr
)
3826 // address in "end" label (but not in alias)
3827 if ( (pivot
->_size
== 0) && (addr
== atomEndAddr
) && !pivot
->isAlias() )
3830 if ( addr
>= atomEndAddr
) {
3832 // move base to atom after pivot
3844 template <typename A
>
3845 ld::Atom::Alignment Section
<A
>::alignmentForAddress(pint_t addr
)
3847 const uint32_t sectionAlignment
= this->_machOSection
->align();
3848 return ld::Atom::Alignment(sectionAlignment
, (addr
% (1 << sectionAlignment
)));
3851 template <typename A
>
3852 uint32_t Section
<A
>::sectionNum(class Parser
<A
>& parser
) const
3854 if ( _machOSection
== NULL
)
3857 return 1 + (this->_machOSection
- parser
.firstMachOSection());
3860 // arm does not have zero cost exceptions
3861 template <> uint32_t CFISection
<arm
>::cfiCount() { return 0; }
3863 template <typename A
>
3864 uint32_t CFISection
<A
>::cfiCount()
3866 // create ObjectAddressSpace object for use by libunwind
3867 OAS
oas(*this, (uint8_t*)this->file().fileContent()+this->_machOSection
->offset());
3868 return libunwind::CFI_Parser
<OAS
>::getCFICount(oas
,
3869 this->_machOSection
->addr(), this->_machOSection
->size());
3872 template <typename A
>
3873 void CFISection
<A
>::warnFunc(void* ref
, uint64_t funcAddr
, const char* msg
)
3875 Parser
<A
>* parser
= (Parser
<A
>*)ref
;
3876 if ( ! parser
->warnUnwindConversionProblems() )
3878 if ( funcAddr
!= CFI_INVALID_ADDRESS
) {
3879 // atoms are not constructed yet, so scan symbol table for labels
3880 const char* name
= parser
->scanSymbolTableForAddress(funcAddr
);
3881 warning("could not create compact unwind for %s: %s", name
, msg
);
3884 warning("could not create compact unwind: %s", msg
);
3889 bool CFISection
<x86_64
>::needsRelocating()
3895 bool CFISection
<arm64
>::needsRelocating()
3900 template <typename A
>
3901 bool CFISection
<A
>::needsRelocating()
3907 void CFISection
<x86_64
>::cfiParse(class Parser
<x86_64
>& parser
, uint8_t* buffer
,
3908 libunwind::CFI_Atom_Info
<CFISection
<x86_64
>::OAS
>::CFI_Atom_Info cfiArray
[],
3909 uint32_t& count
, const pint_t cuStarts
[], uint32_t cuCount
)
3911 // copy __eh_frame data to buffer
3912 memcpy(buffer
, file().fileContent() + this->_machOSection
->offset(), this->_machOSection
->size());
3914 // and apply relocations
3915 const macho_relocation_info
<P
>* relocs
= (macho_relocation_info
<P
>*)(file().fileContent() + this->_machOSection
->reloff());
3916 const macho_relocation_info
<P
>* relocsEnd
= &relocs
[this->_machOSection
->nreloc()];
3917 for (const macho_relocation_info
<P
>* reloc
= relocs
; reloc
< relocsEnd
; ++reloc
) {
3919 switch ( reloc
->r_type() ) {
3920 case X86_64_RELOC_SUBTRACTOR
:
3921 value
= 0 - parser
.symbolFromIndex(reloc
->r_symbolnum()).n_value();
3923 if ( reloc
->r_extern() )
3924 value
+= parser
.symbolFromIndex(reloc
->r_symbolnum()).n_value();
3926 case X86_64_RELOC_UNSIGNED
:
3927 value
= parser
.symbolFromIndex(reloc
->r_symbolnum()).n_value();
3929 case X86_64_RELOC_GOT
:
3930 // this is used for the reference to the personality function in CIEs
3931 // store the symbol number of the personality function for later use as a Fixup
3932 value
= reloc
->r_symbolnum();
3935 fprintf(stderr
, "CFISection::cfiParse() unexpected relocation type at r_address=0x%08X\n", reloc
->r_address());
3940 switch ( reloc
->r_length() ) {
3942 p64
= (uint64_t*)&buffer
[reloc
->r_address()];
3943 E::set64(*p64
, value
+ E::get64(*p64
));
3946 p32
= (uint32_t*)&buffer
[reloc
->r_address()];
3947 E::set32(*p32
, value
+ E::get32(*p32
));
3950 fprintf(stderr
, "CFISection::cfiParse() unexpected relocation size at r_address=0x%08X\n", reloc
->r_address());
3955 // create ObjectAddressSpace object for use by libunwind
3956 OAS
oas(*this, buffer
);
3958 // use libuwind to parse __eh_frame data into array of CFI_Atom_Info
3960 msg
= libunwind::DwarfInstructions
<OAS
, libunwind::Registers_x86_64
>::parseCFIs(
3961 oas
, this->_machOSection
->addr(), this->_machOSection
->size(),
3962 cuStarts
, cuCount
, parser
.keepDwarfUnwind(), parser
.forceDwarfConversion(), cfiArray
, count
, (void*)&parser
, warnFunc
);
3964 throwf("malformed __eh_frame section: %s", msg
);
3968 void CFISection
<x86
>::cfiParse(class Parser
<x86
>& parser
, uint8_t* buffer
,
3969 libunwind::CFI_Atom_Info
<CFISection
<x86
>::OAS
>::CFI_Atom_Info cfiArray
[],
3970 uint32_t& count
, const pint_t cuStarts
[], uint32_t cuCount
)
3972 // create ObjectAddressSpace object for use by libunwind
3973 OAS
oas(*this, (uint8_t*)this->file().fileContent()+this->_machOSection
->offset());
3975 // use libuwind to parse __eh_frame data into array of CFI_Atom_Info
3977 msg
= libunwind::DwarfInstructions
<OAS
, libunwind::Registers_x86
>::parseCFIs(
3978 oas
, this->_machOSection
->addr(), this->_machOSection
->size(),
3979 cuStarts
, cuCount
, parser
.keepDwarfUnwind(), parser
.forceDwarfConversion(), cfiArray
, count
, (void*)&parser
, warnFunc
);
3981 throwf("malformed __eh_frame section: %s", msg
);
3988 void CFISection
<arm
>::cfiParse(class Parser
<arm
>& parser
, uint8_t* buffer
,
3989 libunwind::CFI_Atom_Info
<CFISection
<arm
>::OAS
>::CFI_Atom_Info cfiArray
[],
3990 uint32_t& count
, const pint_t cuStarts
[], uint32_t cuCount
)
3992 // arm does not use zero cost exceptions
3997 void CFISection
<arm64
>::cfiParse(class Parser
<arm64
>& parser
, uint8_t* buffer
,
3998 libunwind::CFI_Atom_Info
<CFISection
<arm64
>::OAS
>::CFI_Atom_Info cfiArray
[],
3999 uint32_t& count
, const pint_t cuStarts
[], uint32_t cuCount
)
4001 // copy __eh_frame data to buffer
4002 memcpy(buffer
, file().fileContent() + this->_machOSection
->offset(), this->_machOSection
->size());
4004 // and apply relocations
4005 const macho_relocation_info
<P
>* relocs
= (macho_relocation_info
<P
>*)(file().fileContent() + this->_machOSection
->reloff());
4006 const macho_relocation_info
<P
>* relocsEnd
= &relocs
[this->_machOSection
->nreloc()];
4007 for (const macho_relocation_info
<P
>* reloc
= relocs
; reloc
< relocsEnd
; ++reloc
) {
4008 uint64_t* p64
= (uint64_t*)&buffer
[reloc
->r_address()];
4009 uint32_t* p32
= (uint32_t*)&buffer
[reloc
->r_address()];
4010 uint32_t addend32
= E::get32(*p32
);
4011 uint64_t addend64
= E::get64(*p64
);
4013 switch ( reloc
->r_type() ) {
4014 case ARM64_RELOC_SUBTRACTOR
:
4015 value
= 0 - parser
.symbolFromIndex(reloc
->r_symbolnum()).n_value();
4017 if ( reloc
->r_extern() )
4018 value
+= parser
.symbolFromIndex(reloc
->r_symbolnum()).n_value();
4020 case ARM64_RELOC_UNSIGNED
:
4021 value
= parser
.symbolFromIndex(reloc
->r_symbolnum()).n_value();
4023 case ARM64_RELOC_POINTER_TO_GOT
:
4024 // this is used for the reference to the personality function in CIEs
4025 // store the symbol number of the personality function for later use as a Fixup
4026 value
= reloc
->r_symbolnum();
4031 fprintf(stderr
, "CFISection::cfiParse() unexpected relocation type at r_address=0x%08X\n", reloc
->r_address());
4034 switch ( reloc
->r_length() ) {
4036 E::set64(*p64
, value
+ addend64
);
4039 E::set32(*p32
, value
+ addend32
);
4042 fprintf(stderr
, "CFISection::cfiParse() unexpected relocation size at r_address=0x%08X\n", reloc
->r_address());
4048 // create ObjectAddressSpace object for use by libunwind
4049 OAS
oas(*this, buffer
);
4051 // use libuwind to parse __eh_frame data into array of CFI_Atom_Info
4053 msg
= libunwind::DwarfInstructions
<OAS
, libunwind::Registers_arm64
>::parseCFIs(
4054 oas
, this->_machOSection
->addr(), this->_machOSection
->size(),
4055 cuStarts
, cuCount
, parser
.keepDwarfUnwind(), parser
.forceDwarfConversion(),
4056 cfiArray
, count
, (void*)&parser
, warnFunc
);
4058 throwf("malformed __eh_frame section: %s", msg
);
4062 template <typename A
>
4063 uint32_t CFISection
<A
>::computeAtomCount(class Parser
<A
>& parser
,
4064 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
4065 const struct Parser
<A
>::CFI_CU_InfoArrays
& cfis
)
4067 return cfis
.cfiCount
;
4072 template <typename A
>
4073 uint32_t CFISection
<A
>::appendAtoms(class Parser
<A
>& parser
, uint8_t* p
,
4074 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
4075 const struct Parser
<A
>::CFI_CU_InfoArrays
& cfis
)
4077 this->_beginAtoms
= (Atom
<A
>*)p
;
4078 // walk CFI_Atom_Info array and create atom for each entry
4079 const CFI_Atom_Info
* start
= &cfis
.cfiArray
[0];
4080 const CFI_Atom_Info
* end
= &cfis
.cfiArray
[cfis
.cfiCount
];
4081 for(const CFI_Atom_Info
* a
=start
; a
< end
; ++a
) {
4082 Atom
<A
>* space
= (Atom
<A
>*)p
;
4083 new (space
) Atom
<A
>(*this, (a
->isCIE
? "CIE" : "FDE"), a
->address
, a
->size
,
4084 ld::Atom::definitionRegular
, ld::Atom::combineNever
, ld::Atom::scopeTranslationUnit
,
4085 ld::Atom::typeCFI
, ld::Atom::symbolTableNotInFinalLinkedImages
,
4086 false, false, false, ld::Atom::Alignment(0));
4087 p
+= sizeof(Atom
<A
>);
4089 this->_endAtoms
= (Atom
<A
>*)p
;
4090 return cfis
.cfiCount
;
4094 template <> bool CFISection
<x86_64
>::bigEndian() { return false; }
4095 template <> bool CFISection
<x86
>::bigEndian() { return false; }
4096 template <> bool CFISection
<arm
>::bigEndian() { return false; }
4097 template <> bool CFISection
<arm64
>::bigEndian() { return false; }
4101 void CFISection
<x86_64
>::addCiePersonalityFixups(class Parser
<x86_64
>& parser
, const CFI_Atom_Info
* cieInfo
)
4103 uint8_t personalityEncoding
= cieInfo
->u
.cieInfo
.personality
.encodingOfTargetAddress
;
4104 if ( personalityEncoding
== 0x9B ) {
4105 // compiler always produces X86_64_RELOC_GOT with addend of 4 to personality function
4106 // CFISection<x86_64>::cfiParse() set targetAddress to be symbolIndex + 4 + addressInCIE
4107 uint32_t symbolIndex
= cieInfo
->u
.cieInfo
.personality
.targetAddress
- 4
4108 - cieInfo
->address
- cieInfo
->u
.cieInfo
.personality
.offsetInCFI
;
4109 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(symbolIndex
);
4110 const char* personalityName
= parser
.nameFromSymbol(sym
);
4112 Atom
<x86_64
>* cieAtom
= this->findAtomByAddress(cieInfo
->address
);
4113 Parser
<x86_64
>::SourceLocation
src(cieAtom
, cieInfo
->u
.cieInfo
.personality
.offsetInCFI
);
4114 parser
.addFixup(src
, ld::Fixup::k1of3
, ld::Fixup::kindSetTargetAddress
, false, personalityName
);
4115 parser
.addFixup(src
, ld::Fixup::k2of3
, ld::Fixup::kindAddAddend
, 4);
4116 parser
.addFixup(src
, ld::Fixup::k3of3
, ld::Fixup::kindStoreX86PCRel32GOT
);
4118 else if ( personalityEncoding
!= 0 ) {
4119 throwf("unsupported address encoding (%02X) of personality function in CIE",
4120 personalityEncoding
);
4125 void CFISection
<x86
>::addCiePersonalityFixups(class Parser
<x86
>& parser
, const CFI_Atom_Info
* cieInfo
)
4127 uint8_t personalityEncoding
= cieInfo
->u
.cieInfo
.personality
.encodingOfTargetAddress
;
4128 if ( (personalityEncoding
== 0x9B) || (personalityEncoding
== 0x90) ) {
4129 uint32_t offsetInCFI
= cieInfo
->u
.cieInfo
.personality
.offsetInCFI
;
4130 uint32_t nlpAddr
= cieInfo
->u
.cieInfo
.personality
.targetAddress
;
4131 Atom
<x86
>* cieAtom
= this->findAtomByAddress(cieInfo
->address
);
4132 Atom
<x86
>* nlpAtom
= parser
.findAtomByAddress(nlpAddr
);
4133 assert(nlpAtom
->contentType() == ld::Atom::typeNonLazyPointer
);
4134 Parser
<x86
>::SourceLocation
src(cieAtom
, cieInfo
->u
.cieInfo
.personality
.offsetInCFI
);
4136 parser
.addFixup(src
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, nlpAtom
);
4137 parser
.addFixup(src
, ld::Fixup::k2of4
, ld::Fixup::kindSubtractTargetAddress
, cieAtom
);
4138 parser
.addFixup(src
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractAddend
, offsetInCFI
);
4139 parser
.addFixup(src
, ld::Fixup::k4of4
, ld::Fixup::kindStoreLittleEndian32
);
4141 else if ( personalityEncoding
!= 0 ) {
4142 throwf("unsupported address encoding (%02X) of personality function in CIE", personalityEncoding
);
4148 #if SUPPORT_ARCH_arm64
4150 void CFISection
<arm64
>::addCiePersonalityFixups(class Parser
<arm64
>& parser
, const CFI_Atom_Info
* cieInfo
)
4152 uint8_t personalityEncoding
= cieInfo
->u
.cieInfo
.personality
.encodingOfTargetAddress
;
4153 if ( personalityEncoding
== 0x9B ) {
4154 // compiler always produces ARM64_RELOC_GOT r_pcrel=1 to personality function
4155 // CFISection<arm64>::cfiParse() set targetAddress to be symbolIndex + addressInCIE
4156 uint32_t symbolIndex
= cieInfo
->u
.cieInfo
.personality
.targetAddress
4157 - cieInfo
->address
- cieInfo
->u
.cieInfo
.personality
.offsetInCFI
;
4158 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(symbolIndex
);
4159 const char* personalityName
= parser
.nameFromSymbol(sym
);
4161 Atom
<arm64
>* cieAtom
= this->findAtomByAddress(cieInfo
->address
);
4162 Parser
<arm64
>::SourceLocation
src(cieAtom
, cieInfo
->u
.cieInfo
.personality
.offsetInCFI
);
4163 parser
.addFixup(src
, ld::Fixup::k1of2
, ld::Fixup::kindSetTargetAddress
, false, personalityName
);
4164 parser
.addFixup(src
, ld::Fixup::k2of2
, ld::Fixup::kindStoreARM64PCRelToGOT
);
4166 else if ( personalityEncoding
!= 0 ) {
4167 throwf("unsupported address encoding (%02X) of personality function in CIE",
4168 personalityEncoding
);
4173 template <typename A
>
4174 void CFISection
<A
>::addCiePersonalityFixups(class Parser
<A
>& parser
, const CFI_Atom_Info
* cieInfo
)
4176 assert(0 && "addCiePersonalityFixups() not implemented for arch");
4179 template <typename A
>
4180 void CFISection
<A
>::makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
& cfis
)
4182 ld::Fixup::Kind store32
= bigEndian() ? ld::Fixup::kindStoreBigEndian32
: ld::Fixup::kindStoreLittleEndian32
;
4183 ld::Fixup::Kind store64
= bigEndian() ? ld::Fixup::kindStoreBigEndian64
: ld::Fixup::kindStoreLittleEndian64
;
4185 // add all references for FDEs, including implicit group references
4186 const CFI_Atom_Info
* end
= &cfis
.cfiArray
[cfis
.cfiCount
];
4187 for(const CFI_Atom_Info
* p
= &cfis
.cfiArray
[0]; p
< end
; ++p
) {
4189 // add reference to personality function if used
4190 if ( p
->u
.cieInfo
.personality
.targetAddress
!= CFI_INVALID_ADDRESS
) {
4191 this->addCiePersonalityFixups(parser
, p
);
4196 Atom
<A
>* fdeAtom
= this->findAtomByAddress(p
->address
);
4197 // find function Atom
4198 Atom
<A
>* functionAtom
= parser
.findAtomByAddress(p
->u
.fdeInfo
.function
.targetAddress
);
4200 Atom
<A
>* cieAtom
= this->findAtomByAddress(p
->u
.fdeInfo
.cie
.targetAddress
);
4202 Atom
<A
>* lsdaAtom
= NULL
;
4203 if ( p
->u
.fdeInfo
.lsda
.targetAddress
!= CFI_INVALID_ADDRESS
) {
4204 lsdaAtom
= parser
.findAtomByAddress(p
->u
.fdeInfo
.lsda
.targetAddress
);
4206 // add reference from FDE to CIE (always 32-bit pc-rel)
4207 typename Parser
<A
>::SourceLocation
fdeToCieSrc(fdeAtom
, p
->u
.fdeInfo
.cie
.offsetInCFI
);
4208 parser
.addFixup(fdeToCieSrc
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, fdeAtom
);
4209 parser
.addFixup(fdeToCieSrc
, ld::Fixup::k2of4
, ld::Fixup::kindAddAddend
, p
->u
.fdeInfo
.cie
.offsetInCFI
);
4210 parser
.addFixup(fdeToCieSrc
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractTargetAddress
, cieAtom
);
4211 parser
.addFixup(fdeToCieSrc
, ld::Fixup::k4of4
, store32
, cieAtom
);
4213 // add reference from FDE to function
4214 typename Parser
<A
>::SourceLocation
fdeToFuncSrc(fdeAtom
, p
->u
.fdeInfo
.function
.offsetInCFI
);
4215 switch (p
->u
.fdeInfo
.function
.encodingOfTargetAddress
) {
4216 case DW_EH_PE_pcrel
|DW_EH_PE_ptr
:
4217 if ( sizeof(typename
A::P::uint_t
) == 8 ) {
4218 parser
.addFixup(fdeToFuncSrc
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, functionAtom
);
4219 parser
.addFixup(fdeToFuncSrc
, ld::Fixup::k2of4
, ld::Fixup::kindSubtractTargetAddress
, fdeAtom
);
4220 parser
.addFixup(fdeToFuncSrc
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractAddend
, p
->u
.fdeInfo
.function
.offsetInCFI
);
4221 parser
.addFixup(fdeToFuncSrc
, ld::Fixup::k4of4
, store64
);
4224 // else fall into 32-bit case
4225 case DW_EH_PE_pcrel
|DW_EH_PE_sdata4
:
4226 parser
.addFixup(fdeToFuncSrc
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, functionAtom
);
4227 parser
.addFixup(fdeToFuncSrc
, ld::Fixup::k2of4
, ld::Fixup::kindSubtractTargetAddress
, fdeAtom
);
4228 parser
.addFixup(fdeToFuncSrc
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractAddend
, p
->u
.fdeInfo
.function
.offsetInCFI
);
4229 parser
.addFixup(fdeToFuncSrc
, ld::Fixup::k4of4
, store32
);
4232 throw "unsupported encoding in FDE of pointer to function";
4235 // add reference from FDE to LSDA
4236 typename Parser
<A
>::SourceLocation
fdeToLsdaSrc(fdeAtom
, p
->u
.fdeInfo
.lsda
.offsetInCFI
);
4237 if ( lsdaAtom
!= NULL
) {
4238 switch (p
->u
.fdeInfo
.lsda
.encodingOfTargetAddress
) {
4239 case DW_EH_PE_pcrel
|DW_EH_PE_ptr
:
4240 if ( sizeof(typename
A::P::uint_t
) == 8 ) {
4241 parser
.addFixup(fdeToLsdaSrc
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, lsdaAtom
);
4242 parser
.addFixup(fdeToLsdaSrc
, ld::Fixup::k2of4
, ld::Fixup::kindSubtractTargetAddress
, fdeAtom
);
4243 parser
.addFixup(fdeToLsdaSrc
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractAddend
, p
->u
.fdeInfo
.lsda
.offsetInCFI
);
4244 parser
.addFixup(fdeToLsdaSrc
, ld::Fixup::k4of4
, store64
);
4247 // else fall into 32-bit case
4248 case DW_EH_PE_pcrel
|DW_EH_PE_sdata4
:
4249 parser
.addFixup(fdeToLsdaSrc
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, lsdaAtom
);
4250 parser
.addFixup(fdeToLsdaSrc
, ld::Fixup::k2of4
, ld::Fixup::kindSubtractTargetAddress
, fdeAtom
);
4251 parser
.addFixup(fdeToLsdaSrc
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractAddend
, p
->u
.fdeInfo
.lsda
.offsetInCFI
);
4252 parser
.addFixup(fdeToLsdaSrc
, ld::Fixup::k4of4
, store32
);
4255 throw "unsupported encoding in FDE of pointer to LSDA";
4259 // FDE is in group lead by function atom
4260 typename Parser
<A
>::SourceLocation
fdeSrc(functionAtom
,0);
4261 parser
.addFixup(fdeSrc
, ld::Fixup::k1of1
, ld::Fixup::kindNoneGroupSubordinateFDE
, fdeAtom
);
4263 // LSDA is in group lead by function atom
4264 if ( lsdaAtom
!= NULL
) {
4265 parser
.addFixup(fdeSrc
, ld::Fixup::k1of1
, ld::Fixup::kindNoneGroupSubordinateLSDA
, lsdaAtom
);
4274 template <typename A
>
4275 const void* CFISection
<A
>::OAS::mappedAddress(pint_t addr
)
4277 if ( (_ehFrameStartAddr
<= addr
) && (addr
< _ehFrameEndAddr
) )
4278 return &_ehFrameContent
[addr
-_ehFrameStartAddr
];
4280 // requested bytes are not in __eh_frame section
4281 // this can occur when examining the instruction bytes in the __text
4282 File
<A
>& file
= _ehFrameSection
.file();
4283 for (uint32_t i
=0; i
< file
._sectionsArrayCount
; ++i
) {
4284 const macho_section
<typename
A::P
>* sect
= file
._sectionsArray
[i
]->machoSection();
4285 // TentativeDefinitionSection and AbsoluteSymbolSection have no mach-o section
4286 if ( sect
!= NULL
) {
4287 if ( (sect
->addr() <= addr
) && (addr
< (sect
->addr()+sect
->size())) ) {
4288 return file
.fileContent() + sect
->offset() + addr
- sect
->addr();
4292 throwf("__eh_frame parsing problem. Can't find target of reference to address 0x%08llX", (uint64_t)addr
);
4297 template <typename A
>
4298 uint64_t CFISection
<A
>::OAS::getULEB128(pint_t
& logicalAddr
, pint_t end
)
4300 uintptr_t size
= (end
- logicalAddr
);
4301 libunwind::LocalAddressSpace::pint_t laddr
= (libunwind::LocalAddressSpace::pint_t
)mappedAddress(logicalAddr
);
4302 libunwind::LocalAddressSpace::pint_t sladdr
= laddr
;
4303 uint64_t result
= libunwind::LocalAddressSpace::getULEB128(laddr
, laddr
+size
);
4304 logicalAddr
+= (laddr
-sladdr
);
4308 template <typename A
>
4309 int64_t CFISection
<A
>::OAS::getSLEB128(pint_t
& logicalAddr
, pint_t end
)
4311 uintptr_t size
= (end
- logicalAddr
);
4312 libunwind::LocalAddressSpace::pint_t laddr
= (libunwind::LocalAddressSpace::pint_t
)mappedAddress(logicalAddr
);
4313 libunwind::LocalAddressSpace::pint_t sladdr
= laddr
;
4314 int64_t result
= libunwind::LocalAddressSpace::getSLEB128(laddr
, laddr
+size
);
4315 logicalAddr
+= (laddr
-sladdr
);
4319 template <typename A
>
4320 typename
A::P::uint_t CFISection
<A
>::OAS::getEncodedP(pint_t
& addr
, pint_t end
, uint8_t encoding
)
4322 pint_t startAddr
= addr
;
4327 switch (encoding
& 0x0F) {
4329 result
= getP(addr
);
4330 p
+= sizeof(pint_t
);
4333 case DW_EH_PE_uleb128
:
4334 result
= getULEB128(addr
, end
);
4336 case DW_EH_PE_udata2
:
4337 result
= get16(addr
);
4341 case DW_EH_PE_udata4
:
4342 result
= get32(addr
);
4346 case DW_EH_PE_udata8
:
4347 result
= get64(addr
);
4351 case DW_EH_PE_sleb128
:
4352 result
= getSLEB128(addr
, end
);
4354 case DW_EH_PE_sdata2
:
4355 result
= (int16_t)get16(addr
);
4359 case DW_EH_PE_sdata4
:
4360 result
= (int32_t)get32(addr
);
4364 case DW_EH_PE_sdata8
:
4365 result
= get64(addr
);
4370 throwf("ObjectFileAddressSpace<A>::getEncodedP() encoding 0x%08X not supported", encoding
);
4373 // then add relative offset
4374 switch ( encoding
& 0x70 ) {
4375 case DW_EH_PE_absptr
:
4378 case DW_EH_PE_pcrel
:
4379 result
+= startAddr
;
4381 case DW_EH_PE_textrel
:
4382 throw "DW_EH_PE_textrel pointer encoding not supported";
4384 case DW_EH_PE_datarel
:
4385 throw "DW_EH_PE_datarel pointer encoding not supported";
4387 case DW_EH_PE_funcrel
:
4388 throw "DW_EH_PE_funcrel pointer encoding not supported";
4390 case DW_EH_PE_aligned
:
4391 throw "DW_EH_PE_aligned pointer encoding not supported";
4394 throwf("ObjectFileAddressSpace<A>::getEncodedP() encoding 0x%08X not supported", encoding
);
4398 // Note: DW_EH_PE_indirect is only used in CIEs to refernce the personality pointer
4399 // When parsing .o files that pointer contains zero, so we don't to return that.
4400 // Instead we skip the dereference and return the address of the pointer.
4401 // if ( encoding & DW_EH_PE_indirect )
4402 // result = getP(result);
4408 const char* CUSection
<x86_64
>::personalityName(class Parser
<x86_64
>& parser
, const macho_relocation_info
<x86_64::P
>* reloc
)
4410 if ( reloc
->r_extern() ) {
4411 assert((reloc
->r_type() == X86_64_RELOC_UNSIGNED
) && "wrong reloc type on personality column in __compact_unwind section");
4412 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(reloc
->r_symbolnum());
4413 return parser
.nameFromSymbol(sym
);
4416 const pint_t
* content
= (pint_t
*)(this->file().fileContent() + this->_machOSection
->offset() + reloc
->r_address());
4417 pint_t personalityAddr
= *content
;
4418 Section
<x86_64
>* personalitySection
= parser
.sectionForAddress(personalityAddr
);
4419 assert((personalitySection
->type() == ld::Section::typeCode
) && "personality column in __compact_unwind section is not pointer to function");
4420 // atoms may not be constructed yet, so scan symbol table for labels
4421 const char* name
= parser
.scanSymbolTableForAddress(personalityAddr
);
4427 const char* CUSection
<x86
>::personalityName(class Parser
<x86
>& parser
, const macho_relocation_info
<x86::P
>* reloc
)
4429 if ( reloc
->r_extern() ) {
4430 assert((reloc
->r_type() == GENERIC_RELOC_VANILLA
) && "wrong reloc type on personality column in __compact_unwind section");
4431 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(reloc
->r_symbolnum());
4432 return parser
.nameFromSymbol(sym
);
4435 // support __LD, __compact_unwind personality entries which are pointer to personality non-lazy pointer
4436 const pint_t
* content
= (pint_t
*)(this->file().fileContent() + this->_machOSection
->offset() + reloc
->r_address());
4437 pint_t nlPointerAddr
= *content
;
4438 Section
<x86
>* nlSection
= parser
.sectionForAddress(nlPointerAddr
);
4439 if ( nlSection
->type() == ld::Section::typeCode
) {
4440 // personality function is defined in this .o file, so this is a direct reference to it
4441 // atoms may not be constructed yet, so scan symbol table for labels
4442 const char* name
= parser
.scanSymbolTableForAddress(nlPointerAddr
);
4446 uint32_t symIndex
= parser
.symbolIndexFromIndirectSectionAddress(nlPointerAddr
, nlSection
->machoSection());
4447 const macho_nlist
<P
>& nlSymbol
= parser
.symbolFromIndex(symIndex
);
4448 return parser
.nameFromSymbol(nlSymbol
);
4453 #if SUPPORT_ARCH_arm64
4455 const char* CUSection
<arm64
>::personalityName(class Parser
<arm64
>& parser
, const macho_relocation_info
<arm64::P
>* reloc
)
4457 if ( reloc
->r_extern() ) {
4458 assert((reloc
->r_type() == ARM64_RELOC_UNSIGNED
) && "wrong reloc type on personality column in __compact_unwind section");
4459 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(reloc
->r_symbolnum());
4460 return parser
.nameFromSymbol(sym
);
4463 const pint_t
* content
= (pint_t
*)(this->file().fileContent() + this->_machOSection
->offset() + reloc
->r_address());
4464 pint_t personalityAddr
= *content
;
4465 Section
<arm64
>* personalitySection
= parser
.sectionForAddress(personalityAddr
);
4466 assert((personalitySection
->type() == ld::Section::typeCode
) && "personality column in __compact_unwind section is not pointer to function");
4467 // atoms may not be constructed yet, so scan symbol table for labels
4468 const char* name
= parser
.scanSymbolTableForAddress(personalityAddr
);
4474 template <typename A
>
4475 const char* CUSection
<A
>::personalityName(class Parser
<A
>& parser
, const macho_relocation_info
<P
>* reloc
)
4481 bool CUSection
<x86
>::encodingMeansUseDwarf(compact_unwind_encoding_t enc
)
4483 return ((enc
& UNWIND_X86_MODE_MASK
) == UNWIND_X86_MODE_DWARF
);
4487 bool CUSection
<x86_64
>::encodingMeansUseDwarf(compact_unwind_encoding_t enc
)
4489 return ((enc
& UNWIND_X86_64_MODE_MASK
) == UNWIND_X86_64_MODE_DWARF
);
4492 #if SUPPORT_ARCH_arm_any
4494 bool CUSection
<arm
>::encodingMeansUseDwarf(compact_unwind_encoding_t enc
)
4500 #if SUPPORT_ARCH_arm64
4502 bool CUSection
<arm64
>::encodingMeansUseDwarf(compact_unwind_encoding_t enc
)
4504 return ((enc
& UNWIND_ARM64_MODE_MASK
) == UNWIND_ARM64_MODE_DWARF
);
4508 template <typename A
>
4509 int CUSection
<A
>::infoSorter(const void* l
, const void* r
)
4511 // sort references by symbol index, then address
4512 const Info
* left
= (Info
*)l
;
4513 const Info
* right
= (Info
*)r
;
4514 if ( left
->functionSymbolIndex
== right
->functionSymbolIndex
)
4515 return (left
->functionStartAddress
- right
->functionStartAddress
);
4517 return (left
->functionSymbolIndex
- right
->functionSymbolIndex
);
4520 template <typename A
>
4521 void CUSection
<A
>::parse(class Parser
<A
>& parser
, uint32_t cnt
, Info array
[])
4523 // walk section content and copy to Info array
4524 const macho_compact_unwind_entry
<P
>* const entries
= (macho_compact_unwind_entry
<P
>*)(this->file().fileContent() + this->_machOSection
->offset());
4525 for (uint32_t i
=0; i
< cnt
; ++i
) {
4526 Info
* info
= &array
[i
];
4527 const macho_compact_unwind_entry
<P
>* entry
= &entries
[i
];
4528 info
->functionStartAddress
= entry
->codeStart();
4529 info
->functionSymbolIndex
= 0xFFFFFFFF;
4530 info
->rangeLength
= entry
->codeLen();
4531 info
->compactUnwindInfo
= entry
->compactUnwindInfo();
4532 info
->personality
= NULL
;
4533 info
->lsdaAddress
= entry
->lsda();
4534 info
->function
= NULL
;
4536 if ( (info
->compactUnwindInfo
& UNWIND_PERSONALITY_MASK
) != 0 )
4537 warning("no bits should be set in UNWIND_PERSONALITY_MASK of compact unwind encoding in __LD,__compact_unwind section");
4538 if ( info
->lsdaAddress
!= 0 ) {
4539 info
->compactUnwindInfo
|= UNWIND_HAS_LSDA
;
4543 // scan relocs, extern relocs are needed for personality references (possibly for function/lsda refs??)
4544 const macho_relocation_info
<P
>* relocs
= (macho_relocation_info
<P
>*)(this->file().fileContent() + this->_machOSection
->reloff());
4545 const macho_relocation_info
<P
>* relocsEnd
= &relocs
[this->_machOSection
->nreloc()];
4546 for (const macho_relocation_info
<P
>* reloc
= relocs
; reloc
< relocsEnd
; ++reloc
) {
4547 if ( reloc
->r_extern() ) {
4548 // only expect external relocs on some colummns
4549 if ( (reloc
->r_address() % sizeof(macho_compact_unwind_entry
<P
>)) == macho_compact_unwind_entry
<P
>::personalityFieldOffset() ) {
4550 uint32_t entryIndex
= reloc
->r_address() / sizeof(macho_compact_unwind_entry
<P
>);
4551 array
[entryIndex
].personality
= this->personalityName(parser
, reloc
);
4553 else if ( (reloc
->r_address() % sizeof(macho_compact_unwind_entry
<P
>)) == macho_compact_unwind_entry
<P
>::lsdaFieldOffset() ) {
4554 uint32_t entryIndex
= reloc
->r_address() / sizeof(macho_compact_unwind_entry
<P
>);
4555 const macho_nlist
<P
>& lsdaSym
= parser
.symbolFromIndex(reloc
->r_symbolnum());
4556 if ( (lsdaSym
.n_type() & N_TYPE
) == N_SECT
)
4557 array
[entryIndex
].lsdaAddress
= lsdaSym
.n_value();
4559 warning("unexpected extern relocation to lsda in __compact_unwind section");
4561 else if ( (reloc
->r_address() % sizeof(macho_compact_unwind_entry
<P
>)) == macho_compact_unwind_entry
<P
>::codeStartFieldOffset() ) {
4562 uint32_t entryIndex
= reloc
->r_address() / sizeof(macho_compact_unwind_entry
<P
>);
4563 array
[entryIndex
].functionSymbolIndex
= reloc
->r_symbolnum();
4564 array
[entryIndex
].functionStartAddress
+= parser
.symbolFromIndex(reloc
->r_symbolnum()).n_value();
4567 warning("unexpected extern relocation in __compact_unwind section");
4571 if ( (reloc
->r_address() % sizeof(macho_compact_unwind_entry
<P
>)) == macho_compact_unwind_entry
<P
>::personalityFieldOffset() ) {
4572 uint32_t entryIndex
= reloc
->r_address() / sizeof(macho_compact_unwind_entry
<P
>);
4573 array
[entryIndex
].personality
= this->personalityName(parser
, reloc
);
4578 // sort array by function start address so unwind infos will be contiguous for a given function
4579 ::qsort(array
, cnt
, sizeof(Info
), infoSorter
);
4582 template <typename A
>
4583 uint32_t CUSection
<A
>::count()
4585 const macho_section
<P
>* machoSect
= this->machoSection();
4586 if ( (machoSect
->size() % sizeof(macho_compact_unwind_entry
<P
>)) != 0 )
4587 throw "malformed __LD,__compact_unwind section, bad length";
4589 return machoSect
->size() / sizeof(macho_compact_unwind_entry
<P
>);
4592 template <typename A
>
4593 void CUSection
<A
>::makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
& cus
)
4595 Info
* const arrayStart
= cus
.cuArray
;
4596 Info
* const arrayEnd
= &cus
.cuArray
[cus
.cuCount
];
4597 for (Info
* info
=arrayStart
; info
< arrayEnd
; ++info
) {
4598 // find function atom from address
4599 info
->function
= parser
.findAtomByAddress(info
->functionStartAddress
);
4600 // find lsda atom from address
4601 if ( info
->lsdaAddress
!= 0 ) {
4602 info
->lsda
= parser
.findAtomByAddress(info
->lsdaAddress
);
4603 // add lsda subordinate
4604 typename Parser
<A
>::SourceLocation
src(info
->function
, info
->functionStartAddress
- info
->function
->objectAddress());
4605 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindNoneGroupSubordinateLSDA
, info
->lsda
);
4607 if ( info
->personality
!= NULL
) {
4608 // add personality subordinate
4609 typename Parser
<A
>::SourceLocation
src(info
->function
, info
->functionStartAddress
- info
->function
->objectAddress());
4610 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindNoneGroupSubordinatePersonality
, false, info
->personality
);
4616 template <typename A
>
4617 SymboledSection
<A
>::SymboledSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
4618 : Section
<A
>(f
, s
), _type(ld::Atom::typeUnclassified
)
4620 switch ( s
->flags() & SECTION_TYPE
) {
4622 _type
= ld::Atom::typeZeroFill
;
4624 case S_MOD_INIT_FUNC_POINTERS
:
4625 _type
= ld::Atom::typeInitializerPointers
;
4627 case S_MOD_TERM_FUNC_POINTERS
:
4628 _type
= ld::Atom::typeTerminatorPointers
;
4630 case S_THREAD_LOCAL_VARIABLES
:
4631 _type
= ld::Atom::typeTLV
;
4633 case S_THREAD_LOCAL_ZEROFILL
:
4634 _type
= ld::Atom::typeTLVZeroFill
;
4636 case S_THREAD_LOCAL_REGULAR
:
4637 _type
= ld::Atom::typeTLVInitialValue
;
4639 case S_THREAD_LOCAL_INIT_FUNCTION_POINTERS
:
4640 _type
= ld::Atom::typeTLVInitializerPointers
;
4643 if ( strncmp(s
->sectname(), "__gcc_except_tab", 16) == 0 )
4644 _type
= ld::Atom::typeLSDA
;
4645 else if ( this->type() == ld::Section::typeInitializerPointers
)
4646 _type
= ld::Atom::typeInitializerPointers
;
4652 template <typename A
>
4653 bool SymboledSection
<A
>::dontDeadStrip()
4656 case ld::Atom::typeInitializerPointers
:
4657 case ld::Atom::typeTerminatorPointers
:
4660 // model an object file without MH_SUBSECTIONS_VIA_SYMBOLS as one in which nothing can be dead stripped
4661 if ( ! this->_file
.canScatterAtoms() )
4664 return Section
<A
>::dontDeadStrip();
4670 template <typename A
>
4671 uint32_t SymboledSection
<A
>::computeAtomCount(class Parser
<A
>& parser
,
4672 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
4673 const struct Parser
<A
>::CFI_CU_InfoArrays
&)
4675 const pint_t startAddr
= this->_machOSection
->addr();
4676 const pint_t endAddr
= startAddr
+ this->_machOSection
->size();
4677 const uint32_t sectNum
= this->sectionNum(parser
);
4682 const macho_nlist
<P
>* sym
;
4683 while ( it
.next(parser
, *this, sectNum
, startAddr
, endAddr
, &addr
, &size
, &sym
) ) {
4686 //fprintf(stderr, "computeAtomCount(%s,%s) => %d\n", this->segmentName(), this->sectionName(), count);
4690 template <typename A
>
4691 uint32_t SymboledSection
<A
>::appendAtoms(class Parser
<A
>& parser
, uint8_t* p
,
4692 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
4693 const struct Parser
<A
>::CFI_CU_InfoArrays
&)
4695 this->_beginAtoms
= (Atom
<A
>*)p
;
4697 //fprintf(stderr, "SymboledSection::appendAtoms() in section %s\n", this->_machOSection->sectname());
4698 const pint_t startAddr
= this->_machOSection
->addr();
4699 const pint_t endAddr
= startAddr
+ this->_machOSection
->size();
4700 const uint32_t sectNum
= this->sectionNum(parser
);
4705 const macho_nlist
<P
>* label
;
4706 while ( it
.next(parser
, *this, sectNum
, startAddr
, endAddr
, &addr
, &size
, &label
) ) {
4707 Atom
<A
>* allocatedSpace
= (Atom
<A
>*)p
;
4708 // is break because of label or CFI?
4709 if ( label
!= NULL
) {
4710 // The size is computed based on the address of the next label (or the end of the section for the last label)
4711 // If there are two labels at the same address, we want them one to be an alias of the other.
4712 // If the label is at the end of a section, it is has zero size, but is not an alias
4713 const bool isAlias
= ( (size
== 0) && (addr
< endAddr
) );
4714 new (allocatedSpace
) Atom
<A
>(*this, parser
, *label
, size
, isAlias
);
4716 this->_hasAliases
= true;
4719 ld::Atom::SymbolTableInclusion inclusion
= ld::Atom::symbolTableNotIn
;
4720 ld::Atom::ContentType ctype
= this->contentType();
4721 if ( ctype
== ld::Atom::typeLSDA
)
4722 inclusion
= ld::Atom::symbolTableInWithRandomAutoStripLabel
;
4723 new (allocatedSpace
) Atom
<A
>(*this, "anon", addr
, size
, ld::Atom::definitionRegular
, ld::Atom::combineNever
,
4724 ld::Atom::scopeTranslationUnit
, ctype
, inclusion
,
4725 this->dontDeadStrip(), false, false, this->alignmentForAddress(addr
));
4727 p
+= sizeof(Atom
<A
>);
4731 this->_endAtoms
= (Atom
<A
>*)p
;
4737 ld::Atom::SymbolTableInclusion ImplicitSizeSection
<arm64
>::symbolTableInclusion()
4739 return ld::Atom::symbolTableInWithRandomAutoStripLabel
;
4742 template <typename A
>
4743 ld::Atom::SymbolTableInclusion ImplicitSizeSection
<A
>::symbolTableInclusion()
4745 return ld::Atom::symbolTableNotIn
;
4749 template <typename A
>
4750 uint32_t ImplicitSizeSection
<A
>::computeAtomCount(class Parser
<A
>& parser
,
4751 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
4752 const struct Parser
<A
>::CFI_CU_InfoArrays
&)
4755 const macho_section
<P
>* sect
= this->machoSection();
4756 const pint_t startAddr
= sect
->addr();
4757 const pint_t endAddr
= startAddr
+ sect
->size();
4758 for (pint_t addr
= startAddr
; addr
< endAddr
; addr
+= elementSizeAtAddress(addr
) ) {
4759 if ( useElementAt(parser
, it
, addr
) )
4762 if ( it
.fileHasOverlappingSymbols
&& (sect
->size() != 0) && (this->combine(parser
, startAddr
) == ld::Atom::combineByNameAndContent
) ) {
4763 // if there are multiple labels in this section for the same address, then clone them into multi atoms
4764 pint_t prevSymbolAddr
= (pint_t
)(-1);
4765 uint8_t prevSymbolSectNum
= 0;
4766 bool prevIgnore
= false;
4767 for(uint32_t i
=0; i
< it
.sortedSymbolCount
; ++i
) {
4768 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(it
.sortedSymbolIndexes
[i
]);
4769 const pint_t symbolAddr
= sym
.n_value();
4770 const uint8_t symbolSectNum
= sym
.n_sect();
4771 const bool ignore
= this->ignoreLabel(parser
.nameFromSymbol(sym
));
4772 if ( !ignore
&& !prevIgnore
&& (symbolAddr
== prevSymbolAddr
) && (prevSymbolSectNum
== symbolSectNum
) && (symbolSectNum
== this->sectionNum(parser
)) ) {
4775 prevSymbolAddr
= symbolAddr
;
4776 prevSymbolSectNum
= symbolSectNum
;
4777 prevIgnore
= ignore
;
4783 template <typename A
>
4784 uint32_t ImplicitSizeSection
<A
>::appendAtoms(class Parser
<A
>& parser
, uint8_t* p
,
4785 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
4786 const struct Parser
<A
>::CFI_CU_InfoArrays
&)
4788 this->_beginAtoms
= (Atom
<A
>*)p
;
4790 const macho_section
<P
>* sect
= this->machoSection();
4791 const pint_t startAddr
= sect
->addr();
4792 const pint_t endAddr
= startAddr
+ sect
->size();
4793 const uint32_t sectNum
= this->sectionNum(parser
);
4794 //fprintf(stderr, "ImplicitSizeSection::appendAtoms() in section %s\n", sect->sectname());
4798 const macho_nlist
<P
>* foundLabel
;
4799 Atom
<A
>* allocatedSpace
;
4800 while ( it
.next(parser
, *this, sectNum
, startAddr
, endAddr
, &foundAddr
, &size
, &foundLabel
) ) {
4801 if ( foundLabel
!= NULL
) {
4803 pint_t labeledAtomSize
= this->elementSizeAtAddress(foundAddr
);
4804 allocatedSpace
= (Atom
<A
>*)p
;
4805 if ( this->ignoreLabel(parser
.nameFromSymbol(*foundLabel
)) ) {
4807 // <rdar://problem/10018737>
4808 // a size of zero means there is another label at same location
4809 // and we are supposed to ignore this label
4813 //fprintf(stderr, " 0x%08llX make annon, size=%lld\n", (uint64_t)foundAddr, (uint64_t)size);
4814 new (allocatedSpace
) Atom
<A
>(*this, this->unlabeledAtomName(parser
, foundAddr
), foundAddr
,
4815 this->elementSizeAtAddress(foundAddr
), this->definition(),
4816 this->combine(parser
, foundAddr
), this->scopeAtAddress(parser
, foundAddr
),
4817 this->contentType(), this->symbolTableInclusion(),
4818 this->dontDeadStrip(), false, false, this->alignmentForAddress(foundAddr
));
4822 // make named atom for label
4823 //fprintf(stderr, " 0x%08llX make labeled\n", (uint64_t)foundAddr);
4824 new (allocatedSpace
) Atom
<A
>(*this, parser
, *foundLabel
, labeledAtomSize
);
4828 p
+= sizeof(Atom
<A
>);
4829 foundAddr
+= labeledAtomSize
;
4830 size
-= labeledAtomSize
;
4833 // some number of anonymous atoms
4834 for (pint_t addr
= foundAddr
; addr
< (foundAddr
+size
); addr
+= elementSizeAtAddress(addr
) ) {
4835 // make anon atoms for area before label
4836 if ( this->useElementAt(parser
, it
, addr
) ) {
4837 //fprintf(stderr, " 0x%08llX make annon, size=%lld\n", (uint64_t)addr, (uint64_t)elementSizeAtAddress(addr));
4838 allocatedSpace
= (Atom
<A
>*)p
;
4839 new (allocatedSpace
) Atom
<A
>(*this, this->unlabeledAtomName(parser
, addr
), addr
, this->elementSizeAtAddress(addr
),
4840 this->definition(), this->combine(parser
, addr
), this->scopeAtAddress(parser
, addr
),
4841 this->contentType(), this->symbolTableInclusion(),
4842 this->dontDeadStrip(), false, false, this->alignmentForAddress(addr
));
4844 p
+= sizeof(Atom
<A
>);
4849 this->_endAtoms
= (Atom
<A
>*)p
;
4855 template <typename A
>
4856 unsigned long Literal4Section
<A
>::contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
4858 const uint32_t* literalContent
= (uint32_t*)atom
->contentPointer();
4859 return *literalContent
;
4862 template <typename A
>
4863 bool Literal4Section
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
4864 const ld::IndirectBindingTable
& ind
) const
4866 assert(this->type() == rhs
.section().type());
4867 const uint32_t* literalContent
= (uint32_t*)atom
->contentPointer();
4869 const Atom
<A
>* rhsAtom
= dynamic_cast<const Atom
<A
>*>(&rhs
);
4870 assert(rhsAtom
!= NULL
);
4871 if ( rhsAtom
!= NULL
) {
4872 const uint32_t* rhsLiteralContent
= (uint32_t*)rhsAtom
->contentPointer();
4873 return (*literalContent
== *rhsLiteralContent
);
4879 template <typename A
>
4880 unsigned long Literal8Section
<A
>::contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
4883 const uint64_t* literalContent
= (uint64_t*)atom
->contentPointer();
4884 return *literalContent
;
4886 unsigned long hash
= 5381;
4887 const uint8_t* byteContent
= atom
->contentPointer();
4888 for (int i
=0; i
< 8; ++i
) {
4889 hash
= hash
* 33 + byteContent
[i
];
4895 template <typename A
>
4896 bool Literal8Section
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
4897 const ld::IndirectBindingTable
& ind
) const
4899 if ( rhs
.section().type() != ld::Section::typeLiteral8
)
4901 assert(this->type() == rhs
.section().type());
4902 const uint64_t* literalContent
= (uint64_t*)atom
->contentPointer();
4904 const Atom
<A
>* rhsAtom
= dynamic_cast<const Atom
<A
>*>(&rhs
);
4905 assert(rhsAtom
!= NULL
);
4906 if ( rhsAtom
!= NULL
) {
4907 const uint64_t* rhsLiteralContent
= (uint64_t*)rhsAtom
->contentPointer();
4908 return (*literalContent
== *rhsLiteralContent
);
4914 template <typename A
>
4915 unsigned long Literal16Section
<A
>::contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
4917 unsigned long hash
= 5381;
4918 const uint8_t* byteContent
= atom
->contentPointer();
4919 for (int i
=0; i
< 16; ++i
) {
4920 hash
= hash
* 33 + byteContent
[i
];
4925 template <typename A
>
4926 bool Literal16Section
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
4927 const ld::IndirectBindingTable
& ind
) const
4929 if ( rhs
.section().type() != ld::Section::typeLiteral16
)
4931 assert(this->type() == rhs
.section().type());
4932 const uint64_t* literalContent
= (uint64_t*)atom
->contentPointer();
4934 const Atom
<A
>* rhsAtom
= dynamic_cast<const Atom
<A
>*>(&rhs
);
4935 assert(rhsAtom
!= NULL
);
4936 if ( rhsAtom
!= NULL
) {
4937 const uint64_t* rhsLiteralContent
= (uint64_t*)rhsAtom
->contentPointer();
4938 return ((literalContent
[0] == rhsLiteralContent
[0]) && (literalContent
[1] == rhsLiteralContent
[1]));
4945 template <typename A
>
4946 typename
A::P::uint_t CStringSection
<A
>::elementSizeAtAddress(pint_t addr
)
4948 const macho_section
<P
>* sect
= this->machoSection();
4949 const char* stringContent
= (char*)(this->file().fileContent() + sect
->offset() + addr
- sect
->addr());
4950 return strlen(stringContent
) + 1;
4953 template <typename A
>
4954 bool CStringSection
<A
>::useElementAt(Parser
<A
>& parser
, struct Parser
<A
>::LabelAndCFIBreakIterator
& it
, pint_t addr
)
4959 template <typename A
>
4960 bool CStringSection
<A
>::ignoreLabel(const char* label
) const
4962 return (label
[0] == 'L') || (label
[0] == 'l');
4966 template <typename A
>
4967 Atom
<A
>* CStringSection
<A
>::findAtomByAddress(pint_t addr
)
4969 Atom
<A
>* result
= this->findContentAtomByAddress(addr
, this->_beginAtoms
, this->_endAtoms
);
4973 template <typename A
>
4974 unsigned long CStringSection
<A
>::contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
4976 unsigned long hash
= 5381;
4977 const char* stringContent
= (char*)atom
->contentPointer();
4978 for (const char* s
= stringContent
; *s
!= '\0'; ++s
) {
4979 hash
= hash
* 33 + *s
;
4985 template <typename A
>
4986 bool CStringSection
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
4987 const ld::IndirectBindingTable
& ind
) const
4989 if ( rhs
.section().type() != ld::Section::typeCString
)
4991 assert(this->type() == rhs
.section().type());
4992 assert(strcmp(this->sectionName(), rhs
.section().sectionName())== 0);
4993 assert(strcmp(this->segmentName(), rhs
.section().segmentName())== 0);
4994 const char* stringContent
= (char*)atom
->contentPointer();
4996 const Atom
<A
>* rhsAtom
= dynamic_cast<const Atom
<A
>*>(&rhs
);
4997 assert(rhsAtom
!= NULL
);
4998 if ( rhsAtom
!= NULL
) {
4999 if ( atom
->_size
!= rhsAtom
->_size
)
5001 const char* rhsStringContent
= (char*)rhsAtom
->contentPointer();
5002 return (strcmp(stringContent
, rhsStringContent
) == 0);
5009 ld::Fixup::Kind NonLazyPointerSection
<x86
>::fixupKind()
5011 return ld::Fixup::kindStoreLittleEndian32
;
5015 ld::Fixup::Kind NonLazyPointerSection
<arm
>::fixupKind()
5017 return ld::Fixup::kindStoreLittleEndian32
;
5021 ld::Fixup::Kind NonLazyPointerSection
<arm64
>::fixupKind()
5023 return ld::Fixup::kindStoreLittleEndian64
;
5028 void NonLazyPointerSection
<x86_64
>::makeFixups(class Parser
<x86_64
>& parser
, const struct Parser
<x86_64
>::CFI_CU_InfoArrays
&)
5030 assert(0 && "x86_64 should not have non-lazy-pointer sections in .o files");
5033 template <typename A
>
5034 void NonLazyPointerSection
<A
>::makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
&)
5036 // add references for each NLP atom based on indirect symbol table
5037 const macho_section
<P
>* sect
= this->machoSection();
5038 const pint_t endAddr
= sect
->addr() + sect
->size();
5039 for( pint_t addr
= sect
->addr(); addr
< endAddr
; addr
+= sizeof(pint_t
)) {
5040 typename Parser
<A
>::SourceLocation src
;
5041 typename Parser
<A
>::TargetDesc target
;
5042 src
.atom
= this->findAtomByAddress(addr
);
5043 src
.offsetInAtom
= 0;
5044 uint32_t symIndex
= parser
.symbolIndexFromIndirectSectionAddress(addr
, sect
);
5047 target
.weakImport
= false;
5049 if ( symIndex
== INDIRECT_SYMBOL_LOCAL
) {
5050 // use direct reference for local symbols
5051 const pint_t
* nlpContent
= (pint_t
*)(this->file().fileContent() + sect
->offset() + addr
- sect
->addr());
5052 pint_t targetAddr
= P::getP(*nlpContent
);
5053 target
.atom
= parser
.findAtomByAddress(targetAddr
);
5054 target
.weakImport
= false;
5055 target
.addend
= (targetAddr
- target
.atom
->objectAddress());
5056 // <rdar://problem/8385011> if pointer to thumb function, mask of thumb bit (not an addend of +1)
5057 if ( target
.atom
->isThumb() )
5058 target
.addend
&= (-2);
5059 assert(src
.atom
->combine() == ld::Atom::combineNever
);
5062 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(symIndex
);
5063 // use direct reference for local symbols
5064 if ( ((sym
.n_type() & N_TYPE
) == N_SECT
) && ((sym
.n_type() & N_EXT
) == 0) ) {
5065 parser
.findTargetFromAddressAndSectionNum(sym
.n_value(), sym
.n_sect(), target
);
5066 assert(src
.atom
->combine() == ld::Atom::combineNever
);
5069 target
.name
= parser
.nameFromSymbol(sym
);
5070 target
.weakImport
= parser
.weakImportFromSymbol(sym
);
5071 assert(src
.atom
->combine() == ld::Atom::combineByNameAndReferences
);
5074 parser
.addFixups(src
, this->fixupKind(), target
);
5078 template <typename A
>
5079 ld::Atom::Combine NonLazyPointerSection
<A
>::combine(Parser
<A
>& parser
, pint_t addr
)
5081 const macho_section
<P
>* sect
= this->machoSection();
5082 uint32_t symIndex
= parser
.symbolIndexFromIndirectSectionAddress(addr
, sect
);
5083 if ( symIndex
== INDIRECT_SYMBOL_LOCAL
)
5084 return ld::Atom::combineNever
;
5086 // don't coalesce non-lazy-pointers to local symbols
5087 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(symIndex
);
5088 if ( ((sym
.n_type() & N_TYPE
) == N_SECT
) && ((sym
.n_type() & N_EXT
) == 0) )
5089 return ld::Atom::combineNever
;
5091 return ld::Atom::combineByNameAndReferences
;
5094 template <typename A
>
5095 const char* NonLazyPointerSection
<A
>::targetName(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
)
5097 assert(atom
->combine() == ld::Atom::combineByNameAndReferences
);
5098 assert(atom
->fixupCount() == 1);
5099 ld::Fixup::iterator fit
= atom
->fixupsBegin();
5100 const char* name
= NULL
;
5101 switch ( fit
->binding
) {
5102 case ld::Fixup::bindingByNameUnbound
:
5105 case ld::Fixup::bindingByContentBound
:
5106 name
= fit
->u
.target
->name();
5108 case ld::Fixup::bindingsIndirectlyBound
:
5109 name
= ind
.indirectName(fit
->u
.bindingIndex
);
5114 assert(name
!= NULL
);
5118 template <typename A
>
5119 unsigned long NonLazyPointerSection
<A
>::contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
5121 assert(atom
->combine() == ld::Atom::combineByNameAndReferences
);
5122 unsigned long hash
= 9508;
5123 for (const char* s
= this->targetName(atom
, ind
); *s
!= '\0'; ++s
) {
5124 hash
= hash
* 33 + *s
;
5129 template <typename A
>
5130 bool NonLazyPointerSection
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
5131 const ld::IndirectBindingTable
& indirectBindingTable
) const
5133 if ( rhs
.section().type() != ld::Section::typeNonLazyPointer
)
5135 assert(this->type() == rhs
.section().type());
5136 // there can be many non-lazy pointer in different section names
5137 // we only want to coalesce in same section name
5138 if ( *this != rhs
.section() )
5140 const Atom
<A
>* rhsAtom
= dynamic_cast<const Atom
<A
>*>(&rhs
);
5141 assert(rhsAtom
!= NULL
);
5142 const char* thisName
= this->targetName(atom
, indirectBindingTable
);
5143 const char* rhsName
= this->targetName(rhsAtom
, indirectBindingTable
);
5144 return (strcmp(thisName
, rhsName
) == 0);
5147 template <typename A
>
5148 ld::Atom::Scope NonLazyPointerSection
<A
>::scopeAtAddress(Parser
<A
>& parser
, pint_t addr
)
5150 const macho_section
<P
>* sect
= this->machoSection();
5151 uint32_t symIndex
= parser
.symbolIndexFromIndirectSectionAddress(addr
, sect
);
5152 if ( symIndex
== INDIRECT_SYMBOL_LOCAL
)
5153 return ld::Atom::scopeTranslationUnit
;
5155 return ld::Atom::scopeLinkageUnit
;
5159 template <typename A
>
5160 const uint8_t* CFStringSection
<A
>::targetContent(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
,
5161 ContentType
* ct
, unsigned int* count
)
5163 *ct
= contentUnknown
;
5164 for (ld::Fixup::iterator fit
=atom
->fixupsBegin(), end
=atom
->fixupsEnd(); fit
!= end
; ++fit
) {
5165 const ld::Atom
* targetAtom
= NULL
;
5166 switch ( fit
->binding
) {
5167 case ld::Fixup::bindingByNameUnbound
:
5168 // ignore reference to ___CFConstantStringClassReference
5169 // we are just looking for reference to backing string data
5170 assert(fit
->offsetInAtom
== 0);
5171 assert(strcmp(fit
->u
.name
, "___CFConstantStringClassReference") == 0);
5173 case ld::Fixup::bindingDirectlyBound
:
5174 case ld::Fixup::bindingByContentBound
:
5175 targetAtom
= fit
->u
.target
;
5177 case ld::Fixup::bindingsIndirectlyBound
:
5178 targetAtom
= ind
.indirectAtom(fit
->u
.bindingIndex
);
5181 assert(0 && "bad binding type");
5183 assert(targetAtom
!= NULL
);
5184 const Atom
<A
>* target
= dynamic_cast<const Atom
<A
>*>(targetAtom
);
5185 if ( targetAtom
->section().type() == ld::Section::typeCString
) {
5187 *count
= targetAtom
->size();
5189 else if ( targetAtom
->section().type() == ld::Section::typeUTF16Strings
) {
5191 *count
= (targetAtom
->size()+1)/2; // round up incase of buggy compiler that has only one trailing zero byte
5193 assert(target
!= NULL
);
5194 return target
->contentPointer();
5200 template <typename A
>
5201 unsigned long CFStringSection
<A
>::contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
5203 // base hash of CFString on hash of cstring it wraps
5206 unsigned int charCount
;
5207 const uint8_t* content
= this->targetContent(atom
, ind
, &cType
, &charCount
);
5211 for (const char* s
= (char*)content
; *s
!= '\0'; ++s
) {
5212 hash
= hash
* 33 + *s
;
5217 --charCount
; // don't add last 0x0000 to hash because some buggy compilers only have trailing single byte
5218 for (const uint16_t* s
= (uint16_t*)content
; charCount
> 0; ++s
, --charCount
) {
5219 hash
= hash
* 1025 + *s
;
5222 case contentUnknown
:
5229 template <typename A
>
5230 bool CFStringSection
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
5231 const ld::IndirectBindingTable
& indirectBindingTable
) const
5235 if ( rhs
.section().type() != ld::Section::typeCFString
)
5237 assert(this->type() == rhs
.section().type());
5238 assert(strcmp(this->sectionName(), "__cfstring") == 0);
5240 ContentType thisType
;
5241 unsigned int charCount
;
5242 const uint8_t* cstringContent
= this->targetContent(atom
, indirectBindingTable
, &thisType
, &charCount
);
5243 ContentType rhsType
;
5244 const Atom
<A
>* rhsAtom
= dynamic_cast<const Atom
<A
>*>(&rhs
);
5245 assert(rhsAtom
!= NULL
);
5246 unsigned int rhsCharCount
;
5247 const uint8_t* rhsStringContent
= this->targetContent(rhsAtom
, indirectBindingTable
, &rhsType
, &rhsCharCount
);
5249 if ( thisType
!= rhsType
)
5252 // no need to compare content of pointers are already the same
5253 if ( cstringContent
== rhsStringContent
)
5256 // no need to compare content if size is different
5257 if ( charCount
!= rhsCharCount
)
5260 switch ( thisType
) {
5262 return (strcmp((char*)cstringContent
, (char*)rhsStringContent
) == 0);
5265 const uint16_t* cstringContent16
= (uint16_t*)cstringContent
;
5266 const uint16_t* rhsStringContent16
= (uint16_t*)rhsStringContent
;
5267 for (unsigned int i
= 0; i
< charCount
; ++i
) {
5268 if ( cstringContent16
[i
] != rhsStringContent16
[i
] )
5273 case contentUnknown
:
5280 template <typename A
>
5281 typename
A::P::uint_t ObjC1ClassSection
<A
>::elementSizeAtAddress(pint_t addr
)
5283 // nominal size for each class is 48 bytes, but sometimes the compiler
5284 // over aligns and there is padding after class data
5285 const macho_section
<P
>* sct
= this->machoSection();
5286 uint32_t align
= 1 << sct
->align();
5287 uint32_t size
= ((12 * sizeof(pint_t
)) + align
-1) & (-align
);
5291 template <typename A
>
5292 const char* ObjC1ClassSection
<A
>::unlabeledAtomName(Parser
<A
>& parser
, pint_t addr
)
5294 // 8-bytes into class object is pointer to class name
5295 const macho_section
<P
>* sct
= this->machoSection();
5296 uint32_t classObjcFileOffset
= sct
->offset() - sct
->addr() + addr
;
5297 const uint8_t* mappedFileContent
= this->file().fileContent();
5298 pint_t nameAddr
= P::getP(*((pint_t
*)(mappedFileContent
+classObjcFileOffset
+2*sizeof(pint_t
))));
5300 // find section containing string address to get string bytes
5301 const macho_section
<P
>* const sections
= parser
.firstMachOSection();
5302 const uint32_t sectionCount
= parser
.machOSectionCount();
5303 for (uint32_t i
=0; i
< sectionCount
; ++i
) {
5304 const macho_section
<P
>* aSect
= §ions
[i
];
5305 if ( (aSect
->addr() <= nameAddr
) && (nameAddr
< (aSect
->addr()+aSect
->size())) ) {
5306 assert((aSect
->flags() & SECTION_TYPE
) == S_CSTRING_LITERALS
);
5307 uint32_t nameFileOffset
= aSect
->offset() - aSect
->addr() + nameAddr
;
5308 const char* name
= (char*)mappedFileContent
+ nameFileOffset
;
5309 // spin through symbol table to find absolute symbol corresponding to this class
5310 for (uint32_t s
=0; s
< parser
.symbolCount(); ++s
) {
5311 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(s
);
5312 if ( (sym
.n_type() & N_TYPE
) != N_ABS
)
5314 const char* absName
= parser
.nameFromSymbol(sym
);
5315 if ( strncmp(absName
, ".objc_class_name_", 17) == 0 ) {
5316 if ( strcmp(&absName
[17], name
) == 0 )
5320 assert(0 && "obj class name not found in symbol table");
5323 assert(0 && "obj class name not found");
5324 return "unknown objc class";
5328 template <typename A
>
5329 const char* ObjC2ClassRefsSection
<A
>::targetClassName(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
5331 assert(atom
->fixupCount() == 1);
5332 ld::Fixup::iterator fit
= atom
->fixupsBegin();
5333 const char* className
= NULL
;
5334 switch ( fit
->binding
) {
5335 case ld::Fixup::bindingByNameUnbound
:
5336 className
= fit
->u
.name
;
5338 case ld::Fixup::bindingDirectlyBound
:
5339 case ld::Fixup::bindingByContentBound
:
5340 className
= fit
->u
.target
->name();
5342 case ld::Fixup::bindingsIndirectlyBound
:
5343 className
= ind
.indirectName(fit
->u
.bindingIndex
);
5346 assert(0 && "unsupported binding in objc2 class ref section");
5348 assert(className
!= NULL
);
5353 template <typename A
>
5354 unsigned long ObjC2ClassRefsSection
<A
>::contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
5356 unsigned long hash
= 978;
5357 for (const char* s
= targetClassName(atom
, ind
); *s
!= '\0'; ++s
) {
5358 hash
= hash
* 33 + *s
;
5363 template <typename A
>
5364 bool ObjC2ClassRefsSection
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
5365 const ld::IndirectBindingTable
& indirectBindingTable
) const
5367 assert(this->type() == rhs
.section().type());
5368 const Atom
<A
>* rhsAtom
= dynamic_cast<const Atom
<A
>*>(&rhs
);
5369 assert(rhsAtom
!= NULL
);
5370 const char* thisClassName
= targetClassName(atom
, indirectBindingTable
);
5371 const char* rhsClassName
= targetClassName(rhsAtom
, indirectBindingTable
);
5372 return (strcmp(thisClassName
, rhsClassName
) == 0);
5376 template <typename A
>
5377 const char* Objc1ClassReferences
<A
>::targetCString(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
5379 assert(atom
->fixupCount() == 2);
5380 ld::Fixup::iterator fit
= atom
->fixupsBegin();
5381 if ( fit
->kind
== ld::Fixup::kindSetTargetAddress
)
5383 const ld::Atom
* targetAtom
= NULL
;
5384 switch ( fit
->binding
) {
5385 case ld::Fixup::bindingByContentBound
:
5386 targetAtom
= fit
->u
.target
;
5388 case ld::Fixup::bindingsIndirectlyBound
:
5389 targetAtom
= ind
.indirectAtom(fit
->u
.bindingIndex
);
5390 if ( targetAtom
== NULL
) {
5391 fprintf(stderr
, "missing target named %s\n", ind
.indirectName(fit
->u
.bindingIndex
));
5397 assert(targetAtom
!= NULL
);
5398 const Atom
<A
>* target
= dynamic_cast<const Atom
<A
>*>(targetAtom
);
5399 assert(target
!= NULL
);
5400 return (char*)target
->contentPointer();
5404 template <typename A
>
5405 const char* PointerToCStringSection
<A
>::targetCString(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
5407 assert(atom
->fixupCount() == 1);
5408 ld::Fixup::iterator fit
= atom
->fixupsBegin();
5409 const ld::Atom
* targetAtom
= NULL
;
5410 switch ( fit
->binding
) {
5411 case ld::Fixup::bindingByContentBound
:
5412 targetAtom
= fit
->u
.target
;
5414 case ld::Fixup::bindingsIndirectlyBound
:
5415 targetAtom
= ind
.indirectAtom(fit
->u
.bindingIndex
);
5417 case ld::Fixup::bindingDirectlyBound
:
5418 targetAtom
= fit
->u
.target
;
5421 assert(0 && "unsupported reference to selector");
5423 assert(targetAtom
!= NULL
);
5424 const Atom
<A
>* target
= dynamic_cast<const Atom
<A
>*>(targetAtom
);
5425 assert(target
!= NULL
);
5426 assert(target
->contentType() == ld::Atom::typeCString
);
5427 return (char*)target
->contentPointer();
5430 template <typename A
>
5431 unsigned long PointerToCStringSection
<A
>::contentHash(const class Atom
<A
>* atom
,
5432 const ld::IndirectBindingTable
& indirectBindingTable
) const
5434 // make hash from section name and target cstring name
5435 unsigned long hash
= 123;
5436 for (const char* s
= this->sectionName(); *s
!= '\0'; ++s
) {
5437 hash
= hash
* 33 + *s
;
5439 for (const char* s
= this->targetCString(atom
, indirectBindingTable
); *s
!= '\0'; ++s
) {
5440 hash
= hash
* 33 + *s
;
5445 template <typename A
>
5446 bool PointerToCStringSection
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
5447 const ld::IndirectBindingTable
& indirectBindingTable
) const
5449 assert(this->type() == rhs
.section().type());
5450 // there can be pointers-to-cstrings in different section names
5451 // we only want to coalesce in same section name
5452 if ( *this != rhs
.section() )
5455 // get string content for this
5456 const char* cstringContent
= this->targetCString(atom
, indirectBindingTable
);
5457 const Atom
<A
>* rhsAtom
= dynamic_cast<const Atom
<A
>*>(&rhs
);
5458 assert(rhsAtom
!= NULL
);
5459 const char* rhsCstringContent
= this->targetCString(rhsAtom
, indirectBindingTable
);
5461 assert(cstringContent
!= NULL
);
5462 assert(rhsCstringContent
!= NULL
);
5463 return (strcmp(cstringContent
, rhsCstringContent
) == 0);
5468 template <typename A
>
5469 unsigned long UTF16StringSection
<A
>::contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
5471 unsigned long hash
= 5381;
5472 const uint16_t* stringContent
= (uint16_t*)atom
->contentPointer();
5473 // some buggy compilers end utf16 data with single byte, so don't use last word in hash computation
5474 unsigned int count
= (atom
->size()/2) - 1;
5475 for (const uint16_t* s
= stringContent
; count
> 0; ++s
, --count
) {
5476 hash
= hash
* 33 + *s
;
5481 template <typename A
>
5482 bool UTF16StringSection
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
5483 const ld::IndirectBindingTable
& ind
) const
5485 if ( rhs
.section().type() != ld::Section::typeUTF16Strings
)
5498 uint32_t Section
<x86_64
>::x86_64PcRelOffset(uint8_t r_type
)
5501 case X86_64_RELOC_SIGNED
:
5503 case X86_64_RELOC_SIGNED_1
:
5505 case X86_64_RELOC_SIGNED_2
:
5507 case X86_64_RELOC_SIGNED_4
:
5515 bool Section
<x86_64
>::addRelocFixup(class Parser
<x86_64
>& parser
, const macho_relocation_info
<P
>* reloc
)
5517 const macho_section
<P
>* sect
= this->machoSection();
5518 uint64_t srcAddr
= sect
->addr() + reloc
->r_address();
5519 Parser
<x86_64
>::SourceLocation src
;
5520 Parser
<x86_64
>::TargetDesc target
;
5521 Parser
<x86_64
>::TargetDesc toTarget
;
5522 src
.atom
= this->findAtomByAddress(srcAddr
);
5523 src
.offsetInAtom
= srcAddr
- src
.atom
->_objAddress
;
5524 const uint8_t* fixUpPtr
= file().fileContent() + sect
->offset() + reloc
->r_address();
5525 uint64_t contentValue
= 0;
5526 const macho_relocation_info
<x86_64::P
>* nextReloc
= &reloc
[1];
5527 bool result
= false;
5528 bool useDirectBinding
;
5529 switch ( reloc
->r_length() ) {
5531 contentValue
= *fixUpPtr
;
5534 contentValue
= (int64_t)(int16_t)E::get16(*((uint16_t*)fixUpPtr
));
5537 contentValue
= (int64_t)(int32_t)E::get32(*((uint32_t*)fixUpPtr
));
5540 contentValue
= E::get64(*((uint64_t*)fixUpPtr
));
5545 target
.weakImport
= false;
5547 if ( reloc
->r_extern() ) {
5548 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(reloc
->r_symbolnum());
5549 // use direct reference for local symbols
5550 if ( ((sym
.n_type() & N_TYPE
) == N_SECT
) && (((sym
.n_type() & N_EXT
) == 0) || (parser
.nameFromSymbol(sym
)[0] == 'L')) ) {
5551 parser
.findTargetFromAddressAndSectionNum(sym
.n_value(), sym
.n_sect(), target
);
5552 target
.addend
+= contentValue
;
5555 target
.name
= parser
.nameFromSymbol(sym
);
5556 target
.weakImport
= parser
.weakImportFromSymbol(sym
);
5557 target
.addend
= contentValue
;
5559 // cfstrings should always use direct reference to backing store
5560 if ( (this->type() == ld::Section::typeCFString
) && (src
.offsetInAtom
!= 0) ) {
5561 parser
.findTargetFromAddressAndSectionNum(sym
.n_value(), sym
.n_sect(), target
);
5562 target
.addend
= contentValue
;
5566 if ( reloc
->r_pcrel() )
5567 contentValue
+= srcAddr
+ x86_64PcRelOffset(reloc
->r_type());
5568 parser
.findTargetFromAddressAndSectionNum(contentValue
, reloc
->r_symbolnum(), target
);
5570 switch ( reloc
->r_type() ) {
5571 case X86_64_RELOC_UNSIGNED
:
5572 if ( reloc
->r_pcrel() )
5573 throw "pcrel and X86_64_RELOC_UNSIGNED not supported";
5574 switch ( reloc
->r_length() ) {
5577 throw "length < 2 and X86_64_RELOC_UNSIGNED not supported";
5579 parser
.addFixups(src
, ld::Fixup::kindStoreLittleEndian32
, target
);
5582 parser
.addFixups(src
, ld::Fixup::kindStoreLittleEndian64
, target
);
5586 case X86_64_RELOC_SIGNED
:
5587 case X86_64_RELOC_SIGNED_1
:
5588 case X86_64_RELOC_SIGNED_2
:
5589 case X86_64_RELOC_SIGNED_4
:
5590 if ( ! reloc
->r_pcrel() )
5591 throw "not pcrel and X86_64_RELOC_SIGNED* not supported";
5592 if ( reloc
->r_length() != 2 )
5593 throw "length != 2 and X86_64_RELOC_SIGNED* not supported";
5594 switch ( reloc
->r_type() ) {
5595 case X86_64_RELOC_SIGNED
:
5596 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel32
, target
);
5598 case X86_64_RELOC_SIGNED_1
:
5599 if ( reloc
->r_extern() )
5601 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel32_1
, target
);
5603 case X86_64_RELOC_SIGNED_2
:
5604 if ( reloc
->r_extern() )
5606 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel32_2
, target
);
5608 case X86_64_RELOC_SIGNED_4
:
5609 if ( reloc
->r_extern() )
5611 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel32_4
, target
);
5615 case X86_64_RELOC_BRANCH
:
5616 if ( ! reloc
->r_pcrel() )
5617 throw "not pcrel and X86_64_RELOC_BRANCH not supported";
5618 switch ( reloc
->r_length() ) {
5620 if ( (target
.name
!= NULL
) && (strncmp(target
.name
, "___dtrace_probe$", 16) == 0) ) {
5621 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindStoreX86DtraceCallSiteNop
, false, target
.name
);
5622 parser
.addDtraceExtraInfos(src
, &target
.name
[16]);
5624 else if ( (target
.name
!= NULL
) && (strncmp(target
.name
, "___dtrace_isenabled$", 20) == 0) ) {
5625 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindStoreX86DtraceIsEnableSiteClear
, false, target
.name
);
5626 parser
.addDtraceExtraInfos(src
, &target
.name
[20]);
5629 parser
.addFixups(src
, ld::Fixup::kindStoreX86BranchPCRel32
, target
);
5633 parser
.addFixups(src
, ld::Fixup::kindStoreX86BranchPCRel8
, target
);
5636 throwf("length=%d and X86_64_RELOC_BRANCH not supported", reloc
->r_length());
5639 case X86_64_RELOC_GOT
:
5640 if ( ! reloc
->r_extern() )
5641 throw "not extern and X86_64_RELOC_GOT not supported";
5642 if ( ! reloc
->r_pcrel() )
5643 throw "not pcrel and X86_64_RELOC_GOT not supported";
5644 if ( reloc
->r_length() != 2 )
5645 throw "length != 2 and X86_64_RELOC_GOT not supported";
5646 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel32GOT
, target
);
5648 case X86_64_RELOC_GOT_LOAD
:
5649 if ( ! reloc
->r_extern() )
5650 throw "not extern and X86_64_RELOC_GOT_LOAD not supported";
5651 if ( ! reloc
->r_pcrel() )
5652 throw "not pcrel and X86_64_RELOC_GOT_LOAD not supported";
5653 if ( reloc
->r_length() != 2 )
5654 throw "length != 2 and X86_64_RELOC_GOT_LOAD not supported";
5655 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel32GOTLoad
, target
);
5657 case X86_64_RELOC_SUBTRACTOR
:
5658 if ( reloc
->r_pcrel() )
5659 throw "X86_64_RELOC_SUBTRACTOR cannot be pc-relative";
5660 if ( reloc
->r_length() < 2 )
5661 throw "X86_64_RELOC_SUBTRACTOR must have r_length of 2 or 3";
5662 if ( !reloc
->r_extern() )
5663 throw "X86_64_RELOC_SUBTRACTOR must have r_extern=1";
5664 if ( nextReloc
->r_type() != X86_64_RELOC_UNSIGNED
)
5665 throw "X86_64_RELOC_SUBTRACTOR must be followed by X86_64_RELOC_UNSIGNED";
5667 if ( nextReloc
->r_pcrel() )
5668 throw "X86_64_RELOC_UNSIGNED following a X86_64_RELOC_SUBTRACTOR cannot be pc-relative";
5669 if ( nextReloc
->r_length() != reloc
->r_length() )
5670 throw "X86_64_RELOC_UNSIGNED following a X86_64_RELOC_SUBTRACTOR must have same r_length";
5671 if ( nextReloc
->r_extern() ) {
5672 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(nextReloc
->r_symbolnum());
5673 // use direct reference for local symbols
5674 if ( ((sym
.n_type() & N_TYPE
) == N_SECT
) && (((sym
.n_type() & N_EXT
) == 0) || (parser
.nameFromSymbol(sym
)[0] == 'L')) ) {
5675 parser
.findTargetFromAddressAndSectionNum(sym
.n_value(), sym
.n_sect(), toTarget
);
5676 toTarget
.addend
= contentValue
;
5677 useDirectBinding
= true;
5680 toTarget
.name
= parser
.nameFromSymbol(sym
);
5681 toTarget
.weakImport
= parser
.weakImportFromSymbol(sym
);
5682 toTarget
.addend
= contentValue
;
5683 useDirectBinding
= false;
5687 parser
.findTargetFromAddressAndSectionNum(contentValue
, nextReloc
->r_symbolnum(), toTarget
);
5688 useDirectBinding
= (toTarget
.atom
->scope() == ld::Atom::scopeTranslationUnit
);
5690 if ( useDirectBinding
)
5691 parser
.addFixup(src
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, toTarget
.atom
);
5693 parser
.addFixup(src
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, toTarget
.weakImport
, toTarget
.name
);
5694 parser
.addFixup(src
, ld::Fixup::k2of4
, ld::Fixup::kindAddAddend
, toTarget
.addend
);
5695 if ( target
.atom
== NULL
)
5696 parser
.addFixup(src
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractTargetAddress
, false, target
.name
);
5698 parser
.addFixup(src
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractTargetAddress
, target
.atom
);
5699 if ( reloc
->r_length() == 2 )
5700 parser
.addFixup(src
, ld::Fixup::k4of4
, ld::Fixup::kindStoreLittleEndian32
);
5702 parser
.addFixup(src
, ld::Fixup::k4of4
, ld::Fixup::kindStoreLittleEndian64
);
5704 case X86_64_RELOC_TLV
:
5705 if ( ! reloc
->r_extern() )
5706 throw "not extern and X86_64_RELOC_TLV not supported";
5707 if ( ! reloc
->r_pcrel() )
5708 throw "not pcrel and X86_64_RELOC_TLV not supported";
5709 if ( reloc
->r_length() != 2 )
5710 throw "length != 2 and X86_64_RELOC_TLV not supported";
5711 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel32TLVLoad
, target
);
5714 throwf("unknown relocation type %d", reloc
->r_type());
5722 bool Section
<x86
>::addRelocFixup(class Parser
<x86
>& parser
, const macho_relocation_info
<P
>* reloc
)
5724 const macho_section
<P
>* sect
= this->machoSection();
5726 const uint8_t* fixUpPtr
;
5727 uint32_t contentValue
= 0;
5728 ld::Fixup::Kind kind
= ld::Fixup::kindNone
;
5729 Parser
<x86
>::SourceLocation src
;
5730 Parser
<x86
>::TargetDesc target
;
5732 if ( (reloc
->r_address() & R_SCATTERED
) == 0 ) {
5733 srcAddr
= sect
->addr() + reloc
->r_address();
5734 src
.atom
= this->findAtomByAddress(srcAddr
);
5735 src
.offsetInAtom
= srcAddr
- src
.atom
->_objAddress
;
5736 fixUpPtr
= file().fileContent() + sect
->offset() + reloc
->r_address();
5737 switch ( reloc
->r_type() ) {
5738 case GENERIC_RELOC_VANILLA
:
5739 switch ( reloc
->r_length() ) {
5741 contentValue
= (int32_t)(int8_t)*fixUpPtr
;
5742 if ( reloc
->r_pcrel() ) {
5743 kind
= ld::Fixup::kindStoreX86BranchPCRel8
;
5744 contentValue
+= srcAddr
+ sizeof(uint8_t);
5747 throw "r_length=0 and r_pcrel=0 not supported";
5750 contentValue
= (int32_t)(int16_t)E::get16(*((uint16_t*)fixUpPtr
));
5751 if ( reloc
->r_pcrel() ) {
5752 kind
= ld::Fixup::kindStoreX86PCRel16
;
5753 contentValue
+= srcAddr
+ sizeof(uint16_t);
5756 kind
= ld::Fixup::kindStoreLittleEndian16
;
5759 contentValue
= E::get32(*((uint32_t*)fixUpPtr
));
5760 if ( reloc
->r_pcrel() ) {
5761 kind
= ld::Fixup::kindStoreX86BranchPCRel32
;
5762 contentValue
+= srcAddr
+ sizeof(uint32_t);
5765 kind
= ld::Fixup::kindStoreLittleEndian32
;
5768 throw "r_length=3 not supported";
5770 if ( reloc
->r_extern() ) {
5772 const macho_nlist
<P
>& targetSymbol
= parser
.symbolFromIndex(reloc
->r_symbolnum());
5773 target
.name
= parser
.nameFromSymbol(targetSymbol
);
5774 target
.weakImport
= parser
.weakImportFromSymbol(targetSymbol
);
5775 target
.addend
= (int32_t)contentValue
;
5778 parser
.findTargetFromAddressAndSectionNum(contentValue
, reloc
->r_symbolnum(), target
);
5780 if ( (kind
== ld::Fixup::kindStoreX86BranchPCRel32
) && (target
.name
!= NULL
) ) {
5781 if ( strncmp(target
.name
, "___dtrace_probe$", 16) == 0 ) {
5782 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindStoreX86DtraceCallSiteNop
, false, target
.name
);
5783 parser
.addDtraceExtraInfos(src
, &target
.name
[16]);
5786 else if ( strncmp(target
.name
, "___dtrace_isenabled$", 20) == 0 ) {
5787 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindStoreX86DtraceIsEnableSiteClear
, false, target
.name
);
5788 parser
.addDtraceExtraInfos(src
, &target
.name
[20]);
5792 parser
.addFixups(src
, kind
, target
);
5795 case GENERIC_RLEOC_TLV
:
5797 if ( !reloc
->r_extern() )
5798 throw "r_extern=0 and r_type=GENERIC_RLEOC_TLV not supported";
5799 if ( reloc
->r_length() != 2 )
5800 throw "r_length!=2 and r_type=GENERIC_RLEOC_TLV not supported";
5801 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(reloc
->r_symbolnum());
5802 // use direct reference for local symbols
5803 if ( ((sym
.n_type() & N_TYPE
) == N_SECT
) && ((sym
.n_type() & N_EXT
) == 0) ) {
5804 parser
.findTargetFromAddressAndSectionNum(sym
.n_value(), sym
.n_sect(), target
);
5808 target
.name
= parser
.nameFromSymbol(sym
);
5809 target
.weakImport
= parser
.weakImportFromSymbol(sym
);
5811 target
.addend
= (int64_t)(int32_t)E::get32(*((uint32_t*)fixUpPtr
));
5812 if ( reloc
->r_pcrel() ) {
5813 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel32TLVLoad
, target
);
5816 parser
.addFixups(src
, ld::Fixup::kindStoreX86Abs32TLVLoad
, target
);
5822 throwf("unsupported i386 relocation type (%d)", reloc
->r_type());
5826 // scattered relocation
5827 const macho_scattered_relocation_info
<P
>* sreloc
= (macho_scattered_relocation_info
<P
>*)reloc
;
5828 srcAddr
= sect
->addr() + sreloc
->r_address();
5829 src
.atom
= this->findAtomByAddress(srcAddr
);
5830 assert(src
.atom
!= NULL
);
5831 src
.offsetInAtom
= srcAddr
- src
.atom
->_objAddress
;
5832 fixUpPtr
= file().fileContent() + sect
->offset() + sreloc
->r_address();
5833 uint32_t relocValue
= sreloc
->r_value();
5834 bool result
= false;
5835 // file format allows pair to be scattered or not
5836 const macho_scattered_relocation_info
<P
>* nextSReloc
= &sreloc
[1];
5837 const macho_relocation_info
<P
>* nextReloc
= &reloc
[1];
5838 bool nextRelocIsPair
= false;
5839 uint32_t nextRelocAddress
= 0;
5840 uint32_t nextRelocValue
= 0;
5841 if ( (nextReloc
->r_address() & R_SCATTERED
) == 0 ) {
5842 if ( nextReloc
->r_type() == GENERIC_RELOC_PAIR
) {
5843 nextRelocIsPair
= true;
5844 nextRelocAddress
= nextReloc
->r_address();
5845 result
= true; // iterator should skip next reloc, since we've consumed it here
5849 if ( nextSReloc
->r_type() == GENERIC_RELOC_PAIR
) {
5850 nextRelocIsPair
= true;
5851 nextRelocAddress
= nextSReloc
->r_address();
5852 nextRelocValue
= nextSReloc
->r_value();
5855 switch (sreloc
->r_type()) {
5856 case GENERIC_RELOC_VANILLA
:
5857 // with a scattered relocation we get both the target (sreloc->r_value()) and the target+offset (*fixUpPtr)
5858 target
.atom
= parser
.findAtomByAddress(relocValue
);
5859 if ( sreloc
->r_pcrel() ) {
5860 switch ( sreloc
->r_length() ) {
5862 contentValue
= srcAddr
+ 1 + *fixUpPtr
;
5863 target
.addend
= (int32_t)contentValue
- (int32_t)relocValue
;
5864 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel8
, target
);
5867 contentValue
= srcAddr
+ 2 + LittleEndian::get16(*((uint16_t*)fixUpPtr
));
5868 target
.addend
= (int32_t)contentValue
- (int32_t)relocValue
;
5869 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel16
, target
);
5872 contentValue
= srcAddr
+ 4 + LittleEndian::get32(*((uint32_t*)fixUpPtr
));
5873 target
.addend
= (int32_t)contentValue
- (int32_t)relocValue
;
5874 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel32
, target
);
5877 throw "unsupported r_length=3 for scattered pc-rel vanilla reloc";
5882 if ( sreloc
->r_length() != 2 )
5883 throwf("unsupported r_length=%d for scattered vanilla reloc", sreloc
->r_length());
5884 contentValue
= LittleEndian::get32(*((uint32_t*)fixUpPtr
));
5885 target
.addend
= (int32_t)contentValue
- (int32_t)(target
.atom
->objectAddress());
5886 parser
.addFixups(src
, ld::Fixup::kindStoreLittleEndian32
, target
);
5889 case GENERIC_RELOC_SECTDIFF
:
5890 case GENERIC_RELOC_LOCAL_SECTDIFF
:
5892 if ( !nextRelocIsPair
)
5893 throw "GENERIC_RELOC_SECTDIFF missing following pair";
5894 switch ( sreloc
->r_length() ) {
5897 throw "bad length for GENERIC_RELOC_SECTDIFF";
5899 contentValue
= (int32_t)(int16_t)LittleEndian::get16(*((uint16_t*)fixUpPtr
));
5900 kind
= ld::Fixup::kindStoreLittleEndian16
;
5903 contentValue
= LittleEndian::get32(*((uint32_t*)fixUpPtr
));
5904 kind
= ld::Fixup::kindStoreLittleEndian32
;
5907 Atom
<x86
>* fromAtom
= parser
.findAtomByAddress(nextRelocValue
);
5908 uint32_t offsetInFrom
= nextRelocValue
- fromAtom
->_objAddress
;
5909 parser
.findTargetFromAddress(sreloc
->r_value(), target
);
5910 // check for addend encoded in the section content
5911 int64_t addend
= (int32_t)contentValue
- (int32_t)(sreloc
->r_value() - nextRelocValue
);
5913 // switch binding base on coalescing
5914 if ( target
.atom
== NULL
) {
5915 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, false, target
.name
);
5917 else if ( target
.atom
->scope() == ld::Atom::scopeTranslationUnit
) {
5918 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, target
.atom
);
5920 else if ( (target
.atom
->combine() == ld::Atom::combineByNameAndContent
) || (target
.atom
->combine() == ld::Atom::combineByNameAndReferences
) ) {
5921 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, target
.atom
);
5924 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, false, target
.atom
->name());
5926 parser
.addFixup(src
, ld::Fixup::k2of5
, ld::Fixup::kindAddAddend
, target
.addend
);
5927 parser
.addFixup(src
, ld::Fixup::k3of5
, ld::Fixup::kindSubtractTargetAddress
, fromAtom
);
5928 parser
.addFixup(src
, ld::Fixup::k4of5
, ld::Fixup::kindSubtractAddend
, offsetInFrom
-addend
);
5929 parser
.addFixup(src
, ld::Fixup::k5of5
, kind
);
5932 // switch binding base on coalescing
5933 if ( target
.atom
== NULL
) {
5934 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, false, target
.name
);
5936 else if ( target
.atom
->scope() == ld::Atom::scopeTranslationUnit
) {
5937 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, target
.atom
);
5939 else if ( (target
.atom
->combine() == ld::Atom::combineByNameAndContent
) || (target
.atom
->combine() == ld::Atom::combineByNameAndReferences
) ) {
5940 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, target
.atom
);
5943 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, false, target
.atom
->name());
5945 parser
.addFixup(src
, ld::Fixup::k2of5
, ld::Fixup::kindAddAddend
, target
.addend
+addend
);
5946 parser
.addFixup(src
, ld::Fixup::k3of5
, ld::Fixup::kindSubtractTargetAddress
, fromAtom
);
5947 parser
.addFixup(src
, ld::Fixup::k4of5
, ld::Fixup::kindSubtractAddend
, offsetInFrom
);
5948 parser
.addFixup(src
, ld::Fixup::k5of5
, kind
);
5961 #if SUPPORT_ARCH_arm_any
5963 bool Section
<arm
>::addRelocFixup(class Parser
<arm
>& parser
, const macho_relocation_info
<P
>* reloc
)
5965 const macho_section
<P
>* sect
= this->machoSection();
5966 bool result
= false;
5970 int32_t displacement
= 0;
5971 uint32_t instruction
= 0;
5972 pint_t contentValue
= 0;
5973 Parser
<arm
>::SourceLocation src
;
5974 Parser
<arm
>::TargetDesc target
;
5975 const macho_relocation_info
<P
>* nextReloc
;
5977 if ( (reloc
->r_address() & R_SCATTERED
) == 0 ) {
5978 bool externSymbolIsThumbDef
= false;
5979 srcAddr
= sect
->addr() + reloc
->r_address();
5980 src
.atom
= this->findAtomByAddress(srcAddr
);
5981 src
.offsetInAtom
= srcAddr
- src
.atom
->_objAddress
;
5982 fixUpPtr
= (uint32_t*)(file().fileContent() + sect
->offset() + reloc
->r_address());
5983 if ( reloc
->r_type() != ARM_RELOC_PAIR
)
5984 instruction
= LittleEndian::get32(*fixUpPtr
);
5985 if ( reloc
->r_extern() ) {
5986 const macho_nlist
<P
>& targetSymbol
= parser
.symbolFromIndex(reloc
->r_symbolnum());
5987 // use direct reference for local symbols
5988 if ( ((targetSymbol
.n_type() & N_TYPE
) == N_SECT
) && (((targetSymbol
.n_type() & N_EXT
) == 0) || (parser
.nameFromSymbol(targetSymbol
)[0] == 'L')) ) {
5989 parser
.findTargetFromAddressAndSectionNum(targetSymbol
.n_value(), targetSymbol
.n_sect(), target
);
5993 target
.name
= parser
.nameFromSymbol(targetSymbol
);
5994 target
.weakImport
= parser
.weakImportFromSymbol(targetSymbol
);
5995 if ( ((targetSymbol
.n_type() & N_TYPE
) == N_SECT
) && (targetSymbol
.n_desc() & N_ARM_THUMB_DEF
) )
5996 externSymbolIsThumbDef
= true;
5999 switch ( reloc
->r_type() ) {
6000 case ARM_RELOC_BR24
:
6001 // Sign-extend displacement
6002 displacement
= (instruction
& 0x00FFFFFF) << 2;
6003 if ( (displacement
& 0x02000000) != 0 )
6004 displacement
|= 0xFC000000;
6005 // The pc added will be +8 from the pc
6007 // If this is BLX add H << 1
6008 if ((instruction
& 0xFE000000) == 0xFA000000)
6009 displacement
+= ((instruction
& 0x01000000) >> 23);
6010 if ( reloc
->r_extern() ) {
6011 target
.addend
= srcAddr
+ displacement
;
6012 if ( externSymbolIsThumbDef
)
6013 target
.addend
&= -2; // remove thumb bit
6016 dstAddr
= srcAddr
+ displacement
;
6017 parser
.findTargetFromAddressAndSectionNum(dstAddr
, reloc
->r_symbolnum(), target
);
6019 // special case "calls" for dtrace
6020 if ( (target
.name
!= NULL
) && (strncmp(target
.name
, "___dtrace_probe$", 16) == 0) ) {
6021 parser
.addFixup(src
, ld::Fixup::k1of1
,
6022 ld::Fixup::kindStoreARMDtraceCallSiteNop
, false, target
.name
);
6023 parser
.addDtraceExtraInfos(src
, &target
.name
[16]);
6025 else if ( (target
.name
!= NULL
) && (strncmp(target
.name
, "___dtrace_isenabled$", 20) == 0) ) {
6026 parser
.addFixup(src
, ld::Fixup::k1of1
,
6027 ld::Fixup::kindStoreARMDtraceIsEnableSiteClear
, false, target
.name
);
6028 parser
.addDtraceExtraInfos(src
, &target
.name
[20]);
6031 parser
.addFixups(src
, ld::Fixup::kindStoreARMBranch24
, target
);
6034 case ARM_THUMB_RELOC_BR22
:
6035 // thumb2 added two more bits to displacement, complicating the displacement decoding
6037 uint32_t s
= (instruction
>> 10) & 0x1;
6038 uint32_t j1
= (instruction
>> 29) & 0x1;
6039 uint32_t j2
= (instruction
>> 27) & 0x1;
6040 uint32_t imm10
= instruction
& 0x3FF;
6041 uint32_t imm11
= (instruction
>> 16) & 0x7FF;
6042 uint32_t i1
= (j1
== s
);
6043 uint32_t i2
= (j2
== s
);
6044 uint32_t dis
= (s
<< 24) | (i1
<< 23) | (i2
<< 22) | (imm10
<< 12) | (imm11
<< 1);
6048 displacement
= sdis
;
6050 // The pc added will be +4 from the pc
6052 // If the instruction was blx, force the low 2 bits to be clear
6053 dstAddr
= srcAddr
+ displacement
;
6054 if ((instruction
& 0xD0000000) == 0xC0000000)
6055 dstAddr
&= 0xFFFFFFFC;
6057 if ( reloc
->r_extern() ) {
6058 target
.addend
= dstAddr
;
6061 parser
.findTargetFromAddressAndSectionNum(dstAddr
, reloc
->r_symbolnum(), target
);
6063 // special case "calls" for dtrace
6064 if ( (target
.name
!= NULL
) && (strncmp(target
.name
, "___dtrace_probe$", 16) == 0) ) {
6065 parser
.addFixup(src
, ld::Fixup::k1of1
,
6066 ld::Fixup::kindStoreThumbDtraceCallSiteNop
, false, target
.name
);
6067 parser
.addDtraceExtraInfos(src
, &target
.name
[16]);
6069 else if ( (target
.name
!= NULL
) && (strncmp(target
.name
, "___dtrace_isenabled$", 20) == 0) ) {
6070 parser
.addFixup(src
, ld::Fixup::k1of1
,
6071 ld::Fixup::kindStoreThumbDtraceIsEnableSiteClear
, false, target
.name
);
6072 parser
.addDtraceExtraInfos(src
, &target
.name
[20]);
6075 parser
.addFixups(src
, ld::Fixup::kindStoreThumbBranch22
, target
);
6078 case ARM_RELOC_VANILLA
:
6079 if ( reloc
->r_length() != 2 )
6080 throw "bad length for ARM_RELOC_VANILLA";
6081 contentValue
= LittleEndian::get32(*fixUpPtr
);
6082 if ( reloc
->r_extern() ) {
6083 target
.addend
= (int32_t)contentValue
;
6084 if ( externSymbolIsThumbDef
)
6085 target
.addend
&= -2; // remove thumb bit
6088 parser
.findTargetFromAddressAndSectionNum(contentValue
, reloc
->r_symbolnum(), target
);
6089 // possible non-extern relocation turned into by-name ref because target is a weak-def
6090 if ( target
.atom
!= NULL
) {
6091 if ( target
.atom
->isThumb() )
6092 target
.addend
&= -2; // remove thumb bit
6093 // if reference to LSDA, add group subordinate fixup
6094 if ( target
.atom
->contentType() == ld::Atom::typeLSDA
) {
6095 Parser
<arm
>::SourceLocation src2
;
6096 src2
.atom
= src
.atom
;
6097 src2
.offsetInAtom
= 0;
6098 parser
.addFixup(src2
, ld::Fixup::k1of1
, ld::Fixup::kindNoneGroupSubordinateLSDA
, target
.atom
);
6102 parser
.addFixups(src
, ld::Fixup::kindStoreLittleEndian32
, target
);
6104 case ARM_THUMB_32BIT_BRANCH
:
6105 // silently ignore old unnecessary reloc
6107 case ARM_RELOC_HALF
:
6108 nextReloc
= &reloc
[1];
6109 if ( nextReloc
->r_type() == ARM_RELOC_PAIR
) {
6110 uint32_t instruction16
;
6111 uint32_t other16
= (nextReloc
->r_address() & 0xFFFF);
6113 if ( reloc
->r_length() & 2 ) {
6115 uint32_t i
= ((instruction
& 0x00000400) >> 10);
6116 uint32_t imm4
= (instruction
& 0x0000000F);
6117 uint32_t imm3
= ((instruction
& 0x70000000) >> 28);
6118 uint32_t imm8
= ((instruction
& 0x00FF0000) >> 16);
6119 instruction16
= (imm4
<< 12) | (i
<< 11) | (imm3
<< 8) | imm8
;
6123 uint32_t imm4
= ((instruction
& 0x000F0000) >> 16);
6124 uint32_t imm12
= (instruction
& 0x00000FFF);
6125 instruction16
= (imm4
<< 12) | imm12
;
6127 if ( reloc
->r_length() & 1 ) {
6129 dstAddr
= ((instruction16
<< 16) | other16
);
6130 if ( reloc
->r_extern() ) {
6131 target
.addend
= dstAddr
;
6132 if ( externSymbolIsThumbDef
)
6133 target
.addend
&= -2; // remove thumb bit
6136 parser
.findTargetFromAddress(dstAddr
, target
);
6137 if ( target
.atom
->isThumb() )
6138 target
.addend
&= (-2); // remove thumb bit
6140 parser
.addFixups(src
, (isThumb
? ld::Fixup::kindStoreThumbHigh16
: ld::Fixup::kindStoreARMHigh16
), target
);
6144 dstAddr
= (other16
<< 16) | instruction16
;
6145 if ( reloc
->r_extern() ) {
6146 target
.addend
= dstAddr
;
6147 if ( externSymbolIsThumbDef
)
6148 target
.addend
&= -2; // remove thumb bit
6151 parser
.findTargetFromAddress(dstAddr
, target
);
6152 if ( target
.atom
->isThumb() )
6153 target
.addend
&= (-2); // remove thumb bit
6155 parser
.addFixups(src
, (isThumb
? ld::Fixup::kindStoreThumbLow16
: ld::Fixup::kindStoreARMLow16
), target
);
6160 throw "for ARM_RELOC_HALF, next reloc is not ARM_RELOC_PAIR";
6163 throwf("unknown relocation type %d", reloc
->r_type());
6168 const macho_scattered_relocation_info
<P
>* sreloc
= (macho_scattered_relocation_info
<P
>*)reloc
;
6169 // file format allows pair to be scattered or not
6170 const macho_scattered_relocation_info
<P
>* nextSReloc
= &sreloc
[1];
6171 nextReloc
= &reloc
[1];
6172 srcAddr
= sect
->addr() + sreloc
->r_address();
6173 dstAddr
= sreloc
->r_value();
6174 fixUpPtr
= (uint32_t*)(file().fileContent() + sect
->offset() + sreloc
->r_address());
6175 instruction
= LittleEndian::get32(*fixUpPtr
);
6176 src
.atom
= this->findAtomByAddress(srcAddr
);
6177 src
.offsetInAtom
= srcAddr
- src
.atom
->_objAddress
;
6178 bool nextRelocIsPair
= false;
6179 uint32_t nextRelocAddress
= 0;
6180 uint32_t nextRelocValue
= 0;
6181 if ( (nextReloc
->r_address() & R_SCATTERED
) == 0 ) {
6182 if ( nextReloc
->r_type() == ARM_RELOC_PAIR
) {
6183 nextRelocIsPair
= true;
6184 nextRelocAddress
= nextReloc
->r_address();
6189 if ( nextSReloc
->r_type() == ARM_RELOC_PAIR
) {
6190 nextRelocIsPair
= true;
6191 nextRelocAddress
= nextSReloc
->r_address();
6192 nextRelocValue
= nextSReloc
->r_value();
6196 switch ( sreloc
->r_type() ) {
6197 case ARM_RELOC_VANILLA
:
6198 // with a scattered relocation we get both the target (sreloc->r_value()) and the target+offset (*fixUpPtr)
6199 if ( sreloc
->r_length() != 2 )
6200 throw "bad length for ARM_RELOC_VANILLA";
6201 target
.atom
= parser
.findAtomByAddress(sreloc
->r_value());
6202 if ( target
.atom
== NULL
)
6203 throwf("bad r_value (0x%08X) for ARM_RELOC_VANILLA\n", sreloc
->r_value());
6204 contentValue
= LittleEndian::get32(*fixUpPtr
);
6205 target
.addend
= contentValue
- target
.atom
->_objAddress
;
6206 if ( target
.atom
->isThumb() )
6207 target
.addend
&= -2; // remove thumb bit
6208 parser
.addFixups(src
, ld::Fixup::kindStoreLittleEndian32
, target
);
6210 case ARM_RELOC_BR24
:
6211 // Sign-extend displacement
6212 displacement
= (instruction
& 0x00FFFFFF) << 2;
6213 if ( (displacement
& 0x02000000) != 0 )
6214 displacement
|= 0xFC000000;
6215 // The pc added will be +8 from the pc
6217 // If this is BLX add H << 1
6218 if ((instruction
& 0xFE000000) == 0xFA000000)
6219 displacement
+= ((instruction
& 0x01000000) >> 23);
6220 target
.atom
= parser
.findAtomByAddress(sreloc
->r_value());
6221 target
.addend
= (int64_t)(srcAddr
+ displacement
) - (int64_t)(target
.atom
->_objAddress
);
6222 parser
.addFixups(src
, ld::Fixup::kindStoreARMBranch24
, target
);
6224 case ARM_THUMB_RELOC_BR22
:
6225 // thumb2 added two more bits to displacement, complicating the displacement decoding
6227 uint32_t s
= (instruction
>> 10) & 0x1;
6228 uint32_t j1
= (instruction
>> 29) & 0x1;
6229 uint32_t j2
= (instruction
>> 27) & 0x1;
6230 uint32_t imm10
= instruction
& 0x3FF;
6231 uint32_t imm11
= (instruction
>> 16) & 0x7FF;
6232 uint32_t i1
= (j1
== s
);
6233 uint32_t i2
= (j2
== s
);
6234 uint32_t dis
= (s
<< 24) | (i1
<< 23) | (i2
<< 22) | (imm10
<< 12) | (imm11
<< 1);
6238 displacement
= sdis
;
6240 // The pc added will be +4 from the pc
6242 dstAddr
= srcAddr
+displacement
;
6243 // If the instruction was blx, force the low 2 bits to be clear
6244 if ((instruction
& 0xF8000000) == 0xE8000000)
6245 dstAddr
&= 0xFFFFFFFC;
6246 target
.atom
= parser
.findAtomByAddress(sreloc
->r_value());
6247 target
.addend
= dstAddr
- target
.atom
->_objAddress
;
6248 parser
.addFixups(src
, ld::Fixup::kindStoreThumbBranch22
, target
);
6250 case ARM_RELOC_SECTDIFF
:
6251 case ARM_RELOC_LOCAL_SECTDIFF
:
6253 if ( ! nextRelocIsPair
)
6254 throw "ARM_RELOC_SECTDIFF missing following pair";
6255 if ( sreloc
->r_length() != 2 )
6256 throw "bad length for ARM_RELOC_SECTDIFF";
6257 contentValue
= LittleEndian::get32(*fixUpPtr
);
6258 Atom
<arm
>* fromAtom
= parser
.findAtomByAddress(nextRelocValue
);
6259 uint32_t offsetInFrom
= nextRelocValue
- fromAtom
->_objAddress
;
6260 uint32_t offsetInTarget
;
6261 Atom
<arm
>* targetAtom
= parser
.findAtomByAddressOrLocalTargetOfStub(sreloc
->r_value(), &offsetInTarget
);
6262 // check for addend encoded in the section content
6263 int64_t addend
= (int32_t)contentValue
- (int32_t)(sreloc
->r_value() - nextRelocValue
);
6264 if ( targetAtom
->isThumb() )
6265 addend
&= -2; // remove thumb bit
6266 // if reference to LSDA, add group subordinate fixup
6267 if ( targetAtom
->contentType() == ld::Atom::typeLSDA
) {
6268 Parser
<arm
>::SourceLocation src2
;
6269 src2
.atom
= src
.atom
;
6270 src2
.offsetInAtom
= 0;
6271 parser
.addFixup(src2
, ld::Fixup::k1of1
, ld::Fixup::kindNoneGroupSubordinateLSDA
, targetAtom
);
6274 // switch binding base on coalescing
6275 if ( targetAtom
->scope() == ld::Atom::scopeTranslationUnit
) {
6276 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, targetAtom
);
6278 else if ( (targetAtom
->combine() == ld::Atom::combineByNameAndContent
) || (targetAtom
->combine() == ld::Atom::combineByNameAndReferences
) ) {
6279 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, targetAtom
);
6282 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, false, targetAtom
->name());
6284 parser
.addFixup(src
, ld::Fixup::k2of5
, ld::Fixup::kindAddAddend
, offsetInTarget
);
6285 parser
.addFixup(src
, ld::Fixup::k3of5
, ld::Fixup::kindSubtractTargetAddress
, fromAtom
);
6286 parser
.addFixup(src
, ld::Fixup::k4of5
, ld::Fixup::kindSubtractAddend
, offsetInFrom
-addend
);
6287 parser
.addFixup(src
, ld::Fixup::k5of5
, ld::Fixup::kindStoreLittleEndian32
);
6290 if ( targetAtom
->scope() == ld::Atom::scopeTranslationUnit
) {
6291 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, targetAtom
);
6293 else if ( (targetAtom
->combine() == ld::Atom::combineByNameAndContent
) || (targetAtom
->combine() == ld::Atom::combineByNameAndReferences
) ) {
6294 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, targetAtom
);
6297 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, false, targetAtom
->name());
6299 parser
.addFixup(src
, ld::Fixup::k2of5
, ld::Fixup::kindAddAddend
, (uint32_t)(offsetInTarget
+addend
));
6300 parser
.addFixup(src
, ld::Fixup::k3of5
, ld::Fixup::kindSubtractTargetAddress
, fromAtom
);
6301 parser
.addFixup(src
, ld::Fixup::k4of5
, ld::Fixup::kindSubtractAddend
, offsetInFrom
);
6302 parser
.addFixup(src
, ld::Fixup::k5of5
, ld::Fixup::kindStoreLittleEndian32
);
6306 case ARM_RELOC_HALF_SECTDIFF
:
6307 if ( nextRelocIsPair
) {
6308 instruction
= LittleEndian::get32(*fixUpPtr
);
6309 Atom
<arm
>* fromAtom
= parser
.findAtomByAddress(nextRelocValue
);
6310 uint32_t offsetInFrom
= nextRelocValue
- fromAtom
->_objAddress
;
6311 Atom
<arm
>* targetAtom
= parser
.findAtomByAddress(sreloc
->r_value());
6312 uint32_t offsetInTarget
= sreloc
->r_value() - targetAtom
->_objAddress
;
6313 uint32_t instruction16
;
6314 uint32_t other16
= (nextRelocAddress
& 0xFFFF);
6316 if ( sreloc
->r_length() & 2 ) {
6318 uint32_t i
= ((instruction
& 0x00000400) >> 10);
6319 uint32_t imm4
= (instruction
& 0x0000000F);
6320 uint32_t imm3
= ((instruction
& 0x70000000) >> 28);
6321 uint32_t imm8
= ((instruction
& 0x00FF0000) >> 16);
6322 instruction16
= (imm4
<< 12) | (i
<< 11) | (imm3
<< 8) | imm8
;
6326 uint32_t imm4
= ((instruction
& 0x000F0000) >> 16);
6327 uint32_t imm12
= (instruction
& 0x00000FFF);
6328 instruction16
= (imm4
<< 12) | imm12
;
6330 if ( sreloc
->r_length() & 1 )
6331 dstAddr
= ((instruction16
<< 16) | other16
);
6333 dstAddr
= (other16
<< 16) | instruction16
;
6334 if ( targetAtom
->isThumb() )
6335 dstAddr
&= (-2); // remove thumb bit
6336 int32_t addend
= dstAddr
- (sreloc
->r_value() - nextRelocValue
);
6337 if ( targetAtom
->scope() == ld::Atom::scopeTranslationUnit
) {
6338 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, targetAtom
);
6340 else if ( (targetAtom
->combine() == ld::Atom::combineByNameAndContent
) || (targetAtom
->combine() == ld::Atom::combineByNameAndReferences
) ) {
6341 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, targetAtom
);
6344 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, false, targetAtom
->name());
6346 parser
.addFixup(src
, ld::Fixup::k2of5
, ld::Fixup::kindAddAddend
, (uint32_t)offsetInTarget
+addend
);
6347 parser
.addFixup(src
, ld::Fixup::k3of5
, ld::Fixup::kindSubtractTargetAddress
, fromAtom
);
6348 parser
.addFixup(src
, ld::Fixup::k4of5
, ld::Fixup::kindSubtractAddend
, offsetInFrom
);
6349 if ( sreloc
->r_length() & 1 ) {
6351 parser
.addFixup(src
, ld::Fixup::k5of5
, (isThumb
? ld::Fixup::kindStoreThumbHigh16
: ld::Fixup::kindStoreARMHigh16
));
6355 parser
.addFixup(src
, ld::Fixup::k5of5
, (isThumb
? ld::Fixup::kindStoreThumbLow16
: ld::Fixup::kindStoreARMLow16
));
6360 throw "ARM_RELOC_HALF_SECTDIFF reloc missing following pair";
6362 case ARM_RELOC_HALF
:
6363 if ( nextRelocIsPair
) {
6364 instruction
= LittleEndian::get32(*fixUpPtr
);
6365 Atom
<arm
>* targetAtom
= parser
.findAtomByAddress(sreloc
->r_value());
6366 uint32_t instruction16
;
6367 uint32_t other16
= (nextRelocAddress
& 0xFFFF);
6369 if ( sreloc
->r_length() & 2 ) {
6371 uint32_t i
= ((instruction
& 0x00000400) >> 10);
6372 uint32_t imm4
= (instruction
& 0x0000000F);
6373 uint32_t imm3
= ((instruction
& 0x70000000) >> 28);
6374 uint32_t imm8
= ((instruction
& 0x00FF0000) >> 16);
6375 instruction16
= (imm4
<< 12) | (i
<< 11) | (imm3
<< 8) | imm8
;
6379 uint32_t imm4
= ((instruction
& 0x000F0000) >> 16);
6380 uint32_t imm12
= (instruction
& 0x00000FFF);
6381 instruction16
= (imm4
<< 12) | imm12
;
6383 if ( sreloc
->r_length() & 1 )
6384 dstAddr
= ((instruction16
<< 16) | other16
);
6386 dstAddr
= (other16
<< 16) | instruction16
;
6387 if ( targetAtom
->scope() == ld::Atom::scopeTranslationUnit
) {
6388 parser
.addFixup(src
, ld::Fixup::k1of3
, ld::Fixup::kindSetTargetAddress
, targetAtom
);
6390 else if ( (targetAtom
->combine() == ld::Atom::combineByNameAndContent
) || (targetAtom
->combine() == ld::Atom::combineByNameAndReferences
) ) {
6391 parser
.addFixup(src
, ld::Fixup::k1of3
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, targetAtom
);
6394 parser
.addFixup(src
, ld::Fixup::k1of3
, ld::Fixup::kindSetTargetAddress
, false, targetAtom
->name());
6396 parser
.addFixup(src
, ld::Fixup::k2of3
, ld::Fixup::kindAddAddend
, dstAddr
- targetAtom
->_objAddress
);
6397 if ( sreloc
->r_length() & 1 ) {
6399 parser
.addFixup(src
, ld::Fixup::k3of3
, (isThumb
? ld::Fixup::kindStoreThumbHigh16
: ld::Fixup::kindStoreARMHigh16
));
6403 parser
.addFixup(src
, ld::Fixup::k3of3
, (isThumb
? ld::Fixup::kindStoreThumbLow16
: ld::Fixup::kindStoreARMLow16
));
6408 throw "scattered ARM_RELOC_HALF reloc missing following pair";
6411 throwf("unknown ARM scattered relocation type %d", sreloc
->r_type());
6419 #if SUPPORT_ARCH_arm64
6421 bool Section
<arm64
>::addRelocFixup(class Parser
<arm64
>& parser
, const macho_relocation_info
<P
>* reloc
)
6423 bool result
= false;
6424 Parser
<arm64
>::SourceLocation src
;
6425 Parser
<arm64
>::TargetDesc target
= { NULL
, NULL
, false, 0 };
6426 Parser
<arm64
>::TargetDesc toTarget
;
6427 int32_t prefixRelocAddend
= 0;
6428 if ( reloc
->r_type() == ARM64_RELOC_ADDEND
) {
6429 uint32_t rawAddend
= reloc
->r_symbolnum();
6430 prefixRelocAddend
= rawAddend
;
6431 if ( rawAddend
& 0x00800000 )
6432 prefixRelocAddend
|= 0xFF000000; // sign extend 24-bit signed int to 32-bits
6433 uint32_t addendAddress
= reloc
->r_address();
6434 ++reloc
; //advance to next reloc record
6436 if ( reloc
->r_address() != addendAddress
)
6437 throw "ARM64_RELOC_ADDEND r_address does not match next reloc's r_address";
6439 const macho_section
<P
>* sect
= this->machoSection();
6440 uint64_t srcAddr
= sect
->addr() + reloc
->r_address();
6441 src
.atom
= this->findAtomByAddress(srcAddr
);
6442 src
.offsetInAtom
= srcAddr
- src
.atom
->_objAddress
;
6443 const uint8_t* fixUpPtr
= file().fileContent() + sect
->offset() + reloc
->r_address();
6444 uint64_t contentValue
= 0;
6445 const macho_relocation_info
<arm64::P
>* nextReloc
= &reloc
[1];
6446 bool useDirectBinding
;
6447 uint32_t instruction
;
6448 uint32_t encodedAddend
;
6449 switch ( reloc
->r_length() ) {
6451 contentValue
= *fixUpPtr
;
6454 contentValue
= (int64_t)(int16_t)E::get16(*((uint16_t*)fixUpPtr
));
6457 contentValue
= (int64_t)(int32_t)E::get32(*((uint32_t*)fixUpPtr
));
6460 contentValue
= E::get64(*((uint64_t*)fixUpPtr
));
6463 if ( reloc
->r_extern() ) {
6464 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(reloc
->r_symbolnum());
6465 const char* symbolName
= parser
.nameFromSymbol(sym
);
6466 if ( ((sym
.n_type() & N_TYPE
) == N_SECT
) && (((sym
.n_type() & N_EXT
) == 0) || (symbolName
[0] == 'L') || (symbolName
[0] == 'l')) ) {
6467 // use direct reference for local symbols
6468 parser
.findTargetFromAddressAndSectionNum(sym
.n_value(), sym
.n_sect(), target
);
6469 //target.addend += contentValue;
6471 else if ( ((sym
.n_type() & N_TYPE
) == N_SECT
) && (src
.atom
->_objAddress
<= sym
.n_value()) && (sym
.n_value() < (src
.atom
->_objAddress
+src
.atom
->size())) ) {
6472 // <rdar://problem/13700961> spurious warning when weak function has reference to itself
6473 // use direct reference when atom targets itself
6474 target
.atom
= src
.atom
;
6478 target
.name
= symbolName
;
6479 target
.weakImport
= parser
.weakImportFromSymbol(sym
);
6480 //target.addend = contentValue;
6482 // cfstrings should always use direct reference to backing store
6483 if ( (this->type() == ld::Section::typeCFString
) && (src
.offsetInAtom
!= 0) ) {
6484 parser
.findTargetFromAddressAndSectionNum(sym
.n_value(), sym
.n_sect(), target
);
6485 //target.addend = contentValue;
6489 if ( reloc
->r_pcrel() )
6490 contentValue
+= srcAddr
;
6491 parser
.findTargetFromAddressAndSectionNum(contentValue
, reloc
->r_symbolnum(), target
);
6493 switch ( reloc
->r_type() ) {
6494 case ARM64_RELOC_UNSIGNED
:
6495 if ( reloc
->r_pcrel() )
6496 throw "pcrel and ARM64_RELOC_UNSIGNED not supported";
6497 target
.addend
= contentValue
;
6498 switch ( reloc
->r_length() ) {
6501 throw "length < 2 and ARM64_RELOC_UNSIGNED not supported";
6503 parser
.addFixups(src
, ld::Fixup::kindStoreLittleEndian32
, target
);
6506 parser
.addFixups(src
, ld::Fixup::kindStoreLittleEndian64
, target
);
6510 case ARM64_RELOC_BRANCH26
:
6511 if ( ! reloc
->r_pcrel() )
6512 throw "not pcrel and ARM64_RELOC_BRANCH26 not supported";
6513 if ( ! reloc
->r_extern() )
6514 throw "r_extern == 0 and ARM64_RELOC_BRANCH26 not supported";
6515 if ( reloc
->r_length() != 2 )
6516 throw "r_length != 2 and ARM64_RELOC_BRANCH26 not supported";
6517 if ( (target
.name
!= NULL
) && (strncmp(target
.name
, "___dtrace_probe$", 16) == 0) ) {
6518 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindStoreARM64DtraceCallSiteNop
, false, target
.name
);
6519 parser
.addDtraceExtraInfos(src
, &target
.name
[16]);
6521 else if ( (target
.name
!= NULL
) && (strncmp(target
.name
, "___dtrace_isenabled$", 20) == 0) ) {
6522 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindStoreARM64DtraceIsEnableSiteClear
, false, target
.name
);
6523 parser
.addDtraceExtraInfos(src
, &target
.name
[20]);
6526 target
.addend
= prefixRelocAddend
;
6527 instruction
= contentValue
;
6528 encodedAddend
= (instruction
& 0x03FFFFFF) << 2;
6529 if ( encodedAddend
!= 0 ) {
6530 if ( prefixRelocAddend
== 0 ) {
6531 warning("branch26 instruction at 0x%08X has embedded addend. ARM64_RELOC_ADDEND should be used instead", reloc
->r_address());
6532 target
.addend
= encodedAddend
;
6535 throwf("branch26 instruction at 0x%08X has embedded addend and ARM64_RELOC_ADDEND also used", reloc
->r_address());
6538 parser
.addFixups(src
, ld::Fixup::kindStoreARM64Branch26
, target
);
6541 case ARM64_RELOC_PAGE21
:
6542 if ( ! reloc
->r_pcrel() )
6543 throw "not pcrel and ARM64_RELOC_PAGE21 not supported";
6544 if ( ! reloc
->r_extern() )
6545 throw "r_extern == 0 and ARM64_RELOC_PAGE21 not supported";
6546 if ( reloc
->r_length() != 2 )
6547 throw "length != 2 and ARM64_RELOC_PAGE21 not supported";
6548 target
.addend
= prefixRelocAddend
;
6549 instruction
= contentValue
;
6550 encodedAddend
= ((instruction
& 0x60000000) >> 29) | ((instruction
& 0x01FFFFE0) >> 3);
6551 encodedAddend
*= 4096; // internally addend is in bytes, so scale
6552 if ( encodedAddend
!= 0 ) {
6553 if ( prefixRelocAddend
== 0 ) {
6554 warning("adrp instruction at 0x%08X has embedded addend. ARM64_RELOC_ADDEND should be used instead", reloc
->r_address());
6555 target
.addend
= encodedAddend
;
6558 throwf("adrp instruction at 0x%08X has embedded addend and ARM64_RELOC_ADDEND also used", reloc
->r_address());
6561 parser
.addFixups(src
, ld::Fixup::kindStoreARM64Page21
, target
);
6563 case ARM64_RELOC_PAGEOFF12
:
6564 if ( reloc
->r_pcrel() )
6565 throw "pcrel and ARM64_RELOC_PAGEOFF12 not supported";
6566 if ( ! reloc
->r_extern() )
6567 throw "r_extern == 0 and ARM64_RELOC_PAGEOFF12 not supported";
6568 if ( reloc
->r_length() != 2 )
6569 throw "length != 2 and ARM64_RELOC_PAGEOFF12 not supported";
6570 target
.addend
= prefixRelocAddend
;
6571 instruction
= contentValue
;
6572 encodedAddend
= ((instruction
& 0x003FFC00) >> 10);
6573 // internally addend is in bytes. Some instructions have an implicit scale factor
6574 if ( (instruction
& 0x3B000000) == 0x39000000 ) {
6575 switch ( instruction
& 0xC0000000 ) {
6589 if ( encodedAddend
!= 0 ) {
6590 if ( prefixRelocAddend
== 0 ) {
6591 warning("pageoff12 instruction at 0x%08X has embedded addend. ARM64_RELOC_ADDEND should be used instead", reloc
->r_address());
6592 target
.addend
= encodedAddend
;
6595 throwf("pageoff12 instruction at 0x%08X has embedded addend and ARM64_RELOC_ADDEND also used", reloc
->r_address());
6598 parser
.addFixups(src
, ld::Fixup::kindStoreARM64PageOff12
, target
);
6600 case ARM64_RELOC_GOT_LOAD_PAGE21
:
6601 if ( ! reloc
->r_pcrel() )
6602 throw "not pcrel and ARM64_RELOC_GOT_LOAD_PAGE21 not supported";
6603 if ( ! reloc
->r_extern() )
6604 throw "r_extern == 0 and ARM64_RELOC_GOT_LOAD_PAGE21 not supported";
6605 if ( reloc
->r_length() != 2 )
6606 throw "length != 2 and ARM64_RELOC_GOT_LOAD_PAGE21 not supported";
6607 if ( prefixRelocAddend
!= 0 )
6608 throw "ARM64_RELOC_ADDEND followed by ARM64_RELOC_GOT_LOAD_PAGE21 not supported";
6609 instruction
= contentValue
;
6610 target
.addend
= ((instruction
& 0x60000000) >> 29) | ((instruction
& 0x01FFFFE0) >> 3);
6611 if ( target
.addend
!= 0 )
6612 throw "non-zero addend with ARM64_RELOC_GOT_LOAD_PAGE21 is not supported";
6613 parser
.addFixups(src
, ld::Fixup::kindStoreARM64GOTLoadPage21
, target
);
6615 case ARM64_RELOC_GOT_LOAD_PAGEOFF12
:
6616 if ( reloc
->r_pcrel() )
6617 throw "pcrel and ARM64_RELOC_GOT_LOAD_PAGEOFF12 not supported";
6618 if ( ! reloc
->r_extern() )
6619 throw "r_extern == 0 and ARM64_RELOC_GOT_LOAD_PAGEOFF12 not supported";
6620 if ( reloc
->r_length() != 2 )
6621 throw "length != 2 and ARM64_RELOC_GOT_LOAD_PAGEOFF12 not supported";
6622 if ( prefixRelocAddend
!= 0 )
6623 throw "ARM64_RELOC_ADDEND followed by ARM64_RELOC_GOT_LOAD_PAGEOFF12 not supported";
6624 instruction
= contentValue
;
6625 target
.addend
= ((instruction
& 0x003FFC00) >> 10);
6626 parser
.addFixups(src
, ld::Fixup::kindStoreARM64GOTLoadPageOff12
, target
);
6628 case ARM64_RELOC_TLVP_LOAD_PAGE21
:
6629 if ( ! reloc
->r_pcrel() )
6630 throw "not pcrel and ARM64_RELOC_TLVP_LOAD_PAGE21 not supported";
6631 if ( ! reloc
->r_extern() )
6632 throw "r_extern == 0 and ARM64_RELOC_TLVP_LOAD_PAGE21 not supported";
6633 if ( reloc
->r_length() != 2 )
6634 throw "length != 2 and ARM64_RELOC_TLVP_LOAD_PAGE21 not supported";
6635 if ( prefixRelocAddend
!= 0 )
6636 throw "ARM64_RELOC_ADDEND followed by ARM64_RELOC_TLVP_LOAD_PAGE21 not supported";
6637 instruction
= contentValue
;
6638 target
.addend
= ((instruction
& 0x60000000) >> 29) | ((instruction
& 0x01FFFFE0) >> 3);
6639 if ( target
.addend
!= 0 )
6640 throw "non-zero addend with ARM64_RELOC_GOT_LOAD_PAGE21 is not supported";
6641 parser
.addFixups(src
, ld::Fixup::kindStoreARM64TLVPLoadPage21
, target
);
6643 case ARM64_RELOC_TLVP_LOAD_PAGEOFF12
:
6644 if ( reloc
->r_pcrel() )
6645 throw "pcrel and ARM64_RELOC_TLVP_LOAD_PAGEOFF12 not supported";
6646 if ( ! reloc
->r_extern() )
6647 throw "r_extern == 0 and ARM64_RELOC_TLVP_LOAD_PAGEOFF12 not supported";
6648 if ( reloc
->r_length() != 2 )
6649 throw "length != 2 and ARM64_RELOC_TLVP_LOAD_PAGEOFF12 not supported";
6650 if ( prefixRelocAddend
!= 0 )
6651 throw "ARM64_RELOC_ADDEND followed by ARM64_RELOC_TLVP_LOAD_PAGEOFF12 not supported";
6652 instruction
= contentValue
;
6653 target
.addend
= ((instruction
& 0x003FFC00) >> 10);
6654 parser
.addFixups(src
, ld::Fixup::kindStoreARM64TLVPLoadPageOff12
, target
);
6656 case ARM64_RELOC_SUBTRACTOR
:
6657 if ( reloc
->r_pcrel() )
6658 throw "ARM64_RELOC_SUBTRACTOR cannot be pc-relative";
6659 if ( reloc
->r_length() < 2 )
6660 throw "ARM64_RELOC_SUBTRACTOR must have r_length of 2 or 3";
6661 if ( !reloc
->r_extern() )
6662 throw "ARM64_RELOC_SUBTRACTOR must have r_extern=1";
6663 if ( nextReloc
->r_type() != ARM64_RELOC_UNSIGNED
)
6664 throw "ARM64_RELOC_SUBTRACTOR must be followed by ARM64_RELOC_UNSIGNED";
6665 if ( prefixRelocAddend
!= 0 )
6666 throw "ARM64_RELOC_ADDEND followed by ARM64_RELOC_SUBTRACTOR not supported";
6668 if ( nextReloc
->r_pcrel() )
6669 throw "ARM64_RELOC_UNSIGNED following a ARM64_RELOC_SUBTRACTOR cannot be pc-relative";
6670 if ( nextReloc
->r_length() != reloc
->r_length() )
6671 throw "ARM64_RELOC_UNSIGNED following a ARM64_RELOC_SUBTRACTOR must have same r_length";
6672 if ( nextReloc
->r_extern() ) {
6673 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(nextReloc
->r_symbolnum());
6674 // use direct reference for local symbols
6675 if ( ((sym
.n_type() & N_TYPE
) == N_SECT
) && (((sym
.n_type() & N_EXT
) == 0) || (parser
.nameFromSymbol(sym
)[0] == 'L')) ) {
6676 parser
.findTargetFromAddressAndSectionNum(sym
.n_value(), sym
.n_sect(), toTarget
);
6677 toTarget
.addend
= contentValue
;
6678 useDirectBinding
= true;
6681 toTarget
.name
= parser
.nameFromSymbol(sym
);
6682 toTarget
.weakImport
= parser
.weakImportFromSymbol(sym
);
6683 toTarget
.addend
= contentValue
;
6684 useDirectBinding
= false;
6688 parser
.findTargetFromAddressAndSectionNum(contentValue
, nextReloc
->r_symbolnum(), toTarget
);
6689 useDirectBinding
= (toTarget
.atom
->scope() == ld::Atom::scopeTranslationUnit
);
6691 if ( useDirectBinding
)
6692 parser
.addFixup(src
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, toTarget
.atom
);
6694 parser
.addFixup(src
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, toTarget
.weakImport
, toTarget
.name
);
6695 parser
.addFixup(src
, ld::Fixup::k2of4
, ld::Fixup::kindAddAddend
, toTarget
.addend
);
6696 if ( target
.atom
== NULL
)
6697 parser
.addFixup(src
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractTargetAddress
, false, target
.name
);
6699 parser
.addFixup(src
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractTargetAddress
, target
.atom
);
6700 if ( reloc
->r_length() == 2 )
6701 parser
.addFixup(src
, ld::Fixup::k4of4
, ld::Fixup::kindStoreLittleEndian32
);
6703 parser
.addFixup(src
, ld::Fixup::k4of4
, ld::Fixup::kindStoreLittleEndian64
);
6705 case ARM64_RELOC_POINTER_TO_GOT
:
6706 if ( ! reloc
->r_extern() )
6707 throw "r_extern == 0 and ARM64_RELOC_POINTER_TO_GOT not supported";
6708 if ( prefixRelocAddend
!= 0 )
6709 throw "ARM64_RELOC_ADDEND followed by ARM64_RELOC_POINTER_TO_GOT not supported";
6710 if ( reloc
->r_pcrel() ) {
6711 if ( reloc
->r_length() != 2 )
6712 throw "r_length != 2 and r_extern = 1 and ARM64_RELOC_POINTER_TO_GOT not supported";
6713 parser
.addFixups(src
, ld::Fixup::kindStoreARM64PCRelToGOT
, target
);
6716 if ( reloc
->r_length() != 3 )
6717 throw "r_length != 3 and r_extern = 0 and ARM64_RELOC_POINTER_TO_GOT not supported";
6718 parser
.addFixups(src
, ld::Fixup::kindStoreARM64PointerToGOT
, target
);
6722 throwf("unknown relocation type %d", reloc
->r_type());
6728 template <typename A
>
6729 bool ObjC1ClassSection
<A
>::addRelocFixup(class Parser
<A
>& parser
, const macho_relocation_info
<P
>* reloc
)
6732 FixedSizeSection
<A
>::addRelocFixup(parser
, reloc
);
6734 assert(0 && "needs template specialization");
6739 bool ObjC1ClassSection
<x86
>::addRelocFixup(class Parser
<x86
>& parser
, const macho_relocation_info
<x86::P
>* reloc
)
6741 // if this is the reloc for the super class name string, add implicit reference to super class
6742 if ( ((reloc
->r_address() & R_SCATTERED
) == 0) && (reloc
->r_type() == GENERIC_RELOC_VANILLA
) ) {
6743 assert( reloc
->r_length() == 2 );
6744 assert( ! reloc
->r_pcrel() );
6746 const macho_section
<P
>* sect
= this->machoSection();
6747 Parser
<x86
>::SourceLocation src
;
6748 uint32_t srcAddr
= sect
->addr() + reloc
->r_address();
6749 src
.atom
= this->findAtomByAddress(srcAddr
);
6750 src
.offsetInAtom
= srcAddr
- src
.atom
->objectAddress();
6751 if ( src
.offsetInAtom
== 4 ) {
6752 Parser
<x86
>::TargetDesc stringTarget
;
6753 const uint8_t* fixUpPtr
= file().fileContent() + sect
->offset() + reloc
->r_address();
6754 uint32_t contentValue
= LittleEndian::get32(*((uint32_t*)fixUpPtr
));
6755 parser
.findTargetFromAddressAndSectionNum(contentValue
, reloc
->r_symbolnum(), stringTarget
);
6757 assert(stringTarget
.atom
!= NULL
);
6758 assert(stringTarget
.atom
->contentType() == ld::Atom::typeCString
);
6759 const char* superClassBaseName
= (char*)stringTarget
.atom
->rawContentPointer();
6760 char* superClassName
= new char[strlen(superClassBaseName
) + 20];
6761 strcpy(superClassName
, ".objc_class_name_");
6762 strcat(superClassName
, superClassBaseName
);
6764 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindSetTargetAddress
, false, superClassName
);
6768 return FixedSizeSection
<x86
>::addRelocFixup(parser
, reloc
);
6773 template <typename A
>
6774 bool Objc1ClassReferences
<A
>::addRelocFixup(class Parser
<A
>& parser
, const macho_relocation_info
<P
>* reloc
)
6777 PointerToCStringSection
<A
>::addRelocFixup(parser
, reloc
);
6779 assert(0 && "needs template specialization");
6786 bool Objc1ClassReferences
<x86
>::addRelocFixup(class Parser
<x86
>& parser
, const macho_relocation_info
<x86::P
>* reloc
)
6788 // add implict class refs, fixups not usable yet, so look at relocations
6789 assert( (reloc
->r_address() & R_SCATTERED
) == 0 );
6790 assert( reloc
->r_type() == GENERIC_RELOC_VANILLA
);
6791 assert( reloc
->r_length() == 2 );
6792 assert( ! reloc
->r_pcrel() );
6794 const macho_section
<P
>* sect
= this->machoSection();
6795 Parser
<x86
>::SourceLocation src
;
6796 uint32_t srcAddr
= sect
->addr() + reloc
->r_address();
6797 src
.atom
= this->findAtomByAddress(srcAddr
);
6798 src
.offsetInAtom
= srcAddr
- src
.atom
->objectAddress();
6799 Parser
<x86
>::TargetDesc stringTarget
;
6800 const uint8_t* fixUpPtr
= file().fileContent() + sect
->offset() + reloc
->r_address();
6801 uint32_t contentValue
= LittleEndian::get32(*((uint32_t*)fixUpPtr
));
6802 parser
.findTargetFromAddressAndSectionNum(contentValue
, reloc
->r_symbolnum(), stringTarget
);
6804 assert(stringTarget
.atom
!= NULL
);
6805 assert(stringTarget
.atom
->contentType() == ld::Atom::typeCString
);
6806 const char* baseClassName
= (char*)stringTarget
.atom
->rawContentPointer();
6807 char* objcClassName
= new char[strlen(baseClassName
) + 20];
6808 strcpy(objcClassName
, ".objc_class_name_");
6809 strcat(objcClassName
, baseClassName
);
6811 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindSetTargetAddress
, false, objcClassName
);
6814 return PointerToCStringSection
<x86
>::addRelocFixup(parser
, reloc
);
6818 template <typename A
>
6819 void Section
<A
>::makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
&)
6821 const macho_section
<P
>* sect
= this->machoSection();
6822 const macho_relocation_info
<P
>* relocs
= (macho_relocation_info
<P
>*)(file().fileContent() + sect
->reloff());
6823 const uint32_t relocCount
= sect
->nreloc();
6824 for (uint32_t r
= 0; r
< relocCount
; ++r
) {
6826 if ( this->addRelocFixup(parser
, &relocs
[r
]) )
6829 catch (const char* msg
) {
6830 throwf("in section %s,%s reloc %u: %s", sect
->segname(), Section
<A
>::makeSectionName(sect
), r
, msg
);
6834 // add follow-on fixups if .o file is missing .subsections_via_symbols
6835 if ( this->addFollowOnFixups() ) {
6836 Atom
<A
>* end
= &_endAtoms
[-1];
6837 for(Atom
<A
>* p
= _beginAtoms
; p
< end
; ++p
) {
6838 typename Parser
<A
>::SourceLocation
src(p
, 0);
6839 Atom
<A
>* nextAtom
= &p
[1];
6840 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindNoneFollowOn
, nextAtom
);
6843 else if ( this->type() == ld::Section::typeCode
) {
6844 // if FDE broke text not at a symbol, use followOn to keep code together
6845 Atom
<A
>* end
= &_endAtoms
[-1];
6846 for(Atom
<A
>* p
= _beginAtoms
; p
< end
; ++p
) {
6847 typename Parser
<A
>::SourceLocation
src(p
, 0);
6848 Atom
<A
>* nextAtom
= &p
[1];
6849 if ( (p
->symbolTableInclusion() == ld::Atom::symbolTableIn
) && (nextAtom
->symbolTableInclusion() == ld::Atom::symbolTableNotIn
) ) {
6850 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindNoneFollowOn
, nextAtom
);
6855 // <rdar://problem/9218847> track data-in-code
6856 if ( parser
.hasDataInCodeLabels() && (this->type() == ld::Section::typeCode
) ) {
6857 for (uint32_t i
=0; i
< parser
.symbolCount(); ++i
) {
6858 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(i
);
6860 if ( (sym
.n_type() & N_STAB
) != 0 )
6862 // ignore non-definitions
6863 if ( (sym
.n_type() & N_TYPE
) != N_SECT
)
6866 // 'L' labels do not denote atom breaks
6867 const char* symbolName
= parser
.nameFromSymbol(sym
);
6868 if ( symbolName
[0] == 'L' ) {
6869 if ( strncmp(symbolName
, "L$start$", 8) == 0 ) {
6870 ld::Fixup::Kind kind
= ld::Fixup::kindNone
;
6871 if ( strncmp(&symbolName
[8], "data$", 5) == 0 )
6872 kind
= ld::Fixup::kindDataInCodeStartData
;
6873 else if ( strncmp(&symbolName
[8], "code$", 5) == 0 )
6874 kind
= ld::Fixup::kindDataInCodeEnd
;
6875 else if ( strncmp(&symbolName
[8], "jt8$", 4) == 0 )
6876 kind
= ld::Fixup::kindDataInCodeStartJT8
;
6877 else if ( strncmp(&symbolName
[8], "jt16$", 4) == 0 )
6878 kind
= ld::Fixup::kindDataInCodeStartJT16
;
6879 else if ( strncmp(&symbolName
[8], "jt32$", 4) == 0 )
6880 kind
= ld::Fixup::kindDataInCodeStartJT32
;
6881 else if ( strncmp(&symbolName
[8], "jta32$", 4) == 0 )
6882 kind
= ld::Fixup::kindDataInCodeStartJTA32
;
6884 warning("unknown L$start$ label %s in file %s", symbolName
, this->file().path());
6885 if ( kind
!= ld::Fixup::kindNone
) {
6886 Atom
<A
>* inAtom
= parser
.findAtomByAddress(sym
.n_value());
6887 typename Parser
<A
>::SourceLocation
src(inAtom
, sym
.n_value() - inAtom
->objectAddress());
6888 parser
.addFixup(src
, ld::Fixup::k1of1
, kind
);
6895 // <rdar://problem/11150575> Handle LC_DATA_IN_CODE in object files
6896 if ( this->type() == ld::Section::typeCode
) {
6897 const pint_t startAddr
= this->_machOSection
->addr();
6898 const pint_t endAddr
= startAddr
+ this->_machOSection
->size();
6899 for ( const macho_data_in_code_entry
<P
>* p
= parser
.dataInCodeStart(); p
!= parser
.dataInCodeEnd(); ++p
) {
6900 if ( (p
->offset() >= startAddr
) && (p
->offset() < endAddr
) ) {
6901 ld::Fixup::Kind kind
= ld::Fixup::kindNone
;
6902 switch ( p
->kind() ) {
6903 case DICE_KIND_DATA
:
6904 kind
= ld::Fixup::kindDataInCodeStartData
;
6906 case DICE_KIND_JUMP_TABLE8
:
6907 kind
= ld::Fixup::kindDataInCodeStartJT8
;
6909 case DICE_KIND_JUMP_TABLE16
:
6910 kind
= ld::Fixup::kindDataInCodeStartJT16
;
6912 case DICE_KIND_JUMP_TABLE32
:
6913 kind
= ld::Fixup::kindDataInCodeStartJT32
;
6915 case DICE_KIND_ABS_JUMP_TABLE32
:
6916 kind
= ld::Fixup::kindDataInCodeStartJTA32
;
6919 kind
= ld::Fixup::kindDataInCodeStartData
;
6920 warning("uknown LC_DATA_IN_CODE kind (%d) at offset 0x%08X", p
->kind(), p
->offset());
6923 Atom
<A
>* inAtom
= parser
.findAtomByAddress(p
->offset());
6924 typename Parser
<A
>::SourceLocation
srcStart(inAtom
, p
->offset() - inAtom
->objectAddress());
6925 parser
.addFixup(srcStart
, ld::Fixup::k1of1
, kind
);
6926 typename Parser
<A
>::SourceLocation
srcEnd(inAtom
, p
->offset() + p
->length() - inAtom
->objectAddress());
6927 parser
.addFixup(srcEnd
, ld::Fixup::k1of1
, ld::Fixup::kindDataInCodeEnd
);
6933 // add follow-on fixups for aliases
6934 if ( _hasAliases
) {
6935 for(Atom
<A
>* p
= _beginAtoms
; p
< _endAtoms
; ++p
) {
6936 if ( p
->isAlias() && ! this->addFollowOnFixups() ) {
6937 Atom
<A
>* targetOfAlias
= &p
[1];
6938 assert(p
< &_endAtoms
[-1]);
6939 assert(p
->_objAddress
== targetOfAlias
->_objAddress
);
6940 typename Parser
<A
>::SourceLocation
src(p
, 0);
6941 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindNoneFollowOn
, targetOfAlias
);
6950 // main function used by linker to instantiate ld::Files
6952 ld::relocatable::File
* parse(const uint8_t* fileContent
, uint64_t fileLength
,
6953 const char* path
, time_t modTime
, ld::File::Ordinal ordinal
, const ParserOptions
& opts
)
6955 switch ( opts
.architecture
) {
6956 #if SUPPORT_ARCH_x86_64
6957 case CPU_TYPE_X86_64
:
6958 if ( mach_o::relocatable::Parser
<x86_64
>::validFile(fileContent
) )
6959 return mach_o::relocatable::Parser
<x86_64
>::parse(fileContent
, fileLength
, path
, modTime
, ordinal
, opts
);
6962 #if SUPPORT_ARCH_i386
6964 if ( mach_o::relocatable::Parser
<x86
>::validFile(fileContent
) )
6965 return mach_o::relocatable::Parser
<x86
>::parse(fileContent
, fileLength
, path
, modTime
, ordinal
, opts
);
6968 #if SUPPORT_ARCH_arm_any
6970 if ( mach_o::relocatable::Parser
<arm
>::validFile(fileContent
, opts
.objSubtypeMustMatch
, opts
.subType
) )
6971 return mach_o::relocatable::Parser
<arm
>::parse(fileContent
, fileLength
, path
, modTime
, ordinal
, opts
);
6974 #if SUPPORT_ARCH_arm64
6975 case CPU_TYPE_ARM64
:
6976 if ( mach_o::relocatable::Parser
<arm64
>::validFile(fileContent
, opts
.objSubtypeMustMatch
, opts
.subType
) )
6977 return mach_o::relocatable::Parser
<arm64
>::parse(fileContent
, fileLength
, path
, modTime
, ordinal
, opts
);
6985 // used by archive reader to validate member object file
6987 bool isObjectFile(const uint8_t* fileContent
, uint64_t fileLength
, const ParserOptions
& opts
)
6989 switch ( opts
.architecture
) {
6990 case CPU_TYPE_X86_64
:
6991 return ( mach_o::relocatable::Parser
<x86_64
>::validFile(fileContent
) );
6993 return ( mach_o::relocatable::Parser
<x86
>::validFile(fileContent
) );
6995 return ( mach_o::relocatable::Parser
<arm
>::validFile(fileContent
, opts
.objSubtypeMustMatch
, opts
.subType
) );
6996 case CPU_TYPE_ARM64
:
6997 return ( mach_o::relocatable::Parser
<arm64
>::validFile(fileContent
, opts
.objSubtypeMustMatch
, opts
.subType
) );
7003 // used by linker to infer architecture when no -arch is on command line
7005 bool isObjectFile(const uint8_t* fileContent
, cpu_type_t
* result
, cpu_subtype_t
* subResult
)
7007 if ( mach_o::relocatable::Parser
<x86_64
>::validFile(fileContent
) ) {
7008 *result
= CPU_TYPE_X86_64
;
7009 *subResult
= CPU_SUBTYPE_X86_64_ALL
;
7012 if ( mach_o::relocatable::Parser
<x86
>::validFile(fileContent
) ) {
7013 *result
= CPU_TYPE_I386
;
7014 *subResult
= CPU_SUBTYPE_X86_ALL
;
7017 if ( mach_o::relocatable::Parser
<arm
>::validFile(fileContent
, false, 0) ) {
7018 *result
= CPU_TYPE_ARM
;
7019 const macho_header
<Pointer32
<LittleEndian
> >* header
= (const macho_header
<Pointer32
<LittleEndian
> >*)fileContent
;
7020 *subResult
= header
->cpusubtype();
7023 if ( mach_o::relocatable::Parser
<arm64
>::validFile(fileContent
, false, 0) ) {
7024 *result
= CPU_TYPE_ARM64
;
7025 *subResult
= CPU_SUBTYPE_ARM64_ALL
;
7032 // used by linker is error messages to describe bad .o file
7034 const char* archName(const uint8_t* fileContent
)
7036 if ( mach_o::relocatable::Parser
<x86_64
>::validFile(fileContent
) ) {
7037 return mach_o::relocatable::Parser
<x86_64
>::fileKind(fileContent
);
7039 if ( mach_o::relocatable::Parser
<x86
>::validFile(fileContent
) ) {
7040 return mach_o::relocatable::Parser
<x86
>::fileKind(fileContent
);
7042 if ( mach_o::relocatable::Parser
<arm
>::validFile(fileContent
, false, 0) ) {
7043 return mach_o::relocatable::Parser
<arm
>::fileKind(fileContent
);
7049 // Used by archive reader when -ObjC option is specified
7051 bool hasObjC2Categories(const uint8_t* fileContent
)
7053 if ( mach_o::relocatable::Parser
<x86_64
>::validFile(fileContent
) ) {
7054 return mach_o::relocatable::Parser
<x86_64
>::hasObjC2Categories(fileContent
);
7056 else if ( mach_o::relocatable::Parser
<arm
>::validFile(fileContent
, false, 0) ) {
7057 return mach_o::relocatable::Parser
<arm
>::hasObjC2Categories(fileContent
);
7059 else if ( mach_o::relocatable::Parser
<x86
>::validFile(fileContent
, false, 0) ) {
7060 return mach_o::relocatable::Parser
<x86
>::hasObjC2Categories(fileContent
);
7062 #if SUPPORT_ARCH_arm64
7063 else if ( mach_o::relocatable::Parser
<arm64
>::validFile(fileContent
, false, 0) ) {
7064 return mach_o::relocatable::Parser
<arm64
>::hasObjC2Categories(fileContent
);
7071 // Used by archive reader when -ObjC option is specified
7073 bool hasObjC1Categories(const uint8_t* fileContent
)
7075 if ( mach_o::relocatable::Parser
<x86
>::validFile(fileContent
, false, 0) ) {
7076 return mach_o::relocatable::Parser
<x86
>::hasObjC1Categories(fileContent
);
7083 } // namespace relocatable
7084 } // namespace mach_o