1 /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-
3 * Copyright (c) 2009-2010 Apple Inc. All rights reserved.
5 * @APPLE_LICENSE_HEADER_START@
7 * This file contains Original Code and/or Modifications of Original Code
8 * as defined in and that are subject to the Apple Public Source License
9 * Version 2.0 (the 'License'). You may not use this file except in
10 * compliance with the License. Please obtain a copy of the License at
11 * http://www.opensource.apple.com/apsl/ and read it before using this
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
19 * Please see the License for the specific language governing rights and
20 * limitations under the License.
22 * @APPLE_LICENSE_HEADER_END@
31 #include <sys/param.h>
35 #include "MachOFileAbstraction.hpp"
37 #include "libunwind/DwarfInstructions.hpp"
38 #include "libunwind/AddressSpace.hpp"
39 #include "libunwind/Registers.hpp"
45 #include <type_traits>
48 #include "debugline.h"
50 #include "Architectures.hpp"
51 #include "Bitcode.hpp"
53 #include "macho_relocatable_file.h"
57 extern void throwf(const char* format
, ...) __attribute__ ((noreturn
,format(printf
, 1, 2)));
58 extern void warning(const char* format
, ...) __attribute__((format(printf
, 1, 2)));
61 namespace relocatable
{
65 template <typename A
> class Parser
;
66 template <typename A
> class Atom
;
67 template <typename A
> class Section
;
68 template <typename A
> class CFISection
;
69 template <typename A
> class CUSection
;
72 class File
: public ld::relocatable::File
75 File(const char* p
, time_t mTime
, const uint8_t* content
, ld::File::Ordinal ord
) :
76 ld::relocatable::File(p
,mTime
,ord
), _fileContent(content
),
77 _sectionsArray(NULL
), _atomsArray(NULL
),
78 _sectionsArrayCount(0), _atomsArrayCount(0), _aliasAtomsArrayCount(0),
79 _debugInfoKind(ld::relocatable::File::kDebugInfoNone
),
80 _dwarfTranslationUnitPath(NULL
),
81 _dwarfDebugInfoSect(NULL
), _dwarfDebugAbbrevSect(NULL
),
82 _dwarfDebugLineSect(NULL
), _dwarfDebugStringSect(NULL
),
83 _objConstraint(ld::File::objcConstraintNone
),
87 _platform(Options::kPlatformUnknown
),
88 _canScatterAtoms(false),
89 _objcHasCategoryClassPropertiesField(false),
90 _srcKind(kSourceUnknown
) { }
93 // overrides of ld::File
94 virtual bool forEachAtom(ld::File::AtomHandler
&) const;
95 virtual bool justInTimeforEachAtom(const char* name
, ld::File::AtomHandler
&) const
97 virtual uint32_t minOSVersion() const { return _minOSVersion
; }
98 virtual uint32_t platform() const { return _platform
; }
100 // overrides of ld::relocatable::File
101 virtual ObjcConstraint
objCConstraint() const { return _objConstraint
; }
102 virtual bool objcHasCategoryClassPropertiesField() const
103 { return _objcHasCategoryClassPropertiesField
; }
104 virtual uint32_t cpuSubType() const { return _cpuSubType
; }
105 virtual DebugInfoKind
debugInfo() const { return _debugInfoKind
; }
106 virtual const std::vector
<ld::relocatable::File::Stab
>* stabs() const { return &_stabs
; }
107 virtual bool canScatterAtoms() const { return _canScatterAtoms
; }
108 virtual const char* translationUnitSource() const;
109 virtual LinkerOptionsList
* linkerOptions() const { return &_linkerOptions
; }
110 virtual const ToolVersionList
& toolVersions() const { return _toolVersions
; }
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 virtual const uint8_t* fileContent() const { 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
;
145 Options::Platform _platform
;
146 bool _canScatterAtoms
;
147 bool _objcHasCategoryClassPropertiesField
;
148 std::vector
<std::vector
<const char*> > _linkerOptions
;
149 std::unique_ptr
<ld::Bitcode
> _bitcode
;
151 ToolVersionList _toolVersions
;
155 template <typename A
>
156 class Section
: public ld::Section
159 typedef typename
A::P::uint_t pint_t
;
160 typedef typename
A::P P
;
161 typedef typename
A::P::E E
;
163 virtual ~Section() { }
164 class File
<A
>& file() const { return _file
; }
165 const macho_section
<P
>* machoSection() const { return _machOSection
; }
166 uint32_t sectionNum(class Parser
<A
>&) const;
167 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
);
168 virtual ld::Atom::ContentType
contentType() { return ld::Atom::typeUnclassified
; }
169 virtual bool dontDeadStrip() { return (this->_machOSection
->flags() & S_ATTR_NO_DEAD_STRIP
); }
170 virtual bool dontDeadStripIfReferencesLive() { return ( (this->_machOSection
!= NULL
) && (this->_machOSection
->flags() & S_ATTR_LIVE_SUPPORT
) ); }
171 virtual Atom
<A
>* findAtomByAddress(pint_t addr
) { return this->findContentAtomByAddress(addr
, this->_beginAtoms
, this->_endAtoms
); }
172 virtual bool addFollowOnFixups() const { return ! _file
.canScatterAtoms(); }
173 virtual uint32_t appendAtoms(class Parser
<A
>& parser
, uint8_t* buffer
,
174 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
175 const struct Parser
<A
>::CFI_CU_InfoArrays
&) = 0;
176 virtual uint32_t computeAtomCount(class Parser
<A
>& parser
,
177 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
178 const struct Parser
<A
>::CFI_CU_InfoArrays
&) = 0;
179 virtual void makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
&);
180 virtual bool addRelocFixup(class Parser
<A
>& parser
, const macho_relocation_info
<P
>*);
181 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const { return 0; }
182 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
183 const ld::IndirectBindingTable
& ind
) const { return false; }
184 virtual bool ignoreLabel(const char* label
) const { return false; }
185 static const char* makeSectionName(const macho_section
<typename
A::P
>* s
);
188 Section(File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
189 : ld::Section(makeSegmentName(s
), makeSectionName(s
), sectionType(s
)),
190 _file(f
), _machOSection(s
), _beginAtoms(NULL
), _endAtoms(NULL
), _hasAliases(false) { }
191 Section(File
<A
>& f
, const char* segName
, const char* sectName
, ld::Section::Type t
, bool hidden
=false)
192 : ld::Section(segName
, sectName
, t
, hidden
), _file(f
), _machOSection(NULL
),
193 _beginAtoms(NULL
), _endAtoms(NULL
), _hasAliases(false) { }
196 Atom
<A
>* findContentAtomByAddress(pint_t addr
, class Atom
<A
>* start
, class Atom
<A
>* end
);
197 uint32_t x86_64PcRelOffset(uint8_t r_type
);
198 void addLOH(class Parser
<A
>& parser
, int kind
, int count
, const uint64_t addrs
[]);
199 static const char* makeSegmentName(const macho_section
<typename
A::P
>* s
);
200 static bool readable(const macho_section
<typename
A::P
>* s
);
201 static bool writable(const macho_section
<typename
A::P
>* s
);
202 static bool exectuable(const macho_section
<typename
A::P
>* s
);
203 static ld::Section::Type
sectionType(const macho_section
<typename
A::P
>* s
);
206 const macho_section
<P
>* _machOSection
;
207 class Atom
<A
>* _beginAtoms
;
208 class Atom
<A
>* _endAtoms
;
210 std::set
<const class Atom
<A
>*> _altEntries
;
214 template <typename A
>
215 class CFISection
: public Section
<A
>
218 CFISection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
219 : Section
<A
>(f
, s
) { }
220 uint32_t cfiCount(Parser
<A
>& parser
);
222 virtual ld::Atom::ContentType
contentType() { return ld::Atom::typeCFI
; }
223 virtual uint32_t computeAtomCount(class Parser
<A
>& parser
, struct Parser
<A
>::LabelAndCFIBreakIterator
& it
, const struct Parser
<A
>::CFI_CU_InfoArrays
&);
224 virtual uint32_t appendAtoms(class Parser
<A
>& parser
, uint8_t* buffer
, struct Parser
<A
>::LabelAndCFIBreakIterator
& it
, const struct Parser
<A
>::CFI_CU_InfoArrays
&);
225 virtual void makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
&);
226 virtual bool addFollowOnFixups() const { return false; }
230 /// ObjectFileAddressSpace is used as a template parameter to UnwindCursor for parsing
231 /// dwarf CFI information in an object file.
236 typedef typename
A::P::uint_t pint_t
;
237 typedef typename
A::P P
;
238 typedef typename
A::P::E E
;
239 typedef typename
A::P::uint_t sint_t
;
241 OAS(CFISection
<A
>& ehFrameSection
, const uint8_t* ehFrameBuffer
) :
242 _ehFrameSection(ehFrameSection
),
243 _ehFrameContent(ehFrameBuffer
),
244 _ehFrameStartAddr(ehFrameSection
.machoSection()->addr()),
245 _ehFrameEndAddr(ehFrameSection
.machoSection()->addr()+ehFrameSection
.machoSection()->size()) {}
247 uint8_t get8(pint_t addr
) { return *((uint8_t*)mappedAddress(addr
)); }
248 uint16_t get16(pint_t addr
) { return E::get16(*((uint16_t*)mappedAddress(addr
))); }
249 uint32_t get32(pint_t addr
) { return E::get32(*((uint32_t*)mappedAddress(addr
))); }
250 uint64_t get64(pint_t addr
) { return E::get64(*((uint64_t*)mappedAddress(addr
))); }
251 pint_t
getP(pint_t addr
) { return P::getP(*((pint_t
*)mappedAddress(addr
))); }
252 uint64_t getULEB128(pint_t
& addr
, pint_t end
);
253 int64_t getSLEB128(pint_t
& addr
, pint_t end
);
254 pint_t
getEncodedP(pint_t
& addr
, pint_t end
, uint8_t encoding
);
256 const void* mappedAddress(pint_t addr
);
258 CFISection
<A
>& _ehFrameSection
;
259 const uint8_t* _ehFrameContent
;
260 pint_t _ehFrameStartAddr
;
261 pint_t _ehFrameEndAddr
;
265 typedef typename
A::P::uint_t pint_t
;
266 typedef libunwind::CFI_Atom_Info
<OAS
> CFI_Atom_Info
;
268 void cfiParse(class Parser
<A
>& parser
, uint8_t* buffer
, CFI_Atom_Info cfiArray
[], uint32_t& cfiCount
, const pint_t cuStarts
[], uint32_t cuCount
);
269 bool needsRelocating();
271 static bool bigEndian();
273 void addCiePersonalityFixups(class Parser
<A
>& parser
, const CFI_Atom_Info
* cieInfo
);
274 static void warnFunc(void* ref
, uint64_t funcAddr
, const char* msg
);
278 template <typename A
>
279 class CUSection
: public Section
<A
>
282 CUSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
283 : Section
<A
>(f
, s
) { }
285 typedef typename
A::P::uint_t pint_t
;
286 typedef typename
A::P P
;
287 typedef typename
A::P::E E
;
289 virtual uint32_t computeAtomCount(class Parser
<A
>& parser
, struct Parser
<A
>::LabelAndCFIBreakIterator
& it
, const struct Parser
<A
>::CFI_CU_InfoArrays
&) { return 0; }
290 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; }
291 virtual void makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
&);
292 virtual bool addFollowOnFixups() const { return false; }
295 pint_t functionStartAddress
;
296 uint32_t functionSymbolIndex
;
297 uint32_t rangeLength
;
298 uint32_t compactUnwindInfo
;
299 const char* personality
;
306 void parse(class Parser
<A
>& parser
, uint32_t cnt
, Info array
[]);
307 static bool encodingMeansUseDwarf(compact_unwind_encoding_t enc
);
312 const char* personalityName(class Parser
<A
>& parser
, const macho_relocation_info
<P
>* reloc
);
314 static int infoSorter(const void* l
, const void* r
);
319 template <typename A
>
320 class TentativeDefinitionSection
: public Section
<A
>
323 TentativeDefinitionSection(Parser
<A
>& parser
, File
<A
>& f
)
324 : Section
<A
>(f
, "__DATA", "__comm/tent", ld::Section::typeTentativeDefs
) {}
326 virtual ld::Atom::ContentType
contentType() { return ld::Atom::typeZeroFill
; }
327 virtual bool addFollowOnFixups() const { return false; }
328 virtual Atom
<A
>* findAtomByAddress(typename
A::P::uint_t addr
) { throw "TentativeDefinitionSection::findAtomByAddress() should never be called"; }
329 virtual uint32_t computeAtomCount(class Parser
<A
>& parser
, struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
330 const struct Parser
<A
>::CFI_CU_InfoArrays
&);
331 virtual uint32_t appendAtoms(class Parser
<A
>& parser
, uint8_t* buffer
,
332 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
333 const struct Parser
<A
>::CFI_CU_InfoArrays
&);
334 virtual void makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
&) {}
336 typedef typename
A::P::uint_t pint_t
;
337 typedef typename
A::P P
;
341 template <typename A
>
342 class AbsoluteSymbolSection
: public Section
<A
>
345 AbsoluteSymbolSection(Parser
<A
>& parser
, File
<A
>& f
)
346 : Section
<A
>(f
, "__DATA", "__abs", ld::Section::typeAbsoluteSymbols
, true) {}
348 virtual ld::Atom::ContentType
contentType() { return ld::Atom::typeUnclassified
; }
349 virtual bool dontDeadStrip() { return false; }
350 virtual ld::Atom::Alignment
alignmentForAddress(typename
A::P::uint_t addr
) { return ld::Atom::Alignment(0); }
351 virtual bool addFollowOnFixups() const { return false; }
352 virtual Atom
<A
>* findAtomByAddress(typename
A::P::uint_t addr
) { throw "AbsoluteSymbolSection::findAtomByAddress() should never be called"; }
353 virtual uint32_t computeAtomCount(class Parser
<A
>& parser
, struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
354 const struct Parser
<A
>::CFI_CU_InfoArrays
&);
355 virtual uint32_t appendAtoms(class Parser
<A
>& parser
, uint8_t* buffer
,
356 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
357 const struct Parser
<A
>::CFI_CU_InfoArrays
&);
358 virtual void makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
&) {}
359 virtual Atom
<A
>* findAbsAtomForValue(typename
A::P::uint_t
);
362 typedef typename
A::P::uint_t pint_t
;
363 typedef typename
A::P P
;
367 template <typename A
>
368 class SymboledSection
: public Section
<A
>
371 SymboledSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
);
372 virtual ld::Atom::ContentType
contentType() { return _type
; }
373 virtual bool dontDeadStrip();
374 virtual uint32_t computeAtomCount(class Parser
<A
>& parser
, struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
375 const struct Parser
<A
>::CFI_CU_InfoArrays
&);
376 virtual uint32_t appendAtoms(class Parser
<A
>& parser
, uint8_t* buffer
,
377 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
378 const struct Parser
<A
>::CFI_CU_InfoArrays
&);
380 typedef typename
A::P::uint_t pint_t
;
381 typedef typename
A::P P
;
383 ld::Atom::ContentType _type
;
387 template <typename A
>
388 class TLVDefsSection
: public SymboledSection
<A
>
391 TLVDefsSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
) :
392 SymboledSection
<A
>(parser
, f
, s
) { }
394 typedef typename
A::P::uint_t pint_t
;
396 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
) { return ld::Atom::Alignment(log2(sizeof(pint_t
))); }
403 template <typename A
>
404 class ImplicitSizeSection
: public Section
<A
>
407 ImplicitSizeSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
408 : Section
<A
>(f
, s
) { }
409 virtual uint32_t computeAtomCount(class Parser
<A
>& parser
, struct Parser
<A
>::LabelAndCFIBreakIterator
& it
, const struct Parser
<A
>::CFI_CU_InfoArrays
&);
410 virtual uint32_t appendAtoms(class Parser
<A
>& parser
, uint8_t* buffer
, struct Parser
<A
>::LabelAndCFIBreakIterator
& it
, const struct Parser
<A
>::CFI_CU_InfoArrays
&);
412 typedef typename
A::P::uint_t pint_t
;
413 typedef typename
A::P P
;
415 virtual bool addFollowOnFixups() const { return false; }
416 virtual const char* unlabeledAtomName(Parser
<A
>& parser
, pint_t addr
) = 0;
417 virtual ld::Atom::SymbolTableInclusion
symbolTableInclusion();
418 virtual pint_t
elementSizeAtAddress(pint_t addr
) = 0;
419 virtual ld::Atom::Scope
scopeAtAddress(Parser
<A
>& parser
, pint_t addr
) { return ld::Atom::scopeLinkageUnit
; }
420 virtual bool useElementAt(Parser
<A
>& parser
,
421 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
, pint_t addr
) = 0;
422 virtual ld::Atom::Definition
definition() { return ld::Atom::definitionRegular
; }
423 virtual ld::Atom::Combine
combine(Parser
<A
>& parser
, pint_t addr
) = 0;
424 virtual bool ignoreLabel(const char* label
) const { return (label
[0] == 'L'); }
428 template <typename A
>
429 class FixedSizeSection
: public ImplicitSizeSection
<A
>
432 FixedSizeSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
433 : ImplicitSizeSection
<A
>(parser
, f
, s
) { }
435 typedef typename
A::P::uint_t pint_t
;
436 typedef typename
A::P P
;
437 typedef typename
A::P::E E
;
439 virtual bool useElementAt(Parser
<A
>& parser
,
440 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
, pint_t addr
)
445 template <typename A
>
446 class Literal4Section
: public FixedSizeSection
<A
>
449 Literal4Section(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
450 : FixedSizeSection
<A
>(parser
, f
, s
) {}
452 typedef typename
A::P::uint_t pint_t
;
453 typedef typename
A::P P
;
455 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
) { return ld::Atom::Alignment(2); }
456 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "4-byte-literal"; }
457 virtual pint_t
elementSizeAtAddress(pint_t addr
) { return 4; }
458 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
) { return ld::Atom::combineByNameAndContent
; }
459 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
460 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
461 const ld::IndirectBindingTable
& ind
) const;
462 virtual bool ignoreLabel(const char* label
) const;
465 template <typename A
>
466 class Literal8Section
: public FixedSizeSection
<A
>
469 Literal8Section(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
470 : FixedSizeSection
<A
>(parser
, f
, s
) {}
472 typedef typename
A::P::uint_t pint_t
;
473 typedef typename
A::P P
;
475 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
) { return ld::Atom::Alignment(3); }
476 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "8-byte-literal"; }
477 virtual pint_t
elementSizeAtAddress(pint_t addr
) { return 8; }
478 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
) { return ld::Atom::combineByNameAndContent
; }
479 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
480 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
481 const ld::IndirectBindingTable
& ind
) const;
482 virtual bool ignoreLabel(const char* label
) const;
485 template <typename A
>
486 class Literal16Section
: public FixedSizeSection
<A
>
489 Literal16Section(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
490 : FixedSizeSection
<A
>(parser
, f
, s
) {}
492 typedef typename
A::P::uint_t pint_t
;
493 typedef typename
A::P P
;
495 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
) { return ld::Atom::Alignment(4); }
496 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "16-byte-literal"; }
497 virtual pint_t
elementSizeAtAddress(pint_t addr
) { return 16; }
498 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
) { return ld::Atom::combineByNameAndContent
; }
499 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
500 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
501 const ld::IndirectBindingTable
& ind
) const;
502 virtual bool ignoreLabel(const char* label
) const;
506 template <typename A
>
507 class NonLazyPointerSection
: public FixedSizeSection
<A
>
510 NonLazyPointerSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
511 : FixedSizeSection
<A
>(parser
, f
, s
) {}
513 typedef typename
A::P::uint_t pint_t
;
514 typedef typename
A::P P
;
516 virtual void makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
&);
517 virtual ld::Atom::ContentType
contentType() { return ld::Atom::typeNonLazyPointer
; }
518 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
) { return ld::Atom::Alignment(log2(sizeof(pint_t
))); }
519 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "non_lazy_ptr"; }
520 virtual pint_t
elementSizeAtAddress(pint_t addr
) { return sizeof(pint_t
); }
521 virtual ld::Atom::Scope
scopeAtAddress(Parser
<A
>& parser
, pint_t addr
);
522 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
);
523 virtual bool ignoreLabel(const char* label
) const { return true; }
524 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
525 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
526 const ld::IndirectBindingTable
& ind
) const;
529 static const char* targetName(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
);
530 static ld::Fixup::Kind
fixupKind();
533 template <typename A
>
534 class TLVPointerSection
: public FixedSizeSection
<A
>
537 TLVPointerSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
538 : FixedSizeSection
<A
>(parser
, f
, s
) {}
540 typedef typename
A::P::uint_t pint_t
;
541 typedef typename
A::P P
;
543 virtual void makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
&);
544 virtual ld::Atom::ContentType
contentType() { return ld::Atom::typeTLVPointer
; }
545 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
) { return ld::Atom::Alignment(log2(sizeof(pint_t
))); }
546 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "tlv_lazy_ptr"; }
547 virtual pint_t
elementSizeAtAddress(pint_t addr
) { return sizeof(pint_t
); }
548 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
);
549 virtual bool ignoreLabel(const char* label
) const { return true; }
550 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
551 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
552 const ld::IndirectBindingTable
& ind
) const;
555 static const char* targetName(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
, bool* isStatic
);
559 template <typename A
>
560 class CFStringSection
: public FixedSizeSection
<A
>
563 CFStringSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
564 : FixedSizeSection
<A
>(parser
, f
, s
) {}
566 typedef typename
A::P::uint_t pint_t
;
568 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
) { return ld::Atom::Alignment(log2(sizeof(pint_t
))); }
569 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "CFString"; }
570 virtual pint_t
elementSizeAtAddress(pint_t addr
) { return 4*sizeof(pint_t
); }
571 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
) { return ld::Atom::combineByNameAndReferences
; }
572 virtual bool ignoreLabel(const char* label
) const { return true; }
573 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
574 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
575 const ld::IndirectBindingTable
& ind
) const;
577 enum ContentType
{ contentUTF8
, contentUTF16
, contentUnknown
};
578 static const uint8_t* targetContent(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
,
579 ContentType
* ct
, unsigned int* count
);
583 template <typename A
>
584 class ObjC1ClassSection
: public FixedSizeSection
<A
>
587 ObjC1ClassSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
588 : FixedSizeSection
<A
>(parser
, f
, s
) {}
590 typedef typename
A::P::uint_t pint_t
;
591 typedef typename
A::P P
;
592 typedef typename
A::P::E E
;
594 virtual ld::Atom::Scope
scopeAtAddress(Parser
<A
>& , pint_t
) { return ld::Atom::scopeGlobal
; }
595 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
) { return ld::Atom::Alignment(2); }
596 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
);
597 virtual ld::Atom::SymbolTableInclusion
symbolTableInclusion() { return ld::Atom::symbolTableIn
; }
598 virtual pint_t
elementSizeAtAddress(pint_t addr
);
599 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
) { return ld::Atom::combineNever
; }
600 virtual bool ignoreLabel(const char* label
) const { return true; }
601 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
603 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
604 const ld::IndirectBindingTable
& ind
) const { return false; }
605 virtual bool addRelocFixup(class Parser
<A
>& parser
, const macho_relocation_info
<P
>*);
609 template <typename A
>
610 class ObjC2ClassRefsSection
: public FixedSizeSection
<A
>
613 ObjC2ClassRefsSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
614 : FixedSizeSection
<A
>(parser
, f
, s
) {}
616 typedef typename
A::P::uint_t pint_t
;
618 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
) { return ld::Atom::Alignment(log2(sizeof(pint_t
))); }
619 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "objc-class-ref"; }
620 virtual pint_t
elementSizeAtAddress(pint_t addr
) { return sizeof(pint_t
); }
621 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
) { return ld::Atom::combineByNameAndReferences
; }
622 virtual bool ignoreLabel(const char* label
) const { return true; }
623 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
624 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
625 const ld::IndirectBindingTable
& ind
) const;
627 const char* targetClassName(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
631 template <typename A
>
632 class ObjC2CategoryListSection
: public FixedSizeSection
<A
>
635 ObjC2CategoryListSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
636 : FixedSizeSection
<A
>(parser
, f
, s
) {}
638 typedef typename
A::P::uint_t pint_t
;
640 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
) { return ld::Atom::Alignment(log2(sizeof(pint_t
))); }
641 virtual ld::Atom::Scope
scopeAtAddress(Parser
<A
>& parser
, pint_t addr
) { return ld::Atom::scopeTranslationUnit
; }
642 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "objc-cat-list"; }
643 virtual pint_t
elementSizeAtAddress(pint_t addr
) { return sizeof(pint_t
); }
644 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
) { return ld::Atom::combineNever
; }
645 virtual bool ignoreLabel(const char* label
) const { return true; }
647 const char* targetClassName(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
651 template <typename A
>
652 class PointerToCStringSection
: public FixedSizeSection
<A
>
655 PointerToCStringSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
656 : FixedSizeSection
<A
>(parser
, f
, s
) {}
658 typedef typename
A::P::uint_t pint_t
;
660 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
) { return ld::Atom::Alignment(log2(sizeof(pint_t
))); }
661 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "pointer-to-literal-cstring"; }
662 virtual pint_t
elementSizeAtAddress(pint_t addr
) { return sizeof(pint_t
); }
663 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
) { return ld::Atom::combineByNameAndReferences
; }
664 virtual bool ignoreLabel(const char* label
) const { return true; }
665 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
666 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
667 const ld::IndirectBindingTable
& ind
) const;
668 virtual const char* targetCString(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
672 template <typename A
>
673 class Objc1ClassReferences
: public PointerToCStringSection
<A
>
676 Objc1ClassReferences(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
677 : PointerToCStringSection
<A
>(parser
, f
, s
) {}
679 typedef typename
A::P::uint_t pint_t
;
680 typedef typename
A::P P
;
682 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "pointer-to-literal-objc-class-name"; }
683 virtual bool addRelocFixup(class Parser
<A
>& parser
, const macho_relocation_info
<P
>*);
684 virtual const char* targetCString(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
688 template <typename A
>
689 class CStringSection
: public ImplicitSizeSection
<A
>
692 CStringSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
693 : ImplicitSizeSection
<A
>(parser
, f
, s
) {}
695 typedef typename
A::P::uint_t pint_t
;
696 typedef typename
A::P P
;
698 virtual ld::Atom::ContentType
contentType() { return ld::Atom::typeCString
; }
699 virtual Atom
<A
>* findAtomByAddress(pint_t addr
);
700 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "cstring"; }
701 virtual pint_t
elementSizeAtAddress(pint_t addr
);
702 virtual bool ignoreLabel(const char* label
) const;
703 virtual bool useElementAt(Parser
<A
>& parser
,
704 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
, pint_t addr
);
705 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
) { return ld::Atom::combineByNameAndContent
; }
706 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
707 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
708 const ld::IndirectBindingTable
& ind
) const;
713 template <typename A
>
714 class UTF16StringSection
: public SymboledSection
<A
>
717 UTF16StringSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
718 : SymboledSection
<A
>(parser
, f
, s
) {}
720 typedef typename
A::P::uint_t pint_t
;
721 typedef typename
A::P P
;
723 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
) { return ld::Atom::combineByNameAndContent
; }
724 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
725 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
726 const ld::IndirectBindingTable
& ind
) const;
731 // Atoms in mach-o files
733 template <typename A
>
734 class Atom
: public ld::Atom
737 // overrides of ld::Atom
738 virtual const ld::File
* file() const;
739 virtual const char* translationUnitSource() const
740 { return sect().file().translationUnitSource(); }
741 virtual const char* name() const { return _name
; }
742 virtual uint64_t size() const { return _size
; }
743 virtual uint64_t objectAddress() const { return _objAddress
; }
744 virtual void copyRawContent(uint8_t buffer
[]) const;
745 virtual const uint8_t* rawContentPointer() const { return contentPointer(); }
746 virtual unsigned long contentHash(const ld::IndirectBindingTable
& ind
) const
747 { if ( _hash
== 0 ) _hash
= sect().contentHash(this, ind
); return _hash
; }
748 virtual bool canCoalesceWith(const ld::Atom
& rhs
, const ld::IndirectBindingTable
& ind
) const
749 { return sect().canCoalesceWith(this, rhs
, ind
); }
750 virtual ld::Fixup::iterator
fixupsBegin() const { return &machofile()._fixups
[_fixupsStartIndex
]; }
751 virtual ld::Fixup::iterator
fixupsEnd() const { return &machofile()._fixups
[_fixupsStartIndex
+_fixupsCount
]; }
752 virtual ld::Atom::UnwindInfo::iterator
beginUnwind() const { return &machofile()._unwindInfos
[_unwindInfoStartIndex
]; }
753 virtual ld::Atom::UnwindInfo::iterator
endUnwind() const { return &machofile()._unwindInfos
[_unwindInfoStartIndex
+_unwindInfoCount
]; }
754 virtual ld::Atom::LineInfo::iterator
beginLineInfo() const{ return &machofile()._lineInfos
[_lineInfoStartIndex
]; }
755 virtual ld::Atom::LineInfo::iterator
endLineInfo() const { return &machofile()._lineInfos
[_lineInfoStartIndex
+_lineInfoCount
]; }
756 virtual void setFile(const ld::File
* f
);
760 enum { kFixupStartIndexBits
= 32,
761 kLineInfoStartIndexBits
= 32,
762 kUnwindInfoStartIndexBits
= 24,
763 kFixupCountBits
= 24,
764 kLineInfoCountBits
= 12,
765 kUnwindInfoCountBits
= 4
766 }; // must sum to 128
769 // methods for all atoms from mach-o object file
770 Section
<A
>& sect() const { return (Section
<A
>&)section(); }
771 File
<A
>& machofile() const { return ((Section
<A
>*)(this->_section
))->file(); }
772 void setFixupsRange(uint32_t s
, uint32_t c
);
773 void setUnwindInfoRange(uint32_t s
, uint32_t c
);
774 void extendUnwindInfoRange();
775 void setLineInfoRange(uint32_t s
, uint32_t c
);
776 bool roomForMoreLineInfoCount() { return (_lineInfoCount
< ((1<<kLineInfoCountBits
)-1)); }
777 void incrementLineInfoCount() { assert(roomForMoreLineInfoCount()); ++_lineInfoCount
; }
778 void incrementFixupCount() { if (_fixupsCount
== ((1 << kFixupCountBits
)-1))
779 throwf("too may fixups in %s", name()); ++_fixupsCount
; }
780 const uint8_t* contentPointer() const;
781 uint32_t fixupCount() const { return _fixupsCount
; }
782 void verifyAlignment(const macho_section
<typename
A::P
>&) const;
784 typedef typename
A::P P
;
785 typedef typename
A::P::E E
;
786 typedef typename
A::P::uint_t pint_t
;
787 // constuct via all attributes
788 Atom(Section
<A
>& sct
, const char* nm
, pint_t addr
, uint64_t sz
,
789 ld::Atom::Definition d
, ld::Atom::Combine c
, ld::Atom::Scope s
,
790 ld::Atom::ContentType ct
, ld::Atom::SymbolTableInclusion i
,
791 bool dds
, bool thumb
, bool al
, ld::Atom::Alignment a
)
792 : ld::Atom((ld::Section
&)sct
, d
, c
, s
, ct
, i
, dds
, thumb
, al
, a
),
793 _size(sz
), _objAddress(addr
), _name(nm
), _hash(0),
794 _fixupsStartIndex(0), _lineInfoStartIndex(0),
795 _unwindInfoStartIndex(0), _fixupsCount(0),
796 _lineInfoCount(0), _unwindInfoCount(0) { }
797 // construct via symbol table entry
798 Atom(Section
<A
>& sct
, Parser
<A
>& parser
, const macho_nlist
<P
>& sym
,
799 uint64_t sz
, bool alias
=false)
800 : ld::Atom((ld::Section
&)sct
, parser
.definitionFromSymbol(sym
),
801 parser
.combineFromSymbol(sym
), parser
.scopeFromSymbol(sym
),
802 parser
.resolverFromSymbol(sym
) ? ld::Atom::typeResolver
: sct
.contentType(),
803 parser
.inclusionFromSymbol(sym
),
804 (parser
.dontDeadStripFromSymbol(sym
) && !sct
.dontDeadStripIfReferencesLive()) || sct
.dontDeadStrip(),
805 parser
.isThumbFromSymbol(sym
), alias
,
806 sct
.alignmentForAddress(sym
.n_value())),
807 _size(sz
), _objAddress(sym
.n_value()),
808 _name(parser
.nameFromSymbol(sym
)), _hash(0),
809 _fixupsStartIndex(0), _lineInfoStartIndex(0),
810 _unwindInfoStartIndex(0), _fixupsCount(0),
811 _lineInfoCount(0), _unwindInfoCount(0) {
812 // <rdar://problem/6783167> support auto-hidden weak symbols
813 if ( _scope
== ld::Atom::scopeGlobal
&&
814 (sym
.n_desc() & (N_WEAK_DEF
|N_WEAK_REF
)) == (N_WEAK_DEF
|N_WEAK_REF
) )
816 this->verifyAlignment(*sct
.machoSection());
817 if ( sct
.dontDeadStripIfReferencesLive() )
818 this->setDontDeadStripIfReferencesLive();
822 friend class Parser
<A
>;
823 friend class Section
<A
>;
824 friend class CStringSection
<A
>;
825 friend class AbsoluteSymbolSection
<A
>;
830 mutable unsigned long _hash
;
832 uint64_t _fixupsStartIndex
: kFixupStartIndexBits
,
833 _lineInfoStartIndex
: kLineInfoStartIndexBits
,
834 _unwindInfoStartIndex
: kUnwindInfoStartIndexBits
,
835 _fixupsCount
: kFixupCountBits
,
836 _lineInfoCount
: kLineInfoCountBits
,
837 _unwindInfoCount
: kUnwindInfoCountBits
;
839 static std::map
<const ld::Atom
*, const ld::File
*> _s_fileOverride
;
842 template <typename A
>
843 std::map
<const ld::Atom
*, const ld::File
*> Atom
<A
>::_s_fileOverride
;
845 template <typename A
>
846 void Atom
<A
>::setFile(const ld::File
* f
) {
847 _s_fileOverride
[this] = f
;
850 template <typename A
>
851 const ld::File
* Atom
<A
>::file() const
853 std::map
<const ld::Atom
*, const ld::File
*>::iterator pos
= _s_fileOverride
.find(this);
854 if ( pos
!= _s_fileOverride
.end() )
857 return §().file();
860 template <typename A
>
861 void Atom
<A
>::setFixupsRange(uint32_t startIndex
, uint32_t count
)
863 if ( count
>= (1 << kFixupCountBits
) )
864 throwf("too many fixups in function %s", this->name());
865 if ( startIndex
>= (1 << kFixupStartIndexBits
) )
866 throwf("too many fixups in file");
867 assert(((startIndex
+count
) <= sect().file()._fixups
.size()) && "fixup index out of range");
868 _fixupsStartIndex
= startIndex
;
869 _fixupsCount
= count
;
872 template <typename A
>
873 void Atom
<A
>::setUnwindInfoRange(uint32_t startIndex
, uint32_t count
)
875 if ( count
>= (1 << kUnwindInfoCountBits
) )
876 throwf("too many compact unwind infos in function %s", this->name());
877 if ( startIndex
>= (1 << kUnwindInfoStartIndexBits
) )
878 throwf("too many compact unwind infos (%d) in file", startIndex
);
879 assert((startIndex
+count
) <= sect().file()._unwindInfos
.size() && "unwindinfo index out of range");
880 _unwindInfoStartIndex
= startIndex
;
881 _unwindInfoCount
= count
;
884 template <typename A
>
885 void Atom
<A
>::extendUnwindInfoRange()
887 if ( _unwindInfoCount
+1 >= (1 << kUnwindInfoCountBits
) )
888 throwf("too many compact unwind infos in function %s", this->name());
889 _unwindInfoCount
+= 1;
892 template <typename A
>
893 void Atom
<A
>::setLineInfoRange(uint32_t startIndex
, uint32_t count
)
895 assert((count
< (1 << kLineInfoCountBits
)) && "too many line infos");
896 assert((startIndex
+count
) < sect().file()._lineInfos
.size() && "line info index out of range");
897 _lineInfoStartIndex
= startIndex
;
898 _lineInfoCount
= count
;
901 template <typename A
>
902 const uint8_t* Atom
<A
>::contentPointer() const
904 const macho_section
<P
>* sct
= this->sect().machoSection();
905 if ( this->_objAddress
> sct
->addr() + sct
->size() )
906 throwf("malformed .o file, symbol has address 0x%0llX which is outside range of its section", (uint64_t)this->_objAddress
);
907 uint32_t fileOffset
= sct
->offset() - sct
->addr() + this->_objAddress
;
908 return this->sect().file().fileContent()+fileOffset
;
912 template <typename A
>
913 void Atom
<A
>::copyRawContent(uint8_t buffer
[]) const
916 if ( this->contentType() == ld::Atom::typeZeroFill
) {
917 bzero(buffer
, _size
);
919 else if ( _size
!= 0 ) {
920 memcpy(buffer
, this->contentPointer(), _size
);
925 void Atom
<arm
>::verifyAlignment(const macho_section
<P
>&) const
927 if ( (this->section().type() == ld::Section::typeCode
) && ! isThumb() ) {
928 if ( ((_objAddress
% 4) != 0) || (this->alignment().powerOf2
< 2) )
929 warning("ARM function not 4-byte aligned: %s from %s", this->name(), this->file()->path());
933 #if SUPPORT_ARCH_arm64
935 void Atom
<arm64
>::verifyAlignment(const macho_section
<P
>& sect
) const
937 if ( (this->section().type() == ld::Section::typeCode
) && (sect
.size() != 0) ) {
938 if ( ((_objAddress
% 4) != 0) || (this->alignment().powerOf2
< 2) )
939 warning("arm64 function not 4-byte aligned: %s from %s", this->name(), this->file()->path());
945 template <typename A
>
946 void Atom
<A
>::verifyAlignment(const macho_section
<P
>&) const
951 class AliasAtom
: public ld::Atom
954 AliasAtom(const char* name
, bool hidden
, const ld::File
* file
, const char* aliasOfName
) :
955 ld::Atom(_s_section
, ld::Atom::definitionRegular
, ld::Atom::combineNever
,
956 (hidden
? ld::Atom::scopeLinkageUnit
: ld::Atom::scopeGlobal
),
957 ld::Atom::typeUnclassified
, ld::Atom::symbolTableIn
,
958 false, false, true, 0),
961 _fixup(0, ld::Fixup::k1of1
, ld::Fixup::kindNoneFollowOn
, ld::Fixup::bindingByNameUnbound
, aliasOfName
) { }
963 virtual const ld::File
* file() const { return _file
; }
964 virtual const char* translationUnitSource() const
966 virtual const char* name() const { return _name
; }
967 virtual uint64_t size() const { return 0; }
968 virtual uint64_t objectAddress() const { return 0; }
969 virtual void copyRawContent(uint8_t buffer
[]) const { }
970 virtual ld::Fixup::iterator
fixupsBegin() const { return &((ld::Fixup
*)&_fixup
)[0]; }
971 virtual ld::Fixup::iterator
fixupsEnd() const { return &((ld::Fixup
*)&_fixup
)[1]; }
974 static ld::Section _s_section
;
976 const ld::File
* _file
;
981 ld::Section
AliasAtom::_s_section("__LD", "__aliases", ld::Section::typeTempAlias
, true);
984 template <typename A
>
988 static bool validFile(const uint8_t* fileContent
, bool subtypeMustMatch
=false,
989 cpu_subtype_t subtype
=0);
990 static const char* fileKind(const uint8_t* fileContent
);
991 static Options::Platform
findPlatform(const macho_header
<typename
A::P
>* header
);
992 static bool hasObjC2Categories(const uint8_t* fileContent
);
993 static bool hasObjC1Categories(const uint8_t* fileContent
);
994 static bool getNonLocalSymbols(const uint8_t* fileContnet
, std::vector
<const char*> &syms
);
995 static ld::relocatable::File
* parse(const uint8_t* fileContent
, uint64_t fileLength
,
996 const char* path
, time_t modTime
, ld::File::Ordinal ordinal
,
997 const ParserOptions
& opts
) {
998 Parser
p(fileContent
, fileLength
, path
, modTime
,
999 ordinal
, opts
.warnUnwindConversionProblems
,
1000 opts
.keepDwarfUnwind
, opts
.forceDwarfConversion
,
1001 opts
.neverConvertDwarf
, opts
.verboseOptimizationHints
,
1002 opts
.ignoreMismatchPlatform
);
1003 return p
.parse(opts
);
1006 typedef typename
A::P P
;
1007 typedef typename
A::P::E E
;
1008 typedef typename
A::P::uint_t pint_t
;
1010 struct SourceLocation
{
1012 SourceLocation(Atom
<A
>* a
, uint32_t o
) : atom(a
), offsetInAtom(o
) {}
1014 uint32_t offsetInAtom
;
1019 const char* name
; // only used if targetAtom is NULL
1021 bool weakImport
; // only used if targetAtom is NULL
1024 struct FixupInAtom
{
1025 FixupInAtom(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, Atom
<A
>* target
) :
1026 fixup(src
.offsetInAtom
, c
, k
, target
), atom(src
.atom
) { src
.atom
->incrementFixupCount(); }
1028 FixupInAtom(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, ld::Fixup::TargetBinding b
, Atom
<A
>* target
) :
1029 fixup(src
.offsetInAtom
, c
, k
, b
, target
), atom(src
.atom
) { src
.atom
->incrementFixupCount(); }
1031 FixupInAtom(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, bool wi
, const char* name
) :
1032 fixup(src
.offsetInAtom
, c
, k
, wi
, name
), atom(src
.atom
) { src
.atom
->incrementFixupCount(); }
1034 FixupInAtom(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, ld::Fixup::TargetBinding b
, const char* name
) :
1035 fixup(src
.offsetInAtom
, c
, k
, b
, name
), atom(src
.atom
) { src
.atom
->incrementFixupCount(); }
1037 FixupInAtom(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, uint64_t addend
) :
1038 fixup(src
.offsetInAtom
, c
, k
, addend
), atom(src
.atom
) { src
.atom
->incrementFixupCount(); }
1040 FixupInAtom(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
) :
1041 fixup(src
.offsetInAtom
, c
, k
, (uint64_t)0), atom(src
.atom
) { src
.atom
->incrementFixupCount(); }
1047 void addFixup(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, Atom
<A
>* target
) {
1048 _allFixups
.push_back(FixupInAtom(src
, c
, k
, target
));
1051 void addFixup(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, ld::Fixup::TargetBinding b
, Atom
<A
>* target
) {
1052 _allFixups
.push_back(FixupInAtom(src
, c
, k
, b
, target
));
1055 void addFixup(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, bool wi
, const char* name
) {
1056 _allFixups
.push_back(FixupInAtom(src
, c
, k
, wi
, name
));
1059 void addFixup(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, ld::Fixup::TargetBinding b
, const char* name
) {
1060 _allFixups
.push_back(FixupInAtom(src
, c
, k
, b
, name
));
1063 void addFixup(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, uint64_t addend
) {
1064 _allFixups
.push_back(FixupInAtom(src
, c
, k
, addend
));
1067 void addFixup(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
) {
1068 _allFixups
.push_back(FixupInAtom(src
, c
, k
));
1071 const char* path() { return _path
; }
1072 uint32_t symbolCount() { return _symbolCount
; }
1073 uint32_t indirectSymbol(uint32_t indirectIndex
);
1074 const macho_nlist
<P
>& symbolFromIndex(uint32_t index
);
1075 const char* nameFromSymbol(const macho_nlist
<P
>& sym
);
1076 ld::Atom::Scope
scopeFromSymbol(const macho_nlist
<P
>& sym
);
1077 static ld::Atom::Definition
definitionFromSymbol(const macho_nlist
<P
>& sym
);
1078 static ld::Atom::Combine
combineFromSymbol(const macho_nlist
<P
>& sym
);
1079 ld::Atom::SymbolTableInclusion
inclusionFromSymbol(const macho_nlist
<P
>& sym
);
1080 static bool dontDeadStripFromSymbol(const macho_nlist
<P
>& sym
);
1081 static bool isThumbFromSymbol(const macho_nlist
<P
>& sym
);
1082 static bool weakImportFromSymbol(const macho_nlist
<P
>& sym
);
1083 static bool resolverFromSymbol(const macho_nlist
<P
>& sym
);
1084 static bool altEntryFromSymbol(const macho_nlist
<P
>& sym
);
1085 uint32_t symbolIndexFromIndirectSectionAddress(pint_t
,const macho_section
<P
>*);
1086 const macho_section
<P
>* firstMachOSection() { return _sectionsStart
; }
1087 const macho_section
<P
>* machOSectionFromSectionIndex(uint32_t index
);
1088 uint32_t machOSectionCount() { return _machOSectionsCount
; }
1089 uint32_t undefinedStartIndex() { return _undefinedStartIndex
; }
1090 uint32_t undefinedEndIndex() { return _undefinedEndIndex
; }
1091 void addFixup(FixupInAtom f
) { _allFixups
.push_back(f
); }
1092 Section
<A
>* sectionForNum(unsigned int sectNum
);
1093 Section
<A
>* sectionForAddress(pint_t addr
);
1094 Atom
<A
>* findAtomByAddress(pint_t addr
);
1095 Atom
<A
>* findAtomByAddressOrNullIfStub(pint_t addr
);
1096 Atom
<A
>* findAtomByAddressOrLocalTargetOfStub(pint_t addr
, uint32_t* offsetInAtom
);
1097 Atom
<A
>* findAtomByName(const char* name
); // slow!
1098 void findTargetFromAddress(pint_t addr
, TargetDesc
& target
);
1099 void findTargetFromAddress(pint_t baseAddr
, pint_t addr
, TargetDesc
& target
);
1100 void findTargetFromAddressAndSectionNum(pint_t addr
, unsigned int sectNum
,
1101 TargetDesc
& target
);
1102 uint32_t tentativeDefinitionCount() { return _tentativeDefinitionCount
; }
1103 uint32_t absoluteSymbolCount() { return _absoluteSymbolCount
; }
1105 uint32_t fileLength() const { return _fileLength
; }
1106 bool hasStubsSection() { return (_stubsSectionNum
!= 0); }
1107 unsigned int stubsSectionNum() { return _stubsSectionNum
; }
1108 void addDtraceExtraInfos(const SourceLocation
& src
, const char* provider
);
1109 const char* scanSymbolTableForAddress(uint64_t addr
);
1110 bool warnUnwindConversionProblems() { return _warnUnwindConversionProblems
; }
1111 bool hasDataInCodeLabels() { return _hasDataInCodeLabels
; }
1112 bool keepDwarfUnwind() { return _keepDwarfUnwind
; }
1113 bool forceDwarfConversion() { return _forceDwarfConversion
; }
1114 bool verboseOptimizationHints() { return _verboseOptimizationHints
; }
1115 bool neverConvertDwarf() { return _neverConvertDwarf
; }
1116 bool armUsesZeroCostExceptions() { return _armUsesZeroCostExceptions
; }
1117 uint8_t maxDefaultCommonAlignment() { return _maxDefaultCommonAlignment
; }
1120 macho_data_in_code_entry
<P
>* dataInCodeStart() { return _dataInCodeStart
; }
1121 macho_data_in_code_entry
<P
>* dataInCodeEnd() { return _dataInCodeEnd
; }
1122 const uint8_t* optimizationHintsStart() { return _lohStart
; }
1123 const uint8_t* optimizationHintsEnd() { return _lohEnd
; }
1124 bool hasOptimizationHints() { return _lohStart
!= _lohEnd
; }
1127 void addFixups(const SourceLocation
& src
, ld::Fixup::Kind kind
, const TargetDesc
& target
);
1128 void addFixups(const SourceLocation
& src
, ld::Fixup::Kind kind
, const TargetDesc
& target
, const TargetDesc
& picBase
);
1132 struct LabelAndCFIBreakIterator
{
1133 typedef typename CFISection
<A
>::CFI_Atom_Info CFI_Atom_Info
;
1134 LabelAndCFIBreakIterator(const uint32_t* ssa
, uint32_t ssc
, const pint_t
* cfisa
,
1135 uint32_t cfisc
, bool ols
)
1136 : sortedSymbolIndexes(ssa
), sortedSymbolCount(ssc
), cfiStartsArray(cfisa
),
1137 cfiStartsCount(cfisc
), fileHasOverlappingSymbols(ols
),
1138 newSection(false), cfiIndex(0), symIndex(0) {}
1139 bool next(Parser
<A
>& parser
, const Section
<A
>& sect
, uint32_t sectNum
, pint_t startAddr
, pint_t endAddr
,
1140 pint_t
* addr
, pint_t
* size
, const macho_nlist
<P
>** sym
);
1141 pint_t
peek(Parser
<A
>& parser
, pint_t startAddr
, pint_t endAddr
);
1142 void beginSection() { newSection
= true; symIndex
= 0; }
1144 const uint32_t* const sortedSymbolIndexes
;
1145 const uint32_t sortedSymbolCount
;
1146 const pint_t
* cfiStartsArray
;
1147 const uint32_t cfiStartsCount
;
1148 const bool fileHasOverlappingSymbols
;
1154 struct CFI_CU_InfoArrays
{
1155 typedef typename CFISection
<A
>::CFI_Atom_Info CFI_Atom_Info
;
1156 typedef typename CUSection
<A
>::Info CU_Info
;
1157 CFI_CU_InfoArrays(const CFI_Atom_Info
* cfiAr
, uint32_t cfiC
, CU_Info
* cuAr
, uint32_t cuC
)
1158 : cfiArray(cfiAr
), cuArray(cuAr
), cfiCount(cfiC
), cuCount(cuC
) {}
1159 const CFI_Atom_Info
* const cfiArray
;
1160 CU_Info
* const cuArray
;
1161 const uint32_t cfiCount
;
1162 const uint32_t cuCount
;
1168 friend class Section
<A
>;
1170 enum SectionType
{ sectionTypeIgnore
, sectionTypeLiteral4
, sectionTypeLiteral8
, sectionTypeLiteral16
,
1171 sectionTypeNonLazy
, sectionTypeCFI
, sectionTypeCString
, sectionTypeCStringPointer
,
1172 sectionTypeUTF16Strings
, sectionTypeCFString
, sectionTypeObjC2ClassRefs
, typeObjC2CategoryList
,
1173 sectionTypeObjC1Classes
, sectionTypeSymboled
, sectionTypeObjC1ClassRefs
,
1174 sectionTypeTentativeDefinitions
, sectionTypeAbsoluteSymbols
, sectionTypeTLVDefs
,
1175 sectionTypeCompactUnwind
, sectionTypeTLVPointers
};
1177 template <typename P
>
1178 struct MachOSectionAndSectionClass
1180 const macho_section
<P
>* sect
;
1183 static int sorter(const void* l
, const void* r
) {
1184 const MachOSectionAndSectionClass
<P
>* left
= (MachOSectionAndSectionClass
<P
>*)l
;
1185 const MachOSectionAndSectionClass
<P
>* right
= (MachOSectionAndSectionClass
<P
>*)r
;
1186 int64_t diff
= left
->sect
->addr() - right
->sect
->addr();
1196 struct ParserAndSectionsArray
{ Parser
* parser
; const uint32_t* sortedSectionsArray
; };
1199 Parser(const uint8_t* fileContent
, uint64_t fileLength
,
1200 const char* path
, time_t modTime
, ld::File::Ordinal ordinal
,
1201 bool warnUnwindConversionProblems
, bool keepDwarfUnwind
,
1202 bool forceDwarfConversion
, bool neverConvertDwarf
,
1203 bool verboseOptimizationHints
, bool ignoreMismatchPlatform
);
1204 ld::relocatable::File
* parse(const ParserOptions
& opts
);
1205 static uint8_t loadCommandSizeMask();
1206 bool parseLoadCommands(Options::Platform platform
, uint32_t minOSVersion
, bool simulator
, bool ignoreMismatchPlatform
);
1207 void makeSections();
1208 void prescanSymbolTable();
1209 void makeSortedSymbolsArray(uint32_t symArray
[], const uint32_t sectionArray
[]);
1210 void makeSortedSectionsArray(uint32_t array
[]);
1211 static int pointerSorter(const void* l
, const void* r
);
1212 static int symbolIndexSorter(void* extra
, const void* l
, const void* r
);
1213 static int sectionIndexSorter(void* extra
, const void* l
, const void* r
);
1215 void parseDebugInfo();
1217 void appendAliasAtoms(uint8_t* atomBuffer
);
1218 static bool isConstFunStabs(const char *stabStr
);
1219 bool read_comp_unit(const char ** name
, const char ** comp_dir
,
1220 uint64_t *stmt_list
);
1221 pint_t
realAddr(pint_t addr
);
1222 const char* getDwarfString(uint64_t form
, const uint8_t*& p
);
1223 uint64_t getDwarfOffset(uint64_t form
, const uint8_t*& di
, bool dwarf64
);
1224 bool skip_form(const uint8_t ** offset
, const uint8_t * end
,
1225 uint64_t form
, uint8_t addr_size
, bool dwarf64
);
1228 // filled in by constructor
1229 const uint8_t* _fileContent
;
1230 uint32_t _fileLength
;
1233 ld::File::Ordinal _ordinal
;
1235 // filled in by parseLoadCommands()
1237 const macho_nlist
<P
>* _symbols
;
1238 uint32_t _symbolCount
;
1239 uint32_t _indirectSymbolCount
;
1240 const char* _strings
;
1241 uint32_t _stringsSize
;
1242 const uint32_t* _indirectTable
;
1243 uint32_t _indirectTableCount
;
1244 uint32_t _undefinedStartIndex
;
1245 uint32_t _undefinedEndIndex
;
1246 const macho_section
<P
>* _sectionsStart
;
1247 uint32_t _machOSectionsCount
;
1249 macho_data_in_code_entry
<P
>* _dataInCodeStart
;
1250 macho_data_in_code_entry
<P
>* _dataInCodeEnd
;
1251 const uint8_t* _lohStart
;
1252 const uint8_t* _lohEnd
;
1254 // filled in by parse()
1255 CFISection
<A
>* _EHFrameSection
;
1256 CUSection
<A
>* _compactUnwindSection
;
1257 AbsoluteSymbolSection
<A
>* _absoluteSection
;
1258 uint32_t _tentativeDefinitionCount
;
1259 uint32_t _absoluteSymbolCount
;
1260 uint32_t _symbolsInSections
;
1261 bool _hasLongBranchStubs
;
1262 bool _AppleObjc
; // FSF has objc that uses different data layout
1263 bool _overlappingSymbols
;
1264 bool _warnUnwindConversionProblems
;
1265 bool _hasDataInCodeLabels
;
1266 bool _keepDwarfUnwind
;
1267 bool _forceDwarfConversion
;
1268 bool _neverConvertDwarf
;
1269 bool _verboseOptimizationHints
;
1270 bool _armUsesZeroCostExceptions
;
1271 bool _ignoreMismatchPlatform
;
1272 bool _treateBitcodeAsData
;
1274 uint8_t _maxDefaultCommonAlignment
;
1275 unsigned int _stubsSectionNum
;
1276 const macho_section
<P
>* _stubsMachOSection
;
1277 std::vector
<const char*> _dtraceProviderInfo
;
1278 std::vector
<FixupInAtom
> _allFixups
;
1283 template <typename A
>
1284 Parser
<A
>::Parser(const uint8_t* fileContent
, uint64_t fileLength
, const char* path
, time_t modTime
,
1285 ld::File::Ordinal ordinal
, bool convertDUI
, bool keepDwarfUnwind
, bool forceDwarfConversion
,
1286 bool neverConvertDwarf
, bool verboseOptimizationHints
, bool ignoreMismatchPlatform
)
1287 : _fileContent(fileContent
), _fileLength(fileLength
), _path(path
), _modTime(modTime
),
1288 _ordinal(ordinal
), _file(NULL
),
1289 _symbols(NULL
), _symbolCount(0), _indirectSymbolCount(0), _strings(NULL
), _stringsSize(0),
1290 _indirectTable(NULL
), _indirectTableCount(0),
1291 _undefinedStartIndex(0), _undefinedEndIndex(0),
1292 _sectionsStart(NULL
), _machOSectionsCount(0), _hasUUID(false),
1293 _dataInCodeStart(NULL
), _dataInCodeEnd(NULL
),
1294 _lohStart(NULL
), _lohEnd(NULL
),
1295 _EHFrameSection(NULL
), _compactUnwindSection(NULL
), _absoluteSection(NULL
),
1296 _tentativeDefinitionCount(0), _absoluteSymbolCount(0),
1297 _symbolsInSections(0), _hasLongBranchStubs(false), _AppleObjc(false),
1298 _overlappingSymbols(false), _warnUnwindConversionProblems(convertDUI
), _hasDataInCodeLabels(false),
1299 _keepDwarfUnwind(keepDwarfUnwind
), _forceDwarfConversion(forceDwarfConversion
),
1300 _neverConvertDwarf(neverConvertDwarf
),
1301 _verboseOptimizationHints(verboseOptimizationHints
),
1302 _ignoreMismatchPlatform(ignoreMismatchPlatform
),
1303 _stubsSectionNum(0), _stubsMachOSection(NULL
)
1309 bool Parser
<x86
>::validFile(const uint8_t* fileContent
, bool, cpu_subtype_t
)
1311 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1312 if ( header
->magic() != MH_MAGIC
)
1314 if ( header
->cputype() != CPU_TYPE_I386
)
1316 if ( header
->filetype() != MH_OBJECT
)
1322 bool Parser
<x86_64
>::validFile(const uint8_t* fileContent
, bool, cpu_subtype_t
)
1324 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1325 if ( header
->magic() != MH_MAGIC_64
)
1327 if ( header
->cputype() != CPU_TYPE_X86_64
)
1329 if ( header
->filetype() != MH_OBJECT
)
1335 bool Parser
<arm
>::validFile(const uint8_t* fileContent
, bool subtypeMustMatch
, cpu_subtype_t subtype
)
1337 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1338 if ( header
->magic() != MH_MAGIC
)
1340 if ( header
->cputype() != CPU_TYPE_ARM
)
1342 if ( header
->filetype() != MH_OBJECT
)
1344 if ( subtypeMustMatch
) {
1345 if ( (cpu_subtype_t
)header
->cpusubtype() == subtype
)
1347 // hack until libcc_kext.a is made fat
1348 if ( header
->cpusubtype() == CPU_SUBTYPE_ARM_ALL
)
1357 bool Parser
<arm64
>::validFile(const uint8_t* fileContent
, bool subtypeMustMatch
, cpu_subtype_t subtype
)
1359 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1360 if ( header
->magic() != MH_MAGIC_64
)
1362 if ( header
->cputype() != CPU_TYPE_ARM64
)
1364 if ( header
->filetype() != MH_OBJECT
)
1371 const char* Parser
<x86
>::fileKind(const uint8_t* fileContent
)
1373 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1374 if ( header
->magic() != MH_MAGIC
)
1376 if ( header
->cputype() != CPU_TYPE_I386
)
1382 const char* Parser
<x86_64
>::fileKind(const uint8_t* fileContent
)
1384 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1385 if ( header
->magic() != MH_MAGIC_64
)
1387 if ( header
->cputype() != CPU_TYPE_X86_64
)
1393 const char* Parser
<arm
>::fileKind(const uint8_t* fileContent
)
1395 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1396 if ( header
->magic() != MH_MAGIC
)
1398 if ( header
->cputype() != CPU_TYPE_ARM
)
1400 for (const ArchInfo
* t
=archInfoArray
; t
->archName
!= NULL
; ++t
) {
1401 if ( (t
->cpuType
== CPU_TYPE_ARM
) && ((cpu_subtype_t
)header
->cpusubtype() == t
->cpuSubType
) ) {
1408 #if SUPPORT_ARCH_arm64
1410 const char* Parser
<arm64
>::fileKind(const uint8_t* fileContent
)
1412 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1413 if ( header
->magic() != MH_MAGIC_64
)
1415 if ( header
->cputype() != CPU_TYPE_ARM64
)
1422 template <typename A
>
1423 bool Parser
<A
>::hasObjC2Categories(const uint8_t* fileContent
)
1425 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1426 const uint32_t cmd_count
= header
->ncmds();
1427 const macho_load_command
<P
>* const cmds
= (macho_load_command
<P
>*)((char*)header
+ sizeof(macho_header
<P
>));
1428 const macho_load_command
<P
>* const cmdsEnd
= (macho_load_command
<P
>*)((char*)header
+ sizeof(macho_header
<P
>) + header
->sizeofcmds());
1429 const macho_load_command
<P
>* cmd
= cmds
;
1430 for (uint32_t i
= 0; i
< cmd_count
; ++i
) {
1431 if ( cmd
->cmd() == macho_segment_command
<P
>::CMD
) {
1432 const macho_segment_command
<P
>* segment
= (macho_segment_command
<P
>*)cmd
;
1433 const macho_section
<P
>* sectionsStart
= (macho_section
<P
>*)((char*)segment
+ sizeof(macho_segment_command
<P
>));
1434 for (uint32_t si
=0; si
< segment
->nsects(); ++si
) {
1435 const macho_section
<P
>* sect
= §ionsStart
[si
];
1436 if ( (sect
->size() > 0)
1437 && (strcmp(sect
->sectname(), "__objc_catlist") == 0)
1438 && (strcmp(sect
->segname(), "__DATA") == 0) ) {
1443 cmd
= (const macho_load_command
<P
>*)(((char*)cmd
)+cmd
->cmdsize());
1444 if ( cmd
> cmdsEnd
)
1445 throwf("malformed mach-o file, load command #%d is outside size of load commands", i
);
1451 template <typename A
>
1452 bool Parser
<A
>::hasObjC1Categories(const uint8_t* fileContent
)
1454 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1455 const uint32_t cmd_count
= header
->ncmds();
1456 const macho_load_command
<P
>* const cmds
= (macho_load_command
<P
>*)((char*)header
+ sizeof(macho_header
<P
>));
1457 const macho_load_command
<P
>* const cmdsEnd
= (macho_load_command
<P
>*)((char*)header
+ sizeof(macho_header
<P
>) + header
->sizeofcmds());
1458 const macho_load_command
<P
>* cmd
= cmds
;
1459 for (uint32_t i
= 0; i
< cmd_count
; ++i
) {
1460 if ( cmd
->cmd() == macho_segment_command
<P
>::CMD
) {
1461 const macho_segment_command
<P
>* segment
= (macho_segment_command
<P
>*)cmd
;
1462 const macho_section
<P
>* sectionsStart
= (macho_section
<P
>*)((char*)segment
+ sizeof(macho_segment_command
<P
>));
1463 for (uint32_t si
=0; si
< segment
->nsects(); ++si
) {
1464 const macho_section
<P
>* sect
= §ionsStart
[si
];
1465 if ( (sect
->size() > 0)
1466 && (strcmp(sect
->sectname(), "__category") == 0)
1467 && (strcmp(sect
->segname(), "__OBJC") == 0) ) {
1472 cmd
= (const macho_load_command
<P
>*)(((char*)cmd
)+cmd
->cmdsize());
1473 if ( cmd
> cmdsEnd
)
1474 throwf("malformed mach-o file, load command #%d is outside size of load commands", i
);
1480 template <typename A
>
1481 bool Parser
<A
>::getNonLocalSymbols(const uint8_t* fileContent
, std::vector
<const char*> &syms
)
1483 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1484 const uint32_t cmd_count
= header
->ncmds();
1485 const macho_load_command
<P
>* const cmds
= (macho_load_command
<P
>*)((char*)header
+ sizeof(macho_header
<P
>));
1486 const macho_load_command
<P
>* const cmdsEnd
= (macho_load_command
<P
>*)((char*)header
+ sizeof(macho_header
<P
>) + header
->sizeofcmds());
1487 const macho_load_command
<P
>* cmd
= cmds
;
1488 for (uint32_t i
= 0; i
< cmd_count
; ++i
) {
1489 if ( cmd
->cmd() == LC_SYMTAB
) {
1490 const macho_symtab_command
<P
>* symtab
= (macho_symtab_command
<P
>*)cmd
;
1491 uint32_t symbolCount
= symtab
->nsyms();
1492 const macho_nlist
<P
>* symbols
= (const macho_nlist
<P
>*)(fileContent
+ symtab
->symoff());
1493 const char* strings
= (char*)fileContent
+ symtab
->stroff();
1494 for (uint32_t j
= 0; j
< symbolCount
; ++j
) {
1495 // ignore stabs and count only ext symbols
1496 if ( (symbols
[j
].n_type() & N_STAB
) == 0 &&
1497 (symbols
[j
].n_type() & N_EXT
) != 0 ) {
1498 const char* symName
= &strings
[symbols
[j
].n_strx()];
1499 syms
.push_back(symName
);
1504 cmd
= (const macho_load_command
<P
>*)(((char*)cmd
)+cmd
->cmdsize());
1505 if ( cmd
> cmdsEnd
)
1506 throwf("malformed mach-o file, load command #%d is outside size of load commands", i
);
1512 template <typename A
>
1513 int Parser
<A
>::pointerSorter(const void* l
, const void* r
)
1515 // sort references by address
1516 const pint_t
* left
= (pint_t
*)l
;
1517 const pint_t
* right
= (pint_t
*)r
;
1518 return (*left
- *right
);
1521 template <typename A
>
1522 typename
A::P::uint_t Parser
<A
>::LabelAndCFIBreakIterator::peek(Parser
<A
>& parser
, pint_t startAddr
, pint_t endAddr
)
1525 if ( symIndex
< sortedSymbolCount
)
1526 symbolAddr
= parser
.symbolFromIndex(sortedSymbolIndexes
[symIndex
]).n_value();
1528 symbolAddr
= endAddr
;
1530 if ( cfiIndex
< cfiStartsCount
)
1531 cfiAddr
= cfiStartsArray
[cfiIndex
];
1534 if ( (cfiAddr
< symbolAddr
) && (cfiAddr
>= startAddr
) ) {
1535 if ( cfiAddr
< endAddr
)
1541 if ( symbolAddr
< endAddr
)
1549 // Parses up a section into chunks based on labels and CFI information.
1550 // Each call returns the next chunk address and size, and (if the break
1551 // was becuase of a label, the symbol). Returns false when no more chunks.
1553 template <typename A
>
1554 bool Parser
<A
>::LabelAndCFIBreakIterator::next(Parser
<A
>& parser
, const Section
<A
>& sect
, uint32_t sectNum
, pint_t startAddr
, pint_t endAddr
,
1555 pint_t
* addr
, pint_t
* size
, const macho_nlist
<P
>** symbol
)
1557 bool cfiApplicable
= (sect
.machoSection()->flags() & (S_ATTR_PURE_INSTRUCTIONS
| S_ATTR_SOME_INSTRUCTIONS
));
1558 // may not be a label on start of section, but need atom demarcation there
1561 // advance symIndex until we get to the first label at or past the start of this section
1562 while ( symIndex
< sortedSymbolCount
) {
1563 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(sortedSymbolIndexes
[symIndex
]);
1564 if ( ! sect
.ignoreLabel(parser
.nameFromSymbol(sym
)) ) {
1565 pint_t nextSymbolAddr
= sym
.n_value();
1566 //fprintf(stderr, "sectNum=%d, nextSymbolAddr=0x%08llX, name=%s\n", sectNum, (uint64_t)nextSymbolAddr, parser.nameFromSymbol(sym));
1567 if ( (nextSymbolAddr
> startAddr
) || ((nextSymbolAddr
== startAddr
) && (sym
.n_sect() == sectNum
)) )
1572 if ( symIndex
< sortedSymbolCount
) {
1573 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(sortedSymbolIndexes
[symIndex
]);
1574 pint_t nextSymbolAddr
= sym
.n_value();
1575 // if next symbol found is not in this section
1576 if ( sym
.n_sect() != sectNum
) {
1577 // check for CFI break instead of symbol break
1578 if ( cfiIndex
< cfiStartsCount
) {
1579 pint_t nextCfiAddr
= cfiStartsArray
[cfiIndex
];
1580 if ( nextCfiAddr
< endAddr
) {
1583 *addr
= nextCfiAddr
;
1584 *size
= peek(parser
, startAddr
, endAddr
) - nextCfiAddr
;
1590 *size
= endAddr
- startAddr
;
1592 if ( startAddr
== endAddr
)
1593 return false; // zero size section
1595 return true; // whole section is one atom with no label
1597 // if also CFI break here, eat it
1598 if ( cfiIndex
< cfiStartsCount
) {
1599 if ( cfiStartsArray
[cfiIndex
] == nextSymbolAddr
)
1602 if ( nextSymbolAddr
== startAddr
) {
1603 // label at start of section, return it as chunk
1606 *size
= peek(parser
, startAddr
, endAddr
) - startAddr
;
1610 // return chunk before first symbol
1612 *size
= nextSymbolAddr
- startAddr
;
1616 // no symbols in section, check CFI
1617 if ( cfiApplicable
&& (cfiIndex
< cfiStartsCount
) ) {
1618 pint_t nextCfiAddr
= cfiStartsArray
[cfiIndex
];
1619 if ( nextCfiAddr
< endAddr
) {
1622 *addr
= nextCfiAddr
;
1623 *size
= peek(parser
, startAddr
, endAddr
) - nextCfiAddr
;
1628 // no cfi, so whole section is one chunk
1630 *size
= endAddr
- startAddr
;
1632 if ( startAddr
== endAddr
)
1633 return false; // zero size section
1635 return true; // whole section is one atom with no label
1638 while ( (symIndex
< sortedSymbolCount
) && (cfiIndex
< cfiStartsCount
) ) {
1639 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(sortedSymbolIndexes
[symIndex
]);
1640 pint_t nextSymbolAddr
= sym
.n_value();
1641 pint_t nextCfiAddr
= cfiStartsArray
[cfiIndex
];
1642 if ( nextSymbolAddr
< nextCfiAddr
) {
1643 if ( nextSymbolAddr
>= endAddr
)
1646 if ( nextSymbolAddr
< startAddr
)
1648 *addr
= nextSymbolAddr
;
1649 *size
= peek(parser
, startAddr
, endAddr
) - nextSymbolAddr
;
1653 else if ( nextCfiAddr
< nextSymbolAddr
) {
1654 if ( nextCfiAddr
>= endAddr
)
1657 if ( nextCfiAddr
< startAddr
)
1659 *addr
= nextCfiAddr
;
1660 *size
= peek(parser
, startAddr
, endAddr
) - nextCfiAddr
;
1665 if ( nextCfiAddr
>= endAddr
)
1669 if ( nextCfiAddr
< startAddr
)
1671 *addr
= nextCfiAddr
;
1672 *size
= peek(parser
, startAddr
, endAddr
) - nextCfiAddr
;
1677 while ( symIndex
< sortedSymbolCount
) {
1678 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(sortedSymbolIndexes
[symIndex
]);
1679 pint_t nextSymbolAddr
= sym
.n_value();
1680 // if next symbol found is not in this section, then done with iteration
1681 if ( sym
.n_sect() != sectNum
)
1684 if ( nextSymbolAddr
< startAddr
)
1686 *addr
= nextSymbolAddr
;
1687 *size
= peek(parser
, startAddr
, endAddr
) - nextSymbolAddr
;
1691 while ( cfiIndex
< cfiStartsCount
) {
1692 pint_t nextCfiAddr
= cfiStartsArray
[cfiIndex
];
1693 if ( nextCfiAddr
>= endAddr
)
1696 if ( nextCfiAddr
< startAddr
)
1698 *addr
= nextCfiAddr
;
1699 *size
= peek(parser
, startAddr
, endAddr
) - nextCfiAddr
;
1707 typename
arm::P::uint_t Parser
<arm
>::realAddr(typename
arm::P::uint_t addr
)
1712 template <typename A
>
1713 typename
A::P::uint_t Parser
<A
>::realAddr(typename
A::P::uint_t addr
)
1718 #define STACK_ALLOC_IF_SMALL(_type, _name, _actual_count, _maxCount) \
1719 _type* _name = NULL; \
1720 uint32_t _name##_count = 1; \
1721 uint32_t _name##_stack_count = _actual_count; \
1722 if ( _actual_count > _maxCount ) { \
1723 _name = (_type*)malloc(sizeof(_type) * _actual_count); \
1724 _name##_stack_count = 1; \
1727 _name##_count = _actual_count; \
1728 _type _name##_buffer[_name##_stack_count]; \
1729 if ( _name == NULL ) \
1730 _name = _name##_buffer;
1733 template <typename A
>
1734 ld::relocatable::File
* Parser
<A
>::parse(const ParserOptions
& opts
)
1736 // create file object
1737 _file
= new File
<A
>(_path
, _modTime
, _fileContent
, _ordinal
);
1740 _file
->_srcKind
= opts
.srcKind
;
1741 // set treatBitcodeAsData
1742 _treateBitcodeAsData
= opts
.treateBitcodeAsData
;
1743 _usingBitcode
= opts
.usingBitcode
;
1745 // respond to -t option
1746 if ( opts
.logAllFiles
)
1747 printf("%s\n", _path
);
1749 _armUsesZeroCostExceptions
= opts
.armUsesZeroCostExceptions
;
1750 _maxDefaultCommonAlignment
= opts
.maxDefaultCommonAlignment
;
1752 // parse start of mach-o file
1753 if ( ! parseLoadCommands(opts
.platform
, opts
.minOSVersion
, opts
.simulator
, opts
.ignoreMismatchPlatform
) )
1757 uint32_t sortedSectionIndexes
[_machOSectionsCount
];
1758 this->makeSortedSectionsArray(sortedSectionIndexes
);
1760 // make symbol table sorted by address
1761 this->prescanSymbolTable();
1762 uint32_t sortedSymbolIndexes
[_symbolsInSections
];
1763 this->makeSortedSymbolsArray(sortedSymbolIndexes
, sortedSectionIndexes
);
1765 // allocate Section<A> object for each mach-o section
1768 // if it exists, do special early parsing of __compact_unwind section
1769 uint32_t countOfCUs
= 0;
1770 if ( _compactUnwindSection
!= NULL
)
1771 countOfCUs
= _compactUnwindSection
->count();
1772 // stack allocate (if not too large) cuInfoBuffer
1773 STACK_ALLOC_IF_SMALL(typename CUSection
<A
>::Info
, cuInfoArray
, countOfCUs
, 1024);
1774 if ( countOfCUs
!= 0 )
1775 _compactUnwindSection
->parse(*this, countOfCUs
, cuInfoArray
);
1777 // create lists of address that already have compact unwind and thus don't need the dwarf parsed
1778 unsigned cuLsdaCount
= 0;
1779 STACK_ALLOC_IF_SMALL(pint_t
, cuStarts
, countOfCUs
, 1024);
1780 for (uint32_t i
=0; i
< countOfCUs
; ++i
) {
1781 if ( CUSection
<A
>::encodingMeansUseDwarf(cuInfoArray
[i
].compactUnwindInfo
) )
1784 cuStarts
[i
] = cuInfoArray
[i
].functionStartAddress
;
1785 if ( cuInfoArray
[i
].lsdaAddress
!= 0 )
1790 // if it exists, do special early parsing of __eh_frame section
1791 // stack allocate (if not too large) array of CFI_Atom_Info
1792 uint32_t countOfCFIs
= 0;
1793 if ( _EHFrameSection
!= NULL
)
1794 countOfCFIs
= _EHFrameSection
->cfiCount(*this);
1795 STACK_ALLOC_IF_SMALL(typename CFISection
<A
>::CFI_Atom_Info
, cfiArray
, countOfCFIs
, 1024);
1797 // stack allocate (if not too large) a copy of __eh_frame to apply relocations to
1798 uint32_t sectSize
= 4;
1799 if ( (countOfCFIs
!= 0) && _EHFrameSection
->needsRelocating() )
1800 sectSize
= _EHFrameSection
->machoSection()->size()+4;
1801 STACK_ALLOC_IF_SMALL(uint8_t, ehBuffer
, sectSize
, 50*1024);
1802 uint32_t cfiStartsCount
= 0;
1803 if ( countOfCFIs
!= 0 ) {
1804 _EHFrameSection
->cfiParse(*this, ehBuffer
, cfiArray
, countOfCFIs
, cuStarts
, countOfCUs
);
1805 // count functions and lsdas
1806 for(uint32_t i
=0; i
< countOfCFIs
; ++i
) {
1807 if ( cfiArray
[i
].isCIE
)
1809 //fprintf(stderr, "cfiArray[i].func = 0x%08llX, cfiArray[i].lsda = 0x%08llX, encoding=0x%08X\n",
1810 // (uint64_t)cfiArray[i].u.fdeInfo.function.targetAddress,
1811 // (uint64_t)cfiArray[i].u.fdeInfo.lsda.targetAddress,
1812 // cfiArray[i].u.fdeInfo.compactUnwindInfo);
1813 if ( cfiArray
[i
].u
.fdeInfo
.function
.targetAddress
!= CFI_INVALID_ADDRESS
)
1815 if ( cfiArray
[i
].u
.fdeInfo
.lsda
.targetAddress
!= CFI_INVALID_ADDRESS
)
1819 CFI_CU_InfoArrays
cfis(cfiArray
, countOfCFIs
, cuInfoArray
, countOfCUs
);
1821 // create sorted array of function starts and lsda starts
1822 pint_t cfiStartsArray
[cfiStartsCount
+cuLsdaCount
];
1823 uint32_t countOfFDEs
= 0;
1824 uint32_t cfiStartsArrayCount
= 0;
1825 if ( countOfCFIs
!= 0 ) {
1826 for(uint32_t i
=0; i
< countOfCFIs
; ++i
) {
1827 if ( cfiArray
[i
].isCIE
)
1829 if ( cfiArray
[i
].u
.fdeInfo
.function
.targetAddress
!= CFI_INVALID_ADDRESS
)
1830 cfiStartsArray
[cfiStartsArrayCount
++] = realAddr(cfiArray
[i
].u
.fdeInfo
.function
.targetAddress
);
1831 if ( cfiArray
[i
].u
.fdeInfo
.lsda
.targetAddress
!= CFI_INVALID_ADDRESS
)
1832 cfiStartsArray
[cfiStartsArrayCount
++] = cfiArray
[i
].u
.fdeInfo
.lsda
.targetAddress
;
1836 if ( cuLsdaCount
!= 0 ) {
1837 // merge in an lsda info from compact unwind
1838 for (uint32_t i
=0; i
< countOfCUs
; ++i
) {
1839 if ( cuInfoArray
[i
].lsdaAddress
== 0 )
1841 // append to cfiStartsArray if not already in that list
1843 for(uint32_t j
=0; j
< cfiStartsArrayCount
; ++j
) {
1844 if ( cfiStartsArray
[j
] == cuInfoArray
[i
].lsdaAddress
)
1848 cfiStartsArray
[cfiStartsArrayCount
++] = cuInfoArray
[i
].lsdaAddress
;
1852 if ( cfiStartsArrayCount
!= 0 ) {
1853 ::qsort(cfiStartsArray
, cfiStartsArrayCount
, sizeof(pint_t
), pointerSorter
);
1855 // scan for FDEs claming the same function
1856 for(uint32_t i
=1; i
< cfiStartsArrayCount
; ++i
) {
1857 assert( cfiStartsArray
[i
] != cfiStartsArray
[i
-1] );
1862 Section
<A
>** sections
= _file
->_sectionsArray
;
1863 uint32_t sectionsCount
= _file
->_sectionsArrayCount
;
1865 // figure out how many atoms will be allocated and allocate
1866 LabelAndCFIBreakIterator
breakIterator(sortedSymbolIndexes
, _symbolsInSections
, cfiStartsArray
,
1867 cfiStartsArrayCount
, _overlappingSymbols
);
1868 uint32_t computedAtomCount
= 0;
1869 for (uint32_t i
=0; i
< sectionsCount
; ++i
) {
1870 breakIterator
.beginSection();
1871 uint32_t count
= sections
[i
]->computeAtomCount(*this, breakIterator
, cfis
);
1872 //const macho_section<P>* sect = sections[i]->machoSection();
1873 //fprintf(stderr, "computed count=%u for section %s size=%llu\n", count, sect->sectname(), (sect != NULL) ? sect->size() : 0);
1874 computedAtomCount
+= count
;
1876 //fprintf(stderr, "allocating %d atoms * sizeof(Atom<A>)=%ld, sizeof(ld::Atom)=%ld\n", computedAtomCount, sizeof(Atom<A>), sizeof(ld::Atom));
1877 _file
->_atomsArray
= new uint8_t[computedAtomCount
*sizeof(Atom
<A
>)];
1878 _file
->_atomsArrayCount
= 0;
1880 // have each section append atoms to _atomsArray
1881 LabelAndCFIBreakIterator
breakIterator2(sortedSymbolIndexes
, _symbolsInSections
, cfiStartsArray
,
1882 cfiStartsArrayCount
, _overlappingSymbols
);
1883 for (uint32_t i
=0; i
< sectionsCount
; ++i
) {
1884 uint8_t* atoms
= _file
->_atomsArray
+ _file
->_atomsArrayCount
*sizeof(Atom
<A
>);
1885 breakIterator2
.beginSection();
1886 uint32_t count
= sections
[i
]->appendAtoms(*this, atoms
, breakIterator2
, cfis
);
1887 //fprintf(stderr, "append count=%u for section %s/%s\n", count, sections[i]->machoSection()->segname(), sections[i]->machoSection()->sectname());
1888 _file
->_atomsArrayCount
+= count
;
1890 assert( _file
->_atomsArrayCount
== computedAtomCount
&& "more atoms allocated than expected");
1893 // have each section add all fix-ups for its atoms
1894 _allFixups
.reserve(computedAtomCount
*5);
1895 for (uint32_t i
=0; i
< sectionsCount
; ++i
)
1896 sections
[i
]->makeFixups(*this, cfis
);
1898 // assign fixups start offset for each atom
1899 uint8_t* p
= _file
->_atomsArray
;
1900 uint32_t fixupOffset
= 0;
1901 for(int i
=_file
->_atomsArrayCount
; i
> 0; --i
) {
1902 Atom
<A
>* atom
= (Atom
<A
>*)p
;
1903 atom
->_fixupsStartIndex
= fixupOffset
;
1904 fixupOffset
+= atom
->_fixupsCount
;
1905 atom
->_fixupsCount
= 0;
1906 p
+= sizeof(Atom
<A
>);
1908 assert(fixupOffset
== _allFixups
.size());
1909 _file
->_fixups
.resize(fixupOffset
);
1911 // copy each fixup for each atom
1912 for(typename
std::vector
<FixupInAtom
>::iterator it
=_allFixups
.begin(); it
!= _allFixups
.end(); ++it
) {
1913 uint32_t slot
= it
->atom
->_fixupsStartIndex
+ it
->atom
->_fixupsCount
;
1914 _file
->_fixups
[slot
] = it
->fixup
;
1915 it
->atom
->_fixupsCount
++;
1918 // done with temp vector
1922 _file
->_unwindInfos
.reserve(countOfFDEs
+countOfCUs
);
1923 for(uint32_t i
=0; i
< countOfCFIs
; ++i
) {
1924 if ( cfiArray
[i
].isCIE
)
1926 if ( cfiArray
[i
].u
.fdeInfo
.function
.targetAddress
!= CFI_INVALID_ADDRESS
) {
1927 ld::Atom::UnwindInfo info
;
1928 info
.startOffset
= 0;
1929 info
.unwindInfo
= cfiArray
[i
].u
.fdeInfo
.compactUnwindInfo
;
1930 _file
->_unwindInfos
.push_back(info
);
1931 Atom
<A
>* func
= findAtomByAddress(cfiArray
[i
].u
.fdeInfo
.function
.targetAddress
);
1932 func
->setUnwindInfoRange(_file
->_unwindInfos
.size()-1, 1);
1933 //fprintf(stderr, "cu from dwarf =0x%08X, atom=%s\n", info.unwindInfo, func->name());
1936 // apply compact infos in __LD,__compact_unwind section to each function
1937 // if function also has dwarf unwind, CU will override it
1938 Atom
<A
>* lastFunc
= NULL
;
1939 uint32_t lastEnd
= 0;
1940 for(uint32_t i
=0; i
< countOfCUs
; ++i
) {
1941 typename CUSection
<A
>::Info
* info
= &cuInfoArray
[i
];
1942 assert(info
->function
!= NULL
);
1943 ld::Atom::UnwindInfo ui
;
1944 ui
.startOffset
= info
->functionStartAddress
- info
->function
->objectAddress();
1945 ui
.unwindInfo
= info
->compactUnwindInfo
;
1946 _file
->_unwindInfos
.push_back(ui
);
1947 // don't override with converted cu with "use dwarf" cu, if forcing dwarf conversion
1948 if ( !_forceDwarfConversion
|| !CUSection
<A
>::encodingMeansUseDwarf(info
->compactUnwindInfo
) ) {
1949 //fprintf(stderr, "cu=0x%08X, atom=%s\n", ui.unwindInfo, info->function->name());
1950 // if previous is for same function, extend range
1951 if ( info
->function
== lastFunc
) {
1952 if ( lastEnd
!= ui
.startOffset
) {
1953 if ( lastEnd
< ui
.startOffset
)
1954 warning("__LD,__compact_unwind entries for %s have a gap at offset 0x%0X", info
->function
->name(), lastEnd
);
1956 warning("__LD,__compact_unwind entries for %s overlap at offset 0x%0X", info
->function
->name(), lastEnd
);
1958 lastFunc
->extendUnwindInfoRange();
1961 info
->function
->setUnwindInfoRange(_file
->_unwindInfos
.size()-1, 1);
1962 lastFunc
= info
->function
;
1963 lastEnd
= ui
.startOffset
+ info
->rangeLength
;
1967 // process indirect symbols which become AliasAtoms
1968 _file
->_aliasAtomsArray
= NULL
;
1969 _file
->_aliasAtomsArrayCount
= 0;
1970 if ( _indirectSymbolCount
!= 0 ) {
1971 _file
->_aliasAtomsArrayCount
= _indirectSymbolCount
;
1972 _file
->_aliasAtomsArray
= new uint8_t[_file
->_aliasAtomsArrayCount
*sizeof(AliasAtom
)];
1973 this->appendAliasAtoms(_file
->_aliasAtomsArray
);
1977 // parse dwarf debug info to get line info
1978 this->parseDebugInfo();
1983 static void versionToString(uint32_t value
, char buffer
[32])
1986 sprintf(buffer
, "%d.%d.%d", value
>> 16, (value
>> 8) & 0xFF, value
& 0xFF);
1988 sprintf(buffer
, "%d.%d", value
>> 16, (value
>> 8) & 0xFF);
1991 template <> uint8_t Parser
<x86
>::loadCommandSizeMask() { return 0x03; }
1992 template <> uint8_t Parser
<x86_64
>::loadCommandSizeMask() { return 0x07; }
1993 template <> uint8_t Parser
<arm
>::loadCommandSizeMask() { return 0x03; }
1994 template <> uint8_t Parser
<arm64
>::loadCommandSizeMask() { return 0x07; }
1996 template <typename A
>
1997 bool Parser
<A
>::parseLoadCommands(Options::Platform platform
, uint32_t linkMinOSVersion
, bool simulator
, bool ignoreMismatchPlatform
)
1999 const macho_header
<P
>* header
= (const macho_header
<P
>*)_fileContent
;
2001 // set File attributes
2002 _file
->_canScatterAtoms
= (header
->flags() & MH_SUBSECTIONS_VIA_SYMBOLS
);
2003 _file
->_cpuSubType
= header
->cpusubtype();
2005 const macho_segment_command
<P
>* segment
= NULL
;
2006 const uint8_t* const endOfFile
= _fileContent
+ _fileLength
;
2007 const uint32_t cmd_count
= header
->ncmds();
2008 // <rdar://problem/5394172> an empty .o file with zero load commands will crash linker
2009 if ( cmd_count
== 0 )
2011 Options::Platform lcPlatform
= Options::kPlatformUnknown
;
2012 const macho_load_command
<P
>* const cmds
= (macho_load_command
<P
>*)((char*)header
+ sizeof(macho_header
<P
>));
2013 const macho_load_command
<P
>* const cmdsEnd
= (macho_load_command
<P
>*)((char*)header
+ sizeof(macho_header
<P
>) + header
->sizeofcmds());
2014 const macho_load_command
<P
>* cmd
= cmds
;
2015 for (uint32_t i
= 0; i
< cmd_count
; ++i
) {
2016 uint32_t size
= cmd
->cmdsize();
2017 if ( (size
& this->loadCommandSizeMask()) != 0 )
2018 throwf("load command #%d has a unaligned size", i
);
2019 const uint8_t* endOfCmd
= ((uint8_t*)cmd
)+cmd
->cmdsize();
2020 if ( endOfCmd
> (uint8_t*)cmdsEnd
)
2021 throwf("load command #%d extends beyond the end of the load commands", i
);
2022 if ( endOfCmd
> endOfFile
)
2023 throwf("load command #%d extends beyond the end of the file", i
);
2024 switch (cmd
->cmd()) {
2027 const macho_symtab_command
<P
>* symtab
= (macho_symtab_command
<P
>*)cmd
;
2028 _symbolCount
= symtab
->nsyms();
2029 _symbols
= (const macho_nlist
<P
>*)(_fileContent
+ symtab
->symoff());
2030 _strings
= (char*)_fileContent
+ symtab
->stroff();
2031 _stringsSize
= symtab
->strsize();
2032 if ( (symtab
->symoff() + _symbolCount
*sizeof(macho_nlist
<P
>)) > _fileLength
)
2033 throw "mach-o symbol table extends beyond end of file";
2034 if ( (_strings
+ _stringsSize
) > (char*)endOfFile
)
2035 throw "mach-o string pool extends beyond end of file";
2036 if ( _indirectTable
== NULL
) {
2037 if ( _undefinedEndIndex
== 0 ) {
2038 _undefinedStartIndex
= 0;
2039 _undefinedEndIndex
= symtab
->nsyms();
2046 const macho_dysymtab_command
<P
>* dsymtab
= (macho_dysymtab_command
<P
>*)cmd
;
2047 _indirectTable
= (uint32_t*)(_fileContent
+ dsymtab
->indirectsymoff());
2048 _indirectTableCount
= dsymtab
->nindirectsyms();
2049 if ( &_indirectTable
[_indirectTableCount
] > (uint32_t*)endOfFile
)
2050 throw "indirect symbol table extends beyond end of file";
2051 _undefinedStartIndex
= dsymtab
->iundefsym();
2052 _undefinedEndIndex
= _undefinedStartIndex
+ dsymtab
->nundefsym();
2058 case LC_DATA_IN_CODE
:
2060 const macho_linkedit_data_command
<P
>* dc
= (macho_linkedit_data_command
<P
>*)cmd
;
2061 _dataInCodeStart
= (macho_data_in_code_entry
<P
>*)(_fileContent
+ dc
->dataoff());
2062 _dataInCodeEnd
= (macho_data_in_code_entry
<P
>*)(_fileContent
+ dc
->dataoff() + dc
->datasize());
2063 if ( _dataInCodeEnd
> (macho_data_in_code_entry
<P
>*)endOfFile
)
2064 throw "LC_DATA_IN_CODE table extends beyond end of file";
2067 case LC_LINKER_OPTION
:
2069 const macho_linker_option_command
<P
>* loc
= (macho_linker_option_command
<P
>*)cmd
;
2070 const char* buffer
= loc
->buffer();
2071 _file
->_linkerOptions
.resize(_file
->_linkerOptions
.size() + 1);
2072 std::vector
<const char*>& vec
= _file
->_linkerOptions
.back();
2073 for (uint32_t j
=0; j
< loc
->count(); ++j
) {
2074 vec
.push_back(buffer
);
2075 buffer
+= strlen(buffer
) + 1;
2077 if ( buffer
> ((char*)cmd
+ loc
->cmdsize()) )
2078 throw "malformed LC_LINKER_OPTION";
2081 case LC_LINKER_OPTIMIZATION_HINTS
:
2083 const macho_linkedit_data_command
<P
>* loh
= (macho_linkedit_data_command
<P
>*)cmd
;
2084 _lohStart
= _fileContent
+ loh
->dataoff();
2085 _lohEnd
= _fileContent
+ loh
->dataoff() + loh
->datasize();
2086 if ( _lohEnd
> endOfFile
)
2087 throw "LC_LINKER_OPTIMIZATION_HINTS table extends beyond end of file";
2090 case LC_VERSION_MIN_MACOSX
:
2091 case LC_VERSION_MIN_IPHONEOS
:
2092 case LC_VERSION_MIN_WATCHOS
:
2093 #if SUPPORT_APPLE_TV
2094 case LC_VERSION_MIN_TVOS
:
2096 if ( ignoreMismatchPlatform
)
2098 lcPlatform
= Options::platformForLoadCommand(cmd
->cmd());
2099 _file
->_platform
= lcPlatform
;
2100 _file
->_minOSVersion
= ((macho_version_min_command
<P
>*)cmd
)->version();
2102 case LC_BUILD_VERSION
:
2104 const macho_build_version_command
<P
>* buildVersCmd
= (macho_build_version_command
<P
>*)cmd
;
2105 if ( ignoreMismatchPlatform
)
2107 lcPlatform
= (Options::Platform
)buildVersCmd
->platform();
2108 _file
->_platform
= lcPlatform
;
2109 _file
->_minOSVersion
= buildVersCmd
->minos();
2110 const macho_build_tool_version
<P
>* entry
= (macho_build_tool_version
<P
>*)((uint8_t*)cmd
+ sizeof(macho_build_version_command
<P
>));
2111 for (uint32_t t
=0; t
< buildVersCmd
->ntools(); ++t
) {
2112 _file
->_toolVersions
.push_back(std::make_pair(entry
->tool(), entry
->version()));
2117 case macho_segment_command
<P
>::CMD
:
2118 if ( segment
!= NULL
)
2119 throw "more than one LC_SEGMENT found in object file";
2120 segment
= (macho_segment_command
<P
>*)cmd
;
2123 // ignore unknown load commands
2126 cmd
= (const macho_load_command
<P
>*)(((char*)cmd
)+cmd
->cmdsize());
2127 if ( cmd
> cmdsEnd
)
2128 throwf("malformed mach-o file, load command #%d is outside size of load commands", i
);
2130 // arm/arm64 objects are default to ios platform if not set.
2131 // rdar://problem/21746314
2132 if (lcPlatform
== Options::kPlatformUnknown
&&
2133 (std::is_same
<A
, arm
>::value
|| std::is_same
<A
, arm64
>::value
))
2134 lcPlatform
= Options::kPlatformiOS
;
2136 // Check platform cross-linking.
2137 if ( !ignoreMismatchPlatform
) {
2138 if ( lcPlatform
!= platform
) {
2140 case Options::kPlatformOSX
:
2141 case Options::kPlatformiOS
:
2142 if ( lcPlatform
== Options::kPlatformUnknown
)
2144 // fall through if the Platform is not Unknown
2145 case Options::kPlatform_bridgeOS
:
2146 case Options::kPlatformWatchOS
:
2147 // Error when using bitcocde, warning otherwise.
2149 throwf("building for %s%s, but linking in object file built for %s,",
2150 Options::platformName(platform
), (simulator
? " simulator" : ""),
2151 Options::platformName(lcPlatform
));
2153 warning("URGENT: building for %s%s, but linking in object file (%s) built for %s. "
2154 "Note: This will be an error in the future.",
2155 Options::platformName(platform
), (simulator
? " simulator" : ""), path(),
2156 Options::platformName(lcPlatform
));
2158 #if SUPPORT_APPLE_TV
2159 case Options::kPlatform_tvOS
:
2160 // Error when using bitcocde, warning otherwise.
2162 throwf("building for %s%s, but linking in object file built for %s,",
2163 Options::platformName(platform
), (simulator
? " simulator" : ""),
2164 Options::platformName(lcPlatform
));
2166 warning("URGENT: building for %s%s, but linking in object file (%s) built for %s. "
2167 "Note: This will be an error in the future.",
2168 Options::platformName(platform
), (simulator
? " simulator" : ""), path(),
2169 Options::platformName(lcPlatform
));
2172 case Options::kPlatformUnknown
:
2173 // skip if the target platform is unknown
2177 if ( linkMinOSVersion
&& (_file
->_minOSVersion
> linkMinOSVersion
) ) {
2180 versionToString(_file
->_minOSVersion
, t1
);
2181 versionToString(linkMinOSVersion
, t2
);
2182 warning("object file (%s) was built for newer %s version (%s) than being linked (%s)",
2183 _path
, Options::platformName(lcPlatform
), t1
, t2
);
2188 // validate just one segment
2189 if ( segment
== NULL
)
2190 throw "missing LC_SEGMENT";
2191 if ( segment
->filesize() > _fileLength
)
2192 throw "LC_SEGMENT filesize too large";
2194 // record and validate sections
2195 _sectionsStart
= (macho_section
<P
>*)((char*)segment
+ sizeof(macho_segment_command
<P
>));
2196 _machOSectionsCount
= segment
->nsects();
2197 if ( (sizeof(macho_segment_command
<P
>) + _machOSectionsCount
* sizeof(macho_section
<P
>)) > segment
->cmdsize() )
2198 throw "too many sections for size of LC_SEGMENT command";
2203 template <typename A
>
2204 Options::Platform Parser
<A
>::findPlatform(const macho_header
<P
>* header
)
2206 const uint32_t cmd_count
= header
->ncmds();
2207 if ( cmd_count
== 0 )
2208 return Options::kPlatformUnknown
;
2209 const macho_load_command
<P
>* const cmds
= (macho_load_command
<P
>*)((char*)header
+ sizeof(macho_header
<P
>));
2210 const macho_load_command
<P
>* const cmdsEnd
= (macho_load_command
<P
>*)((char*)header
+ sizeof(macho_header
<P
>) + header
->sizeofcmds());
2211 const macho_load_command
<P
>* cmd
= cmds
;
2212 for (uint32_t i
= 0; i
< cmd_count
; ++i
) {
2213 uint32_t size
= cmd
->cmdsize();
2214 if ( (size
& loadCommandSizeMask()) != 0 )
2215 throwf("load command #%d has a unaligned size", i
);
2216 const uint8_t* endOfCmd
= ((uint8_t*)cmd
)+cmd
->cmdsize();
2217 if ( endOfCmd
> (uint8_t*)cmdsEnd
)
2218 throwf("load command #%d extends beyond the end of the load commands", i
);
2219 switch (cmd
->cmd()) {
2220 case LC_VERSION_MIN_MACOSX
:
2221 return Options::kPlatformOSX
;
2222 case LC_VERSION_MIN_IPHONEOS
:
2223 return Options::kPlatformiOS
;
2224 case LC_VERSION_MIN_WATCHOS
:
2225 return Options::kPlatformWatchOS
;
2226 #if SUPPORT_APPLE_TV
2227 case LC_VERSION_MIN_TVOS
:
2228 return Options::kPlatform_tvOS
;
2230 case LC_BUILD_VERSION
:
2231 return (Options::Platform
)((macho_build_version_command
<P
>*)cmd
)->platform();
2233 cmd
= (const macho_load_command
<P
>*)(((char*)cmd
)+cmd
->cmdsize());
2234 if ( cmd
> cmdsEnd
)
2235 throwf("malformed mach-o file, load command #%d is outside size of load commands", i
);
2237 return Options::kPlatformUnknown
;
2241 template <typename A
>
2242 void Parser
<A
>::prescanSymbolTable()
2244 _tentativeDefinitionCount
= 0;
2245 _absoluteSymbolCount
= 0;
2246 _symbolsInSections
= 0;
2247 _hasDataInCodeLabels
= false;
2248 for (uint32_t i
=0; i
< this->_symbolCount
; ++i
) {
2249 const macho_nlist
<P
>& sym
= symbolFromIndex(i
);
2251 if ( (sym
.n_type() & N_STAB
) != 0 )
2254 // look at undefines
2255 const char* symbolName
= this->nameFromSymbol(sym
);
2256 if ( (sym
.n_type() & N_TYPE
) == N_UNDF
) {
2257 if ( sym
.n_value() != 0 ) {
2258 // count tentative definitions
2259 ++_tentativeDefinitionCount
;
2261 else if ( strncmp(symbolName
, "___dtrace_", 10) == 0 ) {
2262 // any undefined starting with __dtrace_*$ that is not ___dtrace_probe$* or ___dtrace_isenabled$*
2263 // is extra provider info
2264 if ( (strncmp(&symbolName
[10], "probe$", 6) != 0) && (strncmp(&symbolName
[10], "isenabled$", 10) != 0) ) {
2265 _dtraceProviderInfo
.push_back(symbolName
);
2270 else if ( ((sym
.n_type() & N_TYPE
) == N_INDR
) && ((sym
.n_type() & N_EXT
) != 0) ) {
2271 _indirectSymbolCount
++;
2275 // count absolute symbols
2276 if ( (sym
.n_type() & N_TYPE
) == N_ABS
) {
2277 const char* absName
= this->nameFromSymbol(sym
);
2278 // ignore .objc_class_name_* symbols
2279 if ( strncmp(absName
, ".objc_class_name_", 17) == 0 ) {
2283 // ignore .objc_class_name_* symbols
2284 if ( strncmp(absName
, ".objc_category_name_", 20) == 0 )
2286 // ignore empty *.eh symbols
2287 if ( strcmp(&absName
[strlen(absName
)-3], ".eh") == 0 )
2289 ++_absoluteSymbolCount
;
2292 // only look at definitions
2293 if ( (sym
.n_type() & N_TYPE
) != N_SECT
)
2296 // 'L' labels do not denote atom breaks
2297 if ( symbolName
[0] == 'L' ) {
2298 // <rdar://problem/9218847> Formalize data in code with L$start$ labels
2299 if ( strncmp(symbolName
, "L$start$", 8) == 0 )
2300 _hasDataInCodeLabels
= true;
2303 // how many def syms in each section
2304 if ( sym
.n_sect() > _machOSectionsCount
)
2305 throw "bad n_sect in symbol table";
2307 _symbolsInSections
++;
2311 template <typename A
>
2312 void Parser
<A
>::appendAliasAtoms(uint8_t* p
)
2314 for (uint32_t i
=0; i
< this->_symbolCount
; ++i
) {
2315 const macho_nlist
<P
>& sym
= symbolFromIndex(i
);
2317 if ( (sym
.n_type() & N_STAB
) != 0 )
2320 // only look at N_INDR symbols
2321 if ( (sym
.n_type() & N_TYPE
) != N_INDR
)
2324 // skip non-external aliases
2325 if ( (sym
.n_type() & N_EXT
) == 0 )
2328 const char* symbolName
= this->nameFromSymbol(sym
);
2329 const char* aliasOfName
= &_strings
[sym
.n_value()];
2330 bool isHiddenVisibility
= (sym
.n_type() & N_PEXT
);
2331 AliasAtom
* allocatedSpace
= (AliasAtom
*)p
;
2332 new (allocatedSpace
) AliasAtom(symbolName
, isHiddenVisibility
, _file
, aliasOfName
);
2333 p
+= sizeof(AliasAtom
);
2339 template <typename A
>
2340 int Parser
<A
>::sectionIndexSorter(void* extra
, const void* l
, const void* r
)
2342 Parser
<A
>* parser
= (Parser
<A
>*)extra
;
2343 const uint32_t* left
= (uint32_t*)l
;
2344 const uint32_t* right
= (uint32_t*)r
;
2345 const macho_section
<P
>* leftSect
= parser
->machOSectionFromSectionIndex(*left
);
2346 const macho_section
<P
>* rightSect
= parser
->machOSectionFromSectionIndex(*right
);
2348 // can't just return difference because 64-bit diff does not fit in 32-bit return type
2349 int64_t result
= leftSect
->addr() - rightSect
->addr();
2350 if ( result
== 0 ) {
2351 // two sections with same start address
2352 // one with zero size goes first
2353 bool leftEmpty
= ( leftSect
->size() == 0 );
2354 bool rightEmpty
= ( rightSect
->size() == 0 );
2355 if ( leftEmpty
!= rightEmpty
) {
2356 return ( rightEmpty
? 1 : -1 );
2358 if ( !leftEmpty
&& !rightEmpty
)
2359 throwf("overlapping sections");
2360 // both empty, so chose file order
2361 return ( rightSect
- leftSect
);
2363 else if ( result
< 0 )
2369 template <typename A
>
2370 void Parser
<A
>::makeSortedSectionsArray(uint32_t array
[])
2372 const bool log
= false;
2375 fprintf(stderr
, "unsorted sections:\n");
2376 for(unsigned int i
=0; i
< _machOSectionsCount
; ++i
)
2377 fprintf(stderr
, "0x%08llX %s %s\n", _sectionsStart
[i
].addr(), _sectionsStart
[i
].segname(), _sectionsStart
[i
].sectname());
2380 // sort by symbol table address
2381 for (uint32_t i
=0; i
< _machOSectionsCount
; ++i
)
2383 ::qsort_r(array
, _machOSectionsCount
, sizeof(uint32_t), this, §ionIndexSorter
);
2386 fprintf(stderr
, "sorted sections:\n");
2387 for(unsigned int i
=0; i
< _machOSectionsCount
; ++i
)
2388 fprintf(stderr
, "0x%08llX %s %s\n", _sectionsStart
[array
[i
]].addr(), _sectionsStart
[array
[i
]].segname(), _sectionsStart
[array
[i
]].sectname());
2394 template <typename A
>
2395 int Parser
<A
>::symbolIndexSorter(void* extra
, const void* l
, const void* r
)
2397 ParserAndSectionsArray
* extraInfo
= (ParserAndSectionsArray
*)extra
;
2398 Parser
<A
>* parser
= extraInfo
->parser
;
2399 const uint32_t* sortedSectionsArray
= extraInfo
->sortedSectionsArray
;
2400 const uint32_t* left
= (uint32_t*)l
;
2401 const uint32_t* right
= (uint32_t*)r
;
2402 const macho_nlist
<P
>& leftSym
= parser
->symbolFromIndex(*left
);
2403 const macho_nlist
<P
>& rightSym
= parser
->symbolFromIndex(*right
);
2404 // can't just return difference because 64-bit diff does not fit in 32-bit return type
2405 int64_t result
= leftSym
.n_value() - rightSym
.n_value();
2406 if ( result
== 0 ) {
2407 // two symbols with same address
2408 // if in different sections, sort earlier section first
2409 if ( leftSym
.n_sect() != rightSym
.n_sect() ) {
2410 for (uint32_t i
=0; i
< parser
->machOSectionCount(); ++i
) {
2411 if ( sortedSectionsArray
[i
]+1 == leftSym
.n_sect() )
2413 if ( sortedSectionsArray
[i
]+1 == rightSym
.n_sect() )
2417 // two symbols in same section, means one is an alias
2418 // if one is ltmp*, make it an alias (sort first)
2419 const char* leftName
= parser
->nameFromSymbol(leftSym
);
2420 const char* rightName
= parser
->nameFromSymbol(rightSym
);
2421 bool leftIsTmp
= strncmp(leftName
, "ltmp", 4);
2422 bool rightIsTmp
= strncmp(rightName
, "ltmp", 4);
2423 if ( leftIsTmp
!= rightIsTmp
) {
2424 return (rightIsTmp
? -1 : 1);
2427 // if only one is global, make the other an alias (sort first)
2428 if ( (leftSym
.n_type() & N_EXT
) != (rightSym
.n_type() & N_EXT
) ) {
2429 if ( (rightSym
.n_type() & N_EXT
) != 0 )
2434 // if both are global, sort alphabetically. earlier one will be the alias
2435 return ( strcmp(rightName
, leftName
) );
2437 else if ( result
< 0 )
2444 template <typename A
>
2445 void Parser
<A
>::makeSortedSymbolsArray(uint32_t array
[], const uint32_t sectionArray
[])
2447 const bool log
= false;
2449 uint32_t* p
= array
;
2450 for (uint32_t i
=0; i
< this->_symbolCount
; ++i
) {
2451 const macho_nlist
<P
>& sym
= symbolFromIndex(i
);
2453 if ( (sym
.n_type() & N_STAB
) != 0 )
2456 // only look at definitions
2457 if ( (sym
.n_type() & N_TYPE
) != N_SECT
)
2460 // 'L' labels do not denote atom breaks
2461 const char* symbolName
= this->nameFromSymbol(sym
);
2462 if ( symbolName
[0] == 'L' )
2465 // how many def syms in each section
2466 if ( sym
.n_sect() > _machOSectionsCount
)
2467 throw "bad n_sect in symbol table";
2472 assert(p
== &array
[_symbolsInSections
] && "second pass over symbol table yield a different number of symbols");
2474 // sort by symbol table address
2475 ParserAndSectionsArray extra
= { this, sectionArray
};
2476 ::qsort_r(array
, _symbolsInSections
, sizeof(uint32_t), &extra
, &symbolIndexSorter
);
2479 // look for two symbols at same address
2480 _overlappingSymbols
= false;
2481 for (unsigned int i
=1; i
< _symbolsInSections
; ++i
) {
2482 if ( symbolFromIndex(array
[i
-1]).n_value() == symbolFromIndex(array
[i
]).n_value() ) {
2483 //fprintf(stderr, "overlapping symbols at 0x%08llX\n", symbolFromIndex(array[i-1]).n_value());
2484 _overlappingSymbols
= true;
2490 fprintf(stderr
, "sorted symbols:\n");
2491 for(unsigned int i
=0; i
< _symbolsInSections
; ++i
)
2492 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
])) );
2496 template <typename A
>
2497 void Parser
<A
>::makeSections()
2499 // classify each section by type
2500 // compute how many Section objects will be needed and total size for all
2501 unsigned int totalSectionsSize
= 0;
2502 uint8_t machOSectsStorage
[sizeof(MachOSectionAndSectionClass
<P
>)*(_machOSectionsCount
+2)]; // also room for tentative-defs and absolute symbols
2503 // allocate raw storage for all section objects on stack
2504 MachOSectionAndSectionClass
<P
>* machOSects
= (MachOSectionAndSectionClass
<P
>*)machOSectsStorage
;
2505 unsigned int count
= 0;
2506 // local variable for bitcode parsing
2507 const macho_section
<P
>* bitcodeSect
= NULL
;
2508 const macho_section
<P
>* cmdlineSect
= NULL
;
2509 const macho_section
<P
>* swiftCmdlineSect
= NULL
;
2510 const macho_section
<P
>* bundleSect
= NULL
;
2511 bool bitcodeAsm
= false;
2513 for (uint32_t i
=0; i
< _machOSectionsCount
; ++i
) {
2514 const macho_section
<P
>* sect
= &_sectionsStart
[i
];
2515 uint8_t sectionType
= (sect
->flags() & SECTION_TYPE
);
2516 if ( (sect
->offset() + sect
->size() > _fileLength
) && (sectionType
!= S_ZEROFILL
) && (sectionType
!= S_THREAD_LOCAL_ZEROFILL
) )
2517 throwf("section %s/%s extends beyond end of file,", sect
->segname(), sect
->sectname());
2519 if ( (sect
->flags() & S_ATTR_DEBUG
) != 0 ) {
2520 if ( strcmp(sect
->segname(), "__DWARF") == 0 ) {
2521 // note that .o file has dwarf
2522 _file
->_debugInfoKind
= ld::relocatable::File::kDebugInfoDwarf
;
2523 // save off iteresting dwarf sections
2524 if ( strcmp(sect
->sectname(), "__debug_info") == 0 )
2525 _file
->_dwarfDebugInfoSect
= sect
;
2526 else if ( strcmp(sect
->sectname(), "__debug_abbrev") == 0 )
2527 _file
->_dwarfDebugAbbrevSect
= sect
;
2528 else if ( strcmp(sect
->sectname(), "__debug_line") == 0 )
2529 _file
->_dwarfDebugLineSect
= sect
;
2530 else if ( strcmp(sect
->sectname(), "__debug_str") == 0 )
2531 _file
->_dwarfDebugStringSect
= sect
;
2532 // linker does not propagate dwarf sections to output file
2535 else if ( strcmp(sect
->segname(), "__LD") == 0 ) {
2536 if ( strncmp(sect
->sectname(), "__compact_unwind", 16) == 0 ) {
2537 machOSects
[count
].sect
= sect
;
2538 totalSectionsSize
+= sizeof(CUSection
<A
>);
2539 machOSects
[count
++].type
= sectionTypeCompactUnwind
;
2544 if ( strcmp(sect
->segname(), "__LLVM") == 0 ) {
2545 // Process bitcode segement
2546 if ( strncmp(sect
->sectname(), "__bitcode", 9) == 0 ) {
2548 } else if ( strncmp(sect
->sectname(), "__cmdline", 9) == 0 ) {
2550 } else if ( strncmp(sect
->sectname(), "__swift_cmdline", 15) == 0 ) {
2551 swiftCmdlineSect
= sect
;
2552 } else if ( strncmp(sect
->sectname(), "__bundle", 8) == 0 ) {
2554 } else if ( strncmp(sect
->sectname(), "__asm", 5) == 0 ) {
2557 // If treat the bitcode as data, continue to parse as a normal section.
2558 if ( !_treateBitcodeAsData
)
2561 // ignore empty __OBJC sections
2562 if ( (sect
->size() == 0) && (strcmp(sect
->segname(), "__OBJC") == 0) )
2564 // objc image info section is really attributes and not content
2565 if ( ((strcmp(sect
->sectname(), "__image_info") == 0) && (strcmp(sect
->segname(), "__OBJC") == 0))
2566 || ((strncmp(sect
->sectname(), "__objc_imageinfo", 16) == 0) && (strcmp(sect
->segname(), "__DATA") == 0)) ) {
2567 // struct objc_image_info {
2568 // uint32_t version; // initially 0
2571 // #define OBJC_IMAGE_SUPPORTS_GC 2
2572 // #define OBJC_IMAGE_GC_ONLY 4
2573 // #define OBJC_IMAGE_IS_SIMULATED 32
2574 // #define OBJC_IMAGE_HAS_CATEGORY_CLASS_PROPERTIES 64
2576 const uint32_t* contents
= (uint32_t*)(_file
->fileContent()+sect
->offset());
2577 if ( (sect
->size() >= 8) && (contents
[0] == 0) ) {
2578 uint32_t flags
= E::get32(contents
[1]);
2579 if ( (flags
& 4) == 4 )
2580 _file
->_objConstraint
= ld::File::objcConstraintGC
;
2581 else if ( (flags
& 2) == 2 )
2582 _file
->_objConstraint
= ld::File::objcConstraintRetainReleaseOrGC
;
2583 else if ( (flags
& 32) == 32 )
2584 _file
->_objConstraint
= ld::File::objcConstraintRetainReleaseForSimulator
;
2586 _file
->_objConstraint
= ld::File::objcConstraintRetainRelease
;
2587 _file
->_swiftVersion
= ((flags
>> 8) & 0xFF);
2588 _file
->_objcHasCategoryClassPropertiesField
= (flags
& 64);
2589 if ( sect
->size() > 8 ) {
2590 warning("section %s/%s has unexpectedly large size %llu in %s",
2591 sect
->segname(), Section
<A
>::makeSectionName(sect
), sect
->size(), _file
->path());
2595 warning("can't parse %s/%s section in %s", sect
->segname(), Section
<A
>::makeSectionName(sect
), _file
->path());
2599 machOSects
[count
].sect
= sect
;
2600 switch ( sect
->flags() & SECTION_TYPE
) {
2601 case S_SYMBOL_STUBS
:
2602 if ( _stubsSectionNum
== 0 ) {
2603 _stubsSectionNum
= i
+1;
2604 _stubsMachOSection
= sect
;
2607 assert(1 && "multiple S_SYMBOL_STUBS sections");
2608 case S_LAZY_SYMBOL_POINTERS
:
2610 case S_4BYTE_LITERALS
:
2611 totalSectionsSize
+= sizeof(Literal4Section
<A
>);
2612 machOSects
[count
++].type
= sectionTypeLiteral4
;
2614 case S_8BYTE_LITERALS
:
2615 totalSectionsSize
+= sizeof(Literal8Section
<A
>);
2616 machOSects
[count
++].type
= sectionTypeLiteral8
;
2618 case S_16BYTE_LITERALS
:
2619 totalSectionsSize
+= sizeof(Literal16Section
<A
>);
2620 machOSects
[count
++].type
= sectionTypeLiteral16
;
2622 case S_NON_LAZY_SYMBOL_POINTERS
:
2623 totalSectionsSize
+= sizeof(NonLazyPointerSection
<A
>);
2624 machOSects
[count
++].type
= sectionTypeNonLazy
;
2626 case S_THREAD_LOCAL_VARIABLE_POINTERS
:
2627 totalSectionsSize
+= sizeof(TLVPointerSection
<A
>);
2628 machOSects
[count
++].type
= sectionTypeTLVPointers
;
2630 case S_LITERAL_POINTERS
:
2631 if ( (strcmp(sect
->segname(), "__OBJC") == 0) && (strcmp(sect
->sectname(), "__cls_refs") == 0) ) {
2632 totalSectionsSize
+= sizeof(Objc1ClassReferences
<A
>);
2633 machOSects
[count
++].type
= sectionTypeObjC1ClassRefs
;
2636 totalSectionsSize
+= sizeof(PointerToCStringSection
<A
>);
2637 machOSects
[count
++].type
= sectionTypeCStringPointer
;
2640 case S_CSTRING_LITERALS
:
2641 totalSectionsSize
+= sizeof(CStringSection
<A
>);
2642 machOSects
[count
++].type
= sectionTypeCString
;
2644 case S_MOD_INIT_FUNC_POINTERS
:
2645 case S_MOD_TERM_FUNC_POINTERS
:
2646 case S_THREAD_LOCAL_INIT_FUNCTION_POINTERS
:
2651 case S_THREAD_LOCAL_REGULAR
:
2652 case S_THREAD_LOCAL_ZEROFILL
:
2653 if ( (strcmp(sect
->segname(), "__TEXT") == 0) && (strcmp(sect
->sectname(), "__eh_frame") == 0) ) {
2654 totalSectionsSize
+= sizeof(CFISection
<A
>);
2655 machOSects
[count
++].type
= sectionTypeCFI
;
2657 else if ( (strcmp(sect
->segname(), "__DATA") == 0) && (strcmp(sect
->sectname(), "__cfstring") == 0) ) {
2658 totalSectionsSize
+= sizeof(CFStringSection
<A
>);
2659 machOSects
[count
++].type
= sectionTypeCFString
;
2661 else if ( (strcmp(sect
->segname(), "__TEXT") == 0) && (strcmp(sect
->sectname(), "__ustring") == 0) ) {
2662 totalSectionsSize
+= sizeof(UTF16StringSection
<A
>);
2663 machOSects
[count
++].type
= sectionTypeUTF16Strings
;
2665 else if ( (strcmp(sect
->segname(), "__DATA") == 0) && (strncmp(sect
->sectname(), "__objc_classrefs", 16) == 0) ) {
2666 totalSectionsSize
+= sizeof(ObjC2ClassRefsSection
<A
>);
2667 machOSects
[count
++].type
= sectionTypeObjC2ClassRefs
;
2669 else if ( (strcmp(sect
->segname(), "__DATA") == 0) && (strcmp(sect
->sectname(), "__objc_catlist") == 0) ) {
2670 totalSectionsSize
+= sizeof(ObjC2CategoryListSection
<A
>);
2671 machOSects
[count
++].type
= typeObjC2CategoryList
;
2673 else if ( _AppleObjc
&& (strcmp(sect
->segname(), "__OBJC") == 0) && (strcmp(sect
->sectname(), "__class") == 0) ) {
2674 totalSectionsSize
+= sizeof(ObjC1ClassSection
<A
>);
2675 machOSects
[count
++].type
= sectionTypeObjC1Classes
;
2678 totalSectionsSize
+= sizeof(SymboledSection
<A
>);
2679 machOSects
[count
++].type
= sectionTypeSymboled
;
2682 case S_THREAD_LOCAL_VARIABLES
:
2683 totalSectionsSize
+= sizeof(TLVDefsSection
<A
>);
2684 machOSects
[count
++].type
= sectionTypeTLVDefs
;
2687 throwf("unknown section type %d", sect
->flags() & SECTION_TYPE
);
2692 if ( bitcodeSect
!= NULL
) {
2693 if ( cmdlineSect
!= NULL
)
2694 _file
->_bitcode
= std::unique_ptr
<ld::Bitcode
>(new ld::ClangBitcode(&_fileContent
[bitcodeSect
->offset()], bitcodeSect
->size(),
2695 &_fileContent
[cmdlineSect
->offset()], cmdlineSect
->size()));
2696 else if ( swiftCmdlineSect
!= NULL
)
2697 _file
->_bitcode
= std::unique_ptr
<ld::Bitcode
>(new ld::SwiftBitcode(&_fileContent
[bitcodeSect
->offset()], bitcodeSect
->size(),
2698 &_fileContent
[swiftCmdlineSect
->offset()], swiftCmdlineSect
->size()));
2700 throwf("Object file with bitcode missing cmdline options: %s", _file
->path());
2702 else if ( bundleSect
!= NULL
)
2703 _file
->_bitcode
= std::unique_ptr
<ld::Bitcode
>(new ld::BundleBitcode(&_fileContent
[bundleSect
->offset()], bundleSect
->size()));
2704 else if ( bitcodeAsm
)
2705 _file
->_bitcode
= std::unique_ptr
<ld::Bitcode
>(new ld::AsmBitcode(_fileContent
, _fileLength
));
2707 // sort by address (mach-o object files don't aways have sections sorted)
2708 ::qsort(machOSects
, count
, sizeof(MachOSectionAndSectionClass
<P
>), MachOSectionAndSectionClass
<P
>::sorter
);
2710 // we will synthesize a dummy Section<A> object for tentative definitions
2711 if ( _tentativeDefinitionCount
> 0 ) {
2712 totalSectionsSize
+= sizeof(TentativeDefinitionSection
<A
>);
2713 machOSects
[count
++].type
= sectionTypeTentativeDefinitions
;
2716 // we will synthesize a dummy Section<A> object for Absolute symbols
2717 if ( _absoluteSymbolCount
> 0 ) {
2718 totalSectionsSize
+= sizeof(AbsoluteSymbolSection
<A
>);
2719 machOSects
[count
++].type
= sectionTypeAbsoluteSymbols
;
2722 // allocate one block for all Section objects as well as pointers to each
2723 uint8_t* space
= new uint8_t[totalSectionsSize
+count
*sizeof(Section
<A
>*)];
2724 _file
->_sectionsArray
= (Section
<A
>**)space
;
2725 _file
->_sectionsArrayCount
= count
;
2726 Section
<A
>** objects
= _file
->_sectionsArray
;
2727 space
+= count
*sizeof(Section
<A
>*);
2728 for (uint32_t i
=0; i
< count
; ++i
) {
2729 switch ( machOSects
[i
].type
) {
2730 case sectionTypeIgnore
:
2732 case sectionTypeLiteral4
:
2733 *objects
++ = new (space
) Literal4Section
<A
>(*this, *_file
, machOSects
[i
].sect
);
2734 space
+= sizeof(Literal4Section
<A
>);
2736 case sectionTypeLiteral8
:
2737 *objects
++ = new (space
) Literal8Section
<A
>(*this, *_file
, machOSects
[i
].sect
);
2738 space
+= sizeof(Literal8Section
<A
>);
2740 case sectionTypeLiteral16
:
2741 *objects
++ = new (space
) Literal16Section
<A
>(*this, *_file
, machOSects
[i
].sect
);
2742 space
+= sizeof(Literal16Section
<A
>);
2744 case sectionTypeNonLazy
:
2745 *objects
++ = new (space
) NonLazyPointerSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2746 space
+= sizeof(NonLazyPointerSection
<A
>);
2748 case sectionTypeTLVPointers
:
2749 *objects
++ = new (space
) TLVPointerSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2750 space
+= sizeof(TLVPointerSection
<A
>);
2752 case sectionTypeCFI
:
2753 _EHFrameSection
= new (space
) CFISection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2754 *objects
++ = _EHFrameSection
;
2755 space
+= sizeof(CFISection
<A
>);
2757 case sectionTypeCString
:
2758 *objects
++ = new (space
) CStringSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2759 space
+= sizeof(CStringSection
<A
>);
2761 case sectionTypeCStringPointer
:
2762 *objects
++ = new (space
) PointerToCStringSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2763 space
+= sizeof(PointerToCStringSection
<A
>);
2765 case sectionTypeObjC1ClassRefs
:
2766 *objects
++ = new (space
) Objc1ClassReferences
<A
>(*this, *_file
, machOSects
[i
].sect
);
2767 space
+= sizeof(Objc1ClassReferences
<A
>);
2769 case sectionTypeUTF16Strings
:
2770 *objects
++ = new (space
) UTF16StringSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2771 space
+= sizeof(UTF16StringSection
<A
>);
2773 case sectionTypeCFString
:
2774 *objects
++ = new (space
) CFStringSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2775 space
+= sizeof(CFStringSection
<A
>);
2777 case sectionTypeObjC2ClassRefs
:
2778 *objects
++ = new (space
) ObjC2ClassRefsSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2779 space
+= sizeof(ObjC2ClassRefsSection
<A
>);
2781 case typeObjC2CategoryList
:
2782 *objects
++ = new (space
) ObjC2CategoryListSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2783 space
+= sizeof(ObjC2CategoryListSection
<A
>);
2785 case sectionTypeObjC1Classes
:
2786 *objects
++ = new (space
) ObjC1ClassSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2787 space
+= sizeof(ObjC1ClassSection
<A
>);
2789 case sectionTypeSymboled
:
2790 *objects
++ = new (space
) SymboledSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2791 space
+= sizeof(SymboledSection
<A
>);
2793 case sectionTypeTLVDefs
:
2794 *objects
++ = new (space
) TLVDefsSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2795 space
+= sizeof(TLVDefsSection
<A
>);
2797 case sectionTypeCompactUnwind
:
2798 _compactUnwindSection
= new (space
) CUSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2799 *objects
++ = _compactUnwindSection
;
2800 space
+= sizeof(CUSection
<A
>);
2802 case sectionTypeTentativeDefinitions
:
2803 *objects
++ = new (space
) TentativeDefinitionSection
<A
>(*this, *_file
);
2804 space
+= sizeof(TentativeDefinitionSection
<A
>);
2806 case sectionTypeAbsoluteSymbols
:
2807 _absoluteSection
= new (space
) AbsoluteSymbolSection
<A
>(*this, *_file
);
2808 *objects
++ = _absoluteSection
;
2809 space
+= sizeof(AbsoluteSymbolSection
<A
>);
2812 throw "internal error uknown SectionType";
2818 template <typename A
>
2819 Section
<A
>* Parser
<A
>::sectionForAddress(typename
A::P::uint_t addr
)
2821 for (uint32_t i
=0; i
< _file
->_sectionsArrayCount
; ++i
) {
2822 const macho_section
<typename
A::P
>* sect
= _file
->_sectionsArray
[i
]->machoSection();
2823 // TentativeDefinitionSection and AbsoluteSymbolSection have no mach-o section
2824 if ( sect
!= NULL
) {
2825 if ( (sect
->addr() <= addr
) && (addr
< (sect
->addr()+sect
->size())) ) {
2826 return _file
->_sectionsArray
[i
];
2830 // not strictly in any section
2831 // may be in a zero length section
2832 for (uint32_t i
=0; i
< _file
->_sectionsArrayCount
; ++i
) {
2833 const macho_section
<typename
A::P
>* sect
= _file
->_sectionsArray
[i
]->machoSection();
2834 // TentativeDefinitionSection and AbsoluteSymbolSection have no mach-o section
2835 if ( sect
!= NULL
) {
2836 if ( (sect
->addr() == addr
) && (sect
->size() == 0) ) {
2837 return _file
->_sectionsArray
[i
];
2842 throwf("sectionForAddress(0x%llX) address not in any section", (uint64_t)addr
);
2845 template <typename A
>
2846 Section
<A
>* Parser
<A
>::sectionForNum(unsigned int num
)
2848 for (uint32_t i
=0; i
< _file
->_sectionsArrayCount
; ++i
) {
2849 const macho_section
<typename
A::P
>* sect
= _file
->_sectionsArray
[i
]->machoSection();
2850 // TentativeDefinitionSection and AbsoluteSymbolSection have no mach-o section
2851 if ( sect
!= NULL
) {
2852 if ( num
== (unsigned int)((sect
- _sectionsStart
)+1) )
2853 return _file
->_sectionsArray
[i
];
2856 throwf("sectionForNum(%u) section number not for any section", num
);
2859 template <typename A
>
2860 Atom
<A
>* Parser
<A
>::findAtomByAddress(pint_t addr
)
2862 Section
<A
>* section
= this->sectionForAddress(addr
);
2863 return section
->findAtomByAddress(addr
);
2866 template <typename A
>
2867 Atom
<A
>* Parser
<A
>::findAtomByAddressOrNullIfStub(pint_t addr
)
2869 if ( hasStubsSection() && (_stubsMachOSection
->addr() <= addr
) && (addr
< (_stubsMachOSection
->addr()+_stubsMachOSection
->size())) )
2871 return findAtomByAddress(addr
);
2874 template <typename A
>
2875 Atom
<A
>* Parser
<A
>::findAtomByAddressOrLocalTargetOfStub(pint_t addr
, uint32_t* offsetInAtom
)
2877 if ( hasStubsSection() && (_stubsMachOSection
->addr() <= addr
) && (addr
< (_stubsMachOSection
->addr()+_stubsMachOSection
->size())) ) {
2878 // target is a stub, remove indirection
2879 uint32_t symbolIndex
= this->symbolIndexFromIndirectSectionAddress(addr
, _stubsMachOSection
);
2880 assert(symbolIndex
!= INDIRECT_SYMBOL_LOCAL
);
2881 const macho_nlist
<P
>& sym
= this->symbolFromIndex(symbolIndex
);
2882 // can't be to external weak symbol
2883 assert( (this->combineFromSymbol(sym
) != ld::Atom::combineByName
) || (this->scopeFromSymbol(sym
) != ld::Atom::scopeGlobal
) );
2885 return this->findAtomByName(this->nameFromSymbol(sym
));
2887 Atom
<A
>* target
= this->findAtomByAddress(addr
);
2888 *offsetInAtom
= addr
- target
->_objAddress
;
2892 template <typename A
>
2893 Atom
<A
>* Parser
<A
>::findAtomByName(const char* name
)
2895 uint8_t* p
= _file
->_atomsArray
;
2896 for(int i
=_file
->_atomsArrayCount
; i
> 0; --i
) {
2897 Atom
<A
>* atom
= (Atom
<A
>*)p
;
2898 if ( strcmp(name
, atom
->name()) == 0 )
2900 p
+= sizeof(Atom
<A
>);
2905 template <typename A
>
2906 void Parser
<A
>::findTargetFromAddress(pint_t addr
, TargetDesc
& target
)
2908 if ( hasStubsSection() && (_stubsMachOSection
->addr() <= addr
) && (addr
< (_stubsMachOSection
->addr()+_stubsMachOSection
->size())) ) {
2909 // target is a stub, remove indirection
2910 uint32_t symbolIndex
= this->symbolIndexFromIndirectSectionAddress(addr
, _stubsMachOSection
);
2911 assert(symbolIndex
!= INDIRECT_SYMBOL_LOCAL
);
2912 const macho_nlist
<P
>& sym
= this->symbolFromIndex(symbolIndex
);
2914 target
.name
= this->nameFromSymbol(sym
);
2915 target
.weakImport
= this->weakImportFromSymbol(sym
);
2919 Section
<A
>* section
= this->sectionForAddress(addr
);
2920 target
.atom
= section
->findAtomByAddress(addr
);
2921 target
.addend
= addr
- target
.atom
->_objAddress
;
2922 target
.weakImport
= false;
2926 template <typename A
>
2927 void Parser
<A
>::findTargetFromAddress(pint_t baseAddr
, pint_t addr
, TargetDesc
& target
)
2929 findTargetFromAddress(baseAddr
, target
);
2930 target
.addend
= addr
- target
.atom
->_objAddress
;
2933 template <typename A
>
2934 void Parser
<A
>::findTargetFromAddressAndSectionNum(pint_t addr
, unsigned int sectNum
, TargetDesc
& target
)
2936 if ( sectNum
== R_ABS
) {
2937 // target is absolute symbol that corresponds to addr
2938 if ( _absoluteSection
!= NULL
) {
2939 target
.atom
= _absoluteSection
->findAbsAtomForValue(addr
);
2940 if ( target
.atom
!= NULL
) {
2942 target
.weakImport
= false;
2947 throwf("R_ABS reloc but no absolute symbol at target address");
2950 if ( hasStubsSection() && (stubsSectionNum() == sectNum
) ) {
2951 // target is a stub, remove indirection
2952 uint32_t symbolIndex
= this->symbolIndexFromIndirectSectionAddress(addr
, _stubsMachOSection
);
2953 assert(symbolIndex
!= INDIRECT_SYMBOL_LOCAL
);
2954 const macho_nlist
<P
>& sym
= this->symbolFromIndex(symbolIndex
);
2955 // use direct reference when stub is to a static function
2956 if ( ((sym
.n_type() & N_TYPE
) == N_SECT
) && (((sym
.n_type() & N_EXT
) == 0) || (this->nameFromSymbol(sym
)[0] == 'L')) ) {
2957 this->findTargetFromAddressAndSectionNum(sym
.n_value(), sym
.n_sect(), target
);
2961 target
.name
= this->nameFromSymbol(sym
);
2962 target
.weakImport
= this->weakImportFromSymbol(sym
);
2967 Section
<A
>* section
= this->sectionForNum(sectNum
);
2968 target
.atom
= section
->findAtomByAddress(addr
);
2969 if ( target
.atom
== NULL
) {
2970 typedef typename
A::P::sint_t sint_t
;
2971 sint_t a
= (sint_t
)addr
;
2972 sint_t sectStart
= (sint_t
)(section
->machoSection()->addr());
2973 sint_t sectEnd
= sectStart
+ section
->machoSection()->size();
2974 if ( a
< sectStart
) {
2975 // target address is before start of section, so must be negative addend
2976 target
.atom
= section
->findAtomByAddress(sectStart
);
2977 target
.addend
= a
- sectStart
;
2978 target
.weakImport
= false;
2982 else if ( a
>= sectEnd
) {
2983 target
.atom
= section
->findAtomByAddress(sectEnd
-1);
2984 target
.addend
= a
- sectEnd
;
2985 target
.weakImport
= false;
2990 assert(target
.atom
!= NULL
);
2991 target
.addend
= addr
- target
.atom
->_objAddress
;
2992 target
.weakImport
= false;
2996 template <typename A
>
2997 void Parser
<A
>::addDtraceExtraInfos(const SourceLocation
& src
, const char* providerName
)
2999 // for every ___dtrace_stability$* and ___dtrace_typedefs$* undefine with
3000 // a matching provider name, add a by-name kDtraceTypeReference at probe site
3001 const char* dollar
= strchr(providerName
, '$');
3002 if ( dollar
!= NULL
) {
3003 int providerNameLen
= dollar
-providerName
+1;
3004 for ( std::vector
<const char*>::iterator it
= _dtraceProviderInfo
.begin(); it
!= _dtraceProviderInfo
.end(); ++it
) {
3005 const char* typeDollar
= strchr(*it
, '$');
3006 if ( typeDollar
!= NULL
) {
3007 if ( strncmp(typeDollar
+1, providerName
, providerNameLen
) == 0 ) {
3008 addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindDtraceExtra
,false, *it
);
3015 template <typename A
>
3016 const char* Parser
<A
>::scanSymbolTableForAddress(uint64_t addr
)
3018 uint64_t closestSymAddr
= 0;
3019 const char* closestSymName
= NULL
;
3020 for (uint32_t i
=0; i
< this->_symbolCount
; ++i
) {
3021 const macho_nlist
<P
>& sym
= symbolFromIndex(i
);
3023 if ( (sym
.n_type() & N_STAB
) != 0 )
3026 // only look at definitions
3027 if ( (sym
.n_type() & N_TYPE
) != N_SECT
)
3030 // return with exact match
3031 if ( sym
.n_value() == addr
) {
3032 const char* name
= nameFromSymbol(sym
);
3033 if ( strncmp(name
, "ltmp", 4) != 0 )
3035 // treat 'ltmp*' labels as close match
3036 closestSymAddr
= sym
.n_value();
3037 closestSymName
= name
;
3040 // record closest seen so far
3041 if ( (sym
.n_value() < addr
) && ((sym
.n_value() > closestSymAddr
) || (closestSymName
== NULL
)) )
3042 closestSymName
= nameFromSymbol(sym
);
3045 return (closestSymName
!= NULL
) ? closestSymName
: "unknown";
3049 template <typename A
>
3050 void Parser
<A
>::addFixups(const SourceLocation
& src
, ld::Fixup::Kind setKind
, const TargetDesc
& target
)
3052 // some fixup pairs can be combined
3053 ld::Fixup::Cluster cl
= ld::Fixup::k1of3
;
3054 ld::Fixup::Kind firstKind
= ld::Fixup::kindSetTargetAddress
;
3055 bool combined
= false;
3056 if ( target
.addend
== 0 ) {
3057 cl
= ld::Fixup::k1of1
;
3059 switch ( setKind
) {
3060 case ld::Fixup::kindStoreLittleEndian32
:
3061 firstKind
= ld::Fixup::kindStoreTargetAddressLittleEndian32
;
3063 case ld::Fixup::kindStoreLittleEndian64
:
3064 firstKind
= ld::Fixup::kindStoreTargetAddressLittleEndian64
;
3066 case ld::Fixup::kindStoreBigEndian32
:
3067 firstKind
= ld::Fixup::kindStoreTargetAddressBigEndian32
;
3069 case ld::Fixup::kindStoreBigEndian64
:
3070 firstKind
= ld::Fixup::kindStoreTargetAddressBigEndian64
;
3072 case ld::Fixup::kindStoreX86BranchPCRel32
:
3073 firstKind
= ld::Fixup::kindStoreTargetAddressX86BranchPCRel32
;
3075 case ld::Fixup::kindStoreX86PCRel32
:
3076 firstKind
= ld::Fixup::kindStoreTargetAddressX86PCRel32
;
3078 case ld::Fixup::kindStoreX86PCRel32GOTLoad
:
3079 firstKind
= ld::Fixup::kindStoreTargetAddressX86PCRel32GOTLoad
;
3081 case ld::Fixup::kindStoreX86PCRel32TLVLoad
:
3082 firstKind
= ld::Fixup::kindStoreTargetAddressX86PCRel32TLVLoad
;
3084 case ld::Fixup::kindStoreX86Abs32TLVLoad
:
3085 firstKind
= ld::Fixup::kindStoreTargetAddressX86Abs32TLVLoad
;
3087 case ld::Fixup::kindStoreARMBranch24
:
3088 firstKind
= ld::Fixup::kindStoreTargetAddressARMBranch24
;
3090 case ld::Fixup::kindStoreThumbBranch22
:
3091 firstKind
= ld::Fixup::kindStoreTargetAddressThumbBranch22
;
3093 #if SUPPORT_ARCH_arm64
3094 case ld::Fixup::kindStoreARM64Branch26
:
3095 firstKind
= ld::Fixup::kindStoreTargetAddressARM64Branch26
;
3097 case ld::Fixup::kindStoreARM64Page21
:
3098 firstKind
= ld::Fixup::kindStoreTargetAddressARM64Page21
;
3100 case ld::Fixup::kindStoreARM64PageOff12
:
3101 firstKind
= ld::Fixup::kindStoreTargetAddressARM64PageOff12
;
3103 case ld::Fixup::kindStoreARM64GOTLoadPage21
:
3104 firstKind
= ld::Fixup::kindStoreTargetAddressARM64GOTLoadPage21
;
3106 case ld::Fixup::kindStoreARM64GOTLoadPageOff12
:
3107 firstKind
= ld::Fixup::kindStoreTargetAddressARM64GOTLoadPageOff12
;
3109 case ld::Fixup::kindStoreARM64TLVPLoadPage21
:
3110 firstKind
= ld::Fixup::kindStoreTargetAddressARM64TLVPLoadPage21
;
3112 case ld::Fixup::kindStoreARM64TLVPLoadPageOff12
:
3113 firstKind
= ld::Fixup::kindStoreTargetAddressARM64TLVPLoadPageOff12
;
3118 cl
= ld::Fixup::k1of2
;
3123 if ( target
.atom
!= NULL
) {
3124 if ( target
.atom
->scope() == ld::Atom::scopeTranslationUnit
) {
3125 addFixup(src
, cl
, firstKind
, target
.atom
);
3127 else if ( (target
.atom
->combine() == ld::Atom::combineByNameAndContent
) || (target
.atom
->combine() == ld::Atom::combineByNameAndReferences
) ) {
3128 addFixup(src
, cl
, firstKind
, ld::Fixup::bindingByContentBound
, target
.atom
);
3130 else if ( (src
.atom
->section().type() == ld::Section::typeCFString
) && (src
.offsetInAtom
!= 0) ) {
3131 // backing string in CFStrings should always be direct
3132 addFixup(src
, cl
, firstKind
, target
.atom
);
3134 else if ( (src
.atom
== target
.atom
) && (target
.atom
->combine() == ld::Atom::combineByName
) ) {
3135 // reference to self should always be direct
3136 addFixup(src
, cl
, firstKind
, target
.atom
);
3139 // change direct fixup to by-name fixup
3140 addFixup(src
, cl
, firstKind
, false, target
.atom
->name());
3144 addFixup(src
, cl
, firstKind
, target
.weakImport
, target
.name
);
3146 if ( target
.addend
== 0 ) {
3148 addFixup(src
, ld::Fixup::k2of2
, setKind
);
3151 addFixup(src
, ld::Fixup::k2of3
, ld::Fixup::kindAddAddend
, target
.addend
);
3152 addFixup(src
, ld::Fixup::k3of3
, setKind
);
3156 template <typename A
>
3157 void Parser
<A
>::addFixups(const SourceLocation
& src
, ld::Fixup::Kind kind
, const TargetDesc
& target
, const TargetDesc
& picBase
)
3159 ld::Fixup::Cluster cl
= (target
.addend
== 0) ? ld::Fixup::k1of4
: ld::Fixup::k1of5
;
3160 if ( target
.atom
!= NULL
) {
3161 if ( target
.atom
->scope() == ld::Atom::scopeTranslationUnit
) {
3162 addFixup(src
, cl
, ld::Fixup::kindSetTargetAddress
, target
.atom
);
3164 else if ( (target
.atom
->combine() == ld::Atom::combineByNameAndContent
) || (target
.atom
->combine() == ld::Atom::combineByNameAndReferences
) ) {
3165 addFixup(src
, cl
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, target
.atom
);
3168 addFixup(src
, cl
, ld::Fixup::kindSetTargetAddress
, false, target
.atom
->name());
3172 addFixup(src
, cl
, ld::Fixup::kindSetTargetAddress
, target
.weakImport
, target
.name
);
3174 if ( target
.addend
== 0 ) {
3175 assert(picBase
.atom
!= NULL
);
3176 addFixup(src
, ld::Fixup::k2of4
, ld::Fixup::kindSubtractTargetAddress
, picBase
.atom
);
3177 addFixup(src
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractAddend
, picBase
.addend
);
3178 addFixup(src
, ld::Fixup::k4of4
, kind
);
3181 addFixup(src
, ld::Fixup::k2of5
, ld::Fixup::kindAddAddend
, target
.addend
);
3182 addFixup(src
, ld::Fixup::k3of5
, ld::Fixup::kindSubtractTargetAddress
, picBase
.atom
);
3183 addFixup(src
, ld::Fixup::k4of5
, ld::Fixup::kindSubtractAddend
, picBase
.addend
);
3184 addFixup(src
, ld::Fixup::k5of5
, kind
);
3190 template <typename A
>
3191 uint32_t TentativeDefinitionSection
<A
>::computeAtomCount(class Parser
<A
>& parser
,
3192 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
3193 const struct Parser
<A
>::CFI_CU_InfoArrays
&)
3195 return parser
.tentativeDefinitionCount();
3198 template <typename A
>
3199 uint32_t TentativeDefinitionSection
<A
>::appendAtoms(class Parser
<A
>& parser
, uint8_t* p
,
3200 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
3201 const struct Parser
<A
>::CFI_CU_InfoArrays
&)
3203 this->_beginAtoms
= (Atom
<A
>*)p
;
3205 for (uint32_t i
=parser
.undefinedStartIndex(); i
< parser
.undefinedEndIndex(); ++i
) {
3206 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(i
);
3207 if ( ((sym
.n_type() & N_TYPE
) == N_UNDF
) && (sym
.n_value() != 0) ) {
3208 uint64_t size
= sym
.n_value();
3209 uint8_t alignP2
= GET_COMM_ALIGN(sym
.n_desc());
3210 if ( alignP2
== 0 ) {
3211 // common symbols align to their size
3212 // that is, a 4-byte common aligns to 4-bytes
3213 // if this size is not a power of two,
3214 // then round up to the next power of two
3215 alignP2
= 63 - (uint8_t)__builtin_clzll(size
);
3216 if ( size
!= (1ULL << alignP2
) )
3218 // <rdar://problem/24871389> limit default alignment of large commons
3219 if ( alignP2
> parser
.maxDefaultCommonAlignment() )
3220 alignP2
= parser
.maxDefaultCommonAlignment();
3222 Atom
<A
>* allocatedSpace
= (Atom
<A
>*)p
;
3223 new (allocatedSpace
) Atom
<A
>(*this, parser
.nameFromSymbol(sym
), (pint_t
)ULLONG_MAX
, size
,
3224 ld::Atom::definitionTentative
, ld::Atom::combineByName
,
3225 parser
.scopeFromSymbol(sym
), ld::Atom::typeZeroFill
, ld::Atom::symbolTableIn
,
3226 parser
.dontDeadStripFromSymbol(sym
), false, false, ld::Atom::Alignment(alignP2
) );
3227 p
+= sizeof(Atom
<A
>);
3231 this->_endAtoms
= (Atom
<A
>*)p
;
3236 template <typename A
>
3237 uint32_t AbsoluteSymbolSection
<A
>::computeAtomCount(class Parser
<A
>& parser
,
3238 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
3239 const struct Parser
<A
>::CFI_CU_InfoArrays
&)
3241 return parser
.absoluteSymbolCount();
3244 template <typename A
>
3245 uint32_t AbsoluteSymbolSection
<A
>::appendAtoms(class Parser
<A
>& parser
, uint8_t* p
,
3246 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
3247 const struct Parser
<A
>::CFI_CU_InfoArrays
&)
3249 this->_beginAtoms
= (Atom
<A
>*)p
;
3251 for (uint32_t i
=0; i
< parser
.symbolCount(); ++i
) {
3252 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(i
);
3253 if ( (sym
.n_type() & N_TYPE
) != N_ABS
)
3255 const char* absName
= parser
.nameFromSymbol(sym
);
3256 // ignore .objc_class_name_* symbols
3257 if ( strncmp(absName
, ".objc_class_name_", 17) == 0 )
3259 // ignore .objc_class_name_* symbols
3260 if ( strncmp(absName
, ".objc_category_name_", 20) == 0 )
3262 // ignore empty *.eh symbols
3263 if ( strcmp(&absName
[strlen(absName
)-3], ".eh") == 0 )
3266 Atom
<A
>* allocatedSpace
= (Atom
<A
>*)p
;
3267 new (allocatedSpace
) Atom
<A
>(*this, parser
, sym
, 0);
3268 p
+= sizeof(Atom
<A
>);
3271 this->_endAtoms
= (Atom
<A
>*)p
;
3275 template <typename A
>
3276 Atom
<A
>* AbsoluteSymbolSection
<A
>::findAbsAtomForValue(typename
A::P::uint_t value
)
3278 Atom
<A
>* end
= this->_endAtoms
;
3279 for(Atom
<A
>* p
= this->_beginAtoms
; p
< end
; ++p
) {
3280 if ( p
->_objAddress
== value
)
3287 template <typename A
>
3288 uint32_t Parser
<A
>::indirectSymbol(uint32_t indirectIndex
)
3290 if ( indirectIndex
>= _indirectTableCount
)
3291 throw "indirect symbol index out of range";
3292 return E::get32(_indirectTable
[indirectIndex
]);
3295 template <typename A
>
3296 const macho_nlist
<typename
A::P
>& Parser
<A
>::symbolFromIndex(uint32_t index
)
3298 if ( index
> _symbolCount
)
3299 throw "symbol index out of range";
3300 return _symbols
[index
];
3303 template <typename A
>
3304 const macho_section
<typename
A::P
>* Parser
<A
>::machOSectionFromSectionIndex(uint32_t index
)
3306 if ( index
>= _machOSectionsCount
)
3307 throw "section index out of range";
3308 return &_sectionsStart
[index
];
3311 template <typename A
>
3312 uint32_t Parser
<A
>::symbolIndexFromIndirectSectionAddress(pint_t addr
, const macho_section
<P
>* sect
)
3314 uint32_t elementSize
= 0;
3315 switch ( sect
->flags() & SECTION_TYPE
) {
3316 case S_SYMBOL_STUBS
:
3317 elementSize
= sect
->reserved2();
3319 case S_LAZY_SYMBOL_POINTERS
:
3320 case S_NON_LAZY_SYMBOL_POINTERS
:
3321 case S_THREAD_LOCAL_VARIABLE_POINTERS
:
3322 elementSize
= sizeof(pint_t
);
3325 throw "section does not use indirect symbol table";
3327 uint32_t indexInSection
= (addr
- sect
->addr()) / elementSize
;
3328 uint32_t indexIntoIndirectTable
= sect
->reserved1() + indexInSection
;
3329 return this->indirectSymbol(indexIntoIndirectTable
);
3334 template <typename A
>
3335 const char* Parser
<A
>::nameFromSymbol(const macho_nlist
<P
>& sym
)
3337 return &_strings
[sym
.n_strx()];
3340 template <typename A
>
3341 ld::Atom::Scope Parser
<A
>::scopeFromSymbol(const macho_nlist
<P
>& sym
)
3343 if ( (sym
.n_type() & N_EXT
) == 0 )
3344 return ld::Atom::scopeTranslationUnit
;
3345 else if ( (sym
.n_type() & N_PEXT
) != 0 )
3346 return ld::Atom::scopeLinkageUnit
;
3347 else if ( this->nameFromSymbol(sym
)[0] == 'l' ) // since all 'l' symbols will be remove, don't make them global
3348 return ld::Atom::scopeLinkageUnit
;
3350 return ld::Atom::scopeGlobal
;
3353 template <typename A
>
3354 ld::Atom::Definition Parser
<A
>::definitionFromSymbol(const macho_nlist
<P
>& sym
)
3356 switch ( sym
.n_type() & N_TYPE
) {
3358 return ld::Atom::definitionAbsolute
;
3360 return ld::Atom::definitionRegular
;
3362 if ( sym
.n_value() != 0 )
3363 return ld::Atom::definitionTentative
;
3365 throw "definitionFromSymbol() bad symbol";
3368 template <typename A
>
3369 ld::Atom::Combine Parser
<A
>::combineFromSymbol(const macho_nlist
<P
>& sym
)
3371 if ( sym
.n_desc() & N_WEAK_DEF
)
3372 return ld::Atom::combineByName
;
3374 return ld::Atom::combineNever
;
3378 template <typename A
>
3379 ld::Atom::SymbolTableInclusion Parser
<A
>::inclusionFromSymbol(const macho_nlist
<P
>& sym
)
3381 const char* symbolName
= nameFromSymbol(sym
);
3382 // labels beginning with 'l' (lowercase ell) are automatically removed in final linked images <rdar://problem/4571042>
3383 // labels beginning with 'L' should have been stripped by the assembler, so are stripped now
3384 if ( sym
.n_desc() & REFERENCED_DYNAMICALLY
)
3385 return ld::Atom::symbolTableInAndNeverStrip
;
3386 else if ( symbolName
[0] == 'l' )
3387 return ld::Atom::symbolTableNotInFinalLinkedImages
;
3388 else if ( symbolName
[0] == 'L' )
3389 return ld::Atom::symbolTableNotIn
;
3391 return ld::Atom::symbolTableIn
;
3394 template <typename A
>
3395 bool Parser
<A
>::dontDeadStripFromSymbol(const macho_nlist
<P
>& sym
)
3397 return ( (sym
.n_desc() & (N_NO_DEAD_STRIP
|REFERENCED_DYNAMICALLY
)) != 0 );
3400 template <typename A
>
3401 bool Parser
<A
>::isThumbFromSymbol(const macho_nlist
<P
>& sym
)
3403 return ( sym
.n_desc() & N_ARM_THUMB_DEF
);
3406 template <typename A
>
3407 bool Parser
<A
>::weakImportFromSymbol(const macho_nlist
<P
>& sym
)
3409 return ( ((sym
.n_type() & N_TYPE
) == N_UNDF
) && ((sym
.n_desc() & N_WEAK_REF
) != 0) );
3412 template <typename A
>
3413 bool Parser
<A
>::resolverFromSymbol(const macho_nlist
<P
>& sym
)
3415 return ( sym
.n_desc() & N_SYMBOL_RESOLVER
);
3418 template <typename A
>
3419 bool Parser
<A
>::altEntryFromSymbol(const macho_nlist
<P
>& sym
)
3421 return ( sym
.n_desc() & N_ALT_ENTRY
);
3425 /* Skip over a LEB128 value (signed or unsigned). */
3427 skip_leb128 (const uint8_t ** offset
, const uint8_t * end
)
3429 while (*offset
!= end
&& **offset
>= 0x80)
3435 /* Read a ULEB128 into a 64-bit word. Return (uint64_t)-1 on overflow
3436 or error. On overflow, skip past the rest of the uleb128. */
3438 read_uleb128 (const uint8_t ** offset
, const uint8_t * end
)
3440 uint64_t result
= 0;
3447 return (uint64_t) -1;
3449 b
= **offset
& 0x7f;
3451 if (bit
>= 64 || b
<< bit
>> bit
!= b
)
3452 result
= (uint64_t) -1;
3454 result
|= b
<< bit
, bit
+= 7;
3455 } while (*(*offset
)++ >= 0x80);
3460 /* Skip over a DWARF attribute of form FORM. */
3461 template <typename A
>
3462 bool Parser
<A
>::skip_form(const uint8_t ** offset
, const uint8_t * end
, uint64_t form
,
3463 uint8_t addr_size
, bool dwarf64
)
3473 case DW_FORM_block2
:
3474 if (end
- *offset
< 2)
3476 sz
= 2 + A::P::E::get16(*(uint16_t*)offset
);
3479 case DW_FORM_block4
:
3480 if (end
- *offset
< 4)
3482 sz
= 2 + A::P::E::get32(*(uint32_t*)offset
);
3500 case DW_FORM_string
:
3501 while (*offset
!= end
&& **offset
)
3510 sz
= read_uleb128 (offset
, end
);
3513 case DW_FORM_block1
:
3521 case DW_FORM_ref_udata
:
3522 skip_leb128 (offset
, end
);
3526 case DW_FORM_ref_addr
:
3530 case DW_FORM_sec_offset
:
3531 sz
= sizeof(typename
A::P::uint_t
);
3534 case DW_FORM_exprloc
:
3535 sz
= read_uleb128 (offset
, end
);
3538 case DW_FORM_flag_present
:
3542 case DW_FORM_ref_sig8
:
3549 if (end
- *offset
< sz
)
3556 template <typename A
>
3557 const char* Parser
<A
>::getDwarfString(uint64_t form
, const uint8_t*& di
)
3560 const char* dwarfStrings
;
3561 const char* result
= NULL
;
3563 case DW_FORM_string
:
3564 result
= (const char*)di
;
3565 di
+= strlen(result
) + 1;
3568 offset
= E::get32(*((uint32_t*)di
));
3569 dwarfStrings
= (char*)_file
->fileContent() + _file
->_dwarfDebugStringSect
->offset();
3570 if ( offset
< _file
->_dwarfDebugStringSect
->size() )
3571 result
= &dwarfStrings
[offset
];
3573 warning("dwarf DW_FORM_strp (offset=0x%08X) is too big in %s", offset
, this->_path
);
3577 warning("unknown dwarf string encoding (form=%lld) in %s", form
, this->_path
);
3583 template <typename A
>
3584 uint64_t Parser
<A
>::getDwarfOffset(uint64_t form
, const uint8_t*& di
, bool dwarf64
)
3586 if ( form
== DW_FORM_sec_offset
)
3587 form
= (dwarf64
? DW_FORM_data8
: DW_FORM_data4
);
3588 uint64_t result
= -1;
3591 result
= A::P::E::get32(*(uint32_t*)di
);
3595 result
= A::P::E::get64(*(uint64_t*)di
);
3599 warning("unknown dwarf DW_FORM_ for DW_AT_stmt_list in %s", this->_path
);
3605 template <typename A
>
3606 struct AtomAndLineInfo
{
3608 ld::Atom::LineInfo info
;
3612 // <rdar://problem/5591394> Add support to ld64 for N_FUN stabs when used for symbolic constants
3613 // Returns whether a stabStr belonging to an N_FUN stab represents a
3614 // symbolic constant rather than a function
3615 template <typename A
>
3616 bool Parser
<A
>::isConstFunStabs(const char *stabStr
)
3619 // N_FUN can be used for both constants and for functions. In case it's a constant,
3620 // the format of the stabs string is "symname:c=<value>;"
3621 // ':' cannot appear in the symbol name, except if it's an Objective-C method
3622 // (in which case the symbol name starts with + or -, and then it's definitely
3624 return (stabStr
!= NULL
) && (stabStr
[0] != '+') && (stabStr
[0] != '-')
3625 && ((colon
= strchr(stabStr
, ':')) != NULL
)
3626 && (colon
[1] == 'c') && (colon
[2] == '=');
3630 template <typename A
>
3631 void Parser
<A
>::parseDebugInfo()
3633 // check for dwarf __debug_info section
3634 if ( _file
->_dwarfDebugInfoSect
== NULL
) {
3635 // if no DWARF debug info, look for stabs
3639 if ( _file
->_dwarfDebugInfoSect
->size() == 0 )
3645 if ( !read_comp_unit(&tuName
, &tuDir
, &stmtList
) ) {
3646 // if can't parse dwarf, warn and give up
3647 _file
->_dwarfTranslationUnitPath
= NULL
;
3648 warning("can't parse dwarf compilation unit info in %s", _path
);
3649 _file
->_debugInfoKind
= ld::relocatable::File::kDebugInfoNone
;
3652 if ( (tuName
!= NULL
) && (tuName
[0] == '/') ) {
3653 _file
->_dwarfTranslationUnitPath
= tuName
;
3655 else if ( (tuDir
!= NULL
) && (tuName
!= NULL
) ) {
3656 asprintf((char**)&(_file
->_dwarfTranslationUnitPath
), "%s/%s", tuDir
, tuName
);
3658 else if ( tuDir
== NULL
) {
3659 _file
->_dwarfTranslationUnitPath
= tuName
;
3662 _file
->_dwarfTranslationUnitPath
= NULL
;
3665 // add line number info to atoms from dwarf
3666 std::vector
<AtomAndLineInfo
<A
> > entries
;
3667 entries
.reserve(64);
3668 if ( _file
->_debugInfoKind
== ld::relocatable::File::kDebugInfoDwarf
) {
3669 // file with just data will have no __debug_line info
3670 if ( (_file
->_dwarfDebugLineSect
!= NULL
) && (_file
->_dwarfDebugLineSect
->size() != 0) ) {
3671 // validate stmt_list
3672 if ( (stmtList
!= (uint64_t)-1) && (stmtList
< _file
->_dwarfDebugLineSect
->size()) ) {
3673 const uint8_t* debug_line
= (uint8_t*)_file
->fileContent() + _file
->_dwarfDebugLineSect
->offset();
3674 struct line_reader_data
* lines
= line_open(&debug_line
[stmtList
],
3675 _file
->_dwarfDebugLineSect
->size() - stmtList
, E::little_endian
);
3676 struct line_info result
;
3677 Atom
<A
>* curAtom
= NULL
;
3678 uint32_t curAtomOffset
= 0;
3679 uint32_t curAtomAddress
= 0;
3680 uint32_t curAtomSize
= 0;
3681 std::map
<uint32_t,const char*> dwarfIndexToFile
;
3682 if ( lines
!= NULL
) {
3683 while ( line_next(lines
, &result
, line_stop_pc
) ) {
3684 //fprintf(stderr, "curAtom=%p, result.pc=0x%llX, result.line=%llu, result.end_of_sequence=%d,"
3685 // " curAtomAddress=0x%X, curAtomSize=0x%X\n",
3686 // curAtom, result.pc, result.line, result.end_of_sequence, curAtomAddress, curAtomSize);
3687 // work around weird debug line table compiler generates if no functions in __text section
3688 if ( (curAtom
== NULL
) && (result
.pc
== 0) && result
.end_of_sequence
&& (result
.file
== 1))
3690 // for performance, see if in next pc is in current atom
3691 if ( (curAtom
!= NULL
) && (curAtomAddress
<= result
.pc
) && (result
.pc
< (curAtomAddress
+curAtomSize
)) ) {
3692 curAtomOffset
= result
.pc
- curAtomAddress
;
3694 // or pc at end of current atom
3695 else if ( result
.end_of_sequence
&& (curAtom
!= NULL
) && (result
.pc
== (curAtomAddress
+curAtomSize
)) ) {
3696 curAtomOffset
= result
.pc
- curAtomAddress
;
3698 // or only one function that is a one line function
3699 else if ( result
.end_of_sequence
&& (curAtom
== NULL
) && (this->findAtomByAddress(0) != NULL
) && (result
.pc
== this->findAtomByAddress(0)->size()) ) {
3700 curAtom
= this->findAtomByAddress(0);
3701 curAtomOffset
= result
.pc
- curAtom
->objectAddress();
3702 curAtomAddress
= curAtom
->objectAddress();
3703 curAtomSize
= curAtom
->size();
3706 // do slow look up of atom by address
3708 curAtom
= this->findAtomByAddress(result
.pc
);
3711 // in case of bug in debug info, don't abort link, just limp on
3714 if ( curAtom
== NULL
)
3715 break; // file has line info but no functions
3716 if ( result
.end_of_sequence
&& (curAtomAddress
+curAtomSize
< result
.pc
) ) {
3717 // a one line function can be returned by line_next() as one entry with pc at end of blob
3718 // look for alt atom starting at end of previous atom
3719 uint32_t previousEnd
= curAtomAddress
+curAtomSize
;
3720 Atom
<A
>* alt
= this->findAtomByAddressOrNullIfStub(previousEnd
);
3722 continue; // ignore spurious debug info for stubs
3723 if ( result
.pc
<= alt
->objectAddress() + alt
->size() ) {
3725 curAtomOffset
= result
.pc
- alt
->objectAddress();
3726 curAtomAddress
= alt
->objectAddress();
3727 curAtomSize
= alt
->size();
3730 curAtomOffset
= result
.pc
- curAtom
->objectAddress();
3731 curAtomAddress
= curAtom
->objectAddress();
3732 curAtomSize
= curAtom
->size();
3736 curAtomOffset
= result
.pc
- curAtom
->objectAddress();
3737 curAtomAddress
= curAtom
->objectAddress();
3738 curAtomSize
= curAtom
->size();
3741 const char* filename
;
3742 std::map
<uint32_t,const char*>::iterator pos
= dwarfIndexToFile
.find(result
.file
);
3743 if ( pos
== dwarfIndexToFile
.end() ) {
3744 filename
= line_file(lines
, result
.file
);
3745 dwarfIndexToFile
[result
.file
] = filename
;
3748 filename
= pos
->second
;
3750 // only record for ~8000 line info records per function
3751 if ( curAtom
->roomForMoreLineInfoCount() ) {
3752 AtomAndLineInfo
<A
> entry
;
3753 entry
.atom
= curAtom
;
3754 entry
.info
.atomOffset
= curAtomOffset
;
3755 entry
.info
.fileName
= filename
;
3756 entry
.info
.lineNumber
= result
.line
;
3757 //fprintf(stderr, "addr=0x%08llX, line=%lld, file=%s, atom=%s, atom.size=0x%X, end=%d\n",
3758 // result.pc, result.line, filename, curAtom->name(), curAtomSize, result.end_of_sequence);
3759 entries
.push_back(entry
);
3760 curAtom
->incrementLineInfoCount();
3762 if ( result
.end_of_sequence
) {
3772 // assign line info start offset for each atom
3773 uint8_t* p
= _file
->_atomsArray
;
3774 uint32_t liOffset
= 0;
3775 for(int i
=_file
->_atomsArrayCount
; i
> 0; --i
) {
3776 Atom
<A
>* atom
= (Atom
<A
>*)p
;
3777 atom
->_lineInfoStartIndex
= liOffset
;
3778 liOffset
+= atom
->_lineInfoCount
;
3779 atom
->_lineInfoCount
= 0;
3780 p
+= sizeof(Atom
<A
>);
3782 assert(liOffset
== entries
.size());
3783 _file
->_lineInfos
.resize(liOffset
);
3785 // copy each line info for each atom
3786 for (typename
std::vector
<AtomAndLineInfo
<A
> >::iterator it
= entries
.begin(); it
!= entries
.end(); ++it
) {
3787 uint32_t slot
= it
->atom
->_lineInfoStartIndex
+ it
->atom
->_lineInfoCount
;
3788 _file
->_lineInfos
[slot
] = it
->info
;
3789 it
->atom
->_lineInfoCount
++;
3792 // done with temp vector
3796 template <typename A
>
3797 void Parser
<A
>::parseStabs()
3799 // scan symbol table for stabs entries
3800 Atom
<A
>* currentAtom
= NULL
;
3801 pint_t currentAtomAddress
= 0;
3802 enum { start
, inBeginEnd
, inFun
} state
= start
;
3803 for (uint32_t symbolIndex
= 0; symbolIndex
< _symbolCount
; ++symbolIndex
) {
3804 const macho_nlist
<P
>& sym
= this->symbolFromIndex(symbolIndex
);
3805 bool useStab
= true;
3806 uint8_t type
= sym
.n_type();
3807 const char* symString
= (sym
.n_strx() != 0) ? this->nameFromSymbol(sym
) : NULL
;
3808 if ( (type
& N_STAB
) != 0 ) {
3809 _file
->_debugInfoKind
= (_hasUUID
? ld::relocatable::File::kDebugInfoStabsUUID
: ld::relocatable::File::kDebugInfoStabs
);
3810 ld::relocatable::File::Stab stab
;
3813 stab
.other
= sym
.n_sect();
3814 stab
.desc
= sym
.n_desc();
3815 stab
.value
= sym
.n_value();
3821 // beginning of function block
3823 // fall into case to lookup atom by addresss
3826 currentAtomAddress
= sym
.n_value();
3827 currentAtom
= this->findAtomByAddress(currentAtomAddress
);
3828 if ( currentAtom
!= NULL
) {
3829 stab
.atom
= currentAtom
;
3830 stab
.string
= symString
;
3833 fprintf(stderr
, "can't find atom for stabs BNSYM at %08llX in %s",
3834 (uint64_t)sym
.n_value(), _path
);
3844 // not associated with an atom, just copy
3845 stab
.string
= symString
;
3849 // n_value field is NOT atom address ;-(
3850 // need to find atom by name match
3851 const char* colon
= strchr(symString
, ':');
3852 if ( colon
!= NULL
) {
3853 // build underscore leading name
3854 int nameLen
= colon
- symString
;
3855 char symName
[nameLen
+2];
3856 strlcpy(&symName
[1], symString
, nameLen
+1);
3858 symName
[nameLen
+1] = '\0';
3859 currentAtom
= this->findAtomByName(symName
);
3860 if ( currentAtom
!= NULL
) {
3861 stab
.atom
= currentAtom
;
3862 stab
.string
= symString
;
3866 // might be a debug-note without trailing :G()
3867 currentAtom
= this->findAtomByName(symString
);
3868 if ( currentAtom
!= NULL
) {
3869 stab
.atom
= currentAtom
;
3870 stab
.string
= symString
;
3873 if ( stab
.atom
== NULL
) {
3874 // ld_classic added bogus GSYM stabs for old style dtrace probes
3875 if ( (strncmp(symString
, "__dtrace_probe$", 15) != 0) )
3876 warning("can't find atom for N_GSYM stabs %s in %s", symString
, _path
);
3882 if ( isConstFunStabs(symString
) ) {
3883 // constant not associated with a function
3884 stab
.string
= symString
;
3887 // old style stabs without BNSYM
3889 currentAtomAddress
= sym
.n_value();
3890 currentAtom
= this->findAtomByAddress(currentAtomAddress
);
3891 if ( currentAtom
!= NULL
) {
3892 stab
.atom
= currentAtom
;
3893 stab
.string
= symString
;
3896 warning("can't find atom for stabs FUN at %08llX in %s",
3897 (uint64_t)currentAtomAddress
, _path
);
3903 stab
.string
= symString
;
3909 stab
.string
= symString
;
3910 // -gfull built .o file
3913 warning("unknown stabs type 0x%X in %s", type
, _path
);
3917 stab
.atom
= currentAtom
;
3926 Atom
<A
>* nestedAtom
= this->findAtomByAddress(sym
.n_value());
3927 if ( nestedAtom
!= NULL
) {
3928 stab
.atom
= nestedAtom
;
3929 stab
.string
= symString
;
3932 warning("can't find atom for stabs 0x%X at %08llX in %s",
3933 type
, (uint64_t)sym
.n_value(), _path
);
3940 // adjust value to be offset in atom
3941 stab
.value
-= currentAtomAddress
;
3943 stab
.string
= symString
;
3950 if ( isConstFunStabs(symString
) ) {
3951 stab
.atom
= currentAtom
;
3952 stab
.string
= symString
;
3955 if ( sym
.n_sect() != 0 ) {
3956 // found another start stab, must be really old stabs...
3957 currentAtomAddress
= sym
.n_value();
3958 currentAtom
= this->findAtomByAddress(currentAtomAddress
);
3959 if ( currentAtom
!= NULL
) {
3960 stab
.atom
= currentAtom
;
3961 stab
.string
= symString
;
3964 warning("can't find atom for stabs FUN at %08llX in %s",
3965 (uint64_t)currentAtomAddress
, _path
);
3969 // found ending stab, switch back to start state
3970 stab
.string
= symString
;
3971 stab
.atom
= currentAtom
;
3980 // adjust value to be offset in atom
3981 stab
.value
-= currentAtomAddress
;
3982 stab
.atom
= currentAtom
;
3985 stab
.string
= symString
;
3989 stab
.atom
= currentAtom
;
3990 stab
.string
= symString
;
3995 // add to list of stabs for this .o file
3997 _file
->_stabs
.push_back(stab
);
4004 // Look at the compilation unit DIE and determine
4005 // its NAME, compilation directory (in COMP_DIR) and its
4006 // line number information offset (in STMT_LIST). NAME and COMP_DIR
4007 // may be NULL (especially COMP_DIR) if they are not in the .o file;
4008 // STMT_LIST will be (uint64_t) -1.
4010 // At present this assumes that there's only one compilation unit DIE.
4012 template <typename A
>
4013 bool Parser
<A
>::read_comp_unit(const char ** name
, const char ** comp_dir
,
4014 uint64_t *stmt_list
)
4016 const uint8_t * debug_info
;
4017 const uint8_t * debug_abbrev
;
4019 const uint8_t * next_cu
;
4021 const uint8_t * end
;
4022 const uint8_t * enda
;
4025 uint64_t abbrev_base
;
4027 uint8_t address_size
;
4032 *stmt_list
= (uint64_t) -1;
4034 if ( (_file
->_dwarfDebugInfoSect
== NULL
) || (_file
->_dwarfDebugAbbrevSect
== NULL
) )
4037 if (_file
->_dwarfDebugInfoSect
->size() < 12)
4038 /* Too small to be a real debug_info section. */
4041 debug_info
= (uint8_t*)_file
->fileContent() + _file
->_dwarfDebugInfoSect
->offset();
4042 debug_abbrev
= (uint8_t*)_file
->fileContent() + _file
->_dwarfDebugAbbrevSect
->offset();
4043 next_cu
= debug_info
;
4045 while ((uint64_t)(next_cu
- debug_info
) < _file
->_dwarfDebugInfoSect
->size()) {
4047 sz
= A::P::E::get32(*(uint32_t*)di
);
4049 dwarf64
= sz
== 0xffffffff;
4051 sz
= A::P::E::get64(*(uint64_t*)di
), di
+= 8;
4052 else if (sz
> 0xffffff00)
4053 /* Unknown dwarf format. */
4056 /* Verify claimed size. */
4057 if (sz
+ (di
- debug_info
) > _file
->_dwarfDebugInfoSect
->size() || sz
<= (dwarf64
? 23 : 11))
4062 vers
= A::P::E::get16(*(uint16_t*)di
);
4063 if (vers
< 2 || vers
> 4)
4064 /* DWARF version wrong for this code.
4065 Chances are we could continue anyway, but we don't know for sure. */
4069 /* Find the debug_abbrev section. */
4070 abbrev_base
= dwarf64
? A::P::E::get64(*(uint64_t*)di
) : A::P::E::get32(*(uint32_t*)di
);
4071 di
+= dwarf64
? 8 : 4;
4073 if (abbrev_base
> _file
->_dwarfDebugAbbrevSect
->size())
4075 da
= debug_abbrev
+ abbrev_base
;
4076 enda
= debug_abbrev
+ _file
->_dwarfDebugAbbrevSect
->size();
4078 address_size
= *di
++;
4080 /* Find the abbrev number we're looking for. */
4082 abbrev
= read_uleb128 (&di
, end
);
4083 if (abbrev
== (uint64_t) -1)
4086 /* Skip through the debug_abbrev section looking for that abbrev. */
4089 uint64_t this_abbrev
= read_uleb128 (&da
, enda
);
4092 if (this_abbrev
== abbrev
)
4093 /* This is almost always taken. */
4095 skip_leb128 (&da
, enda
); /* Skip the tag. */
4098 da
++; /* Skip the DW_CHILDREN_* value. */
4101 attr
= read_uleb128 (&da
, enda
);
4102 skip_leb128 (&da
, enda
);
4103 } while (attr
!= 0 && attr
!= (uint64_t) -1);
4108 /* Check that the abbrev is one for a DW_TAG_compile_unit. */
4109 if (read_uleb128 (&da
, enda
) != DW_TAG_compile_unit
)
4113 da
++; /* Skip the DW_CHILDREN_* value. */
4115 /* Now, go through the DIE looking for DW_AT_name,
4116 DW_AT_comp_dir, and DW_AT_stmt_list. */
4117 bool skip_to_next_cu
= false;
4118 while (!skip_to_next_cu
) {
4120 uint64_t attr
= read_uleb128 (&da
, enda
);
4121 uint64_t form
= read_uleb128 (&da
, enda
);
4123 if (attr
== (uint64_t) -1)
4127 if (form
== DW_FORM_indirect
)
4128 form
= read_uleb128 (&di
, end
);
4132 *name
= getDwarfString(form
, di
);
4133 /* Swift object files may contain two CUs: One
4134 describes the Swift code, one is created by the
4135 clang importer. Skip over the CU created by the
4136 clang importer as it may be empty. */
4137 if (std::string(*name
) == "<swift-imported-modules>")
4138 skip_to_next_cu
= true;
4140 case DW_AT_comp_dir
:
4141 *comp_dir
= getDwarfString(form
, di
);
4143 case DW_AT_stmt_list
:
4144 *stmt_list
= getDwarfOffset(form
, di
, dwarf64
);
4147 if (! skip_form (&di
, end
, form
, address_size
, dwarf64
))
4157 template <typename A
>
4160 free(_sectionsArray
);
4164 template <typename A
>
4165 const char* File
<A
>::translationUnitSource() const
4167 return _dwarfTranslationUnitPath
;
4170 template <typename A
>
4171 bool File
<A
>::forEachAtom(ld::File::AtomHandler
& handler
) const
4173 handler
.doFile(*this);
4174 uint8_t* p
= _atomsArray
;
4175 for(int i
=_atomsArrayCount
; i
> 0; --i
) {
4176 handler
.doAtom(*((Atom
<A
>*)p
));
4177 p
+= sizeof(Atom
<A
>);
4179 p
= _aliasAtomsArray
;
4180 for(int i
=_aliasAtomsArrayCount
; i
> 0; --i
) {
4181 handler
.doAtom(*((AliasAtom
*)p
));
4182 p
+= sizeof(AliasAtom
);
4185 return (_atomsArrayCount
!= 0) || (_aliasAtomsArrayCount
!= 0);
4188 template <typename A
>
4189 const char* Section
<A
>::makeSegmentName(const macho_section
<typename
A::P
>* sect
)
4191 // mach-o section record only has room for 16-byte seg/sect names
4192 // so a 16-byte name has no trailing zero
4193 const char* name
= sect
->segname();
4194 if ( strlen(name
) < 16 )
4196 char* tmp
= new char[17];
4197 strlcpy(tmp
, name
, 17);
4201 template <typename A
>
4202 const char* Section
<A
>::makeSectionName(const macho_section
<typename
A::P
>* sect
)
4204 const char* name
= sect
->sectname();
4205 if ( strlen(name
) < 16 )
4208 // special case common long section names so we don't have to malloc
4209 if ( strncmp(sect
->sectname(), "__objc_classrefs", 16) == 0 )
4210 return "__objc_classrefs";
4211 if ( strncmp(sect
->sectname(), "__objc_classlist", 16) == 0 )
4212 return "__objc_classlist";
4213 if ( strncmp(sect
->sectname(), "__objc_nlclslist", 16) == 0 )
4214 return "__objc_nlclslist";
4215 if ( strncmp(sect
->sectname(), "__objc_nlcatlist", 16) == 0 )
4216 return "__objc_nlcatlist";
4217 if ( strncmp(sect
->sectname(), "__objc_protolist", 16) == 0 )
4218 return "__objc_protolist";
4219 if ( strncmp(sect
->sectname(), "__objc_protorefs", 16) == 0 )
4220 return "__objc_protorefs";
4221 if ( strncmp(sect
->sectname(), "__objc_superrefs", 16) == 0 )
4222 return "__objc_superrefs";
4223 if ( strncmp(sect
->sectname(), "__objc_imageinfo", 16) == 0 )
4224 return "__objc_imageinfo";
4225 if ( strncmp(sect
->sectname(), "__objc_stringobj", 16) == 0 )
4226 return "__objc_stringobj";
4227 if ( strncmp(sect
->sectname(), "__gcc_except_tab", 16) == 0 )
4228 return "__gcc_except_tab";
4230 char* tmp
= new char[17];
4231 strlcpy(tmp
, name
, 17);
4235 template <typename A
>
4236 bool Section
<A
>::readable(const macho_section
<typename
A::P
>* sect
)
4241 template <typename A
>
4242 bool Section
<A
>::writable(const macho_section
<typename
A::P
>* sect
)
4244 // mach-o .o files do not contain segment permissions
4245 // we just know TEXT is special
4246 return ( strcmp(sect
->segname(), "__TEXT") != 0 );
4249 template <typename A
>
4250 bool Section
<A
>::exectuable(const macho_section
<typename
A::P
>* sect
)
4252 // mach-o .o files do not contain segment permissions
4253 // we just know TEXT is special
4254 return ( strcmp(sect
->segname(), "__TEXT") == 0 );
4258 template <typename A
>
4259 ld::Section::Type Section
<A
>::sectionType(const macho_section
<typename
A::P
>* sect
)
4261 switch ( sect
->flags() & SECTION_TYPE
) {
4263 return ld::Section::typeZeroFill
;
4264 case S_CSTRING_LITERALS
:
4265 if ( (strcmp(sect
->sectname(), "__cstring") == 0) && (strcmp(sect
->segname(), "__TEXT") == 0) )
4266 return ld::Section::typeCString
;
4268 return ld::Section::typeNonStdCString
;
4269 case S_4BYTE_LITERALS
:
4270 return ld::Section::typeLiteral4
;
4271 case S_8BYTE_LITERALS
:
4272 return ld::Section::typeLiteral8
;
4273 case S_LITERAL_POINTERS
:
4274 return ld::Section::typeCStringPointer
;
4275 case S_NON_LAZY_SYMBOL_POINTERS
:
4276 return ld::Section::typeNonLazyPointer
;
4277 case S_LAZY_SYMBOL_POINTERS
:
4278 return ld::Section::typeLazyPointer
;
4279 case S_SYMBOL_STUBS
:
4280 return ld::Section::typeStub
;
4281 case S_MOD_INIT_FUNC_POINTERS
:
4282 return ld::Section::typeInitializerPointers
;
4283 case S_MOD_TERM_FUNC_POINTERS
:
4284 return ld::Section::typeTerminatorPointers
;
4286 return ld::Section::typeUnclassified
;
4287 case S_16BYTE_LITERALS
:
4288 return ld::Section::typeLiteral16
;
4291 if ( sect
->flags() & S_ATTR_PURE_INSTRUCTIONS
) {
4292 return ld::Section::typeCode
;
4294 else if ( strcmp(sect
->segname(), "__TEXT") == 0 ) {
4295 if ( strcmp(sect
->sectname(), "__eh_frame") == 0 )
4296 return ld::Section::typeCFI
;
4297 else if ( strcmp(sect
->sectname(), "__ustring") == 0 )
4298 return ld::Section::typeUTF16Strings
;
4299 else if ( strcmp(sect
->sectname(), "__textcoal_nt") == 0 )
4300 return ld::Section::typeCode
;
4301 else if ( strcmp(sect
->sectname(), "__StaticInit") == 0 )
4302 return ld::Section::typeCode
;
4303 else if ( strcmp(sect
->sectname(), "__constructor") == 0 )
4304 return ld::Section::typeInitializerPointers
;
4306 else if ( strcmp(sect
->segname(), "__DATA") == 0 ) {
4307 if ( strcmp(sect
->sectname(), "__cfstring") == 0 )
4308 return ld::Section::typeCFString
;
4309 else if ( strcmp(sect
->sectname(), "__dyld") == 0 )
4310 return ld::Section::typeDyldInfo
;
4311 else if ( strcmp(sect
->sectname(), "__program_vars") == 0 )
4312 return ld::Section::typeDyldInfo
;
4313 else if ( strncmp(sect
->sectname(), "__objc_classrefs", 16) == 0 )
4314 return ld::Section::typeObjCClassRefs
;
4315 else if ( strcmp(sect
->sectname(), "__objc_catlist") == 0 )
4316 return ld::Section::typeObjC2CategoryList
;
4318 else if ( strcmp(sect
->segname(), "__OBJC") == 0 ) {
4319 if ( strcmp(sect
->sectname(), "__class") == 0 )
4320 return ld::Section::typeObjC1Classes
;
4323 case S_THREAD_LOCAL_REGULAR
:
4324 return ld::Section::typeTLVInitialValues
;
4325 case S_THREAD_LOCAL_ZEROFILL
:
4326 return ld::Section::typeTLVZeroFill
;
4327 case S_THREAD_LOCAL_VARIABLES
:
4328 return ld::Section::typeTLVDefs
;
4329 case S_THREAD_LOCAL_VARIABLE_POINTERS
:
4330 return ld::Section::typeTLVPointers
;
4331 case S_THREAD_LOCAL_INIT_FUNCTION_POINTERS
:
4332 return ld::Section::typeTLVInitializerPointers
;
4334 return ld::Section::typeUnclassified
;
4338 template <typename A
>
4339 Atom
<A
>* Section
<A
>::findContentAtomByAddress(pint_t addr
, class Atom
<A
>* start
, class Atom
<A
>* end
)
4341 // do a binary search of atom array
4342 uint32_t atomCount
= end
- start
;
4343 Atom
<A
>* base
= start
;
4344 for (uint32_t n
= atomCount
; n
> 0; n
/= 2) {
4345 Atom
<A
>* pivot
= &base
[n
/2];
4346 pint_t atomStartAddr
= pivot
->_objAddress
;
4347 pint_t atomEndAddr
= atomStartAddr
+ pivot
->_size
;
4348 if ( atomStartAddr
<= addr
) {
4349 // address in normal atom
4350 if (addr
< atomEndAddr
)
4352 // address in "end" label (but not in alias)
4353 if ( (pivot
->_size
== 0) && (addr
== atomEndAddr
) && !pivot
->isAlias() )
4356 if ( addr
>= atomEndAddr
) {
4358 // move base to atom after pivot
4370 template <typename A
>
4371 ld::Atom::Alignment Section
<A
>::alignmentForAddress(pint_t addr
)
4373 const uint32_t sectionAlignment
= this->_machOSection
->align();
4374 uint32_t modulus
= (addr
% (1 << sectionAlignment
));
4375 if ( modulus
> 0xFFFF )
4376 warning("alignment for symbol at address 0x%08llX in %s exceeds 2^16", (uint64_t)addr
, this->file().path());
4377 return ld::Atom::Alignment(sectionAlignment
, modulus
);
4380 template <typename A
>
4381 uint32_t Section
<A
>::sectionNum(class Parser
<A
>& parser
) const
4383 if ( _machOSection
== NULL
)
4386 return 1 + (this->_machOSection
- parser
.firstMachOSection());
4389 // arm does not have zero cost exceptions
4391 uint32_t CFISection
<arm
>::cfiCount(Parser
<arm
>& parser
)
4393 if ( parser
.armUsesZeroCostExceptions() ) {
4394 // create ObjectAddressSpace object for use by libunwind
4395 OAS
oas(*this, (uint8_t*)this->file().fileContent()+this->_machOSection
->offset());
4396 return libunwind::CFI_Parser
<OAS
>::getCFICount(oas
,
4397 this->_machOSection
->addr(), this->_machOSection
->size());
4402 template <typename A
>
4403 uint32_t CFISection
<A
>::cfiCount(Parser
<A
>& parser
)
4405 // create ObjectAddressSpace object for use by libunwind
4406 OAS
oas(*this, (uint8_t*)this->file().fileContent()+this->_machOSection
->offset());
4407 return libunwind::CFI_Parser
<OAS
>::getCFICount(oas
,
4408 this->_machOSection
->addr(), this->_machOSection
->size());
4411 template <typename A
>
4412 void CFISection
<A
>::warnFunc(void* ref
, uint64_t funcAddr
, const char* msg
)
4414 Parser
<A
>* parser
= (Parser
<A
>*)ref
;
4415 if ( ! parser
->warnUnwindConversionProblems() )
4417 if ( funcAddr
!= CFI_INVALID_ADDRESS
) {
4418 // atoms are not constructed yet, so scan symbol table for labels
4419 const char* name
= parser
->scanSymbolTableForAddress(funcAddr
);
4420 warning("could not create compact unwind for %s: %s", name
, msg
);
4423 warning("could not create compact unwind: %s", msg
);
4428 bool CFISection
<x86_64
>::needsRelocating()
4434 bool CFISection
<arm64
>::needsRelocating()
4440 template <typename A
>
4441 bool CFISection
<A
>::needsRelocating()
4447 void CFISection
<x86_64
>::cfiParse(class Parser
<x86_64
>& parser
, uint8_t* buffer
,
4448 libunwind::CFI_Atom_Info
<CFISection
<x86_64
>::OAS
>::CFI_Atom_Info cfiArray
[],
4449 uint32_t& count
, const pint_t cuStarts
[], uint32_t cuCount
)
4451 const uint32_t sectionSize
= this->_machOSection
->size();
4452 // copy __eh_frame data to buffer
4453 memcpy(buffer
, file().fileContent() + this->_machOSection
->offset(), sectionSize
);
4455 // and apply relocations
4456 const macho_relocation_info
<P
>* relocs
= (macho_relocation_info
<P
>*)(file().fileContent() + this->_machOSection
->reloff());
4457 const macho_relocation_info
<P
>* relocsEnd
= &relocs
[this->_machOSection
->nreloc()];
4458 for (const macho_relocation_info
<P
>* reloc
= relocs
; reloc
< relocsEnd
; ++reloc
) {
4460 switch ( reloc
->r_type() ) {
4461 case X86_64_RELOC_SUBTRACTOR
:
4462 value
= 0 - parser
.symbolFromIndex(reloc
->r_symbolnum()).n_value();
4464 if ( reloc
->r_extern() )
4465 value
+= parser
.symbolFromIndex(reloc
->r_symbolnum()).n_value();
4467 case X86_64_RELOC_UNSIGNED
:
4468 value
= parser
.symbolFromIndex(reloc
->r_symbolnum()).n_value();
4470 case X86_64_RELOC_GOT
:
4471 // this is used for the reference to the personality function in CIEs
4472 // store the symbol number of the personality function for later use as a Fixup
4473 value
= reloc
->r_symbolnum();
4476 fprintf(stderr
, "CFISection::cfiParse() unexpected relocation type at r_address=0x%08X\n", reloc
->r_address());
4479 if ( reloc
->r_address() > sectionSize
)
4480 throwf("malformed __eh_frame relocation, offset (0x%08X) is beyond end of section,", reloc
->r_address());
4483 switch ( reloc
->r_length() ) {
4485 p64
= (uint64_t*)&buffer
[reloc
->r_address()];
4486 E::set64(*p64
, value
+ E::get64(*p64
));
4489 p32
= (uint32_t*)&buffer
[reloc
->r_address()];
4490 E::set32(*p32
, value
+ E::get32(*p32
));
4493 fprintf(stderr
, "CFISection::cfiParse() unexpected relocation size at r_address=0x%08X\n", reloc
->r_address());
4498 // create ObjectAddressSpace object for use by libunwind
4499 OAS
oas(*this, buffer
);
4501 // use libuwind to parse __eh_frame data into array of CFI_Atom_Info
4503 msg
= libunwind::DwarfInstructions
<OAS
, libunwind::Registers_x86_64
>::parseCFIs(
4504 oas
, this->_machOSection
->addr(), this->_machOSection
->size(),
4505 cuStarts
, cuCount
, parser
.keepDwarfUnwind(), parser
.forceDwarfConversion(), parser
.neverConvertDwarf(),
4506 cfiArray
, count
, (void*)&parser
, warnFunc
);
4508 throwf("malformed __eh_frame section: %s", msg
);
4512 void CFISection
<x86
>::cfiParse(class Parser
<x86
>& parser
, uint8_t* buffer
,
4513 libunwind::CFI_Atom_Info
<CFISection
<x86
>::OAS
>::CFI_Atom_Info cfiArray
[],
4514 uint32_t& count
, const pint_t cuStarts
[], uint32_t cuCount
)
4516 // create ObjectAddressSpace object for use by libunwind
4517 OAS
oas(*this, (uint8_t*)this->file().fileContent()+this->_machOSection
->offset());
4519 // use libuwind to parse __eh_frame data into array of CFI_Atom_Info
4521 msg
= libunwind::DwarfInstructions
<OAS
, libunwind::Registers_x86
>::parseCFIs(
4522 oas
, this->_machOSection
->addr(), this->_machOSection
->size(),
4523 cuStarts
, cuCount
, parser
.keepDwarfUnwind(), parser
.forceDwarfConversion(), parser
.neverConvertDwarf(),
4524 cfiArray
, count
, (void*)&parser
, warnFunc
);
4526 throwf("malformed __eh_frame section: %s", msg
);
4533 void CFISection
<arm
>::cfiParse(class Parser
<arm
>& parser
, uint8_t* buffer
,
4534 libunwind::CFI_Atom_Info
<CFISection
<arm
>::OAS
>::CFI_Atom_Info cfiArray
[],
4535 uint32_t& count
, const pint_t cuStarts
[], uint32_t cuCount
)
4537 if ( !parser
.armUsesZeroCostExceptions() ) {
4538 // most arm do not use zero cost exceptions
4542 // create ObjectAddressSpace object for use by libunwind
4543 OAS
oas(*this, (uint8_t*)this->file().fileContent()+this->_machOSection
->offset());
4545 // use libuwind to parse __eh_frame data into array of CFI_Atom_Info
4547 msg
= libunwind::DwarfInstructions
<OAS
, libunwind::Registers_arm
>::parseCFIs(
4548 oas
, this->_machOSection
->addr(), this->_machOSection
->size(),
4549 cuStarts
, cuCount
, parser
.keepDwarfUnwind(), parser
.forceDwarfConversion(), parser
.neverConvertDwarf(),
4550 cfiArray
, count
, (void*)&parser
, warnFunc
);
4552 throwf("malformed __eh_frame section: %s", msg
);
4559 void CFISection
<arm64
>::cfiParse(class Parser
<arm64
>& parser
, uint8_t* buffer
,
4560 libunwind::CFI_Atom_Info
<CFISection
<arm64
>::OAS
>::CFI_Atom_Info cfiArray
[],
4561 uint32_t& count
, const pint_t cuStarts
[], uint32_t cuCount
)
4563 // copy __eh_frame data to buffer
4564 const uint32_t sectionSize
= this->_machOSection
->size();
4565 memcpy(buffer
, file().fileContent() + this->_machOSection
->offset(), sectionSize
);
4567 // and apply relocations
4568 const macho_relocation_info
<P
>* relocs
= (macho_relocation_info
<P
>*)(file().fileContent() + this->_machOSection
->reloff());
4569 const macho_relocation_info
<P
>* relocsEnd
= &relocs
[this->_machOSection
->nreloc()];
4570 for (const macho_relocation_info
<P
>* reloc
= relocs
; reloc
< relocsEnd
; ++reloc
) {
4571 uint64_t* p64
= (uint64_t*)&buffer
[reloc
->r_address()];
4572 uint32_t* p32
= (uint32_t*)&buffer
[reloc
->r_address()];
4573 uint32_t addend32
= E::get32(*p32
);
4574 uint64_t addend64
= E::get64(*p64
);
4576 switch ( reloc
->r_type() ) {
4577 case ARM64_RELOC_SUBTRACTOR
:
4578 value
= 0 - parser
.symbolFromIndex(reloc
->r_symbolnum()).n_value();
4580 if ( reloc
->r_extern() )
4581 value
+= parser
.symbolFromIndex(reloc
->r_symbolnum()).n_value();
4583 case ARM64_RELOC_UNSIGNED
:
4584 value
= parser
.symbolFromIndex(reloc
->r_symbolnum()).n_value();
4586 case ARM64_RELOC_POINTER_TO_GOT
:
4587 // this is used for the reference to the personality function in CIEs
4588 // store the symbol number of the personality function for later use as a Fixup
4589 value
= reloc
->r_symbolnum();
4594 fprintf(stderr
, "CFISection::cfiParse() unexpected relocation type at r_address=0x%08X\n", reloc
->r_address());
4597 if ( reloc
->r_address() > sectionSize
)
4598 throwf("malformed __eh_frame relocation, offset (0x%08X) is beyond end of section,", reloc
->r_address());
4599 switch ( reloc
->r_length() ) {
4601 E::set64(*p64
, value
+ addend64
);
4604 E::set32(*p32
, value
+ addend32
);
4607 fprintf(stderr
, "CFISection::cfiParse() unexpected relocation size at r_address=0x%08X\n", reloc
->r_address());
4613 // create ObjectAddressSpace object for use by libunwind
4614 OAS
oas(*this, buffer
);
4616 // use libuwind to parse __eh_frame data into array of CFI_Atom_Info
4618 msg
= libunwind::DwarfInstructions
<OAS
, libunwind::Registers_arm64
>::parseCFIs(
4619 oas
, this->_machOSection
->addr(), this->_machOSection
->size(),
4620 cuStarts
, cuCount
, parser
.keepDwarfUnwind(), parser
.forceDwarfConversion(), parser
.neverConvertDwarf(),
4621 cfiArray
, count
, (void*)&parser
, warnFunc
);
4623 throwf("malformed __eh_frame section: %s", msg
);
4627 template <typename A
>
4628 uint32_t CFISection
<A
>::computeAtomCount(class Parser
<A
>& parser
,
4629 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
4630 const struct Parser
<A
>::CFI_CU_InfoArrays
& cfis
)
4632 return cfis
.cfiCount
;
4637 template <typename A
>
4638 uint32_t CFISection
<A
>::appendAtoms(class Parser
<A
>& parser
, uint8_t* p
,
4639 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
4640 const struct Parser
<A
>::CFI_CU_InfoArrays
& cfis
)
4642 this->_beginAtoms
= (Atom
<A
>*)p
;
4643 // walk CFI_Atom_Info array and create atom for each entry
4644 const CFI_Atom_Info
* start
= &cfis
.cfiArray
[0];
4645 const CFI_Atom_Info
* end
= &cfis
.cfiArray
[cfis
.cfiCount
];
4646 for(const CFI_Atom_Info
* a
=start
; a
< end
; ++a
) {
4647 Atom
<A
>* space
= (Atom
<A
>*)p
;
4648 new (space
) Atom
<A
>(*this, (a
->isCIE
? "CIE" : "FDE"), a
->address
, a
->size
,
4649 ld::Atom::definitionRegular
, ld::Atom::combineNever
, ld::Atom::scopeTranslationUnit
,
4650 ld::Atom::typeCFI
, ld::Atom::symbolTableNotInFinalLinkedImages
,
4651 false, false, false, ld::Atom::Alignment(0));
4652 p
+= sizeof(Atom
<A
>);
4654 this->_endAtoms
= (Atom
<A
>*)p
;
4655 return cfis
.cfiCount
;
4659 template <> bool CFISection
<x86_64
>::bigEndian() { return false; }
4660 template <> bool CFISection
<x86
>::bigEndian() { return false; }
4661 template <> bool CFISection
<arm
>::bigEndian() { return false; }
4662 template <> bool CFISection
<arm64
>::bigEndian() { return false; }
4665 void CFISection
<x86_64
>::addCiePersonalityFixups(class Parser
<x86_64
>& parser
, const CFI_Atom_Info
* cieInfo
)
4667 uint8_t personalityEncoding
= cieInfo
->u
.cieInfo
.personality
.encodingOfTargetAddress
;
4668 if ( personalityEncoding
== 0x9B ) {
4669 // compiler always produces X86_64_RELOC_GOT with addend of 4 to personality function
4670 // CFISection<x86_64>::cfiParse() set targetAddress to be symbolIndex + 4 + addressInCIE
4671 uint32_t symbolIndex
= cieInfo
->u
.cieInfo
.personality
.targetAddress
- 4
4672 - cieInfo
->address
- cieInfo
->u
.cieInfo
.personality
.offsetInCFI
;
4673 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(symbolIndex
);
4674 const char* personalityName
= parser
.nameFromSymbol(sym
);
4676 Atom
<x86_64
>* cieAtom
= this->findAtomByAddress(cieInfo
->address
);
4677 Parser
<x86_64
>::SourceLocation
src(cieAtom
, cieInfo
->u
.cieInfo
.personality
.offsetInCFI
);
4678 parser
.addFixup(src
, ld::Fixup::k1of3
, ld::Fixup::kindSetTargetAddress
, false, personalityName
);
4679 parser
.addFixup(src
, ld::Fixup::k2of3
, ld::Fixup::kindAddAddend
, 4);
4680 parser
.addFixup(src
, ld::Fixup::k3of3
, ld::Fixup::kindStoreX86PCRel32GOT
);
4682 else if ( personalityEncoding
!= 0 ) {
4683 throwf("unsupported address encoding (%02X) of personality function in CIE",
4684 personalityEncoding
);
4689 void CFISection
<x86
>::addCiePersonalityFixups(class Parser
<x86
>& parser
, const CFI_Atom_Info
* cieInfo
)
4691 uint8_t personalityEncoding
= cieInfo
->u
.cieInfo
.personality
.encodingOfTargetAddress
;
4692 if ( (personalityEncoding
== 0x9B) || (personalityEncoding
== 0x90) ) {
4693 uint32_t offsetInCFI
= cieInfo
->u
.cieInfo
.personality
.offsetInCFI
;
4694 uint32_t nlpAddr
= cieInfo
->u
.cieInfo
.personality
.targetAddress
;
4695 Atom
<x86
>* cieAtom
= this->findAtomByAddress(cieInfo
->address
);
4696 Atom
<x86
>* nlpAtom
= parser
.findAtomByAddress(nlpAddr
);
4697 assert(nlpAtom
->contentType() == ld::Atom::typeNonLazyPointer
);
4698 Parser
<x86
>::SourceLocation
src(cieAtom
, cieInfo
->u
.cieInfo
.personality
.offsetInCFI
);
4700 parser
.addFixup(src
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, nlpAtom
);
4701 parser
.addFixup(src
, ld::Fixup::k2of4
, ld::Fixup::kindSubtractTargetAddress
, cieAtom
);
4702 parser
.addFixup(src
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractAddend
, offsetInCFI
);
4703 parser
.addFixup(src
, ld::Fixup::k4of4
, ld::Fixup::kindStoreLittleEndian32
);
4705 else if ( personalityEncoding
!= 0 ) {
4706 throwf("unsupported address encoding (%02X) of personality function in CIE", personalityEncoding
);
4710 #if SUPPORT_ARCH_arm64
4712 void CFISection
<arm64
>::addCiePersonalityFixups(class Parser
<arm64
>& parser
, const CFI_Atom_Info
* cieInfo
)
4714 uint8_t personalityEncoding
= cieInfo
->u
.cieInfo
.personality
.encodingOfTargetAddress
;
4715 if ( personalityEncoding
== 0x9B ) {
4716 // compiler always produces ARM64_RELOC_GOT r_pcrel=1 to personality function
4717 // CFISection<arm64>::cfiParse() set targetAddress to be symbolIndex + addressInCIE
4718 uint32_t symbolIndex
= cieInfo
->u
.cieInfo
.personality
.targetAddress
4719 - cieInfo
->address
- cieInfo
->u
.cieInfo
.personality
.offsetInCFI
;
4720 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(symbolIndex
);
4721 const char* personalityName
= parser
.nameFromSymbol(sym
);
4723 Atom
<arm64
>* cieAtom
= this->findAtomByAddress(cieInfo
->address
);
4724 Parser
<arm64
>::SourceLocation
src(cieAtom
, cieInfo
->u
.cieInfo
.personality
.offsetInCFI
);
4725 parser
.addFixup(src
, ld::Fixup::k1of2
, ld::Fixup::kindSetTargetAddress
, false, personalityName
);
4726 parser
.addFixup(src
, ld::Fixup::k2of2
, ld::Fixup::kindStoreARM64PCRelToGOT
);
4728 else if ( personalityEncoding
!= 0 ) {
4729 throwf("unsupported address encoding (%02X) of personality function in CIE",
4730 personalityEncoding
);
4737 void CFISection
<arm
>::addCiePersonalityFixups(class Parser
<arm
>& parser
, const CFI_Atom_Info
* cieInfo
)
4739 uint8_t personalityEncoding
= cieInfo
->u
.cieInfo
.personality
.encodingOfTargetAddress
;
4740 if ( (personalityEncoding
== 0x9B) || (personalityEncoding
== 0x90) ) {
4741 uint32_t offsetInCFI
= cieInfo
->u
.cieInfo
.personality
.offsetInCFI
;
4742 uint32_t nlpAddr
= cieInfo
->u
.cieInfo
.personality
.targetAddress
;
4743 Atom
<arm
>* cieAtom
= this->findAtomByAddress(cieInfo
->address
);
4744 Atom
<arm
>* nlpAtom
= parser
.findAtomByAddress(nlpAddr
);
4745 assert(nlpAtom
->contentType() == ld::Atom::typeNonLazyPointer
);
4746 Parser
<arm
>::SourceLocation
src(cieAtom
, cieInfo
->u
.cieInfo
.personality
.offsetInCFI
);
4748 parser
.addFixup(src
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, nlpAtom
);
4749 parser
.addFixup(src
, ld::Fixup::k2of4
, ld::Fixup::kindSubtractTargetAddress
, cieAtom
);
4750 parser
.addFixup(src
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractAddend
, offsetInCFI
);
4751 parser
.addFixup(src
, ld::Fixup::k4of4
, ld::Fixup::kindStoreLittleEndian32
);
4753 else if ( personalityEncoding
!= 0 ) {
4754 throwf("unsupported address encoding (%02X) of personality function in CIE", personalityEncoding
);
4760 template <typename A
>
4761 void CFISection
<A
>::addCiePersonalityFixups(class Parser
<A
>& parser
, const CFI_Atom_Info
* cieInfo
)
4763 assert(0 && "addCiePersonalityFixups() not implemented for arch");
4766 template <typename A
>
4767 void CFISection
<A
>::makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
& cfis
)
4769 ld::Fixup::Kind store32
= bigEndian() ? ld::Fixup::kindStoreBigEndian32
: ld::Fixup::kindStoreLittleEndian32
;
4770 ld::Fixup::Kind store64
= bigEndian() ? ld::Fixup::kindStoreBigEndian64
: ld::Fixup::kindStoreLittleEndian64
;
4772 // add all references for FDEs, including implicit group references
4773 const CFI_Atom_Info
* end
= &cfis
.cfiArray
[cfis
.cfiCount
];
4774 for(const CFI_Atom_Info
* p
= &cfis
.cfiArray
[0]; p
< end
; ++p
) {
4776 // add reference to personality function if used
4777 if ( p
->u
.cieInfo
.personality
.targetAddress
!= CFI_INVALID_ADDRESS
) {
4778 this->addCiePersonalityFixups(parser
, p
);
4783 Atom
<A
>* fdeAtom
= this->findAtomByAddress(p
->address
);
4784 // find function Atom
4785 Atom
<A
>* functionAtom
= parser
.findAtomByAddress(p
->u
.fdeInfo
.function
.targetAddress
);
4787 Atom
<A
>* cieAtom
= this->findAtomByAddress(p
->u
.fdeInfo
.cie
.targetAddress
);
4789 Atom
<A
>* lsdaAtom
= NULL
;
4790 if ( p
->u
.fdeInfo
.lsda
.targetAddress
!= CFI_INVALID_ADDRESS
) {
4791 lsdaAtom
= parser
.findAtomByAddress(p
->u
.fdeInfo
.lsda
.targetAddress
);
4793 // add reference from FDE to CIE (always 32-bit pc-rel)
4794 typename Parser
<A
>::SourceLocation
fdeToCieSrc(fdeAtom
, p
->u
.fdeInfo
.cie
.offsetInCFI
);
4795 parser
.addFixup(fdeToCieSrc
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, fdeAtom
);
4796 parser
.addFixup(fdeToCieSrc
, ld::Fixup::k2of4
, ld::Fixup::kindAddAddend
, p
->u
.fdeInfo
.cie
.offsetInCFI
);
4797 parser
.addFixup(fdeToCieSrc
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractTargetAddress
, cieAtom
);
4798 parser
.addFixup(fdeToCieSrc
, ld::Fixup::k4of4
, store32
, cieAtom
);
4800 // add reference from FDE to function
4801 typename Parser
<A
>::SourceLocation
fdeToFuncSrc(fdeAtom
, p
->u
.fdeInfo
.function
.offsetInCFI
);
4802 switch (p
->u
.fdeInfo
.function
.encodingOfTargetAddress
) {
4803 case DW_EH_PE_pcrel
|DW_EH_PE_ptr
:
4804 if ( sizeof(typename
A::P::uint_t
) == 8 ) {
4805 parser
.addFixup(fdeToFuncSrc
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, functionAtom
);
4806 parser
.addFixup(fdeToFuncSrc
, ld::Fixup::k2of4
, ld::Fixup::kindSubtractTargetAddress
, fdeAtom
);
4807 parser
.addFixup(fdeToFuncSrc
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractAddend
, p
->u
.fdeInfo
.function
.offsetInCFI
);
4808 parser
.addFixup(fdeToFuncSrc
, ld::Fixup::k4of4
, store64
);
4811 // else fall into 32-bit case
4812 case DW_EH_PE_pcrel
|DW_EH_PE_sdata4
:
4813 parser
.addFixup(fdeToFuncSrc
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, functionAtom
);
4814 parser
.addFixup(fdeToFuncSrc
, ld::Fixup::k2of4
, ld::Fixup::kindSubtractTargetAddress
, fdeAtom
);
4815 parser
.addFixup(fdeToFuncSrc
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractAddend
, p
->u
.fdeInfo
.function
.offsetInCFI
);
4816 parser
.addFixup(fdeToFuncSrc
, ld::Fixup::k4of4
, store32
);
4819 throw "unsupported encoding in FDE of pointer to function";
4822 // add reference from FDE to LSDA
4823 typename Parser
<A
>::SourceLocation
fdeToLsdaSrc(fdeAtom
, p
->u
.fdeInfo
.lsda
.offsetInCFI
);
4824 if ( lsdaAtom
!= NULL
) {
4825 switch (p
->u
.fdeInfo
.lsda
.encodingOfTargetAddress
) {
4826 case DW_EH_PE_pcrel
|DW_EH_PE_ptr
:
4827 if ( sizeof(typename
A::P::uint_t
) == 8 ) {
4828 parser
.addFixup(fdeToLsdaSrc
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, lsdaAtom
);
4829 parser
.addFixup(fdeToLsdaSrc
, ld::Fixup::k2of4
, ld::Fixup::kindSubtractTargetAddress
, fdeAtom
);
4830 parser
.addFixup(fdeToLsdaSrc
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractAddend
, p
->u
.fdeInfo
.lsda
.offsetInCFI
);
4831 parser
.addFixup(fdeToLsdaSrc
, ld::Fixup::k4of4
, store64
);
4834 // else fall into 32-bit case
4835 case DW_EH_PE_pcrel
|DW_EH_PE_sdata4
:
4836 parser
.addFixup(fdeToLsdaSrc
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, lsdaAtom
);
4837 parser
.addFixup(fdeToLsdaSrc
, ld::Fixup::k2of4
, ld::Fixup::kindSubtractTargetAddress
, fdeAtom
);
4838 parser
.addFixup(fdeToLsdaSrc
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractAddend
, p
->u
.fdeInfo
.lsda
.offsetInCFI
);
4839 parser
.addFixup(fdeToLsdaSrc
, ld::Fixup::k4of4
, store32
);
4842 throw "unsupported encoding in FDE of pointer to LSDA";
4846 // FDE is in group lead by function atom
4847 typename Parser
<A
>::SourceLocation
fdeSrc(functionAtom
,0);
4848 parser
.addFixup(fdeSrc
, ld::Fixup::k1of1
, ld::Fixup::kindNoneGroupSubordinateFDE
, fdeAtom
);
4850 // LSDA is in group lead by function atom
4851 if ( lsdaAtom
!= NULL
) {
4852 parser
.addFixup(fdeSrc
, ld::Fixup::k1of1
, ld::Fixup::kindNoneGroupSubordinateLSDA
, lsdaAtom
);
4861 template <typename A
>
4862 const void* CFISection
<A
>::OAS::mappedAddress(pint_t addr
)
4864 if ( (_ehFrameStartAddr
<= addr
) && (addr
< _ehFrameEndAddr
) )
4865 return &_ehFrameContent
[addr
-_ehFrameStartAddr
];
4867 // requested bytes are not in __eh_frame section
4868 // this can occur when examining the instruction bytes in the __text
4869 File
<A
>& file
= _ehFrameSection
.file();
4870 for (uint32_t i
=0; i
< file
._sectionsArrayCount
; ++i
) {
4871 const macho_section
<typename
A::P
>* sect
= file
._sectionsArray
[i
]->machoSection();
4872 // TentativeDefinitionSection and AbsoluteSymbolSection have no mach-o section
4873 if ( sect
!= NULL
) {
4874 if ( (sect
->addr() <= addr
) && (addr
< (sect
->addr()+sect
->size())) ) {
4875 return file
.fileContent() + sect
->offset() + addr
- sect
->addr();
4879 throwf("__eh_frame parsing problem. Can't find target of reference to address 0x%08llX", (uint64_t)addr
);
4884 template <typename A
>
4885 uint64_t CFISection
<A
>::OAS::getULEB128(pint_t
& logicalAddr
, pint_t end
)
4887 uintptr_t size
= (end
- logicalAddr
);
4888 libunwind::LocalAddressSpace::pint_t laddr
= (libunwind::LocalAddressSpace::pint_t
)mappedAddress(logicalAddr
);
4889 libunwind::LocalAddressSpace::pint_t sladdr
= laddr
;
4890 uint64_t result
= libunwind::LocalAddressSpace::getULEB128(laddr
, laddr
+size
);
4891 logicalAddr
+= (laddr
-sladdr
);
4895 template <typename A
>
4896 int64_t CFISection
<A
>::OAS::getSLEB128(pint_t
& logicalAddr
, pint_t end
)
4898 uintptr_t size
= (end
- logicalAddr
);
4899 libunwind::LocalAddressSpace::pint_t laddr
= (libunwind::LocalAddressSpace::pint_t
)mappedAddress(logicalAddr
);
4900 libunwind::LocalAddressSpace::pint_t sladdr
= laddr
;
4901 int64_t result
= libunwind::LocalAddressSpace::getSLEB128(laddr
, laddr
+size
);
4902 logicalAddr
+= (laddr
-sladdr
);
4906 template <typename A
>
4907 typename
A::P::uint_t CFISection
<A
>::OAS::getEncodedP(pint_t
& addr
, pint_t end
, uint8_t encoding
)
4909 pint_t startAddr
= addr
;
4914 switch (encoding
& 0x0F) {
4916 result
= getP(addr
);
4917 p
+= sizeof(pint_t
);
4920 case DW_EH_PE_uleb128
:
4921 result
= getULEB128(addr
, end
);
4923 case DW_EH_PE_udata2
:
4924 result
= get16(addr
);
4928 case DW_EH_PE_udata4
:
4929 result
= get32(addr
);
4933 case DW_EH_PE_udata8
:
4934 result
= get64(addr
);
4938 case DW_EH_PE_sleb128
:
4939 result
= getSLEB128(addr
, end
);
4941 case DW_EH_PE_sdata2
:
4942 result
= (int16_t)get16(addr
);
4946 case DW_EH_PE_sdata4
:
4947 result
= (int32_t)get32(addr
);
4951 case DW_EH_PE_sdata8
:
4952 result
= get64(addr
);
4957 throwf("ObjectFileAddressSpace<A>::getEncodedP() encoding 0x%08X not supported", encoding
);
4960 // then add relative offset
4961 switch ( encoding
& 0x70 ) {
4962 case DW_EH_PE_absptr
:
4965 case DW_EH_PE_pcrel
:
4966 result
+= startAddr
;
4968 case DW_EH_PE_textrel
:
4969 throw "DW_EH_PE_textrel pointer encoding not supported";
4971 case DW_EH_PE_datarel
:
4972 throw "DW_EH_PE_datarel pointer encoding not supported";
4974 case DW_EH_PE_funcrel
:
4975 throw "DW_EH_PE_funcrel pointer encoding not supported";
4977 case DW_EH_PE_aligned
:
4978 throw "DW_EH_PE_aligned pointer encoding not supported";
4981 throwf("ObjectFileAddressSpace<A>::getEncodedP() encoding 0x%08X not supported", encoding
);
4985 // Note: DW_EH_PE_indirect is only used in CIEs to refernce the personality pointer
4986 // When parsing .o files that pointer contains zero, so we don't to return that.
4987 // Instead we skip the dereference and return the address of the pointer.
4988 // if ( encoding & DW_EH_PE_indirect )
4989 // result = getP(result);
4995 const char* CUSection
<x86_64
>::personalityName(class Parser
<x86_64
>& parser
, const macho_relocation_info
<x86_64::P
>* reloc
)
4997 if ( reloc
->r_extern() ) {
4998 assert((reloc
->r_type() == X86_64_RELOC_UNSIGNED
) && "wrong reloc type on personality column in __compact_unwind section");
4999 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(reloc
->r_symbolnum());
5000 return parser
.nameFromSymbol(sym
);
5003 const pint_t
* content
= (pint_t
*)(this->file().fileContent() + this->_machOSection
->offset() + reloc
->r_address());
5004 pint_t personalityAddr
= *content
;
5005 assert((parser
.sectionForAddress(personalityAddr
)->type() == ld::Section::typeCode
) && "personality column in __compact_unwind section is not pointer to function");
5006 // atoms may not be constructed yet, so scan symbol table for labels
5007 const char* name
= parser
.scanSymbolTableForAddress(personalityAddr
);
5013 const char* CUSection
<x86
>::personalityName(class Parser
<x86
>& parser
, const macho_relocation_info
<x86::P
>* reloc
)
5015 if ( reloc
->r_extern() ) {
5016 assert((reloc
->r_type() == GENERIC_RELOC_VANILLA
) && "wrong reloc type on personality column in __compact_unwind section");
5017 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(reloc
->r_symbolnum());
5018 return parser
.nameFromSymbol(sym
);
5021 // support __LD, __compact_unwind personality entries which are pointer to personality non-lazy pointer
5022 const pint_t
* content
= (pint_t
*)(this->file().fileContent() + this->_machOSection
->offset() + reloc
->r_address());
5023 pint_t nlPointerAddr
= *content
;
5024 Section
<x86
>* nlSection
= parser
.sectionForAddress(nlPointerAddr
);
5025 if ( nlSection
->type() == ld::Section::typeCode
) {
5026 // personality function is defined in this .o file, so this is a direct reference to it
5027 // atoms may not be constructed yet, so scan symbol table for labels
5028 const char* name
= parser
.scanSymbolTableForAddress(nlPointerAddr
);
5032 uint32_t symIndex
= parser
.symbolIndexFromIndirectSectionAddress(nlPointerAddr
, nlSection
->machoSection());
5033 const macho_nlist
<P
>& nlSymbol
= parser
.symbolFromIndex(symIndex
);
5034 return parser
.nameFromSymbol(nlSymbol
);
5039 #if SUPPORT_ARCH_arm64
5041 const char* CUSection
<arm64
>::personalityName(class Parser
<arm64
>& parser
, const macho_relocation_info
<arm64::P
>* reloc
)
5043 if ( reloc
->r_extern() ) {
5044 assert((reloc
->r_type() == ARM64_RELOC_UNSIGNED
) && "wrong reloc type on personality column in __compact_unwind section");
5045 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(reloc
->r_symbolnum());
5046 return parser
.nameFromSymbol(sym
);
5049 const pint_t
* content
= (pint_t
*)(this->file().fileContent() + this->_machOSection
->offset() + reloc
->r_address());
5050 pint_t personalityAddr
= *content
;
5051 Section
<arm64
>* personalitySection
= parser
.sectionForAddress(personalityAddr
);
5052 (void)personalitySection
;
5053 assert((personalitySection
->type() == ld::Section::typeCode
) && "personality column in __compact_unwind section is not pointer to function");
5054 // atoms may not be constructed yet, so scan symbol table for labels
5055 const char* name
= parser
.scanSymbolTableForAddress(personalityAddr
);
5062 #if SUPPORT_ARCH_arm_any
5064 const char* CUSection
<arm
>::personalityName(class Parser
<arm
>& parser
, const macho_relocation_info
<arm::P
>* reloc
)
5066 if ( reloc
->r_extern() ) {
5067 assert((reloc
->r_type() == ARM_RELOC_VANILLA
) && "wrong reloc type on personality column in __compact_unwind section");
5068 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(reloc
->r_symbolnum());
5069 return parser
.nameFromSymbol(sym
);
5072 // support __LD, __compact_unwind personality entries which are pointer to personality non-lazy pointer
5073 const pint_t
* content
= (pint_t
*)(this->file().fileContent() + this->_machOSection
->offset() + reloc
->r_address());
5074 pint_t nlPointerAddr
= *content
;
5075 Section
<arm
>* nlSection
= parser
.sectionForAddress(nlPointerAddr
);
5076 if ( nlSection
->type() == ld::Section::typeCode
) {
5077 // personality function is defined in this .o file, so this is a direct reference to it
5078 // atoms may not be constructed yet, so scan symbol table for labels
5079 const char* name
= parser
.scanSymbolTableForAddress(nlPointerAddr
);
5083 uint32_t symIndex
= parser
.symbolIndexFromIndirectSectionAddress(nlPointerAddr
, nlSection
->machoSection());
5084 const macho_nlist
<P
>& nlSymbol
= parser
.symbolFromIndex(symIndex
);
5085 return parser
.nameFromSymbol(nlSymbol
);
5092 template <typename A
>
5093 const char* CUSection
<A
>::personalityName(class Parser
<A
>& parser
, const macho_relocation_info
<P
>* reloc
)
5099 bool CUSection
<x86
>::encodingMeansUseDwarf(compact_unwind_encoding_t enc
)
5101 return ((enc
& UNWIND_X86_MODE_MASK
) == UNWIND_X86_MODE_DWARF
);
5105 bool CUSection
<x86_64
>::encodingMeansUseDwarf(compact_unwind_encoding_t enc
)
5107 return ((enc
& UNWIND_X86_64_MODE_MASK
) == UNWIND_X86_64_MODE_DWARF
);
5110 #if SUPPORT_ARCH_arm_any
5112 bool CUSection
<arm
>::encodingMeansUseDwarf(compact_unwind_encoding_t enc
)
5114 return ((enc
& UNWIND_ARM_MODE_MASK
) == UNWIND_ARM_MODE_DWARF
);
5118 #if SUPPORT_ARCH_arm64
5120 bool CUSection
<arm64
>::encodingMeansUseDwarf(compact_unwind_encoding_t enc
)
5122 return ((enc
& UNWIND_ARM64_MODE_MASK
) == UNWIND_ARM64_MODE_DWARF
);
5127 template <typename A
>
5128 int CUSection
<A
>::infoSorter(const void* l
, const void* r
)
5130 // sort references by symbol index, then address
5131 const Info
* left
= (Info
*)l
;
5132 const Info
* right
= (Info
*)r
;
5133 if ( left
->functionSymbolIndex
== right
->functionSymbolIndex
)
5134 return (left
->functionStartAddress
- right
->functionStartAddress
);
5136 return (left
->functionSymbolIndex
- right
->functionSymbolIndex
);
5139 template <typename A
>
5140 void CUSection
<A
>::parse(class Parser
<A
>& parser
, uint32_t cnt
, Info array
[])
5142 // walk section content and copy to Info array
5143 const macho_compact_unwind_entry
<P
>* const entries
= (macho_compact_unwind_entry
<P
>*)(this->file().fileContent() + this->_machOSection
->offset());
5144 for (uint32_t i
=0; i
< cnt
; ++i
) {
5145 Info
* info
= &array
[i
];
5146 const macho_compact_unwind_entry
<P
>* entry
= &entries
[i
];
5147 info
->functionStartAddress
= entry
->codeStart();
5148 info
->functionSymbolIndex
= 0xFFFFFFFF;
5149 info
->rangeLength
= entry
->codeLen();
5150 info
->compactUnwindInfo
= entry
->compactUnwindInfo();
5151 info
->personality
= NULL
;
5152 info
->lsdaAddress
= entry
->lsda();
5153 info
->function
= NULL
;
5155 if ( (info
->compactUnwindInfo
& UNWIND_PERSONALITY_MASK
) != 0 )
5156 warning("no bits should be set in UNWIND_PERSONALITY_MASK of compact unwind encoding in __LD,__compact_unwind section");
5157 if ( info
->lsdaAddress
!= 0 ) {
5158 info
->compactUnwindInfo
|= UNWIND_HAS_LSDA
;
5162 // scan relocs, extern relocs are needed for personality references (possibly for function/lsda refs??)
5163 const uint32_t sectionSize
= this->_machOSection
->size();
5164 const macho_relocation_info
<P
>* relocs
= (macho_relocation_info
<P
>*)(this->file().fileContent() + this->_machOSection
->reloff());
5165 const macho_relocation_info
<P
>* relocsEnd
= &relocs
[this->_machOSection
->nreloc()];
5166 for (const macho_relocation_info
<P
>* reloc
= relocs
; reloc
< relocsEnd
; ++reloc
) {
5167 if ( reloc
->r_address() & R_SCATTERED
)
5169 if ( reloc
->r_address() > sectionSize
)
5170 throwf("malformed __compact_unwind relocation, offset (0x%08X) is beyond end of section,", reloc
->r_address());
5171 if ( reloc
->r_extern() ) {
5172 // only expect external relocs on some colummns
5173 if ( (reloc
->r_address() % sizeof(macho_compact_unwind_entry
<P
>)) == macho_compact_unwind_entry
<P
>::personalityFieldOffset() ) {
5174 uint32_t entryIndex
= reloc
->r_address() / sizeof(macho_compact_unwind_entry
<P
>);
5175 array
[entryIndex
].personality
= this->personalityName(parser
, reloc
);
5177 else if ( (reloc
->r_address() % sizeof(macho_compact_unwind_entry
<P
>)) == macho_compact_unwind_entry
<P
>::lsdaFieldOffset() ) {
5178 uint32_t entryIndex
= reloc
->r_address() / sizeof(macho_compact_unwind_entry
<P
>);
5179 const macho_nlist
<P
>& lsdaSym
= parser
.symbolFromIndex(reloc
->r_symbolnum());
5180 if ( (lsdaSym
.n_type() & N_TYPE
) == N_SECT
)
5181 array
[entryIndex
].lsdaAddress
= lsdaSym
.n_value();
5183 warning("unexpected extern relocation to lsda in __compact_unwind section");
5185 else if ( (reloc
->r_address() % sizeof(macho_compact_unwind_entry
<P
>)) == macho_compact_unwind_entry
<P
>::codeStartFieldOffset() ) {
5186 uint32_t entryIndex
= reloc
->r_address() / sizeof(macho_compact_unwind_entry
<P
>);
5187 array
[entryIndex
].functionSymbolIndex
= reloc
->r_symbolnum();
5188 array
[entryIndex
].functionStartAddress
+= parser
.symbolFromIndex(reloc
->r_symbolnum()).n_value();
5191 warning("unexpected extern relocation in __compact_unwind section");
5195 if ( (reloc
->r_address() % sizeof(macho_compact_unwind_entry
<P
>)) == macho_compact_unwind_entry
<P
>::personalityFieldOffset() ) {
5196 uint32_t entryIndex
= reloc
->r_address() / sizeof(macho_compact_unwind_entry
<P
>);
5197 array
[entryIndex
].personality
= this->personalityName(parser
, reloc
);
5202 // sort array by function start address so unwind infos will be contiguous for a given function
5203 ::qsort(array
, cnt
, sizeof(Info
), infoSorter
);
5206 template <typename A
>
5207 uint32_t CUSection
<A
>::count()
5209 const macho_section
<P
>* machoSect
= this->machoSection();
5210 if ( (machoSect
->size() % sizeof(macho_compact_unwind_entry
<P
>)) != 0 )
5211 throw "malformed __LD,__compact_unwind section, bad length";
5213 return machoSect
->size() / sizeof(macho_compact_unwind_entry
<P
>);
5216 template <typename A
>
5217 void CUSection
<A
>::makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
& cus
)
5219 Info
* const arrayStart
= cus
.cuArray
;
5220 Info
* const arrayEnd
= &cus
.cuArray
[cus
.cuCount
];
5221 for (Info
* info
=arrayStart
; info
< arrayEnd
; ++info
) {
5222 // find function atom from address
5223 info
->function
= parser
.findAtomByAddress(info
->functionStartAddress
);
5224 // find lsda atom from address
5225 if ( info
->lsdaAddress
!= 0 ) {
5226 info
->lsda
= parser
.findAtomByAddress(info
->lsdaAddress
);
5227 // add lsda subordinate
5228 typename Parser
<A
>::SourceLocation
src(info
->function
, info
->functionStartAddress
- info
->function
->objectAddress());
5229 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindNoneGroupSubordinateLSDA
, info
->lsda
);
5231 if ( info
->personality
!= NULL
) {
5232 // add personality subordinate
5233 typename Parser
<A
>::SourceLocation
src(info
->function
, info
->functionStartAddress
- info
->function
->objectAddress());
5234 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindNoneGroupSubordinatePersonality
, false, info
->personality
);
5240 template <typename A
>
5241 SymboledSection
<A
>::SymboledSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
5242 : Section
<A
>(f
, s
), _type(ld::Atom::typeUnclassified
)
5244 switch ( s
->flags() & SECTION_TYPE
) {
5246 _type
= ld::Atom::typeZeroFill
;
5248 case S_MOD_INIT_FUNC_POINTERS
:
5249 _type
= ld::Atom::typeInitializerPointers
;
5251 case S_MOD_TERM_FUNC_POINTERS
:
5252 _type
= ld::Atom::typeTerminatorPointers
;
5254 case S_THREAD_LOCAL_VARIABLES
:
5255 _type
= ld::Atom::typeTLV
;
5257 case S_THREAD_LOCAL_ZEROFILL
:
5258 _type
= ld::Atom::typeTLVZeroFill
;
5260 case S_THREAD_LOCAL_REGULAR
:
5261 _type
= ld::Atom::typeTLVInitialValue
;
5263 case S_THREAD_LOCAL_INIT_FUNCTION_POINTERS
:
5264 _type
= ld::Atom::typeTLVInitializerPointers
;
5267 if ( strncmp(s
->sectname(), "__gcc_except_tab", 16) == 0 )
5268 _type
= ld::Atom::typeLSDA
;
5269 else if ( this->type() == ld::Section::typeInitializerPointers
)
5270 _type
= ld::Atom::typeInitializerPointers
;
5276 template <typename A
>
5277 bool SymboledSection
<A
>::dontDeadStrip()
5280 case ld::Atom::typeInitializerPointers
:
5281 case ld::Atom::typeTerminatorPointers
:
5284 // model an object file without MH_SUBSECTIONS_VIA_SYMBOLS as one in which nothing can be dead stripped
5285 if ( ! this->_file
.canScatterAtoms() )
5288 return Section
<A
>::dontDeadStrip();
5294 template <typename A
>
5295 uint32_t SymboledSection
<A
>::computeAtomCount(class Parser
<A
>& parser
,
5296 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
5297 const struct Parser
<A
>::CFI_CU_InfoArrays
&)
5299 const pint_t startAddr
= this->_machOSection
->addr();
5300 const pint_t endAddr
= startAddr
+ this->_machOSection
->size();
5301 const uint32_t sectNum
= this->sectionNum(parser
);
5306 const macho_nlist
<P
>* sym
;
5307 while ( it
.next(parser
, *this, sectNum
, startAddr
, endAddr
, &addr
, &size
, &sym
) ) {
5310 //fprintf(stderr, "computeAtomCount(%s,%s) => %d\n", this->segmentName(), this->sectionName(), count);
5314 template <typename A
>
5315 uint32_t SymboledSection
<A
>::appendAtoms(class Parser
<A
>& parser
, uint8_t* p
,
5316 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
5317 const struct Parser
<A
>::CFI_CU_InfoArrays
&)
5319 this->_beginAtoms
= (Atom
<A
>*)p
;
5321 //fprintf(stderr, "SymboledSection::appendAtoms() in section %s\n", this->_machOSection->sectname());
5322 const pint_t startAddr
= this->_machOSection
->addr();
5323 const pint_t endAddr
= startAddr
+ this->_machOSection
->size();
5324 const uint32_t sectNum
= this->sectionNum(parser
);
5329 const macho_nlist
<P
>* label
;
5330 while ( it
.next(parser
, *this, sectNum
, startAddr
, endAddr
, &addr
, &size
, &label
) ) {
5331 Atom
<A
>* allocatedSpace
= (Atom
<A
>*)p
;
5332 // is break because of label or CFI?
5333 if ( label
!= NULL
) {
5334 // The size is computed based on the address of the next label (or the end of the section for the last label)
5335 // If there are two labels at the same address, we want them one to be an alias of the other.
5336 // If the label is at the end of a section, it is has zero size, but is not an alias
5337 const bool isAlias
= ( (size
== 0) && (addr
< endAddr
) );
5338 new (allocatedSpace
) Atom
<A
>(*this, parser
, *label
, size
, isAlias
);
5340 this->_hasAliases
= true;
5341 if ( parser
.altEntryFromSymbol(*label
) )
5342 this->_altEntries
.insert(allocatedSpace
);
5345 ld::Atom::SymbolTableInclusion inclusion
= ld::Atom::symbolTableNotIn
;
5346 ld::Atom::ContentType ctype
= this->contentType();
5347 if ( ctype
== ld::Atom::typeLSDA
)
5348 inclusion
= ld::Atom::symbolTableInWithRandomAutoStripLabel
;
5349 new (allocatedSpace
) Atom
<A
>(*this, "anon", addr
, size
, ld::Atom::definitionRegular
, ld::Atom::combineNever
,
5350 ld::Atom::scopeTranslationUnit
, ctype
, inclusion
,
5351 this->dontDeadStrip(), false, false, this->alignmentForAddress(addr
));
5353 p
+= sizeof(Atom
<A
>);
5357 this->_endAtoms
= (Atom
<A
>*)p
;
5363 ld::Atom::SymbolTableInclusion ImplicitSizeSection
<arm64
>::symbolTableInclusion()
5365 return ld::Atom::symbolTableInWithRandomAutoStripLabel
;
5369 template <typename A
>
5370 ld::Atom::SymbolTableInclusion ImplicitSizeSection
<A
>::symbolTableInclusion()
5372 return ld::Atom::symbolTableNotIn
;
5376 template <typename A
>
5377 uint32_t ImplicitSizeSection
<A
>::computeAtomCount(class Parser
<A
>& parser
,
5378 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
5379 const struct Parser
<A
>::CFI_CU_InfoArrays
&)
5382 const macho_section
<P
>* sect
= this->machoSection();
5383 const pint_t startAddr
= sect
->addr();
5384 const pint_t endAddr
= startAddr
+ sect
->size();
5385 for (pint_t addr
= startAddr
; addr
< endAddr
; addr
+= elementSizeAtAddress(addr
) ) {
5386 if ( useElementAt(parser
, it
, addr
) )
5389 if ( it
.fileHasOverlappingSymbols
&& (sect
->size() != 0) && (this->combine(parser
, startAddr
) == ld::Atom::combineByNameAndContent
) ) {
5390 // if there are multiple labels in this section for the same address, then clone them into multi atoms
5391 pint_t prevSymbolAddr
= (pint_t
)(-1);
5392 uint8_t prevSymbolSectNum
= 0;
5393 bool prevIgnore
= false;
5394 for(uint32_t i
=0; i
< it
.sortedSymbolCount
; ++i
) {
5395 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(it
.sortedSymbolIndexes
[i
]);
5396 const pint_t symbolAddr
= sym
.n_value();
5397 const uint8_t symbolSectNum
= sym
.n_sect();
5398 const bool ignore
= this->ignoreLabel(parser
.nameFromSymbol(sym
));
5399 if ( !ignore
&& !prevIgnore
&& (symbolAddr
== prevSymbolAddr
) && (prevSymbolSectNum
== symbolSectNum
) && (symbolSectNum
== this->sectionNum(parser
)) ) {
5402 prevSymbolAddr
= symbolAddr
;
5403 prevSymbolSectNum
= symbolSectNum
;
5404 prevIgnore
= ignore
;
5410 template <typename A
>
5411 uint32_t ImplicitSizeSection
<A
>::appendAtoms(class Parser
<A
>& parser
, uint8_t* p
,
5412 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
5413 const struct Parser
<A
>::CFI_CU_InfoArrays
&)
5415 this->_beginAtoms
= (Atom
<A
>*)p
;
5417 const macho_section
<P
>* sect
= this->machoSection();
5418 const pint_t startAddr
= sect
->addr();
5419 const pint_t endAddr
= startAddr
+ sect
->size();
5420 const uint32_t sectNum
= this->sectionNum(parser
);
5421 //fprintf(stderr, "ImplicitSizeSection::appendAtoms() in section %s\n", sect->sectname());
5425 const macho_nlist
<P
>* foundLabel
;
5426 Atom
<A
>* allocatedSpace
;
5427 while ( it
.next(parser
, *this, sectNum
, startAddr
, endAddr
, &foundAddr
, &size
, &foundLabel
) ) {
5428 if ( foundLabel
!= NULL
) {
5430 pint_t labeledAtomSize
= this->elementSizeAtAddress(foundAddr
);
5431 allocatedSpace
= (Atom
<A
>*)p
;
5432 if ( this->ignoreLabel(parser
.nameFromSymbol(*foundLabel
)) ) {
5434 // <rdar://problem/10018737>
5435 // a size of zero means there is another label at same location
5436 // and we are supposed to ignore this label
5440 //fprintf(stderr, " 0x%08llX make annon, size=%lld\n", (uint64_t)foundAddr, (uint64_t)size);
5441 new (allocatedSpace
) Atom
<A
>(*this, this->unlabeledAtomName(parser
, foundAddr
), foundAddr
,
5442 this->elementSizeAtAddress(foundAddr
), this->definition(),
5443 this->combine(parser
, foundAddr
), this->scopeAtAddress(parser
, foundAddr
),
5444 this->contentType(), this->symbolTableInclusion(),
5445 this->dontDeadStrip(), false, false, this->alignmentForAddress(foundAddr
));
5449 // make named atom for label
5450 //fprintf(stderr, " 0x%08llX make labeled\n", (uint64_t)foundAddr);
5451 new (allocatedSpace
) Atom
<A
>(*this, parser
, *foundLabel
, labeledAtomSize
);
5455 p
+= sizeof(Atom
<A
>);
5456 foundAddr
+= labeledAtomSize
;
5457 size
-= labeledAtomSize
;
5460 // some number of anonymous atoms
5461 for (pint_t addr
= foundAddr
; addr
< (foundAddr
+size
); addr
+= elementSizeAtAddress(addr
) ) {
5462 // make anon atoms for area before label
5463 if ( this->useElementAt(parser
, it
, addr
) ) {
5464 //fprintf(stderr, " 0x%08llX make annon, size=%lld\n", (uint64_t)addr, (uint64_t)elementSizeAtAddress(addr));
5465 allocatedSpace
= (Atom
<A
>*)p
;
5466 new (allocatedSpace
) Atom
<A
>(*this, this->unlabeledAtomName(parser
, addr
), addr
, this->elementSizeAtAddress(addr
),
5467 this->definition(), this->combine(parser
, addr
), this->scopeAtAddress(parser
, addr
),
5468 this->contentType(), this->symbolTableInclusion(),
5469 this->dontDeadStrip(), false, false, this->alignmentForAddress(addr
));
5471 p
+= sizeof(Atom
<A
>);
5476 this->_endAtoms
= (Atom
<A
>*)p
;
5481 template <typename A
>
5482 bool Literal4Section
<A
>::ignoreLabel(const char* label
) const
5484 return (label
[0] == 'L') || (label
[0] == 'l');
5487 template <typename A
>
5488 unsigned long Literal4Section
<A
>::contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
5490 const uint32_t* literalContent
= (uint32_t*)atom
->contentPointer();
5491 return *literalContent
;
5494 template <typename A
>
5495 bool Literal4Section
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
5496 const ld::IndirectBindingTable
& ind
) const
5498 assert(this->type() == rhs
.section().type());
5499 const uint32_t* literalContent
= (uint32_t*)atom
->contentPointer();
5501 const Atom
<A
>* rhsAtom
= dynamic_cast<const Atom
<A
>*>(&rhs
);
5502 assert(rhsAtom
!= NULL
);
5503 if ( rhsAtom
!= NULL
) {
5504 const uint32_t* rhsLiteralContent
= (uint32_t*)rhsAtom
->contentPointer();
5505 return (*literalContent
== *rhsLiteralContent
);
5511 template <typename A
>
5512 bool Literal8Section
<A
>::ignoreLabel(const char* label
) const
5514 return (label
[0] == 'L') || (label
[0] == 'l');
5517 template <typename A
>
5518 unsigned long Literal8Section
<A
>::contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
5521 const uint64_t* literalContent
= (uint64_t*)atom
->contentPointer();
5522 return *literalContent
;
5524 unsigned long hash
= 5381;
5525 const uint8_t* byteContent
= atom
->contentPointer();
5526 for (int i
=0; i
< 8; ++i
) {
5527 hash
= hash
* 33 + byteContent
[i
];
5533 template <typename A
>
5534 bool Literal8Section
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
5535 const ld::IndirectBindingTable
& ind
) const
5537 if ( rhs
.section().type() != ld::Section::typeLiteral8
)
5539 assert(this->type() == rhs
.section().type());
5540 const uint64_t* literalContent
= (uint64_t*)atom
->contentPointer();
5542 const Atom
<A
>* rhsAtom
= dynamic_cast<const Atom
<A
>*>(&rhs
);
5543 assert(rhsAtom
!= NULL
);
5544 if ( rhsAtom
!= NULL
) {
5545 const uint64_t* rhsLiteralContent
= (uint64_t*)rhsAtom
->contentPointer();
5546 return (*literalContent
== *rhsLiteralContent
);
5551 template <typename A
>
5552 bool Literal16Section
<A
>::ignoreLabel(const char* label
) const
5554 return (label
[0] == 'L') || (label
[0] == 'l');
5557 template <typename A
>
5558 unsigned long Literal16Section
<A
>::contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
5560 unsigned long hash
= 5381;
5561 const uint8_t* byteContent
= atom
->contentPointer();
5562 for (int i
=0; i
< 16; ++i
) {
5563 hash
= hash
* 33 + byteContent
[i
];
5568 template <typename A
>
5569 bool Literal16Section
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
5570 const ld::IndirectBindingTable
& ind
) const
5572 if ( rhs
.section().type() != ld::Section::typeLiteral16
)
5574 assert(this->type() == rhs
.section().type());
5575 const uint64_t* literalContent
= (uint64_t*)atom
->contentPointer();
5577 const Atom
<A
>* rhsAtom
= dynamic_cast<const Atom
<A
>*>(&rhs
);
5578 assert(rhsAtom
!= NULL
);
5579 if ( rhsAtom
!= NULL
) {
5580 const uint64_t* rhsLiteralContent
= (uint64_t*)rhsAtom
->contentPointer();
5581 return ((literalContent
[0] == rhsLiteralContent
[0]) && (literalContent
[1] == rhsLiteralContent
[1]));
5588 template <typename A
>
5589 typename
A::P::uint_t CStringSection
<A
>::elementSizeAtAddress(pint_t addr
)
5591 const macho_section
<P
>* sect
= this->machoSection();
5592 const char* stringContent
= (char*)(this->file().fileContent() + sect
->offset() + addr
- sect
->addr());
5593 return strlen(stringContent
) + 1;
5596 template <typename A
>
5597 bool CStringSection
<A
>::useElementAt(Parser
<A
>& parser
, struct Parser
<A
>::LabelAndCFIBreakIterator
& it
, pint_t addr
)
5602 template <typename A
>
5603 bool CStringSection
<A
>::ignoreLabel(const char* label
) const
5605 return (label
[0] == 'L') || (label
[0] == 'l');
5609 template <typename A
>
5610 Atom
<A
>* CStringSection
<A
>::findAtomByAddress(pint_t addr
)
5612 Atom
<A
>* result
= this->findContentAtomByAddress(addr
, this->_beginAtoms
, this->_endAtoms
);
5616 template <typename A
>
5617 unsigned long CStringSection
<A
>::contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
5619 unsigned long hash
= 5381;
5620 const char* stringContent
= (char*)atom
->contentPointer();
5621 for (const char* s
= stringContent
; *s
!= '\0'; ++s
) {
5622 hash
= hash
* 33 + *s
;
5628 template <typename A
>
5629 bool CStringSection
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
5630 const ld::IndirectBindingTable
& ind
) const
5632 if ( rhs
.section().type() != ld::Section::typeCString
)
5634 assert(this->type() == rhs
.section().type());
5635 assert(strcmp(this->sectionName(), rhs
.section().sectionName())== 0);
5636 assert(strcmp(this->segmentName(), rhs
.section().segmentName())== 0);
5637 const char* stringContent
= (char*)atom
->contentPointer();
5639 const Atom
<A
>* rhsAtom
= dynamic_cast<const Atom
<A
>*>(&rhs
);
5640 assert(rhsAtom
!= NULL
);
5641 if ( rhsAtom
!= NULL
) {
5642 if ( atom
->_size
!= rhsAtom
->_size
)
5644 const char* rhsStringContent
= (char*)rhsAtom
->contentPointer();
5645 return (strcmp(stringContent
, rhsStringContent
) == 0);
5652 ld::Fixup::Kind NonLazyPointerSection
<x86
>::fixupKind()
5654 return ld::Fixup::kindStoreLittleEndian32
;
5658 ld::Fixup::Kind NonLazyPointerSection
<arm
>::fixupKind()
5660 return ld::Fixup::kindStoreLittleEndian32
;
5664 ld::Fixup::Kind NonLazyPointerSection
<arm64
>::fixupKind()
5666 return ld::Fixup::kindStoreLittleEndian64
;
5671 void NonLazyPointerSection
<x86_64
>::makeFixups(class Parser
<x86_64
>& parser
, const struct Parser
<x86_64
>::CFI_CU_InfoArrays
&)
5673 assert(0 && "x86_64 should not have non-lazy-pointer sections in .o files");
5676 template <typename A
>
5677 void NonLazyPointerSection
<A
>::makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
&)
5679 // add references for each NLP atom based on indirect symbol table
5680 const macho_section
<P
>* sect
= this->machoSection();
5681 const pint_t endAddr
= sect
->addr() + sect
->size();
5682 for( pint_t addr
= sect
->addr(); addr
< endAddr
; addr
+= sizeof(pint_t
)) {
5683 typename Parser
<A
>::SourceLocation src
;
5684 typename Parser
<A
>::TargetDesc target
;
5685 src
.atom
= this->findAtomByAddress(addr
);
5686 src
.offsetInAtom
= 0;
5687 uint32_t symIndex
= parser
.symbolIndexFromIndirectSectionAddress(addr
, sect
);
5690 target
.weakImport
= false;
5692 if ( symIndex
== INDIRECT_SYMBOL_LOCAL
) {
5693 // use direct reference for local symbols
5694 const pint_t
* nlpContent
= (pint_t
*)(this->file().fileContent() + sect
->offset() + addr
- sect
->addr());
5695 pint_t targetAddr
= P::getP(*nlpContent
);
5696 target
.atom
= parser
.findAtomByAddress(targetAddr
);
5697 target
.weakImport
= false;
5698 target
.addend
= (targetAddr
- target
.atom
->objectAddress());
5699 // <rdar://problem/8385011> if pointer to thumb function, mask of thumb bit (not an addend of +1)
5700 if ( target
.atom
->isThumb() )
5701 target
.addend
&= (-2);
5702 assert(src
.atom
->combine() == ld::Atom::combineNever
);
5705 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(symIndex
);
5706 // use direct reference for local symbols
5707 if ( ((sym
.n_type() & N_TYPE
) == N_SECT
) && ((sym
.n_type() & N_EXT
) == 0) ) {
5708 parser
.findTargetFromAddressAndSectionNum(sym
.n_value(), sym
.n_sect(), target
);
5709 assert(src
.atom
->combine() == ld::Atom::combineNever
);
5712 target
.name
= parser
.nameFromSymbol(sym
);
5713 target
.weakImport
= parser
.weakImportFromSymbol(sym
);
5714 assert(src
.atom
->combine() == ld::Atom::combineByNameAndReferences
);
5717 parser
.addFixups(src
, this->fixupKind(), target
);
5721 template <typename A
>
5722 ld::Atom::Combine NonLazyPointerSection
<A
>::combine(Parser
<A
>& parser
, pint_t addr
)
5724 const macho_section
<P
>* sect
= this->machoSection();
5725 uint32_t symIndex
= parser
.symbolIndexFromIndirectSectionAddress(addr
, sect
);
5726 if ( symIndex
== INDIRECT_SYMBOL_LOCAL
)
5727 return ld::Atom::combineNever
;
5729 // don't coalesce non-lazy-pointers to local symbols
5730 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(symIndex
);
5731 if ( ((sym
.n_type() & N_TYPE
) == N_SECT
) && ((sym
.n_type() & N_EXT
) == 0) )
5732 return ld::Atom::combineNever
;
5734 return ld::Atom::combineByNameAndReferences
;
5737 template <typename A
>
5738 const char* NonLazyPointerSection
<A
>::targetName(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
)
5740 assert(atom
->combine() == ld::Atom::combineByNameAndReferences
);
5741 assert(atom
->fixupCount() == 1);
5742 ld::Fixup::iterator fit
= atom
->fixupsBegin();
5743 const char* name
= NULL
;
5744 switch ( fit
->binding
) {
5745 case ld::Fixup::bindingByNameUnbound
:
5748 case ld::Fixup::bindingByContentBound
:
5749 name
= fit
->u
.target
->name();
5751 case ld::Fixup::bindingsIndirectlyBound
:
5752 name
= ind
.indirectName(fit
->u
.bindingIndex
);
5757 assert(name
!= NULL
);
5761 template <typename A
>
5762 unsigned long NonLazyPointerSection
<A
>::contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
5764 assert(atom
->combine() == ld::Atom::combineByNameAndReferences
);
5765 unsigned long hash
= 9508;
5766 for (const char* s
= this->targetName(atom
, ind
); *s
!= '\0'; ++s
) {
5767 hash
= hash
* 33 + *s
;
5772 template <typename A
>
5773 bool NonLazyPointerSection
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
5774 const ld::IndirectBindingTable
& indirectBindingTable
) const
5776 if ( rhs
.section().type() != ld::Section::typeNonLazyPointer
)
5778 assert(this->type() == rhs
.section().type());
5779 // there can be many non-lazy pointer in different section names
5780 // we only want to coalesce in same section name
5781 if ( *this != rhs
.section() )
5783 const Atom
<A
>* rhsAtom
= dynamic_cast<const Atom
<A
>*>(&rhs
);
5784 assert(rhsAtom
!= NULL
);
5785 const char* thisName
= this->targetName(atom
, indirectBindingTable
);
5786 const char* rhsName
= this->targetName(rhsAtom
, indirectBindingTable
);
5787 return (strcmp(thisName
, rhsName
) == 0);
5790 template <typename A
>
5791 ld::Atom::Scope NonLazyPointerSection
<A
>::scopeAtAddress(Parser
<A
>& parser
, pint_t addr
)
5793 const macho_section
<P
>* sect
= this->machoSection();
5794 uint32_t symIndex
= parser
.symbolIndexFromIndirectSectionAddress(addr
, sect
);
5795 if ( symIndex
== INDIRECT_SYMBOL_LOCAL
)
5796 return ld::Atom::scopeTranslationUnit
;
5798 return ld::Atom::scopeLinkageUnit
;
5803 template <typename A
>
5804 ld::Atom::Combine TLVPointerSection
<A
>::combine(Parser
<A
>& parser
, pint_t addr
)
5806 return ld::Atom::combineByNameAndReferences
;
5810 void TLVPointerSection
<arm
>::makeFixups(class Parser
<arm
>& parser
, const struct Parser
<arm
>::CFI_CU_InfoArrays
&)
5812 // add references for each thread local pointer atom based on indirect symbol table
5813 const macho_section
<P
>* sect
= this->machoSection();
5814 const pint_t endAddr
= sect
->addr() + sect
->size();
5815 for (pint_t addr
= sect
->addr(); addr
< endAddr
; addr
+= sizeof(pint_t
)) {
5816 typename Parser
<arm
>::SourceLocation src
;
5817 typename Parser
<arm
>::TargetDesc target
;
5818 src
.atom
= this->findAtomByAddress(addr
);
5819 src
.offsetInAtom
= 0;
5820 uint32_t symIndex
= parser
.symbolIndexFromIndirectSectionAddress(addr
, sect
);
5823 target
.weakImport
= false;
5825 if ( symIndex
== INDIRECT_SYMBOL_LOCAL
) {
5826 throwf("unexpected INDIRECT_SYMBOL_LOCAL in section %s", this->sectionName());
5829 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(symIndex
);
5830 // use direct reference for local symbols
5831 if ( ((sym
.n_type() & N_TYPE
) == N_SECT
) && ((sym
.n_type() & N_EXT
) == 0) ) {
5832 throwf("unexpected pointer to local symbol in section %s", this->sectionName());
5835 target
.name
= parser
.nameFromSymbol(sym
);
5836 target
.weakImport
= parser
.weakImportFromSymbol(sym
);
5837 assert(src
.atom
->combine() == ld::Atom::combineByNameAndReferences
);
5840 parser
.addFixups(src
, ld::Fixup::kindStoreLittleEndian32
, target
);
5844 template <typename A
>
5845 void TLVPointerSection
<A
>::makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
&)
5847 assert(0 && "should not have thread-local-pointer sections in .o files");
5851 template <typename A
>
5852 const char* TLVPointerSection
<A
>::targetName(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
, bool* isStatic
)
5854 assert(atom
->combine() == ld::Atom::combineByNameAndReferences
);
5855 assert(atom
->fixupCount() == 1);
5857 ld::Fixup::iterator fit
= atom
->fixupsBegin();
5858 const char* name
= NULL
;
5859 switch ( fit
->binding
) {
5860 case ld::Fixup::bindingByNameUnbound
:
5863 case ld::Fixup::bindingByContentBound
:
5864 name
= fit
->u
.target
->name();
5866 case ld::Fixup::bindingsIndirectlyBound
:
5867 name
= ind
.indirectName(fit
->u
.bindingIndex
);
5869 case ld::Fixup::bindingDirectlyBound
:
5870 name
= fit
->u
.target
->name();
5871 *isStatic
= (fit
->u
.target
->scope() == ld::Atom::scopeTranslationUnit
);
5876 assert(name
!= NULL
);
5880 template <typename A
>
5881 unsigned long TLVPointerSection
<A
>::contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
5883 assert(atom
->combine() == ld::Atom::combineByNameAndReferences
);
5884 unsigned long hash
= 9508;
5886 for (const char* s
= this->targetName(atom
, ind
, &isStatic
); *s
!= '\0'; ++s
) {
5887 hash
= hash
* 33 + *s
;
5892 template <typename A
>
5893 bool TLVPointerSection
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
5894 const ld::IndirectBindingTable
& indirectBindingTable
) const
5896 if ( rhs
.section().type() != ld::Section::typeTLVPointers
)
5898 assert(this->type() == rhs
.section().type());
5899 const Atom
<A
>* rhsAtom
= dynamic_cast<const Atom
<A
>*>(&rhs
);
5900 assert(rhsAtom
!= NULL
);
5903 const char* thisName
= this->targetName(atom
, indirectBindingTable
, &thisIsStatic
);
5904 const char* rhsName
= this->targetName(rhsAtom
, indirectBindingTable
, &rhsIsStatic
);
5905 return !thisIsStatic
&& !rhsIsStatic
&& (strcmp(thisName
, rhsName
) == 0);
5909 template <typename A
>
5910 const uint8_t* CFStringSection
<A
>::targetContent(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
,
5911 ContentType
* ct
, unsigned int* count
)
5913 *ct
= contentUnknown
;
5914 for (ld::Fixup::iterator fit
=atom
->fixupsBegin(), end
=atom
->fixupsEnd(); fit
!= end
; ++fit
) {
5915 const ld::Atom
* targetAtom
= NULL
;
5916 switch ( fit
->binding
) {
5917 case ld::Fixup::bindingByNameUnbound
:
5918 // ignore reference to ___CFConstantStringClassReference
5919 // we are just looking for reference to backing string data
5920 assert(fit
->offsetInAtom
== 0);
5921 assert(strcmp(fit
->u
.name
, "___CFConstantStringClassReference") == 0);
5923 case ld::Fixup::bindingDirectlyBound
:
5924 case ld::Fixup::bindingByContentBound
:
5925 targetAtom
= fit
->u
.target
;
5927 case ld::Fixup::bindingsIndirectlyBound
:
5928 targetAtom
= ind
.indirectAtom(fit
->u
.bindingIndex
);
5931 assert(0 && "bad binding type");
5933 assert(targetAtom
!= NULL
);
5934 const Atom
<A
>* target
= dynamic_cast<const Atom
<A
>*>(targetAtom
);
5935 if ( targetAtom
->section().type() == ld::Section::typeCString
) {
5937 *count
= targetAtom
->size();
5939 else if ( targetAtom
->section().type() == ld::Section::typeUTF16Strings
) {
5941 *count
= (targetAtom
->size()+1)/2; // round up incase of buggy compiler that has only one trailing zero byte
5944 *ct
= contentUnknown
;
5948 return target
->contentPointer();
5954 template <typename A
>
5955 unsigned long CFStringSection
<A
>::contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
5957 // base hash of CFString on hash of cstring it wraps
5960 unsigned int charCount
;
5961 const uint8_t* content
= this->targetContent(atom
, ind
, &cType
, &charCount
);
5965 for (const char* s
= (char*)content
; *s
!= '\0'; ++s
) {
5966 hash
= hash
* 33 + *s
;
5971 --charCount
; // don't add last 0x0000 to hash because some buggy compilers only have trailing single byte
5972 for (const uint16_t* s
= (uint16_t*)content
; charCount
> 0; ++s
, --charCount
) {
5973 hash
= hash
* 1025 + *s
;
5976 case contentUnknown
:
5977 // <rdar://problem/14134211> For malformed CFStrings, hash to address of atom so they have unique hashes
5978 return ULONG_MAX
- (unsigned long)(atom
);
5984 template <typename A
>
5985 bool CFStringSection
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
5986 const ld::IndirectBindingTable
& indirectBindingTable
) const
5990 if ( rhs
.section().type() != ld::Section::typeCFString
)
5992 assert(this->type() == rhs
.section().type());
5993 assert(strcmp(this->sectionName(), "__cfstring") == 0);
5995 ContentType thisType
;
5996 unsigned int charCount
;
5997 const uint8_t* cstringContent
= this->targetContent(atom
, indirectBindingTable
, &thisType
, &charCount
);
5998 ContentType rhsType
;
5999 const Atom
<A
>* rhsAtom
= dynamic_cast<const Atom
<A
>*>(&rhs
);
6000 assert(rhsAtom
!= NULL
);
6001 unsigned int rhsCharCount
;
6002 const uint8_t* rhsStringContent
= this->targetContent(rhsAtom
, indirectBindingTable
, &rhsType
, &rhsCharCount
);
6004 if ( thisType
!= rhsType
)
6007 if ( thisType
== contentUnknown
)
6010 if ( rhsType
== contentUnknown
)
6013 // no need to compare content of pointers are already the same
6014 if ( cstringContent
== rhsStringContent
)
6017 // no need to compare content if size is different
6018 if ( charCount
!= rhsCharCount
)
6021 switch ( thisType
) {
6023 return (strcmp((char*)cstringContent
, (char*)rhsStringContent
) == 0);
6026 const uint16_t* cstringContent16
= (uint16_t*)cstringContent
;
6027 const uint16_t* rhsStringContent16
= (uint16_t*)rhsStringContent
;
6028 for (unsigned int i
= 0; i
< charCount
; ++i
) {
6029 if ( cstringContent16
[i
] != rhsStringContent16
[i
] )
6034 case contentUnknown
:
6041 template <typename A
>
6042 typename
A::P::uint_t ObjC1ClassSection
<A
>::elementSizeAtAddress(pint_t addr
)
6044 // nominal size for each class is 48 bytes, but sometimes the compiler
6045 // over aligns and there is padding after class data
6046 const macho_section
<P
>* sct
= this->machoSection();
6047 uint32_t align
= 1 << sct
->align();
6048 uint32_t size
= ((12 * sizeof(pint_t
)) + align
-1) & (-align
);
6052 template <typename A
>
6053 const char* ObjC1ClassSection
<A
>::unlabeledAtomName(Parser
<A
>& parser
, pint_t addr
)
6055 // 8-bytes into class object is pointer to class name
6056 const macho_section
<P
>* sct
= this->machoSection();
6057 uint32_t classObjcFileOffset
= sct
->offset() - sct
->addr() + addr
;
6058 const uint8_t* mappedFileContent
= this->file().fileContent();
6059 pint_t nameAddr
= P::getP(*((pint_t
*)(mappedFileContent
+classObjcFileOffset
+2*sizeof(pint_t
))));
6061 // find section containing string address to get string bytes
6062 const macho_section
<P
>* const sections
= parser
.firstMachOSection();
6063 const uint32_t sectionCount
= parser
.machOSectionCount();
6064 for (uint32_t i
=0; i
< sectionCount
; ++i
) {
6065 const macho_section
<P
>* aSect
= §ions
[i
];
6066 if ( (aSect
->addr() <= nameAddr
) && (nameAddr
< (aSect
->addr()+aSect
->size())) ) {
6067 assert((aSect
->flags() & SECTION_TYPE
) == S_CSTRING_LITERALS
);
6068 uint32_t nameFileOffset
= aSect
->offset() - aSect
->addr() + nameAddr
;
6069 const char* name
= (char*)mappedFileContent
+ nameFileOffset
;
6070 // spin through symbol table to find absolute symbol corresponding to this class
6071 for (uint32_t s
=0; s
< parser
.symbolCount(); ++s
) {
6072 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(s
);
6073 if ( (sym
.n_type() & N_TYPE
) != N_ABS
)
6075 const char* absName
= parser
.nameFromSymbol(sym
);
6076 if ( strncmp(absName
, ".objc_class_name_", 17) == 0 ) {
6077 if ( strcmp(&absName
[17], name
) == 0 )
6081 assert(0 && "obj class name not found in symbol table");
6084 assert(0 && "obj class name not found");
6085 return "unknown objc class";
6089 template <typename A
>
6090 const char* ObjC2ClassRefsSection
<A
>::targetClassName(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
6092 assert(atom
->fixupCount() == 1);
6093 ld::Fixup::iterator fit
= atom
->fixupsBegin();
6094 const char* className
= NULL
;
6095 switch ( fit
->binding
) {
6096 case ld::Fixup::bindingByNameUnbound
:
6097 className
= fit
->u
.name
;
6099 case ld::Fixup::bindingDirectlyBound
:
6100 case ld::Fixup::bindingByContentBound
:
6101 className
= fit
->u
.target
->name();
6103 case ld::Fixup::bindingsIndirectlyBound
:
6104 className
= ind
.indirectName(fit
->u
.bindingIndex
);
6107 assert(0 && "unsupported binding in objc2 class ref section");
6109 assert(className
!= NULL
);
6114 template <typename A
>
6115 unsigned long ObjC2ClassRefsSection
<A
>::contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
6117 unsigned long hash
= 978;
6118 for (const char* s
= targetClassName(atom
, ind
); *s
!= '\0'; ++s
) {
6119 hash
= hash
* 33 + *s
;
6124 template <typename A
>
6125 bool ObjC2ClassRefsSection
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
6126 const ld::IndirectBindingTable
& indirectBindingTable
) const
6128 assert(this->type() == rhs
.section().type());
6129 const Atom
<A
>* rhsAtom
= dynamic_cast<const Atom
<A
>*>(&rhs
);
6130 assert(rhsAtom
!= NULL
);
6131 const char* thisClassName
= targetClassName(atom
, indirectBindingTable
);
6132 const char* rhsClassName
= targetClassName(rhsAtom
, indirectBindingTable
);
6133 return (strcmp(thisClassName
, rhsClassName
) == 0);
6137 template <typename A
>
6138 const char* Objc1ClassReferences
<A
>::targetCString(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
6140 assert(atom
->fixupCount() == 2);
6141 ld::Fixup::iterator fit
= atom
->fixupsBegin();
6142 if ( fit
->kind
== ld::Fixup::kindSetTargetAddress
)
6144 const ld::Atom
* targetAtom
= NULL
;
6145 switch ( fit
->binding
) {
6146 case ld::Fixup::bindingByContentBound
:
6147 targetAtom
= fit
->u
.target
;
6149 case ld::Fixup::bindingsIndirectlyBound
:
6150 targetAtom
= ind
.indirectAtom(fit
->u
.bindingIndex
);
6151 if ( targetAtom
== NULL
) {
6152 fprintf(stderr
, "missing target named %s\n", ind
.indirectName(fit
->u
.bindingIndex
));
6158 assert(targetAtom
!= NULL
);
6159 const Atom
<A
>* target
= dynamic_cast<const Atom
<A
>*>(targetAtom
);
6160 assert(target
!= NULL
);
6161 return (char*)target
->contentPointer();
6165 template <typename A
>
6166 const char* PointerToCStringSection
<A
>::targetCString(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
6168 assert(atom
->fixupCount() == 1);
6169 ld::Fixup::iterator fit
= atom
->fixupsBegin();
6170 const ld::Atom
* targetAtom
= NULL
;
6171 switch ( fit
->binding
) {
6172 case ld::Fixup::bindingByContentBound
:
6173 targetAtom
= fit
->u
.target
;
6175 case ld::Fixup::bindingsIndirectlyBound
:
6176 targetAtom
= ind
.indirectAtom(fit
->u
.bindingIndex
);
6178 case ld::Fixup::bindingDirectlyBound
:
6179 targetAtom
= fit
->u
.target
;
6182 assert(0 && "unsupported reference to selector");
6184 assert(targetAtom
!= NULL
);
6185 const Atom
<A
>* target
= dynamic_cast<const Atom
<A
>*>(targetAtom
);
6186 assert(target
!= NULL
);
6187 assert(target
->contentType() == ld::Atom::typeCString
);
6188 return (char*)target
->contentPointer();
6191 template <typename A
>
6192 unsigned long PointerToCStringSection
<A
>::contentHash(const class Atom
<A
>* atom
,
6193 const ld::IndirectBindingTable
& indirectBindingTable
) const
6195 // make hash from section name and target cstring name
6196 unsigned long hash
= 123;
6197 for (const char* s
= this->sectionName(); *s
!= '\0'; ++s
) {
6198 hash
= hash
* 33 + *s
;
6200 for (const char* s
= this->targetCString(atom
, indirectBindingTable
); *s
!= '\0'; ++s
) {
6201 hash
= hash
* 33 + *s
;
6206 template <typename A
>
6207 bool PointerToCStringSection
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
6208 const ld::IndirectBindingTable
& indirectBindingTable
) const
6210 assert(this->type() == rhs
.section().type());
6211 // there can be pointers-to-cstrings in different section names
6212 // we only want to coalesce in same section name
6213 if ( *this != rhs
.section() )
6216 // get string content for this
6217 const char* cstringContent
= this->targetCString(atom
, indirectBindingTable
);
6218 const Atom
<A
>* rhsAtom
= dynamic_cast<const Atom
<A
>*>(&rhs
);
6219 assert(rhsAtom
!= NULL
);
6220 const char* rhsCstringContent
= this->targetCString(rhsAtom
, indirectBindingTable
);
6222 assert(cstringContent
!= NULL
);
6223 assert(rhsCstringContent
!= NULL
);
6224 return (strcmp(cstringContent
, rhsCstringContent
) == 0);
6229 template <typename A
>
6230 unsigned long UTF16StringSection
<A
>::contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
6232 unsigned long hash
= 5381;
6233 const uint16_t* stringContent
= (uint16_t*)atom
->contentPointer();
6234 // some buggy compilers end utf16 data with single byte, so don't use last word in hash computation
6235 unsigned int count
= (atom
->size()/2) - 1;
6236 for (const uint16_t* s
= stringContent
; count
> 0; ++s
, --count
) {
6237 hash
= hash
* 33 + *s
;
6242 template <typename A
>
6243 bool UTF16StringSection
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
6244 const ld::IndirectBindingTable
& ind
) const
6246 if ( rhs
.section().type() != ld::Section::typeUTF16Strings
)
6259 uint32_t Section
<x86_64
>::x86_64PcRelOffset(uint8_t r_type
)
6262 case X86_64_RELOC_SIGNED
:
6264 case X86_64_RELOC_SIGNED_1
:
6266 case X86_64_RELOC_SIGNED_2
:
6268 case X86_64_RELOC_SIGNED_4
:
6276 bool Section
<x86_64
>::addRelocFixup(class Parser
<x86_64
>& parser
, const macho_relocation_info
<P
>* reloc
)
6278 const macho_section
<P
>* sect
= this->machoSection();
6279 if ( sect
== NULL
) {
6280 warning("malformed mach-o, relocations not supported on section %s", this->sectionName());
6283 uint64_t srcAddr
= sect
->addr() + reloc
->r_address();
6284 Parser
<x86_64
>::SourceLocation src
;
6285 Parser
<x86_64
>::TargetDesc target
;
6286 Parser
<x86_64
>::TargetDesc toTarget
;
6287 src
.atom
= this->findAtomByAddress(srcAddr
);
6288 if ( src
.atom
== NULL
)
6289 throwf("malformed mach-o, reloc addr 0x%llX not in any atom", srcAddr
);
6290 src
.offsetInAtom
= srcAddr
- src
.atom
->_objAddress
;
6291 const uint8_t* fixUpPtr
= file().fileContent() + sect
->offset() + reloc
->r_address();
6292 uint64_t contentValue
= 0;
6293 const macho_relocation_info
<x86_64::P
>* nextReloc
= &reloc
[1];
6294 bool result
= false;
6295 bool useDirectBinding
;
6296 switch ( reloc
->r_length() ) {
6298 contentValue
= *fixUpPtr
;
6301 contentValue
= (int64_t)(int16_t)E::get16(*((uint16_t*)fixUpPtr
));
6304 contentValue
= (int64_t)(int32_t)E::get32(*((uint32_t*)fixUpPtr
));
6307 contentValue
= E::get64(*((uint64_t*)fixUpPtr
));
6312 target
.weakImport
= false;
6314 if ( reloc
->r_extern() ) {
6315 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(reloc
->r_symbolnum());
6316 // use direct reference for local symbols
6317 if ( ((sym
.n_type() & N_TYPE
) == N_SECT
) && (((sym
.n_type() & N_EXT
) == 0) || (parser
.nameFromSymbol(sym
)[0] == 'L')) ) {
6318 parser
.findTargetFromAddressAndSectionNum(sym
.n_value(), sym
.n_sect(), target
);
6319 target
.addend
+= contentValue
;
6322 target
.name
= parser
.nameFromSymbol(sym
);
6323 target
.weakImport
= parser
.weakImportFromSymbol(sym
);
6324 target
.addend
= contentValue
;
6326 // cfstrings should always use direct reference to backing store
6327 if ( (this->type() == ld::Section::typeCFString
) && (src
.offsetInAtom
!= 0) ) {
6328 parser
.findTargetFromAddressAndSectionNum(sym
.n_value(), sym
.n_sect(), target
);
6329 target
.addend
= contentValue
;
6333 if ( reloc
->r_pcrel() )
6334 contentValue
+= srcAddr
+ x86_64PcRelOffset(reloc
->r_type());
6335 parser
.findTargetFromAddressAndSectionNum(contentValue
, reloc
->r_symbolnum(), target
);
6337 switch ( reloc
->r_type() ) {
6338 case X86_64_RELOC_UNSIGNED
:
6339 if ( reloc
->r_pcrel() )
6340 throw "pcrel and X86_64_RELOC_UNSIGNED not supported";
6341 switch ( reloc
->r_length() ) {
6344 throw "length < 2 and X86_64_RELOC_UNSIGNED not supported";
6346 parser
.addFixups(src
, ld::Fixup::kindStoreLittleEndian32
, target
);
6349 parser
.addFixups(src
, ld::Fixup::kindStoreLittleEndian64
, target
);
6353 case X86_64_RELOC_SIGNED
:
6354 case X86_64_RELOC_SIGNED_1
:
6355 case X86_64_RELOC_SIGNED_2
:
6356 case X86_64_RELOC_SIGNED_4
:
6357 if ( ! reloc
->r_pcrel() )
6358 throw "not pcrel and X86_64_RELOC_SIGNED* not supported";
6359 if ( reloc
->r_length() != 2 )
6360 throw "length != 2 and X86_64_RELOC_SIGNED* not supported";
6361 switch ( reloc
->r_type() ) {
6362 case X86_64_RELOC_SIGNED
:
6363 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel32
, target
);
6365 case X86_64_RELOC_SIGNED_1
:
6366 if ( reloc
->r_extern() )
6368 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel32_1
, target
);
6370 case X86_64_RELOC_SIGNED_2
:
6371 if ( reloc
->r_extern() )
6373 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel32_2
, target
);
6375 case X86_64_RELOC_SIGNED_4
:
6376 if ( reloc
->r_extern() )
6378 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel32_4
, target
);
6382 case X86_64_RELOC_BRANCH
:
6383 if ( ! reloc
->r_pcrel() )
6384 throw "not pcrel and X86_64_RELOC_BRANCH not supported";
6385 switch ( reloc
->r_length() ) {
6387 if ( (target
.name
!= NULL
) && (strncmp(target
.name
, "___dtrace_probe$", 16) == 0) ) {
6388 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindStoreX86DtraceCallSiteNop
, false, target
.name
);
6389 parser
.addDtraceExtraInfos(src
, &target
.name
[16]);
6391 else if ( (target
.name
!= NULL
) && (strncmp(target
.name
, "___dtrace_isenabled$", 20) == 0) ) {
6392 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindStoreX86DtraceIsEnableSiteClear
, false, target
.name
);
6393 parser
.addDtraceExtraInfos(src
, &target
.name
[20]);
6396 parser
.addFixups(src
, ld::Fixup::kindStoreX86BranchPCRel32
, target
);
6400 parser
.addFixups(src
, ld::Fixup::kindStoreX86BranchPCRel8
, target
);
6403 throwf("length=%d and X86_64_RELOC_BRANCH not supported", reloc
->r_length());
6406 case X86_64_RELOC_GOT
:
6407 if ( ! reloc
->r_extern() )
6408 throw "not extern and X86_64_RELOC_GOT not supported";
6409 if ( ! reloc
->r_pcrel() )
6410 throw "not pcrel and X86_64_RELOC_GOT not supported";
6411 if ( reloc
->r_length() != 2 )
6412 throw "length != 2 and X86_64_RELOC_GOT not supported";
6413 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel32GOT
, target
);
6415 case X86_64_RELOC_GOT_LOAD
:
6416 if ( ! reloc
->r_extern() )
6417 throw "not extern and X86_64_RELOC_GOT_LOAD not supported";
6418 if ( ! reloc
->r_pcrel() )
6419 throw "not pcrel and X86_64_RELOC_GOT_LOAD not supported";
6420 if ( reloc
->r_length() != 2 )
6421 throw "length != 2 and X86_64_RELOC_GOT_LOAD not supported";
6422 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel32GOTLoad
, target
);
6424 case X86_64_RELOC_SUBTRACTOR
:
6425 if ( reloc
->r_pcrel() )
6426 throw "X86_64_RELOC_SUBTRACTOR cannot be pc-relative";
6427 if ( reloc
->r_length() < 2 )
6428 throw "X86_64_RELOC_SUBTRACTOR must have r_length of 2 or 3";
6429 if ( !reloc
->r_extern() )
6430 throw "X86_64_RELOC_SUBTRACTOR must have r_extern=1";
6431 if ( nextReloc
->r_type() != X86_64_RELOC_UNSIGNED
)
6432 throw "X86_64_RELOC_SUBTRACTOR must be followed by X86_64_RELOC_UNSIGNED";
6434 if ( nextReloc
->r_pcrel() )
6435 throw "X86_64_RELOC_UNSIGNED following a X86_64_RELOC_SUBTRACTOR cannot be pc-relative";
6436 if ( nextReloc
->r_length() != reloc
->r_length() )
6437 throw "X86_64_RELOC_UNSIGNED following a X86_64_RELOC_SUBTRACTOR must have same r_length";
6438 if ( nextReloc
->r_extern() ) {
6439 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(nextReloc
->r_symbolnum());
6440 // use direct reference for local symbols
6441 if ( ((sym
.n_type() & N_TYPE
) == N_SECT
) && (((sym
.n_type() & N_EXT
) == 0) || (parser
.nameFromSymbol(sym
)[0] == 'L')) ) {
6442 parser
.findTargetFromAddressAndSectionNum(sym
.n_value(), sym
.n_sect(), toTarget
);
6443 toTarget
.addend
= contentValue
;
6444 useDirectBinding
= true;
6447 toTarget
.name
= parser
.nameFromSymbol(sym
);
6448 toTarget
.weakImport
= parser
.weakImportFromSymbol(sym
);
6449 toTarget
.addend
= contentValue
;
6450 useDirectBinding
= false;
6454 parser
.findTargetFromAddressAndSectionNum(contentValue
, nextReloc
->r_symbolnum(), toTarget
);
6455 useDirectBinding
= (toTarget
.atom
->scope() == ld::Atom::scopeTranslationUnit
) || ((toTarget
.atom
->combine() == ld::Atom::combineByNameAndContent
) || (toTarget
.atom
->combine() == ld::Atom::combineByNameAndReferences
));
6457 if ( useDirectBinding
) {
6458 if ( (toTarget
.atom
->combine() == ld::Atom::combineByNameAndContent
) || (toTarget
.atom
->combine() == ld::Atom::combineByNameAndReferences
) )
6459 parser
.addFixup(src
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, toTarget
.atom
);
6461 parser
.addFixup(src
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, toTarget
.atom
);
6464 parser
.addFixup(src
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, toTarget
.weakImport
, toTarget
.name
);
6465 parser
.addFixup(src
, ld::Fixup::k2of4
, ld::Fixup::kindAddAddend
, toTarget
.addend
);
6466 if ( target
.atom
== NULL
)
6467 parser
.addFixup(src
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractTargetAddress
, false, target
.name
);
6469 parser
.addFixup(src
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractTargetAddress
, target
.atom
);
6470 if ( reloc
->r_length() == 2 )
6471 parser
.addFixup(src
, ld::Fixup::k4of4
, ld::Fixup::kindStoreLittleEndian32
);
6473 parser
.addFixup(src
, ld::Fixup::k4of4
, ld::Fixup::kindStoreLittleEndian64
);
6475 case X86_64_RELOC_TLV
:
6476 if ( ! reloc
->r_extern() )
6477 throw "not extern and X86_64_RELOC_TLV not supported";
6478 if ( ! reloc
->r_pcrel() )
6479 throw "not pcrel and X86_64_RELOC_TLV not supported";
6480 if ( reloc
->r_length() != 2 )
6481 throw "length != 2 and X86_64_RELOC_TLV not supported";
6482 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel32TLVLoad
, target
);
6485 throwf("unknown relocation type %d", reloc
->r_type());
6493 bool Section
<x86
>::addRelocFixup(class Parser
<x86
>& parser
, const macho_relocation_info
<P
>* reloc
)
6495 const macho_section
<P
>* sect
= this->machoSection();
6497 const uint8_t* fixUpPtr
;
6498 uint32_t contentValue
= 0;
6499 ld::Fixup::Kind kind
= ld::Fixup::kindNone
;
6500 Parser
<x86
>::SourceLocation src
;
6501 Parser
<x86
>::TargetDesc target
;
6503 if ( (reloc
->r_address() & R_SCATTERED
) == 0 ) {
6504 srcAddr
= sect
->addr() + reloc
->r_address();
6505 src
.atom
= this->findAtomByAddress(srcAddr
);
6506 src
.offsetInAtom
= srcAddr
- src
.atom
->_objAddress
;
6507 fixUpPtr
= file().fileContent() + sect
->offset() + reloc
->r_address();
6508 switch ( reloc
->r_type() ) {
6509 case GENERIC_RELOC_VANILLA
:
6510 switch ( reloc
->r_length() ) {
6512 contentValue
= (int32_t)(int8_t)*fixUpPtr
;
6513 if ( reloc
->r_pcrel() ) {
6514 kind
= ld::Fixup::kindStoreX86BranchPCRel8
;
6515 contentValue
+= srcAddr
+ sizeof(uint8_t);
6518 throw "r_length=0 and r_pcrel=0 not supported";
6521 contentValue
= (int32_t)(int16_t)E::get16(*((uint16_t*)fixUpPtr
));
6522 if ( reloc
->r_pcrel() ) {
6523 kind
= ld::Fixup::kindStoreX86PCRel16
;
6524 contentValue
+= srcAddr
+ sizeof(uint16_t);
6527 kind
= ld::Fixup::kindStoreLittleEndian16
;
6530 contentValue
= E::get32(*((uint32_t*)fixUpPtr
));
6531 if ( reloc
->r_pcrel() ) {
6532 kind
= ld::Fixup::kindStoreX86BranchPCRel32
;
6533 contentValue
+= srcAddr
+ sizeof(uint32_t);
6536 kind
= ld::Fixup::kindStoreLittleEndian32
;
6539 throw "r_length=3 not supported";
6541 if ( reloc
->r_extern() ) {
6543 const macho_nlist
<P
>& targetSymbol
= parser
.symbolFromIndex(reloc
->r_symbolnum());
6544 target
.name
= parser
.nameFromSymbol(targetSymbol
);
6545 target
.weakImport
= parser
.weakImportFromSymbol(targetSymbol
);
6546 target
.addend
= (int32_t)contentValue
;
6549 parser
.findTargetFromAddressAndSectionNum(contentValue
, reloc
->r_symbolnum(), target
);
6551 if ( (kind
== ld::Fixup::kindStoreX86BranchPCRel32
) && (target
.name
!= NULL
) ) {
6552 if ( strncmp(target
.name
, "___dtrace_probe$", 16) == 0 ) {
6553 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindStoreX86DtraceCallSiteNop
, false, target
.name
);
6554 parser
.addDtraceExtraInfos(src
, &target
.name
[16]);
6557 else if ( strncmp(target
.name
, "___dtrace_isenabled$", 20) == 0 ) {
6558 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindStoreX86DtraceIsEnableSiteClear
, false, target
.name
);
6559 parser
.addDtraceExtraInfos(src
, &target
.name
[20]);
6563 parser
.addFixups(src
, kind
, target
);
6566 case GENERIC_RLEOC_TLV
:
6568 if ( !reloc
->r_extern() )
6569 throw "r_extern=0 and r_type=GENERIC_RLEOC_TLV not supported";
6570 if ( reloc
->r_length() != 2 )
6571 throw "r_length!=2 and r_type=GENERIC_RLEOC_TLV not supported";
6572 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(reloc
->r_symbolnum());
6573 // use direct reference for local symbols
6574 if ( ((sym
.n_type() & N_TYPE
) == N_SECT
) && ((sym
.n_type() & N_EXT
) == 0) ) {
6575 parser
.findTargetFromAddressAndSectionNum(sym
.n_value(), sym
.n_sect(), target
);
6579 target
.name
= parser
.nameFromSymbol(sym
);
6580 target
.weakImport
= parser
.weakImportFromSymbol(sym
);
6582 target
.addend
= (int64_t)(int32_t)E::get32(*((uint32_t*)fixUpPtr
));
6583 if ( reloc
->r_pcrel() ) {
6584 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel32TLVLoad
, target
);
6587 parser
.addFixups(src
, ld::Fixup::kindStoreX86Abs32TLVLoad
, target
);
6593 throwf("unsupported i386 relocation type (%d)", reloc
->r_type());
6597 // scattered relocation
6598 const macho_scattered_relocation_info
<P
>* sreloc
= (macho_scattered_relocation_info
<P
>*)reloc
;
6599 srcAddr
= sect
->addr() + sreloc
->r_address();
6600 src
.atom
= this->findAtomByAddress(srcAddr
);
6601 assert(src
.atom
!= NULL
);
6602 src
.offsetInAtom
= srcAddr
- src
.atom
->_objAddress
;
6603 fixUpPtr
= file().fileContent() + sect
->offset() + sreloc
->r_address();
6604 uint32_t relocValue
= sreloc
->r_value();
6605 bool result
= false;
6606 // file format allows pair to be scattered or not
6607 const macho_scattered_relocation_info
<P
>* nextSReloc
= &sreloc
[1];
6608 const macho_relocation_info
<P
>* nextReloc
= &reloc
[1];
6609 bool nextRelocIsPair
= false;
6610 uint32_t nextRelocAddress
= 0;
6611 uint32_t nextRelocValue
= 0;
6612 if ( (nextReloc
->r_address() & R_SCATTERED
) == 0 ) {
6613 if ( nextReloc
->r_type() == GENERIC_RELOC_PAIR
) {
6614 nextRelocIsPair
= true;
6615 nextRelocAddress
= nextReloc
->r_address();
6616 result
= true; // iterator should skip next reloc, since we've consumed it here
6620 if ( nextSReloc
->r_type() == GENERIC_RELOC_PAIR
) {
6621 nextRelocIsPair
= true;
6622 nextRelocAddress
= nextSReloc
->r_address();
6623 nextRelocValue
= nextSReloc
->r_value();
6626 switch (sreloc
->r_type()) {
6627 case GENERIC_RELOC_VANILLA
:
6628 // with a scattered relocation we get both the target (sreloc->r_value()) and the target+offset (*fixUpPtr)
6629 target
.atom
= parser
.findAtomByAddress(relocValue
);
6630 if ( sreloc
->r_pcrel() ) {
6631 switch ( sreloc
->r_length() ) {
6633 contentValue
= srcAddr
+ 1 + *fixUpPtr
;
6634 target
.addend
= (int32_t)contentValue
- (int32_t)relocValue
;
6635 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel8
, target
);
6638 contentValue
= srcAddr
+ 2 + LittleEndian::get16(*((uint16_t*)fixUpPtr
));
6639 target
.addend
= (int32_t)contentValue
- (int32_t)relocValue
;
6640 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel16
, target
);
6643 contentValue
= srcAddr
+ 4 + LittleEndian::get32(*((uint32_t*)fixUpPtr
));
6644 target
.addend
= (int32_t)contentValue
- (int32_t)relocValue
;
6645 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel32
, target
);
6648 throw "unsupported r_length=3 for scattered pc-rel vanilla reloc";
6653 if ( sreloc
->r_length() != 2 )
6654 throwf("unsupported r_length=%d for scattered vanilla reloc", sreloc
->r_length());
6655 contentValue
= LittleEndian::get32(*((uint32_t*)fixUpPtr
));
6656 target
.addend
= (int32_t)contentValue
- (int32_t)(target
.atom
->objectAddress());
6657 parser
.addFixups(src
, ld::Fixup::kindStoreLittleEndian32
, target
);
6660 case GENERIC_RELOC_SECTDIFF
:
6661 case GENERIC_RELOC_LOCAL_SECTDIFF
:
6663 if ( !nextRelocIsPair
)
6664 throw "GENERIC_RELOC_SECTDIFF missing following pair";
6665 switch ( sreloc
->r_length() ) {
6668 throw "bad length for GENERIC_RELOC_SECTDIFF";
6670 contentValue
= (int32_t)(int16_t)LittleEndian::get16(*((uint16_t*)fixUpPtr
));
6671 kind
= ld::Fixup::kindStoreLittleEndian16
;
6674 contentValue
= LittleEndian::get32(*((uint32_t*)fixUpPtr
));
6675 kind
= ld::Fixup::kindStoreLittleEndian32
;
6678 Atom
<x86
>* fromAtom
= parser
.findAtomByAddress(nextRelocValue
);
6679 uint32_t offsetInFrom
= nextRelocValue
- fromAtom
->_objAddress
;
6680 parser
.findTargetFromAddress(sreloc
->r_value(), target
);
6681 // check for addend encoded in the section content
6682 int64_t addend
= (int32_t)contentValue
- (int32_t)(sreloc
->r_value() - nextRelocValue
);
6684 // switch binding base on coalescing
6685 if ( target
.atom
== NULL
) {
6686 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, false, target
.name
);
6688 else if ( target
.atom
->scope() == ld::Atom::scopeTranslationUnit
) {
6689 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, target
.atom
);
6691 else if ( (target
.atom
->combine() == ld::Atom::combineByNameAndContent
) || (target
.atom
->combine() == ld::Atom::combineByNameAndReferences
) ) {
6692 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, target
.atom
);
6695 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, false, target
.atom
->name());
6697 parser
.addFixup(src
, ld::Fixup::k2of5
, ld::Fixup::kindAddAddend
, target
.addend
);
6698 parser
.addFixup(src
, ld::Fixup::k3of5
, ld::Fixup::kindSubtractTargetAddress
, fromAtom
);
6699 parser
.addFixup(src
, ld::Fixup::k4of5
, ld::Fixup::kindSubtractAddend
, offsetInFrom
-addend
);
6700 parser
.addFixup(src
, ld::Fixup::k5of5
, kind
);
6703 // switch binding base on coalescing
6704 if ( target
.atom
== NULL
) {
6705 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, false, target
.name
);
6707 else if ( target
.atom
->scope() == ld::Atom::scopeTranslationUnit
) {
6708 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, target
.atom
);
6710 else if ( (target
.atom
->combine() == ld::Atom::combineByNameAndContent
) || (target
.atom
->combine() == ld::Atom::combineByNameAndReferences
) ) {
6711 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, target
.atom
);
6714 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, false, target
.atom
->name());
6716 parser
.addFixup(src
, ld::Fixup::k2of5
, ld::Fixup::kindAddAddend
, target
.addend
+addend
);
6717 parser
.addFixup(src
, ld::Fixup::k3of5
, ld::Fixup::kindSubtractTargetAddress
, fromAtom
);
6718 parser
.addFixup(src
, ld::Fixup::k4of5
, ld::Fixup::kindSubtractAddend
, offsetInFrom
);
6719 parser
.addFixup(src
, ld::Fixup::k5of5
, kind
);
6732 #if SUPPORT_ARCH_arm_any
6734 bool Section
<arm
>::addRelocFixup(class Parser
<arm
>& parser
, const macho_relocation_info
<P
>* reloc
)
6736 const macho_section
<P
>* sect
= this->machoSection();
6737 bool result
= false;
6741 int32_t displacement
= 0;
6742 uint32_t instruction
= 0;
6743 pint_t contentValue
= 0;
6744 Parser
<arm
>::SourceLocation src
;
6745 Parser
<arm
>::TargetDesc target
;
6746 const macho_relocation_info
<P
>* nextReloc
;
6748 if ( (reloc
->r_address() & R_SCATTERED
) == 0 ) {
6749 bool externSymbolIsThumbDef
= false;
6750 srcAddr
= sect
->addr() + reloc
->r_address();
6751 src
.atom
= this->findAtomByAddress(srcAddr
);
6752 src
.offsetInAtom
= srcAddr
- src
.atom
->_objAddress
;
6753 fixUpPtr
= (uint32_t*)(file().fileContent() + sect
->offset() + reloc
->r_address());
6754 if ( reloc
->r_type() != ARM_RELOC_PAIR
)
6755 instruction
= LittleEndian::get32(*fixUpPtr
);
6756 if ( reloc
->r_extern() ) {
6757 const macho_nlist
<P
>& targetSymbol
= parser
.symbolFromIndex(reloc
->r_symbolnum());
6758 // use direct reference for local symbols
6759 if ( ((targetSymbol
.n_type() & N_TYPE
) == N_SECT
) && (((targetSymbol
.n_type() & N_EXT
) == 0) || (parser
.nameFromSymbol(targetSymbol
)[0] == 'L')) ) {
6760 parser
.findTargetFromAddressAndSectionNum(targetSymbol
.n_value(), targetSymbol
.n_sect(), target
);
6764 target
.name
= parser
.nameFromSymbol(targetSymbol
);
6765 target
.weakImport
= parser
.weakImportFromSymbol(targetSymbol
);
6766 if ( ((targetSymbol
.n_type() & N_TYPE
) == N_SECT
) && (targetSymbol
.n_desc() & N_ARM_THUMB_DEF
) )
6767 externSymbolIsThumbDef
= true;
6770 switch ( reloc
->r_type() ) {
6771 case ARM_RELOC_BR24
:
6772 // Sign-extend displacement
6773 displacement
= (instruction
& 0x00FFFFFF) << 2;
6774 if ( (displacement
& 0x02000000) != 0 )
6775 displacement
|= 0xFC000000;
6776 // The pc added will be +8 from the pc
6778 // If this is BLX add H << 1
6779 if ((instruction
& 0xFE000000) == 0xFA000000)
6780 displacement
+= ((instruction
& 0x01000000) >> 23);
6781 if ( reloc
->r_extern() ) {
6782 dstAddr
= srcAddr
+ displacement
;
6783 // <rdar://problem/16652542> support large .o files
6784 if ( srcAddr
> 0x2000000 ) {
6785 dstAddr
-= ((srcAddr
+ 0x1FFFFFF) & 0xFC000000);
6787 target
.addend
= dstAddr
;
6788 if ( externSymbolIsThumbDef
)
6789 target
.addend
&= -2; // remove thumb bit
6792 dstAddr
= srcAddr
+ displacement
;
6793 parser
.findTargetFromAddressAndSectionNum(dstAddr
, reloc
->r_symbolnum(), target
);
6795 // special case "calls" for dtrace
6796 if ( (target
.name
!= NULL
) && (strncmp(target
.name
, "___dtrace_probe$", 16) == 0) ) {
6797 parser
.addFixup(src
, ld::Fixup::k1of1
,
6798 ld::Fixup::kindStoreARMDtraceCallSiteNop
, false, target
.name
);
6799 parser
.addDtraceExtraInfos(src
, &target
.name
[16]);
6801 else if ( (target
.name
!= NULL
) && (strncmp(target
.name
, "___dtrace_isenabled$", 20) == 0) ) {
6802 parser
.addFixup(src
, ld::Fixup::k1of1
,
6803 ld::Fixup::kindStoreARMDtraceIsEnableSiteClear
, false, target
.name
);
6804 parser
.addDtraceExtraInfos(src
, &target
.name
[20]);
6807 parser
.addFixups(src
, ld::Fixup::kindStoreARMBranch24
, target
);
6810 case ARM_THUMB_RELOC_BR22
:
6811 // thumb2 added two more bits to displacement, complicating the displacement decoding
6813 uint32_t s
= (instruction
>> 10) & 0x1;
6814 uint32_t j1
= (instruction
>> 29) & 0x1;
6815 uint32_t j2
= (instruction
>> 27) & 0x1;
6816 uint32_t imm10
= instruction
& 0x3FF;
6817 uint32_t imm11
= (instruction
>> 16) & 0x7FF;
6818 uint32_t i1
= (j1
== s
);
6819 uint32_t i2
= (j2
== s
);
6820 uint32_t dis
= (s
<< 24) | (i1
<< 23) | (i2
<< 22) | (imm10
<< 12) | (imm11
<< 1);
6824 displacement
= sdis
;
6826 // The pc added will be +4 from the pc
6828 // If the instruction was blx, force the low 2 bits to be clear
6829 dstAddr
= srcAddr
+ displacement
;
6830 if ((instruction
& 0xD0000000) == 0xC0000000)
6831 dstAddr
&= 0xFFFFFFFC;
6833 if ( reloc
->r_extern() ) {
6834 // <rdar://problem/16652542> support large .o files
6835 if ( srcAddr
> 0x1000000 ) {
6836 dstAddr
-= ((srcAddr
+ 0xFFFFFF) & 0xFE000000);
6838 target
.addend
= (int64_t)(int32_t)dstAddr
;
6841 parser
.findTargetFromAddressAndSectionNum(dstAddr
, reloc
->r_symbolnum(), target
);
6843 // special case "calls" for dtrace
6844 if ( (target
.name
!= NULL
) && (strncmp(target
.name
, "___dtrace_probe$", 16) == 0) ) {
6845 parser
.addFixup(src
, ld::Fixup::k1of1
,
6846 ld::Fixup::kindStoreThumbDtraceCallSiteNop
, false, target
.name
);
6847 parser
.addDtraceExtraInfos(src
, &target
.name
[16]);
6849 else if ( (target
.name
!= NULL
) && (strncmp(target
.name
, "___dtrace_isenabled$", 20) == 0) ) {
6850 parser
.addFixup(src
, ld::Fixup::k1of1
,
6851 ld::Fixup::kindStoreThumbDtraceIsEnableSiteClear
, false, target
.name
);
6852 parser
.addDtraceExtraInfos(src
, &target
.name
[20]);
6855 parser
.addFixups(src
, ld::Fixup::kindStoreThumbBranch22
, target
);
6858 case ARM_RELOC_VANILLA
:
6859 if ( reloc
->r_length() != 2 )
6860 throw "bad length for ARM_RELOC_VANILLA";
6861 contentValue
= LittleEndian::get32(*fixUpPtr
);
6862 if ( reloc
->r_extern() ) {
6863 target
.addend
= (int32_t)contentValue
;
6864 if ( externSymbolIsThumbDef
)
6865 target
.addend
&= -2; // remove thumb bit
6868 parser
.findTargetFromAddressAndSectionNum(contentValue
, reloc
->r_symbolnum(), target
);
6869 // possible non-extern relocation turned into by-name ref because target is a weak-def
6870 if ( target
.atom
!= NULL
) {
6871 if ( target
.atom
->isThumb() )
6872 target
.addend
&= -2; // remove thumb bit
6873 // if reference to LSDA, add group subordinate fixup
6874 if ( target
.atom
->contentType() == ld::Atom::typeLSDA
) {
6875 Parser
<arm
>::SourceLocation src2
;
6876 src2
.atom
= src
.atom
;
6877 src2
.offsetInAtom
= 0;
6878 parser
.addFixup(src2
, ld::Fixup::k1of1
, ld::Fixup::kindNoneGroupSubordinateLSDA
, target
.atom
);
6882 parser
.addFixups(src
, ld::Fixup::kindStoreLittleEndian32
, target
);
6884 case ARM_THUMB_32BIT_BRANCH
:
6885 // silently ignore old unnecessary reloc
6887 case ARM_RELOC_HALF
:
6888 nextReloc
= &reloc
[1];
6889 if ( nextReloc
->r_type() == ARM_RELOC_PAIR
) {
6890 uint32_t instruction16
;
6891 uint32_t other16
= (nextReloc
->r_address() & 0xFFFF);
6893 if ( reloc
->r_length() & 2 ) {
6895 uint32_t i
= ((instruction
& 0x00000400) >> 10);
6896 uint32_t imm4
= (instruction
& 0x0000000F);
6897 uint32_t imm3
= ((instruction
& 0x70000000) >> 28);
6898 uint32_t imm8
= ((instruction
& 0x00FF0000) >> 16);
6899 instruction16
= (imm4
<< 12) | (i
<< 11) | (imm3
<< 8) | imm8
;
6903 uint32_t imm4
= ((instruction
& 0x000F0000) >> 16);
6904 uint32_t imm12
= (instruction
& 0x00000FFF);
6905 instruction16
= (imm4
<< 12) | imm12
;
6907 if ( reloc
->r_length() & 1 ) {
6909 dstAddr
= ((instruction16
<< 16) | other16
);
6910 if ( reloc
->r_extern() ) {
6911 target
.addend
= dstAddr
;
6912 if ( externSymbolIsThumbDef
)
6913 target
.addend
&= -2; // remove thumb bit
6916 parser
.findTargetFromAddress(dstAddr
, target
);
6917 if ( target
.atom
->isThumb() )
6918 target
.addend
&= (-2); // remove thumb bit
6920 parser
.addFixups(src
, (isThumb
? ld::Fixup::kindStoreThumbHigh16
: ld::Fixup::kindStoreARMHigh16
), target
);
6924 dstAddr
= (other16
<< 16) | instruction16
;
6925 if ( reloc
->r_extern() ) {
6926 target
.addend
= dstAddr
;
6927 if ( externSymbolIsThumbDef
)
6928 target
.addend
&= -2; // remove thumb bit
6931 parser
.findTargetFromAddress(dstAddr
, target
);
6932 if ( target
.atom
->isThumb() )
6933 target
.addend
&= (-2); // remove thumb bit
6935 parser
.addFixups(src
, (isThumb
? ld::Fixup::kindStoreThumbLow16
: ld::Fixup::kindStoreARMLow16
), target
);
6940 throw "for ARM_RELOC_HALF, next reloc is not ARM_RELOC_PAIR";
6943 throwf("unknown relocation type %d", reloc
->r_type());
6948 const macho_scattered_relocation_info
<P
>* sreloc
= (macho_scattered_relocation_info
<P
>*)reloc
;
6949 // file format allows pair to be scattered or not
6950 const macho_scattered_relocation_info
<P
>* nextSReloc
= &sreloc
[1];
6951 nextReloc
= &reloc
[1];
6952 srcAddr
= sect
->addr() + sreloc
->r_address();
6953 dstAddr
= sreloc
->r_value();
6954 fixUpPtr
= (uint32_t*)(file().fileContent() + sect
->offset() + sreloc
->r_address());
6955 instruction
= LittleEndian::get32(*fixUpPtr
);
6956 src
.atom
= this->findAtomByAddress(srcAddr
);
6957 src
.offsetInAtom
= srcAddr
- src
.atom
->_objAddress
;
6958 bool nextRelocIsPair
= false;
6959 uint32_t nextRelocAddress
= 0;
6960 uint32_t nextRelocValue
= 0;
6961 if ( (nextReloc
->r_address() & R_SCATTERED
) == 0 ) {
6962 if ( nextReloc
->r_type() == ARM_RELOC_PAIR
) {
6963 nextRelocIsPair
= true;
6964 nextRelocAddress
= nextReloc
->r_address();
6969 if ( nextSReloc
->r_type() == ARM_RELOC_PAIR
) {
6970 nextRelocIsPair
= true;
6971 nextRelocAddress
= nextSReloc
->r_address();
6972 nextRelocValue
= nextSReloc
->r_value();
6976 switch ( sreloc
->r_type() ) {
6977 case ARM_RELOC_VANILLA
:
6978 // with a scattered relocation we get both the target (sreloc->r_value()) and the target+offset (*fixUpPtr)
6979 if ( sreloc
->r_length() != 2 )
6980 throw "bad length for ARM_RELOC_VANILLA";
6981 target
.atom
= parser
.findAtomByAddress(sreloc
->r_value());
6982 if ( target
.atom
== NULL
)
6983 throwf("bad r_value (0x%08X) for ARM_RELOC_VANILLA\n", sreloc
->r_value());
6984 contentValue
= LittleEndian::get32(*fixUpPtr
);
6985 target
.addend
= contentValue
- target
.atom
->_objAddress
;
6986 if ( target
.atom
->isThumb() )
6987 target
.addend
&= -2; // remove thumb bit
6988 parser
.addFixups(src
, ld::Fixup::kindStoreLittleEndian32
, target
);
6990 case ARM_RELOC_BR24
:
6991 // Sign-extend displacement
6992 displacement
= (instruction
& 0x00FFFFFF) << 2;
6993 if ( (displacement
& 0x02000000) != 0 )
6994 displacement
|= 0xFC000000;
6995 // The pc added will be +8 from the pc
6997 // If this is BLX add H << 1
6998 if ((instruction
& 0xFE000000) == 0xFA000000)
6999 displacement
+= ((instruction
& 0x01000000) >> 23);
7000 target
.atom
= parser
.findAtomByAddress(sreloc
->r_value());
7001 target
.addend
= (int64_t)(srcAddr
+ displacement
) - (int64_t)(target
.atom
->_objAddress
);
7002 parser
.addFixups(src
, ld::Fixup::kindStoreARMBranch24
, target
);
7004 case ARM_THUMB_RELOC_BR22
:
7005 // thumb2 added two more bits to displacement, complicating the displacement decoding
7007 uint32_t s
= (instruction
>> 10) & 0x1;
7008 uint32_t j1
= (instruction
>> 29) & 0x1;
7009 uint32_t j2
= (instruction
>> 27) & 0x1;
7010 uint32_t imm10
= instruction
& 0x3FF;
7011 uint32_t imm11
= (instruction
>> 16) & 0x7FF;
7012 uint32_t i1
= (j1
== s
);
7013 uint32_t i2
= (j2
== s
);
7014 uint32_t dis
= (s
<< 24) | (i1
<< 23) | (i2
<< 22) | (imm10
<< 12) | (imm11
<< 1);
7018 displacement
= sdis
;
7020 // The pc added will be +4 from the pc
7022 dstAddr
= srcAddr
+displacement
;
7023 // If the instruction was blx, force the low 2 bits to be clear
7024 if ((instruction
& 0xF8000000) == 0xE8000000)
7025 dstAddr
&= 0xFFFFFFFC;
7026 target
.atom
= parser
.findAtomByAddress(sreloc
->r_value());
7027 target
.addend
= dstAddr
- target
.atom
->_objAddress
;
7028 parser
.addFixups(src
, ld::Fixup::kindStoreThumbBranch22
, target
);
7030 case ARM_RELOC_SECTDIFF
:
7031 case ARM_RELOC_LOCAL_SECTDIFF
:
7033 if ( ! nextRelocIsPair
)
7034 throw "ARM_RELOC_SECTDIFF missing following pair";
7035 if ( sreloc
->r_length() != 2 )
7036 throw "bad length for ARM_RELOC_SECTDIFF";
7037 contentValue
= LittleEndian::get32(*fixUpPtr
);
7038 Atom
<arm
>* fromAtom
= parser
.findAtomByAddress(nextRelocValue
);
7039 uint32_t offsetInFrom
= nextRelocValue
- fromAtom
->_objAddress
;
7040 uint32_t offsetInTarget
;
7041 Atom
<arm
>* targetAtom
= parser
.findAtomByAddressOrLocalTargetOfStub(sreloc
->r_value(), &offsetInTarget
);
7042 // check for addend encoded in the section content
7043 int64_t addend
= (int32_t)contentValue
- (int32_t)(sreloc
->r_value() - nextRelocValue
);
7044 if ( targetAtom
->isThumb() )
7045 addend
&= -2; // remove thumb bit
7046 // if reference to LSDA, add group subordinate fixup
7047 if ( targetAtom
->contentType() == ld::Atom::typeLSDA
) {
7048 Parser
<arm
>::SourceLocation src2
;
7049 src2
.atom
= src
.atom
;
7050 src2
.offsetInAtom
= 0;
7051 parser
.addFixup(src2
, ld::Fixup::k1of1
, ld::Fixup::kindNoneGroupSubordinateLSDA
, targetAtom
);
7054 // switch binding base on coalescing
7055 if ( targetAtom
->scope() == ld::Atom::scopeTranslationUnit
) {
7056 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, targetAtom
);
7058 else if ( (targetAtom
->combine() == ld::Atom::combineByNameAndContent
) || (targetAtom
->combine() == ld::Atom::combineByNameAndReferences
) ) {
7059 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, targetAtom
);
7062 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, false, targetAtom
->name());
7064 parser
.addFixup(src
, ld::Fixup::k2of5
, ld::Fixup::kindAddAddend
, offsetInTarget
);
7065 parser
.addFixup(src
, ld::Fixup::k3of5
, ld::Fixup::kindSubtractTargetAddress
, fromAtom
);
7066 parser
.addFixup(src
, ld::Fixup::k4of5
, ld::Fixup::kindSubtractAddend
, offsetInFrom
-addend
);
7067 parser
.addFixup(src
, ld::Fixup::k5of5
, ld::Fixup::kindStoreLittleEndian32
);
7070 if ( targetAtom
->scope() == ld::Atom::scopeTranslationUnit
) {
7071 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, targetAtom
);
7073 else if ( (targetAtom
->combine() == ld::Atom::combineByNameAndContent
) || (targetAtom
->combine() == ld::Atom::combineByNameAndReferences
) ) {
7074 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, targetAtom
);
7077 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, false, targetAtom
->name());
7079 parser
.addFixup(src
, ld::Fixup::k2of5
, ld::Fixup::kindAddAddend
, (uint32_t)(offsetInTarget
+addend
));
7080 parser
.addFixup(src
, ld::Fixup::k3of5
, ld::Fixup::kindSubtractTargetAddress
, fromAtom
);
7081 parser
.addFixup(src
, ld::Fixup::k4of5
, ld::Fixup::kindSubtractAddend
, offsetInFrom
);
7082 parser
.addFixup(src
, ld::Fixup::k5of5
, ld::Fixup::kindStoreLittleEndian32
);
7086 case ARM_RELOC_HALF_SECTDIFF
:
7087 if ( nextRelocIsPair
) {
7088 instruction
= LittleEndian::get32(*fixUpPtr
);
7089 Atom
<arm
>* fromAtom
= parser
.findAtomByAddress(nextRelocValue
);
7090 uint32_t offsetInFrom
= nextRelocValue
- fromAtom
->_objAddress
;
7091 Atom
<arm
>* targetAtom
= parser
.findAtomByAddress(sreloc
->r_value());
7092 uint32_t offsetInTarget
= sreloc
->r_value() - targetAtom
->_objAddress
;
7093 uint32_t instruction16
;
7094 uint32_t other16
= (nextRelocAddress
& 0xFFFF);
7096 if ( sreloc
->r_length() & 2 ) {
7098 uint32_t i
= ((instruction
& 0x00000400) >> 10);
7099 uint32_t imm4
= (instruction
& 0x0000000F);
7100 uint32_t imm3
= ((instruction
& 0x70000000) >> 28);
7101 uint32_t imm8
= ((instruction
& 0x00FF0000) >> 16);
7102 instruction16
= (imm4
<< 12) | (i
<< 11) | (imm3
<< 8) | imm8
;
7106 uint32_t imm4
= ((instruction
& 0x000F0000) >> 16);
7107 uint32_t imm12
= (instruction
& 0x00000FFF);
7108 instruction16
= (imm4
<< 12) | imm12
;
7110 if ( sreloc
->r_length() & 1 )
7111 dstAddr
= ((instruction16
<< 16) | other16
);
7113 dstAddr
= (other16
<< 16) | instruction16
;
7114 if ( targetAtom
->isThumb() )
7115 dstAddr
&= (-2); // remove thumb bit
7116 int32_t addend
= dstAddr
- (sreloc
->r_value() - nextRelocValue
);
7117 if ( targetAtom
->scope() == ld::Atom::scopeTranslationUnit
) {
7118 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, targetAtom
);
7120 else if ( (targetAtom
->combine() == ld::Atom::combineByNameAndContent
) || (targetAtom
->combine() == ld::Atom::combineByNameAndReferences
) ) {
7121 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, targetAtom
);
7124 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, false, targetAtom
->name());
7126 parser
.addFixup(src
, ld::Fixup::k2of5
, ld::Fixup::kindAddAddend
, (uint32_t)offsetInTarget
+addend
);
7127 parser
.addFixup(src
, ld::Fixup::k3of5
, ld::Fixup::kindSubtractTargetAddress
, fromAtom
);
7128 parser
.addFixup(src
, ld::Fixup::k4of5
, ld::Fixup::kindSubtractAddend
, offsetInFrom
);
7129 if ( sreloc
->r_length() & 1 ) {
7131 parser
.addFixup(src
, ld::Fixup::k5of5
, (isThumb
? ld::Fixup::kindStoreThumbHigh16
: ld::Fixup::kindStoreARMHigh16
));
7135 parser
.addFixup(src
, ld::Fixup::k5of5
, (isThumb
? ld::Fixup::kindStoreThumbLow16
: ld::Fixup::kindStoreARMLow16
));
7140 throw "ARM_RELOC_HALF_SECTDIFF reloc missing following pair";
7142 case ARM_RELOC_HALF
:
7143 if ( nextRelocIsPair
) {
7144 instruction
= LittleEndian::get32(*fixUpPtr
);
7145 Atom
<arm
>* targetAtom
= parser
.findAtomByAddress(sreloc
->r_value());
7146 uint32_t instruction16
;
7147 uint32_t other16
= (nextRelocAddress
& 0xFFFF);
7149 if ( sreloc
->r_length() & 2 ) {
7151 uint32_t i
= ((instruction
& 0x00000400) >> 10);
7152 uint32_t imm4
= (instruction
& 0x0000000F);
7153 uint32_t imm3
= ((instruction
& 0x70000000) >> 28);
7154 uint32_t imm8
= ((instruction
& 0x00FF0000) >> 16);
7155 instruction16
= (imm4
<< 12) | (i
<< 11) | (imm3
<< 8) | imm8
;
7159 uint32_t imm4
= ((instruction
& 0x000F0000) >> 16);
7160 uint32_t imm12
= (instruction
& 0x00000FFF);
7161 instruction16
= (imm4
<< 12) | imm12
;
7163 if ( sreloc
->r_length() & 1 )
7164 dstAddr
= ((instruction16
<< 16) | other16
);
7166 dstAddr
= (other16
<< 16) | instruction16
;
7167 if ( targetAtom
->scope() == ld::Atom::scopeTranslationUnit
) {
7168 parser
.addFixup(src
, ld::Fixup::k1of3
, ld::Fixup::kindSetTargetAddress
, targetAtom
);
7170 else if ( (targetAtom
->combine() == ld::Atom::combineByNameAndContent
) || (targetAtom
->combine() == ld::Atom::combineByNameAndReferences
) ) {
7171 parser
.addFixup(src
, ld::Fixup::k1of3
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, targetAtom
);
7174 parser
.addFixup(src
, ld::Fixup::k1of3
, ld::Fixup::kindSetTargetAddress
, false, targetAtom
->name());
7176 parser
.addFixup(src
, ld::Fixup::k2of3
, ld::Fixup::kindAddAddend
, dstAddr
- targetAtom
->_objAddress
);
7177 if ( sreloc
->r_length() & 1 ) {
7179 parser
.addFixup(src
, ld::Fixup::k3of3
, (isThumb
? ld::Fixup::kindStoreThumbHigh16
: ld::Fixup::kindStoreARMHigh16
));
7183 parser
.addFixup(src
, ld::Fixup::k3of3
, (isThumb
? ld::Fixup::kindStoreThumbLow16
: ld::Fixup::kindStoreARMLow16
));
7188 throw "scattered ARM_RELOC_HALF reloc missing following pair";
7191 throwf("unknown ARM scattered relocation type %d", sreloc
->r_type());
7199 #if SUPPORT_ARCH_arm64
7201 bool Section
<arm64
>::addRelocFixup(class Parser
<arm64
>& parser
, const macho_relocation_info
<P
>* reloc
)
7203 bool result
= false;
7204 Parser
<arm64
>::SourceLocation src
;
7205 Parser
<arm64
>::TargetDesc target
= { NULL
, NULL
, false, 0 };
7206 Parser
<arm64
>::TargetDesc toTarget
;
7207 int32_t prefixRelocAddend
= 0;
7208 if ( reloc
->r_type() == ARM64_RELOC_ADDEND
) {
7209 uint32_t rawAddend
= reloc
->r_symbolnum();
7210 prefixRelocAddend
= rawAddend
;
7211 if ( rawAddend
& 0x00800000 )
7212 prefixRelocAddend
|= 0xFF000000; // sign extend 24-bit signed int to 32-bits
7213 uint32_t addendAddress
= reloc
->r_address();
7214 ++reloc
; //advance to next reloc record
7216 if ( reloc
->r_address() != addendAddress
)
7217 throw "ARM64_RELOC_ADDEND r_address does not match next reloc's r_address";
7219 const macho_section
<P
>* sect
= this->machoSection();
7220 uint64_t srcAddr
= sect
->addr() + reloc
->r_address();
7221 src
.atom
= this->findAtomByAddress(srcAddr
);
7222 src
.offsetInAtom
= srcAddr
- src
.atom
->_objAddress
;
7223 const uint8_t* fixUpPtr
= file().fileContent() + sect
->offset() + reloc
->r_address();
7224 uint64_t contentValue
= 0;
7225 const macho_relocation_info
<arm64::P
>* nextReloc
= &reloc
[1];
7226 bool useDirectBinding
;
7227 uint32_t instruction
;
7228 uint32_t encodedAddend
;
7229 switch ( reloc
->r_length() ) {
7231 contentValue
= *fixUpPtr
;
7234 contentValue
= (int64_t)(int16_t)E::get16(*((uint16_t*)fixUpPtr
));
7237 contentValue
= (int64_t)(int32_t)E::get32(*((uint32_t*)fixUpPtr
));
7240 contentValue
= E::get64(*((uint64_t*)fixUpPtr
));
7243 if ( reloc
->r_extern() ) {
7244 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(reloc
->r_symbolnum());
7245 const char* symbolName
= parser
.nameFromSymbol(sym
);
7246 if ( ((sym
.n_type() & N_TYPE
) == N_SECT
) && (((sym
.n_type() & N_EXT
) == 0) || (symbolName
[0] == 'L') || (symbolName
[0] == 'l')) ) {
7247 // use direct reference for local symbols
7248 parser
.findTargetFromAddressAndSectionNum(sym
.n_value(), sym
.n_sect(), target
);
7249 //target.addend += contentValue;
7251 else if ( ((sym
.n_type() & N_TYPE
) == N_SECT
) && (src
.atom
->_objAddress
<= sym
.n_value()) && (sym
.n_value() < (src
.atom
->_objAddress
+src
.atom
->size())) ) {
7252 // <rdar://problem/13700961> spurious warning when weak function has reference to itself
7253 // use direct reference when atom targets itself
7254 target
.atom
= src
.atom
;
7258 target
.name
= symbolName
;
7259 target
.weakImport
= parser
.weakImportFromSymbol(sym
);
7260 //target.addend = contentValue;
7262 // cfstrings should always use direct reference to backing store
7263 if ( (this->type() == ld::Section::typeCFString
) && (src
.offsetInAtom
!= 0) ) {
7264 parser
.findTargetFromAddressAndSectionNum(sym
.n_value(), sym
.n_sect(), target
);
7265 //target.addend = contentValue;
7269 if ( reloc
->r_pcrel() )
7270 contentValue
+= srcAddr
;
7271 parser
.findTargetFromAddressAndSectionNum(contentValue
, reloc
->r_symbolnum(), target
);
7273 switch ( reloc
->r_type() ) {
7274 case ARM64_RELOC_UNSIGNED
:
7275 if ( reloc
->r_pcrel() )
7276 throw "pcrel and ARM64_RELOC_UNSIGNED not supported";
7277 if ( reloc
->r_extern() )
7278 target
.addend
= contentValue
;
7279 switch ( reloc
->r_length() ) {
7282 throw "length < 2 and ARM64_RELOC_UNSIGNED not supported";
7284 parser
.addFixups(src
, ld::Fixup::kindStoreLittleEndian32
, target
);
7287 parser
.addFixups(src
, ld::Fixup::kindStoreLittleEndian64
, target
);
7291 case ARM64_RELOC_BRANCH26
:
7292 if ( ! reloc
->r_pcrel() )
7293 throw "not pcrel and ARM64_RELOC_BRANCH26 not supported";
7294 if ( ! reloc
->r_extern() )
7295 throw "r_extern == 0 and ARM64_RELOC_BRANCH26 not supported";
7296 if ( reloc
->r_length() != 2 )
7297 throw "r_length != 2 and ARM64_RELOC_BRANCH26 not supported";
7298 if ( (target
.name
!= NULL
) && (strncmp(target
.name
, "___dtrace_probe$", 16) == 0) ) {
7299 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindStoreARM64DtraceCallSiteNop
, false, target
.name
);
7300 parser
.addDtraceExtraInfos(src
, &target
.name
[16]);
7302 else if ( (target
.name
!= NULL
) && (strncmp(target
.name
, "___dtrace_isenabled$", 20) == 0) ) {
7303 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindStoreARM64DtraceIsEnableSiteClear
, false, target
.name
);
7304 parser
.addDtraceExtraInfos(src
, &target
.name
[20]);
7307 target
.addend
= prefixRelocAddend
;
7308 instruction
= contentValue
;
7309 encodedAddend
= (instruction
& 0x03FFFFFF) << 2;
7310 if ( encodedAddend
!= 0 ) {
7311 if ( prefixRelocAddend
== 0 ) {
7312 warning("branch26 instruction at 0x%08X has embedded addend. ARM64_RELOC_ADDEND should be used instead", reloc
->r_address());
7313 target
.addend
= encodedAddend
;
7316 throwf("branch26 instruction at 0x%08X has embedded addend and ARM64_RELOC_ADDEND also used", reloc
->r_address());
7319 parser
.addFixups(src
, ld::Fixup::kindStoreARM64Branch26
, target
);
7322 case ARM64_RELOC_PAGE21
:
7323 if ( ! reloc
->r_pcrel() )
7324 throw "not pcrel and ARM64_RELOC_PAGE21 not supported";
7325 if ( ! reloc
->r_extern() )
7326 throw "r_extern == 0 and ARM64_RELOC_PAGE21 not supported";
7327 if ( reloc
->r_length() != 2 )
7328 throw "length != 2 and ARM64_RELOC_PAGE21 not supported";
7329 target
.addend
= prefixRelocAddend
;
7330 instruction
= contentValue
;
7331 encodedAddend
= ((instruction
& 0x60000000) >> 29) | ((instruction
& 0x01FFFFE0) >> 3);
7332 encodedAddend
*= 4096; // internally addend is in bytes, so scale
7333 if ( encodedAddend
!= 0 ) {
7334 if ( prefixRelocAddend
== 0 ) {
7335 warning("adrp instruction at 0x%08X has embedded addend. ARM64_RELOC_ADDEND should be used instead", reloc
->r_address());
7336 target
.addend
= encodedAddend
;
7339 throwf("adrp instruction at 0x%08X has embedded addend and ARM64_RELOC_ADDEND also used", reloc
->r_address());
7342 parser
.addFixups(src
, ld::Fixup::kindStoreARM64Page21
, target
);
7344 case ARM64_RELOC_PAGEOFF12
:
7345 if ( reloc
->r_pcrel() )
7346 throw "pcrel and ARM64_RELOC_PAGEOFF12 not supported";
7347 if ( ! reloc
->r_extern() )
7348 throw "r_extern == 0 and ARM64_RELOC_PAGEOFF12 not supported";
7349 if ( reloc
->r_length() != 2 )
7350 throw "length != 2 and ARM64_RELOC_PAGEOFF12 not supported";
7351 target
.addend
= prefixRelocAddend
;
7352 instruction
= contentValue
;
7353 encodedAddend
= ((instruction
& 0x003FFC00) >> 10);
7354 // internally addend is in bytes. Some instructions have an implicit scale factor
7355 if ( (instruction
& 0x3B000000) == 0x39000000 ) {
7356 switch ( instruction
& 0xC0000000 ) {
7370 if ( encodedAddend
!= 0 ) {
7371 if ( prefixRelocAddend
== 0 ) {
7372 warning("pageoff12 instruction at 0x%08X has embedded addend. ARM64_RELOC_ADDEND should be used instead", reloc
->r_address());
7373 target
.addend
= encodedAddend
;
7376 throwf("pageoff12 instruction at 0x%08X has embedded addend and ARM64_RELOC_ADDEND also used", reloc
->r_address());
7379 parser
.addFixups(src
, ld::Fixup::kindStoreARM64PageOff12
, target
);
7381 case ARM64_RELOC_GOT_LOAD_PAGE21
:
7382 if ( ! reloc
->r_pcrel() )
7383 throw "not pcrel and ARM64_RELOC_GOT_LOAD_PAGE21 not supported";
7384 if ( ! reloc
->r_extern() )
7385 throw "r_extern == 0 and ARM64_RELOC_GOT_LOAD_PAGE21 not supported";
7386 if ( reloc
->r_length() != 2 )
7387 throw "length != 2 and ARM64_RELOC_GOT_LOAD_PAGE21 not supported";
7388 if ( prefixRelocAddend
!= 0 )
7389 throw "ARM64_RELOC_ADDEND followed by ARM64_RELOC_GOT_LOAD_PAGE21 not supported";
7390 instruction
= contentValue
;
7391 target
.addend
= ((instruction
& 0x60000000) >> 29) | ((instruction
& 0x01FFFFE0) >> 3);
7392 if ( target
.addend
!= 0 )
7393 throw "non-zero addend with ARM64_RELOC_GOT_LOAD_PAGE21 is not supported";
7394 parser
.addFixups(src
, ld::Fixup::kindStoreARM64GOTLoadPage21
, target
);
7396 case ARM64_RELOC_GOT_LOAD_PAGEOFF12
:
7397 if ( reloc
->r_pcrel() )
7398 throw "pcrel and ARM64_RELOC_GOT_LOAD_PAGEOFF12 not supported";
7399 if ( ! reloc
->r_extern() )
7400 throw "r_extern == 0 and ARM64_RELOC_GOT_LOAD_PAGEOFF12 not supported";
7401 if ( reloc
->r_length() != 2 )
7402 throw "length != 2 and ARM64_RELOC_GOT_LOAD_PAGEOFF12 not supported";
7403 if ( prefixRelocAddend
!= 0 )
7404 throw "ARM64_RELOC_ADDEND followed by ARM64_RELOC_GOT_LOAD_PAGEOFF12 not supported";
7405 instruction
= contentValue
;
7406 target
.addend
= ((instruction
& 0x003FFC00) >> 10);
7407 parser
.addFixups(src
, ld::Fixup::kindStoreARM64GOTLoadPageOff12
, target
);
7409 case ARM64_RELOC_TLVP_LOAD_PAGE21
:
7410 if ( ! reloc
->r_pcrel() )
7411 throw "not pcrel and ARM64_RELOC_TLVP_LOAD_PAGE21 not supported";
7412 if ( ! reloc
->r_extern() )
7413 throw "r_extern == 0 and ARM64_RELOC_TLVP_LOAD_PAGE21 not supported";
7414 if ( reloc
->r_length() != 2 )
7415 throw "length != 2 and ARM64_RELOC_TLVP_LOAD_PAGE21 not supported";
7416 if ( prefixRelocAddend
!= 0 )
7417 throw "ARM64_RELOC_ADDEND followed by ARM64_RELOC_TLVP_LOAD_PAGE21 not supported";
7418 instruction
= contentValue
;
7419 target
.addend
= ((instruction
& 0x60000000) >> 29) | ((instruction
& 0x01FFFFE0) >> 3);
7420 if ( target
.addend
!= 0 )
7421 throw "non-zero addend with ARM64_RELOC_GOT_LOAD_PAGE21 is not supported";
7422 parser
.addFixups(src
, ld::Fixup::kindStoreARM64TLVPLoadPage21
, target
);
7424 case ARM64_RELOC_TLVP_LOAD_PAGEOFF12
:
7425 if ( reloc
->r_pcrel() )
7426 throw "pcrel and ARM64_RELOC_TLVP_LOAD_PAGEOFF12 not supported";
7427 if ( ! reloc
->r_extern() )
7428 throw "r_extern == 0 and ARM64_RELOC_TLVP_LOAD_PAGEOFF12 not supported";
7429 if ( reloc
->r_length() != 2 )
7430 throw "length != 2 and ARM64_RELOC_TLVP_LOAD_PAGEOFF12 not supported";
7431 if ( prefixRelocAddend
!= 0 )
7432 throw "ARM64_RELOC_ADDEND followed by ARM64_RELOC_TLVP_LOAD_PAGEOFF12 not supported";
7433 instruction
= contentValue
;
7434 target
.addend
= ((instruction
& 0x003FFC00) >> 10);
7435 parser
.addFixups(src
, ld::Fixup::kindStoreARM64TLVPLoadPageOff12
, target
);
7437 case ARM64_RELOC_SUBTRACTOR
:
7438 if ( reloc
->r_pcrel() )
7439 throw "ARM64_RELOC_SUBTRACTOR cannot be pc-relative";
7440 if ( reloc
->r_length() < 2 )
7441 throw "ARM64_RELOC_SUBTRACTOR must have r_length of 2 or 3";
7442 if ( !reloc
->r_extern() )
7443 throw "ARM64_RELOC_SUBTRACTOR must have r_extern=1";
7444 if ( nextReloc
->r_type() != ARM64_RELOC_UNSIGNED
)
7445 throw "ARM64_RELOC_SUBTRACTOR must be followed by ARM64_RELOC_UNSIGNED";
7446 if ( prefixRelocAddend
!= 0 )
7447 throw "ARM64_RELOC_ADDEND followed by ARM64_RELOC_SUBTRACTOR not supported";
7449 if ( nextReloc
->r_pcrel() )
7450 throw "ARM64_RELOC_UNSIGNED following a ARM64_RELOC_SUBTRACTOR cannot be pc-relative";
7451 if ( nextReloc
->r_length() != reloc
->r_length() )
7452 throw "ARM64_RELOC_UNSIGNED following a ARM64_RELOC_SUBTRACTOR must have same r_length";
7453 if ( nextReloc
->r_extern() ) {
7454 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(nextReloc
->r_symbolnum());
7455 // use direct reference for local symbols
7456 if ( ((sym
.n_type() & N_TYPE
) == N_SECT
) && (((sym
.n_type() & N_EXT
) == 0) || (parser
.nameFromSymbol(sym
)[0] == 'L')) ) {
7457 parser
.findTargetFromAddressAndSectionNum(sym
.n_value(), sym
.n_sect(), toTarget
);
7458 toTarget
.addend
= contentValue
;
7459 useDirectBinding
= true;
7462 toTarget
.name
= parser
.nameFromSymbol(sym
);
7463 toTarget
.weakImport
= parser
.weakImportFromSymbol(sym
);
7464 toTarget
.addend
= contentValue
;
7465 useDirectBinding
= false;
7469 parser
.findTargetFromAddressAndSectionNum(contentValue
, nextReloc
->r_symbolnum(), toTarget
);
7470 useDirectBinding
= (toTarget
.atom
->scope() == ld::Atom::scopeTranslationUnit
);
7472 if ( useDirectBinding
)
7473 parser
.addFixup(src
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, toTarget
.atom
);
7475 parser
.addFixup(src
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, toTarget
.weakImport
, toTarget
.name
);
7476 parser
.addFixup(src
, ld::Fixup::k2of4
, ld::Fixup::kindAddAddend
, toTarget
.addend
);
7477 if ( target
.atom
== NULL
)
7478 parser
.addFixup(src
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractTargetAddress
, false, target
.name
);
7480 parser
.addFixup(src
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractTargetAddress
, target
.atom
);
7481 if ( reloc
->r_length() == 2 )
7482 parser
.addFixup(src
, ld::Fixup::k4of4
, ld::Fixup::kindStoreLittleEndian32
);
7484 parser
.addFixup(src
, ld::Fixup::k4of4
, ld::Fixup::kindStoreLittleEndian64
);
7486 case ARM64_RELOC_POINTER_TO_GOT
:
7487 if ( ! reloc
->r_extern() )
7488 throw "r_extern == 0 and ARM64_RELOC_POINTER_TO_GOT not supported";
7489 if ( prefixRelocAddend
!= 0 )
7490 throw "ARM64_RELOC_ADDEND followed by ARM64_RELOC_POINTER_TO_GOT not supported";
7491 if ( reloc
->r_pcrel() ) {
7492 if ( reloc
->r_length() != 2 )
7493 throw "r_length != 2 and r_extern = 1 and ARM64_RELOC_POINTER_TO_GOT not supported";
7494 parser
.addFixups(src
, ld::Fixup::kindStoreARM64PCRelToGOT
, target
);
7497 if ( reloc
->r_length() != 3 )
7498 throw "r_length != 3 and r_extern = 0 and ARM64_RELOC_POINTER_TO_GOT not supported";
7499 parser
.addFixups(src
, ld::Fixup::kindStoreARM64PointerToGOT
, target
);
7503 throwf("unknown relocation type %d", reloc
->r_type());
7510 template <typename A
>
7511 bool ObjC1ClassSection
<A
>::addRelocFixup(class Parser
<A
>& parser
, const macho_relocation_info
<P
>* reloc
)
7514 FixedSizeSection
<A
>::addRelocFixup(parser
, reloc
);
7516 assert(0 && "needs template specialization");
7521 bool ObjC1ClassSection
<x86
>::addRelocFixup(class Parser
<x86
>& parser
, const macho_relocation_info
<x86::P
>* reloc
)
7523 // if this is the reloc for the super class name string, add implicit reference to super class
7524 if ( ((reloc
->r_address() & R_SCATTERED
) == 0) && (reloc
->r_type() == GENERIC_RELOC_VANILLA
) ) {
7525 assert( reloc
->r_length() == 2 );
7526 assert( ! reloc
->r_pcrel() );
7528 const macho_section
<P
>* sect
= this->machoSection();
7529 Parser
<x86
>::SourceLocation src
;
7530 uint32_t srcAddr
= sect
->addr() + reloc
->r_address();
7531 src
.atom
= this->findAtomByAddress(srcAddr
);
7532 src
.offsetInAtom
= srcAddr
- src
.atom
->objectAddress();
7533 if ( src
.offsetInAtom
== 4 ) {
7534 Parser
<x86
>::TargetDesc stringTarget
;
7535 const uint8_t* fixUpPtr
= file().fileContent() + sect
->offset() + reloc
->r_address();
7536 uint32_t contentValue
= LittleEndian::get32(*((uint32_t*)fixUpPtr
));
7537 parser
.findTargetFromAddressAndSectionNum(contentValue
, reloc
->r_symbolnum(), stringTarget
);
7539 assert(stringTarget
.atom
!= NULL
);
7540 assert(stringTarget
.atom
->contentType() == ld::Atom::typeCString
);
7541 const char* superClassBaseName
= (char*)stringTarget
.atom
->rawContentPointer();
7542 char* superClassName
= new char[strlen(superClassBaseName
) + 20];
7543 strcpy(superClassName
, ".objc_class_name_");
7544 strcat(superClassName
, superClassBaseName
);
7546 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindSetTargetAddress
, false, superClassName
);
7550 return FixedSizeSection
<x86
>::addRelocFixup(parser
, reloc
);
7555 template <typename A
>
7556 bool Objc1ClassReferences
<A
>::addRelocFixup(class Parser
<A
>& parser
, const macho_relocation_info
<P
>* reloc
)
7559 PointerToCStringSection
<A
>::addRelocFixup(parser
, reloc
);
7561 assert(0 && "needs template specialization");
7568 bool Objc1ClassReferences
<x86
>::addRelocFixup(class Parser
<x86
>& parser
, const macho_relocation_info
<x86::P
>* reloc
)
7570 // add implict class refs, fixups not usable yet, so look at relocations
7571 assert( (reloc
->r_address() & R_SCATTERED
) == 0 );
7572 assert( reloc
->r_type() == GENERIC_RELOC_VANILLA
);
7573 assert( reloc
->r_length() == 2 );
7574 assert( ! reloc
->r_pcrel() );
7576 const macho_section
<P
>* sect
= this->machoSection();
7577 Parser
<x86
>::SourceLocation src
;
7578 uint32_t srcAddr
= sect
->addr() + reloc
->r_address();
7579 src
.atom
= this->findAtomByAddress(srcAddr
);
7580 src
.offsetInAtom
= srcAddr
- src
.atom
->objectAddress();
7581 Parser
<x86
>::TargetDesc stringTarget
;
7582 const uint8_t* fixUpPtr
= file().fileContent() + sect
->offset() + reloc
->r_address();
7583 uint32_t contentValue
= LittleEndian::get32(*((uint32_t*)fixUpPtr
));
7584 parser
.findTargetFromAddressAndSectionNum(contentValue
, reloc
->r_symbolnum(), stringTarget
);
7586 assert(stringTarget
.atom
!= NULL
);
7587 assert(stringTarget
.atom
->contentType() == ld::Atom::typeCString
);
7588 const char* baseClassName
= (char*)stringTarget
.atom
->rawContentPointer();
7589 char* objcClassName
= new char[strlen(baseClassName
) + 20];
7590 strcpy(objcClassName
, ".objc_class_name_");
7591 strcat(objcClassName
, baseClassName
);
7593 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindSetTargetAddress
, false, objcClassName
);
7596 return PointerToCStringSection
<x86
>::addRelocFixup(parser
, reloc
);
7599 #if SUPPORT_ARCH_arm64
7601 void Section
<arm64
>::addLOH(class Parser
<arm64
>& parser
, int kind
, int count
, const uint64_t addrs
[]) {
7603 case LOH_ARM64_ADRP_ADRP
:
7604 case LOH_ARM64_ADRP_LDR
:
7605 case LOH_ARM64_ADRP_ADD
:
7606 case LOH_ARM64_ADRP_LDR_GOT
:
7608 warning("arm64 Linker Optimiztion Hint %d has wrong number of arguments", kind
);
7610 case LOH_ARM64_ADRP_ADD_LDR
:
7611 case LOH_ARM64_ADRP_LDR_GOT_LDR
:
7612 case LOH_ARM64_ADRP_ADD_STR
:
7613 case LOH_ARM64_ADRP_LDR_GOT_STR
:
7615 warning("arm64 Linker Optimiztion Hint %d has wrong number of arguments", kind
);
7618 // pick lowest address in tuple for use as offsetInAtom
7619 uint64_t lowestAddress
= addrs
[0];
7620 for(int i
=1; i
< count
; ++i
) {
7621 if ( addrs
[i
] < lowestAddress
)
7622 lowestAddress
= addrs
[i
];
7624 // verify all other address are in same atom
7625 Atom
<arm64
>* inAtom
= parser
.findAtomByAddress(lowestAddress
);
7626 const uint64_t atomStartAddr
= inAtom
->objectAddress();
7627 const uint64_t atomEndAddr
= atomStartAddr
+ inAtom
->size();
7628 for(int i
=0; i
< count
; ++i
) {
7629 if ( (addrs
[i
] < atomStartAddr
) || (addrs
[i
] >= atomEndAddr
) ) {
7630 warning("arm64 Linker Optimiztion Hint addresses are not in same atom: 0x%08llX and 0x%08llX",
7631 lowestAddress
, addrs
[i
]);
7632 return; // skip this LOH
7634 if ( (addrs
[i
] & 0x3) != 0 ) {
7635 warning("arm64 Linker Optimiztion Hint address is not 4-byte aligned: 0x%08llX", addrs
[i
]);
7636 return; // skip this LOH
7638 if ( (addrs
[i
] - lowestAddress
) > 0xFFFF ) {
7639 if ( parser
.verboseOptimizationHints() ) {
7640 warning("arm64 Linker Optimiztion Hint addresses are too far apart: 0x%08llX and 0x%08llX",
7641 lowestAddress
, addrs
[i
]);
7643 return; // skip this LOH
7647 // encoded kind, count, and address deltas in 64-bit addend
7648 ld::Fixup::LOH_arm64 extra
;
7650 extra
.info
.kind
= kind
;
7651 extra
.info
.count
= count
-1;
7652 extra
.info
.delta1
= (addrs
[0] - lowestAddress
) >> 2;
7653 extra
.info
.delta2
= (count
> 1) ? ((addrs
[1] - lowestAddress
) >> 2) : 0;
7654 extra
.info
.delta3
= (count
> 2) ? ((addrs
[2] - lowestAddress
) >> 2) : 0;
7655 extra
.info
.delta4
= (count
> 3) ? ((addrs
[3] - lowestAddress
) >> 2) : 0;
7656 typename Parser
<arm64
>::SourceLocation
src(inAtom
, lowestAddress
- inAtom
->objectAddress());
7657 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindLinkerOptimizationHint
, extra
.addend
);
7662 template <typename A
>
7663 void Section
<A
>::addLOH(class Parser
<A
>& parser
, int kind
, int count
, const uint64_t addrs
[]) {
7667 template <typename A
>
7668 void Section
<A
>::makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
&)
7670 const macho_section
<P
>* sect
= this->machoSection();
7671 if ( sect
->reloff() + (sect
->nreloc() * sizeof(macho_relocation_info
<P
>)) > parser
.fileLength() )
7672 throwf("relocations for section %s/%s extends beyond end of file,", sect
->segname(), Section
<A
>::makeSectionName(sect
) );
7673 const macho_relocation_info
<P
>* relocs
= (macho_relocation_info
<P
>*)(file().fileContent() + sect
->reloff());
7674 const uint32_t relocCount
= sect
->nreloc();
7675 for (uint32_t r
= 0; r
< relocCount
; ++r
) {
7677 if ( this->addRelocFixup(parser
, &relocs
[r
]) )
7680 catch (const char* msg
) {
7681 throwf("in section %s,%s reloc %u: %s", sect
->segname(), Section
<A
>::makeSectionName(sect
), r
, msg
);
7685 // add follow-on fixups if .o file is missing .subsections_via_symbols
7686 if ( this->addFollowOnFixups() ) {
7687 Atom
<A
>* end
= &_endAtoms
[-1];
7688 for(Atom
<A
>* p
= _beginAtoms
; p
< end
; ++p
) {
7689 typename Parser
<A
>::SourceLocation
src(p
, 0);
7690 Atom
<A
>* nextAtom
= &p
[1];
7691 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindNoneFollowOn
, nextAtom
);
7694 else if ( this->type() == ld::Section::typeCode
) {
7695 // if FDE broke text not at a symbol, use followOn to keep code together
7696 Atom
<A
>* end
= &_endAtoms
[-1];
7697 for(Atom
<A
>* p
= _beginAtoms
; p
< end
; ++p
) {
7698 typename Parser
<A
>::SourceLocation
src(p
, 0);
7699 Atom
<A
>* nextAtom
= &p
[1];
7700 if ( (p
->symbolTableInclusion() == ld::Atom::symbolTableIn
) && (nextAtom
->symbolTableInclusion() == ld::Atom::symbolTableNotIn
) ) {
7701 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindNoneFollowOn
, nextAtom
);
7705 if ( !this->_altEntries
.empty() && !this->addFollowOnFixups() ) {
7706 if ( _altEntries
.count(_beginAtoms
) != 0 )
7707 warning("N_ALT_ENTRY bit set on first atom in section %s/%s", sect
->segname(), Section
<A
>::makeSectionName(sect
));
7709 Atom
<A
>* end
= &_endAtoms
[-1];
7710 for(Atom
<A
>* p
= _beginAtoms
; p
< end
; ++p
) {
7711 Atom
<A
>* nextAtom
= &p
[1];
7712 // <rdar://problem/22960070> support alt_entry aliases (alias process already added followOn, don't repeat)
7713 if ( (_altEntries
.count(nextAtom
) != 0) && (p
->_objAddress
!= nextAtom
->_objAddress
) ) {
7714 typename Parser
<A
>::SourceLocation
src(p
, 0);
7715 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindNoneFollowOn
, nextAtom
);
7716 typename Parser
<A
>::SourceLocation
src2(nextAtom
, 0);
7717 parser
.addFixup(src2
, ld::Fixup::k1of1
, ld::Fixup::kindNoneGroupSubordinate
, p
);
7722 // <rdar://problem/9218847> track data-in-code
7723 if ( parser
.hasDataInCodeLabels() && (this->type() == ld::Section::typeCode
) ) {
7724 for (uint32_t i
=0; i
< parser
.symbolCount(); ++i
) {
7725 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(i
);
7727 if ( (sym
.n_type() & N_STAB
) != 0 )
7729 // ignore non-definitions
7730 if ( (sym
.n_type() & N_TYPE
) != N_SECT
)
7733 // 'L' labels do not denote atom breaks
7734 const char* symbolName
= parser
.nameFromSymbol(sym
);
7735 if ( symbolName
[0] == 'L' ) {
7736 if ( strncmp(symbolName
, "L$start$", 8) == 0 ) {
7737 ld::Fixup::Kind kind
= ld::Fixup::kindNone
;
7738 if ( strncmp(&symbolName
[8], "data$", 5) == 0 )
7739 kind
= ld::Fixup::kindDataInCodeStartData
;
7740 else if ( strncmp(&symbolName
[8], "code$", 5) == 0 )
7741 kind
= ld::Fixup::kindDataInCodeEnd
;
7742 else if ( strncmp(&symbolName
[8], "jt8$", 4) == 0 )
7743 kind
= ld::Fixup::kindDataInCodeStartJT8
;
7744 else if ( strncmp(&symbolName
[8], "jt16$", 4) == 0 )
7745 kind
= ld::Fixup::kindDataInCodeStartJT16
;
7746 else if ( strncmp(&symbolName
[8], "jt32$", 4) == 0 )
7747 kind
= ld::Fixup::kindDataInCodeStartJT32
;
7748 else if ( strncmp(&symbolName
[8], "jta32$", 4) == 0 )
7749 kind
= ld::Fixup::kindDataInCodeStartJTA32
;
7751 warning("unknown L$start$ label %s in file %s", symbolName
, this->file().path());
7752 if ( kind
!= ld::Fixup::kindNone
) {
7753 Atom
<A
>* inAtom
= parser
.findAtomByAddress(sym
.n_value());
7754 typename Parser
<A
>::SourceLocation
src(inAtom
, sym
.n_value() - inAtom
->objectAddress());
7755 parser
.addFixup(src
, ld::Fixup::k1of1
, kind
);
7762 // <rdar://problem/11150575> Handle LC_DATA_IN_CODE in object files
7763 if ( this->type() == ld::Section::typeCode
) {
7764 const pint_t startAddr
= this->_machOSection
->addr();
7765 const pint_t endAddr
= startAddr
+ this->_machOSection
->size();
7766 for ( const macho_data_in_code_entry
<P
>* p
= parser
.dataInCodeStart(); p
!= parser
.dataInCodeEnd(); ++p
) {
7767 if ( (p
->offset() >= startAddr
) && (p
->offset() < endAddr
) ) {
7768 ld::Fixup::Kind kind
= ld::Fixup::kindNone
;
7769 switch ( p
->kind() ) {
7770 case DICE_KIND_DATA
:
7771 kind
= ld::Fixup::kindDataInCodeStartData
;
7773 case DICE_KIND_JUMP_TABLE8
:
7774 kind
= ld::Fixup::kindDataInCodeStartJT8
;
7776 case DICE_KIND_JUMP_TABLE16
:
7777 kind
= ld::Fixup::kindDataInCodeStartJT16
;
7779 case DICE_KIND_JUMP_TABLE32
:
7780 kind
= ld::Fixup::kindDataInCodeStartJT32
;
7782 case DICE_KIND_ABS_JUMP_TABLE32
:
7783 kind
= ld::Fixup::kindDataInCodeStartJTA32
;
7786 kind
= ld::Fixup::kindDataInCodeStartData
;
7787 warning("uknown LC_DATA_IN_CODE kind (%d) at offset 0x%08X", p
->kind(), p
->offset());
7790 Atom
<A
>* inAtom
= parser
.findAtomByAddress(p
->offset());
7791 typename Parser
<A
>::SourceLocation
srcStart(inAtom
, p
->offset() - inAtom
->objectAddress());
7792 parser
.addFixup(srcStart
, ld::Fixup::k1of1
, kind
);
7793 typename Parser
<A
>::SourceLocation
srcEnd(inAtom
, p
->offset() + p
->length() - inAtom
->objectAddress());
7794 parser
.addFixup(srcEnd
, ld::Fixup::k1of1
, ld::Fixup::kindDataInCodeEnd
);
7799 // <rdar://problem/11945700> convert linker optimization hints into internal format
7800 if ( this->type() == ld::Section::typeCode
&& parser
.hasOptimizationHints() ) {
7801 const pint_t startAddr
= this->_machOSection
->addr();
7802 const pint_t endAddr
= startAddr
+ this->_machOSection
->size();
7803 for (const uint8_t* p
= parser
.optimizationHintsStart(); p
< parser
.optimizationHintsEnd(); ) {
7805 int32_t kind
= read_uleb128(&p
, parser
.optimizationHintsEnd());
7806 if ( kind
== 0 ) // padding at end of loh buffer
7809 warning("malformed uleb128 kind in LC_LINKER_OPTIMIZATION_HINTS");
7812 int32_t count
= read_uleb128(&p
, parser
.optimizationHintsEnd());
7813 if ( count
== -1 ) {
7814 warning("malformed uleb128 count in LC_LINKER_OPTIMIZATION_HINTS");
7818 warning("address count > 3 in LC_LINKER_OPTIMIZATION_HINTS");
7821 for (int32_t i
=0; i
< count
; ++i
) {
7822 addrs
[i
] = read_uleb128(&p
, parser
.optimizationHintsEnd());
7824 if ( (startAddr
<= addrs
[0]) && (addrs
[0] < endAddr
) ) {
7825 this->addLOH(parser
, kind
, count
, addrs
);
7826 //fprintf(stderr, "kind=%d", kind);
7827 //for (int32_t i=0; i < count; ++i) {
7828 // fprintf(stderr, ", addr=0x%08llX", addrs[i]);
7830 //fprintf(stderr, "\n");
7836 // add follow-on fixups for aliases
7837 if ( _hasAliases
) {
7838 for(Atom
<A
>* p
= _beginAtoms
; p
< _endAtoms
; ++p
) {
7839 if ( p
->isAlias() && ! this->addFollowOnFixups() ) {
7840 Atom
<A
>* targetOfAlias
= &p
[1];
7841 assert(p
< &_endAtoms
[-1]);
7842 assert(p
->_objAddress
== targetOfAlias
->_objAddress
);
7843 typename Parser
<A
>::SourceLocation
src(p
, 0);
7844 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindNoneFollowOn
, targetOfAlias
);
7853 // main function used by linker to instantiate ld::Files
7855 ld::relocatable::File
* parse(const uint8_t* fileContent
, uint64_t fileLength
,
7856 const char* path
, time_t modTime
, ld::File::Ordinal ordinal
, const ParserOptions
& opts
)
7858 switch ( opts
.architecture
) {
7859 #if SUPPORT_ARCH_x86_64
7860 case CPU_TYPE_X86_64
:
7861 if ( mach_o::relocatable::Parser
<x86_64
>::validFile(fileContent
) )
7862 return mach_o::relocatable::Parser
<x86_64
>::parse(fileContent
, fileLength
, path
, modTime
, ordinal
, opts
);
7865 #if SUPPORT_ARCH_i386
7867 if ( mach_o::relocatable::Parser
<x86
>::validFile(fileContent
) )
7868 return mach_o::relocatable::Parser
<x86
>::parse(fileContent
, fileLength
, path
, modTime
, ordinal
, opts
);
7871 #if SUPPORT_ARCH_arm_any
7873 if ( mach_o::relocatable::Parser
<arm
>::validFile(fileContent
, opts
.objSubtypeMustMatch
, opts
.subType
) )
7874 return mach_o::relocatable::Parser
<arm
>::parse(fileContent
, fileLength
, path
, modTime
, ordinal
, opts
);
7877 #if SUPPORT_ARCH_arm64
7878 case CPU_TYPE_ARM64
:
7879 if ( mach_o::relocatable::Parser
<arm64
>::validFile(fileContent
, opts
.objSubtypeMustMatch
, opts
.subType
) )
7880 return mach_o::relocatable::Parser
<arm64
>::parse(fileContent
, fileLength
, path
, modTime
, ordinal
, opts
);
7888 // used by archive reader to validate member object file
7890 bool isObjectFile(const uint8_t* fileContent
, uint64_t fileLength
, const ParserOptions
& opts
)
7892 switch ( opts
.architecture
) {
7893 case CPU_TYPE_X86_64
:
7894 return ( mach_o::relocatable::Parser
<x86_64
>::validFile(fileContent
) );
7896 return ( mach_o::relocatable::Parser
<x86
>::validFile(fileContent
) );
7898 return ( mach_o::relocatable::Parser
<arm
>::validFile(fileContent
, opts
.objSubtypeMustMatch
, opts
.subType
) );
7899 case CPU_TYPE_ARM64
:
7900 return ( mach_o::relocatable::Parser
<arm64
>::validFile(fileContent
, opts
.objSubtypeMustMatch
, opts
.subType
) );
7906 // used by linker to infer architecture when no -arch is on command line
7908 bool isObjectFile(const uint8_t* fileContent
, cpu_type_t
* result
, cpu_subtype_t
* subResult
, Options::Platform
* platform
)
7910 if ( mach_o::relocatable::Parser
<x86_64
>::validFile(fileContent
) ) {
7911 *result
= CPU_TYPE_X86_64
;
7912 const macho_header
<Pointer64
<LittleEndian
> >* header
= (const macho_header
<Pointer64
<LittleEndian
> >*)fileContent
;
7913 *subResult
= header
->cpusubtype();
7914 *platform
= Parser
<x86_64
>::findPlatform(header
);
7917 if ( mach_o::relocatable::Parser
<x86
>::validFile(fileContent
) ) {
7918 const macho_header
<Pointer32
<LittleEndian
> >* header
= (const macho_header
<Pointer32
<LittleEndian
> >*)fileContent
;
7919 *result
= CPU_TYPE_I386
;
7920 *subResult
= CPU_SUBTYPE_X86_ALL
;
7921 *platform
= Parser
<x86
>::findPlatform(header
);
7924 if ( mach_o::relocatable::Parser
<arm
>::validFile(fileContent
, false, 0) ) {
7925 const macho_header
<Pointer32
<LittleEndian
> >* header
= (const macho_header
<Pointer32
<LittleEndian
> >*)fileContent
;
7926 *result
= CPU_TYPE_ARM
;
7927 *subResult
= header
->cpusubtype();
7928 *platform
= Parser
<arm
>::findPlatform(header
);
7931 if ( mach_o::relocatable::Parser
<arm64
>::validFile(fileContent
, false, 0) ) {
7932 const macho_header
<Pointer64
<LittleEndian
> >* header
= (const macho_header
<Pointer64
<LittleEndian
> >*)fileContent
;
7933 *result
= CPU_TYPE_ARM64
;
7934 *subResult
= header
->cpusubtype();
7935 *platform
= Parser
<arm64
>::findPlatform(header
);
7942 // used by linker is error messages to describe bad .o file
7944 const char* archName(const uint8_t* fileContent
)
7946 if ( mach_o::relocatable::Parser
<x86_64
>::validFile(fileContent
) ) {
7947 return mach_o::relocatable::Parser
<x86_64
>::fileKind(fileContent
);
7949 if ( mach_o::relocatable::Parser
<x86
>::validFile(fileContent
) ) {
7950 return mach_o::relocatable::Parser
<x86
>::fileKind(fileContent
);
7952 if ( mach_o::relocatable::Parser
<arm
>::validFile(fileContent
, false, 0) ) {
7953 return mach_o::relocatable::Parser
<arm
>::fileKind(fileContent
);
7959 // Used by archive reader when -ObjC option is specified
7961 bool hasObjC2Categories(const uint8_t* fileContent
)
7963 if ( mach_o::relocatable::Parser
<x86_64
>::validFile(fileContent
) ) {
7964 return mach_o::relocatable::Parser
<x86_64
>::hasObjC2Categories(fileContent
);
7966 else if ( mach_o::relocatable::Parser
<arm
>::validFile(fileContent
, false, 0) ) {
7967 return mach_o::relocatable::Parser
<arm
>::hasObjC2Categories(fileContent
);
7969 else if ( mach_o::relocatable::Parser
<x86
>::validFile(fileContent
, false, 0) ) {
7970 return mach_o::relocatable::Parser
<x86
>::hasObjC2Categories(fileContent
);
7972 #if SUPPORT_ARCH_arm64
7973 else if ( mach_o::relocatable::Parser
<arm64
>::validFile(fileContent
, false, 0) ) {
7974 return mach_o::relocatable::Parser
<arm64
>::hasObjC2Categories(fileContent
);
7981 // Used by archive reader when -ObjC option is specified
7983 bool hasObjC1Categories(const uint8_t* fileContent
)
7985 if ( mach_o::relocatable::Parser
<x86
>::validFile(fileContent
, false, 0) ) {
7986 return mach_o::relocatable::Parser
<x86
>::hasObjC1Categories(fileContent
);
7992 // Used by bitcode obfuscator to get a list of non local symbols from object file
7994 bool getNonLocalSymbols(const uint8_t* fileContent
, std::vector
<const char*> &syms
)
7996 if ( mach_o::relocatable::Parser
<x86_64
>::validFile(fileContent
) ) {
7997 return mach_o::relocatable::Parser
<x86_64
>::getNonLocalSymbols(fileContent
, syms
);
7999 else if ( mach_o::relocatable::Parser
<arm
>::validFile(fileContent
, false, 0) ) {
8000 return mach_o::relocatable::Parser
<arm
>::getNonLocalSymbols(fileContent
, syms
);
8002 else if ( mach_o::relocatable::Parser
<x86
>::validFile(fileContent
, false, 0) ) {
8003 return mach_o::relocatable::Parser
<x86
>::getNonLocalSymbols(fileContent
, syms
);
8005 else if ( mach_o::relocatable::Parser
<arm64
>::validFile(fileContent
, false, 0) ) {
8006 return mach_o::relocatable::Parser
<arm64
>::getNonLocalSymbols(fileContent
, syms
);
8013 } // namespace relocatable
8014 } // namespace mach_o