1 /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-
3 * Copyright (c) 2009-2010 Apple Inc. All rights reserved.
5 * @APPLE_LICENSE_HEADER_START@
7 * This file contains Original Code and/or Modifications of Original Code
8 * as defined in and that are subject to the Apple Public Source License
9 * Version 2.0 (the 'License'). You may not use this file except in
10 * compliance with the License. Please obtain a copy of the License at
11 * http://www.opensource.apple.com/apsl/ and read it before using this
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
19 * Please see the License for the specific language governing rights and
20 * limitations under the License.
22 * @APPLE_LICENSE_HEADER_END@
31 #include <sys/param.h>
35 #include "MachOFileAbstraction.hpp"
37 #include "libunwind/DwarfInstructions.hpp"
38 #include "libunwind/AddressSpace.hpp"
39 #include "libunwind/Registers.hpp"
45 #include <type_traits>
48 #include "debugline.h"
50 #include "Architectures.hpp"
51 #include "Bitcode.hpp"
53 #include "macho_relocatable_file.h"
57 extern void throwf(const char* format
, ...) __attribute__ ((noreturn
,format(printf
, 1, 2)));
58 extern void warning(const char* format
, ...) __attribute__((format(printf
, 1, 2)));
61 namespace relocatable
{
65 template <typename A
> class Parser
;
66 template <typename A
> class Atom
;
67 template <typename A
> class Section
;
68 template <typename A
> class CFISection
;
69 template <typename A
> class CUSection
;
72 class File
: public ld::relocatable::File
75 File(const char* p
, time_t mTime
, const uint8_t* content
, ld::File::Ordinal ord
) :
76 ld::relocatable::File(p
,mTime
,ord
), _fileContent(content
),
77 _sectionsArray(NULL
), _atomsArray(NULL
),
78 _sectionsArrayCount(0), _atomsArrayCount(0), _aliasAtomsArrayCount(0),
79 _debugInfoKind(ld::relocatable::File::kDebugInfoNone
),
80 _dwarfTranslationUnitPath(NULL
),
81 _dwarfDebugInfoSect(NULL
), _dwarfDebugAbbrevSect(NULL
),
82 _dwarfDebugLineSect(NULL
), _dwarfDebugStringSect(NULL
),
83 _objConstraint(ld::File::objcConstraintNone
),
88 _canScatterAtoms(false),
89 _objcHasCategoryClassPropertiesField(false),
90 _srcKind(kSourceUnknown
) { }
93 // overrides of ld::File
94 virtual bool forEachAtom(ld::File::AtomHandler
&) const;
95 virtual bool justInTimeforEachAtom(const char* name
, ld::File::AtomHandler
&) const
97 virtual uint32_t minOSVersion() const { return _minOSVersion
; }
98 virtual uint32_t platformLoadCommand() const { return _platform
; }
100 // overrides of ld::relocatable::File
101 virtual ObjcConstraint
objCConstraint() const { return _objConstraint
; }
102 virtual bool objcHasCategoryClassPropertiesField() const
103 { return _objcHasCategoryClassPropertiesField
; }
104 virtual uint32_t cpuSubType() const { return _cpuSubType
; }
105 virtual DebugInfoKind
debugInfo() const { return _debugInfoKind
; }
106 virtual const std::vector
<ld::relocatable::File::Stab
>* stabs() const { return &_stabs
; }
107 virtual bool canScatterAtoms() const { return _canScatterAtoms
; }
108 virtual const char* translationUnitSource() const;
109 virtual LinkerOptionsList
* linkerOptions() const { return &_linkerOptions
; }
110 virtual uint8_t swiftVersion() const { return _swiftVersion
; }
111 virtual ld::Bitcode
* getBitcode() const { return _bitcode
.get(); }
112 virtual SourceKind
sourceKind() const { return _srcKind
; }
114 virtual const uint8_t* fileContent() const { return _fileContent
; }
116 friend class Atom
<A
>;
117 friend class Section
<A
>;
118 friend class Parser
<A
>;
119 friend class CFISection
<A
>::OAS
;
121 typedef typename
A::P P
;
123 const uint8_t* _fileContent
;
124 Section
<A
>** _sectionsArray
;
125 uint8_t* _atomsArray
;
126 uint8_t* _aliasAtomsArray
;
127 uint32_t _sectionsArrayCount
;
128 uint32_t _atomsArrayCount
;
129 uint32_t _aliasAtomsArrayCount
;
130 std::vector
<ld::Fixup
> _fixups
;
131 std::vector
<ld::Atom::UnwindInfo
> _unwindInfos
;
132 std::vector
<ld::Atom::LineInfo
> _lineInfos
;
133 std::vector
<ld::relocatable::File::Stab
>_stabs
;
134 ld::relocatable::File::DebugInfoKind _debugInfoKind
;
135 const char* _dwarfTranslationUnitPath
;
136 const macho_section
<P
>* _dwarfDebugInfoSect
;
137 const macho_section
<P
>* _dwarfDebugAbbrevSect
;
138 const macho_section
<P
>* _dwarfDebugLineSect
;
139 const macho_section
<P
>* _dwarfDebugStringSect
;
140 ld::File::ObjcConstraint _objConstraint
;
141 uint8_t _swiftVersion
;
142 uint32_t _cpuSubType
;
143 uint32_t _minOSVersion
;
145 bool _canScatterAtoms
;
146 bool _objcHasCategoryClassPropertiesField
;
147 std::vector
<std::vector
<const char*> > _linkerOptions
;
148 std::unique_ptr
<ld::Bitcode
> _bitcode
;
153 template <typename A
>
154 class Section
: public ld::Section
157 typedef typename
A::P::uint_t pint_t
;
158 typedef typename
A::P P
;
159 typedef typename
A::P::E E
;
161 virtual ~Section() { }
162 class File
<A
>& file() const { return _file
; }
163 const macho_section
<P
>* machoSection() const { return _machOSection
; }
164 uint32_t sectionNum(class Parser
<A
>&) const;
165 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
);
166 virtual ld::Atom::ContentType
contentType() { return ld::Atom::typeUnclassified
; }
167 virtual bool dontDeadStrip() { return (this->_machOSection
->flags() & S_ATTR_NO_DEAD_STRIP
); }
168 virtual bool dontDeadStripIfReferencesLive() { return ( (this->_machOSection
!= NULL
) && (this->_machOSection
->flags() & S_ATTR_LIVE_SUPPORT
) ); }
169 virtual Atom
<A
>* findAtomByAddress(pint_t addr
) { return this->findContentAtomByAddress(addr
, this->_beginAtoms
, this->_endAtoms
); }
170 virtual bool addFollowOnFixups() const { return ! _file
.canScatterAtoms(); }
171 virtual uint32_t appendAtoms(class Parser
<A
>& parser
, uint8_t* buffer
,
172 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
173 const struct Parser
<A
>::CFI_CU_InfoArrays
&) = 0;
174 virtual uint32_t computeAtomCount(class Parser
<A
>& parser
,
175 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
176 const struct Parser
<A
>::CFI_CU_InfoArrays
&) = 0;
177 virtual void makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
&);
178 virtual bool addRelocFixup(class Parser
<A
>& parser
, const macho_relocation_info
<P
>*);
179 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const { return 0; }
180 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
181 const ld::IndirectBindingTable
& ind
) const { return false; }
182 virtual bool ignoreLabel(const char* label
) const { return false; }
183 static const char* makeSectionName(const macho_section
<typename
A::P
>* s
);
186 Section(File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
187 : ld::Section(makeSegmentName(s
), makeSectionName(s
), sectionType(s
)),
188 _file(f
), _machOSection(s
), _beginAtoms(NULL
), _endAtoms(NULL
), _hasAliases(false) { }
189 Section(File
<A
>& f
, const char* segName
, const char* sectName
, ld::Section::Type t
, bool hidden
=false)
190 : ld::Section(segName
, sectName
, t
, hidden
), _file(f
), _machOSection(NULL
),
191 _beginAtoms(NULL
), _endAtoms(NULL
), _hasAliases(false) { }
194 Atom
<A
>* findContentAtomByAddress(pint_t addr
, class Atom
<A
>* start
, class Atom
<A
>* end
);
195 uint32_t x86_64PcRelOffset(uint8_t r_type
);
196 void addLOH(class Parser
<A
>& parser
, int kind
, int count
, const uint64_t addrs
[]);
197 static const char* makeSegmentName(const macho_section
<typename
A::P
>* s
);
198 static bool readable(const macho_section
<typename
A::P
>* s
);
199 static bool writable(const macho_section
<typename
A::P
>* s
);
200 static bool exectuable(const macho_section
<typename
A::P
>* s
);
201 static ld::Section::Type
sectionType(const macho_section
<typename
A::P
>* s
);
204 const macho_section
<P
>* _machOSection
;
205 class Atom
<A
>* _beginAtoms
;
206 class Atom
<A
>* _endAtoms
;
208 std::set
<const class Atom
<A
>*> _altEntries
;
212 template <typename A
>
213 class CFISection
: public Section
<A
>
216 CFISection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
217 : Section
<A
>(f
, s
) { }
218 uint32_t cfiCount(Parser
<A
>& parser
);
220 virtual ld::Atom::ContentType
contentType() { return ld::Atom::typeCFI
; }
221 virtual uint32_t computeAtomCount(class Parser
<A
>& parser
, struct Parser
<A
>::LabelAndCFIBreakIterator
& it
, const struct Parser
<A
>::CFI_CU_InfoArrays
&);
222 virtual uint32_t appendAtoms(class Parser
<A
>& parser
, uint8_t* buffer
, struct Parser
<A
>::LabelAndCFIBreakIterator
& it
, const struct Parser
<A
>::CFI_CU_InfoArrays
&);
223 virtual void makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
&);
224 virtual bool addFollowOnFixups() const { return false; }
228 /// ObjectFileAddressSpace is used as a template parameter to UnwindCursor for parsing
229 /// dwarf CFI information in an object file.
234 typedef typename
A::P::uint_t pint_t
;
235 typedef typename
A::P P
;
236 typedef typename
A::P::E E
;
237 typedef typename
A::P::uint_t sint_t
;
239 OAS(CFISection
<A
>& ehFrameSection
, const uint8_t* ehFrameBuffer
) :
240 _ehFrameSection(ehFrameSection
),
241 _ehFrameContent(ehFrameBuffer
),
242 _ehFrameStartAddr(ehFrameSection
.machoSection()->addr()),
243 _ehFrameEndAddr(ehFrameSection
.machoSection()->addr()+ehFrameSection
.machoSection()->size()) {}
245 uint8_t get8(pint_t addr
) { return *((uint8_t*)mappedAddress(addr
)); }
246 uint16_t get16(pint_t addr
) { return E::get16(*((uint16_t*)mappedAddress(addr
))); }
247 uint32_t get32(pint_t addr
) { return E::get32(*((uint32_t*)mappedAddress(addr
))); }
248 uint64_t get64(pint_t addr
) { return E::get64(*((uint64_t*)mappedAddress(addr
))); }
249 pint_t
getP(pint_t addr
) { return P::getP(*((pint_t
*)mappedAddress(addr
))); }
250 uint64_t getULEB128(pint_t
& addr
, pint_t end
);
251 int64_t getSLEB128(pint_t
& addr
, pint_t end
);
252 pint_t
getEncodedP(pint_t
& addr
, pint_t end
, uint8_t encoding
);
254 const void* mappedAddress(pint_t addr
);
256 CFISection
<A
>& _ehFrameSection
;
257 const uint8_t* _ehFrameContent
;
258 pint_t _ehFrameStartAddr
;
259 pint_t _ehFrameEndAddr
;
263 typedef typename
A::P::uint_t pint_t
;
264 typedef libunwind::CFI_Atom_Info
<OAS
> CFI_Atom_Info
;
266 void cfiParse(class Parser
<A
>& parser
, uint8_t* buffer
, CFI_Atom_Info cfiArray
[], uint32_t& cfiCount
, const pint_t cuStarts
[], uint32_t cuCount
);
267 bool needsRelocating();
269 static bool bigEndian();
271 void addCiePersonalityFixups(class Parser
<A
>& parser
, const CFI_Atom_Info
* cieInfo
);
272 static void warnFunc(void* ref
, uint64_t funcAddr
, const char* msg
);
276 template <typename A
>
277 class CUSection
: public Section
<A
>
280 CUSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
281 : Section
<A
>(f
, s
) { }
283 typedef typename
A::P::uint_t pint_t
;
284 typedef typename
A::P P
;
285 typedef typename
A::P::E E
;
287 virtual uint32_t computeAtomCount(class Parser
<A
>& parser
, struct Parser
<A
>::LabelAndCFIBreakIterator
& it
, const struct Parser
<A
>::CFI_CU_InfoArrays
&) { return 0; }
288 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; }
289 virtual void makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
&);
290 virtual bool addFollowOnFixups() const { return false; }
293 pint_t functionStartAddress
;
294 uint32_t functionSymbolIndex
;
295 uint32_t rangeLength
;
296 uint32_t compactUnwindInfo
;
297 const char* personality
;
304 void parse(class Parser
<A
>& parser
, uint32_t cnt
, Info array
[]);
305 static bool encodingMeansUseDwarf(compact_unwind_encoding_t enc
);
310 const char* personalityName(class Parser
<A
>& parser
, const macho_relocation_info
<P
>* reloc
);
312 static int infoSorter(const void* l
, const void* r
);
317 template <typename A
>
318 class TentativeDefinitionSection
: public Section
<A
>
321 TentativeDefinitionSection(Parser
<A
>& parser
, File
<A
>& f
)
322 : Section
<A
>(f
, "__DATA", "__comm/tent", ld::Section::typeTentativeDefs
) {}
324 virtual ld::Atom::ContentType
contentType() { return ld::Atom::typeZeroFill
; }
325 virtual bool addFollowOnFixups() const { return false; }
326 virtual Atom
<A
>* findAtomByAddress(typename
A::P::uint_t addr
) { throw "TentativeDefinitionSection::findAtomByAddress() should never be called"; }
327 virtual uint32_t computeAtomCount(class Parser
<A
>& parser
, struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
328 const struct Parser
<A
>::CFI_CU_InfoArrays
&);
329 virtual uint32_t appendAtoms(class Parser
<A
>& parser
, uint8_t* buffer
,
330 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
331 const struct Parser
<A
>::CFI_CU_InfoArrays
&);
332 virtual void makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
&) {}
334 typedef typename
A::P::uint_t pint_t
;
335 typedef typename
A::P P
;
339 template <typename A
>
340 class AbsoluteSymbolSection
: public Section
<A
>
343 AbsoluteSymbolSection(Parser
<A
>& parser
, File
<A
>& f
)
344 : Section
<A
>(f
, "__DATA", "__abs", ld::Section::typeAbsoluteSymbols
, true) {}
346 virtual ld::Atom::ContentType
contentType() { return ld::Atom::typeUnclassified
; }
347 virtual bool dontDeadStrip() { return false; }
348 virtual ld::Atom::Alignment
alignmentForAddress(typename
A::P::uint_t addr
) { return ld::Atom::Alignment(0); }
349 virtual bool addFollowOnFixups() const { return false; }
350 virtual Atom
<A
>* findAtomByAddress(typename
A::P::uint_t addr
) { throw "AbsoluteSymbolSection::findAtomByAddress() should never be called"; }
351 virtual uint32_t computeAtomCount(class Parser
<A
>& parser
, struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
352 const struct Parser
<A
>::CFI_CU_InfoArrays
&);
353 virtual uint32_t appendAtoms(class Parser
<A
>& parser
, uint8_t* buffer
,
354 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
355 const struct Parser
<A
>::CFI_CU_InfoArrays
&);
356 virtual void makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
&) {}
357 virtual Atom
<A
>* findAbsAtomForValue(typename
A::P::uint_t
);
360 typedef typename
A::P::uint_t pint_t
;
361 typedef typename
A::P P
;
365 template <typename A
>
366 class SymboledSection
: public Section
<A
>
369 SymboledSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
);
370 virtual ld::Atom::ContentType
contentType() { return _type
; }
371 virtual bool dontDeadStrip();
372 virtual uint32_t computeAtomCount(class Parser
<A
>& parser
, struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
373 const struct Parser
<A
>::CFI_CU_InfoArrays
&);
374 virtual uint32_t appendAtoms(class Parser
<A
>& parser
, uint8_t* buffer
,
375 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
376 const struct Parser
<A
>::CFI_CU_InfoArrays
&);
378 typedef typename
A::P::uint_t pint_t
;
379 typedef typename
A::P P
;
381 ld::Atom::ContentType _type
;
385 template <typename A
>
386 class TLVDefsSection
: public SymboledSection
<A
>
389 TLVDefsSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
) :
390 SymboledSection
<A
>(parser
, f
, s
) { }
392 typedef typename
A::P::uint_t pint_t
;
394 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
) { return ld::Atom::Alignment(log2(sizeof(pint_t
))); }
401 template <typename A
>
402 class ImplicitSizeSection
: public Section
<A
>
405 ImplicitSizeSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
406 : Section
<A
>(f
, s
) { }
407 virtual uint32_t computeAtomCount(class Parser
<A
>& parser
, struct Parser
<A
>::LabelAndCFIBreakIterator
& it
, const struct Parser
<A
>::CFI_CU_InfoArrays
&);
408 virtual uint32_t appendAtoms(class Parser
<A
>& parser
, uint8_t* buffer
, struct Parser
<A
>::LabelAndCFIBreakIterator
& it
, const struct Parser
<A
>::CFI_CU_InfoArrays
&);
410 typedef typename
A::P::uint_t pint_t
;
411 typedef typename
A::P P
;
413 virtual bool addFollowOnFixups() const { return false; }
414 virtual const char* unlabeledAtomName(Parser
<A
>& parser
, pint_t addr
) = 0;
415 virtual ld::Atom::SymbolTableInclusion
symbolTableInclusion();
416 virtual pint_t
elementSizeAtAddress(pint_t addr
) = 0;
417 virtual ld::Atom::Scope
scopeAtAddress(Parser
<A
>& parser
, pint_t addr
) { return ld::Atom::scopeLinkageUnit
; }
418 virtual bool useElementAt(Parser
<A
>& parser
,
419 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
, pint_t addr
) = 0;
420 virtual ld::Atom::Definition
definition() { return ld::Atom::definitionRegular
; }
421 virtual ld::Atom::Combine
combine(Parser
<A
>& parser
, pint_t addr
) = 0;
422 virtual bool ignoreLabel(const char* label
) const { return (label
[0] == 'L'); }
426 template <typename A
>
427 class FixedSizeSection
: public ImplicitSizeSection
<A
>
430 FixedSizeSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
431 : ImplicitSizeSection
<A
>(parser
, f
, s
) { }
433 typedef typename
A::P::uint_t pint_t
;
434 typedef typename
A::P P
;
435 typedef typename
A::P::E E
;
437 virtual bool useElementAt(Parser
<A
>& parser
,
438 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
, pint_t addr
)
443 template <typename A
>
444 class Literal4Section
: public FixedSizeSection
<A
>
447 Literal4Section(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
448 : FixedSizeSection
<A
>(parser
, f
, s
) {}
450 typedef typename
A::P::uint_t pint_t
;
451 typedef typename
A::P P
;
453 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
) { return ld::Atom::Alignment(2); }
454 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "4-byte-literal"; }
455 virtual pint_t
elementSizeAtAddress(pint_t addr
) { return 4; }
456 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
) { return ld::Atom::combineByNameAndContent
; }
457 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
458 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
459 const ld::IndirectBindingTable
& ind
) const;
460 virtual bool ignoreLabel(const char* label
) const;
463 template <typename A
>
464 class Literal8Section
: public FixedSizeSection
<A
>
467 Literal8Section(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
468 : FixedSizeSection
<A
>(parser
, f
, s
) {}
470 typedef typename
A::P::uint_t pint_t
;
471 typedef typename
A::P P
;
473 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
) { return ld::Atom::Alignment(3); }
474 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "8-byte-literal"; }
475 virtual pint_t
elementSizeAtAddress(pint_t addr
) { return 8; }
476 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
) { return ld::Atom::combineByNameAndContent
; }
477 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
478 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
479 const ld::IndirectBindingTable
& ind
) const;
480 virtual bool ignoreLabel(const char* label
) const;
483 template <typename A
>
484 class Literal16Section
: public FixedSizeSection
<A
>
487 Literal16Section(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
488 : FixedSizeSection
<A
>(parser
, f
, s
) {}
490 typedef typename
A::P::uint_t pint_t
;
491 typedef typename
A::P P
;
493 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
) { return ld::Atom::Alignment(4); }
494 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "16-byte-literal"; }
495 virtual pint_t
elementSizeAtAddress(pint_t addr
) { return 16; }
496 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
) { return ld::Atom::combineByNameAndContent
; }
497 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
498 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
499 const ld::IndirectBindingTable
& ind
) const;
500 virtual bool ignoreLabel(const char* label
) const;
504 template <typename A
>
505 class NonLazyPointerSection
: public FixedSizeSection
<A
>
508 NonLazyPointerSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
509 : FixedSizeSection
<A
>(parser
, f
, s
) {}
511 typedef typename
A::P::uint_t pint_t
;
512 typedef typename
A::P P
;
514 virtual void makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
&);
515 virtual ld::Atom::ContentType
contentType() { return ld::Atom::typeNonLazyPointer
; }
516 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
) { return ld::Atom::Alignment(log2(sizeof(pint_t
))); }
517 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "non_lazy_ptr"; }
518 virtual pint_t
elementSizeAtAddress(pint_t addr
) { return sizeof(pint_t
); }
519 virtual ld::Atom::Scope
scopeAtAddress(Parser
<A
>& parser
, pint_t addr
);
520 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
);
521 virtual bool ignoreLabel(const char* label
) const { return true; }
522 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
523 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
524 const ld::IndirectBindingTable
& ind
) const;
527 static const char* targetName(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
);
528 static ld::Fixup::Kind
fixupKind();
531 template <typename A
>
532 class TLVPointerSection
: public FixedSizeSection
<A
>
535 TLVPointerSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
536 : FixedSizeSection
<A
>(parser
, f
, s
) {}
538 typedef typename
A::P::uint_t pint_t
;
539 typedef typename
A::P P
;
541 virtual void makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
&);
542 virtual ld::Atom::ContentType
contentType() { return ld::Atom::typeTLVPointer
; }
543 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
) { return ld::Atom::Alignment(log2(sizeof(pint_t
))); }
544 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "tlv_lazy_ptr"; }
545 virtual pint_t
elementSizeAtAddress(pint_t addr
) { return sizeof(pint_t
); }
546 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
);
547 virtual bool ignoreLabel(const char* label
) const { return true; }
548 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
549 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
550 const ld::IndirectBindingTable
& ind
) const;
553 static const char* targetName(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
, bool* isStatic
);
557 template <typename A
>
558 class CFStringSection
: public FixedSizeSection
<A
>
561 CFStringSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
562 : FixedSizeSection
<A
>(parser
, f
, s
) {}
564 typedef typename
A::P::uint_t pint_t
;
566 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
) { return ld::Atom::Alignment(log2(sizeof(pint_t
))); }
567 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "CFString"; }
568 virtual pint_t
elementSizeAtAddress(pint_t addr
) { return 4*sizeof(pint_t
); }
569 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
) { return ld::Atom::combineByNameAndReferences
; }
570 virtual bool ignoreLabel(const char* label
) const { return true; }
571 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
572 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
573 const ld::IndirectBindingTable
& ind
) const;
575 enum ContentType
{ contentUTF8
, contentUTF16
, contentUnknown
};
576 static const uint8_t* targetContent(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
,
577 ContentType
* ct
, unsigned int* count
);
581 template <typename A
>
582 class ObjC1ClassSection
: public FixedSizeSection
<A
>
585 ObjC1ClassSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
586 : FixedSizeSection
<A
>(parser
, f
, s
) {}
588 typedef typename
A::P::uint_t pint_t
;
589 typedef typename
A::P P
;
590 typedef typename
A::P::E E
;
592 virtual ld::Atom::Scope
scopeAtAddress(Parser
<A
>& , pint_t
) { return ld::Atom::scopeGlobal
; }
593 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
) { return ld::Atom::Alignment(2); }
594 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
);
595 virtual ld::Atom::SymbolTableInclusion
symbolTableInclusion() { return ld::Atom::symbolTableIn
; }
596 virtual pint_t
elementSizeAtAddress(pint_t addr
);
597 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
) { return ld::Atom::combineNever
; }
598 virtual bool ignoreLabel(const char* label
) const { return true; }
599 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
601 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
602 const ld::IndirectBindingTable
& ind
) const { return false; }
603 virtual bool addRelocFixup(class Parser
<A
>& parser
, const macho_relocation_info
<P
>*);
607 template <typename A
>
608 class ObjC2ClassRefsSection
: public FixedSizeSection
<A
>
611 ObjC2ClassRefsSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
612 : FixedSizeSection
<A
>(parser
, f
, s
) {}
614 typedef typename
A::P::uint_t pint_t
;
616 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
) { return ld::Atom::Alignment(log2(sizeof(pint_t
))); }
617 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "objc-class-ref"; }
618 virtual pint_t
elementSizeAtAddress(pint_t addr
) { return sizeof(pint_t
); }
619 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
) { return ld::Atom::combineByNameAndReferences
; }
620 virtual bool ignoreLabel(const char* label
) const { return true; }
621 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
622 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
623 const ld::IndirectBindingTable
& ind
) const;
625 const char* targetClassName(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
629 template <typename A
>
630 class ObjC2CategoryListSection
: public FixedSizeSection
<A
>
633 ObjC2CategoryListSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
634 : FixedSizeSection
<A
>(parser
, f
, s
) {}
636 typedef typename
A::P::uint_t pint_t
;
638 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
) { return ld::Atom::Alignment(log2(sizeof(pint_t
))); }
639 virtual ld::Atom::Scope
scopeAtAddress(Parser
<A
>& parser
, pint_t addr
) { return ld::Atom::scopeTranslationUnit
; }
640 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "objc-cat-list"; }
641 virtual pint_t
elementSizeAtAddress(pint_t addr
) { return sizeof(pint_t
); }
642 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
) { return ld::Atom::combineNever
; }
643 virtual bool ignoreLabel(const char* label
) const { return true; }
645 const char* targetClassName(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
649 template <typename A
>
650 class PointerToCStringSection
: public FixedSizeSection
<A
>
653 PointerToCStringSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
654 : FixedSizeSection
<A
>(parser
, f
, s
) {}
656 typedef typename
A::P::uint_t pint_t
;
658 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
) { return ld::Atom::Alignment(log2(sizeof(pint_t
))); }
659 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "pointer-to-literal-cstring"; }
660 virtual pint_t
elementSizeAtAddress(pint_t addr
) { return sizeof(pint_t
); }
661 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
) { return ld::Atom::combineByNameAndReferences
; }
662 virtual bool ignoreLabel(const char* label
) const { return true; }
663 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
664 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
665 const ld::IndirectBindingTable
& ind
) const;
666 virtual const char* targetCString(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
670 template <typename A
>
671 class Objc1ClassReferences
: public PointerToCStringSection
<A
>
674 Objc1ClassReferences(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
675 : PointerToCStringSection
<A
>(parser
, f
, s
) {}
677 typedef typename
A::P::uint_t pint_t
;
678 typedef typename
A::P P
;
680 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "pointer-to-literal-objc-class-name"; }
681 virtual bool addRelocFixup(class Parser
<A
>& parser
, const macho_relocation_info
<P
>*);
682 virtual const char* targetCString(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
686 template <typename A
>
687 class CStringSection
: public ImplicitSizeSection
<A
>
690 CStringSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
691 : ImplicitSizeSection
<A
>(parser
, f
, s
) {}
693 typedef typename
A::P::uint_t pint_t
;
694 typedef typename
A::P P
;
696 virtual ld::Atom::ContentType
contentType() { return ld::Atom::typeCString
; }
697 virtual Atom
<A
>* findAtomByAddress(pint_t addr
);
698 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "cstring"; }
699 virtual pint_t
elementSizeAtAddress(pint_t addr
);
700 virtual bool ignoreLabel(const char* label
) const;
701 virtual bool useElementAt(Parser
<A
>& parser
,
702 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
, pint_t addr
);
703 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
) { return ld::Atom::combineByNameAndContent
; }
704 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
705 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
706 const ld::IndirectBindingTable
& ind
) const;
711 template <typename A
>
712 class UTF16StringSection
: public SymboledSection
<A
>
715 UTF16StringSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
716 : SymboledSection
<A
>(parser
, f
, s
) {}
718 typedef typename
A::P::uint_t pint_t
;
719 typedef typename
A::P P
;
721 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
) { return ld::Atom::combineByNameAndContent
; }
722 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
723 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
724 const ld::IndirectBindingTable
& ind
) const;
729 // Atoms in mach-o files
731 template <typename A
>
732 class Atom
: public ld::Atom
735 // overrides of ld::Atom
736 virtual const ld::File
* file() const;
737 virtual const char* translationUnitSource() const
738 { return sect().file().translationUnitSource(); }
739 virtual const char* name() const { return _name
; }
740 virtual uint64_t size() const { return _size
; }
741 virtual uint64_t objectAddress() const { return _objAddress
; }
742 virtual void copyRawContent(uint8_t buffer
[]) const;
743 virtual const uint8_t* rawContentPointer() const { return contentPointer(); }
744 virtual unsigned long contentHash(const ld::IndirectBindingTable
& ind
) const
745 { if ( _hash
== 0 ) _hash
= sect().contentHash(this, ind
); return _hash
; }
746 virtual bool canCoalesceWith(const ld::Atom
& rhs
, const ld::IndirectBindingTable
& ind
) const
747 { return sect().canCoalesceWith(this, rhs
, ind
); }
748 virtual ld::Fixup::iterator
fixupsBegin() const { return &machofile()._fixups
[_fixupsStartIndex
]; }
749 virtual ld::Fixup::iterator
fixupsEnd() const { return &machofile()._fixups
[_fixupsStartIndex
+_fixupsCount
]; }
750 virtual ld::Atom::UnwindInfo::iterator
beginUnwind() const { return &machofile()._unwindInfos
[_unwindInfoStartIndex
]; }
751 virtual ld::Atom::UnwindInfo::iterator
endUnwind() const { return &machofile()._unwindInfos
[_unwindInfoStartIndex
+_unwindInfoCount
]; }
752 virtual ld::Atom::LineInfo::iterator
beginLineInfo() const{ return &machofile()._lineInfos
[_lineInfoStartIndex
]; }
753 virtual ld::Atom::LineInfo::iterator
endLineInfo() const { return &machofile()._lineInfos
[_lineInfoStartIndex
+_lineInfoCount
]; }
754 virtual void setFile(const ld::File
* f
);
758 enum { kFixupStartIndexBits
= 32,
759 kLineInfoStartIndexBits
= 32,
760 kUnwindInfoStartIndexBits
= 24,
761 kFixupCountBits
= 24,
762 kLineInfoCountBits
= 12,
763 kUnwindInfoCountBits
= 4
764 }; // must sum to 128
767 // methods for all atoms from mach-o object file
768 Section
<A
>& sect() const { return (Section
<A
>&)section(); }
769 File
<A
>& machofile() const { return ((Section
<A
>*)(this->_section
))->file(); }
770 void setFixupsRange(uint32_t s
, uint32_t c
);
771 void setUnwindInfoRange(uint32_t s
, uint32_t c
);
772 void extendUnwindInfoRange();
773 void setLineInfoRange(uint32_t s
, uint32_t c
);
774 bool roomForMoreLineInfoCount() { return (_lineInfoCount
< ((1<<kLineInfoCountBits
)-1)); }
775 void incrementLineInfoCount() { assert(roomForMoreLineInfoCount()); ++_lineInfoCount
; }
776 void incrementFixupCount() { if (_fixupsCount
== ((1 << kFixupCountBits
)-1))
777 throwf("too may fixups in %s", name()); ++_fixupsCount
; }
778 const uint8_t* contentPointer() const;
779 uint32_t fixupCount() const { return _fixupsCount
; }
780 void verifyAlignment(const macho_section
<typename
A::P
>&) const;
782 typedef typename
A::P P
;
783 typedef typename
A::P::E E
;
784 typedef typename
A::P::uint_t pint_t
;
785 // constuct via all attributes
786 Atom(Section
<A
>& sct
, const char* nm
, pint_t addr
, uint64_t sz
,
787 ld::Atom::Definition d
, ld::Atom::Combine c
, ld::Atom::Scope s
,
788 ld::Atom::ContentType ct
, ld::Atom::SymbolTableInclusion i
,
789 bool dds
, bool thumb
, bool al
, ld::Atom::Alignment a
)
790 : ld::Atom((ld::Section
&)sct
, d
, c
, s
, ct
, i
, dds
, thumb
, al
, a
),
791 _size(sz
), _objAddress(addr
), _name(nm
), _hash(0),
792 _fixupsStartIndex(0), _lineInfoStartIndex(0),
793 _unwindInfoStartIndex(0), _fixupsCount(0),
794 _lineInfoCount(0), _unwindInfoCount(0) { }
795 // construct via symbol table entry
796 Atom(Section
<A
>& sct
, Parser
<A
>& parser
, const macho_nlist
<P
>& sym
,
797 uint64_t sz
, bool alias
=false)
798 : ld::Atom((ld::Section
&)sct
, parser
.definitionFromSymbol(sym
),
799 parser
.combineFromSymbol(sym
), parser
.scopeFromSymbol(sym
),
800 parser
.resolverFromSymbol(sym
) ? ld::Atom::typeResolver
: sct
.contentType(),
801 parser
.inclusionFromSymbol(sym
),
802 (parser
.dontDeadStripFromSymbol(sym
) && !sct
.dontDeadStripIfReferencesLive()) || sct
.dontDeadStrip(),
803 parser
.isThumbFromSymbol(sym
), alias
,
804 sct
.alignmentForAddress(sym
.n_value())),
805 _size(sz
), _objAddress(sym
.n_value()),
806 _name(parser
.nameFromSymbol(sym
)), _hash(0),
807 _fixupsStartIndex(0), _lineInfoStartIndex(0),
808 _unwindInfoStartIndex(0), _fixupsCount(0),
809 _lineInfoCount(0), _unwindInfoCount(0) {
810 // <rdar://problem/6783167> support auto-hidden weak symbols
811 if ( _scope
== ld::Atom::scopeGlobal
&&
812 (sym
.n_desc() & (N_WEAK_DEF
|N_WEAK_REF
)) == (N_WEAK_DEF
|N_WEAK_REF
) )
814 this->verifyAlignment(*sct
.machoSection());
815 if ( sct
.dontDeadStripIfReferencesLive() )
816 this->setDontDeadStripIfReferencesLive();
820 friend class Parser
<A
>;
821 friend class Section
<A
>;
822 friend class CStringSection
<A
>;
823 friend class AbsoluteSymbolSection
<A
>;
828 mutable unsigned long _hash
;
830 uint64_t _fixupsStartIndex
: kFixupStartIndexBits
,
831 _lineInfoStartIndex
: kLineInfoStartIndexBits
,
832 _unwindInfoStartIndex
: kUnwindInfoStartIndexBits
,
833 _fixupsCount
: kFixupCountBits
,
834 _lineInfoCount
: kLineInfoCountBits
,
835 _unwindInfoCount
: kUnwindInfoCountBits
;
837 static std::map
<const ld::Atom
*, const ld::File
*> _s_fileOverride
;
840 template <typename A
>
841 std::map
<const ld::Atom
*, const ld::File
*> Atom
<A
>::_s_fileOverride
;
843 template <typename A
>
844 void Atom
<A
>::setFile(const ld::File
* f
) {
845 _s_fileOverride
[this] = f
;
848 template <typename A
>
849 const ld::File
* Atom
<A
>::file() const
851 std::map
<const ld::Atom
*, const ld::File
*>::iterator pos
= _s_fileOverride
.find(this);
852 if ( pos
!= _s_fileOverride
.end() )
855 return §().file();
858 template <typename A
>
859 void Atom
<A
>::setFixupsRange(uint32_t startIndex
, uint32_t count
)
861 if ( count
>= (1 << kFixupCountBits
) )
862 throwf("too many fixups in function %s", this->name());
863 if ( startIndex
>= (1 << kFixupStartIndexBits
) )
864 throwf("too many fixups in file");
865 assert(((startIndex
+count
) <= sect().file()._fixups
.size()) && "fixup index out of range");
866 _fixupsStartIndex
= startIndex
;
867 _fixupsCount
= count
;
870 template <typename A
>
871 void Atom
<A
>::setUnwindInfoRange(uint32_t startIndex
, uint32_t count
)
873 if ( count
>= (1 << kUnwindInfoCountBits
) )
874 throwf("too many compact unwind infos in function %s", this->name());
875 if ( startIndex
>= (1 << kUnwindInfoStartIndexBits
) )
876 throwf("too many compact unwind infos (%d) in file", startIndex
);
877 assert((startIndex
+count
) <= sect().file()._unwindInfos
.size() && "unwindinfo index out of range");
878 _unwindInfoStartIndex
= startIndex
;
879 _unwindInfoCount
= count
;
882 template <typename A
>
883 void Atom
<A
>::extendUnwindInfoRange()
885 if ( _unwindInfoCount
+1 >= (1 << kUnwindInfoCountBits
) )
886 throwf("too many compact unwind infos in function %s", this->name());
887 _unwindInfoCount
+= 1;
890 template <typename A
>
891 void Atom
<A
>::setLineInfoRange(uint32_t startIndex
, uint32_t count
)
893 assert((count
< (1 << kLineInfoCountBits
)) && "too many line infos");
894 assert((startIndex
+count
) < sect().file()._lineInfos
.size() && "line info index out of range");
895 _lineInfoStartIndex
= startIndex
;
896 _lineInfoCount
= count
;
899 template <typename A
>
900 const uint8_t* Atom
<A
>::contentPointer() const
902 const macho_section
<P
>* sct
= this->sect().machoSection();
903 if ( this->_objAddress
> sct
->addr() + sct
->size() )
904 throwf("malformed .o file, symbol has address 0x%0llX which is outside range of its section", (uint64_t)this->_objAddress
);
905 uint32_t fileOffset
= sct
->offset() - sct
->addr() + this->_objAddress
;
906 return this->sect().file().fileContent()+fileOffset
;
910 template <typename A
>
911 void Atom
<A
>::copyRawContent(uint8_t buffer
[]) const
914 if ( this->contentType() == ld::Atom::typeZeroFill
) {
915 bzero(buffer
, _size
);
917 else if ( _size
!= 0 ) {
918 memcpy(buffer
, this->contentPointer(), _size
);
923 void Atom
<arm
>::verifyAlignment(const macho_section
<P
>&) const
925 if ( (this->section().type() == ld::Section::typeCode
) && ! isThumb() ) {
926 if ( ((_objAddress
% 4) != 0) || (this->alignment().powerOf2
< 2) )
927 warning("ARM function not 4-byte aligned: %s from %s", this->name(), this->file()->path());
931 #if SUPPORT_ARCH_arm64
933 void Atom
<arm64
>::verifyAlignment(const macho_section
<P
>& sect
) const
935 if ( (this->section().type() == ld::Section::typeCode
) && (sect
.size() != 0) ) {
936 if ( ((_objAddress
% 4) != 0) || (this->alignment().powerOf2
< 2) )
937 warning("arm64 function not 4-byte aligned: %s from %s", this->name(), this->file()->path());
943 template <typename A
>
944 void Atom
<A
>::verifyAlignment(const macho_section
<P
>&) const
949 class AliasAtom
: public ld::Atom
952 AliasAtom(const char* name
, bool hidden
, const ld::File
* file
, const char* aliasOfName
) :
953 ld::Atom(_s_section
, ld::Atom::definitionRegular
, ld::Atom::combineNever
,
954 (hidden
? ld::Atom::scopeLinkageUnit
: ld::Atom::scopeGlobal
),
955 ld::Atom::typeUnclassified
, ld::Atom::symbolTableIn
,
956 false, false, true, 0),
959 _fixup(0, ld::Fixup::k1of1
, ld::Fixup::kindNoneFollowOn
, ld::Fixup::bindingByNameUnbound
, aliasOfName
) { }
961 virtual const ld::File
* file() const { return _file
; }
962 virtual const char* translationUnitSource() const
964 virtual const char* name() const { return _name
; }
965 virtual uint64_t size() const { return 0; }
966 virtual uint64_t objectAddress() const { return 0; }
967 virtual void copyRawContent(uint8_t buffer
[]) const { }
968 virtual ld::Fixup::iterator
fixupsBegin() const { return &((ld::Fixup
*)&_fixup
)[0]; }
969 virtual ld::Fixup::iterator
fixupsEnd() const { return &((ld::Fixup
*)&_fixup
)[1]; }
972 static ld::Section _s_section
;
974 const ld::File
* _file
;
979 ld::Section
AliasAtom::_s_section("__LD", "__aliases", ld::Section::typeTempAlias
, true);
982 template <typename A
>
986 static bool validFile(const uint8_t* fileContent
, bool subtypeMustMatch
=false,
987 cpu_subtype_t subtype
=0);
988 static const char* fileKind(const uint8_t* fileContent
);
989 static Options::Platform
findPlatform(const macho_header
<typename
A::P
>* header
);
990 static bool hasObjC2Categories(const uint8_t* fileContent
);
991 static bool hasObjC1Categories(const uint8_t* fileContent
);
992 static bool getNonLocalSymbols(const uint8_t* fileContnet
, std::vector
<const char*> &syms
);
993 static ld::relocatable::File
* parse(const uint8_t* fileContent
, uint64_t fileLength
,
994 const char* path
, time_t modTime
, ld::File::Ordinal ordinal
,
995 const ParserOptions
& opts
) {
996 Parser
p(fileContent
, fileLength
, path
, modTime
,
997 ordinal
, opts
.warnUnwindConversionProblems
,
998 opts
.keepDwarfUnwind
, opts
.forceDwarfConversion
,
999 opts
.neverConvertDwarf
, opts
.verboseOptimizationHints
,
1000 opts
.ignoreMismatchPlatform
);
1001 return p
.parse(opts
);
1004 typedef typename
A::P P
;
1005 typedef typename
A::P::E E
;
1006 typedef typename
A::P::uint_t pint_t
;
1008 struct SourceLocation
{
1010 SourceLocation(Atom
<A
>* a
, uint32_t o
) : atom(a
), offsetInAtom(o
) {}
1012 uint32_t offsetInAtom
;
1017 const char* name
; // only used if targetAtom is NULL
1019 bool weakImport
; // only used if targetAtom is NULL
1022 struct FixupInAtom
{
1023 FixupInAtom(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, Atom
<A
>* target
) :
1024 fixup(src
.offsetInAtom
, c
, k
, target
), atom(src
.atom
) { src
.atom
->incrementFixupCount(); }
1026 FixupInAtom(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, ld::Fixup::TargetBinding b
, Atom
<A
>* target
) :
1027 fixup(src
.offsetInAtom
, c
, k
, b
, target
), atom(src
.atom
) { src
.atom
->incrementFixupCount(); }
1029 FixupInAtom(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, bool wi
, const char* name
) :
1030 fixup(src
.offsetInAtom
, c
, k
, wi
, name
), atom(src
.atom
) { src
.atom
->incrementFixupCount(); }
1032 FixupInAtom(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, ld::Fixup::TargetBinding b
, const char* name
) :
1033 fixup(src
.offsetInAtom
, c
, k
, b
, name
), atom(src
.atom
) { src
.atom
->incrementFixupCount(); }
1035 FixupInAtom(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, uint64_t addend
) :
1036 fixup(src
.offsetInAtom
, c
, k
, addend
), atom(src
.atom
) { src
.atom
->incrementFixupCount(); }
1038 FixupInAtom(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
) :
1039 fixup(src
.offsetInAtom
, c
, k
, (uint64_t)0), atom(src
.atom
) { src
.atom
->incrementFixupCount(); }
1045 void addFixup(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, Atom
<A
>* target
) {
1046 _allFixups
.push_back(FixupInAtom(src
, c
, k
, target
));
1049 void addFixup(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, ld::Fixup::TargetBinding b
, Atom
<A
>* target
) {
1050 _allFixups
.push_back(FixupInAtom(src
, c
, k
, b
, target
));
1053 void addFixup(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, bool wi
, const char* name
) {
1054 _allFixups
.push_back(FixupInAtom(src
, c
, k
, wi
, name
));
1057 void addFixup(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, ld::Fixup::TargetBinding b
, const char* name
) {
1058 _allFixups
.push_back(FixupInAtom(src
, c
, k
, b
, name
));
1061 void addFixup(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, uint64_t addend
) {
1062 _allFixups
.push_back(FixupInAtom(src
, c
, k
, addend
));
1065 void addFixup(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
) {
1066 _allFixups
.push_back(FixupInAtom(src
, c
, k
));
1069 const char* path() { return _path
; }
1070 uint32_t symbolCount() { return _symbolCount
; }
1071 uint32_t indirectSymbol(uint32_t indirectIndex
);
1072 const macho_nlist
<P
>& symbolFromIndex(uint32_t index
);
1073 const char* nameFromSymbol(const macho_nlist
<P
>& sym
);
1074 ld::Atom::Scope
scopeFromSymbol(const macho_nlist
<P
>& sym
);
1075 static ld::Atom::Definition
definitionFromSymbol(const macho_nlist
<P
>& sym
);
1076 static ld::Atom::Combine
combineFromSymbol(const macho_nlist
<P
>& sym
);
1077 ld::Atom::SymbolTableInclusion
inclusionFromSymbol(const macho_nlist
<P
>& sym
);
1078 static bool dontDeadStripFromSymbol(const macho_nlist
<P
>& sym
);
1079 static bool isThumbFromSymbol(const macho_nlist
<P
>& sym
);
1080 static bool weakImportFromSymbol(const macho_nlist
<P
>& sym
);
1081 static bool resolverFromSymbol(const macho_nlist
<P
>& sym
);
1082 static bool altEntryFromSymbol(const macho_nlist
<P
>& sym
);
1083 uint32_t symbolIndexFromIndirectSectionAddress(pint_t
,const macho_section
<P
>*);
1084 const macho_section
<P
>* firstMachOSection() { return _sectionsStart
; }
1085 const macho_section
<P
>* machOSectionFromSectionIndex(uint32_t index
);
1086 uint32_t machOSectionCount() { return _machOSectionsCount
; }
1087 uint32_t undefinedStartIndex() { return _undefinedStartIndex
; }
1088 uint32_t undefinedEndIndex() { return _undefinedEndIndex
; }
1089 void addFixup(FixupInAtom f
) { _allFixups
.push_back(f
); }
1090 Section
<A
>* sectionForNum(unsigned int sectNum
);
1091 Section
<A
>* sectionForAddress(pint_t addr
);
1092 Atom
<A
>* findAtomByAddress(pint_t addr
);
1093 Atom
<A
>* findAtomByAddressOrNullIfStub(pint_t addr
);
1094 Atom
<A
>* findAtomByAddressOrLocalTargetOfStub(pint_t addr
, uint32_t* offsetInAtom
);
1095 Atom
<A
>* findAtomByName(const char* name
); // slow!
1096 void findTargetFromAddress(pint_t addr
, TargetDesc
& target
);
1097 void findTargetFromAddress(pint_t baseAddr
, pint_t addr
, TargetDesc
& target
);
1098 void findTargetFromAddressAndSectionNum(pint_t addr
, unsigned int sectNum
,
1099 TargetDesc
& target
);
1100 uint32_t tentativeDefinitionCount() { return _tentativeDefinitionCount
; }
1101 uint32_t absoluteSymbolCount() { return _absoluteSymbolCount
; }
1103 uint32_t fileLength() const { return _fileLength
; }
1104 bool hasStubsSection() { return (_stubsSectionNum
!= 0); }
1105 unsigned int stubsSectionNum() { return _stubsSectionNum
; }
1106 void addDtraceExtraInfos(const SourceLocation
& src
, const char* provider
);
1107 const char* scanSymbolTableForAddress(uint64_t addr
);
1108 bool warnUnwindConversionProblems() { return _warnUnwindConversionProblems
; }
1109 bool hasDataInCodeLabels() { return _hasDataInCodeLabels
; }
1110 bool keepDwarfUnwind() { return _keepDwarfUnwind
; }
1111 bool forceDwarfConversion() { return _forceDwarfConversion
; }
1112 bool verboseOptimizationHints() { return _verboseOptimizationHints
; }
1113 bool neverConvertDwarf() { return _neverConvertDwarf
; }
1114 bool armUsesZeroCostExceptions() { return _armUsesZeroCostExceptions
; }
1115 uint8_t maxDefaultCommonAlignment() { return _maxDefaultCommonAlignment
; }
1118 macho_data_in_code_entry
<P
>* dataInCodeStart() { return _dataInCodeStart
; }
1119 macho_data_in_code_entry
<P
>* dataInCodeEnd() { return _dataInCodeEnd
; }
1120 const uint8_t* optimizationHintsStart() { return _lohStart
; }
1121 const uint8_t* optimizationHintsEnd() { return _lohEnd
; }
1122 bool hasOptimizationHints() { return _lohStart
!= _lohEnd
; }
1125 void addFixups(const SourceLocation
& src
, ld::Fixup::Kind kind
, const TargetDesc
& target
);
1126 void addFixups(const SourceLocation
& src
, ld::Fixup::Kind kind
, const TargetDesc
& target
, const TargetDesc
& picBase
);
1130 struct LabelAndCFIBreakIterator
{
1131 typedef typename CFISection
<A
>::CFI_Atom_Info CFI_Atom_Info
;
1132 LabelAndCFIBreakIterator(const uint32_t* ssa
, uint32_t ssc
, const pint_t
* cfisa
,
1133 uint32_t cfisc
, bool ols
)
1134 : sortedSymbolIndexes(ssa
), sortedSymbolCount(ssc
), cfiStartsArray(cfisa
),
1135 cfiStartsCount(cfisc
), fileHasOverlappingSymbols(ols
),
1136 newSection(false), cfiIndex(0), symIndex(0) {}
1137 bool next(Parser
<A
>& parser
, const Section
<A
>& sect
, uint32_t sectNum
, pint_t startAddr
, pint_t endAddr
,
1138 pint_t
* addr
, pint_t
* size
, const macho_nlist
<P
>** sym
);
1139 pint_t
peek(Parser
<A
>& parser
, pint_t startAddr
, pint_t endAddr
);
1140 void beginSection() { newSection
= true; symIndex
= 0; }
1142 const uint32_t* const sortedSymbolIndexes
;
1143 const uint32_t sortedSymbolCount
;
1144 const pint_t
* cfiStartsArray
;
1145 const uint32_t cfiStartsCount
;
1146 const bool fileHasOverlappingSymbols
;
1152 struct CFI_CU_InfoArrays
{
1153 typedef typename CFISection
<A
>::CFI_Atom_Info CFI_Atom_Info
;
1154 typedef typename CUSection
<A
>::Info CU_Info
;
1155 CFI_CU_InfoArrays(const CFI_Atom_Info
* cfiAr
, uint32_t cfiC
, CU_Info
* cuAr
, uint32_t cuC
)
1156 : cfiArray(cfiAr
), cuArray(cuAr
), cfiCount(cfiC
), cuCount(cuC
) {}
1157 const CFI_Atom_Info
* const cfiArray
;
1158 CU_Info
* const cuArray
;
1159 const uint32_t cfiCount
;
1160 const uint32_t cuCount
;
1166 friend class Section
<A
>;
1168 enum SectionType
{ sectionTypeIgnore
, sectionTypeLiteral4
, sectionTypeLiteral8
, sectionTypeLiteral16
,
1169 sectionTypeNonLazy
, sectionTypeCFI
, sectionTypeCString
, sectionTypeCStringPointer
,
1170 sectionTypeUTF16Strings
, sectionTypeCFString
, sectionTypeObjC2ClassRefs
, typeObjC2CategoryList
,
1171 sectionTypeObjC1Classes
, sectionTypeSymboled
, sectionTypeObjC1ClassRefs
,
1172 sectionTypeTentativeDefinitions
, sectionTypeAbsoluteSymbols
, sectionTypeTLVDefs
,
1173 sectionTypeCompactUnwind
, sectionTypeTLVPointers
};
1175 template <typename P
>
1176 struct MachOSectionAndSectionClass
1178 const macho_section
<P
>* sect
;
1181 static int sorter(const void* l
, const void* r
) {
1182 const MachOSectionAndSectionClass
<P
>* left
= (MachOSectionAndSectionClass
<P
>*)l
;
1183 const MachOSectionAndSectionClass
<P
>* right
= (MachOSectionAndSectionClass
<P
>*)r
;
1184 int64_t diff
= left
->sect
->addr() - right
->sect
->addr();
1194 struct ParserAndSectionsArray
{ Parser
* parser
; const uint32_t* sortedSectionsArray
; };
1197 Parser(const uint8_t* fileContent
, uint64_t fileLength
,
1198 const char* path
, time_t modTime
, ld::File::Ordinal ordinal
,
1199 bool warnUnwindConversionProblems
, bool keepDwarfUnwind
,
1200 bool forceDwarfConversion
, bool neverConvertDwarf
,
1201 bool verboseOptimizationHints
, bool ignoreMismatchPlatform
);
1202 ld::relocatable::File
* parse(const ParserOptions
& opts
);
1203 static uint8_t loadCommandSizeMask();
1204 bool parseLoadCommands(Options::Platform platform
, uint32_t minOSVersion
, bool simulator
, bool ignoreMismatchPlatform
);
1205 void makeSections();
1206 void prescanSymbolTable();
1207 void makeSortedSymbolsArray(uint32_t symArray
[], const uint32_t sectionArray
[]);
1208 void makeSortedSectionsArray(uint32_t array
[]);
1209 static int pointerSorter(const void* l
, const void* r
);
1210 static int symbolIndexSorter(void* extra
, const void* l
, const void* r
);
1211 static int sectionIndexSorter(void* extra
, const void* l
, const void* r
);
1213 void parseDebugInfo();
1215 void appendAliasAtoms(uint8_t* atomBuffer
);
1216 static bool isConstFunStabs(const char *stabStr
);
1217 bool read_comp_unit(const char ** name
, const char ** comp_dir
,
1218 uint64_t *stmt_list
);
1219 pint_t
realAddr(pint_t addr
);
1220 const char* getDwarfString(uint64_t form
, const uint8_t*& p
);
1221 uint64_t getDwarfOffset(uint64_t form
, const uint8_t*& di
, bool dwarf64
);
1222 bool skip_form(const uint8_t ** offset
, const uint8_t * end
,
1223 uint64_t form
, uint8_t addr_size
, bool dwarf64
);
1226 // filled in by constructor
1227 const uint8_t* _fileContent
;
1228 uint32_t _fileLength
;
1231 ld::File::Ordinal _ordinal
;
1233 // filled in by parseLoadCommands()
1235 const macho_nlist
<P
>* _symbols
;
1236 uint32_t _symbolCount
;
1237 uint32_t _indirectSymbolCount
;
1238 const char* _strings
;
1239 uint32_t _stringsSize
;
1240 const uint32_t* _indirectTable
;
1241 uint32_t _indirectTableCount
;
1242 uint32_t _undefinedStartIndex
;
1243 uint32_t _undefinedEndIndex
;
1244 const macho_section
<P
>* _sectionsStart
;
1245 uint32_t _machOSectionsCount
;
1247 macho_data_in_code_entry
<P
>* _dataInCodeStart
;
1248 macho_data_in_code_entry
<P
>* _dataInCodeEnd
;
1249 const uint8_t* _lohStart
;
1250 const uint8_t* _lohEnd
;
1252 // filled in by parse()
1253 CFISection
<A
>* _EHFrameSection
;
1254 CUSection
<A
>* _compactUnwindSection
;
1255 AbsoluteSymbolSection
<A
>* _absoluteSection
;
1256 uint32_t _tentativeDefinitionCount
;
1257 uint32_t _absoluteSymbolCount
;
1258 uint32_t _symbolsInSections
;
1259 bool _hasLongBranchStubs
;
1260 bool _AppleObjc
; // FSF has objc that uses different data layout
1261 bool _overlappingSymbols
;
1262 bool _warnUnwindConversionProblems
;
1263 bool _hasDataInCodeLabels
;
1264 bool _keepDwarfUnwind
;
1265 bool _forceDwarfConversion
;
1266 bool _neverConvertDwarf
;
1267 bool _verboseOptimizationHints
;
1268 bool _armUsesZeroCostExceptions
;
1269 bool _ignoreMismatchPlatform
;
1270 bool _treateBitcodeAsData
;
1272 uint8_t _maxDefaultCommonAlignment
;
1273 unsigned int _stubsSectionNum
;
1274 const macho_section
<P
>* _stubsMachOSection
;
1275 std::vector
<const char*> _dtraceProviderInfo
;
1276 std::vector
<FixupInAtom
> _allFixups
;
1281 template <typename A
>
1282 Parser
<A
>::Parser(const uint8_t* fileContent
, uint64_t fileLength
, const char* path
, time_t modTime
,
1283 ld::File::Ordinal ordinal
, bool convertDUI
, bool keepDwarfUnwind
, bool forceDwarfConversion
,
1284 bool neverConvertDwarf
, bool verboseOptimizationHints
, bool ignoreMismatchPlatform
)
1285 : _fileContent(fileContent
), _fileLength(fileLength
), _path(path
), _modTime(modTime
),
1286 _ordinal(ordinal
), _file(NULL
),
1287 _symbols(NULL
), _symbolCount(0), _indirectSymbolCount(0), _strings(NULL
), _stringsSize(0),
1288 _indirectTable(NULL
), _indirectTableCount(0),
1289 _undefinedStartIndex(0), _undefinedEndIndex(0),
1290 _sectionsStart(NULL
), _machOSectionsCount(0), _hasUUID(false),
1291 _dataInCodeStart(NULL
), _dataInCodeEnd(NULL
),
1292 _lohStart(NULL
), _lohEnd(NULL
),
1293 _EHFrameSection(NULL
), _compactUnwindSection(NULL
), _absoluteSection(NULL
),
1294 _tentativeDefinitionCount(0), _absoluteSymbolCount(0),
1295 _symbolsInSections(0), _hasLongBranchStubs(false), _AppleObjc(false),
1296 _overlappingSymbols(false), _warnUnwindConversionProblems(convertDUI
), _hasDataInCodeLabels(false),
1297 _keepDwarfUnwind(keepDwarfUnwind
), _forceDwarfConversion(forceDwarfConversion
),
1298 _neverConvertDwarf(neverConvertDwarf
),
1299 _verboseOptimizationHints(verboseOptimizationHints
),
1300 _ignoreMismatchPlatform(ignoreMismatchPlatform
),
1301 _stubsSectionNum(0), _stubsMachOSection(NULL
)
1307 bool Parser
<x86
>::validFile(const uint8_t* fileContent
, bool, cpu_subtype_t
)
1309 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1310 if ( header
->magic() != MH_MAGIC
)
1312 if ( header
->cputype() != CPU_TYPE_I386
)
1314 if ( header
->filetype() != MH_OBJECT
)
1320 bool Parser
<x86_64
>::validFile(const uint8_t* fileContent
, bool, cpu_subtype_t
)
1322 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1323 if ( header
->magic() != MH_MAGIC_64
)
1325 if ( header
->cputype() != CPU_TYPE_X86_64
)
1327 if ( header
->filetype() != MH_OBJECT
)
1333 bool Parser
<arm
>::validFile(const uint8_t* fileContent
, bool subtypeMustMatch
, cpu_subtype_t subtype
)
1335 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1336 if ( header
->magic() != MH_MAGIC
)
1338 if ( header
->cputype() != CPU_TYPE_ARM
)
1340 if ( header
->filetype() != MH_OBJECT
)
1342 if ( subtypeMustMatch
) {
1343 if ( (cpu_subtype_t
)header
->cpusubtype() == subtype
)
1345 // hack until libcc_kext.a is made fat
1346 if ( header
->cpusubtype() == CPU_SUBTYPE_ARM_ALL
)
1355 bool Parser
<arm64
>::validFile(const uint8_t* fileContent
, bool subtypeMustMatch
, cpu_subtype_t subtype
)
1357 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1358 if ( header
->magic() != MH_MAGIC_64
)
1360 if ( header
->cputype() != CPU_TYPE_ARM64
)
1362 if ( header
->filetype() != MH_OBJECT
)
1369 const char* Parser
<x86
>::fileKind(const uint8_t* fileContent
)
1371 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1372 if ( header
->magic() != MH_MAGIC
)
1374 if ( header
->cputype() != CPU_TYPE_I386
)
1380 const char* Parser
<x86_64
>::fileKind(const uint8_t* fileContent
)
1382 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1383 if ( header
->magic() != MH_MAGIC_64
)
1385 if ( header
->cputype() != CPU_TYPE_X86_64
)
1391 const char* Parser
<arm
>::fileKind(const uint8_t* fileContent
)
1393 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1394 if ( header
->magic() != MH_MAGIC
)
1396 if ( header
->cputype() != CPU_TYPE_ARM
)
1398 for (const ArchInfo
* t
=archInfoArray
; t
->archName
!= NULL
; ++t
) {
1399 if ( (t
->cpuType
== CPU_TYPE_ARM
) && ((cpu_subtype_t
)header
->cpusubtype() == t
->cpuSubType
) ) {
1406 #if SUPPORT_ARCH_arm64
1408 const char* Parser
<arm64
>::fileKind(const uint8_t* fileContent
)
1410 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1411 if ( header
->magic() != MH_MAGIC_64
)
1413 if ( header
->cputype() != CPU_TYPE_ARM64
)
1420 template <typename A
>
1421 bool Parser
<A
>::hasObjC2Categories(const uint8_t* fileContent
)
1423 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1424 const uint32_t cmd_count
= header
->ncmds();
1425 const macho_load_command
<P
>* const cmds
= (macho_load_command
<P
>*)((char*)header
+ sizeof(macho_header
<P
>));
1426 const macho_load_command
<P
>* const cmdsEnd
= (macho_load_command
<P
>*)((char*)header
+ sizeof(macho_header
<P
>) + header
->sizeofcmds());
1427 const macho_load_command
<P
>* cmd
= cmds
;
1428 for (uint32_t i
= 0; i
< cmd_count
; ++i
) {
1429 if ( cmd
->cmd() == macho_segment_command
<P
>::CMD
) {
1430 const macho_segment_command
<P
>* segment
= (macho_segment_command
<P
>*)cmd
;
1431 const macho_section
<P
>* sectionsStart
= (macho_section
<P
>*)((char*)segment
+ sizeof(macho_segment_command
<P
>));
1432 for (uint32_t si
=0; si
< segment
->nsects(); ++si
) {
1433 const macho_section
<P
>* sect
= §ionsStart
[si
];
1434 if ( (sect
->size() > 0)
1435 && (strcmp(sect
->sectname(), "__objc_catlist") == 0)
1436 && (strcmp(sect
->segname(), "__DATA") == 0) ) {
1441 cmd
= (const macho_load_command
<P
>*)(((char*)cmd
)+cmd
->cmdsize());
1442 if ( cmd
> cmdsEnd
)
1443 throwf("malformed mach-o file, load command #%d is outside size of load commands", i
);
1449 template <typename A
>
1450 bool Parser
<A
>::hasObjC1Categories(const uint8_t* fileContent
)
1452 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1453 const uint32_t cmd_count
= header
->ncmds();
1454 const macho_load_command
<P
>* const cmds
= (macho_load_command
<P
>*)((char*)header
+ sizeof(macho_header
<P
>));
1455 const macho_load_command
<P
>* const cmdsEnd
= (macho_load_command
<P
>*)((char*)header
+ sizeof(macho_header
<P
>) + header
->sizeofcmds());
1456 const macho_load_command
<P
>* cmd
= cmds
;
1457 for (uint32_t i
= 0; i
< cmd_count
; ++i
) {
1458 if ( cmd
->cmd() == macho_segment_command
<P
>::CMD
) {
1459 const macho_segment_command
<P
>* segment
= (macho_segment_command
<P
>*)cmd
;
1460 const macho_section
<P
>* sectionsStart
= (macho_section
<P
>*)((char*)segment
+ sizeof(macho_segment_command
<P
>));
1461 for (uint32_t si
=0; si
< segment
->nsects(); ++si
) {
1462 const macho_section
<P
>* sect
= §ionsStart
[si
];
1463 if ( (sect
->size() > 0)
1464 && (strcmp(sect
->sectname(), "__category") == 0)
1465 && (strcmp(sect
->segname(), "__OBJC") == 0) ) {
1470 cmd
= (const macho_load_command
<P
>*)(((char*)cmd
)+cmd
->cmdsize());
1471 if ( cmd
> cmdsEnd
)
1472 throwf("malformed mach-o file, load command #%d is outside size of load commands", i
);
1478 template <typename A
>
1479 bool Parser
<A
>::getNonLocalSymbols(const uint8_t* fileContent
, std::vector
<const char*> &syms
)
1481 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1482 const uint32_t cmd_count
= header
->ncmds();
1483 const macho_load_command
<P
>* const cmds
= (macho_load_command
<P
>*)((char*)header
+ sizeof(macho_header
<P
>));
1484 const macho_load_command
<P
>* const cmdsEnd
= (macho_load_command
<P
>*)((char*)header
+ sizeof(macho_header
<P
>) + header
->sizeofcmds());
1485 const macho_load_command
<P
>* cmd
= cmds
;
1486 for (uint32_t i
= 0; i
< cmd_count
; ++i
) {
1487 if ( cmd
->cmd() == LC_SYMTAB
) {
1488 const macho_symtab_command
<P
>* symtab
= (macho_symtab_command
<P
>*)cmd
;
1489 uint32_t symbolCount
= symtab
->nsyms();
1490 const macho_nlist
<P
>* symbols
= (const macho_nlist
<P
>*)(fileContent
+ symtab
->symoff());
1491 const char* strings
= (char*)fileContent
+ symtab
->stroff();
1492 for (uint32_t j
= 0; j
< symbolCount
; ++j
) {
1493 // ignore stabs and count only ext symbols
1494 if ( (symbols
[j
].n_type() & N_STAB
) == 0 &&
1495 (symbols
[j
].n_type() & N_EXT
) != 0 ) {
1496 const char* symName
= &strings
[symbols
[j
].n_strx()];
1497 syms
.push_back(symName
);
1502 cmd
= (const macho_load_command
<P
>*)(((char*)cmd
)+cmd
->cmdsize());
1503 if ( cmd
> cmdsEnd
)
1504 throwf("malformed mach-o file, load command #%d is outside size of load commands", i
);
1510 template <typename A
>
1511 int Parser
<A
>::pointerSorter(const void* l
, const void* r
)
1513 // sort references by address
1514 const pint_t
* left
= (pint_t
*)l
;
1515 const pint_t
* right
= (pint_t
*)r
;
1516 return (*left
- *right
);
1519 template <typename A
>
1520 typename
A::P::uint_t Parser
<A
>::LabelAndCFIBreakIterator::peek(Parser
<A
>& parser
, pint_t startAddr
, pint_t endAddr
)
1523 if ( symIndex
< sortedSymbolCount
)
1524 symbolAddr
= parser
.symbolFromIndex(sortedSymbolIndexes
[symIndex
]).n_value();
1526 symbolAddr
= endAddr
;
1528 if ( cfiIndex
< cfiStartsCount
)
1529 cfiAddr
= cfiStartsArray
[cfiIndex
];
1532 if ( (cfiAddr
< symbolAddr
) && (cfiAddr
>= startAddr
) ) {
1533 if ( cfiAddr
< endAddr
)
1539 if ( symbolAddr
< endAddr
)
1547 // Parses up a section into chunks based on labels and CFI information.
1548 // Each call returns the next chunk address and size, and (if the break
1549 // was becuase of a label, the symbol). Returns false when no more chunks.
1551 template <typename A
>
1552 bool Parser
<A
>::LabelAndCFIBreakIterator::next(Parser
<A
>& parser
, const Section
<A
>& sect
, uint32_t sectNum
, pint_t startAddr
, pint_t endAddr
,
1553 pint_t
* addr
, pint_t
* size
, const macho_nlist
<P
>** symbol
)
1555 // may not be a label on start of section, but need atom demarcation there
1558 // advance symIndex until we get to the first label at or past the start of this section
1559 while ( symIndex
< sortedSymbolCount
) {
1560 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(sortedSymbolIndexes
[symIndex
]);
1561 if ( ! sect
.ignoreLabel(parser
.nameFromSymbol(sym
)) ) {
1562 pint_t nextSymbolAddr
= sym
.n_value();
1563 //fprintf(stderr, "sectNum=%d, nextSymbolAddr=0x%08llX, name=%s\n", sectNum, (uint64_t)nextSymbolAddr, parser.nameFromSymbol(sym));
1564 if ( (nextSymbolAddr
> startAddr
) || ((nextSymbolAddr
== startAddr
) && (sym
.n_sect() == sectNum
)) )
1569 if ( symIndex
< sortedSymbolCount
) {
1570 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(sortedSymbolIndexes
[symIndex
]);
1571 pint_t nextSymbolAddr
= sym
.n_value();
1572 // if next symbol found is not in this section
1573 if ( sym
.n_sect() != sectNum
) {
1574 // check for CFI break instead of symbol break
1575 if ( cfiIndex
< cfiStartsCount
) {
1576 pint_t nextCfiAddr
= cfiStartsArray
[cfiIndex
];
1577 if ( nextCfiAddr
< endAddr
) {
1580 *addr
= nextCfiAddr
;
1581 *size
= peek(parser
, startAddr
, endAddr
) - nextCfiAddr
;
1587 *size
= endAddr
- startAddr
;
1589 if ( startAddr
== endAddr
)
1590 return false; // zero size section
1592 return true; // whole section is one atom with no label
1594 // if also CFI break here, eat it
1595 if ( cfiIndex
< cfiStartsCount
) {
1596 if ( cfiStartsArray
[cfiIndex
] == nextSymbolAddr
)
1599 if ( nextSymbolAddr
== startAddr
) {
1600 // label at start of section, return it as chunk
1603 *size
= peek(parser
, startAddr
, endAddr
) - startAddr
;
1607 // return chunk before first symbol
1609 *size
= nextSymbolAddr
- startAddr
;
1613 // no symbols in section, check CFI
1614 if ( cfiIndex
< cfiStartsCount
) {
1615 pint_t nextCfiAddr
= cfiStartsArray
[cfiIndex
];
1616 if ( nextCfiAddr
< endAddr
) {
1619 *addr
= nextCfiAddr
;
1620 *size
= peek(parser
, startAddr
, endAddr
) - nextCfiAddr
;
1625 // no cfi, so whole section is one chunk
1627 *size
= endAddr
- startAddr
;
1629 if ( startAddr
== endAddr
)
1630 return false; // zero size section
1632 return true; // whole section is one atom with no label
1635 while ( (symIndex
< sortedSymbolCount
) && (cfiIndex
< cfiStartsCount
) ) {
1636 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(sortedSymbolIndexes
[symIndex
]);
1637 pint_t nextSymbolAddr
= sym
.n_value();
1638 pint_t nextCfiAddr
= cfiStartsArray
[cfiIndex
];
1639 if ( nextSymbolAddr
< nextCfiAddr
) {
1640 if ( nextSymbolAddr
>= endAddr
)
1643 if ( nextSymbolAddr
< startAddr
)
1645 *addr
= nextSymbolAddr
;
1646 *size
= peek(parser
, startAddr
, endAddr
) - nextSymbolAddr
;
1650 else if ( nextCfiAddr
< nextSymbolAddr
) {
1651 if ( nextCfiAddr
>= endAddr
)
1654 if ( nextCfiAddr
< startAddr
)
1656 *addr
= nextCfiAddr
;
1657 *size
= peek(parser
, startAddr
, endAddr
) - nextCfiAddr
;
1662 if ( nextCfiAddr
>= endAddr
)
1666 if ( nextCfiAddr
< startAddr
)
1668 *addr
= nextCfiAddr
;
1669 *size
= peek(parser
, startAddr
, endAddr
) - nextCfiAddr
;
1674 while ( symIndex
< sortedSymbolCount
) {
1675 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(sortedSymbolIndexes
[symIndex
]);
1676 pint_t nextSymbolAddr
= sym
.n_value();
1677 // if next symbol found is not in this section, then done with iteration
1678 if ( sym
.n_sect() != sectNum
)
1681 if ( nextSymbolAddr
< startAddr
)
1683 *addr
= nextSymbolAddr
;
1684 *size
= peek(parser
, startAddr
, endAddr
) - nextSymbolAddr
;
1688 while ( cfiIndex
< cfiStartsCount
) {
1689 pint_t nextCfiAddr
= cfiStartsArray
[cfiIndex
];
1690 if ( nextCfiAddr
>= endAddr
)
1693 if ( nextCfiAddr
< startAddr
)
1695 *addr
= nextCfiAddr
;
1696 *size
= peek(parser
, startAddr
, endAddr
) - nextCfiAddr
;
1704 typename
arm::P::uint_t Parser
<arm
>::realAddr(typename
arm::P::uint_t addr
)
1709 template <typename A
>
1710 typename
A::P::uint_t Parser
<A
>::realAddr(typename
A::P::uint_t addr
)
1715 #define STACK_ALLOC_IF_SMALL(_type, _name, _actual_count, _maxCount) \
1716 _type* _name = NULL; \
1717 uint32_t _name##_count = 1; \
1718 uint32_t _name##_stack_count = _actual_count; \
1719 if ( _actual_count > _maxCount ) { \
1720 _name = (_type*)malloc(sizeof(_type) * _actual_count); \
1721 _name##_stack_count = 1; \
1724 _name##_count = _actual_count; \
1725 _type _name##_buffer[_name##_stack_count]; \
1726 if ( _name == NULL ) \
1727 _name = _name##_buffer;
1730 template <typename A
>
1731 ld::relocatable::File
* Parser
<A
>::parse(const ParserOptions
& opts
)
1733 // create file object
1734 _file
= new File
<A
>(_path
, _modTime
, _fileContent
, _ordinal
);
1737 _file
->_srcKind
= opts
.srcKind
;
1738 // set treatBitcodeAsData
1739 _treateBitcodeAsData
= opts
.treateBitcodeAsData
;
1740 _usingBitcode
= opts
.usingBitcode
;
1742 // respond to -t option
1743 if ( opts
.logAllFiles
)
1744 printf("%s\n", _path
);
1746 _armUsesZeroCostExceptions
= opts
.armUsesZeroCostExceptions
;
1747 _maxDefaultCommonAlignment
= opts
.maxDefaultCommonAlignment
;
1749 // parse start of mach-o file
1750 if ( ! parseLoadCommands(opts
.platform
, opts
.minOSVersion
, opts
.simulator
, opts
.ignoreMismatchPlatform
) )
1754 uint32_t sortedSectionIndexes
[_machOSectionsCount
];
1755 this->makeSortedSectionsArray(sortedSectionIndexes
);
1757 // make symbol table sorted by address
1758 this->prescanSymbolTable();
1759 uint32_t sortedSymbolIndexes
[_symbolsInSections
];
1760 this->makeSortedSymbolsArray(sortedSymbolIndexes
, sortedSectionIndexes
);
1762 // allocate Section<A> object for each mach-o section
1765 // if it exists, do special early parsing of __compact_unwind section
1766 uint32_t countOfCUs
= 0;
1767 if ( _compactUnwindSection
!= NULL
)
1768 countOfCUs
= _compactUnwindSection
->count();
1769 // stack allocate (if not too large) cuInfoBuffer
1770 STACK_ALLOC_IF_SMALL(typename CUSection
<A
>::Info
, cuInfoArray
, countOfCUs
, 1024);
1771 if ( countOfCUs
!= 0 )
1772 _compactUnwindSection
->parse(*this, countOfCUs
, cuInfoArray
);
1774 // create lists of address that already have compact unwind and thus don't need the dwarf parsed
1775 unsigned cuLsdaCount
= 0;
1776 STACK_ALLOC_IF_SMALL(pint_t
, cuStarts
, countOfCUs
, 1024);
1777 for (uint32_t i
=0; i
< countOfCUs
; ++i
) {
1778 if ( CUSection
<A
>::encodingMeansUseDwarf(cuInfoArray
[i
].compactUnwindInfo
) )
1781 cuStarts
[i
] = cuInfoArray
[i
].functionStartAddress
;
1782 if ( cuInfoArray
[i
].lsdaAddress
!= 0 )
1787 // if it exists, do special early parsing of __eh_frame section
1788 // stack allocate (if not too large) array of CFI_Atom_Info
1789 uint32_t countOfCFIs
= 0;
1790 if ( _EHFrameSection
!= NULL
)
1791 countOfCFIs
= _EHFrameSection
->cfiCount(*this);
1792 STACK_ALLOC_IF_SMALL(typename CFISection
<A
>::CFI_Atom_Info
, cfiArray
, countOfCFIs
, 1024);
1794 // stack allocate (if not too large) a copy of __eh_frame to apply relocations to
1795 uint32_t sectSize
= 4;
1796 if ( (countOfCFIs
!= 0) && _EHFrameSection
->needsRelocating() )
1797 sectSize
= _EHFrameSection
->machoSection()->size()+4;
1798 STACK_ALLOC_IF_SMALL(uint8_t, ehBuffer
, sectSize
, 50*1024);
1799 uint32_t cfiStartsCount
= 0;
1800 if ( countOfCFIs
!= 0 ) {
1801 _EHFrameSection
->cfiParse(*this, ehBuffer
, cfiArray
, countOfCFIs
, cuStarts
, countOfCUs
);
1802 // count functions and lsdas
1803 for(uint32_t i
=0; i
< countOfCFIs
; ++i
) {
1804 if ( cfiArray
[i
].isCIE
)
1806 //fprintf(stderr, "cfiArray[i].func = 0x%08llX, cfiArray[i].lsda = 0x%08llX, encoding=0x%08X\n",
1807 // (uint64_t)cfiArray[i].u.fdeInfo.function.targetAddress,
1808 // (uint64_t)cfiArray[i].u.fdeInfo.lsda.targetAddress,
1809 // cfiArray[i].u.fdeInfo.compactUnwindInfo);
1810 if ( cfiArray
[i
].u
.fdeInfo
.function
.targetAddress
!= CFI_INVALID_ADDRESS
)
1812 if ( cfiArray
[i
].u
.fdeInfo
.lsda
.targetAddress
!= CFI_INVALID_ADDRESS
)
1816 CFI_CU_InfoArrays
cfis(cfiArray
, countOfCFIs
, cuInfoArray
, countOfCUs
);
1818 // create sorted array of function starts and lsda starts
1819 pint_t cfiStartsArray
[cfiStartsCount
+cuLsdaCount
];
1820 uint32_t countOfFDEs
= 0;
1821 uint32_t cfiStartsArrayCount
= 0;
1822 if ( countOfCFIs
!= 0 ) {
1823 for(uint32_t i
=0; i
< countOfCFIs
; ++i
) {
1824 if ( cfiArray
[i
].isCIE
)
1826 if ( cfiArray
[i
].u
.fdeInfo
.function
.targetAddress
!= CFI_INVALID_ADDRESS
)
1827 cfiStartsArray
[cfiStartsArrayCount
++] = realAddr(cfiArray
[i
].u
.fdeInfo
.function
.targetAddress
);
1828 if ( cfiArray
[i
].u
.fdeInfo
.lsda
.targetAddress
!= CFI_INVALID_ADDRESS
)
1829 cfiStartsArray
[cfiStartsArrayCount
++] = cfiArray
[i
].u
.fdeInfo
.lsda
.targetAddress
;
1833 if ( cuLsdaCount
!= 0 ) {
1834 // merge in an lsda info from compact unwind
1835 for (uint32_t i
=0; i
< countOfCUs
; ++i
) {
1836 if ( cuInfoArray
[i
].lsdaAddress
== 0 )
1838 // append to cfiStartsArray if not already in that list
1840 for(uint32_t j
=0; j
< cfiStartsArrayCount
; ++j
) {
1841 if ( cfiStartsArray
[j
] == cuInfoArray
[i
].lsdaAddress
)
1845 cfiStartsArray
[cfiStartsArrayCount
++] = cuInfoArray
[i
].lsdaAddress
;
1849 if ( cfiStartsArrayCount
!= 0 ) {
1850 ::qsort(cfiStartsArray
, cfiStartsArrayCount
, sizeof(pint_t
), pointerSorter
);
1852 // scan for FDEs claming the same function
1853 for(uint32_t i
=1; i
< cfiStartsArrayCount
; ++i
) {
1854 assert( cfiStartsArray
[i
] != cfiStartsArray
[i
-1] );
1859 Section
<A
>** sections
= _file
->_sectionsArray
;
1860 uint32_t sectionsCount
= _file
->_sectionsArrayCount
;
1862 // figure out how many atoms will be allocated and allocate
1863 LabelAndCFIBreakIterator
breakIterator(sortedSymbolIndexes
, _symbolsInSections
, cfiStartsArray
,
1864 cfiStartsArrayCount
, _overlappingSymbols
);
1865 uint32_t computedAtomCount
= 0;
1866 for (uint32_t i
=0; i
< sectionsCount
; ++i
) {
1867 breakIterator
.beginSection();
1868 uint32_t count
= sections
[i
]->computeAtomCount(*this, breakIterator
, cfis
);
1869 //const macho_section<P>* sect = sections[i]->machoSection();
1870 //fprintf(stderr, "computed count=%u for section %s size=%llu\n", count, sect->sectname(), (sect != NULL) ? sect->size() : 0);
1871 computedAtomCount
+= count
;
1873 //fprintf(stderr, "allocating %d atoms * sizeof(Atom<A>)=%ld, sizeof(ld::Atom)=%ld\n", computedAtomCount, sizeof(Atom<A>), sizeof(ld::Atom));
1874 _file
->_atomsArray
= new uint8_t[computedAtomCount
*sizeof(Atom
<A
>)];
1875 _file
->_atomsArrayCount
= 0;
1877 // have each section append atoms to _atomsArray
1878 LabelAndCFIBreakIterator
breakIterator2(sortedSymbolIndexes
, _symbolsInSections
, cfiStartsArray
,
1879 cfiStartsArrayCount
, _overlappingSymbols
);
1880 for (uint32_t i
=0; i
< sectionsCount
; ++i
) {
1881 uint8_t* atoms
= _file
->_atomsArray
+ _file
->_atomsArrayCount
*sizeof(Atom
<A
>);
1882 breakIterator2
.beginSection();
1883 uint32_t count
= sections
[i
]->appendAtoms(*this, atoms
, breakIterator2
, cfis
);
1884 //fprintf(stderr, "append count=%u for section %s/%s\n", count, sections[i]->machoSection()->segname(), sections[i]->machoSection()->sectname());
1885 _file
->_atomsArrayCount
+= count
;
1887 assert( _file
->_atomsArrayCount
== computedAtomCount
&& "more atoms allocated than expected");
1890 // have each section add all fix-ups for its atoms
1891 _allFixups
.reserve(computedAtomCount
*5);
1892 for (uint32_t i
=0; i
< sectionsCount
; ++i
)
1893 sections
[i
]->makeFixups(*this, cfis
);
1895 // assign fixups start offset for each atom
1896 uint8_t* p
= _file
->_atomsArray
;
1897 uint32_t fixupOffset
= 0;
1898 for(int i
=_file
->_atomsArrayCount
; i
> 0; --i
) {
1899 Atom
<A
>* atom
= (Atom
<A
>*)p
;
1900 atom
->_fixupsStartIndex
= fixupOffset
;
1901 fixupOffset
+= atom
->_fixupsCount
;
1902 atom
->_fixupsCount
= 0;
1903 p
+= sizeof(Atom
<A
>);
1905 assert(fixupOffset
== _allFixups
.size());
1906 _file
->_fixups
.resize(fixupOffset
);
1908 // copy each fixup for each atom
1909 for(typename
std::vector
<FixupInAtom
>::iterator it
=_allFixups
.begin(); it
!= _allFixups
.end(); ++it
) {
1910 uint32_t slot
= it
->atom
->_fixupsStartIndex
+ it
->atom
->_fixupsCount
;
1911 _file
->_fixups
[slot
] = it
->fixup
;
1912 it
->atom
->_fixupsCount
++;
1915 // done with temp vector
1919 _file
->_unwindInfos
.reserve(countOfFDEs
+countOfCUs
);
1920 for(uint32_t i
=0; i
< countOfCFIs
; ++i
) {
1921 if ( cfiArray
[i
].isCIE
)
1923 if ( cfiArray
[i
].u
.fdeInfo
.function
.targetAddress
!= CFI_INVALID_ADDRESS
) {
1924 ld::Atom::UnwindInfo info
;
1925 info
.startOffset
= 0;
1926 info
.unwindInfo
= cfiArray
[i
].u
.fdeInfo
.compactUnwindInfo
;
1927 _file
->_unwindInfos
.push_back(info
);
1928 Atom
<A
>* func
= findAtomByAddress(cfiArray
[i
].u
.fdeInfo
.function
.targetAddress
);
1929 func
->setUnwindInfoRange(_file
->_unwindInfos
.size()-1, 1);
1930 //fprintf(stderr, "cu from dwarf =0x%08X, atom=%s\n", info.unwindInfo, func->name());
1933 // apply compact infos in __LD,__compact_unwind section to each function
1934 // if function also has dwarf unwind, CU will override it
1935 Atom
<A
>* lastFunc
= NULL
;
1936 uint32_t lastEnd
= 0;
1937 for(uint32_t i
=0; i
< countOfCUs
; ++i
) {
1938 typename CUSection
<A
>::Info
* info
= &cuInfoArray
[i
];
1939 assert(info
->function
!= NULL
);
1940 ld::Atom::UnwindInfo ui
;
1941 ui
.startOffset
= info
->functionStartAddress
- info
->function
->objectAddress();
1942 ui
.unwindInfo
= info
->compactUnwindInfo
;
1943 _file
->_unwindInfos
.push_back(ui
);
1944 // don't override with converted cu with "use dwarf" cu, if forcing dwarf conversion
1945 if ( !_forceDwarfConversion
|| !CUSection
<A
>::encodingMeansUseDwarf(info
->compactUnwindInfo
) ) {
1946 //fprintf(stderr, "cu=0x%08X, atom=%s\n", ui.unwindInfo, info->function->name());
1947 // if previous is for same function, extend range
1948 if ( info
->function
== lastFunc
) {
1949 if ( lastEnd
!= ui
.startOffset
) {
1950 if ( lastEnd
< ui
.startOffset
)
1951 warning("__LD,__compact_unwind entries for %s have a gap at offset 0x%0X", info
->function
->name(), lastEnd
);
1953 warning("__LD,__compact_unwind entries for %s overlap at offset 0x%0X", info
->function
->name(), lastEnd
);
1955 lastFunc
->extendUnwindInfoRange();
1958 info
->function
->setUnwindInfoRange(_file
->_unwindInfos
.size()-1, 1);
1959 lastFunc
= info
->function
;
1960 lastEnd
= ui
.startOffset
+ info
->rangeLength
;
1964 // process indirect symbols which become AliasAtoms
1965 _file
->_aliasAtomsArray
= NULL
;
1966 _file
->_aliasAtomsArrayCount
= 0;
1967 if ( _indirectSymbolCount
!= 0 ) {
1968 _file
->_aliasAtomsArrayCount
= _indirectSymbolCount
;
1969 _file
->_aliasAtomsArray
= new uint8_t[_file
->_aliasAtomsArrayCount
*sizeof(AliasAtom
)];
1970 this->appendAliasAtoms(_file
->_aliasAtomsArray
);
1974 // parse dwarf debug info to get line info
1975 this->parseDebugInfo();
1980 static void versionToString(uint32_t value
, char buffer
[32])
1983 sprintf(buffer
, "%d.%d.%d", value
>> 16, (value
>> 8) & 0xFF, value
& 0xFF);
1985 sprintf(buffer
, "%d.%d", value
>> 16, (value
>> 8) & 0xFF);
1988 template <> uint8_t Parser
<x86
>::loadCommandSizeMask() { return 0x03; }
1989 template <> uint8_t Parser
<x86_64
>::loadCommandSizeMask() { return 0x07; }
1990 template <> uint8_t Parser
<arm
>::loadCommandSizeMask() { return 0x03; }
1991 template <> uint8_t Parser
<arm64
>::loadCommandSizeMask() { return 0x07; }
1993 template <typename A
>
1994 bool Parser
<A
>::parseLoadCommands(Options::Platform platform
, uint32_t linkMinOSVersion
, bool simulator
, bool ignoreMismatchPlatform
)
1996 const macho_header
<P
>* header
= (const macho_header
<P
>*)_fileContent
;
1998 // set File attributes
1999 _file
->_canScatterAtoms
= (header
->flags() & MH_SUBSECTIONS_VIA_SYMBOLS
);
2000 _file
->_cpuSubType
= header
->cpusubtype();
2002 const macho_segment_command
<P
>* segment
= NULL
;
2003 const uint8_t* const endOfFile
= _fileContent
+ _fileLength
;
2004 const uint32_t cmd_count
= header
->ncmds();
2005 // <rdar://problem/5394172> an empty .o file with zero load commands will crash linker
2006 if ( cmd_count
== 0 )
2008 Options::Platform lcPlatform
= Options::kPlatformUnknown
;
2009 const macho_load_command
<P
>* const cmds
= (macho_load_command
<P
>*)((char*)header
+ sizeof(macho_header
<P
>));
2010 const macho_load_command
<P
>* const cmdsEnd
= (macho_load_command
<P
>*)((char*)header
+ sizeof(macho_header
<P
>) + header
->sizeofcmds());
2011 const macho_load_command
<P
>* cmd
= cmds
;
2012 for (uint32_t i
= 0; i
< cmd_count
; ++i
) {
2013 uint32_t size
= cmd
->cmdsize();
2014 if ( (size
& this->loadCommandSizeMask()) != 0 )
2015 throwf("load command #%d has a unaligned size", i
);
2016 const uint8_t* endOfCmd
= ((uint8_t*)cmd
)+cmd
->cmdsize();
2017 if ( endOfCmd
> (uint8_t*)cmdsEnd
)
2018 throwf("load command #%d extends beyond the end of the load commands", i
);
2019 if ( endOfCmd
> endOfFile
)
2020 throwf("load command #%d extends beyond the end of the file", i
);
2021 switch (cmd
->cmd()) {
2024 const macho_symtab_command
<P
>* symtab
= (macho_symtab_command
<P
>*)cmd
;
2025 _symbolCount
= symtab
->nsyms();
2026 _symbols
= (const macho_nlist
<P
>*)(_fileContent
+ symtab
->symoff());
2027 _strings
= (char*)_fileContent
+ symtab
->stroff();
2028 _stringsSize
= symtab
->strsize();
2029 if ( (symtab
->symoff() + _symbolCount
*sizeof(macho_nlist
<P
>)) > _fileLength
)
2030 throw "mach-o symbol table extends beyond end of file";
2031 if ( (_strings
+ _stringsSize
) > (char*)endOfFile
)
2032 throw "mach-o string pool extends beyond end of file";
2033 if ( _indirectTable
== NULL
) {
2034 if ( _undefinedEndIndex
== 0 ) {
2035 _undefinedStartIndex
= 0;
2036 _undefinedEndIndex
= symtab
->nsyms();
2043 const macho_dysymtab_command
<P
>* dsymtab
= (macho_dysymtab_command
<P
>*)cmd
;
2044 _indirectTable
= (uint32_t*)(_fileContent
+ dsymtab
->indirectsymoff());
2045 _indirectTableCount
= dsymtab
->nindirectsyms();
2046 if ( &_indirectTable
[_indirectTableCount
] > (uint32_t*)endOfFile
)
2047 throw "indirect symbol table extends beyond end of file";
2048 _undefinedStartIndex
= dsymtab
->iundefsym();
2049 _undefinedEndIndex
= _undefinedStartIndex
+ dsymtab
->nundefsym();
2055 case LC_DATA_IN_CODE
:
2057 const macho_linkedit_data_command
<P
>* dc
= (macho_linkedit_data_command
<P
>*)cmd
;
2058 _dataInCodeStart
= (macho_data_in_code_entry
<P
>*)(_fileContent
+ dc
->dataoff());
2059 _dataInCodeEnd
= (macho_data_in_code_entry
<P
>*)(_fileContent
+ dc
->dataoff() + dc
->datasize());
2060 if ( _dataInCodeEnd
> (macho_data_in_code_entry
<P
>*)endOfFile
)
2061 throw "LC_DATA_IN_CODE table extends beyond end of file";
2064 case LC_LINKER_OPTION
:
2066 const macho_linker_option_command
<P
>* loc
= (macho_linker_option_command
<P
>*)cmd
;
2067 const char* buffer
= loc
->buffer();
2068 _file
->_linkerOptions
.resize(_file
->_linkerOptions
.size() + 1);
2069 std::vector
<const char*>& vec
= _file
->_linkerOptions
.back();
2070 for (uint32_t j
=0; j
< loc
->count(); ++j
) {
2071 vec
.push_back(buffer
);
2072 buffer
+= strlen(buffer
) + 1;
2074 if ( buffer
> ((char*)cmd
+ loc
->cmdsize()) )
2075 throw "malformed LC_LINKER_OPTION";
2078 case LC_LINKER_OPTIMIZATION_HINTS
:
2080 const macho_linkedit_data_command
<P
>* loh
= (macho_linkedit_data_command
<P
>*)cmd
;
2081 _lohStart
= _fileContent
+ loh
->dataoff();
2082 _lohEnd
= _fileContent
+ loh
->dataoff() + loh
->datasize();
2083 if ( _lohEnd
> endOfFile
)
2084 throw "LC_LINKER_OPTIMIZATION_HINTS table extends beyond end of file";
2087 case LC_VERSION_MIN_MACOSX
:
2088 case LC_VERSION_MIN_IPHONEOS
:
2089 case LC_VERSION_MIN_WATCHOS
:
2090 #if SUPPORT_APPLE_TV
2091 case LC_VERSION_MIN_TVOS
:
2093 if ( ignoreMismatchPlatform
)
2095 _file
->_platform
= cmd
->cmd();
2096 lcPlatform
= Options::platformForLoadCommand(cmd
->cmd());
2097 _file
->_minOSVersion
= ((macho_version_min_command
<P
>*)cmd
)->version();
2099 case macho_segment_command
<P
>::CMD
:
2100 if ( segment
!= NULL
)
2101 throw "more than one LC_SEGMENT found in object file";
2102 segment
= (macho_segment_command
<P
>*)cmd
;
2105 // ignore unknown load commands
2108 cmd
= (const macho_load_command
<P
>*)(((char*)cmd
)+cmd
->cmdsize());
2109 if ( cmd
> cmdsEnd
)
2110 throwf("malformed mach-o file, load command #%d is outside size of load commands", i
);
2112 // arm/arm64 objects are default to ios platform if not set.
2113 // rdar://problem/21746314
2114 if (lcPlatform
== Options::kPlatformUnknown
&&
2115 (std::is_same
<A
, arm
>::value
|| std::is_same
<A
, arm64
>::value
))
2116 lcPlatform
= Options::kPlatformiOS
;
2118 // Check platform cross-linking.
2119 if ( !ignoreMismatchPlatform
) {
2120 if ( lcPlatform
!= platform
) {
2122 case Options::kPlatformOSX
:
2123 case Options::kPlatformiOS
:
2124 if ( lcPlatform
== Options::kPlatformUnknown
)
2126 // fall through if the Platform is not Unknown
2127 case Options::kPlatformWatchOS
:
2128 // Error when using bitcocde, warning otherwise.
2130 throwf("building for %s%s, but linking in object file built for %s,",
2131 Options::platformName(platform
), (simulator
? " simulator" : ""),
2132 Options::platformName(lcPlatform
));
2134 warning("URGENT: building for %s%s, but linking in object file (%s) built for %s. "
2135 "Note: This will be an error in the future.",
2136 Options::platformName(platform
), (simulator
? " simulator" : ""), path(),
2137 Options::platformName(lcPlatform
));
2139 #if SUPPORT_APPLE_TV
2140 case Options::kPlatform_tvOS
:
2141 // Error when using bitcocde, warning otherwise.
2143 throwf("building for %s%s, but linking in object file built for %s,",
2144 Options::platformName(platform
), (simulator
? " simulator" : ""),
2145 Options::platformName(lcPlatform
));
2147 warning("URGENT: building for %s%s, but linking in object file (%s) built for %s. "
2148 "Note: This will be an error in the future.",
2149 Options::platformName(platform
), (simulator
? " simulator" : ""), path(),
2150 Options::platformName(lcPlatform
));
2153 case Options::kPlatformUnknown
:
2154 // skip if the target platform is unknown
2158 if ( linkMinOSVersion
&& (_file
->_minOSVersion
> linkMinOSVersion
) ) {
2161 versionToString(_file
->_minOSVersion
, t1
);
2162 versionToString(linkMinOSVersion
, t2
);
2163 warning("object file (%s) was built for newer %s version (%s) than being linked (%s)",
2164 _path
, Options::platformName(lcPlatform
), t1
, t2
);
2169 // record range of sections
2170 if ( segment
== NULL
)
2171 throw "missing LC_SEGMENT";
2172 _sectionsStart
= (macho_section
<P
>*)((char*)segment
+ sizeof(macho_segment_command
<P
>));
2173 _machOSectionsCount
= segment
->nsects();
2174 if ( (sizeof(macho_segment_command
<P
>) + _machOSectionsCount
* sizeof(macho_section
<P
>)) > segment
->cmdsize() )
2175 throw "too many sections for size of LC_SEGMENT command";
2179 template <typename A
>
2180 Options::Platform Parser
<A
>::findPlatform(const macho_header
<P
>* header
)
2182 const uint32_t cmd_count
= header
->ncmds();
2183 if ( cmd_count
== 0 )
2184 return Options::kPlatformUnknown
;
2185 const macho_load_command
<P
>* const cmds
= (macho_load_command
<P
>*)((char*)header
+ sizeof(macho_header
<P
>));
2186 const macho_load_command
<P
>* const cmdsEnd
= (macho_load_command
<P
>*)((char*)header
+ sizeof(macho_header
<P
>) + header
->sizeofcmds());
2187 const macho_load_command
<P
>* cmd
= cmds
;
2188 for (uint32_t i
= 0; i
< cmd_count
; ++i
) {
2189 uint32_t size
= cmd
->cmdsize();
2190 if ( (size
& loadCommandSizeMask()) != 0 )
2191 throwf("load command #%d has a unaligned size", i
);
2192 const uint8_t* endOfCmd
= ((uint8_t*)cmd
)+cmd
->cmdsize();
2193 if ( endOfCmd
> (uint8_t*)cmdsEnd
)
2194 throwf("load command #%d extends beyond the end of the load commands", i
);
2195 switch (cmd
->cmd()) {
2196 case LC_VERSION_MIN_MACOSX
:
2197 return Options::kPlatformOSX
;
2198 case LC_VERSION_MIN_IPHONEOS
:
2199 return Options::kPlatformiOS
;
2201 cmd
= (const macho_load_command
<P
>*)(((char*)cmd
)+cmd
->cmdsize());
2202 if ( cmd
> cmdsEnd
)
2203 throwf("malformed mach-o file, load command #%d is outside size of load commands", i
);
2205 return Options::kPlatformUnknown
;
2209 template <typename A
>
2210 void Parser
<A
>::prescanSymbolTable()
2212 _tentativeDefinitionCount
= 0;
2213 _absoluteSymbolCount
= 0;
2214 _symbolsInSections
= 0;
2215 _hasDataInCodeLabels
= false;
2216 for (uint32_t i
=0; i
< this->_symbolCount
; ++i
) {
2217 const macho_nlist
<P
>& sym
= symbolFromIndex(i
);
2219 if ( (sym
.n_type() & N_STAB
) != 0 )
2222 // look at undefines
2223 const char* symbolName
= this->nameFromSymbol(sym
);
2224 if ( (sym
.n_type() & N_TYPE
) == N_UNDF
) {
2225 if ( sym
.n_value() != 0 ) {
2226 // count tentative definitions
2227 ++_tentativeDefinitionCount
;
2229 else if ( strncmp(symbolName
, "___dtrace_", 10) == 0 ) {
2230 // any undefined starting with __dtrace_*$ that is not ___dtrace_probe$* or ___dtrace_isenabled$*
2231 // is extra provider info
2232 if ( (strncmp(&symbolName
[10], "probe$", 6) != 0) && (strncmp(&symbolName
[10], "isenabled$", 10) != 0) ) {
2233 _dtraceProviderInfo
.push_back(symbolName
);
2238 else if ( ((sym
.n_type() & N_TYPE
) == N_INDR
) && ((sym
.n_type() & N_EXT
) != 0) ) {
2239 _indirectSymbolCount
++;
2243 // count absolute symbols
2244 if ( (sym
.n_type() & N_TYPE
) == N_ABS
) {
2245 const char* absName
= this->nameFromSymbol(sym
);
2246 // ignore .objc_class_name_* symbols
2247 if ( strncmp(absName
, ".objc_class_name_", 17) == 0 ) {
2251 // ignore .objc_class_name_* symbols
2252 if ( strncmp(absName
, ".objc_category_name_", 20) == 0 )
2254 // ignore empty *.eh symbols
2255 if ( strcmp(&absName
[strlen(absName
)-3], ".eh") == 0 )
2257 ++_absoluteSymbolCount
;
2260 // only look at definitions
2261 if ( (sym
.n_type() & N_TYPE
) != N_SECT
)
2264 // 'L' labels do not denote atom breaks
2265 if ( symbolName
[0] == 'L' ) {
2266 // <rdar://problem/9218847> Formalize data in code with L$start$ labels
2267 if ( strncmp(symbolName
, "L$start$", 8) == 0 )
2268 _hasDataInCodeLabels
= true;
2271 // how many def syms in each section
2272 if ( sym
.n_sect() > _machOSectionsCount
)
2273 throw "bad n_sect in symbol table";
2275 _symbolsInSections
++;
2279 template <typename A
>
2280 void Parser
<A
>::appendAliasAtoms(uint8_t* p
)
2282 for (uint32_t i
=0; i
< this->_symbolCount
; ++i
) {
2283 const macho_nlist
<P
>& sym
= symbolFromIndex(i
);
2285 if ( (sym
.n_type() & N_STAB
) != 0 )
2288 // only look at N_INDR symbols
2289 if ( (sym
.n_type() & N_TYPE
) != N_INDR
)
2292 // skip non-external aliases
2293 if ( (sym
.n_type() & N_EXT
) == 0 )
2296 const char* symbolName
= this->nameFromSymbol(sym
);
2297 const char* aliasOfName
= &_strings
[sym
.n_value()];
2298 bool isHiddenVisibility
= (sym
.n_type() & N_PEXT
);
2299 AliasAtom
* allocatedSpace
= (AliasAtom
*)p
;
2300 new (allocatedSpace
) AliasAtom(symbolName
, isHiddenVisibility
, _file
, aliasOfName
);
2301 p
+= sizeof(AliasAtom
);
2307 template <typename A
>
2308 int Parser
<A
>::sectionIndexSorter(void* extra
, const void* l
, const void* r
)
2310 Parser
<A
>* parser
= (Parser
<A
>*)extra
;
2311 const uint32_t* left
= (uint32_t*)l
;
2312 const uint32_t* right
= (uint32_t*)r
;
2313 const macho_section
<P
>* leftSect
= parser
->machOSectionFromSectionIndex(*left
);
2314 const macho_section
<P
>* rightSect
= parser
->machOSectionFromSectionIndex(*right
);
2316 // can't just return difference because 64-bit diff does not fit in 32-bit return type
2317 int64_t result
= leftSect
->addr() - rightSect
->addr();
2318 if ( result
== 0 ) {
2319 // two sections with same start address
2320 // one with zero size goes first
2321 bool leftEmpty
= ( leftSect
->size() == 0 );
2322 bool rightEmpty
= ( rightSect
->size() == 0 );
2323 if ( leftEmpty
!= rightEmpty
) {
2324 return ( rightEmpty
? 1 : -1 );
2326 if ( !leftEmpty
&& !rightEmpty
)
2327 throwf("overlapping sections");
2328 // both empty, so chose file order
2329 return ( rightSect
- leftSect
);
2331 else if ( result
< 0 )
2337 template <typename A
>
2338 void Parser
<A
>::makeSortedSectionsArray(uint32_t array
[])
2340 const bool log
= false;
2343 fprintf(stderr
, "unsorted sections:\n");
2344 for(unsigned int i
=0; i
< _machOSectionsCount
; ++i
)
2345 fprintf(stderr
, "0x%08llX %s %s\n", _sectionsStart
[i
].addr(), _sectionsStart
[i
].segname(), _sectionsStart
[i
].sectname());
2348 // sort by symbol table address
2349 for (uint32_t i
=0; i
< _machOSectionsCount
; ++i
)
2351 ::qsort_r(array
, _machOSectionsCount
, sizeof(uint32_t), this, §ionIndexSorter
);
2354 fprintf(stderr
, "sorted sections:\n");
2355 for(unsigned int i
=0; i
< _machOSectionsCount
; ++i
)
2356 fprintf(stderr
, "0x%08llX %s %s\n", _sectionsStart
[array
[i
]].addr(), _sectionsStart
[array
[i
]].segname(), _sectionsStart
[array
[i
]].sectname());
2362 template <typename A
>
2363 int Parser
<A
>::symbolIndexSorter(void* extra
, const void* l
, const void* r
)
2365 ParserAndSectionsArray
* extraInfo
= (ParserAndSectionsArray
*)extra
;
2366 Parser
<A
>* parser
= extraInfo
->parser
;
2367 const uint32_t* sortedSectionsArray
= extraInfo
->sortedSectionsArray
;
2368 const uint32_t* left
= (uint32_t*)l
;
2369 const uint32_t* right
= (uint32_t*)r
;
2370 const macho_nlist
<P
>& leftSym
= parser
->symbolFromIndex(*left
);
2371 const macho_nlist
<P
>& rightSym
= parser
->symbolFromIndex(*right
);
2372 // can't just return difference because 64-bit diff does not fit in 32-bit return type
2373 int64_t result
= leftSym
.n_value() - rightSym
.n_value();
2374 if ( result
== 0 ) {
2375 // two symbols with same address
2376 // if in different sections, sort earlier section first
2377 if ( leftSym
.n_sect() != rightSym
.n_sect() ) {
2378 for (uint32_t i
=0; i
< parser
->machOSectionCount(); ++i
) {
2379 if ( sortedSectionsArray
[i
]+1 == leftSym
.n_sect() )
2381 if ( sortedSectionsArray
[i
]+1 == rightSym
.n_sect() )
2385 // two symbols in same section, means one is an alias
2386 // if one is ltmp*, make it an alias (sort first)
2387 const char* leftName
= parser
->nameFromSymbol(leftSym
);
2388 const char* rightName
= parser
->nameFromSymbol(rightSym
);
2389 bool leftIsTmp
= strncmp(leftName
, "ltmp", 4);
2390 bool rightIsTmp
= strncmp(rightName
, "ltmp", 4);
2391 if ( leftIsTmp
!= rightIsTmp
) {
2392 return (rightIsTmp
? -1 : 1);
2395 // if only one is global, make the other an alias (sort first)
2396 if ( (leftSym
.n_type() & N_EXT
) != (rightSym
.n_type() & N_EXT
) ) {
2397 if ( (rightSym
.n_type() & N_EXT
) != 0 )
2402 // if both are global, sort alphabetically. earlier one will be the alias
2403 return ( strcmp(rightName
, leftName
) );
2405 else if ( result
< 0 )
2412 template <typename A
>
2413 void Parser
<A
>::makeSortedSymbolsArray(uint32_t array
[], const uint32_t sectionArray
[])
2415 const bool log
= false;
2417 uint32_t* p
= array
;
2418 for (uint32_t i
=0; i
< this->_symbolCount
; ++i
) {
2419 const macho_nlist
<P
>& sym
= symbolFromIndex(i
);
2421 if ( (sym
.n_type() & N_STAB
) != 0 )
2424 // only look at definitions
2425 if ( (sym
.n_type() & N_TYPE
) != N_SECT
)
2428 // 'L' labels do not denote atom breaks
2429 const char* symbolName
= this->nameFromSymbol(sym
);
2430 if ( symbolName
[0] == 'L' )
2433 // how many def syms in each section
2434 if ( sym
.n_sect() > _machOSectionsCount
)
2435 throw "bad n_sect in symbol table";
2440 assert(p
== &array
[_symbolsInSections
] && "second pass over symbol table yield a different number of symbols");
2442 // sort by symbol table address
2443 ParserAndSectionsArray extra
= { this, sectionArray
};
2444 ::qsort_r(array
, _symbolsInSections
, sizeof(uint32_t), &extra
, &symbolIndexSorter
);
2447 // look for two symbols at same address
2448 _overlappingSymbols
= false;
2449 for (unsigned int i
=1; i
< _symbolsInSections
; ++i
) {
2450 if ( symbolFromIndex(array
[i
-1]).n_value() == symbolFromIndex(array
[i
]).n_value() ) {
2451 //fprintf(stderr, "overlapping symbols at 0x%08llX\n", symbolFromIndex(array[i-1]).n_value());
2452 _overlappingSymbols
= true;
2458 fprintf(stderr
, "sorted symbols:\n");
2459 for(unsigned int i
=0; i
< _symbolsInSections
; ++i
)
2460 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
])) );
2464 template <typename A
>
2465 void Parser
<A
>::makeSections()
2467 // classify each section by type
2468 // compute how many Section objects will be needed and total size for all
2469 unsigned int totalSectionsSize
= 0;
2470 uint8_t machOSectsStorage
[sizeof(MachOSectionAndSectionClass
<P
>)*(_machOSectionsCount
+2)]; // also room for tentative-defs and absolute symbols
2471 // allocate raw storage for all section objects on stack
2472 MachOSectionAndSectionClass
<P
>* machOSects
= (MachOSectionAndSectionClass
<P
>*)machOSectsStorage
;
2473 unsigned int count
= 0;
2474 // local variable for bitcode parsing
2475 const macho_section
<P
>* bitcodeSect
= NULL
;
2476 const macho_section
<P
>* cmdlineSect
= NULL
;
2477 const macho_section
<P
>* swiftCmdlineSect
= NULL
;
2478 const macho_section
<P
>* bundleSect
= NULL
;
2479 bool bitcodeAsm
= false;
2481 for (uint32_t i
=0; i
< _machOSectionsCount
; ++i
) {
2482 const macho_section
<P
>* sect
= &_sectionsStart
[i
];
2483 uint8_t sectionType
= (sect
->flags() & SECTION_TYPE
);
2484 if ( (sect
->offset() + sect
->size() > _fileLength
) && (sectionType
!= S_ZEROFILL
) && (sectionType
!= S_THREAD_LOCAL_ZEROFILL
) )
2485 throwf("section %s/%s extends beyond end of file,", sect
->segname(), sect
->sectname());
2487 if ( (sect
->flags() & S_ATTR_DEBUG
) != 0 ) {
2488 if ( strcmp(sect
->segname(), "__DWARF") == 0 ) {
2489 // note that .o file has dwarf
2490 _file
->_debugInfoKind
= ld::relocatable::File::kDebugInfoDwarf
;
2491 // save off iteresting dwarf sections
2492 if ( strcmp(sect
->sectname(), "__debug_info") == 0 )
2493 _file
->_dwarfDebugInfoSect
= sect
;
2494 else if ( strcmp(sect
->sectname(), "__debug_abbrev") == 0 )
2495 _file
->_dwarfDebugAbbrevSect
= sect
;
2496 else if ( strcmp(sect
->sectname(), "__debug_line") == 0 )
2497 _file
->_dwarfDebugLineSect
= sect
;
2498 else if ( strcmp(sect
->sectname(), "__debug_str") == 0 )
2499 _file
->_dwarfDebugStringSect
= sect
;
2500 // linker does not propagate dwarf sections to output file
2503 else if ( strcmp(sect
->segname(), "__LD") == 0 ) {
2504 if ( strncmp(sect
->sectname(), "__compact_unwind", 16) == 0 ) {
2505 machOSects
[count
].sect
= sect
;
2506 totalSectionsSize
+= sizeof(CUSection
<A
>);
2507 machOSects
[count
++].type
= sectionTypeCompactUnwind
;
2512 if ( strcmp(sect
->segname(), "__LLVM") == 0 ) {
2513 // Process bitcode segement
2514 if ( strncmp(sect
->sectname(), "__bitcode", 9) == 0 ) {
2516 } else if ( strncmp(sect
->sectname(), "__cmdline", 9) == 0 ) {
2518 } else if ( strncmp(sect
->sectname(), "__swift_cmdline", 15) == 0 ) {
2519 swiftCmdlineSect
= sect
;
2520 } else if ( strncmp(sect
->sectname(), "__bundle", 8) == 0 ) {
2522 } else if ( strncmp(sect
->sectname(), "__asm", 5) == 0 ) {
2525 // If treat the bitcode as data, continue to parse as a normal section.
2526 if ( !_treateBitcodeAsData
)
2529 // ignore empty __OBJC sections
2530 if ( (sect
->size() == 0) && (strcmp(sect
->segname(), "__OBJC") == 0) )
2532 // objc image info section is really attributes and not content
2533 if ( ((strcmp(sect
->sectname(), "__image_info") == 0) && (strcmp(sect
->segname(), "__OBJC") == 0))
2534 || ((strncmp(sect
->sectname(), "__objc_imageinfo", 16) == 0) && (strcmp(sect
->segname(), "__DATA") == 0)) ) {
2535 // struct objc_image_info {
2536 // uint32_t version; // initially 0
2539 // #define OBJC_IMAGE_SUPPORTS_GC 2
2540 // #define OBJC_IMAGE_GC_ONLY 4
2541 // #define OBJC_IMAGE_IS_SIMULATED 32
2542 // #define OBJC_IMAGE_HAS_CATEGORY_CLASS_PROPERTIES 64
2544 const uint32_t* contents
= (uint32_t*)(_file
->fileContent()+sect
->offset());
2545 if ( (sect
->size() >= 8) && (contents
[0] == 0) ) {
2546 uint32_t flags
= E::get32(contents
[1]);
2547 if ( (flags
& 4) == 4 )
2548 _file
->_objConstraint
= ld::File::objcConstraintGC
;
2549 else if ( (flags
& 2) == 2 )
2550 _file
->_objConstraint
= ld::File::objcConstraintRetainReleaseOrGC
;
2551 else if ( (flags
& 32) == 32 )
2552 _file
->_objConstraint
= ld::File::objcConstraintRetainReleaseForSimulator
;
2554 _file
->_objConstraint
= ld::File::objcConstraintRetainRelease
;
2555 _file
->_swiftVersion
= ((flags
>> 8) & 0xFF);
2556 _file
->_objcHasCategoryClassPropertiesField
= (flags
& 64);
2557 if ( sect
->size() > 8 ) {
2558 warning("section %s/%s has unexpectedly large size %llu in %s",
2559 sect
->segname(), Section
<A
>::makeSectionName(sect
), sect
->size(), _file
->path());
2563 warning("can't parse %s/%s section in %s", sect
->segname(), Section
<A
>::makeSectionName(sect
), _file
->path());
2567 machOSects
[count
].sect
= sect
;
2568 switch ( sect
->flags() & SECTION_TYPE
) {
2569 case S_SYMBOL_STUBS
:
2570 if ( _stubsSectionNum
== 0 ) {
2571 _stubsSectionNum
= i
+1;
2572 _stubsMachOSection
= sect
;
2575 assert(1 && "multiple S_SYMBOL_STUBS sections");
2576 case S_LAZY_SYMBOL_POINTERS
:
2578 case S_4BYTE_LITERALS
:
2579 totalSectionsSize
+= sizeof(Literal4Section
<A
>);
2580 machOSects
[count
++].type
= sectionTypeLiteral4
;
2582 case S_8BYTE_LITERALS
:
2583 totalSectionsSize
+= sizeof(Literal8Section
<A
>);
2584 machOSects
[count
++].type
= sectionTypeLiteral8
;
2586 case S_16BYTE_LITERALS
:
2587 totalSectionsSize
+= sizeof(Literal16Section
<A
>);
2588 machOSects
[count
++].type
= sectionTypeLiteral16
;
2590 case S_NON_LAZY_SYMBOL_POINTERS
:
2591 totalSectionsSize
+= sizeof(NonLazyPointerSection
<A
>);
2592 machOSects
[count
++].type
= sectionTypeNonLazy
;
2594 case S_THREAD_LOCAL_VARIABLE_POINTERS
:
2595 totalSectionsSize
+= sizeof(TLVPointerSection
<A
>);
2596 machOSects
[count
++].type
= sectionTypeTLVPointers
;
2598 case S_LITERAL_POINTERS
:
2599 if ( (strcmp(sect
->segname(), "__OBJC") == 0) && (strcmp(sect
->sectname(), "__cls_refs") == 0) ) {
2600 totalSectionsSize
+= sizeof(Objc1ClassReferences
<A
>);
2601 machOSects
[count
++].type
= sectionTypeObjC1ClassRefs
;
2604 totalSectionsSize
+= sizeof(PointerToCStringSection
<A
>);
2605 machOSects
[count
++].type
= sectionTypeCStringPointer
;
2608 case S_CSTRING_LITERALS
:
2609 totalSectionsSize
+= sizeof(CStringSection
<A
>);
2610 machOSects
[count
++].type
= sectionTypeCString
;
2612 case S_MOD_INIT_FUNC_POINTERS
:
2613 case S_MOD_TERM_FUNC_POINTERS
:
2614 case S_THREAD_LOCAL_INIT_FUNCTION_POINTERS
:
2619 case S_THREAD_LOCAL_REGULAR
:
2620 case S_THREAD_LOCAL_ZEROFILL
:
2621 if ( (strcmp(sect
->segname(), "__TEXT") == 0) && (strcmp(sect
->sectname(), "__eh_frame") == 0) ) {
2622 totalSectionsSize
+= sizeof(CFISection
<A
>);
2623 machOSects
[count
++].type
= sectionTypeCFI
;
2625 else if ( (strcmp(sect
->segname(), "__DATA") == 0) && (strcmp(sect
->sectname(), "__cfstring") == 0) ) {
2626 totalSectionsSize
+= sizeof(CFStringSection
<A
>);
2627 machOSects
[count
++].type
= sectionTypeCFString
;
2629 else if ( (strcmp(sect
->segname(), "__TEXT") == 0) && (strcmp(sect
->sectname(), "__ustring") == 0) ) {
2630 totalSectionsSize
+= sizeof(UTF16StringSection
<A
>);
2631 machOSects
[count
++].type
= sectionTypeUTF16Strings
;
2633 else if ( (strcmp(sect
->segname(), "__DATA") == 0) && (strncmp(sect
->sectname(), "__objc_classrefs", 16) == 0) ) {
2634 totalSectionsSize
+= sizeof(ObjC2ClassRefsSection
<A
>);
2635 machOSects
[count
++].type
= sectionTypeObjC2ClassRefs
;
2637 else if ( (strcmp(sect
->segname(), "__DATA") == 0) && (strcmp(sect
->sectname(), "__objc_catlist") == 0) ) {
2638 totalSectionsSize
+= sizeof(ObjC2CategoryListSection
<A
>);
2639 machOSects
[count
++].type
= typeObjC2CategoryList
;
2641 else if ( _AppleObjc
&& (strcmp(sect
->segname(), "__OBJC") == 0) && (strcmp(sect
->sectname(), "__class") == 0) ) {
2642 totalSectionsSize
+= sizeof(ObjC1ClassSection
<A
>);
2643 machOSects
[count
++].type
= sectionTypeObjC1Classes
;
2646 totalSectionsSize
+= sizeof(SymboledSection
<A
>);
2647 machOSects
[count
++].type
= sectionTypeSymboled
;
2650 case S_THREAD_LOCAL_VARIABLES
:
2651 totalSectionsSize
+= sizeof(TLVDefsSection
<A
>);
2652 machOSects
[count
++].type
= sectionTypeTLVDefs
;
2655 throwf("unknown section type %d", sect
->flags() & SECTION_TYPE
);
2660 if ( bitcodeSect
!= NULL
) {
2661 if ( cmdlineSect
!= NULL
)
2662 _file
->_bitcode
= std::unique_ptr
<ld::Bitcode
>(new ld::ClangBitcode(&_fileContent
[bitcodeSect
->offset()], bitcodeSect
->size(),
2663 &_fileContent
[cmdlineSect
->offset()], cmdlineSect
->size()));
2664 else if ( swiftCmdlineSect
!= NULL
)
2665 _file
->_bitcode
= std::unique_ptr
<ld::Bitcode
>(new ld::SwiftBitcode(&_fileContent
[bitcodeSect
->offset()], bitcodeSect
->size(),
2666 &_fileContent
[swiftCmdlineSect
->offset()], swiftCmdlineSect
->size()));
2668 throwf("Object file with bitcode missing cmdline options: %s", _file
->path());
2670 else if ( bundleSect
!= NULL
)
2671 _file
->_bitcode
= std::unique_ptr
<ld::Bitcode
>(new ld::BundleBitcode(&_fileContent
[bundleSect
->offset()], bundleSect
->size()));
2672 else if ( bitcodeAsm
)
2673 _file
->_bitcode
= std::unique_ptr
<ld::Bitcode
>(new ld::AsmBitcode(_fileContent
, _fileLength
));
2675 // sort by address (mach-o object files don't aways have sections sorted)
2676 ::qsort(machOSects
, count
, sizeof(MachOSectionAndSectionClass
<P
>), MachOSectionAndSectionClass
<P
>::sorter
);
2678 // we will synthesize a dummy Section<A> object for tentative definitions
2679 if ( _tentativeDefinitionCount
> 0 ) {
2680 totalSectionsSize
+= sizeof(TentativeDefinitionSection
<A
>);
2681 machOSects
[count
++].type
= sectionTypeTentativeDefinitions
;
2684 // we will synthesize a dummy Section<A> object for Absolute symbols
2685 if ( _absoluteSymbolCount
> 0 ) {
2686 totalSectionsSize
+= sizeof(AbsoluteSymbolSection
<A
>);
2687 machOSects
[count
++].type
= sectionTypeAbsoluteSymbols
;
2690 // allocate one block for all Section objects as well as pointers to each
2691 uint8_t* space
= new uint8_t[totalSectionsSize
+count
*sizeof(Section
<A
>*)];
2692 _file
->_sectionsArray
= (Section
<A
>**)space
;
2693 _file
->_sectionsArrayCount
= count
;
2694 Section
<A
>** objects
= _file
->_sectionsArray
;
2695 space
+= count
*sizeof(Section
<A
>*);
2696 for (uint32_t i
=0; i
< count
; ++i
) {
2697 switch ( machOSects
[i
].type
) {
2698 case sectionTypeIgnore
:
2700 case sectionTypeLiteral4
:
2701 *objects
++ = new (space
) Literal4Section
<A
>(*this, *_file
, machOSects
[i
].sect
);
2702 space
+= sizeof(Literal4Section
<A
>);
2704 case sectionTypeLiteral8
:
2705 *objects
++ = new (space
) Literal8Section
<A
>(*this, *_file
, machOSects
[i
].sect
);
2706 space
+= sizeof(Literal8Section
<A
>);
2708 case sectionTypeLiteral16
:
2709 *objects
++ = new (space
) Literal16Section
<A
>(*this, *_file
, machOSects
[i
].sect
);
2710 space
+= sizeof(Literal16Section
<A
>);
2712 case sectionTypeNonLazy
:
2713 *objects
++ = new (space
) NonLazyPointerSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2714 space
+= sizeof(NonLazyPointerSection
<A
>);
2716 case sectionTypeTLVPointers
:
2717 *objects
++ = new (space
) TLVPointerSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2718 space
+= sizeof(TLVPointerSection
<A
>);
2720 case sectionTypeCFI
:
2721 _EHFrameSection
= new (space
) CFISection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2722 *objects
++ = _EHFrameSection
;
2723 space
+= sizeof(CFISection
<A
>);
2725 case sectionTypeCString
:
2726 *objects
++ = new (space
) CStringSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2727 space
+= sizeof(CStringSection
<A
>);
2729 case sectionTypeCStringPointer
:
2730 *objects
++ = new (space
) PointerToCStringSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2731 space
+= sizeof(PointerToCStringSection
<A
>);
2733 case sectionTypeObjC1ClassRefs
:
2734 *objects
++ = new (space
) Objc1ClassReferences
<A
>(*this, *_file
, machOSects
[i
].sect
);
2735 space
+= sizeof(Objc1ClassReferences
<A
>);
2737 case sectionTypeUTF16Strings
:
2738 *objects
++ = new (space
) UTF16StringSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2739 space
+= sizeof(UTF16StringSection
<A
>);
2741 case sectionTypeCFString
:
2742 *objects
++ = new (space
) CFStringSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2743 space
+= sizeof(CFStringSection
<A
>);
2745 case sectionTypeObjC2ClassRefs
:
2746 *objects
++ = new (space
) ObjC2ClassRefsSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2747 space
+= sizeof(ObjC2ClassRefsSection
<A
>);
2749 case typeObjC2CategoryList
:
2750 *objects
++ = new (space
) ObjC2CategoryListSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2751 space
+= sizeof(ObjC2CategoryListSection
<A
>);
2753 case sectionTypeObjC1Classes
:
2754 *objects
++ = new (space
) ObjC1ClassSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2755 space
+= sizeof(ObjC1ClassSection
<A
>);
2757 case sectionTypeSymboled
:
2758 *objects
++ = new (space
) SymboledSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2759 space
+= sizeof(SymboledSection
<A
>);
2761 case sectionTypeTLVDefs
:
2762 *objects
++ = new (space
) TLVDefsSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2763 space
+= sizeof(TLVDefsSection
<A
>);
2765 case sectionTypeCompactUnwind
:
2766 _compactUnwindSection
= new (space
) CUSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2767 *objects
++ = _compactUnwindSection
;
2768 space
+= sizeof(CUSection
<A
>);
2770 case sectionTypeTentativeDefinitions
:
2771 *objects
++ = new (space
) TentativeDefinitionSection
<A
>(*this, *_file
);
2772 space
+= sizeof(TentativeDefinitionSection
<A
>);
2774 case sectionTypeAbsoluteSymbols
:
2775 _absoluteSection
= new (space
) AbsoluteSymbolSection
<A
>(*this, *_file
);
2776 *objects
++ = _absoluteSection
;
2777 space
+= sizeof(AbsoluteSymbolSection
<A
>);
2780 throw "internal error uknown SectionType";
2786 template <typename A
>
2787 Section
<A
>* Parser
<A
>::sectionForAddress(typename
A::P::uint_t addr
)
2789 for (uint32_t i
=0; i
< _file
->_sectionsArrayCount
; ++i
) {
2790 const macho_section
<typename
A::P
>* sect
= _file
->_sectionsArray
[i
]->machoSection();
2791 // TentativeDefinitionSection and AbsoluteSymbolSection have no mach-o section
2792 if ( sect
!= NULL
) {
2793 if ( (sect
->addr() <= addr
) && (addr
< (sect
->addr()+sect
->size())) ) {
2794 return _file
->_sectionsArray
[i
];
2798 // not strictly in any section
2799 // may be in a zero length section
2800 for (uint32_t i
=0; i
< _file
->_sectionsArrayCount
; ++i
) {
2801 const macho_section
<typename
A::P
>* sect
= _file
->_sectionsArray
[i
]->machoSection();
2802 // TentativeDefinitionSection and AbsoluteSymbolSection have no mach-o section
2803 if ( sect
!= NULL
) {
2804 if ( (sect
->addr() == addr
) && (sect
->size() == 0) ) {
2805 return _file
->_sectionsArray
[i
];
2810 throwf("sectionForAddress(0x%llX) address not in any section", (uint64_t)addr
);
2813 template <typename A
>
2814 Section
<A
>* Parser
<A
>::sectionForNum(unsigned int num
)
2816 for (uint32_t i
=0; i
< _file
->_sectionsArrayCount
; ++i
) {
2817 const macho_section
<typename
A::P
>* sect
= _file
->_sectionsArray
[i
]->machoSection();
2818 // TentativeDefinitionSection and AbsoluteSymbolSection have no mach-o section
2819 if ( sect
!= NULL
) {
2820 if ( num
== (unsigned int)((sect
- _sectionsStart
)+1) )
2821 return _file
->_sectionsArray
[i
];
2824 throwf("sectionForNum(%u) section number not for any section", num
);
2827 template <typename A
>
2828 Atom
<A
>* Parser
<A
>::findAtomByAddress(pint_t addr
)
2830 Section
<A
>* section
= this->sectionForAddress(addr
);
2831 return section
->findAtomByAddress(addr
);
2834 template <typename A
>
2835 Atom
<A
>* Parser
<A
>::findAtomByAddressOrNullIfStub(pint_t addr
)
2837 if ( hasStubsSection() && (_stubsMachOSection
->addr() <= addr
) && (addr
< (_stubsMachOSection
->addr()+_stubsMachOSection
->size())) )
2839 return findAtomByAddress(addr
);
2842 template <typename A
>
2843 Atom
<A
>* Parser
<A
>::findAtomByAddressOrLocalTargetOfStub(pint_t addr
, uint32_t* offsetInAtom
)
2845 if ( hasStubsSection() && (_stubsMachOSection
->addr() <= addr
) && (addr
< (_stubsMachOSection
->addr()+_stubsMachOSection
->size())) ) {
2846 // target is a stub, remove indirection
2847 uint32_t symbolIndex
= this->symbolIndexFromIndirectSectionAddress(addr
, _stubsMachOSection
);
2848 assert(symbolIndex
!= INDIRECT_SYMBOL_LOCAL
);
2849 const macho_nlist
<P
>& sym
= this->symbolFromIndex(symbolIndex
);
2850 // can't be to external weak symbol
2851 assert( (this->combineFromSymbol(sym
) != ld::Atom::combineByName
) || (this->scopeFromSymbol(sym
) != ld::Atom::scopeGlobal
) );
2853 return this->findAtomByName(this->nameFromSymbol(sym
));
2855 Atom
<A
>* target
= this->findAtomByAddress(addr
);
2856 *offsetInAtom
= addr
- target
->_objAddress
;
2860 template <typename A
>
2861 Atom
<A
>* Parser
<A
>::findAtomByName(const char* name
)
2863 uint8_t* p
= _file
->_atomsArray
;
2864 for(int i
=_file
->_atomsArrayCount
; i
> 0; --i
) {
2865 Atom
<A
>* atom
= (Atom
<A
>*)p
;
2866 if ( strcmp(name
, atom
->name()) == 0 )
2868 p
+= sizeof(Atom
<A
>);
2873 template <typename A
>
2874 void Parser
<A
>::findTargetFromAddress(pint_t addr
, TargetDesc
& target
)
2876 if ( hasStubsSection() && (_stubsMachOSection
->addr() <= addr
) && (addr
< (_stubsMachOSection
->addr()+_stubsMachOSection
->size())) ) {
2877 // target is a stub, remove indirection
2878 uint32_t symbolIndex
= this->symbolIndexFromIndirectSectionAddress(addr
, _stubsMachOSection
);
2879 assert(symbolIndex
!= INDIRECT_SYMBOL_LOCAL
);
2880 const macho_nlist
<P
>& sym
= this->symbolFromIndex(symbolIndex
);
2882 target
.name
= this->nameFromSymbol(sym
);
2883 target
.weakImport
= this->weakImportFromSymbol(sym
);
2887 Section
<A
>* section
= this->sectionForAddress(addr
);
2888 target
.atom
= section
->findAtomByAddress(addr
);
2889 target
.addend
= addr
- target
.atom
->_objAddress
;
2890 target
.weakImport
= false;
2894 template <typename A
>
2895 void Parser
<A
>::findTargetFromAddress(pint_t baseAddr
, pint_t addr
, TargetDesc
& target
)
2897 findTargetFromAddress(baseAddr
, target
);
2898 target
.addend
= addr
- target
.atom
->_objAddress
;
2901 template <typename A
>
2902 void Parser
<A
>::findTargetFromAddressAndSectionNum(pint_t addr
, unsigned int sectNum
, TargetDesc
& target
)
2904 if ( sectNum
== R_ABS
) {
2905 // target is absolute symbol that corresponds to addr
2906 if ( _absoluteSection
!= NULL
) {
2907 target
.atom
= _absoluteSection
->findAbsAtomForValue(addr
);
2908 if ( target
.atom
!= NULL
) {
2910 target
.weakImport
= false;
2915 throwf("R_ABS reloc but no absolute symbol at target address");
2918 if ( hasStubsSection() && (stubsSectionNum() == sectNum
) ) {
2919 // target is a stub, remove indirection
2920 uint32_t symbolIndex
= this->symbolIndexFromIndirectSectionAddress(addr
, _stubsMachOSection
);
2921 assert(symbolIndex
!= INDIRECT_SYMBOL_LOCAL
);
2922 const macho_nlist
<P
>& sym
= this->symbolFromIndex(symbolIndex
);
2923 // use direct reference when stub is to a static function
2924 if ( ((sym
.n_type() & N_TYPE
) == N_SECT
) && (((sym
.n_type() & N_EXT
) == 0) || (this->nameFromSymbol(sym
)[0] == 'L')) ) {
2925 this->findTargetFromAddressAndSectionNum(sym
.n_value(), sym
.n_sect(), target
);
2929 target
.name
= this->nameFromSymbol(sym
);
2930 target
.weakImport
= this->weakImportFromSymbol(sym
);
2935 Section
<A
>* section
= this->sectionForNum(sectNum
);
2936 target
.atom
= section
->findAtomByAddress(addr
);
2937 if ( target
.atom
== NULL
) {
2938 typedef typename
A::P::sint_t sint_t
;
2939 sint_t a
= (sint_t
)addr
;
2940 sint_t sectStart
= (sint_t
)(section
->machoSection()->addr());
2941 sint_t sectEnd
= sectStart
+ section
->machoSection()->size();
2942 if ( a
< sectStart
) {
2943 // target address is before start of section, so must be negative addend
2944 target
.atom
= section
->findAtomByAddress(sectStart
);
2945 target
.addend
= a
- sectStart
;
2946 target
.weakImport
= false;
2950 else if ( a
>= sectEnd
) {
2951 target
.atom
= section
->findAtomByAddress(sectEnd
-1);
2952 target
.addend
= a
- sectEnd
;
2953 target
.weakImport
= false;
2958 assert(target
.atom
!= NULL
);
2959 target
.addend
= addr
- target
.atom
->_objAddress
;
2960 target
.weakImport
= false;
2964 template <typename A
>
2965 void Parser
<A
>::addDtraceExtraInfos(const SourceLocation
& src
, const char* providerName
)
2967 // for every ___dtrace_stability$* and ___dtrace_typedefs$* undefine with
2968 // a matching provider name, add a by-name kDtraceTypeReference at probe site
2969 const char* dollar
= strchr(providerName
, '$');
2970 if ( dollar
!= NULL
) {
2971 int providerNameLen
= dollar
-providerName
+1;
2972 for ( std::vector
<const char*>::iterator it
= _dtraceProviderInfo
.begin(); it
!= _dtraceProviderInfo
.end(); ++it
) {
2973 const char* typeDollar
= strchr(*it
, '$');
2974 if ( typeDollar
!= NULL
) {
2975 if ( strncmp(typeDollar
+1, providerName
, providerNameLen
) == 0 ) {
2976 addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindDtraceExtra
,false, *it
);
2983 template <typename A
>
2984 const char* Parser
<A
>::scanSymbolTableForAddress(uint64_t addr
)
2986 uint64_t closestSymAddr
= 0;
2987 const char* closestSymName
= NULL
;
2988 for (uint32_t i
=0; i
< this->_symbolCount
; ++i
) {
2989 const macho_nlist
<P
>& sym
= symbolFromIndex(i
);
2991 if ( (sym
.n_type() & N_STAB
) != 0 )
2994 // only look at definitions
2995 if ( (sym
.n_type() & N_TYPE
) != N_SECT
)
2998 // return with exact match
2999 if ( sym
.n_value() == addr
) {
3000 const char* name
= nameFromSymbol(sym
);
3001 if ( strncmp(name
, "ltmp", 4) != 0 )
3003 // treat 'ltmp*' labels as close match
3004 closestSymAddr
= sym
.n_value();
3005 closestSymName
= name
;
3008 // record closest seen so far
3009 if ( (sym
.n_value() < addr
) && ((sym
.n_value() > closestSymAddr
) || (closestSymName
== NULL
)) )
3010 closestSymName
= nameFromSymbol(sym
);
3013 return (closestSymName
!= NULL
) ? closestSymName
: "unknown";
3017 template <typename A
>
3018 void Parser
<A
>::addFixups(const SourceLocation
& src
, ld::Fixup::Kind setKind
, const TargetDesc
& target
)
3020 // some fixup pairs can be combined
3021 ld::Fixup::Cluster cl
= ld::Fixup::k1of3
;
3022 ld::Fixup::Kind firstKind
= ld::Fixup::kindSetTargetAddress
;
3023 bool combined
= false;
3024 if ( target
.addend
== 0 ) {
3025 cl
= ld::Fixup::k1of1
;
3027 switch ( setKind
) {
3028 case ld::Fixup::kindStoreLittleEndian32
:
3029 firstKind
= ld::Fixup::kindStoreTargetAddressLittleEndian32
;
3031 case ld::Fixup::kindStoreLittleEndian64
:
3032 firstKind
= ld::Fixup::kindStoreTargetAddressLittleEndian64
;
3034 case ld::Fixup::kindStoreBigEndian32
:
3035 firstKind
= ld::Fixup::kindStoreTargetAddressBigEndian32
;
3037 case ld::Fixup::kindStoreBigEndian64
:
3038 firstKind
= ld::Fixup::kindStoreTargetAddressBigEndian64
;
3040 case ld::Fixup::kindStoreX86BranchPCRel32
:
3041 firstKind
= ld::Fixup::kindStoreTargetAddressX86BranchPCRel32
;
3043 case ld::Fixup::kindStoreX86PCRel32
:
3044 firstKind
= ld::Fixup::kindStoreTargetAddressX86PCRel32
;
3046 case ld::Fixup::kindStoreX86PCRel32GOTLoad
:
3047 firstKind
= ld::Fixup::kindStoreTargetAddressX86PCRel32GOTLoad
;
3049 case ld::Fixup::kindStoreX86PCRel32TLVLoad
:
3050 firstKind
= ld::Fixup::kindStoreTargetAddressX86PCRel32TLVLoad
;
3052 case ld::Fixup::kindStoreX86Abs32TLVLoad
:
3053 firstKind
= ld::Fixup::kindStoreTargetAddressX86Abs32TLVLoad
;
3055 case ld::Fixup::kindStoreARMBranch24
:
3056 firstKind
= ld::Fixup::kindStoreTargetAddressARMBranch24
;
3058 case ld::Fixup::kindStoreThumbBranch22
:
3059 firstKind
= ld::Fixup::kindStoreTargetAddressThumbBranch22
;
3061 #if SUPPORT_ARCH_arm64
3062 case ld::Fixup::kindStoreARM64Branch26
:
3063 firstKind
= ld::Fixup::kindStoreTargetAddressARM64Branch26
;
3065 case ld::Fixup::kindStoreARM64Page21
:
3066 firstKind
= ld::Fixup::kindStoreTargetAddressARM64Page21
;
3068 case ld::Fixup::kindStoreARM64PageOff12
:
3069 firstKind
= ld::Fixup::kindStoreTargetAddressARM64PageOff12
;
3071 case ld::Fixup::kindStoreARM64GOTLoadPage21
:
3072 firstKind
= ld::Fixup::kindStoreTargetAddressARM64GOTLoadPage21
;
3074 case ld::Fixup::kindStoreARM64GOTLoadPageOff12
:
3075 firstKind
= ld::Fixup::kindStoreTargetAddressARM64GOTLoadPageOff12
;
3077 case ld::Fixup::kindStoreARM64TLVPLoadPage21
:
3078 firstKind
= ld::Fixup::kindStoreTargetAddressARM64TLVPLoadPage21
;
3080 case ld::Fixup::kindStoreARM64TLVPLoadPageOff12
:
3081 firstKind
= ld::Fixup::kindStoreTargetAddressARM64TLVPLoadPageOff12
;
3086 cl
= ld::Fixup::k1of2
;
3091 if ( target
.atom
!= NULL
) {
3092 if ( target
.atom
->scope() == ld::Atom::scopeTranslationUnit
) {
3093 addFixup(src
, cl
, firstKind
, target
.atom
);
3095 else if ( (target
.atom
->combine() == ld::Atom::combineByNameAndContent
) || (target
.atom
->combine() == ld::Atom::combineByNameAndReferences
) ) {
3096 addFixup(src
, cl
, firstKind
, ld::Fixup::bindingByContentBound
, target
.atom
);
3098 else if ( (src
.atom
->section().type() == ld::Section::typeCFString
) && (src
.offsetInAtom
!= 0) ) {
3099 // backing string in CFStrings should always be direct
3100 addFixup(src
, cl
, firstKind
, target
.atom
);
3102 else if ( (src
.atom
== target
.atom
) && (target
.atom
->combine() == ld::Atom::combineByName
) ) {
3103 // reference to self should always be direct
3104 addFixup(src
, cl
, firstKind
, target
.atom
);
3107 // change direct fixup to by-name fixup
3108 addFixup(src
, cl
, firstKind
, false, target
.atom
->name());
3112 addFixup(src
, cl
, firstKind
, target
.weakImport
, target
.name
);
3114 if ( target
.addend
== 0 ) {
3116 addFixup(src
, ld::Fixup::k2of2
, setKind
);
3119 addFixup(src
, ld::Fixup::k2of3
, ld::Fixup::kindAddAddend
, target
.addend
);
3120 addFixup(src
, ld::Fixup::k3of3
, setKind
);
3124 template <typename A
>
3125 void Parser
<A
>::addFixups(const SourceLocation
& src
, ld::Fixup::Kind kind
, const TargetDesc
& target
, const TargetDesc
& picBase
)
3127 ld::Fixup::Cluster cl
= (target
.addend
== 0) ? ld::Fixup::k1of4
: ld::Fixup::k1of5
;
3128 if ( target
.atom
!= NULL
) {
3129 if ( target
.atom
->scope() == ld::Atom::scopeTranslationUnit
) {
3130 addFixup(src
, cl
, ld::Fixup::kindSetTargetAddress
, target
.atom
);
3132 else if ( (target
.atom
->combine() == ld::Atom::combineByNameAndContent
) || (target
.atom
->combine() == ld::Atom::combineByNameAndReferences
) ) {
3133 addFixup(src
, cl
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, target
.atom
);
3136 addFixup(src
, cl
, ld::Fixup::kindSetTargetAddress
, false, target
.atom
->name());
3140 addFixup(src
, cl
, ld::Fixup::kindSetTargetAddress
, target
.weakImport
, target
.name
);
3142 if ( target
.addend
== 0 ) {
3143 assert(picBase
.atom
!= NULL
);
3144 addFixup(src
, ld::Fixup::k2of4
, ld::Fixup::kindSubtractTargetAddress
, picBase
.atom
);
3145 addFixup(src
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractAddend
, picBase
.addend
);
3146 addFixup(src
, ld::Fixup::k4of4
, kind
);
3149 addFixup(src
, ld::Fixup::k2of5
, ld::Fixup::kindAddAddend
, target
.addend
);
3150 addFixup(src
, ld::Fixup::k3of5
, ld::Fixup::kindSubtractTargetAddress
, picBase
.atom
);
3151 addFixup(src
, ld::Fixup::k4of5
, ld::Fixup::kindSubtractAddend
, picBase
.addend
);
3152 addFixup(src
, ld::Fixup::k5of5
, kind
);
3158 template <typename A
>
3159 uint32_t TentativeDefinitionSection
<A
>::computeAtomCount(class Parser
<A
>& parser
,
3160 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
3161 const struct Parser
<A
>::CFI_CU_InfoArrays
&)
3163 return parser
.tentativeDefinitionCount();
3166 template <typename A
>
3167 uint32_t TentativeDefinitionSection
<A
>::appendAtoms(class Parser
<A
>& parser
, uint8_t* p
,
3168 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
3169 const struct Parser
<A
>::CFI_CU_InfoArrays
&)
3171 this->_beginAtoms
= (Atom
<A
>*)p
;
3173 for (uint32_t i
=parser
.undefinedStartIndex(); i
< parser
.undefinedEndIndex(); ++i
) {
3174 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(i
);
3175 if ( ((sym
.n_type() & N_TYPE
) == N_UNDF
) && (sym
.n_value() != 0) ) {
3176 uint64_t size
= sym
.n_value();
3177 uint8_t alignP2
= GET_COMM_ALIGN(sym
.n_desc());
3178 if ( alignP2
== 0 ) {
3179 // common symbols align to their size
3180 // that is, a 4-byte common aligns to 4-bytes
3181 // if this size is not a power of two,
3182 // then round up to the next power of two
3183 alignP2
= 63 - (uint8_t)__builtin_clzll(size
);
3184 if ( size
!= (1ULL << alignP2
) )
3186 // <rdar://problem/24871389> limit default alignment of large commons
3187 if ( alignP2
> parser
.maxDefaultCommonAlignment() )
3188 alignP2
= parser
.maxDefaultCommonAlignment();
3190 Atom
<A
>* allocatedSpace
= (Atom
<A
>*)p
;
3191 new (allocatedSpace
) Atom
<A
>(*this, parser
.nameFromSymbol(sym
), (pint_t
)ULLONG_MAX
, size
,
3192 ld::Atom::definitionTentative
, ld::Atom::combineByName
,
3193 parser
.scopeFromSymbol(sym
), ld::Atom::typeZeroFill
, ld::Atom::symbolTableIn
,
3194 parser
.dontDeadStripFromSymbol(sym
), false, false, ld::Atom::Alignment(alignP2
) );
3195 p
+= sizeof(Atom
<A
>);
3199 this->_endAtoms
= (Atom
<A
>*)p
;
3204 template <typename A
>
3205 uint32_t AbsoluteSymbolSection
<A
>::computeAtomCount(class Parser
<A
>& parser
,
3206 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
3207 const struct Parser
<A
>::CFI_CU_InfoArrays
&)
3209 return parser
.absoluteSymbolCount();
3212 template <typename A
>
3213 uint32_t AbsoluteSymbolSection
<A
>::appendAtoms(class Parser
<A
>& parser
, uint8_t* p
,
3214 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
3215 const struct Parser
<A
>::CFI_CU_InfoArrays
&)
3217 this->_beginAtoms
= (Atom
<A
>*)p
;
3219 for (uint32_t i
=0; i
< parser
.symbolCount(); ++i
) {
3220 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(i
);
3221 if ( (sym
.n_type() & N_TYPE
) != N_ABS
)
3223 const char* absName
= parser
.nameFromSymbol(sym
);
3224 // ignore .objc_class_name_* symbols
3225 if ( strncmp(absName
, ".objc_class_name_", 17) == 0 )
3227 // ignore .objc_class_name_* symbols
3228 if ( strncmp(absName
, ".objc_category_name_", 20) == 0 )
3230 // ignore empty *.eh symbols
3231 if ( strcmp(&absName
[strlen(absName
)-3], ".eh") == 0 )
3234 Atom
<A
>* allocatedSpace
= (Atom
<A
>*)p
;
3235 new (allocatedSpace
) Atom
<A
>(*this, parser
, sym
, 0);
3236 p
+= sizeof(Atom
<A
>);
3239 this->_endAtoms
= (Atom
<A
>*)p
;
3243 template <typename A
>
3244 Atom
<A
>* AbsoluteSymbolSection
<A
>::findAbsAtomForValue(typename
A::P::uint_t value
)
3246 Atom
<A
>* end
= this->_endAtoms
;
3247 for(Atom
<A
>* p
= this->_beginAtoms
; p
< end
; ++p
) {
3248 if ( p
->_objAddress
== value
)
3255 template <typename A
>
3256 uint32_t Parser
<A
>::indirectSymbol(uint32_t indirectIndex
)
3258 if ( indirectIndex
>= _indirectTableCount
)
3259 throw "indirect symbol index out of range";
3260 return E::get32(_indirectTable
[indirectIndex
]);
3263 template <typename A
>
3264 const macho_nlist
<typename
A::P
>& Parser
<A
>::symbolFromIndex(uint32_t index
)
3266 if ( index
> _symbolCount
)
3267 throw "symbol index out of range";
3268 return _symbols
[index
];
3271 template <typename A
>
3272 const macho_section
<typename
A::P
>* Parser
<A
>::machOSectionFromSectionIndex(uint32_t index
)
3274 if ( index
>= _machOSectionsCount
)
3275 throw "section index out of range";
3276 return &_sectionsStart
[index
];
3279 template <typename A
>
3280 uint32_t Parser
<A
>::symbolIndexFromIndirectSectionAddress(pint_t addr
, const macho_section
<P
>* sect
)
3282 uint32_t elementSize
= 0;
3283 switch ( sect
->flags() & SECTION_TYPE
) {
3284 case S_SYMBOL_STUBS
:
3285 elementSize
= sect
->reserved2();
3287 case S_LAZY_SYMBOL_POINTERS
:
3288 case S_NON_LAZY_SYMBOL_POINTERS
:
3289 case S_THREAD_LOCAL_VARIABLE_POINTERS
:
3290 elementSize
= sizeof(pint_t
);
3293 throw "section does not use indirect symbol table";
3295 uint32_t indexInSection
= (addr
- sect
->addr()) / elementSize
;
3296 uint32_t indexIntoIndirectTable
= sect
->reserved1() + indexInSection
;
3297 return this->indirectSymbol(indexIntoIndirectTable
);
3302 template <typename A
>
3303 const char* Parser
<A
>::nameFromSymbol(const macho_nlist
<P
>& sym
)
3305 return &_strings
[sym
.n_strx()];
3308 template <typename A
>
3309 ld::Atom::Scope Parser
<A
>::scopeFromSymbol(const macho_nlist
<P
>& sym
)
3311 if ( (sym
.n_type() & N_EXT
) == 0 )
3312 return ld::Atom::scopeTranslationUnit
;
3313 else if ( (sym
.n_type() & N_PEXT
) != 0 )
3314 return ld::Atom::scopeLinkageUnit
;
3315 else if ( this->nameFromSymbol(sym
)[0] == 'l' ) // since all 'l' symbols will be remove, don't make them global
3316 return ld::Atom::scopeLinkageUnit
;
3318 return ld::Atom::scopeGlobal
;
3321 template <typename A
>
3322 ld::Atom::Definition Parser
<A
>::definitionFromSymbol(const macho_nlist
<P
>& sym
)
3324 switch ( sym
.n_type() & N_TYPE
) {
3326 return ld::Atom::definitionAbsolute
;
3328 return ld::Atom::definitionRegular
;
3330 if ( sym
.n_value() != 0 )
3331 return ld::Atom::definitionTentative
;
3333 throw "definitionFromSymbol() bad symbol";
3336 template <typename A
>
3337 ld::Atom::Combine Parser
<A
>::combineFromSymbol(const macho_nlist
<P
>& sym
)
3339 if ( sym
.n_desc() & N_WEAK_DEF
)
3340 return ld::Atom::combineByName
;
3342 return ld::Atom::combineNever
;
3346 template <typename A
>
3347 ld::Atom::SymbolTableInclusion Parser
<A
>::inclusionFromSymbol(const macho_nlist
<P
>& sym
)
3349 const char* symbolName
= nameFromSymbol(sym
);
3350 // labels beginning with 'l' (lowercase ell) are automatically removed in final linked images <rdar://problem/4571042>
3351 // labels beginning with 'L' should have been stripped by the assembler, so are stripped now
3352 if ( sym
.n_desc() & REFERENCED_DYNAMICALLY
)
3353 return ld::Atom::symbolTableInAndNeverStrip
;
3354 else if ( symbolName
[0] == 'l' )
3355 return ld::Atom::symbolTableNotInFinalLinkedImages
;
3356 else if ( symbolName
[0] == 'L' )
3357 return ld::Atom::symbolTableNotIn
;
3359 return ld::Atom::symbolTableIn
;
3362 template <typename A
>
3363 bool Parser
<A
>::dontDeadStripFromSymbol(const macho_nlist
<P
>& sym
)
3365 return ( (sym
.n_desc() & (N_NO_DEAD_STRIP
|REFERENCED_DYNAMICALLY
)) != 0 );
3368 template <typename A
>
3369 bool Parser
<A
>::isThumbFromSymbol(const macho_nlist
<P
>& sym
)
3371 return ( sym
.n_desc() & N_ARM_THUMB_DEF
);
3374 template <typename A
>
3375 bool Parser
<A
>::weakImportFromSymbol(const macho_nlist
<P
>& sym
)
3377 return ( ((sym
.n_type() & N_TYPE
) == N_UNDF
) && ((sym
.n_desc() & N_WEAK_REF
) != 0) );
3380 template <typename A
>
3381 bool Parser
<A
>::resolverFromSymbol(const macho_nlist
<P
>& sym
)
3383 return ( sym
.n_desc() & N_SYMBOL_RESOLVER
);
3386 template <typename A
>
3387 bool Parser
<A
>::altEntryFromSymbol(const macho_nlist
<P
>& sym
)
3389 return ( sym
.n_desc() & N_ALT_ENTRY
);
3393 /* Skip over a LEB128 value (signed or unsigned). */
3395 skip_leb128 (const uint8_t ** offset
, const uint8_t * end
)
3397 while (*offset
!= end
&& **offset
>= 0x80)
3403 /* Read a ULEB128 into a 64-bit word. Return (uint64_t)-1 on overflow
3404 or error. On overflow, skip past the rest of the uleb128. */
3406 read_uleb128 (const uint8_t ** offset
, const uint8_t * end
)
3408 uint64_t result
= 0;
3415 return (uint64_t) -1;
3417 b
= **offset
& 0x7f;
3419 if (bit
>= 64 || b
<< bit
>> bit
!= b
)
3420 result
= (uint64_t) -1;
3422 result
|= b
<< bit
, bit
+= 7;
3423 } while (*(*offset
)++ >= 0x80);
3428 /* Skip over a DWARF attribute of form FORM. */
3429 template <typename A
>
3430 bool Parser
<A
>::skip_form(const uint8_t ** offset
, const uint8_t * end
, uint64_t form
,
3431 uint8_t addr_size
, bool dwarf64
)
3441 case DW_FORM_block2
:
3442 if (end
- *offset
< 2)
3444 sz
= 2 + A::P::E::get16(*(uint16_t*)offset
);
3447 case DW_FORM_block4
:
3448 if (end
- *offset
< 4)
3450 sz
= 2 + A::P::E::get32(*(uint32_t*)offset
);
3468 case DW_FORM_string
:
3469 while (*offset
!= end
&& **offset
)
3478 sz
= read_uleb128 (offset
, end
);
3481 case DW_FORM_block1
:
3489 case DW_FORM_ref_udata
:
3490 skip_leb128 (offset
, end
);
3494 case DW_FORM_ref_addr
:
3498 case DW_FORM_sec_offset
:
3499 sz
= sizeof(typename
A::P::uint_t
);
3502 case DW_FORM_exprloc
:
3503 sz
= read_uleb128 (offset
, end
);
3506 case DW_FORM_flag_present
:
3510 case DW_FORM_ref_sig8
:
3517 if (end
- *offset
< sz
)
3524 template <typename A
>
3525 const char* Parser
<A
>::getDwarfString(uint64_t form
, const uint8_t*& di
)
3528 const char* dwarfStrings
;
3529 const char* result
= NULL
;
3531 case DW_FORM_string
:
3532 result
= (const char*)di
;
3533 di
+= strlen(result
) + 1;
3536 offset
= E::get32(*((uint32_t*)di
));
3537 dwarfStrings
= (char*)_file
->fileContent() + _file
->_dwarfDebugStringSect
->offset();
3538 if ( offset
< _file
->_dwarfDebugStringSect
->size() )
3539 result
= &dwarfStrings
[offset
];
3541 warning("dwarf DW_FORM_strp (offset=0x%08X) is too big in %s", offset
, this->_path
);
3545 warning("unknown dwarf string encoding (form=%lld) in %s", form
, this->_path
);
3551 template <typename A
>
3552 uint64_t Parser
<A
>::getDwarfOffset(uint64_t form
, const uint8_t*& di
, bool dwarf64
)
3554 if ( form
== DW_FORM_sec_offset
)
3555 form
= (dwarf64
? DW_FORM_data8
: DW_FORM_data4
);
3556 uint64_t result
= -1;
3559 result
= A::P::E::get32(*(uint32_t*)di
);
3563 result
= A::P::E::get64(*(uint64_t*)di
);
3567 warning("unknown dwarf DW_FORM_ for DW_AT_stmt_list in %s", this->_path
);
3573 template <typename A
>
3574 struct AtomAndLineInfo
{
3576 ld::Atom::LineInfo info
;
3580 // <rdar://problem/5591394> Add support to ld64 for N_FUN stabs when used for symbolic constants
3581 // Returns whether a stabStr belonging to an N_FUN stab represents a
3582 // symbolic constant rather than a function
3583 template <typename A
>
3584 bool Parser
<A
>::isConstFunStabs(const char *stabStr
)
3587 // N_FUN can be used for both constants and for functions. In case it's a constant,
3588 // the format of the stabs string is "symname:c=<value>;"
3589 // ':' cannot appear in the symbol name, except if it's an Objective-C method
3590 // (in which case the symbol name starts with + or -, and then it's definitely
3592 return (stabStr
!= NULL
) && (stabStr
[0] != '+') && (stabStr
[0] != '-')
3593 && ((colon
= strchr(stabStr
, ':')) != NULL
)
3594 && (colon
[1] == 'c') && (colon
[2] == '=');
3598 template <typename A
>
3599 void Parser
<A
>::parseDebugInfo()
3601 // check for dwarf __debug_info section
3602 if ( _file
->_dwarfDebugInfoSect
== NULL
) {
3603 // if no DWARF debug info, look for stabs
3607 if ( _file
->_dwarfDebugInfoSect
->size() == 0 )
3613 if ( !read_comp_unit(&tuName
, &tuDir
, &stmtList
) ) {
3614 // if can't parse dwarf, warn and give up
3615 _file
->_dwarfTranslationUnitPath
= NULL
;
3616 warning("can't parse dwarf compilation unit info in %s", _path
);
3617 _file
->_debugInfoKind
= ld::relocatable::File::kDebugInfoNone
;
3620 if ( (tuName
!= NULL
) && (tuName
[0] == '/') ) {
3621 _file
->_dwarfTranslationUnitPath
= tuName
;
3623 else if ( (tuDir
!= NULL
) && (tuName
!= NULL
) ) {
3624 asprintf((char**)&(_file
->_dwarfTranslationUnitPath
), "%s/%s", tuDir
, tuName
);
3626 else if ( tuDir
== NULL
) {
3627 _file
->_dwarfTranslationUnitPath
= tuName
;
3630 _file
->_dwarfTranslationUnitPath
= NULL
;
3633 // add line number info to atoms from dwarf
3634 std::vector
<AtomAndLineInfo
<A
> > entries
;
3635 entries
.reserve(64);
3636 if ( _file
->_debugInfoKind
== ld::relocatable::File::kDebugInfoDwarf
) {
3637 // file with just data will have no __debug_line info
3638 if ( (_file
->_dwarfDebugLineSect
!= NULL
) && (_file
->_dwarfDebugLineSect
->size() != 0) ) {
3639 // validate stmt_list
3640 if ( (stmtList
!= (uint64_t)-1) && (stmtList
< _file
->_dwarfDebugLineSect
->size()) ) {
3641 const uint8_t* debug_line
= (uint8_t*)_file
->fileContent() + _file
->_dwarfDebugLineSect
->offset();
3642 struct line_reader_data
* lines
= line_open(&debug_line
[stmtList
],
3643 _file
->_dwarfDebugLineSect
->size() - stmtList
, E::little_endian
);
3644 struct line_info result
;
3645 Atom
<A
>* curAtom
= NULL
;
3646 uint32_t curAtomOffset
= 0;
3647 uint32_t curAtomAddress
= 0;
3648 uint32_t curAtomSize
= 0;
3649 std::map
<uint32_t,const char*> dwarfIndexToFile
;
3650 if ( lines
!= NULL
) {
3651 while ( line_next(lines
, &result
, line_stop_pc
) ) {
3652 //fprintf(stderr, "curAtom=%p, result.pc=0x%llX, result.line=%llu, result.end_of_sequence=%d,"
3653 // " curAtomAddress=0x%X, curAtomSize=0x%X\n",
3654 // curAtom, result.pc, result.line, result.end_of_sequence, curAtomAddress, curAtomSize);
3655 // work around weird debug line table compiler generates if no functions in __text section
3656 if ( (curAtom
== NULL
) && (result
.pc
== 0) && result
.end_of_sequence
&& (result
.file
== 1))
3658 // for performance, see if in next pc is in current atom
3659 if ( (curAtom
!= NULL
) && (curAtomAddress
<= result
.pc
) && (result
.pc
< (curAtomAddress
+curAtomSize
)) ) {
3660 curAtomOffset
= result
.pc
- curAtomAddress
;
3662 // or pc at end of current atom
3663 else if ( result
.end_of_sequence
&& (curAtom
!= NULL
) && (result
.pc
== (curAtomAddress
+curAtomSize
)) ) {
3664 curAtomOffset
= result
.pc
- curAtomAddress
;
3666 // or only one function that is a one line function
3667 else if ( result
.end_of_sequence
&& (curAtom
== NULL
) && (this->findAtomByAddress(0) != NULL
) && (result
.pc
== this->findAtomByAddress(0)->size()) ) {
3668 curAtom
= this->findAtomByAddress(0);
3669 curAtomOffset
= result
.pc
- curAtom
->objectAddress();
3670 curAtomAddress
= curAtom
->objectAddress();
3671 curAtomSize
= curAtom
->size();
3674 // do slow look up of atom by address
3676 curAtom
= this->findAtomByAddress(result
.pc
);
3679 // in case of bug in debug info, don't abort link, just limp on
3682 if ( curAtom
== NULL
)
3683 break; // file has line info but no functions
3684 if ( result
.end_of_sequence
&& (curAtomAddress
+curAtomSize
< result
.pc
) ) {
3685 // a one line function can be returned by line_next() as one entry with pc at end of blob
3686 // look for alt atom starting at end of previous atom
3687 uint32_t previousEnd
= curAtomAddress
+curAtomSize
;
3688 Atom
<A
>* alt
= this->findAtomByAddressOrNullIfStub(previousEnd
);
3690 continue; // ignore spurious debug info for stubs
3691 if ( result
.pc
<= alt
->objectAddress() + alt
->size() ) {
3693 curAtomOffset
= result
.pc
- alt
->objectAddress();
3694 curAtomAddress
= alt
->objectAddress();
3695 curAtomSize
= alt
->size();
3698 curAtomOffset
= result
.pc
- curAtom
->objectAddress();
3699 curAtomAddress
= curAtom
->objectAddress();
3700 curAtomSize
= curAtom
->size();
3704 curAtomOffset
= result
.pc
- curAtom
->objectAddress();
3705 curAtomAddress
= curAtom
->objectAddress();
3706 curAtomSize
= curAtom
->size();
3709 const char* filename
;
3710 std::map
<uint32_t,const char*>::iterator pos
= dwarfIndexToFile
.find(result
.file
);
3711 if ( pos
== dwarfIndexToFile
.end() ) {
3712 filename
= line_file(lines
, result
.file
);
3713 dwarfIndexToFile
[result
.file
] = filename
;
3716 filename
= pos
->second
;
3718 // only record for ~8000 line info records per function
3719 if ( curAtom
->roomForMoreLineInfoCount() ) {
3720 AtomAndLineInfo
<A
> entry
;
3721 entry
.atom
= curAtom
;
3722 entry
.info
.atomOffset
= curAtomOffset
;
3723 entry
.info
.fileName
= filename
;
3724 entry
.info
.lineNumber
= result
.line
;
3725 //fprintf(stderr, "addr=0x%08llX, line=%lld, file=%s, atom=%s, atom.size=0x%X, end=%d\n",
3726 // result.pc, result.line, filename, curAtom->name(), curAtomSize, result.end_of_sequence);
3727 entries
.push_back(entry
);
3728 curAtom
->incrementLineInfoCount();
3730 if ( result
.end_of_sequence
) {
3740 // assign line info start offset for each atom
3741 uint8_t* p
= _file
->_atomsArray
;
3742 uint32_t liOffset
= 0;
3743 for(int i
=_file
->_atomsArrayCount
; i
> 0; --i
) {
3744 Atom
<A
>* atom
= (Atom
<A
>*)p
;
3745 atom
->_lineInfoStartIndex
= liOffset
;
3746 liOffset
+= atom
->_lineInfoCount
;
3747 atom
->_lineInfoCount
= 0;
3748 p
+= sizeof(Atom
<A
>);
3750 assert(liOffset
== entries
.size());
3751 _file
->_lineInfos
.resize(liOffset
);
3753 // copy each line info for each atom
3754 for (typename
std::vector
<AtomAndLineInfo
<A
> >::iterator it
= entries
.begin(); it
!= entries
.end(); ++it
) {
3755 uint32_t slot
= it
->atom
->_lineInfoStartIndex
+ it
->atom
->_lineInfoCount
;
3756 _file
->_lineInfos
[slot
] = it
->info
;
3757 it
->atom
->_lineInfoCount
++;
3760 // done with temp vector
3764 template <typename A
>
3765 void Parser
<A
>::parseStabs()
3767 // scan symbol table for stabs entries
3768 Atom
<A
>* currentAtom
= NULL
;
3769 pint_t currentAtomAddress
= 0;
3770 enum { start
, inBeginEnd
, inFun
} state
= start
;
3771 for (uint32_t symbolIndex
= 0; symbolIndex
< _symbolCount
; ++symbolIndex
) {
3772 const macho_nlist
<P
>& sym
= this->symbolFromIndex(symbolIndex
);
3773 bool useStab
= true;
3774 uint8_t type
= sym
.n_type();
3775 const char* symString
= (sym
.n_strx() != 0) ? this->nameFromSymbol(sym
) : NULL
;
3776 if ( (type
& N_STAB
) != 0 ) {
3777 _file
->_debugInfoKind
= (_hasUUID
? ld::relocatable::File::kDebugInfoStabsUUID
: ld::relocatable::File::kDebugInfoStabs
);
3778 ld::relocatable::File::Stab stab
;
3781 stab
.other
= sym
.n_sect();
3782 stab
.desc
= sym
.n_desc();
3783 stab
.value
= sym
.n_value();
3789 // beginning of function block
3791 // fall into case to lookup atom by addresss
3794 currentAtomAddress
= sym
.n_value();
3795 currentAtom
= this->findAtomByAddress(currentAtomAddress
);
3796 if ( currentAtom
!= NULL
) {
3797 stab
.atom
= currentAtom
;
3798 stab
.string
= symString
;
3801 fprintf(stderr
, "can't find atom for stabs BNSYM at %08llX in %s",
3802 (uint64_t)sym
.n_value(), _path
);
3812 // not associated with an atom, just copy
3813 stab
.string
= symString
;
3817 // n_value field is NOT atom address ;-(
3818 // need to find atom by name match
3819 const char* colon
= strchr(symString
, ':');
3820 if ( colon
!= NULL
) {
3821 // build underscore leading name
3822 int nameLen
= colon
- symString
;
3823 char symName
[nameLen
+2];
3824 strlcpy(&symName
[1], symString
, nameLen
+1);
3826 symName
[nameLen
+1] = '\0';
3827 currentAtom
= this->findAtomByName(symName
);
3828 if ( currentAtom
!= NULL
) {
3829 stab
.atom
= currentAtom
;
3830 stab
.string
= symString
;
3834 // might be a debug-note without trailing :G()
3835 currentAtom
= this->findAtomByName(symString
);
3836 if ( currentAtom
!= NULL
) {
3837 stab
.atom
= currentAtom
;
3838 stab
.string
= symString
;
3841 if ( stab
.atom
== NULL
) {
3842 // ld_classic added bogus GSYM stabs for old style dtrace probes
3843 if ( (strncmp(symString
, "__dtrace_probe$", 15) != 0) )
3844 warning("can't find atom for N_GSYM stabs %s in %s", symString
, _path
);
3850 if ( isConstFunStabs(symString
) ) {
3851 // constant not associated with a function
3852 stab
.string
= symString
;
3855 // old style stabs without BNSYM
3857 currentAtomAddress
= sym
.n_value();
3858 currentAtom
= this->findAtomByAddress(currentAtomAddress
);
3859 if ( currentAtom
!= NULL
) {
3860 stab
.atom
= currentAtom
;
3861 stab
.string
= symString
;
3864 warning("can't find atom for stabs FUN at %08llX in %s",
3865 (uint64_t)currentAtomAddress
, _path
);
3871 stab
.string
= symString
;
3877 stab
.string
= symString
;
3878 // -gfull built .o file
3881 warning("unknown stabs type 0x%X in %s", type
, _path
);
3885 stab
.atom
= currentAtom
;
3894 Atom
<A
>* nestedAtom
= this->findAtomByAddress(sym
.n_value());
3895 if ( nestedAtom
!= NULL
) {
3896 stab
.atom
= nestedAtom
;
3897 stab
.string
= symString
;
3900 warning("can't find atom for stabs 0x%X at %08llX in %s",
3901 type
, (uint64_t)sym
.n_value(), _path
);
3908 // adjust value to be offset in atom
3909 stab
.value
-= currentAtomAddress
;
3911 stab
.string
= symString
;
3918 if ( isConstFunStabs(symString
) ) {
3919 stab
.atom
= currentAtom
;
3920 stab
.string
= symString
;
3923 if ( sym
.n_sect() != 0 ) {
3924 // found another start stab, must be really old stabs...
3925 currentAtomAddress
= sym
.n_value();
3926 currentAtom
= this->findAtomByAddress(currentAtomAddress
);
3927 if ( currentAtom
!= NULL
) {
3928 stab
.atom
= currentAtom
;
3929 stab
.string
= symString
;
3932 warning("can't find atom for stabs FUN at %08llX in %s",
3933 (uint64_t)currentAtomAddress
, _path
);
3937 // found ending stab, switch back to start state
3938 stab
.string
= symString
;
3939 stab
.atom
= currentAtom
;
3948 // adjust value to be offset in atom
3949 stab
.value
-= currentAtomAddress
;
3950 stab
.atom
= currentAtom
;
3953 stab
.string
= symString
;
3957 stab
.atom
= currentAtom
;
3958 stab
.string
= symString
;
3963 // add to list of stabs for this .o file
3965 _file
->_stabs
.push_back(stab
);
3972 // Look at the compilation unit DIE and determine
3973 // its NAME, compilation directory (in COMP_DIR) and its
3974 // line number information offset (in STMT_LIST). NAME and COMP_DIR
3975 // may be NULL (especially COMP_DIR) if they are not in the .o file;
3976 // STMT_LIST will be (uint64_t) -1.
3978 // At present this assumes that there's only one compilation unit DIE.
3980 template <typename A
>
3981 bool Parser
<A
>::read_comp_unit(const char ** name
, const char ** comp_dir
,
3982 uint64_t *stmt_list
)
3984 const uint8_t * debug_info
;
3985 const uint8_t * debug_abbrev
;
3987 const uint8_t * next_cu
;
3989 const uint8_t * end
;
3990 const uint8_t * enda
;
3993 uint64_t abbrev_base
;
3995 uint8_t address_size
;
4000 *stmt_list
= (uint64_t) -1;
4002 if ( (_file
->_dwarfDebugInfoSect
== NULL
) || (_file
->_dwarfDebugAbbrevSect
== NULL
) )
4005 if (_file
->_dwarfDebugInfoSect
->size() < 12)
4006 /* Too small to be a real debug_info section. */
4009 debug_info
= (uint8_t*)_file
->fileContent() + _file
->_dwarfDebugInfoSect
->offset();
4010 debug_abbrev
= (uint8_t*)_file
->fileContent() + _file
->_dwarfDebugAbbrevSect
->offset();
4011 next_cu
= debug_info
;
4013 while ((uint64_t)(next_cu
- debug_info
) < _file
->_dwarfDebugInfoSect
->size()) {
4015 sz
= A::P::E::get32(*(uint32_t*)di
);
4017 dwarf64
= sz
== 0xffffffff;
4019 sz
= A::P::E::get64(*(uint64_t*)di
), di
+= 8;
4020 else if (sz
> 0xffffff00)
4021 /* Unknown dwarf format. */
4024 /* Verify claimed size. */
4025 if (sz
+ (di
- debug_info
) > _file
->_dwarfDebugInfoSect
->size() || sz
<= (dwarf64
? 23 : 11))
4030 vers
= A::P::E::get16(*(uint16_t*)di
);
4031 if (vers
< 2 || vers
> 4)
4032 /* DWARF version wrong for this code.
4033 Chances are we could continue anyway, but we don't know for sure. */
4037 /* Find the debug_abbrev section. */
4038 abbrev_base
= dwarf64
? A::P::E::get64(*(uint64_t*)di
) : A::P::E::get32(*(uint32_t*)di
);
4039 di
+= dwarf64
? 8 : 4;
4041 if (abbrev_base
> _file
->_dwarfDebugAbbrevSect
->size())
4043 da
= debug_abbrev
+ abbrev_base
;
4044 enda
= debug_abbrev
+ _file
->_dwarfDebugAbbrevSect
->size();
4046 address_size
= *di
++;
4048 /* Find the abbrev number we're looking for. */
4050 abbrev
= read_uleb128 (&di
, end
);
4051 if (abbrev
== (uint64_t) -1)
4054 /* Skip through the debug_abbrev section looking for that abbrev. */
4057 uint64_t this_abbrev
= read_uleb128 (&da
, enda
);
4060 if (this_abbrev
== abbrev
)
4061 /* This is almost always taken. */
4063 skip_leb128 (&da
, enda
); /* Skip the tag. */
4066 da
++; /* Skip the DW_CHILDREN_* value. */
4069 attr
= read_uleb128 (&da
, enda
);
4070 skip_leb128 (&da
, enda
);
4071 } while (attr
!= 0 && attr
!= (uint64_t) -1);
4076 /* Check that the abbrev is one for a DW_TAG_compile_unit. */
4077 if (read_uleb128 (&da
, enda
) != DW_TAG_compile_unit
)
4081 da
++; /* Skip the DW_CHILDREN_* value. */
4083 /* Now, go through the DIE looking for DW_AT_name,
4084 DW_AT_comp_dir, and DW_AT_stmt_list. */
4085 bool skip_to_next_cu
= false;
4086 while (!skip_to_next_cu
) {
4088 uint64_t attr
= read_uleb128 (&da
, enda
);
4089 uint64_t form
= read_uleb128 (&da
, enda
);
4091 if (attr
== (uint64_t) -1)
4095 if (form
== DW_FORM_indirect
)
4096 form
= read_uleb128 (&di
, end
);
4100 *name
= getDwarfString(form
, di
);
4101 /* Swift object files may contain two CUs: One
4102 describes the Swift code, one is created by the
4103 clang importer. Skip over the CU created by the
4104 clang importer as it may be empty. */
4105 if (std::string(*name
) == "<swift-imported-modules>")
4106 skip_to_next_cu
= true;
4108 case DW_AT_comp_dir
:
4109 *comp_dir
= getDwarfString(form
, di
);
4111 case DW_AT_stmt_list
:
4112 *stmt_list
= getDwarfOffset(form
, di
, dwarf64
);
4115 if (! skip_form (&di
, end
, form
, address_size
, dwarf64
))
4125 template <typename A
>
4128 free(_sectionsArray
);
4132 template <typename A
>
4133 const char* File
<A
>::translationUnitSource() const
4135 return _dwarfTranslationUnitPath
;
4138 template <typename A
>
4139 bool File
<A
>::forEachAtom(ld::File::AtomHandler
& handler
) const
4141 handler
.doFile(*this);
4142 uint8_t* p
= _atomsArray
;
4143 for(int i
=_atomsArrayCount
; i
> 0; --i
) {
4144 handler
.doAtom(*((Atom
<A
>*)p
));
4145 p
+= sizeof(Atom
<A
>);
4147 p
= _aliasAtomsArray
;
4148 for(int i
=_aliasAtomsArrayCount
; i
> 0; --i
) {
4149 handler
.doAtom(*((AliasAtom
*)p
));
4150 p
+= sizeof(AliasAtom
);
4153 return (_atomsArrayCount
!= 0) || (_aliasAtomsArrayCount
!= 0);
4156 template <typename A
>
4157 const char* Section
<A
>::makeSegmentName(const macho_section
<typename
A::P
>* sect
)
4159 // mach-o section record only has room for 16-byte seg/sect names
4160 // so a 16-byte name has no trailing zero
4161 const char* name
= sect
->segname();
4162 if ( strlen(name
) < 16 )
4164 char* tmp
= new char[17];
4165 strlcpy(tmp
, name
, 17);
4169 template <typename A
>
4170 const char* Section
<A
>::makeSectionName(const macho_section
<typename
A::P
>* sect
)
4172 const char* name
= sect
->sectname();
4173 if ( strlen(name
) < 16 )
4176 // special case common long section names so we don't have to malloc
4177 if ( strncmp(sect
->sectname(), "__objc_classrefs", 16) == 0 )
4178 return "__objc_classrefs";
4179 if ( strncmp(sect
->sectname(), "__objc_classlist", 16) == 0 )
4180 return "__objc_classlist";
4181 if ( strncmp(sect
->sectname(), "__objc_nlclslist", 16) == 0 )
4182 return "__objc_nlclslist";
4183 if ( strncmp(sect
->sectname(), "__objc_nlcatlist", 16) == 0 )
4184 return "__objc_nlcatlist";
4185 if ( strncmp(sect
->sectname(), "__objc_protolist", 16) == 0 )
4186 return "__objc_protolist";
4187 if ( strncmp(sect
->sectname(), "__objc_protorefs", 16) == 0 )
4188 return "__objc_protorefs";
4189 if ( strncmp(sect
->sectname(), "__objc_superrefs", 16) == 0 )
4190 return "__objc_superrefs";
4191 if ( strncmp(sect
->sectname(), "__objc_imageinfo", 16) == 0 )
4192 return "__objc_imageinfo";
4193 if ( strncmp(sect
->sectname(), "__objc_stringobj", 16) == 0 )
4194 return "__objc_stringobj";
4195 if ( strncmp(sect
->sectname(), "__gcc_except_tab", 16) == 0 )
4196 return "__gcc_except_tab";
4198 char* tmp
= new char[17];
4199 strlcpy(tmp
, name
, 17);
4203 template <typename A
>
4204 bool Section
<A
>::readable(const macho_section
<typename
A::P
>* sect
)
4209 template <typename A
>
4210 bool Section
<A
>::writable(const macho_section
<typename
A::P
>* sect
)
4212 // mach-o .o files do not contain segment permissions
4213 // we just know TEXT is special
4214 return ( strcmp(sect
->segname(), "__TEXT") != 0 );
4217 template <typename A
>
4218 bool Section
<A
>::exectuable(const macho_section
<typename
A::P
>* sect
)
4220 // mach-o .o files do not contain segment permissions
4221 // we just know TEXT is special
4222 return ( strcmp(sect
->segname(), "__TEXT") == 0 );
4226 template <typename A
>
4227 ld::Section::Type Section
<A
>::sectionType(const macho_section
<typename
A::P
>* sect
)
4229 switch ( sect
->flags() & SECTION_TYPE
) {
4231 return ld::Section::typeZeroFill
;
4232 case S_CSTRING_LITERALS
:
4233 if ( (strcmp(sect
->sectname(), "__cstring") == 0) && (strcmp(sect
->segname(), "__TEXT") == 0) )
4234 return ld::Section::typeCString
;
4236 return ld::Section::typeNonStdCString
;
4237 case S_4BYTE_LITERALS
:
4238 return ld::Section::typeLiteral4
;
4239 case S_8BYTE_LITERALS
:
4240 return ld::Section::typeLiteral8
;
4241 case S_LITERAL_POINTERS
:
4242 return ld::Section::typeCStringPointer
;
4243 case S_NON_LAZY_SYMBOL_POINTERS
:
4244 return ld::Section::typeNonLazyPointer
;
4245 case S_LAZY_SYMBOL_POINTERS
:
4246 return ld::Section::typeLazyPointer
;
4247 case S_SYMBOL_STUBS
:
4248 return ld::Section::typeStub
;
4249 case S_MOD_INIT_FUNC_POINTERS
:
4250 return ld::Section::typeInitializerPointers
;
4251 case S_MOD_TERM_FUNC_POINTERS
:
4252 return ld::Section::typeTerminatorPointers
;
4254 return ld::Section::typeUnclassified
;
4255 case S_16BYTE_LITERALS
:
4256 return ld::Section::typeLiteral16
;
4259 if ( sect
->flags() & S_ATTR_PURE_INSTRUCTIONS
) {
4260 return ld::Section::typeCode
;
4262 else if ( strcmp(sect
->segname(), "__TEXT") == 0 ) {
4263 if ( strcmp(sect
->sectname(), "__eh_frame") == 0 )
4264 return ld::Section::typeCFI
;
4265 else if ( strcmp(sect
->sectname(), "__ustring") == 0 )
4266 return ld::Section::typeUTF16Strings
;
4267 else if ( strcmp(sect
->sectname(), "__textcoal_nt") == 0 )
4268 return ld::Section::typeCode
;
4269 else if ( strcmp(sect
->sectname(), "__StaticInit") == 0 )
4270 return ld::Section::typeCode
;
4271 else if ( strcmp(sect
->sectname(), "__constructor") == 0 )
4272 return ld::Section::typeInitializerPointers
;
4274 else if ( strcmp(sect
->segname(), "__DATA") == 0 ) {
4275 if ( strcmp(sect
->sectname(), "__cfstring") == 0 )
4276 return ld::Section::typeCFString
;
4277 else if ( strcmp(sect
->sectname(), "__dyld") == 0 )
4278 return ld::Section::typeDyldInfo
;
4279 else if ( strcmp(sect
->sectname(), "__program_vars") == 0 )
4280 return ld::Section::typeDyldInfo
;
4281 else if ( strncmp(sect
->sectname(), "__objc_classrefs", 16) == 0 )
4282 return ld::Section::typeObjCClassRefs
;
4283 else if ( strcmp(sect
->sectname(), "__objc_catlist") == 0 )
4284 return ld::Section::typeObjC2CategoryList
;
4286 else if ( strcmp(sect
->segname(), "__OBJC") == 0 ) {
4287 if ( strcmp(sect
->sectname(), "__class") == 0 )
4288 return ld::Section::typeObjC1Classes
;
4291 case S_THREAD_LOCAL_REGULAR
:
4292 return ld::Section::typeTLVInitialValues
;
4293 case S_THREAD_LOCAL_ZEROFILL
:
4294 return ld::Section::typeTLVZeroFill
;
4295 case S_THREAD_LOCAL_VARIABLES
:
4296 return ld::Section::typeTLVDefs
;
4297 case S_THREAD_LOCAL_VARIABLE_POINTERS
:
4298 return ld::Section::typeTLVPointers
;
4299 case S_THREAD_LOCAL_INIT_FUNCTION_POINTERS
:
4300 return ld::Section::typeTLVInitializerPointers
;
4302 return ld::Section::typeUnclassified
;
4306 template <typename A
>
4307 Atom
<A
>* Section
<A
>::findContentAtomByAddress(pint_t addr
, class Atom
<A
>* start
, class Atom
<A
>* end
)
4309 // do a binary search of atom array
4310 uint32_t atomCount
= end
- start
;
4311 Atom
<A
>* base
= start
;
4312 for (uint32_t n
= atomCount
; n
> 0; n
/= 2) {
4313 Atom
<A
>* pivot
= &base
[n
/2];
4314 pint_t atomStartAddr
= pivot
->_objAddress
;
4315 pint_t atomEndAddr
= atomStartAddr
+ pivot
->_size
;
4316 if ( atomStartAddr
<= addr
) {
4317 // address in normal atom
4318 if (addr
< atomEndAddr
)
4320 // address in "end" label (but not in alias)
4321 if ( (pivot
->_size
== 0) && (addr
== atomEndAddr
) && !pivot
->isAlias() )
4324 if ( addr
>= atomEndAddr
) {
4326 // move base to atom after pivot
4338 template <typename A
>
4339 ld::Atom::Alignment Section
<A
>::alignmentForAddress(pint_t addr
)
4341 const uint32_t sectionAlignment
= this->_machOSection
->align();
4342 uint32_t modulus
= (addr
% (1 << sectionAlignment
));
4343 if ( modulus
> 0xFFFF )
4344 warning("alignment for symbol at address 0x%08llX in %s exceeds 2^16", (uint64_t)addr
, this->file().path());
4345 return ld::Atom::Alignment(sectionAlignment
, modulus
);
4348 template <typename A
>
4349 uint32_t Section
<A
>::sectionNum(class Parser
<A
>& parser
) const
4351 if ( _machOSection
== NULL
)
4354 return 1 + (this->_machOSection
- parser
.firstMachOSection());
4357 // arm does not have zero cost exceptions
4359 uint32_t CFISection
<arm
>::cfiCount(Parser
<arm
>& parser
)
4361 if ( parser
.armUsesZeroCostExceptions() ) {
4362 // create ObjectAddressSpace object for use by libunwind
4363 OAS
oas(*this, (uint8_t*)this->file().fileContent()+this->_machOSection
->offset());
4364 return libunwind::CFI_Parser
<OAS
>::getCFICount(oas
,
4365 this->_machOSection
->addr(), this->_machOSection
->size());
4370 template <typename A
>
4371 uint32_t CFISection
<A
>::cfiCount(Parser
<A
>& parser
)
4373 // create ObjectAddressSpace object for use by libunwind
4374 OAS
oas(*this, (uint8_t*)this->file().fileContent()+this->_machOSection
->offset());
4375 return libunwind::CFI_Parser
<OAS
>::getCFICount(oas
,
4376 this->_machOSection
->addr(), this->_machOSection
->size());
4379 template <typename A
>
4380 void CFISection
<A
>::warnFunc(void* ref
, uint64_t funcAddr
, const char* msg
)
4382 Parser
<A
>* parser
= (Parser
<A
>*)ref
;
4383 if ( ! parser
->warnUnwindConversionProblems() )
4385 if ( funcAddr
!= CFI_INVALID_ADDRESS
) {
4386 // atoms are not constructed yet, so scan symbol table for labels
4387 const char* name
= parser
->scanSymbolTableForAddress(funcAddr
);
4388 warning("could not create compact unwind for %s: %s", name
, msg
);
4391 warning("could not create compact unwind: %s", msg
);
4396 bool CFISection
<x86_64
>::needsRelocating()
4402 bool CFISection
<arm64
>::needsRelocating()
4408 template <typename A
>
4409 bool CFISection
<A
>::needsRelocating()
4415 void CFISection
<x86_64
>::cfiParse(class Parser
<x86_64
>& parser
, uint8_t* buffer
,
4416 libunwind::CFI_Atom_Info
<CFISection
<x86_64
>::OAS
>::CFI_Atom_Info cfiArray
[],
4417 uint32_t& count
, const pint_t cuStarts
[], uint32_t cuCount
)
4419 const uint32_t sectionSize
= this->_machOSection
->size();
4420 // copy __eh_frame data to buffer
4421 memcpy(buffer
, file().fileContent() + this->_machOSection
->offset(), sectionSize
);
4423 // and apply relocations
4424 const macho_relocation_info
<P
>* relocs
= (macho_relocation_info
<P
>*)(file().fileContent() + this->_machOSection
->reloff());
4425 const macho_relocation_info
<P
>* relocsEnd
= &relocs
[this->_machOSection
->nreloc()];
4426 for (const macho_relocation_info
<P
>* reloc
= relocs
; reloc
< relocsEnd
; ++reloc
) {
4428 switch ( reloc
->r_type() ) {
4429 case X86_64_RELOC_SUBTRACTOR
:
4430 value
= 0 - parser
.symbolFromIndex(reloc
->r_symbolnum()).n_value();
4432 if ( reloc
->r_extern() )
4433 value
+= parser
.symbolFromIndex(reloc
->r_symbolnum()).n_value();
4435 case X86_64_RELOC_UNSIGNED
:
4436 value
= parser
.symbolFromIndex(reloc
->r_symbolnum()).n_value();
4438 case X86_64_RELOC_GOT
:
4439 // this is used for the reference to the personality function in CIEs
4440 // store the symbol number of the personality function for later use as a Fixup
4441 value
= reloc
->r_symbolnum();
4444 fprintf(stderr
, "CFISection::cfiParse() unexpected relocation type at r_address=0x%08X\n", reloc
->r_address());
4447 if ( reloc
->r_address() > sectionSize
)
4448 throwf("malformed __eh_frame relocation, offset (0x%08X) is beyond end of section,", reloc
->r_address());
4451 switch ( reloc
->r_length() ) {
4453 p64
= (uint64_t*)&buffer
[reloc
->r_address()];
4454 E::set64(*p64
, value
+ E::get64(*p64
));
4457 p32
= (uint32_t*)&buffer
[reloc
->r_address()];
4458 E::set32(*p32
, value
+ E::get32(*p32
));
4461 fprintf(stderr
, "CFISection::cfiParse() unexpected relocation size at r_address=0x%08X\n", reloc
->r_address());
4466 // create ObjectAddressSpace object for use by libunwind
4467 OAS
oas(*this, buffer
);
4469 // use libuwind to parse __eh_frame data into array of CFI_Atom_Info
4471 msg
= libunwind::DwarfInstructions
<OAS
, libunwind::Registers_x86_64
>::parseCFIs(
4472 oas
, this->_machOSection
->addr(), this->_machOSection
->size(),
4473 cuStarts
, cuCount
, parser
.keepDwarfUnwind(), parser
.forceDwarfConversion(), parser
.neverConvertDwarf(),
4474 cfiArray
, count
, (void*)&parser
, warnFunc
);
4476 throwf("malformed __eh_frame section: %s", msg
);
4480 void CFISection
<x86
>::cfiParse(class Parser
<x86
>& parser
, uint8_t* buffer
,
4481 libunwind::CFI_Atom_Info
<CFISection
<x86
>::OAS
>::CFI_Atom_Info cfiArray
[],
4482 uint32_t& count
, const pint_t cuStarts
[], uint32_t cuCount
)
4484 // create ObjectAddressSpace object for use by libunwind
4485 OAS
oas(*this, (uint8_t*)this->file().fileContent()+this->_machOSection
->offset());
4487 // use libuwind to parse __eh_frame data into array of CFI_Atom_Info
4489 msg
= libunwind::DwarfInstructions
<OAS
, libunwind::Registers_x86
>::parseCFIs(
4490 oas
, this->_machOSection
->addr(), this->_machOSection
->size(),
4491 cuStarts
, cuCount
, parser
.keepDwarfUnwind(), parser
.forceDwarfConversion(), parser
.neverConvertDwarf(),
4492 cfiArray
, count
, (void*)&parser
, warnFunc
);
4494 throwf("malformed __eh_frame section: %s", msg
);
4501 void CFISection
<arm
>::cfiParse(class Parser
<arm
>& parser
, uint8_t* buffer
,
4502 libunwind::CFI_Atom_Info
<CFISection
<arm
>::OAS
>::CFI_Atom_Info cfiArray
[],
4503 uint32_t& count
, const pint_t cuStarts
[], uint32_t cuCount
)
4505 if ( !parser
.armUsesZeroCostExceptions() ) {
4506 // most arm do not use zero cost exceptions
4510 // create ObjectAddressSpace object for use by libunwind
4511 OAS
oas(*this, (uint8_t*)this->file().fileContent()+this->_machOSection
->offset());
4513 // use libuwind to parse __eh_frame data into array of CFI_Atom_Info
4515 msg
= libunwind::DwarfInstructions
<OAS
, libunwind::Registers_arm
>::parseCFIs(
4516 oas
, this->_machOSection
->addr(), this->_machOSection
->size(),
4517 cuStarts
, cuCount
, parser
.keepDwarfUnwind(), parser
.forceDwarfConversion(), parser
.neverConvertDwarf(),
4518 cfiArray
, count
, (void*)&parser
, warnFunc
);
4520 throwf("malformed __eh_frame section: %s", msg
);
4527 void CFISection
<arm64
>::cfiParse(class Parser
<arm64
>& parser
, uint8_t* buffer
,
4528 libunwind::CFI_Atom_Info
<CFISection
<arm64
>::OAS
>::CFI_Atom_Info cfiArray
[],
4529 uint32_t& count
, const pint_t cuStarts
[], uint32_t cuCount
)
4531 // copy __eh_frame data to buffer
4532 const uint32_t sectionSize
= this->_machOSection
->size();
4533 memcpy(buffer
, file().fileContent() + this->_machOSection
->offset(), sectionSize
);
4535 // and apply relocations
4536 const macho_relocation_info
<P
>* relocs
= (macho_relocation_info
<P
>*)(file().fileContent() + this->_machOSection
->reloff());
4537 const macho_relocation_info
<P
>* relocsEnd
= &relocs
[this->_machOSection
->nreloc()];
4538 for (const macho_relocation_info
<P
>* reloc
= relocs
; reloc
< relocsEnd
; ++reloc
) {
4539 uint64_t* p64
= (uint64_t*)&buffer
[reloc
->r_address()];
4540 uint32_t* p32
= (uint32_t*)&buffer
[reloc
->r_address()];
4541 uint32_t addend32
= E::get32(*p32
);
4542 uint64_t addend64
= E::get64(*p64
);
4544 switch ( reloc
->r_type() ) {
4545 case ARM64_RELOC_SUBTRACTOR
:
4546 value
= 0 - parser
.symbolFromIndex(reloc
->r_symbolnum()).n_value();
4548 if ( reloc
->r_extern() )
4549 value
+= parser
.symbolFromIndex(reloc
->r_symbolnum()).n_value();
4551 case ARM64_RELOC_UNSIGNED
:
4552 value
= parser
.symbolFromIndex(reloc
->r_symbolnum()).n_value();
4554 case ARM64_RELOC_POINTER_TO_GOT
:
4555 // this is used for the reference to the personality function in CIEs
4556 // store the symbol number of the personality function for later use as a Fixup
4557 value
= reloc
->r_symbolnum();
4562 fprintf(stderr
, "CFISection::cfiParse() unexpected relocation type at r_address=0x%08X\n", reloc
->r_address());
4565 if ( reloc
->r_address() > sectionSize
)
4566 throwf("malformed __eh_frame relocation, offset (0x%08X) is beyond end of section,", reloc
->r_address());
4567 switch ( reloc
->r_length() ) {
4569 E::set64(*p64
, value
+ addend64
);
4572 E::set32(*p32
, value
+ addend32
);
4575 fprintf(stderr
, "CFISection::cfiParse() unexpected relocation size at r_address=0x%08X\n", reloc
->r_address());
4581 // create ObjectAddressSpace object for use by libunwind
4582 OAS
oas(*this, buffer
);
4584 // use libuwind to parse __eh_frame data into array of CFI_Atom_Info
4586 msg
= libunwind::DwarfInstructions
<OAS
, libunwind::Registers_arm64
>::parseCFIs(
4587 oas
, this->_machOSection
->addr(), this->_machOSection
->size(),
4588 cuStarts
, cuCount
, parser
.keepDwarfUnwind(), parser
.forceDwarfConversion(), parser
.neverConvertDwarf(),
4589 cfiArray
, count
, (void*)&parser
, warnFunc
);
4591 throwf("malformed __eh_frame section: %s", msg
);
4595 template <typename A
>
4596 uint32_t CFISection
<A
>::computeAtomCount(class Parser
<A
>& parser
,
4597 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
4598 const struct Parser
<A
>::CFI_CU_InfoArrays
& cfis
)
4600 return cfis
.cfiCount
;
4605 template <typename A
>
4606 uint32_t CFISection
<A
>::appendAtoms(class Parser
<A
>& parser
, uint8_t* p
,
4607 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
4608 const struct Parser
<A
>::CFI_CU_InfoArrays
& cfis
)
4610 this->_beginAtoms
= (Atom
<A
>*)p
;
4611 // walk CFI_Atom_Info array and create atom for each entry
4612 const CFI_Atom_Info
* start
= &cfis
.cfiArray
[0];
4613 const CFI_Atom_Info
* end
= &cfis
.cfiArray
[cfis
.cfiCount
];
4614 for(const CFI_Atom_Info
* a
=start
; a
< end
; ++a
) {
4615 Atom
<A
>* space
= (Atom
<A
>*)p
;
4616 new (space
) Atom
<A
>(*this, (a
->isCIE
? "CIE" : "FDE"), a
->address
, a
->size
,
4617 ld::Atom::definitionRegular
, ld::Atom::combineNever
, ld::Atom::scopeTranslationUnit
,
4618 ld::Atom::typeCFI
, ld::Atom::symbolTableNotInFinalLinkedImages
,
4619 false, false, false, ld::Atom::Alignment(0));
4620 p
+= sizeof(Atom
<A
>);
4622 this->_endAtoms
= (Atom
<A
>*)p
;
4623 return cfis
.cfiCount
;
4627 template <> bool CFISection
<x86_64
>::bigEndian() { return false; }
4628 template <> bool CFISection
<x86
>::bigEndian() { return false; }
4629 template <> bool CFISection
<arm
>::bigEndian() { return false; }
4630 template <> bool CFISection
<arm64
>::bigEndian() { return false; }
4633 void CFISection
<x86_64
>::addCiePersonalityFixups(class Parser
<x86_64
>& parser
, const CFI_Atom_Info
* cieInfo
)
4635 uint8_t personalityEncoding
= cieInfo
->u
.cieInfo
.personality
.encodingOfTargetAddress
;
4636 if ( personalityEncoding
== 0x9B ) {
4637 // compiler always produces X86_64_RELOC_GOT with addend of 4 to personality function
4638 // CFISection<x86_64>::cfiParse() set targetAddress to be symbolIndex + 4 + addressInCIE
4639 uint32_t symbolIndex
= cieInfo
->u
.cieInfo
.personality
.targetAddress
- 4
4640 - cieInfo
->address
- cieInfo
->u
.cieInfo
.personality
.offsetInCFI
;
4641 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(symbolIndex
);
4642 const char* personalityName
= parser
.nameFromSymbol(sym
);
4644 Atom
<x86_64
>* cieAtom
= this->findAtomByAddress(cieInfo
->address
);
4645 Parser
<x86_64
>::SourceLocation
src(cieAtom
, cieInfo
->u
.cieInfo
.personality
.offsetInCFI
);
4646 parser
.addFixup(src
, ld::Fixup::k1of3
, ld::Fixup::kindSetTargetAddress
, false, personalityName
);
4647 parser
.addFixup(src
, ld::Fixup::k2of3
, ld::Fixup::kindAddAddend
, 4);
4648 parser
.addFixup(src
, ld::Fixup::k3of3
, ld::Fixup::kindStoreX86PCRel32GOT
);
4650 else if ( personalityEncoding
!= 0 ) {
4651 throwf("unsupported address encoding (%02X) of personality function in CIE",
4652 personalityEncoding
);
4657 void CFISection
<x86
>::addCiePersonalityFixups(class Parser
<x86
>& parser
, const CFI_Atom_Info
* cieInfo
)
4659 uint8_t personalityEncoding
= cieInfo
->u
.cieInfo
.personality
.encodingOfTargetAddress
;
4660 if ( (personalityEncoding
== 0x9B) || (personalityEncoding
== 0x90) ) {
4661 uint32_t offsetInCFI
= cieInfo
->u
.cieInfo
.personality
.offsetInCFI
;
4662 uint32_t nlpAddr
= cieInfo
->u
.cieInfo
.personality
.targetAddress
;
4663 Atom
<x86
>* cieAtom
= this->findAtomByAddress(cieInfo
->address
);
4664 Atom
<x86
>* nlpAtom
= parser
.findAtomByAddress(nlpAddr
);
4665 assert(nlpAtom
->contentType() == ld::Atom::typeNonLazyPointer
);
4666 Parser
<x86
>::SourceLocation
src(cieAtom
, cieInfo
->u
.cieInfo
.personality
.offsetInCFI
);
4668 parser
.addFixup(src
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, nlpAtom
);
4669 parser
.addFixup(src
, ld::Fixup::k2of4
, ld::Fixup::kindSubtractTargetAddress
, cieAtom
);
4670 parser
.addFixup(src
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractAddend
, offsetInCFI
);
4671 parser
.addFixup(src
, ld::Fixup::k4of4
, ld::Fixup::kindStoreLittleEndian32
);
4673 else if ( personalityEncoding
!= 0 ) {
4674 throwf("unsupported address encoding (%02X) of personality function in CIE", personalityEncoding
);
4678 #if SUPPORT_ARCH_arm64
4680 void CFISection
<arm64
>::addCiePersonalityFixups(class Parser
<arm64
>& parser
, const CFI_Atom_Info
* cieInfo
)
4682 uint8_t personalityEncoding
= cieInfo
->u
.cieInfo
.personality
.encodingOfTargetAddress
;
4683 if ( personalityEncoding
== 0x9B ) {
4684 // compiler always produces ARM64_RELOC_GOT r_pcrel=1 to personality function
4685 // CFISection<arm64>::cfiParse() set targetAddress to be symbolIndex + addressInCIE
4686 uint32_t symbolIndex
= cieInfo
->u
.cieInfo
.personality
.targetAddress
4687 - cieInfo
->address
- cieInfo
->u
.cieInfo
.personality
.offsetInCFI
;
4688 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(symbolIndex
);
4689 const char* personalityName
= parser
.nameFromSymbol(sym
);
4691 Atom
<arm64
>* cieAtom
= this->findAtomByAddress(cieInfo
->address
);
4692 Parser
<arm64
>::SourceLocation
src(cieAtom
, cieInfo
->u
.cieInfo
.personality
.offsetInCFI
);
4693 parser
.addFixup(src
, ld::Fixup::k1of2
, ld::Fixup::kindSetTargetAddress
, false, personalityName
);
4694 parser
.addFixup(src
, ld::Fixup::k2of2
, ld::Fixup::kindStoreARM64PCRelToGOT
);
4696 else if ( personalityEncoding
!= 0 ) {
4697 throwf("unsupported address encoding (%02X) of personality function in CIE",
4698 personalityEncoding
);
4705 void CFISection
<arm
>::addCiePersonalityFixups(class Parser
<arm
>& parser
, const CFI_Atom_Info
* cieInfo
)
4707 uint8_t personalityEncoding
= cieInfo
->u
.cieInfo
.personality
.encodingOfTargetAddress
;
4708 if ( (personalityEncoding
== 0x9B) || (personalityEncoding
== 0x90) ) {
4709 uint32_t offsetInCFI
= cieInfo
->u
.cieInfo
.personality
.offsetInCFI
;
4710 uint32_t nlpAddr
= cieInfo
->u
.cieInfo
.personality
.targetAddress
;
4711 Atom
<arm
>* cieAtom
= this->findAtomByAddress(cieInfo
->address
);
4712 Atom
<arm
>* nlpAtom
= parser
.findAtomByAddress(nlpAddr
);
4713 assert(nlpAtom
->contentType() == ld::Atom::typeNonLazyPointer
);
4714 Parser
<arm
>::SourceLocation
src(cieAtom
, cieInfo
->u
.cieInfo
.personality
.offsetInCFI
);
4716 parser
.addFixup(src
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, nlpAtom
);
4717 parser
.addFixup(src
, ld::Fixup::k2of4
, ld::Fixup::kindSubtractTargetAddress
, cieAtom
);
4718 parser
.addFixup(src
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractAddend
, offsetInCFI
);
4719 parser
.addFixup(src
, ld::Fixup::k4of4
, ld::Fixup::kindStoreLittleEndian32
);
4721 else if ( personalityEncoding
!= 0 ) {
4722 throwf("unsupported address encoding (%02X) of personality function in CIE", personalityEncoding
);
4728 template <typename A
>
4729 void CFISection
<A
>::addCiePersonalityFixups(class Parser
<A
>& parser
, const CFI_Atom_Info
* cieInfo
)
4731 assert(0 && "addCiePersonalityFixups() not implemented for arch");
4734 template <typename A
>
4735 void CFISection
<A
>::makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
& cfis
)
4737 ld::Fixup::Kind store32
= bigEndian() ? ld::Fixup::kindStoreBigEndian32
: ld::Fixup::kindStoreLittleEndian32
;
4738 ld::Fixup::Kind store64
= bigEndian() ? ld::Fixup::kindStoreBigEndian64
: ld::Fixup::kindStoreLittleEndian64
;
4740 // add all references for FDEs, including implicit group references
4741 const CFI_Atom_Info
* end
= &cfis
.cfiArray
[cfis
.cfiCount
];
4742 for(const CFI_Atom_Info
* p
= &cfis
.cfiArray
[0]; p
< end
; ++p
) {
4744 // add reference to personality function if used
4745 if ( p
->u
.cieInfo
.personality
.targetAddress
!= CFI_INVALID_ADDRESS
) {
4746 this->addCiePersonalityFixups(parser
, p
);
4751 Atom
<A
>* fdeAtom
= this->findAtomByAddress(p
->address
);
4752 // find function Atom
4753 Atom
<A
>* functionAtom
= parser
.findAtomByAddress(p
->u
.fdeInfo
.function
.targetAddress
);
4755 Atom
<A
>* cieAtom
= this->findAtomByAddress(p
->u
.fdeInfo
.cie
.targetAddress
);
4757 Atom
<A
>* lsdaAtom
= NULL
;
4758 if ( p
->u
.fdeInfo
.lsda
.targetAddress
!= CFI_INVALID_ADDRESS
) {
4759 lsdaAtom
= parser
.findAtomByAddress(p
->u
.fdeInfo
.lsda
.targetAddress
);
4761 // add reference from FDE to CIE (always 32-bit pc-rel)
4762 typename Parser
<A
>::SourceLocation
fdeToCieSrc(fdeAtom
, p
->u
.fdeInfo
.cie
.offsetInCFI
);
4763 parser
.addFixup(fdeToCieSrc
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, fdeAtom
);
4764 parser
.addFixup(fdeToCieSrc
, ld::Fixup::k2of4
, ld::Fixup::kindAddAddend
, p
->u
.fdeInfo
.cie
.offsetInCFI
);
4765 parser
.addFixup(fdeToCieSrc
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractTargetAddress
, cieAtom
);
4766 parser
.addFixup(fdeToCieSrc
, ld::Fixup::k4of4
, store32
, cieAtom
);
4768 // add reference from FDE to function
4769 typename Parser
<A
>::SourceLocation
fdeToFuncSrc(fdeAtom
, p
->u
.fdeInfo
.function
.offsetInCFI
);
4770 switch (p
->u
.fdeInfo
.function
.encodingOfTargetAddress
) {
4771 case DW_EH_PE_pcrel
|DW_EH_PE_ptr
:
4772 if ( sizeof(typename
A::P::uint_t
) == 8 ) {
4773 parser
.addFixup(fdeToFuncSrc
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, functionAtom
);
4774 parser
.addFixup(fdeToFuncSrc
, ld::Fixup::k2of4
, ld::Fixup::kindSubtractTargetAddress
, fdeAtom
);
4775 parser
.addFixup(fdeToFuncSrc
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractAddend
, p
->u
.fdeInfo
.function
.offsetInCFI
);
4776 parser
.addFixup(fdeToFuncSrc
, ld::Fixup::k4of4
, store64
);
4779 // else fall into 32-bit case
4780 case DW_EH_PE_pcrel
|DW_EH_PE_sdata4
:
4781 parser
.addFixup(fdeToFuncSrc
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, functionAtom
);
4782 parser
.addFixup(fdeToFuncSrc
, ld::Fixup::k2of4
, ld::Fixup::kindSubtractTargetAddress
, fdeAtom
);
4783 parser
.addFixup(fdeToFuncSrc
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractAddend
, p
->u
.fdeInfo
.function
.offsetInCFI
);
4784 parser
.addFixup(fdeToFuncSrc
, ld::Fixup::k4of4
, store32
);
4787 throw "unsupported encoding in FDE of pointer to function";
4790 // add reference from FDE to LSDA
4791 typename Parser
<A
>::SourceLocation
fdeToLsdaSrc(fdeAtom
, p
->u
.fdeInfo
.lsda
.offsetInCFI
);
4792 if ( lsdaAtom
!= NULL
) {
4793 switch (p
->u
.fdeInfo
.lsda
.encodingOfTargetAddress
) {
4794 case DW_EH_PE_pcrel
|DW_EH_PE_ptr
:
4795 if ( sizeof(typename
A::P::uint_t
) == 8 ) {
4796 parser
.addFixup(fdeToLsdaSrc
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, lsdaAtom
);
4797 parser
.addFixup(fdeToLsdaSrc
, ld::Fixup::k2of4
, ld::Fixup::kindSubtractTargetAddress
, fdeAtom
);
4798 parser
.addFixup(fdeToLsdaSrc
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractAddend
, p
->u
.fdeInfo
.lsda
.offsetInCFI
);
4799 parser
.addFixup(fdeToLsdaSrc
, ld::Fixup::k4of4
, store64
);
4802 // else fall into 32-bit case
4803 case DW_EH_PE_pcrel
|DW_EH_PE_sdata4
:
4804 parser
.addFixup(fdeToLsdaSrc
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, lsdaAtom
);
4805 parser
.addFixup(fdeToLsdaSrc
, ld::Fixup::k2of4
, ld::Fixup::kindSubtractTargetAddress
, fdeAtom
);
4806 parser
.addFixup(fdeToLsdaSrc
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractAddend
, p
->u
.fdeInfo
.lsda
.offsetInCFI
);
4807 parser
.addFixup(fdeToLsdaSrc
, ld::Fixup::k4of4
, store32
);
4810 throw "unsupported encoding in FDE of pointer to LSDA";
4814 // FDE is in group lead by function atom
4815 typename Parser
<A
>::SourceLocation
fdeSrc(functionAtom
,0);
4816 parser
.addFixup(fdeSrc
, ld::Fixup::k1of1
, ld::Fixup::kindNoneGroupSubordinateFDE
, fdeAtom
);
4818 // LSDA is in group lead by function atom
4819 if ( lsdaAtom
!= NULL
) {
4820 parser
.addFixup(fdeSrc
, ld::Fixup::k1of1
, ld::Fixup::kindNoneGroupSubordinateLSDA
, lsdaAtom
);
4829 template <typename A
>
4830 const void* CFISection
<A
>::OAS::mappedAddress(pint_t addr
)
4832 if ( (_ehFrameStartAddr
<= addr
) && (addr
< _ehFrameEndAddr
) )
4833 return &_ehFrameContent
[addr
-_ehFrameStartAddr
];
4835 // requested bytes are not in __eh_frame section
4836 // this can occur when examining the instruction bytes in the __text
4837 File
<A
>& file
= _ehFrameSection
.file();
4838 for (uint32_t i
=0; i
< file
._sectionsArrayCount
; ++i
) {
4839 const macho_section
<typename
A::P
>* sect
= file
._sectionsArray
[i
]->machoSection();
4840 // TentativeDefinitionSection and AbsoluteSymbolSection have no mach-o section
4841 if ( sect
!= NULL
) {
4842 if ( (sect
->addr() <= addr
) && (addr
< (sect
->addr()+sect
->size())) ) {
4843 return file
.fileContent() + sect
->offset() + addr
- sect
->addr();
4847 throwf("__eh_frame parsing problem. Can't find target of reference to address 0x%08llX", (uint64_t)addr
);
4852 template <typename A
>
4853 uint64_t CFISection
<A
>::OAS::getULEB128(pint_t
& logicalAddr
, pint_t end
)
4855 uintptr_t size
= (end
- logicalAddr
);
4856 libunwind::LocalAddressSpace::pint_t laddr
= (libunwind::LocalAddressSpace::pint_t
)mappedAddress(logicalAddr
);
4857 libunwind::LocalAddressSpace::pint_t sladdr
= laddr
;
4858 uint64_t result
= libunwind::LocalAddressSpace::getULEB128(laddr
, laddr
+size
);
4859 logicalAddr
+= (laddr
-sladdr
);
4863 template <typename A
>
4864 int64_t CFISection
<A
>::OAS::getSLEB128(pint_t
& logicalAddr
, pint_t end
)
4866 uintptr_t size
= (end
- logicalAddr
);
4867 libunwind::LocalAddressSpace::pint_t laddr
= (libunwind::LocalAddressSpace::pint_t
)mappedAddress(logicalAddr
);
4868 libunwind::LocalAddressSpace::pint_t sladdr
= laddr
;
4869 int64_t result
= libunwind::LocalAddressSpace::getSLEB128(laddr
, laddr
+size
);
4870 logicalAddr
+= (laddr
-sladdr
);
4874 template <typename A
>
4875 typename
A::P::uint_t CFISection
<A
>::OAS::getEncodedP(pint_t
& addr
, pint_t end
, uint8_t encoding
)
4877 pint_t startAddr
= addr
;
4882 switch (encoding
& 0x0F) {
4884 result
= getP(addr
);
4885 p
+= sizeof(pint_t
);
4888 case DW_EH_PE_uleb128
:
4889 result
= getULEB128(addr
, end
);
4891 case DW_EH_PE_udata2
:
4892 result
= get16(addr
);
4896 case DW_EH_PE_udata4
:
4897 result
= get32(addr
);
4901 case DW_EH_PE_udata8
:
4902 result
= get64(addr
);
4906 case DW_EH_PE_sleb128
:
4907 result
= getSLEB128(addr
, end
);
4909 case DW_EH_PE_sdata2
:
4910 result
= (int16_t)get16(addr
);
4914 case DW_EH_PE_sdata4
:
4915 result
= (int32_t)get32(addr
);
4919 case DW_EH_PE_sdata8
:
4920 result
= get64(addr
);
4925 throwf("ObjectFileAddressSpace<A>::getEncodedP() encoding 0x%08X not supported", encoding
);
4928 // then add relative offset
4929 switch ( encoding
& 0x70 ) {
4930 case DW_EH_PE_absptr
:
4933 case DW_EH_PE_pcrel
:
4934 result
+= startAddr
;
4936 case DW_EH_PE_textrel
:
4937 throw "DW_EH_PE_textrel pointer encoding not supported";
4939 case DW_EH_PE_datarel
:
4940 throw "DW_EH_PE_datarel pointer encoding not supported";
4942 case DW_EH_PE_funcrel
:
4943 throw "DW_EH_PE_funcrel pointer encoding not supported";
4945 case DW_EH_PE_aligned
:
4946 throw "DW_EH_PE_aligned pointer encoding not supported";
4949 throwf("ObjectFileAddressSpace<A>::getEncodedP() encoding 0x%08X not supported", encoding
);
4953 // Note: DW_EH_PE_indirect is only used in CIEs to refernce the personality pointer
4954 // When parsing .o files that pointer contains zero, so we don't to return that.
4955 // Instead we skip the dereference and return the address of the pointer.
4956 // if ( encoding & DW_EH_PE_indirect )
4957 // result = getP(result);
4963 const char* CUSection
<x86_64
>::personalityName(class Parser
<x86_64
>& parser
, const macho_relocation_info
<x86_64::P
>* reloc
)
4965 if ( reloc
->r_extern() ) {
4966 assert((reloc
->r_type() == X86_64_RELOC_UNSIGNED
) && "wrong reloc type on personality column in __compact_unwind section");
4967 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(reloc
->r_symbolnum());
4968 return parser
.nameFromSymbol(sym
);
4971 const pint_t
* content
= (pint_t
*)(this->file().fileContent() + this->_machOSection
->offset() + reloc
->r_address());
4972 pint_t personalityAddr
= *content
;
4973 assert((parser
.sectionForAddress(personalityAddr
)->type() == ld::Section::typeCode
) && "personality column in __compact_unwind section is not pointer to function");
4974 // atoms may not be constructed yet, so scan symbol table for labels
4975 const char* name
= parser
.scanSymbolTableForAddress(personalityAddr
);
4981 const char* CUSection
<x86
>::personalityName(class Parser
<x86
>& parser
, const macho_relocation_info
<x86::P
>* reloc
)
4983 if ( reloc
->r_extern() ) {
4984 assert((reloc
->r_type() == GENERIC_RELOC_VANILLA
) && "wrong reloc type on personality column in __compact_unwind section");
4985 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(reloc
->r_symbolnum());
4986 return parser
.nameFromSymbol(sym
);
4989 // support __LD, __compact_unwind personality entries which are pointer to personality non-lazy pointer
4990 const pint_t
* content
= (pint_t
*)(this->file().fileContent() + this->_machOSection
->offset() + reloc
->r_address());
4991 pint_t nlPointerAddr
= *content
;
4992 Section
<x86
>* nlSection
= parser
.sectionForAddress(nlPointerAddr
);
4993 if ( nlSection
->type() == ld::Section::typeCode
) {
4994 // personality function is defined in this .o file, so this is a direct reference to it
4995 // atoms may not be constructed yet, so scan symbol table for labels
4996 const char* name
= parser
.scanSymbolTableForAddress(nlPointerAddr
);
5000 uint32_t symIndex
= parser
.symbolIndexFromIndirectSectionAddress(nlPointerAddr
, nlSection
->machoSection());
5001 const macho_nlist
<P
>& nlSymbol
= parser
.symbolFromIndex(symIndex
);
5002 return parser
.nameFromSymbol(nlSymbol
);
5007 #if SUPPORT_ARCH_arm64
5009 const char* CUSection
<arm64
>::personalityName(class Parser
<arm64
>& parser
, const macho_relocation_info
<arm64::P
>* reloc
)
5011 if ( reloc
->r_extern() ) {
5012 assert((reloc
->r_type() == ARM64_RELOC_UNSIGNED
) && "wrong reloc type on personality column in __compact_unwind section");
5013 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(reloc
->r_symbolnum());
5014 return parser
.nameFromSymbol(sym
);
5017 const pint_t
* content
= (pint_t
*)(this->file().fileContent() + this->_machOSection
->offset() + reloc
->r_address());
5018 pint_t personalityAddr
= *content
;
5019 Section
<arm64
>* personalitySection
= parser
.sectionForAddress(personalityAddr
);
5020 (void)personalitySection
;
5021 assert((personalitySection
->type() == ld::Section::typeCode
) && "personality column in __compact_unwind section is not pointer to function");
5022 // atoms may not be constructed yet, so scan symbol table for labels
5023 const char* name
= parser
.scanSymbolTableForAddress(personalityAddr
);
5030 #if SUPPORT_ARCH_arm_any
5032 const char* CUSection
<arm
>::personalityName(class Parser
<arm
>& parser
, const macho_relocation_info
<arm::P
>* reloc
)
5034 if ( reloc
->r_extern() ) {
5035 assert((reloc
->r_type() == ARM_RELOC_VANILLA
) && "wrong reloc type on personality column in __compact_unwind section");
5036 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(reloc
->r_symbolnum());
5037 return parser
.nameFromSymbol(sym
);
5040 // support __LD, __compact_unwind personality entries which are pointer to personality non-lazy pointer
5041 const pint_t
* content
= (pint_t
*)(this->file().fileContent() + this->_machOSection
->offset() + reloc
->r_address());
5042 pint_t nlPointerAddr
= *content
;
5043 Section
<arm
>* nlSection
= parser
.sectionForAddress(nlPointerAddr
);
5044 if ( nlSection
->type() == ld::Section::typeCode
) {
5045 // personality function is defined in this .o file, so this is a direct reference to it
5046 // atoms may not be constructed yet, so scan symbol table for labels
5047 const char* name
= parser
.scanSymbolTableForAddress(nlPointerAddr
);
5051 uint32_t symIndex
= parser
.symbolIndexFromIndirectSectionAddress(nlPointerAddr
, nlSection
->machoSection());
5052 const macho_nlist
<P
>& nlSymbol
= parser
.symbolFromIndex(symIndex
);
5053 return parser
.nameFromSymbol(nlSymbol
);
5060 template <typename A
>
5061 const char* CUSection
<A
>::personalityName(class Parser
<A
>& parser
, const macho_relocation_info
<P
>* reloc
)
5067 bool CUSection
<x86
>::encodingMeansUseDwarf(compact_unwind_encoding_t enc
)
5069 return ((enc
& UNWIND_X86_MODE_MASK
) == UNWIND_X86_MODE_DWARF
);
5073 bool CUSection
<x86_64
>::encodingMeansUseDwarf(compact_unwind_encoding_t enc
)
5075 return ((enc
& UNWIND_X86_64_MODE_MASK
) == UNWIND_X86_64_MODE_DWARF
);
5078 #if SUPPORT_ARCH_arm_any
5080 bool CUSection
<arm
>::encodingMeansUseDwarf(compact_unwind_encoding_t enc
)
5082 return ((enc
& UNWIND_ARM_MODE_MASK
) == UNWIND_ARM_MODE_DWARF
);
5086 #if SUPPORT_ARCH_arm64
5088 bool CUSection
<arm64
>::encodingMeansUseDwarf(compact_unwind_encoding_t enc
)
5090 return ((enc
& UNWIND_ARM64_MODE_MASK
) == UNWIND_ARM64_MODE_DWARF
);
5095 template <typename A
>
5096 int CUSection
<A
>::infoSorter(const void* l
, const void* r
)
5098 // sort references by symbol index, then address
5099 const Info
* left
= (Info
*)l
;
5100 const Info
* right
= (Info
*)r
;
5101 if ( left
->functionSymbolIndex
== right
->functionSymbolIndex
)
5102 return (left
->functionStartAddress
- right
->functionStartAddress
);
5104 return (left
->functionSymbolIndex
- right
->functionSymbolIndex
);
5107 template <typename A
>
5108 void CUSection
<A
>::parse(class Parser
<A
>& parser
, uint32_t cnt
, Info array
[])
5110 // walk section content and copy to Info array
5111 const macho_compact_unwind_entry
<P
>* const entries
= (macho_compact_unwind_entry
<P
>*)(this->file().fileContent() + this->_machOSection
->offset());
5112 for (uint32_t i
=0; i
< cnt
; ++i
) {
5113 Info
* info
= &array
[i
];
5114 const macho_compact_unwind_entry
<P
>* entry
= &entries
[i
];
5115 info
->functionStartAddress
= entry
->codeStart();
5116 info
->functionSymbolIndex
= 0xFFFFFFFF;
5117 info
->rangeLength
= entry
->codeLen();
5118 info
->compactUnwindInfo
= entry
->compactUnwindInfo();
5119 info
->personality
= NULL
;
5120 info
->lsdaAddress
= entry
->lsda();
5121 info
->function
= NULL
;
5123 if ( (info
->compactUnwindInfo
& UNWIND_PERSONALITY_MASK
) != 0 )
5124 warning("no bits should be set in UNWIND_PERSONALITY_MASK of compact unwind encoding in __LD,__compact_unwind section");
5125 if ( info
->lsdaAddress
!= 0 ) {
5126 info
->compactUnwindInfo
|= UNWIND_HAS_LSDA
;
5130 // scan relocs, extern relocs are needed for personality references (possibly for function/lsda refs??)
5131 const uint32_t sectionSize
= this->_machOSection
->size();
5132 const macho_relocation_info
<P
>* relocs
= (macho_relocation_info
<P
>*)(this->file().fileContent() + this->_machOSection
->reloff());
5133 const macho_relocation_info
<P
>* relocsEnd
= &relocs
[this->_machOSection
->nreloc()];
5134 for (const macho_relocation_info
<P
>* reloc
= relocs
; reloc
< relocsEnd
; ++reloc
) {
5135 if ( reloc
->r_address() & R_SCATTERED
)
5137 if ( reloc
->r_address() > sectionSize
)
5138 throwf("malformed __compact_unwind relocation, offset (0x%08X) is beyond end of section,", reloc
->r_address());
5139 if ( reloc
->r_extern() ) {
5140 // only expect external relocs on some colummns
5141 if ( (reloc
->r_address() % sizeof(macho_compact_unwind_entry
<P
>)) == macho_compact_unwind_entry
<P
>::personalityFieldOffset() ) {
5142 uint32_t entryIndex
= reloc
->r_address() / sizeof(macho_compact_unwind_entry
<P
>);
5143 array
[entryIndex
].personality
= this->personalityName(parser
, reloc
);
5145 else if ( (reloc
->r_address() % sizeof(macho_compact_unwind_entry
<P
>)) == macho_compact_unwind_entry
<P
>::lsdaFieldOffset() ) {
5146 uint32_t entryIndex
= reloc
->r_address() / sizeof(macho_compact_unwind_entry
<P
>);
5147 const macho_nlist
<P
>& lsdaSym
= parser
.symbolFromIndex(reloc
->r_symbolnum());
5148 if ( (lsdaSym
.n_type() & N_TYPE
) == N_SECT
)
5149 array
[entryIndex
].lsdaAddress
= lsdaSym
.n_value();
5151 warning("unexpected extern relocation to lsda in __compact_unwind section");
5153 else if ( (reloc
->r_address() % sizeof(macho_compact_unwind_entry
<P
>)) == macho_compact_unwind_entry
<P
>::codeStartFieldOffset() ) {
5154 uint32_t entryIndex
= reloc
->r_address() / sizeof(macho_compact_unwind_entry
<P
>);
5155 array
[entryIndex
].functionSymbolIndex
= reloc
->r_symbolnum();
5156 array
[entryIndex
].functionStartAddress
+= parser
.symbolFromIndex(reloc
->r_symbolnum()).n_value();
5159 warning("unexpected extern relocation in __compact_unwind section");
5163 if ( (reloc
->r_address() % sizeof(macho_compact_unwind_entry
<P
>)) == macho_compact_unwind_entry
<P
>::personalityFieldOffset() ) {
5164 uint32_t entryIndex
= reloc
->r_address() / sizeof(macho_compact_unwind_entry
<P
>);
5165 array
[entryIndex
].personality
= this->personalityName(parser
, reloc
);
5170 // sort array by function start address so unwind infos will be contiguous for a given function
5171 ::qsort(array
, cnt
, sizeof(Info
), infoSorter
);
5174 template <typename A
>
5175 uint32_t CUSection
<A
>::count()
5177 const macho_section
<P
>* machoSect
= this->machoSection();
5178 if ( (machoSect
->size() % sizeof(macho_compact_unwind_entry
<P
>)) != 0 )
5179 throw "malformed __LD,__compact_unwind section, bad length";
5181 return machoSect
->size() / sizeof(macho_compact_unwind_entry
<P
>);
5184 template <typename A
>
5185 void CUSection
<A
>::makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
& cus
)
5187 Info
* const arrayStart
= cus
.cuArray
;
5188 Info
* const arrayEnd
= &cus
.cuArray
[cus
.cuCount
];
5189 for (Info
* info
=arrayStart
; info
< arrayEnd
; ++info
) {
5190 // find function atom from address
5191 info
->function
= parser
.findAtomByAddress(info
->functionStartAddress
);
5192 // find lsda atom from address
5193 if ( info
->lsdaAddress
!= 0 ) {
5194 info
->lsda
= parser
.findAtomByAddress(info
->lsdaAddress
);
5195 // add lsda subordinate
5196 typename Parser
<A
>::SourceLocation
src(info
->function
, info
->functionStartAddress
- info
->function
->objectAddress());
5197 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindNoneGroupSubordinateLSDA
, info
->lsda
);
5199 if ( info
->personality
!= NULL
) {
5200 // add personality subordinate
5201 typename Parser
<A
>::SourceLocation
src(info
->function
, info
->functionStartAddress
- info
->function
->objectAddress());
5202 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindNoneGroupSubordinatePersonality
, false, info
->personality
);
5208 template <typename A
>
5209 SymboledSection
<A
>::SymboledSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
5210 : Section
<A
>(f
, s
), _type(ld::Atom::typeUnclassified
)
5212 switch ( s
->flags() & SECTION_TYPE
) {
5214 _type
= ld::Atom::typeZeroFill
;
5216 case S_MOD_INIT_FUNC_POINTERS
:
5217 _type
= ld::Atom::typeInitializerPointers
;
5219 case S_MOD_TERM_FUNC_POINTERS
:
5220 _type
= ld::Atom::typeTerminatorPointers
;
5222 case S_THREAD_LOCAL_VARIABLES
:
5223 _type
= ld::Atom::typeTLV
;
5225 case S_THREAD_LOCAL_ZEROFILL
:
5226 _type
= ld::Atom::typeTLVZeroFill
;
5228 case S_THREAD_LOCAL_REGULAR
:
5229 _type
= ld::Atom::typeTLVInitialValue
;
5231 case S_THREAD_LOCAL_INIT_FUNCTION_POINTERS
:
5232 _type
= ld::Atom::typeTLVInitializerPointers
;
5235 if ( strncmp(s
->sectname(), "__gcc_except_tab", 16) == 0 )
5236 _type
= ld::Atom::typeLSDA
;
5237 else if ( this->type() == ld::Section::typeInitializerPointers
)
5238 _type
= ld::Atom::typeInitializerPointers
;
5244 template <typename A
>
5245 bool SymboledSection
<A
>::dontDeadStrip()
5248 case ld::Atom::typeInitializerPointers
:
5249 case ld::Atom::typeTerminatorPointers
:
5252 // model an object file without MH_SUBSECTIONS_VIA_SYMBOLS as one in which nothing can be dead stripped
5253 if ( ! this->_file
.canScatterAtoms() )
5256 return Section
<A
>::dontDeadStrip();
5262 template <typename A
>
5263 uint32_t SymboledSection
<A
>::computeAtomCount(class Parser
<A
>& parser
,
5264 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
5265 const struct Parser
<A
>::CFI_CU_InfoArrays
&)
5267 const pint_t startAddr
= this->_machOSection
->addr();
5268 const pint_t endAddr
= startAddr
+ this->_machOSection
->size();
5269 const uint32_t sectNum
= this->sectionNum(parser
);
5274 const macho_nlist
<P
>* sym
;
5275 while ( it
.next(parser
, *this, sectNum
, startAddr
, endAddr
, &addr
, &size
, &sym
) ) {
5278 //fprintf(stderr, "computeAtomCount(%s,%s) => %d\n", this->segmentName(), this->sectionName(), count);
5282 template <typename A
>
5283 uint32_t SymboledSection
<A
>::appendAtoms(class Parser
<A
>& parser
, uint8_t* p
,
5284 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
5285 const struct Parser
<A
>::CFI_CU_InfoArrays
&)
5287 this->_beginAtoms
= (Atom
<A
>*)p
;
5289 //fprintf(stderr, "SymboledSection::appendAtoms() in section %s\n", this->_machOSection->sectname());
5290 const pint_t startAddr
= this->_machOSection
->addr();
5291 const pint_t endAddr
= startAddr
+ this->_machOSection
->size();
5292 const uint32_t sectNum
= this->sectionNum(parser
);
5297 const macho_nlist
<P
>* label
;
5298 while ( it
.next(parser
, *this, sectNum
, startAddr
, endAddr
, &addr
, &size
, &label
) ) {
5299 Atom
<A
>* allocatedSpace
= (Atom
<A
>*)p
;
5300 // is break because of label or CFI?
5301 if ( label
!= NULL
) {
5302 // The size is computed based on the address of the next label (or the end of the section for the last label)
5303 // If there are two labels at the same address, we want them one to be an alias of the other.
5304 // If the label is at the end of a section, it is has zero size, but is not an alias
5305 const bool isAlias
= ( (size
== 0) && (addr
< endAddr
) );
5306 new (allocatedSpace
) Atom
<A
>(*this, parser
, *label
, size
, isAlias
);
5308 this->_hasAliases
= true;
5309 if ( parser
.altEntryFromSymbol(*label
) )
5310 this->_altEntries
.insert(allocatedSpace
);
5313 ld::Atom::SymbolTableInclusion inclusion
= ld::Atom::symbolTableNotIn
;
5314 ld::Atom::ContentType ctype
= this->contentType();
5315 if ( ctype
== ld::Atom::typeLSDA
)
5316 inclusion
= ld::Atom::symbolTableInWithRandomAutoStripLabel
;
5317 new (allocatedSpace
) Atom
<A
>(*this, "anon", addr
, size
, ld::Atom::definitionRegular
, ld::Atom::combineNever
,
5318 ld::Atom::scopeTranslationUnit
, ctype
, inclusion
,
5319 this->dontDeadStrip(), false, false, this->alignmentForAddress(addr
));
5321 p
+= sizeof(Atom
<A
>);
5325 this->_endAtoms
= (Atom
<A
>*)p
;
5331 ld::Atom::SymbolTableInclusion ImplicitSizeSection
<arm64
>::symbolTableInclusion()
5333 return ld::Atom::symbolTableInWithRandomAutoStripLabel
;
5337 template <typename A
>
5338 ld::Atom::SymbolTableInclusion ImplicitSizeSection
<A
>::symbolTableInclusion()
5340 return ld::Atom::symbolTableNotIn
;
5344 template <typename A
>
5345 uint32_t ImplicitSizeSection
<A
>::computeAtomCount(class Parser
<A
>& parser
,
5346 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
5347 const struct Parser
<A
>::CFI_CU_InfoArrays
&)
5350 const macho_section
<P
>* sect
= this->machoSection();
5351 const pint_t startAddr
= sect
->addr();
5352 const pint_t endAddr
= startAddr
+ sect
->size();
5353 for (pint_t addr
= startAddr
; addr
< endAddr
; addr
+= elementSizeAtAddress(addr
) ) {
5354 if ( useElementAt(parser
, it
, addr
) )
5357 if ( it
.fileHasOverlappingSymbols
&& (sect
->size() != 0) && (this->combine(parser
, startAddr
) == ld::Atom::combineByNameAndContent
) ) {
5358 // if there are multiple labels in this section for the same address, then clone them into multi atoms
5359 pint_t prevSymbolAddr
= (pint_t
)(-1);
5360 uint8_t prevSymbolSectNum
= 0;
5361 bool prevIgnore
= false;
5362 for(uint32_t i
=0; i
< it
.sortedSymbolCount
; ++i
) {
5363 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(it
.sortedSymbolIndexes
[i
]);
5364 const pint_t symbolAddr
= sym
.n_value();
5365 const uint8_t symbolSectNum
= sym
.n_sect();
5366 const bool ignore
= this->ignoreLabel(parser
.nameFromSymbol(sym
));
5367 if ( !ignore
&& !prevIgnore
&& (symbolAddr
== prevSymbolAddr
) && (prevSymbolSectNum
== symbolSectNum
) && (symbolSectNum
== this->sectionNum(parser
)) ) {
5370 prevSymbolAddr
= symbolAddr
;
5371 prevSymbolSectNum
= symbolSectNum
;
5372 prevIgnore
= ignore
;
5378 template <typename A
>
5379 uint32_t ImplicitSizeSection
<A
>::appendAtoms(class Parser
<A
>& parser
, uint8_t* p
,
5380 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
5381 const struct Parser
<A
>::CFI_CU_InfoArrays
&)
5383 this->_beginAtoms
= (Atom
<A
>*)p
;
5385 const macho_section
<P
>* sect
= this->machoSection();
5386 const pint_t startAddr
= sect
->addr();
5387 const pint_t endAddr
= startAddr
+ sect
->size();
5388 const uint32_t sectNum
= this->sectionNum(parser
);
5389 //fprintf(stderr, "ImplicitSizeSection::appendAtoms() in section %s\n", sect->sectname());
5393 const macho_nlist
<P
>* foundLabel
;
5394 Atom
<A
>* allocatedSpace
;
5395 while ( it
.next(parser
, *this, sectNum
, startAddr
, endAddr
, &foundAddr
, &size
, &foundLabel
) ) {
5396 if ( foundLabel
!= NULL
) {
5398 pint_t labeledAtomSize
= this->elementSizeAtAddress(foundAddr
);
5399 allocatedSpace
= (Atom
<A
>*)p
;
5400 if ( this->ignoreLabel(parser
.nameFromSymbol(*foundLabel
)) ) {
5402 // <rdar://problem/10018737>
5403 // a size of zero means there is another label at same location
5404 // and we are supposed to ignore this label
5408 //fprintf(stderr, " 0x%08llX make annon, size=%lld\n", (uint64_t)foundAddr, (uint64_t)size);
5409 new (allocatedSpace
) Atom
<A
>(*this, this->unlabeledAtomName(parser
, foundAddr
), foundAddr
,
5410 this->elementSizeAtAddress(foundAddr
), this->definition(),
5411 this->combine(parser
, foundAddr
), this->scopeAtAddress(parser
, foundAddr
),
5412 this->contentType(), this->symbolTableInclusion(),
5413 this->dontDeadStrip(), false, false, this->alignmentForAddress(foundAddr
));
5417 // make named atom for label
5418 //fprintf(stderr, " 0x%08llX make labeled\n", (uint64_t)foundAddr);
5419 new (allocatedSpace
) Atom
<A
>(*this, parser
, *foundLabel
, labeledAtomSize
);
5423 p
+= sizeof(Atom
<A
>);
5424 foundAddr
+= labeledAtomSize
;
5425 size
-= labeledAtomSize
;
5428 // some number of anonymous atoms
5429 for (pint_t addr
= foundAddr
; addr
< (foundAddr
+size
); addr
+= elementSizeAtAddress(addr
) ) {
5430 // make anon atoms for area before label
5431 if ( this->useElementAt(parser
, it
, addr
) ) {
5432 //fprintf(stderr, " 0x%08llX make annon, size=%lld\n", (uint64_t)addr, (uint64_t)elementSizeAtAddress(addr));
5433 allocatedSpace
= (Atom
<A
>*)p
;
5434 new (allocatedSpace
) Atom
<A
>(*this, this->unlabeledAtomName(parser
, addr
), addr
, this->elementSizeAtAddress(addr
),
5435 this->definition(), this->combine(parser
, addr
), this->scopeAtAddress(parser
, addr
),
5436 this->contentType(), this->symbolTableInclusion(),
5437 this->dontDeadStrip(), false, false, this->alignmentForAddress(addr
));
5439 p
+= sizeof(Atom
<A
>);
5444 this->_endAtoms
= (Atom
<A
>*)p
;
5449 template <typename A
>
5450 bool Literal4Section
<A
>::ignoreLabel(const char* label
) const
5452 return (label
[0] == 'L') || (label
[0] == 'l');
5455 template <typename A
>
5456 unsigned long Literal4Section
<A
>::contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
5458 const uint32_t* literalContent
= (uint32_t*)atom
->contentPointer();
5459 return *literalContent
;
5462 template <typename A
>
5463 bool Literal4Section
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
5464 const ld::IndirectBindingTable
& ind
) const
5466 assert(this->type() == rhs
.section().type());
5467 const uint32_t* literalContent
= (uint32_t*)atom
->contentPointer();
5469 const Atom
<A
>* rhsAtom
= dynamic_cast<const Atom
<A
>*>(&rhs
);
5470 assert(rhsAtom
!= NULL
);
5471 if ( rhsAtom
!= NULL
) {
5472 const uint32_t* rhsLiteralContent
= (uint32_t*)rhsAtom
->contentPointer();
5473 return (*literalContent
== *rhsLiteralContent
);
5479 template <typename A
>
5480 bool Literal8Section
<A
>::ignoreLabel(const char* label
) const
5482 return (label
[0] == 'L') || (label
[0] == 'l');
5485 template <typename A
>
5486 unsigned long Literal8Section
<A
>::contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
5489 const uint64_t* literalContent
= (uint64_t*)atom
->contentPointer();
5490 return *literalContent
;
5492 unsigned long hash
= 5381;
5493 const uint8_t* byteContent
= atom
->contentPointer();
5494 for (int i
=0; i
< 8; ++i
) {
5495 hash
= hash
* 33 + byteContent
[i
];
5501 template <typename A
>
5502 bool Literal8Section
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
5503 const ld::IndirectBindingTable
& ind
) const
5505 if ( rhs
.section().type() != ld::Section::typeLiteral8
)
5507 assert(this->type() == rhs
.section().type());
5508 const uint64_t* literalContent
= (uint64_t*)atom
->contentPointer();
5510 const Atom
<A
>* rhsAtom
= dynamic_cast<const Atom
<A
>*>(&rhs
);
5511 assert(rhsAtom
!= NULL
);
5512 if ( rhsAtom
!= NULL
) {
5513 const uint64_t* rhsLiteralContent
= (uint64_t*)rhsAtom
->contentPointer();
5514 return (*literalContent
== *rhsLiteralContent
);
5519 template <typename A
>
5520 bool Literal16Section
<A
>::ignoreLabel(const char* label
) const
5522 return (label
[0] == 'L') || (label
[0] == 'l');
5525 template <typename A
>
5526 unsigned long Literal16Section
<A
>::contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
5528 unsigned long hash
= 5381;
5529 const uint8_t* byteContent
= atom
->contentPointer();
5530 for (int i
=0; i
< 16; ++i
) {
5531 hash
= hash
* 33 + byteContent
[i
];
5536 template <typename A
>
5537 bool Literal16Section
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
5538 const ld::IndirectBindingTable
& ind
) const
5540 if ( rhs
.section().type() != ld::Section::typeLiteral16
)
5542 assert(this->type() == rhs
.section().type());
5543 const uint64_t* literalContent
= (uint64_t*)atom
->contentPointer();
5545 const Atom
<A
>* rhsAtom
= dynamic_cast<const Atom
<A
>*>(&rhs
);
5546 assert(rhsAtom
!= NULL
);
5547 if ( rhsAtom
!= NULL
) {
5548 const uint64_t* rhsLiteralContent
= (uint64_t*)rhsAtom
->contentPointer();
5549 return ((literalContent
[0] == rhsLiteralContent
[0]) && (literalContent
[1] == rhsLiteralContent
[1]));
5556 template <typename A
>
5557 typename
A::P::uint_t CStringSection
<A
>::elementSizeAtAddress(pint_t addr
)
5559 const macho_section
<P
>* sect
= this->machoSection();
5560 const char* stringContent
= (char*)(this->file().fileContent() + sect
->offset() + addr
- sect
->addr());
5561 return strlen(stringContent
) + 1;
5564 template <typename A
>
5565 bool CStringSection
<A
>::useElementAt(Parser
<A
>& parser
, struct Parser
<A
>::LabelAndCFIBreakIterator
& it
, pint_t addr
)
5570 template <typename A
>
5571 bool CStringSection
<A
>::ignoreLabel(const char* label
) const
5573 return (label
[0] == 'L') || (label
[0] == 'l');
5577 template <typename A
>
5578 Atom
<A
>* CStringSection
<A
>::findAtomByAddress(pint_t addr
)
5580 Atom
<A
>* result
= this->findContentAtomByAddress(addr
, this->_beginAtoms
, this->_endAtoms
);
5584 template <typename A
>
5585 unsigned long CStringSection
<A
>::contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
5587 unsigned long hash
= 5381;
5588 const char* stringContent
= (char*)atom
->contentPointer();
5589 for (const char* s
= stringContent
; *s
!= '\0'; ++s
) {
5590 hash
= hash
* 33 + *s
;
5596 template <typename A
>
5597 bool CStringSection
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
5598 const ld::IndirectBindingTable
& ind
) const
5600 if ( rhs
.section().type() != ld::Section::typeCString
)
5602 assert(this->type() == rhs
.section().type());
5603 assert(strcmp(this->sectionName(), rhs
.section().sectionName())== 0);
5604 assert(strcmp(this->segmentName(), rhs
.section().segmentName())== 0);
5605 const char* stringContent
= (char*)atom
->contentPointer();
5607 const Atom
<A
>* rhsAtom
= dynamic_cast<const Atom
<A
>*>(&rhs
);
5608 assert(rhsAtom
!= NULL
);
5609 if ( rhsAtom
!= NULL
) {
5610 if ( atom
->_size
!= rhsAtom
->_size
)
5612 const char* rhsStringContent
= (char*)rhsAtom
->contentPointer();
5613 return (strcmp(stringContent
, rhsStringContent
) == 0);
5620 ld::Fixup::Kind NonLazyPointerSection
<x86
>::fixupKind()
5622 return ld::Fixup::kindStoreLittleEndian32
;
5626 ld::Fixup::Kind NonLazyPointerSection
<arm
>::fixupKind()
5628 return ld::Fixup::kindStoreLittleEndian32
;
5632 ld::Fixup::Kind NonLazyPointerSection
<arm64
>::fixupKind()
5634 return ld::Fixup::kindStoreLittleEndian64
;
5639 void NonLazyPointerSection
<x86_64
>::makeFixups(class Parser
<x86_64
>& parser
, const struct Parser
<x86_64
>::CFI_CU_InfoArrays
&)
5641 assert(0 && "x86_64 should not have non-lazy-pointer sections in .o files");
5644 template <typename A
>
5645 void NonLazyPointerSection
<A
>::makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
&)
5647 // add references for each NLP atom based on indirect symbol table
5648 const macho_section
<P
>* sect
= this->machoSection();
5649 const pint_t endAddr
= sect
->addr() + sect
->size();
5650 for( pint_t addr
= sect
->addr(); addr
< endAddr
; addr
+= sizeof(pint_t
)) {
5651 typename Parser
<A
>::SourceLocation src
;
5652 typename Parser
<A
>::TargetDesc target
;
5653 src
.atom
= this->findAtomByAddress(addr
);
5654 src
.offsetInAtom
= 0;
5655 uint32_t symIndex
= parser
.symbolIndexFromIndirectSectionAddress(addr
, sect
);
5658 target
.weakImport
= false;
5660 if ( symIndex
== INDIRECT_SYMBOL_LOCAL
) {
5661 // use direct reference for local symbols
5662 const pint_t
* nlpContent
= (pint_t
*)(this->file().fileContent() + sect
->offset() + addr
- sect
->addr());
5663 pint_t targetAddr
= P::getP(*nlpContent
);
5664 target
.atom
= parser
.findAtomByAddress(targetAddr
);
5665 target
.weakImport
= false;
5666 target
.addend
= (targetAddr
- target
.atom
->objectAddress());
5667 // <rdar://problem/8385011> if pointer to thumb function, mask of thumb bit (not an addend of +1)
5668 if ( target
.atom
->isThumb() )
5669 target
.addend
&= (-2);
5670 assert(src
.atom
->combine() == ld::Atom::combineNever
);
5673 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(symIndex
);
5674 // use direct reference for local symbols
5675 if ( ((sym
.n_type() & N_TYPE
) == N_SECT
) && ((sym
.n_type() & N_EXT
) == 0) ) {
5676 parser
.findTargetFromAddressAndSectionNum(sym
.n_value(), sym
.n_sect(), target
);
5677 assert(src
.atom
->combine() == ld::Atom::combineNever
);
5680 target
.name
= parser
.nameFromSymbol(sym
);
5681 target
.weakImport
= parser
.weakImportFromSymbol(sym
);
5682 assert(src
.atom
->combine() == ld::Atom::combineByNameAndReferences
);
5685 parser
.addFixups(src
, this->fixupKind(), target
);
5689 template <typename A
>
5690 ld::Atom::Combine NonLazyPointerSection
<A
>::combine(Parser
<A
>& parser
, pint_t addr
)
5692 const macho_section
<P
>* sect
= this->machoSection();
5693 uint32_t symIndex
= parser
.symbolIndexFromIndirectSectionAddress(addr
, sect
);
5694 if ( symIndex
== INDIRECT_SYMBOL_LOCAL
)
5695 return ld::Atom::combineNever
;
5697 // don't coalesce non-lazy-pointers to local symbols
5698 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(symIndex
);
5699 if ( ((sym
.n_type() & N_TYPE
) == N_SECT
) && ((sym
.n_type() & N_EXT
) == 0) )
5700 return ld::Atom::combineNever
;
5702 return ld::Atom::combineByNameAndReferences
;
5705 template <typename A
>
5706 const char* NonLazyPointerSection
<A
>::targetName(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
)
5708 assert(atom
->combine() == ld::Atom::combineByNameAndReferences
);
5709 assert(atom
->fixupCount() == 1);
5710 ld::Fixup::iterator fit
= atom
->fixupsBegin();
5711 const char* name
= NULL
;
5712 switch ( fit
->binding
) {
5713 case ld::Fixup::bindingByNameUnbound
:
5716 case ld::Fixup::bindingByContentBound
:
5717 name
= fit
->u
.target
->name();
5719 case ld::Fixup::bindingsIndirectlyBound
:
5720 name
= ind
.indirectName(fit
->u
.bindingIndex
);
5725 assert(name
!= NULL
);
5729 template <typename A
>
5730 unsigned long NonLazyPointerSection
<A
>::contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
5732 assert(atom
->combine() == ld::Atom::combineByNameAndReferences
);
5733 unsigned long hash
= 9508;
5734 for (const char* s
= this->targetName(atom
, ind
); *s
!= '\0'; ++s
) {
5735 hash
= hash
* 33 + *s
;
5740 template <typename A
>
5741 bool NonLazyPointerSection
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
5742 const ld::IndirectBindingTable
& indirectBindingTable
) const
5744 if ( rhs
.section().type() != ld::Section::typeNonLazyPointer
)
5746 assert(this->type() == rhs
.section().type());
5747 // there can be many non-lazy pointer in different section names
5748 // we only want to coalesce in same section name
5749 if ( *this != rhs
.section() )
5751 const Atom
<A
>* rhsAtom
= dynamic_cast<const Atom
<A
>*>(&rhs
);
5752 assert(rhsAtom
!= NULL
);
5753 const char* thisName
= this->targetName(atom
, indirectBindingTable
);
5754 const char* rhsName
= this->targetName(rhsAtom
, indirectBindingTable
);
5755 return (strcmp(thisName
, rhsName
) == 0);
5758 template <typename A
>
5759 ld::Atom::Scope NonLazyPointerSection
<A
>::scopeAtAddress(Parser
<A
>& parser
, pint_t addr
)
5761 const macho_section
<P
>* sect
= this->machoSection();
5762 uint32_t symIndex
= parser
.symbolIndexFromIndirectSectionAddress(addr
, sect
);
5763 if ( symIndex
== INDIRECT_SYMBOL_LOCAL
)
5764 return ld::Atom::scopeTranslationUnit
;
5766 return ld::Atom::scopeLinkageUnit
;
5771 template <typename A
>
5772 ld::Atom::Combine TLVPointerSection
<A
>::combine(Parser
<A
>& parser
, pint_t addr
)
5774 return ld::Atom::combineByNameAndReferences
;
5778 void TLVPointerSection
<arm
>::makeFixups(class Parser
<arm
>& parser
, const struct Parser
<arm
>::CFI_CU_InfoArrays
&)
5780 // add references for each thread local pointer atom based on indirect symbol table
5781 const macho_section
<P
>* sect
= this->machoSection();
5782 const pint_t endAddr
= sect
->addr() + sect
->size();
5783 for (pint_t addr
= sect
->addr(); addr
< endAddr
; addr
+= sizeof(pint_t
)) {
5784 typename Parser
<arm
>::SourceLocation src
;
5785 typename Parser
<arm
>::TargetDesc target
;
5786 src
.atom
= this->findAtomByAddress(addr
);
5787 src
.offsetInAtom
= 0;
5788 uint32_t symIndex
= parser
.symbolIndexFromIndirectSectionAddress(addr
, sect
);
5791 target
.weakImport
= false;
5793 if ( symIndex
== INDIRECT_SYMBOL_LOCAL
) {
5794 throwf("unexpected INDIRECT_SYMBOL_LOCAL in section %s", this->sectionName());
5797 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(symIndex
);
5798 // use direct reference for local symbols
5799 if ( ((sym
.n_type() & N_TYPE
) == N_SECT
) && ((sym
.n_type() & N_EXT
) == 0) ) {
5800 throwf("unexpected pointer to local symbol in section %s", this->sectionName());
5803 target
.name
= parser
.nameFromSymbol(sym
);
5804 target
.weakImport
= parser
.weakImportFromSymbol(sym
);
5805 assert(src
.atom
->combine() == ld::Atom::combineByNameAndReferences
);
5808 parser
.addFixups(src
, ld::Fixup::kindStoreLittleEndian32
, target
);
5812 template <typename A
>
5813 void TLVPointerSection
<A
>::makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
&)
5815 assert(0 && "should not have thread-local-pointer sections in .o files");
5819 template <typename A
>
5820 const char* TLVPointerSection
<A
>::targetName(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
, bool* isStatic
)
5822 assert(atom
->combine() == ld::Atom::combineByNameAndReferences
);
5823 assert(atom
->fixupCount() == 1);
5825 ld::Fixup::iterator fit
= atom
->fixupsBegin();
5826 const char* name
= NULL
;
5827 switch ( fit
->binding
) {
5828 case ld::Fixup::bindingByNameUnbound
:
5831 case ld::Fixup::bindingByContentBound
:
5832 name
= fit
->u
.target
->name();
5834 case ld::Fixup::bindingsIndirectlyBound
:
5835 name
= ind
.indirectName(fit
->u
.bindingIndex
);
5837 case ld::Fixup::bindingDirectlyBound
:
5838 name
= fit
->u
.target
->name();
5839 *isStatic
= (fit
->u
.target
->scope() == ld::Atom::scopeTranslationUnit
);
5844 assert(name
!= NULL
);
5848 template <typename A
>
5849 unsigned long TLVPointerSection
<A
>::contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
5851 assert(atom
->combine() == ld::Atom::combineByNameAndReferences
);
5852 unsigned long hash
= 9508;
5854 for (const char* s
= this->targetName(atom
, ind
, &isStatic
); *s
!= '\0'; ++s
) {
5855 hash
= hash
* 33 + *s
;
5860 template <typename A
>
5861 bool TLVPointerSection
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
5862 const ld::IndirectBindingTable
& indirectBindingTable
) const
5864 if ( rhs
.section().type() != ld::Section::typeTLVPointers
)
5866 assert(this->type() == rhs
.section().type());
5867 const Atom
<A
>* rhsAtom
= dynamic_cast<const Atom
<A
>*>(&rhs
);
5868 assert(rhsAtom
!= NULL
);
5871 const char* thisName
= this->targetName(atom
, indirectBindingTable
, &thisIsStatic
);
5872 const char* rhsName
= this->targetName(rhsAtom
, indirectBindingTable
, &rhsIsStatic
);
5873 return !thisIsStatic
&& !rhsIsStatic
&& (strcmp(thisName
, rhsName
) == 0);
5877 template <typename A
>
5878 const uint8_t* CFStringSection
<A
>::targetContent(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
,
5879 ContentType
* ct
, unsigned int* count
)
5881 *ct
= contentUnknown
;
5882 for (ld::Fixup::iterator fit
=atom
->fixupsBegin(), end
=atom
->fixupsEnd(); fit
!= end
; ++fit
) {
5883 const ld::Atom
* targetAtom
= NULL
;
5884 switch ( fit
->binding
) {
5885 case ld::Fixup::bindingByNameUnbound
:
5886 // ignore reference to ___CFConstantStringClassReference
5887 // we are just looking for reference to backing string data
5888 assert(fit
->offsetInAtom
== 0);
5889 assert(strcmp(fit
->u
.name
, "___CFConstantStringClassReference") == 0);
5891 case ld::Fixup::bindingDirectlyBound
:
5892 case ld::Fixup::bindingByContentBound
:
5893 targetAtom
= fit
->u
.target
;
5895 case ld::Fixup::bindingsIndirectlyBound
:
5896 targetAtom
= ind
.indirectAtom(fit
->u
.bindingIndex
);
5899 assert(0 && "bad binding type");
5901 assert(targetAtom
!= NULL
);
5902 const Atom
<A
>* target
= dynamic_cast<const Atom
<A
>*>(targetAtom
);
5903 if ( targetAtom
->section().type() == ld::Section::typeCString
) {
5905 *count
= targetAtom
->size();
5907 else if ( targetAtom
->section().type() == ld::Section::typeUTF16Strings
) {
5909 *count
= (targetAtom
->size()+1)/2; // round up incase of buggy compiler that has only one trailing zero byte
5912 *ct
= contentUnknown
;
5916 return target
->contentPointer();
5922 template <typename A
>
5923 unsigned long CFStringSection
<A
>::contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
5925 // base hash of CFString on hash of cstring it wraps
5928 unsigned int charCount
;
5929 const uint8_t* content
= this->targetContent(atom
, ind
, &cType
, &charCount
);
5933 for (const char* s
= (char*)content
; *s
!= '\0'; ++s
) {
5934 hash
= hash
* 33 + *s
;
5939 --charCount
; // don't add last 0x0000 to hash because some buggy compilers only have trailing single byte
5940 for (const uint16_t* s
= (uint16_t*)content
; charCount
> 0; ++s
, --charCount
) {
5941 hash
= hash
* 1025 + *s
;
5944 case contentUnknown
:
5945 // <rdar://problem/14134211> For malformed CFStrings, hash to address of atom so they have unique hashes
5946 return ULONG_MAX
- (unsigned long)(atom
);
5952 template <typename A
>
5953 bool CFStringSection
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
5954 const ld::IndirectBindingTable
& indirectBindingTable
) const
5958 if ( rhs
.section().type() != ld::Section::typeCFString
)
5960 assert(this->type() == rhs
.section().type());
5961 assert(strcmp(this->sectionName(), "__cfstring") == 0);
5963 ContentType thisType
;
5964 unsigned int charCount
;
5965 const uint8_t* cstringContent
= this->targetContent(atom
, indirectBindingTable
, &thisType
, &charCount
);
5966 ContentType rhsType
;
5967 const Atom
<A
>* rhsAtom
= dynamic_cast<const Atom
<A
>*>(&rhs
);
5968 assert(rhsAtom
!= NULL
);
5969 unsigned int rhsCharCount
;
5970 const uint8_t* rhsStringContent
= this->targetContent(rhsAtom
, indirectBindingTable
, &rhsType
, &rhsCharCount
);
5972 if ( thisType
!= rhsType
)
5975 if ( thisType
== contentUnknown
)
5978 if ( rhsType
== contentUnknown
)
5981 // no need to compare content of pointers are already the same
5982 if ( cstringContent
== rhsStringContent
)
5985 // no need to compare content if size is different
5986 if ( charCount
!= rhsCharCount
)
5989 switch ( thisType
) {
5991 return (strcmp((char*)cstringContent
, (char*)rhsStringContent
) == 0);
5994 const uint16_t* cstringContent16
= (uint16_t*)cstringContent
;
5995 const uint16_t* rhsStringContent16
= (uint16_t*)rhsStringContent
;
5996 for (unsigned int i
= 0; i
< charCount
; ++i
) {
5997 if ( cstringContent16
[i
] != rhsStringContent16
[i
] )
6002 case contentUnknown
:
6009 template <typename A
>
6010 typename
A::P::uint_t ObjC1ClassSection
<A
>::elementSizeAtAddress(pint_t addr
)
6012 // nominal size for each class is 48 bytes, but sometimes the compiler
6013 // over aligns and there is padding after class data
6014 const macho_section
<P
>* sct
= this->machoSection();
6015 uint32_t align
= 1 << sct
->align();
6016 uint32_t size
= ((12 * sizeof(pint_t
)) + align
-1) & (-align
);
6020 template <typename A
>
6021 const char* ObjC1ClassSection
<A
>::unlabeledAtomName(Parser
<A
>& parser
, pint_t addr
)
6023 // 8-bytes into class object is pointer to class name
6024 const macho_section
<P
>* sct
= this->machoSection();
6025 uint32_t classObjcFileOffset
= sct
->offset() - sct
->addr() + addr
;
6026 const uint8_t* mappedFileContent
= this->file().fileContent();
6027 pint_t nameAddr
= P::getP(*((pint_t
*)(mappedFileContent
+classObjcFileOffset
+2*sizeof(pint_t
))));
6029 // find section containing string address to get string bytes
6030 const macho_section
<P
>* const sections
= parser
.firstMachOSection();
6031 const uint32_t sectionCount
= parser
.machOSectionCount();
6032 for (uint32_t i
=0; i
< sectionCount
; ++i
) {
6033 const macho_section
<P
>* aSect
= §ions
[i
];
6034 if ( (aSect
->addr() <= nameAddr
) && (nameAddr
< (aSect
->addr()+aSect
->size())) ) {
6035 assert((aSect
->flags() & SECTION_TYPE
) == S_CSTRING_LITERALS
);
6036 uint32_t nameFileOffset
= aSect
->offset() - aSect
->addr() + nameAddr
;
6037 const char* name
= (char*)mappedFileContent
+ nameFileOffset
;
6038 // spin through symbol table to find absolute symbol corresponding to this class
6039 for (uint32_t s
=0; s
< parser
.symbolCount(); ++s
) {
6040 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(s
);
6041 if ( (sym
.n_type() & N_TYPE
) != N_ABS
)
6043 const char* absName
= parser
.nameFromSymbol(sym
);
6044 if ( strncmp(absName
, ".objc_class_name_", 17) == 0 ) {
6045 if ( strcmp(&absName
[17], name
) == 0 )
6049 assert(0 && "obj class name not found in symbol table");
6052 assert(0 && "obj class name not found");
6053 return "unknown objc class";
6057 template <typename A
>
6058 const char* ObjC2ClassRefsSection
<A
>::targetClassName(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
6060 assert(atom
->fixupCount() == 1);
6061 ld::Fixup::iterator fit
= atom
->fixupsBegin();
6062 const char* className
= NULL
;
6063 switch ( fit
->binding
) {
6064 case ld::Fixup::bindingByNameUnbound
:
6065 className
= fit
->u
.name
;
6067 case ld::Fixup::bindingDirectlyBound
:
6068 case ld::Fixup::bindingByContentBound
:
6069 className
= fit
->u
.target
->name();
6071 case ld::Fixup::bindingsIndirectlyBound
:
6072 className
= ind
.indirectName(fit
->u
.bindingIndex
);
6075 assert(0 && "unsupported binding in objc2 class ref section");
6077 assert(className
!= NULL
);
6082 template <typename A
>
6083 unsigned long ObjC2ClassRefsSection
<A
>::contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
6085 unsigned long hash
= 978;
6086 for (const char* s
= targetClassName(atom
, ind
); *s
!= '\0'; ++s
) {
6087 hash
= hash
* 33 + *s
;
6092 template <typename A
>
6093 bool ObjC2ClassRefsSection
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
6094 const ld::IndirectBindingTable
& indirectBindingTable
) const
6096 assert(this->type() == rhs
.section().type());
6097 const Atom
<A
>* rhsAtom
= dynamic_cast<const Atom
<A
>*>(&rhs
);
6098 assert(rhsAtom
!= NULL
);
6099 const char* thisClassName
= targetClassName(atom
, indirectBindingTable
);
6100 const char* rhsClassName
= targetClassName(rhsAtom
, indirectBindingTable
);
6101 return (strcmp(thisClassName
, rhsClassName
) == 0);
6105 template <typename A
>
6106 const char* Objc1ClassReferences
<A
>::targetCString(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
6108 assert(atom
->fixupCount() == 2);
6109 ld::Fixup::iterator fit
= atom
->fixupsBegin();
6110 if ( fit
->kind
== ld::Fixup::kindSetTargetAddress
)
6112 const ld::Atom
* targetAtom
= NULL
;
6113 switch ( fit
->binding
) {
6114 case ld::Fixup::bindingByContentBound
:
6115 targetAtom
= fit
->u
.target
;
6117 case ld::Fixup::bindingsIndirectlyBound
:
6118 targetAtom
= ind
.indirectAtom(fit
->u
.bindingIndex
);
6119 if ( targetAtom
== NULL
) {
6120 fprintf(stderr
, "missing target named %s\n", ind
.indirectName(fit
->u
.bindingIndex
));
6126 assert(targetAtom
!= NULL
);
6127 const Atom
<A
>* target
= dynamic_cast<const Atom
<A
>*>(targetAtom
);
6128 assert(target
!= NULL
);
6129 return (char*)target
->contentPointer();
6133 template <typename A
>
6134 const char* PointerToCStringSection
<A
>::targetCString(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
6136 assert(atom
->fixupCount() == 1);
6137 ld::Fixup::iterator fit
= atom
->fixupsBegin();
6138 const ld::Atom
* targetAtom
= NULL
;
6139 switch ( fit
->binding
) {
6140 case ld::Fixup::bindingByContentBound
:
6141 targetAtom
= fit
->u
.target
;
6143 case ld::Fixup::bindingsIndirectlyBound
:
6144 targetAtom
= ind
.indirectAtom(fit
->u
.bindingIndex
);
6146 case ld::Fixup::bindingDirectlyBound
:
6147 targetAtom
= fit
->u
.target
;
6150 assert(0 && "unsupported reference to selector");
6152 assert(targetAtom
!= NULL
);
6153 const Atom
<A
>* target
= dynamic_cast<const Atom
<A
>*>(targetAtom
);
6154 assert(target
!= NULL
);
6155 assert(target
->contentType() == ld::Atom::typeCString
);
6156 return (char*)target
->contentPointer();
6159 template <typename A
>
6160 unsigned long PointerToCStringSection
<A
>::contentHash(const class Atom
<A
>* atom
,
6161 const ld::IndirectBindingTable
& indirectBindingTable
) const
6163 // make hash from section name and target cstring name
6164 unsigned long hash
= 123;
6165 for (const char* s
= this->sectionName(); *s
!= '\0'; ++s
) {
6166 hash
= hash
* 33 + *s
;
6168 for (const char* s
= this->targetCString(atom
, indirectBindingTable
); *s
!= '\0'; ++s
) {
6169 hash
= hash
* 33 + *s
;
6174 template <typename A
>
6175 bool PointerToCStringSection
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
6176 const ld::IndirectBindingTable
& indirectBindingTable
) const
6178 assert(this->type() == rhs
.section().type());
6179 // there can be pointers-to-cstrings in different section names
6180 // we only want to coalesce in same section name
6181 if ( *this != rhs
.section() )
6184 // get string content for this
6185 const char* cstringContent
= this->targetCString(atom
, indirectBindingTable
);
6186 const Atom
<A
>* rhsAtom
= dynamic_cast<const Atom
<A
>*>(&rhs
);
6187 assert(rhsAtom
!= NULL
);
6188 const char* rhsCstringContent
= this->targetCString(rhsAtom
, indirectBindingTable
);
6190 assert(cstringContent
!= NULL
);
6191 assert(rhsCstringContent
!= NULL
);
6192 return (strcmp(cstringContent
, rhsCstringContent
) == 0);
6197 template <typename A
>
6198 unsigned long UTF16StringSection
<A
>::contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
6200 unsigned long hash
= 5381;
6201 const uint16_t* stringContent
= (uint16_t*)atom
->contentPointer();
6202 // some buggy compilers end utf16 data with single byte, so don't use last word in hash computation
6203 unsigned int count
= (atom
->size()/2) - 1;
6204 for (const uint16_t* s
= stringContent
; count
> 0; ++s
, --count
) {
6205 hash
= hash
* 33 + *s
;
6210 template <typename A
>
6211 bool UTF16StringSection
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
6212 const ld::IndirectBindingTable
& ind
) const
6214 if ( rhs
.section().type() != ld::Section::typeUTF16Strings
)
6227 uint32_t Section
<x86_64
>::x86_64PcRelOffset(uint8_t r_type
)
6230 case X86_64_RELOC_SIGNED
:
6232 case X86_64_RELOC_SIGNED_1
:
6234 case X86_64_RELOC_SIGNED_2
:
6236 case X86_64_RELOC_SIGNED_4
:
6244 bool Section
<x86_64
>::addRelocFixup(class Parser
<x86_64
>& parser
, const macho_relocation_info
<P
>* reloc
)
6246 const macho_section
<P
>* sect
= this->machoSection();
6247 if ( sect
== NULL
) {
6248 warning("malformed mach-o, relocations not supported on section %s", this->sectionName());
6251 uint64_t srcAddr
= sect
->addr() + reloc
->r_address();
6252 Parser
<x86_64
>::SourceLocation src
;
6253 Parser
<x86_64
>::TargetDesc target
;
6254 Parser
<x86_64
>::TargetDesc toTarget
;
6255 src
.atom
= this->findAtomByAddress(srcAddr
);
6256 src
.offsetInAtom
= srcAddr
- src
.atom
->_objAddress
;
6257 const uint8_t* fixUpPtr
= file().fileContent() + sect
->offset() + reloc
->r_address();
6258 uint64_t contentValue
= 0;
6259 const macho_relocation_info
<x86_64::P
>* nextReloc
= &reloc
[1];
6260 bool result
= false;
6261 bool useDirectBinding
;
6262 switch ( reloc
->r_length() ) {
6264 contentValue
= *fixUpPtr
;
6267 contentValue
= (int64_t)(int16_t)E::get16(*((uint16_t*)fixUpPtr
));
6270 contentValue
= (int64_t)(int32_t)E::get32(*((uint32_t*)fixUpPtr
));
6273 contentValue
= E::get64(*((uint64_t*)fixUpPtr
));
6278 target
.weakImport
= false;
6280 if ( reloc
->r_extern() ) {
6281 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(reloc
->r_symbolnum());
6282 // use direct reference for local symbols
6283 if ( ((sym
.n_type() & N_TYPE
) == N_SECT
) && (((sym
.n_type() & N_EXT
) == 0) || (parser
.nameFromSymbol(sym
)[0] == 'L')) ) {
6284 parser
.findTargetFromAddressAndSectionNum(sym
.n_value(), sym
.n_sect(), target
);
6285 target
.addend
+= contentValue
;
6288 target
.name
= parser
.nameFromSymbol(sym
);
6289 target
.weakImport
= parser
.weakImportFromSymbol(sym
);
6290 target
.addend
= contentValue
;
6292 // cfstrings should always use direct reference to backing store
6293 if ( (this->type() == ld::Section::typeCFString
) && (src
.offsetInAtom
!= 0) ) {
6294 parser
.findTargetFromAddressAndSectionNum(sym
.n_value(), sym
.n_sect(), target
);
6295 target
.addend
= contentValue
;
6299 if ( reloc
->r_pcrel() )
6300 contentValue
+= srcAddr
+ x86_64PcRelOffset(reloc
->r_type());
6301 parser
.findTargetFromAddressAndSectionNum(contentValue
, reloc
->r_symbolnum(), target
);
6303 switch ( reloc
->r_type() ) {
6304 case X86_64_RELOC_UNSIGNED
:
6305 if ( reloc
->r_pcrel() )
6306 throw "pcrel and X86_64_RELOC_UNSIGNED not supported";
6307 switch ( reloc
->r_length() ) {
6310 throw "length < 2 and X86_64_RELOC_UNSIGNED not supported";
6312 parser
.addFixups(src
, ld::Fixup::kindStoreLittleEndian32
, target
);
6315 parser
.addFixups(src
, ld::Fixup::kindStoreLittleEndian64
, target
);
6319 case X86_64_RELOC_SIGNED
:
6320 case X86_64_RELOC_SIGNED_1
:
6321 case X86_64_RELOC_SIGNED_2
:
6322 case X86_64_RELOC_SIGNED_4
:
6323 if ( ! reloc
->r_pcrel() )
6324 throw "not pcrel and X86_64_RELOC_SIGNED* not supported";
6325 if ( reloc
->r_length() != 2 )
6326 throw "length != 2 and X86_64_RELOC_SIGNED* not supported";
6327 switch ( reloc
->r_type() ) {
6328 case X86_64_RELOC_SIGNED
:
6329 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel32
, target
);
6331 case X86_64_RELOC_SIGNED_1
:
6332 if ( reloc
->r_extern() )
6334 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel32_1
, target
);
6336 case X86_64_RELOC_SIGNED_2
:
6337 if ( reloc
->r_extern() )
6339 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel32_2
, target
);
6341 case X86_64_RELOC_SIGNED_4
:
6342 if ( reloc
->r_extern() )
6344 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel32_4
, target
);
6348 case X86_64_RELOC_BRANCH
:
6349 if ( ! reloc
->r_pcrel() )
6350 throw "not pcrel and X86_64_RELOC_BRANCH not supported";
6351 switch ( reloc
->r_length() ) {
6353 if ( (target
.name
!= NULL
) && (strncmp(target
.name
, "___dtrace_probe$", 16) == 0) ) {
6354 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindStoreX86DtraceCallSiteNop
, false, target
.name
);
6355 parser
.addDtraceExtraInfos(src
, &target
.name
[16]);
6357 else if ( (target
.name
!= NULL
) && (strncmp(target
.name
, "___dtrace_isenabled$", 20) == 0) ) {
6358 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindStoreX86DtraceIsEnableSiteClear
, false, target
.name
);
6359 parser
.addDtraceExtraInfos(src
, &target
.name
[20]);
6362 parser
.addFixups(src
, ld::Fixup::kindStoreX86BranchPCRel32
, target
);
6366 parser
.addFixups(src
, ld::Fixup::kindStoreX86BranchPCRel8
, target
);
6369 throwf("length=%d and X86_64_RELOC_BRANCH not supported", reloc
->r_length());
6372 case X86_64_RELOC_GOT
:
6373 if ( ! reloc
->r_extern() )
6374 throw "not extern and X86_64_RELOC_GOT not supported";
6375 if ( ! reloc
->r_pcrel() )
6376 throw "not pcrel and X86_64_RELOC_GOT not supported";
6377 if ( reloc
->r_length() != 2 )
6378 throw "length != 2 and X86_64_RELOC_GOT not supported";
6379 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel32GOT
, target
);
6381 case X86_64_RELOC_GOT_LOAD
:
6382 if ( ! reloc
->r_extern() )
6383 throw "not extern and X86_64_RELOC_GOT_LOAD not supported";
6384 if ( ! reloc
->r_pcrel() )
6385 throw "not pcrel and X86_64_RELOC_GOT_LOAD not supported";
6386 if ( reloc
->r_length() != 2 )
6387 throw "length != 2 and X86_64_RELOC_GOT_LOAD not supported";
6388 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel32GOTLoad
, target
);
6390 case X86_64_RELOC_SUBTRACTOR
:
6391 if ( reloc
->r_pcrel() )
6392 throw "X86_64_RELOC_SUBTRACTOR cannot be pc-relative";
6393 if ( reloc
->r_length() < 2 )
6394 throw "X86_64_RELOC_SUBTRACTOR must have r_length of 2 or 3";
6395 if ( !reloc
->r_extern() )
6396 throw "X86_64_RELOC_SUBTRACTOR must have r_extern=1";
6397 if ( nextReloc
->r_type() != X86_64_RELOC_UNSIGNED
)
6398 throw "X86_64_RELOC_SUBTRACTOR must be followed by X86_64_RELOC_UNSIGNED";
6400 if ( nextReloc
->r_pcrel() )
6401 throw "X86_64_RELOC_UNSIGNED following a X86_64_RELOC_SUBTRACTOR cannot be pc-relative";
6402 if ( nextReloc
->r_length() != reloc
->r_length() )
6403 throw "X86_64_RELOC_UNSIGNED following a X86_64_RELOC_SUBTRACTOR must have same r_length";
6404 if ( nextReloc
->r_extern() ) {
6405 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(nextReloc
->r_symbolnum());
6406 // use direct reference for local symbols
6407 if ( ((sym
.n_type() & N_TYPE
) == N_SECT
) && (((sym
.n_type() & N_EXT
) == 0) || (parser
.nameFromSymbol(sym
)[0] == 'L')) ) {
6408 parser
.findTargetFromAddressAndSectionNum(sym
.n_value(), sym
.n_sect(), toTarget
);
6409 toTarget
.addend
= contentValue
;
6410 useDirectBinding
= true;
6413 toTarget
.name
= parser
.nameFromSymbol(sym
);
6414 toTarget
.weakImport
= parser
.weakImportFromSymbol(sym
);
6415 toTarget
.addend
= contentValue
;
6416 useDirectBinding
= false;
6420 parser
.findTargetFromAddressAndSectionNum(contentValue
, nextReloc
->r_symbolnum(), toTarget
);
6421 useDirectBinding
= (toTarget
.atom
->scope() == ld::Atom::scopeTranslationUnit
) || ((toTarget
.atom
->combine() == ld::Atom::combineByNameAndContent
) || (toTarget
.atom
->combine() == ld::Atom::combineByNameAndReferences
));
6423 if ( useDirectBinding
) {
6424 if ( (toTarget
.atom
->combine() == ld::Atom::combineByNameAndContent
) || (toTarget
.atom
->combine() == ld::Atom::combineByNameAndReferences
) )
6425 parser
.addFixup(src
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, toTarget
.atom
);
6427 parser
.addFixup(src
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, toTarget
.atom
);
6430 parser
.addFixup(src
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, toTarget
.weakImport
, toTarget
.name
);
6431 parser
.addFixup(src
, ld::Fixup::k2of4
, ld::Fixup::kindAddAddend
, toTarget
.addend
);
6432 if ( target
.atom
== NULL
)
6433 parser
.addFixup(src
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractTargetAddress
, false, target
.name
);
6435 parser
.addFixup(src
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractTargetAddress
, target
.atom
);
6436 if ( reloc
->r_length() == 2 )
6437 parser
.addFixup(src
, ld::Fixup::k4of4
, ld::Fixup::kindStoreLittleEndian32
);
6439 parser
.addFixup(src
, ld::Fixup::k4of4
, ld::Fixup::kindStoreLittleEndian64
);
6441 case X86_64_RELOC_TLV
:
6442 if ( ! reloc
->r_extern() )
6443 throw "not extern and X86_64_RELOC_TLV not supported";
6444 if ( ! reloc
->r_pcrel() )
6445 throw "not pcrel and X86_64_RELOC_TLV not supported";
6446 if ( reloc
->r_length() != 2 )
6447 throw "length != 2 and X86_64_RELOC_TLV not supported";
6448 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel32TLVLoad
, target
);
6451 throwf("unknown relocation type %d", reloc
->r_type());
6459 bool Section
<x86
>::addRelocFixup(class Parser
<x86
>& parser
, const macho_relocation_info
<P
>* reloc
)
6461 const macho_section
<P
>* sect
= this->machoSection();
6463 const uint8_t* fixUpPtr
;
6464 uint32_t contentValue
= 0;
6465 ld::Fixup::Kind kind
= ld::Fixup::kindNone
;
6466 Parser
<x86
>::SourceLocation src
;
6467 Parser
<x86
>::TargetDesc target
;
6469 if ( (reloc
->r_address() & R_SCATTERED
) == 0 ) {
6470 srcAddr
= sect
->addr() + reloc
->r_address();
6471 src
.atom
= this->findAtomByAddress(srcAddr
);
6472 src
.offsetInAtom
= srcAddr
- src
.atom
->_objAddress
;
6473 fixUpPtr
= file().fileContent() + sect
->offset() + reloc
->r_address();
6474 switch ( reloc
->r_type() ) {
6475 case GENERIC_RELOC_VANILLA
:
6476 switch ( reloc
->r_length() ) {
6478 contentValue
= (int32_t)(int8_t)*fixUpPtr
;
6479 if ( reloc
->r_pcrel() ) {
6480 kind
= ld::Fixup::kindStoreX86BranchPCRel8
;
6481 contentValue
+= srcAddr
+ sizeof(uint8_t);
6484 throw "r_length=0 and r_pcrel=0 not supported";
6487 contentValue
= (int32_t)(int16_t)E::get16(*((uint16_t*)fixUpPtr
));
6488 if ( reloc
->r_pcrel() ) {
6489 kind
= ld::Fixup::kindStoreX86PCRel16
;
6490 contentValue
+= srcAddr
+ sizeof(uint16_t);
6493 kind
= ld::Fixup::kindStoreLittleEndian16
;
6496 contentValue
= E::get32(*((uint32_t*)fixUpPtr
));
6497 if ( reloc
->r_pcrel() ) {
6498 kind
= ld::Fixup::kindStoreX86BranchPCRel32
;
6499 contentValue
+= srcAddr
+ sizeof(uint32_t);
6502 kind
= ld::Fixup::kindStoreLittleEndian32
;
6505 throw "r_length=3 not supported";
6507 if ( reloc
->r_extern() ) {
6509 const macho_nlist
<P
>& targetSymbol
= parser
.symbolFromIndex(reloc
->r_symbolnum());
6510 target
.name
= parser
.nameFromSymbol(targetSymbol
);
6511 target
.weakImport
= parser
.weakImportFromSymbol(targetSymbol
);
6512 target
.addend
= (int32_t)contentValue
;
6515 parser
.findTargetFromAddressAndSectionNum(contentValue
, reloc
->r_symbolnum(), target
);
6517 if ( (kind
== ld::Fixup::kindStoreX86BranchPCRel32
) && (target
.name
!= NULL
) ) {
6518 if ( strncmp(target
.name
, "___dtrace_probe$", 16) == 0 ) {
6519 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindStoreX86DtraceCallSiteNop
, false, target
.name
);
6520 parser
.addDtraceExtraInfos(src
, &target
.name
[16]);
6523 else if ( strncmp(target
.name
, "___dtrace_isenabled$", 20) == 0 ) {
6524 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindStoreX86DtraceIsEnableSiteClear
, false, target
.name
);
6525 parser
.addDtraceExtraInfos(src
, &target
.name
[20]);
6529 parser
.addFixups(src
, kind
, target
);
6532 case GENERIC_RLEOC_TLV
:
6534 if ( !reloc
->r_extern() )
6535 throw "r_extern=0 and r_type=GENERIC_RLEOC_TLV not supported";
6536 if ( reloc
->r_length() != 2 )
6537 throw "r_length!=2 and r_type=GENERIC_RLEOC_TLV not supported";
6538 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(reloc
->r_symbolnum());
6539 // use direct reference for local symbols
6540 if ( ((sym
.n_type() & N_TYPE
) == N_SECT
) && ((sym
.n_type() & N_EXT
) == 0) ) {
6541 parser
.findTargetFromAddressAndSectionNum(sym
.n_value(), sym
.n_sect(), target
);
6545 target
.name
= parser
.nameFromSymbol(sym
);
6546 target
.weakImport
= parser
.weakImportFromSymbol(sym
);
6548 target
.addend
= (int64_t)(int32_t)E::get32(*((uint32_t*)fixUpPtr
));
6549 if ( reloc
->r_pcrel() ) {
6550 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel32TLVLoad
, target
);
6553 parser
.addFixups(src
, ld::Fixup::kindStoreX86Abs32TLVLoad
, target
);
6559 throwf("unsupported i386 relocation type (%d)", reloc
->r_type());
6563 // scattered relocation
6564 const macho_scattered_relocation_info
<P
>* sreloc
= (macho_scattered_relocation_info
<P
>*)reloc
;
6565 srcAddr
= sect
->addr() + sreloc
->r_address();
6566 src
.atom
= this->findAtomByAddress(srcAddr
);
6567 assert(src
.atom
!= NULL
);
6568 src
.offsetInAtom
= srcAddr
- src
.atom
->_objAddress
;
6569 fixUpPtr
= file().fileContent() + sect
->offset() + sreloc
->r_address();
6570 uint32_t relocValue
= sreloc
->r_value();
6571 bool result
= false;
6572 // file format allows pair to be scattered or not
6573 const macho_scattered_relocation_info
<P
>* nextSReloc
= &sreloc
[1];
6574 const macho_relocation_info
<P
>* nextReloc
= &reloc
[1];
6575 bool nextRelocIsPair
= false;
6576 uint32_t nextRelocAddress
= 0;
6577 uint32_t nextRelocValue
= 0;
6578 if ( (nextReloc
->r_address() & R_SCATTERED
) == 0 ) {
6579 if ( nextReloc
->r_type() == GENERIC_RELOC_PAIR
) {
6580 nextRelocIsPair
= true;
6581 nextRelocAddress
= nextReloc
->r_address();
6582 result
= true; // iterator should skip next reloc, since we've consumed it here
6586 if ( nextSReloc
->r_type() == GENERIC_RELOC_PAIR
) {
6587 nextRelocIsPair
= true;
6588 nextRelocAddress
= nextSReloc
->r_address();
6589 nextRelocValue
= nextSReloc
->r_value();
6592 switch (sreloc
->r_type()) {
6593 case GENERIC_RELOC_VANILLA
:
6594 // with a scattered relocation we get both the target (sreloc->r_value()) and the target+offset (*fixUpPtr)
6595 target
.atom
= parser
.findAtomByAddress(relocValue
);
6596 if ( sreloc
->r_pcrel() ) {
6597 switch ( sreloc
->r_length() ) {
6599 contentValue
= srcAddr
+ 1 + *fixUpPtr
;
6600 target
.addend
= (int32_t)contentValue
- (int32_t)relocValue
;
6601 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel8
, target
);
6604 contentValue
= srcAddr
+ 2 + LittleEndian::get16(*((uint16_t*)fixUpPtr
));
6605 target
.addend
= (int32_t)contentValue
- (int32_t)relocValue
;
6606 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel16
, target
);
6609 contentValue
= srcAddr
+ 4 + LittleEndian::get32(*((uint32_t*)fixUpPtr
));
6610 target
.addend
= (int32_t)contentValue
- (int32_t)relocValue
;
6611 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel32
, target
);
6614 throw "unsupported r_length=3 for scattered pc-rel vanilla reloc";
6619 if ( sreloc
->r_length() != 2 )
6620 throwf("unsupported r_length=%d for scattered vanilla reloc", sreloc
->r_length());
6621 contentValue
= LittleEndian::get32(*((uint32_t*)fixUpPtr
));
6622 target
.addend
= (int32_t)contentValue
- (int32_t)(target
.atom
->objectAddress());
6623 parser
.addFixups(src
, ld::Fixup::kindStoreLittleEndian32
, target
);
6626 case GENERIC_RELOC_SECTDIFF
:
6627 case GENERIC_RELOC_LOCAL_SECTDIFF
:
6629 if ( !nextRelocIsPair
)
6630 throw "GENERIC_RELOC_SECTDIFF missing following pair";
6631 switch ( sreloc
->r_length() ) {
6634 throw "bad length for GENERIC_RELOC_SECTDIFF";
6636 contentValue
= (int32_t)(int16_t)LittleEndian::get16(*((uint16_t*)fixUpPtr
));
6637 kind
= ld::Fixup::kindStoreLittleEndian16
;
6640 contentValue
= LittleEndian::get32(*((uint32_t*)fixUpPtr
));
6641 kind
= ld::Fixup::kindStoreLittleEndian32
;
6644 Atom
<x86
>* fromAtom
= parser
.findAtomByAddress(nextRelocValue
);
6645 uint32_t offsetInFrom
= nextRelocValue
- fromAtom
->_objAddress
;
6646 parser
.findTargetFromAddress(sreloc
->r_value(), target
);
6647 // check for addend encoded in the section content
6648 int64_t addend
= (int32_t)contentValue
- (int32_t)(sreloc
->r_value() - nextRelocValue
);
6650 // switch binding base on coalescing
6651 if ( target
.atom
== NULL
) {
6652 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, false, target
.name
);
6654 else if ( target
.atom
->scope() == ld::Atom::scopeTranslationUnit
) {
6655 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, target
.atom
);
6657 else if ( (target
.atom
->combine() == ld::Atom::combineByNameAndContent
) || (target
.atom
->combine() == ld::Atom::combineByNameAndReferences
) ) {
6658 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, target
.atom
);
6661 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, false, target
.atom
->name());
6663 parser
.addFixup(src
, ld::Fixup::k2of5
, ld::Fixup::kindAddAddend
, target
.addend
);
6664 parser
.addFixup(src
, ld::Fixup::k3of5
, ld::Fixup::kindSubtractTargetAddress
, fromAtom
);
6665 parser
.addFixup(src
, ld::Fixup::k4of5
, ld::Fixup::kindSubtractAddend
, offsetInFrom
-addend
);
6666 parser
.addFixup(src
, ld::Fixup::k5of5
, kind
);
6669 // switch binding base on coalescing
6670 if ( target
.atom
== NULL
) {
6671 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, false, target
.name
);
6673 else if ( target
.atom
->scope() == ld::Atom::scopeTranslationUnit
) {
6674 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, target
.atom
);
6676 else if ( (target
.atom
->combine() == ld::Atom::combineByNameAndContent
) || (target
.atom
->combine() == ld::Atom::combineByNameAndReferences
) ) {
6677 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, target
.atom
);
6680 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, false, target
.atom
->name());
6682 parser
.addFixup(src
, ld::Fixup::k2of5
, ld::Fixup::kindAddAddend
, target
.addend
+addend
);
6683 parser
.addFixup(src
, ld::Fixup::k3of5
, ld::Fixup::kindSubtractTargetAddress
, fromAtom
);
6684 parser
.addFixup(src
, ld::Fixup::k4of5
, ld::Fixup::kindSubtractAddend
, offsetInFrom
);
6685 parser
.addFixup(src
, ld::Fixup::k5of5
, kind
);
6698 #if SUPPORT_ARCH_arm_any
6700 bool Section
<arm
>::addRelocFixup(class Parser
<arm
>& parser
, const macho_relocation_info
<P
>* reloc
)
6702 const macho_section
<P
>* sect
= this->machoSection();
6703 bool result
= false;
6707 int32_t displacement
= 0;
6708 uint32_t instruction
= 0;
6709 pint_t contentValue
= 0;
6710 Parser
<arm
>::SourceLocation src
;
6711 Parser
<arm
>::TargetDesc target
;
6712 const macho_relocation_info
<P
>* nextReloc
;
6714 if ( (reloc
->r_address() & R_SCATTERED
) == 0 ) {
6715 bool externSymbolIsThumbDef
= false;
6716 srcAddr
= sect
->addr() + reloc
->r_address();
6717 src
.atom
= this->findAtomByAddress(srcAddr
);
6718 src
.offsetInAtom
= srcAddr
- src
.atom
->_objAddress
;
6719 fixUpPtr
= (uint32_t*)(file().fileContent() + sect
->offset() + reloc
->r_address());
6720 if ( reloc
->r_type() != ARM_RELOC_PAIR
)
6721 instruction
= LittleEndian::get32(*fixUpPtr
);
6722 if ( reloc
->r_extern() ) {
6723 const macho_nlist
<P
>& targetSymbol
= parser
.symbolFromIndex(reloc
->r_symbolnum());
6724 // use direct reference for local symbols
6725 if ( ((targetSymbol
.n_type() & N_TYPE
) == N_SECT
) && (((targetSymbol
.n_type() & N_EXT
) == 0) || (parser
.nameFromSymbol(targetSymbol
)[0] == 'L')) ) {
6726 parser
.findTargetFromAddressAndSectionNum(targetSymbol
.n_value(), targetSymbol
.n_sect(), target
);
6730 target
.name
= parser
.nameFromSymbol(targetSymbol
);
6731 target
.weakImport
= parser
.weakImportFromSymbol(targetSymbol
);
6732 if ( ((targetSymbol
.n_type() & N_TYPE
) == N_SECT
) && (targetSymbol
.n_desc() & N_ARM_THUMB_DEF
) )
6733 externSymbolIsThumbDef
= true;
6736 switch ( reloc
->r_type() ) {
6737 case ARM_RELOC_BR24
:
6738 // Sign-extend displacement
6739 displacement
= (instruction
& 0x00FFFFFF) << 2;
6740 if ( (displacement
& 0x02000000) != 0 )
6741 displacement
|= 0xFC000000;
6742 // The pc added will be +8 from the pc
6744 // If this is BLX add H << 1
6745 if ((instruction
& 0xFE000000) == 0xFA000000)
6746 displacement
+= ((instruction
& 0x01000000) >> 23);
6747 if ( reloc
->r_extern() ) {
6748 dstAddr
= srcAddr
+ displacement
;
6749 // <rdar://problem/16652542> support large .o files
6750 if ( srcAddr
> 0x2000000 ) {
6751 dstAddr
-= ((srcAddr
+ 0x1FFFFFF) & 0xFC000000);
6753 target
.addend
= dstAddr
;
6754 if ( externSymbolIsThumbDef
)
6755 target
.addend
&= -2; // remove thumb bit
6758 dstAddr
= srcAddr
+ displacement
;
6759 parser
.findTargetFromAddressAndSectionNum(dstAddr
, reloc
->r_symbolnum(), target
);
6761 // special case "calls" for dtrace
6762 if ( (target
.name
!= NULL
) && (strncmp(target
.name
, "___dtrace_probe$", 16) == 0) ) {
6763 parser
.addFixup(src
, ld::Fixup::k1of1
,
6764 ld::Fixup::kindStoreARMDtraceCallSiteNop
, false, target
.name
);
6765 parser
.addDtraceExtraInfos(src
, &target
.name
[16]);
6767 else if ( (target
.name
!= NULL
) && (strncmp(target
.name
, "___dtrace_isenabled$", 20) == 0) ) {
6768 parser
.addFixup(src
, ld::Fixup::k1of1
,
6769 ld::Fixup::kindStoreARMDtraceIsEnableSiteClear
, false, target
.name
);
6770 parser
.addDtraceExtraInfos(src
, &target
.name
[20]);
6773 parser
.addFixups(src
, ld::Fixup::kindStoreARMBranch24
, target
);
6776 case ARM_THUMB_RELOC_BR22
:
6777 // thumb2 added two more bits to displacement, complicating the displacement decoding
6779 uint32_t s
= (instruction
>> 10) & 0x1;
6780 uint32_t j1
= (instruction
>> 29) & 0x1;
6781 uint32_t j2
= (instruction
>> 27) & 0x1;
6782 uint32_t imm10
= instruction
& 0x3FF;
6783 uint32_t imm11
= (instruction
>> 16) & 0x7FF;
6784 uint32_t i1
= (j1
== s
);
6785 uint32_t i2
= (j2
== s
);
6786 uint32_t dis
= (s
<< 24) | (i1
<< 23) | (i2
<< 22) | (imm10
<< 12) | (imm11
<< 1);
6790 displacement
= sdis
;
6792 // The pc added will be +4 from the pc
6794 // If the instruction was blx, force the low 2 bits to be clear
6795 dstAddr
= srcAddr
+ displacement
;
6796 if ((instruction
& 0xD0000000) == 0xC0000000)
6797 dstAddr
&= 0xFFFFFFFC;
6799 if ( reloc
->r_extern() ) {
6800 // <rdar://problem/16652542> support large .o files
6801 if ( srcAddr
> 0x1000000 ) {
6802 dstAddr
-= ((srcAddr
+ 0xFFFFFF) & 0xFE000000);
6804 target
.addend
= (int64_t)(int32_t)dstAddr
;
6807 parser
.findTargetFromAddressAndSectionNum(dstAddr
, reloc
->r_symbolnum(), target
);
6809 // special case "calls" for dtrace
6810 if ( (target
.name
!= NULL
) && (strncmp(target
.name
, "___dtrace_probe$", 16) == 0) ) {
6811 parser
.addFixup(src
, ld::Fixup::k1of1
,
6812 ld::Fixup::kindStoreThumbDtraceCallSiteNop
, false, target
.name
);
6813 parser
.addDtraceExtraInfos(src
, &target
.name
[16]);
6815 else if ( (target
.name
!= NULL
) && (strncmp(target
.name
, "___dtrace_isenabled$", 20) == 0) ) {
6816 parser
.addFixup(src
, ld::Fixup::k1of1
,
6817 ld::Fixup::kindStoreThumbDtraceIsEnableSiteClear
, false, target
.name
);
6818 parser
.addDtraceExtraInfos(src
, &target
.name
[20]);
6821 parser
.addFixups(src
, ld::Fixup::kindStoreThumbBranch22
, target
);
6824 case ARM_RELOC_VANILLA
:
6825 if ( reloc
->r_length() != 2 )
6826 throw "bad length for ARM_RELOC_VANILLA";
6827 contentValue
= LittleEndian::get32(*fixUpPtr
);
6828 if ( reloc
->r_extern() ) {
6829 target
.addend
= (int32_t)contentValue
;
6830 if ( externSymbolIsThumbDef
)
6831 target
.addend
&= -2; // remove thumb bit
6834 parser
.findTargetFromAddressAndSectionNum(contentValue
, reloc
->r_symbolnum(), target
);
6835 // possible non-extern relocation turned into by-name ref because target is a weak-def
6836 if ( target
.atom
!= NULL
) {
6837 if ( target
.atom
->isThumb() )
6838 target
.addend
&= -2; // remove thumb bit
6839 // if reference to LSDA, add group subordinate fixup
6840 if ( target
.atom
->contentType() == ld::Atom::typeLSDA
) {
6841 Parser
<arm
>::SourceLocation src2
;
6842 src2
.atom
= src
.atom
;
6843 src2
.offsetInAtom
= 0;
6844 parser
.addFixup(src2
, ld::Fixup::k1of1
, ld::Fixup::kindNoneGroupSubordinateLSDA
, target
.atom
);
6848 parser
.addFixups(src
, ld::Fixup::kindStoreLittleEndian32
, target
);
6850 case ARM_THUMB_32BIT_BRANCH
:
6851 // silently ignore old unnecessary reloc
6853 case ARM_RELOC_HALF
:
6854 nextReloc
= &reloc
[1];
6855 if ( nextReloc
->r_type() == ARM_RELOC_PAIR
) {
6856 uint32_t instruction16
;
6857 uint32_t other16
= (nextReloc
->r_address() & 0xFFFF);
6859 if ( reloc
->r_length() & 2 ) {
6861 uint32_t i
= ((instruction
& 0x00000400) >> 10);
6862 uint32_t imm4
= (instruction
& 0x0000000F);
6863 uint32_t imm3
= ((instruction
& 0x70000000) >> 28);
6864 uint32_t imm8
= ((instruction
& 0x00FF0000) >> 16);
6865 instruction16
= (imm4
<< 12) | (i
<< 11) | (imm3
<< 8) | imm8
;
6869 uint32_t imm4
= ((instruction
& 0x000F0000) >> 16);
6870 uint32_t imm12
= (instruction
& 0x00000FFF);
6871 instruction16
= (imm4
<< 12) | imm12
;
6873 if ( reloc
->r_length() & 1 ) {
6875 dstAddr
= ((instruction16
<< 16) | other16
);
6876 if ( reloc
->r_extern() ) {
6877 target
.addend
= dstAddr
;
6878 if ( externSymbolIsThumbDef
)
6879 target
.addend
&= -2; // remove thumb bit
6882 parser
.findTargetFromAddress(dstAddr
, target
);
6883 if ( target
.atom
->isThumb() )
6884 target
.addend
&= (-2); // remove thumb bit
6886 parser
.addFixups(src
, (isThumb
? ld::Fixup::kindStoreThumbHigh16
: ld::Fixup::kindStoreARMHigh16
), target
);
6890 dstAddr
= (other16
<< 16) | instruction16
;
6891 if ( reloc
->r_extern() ) {
6892 target
.addend
= dstAddr
;
6893 if ( externSymbolIsThumbDef
)
6894 target
.addend
&= -2; // remove thumb bit
6897 parser
.findTargetFromAddress(dstAddr
, target
);
6898 if ( target
.atom
->isThumb() )
6899 target
.addend
&= (-2); // remove thumb bit
6901 parser
.addFixups(src
, (isThumb
? ld::Fixup::kindStoreThumbLow16
: ld::Fixup::kindStoreARMLow16
), target
);
6906 throw "for ARM_RELOC_HALF, next reloc is not ARM_RELOC_PAIR";
6909 throwf("unknown relocation type %d", reloc
->r_type());
6914 const macho_scattered_relocation_info
<P
>* sreloc
= (macho_scattered_relocation_info
<P
>*)reloc
;
6915 // file format allows pair to be scattered or not
6916 const macho_scattered_relocation_info
<P
>* nextSReloc
= &sreloc
[1];
6917 nextReloc
= &reloc
[1];
6918 srcAddr
= sect
->addr() + sreloc
->r_address();
6919 dstAddr
= sreloc
->r_value();
6920 fixUpPtr
= (uint32_t*)(file().fileContent() + sect
->offset() + sreloc
->r_address());
6921 instruction
= LittleEndian::get32(*fixUpPtr
);
6922 src
.atom
= this->findAtomByAddress(srcAddr
);
6923 src
.offsetInAtom
= srcAddr
- src
.atom
->_objAddress
;
6924 bool nextRelocIsPair
= false;
6925 uint32_t nextRelocAddress
= 0;
6926 uint32_t nextRelocValue
= 0;
6927 if ( (nextReloc
->r_address() & R_SCATTERED
) == 0 ) {
6928 if ( nextReloc
->r_type() == ARM_RELOC_PAIR
) {
6929 nextRelocIsPair
= true;
6930 nextRelocAddress
= nextReloc
->r_address();
6935 if ( nextSReloc
->r_type() == ARM_RELOC_PAIR
) {
6936 nextRelocIsPair
= true;
6937 nextRelocAddress
= nextSReloc
->r_address();
6938 nextRelocValue
= nextSReloc
->r_value();
6942 switch ( sreloc
->r_type() ) {
6943 case ARM_RELOC_VANILLA
:
6944 // with a scattered relocation we get both the target (sreloc->r_value()) and the target+offset (*fixUpPtr)
6945 if ( sreloc
->r_length() != 2 )
6946 throw "bad length for ARM_RELOC_VANILLA";
6947 target
.atom
= parser
.findAtomByAddress(sreloc
->r_value());
6948 if ( target
.atom
== NULL
)
6949 throwf("bad r_value (0x%08X) for ARM_RELOC_VANILLA\n", sreloc
->r_value());
6950 contentValue
= LittleEndian::get32(*fixUpPtr
);
6951 target
.addend
= contentValue
- target
.atom
->_objAddress
;
6952 if ( target
.atom
->isThumb() )
6953 target
.addend
&= -2; // remove thumb bit
6954 parser
.addFixups(src
, ld::Fixup::kindStoreLittleEndian32
, target
);
6956 case ARM_RELOC_BR24
:
6957 // Sign-extend displacement
6958 displacement
= (instruction
& 0x00FFFFFF) << 2;
6959 if ( (displacement
& 0x02000000) != 0 )
6960 displacement
|= 0xFC000000;
6961 // The pc added will be +8 from the pc
6963 // If this is BLX add H << 1
6964 if ((instruction
& 0xFE000000) == 0xFA000000)
6965 displacement
+= ((instruction
& 0x01000000) >> 23);
6966 target
.atom
= parser
.findAtomByAddress(sreloc
->r_value());
6967 target
.addend
= (int64_t)(srcAddr
+ displacement
) - (int64_t)(target
.atom
->_objAddress
);
6968 parser
.addFixups(src
, ld::Fixup::kindStoreARMBranch24
, target
);
6970 case ARM_THUMB_RELOC_BR22
:
6971 // thumb2 added two more bits to displacement, complicating the displacement decoding
6973 uint32_t s
= (instruction
>> 10) & 0x1;
6974 uint32_t j1
= (instruction
>> 29) & 0x1;
6975 uint32_t j2
= (instruction
>> 27) & 0x1;
6976 uint32_t imm10
= instruction
& 0x3FF;
6977 uint32_t imm11
= (instruction
>> 16) & 0x7FF;
6978 uint32_t i1
= (j1
== s
);
6979 uint32_t i2
= (j2
== s
);
6980 uint32_t dis
= (s
<< 24) | (i1
<< 23) | (i2
<< 22) | (imm10
<< 12) | (imm11
<< 1);
6984 displacement
= sdis
;
6986 // The pc added will be +4 from the pc
6988 dstAddr
= srcAddr
+displacement
;
6989 // If the instruction was blx, force the low 2 bits to be clear
6990 if ((instruction
& 0xF8000000) == 0xE8000000)
6991 dstAddr
&= 0xFFFFFFFC;
6992 target
.atom
= parser
.findAtomByAddress(sreloc
->r_value());
6993 target
.addend
= dstAddr
- target
.atom
->_objAddress
;
6994 parser
.addFixups(src
, ld::Fixup::kindStoreThumbBranch22
, target
);
6996 case ARM_RELOC_SECTDIFF
:
6997 case ARM_RELOC_LOCAL_SECTDIFF
:
6999 if ( ! nextRelocIsPair
)
7000 throw "ARM_RELOC_SECTDIFF missing following pair";
7001 if ( sreloc
->r_length() != 2 )
7002 throw "bad length for ARM_RELOC_SECTDIFF";
7003 contentValue
= LittleEndian::get32(*fixUpPtr
);
7004 Atom
<arm
>* fromAtom
= parser
.findAtomByAddress(nextRelocValue
);
7005 uint32_t offsetInFrom
= nextRelocValue
- fromAtom
->_objAddress
;
7006 uint32_t offsetInTarget
;
7007 Atom
<arm
>* targetAtom
= parser
.findAtomByAddressOrLocalTargetOfStub(sreloc
->r_value(), &offsetInTarget
);
7008 // check for addend encoded in the section content
7009 int64_t addend
= (int32_t)contentValue
- (int32_t)(sreloc
->r_value() - nextRelocValue
);
7010 if ( targetAtom
->isThumb() )
7011 addend
&= -2; // remove thumb bit
7012 // if reference to LSDA, add group subordinate fixup
7013 if ( targetAtom
->contentType() == ld::Atom::typeLSDA
) {
7014 Parser
<arm
>::SourceLocation src2
;
7015 src2
.atom
= src
.atom
;
7016 src2
.offsetInAtom
= 0;
7017 parser
.addFixup(src2
, ld::Fixup::k1of1
, ld::Fixup::kindNoneGroupSubordinateLSDA
, targetAtom
);
7020 // switch binding base on coalescing
7021 if ( targetAtom
->scope() == ld::Atom::scopeTranslationUnit
) {
7022 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, targetAtom
);
7024 else if ( (targetAtom
->combine() == ld::Atom::combineByNameAndContent
) || (targetAtom
->combine() == ld::Atom::combineByNameAndReferences
) ) {
7025 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, targetAtom
);
7028 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, false, targetAtom
->name());
7030 parser
.addFixup(src
, ld::Fixup::k2of5
, ld::Fixup::kindAddAddend
, offsetInTarget
);
7031 parser
.addFixup(src
, ld::Fixup::k3of5
, ld::Fixup::kindSubtractTargetAddress
, fromAtom
);
7032 parser
.addFixup(src
, ld::Fixup::k4of5
, ld::Fixup::kindSubtractAddend
, offsetInFrom
-addend
);
7033 parser
.addFixup(src
, ld::Fixup::k5of5
, ld::Fixup::kindStoreLittleEndian32
);
7036 if ( targetAtom
->scope() == ld::Atom::scopeTranslationUnit
) {
7037 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, targetAtom
);
7039 else if ( (targetAtom
->combine() == ld::Atom::combineByNameAndContent
) || (targetAtom
->combine() == ld::Atom::combineByNameAndReferences
) ) {
7040 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, targetAtom
);
7043 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, false, targetAtom
->name());
7045 parser
.addFixup(src
, ld::Fixup::k2of5
, ld::Fixup::kindAddAddend
, (uint32_t)(offsetInTarget
+addend
));
7046 parser
.addFixup(src
, ld::Fixup::k3of5
, ld::Fixup::kindSubtractTargetAddress
, fromAtom
);
7047 parser
.addFixup(src
, ld::Fixup::k4of5
, ld::Fixup::kindSubtractAddend
, offsetInFrom
);
7048 parser
.addFixup(src
, ld::Fixup::k5of5
, ld::Fixup::kindStoreLittleEndian32
);
7052 case ARM_RELOC_HALF_SECTDIFF
:
7053 if ( nextRelocIsPair
) {
7054 instruction
= LittleEndian::get32(*fixUpPtr
);
7055 Atom
<arm
>* fromAtom
= parser
.findAtomByAddress(nextRelocValue
);
7056 uint32_t offsetInFrom
= nextRelocValue
- fromAtom
->_objAddress
;
7057 Atom
<arm
>* targetAtom
= parser
.findAtomByAddress(sreloc
->r_value());
7058 uint32_t offsetInTarget
= sreloc
->r_value() - targetAtom
->_objAddress
;
7059 uint32_t instruction16
;
7060 uint32_t other16
= (nextRelocAddress
& 0xFFFF);
7062 if ( sreloc
->r_length() & 2 ) {
7064 uint32_t i
= ((instruction
& 0x00000400) >> 10);
7065 uint32_t imm4
= (instruction
& 0x0000000F);
7066 uint32_t imm3
= ((instruction
& 0x70000000) >> 28);
7067 uint32_t imm8
= ((instruction
& 0x00FF0000) >> 16);
7068 instruction16
= (imm4
<< 12) | (i
<< 11) | (imm3
<< 8) | imm8
;
7072 uint32_t imm4
= ((instruction
& 0x000F0000) >> 16);
7073 uint32_t imm12
= (instruction
& 0x00000FFF);
7074 instruction16
= (imm4
<< 12) | imm12
;
7076 if ( sreloc
->r_length() & 1 )
7077 dstAddr
= ((instruction16
<< 16) | other16
);
7079 dstAddr
= (other16
<< 16) | instruction16
;
7080 if ( targetAtom
->isThumb() )
7081 dstAddr
&= (-2); // remove thumb bit
7082 int32_t addend
= dstAddr
- (sreloc
->r_value() - nextRelocValue
);
7083 if ( targetAtom
->scope() == ld::Atom::scopeTranslationUnit
) {
7084 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, targetAtom
);
7086 else if ( (targetAtom
->combine() == ld::Atom::combineByNameAndContent
) || (targetAtom
->combine() == ld::Atom::combineByNameAndReferences
) ) {
7087 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, targetAtom
);
7090 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, false, targetAtom
->name());
7092 parser
.addFixup(src
, ld::Fixup::k2of5
, ld::Fixup::kindAddAddend
, (uint32_t)offsetInTarget
+addend
);
7093 parser
.addFixup(src
, ld::Fixup::k3of5
, ld::Fixup::kindSubtractTargetAddress
, fromAtom
);
7094 parser
.addFixup(src
, ld::Fixup::k4of5
, ld::Fixup::kindSubtractAddend
, offsetInFrom
);
7095 if ( sreloc
->r_length() & 1 ) {
7097 parser
.addFixup(src
, ld::Fixup::k5of5
, (isThumb
? ld::Fixup::kindStoreThumbHigh16
: ld::Fixup::kindStoreARMHigh16
));
7101 parser
.addFixup(src
, ld::Fixup::k5of5
, (isThumb
? ld::Fixup::kindStoreThumbLow16
: ld::Fixup::kindStoreARMLow16
));
7106 throw "ARM_RELOC_HALF_SECTDIFF reloc missing following pair";
7108 case ARM_RELOC_HALF
:
7109 if ( nextRelocIsPair
) {
7110 instruction
= LittleEndian::get32(*fixUpPtr
);
7111 Atom
<arm
>* targetAtom
= parser
.findAtomByAddress(sreloc
->r_value());
7112 uint32_t instruction16
;
7113 uint32_t other16
= (nextRelocAddress
& 0xFFFF);
7115 if ( sreloc
->r_length() & 2 ) {
7117 uint32_t i
= ((instruction
& 0x00000400) >> 10);
7118 uint32_t imm4
= (instruction
& 0x0000000F);
7119 uint32_t imm3
= ((instruction
& 0x70000000) >> 28);
7120 uint32_t imm8
= ((instruction
& 0x00FF0000) >> 16);
7121 instruction16
= (imm4
<< 12) | (i
<< 11) | (imm3
<< 8) | imm8
;
7125 uint32_t imm4
= ((instruction
& 0x000F0000) >> 16);
7126 uint32_t imm12
= (instruction
& 0x00000FFF);
7127 instruction16
= (imm4
<< 12) | imm12
;
7129 if ( sreloc
->r_length() & 1 )
7130 dstAddr
= ((instruction16
<< 16) | other16
);
7132 dstAddr
= (other16
<< 16) | instruction16
;
7133 if ( targetAtom
->scope() == ld::Atom::scopeTranslationUnit
) {
7134 parser
.addFixup(src
, ld::Fixup::k1of3
, ld::Fixup::kindSetTargetAddress
, targetAtom
);
7136 else if ( (targetAtom
->combine() == ld::Atom::combineByNameAndContent
) || (targetAtom
->combine() == ld::Atom::combineByNameAndReferences
) ) {
7137 parser
.addFixup(src
, ld::Fixup::k1of3
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, targetAtom
);
7140 parser
.addFixup(src
, ld::Fixup::k1of3
, ld::Fixup::kindSetTargetAddress
, false, targetAtom
->name());
7142 parser
.addFixup(src
, ld::Fixup::k2of3
, ld::Fixup::kindAddAddend
, dstAddr
- targetAtom
->_objAddress
);
7143 if ( sreloc
->r_length() & 1 ) {
7145 parser
.addFixup(src
, ld::Fixup::k3of3
, (isThumb
? ld::Fixup::kindStoreThumbHigh16
: ld::Fixup::kindStoreARMHigh16
));
7149 parser
.addFixup(src
, ld::Fixup::k3of3
, (isThumb
? ld::Fixup::kindStoreThumbLow16
: ld::Fixup::kindStoreARMLow16
));
7154 throw "scattered ARM_RELOC_HALF reloc missing following pair";
7157 throwf("unknown ARM scattered relocation type %d", sreloc
->r_type());
7165 #if SUPPORT_ARCH_arm64
7167 bool Section
<arm64
>::addRelocFixup(class Parser
<arm64
>& parser
, const macho_relocation_info
<P
>* reloc
)
7169 bool result
= false;
7170 Parser
<arm64
>::SourceLocation src
;
7171 Parser
<arm64
>::TargetDesc target
= { NULL
, NULL
, false, 0 };
7172 Parser
<arm64
>::TargetDesc toTarget
;
7173 int32_t prefixRelocAddend
= 0;
7174 if ( reloc
->r_type() == ARM64_RELOC_ADDEND
) {
7175 uint32_t rawAddend
= reloc
->r_symbolnum();
7176 prefixRelocAddend
= rawAddend
;
7177 if ( rawAddend
& 0x00800000 )
7178 prefixRelocAddend
|= 0xFF000000; // sign extend 24-bit signed int to 32-bits
7179 uint32_t addendAddress
= reloc
->r_address();
7180 ++reloc
; //advance to next reloc record
7182 if ( reloc
->r_address() != addendAddress
)
7183 throw "ARM64_RELOC_ADDEND r_address does not match next reloc's r_address";
7185 const macho_section
<P
>* sect
= this->machoSection();
7186 uint64_t srcAddr
= sect
->addr() + reloc
->r_address();
7187 src
.atom
= this->findAtomByAddress(srcAddr
);
7188 src
.offsetInAtom
= srcAddr
- src
.atom
->_objAddress
;
7189 const uint8_t* fixUpPtr
= file().fileContent() + sect
->offset() + reloc
->r_address();
7190 uint64_t contentValue
= 0;
7191 const macho_relocation_info
<arm64::P
>* nextReloc
= &reloc
[1];
7192 bool useDirectBinding
;
7193 uint32_t instruction
;
7194 uint32_t encodedAddend
;
7195 switch ( reloc
->r_length() ) {
7197 contentValue
= *fixUpPtr
;
7200 contentValue
= (int64_t)(int16_t)E::get16(*((uint16_t*)fixUpPtr
));
7203 contentValue
= (int64_t)(int32_t)E::get32(*((uint32_t*)fixUpPtr
));
7206 contentValue
= E::get64(*((uint64_t*)fixUpPtr
));
7209 if ( reloc
->r_extern() ) {
7210 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(reloc
->r_symbolnum());
7211 const char* symbolName
= parser
.nameFromSymbol(sym
);
7212 if ( ((sym
.n_type() & N_TYPE
) == N_SECT
) && (((sym
.n_type() & N_EXT
) == 0) || (symbolName
[0] == 'L') || (symbolName
[0] == 'l')) ) {
7213 // use direct reference for local symbols
7214 parser
.findTargetFromAddressAndSectionNum(sym
.n_value(), sym
.n_sect(), target
);
7215 //target.addend += contentValue;
7217 else if ( ((sym
.n_type() & N_TYPE
) == N_SECT
) && (src
.atom
->_objAddress
<= sym
.n_value()) && (sym
.n_value() < (src
.atom
->_objAddress
+src
.atom
->size())) ) {
7218 // <rdar://problem/13700961> spurious warning when weak function has reference to itself
7219 // use direct reference when atom targets itself
7220 target
.atom
= src
.atom
;
7224 target
.name
= symbolName
;
7225 target
.weakImport
= parser
.weakImportFromSymbol(sym
);
7226 //target.addend = contentValue;
7228 // cfstrings should always use direct reference to backing store
7229 if ( (this->type() == ld::Section::typeCFString
) && (src
.offsetInAtom
!= 0) ) {
7230 parser
.findTargetFromAddressAndSectionNum(sym
.n_value(), sym
.n_sect(), target
);
7231 //target.addend = contentValue;
7235 if ( reloc
->r_pcrel() )
7236 contentValue
+= srcAddr
;
7237 parser
.findTargetFromAddressAndSectionNum(contentValue
, reloc
->r_symbolnum(), target
);
7239 switch ( reloc
->r_type() ) {
7240 case ARM64_RELOC_UNSIGNED
:
7241 if ( reloc
->r_pcrel() )
7242 throw "pcrel and ARM64_RELOC_UNSIGNED not supported";
7243 if ( reloc
->r_extern() )
7244 target
.addend
= contentValue
;
7245 switch ( reloc
->r_length() ) {
7248 throw "length < 2 and ARM64_RELOC_UNSIGNED not supported";
7250 parser
.addFixups(src
, ld::Fixup::kindStoreLittleEndian32
, target
);
7253 parser
.addFixups(src
, ld::Fixup::kindStoreLittleEndian64
, target
);
7257 case ARM64_RELOC_BRANCH26
:
7258 if ( ! reloc
->r_pcrel() )
7259 throw "not pcrel and ARM64_RELOC_BRANCH26 not supported";
7260 if ( ! reloc
->r_extern() )
7261 throw "r_extern == 0 and ARM64_RELOC_BRANCH26 not supported";
7262 if ( reloc
->r_length() != 2 )
7263 throw "r_length != 2 and ARM64_RELOC_BRANCH26 not supported";
7264 if ( (target
.name
!= NULL
) && (strncmp(target
.name
, "___dtrace_probe$", 16) == 0) ) {
7265 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindStoreARM64DtraceCallSiteNop
, false, target
.name
);
7266 parser
.addDtraceExtraInfos(src
, &target
.name
[16]);
7268 else if ( (target
.name
!= NULL
) && (strncmp(target
.name
, "___dtrace_isenabled$", 20) == 0) ) {
7269 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindStoreARM64DtraceIsEnableSiteClear
, false, target
.name
);
7270 parser
.addDtraceExtraInfos(src
, &target
.name
[20]);
7273 target
.addend
= prefixRelocAddend
;
7274 instruction
= contentValue
;
7275 encodedAddend
= (instruction
& 0x03FFFFFF) << 2;
7276 if ( encodedAddend
!= 0 ) {
7277 if ( prefixRelocAddend
== 0 ) {
7278 warning("branch26 instruction at 0x%08X has embedded addend. ARM64_RELOC_ADDEND should be used instead", reloc
->r_address());
7279 target
.addend
= encodedAddend
;
7282 throwf("branch26 instruction at 0x%08X has embedded addend and ARM64_RELOC_ADDEND also used", reloc
->r_address());
7285 parser
.addFixups(src
, ld::Fixup::kindStoreARM64Branch26
, target
);
7288 case ARM64_RELOC_PAGE21
:
7289 if ( ! reloc
->r_pcrel() )
7290 throw "not pcrel and ARM64_RELOC_PAGE21 not supported";
7291 if ( ! reloc
->r_extern() )
7292 throw "r_extern == 0 and ARM64_RELOC_PAGE21 not supported";
7293 if ( reloc
->r_length() != 2 )
7294 throw "length != 2 and ARM64_RELOC_PAGE21 not supported";
7295 target
.addend
= prefixRelocAddend
;
7296 instruction
= contentValue
;
7297 encodedAddend
= ((instruction
& 0x60000000) >> 29) | ((instruction
& 0x01FFFFE0) >> 3);
7298 encodedAddend
*= 4096; // internally addend is in bytes, so scale
7299 if ( encodedAddend
!= 0 ) {
7300 if ( prefixRelocAddend
== 0 ) {
7301 warning("adrp instruction at 0x%08X has embedded addend. ARM64_RELOC_ADDEND should be used instead", reloc
->r_address());
7302 target
.addend
= encodedAddend
;
7305 throwf("adrp instruction at 0x%08X has embedded addend and ARM64_RELOC_ADDEND also used", reloc
->r_address());
7308 parser
.addFixups(src
, ld::Fixup::kindStoreARM64Page21
, target
);
7310 case ARM64_RELOC_PAGEOFF12
:
7311 if ( reloc
->r_pcrel() )
7312 throw "pcrel and ARM64_RELOC_PAGEOFF12 not supported";
7313 if ( ! reloc
->r_extern() )
7314 throw "r_extern == 0 and ARM64_RELOC_PAGEOFF12 not supported";
7315 if ( reloc
->r_length() != 2 )
7316 throw "length != 2 and ARM64_RELOC_PAGEOFF12 not supported";
7317 target
.addend
= prefixRelocAddend
;
7318 instruction
= contentValue
;
7319 encodedAddend
= ((instruction
& 0x003FFC00) >> 10);
7320 // internally addend is in bytes. Some instructions have an implicit scale factor
7321 if ( (instruction
& 0x3B000000) == 0x39000000 ) {
7322 switch ( instruction
& 0xC0000000 ) {
7336 if ( encodedAddend
!= 0 ) {
7337 if ( prefixRelocAddend
== 0 ) {
7338 warning("pageoff12 instruction at 0x%08X has embedded addend. ARM64_RELOC_ADDEND should be used instead", reloc
->r_address());
7339 target
.addend
= encodedAddend
;
7342 throwf("pageoff12 instruction at 0x%08X has embedded addend and ARM64_RELOC_ADDEND also used", reloc
->r_address());
7345 parser
.addFixups(src
, ld::Fixup::kindStoreARM64PageOff12
, target
);
7347 case ARM64_RELOC_GOT_LOAD_PAGE21
:
7348 if ( ! reloc
->r_pcrel() )
7349 throw "not pcrel and ARM64_RELOC_GOT_LOAD_PAGE21 not supported";
7350 if ( ! reloc
->r_extern() )
7351 throw "r_extern == 0 and ARM64_RELOC_GOT_LOAD_PAGE21 not supported";
7352 if ( reloc
->r_length() != 2 )
7353 throw "length != 2 and ARM64_RELOC_GOT_LOAD_PAGE21 not supported";
7354 if ( prefixRelocAddend
!= 0 )
7355 throw "ARM64_RELOC_ADDEND followed by ARM64_RELOC_GOT_LOAD_PAGE21 not supported";
7356 instruction
= contentValue
;
7357 target
.addend
= ((instruction
& 0x60000000) >> 29) | ((instruction
& 0x01FFFFE0) >> 3);
7358 if ( target
.addend
!= 0 )
7359 throw "non-zero addend with ARM64_RELOC_GOT_LOAD_PAGE21 is not supported";
7360 parser
.addFixups(src
, ld::Fixup::kindStoreARM64GOTLoadPage21
, target
);
7362 case ARM64_RELOC_GOT_LOAD_PAGEOFF12
:
7363 if ( reloc
->r_pcrel() )
7364 throw "pcrel and ARM64_RELOC_GOT_LOAD_PAGEOFF12 not supported";
7365 if ( ! reloc
->r_extern() )
7366 throw "r_extern == 0 and ARM64_RELOC_GOT_LOAD_PAGEOFF12 not supported";
7367 if ( reloc
->r_length() != 2 )
7368 throw "length != 2 and ARM64_RELOC_GOT_LOAD_PAGEOFF12 not supported";
7369 if ( prefixRelocAddend
!= 0 )
7370 throw "ARM64_RELOC_ADDEND followed by ARM64_RELOC_GOT_LOAD_PAGEOFF12 not supported";
7371 instruction
= contentValue
;
7372 target
.addend
= ((instruction
& 0x003FFC00) >> 10);
7373 parser
.addFixups(src
, ld::Fixup::kindStoreARM64GOTLoadPageOff12
, target
);
7375 case ARM64_RELOC_TLVP_LOAD_PAGE21
:
7376 if ( ! reloc
->r_pcrel() )
7377 throw "not pcrel and ARM64_RELOC_TLVP_LOAD_PAGE21 not supported";
7378 if ( ! reloc
->r_extern() )
7379 throw "r_extern == 0 and ARM64_RELOC_TLVP_LOAD_PAGE21 not supported";
7380 if ( reloc
->r_length() != 2 )
7381 throw "length != 2 and ARM64_RELOC_TLVP_LOAD_PAGE21 not supported";
7382 if ( prefixRelocAddend
!= 0 )
7383 throw "ARM64_RELOC_ADDEND followed by ARM64_RELOC_TLVP_LOAD_PAGE21 not supported";
7384 instruction
= contentValue
;
7385 target
.addend
= ((instruction
& 0x60000000) >> 29) | ((instruction
& 0x01FFFFE0) >> 3);
7386 if ( target
.addend
!= 0 )
7387 throw "non-zero addend with ARM64_RELOC_GOT_LOAD_PAGE21 is not supported";
7388 parser
.addFixups(src
, ld::Fixup::kindStoreARM64TLVPLoadPage21
, target
);
7390 case ARM64_RELOC_TLVP_LOAD_PAGEOFF12
:
7391 if ( reloc
->r_pcrel() )
7392 throw "pcrel and ARM64_RELOC_TLVP_LOAD_PAGEOFF12 not supported";
7393 if ( ! reloc
->r_extern() )
7394 throw "r_extern == 0 and ARM64_RELOC_TLVP_LOAD_PAGEOFF12 not supported";
7395 if ( reloc
->r_length() != 2 )
7396 throw "length != 2 and ARM64_RELOC_TLVP_LOAD_PAGEOFF12 not supported";
7397 if ( prefixRelocAddend
!= 0 )
7398 throw "ARM64_RELOC_ADDEND followed by ARM64_RELOC_TLVP_LOAD_PAGEOFF12 not supported";
7399 instruction
= contentValue
;
7400 target
.addend
= ((instruction
& 0x003FFC00) >> 10);
7401 parser
.addFixups(src
, ld::Fixup::kindStoreARM64TLVPLoadPageOff12
, target
);
7403 case ARM64_RELOC_SUBTRACTOR
:
7404 if ( reloc
->r_pcrel() )
7405 throw "ARM64_RELOC_SUBTRACTOR cannot be pc-relative";
7406 if ( reloc
->r_length() < 2 )
7407 throw "ARM64_RELOC_SUBTRACTOR must have r_length of 2 or 3";
7408 if ( !reloc
->r_extern() )
7409 throw "ARM64_RELOC_SUBTRACTOR must have r_extern=1";
7410 if ( nextReloc
->r_type() != ARM64_RELOC_UNSIGNED
)
7411 throw "ARM64_RELOC_SUBTRACTOR must be followed by ARM64_RELOC_UNSIGNED";
7412 if ( prefixRelocAddend
!= 0 )
7413 throw "ARM64_RELOC_ADDEND followed by ARM64_RELOC_SUBTRACTOR not supported";
7415 if ( nextReloc
->r_pcrel() )
7416 throw "ARM64_RELOC_UNSIGNED following a ARM64_RELOC_SUBTRACTOR cannot be pc-relative";
7417 if ( nextReloc
->r_length() != reloc
->r_length() )
7418 throw "ARM64_RELOC_UNSIGNED following a ARM64_RELOC_SUBTRACTOR must have same r_length";
7419 if ( nextReloc
->r_extern() ) {
7420 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(nextReloc
->r_symbolnum());
7421 // use direct reference for local symbols
7422 if ( ((sym
.n_type() & N_TYPE
) == N_SECT
) && (((sym
.n_type() & N_EXT
) == 0) || (parser
.nameFromSymbol(sym
)[0] == 'L')) ) {
7423 parser
.findTargetFromAddressAndSectionNum(sym
.n_value(), sym
.n_sect(), toTarget
);
7424 toTarget
.addend
= contentValue
;
7425 useDirectBinding
= true;
7428 toTarget
.name
= parser
.nameFromSymbol(sym
);
7429 toTarget
.weakImport
= parser
.weakImportFromSymbol(sym
);
7430 toTarget
.addend
= contentValue
;
7431 useDirectBinding
= false;
7435 parser
.findTargetFromAddressAndSectionNum(contentValue
, nextReloc
->r_symbolnum(), toTarget
);
7436 useDirectBinding
= (toTarget
.atom
->scope() == ld::Atom::scopeTranslationUnit
);
7438 if ( useDirectBinding
)
7439 parser
.addFixup(src
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, toTarget
.atom
);
7441 parser
.addFixup(src
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, toTarget
.weakImport
, toTarget
.name
);
7442 parser
.addFixup(src
, ld::Fixup::k2of4
, ld::Fixup::kindAddAddend
, toTarget
.addend
);
7443 if ( target
.atom
== NULL
)
7444 parser
.addFixup(src
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractTargetAddress
, false, target
.name
);
7446 parser
.addFixup(src
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractTargetAddress
, target
.atom
);
7447 if ( reloc
->r_length() == 2 )
7448 parser
.addFixup(src
, ld::Fixup::k4of4
, ld::Fixup::kindStoreLittleEndian32
);
7450 parser
.addFixup(src
, ld::Fixup::k4of4
, ld::Fixup::kindStoreLittleEndian64
);
7452 case ARM64_RELOC_POINTER_TO_GOT
:
7453 if ( ! reloc
->r_extern() )
7454 throw "r_extern == 0 and ARM64_RELOC_POINTER_TO_GOT not supported";
7455 if ( prefixRelocAddend
!= 0 )
7456 throw "ARM64_RELOC_ADDEND followed by ARM64_RELOC_POINTER_TO_GOT not supported";
7457 if ( reloc
->r_pcrel() ) {
7458 if ( reloc
->r_length() != 2 )
7459 throw "r_length != 2 and r_extern = 1 and ARM64_RELOC_POINTER_TO_GOT not supported";
7460 parser
.addFixups(src
, ld::Fixup::kindStoreARM64PCRelToGOT
, target
);
7463 if ( reloc
->r_length() != 3 )
7464 throw "r_length != 3 and r_extern = 0 and ARM64_RELOC_POINTER_TO_GOT not supported";
7465 parser
.addFixups(src
, ld::Fixup::kindStoreARM64PointerToGOT
, target
);
7469 throwf("unknown relocation type %d", reloc
->r_type());
7476 template <typename A
>
7477 bool ObjC1ClassSection
<A
>::addRelocFixup(class Parser
<A
>& parser
, const macho_relocation_info
<P
>* reloc
)
7480 FixedSizeSection
<A
>::addRelocFixup(parser
, reloc
);
7482 assert(0 && "needs template specialization");
7487 bool ObjC1ClassSection
<x86
>::addRelocFixup(class Parser
<x86
>& parser
, const macho_relocation_info
<x86::P
>* reloc
)
7489 // if this is the reloc for the super class name string, add implicit reference to super class
7490 if ( ((reloc
->r_address() & R_SCATTERED
) == 0) && (reloc
->r_type() == GENERIC_RELOC_VANILLA
) ) {
7491 assert( reloc
->r_length() == 2 );
7492 assert( ! reloc
->r_pcrel() );
7494 const macho_section
<P
>* sect
= this->machoSection();
7495 Parser
<x86
>::SourceLocation src
;
7496 uint32_t srcAddr
= sect
->addr() + reloc
->r_address();
7497 src
.atom
= this->findAtomByAddress(srcAddr
);
7498 src
.offsetInAtom
= srcAddr
- src
.atom
->objectAddress();
7499 if ( src
.offsetInAtom
== 4 ) {
7500 Parser
<x86
>::TargetDesc stringTarget
;
7501 const uint8_t* fixUpPtr
= file().fileContent() + sect
->offset() + reloc
->r_address();
7502 uint32_t contentValue
= LittleEndian::get32(*((uint32_t*)fixUpPtr
));
7503 parser
.findTargetFromAddressAndSectionNum(contentValue
, reloc
->r_symbolnum(), stringTarget
);
7505 assert(stringTarget
.atom
!= NULL
);
7506 assert(stringTarget
.atom
->contentType() == ld::Atom::typeCString
);
7507 const char* superClassBaseName
= (char*)stringTarget
.atom
->rawContentPointer();
7508 char* superClassName
= new char[strlen(superClassBaseName
) + 20];
7509 strcpy(superClassName
, ".objc_class_name_");
7510 strcat(superClassName
, superClassBaseName
);
7512 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindSetTargetAddress
, false, superClassName
);
7516 return FixedSizeSection
<x86
>::addRelocFixup(parser
, reloc
);
7521 template <typename A
>
7522 bool Objc1ClassReferences
<A
>::addRelocFixup(class Parser
<A
>& parser
, const macho_relocation_info
<P
>* reloc
)
7525 PointerToCStringSection
<A
>::addRelocFixup(parser
, reloc
);
7527 assert(0 && "needs template specialization");
7534 bool Objc1ClassReferences
<x86
>::addRelocFixup(class Parser
<x86
>& parser
, const macho_relocation_info
<x86::P
>* reloc
)
7536 // add implict class refs, fixups not usable yet, so look at relocations
7537 assert( (reloc
->r_address() & R_SCATTERED
) == 0 );
7538 assert( reloc
->r_type() == GENERIC_RELOC_VANILLA
);
7539 assert( reloc
->r_length() == 2 );
7540 assert( ! reloc
->r_pcrel() );
7542 const macho_section
<P
>* sect
= this->machoSection();
7543 Parser
<x86
>::SourceLocation src
;
7544 uint32_t srcAddr
= sect
->addr() + reloc
->r_address();
7545 src
.atom
= this->findAtomByAddress(srcAddr
);
7546 src
.offsetInAtom
= srcAddr
- src
.atom
->objectAddress();
7547 Parser
<x86
>::TargetDesc stringTarget
;
7548 const uint8_t* fixUpPtr
= file().fileContent() + sect
->offset() + reloc
->r_address();
7549 uint32_t contentValue
= LittleEndian::get32(*((uint32_t*)fixUpPtr
));
7550 parser
.findTargetFromAddressAndSectionNum(contentValue
, reloc
->r_symbolnum(), stringTarget
);
7552 assert(stringTarget
.atom
!= NULL
);
7553 assert(stringTarget
.atom
->contentType() == ld::Atom::typeCString
);
7554 const char* baseClassName
= (char*)stringTarget
.atom
->rawContentPointer();
7555 char* objcClassName
= new char[strlen(baseClassName
) + 20];
7556 strcpy(objcClassName
, ".objc_class_name_");
7557 strcat(objcClassName
, baseClassName
);
7559 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindSetTargetAddress
, false, objcClassName
);
7562 return PointerToCStringSection
<x86
>::addRelocFixup(parser
, reloc
);
7565 #if SUPPORT_ARCH_arm64
7567 void Section
<arm64
>::addLOH(class Parser
<arm64
>& parser
, int kind
, int count
, const uint64_t addrs
[]) {
7569 case LOH_ARM64_ADRP_ADRP
:
7570 case LOH_ARM64_ADRP_LDR
:
7571 case LOH_ARM64_ADRP_ADD
:
7572 case LOH_ARM64_ADRP_LDR_GOT
:
7574 warning("arm64 Linker Optimiztion Hint %d has wrong number of arguments", kind
);
7576 case LOH_ARM64_ADRP_ADD_LDR
:
7577 case LOH_ARM64_ADRP_LDR_GOT_LDR
:
7578 case LOH_ARM64_ADRP_ADD_STR
:
7579 case LOH_ARM64_ADRP_LDR_GOT_STR
:
7581 warning("arm64 Linker Optimiztion Hint %d has wrong number of arguments", kind
);
7584 // pick lowest address in tuple for use as offsetInAtom
7585 uint64_t lowestAddress
= addrs
[0];
7586 for(int i
=1; i
< count
; ++i
) {
7587 if ( addrs
[i
] < lowestAddress
)
7588 lowestAddress
= addrs
[i
];
7590 // verify all other address are in same atom
7591 Atom
<arm64
>* inAtom
= parser
.findAtomByAddress(lowestAddress
);
7592 const uint64_t atomStartAddr
= inAtom
->objectAddress();
7593 const uint64_t atomEndAddr
= atomStartAddr
+ inAtom
->size();
7594 for(int i
=0; i
< count
; ++i
) {
7595 if ( (addrs
[i
] < atomStartAddr
) || (addrs
[i
] >= atomEndAddr
) ) {
7596 warning("arm64 Linker Optimiztion Hint addresses are not in same atom: 0x%08llX and 0x%08llX",
7597 lowestAddress
, addrs
[i
]);
7598 return; // skip this LOH
7600 if ( (addrs
[i
] & 0x3) != 0 ) {
7601 warning("arm64 Linker Optimiztion Hint address is not 4-byte aligned: 0x%08llX", addrs
[i
]);
7602 return; // skip this LOH
7604 if ( (addrs
[i
] - lowestAddress
) > 0xFFFF ) {
7605 if ( parser
.verboseOptimizationHints() ) {
7606 warning("arm64 Linker Optimiztion Hint addresses are too far apart: 0x%08llX and 0x%08llX",
7607 lowestAddress
, addrs
[i
]);
7609 return; // skip this LOH
7613 // encoded kind, count, and address deltas in 64-bit addend
7614 ld::Fixup::LOH_arm64 extra
;
7616 extra
.info
.kind
= kind
;
7617 extra
.info
.count
= count
-1;
7618 extra
.info
.delta1
= (addrs
[0] - lowestAddress
) >> 2;
7619 extra
.info
.delta2
= (count
> 1) ? ((addrs
[1] - lowestAddress
) >> 2) : 0;
7620 extra
.info
.delta3
= (count
> 2) ? ((addrs
[2] - lowestAddress
) >> 2) : 0;
7621 extra
.info
.delta4
= (count
> 3) ? ((addrs
[3] - lowestAddress
) >> 2) : 0;
7622 typename Parser
<arm64
>::SourceLocation
src(inAtom
, lowestAddress
- inAtom
->objectAddress());
7623 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindLinkerOptimizationHint
, extra
.addend
);
7628 template <typename A
>
7629 void Section
<A
>::addLOH(class Parser
<A
>& parser
, int kind
, int count
, const uint64_t addrs
[]) {
7633 template <typename A
>
7634 void Section
<A
>::makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
&)
7636 const macho_section
<P
>* sect
= this->machoSection();
7637 if ( sect
->reloff() + (sect
->nreloc() * sizeof(macho_relocation_info
<P
>)) > parser
.fileLength() )
7638 throwf("relocations for section %s/%s extends beyond end of file,", sect
->segname(), Section
<A
>::makeSectionName(sect
) );
7639 const macho_relocation_info
<P
>* relocs
= (macho_relocation_info
<P
>*)(file().fileContent() + sect
->reloff());
7640 const uint32_t relocCount
= sect
->nreloc();
7641 for (uint32_t r
= 0; r
< relocCount
; ++r
) {
7643 if ( this->addRelocFixup(parser
, &relocs
[r
]) )
7646 catch (const char* msg
) {
7647 throwf("in section %s,%s reloc %u: %s", sect
->segname(), Section
<A
>::makeSectionName(sect
), r
, msg
);
7651 // add follow-on fixups if .o file is missing .subsections_via_symbols
7652 if ( this->addFollowOnFixups() ) {
7653 Atom
<A
>* end
= &_endAtoms
[-1];
7654 for(Atom
<A
>* p
= _beginAtoms
; p
< end
; ++p
) {
7655 typename Parser
<A
>::SourceLocation
src(p
, 0);
7656 Atom
<A
>* nextAtom
= &p
[1];
7657 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindNoneFollowOn
, nextAtom
);
7660 else if ( this->type() == ld::Section::typeCode
) {
7661 // if FDE broke text not at a symbol, use followOn to keep code together
7662 Atom
<A
>* end
= &_endAtoms
[-1];
7663 for(Atom
<A
>* p
= _beginAtoms
; p
< end
; ++p
) {
7664 typename Parser
<A
>::SourceLocation
src(p
, 0);
7665 Atom
<A
>* nextAtom
= &p
[1];
7666 if ( (p
->symbolTableInclusion() == ld::Atom::symbolTableIn
) && (nextAtom
->symbolTableInclusion() == ld::Atom::symbolTableNotIn
) ) {
7667 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindNoneFollowOn
, nextAtom
);
7671 if ( !this->_altEntries
.empty() && !this->addFollowOnFixups() ) {
7672 if ( _altEntries
.count(_beginAtoms
) != 0 )
7673 warning("N_ALT_ENTRY bit set on first atom in section %s/%s", sect
->segname(), Section
<A
>::makeSectionName(sect
));
7675 Atom
<A
>* end
= &_endAtoms
[-1];
7676 for(Atom
<A
>* p
= _beginAtoms
; p
< end
; ++p
) {
7677 Atom
<A
>* nextAtom
= &p
[1];
7678 // <rdar://problem/22960070> support alt_entry aliases (alias process already added followOn, don't repeat)
7679 if ( (_altEntries
.count(nextAtom
) != 0) && (p
->_objAddress
!= nextAtom
->_objAddress
) ) {
7680 typename Parser
<A
>::SourceLocation
src(p
, 0);
7681 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindNoneFollowOn
, nextAtom
);
7682 typename Parser
<A
>::SourceLocation
src2(nextAtom
, 0);
7683 parser
.addFixup(src2
, ld::Fixup::k1of1
, ld::Fixup::kindNoneGroupSubordinate
, p
);
7688 // <rdar://problem/9218847> track data-in-code
7689 if ( parser
.hasDataInCodeLabels() && (this->type() == ld::Section::typeCode
) ) {
7690 for (uint32_t i
=0; i
< parser
.symbolCount(); ++i
) {
7691 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(i
);
7693 if ( (sym
.n_type() & N_STAB
) != 0 )
7695 // ignore non-definitions
7696 if ( (sym
.n_type() & N_TYPE
) != N_SECT
)
7699 // 'L' labels do not denote atom breaks
7700 const char* symbolName
= parser
.nameFromSymbol(sym
);
7701 if ( symbolName
[0] == 'L' ) {
7702 if ( strncmp(symbolName
, "L$start$", 8) == 0 ) {
7703 ld::Fixup::Kind kind
= ld::Fixup::kindNone
;
7704 if ( strncmp(&symbolName
[8], "data$", 5) == 0 )
7705 kind
= ld::Fixup::kindDataInCodeStartData
;
7706 else if ( strncmp(&symbolName
[8], "code$", 5) == 0 )
7707 kind
= ld::Fixup::kindDataInCodeEnd
;
7708 else if ( strncmp(&symbolName
[8], "jt8$", 4) == 0 )
7709 kind
= ld::Fixup::kindDataInCodeStartJT8
;
7710 else if ( strncmp(&symbolName
[8], "jt16$", 4) == 0 )
7711 kind
= ld::Fixup::kindDataInCodeStartJT16
;
7712 else if ( strncmp(&symbolName
[8], "jt32$", 4) == 0 )
7713 kind
= ld::Fixup::kindDataInCodeStartJT32
;
7714 else if ( strncmp(&symbolName
[8], "jta32$", 4) == 0 )
7715 kind
= ld::Fixup::kindDataInCodeStartJTA32
;
7717 warning("unknown L$start$ label %s in file %s", symbolName
, this->file().path());
7718 if ( kind
!= ld::Fixup::kindNone
) {
7719 Atom
<A
>* inAtom
= parser
.findAtomByAddress(sym
.n_value());
7720 typename Parser
<A
>::SourceLocation
src(inAtom
, sym
.n_value() - inAtom
->objectAddress());
7721 parser
.addFixup(src
, ld::Fixup::k1of1
, kind
);
7728 // <rdar://problem/11150575> Handle LC_DATA_IN_CODE in object files
7729 if ( this->type() == ld::Section::typeCode
) {
7730 const pint_t startAddr
= this->_machOSection
->addr();
7731 const pint_t endAddr
= startAddr
+ this->_machOSection
->size();
7732 for ( const macho_data_in_code_entry
<P
>* p
= parser
.dataInCodeStart(); p
!= parser
.dataInCodeEnd(); ++p
) {
7733 if ( (p
->offset() >= startAddr
) && (p
->offset() < endAddr
) ) {
7734 ld::Fixup::Kind kind
= ld::Fixup::kindNone
;
7735 switch ( p
->kind() ) {
7736 case DICE_KIND_DATA
:
7737 kind
= ld::Fixup::kindDataInCodeStartData
;
7739 case DICE_KIND_JUMP_TABLE8
:
7740 kind
= ld::Fixup::kindDataInCodeStartJT8
;
7742 case DICE_KIND_JUMP_TABLE16
:
7743 kind
= ld::Fixup::kindDataInCodeStartJT16
;
7745 case DICE_KIND_JUMP_TABLE32
:
7746 kind
= ld::Fixup::kindDataInCodeStartJT32
;
7748 case DICE_KIND_ABS_JUMP_TABLE32
:
7749 kind
= ld::Fixup::kindDataInCodeStartJTA32
;
7752 kind
= ld::Fixup::kindDataInCodeStartData
;
7753 warning("uknown LC_DATA_IN_CODE kind (%d) at offset 0x%08X", p
->kind(), p
->offset());
7756 Atom
<A
>* inAtom
= parser
.findAtomByAddress(p
->offset());
7757 typename Parser
<A
>::SourceLocation
srcStart(inAtom
, p
->offset() - inAtom
->objectAddress());
7758 parser
.addFixup(srcStart
, ld::Fixup::k1of1
, kind
);
7759 typename Parser
<A
>::SourceLocation
srcEnd(inAtom
, p
->offset() + p
->length() - inAtom
->objectAddress());
7760 parser
.addFixup(srcEnd
, ld::Fixup::k1of1
, ld::Fixup::kindDataInCodeEnd
);
7765 // <rdar://problem/11945700> convert linker optimization hints into internal format
7766 if ( this->type() == ld::Section::typeCode
&& parser
.hasOptimizationHints() ) {
7767 const pint_t startAddr
= this->_machOSection
->addr();
7768 const pint_t endAddr
= startAddr
+ this->_machOSection
->size();
7769 for (const uint8_t* p
= parser
.optimizationHintsStart(); p
< parser
.optimizationHintsEnd(); ) {
7771 int32_t kind
= read_uleb128(&p
, parser
.optimizationHintsEnd());
7772 if ( kind
== 0 ) // padding at end of loh buffer
7775 warning("malformed uleb128 kind in LC_LINKER_OPTIMIZATION_HINTS");
7778 int32_t count
= read_uleb128(&p
, parser
.optimizationHintsEnd());
7779 if ( count
== -1 ) {
7780 warning("malformed uleb128 count in LC_LINKER_OPTIMIZATION_HINTS");
7784 warning("address count > 3 in LC_LINKER_OPTIMIZATION_HINTS");
7787 for (int32_t i
=0; i
< count
; ++i
) {
7788 addrs
[i
] = read_uleb128(&p
, parser
.optimizationHintsEnd());
7790 if ( (startAddr
<= addrs
[0]) && (addrs
[0] < endAddr
) ) {
7791 this->addLOH(parser
, kind
, count
, addrs
);
7792 //fprintf(stderr, "kind=%d", kind);
7793 //for (int32_t i=0; i < count; ++i) {
7794 // fprintf(stderr, ", addr=0x%08llX", addrs[i]);
7796 //fprintf(stderr, "\n");
7802 // add follow-on fixups for aliases
7803 if ( _hasAliases
) {
7804 for(Atom
<A
>* p
= _beginAtoms
; p
< _endAtoms
; ++p
) {
7805 if ( p
->isAlias() && ! this->addFollowOnFixups() ) {
7806 Atom
<A
>* targetOfAlias
= &p
[1];
7807 assert(p
< &_endAtoms
[-1]);
7808 assert(p
->_objAddress
== targetOfAlias
->_objAddress
);
7809 typename Parser
<A
>::SourceLocation
src(p
, 0);
7810 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindNoneFollowOn
, targetOfAlias
);
7819 // main function used by linker to instantiate ld::Files
7821 ld::relocatable::File
* parse(const uint8_t* fileContent
, uint64_t fileLength
,
7822 const char* path
, time_t modTime
, ld::File::Ordinal ordinal
, const ParserOptions
& opts
)
7824 switch ( opts
.architecture
) {
7825 #if SUPPORT_ARCH_x86_64
7826 case CPU_TYPE_X86_64
:
7827 if ( mach_o::relocatable::Parser
<x86_64
>::validFile(fileContent
) )
7828 return mach_o::relocatable::Parser
<x86_64
>::parse(fileContent
, fileLength
, path
, modTime
, ordinal
, opts
);
7831 #if SUPPORT_ARCH_i386
7833 if ( mach_o::relocatable::Parser
<x86
>::validFile(fileContent
) )
7834 return mach_o::relocatable::Parser
<x86
>::parse(fileContent
, fileLength
, path
, modTime
, ordinal
, opts
);
7837 #if SUPPORT_ARCH_arm_any
7839 if ( mach_o::relocatable::Parser
<arm
>::validFile(fileContent
, opts
.objSubtypeMustMatch
, opts
.subType
) )
7840 return mach_o::relocatable::Parser
<arm
>::parse(fileContent
, fileLength
, path
, modTime
, ordinal
, opts
);
7843 #if SUPPORT_ARCH_arm64
7844 case CPU_TYPE_ARM64
:
7845 if ( mach_o::relocatable::Parser
<arm64
>::validFile(fileContent
, opts
.objSubtypeMustMatch
, opts
.subType
) )
7846 return mach_o::relocatable::Parser
<arm64
>::parse(fileContent
, fileLength
, path
, modTime
, ordinal
, opts
);
7854 // used by archive reader to validate member object file
7856 bool isObjectFile(const uint8_t* fileContent
, uint64_t fileLength
, const ParserOptions
& opts
)
7858 switch ( opts
.architecture
) {
7859 case CPU_TYPE_X86_64
:
7860 return ( mach_o::relocatable::Parser
<x86_64
>::validFile(fileContent
) );
7862 return ( mach_o::relocatable::Parser
<x86
>::validFile(fileContent
) );
7864 return ( mach_o::relocatable::Parser
<arm
>::validFile(fileContent
, opts
.objSubtypeMustMatch
, opts
.subType
) );
7865 case CPU_TYPE_ARM64
:
7866 return ( mach_o::relocatable::Parser
<arm64
>::validFile(fileContent
, opts
.objSubtypeMustMatch
, opts
.subType
) );
7872 // used by linker to infer architecture when no -arch is on command line
7874 bool isObjectFile(const uint8_t* fileContent
, cpu_type_t
* result
, cpu_subtype_t
* subResult
, Options::Platform
* platform
)
7876 if ( mach_o::relocatable::Parser
<x86_64
>::validFile(fileContent
) ) {
7877 *result
= CPU_TYPE_X86_64
;
7878 const macho_header
<Pointer64
<LittleEndian
> >* header
= (const macho_header
<Pointer64
<LittleEndian
> >*)fileContent
;
7879 *subResult
= header
->cpusubtype();
7880 *platform
= Parser
<x86_64
>::findPlatform(header
);
7883 if ( mach_o::relocatable::Parser
<x86
>::validFile(fileContent
) ) {
7884 const macho_header
<Pointer32
<LittleEndian
> >* header
= (const macho_header
<Pointer32
<LittleEndian
> >*)fileContent
;
7885 *result
= CPU_TYPE_I386
;
7886 *subResult
= CPU_SUBTYPE_X86_ALL
;
7887 *platform
= Parser
<x86
>::findPlatform(header
);
7890 if ( mach_o::relocatable::Parser
<arm
>::validFile(fileContent
, false, 0) ) {
7891 const macho_header
<Pointer32
<LittleEndian
> >* header
= (const macho_header
<Pointer32
<LittleEndian
> >*)fileContent
;
7892 *result
= CPU_TYPE_ARM
;
7893 *subResult
= header
->cpusubtype();
7894 *platform
= Parser
<arm
>::findPlatform(header
);
7897 if ( mach_o::relocatable::Parser
<arm64
>::validFile(fileContent
, false, 0) ) {
7898 const macho_header
<Pointer64
<LittleEndian
> >* header
= (const macho_header
<Pointer64
<LittleEndian
> >*)fileContent
;
7899 *result
= CPU_TYPE_ARM64
;
7900 *subResult
= CPU_SUBTYPE_ARM64_ALL
;
7901 *platform
= Parser
<arm64
>::findPlatform(header
);
7908 // used by linker is error messages to describe bad .o file
7910 const char* archName(const uint8_t* fileContent
)
7912 if ( mach_o::relocatable::Parser
<x86_64
>::validFile(fileContent
) ) {
7913 return mach_o::relocatable::Parser
<x86_64
>::fileKind(fileContent
);
7915 if ( mach_o::relocatable::Parser
<x86
>::validFile(fileContent
) ) {
7916 return mach_o::relocatable::Parser
<x86
>::fileKind(fileContent
);
7918 if ( mach_o::relocatable::Parser
<arm
>::validFile(fileContent
, false, 0) ) {
7919 return mach_o::relocatable::Parser
<arm
>::fileKind(fileContent
);
7925 // Used by archive reader when -ObjC option is specified
7927 bool hasObjC2Categories(const uint8_t* fileContent
)
7929 if ( mach_o::relocatable::Parser
<x86_64
>::validFile(fileContent
) ) {
7930 return mach_o::relocatable::Parser
<x86_64
>::hasObjC2Categories(fileContent
);
7932 else if ( mach_o::relocatable::Parser
<arm
>::validFile(fileContent
, false, 0) ) {
7933 return mach_o::relocatable::Parser
<arm
>::hasObjC2Categories(fileContent
);
7935 else if ( mach_o::relocatable::Parser
<x86
>::validFile(fileContent
, false, 0) ) {
7936 return mach_o::relocatable::Parser
<x86
>::hasObjC2Categories(fileContent
);
7938 #if SUPPORT_ARCH_arm64
7939 else if ( mach_o::relocatable::Parser
<arm64
>::validFile(fileContent
, false, 0) ) {
7940 return mach_o::relocatable::Parser
<arm64
>::hasObjC2Categories(fileContent
);
7947 // Used by archive reader when -ObjC option is specified
7949 bool hasObjC1Categories(const uint8_t* fileContent
)
7951 if ( mach_o::relocatable::Parser
<x86
>::validFile(fileContent
, false, 0) ) {
7952 return mach_o::relocatable::Parser
<x86
>::hasObjC1Categories(fileContent
);
7958 // Used by bitcode obfuscator to get a list of non local symbols from object file
7960 bool getNonLocalSymbols(const uint8_t* fileContent
, std::vector
<const char*> &syms
)
7962 if ( mach_o::relocatable::Parser
<x86_64
>::validFile(fileContent
) ) {
7963 return mach_o::relocatable::Parser
<x86_64
>::getNonLocalSymbols(fileContent
, syms
);
7965 else if ( mach_o::relocatable::Parser
<arm
>::validFile(fileContent
, false, 0) ) {
7966 return mach_o::relocatable::Parser
<arm
>::getNonLocalSymbols(fileContent
, syms
);
7968 else if ( mach_o::relocatable::Parser
<x86
>::validFile(fileContent
, false, 0) ) {
7969 return mach_o::relocatable::Parser
<x86
>::getNonLocalSymbols(fileContent
, syms
);
7971 else if ( mach_o::relocatable::Parser
<arm64
>::validFile(fileContent
, false, 0) ) {
7972 return mach_o::relocatable::Parser
<arm64
>::getNonLocalSymbols(fileContent
, syms
);
7979 } // namespace relocatable
7980 } // namespace mach_o