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>
49 #include "debugline.h"
51 #include "Architectures.hpp"
52 #include "Bitcode.hpp"
54 #include "macho_relocatable_file.h"
58 extern void throwf(const char* format
, ...) __attribute__ ((noreturn
,format(printf
, 1, 2)));
59 extern void warning(const char* format
, ...) __attribute__((format(printf
, 1, 2)));
62 namespace relocatable
{
66 template <typename A
> class Parser
;
67 template <typename A
> class Atom
;
68 template <typename A
> class Section
;
69 template <typename A
> class CFISection
;
70 template <typename A
> class CUSection
;
73 class File
: public ld::relocatable::File
76 File(const char* p
, time_t mTime
, const uint8_t* content
, ld::File::Ordinal ord
) :
77 ld::relocatable::File(p
,mTime
,ord
), _fileContent(content
),
78 _sectionsArray(NULL
), _atomsArray(NULL
),
79 _sectionsArrayCount(0), _atomsArrayCount(0), _aliasAtomsArrayCount(0),
80 _debugInfoKind(ld::relocatable::File::kDebugInfoNone
),
81 _dwarfTranslationUnitPath(NULL
),
82 _dwarfDebugInfoSect(NULL
), _dwarfDebugAbbrevSect(NULL
),
83 _dwarfDebugLineSect(NULL
), _dwarfDebugStringSect(NULL
),
84 _objConstraint(ld::File::objcConstraintNone
),
89 _canScatterAtoms(false),
90 _objcHasCategoryClassPropertiesField(false),
91 _srcKind(kSourceUnknown
) { }
94 // overrides of ld::File
95 virtual bool forEachAtom(ld::File::AtomHandler
&) const;
96 virtual bool justInTimeforEachAtom(const char* name
, ld::File::AtomHandler
&) const
98 virtual uint32_t minOSVersion() const { return _minOSVersion
; }
99 virtual uint32_t platformLoadCommand() const { return _platform
; }
101 // overrides of ld::relocatable::File
102 virtual ObjcConstraint
objCConstraint() const { return _objConstraint
; }
103 virtual bool objcHasCategoryClassPropertiesField() const
104 { return _objcHasCategoryClassPropertiesField
; }
105 virtual uint32_t cpuSubType() const { return _cpuSubType
; }
106 virtual DebugInfoKind
debugInfo() const { return _debugInfoKind
; }
107 virtual const std::vector
<ld::relocatable::File::Stab
>* stabs() const { return &_stabs
; }
108 virtual bool canScatterAtoms() const { return _canScatterAtoms
; }
109 virtual const char* translationUnitSource() const;
110 virtual LinkerOptionsList
* linkerOptions() const { return &_linkerOptions
; }
111 virtual uint8_t swiftVersion() const { return _swiftVersion
; }
112 virtual ld::Bitcode
* getBitcode() const { return _bitcode
.get(); }
113 virtual SourceKind
sourceKind() const { return _srcKind
; }
115 const uint8_t* fileContent() { return _fileContent
; }
117 friend class Atom
<A
>;
118 friend class Section
<A
>;
119 friend class Parser
<A
>;
120 friend class CFISection
<A
>::OAS
;
122 typedef typename
A::P P
;
124 const uint8_t* _fileContent
;
125 Section
<A
>** _sectionsArray
;
126 uint8_t* _atomsArray
;
127 uint8_t* _aliasAtomsArray
;
128 uint32_t _sectionsArrayCount
;
129 uint32_t _atomsArrayCount
;
130 uint32_t _aliasAtomsArrayCount
;
131 std::vector
<ld::Fixup
> _fixups
;
132 std::vector
<ld::Atom::UnwindInfo
> _unwindInfos
;
133 std::vector
<ld::Atom::LineInfo
> _lineInfos
;
134 std::vector
<ld::relocatable::File::Stab
>_stabs
;
135 ld::relocatable::File::DebugInfoKind _debugInfoKind
;
136 const char* _dwarfTranslationUnitPath
;
137 const macho_section
<P
>* _dwarfDebugInfoSect
;
138 const macho_section
<P
>* _dwarfDebugAbbrevSect
;
139 const macho_section
<P
>* _dwarfDebugLineSect
;
140 const macho_section
<P
>* _dwarfDebugStringSect
;
141 ld::File::ObjcConstraint _objConstraint
;
142 uint8_t _swiftVersion
;
143 uint32_t _cpuSubType
;
144 uint32_t _minOSVersion
;
146 bool _canScatterAtoms
;
147 bool _objcHasCategoryClassPropertiesField
;
148 std::vector
<std::vector
<const char*> > _linkerOptions
;
149 std::unique_ptr
<ld::Bitcode
> _bitcode
;
154 template <typename A
>
155 class Section
: public ld::Section
158 typedef typename
A::P::uint_t pint_t
;
159 typedef typename
A::P P
;
160 typedef typename
A::P::E E
;
162 virtual ~Section() { }
163 class File
<A
>& file() const { return _file
; }
164 const macho_section
<P
>* machoSection() const { return _machOSection
; }
165 uint32_t sectionNum(class Parser
<A
>&) const;
166 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
);
167 virtual ld::Atom::ContentType
contentType() { return ld::Atom::typeUnclassified
; }
168 virtual bool dontDeadStrip() { return (this->_machOSection
->flags() & S_ATTR_NO_DEAD_STRIP
); }
169 virtual bool dontDeadStripIfReferencesLive() { return ( (this->_machOSection
!= NULL
) && (this->_machOSection
->flags() & S_ATTR_LIVE_SUPPORT
) ); }
170 virtual Atom
<A
>* findAtomByAddress(pint_t addr
) { return this->findContentAtomByAddress(addr
, this->_beginAtoms
, this->_endAtoms
); }
171 virtual bool addFollowOnFixups() const { return ! _file
.canScatterAtoms(); }
172 virtual uint32_t appendAtoms(class Parser
<A
>& parser
, uint8_t* buffer
,
173 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
174 const struct Parser
<A
>::CFI_CU_InfoArrays
&) = 0;
175 virtual uint32_t computeAtomCount(class Parser
<A
>& parser
,
176 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
177 const struct Parser
<A
>::CFI_CU_InfoArrays
&) = 0;
178 virtual void makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
&);
179 virtual bool addRelocFixup(class Parser
<A
>& parser
, const macho_relocation_info
<P
>*);
180 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const { return 0; }
181 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
182 const ld::IndirectBindingTable
& ind
) const { return false; }
183 virtual bool ignoreLabel(const char* label
) const { return false; }
184 static const char* makeSectionName(const macho_section
<typename
A::P
>* s
);
187 Section(File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
188 : ld::Section(makeSegmentName(s
), makeSectionName(s
), sectionType(s
)),
189 _file(f
), _machOSection(s
), _beginAtoms(NULL
), _endAtoms(NULL
), _hasAliases(false) { }
190 Section(File
<A
>& f
, const char* segName
, const char* sectName
, ld::Section::Type t
, bool hidden
=false)
191 : ld::Section(segName
, sectName
, t
, hidden
), _file(f
), _machOSection(NULL
),
192 _beginAtoms(NULL
), _endAtoms(NULL
), _hasAliases(false) { }
195 Atom
<A
>* findContentAtomByAddress(pint_t addr
, class Atom
<A
>* start
, class Atom
<A
>* end
);
196 uint32_t x86_64PcRelOffset(uint8_t r_type
);
197 void addLOH(class Parser
<A
>& parser
, int kind
, int count
, const uint64_t addrs
[]);
198 static const char* makeSegmentName(const macho_section
<typename
A::P
>* s
);
199 static bool readable(const macho_section
<typename
A::P
>* s
);
200 static bool writable(const macho_section
<typename
A::P
>* s
);
201 static bool exectuable(const macho_section
<typename
A::P
>* s
);
202 static ld::Section::Type
sectionType(const macho_section
<typename
A::P
>* s
);
205 const macho_section
<P
>* _machOSection
;
206 class Atom
<A
>* _beginAtoms
;
207 class Atom
<A
>* _endAtoms
;
209 std::set
<const class Atom
<A
>*> _altEntries
;
213 template <typename A
>
214 class CFISection
: public Section
<A
>
217 CFISection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
218 : Section
<A
>(f
, s
) { }
219 uint32_t cfiCount(Parser
<A
>& parser
);
221 virtual ld::Atom::ContentType
contentType() { return ld::Atom::typeCFI
; }
222 virtual uint32_t computeAtomCount(class Parser
<A
>& parser
, struct Parser
<A
>::LabelAndCFIBreakIterator
& it
, const struct Parser
<A
>::CFI_CU_InfoArrays
&);
223 virtual uint32_t appendAtoms(class Parser
<A
>& parser
, uint8_t* buffer
, struct Parser
<A
>::LabelAndCFIBreakIterator
& it
, const struct Parser
<A
>::CFI_CU_InfoArrays
&);
224 virtual void makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
&);
225 virtual bool addFollowOnFixups() const { return false; }
229 /// ObjectFileAddressSpace is used as a template parameter to UnwindCursor for parsing
230 /// dwarf CFI information in an object file.
235 typedef typename
A::P::uint_t pint_t
;
236 typedef typename
A::P P
;
237 typedef typename
A::P::E E
;
238 typedef typename
A::P::uint_t sint_t
;
240 OAS(CFISection
<A
>& ehFrameSection
, const uint8_t* ehFrameBuffer
) :
241 _ehFrameSection(ehFrameSection
),
242 _ehFrameContent(ehFrameBuffer
),
243 _ehFrameStartAddr(ehFrameSection
.machoSection()->addr()),
244 _ehFrameEndAddr(ehFrameSection
.machoSection()->addr()+ehFrameSection
.machoSection()->size()) {}
246 uint8_t get8(pint_t addr
) { return *((uint8_t*)mappedAddress(addr
)); }
247 uint16_t get16(pint_t addr
) { return E::get16(*((uint16_t*)mappedAddress(addr
))); }
248 uint32_t get32(pint_t addr
) { return E::get32(*((uint32_t*)mappedAddress(addr
))); }
249 uint64_t get64(pint_t addr
) { return E::get64(*((uint64_t*)mappedAddress(addr
))); }
250 pint_t
getP(pint_t addr
) { return P::getP(*((pint_t
*)mappedAddress(addr
))); }
251 uint64_t getULEB128(pint_t
& addr
, pint_t end
);
252 int64_t getSLEB128(pint_t
& addr
, pint_t end
);
253 pint_t
getEncodedP(pint_t
& addr
, pint_t end
, uint8_t encoding
);
255 const void* mappedAddress(pint_t addr
);
257 CFISection
<A
>& _ehFrameSection
;
258 const uint8_t* _ehFrameContent
;
259 pint_t _ehFrameStartAddr
;
260 pint_t _ehFrameEndAddr
;
264 typedef typename
A::P::uint_t pint_t
;
265 typedef libunwind::CFI_Atom_Info
<OAS
> CFI_Atom_Info
;
267 void cfiParse(class Parser
<A
>& parser
, uint8_t* buffer
, CFI_Atom_Info cfiArray
[], uint32_t& cfiCount
, const pint_t cuStarts
[], uint32_t cuCount
);
268 bool needsRelocating();
270 static bool bigEndian();
272 void addCiePersonalityFixups(class Parser
<A
>& parser
, const CFI_Atom_Info
* cieInfo
);
273 static void warnFunc(void* ref
, uint64_t funcAddr
, const char* msg
);
277 template <typename A
>
278 class CUSection
: public Section
<A
>
281 CUSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
282 : Section
<A
>(f
, s
) { }
284 typedef typename
A::P::uint_t pint_t
;
285 typedef typename
A::P P
;
286 typedef typename
A::P::E E
;
288 virtual uint32_t computeAtomCount(class Parser
<A
>& parser
, struct Parser
<A
>::LabelAndCFIBreakIterator
& it
, const struct Parser
<A
>::CFI_CU_InfoArrays
&) { return 0; }
289 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; }
290 virtual void makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
&);
291 virtual bool addFollowOnFixups() const { return false; }
294 pint_t functionStartAddress
;
295 uint32_t functionSymbolIndex
;
296 uint32_t rangeLength
;
297 uint32_t compactUnwindInfo
;
298 const char* personality
;
305 void parse(class Parser
<A
>& parser
, uint32_t cnt
, Info array
[]);
306 static bool encodingMeansUseDwarf(compact_unwind_encoding_t enc
);
311 const char* personalityName(class Parser
<A
>& parser
, const macho_relocation_info
<P
>* reloc
);
313 static int infoSorter(const void* l
, const void* r
);
318 template <typename A
>
319 class TentativeDefinitionSection
: public Section
<A
>
322 TentativeDefinitionSection(Parser
<A
>& parser
, File
<A
>& f
)
323 : Section
<A
>(f
, "__DATA", "__comm/tent", ld::Section::typeTentativeDefs
) {}
325 virtual ld::Atom::ContentType
contentType() { return ld::Atom::typeZeroFill
; }
326 virtual bool addFollowOnFixups() const { return false; }
327 virtual Atom
<A
>* findAtomByAddress(typename
A::P::uint_t addr
) { throw "TentativeDefinitionSection::findAtomByAddress() should never be called"; }
328 virtual uint32_t computeAtomCount(class Parser
<A
>& parser
, struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
329 const struct Parser
<A
>::CFI_CU_InfoArrays
&);
330 virtual uint32_t appendAtoms(class Parser
<A
>& parser
, uint8_t* buffer
,
331 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
332 const struct Parser
<A
>::CFI_CU_InfoArrays
&);
333 virtual void makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
&) {}
335 typedef typename
A::P::uint_t pint_t
;
336 typedef typename
A::P P
;
340 template <typename A
>
341 class AbsoluteSymbolSection
: public Section
<A
>
344 AbsoluteSymbolSection(Parser
<A
>& parser
, File
<A
>& f
)
345 : Section
<A
>(f
, "__DATA", "__abs", ld::Section::typeAbsoluteSymbols
, true) {}
347 virtual ld::Atom::ContentType
contentType() { return ld::Atom::typeUnclassified
; }
348 virtual bool dontDeadStrip() { return false; }
349 virtual ld::Atom::Alignment
alignmentForAddress(typename
A::P::uint_t addr
) { return ld::Atom::Alignment(0); }
350 virtual bool addFollowOnFixups() const { return false; }
351 virtual Atom
<A
>* findAtomByAddress(typename
A::P::uint_t addr
) { throw "AbsoluteSymbolSection::findAtomByAddress() should never be called"; }
352 virtual uint32_t computeAtomCount(class Parser
<A
>& parser
, struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
353 const struct Parser
<A
>::CFI_CU_InfoArrays
&);
354 virtual uint32_t appendAtoms(class Parser
<A
>& parser
, uint8_t* buffer
,
355 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
356 const struct Parser
<A
>::CFI_CU_InfoArrays
&);
357 virtual void makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
&) {}
358 virtual Atom
<A
>* findAbsAtomForValue(typename
A::P::uint_t
);
361 typedef typename
A::P::uint_t pint_t
;
362 typedef typename
A::P P
;
366 template <typename A
>
367 class SymboledSection
: public Section
<A
>
370 SymboledSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
);
371 virtual ld::Atom::ContentType
contentType() { return _type
; }
372 virtual bool dontDeadStrip();
373 virtual uint32_t computeAtomCount(class Parser
<A
>& parser
, struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
374 const struct Parser
<A
>::CFI_CU_InfoArrays
&);
375 virtual uint32_t appendAtoms(class Parser
<A
>& parser
, uint8_t* buffer
,
376 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
377 const struct Parser
<A
>::CFI_CU_InfoArrays
&);
379 typedef typename
A::P::uint_t pint_t
;
380 typedef typename
A::P P
;
382 ld::Atom::ContentType _type
;
386 template <typename A
>
387 class TLVDefsSection
: public SymboledSection
<A
>
390 TLVDefsSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
) :
391 SymboledSection
<A
>(parser
, f
, s
) { }
393 typedef typename
A::P::uint_t pint_t
;
395 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
) { return ld::Atom::Alignment(log2(sizeof(pint_t
))); }
402 template <typename A
>
403 class ImplicitSizeSection
: public Section
<A
>
406 ImplicitSizeSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
407 : Section
<A
>(f
, s
) { }
408 virtual uint32_t computeAtomCount(class Parser
<A
>& parser
, struct Parser
<A
>::LabelAndCFIBreakIterator
& it
, const struct Parser
<A
>::CFI_CU_InfoArrays
&);
409 virtual uint32_t appendAtoms(class Parser
<A
>& parser
, uint8_t* buffer
, struct Parser
<A
>::LabelAndCFIBreakIterator
& it
, const struct Parser
<A
>::CFI_CU_InfoArrays
&);
411 typedef typename
A::P::uint_t pint_t
;
412 typedef typename
A::P P
;
414 virtual bool addFollowOnFixups() const { return false; }
415 virtual const char* unlabeledAtomName(Parser
<A
>& parser
, pint_t addr
) = 0;
416 virtual ld::Atom::SymbolTableInclusion
symbolTableInclusion();
417 virtual pint_t
elementSizeAtAddress(pint_t addr
) = 0;
418 virtual ld::Atom::Scope
scopeAtAddress(Parser
<A
>& parser
, pint_t addr
) { return ld::Atom::scopeLinkageUnit
; }
419 virtual bool useElementAt(Parser
<A
>& parser
,
420 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
, pint_t addr
) = 0;
421 virtual ld::Atom::Definition
definition() { return ld::Atom::definitionRegular
; }
422 virtual ld::Atom::Combine
combine(Parser
<A
>& parser
, pint_t addr
) = 0;
423 virtual bool ignoreLabel(const char* label
) const { return (label
[0] == 'L'); }
427 template <typename A
>
428 class FixedSizeSection
: public ImplicitSizeSection
<A
>
431 FixedSizeSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
432 : ImplicitSizeSection
<A
>(parser
, f
, s
) { }
434 typedef typename
A::P::uint_t pint_t
;
435 typedef typename
A::P P
;
436 typedef typename
A::P::E E
;
438 virtual bool useElementAt(Parser
<A
>& parser
,
439 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
, pint_t addr
)
444 template <typename A
>
445 class Literal4Section
: public FixedSizeSection
<A
>
448 Literal4Section(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
449 : FixedSizeSection
<A
>(parser
, f
, s
) {}
451 typedef typename
A::P::uint_t pint_t
;
452 typedef typename
A::P P
;
454 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
) { return ld::Atom::Alignment(2); }
455 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "4-byte-literal"; }
456 virtual pint_t
elementSizeAtAddress(pint_t addr
) { return 4; }
457 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
) { return ld::Atom::combineByNameAndContent
; }
458 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
459 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
460 const ld::IndirectBindingTable
& ind
) const;
461 virtual bool ignoreLabel(const char* label
) const;
464 template <typename A
>
465 class Literal8Section
: public FixedSizeSection
<A
>
468 Literal8Section(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
469 : FixedSizeSection
<A
>(parser
, f
, s
) {}
471 typedef typename
A::P::uint_t pint_t
;
472 typedef typename
A::P P
;
474 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
) { return ld::Atom::Alignment(3); }
475 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "8-byte-literal"; }
476 virtual pint_t
elementSizeAtAddress(pint_t addr
) { return 8; }
477 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
) { return ld::Atom::combineByNameAndContent
; }
478 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
479 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
480 const ld::IndirectBindingTable
& ind
) const;
481 virtual bool ignoreLabel(const char* label
) const;
484 template <typename A
>
485 class Literal16Section
: public FixedSizeSection
<A
>
488 Literal16Section(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
489 : FixedSizeSection
<A
>(parser
, f
, s
) {}
491 typedef typename
A::P::uint_t pint_t
;
492 typedef typename
A::P P
;
494 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
) { return ld::Atom::Alignment(4); }
495 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "16-byte-literal"; }
496 virtual pint_t
elementSizeAtAddress(pint_t addr
) { return 16; }
497 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
) { return ld::Atom::combineByNameAndContent
; }
498 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
499 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
500 const ld::IndirectBindingTable
& ind
) const;
501 virtual bool ignoreLabel(const char* label
) const;
505 template <typename A
>
506 class NonLazyPointerSection
: public FixedSizeSection
<A
>
509 NonLazyPointerSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
510 : FixedSizeSection
<A
>(parser
, f
, s
) {}
512 typedef typename
A::P::uint_t pint_t
;
513 typedef typename
A::P P
;
515 virtual void makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
&);
516 virtual ld::Atom::ContentType
contentType() { return ld::Atom::typeNonLazyPointer
; }
517 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
) { return ld::Atom::Alignment(log2(sizeof(pint_t
))); }
518 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "non_lazy_ptr"; }
519 virtual pint_t
elementSizeAtAddress(pint_t addr
) { return sizeof(pint_t
); }
520 virtual ld::Atom::Scope
scopeAtAddress(Parser
<A
>& parser
, pint_t addr
);
521 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
);
522 virtual bool ignoreLabel(const char* label
) const { return true; }
523 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
524 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
525 const ld::IndirectBindingTable
& ind
) const;
528 static const char* targetName(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
);
529 static ld::Fixup::Kind
fixupKind();
532 template <typename A
>
533 class TLVPointerSection
: public FixedSizeSection
<A
>
536 TLVPointerSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
537 : FixedSizeSection
<A
>(parser
, f
, s
) {}
539 typedef typename
A::P::uint_t pint_t
;
540 typedef typename
A::P P
;
542 virtual void makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
&);
543 virtual ld::Atom::ContentType
contentType() { return ld::Atom::typeTLVPointer
; }
544 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
) { return ld::Atom::Alignment(log2(sizeof(pint_t
))); }
545 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "tlv_lazy_ptr"; }
546 virtual pint_t
elementSizeAtAddress(pint_t addr
) { return sizeof(pint_t
); }
547 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
);
548 virtual bool ignoreLabel(const char* label
) const { return true; }
549 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
550 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
551 const ld::IndirectBindingTable
& ind
) const;
554 static const char* targetName(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
, bool* isStatic
);
558 template <typename A
>
559 class CFStringSection
: public FixedSizeSection
<A
>
562 CFStringSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
563 : FixedSizeSection
<A
>(parser
, f
, s
) {}
565 typedef typename
A::P::uint_t pint_t
;
567 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
) { return ld::Atom::Alignment(log2(sizeof(pint_t
))); }
568 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "CFString"; }
569 virtual pint_t
elementSizeAtAddress(pint_t addr
) { return 4*sizeof(pint_t
); }
570 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
) { return ld::Atom::combineByNameAndReferences
; }
571 virtual bool ignoreLabel(const char* label
) const { return true; }
572 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
573 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
574 const ld::IndirectBindingTable
& ind
) const;
576 enum ContentType
{ contentUTF8
, contentUTF16
, contentUnknown
};
577 static const uint8_t* targetContent(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
,
578 ContentType
* ct
, unsigned int* count
);
582 template <typename A
>
583 class ObjC1ClassSection
: public FixedSizeSection
<A
>
586 ObjC1ClassSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
587 : FixedSizeSection
<A
>(parser
, f
, s
) {}
589 typedef typename
A::P::uint_t pint_t
;
590 typedef typename
A::P P
;
591 typedef typename
A::P::E E
;
593 virtual ld::Atom::Scope
scopeAtAddress(Parser
<A
>& , pint_t
) { return ld::Atom::scopeGlobal
; }
594 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
) { return ld::Atom::Alignment(2); }
595 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
);
596 virtual ld::Atom::SymbolTableInclusion
symbolTableInclusion() { return ld::Atom::symbolTableIn
; }
597 virtual pint_t
elementSizeAtAddress(pint_t addr
);
598 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
) { return ld::Atom::combineNever
; }
599 virtual bool ignoreLabel(const char* label
) const { return true; }
600 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
602 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
603 const ld::IndirectBindingTable
& ind
) const { return false; }
604 virtual bool addRelocFixup(class Parser
<A
>& parser
, const macho_relocation_info
<P
>*);
608 template <typename A
>
609 class ObjC2ClassRefsSection
: public FixedSizeSection
<A
>
612 ObjC2ClassRefsSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
613 : FixedSizeSection
<A
>(parser
, f
, s
) {}
615 typedef typename
A::P::uint_t pint_t
;
617 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
) { return ld::Atom::Alignment(log2(sizeof(pint_t
))); }
618 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "objc-class-ref"; }
619 virtual pint_t
elementSizeAtAddress(pint_t addr
) { return sizeof(pint_t
); }
620 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
) { return ld::Atom::combineByNameAndReferences
; }
621 virtual bool ignoreLabel(const char* label
) const { return true; }
622 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
623 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
624 const ld::IndirectBindingTable
& ind
) const;
626 const char* targetClassName(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
630 template <typename A
>
631 class ObjC2CategoryListSection
: public FixedSizeSection
<A
>
634 ObjC2CategoryListSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
635 : FixedSizeSection
<A
>(parser
, f
, s
) {}
637 typedef typename
A::P::uint_t pint_t
;
639 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
) { return ld::Atom::Alignment(log2(sizeof(pint_t
))); }
640 virtual ld::Atom::Scope
scopeAtAddress(Parser
<A
>& parser
, pint_t addr
) { return ld::Atom::scopeTranslationUnit
; }
641 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "objc-cat-list"; }
642 virtual pint_t
elementSizeAtAddress(pint_t addr
) { return sizeof(pint_t
); }
643 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
) { return ld::Atom::combineNever
; }
644 virtual bool ignoreLabel(const char* label
) const { return true; }
646 const char* targetClassName(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
650 template <typename A
>
651 class PointerToCStringSection
: public FixedSizeSection
<A
>
654 PointerToCStringSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
655 : FixedSizeSection
<A
>(parser
, f
, s
) {}
657 typedef typename
A::P::uint_t pint_t
;
659 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
) { return ld::Atom::Alignment(log2(sizeof(pint_t
))); }
660 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "pointer-to-literal-cstring"; }
661 virtual pint_t
elementSizeAtAddress(pint_t addr
) { return sizeof(pint_t
); }
662 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
) { return ld::Atom::combineByNameAndReferences
; }
663 virtual bool ignoreLabel(const char* label
) const { return true; }
664 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
665 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
666 const ld::IndirectBindingTable
& ind
) const;
667 virtual const char* targetCString(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
671 template <typename A
>
672 class Objc1ClassReferences
: public PointerToCStringSection
<A
>
675 Objc1ClassReferences(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
676 : PointerToCStringSection
<A
>(parser
, f
, s
) {}
678 typedef typename
A::P::uint_t pint_t
;
679 typedef typename
A::P P
;
681 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "pointer-to-literal-objc-class-name"; }
682 virtual bool addRelocFixup(class Parser
<A
>& parser
, const macho_relocation_info
<P
>*);
683 virtual const char* targetCString(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
687 template <typename A
>
688 class CStringSection
: public ImplicitSizeSection
<A
>
691 CStringSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
692 : ImplicitSizeSection
<A
>(parser
, f
, s
) {}
694 typedef typename
A::P::uint_t pint_t
;
695 typedef typename
A::P P
;
697 virtual ld::Atom::ContentType
contentType() { return ld::Atom::typeCString
; }
698 virtual Atom
<A
>* findAtomByAddress(pint_t addr
);
699 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "cstring"; }
700 virtual pint_t
elementSizeAtAddress(pint_t addr
);
701 virtual bool ignoreLabel(const char* label
) const;
702 virtual bool useElementAt(Parser
<A
>& parser
,
703 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
, pint_t addr
);
704 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
) { return ld::Atom::combineByNameAndContent
; }
705 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
706 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
707 const ld::IndirectBindingTable
& ind
) const;
712 template <typename A
>
713 class UTF16StringSection
: public SymboledSection
<A
>
716 UTF16StringSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
717 : SymboledSection
<A
>(parser
, f
, s
) {}
719 typedef typename
A::P::uint_t pint_t
;
720 typedef typename
A::P P
;
722 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
) { return ld::Atom::combineByNameAndContent
; }
723 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
724 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
725 const ld::IndirectBindingTable
& ind
) const;
730 // Atoms in mach-o files
732 template <typename A
>
733 class Atom
: public ld::Atom
736 // overrides of ld::Atom
737 virtual const ld::File
* file() const;
738 virtual const char* translationUnitSource() const
739 { return sect().file().translationUnitSource(); }
740 virtual const char* name() const { return _name
; }
741 virtual uint64_t size() const { return _size
; }
742 virtual uint64_t objectAddress() const { return _objAddress
; }
743 virtual void copyRawContent(uint8_t buffer
[]) const;
744 virtual const uint8_t* rawContentPointer() const { return contentPointer(); }
745 virtual unsigned long contentHash(const ld::IndirectBindingTable
& ind
) const
746 { if ( _hash
== 0 ) _hash
= sect().contentHash(this, ind
); return _hash
; }
747 virtual bool canCoalesceWith(const ld::Atom
& rhs
, const ld::IndirectBindingTable
& ind
) const
748 { return sect().canCoalesceWith(this, rhs
, ind
); }
749 virtual ld::Fixup::iterator
fixupsBegin() const { return &machofile()._fixups
[_fixupsStartIndex
]; }
750 virtual ld::Fixup::iterator
fixupsEnd() const { return &machofile()._fixups
[_fixupsStartIndex
+_fixupsCount
]; }
751 virtual ld::Atom::UnwindInfo::iterator
beginUnwind() const { return &machofile()._unwindInfos
[_unwindInfoStartIndex
]; }
752 virtual ld::Atom::UnwindInfo::iterator
endUnwind() const { return &machofile()._unwindInfos
[_unwindInfoStartIndex
+_unwindInfoCount
]; }
753 virtual ld::Atom::LineInfo::iterator
beginLineInfo() const{ return &machofile()._lineInfos
[_lineInfoStartIndex
]; }
754 virtual ld::Atom::LineInfo::iterator
endLineInfo() const { return &machofile()._lineInfos
[_lineInfoStartIndex
+_lineInfoCount
]; }
755 virtual void setFile(const ld::File
* f
);
759 enum { kFixupStartIndexBits
= 32,
760 kLineInfoStartIndexBits
= 32,
761 kUnwindInfoStartIndexBits
= 24,
762 kFixupCountBits
= 24,
763 kLineInfoCountBits
= 12,
764 kUnwindInfoCountBits
= 4
765 }; // must sum to 128
768 // methods for all atoms from mach-o object file
769 Section
<A
>& sect() const { return (Section
<A
>&)section(); }
770 File
<A
>& machofile() const { return ((Section
<A
>*)(this->_section
))->file(); }
771 void setFixupsRange(uint32_t s
, uint32_t c
);
772 void setUnwindInfoRange(uint32_t s
, uint32_t c
);
773 void extendUnwindInfoRange();
774 void setLineInfoRange(uint32_t s
, uint32_t c
);
775 bool roomForMoreLineInfoCount() { return (_lineInfoCount
< ((1<<kLineInfoCountBits
)-1)); }
776 void incrementLineInfoCount() { assert(roomForMoreLineInfoCount()); ++_lineInfoCount
; }
777 void incrementFixupCount() { if (_fixupsCount
== ((1 << kFixupCountBits
)-1))
778 throwf("too may fixups in %s", name()); ++_fixupsCount
; }
779 const uint8_t* contentPointer() const;
780 uint32_t fixupCount() const { return _fixupsCount
; }
781 void verifyAlignment(const macho_section
<typename
A::P
>&) const;
783 typedef typename
A::P P
;
784 typedef typename
A::P::E E
;
785 typedef typename
A::P::uint_t pint_t
;
786 // constuct via all attributes
787 Atom(Section
<A
>& sct
, const char* nm
, pint_t addr
, uint64_t sz
,
788 ld::Atom::Definition d
, ld::Atom::Combine c
, ld::Atom::Scope s
,
789 ld::Atom::ContentType ct
, ld::Atom::SymbolTableInclusion i
,
790 bool dds
, bool thumb
, bool al
, ld::Atom::Alignment a
)
791 : ld::Atom((ld::Section
&)sct
, d
, c
, s
, ct
, i
, dds
, thumb
, al
, a
),
792 _size(sz
), _objAddress(addr
), _name(nm
), _hash(0),
793 _fixupsStartIndex(0), _lineInfoStartIndex(0),
794 _unwindInfoStartIndex(0), _fixupsCount(0),
795 _lineInfoCount(0), _unwindInfoCount(0) { }
796 // construct via symbol table entry
797 Atom(Section
<A
>& sct
, Parser
<A
>& parser
, const macho_nlist
<P
>& sym
,
798 uint64_t sz
, bool alias
=false)
799 : ld::Atom((ld::Section
&)sct
, parser
.definitionFromSymbol(sym
),
800 parser
.combineFromSymbol(sym
), parser
.scopeFromSymbol(sym
),
801 parser
.resolverFromSymbol(sym
) ? ld::Atom::typeResolver
: sct
.contentType(),
802 parser
.inclusionFromSymbol(sym
),
803 (parser
.dontDeadStripFromSymbol(sym
) && !sct
.dontDeadStripIfReferencesLive()) || sct
.dontDeadStrip(),
804 parser
.isThumbFromSymbol(sym
), alias
,
805 sct
.alignmentForAddress(sym
.n_value())),
806 _size(sz
), _objAddress(sym
.n_value()),
807 _name(parser
.nameFromSymbol(sym
)), _hash(0),
808 _fixupsStartIndex(0), _lineInfoStartIndex(0),
809 _unwindInfoStartIndex(0), _fixupsCount(0),
810 _lineInfoCount(0), _unwindInfoCount(0) {
811 // <rdar://problem/6783167> support auto-hidden weak symbols
812 if ( _scope
== ld::Atom::scopeGlobal
&&
813 (sym
.n_desc() & (N_WEAK_DEF
|N_WEAK_REF
)) == (N_WEAK_DEF
|N_WEAK_REF
) )
815 this->verifyAlignment(*sct
.machoSection());
816 if ( sct
.dontDeadStripIfReferencesLive() )
817 this->setDontDeadStripIfReferencesLive();
821 friend class Parser
<A
>;
822 friend class Section
<A
>;
823 friend class CStringSection
<A
>;
824 friend class AbsoluteSymbolSection
<A
>;
829 mutable unsigned long _hash
;
831 uint64_t _fixupsStartIndex
: kFixupStartIndexBits
,
832 _lineInfoStartIndex
: kLineInfoStartIndexBits
,
833 _unwindInfoStartIndex
: kUnwindInfoStartIndexBits
,
834 _fixupsCount
: kFixupCountBits
,
835 _lineInfoCount
: kLineInfoCountBits
,
836 _unwindInfoCount
: kUnwindInfoCountBits
;
838 static std::map
<const ld::Atom
*, const ld::File
*> _s_fileOverride
;
841 template <typename A
>
842 std::map
<const ld::Atom
*, const ld::File
*> Atom
<A
>::_s_fileOverride
;
844 template <typename A
>
845 void Atom
<A
>::setFile(const ld::File
* f
) {
846 _s_fileOverride
[this] = f
;
849 template <typename A
>
850 const ld::File
* Atom
<A
>::file() const
852 std::map
<const ld::Atom
*, const ld::File
*>::iterator pos
= _s_fileOverride
.find(this);
853 if ( pos
!= _s_fileOverride
.end() )
856 return §().file();
859 template <typename A
>
860 void Atom
<A
>::setFixupsRange(uint32_t startIndex
, uint32_t count
)
862 if ( count
>= (1 << kFixupCountBits
) )
863 throwf("too many fixups in function %s", this->name());
864 if ( startIndex
>= (1 << kFixupStartIndexBits
) )
865 throwf("too many fixups in file");
866 assert(((startIndex
+count
) <= sect().file()._fixups
.size()) && "fixup index out of range");
867 _fixupsStartIndex
= startIndex
;
868 _fixupsCount
= count
;
871 template <typename A
>
872 void Atom
<A
>::setUnwindInfoRange(uint32_t startIndex
, uint32_t count
)
874 if ( count
>= (1 << kUnwindInfoCountBits
) )
875 throwf("too many compact unwind infos in function %s", this->name());
876 if ( startIndex
>= (1 << kUnwindInfoStartIndexBits
) )
877 throwf("too many compact unwind infos (%d) in file", startIndex
);
878 assert((startIndex
+count
) <= sect().file()._unwindInfos
.size() && "unwindinfo index out of range");
879 _unwindInfoStartIndex
= startIndex
;
880 _unwindInfoCount
= count
;
883 template <typename A
>
884 void Atom
<A
>::extendUnwindInfoRange()
886 if ( _unwindInfoCount
+1 >= (1 << kUnwindInfoCountBits
) )
887 throwf("too many compact unwind infos in function %s", this->name());
888 _unwindInfoCount
+= 1;
891 template <typename A
>
892 void Atom
<A
>::setLineInfoRange(uint32_t startIndex
, uint32_t count
)
894 assert((count
< (1 << kLineInfoCountBits
)) && "too many line infos");
895 assert((startIndex
+count
) < sect().file()._lineInfos
.size() && "line info index out of range");
896 _lineInfoStartIndex
= startIndex
;
897 _lineInfoCount
= count
;
900 template <typename A
>
901 const uint8_t* Atom
<A
>::contentPointer() const
903 const macho_section
<P
>* sct
= this->sect().machoSection();
904 if ( this->_objAddress
> sct
->addr() + sct
->size() )
905 throwf("malformed .o file, symbol has address 0x%0llX which is outside range of its section", (uint64_t)this->_objAddress
);
906 uint32_t fileOffset
= sct
->offset() - sct
->addr() + this->_objAddress
;
907 return this->sect().file().fileContent()+fileOffset
;
911 template <typename A
>
912 void Atom
<A
>::copyRawContent(uint8_t buffer
[]) const
915 if ( this->contentType() == ld::Atom::typeZeroFill
) {
916 bzero(buffer
, _size
);
918 else if ( _size
!= 0 ) {
919 memcpy(buffer
, this->contentPointer(), _size
);
924 void Atom
<arm
>::verifyAlignment(const macho_section
<P
>&) const
926 if ( (this->section().type() == ld::Section::typeCode
) && ! isThumb() ) {
927 if ( ((_objAddress
% 4) != 0) || (this->alignment().powerOf2
< 2) )
928 warning("ARM function not 4-byte aligned: %s from %s", this->name(), this->file()->path());
932 #if SUPPORT_ARCH_arm64
934 void Atom
<arm64
>::verifyAlignment(const macho_section
<P
>& sect
) const
936 if ( (this->section().type() == ld::Section::typeCode
) && (sect
.size() != 0) ) {
937 if ( ((_objAddress
% 4) != 0) || (this->alignment().powerOf2
< 2) )
938 warning("arm64 function not 4-byte aligned: %s from %s", this->name(), this->file()->path());
944 template <typename A
>
945 void Atom
<A
>::verifyAlignment(const macho_section
<P
>&) const
950 class AliasAtom
: public ld::Atom
953 AliasAtom(const char* name
, bool hidden
, const ld::File
* file
, const char* aliasOfName
) :
954 ld::Atom(_s_section
, ld::Atom::definitionRegular
, ld::Atom::combineNever
,
955 (hidden
? ld::Atom::scopeLinkageUnit
: ld::Atom::scopeGlobal
),
956 ld::Atom::typeUnclassified
, ld::Atom::symbolTableIn
,
957 false, false, true, 0),
960 _fixup(0, ld::Fixup::k1of1
, ld::Fixup::kindNoneFollowOn
, ld::Fixup::bindingByNameUnbound
, aliasOfName
) { }
962 virtual const ld::File
* file() const { return _file
; }
963 virtual const char* translationUnitSource() const
965 virtual const char* name() const { return _name
; }
966 virtual uint64_t size() const { return 0; }
967 virtual uint64_t objectAddress() const { return 0; }
968 virtual void copyRawContent(uint8_t buffer
[]) const { }
969 virtual ld::Fixup::iterator
fixupsBegin() const { return &((ld::Fixup
*)&_fixup
)[0]; }
970 virtual ld::Fixup::iterator
fixupsEnd() const { return &((ld::Fixup
*)&_fixup
)[1]; }
973 static ld::Section _s_section
;
975 const ld::File
* _file
;
980 ld::Section
AliasAtom::_s_section("__LD", "__aliases", ld::Section::typeTempAlias
, true);
983 template <typename A
>
987 static bool validFile(const uint8_t* fileContent
, bool subtypeMustMatch
=false,
988 cpu_subtype_t subtype
=0);
989 static const char* fileKind(const uint8_t* fileContent
);
990 static Options::Platform
findPlatform(const macho_header
<typename
A::P
>* header
);
991 static bool hasObjC2Categories(const uint8_t* fileContent
);
992 static bool hasObjC1Categories(const uint8_t* fileContent
);
993 static bool getNonLocalSymbols(const uint8_t* fileContnet
, std::vector
<const char*> &syms
);
994 static ld::relocatable::File
* parse(const uint8_t* fileContent
, uint64_t fileLength
,
995 const char* path
, time_t modTime
, ld::File::Ordinal ordinal
,
996 const ParserOptions
& opts
) {
997 Parser
p(fileContent
, fileLength
, path
, modTime
,
998 ordinal
, opts
.warnUnwindConversionProblems
,
999 opts
.keepDwarfUnwind
, opts
.forceDwarfConversion
,
1000 opts
.neverConvertDwarf
, opts
.verboseOptimizationHints
,
1001 opts
.ignoreMismatchPlatform
);
1002 return p
.parse(opts
);
1005 typedef typename
A::P P
;
1006 typedef typename
A::P::E E
;
1007 typedef typename
A::P::uint_t pint_t
;
1009 struct SourceLocation
{
1011 SourceLocation(Atom
<A
>* a
, uint32_t o
) : atom(a
), offsetInAtom(o
) {}
1013 uint32_t offsetInAtom
;
1018 const char* name
; // only used if targetAtom is NULL
1020 bool weakImport
; // only used if targetAtom is NULL
1023 struct FixupInAtom
{
1024 FixupInAtom(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, Atom
<A
>* target
) :
1025 fixup(src
.offsetInAtom
, c
, k
, target
), atom(src
.atom
) { src
.atom
->incrementFixupCount(); }
1027 FixupInAtom(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, ld::Fixup::TargetBinding b
, Atom
<A
>* target
) :
1028 fixup(src
.offsetInAtom
, c
, k
, b
, target
), atom(src
.atom
) { src
.atom
->incrementFixupCount(); }
1030 FixupInAtom(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, bool wi
, const char* name
) :
1031 fixup(src
.offsetInAtom
, c
, k
, wi
, name
), atom(src
.atom
) { src
.atom
->incrementFixupCount(); }
1033 FixupInAtom(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, ld::Fixup::TargetBinding b
, const char* name
) :
1034 fixup(src
.offsetInAtom
, c
, k
, b
, name
), atom(src
.atom
) { src
.atom
->incrementFixupCount(); }
1036 FixupInAtom(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, uint64_t addend
) :
1037 fixup(src
.offsetInAtom
, c
, k
, addend
), atom(src
.atom
) { src
.atom
->incrementFixupCount(); }
1039 FixupInAtom(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
) :
1040 fixup(src
.offsetInAtom
, c
, k
, (uint64_t)0), atom(src
.atom
) { src
.atom
->incrementFixupCount(); }
1046 void addFixup(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, Atom
<A
>* target
) {
1047 _allFixups
.push_back(FixupInAtom(src
, c
, k
, target
));
1050 void addFixup(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, ld::Fixup::TargetBinding b
, Atom
<A
>* target
) {
1051 _allFixups
.push_back(FixupInAtom(src
, c
, k
, b
, target
));
1054 void addFixup(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, bool wi
, const char* name
) {
1055 _allFixups
.push_back(FixupInAtom(src
, c
, k
, wi
, name
));
1058 void addFixup(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, ld::Fixup::TargetBinding b
, const char* name
) {
1059 _allFixups
.push_back(FixupInAtom(src
, c
, k
, b
, name
));
1062 void addFixup(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, uint64_t addend
) {
1063 _allFixups
.push_back(FixupInAtom(src
, c
, k
, addend
));
1066 void addFixup(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
) {
1067 _allFixups
.push_back(FixupInAtom(src
, c
, k
));
1070 const char* path() { return _path
; }
1071 uint32_t symbolCount() { return _symbolCount
; }
1072 uint32_t indirectSymbol(uint32_t indirectIndex
);
1073 const macho_nlist
<P
>& symbolFromIndex(uint32_t index
);
1074 const char* nameFromSymbol(const macho_nlist
<P
>& sym
);
1075 ld::Atom::Scope
scopeFromSymbol(const macho_nlist
<P
>& sym
);
1076 static ld::Atom::Definition
definitionFromSymbol(const macho_nlist
<P
>& sym
);
1077 static ld::Atom::Combine
combineFromSymbol(const macho_nlist
<P
>& sym
);
1078 ld::Atom::SymbolTableInclusion
inclusionFromSymbol(const macho_nlist
<P
>& sym
);
1079 static bool dontDeadStripFromSymbol(const macho_nlist
<P
>& sym
);
1080 static bool isThumbFromSymbol(const macho_nlist
<P
>& sym
);
1081 static bool weakImportFromSymbol(const macho_nlist
<P
>& sym
);
1082 static bool resolverFromSymbol(const macho_nlist
<P
>& sym
);
1083 static bool altEntryFromSymbol(const macho_nlist
<P
>& sym
);
1084 uint32_t symbolIndexFromIndirectSectionAddress(pint_t
,const macho_section
<P
>*);
1085 const macho_section
<P
>* firstMachOSection() { return _sectionsStart
; }
1086 const macho_section
<P
>* machOSectionFromSectionIndex(uint32_t index
);
1087 uint32_t machOSectionCount() { return _machOSectionsCount
; }
1088 uint32_t undefinedStartIndex() { return _undefinedStartIndex
; }
1089 uint32_t undefinedEndIndex() { return _undefinedEndIndex
; }
1090 void addFixup(FixupInAtom f
) { _allFixups
.push_back(f
); }
1091 Section
<A
>* sectionForNum(unsigned int sectNum
);
1092 Section
<A
>* sectionForAddress(pint_t addr
);
1093 Atom
<A
>* findAtomByAddress(pint_t addr
);
1094 Atom
<A
>* findAtomByAddressOrNullIfStub(pint_t addr
);
1095 Atom
<A
>* findAtomByAddressOrLocalTargetOfStub(pint_t addr
, uint32_t* offsetInAtom
);
1096 Atom
<A
>* findAtomByName(const char* name
); // slow!
1097 void findTargetFromAddress(pint_t addr
, TargetDesc
& target
);
1098 void findTargetFromAddress(pint_t baseAddr
, pint_t addr
, TargetDesc
& target
);
1099 void findTargetFromAddressAndSectionNum(pint_t addr
, unsigned int sectNum
,
1100 TargetDesc
& target
);
1101 uint32_t tentativeDefinitionCount() { return _tentativeDefinitionCount
; }
1102 uint32_t absoluteSymbolCount() { return _absoluteSymbolCount
; }
1104 uint32_t fileLength() const { return _fileLength
; }
1105 bool hasStubsSection() { return (_stubsSectionNum
!= 0); }
1106 unsigned int stubsSectionNum() { return _stubsSectionNum
; }
1107 void addDtraceExtraInfos(const SourceLocation
& src
, const char* provider
);
1108 const char* scanSymbolTableForAddress(uint64_t addr
);
1109 bool warnUnwindConversionProblems() { return _warnUnwindConversionProblems
; }
1110 bool hasDataInCodeLabels() { return _hasDataInCodeLabels
; }
1111 bool keepDwarfUnwind() { return _keepDwarfUnwind
; }
1112 bool forceDwarfConversion() { return _forceDwarfConversion
; }
1113 bool verboseOptimizationHints() { return _verboseOptimizationHints
; }
1114 bool neverConvertDwarf() { return _neverConvertDwarf
; }
1115 bool armUsesZeroCostExceptions() { return _armUsesZeroCostExceptions
; }
1116 uint8_t maxDefaultCommonAlignment() { return _maxDefaultCommonAlignment
; }
1119 macho_data_in_code_entry
<P
>* dataInCodeStart() { return _dataInCodeStart
; }
1120 macho_data_in_code_entry
<P
>* dataInCodeEnd() { return _dataInCodeEnd
; }
1121 const uint8_t* optimizationHintsStart() { return _lohStart
; }
1122 const uint8_t* optimizationHintsEnd() { return _lohEnd
; }
1123 bool hasOptimizationHints() { return _lohStart
!= _lohEnd
; }
1126 void addFixups(const SourceLocation
& src
, ld::Fixup::Kind kind
, const TargetDesc
& target
);
1127 void addFixups(const SourceLocation
& src
, ld::Fixup::Kind kind
, const TargetDesc
& target
, const TargetDesc
& picBase
);
1131 struct LabelAndCFIBreakIterator
{
1132 typedef typename CFISection
<A
>::CFI_Atom_Info CFI_Atom_Info
;
1133 LabelAndCFIBreakIterator(const uint32_t* ssa
, uint32_t ssc
, const pint_t
* cfisa
,
1134 uint32_t cfisc
, bool ols
)
1135 : sortedSymbolIndexes(ssa
), sortedSymbolCount(ssc
), cfiStartsArray(cfisa
),
1136 cfiStartsCount(cfisc
), fileHasOverlappingSymbols(ols
),
1137 newSection(false), cfiIndex(0), symIndex(0) {}
1138 bool next(Parser
<A
>& parser
, const Section
<A
>& sect
, uint32_t sectNum
, pint_t startAddr
, pint_t endAddr
,
1139 pint_t
* addr
, pint_t
* size
, const macho_nlist
<P
>** sym
);
1140 pint_t
peek(Parser
<A
>& parser
, pint_t startAddr
, pint_t endAddr
);
1141 void beginSection() { newSection
= true; symIndex
= 0; }
1143 const uint32_t* const sortedSymbolIndexes
;
1144 const uint32_t sortedSymbolCount
;
1145 const pint_t
* cfiStartsArray
;
1146 const uint32_t cfiStartsCount
;
1147 const bool fileHasOverlappingSymbols
;
1153 struct CFI_CU_InfoArrays
{
1154 typedef typename CFISection
<A
>::CFI_Atom_Info CFI_Atom_Info
;
1155 typedef typename CUSection
<A
>::Info CU_Info
;
1156 CFI_CU_InfoArrays(const CFI_Atom_Info
* cfiAr
, uint32_t cfiC
, CU_Info
* cuAr
, uint32_t cuC
)
1157 : cfiArray(cfiAr
), cuArray(cuAr
), cfiCount(cfiC
), cuCount(cuC
) {}
1158 const CFI_Atom_Info
* const cfiArray
;
1159 CU_Info
* const cuArray
;
1160 const uint32_t cfiCount
;
1161 const uint32_t cuCount
;
1167 friend class Section
<A
>;
1169 enum SectionType
{ sectionTypeIgnore
, sectionTypeLiteral4
, sectionTypeLiteral8
, sectionTypeLiteral16
,
1170 sectionTypeNonLazy
, sectionTypeCFI
, sectionTypeCString
, sectionTypeCStringPointer
,
1171 sectionTypeUTF16Strings
, sectionTypeCFString
, sectionTypeObjC2ClassRefs
, typeObjC2CategoryList
,
1172 sectionTypeObjC1Classes
, sectionTypeSymboled
, sectionTypeObjC1ClassRefs
,
1173 sectionTypeTentativeDefinitions
, sectionTypeAbsoluteSymbols
, sectionTypeTLVDefs
,
1174 sectionTypeCompactUnwind
, sectionTypeTLVPointers
};
1176 template <typename P
>
1177 struct MachOSectionAndSectionClass
1179 const macho_section
<P
>* sect
;
1182 static int sorter(const void* l
, const void* r
) {
1183 const MachOSectionAndSectionClass
<P
>* left
= (MachOSectionAndSectionClass
<P
>*)l
;
1184 const MachOSectionAndSectionClass
<P
>* right
= (MachOSectionAndSectionClass
<P
>*)r
;
1185 int64_t diff
= left
->sect
->addr() - right
->sect
->addr();
1195 struct ParserAndSectionsArray
{ Parser
* parser
; const uint32_t* sortedSectionsArray
; };
1198 Parser(const uint8_t* fileContent
, uint64_t fileLength
,
1199 const char* path
, time_t modTime
, ld::File::Ordinal ordinal
,
1200 bool warnUnwindConversionProblems
, bool keepDwarfUnwind
,
1201 bool forceDwarfConversion
, bool neverConvertDwarf
,
1202 bool verboseOptimizationHints
, bool ignoreMismatchPlatform
);
1203 ld::relocatable::File
* parse(const ParserOptions
& opts
);
1204 static uint8_t loadCommandSizeMask();
1205 bool parseLoadCommands(Options::Platform platform
, uint32_t minOSVersion
, bool simulator
, bool ignoreMismatchPlatform
);
1206 void makeSections();
1207 void prescanSymbolTable();
1208 void makeSortedSymbolsArray(uint32_t symArray
[], const uint32_t sectionArray
[]);
1209 void makeSortedSectionsArray(uint32_t array
[]);
1210 static int pointerSorter(const void* l
, const void* r
);
1211 static int symbolIndexSorter(void* extra
, const void* l
, const void* r
);
1212 static int sectionIndexSorter(void* extra
, const void* l
, const void* r
);
1214 void parseDebugInfo();
1216 void appendAliasAtoms(uint8_t* atomBuffer
);
1217 static bool isConstFunStabs(const char *stabStr
);
1218 bool read_comp_unit(const char ** name
, const char ** comp_dir
,
1219 uint64_t *stmt_list
);
1220 pint_t
realAddr(pint_t addr
);
1221 const char* getDwarfString(uint64_t form
, const uint8_t*& p
);
1222 uint64_t getDwarfOffset(uint64_t form
, const uint8_t*& di
, bool dwarf64
);
1223 bool skip_form(const uint8_t ** offset
, const uint8_t * end
,
1224 uint64_t form
, uint8_t addr_size
, bool dwarf64
);
1227 // filled in by constructor
1228 const uint8_t* _fileContent
;
1229 uint32_t _fileLength
;
1232 ld::File::Ordinal _ordinal
;
1234 // filled in by parseLoadCommands()
1236 const macho_nlist
<P
>* _symbols
;
1237 uint32_t _symbolCount
;
1238 uint32_t _indirectSymbolCount
;
1239 const char* _strings
;
1240 uint32_t _stringsSize
;
1241 const uint32_t* _indirectTable
;
1242 uint32_t _indirectTableCount
;
1243 uint32_t _undefinedStartIndex
;
1244 uint32_t _undefinedEndIndex
;
1245 const macho_section
<P
>* _sectionsStart
;
1246 uint32_t _machOSectionsCount
;
1248 macho_data_in_code_entry
<P
>* _dataInCodeStart
;
1249 macho_data_in_code_entry
<P
>* _dataInCodeEnd
;
1250 const uint8_t* _lohStart
;
1251 const uint8_t* _lohEnd
;
1253 // filled in by parse()
1254 CFISection
<A
>* _EHFrameSection
;
1255 CUSection
<A
>* _compactUnwindSection
;
1256 AbsoluteSymbolSection
<A
>* _absoluteSection
;
1257 uint32_t _tentativeDefinitionCount
;
1258 uint32_t _absoluteSymbolCount
;
1259 uint32_t _symbolsInSections
;
1260 bool _hasLongBranchStubs
;
1261 bool _AppleObjc
; // FSF has objc that uses different data layout
1262 bool _overlappingSymbols
;
1263 bool _warnUnwindConversionProblems
;
1264 bool _hasDataInCodeLabels
;
1265 bool _keepDwarfUnwind
;
1266 bool _forceDwarfConversion
;
1267 bool _neverConvertDwarf
;
1268 bool _verboseOptimizationHints
;
1269 bool _armUsesZeroCostExceptions
;
1270 bool _ignoreMismatchPlatform
;
1271 bool _treateBitcodeAsData
;
1273 uint8_t _maxDefaultCommonAlignment
;
1274 unsigned int _stubsSectionNum
;
1275 const macho_section
<P
>* _stubsMachOSection
;
1276 std::vector
<const char*> _dtraceProviderInfo
;
1277 std::vector
<FixupInAtom
> _allFixups
;
1282 template <typename A
>
1283 Parser
<A
>::Parser(const uint8_t* fileContent
, uint64_t fileLength
, const char* path
, time_t modTime
,
1284 ld::File::Ordinal ordinal
, bool convertDUI
, bool keepDwarfUnwind
, bool forceDwarfConversion
,
1285 bool neverConvertDwarf
, bool verboseOptimizationHints
, bool ignoreMismatchPlatform
)
1286 : _fileContent(fileContent
), _fileLength(fileLength
), _path(path
), _modTime(modTime
),
1287 _ordinal(ordinal
), _file(NULL
),
1288 _symbols(NULL
), _symbolCount(0), _indirectSymbolCount(0), _strings(NULL
), _stringsSize(0),
1289 _indirectTable(NULL
), _indirectTableCount(0),
1290 _undefinedStartIndex(0), _undefinedEndIndex(0),
1291 _sectionsStart(NULL
), _machOSectionsCount(0), _hasUUID(false),
1292 _dataInCodeStart(NULL
), _dataInCodeEnd(NULL
),
1293 _lohStart(NULL
), _lohEnd(NULL
),
1294 _EHFrameSection(NULL
), _compactUnwindSection(NULL
), _absoluteSection(NULL
),
1295 _tentativeDefinitionCount(0), _absoluteSymbolCount(0),
1296 _symbolsInSections(0), _hasLongBranchStubs(false), _AppleObjc(false),
1297 _overlappingSymbols(false), _warnUnwindConversionProblems(convertDUI
), _hasDataInCodeLabels(false),
1298 _keepDwarfUnwind(keepDwarfUnwind
), _forceDwarfConversion(forceDwarfConversion
),
1299 _neverConvertDwarf(neverConvertDwarf
),
1300 _verboseOptimizationHints(verboseOptimizationHints
),
1301 _ignoreMismatchPlatform(ignoreMismatchPlatform
),
1302 _stubsSectionNum(0), _stubsMachOSection(NULL
)
1308 bool Parser
<x86
>::validFile(const uint8_t* fileContent
, bool, cpu_subtype_t
)
1310 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1311 if ( header
->magic() != MH_MAGIC
)
1313 if ( header
->cputype() != CPU_TYPE_I386
)
1315 if ( header
->filetype() != MH_OBJECT
)
1321 bool Parser
<x86_64
>::validFile(const uint8_t* fileContent
, bool, cpu_subtype_t
)
1323 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1324 if ( header
->magic() != MH_MAGIC_64
)
1326 if ( header
->cputype() != CPU_TYPE_X86_64
)
1328 if ( header
->filetype() != MH_OBJECT
)
1334 bool Parser
<arm
>::validFile(const uint8_t* fileContent
, bool subtypeMustMatch
, cpu_subtype_t subtype
)
1336 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1337 if ( header
->magic() != MH_MAGIC
)
1339 if ( header
->cputype() != CPU_TYPE_ARM
)
1341 if ( header
->filetype() != MH_OBJECT
)
1343 if ( subtypeMustMatch
) {
1344 if ( (cpu_subtype_t
)header
->cpusubtype() == subtype
)
1346 // hack until libcc_kext.a is made fat
1347 if ( header
->cpusubtype() == CPU_SUBTYPE_ARM_ALL
)
1356 bool Parser
<arm64
>::validFile(const uint8_t* fileContent
, bool subtypeMustMatch
, cpu_subtype_t subtype
)
1358 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1359 if ( header
->magic() != MH_MAGIC_64
)
1361 if ( header
->cputype() != CPU_TYPE_ARM64
)
1363 if ( header
->filetype() != MH_OBJECT
)
1370 const char* Parser
<x86
>::fileKind(const uint8_t* fileContent
)
1372 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1373 if ( header
->magic() != MH_MAGIC
)
1375 if ( header
->cputype() != CPU_TYPE_I386
)
1381 const char* Parser
<x86_64
>::fileKind(const uint8_t* fileContent
)
1383 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1384 if ( header
->magic() != MH_MAGIC_64
)
1386 if ( header
->cputype() != CPU_TYPE_X86_64
)
1392 const char* Parser
<arm
>::fileKind(const uint8_t* fileContent
)
1394 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1395 if ( header
->magic() != MH_MAGIC
)
1397 if ( header
->cputype() != CPU_TYPE_ARM
)
1399 for (const ArchInfo
* t
=archInfoArray
; t
->archName
!= NULL
; ++t
) {
1400 if ( (t
->cpuType
== CPU_TYPE_ARM
) && ((cpu_subtype_t
)header
->cpusubtype() == t
->cpuSubType
) ) {
1407 #if SUPPORT_ARCH_arm64
1409 const char* Parser
<arm64
>::fileKind(const uint8_t* fileContent
)
1411 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1412 if ( header
->magic() != MH_MAGIC_64
)
1414 if ( header
->cputype() != CPU_TYPE_ARM64
)
1421 template <typename A
>
1422 bool Parser
<A
>::hasObjC2Categories(const uint8_t* fileContent
)
1424 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1425 const uint32_t cmd_count
= header
->ncmds();
1426 const macho_load_command
<P
>* const cmds
= (macho_load_command
<P
>*)((char*)header
+ sizeof(macho_header
<P
>));
1427 const macho_load_command
<P
>* const cmdsEnd
= (macho_load_command
<P
>*)((char*)header
+ sizeof(macho_header
<P
>) + header
->sizeofcmds());
1428 const macho_load_command
<P
>* cmd
= cmds
;
1429 for (uint32_t i
= 0; i
< cmd_count
; ++i
) {
1430 if ( cmd
->cmd() == macho_segment_command
<P
>::CMD
) {
1431 const macho_segment_command
<P
>* segment
= (macho_segment_command
<P
>*)cmd
;
1432 const macho_section
<P
>* sectionsStart
= (macho_section
<P
>*)((char*)segment
+ sizeof(macho_segment_command
<P
>));
1433 for (uint32_t si
=0; si
< segment
->nsects(); ++si
) {
1434 const macho_section
<P
>* sect
= §ionsStart
[si
];
1435 if ( (sect
->size() > 0)
1436 && (strcmp(sect
->sectname(), "__objc_catlist") == 0)
1437 && (strcmp(sect
->segname(), "__DATA") == 0) ) {
1442 cmd
= (const macho_load_command
<P
>*)(((char*)cmd
)+cmd
->cmdsize());
1443 if ( cmd
> cmdsEnd
)
1444 throwf("malformed mach-o file, load command #%d is outside size of load commands", i
);
1450 template <typename A
>
1451 bool Parser
<A
>::hasObjC1Categories(const uint8_t* fileContent
)
1453 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1454 const uint32_t cmd_count
= header
->ncmds();
1455 const macho_load_command
<P
>* const cmds
= (macho_load_command
<P
>*)((char*)header
+ sizeof(macho_header
<P
>));
1456 const macho_load_command
<P
>* const cmdsEnd
= (macho_load_command
<P
>*)((char*)header
+ sizeof(macho_header
<P
>) + header
->sizeofcmds());
1457 const macho_load_command
<P
>* cmd
= cmds
;
1458 for (uint32_t i
= 0; i
< cmd_count
; ++i
) {
1459 if ( cmd
->cmd() == macho_segment_command
<P
>::CMD
) {
1460 const macho_segment_command
<P
>* segment
= (macho_segment_command
<P
>*)cmd
;
1461 const macho_section
<P
>* sectionsStart
= (macho_section
<P
>*)((char*)segment
+ sizeof(macho_segment_command
<P
>));
1462 for (uint32_t si
=0; si
< segment
->nsects(); ++si
) {
1463 const macho_section
<P
>* sect
= §ionsStart
[si
];
1464 if ( (sect
->size() > 0)
1465 && (strcmp(sect
->sectname(), "__category") == 0)
1466 && (strcmp(sect
->segname(), "__OBJC") == 0) ) {
1471 cmd
= (const macho_load_command
<P
>*)(((char*)cmd
)+cmd
->cmdsize());
1472 if ( cmd
> cmdsEnd
)
1473 throwf("malformed mach-o file, load command #%d is outside size of load commands", i
);
1479 template <typename A
>
1480 bool Parser
<A
>::getNonLocalSymbols(const uint8_t* fileContent
, std::vector
<const char*> &syms
)
1482 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1483 const uint32_t cmd_count
= header
->ncmds();
1484 const macho_load_command
<P
>* const cmds
= (macho_load_command
<P
>*)((char*)header
+ sizeof(macho_header
<P
>));
1485 const macho_load_command
<P
>* const cmdsEnd
= (macho_load_command
<P
>*)((char*)header
+ sizeof(macho_header
<P
>) + header
->sizeofcmds());
1486 const macho_load_command
<P
>* cmd
= cmds
;
1487 for (uint32_t i
= 0; i
< cmd_count
; ++i
) {
1488 if ( cmd
->cmd() == LC_SYMTAB
) {
1489 const macho_symtab_command
<P
>* symtab
= (macho_symtab_command
<P
>*)cmd
;
1490 uint32_t symbolCount
= symtab
->nsyms();
1491 const macho_nlist
<P
>* symbols
= (const macho_nlist
<P
>*)(fileContent
+ symtab
->symoff());
1492 const char* strings
= (char*)fileContent
+ symtab
->stroff();
1493 for (uint32_t j
= 0; j
< symbolCount
; ++j
) {
1494 // ignore stabs and count only ext symbols
1495 if ( (symbols
[j
].n_type() & N_STAB
) == 0 &&
1496 (symbols
[j
].n_type() & N_EXT
) != 0 ) {
1497 const char* symName
= &strings
[symbols
[j
].n_strx()];
1498 syms
.push_back(symName
);
1503 cmd
= (const macho_load_command
<P
>*)(((char*)cmd
)+cmd
->cmdsize());
1504 if ( cmd
> cmdsEnd
)
1505 throwf("malformed mach-o file, load command #%d is outside size of load commands", i
);
1511 template <typename A
>
1512 int Parser
<A
>::pointerSorter(const void* l
, const void* r
)
1514 // sort references by address
1515 const pint_t
* left
= (pint_t
*)l
;
1516 const pint_t
* right
= (pint_t
*)r
;
1517 return (*left
- *right
);
1520 template <typename A
>
1521 typename
A::P::uint_t Parser
<A
>::LabelAndCFIBreakIterator::peek(Parser
<A
>& parser
, pint_t startAddr
, pint_t endAddr
)
1524 if ( symIndex
< sortedSymbolCount
)
1525 symbolAddr
= parser
.symbolFromIndex(sortedSymbolIndexes
[symIndex
]).n_value();
1527 symbolAddr
= endAddr
;
1529 if ( cfiIndex
< cfiStartsCount
)
1530 cfiAddr
= cfiStartsArray
[cfiIndex
];
1533 if ( (cfiAddr
< symbolAddr
) && (cfiAddr
>= startAddr
) ) {
1534 if ( cfiAddr
< endAddr
)
1540 if ( symbolAddr
< endAddr
)
1548 // Parses up a section into chunks based on labels and CFI information.
1549 // Each call returns the next chunk address and size, and (if the break
1550 // was becuase of a label, the symbol). Returns false when no more chunks.
1552 template <typename A
>
1553 bool Parser
<A
>::LabelAndCFIBreakIterator::next(Parser
<A
>& parser
, const Section
<A
>& sect
, uint32_t sectNum
, pint_t startAddr
, pint_t endAddr
,
1554 pint_t
* addr
, pint_t
* size
, const macho_nlist
<P
>** symbol
)
1556 // may not be a label on start of section, but need atom demarcation there
1559 // advance symIndex until we get to the first label at or past the start of this section
1560 while ( symIndex
< sortedSymbolCount
) {
1561 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(sortedSymbolIndexes
[symIndex
]);
1562 if ( ! sect
.ignoreLabel(parser
.nameFromSymbol(sym
)) ) {
1563 pint_t nextSymbolAddr
= sym
.n_value();
1564 //fprintf(stderr, "sectNum=%d, nextSymbolAddr=0x%08llX, name=%s\n", sectNum, (uint64_t)nextSymbolAddr, parser.nameFromSymbol(sym));
1565 if ( (nextSymbolAddr
> startAddr
) || ((nextSymbolAddr
== startAddr
) && (sym
.n_sect() == sectNum
)) )
1570 if ( symIndex
< sortedSymbolCount
) {
1571 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(sortedSymbolIndexes
[symIndex
]);
1572 pint_t nextSymbolAddr
= sym
.n_value();
1573 // if next symbol found is not in this section
1574 if ( sym
.n_sect() != sectNum
) {
1575 // check for CFI break instead of symbol break
1576 if ( cfiIndex
< cfiStartsCount
) {
1577 pint_t nextCfiAddr
= cfiStartsArray
[cfiIndex
];
1578 if ( nextCfiAddr
< endAddr
) {
1581 *addr
= nextCfiAddr
;
1582 *size
= peek(parser
, startAddr
, endAddr
) - nextCfiAddr
;
1588 *size
= endAddr
- startAddr
;
1590 if ( startAddr
== endAddr
)
1591 return false; // zero size section
1593 return true; // whole section is one atom with no label
1595 // if also CFI break here, eat it
1596 if ( cfiIndex
< cfiStartsCount
) {
1597 if ( cfiStartsArray
[cfiIndex
] == nextSymbolAddr
)
1600 if ( nextSymbolAddr
== startAddr
) {
1601 // label at start of section, return it as chunk
1604 *size
= peek(parser
, startAddr
, endAddr
) - startAddr
;
1608 // return chunk before first symbol
1610 *size
= nextSymbolAddr
- startAddr
;
1614 // no symbols in section, check CFI
1615 if ( cfiIndex
< cfiStartsCount
) {
1616 pint_t nextCfiAddr
= cfiStartsArray
[cfiIndex
];
1617 if ( nextCfiAddr
< endAddr
) {
1620 *addr
= nextCfiAddr
;
1621 *size
= peek(parser
, startAddr
, endAddr
) - nextCfiAddr
;
1626 // no cfi, so whole section is one chunk
1628 *size
= endAddr
- startAddr
;
1630 if ( startAddr
== endAddr
)
1631 return false; // zero size section
1633 return true; // whole section is one atom with no label
1636 while ( (symIndex
< sortedSymbolCount
) && (cfiIndex
< cfiStartsCount
) ) {
1637 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(sortedSymbolIndexes
[symIndex
]);
1638 pint_t nextSymbolAddr
= sym
.n_value();
1639 pint_t nextCfiAddr
= cfiStartsArray
[cfiIndex
];
1640 if ( nextSymbolAddr
< nextCfiAddr
) {
1641 if ( nextSymbolAddr
>= endAddr
)
1644 if ( nextSymbolAddr
< startAddr
)
1646 *addr
= nextSymbolAddr
;
1647 *size
= peek(parser
, startAddr
, endAddr
) - nextSymbolAddr
;
1651 else if ( nextCfiAddr
< nextSymbolAddr
) {
1652 if ( nextCfiAddr
>= endAddr
)
1655 if ( nextCfiAddr
< startAddr
)
1657 *addr
= nextCfiAddr
;
1658 *size
= peek(parser
, startAddr
, endAddr
) - nextCfiAddr
;
1663 if ( nextCfiAddr
>= endAddr
)
1667 if ( nextCfiAddr
< startAddr
)
1669 *addr
= nextCfiAddr
;
1670 *size
= peek(parser
, startAddr
, endAddr
) - nextCfiAddr
;
1675 while ( symIndex
< sortedSymbolCount
) {
1676 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(sortedSymbolIndexes
[symIndex
]);
1677 pint_t nextSymbolAddr
= sym
.n_value();
1678 // if next symbol found is not in this section, then done with iteration
1679 if ( sym
.n_sect() != sectNum
)
1682 if ( nextSymbolAddr
< startAddr
)
1684 *addr
= nextSymbolAddr
;
1685 *size
= peek(parser
, startAddr
, endAddr
) - nextSymbolAddr
;
1689 while ( cfiIndex
< cfiStartsCount
) {
1690 pint_t nextCfiAddr
= cfiStartsArray
[cfiIndex
];
1691 if ( nextCfiAddr
>= endAddr
)
1694 if ( nextCfiAddr
< startAddr
)
1696 *addr
= nextCfiAddr
;
1697 *size
= peek(parser
, startAddr
, endAddr
) - nextCfiAddr
;
1705 typename
arm::P::uint_t Parser
<arm
>::realAddr(typename
arm::P::uint_t addr
)
1710 template <typename A
>
1711 typename
A::P::uint_t Parser
<A
>::realAddr(typename
A::P::uint_t addr
)
1716 #define STACK_ALLOC_IF_SMALL(_type, _name, _actual_count, _maxCount) \
1717 _type* _name = NULL; \
1718 uint32_t _name##_count = 1; \
1719 if ( _actual_count > _maxCount ) \
1720 _name = (_type*)malloc(sizeof(_type) * _actual_count); \
1722 _name##_count = _actual_count; \
1723 _type _name##_buffer[_name##_count]; \
1724 if ( _name == NULL ) \
1725 _name = _name##_buffer;
1728 template <typename A
>
1729 ld::relocatable::File
* Parser
<A
>::parse(const ParserOptions
& opts
)
1731 // create file object
1732 _file
= new File
<A
>(_path
, _modTime
, _fileContent
, _ordinal
);
1735 _file
->_srcKind
= opts
.srcKind
;
1736 // set treatBitcodeAsData
1737 _treateBitcodeAsData
= opts
.treateBitcodeAsData
;
1738 _usingBitcode
= opts
.usingBitcode
;
1740 // respond to -t option
1741 if ( opts
.logAllFiles
)
1742 printf("%s\n", _path
);
1744 _armUsesZeroCostExceptions
= opts
.armUsesZeroCostExceptions
;
1745 _maxDefaultCommonAlignment
= opts
.maxDefaultCommonAlignment
;
1747 // parse start of mach-o file
1748 if ( ! parseLoadCommands(opts
.platform
, opts
.minOSVersion
, opts
.simulator
, opts
.ignoreMismatchPlatform
) )
1752 uint32_t sortedSectionIndexes
[_machOSectionsCount
];
1753 this->makeSortedSectionsArray(sortedSectionIndexes
);
1755 // make symbol table sorted by address
1756 this->prescanSymbolTable();
1757 uint32_t sortedSymbolIndexes
[_symbolsInSections
];
1758 this->makeSortedSymbolsArray(sortedSymbolIndexes
, sortedSectionIndexes
);
1760 // allocate Section<A> object for each mach-o section
1763 // if it exists, do special early parsing of __compact_unwind section
1764 uint32_t countOfCUs
= 0;
1765 if ( _compactUnwindSection
!= NULL
)
1766 countOfCUs
= _compactUnwindSection
->count();
1767 // stack allocate (if not too large) cuInfoBuffer
1768 STACK_ALLOC_IF_SMALL(typename CUSection
<A
>::Info
, cuInfoArray
, countOfCUs
, 1024);
1769 if ( countOfCUs
!= 0 )
1770 _compactUnwindSection
->parse(*this, countOfCUs
, cuInfoArray
);
1772 // create lists of address that already have compact unwind and thus don't need the dwarf parsed
1773 unsigned cuLsdaCount
= 0;
1774 pint_t cuStarts
[countOfCUs
];
1775 for (uint32_t i
=0; i
< countOfCUs
; ++i
) {
1776 if ( CUSection
<A
>::encodingMeansUseDwarf(cuInfoArray
[i
].compactUnwindInfo
) )
1779 cuStarts
[i
] = cuInfoArray
[i
].functionStartAddress
;
1780 if ( cuInfoArray
[i
].lsdaAddress
!= 0 )
1785 // if it exists, do special early parsing of __eh_frame section
1786 // stack allocate (if not too large) array of CFI_Atom_Info
1787 uint32_t countOfCFIs
= 0;
1788 if ( _EHFrameSection
!= NULL
)
1789 countOfCFIs
= _EHFrameSection
->cfiCount(*this);
1790 STACK_ALLOC_IF_SMALL(typename CFISection
<A
>::CFI_Atom_Info
, cfiArray
, countOfCFIs
, 1024);
1792 // stack allocate (if not too large) a copy of __eh_frame to apply relocations to
1793 uint32_t sectSize
= 4;
1794 if ( (countOfCFIs
!= 0) && _EHFrameSection
->needsRelocating() )
1795 sectSize
= _EHFrameSection
->machoSection()->size()+4;
1796 STACK_ALLOC_IF_SMALL(uint8_t, ehBuffer
, sectSize
, 50*1024);
1797 uint32_t cfiStartsCount
= 0;
1798 if ( countOfCFIs
!= 0 ) {
1799 _EHFrameSection
->cfiParse(*this, ehBuffer
, cfiArray
, countOfCFIs
, cuStarts
, countOfCUs
);
1800 // count functions and lsdas
1801 for(uint32_t i
=0; i
< countOfCFIs
; ++i
) {
1802 if ( cfiArray
[i
].isCIE
)
1804 //fprintf(stderr, "cfiArray[i].func = 0x%08llX, cfiArray[i].lsda = 0x%08llX, encoding=0x%08X\n",
1805 // (uint64_t)cfiArray[i].u.fdeInfo.function.targetAddress,
1806 // (uint64_t)cfiArray[i].u.fdeInfo.lsda.targetAddress,
1807 // cfiArray[i].u.fdeInfo.compactUnwindInfo);
1808 if ( cfiArray
[i
].u
.fdeInfo
.function
.targetAddress
!= CFI_INVALID_ADDRESS
)
1810 if ( cfiArray
[i
].u
.fdeInfo
.lsda
.targetAddress
!= CFI_INVALID_ADDRESS
)
1814 CFI_CU_InfoArrays
cfis(cfiArray
, countOfCFIs
, cuInfoArray
, countOfCUs
);
1816 // create sorted array of function starts and lsda starts
1817 pint_t cfiStartsArray
[cfiStartsCount
+cuLsdaCount
];
1818 uint32_t countOfFDEs
= 0;
1819 uint32_t cfiStartsArrayCount
= 0;
1820 if ( countOfCFIs
!= 0 ) {
1821 for(uint32_t i
=0; i
< countOfCFIs
; ++i
) {
1822 if ( cfiArray
[i
].isCIE
)
1824 if ( cfiArray
[i
].u
.fdeInfo
.function
.targetAddress
!= CFI_INVALID_ADDRESS
)
1825 cfiStartsArray
[cfiStartsArrayCount
++] = realAddr(cfiArray
[i
].u
.fdeInfo
.function
.targetAddress
);
1826 if ( cfiArray
[i
].u
.fdeInfo
.lsda
.targetAddress
!= CFI_INVALID_ADDRESS
)
1827 cfiStartsArray
[cfiStartsArrayCount
++] = cfiArray
[i
].u
.fdeInfo
.lsda
.targetAddress
;
1831 if ( cuLsdaCount
!= 0 ) {
1832 // merge in an lsda info from compact unwind
1833 for (uint32_t i
=0; i
< countOfCUs
; ++i
) {
1834 if ( cuInfoArray
[i
].lsdaAddress
== 0 )
1836 // append to cfiStartsArray if not already in that list
1838 for(uint32_t j
=0; j
< cfiStartsArrayCount
; ++j
) {
1839 if ( cfiStartsArray
[j
] == cuInfoArray
[i
].lsdaAddress
)
1843 cfiStartsArray
[cfiStartsArrayCount
++] = cuInfoArray
[i
].lsdaAddress
;
1847 if ( cfiStartsArrayCount
!= 0 ) {
1848 ::qsort(cfiStartsArray
, cfiStartsArrayCount
, sizeof(pint_t
), pointerSorter
);
1850 // scan for FDEs claming the same function
1851 for(uint32_t i
=1; i
< cfiStartsArrayCount
; ++i
) {
1852 assert( cfiStartsArray
[i
] != cfiStartsArray
[i
-1] );
1857 Section
<A
>** sections
= _file
->_sectionsArray
;
1858 uint32_t sectionsCount
= _file
->_sectionsArrayCount
;
1860 // figure out how many atoms will be allocated and allocate
1861 LabelAndCFIBreakIterator
breakIterator(sortedSymbolIndexes
, _symbolsInSections
, cfiStartsArray
,
1862 cfiStartsArrayCount
, _overlappingSymbols
);
1863 uint32_t computedAtomCount
= 0;
1864 for (uint32_t i
=0; i
< sectionsCount
; ++i
) {
1865 breakIterator
.beginSection();
1866 uint32_t count
= sections
[i
]->computeAtomCount(*this, breakIterator
, cfis
);
1867 //const macho_section<P>* sect = sections[i]->machoSection();
1868 //fprintf(stderr, "computed count=%u for section %s size=%llu\n", count, sect->sectname(), (sect != NULL) ? sect->size() : 0);
1869 computedAtomCount
+= count
;
1871 //fprintf(stderr, "allocating %d atoms * sizeof(Atom<A>)=%ld, sizeof(ld::Atom)=%ld\n", computedAtomCount, sizeof(Atom<A>), sizeof(ld::Atom));
1872 _file
->_atomsArray
= new uint8_t[computedAtomCount
*sizeof(Atom
<A
>)];
1873 _file
->_atomsArrayCount
= 0;
1875 // have each section append atoms to _atomsArray
1876 LabelAndCFIBreakIterator
breakIterator2(sortedSymbolIndexes
, _symbolsInSections
, cfiStartsArray
,
1877 cfiStartsArrayCount
, _overlappingSymbols
);
1878 for (uint32_t i
=0; i
< sectionsCount
; ++i
) {
1879 uint8_t* atoms
= _file
->_atomsArray
+ _file
->_atomsArrayCount
*sizeof(Atom
<A
>);
1880 breakIterator2
.beginSection();
1881 uint32_t count
= sections
[i
]->appendAtoms(*this, atoms
, breakIterator2
, cfis
);
1882 //fprintf(stderr, "append count=%u for section %s/%s\n", count, sections[i]->machoSection()->segname(), sections[i]->machoSection()->sectname());
1883 _file
->_atomsArrayCount
+= count
;
1885 assert( _file
->_atomsArrayCount
== computedAtomCount
&& "more atoms allocated than expected");
1888 // have each section add all fix-ups for its atoms
1889 _allFixups
.reserve(computedAtomCount
*5);
1890 for (uint32_t i
=0; i
< sectionsCount
; ++i
)
1891 sections
[i
]->makeFixups(*this, cfis
);
1893 // assign fixups start offset for each atom
1894 uint8_t* p
= _file
->_atomsArray
;
1895 uint32_t fixupOffset
= 0;
1896 for(int i
=_file
->_atomsArrayCount
; i
> 0; --i
) {
1897 Atom
<A
>* atom
= (Atom
<A
>*)p
;
1898 atom
->_fixupsStartIndex
= fixupOffset
;
1899 fixupOffset
+= atom
->_fixupsCount
;
1900 atom
->_fixupsCount
= 0;
1901 p
+= sizeof(Atom
<A
>);
1903 assert(fixupOffset
== _allFixups
.size());
1904 _file
->_fixups
.resize(fixupOffset
);
1906 // copy each fixup for each atom
1907 for(typename
std::vector
<FixupInAtom
>::iterator it
=_allFixups
.begin(); it
!= _allFixups
.end(); ++it
) {
1908 uint32_t slot
= it
->atom
->_fixupsStartIndex
+ it
->atom
->_fixupsCount
;
1909 _file
->_fixups
[slot
] = it
->fixup
;
1910 it
->atom
->_fixupsCount
++;
1913 // done with temp vector
1917 _file
->_unwindInfos
.reserve(countOfFDEs
+countOfCUs
);
1918 for(uint32_t i
=0; i
< countOfCFIs
; ++i
) {
1919 if ( cfiArray
[i
].isCIE
)
1921 if ( cfiArray
[i
].u
.fdeInfo
.function
.targetAddress
!= CFI_INVALID_ADDRESS
) {
1922 ld::Atom::UnwindInfo info
;
1923 info
.startOffset
= 0;
1924 info
.unwindInfo
= cfiArray
[i
].u
.fdeInfo
.compactUnwindInfo
;
1925 _file
->_unwindInfos
.push_back(info
);
1926 Atom
<A
>* func
= findAtomByAddress(cfiArray
[i
].u
.fdeInfo
.function
.targetAddress
);
1927 func
->setUnwindInfoRange(_file
->_unwindInfos
.size()-1, 1);
1928 //fprintf(stderr, "cu from dwarf =0x%08X, atom=%s\n", info.unwindInfo, func->name());
1931 // apply compact infos in __LD,__compact_unwind section to each function
1932 // if function also has dwarf unwind, CU will override it
1933 Atom
<A
>* lastFunc
= NULL
;
1934 uint32_t lastEnd
= 0;
1935 for(uint32_t i
=0; i
< countOfCUs
; ++i
) {
1936 typename CUSection
<A
>::Info
* info
= &cuInfoArray
[i
];
1937 assert(info
->function
!= NULL
);
1938 ld::Atom::UnwindInfo ui
;
1939 ui
.startOffset
= info
->functionStartAddress
- info
->function
->objectAddress();
1940 ui
.unwindInfo
= info
->compactUnwindInfo
;
1941 _file
->_unwindInfos
.push_back(ui
);
1942 // don't override with converted cu with "use dwarf" cu, if forcing dwarf conversion
1943 if ( !_forceDwarfConversion
|| !CUSection
<A
>::encodingMeansUseDwarf(info
->compactUnwindInfo
) ) {
1944 //fprintf(stderr, "cu=0x%08X, atom=%s\n", ui.unwindInfo, info->function->name());
1945 // if previous is for same function, extend range
1946 if ( info
->function
== lastFunc
) {
1947 if ( lastEnd
!= ui
.startOffset
) {
1948 if ( lastEnd
< ui
.startOffset
)
1949 warning("__LD,__compact_unwind entries for %s have a gap at offset 0x%0X", info
->function
->name(), lastEnd
);
1951 warning("__LD,__compact_unwind entries for %s overlap at offset 0x%0X", info
->function
->name(), lastEnd
);
1953 lastFunc
->extendUnwindInfoRange();
1956 info
->function
->setUnwindInfoRange(_file
->_unwindInfos
.size()-1, 1);
1957 lastFunc
= info
->function
;
1958 lastEnd
= ui
.startOffset
+ info
->rangeLength
;
1962 // process indirect symbols which become AliasAtoms
1963 _file
->_aliasAtomsArray
= NULL
;
1964 _file
->_aliasAtomsArrayCount
= 0;
1965 if ( _indirectSymbolCount
!= 0 ) {
1966 _file
->_aliasAtomsArrayCount
= _indirectSymbolCount
;
1967 _file
->_aliasAtomsArray
= new uint8_t[_file
->_aliasAtomsArrayCount
*sizeof(AliasAtom
)];
1968 this->appendAliasAtoms(_file
->_aliasAtomsArray
);
1972 // parse dwarf debug info to get line info
1973 this->parseDebugInfo();
1978 static void versionToString(uint32_t value
, char buffer
[32])
1981 sprintf(buffer
, "%d.%d.%d", value
>> 16, (value
>> 8) & 0xFF, value
& 0xFF);
1983 sprintf(buffer
, "%d.%d", value
>> 16, (value
>> 8) & 0xFF);
1986 template <> uint8_t Parser
<x86
>::loadCommandSizeMask() { return 0x03; }
1987 template <> uint8_t Parser
<x86_64
>::loadCommandSizeMask() { return 0x07; }
1988 template <> uint8_t Parser
<arm
>::loadCommandSizeMask() { return 0x03; }
1989 template <> uint8_t Parser
<arm64
>::loadCommandSizeMask() { return 0x07; }
1991 template <typename A
>
1992 bool Parser
<A
>::parseLoadCommands(Options::Platform platform
, uint32_t linkMinOSVersion
, bool simulator
, bool ignoreMismatchPlatform
)
1994 const macho_header
<P
>* header
= (const macho_header
<P
>*)_fileContent
;
1996 // set File attributes
1997 _file
->_canScatterAtoms
= (header
->flags() & MH_SUBSECTIONS_VIA_SYMBOLS
);
1998 _file
->_cpuSubType
= header
->cpusubtype();
2000 const macho_segment_command
<P
>* segment
= NULL
;
2001 const uint8_t* const endOfFile
= _fileContent
+ _fileLength
;
2002 const uint32_t cmd_count
= header
->ncmds();
2003 // <rdar://problem/5394172> an empty .o file with zero load commands will crash linker
2004 if ( cmd_count
== 0 )
2006 Options::Platform lcPlatform
= Options::kPlatformUnknown
;
2007 const macho_load_command
<P
>* const cmds
= (macho_load_command
<P
>*)((char*)header
+ sizeof(macho_header
<P
>));
2008 const macho_load_command
<P
>* const cmdsEnd
= (macho_load_command
<P
>*)((char*)header
+ sizeof(macho_header
<P
>) + header
->sizeofcmds());
2009 const macho_load_command
<P
>* cmd
= cmds
;
2010 for (uint32_t i
= 0; i
< cmd_count
; ++i
) {
2011 uint32_t size
= cmd
->cmdsize();
2012 if ( (size
& this->loadCommandSizeMask()) != 0 )
2013 throwf("load command #%d has a unaligned size", i
);
2014 const uint8_t* endOfCmd
= ((uint8_t*)cmd
)+cmd
->cmdsize();
2015 if ( endOfCmd
> (uint8_t*)cmdsEnd
)
2016 throwf("load command #%d extends beyond the end of the load commands", i
);
2017 if ( endOfCmd
> endOfFile
)
2018 throwf("load command #%d extends beyond the end of the file", i
);
2019 switch (cmd
->cmd()) {
2022 const macho_symtab_command
<P
>* symtab
= (macho_symtab_command
<P
>*)cmd
;
2023 _symbolCount
= symtab
->nsyms();
2024 _symbols
= (const macho_nlist
<P
>*)(_fileContent
+ symtab
->symoff());
2025 _strings
= (char*)_fileContent
+ symtab
->stroff();
2026 _stringsSize
= symtab
->strsize();
2027 if ( (symtab
->symoff() + _symbolCount
*sizeof(macho_nlist
<P
>)) > _fileLength
)
2028 throw "mach-o symbol table extends beyond end of file";
2029 if ( (_strings
+ _stringsSize
) > (char*)endOfFile
)
2030 throw "mach-o string pool extends beyond end of file";
2031 if ( _indirectTable
== NULL
) {
2032 if ( _undefinedEndIndex
== 0 ) {
2033 _undefinedStartIndex
= 0;
2034 _undefinedEndIndex
= symtab
->nsyms();
2041 const macho_dysymtab_command
<P
>* dsymtab
= (macho_dysymtab_command
<P
>*)cmd
;
2042 _indirectTable
= (uint32_t*)(_fileContent
+ dsymtab
->indirectsymoff());
2043 _indirectTableCount
= dsymtab
->nindirectsyms();
2044 if ( &_indirectTable
[_indirectTableCount
] > (uint32_t*)endOfFile
)
2045 throw "indirect symbol table extends beyond end of file";
2046 _undefinedStartIndex
= dsymtab
->iundefsym();
2047 _undefinedEndIndex
= _undefinedStartIndex
+ dsymtab
->nundefsym();
2053 case LC_DATA_IN_CODE
:
2055 const macho_linkedit_data_command
<P
>* dc
= (macho_linkedit_data_command
<P
>*)cmd
;
2056 _dataInCodeStart
= (macho_data_in_code_entry
<P
>*)(_fileContent
+ dc
->dataoff());
2057 _dataInCodeEnd
= (macho_data_in_code_entry
<P
>*)(_fileContent
+ dc
->dataoff() + dc
->datasize());
2058 if ( _dataInCodeEnd
> (macho_data_in_code_entry
<P
>*)endOfFile
)
2059 throw "LC_DATA_IN_CODE table extends beyond end of file";
2062 case LC_LINKER_OPTION
:
2064 const macho_linker_option_command
<P
>* loc
= (macho_linker_option_command
<P
>*)cmd
;
2065 const char* buffer
= loc
->buffer();
2066 _file
->_linkerOptions
.resize(_file
->_linkerOptions
.size() + 1);
2067 std::vector
<const char*>& vec
= _file
->_linkerOptions
.back();
2068 for (uint32_t j
=0; j
< loc
->count(); ++j
) {
2069 vec
.push_back(buffer
);
2070 buffer
+= strlen(buffer
) + 1;
2072 if ( buffer
> ((char*)cmd
+ loc
->cmdsize()) )
2073 throw "malformed LC_LINKER_OPTION";
2076 case LC_LINKER_OPTIMIZATION_HINTS
:
2078 const macho_linkedit_data_command
<P
>* loh
= (macho_linkedit_data_command
<P
>*)cmd
;
2079 _lohStart
= _fileContent
+ loh
->dataoff();
2080 _lohEnd
= _fileContent
+ loh
->dataoff() + loh
->datasize();
2081 if ( _lohEnd
> endOfFile
)
2082 throw "LC_LINKER_OPTIMIZATION_HINTS table extends beyond end of file";
2085 case LC_VERSION_MIN_MACOSX
:
2086 case LC_VERSION_MIN_IPHONEOS
:
2087 case LC_VERSION_MIN_WATCHOS
:
2088 #if SUPPORT_APPLE_TV
2089 case LC_VERSION_MIN_TVOS
:
2091 if ( ignoreMismatchPlatform
)
2093 _file
->_platform
= cmd
->cmd();
2094 lcPlatform
= Options::platformForLoadCommand(cmd
->cmd());
2095 _file
->_minOSVersion
= ((macho_version_min_command
<P
>*)cmd
)->version();
2097 case macho_segment_command
<P
>::CMD
:
2098 if ( segment
!= NULL
)
2099 throw "more than one LC_SEGMENT found in object file";
2100 segment
= (macho_segment_command
<P
>*)cmd
;
2103 // ignore unknown load commands
2106 cmd
= (const macho_load_command
<P
>*)(((char*)cmd
)+cmd
->cmdsize());
2107 if ( cmd
> cmdsEnd
)
2108 throwf("malformed mach-o file, load command #%d is outside size of load commands", i
);
2110 // arm/arm64 objects are default to ios platform if not set.
2111 // rdar://problem/21746314
2112 if (lcPlatform
== Options::kPlatformUnknown
&&
2113 (std::is_same
<A
, arm
>::value
|| std::is_same
<A
, arm64
>::value
))
2114 lcPlatform
= Options::kPlatformiOS
;
2116 // Check platform cross-linking.
2117 if ( !ignoreMismatchPlatform
) {
2118 if ( lcPlatform
!= platform
) {
2120 case Options::kPlatformOSX
:
2121 case Options::kPlatformiOS
:
2122 if ( lcPlatform
== Options::kPlatformUnknown
)
2124 // fall through if the Platform is not Unknown
2125 case Options::kPlatformWatchOS
:
2126 // Error when using bitcocde, warning otherwise.
2128 throwf("building for %s%s, but linking in object file built for %s,",
2129 Options::platformName(platform
), (simulator
? " simulator" : ""),
2130 Options::platformName(lcPlatform
));
2132 warning("URGENT: building for %s%s, but linking in object file (%s) built for %s. "
2133 "Note: This will be an error in the future.",
2134 Options::platformName(platform
), (simulator
? " simulator" : ""), path(),
2135 Options::platformName(lcPlatform
));
2137 #if SUPPORT_APPLE_TV
2138 case Options::kPlatform_tvOS
:
2139 // Error when using bitcocde, warning otherwise.
2141 throwf("building for %s%s, but linking in object file built for %s,",
2142 Options::platformName(platform
), (simulator
? " simulator" : ""),
2143 Options::platformName(lcPlatform
));
2145 warning("URGENT: building for %s%s, but linking in object file (%s) built for %s. "
2146 "Note: This will be an error in the future.",
2147 Options::platformName(platform
), (simulator
? " simulator" : ""), path(),
2148 Options::platformName(lcPlatform
));
2151 case Options::kPlatformUnknown
:
2152 // skip if the target platform is unknown
2156 if ( linkMinOSVersion
&& (_file
->_minOSVersion
> linkMinOSVersion
) ) {
2159 versionToString(_file
->_minOSVersion
, t1
);
2160 versionToString(linkMinOSVersion
, t2
);
2161 warning("object file (%s) was built for newer %s version (%s) than being linked (%s)",
2162 _path
, Options::platformName(lcPlatform
), t1
, t2
);
2167 // record range of sections
2168 if ( segment
== NULL
)
2169 throw "missing LC_SEGMENT";
2170 _sectionsStart
= (macho_section
<P
>*)((char*)segment
+ sizeof(macho_segment_command
<P
>));
2171 _machOSectionsCount
= segment
->nsects();
2172 if ( (sizeof(macho_segment_command
<P
>) + _machOSectionsCount
* sizeof(macho_section
<P
>)) > segment
->cmdsize() )
2173 throw "too many sections for size of LC_SEGMENT command";
2177 template <typename A
>
2178 Options::Platform Parser
<A
>::findPlatform(const macho_header
<P
>* header
)
2180 const uint32_t cmd_count
= header
->ncmds();
2181 if ( cmd_count
== 0 )
2182 return Options::kPlatformUnknown
;
2183 const macho_load_command
<P
>* const cmds
= (macho_load_command
<P
>*)((char*)header
+ sizeof(macho_header
<P
>));
2184 const macho_load_command
<P
>* const cmdsEnd
= (macho_load_command
<P
>*)((char*)header
+ sizeof(macho_header
<P
>) + header
->sizeofcmds());
2185 const macho_load_command
<P
>* cmd
= cmds
;
2186 for (uint32_t i
= 0; i
< cmd_count
; ++i
) {
2187 uint32_t size
= cmd
->cmdsize();
2188 if ( (size
& loadCommandSizeMask()) != 0 )
2189 throwf("load command #%d has a unaligned size", i
);
2190 const uint8_t* endOfCmd
= ((uint8_t*)cmd
)+cmd
->cmdsize();
2191 if ( endOfCmd
> (uint8_t*)cmdsEnd
)
2192 throwf("load command #%d extends beyond the end of the load commands", i
);
2193 switch (cmd
->cmd()) {
2194 case LC_VERSION_MIN_MACOSX
:
2195 return Options::kPlatformOSX
;
2196 case LC_VERSION_MIN_IPHONEOS
:
2197 return Options::kPlatformiOS
;
2199 cmd
= (const macho_load_command
<P
>*)(((char*)cmd
)+cmd
->cmdsize());
2200 if ( cmd
> cmdsEnd
)
2201 throwf("malformed mach-o file, load command #%d is outside size of load commands", i
);
2203 return Options::kPlatformUnknown
;
2207 template <typename A
>
2208 void Parser
<A
>::prescanSymbolTable()
2210 _tentativeDefinitionCount
= 0;
2211 _absoluteSymbolCount
= 0;
2212 _symbolsInSections
= 0;
2213 _hasDataInCodeLabels
= false;
2214 for (uint32_t i
=0; i
< this->_symbolCount
; ++i
) {
2215 const macho_nlist
<P
>& sym
= symbolFromIndex(i
);
2217 if ( (sym
.n_type() & N_STAB
) != 0 )
2220 // look at undefines
2221 const char* symbolName
= this->nameFromSymbol(sym
);
2222 if ( (sym
.n_type() & N_TYPE
) == N_UNDF
) {
2223 if ( sym
.n_value() != 0 ) {
2224 // count tentative definitions
2225 ++_tentativeDefinitionCount
;
2227 else if ( strncmp(symbolName
, "___dtrace_", 10) == 0 ) {
2228 // any undefined starting with __dtrace_*$ that is not ___dtrace_probe$* or ___dtrace_isenabled$*
2229 // is extra provider info
2230 if ( (strncmp(&symbolName
[10], "probe$", 6) != 0) && (strncmp(&symbolName
[10], "isenabled$", 10) != 0) ) {
2231 _dtraceProviderInfo
.push_back(symbolName
);
2236 else if ( ((sym
.n_type() & N_TYPE
) == N_INDR
) && ((sym
.n_type() & N_EXT
) != 0) ) {
2237 _indirectSymbolCount
++;
2241 // count absolute symbols
2242 if ( (sym
.n_type() & N_TYPE
) == N_ABS
) {
2243 const char* absName
= this->nameFromSymbol(sym
);
2244 // ignore .objc_class_name_* symbols
2245 if ( strncmp(absName
, ".objc_class_name_", 17) == 0 ) {
2249 // ignore .objc_class_name_* symbols
2250 if ( strncmp(absName
, ".objc_category_name_", 20) == 0 )
2252 // ignore empty *.eh symbols
2253 if ( strcmp(&absName
[strlen(absName
)-3], ".eh") == 0 )
2255 ++_absoluteSymbolCount
;
2258 // only look at definitions
2259 if ( (sym
.n_type() & N_TYPE
) != N_SECT
)
2262 // 'L' labels do not denote atom breaks
2263 if ( symbolName
[0] == 'L' ) {
2264 // <rdar://problem/9218847> Formalize data in code with L$start$ labels
2265 if ( strncmp(symbolName
, "L$start$", 8) == 0 )
2266 _hasDataInCodeLabels
= true;
2269 // how many def syms in each section
2270 if ( sym
.n_sect() > _machOSectionsCount
)
2271 throw "bad n_sect in symbol table";
2273 _symbolsInSections
++;
2277 template <typename A
>
2278 void Parser
<A
>::appendAliasAtoms(uint8_t* p
)
2280 for (uint32_t i
=0; i
< this->_symbolCount
; ++i
) {
2281 const macho_nlist
<P
>& sym
= symbolFromIndex(i
);
2283 if ( (sym
.n_type() & N_STAB
) != 0 )
2286 // only look at N_INDR symbols
2287 if ( (sym
.n_type() & N_TYPE
) != N_INDR
)
2290 // skip non-external aliases
2291 if ( (sym
.n_type() & N_EXT
) == 0 )
2294 const char* symbolName
= this->nameFromSymbol(sym
);
2295 const char* aliasOfName
= &_strings
[sym
.n_value()];
2296 bool isHiddenVisibility
= (sym
.n_type() & N_PEXT
);
2297 AliasAtom
* allocatedSpace
= (AliasAtom
*)p
;
2298 new (allocatedSpace
) AliasAtom(symbolName
, isHiddenVisibility
, _file
, aliasOfName
);
2299 p
+= sizeof(AliasAtom
);
2305 template <typename A
>
2306 int Parser
<A
>::sectionIndexSorter(void* extra
, const void* l
, const void* r
)
2308 Parser
<A
>* parser
= (Parser
<A
>*)extra
;
2309 const uint32_t* left
= (uint32_t*)l
;
2310 const uint32_t* right
= (uint32_t*)r
;
2311 const macho_section
<P
>* leftSect
= parser
->machOSectionFromSectionIndex(*left
);
2312 const macho_section
<P
>* rightSect
= parser
->machOSectionFromSectionIndex(*right
);
2314 // can't just return difference because 64-bit diff does not fit in 32-bit return type
2315 int64_t result
= leftSect
->addr() - rightSect
->addr();
2316 if ( result
== 0 ) {
2317 // two sections with same start address
2318 // one with zero size goes first
2319 bool leftEmpty
= ( leftSect
->size() == 0 );
2320 bool rightEmpty
= ( rightSect
->size() == 0 );
2321 if ( leftEmpty
!= rightEmpty
) {
2322 return ( rightEmpty
? 1 : -1 );
2324 if ( !leftEmpty
&& !rightEmpty
)
2325 throwf("overlapping sections");
2326 // both empty, so chose file order
2327 return ( rightSect
- leftSect
);
2329 else if ( result
< 0 )
2335 template <typename A
>
2336 void Parser
<A
>::makeSortedSectionsArray(uint32_t array
[])
2338 const bool log
= false;
2341 fprintf(stderr
, "unsorted sections:\n");
2342 for(unsigned int i
=0; i
< _machOSectionsCount
; ++i
)
2343 fprintf(stderr
, "0x%08llX %s %s\n", _sectionsStart
[i
].addr(), _sectionsStart
[i
].segname(), _sectionsStart
[i
].sectname());
2346 // sort by symbol table address
2347 for (uint32_t i
=0; i
< _machOSectionsCount
; ++i
)
2349 ::qsort_r(array
, _machOSectionsCount
, sizeof(uint32_t), this, §ionIndexSorter
);
2352 fprintf(stderr
, "sorted sections:\n");
2353 for(unsigned int i
=0; i
< _machOSectionsCount
; ++i
)
2354 fprintf(stderr
, "0x%08llX %s %s\n", _sectionsStart
[array
[i
]].addr(), _sectionsStart
[array
[i
]].segname(), _sectionsStart
[array
[i
]].sectname());
2360 template <typename A
>
2361 int Parser
<A
>::symbolIndexSorter(void* extra
, const void* l
, const void* r
)
2363 ParserAndSectionsArray
* extraInfo
= (ParserAndSectionsArray
*)extra
;
2364 Parser
<A
>* parser
= extraInfo
->parser
;
2365 const uint32_t* sortedSectionsArray
= extraInfo
->sortedSectionsArray
;
2366 const uint32_t* left
= (uint32_t*)l
;
2367 const uint32_t* right
= (uint32_t*)r
;
2368 const macho_nlist
<P
>& leftSym
= parser
->symbolFromIndex(*left
);
2369 const macho_nlist
<P
>& rightSym
= parser
->symbolFromIndex(*right
);
2370 // can't just return difference because 64-bit diff does not fit in 32-bit return type
2371 int64_t result
= leftSym
.n_value() - rightSym
.n_value();
2372 if ( result
== 0 ) {
2373 // two symbols with same address
2374 // if in different sections, sort earlier section first
2375 if ( leftSym
.n_sect() != rightSym
.n_sect() ) {
2376 for (uint32_t i
=0; i
< parser
->machOSectionCount(); ++i
) {
2377 if ( sortedSectionsArray
[i
]+1 == leftSym
.n_sect() )
2379 if ( sortedSectionsArray
[i
]+1 == rightSym
.n_sect() )
2383 // two symbols in same section, means one is an alias
2384 // if one is ltmp*, make it an alias (sort first)
2385 const char* leftName
= parser
->nameFromSymbol(leftSym
);
2386 const char* rightName
= parser
->nameFromSymbol(rightSym
);
2387 bool leftIsTmp
= strncmp(leftName
, "ltmp", 4);
2388 bool rightIsTmp
= strncmp(rightName
, "ltmp", 4);
2389 if ( leftIsTmp
!= rightIsTmp
) {
2390 return (rightIsTmp
? -1 : 1);
2393 // if only one is global, make the other an alias (sort first)
2394 if ( (leftSym
.n_type() & N_EXT
) != (rightSym
.n_type() & N_EXT
) ) {
2395 if ( (rightSym
.n_type() & N_EXT
) != 0 )
2400 // if both are global, sort alphabetically. earlier one will be the alias
2401 return ( strcmp(rightName
, leftName
) );
2403 else if ( result
< 0 )
2410 template <typename A
>
2411 void Parser
<A
>::makeSortedSymbolsArray(uint32_t array
[], const uint32_t sectionArray
[])
2413 const bool log
= false;
2415 uint32_t* p
= array
;
2416 for (uint32_t i
=0; i
< this->_symbolCount
; ++i
) {
2417 const macho_nlist
<P
>& sym
= symbolFromIndex(i
);
2419 if ( (sym
.n_type() & N_STAB
) != 0 )
2422 // only look at definitions
2423 if ( (sym
.n_type() & N_TYPE
) != N_SECT
)
2426 // 'L' labels do not denote atom breaks
2427 const char* symbolName
= this->nameFromSymbol(sym
);
2428 if ( symbolName
[0] == 'L' )
2431 // how many def syms in each section
2432 if ( sym
.n_sect() > _machOSectionsCount
)
2433 throw "bad n_sect in symbol table";
2438 assert(p
== &array
[_symbolsInSections
] && "second pass over symbol table yield a different number of symbols");
2440 // sort by symbol table address
2441 ParserAndSectionsArray extra
= { this, sectionArray
};
2442 ::qsort_r(array
, _symbolsInSections
, sizeof(uint32_t), &extra
, &symbolIndexSorter
);
2445 // look for two symbols at same address
2446 _overlappingSymbols
= false;
2447 for (unsigned int i
=1; i
< _symbolsInSections
; ++i
) {
2448 if ( symbolFromIndex(array
[i
-1]).n_value() == symbolFromIndex(array
[i
]).n_value() ) {
2449 //fprintf(stderr, "overlapping symbols at 0x%08llX\n", symbolFromIndex(array[i-1]).n_value());
2450 _overlappingSymbols
= true;
2456 fprintf(stderr
, "sorted symbols:\n");
2457 for(unsigned int i
=0; i
< _symbolsInSections
; ++i
)
2458 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
])) );
2462 template <typename A
>
2463 void Parser
<A
>::makeSections()
2465 // classify each section by type
2466 // compute how many Section objects will be needed and total size for all
2467 unsigned int totalSectionsSize
= 0;
2468 uint8_t machOSectsStorage
[sizeof(MachOSectionAndSectionClass
<P
>)*(_machOSectionsCount
+2)]; // also room for tentative-defs and absolute symbols
2469 // allocate raw storage for all section objects on stack
2470 MachOSectionAndSectionClass
<P
>* machOSects
= (MachOSectionAndSectionClass
<P
>*)machOSectsStorage
;
2471 unsigned int count
= 0;
2472 // local variable for bitcode parsing
2473 const macho_section
<P
>* bitcodeSect
= NULL
;
2474 const macho_section
<P
>* cmdlineSect
= NULL
;
2475 const macho_section
<P
>* swiftCmdlineSect
= NULL
;
2476 const macho_section
<P
>* bundleSect
= NULL
;
2477 bool bitcodeAsm
= false;
2479 for (uint32_t i
=0; i
< _machOSectionsCount
; ++i
) {
2480 const macho_section
<P
>* sect
= &_sectionsStart
[i
];
2481 uint8_t sectionType
= (sect
->flags() & SECTION_TYPE
);
2482 if ( (sect
->offset() + sect
->size() > _fileLength
) && (sectionType
!= S_ZEROFILL
) && (sectionType
!= S_THREAD_LOCAL_ZEROFILL
) )
2483 throwf("section %s/%s extends beyond end of file,", sect
->segname(), sect
->sectname());
2485 if ( (sect
->flags() & S_ATTR_DEBUG
) != 0 ) {
2486 if ( strcmp(sect
->segname(), "__DWARF") == 0 ) {
2487 // note that .o file has dwarf
2488 _file
->_debugInfoKind
= ld::relocatable::File::kDebugInfoDwarf
;
2489 // save off iteresting dwarf sections
2490 if ( strcmp(sect
->sectname(), "__debug_info") == 0 )
2491 _file
->_dwarfDebugInfoSect
= sect
;
2492 else if ( strcmp(sect
->sectname(), "__debug_abbrev") == 0 )
2493 _file
->_dwarfDebugAbbrevSect
= sect
;
2494 else if ( strcmp(sect
->sectname(), "__debug_line") == 0 )
2495 _file
->_dwarfDebugLineSect
= sect
;
2496 else if ( strcmp(sect
->sectname(), "__debug_str") == 0 )
2497 _file
->_dwarfDebugStringSect
= sect
;
2498 // linker does not propagate dwarf sections to output file
2501 else if ( strcmp(sect
->segname(), "__LD") == 0 ) {
2502 if ( strncmp(sect
->sectname(), "__compact_unwind", 16) == 0 ) {
2503 machOSects
[count
].sect
= sect
;
2504 totalSectionsSize
+= sizeof(CUSection
<A
>);
2505 machOSects
[count
++].type
= sectionTypeCompactUnwind
;
2510 if ( strcmp(sect
->segname(), "__LLVM") == 0 ) {
2511 // Process bitcode segement
2512 if ( strncmp(sect
->sectname(), "__bitcode", 9) == 0 ) {
2514 } else if ( strncmp(sect
->sectname(), "__cmdline", 9) == 0 ) {
2516 } else if ( strncmp(sect
->sectname(), "__swift_cmdline", 15) == 0 ) {
2517 swiftCmdlineSect
= sect
;
2518 } else if ( strncmp(sect
->sectname(), "__bundle", 8) == 0 ) {
2520 } else if ( strncmp(sect
->sectname(), "__asm", 5) == 0 ) {
2523 // If treat the bitcode as data, continue to parse as a normal section.
2524 if ( !_treateBitcodeAsData
)
2527 // ignore empty __OBJC sections
2528 if ( (sect
->size() == 0) && (strcmp(sect
->segname(), "__OBJC") == 0) )
2530 // objc image info section is really attributes and not content
2531 if ( ((strcmp(sect
->sectname(), "__image_info") == 0) && (strcmp(sect
->segname(), "__OBJC") == 0))
2532 || ((strncmp(sect
->sectname(), "__objc_imageinfo", 16) == 0) && (strcmp(sect
->segname(), "__DATA") == 0)) ) {
2533 // struct objc_image_info {
2534 // uint32_t version; // initially 0
2537 // #define OBJC_IMAGE_SUPPORTS_GC 2
2538 // #define OBJC_IMAGE_GC_ONLY 4
2539 // #define OBJC_IMAGE_IS_SIMULATED 32
2540 // #define OBJC_IMAGE_HAS_CATEGORY_CLASS_PROPERTIES 64
2542 const uint32_t* contents
= (uint32_t*)(_file
->fileContent()+sect
->offset());
2543 if ( (sect
->size() >= 8) && (contents
[0] == 0) ) {
2544 uint32_t flags
= E::get32(contents
[1]);
2545 if ( (flags
& 4) == 4 )
2546 _file
->_objConstraint
= ld::File::objcConstraintGC
;
2547 else if ( (flags
& 2) == 2 )
2548 _file
->_objConstraint
= ld::File::objcConstraintRetainReleaseOrGC
;
2549 else if ( (flags
& 32) == 32 )
2550 _file
->_objConstraint
= ld::File::objcConstraintRetainReleaseForSimulator
;
2552 _file
->_objConstraint
= ld::File::objcConstraintRetainRelease
;
2553 _file
->_swiftVersion
= ((flags
>> 8) & 0xFF);
2554 _file
->_objcHasCategoryClassPropertiesField
= (flags
& 64);
2555 if ( sect
->size() > 8 ) {
2556 warning("section %s/%s has unexpectedly large size %llu in %s",
2557 sect
->segname(), Section
<A
>::makeSectionName(sect
), sect
->size(), _file
->path());
2561 warning("can't parse %s/%s section in %s", sect
->segname(), Section
<A
>::makeSectionName(sect
), _file
->path());
2565 machOSects
[count
].sect
= sect
;
2566 switch ( sect
->flags() & SECTION_TYPE
) {
2567 case S_SYMBOL_STUBS
:
2568 if ( _stubsSectionNum
== 0 ) {
2569 _stubsSectionNum
= i
+1;
2570 _stubsMachOSection
= sect
;
2573 assert(1 && "multiple S_SYMBOL_STUBS sections");
2574 case S_LAZY_SYMBOL_POINTERS
:
2576 case S_4BYTE_LITERALS
:
2577 totalSectionsSize
+= sizeof(Literal4Section
<A
>);
2578 machOSects
[count
++].type
= sectionTypeLiteral4
;
2580 case S_8BYTE_LITERALS
:
2581 totalSectionsSize
+= sizeof(Literal8Section
<A
>);
2582 machOSects
[count
++].type
= sectionTypeLiteral8
;
2584 case S_16BYTE_LITERALS
:
2585 totalSectionsSize
+= sizeof(Literal16Section
<A
>);
2586 machOSects
[count
++].type
= sectionTypeLiteral16
;
2588 case S_NON_LAZY_SYMBOL_POINTERS
:
2589 totalSectionsSize
+= sizeof(NonLazyPointerSection
<A
>);
2590 machOSects
[count
++].type
= sectionTypeNonLazy
;
2592 case S_THREAD_LOCAL_VARIABLE_POINTERS
:
2593 totalSectionsSize
+= sizeof(TLVPointerSection
<A
>);
2594 machOSects
[count
++].type
= sectionTypeTLVPointers
;
2596 case S_LITERAL_POINTERS
:
2597 if ( (strcmp(sect
->segname(), "__OBJC") == 0) && (strcmp(sect
->sectname(), "__cls_refs") == 0) ) {
2598 totalSectionsSize
+= sizeof(Objc1ClassReferences
<A
>);
2599 machOSects
[count
++].type
= sectionTypeObjC1ClassRefs
;
2602 totalSectionsSize
+= sizeof(PointerToCStringSection
<A
>);
2603 machOSects
[count
++].type
= sectionTypeCStringPointer
;
2606 case S_CSTRING_LITERALS
:
2607 totalSectionsSize
+= sizeof(CStringSection
<A
>);
2608 machOSects
[count
++].type
= sectionTypeCString
;
2610 case S_MOD_INIT_FUNC_POINTERS
:
2611 case S_MOD_TERM_FUNC_POINTERS
:
2612 case S_THREAD_LOCAL_INIT_FUNCTION_POINTERS
:
2617 case S_THREAD_LOCAL_REGULAR
:
2618 case S_THREAD_LOCAL_ZEROFILL
:
2619 if ( (strcmp(sect
->segname(), "__TEXT") == 0) && (strcmp(sect
->sectname(), "__eh_frame") == 0) ) {
2620 totalSectionsSize
+= sizeof(CFISection
<A
>);
2621 machOSects
[count
++].type
= sectionTypeCFI
;
2623 else if ( (strcmp(sect
->segname(), "__DATA") == 0) && (strcmp(sect
->sectname(), "__cfstring") == 0) ) {
2624 totalSectionsSize
+= sizeof(CFStringSection
<A
>);
2625 machOSects
[count
++].type
= sectionTypeCFString
;
2627 else if ( (strcmp(sect
->segname(), "__TEXT") == 0) && (strcmp(sect
->sectname(), "__ustring") == 0) ) {
2628 totalSectionsSize
+= sizeof(UTF16StringSection
<A
>);
2629 machOSects
[count
++].type
= sectionTypeUTF16Strings
;
2631 else if ( (strcmp(sect
->segname(), "__DATA") == 0) && (strncmp(sect
->sectname(), "__objc_classrefs", 16) == 0) ) {
2632 totalSectionsSize
+= sizeof(ObjC2ClassRefsSection
<A
>);
2633 machOSects
[count
++].type
= sectionTypeObjC2ClassRefs
;
2635 else if ( (strcmp(sect
->segname(), "__DATA") == 0) && (strcmp(sect
->sectname(), "__objc_catlist") == 0) ) {
2636 totalSectionsSize
+= sizeof(ObjC2CategoryListSection
<A
>);
2637 machOSects
[count
++].type
= typeObjC2CategoryList
;
2639 else if ( _AppleObjc
&& (strcmp(sect
->segname(), "__OBJC") == 0) && (strcmp(sect
->sectname(), "__class") == 0) ) {
2640 totalSectionsSize
+= sizeof(ObjC1ClassSection
<A
>);
2641 machOSects
[count
++].type
= sectionTypeObjC1Classes
;
2644 totalSectionsSize
+= sizeof(SymboledSection
<A
>);
2645 machOSects
[count
++].type
= sectionTypeSymboled
;
2648 case S_THREAD_LOCAL_VARIABLES
:
2649 totalSectionsSize
+= sizeof(TLVDefsSection
<A
>);
2650 machOSects
[count
++].type
= sectionTypeTLVDefs
;
2653 throwf("unknown section type %d", sect
->flags() & SECTION_TYPE
);
2658 if ( bitcodeSect
!= NULL
) {
2659 if ( cmdlineSect
!= NULL
)
2660 _file
->_bitcode
= std::unique_ptr
<ld::Bitcode
>(new ld::ClangBitcode(&_fileContent
[bitcodeSect
->offset()], bitcodeSect
->size(),
2661 &_fileContent
[cmdlineSect
->offset()], cmdlineSect
->size()));
2662 else if ( swiftCmdlineSect
!= NULL
)
2663 _file
->_bitcode
= std::unique_ptr
<ld::Bitcode
>(new ld::SwiftBitcode(&_fileContent
[bitcodeSect
->offset()], bitcodeSect
->size(),
2664 &_fileContent
[swiftCmdlineSect
->offset()], swiftCmdlineSect
->size()));
2666 throwf("Object file with bitcode missing cmdline options: %s", _file
->path());
2668 else if ( bundleSect
!= NULL
)
2669 _file
->_bitcode
= std::unique_ptr
<ld::Bitcode
>(new ld::BundleBitcode(&_fileContent
[bundleSect
->offset()], bundleSect
->size()));
2670 else if ( bitcodeAsm
)
2671 _file
->_bitcode
= std::unique_ptr
<ld::Bitcode
>(new ld::AsmBitcode(_fileContent
, _fileLength
));
2673 // sort by address (mach-o object files don't aways have sections sorted)
2674 ::qsort(machOSects
, count
, sizeof(MachOSectionAndSectionClass
<P
>), MachOSectionAndSectionClass
<P
>::sorter
);
2676 // we will synthesize a dummy Section<A> object for tentative definitions
2677 if ( _tentativeDefinitionCount
> 0 ) {
2678 totalSectionsSize
+= sizeof(TentativeDefinitionSection
<A
>);
2679 machOSects
[count
++].type
= sectionTypeTentativeDefinitions
;
2682 // we will synthesize a dummy Section<A> object for Absolute symbols
2683 if ( _absoluteSymbolCount
> 0 ) {
2684 totalSectionsSize
+= sizeof(AbsoluteSymbolSection
<A
>);
2685 machOSects
[count
++].type
= sectionTypeAbsoluteSymbols
;
2688 // allocate one block for all Section objects as well as pointers to each
2689 uint8_t* space
= new uint8_t[totalSectionsSize
+count
*sizeof(Section
<A
>*)];
2690 _file
->_sectionsArray
= (Section
<A
>**)space
;
2691 _file
->_sectionsArrayCount
= count
;
2692 Section
<A
>** objects
= _file
->_sectionsArray
;
2693 space
+= count
*sizeof(Section
<A
>*);
2694 for (uint32_t i
=0; i
< count
; ++i
) {
2695 switch ( machOSects
[i
].type
) {
2696 case sectionTypeIgnore
:
2698 case sectionTypeLiteral4
:
2699 *objects
++ = new (space
) Literal4Section
<A
>(*this, *_file
, machOSects
[i
].sect
);
2700 space
+= sizeof(Literal4Section
<A
>);
2702 case sectionTypeLiteral8
:
2703 *objects
++ = new (space
) Literal8Section
<A
>(*this, *_file
, machOSects
[i
].sect
);
2704 space
+= sizeof(Literal8Section
<A
>);
2706 case sectionTypeLiteral16
:
2707 *objects
++ = new (space
) Literal16Section
<A
>(*this, *_file
, machOSects
[i
].sect
);
2708 space
+= sizeof(Literal16Section
<A
>);
2710 case sectionTypeNonLazy
:
2711 *objects
++ = new (space
) NonLazyPointerSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2712 space
+= sizeof(NonLazyPointerSection
<A
>);
2714 case sectionTypeTLVPointers
:
2715 *objects
++ = new (space
) TLVPointerSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2716 space
+= sizeof(TLVPointerSection
<A
>);
2718 case sectionTypeCFI
:
2719 _EHFrameSection
= new (space
) CFISection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2720 *objects
++ = _EHFrameSection
;
2721 space
+= sizeof(CFISection
<A
>);
2723 case sectionTypeCString
:
2724 *objects
++ = new (space
) CStringSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2725 space
+= sizeof(CStringSection
<A
>);
2727 case sectionTypeCStringPointer
:
2728 *objects
++ = new (space
) PointerToCStringSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2729 space
+= sizeof(PointerToCStringSection
<A
>);
2731 case sectionTypeObjC1ClassRefs
:
2732 *objects
++ = new (space
) Objc1ClassReferences
<A
>(*this, *_file
, machOSects
[i
].sect
);
2733 space
+= sizeof(Objc1ClassReferences
<A
>);
2735 case sectionTypeUTF16Strings
:
2736 *objects
++ = new (space
) UTF16StringSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2737 space
+= sizeof(UTF16StringSection
<A
>);
2739 case sectionTypeCFString
:
2740 *objects
++ = new (space
) CFStringSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2741 space
+= sizeof(CFStringSection
<A
>);
2743 case sectionTypeObjC2ClassRefs
:
2744 *objects
++ = new (space
) ObjC2ClassRefsSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2745 space
+= sizeof(ObjC2ClassRefsSection
<A
>);
2747 case typeObjC2CategoryList
:
2748 *objects
++ = new (space
) ObjC2CategoryListSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2749 space
+= sizeof(ObjC2CategoryListSection
<A
>);
2751 case sectionTypeObjC1Classes
:
2752 *objects
++ = new (space
) ObjC1ClassSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2753 space
+= sizeof(ObjC1ClassSection
<A
>);
2755 case sectionTypeSymboled
:
2756 *objects
++ = new (space
) SymboledSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2757 space
+= sizeof(SymboledSection
<A
>);
2759 case sectionTypeTLVDefs
:
2760 *objects
++ = new (space
) TLVDefsSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2761 space
+= sizeof(TLVDefsSection
<A
>);
2763 case sectionTypeCompactUnwind
:
2764 _compactUnwindSection
= new (space
) CUSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2765 *objects
++ = _compactUnwindSection
;
2766 space
+= sizeof(CUSection
<A
>);
2768 case sectionTypeTentativeDefinitions
:
2769 *objects
++ = new (space
) TentativeDefinitionSection
<A
>(*this, *_file
);
2770 space
+= sizeof(TentativeDefinitionSection
<A
>);
2772 case sectionTypeAbsoluteSymbols
:
2773 _absoluteSection
= new (space
) AbsoluteSymbolSection
<A
>(*this, *_file
);
2774 *objects
++ = _absoluteSection
;
2775 space
+= sizeof(AbsoluteSymbolSection
<A
>);
2778 throw "internal error uknown SectionType";
2784 template <typename A
>
2785 Section
<A
>* Parser
<A
>::sectionForAddress(typename
A::P::uint_t addr
)
2787 for (uint32_t i
=0; i
< _file
->_sectionsArrayCount
; ++i
) {
2788 const macho_section
<typename
A::P
>* sect
= _file
->_sectionsArray
[i
]->machoSection();
2789 // TentativeDefinitionSection and AbsoluteSymbolSection have no mach-o section
2790 if ( sect
!= NULL
) {
2791 if ( (sect
->addr() <= addr
) && (addr
< (sect
->addr()+sect
->size())) ) {
2792 return _file
->_sectionsArray
[i
];
2796 // not strictly in any section
2797 // may be in a zero length section
2798 for (uint32_t i
=0; i
< _file
->_sectionsArrayCount
; ++i
) {
2799 const macho_section
<typename
A::P
>* sect
= _file
->_sectionsArray
[i
]->machoSection();
2800 // TentativeDefinitionSection and AbsoluteSymbolSection have no mach-o section
2801 if ( sect
!= NULL
) {
2802 if ( (sect
->addr() == addr
) && (sect
->size() == 0) ) {
2803 return _file
->_sectionsArray
[i
];
2808 throwf("sectionForAddress(0x%llX) address not in any section", (uint64_t)addr
);
2811 template <typename A
>
2812 Section
<A
>* Parser
<A
>::sectionForNum(unsigned int num
)
2814 for (uint32_t i
=0; i
< _file
->_sectionsArrayCount
; ++i
) {
2815 const macho_section
<typename
A::P
>* sect
= _file
->_sectionsArray
[i
]->machoSection();
2816 // TentativeDefinitionSection and AbsoluteSymbolSection have no mach-o section
2817 if ( sect
!= NULL
) {
2818 if ( num
== (unsigned int)((sect
- _sectionsStart
)+1) )
2819 return _file
->_sectionsArray
[i
];
2822 throwf("sectionForNum(%u) section number not for any section", num
);
2825 template <typename A
>
2826 Atom
<A
>* Parser
<A
>::findAtomByAddress(pint_t addr
)
2828 Section
<A
>* section
= this->sectionForAddress(addr
);
2829 return section
->findAtomByAddress(addr
);
2832 template <typename A
>
2833 Atom
<A
>* Parser
<A
>::findAtomByAddressOrNullIfStub(pint_t addr
)
2835 if ( hasStubsSection() && (_stubsMachOSection
->addr() <= addr
) && (addr
< (_stubsMachOSection
->addr()+_stubsMachOSection
->size())) )
2837 return findAtomByAddress(addr
);
2840 template <typename A
>
2841 Atom
<A
>* Parser
<A
>::findAtomByAddressOrLocalTargetOfStub(pint_t addr
, uint32_t* offsetInAtom
)
2843 if ( hasStubsSection() && (_stubsMachOSection
->addr() <= addr
) && (addr
< (_stubsMachOSection
->addr()+_stubsMachOSection
->size())) ) {
2844 // target is a stub, remove indirection
2845 uint32_t symbolIndex
= this->symbolIndexFromIndirectSectionAddress(addr
, _stubsMachOSection
);
2846 assert(symbolIndex
!= INDIRECT_SYMBOL_LOCAL
);
2847 const macho_nlist
<P
>& sym
= this->symbolFromIndex(symbolIndex
);
2848 // can't be to external weak symbol
2849 assert( (this->combineFromSymbol(sym
) != ld::Atom::combineByName
) || (this->scopeFromSymbol(sym
) != ld::Atom::scopeGlobal
) );
2851 return this->findAtomByName(this->nameFromSymbol(sym
));
2853 Atom
<A
>* target
= this->findAtomByAddress(addr
);
2854 *offsetInAtom
= addr
- target
->_objAddress
;
2858 template <typename A
>
2859 Atom
<A
>* Parser
<A
>::findAtomByName(const char* name
)
2861 uint8_t* p
= _file
->_atomsArray
;
2862 for(int i
=_file
->_atomsArrayCount
; i
> 0; --i
) {
2863 Atom
<A
>* atom
= (Atom
<A
>*)p
;
2864 if ( strcmp(name
, atom
->name()) == 0 )
2866 p
+= sizeof(Atom
<A
>);
2871 template <typename A
>
2872 void Parser
<A
>::findTargetFromAddress(pint_t addr
, TargetDesc
& target
)
2874 if ( hasStubsSection() && (_stubsMachOSection
->addr() <= addr
) && (addr
< (_stubsMachOSection
->addr()+_stubsMachOSection
->size())) ) {
2875 // target is a stub, remove indirection
2876 uint32_t symbolIndex
= this->symbolIndexFromIndirectSectionAddress(addr
, _stubsMachOSection
);
2877 assert(symbolIndex
!= INDIRECT_SYMBOL_LOCAL
);
2878 const macho_nlist
<P
>& sym
= this->symbolFromIndex(symbolIndex
);
2880 target
.name
= this->nameFromSymbol(sym
);
2881 target
.weakImport
= this->weakImportFromSymbol(sym
);
2885 Section
<A
>* section
= this->sectionForAddress(addr
);
2886 target
.atom
= section
->findAtomByAddress(addr
);
2887 target
.addend
= addr
- target
.atom
->_objAddress
;
2888 target
.weakImport
= false;
2892 template <typename A
>
2893 void Parser
<A
>::findTargetFromAddress(pint_t baseAddr
, pint_t addr
, TargetDesc
& target
)
2895 findTargetFromAddress(baseAddr
, target
);
2896 target
.addend
= addr
- target
.atom
->_objAddress
;
2899 template <typename A
>
2900 void Parser
<A
>::findTargetFromAddressAndSectionNum(pint_t addr
, unsigned int sectNum
, TargetDesc
& target
)
2902 if ( sectNum
== R_ABS
) {
2903 // target is absolute symbol that corresponds to addr
2904 if ( _absoluteSection
!= NULL
) {
2905 target
.atom
= _absoluteSection
->findAbsAtomForValue(addr
);
2906 if ( target
.atom
!= NULL
) {
2908 target
.weakImport
= false;
2913 throwf("R_ABS reloc but no absolute symbol at target address");
2916 if ( hasStubsSection() && (stubsSectionNum() == sectNum
) ) {
2917 // target is a stub, remove indirection
2918 uint32_t symbolIndex
= this->symbolIndexFromIndirectSectionAddress(addr
, _stubsMachOSection
);
2919 assert(symbolIndex
!= INDIRECT_SYMBOL_LOCAL
);
2920 const macho_nlist
<P
>& sym
= this->symbolFromIndex(symbolIndex
);
2921 // use direct reference when stub is to a static function
2922 if ( ((sym
.n_type() & N_TYPE
) == N_SECT
) && (((sym
.n_type() & N_EXT
) == 0) || (this->nameFromSymbol(sym
)[0] == 'L')) ) {
2923 this->findTargetFromAddressAndSectionNum(sym
.n_value(), sym
.n_sect(), target
);
2927 target
.name
= this->nameFromSymbol(sym
);
2928 target
.weakImport
= this->weakImportFromSymbol(sym
);
2933 Section
<A
>* section
= this->sectionForNum(sectNum
);
2934 target
.atom
= section
->findAtomByAddress(addr
);
2935 if ( target
.atom
== NULL
) {
2936 typedef typename
A::P::sint_t sint_t
;
2937 sint_t a
= (sint_t
)addr
;
2938 sint_t sectStart
= (sint_t
)(section
->machoSection()->addr());
2939 sint_t sectEnd
= sectStart
+ section
->machoSection()->size();
2940 if ( a
< sectStart
) {
2941 // target address is before start of section, so must be negative addend
2942 target
.atom
= section
->findAtomByAddress(sectStart
);
2943 target
.addend
= a
- sectStart
;
2944 target
.weakImport
= false;
2948 else if ( a
>= sectEnd
) {
2949 target
.atom
= section
->findAtomByAddress(sectEnd
-1);
2950 target
.addend
= a
- sectEnd
;
2951 target
.weakImport
= false;
2956 assert(target
.atom
!= NULL
);
2957 target
.addend
= addr
- target
.atom
->_objAddress
;
2958 target
.weakImport
= false;
2962 template <typename A
>
2963 void Parser
<A
>::addDtraceExtraInfos(const SourceLocation
& src
, const char* providerName
)
2965 // for every ___dtrace_stability$* and ___dtrace_typedefs$* undefine with
2966 // a matching provider name, add a by-name kDtraceTypeReference at probe site
2967 const char* dollar
= strchr(providerName
, '$');
2968 if ( dollar
!= NULL
) {
2969 int providerNameLen
= dollar
-providerName
+1;
2970 for ( std::vector
<const char*>::iterator it
= _dtraceProviderInfo
.begin(); it
!= _dtraceProviderInfo
.end(); ++it
) {
2971 const char* typeDollar
= strchr(*it
, '$');
2972 if ( typeDollar
!= NULL
) {
2973 if ( strncmp(typeDollar
+1, providerName
, providerNameLen
) == 0 ) {
2974 addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindDtraceExtra
,false, *it
);
2981 template <typename A
>
2982 const char* Parser
<A
>::scanSymbolTableForAddress(uint64_t addr
)
2984 uint64_t closestSymAddr
= 0;
2985 const char* closestSymName
= NULL
;
2986 for (uint32_t i
=0; i
< this->_symbolCount
; ++i
) {
2987 const macho_nlist
<P
>& sym
= symbolFromIndex(i
);
2989 if ( (sym
.n_type() & N_STAB
) != 0 )
2992 // only look at definitions
2993 if ( (sym
.n_type() & N_TYPE
) != N_SECT
)
2996 // return with exact match
2997 if ( sym
.n_value() == addr
) {
2998 const char* name
= nameFromSymbol(sym
);
2999 if ( strncmp(name
, "ltmp", 4) != 0 )
3001 // treat 'ltmp*' labels as close match
3002 closestSymAddr
= sym
.n_value();
3003 closestSymName
= name
;
3006 // record closest seen so far
3007 if ( (sym
.n_value() < addr
) && ((sym
.n_value() > closestSymAddr
) || (closestSymName
== NULL
)) )
3008 closestSymName
= nameFromSymbol(sym
);
3011 return (closestSymName
!= NULL
) ? closestSymName
: "unknown";
3015 template <typename A
>
3016 void Parser
<A
>::addFixups(const SourceLocation
& src
, ld::Fixup::Kind setKind
, const TargetDesc
& target
)
3018 // some fixup pairs can be combined
3019 ld::Fixup::Cluster cl
= ld::Fixup::k1of3
;
3020 ld::Fixup::Kind firstKind
= ld::Fixup::kindSetTargetAddress
;
3021 bool combined
= false;
3022 if ( target
.addend
== 0 ) {
3023 cl
= ld::Fixup::k1of1
;
3025 switch ( setKind
) {
3026 case ld::Fixup::kindStoreLittleEndian32
:
3027 firstKind
= ld::Fixup::kindStoreTargetAddressLittleEndian32
;
3029 case ld::Fixup::kindStoreLittleEndian64
:
3030 firstKind
= ld::Fixup::kindStoreTargetAddressLittleEndian64
;
3032 case ld::Fixup::kindStoreBigEndian32
:
3033 firstKind
= ld::Fixup::kindStoreTargetAddressBigEndian32
;
3035 case ld::Fixup::kindStoreBigEndian64
:
3036 firstKind
= ld::Fixup::kindStoreTargetAddressBigEndian64
;
3038 case ld::Fixup::kindStoreX86BranchPCRel32
:
3039 firstKind
= ld::Fixup::kindStoreTargetAddressX86BranchPCRel32
;
3041 case ld::Fixup::kindStoreX86PCRel32
:
3042 firstKind
= ld::Fixup::kindStoreTargetAddressX86PCRel32
;
3044 case ld::Fixup::kindStoreX86PCRel32GOTLoad
:
3045 firstKind
= ld::Fixup::kindStoreTargetAddressX86PCRel32GOTLoad
;
3047 case ld::Fixup::kindStoreX86PCRel32TLVLoad
:
3048 firstKind
= ld::Fixup::kindStoreTargetAddressX86PCRel32TLVLoad
;
3050 case ld::Fixup::kindStoreX86Abs32TLVLoad
:
3051 firstKind
= ld::Fixup::kindStoreTargetAddressX86Abs32TLVLoad
;
3053 case ld::Fixup::kindStoreARMBranch24
:
3054 firstKind
= ld::Fixup::kindStoreTargetAddressARMBranch24
;
3056 case ld::Fixup::kindStoreThumbBranch22
:
3057 firstKind
= ld::Fixup::kindStoreTargetAddressThumbBranch22
;
3059 #if SUPPORT_ARCH_arm64
3060 case ld::Fixup::kindStoreARM64Branch26
:
3061 firstKind
= ld::Fixup::kindStoreTargetAddressARM64Branch26
;
3063 case ld::Fixup::kindStoreARM64Page21
:
3064 firstKind
= ld::Fixup::kindStoreTargetAddressARM64Page21
;
3066 case ld::Fixup::kindStoreARM64PageOff12
:
3067 firstKind
= ld::Fixup::kindStoreTargetAddressARM64PageOff12
;
3069 case ld::Fixup::kindStoreARM64GOTLoadPage21
:
3070 firstKind
= ld::Fixup::kindStoreTargetAddressARM64GOTLoadPage21
;
3072 case ld::Fixup::kindStoreARM64GOTLoadPageOff12
:
3073 firstKind
= ld::Fixup::kindStoreTargetAddressARM64GOTLoadPageOff12
;
3075 case ld::Fixup::kindStoreARM64TLVPLoadPage21
:
3076 firstKind
= ld::Fixup::kindStoreTargetAddressARM64TLVPLoadPage21
;
3078 case ld::Fixup::kindStoreARM64TLVPLoadPageOff12
:
3079 firstKind
= ld::Fixup::kindStoreTargetAddressARM64TLVPLoadPageOff12
;
3084 cl
= ld::Fixup::k1of2
;
3089 if ( target
.atom
!= NULL
) {
3090 if ( target
.atom
->scope() == ld::Atom::scopeTranslationUnit
) {
3091 addFixup(src
, cl
, firstKind
, target
.atom
);
3093 else if ( (target
.atom
->combine() == ld::Atom::combineByNameAndContent
) || (target
.atom
->combine() == ld::Atom::combineByNameAndReferences
) ) {
3094 addFixup(src
, cl
, firstKind
, ld::Fixup::bindingByContentBound
, target
.atom
);
3096 else if ( (src
.atom
->section().type() == ld::Section::typeCFString
) && (src
.offsetInAtom
!= 0) ) {
3097 // backing string in CFStrings should always be direct
3098 addFixup(src
, cl
, firstKind
, target
.atom
);
3100 else if ( (src
.atom
== target
.atom
) && (target
.atom
->combine() == ld::Atom::combineByName
) ) {
3101 // reference to self should always be direct
3102 addFixup(src
, cl
, firstKind
, target
.atom
);
3105 // change direct fixup to by-name fixup
3106 addFixup(src
, cl
, firstKind
, false, target
.atom
->name());
3110 addFixup(src
, cl
, firstKind
, target
.weakImport
, target
.name
);
3112 if ( target
.addend
== 0 ) {
3114 addFixup(src
, ld::Fixup::k2of2
, setKind
);
3117 addFixup(src
, ld::Fixup::k2of3
, ld::Fixup::kindAddAddend
, target
.addend
);
3118 addFixup(src
, ld::Fixup::k3of3
, setKind
);
3122 template <typename A
>
3123 void Parser
<A
>::addFixups(const SourceLocation
& src
, ld::Fixup::Kind kind
, const TargetDesc
& target
, const TargetDesc
& picBase
)
3125 ld::Fixup::Cluster cl
= (target
.addend
== 0) ? ld::Fixup::k1of4
: ld::Fixup::k1of5
;
3126 if ( target
.atom
!= NULL
) {
3127 if ( target
.atom
->scope() == ld::Atom::scopeTranslationUnit
) {
3128 addFixup(src
, cl
, ld::Fixup::kindSetTargetAddress
, target
.atom
);
3130 else if ( (target
.atom
->combine() == ld::Atom::combineByNameAndContent
) || (target
.atom
->combine() == ld::Atom::combineByNameAndReferences
) ) {
3131 addFixup(src
, cl
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, target
.atom
);
3134 addFixup(src
, cl
, ld::Fixup::kindSetTargetAddress
, false, target
.atom
->name());
3138 addFixup(src
, cl
, ld::Fixup::kindSetTargetAddress
, target
.weakImport
, target
.name
);
3140 if ( target
.addend
== 0 ) {
3141 assert(picBase
.atom
!= NULL
);
3142 addFixup(src
, ld::Fixup::k2of4
, ld::Fixup::kindSubtractTargetAddress
, picBase
.atom
);
3143 addFixup(src
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractAddend
, picBase
.addend
);
3144 addFixup(src
, ld::Fixup::k4of4
, kind
);
3147 addFixup(src
, ld::Fixup::k2of5
, ld::Fixup::kindAddAddend
, target
.addend
);
3148 addFixup(src
, ld::Fixup::k3of5
, ld::Fixup::kindSubtractTargetAddress
, picBase
.atom
);
3149 addFixup(src
, ld::Fixup::k4of5
, ld::Fixup::kindSubtractAddend
, picBase
.addend
);
3150 addFixup(src
, ld::Fixup::k5of5
, kind
);
3156 template <typename A
>
3157 uint32_t TentativeDefinitionSection
<A
>::computeAtomCount(class Parser
<A
>& parser
,
3158 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
3159 const struct Parser
<A
>::CFI_CU_InfoArrays
&)
3161 return parser
.tentativeDefinitionCount();
3164 template <typename A
>
3165 uint32_t TentativeDefinitionSection
<A
>::appendAtoms(class Parser
<A
>& parser
, uint8_t* p
,
3166 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
3167 const struct Parser
<A
>::CFI_CU_InfoArrays
&)
3169 this->_beginAtoms
= (Atom
<A
>*)p
;
3171 for (uint32_t i
=parser
.undefinedStartIndex(); i
< parser
.undefinedEndIndex(); ++i
) {
3172 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(i
);
3173 if ( ((sym
.n_type() & N_TYPE
) == N_UNDF
) && (sym
.n_value() != 0) ) {
3174 uint64_t size
= sym
.n_value();
3175 uint8_t alignP2
= GET_COMM_ALIGN(sym
.n_desc());
3176 if ( alignP2
== 0 ) {
3177 // common symbols align to their size
3178 // that is, a 4-byte common aligns to 4-bytes
3179 // if this size is not a power of two,
3180 // then round up to the next power of two
3181 alignP2
= 63 - (uint8_t)__builtin_clzll(size
);
3182 if ( size
!= (1ULL << alignP2
) )
3184 // <rdar://problem/24871389> limit default alignment of large commons
3185 if ( alignP2
> parser
.maxDefaultCommonAlignment() )
3186 alignP2
= parser
.maxDefaultCommonAlignment();
3188 Atom
<A
>* allocatedSpace
= (Atom
<A
>*)p
;
3189 new (allocatedSpace
) Atom
<A
>(*this, parser
.nameFromSymbol(sym
), (pint_t
)ULLONG_MAX
, size
,
3190 ld::Atom::definitionTentative
, ld::Atom::combineByName
,
3191 parser
.scopeFromSymbol(sym
), ld::Atom::typeZeroFill
, ld::Atom::symbolTableIn
,
3192 parser
.dontDeadStripFromSymbol(sym
), false, false, ld::Atom::Alignment(alignP2
) );
3193 p
+= sizeof(Atom
<A
>);
3197 this->_endAtoms
= (Atom
<A
>*)p
;
3202 template <typename A
>
3203 uint32_t AbsoluteSymbolSection
<A
>::computeAtomCount(class Parser
<A
>& parser
,
3204 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
3205 const struct Parser
<A
>::CFI_CU_InfoArrays
&)
3207 return parser
.absoluteSymbolCount();
3210 template <typename A
>
3211 uint32_t AbsoluteSymbolSection
<A
>::appendAtoms(class Parser
<A
>& parser
, uint8_t* p
,
3212 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
3213 const struct Parser
<A
>::CFI_CU_InfoArrays
&)
3215 this->_beginAtoms
= (Atom
<A
>*)p
;
3217 for (uint32_t i
=0; i
< parser
.symbolCount(); ++i
) {
3218 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(i
);
3219 if ( (sym
.n_type() & N_TYPE
) != N_ABS
)
3221 const char* absName
= parser
.nameFromSymbol(sym
);
3222 // ignore .objc_class_name_* symbols
3223 if ( strncmp(absName
, ".objc_class_name_", 17) == 0 )
3225 // ignore .objc_class_name_* symbols
3226 if ( strncmp(absName
, ".objc_category_name_", 20) == 0 )
3228 // ignore empty *.eh symbols
3229 if ( strcmp(&absName
[strlen(absName
)-3], ".eh") == 0 )
3232 Atom
<A
>* allocatedSpace
= (Atom
<A
>*)p
;
3233 new (allocatedSpace
) Atom
<A
>(*this, parser
, sym
, 0);
3234 p
+= sizeof(Atom
<A
>);
3237 this->_endAtoms
= (Atom
<A
>*)p
;
3241 template <typename A
>
3242 Atom
<A
>* AbsoluteSymbolSection
<A
>::findAbsAtomForValue(typename
A::P::uint_t value
)
3244 Atom
<A
>* end
= this->_endAtoms
;
3245 for(Atom
<A
>* p
= this->_beginAtoms
; p
< end
; ++p
) {
3246 if ( p
->_objAddress
== value
)
3253 template <typename A
>
3254 uint32_t Parser
<A
>::indirectSymbol(uint32_t indirectIndex
)
3256 if ( indirectIndex
>= _indirectTableCount
)
3257 throw "indirect symbol index out of range";
3258 return E::get32(_indirectTable
[indirectIndex
]);
3261 template <typename A
>
3262 const macho_nlist
<typename
A::P
>& Parser
<A
>::symbolFromIndex(uint32_t index
)
3264 if ( index
> _symbolCount
)
3265 throw "symbol index out of range";
3266 return _symbols
[index
];
3269 template <typename A
>
3270 const macho_section
<typename
A::P
>* Parser
<A
>::machOSectionFromSectionIndex(uint32_t index
)
3272 if ( index
>= _machOSectionsCount
)
3273 throw "section index out of range";
3274 return &_sectionsStart
[index
];
3277 template <typename A
>
3278 uint32_t Parser
<A
>::symbolIndexFromIndirectSectionAddress(pint_t addr
, const macho_section
<P
>* sect
)
3280 uint32_t elementSize
= 0;
3281 switch ( sect
->flags() & SECTION_TYPE
) {
3282 case S_SYMBOL_STUBS
:
3283 elementSize
= sect
->reserved2();
3285 case S_LAZY_SYMBOL_POINTERS
:
3286 case S_NON_LAZY_SYMBOL_POINTERS
:
3287 case S_THREAD_LOCAL_VARIABLE_POINTERS
:
3288 elementSize
= sizeof(pint_t
);
3291 throw "section does not use indirect symbol table";
3293 uint32_t indexInSection
= (addr
- sect
->addr()) / elementSize
;
3294 uint32_t indexIntoIndirectTable
= sect
->reserved1() + indexInSection
;
3295 return this->indirectSymbol(indexIntoIndirectTable
);
3300 template <typename A
>
3301 const char* Parser
<A
>::nameFromSymbol(const macho_nlist
<P
>& sym
)
3303 return &_strings
[sym
.n_strx()];
3306 template <typename A
>
3307 ld::Atom::Scope Parser
<A
>::scopeFromSymbol(const macho_nlist
<P
>& sym
)
3309 if ( (sym
.n_type() & N_EXT
) == 0 )
3310 return ld::Atom::scopeTranslationUnit
;
3311 else if ( (sym
.n_type() & N_PEXT
) != 0 )
3312 return ld::Atom::scopeLinkageUnit
;
3313 else if ( this->nameFromSymbol(sym
)[0] == 'l' ) // since all 'l' symbols will be remove, don't make them global
3314 return ld::Atom::scopeLinkageUnit
;
3316 return ld::Atom::scopeGlobal
;
3319 template <typename A
>
3320 ld::Atom::Definition Parser
<A
>::definitionFromSymbol(const macho_nlist
<P
>& sym
)
3322 switch ( sym
.n_type() & N_TYPE
) {
3324 return ld::Atom::definitionAbsolute
;
3326 return ld::Atom::definitionRegular
;
3328 if ( sym
.n_value() != 0 )
3329 return ld::Atom::definitionTentative
;
3331 throw "definitionFromSymbol() bad symbol";
3334 template <typename A
>
3335 ld::Atom::Combine Parser
<A
>::combineFromSymbol(const macho_nlist
<P
>& sym
)
3337 if ( sym
.n_desc() & N_WEAK_DEF
)
3338 return ld::Atom::combineByName
;
3340 return ld::Atom::combineNever
;
3344 template <typename A
>
3345 ld::Atom::SymbolTableInclusion Parser
<A
>::inclusionFromSymbol(const macho_nlist
<P
>& sym
)
3347 const char* symbolName
= nameFromSymbol(sym
);
3348 // labels beginning with 'l' (lowercase ell) are automatically removed in final linked images <rdar://problem/4571042>
3349 // labels beginning with 'L' should have been stripped by the assembler, so are stripped now
3350 if ( sym
.n_desc() & REFERENCED_DYNAMICALLY
)
3351 return ld::Atom::symbolTableInAndNeverStrip
;
3352 else if ( symbolName
[0] == 'l' )
3353 return ld::Atom::symbolTableNotInFinalLinkedImages
;
3354 else if ( symbolName
[0] == 'L' )
3355 return ld::Atom::symbolTableNotIn
;
3357 return ld::Atom::symbolTableIn
;
3360 template <typename A
>
3361 bool Parser
<A
>::dontDeadStripFromSymbol(const macho_nlist
<P
>& sym
)
3363 return ( (sym
.n_desc() & (N_NO_DEAD_STRIP
|REFERENCED_DYNAMICALLY
)) != 0 );
3366 template <typename A
>
3367 bool Parser
<A
>::isThumbFromSymbol(const macho_nlist
<P
>& sym
)
3369 return ( sym
.n_desc() & N_ARM_THUMB_DEF
);
3372 template <typename A
>
3373 bool Parser
<A
>::weakImportFromSymbol(const macho_nlist
<P
>& sym
)
3375 return ( ((sym
.n_type() & N_TYPE
) == N_UNDF
) && ((sym
.n_desc() & N_WEAK_REF
) != 0) );
3378 template <typename A
>
3379 bool Parser
<A
>::resolverFromSymbol(const macho_nlist
<P
>& sym
)
3381 return ( sym
.n_desc() & N_SYMBOL_RESOLVER
);
3384 template <typename A
>
3385 bool Parser
<A
>::altEntryFromSymbol(const macho_nlist
<P
>& sym
)
3387 return ( sym
.n_desc() & N_ALT_ENTRY
);
3391 /* Skip over a LEB128 value (signed or unsigned). */
3393 skip_leb128 (const uint8_t ** offset
, const uint8_t * end
)
3395 while (*offset
!= end
&& **offset
>= 0x80)
3401 /* Read a ULEB128 into a 64-bit word. Return (uint64_t)-1 on overflow
3402 or error. On overflow, skip past the rest of the uleb128. */
3404 read_uleb128 (const uint8_t ** offset
, const uint8_t * end
)
3406 uint64_t result
= 0;
3413 return (uint64_t) -1;
3415 b
= **offset
& 0x7f;
3417 if (bit
>= 64 || b
<< bit
>> bit
!= b
)
3418 result
= (uint64_t) -1;
3420 result
|= b
<< bit
, bit
+= 7;
3421 } while (*(*offset
)++ >= 0x80);
3426 /* Skip over a DWARF attribute of form FORM. */
3427 template <typename A
>
3428 bool Parser
<A
>::skip_form(const uint8_t ** offset
, const uint8_t * end
, uint64_t form
,
3429 uint8_t addr_size
, bool dwarf64
)
3439 case DW_FORM_block2
:
3440 if (end
- *offset
< 2)
3442 sz
= 2 + A::P::E::get16(*(uint16_t*)offset
);
3445 case DW_FORM_block4
:
3446 if (end
- *offset
< 4)
3448 sz
= 2 + A::P::E::get32(*(uint32_t*)offset
);
3466 case DW_FORM_string
:
3467 while (*offset
!= end
&& **offset
)
3476 sz
= read_uleb128 (offset
, end
);
3479 case DW_FORM_block1
:
3487 case DW_FORM_ref_udata
:
3488 skip_leb128 (offset
, end
);
3492 case DW_FORM_ref_addr
:
3496 case DW_FORM_sec_offset
:
3497 sz
= sizeof(typename
A::P::uint_t
);
3500 case DW_FORM_exprloc
:
3501 sz
= read_uleb128 (offset
, end
);
3504 case DW_FORM_flag_present
:
3508 case DW_FORM_ref_sig8
:
3515 if (end
- *offset
< sz
)
3522 template <typename A
>
3523 const char* Parser
<A
>::getDwarfString(uint64_t form
, const uint8_t*& di
)
3526 const char* dwarfStrings
;
3527 const char* result
= NULL
;
3529 case DW_FORM_string
:
3530 result
= (const char*)di
;
3531 di
+= strlen(result
) + 1;
3534 offset
= E::get32(*((uint32_t*)di
));
3535 dwarfStrings
= (char*)_file
->fileContent() + _file
->_dwarfDebugStringSect
->offset();
3536 if ( offset
< _file
->_dwarfDebugStringSect
->size() )
3537 result
= &dwarfStrings
[offset
];
3539 warning("dwarf DW_FORM_strp (offset=0x%08X) is too big in %s", offset
, this->_path
);
3543 warning("unknown dwarf string encoding (form=%lld) in %s", form
, this->_path
);
3549 template <typename A
>
3550 uint64_t Parser
<A
>::getDwarfOffset(uint64_t form
, const uint8_t*& di
, bool dwarf64
)
3552 if ( form
== DW_FORM_sec_offset
)
3553 form
= (dwarf64
? DW_FORM_data8
: DW_FORM_data4
);
3554 uint64_t result
= -1;
3557 result
= A::P::E::get32(*(uint32_t*)di
);
3561 result
= A::P::E::get64(*(uint64_t*)di
);
3565 warning("unknown dwarf DW_FORM_ for DW_AT_stmt_list in %s", this->_path
);
3571 template <typename A
>
3572 struct AtomAndLineInfo
{
3574 ld::Atom::LineInfo info
;
3578 // <rdar://problem/5591394> Add support to ld64 for N_FUN stabs when used for symbolic constants
3579 // Returns whether a stabStr belonging to an N_FUN stab represents a
3580 // symbolic constant rather than a function
3581 template <typename A
>
3582 bool Parser
<A
>::isConstFunStabs(const char *stabStr
)
3585 // N_FUN can be used for both constants and for functions. In case it's a constant,
3586 // the format of the stabs string is "symname:c=<value>;"
3587 // ':' cannot appear in the symbol name, except if it's an Objective-C method
3588 // (in which case the symbol name starts with + or -, and then it's definitely
3590 return (stabStr
!= NULL
) && (stabStr
[0] != '+') && (stabStr
[0] != '-')
3591 && ((colon
= strchr(stabStr
, ':')) != NULL
)
3592 && (colon
[1] == 'c') && (colon
[2] == '=');
3596 template <typename A
>
3597 void Parser
<A
>::parseDebugInfo()
3599 // check for dwarf __debug_info section
3600 if ( _file
->_dwarfDebugInfoSect
== NULL
) {
3601 // if no DWARF debug info, look for stabs
3605 if ( _file
->_dwarfDebugInfoSect
->size() == 0 )
3611 if ( !read_comp_unit(&tuName
, &tuDir
, &stmtList
) ) {
3612 // if can't parse dwarf, warn and give up
3613 _file
->_dwarfTranslationUnitPath
= NULL
;
3614 warning("can't parse dwarf compilation unit info in %s", _path
);
3615 _file
->_debugInfoKind
= ld::relocatable::File::kDebugInfoNone
;
3618 if ( (tuName
!= NULL
) && (tuName
[0] == '/') ) {
3619 _file
->_dwarfTranslationUnitPath
= tuName
;
3621 else if ( (tuDir
!= NULL
) && (tuName
!= NULL
) ) {
3622 asprintf((char**)&(_file
->_dwarfTranslationUnitPath
), "%s/%s", tuDir
, tuName
);
3624 else if ( tuDir
== NULL
) {
3625 _file
->_dwarfTranslationUnitPath
= tuName
;
3628 _file
->_dwarfTranslationUnitPath
= NULL
;
3631 // add line number info to atoms from dwarf
3632 std::vector
<AtomAndLineInfo
<A
> > entries
;
3633 entries
.reserve(64);
3634 if ( _file
->_debugInfoKind
== ld::relocatable::File::kDebugInfoDwarf
) {
3635 // file with just data will have no __debug_line info
3636 if ( (_file
->_dwarfDebugLineSect
!= NULL
) && (_file
->_dwarfDebugLineSect
->size() != 0) ) {
3637 // validate stmt_list
3638 if ( (stmtList
!= (uint64_t)-1) && (stmtList
< _file
->_dwarfDebugLineSect
->size()) ) {
3639 const uint8_t* debug_line
= (uint8_t*)_file
->fileContent() + _file
->_dwarfDebugLineSect
->offset();
3640 struct line_reader_data
* lines
= line_open(&debug_line
[stmtList
],
3641 _file
->_dwarfDebugLineSect
->size() - stmtList
, E::little_endian
);
3642 struct line_info result
;
3643 Atom
<A
>* curAtom
= NULL
;
3644 uint32_t curAtomOffset
= 0;
3645 uint32_t curAtomAddress
= 0;
3646 uint32_t curAtomSize
= 0;
3647 std::map
<uint32_t,const char*> dwarfIndexToFile
;
3648 if ( lines
!= NULL
) {
3649 while ( line_next(lines
, &result
, line_stop_pc
) ) {
3650 //fprintf(stderr, "curAtom=%p, result.pc=0x%llX, result.line=%llu, result.end_of_sequence=%d,"
3651 // " curAtomAddress=0x%X, curAtomSize=0x%X\n",
3652 // curAtom, result.pc, result.line, result.end_of_sequence, curAtomAddress, curAtomSize);
3653 // work around weird debug line table compiler generates if no functions in __text section
3654 if ( (curAtom
== NULL
) && (result
.pc
== 0) && result
.end_of_sequence
&& (result
.file
== 1))
3656 // for performance, see if in next pc is in current atom
3657 if ( (curAtom
!= NULL
) && (curAtomAddress
<= result
.pc
) && (result
.pc
< (curAtomAddress
+curAtomSize
)) ) {
3658 curAtomOffset
= result
.pc
- curAtomAddress
;
3660 // or pc at end of current atom
3661 else if ( result
.end_of_sequence
&& (curAtom
!= NULL
) && (result
.pc
== (curAtomAddress
+curAtomSize
)) ) {
3662 curAtomOffset
= result
.pc
- curAtomAddress
;
3664 // or only one function that is a one line function
3665 else if ( result
.end_of_sequence
&& (curAtom
== NULL
) && (this->findAtomByAddress(0) != NULL
) && (result
.pc
== this->findAtomByAddress(0)->size()) ) {
3666 curAtom
= this->findAtomByAddress(0);
3667 curAtomOffset
= result
.pc
- curAtom
->objectAddress();
3668 curAtomAddress
= curAtom
->objectAddress();
3669 curAtomSize
= curAtom
->size();
3672 // do slow look up of atom by address
3674 curAtom
= this->findAtomByAddress(result
.pc
);
3677 // in case of bug in debug info, don't abort link, just limp on
3680 if ( curAtom
== NULL
)
3681 break; // file has line info but no functions
3682 if ( result
.end_of_sequence
&& (curAtomAddress
+curAtomSize
< result
.pc
) ) {
3683 // a one line function can be returned by line_next() as one entry with pc at end of blob
3684 // look for alt atom starting at end of previous atom
3685 uint32_t previousEnd
= curAtomAddress
+curAtomSize
;
3686 Atom
<A
>* alt
= this->findAtomByAddressOrNullIfStub(previousEnd
);
3688 continue; // ignore spurious debug info for stubs
3689 if ( result
.pc
<= alt
->objectAddress() + alt
->size() ) {
3691 curAtomOffset
= result
.pc
- alt
->objectAddress();
3692 curAtomAddress
= alt
->objectAddress();
3693 curAtomSize
= alt
->size();
3696 curAtomOffset
= result
.pc
- curAtom
->objectAddress();
3697 curAtomAddress
= curAtom
->objectAddress();
3698 curAtomSize
= curAtom
->size();
3702 curAtomOffset
= result
.pc
- curAtom
->objectAddress();
3703 curAtomAddress
= curAtom
->objectAddress();
3704 curAtomSize
= curAtom
->size();
3707 const char* filename
;
3708 std::map
<uint32_t,const char*>::iterator pos
= dwarfIndexToFile
.find(result
.file
);
3709 if ( pos
== dwarfIndexToFile
.end() ) {
3710 filename
= line_file(lines
, result
.file
);
3711 dwarfIndexToFile
[result
.file
] = filename
;
3714 filename
= pos
->second
;
3716 // only record for ~8000 line info records per function
3717 if ( curAtom
->roomForMoreLineInfoCount() ) {
3718 AtomAndLineInfo
<A
> entry
;
3719 entry
.atom
= curAtom
;
3720 entry
.info
.atomOffset
= curAtomOffset
;
3721 entry
.info
.fileName
= filename
;
3722 entry
.info
.lineNumber
= result
.line
;
3723 //fprintf(stderr, "addr=0x%08llX, line=%lld, file=%s, atom=%s, atom.size=0x%X, end=%d\n",
3724 // result.pc, result.line, filename, curAtom->name(), curAtomSize, result.end_of_sequence);
3725 entries
.push_back(entry
);
3726 curAtom
->incrementLineInfoCount();
3728 if ( result
.end_of_sequence
) {
3738 // assign line info start offset for each atom
3739 uint8_t* p
= _file
->_atomsArray
;
3740 uint32_t liOffset
= 0;
3741 for(int i
=_file
->_atomsArrayCount
; i
> 0; --i
) {
3742 Atom
<A
>* atom
= (Atom
<A
>*)p
;
3743 atom
->_lineInfoStartIndex
= liOffset
;
3744 liOffset
+= atom
->_lineInfoCount
;
3745 atom
->_lineInfoCount
= 0;
3746 p
+= sizeof(Atom
<A
>);
3748 assert(liOffset
== entries
.size());
3749 _file
->_lineInfos
.resize(liOffset
);
3751 // copy each line info for each atom
3752 for (typename
std::vector
<AtomAndLineInfo
<A
> >::iterator it
= entries
.begin(); it
!= entries
.end(); ++it
) {
3753 uint32_t slot
= it
->atom
->_lineInfoStartIndex
+ it
->atom
->_lineInfoCount
;
3754 _file
->_lineInfos
[slot
] = it
->info
;
3755 it
->atom
->_lineInfoCount
++;
3758 // done with temp vector
3762 template <typename A
>
3763 void Parser
<A
>::parseStabs()
3765 // scan symbol table for stabs entries
3766 Atom
<A
>* currentAtom
= NULL
;
3767 pint_t currentAtomAddress
= 0;
3768 enum { start
, inBeginEnd
, inFun
} state
= start
;
3769 for (uint32_t symbolIndex
= 0; symbolIndex
< _symbolCount
; ++symbolIndex
) {
3770 const macho_nlist
<P
>& sym
= this->symbolFromIndex(symbolIndex
);
3771 bool useStab
= true;
3772 uint8_t type
= sym
.n_type();
3773 const char* symString
= (sym
.n_strx() != 0) ? this->nameFromSymbol(sym
) : NULL
;
3774 if ( (type
& N_STAB
) != 0 ) {
3775 _file
->_debugInfoKind
= (_hasUUID
? ld::relocatable::File::kDebugInfoStabsUUID
: ld::relocatable::File::kDebugInfoStabs
);
3776 ld::relocatable::File::Stab stab
;
3779 stab
.other
= sym
.n_sect();
3780 stab
.desc
= sym
.n_desc();
3781 stab
.value
= sym
.n_value();
3787 // beginning of function block
3789 // fall into case to lookup atom by addresss
3792 currentAtomAddress
= sym
.n_value();
3793 currentAtom
= this->findAtomByAddress(currentAtomAddress
);
3794 if ( currentAtom
!= NULL
) {
3795 stab
.atom
= currentAtom
;
3796 stab
.string
= symString
;
3799 fprintf(stderr
, "can't find atom for stabs BNSYM at %08llX in %s",
3800 (uint64_t)sym
.n_value(), _path
);
3810 // not associated with an atom, just copy
3811 stab
.string
= symString
;
3815 // n_value field is NOT atom address ;-(
3816 // need to find atom by name match
3817 const char* colon
= strchr(symString
, ':');
3818 if ( colon
!= NULL
) {
3819 // build underscore leading name
3820 int nameLen
= colon
- symString
;
3821 char symName
[nameLen
+2];
3822 strlcpy(&symName
[1], symString
, nameLen
+1);
3824 symName
[nameLen
+1] = '\0';
3825 currentAtom
= this->findAtomByName(symName
);
3826 if ( currentAtom
!= NULL
) {
3827 stab
.atom
= currentAtom
;
3828 stab
.string
= symString
;
3832 // might be a debug-note without trailing :G()
3833 currentAtom
= this->findAtomByName(symString
);
3834 if ( currentAtom
!= NULL
) {
3835 stab
.atom
= currentAtom
;
3836 stab
.string
= symString
;
3839 if ( stab
.atom
== NULL
) {
3840 // ld_classic added bogus GSYM stabs for old style dtrace probes
3841 if ( (strncmp(symString
, "__dtrace_probe$", 15) != 0) )
3842 warning("can't find atom for N_GSYM stabs %s in %s", symString
, _path
);
3848 if ( isConstFunStabs(symString
) ) {
3849 // constant not associated with a function
3850 stab
.string
= symString
;
3853 // old style stabs without BNSYM
3855 currentAtomAddress
= sym
.n_value();
3856 currentAtom
= this->findAtomByAddress(currentAtomAddress
);
3857 if ( currentAtom
!= NULL
) {
3858 stab
.atom
= currentAtom
;
3859 stab
.string
= symString
;
3862 warning("can't find atom for stabs FUN at %08llX in %s",
3863 (uint64_t)currentAtomAddress
, _path
);
3869 stab
.string
= symString
;
3875 stab
.string
= symString
;
3876 // -gfull built .o file
3879 warning("unknown stabs type 0x%X in %s", type
, _path
);
3883 stab
.atom
= currentAtom
;
3892 Atom
<A
>* nestedAtom
= this->findAtomByAddress(sym
.n_value());
3893 if ( nestedAtom
!= NULL
) {
3894 stab
.atom
= nestedAtom
;
3895 stab
.string
= symString
;
3898 warning("can't find atom for stabs 0x%X at %08llX in %s",
3899 type
, (uint64_t)sym
.n_value(), _path
);
3906 // adjust value to be offset in atom
3907 stab
.value
-= currentAtomAddress
;
3909 stab
.string
= symString
;
3916 if ( isConstFunStabs(symString
) ) {
3917 stab
.atom
= currentAtom
;
3918 stab
.string
= symString
;
3921 if ( sym
.n_sect() != 0 ) {
3922 // found another start stab, must be really old stabs...
3923 currentAtomAddress
= sym
.n_value();
3924 currentAtom
= this->findAtomByAddress(currentAtomAddress
);
3925 if ( currentAtom
!= NULL
) {
3926 stab
.atom
= currentAtom
;
3927 stab
.string
= symString
;
3930 warning("can't find atom for stabs FUN at %08llX in %s",
3931 (uint64_t)currentAtomAddress
, _path
);
3935 // found ending stab, switch back to start state
3936 stab
.string
= symString
;
3937 stab
.atom
= currentAtom
;
3946 // adjust value to be offset in atom
3947 stab
.value
-= currentAtomAddress
;
3948 stab
.atom
= currentAtom
;
3951 stab
.string
= symString
;
3955 stab
.atom
= currentAtom
;
3956 stab
.string
= symString
;
3961 // add to list of stabs for this .o file
3963 _file
->_stabs
.push_back(stab
);
3970 // Look at the compilation unit DIE and determine
3971 // its NAME, compilation directory (in COMP_DIR) and its
3972 // line number information offset (in STMT_LIST). NAME and COMP_DIR
3973 // may be NULL (especially COMP_DIR) if they are not in the .o file;
3974 // STMT_LIST will be (uint64_t) -1.
3976 // At present this assumes that there's only one compilation unit DIE.
3978 template <typename A
>
3979 bool Parser
<A
>::read_comp_unit(const char ** name
, const char ** comp_dir
,
3980 uint64_t *stmt_list
)
3982 const uint8_t * debug_info
;
3983 const uint8_t * debug_abbrev
;
3985 const uint8_t * next_cu
;
3987 const uint8_t * end
;
3988 const uint8_t * enda
;
3991 uint64_t abbrev_base
;
3993 uint8_t address_size
;
3998 *stmt_list
= (uint64_t) -1;
4000 if ( (_file
->_dwarfDebugInfoSect
== NULL
) || (_file
->_dwarfDebugAbbrevSect
== NULL
) )
4003 if (_file
->_dwarfDebugInfoSect
->size() < 12)
4004 /* Too small to be a real debug_info section. */
4007 debug_info
= (uint8_t*)_file
->fileContent() + _file
->_dwarfDebugInfoSect
->offset();
4008 debug_abbrev
= (uint8_t*)_file
->fileContent() + _file
->_dwarfDebugAbbrevSect
->offset();
4009 next_cu
= debug_info
;
4011 while ((uint64_t)(next_cu
- debug_info
) < _file
->_dwarfDebugInfoSect
->size()) {
4013 sz
= A::P::E::get32(*(uint32_t*)di
);
4015 dwarf64
= sz
== 0xffffffff;
4017 sz
= A::P::E::get64(*(uint64_t*)di
), di
+= 8;
4018 else if (sz
> 0xffffff00)
4019 /* Unknown dwarf format. */
4022 /* Verify claimed size. */
4023 if (sz
+ (di
- debug_info
) > _file
->_dwarfDebugInfoSect
->size() || sz
<= (dwarf64
? 23 : 11))
4028 vers
= A::P::E::get16(*(uint16_t*)di
);
4029 if (vers
< 2 || vers
> 4)
4030 /* DWARF version wrong for this code.
4031 Chances are we could continue anyway, but we don't know for sure. */
4035 /* Find the debug_abbrev section. */
4036 abbrev_base
= dwarf64
? A::P::E::get64(*(uint64_t*)di
) : A::P::E::get32(*(uint32_t*)di
);
4037 di
+= dwarf64
? 8 : 4;
4039 if (abbrev_base
> _file
->_dwarfDebugAbbrevSect
->size())
4041 da
= debug_abbrev
+ abbrev_base
;
4042 enda
= debug_abbrev
+ _file
->_dwarfDebugAbbrevSect
->size();
4044 address_size
= *di
++;
4046 /* Find the abbrev number we're looking for. */
4048 abbrev
= read_uleb128 (&di
, end
);
4049 if (abbrev
== (uint64_t) -1)
4052 /* Skip through the debug_abbrev section looking for that abbrev. */
4055 uint64_t this_abbrev
= read_uleb128 (&da
, enda
);
4058 if (this_abbrev
== abbrev
)
4059 /* This is almost always taken. */
4061 skip_leb128 (&da
, enda
); /* Skip the tag. */
4064 da
++; /* Skip the DW_CHILDREN_* value. */
4067 attr
= read_uleb128 (&da
, enda
);
4068 skip_leb128 (&da
, enda
);
4069 } while (attr
!= 0 && attr
!= (uint64_t) -1);
4074 /* Check that the abbrev is one for a DW_TAG_compile_unit. */
4075 if (read_uleb128 (&da
, enda
) != DW_TAG_compile_unit
)
4079 da
++; /* Skip the DW_CHILDREN_* value. */
4081 /* Now, go through the DIE looking for DW_AT_name,
4082 DW_AT_comp_dir, and DW_AT_stmt_list. */
4083 bool skip_to_next_cu
= false;
4084 while (!skip_to_next_cu
) {
4086 uint64_t attr
= read_uleb128 (&da
, enda
);
4087 uint64_t form
= read_uleb128 (&da
, enda
);
4089 if (attr
== (uint64_t) -1)
4093 if (form
== DW_FORM_indirect
)
4094 form
= read_uleb128 (&di
, end
);
4098 *name
= getDwarfString(form
, di
);
4099 /* Swift object files may contain two CUs: One
4100 describes the Swift code, one is created by the
4101 clang importer. Skip over the CU created by the
4102 clang importer as it may be empty. */
4103 if (std::string(*name
) == "<swift-imported-modules>")
4104 skip_to_next_cu
= true;
4106 case DW_AT_comp_dir
:
4107 *comp_dir
= getDwarfString(form
, di
);
4109 case DW_AT_stmt_list
:
4110 *stmt_list
= getDwarfOffset(form
, di
, dwarf64
);
4113 if (! skip_form (&di
, end
, form
, address_size
, dwarf64
))
4123 template <typename A
>
4126 free(_sectionsArray
);
4130 template <typename A
>
4131 const char* File
<A
>::translationUnitSource() const
4133 return _dwarfTranslationUnitPath
;
4136 template <typename A
>
4137 bool File
<A
>::forEachAtom(ld::File::AtomHandler
& handler
) const
4139 handler
.doFile(*this);
4140 uint8_t* p
= _atomsArray
;
4141 for(int i
=_atomsArrayCount
; i
> 0; --i
) {
4142 handler
.doAtom(*((Atom
<A
>*)p
));
4143 p
+= sizeof(Atom
<A
>);
4145 p
= _aliasAtomsArray
;
4146 for(int i
=_aliasAtomsArrayCount
; i
> 0; --i
) {
4147 handler
.doAtom(*((AliasAtom
*)p
));
4148 p
+= sizeof(AliasAtom
);
4151 return (_atomsArrayCount
!= 0) || (_aliasAtomsArrayCount
!= 0);
4154 template <typename A
>
4155 const char* Section
<A
>::makeSegmentName(const macho_section
<typename
A::P
>* sect
)
4157 // mach-o section record only has room for 16-byte seg/sect names
4158 // so a 16-byte name has no trailing zero
4159 const char* name
= sect
->segname();
4160 if ( strlen(name
) < 16 )
4162 char* tmp
= new char[17];
4163 strlcpy(tmp
, name
, 17);
4167 template <typename A
>
4168 const char* Section
<A
>::makeSectionName(const macho_section
<typename
A::P
>* sect
)
4170 const char* name
= sect
->sectname();
4171 if ( strlen(name
) < 16 )
4174 // special case common long section names so we don't have to malloc
4175 if ( strncmp(sect
->sectname(), "__objc_classrefs", 16) == 0 )
4176 return "__objc_classrefs";
4177 if ( strncmp(sect
->sectname(), "__objc_classlist", 16) == 0 )
4178 return "__objc_classlist";
4179 if ( strncmp(sect
->sectname(), "__objc_nlclslist", 16) == 0 )
4180 return "__objc_nlclslist";
4181 if ( strncmp(sect
->sectname(), "__objc_nlcatlist", 16) == 0 )
4182 return "__objc_nlcatlist";
4183 if ( strncmp(sect
->sectname(), "__objc_protolist", 16) == 0 )
4184 return "__objc_protolist";
4185 if ( strncmp(sect
->sectname(), "__objc_protorefs", 16) == 0 )
4186 return "__objc_protorefs";
4187 if ( strncmp(sect
->sectname(), "__objc_superrefs", 16) == 0 )
4188 return "__objc_superrefs";
4189 if ( strncmp(sect
->sectname(), "__objc_imageinfo", 16) == 0 )
4190 return "__objc_imageinfo";
4191 if ( strncmp(sect
->sectname(), "__objc_stringobj", 16) == 0 )
4192 return "__objc_stringobj";
4193 if ( strncmp(sect
->sectname(), "__gcc_except_tab", 16) == 0 )
4194 return "__gcc_except_tab";
4196 char* tmp
= new char[17];
4197 strlcpy(tmp
, name
, 17);
4201 template <typename A
>
4202 bool Section
<A
>::readable(const macho_section
<typename
A::P
>* sect
)
4207 template <typename A
>
4208 bool Section
<A
>::writable(const macho_section
<typename
A::P
>* sect
)
4210 // mach-o .o files do not contain segment permissions
4211 // we just know TEXT is special
4212 return ( strcmp(sect
->segname(), "__TEXT") != 0 );
4215 template <typename A
>
4216 bool Section
<A
>::exectuable(const macho_section
<typename
A::P
>* sect
)
4218 // mach-o .o files do not contain segment permissions
4219 // we just know TEXT is special
4220 return ( strcmp(sect
->segname(), "__TEXT") == 0 );
4224 template <typename A
>
4225 ld::Section::Type Section
<A
>::sectionType(const macho_section
<typename
A::P
>* sect
)
4227 switch ( sect
->flags() & SECTION_TYPE
) {
4229 return ld::Section::typeZeroFill
;
4230 case S_CSTRING_LITERALS
:
4231 if ( (strcmp(sect
->sectname(), "__cstring") == 0) && (strcmp(sect
->segname(), "__TEXT") == 0) )
4232 return ld::Section::typeCString
;
4234 return ld::Section::typeNonStdCString
;
4235 case S_4BYTE_LITERALS
:
4236 return ld::Section::typeLiteral4
;
4237 case S_8BYTE_LITERALS
:
4238 return ld::Section::typeLiteral8
;
4239 case S_LITERAL_POINTERS
:
4240 return ld::Section::typeCStringPointer
;
4241 case S_NON_LAZY_SYMBOL_POINTERS
:
4242 return ld::Section::typeNonLazyPointer
;
4243 case S_LAZY_SYMBOL_POINTERS
:
4244 return ld::Section::typeLazyPointer
;
4245 case S_SYMBOL_STUBS
:
4246 return ld::Section::typeStub
;
4247 case S_MOD_INIT_FUNC_POINTERS
:
4248 return ld::Section::typeInitializerPointers
;
4249 case S_MOD_TERM_FUNC_POINTERS
:
4250 return ld::Section::typeTerminatorPointers
;
4252 return ld::Section::typeUnclassified
;
4253 case S_16BYTE_LITERALS
:
4254 return ld::Section::typeLiteral16
;
4257 if ( sect
->flags() & S_ATTR_PURE_INSTRUCTIONS
) {
4258 return ld::Section::typeCode
;
4260 else if ( strcmp(sect
->segname(), "__TEXT") == 0 ) {
4261 if ( strcmp(sect
->sectname(), "__eh_frame") == 0 )
4262 return ld::Section::typeCFI
;
4263 else if ( strcmp(sect
->sectname(), "__ustring") == 0 )
4264 return ld::Section::typeUTF16Strings
;
4265 else if ( strcmp(sect
->sectname(), "__textcoal_nt") == 0 )
4266 return ld::Section::typeCode
;
4267 else if ( strcmp(sect
->sectname(), "__StaticInit") == 0 )
4268 return ld::Section::typeCode
;
4269 else if ( strcmp(sect
->sectname(), "__constructor") == 0 )
4270 return ld::Section::typeInitializerPointers
;
4272 else if ( strcmp(sect
->segname(), "__DATA") == 0 ) {
4273 if ( strcmp(sect
->sectname(), "__cfstring") == 0 )
4274 return ld::Section::typeCFString
;
4275 else if ( strcmp(sect
->sectname(), "__dyld") == 0 )
4276 return ld::Section::typeDyldInfo
;
4277 else if ( strcmp(sect
->sectname(), "__program_vars") == 0 )
4278 return ld::Section::typeDyldInfo
;
4279 else if ( strncmp(sect
->sectname(), "__objc_classrefs", 16) == 0 )
4280 return ld::Section::typeObjCClassRefs
;
4281 else if ( strcmp(sect
->sectname(), "__objc_catlist") == 0 )
4282 return ld::Section::typeObjC2CategoryList
;
4284 else if ( strcmp(sect
->segname(), "__OBJC") == 0 ) {
4285 if ( strcmp(sect
->sectname(), "__class") == 0 )
4286 return ld::Section::typeObjC1Classes
;
4289 case S_THREAD_LOCAL_REGULAR
:
4290 return ld::Section::typeTLVInitialValues
;
4291 case S_THREAD_LOCAL_ZEROFILL
:
4292 return ld::Section::typeTLVZeroFill
;
4293 case S_THREAD_LOCAL_VARIABLES
:
4294 return ld::Section::typeTLVDefs
;
4295 case S_THREAD_LOCAL_VARIABLE_POINTERS
:
4296 return ld::Section::typeTLVPointers
;
4297 case S_THREAD_LOCAL_INIT_FUNCTION_POINTERS
:
4298 return ld::Section::typeTLVInitializerPointers
;
4300 return ld::Section::typeUnclassified
;
4304 template <typename A
>
4305 Atom
<A
>* Section
<A
>::findContentAtomByAddress(pint_t addr
, class Atom
<A
>* start
, class Atom
<A
>* end
)
4307 // do a binary search of atom array
4308 uint32_t atomCount
= end
- start
;
4309 Atom
<A
>* base
= start
;
4310 for (uint32_t n
= atomCount
; n
> 0; n
/= 2) {
4311 Atom
<A
>* pivot
= &base
[n
/2];
4312 pint_t atomStartAddr
= pivot
->_objAddress
;
4313 pint_t atomEndAddr
= atomStartAddr
+ pivot
->_size
;
4314 if ( atomStartAddr
<= addr
) {
4315 // address in normal atom
4316 if (addr
< atomEndAddr
)
4318 // address in "end" label (but not in alias)
4319 if ( (pivot
->_size
== 0) && (addr
== atomEndAddr
) && !pivot
->isAlias() )
4322 if ( addr
>= atomEndAddr
) {
4324 // move base to atom after pivot
4336 template <typename A
>
4337 ld::Atom::Alignment Section
<A
>::alignmentForAddress(pint_t addr
)
4339 const uint32_t sectionAlignment
= this->_machOSection
->align();
4340 uint32_t modulus
= (addr
% (1 << sectionAlignment
));
4341 if ( modulus
> 0xFFFF )
4342 warning("alignment for symbol at address 0x%08llX in %s exceeds 2^16", (uint64_t)addr
, this->file().path());
4343 return ld::Atom::Alignment(sectionAlignment
, modulus
);
4346 template <typename A
>
4347 uint32_t Section
<A
>::sectionNum(class Parser
<A
>& parser
) const
4349 if ( _machOSection
== NULL
)
4352 return 1 + (this->_machOSection
- parser
.firstMachOSection());
4355 // arm does not have zero cost exceptions
4357 uint32_t CFISection
<arm
>::cfiCount(Parser
<arm
>& parser
)
4359 if ( parser
.armUsesZeroCostExceptions() ) {
4360 // create ObjectAddressSpace object for use by libunwind
4361 OAS
oas(*this, (uint8_t*)this->file().fileContent()+this->_machOSection
->offset());
4362 return libunwind::CFI_Parser
<OAS
>::getCFICount(oas
,
4363 this->_machOSection
->addr(), this->_machOSection
->size());
4368 template <typename A
>
4369 uint32_t CFISection
<A
>::cfiCount(Parser
<A
>& parser
)
4371 // create ObjectAddressSpace object for use by libunwind
4372 OAS
oas(*this, (uint8_t*)this->file().fileContent()+this->_machOSection
->offset());
4373 return libunwind::CFI_Parser
<OAS
>::getCFICount(oas
,
4374 this->_machOSection
->addr(), this->_machOSection
->size());
4377 template <typename A
>
4378 void CFISection
<A
>::warnFunc(void* ref
, uint64_t funcAddr
, const char* msg
)
4380 Parser
<A
>* parser
= (Parser
<A
>*)ref
;
4381 if ( ! parser
->warnUnwindConversionProblems() )
4383 if ( funcAddr
!= CFI_INVALID_ADDRESS
) {
4384 // atoms are not constructed yet, so scan symbol table for labels
4385 const char* name
= parser
->scanSymbolTableForAddress(funcAddr
);
4386 warning("could not create compact unwind for %s: %s", name
, msg
);
4389 warning("could not create compact unwind: %s", msg
);
4394 bool CFISection
<x86_64
>::needsRelocating()
4400 bool CFISection
<arm64
>::needsRelocating()
4406 template <typename A
>
4407 bool CFISection
<A
>::needsRelocating()
4413 void CFISection
<x86_64
>::cfiParse(class Parser
<x86_64
>& parser
, uint8_t* buffer
,
4414 libunwind::CFI_Atom_Info
<CFISection
<x86_64
>::OAS
>::CFI_Atom_Info cfiArray
[],
4415 uint32_t& count
, const pint_t cuStarts
[], uint32_t cuCount
)
4417 const uint32_t sectionSize
= this->_machOSection
->size();
4418 // copy __eh_frame data to buffer
4419 memcpy(buffer
, file().fileContent() + this->_machOSection
->offset(), sectionSize
);
4421 // and apply relocations
4422 const macho_relocation_info
<P
>* relocs
= (macho_relocation_info
<P
>*)(file().fileContent() + this->_machOSection
->reloff());
4423 const macho_relocation_info
<P
>* relocsEnd
= &relocs
[this->_machOSection
->nreloc()];
4424 for (const macho_relocation_info
<P
>* reloc
= relocs
; reloc
< relocsEnd
; ++reloc
) {
4426 switch ( reloc
->r_type() ) {
4427 case X86_64_RELOC_SUBTRACTOR
:
4428 value
= 0 - parser
.symbolFromIndex(reloc
->r_symbolnum()).n_value();
4430 if ( reloc
->r_extern() )
4431 value
+= parser
.symbolFromIndex(reloc
->r_symbolnum()).n_value();
4433 case X86_64_RELOC_UNSIGNED
:
4434 value
= parser
.symbolFromIndex(reloc
->r_symbolnum()).n_value();
4436 case X86_64_RELOC_GOT
:
4437 // this is used for the reference to the personality function in CIEs
4438 // store the symbol number of the personality function for later use as a Fixup
4439 value
= reloc
->r_symbolnum();
4442 fprintf(stderr
, "CFISection::cfiParse() unexpected relocation type at r_address=0x%08X\n", reloc
->r_address());
4445 if ( reloc
->r_address() > sectionSize
)
4446 throwf("malformed __eh_frame relocation, offset (0x%08X) is beyond end of section,", reloc
->r_address());
4449 switch ( reloc
->r_length() ) {
4451 p64
= (uint64_t*)&buffer
[reloc
->r_address()];
4452 E::set64(*p64
, value
+ E::get64(*p64
));
4455 p32
= (uint32_t*)&buffer
[reloc
->r_address()];
4456 E::set32(*p32
, value
+ E::get32(*p32
));
4459 fprintf(stderr
, "CFISection::cfiParse() unexpected relocation size at r_address=0x%08X\n", reloc
->r_address());
4464 // create ObjectAddressSpace object for use by libunwind
4465 OAS
oas(*this, buffer
);
4467 // use libuwind to parse __eh_frame data into array of CFI_Atom_Info
4469 msg
= libunwind::DwarfInstructions
<OAS
, libunwind::Registers_x86_64
>::parseCFIs(
4470 oas
, this->_machOSection
->addr(), this->_machOSection
->size(),
4471 cuStarts
, cuCount
, parser
.keepDwarfUnwind(), parser
.forceDwarfConversion(), parser
.neverConvertDwarf(),
4472 cfiArray
, count
, (void*)&parser
, warnFunc
);
4474 throwf("malformed __eh_frame section: %s", msg
);
4478 void CFISection
<x86
>::cfiParse(class Parser
<x86
>& parser
, uint8_t* buffer
,
4479 libunwind::CFI_Atom_Info
<CFISection
<x86
>::OAS
>::CFI_Atom_Info cfiArray
[],
4480 uint32_t& count
, const pint_t cuStarts
[], uint32_t cuCount
)
4482 // create ObjectAddressSpace object for use by libunwind
4483 OAS
oas(*this, (uint8_t*)this->file().fileContent()+this->_machOSection
->offset());
4485 // use libuwind to parse __eh_frame data into array of CFI_Atom_Info
4487 msg
= libunwind::DwarfInstructions
<OAS
, libunwind::Registers_x86
>::parseCFIs(
4488 oas
, this->_machOSection
->addr(), this->_machOSection
->size(),
4489 cuStarts
, cuCount
, parser
.keepDwarfUnwind(), parser
.forceDwarfConversion(), parser
.neverConvertDwarf(),
4490 cfiArray
, count
, (void*)&parser
, warnFunc
);
4492 throwf("malformed __eh_frame section: %s", msg
);
4499 void CFISection
<arm
>::cfiParse(class Parser
<arm
>& parser
, uint8_t* buffer
,
4500 libunwind::CFI_Atom_Info
<CFISection
<arm
>::OAS
>::CFI_Atom_Info cfiArray
[],
4501 uint32_t& count
, const pint_t cuStarts
[], uint32_t cuCount
)
4503 if ( !parser
.armUsesZeroCostExceptions() ) {
4504 // most arm do not use zero cost exceptions
4508 // create ObjectAddressSpace object for use by libunwind
4509 OAS
oas(*this, (uint8_t*)this->file().fileContent()+this->_machOSection
->offset());
4511 // use libuwind to parse __eh_frame data into array of CFI_Atom_Info
4513 msg
= libunwind::DwarfInstructions
<OAS
, libunwind::Registers_arm
>::parseCFIs(
4514 oas
, this->_machOSection
->addr(), this->_machOSection
->size(),
4515 cuStarts
, cuCount
, parser
.keepDwarfUnwind(), parser
.forceDwarfConversion(), parser
.neverConvertDwarf(),
4516 cfiArray
, count
, (void*)&parser
, warnFunc
);
4518 throwf("malformed __eh_frame section: %s", msg
);
4525 void CFISection
<arm64
>::cfiParse(class Parser
<arm64
>& parser
, uint8_t* buffer
,
4526 libunwind::CFI_Atom_Info
<CFISection
<arm64
>::OAS
>::CFI_Atom_Info cfiArray
[],
4527 uint32_t& count
, const pint_t cuStarts
[], uint32_t cuCount
)
4529 // copy __eh_frame data to buffer
4530 const uint32_t sectionSize
= this->_machOSection
->size();
4531 memcpy(buffer
, file().fileContent() + this->_machOSection
->offset(), sectionSize
);
4533 // and apply relocations
4534 const macho_relocation_info
<P
>* relocs
= (macho_relocation_info
<P
>*)(file().fileContent() + this->_machOSection
->reloff());
4535 const macho_relocation_info
<P
>* relocsEnd
= &relocs
[this->_machOSection
->nreloc()];
4536 for (const macho_relocation_info
<P
>* reloc
= relocs
; reloc
< relocsEnd
; ++reloc
) {
4537 uint64_t* p64
= (uint64_t*)&buffer
[reloc
->r_address()];
4538 uint32_t* p32
= (uint32_t*)&buffer
[reloc
->r_address()];
4539 uint32_t addend32
= E::get32(*p32
);
4540 uint64_t addend64
= E::get64(*p64
);
4542 switch ( reloc
->r_type() ) {
4543 case ARM64_RELOC_SUBTRACTOR
:
4544 value
= 0 - parser
.symbolFromIndex(reloc
->r_symbolnum()).n_value();
4546 if ( reloc
->r_extern() )
4547 value
+= parser
.symbolFromIndex(reloc
->r_symbolnum()).n_value();
4549 case ARM64_RELOC_UNSIGNED
:
4550 value
= parser
.symbolFromIndex(reloc
->r_symbolnum()).n_value();
4552 case ARM64_RELOC_POINTER_TO_GOT
:
4553 // this is used for the reference to the personality function in CIEs
4554 // store the symbol number of the personality function for later use as a Fixup
4555 value
= reloc
->r_symbolnum();
4560 fprintf(stderr
, "CFISection::cfiParse() unexpected relocation type at r_address=0x%08X\n", reloc
->r_address());
4563 if ( reloc
->r_address() > sectionSize
)
4564 throwf("malformed __eh_frame relocation, offset (0x%08X) is beyond end of section,", reloc
->r_address());
4565 switch ( reloc
->r_length() ) {
4567 E::set64(*p64
, value
+ addend64
);
4570 E::set32(*p32
, value
+ addend32
);
4573 fprintf(stderr
, "CFISection::cfiParse() unexpected relocation size at r_address=0x%08X\n", reloc
->r_address());
4579 // create ObjectAddressSpace object for use by libunwind
4580 OAS
oas(*this, buffer
);
4582 // use libuwind to parse __eh_frame data into array of CFI_Atom_Info
4584 msg
= libunwind::DwarfInstructions
<OAS
, libunwind::Registers_arm64
>::parseCFIs(
4585 oas
, this->_machOSection
->addr(), this->_machOSection
->size(),
4586 cuStarts
, cuCount
, parser
.keepDwarfUnwind(), parser
.forceDwarfConversion(), parser
.neverConvertDwarf(),
4587 cfiArray
, count
, (void*)&parser
, warnFunc
);
4589 throwf("malformed __eh_frame section: %s", msg
);
4593 template <typename A
>
4594 uint32_t CFISection
<A
>::computeAtomCount(class Parser
<A
>& parser
,
4595 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
4596 const struct Parser
<A
>::CFI_CU_InfoArrays
& cfis
)
4598 return cfis
.cfiCount
;
4603 template <typename A
>
4604 uint32_t CFISection
<A
>::appendAtoms(class Parser
<A
>& parser
, uint8_t* p
,
4605 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
4606 const struct Parser
<A
>::CFI_CU_InfoArrays
& cfis
)
4608 this->_beginAtoms
= (Atom
<A
>*)p
;
4609 // walk CFI_Atom_Info array and create atom for each entry
4610 const CFI_Atom_Info
* start
= &cfis
.cfiArray
[0];
4611 const CFI_Atom_Info
* end
= &cfis
.cfiArray
[cfis
.cfiCount
];
4612 for(const CFI_Atom_Info
* a
=start
; a
< end
; ++a
) {
4613 Atom
<A
>* space
= (Atom
<A
>*)p
;
4614 new (space
) Atom
<A
>(*this, (a
->isCIE
? "CIE" : "FDE"), a
->address
, a
->size
,
4615 ld::Atom::definitionRegular
, ld::Atom::combineNever
, ld::Atom::scopeTranslationUnit
,
4616 ld::Atom::typeCFI
, ld::Atom::symbolTableNotInFinalLinkedImages
,
4617 false, false, false, ld::Atom::Alignment(0));
4618 p
+= sizeof(Atom
<A
>);
4620 this->_endAtoms
= (Atom
<A
>*)p
;
4621 return cfis
.cfiCount
;
4625 template <> bool CFISection
<x86_64
>::bigEndian() { return false; }
4626 template <> bool CFISection
<x86
>::bigEndian() { return false; }
4627 template <> bool CFISection
<arm
>::bigEndian() { return false; }
4628 template <> bool CFISection
<arm64
>::bigEndian() { return false; }
4631 void CFISection
<x86_64
>::addCiePersonalityFixups(class Parser
<x86_64
>& parser
, const CFI_Atom_Info
* cieInfo
)
4633 uint8_t personalityEncoding
= cieInfo
->u
.cieInfo
.personality
.encodingOfTargetAddress
;
4634 if ( personalityEncoding
== 0x9B ) {
4635 // compiler always produces X86_64_RELOC_GOT with addend of 4 to personality function
4636 // CFISection<x86_64>::cfiParse() set targetAddress to be symbolIndex + 4 + addressInCIE
4637 uint32_t symbolIndex
= cieInfo
->u
.cieInfo
.personality
.targetAddress
- 4
4638 - cieInfo
->address
- cieInfo
->u
.cieInfo
.personality
.offsetInCFI
;
4639 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(symbolIndex
);
4640 const char* personalityName
= parser
.nameFromSymbol(sym
);
4642 Atom
<x86_64
>* cieAtom
= this->findAtomByAddress(cieInfo
->address
);
4643 Parser
<x86_64
>::SourceLocation
src(cieAtom
, cieInfo
->u
.cieInfo
.personality
.offsetInCFI
);
4644 parser
.addFixup(src
, ld::Fixup::k1of3
, ld::Fixup::kindSetTargetAddress
, false, personalityName
);
4645 parser
.addFixup(src
, ld::Fixup::k2of3
, ld::Fixup::kindAddAddend
, 4);
4646 parser
.addFixup(src
, ld::Fixup::k3of3
, ld::Fixup::kindStoreX86PCRel32GOT
);
4648 else if ( personalityEncoding
!= 0 ) {
4649 throwf("unsupported address encoding (%02X) of personality function in CIE",
4650 personalityEncoding
);
4655 void CFISection
<x86
>::addCiePersonalityFixups(class Parser
<x86
>& parser
, const CFI_Atom_Info
* cieInfo
)
4657 uint8_t personalityEncoding
= cieInfo
->u
.cieInfo
.personality
.encodingOfTargetAddress
;
4658 if ( (personalityEncoding
== 0x9B) || (personalityEncoding
== 0x90) ) {
4659 uint32_t offsetInCFI
= cieInfo
->u
.cieInfo
.personality
.offsetInCFI
;
4660 uint32_t nlpAddr
= cieInfo
->u
.cieInfo
.personality
.targetAddress
;
4661 Atom
<x86
>* cieAtom
= this->findAtomByAddress(cieInfo
->address
);
4662 Atom
<x86
>* nlpAtom
= parser
.findAtomByAddress(nlpAddr
);
4663 assert(nlpAtom
->contentType() == ld::Atom::typeNonLazyPointer
);
4664 Parser
<x86
>::SourceLocation
src(cieAtom
, cieInfo
->u
.cieInfo
.personality
.offsetInCFI
);
4666 parser
.addFixup(src
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, nlpAtom
);
4667 parser
.addFixup(src
, ld::Fixup::k2of4
, ld::Fixup::kindSubtractTargetAddress
, cieAtom
);
4668 parser
.addFixup(src
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractAddend
, offsetInCFI
);
4669 parser
.addFixup(src
, ld::Fixup::k4of4
, ld::Fixup::kindStoreLittleEndian32
);
4671 else if ( personalityEncoding
!= 0 ) {
4672 throwf("unsupported address encoding (%02X) of personality function in CIE", personalityEncoding
);
4676 #if SUPPORT_ARCH_arm64
4678 void CFISection
<arm64
>::addCiePersonalityFixups(class Parser
<arm64
>& parser
, const CFI_Atom_Info
* cieInfo
)
4680 uint8_t personalityEncoding
= cieInfo
->u
.cieInfo
.personality
.encodingOfTargetAddress
;
4681 if ( personalityEncoding
== 0x9B ) {
4682 // compiler always produces ARM64_RELOC_GOT r_pcrel=1 to personality function
4683 // CFISection<arm64>::cfiParse() set targetAddress to be symbolIndex + addressInCIE
4684 uint32_t symbolIndex
= cieInfo
->u
.cieInfo
.personality
.targetAddress
4685 - cieInfo
->address
- cieInfo
->u
.cieInfo
.personality
.offsetInCFI
;
4686 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(symbolIndex
);
4687 const char* personalityName
= parser
.nameFromSymbol(sym
);
4689 Atom
<arm64
>* cieAtom
= this->findAtomByAddress(cieInfo
->address
);
4690 Parser
<arm64
>::SourceLocation
src(cieAtom
, cieInfo
->u
.cieInfo
.personality
.offsetInCFI
);
4691 parser
.addFixup(src
, ld::Fixup::k1of2
, ld::Fixup::kindSetTargetAddress
, false, personalityName
);
4692 parser
.addFixup(src
, ld::Fixup::k2of2
, ld::Fixup::kindStoreARM64PCRelToGOT
);
4694 else if ( personalityEncoding
!= 0 ) {
4695 throwf("unsupported address encoding (%02X) of personality function in CIE",
4696 personalityEncoding
);
4703 void CFISection
<arm
>::addCiePersonalityFixups(class Parser
<arm
>& parser
, const CFI_Atom_Info
* cieInfo
)
4705 uint8_t personalityEncoding
= cieInfo
->u
.cieInfo
.personality
.encodingOfTargetAddress
;
4706 if ( (personalityEncoding
== 0x9B) || (personalityEncoding
== 0x90) ) {
4707 uint32_t offsetInCFI
= cieInfo
->u
.cieInfo
.personality
.offsetInCFI
;
4708 uint32_t nlpAddr
= cieInfo
->u
.cieInfo
.personality
.targetAddress
;
4709 Atom
<arm
>* cieAtom
= this->findAtomByAddress(cieInfo
->address
);
4710 Atom
<arm
>* nlpAtom
= parser
.findAtomByAddress(nlpAddr
);
4711 assert(nlpAtom
->contentType() == ld::Atom::typeNonLazyPointer
);
4712 Parser
<arm
>::SourceLocation
src(cieAtom
, cieInfo
->u
.cieInfo
.personality
.offsetInCFI
);
4714 parser
.addFixup(src
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, nlpAtom
);
4715 parser
.addFixup(src
, ld::Fixup::k2of4
, ld::Fixup::kindSubtractTargetAddress
, cieAtom
);
4716 parser
.addFixup(src
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractAddend
, offsetInCFI
);
4717 parser
.addFixup(src
, ld::Fixup::k4of4
, ld::Fixup::kindStoreLittleEndian32
);
4719 else if ( personalityEncoding
!= 0 ) {
4720 throwf("unsupported address encoding (%02X) of personality function in CIE", personalityEncoding
);
4726 template <typename A
>
4727 void CFISection
<A
>::addCiePersonalityFixups(class Parser
<A
>& parser
, const CFI_Atom_Info
* cieInfo
)
4729 assert(0 && "addCiePersonalityFixups() not implemented for arch");
4732 template <typename A
>
4733 void CFISection
<A
>::makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
& cfis
)
4735 ld::Fixup::Kind store32
= bigEndian() ? ld::Fixup::kindStoreBigEndian32
: ld::Fixup::kindStoreLittleEndian32
;
4736 ld::Fixup::Kind store64
= bigEndian() ? ld::Fixup::kindStoreBigEndian64
: ld::Fixup::kindStoreLittleEndian64
;
4738 // add all references for FDEs, including implicit group references
4739 const CFI_Atom_Info
* end
= &cfis
.cfiArray
[cfis
.cfiCount
];
4740 for(const CFI_Atom_Info
* p
= &cfis
.cfiArray
[0]; p
< end
; ++p
) {
4742 // add reference to personality function if used
4743 if ( p
->u
.cieInfo
.personality
.targetAddress
!= CFI_INVALID_ADDRESS
) {
4744 this->addCiePersonalityFixups(parser
, p
);
4749 Atom
<A
>* fdeAtom
= this->findAtomByAddress(p
->address
);
4750 // find function Atom
4751 Atom
<A
>* functionAtom
= parser
.findAtomByAddress(p
->u
.fdeInfo
.function
.targetAddress
);
4753 Atom
<A
>* cieAtom
= this->findAtomByAddress(p
->u
.fdeInfo
.cie
.targetAddress
);
4755 Atom
<A
>* lsdaAtom
= NULL
;
4756 if ( p
->u
.fdeInfo
.lsda
.targetAddress
!= CFI_INVALID_ADDRESS
) {
4757 lsdaAtom
= parser
.findAtomByAddress(p
->u
.fdeInfo
.lsda
.targetAddress
);
4759 // add reference from FDE to CIE (always 32-bit pc-rel)
4760 typename Parser
<A
>::SourceLocation
fdeToCieSrc(fdeAtom
, p
->u
.fdeInfo
.cie
.offsetInCFI
);
4761 parser
.addFixup(fdeToCieSrc
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, fdeAtom
);
4762 parser
.addFixup(fdeToCieSrc
, ld::Fixup::k2of4
, ld::Fixup::kindAddAddend
, p
->u
.fdeInfo
.cie
.offsetInCFI
);
4763 parser
.addFixup(fdeToCieSrc
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractTargetAddress
, cieAtom
);
4764 parser
.addFixup(fdeToCieSrc
, ld::Fixup::k4of4
, store32
, cieAtom
);
4766 // add reference from FDE to function
4767 typename Parser
<A
>::SourceLocation
fdeToFuncSrc(fdeAtom
, p
->u
.fdeInfo
.function
.offsetInCFI
);
4768 switch (p
->u
.fdeInfo
.function
.encodingOfTargetAddress
) {
4769 case DW_EH_PE_pcrel
|DW_EH_PE_ptr
:
4770 if ( sizeof(typename
A::P::uint_t
) == 8 ) {
4771 parser
.addFixup(fdeToFuncSrc
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, functionAtom
);
4772 parser
.addFixup(fdeToFuncSrc
, ld::Fixup::k2of4
, ld::Fixup::kindSubtractTargetAddress
, fdeAtom
);
4773 parser
.addFixup(fdeToFuncSrc
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractAddend
, p
->u
.fdeInfo
.function
.offsetInCFI
);
4774 parser
.addFixup(fdeToFuncSrc
, ld::Fixup::k4of4
, store64
);
4777 // else fall into 32-bit case
4778 case DW_EH_PE_pcrel
|DW_EH_PE_sdata4
:
4779 parser
.addFixup(fdeToFuncSrc
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, functionAtom
);
4780 parser
.addFixup(fdeToFuncSrc
, ld::Fixup::k2of4
, ld::Fixup::kindSubtractTargetAddress
, fdeAtom
);
4781 parser
.addFixup(fdeToFuncSrc
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractAddend
, p
->u
.fdeInfo
.function
.offsetInCFI
);
4782 parser
.addFixup(fdeToFuncSrc
, ld::Fixup::k4of4
, store32
);
4785 throw "unsupported encoding in FDE of pointer to function";
4788 // add reference from FDE to LSDA
4789 typename Parser
<A
>::SourceLocation
fdeToLsdaSrc(fdeAtom
, p
->u
.fdeInfo
.lsda
.offsetInCFI
);
4790 if ( lsdaAtom
!= NULL
) {
4791 switch (p
->u
.fdeInfo
.lsda
.encodingOfTargetAddress
) {
4792 case DW_EH_PE_pcrel
|DW_EH_PE_ptr
:
4793 if ( sizeof(typename
A::P::uint_t
) == 8 ) {
4794 parser
.addFixup(fdeToLsdaSrc
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, lsdaAtom
);
4795 parser
.addFixup(fdeToLsdaSrc
, ld::Fixup::k2of4
, ld::Fixup::kindSubtractTargetAddress
, fdeAtom
);
4796 parser
.addFixup(fdeToLsdaSrc
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractAddend
, p
->u
.fdeInfo
.lsda
.offsetInCFI
);
4797 parser
.addFixup(fdeToLsdaSrc
, ld::Fixup::k4of4
, store64
);
4800 // else fall into 32-bit case
4801 case DW_EH_PE_pcrel
|DW_EH_PE_sdata4
:
4802 parser
.addFixup(fdeToLsdaSrc
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, lsdaAtom
);
4803 parser
.addFixup(fdeToLsdaSrc
, ld::Fixup::k2of4
, ld::Fixup::kindSubtractTargetAddress
, fdeAtom
);
4804 parser
.addFixup(fdeToLsdaSrc
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractAddend
, p
->u
.fdeInfo
.lsda
.offsetInCFI
);
4805 parser
.addFixup(fdeToLsdaSrc
, ld::Fixup::k4of4
, store32
);
4808 throw "unsupported encoding in FDE of pointer to LSDA";
4812 // FDE is in group lead by function atom
4813 typename Parser
<A
>::SourceLocation
fdeSrc(functionAtom
,0);
4814 parser
.addFixup(fdeSrc
, ld::Fixup::k1of1
, ld::Fixup::kindNoneGroupSubordinateFDE
, fdeAtom
);
4816 // LSDA is in group lead by function atom
4817 if ( lsdaAtom
!= NULL
) {
4818 parser
.addFixup(fdeSrc
, ld::Fixup::k1of1
, ld::Fixup::kindNoneGroupSubordinateLSDA
, lsdaAtom
);
4827 template <typename A
>
4828 const void* CFISection
<A
>::OAS::mappedAddress(pint_t addr
)
4830 if ( (_ehFrameStartAddr
<= addr
) && (addr
< _ehFrameEndAddr
) )
4831 return &_ehFrameContent
[addr
-_ehFrameStartAddr
];
4833 // requested bytes are not in __eh_frame section
4834 // this can occur when examining the instruction bytes in the __text
4835 File
<A
>& file
= _ehFrameSection
.file();
4836 for (uint32_t i
=0; i
< file
._sectionsArrayCount
; ++i
) {
4837 const macho_section
<typename
A::P
>* sect
= file
._sectionsArray
[i
]->machoSection();
4838 // TentativeDefinitionSection and AbsoluteSymbolSection have no mach-o section
4839 if ( sect
!= NULL
) {
4840 if ( (sect
->addr() <= addr
) && (addr
< (sect
->addr()+sect
->size())) ) {
4841 return file
.fileContent() + sect
->offset() + addr
- sect
->addr();
4845 throwf("__eh_frame parsing problem. Can't find target of reference to address 0x%08llX", (uint64_t)addr
);
4850 template <typename A
>
4851 uint64_t CFISection
<A
>::OAS::getULEB128(pint_t
& logicalAddr
, pint_t end
)
4853 uintptr_t size
= (end
- logicalAddr
);
4854 libunwind::LocalAddressSpace::pint_t laddr
= (libunwind::LocalAddressSpace::pint_t
)mappedAddress(logicalAddr
);
4855 libunwind::LocalAddressSpace::pint_t sladdr
= laddr
;
4856 uint64_t result
= libunwind::LocalAddressSpace::getULEB128(laddr
, laddr
+size
);
4857 logicalAddr
+= (laddr
-sladdr
);
4861 template <typename A
>
4862 int64_t CFISection
<A
>::OAS::getSLEB128(pint_t
& logicalAddr
, pint_t end
)
4864 uintptr_t size
= (end
- logicalAddr
);
4865 libunwind::LocalAddressSpace::pint_t laddr
= (libunwind::LocalAddressSpace::pint_t
)mappedAddress(logicalAddr
);
4866 libunwind::LocalAddressSpace::pint_t sladdr
= laddr
;
4867 int64_t result
= libunwind::LocalAddressSpace::getSLEB128(laddr
, laddr
+size
);
4868 logicalAddr
+= (laddr
-sladdr
);
4872 template <typename A
>
4873 typename
A::P::uint_t CFISection
<A
>::OAS::getEncodedP(pint_t
& addr
, pint_t end
, uint8_t encoding
)
4875 pint_t startAddr
= addr
;
4880 switch (encoding
& 0x0F) {
4882 result
= getP(addr
);
4883 p
+= sizeof(pint_t
);
4886 case DW_EH_PE_uleb128
:
4887 result
= getULEB128(addr
, end
);
4889 case DW_EH_PE_udata2
:
4890 result
= get16(addr
);
4894 case DW_EH_PE_udata4
:
4895 result
= get32(addr
);
4899 case DW_EH_PE_udata8
:
4900 result
= get64(addr
);
4904 case DW_EH_PE_sleb128
:
4905 result
= getSLEB128(addr
, end
);
4907 case DW_EH_PE_sdata2
:
4908 result
= (int16_t)get16(addr
);
4912 case DW_EH_PE_sdata4
:
4913 result
= (int32_t)get32(addr
);
4917 case DW_EH_PE_sdata8
:
4918 result
= get64(addr
);
4923 throwf("ObjectFileAddressSpace<A>::getEncodedP() encoding 0x%08X not supported", encoding
);
4926 // then add relative offset
4927 switch ( encoding
& 0x70 ) {
4928 case DW_EH_PE_absptr
:
4931 case DW_EH_PE_pcrel
:
4932 result
+= startAddr
;
4934 case DW_EH_PE_textrel
:
4935 throw "DW_EH_PE_textrel pointer encoding not supported";
4937 case DW_EH_PE_datarel
:
4938 throw "DW_EH_PE_datarel pointer encoding not supported";
4940 case DW_EH_PE_funcrel
:
4941 throw "DW_EH_PE_funcrel pointer encoding not supported";
4943 case DW_EH_PE_aligned
:
4944 throw "DW_EH_PE_aligned pointer encoding not supported";
4947 throwf("ObjectFileAddressSpace<A>::getEncodedP() encoding 0x%08X not supported", encoding
);
4951 // Note: DW_EH_PE_indirect is only used in CIEs to refernce the personality pointer
4952 // When parsing .o files that pointer contains zero, so we don't to return that.
4953 // Instead we skip the dereference and return the address of the pointer.
4954 // if ( encoding & DW_EH_PE_indirect )
4955 // result = getP(result);
4961 const char* CUSection
<x86_64
>::personalityName(class Parser
<x86_64
>& parser
, const macho_relocation_info
<x86_64::P
>* reloc
)
4963 if ( reloc
->r_extern() ) {
4964 assert((reloc
->r_type() == X86_64_RELOC_UNSIGNED
) && "wrong reloc type on personality column in __compact_unwind section");
4965 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(reloc
->r_symbolnum());
4966 return parser
.nameFromSymbol(sym
);
4969 const pint_t
* content
= (pint_t
*)(this->file().fileContent() + this->_machOSection
->offset() + reloc
->r_address());
4970 pint_t personalityAddr
= *content
;
4971 assert((parser
.sectionForAddress(personalityAddr
)->type() == ld::Section::typeCode
) && "personality column in __compact_unwind section is not pointer to function");
4972 // atoms may not be constructed yet, so scan symbol table for labels
4973 const char* name
= parser
.scanSymbolTableForAddress(personalityAddr
);
4979 const char* CUSection
<x86
>::personalityName(class Parser
<x86
>& parser
, const macho_relocation_info
<x86::P
>* reloc
)
4981 if ( reloc
->r_extern() ) {
4982 assert((reloc
->r_type() == GENERIC_RELOC_VANILLA
) && "wrong reloc type on personality column in __compact_unwind section");
4983 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(reloc
->r_symbolnum());
4984 return parser
.nameFromSymbol(sym
);
4987 // support __LD, __compact_unwind personality entries which are pointer to personality non-lazy pointer
4988 const pint_t
* content
= (pint_t
*)(this->file().fileContent() + this->_machOSection
->offset() + reloc
->r_address());
4989 pint_t nlPointerAddr
= *content
;
4990 Section
<x86
>* nlSection
= parser
.sectionForAddress(nlPointerAddr
);
4991 if ( nlSection
->type() == ld::Section::typeCode
) {
4992 // personality function is defined in this .o file, so this is a direct reference to it
4993 // atoms may not be constructed yet, so scan symbol table for labels
4994 const char* name
= parser
.scanSymbolTableForAddress(nlPointerAddr
);
4998 uint32_t symIndex
= parser
.symbolIndexFromIndirectSectionAddress(nlPointerAddr
, nlSection
->machoSection());
4999 const macho_nlist
<P
>& nlSymbol
= parser
.symbolFromIndex(symIndex
);
5000 return parser
.nameFromSymbol(nlSymbol
);
5005 #if SUPPORT_ARCH_arm64
5007 const char* CUSection
<arm64
>::personalityName(class Parser
<arm64
>& parser
, const macho_relocation_info
<arm64::P
>* reloc
)
5009 if ( reloc
->r_extern() ) {
5010 assert((reloc
->r_type() == ARM64_RELOC_UNSIGNED
) && "wrong reloc type on personality column in __compact_unwind section");
5011 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(reloc
->r_symbolnum());
5012 return parser
.nameFromSymbol(sym
);
5015 const pint_t
* content
= (pint_t
*)(this->file().fileContent() + this->_machOSection
->offset() + reloc
->r_address());
5016 pint_t personalityAddr
= *content
;
5017 Section
<arm64
>* personalitySection
= parser
.sectionForAddress(personalityAddr
);
5018 (void)personalitySection
;
5019 assert((personalitySection
->type() == ld::Section::typeCode
) && "personality column in __compact_unwind section is not pointer to function");
5020 // atoms may not be constructed yet, so scan symbol table for labels
5021 const char* name
= parser
.scanSymbolTableForAddress(personalityAddr
);
5028 #if SUPPORT_ARCH_arm_any
5030 const char* CUSection
<arm
>::personalityName(class Parser
<arm
>& parser
, const macho_relocation_info
<arm::P
>* reloc
)
5032 if ( reloc
->r_extern() ) {
5033 assert((reloc
->r_type() == ARM_RELOC_VANILLA
) && "wrong reloc type on personality column in __compact_unwind section");
5034 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(reloc
->r_symbolnum());
5035 return parser
.nameFromSymbol(sym
);
5038 // support __LD, __compact_unwind personality entries which are pointer to personality non-lazy pointer
5039 const pint_t
* content
= (pint_t
*)(this->file().fileContent() + this->_machOSection
->offset() + reloc
->r_address());
5040 pint_t nlPointerAddr
= *content
;
5041 Section
<arm
>* nlSection
= parser
.sectionForAddress(nlPointerAddr
);
5042 if ( nlSection
->type() == ld::Section::typeCode
) {
5043 // personality function is defined in this .o file, so this is a direct reference to it
5044 // atoms may not be constructed yet, so scan symbol table for labels
5045 const char* name
= parser
.scanSymbolTableForAddress(nlPointerAddr
);
5049 uint32_t symIndex
= parser
.symbolIndexFromIndirectSectionAddress(nlPointerAddr
, nlSection
->machoSection());
5050 const macho_nlist
<P
>& nlSymbol
= parser
.symbolFromIndex(symIndex
);
5051 return parser
.nameFromSymbol(nlSymbol
);
5058 template <typename A
>
5059 const char* CUSection
<A
>::personalityName(class Parser
<A
>& parser
, const macho_relocation_info
<P
>* reloc
)
5065 bool CUSection
<x86
>::encodingMeansUseDwarf(compact_unwind_encoding_t enc
)
5067 return ((enc
& UNWIND_X86_MODE_MASK
) == UNWIND_X86_MODE_DWARF
);
5071 bool CUSection
<x86_64
>::encodingMeansUseDwarf(compact_unwind_encoding_t enc
)
5073 return ((enc
& UNWIND_X86_64_MODE_MASK
) == UNWIND_X86_64_MODE_DWARF
);
5076 #if SUPPORT_ARCH_arm_any
5078 bool CUSection
<arm
>::encodingMeansUseDwarf(compact_unwind_encoding_t enc
)
5080 return ((enc
& UNWIND_ARM_MODE_MASK
) == UNWIND_ARM_MODE_DWARF
);
5084 #if SUPPORT_ARCH_arm64
5086 bool CUSection
<arm64
>::encodingMeansUseDwarf(compact_unwind_encoding_t enc
)
5088 return ((enc
& UNWIND_ARM64_MODE_MASK
) == UNWIND_ARM64_MODE_DWARF
);
5093 template <typename A
>
5094 int CUSection
<A
>::infoSorter(const void* l
, const void* r
)
5096 // sort references by symbol index, then address
5097 const Info
* left
= (Info
*)l
;
5098 const Info
* right
= (Info
*)r
;
5099 if ( left
->functionSymbolIndex
== right
->functionSymbolIndex
)
5100 return (left
->functionStartAddress
- right
->functionStartAddress
);
5102 return (left
->functionSymbolIndex
- right
->functionSymbolIndex
);
5105 template <typename A
>
5106 void CUSection
<A
>::parse(class Parser
<A
>& parser
, uint32_t cnt
, Info array
[])
5108 // walk section content and copy to Info array
5109 const macho_compact_unwind_entry
<P
>* const entries
= (macho_compact_unwind_entry
<P
>*)(this->file().fileContent() + this->_machOSection
->offset());
5110 for (uint32_t i
=0; i
< cnt
; ++i
) {
5111 Info
* info
= &array
[i
];
5112 const macho_compact_unwind_entry
<P
>* entry
= &entries
[i
];
5113 info
->functionStartAddress
= entry
->codeStart();
5114 info
->functionSymbolIndex
= 0xFFFFFFFF;
5115 info
->rangeLength
= entry
->codeLen();
5116 info
->compactUnwindInfo
= entry
->compactUnwindInfo();
5117 info
->personality
= NULL
;
5118 info
->lsdaAddress
= entry
->lsda();
5119 info
->function
= NULL
;
5121 if ( (info
->compactUnwindInfo
& UNWIND_PERSONALITY_MASK
) != 0 )
5122 warning("no bits should be set in UNWIND_PERSONALITY_MASK of compact unwind encoding in __LD,__compact_unwind section");
5123 if ( info
->lsdaAddress
!= 0 ) {
5124 info
->compactUnwindInfo
|= UNWIND_HAS_LSDA
;
5128 // scan relocs, extern relocs are needed for personality references (possibly for function/lsda refs??)
5129 const uint32_t sectionSize
= this->_machOSection
->size();
5130 const macho_relocation_info
<P
>* relocs
= (macho_relocation_info
<P
>*)(this->file().fileContent() + this->_machOSection
->reloff());
5131 const macho_relocation_info
<P
>* relocsEnd
= &relocs
[this->_machOSection
->nreloc()];
5132 for (const macho_relocation_info
<P
>* reloc
= relocs
; reloc
< relocsEnd
; ++reloc
) {
5133 if ( reloc
->r_address() & R_SCATTERED
)
5135 if ( reloc
->r_address() > sectionSize
)
5136 throwf("malformed __compact_unwind relocation, offset (0x%08X) is beyond end of section,", reloc
->r_address());
5137 if ( reloc
->r_extern() ) {
5138 // only expect external relocs on some colummns
5139 if ( (reloc
->r_address() % sizeof(macho_compact_unwind_entry
<P
>)) == macho_compact_unwind_entry
<P
>::personalityFieldOffset() ) {
5140 uint32_t entryIndex
= reloc
->r_address() / sizeof(macho_compact_unwind_entry
<P
>);
5141 array
[entryIndex
].personality
= this->personalityName(parser
, reloc
);
5143 else if ( (reloc
->r_address() % sizeof(macho_compact_unwind_entry
<P
>)) == macho_compact_unwind_entry
<P
>::lsdaFieldOffset() ) {
5144 uint32_t entryIndex
= reloc
->r_address() / sizeof(macho_compact_unwind_entry
<P
>);
5145 const macho_nlist
<P
>& lsdaSym
= parser
.symbolFromIndex(reloc
->r_symbolnum());
5146 if ( (lsdaSym
.n_type() & N_TYPE
) == N_SECT
)
5147 array
[entryIndex
].lsdaAddress
= lsdaSym
.n_value();
5149 warning("unexpected extern relocation to lsda in __compact_unwind section");
5151 else if ( (reloc
->r_address() % sizeof(macho_compact_unwind_entry
<P
>)) == macho_compact_unwind_entry
<P
>::codeStartFieldOffset() ) {
5152 uint32_t entryIndex
= reloc
->r_address() / sizeof(macho_compact_unwind_entry
<P
>);
5153 array
[entryIndex
].functionSymbolIndex
= reloc
->r_symbolnum();
5154 array
[entryIndex
].functionStartAddress
+= parser
.symbolFromIndex(reloc
->r_symbolnum()).n_value();
5157 warning("unexpected extern relocation in __compact_unwind section");
5161 if ( (reloc
->r_address() % sizeof(macho_compact_unwind_entry
<P
>)) == macho_compact_unwind_entry
<P
>::personalityFieldOffset() ) {
5162 uint32_t entryIndex
= reloc
->r_address() / sizeof(macho_compact_unwind_entry
<P
>);
5163 array
[entryIndex
].personality
= this->personalityName(parser
, reloc
);
5168 // sort array by function start address so unwind infos will be contiguous for a given function
5169 ::qsort(array
, cnt
, sizeof(Info
), infoSorter
);
5172 template <typename A
>
5173 uint32_t CUSection
<A
>::count()
5175 const macho_section
<P
>* machoSect
= this->machoSection();
5176 if ( (machoSect
->size() % sizeof(macho_compact_unwind_entry
<P
>)) != 0 )
5177 throw "malformed __LD,__compact_unwind section, bad length";
5179 return machoSect
->size() / sizeof(macho_compact_unwind_entry
<P
>);
5182 template <typename A
>
5183 void CUSection
<A
>::makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
& cus
)
5185 Info
* const arrayStart
= cus
.cuArray
;
5186 Info
* const arrayEnd
= &cus
.cuArray
[cus
.cuCount
];
5187 for (Info
* info
=arrayStart
; info
< arrayEnd
; ++info
) {
5188 // find function atom from address
5189 info
->function
= parser
.findAtomByAddress(info
->functionStartAddress
);
5190 // find lsda atom from address
5191 if ( info
->lsdaAddress
!= 0 ) {
5192 info
->lsda
= parser
.findAtomByAddress(info
->lsdaAddress
);
5193 // add lsda subordinate
5194 typename Parser
<A
>::SourceLocation
src(info
->function
, info
->functionStartAddress
- info
->function
->objectAddress());
5195 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindNoneGroupSubordinateLSDA
, info
->lsda
);
5197 if ( info
->personality
!= NULL
) {
5198 // add personality subordinate
5199 typename Parser
<A
>::SourceLocation
src(info
->function
, info
->functionStartAddress
- info
->function
->objectAddress());
5200 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindNoneGroupSubordinatePersonality
, false, info
->personality
);
5206 template <typename A
>
5207 SymboledSection
<A
>::SymboledSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
5208 : Section
<A
>(f
, s
), _type(ld::Atom::typeUnclassified
)
5210 switch ( s
->flags() & SECTION_TYPE
) {
5212 _type
= ld::Atom::typeZeroFill
;
5214 case S_MOD_INIT_FUNC_POINTERS
:
5215 _type
= ld::Atom::typeInitializerPointers
;
5217 case S_MOD_TERM_FUNC_POINTERS
:
5218 _type
= ld::Atom::typeTerminatorPointers
;
5220 case S_THREAD_LOCAL_VARIABLES
:
5221 _type
= ld::Atom::typeTLV
;
5223 case S_THREAD_LOCAL_ZEROFILL
:
5224 _type
= ld::Atom::typeTLVZeroFill
;
5226 case S_THREAD_LOCAL_REGULAR
:
5227 _type
= ld::Atom::typeTLVInitialValue
;
5229 case S_THREAD_LOCAL_INIT_FUNCTION_POINTERS
:
5230 _type
= ld::Atom::typeTLVInitializerPointers
;
5233 if ( strncmp(s
->sectname(), "__gcc_except_tab", 16) == 0 )
5234 _type
= ld::Atom::typeLSDA
;
5235 else if ( this->type() == ld::Section::typeInitializerPointers
)
5236 _type
= ld::Atom::typeInitializerPointers
;
5242 template <typename A
>
5243 bool SymboledSection
<A
>::dontDeadStrip()
5246 case ld::Atom::typeInitializerPointers
:
5247 case ld::Atom::typeTerminatorPointers
:
5250 // model an object file without MH_SUBSECTIONS_VIA_SYMBOLS as one in which nothing can be dead stripped
5251 if ( ! this->_file
.canScatterAtoms() )
5254 return Section
<A
>::dontDeadStrip();
5260 template <typename A
>
5261 uint32_t SymboledSection
<A
>::computeAtomCount(class Parser
<A
>& parser
,
5262 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
5263 const struct Parser
<A
>::CFI_CU_InfoArrays
&)
5265 const pint_t startAddr
= this->_machOSection
->addr();
5266 const pint_t endAddr
= startAddr
+ this->_machOSection
->size();
5267 const uint32_t sectNum
= this->sectionNum(parser
);
5272 const macho_nlist
<P
>* sym
;
5273 while ( it
.next(parser
, *this, sectNum
, startAddr
, endAddr
, &addr
, &size
, &sym
) ) {
5276 //fprintf(stderr, "computeAtomCount(%s,%s) => %d\n", this->segmentName(), this->sectionName(), count);
5280 template <typename A
>
5281 uint32_t SymboledSection
<A
>::appendAtoms(class Parser
<A
>& parser
, uint8_t* p
,
5282 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
5283 const struct Parser
<A
>::CFI_CU_InfoArrays
&)
5285 this->_beginAtoms
= (Atom
<A
>*)p
;
5287 //fprintf(stderr, "SymboledSection::appendAtoms() in section %s\n", this->_machOSection->sectname());
5288 const pint_t startAddr
= this->_machOSection
->addr();
5289 const pint_t endAddr
= startAddr
+ this->_machOSection
->size();
5290 const uint32_t sectNum
= this->sectionNum(parser
);
5295 const macho_nlist
<P
>* label
;
5296 while ( it
.next(parser
, *this, sectNum
, startAddr
, endAddr
, &addr
, &size
, &label
) ) {
5297 Atom
<A
>* allocatedSpace
= (Atom
<A
>*)p
;
5298 // is break because of label or CFI?
5299 if ( label
!= NULL
) {
5300 // The size is computed based on the address of the next label (or the end of the section for the last label)
5301 // If there are two labels at the same address, we want them one to be an alias of the other.
5302 // If the label is at the end of a section, it is has zero size, but is not an alias
5303 const bool isAlias
= ( (size
== 0) && (addr
< endAddr
) );
5304 new (allocatedSpace
) Atom
<A
>(*this, parser
, *label
, size
, isAlias
);
5306 this->_hasAliases
= true;
5307 if ( parser
.altEntryFromSymbol(*label
) )
5308 this->_altEntries
.insert(allocatedSpace
);
5311 ld::Atom::SymbolTableInclusion inclusion
= ld::Atom::symbolTableNotIn
;
5312 ld::Atom::ContentType ctype
= this->contentType();
5313 if ( ctype
== ld::Atom::typeLSDA
)
5314 inclusion
= ld::Atom::symbolTableInWithRandomAutoStripLabel
;
5315 new (allocatedSpace
) Atom
<A
>(*this, "anon", addr
, size
, ld::Atom::definitionRegular
, ld::Atom::combineNever
,
5316 ld::Atom::scopeTranslationUnit
, ctype
, inclusion
,
5317 this->dontDeadStrip(), false, false, this->alignmentForAddress(addr
));
5319 p
+= sizeof(Atom
<A
>);
5323 this->_endAtoms
= (Atom
<A
>*)p
;
5329 ld::Atom::SymbolTableInclusion ImplicitSizeSection
<arm64
>::symbolTableInclusion()
5331 return ld::Atom::symbolTableInWithRandomAutoStripLabel
;
5335 template <typename A
>
5336 ld::Atom::SymbolTableInclusion ImplicitSizeSection
<A
>::symbolTableInclusion()
5338 return ld::Atom::symbolTableNotIn
;
5342 template <typename A
>
5343 uint32_t ImplicitSizeSection
<A
>::computeAtomCount(class Parser
<A
>& parser
,
5344 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
5345 const struct Parser
<A
>::CFI_CU_InfoArrays
&)
5348 const macho_section
<P
>* sect
= this->machoSection();
5349 const pint_t startAddr
= sect
->addr();
5350 const pint_t endAddr
= startAddr
+ sect
->size();
5351 for (pint_t addr
= startAddr
; addr
< endAddr
; addr
+= elementSizeAtAddress(addr
) ) {
5352 if ( useElementAt(parser
, it
, addr
) )
5355 if ( it
.fileHasOverlappingSymbols
&& (sect
->size() != 0) && (this->combine(parser
, startAddr
) == ld::Atom::combineByNameAndContent
) ) {
5356 // if there are multiple labels in this section for the same address, then clone them into multi atoms
5357 pint_t prevSymbolAddr
= (pint_t
)(-1);
5358 uint8_t prevSymbolSectNum
= 0;
5359 bool prevIgnore
= false;
5360 for(uint32_t i
=0; i
< it
.sortedSymbolCount
; ++i
) {
5361 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(it
.sortedSymbolIndexes
[i
]);
5362 const pint_t symbolAddr
= sym
.n_value();
5363 const uint8_t symbolSectNum
= sym
.n_sect();
5364 const bool ignore
= this->ignoreLabel(parser
.nameFromSymbol(sym
));
5365 if ( !ignore
&& !prevIgnore
&& (symbolAddr
== prevSymbolAddr
) && (prevSymbolSectNum
== symbolSectNum
) && (symbolSectNum
== this->sectionNum(parser
)) ) {
5368 prevSymbolAddr
= symbolAddr
;
5369 prevSymbolSectNum
= symbolSectNum
;
5370 prevIgnore
= ignore
;
5376 template <typename A
>
5377 uint32_t ImplicitSizeSection
<A
>::appendAtoms(class Parser
<A
>& parser
, uint8_t* p
,
5378 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
5379 const struct Parser
<A
>::CFI_CU_InfoArrays
&)
5381 this->_beginAtoms
= (Atom
<A
>*)p
;
5383 const macho_section
<P
>* sect
= this->machoSection();
5384 const pint_t startAddr
= sect
->addr();
5385 const pint_t endAddr
= startAddr
+ sect
->size();
5386 const uint32_t sectNum
= this->sectionNum(parser
);
5387 //fprintf(stderr, "ImplicitSizeSection::appendAtoms() in section %s\n", sect->sectname());
5391 const macho_nlist
<P
>* foundLabel
;
5392 Atom
<A
>* allocatedSpace
;
5393 while ( it
.next(parser
, *this, sectNum
, startAddr
, endAddr
, &foundAddr
, &size
, &foundLabel
) ) {
5394 if ( foundLabel
!= NULL
) {
5396 pint_t labeledAtomSize
= this->elementSizeAtAddress(foundAddr
);
5397 allocatedSpace
= (Atom
<A
>*)p
;
5398 if ( this->ignoreLabel(parser
.nameFromSymbol(*foundLabel
)) ) {
5400 // <rdar://problem/10018737>
5401 // a size of zero means there is another label at same location
5402 // and we are supposed to ignore this label
5406 //fprintf(stderr, " 0x%08llX make annon, size=%lld\n", (uint64_t)foundAddr, (uint64_t)size);
5407 new (allocatedSpace
) Atom
<A
>(*this, this->unlabeledAtomName(parser
, foundAddr
), foundAddr
,
5408 this->elementSizeAtAddress(foundAddr
), this->definition(),
5409 this->combine(parser
, foundAddr
), this->scopeAtAddress(parser
, foundAddr
),
5410 this->contentType(), this->symbolTableInclusion(),
5411 this->dontDeadStrip(), false, false, this->alignmentForAddress(foundAddr
));
5415 // make named atom for label
5416 //fprintf(stderr, " 0x%08llX make labeled\n", (uint64_t)foundAddr);
5417 new (allocatedSpace
) Atom
<A
>(*this, parser
, *foundLabel
, labeledAtomSize
);
5421 p
+= sizeof(Atom
<A
>);
5422 foundAddr
+= labeledAtomSize
;
5423 size
-= labeledAtomSize
;
5426 // some number of anonymous atoms
5427 for (pint_t addr
= foundAddr
; addr
< (foundAddr
+size
); addr
+= elementSizeAtAddress(addr
) ) {
5428 // make anon atoms for area before label
5429 if ( this->useElementAt(parser
, it
, addr
) ) {
5430 //fprintf(stderr, " 0x%08llX make annon, size=%lld\n", (uint64_t)addr, (uint64_t)elementSizeAtAddress(addr));
5431 allocatedSpace
= (Atom
<A
>*)p
;
5432 new (allocatedSpace
) Atom
<A
>(*this, this->unlabeledAtomName(parser
, addr
), addr
, this->elementSizeAtAddress(addr
),
5433 this->definition(), this->combine(parser
, addr
), this->scopeAtAddress(parser
, addr
),
5434 this->contentType(), this->symbolTableInclusion(),
5435 this->dontDeadStrip(), false, false, this->alignmentForAddress(addr
));
5437 p
+= sizeof(Atom
<A
>);
5442 this->_endAtoms
= (Atom
<A
>*)p
;
5447 template <typename A
>
5448 bool Literal4Section
<A
>::ignoreLabel(const char* label
) const
5450 return (label
[0] == 'L') || (label
[0] == 'l');
5453 template <typename A
>
5454 unsigned long Literal4Section
<A
>::contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
5456 const uint32_t* literalContent
= (uint32_t*)atom
->contentPointer();
5457 return *literalContent
;
5460 template <typename A
>
5461 bool Literal4Section
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
5462 const ld::IndirectBindingTable
& ind
) const
5464 assert(this->type() == rhs
.section().type());
5465 const uint32_t* literalContent
= (uint32_t*)atom
->contentPointer();
5467 const Atom
<A
>* rhsAtom
= dynamic_cast<const Atom
<A
>*>(&rhs
);
5468 assert(rhsAtom
!= NULL
);
5469 if ( rhsAtom
!= NULL
) {
5470 const uint32_t* rhsLiteralContent
= (uint32_t*)rhsAtom
->contentPointer();
5471 return (*literalContent
== *rhsLiteralContent
);
5477 template <typename A
>
5478 bool Literal8Section
<A
>::ignoreLabel(const char* label
) const
5480 return (label
[0] == 'L') || (label
[0] == 'l');
5483 template <typename A
>
5484 unsigned long Literal8Section
<A
>::contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
5487 const uint64_t* literalContent
= (uint64_t*)atom
->contentPointer();
5488 return *literalContent
;
5490 unsigned long hash
= 5381;
5491 const uint8_t* byteContent
= atom
->contentPointer();
5492 for (int i
=0; i
< 8; ++i
) {
5493 hash
= hash
* 33 + byteContent
[i
];
5499 template <typename A
>
5500 bool Literal8Section
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
5501 const ld::IndirectBindingTable
& ind
) const
5503 if ( rhs
.section().type() != ld::Section::typeLiteral8
)
5505 assert(this->type() == rhs
.section().type());
5506 const uint64_t* literalContent
= (uint64_t*)atom
->contentPointer();
5508 const Atom
<A
>* rhsAtom
= dynamic_cast<const Atom
<A
>*>(&rhs
);
5509 assert(rhsAtom
!= NULL
);
5510 if ( rhsAtom
!= NULL
) {
5511 const uint64_t* rhsLiteralContent
= (uint64_t*)rhsAtom
->contentPointer();
5512 return (*literalContent
== *rhsLiteralContent
);
5517 template <typename A
>
5518 bool Literal16Section
<A
>::ignoreLabel(const char* label
) const
5520 return (label
[0] == 'L') || (label
[0] == 'l');
5523 template <typename A
>
5524 unsigned long Literal16Section
<A
>::contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
5526 unsigned long hash
= 5381;
5527 const uint8_t* byteContent
= atom
->contentPointer();
5528 for (int i
=0; i
< 16; ++i
) {
5529 hash
= hash
* 33 + byteContent
[i
];
5534 template <typename A
>
5535 bool Literal16Section
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
5536 const ld::IndirectBindingTable
& ind
) const
5538 if ( rhs
.section().type() != ld::Section::typeLiteral16
)
5540 assert(this->type() == rhs
.section().type());
5541 const uint64_t* literalContent
= (uint64_t*)atom
->contentPointer();
5543 const Atom
<A
>* rhsAtom
= dynamic_cast<const Atom
<A
>*>(&rhs
);
5544 assert(rhsAtom
!= NULL
);
5545 if ( rhsAtom
!= NULL
) {
5546 const uint64_t* rhsLiteralContent
= (uint64_t*)rhsAtom
->contentPointer();
5547 return ((literalContent
[0] == rhsLiteralContent
[0]) && (literalContent
[1] == rhsLiteralContent
[1]));
5554 template <typename A
>
5555 typename
A::P::uint_t CStringSection
<A
>::elementSizeAtAddress(pint_t addr
)
5557 const macho_section
<P
>* sect
= this->machoSection();
5558 const char* stringContent
= (char*)(this->file().fileContent() + sect
->offset() + addr
- sect
->addr());
5559 return strlen(stringContent
) + 1;
5562 template <typename A
>
5563 bool CStringSection
<A
>::useElementAt(Parser
<A
>& parser
, struct Parser
<A
>::LabelAndCFIBreakIterator
& it
, pint_t addr
)
5568 template <typename A
>
5569 bool CStringSection
<A
>::ignoreLabel(const char* label
) const
5571 return (label
[0] == 'L') || (label
[0] == 'l');
5575 template <typename A
>
5576 Atom
<A
>* CStringSection
<A
>::findAtomByAddress(pint_t addr
)
5578 Atom
<A
>* result
= this->findContentAtomByAddress(addr
, this->_beginAtoms
, this->_endAtoms
);
5582 template <typename A
>
5583 unsigned long CStringSection
<A
>::contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
5585 unsigned long hash
= 5381;
5586 const char* stringContent
= (char*)atom
->contentPointer();
5587 for (const char* s
= stringContent
; *s
!= '\0'; ++s
) {
5588 hash
= hash
* 33 + *s
;
5594 template <typename A
>
5595 bool CStringSection
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
5596 const ld::IndirectBindingTable
& ind
) const
5598 if ( rhs
.section().type() != ld::Section::typeCString
)
5600 assert(this->type() == rhs
.section().type());
5601 assert(strcmp(this->sectionName(), rhs
.section().sectionName())== 0);
5602 assert(strcmp(this->segmentName(), rhs
.section().segmentName())== 0);
5603 const char* stringContent
= (char*)atom
->contentPointer();
5605 const Atom
<A
>* rhsAtom
= dynamic_cast<const Atom
<A
>*>(&rhs
);
5606 assert(rhsAtom
!= NULL
);
5607 if ( rhsAtom
!= NULL
) {
5608 if ( atom
->_size
!= rhsAtom
->_size
)
5610 const char* rhsStringContent
= (char*)rhsAtom
->contentPointer();
5611 return (strcmp(stringContent
, rhsStringContent
) == 0);
5618 ld::Fixup::Kind NonLazyPointerSection
<x86
>::fixupKind()
5620 return ld::Fixup::kindStoreLittleEndian32
;
5624 ld::Fixup::Kind NonLazyPointerSection
<arm
>::fixupKind()
5626 return ld::Fixup::kindStoreLittleEndian32
;
5630 ld::Fixup::Kind NonLazyPointerSection
<arm64
>::fixupKind()
5632 return ld::Fixup::kindStoreLittleEndian64
;
5637 void NonLazyPointerSection
<x86_64
>::makeFixups(class Parser
<x86_64
>& parser
, const struct Parser
<x86_64
>::CFI_CU_InfoArrays
&)
5639 assert(0 && "x86_64 should not have non-lazy-pointer sections in .o files");
5642 template <typename A
>
5643 void NonLazyPointerSection
<A
>::makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
&)
5645 // add references for each NLP atom based on indirect symbol table
5646 const macho_section
<P
>* sect
= this->machoSection();
5647 const pint_t endAddr
= sect
->addr() + sect
->size();
5648 for( pint_t addr
= sect
->addr(); addr
< endAddr
; addr
+= sizeof(pint_t
)) {
5649 typename Parser
<A
>::SourceLocation src
;
5650 typename Parser
<A
>::TargetDesc target
;
5651 src
.atom
= this->findAtomByAddress(addr
);
5652 src
.offsetInAtom
= 0;
5653 uint32_t symIndex
= parser
.symbolIndexFromIndirectSectionAddress(addr
, sect
);
5656 target
.weakImport
= false;
5658 if ( symIndex
== INDIRECT_SYMBOL_LOCAL
) {
5659 // use direct reference for local symbols
5660 const pint_t
* nlpContent
= (pint_t
*)(this->file().fileContent() + sect
->offset() + addr
- sect
->addr());
5661 pint_t targetAddr
= P::getP(*nlpContent
);
5662 target
.atom
= parser
.findAtomByAddress(targetAddr
);
5663 target
.weakImport
= false;
5664 target
.addend
= (targetAddr
- target
.atom
->objectAddress());
5665 // <rdar://problem/8385011> if pointer to thumb function, mask of thumb bit (not an addend of +1)
5666 if ( target
.atom
->isThumb() )
5667 target
.addend
&= (-2);
5668 assert(src
.atom
->combine() == ld::Atom::combineNever
);
5671 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(symIndex
);
5672 // use direct reference for local symbols
5673 if ( ((sym
.n_type() & N_TYPE
) == N_SECT
) && ((sym
.n_type() & N_EXT
) == 0) ) {
5674 parser
.findTargetFromAddressAndSectionNum(sym
.n_value(), sym
.n_sect(), target
);
5675 assert(src
.atom
->combine() == ld::Atom::combineNever
);
5678 target
.name
= parser
.nameFromSymbol(sym
);
5679 target
.weakImport
= parser
.weakImportFromSymbol(sym
);
5680 assert(src
.atom
->combine() == ld::Atom::combineByNameAndReferences
);
5683 parser
.addFixups(src
, this->fixupKind(), target
);
5687 template <typename A
>
5688 ld::Atom::Combine NonLazyPointerSection
<A
>::combine(Parser
<A
>& parser
, pint_t addr
)
5690 const macho_section
<P
>* sect
= this->machoSection();
5691 uint32_t symIndex
= parser
.symbolIndexFromIndirectSectionAddress(addr
, sect
);
5692 if ( symIndex
== INDIRECT_SYMBOL_LOCAL
)
5693 return ld::Atom::combineNever
;
5695 // don't coalesce non-lazy-pointers to local symbols
5696 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(symIndex
);
5697 if ( ((sym
.n_type() & N_TYPE
) == N_SECT
) && ((sym
.n_type() & N_EXT
) == 0) )
5698 return ld::Atom::combineNever
;
5700 return ld::Atom::combineByNameAndReferences
;
5703 template <typename A
>
5704 const char* NonLazyPointerSection
<A
>::targetName(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
)
5706 assert(atom
->combine() == ld::Atom::combineByNameAndReferences
);
5707 assert(atom
->fixupCount() == 1);
5708 ld::Fixup::iterator fit
= atom
->fixupsBegin();
5709 const char* name
= NULL
;
5710 switch ( fit
->binding
) {
5711 case ld::Fixup::bindingByNameUnbound
:
5714 case ld::Fixup::bindingByContentBound
:
5715 name
= fit
->u
.target
->name();
5717 case ld::Fixup::bindingsIndirectlyBound
:
5718 name
= ind
.indirectName(fit
->u
.bindingIndex
);
5723 assert(name
!= NULL
);
5727 template <typename A
>
5728 unsigned long NonLazyPointerSection
<A
>::contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
5730 assert(atom
->combine() == ld::Atom::combineByNameAndReferences
);
5731 unsigned long hash
= 9508;
5732 for (const char* s
= this->targetName(atom
, ind
); *s
!= '\0'; ++s
) {
5733 hash
= hash
* 33 + *s
;
5738 template <typename A
>
5739 bool NonLazyPointerSection
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
5740 const ld::IndirectBindingTable
& indirectBindingTable
) const
5742 if ( rhs
.section().type() != ld::Section::typeNonLazyPointer
)
5744 assert(this->type() == rhs
.section().type());
5745 // there can be many non-lazy pointer in different section names
5746 // we only want to coalesce in same section name
5747 if ( *this != rhs
.section() )
5749 const Atom
<A
>* rhsAtom
= dynamic_cast<const Atom
<A
>*>(&rhs
);
5750 assert(rhsAtom
!= NULL
);
5751 const char* thisName
= this->targetName(atom
, indirectBindingTable
);
5752 const char* rhsName
= this->targetName(rhsAtom
, indirectBindingTable
);
5753 return (strcmp(thisName
, rhsName
) == 0);
5756 template <typename A
>
5757 ld::Atom::Scope NonLazyPointerSection
<A
>::scopeAtAddress(Parser
<A
>& parser
, pint_t addr
)
5759 const macho_section
<P
>* sect
= this->machoSection();
5760 uint32_t symIndex
= parser
.symbolIndexFromIndirectSectionAddress(addr
, sect
);
5761 if ( symIndex
== INDIRECT_SYMBOL_LOCAL
)
5762 return ld::Atom::scopeTranslationUnit
;
5764 return ld::Atom::scopeLinkageUnit
;
5769 template <typename A
>
5770 ld::Atom::Combine TLVPointerSection
<A
>::combine(Parser
<A
>& parser
, pint_t addr
)
5772 return ld::Atom::combineByNameAndReferences
;
5776 void TLVPointerSection
<arm
>::makeFixups(class Parser
<arm
>& parser
, const struct Parser
<arm
>::CFI_CU_InfoArrays
&)
5778 // add references for each thread local pointer atom based on indirect symbol table
5779 const macho_section
<P
>* sect
= this->machoSection();
5780 const pint_t endAddr
= sect
->addr() + sect
->size();
5781 for (pint_t addr
= sect
->addr(); addr
< endAddr
; addr
+= sizeof(pint_t
)) {
5782 typename Parser
<arm
>::SourceLocation src
;
5783 typename Parser
<arm
>::TargetDesc target
;
5784 src
.atom
= this->findAtomByAddress(addr
);
5785 src
.offsetInAtom
= 0;
5786 uint32_t symIndex
= parser
.symbolIndexFromIndirectSectionAddress(addr
, sect
);
5789 target
.weakImport
= false;
5791 if ( symIndex
== INDIRECT_SYMBOL_LOCAL
) {
5792 throwf("unexpected INDIRECT_SYMBOL_LOCAL in section %s", this->sectionName());
5795 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(symIndex
);
5796 // use direct reference for local symbols
5797 if ( ((sym
.n_type() & N_TYPE
) == N_SECT
) && ((sym
.n_type() & N_EXT
) == 0) ) {
5798 throwf("unexpected pointer to local symbol in section %s", this->sectionName());
5801 target
.name
= parser
.nameFromSymbol(sym
);
5802 target
.weakImport
= parser
.weakImportFromSymbol(sym
);
5803 assert(src
.atom
->combine() == ld::Atom::combineByNameAndReferences
);
5806 parser
.addFixups(src
, ld::Fixup::kindStoreLittleEndian32
, target
);
5810 template <typename A
>
5811 void TLVPointerSection
<A
>::makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
&)
5813 assert(0 && "should not have thread-local-pointer sections in .o files");
5817 template <typename A
>
5818 const char* TLVPointerSection
<A
>::targetName(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
, bool* isStatic
)
5820 assert(atom
->combine() == ld::Atom::combineByNameAndReferences
);
5821 assert(atom
->fixupCount() == 1);
5823 ld::Fixup::iterator fit
= atom
->fixupsBegin();
5824 const char* name
= NULL
;
5825 switch ( fit
->binding
) {
5826 case ld::Fixup::bindingByNameUnbound
:
5829 case ld::Fixup::bindingByContentBound
:
5830 name
= fit
->u
.target
->name();
5832 case ld::Fixup::bindingsIndirectlyBound
:
5833 name
= ind
.indirectName(fit
->u
.bindingIndex
);
5835 case ld::Fixup::bindingDirectlyBound
:
5836 name
= fit
->u
.target
->name();
5837 *isStatic
= (fit
->u
.target
->scope() == ld::Atom::scopeTranslationUnit
);
5842 assert(name
!= NULL
);
5846 template <typename A
>
5847 unsigned long TLVPointerSection
<A
>::contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
5849 assert(atom
->combine() == ld::Atom::combineByNameAndReferences
);
5850 unsigned long hash
= 9508;
5852 for (const char* s
= this->targetName(atom
, ind
, &isStatic
); *s
!= '\0'; ++s
) {
5853 hash
= hash
* 33 + *s
;
5858 template <typename A
>
5859 bool TLVPointerSection
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
5860 const ld::IndirectBindingTable
& indirectBindingTable
) const
5862 if ( rhs
.section().type() != ld::Section::typeTLVPointers
)
5864 assert(this->type() == rhs
.section().type());
5865 const Atom
<A
>* rhsAtom
= dynamic_cast<const Atom
<A
>*>(&rhs
);
5866 assert(rhsAtom
!= NULL
);
5869 const char* thisName
= this->targetName(atom
, indirectBindingTable
, &thisIsStatic
);
5870 const char* rhsName
= this->targetName(rhsAtom
, indirectBindingTable
, &rhsIsStatic
);
5871 return !thisIsStatic
&& !rhsIsStatic
&& (strcmp(thisName
, rhsName
) == 0);
5875 template <typename A
>
5876 const uint8_t* CFStringSection
<A
>::targetContent(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
,
5877 ContentType
* ct
, unsigned int* count
)
5879 *ct
= contentUnknown
;
5880 for (ld::Fixup::iterator fit
=atom
->fixupsBegin(), end
=atom
->fixupsEnd(); fit
!= end
; ++fit
) {
5881 const ld::Atom
* targetAtom
= NULL
;
5882 switch ( fit
->binding
) {
5883 case ld::Fixup::bindingByNameUnbound
:
5884 // ignore reference to ___CFConstantStringClassReference
5885 // we are just looking for reference to backing string data
5886 assert(fit
->offsetInAtom
== 0);
5887 assert(strcmp(fit
->u
.name
, "___CFConstantStringClassReference") == 0);
5889 case ld::Fixup::bindingDirectlyBound
:
5890 case ld::Fixup::bindingByContentBound
:
5891 targetAtom
= fit
->u
.target
;
5893 case ld::Fixup::bindingsIndirectlyBound
:
5894 targetAtom
= ind
.indirectAtom(fit
->u
.bindingIndex
);
5897 assert(0 && "bad binding type");
5899 assert(targetAtom
!= NULL
);
5900 const Atom
<A
>* target
= dynamic_cast<const Atom
<A
>*>(targetAtom
);
5901 if ( targetAtom
->section().type() == ld::Section::typeCString
) {
5903 *count
= targetAtom
->size();
5905 else if ( targetAtom
->section().type() == ld::Section::typeUTF16Strings
) {
5907 *count
= (targetAtom
->size()+1)/2; // round up incase of buggy compiler that has only one trailing zero byte
5910 *ct
= contentUnknown
;
5914 return target
->contentPointer();
5920 template <typename A
>
5921 unsigned long CFStringSection
<A
>::contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
5923 // base hash of CFString on hash of cstring it wraps
5926 unsigned int charCount
;
5927 const uint8_t* content
= this->targetContent(atom
, ind
, &cType
, &charCount
);
5931 for (const char* s
= (char*)content
; *s
!= '\0'; ++s
) {
5932 hash
= hash
* 33 + *s
;
5937 --charCount
; // don't add last 0x0000 to hash because some buggy compilers only have trailing single byte
5938 for (const uint16_t* s
= (uint16_t*)content
; charCount
> 0; ++s
, --charCount
) {
5939 hash
= hash
* 1025 + *s
;
5942 case contentUnknown
:
5943 // <rdar://problem/14134211> For malformed CFStrings, hash to address of atom so they have unique hashes
5944 return ULONG_MAX
- (unsigned long)(atom
);
5950 template <typename A
>
5951 bool CFStringSection
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
5952 const ld::IndirectBindingTable
& indirectBindingTable
) const
5956 if ( rhs
.section().type() != ld::Section::typeCFString
)
5958 assert(this->type() == rhs
.section().type());
5959 assert(strcmp(this->sectionName(), "__cfstring") == 0);
5961 ContentType thisType
;
5962 unsigned int charCount
;
5963 const uint8_t* cstringContent
= this->targetContent(atom
, indirectBindingTable
, &thisType
, &charCount
);
5964 ContentType rhsType
;
5965 const Atom
<A
>* rhsAtom
= dynamic_cast<const Atom
<A
>*>(&rhs
);
5966 assert(rhsAtom
!= NULL
);
5967 unsigned int rhsCharCount
;
5968 const uint8_t* rhsStringContent
= this->targetContent(rhsAtom
, indirectBindingTable
, &rhsType
, &rhsCharCount
);
5970 if ( thisType
!= rhsType
)
5973 if ( thisType
== contentUnknown
)
5976 if ( rhsType
== contentUnknown
)
5979 // no need to compare content of pointers are already the same
5980 if ( cstringContent
== rhsStringContent
)
5983 // no need to compare content if size is different
5984 if ( charCount
!= rhsCharCount
)
5987 switch ( thisType
) {
5989 return (strcmp((char*)cstringContent
, (char*)rhsStringContent
) == 0);
5992 const uint16_t* cstringContent16
= (uint16_t*)cstringContent
;
5993 const uint16_t* rhsStringContent16
= (uint16_t*)rhsStringContent
;
5994 for (unsigned int i
= 0; i
< charCount
; ++i
) {
5995 if ( cstringContent16
[i
] != rhsStringContent16
[i
] )
6000 case contentUnknown
:
6007 template <typename A
>
6008 typename
A::P::uint_t ObjC1ClassSection
<A
>::elementSizeAtAddress(pint_t addr
)
6010 // nominal size for each class is 48 bytes, but sometimes the compiler
6011 // over aligns and there is padding after class data
6012 const macho_section
<P
>* sct
= this->machoSection();
6013 uint32_t align
= 1 << sct
->align();
6014 uint32_t size
= ((12 * sizeof(pint_t
)) + align
-1) & (-align
);
6018 template <typename A
>
6019 const char* ObjC1ClassSection
<A
>::unlabeledAtomName(Parser
<A
>& parser
, pint_t addr
)
6021 // 8-bytes into class object is pointer to class name
6022 const macho_section
<P
>* sct
= this->machoSection();
6023 uint32_t classObjcFileOffset
= sct
->offset() - sct
->addr() + addr
;
6024 const uint8_t* mappedFileContent
= this->file().fileContent();
6025 pint_t nameAddr
= P::getP(*((pint_t
*)(mappedFileContent
+classObjcFileOffset
+2*sizeof(pint_t
))));
6027 // find section containing string address to get string bytes
6028 const macho_section
<P
>* const sections
= parser
.firstMachOSection();
6029 const uint32_t sectionCount
= parser
.machOSectionCount();
6030 for (uint32_t i
=0; i
< sectionCount
; ++i
) {
6031 const macho_section
<P
>* aSect
= §ions
[i
];
6032 if ( (aSect
->addr() <= nameAddr
) && (nameAddr
< (aSect
->addr()+aSect
->size())) ) {
6033 assert((aSect
->flags() & SECTION_TYPE
) == S_CSTRING_LITERALS
);
6034 uint32_t nameFileOffset
= aSect
->offset() - aSect
->addr() + nameAddr
;
6035 const char* name
= (char*)mappedFileContent
+ nameFileOffset
;
6036 // spin through symbol table to find absolute symbol corresponding to this class
6037 for (uint32_t s
=0; s
< parser
.symbolCount(); ++s
) {
6038 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(s
);
6039 if ( (sym
.n_type() & N_TYPE
) != N_ABS
)
6041 const char* absName
= parser
.nameFromSymbol(sym
);
6042 if ( strncmp(absName
, ".objc_class_name_", 17) == 0 ) {
6043 if ( strcmp(&absName
[17], name
) == 0 )
6047 assert(0 && "obj class name not found in symbol table");
6050 assert(0 && "obj class name not found");
6051 return "unknown objc class";
6055 template <typename A
>
6056 const char* ObjC2ClassRefsSection
<A
>::targetClassName(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
6058 assert(atom
->fixupCount() == 1);
6059 ld::Fixup::iterator fit
= atom
->fixupsBegin();
6060 const char* className
= NULL
;
6061 switch ( fit
->binding
) {
6062 case ld::Fixup::bindingByNameUnbound
:
6063 className
= fit
->u
.name
;
6065 case ld::Fixup::bindingDirectlyBound
:
6066 case ld::Fixup::bindingByContentBound
:
6067 className
= fit
->u
.target
->name();
6069 case ld::Fixup::bindingsIndirectlyBound
:
6070 className
= ind
.indirectName(fit
->u
.bindingIndex
);
6073 assert(0 && "unsupported binding in objc2 class ref section");
6075 assert(className
!= NULL
);
6080 template <typename A
>
6081 unsigned long ObjC2ClassRefsSection
<A
>::contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
6083 unsigned long hash
= 978;
6084 for (const char* s
= targetClassName(atom
, ind
); *s
!= '\0'; ++s
) {
6085 hash
= hash
* 33 + *s
;
6090 template <typename A
>
6091 bool ObjC2ClassRefsSection
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
6092 const ld::IndirectBindingTable
& indirectBindingTable
) const
6094 assert(this->type() == rhs
.section().type());
6095 const Atom
<A
>* rhsAtom
= dynamic_cast<const Atom
<A
>*>(&rhs
);
6096 assert(rhsAtom
!= NULL
);
6097 const char* thisClassName
= targetClassName(atom
, indirectBindingTable
);
6098 const char* rhsClassName
= targetClassName(rhsAtom
, indirectBindingTable
);
6099 return (strcmp(thisClassName
, rhsClassName
) == 0);
6103 template <typename A
>
6104 const char* Objc1ClassReferences
<A
>::targetCString(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
6106 assert(atom
->fixupCount() == 2);
6107 ld::Fixup::iterator fit
= atom
->fixupsBegin();
6108 if ( fit
->kind
== ld::Fixup::kindSetTargetAddress
)
6110 const ld::Atom
* targetAtom
= NULL
;
6111 switch ( fit
->binding
) {
6112 case ld::Fixup::bindingByContentBound
:
6113 targetAtom
= fit
->u
.target
;
6115 case ld::Fixup::bindingsIndirectlyBound
:
6116 targetAtom
= ind
.indirectAtom(fit
->u
.bindingIndex
);
6117 if ( targetAtom
== NULL
) {
6118 fprintf(stderr
, "missing target named %s\n", ind
.indirectName(fit
->u
.bindingIndex
));
6124 assert(targetAtom
!= NULL
);
6125 const Atom
<A
>* target
= dynamic_cast<const Atom
<A
>*>(targetAtom
);
6126 assert(target
!= NULL
);
6127 return (char*)target
->contentPointer();
6131 template <typename A
>
6132 const char* PointerToCStringSection
<A
>::targetCString(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
6134 assert(atom
->fixupCount() == 1);
6135 ld::Fixup::iterator fit
= atom
->fixupsBegin();
6136 const ld::Atom
* targetAtom
= NULL
;
6137 switch ( fit
->binding
) {
6138 case ld::Fixup::bindingByContentBound
:
6139 targetAtom
= fit
->u
.target
;
6141 case ld::Fixup::bindingsIndirectlyBound
:
6142 targetAtom
= ind
.indirectAtom(fit
->u
.bindingIndex
);
6144 case ld::Fixup::bindingDirectlyBound
:
6145 targetAtom
= fit
->u
.target
;
6148 assert(0 && "unsupported reference to selector");
6150 assert(targetAtom
!= NULL
);
6151 const Atom
<A
>* target
= dynamic_cast<const Atom
<A
>*>(targetAtom
);
6152 assert(target
!= NULL
);
6153 assert(target
->contentType() == ld::Atom::typeCString
);
6154 return (char*)target
->contentPointer();
6157 template <typename A
>
6158 unsigned long PointerToCStringSection
<A
>::contentHash(const class Atom
<A
>* atom
,
6159 const ld::IndirectBindingTable
& indirectBindingTable
) const
6161 // make hash from section name and target cstring name
6162 unsigned long hash
= 123;
6163 for (const char* s
= this->sectionName(); *s
!= '\0'; ++s
) {
6164 hash
= hash
* 33 + *s
;
6166 for (const char* s
= this->targetCString(atom
, indirectBindingTable
); *s
!= '\0'; ++s
) {
6167 hash
= hash
* 33 + *s
;
6172 template <typename A
>
6173 bool PointerToCStringSection
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
6174 const ld::IndirectBindingTable
& indirectBindingTable
) const
6176 assert(this->type() == rhs
.section().type());
6177 // there can be pointers-to-cstrings in different section names
6178 // we only want to coalesce in same section name
6179 if ( *this != rhs
.section() )
6182 // get string content for this
6183 const char* cstringContent
= this->targetCString(atom
, indirectBindingTable
);
6184 const Atom
<A
>* rhsAtom
= dynamic_cast<const Atom
<A
>*>(&rhs
);
6185 assert(rhsAtom
!= NULL
);
6186 const char* rhsCstringContent
= this->targetCString(rhsAtom
, indirectBindingTable
);
6188 assert(cstringContent
!= NULL
);
6189 assert(rhsCstringContent
!= NULL
);
6190 return (strcmp(cstringContent
, rhsCstringContent
) == 0);
6195 template <typename A
>
6196 unsigned long UTF16StringSection
<A
>::contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
6198 unsigned long hash
= 5381;
6199 const uint16_t* stringContent
= (uint16_t*)atom
->contentPointer();
6200 // some buggy compilers end utf16 data with single byte, so don't use last word in hash computation
6201 unsigned int count
= (atom
->size()/2) - 1;
6202 for (const uint16_t* s
= stringContent
; count
> 0; ++s
, --count
) {
6203 hash
= hash
* 33 + *s
;
6208 template <typename A
>
6209 bool UTF16StringSection
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
6210 const ld::IndirectBindingTable
& ind
) const
6212 if ( rhs
.section().type() != ld::Section::typeUTF16Strings
)
6225 uint32_t Section
<x86_64
>::x86_64PcRelOffset(uint8_t r_type
)
6228 case X86_64_RELOC_SIGNED
:
6230 case X86_64_RELOC_SIGNED_1
:
6232 case X86_64_RELOC_SIGNED_2
:
6234 case X86_64_RELOC_SIGNED_4
:
6242 bool Section
<x86_64
>::addRelocFixup(class Parser
<x86_64
>& parser
, const macho_relocation_info
<P
>* reloc
)
6244 const macho_section
<P
>* sect
= this->machoSection();
6245 uint64_t srcAddr
= sect
->addr() + reloc
->r_address();
6246 Parser
<x86_64
>::SourceLocation src
;
6247 Parser
<x86_64
>::TargetDesc target
;
6248 Parser
<x86_64
>::TargetDesc toTarget
;
6249 src
.atom
= this->findAtomByAddress(srcAddr
);
6250 src
.offsetInAtom
= srcAddr
- src
.atom
->_objAddress
;
6251 const uint8_t* fixUpPtr
= file().fileContent() + sect
->offset() + reloc
->r_address();
6252 uint64_t contentValue
= 0;
6253 const macho_relocation_info
<x86_64::P
>* nextReloc
= &reloc
[1];
6254 bool result
= false;
6255 bool useDirectBinding
;
6256 switch ( reloc
->r_length() ) {
6258 contentValue
= *fixUpPtr
;
6261 contentValue
= (int64_t)(int16_t)E::get16(*((uint16_t*)fixUpPtr
));
6264 contentValue
= (int64_t)(int32_t)E::get32(*((uint32_t*)fixUpPtr
));
6267 contentValue
= E::get64(*((uint64_t*)fixUpPtr
));
6272 target
.weakImport
= false;
6274 if ( reloc
->r_extern() ) {
6275 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(reloc
->r_symbolnum());
6276 // use direct reference for local symbols
6277 if ( ((sym
.n_type() & N_TYPE
) == N_SECT
) && (((sym
.n_type() & N_EXT
) == 0) || (parser
.nameFromSymbol(sym
)[0] == 'L')) ) {
6278 parser
.findTargetFromAddressAndSectionNum(sym
.n_value(), sym
.n_sect(), target
);
6279 target
.addend
+= contentValue
;
6282 target
.name
= parser
.nameFromSymbol(sym
);
6283 target
.weakImport
= parser
.weakImportFromSymbol(sym
);
6284 target
.addend
= contentValue
;
6286 // cfstrings should always use direct reference to backing store
6287 if ( (this->type() == ld::Section::typeCFString
) && (src
.offsetInAtom
!= 0) ) {
6288 parser
.findTargetFromAddressAndSectionNum(sym
.n_value(), sym
.n_sect(), target
);
6289 target
.addend
= contentValue
;
6293 if ( reloc
->r_pcrel() )
6294 contentValue
+= srcAddr
+ x86_64PcRelOffset(reloc
->r_type());
6295 parser
.findTargetFromAddressAndSectionNum(contentValue
, reloc
->r_symbolnum(), target
);
6297 switch ( reloc
->r_type() ) {
6298 case X86_64_RELOC_UNSIGNED
:
6299 if ( reloc
->r_pcrel() )
6300 throw "pcrel and X86_64_RELOC_UNSIGNED not supported";
6301 switch ( reloc
->r_length() ) {
6304 throw "length < 2 and X86_64_RELOC_UNSIGNED not supported";
6306 parser
.addFixups(src
, ld::Fixup::kindStoreLittleEndian32
, target
);
6309 parser
.addFixups(src
, ld::Fixup::kindStoreLittleEndian64
, target
);
6313 case X86_64_RELOC_SIGNED
:
6314 case X86_64_RELOC_SIGNED_1
:
6315 case X86_64_RELOC_SIGNED_2
:
6316 case X86_64_RELOC_SIGNED_4
:
6317 if ( ! reloc
->r_pcrel() )
6318 throw "not pcrel and X86_64_RELOC_SIGNED* not supported";
6319 if ( reloc
->r_length() != 2 )
6320 throw "length != 2 and X86_64_RELOC_SIGNED* not supported";
6321 switch ( reloc
->r_type() ) {
6322 case X86_64_RELOC_SIGNED
:
6323 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel32
, target
);
6325 case X86_64_RELOC_SIGNED_1
:
6326 if ( reloc
->r_extern() )
6328 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel32_1
, target
);
6330 case X86_64_RELOC_SIGNED_2
:
6331 if ( reloc
->r_extern() )
6333 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel32_2
, target
);
6335 case X86_64_RELOC_SIGNED_4
:
6336 if ( reloc
->r_extern() )
6338 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel32_4
, target
);
6342 case X86_64_RELOC_BRANCH
:
6343 if ( ! reloc
->r_pcrel() )
6344 throw "not pcrel and X86_64_RELOC_BRANCH not supported";
6345 switch ( reloc
->r_length() ) {
6347 if ( (target
.name
!= NULL
) && (strncmp(target
.name
, "___dtrace_probe$", 16) == 0) ) {
6348 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindStoreX86DtraceCallSiteNop
, false, target
.name
);
6349 parser
.addDtraceExtraInfos(src
, &target
.name
[16]);
6351 else if ( (target
.name
!= NULL
) && (strncmp(target
.name
, "___dtrace_isenabled$", 20) == 0) ) {
6352 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindStoreX86DtraceIsEnableSiteClear
, false, target
.name
);
6353 parser
.addDtraceExtraInfos(src
, &target
.name
[20]);
6356 parser
.addFixups(src
, ld::Fixup::kindStoreX86BranchPCRel32
, target
);
6360 parser
.addFixups(src
, ld::Fixup::kindStoreX86BranchPCRel8
, target
);
6363 throwf("length=%d and X86_64_RELOC_BRANCH not supported", reloc
->r_length());
6366 case X86_64_RELOC_GOT
:
6367 if ( ! reloc
->r_extern() )
6368 throw "not extern and X86_64_RELOC_GOT not supported";
6369 if ( ! reloc
->r_pcrel() )
6370 throw "not pcrel and X86_64_RELOC_GOT not supported";
6371 if ( reloc
->r_length() != 2 )
6372 throw "length != 2 and X86_64_RELOC_GOT not supported";
6373 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel32GOT
, target
);
6375 case X86_64_RELOC_GOT_LOAD
:
6376 if ( ! reloc
->r_extern() )
6377 throw "not extern and X86_64_RELOC_GOT_LOAD not supported";
6378 if ( ! reloc
->r_pcrel() )
6379 throw "not pcrel and X86_64_RELOC_GOT_LOAD not supported";
6380 if ( reloc
->r_length() != 2 )
6381 throw "length != 2 and X86_64_RELOC_GOT_LOAD not supported";
6382 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel32GOTLoad
, target
);
6384 case X86_64_RELOC_SUBTRACTOR
:
6385 if ( reloc
->r_pcrel() )
6386 throw "X86_64_RELOC_SUBTRACTOR cannot be pc-relative";
6387 if ( reloc
->r_length() < 2 )
6388 throw "X86_64_RELOC_SUBTRACTOR must have r_length of 2 or 3";
6389 if ( !reloc
->r_extern() )
6390 throw "X86_64_RELOC_SUBTRACTOR must have r_extern=1";
6391 if ( nextReloc
->r_type() != X86_64_RELOC_UNSIGNED
)
6392 throw "X86_64_RELOC_SUBTRACTOR must be followed by X86_64_RELOC_UNSIGNED";
6394 if ( nextReloc
->r_pcrel() )
6395 throw "X86_64_RELOC_UNSIGNED following a X86_64_RELOC_SUBTRACTOR cannot be pc-relative";
6396 if ( nextReloc
->r_length() != reloc
->r_length() )
6397 throw "X86_64_RELOC_UNSIGNED following a X86_64_RELOC_SUBTRACTOR must have same r_length";
6398 if ( nextReloc
->r_extern() ) {
6399 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(nextReloc
->r_symbolnum());
6400 // use direct reference for local symbols
6401 if ( ((sym
.n_type() & N_TYPE
) == N_SECT
) && (((sym
.n_type() & N_EXT
) == 0) || (parser
.nameFromSymbol(sym
)[0] == 'L')) ) {
6402 parser
.findTargetFromAddressAndSectionNum(sym
.n_value(), sym
.n_sect(), toTarget
);
6403 toTarget
.addend
= contentValue
;
6404 useDirectBinding
= true;
6407 toTarget
.name
= parser
.nameFromSymbol(sym
);
6408 toTarget
.weakImport
= parser
.weakImportFromSymbol(sym
);
6409 toTarget
.addend
= contentValue
;
6410 useDirectBinding
= false;
6414 parser
.findTargetFromAddressAndSectionNum(contentValue
, nextReloc
->r_symbolnum(), toTarget
);
6415 useDirectBinding
= (toTarget
.atom
->scope() == ld::Atom::scopeTranslationUnit
) || ((toTarget
.atom
->combine() == ld::Atom::combineByNameAndContent
) || (toTarget
.atom
->combine() == ld::Atom::combineByNameAndReferences
));
6417 if ( useDirectBinding
) {
6418 if ( (toTarget
.atom
->combine() == ld::Atom::combineByNameAndContent
) || (toTarget
.atom
->combine() == ld::Atom::combineByNameAndReferences
) )
6419 parser
.addFixup(src
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, toTarget
.atom
);
6421 parser
.addFixup(src
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, toTarget
.atom
);
6424 parser
.addFixup(src
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, toTarget
.weakImport
, toTarget
.name
);
6425 parser
.addFixup(src
, ld::Fixup::k2of4
, ld::Fixup::kindAddAddend
, toTarget
.addend
);
6426 if ( target
.atom
== NULL
)
6427 parser
.addFixup(src
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractTargetAddress
, false, target
.name
);
6429 parser
.addFixup(src
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractTargetAddress
, target
.atom
);
6430 if ( reloc
->r_length() == 2 )
6431 parser
.addFixup(src
, ld::Fixup::k4of4
, ld::Fixup::kindStoreLittleEndian32
);
6433 parser
.addFixup(src
, ld::Fixup::k4of4
, ld::Fixup::kindStoreLittleEndian64
);
6435 case X86_64_RELOC_TLV
:
6436 if ( ! reloc
->r_extern() )
6437 throw "not extern and X86_64_RELOC_TLV not supported";
6438 if ( ! reloc
->r_pcrel() )
6439 throw "not pcrel and X86_64_RELOC_TLV not supported";
6440 if ( reloc
->r_length() != 2 )
6441 throw "length != 2 and X86_64_RELOC_TLV not supported";
6442 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel32TLVLoad
, target
);
6445 throwf("unknown relocation type %d", reloc
->r_type());
6453 bool Section
<x86
>::addRelocFixup(class Parser
<x86
>& parser
, const macho_relocation_info
<P
>* reloc
)
6455 const macho_section
<P
>* sect
= this->machoSection();
6457 const uint8_t* fixUpPtr
;
6458 uint32_t contentValue
= 0;
6459 ld::Fixup::Kind kind
= ld::Fixup::kindNone
;
6460 Parser
<x86
>::SourceLocation src
;
6461 Parser
<x86
>::TargetDesc target
;
6463 if ( (reloc
->r_address() & R_SCATTERED
) == 0 ) {
6464 srcAddr
= sect
->addr() + reloc
->r_address();
6465 src
.atom
= this->findAtomByAddress(srcAddr
);
6466 src
.offsetInAtom
= srcAddr
- src
.atom
->_objAddress
;
6467 fixUpPtr
= file().fileContent() + sect
->offset() + reloc
->r_address();
6468 switch ( reloc
->r_type() ) {
6469 case GENERIC_RELOC_VANILLA
:
6470 switch ( reloc
->r_length() ) {
6472 contentValue
= (int32_t)(int8_t)*fixUpPtr
;
6473 if ( reloc
->r_pcrel() ) {
6474 kind
= ld::Fixup::kindStoreX86BranchPCRel8
;
6475 contentValue
+= srcAddr
+ sizeof(uint8_t);
6478 throw "r_length=0 and r_pcrel=0 not supported";
6481 contentValue
= (int32_t)(int16_t)E::get16(*((uint16_t*)fixUpPtr
));
6482 if ( reloc
->r_pcrel() ) {
6483 kind
= ld::Fixup::kindStoreX86PCRel16
;
6484 contentValue
+= srcAddr
+ sizeof(uint16_t);
6487 kind
= ld::Fixup::kindStoreLittleEndian16
;
6490 contentValue
= E::get32(*((uint32_t*)fixUpPtr
));
6491 if ( reloc
->r_pcrel() ) {
6492 kind
= ld::Fixup::kindStoreX86BranchPCRel32
;
6493 contentValue
+= srcAddr
+ sizeof(uint32_t);
6496 kind
= ld::Fixup::kindStoreLittleEndian32
;
6499 throw "r_length=3 not supported";
6501 if ( reloc
->r_extern() ) {
6503 const macho_nlist
<P
>& targetSymbol
= parser
.symbolFromIndex(reloc
->r_symbolnum());
6504 target
.name
= parser
.nameFromSymbol(targetSymbol
);
6505 target
.weakImport
= parser
.weakImportFromSymbol(targetSymbol
);
6506 target
.addend
= (int32_t)contentValue
;
6509 parser
.findTargetFromAddressAndSectionNum(contentValue
, reloc
->r_symbolnum(), target
);
6511 if ( (kind
== ld::Fixup::kindStoreX86BranchPCRel32
) && (target
.name
!= NULL
) ) {
6512 if ( strncmp(target
.name
, "___dtrace_probe$", 16) == 0 ) {
6513 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindStoreX86DtraceCallSiteNop
, false, target
.name
);
6514 parser
.addDtraceExtraInfos(src
, &target
.name
[16]);
6517 else if ( strncmp(target
.name
, "___dtrace_isenabled$", 20) == 0 ) {
6518 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindStoreX86DtraceIsEnableSiteClear
, false, target
.name
);
6519 parser
.addDtraceExtraInfos(src
, &target
.name
[20]);
6523 parser
.addFixups(src
, kind
, target
);
6526 case GENERIC_RLEOC_TLV
:
6528 if ( !reloc
->r_extern() )
6529 throw "r_extern=0 and r_type=GENERIC_RLEOC_TLV not supported";
6530 if ( reloc
->r_length() != 2 )
6531 throw "r_length!=2 and r_type=GENERIC_RLEOC_TLV not supported";
6532 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(reloc
->r_symbolnum());
6533 // use direct reference for local symbols
6534 if ( ((sym
.n_type() & N_TYPE
) == N_SECT
) && ((sym
.n_type() & N_EXT
) == 0) ) {
6535 parser
.findTargetFromAddressAndSectionNum(sym
.n_value(), sym
.n_sect(), target
);
6539 target
.name
= parser
.nameFromSymbol(sym
);
6540 target
.weakImport
= parser
.weakImportFromSymbol(sym
);
6542 target
.addend
= (int64_t)(int32_t)E::get32(*((uint32_t*)fixUpPtr
));
6543 if ( reloc
->r_pcrel() ) {
6544 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel32TLVLoad
, target
);
6547 parser
.addFixups(src
, ld::Fixup::kindStoreX86Abs32TLVLoad
, target
);
6553 throwf("unsupported i386 relocation type (%d)", reloc
->r_type());
6557 // scattered relocation
6558 const macho_scattered_relocation_info
<P
>* sreloc
= (macho_scattered_relocation_info
<P
>*)reloc
;
6559 srcAddr
= sect
->addr() + sreloc
->r_address();
6560 src
.atom
= this->findAtomByAddress(srcAddr
);
6561 assert(src
.atom
!= NULL
);
6562 src
.offsetInAtom
= srcAddr
- src
.atom
->_objAddress
;
6563 fixUpPtr
= file().fileContent() + sect
->offset() + sreloc
->r_address();
6564 uint32_t relocValue
= sreloc
->r_value();
6565 bool result
= false;
6566 // file format allows pair to be scattered or not
6567 const macho_scattered_relocation_info
<P
>* nextSReloc
= &sreloc
[1];
6568 const macho_relocation_info
<P
>* nextReloc
= &reloc
[1];
6569 bool nextRelocIsPair
= false;
6570 uint32_t nextRelocAddress
= 0;
6571 uint32_t nextRelocValue
= 0;
6572 if ( (nextReloc
->r_address() & R_SCATTERED
) == 0 ) {
6573 if ( nextReloc
->r_type() == GENERIC_RELOC_PAIR
) {
6574 nextRelocIsPair
= true;
6575 nextRelocAddress
= nextReloc
->r_address();
6576 result
= true; // iterator should skip next reloc, since we've consumed it here
6580 if ( nextSReloc
->r_type() == GENERIC_RELOC_PAIR
) {
6581 nextRelocIsPair
= true;
6582 nextRelocAddress
= nextSReloc
->r_address();
6583 nextRelocValue
= nextSReloc
->r_value();
6586 switch (sreloc
->r_type()) {
6587 case GENERIC_RELOC_VANILLA
:
6588 // with a scattered relocation we get both the target (sreloc->r_value()) and the target+offset (*fixUpPtr)
6589 target
.atom
= parser
.findAtomByAddress(relocValue
);
6590 if ( sreloc
->r_pcrel() ) {
6591 switch ( sreloc
->r_length() ) {
6593 contentValue
= srcAddr
+ 1 + *fixUpPtr
;
6594 target
.addend
= (int32_t)contentValue
- (int32_t)relocValue
;
6595 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel8
, target
);
6598 contentValue
= srcAddr
+ 2 + LittleEndian::get16(*((uint16_t*)fixUpPtr
));
6599 target
.addend
= (int32_t)contentValue
- (int32_t)relocValue
;
6600 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel16
, target
);
6603 contentValue
= srcAddr
+ 4 + LittleEndian::get32(*((uint32_t*)fixUpPtr
));
6604 target
.addend
= (int32_t)contentValue
- (int32_t)relocValue
;
6605 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel32
, target
);
6608 throw "unsupported r_length=3 for scattered pc-rel vanilla reloc";
6613 if ( sreloc
->r_length() != 2 )
6614 throwf("unsupported r_length=%d for scattered vanilla reloc", sreloc
->r_length());
6615 contentValue
= LittleEndian::get32(*((uint32_t*)fixUpPtr
));
6616 target
.addend
= (int32_t)contentValue
- (int32_t)(target
.atom
->objectAddress());
6617 parser
.addFixups(src
, ld::Fixup::kindStoreLittleEndian32
, target
);
6620 case GENERIC_RELOC_SECTDIFF
:
6621 case GENERIC_RELOC_LOCAL_SECTDIFF
:
6623 if ( !nextRelocIsPair
)
6624 throw "GENERIC_RELOC_SECTDIFF missing following pair";
6625 switch ( sreloc
->r_length() ) {
6628 throw "bad length for GENERIC_RELOC_SECTDIFF";
6630 contentValue
= (int32_t)(int16_t)LittleEndian::get16(*((uint16_t*)fixUpPtr
));
6631 kind
= ld::Fixup::kindStoreLittleEndian16
;
6634 contentValue
= LittleEndian::get32(*((uint32_t*)fixUpPtr
));
6635 kind
= ld::Fixup::kindStoreLittleEndian32
;
6638 Atom
<x86
>* fromAtom
= parser
.findAtomByAddress(nextRelocValue
);
6639 uint32_t offsetInFrom
= nextRelocValue
- fromAtom
->_objAddress
;
6640 parser
.findTargetFromAddress(sreloc
->r_value(), target
);
6641 // check for addend encoded in the section content
6642 int64_t addend
= (int32_t)contentValue
- (int32_t)(sreloc
->r_value() - nextRelocValue
);
6644 // switch binding base on coalescing
6645 if ( target
.atom
== NULL
) {
6646 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, false, target
.name
);
6648 else if ( target
.atom
->scope() == ld::Atom::scopeTranslationUnit
) {
6649 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, target
.atom
);
6651 else if ( (target
.atom
->combine() == ld::Atom::combineByNameAndContent
) || (target
.atom
->combine() == ld::Atom::combineByNameAndReferences
) ) {
6652 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, target
.atom
);
6655 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, false, target
.atom
->name());
6657 parser
.addFixup(src
, ld::Fixup::k2of5
, ld::Fixup::kindAddAddend
, target
.addend
);
6658 parser
.addFixup(src
, ld::Fixup::k3of5
, ld::Fixup::kindSubtractTargetAddress
, fromAtom
);
6659 parser
.addFixup(src
, ld::Fixup::k4of5
, ld::Fixup::kindSubtractAddend
, offsetInFrom
-addend
);
6660 parser
.addFixup(src
, ld::Fixup::k5of5
, kind
);
6663 // switch binding base on coalescing
6664 if ( target
.atom
== NULL
) {
6665 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, false, target
.name
);
6667 else if ( target
.atom
->scope() == ld::Atom::scopeTranslationUnit
) {
6668 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, target
.atom
);
6670 else if ( (target
.atom
->combine() == ld::Atom::combineByNameAndContent
) || (target
.atom
->combine() == ld::Atom::combineByNameAndReferences
) ) {
6671 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, target
.atom
);
6674 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, false, target
.atom
->name());
6676 parser
.addFixup(src
, ld::Fixup::k2of5
, ld::Fixup::kindAddAddend
, target
.addend
+addend
);
6677 parser
.addFixup(src
, ld::Fixup::k3of5
, ld::Fixup::kindSubtractTargetAddress
, fromAtom
);
6678 parser
.addFixup(src
, ld::Fixup::k4of5
, ld::Fixup::kindSubtractAddend
, offsetInFrom
);
6679 parser
.addFixup(src
, ld::Fixup::k5of5
, kind
);
6692 #if SUPPORT_ARCH_arm_any
6694 bool Section
<arm
>::addRelocFixup(class Parser
<arm
>& parser
, const macho_relocation_info
<P
>* reloc
)
6696 const macho_section
<P
>* sect
= this->machoSection();
6697 bool result
= false;
6701 int32_t displacement
= 0;
6702 uint32_t instruction
= 0;
6703 pint_t contentValue
= 0;
6704 Parser
<arm
>::SourceLocation src
;
6705 Parser
<arm
>::TargetDesc target
;
6706 const macho_relocation_info
<P
>* nextReloc
;
6708 if ( (reloc
->r_address() & R_SCATTERED
) == 0 ) {
6709 bool externSymbolIsThumbDef
= false;
6710 srcAddr
= sect
->addr() + reloc
->r_address();
6711 src
.atom
= this->findAtomByAddress(srcAddr
);
6712 src
.offsetInAtom
= srcAddr
- src
.atom
->_objAddress
;
6713 fixUpPtr
= (uint32_t*)(file().fileContent() + sect
->offset() + reloc
->r_address());
6714 if ( reloc
->r_type() != ARM_RELOC_PAIR
)
6715 instruction
= LittleEndian::get32(*fixUpPtr
);
6716 if ( reloc
->r_extern() ) {
6717 const macho_nlist
<P
>& targetSymbol
= parser
.symbolFromIndex(reloc
->r_symbolnum());
6718 // use direct reference for local symbols
6719 if ( ((targetSymbol
.n_type() & N_TYPE
) == N_SECT
) && (((targetSymbol
.n_type() & N_EXT
) == 0) || (parser
.nameFromSymbol(targetSymbol
)[0] == 'L')) ) {
6720 parser
.findTargetFromAddressAndSectionNum(targetSymbol
.n_value(), targetSymbol
.n_sect(), target
);
6724 target
.name
= parser
.nameFromSymbol(targetSymbol
);
6725 target
.weakImport
= parser
.weakImportFromSymbol(targetSymbol
);
6726 if ( ((targetSymbol
.n_type() & N_TYPE
) == N_SECT
) && (targetSymbol
.n_desc() & N_ARM_THUMB_DEF
) )
6727 externSymbolIsThumbDef
= true;
6730 switch ( reloc
->r_type() ) {
6731 case ARM_RELOC_BR24
:
6732 // Sign-extend displacement
6733 displacement
= (instruction
& 0x00FFFFFF) << 2;
6734 if ( (displacement
& 0x02000000) != 0 )
6735 displacement
|= 0xFC000000;
6736 // The pc added will be +8 from the pc
6738 // If this is BLX add H << 1
6739 if ((instruction
& 0xFE000000) == 0xFA000000)
6740 displacement
+= ((instruction
& 0x01000000) >> 23);
6741 if ( reloc
->r_extern() ) {
6742 dstAddr
= srcAddr
+ displacement
;
6743 // <rdar://problem/16652542> support large .o files
6744 if ( srcAddr
> 0x2000000 ) {
6745 dstAddr
-= ((srcAddr
+ 0x1FFFFFF) & 0xFC000000);
6747 target
.addend
= dstAddr
;
6748 if ( externSymbolIsThumbDef
)
6749 target
.addend
&= -2; // remove thumb bit
6752 dstAddr
= srcAddr
+ displacement
;
6753 parser
.findTargetFromAddressAndSectionNum(dstAddr
, reloc
->r_symbolnum(), target
);
6755 // special case "calls" for dtrace
6756 if ( (target
.name
!= NULL
) && (strncmp(target
.name
, "___dtrace_probe$", 16) == 0) ) {
6757 parser
.addFixup(src
, ld::Fixup::k1of1
,
6758 ld::Fixup::kindStoreARMDtraceCallSiteNop
, false, target
.name
);
6759 parser
.addDtraceExtraInfos(src
, &target
.name
[16]);
6761 else if ( (target
.name
!= NULL
) && (strncmp(target
.name
, "___dtrace_isenabled$", 20) == 0) ) {
6762 parser
.addFixup(src
, ld::Fixup::k1of1
,
6763 ld::Fixup::kindStoreARMDtraceIsEnableSiteClear
, false, target
.name
);
6764 parser
.addDtraceExtraInfos(src
, &target
.name
[20]);
6767 parser
.addFixups(src
, ld::Fixup::kindStoreARMBranch24
, target
);
6770 case ARM_THUMB_RELOC_BR22
:
6771 // thumb2 added two more bits to displacement, complicating the displacement decoding
6773 uint32_t s
= (instruction
>> 10) & 0x1;
6774 uint32_t j1
= (instruction
>> 29) & 0x1;
6775 uint32_t j2
= (instruction
>> 27) & 0x1;
6776 uint32_t imm10
= instruction
& 0x3FF;
6777 uint32_t imm11
= (instruction
>> 16) & 0x7FF;
6778 uint32_t i1
= (j1
== s
);
6779 uint32_t i2
= (j2
== s
);
6780 uint32_t dis
= (s
<< 24) | (i1
<< 23) | (i2
<< 22) | (imm10
<< 12) | (imm11
<< 1);
6784 displacement
= sdis
;
6786 // The pc added will be +4 from the pc
6788 // If the instruction was blx, force the low 2 bits to be clear
6789 dstAddr
= srcAddr
+ displacement
;
6790 if ((instruction
& 0xD0000000) == 0xC0000000)
6791 dstAddr
&= 0xFFFFFFFC;
6793 if ( reloc
->r_extern() ) {
6794 // <rdar://problem/16652542> support large .o files
6795 if ( srcAddr
> 0x1000000 ) {
6796 dstAddr
-= ((srcAddr
+ 0xFFFFFF) & 0xFE000000);
6798 target
.addend
= (int64_t)(int32_t)dstAddr
;
6801 parser
.findTargetFromAddressAndSectionNum(dstAddr
, reloc
->r_symbolnum(), target
);
6803 // special case "calls" for dtrace
6804 if ( (target
.name
!= NULL
) && (strncmp(target
.name
, "___dtrace_probe$", 16) == 0) ) {
6805 parser
.addFixup(src
, ld::Fixup::k1of1
,
6806 ld::Fixup::kindStoreThumbDtraceCallSiteNop
, false, target
.name
);
6807 parser
.addDtraceExtraInfos(src
, &target
.name
[16]);
6809 else if ( (target
.name
!= NULL
) && (strncmp(target
.name
, "___dtrace_isenabled$", 20) == 0) ) {
6810 parser
.addFixup(src
, ld::Fixup::k1of1
,
6811 ld::Fixup::kindStoreThumbDtraceIsEnableSiteClear
, false, target
.name
);
6812 parser
.addDtraceExtraInfos(src
, &target
.name
[20]);
6815 parser
.addFixups(src
, ld::Fixup::kindStoreThumbBranch22
, target
);
6818 case ARM_RELOC_VANILLA
:
6819 if ( reloc
->r_length() != 2 )
6820 throw "bad length for ARM_RELOC_VANILLA";
6821 contentValue
= LittleEndian::get32(*fixUpPtr
);
6822 if ( reloc
->r_extern() ) {
6823 target
.addend
= (int32_t)contentValue
;
6824 if ( externSymbolIsThumbDef
)
6825 target
.addend
&= -2; // remove thumb bit
6828 parser
.findTargetFromAddressAndSectionNum(contentValue
, reloc
->r_symbolnum(), target
);
6829 // possible non-extern relocation turned into by-name ref because target is a weak-def
6830 if ( target
.atom
!= NULL
) {
6831 if ( target
.atom
->isThumb() )
6832 target
.addend
&= -2; // remove thumb bit
6833 // if reference to LSDA, add group subordinate fixup
6834 if ( target
.atom
->contentType() == ld::Atom::typeLSDA
) {
6835 Parser
<arm
>::SourceLocation src2
;
6836 src2
.atom
= src
.atom
;
6837 src2
.offsetInAtom
= 0;
6838 parser
.addFixup(src2
, ld::Fixup::k1of1
, ld::Fixup::kindNoneGroupSubordinateLSDA
, target
.atom
);
6842 parser
.addFixups(src
, ld::Fixup::kindStoreLittleEndian32
, target
);
6844 case ARM_THUMB_32BIT_BRANCH
:
6845 // silently ignore old unnecessary reloc
6847 case ARM_RELOC_HALF
:
6848 nextReloc
= &reloc
[1];
6849 if ( nextReloc
->r_type() == ARM_RELOC_PAIR
) {
6850 uint32_t instruction16
;
6851 uint32_t other16
= (nextReloc
->r_address() & 0xFFFF);
6853 if ( reloc
->r_length() & 2 ) {
6855 uint32_t i
= ((instruction
& 0x00000400) >> 10);
6856 uint32_t imm4
= (instruction
& 0x0000000F);
6857 uint32_t imm3
= ((instruction
& 0x70000000) >> 28);
6858 uint32_t imm8
= ((instruction
& 0x00FF0000) >> 16);
6859 instruction16
= (imm4
<< 12) | (i
<< 11) | (imm3
<< 8) | imm8
;
6863 uint32_t imm4
= ((instruction
& 0x000F0000) >> 16);
6864 uint32_t imm12
= (instruction
& 0x00000FFF);
6865 instruction16
= (imm4
<< 12) | imm12
;
6867 if ( reloc
->r_length() & 1 ) {
6869 dstAddr
= ((instruction16
<< 16) | other16
);
6870 if ( reloc
->r_extern() ) {
6871 target
.addend
= dstAddr
;
6872 if ( externSymbolIsThumbDef
)
6873 target
.addend
&= -2; // remove thumb bit
6876 parser
.findTargetFromAddress(dstAddr
, target
);
6877 if ( target
.atom
->isThumb() )
6878 target
.addend
&= (-2); // remove thumb bit
6880 parser
.addFixups(src
, (isThumb
? ld::Fixup::kindStoreThumbHigh16
: ld::Fixup::kindStoreARMHigh16
), target
);
6884 dstAddr
= (other16
<< 16) | instruction16
;
6885 if ( reloc
->r_extern() ) {
6886 target
.addend
= dstAddr
;
6887 if ( externSymbolIsThumbDef
)
6888 target
.addend
&= -2; // remove thumb bit
6891 parser
.findTargetFromAddress(dstAddr
, target
);
6892 if ( target
.atom
->isThumb() )
6893 target
.addend
&= (-2); // remove thumb bit
6895 parser
.addFixups(src
, (isThumb
? ld::Fixup::kindStoreThumbLow16
: ld::Fixup::kindStoreARMLow16
), target
);
6900 throw "for ARM_RELOC_HALF, next reloc is not ARM_RELOC_PAIR";
6903 throwf("unknown relocation type %d", reloc
->r_type());
6908 const macho_scattered_relocation_info
<P
>* sreloc
= (macho_scattered_relocation_info
<P
>*)reloc
;
6909 // file format allows pair to be scattered or not
6910 const macho_scattered_relocation_info
<P
>* nextSReloc
= &sreloc
[1];
6911 nextReloc
= &reloc
[1];
6912 srcAddr
= sect
->addr() + sreloc
->r_address();
6913 dstAddr
= sreloc
->r_value();
6914 fixUpPtr
= (uint32_t*)(file().fileContent() + sect
->offset() + sreloc
->r_address());
6915 instruction
= LittleEndian::get32(*fixUpPtr
);
6916 src
.atom
= this->findAtomByAddress(srcAddr
);
6917 src
.offsetInAtom
= srcAddr
- src
.atom
->_objAddress
;
6918 bool nextRelocIsPair
= false;
6919 uint32_t nextRelocAddress
= 0;
6920 uint32_t nextRelocValue
= 0;
6921 if ( (nextReloc
->r_address() & R_SCATTERED
) == 0 ) {
6922 if ( nextReloc
->r_type() == ARM_RELOC_PAIR
) {
6923 nextRelocIsPair
= true;
6924 nextRelocAddress
= nextReloc
->r_address();
6929 if ( nextSReloc
->r_type() == ARM_RELOC_PAIR
) {
6930 nextRelocIsPair
= true;
6931 nextRelocAddress
= nextSReloc
->r_address();
6932 nextRelocValue
= nextSReloc
->r_value();
6936 switch ( sreloc
->r_type() ) {
6937 case ARM_RELOC_VANILLA
:
6938 // with a scattered relocation we get both the target (sreloc->r_value()) and the target+offset (*fixUpPtr)
6939 if ( sreloc
->r_length() != 2 )
6940 throw "bad length for ARM_RELOC_VANILLA";
6941 target
.atom
= parser
.findAtomByAddress(sreloc
->r_value());
6942 if ( target
.atom
== NULL
)
6943 throwf("bad r_value (0x%08X) for ARM_RELOC_VANILLA\n", sreloc
->r_value());
6944 contentValue
= LittleEndian::get32(*fixUpPtr
);
6945 target
.addend
= contentValue
- target
.atom
->_objAddress
;
6946 if ( target
.atom
->isThumb() )
6947 target
.addend
&= -2; // remove thumb bit
6948 parser
.addFixups(src
, ld::Fixup::kindStoreLittleEndian32
, target
);
6950 case ARM_RELOC_BR24
:
6951 // Sign-extend displacement
6952 displacement
= (instruction
& 0x00FFFFFF) << 2;
6953 if ( (displacement
& 0x02000000) != 0 )
6954 displacement
|= 0xFC000000;
6955 // The pc added will be +8 from the pc
6957 // If this is BLX add H << 1
6958 if ((instruction
& 0xFE000000) == 0xFA000000)
6959 displacement
+= ((instruction
& 0x01000000) >> 23);
6960 target
.atom
= parser
.findAtomByAddress(sreloc
->r_value());
6961 target
.addend
= (int64_t)(srcAddr
+ displacement
) - (int64_t)(target
.atom
->_objAddress
);
6962 parser
.addFixups(src
, ld::Fixup::kindStoreARMBranch24
, target
);
6964 case ARM_THUMB_RELOC_BR22
:
6965 // thumb2 added two more bits to displacement, complicating the displacement decoding
6967 uint32_t s
= (instruction
>> 10) & 0x1;
6968 uint32_t j1
= (instruction
>> 29) & 0x1;
6969 uint32_t j2
= (instruction
>> 27) & 0x1;
6970 uint32_t imm10
= instruction
& 0x3FF;
6971 uint32_t imm11
= (instruction
>> 16) & 0x7FF;
6972 uint32_t i1
= (j1
== s
);
6973 uint32_t i2
= (j2
== s
);
6974 uint32_t dis
= (s
<< 24) | (i1
<< 23) | (i2
<< 22) | (imm10
<< 12) | (imm11
<< 1);
6978 displacement
= sdis
;
6980 // The pc added will be +4 from the pc
6982 dstAddr
= srcAddr
+displacement
;
6983 // If the instruction was blx, force the low 2 bits to be clear
6984 if ((instruction
& 0xF8000000) == 0xE8000000)
6985 dstAddr
&= 0xFFFFFFFC;
6986 target
.atom
= parser
.findAtomByAddress(sreloc
->r_value());
6987 target
.addend
= dstAddr
- target
.atom
->_objAddress
;
6988 parser
.addFixups(src
, ld::Fixup::kindStoreThumbBranch22
, target
);
6990 case ARM_RELOC_SECTDIFF
:
6991 case ARM_RELOC_LOCAL_SECTDIFF
:
6993 if ( ! nextRelocIsPair
)
6994 throw "ARM_RELOC_SECTDIFF missing following pair";
6995 if ( sreloc
->r_length() != 2 )
6996 throw "bad length for ARM_RELOC_SECTDIFF";
6997 contentValue
= LittleEndian::get32(*fixUpPtr
);
6998 Atom
<arm
>* fromAtom
= parser
.findAtomByAddress(nextRelocValue
);
6999 uint32_t offsetInFrom
= nextRelocValue
- fromAtom
->_objAddress
;
7000 uint32_t offsetInTarget
;
7001 Atom
<arm
>* targetAtom
= parser
.findAtomByAddressOrLocalTargetOfStub(sreloc
->r_value(), &offsetInTarget
);
7002 // check for addend encoded in the section content
7003 int64_t addend
= (int32_t)contentValue
- (int32_t)(sreloc
->r_value() - nextRelocValue
);
7004 if ( targetAtom
->isThumb() )
7005 addend
&= -2; // remove thumb bit
7006 // if reference to LSDA, add group subordinate fixup
7007 if ( targetAtom
->contentType() == ld::Atom::typeLSDA
) {
7008 Parser
<arm
>::SourceLocation src2
;
7009 src2
.atom
= src
.atom
;
7010 src2
.offsetInAtom
= 0;
7011 parser
.addFixup(src2
, ld::Fixup::k1of1
, ld::Fixup::kindNoneGroupSubordinateLSDA
, targetAtom
);
7014 // switch binding base on coalescing
7015 if ( targetAtom
->scope() == ld::Atom::scopeTranslationUnit
) {
7016 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, targetAtom
);
7018 else if ( (targetAtom
->combine() == ld::Atom::combineByNameAndContent
) || (targetAtom
->combine() == ld::Atom::combineByNameAndReferences
) ) {
7019 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, targetAtom
);
7022 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, false, targetAtom
->name());
7024 parser
.addFixup(src
, ld::Fixup::k2of5
, ld::Fixup::kindAddAddend
, offsetInTarget
);
7025 parser
.addFixup(src
, ld::Fixup::k3of5
, ld::Fixup::kindSubtractTargetAddress
, fromAtom
);
7026 parser
.addFixup(src
, ld::Fixup::k4of5
, ld::Fixup::kindSubtractAddend
, offsetInFrom
-addend
);
7027 parser
.addFixup(src
, ld::Fixup::k5of5
, ld::Fixup::kindStoreLittleEndian32
);
7030 if ( targetAtom
->scope() == ld::Atom::scopeTranslationUnit
) {
7031 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, targetAtom
);
7033 else if ( (targetAtom
->combine() == ld::Atom::combineByNameAndContent
) || (targetAtom
->combine() == ld::Atom::combineByNameAndReferences
) ) {
7034 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, targetAtom
);
7037 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, false, targetAtom
->name());
7039 parser
.addFixup(src
, ld::Fixup::k2of5
, ld::Fixup::kindAddAddend
, (uint32_t)(offsetInTarget
+addend
));
7040 parser
.addFixup(src
, ld::Fixup::k3of5
, ld::Fixup::kindSubtractTargetAddress
, fromAtom
);
7041 parser
.addFixup(src
, ld::Fixup::k4of5
, ld::Fixup::kindSubtractAddend
, offsetInFrom
);
7042 parser
.addFixup(src
, ld::Fixup::k5of5
, ld::Fixup::kindStoreLittleEndian32
);
7046 case ARM_RELOC_HALF_SECTDIFF
:
7047 if ( nextRelocIsPair
) {
7048 instruction
= LittleEndian::get32(*fixUpPtr
);
7049 Atom
<arm
>* fromAtom
= parser
.findAtomByAddress(nextRelocValue
);
7050 uint32_t offsetInFrom
= nextRelocValue
- fromAtom
->_objAddress
;
7051 Atom
<arm
>* targetAtom
= parser
.findAtomByAddress(sreloc
->r_value());
7052 uint32_t offsetInTarget
= sreloc
->r_value() - targetAtom
->_objAddress
;
7053 uint32_t instruction16
;
7054 uint32_t other16
= (nextRelocAddress
& 0xFFFF);
7056 if ( sreloc
->r_length() & 2 ) {
7058 uint32_t i
= ((instruction
& 0x00000400) >> 10);
7059 uint32_t imm4
= (instruction
& 0x0000000F);
7060 uint32_t imm3
= ((instruction
& 0x70000000) >> 28);
7061 uint32_t imm8
= ((instruction
& 0x00FF0000) >> 16);
7062 instruction16
= (imm4
<< 12) | (i
<< 11) | (imm3
<< 8) | imm8
;
7066 uint32_t imm4
= ((instruction
& 0x000F0000) >> 16);
7067 uint32_t imm12
= (instruction
& 0x00000FFF);
7068 instruction16
= (imm4
<< 12) | imm12
;
7070 if ( sreloc
->r_length() & 1 )
7071 dstAddr
= ((instruction16
<< 16) | other16
);
7073 dstAddr
= (other16
<< 16) | instruction16
;
7074 if ( targetAtom
->isThumb() )
7075 dstAddr
&= (-2); // remove thumb bit
7076 int32_t addend
= dstAddr
- (sreloc
->r_value() - nextRelocValue
);
7077 if ( targetAtom
->scope() == ld::Atom::scopeTranslationUnit
) {
7078 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, targetAtom
);
7080 else if ( (targetAtom
->combine() == ld::Atom::combineByNameAndContent
) || (targetAtom
->combine() == ld::Atom::combineByNameAndReferences
) ) {
7081 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, targetAtom
);
7084 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, false, targetAtom
->name());
7086 parser
.addFixup(src
, ld::Fixup::k2of5
, ld::Fixup::kindAddAddend
, (uint32_t)offsetInTarget
+addend
);
7087 parser
.addFixup(src
, ld::Fixup::k3of5
, ld::Fixup::kindSubtractTargetAddress
, fromAtom
);
7088 parser
.addFixup(src
, ld::Fixup::k4of5
, ld::Fixup::kindSubtractAddend
, offsetInFrom
);
7089 if ( sreloc
->r_length() & 1 ) {
7091 parser
.addFixup(src
, ld::Fixup::k5of5
, (isThumb
? ld::Fixup::kindStoreThumbHigh16
: ld::Fixup::kindStoreARMHigh16
));
7095 parser
.addFixup(src
, ld::Fixup::k5of5
, (isThumb
? ld::Fixup::kindStoreThumbLow16
: ld::Fixup::kindStoreARMLow16
));
7100 throw "ARM_RELOC_HALF_SECTDIFF reloc missing following pair";
7102 case ARM_RELOC_HALF
:
7103 if ( nextRelocIsPair
) {
7104 instruction
= LittleEndian::get32(*fixUpPtr
);
7105 Atom
<arm
>* targetAtom
= parser
.findAtomByAddress(sreloc
->r_value());
7106 uint32_t instruction16
;
7107 uint32_t other16
= (nextRelocAddress
& 0xFFFF);
7109 if ( sreloc
->r_length() & 2 ) {
7111 uint32_t i
= ((instruction
& 0x00000400) >> 10);
7112 uint32_t imm4
= (instruction
& 0x0000000F);
7113 uint32_t imm3
= ((instruction
& 0x70000000) >> 28);
7114 uint32_t imm8
= ((instruction
& 0x00FF0000) >> 16);
7115 instruction16
= (imm4
<< 12) | (i
<< 11) | (imm3
<< 8) | imm8
;
7119 uint32_t imm4
= ((instruction
& 0x000F0000) >> 16);
7120 uint32_t imm12
= (instruction
& 0x00000FFF);
7121 instruction16
= (imm4
<< 12) | imm12
;
7123 if ( sreloc
->r_length() & 1 )
7124 dstAddr
= ((instruction16
<< 16) | other16
);
7126 dstAddr
= (other16
<< 16) | instruction16
;
7127 if ( targetAtom
->scope() == ld::Atom::scopeTranslationUnit
) {
7128 parser
.addFixup(src
, ld::Fixup::k1of3
, ld::Fixup::kindSetTargetAddress
, targetAtom
);
7130 else if ( (targetAtom
->combine() == ld::Atom::combineByNameAndContent
) || (targetAtom
->combine() == ld::Atom::combineByNameAndReferences
) ) {
7131 parser
.addFixup(src
, ld::Fixup::k1of3
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, targetAtom
);
7134 parser
.addFixup(src
, ld::Fixup::k1of3
, ld::Fixup::kindSetTargetAddress
, false, targetAtom
->name());
7136 parser
.addFixup(src
, ld::Fixup::k2of3
, ld::Fixup::kindAddAddend
, dstAddr
- targetAtom
->_objAddress
);
7137 if ( sreloc
->r_length() & 1 ) {
7139 parser
.addFixup(src
, ld::Fixup::k3of3
, (isThumb
? ld::Fixup::kindStoreThumbHigh16
: ld::Fixup::kindStoreARMHigh16
));
7143 parser
.addFixup(src
, ld::Fixup::k3of3
, (isThumb
? ld::Fixup::kindStoreThumbLow16
: ld::Fixup::kindStoreARMLow16
));
7148 throw "scattered ARM_RELOC_HALF reloc missing following pair";
7151 throwf("unknown ARM scattered relocation type %d", sreloc
->r_type());
7159 #if SUPPORT_ARCH_arm64
7161 bool Section
<arm64
>::addRelocFixup(class Parser
<arm64
>& parser
, const macho_relocation_info
<P
>* reloc
)
7163 bool result
= false;
7164 Parser
<arm64
>::SourceLocation src
;
7165 Parser
<arm64
>::TargetDesc target
= { NULL
, NULL
, false, 0 };
7166 Parser
<arm64
>::TargetDesc toTarget
;
7167 int32_t prefixRelocAddend
= 0;
7168 if ( reloc
->r_type() == ARM64_RELOC_ADDEND
) {
7169 uint32_t rawAddend
= reloc
->r_symbolnum();
7170 prefixRelocAddend
= rawAddend
;
7171 if ( rawAddend
& 0x00800000 )
7172 prefixRelocAddend
|= 0xFF000000; // sign extend 24-bit signed int to 32-bits
7173 uint32_t addendAddress
= reloc
->r_address();
7174 ++reloc
; //advance to next reloc record
7176 if ( reloc
->r_address() != addendAddress
)
7177 throw "ARM64_RELOC_ADDEND r_address does not match next reloc's r_address";
7179 const macho_section
<P
>* sect
= this->machoSection();
7180 uint64_t srcAddr
= sect
->addr() + reloc
->r_address();
7181 src
.atom
= this->findAtomByAddress(srcAddr
);
7182 src
.offsetInAtom
= srcAddr
- src
.atom
->_objAddress
;
7183 const uint8_t* fixUpPtr
= file().fileContent() + sect
->offset() + reloc
->r_address();
7184 uint64_t contentValue
= 0;
7185 const macho_relocation_info
<arm64::P
>* nextReloc
= &reloc
[1];
7186 bool useDirectBinding
;
7187 uint32_t instruction
;
7188 uint32_t encodedAddend
;
7189 switch ( reloc
->r_length() ) {
7191 contentValue
= *fixUpPtr
;
7194 contentValue
= (int64_t)(int16_t)E::get16(*((uint16_t*)fixUpPtr
));
7197 contentValue
= (int64_t)(int32_t)E::get32(*((uint32_t*)fixUpPtr
));
7200 contentValue
= E::get64(*((uint64_t*)fixUpPtr
));
7203 if ( reloc
->r_extern() ) {
7204 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(reloc
->r_symbolnum());
7205 const char* symbolName
= parser
.nameFromSymbol(sym
);
7206 if ( ((sym
.n_type() & N_TYPE
) == N_SECT
) && (((sym
.n_type() & N_EXT
) == 0) || (symbolName
[0] == 'L') || (symbolName
[0] == 'l')) ) {
7207 // use direct reference for local symbols
7208 parser
.findTargetFromAddressAndSectionNum(sym
.n_value(), sym
.n_sect(), target
);
7209 //target.addend += contentValue;
7211 else if ( ((sym
.n_type() & N_TYPE
) == N_SECT
) && (src
.atom
->_objAddress
<= sym
.n_value()) && (sym
.n_value() < (src
.atom
->_objAddress
+src
.atom
->size())) ) {
7212 // <rdar://problem/13700961> spurious warning when weak function has reference to itself
7213 // use direct reference when atom targets itself
7214 target
.atom
= src
.atom
;
7218 target
.name
= symbolName
;
7219 target
.weakImport
= parser
.weakImportFromSymbol(sym
);
7220 //target.addend = contentValue;
7222 // cfstrings should always use direct reference to backing store
7223 if ( (this->type() == ld::Section::typeCFString
) && (src
.offsetInAtom
!= 0) ) {
7224 parser
.findTargetFromAddressAndSectionNum(sym
.n_value(), sym
.n_sect(), target
);
7225 //target.addend = contentValue;
7229 if ( reloc
->r_pcrel() )
7230 contentValue
+= srcAddr
;
7231 parser
.findTargetFromAddressAndSectionNum(contentValue
, reloc
->r_symbolnum(), target
);
7233 switch ( reloc
->r_type() ) {
7234 case ARM64_RELOC_UNSIGNED
:
7235 if ( reloc
->r_pcrel() )
7236 throw "pcrel and ARM64_RELOC_UNSIGNED not supported";
7237 if ( reloc
->r_extern() )
7238 target
.addend
= contentValue
;
7239 switch ( reloc
->r_length() ) {
7242 throw "length < 2 and ARM64_RELOC_UNSIGNED not supported";
7244 parser
.addFixups(src
, ld::Fixup::kindStoreLittleEndian32
, target
);
7247 parser
.addFixups(src
, ld::Fixup::kindStoreLittleEndian64
, target
);
7251 case ARM64_RELOC_BRANCH26
:
7252 if ( ! reloc
->r_pcrel() )
7253 throw "not pcrel and ARM64_RELOC_BRANCH26 not supported";
7254 if ( ! reloc
->r_extern() )
7255 throw "r_extern == 0 and ARM64_RELOC_BRANCH26 not supported";
7256 if ( reloc
->r_length() != 2 )
7257 throw "r_length != 2 and ARM64_RELOC_BRANCH26 not supported";
7258 if ( (target
.name
!= NULL
) && (strncmp(target
.name
, "___dtrace_probe$", 16) == 0) ) {
7259 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindStoreARM64DtraceCallSiteNop
, false, target
.name
);
7260 parser
.addDtraceExtraInfos(src
, &target
.name
[16]);
7262 else if ( (target
.name
!= NULL
) && (strncmp(target
.name
, "___dtrace_isenabled$", 20) == 0) ) {
7263 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindStoreARM64DtraceIsEnableSiteClear
, false, target
.name
);
7264 parser
.addDtraceExtraInfos(src
, &target
.name
[20]);
7267 target
.addend
= prefixRelocAddend
;
7268 instruction
= contentValue
;
7269 encodedAddend
= (instruction
& 0x03FFFFFF) << 2;
7270 if ( encodedAddend
!= 0 ) {
7271 if ( prefixRelocAddend
== 0 ) {
7272 warning("branch26 instruction at 0x%08X has embedded addend. ARM64_RELOC_ADDEND should be used instead", reloc
->r_address());
7273 target
.addend
= encodedAddend
;
7276 throwf("branch26 instruction at 0x%08X has embedded addend and ARM64_RELOC_ADDEND also used", reloc
->r_address());
7279 parser
.addFixups(src
, ld::Fixup::kindStoreARM64Branch26
, target
);
7282 case ARM64_RELOC_PAGE21
:
7283 if ( ! reloc
->r_pcrel() )
7284 throw "not pcrel and ARM64_RELOC_PAGE21 not supported";
7285 if ( ! reloc
->r_extern() )
7286 throw "r_extern == 0 and ARM64_RELOC_PAGE21 not supported";
7287 if ( reloc
->r_length() != 2 )
7288 throw "length != 2 and ARM64_RELOC_PAGE21 not supported";
7289 target
.addend
= prefixRelocAddend
;
7290 instruction
= contentValue
;
7291 encodedAddend
= ((instruction
& 0x60000000) >> 29) | ((instruction
& 0x01FFFFE0) >> 3);
7292 encodedAddend
*= 4096; // internally addend is in bytes, so scale
7293 if ( encodedAddend
!= 0 ) {
7294 if ( prefixRelocAddend
== 0 ) {
7295 warning("adrp instruction at 0x%08X has embedded addend. ARM64_RELOC_ADDEND should be used instead", reloc
->r_address());
7296 target
.addend
= encodedAddend
;
7299 throwf("adrp instruction at 0x%08X has embedded addend and ARM64_RELOC_ADDEND also used", reloc
->r_address());
7302 parser
.addFixups(src
, ld::Fixup::kindStoreARM64Page21
, target
);
7304 case ARM64_RELOC_PAGEOFF12
:
7305 if ( reloc
->r_pcrel() )
7306 throw "pcrel and ARM64_RELOC_PAGEOFF12 not supported";
7307 if ( ! reloc
->r_extern() )
7308 throw "r_extern == 0 and ARM64_RELOC_PAGEOFF12 not supported";
7309 if ( reloc
->r_length() != 2 )
7310 throw "length != 2 and ARM64_RELOC_PAGEOFF12 not supported";
7311 target
.addend
= prefixRelocAddend
;
7312 instruction
= contentValue
;
7313 encodedAddend
= ((instruction
& 0x003FFC00) >> 10);
7314 // internally addend is in bytes. Some instructions have an implicit scale factor
7315 if ( (instruction
& 0x3B000000) == 0x39000000 ) {
7316 switch ( instruction
& 0xC0000000 ) {
7330 if ( encodedAddend
!= 0 ) {
7331 if ( prefixRelocAddend
== 0 ) {
7332 warning("pageoff12 instruction at 0x%08X has embedded addend. ARM64_RELOC_ADDEND should be used instead", reloc
->r_address());
7333 target
.addend
= encodedAddend
;
7336 throwf("pageoff12 instruction at 0x%08X has embedded addend and ARM64_RELOC_ADDEND also used", reloc
->r_address());
7339 parser
.addFixups(src
, ld::Fixup::kindStoreARM64PageOff12
, target
);
7341 case ARM64_RELOC_GOT_LOAD_PAGE21
:
7342 if ( ! reloc
->r_pcrel() )
7343 throw "not pcrel and ARM64_RELOC_GOT_LOAD_PAGE21 not supported";
7344 if ( ! reloc
->r_extern() )
7345 throw "r_extern == 0 and ARM64_RELOC_GOT_LOAD_PAGE21 not supported";
7346 if ( reloc
->r_length() != 2 )
7347 throw "length != 2 and ARM64_RELOC_GOT_LOAD_PAGE21 not supported";
7348 if ( prefixRelocAddend
!= 0 )
7349 throw "ARM64_RELOC_ADDEND followed by ARM64_RELOC_GOT_LOAD_PAGE21 not supported";
7350 instruction
= contentValue
;
7351 target
.addend
= ((instruction
& 0x60000000) >> 29) | ((instruction
& 0x01FFFFE0) >> 3);
7352 if ( target
.addend
!= 0 )
7353 throw "non-zero addend with ARM64_RELOC_GOT_LOAD_PAGE21 is not supported";
7354 parser
.addFixups(src
, ld::Fixup::kindStoreARM64GOTLoadPage21
, target
);
7356 case ARM64_RELOC_GOT_LOAD_PAGEOFF12
:
7357 if ( reloc
->r_pcrel() )
7358 throw "pcrel and ARM64_RELOC_GOT_LOAD_PAGEOFF12 not supported";
7359 if ( ! reloc
->r_extern() )
7360 throw "r_extern == 0 and ARM64_RELOC_GOT_LOAD_PAGEOFF12 not supported";
7361 if ( reloc
->r_length() != 2 )
7362 throw "length != 2 and ARM64_RELOC_GOT_LOAD_PAGEOFF12 not supported";
7363 if ( prefixRelocAddend
!= 0 )
7364 throw "ARM64_RELOC_ADDEND followed by ARM64_RELOC_GOT_LOAD_PAGEOFF12 not supported";
7365 instruction
= contentValue
;
7366 target
.addend
= ((instruction
& 0x003FFC00) >> 10);
7367 parser
.addFixups(src
, ld::Fixup::kindStoreARM64GOTLoadPageOff12
, target
);
7369 case ARM64_RELOC_TLVP_LOAD_PAGE21
:
7370 if ( ! reloc
->r_pcrel() )
7371 throw "not pcrel and ARM64_RELOC_TLVP_LOAD_PAGE21 not supported";
7372 if ( ! reloc
->r_extern() )
7373 throw "r_extern == 0 and ARM64_RELOC_TLVP_LOAD_PAGE21 not supported";
7374 if ( reloc
->r_length() != 2 )
7375 throw "length != 2 and ARM64_RELOC_TLVP_LOAD_PAGE21 not supported";
7376 if ( prefixRelocAddend
!= 0 )
7377 throw "ARM64_RELOC_ADDEND followed by ARM64_RELOC_TLVP_LOAD_PAGE21 not supported";
7378 instruction
= contentValue
;
7379 target
.addend
= ((instruction
& 0x60000000) >> 29) | ((instruction
& 0x01FFFFE0) >> 3);
7380 if ( target
.addend
!= 0 )
7381 throw "non-zero addend with ARM64_RELOC_GOT_LOAD_PAGE21 is not supported";
7382 parser
.addFixups(src
, ld::Fixup::kindStoreARM64TLVPLoadPage21
, target
);
7384 case ARM64_RELOC_TLVP_LOAD_PAGEOFF12
:
7385 if ( reloc
->r_pcrel() )
7386 throw "pcrel and ARM64_RELOC_TLVP_LOAD_PAGEOFF12 not supported";
7387 if ( ! reloc
->r_extern() )
7388 throw "r_extern == 0 and ARM64_RELOC_TLVP_LOAD_PAGEOFF12 not supported";
7389 if ( reloc
->r_length() != 2 )
7390 throw "length != 2 and ARM64_RELOC_TLVP_LOAD_PAGEOFF12 not supported";
7391 if ( prefixRelocAddend
!= 0 )
7392 throw "ARM64_RELOC_ADDEND followed by ARM64_RELOC_TLVP_LOAD_PAGEOFF12 not supported";
7393 instruction
= contentValue
;
7394 target
.addend
= ((instruction
& 0x003FFC00) >> 10);
7395 parser
.addFixups(src
, ld::Fixup::kindStoreARM64TLVPLoadPageOff12
, target
);
7397 case ARM64_RELOC_SUBTRACTOR
:
7398 if ( reloc
->r_pcrel() )
7399 throw "ARM64_RELOC_SUBTRACTOR cannot be pc-relative";
7400 if ( reloc
->r_length() < 2 )
7401 throw "ARM64_RELOC_SUBTRACTOR must have r_length of 2 or 3";
7402 if ( !reloc
->r_extern() )
7403 throw "ARM64_RELOC_SUBTRACTOR must have r_extern=1";
7404 if ( nextReloc
->r_type() != ARM64_RELOC_UNSIGNED
)
7405 throw "ARM64_RELOC_SUBTRACTOR must be followed by ARM64_RELOC_UNSIGNED";
7406 if ( prefixRelocAddend
!= 0 )
7407 throw "ARM64_RELOC_ADDEND followed by ARM64_RELOC_SUBTRACTOR not supported";
7409 if ( nextReloc
->r_pcrel() )
7410 throw "ARM64_RELOC_UNSIGNED following a ARM64_RELOC_SUBTRACTOR cannot be pc-relative";
7411 if ( nextReloc
->r_length() != reloc
->r_length() )
7412 throw "ARM64_RELOC_UNSIGNED following a ARM64_RELOC_SUBTRACTOR must have same r_length";
7413 if ( nextReloc
->r_extern() ) {
7414 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(nextReloc
->r_symbolnum());
7415 // use direct reference for local symbols
7416 if ( ((sym
.n_type() & N_TYPE
) == N_SECT
) && (((sym
.n_type() & N_EXT
) == 0) || (parser
.nameFromSymbol(sym
)[0] == 'L')) ) {
7417 parser
.findTargetFromAddressAndSectionNum(sym
.n_value(), sym
.n_sect(), toTarget
);
7418 toTarget
.addend
= contentValue
;
7419 useDirectBinding
= true;
7422 toTarget
.name
= parser
.nameFromSymbol(sym
);
7423 toTarget
.weakImport
= parser
.weakImportFromSymbol(sym
);
7424 toTarget
.addend
= contentValue
;
7425 useDirectBinding
= false;
7429 parser
.findTargetFromAddressAndSectionNum(contentValue
, nextReloc
->r_symbolnum(), toTarget
);
7430 useDirectBinding
= (toTarget
.atom
->scope() == ld::Atom::scopeTranslationUnit
);
7432 if ( useDirectBinding
)
7433 parser
.addFixup(src
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, toTarget
.atom
);
7435 parser
.addFixup(src
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, toTarget
.weakImport
, toTarget
.name
);
7436 parser
.addFixup(src
, ld::Fixup::k2of4
, ld::Fixup::kindAddAddend
, toTarget
.addend
);
7437 if ( target
.atom
== NULL
)
7438 parser
.addFixup(src
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractTargetAddress
, false, target
.name
);
7440 parser
.addFixup(src
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractTargetAddress
, target
.atom
);
7441 if ( reloc
->r_length() == 2 )
7442 parser
.addFixup(src
, ld::Fixup::k4of4
, ld::Fixup::kindStoreLittleEndian32
);
7444 parser
.addFixup(src
, ld::Fixup::k4of4
, ld::Fixup::kindStoreLittleEndian64
);
7446 case ARM64_RELOC_POINTER_TO_GOT
:
7447 if ( ! reloc
->r_extern() )
7448 throw "r_extern == 0 and ARM64_RELOC_POINTER_TO_GOT not supported";
7449 if ( prefixRelocAddend
!= 0 )
7450 throw "ARM64_RELOC_ADDEND followed by ARM64_RELOC_POINTER_TO_GOT not supported";
7451 if ( reloc
->r_pcrel() ) {
7452 if ( reloc
->r_length() != 2 )
7453 throw "r_length != 2 and r_extern = 1 and ARM64_RELOC_POINTER_TO_GOT not supported";
7454 parser
.addFixups(src
, ld::Fixup::kindStoreARM64PCRelToGOT
, target
);
7457 if ( reloc
->r_length() != 3 )
7458 throw "r_length != 3 and r_extern = 0 and ARM64_RELOC_POINTER_TO_GOT not supported";
7459 parser
.addFixups(src
, ld::Fixup::kindStoreARM64PointerToGOT
, target
);
7463 throwf("unknown relocation type %d", reloc
->r_type());
7470 template <typename A
>
7471 bool ObjC1ClassSection
<A
>::addRelocFixup(class Parser
<A
>& parser
, const macho_relocation_info
<P
>* reloc
)
7474 FixedSizeSection
<A
>::addRelocFixup(parser
, reloc
);
7476 assert(0 && "needs template specialization");
7481 bool ObjC1ClassSection
<x86
>::addRelocFixup(class Parser
<x86
>& parser
, const macho_relocation_info
<x86::P
>* reloc
)
7483 // if this is the reloc for the super class name string, add implicit reference to super class
7484 if ( ((reloc
->r_address() & R_SCATTERED
) == 0) && (reloc
->r_type() == GENERIC_RELOC_VANILLA
) ) {
7485 assert( reloc
->r_length() == 2 );
7486 assert( ! reloc
->r_pcrel() );
7488 const macho_section
<P
>* sect
= this->machoSection();
7489 Parser
<x86
>::SourceLocation src
;
7490 uint32_t srcAddr
= sect
->addr() + reloc
->r_address();
7491 src
.atom
= this->findAtomByAddress(srcAddr
);
7492 src
.offsetInAtom
= srcAddr
- src
.atom
->objectAddress();
7493 if ( src
.offsetInAtom
== 4 ) {
7494 Parser
<x86
>::TargetDesc stringTarget
;
7495 const uint8_t* fixUpPtr
= file().fileContent() + sect
->offset() + reloc
->r_address();
7496 uint32_t contentValue
= LittleEndian::get32(*((uint32_t*)fixUpPtr
));
7497 parser
.findTargetFromAddressAndSectionNum(contentValue
, reloc
->r_symbolnum(), stringTarget
);
7499 assert(stringTarget
.atom
!= NULL
);
7500 assert(stringTarget
.atom
->contentType() == ld::Atom::typeCString
);
7501 const char* superClassBaseName
= (char*)stringTarget
.atom
->rawContentPointer();
7502 char* superClassName
= new char[strlen(superClassBaseName
) + 20];
7503 strcpy(superClassName
, ".objc_class_name_");
7504 strcat(superClassName
, superClassBaseName
);
7506 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindSetTargetAddress
, false, superClassName
);
7510 return FixedSizeSection
<x86
>::addRelocFixup(parser
, reloc
);
7515 template <typename A
>
7516 bool Objc1ClassReferences
<A
>::addRelocFixup(class Parser
<A
>& parser
, const macho_relocation_info
<P
>* reloc
)
7519 PointerToCStringSection
<A
>::addRelocFixup(parser
, reloc
);
7521 assert(0 && "needs template specialization");
7528 bool Objc1ClassReferences
<x86
>::addRelocFixup(class Parser
<x86
>& parser
, const macho_relocation_info
<x86::P
>* reloc
)
7530 // add implict class refs, fixups not usable yet, so look at relocations
7531 assert( (reloc
->r_address() & R_SCATTERED
) == 0 );
7532 assert( reloc
->r_type() == GENERIC_RELOC_VANILLA
);
7533 assert( reloc
->r_length() == 2 );
7534 assert( ! reloc
->r_pcrel() );
7536 const macho_section
<P
>* sect
= this->machoSection();
7537 Parser
<x86
>::SourceLocation src
;
7538 uint32_t srcAddr
= sect
->addr() + reloc
->r_address();
7539 src
.atom
= this->findAtomByAddress(srcAddr
);
7540 src
.offsetInAtom
= srcAddr
- src
.atom
->objectAddress();
7541 Parser
<x86
>::TargetDesc stringTarget
;
7542 const uint8_t* fixUpPtr
= file().fileContent() + sect
->offset() + reloc
->r_address();
7543 uint32_t contentValue
= LittleEndian::get32(*((uint32_t*)fixUpPtr
));
7544 parser
.findTargetFromAddressAndSectionNum(contentValue
, reloc
->r_symbolnum(), stringTarget
);
7546 assert(stringTarget
.atom
!= NULL
);
7547 assert(stringTarget
.atom
->contentType() == ld::Atom::typeCString
);
7548 const char* baseClassName
= (char*)stringTarget
.atom
->rawContentPointer();
7549 char* objcClassName
= new char[strlen(baseClassName
) + 20];
7550 strcpy(objcClassName
, ".objc_class_name_");
7551 strcat(objcClassName
, baseClassName
);
7553 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindSetTargetAddress
, false, objcClassName
);
7556 return PointerToCStringSection
<x86
>::addRelocFixup(parser
, reloc
);
7559 #if SUPPORT_ARCH_arm64
7561 void Section
<arm64
>::addLOH(class Parser
<arm64
>& parser
, int kind
, int count
, const uint64_t addrs
[]) {
7563 case LOH_ARM64_ADRP_ADRP
:
7564 case LOH_ARM64_ADRP_LDR
:
7565 case LOH_ARM64_ADRP_ADD
:
7566 case LOH_ARM64_ADRP_LDR_GOT
:
7568 warning("arm64 Linker Optimiztion Hint %d has wrong number of arguments", kind
);
7570 case LOH_ARM64_ADRP_ADD_LDR
:
7571 case LOH_ARM64_ADRP_LDR_GOT_LDR
:
7572 case LOH_ARM64_ADRP_ADD_STR
:
7573 case LOH_ARM64_ADRP_LDR_GOT_STR
:
7575 warning("arm64 Linker Optimiztion Hint %d has wrong number of arguments", kind
);
7578 // pick lowest address in tuple for use as offsetInAtom
7579 uint64_t lowestAddress
= addrs
[0];
7580 for(int i
=1; i
< count
; ++i
) {
7581 if ( addrs
[i
] < lowestAddress
)
7582 lowestAddress
= addrs
[i
];
7584 // verify all other address are in same atom
7585 Atom
<arm64
>* inAtom
= parser
.findAtomByAddress(lowestAddress
);
7586 const uint64_t atomStartAddr
= inAtom
->objectAddress();
7587 const uint64_t atomEndAddr
= atomStartAddr
+ inAtom
->size();
7588 for(int i
=0; i
< count
; ++i
) {
7589 if ( (addrs
[i
] < atomStartAddr
) || (addrs
[i
] >= atomEndAddr
) ) {
7590 warning("arm64 Linker Optimiztion Hint addresses are not in same atom: 0x%08llX and 0x%08llX",
7591 lowestAddress
, addrs
[i
]);
7592 return; // skip this LOH
7594 if ( (addrs
[i
] & 0x3) != 0 ) {
7595 warning("arm64 Linker Optimiztion Hint address is not 4-byte aligned: 0x%08llX", addrs
[i
]);
7596 return; // skip this LOH
7598 if ( (addrs
[i
] - lowestAddress
) > 0xFFFF ) {
7599 if ( parser
.verboseOptimizationHints() ) {
7600 warning("arm64 Linker Optimiztion Hint addresses are too far apart: 0x%08llX and 0x%08llX",
7601 lowestAddress
, addrs
[i
]);
7603 return; // skip this LOH
7607 // encoded kind, count, and address deltas in 64-bit addend
7608 ld::Fixup::LOH_arm64 extra
;
7610 extra
.info
.kind
= kind
;
7611 extra
.info
.count
= count
-1;
7612 extra
.info
.delta1
= (addrs
[0] - lowestAddress
) >> 2;
7613 extra
.info
.delta2
= (count
> 1) ? ((addrs
[1] - lowestAddress
) >> 2) : 0;
7614 extra
.info
.delta3
= (count
> 2) ? ((addrs
[2] - lowestAddress
) >> 2) : 0;
7615 extra
.info
.delta4
= (count
> 3) ? ((addrs
[3] - lowestAddress
) >> 2) : 0;
7616 typename Parser
<arm64
>::SourceLocation
src(inAtom
, lowestAddress
- inAtom
->objectAddress());
7617 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindLinkerOptimizationHint
, extra
.addend
);
7622 template <typename A
>
7623 void Section
<A
>::addLOH(class Parser
<A
>& parser
, int kind
, int count
, const uint64_t addrs
[]) {
7627 template <typename A
>
7628 void Section
<A
>::makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
&)
7630 const macho_section
<P
>* sect
= this->machoSection();
7631 if ( sect
->reloff() + (sect
->nreloc() * sizeof(macho_relocation_info
<P
>)) > parser
.fileLength() )
7632 throwf("relocations for section %s/%s extends beyond end of file,", sect
->segname(), Section
<A
>::makeSectionName(sect
) );
7633 const macho_relocation_info
<P
>* relocs
= (macho_relocation_info
<P
>*)(file().fileContent() + sect
->reloff());
7634 const uint32_t relocCount
= sect
->nreloc();
7635 for (uint32_t r
= 0; r
< relocCount
; ++r
) {
7637 if ( this->addRelocFixup(parser
, &relocs
[r
]) )
7640 catch (const char* msg
) {
7641 throwf("in section %s,%s reloc %u: %s", sect
->segname(), Section
<A
>::makeSectionName(sect
), r
, msg
);
7645 // add follow-on fixups if .o file is missing .subsections_via_symbols
7646 if ( this->addFollowOnFixups() ) {
7647 Atom
<A
>* end
= &_endAtoms
[-1];
7648 for(Atom
<A
>* p
= _beginAtoms
; p
< end
; ++p
) {
7649 typename Parser
<A
>::SourceLocation
src(p
, 0);
7650 Atom
<A
>* nextAtom
= &p
[1];
7651 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindNoneFollowOn
, nextAtom
);
7654 else if ( this->type() == ld::Section::typeCode
) {
7655 // if FDE broke text not at a symbol, use followOn to keep code together
7656 Atom
<A
>* end
= &_endAtoms
[-1];
7657 for(Atom
<A
>* p
= _beginAtoms
; p
< end
; ++p
) {
7658 typename Parser
<A
>::SourceLocation
src(p
, 0);
7659 Atom
<A
>* nextAtom
= &p
[1];
7660 if ( (p
->symbolTableInclusion() == ld::Atom::symbolTableIn
) && (nextAtom
->symbolTableInclusion() == ld::Atom::symbolTableNotIn
) ) {
7661 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindNoneFollowOn
, nextAtom
);
7665 if ( !this->_altEntries
.empty() && !this->addFollowOnFixups() ) {
7666 if ( _altEntries
.count(_beginAtoms
) != 0 )
7667 warning("N_ALT_ENTRY bit set on first atom in section %s/%s", sect
->segname(), Section
<A
>::makeSectionName(sect
));
7669 Atom
<A
>* end
= &_endAtoms
[-1];
7670 for(Atom
<A
>* p
= _beginAtoms
; p
< end
; ++p
) {
7671 Atom
<A
>* nextAtom
= &p
[1];
7672 // <rdar://problem/22960070> support alt_entry aliases (alias process already added followOn, don't repeat)
7673 if ( (_altEntries
.count(nextAtom
) != 0) && (p
->_objAddress
!= nextAtom
->_objAddress
) ) {
7674 typename Parser
<A
>::SourceLocation
src(p
, 0);
7675 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindNoneFollowOn
, nextAtom
);
7676 typename Parser
<A
>::SourceLocation
src2(nextAtom
, 0);
7677 parser
.addFixup(src2
, ld::Fixup::k1of1
, ld::Fixup::kindNoneGroupSubordinate
, p
);
7682 // <rdar://problem/9218847> track data-in-code
7683 if ( parser
.hasDataInCodeLabels() && (this->type() == ld::Section::typeCode
) ) {
7684 for (uint32_t i
=0; i
< parser
.symbolCount(); ++i
) {
7685 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(i
);
7687 if ( (sym
.n_type() & N_STAB
) != 0 )
7689 // ignore non-definitions
7690 if ( (sym
.n_type() & N_TYPE
) != N_SECT
)
7693 // 'L' labels do not denote atom breaks
7694 const char* symbolName
= parser
.nameFromSymbol(sym
);
7695 if ( symbolName
[0] == 'L' ) {
7696 if ( strncmp(symbolName
, "L$start$", 8) == 0 ) {
7697 ld::Fixup::Kind kind
= ld::Fixup::kindNone
;
7698 if ( strncmp(&symbolName
[8], "data$", 5) == 0 )
7699 kind
= ld::Fixup::kindDataInCodeStartData
;
7700 else if ( strncmp(&symbolName
[8], "code$", 5) == 0 )
7701 kind
= ld::Fixup::kindDataInCodeEnd
;
7702 else if ( strncmp(&symbolName
[8], "jt8$", 4) == 0 )
7703 kind
= ld::Fixup::kindDataInCodeStartJT8
;
7704 else if ( strncmp(&symbolName
[8], "jt16$", 4) == 0 )
7705 kind
= ld::Fixup::kindDataInCodeStartJT16
;
7706 else if ( strncmp(&symbolName
[8], "jt32$", 4) == 0 )
7707 kind
= ld::Fixup::kindDataInCodeStartJT32
;
7708 else if ( strncmp(&symbolName
[8], "jta32$", 4) == 0 )
7709 kind
= ld::Fixup::kindDataInCodeStartJTA32
;
7711 warning("unknown L$start$ label %s in file %s", symbolName
, this->file().path());
7712 if ( kind
!= ld::Fixup::kindNone
) {
7713 Atom
<A
>* inAtom
= parser
.findAtomByAddress(sym
.n_value());
7714 typename Parser
<A
>::SourceLocation
src(inAtom
, sym
.n_value() - inAtom
->objectAddress());
7715 parser
.addFixup(src
, ld::Fixup::k1of1
, kind
);
7722 // <rdar://problem/11150575> Handle LC_DATA_IN_CODE in object files
7723 if ( this->type() == ld::Section::typeCode
) {
7724 const pint_t startAddr
= this->_machOSection
->addr();
7725 const pint_t endAddr
= startAddr
+ this->_machOSection
->size();
7726 for ( const macho_data_in_code_entry
<P
>* p
= parser
.dataInCodeStart(); p
!= parser
.dataInCodeEnd(); ++p
) {
7727 if ( (p
->offset() >= startAddr
) && (p
->offset() < endAddr
) ) {
7728 ld::Fixup::Kind kind
= ld::Fixup::kindNone
;
7729 switch ( p
->kind() ) {
7730 case DICE_KIND_DATA
:
7731 kind
= ld::Fixup::kindDataInCodeStartData
;
7733 case DICE_KIND_JUMP_TABLE8
:
7734 kind
= ld::Fixup::kindDataInCodeStartJT8
;
7736 case DICE_KIND_JUMP_TABLE16
:
7737 kind
= ld::Fixup::kindDataInCodeStartJT16
;
7739 case DICE_KIND_JUMP_TABLE32
:
7740 kind
= ld::Fixup::kindDataInCodeStartJT32
;
7742 case DICE_KIND_ABS_JUMP_TABLE32
:
7743 kind
= ld::Fixup::kindDataInCodeStartJTA32
;
7746 kind
= ld::Fixup::kindDataInCodeStartData
;
7747 warning("uknown LC_DATA_IN_CODE kind (%d) at offset 0x%08X", p
->kind(), p
->offset());
7750 Atom
<A
>* inAtom
= parser
.findAtomByAddress(p
->offset());
7751 typename Parser
<A
>::SourceLocation
srcStart(inAtom
, p
->offset() - inAtom
->objectAddress());
7752 parser
.addFixup(srcStart
, ld::Fixup::k1of1
, kind
);
7753 typename Parser
<A
>::SourceLocation
srcEnd(inAtom
, p
->offset() + p
->length() - inAtom
->objectAddress());
7754 parser
.addFixup(srcEnd
, ld::Fixup::k1of1
, ld::Fixup::kindDataInCodeEnd
);
7759 // <rdar://problem/11945700> convert linker optimization hints into internal format
7760 if ( this->type() == ld::Section::typeCode
&& parser
.hasOptimizationHints() ) {
7761 const pint_t startAddr
= this->_machOSection
->addr();
7762 const pint_t endAddr
= startAddr
+ this->_machOSection
->size();
7763 for (const uint8_t* p
= parser
.optimizationHintsStart(); p
< parser
.optimizationHintsEnd(); ) {
7765 int32_t kind
= read_uleb128(&p
, parser
.optimizationHintsEnd());
7766 if ( kind
== 0 ) // padding at end of loh buffer
7769 warning("malformed uleb128 kind in LC_LINKER_OPTIMIZATION_HINTS");
7772 int32_t count
= read_uleb128(&p
, parser
.optimizationHintsEnd());
7773 if ( count
== -1 ) {
7774 warning("malformed uleb128 count in LC_LINKER_OPTIMIZATION_HINTS");
7778 warning("address count > 3 in LC_LINKER_OPTIMIZATION_HINTS");
7781 for (int32_t i
=0; i
< count
; ++i
) {
7782 addrs
[i
] = read_uleb128(&p
, parser
.optimizationHintsEnd());
7784 if ( (startAddr
<= addrs
[0]) && (addrs
[0] < endAddr
) ) {
7785 this->addLOH(parser
, kind
, count
, addrs
);
7786 //fprintf(stderr, "kind=%d", kind);
7787 //for (int32_t i=0; i < count; ++i) {
7788 // fprintf(stderr, ", addr=0x%08llX", addrs[i]);
7790 //fprintf(stderr, "\n");
7796 // add follow-on fixups for aliases
7797 if ( _hasAliases
) {
7798 for(Atom
<A
>* p
= _beginAtoms
; p
< _endAtoms
; ++p
) {
7799 if ( p
->isAlias() && ! this->addFollowOnFixups() ) {
7800 Atom
<A
>* targetOfAlias
= &p
[1];
7801 assert(p
< &_endAtoms
[-1]);
7802 assert(p
->_objAddress
== targetOfAlias
->_objAddress
);
7803 typename Parser
<A
>::SourceLocation
src(p
, 0);
7804 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindNoneFollowOn
, targetOfAlias
);
7813 // main function used by linker to instantiate ld::Files
7815 ld::relocatable::File
* parse(const uint8_t* fileContent
, uint64_t fileLength
,
7816 const char* path
, time_t modTime
, ld::File::Ordinal ordinal
, const ParserOptions
& opts
)
7818 switch ( opts
.architecture
) {
7819 #if SUPPORT_ARCH_x86_64
7820 case CPU_TYPE_X86_64
:
7821 if ( mach_o::relocatable::Parser
<x86_64
>::validFile(fileContent
) )
7822 return mach_o::relocatable::Parser
<x86_64
>::parse(fileContent
, fileLength
, path
, modTime
, ordinal
, opts
);
7825 #if SUPPORT_ARCH_i386
7827 if ( mach_o::relocatable::Parser
<x86
>::validFile(fileContent
) )
7828 return mach_o::relocatable::Parser
<x86
>::parse(fileContent
, fileLength
, path
, modTime
, ordinal
, opts
);
7831 #if SUPPORT_ARCH_arm_any
7833 if ( mach_o::relocatable::Parser
<arm
>::validFile(fileContent
, opts
.objSubtypeMustMatch
, opts
.subType
) )
7834 return mach_o::relocatable::Parser
<arm
>::parse(fileContent
, fileLength
, path
, modTime
, ordinal
, opts
);
7837 #if SUPPORT_ARCH_arm64
7838 case CPU_TYPE_ARM64
:
7839 if ( mach_o::relocatable::Parser
<arm64
>::validFile(fileContent
, opts
.objSubtypeMustMatch
, opts
.subType
) )
7840 return mach_o::relocatable::Parser
<arm64
>::parse(fileContent
, fileLength
, path
, modTime
, ordinal
, opts
);
7848 // used by archive reader to validate member object file
7850 bool isObjectFile(const uint8_t* fileContent
, uint64_t fileLength
, const ParserOptions
& opts
)
7852 switch ( opts
.architecture
) {
7853 case CPU_TYPE_X86_64
:
7854 return ( mach_o::relocatable::Parser
<x86_64
>::validFile(fileContent
) );
7856 return ( mach_o::relocatable::Parser
<x86
>::validFile(fileContent
) );
7858 return ( mach_o::relocatable::Parser
<arm
>::validFile(fileContent
, opts
.objSubtypeMustMatch
, opts
.subType
) );
7859 case CPU_TYPE_ARM64
:
7860 return ( mach_o::relocatable::Parser
<arm64
>::validFile(fileContent
, opts
.objSubtypeMustMatch
, opts
.subType
) );
7866 // used by linker to infer architecture when no -arch is on command line
7868 bool isObjectFile(const uint8_t* fileContent
, cpu_type_t
* result
, cpu_subtype_t
* subResult
, Options::Platform
* platform
)
7870 if ( mach_o::relocatable::Parser
<x86_64
>::validFile(fileContent
) ) {
7871 *result
= CPU_TYPE_X86_64
;
7872 const macho_header
<Pointer64
<LittleEndian
> >* header
= (const macho_header
<Pointer64
<LittleEndian
> >*)fileContent
;
7873 *subResult
= header
->cpusubtype();
7874 *platform
= Parser
<x86_64
>::findPlatform(header
);
7877 if ( mach_o::relocatable::Parser
<x86
>::validFile(fileContent
) ) {
7878 const macho_header
<Pointer32
<LittleEndian
> >* header
= (const macho_header
<Pointer32
<LittleEndian
> >*)fileContent
;
7879 *result
= CPU_TYPE_I386
;
7880 *subResult
= CPU_SUBTYPE_X86_ALL
;
7881 *platform
= Parser
<x86
>::findPlatform(header
);
7884 if ( mach_o::relocatable::Parser
<arm
>::validFile(fileContent
, false, 0) ) {
7885 const macho_header
<Pointer32
<LittleEndian
> >* header
= (const macho_header
<Pointer32
<LittleEndian
> >*)fileContent
;
7886 *result
= CPU_TYPE_ARM
;
7887 *subResult
= header
->cpusubtype();
7888 *platform
= Parser
<arm
>::findPlatform(header
);
7891 if ( mach_o::relocatable::Parser
<arm64
>::validFile(fileContent
, false, 0) ) {
7892 const macho_header
<Pointer64
<LittleEndian
> >* header
= (const macho_header
<Pointer64
<LittleEndian
> >*)fileContent
;
7893 *result
= CPU_TYPE_ARM64
;
7894 *subResult
= CPU_SUBTYPE_ARM64_ALL
;
7895 *platform
= Parser
<arm64
>::findPlatform(header
);
7902 // used by linker is error messages to describe bad .o file
7904 const char* archName(const uint8_t* fileContent
)
7906 if ( mach_o::relocatable::Parser
<x86_64
>::validFile(fileContent
) ) {
7907 return mach_o::relocatable::Parser
<x86_64
>::fileKind(fileContent
);
7909 if ( mach_o::relocatable::Parser
<x86
>::validFile(fileContent
) ) {
7910 return mach_o::relocatable::Parser
<x86
>::fileKind(fileContent
);
7912 if ( mach_o::relocatable::Parser
<arm
>::validFile(fileContent
, false, 0) ) {
7913 return mach_o::relocatable::Parser
<arm
>::fileKind(fileContent
);
7919 // Used by archive reader when -ObjC option is specified
7921 bool hasObjC2Categories(const uint8_t* fileContent
)
7923 if ( mach_o::relocatable::Parser
<x86_64
>::validFile(fileContent
) ) {
7924 return mach_o::relocatable::Parser
<x86_64
>::hasObjC2Categories(fileContent
);
7926 else if ( mach_o::relocatable::Parser
<arm
>::validFile(fileContent
, false, 0) ) {
7927 return mach_o::relocatable::Parser
<arm
>::hasObjC2Categories(fileContent
);
7929 else if ( mach_o::relocatable::Parser
<x86
>::validFile(fileContent
, false, 0) ) {
7930 return mach_o::relocatable::Parser
<x86
>::hasObjC2Categories(fileContent
);
7932 #if SUPPORT_ARCH_arm64
7933 else if ( mach_o::relocatable::Parser
<arm64
>::validFile(fileContent
, false, 0) ) {
7934 return mach_o::relocatable::Parser
<arm64
>::hasObjC2Categories(fileContent
);
7941 // Used by archive reader when -ObjC option is specified
7943 bool hasObjC1Categories(const uint8_t* fileContent
)
7945 if ( mach_o::relocatable::Parser
<x86
>::validFile(fileContent
, false, 0) ) {
7946 return mach_o::relocatable::Parser
<x86
>::hasObjC1Categories(fileContent
);
7952 // Used by bitcode obfuscator to get a list of non local symbols from object file
7954 bool getNonLocalSymbols(const uint8_t* fileContent
, std::vector
<const char*> &syms
)
7956 if ( mach_o::relocatable::Parser
<x86_64
>::validFile(fileContent
) ) {
7957 return mach_o::relocatable::Parser
<x86_64
>::getNonLocalSymbols(fileContent
, syms
);
7959 else if ( mach_o::relocatable::Parser
<arm
>::validFile(fileContent
, false, 0) ) {
7960 return mach_o::relocatable::Parser
<arm
>::getNonLocalSymbols(fileContent
, syms
);
7962 else if ( mach_o::relocatable::Parser
<x86
>::validFile(fileContent
, false, 0) ) {
7963 return mach_o::relocatable::Parser
<x86
>::getNonLocalSymbols(fileContent
, syms
);
7965 else if ( mach_o::relocatable::Parser
<arm64
>::validFile(fileContent
, false, 0) ) {
7966 return mach_o::relocatable::Parser
<arm64
>::getNonLocalSymbols(fileContent
, syms
);
7973 } // namespace relocatable
7974 } // namespace mach_o