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
),
87 _canScatterAtoms(false),
88 _hasllvmProfiling(false),
89 _objcHasCategoryClassPropertiesField(false),
90 _srcKind(kSourceUnknown
) { }
93 // overrides of ld::File
94 virtual bool forEachAtom(ld::File::AtomHandler
&) const;
95 virtual bool justInTimeforEachAtom(const char* name
, ld::File::AtomHandler
&) const
97 virtual const ld::VersionSet
& platforms() const { return _platforms
; }
99 // overrides of ld::relocatable::File
100 virtual bool hasObjC() const { return _hasObjC
; }
101 virtual bool objcHasCategoryClassPropertiesField() const
102 { return _objcHasCategoryClassPropertiesField
; }
103 virtual uint32_t cpuSubType() const { return _cpuSubType
; }
104 virtual DebugInfoKind
debugInfo() const { return _debugInfoKind
; }
105 virtual const std::vector
<ld::relocatable::File::Stab
>* stabs() const { return &_stabs
; }
106 virtual bool canScatterAtoms() const { return _canScatterAtoms
; }
107 virtual bool hasllvmProfiling() const { return _hasllvmProfiling
; }
108 virtual const char* translationUnitSource() const;
109 virtual LinkerOptionsList
* linkerOptions() const { return &_linkerOptions
; }
110 virtual const ToolVersionList
& toolVersions() const { return _toolVersions
; }
111 virtual uint8_t swiftVersion() const { return _swiftVersion
; }
112 virtual ld::Bitcode
* getBitcode() const { return _bitcode
.get(); }
113 virtual SourceKind
sourceKind() const { return _srcKind
; }
115 virtual const uint8_t* fileContent() const { return _fileContent
; }
116 virtual const std::vector
<AstTimeAndPath
>* astFiles() const { return &_astFiles
; }
118 void setHasllvmProfiling() { _hasllvmProfiling
= true; }
120 friend class Atom
<A
>;
121 friend class Section
<A
>;
122 friend class Parser
<A
>;
123 friend class CFISection
<A
>::OAS
;
125 typedef typename
A::P P
;
127 const uint8_t* _fileContent
;
128 Section
<A
>** _sectionsArray
;
129 uint8_t* _atomsArray
;
130 uint8_t* _aliasAtomsArray
;
131 uint32_t _sectionsArrayCount
;
132 uint32_t _atomsArrayCount
;
133 uint32_t _aliasAtomsArrayCount
;
134 std::vector
<ld::Fixup
> _fixups
;
135 std::vector
<ld::Atom::UnwindInfo
> _unwindInfos
;
136 std::vector
<ld::Atom::LineInfo
> _lineInfos
;
137 std::vector
<ld::relocatable::File::Stab
>_stabs
;
138 std::vector
<AstTimeAndPath
> _astFiles
;
139 ld::relocatable::File::DebugInfoKind _debugInfoKind
;
140 const char* _dwarfTranslationUnitPath
;
141 const macho_section
<P
>* _dwarfDebugInfoSect
;
142 const macho_section
<P
>* _dwarfDebugAbbrevSect
;
143 const macho_section
<P
>* _dwarfDebugLineSect
;
144 const macho_section
<P
>* _dwarfDebugStringSect
;
146 uint8_t _swiftVersion
;
147 uint32_t _cpuSubType
;
148 uint32_t _minOSVersion
;
149 ld::VersionSet _platforms
;
150 bool _canScatterAtoms
;
151 bool _hasllvmProfiling
;
152 bool _objcHasCategoryClassPropertiesField
;
153 std::vector
<std::vector
<const char*> > _linkerOptions
;
154 std::unique_ptr
<ld::Bitcode
> _bitcode
;
156 ToolVersionList _toolVersions
;
160 template <typename A
>
161 class Section
: public ld::Section
164 typedef typename
A::P::uint_t pint_t
;
165 typedef typename
A::P P
;
166 typedef typename
A::P::E E
;
168 virtual ~Section() { }
169 class File
<A
>& file() const { return _file
; }
170 const macho_section
<P
>* machoSection() const { return _machOSection
; }
171 uint32_t sectionNum(class Parser
<A
>&) const;
172 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
);
173 virtual ld::Atom::ContentType
contentType() { return ld::Atom::typeUnclassified
; }
174 virtual bool dontDeadStrip() { return (this->_machOSection
->flags() & S_ATTR_NO_DEAD_STRIP
); }
175 virtual bool dontDeadStripIfReferencesLive() { return ( (this->_machOSection
!= NULL
) && (this->_machOSection
->flags() & S_ATTR_LIVE_SUPPORT
) ); }
176 virtual Atom
<A
>* findAtomByAddress(pint_t addr
) { return this->findContentAtomByAddress(addr
, this->_beginAtoms
, this->_endAtoms
); }
177 virtual bool addFollowOnFixups() const { return ! _file
.canScatterAtoms(); }
178 virtual uint32_t appendAtoms(class Parser
<A
>& parser
, uint8_t* buffer
,
179 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
180 const struct Parser
<A
>::CFI_CU_InfoArrays
&) = 0;
181 virtual uint32_t computeAtomCount(class Parser
<A
>& parser
,
182 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
183 const struct Parser
<A
>::CFI_CU_InfoArrays
&) = 0;
184 virtual void makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
&);
185 virtual bool addRelocFixup(class Parser
<A
>& parser
, const macho_relocation_info
<P
>*);
186 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const { return 0; }
187 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
188 const ld::IndirectBindingTable
& ind
) const { return false; }
189 virtual bool ignoreLabel(const char* label
) const { return false; }
190 static const char* makeSectionName(const macho_section
<typename
A::P
>* s
);
193 Section(File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
194 : ld::Section(makeSegmentName(s
), makeSectionName(s
), sectionType(s
)),
195 _file(f
), _machOSection(s
), _beginAtoms(NULL
), _endAtoms(NULL
), _hasAliases(false) { }
196 Section(File
<A
>& f
, const char* segName
, const char* sectName
, ld::Section::Type t
, bool hidden
=false)
197 : ld::Section(segName
, sectName
, t
, hidden
), _file(f
), _machOSection(NULL
),
198 _beginAtoms(NULL
), _endAtoms(NULL
), _hasAliases(false) { }
201 Atom
<A
>* findContentAtomByAddress(pint_t addr
, class Atom
<A
>* start
, class Atom
<A
>* end
);
202 uint32_t x86_64PcRelOffset(uint8_t r_type
);
203 void addLOH(class Parser
<A
>& parser
, int kind
, int count
, const uint64_t addrs
[]);
204 static const char* makeSegmentName(const macho_section
<typename
A::P
>* s
);
205 static bool readable(const macho_section
<typename
A::P
>* s
);
206 static bool writable(const macho_section
<typename
A::P
>* s
);
207 static bool exectuable(const macho_section
<typename
A::P
>* s
);
208 static ld::Section::Type
sectionType(const macho_section
<typename
A::P
>* s
);
211 const macho_section
<P
>* _machOSection
;
212 class Atom
<A
>* _beginAtoms
;
213 class Atom
<A
>* _endAtoms
;
215 std::set
<const class Atom
<A
>*> _altEntries
;
219 template <typename A
>
220 class CFISection
: public Section
<A
>
223 CFISection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
224 : Section
<A
>(f
, s
) { }
225 uint32_t cfiCount(Parser
<A
>& parser
);
227 virtual ld::Atom::ContentType
contentType() { return ld::Atom::typeCFI
; }
228 virtual uint32_t computeAtomCount(class Parser
<A
>& parser
, struct Parser
<A
>::LabelAndCFIBreakIterator
& it
, const struct Parser
<A
>::CFI_CU_InfoArrays
&);
229 virtual uint32_t appendAtoms(class Parser
<A
>& parser
, uint8_t* buffer
, struct Parser
<A
>::LabelAndCFIBreakIterator
& it
, const struct Parser
<A
>::CFI_CU_InfoArrays
&);
230 virtual void makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
&);
231 virtual bool addFollowOnFixups() const { return false; }
235 /// ObjectFileAddressSpace is used as a template parameter to UnwindCursor for parsing
236 /// dwarf CFI information in an object file.
241 typedef typename
A::P::uint_t pint_t
;
242 typedef typename
A::P P
;
243 typedef typename
A::P::E E
;
244 typedef typename
A::P::uint_t sint_t
;
246 OAS(CFISection
<A
>& ehFrameSection
, const uint8_t* ehFrameBuffer
) :
247 _ehFrameSection(ehFrameSection
),
248 _ehFrameContent(ehFrameBuffer
),
249 _ehFrameStartAddr(ehFrameSection
.machoSection()->addr()),
250 _ehFrameEndAddr(ehFrameSection
.machoSection()->addr()+ehFrameSection
.machoSection()->size()) {}
252 uint8_t get8(pint_t addr
) { return *((uint8_t*)mappedAddress(addr
)); }
253 uint16_t get16(pint_t addr
) { return E::get16(*((uint16_t*)mappedAddress(addr
))); }
254 uint32_t get32(pint_t addr
) { return E::get32(*((uint32_t*)mappedAddress(addr
))); }
255 uint64_t get64(pint_t addr
) { return E::get64(*((uint64_t*)mappedAddress(addr
))); }
256 pint_t
getP(pint_t addr
) { return P::getP(*((pint_t
*)mappedAddress(addr
))); }
257 uint64_t getULEB128(pint_t
& addr
, pint_t end
);
258 int64_t getSLEB128(pint_t
& addr
, pint_t end
);
259 pint_t
getEncodedP(pint_t
& addr
, pint_t end
, uint8_t encoding
);
261 const void* mappedAddress(pint_t addr
);
263 CFISection
<A
>& _ehFrameSection
;
264 const uint8_t* _ehFrameContent
;
265 pint_t _ehFrameStartAddr
;
266 pint_t _ehFrameEndAddr
;
270 typedef typename
A::P::uint_t pint_t
;
271 typedef libunwind::CFI_Atom_Info
<OAS
> CFI_Atom_Info
;
273 void cfiParse(class Parser
<A
>& parser
, uint8_t* buffer
, CFI_Atom_Info cfiArray
[], uint32_t& cfiCount
, const pint_t cuStarts
[], uint32_t cuCount
);
274 bool needsRelocating();
276 static bool bigEndian();
278 void addCiePersonalityFixups(class Parser
<A
>& parser
, const CFI_Atom_Info
* cieInfo
);
279 static void warnFunc(void* ref
, uint64_t funcAddr
, const char* msg
);
283 template <typename A
>
284 class CUSection
: public Section
<A
>
287 CUSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
288 : Section
<A
>(f
, s
) { }
290 typedef typename
A::P::uint_t pint_t
;
291 typedef typename
A::P P
;
292 typedef typename
A::P::E E
;
294 virtual uint32_t computeAtomCount(class Parser
<A
>& parser
, struct Parser
<A
>::LabelAndCFIBreakIterator
& it
, const struct Parser
<A
>::CFI_CU_InfoArrays
&) { return 0; }
295 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; }
296 virtual void makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
&);
297 virtual bool addFollowOnFixups() const { return false; }
300 pint_t functionStartAddress
;
301 uint32_t functionSymbolIndex
;
302 uint32_t rangeLength
;
303 uint32_t compactUnwindInfo
;
304 const char* personality
;
311 void parse(class Parser
<A
>& parser
, uint32_t cnt
, Info array
[]);
312 static bool encodingMeansUseDwarf(compact_unwind_encoding_t enc
);
317 const char* personalityName(class Parser
<A
>& parser
, const macho_relocation_info
<P
>* reloc
);
319 static int infoSorter(const void* l
, const void* r
);
324 template <typename A
>
325 class TentativeDefinitionSection
: public Section
<A
>
328 TentativeDefinitionSection(Parser
<A
>& parser
, File
<A
>& f
)
329 : Section
<A
>(f
, "__DATA", "__comm/tent", ld::Section::typeTentativeDefs
) {}
331 virtual ld::Atom::ContentType
contentType() { return ld::Atom::typeZeroFill
; }
332 virtual bool addFollowOnFixups() const { return false; }
333 virtual Atom
<A
>* findAtomByAddress(typename
A::P::uint_t addr
) { throw "TentativeDefinitionSection::findAtomByAddress() should never be called"; }
334 virtual uint32_t computeAtomCount(class Parser
<A
>& parser
, struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
335 const struct Parser
<A
>::CFI_CU_InfoArrays
&);
336 virtual uint32_t appendAtoms(class Parser
<A
>& parser
, uint8_t* buffer
,
337 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
338 const struct Parser
<A
>::CFI_CU_InfoArrays
&);
339 virtual void makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
&) {}
341 typedef typename
A::P::uint_t pint_t
;
342 typedef typename
A::P P
;
346 template <typename A
>
347 class AbsoluteSymbolSection
: public Section
<A
>
350 AbsoluteSymbolSection(Parser
<A
>& parser
, File
<A
>& f
)
351 : Section
<A
>(f
, "__DATA", "__abs", ld::Section::typeAbsoluteSymbols
, true) {}
353 virtual ld::Atom::ContentType
contentType() { return ld::Atom::typeUnclassified
; }
354 virtual bool dontDeadStrip() { return false; }
355 virtual ld::Atom::Alignment
alignmentForAddress(typename
A::P::uint_t addr
) { return ld::Atom::Alignment(0); }
356 virtual bool addFollowOnFixups() const { return false; }
357 virtual Atom
<A
>* findAtomByAddress(typename
A::P::uint_t addr
) { throw "AbsoluteSymbolSection::findAtomByAddress() should never be called"; }
358 virtual uint32_t computeAtomCount(class Parser
<A
>& parser
, struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
359 const struct Parser
<A
>::CFI_CU_InfoArrays
&);
360 virtual uint32_t appendAtoms(class Parser
<A
>& parser
, uint8_t* buffer
,
361 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
362 const struct Parser
<A
>::CFI_CU_InfoArrays
&);
363 virtual void makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
&) {}
364 virtual Atom
<A
>* findAbsAtomForValue(typename
A::P::uint_t
);
367 typedef typename
A::P::uint_t pint_t
;
368 typedef typename
A::P P
;
372 template <typename A
>
373 class SymboledSection
: public Section
<A
>
376 SymboledSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
);
377 virtual ld::Atom::ContentType
contentType() { return _type
; }
378 virtual bool dontDeadStrip();
379 virtual uint32_t computeAtomCount(class Parser
<A
>& parser
, struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
380 const struct Parser
<A
>::CFI_CU_InfoArrays
&);
381 virtual uint32_t appendAtoms(class Parser
<A
>& parser
, uint8_t* buffer
,
382 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
383 const struct Parser
<A
>::CFI_CU_InfoArrays
&);
385 typedef typename
A::P::uint_t pint_t
;
386 typedef typename
A::P P
;
388 ld::Atom::ContentType _type
;
392 template <typename A
>
393 class TLVDefsSection
: public SymboledSection
<A
>
396 TLVDefsSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
) :
397 SymboledSection
<A
>(parser
, f
, s
) { }
399 typedef typename
A::P::uint_t pint_t
;
401 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
) { return ld::Atom::Alignment(log2(sizeof(pint_t
))); }
408 template <typename A
>
409 class ImplicitSizeSection
: public Section
<A
>
412 ImplicitSizeSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
413 : Section
<A
>(f
, s
) { }
414 virtual uint32_t computeAtomCount(class Parser
<A
>& parser
, struct Parser
<A
>::LabelAndCFIBreakIterator
& it
, const struct Parser
<A
>::CFI_CU_InfoArrays
&);
415 virtual uint32_t appendAtoms(class Parser
<A
>& parser
, uint8_t* buffer
, struct Parser
<A
>::LabelAndCFIBreakIterator
& it
, const struct Parser
<A
>::CFI_CU_InfoArrays
&);
417 typedef typename
A::P::uint_t pint_t
;
418 typedef typename
A::P P
;
420 virtual bool addFollowOnFixups() const { return false; }
421 virtual const char* unlabeledAtomName(Parser
<A
>& parser
, pint_t addr
) = 0;
422 virtual ld::Atom::SymbolTableInclusion
symbolTableInclusion();
423 virtual pint_t
elementSizeAtAddress(pint_t addr
) = 0;
424 virtual ld::Atom::Scope
scopeAtAddress(Parser
<A
>& parser
, pint_t addr
) { return ld::Atom::scopeLinkageUnit
; }
425 virtual bool useElementAt(Parser
<A
>& parser
,
426 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
, pint_t addr
) = 0;
427 virtual ld::Atom::Definition
definition() { return ld::Atom::definitionRegular
; }
428 virtual ld::Atom::Combine
combine(Parser
<A
>& parser
, pint_t addr
) = 0;
429 virtual bool ignoreLabel(const char* label
) const { return (label
[0] == 'L'); }
433 template <typename A
>
434 class FixedSizeSection
: public ImplicitSizeSection
<A
>
437 FixedSizeSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
438 : ImplicitSizeSection
<A
>(parser
, f
, s
) { }
440 typedef typename
A::P::uint_t pint_t
;
441 typedef typename
A::P P
;
442 typedef typename
A::P::E E
;
444 virtual bool useElementAt(Parser
<A
>& parser
,
445 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
, pint_t addr
)
450 template <typename A
>
451 class Literal4Section
: public FixedSizeSection
<A
>
454 Literal4Section(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
455 : FixedSizeSection
<A
>(parser
, f
, s
) {}
457 typedef typename
A::P::uint_t pint_t
;
458 typedef typename
A::P P
;
460 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
) { return ld::Atom::Alignment(2); }
461 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "4-byte-literal"; }
462 virtual pint_t
elementSizeAtAddress(pint_t addr
) { return 4; }
463 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
) { return ld::Atom::combineByNameAndContent
; }
464 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
465 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
466 const ld::IndirectBindingTable
& ind
) const;
467 virtual bool ignoreLabel(const char* label
) const;
470 template <typename A
>
471 class Literal8Section
: public FixedSizeSection
<A
>
474 Literal8Section(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
475 : FixedSizeSection
<A
>(parser
, f
, s
) {}
477 typedef typename
A::P::uint_t pint_t
;
478 typedef typename
A::P P
;
480 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
) { return ld::Atom::Alignment(3); }
481 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "8-byte-literal"; }
482 virtual pint_t
elementSizeAtAddress(pint_t addr
) { return 8; }
483 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
) { return ld::Atom::combineByNameAndContent
; }
484 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
485 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
486 const ld::IndirectBindingTable
& ind
) const;
487 virtual bool ignoreLabel(const char* label
) const;
490 template <typename A
>
491 class Literal16Section
: public FixedSizeSection
<A
>
494 Literal16Section(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
495 : FixedSizeSection
<A
>(parser
, f
, s
) {}
497 typedef typename
A::P::uint_t pint_t
;
498 typedef typename
A::P P
;
500 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
) { return ld::Atom::Alignment(4); }
501 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "16-byte-literal"; }
502 virtual pint_t
elementSizeAtAddress(pint_t addr
) { return 16; }
503 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
) { return ld::Atom::combineByNameAndContent
; }
504 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
505 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
506 const ld::IndirectBindingTable
& ind
) const;
507 virtual bool ignoreLabel(const char* label
) const;
511 template <typename A
>
512 class NonLazyPointerSection
: public FixedSizeSection
<A
>
515 NonLazyPointerSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
516 : FixedSizeSection
<A
>(parser
, f
, s
) {}
518 typedef typename
A::P::uint_t pint_t
;
519 typedef typename
A::P P
;
521 virtual void makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
&);
522 virtual ld::Atom::ContentType
contentType() { return ld::Atom::typeNonLazyPointer
; }
523 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
) { return ld::Atom::Alignment(log2(sizeof(pint_t
))); }
524 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "non_lazy_ptr"; }
525 virtual pint_t
elementSizeAtAddress(pint_t addr
) { return sizeof(pint_t
); }
526 virtual ld::Atom::Scope
scopeAtAddress(Parser
<A
>& parser
, pint_t addr
);
527 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
);
528 virtual bool ignoreLabel(const char* label
) const { return true; }
529 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
530 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
531 const ld::IndirectBindingTable
& ind
) const;
534 static const char* targetName(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
);
535 static ld::Fixup::Kind
fixupKind();
538 template <typename A
>
539 class TLVPointerSection
: public FixedSizeSection
<A
>
542 TLVPointerSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
543 : FixedSizeSection
<A
>(parser
, f
, s
) {}
545 typedef typename
A::P::uint_t pint_t
;
546 typedef typename
A::P P
;
548 virtual void makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
&);
549 virtual ld::Atom::ContentType
contentType() { return ld::Atom::typeTLVPointer
; }
550 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
) { return ld::Atom::Alignment(log2(sizeof(pint_t
))); }
551 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "tlv_lazy_ptr"; }
552 virtual pint_t
elementSizeAtAddress(pint_t addr
) { return sizeof(pint_t
); }
553 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
);
554 virtual bool ignoreLabel(const char* label
) const { return true; }
555 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
556 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
557 const ld::IndirectBindingTable
& ind
) const;
560 static const char* targetName(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
, bool* isStatic
);
564 template <typename A
>
565 class CFStringSection
: public FixedSizeSection
<A
>
568 CFStringSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
569 : FixedSizeSection
<A
>(parser
, f
, s
) {}
571 typedef typename
A::P::uint_t pint_t
;
573 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
) { return ld::Atom::Alignment(log2(sizeof(pint_t
))); }
574 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "CFString"; }
575 virtual pint_t
elementSizeAtAddress(pint_t addr
) { return 4*sizeof(pint_t
); }
576 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
) { return ld::Atom::combineByNameAndReferences
; }
577 virtual bool ignoreLabel(const char* label
) const { return true; }
578 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
579 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
580 const ld::IndirectBindingTable
& ind
) const;
582 enum ContentType
{ contentUTF8
, contentUTF16
, contentUnknown
};
583 static const uint8_t* targetContent(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
,
584 ContentType
* ct
, unsigned int* count
);
588 template <typename A
>
589 class ObjC1ClassSection
: public FixedSizeSection
<A
>
592 ObjC1ClassSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
593 : FixedSizeSection
<A
>(parser
, f
, s
) {}
595 typedef typename
A::P::uint_t pint_t
;
596 typedef typename
A::P P
;
597 typedef typename
A::P::E E
;
599 virtual ld::Atom::Scope
scopeAtAddress(Parser
<A
>& , pint_t
) { return ld::Atom::scopeGlobal
; }
600 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
) { return ld::Atom::Alignment(2); }
601 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
);
602 virtual ld::Atom::SymbolTableInclusion
symbolTableInclusion() { return ld::Atom::symbolTableIn
; }
603 virtual pint_t
elementSizeAtAddress(pint_t addr
);
604 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
) { return ld::Atom::combineNever
; }
605 virtual bool ignoreLabel(const char* label
) const { return true; }
606 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
608 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
609 const ld::IndirectBindingTable
& ind
) const { return false; }
610 virtual bool addRelocFixup(class Parser
<A
>& parser
, const macho_relocation_info
<P
>*);
614 template <typename A
>
615 class ObjC2ClassRefsSection
: public FixedSizeSection
<A
>
618 ObjC2ClassRefsSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
619 : FixedSizeSection
<A
>(parser
, f
, s
) {}
621 typedef typename
A::P::uint_t pint_t
;
623 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
) { return ld::Atom::Alignment(log2(sizeof(pint_t
))); }
624 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "objc-class-ref"; }
625 virtual pint_t
elementSizeAtAddress(pint_t addr
) { return sizeof(pint_t
); }
626 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
) { return ld::Atom::combineByNameAndReferences
; }
627 virtual bool ignoreLabel(const char* label
) const { return true; }
628 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
629 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
630 const ld::IndirectBindingTable
& ind
) const;
632 const char* targetClassName(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
636 template <typename A
>
637 class ObjC2CategoryListSection
: public FixedSizeSection
<A
>
640 ObjC2CategoryListSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
641 : FixedSizeSection
<A
>(parser
, f
, s
) {}
643 typedef typename
A::P::uint_t pint_t
;
645 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
) { return ld::Atom::Alignment(log2(sizeof(pint_t
))); }
646 virtual ld::Atom::Scope
scopeAtAddress(Parser
<A
>& parser
, pint_t addr
) { return ld::Atom::scopeTranslationUnit
; }
647 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "objc-cat-list"; }
648 virtual pint_t
elementSizeAtAddress(pint_t addr
) { return sizeof(pint_t
); }
649 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
) { return ld::Atom::combineNever
; }
650 virtual bool ignoreLabel(const char* label
) const { return true; }
652 const char* targetClassName(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
656 template <typename A
>
657 class PointerToCStringSection
: public FixedSizeSection
<A
>
660 PointerToCStringSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
661 : FixedSizeSection
<A
>(parser
, f
, s
) {}
663 typedef typename
A::P::uint_t pint_t
;
665 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
) { return ld::Atom::Alignment(log2(sizeof(pint_t
))); }
666 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "pointer-to-literal-cstring"; }
667 virtual pint_t
elementSizeAtAddress(pint_t addr
) { return sizeof(pint_t
); }
668 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
) { return ld::Atom::combineByNameAndReferences
; }
669 virtual bool ignoreLabel(const char* label
) const { return true; }
670 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
671 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
672 const ld::IndirectBindingTable
& ind
) const;
673 virtual const char* targetCString(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
677 template <typename A
>
678 class Objc1ClassReferences
: public PointerToCStringSection
<A
>
681 Objc1ClassReferences(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
682 : PointerToCStringSection
<A
>(parser
, f
, s
) {}
684 typedef typename
A::P::uint_t pint_t
;
685 typedef typename
A::P P
;
687 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "pointer-to-literal-objc-class-name"; }
688 virtual bool addRelocFixup(class Parser
<A
>& parser
, const macho_relocation_info
<P
>*);
689 virtual const char* targetCString(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
693 template <typename A
>
694 class CStringSection
: public ImplicitSizeSection
<A
>
697 CStringSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
698 : ImplicitSizeSection
<A
>(parser
, f
, s
) {}
700 typedef typename
A::P::uint_t pint_t
;
701 typedef typename
A::P P
;
703 virtual ld::Atom::ContentType
contentType() { return ld::Atom::typeCString
; }
704 virtual Atom
<A
>* findAtomByAddress(pint_t addr
);
705 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "cstring"; }
706 virtual pint_t
elementSizeAtAddress(pint_t addr
);
707 virtual bool ignoreLabel(const char* label
) const;
708 virtual bool useElementAt(Parser
<A
>& parser
,
709 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
, pint_t addr
);
710 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
) { return ld::Atom::combineByNameAndContent
; }
711 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
712 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
713 const ld::IndirectBindingTable
& ind
) const;
718 template <typename A
>
719 class UTF16StringSection
: public SymboledSection
<A
>
722 UTF16StringSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
723 : SymboledSection
<A
>(parser
, f
, s
) {}
725 typedef typename
A::P::uint_t pint_t
;
726 typedef typename
A::P P
;
728 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
) { return ld::Atom::combineByNameAndContent
; }
729 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
730 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
731 const ld::IndirectBindingTable
& ind
) const;
736 // Atoms in mach-o files
738 template <typename A
>
739 class Atom
: public ld::Atom
742 // overrides of ld::Atom
743 virtual const ld::File
* file() const;
744 virtual const char* translationUnitSource() const
745 { return sect().file().translationUnitSource(); }
746 virtual const char* name() const { return _name
; }
747 virtual uint64_t size() const { return _size
; }
748 virtual uint64_t objectAddress() const { return _objAddress
; }
749 virtual void copyRawContent(uint8_t buffer
[]) const;
750 virtual const uint8_t* rawContentPointer() const { return contentPointer(); }
751 virtual unsigned long contentHash(const ld::IndirectBindingTable
& ind
) const
752 { if ( _hash
== 0 ) _hash
= sect().contentHash(this, ind
); return _hash
; }
753 virtual bool canCoalesceWith(const ld::Atom
& rhs
, const ld::IndirectBindingTable
& ind
) const
754 { return sect().canCoalesceWith(this, rhs
, ind
); }
755 virtual ld::Fixup::iterator
fixupsBegin() const { return &machofile()._fixups
[_fixupsStartIndex
]; }
756 virtual ld::Fixup::iterator
fixupsEnd() const { return &machofile()._fixups
[_fixupsStartIndex
+_fixupsCount
]; }
757 virtual ld::Atom::UnwindInfo::iterator
beginUnwind() const { return &machofile()._unwindInfos
[_unwindInfoStartIndex
]; }
758 virtual ld::Atom::UnwindInfo::iterator
endUnwind() const { return &machofile()._unwindInfos
[_unwindInfoStartIndex
+_unwindInfoCount
]; }
759 virtual ld::Atom::LineInfo::iterator
beginLineInfo() const{ return &machofile()._lineInfos
[_lineInfoStartIndex
]; }
760 virtual ld::Atom::LineInfo::iterator
endLineInfo() const { return &machofile()._lineInfos
[_lineInfoStartIndex
+_lineInfoCount
]; }
761 virtual void setFile(const ld::File
* f
);
765 enum { kFixupStartIndexBits
= 32,
766 kLineInfoStartIndexBits
= 32,
767 kUnwindInfoStartIndexBits
= 24,
768 kFixupCountBits
= 24,
769 kLineInfoCountBits
= 12,
770 kUnwindInfoCountBits
= 4
771 }; // must sum to 128
774 // methods for all atoms from mach-o object file
775 Section
<A
>& sect() const { return (Section
<A
>&)section(); }
776 File
<A
>& machofile() const { return ((Section
<A
>*)(this->_section
))->file(); }
777 void setFixupsRange(uint32_t s
, uint32_t c
);
778 void setUnwindInfoRange(uint32_t s
, uint32_t c
);
779 void extendUnwindInfoRange();
780 void setLineInfoRange(uint32_t s
, uint32_t c
);
781 bool roomForMoreLineInfoCount() { return (_lineInfoCount
< ((1<<kLineInfoCountBits
)-1)); }
782 void incrementLineInfoCount() { assert(roomForMoreLineInfoCount()); ++_lineInfoCount
; }
783 void incrementFixupCount() { if (_fixupsCount
== ((1 << kFixupCountBits
)-1))
784 throwf("too may fixups in %s", name()); ++_fixupsCount
; }
785 const uint8_t* contentPointer() const;
786 uint32_t fixupCount() const { return _fixupsCount
; }
787 void verifyAlignment(const macho_section
<typename
A::P
>&) const;
789 typedef typename
A::P P
;
790 typedef typename
A::P::E E
;
791 typedef typename
A::P::uint_t pint_t
;
792 // constuct via all attributes
793 Atom(Section
<A
>& sct
, const char* nm
, pint_t addr
, uint64_t sz
,
794 ld::Atom::Definition d
, ld::Atom::Combine c
, ld::Atom::Scope s
,
795 ld::Atom::ContentType ct
, ld::Atom::SymbolTableInclusion i
,
796 bool dds
, bool thumb
, bool al
, ld::Atom::Alignment a
)
797 : ld::Atom((ld::Section
&)sct
, d
, c
, s
, ct
, i
, dds
, thumb
, al
, a
),
798 _size(sz
), _objAddress(addr
), _name(nm
), _hash(0),
799 _fixupsStartIndex(0), _lineInfoStartIndex(0),
800 _unwindInfoStartIndex(0), _fixupsCount(0),
801 _lineInfoCount(0), _unwindInfoCount(0) { }
802 // construct via symbol table entry
803 Atom(Section
<A
>& sct
, Parser
<A
>& parser
, const macho_nlist
<P
>& sym
,
804 uint64_t sz
, bool alias
=false)
805 : ld::Atom((ld::Section
&)sct
, parser
.definitionFromSymbol(sym
),
806 parser
.combineFromSymbol(sym
), parser
.scopeFromSymbol(sym
),
807 parser
.resolverFromSymbol(sym
) ? ld::Atom::typeResolver
: sct
.contentType(),
808 parser
.inclusionFromSymbol(sym
),
809 (parser
.dontDeadStripFromSymbol(sym
) && !sct
.dontDeadStripIfReferencesLive()) || sct
.dontDeadStrip(),
810 parser
.isThumbFromSymbol(sym
), alias
,
811 sct
.alignmentForAddress(sym
.n_value())),
812 _size(sz
), _objAddress(sym
.n_value()),
813 _name(parser
.nameFromSymbol(sym
)), _hash(0),
814 _fixupsStartIndex(0), _lineInfoStartIndex(0),
815 _unwindInfoStartIndex(0), _fixupsCount(0),
816 _lineInfoCount(0), _unwindInfoCount(0) {
817 // <rdar://problem/6783167> support auto-hidden weak symbols
818 if ( _scope
== ld::Atom::scopeGlobal
&&
819 (sym
.n_desc() & (N_WEAK_DEF
|N_WEAK_REF
)) == (N_WEAK_DEF
|N_WEAK_REF
) )
821 this->verifyAlignment(*sct
.machoSection());
822 if ( sct
.dontDeadStripIfReferencesLive() )
823 this->setDontDeadStripIfReferencesLive();
827 friend class Parser
<A
>;
828 friend class Section
<A
>;
829 friend class CStringSection
<A
>;
830 friend class AbsoluteSymbolSection
<A
>;
835 mutable unsigned long _hash
;
837 uint64_t _fixupsStartIndex
: kFixupStartIndexBits
,
838 _lineInfoStartIndex
: kLineInfoStartIndexBits
,
839 _unwindInfoStartIndex
: kUnwindInfoStartIndexBits
,
840 _fixupsCount
: kFixupCountBits
,
841 _lineInfoCount
: kLineInfoCountBits
,
842 _unwindInfoCount
: kUnwindInfoCountBits
;
844 static std::map
<const ld::Atom
*, const ld::File
*> _s_fileOverride
;
847 template <typename A
>
848 std::map
<const ld::Atom
*, const ld::File
*> Atom
<A
>::_s_fileOverride
;
850 template <typename A
>
851 void Atom
<A
>::setFile(const ld::File
* f
) {
852 _s_fileOverride
[this] = f
;
855 template <typename A
>
856 const ld::File
* Atom
<A
>::file() const
858 std::map
<const ld::Atom
*, const ld::File
*>::iterator pos
= _s_fileOverride
.find(this);
859 if ( pos
!= _s_fileOverride
.end() )
862 return §().file();
865 template <typename A
>
866 void Atom
<A
>::setFixupsRange(uint32_t startIndex
, uint32_t count
)
868 if ( count
>= (1 << kFixupCountBits
) )
869 throwf("too many fixups in function %s", this->name());
870 if ( startIndex
>= (1 << kFixupStartIndexBits
) )
871 throwf("too many fixups in file");
872 assert(((startIndex
+count
) <= sect().file()._fixups
.size()) && "fixup index out of range");
873 _fixupsStartIndex
= startIndex
;
874 _fixupsCount
= count
;
877 template <typename A
>
878 void Atom
<A
>::setUnwindInfoRange(uint32_t startIndex
, uint32_t count
)
880 if ( count
>= (1 << kUnwindInfoCountBits
) )
881 throwf("too many compact unwind infos in function %s", this->name());
882 if ( startIndex
>= (1 << kUnwindInfoStartIndexBits
) )
883 throwf("too many compact unwind infos (%d) in file", startIndex
);
884 assert((startIndex
+count
) <= sect().file()._unwindInfos
.size() && "unwindinfo index out of range");
885 _unwindInfoStartIndex
= startIndex
;
886 _unwindInfoCount
= count
;
889 template <typename A
>
890 void Atom
<A
>::extendUnwindInfoRange()
892 if ( _unwindInfoCount
+1 >= (1 << kUnwindInfoCountBits
) )
893 throwf("too many compact unwind infos in function %s", this->name());
894 _unwindInfoCount
+= 1;
897 template <typename A
>
898 void Atom
<A
>::setLineInfoRange(uint32_t startIndex
, uint32_t count
)
900 assert((count
< (1 << kLineInfoCountBits
)) && "too many line infos");
901 assert((startIndex
+count
) < sect().file()._lineInfos
.size() && "line info index out of range");
902 _lineInfoStartIndex
= startIndex
;
903 _lineInfoCount
= count
;
906 template <typename A
>
907 const uint8_t* Atom
<A
>::contentPointer() const
909 const macho_section
<P
>* sct
= this->sect().machoSection();
910 if ( this->_objAddress
> sct
->addr() + sct
->size() )
911 throwf("malformed .o file, symbol has address 0x%0llX which is outside range of its section", (uint64_t)this->_objAddress
);
912 uint32_t fileOffset
= sct
->offset() - sct
->addr() + this->_objAddress
;
913 return this->sect().file().fileContent()+fileOffset
;
917 template <typename A
>
918 void Atom
<A
>::copyRawContent(uint8_t buffer
[]) const
921 if ( this->contentType() == ld::Atom::typeZeroFill
) {
922 bzero(buffer
, _size
);
924 else if ( _size
!= 0 ) {
925 memcpy(buffer
, this->contentPointer(), _size
);
930 void Atom
<arm
>::verifyAlignment(const macho_section
<P
>&) const
932 if ( (this->section().type() == ld::Section::typeCode
) && ! isThumb() ) {
933 if ( ((_objAddress
% 4) != 0) || (this->alignment().powerOf2
< 2) )
934 warning("ARM function not 4-byte aligned: %s from %s", this->name(), this->file()->path());
938 #if SUPPORT_ARCH_arm64
940 void Atom
<arm64
>::verifyAlignment(const macho_section
<P
>& sect
) const
942 if ( (this->section().type() == ld::Section::typeCode
) && (sect
.size() != 0) ) {
943 if ( ((_objAddress
% 4) != 0) || (this->alignment().powerOf2
< 2) )
944 warning("arm64 function not 4-byte aligned: %s from %s", this->name(), this->file()->path());
950 template <typename A
>
951 void Atom
<A
>::verifyAlignment(const macho_section
<P
>&) const
956 class AliasAtom
: public ld::Atom
959 AliasAtom(const char* name
, bool hidden
, const ld::File
* file
, const char* aliasOfName
) :
960 ld::Atom(_s_section
, ld::Atom::definitionRegular
, ld::Atom::combineNever
,
961 (hidden
? ld::Atom::scopeLinkageUnit
: ld::Atom::scopeGlobal
),
962 ld::Atom::typeUnclassified
, ld::Atom::symbolTableIn
,
963 false, false, true, 0),
966 _fixup(0, ld::Fixup::k1of1
, ld::Fixup::kindNoneFollowOn
, ld::Fixup::bindingByNameUnbound
, aliasOfName
) { }
968 virtual const ld::File
* file() const { return _file
; }
969 virtual const char* translationUnitSource() const
971 virtual const char* name() const { return _name
; }
972 virtual uint64_t size() const { return 0; }
973 virtual uint64_t objectAddress() const { return 0; }
974 virtual void copyRawContent(uint8_t buffer
[]) const { }
975 virtual ld::Fixup::iterator
fixupsBegin() const { return &((ld::Fixup
*)&_fixup
)[0]; }
976 virtual ld::Fixup::iterator
fixupsEnd() const { return &((ld::Fixup
*)&_fixup
)[1]; }
979 static ld::Section _s_section
;
981 const ld::File
* _file
;
986 ld::Section
AliasAtom::_s_section("__LD", "__aliases", ld::Section::typeTempAlias
, true);
989 template <typename A
>
993 static bool validFile(const uint8_t* fileContent
, bool subtypeMustMatch
=false,
994 cpu_subtype_t subtype
=0);
995 static const char* fileKind(const uint8_t* fileContent
);
996 static ld::Platform
findPlatform(const macho_header
<typename
A::P
>* header
, uint32_t* minOsVers
);
997 static bool hasObjC2Categories(const uint8_t* fileContent
);
998 static bool hasObjC1Categories(const uint8_t* fileContent
);
999 static bool getNonLocalSymbols(const uint8_t* fileContnet
, std::vector
<const char*> &syms
);
1000 static ld::relocatable::File
* parse(const uint8_t* fileContent
, uint64_t fileLength
,
1001 const char* path
, time_t modTime
, ld::File::Ordinal ordinal
,
1002 const ParserOptions
& opts
) {
1003 Parser
p(fileContent
, fileLength
, path
, modTime
,
1004 ordinal
, opts
.warnUnwindConversionProblems
,
1005 opts
.keepDwarfUnwind
, opts
.forceDwarfConversion
,
1006 opts
.neverConvertDwarf
, opts
.verboseOptimizationHints
,
1007 opts
.ignoreMismatchPlatform
);
1008 return p
.parse(opts
);
1011 typedef typename
A::P P
;
1012 typedef typename
A::P::E E
;
1013 typedef typename
A::P::uint_t pint_t
;
1015 struct SourceLocation
{
1017 SourceLocation(Atom
<A
>* a
, uint32_t o
) : atom(a
), offsetInAtom(o
) {}
1019 uint32_t offsetInAtom
;
1024 const char* name
; // only used if targetAtom is NULL
1026 bool weakImport
; // only used if targetAtom is NULL
1027 #if SUPPORT_ARCH_arm64e
1028 ld::Fixup::AuthData authData
; // only used for authenticated pointers
1032 struct FixupInAtom
{
1033 FixupInAtom(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, Atom
<A
>* target
) :
1034 fixup(src
.offsetInAtom
, c
, k
, target
), atom(src
.atom
) { src
.atom
->incrementFixupCount(); }
1036 FixupInAtom(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, ld::Fixup::TargetBinding b
, Atom
<A
>* target
) :
1037 fixup(src
.offsetInAtom
, c
, k
, b
, target
), atom(src
.atom
) { src
.atom
->incrementFixupCount(); }
1039 FixupInAtom(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, bool wi
, const char* name
) :
1040 fixup(src
.offsetInAtom
, c
, k
, wi
, name
), atom(src
.atom
) { src
.atom
->incrementFixupCount(); }
1042 FixupInAtom(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, ld::Fixup::TargetBinding b
, const char* name
) :
1043 fixup(src
.offsetInAtom
, c
, k
, b
, name
), atom(src
.atom
) { src
.atom
->incrementFixupCount(); }
1045 FixupInAtom(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, uint64_t addend
) :
1046 fixup(src
.offsetInAtom
, c
, k
, addend
), atom(src
.atom
) { src
.atom
->incrementFixupCount(); }
1048 #if SUPPORT_ARCH_arm64e
1049 FixupInAtom(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, ld::Fixup::AuthData authData
) :
1050 fixup(src
.offsetInAtom
, c
, k
, authData
), atom(src
.atom
) { src
.atom
->incrementFixupCount(); }
1053 FixupInAtom(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
) :
1054 fixup(src
.offsetInAtom
, c
, k
, (uint64_t)0), atom(src
.atom
) { src
.atom
->incrementFixupCount(); }
1060 void addFixup(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, Atom
<A
>* target
) {
1061 _allFixups
.push_back(FixupInAtom(src
, c
, k
, target
));
1064 void addFixup(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, ld::Fixup::TargetBinding b
, Atom
<A
>* target
) {
1065 _allFixups
.push_back(FixupInAtom(src
, c
, k
, b
, target
));
1068 void addFixup(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, bool wi
, const char* name
) {
1069 _allFixups
.push_back(FixupInAtom(src
, c
, k
, wi
, name
));
1072 void addFixup(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, ld::Fixup::TargetBinding b
, const char* name
) {
1073 _allFixups
.push_back(FixupInAtom(src
, c
, k
, b
, name
));
1076 void addFixup(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, uint64_t addend
) {
1077 _allFixups
.push_back(FixupInAtom(src
, c
, k
, addend
));
1080 #if SUPPORT_ARCH_arm64e
1081 void addFixup(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, ld::Fixup::AuthData authData
) {
1082 _allFixups
.push_back(FixupInAtom(src
, c
, k
, authData
));
1086 void addFixup(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
) {
1087 _allFixups
.push_back(FixupInAtom(src
, c
, k
));
1090 const char* path() { return _path
; }
1091 uint32_t symbolCount() { return _symbolCount
; }
1092 uint32_t indirectSymbol(uint32_t indirectIndex
);
1093 const macho_nlist
<P
>& symbolFromIndex(uint32_t index
);
1094 const char* nameFromSymbol(const macho_nlist
<P
>& sym
);
1095 ld::Atom::Scope
scopeFromSymbol(const macho_nlist
<P
>& sym
);
1096 static ld::Atom::Definition
definitionFromSymbol(const macho_nlist
<P
>& sym
);
1097 static ld::Atom::Combine
combineFromSymbol(const macho_nlist
<P
>& sym
);
1098 ld::Atom::SymbolTableInclusion
inclusionFromSymbol(const macho_nlist
<P
>& sym
);
1099 static bool dontDeadStripFromSymbol(const macho_nlist
<P
>& sym
);
1100 static bool isThumbFromSymbol(const macho_nlist
<P
>& sym
);
1101 static bool weakImportFromSymbol(const macho_nlist
<P
>& sym
);
1102 static bool resolverFromSymbol(const macho_nlist
<P
>& sym
);
1103 static bool altEntryFromSymbol(const macho_nlist
<P
>& sym
);
1104 uint32_t symbolIndexFromIndirectSectionAddress(pint_t
,const macho_section
<P
>*);
1105 const macho_section
<P
>* firstMachOSection() { return _sectionsStart
; }
1106 const macho_section
<P
>* machOSectionFromSectionIndex(uint32_t index
);
1107 uint32_t machOSectionCount() { return _machOSectionsCount
; }
1108 uint32_t undefinedStartIndex() { return _undefinedStartIndex
; }
1109 uint32_t undefinedEndIndex() { return _undefinedEndIndex
; }
1110 void addFixup(FixupInAtom f
) { _allFixups
.push_back(f
); }
1111 Section
<A
>* sectionForNum(unsigned int sectNum
);
1112 Section
<A
>* sectionForAddress(pint_t addr
);
1113 Atom
<A
>* findAtomByAddress(pint_t addr
);
1114 Atom
<A
>* findAtomByAddressOrNullIfStub(pint_t addr
);
1115 Atom
<A
>* findAtomByAddressOrLocalTargetOfStub(pint_t addr
, uint32_t* offsetInAtom
);
1116 Atom
<A
>* findAtomByName(const char* name
); // slow!
1117 void findTargetFromAddress(pint_t addr
, TargetDesc
& target
);
1118 void findTargetFromAddress(pint_t baseAddr
, pint_t addr
, TargetDesc
& target
);
1119 void findTargetFromAddressAndSectionNum(pint_t addr
, unsigned int sectNum
,
1120 TargetDesc
& target
);
1121 uint32_t tentativeDefinitionCount() { return _tentativeDefinitionCount
; }
1122 uint32_t absoluteSymbolCount() { return _absoluteSymbolCount
; }
1124 uint32_t fileLength() const { return _fileLength
; }
1125 bool hasStubsSection() { return (_stubsSectionNum
!= 0); }
1126 unsigned int stubsSectionNum() { return _stubsSectionNum
; }
1127 void addDtraceExtraInfos(const SourceLocation
& src
, const char* provider
);
1128 const char* scanSymbolTableForAddress(uint64_t addr
);
1129 bool warnUnwindConversionProblems() { return _warnUnwindConversionProblems
; }
1130 bool hasDataInCodeLabels() { return _hasDataInCodeLabels
; }
1131 bool keepDwarfUnwind() { return _keepDwarfUnwind
; }
1132 bool forceDwarfConversion() { return _forceDwarfConversion
; }
1133 bool verboseOptimizationHints() { return _verboseOptimizationHints
; }
1134 bool neverConvertDwarf() { return _neverConvertDwarf
; }
1135 bool armUsesZeroCostExceptions() { return _armUsesZeroCostExceptions
; }
1136 uint8_t maxDefaultCommonAlignment() { return _maxDefaultCommonAlignment
; }
1139 macho_data_in_code_entry
<P
>* dataInCodeStart() { return _dataInCodeStart
; }
1140 macho_data_in_code_entry
<P
>* dataInCodeEnd() { return _dataInCodeEnd
; }
1141 const uint8_t* optimizationHintsStart() { return _lohStart
; }
1142 const uint8_t* optimizationHintsEnd() { return _lohEnd
; }
1143 bool hasOptimizationHints() { return _lohStart
!= _lohEnd
; }
1146 void addFixups(const SourceLocation
& src
, ld::Fixup::Kind kind
, const TargetDesc
& target
);
1147 void addFixups(const SourceLocation
& src
, ld::Fixup::Kind kind
, const TargetDesc
& target
, const TargetDesc
& picBase
);
1151 struct LabelAndCFIBreakIterator
{
1152 typedef typename CFISection
<A
>::CFI_Atom_Info CFI_Atom_Info
;
1153 LabelAndCFIBreakIterator(const uint32_t* ssa
, uint32_t ssc
, const pint_t
* cfisa
,
1154 uint32_t cfisc
, bool ols
)
1155 : sortedSymbolIndexes(ssa
), sortedSymbolCount(ssc
), cfiStartsArray(cfisa
),
1156 cfiStartsCount(cfisc
), fileHasOverlappingSymbols(ols
),
1157 newSection(false), cfiIndex(0), symIndex(0) {}
1158 bool next(Parser
<A
>& parser
, const Section
<A
>& sect
, uint32_t sectNum
, pint_t startAddr
, pint_t endAddr
,
1159 pint_t
* addr
, pint_t
* size
, const macho_nlist
<P
>** sym
);
1160 pint_t
peek(Parser
<A
>& parser
, pint_t startAddr
, pint_t endAddr
);
1161 void beginSection() { newSection
= true; symIndex
= 0; }
1163 const uint32_t* const sortedSymbolIndexes
;
1164 const uint32_t sortedSymbolCount
;
1165 const pint_t
* cfiStartsArray
;
1166 const uint32_t cfiStartsCount
;
1167 const bool fileHasOverlappingSymbols
;
1173 struct CFI_CU_InfoArrays
{
1174 typedef typename CFISection
<A
>::CFI_Atom_Info CFI_Atom_Info
;
1175 typedef typename CUSection
<A
>::Info CU_Info
;
1176 CFI_CU_InfoArrays(const CFI_Atom_Info
* cfiAr
, uint32_t cfiC
, CU_Info
* cuAr
, uint32_t cuC
)
1177 : cfiArray(cfiAr
), cuArray(cuAr
), cfiCount(cfiC
), cuCount(cuC
) {}
1178 const CFI_Atom_Info
* const cfiArray
;
1179 CU_Info
* const cuArray
;
1180 const uint32_t cfiCount
;
1181 const uint32_t cuCount
;
1187 friend class Section
<A
>;
1189 enum SectionType
{ sectionTypeIgnore
, sectionTypeLiteral4
, sectionTypeLiteral8
, sectionTypeLiteral16
,
1190 sectionTypeNonLazy
, sectionTypeCFI
, sectionTypeCString
, sectionTypeCStringPointer
,
1191 sectionTypeUTF16Strings
, sectionTypeCFString
, sectionTypeObjC2ClassRefs
, typeObjC2CategoryList
,
1192 sectionTypeObjC1Classes
, sectionTypeSymboled
, sectionTypeObjC1ClassRefs
,
1193 sectionTypeTentativeDefinitions
, sectionTypeAbsoluteSymbols
, sectionTypeTLVDefs
,
1194 sectionTypeCompactUnwind
, sectionTypeTLVPointers
};
1196 template <typename P
>
1197 struct MachOSectionAndSectionClass
1199 const macho_section
<P
>* sect
;
1202 static int sorter(const void* l
, const void* r
) {
1203 const MachOSectionAndSectionClass
<P
>* left
= (MachOSectionAndSectionClass
<P
>*)l
;
1204 const MachOSectionAndSectionClass
<P
>* right
= (MachOSectionAndSectionClass
<P
>*)r
;
1205 int64_t diff
= left
->sect
->addr() - right
->sect
->addr();
1215 struct ParserAndSectionsArray
{ Parser
* parser
; const uint32_t* sortedSectionsArray
; };
1218 Parser(const uint8_t* fileContent
, uint64_t fileLength
,
1219 const char* path
, time_t modTime
, ld::File::Ordinal ordinal
,
1220 bool warnUnwindConversionProblems
, bool keepDwarfUnwind
,
1221 bool forceDwarfConversion
, bool neverConvertDwarf
,
1222 bool verboseOptimizationHints
, bool ignoreMismatchPlatform
);
1223 ld::relocatable::File
* parse(const ParserOptions
& opts
);
1224 static uint8_t loadCommandSizeMask();
1225 static bool useSimulatorVariant();
1226 bool parseLoadCommands(ld::VersionSet platforms
, bool simulator
, bool ignoreMismatchPlatform
);
1227 void makeSections();
1228 void prescanSymbolTable();
1229 void makeSortedSymbolsArray(uint32_t symArray
[], const uint32_t sectionArray
[]);
1230 void makeSortedSectionsArray(uint32_t array
[]);
1231 static int pointerSorter(const void* l
, const void* r
);
1232 static int symbolIndexSorter(void* extra
, const void* l
, const void* r
);
1233 static int sectionIndexSorter(void* extra
, const void* l
, const void* r
);
1235 void parseDebugInfo();
1238 void appendAliasAtoms(uint8_t* atomBuffer
);
1239 static bool isConstFunStabs(const char *stabStr
);
1240 bool read_comp_unit(const char ** name
, const char ** comp_dir
,
1241 uint64_t *stmt_list
);
1242 pint_t
realAddr(pint_t addr
);
1243 const char* getDwarfString(uint64_t form
, const uint8_t*& p
);
1244 uint64_t getDwarfOffset(uint64_t form
, const uint8_t*& di
, bool dwarf64
);
1245 bool skip_form(const uint8_t ** offset
, const uint8_t * end
,
1246 uint64_t form
, uint8_t addr_size
, bool dwarf64
);
1249 // filled in by constructor
1250 const uint8_t* _fileContent
;
1251 uint32_t _fileLength
;
1254 ld::File::Ordinal _ordinal
;
1256 // filled in by parseLoadCommands()
1258 const macho_nlist
<P
>* _symbols
;
1259 uint32_t _symbolCount
;
1260 uint32_t _indirectSymbolCount
;
1261 const char* _strings
;
1262 uint32_t _stringsSize
;
1263 const uint32_t* _indirectTable
;
1264 uint32_t _indirectTableCount
;
1265 uint32_t _undefinedStartIndex
;
1266 uint32_t _undefinedEndIndex
;
1267 const macho_section
<P
>* _sectionsStart
;
1268 uint32_t _machOSectionsCount
;
1270 macho_data_in_code_entry
<P
>* _dataInCodeStart
;
1271 macho_data_in_code_entry
<P
>* _dataInCodeEnd
;
1272 const uint8_t* _lohStart
;
1273 const uint8_t* _lohEnd
;
1275 // filled in by parse()
1276 CFISection
<A
>* _EHFrameSection
;
1277 CUSection
<A
>* _compactUnwindSection
;
1278 AbsoluteSymbolSection
<A
>* _absoluteSection
;
1279 uint32_t _tentativeDefinitionCount
;
1280 uint32_t _absoluteSymbolCount
;
1281 uint32_t _symbolsInSections
;
1282 bool _hasLongBranchStubs
;
1283 bool _AppleObjc
; // FSF has objc that uses different data layout
1284 bool _overlappingSymbols
;
1285 bool _warnUnwindConversionProblems
;
1286 bool _hasDataInCodeLabels
;
1287 bool _keepDwarfUnwind
;
1288 bool _forceDwarfConversion
;
1289 bool _neverConvertDwarf
;
1290 bool _verboseOptimizationHints
;
1291 bool _armUsesZeroCostExceptions
;
1292 bool _ignoreMismatchPlatform
;
1293 bool _treateBitcodeAsData
;
1295 uint8_t _maxDefaultCommonAlignment
;
1296 unsigned int _stubsSectionNum
;
1297 const macho_section
<P
>* _stubsMachOSection
;
1298 std::vector
<const char*> _dtraceProviderInfo
;
1299 std::vector
<FixupInAtom
> _allFixups
;
1300 #if SUPPORT_ARCH_arm64e
1301 bool _supportsAuthenticatedPointers
;
1307 template <typename A
>
1308 Parser
<A
>::Parser(const uint8_t* fileContent
, uint64_t fileLength
, const char* path
, time_t modTime
,
1309 ld::File::Ordinal ordinal
, bool convertDUI
, bool keepDwarfUnwind
, bool forceDwarfConversion
,
1310 bool neverConvertDwarf
, bool verboseOptimizationHints
, bool ignoreMismatchPlatform
)
1311 : _fileContent(fileContent
), _fileLength(fileLength
), _path(path
), _modTime(modTime
),
1312 _ordinal(ordinal
), _file(NULL
),
1313 _symbols(NULL
), _symbolCount(0), _indirectSymbolCount(0), _strings(NULL
), _stringsSize(0),
1314 _indirectTable(NULL
), _indirectTableCount(0),
1315 _undefinedStartIndex(0), _undefinedEndIndex(0),
1316 _sectionsStart(NULL
), _machOSectionsCount(0), _hasUUID(false),
1317 _dataInCodeStart(NULL
), _dataInCodeEnd(NULL
),
1318 _lohStart(NULL
), _lohEnd(NULL
),
1319 _EHFrameSection(NULL
), _compactUnwindSection(NULL
), _absoluteSection(NULL
),
1320 _tentativeDefinitionCount(0), _absoluteSymbolCount(0),
1321 _symbolsInSections(0), _hasLongBranchStubs(false), _AppleObjc(false),
1322 _overlappingSymbols(false), _warnUnwindConversionProblems(convertDUI
), _hasDataInCodeLabels(false),
1323 _keepDwarfUnwind(keepDwarfUnwind
), _forceDwarfConversion(forceDwarfConversion
),
1324 _neverConvertDwarf(neverConvertDwarf
),
1325 _verboseOptimizationHints(verboseOptimizationHints
),
1326 _ignoreMismatchPlatform(ignoreMismatchPlatform
),
1327 _stubsSectionNum(0), _stubsMachOSection(NULL
)
1333 bool Parser
<x86
>::validFile(const uint8_t* fileContent
, bool, cpu_subtype_t
)
1335 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1336 if ( header
->magic() != MH_MAGIC
)
1338 if ( header
->cputype() != CPU_TYPE_I386
)
1340 if ( header
->filetype() != MH_OBJECT
)
1346 bool Parser
<x86_64
>::validFile(const uint8_t* fileContent
, bool, cpu_subtype_t
)
1348 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1349 if ( header
->magic() != MH_MAGIC_64
)
1351 if ( header
->cputype() != CPU_TYPE_X86_64
)
1353 if ( header
->filetype() != MH_OBJECT
)
1359 bool Parser
<arm
>::validFile(const uint8_t* fileContent
, bool subtypeMustMatch
, cpu_subtype_t subtype
)
1361 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1362 if ( header
->magic() != MH_MAGIC
)
1364 if ( header
->cputype() != CPU_TYPE_ARM
)
1366 if ( header
->filetype() != MH_OBJECT
)
1368 if ( subtypeMustMatch
) {
1369 if ( (cpu_subtype_t
)header
->cpusubtype() == subtype
)
1371 // hack until libcc_kext.a is made fat
1372 if ( header
->cpusubtype() == CPU_SUBTYPE_ARM_ALL
)
1381 bool Parser
<arm64
>::validFile(const uint8_t* fileContent
, bool subtypeMustMatch
, cpu_subtype_t subtype
)
1383 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1384 if ( header
->magic() != MH_MAGIC_64
)
1386 if ( header
->cputype() != CPU_TYPE_ARM64
)
1388 if ( header
->filetype() != MH_OBJECT
)
1390 if ( subtypeMustMatch
&& (header
->cpusubtype() != (uint32_t)subtype
) )
1397 const char* Parser
<x86
>::fileKind(const uint8_t* fileContent
)
1399 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1400 if ( header
->magic() != MH_MAGIC
)
1402 if ( header
->cputype() != CPU_TYPE_I386
)
1408 const char* Parser
<x86_64
>::fileKind(const uint8_t* fileContent
)
1410 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1411 if ( header
->magic() != MH_MAGIC_64
)
1413 if ( header
->cputype() != CPU_TYPE_X86_64
)
1419 const char* Parser
<arm
>::fileKind(const uint8_t* fileContent
)
1421 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1422 if ( header
->magic() != MH_MAGIC
)
1424 if ( header
->cputype() != CPU_TYPE_ARM
)
1426 for (const ArchInfo
* t
=archInfoArray
; t
->archName
!= NULL
; ++t
) {
1427 if ( (t
->cpuType
== CPU_TYPE_ARM
) && ((cpu_subtype_t
)header
->cpusubtype() == t
->cpuSubType
) ) {
1434 #if SUPPORT_ARCH_arm64
1436 const char* Parser
<arm64
>::fileKind(const uint8_t* fileContent
)
1438 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1439 if ( header
->magic() != MH_MAGIC_64
)
1441 if ( header
->cputype() != CPU_TYPE_ARM64
)
1448 template <typename A
>
1449 bool Parser
<A
>::hasObjC2Categories(const uint8_t* fileContent
)
1451 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1452 const uint32_t cmd_count
= header
->ncmds();
1453 const macho_load_command
<P
>* const cmds
= (macho_load_command
<P
>*)((char*)header
+ sizeof(macho_header
<P
>));
1454 const macho_load_command
<P
>* const cmdsEnd
= (macho_load_command
<P
>*)((char*)header
+ sizeof(macho_header
<P
>) + header
->sizeofcmds());
1455 const macho_load_command
<P
>* cmd
= cmds
;
1456 for (uint32_t i
= 0; i
< cmd_count
; ++i
) {
1457 if ( cmd
->cmd() == macho_segment_command
<P
>::CMD
) {
1458 const macho_segment_command
<P
>* segment
= (macho_segment_command
<P
>*)cmd
;
1459 const macho_section
<P
>* sectionsStart
= (macho_section
<P
>*)((char*)segment
+ sizeof(macho_segment_command
<P
>));
1460 for (uint32_t si
=0; si
< segment
->nsects(); ++si
) {
1461 const macho_section
<P
>* sect
= §ionsStart
[si
];
1462 if ( (sect
->size() > 0)
1463 && (strcmp(sect
->sectname(), "__objc_catlist") == 0)
1464 && (strcmp(sect
->segname(), "__DATA") == 0) ) {
1469 cmd
= (const macho_load_command
<P
>*)(((char*)cmd
)+cmd
->cmdsize());
1470 if ( cmd
> cmdsEnd
)
1471 throwf("malformed mach-o file, load command #%d is outside size of load commands", i
);
1477 template <typename A
>
1478 bool Parser
<A
>::hasObjC1Categories(const uint8_t* fileContent
)
1480 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1481 const uint32_t cmd_count
= header
->ncmds();
1482 const macho_load_command
<P
>* const cmds
= (macho_load_command
<P
>*)((char*)header
+ sizeof(macho_header
<P
>));
1483 const macho_load_command
<P
>* const cmdsEnd
= (macho_load_command
<P
>*)((char*)header
+ sizeof(macho_header
<P
>) + header
->sizeofcmds());
1484 const macho_load_command
<P
>* cmd
= cmds
;
1485 for (uint32_t i
= 0; i
< cmd_count
; ++i
) {
1486 if ( cmd
->cmd() == macho_segment_command
<P
>::CMD
) {
1487 const macho_segment_command
<P
>* segment
= (macho_segment_command
<P
>*)cmd
;
1488 const macho_section
<P
>* sectionsStart
= (macho_section
<P
>*)((char*)segment
+ sizeof(macho_segment_command
<P
>));
1489 for (uint32_t si
=0; si
< segment
->nsects(); ++si
) {
1490 const macho_section
<P
>* sect
= §ionsStart
[si
];
1491 if ( (sect
->size() > 0)
1492 && (strcmp(sect
->sectname(), "__category") == 0)
1493 && (strcmp(sect
->segname(), "__OBJC") == 0) ) {
1498 cmd
= (const macho_load_command
<P
>*)(((char*)cmd
)+cmd
->cmdsize());
1499 if ( cmd
> cmdsEnd
)
1500 throwf("malformed mach-o file, load command #%d is outside size of load commands", i
);
1506 template <typename A
>
1507 bool Parser
<A
>::getNonLocalSymbols(const uint8_t* fileContent
, std::vector
<const char*> &syms
)
1509 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1510 const uint32_t cmd_count
= header
->ncmds();
1511 const macho_load_command
<P
>* const cmds
= (macho_load_command
<P
>*)((char*)header
+ sizeof(macho_header
<P
>));
1512 const macho_load_command
<P
>* const cmdsEnd
= (macho_load_command
<P
>*)((char*)header
+ sizeof(macho_header
<P
>) + header
->sizeofcmds());
1513 const macho_load_command
<P
>* cmd
= cmds
;
1514 for (uint32_t i
= 0; i
< cmd_count
; ++i
) {
1515 if ( cmd
->cmd() == LC_SYMTAB
) {
1516 const macho_symtab_command
<P
>* symtab
= (macho_symtab_command
<P
>*)cmd
;
1517 uint32_t symbolCount
= symtab
->nsyms();
1518 const macho_nlist
<P
>* symbols
= (const macho_nlist
<P
>*)(fileContent
+ symtab
->symoff());
1519 const char* strings
= (char*)fileContent
+ symtab
->stroff();
1520 for (uint32_t j
= 0; j
< symbolCount
; ++j
) {
1521 // ignore stabs and count only ext symbols
1522 if ( (symbols
[j
].n_type() & N_STAB
) == 0 &&
1523 (symbols
[j
].n_type() & N_EXT
) != 0 ) {
1524 const char* symName
= &strings
[symbols
[j
].n_strx()];
1525 syms
.push_back(symName
);
1530 cmd
= (const macho_load_command
<P
>*)(((char*)cmd
)+cmd
->cmdsize());
1531 if ( cmd
> cmdsEnd
)
1532 throwf("malformed mach-o file, load command #%d is outside size of load commands", i
);
1538 template <typename A
>
1539 int Parser
<A
>::pointerSorter(const void* l
, const void* r
)
1541 // sort references by address
1542 const pint_t
* left
= (pint_t
*)l
;
1543 const pint_t
* right
= (pint_t
*)r
;
1544 return (*left
- *right
);
1547 template <typename A
>
1548 typename
A::P::uint_t Parser
<A
>::LabelAndCFIBreakIterator::peek(Parser
<A
>& parser
, pint_t startAddr
, pint_t endAddr
)
1551 if ( symIndex
< sortedSymbolCount
)
1552 symbolAddr
= parser
.symbolFromIndex(sortedSymbolIndexes
[symIndex
]).n_value();
1554 symbolAddr
= endAddr
;
1556 if ( cfiIndex
< cfiStartsCount
)
1557 cfiAddr
= cfiStartsArray
[cfiIndex
];
1560 if ( (cfiAddr
< symbolAddr
) && (cfiAddr
>= startAddr
) ) {
1561 if ( cfiAddr
< endAddr
)
1567 if ( symbolAddr
< endAddr
)
1575 // Parses up a section into chunks based on labels and CFI information.
1576 // Each call returns the next chunk address and size, and (if the break
1577 // was becuase of a label, the symbol). Returns false when no more chunks.
1579 template <typename A
>
1580 bool Parser
<A
>::LabelAndCFIBreakIterator::next(Parser
<A
>& parser
, const Section
<A
>& sect
, uint32_t sectNum
, pint_t startAddr
, pint_t endAddr
,
1581 pint_t
* addr
, pint_t
* size
, const macho_nlist
<P
>** symbol
)
1583 bool cfiApplicable
= (sect
.machoSection()->flags() & (S_ATTR_PURE_INSTRUCTIONS
| S_ATTR_SOME_INSTRUCTIONS
));
1584 // may not be a label on start of section, but need atom demarcation there
1587 // advance symIndex until we get to the first label at or past the start of this section
1588 while ( symIndex
< sortedSymbolCount
) {
1589 const macho_nlist
<P
>* sym
= &parser
.symbolFromIndex(sortedSymbolIndexes
[symIndex
]);
1590 // if compile threw in "ltmp*" symbol at start of section and there is another real label at same location, ignore ltmp one
1591 if ( symIndex
+1 < sortedSymbolCount
) {
1592 const macho_nlist
<P
>* sym2
= &parser
.symbolFromIndex(sortedSymbolIndexes
[symIndex
+1]);
1593 if ( (sym
->n_sect() == sym2
->n_sect()) && (sym
->n_value() == sym2
->n_value()) ) {
1594 if ( strncmp(parser
.nameFromSymbol(*sym
), "ltmp", 4) == 0 ) {
1600 if ( ! sect
.ignoreLabel(parser
.nameFromSymbol(*sym
)) ) {
1601 pint_t nextSymbolAddr
= sym
->n_value();
1602 //fprintf(stderr, "sectNum=%d, nextSymbolAddr=0x%08llX, name=%s\n", sectNum, (uint64_t)nextSymbolAddr, parser.nameFromSymbol(sym));
1603 if ( (nextSymbolAddr
> startAddr
) || ((nextSymbolAddr
== startAddr
) && (sym
->n_sect() == sectNum
)) )
1608 if ( symIndex
< sortedSymbolCount
) {
1609 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(sortedSymbolIndexes
[symIndex
]);
1610 pint_t nextSymbolAddr
= sym
.n_value();
1611 // if next symbol found is not in this section
1612 if ( sym
.n_sect() != sectNum
) {
1613 // check for CFI break instead of symbol break
1614 if ( cfiIndex
< cfiStartsCount
) {
1615 pint_t nextCfiAddr
= cfiStartsArray
[cfiIndex
];
1616 if ( nextCfiAddr
< endAddr
) {
1619 *addr
= nextCfiAddr
;
1620 *size
= peek(parser
, startAddr
, endAddr
) - nextCfiAddr
;
1626 *size
= endAddr
- startAddr
;
1628 if ( startAddr
== endAddr
)
1629 return false; // zero size section
1631 return true; // whole section is one atom with no label
1633 // if also CFI break here, eat it
1634 if ( cfiIndex
< cfiStartsCount
) {
1635 if ( cfiStartsArray
[cfiIndex
] == nextSymbolAddr
)
1638 if ( nextSymbolAddr
== startAddr
) {
1639 // label at start of section, return it as chunk
1642 *size
= peek(parser
, startAddr
, endAddr
) - startAddr
;
1646 // return chunk before first symbol
1648 *size
= nextSymbolAddr
- startAddr
;
1652 // no symbols in section, check CFI
1653 if ( cfiApplicable
&& (cfiIndex
< cfiStartsCount
) ) {
1654 pint_t nextCfiAddr
= cfiStartsArray
[cfiIndex
];
1655 if ( nextCfiAddr
< endAddr
) {
1658 *addr
= nextCfiAddr
;
1659 *size
= peek(parser
, startAddr
, endAddr
) - nextCfiAddr
;
1664 // no cfi, so whole section is one chunk
1666 *size
= endAddr
- startAddr
;
1668 if ( startAddr
== endAddr
)
1669 return false; // zero size section
1671 return true; // whole section is one atom with no label
1674 while ( (symIndex
< sortedSymbolCount
) && (cfiIndex
< cfiStartsCount
) ) {
1675 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(sortedSymbolIndexes
[symIndex
]);
1676 pint_t nextSymbolAddr
= sym
.n_value();
1677 pint_t nextCfiAddr
= cfiStartsArray
[cfiIndex
];
1678 if ( nextSymbolAddr
< nextCfiAddr
) {
1679 if ( nextSymbolAddr
>= endAddr
)
1682 if ( nextSymbolAddr
< startAddr
)
1684 *addr
= nextSymbolAddr
;
1685 *size
= peek(parser
, startAddr
, endAddr
) - nextSymbolAddr
;
1689 else if ( nextCfiAddr
< nextSymbolAddr
) {
1690 if ( nextCfiAddr
>= endAddr
)
1693 if ( nextCfiAddr
< startAddr
)
1695 *addr
= nextCfiAddr
;
1696 *size
= peek(parser
, startAddr
, endAddr
) - nextCfiAddr
;
1701 if ( nextCfiAddr
>= endAddr
)
1705 if ( nextCfiAddr
< startAddr
)
1707 *addr
= nextCfiAddr
;
1708 *size
= peek(parser
, startAddr
, endAddr
) - nextCfiAddr
;
1713 while ( symIndex
< sortedSymbolCount
) {
1714 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(sortedSymbolIndexes
[symIndex
]);
1715 pint_t nextSymbolAddr
= sym
.n_value();
1716 // if next symbol found is not in this section, then done with iteration
1717 if ( sym
.n_sect() != sectNum
)
1720 if ( nextSymbolAddr
< startAddr
)
1722 *addr
= nextSymbolAddr
;
1723 *size
= peek(parser
, startAddr
, endAddr
) - nextSymbolAddr
;
1727 while ( cfiIndex
< cfiStartsCount
) {
1728 pint_t nextCfiAddr
= cfiStartsArray
[cfiIndex
];
1729 if ( nextCfiAddr
>= endAddr
)
1732 if ( nextCfiAddr
< startAddr
)
1734 *addr
= nextCfiAddr
;
1735 *size
= peek(parser
, startAddr
, endAddr
) - nextCfiAddr
;
1743 typename
arm::P::uint_t Parser
<arm
>::realAddr(typename
arm::P::uint_t addr
)
1748 template <typename A
>
1749 typename
A::P::uint_t Parser
<A
>::realAddr(typename
A::P::uint_t addr
)
1754 #define STACK_ALLOC_IF_SMALL(_type, _name, _actual_count, _maxCount) \
1755 _type* _name = NULL; \
1756 uint32_t _name##_count = 1; \
1757 uint32_t _name##_stack_count = _actual_count; \
1758 if ( _actual_count > _maxCount ) { \
1759 _name = (_type*)malloc(sizeof(_type) * _actual_count); \
1760 _name##_stack_count = 1; \
1763 _name##_count = _actual_count; \
1764 _type _name##_buffer[_name##_stack_count]; \
1765 if ( _name == NULL ) \
1766 _name = _name##_buffer;
1769 template <typename A
>
1770 ld::relocatable::File
* Parser
<A
>::parse(const ParserOptions
& opts
)
1772 // create file object
1773 _file
= new File
<A
>(_path
, _modTime
, _fileContent
, _ordinal
);
1776 _file
->_srcKind
= opts
.srcKind
;
1777 // set treatBitcodeAsData
1778 _treateBitcodeAsData
= opts
.treateBitcodeAsData
;
1779 _usingBitcode
= opts
.usingBitcode
;
1781 #if SUPPORT_ARCH_arm64e
1782 _supportsAuthenticatedPointers
= opts
.supportsAuthenticatedPointers
;
1785 // respond to -t option
1786 if ( opts
.logAllFiles
)
1787 printf("%s\n", _path
);
1789 _armUsesZeroCostExceptions
= opts
.armUsesZeroCostExceptions
;
1790 _maxDefaultCommonAlignment
= opts
.maxDefaultCommonAlignment
;
1792 // parse start of mach-o file
1793 if ( ! parseLoadCommands(opts
.platforms
, opts
.simulator
, opts
.ignoreMismatchPlatform
) )
1797 uint32_t sortedSectionIndexes
[_machOSectionsCount
];
1798 this->makeSortedSectionsArray(sortedSectionIndexes
);
1800 // make symbol table sorted by address
1801 this->prescanSymbolTable();
1802 uint32_t sortedSymbolIndexes
[_symbolsInSections
];
1803 this->makeSortedSymbolsArray(sortedSymbolIndexes
, sortedSectionIndexes
);
1805 // allocate Section<A> object for each mach-o section
1808 // if it exists, do special early parsing of __compact_unwind section
1809 uint32_t countOfCUs
= 0;
1810 if ( _compactUnwindSection
!= NULL
)
1811 countOfCUs
= _compactUnwindSection
->count();
1812 // stack allocate (if not too large) cuInfoBuffer
1813 STACK_ALLOC_IF_SMALL(typename CUSection
<A
>::Info
, cuInfoArray
, countOfCUs
, 1024);
1814 if ( countOfCUs
!= 0 )
1815 _compactUnwindSection
->parse(*this, countOfCUs
, cuInfoArray
);
1817 // create lists of address that already have compact unwind and thus don't need the dwarf parsed
1818 unsigned cuLsdaCount
= 0;
1819 STACK_ALLOC_IF_SMALL(pint_t
, cuStarts
, countOfCUs
, 1024);
1820 for (uint32_t i
=0; i
< countOfCUs
; ++i
) {
1821 if ( CUSection
<A
>::encodingMeansUseDwarf(cuInfoArray
[i
].compactUnwindInfo
) )
1824 cuStarts
[i
] = cuInfoArray
[i
].functionStartAddress
;
1825 if ( cuInfoArray
[i
].lsdaAddress
!= 0 )
1830 // if it exists, do special early parsing of __eh_frame section
1831 // stack allocate (if not too large) array of CFI_Atom_Info
1832 uint32_t countOfCFIs
= 0;
1833 if ( _EHFrameSection
!= NULL
)
1834 countOfCFIs
= _EHFrameSection
->cfiCount(*this);
1835 STACK_ALLOC_IF_SMALL(typename CFISection
<A
>::CFI_Atom_Info
, cfiArray
, countOfCFIs
, 1024);
1837 // stack allocate (if not too large) a copy of __eh_frame to apply relocations to
1838 uint32_t sectSize
= 4;
1839 if ( (countOfCFIs
!= 0) && _EHFrameSection
->needsRelocating() )
1840 sectSize
= _EHFrameSection
->machoSection()->size()+4;
1841 STACK_ALLOC_IF_SMALL(uint8_t, ehBuffer
, sectSize
, 50*1024);
1842 uint32_t cfiStartsCount
= 0;
1843 if ( countOfCFIs
!= 0 ) {
1844 _EHFrameSection
->cfiParse(*this, ehBuffer
, cfiArray
, countOfCFIs
, cuStarts
, countOfCUs
);
1845 // count functions and lsdas
1846 for(uint32_t i
=0; i
< countOfCFIs
; ++i
) {
1847 if ( cfiArray
[i
].isCIE
)
1849 //fprintf(stderr, "cfiArray[i].func = 0x%08llX, cfiArray[i].lsda = 0x%08llX, encoding=0x%08X\n",
1850 // (uint64_t)cfiArray[i].u.fdeInfo.function.targetAddress,
1851 // (uint64_t)cfiArray[i].u.fdeInfo.lsda.targetAddress,
1852 // cfiArray[i].u.fdeInfo.compactUnwindInfo);
1853 if ( cfiArray
[i
].u
.fdeInfo
.function
.targetAddress
!= CFI_INVALID_ADDRESS
)
1855 if ( cfiArray
[i
].u
.fdeInfo
.lsda
.targetAddress
!= CFI_INVALID_ADDRESS
)
1859 CFI_CU_InfoArrays
cfis(cfiArray
, countOfCFIs
, cuInfoArray
, countOfCUs
);
1861 // create sorted array of function starts and lsda starts
1862 pint_t cfiStartsArray
[cfiStartsCount
+cuLsdaCount
];
1863 uint32_t countOfFDEs
= 0;
1864 uint32_t cfiStartsArrayCount
= 0;
1865 if ( countOfCFIs
!= 0 ) {
1866 for(uint32_t i
=0; i
< countOfCFIs
; ++i
) {
1867 if ( cfiArray
[i
].isCIE
)
1869 if ( cfiArray
[i
].u
.fdeInfo
.function
.targetAddress
!= CFI_INVALID_ADDRESS
)
1870 cfiStartsArray
[cfiStartsArrayCount
++] = realAddr(cfiArray
[i
].u
.fdeInfo
.function
.targetAddress
);
1871 if ( cfiArray
[i
].u
.fdeInfo
.lsda
.targetAddress
!= CFI_INVALID_ADDRESS
)
1872 cfiStartsArray
[cfiStartsArrayCount
++] = cfiArray
[i
].u
.fdeInfo
.lsda
.targetAddress
;
1876 if ( cuLsdaCount
!= 0 ) {
1877 // merge in an lsda info from compact unwind
1878 for (uint32_t i
=0; i
< countOfCUs
; ++i
) {
1879 if ( cuInfoArray
[i
].lsdaAddress
== 0 )
1881 // append to cfiStartsArray if not already in that list
1883 for(uint32_t j
=0; j
< cfiStartsArrayCount
; ++j
) {
1884 if ( cfiStartsArray
[j
] == cuInfoArray
[i
].lsdaAddress
)
1888 cfiStartsArray
[cfiStartsArrayCount
++] = cuInfoArray
[i
].lsdaAddress
;
1892 if ( cfiStartsArrayCount
!= 0 ) {
1893 ::qsort(cfiStartsArray
, cfiStartsArrayCount
, sizeof(pint_t
), pointerSorter
);
1895 // scan for FDEs claming the same function
1896 for(uint32_t i
=1; i
< cfiStartsArrayCount
; ++i
) {
1897 assert( cfiStartsArray
[i
] != cfiStartsArray
[i
-1] );
1902 Section
<A
>** sections
= _file
->_sectionsArray
;
1903 uint32_t sectionsCount
= _file
->_sectionsArrayCount
;
1905 // figure out how many atoms will be allocated and allocate
1906 LabelAndCFIBreakIterator
breakIterator(sortedSymbolIndexes
, _symbolsInSections
, cfiStartsArray
,
1907 cfiStartsArrayCount
, _overlappingSymbols
);
1908 uint32_t computedAtomCount
= 0;
1909 for (uint32_t i
=0; i
< sectionsCount
; ++i
) {
1910 breakIterator
.beginSection();
1911 uint32_t count
= sections
[i
]->computeAtomCount(*this, breakIterator
, cfis
);
1912 //const macho_section<P>* sect = sections[i]->machoSection();
1913 //fprintf(stderr, "computed count=%u for section %s size=%llu\n", count, sect->sectname(), (sect != NULL) ? sect->size() : 0);
1914 computedAtomCount
+= count
;
1916 //fprintf(stderr, "allocating %d atoms * sizeof(Atom<A>)=%ld, sizeof(ld::Atom)=%ld\n", computedAtomCount, sizeof(Atom<A>), sizeof(ld::Atom));
1917 _file
->_atomsArray
= new uint8_t[computedAtomCount
*sizeof(Atom
<A
>)];
1918 _file
->_atomsArrayCount
= 0;
1920 // have each section append atoms to _atomsArray
1921 LabelAndCFIBreakIterator
breakIterator2(sortedSymbolIndexes
, _symbolsInSections
, cfiStartsArray
,
1922 cfiStartsArrayCount
, _overlappingSymbols
);
1923 for (uint32_t i
=0; i
< sectionsCount
; ++i
) {
1924 uint8_t* atoms
= _file
->_atomsArray
+ _file
->_atomsArrayCount
*sizeof(Atom
<A
>);
1925 breakIterator2
.beginSection();
1926 uint32_t count
= sections
[i
]->appendAtoms(*this, atoms
, breakIterator2
, cfis
);
1927 //fprintf(stderr, "append count=%u for section %s/%s\n", count, sections[i]->machoSection()->segname(), sections[i]->machoSection()->sectname());
1928 _file
->_atomsArrayCount
+= count
;
1930 assert( _file
->_atomsArrayCount
== computedAtomCount
&& "more atoms allocated than expected");
1933 // have each section add all fix-ups for its atoms
1934 _allFixups
.reserve(computedAtomCount
*5);
1935 for (uint32_t i
=0; i
< sectionsCount
; ++i
)
1936 sections
[i
]->makeFixups(*this, cfis
);
1938 // assign fixups start offset for each atom
1939 uint8_t* p
= _file
->_atomsArray
;
1940 uint32_t fixupOffset
= 0;
1941 for(int i
=_file
->_atomsArrayCount
; i
> 0; --i
) {
1942 Atom
<A
>* atom
= (Atom
<A
>*)p
;
1943 atom
->_fixupsStartIndex
= fixupOffset
;
1944 fixupOffset
+= atom
->_fixupsCount
;
1945 atom
->_fixupsCount
= 0;
1946 p
+= sizeof(Atom
<A
>);
1948 assert(fixupOffset
== _allFixups
.size());
1949 _file
->_fixups
.resize(fixupOffset
);
1951 // copy each fixup for each atom
1952 for(typename
std::vector
<FixupInAtom
>::iterator it
=_allFixups
.begin(); it
!= _allFixups
.end(); ++it
) {
1953 uint32_t slot
= it
->atom
->_fixupsStartIndex
+ it
->atom
->_fixupsCount
;
1954 _file
->_fixups
[slot
] = it
->fixup
;
1955 it
->atom
->_fixupsCount
++;
1958 // done with temp vector
1962 _file
->_unwindInfos
.reserve(countOfFDEs
+countOfCUs
);
1963 for(uint32_t i
=0; i
< countOfCFIs
; ++i
) {
1964 if ( cfiArray
[i
].isCIE
)
1966 if ( cfiArray
[i
].u
.fdeInfo
.function
.targetAddress
!= CFI_INVALID_ADDRESS
) {
1967 ld::Atom::UnwindInfo info
;
1968 info
.startOffset
= 0;
1969 info
.unwindInfo
= cfiArray
[i
].u
.fdeInfo
.compactUnwindInfo
;
1970 _file
->_unwindInfos
.push_back(info
);
1971 Atom
<A
>* func
= findAtomByAddress(cfiArray
[i
].u
.fdeInfo
.function
.targetAddress
);
1972 func
->setUnwindInfoRange(_file
->_unwindInfos
.size()-1, 1);
1973 //fprintf(stderr, "cu from dwarf =0x%08X, atom=%s\n", info.unwindInfo, func->name());
1976 // apply compact infos in __LD,__compact_unwind section to each function
1977 // if function also has dwarf unwind, CU will override it
1978 Atom
<A
>* lastFunc
= NULL
;
1979 uint32_t lastEnd
= 0;
1980 for(uint32_t i
=0; i
< countOfCUs
; ++i
) {
1981 typename CUSection
<A
>::Info
* info
= &cuInfoArray
[i
];
1982 assert(info
->function
!= NULL
);
1983 ld::Atom::UnwindInfo ui
;
1984 ui
.startOffset
= info
->functionStartAddress
- info
->function
->objectAddress();
1985 ui
.unwindInfo
= info
->compactUnwindInfo
;
1986 _file
->_unwindInfos
.push_back(ui
);
1987 // don't override with converted cu with "use dwarf" cu, if forcing dwarf conversion
1988 if ( !_forceDwarfConversion
|| !CUSection
<A
>::encodingMeansUseDwarf(info
->compactUnwindInfo
) ) {
1989 //fprintf(stderr, "cu=0x%08X, atom=%s\n", ui.unwindInfo, info->function->name());
1990 // if previous is for same function, extend range
1991 if ( info
->function
== lastFunc
) {
1992 if ( lastEnd
!= ui
.startOffset
) {
1993 if ( lastEnd
< ui
.startOffset
)
1994 warning("__LD,__compact_unwind entries for %s have a gap at offset 0x%0X", info
->function
->name(), lastEnd
);
1996 warning("__LD,__compact_unwind entries for %s overlap at offset 0x%0X", info
->function
->name(), lastEnd
);
1998 lastFunc
->extendUnwindInfoRange();
2001 info
->function
->setUnwindInfoRange(_file
->_unwindInfos
.size()-1, 1);
2002 lastFunc
= info
->function
;
2003 lastEnd
= ui
.startOffset
+ info
->rangeLength
;
2007 // process indirect symbols which become AliasAtoms
2008 _file
->_aliasAtomsArray
= NULL
;
2009 _file
->_aliasAtomsArrayCount
= 0;
2010 if ( _indirectSymbolCount
!= 0 ) {
2011 _file
->_aliasAtomsArrayCount
= _indirectSymbolCount
;
2012 _file
->_aliasAtomsArray
= new uint8_t[_file
->_aliasAtomsArrayCount
*sizeof(AliasAtom
)];
2013 this->appendAliasAtoms(_file
->_aliasAtomsArray
);
2017 // parse dwarf debug info to get line info
2018 this->parseDebugInfo();
2023 static void versionToString(uint32_t value
, char buffer
[32])
2026 sprintf(buffer
, "%d.%d.%d", value
>> 16, (value
>> 8) & 0xFF, value
& 0xFF);
2028 sprintf(buffer
, "%d.%d", value
>> 16, (value
>> 8) & 0xFF);
2031 template <> uint8_t Parser
<x86
>::loadCommandSizeMask() { return 0x03; }
2032 template <> uint8_t Parser
<x86_64
>::loadCommandSizeMask() { return 0x07; }
2033 template <> uint8_t Parser
<arm
>::loadCommandSizeMask() { return 0x03; }
2034 template <> uint8_t Parser
<arm64
>::loadCommandSizeMask() { return 0x07; }
2035 template <> bool Parser
<x86
>::useSimulatorVariant() { return true; }
2036 template <> bool Parser
<x86_64
>::useSimulatorVariant() { return true; }
2037 template <typename A
> bool Parser
<A
>::useSimulatorVariant() { return false; }
2040 template <typename A
>
2041 bool Parser
<A
>::parseLoadCommands(ld::VersionSet platforms
, bool simulator
, bool ignoreMismatchPlatform
)
2043 const macho_header
<P
>* header
= (const macho_header
<P
>*)_fileContent
;
2045 // set File attributes
2046 _file
->_canScatterAtoms
= (header
->flags() & MH_SUBSECTIONS_VIA_SYMBOLS
);
2047 _file
->_cpuSubType
= header
->cpusubtype();
2049 const macho_segment_command
<P
>* segment
= NULL
;
2050 const uint8_t* const endOfFile
= _fileContent
+ _fileLength
;
2051 const uint32_t cmd_count
= header
->ncmds();
2052 // <rdar://problem/5394172> an empty .o file with zero load commands will crash linker
2053 if ( cmd_count
== 0 )
2055 ld::VersionSet lcPlatforms
;
2056 const macho_load_command
<P
>* const cmds
= (macho_load_command
<P
>*)((char*)header
+ sizeof(macho_header
<P
>));
2057 const macho_load_command
<P
>* const cmdsEnd
= (macho_load_command
<P
>*)((char*)header
+ sizeof(macho_header
<P
>) + header
->sizeofcmds());
2058 const macho_load_command
<P
>* cmd
= cmds
;
2059 for (uint32_t i
= 0; i
< cmd_count
; ++i
) {
2060 uint32_t size
= cmd
->cmdsize();
2061 if ( (size
& this->loadCommandSizeMask()) != 0 )
2062 throwf("load command #%d has a unaligned size", i
);
2063 const uint8_t* endOfCmd
= ((uint8_t*)cmd
)+cmd
->cmdsize();
2064 if ( endOfCmd
> (uint8_t*)cmdsEnd
)
2065 throwf("load command #%d extends beyond the end of the load commands", i
);
2066 if ( endOfCmd
> endOfFile
)
2067 throwf("load command #%d extends beyond the end of the file", i
);
2068 switch (cmd
->cmd()) {
2071 const macho_symtab_command
<P
>* symtab
= (macho_symtab_command
<P
>*)cmd
;
2072 _symbolCount
= symtab
->nsyms();
2073 _symbols
= (const macho_nlist
<P
>*)(_fileContent
+ symtab
->symoff());
2074 _strings
= (char*)_fileContent
+ symtab
->stroff();
2075 _stringsSize
= symtab
->strsize();
2076 if ( (symtab
->symoff() + _symbolCount
*sizeof(macho_nlist
<P
>)) > _fileLength
)
2077 throw "mach-o symbol table extends beyond end of file";
2078 if ( (_strings
+ _stringsSize
) > (char*)endOfFile
)
2079 throw "mach-o string pool extends beyond end of file";
2080 if ( _indirectTable
== NULL
) {
2081 if ( _undefinedEndIndex
== 0 ) {
2082 _undefinedStartIndex
= 0;
2083 _undefinedEndIndex
= symtab
->nsyms();
2090 const macho_dysymtab_command
<P
>* dsymtab
= (macho_dysymtab_command
<P
>*)cmd
;
2091 _indirectTable
= (uint32_t*)(_fileContent
+ dsymtab
->indirectsymoff());
2092 _indirectTableCount
= dsymtab
->nindirectsyms();
2093 if ( &_indirectTable
[_indirectTableCount
] > (uint32_t*)endOfFile
)
2094 throw "indirect symbol table extends beyond end of file";
2095 _undefinedStartIndex
= dsymtab
->iundefsym();
2096 _undefinedEndIndex
= _undefinedStartIndex
+ dsymtab
->nundefsym();
2102 case LC_DATA_IN_CODE
:
2104 const macho_linkedit_data_command
<P
>* dc
= (macho_linkedit_data_command
<P
>*)cmd
;
2105 _dataInCodeStart
= (macho_data_in_code_entry
<P
>*)(_fileContent
+ dc
->dataoff());
2106 _dataInCodeEnd
= (macho_data_in_code_entry
<P
>*)(_fileContent
+ dc
->dataoff() + dc
->datasize());
2107 if ( _dataInCodeEnd
> (macho_data_in_code_entry
<P
>*)endOfFile
)
2108 throw "LC_DATA_IN_CODE table extends beyond end of file";
2111 case LC_LINKER_OPTION
:
2113 const macho_linker_option_command
<P
>* loc
= (macho_linker_option_command
<P
>*)cmd
;
2114 const char* buffer
= loc
->buffer();
2115 _file
->_linkerOptions
.resize(_file
->_linkerOptions
.size() + 1);
2116 std::vector
<const char*>& vec
= _file
->_linkerOptions
.back();
2117 for (uint32_t j
=0; j
< loc
->count(); ++j
) {
2118 vec
.push_back(buffer
);
2119 buffer
+= strlen(buffer
) + 1;
2121 if ( buffer
> ((char*)cmd
+ loc
->cmdsize()) )
2122 throw "malformed LC_LINKER_OPTION";
2125 case LC_LINKER_OPTIMIZATION_HINTS
:
2127 const macho_linkedit_data_command
<P
>* loh
= (macho_linkedit_data_command
<P
>*)cmd
;
2128 _lohStart
= _fileContent
+ loh
->dataoff();
2129 _lohEnd
= _fileContent
+ loh
->dataoff() + loh
->datasize();
2130 if ( _lohEnd
> endOfFile
)
2131 throw "LC_LINKER_OPTIMIZATION_HINTS table extends beyond end of file";
2134 case LC_VERSION_MIN_MACOSX
:
2135 case LC_VERSION_MIN_IPHONEOS
:
2136 case LC_VERSION_MIN_WATCHOS
:
2137 case LC_VERSION_MIN_TVOS
:
2138 if ( ignoreMismatchPlatform
)
2140 lcPlatforms
.add({Options::platformForLoadCommand(cmd
->cmd(), useSimulatorVariant()), ((macho_version_min_command
<P
>*)cmd
)->version()});
2141 _file
->_platforms
.add({Options::platformForLoadCommand(cmd
->cmd(), useSimulatorVariant()), ((macho_version_min_command
<P
>*)cmd
)->version()});
2143 case LC_BUILD_VERSION
:
2145 const macho_build_version_command
<P
>* buildVersCmd
= (macho_build_version_command
<P
>*)cmd
;
2146 if ( ignoreMismatchPlatform
)
2148 lcPlatforms
.add({(ld::Platform
)buildVersCmd
->platform(), buildVersCmd
->minos()});
2149 _file
->_platforms
.add({(ld::Platform
)buildVersCmd
->platform(), buildVersCmd
->minos()});
2150 const macho_build_tool_version
<P
>* entry
= (macho_build_tool_version
<P
>*)((uint8_t*)cmd
+ sizeof(macho_build_version_command
<P
>));
2151 for (uint32_t t
=0; t
< buildVersCmd
->ntools(); ++t
) {
2152 _file
->_toolVersions
.push_back(std::make_pair(entry
->tool(), entry
->version()));
2157 case macho_segment_command
<P
>::CMD
:
2158 if ( segment
!= NULL
)
2159 throw "more than one LC_SEGMENT found in object file";
2160 segment
= (macho_segment_command
<P
>*)cmd
;
2163 // ignore unknown load commands
2166 cmd
= (const macho_load_command
<P
>*)(((char*)cmd
)+cmd
->cmdsize());
2167 if ( cmd
> cmdsEnd
)
2168 throwf("malformed mach-o file, load command #%d is outside size of load commands", i
);
2171 // arm/arm64 objects are default to ios platform if not set.
2172 // rdar://problem/21746314
2173 if (lcPlatforms
.empty() &&
2174 (std::is_same
<A
, arm
>::value
|| std::is_same
<A
, arm64
>::value
))
2175 lcPlatforms
.add({ld::kPlatform_iOS
,0});
2177 // Check platform cross-linking.
2178 if ( !ignoreMismatchPlatform
) {
2179 __block
bool warned
= false;
2180 platforms
.forEach(^(ld::Platform platform
, uint32_t version
, bool &stop
) {
2181 if ( !warned
&& !lcPlatforms
.contains(platform
) ) {
2183 throwf("building for %s, but linking in object file built for %s,",
2184 platforms
.to_str().c_str(), lcPlatforms
.to_str().c_str());
2186 // FIXME: Re-enable once clang supports zippering
2187 // <rdar://problem/36749415> Turn off "urgent:" linker warning about iOSMac / macOS mismatch
2189 warning("URGENT: building for %s, but linking in object file (%s) built for %s. "
2190 "Note: This will be an error in the future.",
2191 platforms
.to_str().c_str(), path(), lcPlatforms
.to_str().c_str());
2195 if ( version
&& (lcPlatforms
.minOS(platform
) > version
) ) {
2198 versionToString(lcPlatforms
.minOS(platform
), t1
);
2199 versionToString(version
, t2
);
2200 warning("object file (%s) was built for newer %s version (%s) than being linked (%s)",
2201 _path
, Options::platformName(platform
), t1
, t2
);
2206 // validate just one segment
2207 if ( segment
== NULL
)
2208 throw "missing LC_SEGMENT";
2209 if ( segment
->filesize() > _fileLength
)
2210 throw "LC_SEGMENT filesize too large";
2212 // record and validate sections
2213 _sectionsStart
= (macho_section
<P
>*)((char*)segment
+ sizeof(macho_segment_command
<P
>));
2214 _machOSectionsCount
= segment
->nsects();
2215 if ( (sizeof(macho_segment_command
<P
>) + _machOSectionsCount
* sizeof(macho_section
<P
>)) > segment
->cmdsize() )
2216 throw "too many sections for size of LC_SEGMENT command";
2221 template <typename A
>
2222 ld::Platform Parser
<A
>::findPlatform(const macho_header
<P
>* header
, uint32_t* minOsVers
)
2224 const uint32_t cmd_count
= header
->ncmds();
2225 if ( cmd_count
== 0 )
2226 return ld::kPlatform_unknown
;
2227 const macho_load_command
<P
>* const cmds
= (macho_load_command
<P
>*)((char*)header
+ sizeof(macho_header
<P
>));
2228 const macho_load_command
<P
>* const cmdsEnd
= (macho_load_command
<P
>*)((char*)header
+ sizeof(macho_header
<P
>) + header
->sizeofcmds());
2229 const macho_load_command
<P
>* cmd
= cmds
;
2230 for (uint32_t i
= 0; i
< cmd_count
; ++i
) {
2231 uint32_t size
= cmd
->cmdsize();
2232 if ( (size
& loadCommandSizeMask()) != 0 )
2233 throwf("load command #%d has a unaligned size", i
);
2234 const uint8_t* endOfCmd
= ((uint8_t*)cmd
)+cmd
->cmdsize();
2235 if ( endOfCmd
> (uint8_t*)cmdsEnd
)
2236 throwf("load command #%d extends beyond the end of the load commands", i
);
2237 const macho_version_min_command
<P
>* versCmd
= (macho_version_min_command
<P
>*)cmd
;
2238 const macho_build_version_command
<P
>* buildVersCmd
= (macho_build_version_command
<P
>*)cmd
;
2239 *minOsVers
= versCmd
->version();
2240 switch (cmd
->cmd()) {
2241 case LC_VERSION_MIN_MACOSX
:
2242 return ld::kPlatform_macOS
;
2243 case LC_VERSION_MIN_IPHONEOS
:
2244 if (useSimulatorVariant())
2245 return ld::kPlatform_iOSSimulator
;
2247 return ld::kPlatform_iOS
;
2248 case LC_VERSION_MIN_WATCHOS
:
2249 if (useSimulatorVariant())
2250 return ld::kPlatform_watchOSSimulator
;
2252 return ld::kPlatform_watchOS
;
2253 case LC_VERSION_MIN_TVOS
:
2254 if (useSimulatorVariant())
2255 return ld::kPlatform_tvOSSimulator
;
2257 return ld::kPlatform_tvOS
;
2258 case LC_BUILD_VERSION
:
2259 *minOsVers
= buildVersCmd
->minos();
2260 return (ld::Platform
)buildVersCmd
->platform();
2262 cmd
= (const macho_load_command
<P
>*)(((char*)cmd
)+cmd
->cmdsize());
2263 if ( cmd
> cmdsEnd
)
2264 throwf("malformed mach-o file, load command #%d is outside size of load commands", i
);
2267 return ld::kPlatform_unknown
;
2271 template <typename A
>
2272 void Parser
<A
>::prescanSymbolTable()
2274 _tentativeDefinitionCount
= 0;
2275 _absoluteSymbolCount
= 0;
2276 _symbolsInSections
= 0;
2277 _hasDataInCodeLabels
= false;
2278 for (uint32_t i
=0; i
< this->_symbolCount
; ++i
) {
2279 const macho_nlist
<P
>& sym
= symbolFromIndex(i
);
2281 if ( (sym
.n_type() & N_STAB
) != 0 )
2284 // look at undefines
2285 const char* symbolName
= this->nameFromSymbol(sym
);
2286 if ( (sym
.n_type() & N_TYPE
) == N_UNDF
) {
2287 if ( sym
.n_value() != 0 ) {
2288 // count tentative definitions
2289 ++_tentativeDefinitionCount
;
2291 else if ( strncmp(symbolName
, "___dtrace_", 10) == 0 ) {
2292 // any undefined starting with __dtrace_*$ that is not ___dtrace_probe$* or ___dtrace_isenabled$*
2293 // is extra provider info
2294 if ( (strncmp(&symbolName
[10], "probe$", 6) != 0) && (strncmp(&symbolName
[10], "isenabled$", 10) != 0) ) {
2295 _dtraceProviderInfo
.push_back(symbolName
);
2300 else if ( ((sym
.n_type() & N_TYPE
) == N_INDR
) && ((sym
.n_type() & N_EXT
) != 0) ) {
2301 _indirectSymbolCount
++;
2305 // count absolute symbols
2306 if ( (sym
.n_type() & N_TYPE
) == N_ABS
) {
2307 const char* absName
= this->nameFromSymbol(sym
);
2308 // ignore .objc_class_name_* symbols
2309 if ( strncmp(absName
, ".objc_class_name_", 17) == 0 ) {
2313 // ignore .objc_class_name_* symbols
2314 if ( strncmp(absName
, ".objc_category_name_", 20) == 0 )
2316 // ignore empty *.eh symbols
2317 if ( strcmp(&absName
[strlen(absName
)-3], ".eh") == 0 )
2319 ++_absoluteSymbolCount
;
2322 // only look at definitions
2323 if ( (sym
.n_type() & N_TYPE
) != N_SECT
)
2326 // 'L' labels do not denote atom breaks
2327 if ( symbolName
[0] == 'L' ) {
2328 // <rdar://problem/9218847> Formalize data in code with L$start$ labels
2329 if ( strncmp(symbolName
, "L$start$", 8) == 0 )
2330 _hasDataInCodeLabels
= true;
2333 // how many def syms in each section
2334 if ( sym
.n_sect() > _machOSectionsCount
)
2335 throw "bad n_sect in symbol table";
2337 _symbolsInSections
++;
2341 template <typename A
>
2342 void Parser
<A
>::appendAliasAtoms(uint8_t* p
)
2344 for (uint32_t i
=0; i
< this->_symbolCount
; ++i
) {
2345 const macho_nlist
<P
>& sym
= symbolFromIndex(i
);
2347 if ( (sym
.n_type() & N_STAB
) != 0 )
2350 // only look at N_INDR symbols
2351 if ( (sym
.n_type() & N_TYPE
) != N_INDR
)
2354 // skip non-external aliases
2355 if ( (sym
.n_type() & N_EXT
) == 0 )
2358 const char* symbolName
= this->nameFromSymbol(sym
);
2359 const char* aliasOfName
= &_strings
[sym
.n_value()];
2360 bool isHiddenVisibility
= (sym
.n_type() & N_PEXT
);
2361 AliasAtom
* allocatedSpace
= (AliasAtom
*)p
;
2362 new (allocatedSpace
) AliasAtom(symbolName
, isHiddenVisibility
, _file
, aliasOfName
);
2363 p
+= sizeof(AliasAtom
);
2369 template <typename A
>
2370 int Parser
<A
>::sectionIndexSorter(void* extra
, const void* l
, const void* r
)
2372 Parser
<A
>* parser
= (Parser
<A
>*)extra
;
2373 const uint32_t* left
= (uint32_t*)l
;
2374 const uint32_t* right
= (uint32_t*)r
;
2375 const macho_section
<P
>* leftSect
= parser
->machOSectionFromSectionIndex(*left
);
2376 const macho_section
<P
>* rightSect
= parser
->machOSectionFromSectionIndex(*right
);
2378 // can't just return difference because 64-bit diff does not fit in 32-bit return type
2379 int64_t result
= leftSect
->addr() - rightSect
->addr();
2380 if ( result
== 0 ) {
2381 // two sections with same start address
2382 // one with zero size goes first
2383 bool leftEmpty
= ( leftSect
->size() == 0 );
2384 bool rightEmpty
= ( rightSect
->size() == 0 );
2385 if ( leftEmpty
!= rightEmpty
) {
2386 return ( rightEmpty
? 1 : -1 );
2388 if ( !leftEmpty
&& !rightEmpty
)
2389 throwf("overlapping sections");
2390 // both empty, so chose file order
2391 return ( rightSect
- leftSect
);
2393 else if ( result
< 0 )
2399 template <typename A
>
2400 void Parser
<A
>::makeSortedSectionsArray(uint32_t array
[])
2402 const bool log
= false;
2405 fprintf(stderr
, "unsorted sections:\n");
2406 for(unsigned int i
=0; i
< _machOSectionsCount
; ++i
)
2407 fprintf(stderr
, "0x%08llX %s %s\n", _sectionsStart
[i
].addr(), _sectionsStart
[i
].segname(), _sectionsStart
[i
].sectname());
2410 // sort by symbol table address
2411 for (uint32_t i
=0; i
< _machOSectionsCount
; ++i
)
2413 ::qsort_r(array
, _machOSectionsCount
, sizeof(uint32_t), this, §ionIndexSorter
);
2416 fprintf(stderr
, "sorted sections:\n");
2417 for(unsigned int i
=0; i
< _machOSectionsCount
; ++i
)
2418 fprintf(stderr
, "0x%08llX %s %s\n", _sectionsStart
[array
[i
]].addr(), _sectionsStart
[array
[i
]].segname(), _sectionsStart
[array
[i
]].sectname());
2424 template <typename A
>
2425 int Parser
<A
>::symbolIndexSorter(void* extra
, const void* l
, const void* r
)
2427 ParserAndSectionsArray
* extraInfo
= (ParserAndSectionsArray
*)extra
;
2428 Parser
<A
>* parser
= extraInfo
->parser
;
2429 const uint32_t* sortedSectionsArray
= extraInfo
->sortedSectionsArray
;
2430 const uint32_t* left
= (uint32_t*)l
;
2431 const uint32_t* right
= (uint32_t*)r
;
2432 const macho_nlist
<P
>& leftSym
= parser
->symbolFromIndex(*left
);
2433 const macho_nlist
<P
>& rightSym
= parser
->symbolFromIndex(*right
);
2434 // can't just return difference because 64-bit diff does not fit in 32-bit return type
2435 int64_t result
= leftSym
.n_value() - rightSym
.n_value();
2436 if ( result
== 0 ) {
2437 // two symbols with same address
2438 // if in different sections, sort earlier section first
2439 if ( leftSym
.n_sect() != rightSym
.n_sect() ) {
2440 for (uint32_t i
=0; i
< parser
->machOSectionCount(); ++i
) {
2441 if ( sortedSectionsArray
[i
]+1 == leftSym
.n_sect() )
2443 if ( sortedSectionsArray
[i
]+1 == rightSym
.n_sect() )
2447 // two symbols in same section, means one is an alias
2448 // if one is ltmp*, make it an alias (sort first)
2449 const char* leftName
= parser
->nameFromSymbol(leftSym
);
2450 const char* rightName
= parser
->nameFromSymbol(rightSym
);
2451 bool leftIsTmp
= strncmp(leftName
, "ltmp", 4);
2452 bool rightIsTmp
= strncmp(rightName
, "ltmp", 4);
2453 if ( leftIsTmp
!= rightIsTmp
) {
2454 return (rightIsTmp
? -1 : 1);
2457 // if only one is global, make the other an alias (sort first)
2458 if ( (leftSym
.n_type() & N_EXT
) != (rightSym
.n_type() & N_EXT
) ) {
2459 if ( (rightSym
.n_type() & N_EXT
) != 0 )
2464 // if both are global, sort alphabetically. earlier one will be the alias
2465 return ( strcmp(rightName
, leftName
) );
2467 else if ( result
< 0 )
2474 template <typename A
>
2475 void Parser
<A
>::makeSortedSymbolsArray(uint32_t array
[], const uint32_t sectionArray
[])
2477 const bool log
= false;
2479 uint32_t* p
= array
;
2480 for (uint32_t i
=0; i
< this->_symbolCount
; ++i
) {
2481 const macho_nlist
<P
>& sym
= symbolFromIndex(i
);
2483 if ( (sym
.n_type() & N_STAB
) != 0 )
2486 // only look at definitions
2487 if ( (sym
.n_type() & N_TYPE
) != N_SECT
)
2490 // 'L' labels do not denote atom breaks
2491 const char* symbolName
= this->nameFromSymbol(sym
);
2492 if ( symbolName
[0] == 'L' )
2495 // how many def syms in each section
2496 if ( sym
.n_sect() > _machOSectionsCount
)
2497 throw "bad n_sect in symbol table";
2502 assert(p
== &array
[_symbolsInSections
] && "second pass over symbol table yield a different number of symbols");
2504 // sort by symbol table address
2505 ParserAndSectionsArray extra
= { this, sectionArray
};
2506 ::qsort_r(array
, _symbolsInSections
, sizeof(uint32_t), &extra
, &symbolIndexSorter
);
2509 // look for two symbols at same address
2510 _overlappingSymbols
= false;
2511 for (unsigned int i
=1; i
< _symbolsInSections
; ++i
) {
2512 if ( symbolFromIndex(array
[i
-1]).n_value() == symbolFromIndex(array
[i
]).n_value() ) {
2513 //fprintf(stderr, "overlapping symbols at 0x%08llX\n", symbolFromIndex(array[i-1]).n_value());
2514 _overlappingSymbols
= true;
2520 fprintf(stderr
, "sorted symbols:\n");
2521 for(unsigned int i
=0; i
< _symbolsInSections
; ++i
)
2522 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
])) );
2526 template <typename A
>
2527 void Parser
<A
>::makeSections()
2529 // classify each section by type
2530 // compute how many Section objects will be needed and total size for all
2531 unsigned int totalSectionsSize
= 0;
2532 uint8_t machOSectsStorage
[sizeof(MachOSectionAndSectionClass
<P
>)*(_machOSectionsCount
+2)]; // also room for tentative-defs and absolute symbols
2533 // allocate raw storage for all section objects on stack
2534 MachOSectionAndSectionClass
<P
>* machOSects
= (MachOSectionAndSectionClass
<P
>*)machOSectsStorage
;
2535 unsigned int count
= 0;
2536 // local variable for bitcode parsing
2537 const macho_section
<P
>* bitcodeSect
= NULL
;
2538 const macho_section
<P
>* cmdlineSect
= NULL
;
2539 const macho_section
<P
>* swiftCmdlineSect
= NULL
;
2540 const macho_section
<P
>* bundleSect
= NULL
;
2541 bool bitcodeAsm
= false;
2543 for (uint32_t i
=0; i
< _machOSectionsCount
; ++i
) {
2544 const macho_section
<P
>* sect
= &_sectionsStart
[i
];
2545 uint8_t sectionType
= (sect
->flags() & SECTION_TYPE
);
2546 if ( (sect
->offset() + sect
->size() > _fileLength
) && (sectionType
!= S_ZEROFILL
) && (sectionType
!= S_THREAD_LOCAL_ZEROFILL
) )
2547 throwf("section %s/%s extends beyond end of file,", sect
->segname(), sect
->sectname());
2549 if ( (sect
->flags() & S_ATTR_DEBUG
) != 0 ) {
2550 if ( strcmp(sect
->segname(), "__DWARF") == 0 ) {
2551 // note that .o file has dwarf
2552 _file
->_debugInfoKind
= ld::relocatable::File::kDebugInfoDwarf
;
2553 // save off iteresting dwarf sections
2554 if ( strcmp(sect
->sectname(), "__debug_info") == 0 )
2555 _file
->_dwarfDebugInfoSect
= sect
;
2556 else if ( strcmp(sect
->sectname(), "__debug_abbrev") == 0 )
2557 _file
->_dwarfDebugAbbrevSect
= sect
;
2558 else if ( strcmp(sect
->sectname(), "__debug_line") == 0 )
2559 _file
->_dwarfDebugLineSect
= sect
;
2560 else if ( strcmp(sect
->sectname(), "__debug_str") == 0 )
2561 _file
->_dwarfDebugStringSect
= sect
;
2562 // linker does not propagate dwarf sections to output file
2565 else if ( strcmp(sect
->segname(), "__LD") == 0 ) {
2566 if ( strncmp(sect
->sectname(), "__compact_unwind", 16) == 0 ) {
2567 machOSects
[count
].sect
= sect
;
2568 totalSectionsSize
+= sizeof(CUSection
<A
>);
2569 machOSects
[count
++].type
= sectionTypeCompactUnwind
;
2574 if ( strcmp(sect
->segname(), "__LLVM") == 0 ) {
2575 // Process bitcode segement
2576 if ( strncmp(sect
->sectname(), "__bitcode", 9) == 0 ) {
2578 } else if ( strncmp(sect
->sectname(), "__cmdline", 9) == 0 ) {
2580 } else if ( strncmp(sect
->sectname(), "__swift_cmdline", 15) == 0 ) {
2581 swiftCmdlineSect
= sect
;
2582 } else if ( strncmp(sect
->sectname(), "__bundle", 8) == 0 ) {
2584 } else if ( strncmp(sect
->sectname(), "__asm", 5) == 0 ) {
2587 // If treat the bitcode as data, continue to parse as a normal section.
2588 if ( !_treateBitcodeAsData
)
2591 // ignore empty __OBJC sections
2592 if ( (sect
->size() == 0) && (strcmp(sect
->segname(), "__OBJC") == 0) )
2594 // objc image info section is really attributes and not content
2595 if ( ((strcmp(sect
->sectname(), "__image_info") == 0) && (strcmp(sect
->segname(), "__OBJC") == 0))
2596 || ((strncmp(sect
->sectname(), "__objc_imageinfo", 16) == 0) && (strcmp(sect
->segname(), "__DATA") == 0)) ) {
2597 // struct objc_image_info {
2598 // uint32_t version; // initially 0
2601 // #define OBJC_IMAGE_SUPPORTS_GC 2
2602 // #define OBJC_IMAGE_GC_ONLY 4
2603 // #define OBJC_IMAGE_IS_SIMULATED 32
2604 // #define OBJC_IMAGE_HAS_CATEGORY_CLASS_PROPERTIES 64
2606 const uint32_t* contents
= (uint32_t*)(_file
->fileContent()+sect
->offset());
2607 if ( (sect
->size() >= 8) && (contents
[0] == 0) ) {
2608 uint32_t flags
= E::get32(contents
[1]);
2609 _file
->_hasObjC
= true;
2610 _file
->_swiftVersion
= ((flags
>> 8) & 0xFF);
2611 _file
->_objcHasCategoryClassPropertiesField
= (flags
& 64);
2612 if ( sect
->size() > 8 ) {
2613 warning("section %s/%s has unexpectedly large size %llu in %s",
2614 sect
->segname(), Section
<A
>::makeSectionName(sect
), sect
->size(), _file
->path());
2618 warning("can't parse %s/%s section in %s", sect
->segname(), Section
<A
>::makeSectionName(sect
), _file
->path());
2622 machOSects
[count
].sect
= sect
;
2623 switch ( sect
->flags() & SECTION_TYPE
) {
2624 case S_SYMBOL_STUBS
:
2625 if ( _stubsSectionNum
== 0 ) {
2626 _stubsSectionNum
= i
+1;
2627 _stubsMachOSection
= sect
;
2630 assert(1 && "multiple S_SYMBOL_STUBS sections");
2631 case S_LAZY_SYMBOL_POINTERS
:
2633 case S_4BYTE_LITERALS
:
2634 totalSectionsSize
+= sizeof(Literal4Section
<A
>);
2635 machOSects
[count
++].type
= sectionTypeLiteral4
;
2637 case S_8BYTE_LITERALS
:
2638 totalSectionsSize
+= sizeof(Literal8Section
<A
>);
2639 machOSects
[count
++].type
= sectionTypeLiteral8
;
2641 case S_16BYTE_LITERALS
:
2642 totalSectionsSize
+= sizeof(Literal16Section
<A
>);
2643 machOSects
[count
++].type
= sectionTypeLiteral16
;
2645 case S_NON_LAZY_SYMBOL_POINTERS
:
2646 totalSectionsSize
+= sizeof(NonLazyPointerSection
<A
>);
2647 machOSects
[count
++].type
= sectionTypeNonLazy
;
2649 case S_THREAD_LOCAL_VARIABLE_POINTERS
:
2650 totalSectionsSize
+= sizeof(TLVPointerSection
<A
>);
2651 machOSects
[count
++].type
= sectionTypeTLVPointers
;
2653 case S_LITERAL_POINTERS
:
2654 if ( (strcmp(sect
->segname(), "__OBJC") == 0) && (strcmp(sect
->sectname(), "__cls_refs") == 0) ) {
2655 totalSectionsSize
+= sizeof(Objc1ClassReferences
<A
>);
2656 machOSects
[count
++].type
= sectionTypeObjC1ClassRefs
;
2659 totalSectionsSize
+= sizeof(PointerToCStringSection
<A
>);
2660 machOSects
[count
++].type
= sectionTypeCStringPointer
;
2663 case S_CSTRING_LITERALS
:
2664 totalSectionsSize
+= sizeof(CStringSection
<A
>);
2665 machOSects
[count
++].type
= sectionTypeCString
;
2667 case S_MOD_INIT_FUNC_POINTERS
:
2668 case S_MOD_TERM_FUNC_POINTERS
:
2669 case S_THREAD_LOCAL_INIT_FUNCTION_POINTERS
:
2674 case S_THREAD_LOCAL_REGULAR
:
2675 case S_THREAD_LOCAL_ZEROFILL
:
2676 if ( (strcmp(sect
->segname(), "__TEXT") == 0) && (strcmp(sect
->sectname(), "__eh_frame") == 0) ) {
2677 totalSectionsSize
+= sizeof(CFISection
<A
>);
2678 machOSects
[count
++].type
= sectionTypeCFI
;
2680 else if ( (strcmp(sect
->segname(), "__DATA") == 0) && (strcmp(sect
->sectname(), "__cfstring") == 0) ) {
2681 totalSectionsSize
+= sizeof(CFStringSection
<A
>);
2682 machOSects
[count
++].type
= sectionTypeCFString
;
2684 else if ( (strcmp(sect
->segname(), "__TEXT") == 0) && (strcmp(sect
->sectname(), "__ustring") == 0) ) {
2685 totalSectionsSize
+= sizeof(UTF16StringSection
<A
>);
2686 machOSects
[count
++].type
= sectionTypeUTF16Strings
;
2688 else if ( (strcmp(sect
->segname(), "__DATA") == 0) && (strncmp(sect
->sectname(), "__objc_classrefs", 16) == 0) ) {
2689 totalSectionsSize
+= sizeof(ObjC2ClassRefsSection
<A
>);
2690 machOSects
[count
++].type
= sectionTypeObjC2ClassRefs
;
2692 else if ( (strcmp(sect
->segname(), "__DATA") == 0) && (strcmp(sect
->sectname(), "__objc_catlist") == 0) ) {
2693 totalSectionsSize
+= sizeof(ObjC2CategoryListSection
<A
>);
2694 machOSects
[count
++].type
= typeObjC2CategoryList
;
2696 else if ( _AppleObjc
&& (strcmp(sect
->segname(), "__OBJC") == 0) && (strcmp(sect
->sectname(), "__class") == 0) ) {
2697 totalSectionsSize
+= sizeof(ObjC1ClassSection
<A
>);
2698 machOSects
[count
++].type
= sectionTypeObjC1Classes
;
2701 totalSectionsSize
+= sizeof(SymboledSection
<A
>);
2702 machOSects
[count
++].type
= sectionTypeSymboled
;
2705 case S_THREAD_LOCAL_VARIABLES
:
2706 totalSectionsSize
+= sizeof(TLVDefsSection
<A
>);
2707 machOSects
[count
++].type
= sectionTypeTLVDefs
;
2710 throwf("unknown section type %d", sect
->flags() & SECTION_TYPE
);
2715 if ( bitcodeSect
!= NULL
) {
2716 if ( cmdlineSect
!= NULL
)
2717 _file
->_bitcode
= std::unique_ptr
<ld::Bitcode
>(new ld::ClangBitcode(&_fileContent
[bitcodeSect
->offset()], bitcodeSect
->size(),
2718 &_fileContent
[cmdlineSect
->offset()], cmdlineSect
->size()));
2719 else if ( swiftCmdlineSect
!= NULL
)
2720 _file
->_bitcode
= std::unique_ptr
<ld::Bitcode
>(new ld::SwiftBitcode(&_fileContent
[bitcodeSect
->offset()], bitcodeSect
->size(),
2721 &_fileContent
[swiftCmdlineSect
->offset()], swiftCmdlineSect
->size()));
2723 throwf("Object file with bitcode missing cmdline options: %s", _file
->path());
2725 else if ( bundleSect
!= NULL
)
2726 _file
->_bitcode
= std::unique_ptr
<ld::Bitcode
>(new ld::BundleBitcode(&_fileContent
[bundleSect
->offset()], bundleSect
->size()));
2727 else if ( bitcodeAsm
)
2728 _file
->_bitcode
= std::unique_ptr
<ld::Bitcode
>(new ld::AsmBitcode(_fileContent
, _fileLength
));
2730 // sort by address (mach-o object files don't aways have sections sorted)
2731 ::qsort(machOSects
, count
, sizeof(MachOSectionAndSectionClass
<P
>), MachOSectionAndSectionClass
<P
>::sorter
);
2733 // we will synthesize a dummy Section<A> object for tentative definitions
2734 if ( _tentativeDefinitionCount
> 0 ) {
2735 totalSectionsSize
+= sizeof(TentativeDefinitionSection
<A
>);
2736 machOSects
[count
++].type
= sectionTypeTentativeDefinitions
;
2739 // we will synthesize a dummy Section<A> object for Absolute symbols
2740 if ( _absoluteSymbolCount
> 0 ) {
2741 totalSectionsSize
+= sizeof(AbsoluteSymbolSection
<A
>);
2742 machOSects
[count
++].type
= sectionTypeAbsoluteSymbols
;
2745 // allocate one block for all Section objects as well as pointers to each
2746 uint8_t* space
= new uint8_t[totalSectionsSize
+count
*sizeof(Section
<A
>*)];
2747 _file
->_sectionsArray
= (Section
<A
>**)space
;
2748 _file
->_sectionsArrayCount
= count
;
2749 Section
<A
>** objects
= _file
->_sectionsArray
;
2750 space
+= count
*sizeof(Section
<A
>*);
2751 for (uint32_t i
=0; i
< count
; ++i
) {
2752 switch ( machOSects
[i
].type
) {
2753 case sectionTypeIgnore
:
2755 case sectionTypeLiteral4
:
2756 *objects
++ = new (space
) Literal4Section
<A
>(*this, *_file
, machOSects
[i
].sect
);
2757 space
+= sizeof(Literal4Section
<A
>);
2759 case sectionTypeLiteral8
:
2760 *objects
++ = new (space
) Literal8Section
<A
>(*this, *_file
, machOSects
[i
].sect
);
2761 space
+= sizeof(Literal8Section
<A
>);
2763 case sectionTypeLiteral16
:
2764 *objects
++ = new (space
) Literal16Section
<A
>(*this, *_file
, machOSects
[i
].sect
);
2765 space
+= sizeof(Literal16Section
<A
>);
2767 case sectionTypeNonLazy
:
2768 *objects
++ = new (space
) NonLazyPointerSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2769 space
+= sizeof(NonLazyPointerSection
<A
>);
2771 case sectionTypeTLVPointers
:
2772 *objects
++ = new (space
) TLVPointerSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2773 space
+= sizeof(TLVPointerSection
<A
>);
2775 case sectionTypeCFI
:
2776 _EHFrameSection
= new (space
) CFISection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2777 *objects
++ = _EHFrameSection
;
2778 space
+= sizeof(CFISection
<A
>);
2780 case sectionTypeCString
:
2781 *objects
++ = new (space
) CStringSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2782 space
+= sizeof(CStringSection
<A
>);
2784 case sectionTypeCStringPointer
:
2785 *objects
++ = new (space
) PointerToCStringSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2786 space
+= sizeof(PointerToCStringSection
<A
>);
2788 case sectionTypeObjC1ClassRefs
:
2789 *objects
++ = new (space
) Objc1ClassReferences
<A
>(*this, *_file
, machOSects
[i
].sect
);
2790 space
+= sizeof(Objc1ClassReferences
<A
>);
2792 case sectionTypeUTF16Strings
:
2793 *objects
++ = new (space
) UTF16StringSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2794 space
+= sizeof(UTF16StringSection
<A
>);
2796 case sectionTypeCFString
:
2797 *objects
++ = new (space
) CFStringSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2798 space
+= sizeof(CFStringSection
<A
>);
2800 case sectionTypeObjC2ClassRefs
:
2801 *objects
++ = new (space
) ObjC2ClassRefsSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2802 space
+= sizeof(ObjC2ClassRefsSection
<A
>);
2804 case typeObjC2CategoryList
:
2805 *objects
++ = new (space
) ObjC2CategoryListSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2806 space
+= sizeof(ObjC2CategoryListSection
<A
>);
2808 case sectionTypeObjC1Classes
:
2809 *objects
++ = new (space
) ObjC1ClassSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2810 space
+= sizeof(ObjC1ClassSection
<A
>);
2812 case sectionTypeSymboled
:
2813 *objects
++ = new (space
) SymboledSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2814 space
+= sizeof(SymboledSection
<A
>);
2816 case sectionTypeTLVDefs
:
2817 *objects
++ = new (space
) TLVDefsSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2818 space
+= sizeof(TLVDefsSection
<A
>);
2820 case sectionTypeCompactUnwind
:
2821 _compactUnwindSection
= new (space
) CUSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2822 *objects
++ = _compactUnwindSection
;
2823 space
+= sizeof(CUSection
<A
>);
2825 case sectionTypeTentativeDefinitions
:
2826 *objects
++ = new (space
) TentativeDefinitionSection
<A
>(*this, *_file
);
2827 space
+= sizeof(TentativeDefinitionSection
<A
>);
2829 case sectionTypeAbsoluteSymbols
:
2830 _absoluteSection
= new (space
) AbsoluteSymbolSection
<A
>(*this, *_file
);
2831 *objects
++ = _absoluteSection
;
2832 space
+= sizeof(AbsoluteSymbolSection
<A
>);
2835 throw "internal error uknown SectionType";
2841 template <typename A
>
2842 Section
<A
>* Parser
<A
>::sectionForAddress(typename
A::P::uint_t addr
)
2844 for (uint32_t i
=0; i
< _file
->_sectionsArrayCount
; ++i
) {
2845 const macho_section
<typename
A::P
>* sect
= _file
->_sectionsArray
[i
]->machoSection();
2846 // TentativeDefinitionSection and AbsoluteSymbolSection have no mach-o section
2847 if ( sect
!= NULL
) {
2848 if ( (sect
->addr() <= addr
) && (addr
< (sect
->addr()+sect
->size())) ) {
2849 return _file
->_sectionsArray
[i
];
2853 // not strictly in any section
2854 // may be in a zero length section
2855 for (uint32_t i
=0; i
< _file
->_sectionsArrayCount
; ++i
) {
2856 const macho_section
<typename
A::P
>* sect
= _file
->_sectionsArray
[i
]->machoSection();
2857 // TentativeDefinitionSection and AbsoluteSymbolSection have no mach-o section
2858 if ( sect
!= NULL
) {
2859 if ( (sect
->addr() == addr
) && (sect
->size() == 0) ) {
2860 return _file
->_sectionsArray
[i
];
2865 throwf("sectionForAddress(0x%llX) address not in any section", (uint64_t)addr
);
2868 template <typename A
>
2869 Section
<A
>* Parser
<A
>::sectionForNum(unsigned int num
)
2871 for (uint32_t i
=0; i
< _file
->_sectionsArrayCount
; ++i
) {
2872 const macho_section
<typename
A::P
>* sect
= _file
->_sectionsArray
[i
]->machoSection();
2873 // TentativeDefinitionSection and AbsoluteSymbolSection have no mach-o section
2874 if ( sect
!= NULL
) {
2875 if ( num
== (unsigned int)((sect
- _sectionsStart
)+1) )
2876 return _file
->_sectionsArray
[i
];
2879 throwf("sectionForNum(%u) section number not for any section", num
);
2882 template <typename A
>
2883 Atom
<A
>* Parser
<A
>::findAtomByAddress(pint_t addr
)
2885 Section
<A
>* section
= this->sectionForAddress(addr
);
2886 return section
->findAtomByAddress(addr
);
2889 template <typename A
>
2890 Atom
<A
>* Parser
<A
>::findAtomByAddressOrNullIfStub(pint_t addr
)
2892 if ( hasStubsSection() && (_stubsMachOSection
->addr() <= addr
) && (addr
< (_stubsMachOSection
->addr()+_stubsMachOSection
->size())) )
2894 return findAtomByAddress(addr
);
2897 template <typename A
>
2898 Atom
<A
>* Parser
<A
>::findAtomByAddressOrLocalTargetOfStub(pint_t addr
, uint32_t* offsetInAtom
)
2900 if ( hasStubsSection() && (_stubsMachOSection
->addr() <= addr
) && (addr
< (_stubsMachOSection
->addr()+_stubsMachOSection
->size())) ) {
2901 // target is a stub, remove indirection
2902 uint32_t symbolIndex
= this->symbolIndexFromIndirectSectionAddress(addr
, _stubsMachOSection
);
2903 assert(symbolIndex
!= INDIRECT_SYMBOL_LOCAL
);
2904 const macho_nlist
<P
>& sym
= this->symbolFromIndex(symbolIndex
);
2905 // can't be to external weak symbol
2906 assert( (this->combineFromSymbol(sym
) != ld::Atom::combineByName
) || (this->scopeFromSymbol(sym
) != ld::Atom::scopeGlobal
) );
2908 return this->findAtomByName(this->nameFromSymbol(sym
));
2910 Atom
<A
>* target
= this->findAtomByAddress(addr
);
2911 *offsetInAtom
= addr
- target
->_objAddress
;
2915 template <typename A
>
2916 Atom
<A
>* Parser
<A
>::findAtomByName(const char* name
)
2918 uint8_t* p
= _file
->_atomsArray
;
2919 for(int i
=_file
->_atomsArrayCount
; i
> 0; --i
) {
2920 Atom
<A
>* atom
= (Atom
<A
>*)p
;
2921 if ( strcmp(name
, atom
->name()) == 0 )
2923 p
+= sizeof(Atom
<A
>);
2928 template <typename A
>
2929 void Parser
<A
>::findTargetFromAddress(pint_t addr
, TargetDesc
& target
)
2931 if ( hasStubsSection() && (_stubsMachOSection
->addr() <= addr
) && (addr
< (_stubsMachOSection
->addr()+_stubsMachOSection
->size())) ) {
2932 // target is a stub, remove indirection
2933 uint32_t symbolIndex
= this->symbolIndexFromIndirectSectionAddress(addr
, _stubsMachOSection
);
2934 assert(symbolIndex
!= INDIRECT_SYMBOL_LOCAL
);
2935 const macho_nlist
<P
>& sym
= this->symbolFromIndex(symbolIndex
);
2937 target
.name
= this->nameFromSymbol(sym
);
2938 target
.weakImport
= this->weakImportFromSymbol(sym
);
2942 Section
<A
>* section
= this->sectionForAddress(addr
);
2943 target
.atom
= section
->findAtomByAddress(addr
);
2944 target
.addend
= addr
- target
.atom
->_objAddress
;
2945 target
.weakImport
= false;
2949 template <typename A
>
2950 void Parser
<A
>::findTargetFromAddress(pint_t baseAddr
, pint_t addr
, TargetDesc
& target
)
2952 findTargetFromAddress(baseAddr
, target
);
2953 target
.addend
= addr
- target
.atom
->_objAddress
;
2956 template <typename A
>
2957 void Parser
<A
>::findTargetFromAddressAndSectionNum(pint_t addr
, unsigned int sectNum
, TargetDesc
& target
)
2959 if ( sectNum
== R_ABS
) {
2960 // target is absolute symbol that corresponds to addr
2961 if ( _absoluteSection
!= NULL
) {
2962 target
.atom
= _absoluteSection
->findAbsAtomForValue(addr
);
2963 if ( target
.atom
!= NULL
) {
2965 target
.weakImport
= false;
2970 throwf("R_ABS reloc but no absolute symbol at target address");
2973 if ( hasStubsSection() && (stubsSectionNum() == sectNum
) ) {
2974 // target is a stub, remove indirection
2975 uint32_t symbolIndex
= this->symbolIndexFromIndirectSectionAddress(addr
, _stubsMachOSection
);
2976 assert(symbolIndex
!= INDIRECT_SYMBOL_LOCAL
);
2977 const macho_nlist
<P
>& sym
= this->symbolFromIndex(symbolIndex
);
2978 // use direct reference when stub is to a static function
2979 if ( ((sym
.n_type() & N_TYPE
) == N_SECT
) && (((sym
.n_type() & N_EXT
) == 0) || (this->nameFromSymbol(sym
)[0] == 'L')) ) {
2980 this->findTargetFromAddressAndSectionNum(sym
.n_value(), sym
.n_sect(), target
);
2984 target
.name
= this->nameFromSymbol(sym
);
2985 target
.weakImport
= this->weakImportFromSymbol(sym
);
2990 Section
<A
>* section
= this->sectionForNum(sectNum
);
2991 target
.atom
= section
->findAtomByAddress(addr
);
2992 if ( target
.atom
== NULL
) {
2993 typedef typename
A::P::sint_t sint_t
;
2994 sint_t a
= (sint_t
)addr
;
2995 sint_t sectStart
= (sint_t
)(section
->machoSection()->addr());
2996 sint_t sectEnd
= sectStart
+ section
->machoSection()->size();
2997 if ( a
< sectStart
) {
2998 // target address is before start of section, so must be negative addend
2999 target
.atom
= section
->findAtomByAddress(sectStart
);
3000 target
.addend
= a
- sectStart
;
3001 target
.weakImport
= false;
3005 else if ( a
>= sectEnd
) {
3006 target
.atom
= section
->findAtomByAddress(sectEnd
-1);
3007 target
.addend
= a
- sectEnd
;
3008 target
.weakImport
= false;
3013 assert(target
.atom
!= NULL
);
3014 target
.addend
= addr
- target
.atom
->_objAddress
;
3015 target
.weakImport
= false;
3019 template <typename A
>
3020 void Parser
<A
>::addDtraceExtraInfos(const SourceLocation
& src
, const char* providerName
)
3022 // for every ___dtrace_stability$* and ___dtrace_typedefs$* undefine with
3023 // a matching provider name, add a by-name kDtraceTypeReference at probe site
3024 const char* dollar
= strchr(providerName
, '$');
3025 if ( dollar
!= NULL
) {
3026 int providerNameLen
= dollar
-providerName
+1;
3027 for ( std::vector
<const char*>::iterator it
= _dtraceProviderInfo
.begin(); it
!= _dtraceProviderInfo
.end(); ++it
) {
3028 const char* typeDollar
= strchr(*it
, '$');
3029 if ( typeDollar
!= NULL
) {
3030 if ( strncmp(typeDollar
+1, providerName
, providerNameLen
) == 0 ) {
3031 addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindDtraceExtra
,false, *it
);
3038 template <typename A
>
3039 const char* Parser
<A
>::scanSymbolTableForAddress(uint64_t addr
)
3041 uint64_t closestSymAddr
= 0;
3042 const char* closestSymName
= NULL
;
3043 for (uint32_t i
=0; i
< this->_symbolCount
; ++i
) {
3044 const macho_nlist
<P
>& sym
= symbolFromIndex(i
);
3046 if ( (sym
.n_type() & N_STAB
) != 0 )
3049 // only look at definitions
3050 if ( (sym
.n_type() & N_TYPE
) != N_SECT
)
3053 // return with exact match
3054 if ( sym
.n_value() == addr
) {
3055 const char* name
= nameFromSymbol(sym
);
3056 if ( strncmp(name
, "ltmp", 4) != 0 )
3058 // treat 'ltmp*' labels as close match
3059 closestSymAddr
= sym
.n_value();
3060 closestSymName
= name
;
3063 // record closest seen so far
3064 if ( (sym
.n_value() < addr
) && ((sym
.n_value() > closestSymAddr
) || (closestSymName
== NULL
)) )
3065 closestSymName
= nameFromSymbol(sym
);
3068 return (closestSymName
!= NULL
) ? closestSymName
: "unknown";
3072 template <typename A
>
3073 void Parser
<A
>::addFixups(const SourceLocation
& src
, ld::Fixup::Kind setKind
, const TargetDesc
& target
)
3075 // some fixup pairs can be combined
3076 ld::Fixup::Cluster cl
= ld::Fixup::k1of3
;
3077 ld::Fixup::Kind firstKind
= ld::Fixup::kindSetTargetAddress
;
3078 bool combined
= false;
3080 #if SUPPORT_ARCH_arm64e
3081 bool isAuthenticated
= setKind
== ld::Fixup::kindStoreLittleEndianAuth64
;
3082 // Authenticated pointers need an extra fixup for the auth data.
3083 if (isAuthenticated
)
3084 cl
= ld::Fixup::k2of4
;
3086 if ( target
.addend
== 0 ) {
3087 cl
= ld::Fixup::k1of1
;
3089 switch ( setKind
) {
3090 case ld::Fixup::kindStoreLittleEndian32
:
3091 firstKind
= ld::Fixup::kindStoreTargetAddressLittleEndian32
;
3093 case ld::Fixup::kindStoreLittleEndian64
:
3094 firstKind
= ld::Fixup::kindStoreTargetAddressLittleEndian64
;
3096 case ld::Fixup::kindStoreBigEndian32
:
3097 firstKind
= ld::Fixup::kindStoreTargetAddressBigEndian32
;
3099 case ld::Fixup::kindStoreBigEndian64
:
3100 firstKind
= ld::Fixup::kindStoreTargetAddressBigEndian64
;
3102 case ld::Fixup::kindStoreX86BranchPCRel32
:
3103 firstKind
= ld::Fixup::kindStoreTargetAddressX86BranchPCRel32
;
3105 case ld::Fixup::kindStoreX86PCRel32
:
3106 firstKind
= ld::Fixup::kindStoreTargetAddressX86PCRel32
;
3108 case ld::Fixup::kindStoreX86PCRel32GOTLoad
:
3109 firstKind
= ld::Fixup::kindStoreTargetAddressX86PCRel32GOTLoad
;
3111 case ld::Fixup::kindStoreX86PCRel32TLVLoad
:
3112 firstKind
= ld::Fixup::kindStoreTargetAddressX86PCRel32TLVLoad
;
3114 case ld::Fixup::kindStoreX86Abs32TLVLoad
:
3115 firstKind
= ld::Fixup::kindStoreTargetAddressX86Abs32TLVLoad
;
3117 case ld::Fixup::kindStoreARMBranch24
:
3118 firstKind
= ld::Fixup::kindStoreTargetAddressARMBranch24
;
3120 case ld::Fixup::kindStoreThumbBranch22
:
3121 firstKind
= ld::Fixup::kindStoreTargetAddressThumbBranch22
;
3123 #if SUPPORT_ARCH_arm64
3124 case ld::Fixup::kindStoreARM64Branch26
:
3125 firstKind
= ld::Fixup::kindStoreTargetAddressARM64Branch26
;
3127 case ld::Fixup::kindStoreARM64Page21
:
3128 firstKind
= ld::Fixup::kindStoreTargetAddressARM64Page21
;
3130 case ld::Fixup::kindStoreARM64PageOff12
:
3131 firstKind
= ld::Fixup::kindStoreTargetAddressARM64PageOff12
;
3133 case ld::Fixup::kindStoreARM64GOTLoadPage21
:
3134 firstKind
= ld::Fixup::kindStoreTargetAddressARM64GOTLoadPage21
;
3136 case ld::Fixup::kindStoreARM64GOTLoadPageOff12
:
3137 firstKind
= ld::Fixup::kindStoreTargetAddressARM64GOTLoadPageOff12
;
3139 case ld::Fixup::kindStoreARM64TLVPLoadPage21
:
3140 firstKind
= ld::Fixup::kindStoreTargetAddressARM64TLVPLoadPage21
;
3142 case ld::Fixup::kindStoreARM64TLVPLoadPageOff12
:
3143 firstKind
= ld::Fixup::kindStoreTargetAddressARM64TLVPLoadPageOff12
;
3146 #if SUPPORT_ARCH_arm64e
3147 case ld::Fixup::kindStoreLittleEndianAuth64
:
3148 firstKind
= ld::Fixup::kindStoreTargetAddressLittleEndianAuth64
;
3149 cl
= ld::Fixup::k2of2
;
3154 cl
= ld::Fixup::k1of2
;
3159 #if SUPPORT_ARCH_arm64e
3160 // As the auth data is independent of the addend and target, we can just always
3162 if (isAuthenticated
) {
3163 if (cl
== ld::Fixup::k2of2
) {
3164 addFixup(src
, ld::Fixup::k1of2
, ld::Fixup::kindSetAuthData
, target
.authData
);
3166 assert(cl
== ld::Fixup::k2of4
);
3167 addFixup(src
, ld::Fixup::k1of4
, ld::Fixup::kindSetAuthData
, target
.authData
);
3172 if ( target
.atom
!= NULL
) {
3173 if ( target
.atom
->scope() == ld::Atom::scopeTranslationUnit
) {
3174 addFixup(src
, cl
, firstKind
, target
.atom
);
3176 else if ( (target
.atom
->combine() == ld::Atom::combineByNameAndContent
) || (target
.atom
->combine() == ld::Atom::combineByNameAndReferences
) ) {
3177 addFixup(src
, cl
, firstKind
, ld::Fixup::bindingByContentBound
, target
.atom
);
3179 else if ( (src
.atom
->section().type() == ld::Section::typeCFString
) && (src
.offsetInAtom
!= 0) ) {
3180 // backing string in CFStrings should always be direct
3181 addFixup(src
, cl
, firstKind
, target
.atom
);
3183 else if ( (src
.atom
== target
.atom
) && (target
.atom
->combine() == ld::Atom::combineByName
) ) {
3184 // reference to self should always be direct
3185 addFixup(src
, cl
, firstKind
, target
.atom
);
3188 // change direct fixup to by-name fixup
3189 addFixup(src
, cl
, firstKind
, false, target
.atom
->name());
3193 addFixup(src
, cl
, firstKind
, target
.weakImport
, target
.name
);
3195 if ( target
.addend
== 0 ) {
3196 #if SUPPORT_ARCH_arm64e
3197 if (isAuthenticated
)
3201 addFixup(src
, ld::Fixup::k2of2
, setKind
);
3204 #if SUPPORT_ARCH_arm64e
3205 if (isAuthenticated
) {
3206 addFixup(src
, ld::Fixup::k3of4
, ld::Fixup::kindAddAddend
, target
.addend
);
3207 addFixup(src
, ld::Fixup::k4of4
, setKind
);
3211 addFixup(src
, ld::Fixup::k2of3
, ld::Fixup::kindAddAddend
, target
.addend
);
3212 addFixup(src
, ld::Fixup::k3of3
, setKind
);
3217 template <typename A
>
3218 void Parser
<A
>::addFixups(const SourceLocation
& src
, ld::Fixup::Kind kind
, const TargetDesc
& target
, const TargetDesc
& picBase
)
3220 ld::Fixup::Cluster cl
= (target
.addend
== 0) ? ld::Fixup::k1of4
: ld::Fixup::k1of5
;
3221 if ( target
.atom
!= NULL
) {
3222 if ( target
.atom
->scope() == ld::Atom::scopeTranslationUnit
) {
3223 addFixup(src
, cl
, ld::Fixup::kindSetTargetAddress
, target
.atom
);
3225 else if ( (target
.atom
->combine() == ld::Atom::combineByNameAndContent
) || (target
.atom
->combine() == ld::Atom::combineByNameAndReferences
) ) {
3226 addFixup(src
, cl
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, target
.atom
);
3229 addFixup(src
, cl
, ld::Fixup::kindSetTargetAddress
, false, target
.atom
->name());
3233 addFixup(src
, cl
, ld::Fixup::kindSetTargetAddress
, target
.weakImport
, target
.name
);
3235 if ( target
.addend
== 0 ) {
3236 assert(picBase
.atom
!= NULL
);
3237 addFixup(src
, ld::Fixup::k2of4
, ld::Fixup::kindSubtractTargetAddress
, picBase
.atom
);
3238 addFixup(src
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractAddend
, picBase
.addend
);
3239 addFixup(src
, ld::Fixup::k4of4
, kind
);
3242 addFixup(src
, ld::Fixup::k2of5
, ld::Fixup::kindAddAddend
, target
.addend
);
3243 addFixup(src
, ld::Fixup::k3of5
, ld::Fixup::kindSubtractTargetAddress
, picBase
.atom
);
3244 addFixup(src
, ld::Fixup::k4of5
, ld::Fixup::kindSubtractAddend
, picBase
.addend
);
3245 addFixup(src
, ld::Fixup::k5of5
, kind
);
3251 template <typename A
>
3252 uint32_t TentativeDefinitionSection
<A
>::computeAtomCount(class Parser
<A
>& parser
,
3253 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
3254 const struct Parser
<A
>::CFI_CU_InfoArrays
&)
3256 return parser
.tentativeDefinitionCount();
3259 template <typename A
>
3260 uint32_t TentativeDefinitionSection
<A
>::appendAtoms(class Parser
<A
>& parser
, uint8_t* p
,
3261 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
3262 const struct Parser
<A
>::CFI_CU_InfoArrays
&)
3264 this->_beginAtoms
= (Atom
<A
>*)p
;
3266 for (uint32_t i
=parser
.undefinedStartIndex(); i
< parser
.undefinedEndIndex(); ++i
) {
3267 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(i
);
3268 if ( ((sym
.n_type() & N_TYPE
) == N_UNDF
) && (sym
.n_value() != 0) ) {
3269 uint64_t size
= sym
.n_value();
3270 uint8_t alignP2
= GET_COMM_ALIGN(sym
.n_desc());
3271 if ( alignP2
== 0 ) {
3272 // common symbols align to their size
3273 // that is, a 4-byte common aligns to 4-bytes
3274 // if this size is not a power of two,
3275 // then round up to the next power of two
3276 alignP2
= 63 - (uint8_t)__builtin_clzll(size
);
3277 if ( size
!= (1ULL << alignP2
) )
3279 // <rdar://problem/24871389> limit default alignment of large commons
3280 if ( alignP2
> parser
.maxDefaultCommonAlignment() )
3281 alignP2
= parser
.maxDefaultCommonAlignment();
3283 Atom
<A
>* allocatedSpace
= (Atom
<A
>*)p
;
3284 new (allocatedSpace
) Atom
<A
>(*this, parser
.nameFromSymbol(sym
), (pint_t
)ULLONG_MAX
, size
,
3285 ld::Atom::definitionTentative
, ld::Atom::combineByName
,
3286 parser
.scopeFromSymbol(sym
), ld::Atom::typeZeroFill
, ld::Atom::symbolTableIn
,
3287 parser
.dontDeadStripFromSymbol(sym
), false, false, ld::Atom::Alignment(alignP2
) );
3288 p
+= sizeof(Atom
<A
>);
3292 this->_endAtoms
= (Atom
<A
>*)p
;
3297 template <typename A
>
3298 uint32_t AbsoluteSymbolSection
<A
>::computeAtomCount(class Parser
<A
>& parser
,
3299 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
3300 const struct Parser
<A
>::CFI_CU_InfoArrays
&)
3302 return parser
.absoluteSymbolCount();
3305 template <typename A
>
3306 uint32_t AbsoluteSymbolSection
<A
>::appendAtoms(class Parser
<A
>& parser
, uint8_t* p
,
3307 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
3308 const struct Parser
<A
>::CFI_CU_InfoArrays
&)
3310 this->_beginAtoms
= (Atom
<A
>*)p
;
3312 for (uint32_t i
=0; i
< parser
.symbolCount(); ++i
) {
3313 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(i
);
3314 if ( (sym
.n_type() & N_TYPE
) != N_ABS
)
3316 const char* absName
= parser
.nameFromSymbol(sym
);
3317 // ignore .objc_class_name_* symbols
3318 if ( strncmp(absName
, ".objc_class_name_", 17) == 0 )
3320 // ignore .objc_class_name_* symbols
3321 if ( strncmp(absName
, ".objc_category_name_", 20) == 0 )
3323 // ignore empty *.eh symbols
3324 if ( strcmp(&absName
[strlen(absName
)-3], ".eh") == 0 )
3327 Atom
<A
>* allocatedSpace
= (Atom
<A
>*)p
;
3328 new (allocatedSpace
) Atom
<A
>(*this, parser
, sym
, 0);
3329 p
+= sizeof(Atom
<A
>);
3332 this->_endAtoms
= (Atom
<A
>*)p
;
3336 template <typename A
>
3337 Atom
<A
>* AbsoluteSymbolSection
<A
>::findAbsAtomForValue(typename
A::P::uint_t value
)
3339 Atom
<A
>* end
= this->_endAtoms
;
3340 for(Atom
<A
>* p
= this->_beginAtoms
; p
< end
; ++p
) {
3341 if ( p
->_objAddress
== value
)
3348 template <typename A
>
3349 uint32_t Parser
<A
>::indirectSymbol(uint32_t indirectIndex
)
3351 if ( indirectIndex
>= _indirectTableCount
)
3352 throw "indirect symbol index out of range";
3353 return E::get32(_indirectTable
[indirectIndex
]);
3356 template <typename A
>
3357 const macho_nlist
<typename
A::P
>& Parser
<A
>::symbolFromIndex(uint32_t index
)
3359 if ( index
> _symbolCount
)
3360 throw "symbol index out of range";
3361 return _symbols
[index
];
3364 template <typename A
>
3365 const macho_section
<typename
A::P
>* Parser
<A
>::machOSectionFromSectionIndex(uint32_t index
)
3367 if ( index
>= _machOSectionsCount
)
3368 throw "section index out of range";
3369 return &_sectionsStart
[index
];
3372 template <typename A
>
3373 uint32_t Parser
<A
>::symbolIndexFromIndirectSectionAddress(pint_t addr
, const macho_section
<P
>* sect
)
3375 uint32_t elementSize
= 0;
3376 switch ( sect
->flags() & SECTION_TYPE
) {
3377 case S_SYMBOL_STUBS
:
3378 elementSize
= sect
->reserved2();
3380 case S_LAZY_SYMBOL_POINTERS
:
3381 case S_NON_LAZY_SYMBOL_POINTERS
:
3382 case S_THREAD_LOCAL_VARIABLE_POINTERS
:
3383 elementSize
= sizeof(pint_t
);
3386 throw "section does not use indirect symbol table";
3388 uint32_t indexInSection
= (addr
- sect
->addr()) / elementSize
;
3389 uint32_t indexIntoIndirectTable
= sect
->reserved1() + indexInSection
;
3390 return this->indirectSymbol(indexIntoIndirectTable
);
3395 template <typename A
>
3396 const char* Parser
<A
>::nameFromSymbol(const macho_nlist
<P
>& sym
)
3398 return &_strings
[sym
.n_strx()];
3401 template <typename A
>
3402 ld::Atom::Scope Parser
<A
>::scopeFromSymbol(const macho_nlist
<P
>& sym
)
3404 if ( (sym
.n_type() & N_EXT
) == 0 )
3405 return ld::Atom::scopeTranslationUnit
;
3406 else if ( (sym
.n_type() & N_PEXT
) != 0 )
3407 return ld::Atom::scopeLinkageUnit
;
3408 else if ( this->nameFromSymbol(sym
)[0] == 'l' ) // since all 'l' symbols will be remove, don't make them global
3409 return ld::Atom::scopeLinkageUnit
;
3411 return ld::Atom::scopeGlobal
;
3414 template <typename A
>
3415 ld::Atom::Definition Parser
<A
>::definitionFromSymbol(const macho_nlist
<P
>& sym
)
3417 switch ( sym
.n_type() & N_TYPE
) {
3419 return ld::Atom::definitionAbsolute
;
3421 return ld::Atom::definitionRegular
;
3423 if ( sym
.n_value() != 0 )
3424 return ld::Atom::definitionTentative
;
3426 throw "definitionFromSymbol() bad symbol";
3429 template <typename A
>
3430 ld::Atom::Combine Parser
<A
>::combineFromSymbol(const macho_nlist
<P
>& sym
)
3432 if ( sym
.n_desc() & N_WEAK_DEF
)
3433 return ld::Atom::combineByName
;
3435 return ld::Atom::combineNever
;
3439 template <typename A
>
3440 ld::Atom::SymbolTableInclusion Parser
<A
>::inclusionFromSymbol(const macho_nlist
<P
>& sym
)
3442 const char* symbolName
= nameFromSymbol(sym
);
3443 // labels beginning with 'l' (lowercase ell) are automatically removed in final linked images <rdar://problem/4571042>
3444 // labels beginning with 'L' should have been stripped by the assembler, so are stripped now
3445 if ( sym
.n_desc() & REFERENCED_DYNAMICALLY
)
3446 return ld::Atom::symbolTableInAndNeverStrip
;
3447 else if ( symbolName
[0] == 'l' )
3448 return ld::Atom::symbolTableNotInFinalLinkedImages
;
3449 else if ( symbolName
[0] == 'L' )
3450 return ld::Atom::symbolTableNotIn
;
3452 return ld::Atom::symbolTableIn
;
3455 template <typename A
>
3456 bool Parser
<A
>::dontDeadStripFromSymbol(const macho_nlist
<P
>& sym
)
3458 return ( (sym
.n_desc() & (N_NO_DEAD_STRIP
|REFERENCED_DYNAMICALLY
)) != 0 );
3461 template <typename A
>
3462 bool Parser
<A
>::isThumbFromSymbol(const macho_nlist
<P
>& sym
)
3464 return ( sym
.n_desc() & N_ARM_THUMB_DEF
);
3467 template <typename A
>
3468 bool Parser
<A
>::weakImportFromSymbol(const macho_nlist
<P
>& sym
)
3470 return ( ((sym
.n_type() & N_TYPE
) == N_UNDF
) && ((sym
.n_desc() & N_WEAK_REF
) != 0) );
3473 template <typename A
>
3474 bool Parser
<A
>::resolverFromSymbol(const macho_nlist
<P
>& sym
)
3476 return ( sym
.n_desc() & N_SYMBOL_RESOLVER
);
3479 template <typename A
>
3480 bool Parser
<A
>::altEntryFromSymbol(const macho_nlist
<P
>& sym
)
3482 return ( sym
.n_desc() & N_ALT_ENTRY
);
3486 /* Skip over a LEB128 value (signed or unsigned). */
3488 skip_leb128 (const uint8_t ** offset
, const uint8_t * end
)
3490 while (*offset
!= end
&& **offset
>= 0x80)
3496 /* Read a ULEB128 into a 64-bit word. Return (uint64_t)-1 on overflow
3497 or error. On overflow, skip past the rest of the uleb128. */
3499 read_uleb128 (const uint8_t ** offset
, const uint8_t * end
)
3501 uint64_t result
= 0;
3508 return (uint64_t) -1;
3510 b
= **offset
& 0x7f;
3512 if (bit
>= 64 || b
<< bit
>> bit
!= b
)
3513 result
= (uint64_t) -1;
3515 result
|= b
<< bit
, bit
+= 7;
3516 } while (*(*offset
)++ >= 0x80);
3521 /* Skip over a DWARF attribute of form FORM. */
3522 template <typename A
>
3523 bool Parser
<A
>::skip_form(const uint8_t ** offset
, const uint8_t * end
, uint64_t form
,
3524 uint8_t addr_size
, bool dwarf64
)
3534 case DW_FORM_block2
:
3535 if (end
- *offset
< 2)
3537 sz
= 2 + A::P::E::get16(*(uint16_t*)offset
);
3540 case DW_FORM_block4
:
3541 if (end
- *offset
< 4)
3543 sz
= 2 + A::P::E::get32(*(uint32_t*)offset
);
3561 case DW_FORM_string
:
3562 while (*offset
!= end
&& **offset
)
3571 sz
= read_uleb128 (offset
, end
);
3574 case DW_FORM_block1
:
3582 case DW_FORM_ref_udata
:
3583 skip_leb128 (offset
, end
);
3587 case DW_FORM_ref_addr
:
3591 case DW_FORM_sec_offset
:
3592 sz
= sizeof(typename
A::P::uint_t
);
3595 case DW_FORM_exprloc
:
3596 sz
= read_uleb128 (offset
, end
);
3599 case DW_FORM_flag_present
:
3603 case DW_FORM_ref_sig8
:
3610 if (end
- *offset
< sz
)
3617 template <typename A
>
3618 const char* Parser
<A
>::getDwarfString(uint64_t form
, const uint8_t*& di
)
3621 const char* dwarfStrings
;
3622 const char* result
= NULL
;
3624 case DW_FORM_string
:
3625 result
= (const char*)di
;
3626 di
+= strlen(result
) + 1;
3629 offset
= E::get32(*((uint32_t*)di
));
3630 dwarfStrings
= (char*)_file
->fileContent() + _file
->_dwarfDebugStringSect
->offset();
3631 if ( offset
< _file
->_dwarfDebugStringSect
->size() )
3632 result
= &dwarfStrings
[offset
];
3634 warning("dwarf DW_FORM_strp (offset=0x%08X) is too big in %s", offset
, this->_path
);
3638 warning("unknown dwarf string encoding (form=%lld) in %s", form
, this->_path
);
3644 template <typename A
>
3645 uint64_t Parser
<A
>::getDwarfOffset(uint64_t form
, const uint8_t*& di
, bool dwarf64
)
3647 if ( form
== DW_FORM_sec_offset
)
3648 form
= (dwarf64
? DW_FORM_data8
: DW_FORM_data4
);
3649 uint64_t result
= -1;
3652 result
= A::P::E::get32(*(uint32_t*)di
);
3656 result
= A::P::E::get64(*(uint64_t*)di
);
3660 warning("unknown dwarf DW_FORM_ for DW_AT_stmt_list in %s", this->_path
);
3666 template <typename A
>
3667 struct AtomAndLineInfo
{
3669 ld::Atom::LineInfo info
;
3673 // <rdar://problem/5591394> Add support to ld64 for N_FUN stabs when used for symbolic constants
3674 // Returns whether a stabStr belonging to an N_FUN stab represents a
3675 // symbolic constant rather than a function
3676 template <typename A
>
3677 bool Parser
<A
>::isConstFunStabs(const char *stabStr
)
3680 // N_FUN can be used for both constants and for functions. In case it's a constant,
3681 // the format of the stabs string is "symname:c=<value>;"
3682 // ':' cannot appear in the symbol name, except if it's an Objective-C method
3683 // (in which case the symbol name starts with + or -, and then it's definitely
3685 return (stabStr
!= NULL
) && (stabStr
[0] != '+') && (stabStr
[0] != '-')
3686 && ((colon
= strchr(stabStr
, ':')) != NULL
)
3687 && (colon
[1] == 'c') && (colon
[2] == '=');
3691 template <typename A
>
3692 void Parser
<A
>::parseDebugInfo()
3696 // check for dwarf __debug_info section
3697 if ( _file
->_dwarfDebugInfoSect
== NULL
) {
3698 // if no DWARF debug info, look for stabs
3702 if ( _file
->_dwarfDebugInfoSect
->size() == 0 )
3708 if ( !read_comp_unit(&tuName
, &tuDir
, &stmtList
) ) {
3709 // if can't parse dwarf, warn and give up
3710 _file
->_dwarfTranslationUnitPath
= NULL
;
3711 warning("can't parse dwarf compilation unit info in %s", _path
);
3712 _file
->_debugInfoKind
= ld::relocatable::File::kDebugInfoNone
;
3715 if ( (tuName
!= NULL
) && (tuName
[0] == '/') ) {
3716 _file
->_dwarfTranslationUnitPath
= tuName
;
3718 else if ( (tuDir
!= NULL
) && (tuName
!= NULL
) ) {
3719 asprintf((char**)&(_file
->_dwarfTranslationUnitPath
), "%s/%s", tuDir
, tuName
);
3721 else if ( tuDir
== NULL
) {
3722 _file
->_dwarfTranslationUnitPath
= tuName
;
3725 _file
->_dwarfTranslationUnitPath
= NULL
;
3728 // add line number info to atoms from dwarf
3729 std::vector
<AtomAndLineInfo
<A
> > entries
;
3730 entries
.reserve(64);
3731 if ( _file
->_debugInfoKind
== ld::relocatable::File::kDebugInfoDwarf
) {
3732 // file with just data will have no __debug_line info
3733 if ( (_file
->_dwarfDebugLineSect
!= NULL
) && (_file
->_dwarfDebugLineSect
->size() != 0) ) {
3734 // validate stmt_list
3735 if ( (stmtList
!= (uint64_t)-1) && (stmtList
< _file
->_dwarfDebugLineSect
->size()) ) {
3736 const uint8_t* debug_line
= (uint8_t*)_file
->fileContent() + _file
->_dwarfDebugLineSect
->offset();
3737 struct line_reader_data
* lines
= line_open(&debug_line
[stmtList
],
3738 _file
->_dwarfDebugLineSect
->size() - stmtList
, E::little_endian
);
3739 struct line_info result
;
3740 Atom
<A
>* curAtom
= NULL
;
3741 uint32_t curAtomOffset
= 0;
3742 uint32_t curAtomAddress
= 0;
3743 uint32_t curAtomSize
= 0;
3744 std::map
<uint32_t,const char*> dwarfIndexToFile
;
3745 if ( lines
!= NULL
) {
3746 while ( line_next(lines
, &result
, line_stop_pc
) ) {
3747 //fprintf(stderr, "curAtom=%p, result.pc=0x%llX, result.line=%llu, result.end_of_sequence=%d,"
3748 // " curAtomAddress=0x%X, curAtomSize=0x%X\n",
3749 // curAtom, result.pc, result.line, result.end_of_sequence, curAtomAddress, curAtomSize);
3750 // work around weird debug line table compiler generates if no functions in __text section
3751 if ( (curAtom
== NULL
) && (result
.pc
== 0) && result
.end_of_sequence
&& (result
.file
== 1))
3753 // for performance, see if in next pc is in current atom
3754 if ( (curAtom
!= NULL
) && (curAtomAddress
<= result
.pc
) && (result
.pc
< (curAtomAddress
+curAtomSize
)) ) {
3755 curAtomOffset
= result
.pc
- curAtomAddress
;
3757 // or pc at end of current atom
3758 else if ( result
.end_of_sequence
&& (curAtom
!= NULL
) && (result
.pc
== (curAtomAddress
+curAtomSize
)) ) {
3759 curAtomOffset
= result
.pc
- curAtomAddress
;
3761 // or only one function that is a one line function
3762 else if ( result
.end_of_sequence
&& (curAtom
== NULL
) && (this->findAtomByAddress(0) != NULL
) && (result
.pc
== this->findAtomByAddress(0)->size()) ) {
3763 curAtom
= this->findAtomByAddress(0);
3764 curAtomOffset
= result
.pc
- curAtom
->objectAddress();
3765 curAtomAddress
= curAtom
->objectAddress();
3766 curAtomSize
= curAtom
->size();
3769 // do slow look up of atom by address
3771 curAtom
= this->findAtomByAddress(result
.pc
);
3774 // in case of bug in debug info, don't abort link, just limp on
3777 if ( curAtom
== NULL
)
3778 break; // file has line info but no functions
3779 if ( result
.end_of_sequence
&& (curAtomAddress
+curAtomSize
< result
.pc
) ) {
3780 // a one line function can be returned by line_next() as one entry with pc at end of blob
3781 // look for alt atom starting at end of previous atom
3782 uint32_t previousEnd
= curAtomAddress
+curAtomSize
;
3783 Atom
<A
>* alt
= this->findAtomByAddressOrNullIfStub(previousEnd
);
3785 continue; // ignore spurious debug info for stubs
3786 if ( result
.pc
<= alt
->objectAddress() + alt
->size() ) {
3788 curAtomOffset
= result
.pc
- alt
->objectAddress();
3789 curAtomAddress
= alt
->objectAddress();
3790 curAtomSize
= alt
->size();
3793 curAtomOffset
= result
.pc
- curAtom
->objectAddress();
3794 curAtomAddress
= curAtom
->objectAddress();
3795 curAtomSize
= curAtom
->size();
3799 curAtomOffset
= result
.pc
- curAtom
->objectAddress();
3800 curAtomAddress
= curAtom
->objectAddress();
3801 curAtomSize
= curAtom
->size();
3804 const char* filename
;
3805 std::map
<uint32_t,const char*>::iterator pos
= dwarfIndexToFile
.find(result
.file
);
3806 if ( pos
== dwarfIndexToFile
.end() ) {
3807 filename
= line_file(lines
, result
.file
);
3808 dwarfIndexToFile
[result
.file
] = filename
;
3811 filename
= pos
->second
;
3813 // only record for ~8000 line info records per function
3814 if ( curAtom
->roomForMoreLineInfoCount() ) {
3815 AtomAndLineInfo
<A
> entry
;
3816 entry
.atom
= curAtom
;
3817 entry
.info
.atomOffset
= curAtomOffset
;
3818 entry
.info
.fileName
= filename
;
3819 entry
.info
.lineNumber
= result
.line
;
3820 //fprintf(stderr, "addr=0x%08llX, line=%lld, file=%s, atom=%s, atom.size=0x%X, end=%d\n",
3821 // result.pc, result.line, filename, curAtom->name(), curAtomSize, result.end_of_sequence);
3822 entries
.push_back(entry
);
3823 curAtom
->incrementLineInfoCount();
3825 if ( result
.end_of_sequence
) {
3835 // assign line info start offset for each atom
3836 uint8_t* p
= _file
->_atomsArray
;
3837 uint32_t liOffset
= 0;
3838 for(int i
=_file
->_atomsArrayCount
; i
> 0; --i
) {
3839 Atom
<A
>* atom
= (Atom
<A
>*)p
;
3840 atom
->_lineInfoStartIndex
= liOffset
;
3841 liOffset
+= atom
->_lineInfoCount
;
3842 atom
->_lineInfoCount
= 0;
3843 p
+= sizeof(Atom
<A
>);
3845 assert(liOffset
== entries
.size());
3846 _file
->_lineInfos
.resize(liOffset
);
3848 // copy each line info for each atom
3849 for (typename
std::vector
<AtomAndLineInfo
<A
> >::iterator it
= entries
.begin(); it
!= entries
.end(); ++it
) {
3850 uint32_t slot
= it
->atom
->_lineInfoStartIndex
+ it
->atom
->_lineInfoCount
;
3851 _file
->_lineInfos
[slot
] = it
->info
;
3852 it
->atom
->_lineInfoCount
++;
3855 // done with temp vector
3859 template <typename A
>
3860 void Parser
<A
>::parseStabs()
3862 // scan symbol table for stabs entries
3863 Atom
<A
>* currentAtom
= NULL
;
3864 pint_t currentAtomAddress
= 0;
3865 enum { start
, inBeginEnd
, inFun
} state
= start
;
3866 for (uint32_t symbolIndex
= 0; symbolIndex
< _symbolCount
; ++symbolIndex
) {
3867 const macho_nlist
<P
>& sym
= this->symbolFromIndex(symbolIndex
);
3868 bool useStab
= true;
3869 uint8_t type
= sym
.n_type();
3870 const char* symString
= (sym
.n_strx() != 0) ? this->nameFromSymbol(sym
) : NULL
;
3871 if ( (type
& N_STAB
) != 0 ) {
3872 _file
->_debugInfoKind
= (_hasUUID
? ld::relocatable::File::kDebugInfoStabsUUID
: ld::relocatable::File::kDebugInfoStabs
);
3873 ld::relocatable::File::Stab stab
;
3876 stab
.other
= sym
.n_sect();
3877 stab
.desc
= sym
.n_desc();
3878 stab
.value
= sym
.n_value();
3884 // beginning of function block
3886 // fall into case to lookup atom by addresss
3889 currentAtomAddress
= sym
.n_value();
3890 currentAtom
= this->findAtomByAddress(currentAtomAddress
);
3891 if ( currentAtom
!= NULL
) {
3892 stab
.atom
= currentAtom
;
3893 stab
.string
= symString
;
3896 fprintf(stderr
, "can't find atom for stabs BNSYM at %08llX in %s",
3897 (uint64_t)sym
.n_value(), _path
);
3907 // not associated with an atom, just copy
3908 stab
.string
= symString
;
3912 // n_value field is NOT atom address ;-(
3913 // need to find atom by name match
3914 const char* colon
= strchr(symString
, ':');
3915 if ( colon
!= NULL
) {
3916 // build underscore leading name
3917 int nameLen
= colon
- symString
;
3918 char symName
[nameLen
+2];
3919 strlcpy(&symName
[1], symString
, nameLen
+1);
3921 symName
[nameLen
+1] = '\0';
3922 currentAtom
= this->findAtomByName(symName
);
3923 if ( currentAtom
!= NULL
) {
3924 stab
.atom
= currentAtom
;
3925 stab
.string
= symString
;
3929 // might be a debug-note without trailing :G()
3930 currentAtom
= this->findAtomByName(symString
);
3931 if ( currentAtom
!= NULL
) {
3932 stab
.atom
= currentAtom
;
3933 stab
.string
= symString
;
3936 if ( stab
.atom
== NULL
) {
3937 // ld_classic added bogus GSYM stabs for old style dtrace probes
3938 if ( (strncmp(symString
, "__dtrace_probe$", 15) != 0) )
3939 warning("can't find atom for N_GSYM stabs %s in %s", symString
, _path
);
3945 if ( isConstFunStabs(symString
) ) {
3946 // constant not associated with a function
3947 stab
.string
= symString
;
3950 // old style stabs without BNSYM
3952 currentAtomAddress
= sym
.n_value();
3953 currentAtom
= this->findAtomByAddress(currentAtomAddress
);
3954 if ( currentAtom
!= NULL
) {
3955 stab
.atom
= currentAtom
;
3956 stab
.string
= symString
;
3959 warning("can't find atom for stabs FUN at %08llX in %s",
3960 (uint64_t)currentAtomAddress
, _path
);
3966 stab
.string
= symString
;
3972 stab
.string
= symString
;
3973 // -gfull built .o file
3976 warning("unknown stabs type 0x%X in %s", type
, _path
);
3980 stab
.atom
= currentAtom
;
3989 Atom
<A
>* nestedAtom
= this->findAtomByAddress(sym
.n_value());
3990 if ( nestedAtom
!= NULL
) {
3991 stab
.atom
= nestedAtom
;
3992 stab
.string
= symString
;
3995 warning("can't find atom for stabs 0x%X at %08llX in %s",
3996 type
, (uint64_t)sym
.n_value(), _path
);
4003 // adjust value to be offset in atom
4004 stab
.value
-= currentAtomAddress
;
4006 stab
.string
= symString
;
4013 if ( isConstFunStabs(symString
) ) {
4014 stab
.atom
= currentAtom
;
4015 stab
.string
= symString
;
4018 if ( sym
.n_sect() != 0 ) {
4019 // found another start stab, must be really old stabs...
4020 currentAtomAddress
= sym
.n_value();
4021 currentAtom
= this->findAtomByAddress(currentAtomAddress
);
4022 if ( currentAtom
!= NULL
) {
4023 stab
.atom
= currentAtom
;
4024 stab
.string
= symString
;
4027 warning("can't find atom for stabs FUN at %08llX in %s",
4028 (uint64_t)currentAtomAddress
, _path
);
4032 // found ending stab, switch back to start state
4033 stab
.string
= symString
;
4034 stab
.atom
= currentAtom
;
4043 // adjust value to be offset in atom
4044 stab
.value
-= currentAtomAddress
;
4045 stab
.atom
= currentAtom
;
4048 stab
.string
= symString
;
4052 stab
.atom
= currentAtom
;
4053 stab
.string
= symString
;
4058 // add to list of stabs for this .o file
4060 _file
->_stabs
.push_back(stab
);
4066 template <typename A
>
4067 void Parser
<A
>::addAstFiles()
4069 // scan symbol table for N_AST entries
4070 for (uint32_t symbolIndex
= 0; symbolIndex
< _symbolCount
; ++symbolIndex
) {
4071 const macho_nlist
<P
>& sym
= this->symbolFromIndex(symbolIndex
);
4072 if ( (sym
.n_type() == N_AST
) && (sym
.n_strx() != 0) ) {
4073 const char* symString
= this->nameFromSymbol(sym
);
4074 ld::relocatable::File::AstTimeAndPath entry
;
4075 entry
.time
= sym
.n_value();
4076 entry
.path
= symString
;
4077 _file
->_astFiles
.push_back(entry
);
4083 // Look at the compilation unit DIE and determine
4084 // its NAME, compilation directory (in COMP_DIR) and its
4085 // line number information offset (in STMT_LIST). NAME and COMP_DIR
4086 // may be NULL (especially COMP_DIR) if they are not in the .o file;
4087 // STMT_LIST will be (uint64_t) -1.
4089 // At present this assumes that there's only one compilation unit DIE.
4091 template <typename A
>
4092 bool Parser
<A
>::read_comp_unit(const char ** name
, const char ** comp_dir
,
4093 uint64_t *stmt_list
)
4095 const uint8_t * debug_info
;
4096 const uint8_t * debug_abbrev
;
4098 const uint8_t * next_cu
;
4100 const uint8_t * end
;
4101 const uint8_t * enda
;
4104 uint64_t abbrev_base
;
4106 uint8_t address_size
;
4111 *stmt_list
= (uint64_t) -1;
4113 if ( (_file
->_dwarfDebugInfoSect
== NULL
) || (_file
->_dwarfDebugAbbrevSect
== NULL
) )
4116 if (_file
->_dwarfDebugInfoSect
->size() < 12)
4117 /* Too small to be a real debug_info section. */
4120 debug_info
= (uint8_t*)_file
->fileContent() + _file
->_dwarfDebugInfoSect
->offset();
4121 debug_abbrev
= (uint8_t*)_file
->fileContent() + _file
->_dwarfDebugAbbrevSect
->offset();
4122 next_cu
= debug_info
;
4124 while ((uint64_t)(next_cu
- debug_info
) < _file
->_dwarfDebugInfoSect
->size()) {
4126 sz
= A::P::E::get32(*(uint32_t*)di
);
4128 dwarf64
= sz
== 0xffffffff;
4130 sz
= A::P::E::get64(*(uint64_t*)di
), di
+= 8;
4131 else if (sz
> 0xffffff00)
4132 /* Unknown dwarf format. */
4135 /* Verify claimed size. */
4136 if (sz
+ (di
- debug_info
) > _file
->_dwarfDebugInfoSect
->size() || sz
<= (dwarf64
? 23 : 11))
4141 vers
= A::P::E::get16(*(uint16_t*)di
);
4142 if (vers
< 2 || vers
> 4)
4143 /* DWARF version wrong for this code.
4144 Chances are we could continue anyway, but we don't know for sure. */
4148 /* Find the debug_abbrev section. */
4149 abbrev_base
= dwarf64
? A::P::E::get64(*(uint64_t*)di
) : A::P::E::get32(*(uint32_t*)di
);
4150 di
+= dwarf64
? 8 : 4;
4152 if (abbrev_base
> _file
->_dwarfDebugAbbrevSect
->size())
4154 da
= debug_abbrev
+ abbrev_base
;
4155 enda
= debug_abbrev
+ _file
->_dwarfDebugAbbrevSect
->size();
4157 address_size
= *di
++;
4159 /* Find the abbrev number we're looking for. */
4161 abbrev
= read_uleb128 (&di
, end
);
4162 if (abbrev
== (uint64_t) -1)
4165 /* Skip through the debug_abbrev section looking for that abbrev. */
4168 uint64_t this_abbrev
= read_uleb128 (&da
, enda
);
4171 if (this_abbrev
== abbrev
)
4172 /* This is almost always taken. */
4174 skip_leb128 (&da
, enda
); /* Skip the tag. */
4177 da
++; /* Skip the DW_CHILDREN_* value. */
4180 attr
= read_uleb128 (&da
, enda
);
4181 skip_leb128 (&da
, enda
);
4182 } while (attr
!= 0 && attr
!= (uint64_t) -1);
4187 /* Check that the abbrev is one for a DW_TAG_compile_unit. */
4188 if (read_uleb128 (&da
, enda
) != DW_TAG_compile_unit
)
4192 da
++; /* Skip the DW_CHILDREN_* value. */
4194 /* Now, go through the DIE looking for DW_AT_name,
4195 DW_AT_comp_dir, and DW_AT_stmt_list. */
4196 bool skip_to_next_cu
= false;
4197 while (!skip_to_next_cu
) {
4199 uint64_t attr
= read_uleb128 (&da
, enda
);
4200 uint64_t form
= read_uleb128 (&da
, enda
);
4202 if (attr
== (uint64_t) -1)
4206 if (form
== DW_FORM_indirect
)
4207 form
= read_uleb128 (&di
, end
);
4211 *name
= getDwarfString(form
, di
);
4212 /* Swift object files may contain two CUs: One
4213 describes the Swift code, one is created by the
4214 clang importer. Skip over the CU created by the
4215 clang importer as it may be empty. */
4216 if (std::string(*name
) == "<swift-imported-modules>")
4217 skip_to_next_cu
= true;
4219 case DW_AT_comp_dir
:
4220 *comp_dir
= getDwarfString(form
, di
);
4222 case DW_AT_stmt_list
:
4223 *stmt_list
= getDwarfOffset(form
, di
, dwarf64
);
4226 if (! skip_form (&di
, end
, form
, address_size
, dwarf64
))
4236 template <typename A
>
4239 free(_sectionsArray
);
4243 template <typename A
>
4244 const char* File
<A
>::translationUnitSource() const
4246 return _dwarfTranslationUnitPath
;
4249 template <typename A
>
4250 bool File
<A
>::forEachAtom(ld::File::AtomHandler
& handler
) const
4252 handler
.doFile(*this);
4253 uint8_t* p
= _atomsArray
;
4254 for(int i
=_atomsArrayCount
; i
> 0; --i
) {
4255 handler
.doAtom(*((Atom
<A
>*)p
));
4256 p
+= sizeof(Atom
<A
>);
4258 p
= _aliasAtomsArray
;
4259 for(int i
=_aliasAtomsArrayCount
; i
> 0; --i
) {
4260 handler
.doAtom(*((AliasAtom
*)p
));
4261 p
+= sizeof(AliasAtom
);
4264 return (_atomsArrayCount
!= 0) || (_aliasAtomsArrayCount
!= 0);
4267 template <typename A
>
4268 const char* Section
<A
>::makeSegmentName(const macho_section
<typename
A::P
>* sect
)
4270 // mach-o section record only has room for 16-byte seg/sect names
4271 // so a 16-byte name has no trailing zero
4272 const char* name
= sect
->segname();
4273 if ( strlen(name
) < 16 )
4275 char* tmp
= new char[17];
4276 strlcpy(tmp
, name
, 17);
4280 template <typename A
>
4281 const char* Section
<A
>::makeSectionName(const macho_section
<typename
A::P
>* sect
)
4283 const char* name
= sect
->sectname();
4284 if ( strlen(name
) < 16 )
4287 // special case common long section names so we don't have to malloc
4288 if ( strncmp(sect
->sectname(), "__objc_classrefs", 16) == 0 )
4289 return "__objc_classrefs";
4290 if ( strncmp(sect
->sectname(), "__objc_classlist", 16) == 0 )
4291 return "__objc_classlist";
4292 if ( strncmp(sect
->sectname(), "__objc_nlclslist", 16) == 0 )
4293 return "__objc_nlclslist";
4294 if ( strncmp(sect
->sectname(), "__objc_nlcatlist", 16) == 0 )
4295 return "__objc_nlcatlist";
4296 if ( strncmp(sect
->sectname(), "__objc_protolist", 16) == 0 )
4297 return "__objc_protolist";
4298 if ( strncmp(sect
->sectname(), "__objc_protorefs", 16) == 0 )
4299 return "__objc_protorefs";
4300 if ( strncmp(sect
->sectname(), "__objc_superrefs", 16) == 0 )
4301 return "__objc_superrefs";
4302 if ( strncmp(sect
->sectname(), "__objc_imageinfo", 16) == 0 )
4303 return "__objc_imageinfo";
4304 if ( strncmp(sect
->sectname(), "__objc_stringobj", 16) == 0 )
4305 return "__objc_stringobj";
4306 if ( strncmp(sect
->sectname(), "__gcc_except_tab", 16) == 0 )
4307 return "__gcc_except_tab";
4309 char* tmp
= new char[17];
4310 strlcpy(tmp
, name
, 17);
4314 template <typename A
>
4315 bool Section
<A
>::readable(const macho_section
<typename
A::P
>* sect
)
4320 template <typename A
>
4321 bool Section
<A
>::writable(const macho_section
<typename
A::P
>* sect
)
4323 // mach-o .o files do not contain segment permissions
4324 // we just know TEXT is special
4325 return ( strcmp(sect
->segname(), "__TEXT") != 0 );
4328 template <typename A
>
4329 bool Section
<A
>::exectuable(const macho_section
<typename
A::P
>* sect
)
4331 // mach-o .o files do not contain segment permissions
4332 // we just know TEXT is special
4333 return ( strcmp(sect
->segname(), "__TEXT") == 0 );
4337 template <typename A
>
4338 ld::Section::Type Section
<A
>::sectionType(const macho_section
<typename
A::P
>* sect
)
4340 switch ( sect
->flags() & SECTION_TYPE
) {
4342 return ld::Section::typeZeroFill
;
4343 case S_CSTRING_LITERALS
:
4344 if ( (strcmp(sect
->sectname(), "__cstring") == 0) && (strcmp(sect
->segname(), "__TEXT") == 0) )
4345 return ld::Section::typeCString
;
4347 return ld::Section::typeNonStdCString
;
4348 case S_4BYTE_LITERALS
:
4349 return ld::Section::typeLiteral4
;
4350 case S_8BYTE_LITERALS
:
4351 return ld::Section::typeLiteral8
;
4352 case S_LITERAL_POINTERS
:
4353 return ld::Section::typeCStringPointer
;
4354 case S_NON_LAZY_SYMBOL_POINTERS
:
4355 return ld::Section::typeNonLazyPointer
;
4356 case S_LAZY_SYMBOL_POINTERS
:
4357 return ld::Section::typeLazyPointer
;
4358 case S_SYMBOL_STUBS
:
4359 return ld::Section::typeStub
;
4360 case S_MOD_INIT_FUNC_POINTERS
:
4361 return ld::Section::typeInitializerPointers
;
4362 case S_MOD_TERM_FUNC_POINTERS
:
4363 return ld::Section::typeTerminatorPointers
;
4365 return ld::Section::typeUnclassified
;
4366 case S_16BYTE_LITERALS
:
4367 return ld::Section::typeLiteral16
;
4370 if ( sect
->flags() & S_ATTR_PURE_INSTRUCTIONS
) {
4371 return ld::Section::typeCode
;
4373 else if ( strcmp(sect
->segname(), "__TEXT") == 0 ) {
4374 if ( strcmp(sect
->sectname(), "__eh_frame") == 0 )
4375 return ld::Section::typeCFI
;
4376 else if ( strcmp(sect
->sectname(), "__ustring") == 0 )
4377 return ld::Section::typeUTF16Strings
;
4378 else if ( strcmp(sect
->sectname(), "__textcoal_nt") == 0 )
4379 return ld::Section::typeCode
;
4380 else if ( strcmp(sect
->sectname(), "__StaticInit") == 0 )
4381 return ld::Section::typeCode
;
4382 else if ( strcmp(sect
->sectname(), "__constructor") == 0 )
4383 return ld::Section::typeInitializerPointers
;
4385 else if ( strcmp(sect
->segname(), "__DATA") == 0 ) {
4386 if ( strcmp(sect
->sectname(), "__cfstring") == 0 )
4387 return ld::Section::typeCFString
;
4388 else if ( strcmp(sect
->sectname(), "__dyld") == 0 )
4389 return ld::Section::typeDyldInfo
;
4390 else if ( strcmp(sect
->sectname(), "__program_vars") == 0 )
4391 return ld::Section::typeDyldInfo
;
4392 else if ( strncmp(sect
->sectname(), "__objc_classrefs", 16) == 0 )
4393 return ld::Section::typeObjCClassRefs
;
4394 else if ( strcmp(sect
->sectname(), "__objc_catlist") == 0 )
4395 return ld::Section::typeObjC2CategoryList
;
4397 else if ( strcmp(sect
->segname(), "__OBJC") == 0 ) {
4398 if ( strcmp(sect
->sectname(), "__class") == 0 )
4399 return ld::Section::typeObjC1Classes
;
4402 case S_THREAD_LOCAL_REGULAR
:
4403 return ld::Section::typeTLVInitialValues
;
4404 case S_THREAD_LOCAL_ZEROFILL
:
4405 return ld::Section::typeTLVZeroFill
;
4406 case S_THREAD_LOCAL_VARIABLES
:
4407 return ld::Section::typeTLVDefs
;
4408 case S_THREAD_LOCAL_VARIABLE_POINTERS
:
4409 return ld::Section::typeTLVPointers
;
4410 case S_THREAD_LOCAL_INIT_FUNCTION_POINTERS
:
4411 return ld::Section::typeTLVInitializerPointers
;
4413 return ld::Section::typeUnclassified
;
4417 template <typename A
>
4418 Atom
<A
>* Section
<A
>::findContentAtomByAddress(pint_t addr
, class Atom
<A
>* start
, class Atom
<A
>* end
)
4420 // do a binary search of atom array
4421 uint32_t atomCount
= end
- start
;
4422 Atom
<A
>* base
= start
;
4423 for (uint32_t n
= atomCount
; n
> 0; n
/= 2) {
4424 Atom
<A
>* pivot
= &base
[n
/2];
4425 pint_t atomStartAddr
= pivot
->_objAddress
;
4426 pint_t atomEndAddr
= atomStartAddr
+ pivot
->_size
;
4427 if ( atomStartAddr
<= addr
) {
4428 // address in normal atom
4429 if (addr
< atomEndAddr
)
4431 // address in "end" label (but not in alias)
4432 if ( (pivot
->_size
== 0) && (addr
== atomEndAddr
) && !pivot
->isAlias() )
4435 if ( addr
>= atomEndAddr
) {
4437 // move base to atom after pivot
4449 template <typename A
>
4450 ld::Atom::Alignment Section
<A
>::alignmentForAddress(pint_t addr
)
4452 const uint32_t sectionAlignment
= this->_machOSection
->align();
4453 uint32_t modulus
= (addr
% (1 << sectionAlignment
));
4454 if ( modulus
> 0xFFFF )
4455 warning("alignment for symbol at address 0x%08llX in %s exceeds 2^16", (uint64_t)addr
, this->file().path());
4456 return ld::Atom::Alignment(sectionAlignment
, modulus
);
4459 template <typename A
>
4460 uint32_t Section
<A
>::sectionNum(class Parser
<A
>& parser
) const
4462 if ( _machOSection
== NULL
)
4465 return 1 + (this->_machOSection
- parser
.firstMachOSection());
4468 // arm does not have zero cost exceptions
4470 uint32_t CFISection
<arm
>::cfiCount(Parser
<arm
>& parser
)
4472 if ( parser
.armUsesZeroCostExceptions() ) {
4473 // create ObjectAddressSpace object for use by libunwind
4474 OAS
oas(*this, (uint8_t*)this->file().fileContent()+this->_machOSection
->offset());
4475 return libunwind::CFI_Parser
<OAS
>::getCFICount(oas
,
4476 this->_machOSection
->addr(), this->_machOSection
->size());
4481 template <typename A
>
4482 uint32_t CFISection
<A
>::cfiCount(Parser
<A
>& parser
)
4484 // create ObjectAddressSpace object for use by libunwind
4485 OAS
oas(*this, (uint8_t*)this->file().fileContent()+this->_machOSection
->offset());
4486 return libunwind::CFI_Parser
<OAS
>::getCFICount(oas
,
4487 this->_machOSection
->addr(), this->_machOSection
->size());
4490 template <typename A
>
4491 void CFISection
<A
>::warnFunc(void* ref
, uint64_t funcAddr
, const char* msg
)
4493 Parser
<A
>* parser
= (Parser
<A
>*)ref
;
4494 if ( ! parser
->warnUnwindConversionProblems() )
4496 if ( funcAddr
!= CFI_INVALID_ADDRESS
) {
4497 // atoms are not constructed yet, so scan symbol table for labels
4498 const char* name
= parser
->scanSymbolTableForAddress(funcAddr
);
4499 warning("could not create compact unwind for %s: %s", name
, msg
);
4502 warning("could not create compact unwind: %s", msg
);
4507 bool CFISection
<x86_64
>::needsRelocating()
4513 bool CFISection
<arm64
>::needsRelocating()
4519 template <typename A
>
4520 bool CFISection
<A
>::needsRelocating()
4526 void CFISection
<x86_64
>::cfiParse(class Parser
<x86_64
>& parser
, uint8_t* buffer
,
4527 libunwind::CFI_Atom_Info
<CFISection
<x86_64
>::OAS
> cfiArray
[],
4528 uint32_t& count
, const pint_t cuStarts
[], uint32_t cuCount
)
4530 const uint32_t sectionSize
= this->_machOSection
->size();
4531 // copy __eh_frame data to buffer
4532 memcpy(buffer
, file().fileContent() + this->_machOSection
->offset(), sectionSize
);
4534 // and apply relocations
4535 const macho_relocation_info
<P
>* relocs
= (macho_relocation_info
<P
>*)(file().fileContent() + this->_machOSection
->reloff());
4536 const macho_relocation_info
<P
>* relocsEnd
= &relocs
[this->_machOSection
->nreloc()];
4537 for (const macho_relocation_info
<P
>* reloc
= relocs
; reloc
< relocsEnd
; ++reloc
) {
4539 switch ( reloc
->r_type() ) {
4540 case X86_64_RELOC_SUBTRACTOR
:
4541 value
= 0 - parser
.symbolFromIndex(reloc
->r_symbolnum()).n_value();
4543 if ( reloc
->r_extern() )
4544 value
+= parser
.symbolFromIndex(reloc
->r_symbolnum()).n_value();
4546 case X86_64_RELOC_UNSIGNED
:
4547 value
= parser
.symbolFromIndex(reloc
->r_symbolnum()).n_value();
4549 case X86_64_RELOC_GOT
:
4550 // this is used for the reference to the personality function in CIEs
4551 // store the symbol number of the personality function for later use as a Fixup
4552 value
= reloc
->r_symbolnum();
4555 fprintf(stderr
, "CFISection::cfiParse() unexpected relocation type at r_address=0x%08X\n", reloc
->r_address());
4558 if ( reloc
->r_address() > sectionSize
)
4559 throwf("malformed __eh_frame relocation, offset (0x%08X) is beyond end of section,", reloc
->r_address());
4562 switch ( reloc
->r_length() ) {
4564 p64
= (uint64_t*)&buffer
[reloc
->r_address()];
4565 E::set64(*p64
, value
+ E::get64(*p64
));
4568 p32
= (uint32_t*)&buffer
[reloc
->r_address()];
4569 E::set32(*p32
, value
+ E::get32(*p32
));
4572 fprintf(stderr
, "CFISection::cfiParse() unexpected relocation size at r_address=0x%08X\n", reloc
->r_address());
4577 // create ObjectAddressSpace object for use by libunwind
4578 OAS
oas(*this, buffer
);
4580 // use libuwind to parse __eh_frame data into array of CFI_Atom_Info
4582 msg
= libunwind::DwarfInstructions
<OAS
, libunwind::Registers_x86_64
>::parseCFIs(
4583 oas
, this->_machOSection
->addr(), this->_machOSection
->size(),
4584 cuStarts
, cuCount
, parser
.keepDwarfUnwind(), parser
.forceDwarfConversion(), parser
.neverConvertDwarf(),
4585 cfiArray
, count
, (void*)&parser
, warnFunc
);
4587 throwf("malformed __eh_frame section: %s", msg
);
4591 void CFISection
<x86
>::cfiParse(class Parser
<x86
>& parser
, uint8_t* buffer
,
4592 libunwind::CFI_Atom_Info
<CFISection
<x86
>::OAS
> cfiArray
[],
4593 uint32_t& count
, const pint_t cuStarts
[], uint32_t cuCount
)
4595 // create ObjectAddressSpace object for use by libunwind
4596 OAS
oas(*this, (uint8_t*)this->file().fileContent()+this->_machOSection
->offset());
4598 // use libuwind to parse __eh_frame data into array of CFI_Atom_Info
4600 msg
= libunwind::DwarfInstructions
<OAS
, libunwind::Registers_x86
>::parseCFIs(
4601 oas
, this->_machOSection
->addr(), this->_machOSection
->size(),
4602 cuStarts
, cuCount
, parser
.keepDwarfUnwind(), parser
.forceDwarfConversion(), parser
.neverConvertDwarf(),
4603 cfiArray
, count
, (void*)&parser
, warnFunc
);
4605 throwf("malformed __eh_frame section: %s", msg
);
4612 void CFISection
<arm
>::cfiParse(class Parser
<arm
>& parser
, uint8_t* buffer
,
4613 libunwind::CFI_Atom_Info
<CFISection
<arm
>::OAS
> cfiArray
[],
4614 uint32_t& count
, const pint_t cuStarts
[], uint32_t cuCount
)
4616 if ( !parser
.armUsesZeroCostExceptions() ) {
4617 // most arm do not use zero cost exceptions
4621 // create ObjectAddressSpace object for use by libunwind
4622 OAS
oas(*this, (uint8_t*)this->file().fileContent()+this->_machOSection
->offset());
4624 // use libuwind to parse __eh_frame data into array of CFI_Atom_Info
4626 msg
= libunwind::DwarfInstructions
<OAS
, libunwind::Registers_arm
>::parseCFIs(
4627 oas
, this->_machOSection
->addr(), this->_machOSection
->size(),
4628 cuStarts
, cuCount
, parser
.keepDwarfUnwind(), parser
.forceDwarfConversion(), parser
.neverConvertDwarf(),
4629 cfiArray
, count
, (void*)&parser
, warnFunc
);
4631 throwf("malformed __eh_frame section: %s", msg
);
4638 void CFISection
<arm64
>::cfiParse(class Parser
<arm64
>& parser
, uint8_t* buffer
,
4639 libunwind::CFI_Atom_Info
<CFISection
<arm64
>::OAS
> cfiArray
[],
4640 uint32_t& count
, const pint_t cuStarts
[], uint32_t cuCount
)
4642 // copy __eh_frame data to buffer
4643 const uint32_t sectionSize
= this->_machOSection
->size();
4644 memcpy(buffer
, file().fileContent() + this->_machOSection
->offset(), sectionSize
);
4646 // and apply relocations
4647 const macho_relocation_info
<P
>* relocs
= (macho_relocation_info
<P
>*)(file().fileContent() + this->_machOSection
->reloff());
4648 const macho_relocation_info
<P
>* relocsEnd
= &relocs
[this->_machOSection
->nreloc()];
4649 for (const macho_relocation_info
<P
>* reloc
= relocs
; reloc
< relocsEnd
; ++reloc
) {
4650 uint64_t* p64
= (uint64_t*)&buffer
[reloc
->r_address()];
4651 uint32_t* p32
= (uint32_t*)&buffer
[reloc
->r_address()];
4652 uint32_t addend32
= E::get32(*p32
);
4653 uint64_t addend64
= E::get64(*p64
);
4655 switch ( reloc
->r_type() ) {
4656 case ARM64_RELOC_SUBTRACTOR
:
4657 value
= 0 - parser
.symbolFromIndex(reloc
->r_symbolnum()).n_value();
4659 if ( reloc
->r_extern() )
4660 value
+= parser
.symbolFromIndex(reloc
->r_symbolnum()).n_value();
4662 case ARM64_RELOC_UNSIGNED
:
4663 value
= parser
.symbolFromIndex(reloc
->r_symbolnum()).n_value();
4665 case ARM64_RELOC_POINTER_TO_GOT
:
4666 // this is used for the reference to the personality function in CIEs
4667 // store the symbol number of the personality function for later use as a Fixup
4668 value
= reloc
->r_symbolnum();
4673 fprintf(stderr
, "CFISection::cfiParse() unexpected relocation type at r_address=0x%08X\n", reloc
->r_address());
4676 if ( reloc
->r_address() > sectionSize
)
4677 throwf("malformed __eh_frame relocation, offset (0x%08X) is beyond end of section,", reloc
->r_address());
4678 switch ( reloc
->r_length() ) {
4680 E::set64(*p64
, value
+ addend64
);
4683 E::set32(*p32
, value
+ addend32
);
4686 fprintf(stderr
, "CFISection::cfiParse() unexpected relocation size at r_address=0x%08X\n", reloc
->r_address());
4692 // create ObjectAddressSpace object for use by libunwind
4693 OAS
oas(*this, buffer
);
4695 // use libuwind to parse __eh_frame data into array of CFI_Atom_Info
4697 msg
= libunwind::DwarfInstructions
<OAS
, libunwind::Registers_arm64
>::parseCFIs(
4698 oas
, this->_machOSection
->addr(), this->_machOSection
->size(),
4699 cuStarts
, cuCount
, parser
.keepDwarfUnwind(), parser
.forceDwarfConversion(), parser
.neverConvertDwarf(),
4700 cfiArray
, count
, (void*)&parser
, warnFunc
);
4702 throwf("malformed __eh_frame section: %s", msg
);
4706 template <typename A
>
4707 uint32_t CFISection
<A
>::computeAtomCount(class Parser
<A
>& parser
,
4708 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
4709 const struct Parser
<A
>::CFI_CU_InfoArrays
& cfis
)
4711 return cfis
.cfiCount
;
4716 template <typename A
>
4717 uint32_t CFISection
<A
>::appendAtoms(class Parser
<A
>& parser
, uint8_t* p
,
4718 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
4719 const struct Parser
<A
>::CFI_CU_InfoArrays
& cfis
)
4721 this->_beginAtoms
= (Atom
<A
>*)p
;
4722 // walk CFI_Atom_Info array and create atom for each entry
4723 const CFI_Atom_Info
* start
= &cfis
.cfiArray
[0];
4724 const CFI_Atom_Info
* end
= &cfis
.cfiArray
[cfis
.cfiCount
];
4725 for(const CFI_Atom_Info
* a
=start
; a
< end
; ++a
) {
4726 Atom
<A
>* space
= (Atom
<A
>*)p
;
4727 new (space
) Atom
<A
>(*this, (a
->isCIE
? "CIE" : "FDE"), a
->address
, a
->size
,
4728 ld::Atom::definitionRegular
, ld::Atom::combineNever
, ld::Atom::scopeTranslationUnit
,
4729 ld::Atom::typeCFI
, ld::Atom::symbolTableNotInFinalLinkedImages
,
4730 false, false, false, ld::Atom::Alignment(0));
4731 p
+= sizeof(Atom
<A
>);
4733 this->_endAtoms
= (Atom
<A
>*)p
;
4734 return cfis
.cfiCount
;
4738 template <> bool CFISection
<x86_64
>::bigEndian() { return false; }
4739 template <> bool CFISection
<x86
>::bigEndian() { return false; }
4740 template <> bool CFISection
<arm
>::bigEndian() { return false; }
4741 template <> bool CFISection
<arm64
>::bigEndian() { return false; }
4744 void CFISection
<x86_64
>::addCiePersonalityFixups(class Parser
<x86_64
>& parser
, const CFI_Atom_Info
* cieInfo
)
4746 uint8_t personalityEncoding
= cieInfo
->u
.cieInfo
.personality
.encodingOfTargetAddress
;
4747 if ( personalityEncoding
== 0x9B ) {
4748 // compiler always produces X86_64_RELOC_GOT with addend of 4 to personality function
4749 // CFISection<x86_64>::cfiParse() set targetAddress to be symbolIndex + 4 + addressInCIE
4750 uint32_t symbolIndex
= cieInfo
->u
.cieInfo
.personality
.targetAddress
- 4
4751 - cieInfo
->address
- cieInfo
->u
.cieInfo
.personality
.offsetInCFI
;
4752 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(symbolIndex
);
4753 const char* personalityName
= parser
.nameFromSymbol(sym
);
4755 Atom
<x86_64
>* cieAtom
= this->findAtomByAddress(cieInfo
->address
);
4756 Parser
<x86_64
>::SourceLocation
src(cieAtom
, cieInfo
->u
.cieInfo
.personality
.offsetInCFI
);
4757 parser
.addFixup(src
, ld::Fixup::k1of3
, ld::Fixup::kindSetTargetAddress
, false, personalityName
);
4758 parser
.addFixup(src
, ld::Fixup::k2of3
, ld::Fixup::kindAddAddend
, 4);
4759 parser
.addFixup(src
, ld::Fixup::k3of3
, ld::Fixup::kindStoreX86PCRel32GOT
);
4761 else if ( personalityEncoding
!= 0 ) {
4762 throwf("unsupported address encoding (%02X) of personality function in CIE",
4763 personalityEncoding
);
4768 void CFISection
<x86
>::addCiePersonalityFixups(class Parser
<x86
>& parser
, const CFI_Atom_Info
* cieInfo
)
4770 uint8_t personalityEncoding
= cieInfo
->u
.cieInfo
.personality
.encodingOfTargetAddress
;
4771 if ( (personalityEncoding
== 0x9B) || (personalityEncoding
== 0x90) ) {
4772 uint32_t offsetInCFI
= cieInfo
->u
.cieInfo
.personality
.offsetInCFI
;
4773 uint32_t nlpAddr
= cieInfo
->u
.cieInfo
.personality
.targetAddress
;
4774 Atom
<x86
>* cieAtom
= this->findAtomByAddress(cieInfo
->address
);
4775 Atom
<x86
>* nlpAtom
= parser
.findAtomByAddress(nlpAddr
);
4776 assert(nlpAtom
->contentType() == ld::Atom::typeNonLazyPointer
);
4777 Parser
<x86
>::SourceLocation
src(cieAtom
, cieInfo
->u
.cieInfo
.personality
.offsetInCFI
);
4779 parser
.addFixup(src
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, nlpAtom
);
4780 parser
.addFixup(src
, ld::Fixup::k2of4
, ld::Fixup::kindSubtractTargetAddress
, cieAtom
);
4781 parser
.addFixup(src
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractAddend
, offsetInCFI
);
4782 parser
.addFixup(src
, ld::Fixup::k4of4
, ld::Fixup::kindStoreLittleEndian32
);
4784 else if ( personalityEncoding
!= 0 ) {
4785 throwf("unsupported address encoding (%02X) of personality function in CIE", personalityEncoding
);
4789 #if SUPPORT_ARCH_arm64
4791 void CFISection
<arm64
>::addCiePersonalityFixups(class Parser
<arm64
>& parser
, const CFI_Atom_Info
* cieInfo
)
4793 uint8_t personalityEncoding
= cieInfo
->u
.cieInfo
.personality
.encodingOfTargetAddress
;
4794 if ( personalityEncoding
== 0x9B ) {
4795 // compiler always produces ARM64_RELOC_GOT r_pcrel=1 to personality function
4796 // CFISection<arm64>::cfiParse() set targetAddress to be symbolIndex + addressInCIE
4797 uint32_t symbolIndex
= cieInfo
->u
.cieInfo
.personality
.targetAddress
4798 - cieInfo
->address
- cieInfo
->u
.cieInfo
.personality
.offsetInCFI
;
4799 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(symbolIndex
);
4800 const char* personalityName
= parser
.nameFromSymbol(sym
);
4802 Atom
<arm64
>* cieAtom
= this->findAtomByAddress(cieInfo
->address
);
4803 Parser
<arm64
>::SourceLocation
src(cieAtom
, cieInfo
->u
.cieInfo
.personality
.offsetInCFI
);
4804 parser
.addFixup(src
, ld::Fixup::k1of2
, ld::Fixup::kindSetTargetAddress
, false, personalityName
);
4805 parser
.addFixup(src
, ld::Fixup::k2of2
, ld::Fixup::kindStoreARM64PCRelToGOT
);
4807 else if ( personalityEncoding
!= 0 ) {
4808 throwf("unsupported address encoding (%02X) of personality function in CIE",
4809 personalityEncoding
);
4816 void CFISection
<arm
>::addCiePersonalityFixups(class Parser
<arm
>& parser
, const CFI_Atom_Info
* cieInfo
)
4818 uint8_t personalityEncoding
= cieInfo
->u
.cieInfo
.personality
.encodingOfTargetAddress
;
4819 if ( (personalityEncoding
== 0x9B) || (personalityEncoding
== 0x90) ) {
4820 uint32_t offsetInCFI
= cieInfo
->u
.cieInfo
.personality
.offsetInCFI
;
4821 uint32_t nlpAddr
= cieInfo
->u
.cieInfo
.personality
.targetAddress
;
4822 Atom
<arm
>* cieAtom
= this->findAtomByAddress(cieInfo
->address
);
4823 Atom
<arm
>* nlpAtom
= parser
.findAtomByAddress(nlpAddr
);
4824 assert(nlpAtom
->contentType() == ld::Atom::typeNonLazyPointer
);
4825 Parser
<arm
>::SourceLocation
src(cieAtom
, cieInfo
->u
.cieInfo
.personality
.offsetInCFI
);
4827 parser
.addFixup(src
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, nlpAtom
);
4828 parser
.addFixup(src
, ld::Fixup::k2of4
, ld::Fixup::kindSubtractTargetAddress
, cieAtom
);
4829 parser
.addFixup(src
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractAddend
, offsetInCFI
);
4830 parser
.addFixup(src
, ld::Fixup::k4of4
, ld::Fixup::kindStoreLittleEndian32
);
4832 else if ( personalityEncoding
!= 0 ) {
4833 throwf("unsupported address encoding (%02X) of personality function in CIE", personalityEncoding
);
4839 template <typename A
>
4840 void CFISection
<A
>::addCiePersonalityFixups(class Parser
<A
>& parser
, const CFI_Atom_Info
* cieInfo
)
4842 assert(0 && "addCiePersonalityFixups() not implemented for arch");
4845 template <typename A
>
4846 void CFISection
<A
>::makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
& cfis
)
4848 ld::Fixup::Kind store32
= bigEndian() ? ld::Fixup::kindStoreBigEndian32
: ld::Fixup::kindStoreLittleEndian32
;
4849 ld::Fixup::Kind store64
= bigEndian() ? ld::Fixup::kindStoreBigEndian64
: ld::Fixup::kindStoreLittleEndian64
;
4851 // add all references for FDEs, including implicit group references
4852 const CFI_Atom_Info
* end
= &cfis
.cfiArray
[cfis
.cfiCount
];
4853 for(const CFI_Atom_Info
* p
= &cfis
.cfiArray
[0]; p
< end
; ++p
) {
4855 // add reference to personality function if used
4856 if ( p
->u
.cieInfo
.personality
.targetAddress
!= CFI_INVALID_ADDRESS
) {
4857 this->addCiePersonalityFixups(parser
, p
);
4862 Atom
<A
>* fdeAtom
= this->findAtomByAddress(p
->address
);
4863 // find function Atom
4864 Atom
<A
>* functionAtom
= parser
.findAtomByAddress(p
->u
.fdeInfo
.function
.targetAddress
);
4866 Atom
<A
>* cieAtom
= this->findAtomByAddress(p
->u
.fdeInfo
.cie
.targetAddress
);
4868 Atom
<A
>* lsdaAtom
= NULL
;
4869 if ( p
->u
.fdeInfo
.lsda
.targetAddress
!= CFI_INVALID_ADDRESS
) {
4870 lsdaAtom
= parser
.findAtomByAddress(p
->u
.fdeInfo
.lsda
.targetAddress
);
4872 // add reference from FDE to CIE (always 32-bit pc-rel)
4873 typename Parser
<A
>::SourceLocation
fdeToCieSrc(fdeAtom
, p
->u
.fdeInfo
.cie
.offsetInCFI
);
4874 parser
.addFixup(fdeToCieSrc
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, fdeAtom
);
4875 parser
.addFixup(fdeToCieSrc
, ld::Fixup::k2of4
, ld::Fixup::kindAddAddend
, p
->u
.fdeInfo
.cie
.offsetInCFI
);
4876 parser
.addFixup(fdeToCieSrc
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractTargetAddress
, cieAtom
);
4877 parser
.addFixup(fdeToCieSrc
, ld::Fixup::k4of4
, store32
, cieAtom
);
4879 // add reference from FDE to function
4880 typename Parser
<A
>::SourceLocation
fdeToFuncSrc(fdeAtom
, p
->u
.fdeInfo
.function
.offsetInCFI
);
4881 switch (p
->u
.fdeInfo
.function
.encodingOfTargetAddress
) {
4882 case DW_EH_PE_pcrel
|DW_EH_PE_ptr
:
4883 if ( sizeof(typename
A::P::uint_t
) == 8 ) {
4884 parser
.addFixup(fdeToFuncSrc
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, functionAtom
);
4885 parser
.addFixup(fdeToFuncSrc
, ld::Fixup::k2of4
, ld::Fixup::kindSubtractTargetAddress
, fdeAtom
);
4886 parser
.addFixup(fdeToFuncSrc
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractAddend
, p
->u
.fdeInfo
.function
.offsetInCFI
);
4887 parser
.addFixup(fdeToFuncSrc
, ld::Fixup::k4of4
, store64
);
4890 // else fall into 32-bit case
4891 case DW_EH_PE_pcrel
|DW_EH_PE_sdata4
:
4892 parser
.addFixup(fdeToFuncSrc
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, functionAtom
);
4893 parser
.addFixup(fdeToFuncSrc
, ld::Fixup::k2of4
, ld::Fixup::kindSubtractTargetAddress
, fdeAtom
);
4894 parser
.addFixup(fdeToFuncSrc
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractAddend
, p
->u
.fdeInfo
.function
.offsetInCFI
);
4895 parser
.addFixup(fdeToFuncSrc
, ld::Fixup::k4of4
, store32
);
4898 throw "unsupported encoding in FDE of pointer to function";
4901 // add reference from FDE to LSDA
4902 typename Parser
<A
>::SourceLocation
fdeToLsdaSrc(fdeAtom
, p
->u
.fdeInfo
.lsda
.offsetInCFI
);
4903 if ( lsdaAtom
!= NULL
) {
4904 switch (p
->u
.fdeInfo
.lsda
.encodingOfTargetAddress
) {
4905 case DW_EH_PE_pcrel
|DW_EH_PE_ptr
:
4906 if ( sizeof(typename
A::P::uint_t
) == 8 ) {
4907 parser
.addFixup(fdeToLsdaSrc
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, lsdaAtom
);
4908 parser
.addFixup(fdeToLsdaSrc
, ld::Fixup::k2of4
, ld::Fixup::kindSubtractTargetAddress
, fdeAtom
);
4909 parser
.addFixup(fdeToLsdaSrc
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractAddend
, p
->u
.fdeInfo
.lsda
.offsetInCFI
);
4910 parser
.addFixup(fdeToLsdaSrc
, ld::Fixup::k4of4
, store64
);
4913 // else fall into 32-bit case
4914 case DW_EH_PE_pcrel
|DW_EH_PE_sdata4
:
4915 parser
.addFixup(fdeToLsdaSrc
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, lsdaAtom
);
4916 parser
.addFixup(fdeToLsdaSrc
, ld::Fixup::k2of4
, ld::Fixup::kindSubtractTargetAddress
, fdeAtom
);
4917 parser
.addFixup(fdeToLsdaSrc
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractAddend
, p
->u
.fdeInfo
.lsda
.offsetInCFI
);
4918 parser
.addFixup(fdeToLsdaSrc
, ld::Fixup::k4of4
, store32
);
4921 throw "unsupported encoding in FDE of pointer to LSDA";
4925 // FDE is in group lead by function atom
4926 typename Parser
<A
>::SourceLocation
fdeSrc(functionAtom
,0);
4927 parser
.addFixup(fdeSrc
, ld::Fixup::k1of1
, ld::Fixup::kindNoneGroupSubordinateFDE
, fdeAtom
);
4929 // LSDA is in group lead by function atom
4930 if ( lsdaAtom
!= NULL
) {
4931 parser
.addFixup(fdeSrc
, ld::Fixup::k1of1
, ld::Fixup::kindNoneGroupSubordinateLSDA
, lsdaAtom
);
4940 template <typename A
>
4941 const void* CFISection
<A
>::OAS::mappedAddress(pint_t addr
)
4943 if ( (_ehFrameStartAddr
<= addr
) && (addr
< _ehFrameEndAddr
) )
4944 return &_ehFrameContent
[addr
-_ehFrameStartAddr
];
4946 // requested bytes are not in __eh_frame section
4947 // this can occur when examining the instruction bytes in the __text
4948 File
<A
>& file
= _ehFrameSection
.file();
4949 for (uint32_t i
=0; i
< file
._sectionsArrayCount
; ++i
) {
4950 const macho_section
<typename
A::P
>* sect
= file
._sectionsArray
[i
]->machoSection();
4951 // TentativeDefinitionSection and AbsoluteSymbolSection have no mach-o section
4952 if ( sect
!= NULL
) {
4953 if ( (sect
->addr() <= addr
) && (addr
< (sect
->addr()+sect
->size())) ) {
4954 return file
.fileContent() + sect
->offset() + addr
- sect
->addr();
4958 throwf("__eh_frame parsing problem. Can't find target of reference to address 0x%08llX", (uint64_t)addr
);
4963 template <typename A
>
4964 uint64_t CFISection
<A
>::OAS::getULEB128(pint_t
& logicalAddr
, pint_t end
)
4966 uintptr_t size
= (end
- logicalAddr
);
4967 libunwind::LocalAddressSpace::pint_t laddr
= (libunwind::LocalAddressSpace::pint_t
)mappedAddress(logicalAddr
);
4968 libunwind::LocalAddressSpace::pint_t sladdr
= laddr
;
4969 uint64_t result
= libunwind::LocalAddressSpace::getULEB128(laddr
, laddr
+size
);
4970 logicalAddr
+= (laddr
-sladdr
);
4974 template <typename A
>
4975 int64_t CFISection
<A
>::OAS::getSLEB128(pint_t
& logicalAddr
, pint_t end
)
4977 uintptr_t size
= (end
- logicalAddr
);
4978 libunwind::LocalAddressSpace::pint_t laddr
= (libunwind::LocalAddressSpace::pint_t
)mappedAddress(logicalAddr
);
4979 libunwind::LocalAddressSpace::pint_t sladdr
= laddr
;
4980 int64_t result
= libunwind::LocalAddressSpace::getSLEB128(laddr
, laddr
+size
);
4981 logicalAddr
+= (laddr
-sladdr
);
4985 template <typename A
>
4986 typename
A::P::uint_t CFISection
<A
>::OAS::getEncodedP(pint_t
& addr
, pint_t end
, uint8_t encoding
)
4988 pint_t startAddr
= addr
;
4993 switch (encoding
& 0x0F) {
4995 result
= getP(addr
);
4996 p
+= sizeof(pint_t
);
4999 case DW_EH_PE_uleb128
:
5000 result
= getULEB128(addr
, end
);
5002 case DW_EH_PE_udata2
:
5003 result
= get16(addr
);
5007 case DW_EH_PE_udata4
:
5008 result
= get32(addr
);
5012 case DW_EH_PE_udata8
:
5013 result
= get64(addr
);
5017 case DW_EH_PE_sleb128
:
5018 result
= getSLEB128(addr
, end
);
5020 case DW_EH_PE_sdata2
:
5021 result
= (int16_t)get16(addr
);
5025 case DW_EH_PE_sdata4
:
5026 result
= (int32_t)get32(addr
);
5030 case DW_EH_PE_sdata8
:
5031 result
= get64(addr
);
5036 throwf("ObjectFileAddressSpace<A>::getEncodedP() encoding 0x%08X not supported", encoding
);
5039 // then add relative offset
5040 switch ( encoding
& 0x70 ) {
5041 case DW_EH_PE_absptr
:
5044 case DW_EH_PE_pcrel
:
5045 result
+= startAddr
;
5047 case DW_EH_PE_textrel
:
5048 throw "DW_EH_PE_textrel pointer encoding not supported";
5050 case DW_EH_PE_datarel
:
5051 throw "DW_EH_PE_datarel pointer encoding not supported";
5053 case DW_EH_PE_funcrel
:
5054 throw "DW_EH_PE_funcrel pointer encoding not supported";
5056 case DW_EH_PE_aligned
:
5057 throw "DW_EH_PE_aligned pointer encoding not supported";
5060 throwf("ObjectFileAddressSpace<A>::getEncodedP() encoding 0x%08X not supported", encoding
);
5064 // Note: DW_EH_PE_indirect is only used in CIEs to refernce the personality pointer
5065 // When parsing .o files that pointer contains zero, so we don't to return that.
5066 // Instead we skip the dereference and return the address of the pointer.
5067 // if ( encoding & DW_EH_PE_indirect )
5068 // result = getP(result);
5074 const char* CUSection
<x86_64
>::personalityName(class Parser
<x86_64
>& parser
, const macho_relocation_info
<x86_64::P
>* reloc
)
5076 if ( reloc
->r_extern() ) {
5077 assert((reloc
->r_type() == X86_64_RELOC_UNSIGNED
) && "wrong reloc type on personality column in __compact_unwind section");
5078 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(reloc
->r_symbolnum());
5079 return parser
.nameFromSymbol(sym
);
5082 const pint_t
* content
= (pint_t
*)(this->file().fileContent() + this->_machOSection
->offset() + reloc
->r_address());
5083 pint_t personalityAddr
= *content
;
5084 assert((parser
.sectionForAddress(personalityAddr
)->type() == ld::Section::typeCode
) && "personality column in __compact_unwind section is not pointer to function");
5085 // atoms may not be constructed yet, so scan symbol table for labels
5086 const char* name
= parser
.scanSymbolTableForAddress(personalityAddr
);
5092 const char* CUSection
<x86
>::personalityName(class Parser
<x86
>& parser
, const macho_relocation_info
<x86::P
>* reloc
)
5094 if ( reloc
->r_extern() ) {
5095 assert((reloc
->r_type() == GENERIC_RELOC_VANILLA
) && "wrong reloc type on personality column in __compact_unwind section");
5096 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(reloc
->r_symbolnum());
5097 return parser
.nameFromSymbol(sym
);
5100 // support __LD, __compact_unwind personality entries which are pointer to personality non-lazy pointer
5101 const pint_t
* content
= (pint_t
*)(this->file().fileContent() + this->_machOSection
->offset() + reloc
->r_address());
5102 pint_t nlPointerAddr
= *content
;
5103 Section
<x86
>* nlSection
= parser
.sectionForAddress(nlPointerAddr
);
5104 if ( nlSection
->type() == ld::Section::typeCode
) {
5105 // personality function is defined in this .o file, so this is a direct reference to it
5106 // atoms may not be constructed yet, so scan symbol table for labels
5107 const char* name
= parser
.scanSymbolTableForAddress(nlPointerAddr
);
5111 uint32_t symIndex
= parser
.symbolIndexFromIndirectSectionAddress(nlPointerAddr
, nlSection
->machoSection());
5112 const macho_nlist
<P
>& nlSymbol
= parser
.symbolFromIndex(symIndex
);
5113 return parser
.nameFromSymbol(nlSymbol
);
5118 #if SUPPORT_ARCH_arm64
5120 const char* CUSection
<arm64
>::personalityName(class Parser
<arm64
>& parser
, const macho_relocation_info
<arm64::P
>* reloc
)
5122 if ( reloc
->r_extern() ) {
5123 assert((reloc
->r_type() == ARM64_RELOC_UNSIGNED
) && "wrong reloc type on personality column in __compact_unwind section");
5124 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(reloc
->r_symbolnum());
5125 return parser
.nameFromSymbol(sym
);
5128 const pint_t
* content
= (pint_t
*)(this->file().fileContent() + this->_machOSection
->offset() + reloc
->r_address());
5129 pint_t personalityAddr
= *content
;
5130 Section
<arm64
>* personalitySection
= parser
.sectionForAddress(personalityAddr
);
5131 (void)personalitySection
;
5132 assert((personalitySection
->type() == ld::Section::typeCode
) && "personality column in __compact_unwind section is not pointer to function");
5133 // atoms may not be constructed yet, so scan symbol table for labels
5134 const char* name
= parser
.scanSymbolTableForAddress(personalityAddr
);
5141 #if SUPPORT_ARCH_arm_any
5143 const char* CUSection
<arm
>::personalityName(class Parser
<arm
>& parser
, const macho_relocation_info
<arm::P
>* reloc
)
5145 if ( reloc
->r_extern() ) {
5146 assert((reloc
->r_type() == ARM_RELOC_VANILLA
) && "wrong reloc type on personality column in __compact_unwind section");
5147 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(reloc
->r_symbolnum());
5148 return parser
.nameFromSymbol(sym
);
5151 // support __LD, __compact_unwind personality entries which are pointer to personality non-lazy pointer
5152 const pint_t
* content
= (pint_t
*)(this->file().fileContent() + this->_machOSection
->offset() + reloc
->r_address());
5153 pint_t nlPointerAddr
= *content
;
5154 Section
<arm
>* nlSection
= parser
.sectionForAddress(nlPointerAddr
);
5155 if ( nlSection
->type() == ld::Section::typeCode
) {
5156 // personality function is defined in this .o file, so this is a direct reference to it
5157 // atoms may not be constructed yet, so scan symbol table for labels
5158 const char* name
= parser
.scanSymbolTableForAddress(nlPointerAddr
);
5162 uint32_t symIndex
= parser
.symbolIndexFromIndirectSectionAddress(nlPointerAddr
, nlSection
->machoSection());
5163 const macho_nlist
<P
>& nlSymbol
= parser
.symbolFromIndex(symIndex
);
5164 return parser
.nameFromSymbol(nlSymbol
);
5171 template <typename A
>
5172 const char* CUSection
<A
>::personalityName(class Parser
<A
>& parser
, const macho_relocation_info
<P
>* reloc
)
5178 bool CUSection
<x86
>::encodingMeansUseDwarf(compact_unwind_encoding_t enc
)
5180 return ((enc
& UNWIND_X86_MODE_MASK
) == UNWIND_X86_MODE_DWARF
);
5184 bool CUSection
<x86_64
>::encodingMeansUseDwarf(compact_unwind_encoding_t enc
)
5186 return ((enc
& UNWIND_X86_64_MODE_MASK
) == UNWIND_X86_64_MODE_DWARF
);
5189 #if SUPPORT_ARCH_arm_any
5191 bool CUSection
<arm
>::encodingMeansUseDwarf(compact_unwind_encoding_t enc
)
5193 return ((enc
& UNWIND_ARM_MODE_MASK
) == UNWIND_ARM_MODE_DWARF
);
5197 #if SUPPORT_ARCH_arm64
5199 bool CUSection
<arm64
>::encodingMeansUseDwarf(compact_unwind_encoding_t enc
)
5201 return ((enc
& UNWIND_ARM64_MODE_MASK
) == UNWIND_ARM64_MODE_DWARF
);
5206 template <typename A
>
5207 int CUSection
<A
>::infoSorter(const void* l
, const void* r
)
5209 // sort references by symbol index, then address
5210 const Info
* left
= (Info
*)l
;
5211 const Info
* right
= (Info
*)r
;
5212 if ( left
->functionSymbolIndex
== right
->functionSymbolIndex
)
5213 return (left
->functionStartAddress
- right
->functionStartAddress
);
5215 return (left
->functionSymbolIndex
- right
->functionSymbolIndex
);
5218 template <typename A
>
5219 void CUSection
<A
>::parse(class Parser
<A
>& parser
, uint32_t cnt
, Info array
[])
5221 // walk section content and copy to Info array
5222 const macho_compact_unwind_entry
<P
>* const entries
= (macho_compact_unwind_entry
<P
>*)(this->file().fileContent() + this->_machOSection
->offset());
5223 for (uint32_t i
=0; i
< cnt
; ++i
) {
5224 Info
* info
= &array
[i
];
5225 const macho_compact_unwind_entry
<P
>* entry
= &entries
[i
];
5226 info
->functionStartAddress
= entry
->codeStart();
5227 info
->functionSymbolIndex
= 0xFFFFFFFF;
5228 info
->rangeLength
= entry
->codeLen();
5229 info
->compactUnwindInfo
= entry
->compactUnwindInfo();
5230 info
->personality
= NULL
;
5231 info
->lsdaAddress
= entry
->lsda();
5232 info
->function
= NULL
;
5234 if ( (info
->compactUnwindInfo
& UNWIND_PERSONALITY_MASK
) != 0 )
5235 warning("no bits should be set in UNWIND_PERSONALITY_MASK of compact unwind encoding in __LD,__compact_unwind section");
5236 if ( info
->lsdaAddress
!= 0 ) {
5237 info
->compactUnwindInfo
|= UNWIND_HAS_LSDA
;
5241 // scan relocs, extern relocs are needed for personality references (possibly for function/lsda refs??)
5242 const uint32_t sectionSize
= this->_machOSection
->size();
5243 const macho_relocation_info
<P
>* relocs
= (macho_relocation_info
<P
>*)(this->file().fileContent() + this->_machOSection
->reloff());
5244 const macho_relocation_info
<P
>* relocsEnd
= &relocs
[this->_machOSection
->nreloc()];
5245 for (const macho_relocation_info
<P
>* reloc
= relocs
; reloc
< relocsEnd
; ++reloc
) {
5246 if ( reloc
->r_address() & R_SCATTERED
)
5248 if ( reloc
->r_address() > sectionSize
)
5249 throwf("malformed __compact_unwind relocation, offset (0x%08X) is beyond end of section,", reloc
->r_address());
5250 if ( reloc
->r_extern() ) {
5251 // only expect external relocs on some colummns
5252 if ( (reloc
->r_address() % sizeof(macho_compact_unwind_entry
<P
>)) == macho_compact_unwind_entry
<P
>::personalityFieldOffset() ) {
5253 uint32_t entryIndex
= reloc
->r_address() / sizeof(macho_compact_unwind_entry
<P
>);
5254 array
[entryIndex
].personality
= this->personalityName(parser
, reloc
);
5256 else if ( (reloc
->r_address() % sizeof(macho_compact_unwind_entry
<P
>)) == macho_compact_unwind_entry
<P
>::lsdaFieldOffset() ) {
5257 uint32_t entryIndex
= reloc
->r_address() / sizeof(macho_compact_unwind_entry
<P
>);
5258 const macho_nlist
<P
>& lsdaSym
= parser
.symbolFromIndex(reloc
->r_symbolnum());
5259 if ( (lsdaSym
.n_type() & N_TYPE
) == N_SECT
)
5260 array
[entryIndex
].lsdaAddress
= lsdaSym
.n_value();
5262 warning("unexpected extern relocation to lsda in __compact_unwind section");
5264 else if ( (reloc
->r_address() % sizeof(macho_compact_unwind_entry
<P
>)) == macho_compact_unwind_entry
<P
>::codeStartFieldOffset() ) {
5265 uint32_t entryIndex
= reloc
->r_address() / sizeof(macho_compact_unwind_entry
<P
>);
5266 array
[entryIndex
].functionSymbolIndex
= reloc
->r_symbolnum();
5267 array
[entryIndex
].functionStartAddress
+= parser
.symbolFromIndex(reloc
->r_symbolnum()).n_value();
5270 warning("unexpected extern relocation in __compact_unwind section");
5274 if ( (reloc
->r_address() % sizeof(macho_compact_unwind_entry
<P
>)) == macho_compact_unwind_entry
<P
>::personalityFieldOffset() ) {
5275 uint32_t entryIndex
= reloc
->r_address() / sizeof(macho_compact_unwind_entry
<P
>);
5276 array
[entryIndex
].personality
= this->personalityName(parser
, reloc
);
5281 // sort array by function start address so unwind infos will be contiguous for a given function
5282 ::qsort(array
, cnt
, sizeof(Info
), infoSorter
);
5285 template <typename A
>
5286 uint32_t CUSection
<A
>::count()
5288 const macho_section
<P
>* machoSect
= this->machoSection();
5289 if ( (machoSect
->size() % sizeof(macho_compact_unwind_entry
<P
>)) != 0 )
5290 throw "malformed __LD,__compact_unwind section, bad length";
5292 return machoSect
->size() / sizeof(macho_compact_unwind_entry
<P
>);
5295 template <typename A
>
5296 void CUSection
<A
>::makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
& cus
)
5298 Info
* const arrayStart
= cus
.cuArray
;
5299 Info
* const arrayEnd
= &cus
.cuArray
[cus
.cuCount
];
5300 for (Info
* info
=arrayStart
; info
< arrayEnd
; ++info
) {
5301 // find function atom from address
5302 info
->function
= parser
.findAtomByAddress(info
->functionStartAddress
);
5303 // find lsda atom from address
5304 if ( info
->lsdaAddress
!= 0 ) {
5305 info
->lsda
= parser
.findAtomByAddress(info
->lsdaAddress
);
5306 // add lsda subordinate
5307 typename Parser
<A
>::SourceLocation
src(info
->function
, info
->functionStartAddress
- info
->function
->objectAddress());
5308 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindNoneGroupSubordinateLSDA
, info
->lsda
);
5310 if ( info
->personality
!= NULL
) {
5311 // add personality subordinate
5312 typename Parser
<A
>::SourceLocation
src(info
->function
, info
->functionStartAddress
- info
->function
->objectAddress());
5313 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindNoneGroupSubordinatePersonality
, false, info
->personality
);
5319 template <typename A
>
5320 SymboledSection
<A
>::SymboledSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
5321 : Section
<A
>(f
, s
), _type(ld::Atom::typeUnclassified
)
5323 switch ( s
->flags() & SECTION_TYPE
) {
5325 _type
= ld::Atom::typeZeroFill
;
5327 case S_MOD_INIT_FUNC_POINTERS
:
5328 _type
= ld::Atom::typeInitializerPointers
;
5330 case S_MOD_TERM_FUNC_POINTERS
:
5331 _type
= ld::Atom::typeTerminatorPointers
;
5333 case S_THREAD_LOCAL_VARIABLES
:
5334 _type
= ld::Atom::typeTLV
;
5336 case S_THREAD_LOCAL_ZEROFILL
:
5337 _type
= ld::Atom::typeTLVZeroFill
;
5339 case S_THREAD_LOCAL_REGULAR
:
5340 _type
= ld::Atom::typeTLVInitialValue
;
5342 case S_THREAD_LOCAL_INIT_FUNCTION_POINTERS
:
5343 _type
= ld::Atom::typeTLVInitializerPointers
;
5346 if ( strncmp(s
->sectname(), "__gcc_except_tab", 16) == 0 )
5347 _type
= ld::Atom::typeLSDA
;
5348 else if ( this->type() == ld::Section::typeInitializerPointers
)
5349 _type
= ld::Atom::typeInitializerPointers
;
5350 // <rdar://problem/34716321> don't warn about static initializers in dylibs built for profiling
5351 if ( strncmp(s
->sectname(), "__llvm_prf_", 11) == 0 )
5352 this->_file
.setHasllvmProfiling();
5358 template <typename A
>
5359 bool SymboledSection
<A
>::dontDeadStrip()
5362 case ld::Atom::typeInitializerPointers
:
5363 case ld::Atom::typeTerminatorPointers
:
5366 // model an object file without MH_SUBSECTIONS_VIA_SYMBOLS as one in which nothing can be dead stripped
5367 if ( ! this->_file
.canScatterAtoms() )
5370 return Section
<A
>::dontDeadStrip();
5376 template <typename A
>
5377 uint32_t SymboledSection
<A
>::computeAtomCount(class Parser
<A
>& parser
,
5378 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
5379 const struct Parser
<A
>::CFI_CU_InfoArrays
&)
5381 const pint_t startAddr
= this->_machOSection
->addr();
5382 const pint_t endAddr
= startAddr
+ this->_machOSection
->size();
5383 const uint32_t sectNum
= this->sectionNum(parser
);
5388 const macho_nlist
<P
>* sym
;
5389 while ( it
.next(parser
, *this, sectNum
, startAddr
, endAddr
, &addr
, &size
, &sym
) ) {
5392 //fprintf(stderr, "computeAtomCount(%s,%s) => %d\n", this->segmentName(), this->sectionName(), count);
5396 template <typename A
>
5397 uint32_t SymboledSection
<A
>::appendAtoms(class Parser
<A
>& parser
, uint8_t* p
,
5398 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
5399 const struct Parser
<A
>::CFI_CU_InfoArrays
&)
5401 this->_beginAtoms
= (Atom
<A
>*)p
;
5403 //fprintf(stderr, "SymboledSection::appendAtoms() in section %s\n", this->_machOSection->sectname());
5404 const pint_t startAddr
= this->_machOSection
->addr();
5405 const pint_t endAddr
= startAddr
+ this->_machOSection
->size();
5406 const uint32_t sectNum
= this->sectionNum(parser
);
5411 const macho_nlist
<P
>* label
;
5412 while ( it
.next(parser
, *this, sectNum
, startAddr
, endAddr
, &addr
, &size
, &label
) ) {
5413 Atom
<A
>* allocatedSpace
= (Atom
<A
>*)p
;
5414 // is break because of label or CFI?
5415 if ( label
!= NULL
) {
5416 // The size is computed based on the address of the next label (or the end of the section for the last label)
5417 // If there are two labels at the same address, we want them one to be an alias of the other.
5418 // If the label is at the end of a section, it is has zero size, but is not an alias
5419 const bool isAlias
= ( (size
== 0) && (addr
< endAddr
) );
5420 new (allocatedSpace
) Atom
<A
>(*this, parser
, *label
, size
, isAlias
);
5422 this->_hasAliases
= true;
5423 if ( parser
.altEntryFromSymbol(*label
) )
5424 this->_altEntries
.insert(allocatedSpace
);
5427 ld::Atom::SymbolTableInclusion inclusion
= ld::Atom::symbolTableNotIn
;
5428 ld::Atom::ContentType ctype
= this->contentType();
5429 if ( ctype
== ld::Atom::typeLSDA
)
5430 inclusion
= ld::Atom::symbolTableInWithRandomAutoStripLabel
;
5431 new (allocatedSpace
) Atom
<A
>(*this, "anon", addr
, size
, ld::Atom::definitionRegular
, ld::Atom::combineNever
,
5432 ld::Atom::scopeTranslationUnit
, ctype
, inclusion
,
5433 this->dontDeadStrip(), false, false, this->alignmentForAddress(addr
));
5435 p
+= sizeof(Atom
<A
>);
5439 this->_endAtoms
= (Atom
<A
>*)p
;
5445 ld::Atom::SymbolTableInclusion ImplicitSizeSection
<arm64
>::symbolTableInclusion()
5447 return ld::Atom::symbolTableInWithRandomAutoStripLabel
;
5451 template <typename A
>
5452 ld::Atom::SymbolTableInclusion ImplicitSizeSection
<A
>::symbolTableInclusion()
5454 return ld::Atom::symbolTableNotIn
;
5458 template <typename A
>
5459 uint32_t ImplicitSizeSection
<A
>::computeAtomCount(class Parser
<A
>& parser
,
5460 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
5461 const struct Parser
<A
>::CFI_CU_InfoArrays
&)
5464 const macho_section
<P
>* sect
= this->machoSection();
5465 const pint_t startAddr
= sect
->addr();
5466 const pint_t endAddr
= startAddr
+ sect
->size();
5467 for (pint_t addr
= startAddr
; addr
< endAddr
; addr
+= elementSizeAtAddress(addr
) ) {
5468 if ( useElementAt(parser
, it
, addr
) )
5471 if ( it
.fileHasOverlappingSymbols
&& (sect
->size() != 0) && (this->combine(parser
, startAddr
) == ld::Atom::combineByNameAndContent
) ) {
5472 // if there are multiple labels in this section for the same address, then clone them into multi atoms
5473 pint_t prevSymbolAddr
= (pint_t
)(-1);
5474 uint8_t prevSymbolSectNum
= 0;
5475 bool prevIgnore
= false;
5476 for(uint32_t i
=0; i
< it
.sortedSymbolCount
; ++i
) {
5477 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(it
.sortedSymbolIndexes
[i
]);
5478 const pint_t symbolAddr
= sym
.n_value();
5479 const uint8_t symbolSectNum
= sym
.n_sect();
5480 const bool ignore
= this->ignoreLabel(parser
.nameFromSymbol(sym
));
5481 if ( !ignore
&& !prevIgnore
&& (symbolAddr
== prevSymbolAddr
) && (prevSymbolSectNum
== symbolSectNum
) && (symbolSectNum
== this->sectionNum(parser
)) ) {
5484 prevSymbolAddr
= symbolAddr
;
5485 prevSymbolSectNum
= symbolSectNum
;
5486 prevIgnore
= ignore
;
5492 template <typename A
>
5493 uint32_t ImplicitSizeSection
<A
>::appendAtoms(class Parser
<A
>& parser
, uint8_t* p
,
5494 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
5495 const struct Parser
<A
>::CFI_CU_InfoArrays
&)
5497 this->_beginAtoms
= (Atom
<A
>*)p
;
5499 const macho_section
<P
>* sect
= this->machoSection();
5500 const pint_t startAddr
= sect
->addr();
5501 const pint_t endAddr
= startAddr
+ sect
->size();
5502 const uint32_t sectNum
= this->sectionNum(parser
);
5503 //fprintf(stderr, "ImplicitSizeSection::appendAtoms() in section %s\n", sect->sectname());
5507 const macho_nlist
<P
>* foundLabel
;
5508 Atom
<A
>* allocatedSpace
;
5509 while ( it
.next(parser
, *this, sectNum
, startAddr
, endAddr
, &foundAddr
, &size
, &foundLabel
) ) {
5510 if ( foundLabel
!= NULL
) {
5512 pint_t labeledAtomSize
= this->elementSizeAtAddress(foundAddr
);
5513 allocatedSpace
= (Atom
<A
>*)p
;
5514 if ( this->ignoreLabel(parser
.nameFromSymbol(*foundLabel
)) ) {
5516 // <rdar://problem/10018737>
5517 // a size of zero means there is another label at same location
5518 // and we are supposed to ignore this label
5522 //fprintf(stderr, " 0x%08llX make annon, size=%lld\n", (uint64_t)foundAddr, (uint64_t)size);
5523 new (allocatedSpace
) Atom
<A
>(*this, this->unlabeledAtomName(parser
, foundAddr
), foundAddr
,
5524 this->elementSizeAtAddress(foundAddr
), this->definition(),
5525 this->combine(parser
, foundAddr
), this->scopeAtAddress(parser
, foundAddr
),
5526 this->contentType(), this->symbolTableInclusion(),
5527 this->dontDeadStrip(), false, false, this->alignmentForAddress(foundAddr
));
5531 // make named atom for label
5532 //fprintf(stderr, " 0x%08llX make labeled: %s\n", (uint64_t)foundAddr, parser.nameFromSymbol(*foundLabel));
5533 new (allocatedSpace
) Atom
<A
>(*this, parser
, *foundLabel
, labeledAtomSize
);
5537 p
+= sizeof(Atom
<A
>);
5538 foundAddr
+= labeledAtomSize
;
5539 size
-= labeledAtomSize
;
5542 // some number of anonymous atoms
5543 for (pint_t addr
= foundAddr
; addr
< (foundAddr
+size
); addr
+= elementSizeAtAddress(addr
) ) {
5544 // make anon atoms for area before label
5545 if ( this->useElementAt(parser
, it
, addr
) ) {
5546 //fprintf(stderr, " 0x%08llX make annon, size=%lld\n", (uint64_t)addr, (uint64_t)elementSizeAtAddress(addr));
5547 allocatedSpace
= (Atom
<A
>*)p
;
5548 new (allocatedSpace
) Atom
<A
>(*this, this->unlabeledAtomName(parser
, addr
), addr
, this->elementSizeAtAddress(addr
),
5549 this->definition(), this->combine(parser
, addr
), this->scopeAtAddress(parser
, addr
),
5550 this->contentType(), this->symbolTableInclusion(),
5551 this->dontDeadStrip(), false, false, this->alignmentForAddress(addr
));
5553 p
+= sizeof(Atom
<A
>);
5558 this->_endAtoms
= (Atom
<A
>*)p
;
5563 template <typename A
>
5564 bool Literal4Section
<A
>::ignoreLabel(const char* label
) const
5566 return (label
[0] == 'L') || (label
[0] == 'l');
5569 template <typename A
>
5570 unsigned long Literal4Section
<A
>::contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
5572 const uint32_t* literalContent
= (uint32_t*)atom
->contentPointer();
5573 return *literalContent
;
5576 template <typename A
>
5577 bool Literal4Section
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
5578 const ld::IndirectBindingTable
& ind
) const
5580 assert(this->type() == rhs
.section().type());
5581 const uint32_t* literalContent
= (uint32_t*)atom
->contentPointer();
5583 const Atom
<A
>* rhsAtom
= dynamic_cast<const Atom
<A
>*>(&rhs
);
5584 assert(rhsAtom
!= NULL
);
5585 if ( rhsAtom
!= NULL
) {
5586 const uint32_t* rhsLiteralContent
= (uint32_t*)rhsAtom
->contentPointer();
5587 return (*literalContent
== *rhsLiteralContent
);
5593 template <typename A
>
5594 bool Literal8Section
<A
>::ignoreLabel(const char* label
) const
5596 return (label
[0] == 'L') || (label
[0] == 'l');
5599 template <typename A
>
5600 unsigned long Literal8Section
<A
>::contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
5603 const uint64_t* literalContent
= (uint64_t*)atom
->contentPointer();
5604 return *literalContent
;
5606 unsigned long hash
= 5381;
5607 const uint8_t* byteContent
= atom
->contentPointer();
5608 for (int i
=0; i
< 8; ++i
) {
5609 hash
= hash
* 33 + byteContent
[i
];
5615 template <typename A
>
5616 bool Literal8Section
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
5617 const ld::IndirectBindingTable
& ind
) const
5619 if ( rhs
.section().type() != ld::Section::typeLiteral8
)
5621 assert(this->type() == rhs
.section().type());
5622 const uint64_t* literalContent
= (uint64_t*)atom
->contentPointer();
5624 const Atom
<A
>* rhsAtom
= dynamic_cast<const Atom
<A
>*>(&rhs
);
5625 assert(rhsAtom
!= NULL
);
5626 if ( rhsAtom
!= NULL
) {
5627 const uint64_t* rhsLiteralContent
= (uint64_t*)rhsAtom
->contentPointer();
5628 return (*literalContent
== *rhsLiteralContent
);
5633 template <typename A
>
5634 bool Literal16Section
<A
>::ignoreLabel(const char* label
) const
5636 return (label
[0] == 'L') || (label
[0] == 'l');
5639 template <typename A
>
5640 unsigned long Literal16Section
<A
>::contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
5642 unsigned long hash
= 5381;
5643 const uint8_t* byteContent
= atom
->contentPointer();
5644 for (int i
=0; i
< 16; ++i
) {
5645 hash
= hash
* 33 + byteContent
[i
];
5650 template <typename A
>
5651 bool Literal16Section
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
5652 const ld::IndirectBindingTable
& ind
) const
5654 if ( rhs
.section().type() != ld::Section::typeLiteral16
)
5656 assert(this->type() == rhs
.section().type());
5657 const uint64_t* literalContent
= (uint64_t*)atom
->contentPointer();
5659 const Atom
<A
>* rhsAtom
= dynamic_cast<const Atom
<A
>*>(&rhs
);
5660 assert(rhsAtom
!= NULL
);
5661 if ( rhsAtom
!= NULL
) {
5662 const uint64_t* rhsLiteralContent
= (uint64_t*)rhsAtom
->contentPointer();
5663 return ((literalContent
[0] == rhsLiteralContent
[0]) && (literalContent
[1] == rhsLiteralContent
[1]));
5670 template <typename A
>
5671 typename
A::P::uint_t CStringSection
<A
>::elementSizeAtAddress(pint_t addr
)
5673 const macho_section
<P
>* sect
= this->machoSection();
5674 const char* stringContent
= (char*)(this->file().fileContent() + sect
->offset() + addr
- sect
->addr());
5675 return strlen(stringContent
) + 1;
5678 template <typename A
>
5679 bool CStringSection
<A
>::useElementAt(Parser
<A
>& parser
, struct Parser
<A
>::LabelAndCFIBreakIterator
& it
, pint_t addr
)
5684 template <typename A
>
5685 bool CStringSection
<A
>::ignoreLabel(const char* label
) const
5687 return (label
[0] == 'L') || (label
[0] == 'l');
5691 template <typename A
>
5692 Atom
<A
>* CStringSection
<A
>::findAtomByAddress(pint_t addr
)
5694 Atom
<A
>* result
= this->findContentAtomByAddress(addr
, this->_beginAtoms
, this->_endAtoms
);
5698 template <typename A
>
5699 unsigned long CStringSection
<A
>::contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
5701 unsigned long hash
= 5381;
5702 const char* stringContent
= (char*)atom
->contentPointer();
5703 for (const char* s
= stringContent
; *s
!= '\0'; ++s
) {
5704 hash
= hash
* 33 + *s
;
5710 template <typename A
>
5711 bool CStringSection
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
5712 const ld::IndirectBindingTable
& ind
) const
5714 if ( rhs
.section().type() != ld::Section::typeCString
)
5716 assert(this->type() == rhs
.section().type());
5717 assert(strcmp(this->sectionName(), rhs
.section().sectionName())== 0);
5718 assert(strcmp(this->segmentName(), rhs
.section().segmentName())== 0);
5719 const char* stringContent
= (char*)atom
->contentPointer();
5721 const Atom
<A
>* rhsAtom
= dynamic_cast<const Atom
<A
>*>(&rhs
);
5722 assert(rhsAtom
!= NULL
);
5723 if ( rhsAtom
!= NULL
) {
5724 if ( atom
->_size
!= rhsAtom
->_size
)
5726 const char* rhsStringContent
= (char*)rhsAtom
->contentPointer();
5727 return (strcmp(stringContent
, rhsStringContent
) == 0);
5734 ld::Fixup::Kind NonLazyPointerSection
<x86
>::fixupKind()
5736 return ld::Fixup::kindStoreLittleEndian32
;
5740 ld::Fixup::Kind NonLazyPointerSection
<arm
>::fixupKind()
5742 return ld::Fixup::kindStoreLittleEndian32
;
5746 ld::Fixup::Kind NonLazyPointerSection
<arm64
>::fixupKind()
5748 return ld::Fixup::kindStoreLittleEndian64
;
5753 void NonLazyPointerSection
<x86_64
>::makeFixups(class Parser
<x86_64
>& parser
, const struct Parser
<x86_64
>::CFI_CU_InfoArrays
&)
5755 assert(0 && "x86_64 should not have non-lazy-pointer sections in .o files");
5758 template <typename A
>
5759 void NonLazyPointerSection
<A
>::makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
&)
5761 // add references for each NLP atom based on indirect symbol table
5762 const macho_section
<P
>* sect
= this->machoSection();
5763 const pint_t endAddr
= sect
->addr() + sect
->size();
5764 for( pint_t addr
= sect
->addr(); addr
< endAddr
; addr
+= sizeof(pint_t
)) {
5765 typename Parser
<A
>::SourceLocation src
;
5766 typename Parser
<A
>::TargetDesc target
;
5767 src
.atom
= this->findAtomByAddress(addr
);
5768 src
.offsetInAtom
= 0;
5769 uint32_t symIndex
= parser
.symbolIndexFromIndirectSectionAddress(addr
, sect
);
5772 target
.weakImport
= false;
5774 if ( symIndex
== INDIRECT_SYMBOL_LOCAL
) {
5775 // use direct reference for local symbols
5776 const pint_t
* nlpContent
= (pint_t
*)(this->file().fileContent() + sect
->offset() + addr
- sect
->addr());
5777 pint_t targetAddr
= P::getP(*nlpContent
);
5778 target
.atom
= parser
.findAtomByAddress(targetAddr
);
5779 target
.weakImport
= false;
5780 target
.addend
= (targetAddr
- target
.atom
->objectAddress());
5781 // <rdar://problem/8385011> if pointer to thumb function, mask of thumb bit (not an addend of +1)
5782 if ( target
.atom
->isThumb() )
5783 target
.addend
&= (-2);
5784 assert(src
.atom
->combine() == ld::Atom::combineNever
);
5787 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(symIndex
);
5788 // use direct reference for local symbols
5789 if ( ((sym
.n_type() & N_TYPE
) == N_SECT
) && ((sym
.n_type() & N_EXT
) == 0) ) {
5790 parser
.findTargetFromAddressAndSectionNum(sym
.n_value(), sym
.n_sect(), target
);
5791 assert(src
.atom
->combine() == ld::Atom::combineNever
);
5794 target
.name
= parser
.nameFromSymbol(sym
);
5795 target
.weakImport
= parser
.weakImportFromSymbol(sym
);
5796 assert(src
.atom
->combine() == ld::Atom::combineByNameAndReferences
);
5799 parser
.addFixups(src
, this->fixupKind(), target
);
5803 template <typename A
>
5804 ld::Atom::Combine NonLazyPointerSection
<A
>::combine(Parser
<A
>& parser
, pint_t addr
)
5806 const macho_section
<P
>* sect
= this->machoSection();
5807 uint32_t symIndex
= parser
.symbolIndexFromIndirectSectionAddress(addr
, sect
);
5808 if ( symIndex
== INDIRECT_SYMBOL_LOCAL
)
5809 return ld::Atom::combineNever
;
5811 // don't coalesce non-lazy-pointers to local symbols
5812 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(symIndex
);
5813 if ( ((sym
.n_type() & N_TYPE
) == N_SECT
) && ((sym
.n_type() & N_EXT
) == 0) )
5814 return ld::Atom::combineNever
;
5816 return ld::Atom::combineByNameAndReferences
;
5819 template <typename A
>
5820 const char* NonLazyPointerSection
<A
>::targetName(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
)
5822 assert(atom
->combine() == ld::Atom::combineByNameAndReferences
);
5823 assert(atom
->fixupCount() == 1);
5824 ld::Fixup::iterator fit
= atom
->fixupsBegin();
5825 const char* name
= NULL
;
5826 switch ( fit
->binding
) {
5827 case ld::Fixup::bindingByNameUnbound
:
5830 case ld::Fixup::bindingByContentBound
:
5831 name
= fit
->u
.target
->name();
5833 case ld::Fixup::bindingsIndirectlyBound
:
5834 name
= ind
.indirectName(fit
->u
.bindingIndex
);
5839 assert(name
!= NULL
);
5843 template <typename A
>
5844 unsigned long NonLazyPointerSection
<A
>::contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
5846 assert(atom
->combine() == ld::Atom::combineByNameAndReferences
);
5847 unsigned long hash
= 9508;
5848 for (const char* s
= this->targetName(atom
, ind
); *s
!= '\0'; ++s
) {
5849 hash
= hash
* 33 + *s
;
5854 template <typename A
>
5855 bool NonLazyPointerSection
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
5856 const ld::IndirectBindingTable
& indirectBindingTable
) const
5858 if ( rhs
.section().type() != ld::Section::typeNonLazyPointer
)
5860 assert(this->type() == rhs
.section().type());
5861 // there can be many non-lazy pointer in different section names
5862 // we only want to coalesce in same section name
5863 if ( *this != rhs
.section() )
5865 const Atom
<A
>* rhsAtom
= dynamic_cast<const Atom
<A
>*>(&rhs
);
5866 assert(rhsAtom
!= NULL
);
5867 const char* thisName
= this->targetName(atom
, indirectBindingTable
);
5868 const char* rhsName
= this->targetName(rhsAtom
, indirectBindingTable
);
5869 return (strcmp(thisName
, rhsName
) == 0);
5872 template <typename A
>
5873 ld::Atom::Scope NonLazyPointerSection
<A
>::scopeAtAddress(Parser
<A
>& parser
, pint_t addr
)
5875 const macho_section
<P
>* sect
= this->machoSection();
5876 uint32_t symIndex
= parser
.symbolIndexFromIndirectSectionAddress(addr
, sect
);
5877 if ( symIndex
== INDIRECT_SYMBOL_LOCAL
)
5878 return ld::Atom::scopeTranslationUnit
;
5880 return ld::Atom::scopeLinkageUnit
;
5885 template <typename A
>
5886 ld::Atom::Combine TLVPointerSection
<A
>::combine(Parser
<A
>& parser
, pint_t addr
)
5888 return ld::Atom::combineByNameAndReferences
;
5892 void TLVPointerSection
<arm
>::makeFixups(class Parser
<arm
>& parser
, const struct Parser
<arm
>::CFI_CU_InfoArrays
&)
5894 // add references for each thread local pointer atom based on indirect symbol table
5895 const macho_section
<P
>* sect
= this->machoSection();
5896 const pint_t endAddr
= sect
->addr() + sect
->size();
5897 for (pint_t addr
= sect
->addr(); addr
< endAddr
; addr
+= sizeof(pint_t
)) {
5898 typename Parser
<arm
>::SourceLocation src
;
5899 typename Parser
<arm
>::TargetDesc target
;
5900 src
.atom
= this->findAtomByAddress(addr
);
5901 src
.offsetInAtom
= 0;
5902 uint32_t symIndex
= parser
.symbolIndexFromIndirectSectionAddress(addr
, sect
);
5905 target
.weakImport
= false;
5907 if ( symIndex
== INDIRECT_SYMBOL_LOCAL
) {
5908 throwf("unexpected INDIRECT_SYMBOL_LOCAL in section %s", this->sectionName());
5911 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(symIndex
);
5912 // use direct reference for local symbols
5913 if ( ((sym
.n_type() & N_TYPE
) == N_SECT
) && ((sym
.n_type() & N_EXT
) == 0) ) {
5914 throwf("unexpected pointer to local symbol in section %s", this->sectionName());
5917 target
.name
= parser
.nameFromSymbol(sym
);
5918 target
.weakImport
= parser
.weakImportFromSymbol(sym
);
5919 assert(src
.atom
->combine() == ld::Atom::combineByNameAndReferences
);
5922 parser
.addFixups(src
, ld::Fixup::kindStoreLittleEndian32
, target
);
5926 template <typename A
>
5927 void TLVPointerSection
<A
>::makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
&)
5929 assert(0 && "should not have thread-local-pointer sections in .o files");
5933 template <typename A
>
5934 const char* TLVPointerSection
<A
>::targetName(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
, bool* isStatic
)
5936 assert(atom
->combine() == ld::Atom::combineByNameAndReferences
);
5937 assert(atom
->fixupCount() == 1);
5939 ld::Fixup::iterator fit
= atom
->fixupsBegin();
5940 const char* name
= NULL
;
5941 switch ( fit
->binding
) {
5942 case ld::Fixup::bindingByNameUnbound
:
5945 case ld::Fixup::bindingByContentBound
:
5946 name
= fit
->u
.target
->name();
5948 case ld::Fixup::bindingsIndirectlyBound
:
5949 name
= ind
.indirectName(fit
->u
.bindingIndex
);
5951 case ld::Fixup::bindingDirectlyBound
:
5952 name
= fit
->u
.target
->name();
5953 *isStatic
= (fit
->u
.target
->scope() == ld::Atom::scopeTranslationUnit
);
5958 assert(name
!= NULL
);
5962 template <typename A
>
5963 unsigned long TLVPointerSection
<A
>::contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
5965 assert(atom
->combine() == ld::Atom::combineByNameAndReferences
);
5966 unsigned long hash
= 9508;
5968 for (const char* s
= this->targetName(atom
, ind
, &isStatic
); *s
!= '\0'; ++s
) {
5969 hash
= hash
* 33 + *s
;
5974 template <typename A
>
5975 bool TLVPointerSection
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
5976 const ld::IndirectBindingTable
& indirectBindingTable
) const
5978 if ( rhs
.section().type() != ld::Section::typeTLVPointers
)
5980 assert(this->type() == rhs
.section().type());
5981 const Atom
<A
>* rhsAtom
= dynamic_cast<const Atom
<A
>*>(&rhs
);
5982 assert(rhsAtom
!= NULL
);
5985 const char* thisName
= this->targetName(atom
, indirectBindingTable
, &thisIsStatic
);
5986 const char* rhsName
= this->targetName(rhsAtom
, indirectBindingTable
, &rhsIsStatic
);
5987 return !thisIsStatic
&& !rhsIsStatic
&& (strcmp(thisName
, rhsName
) == 0);
5991 template <typename A
>
5992 const uint8_t* CFStringSection
<A
>::targetContent(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
,
5993 ContentType
* ct
, unsigned int* count
)
5995 *ct
= contentUnknown
;
5996 for (ld::Fixup::iterator fit
=atom
->fixupsBegin(), end
=atom
->fixupsEnd(); fit
!= end
; ++fit
) {
5997 const ld::Atom
* targetAtom
= NULL
;
5998 switch ( fit
->binding
) {
5999 case ld::Fixup::bindingByNameUnbound
:
6000 // ignore reference to ___CFConstantStringClassReference
6001 // we are just looking for reference to backing string data
6002 assert(fit
->offsetInAtom
== 0);
6003 assert(strcmp(fit
->u
.name
, "___CFConstantStringClassReference") == 0);
6005 case ld::Fixup::bindingDirectlyBound
:
6006 case ld::Fixup::bindingByContentBound
:
6007 targetAtom
= fit
->u
.target
;
6009 case ld::Fixup::bindingsIndirectlyBound
:
6010 targetAtom
= ind
.indirectAtom(fit
->u
.bindingIndex
);
6013 assert(0 && "bad binding type");
6015 assert(targetAtom
!= NULL
);
6016 const Atom
<A
>* target
= dynamic_cast<const Atom
<A
>*>(targetAtom
);
6017 if ( targetAtom
->section().type() == ld::Section::typeCString
) {
6019 *count
= targetAtom
->size();
6021 else if ( targetAtom
->section().type() == ld::Section::typeUTF16Strings
) {
6023 *count
= (targetAtom
->size()+1)/2; // round up incase of buggy compiler that has only one trailing zero byte
6026 *ct
= contentUnknown
;
6030 return target
->contentPointer();
6036 template <typename A
>
6037 unsigned long CFStringSection
<A
>::contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
6039 // base hash of CFString on hash of cstring it wraps
6042 unsigned int charCount
;
6043 const uint8_t* content
= this->targetContent(atom
, ind
, &cType
, &charCount
);
6047 for (const char* s
= (char*)content
; *s
!= '\0'; ++s
) {
6048 hash
= hash
* 33 + *s
;
6053 --charCount
; // don't add last 0x0000 to hash because some buggy compilers only have trailing single byte
6054 for (const uint16_t* s
= (uint16_t*)content
; charCount
> 0; ++s
, --charCount
) {
6055 hash
= hash
* 1025 + *s
;
6058 case contentUnknown
:
6059 // <rdar://problem/14134211> For malformed CFStrings, hash to address of atom so they have unique hashes
6060 return ULONG_MAX
- (unsigned long)(atom
);
6066 template <typename A
>
6067 bool CFStringSection
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
6068 const ld::IndirectBindingTable
& indirectBindingTable
) const
6072 if ( rhs
.section().type() != ld::Section::typeCFString
)
6074 assert(this->type() == rhs
.section().type());
6075 assert(strcmp(this->sectionName(), "__cfstring") == 0);
6077 ContentType thisType
;
6078 unsigned int charCount
;
6079 const uint8_t* cstringContent
= this->targetContent(atom
, indirectBindingTable
, &thisType
, &charCount
);
6080 ContentType rhsType
;
6081 const Atom
<A
>* rhsAtom
= dynamic_cast<const Atom
<A
>*>(&rhs
);
6082 assert(rhsAtom
!= NULL
);
6083 unsigned int rhsCharCount
;
6084 const uint8_t* rhsStringContent
= this->targetContent(rhsAtom
, indirectBindingTable
, &rhsType
, &rhsCharCount
);
6086 if ( thisType
!= rhsType
)
6089 if ( thisType
== contentUnknown
)
6092 if ( rhsType
== contentUnknown
)
6095 // no need to compare content of pointers are already the same
6096 if ( cstringContent
== rhsStringContent
)
6099 // no need to compare content if size is different
6100 if ( charCount
!= rhsCharCount
)
6103 switch ( thisType
) {
6105 return (strcmp((char*)cstringContent
, (char*)rhsStringContent
) == 0);
6108 const uint16_t* cstringContent16
= (uint16_t*)cstringContent
;
6109 const uint16_t* rhsStringContent16
= (uint16_t*)rhsStringContent
;
6110 for (unsigned int i
= 0; i
< charCount
; ++i
) {
6111 if ( cstringContent16
[i
] != rhsStringContent16
[i
] )
6116 case contentUnknown
:
6123 template <typename A
>
6124 typename
A::P::uint_t ObjC1ClassSection
<A
>::elementSizeAtAddress(pint_t addr
)
6126 // nominal size for each class is 48 bytes, but sometimes the compiler
6127 // over aligns and there is padding after class data
6128 const macho_section
<P
>* sct
= this->machoSection();
6129 uint32_t align
= 1 << sct
->align();
6130 uint32_t size
= ((12 * sizeof(pint_t
)) + align
-1) & (-align
);
6134 template <typename A
>
6135 const char* ObjC1ClassSection
<A
>::unlabeledAtomName(Parser
<A
>& parser
, pint_t addr
)
6137 // 8-bytes into class object is pointer to class name
6138 const macho_section
<P
>* sct
= this->machoSection();
6139 uint32_t classObjcFileOffset
= sct
->offset() - sct
->addr() + addr
;
6140 const uint8_t* mappedFileContent
= this->file().fileContent();
6141 pint_t nameAddr
= P::getP(*((pint_t
*)(mappedFileContent
+classObjcFileOffset
+2*sizeof(pint_t
))));
6143 // find section containing string address to get string bytes
6144 const macho_section
<P
>* const sections
= parser
.firstMachOSection();
6145 const uint32_t sectionCount
= parser
.machOSectionCount();
6146 for (uint32_t i
=0; i
< sectionCount
; ++i
) {
6147 const macho_section
<P
>* aSect
= §ions
[i
];
6148 if ( (aSect
->addr() <= nameAddr
) && (nameAddr
< (aSect
->addr()+aSect
->size())) ) {
6149 assert((aSect
->flags() & SECTION_TYPE
) == S_CSTRING_LITERALS
);
6150 uint32_t nameFileOffset
= aSect
->offset() - aSect
->addr() + nameAddr
;
6151 const char* name
= (char*)mappedFileContent
+ nameFileOffset
;
6152 // spin through symbol table to find absolute symbol corresponding to this class
6153 for (uint32_t s
=0; s
< parser
.symbolCount(); ++s
) {
6154 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(s
);
6155 if ( (sym
.n_type() & N_TYPE
) != N_ABS
)
6157 const char* absName
= parser
.nameFromSymbol(sym
);
6158 if ( strncmp(absName
, ".objc_class_name_", 17) == 0 ) {
6159 if ( strcmp(&absName
[17], name
) == 0 )
6163 assert(0 && "obj class name not found in symbol table");
6166 assert(0 && "obj class name not found");
6167 return "unknown objc class";
6171 template <typename A
>
6172 const char* ObjC2ClassRefsSection
<A
>::targetClassName(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
6174 assert(atom
->fixupCount() == 1);
6175 ld::Fixup::iterator fit
= atom
->fixupsBegin();
6176 const char* className
= NULL
;
6177 switch ( fit
->binding
) {
6178 case ld::Fixup::bindingByNameUnbound
:
6179 className
= fit
->u
.name
;
6181 case ld::Fixup::bindingDirectlyBound
:
6182 case ld::Fixup::bindingByContentBound
:
6183 className
= fit
->u
.target
->name();
6185 case ld::Fixup::bindingsIndirectlyBound
:
6186 className
= ind
.indirectName(fit
->u
.bindingIndex
);
6189 assert(0 && "unsupported binding in objc2 class ref section");
6191 assert(className
!= NULL
);
6196 template <typename A
>
6197 unsigned long ObjC2ClassRefsSection
<A
>::contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
6199 unsigned long hash
= 978;
6200 for (const char* s
= targetClassName(atom
, ind
); *s
!= '\0'; ++s
) {
6201 hash
= hash
* 33 + *s
;
6206 template <typename A
>
6207 bool ObjC2ClassRefsSection
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
6208 const ld::IndirectBindingTable
& indirectBindingTable
) const
6210 assert(this->type() == rhs
.section().type());
6211 const Atom
<A
>* rhsAtom
= dynamic_cast<const Atom
<A
>*>(&rhs
);
6212 assert(rhsAtom
!= NULL
);
6213 const char* thisClassName
= targetClassName(atom
, indirectBindingTable
);
6214 const char* rhsClassName
= targetClassName(rhsAtom
, indirectBindingTable
);
6215 return (strcmp(thisClassName
, rhsClassName
) == 0);
6219 template <typename A
>
6220 const char* Objc1ClassReferences
<A
>::targetCString(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
6222 assert(atom
->fixupCount() == 2);
6223 ld::Fixup::iterator fit
= atom
->fixupsBegin();
6224 if ( fit
->kind
== ld::Fixup::kindSetTargetAddress
)
6226 const ld::Atom
* targetAtom
= NULL
;
6227 switch ( fit
->binding
) {
6228 case ld::Fixup::bindingByContentBound
:
6229 targetAtom
= fit
->u
.target
;
6231 case ld::Fixup::bindingsIndirectlyBound
:
6232 targetAtom
= ind
.indirectAtom(fit
->u
.bindingIndex
);
6233 if ( targetAtom
== NULL
) {
6234 fprintf(stderr
, "missing target named %s\n", ind
.indirectName(fit
->u
.bindingIndex
));
6240 assert(targetAtom
!= NULL
);
6241 const Atom
<A
>* target
= dynamic_cast<const Atom
<A
>*>(targetAtom
);
6242 assert(target
!= NULL
);
6243 return (char*)target
->contentPointer();
6247 template <typename A
>
6248 const char* PointerToCStringSection
<A
>::targetCString(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
6250 assert(atom
->fixupCount() == 1);
6251 ld::Fixup::iterator fit
= atom
->fixupsBegin();
6252 const ld::Atom
* targetAtom
= NULL
;
6253 switch ( fit
->binding
) {
6254 case ld::Fixup::bindingByContentBound
:
6255 targetAtom
= fit
->u
.target
;
6257 case ld::Fixup::bindingsIndirectlyBound
:
6258 targetAtom
= ind
.indirectAtom(fit
->u
.bindingIndex
);
6260 case ld::Fixup::bindingDirectlyBound
:
6261 targetAtom
= fit
->u
.target
;
6264 assert(0 && "unsupported reference to selector");
6266 assert(targetAtom
!= NULL
);
6267 const Atom
<A
>* target
= dynamic_cast<const Atom
<A
>*>(targetAtom
);
6268 assert(target
!= NULL
);
6269 assert(target
->contentType() == ld::Atom::typeCString
);
6270 return (char*)target
->contentPointer();
6273 template <typename A
>
6274 unsigned long PointerToCStringSection
<A
>::contentHash(const class Atom
<A
>* atom
,
6275 const ld::IndirectBindingTable
& indirectBindingTable
) const
6277 // make hash from section name and target cstring name
6278 unsigned long hash
= 123;
6279 for (const char* s
= this->sectionName(); *s
!= '\0'; ++s
) {
6280 hash
= hash
* 33 + *s
;
6282 for (const char* s
= this->targetCString(atom
, indirectBindingTable
); *s
!= '\0'; ++s
) {
6283 hash
= hash
* 33 + *s
;
6288 template <typename A
>
6289 bool PointerToCStringSection
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
6290 const ld::IndirectBindingTable
& indirectBindingTable
) const
6292 assert(this->type() == rhs
.section().type());
6293 // there can be pointers-to-cstrings in different section names
6294 // we only want to coalesce in same section name
6295 if ( *this != rhs
.section() )
6298 // get string content for this
6299 const char* cstringContent
= this->targetCString(atom
, indirectBindingTable
);
6300 const Atom
<A
>* rhsAtom
= dynamic_cast<const Atom
<A
>*>(&rhs
);
6301 assert(rhsAtom
!= NULL
);
6302 const char* rhsCstringContent
= this->targetCString(rhsAtom
, indirectBindingTable
);
6304 assert(cstringContent
!= NULL
);
6305 assert(rhsCstringContent
!= NULL
);
6306 return (strcmp(cstringContent
, rhsCstringContent
) == 0);
6311 template <typename A
>
6312 unsigned long UTF16StringSection
<A
>::contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
6314 unsigned long hash
= 5381;
6315 const uint16_t* stringContent
= (uint16_t*)atom
->contentPointer();
6316 // some buggy compilers end utf16 data with single byte, so don't use last word in hash computation
6317 unsigned int count
= (atom
->size()/2) - 1;
6318 for (const uint16_t* s
= stringContent
; count
> 0; ++s
, --count
) {
6319 hash
= hash
* 33 + *s
;
6324 template <typename A
>
6325 bool UTF16StringSection
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
6326 const ld::IndirectBindingTable
& ind
) const
6328 if ( rhs
.section().type() != ld::Section::typeUTF16Strings
)
6341 uint32_t Section
<x86_64
>::x86_64PcRelOffset(uint8_t r_type
)
6344 case X86_64_RELOC_SIGNED
:
6346 case X86_64_RELOC_SIGNED_1
:
6348 case X86_64_RELOC_SIGNED_2
:
6350 case X86_64_RELOC_SIGNED_4
:
6358 bool Section
<x86_64
>::addRelocFixup(class Parser
<x86_64
>& parser
, const macho_relocation_info
<P
>* reloc
)
6360 const macho_section
<P
>* sect
= this->machoSection();
6361 if ( sect
== NULL
) {
6362 warning("malformed mach-o, relocations not supported on section %s", this->sectionName());
6365 uint64_t srcAddr
= sect
->addr() + reloc
->r_address();
6366 Parser
<x86_64
>::SourceLocation src
;
6367 Parser
<x86_64
>::TargetDesc target
;
6368 Parser
<x86_64
>::TargetDesc toTarget
;
6369 src
.atom
= this->findAtomByAddress(srcAddr
);
6370 if ( src
.atom
== NULL
)
6371 throwf("malformed mach-o, reloc addr 0x%llX not in any atom", srcAddr
);
6372 src
.offsetInAtom
= srcAddr
- src
.atom
->_objAddress
;
6373 const uint8_t* fixUpPtr
= file().fileContent() + sect
->offset() + reloc
->r_address();
6374 uint64_t contentValue
= 0;
6375 const macho_relocation_info
<x86_64::P
>* nextReloc
= &reloc
[1];
6376 bool result
= false;
6377 bool useDirectBinding
;
6378 switch ( reloc
->r_length() ) {
6380 contentValue
= *fixUpPtr
;
6383 contentValue
= (int64_t)(int16_t)E::get16(*((uint16_t*)fixUpPtr
));
6386 contentValue
= (int64_t)(int32_t)E::get32(*((uint32_t*)fixUpPtr
));
6389 contentValue
= E::get64(*((uint64_t*)fixUpPtr
));
6394 target
.weakImport
= false;
6396 if ( reloc
->r_extern() ) {
6397 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(reloc
->r_symbolnum());
6398 // use direct reference for local symbols
6399 if ( ((sym
.n_type() & N_TYPE
) == N_SECT
) && (((sym
.n_type() & N_EXT
) == 0) || (parser
.nameFromSymbol(sym
)[0] == 'L')) ) {
6400 parser
.findTargetFromAddressAndSectionNum(sym
.n_value(), sym
.n_sect(), target
);
6401 target
.addend
+= contentValue
;
6404 target
.name
= parser
.nameFromSymbol(sym
);
6405 target
.weakImport
= parser
.weakImportFromSymbol(sym
);
6406 target
.addend
= contentValue
;
6408 // cfstrings should always use direct reference to backing store
6409 if ( (this->type() == ld::Section::typeCFString
) && (src
.offsetInAtom
!= 0) ) {
6410 parser
.findTargetFromAddressAndSectionNum(sym
.n_value(), sym
.n_sect(), target
);
6411 target
.addend
= contentValue
;
6415 if ( reloc
->r_pcrel() )
6416 contentValue
+= srcAddr
+ x86_64PcRelOffset(reloc
->r_type());
6417 parser
.findTargetFromAddressAndSectionNum(contentValue
, reloc
->r_symbolnum(), target
);
6419 switch ( reloc
->r_type() ) {
6420 case X86_64_RELOC_UNSIGNED
:
6421 if ( reloc
->r_pcrel() )
6422 throw "pcrel and X86_64_RELOC_UNSIGNED not supported";
6423 switch ( reloc
->r_length() ) {
6426 throw "length < 2 and X86_64_RELOC_UNSIGNED not supported";
6428 parser
.addFixups(src
, ld::Fixup::kindStoreLittleEndian32
, target
);
6431 parser
.addFixups(src
, ld::Fixup::kindStoreLittleEndian64
, target
);
6435 case X86_64_RELOC_SIGNED
:
6436 case X86_64_RELOC_SIGNED_1
:
6437 case X86_64_RELOC_SIGNED_2
:
6438 case X86_64_RELOC_SIGNED_4
:
6439 if ( ! reloc
->r_pcrel() )
6440 throw "not pcrel and X86_64_RELOC_SIGNED* not supported";
6441 if ( reloc
->r_length() != 2 )
6442 throw "length != 2 and X86_64_RELOC_SIGNED* not supported";
6443 switch ( reloc
->r_type() ) {
6444 case X86_64_RELOC_SIGNED
:
6445 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel32
, target
);
6447 case X86_64_RELOC_SIGNED_1
:
6448 if ( reloc
->r_extern() )
6450 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel32_1
, target
);
6452 case X86_64_RELOC_SIGNED_2
:
6453 if ( reloc
->r_extern() )
6455 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel32_2
, target
);
6457 case X86_64_RELOC_SIGNED_4
:
6458 if ( reloc
->r_extern() )
6460 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel32_4
, target
);
6464 case X86_64_RELOC_BRANCH
:
6465 if ( ! reloc
->r_pcrel() )
6466 throw "not pcrel and X86_64_RELOC_BRANCH not supported";
6467 switch ( reloc
->r_length() ) {
6469 if ( (target
.name
!= NULL
) && (strncmp(target
.name
, "___dtrace_probe$", 16) == 0) ) {
6470 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindStoreX86DtraceCallSiteNop
, false, target
.name
);
6471 parser
.addDtraceExtraInfos(src
, &target
.name
[16]);
6473 else if ( (target
.name
!= NULL
) && (strncmp(target
.name
, "___dtrace_isenabled$", 20) == 0) ) {
6474 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindStoreX86DtraceIsEnableSiteClear
, false, target
.name
);
6475 parser
.addDtraceExtraInfos(src
, &target
.name
[20]);
6478 parser
.addFixups(src
, ld::Fixup::kindStoreX86BranchPCRel32
, target
);
6482 parser
.addFixups(src
, ld::Fixup::kindStoreX86BranchPCRel8
, target
);
6485 throwf("length=%d and X86_64_RELOC_BRANCH not supported", reloc
->r_length());
6488 case X86_64_RELOC_GOT
:
6489 if ( ! reloc
->r_extern() )
6490 throw "not extern and X86_64_RELOC_GOT not supported";
6491 if ( ! reloc
->r_pcrel() )
6492 throw "not pcrel and X86_64_RELOC_GOT not supported";
6493 if ( reloc
->r_length() != 2 )
6494 throw "length != 2 and X86_64_RELOC_GOT not supported";
6495 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel32GOT
, target
);
6497 case X86_64_RELOC_GOT_LOAD
:
6498 if ( ! reloc
->r_extern() )
6499 throw "not extern and X86_64_RELOC_GOT_LOAD not supported";
6500 if ( ! reloc
->r_pcrel() )
6501 throw "not pcrel and X86_64_RELOC_GOT_LOAD not supported";
6502 if ( reloc
->r_length() != 2 )
6503 throw "length != 2 and X86_64_RELOC_GOT_LOAD not supported";
6504 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel32GOTLoad
, target
);
6506 case X86_64_RELOC_SUBTRACTOR
:
6507 if ( reloc
->r_pcrel() )
6508 throw "X86_64_RELOC_SUBTRACTOR cannot be pc-relative";
6509 if ( reloc
->r_length() < 2 )
6510 throw "X86_64_RELOC_SUBTRACTOR must have r_length of 2 or 3";
6511 if ( !reloc
->r_extern() )
6512 throw "X86_64_RELOC_SUBTRACTOR must have r_extern=1";
6513 if ( nextReloc
->r_type() != X86_64_RELOC_UNSIGNED
)
6514 throw "X86_64_RELOC_SUBTRACTOR must be followed by X86_64_RELOC_UNSIGNED";
6516 if ( nextReloc
->r_pcrel() )
6517 throw "X86_64_RELOC_UNSIGNED following a X86_64_RELOC_SUBTRACTOR cannot be pc-relative";
6518 if ( nextReloc
->r_length() != reloc
->r_length() )
6519 throw "X86_64_RELOC_UNSIGNED following a X86_64_RELOC_SUBTRACTOR must have same r_length";
6520 if ( nextReloc
->r_extern() ) {
6521 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(nextReloc
->r_symbolnum());
6522 // use direct reference for local symbols
6523 if ( ((sym
.n_type() & N_TYPE
) == N_SECT
) && (((sym
.n_type() & N_EXT
) == 0) || (parser
.nameFromSymbol(sym
)[0] == 'L')) ) {
6524 parser
.findTargetFromAddressAndSectionNum(sym
.n_value(), sym
.n_sect(), toTarget
);
6525 toTarget
.addend
= contentValue
;
6526 useDirectBinding
= true;
6529 toTarget
.name
= parser
.nameFromSymbol(sym
);
6530 toTarget
.weakImport
= parser
.weakImportFromSymbol(sym
);
6531 toTarget
.addend
= contentValue
;
6532 useDirectBinding
= false;
6536 parser
.findTargetFromAddressAndSectionNum(contentValue
, nextReloc
->r_symbolnum(), toTarget
);
6537 useDirectBinding
= (toTarget
.atom
->scope() == ld::Atom::scopeTranslationUnit
) || ((toTarget
.atom
->combine() == ld::Atom::combineByNameAndContent
) || (toTarget
.atom
->combine() == ld::Atom::combineByNameAndReferences
));
6539 if ( useDirectBinding
) {
6540 if ( (toTarget
.atom
->combine() == ld::Atom::combineByNameAndContent
) || (toTarget
.atom
->combine() == ld::Atom::combineByNameAndReferences
) )
6541 parser
.addFixup(src
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, toTarget
.atom
);
6543 parser
.addFixup(src
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, toTarget
.atom
);
6546 parser
.addFixup(src
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, toTarget
.weakImport
, toTarget
.name
);
6547 parser
.addFixup(src
, ld::Fixup::k2of4
, ld::Fixup::kindAddAddend
, toTarget
.addend
);
6548 if ( target
.atom
== NULL
)
6549 parser
.addFixup(src
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractTargetAddress
, false, target
.name
);
6551 parser
.addFixup(src
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractTargetAddress
, target
.atom
);
6552 if ( reloc
->r_length() == 2 )
6553 parser
.addFixup(src
, ld::Fixup::k4of4
, ld::Fixup::kindStoreLittleEndian32
);
6555 parser
.addFixup(src
, ld::Fixup::k4of4
, ld::Fixup::kindStoreLittleEndian64
);
6557 case X86_64_RELOC_TLV
:
6558 if ( ! reloc
->r_extern() )
6559 throw "not extern and X86_64_RELOC_TLV not supported";
6560 if ( ! reloc
->r_pcrel() )
6561 throw "not pcrel and X86_64_RELOC_TLV not supported";
6562 if ( reloc
->r_length() != 2 )
6563 throw "length != 2 and X86_64_RELOC_TLV not supported";
6564 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel32TLVLoad
, target
);
6567 throwf("unknown relocation type %d", reloc
->r_type());
6575 bool Section
<x86
>::addRelocFixup(class Parser
<x86
>& parser
, const macho_relocation_info
<P
>* reloc
)
6577 const macho_section
<P
>* sect
= this->machoSection();
6579 const uint8_t* fixUpPtr
;
6580 uint32_t contentValue
= 0;
6581 ld::Fixup::Kind kind
= ld::Fixup::kindNone
;
6582 Parser
<x86
>::SourceLocation src
;
6583 Parser
<x86
>::TargetDesc target
;
6585 if ( (reloc
->r_address() & R_SCATTERED
) == 0 ) {
6586 srcAddr
= sect
->addr() + reloc
->r_address();
6587 src
.atom
= this->findAtomByAddress(srcAddr
);
6588 src
.offsetInAtom
= srcAddr
- src
.atom
->_objAddress
;
6589 fixUpPtr
= file().fileContent() + sect
->offset() + reloc
->r_address();
6590 switch ( reloc
->r_type() ) {
6591 case GENERIC_RELOC_VANILLA
:
6592 switch ( reloc
->r_length() ) {
6594 contentValue
= (int32_t)(int8_t)*fixUpPtr
;
6595 if ( reloc
->r_pcrel() ) {
6596 kind
= ld::Fixup::kindStoreX86BranchPCRel8
;
6597 contentValue
+= srcAddr
+ sizeof(uint8_t);
6600 throw "r_length=0 and r_pcrel=0 not supported";
6603 contentValue
= (int32_t)(int16_t)E::get16(*((uint16_t*)fixUpPtr
));
6604 if ( reloc
->r_pcrel() ) {
6605 kind
= ld::Fixup::kindStoreX86PCRel16
;
6606 contentValue
+= srcAddr
+ sizeof(uint16_t);
6609 kind
= ld::Fixup::kindStoreLittleEndian16
;
6612 contentValue
= E::get32(*((uint32_t*)fixUpPtr
));
6613 if ( reloc
->r_pcrel() ) {
6614 kind
= ld::Fixup::kindStoreX86BranchPCRel32
;
6615 contentValue
+= srcAddr
+ sizeof(uint32_t);
6618 kind
= ld::Fixup::kindStoreLittleEndian32
;
6621 throw "r_length=3 not supported";
6623 if ( reloc
->r_extern() ) {
6625 const macho_nlist
<P
>& targetSymbol
= parser
.symbolFromIndex(reloc
->r_symbolnum());
6626 target
.name
= parser
.nameFromSymbol(targetSymbol
);
6627 target
.weakImport
= parser
.weakImportFromSymbol(targetSymbol
);
6628 target
.addend
= (int32_t)contentValue
;
6631 parser
.findTargetFromAddressAndSectionNum(contentValue
, reloc
->r_symbolnum(), target
);
6633 if ( (kind
== ld::Fixup::kindStoreX86BranchPCRel32
) && (target
.name
!= NULL
) ) {
6634 if ( strncmp(target
.name
, "___dtrace_probe$", 16) == 0 ) {
6635 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindStoreX86DtraceCallSiteNop
, false, target
.name
);
6636 parser
.addDtraceExtraInfos(src
, &target
.name
[16]);
6639 else if ( strncmp(target
.name
, "___dtrace_isenabled$", 20) == 0 ) {
6640 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindStoreX86DtraceIsEnableSiteClear
, false, target
.name
);
6641 parser
.addDtraceExtraInfos(src
, &target
.name
[20]);
6645 parser
.addFixups(src
, kind
, target
);
6648 case GENERIC_RLEOC_TLV
:
6650 if ( !reloc
->r_extern() )
6651 throw "r_extern=0 and r_type=GENERIC_RLEOC_TLV not supported";
6652 if ( reloc
->r_length() != 2 )
6653 throw "r_length!=2 and r_type=GENERIC_RLEOC_TLV not supported";
6654 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(reloc
->r_symbolnum());
6655 // use direct reference for local symbols
6656 if ( ((sym
.n_type() & N_TYPE
) == N_SECT
) && ((sym
.n_type() & N_EXT
) == 0) ) {
6657 parser
.findTargetFromAddressAndSectionNum(sym
.n_value(), sym
.n_sect(), target
);
6661 target
.name
= parser
.nameFromSymbol(sym
);
6662 target
.weakImport
= parser
.weakImportFromSymbol(sym
);
6664 target
.addend
= (int64_t)(int32_t)E::get32(*((uint32_t*)fixUpPtr
));
6665 if ( reloc
->r_pcrel() ) {
6666 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel32TLVLoad
, target
);
6669 parser
.addFixups(src
, ld::Fixup::kindStoreX86Abs32TLVLoad
, target
);
6675 throwf("unsupported i386 relocation type (%d)", reloc
->r_type());
6679 // scattered relocation
6680 const macho_scattered_relocation_info
<P
>* sreloc
= (macho_scattered_relocation_info
<P
>*)reloc
;
6681 srcAddr
= sect
->addr() + sreloc
->r_address();
6682 src
.atom
= this->findAtomByAddress(srcAddr
);
6683 assert(src
.atom
!= NULL
);
6684 src
.offsetInAtom
= srcAddr
- src
.atom
->_objAddress
;
6685 fixUpPtr
= file().fileContent() + sect
->offset() + sreloc
->r_address();
6686 uint32_t relocValue
= sreloc
->r_value();
6687 bool result
= false;
6688 // file format allows pair to be scattered or not
6689 const macho_scattered_relocation_info
<P
>* nextSReloc
= &sreloc
[1];
6690 const macho_relocation_info
<P
>* nextReloc
= &reloc
[1];
6691 bool nextRelocIsPair
= false;
6692 uint32_t nextRelocAddress
= 0;
6693 uint32_t nextRelocValue
= 0;
6694 if ( (nextReloc
->r_address() & R_SCATTERED
) == 0 ) {
6695 if ( nextReloc
->r_type() == GENERIC_RELOC_PAIR
) {
6696 nextRelocIsPair
= true;
6697 nextRelocAddress
= nextReloc
->r_address();
6698 result
= true; // iterator should skip next reloc, since we've consumed it here
6702 if ( nextSReloc
->r_type() == GENERIC_RELOC_PAIR
) {
6703 nextRelocIsPair
= true;
6704 nextRelocAddress
= nextSReloc
->r_address();
6705 nextRelocValue
= nextSReloc
->r_value();
6708 switch (sreloc
->r_type()) {
6709 case GENERIC_RELOC_VANILLA
:
6710 // with a scattered relocation we get both the target (sreloc->r_value()) and the target+offset (*fixUpPtr)
6711 target
.atom
= parser
.findAtomByAddress(relocValue
);
6712 if ( sreloc
->r_pcrel() ) {
6713 switch ( sreloc
->r_length() ) {
6715 contentValue
= srcAddr
+ 1 + *fixUpPtr
;
6716 target
.addend
= (int32_t)contentValue
- (int32_t)relocValue
;
6717 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel8
, target
);
6720 contentValue
= srcAddr
+ 2 + LittleEndian::get16(*((uint16_t*)fixUpPtr
));
6721 target
.addend
= (int32_t)contentValue
- (int32_t)relocValue
;
6722 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel16
, target
);
6725 contentValue
= srcAddr
+ 4 + LittleEndian::get32(*((uint32_t*)fixUpPtr
));
6726 target
.addend
= (int32_t)contentValue
- (int32_t)relocValue
;
6727 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel32
, target
);
6730 throw "unsupported r_length=3 for scattered pc-rel vanilla reloc";
6735 if ( sreloc
->r_length() != 2 )
6736 throwf("unsupported r_length=%d for scattered vanilla reloc", sreloc
->r_length());
6737 contentValue
= LittleEndian::get32(*((uint32_t*)fixUpPtr
));
6738 target
.addend
= (int32_t)contentValue
- (int32_t)(target
.atom
->objectAddress());
6739 parser
.addFixups(src
, ld::Fixup::kindStoreLittleEndian32
, target
);
6742 case GENERIC_RELOC_SECTDIFF
:
6743 case GENERIC_RELOC_LOCAL_SECTDIFF
:
6745 if ( !nextRelocIsPair
)
6746 throw "GENERIC_RELOC_SECTDIFF missing following pair";
6747 switch ( sreloc
->r_length() ) {
6750 throw "bad length for GENERIC_RELOC_SECTDIFF";
6752 contentValue
= (int32_t)(int16_t)LittleEndian::get16(*((uint16_t*)fixUpPtr
));
6753 kind
= ld::Fixup::kindStoreLittleEndian16
;
6756 contentValue
= LittleEndian::get32(*((uint32_t*)fixUpPtr
));
6757 kind
= ld::Fixup::kindStoreLittleEndian32
;
6760 Atom
<x86
>* fromAtom
= parser
.findAtomByAddress(nextRelocValue
);
6761 uint32_t offsetInFrom
= nextRelocValue
- fromAtom
->_objAddress
;
6762 parser
.findTargetFromAddress(sreloc
->r_value(), target
);
6763 // check for addend encoded in the section content
6764 int64_t addend
= (int32_t)contentValue
- (int32_t)(sreloc
->r_value() - nextRelocValue
);
6766 // switch binding base on coalescing
6767 if ( target
.atom
== NULL
) {
6768 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, false, target
.name
);
6770 else if ( target
.atom
->scope() == ld::Atom::scopeTranslationUnit
) {
6771 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, target
.atom
);
6773 else if ( (target
.atom
->combine() == ld::Atom::combineByNameAndContent
) || (target
.atom
->combine() == ld::Atom::combineByNameAndReferences
) ) {
6774 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, target
.atom
);
6777 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, false, target
.atom
->name());
6779 parser
.addFixup(src
, ld::Fixup::k2of5
, ld::Fixup::kindAddAddend
, target
.addend
);
6780 parser
.addFixup(src
, ld::Fixup::k3of5
, ld::Fixup::kindSubtractTargetAddress
, fromAtom
);
6781 parser
.addFixup(src
, ld::Fixup::k4of5
, ld::Fixup::kindSubtractAddend
, offsetInFrom
-addend
);
6782 parser
.addFixup(src
, ld::Fixup::k5of5
, kind
);
6785 // switch binding base on coalescing
6786 if ( target
.atom
== NULL
) {
6787 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, false, target
.name
);
6789 else if ( target
.atom
->scope() == ld::Atom::scopeTranslationUnit
) {
6790 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, target
.atom
);
6792 else if ( (target
.atom
->combine() == ld::Atom::combineByNameAndContent
) || (target
.atom
->combine() == ld::Atom::combineByNameAndReferences
) ) {
6793 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, target
.atom
);
6796 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, false, target
.atom
->name());
6798 parser
.addFixup(src
, ld::Fixup::k2of5
, ld::Fixup::kindAddAddend
, target
.addend
+addend
);
6799 parser
.addFixup(src
, ld::Fixup::k3of5
, ld::Fixup::kindSubtractTargetAddress
, fromAtom
);
6800 parser
.addFixup(src
, ld::Fixup::k4of5
, ld::Fixup::kindSubtractAddend
, offsetInFrom
);
6801 parser
.addFixup(src
, ld::Fixup::k5of5
, kind
);
6814 #if SUPPORT_ARCH_arm_any
6816 bool Section
<arm
>::addRelocFixup(class Parser
<arm
>& parser
, const macho_relocation_info
<P
>* reloc
)
6818 const macho_section
<P
>* sect
= this->machoSection();
6819 bool result
= false;
6823 int32_t displacement
= 0;
6824 uint32_t instruction
= 0;
6825 pint_t contentValue
= 0;
6826 Parser
<arm
>::SourceLocation src
;
6827 Parser
<arm
>::TargetDesc target
;
6828 const macho_relocation_info
<P
>* nextReloc
;
6830 if ( (reloc
->r_address() & R_SCATTERED
) == 0 ) {
6831 bool externSymbolIsThumbDef
= false;
6832 srcAddr
= sect
->addr() + reloc
->r_address();
6833 src
.atom
= this->findAtomByAddress(srcAddr
);
6834 src
.offsetInAtom
= srcAddr
- src
.atom
->_objAddress
;
6835 fixUpPtr
= (uint32_t*)(file().fileContent() + sect
->offset() + reloc
->r_address());
6836 if ( reloc
->r_type() != ARM_RELOC_PAIR
)
6837 instruction
= LittleEndian::get32(*fixUpPtr
);
6838 if ( reloc
->r_extern() ) {
6839 const macho_nlist
<P
>& targetSymbol
= parser
.symbolFromIndex(reloc
->r_symbolnum());
6840 // use direct reference for local symbols
6841 if ( ((targetSymbol
.n_type() & N_TYPE
) == N_SECT
) && (((targetSymbol
.n_type() & N_EXT
) == 0) || (parser
.nameFromSymbol(targetSymbol
)[0] == 'L')) ) {
6842 parser
.findTargetFromAddressAndSectionNum(targetSymbol
.n_value(), targetSymbol
.n_sect(), target
);
6846 target
.name
= parser
.nameFromSymbol(targetSymbol
);
6847 target
.weakImport
= parser
.weakImportFromSymbol(targetSymbol
);
6848 if ( ((targetSymbol
.n_type() & N_TYPE
) == N_SECT
) && (targetSymbol
.n_desc() & N_ARM_THUMB_DEF
) )
6849 externSymbolIsThumbDef
= true;
6852 switch ( reloc
->r_type() ) {
6853 case ARM_RELOC_BR24
:
6854 // Sign-extend displacement
6855 displacement
= (instruction
& 0x00FFFFFF) << 2;
6856 if ( (displacement
& 0x02000000) != 0 )
6857 displacement
|= 0xFC000000;
6858 // The pc added will be +8 from the pc
6860 // If this is BLX add H << 1
6861 if ((instruction
& 0xFE000000) == 0xFA000000)
6862 displacement
+= ((instruction
& 0x01000000) >> 23);
6863 if ( reloc
->r_extern() ) {
6864 dstAddr
= srcAddr
+ displacement
;
6865 // <rdar://problem/16652542> support large .o files
6866 if ( srcAddr
> 0x2000000 ) {
6867 dstAddr
-= ((srcAddr
+ 0x1FFFFFF) & 0xFC000000);
6869 target
.addend
= dstAddr
;
6870 if ( externSymbolIsThumbDef
)
6871 target
.addend
&= -2; // remove thumb bit
6874 dstAddr
= srcAddr
+ displacement
;
6875 parser
.findTargetFromAddressAndSectionNum(dstAddr
, reloc
->r_symbolnum(), target
);
6877 // special case "calls" for dtrace
6878 if ( (target
.name
!= NULL
) && (strncmp(target
.name
, "___dtrace_probe$", 16) == 0) ) {
6879 parser
.addFixup(src
, ld::Fixup::k1of1
,
6880 ld::Fixup::kindStoreARMDtraceCallSiteNop
, false, target
.name
);
6881 parser
.addDtraceExtraInfos(src
, &target
.name
[16]);
6883 else if ( (target
.name
!= NULL
) && (strncmp(target
.name
, "___dtrace_isenabled$", 20) == 0) ) {
6884 parser
.addFixup(src
, ld::Fixup::k1of1
,
6885 ld::Fixup::kindStoreARMDtraceIsEnableSiteClear
, false, target
.name
);
6886 parser
.addDtraceExtraInfos(src
, &target
.name
[20]);
6889 parser
.addFixups(src
, ld::Fixup::kindStoreARMBranch24
, target
);
6892 case ARM_THUMB_RELOC_BR22
:
6893 // thumb2 added two more bits to displacement, complicating the displacement decoding
6895 uint32_t s
= (instruction
>> 10) & 0x1;
6896 uint32_t j1
= (instruction
>> 29) & 0x1;
6897 uint32_t j2
= (instruction
>> 27) & 0x1;
6898 uint32_t imm10
= instruction
& 0x3FF;
6899 uint32_t imm11
= (instruction
>> 16) & 0x7FF;
6900 uint32_t i1
= (j1
== s
);
6901 uint32_t i2
= (j2
== s
);
6902 uint32_t dis
= (s
<< 24) | (i1
<< 23) | (i2
<< 22) | (imm10
<< 12) | (imm11
<< 1);
6906 displacement
= sdis
;
6908 // The pc added will be +4 from the pc
6910 // If the instruction was blx, force the low 2 bits to be clear
6911 dstAddr
= srcAddr
+ displacement
;
6912 if ((instruction
& 0xD0000000) == 0xC0000000)
6913 dstAddr
&= 0xFFFFFFFC;
6915 if ( reloc
->r_extern() ) {
6916 // <rdar://problem/16652542> support large .o files
6917 if ( srcAddr
> 0x1000000 ) {
6918 dstAddr
-= ((srcAddr
+ 0xFFFFFF) & 0xFE000000);
6920 target
.addend
= (int64_t)(int32_t)dstAddr
;
6923 parser
.findTargetFromAddressAndSectionNum(dstAddr
, reloc
->r_symbolnum(), target
);
6925 // special case "calls" for dtrace
6926 if ( (target
.name
!= NULL
) && (strncmp(target
.name
, "___dtrace_probe$", 16) == 0) ) {
6927 parser
.addFixup(src
, ld::Fixup::k1of1
,
6928 ld::Fixup::kindStoreThumbDtraceCallSiteNop
, false, target
.name
);
6929 parser
.addDtraceExtraInfos(src
, &target
.name
[16]);
6931 else if ( (target
.name
!= NULL
) && (strncmp(target
.name
, "___dtrace_isenabled$", 20) == 0) ) {
6932 parser
.addFixup(src
, ld::Fixup::k1of1
,
6933 ld::Fixup::kindStoreThumbDtraceIsEnableSiteClear
, false, target
.name
);
6934 parser
.addDtraceExtraInfos(src
, &target
.name
[20]);
6937 parser
.addFixups(src
, ld::Fixup::kindStoreThumbBranch22
, target
);
6940 case ARM_RELOC_VANILLA
:
6941 if ( reloc
->r_length() != 2 )
6942 throw "bad length for ARM_RELOC_VANILLA";
6943 contentValue
= LittleEndian::get32(*fixUpPtr
);
6944 if ( reloc
->r_extern() ) {
6945 target
.addend
= (int32_t)contentValue
;
6946 if ( externSymbolIsThumbDef
)
6947 target
.addend
&= -2; // remove thumb bit
6950 parser
.findTargetFromAddressAndSectionNum(contentValue
, reloc
->r_symbolnum(), target
);
6951 // possible non-extern relocation turned into by-name ref because target is a weak-def
6952 if ( target
.atom
!= NULL
) {
6953 if ( target
.atom
->isThumb() )
6954 target
.addend
&= -2; // remove thumb bit
6955 // if reference to LSDA, add group subordinate fixup
6956 if ( target
.atom
->contentType() == ld::Atom::typeLSDA
) {
6957 Parser
<arm
>::SourceLocation src2
;
6958 src2
.atom
= src
.atom
;
6959 src2
.offsetInAtom
= 0;
6960 parser
.addFixup(src2
, ld::Fixup::k1of1
, ld::Fixup::kindNoneGroupSubordinateLSDA
, target
.atom
);
6964 parser
.addFixups(src
, ld::Fixup::kindStoreLittleEndian32
, target
);
6966 case ARM_THUMB_32BIT_BRANCH
:
6967 // silently ignore old unnecessary reloc
6969 case ARM_RELOC_HALF
:
6970 nextReloc
= &reloc
[1];
6971 if ( nextReloc
->r_type() == ARM_RELOC_PAIR
) {
6972 uint32_t instruction16
;
6973 uint32_t other16
= (nextReloc
->r_address() & 0xFFFF);
6975 if ( reloc
->r_length() & 2 ) {
6977 uint32_t i
= ((instruction
& 0x00000400) >> 10);
6978 uint32_t imm4
= (instruction
& 0x0000000F);
6979 uint32_t imm3
= ((instruction
& 0x70000000) >> 28);
6980 uint32_t imm8
= ((instruction
& 0x00FF0000) >> 16);
6981 instruction16
= (imm4
<< 12) | (i
<< 11) | (imm3
<< 8) | imm8
;
6985 uint32_t imm4
= ((instruction
& 0x000F0000) >> 16);
6986 uint32_t imm12
= (instruction
& 0x00000FFF);
6987 instruction16
= (imm4
<< 12) | imm12
;
6989 if ( reloc
->r_length() & 1 ) {
6991 dstAddr
= ((instruction16
<< 16) | other16
);
6992 if ( reloc
->r_extern() ) {
6993 target
.addend
= dstAddr
;
6994 if ( externSymbolIsThumbDef
)
6995 target
.addend
&= -2; // remove thumb bit
6998 parser
.findTargetFromAddress(dstAddr
, target
);
6999 if ( target
.atom
->isThumb() )
7000 target
.addend
&= (-2); // remove thumb bit
7002 parser
.addFixups(src
, (isThumb
? ld::Fixup::kindStoreThumbHigh16
: ld::Fixup::kindStoreARMHigh16
), target
);
7006 dstAddr
= (other16
<< 16) | instruction16
;
7007 if ( reloc
->r_extern() ) {
7008 target
.addend
= dstAddr
;
7009 if ( externSymbolIsThumbDef
)
7010 target
.addend
&= -2; // remove thumb bit
7013 parser
.findTargetFromAddress(dstAddr
, target
);
7014 if ( target
.atom
->isThumb() )
7015 target
.addend
&= (-2); // remove thumb bit
7017 parser
.addFixups(src
, (isThumb
? ld::Fixup::kindStoreThumbLow16
: ld::Fixup::kindStoreARMLow16
), target
);
7022 throw "for ARM_RELOC_HALF, next reloc is not ARM_RELOC_PAIR";
7025 throwf("unknown relocation type %d", reloc
->r_type());
7030 const macho_scattered_relocation_info
<P
>* sreloc
= (macho_scattered_relocation_info
<P
>*)reloc
;
7031 // file format allows pair to be scattered or not
7032 const macho_scattered_relocation_info
<P
>* nextSReloc
= &sreloc
[1];
7033 nextReloc
= &reloc
[1];
7034 srcAddr
= sect
->addr() + sreloc
->r_address();
7035 dstAddr
= sreloc
->r_value();
7036 fixUpPtr
= (uint32_t*)(file().fileContent() + sect
->offset() + sreloc
->r_address());
7037 instruction
= LittleEndian::get32(*fixUpPtr
);
7038 src
.atom
= this->findAtomByAddress(srcAddr
);
7039 src
.offsetInAtom
= srcAddr
- src
.atom
->_objAddress
;
7040 bool nextRelocIsPair
= false;
7041 uint32_t nextRelocAddress
= 0;
7042 uint32_t nextRelocValue
= 0;
7043 if ( (nextReloc
->r_address() & R_SCATTERED
) == 0 ) {
7044 if ( nextReloc
->r_type() == ARM_RELOC_PAIR
) {
7045 nextRelocIsPair
= true;
7046 nextRelocAddress
= nextReloc
->r_address();
7051 if ( nextSReloc
->r_type() == ARM_RELOC_PAIR
) {
7052 nextRelocIsPair
= true;
7053 nextRelocAddress
= nextSReloc
->r_address();
7054 nextRelocValue
= nextSReloc
->r_value();
7058 switch ( sreloc
->r_type() ) {
7059 case ARM_RELOC_VANILLA
:
7060 // with a scattered relocation we get both the target (sreloc->r_value()) and the target+offset (*fixUpPtr)
7061 if ( sreloc
->r_length() != 2 )
7062 throw "bad length for ARM_RELOC_VANILLA";
7063 target
.atom
= parser
.findAtomByAddress(sreloc
->r_value());
7064 if ( target
.atom
== NULL
)
7065 throwf("bad r_value (0x%08X) for ARM_RELOC_VANILLA\n", sreloc
->r_value());
7066 contentValue
= LittleEndian::get32(*fixUpPtr
);
7067 target
.addend
= contentValue
- target
.atom
->_objAddress
;
7068 if ( target
.atom
->isThumb() )
7069 target
.addend
&= -2; // remove thumb bit
7070 parser
.addFixups(src
, ld::Fixup::kindStoreLittleEndian32
, target
);
7072 case ARM_RELOC_BR24
:
7073 // Sign-extend displacement
7074 displacement
= (instruction
& 0x00FFFFFF) << 2;
7075 if ( (displacement
& 0x02000000) != 0 )
7076 displacement
|= 0xFC000000;
7077 // The pc added will be +8 from the pc
7079 // If this is BLX add H << 1
7080 if ((instruction
& 0xFE000000) == 0xFA000000)
7081 displacement
+= ((instruction
& 0x01000000) >> 23);
7082 target
.atom
= parser
.findAtomByAddress(sreloc
->r_value());
7083 target
.addend
= (int64_t)(srcAddr
+ displacement
) - (int64_t)(target
.atom
->_objAddress
);
7084 parser
.addFixups(src
, ld::Fixup::kindStoreARMBranch24
, target
);
7086 case ARM_THUMB_RELOC_BR22
:
7087 // thumb2 added two more bits to displacement, complicating the displacement decoding
7089 uint32_t s
= (instruction
>> 10) & 0x1;
7090 uint32_t j1
= (instruction
>> 29) & 0x1;
7091 uint32_t j2
= (instruction
>> 27) & 0x1;
7092 uint32_t imm10
= instruction
& 0x3FF;
7093 uint32_t imm11
= (instruction
>> 16) & 0x7FF;
7094 uint32_t i1
= (j1
== s
);
7095 uint32_t i2
= (j2
== s
);
7096 uint32_t dis
= (s
<< 24) | (i1
<< 23) | (i2
<< 22) | (imm10
<< 12) | (imm11
<< 1);
7100 displacement
= sdis
;
7102 // The pc added will be +4 from the pc
7104 dstAddr
= srcAddr
+displacement
;
7105 // If the instruction was blx, force the low 2 bits to be clear
7106 if ((instruction
& 0xF8000000) == 0xE8000000)
7107 dstAddr
&= 0xFFFFFFFC;
7108 target
.atom
= parser
.findAtomByAddress(sreloc
->r_value());
7109 target
.addend
= dstAddr
- target
.atom
->_objAddress
;
7110 parser
.addFixups(src
, ld::Fixup::kindStoreThumbBranch22
, target
);
7112 case ARM_RELOC_SECTDIFF
:
7113 case ARM_RELOC_LOCAL_SECTDIFF
:
7115 if ( ! nextRelocIsPair
)
7116 throw "ARM_RELOC_SECTDIFF missing following pair";
7117 if ( sreloc
->r_length() != 2 )
7118 throw "bad length for ARM_RELOC_SECTDIFF";
7119 contentValue
= LittleEndian::get32(*fixUpPtr
);
7120 Atom
<arm
>* fromAtom
= parser
.findAtomByAddress(nextRelocValue
);
7121 uint32_t offsetInFrom
= nextRelocValue
- fromAtom
->_objAddress
;
7122 uint32_t offsetInTarget
;
7123 Atom
<arm
>* targetAtom
= parser
.findAtomByAddressOrLocalTargetOfStub(sreloc
->r_value(), &offsetInTarget
);
7124 // check for addend encoded in the section content
7125 int64_t addend
= (int32_t)contentValue
- (int32_t)(sreloc
->r_value() - nextRelocValue
);
7126 if ( targetAtom
->isThumb() )
7127 addend
&= -2; // remove thumb bit
7128 // if reference to LSDA, add group subordinate fixup
7129 if ( targetAtom
->contentType() == ld::Atom::typeLSDA
) {
7130 Parser
<arm
>::SourceLocation src2
;
7131 src2
.atom
= src
.atom
;
7132 src2
.offsetInAtom
= 0;
7133 parser
.addFixup(src2
, ld::Fixup::k1of1
, ld::Fixup::kindNoneGroupSubordinateLSDA
, targetAtom
);
7136 // switch binding base on coalescing
7137 if ( targetAtom
->scope() == ld::Atom::scopeTranslationUnit
) {
7138 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, targetAtom
);
7140 else if ( (targetAtom
->combine() == ld::Atom::combineByNameAndContent
) || (targetAtom
->combine() == ld::Atom::combineByNameAndReferences
) ) {
7141 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, targetAtom
);
7144 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, false, targetAtom
->name());
7146 parser
.addFixup(src
, ld::Fixup::k2of5
, ld::Fixup::kindAddAddend
, offsetInTarget
);
7147 parser
.addFixup(src
, ld::Fixup::k3of5
, ld::Fixup::kindSubtractTargetAddress
, fromAtom
);
7148 parser
.addFixup(src
, ld::Fixup::k4of5
, ld::Fixup::kindSubtractAddend
, offsetInFrom
-addend
);
7149 parser
.addFixup(src
, ld::Fixup::k5of5
, ld::Fixup::kindStoreLittleEndian32
);
7152 if ( targetAtom
->scope() == ld::Atom::scopeTranslationUnit
) {
7153 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, targetAtom
);
7155 else if ( (targetAtom
->combine() == ld::Atom::combineByNameAndContent
) || (targetAtom
->combine() == ld::Atom::combineByNameAndReferences
) ) {
7156 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, targetAtom
);
7159 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, false, targetAtom
->name());
7161 parser
.addFixup(src
, ld::Fixup::k2of5
, ld::Fixup::kindAddAddend
, (uint32_t)(offsetInTarget
+addend
));
7162 parser
.addFixup(src
, ld::Fixup::k3of5
, ld::Fixup::kindSubtractTargetAddress
, fromAtom
);
7163 parser
.addFixup(src
, ld::Fixup::k4of5
, ld::Fixup::kindSubtractAddend
, offsetInFrom
);
7164 parser
.addFixup(src
, ld::Fixup::k5of5
, ld::Fixup::kindStoreLittleEndian32
);
7168 case ARM_RELOC_HALF_SECTDIFF
:
7169 if ( nextRelocIsPair
) {
7170 instruction
= LittleEndian::get32(*fixUpPtr
);
7171 Atom
<arm
>* fromAtom
= parser
.findAtomByAddress(nextRelocValue
);
7172 uint32_t offsetInFrom
= nextRelocValue
- fromAtom
->_objAddress
;
7173 Atom
<arm
>* targetAtom
= parser
.findAtomByAddress(sreloc
->r_value());
7174 uint32_t offsetInTarget
= sreloc
->r_value() - targetAtom
->_objAddress
;
7175 uint32_t instruction16
;
7176 uint32_t other16
= (nextRelocAddress
& 0xFFFF);
7178 if ( sreloc
->r_length() & 2 ) {
7180 uint32_t i
= ((instruction
& 0x00000400) >> 10);
7181 uint32_t imm4
= (instruction
& 0x0000000F);
7182 uint32_t imm3
= ((instruction
& 0x70000000) >> 28);
7183 uint32_t imm8
= ((instruction
& 0x00FF0000) >> 16);
7184 instruction16
= (imm4
<< 12) | (i
<< 11) | (imm3
<< 8) | imm8
;
7188 uint32_t imm4
= ((instruction
& 0x000F0000) >> 16);
7189 uint32_t imm12
= (instruction
& 0x00000FFF);
7190 instruction16
= (imm4
<< 12) | imm12
;
7192 if ( sreloc
->r_length() & 1 )
7193 dstAddr
= ((instruction16
<< 16) | other16
);
7195 dstAddr
= (other16
<< 16) | instruction16
;
7196 if ( targetAtom
->isThumb() )
7197 dstAddr
&= (-2); // remove thumb bit
7198 int32_t addend
= dstAddr
- (sreloc
->r_value() - nextRelocValue
);
7199 if ( targetAtom
->scope() == ld::Atom::scopeTranslationUnit
) {
7200 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, targetAtom
);
7202 else if ( (targetAtom
->combine() == ld::Atom::combineByNameAndContent
) || (targetAtom
->combine() == ld::Atom::combineByNameAndReferences
) ) {
7203 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, targetAtom
);
7206 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, false, targetAtom
->name());
7208 parser
.addFixup(src
, ld::Fixup::k2of5
, ld::Fixup::kindAddAddend
, (uint32_t)offsetInTarget
+addend
);
7209 parser
.addFixup(src
, ld::Fixup::k3of5
, ld::Fixup::kindSubtractTargetAddress
, fromAtom
);
7210 parser
.addFixup(src
, ld::Fixup::k4of5
, ld::Fixup::kindSubtractAddend
, offsetInFrom
);
7211 if ( sreloc
->r_length() & 1 ) {
7213 parser
.addFixup(src
, ld::Fixup::k5of5
, (isThumb
? ld::Fixup::kindStoreThumbHigh16
: ld::Fixup::kindStoreARMHigh16
));
7217 parser
.addFixup(src
, ld::Fixup::k5of5
, (isThumb
? ld::Fixup::kindStoreThumbLow16
: ld::Fixup::kindStoreARMLow16
));
7222 throw "ARM_RELOC_HALF_SECTDIFF reloc missing following pair";
7224 case ARM_RELOC_HALF
:
7225 if ( nextRelocIsPair
) {
7226 instruction
= LittleEndian::get32(*fixUpPtr
);
7227 Atom
<arm
>* targetAtom
= parser
.findAtomByAddress(sreloc
->r_value());
7228 uint32_t instruction16
;
7229 uint32_t other16
= (nextRelocAddress
& 0xFFFF);
7231 if ( sreloc
->r_length() & 2 ) {
7233 uint32_t i
= ((instruction
& 0x00000400) >> 10);
7234 uint32_t imm4
= (instruction
& 0x0000000F);
7235 uint32_t imm3
= ((instruction
& 0x70000000) >> 28);
7236 uint32_t imm8
= ((instruction
& 0x00FF0000) >> 16);
7237 instruction16
= (imm4
<< 12) | (i
<< 11) | (imm3
<< 8) | imm8
;
7241 uint32_t imm4
= ((instruction
& 0x000F0000) >> 16);
7242 uint32_t imm12
= (instruction
& 0x00000FFF);
7243 instruction16
= (imm4
<< 12) | imm12
;
7245 if ( sreloc
->r_length() & 1 )
7246 dstAddr
= ((instruction16
<< 16) | other16
);
7248 dstAddr
= (other16
<< 16) | instruction16
;
7249 if ( targetAtom
->scope() == ld::Atom::scopeTranslationUnit
) {
7250 parser
.addFixup(src
, ld::Fixup::k1of3
, ld::Fixup::kindSetTargetAddress
, targetAtom
);
7252 else if ( (targetAtom
->combine() == ld::Atom::combineByNameAndContent
) || (targetAtom
->combine() == ld::Atom::combineByNameAndReferences
) ) {
7253 parser
.addFixup(src
, ld::Fixup::k1of3
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, targetAtom
);
7256 parser
.addFixup(src
, ld::Fixup::k1of3
, ld::Fixup::kindSetTargetAddress
, false, targetAtom
->name());
7258 parser
.addFixup(src
, ld::Fixup::k2of3
, ld::Fixup::kindAddAddend
, dstAddr
- targetAtom
->_objAddress
);
7259 if ( sreloc
->r_length() & 1 ) {
7261 parser
.addFixup(src
, ld::Fixup::k3of3
, (isThumb
? ld::Fixup::kindStoreThumbHigh16
: ld::Fixup::kindStoreARMHigh16
));
7265 parser
.addFixup(src
, ld::Fixup::k3of3
, (isThumb
? ld::Fixup::kindStoreThumbLow16
: ld::Fixup::kindStoreARMLow16
));
7270 throw "scattered ARM_RELOC_HALF reloc missing following pair";
7273 throwf("unknown ARM scattered relocation type %d", sreloc
->r_type());
7281 #if SUPPORT_ARCH_arm64
7283 bool Section
<arm64
>::addRelocFixup(class Parser
<arm64
>& parser
, const macho_relocation_info
<P
>* reloc
)
7285 bool result
= false;
7286 Parser
<arm64
>::SourceLocation src
;
7287 Parser
<arm64
>::TargetDesc target
= { NULL
, NULL
, false, 0 };
7288 Parser
<arm64
>::TargetDesc toTarget
;
7289 int32_t prefixRelocAddend
= 0;
7290 if ( reloc
->r_type() == ARM64_RELOC_ADDEND
) {
7291 uint32_t rawAddend
= reloc
->r_symbolnum();
7292 prefixRelocAddend
= rawAddend
;
7293 if ( rawAddend
& 0x00800000 )
7294 prefixRelocAddend
|= 0xFF000000; // sign extend 24-bit signed int to 32-bits
7295 uint32_t addendAddress
= reloc
->r_address();
7296 ++reloc
; //advance to next reloc record
7298 if ( reloc
->r_address() != addendAddress
)
7299 throw "ARM64_RELOC_ADDEND r_address does not match next reloc's r_address";
7301 const macho_section
<P
>* sect
= this->machoSection();
7302 uint64_t srcAddr
= sect
->addr() + reloc
->r_address();
7303 src
.atom
= this->findAtomByAddress(srcAddr
);
7304 src
.offsetInAtom
= srcAddr
- src
.atom
->_objAddress
;
7305 const uint8_t* fixUpPtr
= file().fileContent() + sect
->offset() + reloc
->r_address();
7306 uint64_t contentValue
= 0;
7307 const macho_relocation_info
<arm64::P
>* nextReloc
= &reloc
[1];
7308 bool useDirectBinding
;
7309 uint32_t instruction
;
7310 uint32_t encodedAddend
;
7311 switch ( reloc
->r_length() ) {
7313 contentValue
= *fixUpPtr
;
7316 contentValue
= (int64_t)(int16_t)E::get16(*((uint16_t*)fixUpPtr
));
7319 contentValue
= (int64_t)(int32_t)E::get32(*((uint32_t*)fixUpPtr
));
7322 contentValue
= E::get64(*((uint64_t*)fixUpPtr
));
7325 if ( reloc
->r_extern() ) {
7326 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(reloc
->r_symbolnum());
7327 const char* symbolName
= parser
.nameFromSymbol(sym
);
7328 if ( ((sym
.n_type() & N_TYPE
) == N_SECT
) && (((sym
.n_type() & N_EXT
) == 0) || (symbolName
[0] == 'L') || (symbolName
[0] == 'l')) ) {
7329 // use direct reference for local symbols
7330 parser
.findTargetFromAddressAndSectionNum(sym
.n_value(), sym
.n_sect(), target
);
7331 //target.addend += contentValue;
7333 else if ( ((sym
.n_type() & N_TYPE
) == N_SECT
) && (src
.atom
->_objAddress
<= sym
.n_value()) && (sym
.n_value() < (src
.atom
->_objAddress
+src
.atom
->size())) ) {
7334 // <rdar://problem/13700961> spurious warning when weak function has reference to itself
7335 // use direct reference when atom targets itself
7336 target
.atom
= src
.atom
;
7340 target
.name
= symbolName
;
7341 target
.weakImport
= parser
.weakImportFromSymbol(sym
);
7342 //target.addend = contentValue;
7344 // cfstrings should always use direct reference to backing store
7345 if ( (this->type() == ld::Section::typeCFString
) && (src
.offsetInAtom
!= 0) ) {
7346 parser
.findTargetFromAddressAndSectionNum(sym
.n_value(), sym
.n_sect(), target
);
7347 //target.addend = contentValue;
7351 if ( reloc
->r_pcrel() )
7352 contentValue
+= srcAddr
;
7353 parser
.findTargetFromAddressAndSectionNum(contentValue
, reloc
->r_symbolnum(), target
);
7355 switch ( reloc
->r_type() ) {
7356 case ARM64_RELOC_UNSIGNED
:
7357 if ( reloc
->r_pcrel() )
7358 throw "pcrel and ARM64_RELOC_UNSIGNED not supported";
7359 if ( reloc
->r_extern() )
7360 target
.addend
= contentValue
;
7361 switch ( reloc
->r_length() ) {
7364 throw "length < 2 and ARM64_RELOC_UNSIGNED not supported";
7366 parser
.addFixups(src
, ld::Fixup::kindStoreLittleEndian32
, target
);
7369 parser
.addFixups(src
, ld::Fixup::kindStoreLittleEndian64
, target
);
7373 case ARM64_RELOC_BRANCH26
:
7374 if ( ! reloc
->r_pcrel() )
7375 throw "not pcrel and ARM64_RELOC_BRANCH26 not supported";
7376 if ( ! reloc
->r_extern() )
7377 throw "r_extern == 0 and ARM64_RELOC_BRANCH26 not supported";
7378 if ( reloc
->r_length() != 2 )
7379 throw "r_length != 2 and ARM64_RELOC_BRANCH26 not supported";
7380 if ( (target
.name
!= NULL
) && (strncmp(target
.name
, "___dtrace_probe$", 16) == 0) ) {
7381 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindStoreARM64DtraceCallSiteNop
, false, target
.name
);
7382 parser
.addDtraceExtraInfos(src
, &target
.name
[16]);
7384 else if ( (target
.name
!= NULL
) && (strncmp(target
.name
, "___dtrace_isenabled$", 20) == 0) ) {
7385 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindStoreARM64DtraceIsEnableSiteClear
, false, target
.name
);
7386 parser
.addDtraceExtraInfos(src
, &target
.name
[20]);
7389 target
.addend
= prefixRelocAddend
;
7390 instruction
= contentValue
;
7391 encodedAddend
= (instruction
& 0x03FFFFFF) << 2;
7392 if ( encodedAddend
!= 0 ) {
7393 if ( prefixRelocAddend
== 0 ) {
7394 warning("branch26 instruction at 0x%08X has embedded addend. ARM64_RELOC_ADDEND should be used instead", reloc
->r_address());
7395 target
.addend
= encodedAddend
;
7398 throwf("branch26 instruction at 0x%08X has embedded addend and ARM64_RELOC_ADDEND also used", reloc
->r_address());
7401 parser
.addFixups(src
, ld::Fixup::kindStoreARM64Branch26
, target
);
7404 case ARM64_RELOC_PAGE21
:
7405 if ( ! reloc
->r_pcrel() )
7406 throw "not pcrel and ARM64_RELOC_PAGE21 not supported";
7407 if ( ! reloc
->r_extern() )
7408 throw "r_extern == 0 and ARM64_RELOC_PAGE21 not supported";
7409 if ( reloc
->r_length() != 2 )
7410 throw "length != 2 and ARM64_RELOC_PAGE21 not supported";
7411 target
.addend
= prefixRelocAddend
;
7412 instruction
= contentValue
;
7413 encodedAddend
= ((instruction
& 0x60000000) >> 29) | ((instruction
& 0x01FFFFE0) >> 3);
7414 encodedAddend
*= 4096; // internally addend is in bytes, so scale
7415 if ( encodedAddend
!= 0 ) {
7416 if ( prefixRelocAddend
== 0 ) {
7417 warning("adrp instruction at 0x%08X has embedded addend. ARM64_RELOC_ADDEND should be used instead", reloc
->r_address());
7418 target
.addend
= encodedAddend
;
7421 throwf("adrp instruction at 0x%08X has embedded addend and ARM64_RELOC_ADDEND also used", reloc
->r_address());
7424 parser
.addFixups(src
, ld::Fixup::kindStoreARM64Page21
, target
);
7426 case ARM64_RELOC_PAGEOFF12
:
7427 if ( reloc
->r_pcrel() )
7428 throw "pcrel and ARM64_RELOC_PAGEOFF12 not supported";
7429 if ( ! reloc
->r_extern() )
7430 throw "r_extern == 0 and ARM64_RELOC_PAGEOFF12 not supported";
7431 if ( reloc
->r_length() != 2 )
7432 throw "length != 2 and ARM64_RELOC_PAGEOFF12 not supported";
7433 target
.addend
= prefixRelocAddend
;
7434 instruction
= contentValue
;
7435 encodedAddend
= ((instruction
& 0x003FFC00) >> 10);
7436 // internally addend is in bytes. Some instructions have an implicit scale factor
7437 if ( (instruction
& 0x3B000000) == 0x39000000 ) {
7438 switch ( instruction
& 0xC0000000 ) {
7452 if ( encodedAddend
!= 0 ) {
7453 if ( prefixRelocAddend
== 0 ) {
7454 warning("pageoff12 instruction at 0x%08X has embedded addend. ARM64_RELOC_ADDEND should be used instead", reloc
->r_address());
7455 target
.addend
= encodedAddend
;
7458 throwf("pageoff12 instruction at 0x%08X has embedded addend and ARM64_RELOC_ADDEND also used", reloc
->r_address());
7461 parser
.addFixups(src
, ld::Fixup::kindStoreARM64PageOff12
, target
);
7463 case ARM64_RELOC_GOT_LOAD_PAGE21
:
7464 if ( ! reloc
->r_pcrel() )
7465 throw "not pcrel and ARM64_RELOC_GOT_LOAD_PAGE21 not supported";
7466 if ( ! reloc
->r_extern() )
7467 throw "r_extern == 0 and ARM64_RELOC_GOT_LOAD_PAGE21 not supported";
7468 if ( reloc
->r_length() != 2 )
7469 throw "length != 2 and ARM64_RELOC_GOT_LOAD_PAGE21 not supported";
7470 if ( prefixRelocAddend
!= 0 )
7471 throw "ARM64_RELOC_ADDEND followed by ARM64_RELOC_GOT_LOAD_PAGE21 not supported";
7472 instruction
= contentValue
;
7473 target
.addend
= ((instruction
& 0x60000000) >> 29) | ((instruction
& 0x01FFFFE0) >> 3);
7474 if ( target
.addend
!= 0 )
7475 throw "non-zero addend with ARM64_RELOC_GOT_LOAD_PAGE21 is not supported";
7476 parser
.addFixups(src
, ld::Fixup::kindStoreARM64GOTLoadPage21
, target
);
7478 case ARM64_RELOC_GOT_LOAD_PAGEOFF12
:
7479 if ( reloc
->r_pcrel() )
7480 throw "pcrel and ARM64_RELOC_GOT_LOAD_PAGEOFF12 not supported";
7481 if ( ! reloc
->r_extern() )
7482 throw "r_extern == 0 and ARM64_RELOC_GOT_LOAD_PAGEOFF12 not supported";
7483 if ( reloc
->r_length() != 2 )
7484 throw "length != 2 and ARM64_RELOC_GOT_LOAD_PAGEOFF12 not supported";
7485 if ( prefixRelocAddend
!= 0 )
7486 throw "ARM64_RELOC_ADDEND followed by ARM64_RELOC_GOT_LOAD_PAGEOFF12 not supported";
7487 instruction
= contentValue
;
7488 target
.addend
= ((instruction
& 0x003FFC00) >> 10);
7489 parser
.addFixups(src
, ld::Fixup::kindStoreARM64GOTLoadPageOff12
, target
);
7491 case ARM64_RELOC_TLVP_LOAD_PAGE21
:
7492 if ( ! reloc
->r_pcrel() )
7493 throw "not pcrel and ARM64_RELOC_TLVP_LOAD_PAGE21 not supported";
7494 if ( ! reloc
->r_extern() )
7495 throw "r_extern == 0 and ARM64_RELOC_TLVP_LOAD_PAGE21 not supported";
7496 if ( reloc
->r_length() != 2 )
7497 throw "length != 2 and ARM64_RELOC_TLVP_LOAD_PAGE21 not supported";
7498 if ( prefixRelocAddend
!= 0 )
7499 throw "ARM64_RELOC_ADDEND followed by ARM64_RELOC_TLVP_LOAD_PAGE21 not supported";
7500 instruction
= contentValue
;
7501 target
.addend
= ((instruction
& 0x60000000) >> 29) | ((instruction
& 0x01FFFFE0) >> 3);
7502 if ( target
.addend
!= 0 )
7503 throw "non-zero addend with ARM64_RELOC_GOT_LOAD_PAGE21 is not supported";
7504 parser
.addFixups(src
, ld::Fixup::kindStoreARM64TLVPLoadPage21
, target
);
7506 case ARM64_RELOC_TLVP_LOAD_PAGEOFF12
:
7507 if ( reloc
->r_pcrel() )
7508 throw "pcrel and ARM64_RELOC_TLVP_LOAD_PAGEOFF12 not supported";
7509 if ( ! reloc
->r_extern() )
7510 throw "r_extern == 0 and ARM64_RELOC_TLVP_LOAD_PAGEOFF12 not supported";
7511 if ( reloc
->r_length() != 2 )
7512 throw "length != 2 and ARM64_RELOC_TLVP_LOAD_PAGEOFF12 not supported";
7513 if ( prefixRelocAddend
!= 0 )
7514 throw "ARM64_RELOC_ADDEND followed by ARM64_RELOC_TLVP_LOAD_PAGEOFF12 not supported";
7515 instruction
= contentValue
;
7516 target
.addend
= ((instruction
& 0x003FFC00) >> 10);
7517 parser
.addFixups(src
, ld::Fixup::kindStoreARM64TLVPLoadPageOff12
, target
);
7519 case ARM64_RELOC_SUBTRACTOR
:
7520 if ( reloc
->r_pcrel() )
7521 throw "ARM64_RELOC_SUBTRACTOR cannot be pc-relative";
7522 if ( reloc
->r_length() < 2 )
7523 throw "ARM64_RELOC_SUBTRACTOR must have r_length of 2 or 3";
7524 if ( !reloc
->r_extern() )
7525 throw "ARM64_RELOC_SUBTRACTOR must have r_extern=1";
7526 if ( nextReloc
->r_type() != ARM64_RELOC_UNSIGNED
)
7527 throw "ARM64_RELOC_SUBTRACTOR must be followed by ARM64_RELOC_UNSIGNED";
7528 if ( prefixRelocAddend
!= 0 )
7529 throw "ARM64_RELOC_ADDEND followed by ARM64_RELOC_SUBTRACTOR not supported";
7531 if ( nextReloc
->r_pcrel() )
7532 throw "ARM64_RELOC_UNSIGNED following a ARM64_RELOC_SUBTRACTOR cannot be pc-relative";
7533 if ( nextReloc
->r_length() != reloc
->r_length() )
7534 throw "ARM64_RELOC_UNSIGNED following a ARM64_RELOC_SUBTRACTOR must have same r_length";
7535 if ( nextReloc
->r_extern() ) {
7536 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(nextReloc
->r_symbolnum());
7537 // use direct reference for local symbols
7538 if ( ((sym
.n_type() & N_TYPE
) == N_SECT
) && (((sym
.n_type() & N_EXT
) == 0) || (parser
.nameFromSymbol(sym
)[0] == 'L')) ) {
7539 parser
.findTargetFromAddressAndSectionNum(sym
.n_value(), sym
.n_sect(), toTarget
);
7540 toTarget
.addend
= contentValue
;
7541 useDirectBinding
= true;
7544 toTarget
.name
= parser
.nameFromSymbol(sym
);
7545 toTarget
.weakImport
= parser
.weakImportFromSymbol(sym
);
7546 toTarget
.addend
= contentValue
;
7547 useDirectBinding
= false;
7551 parser
.findTargetFromAddressAndSectionNum(contentValue
, nextReloc
->r_symbolnum(), toTarget
);
7552 useDirectBinding
= (toTarget
.atom
->scope() == ld::Atom::scopeTranslationUnit
);
7554 if ( useDirectBinding
) {
7555 if ( (toTarget
.atom
->combine() == ld::Atom::combineByNameAndContent
) || (toTarget
.atom
->combine() == ld::Atom::combineByNameAndReferences
) )
7556 parser
.addFixup(src
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, toTarget
.atom
);
7558 parser
.addFixup(src
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, toTarget
.atom
);
7561 parser
.addFixup(src
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, toTarget
.weakImport
, toTarget
.name
);
7562 parser
.addFixup(src
, ld::Fixup::k2of4
, ld::Fixup::kindAddAddend
, toTarget
.addend
);
7563 if ( target
.atom
== NULL
)
7564 parser
.addFixup(src
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractTargetAddress
, false, target
.name
);
7566 parser
.addFixup(src
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractTargetAddress
, target
.atom
);
7567 if ( reloc
->r_length() == 2 )
7568 parser
.addFixup(src
, ld::Fixup::k4of4
, ld::Fixup::kindStoreLittleEndian32
);
7570 parser
.addFixup(src
, ld::Fixup::k4of4
, ld::Fixup::kindStoreLittleEndian64
);
7572 case ARM64_RELOC_POINTER_TO_GOT
:
7573 if ( ! reloc
->r_extern() )
7574 throw "r_extern == 0 and ARM64_RELOC_POINTER_TO_GOT not supported";
7575 if ( prefixRelocAddend
!= 0 )
7576 throw "ARM64_RELOC_ADDEND followed by ARM64_RELOC_POINTER_TO_GOT not supported";
7577 if ( reloc
->r_pcrel() ) {
7578 if ( reloc
->r_length() != 2 )
7579 throw "r_length != 2 and r_extern = 1 and ARM64_RELOC_POINTER_TO_GOT not supported";
7580 parser
.addFixups(src
, ld::Fixup::kindStoreARM64PCRelToGOT
, target
);
7583 if ( reloc
->r_length() != 3 )
7584 throw "r_length != 3 and r_extern = 0 and ARM64_RELOC_POINTER_TO_GOT not supported";
7585 parser
.addFixups(src
, ld::Fixup::kindStoreARM64PointerToGOT
, target
);
7588 #if SUPPORT_ARCH_arm64e
7589 case ARM64_RELOC_AUTHENTICATED_POINTER
: {
7590 if ( reloc
->r_pcrel() )
7591 throw "pcrel and ARM64_RELOC_AUTHENTICATED_POINTER not supported";
7592 if ( ! reloc
->r_extern() )
7593 throw "r_extern == 0 and ARM64_RELOC_AUTHENTICATED_POINTER not supported";
7594 // An authenticated pointer is:
7597 // uint16_t diversityData;
7598 // uint16_t hasAddressDiversity : 1;
7599 // uint16_t key : 2;
7600 // uint16_t zeroes : 11;
7601 // uint16_t zero : 1;
7602 // uint16_t authenticated : 1;
7604 target
.addend
= (int32_t)(contentValue
& 0xFFFFFFFF);
7605 if (parser
._supportsAuthenticatedPointers
) {
7606 target
.authData
.discriminator
= (uint16_t)(contentValue
>> 32);
7607 target
.authData
.hasAddressDiversity
= (contentValue
& (1ULL << 48)) != 0;
7608 target
.authData
.key
= (ld::Fixup::AuthData::ptrauth_key
)((contentValue
>> 49) & 0x3);
7610 static bool emittedWarning
= false;
7611 if (!emittedWarning
) {
7612 emittedWarning
= true;
7613 warning("stripping authenticated relocation as image uses -preload or -static");
7616 bool isAuthenticated
= (contentValue
& (1ULL << 63)) != 0;
7617 if (!isAuthenticated
)
7618 throw "ARM64_RELOC_AUTHENTICATED_POINTER value must have authenticated bit set";
7619 switch ( reloc
->r_length() ) {
7623 throw "length < 3 and ARM64_RELOC_AUTHENTICATED_POINTER not supported";
7625 if (parser
._supportsAuthenticatedPointers
)
7626 parser
.addFixups(src
, ld::Fixup::kindStoreLittleEndianAuth64
, target
);
7628 parser
.addFixups(src
, ld::Fixup::kindStoreLittleEndian64
, target
);
7635 throwf("unknown relocation type %d", reloc
->r_type());
7642 template <typename A
>
7643 bool ObjC1ClassSection
<A
>::addRelocFixup(class Parser
<A
>& parser
, const macho_relocation_info
<P
>* reloc
)
7646 FixedSizeSection
<A
>::addRelocFixup(parser
, reloc
);
7648 assert(0 && "needs template specialization");
7653 bool ObjC1ClassSection
<x86
>::addRelocFixup(class Parser
<x86
>& parser
, const macho_relocation_info
<x86::P
>* reloc
)
7655 // if this is the reloc for the super class name string, add implicit reference to super class
7656 if ( ((reloc
->r_address() & R_SCATTERED
) == 0) && (reloc
->r_type() == GENERIC_RELOC_VANILLA
) ) {
7657 assert( reloc
->r_length() == 2 );
7658 assert( ! reloc
->r_pcrel() );
7660 const macho_section
<P
>* sect
= this->machoSection();
7661 Parser
<x86
>::SourceLocation src
;
7662 uint32_t srcAddr
= sect
->addr() + reloc
->r_address();
7663 src
.atom
= this->findAtomByAddress(srcAddr
);
7664 src
.offsetInAtom
= srcAddr
- src
.atom
->objectAddress();
7665 if ( src
.offsetInAtom
== 4 ) {
7666 Parser
<x86
>::TargetDesc stringTarget
;
7667 const uint8_t* fixUpPtr
= file().fileContent() + sect
->offset() + reloc
->r_address();
7668 uint32_t contentValue
= LittleEndian::get32(*((uint32_t*)fixUpPtr
));
7669 parser
.findTargetFromAddressAndSectionNum(contentValue
, reloc
->r_symbolnum(), stringTarget
);
7671 assert(stringTarget
.atom
!= NULL
);
7672 assert(stringTarget
.atom
->contentType() == ld::Atom::typeCString
);
7673 const char* superClassBaseName
= (char*)stringTarget
.atom
->rawContentPointer();
7674 char* superClassName
= new char[strlen(superClassBaseName
) + 20];
7675 strcpy(superClassName
, ".objc_class_name_");
7676 strcat(superClassName
, superClassBaseName
);
7678 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindSetTargetAddress
, false, superClassName
);
7682 return FixedSizeSection
<x86
>::addRelocFixup(parser
, reloc
);
7687 template <typename A
>
7688 bool Objc1ClassReferences
<A
>::addRelocFixup(class Parser
<A
>& parser
, const macho_relocation_info
<P
>* reloc
)
7691 PointerToCStringSection
<A
>::addRelocFixup(parser
, reloc
);
7693 assert(0 && "needs template specialization");
7700 bool Objc1ClassReferences
<x86
>::addRelocFixup(class Parser
<x86
>& parser
, const macho_relocation_info
<x86::P
>* reloc
)
7702 // add implict class refs, fixups not usable yet, so look at relocations
7703 assert( (reloc
->r_address() & R_SCATTERED
) == 0 );
7704 assert( reloc
->r_type() == GENERIC_RELOC_VANILLA
);
7705 assert( reloc
->r_length() == 2 );
7706 assert( ! reloc
->r_pcrel() );
7708 const macho_section
<P
>* sect
= this->machoSection();
7709 Parser
<x86
>::SourceLocation src
;
7710 uint32_t srcAddr
= sect
->addr() + reloc
->r_address();
7711 src
.atom
= this->findAtomByAddress(srcAddr
);
7712 src
.offsetInAtom
= srcAddr
- src
.atom
->objectAddress();
7713 Parser
<x86
>::TargetDesc stringTarget
;
7714 const uint8_t* fixUpPtr
= file().fileContent() + sect
->offset() + reloc
->r_address();
7715 uint32_t contentValue
= LittleEndian::get32(*((uint32_t*)fixUpPtr
));
7716 parser
.findTargetFromAddressAndSectionNum(contentValue
, reloc
->r_symbolnum(), stringTarget
);
7718 assert(stringTarget
.atom
!= NULL
);
7719 assert(stringTarget
.atom
->contentType() == ld::Atom::typeCString
);
7720 const char* baseClassName
= (char*)stringTarget
.atom
->rawContentPointer();
7721 char* objcClassName
= new char[strlen(baseClassName
) + 20];
7722 strcpy(objcClassName
, ".objc_class_name_");
7723 strcat(objcClassName
, baseClassName
);
7725 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindSetTargetAddress
, false, objcClassName
);
7728 return PointerToCStringSection
<x86
>::addRelocFixup(parser
, reloc
);
7731 #if SUPPORT_ARCH_arm64
7733 void Section
<arm64
>::addLOH(class Parser
<arm64
>& parser
, int kind
, int count
, const uint64_t addrs
[]) {
7735 case LOH_ARM64_ADRP_ADRP
:
7736 case LOH_ARM64_ADRP_LDR
:
7737 case LOH_ARM64_ADRP_ADD
:
7738 case LOH_ARM64_ADRP_LDR_GOT
:
7740 warning("arm64 Linker Optimiztion Hint %d has wrong number of arguments", kind
);
7742 case LOH_ARM64_ADRP_ADD_LDR
:
7743 case LOH_ARM64_ADRP_LDR_GOT_LDR
:
7744 case LOH_ARM64_ADRP_ADD_STR
:
7745 case LOH_ARM64_ADRP_LDR_GOT_STR
:
7747 warning("arm64 Linker Optimiztion Hint %d has wrong number of arguments", kind
);
7750 // pick lowest address in tuple for use as offsetInAtom
7751 uint64_t lowestAddress
= addrs
[0];
7752 for(int i
=1; i
< count
; ++i
) {
7753 if ( addrs
[i
] < lowestAddress
)
7754 lowestAddress
= addrs
[i
];
7756 // verify all other address are in same atom
7757 Atom
<arm64
>* inAtom
= parser
.findAtomByAddress(lowestAddress
);
7758 const uint64_t atomStartAddr
= inAtom
->objectAddress();
7759 const uint64_t atomEndAddr
= atomStartAddr
+ inAtom
->size();
7760 for(int i
=0; i
< count
; ++i
) {
7761 if ( (addrs
[i
] < atomStartAddr
) || (addrs
[i
] >= atomEndAddr
) ) {
7762 warning("arm64 Linker Optimiztion Hint addresses are not in same atom: 0x%08llX and 0x%08llX",
7763 lowestAddress
, addrs
[i
]);
7764 return; // skip this LOH
7766 if ( (addrs
[i
] & 0x3) != 0 ) {
7767 warning("arm64 Linker Optimiztion Hint address is not 4-byte aligned: 0x%08llX", addrs
[i
]);
7768 return; // skip this LOH
7770 if ( (addrs
[i
] - lowestAddress
) > 0xFFFF ) {
7771 if ( parser
.verboseOptimizationHints() ) {
7772 warning("arm64 Linker Optimiztion Hint addresses are too far apart: 0x%08llX and 0x%08llX",
7773 lowestAddress
, addrs
[i
]);
7775 return; // skip this LOH
7779 // encoded kind, count, and address deltas in 64-bit addend
7780 ld::Fixup::LOH_arm64 extra
;
7782 extra
.info
.kind
= kind
;
7783 extra
.info
.count
= count
-1;
7784 extra
.info
.delta1
= (addrs
[0] - lowestAddress
) >> 2;
7785 extra
.info
.delta2
= (count
> 1) ? ((addrs
[1] - lowestAddress
) >> 2) : 0;
7786 extra
.info
.delta3
= (count
> 2) ? ((addrs
[2] - lowestAddress
) >> 2) : 0;
7787 extra
.info
.delta4
= (count
> 3) ? ((addrs
[3] - lowestAddress
) >> 2) : 0;
7788 typename Parser
<arm64
>::SourceLocation
src(inAtom
, lowestAddress
- inAtom
->objectAddress());
7789 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindLinkerOptimizationHint
, extra
.addend
);
7794 template <typename A
>
7795 void Section
<A
>::addLOH(class Parser
<A
>& parser
, int kind
, int count
, const uint64_t addrs
[]) {
7799 template <typename A
>
7800 void Section
<A
>::makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
&)
7802 const macho_section
<P
>* sect
= this->machoSection();
7803 if ( sect
->reloff() + (sect
->nreloc() * sizeof(macho_relocation_info
<P
>)) > parser
.fileLength() )
7804 throwf("relocations for section %s/%s extends beyond end of file,", sect
->segname(), Section
<A
>::makeSectionName(sect
) );
7805 const macho_relocation_info
<P
>* relocs
= (macho_relocation_info
<P
>*)(file().fileContent() + sect
->reloff());
7806 const uint32_t relocCount
= sect
->nreloc();
7807 for (uint32_t r
= 0; r
< relocCount
; ++r
) {
7809 if ( this->addRelocFixup(parser
, &relocs
[r
]) )
7812 catch (const char* msg
) {
7813 throwf("in section %s,%s reloc %u: %s", sect
->segname(), Section
<A
>::makeSectionName(sect
), r
, msg
);
7817 // add follow-on fixups if .o file is missing .subsections_via_symbols
7818 if ( this->addFollowOnFixups() ) {
7819 Atom
<A
>* end
= &_endAtoms
[-1];
7820 for(Atom
<A
>* p
= _beginAtoms
; p
< end
; ++p
) {
7821 typename Parser
<A
>::SourceLocation
src(p
, 0);
7822 Atom
<A
>* nextAtom
= &p
[1];
7823 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindNoneFollowOn
, nextAtom
);
7826 else if ( this->type() == ld::Section::typeCode
) {
7827 // if FDE broke text not at a symbol, use followOn to keep code together
7828 Atom
<A
>* end
= &_endAtoms
[-1];
7829 for(Atom
<A
>* p
= _beginAtoms
; p
< end
; ++p
) {
7830 typename Parser
<A
>::SourceLocation
src(p
, 0);
7831 Atom
<A
>* nextAtom
= &p
[1];
7832 if ( (p
->symbolTableInclusion() == ld::Atom::symbolTableIn
) && (nextAtom
->symbolTableInclusion() == ld::Atom::symbolTableNotIn
) ) {
7833 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindNoneFollowOn
, nextAtom
);
7837 if ( !this->_altEntries
.empty() && !this->addFollowOnFixups() ) {
7838 if ( _altEntries
.count(_beginAtoms
) != 0 )
7839 warning("N_ALT_ENTRY bit set on first atom in section %s/%s", sect
->segname(), Section
<A
>::makeSectionName(sect
));
7841 Atom
<A
>* end
= &_endAtoms
[-1];
7842 for(Atom
<A
>* p
= _beginAtoms
; p
< end
; ++p
) {
7843 Atom
<A
>* nextAtom
= &p
[1];
7844 // <rdar://problem/22960070> support alt_entry aliases (alias process already added followOn, don't repeat)
7845 if ( (_altEntries
.count(nextAtom
) != 0) && (p
->_objAddress
!= nextAtom
->_objAddress
) ) {
7846 typename Parser
<A
>::SourceLocation
src(p
, 0);
7847 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindNoneFollowOn
, nextAtom
);
7848 typename Parser
<A
>::SourceLocation
src2(nextAtom
, 0);
7849 parser
.addFixup(src2
, ld::Fixup::k1of1
, ld::Fixup::kindNoneGroupSubordinate
, p
);
7854 // <rdar://problem/9218847> track data-in-code
7855 if ( parser
.hasDataInCodeLabels() && (this->type() == ld::Section::typeCode
) ) {
7856 for (uint32_t i
=0; i
< parser
.symbolCount(); ++i
) {
7857 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(i
);
7859 if ( (sym
.n_type() & N_STAB
) != 0 )
7861 // ignore non-definitions
7862 if ( (sym
.n_type() & N_TYPE
) != N_SECT
)
7865 // 'L' labels do not denote atom breaks
7866 const char* symbolName
= parser
.nameFromSymbol(sym
);
7867 if ( symbolName
[0] == 'L' ) {
7868 if ( strncmp(symbolName
, "L$start$", 8) == 0 ) {
7869 ld::Fixup::Kind kind
= ld::Fixup::kindNone
;
7870 if ( strncmp(&symbolName
[8], "data$", 5) == 0 )
7871 kind
= ld::Fixup::kindDataInCodeStartData
;
7872 else if ( strncmp(&symbolName
[8], "code$", 5) == 0 )
7873 kind
= ld::Fixup::kindDataInCodeEnd
;
7874 else if ( strncmp(&symbolName
[8], "jt8$", 4) == 0 )
7875 kind
= ld::Fixup::kindDataInCodeStartJT8
;
7876 else if ( strncmp(&symbolName
[8], "jt16$", 4) == 0 )
7877 kind
= ld::Fixup::kindDataInCodeStartJT16
;
7878 else if ( strncmp(&symbolName
[8], "jt32$", 4) == 0 )
7879 kind
= ld::Fixup::kindDataInCodeStartJT32
;
7880 else if ( strncmp(&symbolName
[8], "jta32$", 4) == 0 )
7881 kind
= ld::Fixup::kindDataInCodeStartJTA32
;
7883 warning("unknown L$start$ label %s in file %s", symbolName
, this->file().path());
7884 if ( kind
!= ld::Fixup::kindNone
) {
7885 Atom
<A
>* inAtom
= parser
.findAtomByAddress(sym
.n_value());
7886 typename Parser
<A
>::SourceLocation
src(inAtom
, sym
.n_value() - inAtom
->objectAddress());
7887 parser
.addFixup(src
, ld::Fixup::k1of1
, kind
);
7894 // <rdar://problem/11150575> Handle LC_DATA_IN_CODE in object files
7895 if ( this->type() == ld::Section::typeCode
) {
7896 const pint_t startAddr
= this->_machOSection
->addr();
7897 const pint_t endAddr
= startAddr
+ this->_machOSection
->size();
7898 for ( const macho_data_in_code_entry
<P
>* p
= parser
.dataInCodeStart(); p
!= parser
.dataInCodeEnd(); ++p
) {
7899 if ( (p
->offset() >= startAddr
) && (p
->offset() < endAddr
) ) {
7900 ld::Fixup::Kind kind
= ld::Fixup::kindNone
;
7901 switch ( p
->kind() ) {
7902 case DICE_KIND_DATA
:
7903 kind
= ld::Fixup::kindDataInCodeStartData
;
7905 case DICE_KIND_JUMP_TABLE8
:
7906 kind
= ld::Fixup::kindDataInCodeStartJT8
;
7908 case DICE_KIND_JUMP_TABLE16
:
7909 kind
= ld::Fixup::kindDataInCodeStartJT16
;
7911 case DICE_KIND_JUMP_TABLE32
:
7912 kind
= ld::Fixup::kindDataInCodeStartJT32
;
7914 case DICE_KIND_ABS_JUMP_TABLE32
:
7915 kind
= ld::Fixup::kindDataInCodeStartJTA32
;
7918 kind
= ld::Fixup::kindDataInCodeStartData
;
7919 warning("uknown LC_DATA_IN_CODE kind (%d) at offset 0x%08X", p
->kind(), p
->offset());
7922 Atom
<A
>* inAtom
= parser
.findAtomByAddress(p
->offset());
7923 typename Parser
<A
>::SourceLocation
srcStart(inAtom
, p
->offset() - inAtom
->objectAddress());
7924 parser
.addFixup(srcStart
, ld::Fixup::k1of1
, kind
);
7925 typename Parser
<A
>::SourceLocation
srcEnd(inAtom
, p
->offset() + p
->length() - inAtom
->objectAddress());
7926 parser
.addFixup(srcEnd
, ld::Fixup::k1of1
, ld::Fixup::kindDataInCodeEnd
);
7931 // <rdar://problem/11945700> convert linker optimization hints into internal format
7932 if ( this->type() == ld::Section::typeCode
&& parser
.hasOptimizationHints() ) {
7933 const pint_t startAddr
= this->_machOSection
->addr();
7934 const pint_t endAddr
= startAddr
+ this->_machOSection
->size();
7935 for (const uint8_t* p
= parser
.optimizationHintsStart(); p
< parser
.optimizationHintsEnd(); ) {
7937 int32_t kind
= read_uleb128(&p
, parser
.optimizationHintsEnd());
7938 if ( kind
== 0 ) // padding at end of loh buffer
7941 warning("malformed uleb128 kind in LC_LINKER_OPTIMIZATION_HINTS");
7944 int32_t count
= read_uleb128(&p
, parser
.optimizationHintsEnd());
7945 if ( count
== -1 ) {
7946 warning("malformed uleb128 count in LC_LINKER_OPTIMIZATION_HINTS");
7950 warning("address count > 3 in LC_LINKER_OPTIMIZATION_HINTS");
7953 for (int32_t i
=0; i
< count
; ++i
) {
7954 addrs
[i
] = read_uleb128(&p
, parser
.optimizationHintsEnd());
7956 if ( (startAddr
<= addrs
[0]) && (addrs
[0] < endAddr
) ) {
7957 this->addLOH(parser
, kind
, count
, addrs
);
7958 //fprintf(stderr, "kind=%d", kind);
7959 //for (int32_t i=0; i < count; ++i) {
7960 // fprintf(stderr, ", addr=0x%08llX", addrs[i]);
7962 //fprintf(stderr, "\n");
7968 // add follow-on fixups for aliases
7969 if ( _hasAliases
) {
7970 for(Atom
<A
>* p
= _beginAtoms
; p
< _endAtoms
; ++p
) {
7971 if ( p
->isAlias() && ! this->addFollowOnFixups() ) {
7972 Atom
<A
>* targetOfAlias
= &p
[1];
7973 assert(p
< &_endAtoms
[-1]);
7974 assert(p
->_objAddress
== targetOfAlias
->_objAddress
);
7975 typename Parser
<A
>::SourceLocation
src(p
, 0);
7976 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindNoneFollowOn
, targetOfAlias
);
7985 // main function used by linker to instantiate ld::Files
7987 ld::relocatable::File
* parse(const uint8_t* fileContent
, uint64_t fileLength
,
7988 const char* path
, time_t modTime
, ld::File::Ordinal ordinal
, const ParserOptions
& opts
)
7990 switch ( opts
.architecture
) {
7991 #if SUPPORT_ARCH_x86_64
7992 case CPU_TYPE_X86_64
:
7993 if ( mach_o::relocatable::Parser
<x86_64
>::validFile(fileContent
) )
7994 return mach_o::relocatable::Parser
<x86_64
>::parse(fileContent
, fileLength
, path
, modTime
, ordinal
, opts
);
7997 #if SUPPORT_ARCH_i386
7999 if ( mach_o::relocatable::Parser
<x86
>::validFile(fileContent
) )
8000 return mach_o::relocatable::Parser
<x86
>::parse(fileContent
, fileLength
, path
, modTime
, ordinal
, opts
);
8003 #if SUPPORT_ARCH_arm_any
8005 if ( mach_o::relocatable::Parser
<arm
>::validFile(fileContent
, opts
.objSubtypeMustMatch
, opts
.subType
) )
8006 return mach_o::relocatable::Parser
<arm
>::parse(fileContent
, fileLength
, path
, modTime
, ordinal
, opts
);
8009 #if SUPPORT_ARCH_arm64
8010 case CPU_TYPE_ARM64
:
8011 if ( mach_o::relocatable::Parser
<arm64
>::validFile(fileContent
, opts
.objSubtypeMustMatch
, opts
.subType
) )
8012 return mach_o::relocatable::Parser
<arm64
>::parse(fileContent
, fileLength
, path
, modTime
, ordinal
, opts
);
8020 // used by archive reader to validate member object file
8022 bool isObjectFile(const uint8_t* fileContent
, uint64_t fileLength
, const ParserOptions
& opts
)
8024 switch ( opts
.architecture
) {
8025 case CPU_TYPE_X86_64
:
8026 return ( mach_o::relocatable::Parser
<x86_64
>::validFile(fileContent
) );
8028 return ( mach_o::relocatable::Parser
<x86
>::validFile(fileContent
) );
8030 return ( mach_o::relocatable::Parser
<arm
>::validFile(fileContent
, opts
.objSubtypeMustMatch
, opts
.subType
) );
8031 case CPU_TYPE_ARM64
:
8032 return ( mach_o::relocatable::Parser
<arm64
>::validFile(fileContent
, opts
.objSubtypeMustMatch
, opts
.subType
) );
8038 // used by linker to infer architecture when no -arch is on command line
8040 bool isObjectFile(const uint8_t* fileContent
, cpu_type_t
* result
, cpu_subtype_t
* subResult
, ld::Platform
* platform
, uint32_t* minOsVers
)
8042 if ( mach_o::relocatable::Parser
<x86_64
>::validFile(fileContent
) ) {
8043 *result
= CPU_TYPE_X86_64
;
8044 const macho_header
<Pointer64
<LittleEndian
> >* header
= (const macho_header
<Pointer64
<LittleEndian
> >*)fileContent
;
8045 *subResult
= header
->cpusubtype();
8046 *platform
= Parser
<x86_64
>::findPlatform(header
, minOsVers
);
8049 if ( mach_o::relocatable::Parser
<x86
>::validFile(fileContent
) ) {
8050 const macho_header
<Pointer32
<LittleEndian
> >* header
= (const macho_header
<Pointer32
<LittleEndian
> >*)fileContent
;
8051 *result
= CPU_TYPE_I386
;
8052 *subResult
= CPU_SUBTYPE_X86_ALL
;
8053 *platform
= Parser
<x86
>::findPlatform(header
, minOsVers
);
8056 if ( mach_o::relocatable::Parser
<arm
>::validFile(fileContent
, false, 0) ) {
8057 const macho_header
<Pointer32
<LittleEndian
> >* header
= (const macho_header
<Pointer32
<LittleEndian
> >*)fileContent
;
8058 *result
= CPU_TYPE_ARM
;
8059 *subResult
= header
->cpusubtype();
8060 *platform
= Parser
<arm
>::findPlatform(header
, minOsVers
);
8063 if ( mach_o::relocatable::Parser
<arm64
>::validFile(fileContent
, false, 0) ) {
8064 const macho_header
<Pointer64
<LittleEndian
> >* header
= (const macho_header
<Pointer64
<LittleEndian
> >*)fileContent
;
8065 *result
= CPU_TYPE_ARM64
;
8066 *subResult
= header
->cpusubtype();
8067 *platform
= Parser
<arm64
>::findPlatform(header
, minOsVers
);
8074 // used by linker is error messages to describe bad .o file
8076 const char* archName(const uint8_t* fileContent
)
8078 if ( mach_o::relocatable::Parser
<x86_64
>::validFile(fileContent
) ) {
8079 return mach_o::relocatable::Parser
<x86_64
>::fileKind(fileContent
);
8081 if ( mach_o::relocatable::Parser
<x86
>::validFile(fileContent
) ) {
8082 return mach_o::relocatable::Parser
<x86
>::fileKind(fileContent
);
8084 if ( mach_o::relocatable::Parser
<arm
>::validFile(fileContent
, false, 0) ) {
8085 return mach_o::relocatable::Parser
<arm
>::fileKind(fileContent
);
8091 // Used by archive reader when -ObjC option is specified
8093 bool hasObjC2Categories(const uint8_t* fileContent
)
8095 if ( mach_o::relocatable::Parser
<x86_64
>::validFile(fileContent
) ) {
8096 return mach_o::relocatable::Parser
<x86_64
>::hasObjC2Categories(fileContent
);
8098 else if ( mach_o::relocatable::Parser
<arm
>::validFile(fileContent
, false, 0) ) {
8099 return mach_o::relocatable::Parser
<arm
>::hasObjC2Categories(fileContent
);
8101 else if ( mach_o::relocatable::Parser
<x86
>::validFile(fileContent
, false, 0) ) {
8102 return mach_o::relocatable::Parser
<x86
>::hasObjC2Categories(fileContent
);
8104 #if SUPPORT_ARCH_arm64
8105 else if ( mach_o::relocatable::Parser
<arm64
>::validFile(fileContent
, false, 0) ) {
8106 return mach_o::relocatable::Parser
<arm64
>::hasObjC2Categories(fileContent
);
8113 // Used by archive reader when -ObjC option is specified
8115 bool hasObjC1Categories(const uint8_t* fileContent
)
8117 if ( mach_o::relocatable::Parser
<x86
>::validFile(fileContent
, false, 0) ) {
8118 return mach_o::relocatable::Parser
<x86
>::hasObjC1Categories(fileContent
);
8124 // Used by bitcode obfuscator to get a list of non local symbols from object file
8126 bool getNonLocalSymbols(const uint8_t* fileContent
, std::vector
<const char*> &syms
)
8128 if ( mach_o::relocatable::Parser
<x86_64
>::validFile(fileContent
) ) {
8129 return mach_o::relocatable::Parser
<x86_64
>::getNonLocalSymbols(fileContent
, syms
);
8131 else if ( mach_o::relocatable::Parser
<arm
>::validFile(fileContent
, false, 0) ) {
8132 return mach_o::relocatable::Parser
<arm
>::getNonLocalSymbols(fileContent
, syms
);
8134 else if ( mach_o::relocatable::Parser
<x86
>::validFile(fileContent
, false, 0) ) {
8135 return mach_o::relocatable::Parser
<x86
>::getNonLocalSymbols(fileContent
, syms
);
8137 #if SUPPORT_ARCH_arm64
8138 else if ( mach_o::relocatable::Parser
<arm64
>::validFile(fileContent
, false, 0) ) {
8139 return mach_o::relocatable::Parser
<arm64
>::getNonLocalSymbols(fileContent
, syms
);
8147 } // namespace relocatable
8148 } // namespace mach_o