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 _hasllvmProfiling(false),
90 _objcHasCategoryClassPropertiesField(false),
91 _srcKind(kSourceUnknown
) { }
94 // overrides of ld::File
95 virtual bool forEachAtom(ld::File::AtomHandler
&) const;
96 virtual bool justInTimeforEachAtom(const char* name
, ld::File::AtomHandler
&) const
98 virtual uint32_t minOSVersion() const { return _minOSVersion
; }
99 virtual uint32_t platform() const { return _platform
; }
101 // overrides of ld::relocatable::File
102 virtual ObjcConstraint
objCConstraint() const { return _objConstraint
; }
103 virtual bool objcHasCategoryClassPropertiesField() const
104 { return _objcHasCategoryClassPropertiesField
; }
105 virtual uint32_t cpuSubType() const { return _cpuSubType
; }
106 virtual DebugInfoKind
debugInfo() const { return _debugInfoKind
; }
107 virtual const std::vector
<ld::relocatable::File::Stab
>* stabs() const { return &_stabs
; }
108 virtual bool canScatterAtoms() const { return _canScatterAtoms
; }
109 virtual bool hasllvmProfiling() const { return _hasllvmProfiling
; }
110 virtual const char* translationUnitSource() const;
111 virtual LinkerOptionsList
* linkerOptions() const { return &_linkerOptions
; }
112 virtual const ToolVersionList
& toolVersions() const { return _toolVersions
; }
113 virtual uint8_t swiftVersion() const { return _swiftVersion
; }
114 virtual ld::Bitcode
* getBitcode() const { return _bitcode
.get(); }
115 virtual SourceKind
sourceKind() const { return _srcKind
; }
117 virtual const uint8_t* fileContent() const { return _fileContent
; }
118 virtual const std::vector
<AstTimeAndPath
>* astFiles() const { return &_astFiles
; }
120 void setHasllvmProfiling() { _hasllvmProfiling
= true; }
122 friend class Atom
<A
>;
123 friend class Section
<A
>;
124 friend class Parser
<A
>;
125 friend class CFISection
<A
>::OAS
;
127 typedef typename
A::P P
;
129 const uint8_t* _fileContent
;
130 Section
<A
>** _sectionsArray
;
131 uint8_t* _atomsArray
;
132 uint8_t* _aliasAtomsArray
;
133 uint32_t _sectionsArrayCount
;
134 uint32_t _atomsArrayCount
;
135 uint32_t _aliasAtomsArrayCount
;
136 std::vector
<ld::Fixup
> _fixups
;
137 std::vector
<ld::Atom::UnwindInfo
> _unwindInfos
;
138 std::vector
<ld::Atom::LineInfo
> _lineInfos
;
139 std::vector
<ld::relocatable::File::Stab
>_stabs
;
140 std::vector
<AstTimeAndPath
> _astFiles
;
141 ld::relocatable::File::DebugInfoKind _debugInfoKind
;
142 const char* _dwarfTranslationUnitPath
;
143 const macho_section
<P
>* _dwarfDebugInfoSect
;
144 const macho_section
<P
>* _dwarfDebugAbbrevSect
;
145 const macho_section
<P
>* _dwarfDebugLineSect
;
146 const macho_section
<P
>* _dwarfDebugStringSect
;
147 ld::File::ObjcConstraint _objConstraint
;
148 uint8_t _swiftVersion
;
149 uint32_t _cpuSubType
;
150 uint32_t _minOSVersion
;
151 Options::Platform _platform
;
152 bool _canScatterAtoms
;
153 bool _hasllvmProfiling
;
154 bool _objcHasCategoryClassPropertiesField
;
155 std::vector
<std::vector
<const char*> > _linkerOptions
;
156 std::unique_ptr
<ld::Bitcode
> _bitcode
;
158 ToolVersionList _toolVersions
;
162 template <typename A
>
163 class Section
: public ld::Section
166 typedef typename
A::P::uint_t pint_t
;
167 typedef typename
A::P P
;
168 typedef typename
A::P::E E
;
170 virtual ~Section() { }
171 class File
<A
>& file() const { return _file
; }
172 const macho_section
<P
>* machoSection() const { return _machOSection
; }
173 uint32_t sectionNum(class Parser
<A
>&) const;
174 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
);
175 virtual ld::Atom::ContentType
contentType() { return ld::Atom::typeUnclassified
; }
176 virtual bool dontDeadStrip() { return (this->_machOSection
->flags() & S_ATTR_NO_DEAD_STRIP
); }
177 virtual bool dontDeadStripIfReferencesLive() { return ( (this->_machOSection
!= NULL
) && (this->_machOSection
->flags() & S_ATTR_LIVE_SUPPORT
) ); }
178 virtual Atom
<A
>* findAtomByAddress(pint_t addr
) { return this->findContentAtomByAddress(addr
, this->_beginAtoms
, this->_endAtoms
); }
179 virtual bool addFollowOnFixups() const { return ! _file
.canScatterAtoms(); }
180 virtual uint32_t appendAtoms(class Parser
<A
>& parser
, uint8_t* buffer
,
181 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
182 const struct Parser
<A
>::CFI_CU_InfoArrays
&) = 0;
183 virtual uint32_t computeAtomCount(class Parser
<A
>& parser
,
184 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
185 const struct Parser
<A
>::CFI_CU_InfoArrays
&) = 0;
186 virtual void makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
&);
187 virtual bool addRelocFixup(class Parser
<A
>& parser
, const macho_relocation_info
<P
>*);
188 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const { return 0; }
189 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
190 const ld::IndirectBindingTable
& ind
) const { return false; }
191 virtual bool ignoreLabel(const char* label
) const { return false; }
192 static const char* makeSectionName(const macho_section
<typename
A::P
>* s
);
195 Section(File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
196 : ld::Section(makeSegmentName(s
), makeSectionName(s
), sectionType(s
)),
197 _file(f
), _machOSection(s
), _beginAtoms(NULL
), _endAtoms(NULL
), _hasAliases(false) { }
198 Section(File
<A
>& f
, const char* segName
, const char* sectName
, ld::Section::Type t
, bool hidden
=false)
199 : ld::Section(segName
, sectName
, t
, hidden
), _file(f
), _machOSection(NULL
),
200 _beginAtoms(NULL
), _endAtoms(NULL
), _hasAliases(false) { }
203 Atom
<A
>* findContentAtomByAddress(pint_t addr
, class Atom
<A
>* start
, class Atom
<A
>* end
);
204 uint32_t x86_64PcRelOffset(uint8_t r_type
);
205 void addLOH(class Parser
<A
>& parser
, int kind
, int count
, const uint64_t addrs
[]);
206 static const char* makeSegmentName(const macho_section
<typename
A::P
>* s
);
207 static bool readable(const macho_section
<typename
A::P
>* s
);
208 static bool writable(const macho_section
<typename
A::P
>* s
);
209 static bool exectuable(const macho_section
<typename
A::P
>* s
);
210 static ld::Section::Type
sectionType(const macho_section
<typename
A::P
>* s
);
213 const macho_section
<P
>* _machOSection
;
214 class Atom
<A
>* _beginAtoms
;
215 class Atom
<A
>* _endAtoms
;
217 std::set
<const class Atom
<A
>*> _altEntries
;
221 template <typename A
>
222 class CFISection
: public Section
<A
>
225 CFISection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
226 : Section
<A
>(f
, s
) { }
227 uint32_t cfiCount(Parser
<A
>& parser
);
229 virtual ld::Atom::ContentType
contentType() { return ld::Atom::typeCFI
; }
230 virtual uint32_t computeAtomCount(class Parser
<A
>& parser
, struct Parser
<A
>::LabelAndCFIBreakIterator
& it
, const struct Parser
<A
>::CFI_CU_InfoArrays
&);
231 virtual uint32_t appendAtoms(class Parser
<A
>& parser
, uint8_t* buffer
, struct Parser
<A
>::LabelAndCFIBreakIterator
& it
, const struct Parser
<A
>::CFI_CU_InfoArrays
&);
232 virtual void makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
&);
233 virtual bool addFollowOnFixups() const { return false; }
237 /// ObjectFileAddressSpace is used as a template parameter to UnwindCursor for parsing
238 /// dwarf CFI information in an object file.
243 typedef typename
A::P::uint_t pint_t
;
244 typedef typename
A::P P
;
245 typedef typename
A::P::E E
;
246 typedef typename
A::P::uint_t sint_t
;
248 OAS(CFISection
<A
>& ehFrameSection
, const uint8_t* ehFrameBuffer
) :
249 _ehFrameSection(ehFrameSection
),
250 _ehFrameContent(ehFrameBuffer
),
251 _ehFrameStartAddr(ehFrameSection
.machoSection()->addr()),
252 _ehFrameEndAddr(ehFrameSection
.machoSection()->addr()+ehFrameSection
.machoSection()->size()) {}
254 uint8_t get8(pint_t addr
) { return *((uint8_t*)mappedAddress(addr
)); }
255 uint16_t get16(pint_t addr
) { return E::get16(*((uint16_t*)mappedAddress(addr
))); }
256 uint32_t get32(pint_t addr
) { return E::get32(*((uint32_t*)mappedAddress(addr
))); }
257 uint64_t get64(pint_t addr
) { return E::get64(*((uint64_t*)mappedAddress(addr
))); }
258 pint_t
getP(pint_t addr
) { return P::getP(*((pint_t
*)mappedAddress(addr
))); }
259 uint64_t getULEB128(pint_t
& addr
, pint_t end
);
260 int64_t getSLEB128(pint_t
& addr
, pint_t end
);
261 pint_t
getEncodedP(pint_t
& addr
, pint_t end
, uint8_t encoding
);
263 const void* mappedAddress(pint_t addr
);
265 CFISection
<A
>& _ehFrameSection
;
266 const uint8_t* _ehFrameContent
;
267 pint_t _ehFrameStartAddr
;
268 pint_t _ehFrameEndAddr
;
272 typedef typename
A::P::uint_t pint_t
;
273 typedef libunwind::CFI_Atom_Info
<OAS
> CFI_Atom_Info
;
275 void cfiParse(class Parser
<A
>& parser
, uint8_t* buffer
, CFI_Atom_Info cfiArray
[], uint32_t& cfiCount
, const pint_t cuStarts
[], uint32_t cuCount
);
276 bool needsRelocating();
278 static bool bigEndian();
280 void addCiePersonalityFixups(class Parser
<A
>& parser
, const CFI_Atom_Info
* cieInfo
);
281 static void warnFunc(void* ref
, uint64_t funcAddr
, const char* msg
);
285 template <typename A
>
286 class CUSection
: public Section
<A
>
289 CUSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
290 : Section
<A
>(f
, s
) { }
292 typedef typename
A::P::uint_t pint_t
;
293 typedef typename
A::P P
;
294 typedef typename
A::P::E E
;
296 virtual uint32_t computeAtomCount(class Parser
<A
>& parser
, struct Parser
<A
>::LabelAndCFIBreakIterator
& it
, const struct Parser
<A
>::CFI_CU_InfoArrays
&) { return 0; }
297 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; }
298 virtual void makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
&);
299 virtual bool addFollowOnFixups() const { return false; }
302 pint_t functionStartAddress
;
303 uint32_t functionSymbolIndex
;
304 uint32_t rangeLength
;
305 uint32_t compactUnwindInfo
;
306 const char* personality
;
313 void parse(class Parser
<A
>& parser
, uint32_t cnt
, Info array
[]);
314 static bool encodingMeansUseDwarf(compact_unwind_encoding_t enc
);
319 const char* personalityName(class Parser
<A
>& parser
, const macho_relocation_info
<P
>* reloc
);
321 static int infoSorter(const void* l
, const void* r
);
326 template <typename A
>
327 class TentativeDefinitionSection
: public Section
<A
>
330 TentativeDefinitionSection(Parser
<A
>& parser
, File
<A
>& f
)
331 : Section
<A
>(f
, "__DATA", "__comm/tent", ld::Section::typeTentativeDefs
) {}
333 virtual ld::Atom::ContentType
contentType() { return ld::Atom::typeZeroFill
; }
334 virtual bool addFollowOnFixups() const { return false; }
335 virtual Atom
<A
>* findAtomByAddress(typename
A::P::uint_t addr
) { throw "TentativeDefinitionSection::findAtomByAddress() should never be called"; }
336 virtual uint32_t computeAtomCount(class Parser
<A
>& parser
, struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
337 const struct Parser
<A
>::CFI_CU_InfoArrays
&);
338 virtual uint32_t appendAtoms(class Parser
<A
>& parser
, uint8_t* buffer
,
339 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
340 const struct Parser
<A
>::CFI_CU_InfoArrays
&);
341 virtual void makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
&) {}
343 typedef typename
A::P::uint_t pint_t
;
344 typedef typename
A::P P
;
348 template <typename A
>
349 class AbsoluteSymbolSection
: public Section
<A
>
352 AbsoluteSymbolSection(Parser
<A
>& parser
, File
<A
>& f
)
353 : Section
<A
>(f
, "__DATA", "__abs", ld::Section::typeAbsoluteSymbols
, true) {}
355 virtual ld::Atom::ContentType
contentType() { return ld::Atom::typeUnclassified
; }
356 virtual bool dontDeadStrip() { return false; }
357 virtual ld::Atom::Alignment
alignmentForAddress(typename
A::P::uint_t addr
) { return ld::Atom::Alignment(0); }
358 virtual bool addFollowOnFixups() const { return false; }
359 virtual Atom
<A
>* findAtomByAddress(typename
A::P::uint_t addr
) { throw "AbsoluteSymbolSection::findAtomByAddress() should never be called"; }
360 virtual uint32_t computeAtomCount(class Parser
<A
>& parser
, struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
361 const struct Parser
<A
>::CFI_CU_InfoArrays
&);
362 virtual uint32_t appendAtoms(class Parser
<A
>& parser
, uint8_t* buffer
,
363 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
364 const struct Parser
<A
>::CFI_CU_InfoArrays
&);
365 virtual void makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
&) {}
366 virtual Atom
<A
>* findAbsAtomForValue(typename
A::P::uint_t
);
369 typedef typename
A::P::uint_t pint_t
;
370 typedef typename
A::P P
;
374 template <typename A
>
375 class SymboledSection
: public Section
<A
>
378 SymboledSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
);
379 virtual ld::Atom::ContentType
contentType() { return _type
; }
380 virtual bool dontDeadStrip();
381 virtual uint32_t computeAtomCount(class Parser
<A
>& parser
, struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
382 const struct Parser
<A
>::CFI_CU_InfoArrays
&);
383 virtual uint32_t appendAtoms(class Parser
<A
>& parser
, uint8_t* buffer
,
384 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
385 const struct Parser
<A
>::CFI_CU_InfoArrays
&);
387 typedef typename
A::P::uint_t pint_t
;
388 typedef typename
A::P P
;
390 ld::Atom::ContentType _type
;
394 template <typename A
>
395 class TLVDefsSection
: public SymboledSection
<A
>
398 TLVDefsSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
) :
399 SymboledSection
<A
>(parser
, f
, s
) { }
401 typedef typename
A::P::uint_t pint_t
;
403 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
) { return ld::Atom::Alignment(log2(sizeof(pint_t
))); }
410 template <typename A
>
411 class ImplicitSizeSection
: public Section
<A
>
414 ImplicitSizeSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
415 : Section
<A
>(f
, s
) { }
416 virtual uint32_t computeAtomCount(class Parser
<A
>& parser
, struct Parser
<A
>::LabelAndCFIBreakIterator
& it
, const struct Parser
<A
>::CFI_CU_InfoArrays
&);
417 virtual uint32_t appendAtoms(class Parser
<A
>& parser
, uint8_t* buffer
, struct Parser
<A
>::LabelAndCFIBreakIterator
& it
, const struct Parser
<A
>::CFI_CU_InfoArrays
&);
419 typedef typename
A::P::uint_t pint_t
;
420 typedef typename
A::P P
;
422 virtual bool addFollowOnFixups() const { return false; }
423 virtual const char* unlabeledAtomName(Parser
<A
>& parser
, pint_t addr
) = 0;
424 virtual ld::Atom::SymbolTableInclusion
symbolTableInclusion();
425 virtual pint_t
elementSizeAtAddress(pint_t addr
) = 0;
426 virtual ld::Atom::Scope
scopeAtAddress(Parser
<A
>& parser
, pint_t addr
) { return ld::Atom::scopeLinkageUnit
; }
427 virtual bool useElementAt(Parser
<A
>& parser
,
428 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
, pint_t addr
) = 0;
429 virtual ld::Atom::Definition
definition() { return ld::Atom::definitionRegular
; }
430 virtual ld::Atom::Combine
combine(Parser
<A
>& parser
, pint_t addr
) = 0;
431 virtual bool ignoreLabel(const char* label
) const { return (label
[0] == 'L'); }
435 template <typename A
>
436 class FixedSizeSection
: public ImplicitSizeSection
<A
>
439 FixedSizeSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
440 : ImplicitSizeSection
<A
>(parser
, f
, s
) { }
442 typedef typename
A::P::uint_t pint_t
;
443 typedef typename
A::P P
;
444 typedef typename
A::P::E E
;
446 virtual bool useElementAt(Parser
<A
>& parser
,
447 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
, pint_t addr
)
452 template <typename A
>
453 class Literal4Section
: public FixedSizeSection
<A
>
456 Literal4Section(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
457 : FixedSizeSection
<A
>(parser
, f
, s
) {}
459 typedef typename
A::P::uint_t pint_t
;
460 typedef typename
A::P P
;
462 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
) { return ld::Atom::Alignment(2); }
463 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "4-byte-literal"; }
464 virtual pint_t
elementSizeAtAddress(pint_t addr
) { return 4; }
465 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
) { return ld::Atom::combineByNameAndContent
; }
466 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
467 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
468 const ld::IndirectBindingTable
& ind
) const;
469 virtual bool ignoreLabel(const char* label
) const;
472 template <typename A
>
473 class Literal8Section
: public FixedSizeSection
<A
>
476 Literal8Section(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
477 : FixedSizeSection
<A
>(parser
, f
, s
) {}
479 typedef typename
A::P::uint_t pint_t
;
480 typedef typename
A::P P
;
482 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
) { return ld::Atom::Alignment(3); }
483 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "8-byte-literal"; }
484 virtual pint_t
elementSizeAtAddress(pint_t addr
) { return 8; }
485 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
) { return ld::Atom::combineByNameAndContent
; }
486 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
487 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
488 const ld::IndirectBindingTable
& ind
) const;
489 virtual bool ignoreLabel(const char* label
) const;
492 template <typename A
>
493 class Literal16Section
: public FixedSizeSection
<A
>
496 Literal16Section(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
497 : FixedSizeSection
<A
>(parser
, f
, s
) {}
499 typedef typename
A::P::uint_t pint_t
;
500 typedef typename
A::P P
;
502 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
) { return ld::Atom::Alignment(4); }
503 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "16-byte-literal"; }
504 virtual pint_t
elementSizeAtAddress(pint_t addr
) { return 16; }
505 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
) { return ld::Atom::combineByNameAndContent
; }
506 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
507 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
508 const ld::IndirectBindingTable
& ind
) const;
509 virtual bool ignoreLabel(const char* label
) const;
513 template <typename A
>
514 class NonLazyPointerSection
: public FixedSizeSection
<A
>
517 NonLazyPointerSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
518 : FixedSizeSection
<A
>(parser
, f
, s
) {}
520 typedef typename
A::P::uint_t pint_t
;
521 typedef typename
A::P P
;
523 virtual void makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
&);
524 virtual ld::Atom::ContentType
contentType() { return ld::Atom::typeNonLazyPointer
; }
525 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
) { return ld::Atom::Alignment(log2(sizeof(pint_t
))); }
526 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "non_lazy_ptr"; }
527 virtual pint_t
elementSizeAtAddress(pint_t addr
) { return sizeof(pint_t
); }
528 virtual ld::Atom::Scope
scopeAtAddress(Parser
<A
>& parser
, pint_t addr
);
529 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
);
530 virtual bool ignoreLabel(const char* label
) const { return true; }
531 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
532 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
533 const ld::IndirectBindingTable
& ind
) const;
536 static const char* targetName(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
);
537 static ld::Fixup::Kind
fixupKind();
540 template <typename A
>
541 class TLVPointerSection
: public FixedSizeSection
<A
>
544 TLVPointerSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
545 : FixedSizeSection
<A
>(parser
, f
, s
) {}
547 typedef typename
A::P::uint_t pint_t
;
548 typedef typename
A::P P
;
550 virtual void makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
&);
551 virtual ld::Atom::ContentType
contentType() { return ld::Atom::typeTLVPointer
; }
552 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
) { return ld::Atom::Alignment(log2(sizeof(pint_t
))); }
553 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "tlv_lazy_ptr"; }
554 virtual pint_t
elementSizeAtAddress(pint_t addr
) { return sizeof(pint_t
); }
555 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
);
556 virtual bool ignoreLabel(const char* label
) const { return true; }
557 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
558 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
559 const ld::IndirectBindingTable
& ind
) const;
562 static const char* targetName(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
, bool* isStatic
);
566 template <typename A
>
567 class CFStringSection
: public FixedSizeSection
<A
>
570 CFStringSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
571 : FixedSizeSection
<A
>(parser
, f
, s
) {}
573 typedef typename
A::P::uint_t pint_t
;
575 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
) { return ld::Atom::Alignment(log2(sizeof(pint_t
))); }
576 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "CFString"; }
577 virtual pint_t
elementSizeAtAddress(pint_t addr
) { return 4*sizeof(pint_t
); }
578 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
) { return ld::Atom::combineByNameAndReferences
; }
579 virtual bool ignoreLabel(const char* label
) const { return true; }
580 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
581 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
582 const ld::IndirectBindingTable
& ind
) const;
584 enum ContentType
{ contentUTF8
, contentUTF16
, contentUnknown
};
585 static const uint8_t* targetContent(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
,
586 ContentType
* ct
, unsigned int* count
);
590 template <typename A
>
591 class ObjC1ClassSection
: public FixedSizeSection
<A
>
594 ObjC1ClassSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
595 : FixedSizeSection
<A
>(parser
, f
, s
) {}
597 typedef typename
A::P::uint_t pint_t
;
598 typedef typename
A::P P
;
599 typedef typename
A::P::E E
;
601 virtual ld::Atom::Scope
scopeAtAddress(Parser
<A
>& , pint_t
) { return ld::Atom::scopeGlobal
; }
602 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
) { return ld::Atom::Alignment(2); }
603 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
);
604 virtual ld::Atom::SymbolTableInclusion
symbolTableInclusion() { return ld::Atom::symbolTableIn
; }
605 virtual pint_t
elementSizeAtAddress(pint_t addr
);
606 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
) { return ld::Atom::combineNever
; }
607 virtual bool ignoreLabel(const char* label
) const { return true; }
608 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
610 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
611 const ld::IndirectBindingTable
& ind
) const { return false; }
612 virtual bool addRelocFixup(class Parser
<A
>& parser
, const macho_relocation_info
<P
>*);
616 template <typename A
>
617 class ObjC2ClassRefsSection
: public FixedSizeSection
<A
>
620 ObjC2ClassRefsSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
621 : FixedSizeSection
<A
>(parser
, f
, s
) {}
623 typedef typename
A::P::uint_t pint_t
;
625 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
) { return ld::Atom::Alignment(log2(sizeof(pint_t
))); }
626 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "objc-class-ref"; }
627 virtual pint_t
elementSizeAtAddress(pint_t addr
) { return sizeof(pint_t
); }
628 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
) { return ld::Atom::combineByNameAndReferences
; }
629 virtual bool ignoreLabel(const char* label
) const { return true; }
630 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
631 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
632 const ld::IndirectBindingTable
& ind
) const;
634 const char* targetClassName(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
638 template <typename A
>
639 class ObjC2CategoryListSection
: public FixedSizeSection
<A
>
642 ObjC2CategoryListSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
643 : FixedSizeSection
<A
>(parser
, f
, s
) {}
645 typedef typename
A::P::uint_t pint_t
;
647 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
) { return ld::Atom::Alignment(log2(sizeof(pint_t
))); }
648 virtual ld::Atom::Scope
scopeAtAddress(Parser
<A
>& parser
, pint_t addr
) { return ld::Atom::scopeTranslationUnit
; }
649 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "objc-cat-list"; }
650 virtual pint_t
elementSizeAtAddress(pint_t addr
) { return sizeof(pint_t
); }
651 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
) { return ld::Atom::combineNever
; }
652 virtual bool ignoreLabel(const char* label
) const { return true; }
654 const char* targetClassName(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
658 template <typename A
>
659 class PointerToCStringSection
: public FixedSizeSection
<A
>
662 PointerToCStringSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
663 : FixedSizeSection
<A
>(parser
, f
, s
) {}
665 typedef typename
A::P::uint_t pint_t
;
667 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
) { return ld::Atom::Alignment(log2(sizeof(pint_t
))); }
668 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "pointer-to-literal-cstring"; }
669 virtual pint_t
elementSizeAtAddress(pint_t addr
) { return sizeof(pint_t
); }
670 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
) { return ld::Atom::combineByNameAndReferences
; }
671 virtual bool ignoreLabel(const char* label
) const { return true; }
672 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
673 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
674 const ld::IndirectBindingTable
& ind
) const;
675 virtual const char* targetCString(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
679 template <typename A
>
680 class Objc1ClassReferences
: public PointerToCStringSection
<A
>
683 Objc1ClassReferences(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
684 : PointerToCStringSection
<A
>(parser
, f
, s
) {}
686 typedef typename
A::P::uint_t pint_t
;
687 typedef typename
A::P P
;
689 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "pointer-to-literal-objc-class-name"; }
690 virtual bool addRelocFixup(class Parser
<A
>& parser
, const macho_relocation_info
<P
>*);
691 virtual const char* targetCString(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
695 template <typename A
>
696 class CStringSection
: public ImplicitSizeSection
<A
>
699 CStringSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
700 : ImplicitSizeSection
<A
>(parser
, f
, s
) {}
702 typedef typename
A::P::uint_t pint_t
;
703 typedef typename
A::P P
;
705 virtual ld::Atom::ContentType
contentType() { return ld::Atom::typeCString
; }
706 virtual Atom
<A
>* findAtomByAddress(pint_t addr
);
707 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "cstring"; }
708 virtual pint_t
elementSizeAtAddress(pint_t addr
);
709 virtual bool ignoreLabel(const char* label
) const;
710 virtual bool useElementAt(Parser
<A
>& parser
,
711 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
, pint_t addr
);
712 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
) { return ld::Atom::combineByNameAndContent
; }
713 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
714 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
715 const ld::IndirectBindingTable
& ind
) const;
720 template <typename A
>
721 class UTF16StringSection
: public SymboledSection
<A
>
724 UTF16StringSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
725 : SymboledSection
<A
>(parser
, f
, s
) {}
727 typedef typename
A::P::uint_t pint_t
;
728 typedef typename
A::P P
;
730 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
) { return ld::Atom::combineByNameAndContent
; }
731 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
732 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
733 const ld::IndirectBindingTable
& ind
) const;
738 // Atoms in mach-o files
740 template <typename A
>
741 class Atom
: public ld::Atom
744 // overrides of ld::Atom
745 virtual const ld::File
* file() const;
746 virtual const char* translationUnitSource() const
747 { return sect().file().translationUnitSource(); }
748 virtual const char* name() const { return _name
; }
749 virtual uint64_t size() const { return _size
; }
750 virtual uint64_t objectAddress() const { return _objAddress
; }
751 virtual void copyRawContent(uint8_t buffer
[]) const;
752 virtual const uint8_t* rawContentPointer() const { return contentPointer(); }
753 virtual unsigned long contentHash(const ld::IndirectBindingTable
& ind
) const
754 { if ( _hash
== 0 ) _hash
= sect().contentHash(this, ind
); return _hash
; }
755 virtual bool canCoalesceWith(const ld::Atom
& rhs
, const ld::IndirectBindingTable
& ind
) const
756 { return sect().canCoalesceWith(this, rhs
, ind
); }
757 virtual ld::Fixup::iterator
fixupsBegin() const { return &machofile()._fixups
[_fixupsStartIndex
]; }
758 virtual ld::Fixup::iterator
fixupsEnd() const { return &machofile()._fixups
[_fixupsStartIndex
+_fixupsCount
]; }
759 virtual ld::Atom::UnwindInfo::iterator
beginUnwind() const { return &machofile()._unwindInfos
[_unwindInfoStartIndex
]; }
760 virtual ld::Atom::UnwindInfo::iterator
endUnwind() const { return &machofile()._unwindInfos
[_unwindInfoStartIndex
+_unwindInfoCount
]; }
761 virtual ld::Atom::LineInfo::iterator
beginLineInfo() const{ return &machofile()._lineInfos
[_lineInfoStartIndex
]; }
762 virtual ld::Atom::LineInfo::iterator
endLineInfo() const { return &machofile()._lineInfos
[_lineInfoStartIndex
+_lineInfoCount
]; }
763 virtual void setFile(const ld::File
* f
);
767 enum { kFixupStartIndexBits
= 32,
768 kLineInfoStartIndexBits
= 32,
769 kUnwindInfoStartIndexBits
= 24,
770 kFixupCountBits
= 24,
771 kLineInfoCountBits
= 12,
772 kUnwindInfoCountBits
= 4
773 }; // must sum to 128
776 // methods for all atoms from mach-o object file
777 Section
<A
>& sect() const { return (Section
<A
>&)section(); }
778 File
<A
>& machofile() const { return ((Section
<A
>*)(this->_section
))->file(); }
779 void setFixupsRange(uint32_t s
, uint32_t c
);
780 void setUnwindInfoRange(uint32_t s
, uint32_t c
);
781 void extendUnwindInfoRange();
782 void setLineInfoRange(uint32_t s
, uint32_t c
);
783 bool roomForMoreLineInfoCount() { return (_lineInfoCount
< ((1<<kLineInfoCountBits
)-1)); }
784 void incrementLineInfoCount() { assert(roomForMoreLineInfoCount()); ++_lineInfoCount
; }
785 void incrementFixupCount() { if (_fixupsCount
== ((1 << kFixupCountBits
)-1))
786 throwf("too may fixups in %s", name()); ++_fixupsCount
; }
787 const uint8_t* contentPointer() const;
788 uint32_t fixupCount() const { return _fixupsCount
; }
789 void verifyAlignment(const macho_section
<typename
A::P
>&) const;
791 typedef typename
A::P P
;
792 typedef typename
A::P::E E
;
793 typedef typename
A::P::uint_t pint_t
;
794 // constuct via all attributes
795 Atom(Section
<A
>& sct
, const char* nm
, pint_t addr
, uint64_t sz
,
796 ld::Atom::Definition d
, ld::Atom::Combine c
, ld::Atom::Scope s
,
797 ld::Atom::ContentType ct
, ld::Atom::SymbolTableInclusion i
,
798 bool dds
, bool thumb
, bool al
, ld::Atom::Alignment a
)
799 : ld::Atom((ld::Section
&)sct
, d
, c
, s
, ct
, i
, dds
, thumb
, al
, a
),
800 _size(sz
), _objAddress(addr
), _name(nm
), _hash(0),
801 _fixupsStartIndex(0), _lineInfoStartIndex(0),
802 _unwindInfoStartIndex(0), _fixupsCount(0),
803 _lineInfoCount(0), _unwindInfoCount(0) { }
804 // construct via symbol table entry
805 Atom(Section
<A
>& sct
, Parser
<A
>& parser
, const macho_nlist
<P
>& sym
,
806 uint64_t sz
, bool alias
=false)
807 : ld::Atom((ld::Section
&)sct
, parser
.definitionFromSymbol(sym
),
808 parser
.combineFromSymbol(sym
), parser
.scopeFromSymbol(sym
),
809 parser
.resolverFromSymbol(sym
) ? ld::Atom::typeResolver
: sct
.contentType(),
810 parser
.inclusionFromSymbol(sym
),
811 (parser
.dontDeadStripFromSymbol(sym
) && !sct
.dontDeadStripIfReferencesLive()) || sct
.dontDeadStrip(),
812 parser
.isThumbFromSymbol(sym
), alias
,
813 sct
.alignmentForAddress(sym
.n_value())),
814 _size(sz
), _objAddress(sym
.n_value()),
815 _name(parser
.nameFromSymbol(sym
)), _hash(0),
816 _fixupsStartIndex(0), _lineInfoStartIndex(0),
817 _unwindInfoStartIndex(0), _fixupsCount(0),
818 _lineInfoCount(0), _unwindInfoCount(0) {
819 // <rdar://problem/6783167> support auto-hidden weak symbols
820 if ( _scope
== ld::Atom::scopeGlobal
&&
821 (sym
.n_desc() & (N_WEAK_DEF
|N_WEAK_REF
)) == (N_WEAK_DEF
|N_WEAK_REF
) )
823 this->verifyAlignment(*sct
.machoSection());
824 if ( sct
.dontDeadStripIfReferencesLive() )
825 this->setDontDeadStripIfReferencesLive();
829 friend class Parser
<A
>;
830 friend class Section
<A
>;
831 friend class CStringSection
<A
>;
832 friend class AbsoluteSymbolSection
<A
>;
837 mutable unsigned long _hash
;
839 uint64_t _fixupsStartIndex
: kFixupStartIndexBits
,
840 _lineInfoStartIndex
: kLineInfoStartIndexBits
,
841 _unwindInfoStartIndex
: kUnwindInfoStartIndexBits
,
842 _fixupsCount
: kFixupCountBits
,
843 _lineInfoCount
: kLineInfoCountBits
,
844 _unwindInfoCount
: kUnwindInfoCountBits
;
846 static std::map
<const ld::Atom
*, const ld::File
*> _s_fileOverride
;
849 template <typename A
>
850 std::map
<const ld::Atom
*, const ld::File
*> Atom
<A
>::_s_fileOverride
;
852 template <typename A
>
853 void Atom
<A
>::setFile(const ld::File
* f
) {
854 _s_fileOverride
[this] = f
;
857 template <typename A
>
858 const ld::File
* Atom
<A
>::file() const
860 std::map
<const ld::Atom
*, const ld::File
*>::iterator pos
= _s_fileOverride
.find(this);
861 if ( pos
!= _s_fileOverride
.end() )
864 return §().file();
867 template <typename A
>
868 void Atom
<A
>::setFixupsRange(uint32_t startIndex
, uint32_t count
)
870 if ( count
>= (1 << kFixupCountBits
) )
871 throwf("too many fixups in function %s", this->name());
872 if ( startIndex
>= (1 << kFixupStartIndexBits
) )
873 throwf("too many fixups in file");
874 assert(((startIndex
+count
) <= sect().file()._fixups
.size()) && "fixup index out of range");
875 _fixupsStartIndex
= startIndex
;
876 _fixupsCount
= count
;
879 template <typename A
>
880 void Atom
<A
>::setUnwindInfoRange(uint32_t startIndex
, uint32_t count
)
882 if ( count
>= (1 << kUnwindInfoCountBits
) )
883 throwf("too many compact unwind infos in function %s", this->name());
884 if ( startIndex
>= (1 << kUnwindInfoStartIndexBits
) )
885 throwf("too many compact unwind infos (%d) in file", startIndex
);
886 assert((startIndex
+count
) <= sect().file()._unwindInfos
.size() && "unwindinfo index out of range");
887 _unwindInfoStartIndex
= startIndex
;
888 _unwindInfoCount
= count
;
891 template <typename A
>
892 void Atom
<A
>::extendUnwindInfoRange()
894 if ( _unwindInfoCount
+1 >= (1 << kUnwindInfoCountBits
) )
895 throwf("too many compact unwind infos in function %s", this->name());
896 _unwindInfoCount
+= 1;
899 template <typename A
>
900 void Atom
<A
>::setLineInfoRange(uint32_t startIndex
, uint32_t count
)
902 assert((count
< (1 << kLineInfoCountBits
)) && "too many line infos");
903 assert((startIndex
+count
) < sect().file()._lineInfos
.size() && "line info index out of range");
904 _lineInfoStartIndex
= startIndex
;
905 _lineInfoCount
= count
;
908 template <typename A
>
909 const uint8_t* Atom
<A
>::contentPointer() const
911 const macho_section
<P
>* sct
= this->sect().machoSection();
912 if ( this->_objAddress
> sct
->addr() + sct
->size() )
913 throwf("malformed .o file, symbol has address 0x%0llX which is outside range of its section", (uint64_t)this->_objAddress
);
914 uint32_t fileOffset
= sct
->offset() - sct
->addr() + this->_objAddress
;
915 return this->sect().file().fileContent()+fileOffset
;
919 template <typename A
>
920 void Atom
<A
>::copyRawContent(uint8_t buffer
[]) const
923 if ( this->contentType() == ld::Atom::typeZeroFill
) {
924 bzero(buffer
, _size
);
926 else if ( _size
!= 0 ) {
927 memcpy(buffer
, this->contentPointer(), _size
);
932 void Atom
<arm
>::verifyAlignment(const macho_section
<P
>&) const
934 if ( (this->section().type() == ld::Section::typeCode
) && ! isThumb() ) {
935 if ( ((_objAddress
% 4) != 0) || (this->alignment().powerOf2
< 2) )
936 warning("ARM function not 4-byte aligned: %s from %s", this->name(), this->file()->path());
940 #if SUPPORT_ARCH_arm64
942 void Atom
<arm64
>::verifyAlignment(const macho_section
<P
>& sect
) const
944 if ( (this->section().type() == ld::Section::typeCode
) && (sect
.size() != 0) ) {
945 if ( ((_objAddress
% 4) != 0) || (this->alignment().powerOf2
< 2) )
946 warning("arm64 function not 4-byte aligned: %s from %s", this->name(), this->file()->path());
952 template <typename A
>
953 void Atom
<A
>::verifyAlignment(const macho_section
<P
>&) const
958 class AliasAtom
: public ld::Atom
961 AliasAtom(const char* name
, bool hidden
, const ld::File
* file
, const char* aliasOfName
) :
962 ld::Atom(_s_section
, ld::Atom::definitionRegular
, ld::Atom::combineNever
,
963 (hidden
? ld::Atom::scopeLinkageUnit
: ld::Atom::scopeGlobal
),
964 ld::Atom::typeUnclassified
, ld::Atom::symbolTableIn
,
965 false, false, true, 0),
968 _fixup(0, ld::Fixup::k1of1
, ld::Fixup::kindNoneFollowOn
, ld::Fixup::bindingByNameUnbound
, aliasOfName
) { }
970 virtual const ld::File
* file() const { return _file
; }
971 virtual const char* translationUnitSource() const
973 virtual const char* name() const { return _name
; }
974 virtual uint64_t size() const { return 0; }
975 virtual uint64_t objectAddress() const { return 0; }
976 virtual void copyRawContent(uint8_t buffer
[]) const { }
977 virtual ld::Fixup::iterator
fixupsBegin() const { return &((ld::Fixup
*)&_fixup
)[0]; }
978 virtual ld::Fixup::iterator
fixupsEnd() const { return &((ld::Fixup
*)&_fixup
)[1]; }
981 static ld::Section _s_section
;
983 const ld::File
* _file
;
988 ld::Section
AliasAtom::_s_section("__LD", "__aliases", ld::Section::typeTempAlias
, true);
991 template <typename A
>
995 static bool validFile(const uint8_t* fileContent
, bool subtypeMustMatch
=false,
996 cpu_subtype_t subtype
=0);
997 static const char* fileKind(const uint8_t* fileContent
);
998 static Options::Platform
findPlatform(const macho_header
<typename
A::P
>* header
);
999 static bool hasObjC2Categories(const uint8_t* fileContent
);
1000 static bool hasObjC1Categories(const uint8_t* fileContent
);
1001 static bool getNonLocalSymbols(const uint8_t* fileContnet
, std::vector
<const char*> &syms
);
1002 static ld::relocatable::File
* parse(const uint8_t* fileContent
, uint64_t fileLength
,
1003 const char* path
, time_t modTime
, ld::File::Ordinal ordinal
,
1004 const ParserOptions
& opts
) {
1005 Parser
p(fileContent
, fileLength
, path
, modTime
,
1006 ordinal
, opts
.warnUnwindConversionProblems
,
1007 opts
.keepDwarfUnwind
, opts
.forceDwarfConversion
,
1008 opts
.neverConvertDwarf
, opts
.verboseOptimizationHints
,
1009 opts
.ignoreMismatchPlatform
);
1010 return p
.parse(opts
);
1013 typedef typename
A::P P
;
1014 typedef typename
A::P::E E
;
1015 typedef typename
A::P::uint_t pint_t
;
1017 struct SourceLocation
{
1019 SourceLocation(Atom
<A
>* a
, uint32_t o
) : atom(a
), offsetInAtom(o
) {}
1021 uint32_t offsetInAtom
;
1026 const char* name
; // only used if targetAtom is NULL
1028 bool weakImport
; // only used if targetAtom is NULL
1031 struct FixupInAtom
{
1032 FixupInAtom(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, Atom
<A
>* target
) :
1033 fixup(src
.offsetInAtom
, c
, k
, target
), atom(src
.atom
) { src
.atom
->incrementFixupCount(); }
1035 FixupInAtom(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, ld::Fixup::TargetBinding b
, Atom
<A
>* target
) :
1036 fixup(src
.offsetInAtom
, c
, k
, b
, target
), atom(src
.atom
) { src
.atom
->incrementFixupCount(); }
1038 FixupInAtom(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, bool wi
, const char* name
) :
1039 fixup(src
.offsetInAtom
, c
, k
, wi
, name
), atom(src
.atom
) { src
.atom
->incrementFixupCount(); }
1041 FixupInAtom(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, ld::Fixup::TargetBinding b
, const char* name
) :
1042 fixup(src
.offsetInAtom
, c
, k
, b
, name
), atom(src
.atom
) { src
.atom
->incrementFixupCount(); }
1044 FixupInAtom(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, uint64_t addend
) :
1045 fixup(src
.offsetInAtom
, c
, k
, addend
), atom(src
.atom
) { src
.atom
->incrementFixupCount(); }
1047 FixupInAtom(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
) :
1048 fixup(src
.offsetInAtom
, c
, k
, (uint64_t)0), atom(src
.atom
) { src
.atom
->incrementFixupCount(); }
1054 void addFixup(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, Atom
<A
>* target
) {
1055 _allFixups
.push_back(FixupInAtom(src
, c
, k
, target
));
1058 void addFixup(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, ld::Fixup::TargetBinding b
, Atom
<A
>* target
) {
1059 _allFixups
.push_back(FixupInAtom(src
, c
, k
, b
, target
));
1062 void addFixup(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, bool wi
, const char* name
) {
1063 _allFixups
.push_back(FixupInAtom(src
, c
, k
, wi
, name
));
1066 void addFixup(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, ld::Fixup::TargetBinding b
, const char* name
) {
1067 _allFixups
.push_back(FixupInAtom(src
, c
, k
, b
, name
));
1070 void addFixup(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, uint64_t addend
) {
1071 _allFixups
.push_back(FixupInAtom(src
, c
, k
, addend
));
1074 void addFixup(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
) {
1075 _allFixups
.push_back(FixupInAtom(src
, c
, k
));
1078 const char* path() { return _path
; }
1079 uint32_t symbolCount() { return _symbolCount
; }
1080 uint32_t indirectSymbol(uint32_t indirectIndex
);
1081 const macho_nlist
<P
>& symbolFromIndex(uint32_t index
);
1082 const char* nameFromSymbol(const macho_nlist
<P
>& sym
);
1083 ld::Atom::Scope
scopeFromSymbol(const macho_nlist
<P
>& sym
);
1084 static ld::Atom::Definition
definitionFromSymbol(const macho_nlist
<P
>& sym
);
1085 static ld::Atom::Combine
combineFromSymbol(const macho_nlist
<P
>& sym
);
1086 ld::Atom::SymbolTableInclusion
inclusionFromSymbol(const macho_nlist
<P
>& sym
);
1087 static bool dontDeadStripFromSymbol(const macho_nlist
<P
>& sym
);
1088 static bool isThumbFromSymbol(const macho_nlist
<P
>& sym
);
1089 static bool weakImportFromSymbol(const macho_nlist
<P
>& sym
);
1090 static bool resolverFromSymbol(const macho_nlist
<P
>& sym
);
1091 static bool altEntryFromSymbol(const macho_nlist
<P
>& sym
);
1092 uint32_t symbolIndexFromIndirectSectionAddress(pint_t
,const macho_section
<P
>*);
1093 const macho_section
<P
>* firstMachOSection() { return _sectionsStart
; }
1094 const macho_section
<P
>* machOSectionFromSectionIndex(uint32_t index
);
1095 uint32_t machOSectionCount() { return _machOSectionsCount
; }
1096 uint32_t undefinedStartIndex() { return _undefinedStartIndex
; }
1097 uint32_t undefinedEndIndex() { return _undefinedEndIndex
; }
1098 void addFixup(FixupInAtom f
) { _allFixups
.push_back(f
); }
1099 Section
<A
>* sectionForNum(unsigned int sectNum
);
1100 Section
<A
>* sectionForAddress(pint_t addr
);
1101 Atom
<A
>* findAtomByAddress(pint_t addr
);
1102 Atom
<A
>* findAtomByAddressOrNullIfStub(pint_t addr
);
1103 Atom
<A
>* findAtomByAddressOrLocalTargetOfStub(pint_t addr
, uint32_t* offsetInAtom
);
1104 Atom
<A
>* findAtomByName(const char* name
); // slow!
1105 void findTargetFromAddress(pint_t addr
, TargetDesc
& target
);
1106 void findTargetFromAddress(pint_t baseAddr
, pint_t addr
, TargetDesc
& target
);
1107 void findTargetFromAddressAndSectionNum(pint_t addr
, unsigned int sectNum
,
1108 TargetDesc
& target
);
1109 uint32_t tentativeDefinitionCount() { return _tentativeDefinitionCount
; }
1110 uint32_t absoluteSymbolCount() { return _absoluteSymbolCount
; }
1112 uint32_t fileLength() const { return _fileLength
; }
1113 bool hasStubsSection() { return (_stubsSectionNum
!= 0); }
1114 unsigned int stubsSectionNum() { return _stubsSectionNum
; }
1115 void addDtraceExtraInfos(const SourceLocation
& src
, const char* provider
);
1116 const char* scanSymbolTableForAddress(uint64_t addr
);
1117 bool warnUnwindConversionProblems() { return _warnUnwindConversionProblems
; }
1118 bool hasDataInCodeLabels() { return _hasDataInCodeLabels
; }
1119 bool keepDwarfUnwind() { return _keepDwarfUnwind
; }
1120 bool forceDwarfConversion() { return _forceDwarfConversion
; }
1121 bool verboseOptimizationHints() { return _verboseOptimizationHints
; }
1122 bool neverConvertDwarf() { return _neverConvertDwarf
; }
1123 bool armUsesZeroCostExceptions() { return _armUsesZeroCostExceptions
; }
1124 uint8_t maxDefaultCommonAlignment() { return _maxDefaultCommonAlignment
; }
1127 macho_data_in_code_entry
<P
>* dataInCodeStart() { return _dataInCodeStart
; }
1128 macho_data_in_code_entry
<P
>* dataInCodeEnd() { return _dataInCodeEnd
; }
1129 const uint8_t* optimizationHintsStart() { return _lohStart
; }
1130 const uint8_t* optimizationHintsEnd() { return _lohEnd
; }
1131 bool hasOptimizationHints() { return _lohStart
!= _lohEnd
; }
1134 void addFixups(const SourceLocation
& src
, ld::Fixup::Kind kind
, const TargetDesc
& target
);
1135 void addFixups(const SourceLocation
& src
, ld::Fixup::Kind kind
, const TargetDesc
& target
, const TargetDesc
& picBase
);
1139 struct LabelAndCFIBreakIterator
{
1140 typedef typename CFISection
<A
>::CFI_Atom_Info CFI_Atom_Info
;
1141 LabelAndCFIBreakIterator(const uint32_t* ssa
, uint32_t ssc
, const pint_t
* cfisa
,
1142 uint32_t cfisc
, bool ols
)
1143 : sortedSymbolIndexes(ssa
), sortedSymbolCount(ssc
), cfiStartsArray(cfisa
),
1144 cfiStartsCount(cfisc
), fileHasOverlappingSymbols(ols
),
1145 newSection(false), cfiIndex(0), symIndex(0) {}
1146 bool next(Parser
<A
>& parser
, const Section
<A
>& sect
, uint32_t sectNum
, pint_t startAddr
, pint_t endAddr
,
1147 pint_t
* addr
, pint_t
* size
, const macho_nlist
<P
>** sym
);
1148 pint_t
peek(Parser
<A
>& parser
, pint_t startAddr
, pint_t endAddr
);
1149 void beginSection() { newSection
= true; symIndex
= 0; }
1151 const uint32_t* const sortedSymbolIndexes
;
1152 const uint32_t sortedSymbolCount
;
1153 const pint_t
* cfiStartsArray
;
1154 const uint32_t cfiStartsCount
;
1155 const bool fileHasOverlappingSymbols
;
1161 struct CFI_CU_InfoArrays
{
1162 typedef typename CFISection
<A
>::CFI_Atom_Info CFI_Atom_Info
;
1163 typedef typename CUSection
<A
>::Info CU_Info
;
1164 CFI_CU_InfoArrays(const CFI_Atom_Info
* cfiAr
, uint32_t cfiC
, CU_Info
* cuAr
, uint32_t cuC
)
1165 : cfiArray(cfiAr
), cuArray(cuAr
), cfiCount(cfiC
), cuCount(cuC
) {}
1166 const CFI_Atom_Info
* const cfiArray
;
1167 CU_Info
* const cuArray
;
1168 const uint32_t cfiCount
;
1169 const uint32_t cuCount
;
1175 friend class Section
<A
>;
1177 enum SectionType
{ sectionTypeIgnore
, sectionTypeLiteral4
, sectionTypeLiteral8
, sectionTypeLiteral16
,
1178 sectionTypeNonLazy
, sectionTypeCFI
, sectionTypeCString
, sectionTypeCStringPointer
,
1179 sectionTypeUTF16Strings
, sectionTypeCFString
, sectionTypeObjC2ClassRefs
, typeObjC2CategoryList
,
1180 sectionTypeObjC1Classes
, sectionTypeSymboled
, sectionTypeObjC1ClassRefs
,
1181 sectionTypeTentativeDefinitions
, sectionTypeAbsoluteSymbols
, sectionTypeTLVDefs
,
1182 sectionTypeCompactUnwind
, sectionTypeTLVPointers
};
1184 template <typename P
>
1185 struct MachOSectionAndSectionClass
1187 const macho_section
<P
>* sect
;
1190 static int sorter(const void* l
, const void* r
) {
1191 const MachOSectionAndSectionClass
<P
>* left
= (MachOSectionAndSectionClass
<P
>*)l
;
1192 const MachOSectionAndSectionClass
<P
>* right
= (MachOSectionAndSectionClass
<P
>*)r
;
1193 int64_t diff
= left
->sect
->addr() - right
->sect
->addr();
1203 struct ParserAndSectionsArray
{ Parser
* parser
; const uint32_t* sortedSectionsArray
; };
1206 Parser(const uint8_t* fileContent
, uint64_t fileLength
,
1207 const char* path
, time_t modTime
, ld::File::Ordinal ordinal
,
1208 bool warnUnwindConversionProblems
, bool keepDwarfUnwind
,
1209 bool forceDwarfConversion
, bool neverConvertDwarf
,
1210 bool verboseOptimizationHints
, bool ignoreMismatchPlatform
);
1211 ld::relocatable::File
* parse(const ParserOptions
& opts
);
1212 static uint8_t loadCommandSizeMask();
1213 bool parseLoadCommands(Options::Platform platform
, uint32_t minOSVersion
, bool simulator
, bool ignoreMismatchPlatform
);
1214 void makeSections();
1215 void prescanSymbolTable();
1216 void makeSortedSymbolsArray(uint32_t symArray
[], const uint32_t sectionArray
[]);
1217 void makeSortedSectionsArray(uint32_t array
[]);
1218 static int pointerSorter(const void* l
, const void* r
);
1219 static int symbolIndexSorter(void* extra
, const void* l
, const void* r
);
1220 static int sectionIndexSorter(void* extra
, const void* l
, const void* r
);
1222 void parseDebugInfo();
1225 void appendAliasAtoms(uint8_t* atomBuffer
);
1226 static bool isConstFunStabs(const char *stabStr
);
1227 bool read_comp_unit(const char ** name
, const char ** comp_dir
,
1228 uint64_t *stmt_list
);
1229 pint_t
realAddr(pint_t addr
);
1230 const char* getDwarfString(uint64_t form
, const uint8_t*& p
);
1231 uint64_t getDwarfOffset(uint64_t form
, const uint8_t*& di
, bool dwarf64
);
1232 bool skip_form(const uint8_t ** offset
, const uint8_t * end
,
1233 uint64_t form
, uint8_t addr_size
, bool dwarf64
);
1236 // filled in by constructor
1237 const uint8_t* _fileContent
;
1238 uint32_t _fileLength
;
1241 ld::File::Ordinal _ordinal
;
1243 // filled in by parseLoadCommands()
1245 const macho_nlist
<P
>* _symbols
;
1246 uint32_t _symbolCount
;
1247 uint32_t _indirectSymbolCount
;
1248 const char* _strings
;
1249 uint32_t _stringsSize
;
1250 const uint32_t* _indirectTable
;
1251 uint32_t _indirectTableCount
;
1252 uint32_t _undefinedStartIndex
;
1253 uint32_t _undefinedEndIndex
;
1254 const macho_section
<P
>* _sectionsStart
;
1255 uint32_t _machOSectionsCount
;
1257 macho_data_in_code_entry
<P
>* _dataInCodeStart
;
1258 macho_data_in_code_entry
<P
>* _dataInCodeEnd
;
1259 const uint8_t* _lohStart
;
1260 const uint8_t* _lohEnd
;
1262 // filled in by parse()
1263 CFISection
<A
>* _EHFrameSection
;
1264 CUSection
<A
>* _compactUnwindSection
;
1265 AbsoluteSymbolSection
<A
>* _absoluteSection
;
1266 uint32_t _tentativeDefinitionCount
;
1267 uint32_t _absoluteSymbolCount
;
1268 uint32_t _symbolsInSections
;
1269 bool _hasLongBranchStubs
;
1270 bool _AppleObjc
; // FSF has objc that uses different data layout
1271 bool _overlappingSymbols
;
1272 bool _warnUnwindConversionProblems
;
1273 bool _hasDataInCodeLabels
;
1274 bool _keepDwarfUnwind
;
1275 bool _forceDwarfConversion
;
1276 bool _neverConvertDwarf
;
1277 bool _verboseOptimizationHints
;
1278 bool _armUsesZeroCostExceptions
;
1279 bool _ignoreMismatchPlatform
;
1280 bool _treateBitcodeAsData
;
1282 uint8_t _maxDefaultCommonAlignment
;
1283 unsigned int _stubsSectionNum
;
1284 const macho_section
<P
>* _stubsMachOSection
;
1285 std::vector
<const char*> _dtraceProviderInfo
;
1286 std::vector
<FixupInAtom
> _allFixups
;
1291 template <typename A
>
1292 Parser
<A
>::Parser(const uint8_t* fileContent
, uint64_t fileLength
, const char* path
, time_t modTime
,
1293 ld::File::Ordinal ordinal
, bool convertDUI
, bool keepDwarfUnwind
, bool forceDwarfConversion
,
1294 bool neverConvertDwarf
, bool verboseOptimizationHints
, bool ignoreMismatchPlatform
)
1295 : _fileContent(fileContent
), _fileLength(fileLength
), _path(path
), _modTime(modTime
),
1296 _ordinal(ordinal
), _file(NULL
),
1297 _symbols(NULL
), _symbolCount(0), _indirectSymbolCount(0), _strings(NULL
), _stringsSize(0),
1298 _indirectTable(NULL
), _indirectTableCount(0),
1299 _undefinedStartIndex(0), _undefinedEndIndex(0),
1300 _sectionsStart(NULL
), _machOSectionsCount(0), _hasUUID(false),
1301 _dataInCodeStart(NULL
), _dataInCodeEnd(NULL
),
1302 _lohStart(NULL
), _lohEnd(NULL
),
1303 _EHFrameSection(NULL
), _compactUnwindSection(NULL
), _absoluteSection(NULL
),
1304 _tentativeDefinitionCount(0), _absoluteSymbolCount(0),
1305 _symbolsInSections(0), _hasLongBranchStubs(false), _AppleObjc(false),
1306 _overlappingSymbols(false), _warnUnwindConversionProblems(convertDUI
), _hasDataInCodeLabels(false),
1307 _keepDwarfUnwind(keepDwarfUnwind
), _forceDwarfConversion(forceDwarfConversion
),
1308 _neverConvertDwarf(neverConvertDwarf
),
1309 _verboseOptimizationHints(verboseOptimizationHints
),
1310 _ignoreMismatchPlatform(ignoreMismatchPlatform
),
1311 _stubsSectionNum(0), _stubsMachOSection(NULL
)
1317 bool Parser
<x86
>::validFile(const uint8_t* fileContent
, bool, cpu_subtype_t
)
1319 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1320 if ( header
->magic() != MH_MAGIC
)
1322 if ( header
->cputype() != CPU_TYPE_I386
)
1324 if ( header
->filetype() != MH_OBJECT
)
1330 bool Parser
<x86_64
>::validFile(const uint8_t* fileContent
, bool, cpu_subtype_t
)
1332 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1333 if ( header
->magic() != MH_MAGIC_64
)
1335 if ( header
->cputype() != CPU_TYPE_X86_64
)
1337 if ( header
->filetype() != MH_OBJECT
)
1343 bool Parser
<arm
>::validFile(const uint8_t* fileContent
, bool subtypeMustMatch
, cpu_subtype_t subtype
)
1345 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1346 if ( header
->magic() != MH_MAGIC
)
1348 if ( header
->cputype() != CPU_TYPE_ARM
)
1350 if ( header
->filetype() != MH_OBJECT
)
1352 if ( subtypeMustMatch
) {
1353 if ( (cpu_subtype_t
)header
->cpusubtype() == subtype
)
1355 // hack until libcc_kext.a is made fat
1356 if ( header
->cpusubtype() == CPU_SUBTYPE_ARM_ALL
)
1365 bool Parser
<arm64
>::validFile(const uint8_t* fileContent
, bool subtypeMustMatch
, cpu_subtype_t subtype
)
1367 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1368 if ( header
->magic() != MH_MAGIC_64
)
1370 if ( header
->cputype() != CPU_TYPE_ARM64
)
1372 if ( header
->filetype() != MH_OBJECT
)
1379 const char* Parser
<x86
>::fileKind(const uint8_t* fileContent
)
1381 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1382 if ( header
->magic() != MH_MAGIC
)
1384 if ( header
->cputype() != CPU_TYPE_I386
)
1390 const char* Parser
<x86_64
>::fileKind(const uint8_t* fileContent
)
1392 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1393 if ( header
->magic() != MH_MAGIC_64
)
1395 if ( header
->cputype() != CPU_TYPE_X86_64
)
1401 const char* Parser
<arm
>::fileKind(const uint8_t* fileContent
)
1403 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1404 if ( header
->magic() != MH_MAGIC
)
1406 if ( header
->cputype() != CPU_TYPE_ARM
)
1408 for (const ArchInfo
* t
=archInfoArray
; t
->archName
!= NULL
; ++t
) {
1409 if ( (t
->cpuType
== CPU_TYPE_ARM
) && ((cpu_subtype_t
)header
->cpusubtype() == t
->cpuSubType
) ) {
1416 #if SUPPORT_ARCH_arm64
1418 const char* Parser
<arm64
>::fileKind(const uint8_t* fileContent
)
1420 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1421 if ( header
->magic() != MH_MAGIC_64
)
1423 if ( header
->cputype() != CPU_TYPE_ARM64
)
1430 template <typename A
>
1431 bool Parser
<A
>::hasObjC2Categories(const uint8_t* fileContent
)
1433 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1434 const uint32_t cmd_count
= header
->ncmds();
1435 const macho_load_command
<P
>* const cmds
= (macho_load_command
<P
>*)((char*)header
+ sizeof(macho_header
<P
>));
1436 const macho_load_command
<P
>* const cmdsEnd
= (macho_load_command
<P
>*)((char*)header
+ sizeof(macho_header
<P
>) + header
->sizeofcmds());
1437 const macho_load_command
<P
>* cmd
= cmds
;
1438 for (uint32_t i
= 0; i
< cmd_count
; ++i
) {
1439 if ( cmd
->cmd() == macho_segment_command
<P
>::CMD
) {
1440 const macho_segment_command
<P
>* segment
= (macho_segment_command
<P
>*)cmd
;
1441 const macho_section
<P
>* sectionsStart
= (macho_section
<P
>*)((char*)segment
+ sizeof(macho_segment_command
<P
>));
1442 for (uint32_t si
=0; si
< segment
->nsects(); ++si
) {
1443 const macho_section
<P
>* sect
= §ionsStart
[si
];
1444 if ( (sect
->size() > 0)
1445 && (strcmp(sect
->sectname(), "__objc_catlist") == 0)
1446 && (strcmp(sect
->segname(), "__DATA") == 0) ) {
1451 cmd
= (const macho_load_command
<P
>*)(((char*)cmd
)+cmd
->cmdsize());
1452 if ( cmd
> cmdsEnd
)
1453 throwf("malformed mach-o file, load command #%d is outside size of load commands", i
);
1459 template <typename A
>
1460 bool Parser
<A
>::hasObjC1Categories(const uint8_t* fileContent
)
1462 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1463 const uint32_t cmd_count
= header
->ncmds();
1464 const macho_load_command
<P
>* const cmds
= (macho_load_command
<P
>*)((char*)header
+ sizeof(macho_header
<P
>));
1465 const macho_load_command
<P
>* const cmdsEnd
= (macho_load_command
<P
>*)((char*)header
+ sizeof(macho_header
<P
>) + header
->sizeofcmds());
1466 const macho_load_command
<P
>* cmd
= cmds
;
1467 for (uint32_t i
= 0; i
< cmd_count
; ++i
) {
1468 if ( cmd
->cmd() == macho_segment_command
<P
>::CMD
) {
1469 const macho_segment_command
<P
>* segment
= (macho_segment_command
<P
>*)cmd
;
1470 const macho_section
<P
>* sectionsStart
= (macho_section
<P
>*)((char*)segment
+ sizeof(macho_segment_command
<P
>));
1471 for (uint32_t si
=0; si
< segment
->nsects(); ++si
) {
1472 const macho_section
<P
>* sect
= §ionsStart
[si
];
1473 if ( (sect
->size() > 0)
1474 && (strcmp(sect
->sectname(), "__category") == 0)
1475 && (strcmp(sect
->segname(), "__OBJC") == 0) ) {
1480 cmd
= (const macho_load_command
<P
>*)(((char*)cmd
)+cmd
->cmdsize());
1481 if ( cmd
> cmdsEnd
)
1482 throwf("malformed mach-o file, load command #%d is outside size of load commands", i
);
1488 template <typename A
>
1489 bool Parser
<A
>::getNonLocalSymbols(const uint8_t* fileContent
, std::vector
<const char*> &syms
)
1491 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1492 const uint32_t cmd_count
= header
->ncmds();
1493 const macho_load_command
<P
>* const cmds
= (macho_load_command
<P
>*)((char*)header
+ sizeof(macho_header
<P
>));
1494 const macho_load_command
<P
>* const cmdsEnd
= (macho_load_command
<P
>*)((char*)header
+ sizeof(macho_header
<P
>) + header
->sizeofcmds());
1495 const macho_load_command
<P
>* cmd
= cmds
;
1496 for (uint32_t i
= 0; i
< cmd_count
; ++i
) {
1497 if ( cmd
->cmd() == LC_SYMTAB
) {
1498 const macho_symtab_command
<P
>* symtab
= (macho_symtab_command
<P
>*)cmd
;
1499 uint32_t symbolCount
= symtab
->nsyms();
1500 const macho_nlist
<P
>* symbols
= (const macho_nlist
<P
>*)(fileContent
+ symtab
->symoff());
1501 const char* strings
= (char*)fileContent
+ symtab
->stroff();
1502 for (uint32_t j
= 0; j
< symbolCount
; ++j
) {
1503 // ignore stabs and count only ext symbols
1504 if ( (symbols
[j
].n_type() & N_STAB
) == 0 &&
1505 (symbols
[j
].n_type() & N_EXT
) != 0 ) {
1506 const char* symName
= &strings
[symbols
[j
].n_strx()];
1507 syms
.push_back(symName
);
1512 cmd
= (const macho_load_command
<P
>*)(((char*)cmd
)+cmd
->cmdsize());
1513 if ( cmd
> cmdsEnd
)
1514 throwf("malformed mach-o file, load command #%d is outside size of load commands", i
);
1520 template <typename A
>
1521 int Parser
<A
>::pointerSorter(const void* l
, const void* r
)
1523 // sort references by address
1524 const pint_t
* left
= (pint_t
*)l
;
1525 const pint_t
* right
= (pint_t
*)r
;
1526 return (*left
- *right
);
1529 template <typename A
>
1530 typename
A::P::uint_t Parser
<A
>::LabelAndCFIBreakIterator::peek(Parser
<A
>& parser
, pint_t startAddr
, pint_t endAddr
)
1533 if ( symIndex
< sortedSymbolCount
)
1534 symbolAddr
= parser
.symbolFromIndex(sortedSymbolIndexes
[symIndex
]).n_value();
1536 symbolAddr
= endAddr
;
1538 if ( cfiIndex
< cfiStartsCount
)
1539 cfiAddr
= cfiStartsArray
[cfiIndex
];
1542 if ( (cfiAddr
< symbolAddr
) && (cfiAddr
>= startAddr
) ) {
1543 if ( cfiAddr
< endAddr
)
1549 if ( symbolAddr
< endAddr
)
1557 // Parses up a section into chunks based on labels and CFI information.
1558 // Each call returns the next chunk address and size, and (if the break
1559 // was becuase of a label, the symbol). Returns false when no more chunks.
1561 template <typename A
>
1562 bool Parser
<A
>::LabelAndCFIBreakIterator::next(Parser
<A
>& parser
, const Section
<A
>& sect
, uint32_t sectNum
, pint_t startAddr
, pint_t endAddr
,
1563 pint_t
* addr
, pint_t
* size
, const macho_nlist
<P
>** symbol
)
1565 // may not be a label on start of section, but need atom demarcation there
1568 // advance symIndex until we get to the first label at or past the start of this section
1569 while ( symIndex
< sortedSymbolCount
) {
1570 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(sortedSymbolIndexes
[symIndex
]);
1571 if ( ! sect
.ignoreLabel(parser
.nameFromSymbol(sym
)) ) {
1572 pint_t nextSymbolAddr
= sym
.n_value();
1573 //fprintf(stderr, "sectNum=%d, nextSymbolAddr=0x%08llX, name=%s\n", sectNum, (uint64_t)nextSymbolAddr, parser.nameFromSymbol(sym));
1574 if ( (nextSymbolAddr
> startAddr
) || ((nextSymbolAddr
== startAddr
) && (sym
.n_sect() == sectNum
)) )
1579 if ( symIndex
< sortedSymbolCount
) {
1580 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(sortedSymbolIndexes
[symIndex
]);
1581 pint_t nextSymbolAddr
= sym
.n_value();
1582 // if next symbol found is not in this section
1583 if ( sym
.n_sect() != sectNum
) {
1584 // check for CFI break instead of symbol break
1585 if ( cfiIndex
< cfiStartsCount
) {
1586 pint_t nextCfiAddr
= cfiStartsArray
[cfiIndex
];
1587 if ( nextCfiAddr
< endAddr
) {
1590 *addr
= nextCfiAddr
;
1591 *size
= peek(parser
, startAddr
, endAddr
) - nextCfiAddr
;
1597 *size
= endAddr
- startAddr
;
1599 if ( startAddr
== endAddr
)
1600 return false; // zero size section
1602 return true; // whole section is one atom with no label
1604 // if also CFI break here, eat it
1605 if ( cfiIndex
< cfiStartsCount
) {
1606 if ( cfiStartsArray
[cfiIndex
] == nextSymbolAddr
)
1609 if ( nextSymbolAddr
== startAddr
) {
1610 // label at start of section, return it as chunk
1613 *size
= peek(parser
, startAddr
, endAddr
) - startAddr
;
1617 // return chunk before first symbol
1619 *size
= nextSymbolAddr
- startAddr
;
1623 // no symbols in section, check CFI
1624 if ( cfiIndex
< cfiStartsCount
) {
1625 pint_t nextCfiAddr
= cfiStartsArray
[cfiIndex
];
1626 if ( nextCfiAddr
< endAddr
) {
1629 *addr
= nextCfiAddr
;
1630 *size
= peek(parser
, startAddr
, endAddr
) - nextCfiAddr
;
1635 // no cfi, so whole section is one chunk
1637 *size
= endAddr
- startAddr
;
1639 if ( startAddr
== endAddr
)
1640 return false; // zero size section
1642 return true; // whole section is one atom with no label
1645 while ( (symIndex
< sortedSymbolCount
) && (cfiIndex
< cfiStartsCount
) ) {
1646 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(sortedSymbolIndexes
[symIndex
]);
1647 pint_t nextSymbolAddr
= sym
.n_value();
1648 pint_t nextCfiAddr
= cfiStartsArray
[cfiIndex
];
1649 if ( nextSymbolAddr
< nextCfiAddr
) {
1650 if ( nextSymbolAddr
>= endAddr
)
1653 if ( nextSymbolAddr
< startAddr
)
1655 *addr
= nextSymbolAddr
;
1656 *size
= peek(parser
, startAddr
, endAddr
) - nextSymbolAddr
;
1660 else if ( nextCfiAddr
< nextSymbolAddr
) {
1661 if ( nextCfiAddr
>= endAddr
)
1664 if ( nextCfiAddr
< startAddr
)
1666 *addr
= nextCfiAddr
;
1667 *size
= peek(parser
, startAddr
, endAddr
) - nextCfiAddr
;
1672 if ( nextCfiAddr
>= endAddr
)
1676 if ( nextCfiAddr
< startAddr
)
1678 *addr
= nextCfiAddr
;
1679 *size
= peek(parser
, startAddr
, endAddr
) - nextCfiAddr
;
1684 while ( symIndex
< sortedSymbolCount
) {
1685 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(sortedSymbolIndexes
[symIndex
]);
1686 pint_t nextSymbolAddr
= sym
.n_value();
1687 // if next symbol found is not in this section, then done with iteration
1688 if ( sym
.n_sect() != sectNum
)
1691 if ( nextSymbolAddr
< startAddr
)
1693 *addr
= nextSymbolAddr
;
1694 *size
= peek(parser
, startAddr
, endAddr
) - nextSymbolAddr
;
1698 while ( cfiIndex
< cfiStartsCount
) {
1699 pint_t nextCfiAddr
= cfiStartsArray
[cfiIndex
];
1700 if ( nextCfiAddr
>= endAddr
)
1703 if ( nextCfiAddr
< startAddr
)
1705 *addr
= nextCfiAddr
;
1706 *size
= peek(parser
, startAddr
, endAddr
) - nextCfiAddr
;
1714 typename
arm::P::uint_t Parser
<arm
>::realAddr(typename
arm::P::uint_t addr
)
1719 template <typename A
>
1720 typename
A::P::uint_t Parser
<A
>::realAddr(typename
A::P::uint_t addr
)
1725 #define STACK_ALLOC_IF_SMALL(_type, _name, _actual_count, _maxCount) \
1726 _type* _name = NULL; \
1727 uint32_t _name##_count = 1; \
1728 uint32_t _name##_stack_count = _actual_count; \
1729 if ( _actual_count > _maxCount ) { \
1730 _name = (_type*)malloc(sizeof(_type) * _actual_count); \
1731 _name##_stack_count = 1; \
1734 _name##_count = _actual_count; \
1735 _type _name##_buffer[_name##_stack_count]; \
1736 if ( _name == NULL ) \
1737 _name = _name##_buffer;
1740 template <typename A
>
1741 ld::relocatable::File
* Parser
<A
>::parse(const ParserOptions
& opts
)
1743 // create file object
1744 _file
= new File
<A
>(_path
, _modTime
, _fileContent
, _ordinal
);
1747 _file
->_srcKind
= opts
.srcKind
;
1748 // set treatBitcodeAsData
1749 _treateBitcodeAsData
= opts
.treateBitcodeAsData
;
1750 _usingBitcode
= opts
.usingBitcode
;
1752 // respond to -t option
1753 if ( opts
.logAllFiles
)
1754 printf("%s\n", _path
);
1756 _armUsesZeroCostExceptions
= opts
.armUsesZeroCostExceptions
;
1757 _maxDefaultCommonAlignment
= opts
.maxDefaultCommonAlignment
;
1759 // parse start of mach-o file
1760 if ( ! parseLoadCommands(opts
.platform
, opts
.minOSVersion
, opts
.simulator
, opts
.ignoreMismatchPlatform
) )
1764 uint32_t sortedSectionIndexes
[_machOSectionsCount
];
1765 this->makeSortedSectionsArray(sortedSectionIndexes
);
1767 // make symbol table sorted by address
1768 this->prescanSymbolTable();
1769 uint32_t sortedSymbolIndexes
[_symbolsInSections
];
1770 this->makeSortedSymbolsArray(sortedSymbolIndexes
, sortedSectionIndexes
);
1772 // allocate Section<A> object for each mach-o section
1775 // if it exists, do special early parsing of __compact_unwind section
1776 uint32_t countOfCUs
= 0;
1777 if ( _compactUnwindSection
!= NULL
)
1778 countOfCUs
= _compactUnwindSection
->count();
1779 // stack allocate (if not too large) cuInfoBuffer
1780 STACK_ALLOC_IF_SMALL(typename CUSection
<A
>::Info
, cuInfoArray
, countOfCUs
, 1024);
1781 if ( countOfCUs
!= 0 )
1782 _compactUnwindSection
->parse(*this, countOfCUs
, cuInfoArray
);
1784 // create lists of address that already have compact unwind and thus don't need the dwarf parsed
1785 unsigned cuLsdaCount
= 0;
1786 STACK_ALLOC_IF_SMALL(pint_t
, cuStarts
, countOfCUs
, 1024);
1787 for (uint32_t i
=0; i
< countOfCUs
; ++i
) {
1788 if ( CUSection
<A
>::encodingMeansUseDwarf(cuInfoArray
[i
].compactUnwindInfo
) )
1791 cuStarts
[i
] = cuInfoArray
[i
].functionStartAddress
;
1792 if ( cuInfoArray
[i
].lsdaAddress
!= 0 )
1797 // if it exists, do special early parsing of __eh_frame section
1798 // stack allocate (if not too large) array of CFI_Atom_Info
1799 uint32_t countOfCFIs
= 0;
1800 if ( _EHFrameSection
!= NULL
)
1801 countOfCFIs
= _EHFrameSection
->cfiCount(*this);
1802 STACK_ALLOC_IF_SMALL(typename CFISection
<A
>::CFI_Atom_Info
, cfiArray
, countOfCFIs
, 1024);
1804 // stack allocate (if not too large) a copy of __eh_frame to apply relocations to
1805 uint32_t sectSize
= 4;
1806 if ( (countOfCFIs
!= 0) && _EHFrameSection
->needsRelocating() )
1807 sectSize
= _EHFrameSection
->machoSection()->size()+4;
1808 STACK_ALLOC_IF_SMALL(uint8_t, ehBuffer
, sectSize
, 50*1024);
1809 uint32_t cfiStartsCount
= 0;
1810 if ( countOfCFIs
!= 0 ) {
1811 _EHFrameSection
->cfiParse(*this, ehBuffer
, cfiArray
, countOfCFIs
, cuStarts
, countOfCUs
);
1812 // count functions and lsdas
1813 for(uint32_t i
=0; i
< countOfCFIs
; ++i
) {
1814 if ( cfiArray
[i
].isCIE
)
1816 //fprintf(stderr, "cfiArray[i].func = 0x%08llX, cfiArray[i].lsda = 0x%08llX, encoding=0x%08X\n",
1817 // (uint64_t)cfiArray[i].u.fdeInfo.function.targetAddress,
1818 // (uint64_t)cfiArray[i].u.fdeInfo.lsda.targetAddress,
1819 // cfiArray[i].u.fdeInfo.compactUnwindInfo);
1820 if ( cfiArray
[i
].u
.fdeInfo
.function
.targetAddress
!= CFI_INVALID_ADDRESS
)
1822 if ( cfiArray
[i
].u
.fdeInfo
.lsda
.targetAddress
!= CFI_INVALID_ADDRESS
)
1826 CFI_CU_InfoArrays
cfis(cfiArray
, countOfCFIs
, cuInfoArray
, countOfCUs
);
1828 // create sorted array of function starts and lsda starts
1829 pint_t cfiStartsArray
[cfiStartsCount
+cuLsdaCount
];
1830 uint32_t countOfFDEs
= 0;
1831 uint32_t cfiStartsArrayCount
= 0;
1832 if ( countOfCFIs
!= 0 ) {
1833 for(uint32_t i
=0; i
< countOfCFIs
; ++i
) {
1834 if ( cfiArray
[i
].isCIE
)
1836 if ( cfiArray
[i
].u
.fdeInfo
.function
.targetAddress
!= CFI_INVALID_ADDRESS
)
1837 cfiStartsArray
[cfiStartsArrayCount
++] = realAddr(cfiArray
[i
].u
.fdeInfo
.function
.targetAddress
);
1838 if ( cfiArray
[i
].u
.fdeInfo
.lsda
.targetAddress
!= CFI_INVALID_ADDRESS
)
1839 cfiStartsArray
[cfiStartsArrayCount
++] = cfiArray
[i
].u
.fdeInfo
.lsda
.targetAddress
;
1843 if ( cuLsdaCount
!= 0 ) {
1844 // merge in an lsda info from compact unwind
1845 for (uint32_t i
=0; i
< countOfCUs
; ++i
) {
1846 if ( cuInfoArray
[i
].lsdaAddress
== 0 )
1848 // append to cfiStartsArray if not already in that list
1850 for(uint32_t j
=0; j
< cfiStartsArrayCount
; ++j
) {
1851 if ( cfiStartsArray
[j
] == cuInfoArray
[i
].lsdaAddress
)
1855 cfiStartsArray
[cfiStartsArrayCount
++] = cuInfoArray
[i
].lsdaAddress
;
1859 if ( cfiStartsArrayCount
!= 0 ) {
1860 ::qsort(cfiStartsArray
, cfiStartsArrayCount
, sizeof(pint_t
), pointerSorter
);
1862 // scan for FDEs claming the same function
1863 for(uint32_t i
=1; i
< cfiStartsArrayCount
; ++i
) {
1864 assert( cfiStartsArray
[i
] != cfiStartsArray
[i
-1] );
1869 Section
<A
>** sections
= _file
->_sectionsArray
;
1870 uint32_t sectionsCount
= _file
->_sectionsArrayCount
;
1872 // figure out how many atoms will be allocated and allocate
1873 LabelAndCFIBreakIterator
breakIterator(sortedSymbolIndexes
, _symbolsInSections
, cfiStartsArray
,
1874 cfiStartsArrayCount
, _overlappingSymbols
);
1875 uint32_t computedAtomCount
= 0;
1876 for (uint32_t i
=0; i
< sectionsCount
; ++i
) {
1877 breakIterator
.beginSection();
1878 uint32_t count
= sections
[i
]->computeAtomCount(*this, breakIterator
, cfis
);
1879 //const macho_section<P>* sect = sections[i]->machoSection();
1880 //fprintf(stderr, "computed count=%u for section %s size=%llu\n", count, sect->sectname(), (sect != NULL) ? sect->size() : 0);
1881 computedAtomCount
+= count
;
1883 //fprintf(stderr, "allocating %d atoms * sizeof(Atom<A>)=%ld, sizeof(ld::Atom)=%ld\n", computedAtomCount, sizeof(Atom<A>), sizeof(ld::Atom));
1884 _file
->_atomsArray
= new uint8_t[computedAtomCount
*sizeof(Atom
<A
>)];
1885 _file
->_atomsArrayCount
= 0;
1887 // have each section append atoms to _atomsArray
1888 LabelAndCFIBreakIterator
breakIterator2(sortedSymbolIndexes
, _symbolsInSections
, cfiStartsArray
,
1889 cfiStartsArrayCount
, _overlappingSymbols
);
1890 for (uint32_t i
=0; i
< sectionsCount
; ++i
) {
1891 uint8_t* atoms
= _file
->_atomsArray
+ _file
->_atomsArrayCount
*sizeof(Atom
<A
>);
1892 breakIterator2
.beginSection();
1893 uint32_t count
= sections
[i
]->appendAtoms(*this, atoms
, breakIterator2
, cfis
);
1894 //fprintf(stderr, "append count=%u for section %s/%s\n", count, sections[i]->machoSection()->segname(), sections[i]->machoSection()->sectname());
1895 _file
->_atomsArrayCount
+= count
;
1897 assert( _file
->_atomsArrayCount
== computedAtomCount
&& "more atoms allocated than expected");
1900 // have each section add all fix-ups for its atoms
1901 _allFixups
.reserve(computedAtomCount
*5);
1902 for (uint32_t i
=0; i
< sectionsCount
; ++i
)
1903 sections
[i
]->makeFixups(*this, cfis
);
1905 // assign fixups start offset for each atom
1906 uint8_t* p
= _file
->_atomsArray
;
1907 uint32_t fixupOffset
= 0;
1908 for(int i
=_file
->_atomsArrayCount
; i
> 0; --i
) {
1909 Atom
<A
>* atom
= (Atom
<A
>*)p
;
1910 atom
->_fixupsStartIndex
= fixupOffset
;
1911 fixupOffset
+= atom
->_fixupsCount
;
1912 atom
->_fixupsCount
= 0;
1913 p
+= sizeof(Atom
<A
>);
1915 assert(fixupOffset
== _allFixups
.size());
1916 _file
->_fixups
.resize(fixupOffset
);
1918 // copy each fixup for each atom
1919 for(typename
std::vector
<FixupInAtom
>::iterator it
=_allFixups
.begin(); it
!= _allFixups
.end(); ++it
) {
1920 uint32_t slot
= it
->atom
->_fixupsStartIndex
+ it
->atom
->_fixupsCount
;
1921 _file
->_fixups
[slot
] = it
->fixup
;
1922 it
->atom
->_fixupsCount
++;
1925 // done with temp vector
1929 _file
->_unwindInfos
.reserve(countOfFDEs
+countOfCUs
);
1930 for(uint32_t i
=0; i
< countOfCFIs
; ++i
) {
1931 if ( cfiArray
[i
].isCIE
)
1933 if ( cfiArray
[i
].u
.fdeInfo
.function
.targetAddress
!= CFI_INVALID_ADDRESS
) {
1934 ld::Atom::UnwindInfo info
;
1935 info
.startOffset
= 0;
1936 info
.unwindInfo
= cfiArray
[i
].u
.fdeInfo
.compactUnwindInfo
;
1937 _file
->_unwindInfos
.push_back(info
);
1938 Atom
<A
>* func
= findAtomByAddress(cfiArray
[i
].u
.fdeInfo
.function
.targetAddress
);
1939 func
->setUnwindInfoRange(_file
->_unwindInfos
.size()-1, 1);
1940 //fprintf(stderr, "cu from dwarf =0x%08X, atom=%s\n", info.unwindInfo, func->name());
1943 // apply compact infos in __LD,__compact_unwind section to each function
1944 // if function also has dwarf unwind, CU will override it
1945 Atom
<A
>* lastFunc
= NULL
;
1946 uint32_t lastEnd
= 0;
1947 for(uint32_t i
=0; i
< countOfCUs
; ++i
) {
1948 typename CUSection
<A
>::Info
* info
= &cuInfoArray
[i
];
1949 assert(info
->function
!= NULL
);
1950 ld::Atom::UnwindInfo ui
;
1951 ui
.startOffset
= info
->functionStartAddress
- info
->function
->objectAddress();
1952 ui
.unwindInfo
= info
->compactUnwindInfo
;
1953 _file
->_unwindInfos
.push_back(ui
);
1954 // don't override with converted cu with "use dwarf" cu, if forcing dwarf conversion
1955 if ( !_forceDwarfConversion
|| !CUSection
<A
>::encodingMeansUseDwarf(info
->compactUnwindInfo
) ) {
1956 //fprintf(stderr, "cu=0x%08X, atom=%s\n", ui.unwindInfo, info->function->name());
1957 // if previous is for same function, extend range
1958 if ( info
->function
== lastFunc
) {
1959 if ( lastEnd
!= ui
.startOffset
) {
1960 if ( lastEnd
< ui
.startOffset
)
1961 warning("__LD,__compact_unwind entries for %s have a gap at offset 0x%0X", info
->function
->name(), lastEnd
);
1963 warning("__LD,__compact_unwind entries for %s overlap at offset 0x%0X", info
->function
->name(), lastEnd
);
1965 lastFunc
->extendUnwindInfoRange();
1968 info
->function
->setUnwindInfoRange(_file
->_unwindInfos
.size()-1, 1);
1969 lastFunc
= info
->function
;
1970 lastEnd
= ui
.startOffset
+ info
->rangeLength
;
1974 // process indirect symbols which become AliasAtoms
1975 _file
->_aliasAtomsArray
= NULL
;
1976 _file
->_aliasAtomsArrayCount
= 0;
1977 if ( _indirectSymbolCount
!= 0 ) {
1978 _file
->_aliasAtomsArrayCount
= _indirectSymbolCount
;
1979 _file
->_aliasAtomsArray
= new uint8_t[_file
->_aliasAtomsArrayCount
*sizeof(AliasAtom
)];
1980 this->appendAliasAtoms(_file
->_aliasAtomsArray
);
1984 // parse dwarf debug info to get line info
1985 this->parseDebugInfo();
1990 static void versionToString(uint32_t value
, char buffer
[32])
1993 sprintf(buffer
, "%d.%d.%d", value
>> 16, (value
>> 8) & 0xFF, value
& 0xFF);
1995 sprintf(buffer
, "%d.%d", value
>> 16, (value
>> 8) & 0xFF);
1998 template <> uint8_t Parser
<x86
>::loadCommandSizeMask() { return 0x03; }
1999 template <> uint8_t Parser
<x86_64
>::loadCommandSizeMask() { return 0x07; }
2000 template <> uint8_t Parser
<arm
>::loadCommandSizeMask() { return 0x03; }
2001 template <> uint8_t Parser
<arm64
>::loadCommandSizeMask() { return 0x07; }
2003 template <typename A
>
2004 bool Parser
<A
>::parseLoadCommands(Options::Platform platform
, uint32_t linkMinOSVersion
, bool simulator
, bool ignoreMismatchPlatform
)
2006 const macho_header
<P
>* header
= (const macho_header
<P
>*)_fileContent
;
2008 // set File attributes
2009 _file
->_canScatterAtoms
= (header
->flags() & MH_SUBSECTIONS_VIA_SYMBOLS
);
2010 _file
->_cpuSubType
= header
->cpusubtype();
2012 const macho_segment_command
<P
>* segment
= NULL
;
2013 const uint8_t* const endOfFile
= _fileContent
+ _fileLength
;
2014 const uint32_t cmd_count
= header
->ncmds();
2015 // <rdar://problem/5394172> an empty .o file with zero load commands will crash linker
2016 if ( cmd_count
== 0 )
2018 Options::Platform lcPlatform
= Options::kPlatformUnknown
;
2019 const macho_load_command
<P
>* const cmds
= (macho_load_command
<P
>*)((char*)header
+ sizeof(macho_header
<P
>));
2020 const macho_load_command
<P
>* const cmdsEnd
= (macho_load_command
<P
>*)((char*)header
+ sizeof(macho_header
<P
>) + header
->sizeofcmds());
2021 const macho_load_command
<P
>* cmd
= cmds
;
2022 for (uint32_t i
= 0; i
< cmd_count
; ++i
) {
2023 uint32_t size
= cmd
->cmdsize();
2024 if ( (size
& this->loadCommandSizeMask()) != 0 )
2025 throwf("load command #%d has a unaligned size", i
);
2026 const uint8_t* endOfCmd
= ((uint8_t*)cmd
)+cmd
->cmdsize();
2027 if ( endOfCmd
> (uint8_t*)cmdsEnd
)
2028 throwf("load command #%d extends beyond the end of the load commands", i
);
2029 if ( endOfCmd
> endOfFile
)
2030 throwf("load command #%d extends beyond the end of the file", i
);
2031 switch (cmd
->cmd()) {
2034 const macho_symtab_command
<P
>* symtab
= (macho_symtab_command
<P
>*)cmd
;
2035 _symbolCount
= symtab
->nsyms();
2036 _symbols
= (const macho_nlist
<P
>*)(_fileContent
+ symtab
->symoff());
2037 _strings
= (char*)_fileContent
+ symtab
->stroff();
2038 _stringsSize
= symtab
->strsize();
2039 if ( (symtab
->symoff() + _symbolCount
*sizeof(macho_nlist
<P
>)) > _fileLength
)
2040 throw "mach-o symbol table extends beyond end of file";
2041 if ( (_strings
+ _stringsSize
) > (char*)endOfFile
)
2042 throw "mach-o string pool extends beyond end of file";
2043 if ( _indirectTable
== NULL
) {
2044 if ( _undefinedEndIndex
== 0 ) {
2045 _undefinedStartIndex
= 0;
2046 _undefinedEndIndex
= symtab
->nsyms();
2053 const macho_dysymtab_command
<P
>* dsymtab
= (macho_dysymtab_command
<P
>*)cmd
;
2054 _indirectTable
= (uint32_t*)(_fileContent
+ dsymtab
->indirectsymoff());
2055 _indirectTableCount
= dsymtab
->nindirectsyms();
2056 if ( &_indirectTable
[_indirectTableCount
] > (uint32_t*)endOfFile
)
2057 throw "indirect symbol table extends beyond end of file";
2058 _undefinedStartIndex
= dsymtab
->iundefsym();
2059 _undefinedEndIndex
= _undefinedStartIndex
+ dsymtab
->nundefsym();
2065 case LC_DATA_IN_CODE
:
2067 const macho_linkedit_data_command
<P
>* dc
= (macho_linkedit_data_command
<P
>*)cmd
;
2068 _dataInCodeStart
= (macho_data_in_code_entry
<P
>*)(_fileContent
+ dc
->dataoff());
2069 _dataInCodeEnd
= (macho_data_in_code_entry
<P
>*)(_fileContent
+ dc
->dataoff() + dc
->datasize());
2070 if ( _dataInCodeEnd
> (macho_data_in_code_entry
<P
>*)endOfFile
)
2071 throw "LC_DATA_IN_CODE table extends beyond end of file";
2074 case LC_LINKER_OPTION
:
2076 const macho_linker_option_command
<P
>* loc
= (macho_linker_option_command
<P
>*)cmd
;
2077 const char* buffer
= loc
->buffer();
2078 _file
->_linkerOptions
.resize(_file
->_linkerOptions
.size() + 1);
2079 std::vector
<const char*>& vec
= _file
->_linkerOptions
.back();
2080 for (uint32_t j
=0; j
< loc
->count(); ++j
) {
2081 vec
.push_back(buffer
);
2082 buffer
+= strlen(buffer
) + 1;
2084 if ( buffer
> ((char*)cmd
+ loc
->cmdsize()) )
2085 throw "malformed LC_LINKER_OPTION";
2088 case LC_LINKER_OPTIMIZATION_HINTS
:
2090 const macho_linkedit_data_command
<P
>* loh
= (macho_linkedit_data_command
<P
>*)cmd
;
2091 _lohStart
= _fileContent
+ loh
->dataoff();
2092 _lohEnd
= _fileContent
+ loh
->dataoff() + loh
->datasize();
2093 if ( _lohEnd
> endOfFile
)
2094 throw "LC_LINKER_OPTIMIZATION_HINTS table extends beyond end of file";
2097 case LC_VERSION_MIN_MACOSX
:
2098 case LC_VERSION_MIN_IPHONEOS
:
2099 case LC_VERSION_MIN_WATCHOS
:
2100 #if SUPPORT_APPLE_TV
2101 case LC_VERSION_MIN_TVOS
:
2103 if ( ignoreMismatchPlatform
)
2105 lcPlatform
= Options::platformForLoadCommand(cmd
->cmd());
2106 _file
->_platform
= lcPlatform
;
2107 _file
->_minOSVersion
= ((macho_version_min_command
<P
>*)cmd
)->version();
2109 case LC_BUILD_VERSION
:
2111 const macho_build_version_command
<P
>* buildVersCmd
= (macho_build_version_command
<P
>*)cmd
;
2112 if ( ignoreMismatchPlatform
)
2114 lcPlatform
= (Options::Platform
)buildVersCmd
->platform();
2115 _file
->_platform
= lcPlatform
;
2116 _file
->_minOSVersion
= buildVersCmd
->minos();
2117 const macho_build_tool_version
<P
>* entry
= (macho_build_tool_version
<P
>*)((uint8_t*)cmd
+ sizeof(macho_build_version_command
<P
>));
2118 for (uint32_t t
=0; t
< buildVersCmd
->ntools(); ++t
) {
2119 _file
->_toolVersions
.push_back(std::make_pair(entry
->tool(), entry
->version()));
2124 case macho_segment_command
<P
>::CMD
:
2125 if ( segment
!= NULL
)
2126 throw "more than one LC_SEGMENT found in object file";
2127 segment
= (macho_segment_command
<P
>*)cmd
;
2130 // ignore unknown load commands
2133 cmd
= (const macho_load_command
<P
>*)(((char*)cmd
)+cmd
->cmdsize());
2134 if ( cmd
> cmdsEnd
)
2135 throwf("malformed mach-o file, load command #%d is outside size of load commands", i
);
2137 // arm/arm64 objects are default to ios platform if not set.
2138 // rdar://problem/21746314
2139 if (lcPlatform
== Options::kPlatformUnknown
&&
2140 (std::is_same
<A
, arm
>::value
|| std::is_same
<A
, arm64
>::value
))
2141 lcPlatform
= Options::kPlatformiOS
;
2143 // Check platform cross-linking.
2144 if ( !ignoreMismatchPlatform
) {
2145 if ( lcPlatform
!= platform
) {
2147 case Options::kPlatformOSX
:
2148 case Options::kPlatformiOS
:
2149 if ( lcPlatform
== Options::kPlatformUnknown
)
2151 // fall through if the Platform is not Unknown
2152 case Options::kPlatform_bridgeOS
:
2153 case Options::kPlatformWatchOS
:
2154 // Error when using bitcocde, warning otherwise.
2156 throwf("building for %s%s, but linking in object file built for %s,",
2157 Options::platformName(platform
), (simulator
? " simulator" : ""),
2158 Options::platformName(lcPlatform
));
2160 warning("URGENT: building for %s%s, but linking in object file (%s) built for %s. "
2161 "Note: This will be an error in the future.",
2162 Options::platformName(platform
), (simulator
? " simulator" : ""), path(),
2163 Options::platformName(lcPlatform
));
2165 #if SUPPORT_APPLE_TV
2166 case Options::kPlatform_tvOS
:
2167 // Error when using bitcocde, warning otherwise.
2169 throwf("building for %s%s, but linking in object file built for %s,",
2170 Options::platformName(platform
), (simulator
? " simulator" : ""),
2171 Options::platformName(lcPlatform
));
2173 warning("URGENT: building for %s%s, but linking in object file (%s) built for %s. "
2174 "Note: This will be an error in the future.",
2175 Options::platformName(platform
), (simulator
? " simulator" : ""), path(),
2176 Options::platformName(lcPlatform
));
2179 case Options::kPlatformUnknown
:
2180 // skip if the target platform is unknown
2184 if ( linkMinOSVersion
&& (_file
->_minOSVersion
> linkMinOSVersion
) ) {
2187 versionToString(_file
->_minOSVersion
, t1
);
2188 versionToString(linkMinOSVersion
, t2
);
2189 warning("object file (%s) was built for newer %s version (%s) than being linked (%s)",
2190 _path
, Options::platformName(lcPlatform
), t1
, t2
);
2195 // record range of sections
2196 if ( segment
== NULL
)
2197 throw "missing LC_SEGMENT";
2198 _sectionsStart
= (macho_section
<P
>*)((char*)segment
+ sizeof(macho_segment_command
<P
>));
2199 _machOSectionsCount
= segment
->nsects();
2200 if ( (sizeof(macho_segment_command
<P
>) + _machOSectionsCount
* sizeof(macho_section
<P
>)) > segment
->cmdsize() )
2201 throw "too many sections for size of LC_SEGMENT command";
2205 template <typename A
>
2206 Options::Platform Parser
<A
>::findPlatform(const macho_header
<P
>* header
)
2208 const uint32_t cmd_count
= header
->ncmds();
2209 if ( cmd_count
== 0 )
2210 return Options::kPlatformUnknown
;
2211 const macho_load_command
<P
>* const cmds
= (macho_load_command
<P
>*)((char*)header
+ sizeof(macho_header
<P
>));
2212 const macho_load_command
<P
>* const cmdsEnd
= (macho_load_command
<P
>*)((char*)header
+ sizeof(macho_header
<P
>) + header
->sizeofcmds());
2213 const macho_load_command
<P
>* cmd
= cmds
;
2214 for (uint32_t i
= 0; i
< cmd_count
; ++i
) {
2215 uint32_t size
= cmd
->cmdsize();
2216 if ( (size
& loadCommandSizeMask()) != 0 )
2217 throwf("load command #%d has a unaligned size", i
);
2218 const uint8_t* endOfCmd
= ((uint8_t*)cmd
)+cmd
->cmdsize();
2219 if ( endOfCmd
> (uint8_t*)cmdsEnd
)
2220 throwf("load command #%d extends beyond the end of the load commands", i
);
2221 switch (cmd
->cmd()) {
2222 case LC_VERSION_MIN_MACOSX
:
2223 return Options::kPlatformOSX
;
2224 case LC_VERSION_MIN_IPHONEOS
:
2225 return Options::kPlatformiOS
;
2226 case LC_VERSION_MIN_WATCHOS
:
2227 return Options::kPlatformWatchOS
;
2228 #if SUPPORT_APPLE_TV
2229 case LC_VERSION_MIN_TVOS
:
2230 return Options::kPlatform_tvOS
;
2232 case LC_BUILD_VERSION
:
2233 return (Options::Platform
)((macho_build_version_command
<P
>*)cmd
)->platform();
2235 cmd
= (const macho_load_command
<P
>*)(((char*)cmd
)+cmd
->cmdsize());
2236 if ( cmd
> cmdsEnd
)
2237 throwf("malformed mach-o file, load command #%d is outside size of load commands", i
);
2239 return Options::kPlatformUnknown
;
2243 template <typename A
>
2244 void Parser
<A
>::prescanSymbolTable()
2246 _tentativeDefinitionCount
= 0;
2247 _absoluteSymbolCount
= 0;
2248 _symbolsInSections
= 0;
2249 _hasDataInCodeLabels
= false;
2250 for (uint32_t i
=0; i
< this->_symbolCount
; ++i
) {
2251 const macho_nlist
<P
>& sym
= symbolFromIndex(i
);
2253 if ( (sym
.n_type() & N_STAB
) != 0 )
2256 // look at undefines
2257 const char* symbolName
= this->nameFromSymbol(sym
);
2258 if ( (sym
.n_type() & N_TYPE
) == N_UNDF
) {
2259 if ( sym
.n_value() != 0 ) {
2260 // count tentative definitions
2261 ++_tentativeDefinitionCount
;
2263 else if ( strncmp(symbolName
, "___dtrace_", 10) == 0 ) {
2264 // any undefined starting with __dtrace_*$ that is not ___dtrace_probe$* or ___dtrace_isenabled$*
2265 // is extra provider info
2266 if ( (strncmp(&symbolName
[10], "probe$", 6) != 0) && (strncmp(&symbolName
[10], "isenabled$", 10) != 0) ) {
2267 _dtraceProviderInfo
.push_back(symbolName
);
2272 else if ( ((sym
.n_type() & N_TYPE
) == N_INDR
) && ((sym
.n_type() & N_EXT
) != 0) ) {
2273 _indirectSymbolCount
++;
2277 // count absolute symbols
2278 if ( (sym
.n_type() & N_TYPE
) == N_ABS
) {
2279 const char* absName
= this->nameFromSymbol(sym
);
2280 // ignore .objc_class_name_* symbols
2281 if ( strncmp(absName
, ".objc_class_name_", 17) == 0 ) {
2285 // ignore .objc_class_name_* symbols
2286 if ( strncmp(absName
, ".objc_category_name_", 20) == 0 )
2288 // ignore empty *.eh symbols
2289 if ( strcmp(&absName
[strlen(absName
)-3], ".eh") == 0 )
2291 ++_absoluteSymbolCount
;
2294 // only look at definitions
2295 if ( (sym
.n_type() & N_TYPE
) != N_SECT
)
2298 // 'L' labels do not denote atom breaks
2299 if ( symbolName
[0] == 'L' ) {
2300 // <rdar://problem/9218847> Formalize data in code with L$start$ labels
2301 if ( strncmp(symbolName
, "L$start$", 8) == 0 )
2302 _hasDataInCodeLabels
= true;
2305 // how many def syms in each section
2306 if ( sym
.n_sect() > _machOSectionsCount
)
2307 throw "bad n_sect in symbol table";
2309 _symbolsInSections
++;
2313 template <typename A
>
2314 void Parser
<A
>::appendAliasAtoms(uint8_t* p
)
2316 for (uint32_t i
=0; i
< this->_symbolCount
; ++i
) {
2317 const macho_nlist
<P
>& sym
= symbolFromIndex(i
);
2319 if ( (sym
.n_type() & N_STAB
) != 0 )
2322 // only look at N_INDR symbols
2323 if ( (sym
.n_type() & N_TYPE
) != N_INDR
)
2326 // skip non-external aliases
2327 if ( (sym
.n_type() & N_EXT
) == 0 )
2330 const char* symbolName
= this->nameFromSymbol(sym
);
2331 const char* aliasOfName
= &_strings
[sym
.n_value()];
2332 bool isHiddenVisibility
= (sym
.n_type() & N_PEXT
);
2333 AliasAtom
* allocatedSpace
= (AliasAtom
*)p
;
2334 new (allocatedSpace
) AliasAtom(symbolName
, isHiddenVisibility
, _file
, aliasOfName
);
2335 p
+= sizeof(AliasAtom
);
2341 template <typename A
>
2342 int Parser
<A
>::sectionIndexSorter(void* extra
, const void* l
, const void* r
)
2344 Parser
<A
>* parser
= (Parser
<A
>*)extra
;
2345 const uint32_t* left
= (uint32_t*)l
;
2346 const uint32_t* right
= (uint32_t*)r
;
2347 const macho_section
<P
>* leftSect
= parser
->machOSectionFromSectionIndex(*left
);
2348 const macho_section
<P
>* rightSect
= parser
->machOSectionFromSectionIndex(*right
);
2350 // can't just return difference because 64-bit diff does not fit in 32-bit return type
2351 int64_t result
= leftSect
->addr() - rightSect
->addr();
2352 if ( result
== 0 ) {
2353 // two sections with same start address
2354 // one with zero size goes first
2355 bool leftEmpty
= ( leftSect
->size() == 0 );
2356 bool rightEmpty
= ( rightSect
->size() == 0 );
2357 if ( leftEmpty
!= rightEmpty
) {
2358 return ( rightEmpty
? 1 : -1 );
2360 if ( !leftEmpty
&& !rightEmpty
)
2361 throwf("overlapping sections");
2362 // both empty, so chose file order
2363 return ( rightSect
- leftSect
);
2365 else if ( result
< 0 )
2371 template <typename A
>
2372 void Parser
<A
>::makeSortedSectionsArray(uint32_t array
[])
2374 const bool log
= false;
2377 fprintf(stderr
, "unsorted sections:\n");
2378 for(unsigned int i
=0; i
< _machOSectionsCount
; ++i
)
2379 fprintf(stderr
, "0x%08llX %s %s\n", _sectionsStart
[i
].addr(), _sectionsStart
[i
].segname(), _sectionsStart
[i
].sectname());
2382 // sort by symbol table address
2383 for (uint32_t i
=0; i
< _machOSectionsCount
; ++i
)
2385 ::qsort_r(array
, _machOSectionsCount
, sizeof(uint32_t), this, §ionIndexSorter
);
2388 fprintf(stderr
, "sorted sections:\n");
2389 for(unsigned int i
=0; i
< _machOSectionsCount
; ++i
)
2390 fprintf(stderr
, "0x%08llX %s %s\n", _sectionsStart
[array
[i
]].addr(), _sectionsStart
[array
[i
]].segname(), _sectionsStart
[array
[i
]].sectname());
2396 template <typename A
>
2397 int Parser
<A
>::symbolIndexSorter(void* extra
, const void* l
, const void* r
)
2399 ParserAndSectionsArray
* extraInfo
= (ParserAndSectionsArray
*)extra
;
2400 Parser
<A
>* parser
= extraInfo
->parser
;
2401 const uint32_t* sortedSectionsArray
= extraInfo
->sortedSectionsArray
;
2402 const uint32_t* left
= (uint32_t*)l
;
2403 const uint32_t* right
= (uint32_t*)r
;
2404 const macho_nlist
<P
>& leftSym
= parser
->symbolFromIndex(*left
);
2405 const macho_nlist
<P
>& rightSym
= parser
->symbolFromIndex(*right
);
2406 // can't just return difference because 64-bit diff does not fit in 32-bit return type
2407 int64_t result
= leftSym
.n_value() - rightSym
.n_value();
2408 if ( result
== 0 ) {
2409 // two symbols with same address
2410 // if in different sections, sort earlier section first
2411 if ( leftSym
.n_sect() != rightSym
.n_sect() ) {
2412 for (uint32_t i
=0; i
< parser
->machOSectionCount(); ++i
) {
2413 if ( sortedSectionsArray
[i
]+1 == leftSym
.n_sect() )
2415 if ( sortedSectionsArray
[i
]+1 == rightSym
.n_sect() )
2419 // two symbols in same section, means one is an alias
2420 // if one is ltmp*, make it an alias (sort first)
2421 const char* leftName
= parser
->nameFromSymbol(leftSym
);
2422 const char* rightName
= parser
->nameFromSymbol(rightSym
);
2423 bool leftIsTmp
= strncmp(leftName
, "ltmp", 4);
2424 bool rightIsTmp
= strncmp(rightName
, "ltmp", 4);
2425 if ( leftIsTmp
!= rightIsTmp
) {
2426 return (rightIsTmp
? -1 : 1);
2429 // if only one is global, make the other an alias (sort first)
2430 if ( (leftSym
.n_type() & N_EXT
) != (rightSym
.n_type() & N_EXT
) ) {
2431 if ( (rightSym
.n_type() & N_EXT
) != 0 )
2436 // if both are global, sort alphabetically. earlier one will be the alias
2437 return ( strcmp(rightName
, leftName
) );
2439 else if ( result
< 0 )
2446 template <typename A
>
2447 void Parser
<A
>::makeSortedSymbolsArray(uint32_t array
[], const uint32_t sectionArray
[])
2449 const bool log
= false;
2451 uint32_t* p
= array
;
2452 for (uint32_t i
=0; i
< this->_symbolCount
; ++i
) {
2453 const macho_nlist
<P
>& sym
= symbolFromIndex(i
);
2455 if ( (sym
.n_type() & N_STAB
) != 0 )
2458 // only look at definitions
2459 if ( (sym
.n_type() & N_TYPE
) != N_SECT
)
2462 // 'L' labels do not denote atom breaks
2463 const char* symbolName
= this->nameFromSymbol(sym
);
2464 if ( symbolName
[0] == 'L' )
2467 // how many def syms in each section
2468 if ( sym
.n_sect() > _machOSectionsCount
)
2469 throw "bad n_sect in symbol table";
2474 assert(p
== &array
[_symbolsInSections
] && "second pass over symbol table yield a different number of symbols");
2476 // sort by symbol table address
2477 ParserAndSectionsArray extra
= { this, sectionArray
};
2478 ::qsort_r(array
, _symbolsInSections
, sizeof(uint32_t), &extra
, &symbolIndexSorter
);
2481 // look for two symbols at same address
2482 _overlappingSymbols
= false;
2483 for (unsigned int i
=1; i
< _symbolsInSections
; ++i
) {
2484 if ( symbolFromIndex(array
[i
-1]).n_value() == symbolFromIndex(array
[i
]).n_value() ) {
2485 //fprintf(stderr, "overlapping symbols at 0x%08llX\n", symbolFromIndex(array[i-1]).n_value());
2486 _overlappingSymbols
= true;
2492 fprintf(stderr
, "sorted symbols:\n");
2493 for(unsigned int i
=0; i
< _symbolsInSections
; ++i
)
2494 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
])) );
2498 template <typename A
>
2499 void Parser
<A
>::makeSections()
2501 // classify each section by type
2502 // compute how many Section objects will be needed and total size for all
2503 unsigned int totalSectionsSize
= 0;
2504 uint8_t machOSectsStorage
[sizeof(MachOSectionAndSectionClass
<P
>)*(_machOSectionsCount
+2)]; // also room for tentative-defs and absolute symbols
2505 // allocate raw storage for all section objects on stack
2506 MachOSectionAndSectionClass
<P
>* machOSects
= (MachOSectionAndSectionClass
<P
>*)machOSectsStorage
;
2507 unsigned int count
= 0;
2508 // local variable for bitcode parsing
2509 const macho_section
<P
>* bitcodeSect
= NULL
;
2510 const macho_section
<P
>* cmdlineSect
= NULL
;
2511 const macho_section
<P
>* swiftCmdlineSect
= NULL
;
2512 const macho_section
<P
>* bundleSect
= NULL
;
2513 bool bitcodeAsm
= false;
2515 for (uint32_t i
=0; i
< _machOSectionsCount
; ++i
) {
2516 const macho_section
<P
>* sect
= &_sectionsStart
[i
];
2517 uint8_t sectionType
= (sect
->flags() & SECTION_TYPE
);
2518 if ( (sect
->offset() + sect
->size() > _fileLength
) && (sectionType
!= S_ZEROFILL
) && (sectionType
!= S_THREAD_LOCAL_ZEROFILL
) )
2519 throwf("section %s/%s extends beyond end of file,", sect
->segname(), sect
->sectname());
2521 if ( (sect
->flags() & S_ATTR_DEBUG
) != 0 ) {
2522 if ( strcmp(sect
->segname(), "__DWARF") == 0 ) {
2523 // note that .o file has dwarf
2524 _file
->_debugInfoKind
= ld::relocatable::File::kDebugInfoDwarf
;
2525 // save off iteresting dwarf sections
2526 if ( strcmp(sect
->sectname(), "__debug_info") == 0 )
2527 _file
->_dwarfDebugInfoSect
= sect
;
2528 else if ( strcmp(sect
->sectname(), "__debug_abbrev") == 0 )
2529 _file
->_dwarfDebugAbbrevSect
= sect
;
2530 else if ( strcmp(sect
->sectname(), "__debug_line") == 0 )
2531 _file
->_dwarfDebugLineSect
= sect
;
2532 else if ( strcmp(sect
->sectname(), "__debug_str") == 0 )
2533 _file
->_dwarfDebugStringSect
= sect
;
2534 // linker does not propagate dwarf sections to output file
2537 else if ( strcmp(sect
->segname(), "__LD") == 0 ) {
2538 if ( strncmp(sect
->sectname(), "__compact_unwind", 16) == 0 ) {
2539 machOSects
[count
].sect
= sect
;
2540 totalSectionsSize
+= sizeof(CUSection
<A
>);
2541 machOSects
[count
++].type
= sectionTypeCompactUnwind
;
2546 if ( strcmp(sect
->segname(), "__LLVM") == 0 ) {
2547 // Process bitcode segement
2548 if ( strncmp(sect
->sectname(), "__bitcode", 9) == 0 ) {
2550 } else if ( strncmp(sect
->sectname(), "__cmdline", 9) == 0 ) {
2552 } else if ( strncmp(sect
->sectname(), "__swift_cmdline", 15) == 0 ) {
2553 swiftCmdlineSect
= sect
;
2554 } else if ( strncmp(sect
->sectname(), "__bundle", 8) == 0 ) {
2556 } else if ( strncmp(sect
->sectname(), "__asm", 5) == 0 ) {
2559 // If treat the bitcode as data, continue to parse as a normal section.
2560 if ( !_treateBitcodeAsData
)
2563 // ignore empty __OBJC sections
2564 if ( (sect
->size() == 0) && (strcmp(sect
->segname(), "__OBJC") == 0) )
2566 // objc image info section is really attributes and not content
2567 if ( ((strcmp(sect
->sectname(), "__image_info") == 0) && (strcmp(sect
->segname(), "__OBJC") == 0))
2568 || ((strncmp(sect
->sectname(), "__objc_imageinfo", 16) == 0) && (strcmp(sect
->segname(), "__DATA") == 0)) ) {
2569 // struct objc_image_info {
2570 // uint32_t version; // initially 0
2573 // #define OBJC_IMAGE_SUPPORTS_GC 2
2574 // #define OBJC_IMAGE_GC_ONLY 4
2575 // #define OBJC_IMAGE_IS_SIMULATED 32
2576 // #define OBJC_IMAGE_HAS_CATEGORY_CLASS_PROPERTIES 64
2578 const uint32_t* contents
= (uint32_t*)(_file
->fileContent()+sect
->offset());
2579 if ( (sect
->size() >= 8) && (contents
[0] == 0) ) {
2580 uint32_t flags
= E::get32(contents
[1]);
2581 if ( (flags
& 4) == 4 )
2582 _file
->_objConstraint
= ld::File::objcConstraintGC
;
2583 else if ( (flags
& 2) == 2 )
2584 _file
->_objConstraint
= ld::File::objcConstraintRetainReleaseOrGC
;
2585 else if ( (flags
& 32) == 32 )
2586 _file
->_objConstraint
= ld::File::objcConstraintRetainReleaseForSimulator
;
2588 _file
->_objConstraint
= ld::File::objcConstraintRetainRelease
;
2589 _file
->_swiftVersion
= ((flags
>> 8) & 0xFF);
2590 _file
->_objcHasCategoryClassPropertiesField
= (flags
& 64);
2591 if ( sect
->size() > 8 ) {
2592 warning("section %s/%s has unexpectedly large size %llu in %s",
2593 sect
->segname(), Section
<A
>::makeSectionName(sect
), sect
->size(), _file
->path());
2597 warning("can't parse %s/%s section in %s", sect
->segname(), Section
<A
>::makeSectionName(sect
), _file
->path());
2601 machOSects
[count
].sect
= sect
;
2602 switch ( sect
->flags() & SECTION_TYPE
) {
2603 case S_SYMBOL_STUBS
:
2604 if ( _stubsSectionNum
== 0 ) {
2605 _stubsSectionNum
= i
+1;
2606 _stubsMachOSection
= sect
;
2609 assert(1 && "multiple S_SYMBOL_STUBS sections");
2610 case S_LAZY_SYMBOL_POINTERS
:
2612 case S_4BYTE_LITERALS
:
2613 totalSectionsSize
+= sizeof(Literal4Section
<A
>);
2614 machOSects
[count
++].type
= sectionTypeLiteral4
;
2616 case S_8BYTE_LITERALS
:
2617 totalSectionsSize
+= sizeof(Literal8Section
<A
>);
2618 machOSects
[count
++].type
= sectionTypeLiteral8
;
2620 case S_16BYTE_LITERALS
:
2621 totalSectionsSize
+= sizeof(Literal16Section
<A
>);
2622 machOSects
[count
++].type
= sectionTypeLiteral16
;
2624 case S_NON_LAZY_SYMBOL_POINTERS
:
2625 totalSectionsSize
+= sizeof(NonLazyPointerSection
<A
>);
2626 machOSects
[count
++].type
= sectionTypeNonLazy
;
2628 case S_THREAD_LOCAL_VARIABLE_POINTERS
:
2629 totalSectionsSize
+= sizeof(TLVPointerSection
<A
>);
2630 machOSects
[count
++].type
= sectionTypeTLVPointers
;
2632 case S_LITERAL_POINTERS
:
2633 if ( (strcmp(sect
->segname(), "__OBJC") == 0) && (strcmp(sect
->sectname(), "__cls_refs") == 0) ) {
2634 totalSectionsSize
+= sizeof(Objc1ClassReferences
<A
>);
2635 machOSects
[count
++].type
= sectionTypeObjC1ClassRefs
;
2638 totalSectionsSize
+= sizeof(PointerToCStringSection
<A
>);
2639 machOSects
[count
++].type
= sectionTypeCStringPointer
;
2642 case S_CSTRING_LITERALS
:
2643 totalSectionsSize
+= sizeof(CStringSection
<A
>);
2644 machOSects
[count
++].type
= sectionTypeCString
;
2646 case S_MOD_INIT_FUNC_POINTERS
:
2647 case S_MOD_TERM_FUNC_POINTERS
:
2648 case S_THREAD_LOCAL_INIT_FUNCTION_POINTERS
:
2653 case S_THREAD_LOCAL_REGULAR
:
2654 case S_THREAD_LOCAL_ZEROFILL
:
2655 if ( (strcmp(sect
->segname(), "__TEXT") == 0) && (strcmp(sect
->sectname(), "__eh_frame") == 0) ) {
2656 totalSectionsSize
+= sizeof(CFISection
<A
>);
2657 machOSects
[count
++].type
= sectionTypeCFI
;
2659 else if ( (strcmp(sect
->segname(), "__DATA") == 0) && (strcmp(sect
->sectname(), "__cfstring") == 0) ) {
2660 totalSectionsSize
+= sizeof(CFStringSection
<A
>);
2661 machOSects
[count
++].type
= sectionTypeCFString
;
2663 else if ( (strcmp(sect
->segname(), "__TEXT") == 0) && (strcmp(sect
->sectname(), "__ustring") == 0) ) {
2664 totalSectionsSize
+= sizeof(UTF16StringSection
<A
>);
2665 machOSects
[count
++].type
= sectionTypeUTF16Strings
;
2667 else if ( (strcmp(sect
->segname(), "__DATA") == 0) && (strncmp(sect
->sectname(), "__objc_classrefs", 16) == 0) ) {
2668 totalSectionsSize
+= sizeof(ObjC2ClassRefsSection
<A
>);
2669 machOSects
[count
++].type
= sectionTypeObjC2ClassRefs
;
2671 else if ( (strcmp(sect
->segname(), "__DATA") == 0) && (strcmp(sect
->sectname(), "__objc_catlist") == 0) ) {
2672 totalSectionsSize
+= sizeof(ObjC2CategoryListSection
<A
>);
2673 machOSects
[count
++].type
= typeObjC2CategoryList
;
2675 else if ( _AppleObjc
&& (strcmp(sect
->segname(), "__OBJC") == 0) && (strcmp(sect
->sectname(), "__class") == 0) ) {
2676 totalSectionsSize
+= sizeof(ObjC1ClassSection
<A
>);
2677 machOSects
[count
++].type
= sectionTypeObjC1Classes
;
2680 totalSectionsSize
+= sizeof(SymboledSection
<A
>);
2681 machOSects
[count
++].type
= sectionTypeSymboled
;
2684 case S_THREAD_LOCAL_VARIABLES
:
2685 totalSectionsSize
+= sizeof(TLVDefsSection
<A
>);
2686 machOSects
[count
++].type
= sectionTypeTLVDefs
;
2689 throwf("unknown section type %d", sect
->flags() & SECTION_TYPE
);
2694 if ( bitcodeSect
!= NULL
) {
2695 if ( cmdlineSect
!= NULL
)
2696 _file
->_bitcode
= std::unique_ptr
<ld::Bitcode
>(new ld::ClangBitcode(&_fileContent
[bitcodeSect
->offset()], bitcodeSect
->size(),
2697 &_fileContent
[cmdlineSect
->offset()], cmdlineSect
->size()));
2698 else if ( swiftCmdlineSect
!= NULL
)
2699 _file
->_bitcode
= std::unique_ptr
<ld::Bitcode
>(new ld::SwiftBitcode(&_fileContent
[bitcodeSect
->offset()], bitcodeSect
->size(),
2700 &_fileContent
[swiftCmdlineSect
->offset()], swiftCmdlineSect
->size()));
2702 throwf("Object file with bitcode missing cmdline options: %s", _file
->path());
2704 else if ( bundleSect
!= NULL
)
2705 _file
->_bitcode
= std::unique_ptr
<ld::Bitcode
>(new ld::BundleBitcode(&_fileContent
[bundleSect
->offset()], bundleSect
->size()));
2706 else if ( bitcodeAsm
)
2707 _file
->_bitcode
= std::unique_ptr
<ld::Bitcode
>(new ld::AsmBitcode(_fileContent
, _fileLength
));
2709 // sort by address (mach-o object files don't aways have sections sorted)
2710 ::qsort(machOSects
, count
, sizeof(MachOSectionAndSectionClass
<P
>), MachOSectionAndSectionClass
<P
>::sorter
);
2712 // we will synthesize a dummy Section<A> object for tentative definitions
2713 if ( _tentativeDefinitionCount
> 0 ) {
2714 totalSectionsSize
+= sizeof(TentativeDefinitionSection
<A
>);
2715 machOSects
[count
++].type
= sectionTypeTentativeDefinitions
;
2718 // we will synthesize a dummy Section<A> object for Absolute symbols
2719 if ( _absoluteSymbolCount
> 0 ) {
2720 totalSectionsSize
+= sizeof(AbsoluteSymbolSection
<A
>);
2721 machOSects
[count
++].type
= sectionTypeAbsoluteSymbols
;
2724 // allocate one block for all Section objects as well as pointers to each
2725 uint8_t* space
= new uint8_t[totalSectionsSize
+count
*sizeof(Section
<A
>*)];
2726 _file
->_sectionsArray
= (Section
<A
>**)space
;
2727 _file
->_sectionsArrayCount
= count
;
2728 Section
<A
>** objects
= _file
->_sectionsArray
;
2729 space
+= count
*sizeof(Section
<A
>*);
2730 for (uint32_t i
=0; i
< count
; ++i
) {
2731 switch ( machOSects
[i
].type
) {
2732 case sectionTypeIgnore
:
2734 case sectionTypeLiteral4
:
2735 *objects
++ = new (space
) Literal4Section
<A
>(*this, *_file
, machOSects
[i
].sect
);
2736 space
+= sizeof(Literal4Section
<A
>);
2738 case sectionTypeLiteral8
:
2739 *objects
++ = new (space
) Literal8Section
<A
>(*this, *_file
, machOSects
[i
].sect
);
2740 space
+= sizeof(Literal8Section
<A
>);
2742 case sectionTypeLiteral16
:
2743 *objects
++ = new (space
) Literal16Section
<A
>(*this, *_file
, machOSects
[i
].sect
);
2744 space
+= sizeof(Literal16Section
<A
>);
2746 case sectionTypeNonLazy
:
2747 *objects
++ = new (space
) NonLazyPointerSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2748 space
+= sizeof(NonLazyPointerSection
<A
>);
2750 case sectionTypeTLVPointers
:
2751 *objects
++ = new (space
) TLVPointerSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2752 space
+= sizeof(TLVPointerSection
<A
>);
2754 case sectionTypeCFI
:
2755 _EHFrameSection
= new (space
) CFISection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2756 *objects
++ = _EHFrameSection
;
2757 space
+= sizeof(CFISection
<A
>);
2759 case sectionTypeCString
:
2760 *objects
++ = new (space
) CStringSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2761 space
+= sizeof(CStringSection
<A
>);
2763 case sectionTypeCStringPointer
:
2764 *objects
++ = new (space
) PointerToCStringSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2765 space
+= sizeof(PointerToCStringSection
<A
>);
2767 case sectionTypeObjC1ClassRefs
:
2768 *objects
++ = new (space
) Objc1ClassReferences
<A
>(*this, *_file
, machOSects
[i
].sect
);
2769 space
+= sizeof(Objc1ClassReferences
<A
>);
2771 case sectionTypeUTF16Strings
:
2772 *objects
++ = new (space
) UTF16StringSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2773 space
+= sizeof(UTF16StringSection
<A
>);
2775 case sectionTypeCFString
:
2776 *objects
++ = new (space
) CFStringSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2777 space
+= sizeof(CFStringSection
<A
>);
2779 case sectionTypeObjC2ClassRefs
:
2780 *objects
++ = new (space
) ObjC2ClassRefsSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2781 space
+= sizeof(ObjC2ClassRefsSection
<A
>);
2783 case typeObjC2CategoryList
:
2784 *objects
++ = new (space
) ObjC2CategoryListSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2785 space
+= sizeof(ObjC2CategoryListSection
<A
>);
2787 case sectionTypeObjC1Classes
:
2788 *objects
++ = new (space
) ObjC1ClassSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2789 space
+= sizeof(ObjC1ClassSection
<A
>);
2791 case sectionTypeSymboled
:
2792 *objects
++ = new (space
) SymboledSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2793 space
+= sizeof(SymboledSection
<A
>);
2795 case sectionTypeTLVDefs
:
2796 *objects
++ = new (space
) TLVDefsSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2797 space
+= sizeof(TLVDefsSection
<A
>);
2799 case sectionTypeCompactUnwind
:
2800 _compactUnwindSection
= new (space
) CUSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2801 *objects
++ = _compactUnwindSection
;
2802 space
+= sizeof(CUSection
<A
>);
2804 case sectionTypeTentativeDefinitions
:
2805 *objects
++ = new (space
) TentativeDefinitionSection
<A
>(*this, *_file
);
2806 space
+= sizeof(TentativeDefinitionSection
<A
>);
2808 case sectionTypeAbsoluteSymbols
:
2809 _absoluteSection
= new (space
) AbsoluteSymbolSection
<A
>(*this, *_file
);
2810 *objects
++ = _absoluteSection
;
2811 space
+= sizeof(AbsoluteSymbolSection
<A
>);
2814 throw "internal error uknown SectionType";
2820 template <typename A
>
2821 Section
<A
>* Parser
<A
>::sectionForAddress(typename
A::P::uint_t addr
)
2823 for (uint32_t i
=0; i
< _file
->_sectionsArrayCount
; ++i
) {
2824 const macho_section
<typename
A::P
>* sect
= _file
->_sectionsArray
[i
]->machoSection();
2825 // TentativeDefinitionSection and AbsoluteSymbolSection have no mach-o section
2826 if ( sect
!= NULL
) {
2827 if ( (sect
->addr() <= addr
) && (addr
< (sect
->addr()+sect
->size())) ) {
2828 return _file
->_sectionsArray
[i
];
2832 // not strictly in any section
2833 // may be in a zero length section
2834 for (uint32_t i
=0; i
< _file
->_sectionsArrayCount
; ++i
) {
2835 const macho_section
<typename
A::P
>* sect
= _file
->_sectionsArray
[i
]->machoSection();
2836 // TentativeDefinitionSection and AbsoluteSymbolSection have no mach-o section
2837 if ( sect
!= NULL
) {
2838 if ( (sect
->addr() == addr
) && (sect
->size() == 0) ) {
2839 return _file
->_sectionsArray
[i
];
2844 throwf("sectionForAddress(0x%llX) address not in any section", (uint64_t)addr
);
2847 template <typename A
>
2848 Section
<A
>* Parser
<A
>::sectionForNum(unsigned int num
)
2850 for (uint32_t i
=0; i
< _file
->_sectionsArrayCount
; ++i
) {
2851 const macho_section
<typename
A::P
>* sect
= _file
->_sectionsArray
[i
]->machoSection();
2852 // TentativeDefinitionSection and AbsoluteSymbolSection have no mach-o section
2853 if ( sect
!= NULL
) {
2854 if ( num
== (unsigned int)((sect
- _sectionsStart
)+1) )
2855 return _file
->_sectionsArray
[i
];
2858 throwf("sectionForNum(%u) section number not for any section", num
);
2861 template <typename A
>
2862 Atom
<A
>* Parser
<A
>::findAtomByAddress(pint_t addr
)
2864 Section
<A
>* section
= this->sectionForAddress(addr
);
2865 return section
->findAtomByAddress(addr
);
2868 template <typename A
>
2869 Atom
<A
>* Parser
<A
>::findAtomByAddressOrNullIfStub(pint_t addr
)
2871 if ( hasStubsSection() && (_stubsMachOSection
->addr() <= addr
) && (addr
< (_stubsMachOSection
->addr()+_stubsMachOSection
->size())) )
2873 return findAtomByAddress(addr
);
2876 template <typename A
>
2877 Atom
<A
>* Parser
<A
>::findAtomByAddressOrLocalTargetOfStub(pint_t addr
, uint32_t* offsetInAtom
)
2879 if ( hasStubsSection() && (_stubsMachOSection
->addr() <= addr
) && (addr
< (_stubsMachOSection
->addr()+_stubsMachOSection
->size())) ) {
2880 // target is a stub, remove indirection
2881 uint32_t symbolIndex
= this->symbolIndexFromIndirectSectionAddress(addr
, _stubsMachOSection
);
2882 assert(symbolIndex
!= INDIRECT_SYMBOL_LOCAL
);
2883 const macho_nlist
<P
>& sym
= this->symbolFromIndex(symbolIndex
);
2884 // can't be to external weak symbol
2885 assert( (this->combineFromSymbol(sym
) != ld::Atom::combineByName
) || (this->scopeFromSymbol(sym
) != ld::Atom::scopeGlobal
) );
2887 return this->findAtomByName(this->nameFromSymbol(sym
));
2889 Atom
<A
>* target
= this->findAtomByAddress(addr
);
2890 *offsetInAtom
= addr
- target
->_objAddress
;
2894 template <typename A
>
2895 Atom
<A
>* Parser
<A
>::findAtomByName(const char* name
)
2897 uint8_t* p
= _file
->_atomsArray
;
2898 for(int i
=_file
->_atomsArrayCount
; i
> 0; --i
) {
2899 Atom
<A
>* atom
= (Atom
<A
>*)p
;
2900 if ( strcmp(name
, atom
->name()) == 0 )
2902 p
+= sizeof(Atom
<A
>);
2907 template <typename A
>
2908 void Parser
<A
>::findTargetFromAddress(pint_t addr
, TargetDesc
& target
)
2910 if ( hasStubsSection() && (_stubsMachOSection
->addr() <= addr
) && (addr
< (_stubsMachOSection
->addr()+_stubsMachOSection
->size())) ) {
2911 // target is a stub, remove indirection
2912 uint32_t symbolIndex
= this->symbolIndexFromIndirectSectionAddress(addr
, _stubsMachOSection
);
2913 assert(symbolIndex
!= INDIRECT_SYMBOL_LOCAL
);
2914 const macho_nlist
<P
>& sym
= this->symbolFromIndex(symbolIndex
);
2916 target
.name
= this->nameFromSymbol(sym
);
2917 target
.weakImport
= this->weakImportFromSymbol(sym
);
2921 Section
<A
>* section
= this->sectionForAddress(addr
);
2922 target
.atom
= section
->findAtomByAddress(addr
);
2923 target
.addend
= addr
- target
.atom
->_objAddress
;
2924 target
.weakImport
= false;
2928 template <typename A
>
2929 void Parser
<A
>::findTargetFromAddress(pint_t baseAddr
, pint_t addr
, TargetDesc
& target
)
2931 findTargetFromAddress(baseAddr
, target
);
2932 target
.addend
= addr
- target
.atom
->_objAddress
;
2935 template <typename A
>
2936 void Parser
<A
>::findTargetFromAddressAndSectionNum(pint_t addr
, unsigned int sectNum
, TargetDesc
& target
)
2938 if ( sectNum
== R_ABS
) {
2939 // target is absolute symbol that corresponds to addr
2940 if ( _absoluteSection
!= NULL
) {
2941 target
.atom
= _absoluteSection
->findAbsAtomForValue(addr
);
2942 if ( target
.atom
!= NULL
) {
2944 target
.weakImport
= false;
2949 throwf("R_ABS reloc but no absolute symbol at target address");
2952 if ( hasStubsSection() && (stubsSectionNum() == sectNum
) ) {
2953 // target is a stub, remove indirection
2954 uint32_t symbolIndex
= this->symbolIndexFromIndirectSectionAddress(addr
, _stubsMachOSection
);
2955 assert(symbolIndex
!= INDIRECT_SYMBOL_LOCAL
);
2956 const macho_nlist
<P
>& sym
= this->symbolFromIndex(symbolIndex
);
2957 // use direct reference when stub is to a static function
2958 if ( ((sym
.n_type() & N_TYPE
) == N_SECT
) && (((sym
.n_type() & N_EXT
) == 0) || (this->nameFromSymbol(sym
)[0] == 'L')) ) {
2959 this->findTargetFromAddressAndSectionNum(sym
.n_value(), sym
.n_sect(), target
);
2963 target
.name
= this->nameFromSymbol(sym
);
2964 target
.weakImport
= this->weakImportFromSymbol(sym
);
2969 Section
<A
>* section
= this->sectionForNum(sectNum
);
2970 target
.atom
= section
->findAtomByAddress(addr
);
2971 if ( target
.atom
== NULL
) {
2972 typedef typename
A::P::sint_t sint_t
;
2973 sint_t a
= (sint_t
)addr
;
2974 sint_t sectStart
= (sint_t
)(section
->machoSection()->addr());
2975 sint_t sectEnd
= sectStart
+ section
->machoSection()->size();
2976 if ( a
< sectStart
) {
2977 // target address is before start of section, so must be negative addend
2978 target
.atom
= section
->findAtomByAddress(sectStart
);
2979 target
.addend
= a
- sectStart
;
2980 target
.weakImport
= false;
2984 else if ( a
>= sectEnd
) {
2985 target
.atom
= section
->findAtomByAddress(sectEnd
-1);
2986 target
.addend
= a
- sectEnd
;
2987 target
.weakImport
= false;
2992 assert(target
.atom
!= NULL
);
2993 target
.addend
= addr
- target
.atom
->_objAddress
;
2994 target
.weakImport
= false;
2998 template <typename A
>
2999 void Parser
<A
>::addDtraceExtraInfos(const SourceLocation
& src
, const char* providerName
)
3001 // for every ___dtrace_stability$* and ___dtrace_typedefs$* undefine with
3002 // a matching provider name, add a by-name kDtraceTypeReference at probe site
3003 const char* dollar
= strchr(providerName
, '$');
3004 if ( dollar
!= NULL
) {
3005 int providerNameLen
= dollar
-providerName
+1;
3006 for ( std::vector
<const char*>::iterator it
= _dtraceProviderInfo
.begin(); it
!= _dtraceProviderInfo
.end(); ++it
) {
3007 const char* typeDollar
= strchr(*it
, '$');
3008 if ( typeDollar
!= NULL
) {
3009 if ( strncmp(typeDollar
+1, providerName
, providerNameLen
) == 0 ) {
3010 addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindDtraceExtra
,false, *it
);
3017 template <typename A
>
3018 const char* Parser
<A
>::scanSymbolTableForAddress(uint64_t addr
)
3020 uint64_t closestSymAddr
= 0;
3021 const char* closestSymName
= NULL
;
3022 for (uint32_t i
=0; i
< this->_symbolCount
; ++i
) {
3023 const macho_nlist
<P
>& sym
= symbolFromIndex(i
);
3025 if ( (sym
.n_type() & N_STAB
) != 0 )
3028 // only look at definitions
3029 if ( (sym
.n_type() & N_TYPE
) != N_SECT
)
3032 // return with exact match
3033 if ( sym
.n_value() == addr
) {
3034 const char* name
= nameFromSymbol(sym
);
3035 if ( strncmp(name
, "ltmp", 4) != 0 )
3037 // treat 'ltmp*' labels as close match
3038 closestSymAddr
= sym
.n_value();
3039 closestSymName
= name
;
3042 // record closest seen so far
3043 if ( (sym
.n_value() < addr
) && ((sym
.n_value() > closestSymAddr
) || (closestSymName
== NULL
)) )
3044 closestSymName
= nameFromSymbol(sym
);
3047 return (closestSymName
!= NULL
) ? closestSymName
: "unknown";
3051 template <typename A
>
3052 void Parser
<A
>::addFixups(const SourceLocation
& src
, ld::Fixup::Kind setKind
, const TargetDesc
& target
)
3054 // some fixup pairs can be combined
3055 ld::Fixup::Cluster cl
= ld::Fixup::k1of3
;
3056 ld::Fixup::Kind firstKind
= ld::Fixup::kindSetTargetAddress
;
3057 bool combined
= false;
3058 if ( target
.addend
== 0 ) {
3059 cl
= ld::Fixup::k1of1
;
3061 switch ( setKind
) {
3062 case ld::Fixup::kindStoreLittleEndian32
:
3063 firstKind
= ld::Fixup::kindStoreTargetAddressLittleEndian32
;
3065 case ld::Fixup::kindStoreLittleEndian64
:
3066 firstKind
= ld::Fixup::kindStoreTargetAddressLittleEndian64
;
3068 case ld::Fixup::kindStoreBigEndian32
:
3069 firstKind
= ld::Fixup::kindStoreTargetAddressBigEndian32
;
3071 case ld::Fixup::kindStoreBigEndian64
:
3072 firstKind
= ld::Fixup::kindStoreTargetAddressBigEndian64
;
3074 case ld::Fixup::kindStoreX86BranchPCRel32
:
3075 firstKind
= ld::Fixup::kindStoreTargetAddressX86BranchPCRel32
;
3077 case ld::Fixup::kindStoreX86PCRel32
:
3078 firstKind
= ld::Fixup::kindStoreTargetAddressX86PCRel32
;
3080 case ld::Fixup::kindStoreX86PCRel32GOTLoad
:
3081 firstKind
= ld::Fixup::kindStoreTargetAddressX86PCRel32GOTLoad
;
3083 case ld::Fixup::kindStoreX86PCRel32TLVLoad
:
3084 firstKind
= ld::Fixup::kindStoreTargetAddressX86PCRel32TLVLoad
;
3086 case ld::Fixup::kindStoreX86Abs32TLVLoad
:
3087 firstKind
= ld::Fixup::kindStoreTargetAddressX86Abs32TLVLoad
;
3089 case ld::Fixup::kindStoreARMBranch24
:
3090 firstKind
= ld::Fixup::kindStoreTargetAddressARMBranch24
;
3092 case ld::Fixup::kindStoreThumbBranch22
:
3093 firstKind
= ld::Fixup::kindStoreTargetAddressThumbBranch22
;
3095 #if SUPPORT_ARCH_arm64
3096 case ld::Fixup::kindStoreARM64Branch26
:
3097 firstKind
= ld::Fixup::kindStoreTargetAddressARM64Branch26
;
3099 case ld::Fixup::kindStoreARM64Page21
:
3100 firstKind
= ld::Fixup::kindStoreTargetAddressARM64Page21
;
3102 case ld::Fixup::kindStoreARM64PageOff12
:
3103 firstKind
= ld::Fixup::kindStoreTargetAddressARM64PageOff12
;
3105 case ld::Fixup::kindStoreARM64GOTLoadPage21
:
3106 firstKind
= ld::Fixup::kindStoreTargetAddressARM64GOTLoadPage21
;
3108 case ld::Fixup::kindStoreARM64GOTLoadPageOff12
:
3109 firstKind
= ld::Fixup::kindStoreTargetAddressARM64GOTLoadPageOff12
;
3111 case ld::Fixup::kindStoreARM64TLVPLoadPage21
:
3112 firstKind
= ld::Fixup::kindStoreTargetAddressARM64TLVPLoadPage21
;
3114 case ld::Fixup::kindStoreARM64TLVPLoadPageOff12
:
3115 firstKind
= ld::Fixup::kindStoreTargetAddressARM64TLVPLoadPageOff12
;
3120 cl
= ld::Fixup::k1of2
;
3125 if ( target
.atom
!= NULL
) {
3126 if ( target
.atom
->scope() == ld::Atom::scopeTranslationUnit
) {
3127 addFixup(src
, cl
, firstKind
, target
.atom
);
3129 else if ( (target
.atom
->combine() == ld::Atom::combineByNameAndContent
) || (target
.atom
->combine() == ld::Atom::combineByNameAndReferences
) ) {
3130 addFixup(src
, cl
, firstKind
, ld::Fixup::bindingByContentBound
, target
.atom
);
3132 else if ( (src
.atom
->section().type() == ld::Section::typeCFString
) && (src
.offsetInAtom
!= 0) ) {
3133 // backing string in CFStrings should always be direct
3134 addFixup(src
, cl
, firstKind
, target
.atom
);
3136 else if ( (src
.atom
== target
.atom
) && (target
.atom
->combine() == ld::Atom::combineByName
) ) {
3137 // reference to self should always be direct
3138 addFixup(src
, cl
, firstKind
, target
.atom
);
3141 // change direct fixup to by-name fixup
3142 addFixup(src
, cl
, firstKind
, false, target
.atom
->name());
3146 addFixup(src
, cl
, firstKind
, target
.weakImport
, target
.name
);
3148 if ( target
.addend
== 0 ) {
3150 addFixup(src
, ld::Fixup::k2of2
, setKind
);
3153 addFixup(src
, ld::Fixup::k2of3
, ld::Fixup::kindAddAddend
, target
.addend
);
3154 addFixup(src
, ld::Fixup::k3of3
, setKind
);
3158 template <typename A
>
3159 void Parser
<A
>::addFixups(const SourceLocation
& src
, ld::Fixup::Kind kind
, const TargetDesc
& target
, const TargetDesc
& picBase
)
3161 ld::Fixup::Cluster cl
= (target
.addend
== 0) ? ld::Fixup::k1of4
: ld::Fixup::k1of5
;
3162 if ( target
.atom
!= NULL
) {
3163 if ( target
.atom
->scope() == ld::Atom::scopeTranslationUnit
) {
3164 addFixup(src
, cl
, ld::Fixup::kindSetTargetAddress
, target
.atom
);
3166 else if ( (target
.atom
->combine() == ld::Atom::combineByNameAndContent
) || (target
.atom
->combine() == ld::Atom::combineByNameAndReferences
) ) {
3167 addFixup(src
, cl
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, target
.atom
);
3170 addFixup(src
, cl
, ld::Fixup::kindSetTargetAddress
, false, target
.atom
->name());
3174 addFixup(src
, cl
, ld::Fixup::kindSetTargetAddress
, target
.weakImport
, target
.name
);
3176 if ( target
.addend
== 0 ) {
3177 assert(picBase
.atom
!= NULL
);
3178 addFixup(src
, ld::Fixup::k2of4
, ld::Fixup::kindSubtractTargetAddress
, picBase
.atom
);
3179 addFixup(src
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractAddend
, picBase
.addend
);
3180 addFixup(src
, ld::Fixup::k4of4
, kind
);
3183 addFixup(src
, ld::Fixup::k2of5
, ld::Fixup::kindAddAddend
, target
.addend
);
3184 addFixup(src
, ld::Fixup::k3of5
, ld::Fixup::kindSubtractTargetAddress
, picBase
.atom
);
3185 addFixup(src
, ld::Fixup::k4of5
, ld::Fixup::kindSubtractAddend
, picBase
.addend
);
3186 addFixup(src
, ld::Fixup::k5of5
, kind
);
3192 template <typename A
>
3193 uint32_t TentativeDefinitionSection
<A
>::computeAtomCount(class Parser
<A
>& parser
,
3194 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
3195 const struct Parser
<A
>::CFI_CU_InfoArrays
&)
3197 return parser
.tentativeDefinitionCount();
3200 template <typename A
>
3201 uint32_t TentativeDefinitionSection
<A
>::appendAtoms(class Parser
<A
>& parser
, uint8_t* p
,
3202 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
3203 const struct Parser
<A
>::CFI_CU_InfoArrays
&)
3205 this->_beginAtoms
= (Atom
<A
>*)p
;
3207 for (uint32_t i
=parser
.undefinedStartIndex(); i
< parser
.undefinedEndIndex(); ++i
) {
3208 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(i
);
3209 if ( ((sym
.n_type() & N_TYPE
) == N_UNDF
) && (sym
.n_value() != 0) ) {
3210 uint64_t size
= sym
.n_value();
3211 uint8_t alignP2
= GET_COMM_ALIGN(sym
.n_desc());
3212 if ( alignP2
== 0 ) {
3213 // common symbols align to their size
3214 // that is, a 4-byte common aligns to 4-bytes
3215 // if this size is not a power of two,
3216 // then round up to the next power of two
3217 alignP2
= 63 - (uint8_t)__builtin_clzll(size
);
3218 if ( size
!= (1ULL << alignP2
) )
3220 // <rdar://problem/24871389> limit default alignment of large commons
3221 if ( alignP2
> parser
.maxDefaultCommonAlignment() )
3222 alignP2
= parser
.maxDefaultCommonAlignment();
3224 Atom
<A
>* allocatedSpace
= (Atom
<A
>*)p
;
3225 new (allocatedSpace
) Atom
<A
>(*this, parser
.nameFromSymbol(sym
), (pint_t
)ULLONG_MAX
, size
,
3226 ld::Atom::definitionTentative
, ld::Atom::combineByName
,
3227 parser
.scopeFromSymbol(sym
), ld::Atom::typeZeroFill
, ld::Atom::symbolTableIn
,
3228 parser
.dontDeadStripFromSymbol(sym
), false, false, ld::Atom::Alignment(alignP2
) );
3229 p
+= sizeof(Atom
<A
>);
3233 this->_endAtoms
= (Atom
<A
>*)p
;
3238 template <typename A
>
3239 uint32_t AbsoluteSymbolSection
<A
>::computeAtomCount(class Parser
<A
>& parser
,
3240 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
3241 const struct Parser
<A
>::CFI_CU_InfoArrays
&)
3243 return parser
.absoluteSymbolCount();
3246 template <typename A
>
3247 uint32_t AbsoluteSymbolSection
<A
>::appendAtoms(class Parser
<A
>& parser
, uint8_t* p
,
3248 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
3249 const struct Parser
<A
>::CFI_CU_InfoArrays
&)
3251 this->_beginAtoms
= (Atom
<A
>*)p
;
3253 for (uint32_t i
=0; i
< parser
.symbolCount(); ++i
) {
3254 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(i
);
3255 if ( (sym
.n_type() & N_TYPE
) != N_ABS
)
3257 const char* absName
= parser
.nameFromSymbol(sym
);
3258 // ignore .objc_class_name_* symbols
3259 if ( strncmp(absName
, ".objc_class_name_", 17) == 0 )
3261 // ignore .objc_class_name_* symbols
3262 if ( strncmp(absName
, ".objc_category_name_", 20) == 0 )
3264 // ignore empty *.eh symbols
3265 if ( strcmp(&absName
[strlen(absName
)-3], ".eh") == 0 )
3268 Atom
<A
>* allocatedSpace
= (Atom
<A
>*)p
;
3269 new (allocatedSpace
) Atom
<A
>(*this, parser
, sym
, 0);
3270 p
+= sizeof(Atom
<A
>);
3273 this->_endAtoms
= (Atom
<A
>*)p
;
3277 template <typename A
>
3278 Atom
<A
>* AbsoluteSymbolSection
<A
>::findAbsAtomForValue(typename
A::P::uint_t value
)
3280 Atom
<A
>* end
= this->_endAtoms
;
3281 for(Atom
<A
>* p
= this->_beginAtoms
; p
< end
; ++p
) {
3282 if ( p
->_objAddress
== value
)
3289 template <typename A
>
3290 uint32_t Parser
<A
>::indirectSymbol(uint32_t indirectIndex
)
3292 if ( indirectIndex
>= _indirectTableCount
)
3293 throw "indirect symbol index out of range";
3294 return E::get32(_indirectTable
[indirectIndex
]);
3297 template <typename A
>
3298 const macho_nlist
<typename
A::P
>& Parser
<A
>::symbolFromIndex(uint32_t index
)
3300 if ( index
> _symbolCount
)
3301 throw "symbol index out of range";
3302 return _symbols
[index
];
3305 template <typename A
>
3306 const macho_section
<typename
A::P
>* Parser
<A
>::machOSectionFromSectionIndex(uint32_t index
)
3308 if ( index
>= _machOSectionsCount
)
3309 throw "section index out of range";
3310 return &_sectionsStart
[index
];
3313 template <typename A
>
3314 uint32_t Parser
<A
>::symbolIndexFromIndirectSectionAddress(pint_t addr
, const macho_section
<P
>* sect
)
3316 uint32_t elementSize
= 0;
3317 switch ( sect
->flags() & SECTION_TYPE
) {
3318 case S_SYMBOL_STUBS
:
3319 elementSize
= sect
->reserved2();
3321 case S_LAZY_SYMBOL_POINTERS
:
3322 case S_NON_LAZY_SYMBOL_POINTERS
:
3323 case S_THREAD_LOCAL_VARIABLE_POINTERS
:
3324 elementSize
= sizeof(pint_t
);
3327 throw "section does not use indirect symbol table";
3329 uint32_t indexInSection
= (addr
- sect
->addr()) / elementSize
;
3330 uint32_t indexIntoIndirectTable
= sect
->reserved1() + indexInSection
;
3331 return this->indirectSymbol(indexIntoIndirectTable
);
3336 template <typename A
>
3337 const char* Parser
<A
>::nameFromSymbol(const macho_nlist
<P
>& sym
)
3339 return &_strings
[sym
.n_strx()];
3342 template <typename A
>
3343 ld::Atom::Scope Parser
<A
>::scopeFromSymbol(const macho_nlist
<P
>& sym
)
3345 if ( (sym
.n_type() & N_EXT
) == 0 )
3346 return ld::Atom::scopeTranslationUnit
;
3347 else if ( (sym
.n_type() & N_PEXT
) != 0 )
3348 return ld::Atom::scopeLinkageUnit
;
3349 else if ( this->nameFromSymbol(sym
)[0] == 'l' ) // since all 'l' symbols will be remove, don't make them global
3350 return ld::Atom::scopeLinkageUnit
;
3352 return ld::Atom::scopeGlobal
;
3355 template <typename A
>
3356 ld::Atom::Definition Parser
<A
>::definitionFromSymbol(const macho_nlist
<P
>& sym
)
3358 switch ( sym
.n_type() & N_TYPE
) {
3360 return ld::Atom::definitionAbsolute
;
3362 return ld::Atom::definitionRegular
;
3364 if ( sym
.n_value() != 0 )
3365 return ld::Atom::definitionTentative
;
3367 throw "definitionFromSymbol() bad symbol";
3370 template <typename A
>
3371 ld::Atom::Combine Parser
<A
>::combineFromSymbol(const macho_nlist
<P
>& sym
)
3373 if ( sym
.n_desc() & N_WEAK_DEF
)
3374 return ld::Atom::combineByName
;
3376 return ld::Atom::combineNever
;
3380 template <typename A
>
3381 ld::Atom::SymbolTableInclusion Parser
<A
>::inclusionFromSymbol(const macho_nlist
<P
>& sym
)
3383 const char* symbolName
= nameFromSymbol(sym
);
3384 // labels beginning with 'l' (lowercase ell) are automatically removed in final linked images <rdar://problem/4571042>
3385 // labels beginning with 'L' should have been stripped by the assembler, so are stripped now
3386 if ( sym
.n_desc() & REFERENCED_DYNAMICALLY
)
3387 return ld::Atom::symbolTableInAndNeverStrip
;
3388 else if ( symbolName
[0] == 'l' )
3389 return ld::Atom::symbolTableNotInFinalLinkedImages
;
3390 else if ( symbolName
[0] == 'L' )
3391 return ld::Atom::symbolTableNotIn
;
3393 return ld::Atom::symbolTableIn
;
3396 template <typename A
>
3397 bool Parser
<A
>::dontDeadStripFromSymbol(const macho_nlist
<P
>& sym
)
3399 return ( (sym
.n_desc() & (N_NO_DEAD_STRIP
|REFERENCED_DYNAMICALLY
)) != 0 );
3402 template <typename A
>
3403 bool Parser
<A
>::isThumbFromSymbol(const macho_nlist
<P
>& sym
)
3405 return ( sym
.n_desc() & N_ARM_THUMB_DEF
);
3408 template <typename A
>
3409 bool Parser
<A
>::weakImportFromSymbol(const macho_nlist
<P
>& sym
)
3411 return ( ((sym
.n_type() & N_TYPE
) == N_UNDF
) && ((sym
.n_desc() & N_WEAK_REF
) != 0) );
3414 template <typename A
>
3415 bool Parser
<A
>::resolverFromSymbol(const macho_nlist
<P
>& sym
)
3417 return ( sym
.n_desc() & N_SYMBOL_RESOLVER
);
3420 template <typename A
>
3421 bool Parser
<A
>::altEntryFromSymbol(const macho_nlist
<P
>& sym
)
3423 return ( sym
.n_desc() & N_ALT_ENTRY
);
3427 /* Skip over a LEB128 value (signed or unsigned). */
3429 skip_leb128 (const uint8_t ** offset
, const uint8_t * end
)
3431 while (*offset
!= end
&& **offset
>= 0x80)
3437 /* Read a ULEB128 into a 64-bit word. Return (uint64_t)-1 on overflow
3438 or error. On overflow, skip past the rest of the uleb128. */
3440 read_uleb128 (const uint8_t ** offset
, const uint8_t * end
)
3442 uint64_t result
= 0;
3449 return (uint64_t) -1;
3451 b
= **offset
& 0x7f;
3453 if (bit
>= 64 || b
<< bit
>> bit
!= b
)
3454 result
= (uint64_t) -1;
3456 result
|= b
<< bit
, bit
+= 7;
3457 } while (*(*offset
)++ >= 0x80);
3462 /* Skip over a DWARF attribute of form FORM. */
3463 template <typename A
>
3464 bool Parser
<A
>::skip_form(const uint8_t ** offset
, const uint8_t * end
, uint64_t form
,
3465 uint8_t addr_size
, bool dwarf64
)
3475 case DW_FORM_block2
:
3476 if (end
- *offset
< 2)
3478 sz
= 2 + A::P::E::get16(*(uint16_t*)offset
);
3481 case DW_FORM_block4
:
3482 if (end
- *offset
< 4)
3484 sz
= 2 + A::P::E::get32(*(uint32_t*)offset
);
3502 case DW_FORM_string
:
3503 while (*offset
!= end
&& **offset
)
3512 sz
= read_uleb128 (offset
, end
);
3515 case DW_FORM_block1
:
3523 case DW_FORM_ref_udata
:
3524 skip_leb128 (offset
, end
);
3528 case DW_FORM_ref_addr
:
3532 case DW_FORM_sec_offset
:
3533 sz
= sizeof(typename
A::P::uint_t
);
3536 case DW_FORM_exprloc
:
3537 sz
= read_uleb128 (offset
, end
);
3540 case DW_FORM_flag_present
:
3544 case DW_FORM_ref_sig8
:
3551 if (end
- *offset
< sz
)
3558 template <typename A
>
3559 const char* Parser
<A
>::getDwarfString(uint64_t form
, const uint8_t*& di
)
3562 const char* dwarfStrings
;
3563 const char* result
= NULL
;
3565 case DW_FORM_string
:
3566 result
= (const char*)di
;
3567 di
+= strlen(result
) + 1;
3570 offset
= E::get32(*((uint32_t*)di
));
3571 dwarfStrings
= (char*)_file
->fileContent() + _file
->_dwarfDebugStringSect
->offset();
3572 if ( offset
< _file
->_dwarfDebugStringSect
->size() )
3573 result
= &dwarfStrings
[offset
];
3575 warning("dwarf DW_FORM_strp (offset=0x%08X) is too big in %s", offset
, this->_path
);
3579 warning("unknown dwarf string encoding (form=%lld) in %s", form
, this->_path
);
3585 template <typename A
>
3586 uint64_t Parser
<A
>::getDwarfOffset(uint64_t form
, const uint8_t*& di
, bool dwarf64
)
3588 if ( form
== DW_FORM_sec_offset
)
3589 form
= (dwarf64
? DW_FORM_data8
: DW_FORM_data4
);
3590 uint64_t result
= -1;
3593 result
= A::P::E::get32(*(uint32_t*)di
);
3597 result
= A::P::E::get64(*(uint64_t*)di
);
3601 warning("unknown dwarf DW_FORM_ for DW_AT_stmt_list in %s", this->_path
);
3607 template <typename A
>
3608 struct AtomAndLineInfo
{
3610 ld::Atom::LineInfo info
;
3614 // <rdar://problem/5591394> Add support to ld64 for N_FUN stabs when used for symbolic constants
3615 // Returns whether a stabStr belonging to an N_FUN stab represents a
3616 // symbolic constant rather than a function
3617 template <typename A
>
3618 bool Parser
<A
>::isConstFunStabs(const char *stabStr
)
3621 // N_FUN can be used for both constants and for functions. In case it's a constant,
3622 // the format of the stabs string is "symname:c=<value>;"
3623 // ':' cannot appear in the symbol name, except if it's an Objective-C method
3624 // (in which case the symbol name starts with + or -, and then it's definitely
3626 return (stabStr
!= NULL
) && (stabStr
[0] != '+') && (stabStr
[0] != '-')
3627 && ((colon
= strchr(stabStr
, ':')) != NULL
)
3628 && (colon
[1] == 'c') && (colon
[2] == '=');
3632 template <typename A
>
3633 void Parser
<A
>::parseDebugInfo()
3637 // check for dwarf __debug_info section
3638 if ( _file
->_dwarfDebugInfoSect
== NULL
) {
3639 // if no DWARF debug info, look for stabs
3643 if ( _file
->_dwarfDebugInfoSect
->size() == 0 )
3649 if ( !read_comp_unit(&tuName
, &tuDir
, &stmtList
) ) {
3650 // if can't parse dwarf, warn and give up
3651 _file
->_dwarfTranslationUnitPath
= NULL
;
3652 warning("can't parse dwarf compilation unit info in %s", _path
);
3653 _file
->_debugInfoKind
= ld::relocatable::File::kDebugInfoNone
;
3656 if ( (tuName
!= NULL
) && (tuName
[0] == '/') ) {
3657 _file
->_dwarfTranslationUnitPath
= tuName
;
3659 else if ( (tuDir
!= NULL
) && (tuName
!= NULL
) ) {
3660 asprintf((char**)&(_file
->_dwarfTranslationUnitPath
), "%s/%s", tuDir
, tuName
);
3662 else if ( tuDir
== NULL
) {
3663 _file
->_dwarfTranslationUnitPath
= tuName
;
3666 _file
->_dwarfTranslationUnitPath
= NULL
;
3669 // add line number info to atoms from dwarf
3670 std::vector
<AtomAndLineInfo
<A
> > entries
;
3671 entries
.reserve(64);
3672 if ( _file
->_debugInfoKind
== ld::relocatable::File::kDebugInfoDwarf
) {
3673 // file with just data will have no __debug_line info
3674 if ( (_file
->_dwarfDebugLineSect
!= NULL
) && (_file
->_dwarfDebugLineSect
->size() != 0) ) {
3675 // validate stmt_list
3676 if ( (stmtList
!= (uint64_t)-1) && (stmtList
< _file
->_dwarfDebugLineSect
->size()) ) {
3677 const uint8_t* debug_line
= (uint8_t*)_file
->fileContent() + _file
->_dwarfDebugLineSect
->offset();
3678 struct line_reader_data
* lines
= line_open(&debug_line
[stmtList
],
3679 _file
->_dwarfDebugLineSect
->size() - stmtList
, E::little_endian
);
3680 struct line_info result
;
3681 Atom
<A
>* curAtom
= NULL
;
3682 uint32_t curAtomOffset
= 0;
3683 uint32_t curAtomAddress
= 0;
3684 uint32_t curAtomSize
= 0;
3685 std::map
<uint32_t,const char*> dwarfIndexToFile
;
3686 if ( lines
!= NULL
) {
3687 while ( line_next(lines
, &result
, line_stop_pc
) ) {
3688 //fprintf(stderr, "curAtom=%p, result.pc=0x%llX, result.line=%llu, result.end_of_sequence=%d,"
3689 // " curAtomAddress=0x%X, curAtomSize=0x%X\n",
3690 // curAtom, result.pc, result.line, result.end_of_sequence, curAtomAddress, curAtomSize);
3691 // work around weird debug line table compiler generates if no functions in __text section
3692 if ( (curAtom
== NULL
) && (result
.pc
== 0) && result
.end_of_sequence
&& (result
.file
== 1))
3694 // for performance, see if in next pc is in current atom
3695 if ( (curAtom
!= NULL
) && (curAtomAddress
<= result
.pc
) && (result
.pc
< (curAtomAddress
+curAtomSize
)) ) {
3696 curAtomOffset
= result
.pc
- curAtomAddress
;
3698 // or pc at end of current atom
3699 else if ( result
.end_of_sequence
&& (curAtom
!= NULL
) && (result
.pc
== (curAtomAddress
+curAtomSize
)) ) {
3700 curAtomOffset
= result
.pc
- curAtomAddress
;
3702 // or only one function that is a one line function
3703 else if ( result
.end_of_sequence
&& (curAtom
== NULL
) && (this->findAtomByAddress(0) != NULL
) && (result
.pc
== this->findAtomByAddress(0)->size()) ) {
3704 curAtom
= this->findAtomByAddress(0);
3705 curAtomOffset
= result
.pc
- curAtom
->objectAddress();
3706 curAtomAddress
= curAtom
->objectAddress();
3707 curAtomSize
= curAtom
->size();
3710 // do slow look up of atom by address
3712 curAtom
= this->findAtomByAddress(result
.pc
);
3715 // in case of bug in debug info, don't abort link, just limp on
3718 if ( curAtom
== NULL
)
3719 break; // file has line info but no functions
3720 if ( result
.end_of_sequence
&& (curAtomAddress
+curAtomSize
< result
.pc
) ) {
3721 // a one line function can be returned by line_next() as one entry with pc at end of blob
3722 // look for alt atom starting at end of previous atom
3723 uint32_t previousEnd
= curAtomAddress
+curAtomSize
;
3724 Atom
<A
>* alt
= this->findAtomByAddressOrNullIfStub(previousEnd
);
3726 continue; // ignore spurious debug info for stubs
3727 if ( result
.pc
<= alt
->objectAddress() + alt
->size() ) {
3729 curAtomOffset
= result
.pc
- alt
->objectAddress();
3730 curAtomAddress
= alt
->objectAddress();
3731 curAtomSize
= alt
->size();
3734 curAtomOffset
= result
.pc
- curAtom
->objectAddress();
3735 curAtomAddress
= curAtom
->objectAddress();
3736 curAtomSize
= curAtom
->size();
3740 curAtomOffset
= result
.pc
- curAtom
->objectAddress();
3741 curAtomAddress
= curAtom
->objectAddress();
3742 curAtomSize
= curAtom
->size();
3745 const char* filename
;
3746 std::map
<uint32_t,const char*>::iterator pos
= dwarfIndexToFile
.find(result
.file
);
3747 if ( pos
== dwarfIndexToFile
.end() ) {
3748 filename
= line_file(lines
, result
.file
);
3749 dwarfIndexToFile
[result
.file
] = filename
;
3752 filename
= pos
->second
;
3754 // only record for ~8000 line info records per function
3755 if ( curAtom
->roomForMoreLineInfoCount() ) {
3756 AtomAndLineInfo
<A
> entry
;
3757 entry
.atom
= curAtom
;
3758 entry
.info
.atomOffset
= curAtomOffset
;
3759 entry
.info
.fileName
= filename
;
3760 entry
.info
.lineNumber
= result
.line
;
3761 //fprintf(stderr, "addr=0x%08llX, line=%lld, file=%s, atom=%s, atom.size=0x%X, end=%d\n",
3762 // result.pc, result.line, filename, curAtom->name(), curAtomSize, result.end_of_sequence);
3763 entries
.push_back(entry
);
3764 curAtom
->incrementLineInfoCount();
3766 if ( result
.end_of_sequence
) {
3776 // assign line info start offset for each atom
3777 uint8_t* p
= _file
->_atomsArray
;
3778 uint32_t liOffset
= 0;
3779 for(int i
=_file
->_atomsArrayCount
; i
> 0; --i
) {
3780 Atom
<A
>* atom
= (Atom
<A
>*)p
;
3781 atom
->_lineInfoStartIndex
= liOffset
;
3782 liOffset
+= atom
->_lineInfoCount
;
3783 atom
->_lineInfoCount
= 0;
3784 p
+= sizeof(Atom
<A
>);
3786 assert(liOffset
== entries
.size());
3787 _file
->_lineInfos
.resize(liOffset
);
3789 // copy each line info for each atom
3790 for (typename
std::vector
<AtomAndLineInfo
<A
> >::iterator it
= entries
.begin(); it
!= entries
.end(); ++it
) {
3791 uint32_t slot
= it
->atom
->_lineInfoStartIndex
+ it
->atom
->_lineInfoCount
;
3792 _file
->_lineInfos
[slot
] = it
->info
;
3793 it
->atom
->_lineInfoCount
++;
3796 // done with temp vector
3800 template <typename A
>
3801 void Parser
<A
>::parseStabs()
3803 // scan symbol table for stabs entries
3804 Atom
<A
>* currentAtom
= NULL
;
3805 pint_t currentAtomAddress
= 0;
3806 enum { start
, inBeginEnd
, inFun
} state
= start
;
3807 for (uint32_t symbolIndex
= 0; symbolIndex
< _symbolCount
; ++symbolIndex
) {
3808 const macho_nlist
<P
>& sym
= this->symbolFromIndex(symbolIndex
);
3809 bool useStab
= true;
3810 uint8_t type
= sym
.n_type();
3811 const char* symString
= (sym
.n_strx() != 0) ? this->nameFromSymbol(sym
) : NULL
;
3812 if ( (type
& N_STAB
) != 0 ) {
3813 _file
->_debugInfoKind
= (_hasUUID
? ld::relocatable::File::kDebugInfoStabsUUID
: ld::relocatable::File::kDebugInfoStabs
);
3814 ld::relocatable::File::Stab stab
;
3817 stab
.other
= sym
.n_sect();
3818 stab
.desc
= sym
.n_desc();
3819 stab
.value
= sym
.n_value();
3825 // beginning of function block
3827 // fall into case to lookup atom by addresss
3830 currentAtomAddress
= sym
.n_value();
3831 currentAtom
= this->findAtomByAddress(currentAtomAddress
);
3832 if ( currentAtom
!= NULL
) {
3833 stab
.atom
= currentAtom
;
3834 stab
.string
= symString
;
3837 fprintf(stderr
, "can't find atom for stabs BNSYM at %08llX in %s",
3838 (uint64_t)sym
.n_value(), _path
);
3848 // not associated with an atom, just copy
3849 stab
.string
= symString
;
3853 // n_value field is NOT atom address ;-(
3854 // need to find atom by name match
3855 const char* colon
= strchr(symString
, ':');
3856 if ( colon
!= NULL
) {
3857 // build underscore leading name
3858 int nameLen
= colon
- symString
;
3859 char symName
[nameLen
+2];
3860 strlcpy(&symName
[1], symString
, nameLen
+1);
3862 symName
[nameLen
+1] = '\0';
3863 currentAtom
= this->findAtomByName(symName
);
3864 if ( currentAtom
!= NULL
) {
3865 stab
.atom
= currentAtom
;
3866 stab
.string
= symString
;
3870 // might be a debug-note without trailing :G()
3871 currentAtom
= this->findAtomByName(symString
);
3872 if ( currentAtom
!= NULL
) {
3873 stab
.atom
= currentAtom
;
3874 stab
.string
= symString
;
3877 if ( stab
.atom
== NULL
) {
3878 // ld_classic added bogus GSYM stabs for old style dtrace probes
3879 if ( (strncmp(symString
, "__dtrace_probe$", 15) != 0) )
3880 warning("can't find atom for N_GSYM stabs %s in %s", symString
, _path
);
3886 if ( isConstFunStabs(symString
) ) {
3887 // constant not associated with a function
3888 stab
.string
= symString
;
3891 // old style stabs without BNSYM
3893 currentAtomAddress
= sym
.n_value();
3894 currentAtom
= this->findAtomByAddress(currentAtomAddress
);
3895 if ( currentAtom
!= NULL
) {
3896 stab
.atom
= currentAtom
;
3897 stab
.string
= symString
;
3900 warning("can't find atom for stabs FUN at %08llX in %s",
3901 (uint64_t)currentAtomAddress
, _path
);
3907 stab
.string
= symString
;
3913 stab
.string
= symString
;
3914 // -gfull built .o file
3917 warning("unknown stabs type 0x%X in %s", type
, _path
);
3921 stab
.atom
= currentAtom
;
3930 Atom
<A
>* nestedAtom
= this->findAtomByAddress(sym
.n_value());
3931 if ( nestedAtom
!= NULL
) {
3932 stab
.atom
= nestedAtom
;
3933 stab
.string
= symString
;
3936 warning("can't find atom for stabs 0x%X at %08llX in %s",
3937 type
, (uint64_t)sym
.n_value(), _path
);
3944 // adjust value to be offset in atom
3945 stab
.value
-= currentAtomAddress
;
3947 stab
.string
= symString
;
3954 if ( isConstFunStabs(symString
) ) {
3955 stab
.atom
= currentAtom
;
3956 stab
.string
= symString
;
3959 if ( sym
.n_sect() != 0 ) {
3960 // found another start stab, must be really old stabs...
3961 currentAtomAddress
= sym
.n_value();
3962 currentAtom
= this->findAtomByAddress(currentAtomAddress
);
3963 if ( currentAtom
!= NULL
) {
3964 stab
.atom
= currentAtom
;
3965 stab
.string
= symString
;
3968 warning("can't find atom for stabs FUN at %08llX in %s",
3969 (uint64_t)currentAtomAddress
, _path
);
3973 // found ending stab, switch back to start state
3974 stab
.string
= symString
;
3975 stab
.atom
= currentAtom
;
3984 // adjust value to be offset in atom
3985 stab
.value
-= currentAtomAddress
;
3986 stab
.atom
= currentAtom
;
3989 stab
.string
= symString
;
3993 stab
.atom
= currentAtom
;
3994 stab
.string
= symString
;
3999 // add to list of stabs for this .o file
4001 _file
->_stabs
.push_back(stab
);
4007 template <typename A
>
4008 void Parser
<A
>::addAstFiles()
4010 // scan symbol table for N_AST entries
4011 for (uint32_t symbolIndex
= 0; symbolIndex
< _symbolCount
; ++symbolIndex
) {
4012 const macho_nlist
<P
>& sym
= this->symbolFromIndex(symbolIndex
);
4013 if ( (sym
.n_type() == N_AST
) && (sym
.n_strx() != 0) ) {
4014 const char* symString
= this->nameFromSymbol(sym
);
4015 ld::relocatable::File::AstTimeAndPath entry
;
4016 entry
.time
= sym
.n_value();
4017 entry
.path
= symString
;
4018 _file
->_astFiles
.push_back(entry
);
4024 // Look at the compilation unit DIE and determine
4025 // its NAME, compilation directory (in COMP_DIR) and its
4026 // line number information offset (in STMT_LIST). NAME and COMP_DIR
4027 // may be NULL (especially COMP_DIR) if they are not in the .o file;
4028 // STMT_LIST will be (uint64_t) -1.
4030 // At present this assumes that there's only one compilation unit DIE.
4032 template <typename A
>
4033 bool Parser
<A
>::read_comp_unit(const char ** name
, const char ** comp_dir
,
4034 uint64_t *stmt_list
)
4036 const uint8_t * debug_info
;
4037 const uint8_t * debug_abbrev
;
4039 const uint8_t * next_cu
;
4041 const uint8_t * end
;
4042 const uint8_t * enda
;
4045 uint64_t abbrev_base
;
4047 uint8_t address_size
;
4052 *stmt_list
= (uint64_t) -1;
4054 if ( (_file
->_dwarfDebugInfoSect
== NULL
) || (_file
->_dwarfDebugAbbrevSect
== NULL
) )
4057 if (_file
->_dwarfDebugInfoSect
->size() < 12)
4058 /* Too small to be a real debug_info section. */
4061 debug_info
= (uint8_t*)_file
->fileContent() + _file
->_dwarfDebugInfoSect
->offset();
4062 debug_abbrev
= (uint8_t*)_file
->fileContent() + _file
->_dwarfDebugAbbrevSect
->offset();
4063 next_cu
= debug_info
;
4065 while ((uint64_t)(next_cu
- debug_info
) < _file
->_dwarfDebugInfoSect
->size()) {
4067 sz
= A::P::E::get32(*(uint32_t*)di
);
4069 dwarf64
= sz
== 0xffffffff;
4071 sz
= A::P::E::get64(*(uint64_t*)di
), di
+= 8;
4072 else if (sz
> 0xffffff00)
4073 /* Unknown dwarf format. */
4076 /* Verify claimed size. */
4077 if (sz
+ (di
- debug_info
) > _file
->_dwarfDebugInfoSect
->size() || sz
<= (dwarf64
? 23 : 11))
4082 vers
= A::P::E::get16(*(uint16_t*)di
);
4083 if (vers
< 2 || vers
> 4)
4084 /* DWARF version wrong for this code.
4085 Chances are we could continue anyway, but we don't know for sure. */
4089 /* Find the debug_abbrev section. */
4090 abbrev_base
= dwarf64
? A::P::E::get64(*(uint64_t*)di
) : A::P::E::get32(*(uint32_t*)di
);
4091 di
+= dwarf64
? 8 : 4;
4093 if (abbrev_base
> _file
->_dwarfDebugAbbrevSect
->size())
4095 da
= debug_abbrev
+ abbrev_base
;
4096 enda
= debug_abbrev
+ _file
->_dwarfDebugAbbrevSect
->size();
4098 address_size
= *di
++;
4100 /* Find the abbrev number we're looking for. */
4102 abbrev
= read_uleb128 (&di
, end
);
4103 if (abbrev
== (uint64_t) -1)
4106 /* Skip through the debug_abbrev section looking for that abbrev. */
4109 uint64_t this_abbrev
= read_uleb128 (&da
, enda
);
4112 if (this_abbrev
== abbrev
)
4113 /* This is almost always taken. */
4115 skip_leb128 (&da
, enda
); /* Skip the tag. */
4118 da
++; /* Skip the DW_CHILDREN_* value. */
4121 attr
= read_uleb128 (&da
, enda
);
4122 skip_leb128 (&da
, enda
);
4123 } while (attr
!= 0 && attr
!= (uint64_t) -1);
4128 /* Check that the abbrev is one for a DW_TAG_compile_unit. */
4129 if (read_uleb128 (&da
, enda
) != DW_TAG_compile_unit
)
4133 da
++; /* Skip the DW_CHILDREN_* value. */
4135 /* Now, go through the DIE looking for DW_AT_name,
4136 DW_AT_comp_dir, and DW_AT_stmt_list. */
4137 bool skip_to_next_cu
= false;
4138 while (!skip_to_next_cu
) {
4140 uint64_t attr
= read_uleb128 (&da
, enda
);
4141 uint64_t form
= read_uleb128 (&da
, enda
);
4143 if (attr
== (uint64_t) -1)
4147 if (form
== DW_FORM_indirect
)
4148 form
= read_uleb128 (&di
, end
);
4152 *name
= getDwarfString(form
, di
);
4153 /* Swift object files may contain two CUs: One
4154 describes the Swift code, one is created by the
4155 clang importer. Skip over the CU created by the
4156 clang importer as it may be empty. */
4157 if (std::string(*name
) == "<swift-imported-modules>")
4158 skip_to_next_cu
= true;
4160 case DW_AT_comp_dir
:
4161 *comp_dir
= getDwarfString(form
, di
);
4163 case DW_AT_stmt_list
:
4164 *stmt_list
= getDwarfOffset(form
, di
, dwarf64
);
4167 if (! skip_form (&di
, end
, form
, address_size
, dwarf64
))
4177 template <typename A
>
4180 free(_sectionsArray
);
4184 template <typename A
>
4185 const char* File
<A
>::translationUnitSource() const
4187 return _dwarfTranslationUnitPath
;
4190 template <typename A
>
4191 bool File
<A
>::forEachAtom(ld::File::AtomHandler
& handler
) const
4193 handler
.doFile(*this);
4194 uint8_t* p
= _atomsArray
;
4195 for(int i
=_atomsArrayCount
; i
> 0; --i
) {
4196 handler
.doAtom(*((Atom
<A
>*)p
));
4197 p
+= sizeof(Atom
<A
>);
4199 p
= _aliasAtomsArray
;
4200 for(int i
=_aliasAtomsArrayCount
; i
> 0; --i
) {
4201 handler
.doAtom(*((AliasAtom
*)p
));
4202 p
+= sizeof(AliasAtom
);
4205 return (_atomsArrayCount
!= 0) || (_aliasAtomsArrayCount
!= 0);
4208 template <typename A
>
4209 const char* Section
<A
>::makeSegmentName(const macho_section
<typename
A::P
>* sect
)
4211 // mach-o section record only has room for 16-byte seg/sect names
4212 // so a 16-byte name has no trailing zero
4213 const char* name
= sect
->segname();
4214 if ( strlen(name
) < 16 )
4216 char* tmp
= new char[17];
4217 strlcpy(tmp
, name
, 17);
4221 template <typename A
>
4222 const char* Section
<A
>::makeSectionName(const macho_section
<typename
A::P
>* sect
)
4224 const char* name
= sect
->sectname();
4225 if ( strlen(name
) < 16 )
4228 // special case common long section names so we don't have to malloc
4229 if ( strncmp(sect
->sectname(), "__objc_classrefs", 16) == 0 )
4230 return "__objc_classrefs";
4231 if ( strncmp(sect
->sectname(), "__objc_classlist", 16) == 0 )
4232 return "__objc_classlist";
4233 if ( strncmp(sect
->sectname(), "__objc_nlclslist", 16) == 0 )
4234 return "__objc_nlclslist";
4235 if ( strncmp(sect
->sectname(), "__objc_nlcatlist", 16) == 0 )
4236 return "__objc_nlcatlist";
4237 if ( strncmp(sect
->sectname(), "__objc_protolist", 16) == 0 )
4238 return "__objc_protolist";
4239 if ( strncmp(sect
->sectname(), "__objc_protorefs", 16) == 0 )
4240 return "__objc_protorefs";
4241 if ( strncmp(sect
->sectname(), "__objc_superrefs", 16) == 0 )
4242 return "__objc_superrefs";
4243 if ( strncmp(sect
->sectname(), "__objc_imageinfo", 16) == 0 )
4244 return "__objc_imageinfo";
4245 if ( strncmp(sect
->sectname(), "__objc_stringobj", 16) == 0 )
4246 return "__objc_stringobj";
4247 if ( strncmp(sect
->sectname(), "__gcc_except_tab", 16) == 0 )
4248 return "__gcc_except_tab";
4250 char* tmp
= new char[17];
4251 strlcpy(tmp
, name
, 17);
4255 template <typename A
>
4256 bool Section
<A
>::readable(const macho_section
<typename
A::P
>* sect
)
4261 template <typename A
>
4262 bool Section
<A
>::writable(const macho_section
<typename
A::P
>* sect
)
4264 // mach-o .o files do not contain segment permissions
4265 // we just know TEXT is special
4266 return ( strcmp(sect
->segname(), "__TEXT") != 0 );
4269 template <typename A
>
4270 bool Section
<A
>::exectuable(const macho_section
<typename
A::P
>* sect
)
4272 // mach-o .o files do not contain segment permissions
4273 // we just know TEXT is special
4274 return ( strcmp(sect
->segname(), "__TEXT") == 0 );
4278 template <typename A
>
4279 ld::Section::Type Section
<A
>::sectionType(const macho_section
<typename
A::P
>* sect
)
4281 switch ( sect
->flags() & SECTION_TYPE
) {
4283 return ld::Section::typeZeroFill
;
4284 case S_CSTRING_LITERALS
:
4285 if ( (strcmp(sect
->sectname(), "__cstring") == 0) && (strcmp(sect
->segname(), "__TEXT") == 0) )
4286 return ld::Section::typeCString
;
4288 return ld::Section::typeNonStdCString
;
4289 case S_4BYTE_LITERALS
:
4290 return ld::Section::typeLiteral4
;
4291 case S_8BYTE_LITERALS
:
4292 return ld::Section::typeLiteral8
;
4293 case S_LITERAL_POINTERS
:
4294 return ld::Section::typeCStringPointer
;
4295 case S_NON_LAZY_SYMBOL_POINTERS
:
4296 return ld::Section::typeNonLazyPointer
;
4297 case S_LAZY_SYMBOL_POINTERS
:
4298 return ld::Section::typeLazyPointer
;
4299 case S_SYMBOL_STUBS
:
4300 return ld::Section::typeStub
;
4301 case S_MOD_INIT_FUNC_POINTERS
:
4302 return ld::Section::typeInitializerPointers
;
4303 case S_MOD_TERM_FUNC_POINTERS
:
4304 return ld::Section::typeTerminatorPointers
;
4306 return ld::Section::typeUnclassified
;
4307 case S_16BYTE_LITERALS
:
4308 return ld::Section::typeLiteral16
;
4311 if ( sect
->flags() & S_ATTR_PURE_INSTRUCTIONS
) {
4312 return ld::Section::typeCode
;
4314 else if ( strcmp(sect
->segname(), "__TEXT") == 0 ) {
4315 if ( strcmp(sect
->sectname(), "__eh_frame") == 0 )
4316 return ld::Section::typeCFI
;
4317 else if ( strcmp(sect
->sectname(), "__ustring") == 0 )
4318 return ld::Section::typeUTF16Strings
;
4319 else if ( strcmp(sect
->sectname(), "__textcoal_nt") == 0 )
4320 return ld::Section::typeCode
;
4321 else if ( strcmp(sect
->sectname(), "__StaticInit") == 0 )
4322 return ld::Section::typeCode
;
4323 else if ( strcmp(sect
->sectname(), "__constructor") == 0 )
4324 return ld::Section::typeInitializerPointers
;
4326 else if ( strcmp(sect
->segname(), "__DATA") == 0 ) {
4327 if ( strcmp(sect
->sectname(), "__cfstring") == 0 )
4328 return ld::Section::typeCFString
;
4329 else if ( strcmp(sect
->sectname(), "__dyld") == 0 )
4330 return ld::Section::typeDyldInfo
;
4331 else if ( strcmp(sect
->sectname(), "__program_vars") == 0 )
4332 return ld::Section::typeDyldInfo
;
4333 else if ( strncmp(sect
->sectname(), "__objc_classrefs", 16) == 0 )
4334 return ld::Section::typeObjCClassRefs
;
4335 else if ( strcmp(sect
->sectname(), "__objc_catlist") == 0 )
4336 return ld::Section::typeObjC2CategoryList
;
4338 else if ( strcmp(sect
->segname(), "__OBJC") == 0 ) {
4339 if ( strcmp(sect
->sectname(), "__class") == 0 )
4340 return ld::Section::typeObjC1Classes
;
4343 case S_THREAD_LOCAL_REGULAR
:
4344 return ld::Section::typeTLVInitialValues
;
4345 case S_THREAD_LOCAL_ZEROFILL
:
4346 return ld::Section::typeTLVZeroFill
;
4347 case S_THREAD_LOCAL_VARIABLES
:
4348 return ld::Section::typeTLVDefs
;
4349 case S_THREAD_LOCAL_VARIABLE_POINTERS
:
4350 return ld::Section::typeTLVPointers
;
4351 case S_THREAD_LOCAL_INIT_FUNCTION_POINTERS
:
4352 return ld::Section::typeTLVInitializerPointers
;
4354 return ld::Section::typeUnclassified
;
4358 template <typename A
>
4359 Atom
<A
>* Section
<A
>::findContentAtomByAddress(pint_t addr
, class Atom
<A
>* start
, class Atom
<A
>* end
)
4361 // do a binary search of atom array
4362 uint32_t atomCount
= end
- start
;
4363 Atom
<A
>* base
= start
;
4364 for (uint32_t n
= atomCount
; n
> 0; n
/= 2) {
4365 Atom
<A
>* pivot
= &base
[n
/2];
4366 pint_t atomStartAddr
= pivot
->_objAddress
;
4367 pint_t atomEndAddr
= atomStartAddr
+ pivot
->_size
;
4368 if ( atomStartAddr
<= addr
) {
4369 // address in normal atom
4370 if (addr
< atomEndAddr
)
4372 // address in "end" label (but not in alias)
4373 if ( (pivot
->_size
== 0) && (addr
== atomEndAddr
) && !pivot
->isAlias() )
4376 if ( addr
>= atomEndAddr
) {
4378 // move base to atom after pivot
4390 template <typename A
>
4391 ld::Atom::Alignment Section
<A
>::alignmentForAddress(pint_t addr
)
4393 const uint32_t sectionAlignment
= this->_machOSection
->align();
4394 uint32_t modulus
= (addr
% (1 << sectionAlignment
));
4395 if ( modulus
> 0xFFFF )
4396 warning("alignment for symbol at address 0x%08llX in %s exceeds 2^16", (uint64_t)addr
, this->file().path());
4397 return ld::Atom::Alignment(sectionAlignment
, modulus
);
4400 template <typename A
>
4401 uint32_t Section
<A
>::sectionNum(class Parser
<A
>& parser
) const
4403 if ( _machOSection
== NULL
)
4406 return 1 + (this->_machOSection
- parser
.firstMachOSection());
4409 // arm does not have zero cost exceptions
4411 uint32_t CFISection
<arm
>::cfiCount(Parser
<arm
>& parser
)
4413 if ( parser
.armUsesZeroCostExceptions() ) {
4414 // create ObjectAddressSpace object for use by libunwind
4415 OAS
oas(*this, (uint8_t*)this->file().fileContent()+this->_machOSection
->offset());
4416 return libunwind::CFI_Parser
<OAS
>::getCFICount(oas
,
4417 this->_machOSection
->addr(), this->_machOSection
->size());
4422 template <typename A
>
4423 uint32_t CFISection
<A
>::cfiCount(Parser
<A
>& parser
)
4425 // create ObjectAddressSpace object for use by libunwind
4426 OAS
oas(*this, (uint8_t*)this->file().fileContent()+this->_machOSection
->offset());
4427 return libunwind::CFI_Parser
<OAS
>::getCFICount(oas
,
4428 this->_machOSection
->addr(), this->_machOSection
->size());
4431 template <typename A
>
4432 void CFISection
<A
>::warnFunc(void* ref
, uint64_t funcAddr
, const char* msg
)
4434 Parser
<A
>* parser
= (Parser
<A
>*)ref
;
4435 if ( ! parser
->warnUnwindConversionProblems() )
4437 if ( funcAddr
!= CFI_INVALID_ADDRESS
) {
4438 // atoms are not constructed yet, so scan symbol table for labels
4439 const char* name
= parser
->scanSymbolTableForAddress(funcAddr
);
4440 warning("could not create compact unwind for %s: %s", name
, msg
);
4443 warning("could not create compact unwind: %s", msg
);
4448 bool CFISection
<x86_64
>::needsRelocating()
4454 bool CFISection
<arm64
>::needsRelocating()
4460 template <typename A
>
4461 bool CFISection
<A
>::needsRelocating()
4467 void CFISection
<x86_64
>::cfiParse(class Parser
<x86_64
>& parser
, uint8_t* buffer
,
4468 libunwind::CFI_Atom_Info
<CFISection
<x86_64
>::OAS
> cfiArray
[],
4469 uint32_t& count
, const pint_t cuStarts
[], uint32_t cuCount
)
4471 const uint32_t sectionSize
= this->_machOSection
->size();
4472 // copy __eh_frame data to buffer
4473 memcpy(buffer
, file().fileContent() + this->_machOSection
->offset(), sectionSize
);
4475 // and apply relocations
4476 const macho_relocation_info
<P
>* relocs
= (macho_relocation_info
<P
>*)(file().fileContent() + this->_machOSection
->reloff());
4477 const macho_relocation_info
<P
>* relocsEnd
= &relocs
[this->_machOSection
->nreloc()];
4478 for (const macho_relocation_info
<P
>* reloc
= relocs
; reloc
< relocsEnd
; ++reloc
) {
4480 switch ( reloc
->r_type() ) {
4481 case X86_64_RELOC_SUBTRACTOR
:
4482 value
= 0 - parser
.symbolFromIndex(reloc
->r_symbolnum()).n_value();
4484 if ( reloc
->r_extern() )
4485 value
+= parser
.symbolFromIndex(reloc
->r_symbolnum()).n_value();
4487 case X86_64_RELOC_UNSIGNED
:
4488 value
= parser
.symbolFromIndex(reloc
->r_symbolnum()).n_value();
4490 case X86_64_RELOC_GOT
:
4491 // this is used for the reference to the personality function in CIEs
4492 // store the symbol number of the personality function for later use as a Fixup
4493 value
= reloc
->r_symbolnum();
4496 fprintf(stderr
, "CFISection::cfiParse() unexpected relocation type at r_address=0x%08X\n", reloc
->r_address());
4499 if ( reloc
->r_address() > sectionSize
)
4500 throwf("malformed __eh_frame relocation, offset (0x%08X) is beyond end of section,", reloc
->r_address());
4503 switch ( reloc
->r_length() ) {
4505 p64
= (uint64_t*)&buffer
[reloc
->r_address()];
4506 E::set64(*p64
, value
+ E::get64(*p64
));
4509 p32
= (uint32_t*)&buffer
[reloc
->r_address()];
4510 E::set32(*p32
, value
+ E::get32(*p32
));
4513 fprintf(stderr
, "CFISection::cfiParse() unexpected relocation size at r_address=0x%08X\n", reloc
->r_address());
4518 // create ObjectAddressSpace object for use by libunwind
4519 OAS
oas(*this, buffer
);
4521 // use libuwind to parse __eh_frame data into array of CFI_Atom_Info
4523 msg
= libunwind::DwarfInstructions
<OAS
, libunwind::Registers_x86_64
>::parseCFIs(
4524 oas
, this->_machOSection
->addr(), this->_machOSection
->size(),
4525 cuStarts
, cuCount
, parser
.keepDwarfUnwind(), parser
.forceDwarfConversion(), parser
.neverConvertDwarf(),
4526 cfiArray
, count
, (void*)&parser
, warnFunc
);
4528 throwf("malformed __eh_frame section: %s", msg
);
4532 void CFISection
<x86
>::cfiParse(class Parser
<x86
>& parser
, uint8_t* buffer
,
4533 libunwind::CFI_Atom_Info
<CFISection
<x86
>::OAS
> cfiArray
[],
4534 uint32_t& count
, const pint_t cuStarts
[], uint32_t cuCount
)
4536 // create ObjectAddressSpace object for use by libunwind
4537 OAS
oas(*this, (uint8_t*)this->file().fileContent()+this->_machOSection
->offset());
4539 // use libuwind to parse __eh_frame data into array of CFI_Atom_Info
4541 msg
= libunwind::DwarfInstructions
<OAS
, libunwind::Registers_x86
>::parseCFIs(
4542 oas
, this->_machOSection
->addr(), this->_machOSection
->size(),
4543 cuStarts
, cuCount
, parser
.keepDwarfUnwind(), parser
.forceDwarfConversion(), parser
.neverConvertDwarf(),
4544 cfiArray
, count
, (void*)&parser
, warnFunc
);
4546 throwf("malformed __eh_frame section: %s", msg
);
4553 void CFISection
<arm
>::cfiParse(class Parser
<arm
>& parser
, uint8_t* buffer
,
4554 libunwind::CFI_Atom_Info
<CFISection
<arm
>::OAS
> cfiArray
[],
4555 uint32_t& count
, const pint_t cuStarts
[], uint32_t cuCount
)
4557 if ( !parser
.armUsesZeroCostExceptions() ) {
4558 // most arm do not use zero cost exceptions
4562 // create ObjectAddressSpace object for use by libunwind
4563 OAS
oas(*this, (uint8_t*)this->file().fileContent()+this->_machOSection
->offset());
4565 // use libuwind to parse __eh_frame data into array of CFI_Atom_Info
4567 msg
= libunwind::DwarfInstructions
<OAS
, libunwind::Registers_arm
>::parseCFIs(
4568 oas
, this->_machOSection
->addr(), this->_machOSection
->size(),
4569 cuStarts
, cuCount
, parser
.keepDwarfUnwind(), parser
.forceDwarfConversion(), parser
.neverConvertDwarf(),
4570 cfiArray
, count
, (void*)&parser
, warnFunc
);
4572 throwf("malformed __eh_frame section: %s", msg
);
4579 void CFISection
<arm64
>::cfiParse(class Parser
<arm64
>& parser
, uint8_t* buffer
,
4580 libunwind::CFI_Atom_Info
<CFISection
<arm64
>::OAS
> cfiArray
[],
4581 uint32_t& count
, const pint_t cuStarts
[], uint32_t cuCount
)
4583 // copy __eh_frame data to buffer
4584 const uint32_t sectionSize
= this->_machOSection
->size();
4585 memcpy(buffer
, file().fileContent() + this->_machOSection
->offset(), sectionSize
);
4587 // and apply relocations
4588 const macho_relocation_info
<P
>* relocs
= (macho_relocation_info
<P
>*)(file().fileContent() + this->_machOSection
->reloff());
4589 const macho_relocation_info
<P
>* relocsEnd
= &relocs
[this->_machOSection
->nreloc()];
4590 for (const macho_relocation_info
<P
>* reloc
= relocs
; reloc
< relocsEnd
; ++reloc
) {
4591 uint64_t* p64
= (uint64_t*)&buffer
[reloc
->r_address()];
4592 uint32_t* p32
= (uint32_t*)&buffer
[reloc
->r_address()];
4593 uint32_t addend32
= E::get32(*p32
);
4594 uint64_t addend64
= E::get64(*p64
);
4596 switch ( reloc
->r_type() ) {
4597 case ARM64_RELOC_SUBTRACTOR
:
4598 value
= 0 - parser
.symbolFromIndex(reloc
->r_symbolnum()).n_value();
4600 if ( reloc
->r_extern() )
4601 value
+= parser
.symbolFromIndex(reloc
->r_symbolnum()).n_value();
4603 case ARM64_RELOC_UNSIGNED
:
4604 value
= parser
.symbolFromIndex(reloc
->r_symbolnum()).n_value();
4606 case ARM64_RELOC_POINTER_TO_GOT
:
4607 // this is used for the reference to the personality function in CIEs
4608 // store the symbol number of the personality function for later use as a Fixup
4609 value
= reloc
->r_symbolnum();
4614 fprintf(stderr
, "CFISection::cfiParse() unexpected relocation type at r_address=0x%08X\n", reloc
->r_address());
4617 if ( reloc
->r_address() > sectionSize
)
4618 throwf("malformed __eh_frame relocation, offset (0x%08X) is beyond end of section,", reloc
->r_address());
4619 switch ( reloc
->r_length() ) {
4621 E::set64(*p64
, value
+ addend64
);
4624 E::set32(*p32
, value
+ addend32
);
4627 fprintf(stderr
, "CFISection::cfiParse() unexpected relocation size at r_address=0x%08X\n", reloc
->r_address());
4633 // create ObjectAddressSpace object for use by libunwind
4634 OAS
oas(*this, buffer
);
4636 // use libuwind to parse __eh_frame data into array of CFI_Atom_Info
4638 msg
= libunwind::DwarfInstructions
<OAS
, libunwind::Registers_arm64
>::parseCFIs(
4639 oas
, this->_machOSection
->addr(), this->_machOSection
->size(),
4640 cuStarts
, cuCount
, parser
.keepDwarfUnwind(), parser
.forceDwarfConversion(), parser
.neverConvertDwarf(),
4641 cfiArray
, count
, (void*)&parser
, warnFunc
);
4643 throwf("malformed __eh_frame section: %s", msg
);
4647 template <typename A
>
4648 uint32_t CFISection
<A
>::computeAtomCount(class Parser
<A
>& parser
,
4649 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
4650 const struct Parser
<A
>::CFI_CU_InfoArrays
& cfis
)
4652 return cfis
.cfiCount
;
4657 template <typename A
>
4658 uint32_t CFISection
<A
>::appendAtoms(class Parser
<A
>& parser
, uint8_t* p
,
4659 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
4660 const struct Parser
<A
>::CFI_CU_InfoArrays
& cfis
)
4662 this->_beginAtoms
= (Atom
<A
>*)p
;
4663 // walk CFI_Atom_Info array and create atom for each entry
4664 const CFI_Atom_Info
* start
= &cfis
.cfiArray
[0];
4665 const CFI_Atom_Info
* end
= &cfis
.cfiArray
[cfis
.cfiCount
];
4666 for(const CFI_Atom_Info
* a
=start
; a
< end
; ++a
) {
4667 Atom
<A
>* space
= (Atom
<A
>*)p
;
4668 new (space
) Atom
<A
>(*this, (a
->isCIE
? "CIE" : "FDE"), a
->address
, a
->size
,
4669 ld::Atom::definitionRegular
, ld::Atom::combineNever
, ld::Atom::scopeTranslationUnit
,
4670 ld::Atom::typeCFI
, ld::Atom::symbolTableNotInFinalLinkedImages
,
4671 false, false, false, ld::Atom::Alignment(0));
4672 p
+= sizeof(Atom
<A
>);
4674 this->_endAtoms
= (Atom
<A
>*)p
;
4675 return cfis
.cfiCount
;
4679 template <> bool CFISection
<x86_64
>::bigEndian() { return false; }
4680 template <> bool CFISection
<x86
>::bigEndian() { return false; }
4681 template <> bool CFISection
<arm
>::bigEndian() { return false; }
4682 template <> bool CFISection
<arm64
>::bigEndian() { return false; }
4685 void CFISection
<x86_64
>::addCiePersonalityFixups(class Parser
<x86_64
>& parser
, const CFI_Atom_Info
* cieInfo
)
4687 uint8_t personalityEncoding
= cieInfo
->u
.cieInfo
.personality
.encodingOfTargetAddress
;
4688 if ( personalityEncoding
== 0x9B ) {
4689 // compiler always produces X86_64_RELOC_GOT with addend of 4 to personality function
4690 // CFISection<x86_64>::cfiParse() set targetAddress to be symbolIndex + 4 + addressInCIE
4691 uint32_t symbolIndex
= cieInfo
->u
.cieInfo
.personality
.targetAddress
- 4
4692 - cieInfo
->address
- cieInfo
->u
.cieInfo
.personality
.offsetInCFI
;
4693 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(symbolIndex
);
4694 const char* personalityName
= parser
.nameFromSymbol(sym
);
4696 Atom
<x86_64
>* cieAtom
= this->findAtomByAddress(cieInfo
->address
);
4697 Parser
<x86_64
>::SourceLocation
src(cieAtom
, cieInfo
->u
.cieInfo
.personality
.offsetInCFI
);
4698 parser
.addFixup(src
, ld::Fixup::k1of3
, ld::Fixup::kindSetTargetAddress
, false, personalityName
);
4699 parser
.addFixup(src
, ld::Fixup::k2of3
, ld::Fixup::kindAddAddend
, 4);
4700 parser
.addFixup(src
, ld::Fixup::k3of3
, ld::Fixup::kindStoreX86PCRel32GOT
);
4702 else if ( personalityEncoding
!= 0 ) {
4703 throwf("unsupported address encoding (%02X) of personality function in CIE",
4704 personalityEncoding
);
4709 void CFISection
<x86
>::addCiePersonalityFixups(class Parser
<x86
>& parser
, const CFI_Atom_Info
* cieInfo
)
4711 uint8_t personalityEncoding
= cieInfo
->u
.cieInfo
.personality
.encodingOfTargetAddress
;
4712 if ( (personalityEncoding
== 0x9B) || (personalityEncoding
== 0x90) ) {
4713 uint32_t offsetInCFI
= cieInfo
->u
.cieInfo
.personality
.offsetInCFI
;
4714 uint32_t nlpAddr
= cieInfo
->u
.cieInfo
.personality
.targetAddress
;
4715 Atom
<x86
>* cieAtom
= this->findAtomByAddress(cieInfo
->address
);
4716 Atom
<x86
>* nlpAtom
= parser
.findAtomByAddress(nlpAddr
);
4717 assert(nlpAtom
->contentType() == ld::Atom::typeNonLazyPointer
);
4718 Parser
<x86
>::SourceLocation
src(cieAtom
, cieInfo
->u
.cieInfo
.personality
.offsetInCFI
);
4720 parser
.addFixup(src
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, nlpAtom
);
4721 parser
.addFixup(src
, ld::Fixup::k2of4
, ld::Fixup::kindSubtractTargetAddress
, cieAtom
);
4722 parser
.addFixup(src
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractAddend
, offsetInCFI
);
4723 parser
.addFixup(src
, ld::Fixup::k4of4
, ld::Fixup::kindStoreLittleEndian32
);
4725 else if ( personalityEncoding
!= 0 ) {
4726 throwf("unsupported address encoding (%02X) of personality function in CIE", personalityEncoding
);
4730 #if SUPPORT_ARCH_arm64
4732 void CFISection
<arm64
>::addCiePersonalityFixups(class Parser
<arm64
>& parser
, const CFI_Atom_Info
* cieInfo
)
4734 uint8_t personalityEncoding
= cieInfo
->u
.cieInfo
.personality
.encodingOfTargetAddress
;
4735 if ( personalityEncoding
== 0x9B ) {
4736 // compiler always produces ARM64_RELOC_GOT r_pcrel=1 to personality function
4737 // CFISection<arm64>::cfiParse() set targetAddress to be symbolIndex + addressInCIE
4738 uint32_t symbolIndex
= cieInfo
->u
.cieInfo
.personality
.targetAddress
4739 - cieInfo
->address
- cieInfo
->u
.cieInfo
.personality
.offsetInCFI
;
4740 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(symbolIndex
);
4741 const char* personalityName
= parser
.nameFromSymbol(sym
);
4743 Atom
<arm64
>* cieAtom
= this->findAtomByAddress(cieInfo
->address
);
4744 Parser
<arm64
>::SourceLocation
src(cieAtom
, cieInfo
->u
.cieInfo
.personality
.offsetInCFI
);
4745 parser
.addFixup(src
, ld::Fixup::k1of2
, ld::Fixup::kindSetTargetAddress
, false, personalityName
);
4746 parser
.addFixup(src
, ld::Fixup::k2of2
, ld::Fixup::kindStoreARM64PCRelToGOT
);
4748 else if ( personalityEncoding
!= 0 ) {
4749 throwf("unsupported address encoding (%02X) of personality function in CIE",
4750 personalityEncoding
);
4757 void CFISection
<arm
>::addCiePersonalityFixups(class Parser
<arm
>& parser
, const CFI_Atom_Info
* cieInfo
)
4759 uint8_t personalityEncoding
= cieInfo
->u
.cieInfo
.personality
.encodingOfTargetAddress
;
4760 if ( (personalityEncoding
== 0x9B) || (personalityEncoding
== 0x90) ) {
4761 uint32_t offsetInCFI
= cieInfo
->u
.cieInfo
.personality
.offsetInCFI
;
4762 uint32_t nlpAddr
= cieInfo
->u
.cieInfo
.personality
.targetAddress
;
4763 Atom
<arm
>* cieAtom
= this->findAtomByAddress(cieInfo
->address
);
4764 Atom
<arm
>* nlpAtom
= parser
.findAtomByAddress(nlpAddr
);
4765 assert(nlpAtom
->contentType() == ld::Atom::typeNonLazyPointer
);
4766 Parser
<arm
>::SourceLocation
src(cieAtom
, cieInfo
->u
.cieInfo
.personality
.offsetInCFI
);
4768 parser
.addFixup(src
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, nlpAtom
);
4769 parser
.addFixup(src
, ld::Fixup::k2of4
, ld::Fixup::kindSubtractTargetAddress
, cieAtom
);
4770 parser
.addFixup(src
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractAddend
, offsetInCFI
);
4771 parser
.addFixup(src
, ld::Fixup::k4of4
, ld::Fixup::kindStoreLittleEndian32
);
4773 else if ( personalityEncoding
!= 0 ) {
4774 throwf("unsupported address encoding (%02X) of personality function in CIE", personalityEncoding
);
4780 template <typename A
>
4781 void CFISection
<A
>::addCiePersonalityFixups(class Parser
<A
>& parser
, const CFI_Atom_Info
* cieInfo
)
4783 assert(0 && "addCiePersonalityFixups() not implemented for arch");
4786 template <typename A
>
4787 void CFISection
<A
>::makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
& cfis
)
4789 ld::Fixup::Kind store32
= bigEndian() ? ld::Fixup::kindStoreBigEndian32
: ld::Fixup::kindStoreLittleEndian32
;
4790 ld::Fixup::Kind store64
= bigEndian() ? ld::Fixup::kindStoreBigEndian64
: ld::Fixup::kindStoreLittleEndian64
;
4792 // add all references for FDEs, including implicit group references
4793 const CFI_Atom_Info
* end
= &cfis
.cfiArray
[cfis
.cfiCount
];
4794 for(const CFI_Atom_Info
* p
= &cfis
.cfiArray
[0]; p
< end
; ++p
) {
4796 // add reference to personality function if used
4797 if ( p
->u
.cieInfo
.personality
.targetAddress
!= CFI_INVALID_ADDRESS
) {
4798 this->addCiePersonalityFixups(parser
, p
);
4803 Atom
<A
>* fdeAtom
= this->findAtomByAddress(p
->address
);
4804 // find function Atom
4805 Atom
<A
>* functionAtom
= parser
.findAtomByAddress(p
->u
.fdeInfo
.function
.targetAddress
);
4807 Atom
<A
>* cieAtom
= this->findAtomByAddress(p
->u
.fdeInfo
.cie
.targetAddress
);
4809 Atom
<A
>* lsdaAtom
= NULL
;
4810 if ( p
->u
.fdeInfo
.lsda
.targetAddress
!= CFI_INVALID_ADDRESS
) {
4811 lsdaAtom
= parser
.findAtomByAddress(p
->u
.fdeInfo
.lsda
.targetAddress
);
4813 // add reference from FDE to CIE (always 32-bit pc-rel)
4814 typename Parser
<A
>::SourceLocation
fdeToCieSrc(fdeAtom
, p
->u
.fdeInfo
.cie
.offsetInCFI
);
4815 parser
.addFixup(fdeToCieSrc
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, fdeAtom
);
4816 parser
.addFixup(fdeToCieSrc
, ld::Fixup::k2of4
, ld::Fixup::kindAddAddend
, p
->u
.fdeInfo
.cie
.offsetInCFI
);
4817 parser
.addFixup(fdeToCieSrc
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractTargetAddress
, cieAtom
);
4818 parser
.addFixup(fdeToCieSrc
, ld::Fixup::k4of4
, store32
, cieAtom
);
4820 // add reference from FDE to function
4821 typename Parser
<A
>::SourceLocation
fdeToFuncSrc(fdeAtom
, p
->u
.fdeInfo
.function
.offsetInCFI
);
4822 switch (p
->u
.fdeInfo
.function
.encodingOfTargetAddress
) {
4823 case DW_EH_PE_pcrel
|DW_EH_PE_ptr
:
4824 if ( sizeof(typename
A::P::uint_t
) == 8 ) {
4825 parser
.addFixup(fdeToFuncSrc
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, functionAtom
);
4826 parser
.addFixup(fdeToFuncSrc
, ld::Fixup::k2of4
, ld::Fixup::kindSubtractTargetAddress
, fdeAtom
);
4827 parser
.addFixup(fdeToFuncSrc
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractAddend
, p
->u
.fdeInfo
.function
.offsetInCFI
);
4828 parser
.addFixup(fdeToFuncSrc
, ld::Fixup::k4of4
, store64
);
4831 // else fall into 32-bit case
4832 case DW_EH_PE_pcrel
|DW_EH_PE_sdata4
:
4833 parser
.addFixup(fdeToFuncSrc
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, functionAtom
);
4834 parser
.addFixup(fdeToFuncSrc
, ld::Fixup::k2of4
, ld::Fixup::kindSubtractTargetAddress
, fdeAtom
);
4835 parser
.addFixup(fdeToFuncSrc
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractAddend
, p
->u
.fdeInfo
.function
.offsetInCFI
);
4836 parser
.addFixup(fdeToFuncSrc
, ld::Fixup::k4of4
, store32
);
4839 throw "unsupported encoding in FDE of pointer to function";
4842 // add reference from FDE to LSDA
4843 typename Parser
<A
>::SourceLocation
fdeToLsdaSrc(fdeAtom
, p
->u
.fdeInfo
.lsda
.offsetInCFI
);
4844 if ( lsdaAtom
!= NULL
) {
4845 switch (p
->u
.fdeInfo
.lsda
.encodingOfTargetAddress
) {
4846 case DW_EH_PE_pcrel
|DW_EH_PE_ptr
:
4847 if ( sizeof(typename
A::P::uint_t
) == 8 ) {
4848 parser
.addFixup(fdeToLsdaSrc
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, lsdaAtom
);
4849 parser
.addFixup(fdeToLsdaSrc
, ld::Fixup::k2of4
, ld::Fixup::kindSubtractTargetAddress
, fdeAtom
);
4850 parser
.addFixup(fdeToLsdaSrc
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractAddend
, p
->u
.fdeInfo
.lsda
.offsetInCFI
);
4851 parser
.addFixup(fdeToLsdaSrc
, ld::Fixup::k4of4
, store64
);
4854 // else fall into 32-bit case
4855 case DW_EH_PE_pcrel
|DW_EH_PE_sdata4
:
4856 parser
.addFixup(fdeToLsdaSrc
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, lsdaAtom
);
4857 parser
.addFixup(fdeToLsdaSrc
, ld::Fixup::k2of4
, ld::Fixup::kindSubtractTargetAddress
, fdeAtom
);
4858 parser
.addFixup(fdeToLsdaSrc
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractAddend
, p
->u
.fdeInfo
.lsda
.offsetInCFI
);
4859 parser
.addFixup(fdeToLsdaSrc
, ld::Fixup::k4of4
, store32
);
4862 throw "unsupported encoding in FDE of pointer to LSDA";
4866 // FDE is in group lead by function atom
4867 typename Parser
<A
>::SourceLocation
fdeSrc(functionAtom
,0);
4868 parser
.addFixup(fdeSrc
, ld::Fixup::k1of1
, ld::Fixup::kindNoneGroupSubordinateFDE
, fdeAtom
);
4870 // LSDA is in group lead by function atom
4871 if ( lsdaAtom
!= NULL
) {
4872 parser
.addFixup(fdeSrc
, ld::Fixup::k1of1
, ld::Fixup::kindNoneGroupSubordinateLSDA
, lsdaAtom
);
4881 template <typename A
>
4882 const void* CFISection
<A
>::OAS::mappedAddress(pint_t addr
)
4884 if ( (_ehFrameStartAddr
<= addr
) && (addr
< _ehFrameEndAddr
) )
4885 return &_ehFrameContent
[addr
-_ehFrameStartAddr
];
4887 // requested bytes are not in __eh_frame section
4888 // this can occur when examining the instruction bytes in the __text
4889 File
<A
>& file
= _ehFrameSection
.file();
4890 for (uint32_t i
=0; i
< file
._sectionsArrayCount
; ++i
) {
4891 const macho_section
<typename
A::P
>* sect
= file
._sectionsArray
[i
]->machoSection();
4892 // TentativeDefinitionSection and AbsoluteSymbolSection have no mach-o section
4893 if ( sect
!= NULL
) {
4894 if ( (sect
->addr() <= addr
) && (addr
< (sect
->addr()+sect
->size())) ) {
4895 return file
.fileContent() + sect
->offset() + addr
- sect
->addr();
4899 throwf("__eh_frame parsing problem. Can't find target of reference to address 0x%08llX", (uint64_t)addr
);
4904 template <typename A
>
4905 uint64_t CFISection
<A
>::OAS::getULEB128(pint_t
& logicalAddr
, pint_t end
)
4907 uintptr_t size
= (end
- logicalAddr
);
4908 libunwind::LocalAddressSpace::pint_t laddr
= (libunwind::LocalAddressSpace::pint_t
)mappedAddress(logicalAddr
);
4909 libunwind::LocalAddressSpace::pint_t sladdr
= laddr
;
4910 uint64_t result
= libunwind::LocalAddressSpace::getULEB128(laddr
, laddr
+size
);
4911 logicalAddr
+= (laddr
-sladdr
);
4915 template <typename A
>
4916 int64_t CFISection
<A
>::OAS::getSLEB128(pint_t
& logicalAddr
, pint_t end
)
4918 uintptr_t size
= (end
- logicalAddr
);
4919 libunwind::LocalAddressSpace::pint_t laddr
= (libunwind::LocalAddressSpace::pint_t
)mappedAddress(logicalAddr
);
4920 libunwind::LocalAddressSpace::pint_t sladdr
= laddr
;
4921 int64_t result
= libunwind::LocalAddressSpace::getSLEB128(laddr
, laddr
+size
);
4922 logicalAddr
+= (laddr
-sladdr
);
4926 template <typename A
>
4927 typename
A::P::uint_t CFISection
<A
>::OAS::getEncodedP(pint_t
& addr
, pint_t end
, uint8_t encoding
)
4929 pint_t startAddr
= addr
;
4934 switch (encoding
& 0x0F) {
4936 result
= getP(addr
);
4937 p
+= sizeof(pint_t
);
4940 case DW_EH_PE_uleb128
:
4941 result
= getULEB128(addr
, end
);
4943 case DW_EH_PE_udata2
:
4944 result
= get16(addr
);
4948 case DW_EH_PE_udata4
:
4949 result
= get32(addr
);
4953 case DW_EH_PE_udata8
:
4954 result
= get64(addr
);
4958 case DW_EH_PE_sleb128
:
4959 result
= getSLEB128(addr
, end
);
4961 case DW_EH_PE_sdata2
:
4962 result
= (int16_t)get16(addr
);
4966 case DW_EH_PE_sdata4
:
4967 result
= (int32_t)get32(addr
);
4971 case DW_EH_PE_sdata8
:
4972 result
= get64(addr
);
4977 throwf("ObjectFileAddressSpace<A>::getEncodedP() encoding 0x%08X not supported", encoding
);
4980 // then add relative offset
4981 switch ( encoding
& 0x70 ) {
4982 case DW_EH_PE_absptr
:
4985 case DW_EH_PE_pcrel
:
4986 result
+= startAddr
;
4988 case DW_EH_PE_textrel
:
4989 throw "DW_EH_PE_textrel pointer encoding not supported";
4991 case DW_EH_PE_datarel
:
4992 throw "DW_EH_PE_datarel pointer encoding not supported";
4994 case DW_EH_PE_funcrel
:
4995 throw "DW_EH_PE_funcrel pointer encoding not supported";
4997 case DW_EH_PE_aligned
:
4998 throw "DW_EH_PE_aligned pointer encoding not supported";
5001 throwf("ObjectFileAddressSpace<A>::getEncodedP() encoding 0x%08X not supported", encoding
);
5005 // Note: DW_EH_PE_indirect is only used in CIEs to refernce the personality pointer
5006 // When parsing .o files that pointer contains zero, so we don't to return that.
5007 // Instead we skip the dereference and return the address of the pointer.
5008 // if ( encoding & DW_EH_PE_indirect )
5009 // result = getP(result);
5015 const char* CUSection
<x86_64
>::personalityName(class Parser
<x86_64
>& parser
, const macho_relocation_info
<x86_64::P
>* reloc
)
5017 if ( reloc
->r_extern() ) {
5018 assert((reloc
->r_type() == X86_64_RELOC_UNSIGNED
) && "wrong reloc type on personality column in __compact_unwind section");
5019 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(reloc
->r_symbolnum());
5020 return parser
.nameFromSymbol(sym
);
5023 const pint_t
* content
= (pint_t
*)(this->file().fileContent() + this->_machOSection
->offset() + reloc
->r_address());
5024 pint_t personalityAddr
= *content
;
5025 assert((parser
.sectionForAddress(personalityAddr
)->type() == ld::Section::typeCode
) && "personality column in __compact_unwind section is not pointer to function");
5026 // atoms may not be constructed yet, so scan symbol table for labels
5027 const char* name
= parser
.scanSymbolTableForAddress(personalityAddr
);
5033 const char* CUSection
<x86
>::personalityName(class Parser
<x86
>& parser
, const macho_relocation_info
<x86::P
>* reloc
)
5035 if ( reloc
->r_extern() ) {
5036 assert((reloc
->r_type() == GENERIC_RELOC_VANILLA
) && "wrong reloc type on personality column in __compact_unwind section");
5037 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(reloc
->r_symbolnum());
5038 return parser
.nameFromSymbol(sym
);
5041 // support __LD, __compact_unwind personality entries which are pointer to personality non-lazy pointer
5042 const pint_t
* content
= (pint_t
*)(this->file().fileContent() + this->_machOSection
->offset() + reloc
->r_address());
5043 pint_t nlPointerAddr
= *content
;
5044 Section
<x86
>* nlSection
= parser
.sectionForAddress(nlPointerAddr
);
5045 if ( nlSection
->type() == ld::Section::typeCode
) {
5046 // personality function is defined in this .o file, so this is a direct reference to it
5047 // atoms may not be constructed yet, so scan symbol table for labels
5048 const char* name
= parser
.scanSymbolTableForAddress(nlPointerAddr
);
5052 uint32_t symIndex
= parser
.symbolIndexFromIndirectSectionAddress(nlPointerAddr
, nlSection
->machoSection());
5053 const macho_nlist
<P
>& nlSymbol
= parser
.symbolFromIndex(symIndex
);
5054 return parser
.nameFromSymbol(nlSymbol
);
5059 #if SUPPORT_ARCH_arm64
5061 const char* CUSection
<arm64
>::personalityName(class Parser
<arm64
>& parser
, const macho_relocation_info
<arm64::P
>* reloc
)
5063 if ( reloc
->r_extern() ) {
5064 assert((reloc
->r_type() == ARM64_RELOC_UNSIGNED
) && "wrong reloc type on personality column in __compact_unwind section");
5065 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(reloc
->r_symbolnum());
5066 return parser
.nameFromSymbol(sym
);
5069 const pint_t
* content
= (pint_t
*)(this->file().fileContent() + this->_machOSection
->offset() + reloc
->r_address());
5070 pint_t personalityAddr
= *content
;
5071 Section
<arm64
>* personalitySection
= parser
.sectionForAddress(personalityAddr
);
5072 (void)personalitySection
;
5073 assert((personalitySection
->type() == ld::Section::typeCode
) && "personality column in __compact_unwind section is not pointer to function");
5074 // atoms may not be constructed yet, so scan symbol table for labels
5075 const char* name
= parser
.scanSymbolTableForAddress(personalityAddr
);
5082 #if SUPPORT_ARCH_arm_any
5084 const char* CUSection
<arm
>::personalityName(class Parser
<arm
>& parser
, const macho_relocation_info
<arm::P
>* reloc
)
5086 if ( reloc
->r_extern() ) {
5087 assert((reloc
->r_type() == ARM_RELOC_VANILLA
) && "wrong reloc type on personality column in __compact_unwind section");
5088 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(reloc
->r_symbolnum());
5089 return parser
.nameFromSymbol(sym
);
5092 // support __LD, __compact_unwind personality entries which are pointer to personality non-lazy pointer
5093 const pint_t
* content
= (pint_t
*)(this->file().fileContent() + this->_machOSection
->offset() + reloc
->r_address());
5094 pint_t nlPointerAddr
= *content
;
5095 Section
<arm
>* nlSection
= parser
.sectionForAddress(nlPointerAddr
);
5096 if ( nlSection
->type() == ld::Section::typeCode
) {
5097 // personality function is defined in this .o file, so this is a direct reference to it
5098 // atoms may not be constructed yet, so scan symbol table for labels
5099 const char* name
= parser
.scanSymbolTableForAddress(nlPointerAddr
);
5103 uint32_t symIndex
= parser
.symbolIndexFromIndirectSectionAddress(nlPointerAddr
, nlSection
->machoSection());
5104 const macho_nlist
<P
>& nlSymbol
= parser
.symbolFromIndex(symIndex
);
5105 return parser
.nameFromSymbol(nlSymbol
);
5112 template <typename A
>
5113 const char* CUSection
<A
>::personalityName(class Parser
<A
>& parser
, const macho_relocation_info
<P
>* reloc
)
5119 bool CUSection
<x86
>::encodingMeansUseDwarf(compact_unwind_encoding_t enc
)
5121 return ((enc
& UNWIND_X86_MODE_MASK
) == UNWIND_X86_MODE_DWARF
);
5125 bool CUSection
<x86_64
>::encodingMeansUseDwarf(compact_unwind_encoding_t enc
)
5127 return ((enc
& UNWIND_X86_64_MODE_MASK
) == UNWIND_X86_64_MODE_DWARF
);
5130 #if SUPPORT_ARCH_arm_any
5132 bool CUSection
<arm
>::encodingMeansUseDwarf(compact_unwind_encoding_t enc
)
5134 return ((enc
& UNWIND_ARM_MODE_MASK
) == UNWIND_ARM_MODE_DWARF
);
5138 #if SUPPORT_ARCH_arm64
5140 bool CUSection
<arm64
>::encodingMeansUseDwarf(compact_unwind_encoding_t enc
)
5142 return ((enc
& UNWIND_ARM64_MODE_MASK
) == UNWIND_ARM64_MODE_DWARF
);
5147 template <typename A
>
5148 int CUSection
<A
>::infoSorter(const void* l
, const void* r
)
5150 // sort references by symbol index, then address
5151 const Info
* left
= (Info
*)l
;
5152 const Info
* right
= (Info
*)r
;
5153 if ( left
->functionSymbolIndex
== right
->functionSymbolIndex
)
5154 return (left
->functionStartAddress
- right
->functionStartAddress
);
5156 return (left
->functionSymbolIndex
- right
->functionSymbolIndex
);
5159 template <typename A
>
5160 void CUSection
<A
>::parse(class Parser
<A
>& parser
, uint32_t cnt
, Info array
[])
5162 // walk section content and copy to Info array
5163 const macho_compact_unwind_entry
<P
>* const entries
= (macho_compact_unwind_entry
<P
>*)(this->file().fileContent() + this->_machOSection
->offset());
5164 for (uint32_t i
=0; i
< cnt
; ++i
) {
5165 Info
* info
= &array
[i
];
5166 const macho_compact_unwind_entry
<P
>* entry
= &entries
[i
];
5167 info
->functionStartAddress
= entry
->codeStart();
5168 info
->functionSymbolIndex
= 0xFFFFFFFF;
5169 info
->rangeLength
= entry
->codeLen();
5170 info
->compactUnwindInfo
= entry
->compactUnwindInfo();
5171 info
->personality
= NULL
;
5172 info
->lsdaAddress
= entry
->lsda();
5173 info
->function
= NULL
;
5175 if ( (info
->compactUnwindInfo
& UNWIND_PERSONALITY_MASK
) != 0 )
5176 warning("no bits should be set in UNWIND_PERSONALITY_MASK of compact unwind encoding in __LD,__compact_unwind section");
5177 if ( info
->lsdaAddress
!= 0 ) {
5178 info
->compactUnwindInfo
|= UNWIND_HAS_LSDA
;
5182 // scan relocs, extern relocs are needed for personality references (possibly for function/lsda refs??)
5183 const uint32_t sectionSize
= this->_machOSection
->size();
5184 const macho_relocation_info
<P
>* relocs
= (macho_relocation_info
<P
>*)(this->file().fileContent() + this->_machOSection
->reloff());
5185 const macho_relocation_info
<P
>* relocsEnd
= &relocs
[this->_machOSection
->nreloc()];
5186 for (const macho_relocation_info
<P
>* reloc
= relocs
; reloc
< relocsEnd
; ++reloc
) {
5187 if ( reloc
->r_address() & R_SCATTERED
)
5189 if ( reloc
->r_address() > sectionSize
)
5190 throwf("malformed __compact_unwind relocation, offset (0x%08X) is beyond end of section,", reloc
->r_address());
5191 if ( reloc
->r_extern() ) {
5192 // only expect external relocs on some colummns
5193 if ( (reloc
->r_address() % sizeof(macho_compact_unwind_entry
<P
>)) == macho_compact_unwind_entry
<P
>::personalityFieldOffset() ) {
5194 uint32_t entryIndex
= reloc
->r_address() / sizeof(macho_compact_unwind_entry
<P
>);
5195 array
[entryIndex
].personality
= this->personalityName(parser
, reloc
);
5197 else if ( (reloc
->r_address() % sizeof(macho_compact_unwind_entry
<P
>)) == macho_compact_unwind_entry
<P
>::lsdaFieldOffset() ) {
5198 uint32_t entryIndex
= reloc
->r_address() / sizeof(macho_compact_unwind_entry
<P
>);
5199 const macho_nlist
<P
>& lsdaSym
= parser
.symbolFromIndex(reloc
->r_symbolnum());
5200 if ( (lsdaSym
.n_type() & N_TYPE
) == N_SECT
)
5201 array
[entryIndex
].lsdaAddress
= lsdaSym
.n_value();
5203 warning("unexpected extern relocation to lsda in __compact_unwind section");
5205 else if ( (reloc
->r_address() % sizeof(macho_compact_unwind_entry
<P
>)) == macho_compact_unwind_entry
<P
>::codeStartFieldOffset() ) {
5206 uint32_t entryIndex
= reloc
->r_address() / sizeof(macho_compact_unwind_entry
<P
>);
5207 array
[entryIndex
].functionSymbolIndex
= reloc
->r_symbolnum();
5208 array
[entryIndex
].functionStartAddress
+= parser
.symbolFromIndex(reloc
->r_symbolnum()).n_value();
5211 warning("unexpected extern relocation in __compact_unwind section");
5215 if ( (reloc
->r_address() % sizeof(macho_compact_unwind_entry
<P
>)) == macho_compact_unwind_entry
<P
>::personalityFieldOffset() ) {
5216 uint32_t entryIndex
= reloc
->r_address() / sizeof(macho_compact_unwind_entry
<P
>);
5217 array
[entryIndex
].personality
= this->personalityName(parser
, reloc
);
5222 // sort array by function start address so unwind infos will be contiguous for a given function
5223 ::qsort(array
, cnt
, sizeof(Info
), infoSorter
);
5226 template <typename A
>
5227 uint32_t CUSection
<A
>::count()
5229 const macho_section
<P
>* machoSect
= this->machoSection();
5230 if ( (machoSect
->size() % sizeof(macho_compact_unwind_entry
<P
>)) != 0 )
5231 throw "malformed __LD,__compact_unwind section, bad length";
5233 return machoSect
->size() / sizeof(macho_compact_unwind_entry
<P
>);
5236 template <typename A
>
5237 void CUSection
<A
>::makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
& cus
)
5239 Info
* const arrayStart
= cus
.cuArray
;
5240 Info
* const arrayEnd
= &cus
.cuArray
[cus
.cuCount
];
5241 for (Info
* info
=arrayStart
; info
< arrayEnd
; ++info
) {
5242 // find function atom from address
5243 info
->function
= parser
.findAtomByAddress(info
->functionStartAddress
);
5244 // find lsda atom from address
5245 if ( info
->lsdaAddress
!= 0 ) {
5246 info
->lsda
= parser
.findAtomByAddress(info
->lsdaAddress
);
5247 // add lsda subordinate
5248 typename Parser
<A
>::SourceLocation
src(info
->function
, info
->functionStartAddress
- info
->function
->objectAddress());
5249 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindNoneGroupSubordinateLSDA
, info
->lsda
);
5251 if ( info
->personality
!= NULL
) {
5252 // add personality subordinate
5253 typename Parser
<A
>::SourceLocation
src(info
->function
, info
->functionStartAddress
- info
->function
->objectAddress());
5254 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindNoneGroupSubordinatePersonality
, false, info
->personality
);
5260 template <typename A
>
5261 SymboledSection
<A
>::SymboledSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
5262 : Section
<A
>(f
, s
), _type(ld::Atom::typeUnclassified
)
5264 switch ( s
->flags() & SECTION_TYPE
) {
5266 _type
= ld::Atom::typeZeroFill
;
5268 case S_MOD_INIT_FUNC_POINTERS
:
5269 _type
= ld::Atom::typeInitializerPointers
;
5271 case S_MOD_TERM_FUNC_POINTERS
:
5272 _type
= ld::Atom::typeTerminatorPointers
;
5274 case S_THREAD_LOCAL_VARIABLES
:
5275 _type
= ld::Atom::typeTLV
;
5277 case S_THREAD_LOCAL_ZEROFILL
:
5278 _type
= ld::Atom::typeTLVZeroFill
;
5280 case S_THREAD_LOCAL_REGULAR
:
5281 _type
= ld::Atom::typeTLVInitialValue
;
5283 case S_THREAD_LOCAL_INIT_FUNCTION_POINTERS
:
5284 _type
= ld::Atom::typeTLVInitializerPointers
;
5287 if ( strncmp(s
->sectname(), "__gcc_except_tab", 16) == 0 )
5288 _type
= ld::Atom::typeLSDA
;
5289 else if ( this->type() == ld::Section::typeInitializerPointers
)
5290 _type
= ld::Atom::typeInitializerPointers
;
5291 // <rdar://problem/34716321> don't warn about static initializers in dylibs built for profiling
5292 if ( strncmp(s
->sectname(), "__llvm_prf_", 11) == 0 )
5293 this->_file
.setHasllvmProfiling();
5299 template <typename A
>
5300 bool SymboledSection
<A
>::dontDeadStrip()
5303 case ld::Atom::typeInitializerPointers
:
5304 case ld::Atom::typeTerminatorPointers
:
5307 // model an object file without MH_SUBSECTIONS_VIA_SYMBOLS as one in which nothing can be dead stripped
5308 if ( ! this->_file
.canScatterAtoms() )
5311 return Section
<A
>::dontDeadStrip();
5317 template <typename A
>
5318 uint32_t SymboledSection
<A
>::computeAtomCount(class Parser
<A
>& parser
,
5319 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
5320 const struct Parser
<A
>::CFI_CU_InfoArrays
&)
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
>* sym
;
5330 while ( it
.next(parser
, *this, sectNum
, startAddr
, endAddr
, &addr
, &size
, &sym
) ) {
5333 //fprintf(stderr, "computeAtomCount(%s,%s) => %d\n", this->segmentName(), this->sectionName(), count);
5337 template <typename A
>
5338 uint32_t SymboledSection
<A
>::appendAtoms(class Parser
<A
>& parser
, uint8_t* p
,
5339 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
5340 const struct Parser
<A
>::CFI_CU_InfoArrays
&)
5342 this->_beginAtoms
= (Atom
<A
>*)p
;
5344 //fprintf(stderr, "SymboledSection::appendAtoms() in section %s\n", this->_machOSection->sectname());
5345 const pint_t startAddr
= this->_machOSection
->addr();
5346 const pint_t endAddr
= startAddr
+ this->_machOSection
->size();
5347 const uint32_t sectNum
= this->sectionNum(parser
);
5352 const macho_nlist
<P
>* label
;
5353 while ( it
.next(parser
, *this, sectNum
, startAddr
, endAddr
, &addr
, &size
, &label
) ) {
5354 Atom
<A
>* allocatedSpace
= (Atom
<A
>*)p
;
5355 // is break because of label or CFI?
5356 if ( label
!= NULL
) {
5357 // The size is computed based on the address of the next label (or the end of the section for the last label)
5358 // If there are two labels at the same address, we want them one to be an alias of the other.
5359 // If the label is at the end of a section, it is has zero size, but is not an alias
5360 const bool isAlias
= ( (size
== 0) && (addr
< endAddr
) );
5361 new (allocatedSpace
) Atom
<A
>(*this, parser
, *label
, size
, isAlias
);
5363 this->_hasAliases
= true;
5364 if ( parser
.altEntryFromSymbol(*label
) )
5365 this->_altEntries
.insert(allocatedSpace
);
5368 ld::Atom::SymbolTableInclusion inclusion
= ld::Atom::symbolTableNotIn
;
5369 ld::Atom::ContentType ctype
= this->contentType();
5370 if ( ctype
== ld::Atom::typeLSDA
)
5371 inclusion
= ld::Atom::symbolTableInWithRandomAutoStripLabel
;
5372 new (allocatedSpace
) Atom
<A
>(*this, "anon", addr
, size
, ld::Atom::definitionRegular
, ld::Atom::combineNever
,
5373 ld::Atom::scopeTranslationUnit
, ctype
, inclusion
,
5374 this->dontDeadStrip(), false, false, this->alignmentForAddress(addr
));
5376 p
+= sizeof(Atom
<A
>);
5380 this->_endAtoms
= (Atom
<A
>*)p
;
5386 ld::Atom::SymbolTableInclusion ImplicitSizeSection
<arm64
>::symbolTableInclusion()
5388 return ld::Atom::symbolTableInWithRandomAutoStripLabel
;
5392 template <typename A
>
5393 ld::Atom::SymbolTableInclusion ImplicitSizeSection
<A
>::symbolTableInclusion()
5395 return ld::Atom::symbolTableNotIn
;
5399 template <typename A
>
5400 uint32_t ImplicitSizeSection
<A
>::computeAtomCount(class Parser
<A
>& parser
,
5401 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
5402 const struct Parser
<A
>::CFI_CU_InfoArrays
&)
5405 const macho_section
<P
>* sect
= this->machoSection();
5406 const pint_t startAddr
= sect
->addr();
5407 const pint_t endAddr
= startAddr
+ sect
->size();
5408 for (pint_t addr
= startAddr
; addr
< endAddr
; addr
+= elementSizeAtAddress(addr
) ) {
5409 if ( useElementAt(parser
, it
, addr
) )
5412 if ( it
.fileHasOverlappingSymbols
&& (sect
->size() != 0) && (this->combine(parser
, startAddr
) == ld::Atom::combineByNameAndContent
) ) {
5413 // if there are multiple labels in this section for the same address, then clone them into multi atoms
5414 pint_t prevSymbolAddr
= (pint_t
)(-1);
5415 uint8_t prevSymbolSectNum
= 0;
5416 bool prevIgnore
= false;
5417 for(uint32_t i
=0; i
< it
.sortedSymbolCount
; ++i
) {
5418 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(it
.sortedSymbolIndexes
[i
]);
5419 const pint_t symbolAddr
= sym
.n_value();
5420 const uint8_t symbolSectNum
= sym
.n_sect();
5421 const bool ignore
= this->ignoreLabel(parser
.nameFromSymbol(sym
));
5422 if ( !ignore
&& !prevIgnore
&& (symbolAddr
== prevSymbolAddr
) && (prevSymbolSectNum
== symbolSectNum
) && (symbolSectNum
== this->sectionNum(parser
)) ) {
5425 prevSymbolAddr
= symbolAddr
;
5426 prevSymbolSectNum
= symbolSectNum
;
5427 prevIgnore
= ignore
;
5433 template <typename A
>
5434 uint32_t ImplicitSizeSection
<A
>::appendAtoms(class Parser
<A
>& parser
, uint8_t* p
,
5435 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
5436 const struct Parser
<A
>::CFI_CU_InfoArrays
&)
5438 this->_beginAtoms
= (Atom
<A
>*)p
;
5440 const macho_section
<P
>* sect
= this->machoSection();
5441 const pint_t startAddr
= sect
->addr();
5442 const pint_t endAddr
= startAddr
+ sect
->size();
5443 const uint32_t sectNum
= this->sectionNum(parser
);
5444 //fprintf(stderr, "ImplicitSizeSection::appendAtoms() in section %s\n", sect->sectname());
5448 const macho_nlist
<P
>* foundLabel
;
5449 Atom
<A
>* allocatedSpace
;
5450 while ( it
.next(parser
, *this, sectNum
, startAddr
, endAddr
, &foundAddr
, &size
, &foundLabel
) ) {
5451 if ( foundLabel
!= NULL
) {
5453 pint_t labeledAtomSize
= this->elementSizeAtAddress(foundAddr
);
5454 allocatedSpace
= (Atom
<A
>*)p
;
5455 if ( this->ignoreLabel(parser
.nameFromSymbol(*foundLabel
)) ) {
5457 // <rdar://problem/10018737>
5458 // a size of zero means there is another label at same location
5459 // and we are supposed to ignore this label
5463 //fprintf(stderr, " 0x%08llX make annon, size=%lld\n", (uint64_t)foundAddr, (uint64_t)size);
5464 new (allocatedSpace
) Atom
<A
>(*this, this->unlabeledAtomName(parser
, foundAddr
), foundAddr
,
5465 this->elementSizeAtAddress(foundAddr
), this->definition(),
5466 this->combine(parser
, foundAddr
), this->scopeAtAddress(parser
, foundAddr
),
5467 this->contentType(), this->symbolTableInclusion(),
5468 this->dontDeadStrip(), false, false, this->alignmentForAddress(foundAddr
));
5472 // make named atom for label
5473 //fprintf(stderr, " 0x%08llX make labeled\n", (uint64_t)foundAddr);
5474 new (allocatedSpace
) Atom
<A
>(*this, parser
, *foundLabel
, labeledAtomSize
);
5478 p
+= sizeof(Atom
<A
>);
5479 foundAddr
+= labeledAtomSize
;
5480 size
-= labeledAtomSize
;
5483 // some number of anonymous atoms
5484 for (pint_t addr
= foundAddr
; addr
< (foundAddr
+size
); addr
+= elementSizeAtAddress(addr
) ) {
5485 // make anon atoms for area before label
5486 if ( this->useElementAt(parser
, it
, addr
) ) {
5487 //fprintf(stderr, " 0x%08llX make annon, size=%lld\n", (uint64_t)addr, (uint64_t)elementSizeAtAddress(addr));
5488 allocatedSpace
= (Atom
<A
>*)p
;
5489 new (allocatedSpace
) Atom
<A
>(*this, this->unlabeledAtomName(parser
, addr
), addr
, this->elementSizeAtAddress(addr
),
5490 this->definition(), this->combine(parser
, addr
), this->scopeAtAddress(parser
, addr
),
5491 this->contentType(), this->symbolTableInclusion(),
5492 this->dontDeadStrip(), false, false, this->alignmentForAddress(addr
));
5494 p
+= sizeof(Atom
<A
>);
5499 this->_endAtoms
= (Atom
<A
>*)p
;
5504 template <typename A
>
5505 bool Literal4Section
<A
>::ignoreLabel(const char* label
) const
5507 return (label
[0] == 'L') || (label
[0] == 'l');
5510 template <typename A
>
5511 unsigned long Literal4Section
<A
>::contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
5513 const uint32_t* literalContent
= (uint32_t*)atom
->contentPointer();
5514 return *literalContent
;
5517 template <typename A
>
5518 bool Literal4Section
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
5519 const ld::IndirectBindingTable
& ind
) const
5521 assert(this->type() == rhs
.section().type());
5522 const uint32_t* literalContent
= (uint32_t*)atom
->contentPointer();
5524 const Atom
<A
>* rhsAtom
= dynamic_cast<const Atom
<A
>*>(&rhs
);
5525 assert(rhsAtom
!= NULL
);
5526 if ( rhsAtom
!= NULL
) {
5527 const uint32_t* rhsLiteralContent
= (uint32_t*)rhsAtom
->contentPointer();
5528 return (*literalContent
== *rhsLiteralContent
);
5534 template <typename A
>
5535 bool Literal8Section
<A
>::ignoreLabel(const char* label
) const
5537 return (label
[0] == 'L') || (label
[0] == 'l');
5540 template <typename A
>
5541 unsigned long Literal8Section
<A
>::contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
5544 const uint64_t* literalContent
= (uint64_t*)atom
->contentPointer();
5545 return *literalContent
;
5547 unsigned long hash
= 5381;
5548 const uint8_t* byteContent
= atom
->contentPointer();
5549 for (int i
=0; i
< 8; ++i
) {
5550 hash
= hash
* 33 + byteContent
[i
];
5556 template <typename A
>
5557 bool Literal8Section
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
5558 const ld::IndirectBindingTable
& ind
) const
5560 if ( rhs
.section().type() != ld::Section::typeLiteral8
)
5562 assert(this->type() == rhs
.section().type());
5563 const uint64_t* literalContent
= (uint64_t*)atom
->contentPointer();
5565 const Atom
<A
>* rhsAtom
= dynamic_cast<const Atom
<A
>*>(&rhs
);
5566 assert(rhsAtom
!= NULL
);
5567 if ( rhsAtom
!= NULL
) {
5568 const uint64_t* rhsLiteralContent
= (uint64_t*)rhsAtom
->contentPointer();
5569 return (*literalContent
== *rhsLiteralContent
);
5574 template <typename A
>
5575 bool Literal16Section
<A
>::ignoreLabel(const char* label
) const
5577 return (label
[0] == 'L') || (label
[0] == 'l');
5580 template <typename A
>
5581 unsigned long Literal16Section
<A
>::contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
5583 unsigned long hash
= 5381;
5584 const uint8_t* byteContent
= atom
->contentPointer();
5585 for (int i
=0; i
< 16; ++i
) {
5586 hash
= hash
* 33 + byteContent
[i
];
5591 template <typename A
>
5592 bool Literal16Section
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
5593 const ld::IndirectBindingTable
& ind
) const
5595 if ( rhs
.section().type() != ld::Section::typeLiteral16
)
5597 assert(this->type() == rhs
.section().type());
5598 const uint64_t* literalContent
= (uint64_t*)atom
->contentPointer();
5600 const Atom
<A
>* rhsAtom
= dynamic_cast<const Atom
<A
>*>(&rhs
);
5601 assert(rhsAtom
!= NULL
);
5602 if ( rhsAtom
!= NULL
) {
5603 const uint64_t* rhsLiteralContent
= (uint64_t*)rhsAtom
->contentPointer();
5604 return ((literalContent
[0] == rhsLiteralContent
[0]) && (literalContent
[1] == rhsLiteralContent
[1]));
5611 template <typename A
>
5612 typename
A::P::uint_t CStringSection
<A
>::elementSizeAtAddress(pint_t addr
)
5614 const macho_section
<P
>* sect
= this->machoSection();
5615 const char* stringContent
= (char*)(this->file().fileContent() + sect
->offset() + addr
- sect
->addr());
5616 return strlen(stringContent
) + 1;
5619 template <typename A
>
5620 bool CStringSection
<A
>::useElementAt(Parser
<A
>& parser
, struct Parser
<A
>::LabelAndCFIBreakIterator
& it
, pint_t addr
)
5625 template <typename A
>
5626 bool CStringSection
<A
>::ignoreLabel(const char* label
) const
5628 return (label
[0] == 'L') || (label
[0] == 'l');
5632 template <typename A
>
5633 Atom
<A
>* CStringSection
<A
>::findAtomByAddress(pint_t addr
)
5635 Atom
<A
>* result
= this->findContentAtomByAddress(addr
, this->_beginAtoms
, this->_endAtoms
);
5639 template <typename A
>
5640 unsigned long CStringSection
<A
>::contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
5642 unsigned long hash
= 5381;
5643 const char* stringContent
= (char*)atom
->contentPointer();
5644 for (const char* s
= stringContent
; *s
!= '\0'; ++s
) {
5645 hash
= hash
* 33 + *s
;
5651 template <typename A
>
5652 bool CStringSection
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
5653 const ld::IndirectBindingTable
& ind
) const
5655 if ( rhs
.section().type() != ld::Section::typeCString
)
5657 assert(this->type() == rhs
.section().type());
5658 assert(strcmp(this->sectionName(), rhs
.section().sectionName())== 0);
5659 assert(strcmp(this->segmentName(), rhs
.section().segmentName())== 0);
5660 const char* stringContent
= (char*)atom
->contentPointer();
5662 const Atom
<A
>* rhsAtom
= dynamic_cast<const Atom
<A
>*>(&rhs
);
5663 assert(rhsAtom
!= NULL
);
5664 if ( rhsAtom
!= NULL
) {
5665 if ( atom
->_size
!= rhsAtom
->_size
)
5667 const char* rhsStringContent
= (char*)rhsAtom
->contentPointer();
5668 return (strcmp(stringContent
, rhsStringContent
) == 0);
5675 ld::Fixup::Kind NonLazyPointerSection
<x86
>::fixupKind()
5677 return ld::Fixup::kindStoreLittleEndian32
;
5681 ld::Fixup::Kind NonLazyPointerSection
<arm
>::fixupKind()
5683 return ld::Fixup::kindStoreLittleEndian32
;
5687 ld::Fixup::Kind NonLazyPointerSection
<arm64
>::fixupKind()
5689 return ld::Fixup::kindStoreLittleEndian64
;
5694 void NonLazyPointerSection
<x86_64
>::makeFixups(class Parser
<x86_64
>& parser
, const struct Parser
<x86_64
>::CFI_CU_InfoArrays
&)
5696 assert(0 && "x86_64 should not have non-lazy-pointer sections in .o files");
5699 template <typename A
>
5700 void NonLazyPointerSection
<A
>::makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
&)
5702 // add references for each NLP atom based on indirect symbol table
5703 const macho_section
<P
>* sect
= this->machoSection();
5704 const pint_t endAddr
= sect
->addr() + sect
->size();
5705 for( pint_t addr
= sect
->addr(); addr
< endAddr
; addr
+= sizeof(pint_t
)) {
5706 typename Parser
<A
>::SourceLocation src
;
5707 typename Parser
<A
>::TargetDesc target
;
5708 src
.atom
= this->findAtomByAddress(addr
);
5709 src
.offsetInAtom
= 0;
5710 uint32_t symIndex
= parser
.symbolIndexFromIndirectSectionAddress(addr
, sect
);
5713 target
.weakImport
= false;
5715 if ( symIndex
== INDIRECT_SYMBOL_LOCAL
) {
5716 // use direct reference for local symbols
5717 const pint_t
* nlpContent
= (pint_t
*)(this->file().fileContent() + sect
->offset() + addr
- sect
->addr());
5718 pint_t targetAddr
= P::getP(*nlpContent
);
5719 target
.atom
= parser
.findAtomByAddress(targetAddr
);
5720 target
.weakImport
= false;
5721 target
.addend
= (targetAddr
- target
.atom
->objectAddress());
5722 // <rdar://problem/8385011> if pointer to thumb function, mask of thumb bit (not an addend of +1)
5723 if ( target
.atom
->isThumb() )
5724 target
.addend
&= (-2);
5725 assert(src
.atom
->combine() == ld::Atom::combineNever
);
5728 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(symIndex
);
5729 // use direct reference for local symbols
5730 if ( ((sym
.n_type() & N_TYPE
) == N_SECT
) && ((sym
.n_type() & N_EXT
) == 0) ) {
5731 parser
.findTargetFromAddressAndSectionNum(sym
.n_value(), sym
.n_sect(), target
);
5732 assert(src
.atom
->combine() == ld::Atom::combineNever
);
5735 target
.name
= parser
.nameFromSymbol(sym
);
5736 target
.weakImport
= parser
.weakImportFromSymbol(sym
);
5737 assert(src
.atom
->combine() == ld::Atom::combineByNameAndReferences
);
5740 parser
.addFixups(src
, this->fixupKind(), target
);
5744 template <typename A
>
5745 ld::Atom::Combine NonLazyPointerSection
<A
>::combine(Parser
<A
>& parser
, pint_t addr
)
5747 const macho_section
<P
>* sect
= this->machoSection();
5748 uint32_t symIndex
= parser
.symbolIndexFromIndirectSectionAddress(addr
, sect
);
5749 if ( symIndex
== INDIRECT_SYMBOL_LOCAL
)
5750 return ld::Atom::combineNever
;
5752 // don't coalesce non-lazy-pointers to local symbols
5753 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(symIndex
);
5754 if ( ((sym
.n_type() & N_TYPE
) == N_SECT
) && ((sym
.n_type() & N_EXT
) == 0) )
5755 return ld::Atom::combineNever
;
5757 return ld::Atom::combineByNameAndReferences
;
5760 template <typename A
>
5761 const char* NonLazyPointerSection
<A
>::targetName(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
)
5763 assert(atom
->combine() == ld::Atom::combineByNameAndReferences
);
5764 assert(atom
->fixupCount() == 1);
5765 ld::Fixup::iterator fit
= atom
->fixupsBegin();
5766 const char* name
= NULL
;
5767 switch ( fit
->binding
) {
5768 case ld::Fixup::bindingByNameUnbound
:
5771 case ld::Fixup::bindingByContentBound
:
5772 name
= fit
->u
.target
->name();
5774 case ld::Fixup::bindingsIndirectlyBound
:
5775 name
= ind
.indirectName(fit
->u
.bindingIndex
);
5780 assert(name
!= NULL
);
5784 template <typename A
>
5785 unsigned long NonLazyPointerSection
<A
>::contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
5787 assert(atom
->combine() == ld::Atom::combineByNameAndReferences
);
5788 unsigned long hash
= 9508;
5789 for (const char* s
= this->targetName(atom
, ind
); *s
!= '\0'; ++s
) {
5790 hash
= hash
* 33 + *s
;
5795 template <typename A
>
5796 bool NonLazyPointerSection
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
5797 const ld::IndirectBindingTable
& indirectBindingTable
) const
5799 if ( rhs
.section().type() != ld::Section::typeNonLazyPointer
)
5801 assert(this->type() == rhs
.section().type());
5802 // there can be many non-lazy pointer in different section names
5803 // we only want to coalesce in same section name
5804 if ( *this != rhs
.section() )
5806 const Atom
<A
>* rhsAtom
= dynamic_cast<const Atom
<A
>*>(&rhs
);
5807 assert(rhsAtom
!= NULL
);
5808 const char* thisName
= this->targetName(atom
, indirectBindingTable
);
5809 const char* rhsName
= this->targetName(rhsAtom
, indirectBindingTable
);
5810 return (strcmp(thisName
, rhsName
) == 0);
5813 template <typename A
>
5814 ld::Atom::Scope NonLazyPointerSection
<A
>::scopeAtAddress(Parser
<A
>& parser
, pint_t addr
)
5816 const macho_section
<P
>* sect
= this->machoSection();
5817 uint32_t symIndex
= parser
.symbolIndexFromIndirectSectionAddress(addr
, sect
);
5818 if ( symIndex
== INDIRECT_SYMBOL_LOCAL
)
5819 return ld::Atom::scopeTranslationUnit
;
5821 return ld::Atom::scopeLinkageUnit
;
5826 template <typename A
>
5827 ld::Atom::Combine TLVPointerSection
<A
>::combine(Parser
<A
>& parser
, pint_t addr
)
5829 return ld::Atom::combineByNameAndReferences
;
5833 void TLVPointerSection
<arm
>::makeFixups(class Parser
<arm
>& parser
, const struct Parser
<arm
>::CFI_CU_InfoArrays
&)
5835 // add references for each thread local pointer atom based on indirect symbol table
5836 const macho_section
<P
>* sect
= this->machoSection();
5837 const pint_t endAddr
= sect
->addr() + sect
->size();
5838 for (pint_t addr
= sect
->addr(); addr
< endAddr
; addr
+= sizeof(pint_t
)) {
5839 typename Parser
<arm
>::SourceLocation src
;
5840 typename Parser
<arm
>::TargetDesc target
;
5841 src
.atom
= this->findAtomByAddress(addr
);
5842 src
.offsetInAtom
= 0;
5843 uint32_t symIndex
= parser
.symbolIndexFromIndirectSectionAddress(addr
, sect
);
5846 target
.weakImport
= false;
5848 if ( symIndex
== INDIRECT_SYMBOL_LOCAL
) {
5849 throwf("unexpected INDIRECT_SYMBOL_LOCAL in section %s", this->sectionName());
5852 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(symIndex
);
5853 // use direct reference for local symbols
5854 if ( ((sym
.n_type() & N_TYPE
) == N_SECT
) && ((sym
.n_type() & N_EXT
) == 0) ) {
5855 throwf("unexpected pointer to local symbol in section %s", this->sectionName());
5858 target
.name
= parser
.nameFromSymbol(sym
);
5859 target
.weakImport
= parser
.weakImportFromSymbol(sym
);
5860 assert(src
.atom
->combine() == ld::Atom::combineByNameAndReferences
);
5863 parser
.addFixups(src
, ld::Fixup::kindStoreLittleEndian32
, target
);
5867 template <typename A
>
5868 void TLVPointerSection
<A
>::makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
&)
5870 assert(0 && "should not have thread-local-pointer sections in .o files");
5874 template <typename A
>
5875 const char* TLVPointerSection
<A
>::targetName(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
, bool* isStatic
)
5877 assert(atom
->combine() == ld::Atom::combineByNameAndReferences
);
5878 assert(atom
->fixupCount() == 1);
5880 ld::Fixup::iterator fit
= atom
->fixupsBegin();
5881 const char* name
= NULL
;
5882 switch ( fit
->binding
) {
5883 case ld::Fixup::bindingByNameUnbound
:
5886 case ld::Fixup::bindingByContentBound
:
5887 name
= fit
->u
.target
->name();
5889 case ld::Fixup::bindingsIndirectlyBound
:
5890 name
= ind
.indirectName(fit
->u
.bindingIndex
);
5892 case ld::Fixup::bindingDirectlyBound
:
5893 name
= fit
->u
.target
->name();
5894 *isStatic
= (fit
->u
.target
->scope() == ld::Atom::scopeTranslationUnit
);
5899 assert(name
!= NULL
);
5903 template <typename A
>
5904 unsigned long TLVPointerSection
<A
>::contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
5906 assert(atom
->combine() == ld::Atom::combineByNameAndReferences
);
5907 unsigned long hash
= 9508;
5909 for (const char* s
= this->targetName(atom
, ind
, &isStatic
); *s
!= '\0'; ++s
) {
5910 hash
= hash
* 33 + *s
;
5915 template <typename A
>
5916 bool TLVPointerSection
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
5917 const ld::IndirectBindingTable
& indirectBindingTable
) const
5919 if ( rhs
.section().type() != ld::Section::typeTLVPointers
)
5921 assert(this->type() == rhs
.section().type());
5922 const Atom
<A
>* rhsAtom
= dynamic_cast<const Atom
<A
>*>(&rhs
);
5923 assert(rhsAtom
!= NULL
);
5926 const char* thisName
= this->targetName(atom
, indirectBindingTable
, &thisIsStatic
);
5927 const char* rhsName
= this->targetName(rhsAtom
, indirectBindingTable
, &rhsIsStatic
);
5928 return !thisIsStatic
&& !rhsIsStatic
&& (strcmp(thisName
, rhsName
) == 0);
5932 template <typename A
>
5933 const uint8_t* CFStringSection
<A
>::targetContent(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
,
5934 ContentType
* ct
, unsigned int* count
)
5936 *ct
= contentUnknown
;
5937 for (ld::Fixup::iterator fit
=atom
->fixupsBegin(), end
=atom
->fixupsEnd(); fit
!= end
; ++fit
) {
5938 const ld::Atom
* targetAtom
= NULL
;
5939 switch ( fit
->binding
) {
5940 case ld::Fixup::bindingByNameUnbound
:
5941 // ignore reference to ___CFConstantStringClassReference
5942 // we are just looking for reference to backing string data
5943 assert(fit
->offsetInAtom
== 0);
5944 assert(strcmp(fit
->u
.name
, "___CFConstantStringClassReference") == 0);
5946 case ld::Fixup::bindingDirectlyBound
:
5947 case ld::Fixup::bindingByContentBound
:
5948 targetAtom
= fit
->u
.target
;
5950 case ld::Fixup::bindingsIndirectlyBound
:
5951 targetAtom
= ind
.indirectAtom(fit
->u
.bindingIndex
);
5954 assert(0 && "bad binding type");
5956 assert(targetAtom
!= NULL
);
5957 const Atom
<A
>* target
= dynamic_cast<const Atom
<A
>*>(targetAtom
);
5958 if ( targetAtom
->section().type() == ld::Section::typeCString
) {
5960 *count
= targetAtom
->size();
5962 else if ( targetAtom
->section().type() == ld::Section::typeUTF16Strings
) {
5964 *count
= (targetAtom
->size()+1)/2; // round up incase of buggy compiler that has only one trailing zero byte
5967 *ct
= contentUnknown
;
5971 return target
->contentPointer();
5977 template <typename A
>
5978 unsigned long CFStringSection
<A
>::contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
5980 // base hash of CFString on hash of cstring it wraps
5983 unsigned int charCount
;
5984 const uint8_t* content
= this->targetContent(atom
, ind
, &cType
, &charCount
);
5988 for (const char* s
= (char*)content
; *s
!= '\0'; ++s
) {
5989 hash
= hash
* 33 + *s
;
5994 --charCount
; // don't add last 0x0000 to hash because some buggy compilers only have trailing single byte
5995 for (const uint16_t* s
= (uint16_t*)content
; charCount
> 0; ++s
, --charCount
) {
5996 hash
= hash
* 1025 + *s
;
5999 case contentUnknown
:
6000 // <rdar://problem/14134211> For malformed CFStrings, hash to address of atom so they have unique hashes
6001 return ULONG_MAX
- (unsigned long)(atom
);
6007 template <typename A
>
6008 bool CFStringSection
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
6009 const ld::IndirectBindingTable
& indirectBindingTable
) const
6013 if ( rhs
.section().type() != ld::Section::typeCFString
)
6015 assert(this->type() == rhs
.section().type());
6016 assert(strcmp(this->sectionName(), "__cfstring") == 0);
6018 ContentType thisType
;
6019 unsigned int charCount
;
6020 const uint8_t* cstringContent
= this->targetContent(atom
, indirectBindingTable
, &thisType
, &charCount
);
6021 ContentType rhsType
;
6022 const Atom
<A
>* rhsAtom
= dynamic_cast<const Atom
<A
>*>(&rhs
);
6023 assert(rhsAtom
!= NULL
);
6024 unsigned int rhsCharCount
;
6025 const uint8_t* rhsStringContent
= this->targetContent(rhsAtom
, indirectBindingTable
, &rhsType
, &rhsCharCount
);
6027 if ( thisType
!= rhsType
)
6030 if ( thisType
== contentUnknown
)
6033 if ( rhsType
== contentUnknown
)
6036 // no need to compare content of pointers are already the same
6037 if ( cstringContent
== rhsStringContent
)
6040 // no need to compare content if size is different
6041 if ( charCount
!= rhsCharCount
)
6044 switch ( thisType
) {
6046 return (strcmp((char*)cstringContent
, (char*)rhsStringContent
) == 0);
6049 const uint16_t* cstringContent16
= (uint16_t*)cstringContent
;
6050 const uint16_t* rhsStringContent16
= (uint16_t*)rhsStringContent
;
6051 for (unsigned int i
= 0; i
< charCount
; ++i
) {
6052 if ( cstringContent16
[i
] != rhsStringContent16
[i
] )
6057 case contentUnknown
:
6064 template <typename A
>
6065 typename
A::P::uint_t ObjC1ClassSection
<A
>::elementSizeAtAddress(pint_t addr
)
6067 // nominal size for each class is 48 bytes, but sometimes the compiler
6068 // over aligns and there is padding after class data
6069 const macho_section
<P
>* sct
= this->machoSection();
6070 uint32_t align
= 1 << sct
->align();
6071 uint32_t size
= ((12 * sizeof(pint_t
)) + align
-1) & (-align
);
6075 template <typename A
>
6076 const char* ObjC1ClassSection
<A
>::unlabeledAtomName(Parser
<A
>& parser
, pint_t addr
)
6078 // 8-bytes into class object is pointer to class name
6079 const macho_section
<P
>* sct
= this->machoSection();
6080 uint32_t classObjcFileOffset
= sct
->offset() - sct
->addr() + addr
;
6081 const uint8_t* mappedFileContent
= this->file().fileContent();
6082 pint_t nameAddr
= P::getP(*((pint_t
*)(mappedFileContent
+classObjcFileOffset
+2*sizeof(pint_t
))));
6084 // find section containing string address to get string bytes
6085 const macho_section
<P
>* const sections
= parser
.firstMachOSection();
6086 const uint32_t sectionCount
= parser
.machOSectionCount();
6087 for (uint32_t i
=0; i
< sectionCount
; ++i
) {
6088 const macho_section
<P
>* aSect
= §ions
[i
];
6089 if ( (aSect
->addr() <= nameAddr
) && (nameAddr
< (aSect
->addr()+aSect
->size())) ) {
6090 assert((aSect
->flags() & SECTION_TYPE
) == S_CSTRING_LITERALS
);
6091 uint32_t nameFileOffset
= aSect
->offset() - aSect
->addr() + nameAddr
;
6092 const char* name
= (char*)mappedFileContent
+ nameFileOffset
;
6093 // spin through symbol table to find absolute symbol corresponding to this class
6094 for (uint32_t s
=0; s
< parser
.symbolCount(); ++s
) {
6095 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(s
);
6096 if ( (sym
.n_type() & N_TYPE
) != N_ABS
)
6098 const char* absName
= parser
.nameFromSymbol(sym
);
6099 if ( strncmp(absName
, ".objc_class_name_", 17) == 0 ) {
6100 if ( strcmp(&absName
[17], name
) == 0 )
6104 assert(0 && "obj class name not found in symbol table");
6107 assert(0 && "obj class name not found");
6108 return "unknown objc class";
6112 template <typename A
>
6113 const char* ObjC2ClassRefsSection
<A
>::targetClassName(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
6115 assert(atom
->fixupCount() == 1);
6116 ld::Fixup::iterator fit
= atom
->fixupsBegin();
6117 const char* className
= NULL
;
6118 switch ( fit
->binding
) {
6119 case ld::Fixup::bindingByNameUnbound
:
6120 className
= fit
->u
.name
;
6122 case ld::Fixup::bindingDirectlyBound
:
6123 case ld::Fixup::bindingByContentBound
:
6124 className
= fit
->u
.target
->name();
6126 case ld::Fixup::bindingsIndirectlyBound
:
6127 className
= ind
.indirectName(fit
->u
.bindingIndex
);
6130 assert(0 && "unsupported binding in objc2 class ref section");
6132 assert(className
!= NULL
);
6137 template <typename A
>
6138 unsigned long ObjC2ClassRefsSection
<A
>::contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
6140 unsigned long hash
= 978;
6141 for (const char* s
= targetClassName(atom
, ind
); *s
!= '\0'; ++s
) {
6142 hash
= hash
* 33 + *s
;
6147 template <typename A
>
6148 bool ObjC2ClassRefsSection
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
6149 const ld::IndirectBindingTable
& indirectBindingTable
) const
6151 assert(this->type() == rhs
.section().type());
6152 const Atom
<A
>* rhsAtom
= dynamic_cast<const Atom
<A
>*>(&rhs
);
6153 assert(rhsAtom
!= NULL
);
6154 const char* thisClassName
= targetClassName(atom
, indirectBindingTable
);
6155 const char* rhsClassName
= targetClassName(rhsAtom
, indirectBindingTable
);
6156 return (strcmp(thisClassName
, rhsClassName
) == 0);
6160 template <typename A
>
6161 const char* Objc1ClassReferences
<A
>::targetCString(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
6163 assert(atom
->fixupCount() == 2);
6164 ld::Fixup::iterator fit
= atom
->fixupsBegin();
6165 if ( fit
->kind
== ld::Fixup::kindSetTargetAddress
)
6167 const ld::Atom
* targetAtom
= NULL
;
6168 switch ( fit
->binding
) {
6169 case ld::Fixup::bindingByContentBound
:
6170 targetAtom
= fit
->u
.target
;
6172 case ld::Fixup::bindingsIndirectlyBound
:
6173 targetAtom
= ind
.indirectAtom(fit
->u
.bindingIndex
);
6174 if ( targetAtom
== NULL
) {
6175 fprintf(stderr
, "missing target named %s\n", ind
.indirectName(fit
->u
.bindingIndex
));
6181 assert(targetAtom
!= NULL
);
6182 const Atom
<A
>* target
= dynamic_cast<const Atom
<A
>*>(targetAtom
);
6183 assert(target
!= NULL
);
6184 return (char*)target
->contentPointer();
6188 template <typename A
>
6189 const char* PointerToCStringSection
<A
>::targetCString(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
6191 assert(atom
->fixupCount() == 1);
6192 ld::Fixup::iterator fit
= atom
->fixupsBegin();
6193 const ld::Atom
* targetAtom
= NULL
;
6194 switch ( fit
->binding
) {
6195 case ld::Fixup::bindingByContentBound
:
6196 targetAtom
= fit
->u
.target
;
6198 case ld::Fixup::bindingsIndirectlyBound
:
6199 targetAtom
= ind
.indirectAtom(fit
->u
.bindingIndex
);
6201 case ld::Fixup::bindingDirectlyBound
:
6202 targetAtom
= fit
->u
.target
;
6205 assert(0 && "unsupported reference to selector");
6207 assert(targetAtom
!= NULL
);
6208 const Atom
<A
>* target
= dynamic_cast<const Atom
<A
>*>(targetAtom
);
6209 assert(target
!= NULL
);
6210 assert(target
->contentType() == ld::Atom::typeCString
);
6211 return (char*)target
->contentPointer();
6214 template <typename A
>
6215 unsigned long PointerToCStringSection
<A
>::contentHash(const class Atom
<A
>* atom
,
6216 const ld::IndirectBindingTable
& indirectBindingTable
) const
6218 // make hash from section name and target cstring name
6219 unsigned long hash
= 123;
6220 for (const char* s
= this->sectionName(); *s
!= '\0'; ++s
) {
6221 hash
= hash
* 33 + *s
;
6223 for (const char* s
= this->targetCString(atom
, indirectBindingTable
); *s
!= '\0'; ++s
) {
6224 hash
= hash
* 33 + *s
;
6229 template <typename A
>
6230 bool PointerToCStringSection
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
6231 const ld::IndirectBindingTable
& indirectBindingTable
) const
6233 assert(this->type() == rhs
.section().type());
6234 // there can be pointers-to-cstrings in different section names
6235 // we only want to coalesce in same section name
6236 if ( *this != rhs
.section() )
6239 // get string content for this
6240 const char* cstringContent
= this->targetCString(atom
, indirectBindingTable
);
6241 const Atom
<A
>* rhsAtom
= dynamic_cast<const Atom
<A
>*>(&rhs
);
6242 assert(rhsAtom
!= NULL
);
6243 const char* rhsCstringContent
= this->targetCString(rhsAtom
, indirectBindingTable
);
6245 assert(cstringContent
!= NULL
);
6246 assert(rhsCstringContent
!= NULL
);
6247 return (strcmp(cstringContent
, rhsCstringContent
) == 0);
6252 template <typename A
>
6253 unsigned long UTF16StringSection
<A
>::contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
6255 unsigned long hash
= 5381;
6256 const uint16_t* stringContent
= (uint16_t*)atom
->contentPointer();
6257 // some buggy compilers end utf16 data with single byte, so don't use last word in hash computation
6258 unsigned int count
= (atom
->size()/2) - 1;
6259 for (const uint16_t* s
= stringContent
; count
> 0; ++s
, --count
) {
6260 hash
= hash
* 33 + *s
;
6265 template <typename A
>
6266 bool UTF16StringSection
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
6267 const ld::IndirectBindingTable
& ind
) const
6269 if ( rhs
.section().type() != ld::Section::typeUTF16Strings
)
6282 uint32_t Section
<x86_64
>::x86_64PcRelOffset(uint8_t r_type
)
6285 case X86_64_RELOC_SIGNED
:
6287 case X86_64_RELOC_SIGNED_1
:
6289 case X86_64_RELOC_SIGNED_2
:
6291 case X86_64_RELOC_SIGNED_4
:
6299 bool Section
<x86_64
>::addRelocFixup(class Parser
<x86_64
>& parser
, const macho_relocation_info
<P
>* reloc
)
6301 const macho_section
<P
>* sect
= this->machoSection();
6302 if ( sect
== NULL
) {
6303 warning("malformed mach-o, relocations not supported on section %s", this->sectionName());
6306 uint64_t srcAddr
= sect
->addr() + reloc
->r_address();
6307 Parser
<x86_64
>::SourceLocation src
;
6308 Parser
<x86_64
>::TargetDesc target
;
6309 Parser
<x86_64
>::TargetDesc toTarget
;
6310 src
.atom
= this->findAtomByAddress(srcAddr
);
6311 src
.offsetInAtom
= srcAddr
- src
.atom
->_objAddress
;
6312 const uint8_t* fixUpPtr
= file().fileContent() + sect
->offset() + reloc
->r_address();
6313 uint64_t contentValue
= 0;
6314 const macho_relocation_info
<x86_64::P
>* nextReloc
= &reloc
[1];
6315 bool result
= false;
6316 bool useDirectBinding
;
6317 switch ( reloc
->r_length() ) {
6319 contentValue
= *fixUpPtr
;
6322 contentValue
= (int64_t)(int16_t)E::get16(*((uint16_t*)fixUpPtr
));
6325 contentValue
= (int64_t)(int32_t)E::get32(*((uint32_t*)fixUpPtr
));
6328 contentValue
= E::get64(*((uint64_t*)fixUpPtr
));
6333 target
.weakImport
= false;
6335 if ( reloc
->r_extern() ) {
6336 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(reloc
->r_symbolnum());
6337 // use direct reference for local symbols
6338 if ( ((sym
.n_type() & N_TYPE
) == N_SECT
) && (((sym
.n_type() & N_EXT
) == 0) || (parser
.nameFromSymbol(sym
)[0] == 'L')) ) {
6339 parser
.findTargetFromAddressAndSectionNum(sym
.n_value(), sym
.n_sect(), target
);
6340 target
.addend
+= contentValue
;
6343 target
.name
= parser
.nameFromSymbol(sym
);
6344 target
.weakImport
= parser
.weakImportFromSymbol(sym
);
6345 target
.addend
= contentValue
;
6347 // cfstrings should always use direct reference to backing store
6348 if ( (this->type() == ld::Section::typeCFString
) && (src
.offsetInAtom
!= 0) ) {
6349 parser
.findTargetFromAddressAndSectionNum(sym
.n_value(), sym
.n_sect(), target
);
6350 target
.addend
= contentValue
;
6354 if ( reloc
->r_pcrel() )
6355 contentValue
+= srcAddr
+ x86_64PcRelOffset(reloc
->r_type());
6356 parser
.findTargetFromAddressAndSectionNum(contentValue
, reloc
->r_symbolnum(), target
);
6358 switch ( reloc
->r_type() ) {
6359 case X86_64_RELOC_UNSIGNED
:
6360 if ( reloc
->r_pcrel() )
6361 throw "pcrel and X86_64_RELOC_UNSIGNED not supported";
6362 switch ( reloc
->r_length() ) {
6365 throw "length < 2 and X86_64_RELOC_UNSIGNED not supported";
6367 parser
.addFixups(src
, ld::Fixup::kindStoreLittleEndian32
, target
);
6370 parser
.addFixups(src
, ld::Fixup::kindStoreLittleEndian64
, target
);
6374 case X86_64_RELOC_SIGNED
:
6375 case X86_64_RELOC_SIGNED_1
:
6376 case X86_64_RELOC_SIGNED_2
:
6377 case X86_64_RELOC_SIGNED_4
:
6378 if ( ! reloc
->r_pcrel() )
6379 throw "not pcrel and X86_64_RELOC_SIGNED* not supported";
6380 if ( reloc
->r_length() != 2 )
6381 throw "length != 2 and X86_64_RELOC_SIGNED* not supported";
6382 switch ( reloc
->r_type() ) {
6383 case X86_64_RELOC_SIGNED
:
6384 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel32
, target
);
6386 case X86_64_RELOC_SIGNED_1
:
6387 if ( reloc
->r_extern() )
6389 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel32_1
, target
);
6391 case X86_64_RELOC_SIGNED_2
:
6392 if ( reloc
->r_extern() )
6394 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel32_2
, target
);
6396 case X86_64_RELOC_SIGNED_4
:
6397 if ( reloc
->r_extern() )
6399 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel32_4
, target
);
6403 case X86_64_RELOC_BRANCH
:
6404 if ( ! reloc
->r_pcrel() )
6405 throw "not pcrel and X86_64_RELOC_BRANCH not supported";
6406 switch ( reloc
->r_length() ) {
6408 if ( (target
.name
!= NULL
) && (strncmp(target
.name
, "___dtrace_probe$", 16) == 0) ) {
6409 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindStoreX86DtraceCallSiteNop
, false, target
.name
);
6410 parser
.addDtraceExtraInfos(src
, &target
.name
[16]);
6412 else if ( (target
.name
!= NULL
) && (strncmp(target
.name
, "___dtrace_isenabled$", 20) == 0) ) {
6413 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindStoreX86DtraceIsEnableSiteClear
, false, target
.name
);
6414 parser
.addDtraceExtraInfos(src
, &target
.name
[20]);
6417 parser
.addFixups(src
, ld::Fixup::kindStoreX86BranchPCRel32
, target
);
6421 parser
.addFixups(src
, ld::Fixup::kindStoreX86BranchPCRel8
, target
);
6424 throwf("length=%d and X86_64_RELOC_BRANCH not supported", reloc
->r_length());
6427 case X86_64_RELOC_GOT
:
6428 if ( ! reloc
->r_extern() )
6429 throw "not extern and X86_64_RELOC_GOT not supported";
6430 if ( ! reloc
->r_pcrel() )
6431 throw "not pcrel and X86_64_RELOC_GOT not supported";
6432 if ( reloc
->r_length() != 2 )
6433 throw "length != 2 and X86_64_RELOC_GOT not supported";
6434 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel32GOT
, target
);
6436 case X86_64_RELOC_GOT_LOAD
:
6437 if ( ! reloc
->r_extern() )
6438 throw "not extern and X86_64_RELOC_GOT_LOAD not supported";
6439 if ( ! reloc
->r_pcrel() )
6440 throw "not pcrel and X86_64_RELOC_GOT_LOAD not supported";
6441 if ( reloc
->r_length() != 2 )
6442 throw "length != 2 and X86_64_RELOC_GOT_LOAD not supported";
6443 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel32GOTLoad
, target
);
6445 case X86_64_RELOC_SUBTRACTOR
:
6446 if ( reloc
->r_pcrel() )
6447 throw "X86_64_RELOC_SUBTRACTOR cannot be pc-relative";
6448 if ( reloc
->r_length() < 2 )
6449 throw "X86_64_RELOC_SUBTRACTOR must have r_length of 2 or 3";
6450 if ( !reloc
->r_extern() )
6451 throw "X86_64_RELOC_SUBTRACTOR must have r_extern=1";
6452 if ( nextReloc
->r_type() != X86_64_RELOC_UNSIGNED
)
6453 throw "X86_64_RELOC_SUBTRACTOR must be followed by X86_64_RELOC_UNSIGNED";
6455 if ( nextReloc
->r_pcrel() )
6456 throw "X86_64_RELOC_UNSIGNED following a X86_64_RELOC_SUBTRACTOR cannot be pc-relative";
6457 if ( nextReloc
->r_length() != reloc
->r_length() )
6458 throw "X86_64_RELOC_UNSIGNED following a X86_64_RELOC_SUBTRACTOR must have same r_length";
6459 if ( nextReloc
->r_extern() ) {
6460 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(nextReloc
->r_symbolnum());
6461 // use direct reference for local symbols
6462 if ( ((sym
.n_type() & N_TYPE
) == N_SECT
) && (((sym
.n_type() & N_EXT
) == 0) || (parser
.nameFromSymbol(sym
)[0] == 'L')) ) {
6463 parser
.findTargetFromAddressAndSectionNum(sym
.n_value(), sym
.n_sect(), toTarget
);
6464 toTarget
.addend
= contentValue
;
6465 useDirectBinding
= true;
6468 toTarget
.name
= parser
.nameFromSymbol(sym
);
6469 toTarget
.weakImport
= parser
.weakImportFromSymbol(sym
);
6470 toTarget
.addend
= contentValue
;
6471 useDirectBinding
= false;
6475 parser
.findTargetFromAddressAndSectionNum(contentValue
, nextReloc
->r_symbolnum(), toTarget
);
6476 useDirectBinding
= (toTarget
.atom
->scope() == ld::Atom::scopeTranslationUnit
) || ((toTarget
.atom
->combine() == ld::Atom::combineByNameAndContent
) || (toTarget
.atom
->combine() == ld::Atom::combineByNameAndReferences
));
6478 if ( useDirectBinding
) {
6479 if ( (toTarget
.atom
->combine() == ld::Atom::combineByNameAndContent
) || (toTarget
.atom
->combine() == ld::Atom::combineByNameAndReferences
) )
6480 parser
.addFixup(src
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, toTarget
.atom
);
6482 parser
.addFixup(src
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, toTarget
.atom
);
6485 parser
.addFixup(src
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, toTarget
.weakImport
, toTarget
.name
);
6486 parser
.addFixup(src
, ld::Fixup::k2of4
, ld::Fixup::kindAddAddend
, toTarget
.addend
);
6487 if ( target
.atom
== NULL
)
6488 parser
.addFixup(src
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractTargetAddress
, false, target
.name
);
6490 parser
.addFixup(src
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractTargetAddress
, target
.atom
);
6491 if ( reloc
->r_length() == 2 )
6492 parser
.addFixup(src
, ld::Fixup::k4of4
, ld::Fixup::kindStoreLittleEndian32
);
6494 parser
.addFixup(src
, ld::Fixup::k4of4
, ld::Fixup::kindStoreLittleEndian64
);
6496 case X86_64_RELOC_TLV
:
6497 if ( ! reloc
->r_extern() )
6498 throw "not extern and X86_64_RELOC_TLV not supported";
6499 if ( ! reloc
->r_pcrel() )
6500 throw "not pcrel and X86_64_RELOC_TLV not supported";
6501 if ( reloc
->r_length() != 2 )
6502 throw "length != 2 and X86_64_RELOC_TLV not supported";
6503 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel32TLVLoad
, target
);
6506 throwf("unknown relocation type %d", reloc
->r_type());
6514 bool Section
<x86
>::addRelocFixup(class Parser
<x86
>& parser
, const macho_relocation_info
<P
>* reloc
)
6516 const macho_section
<P
>* sect
= this->machoSection();
6518 const uint8_t* fixUpPtr
;
6519 uint32_t contentValue
= 0;
6520 ld::Fixup::Kind kind
= ld::Fixup::kindNone
;
6521 Parser
<x86
>::SourceLocation src
;
6522 Parser
<x86
>::TargetDesc target
;
6524 if ( (reloc
->r_address() & R_SCATTERED
) == 0 ) {
6525 srcAddr
= sect
->addr() + reloc
->r_address();
6526 src
.atom
= this->findAtomByAddress(srcAddr
);
6527 src
.offsetInAtom
= srcAddr
- src
.atom
->_objAddress
;
6528 fixUpPtr
= file().fileContent() + sect
->offset() + reloc
->r_address();
6529 switch ( reloc
->r_type() ) {
6530 case GENERIC_RELOC_VANILLA
:
6531 switch ( reloc
->r_length() ) {
6533 contentValue
= (int32_t)(int8_t)*fixUpPtr
;
6534 if ( reloc
->r_pcrel() ) {
6535 kind
= ld::Fixup::kindStoreX86BranchPCRel8
;
6536 contentValue
+= srcAddr
+ sizeof(uint8_t);
6539 throw "r_length=0 and r_pcrel=0 not supported";
6542 contentValue
= (int32_t)(int16_t)E::get16(*((uint16_t*)fixUpPtr
));
6543 if ( reloc
->r_pcrel() ) {
6544 kind
= ld::Fixup::kindStoreX86PCRel16
;
6545 contentValue
+= srcAddr
+ sizeof(uint16_t);
6548 kind
= ld::Fixup::kindStoreLittleEndian16
;
6551 contentValue
= E::get32(*((uint32_t*)fixUpPtr
));
6552 if ( reloc
->r_pcrel() ) {
6553 kind
= ld::Fixup::kindStoreX86BranchPCRel32
;
6554 contentValue
+= srcAddr
+ sizeof(uint32_t);
6557 kind
= ld::Fixup::kindStoreLittleEndian32
;
6560 throw "r_length=3 not supported";
6562 if ( reloc
->r_extern() ) {
6564 const macho_nlist
<P
>& targetSymbol
= parser
.symbolFromIndex(reloc
->r_symbolnum());
6565 target
.name
= parser
.nameFromSymbol(targetSymbol
);
6566 target
.weakImport
= parser
.weakImportFromSymbol(targetSymbol
);
6567 target
.addend
= (int32_t)contentValue
;
6570 parser
.findTargetFromAddressAndSectionNum(contentValue
, reloc
->r_symbolnum(), target
);
6572 if ( (kind
== ld::Fixup::kindStoreX86BranchPCRel32
) && (target
.name
!= NULL
) ) {
6573 if ( strncmp(target
.name
, "___dtrace_probe$", 16) == 0 ) {
6574 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindStoreX86DtraceCallSiteNop
, false, target
.name
);
6575 parser
.addDtraceExtraInfos(src
, &target
.name
[16]);
6578 else if ( strncmp(target
.name
, "___dtrace_isenabled$", 20) == 0 ) {
6579 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindStoreX86DtraceIsEnableSiteClear
, false, target
.name
);
6580 parser
.addDtraceExtraInfos(src
, &target
.name
[20]);
6584 parser
.addFixups(src
, kind
, target
);
6587 case GENERIC_RLEOC_TLV
:
6589 if ( !reloc
->r_extern() )
6590 throw "r_extern=0 and r_type=GENERIC_RLEOC_TLV not supported";
6591 if ( reloc
->r_length() != 2 )
6592 throw "r_length!=2 and r_type=GENERIC_RLEOC_TLV not supported";
6593 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(reloc
->r_symbolnum());
6594 // use direct reference for local symbols
6595 if ( ((sym
.n_type() & N_TYPE
) == N_SECT
) && ((sym
.n_type() & N_EXT
) == 0) ) {
6596 parser
.findTargetFromAddressAndSectionNum(sym
.n_value(), sym
.n_sect(), target
);
6600 target
.name
= parser
.nameFromSymbol(sym
);
6601 target
.weakImport
= parser
.weakImportFromSymbol(sym
);
6603 target
.addend
= (int64_t)(int32_t)E::get32(*((uint32_t*)fixUpPtr
));
6604 if ( reloc
->r_pcrel() ) {
6605 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel32TLVLoad
, target
);
6608 parser
.addFixups(src
, ld::Fixup::kindStoreX86Abs32TLVLoad
, target
);
6614 throwf("unsupported i386 relocation type (%d)", reloc
->r_type());
6618 // scattered relocation
6619 const macho_scattered_relocation_info
<P
>* sreloc
= (macho_scattered_relocation_info
<P
>*)reloc
;
6620 srcAddr
= sect
->addr() + sreloc
->r_address();
6621 src
.atom
= this->findAtomByAddress(srcAddr
);
6622 assert(src
.atom
!= NULL
);
6623 src
.offsetInAtom
= srcAddr
- src
.atom
->_objAddress
;
6624 fixUpPtr
= file().fileContent() + sect
->offset() + sreloc
->r_address();
6625 uint32_t relocValue
= sreloc
->r_value();
6626 bool result
= false;
6627 // file format allows pair to be scattered or not
6628 const macho_scattered_relocation_info
<P
>* nextSReloc
= &sreloc
[1];
6629 const macho_relocation_info
<P
>* nextReloc
= &reloc
[1];
6630 bool nextRelocIsPair
= false;
6631 uint32_t nextRelocAddress
= 0;
6632 uint32_t nextRelocValue
= 0;
6633 if ( (nextReloc
->r_address() & R_SCATTERED
) == 0 ) {
6634 if ( nextReloc
->r_type() == GENERIC_RELOC_PAIR
) {
6635 nextRelocIsPair
= true;
6636 nextRelocAddress
= nextReloc
->r_address();
6637 result
= true; // iterator should skip next reloc, since we've consumed it here
6641 if ( nextSReloc
->r_type() == GENERIC_RELOC_PAIR
) {
6642 nextRelocIsPair
= true;
6643 nextRelocAddress
= nextSReloc
->r_address();
6644 nextRelocValue
= nextSReloc
->r_value();
6647 switch (sreloc
->r_type()) {
6648 case GENERIC_RELOC_VANILLA
:
6649 // with a scattered relocation we get both the target (sreloc->r_value()) and the target+offset (*fixUpPtr)
6650 target
.atom
= parser
.findAtomByAddress(relocValue
);
6651 if ( sreloc
->r_pcrel() ) {
6652 switch ( sreloc
->r_length() ) {
6654 contentValue
= srcAddr
+ 1 + *fixUpPtr
;
6655 target
.addend
= (int32_t)contentValue
- (int32_t)relocValue
;
6656 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel8
, target
);
6659 contentValue
= srcAddr
+ 2 + LittleEndian::get16(*((uint16_t*)fixUpPtr
));
6660 target
.addend
= (int32_t)contentValue
- (int32_t)relocValue
;
6661 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel16
, target
);
6664 contentValue
= srcAddr
+ 4 + LittleEndian::get32(*((uint32_t*)fixUpPtr
));
6665 target
.addend
= (int32_t)contentValue
- (int32_t)relocValue
;
6666 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel32
, target
);
6669 throw "unsupported r_length=3 for scattered pc-rel vanilla reloc";
6674 if ( sreloc
->r_length() != 2 )
6675 throwf("unsupported r_length=%d for scattered vanilla reloc", sreloc
->r_length());
6676 contentValue
= LittleEndian::get32(*((uint32_t*)fixUpPtr
));
6677 target
.addend
= (int32_t)contentValue
- (int32_t)(target
.atom
->objectAddress());
6678 parser
.addFixups(src
, ld::Fixup::kindStoreLittleEndian32
, target
);
6681 case GENERIC_RELOC_SECTDIFF
:
6682 case GENERIC_RELOC_LOCAL_SECTDIFF
:
6684 if ( !nextRelocIsPair
)
6685 throw "GENERIC_RELOC_SECTDIFF missing following pair";
6686 switch ( sreloc
->r_length() ) {
6689 throw "bad length for GENERIC_RELOC_SECTDIFF";
6691 contentValue
= (int32_t)(int16_t)LittleEndian::get16(*((uint16_t*)fixUpPtr
));
6692 kind
= ld::Fixup::kindStoreLittleEndian16
;
6695 contentValue
= LittleEndian::get32(*((uint32_t*)fixUpPtr
));
6696 kind
= ld::Fixup::kindStoreLittleEndian32
;
6699 Atom
<x86
>* fromAtom
= parser
.findAtomByAddress(nextRelocValue
);
6700 uint32_t offsetInFrom
= nextRelocValue
- fromAtom
->_objAddress
;
6701 parser
.findTargetFromAddress(sreloc
->r_value(), target
);
6702 // check for addend encoded in the section content
6703 int64_t addend
= (int32_t)contentValue
- (int32_t)(sreloc
->r_value() - nextRelocValue
);
6705 // switch binding base on coalescing
6706 if ( target
.atom
== NULL
) {
6707 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, false, target
.name
);
6709 else if ( target
.atom
->scope() == ld::Atom::scopeTranslationUnit
) {
6710 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, target
.atom
);
6712 else if ( (target
.atom
->combine() == ld::Atom::combineByNameAndContent
) || (target
.atom
->combine() == ld::Atom::combineByNameAndReferences
) ) {
6713 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, target
.atom
);
6716 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, false, target
.atom
->name());
6718 parser
.addFixup(src
, ld::Fixup::k2of5
, ld::Fixup::kindAddAddend
, target
.addend
);
6719 parser
.addFixup(src
, ld::Fixup::k3of5
, ld::Fixup::kindSubtractTargetAddress
, fromAtom
);
6720 parser
.addFixup(src
, ld::Fixup::k4of5
, ld::Fixup::kindSubtractAddend
, offsetInFrom
-addend
);
6721 parser
.addFixup(src
, ld::Fixup::k5of5
, kind
);
6724 // switch binding base on coalescing
6725 if ( target
.atom
== NULL
) {
6726 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, false, target
.name
);
6728 else if ( target
.atom
->scope() == ld::Atom::scopeTranslationUnit
) {
6729 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, target
.atom
);
6731 else if ( (target
.atom
->combine() == ld::Atom::combineByNameAndContent
) || (target
.atom
->combine() == ld::Atom::combineByNameAndReferences
) ) {
6732 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, target
.atom
);
6735 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, false, target
.atom
->name());
6737 parser
.addFixup(src
, ld::Fixup::k2of5
, ld::Fixup::kindAddAddend
, target
.addend
+addend
);
6738 parser
.addFixup(src
, ld::Fixup::k3of5
, ld::Fixup::kindSubtractTargetAddress
, fromAtom
);
6739 parser
.addFixup(src
, ld::Fixup::k4of5
, ld::Fixup::kindSubtractAddend
, offsetInFrom
);
6740 parser
.addFixup(src
, ld::Fixup::k5of5
, kind
);
6753 #if SUPPORT_ARCH_arm_any
6755 bool Section
<arm
>::addRelocFixup(class Parser
<arm
>& parser
, const macho_relocation_info
<P
>* reloc
)
6757 const macho_section
<P
>* sect
= this->machoSection();
6758 bool result
= false;
6762 int32_t displacement
= 0;
6763 uint32_t instruction
= 0;
6764 pint_t contentValue
= 0;
6765 Parser
<arm
>::SourceLocation src
;
6766 Parser
<arm
>::TargetDesc target
;
6767 const macho_relocation_info
<P
>* nextReloc
;
6769 if ( (reloc
->r_address() & R_SCATTERED
) == 0 ) {
6770 bool externSymbolIsThumbDef
= false;
6771 srcAddr
= sect
->addr() + reloc
->r_address();
6772 src
.atom
= this->findAtomByAddress(srcAddr
);
6773 src
.offsetInAtom
= srcAddr
- src
.atom
->_objAddress
;
6774 fixUpPtr
= (uint32_t*)(file().fileContent() + sect
->offset() + reloc
->r_address());
6775 if ( reloc
->r_type() != ARM_RELOC_PAIR
)
6776 instruction
= LittleEndian::get32(*fixUpPtr
);
6777 if ( reloc
->r_extern() ) {
6778 const macho_nlist
<P
>& targetSymbol
= parser
.symbolFromIndex(reloc
->r_symbolnum());
6779 // use direct reference for local symbols
6780 if ( ((targetSymbol
.n_type() & N_TYPE
) == N_SECT
) && (((targetSymbol
.n_type() & N_EXT
) == 0) || (parser
.nameFromSymbol(targetSymbol
)[0] == 'L')) ) {
6781 parser
.findTargetFromAddressAndSectionNum(targetSymbol
.n_value(), targetSymbol
.n_sect(), target
);
6785 target
.name
= parser
.nameFromSymbol(targetSymbol
);
6786 target
.weakImport
= parser
.weakImportFromSymbol(targetSymbol
);
6787 if ( ((targetSymbol
.n_type() & N_TYPE
) == N_SECT
) && (targetSymbol
.n_desc() & N_ARM_THUMB_DEF
) )
6788 externSymbolIsThumbDef
= true;
6791 switch ( reloc
->r_type() ) {
6792 case ARM_RELOC_BR24
:
6793 // Sign-extend displacement
6794 displacement
= (instruction
& 0x00FFFFFF) << 2;
6795 if ( (displacement
& 0x02000000) != 0 )
6796 displacement
|= 0xFC000000;
6797 // The pc added will be +8 from the pc
6799 // If this is BLX add H << 1
6800 if ((instruction
& 0xFE000000) == 0xFA000000)
6801 displacement
+= ((instruction
& 0x01000000) >> 23);
6802 if ( reloc
->r_extern() ) {
6803 dstAddr
= srcAddr
+ displacement
;
6804 // <rdar://problem/16652542> support large .o files
6805 if ( srcAddr
> 0x2000000 ) {
6806 dstAddr
-= ((srcAddr
+ 0x1FFFFFF) & 0xFC000000);
6808 target
.addend
= dstAddr
;
6809 if ( externSymbolIsThumbDef
)
6810 target
.addend
&= -2; // remove thumb bit
6813 dstAddr
= srcAddr
+ displacement
;
6814 parser
.findTargetFromAddressAndSectionNum(dstAddr
, reloc
->r_symbolnum(), target
);
6816 // special case "calls" for dtrace
6817 if ( (target
.name
!= NULL
) && (strncmp(target
.name
, "___dtrace_probe$", 16) == 0) ) {
6818 parser
.addFixup(src
, ld::Fixup::k1of1
,
6819 ld::Fixup::kindStoreARMDtraceCallSiteNop
, false, target
.name
);
6820 parser
.addDtraceExtraInfos(src
, &target
.name
[16]);
6822 else if ( (target
.name
!= NULL
) && (strncmp(target
.name
, "___dtrace_isenabled$", 20) == 0) ) {
6823 parser
.addFixup(src
, ld::Fixup::k1of1
,
6824 ld::Fixup::kindStoreARMDtraceIsEnableSiteClear
, false, target
.name
);
6825 parser
.addDtraceExtraInfos(src
, &target
.name
[20]);
6828 parser
.addFixups(src
, ld::Fixup::kindStoreARMBranch24
, target
);
6831 case ARM_THUMB_RELOC_BR22
:
6832 // thumb2 added two more bits to displacement, complicating the displacement decoding
6834 uint32_t s
= (instruction
>> 10) & 0x1;
6835 uint32_t j1
= (instruction
>> 29) & 0x1;
6836 uint32_t j2
= (instruction
>> 27) & 0x1;
6837 uint32_t imm10
= instruction
& 0x3FF;
6838 uint32_t imm11
= (instruction
>> 16) & 0x7FF;
6839 uint32_t i1
= (j1
== s
);
6840 uint32_t i2
= (j2
== s
);
6841 uint32_t dis
= (s
<< 24) | (i1
<< 23) | (i2
<< 22) | (imm10
<< 12) | (imm11
<< 1);
6845 displacement
= sdis
;
6847 // The pc added will be +4 from the pc
6849 // If the instruction was blx, force the low 2 bits to be clear
6850 dstAddr
= srcAddr
+ displacement
;
6851 if ((instruction
& 0xD0000000) == 0xC0000000)
6852 dstAddr
&= 0xFFFFFFFC;
6854 if ( reloc
->r_extern() ) {
6855 // <rdar://problem/16652542> support large .o files
6856 if ( srcAddr
> 0x1000000 ) {
6857 dstAddr
-= ((srcAddr
+ 0xFFFFFF) & 0xFE000000);
6859 target
.addend
= (int64_t)(int32_t)dstAddr
;
6862 parser
.findTargetFromAddressAndSectionNum(dstAddr
, reloc
->r_symbolnum(), target
);
6864 // special case "calls" for dtrace
6865 if ( (target
.name
!= NULL
) && (strncmp(target
.name
, "___dtrace_probe$", 16) == 0) ) {
6866 parser
.addFixup(src
, ld::Fixup::k1of1
,
6867 ld::Fixup::kindStoreThumbDtraceCallSiteNop
, false, target
.name
);
6868 parser
.addDtraceExtraInfos(src
, &target
.name
[16]);
6870 else if ( (target
.name
!= NULL
) && (strncmp(target
.name
, "___dtrace_isenabled$", 20) == 0) ) {
6871 parser
.addFixup(src
, ld::Fixup::k1of1
,
6872 ld::Fixup::kindStoreThumbDtraceIsEnableSiteClear
, false, target
.name
);
6873 parser
.addDtraceExtraInfos(src
, &target
.name
[20]);
6876 parser
.addFixups(src
, ld::Fixup::kindStoreThumbBranch22
, target
);
6879 case ARM_RELOC_VANILLA
:
6880 if ( reloc
->r_length() != 2 )
6881 throw "bad length for ARM_RELOC_VANILLA";
6882 contentValue
= LittleEndian::get32(*fixUpPtr
);
6883 if ( reloc
->r_extern() ) {
6884 target
.addend
= (int32_t)contentValue
;
6885 if ( externSymbolIsThumbDef
)
6886 target
.addend
&= -2; // remove thumb bit
6889 parser
.findTargetFromAddressAndSectionNum(contentValue
, reloc
->r_symbolnum(), target
);
6890 // possible non-extern relocation turned into by-name ref because target is a weak-def
6891 if ( target
.atom
!= NULL
) {
6892 if ( target
.atom
->isThumb() )
6893 target
.addend
&= -2; // remove thumb bit
6894 // if reference to LSDA, add group subordinate fixup
6895 if ( target
.atom
->contentType() == ld::Atom::typeLSDA
) {
6896 Parser
<arm
>::SourceLocation src2
;
6897 src2
.atom
= src
.atom
;
6898 src2
.offsetInAtom
= 0;
6899 parser
.addFixup(src2
, ld::Fixup::k1of1
, ld::Fixup::kindNoneGroupSubordinateLSDA
, target
.atom
);
6903 parser
.addFixups(src
, ld::Fixup::kindStoreLittleEndian32
, target
);
6905 case ARM_THUMB_32BIT_BRANCH
:
6906 // silently ignore old unnecessary reloc
6908 case ARM_RELOC_HALF
:
6909 nextReloc
= &reloc
[1];
6910 if ( nextReloc
->r_type() == ARM_RELOC_PAIR
) {
6911 uint32_t instruction16
;
6912 uint32_t other16
= (nextReloc
->r_address() & 0xFFFF);
6914 if ( reloc
->r_length() & 2 ) {
6916 uint32_t i
= ((instruction
& 0x00000400) >> 10);
6917 uint32_t imm4
= (instruction
& 0x0000000F);
6918 uint32_t imm3
= ((instruction
& 0x70000000) >> 28);
6919 uint32_t imm8
= ((instruction
& 0x00FF0000) >> 16);
6920 instruction16
= (imm4
<< 12) | (i
<< 11) | (imm3
<< 8) | imm8
;
6924 uint32_t imm4
= ((instruction
& 0x000F0000) >> 16);
6925 uint32_t imm12
= (instruction
& 0x00000FFF);
6926 instruction16
= (imm4
<< 12) | imm12
;
6928 if ( reloc
->r_length() & 1 ) {
6930 dstAddr
= ((instruction16
<< 16) | other16
);
6931 if ( reloc
->r_extern() ) {
6932 target
.addend
= dstAddr
;
6933 if ( externSymbolIsThumbDef
)
6934 target
.addend
&= -2; // remove thumb bit
6937 parser
.findTargetFromAddress(dstAddr
, target
);
6938 if ( target
.atom
->isThumb() )
6939 target
.addend
&= (-2); // remove thumb bit
6941 parser
.addFixups(src
, (isThumb
? ld::Fixup::kindStoreThumbHigh16
: ld::Fixup::kindStoreARMHigh16
), target
);
6945 dstAddr
= (other16
<< 16) | instruction16
;
6946 if ( reloc
->r_extern() ) {
6947 target
.addend
= dstAddr
;
6948 if ( externSymbolIsThumbDef
)
6949 target
.addend
&= -2; // remove thumb bit
6952 parser
.findTargetFromAddress(dstAddr
, target
);
6953 if ( target
.atom
->isThumb() )
6954 target
.addend
&= (-2); // remove thumb bit
6956 parser
.addFixups(src
, (isThumb
? ld::Fixup::kindStoreThumbLow16
: ld::Fixup::kindStoreARMLow16
), target
);
6961 throw "for ARM_RELOC_HALF, next reloc is not ARM_RELOC_PAIR";
6964 throwf("unknown relocation type %d", reloc
->r_type());
6969 const macho_scattered_relocation_info
<P
>* sreloc
= (macho_scattered_relocation_info
<P
>*)reloc
;
6970 // file format allows pair to be scattered or not
6971 const macho_scattered_relocation_info
<P
>* nextSReloc
= &sreloc
[1];
6972 nextReloc
= &reloc
[1];
6973 srcAddr
= sect
->addr() + sreloc
->r_address();
6974 dstAddr
= sreloc
->r_value();
6975 fixUpPtr
= (uint32_t*)(file().fileContent() + sect
->offset() + sreloc
->r_address());
6976 instruction
= LittleEndian::get32(*fixUpPtr
);
6977 src
.atom
= this->findAtomByAddress(srcAddr
);
6978 src
.offsetInAtom
= srcAddr
- src
.atom
->_objAddress
;
6979 bool nextRelocIsPair
= false;
6980 uint32_t nextRelocAddress
= 0;
6981 uint32_t nextRelocValue
= 0;
6982 if ( (nextReloc
->r_address() & R_SCATTERED
) == 0 ) {
6983 if ( nextReloc
->r_type() == ARM_RELOC_PAIR
) {
6984 nextRelocIsPair
= true;
6985 nextRelocAddress
= nextReloc
->r_address();
6990 if ( nextSReloc
->r_type() == ARM_RELOC_PAIR
) {
6991 nextRelocIsPair
= true;
6992 nextRelocAddress
= nextSReloc
->r_address();
6993 nextRelocValue
= nextSReloc
->r_value();
6997 switch ( sreloc
->r_type() ) {
6998 case ARM_RELOC_VANILLA
:
6999 // with a scattered relocation we get both the target (sreloc->r_value()) and the target+offset (*fixUpPtr)
7000 if ( sreloc
->r_length() != 2 )
7001 throw "bad length for ARM_RELOC_VANILLA";
7002 target
.atom
= parser
.findAtomByAddress(sreloc
->r_value());
7003 if ( target
.atom
== NULL
)
7004 throwf("bad r_value (0x%08X) for ARM_RELOC_VANILLA\n", sreloc
->r_value());
7005 contentValue
= LittleEndian::get32(*fixUpPtr
);
7006 target
.addend
= contentValue
- target
.atom
->_objAddress
;
7007 if ( target
.atom
->isThumb() )
7008 target
.addend
&= -2; // remove thumb bit
7009 parser
.addFixups(src
, ld::Fixup::kindStoreLittleEndian32
, target
);
7011 case ARM_RELOC_BR24
:
7012 // Sign-extend displacement
7013 displacement
= (instruction
& 0x00FFFFFF) << 2;
7014 if ( (displacement
& 0x02000000) != 0 )
7015 displacement
|= 0xFC000000;
7016 // The pc added will be +8 from the pc
7018 // If this is BLX add H << 1
7019 if ((instruction
& 0xFE000000) == 0xFA000000)
7020 displacement
+= ((instruction
& 0x01000000) >> 23);
7021 target
.atom
= parser
.findAtomByAddress(sreloc
->r_value());
7022 target
.addend
= (int64_t)(srcAddr
+ displacement
) - (int64_t)(target
.atom
->_objAddress
);
7023 parser
.addFixups(src
, ld::Fixup::kindStoreARMBranch24
, target
);
7025 case ARM_THUMB_RELOC_BR22
:
7026 // thumb2 added two more bits to displacement, complicating the displacement decoding
7028 uint32_t s
= (instruction
>> 10) & 0x1;
7029 uint32_t j1
= (instruction
>> 29) & 0x1;
7030 uint32_t j2
= (instruction
>> 27) & 0x1;
7031 uint32_t imm10
= instruction
& 0x3FF;
7032 uint32_t imm11
= (instruction
>> 16) & 0x7FF;
7033 uint32_t i1
= (j1
== s
);
7034 uint32_t i2
= (j2
== s
);
7035 uint32_t dis
= (s
<< 24) | (i1
<< 23) | (i2
<< 22) | (imm10
<< 12) | (imm11
<< 1);
7039 displacement
= sdis
;
7041 // The pc added will be +4 from the pc
7043 dstAddr
= srcAddr
+displacement
;
7044 // If the instruction was blx, force the low 2 bits to be clear
7045 if ((instruction
& 0xF8000000) == 0xE8000000)
7046 dstAddr
&= 0xFFFFFFFC;
7047 target
.atom
= parser
.findAtomByAddress(sreloc
->r_value());
7048 target
.addend
= dstAddr
- target
.atom
->_objAddress
;
7049 parser
.addFixups(src
, ld::Fixup::kindStoreThumbBranch22
, target
);
7051 case ARM_RELOC_SECTDIFF
:
7052 case ARM_RELOC_LOCAL_SECTDIFF
:
7054 if ( ! nextRelocIsPair
)
7055 throw "ARM_RELOC_SECTDIFF missing following pair";
7056 if ( sreloc
->r_length() != 2 )
7057 throw "bad length for ARM_RELOC_SECTDIFF";
7058 contentValue
= LittleEndian::get32(*fixUpPtr
);
7059 Atom
<arm
>* fromAtom
= parser
.findAtomByAddress(nextRelocValue
);
7060 uint32_t offsetInFrom
= nextRelocValue
- fromAtom
->_objAddress
;
7061 uint32_t offsetInTarget
;
7062 Atom
<arm
>* targetAtom
= parser
.findAtomByAddressOrLocalTargetOfStub(sreloc
->r_value(), &offsetInTarget
);
7063 // check for addend encoded in the section content
7064 int64_t addend
= (int32_t)contentValue
- (int32_t)(sreloc
->r_value() - nextRelocValue
);
7065 if ( targetAtom
->isThumb() )
7066 addend
&= -2; // remove thumb bit
7067 // if reference to LSDA, add group subordinate fixup
7068 if ( targetAtom
->contentType() == ld::Atom::typeLSDA
) {
7069 Parser
<arm
>::SourceLocation src2
;
7070 src2
.atom
= src
.atom
;
7071 src2
.offsetInAtom
= 0;
7072 parser
.addFixup(src2
, ld::Fixup::k1of1
, ld::Fixup::kindNoneGroupSubordinateLSDA
, targetAtom
);
7075 // switch binding base on coalescing
7076 if ( targetAtom
->scope() == ld::Atom::scopeTranslationUnit
) {
7077 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, targetAtom
);
7079 else if ( (targetAtom
->combine() == ld::Atom::combineByNameAndContent
) || (targetAtom
->combine() == ld::Atom::combineByNameAndReferences
) ) {
7080 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, targetAtom
);
7083 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, false, targetAtom
->name());
7085 parser
.addFixup(src
, ld::Fixup::k2of5
, ld::Fixup::kindAddAddend
, offsetInTarget
);
7086 parser
.addFixup(src
, ld::Fixup::k3of5
, ld::Fixup::kindSubtractTargetAddress
, fromAtom
);
7087 parser
.addFixup(src
, ld::Fixup::k4of5
, ld::Fixup::kindSubtractAddend
, offsetInFrom
-addend
);
7088 parser
.addFixup(src
, ld::Fixup::k5of5
, ld::Fixup::kindStoreLittleEndian32
);
7091 if ( targetAtom
->scope() == ld::Atom::scopeTranslationUnit
) {
7092 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, targetAtom
);
7094 else if ( (targetAtom
->combine() == ld::Atom::combineByNameAndContent
) || (targetAtom
->combine() == ld::Atom::combineByNameAndReferences
) ) {
7095 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, targetAtom
);
7098 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, false, targetAtom
->name());
7100 parser
.addFixup(src
, ld::Fixup::k2of5
, ld::Fixup::kindAddAddend
, (uint32_t)(offsetInTarget
+addend
));
7101 parser
.addFixup(src
, ld::Fixup::k3of5
, ld::Fixup::kindSubtractTargetAddress
, fromAtom
);
7102 parser
.addFixup(src
, ld::Fixup::k4of5
, ld::Fixup::kindSubtractAddend
, offsetInFrom
);
7103 parser
.addFixup(src
, ld::Fixup::k5of5
, ld::Fixup::kindStoreLittleEndian32
);
7107 case ARM_RELOC_HALF_SECTDIFF
:
7108 if ( nextRelocIsPair
) {
7109 instruction
= LittleEndian::get32(*fixUpPtr
);
7110 Atom
<arm
>* fromAtom
= parser
.findAtomByAddress(nextRelocValue
);
7111 uint32_t offsetInFrom
= nextRelocValue
- fromAtom
->_objAddress
;
7112 Atom
<arm
>* targetAtom
= parser
.findAtomByAddress(sreloc
->r_value());
7113 uint32_t offsetInTarget
= sreloc
->r_value() - targetAtom
->_objAddress
;
7114 uint32_t instruction16
;
7115 uint32_t other16
= (nextRelocAddress
& 0xFFFF);
7117 if ( sreloc
->r_length() & 2 ) {
7119 uint32_t i
= ((instruction
& 0x00000400) >> 10);
7120 uint32_t imm4
= (instruction
& 0x0000000F);
7121 uint32_t imm3
= ((instruction
& 0x70000000) >> 28);
7122 uint32_t imm8
= ((instruction
& 0x00FF0000) >> 16);
7123 instruction16
= (imm4
<< 12) | (i
<< 11) | (imm3
<< 8) | imm8
;
7127 uint32_t imm4
= ((instruction
& 0x000F0000) >> 16);
7128 uint32_t imm12
= (instruction
& 0x00000FFF);
7129 instruction16
= (imm4
<< 12) | imm12
;
7131 if ( sreloc
->r_length() & 1 )
7132 dstAddr
= ((instruction16
<< 16) | other16
);
7134 dstAddr
= (other16
<< 16) | instruction16
;
7135 if ( targetAtom
->isThumb() )
7136 dstAddr
&= (-2); // remove thumb bit
7137 int32_t addend
= dstAddr
- (sreloc
->r_value() - nextRelocValue
);
7138 if ( targetAtom
->scope() == ld::Atom::scopeTranslationUnit
) {
7139 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, targetAtom
);
7141 else if ( (targetAtom
->combine() == ld::Atom::combineByNameAndContent
) || (targetAtom
->combine() == ld::Atom::combineByNameAndReferences
) ) {
7142 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, targetAtom
);
7145 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, false, targetAtom
->name());
7147 parser
.addFixup(src
, ld::Fixup::k2of5
, ld::Fixup::kindAddAddend
, (uint32_t)offsetInTarget
+addend
);
7148 parser
.addFixup(src
, ld::Fixup::k3of5
, ld::Fixup::kindSubtractTargetAddress
, fromAtom
);
7149 parser
.addFixup(src
, ld::Fixup::k4of5
, ld::Fixup::kindSubtractAddend
, offsetInFrom
);
7150 if ( sreloc
->r_length() & 1 ) {
7152 parser
.addFixup(src
, ld::Fixup::k5of5
, (isThumb
? ld::Fixup::kindStoreThumbHigh16
: ld::Fixup::kindStoreARMHigh16
));
7156 parser
.addFixup(src
, ld::Fixup::k5of5
, (isThumb
? ld::Fixup::kindStoreThumbLow16
: ld::Fixup::kindStoreARMLow16
));
7161 throw "ARM_RELOC_HALF_SECTDIFF reloc missing following pair";
7163 case ARM_RELOC_HALF
:
7164 if ( nextRelocIsPair
) {
7165 instruction
= LittleEndian::get32(*fixUpPtr
);
7166 Atom
<arm
>* targetAtom
= parser
.findAtomByAddress(sreloc
->r_value());
7167 uint32_t instruction16
;
7168 uint32_t other16
= (nextRelocAddress
& 0xFFFF);
7170 if ( sreloc
->r_length() & 2 ) {
7172 uint32_t i
= ((instruction
& 0x00000400) >> 10);
7173 uint32_t imm4
= (instruction
& 0x0000000F);
7174 uint32_t imm3
= ((instruction
& 0x70000000) >> 28);
7175 uint32_t imm8
= ((instruction
& 0x00FF0000) >> 16);
7176 instruction16
= (imm4
<< 12) | (i
<< 11) | (imm3
<< 8) | imm8
;
7180 uint32_t imm4
= ((instruction
& 0x000F0000) >> 16);
7181 uint32_t imm12
= (instruction
& 0x00000FFF);
7182 instruction16
= (imm4
<< 12) | imm12
;
7184 if ( sreloc
->r_length() & 1 )
7185 dstAddr
= ((instruction16
<< 16) | other16
);
7187 dstAddr
= (other16
<< 16) | instruction16
;
7188 if ( targetAtom
->scope() == ld::Atom::scopeTranslationUnit
) {
7189 parser
.addFixup(src
, ld::Fixup::k1of3
, ld::Fixup::kindSetTargetAddress
, targetAtom
);
7191 else if ( (targetAtom
->combine() == ld::Atom::combineByNameAndContent
) || (targetAtom
->combine() == ld::Atom::combineByNameAndReferences
) ) {
7192 parser
.addFixup(src
, ld::Fixup::k1of3
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, targetAtom
);
7195 parser
.addFixup(src
, ld::Fixup::k1of3
, ld::Fixup::kindSetTargetAddress
, false, targetAtom
->name());
7197 parser
.addFixup(src
, ld::Fixup::k2of3
, ld::Fixup::kindAddAddend
, dstAddr
- targetAtom
->_objAddress
);
7198 if ( sreloc
->r_length() & 1 ) {
7200 parser
.addFixup(src
, ld::Fixup::k3of3
, (isThumb
? ld::Fixup::kindStoreThumbHigh16
: ld::Fixup::kindStoreARMHigh16
));
7204 parser
.addFixup(src
, ld::Fixup::k3of3
, (isThumb
? ld::Fixup::kindStoreThumbLow16
: ld::Fixup::kindStoreARMLow16
));
7209 throw "scattered ARM_RELOC_HALF reloc missing following pair";
7212 throwf("unknown ARM scattered relocation type %d", sreloc
->r_type());
7220 #if SUPPORT_ARCH_arm64
7222 bool Section
<arm64
>::addRelocFixup(class Parser
<arm64
>& parser
, const macho_relocation_info
<P
>* reloc
)
7224 bool result
= false;
7225 Parser
<arm64
>::SourceLocation src
;
7226 Parser
<arm64
>::TargetDesc target
= { NULL
, NULL
, false, 0 };
7227 Parser
<arm64
>::TargetDesc toTarget
;
7228 int32_t prefixRelocAddend
= 0;
7229 if ( reloc
->r_type() == ARM64_RELOC_ADDEND
) {
7230 uint32_t rawAddend
= reloc
->r_symbolnum();
7231 prefixRelocAddend
= rawAddend
;
7232 if ( rawAddend
& 0x00800000 )
7233 prefixRelocAddend
|= 0xFF000000; // sign extend 24-bit signed int to 32-bits
7234 uint32_t addendAddress
= reloc
->r_address();
7235 ++reloc
; //advance to next reloc record
7237 if ( reloc
->r_address() != addendAddress
)
7238 throw "ARM64_RELOC_ADDEND r_address does not match next reloc's r_address";
7240 const macho_section
<P
>* sect
= this->machoSection();
7241 uint64_t srcAddr
= sect
->addr() + reloc
->r_address();
7242 src
.atom
= this->findAtomByAddress(srcAddr
);
7243 src
.offsetInAtom
= srcAddr
- src
.atom
->_objAddress
;
7244 const uint8_t* fixUpPtr
= file().fileContent() + sect
->offset() + reloc
->r_address();
7245 uint64_t contentValue
= 0;
7246 const macho_relocation_info
<arm64::P
>* nextReloc
= &reloc
[1];
7247 bool useDirectBinding
;
7248 uint32_t instruction
;
7249 uint32_t encodedAddend
;
7250 switch ( reloc
->r_length() ) {
7252 contentValue
= *fixUpPtr
;
7255 contentValue
= (int64_t)(int16_t)E::get16(*((uint16_t*)fixUpPtr
));
7258 contentValue
= (int64_t)(int32_t)E::get32(*((uint32_t*)fixUpPtr
));
7261 contentValue
= E::get64(*((uint64_t*)fixUpPtr
));
7264 if ( reloc
->r_extern() ) {
7265 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(reloc
->r_symbolnum());
7266 const char* symbolName
= parser
.nameFromSymbol(sym
);
7267 if ( ((sym
.n_type() & N_TYPE
) == N_SECT
) && (((sym
.n_type() & N_EXT
) == 0) || (symbolName
[0] == 'L') || (symbolName
[0] == 'l')) ) {
7268 // use direct reference for local symbols
7269 parser
.findTargetFromAddressAndSectionNum(sym
.n_value(), sym
.n_sect(), target
);
7270 //target.addend += contentValue;
7272 else if ( ((sym
.n_type() & N_TYPE
) == N_SECT
) && (src
.atom
->_objAddress
<= sym
.n_value()) && (sym
.n_value() < (src
.atom
->_objAddress
+src
.atom
->size())) ) {
7273 // <rdar://problem/13700961> spurious warning when weak function has reference to itself
7274 // use direct reference when atom targets itself
7275 target
.atom
= src
.atom
;
7279 target
.name
= symbolName
;
7280 target
.weakImport
= parser
.weakImportFromSymbol(sym
);
7281 //target.addend = contentValue;
7283 // cfstrings should always use direct reference to backing store
7284 if ( (this->type() == ld::Section::typeCFString
) && (src
.offsetInAtom
!= 0) ) {
7285 parser
.findTargetFromAddressAndSectionNum(sym
.n_value(), sym
.n_sect(), target
);
7286 //target.addend = contentValue;
7290 if ( reloc
->r_pcrel() )
7291 contentValue
+= srcAddr
;
7292 parser
.findTargetFromAddressAndSectionNum(contentValue
, reloc
->r_symbolnum(), target
);
7294 switch ( reloc
->r_type() ) {
7295 case ARM64_RELOC_UNSIGNED
:
7296 if ( reloc
->r_pcrel() )
7297 throw "pcrel and ARM64_RELOC_UNSIGNED not supported";
7298 if ( reloc
->r_extern() )
7299 target
.addend
= contentValue
;
7300 switch ( reloc
->r_length() ) {
7303 throw "length < 2 and ARM64_RELOC_UNSIGNED not supported";
7305 parser
.addFixups(src
, ld::Fixup::kindStoreLittleEndian32
, target
);
7308 parser
.addFixups(src
, ld::Fixup::kindStoreLittleEndian64
, target
);
7312 case ARM64_RELOC_BRANCH26
:
7313 if ( ! reloc
->r_pcrel() )
7314 throw "not pcrel and ARM64_RELOC_BRANCH26 not supported";
7315 if ( ! reloc
->r_extern() )
7316 throw "r_extern == 0 and ARM64_RELOC_BRANCH26 not supported";
7317 if ( reloc
->r_length() != 2 )
7318 throw "r_length != 2 and ARM64_RELOC_BRANCH26 not supported";
7319 if ( (target
.name
!= NULL
) && (strncmp(target
.name
, "___dtrace_probe$", 16) == 0) ) {
7320 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindStoreARM64DtraceCallSiteNop
, false, target
.name
);
7321 parser
.addDtraceExtraInfos(src
, &target
.name
[16]);
7323 else if ( (target
.name
!= NULL
) && (strncmp(target
.name
, "___dtrace_isenabled$", 20) == 0) ) {
7324 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindStoreARM64DtraceIsEnableSiteClear
, false, target
.name
);
7325 parser
.addDtraceExtraInfos(src
, &target
.name
[20]);
7328 target
.addend
= prefixRelocAddend
;
7329 instruction
= contentValue
;
7330 encodedAddend
= (instruction
& 0x03FFFFFF) << 2;
7331 if ( encodedAddend
!= 0 ) {
7332 if ( prefixRelocAddend
== 0 ) {
7333 warning("branch26 instruction at 0x%08X has embedded addend. ARM64_RELOC_ADDEND should be used instead", reloc
->r_address());
7334 target
.addend
= encodedAddend
;
7337 throwf("branch26 instruction at 0x%08X has embedded addend and ARM64_RELOC_ADDEND also used", reloc
->r_address());
7340 parser
.addFixups(src
, ld::Fixup::kindStoreARM64Branch26
, target
);
7343 case ARM64_RELOC_PAGE21
:
7344 if ( ! reloc
->r_pcrel() )
7345 throw "not pcrel and ARM64_RELOC_PAGE21 not supported";
7346 if ( ! reloc
->r_extern() )
7347 throw "r_extern == 0 and ARM64_RELOC_PAGE21 not supported";
7348 if ( reloc
->r_length() != 2 )
7349 throw "length != 2 and ARM64_RELOC_PAGE21 not supported";
7350 target
.addend
= prefixRelocAddend
;
7351 instruction
= contentValue
;
7352 encodedAddend
= ((instruction
& 0x60000000) >> 29) | ((instruction
& 0x01FFFFE0) >> 3);
7353 encodedAddend
*= 4096; // internally addend is in bytes, so scale
7354 if ( encodedAddend
!= 0 ) {
7355 if ( prefixRelocAddend
== 0 ) {
7356 warning("adrp instruction at 0x%08X has embedded addend. ARM64_RELOC_ADDEND should be used instead", reloc
->r_address());
7357 target
.addend
= encodedAddend
;
7360 throwf("adrp instruction at 0x%08X has embedded addend and ARM64_RELOC_ADDEND also used", reloc
->r_address());
7363 parser
.addFixups(src
, ld::Fixup::kindStoreARM64Page21
, target
);
7365 case ARM64_RELOC_PAGEOFF12
:
7366 if ( reloc
->r_pcrel() )
7367 throw "pcrel and ARM64_RELOC_PAGEOFF12 not supported";
7368 if ( ! reloc
->r_extern() )
7369 throw "r_extern == 0 and ARM64_RELOC_PAGEOFF12 not supported";
7370 if ( reloc
->r_length() != 2 )
7371 throw "length != 2 and ARM64_RELOC_PAGEOFF12 not supported";
7372 target
.addend
= prefixRelocAddend
;
7373 instruction
= contentValue
;
7374 encodedAddend
= ((instruction
& 0x003FFC00) >> 10);
7375 // internally addend is in bytes. Some instructions have an implicit scale factor
7376 if ( (instruction
& 0x3B000000) == 0x39000000 ) {
7377 switch ( instruction
& 0xC0000000 ) {
7391 if ( encodedAddend
!= 0 ) {
7392 if ( prefixRelocAddend
== 0 ) {
7393 warning("pageoff12 instruction at 0x%08X has embedded addend. ARM64_RELOC_ADDEND should be used instead", reloc
->r_address());
7394 target
.addend
= encodedAddend
;
7397 throwf("pageoff12 instruction at 0x%08X has embedded addend and ARM64_RELOC_ADDEND also used", reloc
->r_address());
7400 parser
.addFixups(src
, ld::Fixup::kindStoreARM64PageOff12
, target
);
7402 case ARM64_RELOC_GOT_LOAD_PAGE21
:
7403 if ( ! reloc
->r_pcrel() )
7404 throw "not pcrel and ARM64_RELOC_GOT_LOAD_PAGE21 not supported";
7405 if ( ! reloc
->r_extern() )
7406 throw "r_extern == 0 and ARM64_RELOC_GOT_LOAD_PAGE21 not supported";
7407 if ( reloc
->r_length() != 2 )
7408 throw "length != 2 and ARM64_RELOC_GOT_LOAD_PAGE21 not supported";
7409 if ( prefixRelocAddend
!= 0 )
7410 throw "ARM64_RELOC_ADDEND followed by ARM64_RELOC_GOT_LOAD_PAGE21 not supported";
7411 instruction
= contentValue
;
7412 target
.addend
= ((instruction
& 0x60000000) >> 29) | ((instruction
& 0x01FFFFE0) >> 3);
7413 if ( target
.addend
!= 0 )
7414 throw "non-zero addend with ARM64_RELOC_GOT_LOAD_PAGE21 is not supported";
7415 parser
.addFixups(src
, ld::Fixup::kindStoreARM64GOTLoadPage21
, target
);
7417 case ARM64_RELOC_GOT_LOAD_PAGEOFF12
:
7418 if ( reloc
->r_pcrel() )
7419 throw "pcrel and ARM64_RELOC_GOT_LOAD_PAGEOFF12 not supported";
7420 if ( ! reloc
->r_extern() )
7421 throw "r_extern == 0 and ARM64_RELOC_GOT_LOAD_PAGEOFF12 not supported";
7422 if ( reloc
->r_length() != 2 )
7423 throw "length != 2 and ARM64_RELOC_GOT_LOAD_PAGEOFF12 not supported";
7424 if ( prefixRelocAddend
!= 0 )
7425 throw "ARM64_RELOC_ADDEND followed by ARM64_RELOC_GOT_LOAD_PAGEOFF12 not supported";
7426 instruction
= contentValue
;
7427 target
.addend
= ((instruction
& 0x003FFC00) >> 10);
7428 parser
.addFixups(src
, ld::Fixup::kindStoreARM64GOTLoadPageOff12
, target
);
7430 case ARM64_RELOC_TLVP_LOAD_PAGE21
:
7431 if ( ! reloc
->r_pcrel() )
7432 throw "not pcrel and ARM64_RELOC_TLVP_LOAD_PAGE21 not supported";
7433 if ( ! reloc
->r_extern() )
7434 throw "r_extern == 0 and ARM64_RELOC_TLVP_LOAD_PAGE21 not supported";
7435 if ( reloc
->r_length() != 2 )
7436 throw "length != 2 and ARM64_RELOC_TLVP_LOAD_PAGE21 not supported";
7437 if ( prefixRelocAddend
!= 0 )
7438 throw "ARM64_RELOC_ADDEND followed by ARM64_RELOC_TLVP_LOAD_PAGE21 not supported";
7439 instruction
= contentValue
;
7440 target
.addend
= ((instruction
& 0x60000000) >> 29) | ((instruction
& 0x01FFFFE0) >> 3);
7441 if ( target
.addend
!= 0 )
7442 throw "non-zero addend with ARM64_RELOC_GOT_LOAD_PAGE21 is not supported";
7443 parser
.addFixups(src
, ld::Fixup::kindStoreARM64TLVPLoadPage21
, target
);
7445 case ARM64_RELOC_TLVP_LOAD_PAGEOFF12
:
7446 if ( reloc
->r_pcrel() )
7447 throw "pcrel and ARM64_RELOC_TLVP_LOAD_PAGEOFF12 not supported";
7448 if ( ! reloc
->r_extern() )
7449 throw "r_extern == 0 and ARM64_RELOC_TLVP_LOAD_PAGEOFF12 not supported";
7450 if ( reloc
->r_length() != 2 )
7451 throw "length != 2 and ARM64_RELOC_TLVP_LOAD_PAGEOFF12 not supported";
7452 if ( prefixRelocAddend
!= 0 )
7453 throw "ARM64_RELOC_ADDEND followed by ARM64_RELOC_TLVP_LOAD_PAGEOFF12 not supported";
7454 instruction
= contentValue
;
7455 target
.addend
= ((instruction
& 0x003FFC00) >> 10);
7456 parser
.addFixups(src
, ld::Fixup::kindStoreARM64TLVPLoadPageOff12
, target
);
7458 case ARM64_RELOC_SUBTRACTOR
:
7459 if ( reloc
->r_pcrel() )
7460 throw "ARM64_RELOC_SUBTRACTOR cannot be pc-relative";
7461 if ( reloc
->r_length() < 2 )
7462 throw "ARM64_RELOC_SUBTRACTOR must have r_length of 2 or 3";
7463 if ( !reloc
->r_extern() )
7464 throw "ARM64_RELOC_SUBTRACTOR must have r_extern=1";
7465 if ( nextReloc
->r_type() != ARM64_RELOC_UNSIGNED
)
7466 throw "ARM64_RELOC_SUBTRACTOR must be followed by ARM64_RELOC_UNSIGNED";
7467 if ( prefixRelocAddend
!= 0 )
7468 throw "ARM64_RELOC_ADDEND followed by ARM64_RELOC_SUBTRACTOR not supported";
7470 if ( nextReloc
->r_pcrel() )
7471 throw "ARM64_RELOC_UNSIGNED following a ARM64_RELOC_SUBTRACTOR cannot be pc-relative";
7472 if ( nextReloc
->r_length() != reloc
->r_length() )
7473 throw "ARM64_RELOC_UNSIGNED following a ARM64_RELOC_SUBTRACTOR must have same r_length";
7474 if ( nextReloc
->r_extern() ) {
7475 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(nextReloc
->r_symbolnum());
7476 // use direct reference for local symbols
7477 if ( ((sym
.n_type() & N_TYPE
) == N_SECT
) && (((sym
.n_type() & N_EXT
) == 0) || (parser
.nameFromSymbol(sym
)[0] == 'L')) ) {
7478 parser
.findTargetFromAddressAndSectionNum(sym
.n_value(), sym
.n_sect(), toTarget
);
7479 toTarget
.addend
= contentValue
;
7480 useDirectBinding
= true;
7483 toTarget
.name
= parser
.nameFromSymbol(sym
);
7484 toTarget
.weakImport
= parser
.weakImportFromSymbol(sym
);
7485 toTarget
.addend
= contentValue
;
7486 useDirectBinding
= false;
7490 parser
.findTargetFromAddressAndSectionNum(contentValue
, nextReloc
->r_symbolnum(), toTarget
);
7491 useDirectBinding
= (toTarget
.atom
->scope() == ld::Atom::scopeTranslationUnit
);
7493 if ( useDirectBinding
) {
7494 if ( (toTarget
.atom
->combine() == ld::Atom::combineByNameAndContent
) || (toTarget
.atom
->combine() == ld::Atom::combineByNameAndReferences
) )
7495 parser
.addFixup(src
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, toTarget
.atom
);
7497 parser
.addFixup(src
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, toTarget
.atom
);
7500 parser
.addFixup(src
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, toTarget
.weakImport
, toTarget
.name
);
7501 parser
.addFixup(src
, ld::Fixup::k2of4
, ld::Fixup::kindAddAddend
, toTarget
.addend
);
7502 if ( target
.atom
== NULL
)
7503 parser
.addFixup(src
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractTargetAddress
, false, target
.name
);
7505 parser
.addFixup(src
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractTargetAddress
, target
.atom
);
7506 if ( reloc
->r_length() == 2 )
7507 parser
.addFixup(src
, ld::Fixup::k4of4
, ld::Fixup::kindStoreLittleEndian32
);
7509 parser
.addFixup(src
, ld::Fixup::k4of4
, ld::Fixup::kindStoreLittleEndian64
);
7511 case ARM64_RELOC_POINTER_TO_GOT
:
7512 if ( ! reloc
->r_extern() )
7513 throw "r_extern == 0 and ARM64_RELOC_POINTER_TO_GOT not supported";
7514 if ( prefixRelocAddend
!= 0 )
7515 throw "ARM64_RELOC_ADDEND followed by ARM64_RELOC_POINTER_TO_GOT not supported";
7516 if ( reloc
->r_pcrel() ) {
7517 if ( reloc
->r_length() != 2 )
7518 throw "r_length != 2 and r_extern = 1 and ARM64_RELOC_POINTER_TO_GOT not supported";
7519 parser
.addFixups(src
, ld::Fixup::kindStoreARM64PCRelToGOT
, target
);
7522 if ( reloc
->r_length() != 3 )
7523 throw "r_length != 3 and r_extern = 0 and ARM64_RELOC_POINTER_TO_GOT not supported";
7524 parser
.addFixups(src
, ld::Fixup::kindStoreARM64PointerToGOT
, target
);
7528 throwf("unknown relocation type %d", reloc
->r_type());
7535 template <typename A
>
7536 bool ObjC1ClassSection
<A
>::addRelocFixup(class Parser
<A
>& parser
, const macho_relocation_info
<P
>* reloc
)
7539 FixedSizeSection
<A
>::addRelocFixup(parser
, reloc
);
7541 assert(0 && "needs template specialization");
7546 bool ObjC1ClassSection
<x86
>::addRelocFixup(class Parser
<x86
>& parser
, const macho_relocation_info
<x86::P
>* reloc
)
7548 // if this is the reloc for the super class name string, add implicit reference to super class
7549 if ( ((reloc
->r_address() & R_SCATTERED
) == 0) && (reloc
->r_type() == GENERIC_RELOC_VANILLA
) ) {
7550 assert( reloc
->r_length() == 2 );
7551 assert( ! reloc
->r_pcrel() );
7553 const macho_section
<P
>* sect
= this->machoSection();
7554 Parser
<x86
>::SourceLocation src
;
7555 uint32_t srcAddr
= sect
->addr() + reloc
->r_address();
7556 src
.atom
= this->findAtomByAddress(srcAddr
);
7557 src
.offsetInAtom
= srcAddr
- src
.atom
->objectAddress();
7558 if ( src
.offsetInAtom
== 4 ) {
7559 Parser
<x86
>::TargetDesc stringTarget
;
7560 const uint8_t* fixUpPtr
= file().fileContent() + sect
->offset() + reloc
->r_address();
7561 uint32_t contentValue
= LittleEndian::get32(*((uint32_t*)fixUpPtr
));
7562 parser
.findTargetFromAddressAndSectionNum(contentValue
, reloc
->r_symbolnum(), stringTarget
);
7564 assert(stringTarget
.atom
!= NULL
);
7565 assert(stringTarget
.atom
->contentType() == ld::Atom::typeCString
);
7566 const char* superClassBaseName
= (char*)stringTarget
.atom
->rawContentPointer();
7567 char* superClassName
= new char[strlen(superClassBaseName
) + 20];
7568 strcpy(superClassName
, ".objc_class_name_");
7569 strcat(superClassName
, superClassBaseName
);
7571 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindSetTargetAddress
, false, superClassName
);
7575 return FixedSizeSection
<x86
>::addRelocFixup(parser
, reloc
);
7580 template <typename A
>
7581 bool Objc1ClassReferences
<A
>::addRelocFixup(class Parser
<A
>& parser
, const macho_relocation_info
<P
>* reloc
)
7584 PointerToCStringSection
<A
>::addRelocFixup(parser
, reloc
);
7586 assert(0 && "needs template specialization");
7593 bool Objc1ClassReferences
<x86
>::addRelocFixup(class Parser
<x86
>& parser
, const macho_relocation_info
<x86::P
>* reloc
)
7595 // add implict class refs, fixups not usable yet, so look at relocations
7596 assert( (reloc
->r_address() & R_SCATTERED
) == 0 );
7597 assert( reloc
->r_type() == GENERIC_RELOC_VANILLA
);
7598 assert( reloc
->r_length() == 2 );
7599 assert( ! reloc
->r_pcrel() );
7601 const macho_section
<P
>* sect
= this->machoSection();
7602 Parser
<x86
>::SourceLocation src
;
7603 uint32_t srcAddr
= sect
->addr() + reloc
->r_address();
7604 src
.atom
= this->findAtomByAddress(srcAddr
);
7605 src
.offsetInAtom
= srcAddr
- src
.atom
->objectAddress();
7606 Parser
<x86
>::TargetDesc stringTarget
;
7607 const uint8_t* fixUpPtr
= file().fileContent() + sect
->offset() + reloc
->r_address();
7608 uint32_t contentValue
= LittleEndian::get32(*((uint32_t*)fixUpPtr
));
7609 parser
.findTargetFromAddressAndSectionNum(contentValue
, reloc
->r_symbolnum(), stringTarget
);
7611 assert(stringTarget
.atom
!= NULL
);
7612 assert(stringTarget
.atom
->contentType() == ld::Atom::typeCString
);
7613 const char* baseClassName
= (char*)stringTarget
.atom
->rawContentPointer();
7614 char* objcClassName
= new char[strlen(baseClassName
) + 20];
7615 strcpy(objcClassName
, ".objc_class_name_");
7616 strcat(objcClassName
, baseClassName
);
7618 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindSetTargetAddress
, false, objcClassName
);
7621 return PointerToCStringSection
<x86
>::addRelocFixup(parser
, reloc
);
7624 #if SUPPORT_ARCH_arm64
7626 void Section
<arm64
>::addLOH(class Parser
<arm64
>& parser
, int kind
, int count
, const uint64_t addrs
[]) {
7628 case LOH_ARM64_ADRP_ADRP
:
7629 case LOH_ARM64_ADRP_LDR
:
7630 case LOH_ARM64_ADRP_ADD
:
7631 case LOH_ARM64_ADRP_LDR_GOT
:
7633 warning("arm64 Linker Optimiztion Hint %d has wrong number of arguments", kind
);
7635 case LOH_ARM64_ADRP_ADD_LDR
:
7636 case LOH_ARM64_ADRP_LDR_GOT_LDR
:
7637 case LOH_ARM64_ADRP_ADD_STR
:
7638 case LOH_ARM64_ADRP_LDR_GOT_STR
:
7640 warning("arm64 Linker Optimiztion Hint %d has wrong number of arguments", kind
);
7643 // pick lowest address in tuple for use as offsetInAtom
7644 uint64_t lowestAddress
= addrs
[0];
7645 for(int i
=1; i
< count
; ++i
) {
7646 if ( addrs
[i
] < lowestAddress
)
7647 lowestAddress
= addrs
[i
];
7649 // verify all other address are in same atom
7650 Atom
<arm64
>* inAtom
= parser
.findAtomByAddress(lowestAddress
);
7651 const uint64_t atomStartAddr
= inAtom
->objectAddress();
7652 const uint64_t atomEndAddr
= atomStartAddr
+ inAtom
->size();
7653 for(int i
=0; i
< count
; ++i
) {
7654 if ( (addrs
[i
] < atomStartAddr
) || (addrs
[i
] >= atomEndAddr
) ) {
7655 warning("arm64 Linker Optimiztion Hint addresses are not in same atom: 0x%08llX and 0x%08llX",
7656 lowestAddress
, addrs
[i
]);
7657 return; // skip this LOH
7659 if ( (addrs
[i
] & 0x3) != 0 ) {
7660 warning("arm64 Linker Optimiztion Hint address is not 4-byte aligned: 0x%08llX", addrs
[i
]);
7661 return; // skip this LOH
7663 if ( (addrs
[i
] - lowestAddress
) > 0xFFFF ) {
7664 if ( parser
.verboseOptimizationHints() ) {
7665 warning("arm64 Linker Optimiztion Hint addresses are too far apart: 0x%08llX and 0x%08llX",
7666 lowestAddress
, addrs
[i
]);
7668 return; // skip this LOH
7672 // encoded kind, count, and address deltas in 64-bit addend
7673 ld::Fixup::LOH_arm64 extra
;
7675 extra
.info
.kind
= kind
;
7676 extra
.info
.count
= count
-1;
7677 extra
.info
.delta1
= (addrs
[0] - lowestAddress
) >> 2;
7678 extra
.info
.delta2
= (count
> 1) ? ((addrs
[1] - lowestAddress
) >> 2) : 0;
7679 extra
.info
.delta3
= (count
> 2) ? ((addrs
[2] - lowestAddress
) >> 2) : 0;
7680 extra
.info
.delta4
= (count
> 3) ? ((addrs
[3] - lowestAddress
) >> 2) : 0;
7681 typename Parser
<arm64
>::SourceLocation
src(inAtom
, lowestAddress
- inAtom
->objectAddress());
7682 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindLinkerOptimizationHint
, extra
.addend
);
7687 template <typename A
>
7688 void Section
<A
>::addLOH(class Parser
<A
>& parser
, int kind
, int count
, const uint64_t addrs
[]) {
7692 template <typename A
>
7693 void Section
<A
>::makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
&)
7695 const macho_section
<P
>* sect
= this->machoSection();
7696 if ( sect
->reloff() + (sect
->nreloc() * sizeof(macho_relocation_info
<P
>)) > parser
.fileLength() )
7697 throwf("relocations for section %s/%s extends beyond end of file,", sect
->segname(), Section
<A
>::makeSectionName(sect
) );
7698 const macho_relocation_info
<P
>* relocs
= (macho_relocation_info
<P
>*)(file().fileContent() + sect
->reloff());
7699 const uint32_t relocCount
= sect
->nreloc();
7700 for (uint32_t r
= 0; r
< relocCount
; ++r
) {
7702 if ( this->addRelocFixup(parser
, &relocs
[r
]) )
7705 catch (const char* msg
) {
7706 throwf("in section %s,%s reloc %u: %s", sect
->segname(), Section
<A
>::makeSectionName(sect
), r
, msg
);
7710 // add follow-on fixups if .o file is missing .subsections_via_symbols
7711 if ( this->addFollowOnFixups() ) {
7712 Atom
<A
>* end
= &_endAtoms
[-1];
7713 for(Atom
<A
>* p
= _beginAtoms
; p
< end
; ++p
) {
7714 typename Parser
<A
>::SourceLocation
src(p
, 0);
7715 Atom
<A
>* nextAtom
= &p
[1];
7716 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindNoneFollowOn
, nextAtom
);
7719 else if ( this->type() == ld::Section::typeCode
) {
7720 // if FDE broke text not at a symbol, use followOn to keep code together
7721 Atom
<A
>* end
= &_endAtoms
[-1];
7722 for(Atom
<A
>* p
= _beginAtoms
; p
< end
; ++p
) {
7723 typename Parser
<A
>::SourceLocation
src(p
, 0);
7724 Atom
<A
>* nextAtom
= &p
[1];
7725 if ( (p
->symbolTableInclusion() == ld::Atom::symbolTableIn
) && (nextAtom
->symbolTableInclusion() == ld::Atom::symbolTableNotIn
) ) {
7726 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindNoneFollowOn
, nextAtom
);
7730 if ( !this->_altEntries
.empty() && !this->addFollowOnFixups() ) {
7731 if ( _altEntries
.count(_beginAtoms
) != 0 )
7732 warning("N_ALT_ENTRY bit set on first atom in section %s/%s", sect
->segname(), Section
<A
>::makeSectionName(sect
));
7734 Atom
<A
>* end
= &_endAtoms
[-1];
7735 for(Atom
<A
>* p
= _beginAtoms
; p
< end
; ++p
) {
7736 Atom
<A
>* nextAtom
= &p
[1];
7737 // <rdar://problem/22960070> support alt_entry aliases (alias process already added followOn, don't repeat)
7738 if ( (_altEntries
.count(nextAtom
) != 0) && (p
->_objAddress
!= nextAtom
->_objAddress
) ) {
7739 typename Parser
<A
>::SourceLocation
src(p
, 0);
7740 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindNoneFollowOn
, nextAtom
);
7741 typename Parser
<A
>::SourceLocation
src2(nextAtom
, 0);
7742 parser
.addFixup(src2
, ld::Fixup::k1of1
, ld::Fixup::kindNoneGroupSubordinate
, p
);
7747 // <rdar://problem/9218847> track data-in-code
7748 if ( parser
.hasDataInCodeLabels() && (this->type() == ld::Section::typeCode
) ) {
7749 for (uint32_t i
=0; i
< parser
.symbolCount(); ++i
) {
7750 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(i
);
7752 if ( (sym
.n_type() & N_STAB
) != 0 )
7754 // ignore non-definitions
7755 if ( (sym
.n_type() & N_TYPE
) != N_SECT
)
7758 // 'L' labels do not denote atom breaks
7759 const char* symbolName
= parser
.nameFromSymbol(sym
);
7760 if ( symbolName
[0] == 'L' ) {
7761 if ( strncmp(symbolName
, "L$start$", 8) == 0 ) {
7762 ld::Fixup::Kind kind
= ld::Fixup::kindNone
;
7763 if ( strncmp(&symbolName
[8], "data$", 5) == 0 )
7764 kind
= ld::Fixup::kindDataInCodeStartData
;
7765 else if ( strncmp(&symbolName
[8], "code$", 5) == 0 )
7766 kind
= ld::Fixup::kindDataInCodeEnd
;
7767 else if ( strncmp(&symbolName
[8], "jt8$", 4) == 0 )
7768 kind
= ld::Fixup::kindDataInCodeStartJT8
;
7769 else if ( strncmp(&symbolName
[8], "jt16$", 4) == 0 )
7770 kind
= ld::Fixup::kindDataInCodeStartJT16
;
7771 else if ( strncmp(&symbolName
[8], "jt32$", 4) == 0 )
7772 kind
= ld::Fixup::kindDataInCodeStartJT32
;
7773 else if ( strncmp(&symbolName
[8], "jta32$", 4) == 0 )
7774 kind
= ld::Fixup::kindDataInCodeStartJTA32
;
7776 warning("unknown L$start$ label %s in file %s", symbolName
, this->file().path());
7777 if ( kind
!= ld::Fixup::kindNone
) {
7778 Atom
<A
>* inAtom
= parser
.findAtomByAddress(sym
.n_value());
7779 typename Parser
<A
>::SourceLocation
src(inAtom
, sym
.n_value() - inAtom
->objectAddress());
7780 parser
.addFixup(src
, ld::Fixup::k1of1
, kind
);
7787 // <rdar://problem/11150575> Handle LC_DATA_IN_CODE in object files
7788 if ( this->type() == ld::Section::typeCode
) {
7789 const pint_t startAddr
= this->_machOSection
->addr();
7790 const pint_t endAddr
= startAddr
+ this->_machOSection
->size();
7791 for ( const macho_data_in_code_entry
<P
>* p
= parser
.dataInCodeStart(); p
!= parser
.dataInCodeEnd(); ++p
) {
7792 if ( (p
->offset() >= startAddr
) && (p
->offset() < endAddr
) ) {
7793 ld::Fixup::Kind kind
= ld::Fixup::kindNone
;
7794 switch ( p
->kind() ) {
7795 case DICE_KIND_DATA
:
7796 kind
= ld::Fixup::kindDataInCodeStartData
;
7798 case DICE_KIND_JUMP_TABLE8
:
7799 kind
= ld::Fixup::kindDataInCodeStartJT8
;
7801 case DICE_KIND_JUMP_TABLE16
:
7802 kind
= ld::Fixup::kindDataInCodeStartJT16
;
7804 case DICE_KIND_JUMP_TABLE32
:
7805 kind
= ld::Fixup::kindDataInCodeStartJT32
;
7807 case DICE_KIND_ABS_JUMP_TABLE32
:
7808 kind
= ld::Fixup::kindDataInCodeStartJTA32
;
7811 kind
= ld::Fixup::kindDataInCodeStartData
;
7812 warning("uknown LC_DATA_IN_CODE kind (%d) at offset 0x%08X", p
->kind(), p
->offset());
7815 Atom
<A
>* inAtom
= parser
.findAtomByAddress(p
->offset());
7816 typename Parser
<A
>::SourceLocation
srcStart(inAtom
, p
->offset() - inAtom
->objectAddress());
7817 parser
.addFixup(srcStart
, ld::Fixup::k1of1
, kind
);
7818 typename Parser
<A
>::SourceLocation
srcEnd(inAtom
, p
->offset() + p
->length() - inAtom
->objectAddress());
7819 parser
.addFixup(srcEnd
, ld::Fixup::k1of1
, ld::Fixup::kindDataInCodeEnd
);
7824 // <rdar://problem/11945700> convert linker optimization hints into internal format
7825 if ( this->type() == ld::Section::typeCode
&& parser
.hasOptimizationHints() ) {
7826 const pint_t startAddr
= this->_machOSection
->addr();
7827 const pint_t endAddr
= startAddr
+ this->_machOSection
->size();
7828 for (const uint8_t* p
= parser
.optimizationHintsStart(); p
< parser
.optimizationHintsEnd(); ) {
7830 int32_t kind
= read_uleb128(&p
, parser
.optimizationHintsEnd());
7831 if ( kind
== 0 ) // padding at end of loh buffer
7834 warning("malformed uleb128 kind in LC_LINKER_OPTIMIZATION_HINTS");
7837 int32_t count
= read_uleb128(&p
, parser
.optimizationHintsEnd());
7838 if ( count
== -1 ) {
7839 warning("malformed uleb128 count in LC_LINKER_OPTIMIZATION_HINTS");
7843 warning("address count > 3 in LC_LINKER_OPTIMIZATION_HINTS");
7846 for (int32_t i
=0; i
< count
; ++i
) {
7847 addrs
[i
] = read_uleb128(&p
, parser
.optimizationHintsEnd());
7849 if ( (startAddr
<= addrs
[0]) && (addrs
[0] < endAddr
) ) {
7850 this->addLOH(parser
, kind
, count
, addrs
);
7851 //fprintf(stderr, "kind=%d", kind);
7852 //for (int32_t i=0; i < count; ++i) {
7853 // fprintf(stderr, ", addr=0x%08llX", addrs[i]);
7855 //fprintf(stderr, "\n");
7861 // add follow-on fixups for aliases
7862 if ( _hasAliases
) {
7863 for(Atom
<A
>* p
= _beginAtoms
; p
< _endAtoms
; ++p
) {
7864 if ( p
->isAlias() && ! this->addFollowOnFixups() ) {
7865 Atom
<A
>* targetOfAlias
= &p
[1];
7866 assert(p
< &_endAtoms
[-1]);
7867 assert(p
->_objAddress
== targetOfAlias
->_objAddress
);
7868 typename Parser
<A
>::SourceLocation
src(p
, 0);
7869 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindNoneFollowOn
, targetOfAlias
);
7878 // main function used by linker to instantiate ld::Files
7880 ld::relocatable::File
* parse(const uint8_t* fileContent
, uint64_t fileLength
,
7881 const char* path
, time_t modTime
, ld::File::Ordinal ordinal
, const ParserOptions
& opts
)
7883 switch ( opts
.architecture
) {
7884 #if SUPPORT_ARCH_x86_64
7885 case CPU_TYPE_X86_64
:
7886 if ( mach_o::relocatable::Parser
<x86_64
>::validFile(fileContent
) )
7887 return mach_o::relocatable::Parser
<x86_64
>::parse(fileContent
, fileLength
, path
, modTime
, ordinal
, opts
);
7890 #if SUPPORT_ARCH_i386
7892 if ( mach_o::relocatable::Parser
<x86
>::validFile(fileContent
) )
7893 return mach_o::relocatable::Parser
<x86
>::parse(fileContent
, fileLength
, path
, modTime
, ordinal
, opts
);
7896 #if SUPPORT_ARCH_arm_any
7898 if ( mach_o::relocatable::Parser
<arm
>::validFile(fileContent
, opts
.objSubtypeMustMatch
, opts
.subType
) )
7899 return mach_o::relocatable::Parser
<arm
>::parse(fileContent
, fileLength
, path
, modTime
, ordinal
, opts
);
7902 #if SUPPORT_ARCH_arm64
7903 case CPU_TYPE_ARM64
:
7904 if ( mach_o::relocatable::Parser
<arm64
>::validFile(fileContent
, opts
.objSubtypeMustMatch
, opts
.subType
) )
7905 return mach_o::relocatable::Parser
<arm64
>::parse(fileContent
, fileLength
, path
, modTime
, ordinal
, opts
);
7913 // used by archive reader to validate member object file
7915 bool isObjectFile(const uint8_t* fileContent
, uint64_t fileLength
, const ParserOptions
& opts
)
7917 switch ( opts
.architecture
) {
7918 case CPU_TYPE_X86_64
:
7919 return ( mach_o::relocatable::Parser
<x86_64
>::validFile(fileContent
) );
7921 return ( mach_o::relocatable::Parser
<x86
>::validFile(fileContent
) );
7923 return ( mach_o::relocatable::Parser
<arm
>::validFile(fileContent
, opts
.objSubtypeMustMatch
, opts
.subType
) );
7924 case CPU_TYPE_ARM64
:
7925 return ( mach_o::relocatable::Parser
<arm64
>::validFile(fileContent
, opts
.objSubtypeMustMatch
, opts
.subType
) );
7931 // used by linker to infer architecture when no -arch is on command line
7933 bool isObjectFile(const uint8_t* fileContent
, cpu_type_t
* result
, cpu_subtype_t
* subResult
, Options::Platform
* platform
)
7935 if ( mach_o::relocatable::Parser
<x86_64
>::validFile(fileContent
) ) {
7936 *result
= CPU_TYPE_X86_64
;
7937 const macho_header
<Pointer64
<LittleEndian
> >* header
= (const macho_header
<Pointer64
<LittleEndian
> >*)fileContent
;
7938 *subResult
= header
->cpusubtype();
7939 *platform
= Parser
<x86_64
>::findPlatform(header
);
7942 if ( mach_o::relocatable::Parser
<x86
>::validFile(fileContent
) ) {
7943 const macho_header
<Pointer32
<LittleEndian
> >* header
= (const macho_header
<Pointer32
<LittleEndian
> >*)fileContent
;
7944 *result
= CPU_TYPE_I386
;
7945 *subResult
= CPU_SUBTYPE_X86_ALL
;
7946 *platform
= Parser
<x86
>::findPlatform(header
);
7949 if ( mach_o::relocatable::Parser
<arm
>::validFile(fileContent
, false, 0) ) {
7950 const macho_header
<Pointer32
<LittleEndian
> >* header
= (const macho_header
<Pointer32
<LittleEndian
> >*)fileContent
;
7951 *result
= CPU_TYPE_ARM
;
7952 *subResult
= header
->cpusubtype();
7953 *platform
= Parser
<arm
>::findPlatform(header
);
7956 if ( mach_o::relocatable::Parser
<arm64
>::validFile(fileContent
, false, 0) ) {
7957 const macho_header
<Pointer64
<LittleEndian
> >* header
= (const macho_header
<Pointer64
<LittleEndian
> >*)fileContent
;
7958 *result
= CPU_TYPE_ARM64
;
7959 *subResult
= header
->cpusubtype();
7960 *platform
= Parser
<arm64
>::findPlatform(header
);
7967 // used by linker is error messages to describe bad .o file
7969 const char* archName(const uint8_t* fileContent
)
7971 if ( mach_o::relocatable::Parser
<x86_64
>::validFile(fileContent
) ) {
7972 return mach_o::relocatable::Parser
<x86_64
>::fileKind(fileContent
);
7974 if ( mach_o::relocatable::Parser
<x86
>::validFile(fileContent
) ) {
7975 return mach_o::relocatable::Parser
<x86
>::fileKind(fileContent
);
7977 if ( mach_o::relocatable::Parser
<arm
>::validFile(fileContent
, false, 0) ) {
7978 return mach_o::relocatable::Parser
<arm
>::fileKind(fileContent
);
7984 // Used by archive reader when -ObjC option is specified
7986 bool hasObjC2Categories(const uint8_t* fileContent
)
7988 if ( mach_o::relocatable::Parser
<x86_64
>::validFile(fileContent
) ) {
7989 return mach_o::relocatable::Parser
<x86_64
>::hasObjC2Categories(fileContent
);
7991 else if ( mach_o::relocatable::Parser
<arm
>::validFile(fileContent
, false, 0) ) {
7992 return mach_o::relocatable::Parser
<arm
>::hasObjC2Categories(fileContent
);
7994 else if ( mach_o::relocatable::Parser
<x86
>::validFile(fileContent
, false, 0) ) {
7995 return mach_o::relocatable::Parser
<x86
>::hasObjC2Categories(fileContent
);
7997 #if SUPPORT_ARCH_arm64
7998 else if ( mach_o::relocatable::Parser
<arm64
>::validFile(fileContent
, false, 0) ) {
7999 return mach_o::relocatable::Parser
<arm64
>::hasObjC2Categories(fileContent
);
8006 // Used by archive reader when -ObjC option is specified
8008 bool hasObjC1Categories(const uint8_t* fileContent
)
8010 if ( mach_o::relocatable::Parser
<x86
>::validFile(fileContent
, false, 0) ) {
8011 return mach_o::relocatable::Parser
<x86
>::hasObjC1Categories(fileContent
);
8017 // Used by bitcode obfuscator to get a list of non local symbols from object file
8019 bool getNonLocalSymbols(const uint8_t* fileContent
, std::vector
<const char*> &syms
)
8021 if ( mach_o::relocatable::Parser
<x86_64
>::validFile(fileContent
) ) {
8022 return mach_o::relocatable::Parser
<x86_64
>::getNonLocalSymbols(fileContent
, syms
);
8024 else if ( mach_o::relocatable::Parser
<arm
>::validFile(fileContent
, false, 0) ) {
8025 return mach_o::relocatable::Parser
<arm
>::getNonLocalSymbols(fileContent
, syms
);
8027 else if ( mach_o::relocatable::Parser
<x86
>::validFile(fileContent
, false, 0) ) {
8028 return mach_o::relocatable::Parser
<x86
>::getNonLocalSymbols(fileContent
, syms
);
8030 else if ( mach_o::relocatable::Parser
<arm64
>::validFile(fileContent
, false, 0) ) {
8031 return mach_o::relocatable::Parser
<arm64
>::getNonLocalSymbols(fileContent
, syms
);
8038 } // namespace relocatable
8039 } // namespace mach_o