1 /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-
3 * Copyright (c) 2009-2010 Apple Inc. All rights reserved.
5 * @APPLE_LICENSE_HEADER_START@
7 * This file contains Original Code and/or Modifications of Original Code
8 * as defined in and that are subject to the Apple Public Source License
9 * Version 2.0 (the 'License'). You may not use this file except in
10 * compliance with the License. Please obtain a copy of the License at
11 * http://www.opensource.apple.com/apsl/ and read it before using this
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
19 * Please see the License for the specific language governing rights and
20 * limitations under the License.
22 * @APPLE_LICENSE_HEADER_END@
31 #include <sys/param.h>
35 #include "MachOFileAbstraction.hpp"
37 #include "libunwind/DwarfInstructions.hpp"
38 #include "libunwind/AddressSpace.hpp"
39 #include "libunwind/Registers.hpp"
45 #include <type_traits>
48 #include "debugline.h"
50 #include "Architectures.hpp"
51 #include "Bitcode.hpp"
53 #include "macho_relocatable_file.h"
57 extern void throwf(const char* format
, ...) __attribute__ ((noreturn
,format(printf
, 1, 2)));
58 extern void warning(const char* format
, ...) __attribute__((format(printf
, 1, 2)));
61 namespace relocatable
{
65 template <typename A
> class Parser
;
66 template <typename A
> class Atom
;
67 template <typename A
> class Section
;
68 template <typename A
> class CFISection
;
69 template <typename A
> class CUSection
;
72 class File
: public ld::relocatable::File
75 File(const char* p
, time_t mTime
, const uint8_t* content
, ld::File::Ordinal ord
) :
76 ld::relocatable::File(p
,mTime
,ord
), _fileContent(content
),
77 _sectionsArray(NULL
), _atomsArray(NULL
),
78 _sectionsArrayCount(0), _atomsArrayCount(0), _aliasAtomsArrayCount(0),
79 _debugInfoKind(ld::relocatable::File::kDebugInfoNone
),
80 _dwarfTranslationUnitPath(NULL
),
81 _dwarfDebugInfoSect(NULL
), _dwarfDebugAbbrevSect(NULL
),
82 _dwarfDebugLineSect(NULL
), _dwarfDebugStringSect(NULL
),
83 _objConstraint(ld::File::objcConstraintNone
),
88 _canScatterAtoms(false),
89 _srcKind(kSourceUnknown
) {}
92 // overrides of ld::File
93 virtual bool forEachAtom(ld::File::AtomHandler
&) const;
94 virtual bool justInTimeforEachAtom(const char* name
, ld::File::AtomHandler
&) const
96 virtual uint32_t minOSVersion() const { return _minOSVersion
; }
97 virtual uint32_t platformLoadCommand() const { return _platform
; }
99 // overrides of ld::relocatable::File
100 virtual ObjcConstraint
objCConstraint() const { return _objConstraint
; }
101 virtual uint32_t cpuSubType() const { return _cpuSubType
; }
102 virtual DebugInfoKind
debugInfo() const { return _debugInfoKind
; }
103 virtual const std::vector
<ld::relocatable::File::Stab
>* stabs() const { return &_stabs
; }
104 virtual bool canScatterAtoms() const { return _canScatterAtoms
; }
105 virtual const char* translationUnitSource() const;
106 virtual LinkerOptionsList
* linkerOptions() const { return &_linkerOptions
; }
107 virtual uint8_t swiftVersion() const { return _swiftVersion
; }
108 virtual ld::Bitcode
* getBitcode() const { return _bitcode
.get(); }
109 virtual SourceKind
sourceKind() const { return _srcKind
; }
111 const uint8_t* fileContent() { return _fileContent
; }
113 friend class Atom
<A
>;
114 friend class Section
<A
>;
115 friend class Parser
<A
>;
116 friend class CFISection
<A
>::OAS
;
118 typedef typename
A::P P
;
120 const uint8_t* _fileContent
;
121 Section
<A
>** _sectionsArray
;
122 uint8_t* _atomsArray
;
123 uint8_t* _aliasAtomsArray
;
124 uint32_t _sectionsArrayCount
;
125 uint32_t _atomsArrayCount
;
126 uint32_t _aliasAtomsArrayCount
;
127 std::vector
<ld::Fixup
> _fixups
;
128 std::vector
<ld::Atom::UnwindInfo
> _unwindInfos
;
129 std::vector
<ld::Atom::LineInfo
> _lineInfos
;
130 std::vector
<ld::relocatable::File::Stab
>_stabs
;
131 ld::relocatable::File::DebugInfoKind _debugInfoKind
;
132 const char* _dwarfTranslationUnitPath
;
133 const macho_section
<P
>* _dwarfDebugInfoSect
;
134 const macho_section
<P
>* _dwarfDebugAbbrevSect
;
135 const macho_section
<P
>* _dwarfDebugLineSect
;
136 const macho_section
<P
>* _dwarfDebugStringSect
;
137 ld::File::ObjcConstraint _objConstraint
;
138 uint8_t _swiftVersion
;
139 uint32_t _cpuSubType
;
140 uint32_t _minOSVersion
;
142 bool _canScatterAtoms
;
143 std::vector
<std::vector
<const char*> > _linkerOptions
;
144 std::unique_ptr
<ld::Bitcode
> _bitcode
;
149 template <typename A
>
150 class Section
: public ld::Section
153 typedef typename
A::P::uint_t pint_t
;
154 typedef typename
A::P P
;
155 typedef typename
A::P::E E
;
157 virtual ~Section() { }
158 class File
<A
>& file() const { return _file
; }
159 const macho_section
<P
>* machoSection() const { return _machOSection
; }
160 uint32_t sectionNum(class Parser
<A
>&) const;
161 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
);
162 virtual ld::Atom::ContentType
contentType() { return ld::Atom::typeUnclassified
; }
163 virtual bool dontDeadStrip() { return (this->_machOSection
->flags() & S_ATTR_NO_DEAD_STRIP
); }
164 virtual bool dontDeadStripIfReferencesLive() { return ( (this->_machOSection
!= NULL
) && (this->_machOSection
->flags() & S_ATTR_LIVE_SUPPORT
) ); }
165 virtual Atom
<A
>* findAtomByAddress(pint_t addr
) { return this->findContentAtomByAddress(addr
, this->_beginAtoms
, this->_endAtoms
); }
166 virtual bool addFollowOnFixups() const { return ! _file
.canScatterAtoms(); }
167 virtual uint32_t appendAtoms(class Parser
<A
>& parser
, uint8_t* buffer
,
168 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
169 const struct Parser
<A
>::CFI_CU_InfoArrays
&) = 0;
170 virtual uint32_t computeAtomCount(class Parser
<A
>& parser
,
171 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
172 const struct Parser
<A
>::CFI_CU_InfoArrays
&) = 0;
173 virtual void makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
&);
174 virtual bool addRelocFixup(class Parser
<A
>& parser
, const macho_relocation_info
<P
>*);
175 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const { return 0; }
176 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
177 const ld::IndirectBindingTable
& ind
) const { return false; }
178 virtual bool ignoreLabel(const char* label
) const { return false; }
179 static const char* makeSectionName(const macho_section
<typename
A::P
>* s
);
182 Section(File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
183 : ld::Section(makeSegmentName(s
), makeSectionName(s
), sectionType(s
)),
184 _file(f
), _machOSection(s
), _beginAtoms(NULL
), _endAtoms(NULL
), _hasAliases(false) { }
185 Section(File
<A
>& f
, const char* segName
, const char* sectName
, ld::Section::Type t
, bool hidden
=false)
186 : ld::Section(segName
, sectName
, t
, hidden
), _file(f
), _machOSection(NULL
),
187 _beginAtoms(NULL
), _endAtoms(NULL
), _hasAliases(false) { }
190 Atom
<A
>* findContentAtomByAddress(pint_t addr
, class Atom
<A
>* start
, class Atom
<A
>* end
);
191 uint32_t x86_64PcRelOffset(uint8_t r_type
);
192 void addLOH(class Parser
<A
>& parser
, int kind
, int count
, const uint64_t addrs
[]);
193 static const char* makeSegmentName(const macho_section
<typename
A::P
>* s
);
194 static bool readable(const macho_section
<typename
A::P
>* s
);
195 static bool writable(const macho_section
<typename
A::P
>* s
);
196 static bool exectuable(const macho_section
<typename
A::P
>* s
);
197 static ld::Section::Type
sectionType(const macho_section
<typename
A::P
>* s
);
200 const macho_section
<P
>* _machOSection
;
201 class Atom
<A
>* _beginAtoms
;
202 class Atom
<A
>* _endAtoms
;
204 std::set
<const class Atom
<A
>*> _altEntries
;
208 template <typename A
>
209 class CFISection
: public Section
<A
>
212 CFISection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
213 : Section
<A
>(f
, s
) { }
214 uint32_t cfiCount(Parser
<A
>& parser
);
216 virtual ld::Atom::ContentType
contentType() { return ld::Atom::typeCFI
; }
217 virtual uint32_t computeAtomCount(class Parser
<A
>& parser
, struct Parser
<A
>::LabelAndCFIBreakIterator
& it
, const struct Parser
<A
>::CFI_CU_InfoArrays
&);
218 virtual uint32_t appendAtoms(class Parser
<A
>& parser
, uint8_t* buffer
, struct Parser
<A
>::LabelAndCFIBreakIterator
& it
, const struct Parser
<A
>::CFI_CU_InfoArrays
&);
219 virtual void makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
&);
220 virtual bool addFollowOnFixups() const { return false; }
224 /// ObjectFileAddressSpace is used as a template parameter to UnwindCursor for parsing
225 /// dwarf CFI information in an object file.
230 typedef typename
A::P::uint_t pint_t
;
231 typedef typename
A::P P
;
232 typedef typename
A::P::E E
;
233 typedef typename
A::P::uint_t sint_t
;
235 OAS(CFISection
<A
>& ehFrameSection
, const uint8_t* ehFrameBuffer
) :
236 _ehFrameSection(ehFrameSection
),
237 _ehFrameContent(ehFrameBuffer
),
238 _ehFrameStartAddr(ehFrameSection
.machoSection()->addr()),
239 _ehFrameEndAddr(ehFrameSection
.machoSection()->addr()+ehFrameSection
.machoSection()->size()) {}
241 uint8_t get8(pint_t addr
) { return *((uint8_t*)mappedAddress(addr
)); }
242 uint16_t get16(pint_t addr
) { return E::get16(*((uint16_t*)mappedAddress(addr
))); }
243 uint32_t get32(pint_t addr
) { return E::get32(*((uint32_t*)mappedAddress(addr
))); }
244 uint64_t get64(pint_t addr
) { return E::get64(*((uint64_t*)mappedAddress(addr
))); }
245 pint_t
getP(pint_t addr
) { return P::getP(*((pint_t
*)mappedAddress(addr
))); }
246 uint64_t getULEB128(pint_t
& addr
, pint_t end
);
247 int64_t getSLEB128(pint_t
& addr
, pint_t end
);
248 pint_t
getEncodedP(pint_t
& addr
, pint_t end
, uint8_t encoding
);
250 const void* mappedAddress(pint_t addr
);
252 CFISection
<A
>& _ehFrameSection
;
253 const uint8_t* _ehFrameContent
;
254 pint_t _ehFrameStartAddr
;
255 pint_t _ehFrameEndAddr
;
259 typedef typename
A::P::uint_t pint_t
;
260 typedef libunwind::CFI_Atom_Info
<OAS
> CFI_Atom_Info
;
262 void cfiParse(class Parser
<A
>& parser
, uint8_t* buffer
, CFI_Atom_Info cfiArray
[], uint32_t& cfiCount
, const pint_t cuStarts
[], uint32_t cuCount
);
263 bool needsRelocating();
265 static bool bigEndian();
267 void addCiePersonalityFixups(class Parser
<A
>& parser
, const CFI_Atom_Info
* cieInfo
);
268 static void warnFunc(void* ref
, uint64_t funcAddr
, const char* msg
);
272 template <typename A
>
273 class CUSection
: public Section
<A
>
276 CUSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
277 : Section
<A
>(f
, s
) { }
279 typedef typename
A::P::uint_t pint_t
;
280 typedef typename
A::P P
;
281 typedef typename
A::P::E E
;
283 virtual uint32_t computeAtomCount(class Parser
<A
>& parser
, struct Parser
<A
>::LabelAndCFIBreakIterator
& it
, const struct Parser
<A
>::CFI_CU_InfoArrays
&) { return 0; }
284 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; }
285 virtual void makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
&);
286 virtual bool addFollowOnFixups() const { return false; }
289 pint_t functionStartAddress
;
290 uint32_t functionSymbolIndex
;
291 uint32_t rangeLength
;
292 uint32_t compactUnwindInfo
;
293 const char* personality
;
300 void parse(class Parser
<A
>& parser
, uint32_t cnt
, Info array
[]);
301 static bool encodingMeansUseDwarf(compact_unwind_encoding_t enc
);
306 const char* personalityName(class Parser
<A
>& parser
, const macho_relocation_info
<P
>* reloc
);
308 static int infoSorter(const void* l
, const void* r
);
313 template <typename A
>
314 class TentativeDefinitionSection
: public Section
<A
>
317 TentativeDefinitionSection(Parser
<A
>& parser
, File
<A
>& f
)
318 : Section
<A
>(f
, "__DATA", "__comm/tent", ld::Section::typeTentativeDefs
) {}
320 virtual ld::Atom::ContentType
contentType() { return ld::Atom::typeZeroFill
; }
321 virtual bool addFollowOnFixups() const { return false; }
322 virtual Atom
<A
>* findAtomByAddress(typename
A::P::uint_t addr
) { throw "TentativeDefinitionSection::findAtomByAddress() should never be called"; }
323 virtual uint32_t computeAtomCount(class Parser
<A
>& parser
, struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
324 const struct Parser
<A
>::CFI_CU_InfoArrays
&);
325 virtual uint32_t appendAtoms(class Parser
<A
>& parser
, uint8_t* buffer
,
326 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
327 const struct Parser
<A
>::CFI_CU_InfoArrays
&);
328 virtual void makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
&) {}
330 typedef typename
A::P::uint_t pint_t
;
331 typedef typename
A::P P
;
335 template <typename A
>
336 class AbsoluteSymbolSection
: public Section
<A
>
339 AbsoluteSymbolSection(Parser
<A
>& parser
, File
<A
>& f
)
340 : Section
<A
>(f
, "__DATA", "__abs", ld::Section::typeAbsoluteSymbols
, true) {}
342 virtual ld::Atom::ContentType
contentType() { return ld::Atom::typeUnclassified
; }
343 virtual bool dontDeadStrip() { return false; }
344 virtual ld::Atom::Alignment
alignmentForAddress(typename
A::P::uint_t addr
) { return ld::Atom::Alignment(0); }
345 virtual bool addFollowOnFixups() const { return false; }
346 virtual Atom
<A
>* findAtomByAddress(typename
A::P::uint_t addr
) { throw "AbsoluteSymbolSection::findAtomByAddress() should never be called"; }
347 virtual uint32_t computeAtomCount(class Parser
<A
>& parser
, struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
348 const struct Parser
<A
>::CFI_CU_InfoArrays
&);
349 virtual uint32_t appendAtoms(class Parser
<A
>& parser
, uint8_t* buffer
,
350 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
351 const struct Parser
<A
>::CFI_CU_InfoArrays
&);
352 virtual void makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
&) {}
353 virtual Atom
<A
>* findAbsAtomForValue(typename
A::P::uint_t
);
356 typedef typename
A::P::uint_t pint_t
;
357 typedef typename
A::P P
;
361 template <typename A
>
362 class SymboledSection
: public Section
<A
>
365 SymboledSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
);
366 virtual ld::Atom::ContentType
contentType() { return _type
; }
367 virtual bool dontDeadStrip();
368 virtual uint32_t computeAtomCount(class Parser
<A
>& parser
, struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
369 const struct Parser
<A
>::CFI_CU_InfoArrays
&);
370 virtual uint32_t appendAtoms(class Parser
<A
>& parser
, uint8_t* buffer
,
371 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
372 const struct Parser
<A
>::CFI_CU_InfoArrays
&);
374 typedef typename
A::P::uint_t pint_t
;
375 typedef typename
A::P P
;
377 ld::Atom::ContentType _type
;
381 template <typename A
>
382 class TLVDefsSection
: public SymboledSection
<A
>
385 TLVDefsSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
) :
386 SymboledSection
<A
>(parser
, f
, s
) { }
393 template <typename A
>
394 class ImplicitSizeSection
: public Section
<A
>
397 ImplicitSizeSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
398 : Section
<A
>(f
, s
) { }
399 virtual uint32_t computeAtomCount(class Parser
<A
>& parser
, struct Parser
<A
>::LabelAndCFIBreakIterator
& it
, const struct Parser
<A
>::CFI_CU_InfoArrays
&);
400 virtual uint32_t appendAtoms(class Parser
<A
>& parser
, uint8_t* buffer
, struct Parser
<A
>::LabelAndCFIBreakIterator
& it
, const struct Parser
<A
>::CFI_CU_InfoArrays
&);
402 typedef typename
A::P::uint_t pint_t
;
403 typedef typename
A::P P
;
405 virtual bool addFollowOnFixups() const { return false; }
406 virtual const char* unlabeledAtomName(Parser
<A
>& parser
, pint_t addr
) = 0;
407 virtual ld::Atom::SymbolTableInclusion
symbolTableInclusion();
408 virtual pint_t
elementSizeAtAddress(pint_t addr
) = 0;
409 virtual ld::Atom::Scope
scopeAtAddress(Parser
<A
>& parser
, pint_t addr
) { return ld::Atom::scopeLinkageUnit
; }
410 virtual bool useElementAt(Parser
<A
>& parser
,
411 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
, pint_t addr
) = 0;
412 virtual ld::Atom::Definition
definition() { return ld::Atom::definitionRegular
; }
413 virtual ld::Atom::Combine
combine(Parser
<A
>& parser
, pint_t addr
) = 0;
414 virtual bool ignoreLabel(const char* label
) const { return (label
[0] == 'L'); }
418 template <typename A
>
419 class FixedSizeSection
: public ImplicitSizeSection
<A
>
422 FixedSizeSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
423 : ImplicitSizeSection
<A
>(parser
, f
, s
) { }
425 typedef typename
A::P::uint_t pint_t
;
426 typedef typename
A::P P
;
427 typedef typename
A::P::E E
;
429 virtual bool useElementAt(Parser
<A
>& parser
,
430 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
, pint_t addr
)
435 template <typename A
>
436 class Literal4Section
: public FixedSizeSection
<A
>
439 Literal4Section(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
440 : FixedSizeSection
<A
>(parser
, f
, s
) {}
442 typedef typename
A::P::uint_t pint_t
;
443 typedef typename
A::P P
;
445 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
) { return ld::Atom::Alignment(2); }
446 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "4-byte-literal"; }
447 virtual pint_t
elementSizeAtAddress(pint_t addr
) { return 4; }
448 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
) { return ld::Atom::combineByNameAndContent
; }
449 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
450 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
451 const ld::IndirectBindingTable
& ind
) const;
452 virtual bool ignoreLabel(const char* label
) const;
455 template <typename A
>
456 class Literal8Section
: public FixedSizeSection
<A
>
459 Literal8Section(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
460 : FixedSizeSection
<A
>(parser
, f
, s
) {}
462 typedef typename
A::P::uint_t pint_t
;
463 typedef typename
A::P P
;
465 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
) { return ld::Atom::Alignment(3); }
466 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "8-byte-literal"; }
467 virtual pint_t
elementSizeAtAddress(pint_t addr
) { return 8; }
468 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
) { return ld::Atom::combineByNameAndContent
; }
469 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
470 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
471 const ld::IndirectBindingTable
& ind
) const;
472 virtual bool ignoreLabel(const char* label
) const;
475 template <typename A
>
476 class Literal16Section
: public FixedSizeSection
<A
>
479 Literal16Section(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
480 : FixedSizeSection
<A
>(parser
, f
, s
) {}
482 typedef typename
A::P::uint_t pint_t
;
483 typedef typename
A::P P
;
485 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
) { return ld::Atom::Alignment(4); }
486 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "16-byte-literal"; }
487 virtual pint_t
elementSizeAtAddress(pint_t addr
) { return 16; }
488 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
) { return ld::Atom::combineByNameAndContent
; }
489 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
490 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
491 const ld::IndirectBindingTable
& ind
) const;
492 virtual bool ignoreLabel(const char* label
) const;
496 template <typename A
>
497 class NonLazyPointerSection
: public FixedSizeSection
<A
>
500 NonLazyPointerSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
501 : FixedSizeSection
<A
>(parser
, f
, s
) {}
503 typedef typename
A::P::uint_t pint_t
;
504 typedef typename
A::P P
;
506 virtual void makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
&);
507 virtual ld::Atom::ContentType
contentType() { return ld::Atom::typeNonLazyPointer
; }
508 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
) { return ld::Atom::Alignment(log2(sizeof(pint_t
))); }
509 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "non_lazy_ptr"; }
510 virtual pint_t
elementSizeAtAddress(pint_t addr
) { return sizeof(pint_t
); }
511 virtual ld::Atom::Scope
scopeAtAddress(Parser
<A
>& parser
, pint_t addr
);
512 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
);
513 virtual bool ignoreLabel(const char* label
) const { return true; }
514 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
515 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
516 const ld::IndirectBindingTable
& ind
) const;
519 static const char* targetName(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
);
520 static ld::Fixup::Kind
fixupKind();
523 template <typename A
>
524 class TLVPointerSection
: public FixedSizeSection
<A
>
527 TLVPointerSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
528 : FixedSizeSection
<A
>(parser
, f
, s
) {}
530 typedef typename
A::P::uint_t pint_t
;
531 typedef typename
A::P P
;
533 virtual ld::Atom::ContentType
contentType() { return ld::Atom::typeTLVPointer
; }
534 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
) { return ld::Atom::Alignment(log2(sizeof(pint_t
))); }
535 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "tlv_lazy_ptr"; }
536 virtual pint_t
elementSizeAtAddress(pint_t addr
) { return sizeof(pint_t
); }
537 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
);
538 virtual bool ignoreLabel(const char* label
) const { return true; }
539 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
540 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
541 const ld::IndirectBindingTable
& ind
) const;
544 static const char* targetName(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
, bool* isStatic
);
548 template <typename A
>
549 class CFStringSection
: public FixedSizeSection
<A
>
552 CFStringSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
553 : FixedSizeSection
<A
>(parser
, f
, s
) {}
555 typedef typename
A::P::uint_t pint_t
;
557 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
) { return ld::Atom::Alignment(log2(sizeof(pint_t
))); }
558 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "CFString"; }
559 virtual pint_t
elementSizeAtAddress(pint_t addr
) { return 4*sizeof(pint_t
); }
560 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
) { return ld::Atom::combineByNameAndReferences
; }
561 virtual bool ignoreLabel(const char* label
) const { return true; }
562 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
563 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
564 const ld::IndirectBindingTable
& ind
) const;
566 enum ContentType
{ contentUTF8
, contentUTF16
, contentUnknown
};
567 static const uint8_t* targetContent(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
,
568 ContentType
* ct
, unsigned int* count
);
572 template <typename A
>
573 class ObjC1ClassSection
: public FixedSizeSection
<A
>
576 ObjC1ClassSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
577 : FixedSizeSection
<A
>(parser
, f
, s
) {}
579 typedef typename
A::P::uint_t pint_t
;
580 typedef typename
A::P P
;
581 typedef typename
A::P::E E
;
583 virtual ld::Atom::Scope
scopeAtAddress(Parser
<A
>& , pint_t
) { return ld::Atom::scopeGlobal
; }
584 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
) { return ld::Atom::Alignment(2); }
585 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
);
586 virtual ld::Atom::SymbolTableInclusion
symbolTableInclusion() { return ld::Atom::symbolTableIn
; }
587 virtual pint_t
elementSizeAtAddress(pint_t addr
);
588 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
) { return ld::Atom::combineNever
; }
589 virtual bool ignoreLabel(const char* label
) const { return true; }
590 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
592 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
593 const ld::IndirectBindingTable
& ind
) const { return false; }
594 virtual bool addRelocFixup(class Parser
<A
>& parser
, const macho_relocation_info
<P
>*);
598 template <typename A
>
599 class ObjC2ClassRefsSection
: public FixedSizeSection
<A
>
602 ObjC2ClassRefsSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
603 : FixedSizeSection
<A
>(parser
, f
, s
) {}
605 typedef typename
A::P::uint_t pint_t
;
607 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
) { return ld::Atom::Alignment(log2(sizeof(pint_t
))); }
608 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "objc-class-ref"; }
609 virtual pint_t
elementSizeAtAddress(pint_t addr
) { return sizeof(pint_t
); }
610 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
) { return ld::Atom::combineByNameAndReferences
; }
611 virtual bool ignoreLabel(const char* label
) const { return true; }
612 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
613 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
614 const ld::IndirectBindingTable
& ind
) const;
616 const char* targetClassName(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
620 template <typename A
>
621 class ObjC2CategoryListSection
: public FixedSizeSection
<A
>
624 ObjC2CategoryListSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
625 : FixedSizeSection
<A
>(parser
, f
, s
) {}
627 typedef typename
A::P::uint_t pint_t
;
629 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
) { return ld::Atom::Alignment(log2(sizeof(pint_t
))); }
630 virtual ld::Atom::Scope
scopeAtAddress(Parser
<A
>& parser
, pint_t addr
) { return ld::Atom::scopeTranslationUnit
; }
631 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "objc-cat-list"; }
632 virtual pint_t
elementSizeAtAddress(pint_t addr
) { return sizeof(pint_t
); }
633 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
) { return ld::Atom::combineNever
; }
634 virtual bool ignoreLabel(const char* label
) const { return true; }
636 const char* targetClassName(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
640 template <typename A
>
641 class PointerToCStringSection
: public FixedSizeSection
<A
>
644 PointerToCStringSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
645 : FixedSizeSection
<A
>(parser
, f
, s
) {}
647 typedef typename
A::P::uint_t pint_t
;
649 virtual ld::Atom::Alignment
alignmentForAddress(pint_t addr
) { return ld::Atom::Alignment(log2(sizeof(pint_t
))); }
650 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "pointer-to-literal-cstring"; }
651 virtual pint_t
elementSizeAtAddress(pint_t addr
) { return sizeof(pint_t
); }
652 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
) { return ld::Atom::combineByNameAndReferences
; }
653 virtual bool ignoreLabel(const char* label
) const { return true; }
654 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
655 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
656 const ld::IndirectBindingTable
& ind
) const;
657 virtual const char* targetCString(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
661 template <typename A
>
662 class Objc1ClassReferences
: public PointerToCStringSection
<A
>
665 Objc1ClassReferences(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
666 : PointerToCStringSection
<A
>(parser
, f
, s
) {}
668 typedef typename
A::P::uint_t pint_t
;
669 typedef typename
A::P P
;
671 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "pointer-to-literal-objc-class-name"; }
672 virtual bool addRelocFixup(class Parser
<A
>& parser
, const macho_relocation_info
<P
>*);
673 virtual const char* targetCString(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
677 template <typename A
>
678 class CStringSection
: public ImplicitSizeSection
<A
>
681 CStringSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
682 : ImplicitSizeSection
<A
>(parser
, f
, s
) {}
684 typedef typename
A::P::uint_t pint_t
;
685 typedef typename
A::P P
;
687 virtual ld::Atom::ContentType
contentType() { return ld::Atom::typeCString
; }
688 virtual Atom
<A
>* findAtomByAddress(pint_t addr
);
689 virtual const char* unlabeledAtomName(Parser
<A
>&, pint_t
) { return "cstring"; }
690 virtual pint_t
elementSizeAtAddress(pint_t addr
);
691 virtual bool ignoreLabel(const char* label
) const;
692 virtual bool useElementAt(Parser
<A
>& parser
,
693 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
, pint_t addr
);
694 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
) { return ld::Atom::combineByNameAndContent
; }
695 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
696 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
697 const ld::IndirectBindingTable
& ind
) const;
702 template <typename A
>
703 class UTF16StringSection
: public SymboledSection
<A
>
706 UTF16StringSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
707 : SymboledSection
<A
>(parser
, f
, s
) {}
709 typedef typename
A::P::uint_t pint_t
;
710 typedef typename
A::P P
;
712 virtual ld::Atom::Combine
combine(Parser
<A
>&, pint_t
) { return ld::Atom::combineByNameAndContent
; }
713 virtual unsigned long contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const;
714 virtual bool canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
715 const ld::IndirectBindingTable
& ind
) const;
720 // Atoms in mach-o files
722 template <typename A
>
723 class Atom
: public ld::Atom
726 // overrides of ld::Atom
727 virtual const ld::File
* file() const;
728 virtual const char* translationUnitSource() const
729 { return sect().file().translationUnitSource(); }
730 virtual const char* name() const { return _name
; }
731 virtual uint64_t size() const { return _size
; }
732 virtual uint64_t objectAddress() const { return _objAddress
; }
733 virtual void copyRawContent(uint8_t buffer
[]) const;
734 virtual const uint8_t* rawContentPointer() const { return contentPointer(); }
735 virtual unsigned long contentHash(const ld::IndirectBindingTable
& ind
) const
736 { if ( _hash
== 0 ) _hash
= sect().contentHash(this, ind
); return _hash
; }
737 virtual bool canCoalesceWith(const ld::Atom
& rhs
, const ld::IndirectBindingTable
& ind
) const
738 { return sect().canCoalesceWith(this, rhs
, ind
); }
739 virtual ld::Fixup::iterator
fixupsBegin() const { return &machofile()._fixups
[_fixupsStartIndex
]; }
740 virtual ld::Fixup::iterator
fixupsEnd() const { return &machofile()._fixups
[_fixupsStartIndex
+_fixupsCount
]; }
741 virtual ld::Atom::UnwindInfo::iterator
beginUnwind() const { return &machofile()._unwindInfos
[_unwindInfoStartIndex
]; }
742 virtual ld::Atom::UnwindInfo::iterator
endUnwind() const { return &machofile()._unwindInfos
[_unwindInfoStartIndex
+_unwindInfoCount
]; }
743 virtual ld::Atom::LineInfo::iterator
beginLineInfo() const{ return &machofile()._lineInfos
[_lineInfoStartIndex
]; }
744 virtual ld::Atom::LineInfo::iterator
endLineInfo() const { return &machofile()._lineInfos
[_lineInfoStartIndex
+_lineInfoCount
]; }
745 virtual void setFile(const ld::File
* f
);
749 enum { kFixupStartIndexBits
= 32,
750 kLineInfoStartIndexBits
= 32,
751 kUnwindInfoStartIndexBits
= 24,
752 kFixupCountBits
= 24,
753 kLineInfoCountBits
= 12,
754 kUnwindInfoCountBits
= 4
755 }; // must sum to 128
758 // methods for all atoms from mach-o object file
759 Section
<A
>& sect() const { return (Section
<A
>&)section(); }
760 File
<A
>& machofile() const { return ((Section
<A
>*)(this->_section
))->file(); }
761 void setFixupsRange(uint32_t s
, uint32_t c
);
762 void setUnwindInfoRange(uint32_t s
, uint32_t c
);
763 void extendUnwindInfoRange();
764 void setLineInfoRange(uint32_t s
, uint32_t c
);
765 bool roomForMoreLineInfoCount() { return (_lineInfoCount
< ((1<<kLineInfoCountBits
)-1)); }
766 void incrementLineInfoCount() { assert(roomForMoreLineInfoCount()); ++_lineInfoCount
; }
767 void incrementFixupCount() { if (_fixupsCount
== ((1 << kFixupCountBits
)-1))
768 throwf("too may fixups in %s", name()); ++_fixupsCount
; }
769 const uint8_t* contentPointer() const;
770 uint32_t fixupCount() const { return _fixupsCount
; }
771 void verifyAlignment(const macho_section
<typename
A::P
>&) const;
773 typedef typename
A::P P
;
774 typedef typename
A::P::E E
;
775 typedef typename
A::P::uint_t pint_t
;
776 // constuct via all attributes
777 Atom(Section
<A
>& sct
, const char* nm
, pint_t addr
, uint64_t sz
,
778 ld::Atom::Definition d
, ld::Atom::Combine c
, ld::Atom::Scope s
,
779 ld::Atom::ContentType ct
, ld::Atom::SymbolTableInclusion i
,
780 bool dds
, bool thumb
, bool al
, ld::Atom::Alignment a
)
781 : ld::Atom((ld::Section
&)sct
, d
, c
, s
, ct
, i
, dds
, thumb
, al
, a
),
782 _size(sz
), _objAddress(addr
), _name(nm
), _hash(0),
783 _fixupsStartIndex(0), _lineInfoStartIndex(0),
784 _unwindInfoStartIndex(0), _fixupsCount(0),
785 _lineInfoCount(0), _unwindInfoCount(0) { }
786 // construct via symbol table entry
787 Atom(Section
<A
>& sct
, Parser
<A
>& parser
, const macho_nlist
<P
>& sym
,
788 uint64_t sz
, bool alias
=false)
789 : ld::Atom((ld::Section
&)sct
, parser
.definitionFromSymbol(sym
),
790 parser
.combineFromSymbol(sym
), parser
.scopeFromSymbol(sym
),
791 parser
.resolverFromSymbol(sym
) ? ld::Atom::typeResolver
: sct
.contentType(),
792 parser
.inclusionFromSymbol(sym
),
793 (parser
.dontDeadStripFromSymbol(sym
) && !sct
.dontDeadStripIfReferencesLive()) || sct
.dontDeadStrip(),
794 parser
.isThumbFromSymbol(sym
), alias
,
795 sct
.alignmentForAddress(sym
.n_value())),
796 _size(sz
), _objAddress(sym
.n_value()),
797 _name(parser
.nameFromSymbol(sym
)), _hash(0),
798 _fixupsStartIndex(0), _lineInfoStartIndex(0),
799 _unwindInfoStartIndex(0), _fixupsCount(0),
800 _lineInfoCount(0), _unwindInfoCount(0) {
801 // <rdar://problem/6783167> support auto-hidden weak symbols
802 if ( _scope
== ld::Atom::scopeGlobal
&&
803 (sym
.n_desc() & (N_WEAK_DEF
|N_WEAK_REF
)) == (N_WEAK_DEF
|N_WEAK_REF
) )
805 this->verifyAlignment(*sct
.machoSection());
806 if ( sct
.dontDeadStripIfReferencesLive() )
807 this->setDontDeadStripIfReferencesLive();
811 friend class Parser
<A
>;
812 friend class Section
<A
>;
813 friend class CStringSection
<A
>;
814 friend class AbsoluteSymbolSection
<A
>;
819 mutable unsigned long _hash
;
821 uint64_t _fixupsStartIndex
: kFixupStartIndexBits
,
822 _lineInfoStartIndex
: kLineInfoStartIndexBits
,
823 _unwindInfoStartIndex
: kUnwindInfoStartIndexBits
,
824 _fixupsCount
: kFixupCountBits
,
825 _lineInfoCount
: kLineInfoCountBits
,
826 _unwindInfoCount
: kUnwindInfoCountBits
;
828 static std::map
<const ld::Atom
*, const ld::File
*> _s_fileOverride
;
831 template <typename A
>
832 std::map
<const ld::Atom
*, const ld::File
*> Atom
<A
>::_s_fileOverride
;
834 template <typename A
>
835 void Atom
<A
>::setFile(const ld::File
* f
) {
836 _s_fileOverride
[this] = f
;
839 template <typename A
>
840 const ld::File
* Atom
<A
>::file() const
842 std::map
<const ld::Atom
*, const ld::File
*>::iterator pos
= _s_fileOverride
.find(this);
843 if ( pos
!= _s_fileOverride
.end() )
846 return §().file();
849 template <typename A
>
850 void Atom
<A
>::setFixupsRange(uint32_t startIndex
, uint32_t count
)
852 if ( count
>= (1 << kFixupCountBits
) )
853 throwf("too many fixups in function %s", this->name());
854 if ( startIndex
>= (1 << kFixupStartIndexBits
) )
855 throwf("too many fixups in file");
856 assert(((startIndex
+count
) <= sect().file()._fixups
.size()) && "fixup index out of range");
857 _fixupsStartIndex
= startIndex
;
858 _fixupsCount
= count
;
861 template <typename A
>
862 void Atom
<A
>::setUnwindInfoRange(uint32_t startIndex
, uint32_t count
)
864 if ( count
>= (1 << kUnwindInfoCountBits
) )
865 throwf("too many compact unwind infos in function %s", this->name());
866 if ( startIndex
>= (1 << kUnwindInfoStartIndexBits
) )
867 throwf("too many compact unwind infos (%d) in file", startIndex
);
868 assert((startIndex
+count
) <= sect().file()._unwindInfos
.size() && "unwindinfo index out of range");
869 _unwindInfoStartIndex
= startIndex
;
870 _unwindInfoCount
= count
;
873 template <typename A
>
874 void Atom
<A
>::extendUnwindInfoRange()
876 if ( _unwindInfoCount
+1 >= (1 << kUnwindInfoCountBits
) )
877 throwf("too many compact unwind infos in function %s", this->name());
878 _unwindInfoCount
+= 1;
881 template <typename A
>
882 void Atom
<A
>::setLineInfoRange(uint32_t startIndex
, uint32_t count
)
884 assert((count
< (1 << kLineInfoCountBits
)) && "too many line infos");
885 assert((startIndex
+count
) < sect().file()._lineInfos
.size() && "line info index out of range");
886 _lineInfoStartIndex
= startIndex
;
887 _lineInfoCount
= count
;
890 template <typename A
>
891 const uint8_t* Atom
<A
>::contentPointer() const
893 const macho_section
<P
>* sct
= this->sect().machoSection();
894 if ( this->_objAddress
> sct
->addr() + sct
->size() )
895 throwf("malformed .o file, symbol has address 0x%0llX which is outside range of its section", (uint64_t)this->_objAddress
);
896 uint32_t fileOffset
= sct
->offset() - sct
->addr() + this->_objAddress
;
897 return this->sect().file().fileContent()+fileOffset
;
901 template <typename A
>
902 void Atom
<A
>::copyRawContent(uint8_t buffer
[]) const
905 if ( this->contentType() == ld::Atom::typeZeroFill
) {
906 bzero(buffer
, _size
);
908 else if ( _size
!= 0 ) {
909 memcpy(buffer
, this->contentPointer(), _size
);
914 void Atom
<arm
>::verifyAlignment(const macho_section
<P
>&) const
916 if ( (this->section().type() == ld::Section::typeCode
) && ! isThumb() ) {
917 if ( ((_objAddress
% 4) != 0) || (this->alignment().powerOf2
< 2) )
918 warning("ARM function not 4-byte aligned: %s from %s", this->name(), this->file()->path());
922 #if SUPPORT_ARCH_arm64
924 void Atom
<arm64
>::verifyAlignment(const macho_section
<P
>& sect
) const
926 if ( (this->section().type() == ld::Section::typeCode
) && (sect
.size() != 0) ) {
927 if ( ((_objAddress
% 4) != 0) || (this->alignment().powerOf2
< 2) )
928 warning("arm64 function not 4-byte aligned: %s from %s", this->name(), this->file()->path());
933 template <typename A
>
934 void Atom
<A
>::verifyAlignment(const macho_section
<P
>&) const
939 class AliasAtom
: public ld::Atom
942 AliasAtom(const char* name
, bool hidden
, const ld::File
* file
, const char* aliasOfName
) :
943 ld::Atom(_s_section
, ld::Atom::definitionRegular
, ld::Atom::combineNever
,
944 (hidden
? ld::Atom::scopeLinkageUnit
: ld::Atom::scopeGlobal
),
945 ld::Atom::typeUnclassified
, ld::Atom::symbolTableIn
,
946 false, false, true, 0),
949 _fixup(0, ld::Fixup::k1of1
, ld::Fixup::kindNoneFollowOn
, ld::Fixup::bindingByNameUnbound
, aliasOfName
) { }
951 virtual const ld::File
* file() const { return _file
; }
952 virtual const char* translationUnitSource() const
954 virtual const char* name() const { return _name
; }
955 virtual uint64_t size() const { return 0; }
956 virtual uint64_t objectAddress() const { return 0; }
957 virtual void copyRawContent(uint8_t buffer
[]) const { }
958 virtual ld::Fixup::iterator
fixupsBegin() const { return &((ld::Fixup
*)&_fixup
)[0]; }
959 virtual ld::Fixup::iterator
fixupsEnd() const { return &((ld::Fixup
*)&_fixup
)[1]; }
962 static ld::Section _s_section
;
964 const ld::File
* _file
;
969 ld::Section
AliasAtom::_s_section("__LD", "__aliases", ld::Section::typeTempAlias
, true);
972 template <typename A
>
976 static bool validFile(const uint8_t* fileContent
, bool subtypeMustMatch
=false,
977 cpu_subtype_t subtype
=0);
978 static const char* fileKind(const uint8_t* fileContent
);
979 static Options::Platform
findPlatform(const macho_header
<typename
A::P
>* header
);
980 static bool hasObjC2Categories(const uint8_t* fileContent
);
981 static bool hasObjC1Categories(const uint8_t* fileContent
);
982 static bool getNonLocalSymbols(const uint8_t* fileContnet
, std::vector
<const char*> &syms
);
983 static ld::relocatable::File
* parse(const uint8_t* fileContent
, uint64_t fileLength
,
984 const char* path
, time_t modTime
, ld::File::Ordinal ordinal
,
985 const ParserOptions
& opts
) {
986 Parser
p(fileContent
, fileLength
, path
, modTime
,
987 ordinal
, opts
.warnUnwindConversionProblems
,
988 opts
.keepDwarfUnwind
, opts
.forceDwarfConversion
,
989 opts
.neverConvertDwarf
, opts
.verboseOptimizationHints
,
990 opts
.ignoreMismatchPlatform
);
991 return p
.parse(opts
);
994 typedef typename
A::P P
;
995 typedef typename
A::P::E E
;
996 typedef typename
A::P::uint_t pint_t
;
998 struct SourceLocation
{
1000 SourceLocation(Atom
<A
>* a
, uint32_t o
) : atom(a
), offsetInAtom(o
) {}
1002 uint32_t offsetInAtom
;
1007 const char* name
; // only used if targetAtom is NULL
1009 bool weakImport
; // only used if targetAtom is NULL
1012 struct FixupInAtom
{
1013 FixupInAtom(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, Atom
<A
>* target
) :
1014 fixup(src
.offsetInAtom
, c
, k
, target
), atom(src
.atom
) { src
.atom
->incrementFixupCount(); }
1016 FixupInAtom(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, ld::Fixup::TargetBinding b
, Atom
<A
>* target
) :
1017 fixup(src
.offsetInAtom
, c
, k
, b
, target
), atom(src
.atom
) { src
.atom
->incrementFixupCount(); }
1019 FixupInAtom(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, bool wi
, const char* name
) :
1020 fixup(src
.offsetInAtom
, c
, k
, wi
, name
), atom(src
.atom
) { src
.atom
->incrementFixupCount(); }
1022 FixupInAtom(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, ld::Fixup::TargetBinding b
, const char* name
) :
1023 fixup(src
.offsetInAtom
, c
, k
, b
, name
), atom(src
.atom
) { src
.atom
->incrementFixupCount(); }
1025 FixupInAtom(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, uint64_t addend
) :
1026 fixup(src
.offsetInAtom
, c
, k
, addend
), atom(src
.atom
) { src
.atom
->incrementFixupCount(); }
1028 FixupInAtom(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
) :
1029 fixup(src
.offsetInAtom
, c
, k
, (uint64_t)0), atom(src
.atom
) { src
.atom
->incrementFixupCount(); }
1035 void addFixup(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, Atom
<A
>* target
) {
1036 _allFixups
.push_back(FixupInAtom(src
, c
, k
, target
));
1039 void addFixup(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, ld::Fixup::TargetBinding b
, Atom
<A
>* target
) {
1040 _allFixups
.push_back(FixupInAtom(src
, c
, k
, b
, target
));
1043 void addFixup(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, bool wi
, const char* name
) {
1044 _allFixups
.push_back(FixupInAtom(src
, c
, k
, wi
, name
));
1047 void addFixup(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, ld::Fixup::TargetBinding b
, const char* name
) {
1048 _allFixups
.push_back(FixupInAtom(src
, c
, k
, b
, name
));
1051 void addFixup(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
, uint64_t addend
) {
1052 _allFixups
.push_back(FixupInAtom(src
, c
, k
, addend
));
1055 void addFixup(const SourceLocation
& src
, ld::Fixup::Cluster c
, ld::Fixup::Kind k
) {
1056 _allFixups
.push_back(FixupInAtom(src
, c
, k
));
1059 const char* path() { return _path
; }
1060 uint32_t symbolCount() { return _symbolCount
; }
1061 uint32_t indirectSymbol(uint32_t indirectIndex
);
1062 const macho_nlist
<P
>& symbolFromIndex(uint32_t index
);
1063 const char* nameFromSymbol(const macho_nlist
<P
>& sym
);
1064 ld::Atom::Scope
scopeFromSymbol(const macho_nlist
<P
>& sym
);
1065 static ld::Atom::Definition
definitionFromSymbol(const macho_nlist
<P
>& sym
);
1066 static ld::Atom::Combine
combineFromSymbol(const macho_nlist
<P
>& sym
);
1067 ld::Atom::SymbolTableInclusion
inclusionFromSymbol(const macho_nlist
<P
>& sym
);
1068 static bool dontDeadStripFromSymbol(const macho_nlist
<P
>& sym
);
1069 static bool isThumbFromSymbol(const macho_nlist
<P
>& sym
);
1070 static bool weakImportFromSymbol(const macho_nlist
<P
>& sym
);
1071 static bool resolverFromSymbol(const macho_nlist
<P
>& sym
);
1072 static bool altEntryFromSymbol(const macho_nlist
<P
>& sym
);
1073 uint32_t symbolIndexFromIndirectSectionAddress(pint_t
,const macho_section
<P
>*);
1074 const macho_section
<P
>* firstMachOSection() { return _sectionsStart
; }
1075 const macho_section
<P
>* machOSectionFromSectionIndex(uint32_t index
);
1076 uint32_t machOSectionCount() { return _machOSectionsCount
; }
1077 uint32_t undefinedStartIndex() { return _undefinedStartIndex
; }
1078 uint32_t undefinedEndIndex() { return _undefinedEndIndex
; }
1079 void addFixup(FixupInAtom f
) { _allFixups
.push_back(f
); }
1080 Section
<A
>* sectionForNum(unsigned int sectNum
);
1081 Section
<A
>* sectionForAddress(pint_t addr
);
1082 Atom
<A
>* findAtomByAddress(pint_t addr
);
1083 Atom
<A
>* findAtomByAddressOrNullIfStub(pint_t addr
);
1084 Atom
<A
>* findAtomByAddressOrLocalTargetOfStub(pint_t addr
, uint32_t* offsetInAtom
);
1085 Atom
<A
>* findAtomByName(const char* name
); // slow!
1086 void findTargetFromAddress(pint_t addr
, TargetDesc
& target
);
1087 void findTargetFromAddress(pint_t baseAddr
, pint_t addr
, TargetDesc
& target
);
1088 void findTargetFromAddressAndSectionNum(pint_t addr
, unsigned int sectNum
,
1089 TargetDesc
& target
);
1090 uint32_t tentativeDefinitionCount() { return _tentativeDefinitionCount
; }
1091 uint32_t absoluteSymbolCount() { return _absoluteSymbolCount
; }
1093 uint32_t fileLength() const { return _fileLength
; }
1094 bool hasStubsSection() { return (_stubsSectionNum
!= 0); }
1095 unsigned int stubsSectionNum() { return _stubsSectionNum
; }
1096 void addDtraceExtraInfos(const SourceLocation
& src
, const char* provider
);
1097 const char* scanSymbolTableForAddress(uint64_t addr
);
1098 bool warnUnwindConversionProblems() { return _warnUnwindConversionProblems
; }
1099 bool hasDataInCodeLabels() { return _hasDataInCodeLabels
; }
1100 bool keepDwarfUnwind() { return _keepDwarfUnwind
; }
1101 bool forceDwarfConversion() { return _forceDwarfConversion
; }
1102 bool verboseOptimizationHints() { return _verboseOptimizationHints
; }
1103 bool neverConvertDwarf() { return _neverConvertDwarf
; }
1104 bool armUsesZeroCostExceptions() { return _armUsesZeroCostExceptions
; }
1105 uint8_t maxDefaultCommonAlignment() { return _maxDefaultCommonAlignment
; }
1108 macho_data_in_code_entry
<P
>* dataInCodeStart() { return _dataInCodeStart
; }
1109 macho_data_in_code_entry
<P
>* dataInCodeEnd() { return _dataInCodeEnd
; }
1110 const uint8_t* optimizationHintsStart() { return _lohStart
; }
1111 const uint8_t* optimizationHintsEnd() { return _lohEnd
; }
1112 bool hasOptimizationHints() { return _lohStart
!= _lohEnd
; }
1115 void addFixups(const SourceLocation
& src
, ld::Fixup::Kind kind
, const TargetDesc
& target
);
1116 void addFixups(const SourceLocation
& src
, ld::Fixup::Kind kind
, const TargetDesc
& target
, const TargetDesc
& picBase
);
1120 struct LabelAndCFIBreakIterator
{
1121 typedef typename CFISection
<A
>::CFI_Atom_Info CFI_Atom_Info
;
1122 LabelAndCFIBreakIterator(const uint32_t* ssa
, uint32_t ssc
, const pint_t
* cfisa
,
1123 uint32_t cfisc
, bool ols
)
1124 : sortedSymbolIndexes(ssa
), sortedSymbolCount(ssc
), cfiStartsArray(cfisa
),
1125 cfiStartsCount(cfisc
), fileHasOverlappingSymbols(ols
),
1126 newSection(false), cfiIndex(0), symIndex(0) {}
1127 bool next(Parser
<A
>& parser
, const Section
<A
>& sect
, uint32_t sectNum
, pint_t startAddr
, pint_t endAddr
,
1128 pint_t
* addr
, pint_t
* size
, const macho_nlist
<P
>** sym
);
1129 pint_t
peek(Parser
<A
>& parser
, pint_t startAddr
, pint_t endAddr
);
1130 void beginSection() { newSection
= true; symIndex
= 0; }
1132 const uint32_t* const sortedSymbolIndexes
;
1133 const uint32_t sortedSymbolCount
;
1134 const pint_t
* cfiStartsArray
;
1135 const uint32_t cfiStartsCount
;
1136 const bool fileHasOverlappingSymbols
;
1142 struct CFI_CU_InfoArrays
{
1143 typedef typename CFISection
<A
>::CFI_Atom_Info CFI_Atom_Info
;
1144 typedef typename CUSection
<A
>::Info CU_Info
;
1145 CFI_CU_InfoArrays(const CFI_Atom_Info
* cfiAr
, uint32_t cfiC
, CU_Info
* cuAr
, uint32_t cuC
)
1146 : cfiArray(cfiAr
), cuArray(cuAr
), cfiCount(cfiC
), cuCount(cuC
) {}
1147 const CFI_Atom_Info
* const cfiArray
;
1148 CU_Info
* const cuArray
;
1149 const uint32_t cfiCount
;
1150 const uint32_t cuCount
;
1156 friend class Section
<A
>;
1158 enum SectionType
{ sectionTypeIgnore
, sectionTypeLiteral4
, sectionTypeLiteral8
, sectionTypeLiteral16
,
1159 sectionTypeNonLazy
, sectionTypeCFI
, sectionTypeCString
, sectionTypeCStringPointer
,
1160 sectionTypeUTF16Strings
, sectionTypeCFString
, sectionTypeObjC2ClassRefs
, typeObjC2CategoryList
,
1161 sectionTypeObjC1Classes
, sectionTypeSymboled
, sectionTypeObjC1ClassRefs
,
1162 sectionTypeTentativeDefinitions
, sectionTypeAbsoluteSymbols
, sectionTypeTLVDefs
,
1163 sectionTypeCompactUnwind
, sectionTypeTLVPointers
};
1165 template <typename P
>
1166 struct MachOSectionAndSectionClass
1168 const macho_section
<P
>* sect
;
1171 static int sorter(const void* l
, const void* r
) {
1172 const MachOSectionAndSectionClass
<P
>* left
= (MachOSectionAndSectionClass
<P
>*)l
;
1173 const MachOSectionAndSectionClass
<P
>* right
= (MachOSectionAndSectionClass
<P
>*)r
;
1174 int64_t diff
= left
->sect
->addr() - right
->sect
->addr();
1184 struct ParserAndSectionsArray
{ Parser
* parser
; const uint32_t* sortedSectionsArray
; };
1187 Parser(const uint8_t* fileContent
, uint64_t fileLength
,
1188 const char* path
, time_t modTime
, ld::File::Ordinal ordinal
,
1189 bool warnUnwindConversionProblems
, bool keepDwarfUnwind
,
1190 bool forceDwarfConversion
, bool neverConvertDwarf
,
1191 bool verboseOptimizationHints
, bool ignoreMismatchPlatform
);
1192 ld::relocatable::File
* parse(const ParserOptions
& opts
);
1193 static uint8_t loadCommandSizeMask();
1194 bool parseLoadCommands(Options::Platform platform
, uint32_t minOSVersion
, bool simulator
, bool ignoreMismatchPlatform
);
1195 void makeSections();
1196 void prescanSymbolTable();
1197 void makeSortedSymbolsArray(uint32_t symArray
[], const uint32_t sectionArray
[]);
1198 void makeSortedSectionsArray(uint32_t array
[]);
1199 static int pointerSorter(const void* l
, const void* r
);
1200 static int symbolIndexSorter(void* extra
, const void* l
, const void* r
);
1201 static int sectionIndexSorter(void* extra
, const void* l
, const void* r
);
1203 void parseDebugInfo();
1205 void appendAliasAtoms(uint8_t* atomBuffer
);
1206 static bool isConstFunStabs(const char *stabStr
);
1207 bool read_comp_unit(const char ** name
, const char ** comp_dir
,
1208 uint64_t *stmt_list
);
1209 pint_t
realAddr(pint_t addr
);
1210 const char* getDwarfString(uint64_t form
, const uint8_t*& p
);
1211 uint64_t getDwarfOffset(uint64_t form
, const uint8_t*& di
, bool dwarf64
);
1212 bool skip_form(const uint8_t ** offset
, const uint8_t * end
,
1213 uint64_t form
, uint8_t addr_size
, bool dwarf64
);
1216 // filled in by constructor
1217 const uint8_t* _fileContent
;
1218 uint32_t _fileLength
;
1221 ld::File::Ordinal _ordinal
;
1223 // filled in by parseLoadCommands()
1225 const macho_nlist
<P
>* _symbols
;
1226 uint32_t _symbolCount
;
1227 uint32_t _indirectSymbolCount
;
1228 const char* _strings
;
1229 uint32_t _stringsSize
;
1230 const uint32_t* _indirectTable
;
1231 uint32_t _indirectTableCount
;
1232 uint32_t _undefinedStartIndex
;
1233 uint32_t _undefinedEndIndex
;
1234 const macho_section
<P
>* _sectionsStart
;
1235 uint32_t _machOSectionsCount
;
1237 macho_data_in_code_entry
<P
>* _dataInCodeStart
;
1238 macho_data_in_code_entry
<P
>* _dataInCodeEnd
;
1239 const uint8_t* _lohStart
;
1240 const uint8_t* _lohEnd
;
1242 // filled in by parse()
1243 CFISection
<A
>* _EHFrameSection
;
1244 CUSection
<A
>* _compactUnwindSection
;
1245 AbsoluteSymbolSection
<A
>* _absoluteSection
;
1246 uint32_t _tentativeDefinitionCount
;
1247 uint32_t _absoluteSymbolCount
;
1248 uint32_t _symbolsInSections
;
1249 bool _hasLongBranchStubs
;
1250 bool _AppleObjc
; // FSF has objc that uses different data layout
1251 bool _overlappingSymbols
;
1252 bool _warnUnwindConversionProblems
;
1253 bool _hasDataInCodeLabels
;
1254 bool _keepDwarfUnwind
;
1255 bool _forceDwarfConversion
;
1256 bool _neverConvertDwarf
;
1257 bool _verboseOptimizationHints
;
1258 bool _armUsesZeroCostExceptions
;
1259 bool _ignoreMismatchPlatform
;
1260 bool _treateBitcodeAsData
;
1262 uint8_t _maxDefaultCommonAlignment
;
1263 unsigned int _stubsSectionNum
;
1264 const macho_section
<P
>* _stubsMachOSection
;
1265 std::vector
<const char*> _dtraceProviderInfo
;
1266 std::vector
<FixupInAtom
> _allFixups
;
1271 template <typename A
>
1272 Parser
<A
>::Parser(const uint8_t* fileContent
, uint64_t fileLength
, const char* path
, time_t modTime
,
1273 ld::File::Ordinal ordinal
, bool convertDUI
, bool keepDwarfUnwind
, bool forceDwarfConversion
,
1274 bool neverConvertDwarf
, bool verboseOptimizationHints
, bool ignoreMismatchPlatform
)
1275 : _fileContent(fileContent
), _fileLength(fileLength
), _path(path
), _modTime(modTime
),
1276 _ordinal(ordinal
), _file(NULL
),
1277 _symbols(NULL
), _symbolCount(0), _indirectSymbolCount(0), _strings(NULL
), _stringsSize(0),
1278 _indirectTable(NULL
), _indirectTableCount(0),
1279 _undefinedStartIndex(0), _undefinedEndIndex(0),
1280 _sectionsStart(NULL
), _machOSectionsCount(0), _hasUUID(false),
1281 _dataInCodeStart(NULL
), _dataInCodeEnd(NULL
),
1282 _lohStart(NULL
), _lohEnd(NULL
),
1283 _EHFrameSection(NULL
), _compactUnwindSection(NULL
), _absoluteSection(NULL
),
1284 _tentativeDefinitionCount(0), _absoluteSymbolCount(0),
1285 _symbolsInSections(0), _hasLongBranchStubs(false), _AppleObjc(false),
1286 _overlappingSymbols(false), _warnUnwindConversionProblems(convertDUI
), _hasDataInCodeLabels(false),
1287 _keepDwarfUnwind(keepDwarfUnwind
), _forceDwarfConversion(forceDwarfConversion
),
1288 _neverConvertDwarf(neverConvertDwarf
),
1289 _verboseOptimizationHints(verboseOptimizationHints
),
1290 _ignoreMismatchPlatform(ignoreMismatchPlatform
),
1291 _stubsSectionNum(0), _stubsMachOSection(NULL
)
1297 bool Parser
<x86
>::validFile(const uint8_t* fileContent
, bool, cpu_subtype_t
)
1299 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1300 if ( header
->magic() != MH_MAGIC
)
1302 if ( header
->cputype() != CPU_TYPE_I386
)
1304 if ( header
->filetype() != MH_OBJECT
)
1310 bool Parser
<x86_64
>::validFile(const uint8_t* fileContent
, bool, cpu_subtype_t
)
1312 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1313 if ( header
->magic() != MH_MAGIC_64
)
1315 if ( header
->cputype() != CPU_TYPE_X86_64
)
1317 if ( header
->filetype() != MH_OBJECT
)
1323 bool Parser
<arm
>::validFile(const uint8_t* fileContent
, bool subtypeMustMatch
, cpu_subtype_t subtype
)
1325 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1326 if ( header
->magic() != MH_MAGIC
)
1328 if ( header
->cputype() != CPU_TYPE_ARM
)
1330 if ( header
->filetype() != MH_OBJECT
)
1332 if ( subtypeMustMatch
) {
1333 if ( (cpu_subtype_t
)header
->cpusubtype() == subtype
)
1335 // hack until libcc_kext.a is made fat
1336 if ( header
->cpusubtype() == CPU_SUBTYPE_ARM_ALL
)
1345 bool Parser
<arm64
>::validFile(const uint8_t* fileContent
, bool subtypeMustMatch
, cpu_subtype_t subtype
)
1347 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1348 if ( header
->magic() != MH_MAGIC_64
)
1350 if ( header
->cputype() != CPU_TYPE_ARM64
)
1352 if ( header
->filetype() != MH_OBJECT
)
1359 const char* Parser
<x86
>::fileKind(const uint8_t* fileContent
)
1361 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1362 if ( header
->magic() != MH_MAGIC
)
1364 if ( header
->cputype() != CPU_TYPE_I386
)
1370 const char* Parser
<x86_64
>::fileKind(const uint8_t* fileContent
)
1372 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1373 if ( header
->magic() != MH_MAGIC_64
)
1375 if ( header
->cputype() != CPU_TYPE_X86_64
)
1381 const char* Parser
<arm
>::fileKind(const uint8_t* fileContent
)
1383 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1384 if ( header
->magic() != MH_MAGIC
)
1386 if ( header
->cputype() != CPU_TYPE_ARM
)
1388 for (const ArchInfo
* t
=archInfoArray
; t
->archName
!= NULL
; ++t
) {
1389 if ( (t
->cpuType
== CPU_TYPE_ARM
) && ((cpu_subtype_t
)header
->cpusubtype() == t
->cpuSubType
) ) {
1396 #if SUPPORT_ARCH_arm64
1398 const char* Parser
<arm64
>::fileKind(const uint8_t* fileContent
)
1400 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1401 if ( header
->magic() != MH_MAGIC_64
)
1403 if ( header
->cputype() != CPU_TYPE_ARM64
)
1409 template <typename A
>
1410 bool Parser
<A
>::hasObjC2Categories(const uint8_t* fileContent
)
1412 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1413 const uint32_t cmd_count
= header
->ncmds();
1414 const macho_load_command
<P
>* const cmds
= (macho_load_command
<P
>*)((char*)header
+ sizeof(macho_header
<P
>));
1415 const macho_load_command
<P
>* const cmdsEnd
= (macho_load_command
<P
>*)((char*)header
+ sizeof(macho_header
<P
>) + header
->sizeofcmds());
1416 const macho_load_command
<P
>* cmd
= cmds
;
1417 for (uint32_t i
= 0; i
< cmd_count
; ++i
) {
1418 if ( cmd
->cmd() == macho_segment_command
<P
>::CMD
) {
1419 const macho_segment_command
<P
>* segment
= (macho_segment_command
<P
>*)cmd
;
1420 const macho_section
<P
>* sectionsStart
= (macho_section
<P
>*)((char*)segment
+ sizeof(macho_segment_command
<P
>));
1421 for (uint32_t si
=0; si
< segment
->nsects(); ++si
) {
1422 const macho_section
<P
>* sect
= §ionsStart
[si
];
1423 if ( (sect
->size() > 0)
1424 && (strcmp(sect
->sectname(), "__objc_catlist") == 0)
1425 && (strcmp(sect
->segname(), "__DATA") == 0) ) {
1430 cmd
= (const macho_load_command
<P
>*)(((char*)cmd
)+cmd
->cmdsize());
1431 if ( cmd
> cmdsEnd
)
1432 throwf("malformed mach-o file, load command #%d is outside size of load commands", i
);
1438 template <typename A
>
1439 bool Parser
<A
>::hasObjC1Categories(const uint8_t* fileContent
)
1441 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1442 const uint32_t cmd_count
= header
->ncmds();
1443 const macho_load_command
<P
>* const cmds
= (macho_load_command
<P
>*)((char*)header
+ sizeof(macho_header
<P
>));
1444 const macho_load_command
<P
>* const cmdsEnd
= (macho_load_command
<P
>*)((char*)header
+ sizeof(macho_header
<P
>) + header
->sizeofcmds());
1445 const macho_load_command
<P
>* cmd
= cmds
;
1446 for (uint32_t i
= 0; i
< cmd_count
; ++i
) {
1447 if ( cmd
->cmd() == macho_segment_command
<P
>::CMD
) {
1448 const macho_segment_command
<P
>* segment
= (macho_segment_command
<P
>*)cmd
;
1449 const macho_section
<P
>* sectionsStart
= (macho_section
<P
>*)((char*)segment
+ sizeof(macho_segment_command
<P
>));
1450 for (uint32_t si
=0; si
< segment
->nsects(); ++si
) {
1451 const macho_section
<P
>* sect
= §ionsStart
[si
];
1452 if ( (sect
->size() > 0)
1453 && (strcmp(sect
->sectname(), "__category") == 0)
1454 && (strcmp(sect
->segname(), "__OBJC") == 0) ) {
1459 cmd
= (const macho_load_command
<P
>*)(((char*)cmd
)+cmd
->cmdsize());
1460 if ( cmd
> cmdsEnd
)
1461 throwf("malformed mach-o file, load command #%d is outside size of load commands", i
);
1467 template <typename A
>
1468 bool Parser
<A
>::getNonLocalSymbols(const uint8_t* fileContent
, std::vector
<const char*> &syms
)
1470 const macho_header
<P
>* header
= (const macho_header
<P
>*)fileContent
;
1471 const uint32_t cmd_count
= header
->ncmds();
1472 const macho_load_command
<P
>* const cmds
= (macho_load_command
<P
>*)((char*)header
+ sizeof(macho_header
<P
>));
1473 const macho_load_command
<P
>* const cmdsEnd
= (macho_load_command
<P
>*)((char*)header
+ sizeof(macho_header
<P
>) + header
->sizeofcmds());
1474 const macho_load_command
<P
>* cmd
= cmds
;
1475 for (uint32_t i
= 0; i
< cmd_count
; ++i
) {
1476 if ( cmd
->cmd() == LC_SYMTAB
) {
1477 const macho_symtab_command
<P
>* symtab
= (macho_symtab_command
<P
>*)cmd
;
1478 uint32_t symbolCount
= symtab
->nsyms();
1479 const macho_nlist
<P
>* symbols
= (const macho_nlist
<P
>*)(fileContent
+ symtab
->symoff());
1480 const char* strings
= (char*)fileContent
+ symtab
->stroff();
1481 for (uint32_t j
= 0; j
< symbolCount
; ++j
) {
1482 // ignore stabs and count only ext symbols
1483 if ( (symbols
[j
].n_type() & N_STAB
) == 0 &&
1484 (symbols
[j
].n_type() & N_EXT
) != 0 ) {
1485 const char* symName
= &strings
[symbols
[j
].n_strx()];
1486 syms
.push_back(symName
);
1491 cmd
= (const macho_load_command
<P
>*)(((char*)cmd
)+cmd
->cmdsize());
1492 if ( cmd
> cmdsEnd
)
1493 throwf("malformed mach-o file, load command #%d is outside size of load commands", i
);
1499 template <typename A
>
1500 int Parser
<A
>::pointerSorter(const void* l
, const void* r
)
1502 // sort references by address
1503 const pint_t
* left
= (pint_t
*)l
;
1504 const pint_t
* right
= (pint_t
*)r
;
1505 return (*left
- *right
);
1508 template <typename A
>
1509 typename
A::P::uint_t Parser
<A
>::LabelAndCFIBreakIterator::peek(Parser
<A
>& parser
, pint_t startAddr
, pint_t endAddr
)
1512 if ( symIndex
< sortedSymbolCount
)
1513 symbolAddr
= parser
.symbolFromIndex(sortedSymbolIndexes
[symIndex
]).n_value();
1515 symbolAddr
= endAddr
;
1517 if ( cfiIndex
< cfiStartsCount
)
1518 cfiAddr
= cfiStartsArray
[cfiIndex
];
1521 if ( (cfiAddr
< symbolAddr
) && (cfiAddr
>= startAddr
) ) {
1522 if ( cfiAddr
< endAddr
)
1528 if ( symbolAddr
< endAddr
)
1536 // Parses up a section into chunks based on labels and CFI information.
1537 // Each call returns the next chunk address and size, and (if the break
1538 // was becuase of a label, the symbol). Returns false when no more chunks.
1540 template <typename A
>
1541 bool Parser
<A
>::LabelAndCFIBreakIterator::next(Parser
<A
>& parser
, const Section
<A
>& sect
, uint32_t sectNum
, pint_t startAddr
, pint_t endAddr
,
1542 pint_t
* addr
, pint_t
* size
, const macho_nlist
<P
>** symbol
)
1544 // may not be a label on start of section, but need atom demarcation there
1547 // advance symIndex until we get to the first label at or past the start of this section
1548 while ( symIndex
< sortedSymbolCount
) {
1549 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(sortedSymbolIndexes
[symIndex
]);
1550 if ( ! sect
.ignoreLabel(parser
.nameFromSymbol(sym
)) ) {
1551 pint_t nextSymbolAddr
= sym
.n_value();
1552 //fprintf(stderr, "sectNum=%d, nextSymbolAddr=0x%08llX, name=%s\n", sectNum, (uint64_t)nextSymbolAddr, parser.nameFromSymbol(sym));
1553 if ( (nextSymbolAddr
> startAddr
) || ((nextSymbolAddr
== startAddr
) && (sym
.n_sect() == sectNum
)) )
1558 if ( symIndex
< sortedSymbolCount
) {
1559 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(sortedSymbolIndexes
[symIndex
]);
1560 pint_t nextSymbolAddr
= sym
.n_value();
1561 // if next symbol found is not in this section
1562 if ( sym
.n_sect() != sectNum
) {
1563 // check for CFI break instead of symbol break
1564 if ( cfiIndex
< cfiStartsCount
) {
1565 pint_t nextCfiAddr
= cfiStartsArray
[cfiIndex
];
1566 if ( nextCfiAddr
< endAddr
) {
1569 *addr
= nextCfiAddr
;
1570 *size
= peek(parser
, startAddr
, endAddr
) - nextCfiAddr
;
1576 *size
= endAddr
- startAddr
;
1578 if ( startAddr
== endAddr
)
1579 return false; // zero size section
1581 return true; // whole section is one atom with no label
1583 // if also CFI break here, eat it
1584 if ( cfiIndex
< cfiStartsCount
) {
1585 if ( cfiStartsArray
[cfiIndex
] == nextSymbolAddr
)
1588 if ( nextSymbolAddr
== startAddr
) {
1589 // label at start of section, return it as chunk
1592 *size
= peek(parser
, startAddr
, endAddr
) - startAddr
;
1596 // return chunk before first symbol
1598 *size
= nextSymbolAddr
- startAddr
;
1602 // no symbols in section, check CFI
1603 if ( cfiIndex
< cfiStartsCount
) {
1604 pint_t nextCfiAddr
= cfiStartsArray
[cfiIndex
];
1605 if ( nextCfiAddr
< endAddr
) {
1608 *addr
= nextCfiAddr
;
1609 *size
= peek(parser
, startAddr
, endAddr
) - nextCfiAddr
;
1614 // no cfi, so whole section is one chunk
1616 *size
= endAddr
- startAddr
;
1618 if ( startAddr
== endAddr
)
1619 return false; // zero size section
1621 return true; // whole section is one atom with no label
1624 while ( (symIndex
< sortedSymbolCount
) && (cfiIndex
< cfiStartsCount
) ) {
1625 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(sortedSymbolIndexes
[symIndex
]);
1626 pint_t nextSymbolAddr
= sym
.n_value();
1627 pint_t nextCfiAddr
= cfiStartsArray
[cfiIndex
];
1628 if ( nextSymbolAddr
< nextCfiAddr
) {
1629 if ( nextSymbolAddr
>= endAddr
)
1632 if ( nextSymbolAddr
< startAddr
)
1634 *addr
= nextSymbolAddr
;
1635 *size
= peek(parser
, startAddr
, endAddr
) - nextSymbolAddr
;
1639 else if ( nextCfiAddr
< nextSymbolAddr
) {
1640 if ( nextCfiAddr
>= endAddr
)
1643 if ( nextCfiAddr
< startAddr
)
1645 *addr
= nextCfiAddr
;
1646 *size
= peek(parser
, startAddr
, endAddr
) - nextCfiAddr
;
1651 if ( nextCfiAddr
>= endAddr
)
1655 if ( nextCfiAddr
< startAddr
)
1657 *addr
= nextCfiAddr
;
1658 *size
= peek(parser
, startAddr
, endAddr
) - nextCfiAddr
;
1663 while ( symIndex
< sortedSymbolCount
) {
1664 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(sortedSymbolIndexes
[symIndex
]);
1665 pint_t nextSymbolAddr
= sym
.n_value();
1666 // if next symbol found is not in this section, then done with iteration
1667 if ( sym
.n_sect() != sectNum
)
1670 if ( nextSymbolAddr
< startAddr
)
1672 *addr
= nextSymbolAddr
;
1673 *size
= peek(parser
, startAddr
, endAddr
) - nextSymbolAddr
;
1677 while ( cfiIndex
< cfiStartsCount
) {
1678 pint_t nextCfiAddr
= cfiStartsArray
[cfiIndex
];
1679 if ( nextCfiAddr
>= endAddr
)
1682 if ( nextCfiAddr
< startAddr
)
1684 *addr
= nextCfiAddr
;
1685 *size
= peek(parser
, startAddr
, endAddr
) - nextCfiAddr
;
1693 typename
arm::P::uint_t Parser
<arm
>::realAddr(typename
arm::P::uint_t addr
)
1698 template <typename A
>
1699 typename
A::P::uint_t Parser
<A
>::realAddr(typename
A::P::uint_t addr
)
1704 #define STACK_ALLOC_IF_SMALL(_type, _name, _actual_count, _maxCount) \
1705 _type* _name = NULL; \
1706 uint32_t _name##_count = 1; \
1707 if ( _actual_count > _maxCount ) \
1708 _name = (_type*)malloc(sizeof(_type) * _actual_count); \
1710 _name##_count = _actual_count; \
1711 _type _name##_buffer[_name##_count]; \
1712 if ( _name == NULL ) \
1713 _name = _name##_buffer;
1716 template <typename A
>
1717 ld::relocatable::File
* Parser
<A
>::parse(const ParserOptions
& opts
)
1719 // create file object
1720 _file
= new File
<A
>(_path
, _modTime
, _fileContent
, _ordinal
);
1723 _file
->_srcKind
= opts
.srcKind
;
1724 // set treatBitcodeAsData
1725 _treateBitcodeAsData
= opts
.treateBitcodeAsData
;
1726 _usingBitcode
= opts
.usingBitcode
;
1728 // respond to -t option
1729 if ( opts
.logAllFiles
)
1730 printf("%s\n", _path
);
1732 _armUsesZeroCostExceptions
= opts
.armUsesZeroCostExceptions
;
1733 _maxDefaultCommonAlignment
= opts
.maxDefaultCommonAlignment
;
1735 // parse start of mach-o file
1736 if ( ! parseLoadCommands(opts
.platform
, opts
.minOSVersion
, opts
.simulator
, opts
.ignoreMismatchPlatform
) )
1740 uint32_t sortedSectionIndexes
[_machOSectionsCount
];
1741 this->makeSortedSectionsArray(sortedSectionIndexes
);
1743 // make symbol table sorted by address
1744 this->prescanSymbolTable();
1745 uint32_t sortedSymbolIndexes
[_symbolsInSections
];
1746 this->makeSortedSymbolsArray(sortedSymbolIndexes
, sortedSectionIndexes
);
1748 // allocate Section<A> object for each mach-o section
1751 // if it exists, do special early parsing of __compact_unwind section
1752 uint32_t countOfCUs
= 0;
1753 if ( _compactUnwindSection
!= NULL
)
1754 countOfCUs
= _compactUnwindSection
->count();
1755 // stack allocate (if not too large) cuInfoBuffer
1756 STACK_ALLOC_IF_SMALL(typename CUSection
<A
>::Info
, cuInfoArray
, countOfCUs
, 1024);
1757 if ( countOfCUs
!= 0 )
1758 _compactUnwindSection
->parse(*this, countOfCUs
, cuInfoArray
);
1760 // create lists of address that already have compact unwind and thus don't need the dwarf parsed
1761 unsigned cuLsdaCount
= 0;
1762 pint_t cuStarts
[countOfCUs
];
1763 for (uint32_t i
=0; i
< countOfCUs
; ++i
) {
1764 if ( CUSection
<A
>::encodingMeansUseDwarf(cuInfoArray
[i
].compactUnwindInfo
) )
1767 cuStarts
[i
] = cuInfoArray
[i
].functionStartAddress
;
1768 if ( cuInfoArray
[i
].lsdaAddress
!= 0 )
1773 // if it exists, do special early parsing of __eh_frame section
1774 // stack allocate (if not too large) array of CFI_Atom_Info
1775 uint32_t countOfCFIs
= 0;
1776 if ( _EHFrameSection
!= NULL
)
1777 countOfCFIs
= _EHFrameSection
->cfiCount(*this);
1778 STACK_ALLOC_IF_SMALL(typename CFISection
<A
>::CFI_Atom_Info
, cfiArray
, countOfCFIs
, 1024);
1780 // stack allocate (if not too large) a copy of __eh_frame to apply relocations to
1781 uint32_t sectSize
= 4;
1782 if ( (countOfCFIs
!= 0) && _EHFrameSection
->needsRelocating() )
1783 sectSize
= _EHFrameSection
->machoSection()->size()+4;
1784 STACK_ALLOC_IF_SMALL(uint8_t, ehBuffer
, sectSize
, 50*1024);
1785 uint32_t cfiStartsCount
= 0;
1786 if ( countOfCFIs
!= 0 ) {
1787 _EHFrameSection
->cfiParse(*this, ehBuffer
, cfiArray
, countOfCFIs
, cuStarts
, countOfCUs
);
1788 // count functions and lsdas
1789 for(uint32_t i
=0; i
< countOfCFIs
; ++i
) {
1790 if ( cfiArray
[i
].isCIE
)
1792 //fprintf(stderr, "cfiArray[i].func = 0x%08llX, cfiArray[i].lsda = 0x%08llX, encoding=0x%08X\n",
1793 // (uint64_t)cfiArray[i].u.fdeInfo.function.targetAddress,
1794 // (uint64_t)cfiArray[i].u.fdeInfo.lsda.targetAddress,
1795 // cfiArray[i].u.fdeInfo.compactUnwindInfo);
1796 if ( cfiArray
[i
].u
.fdeInfo
.function
.targetAddress
!= CFI_INVALID_ADDRESS
)
1798 if ( cfiArray
[i
].u
.fdeInfo
.lsda
.targetAddress
!= CFI_INVALID_ADDRESS
)
1802 CFI_CU_InfoArrays
cfis(cfiArray
, countOfCFIs
, cuInfoArray
, countOfCUs
);
1804 // create sorted array of function starts and lsda starts
1805 pint_t cfiStartsArray
[cfiStartsCount
+cuLsdaCount
];
1806 uint32_t countOfFDEs
= 0;
1807 uint32_t cfiStartsArrayCount
= 0;
1808 if ( countOfCFIs
!= 0 ) {
1809 for(uint32_t i
=0; i
< countOfCFIs
; ++i
) {
1810 if ( cfiArray
[i
].isCIE
)
1812 if ( cfiArray
[i
].u
.fdeInfo
.function
.targetAddress
!= CFI_INVALID_ADDRESS
)
1813 cfiStartsArray
[cfiStartsArrayCount
++] = realAddr(cfiArray
[i
].u
.fdeInfo
.function
.targetAddress
);
1814 if ( cfiArray
[i
].u
.fdeInfo
.lsda
.targetAddress
!= CFI_INVALID_ADDRESS
)
1815 cfiStartsArray
[cfiStartsArrayCount
++] = cfiArray
[i
].u
.fdeInfo
.lsda
.targetAddress
;
1819 if ( cuLsdaCount
!= 0 ) {
1820 // merge in an lsda info from compact unwind
1821 for (uint32_t i
=0; i
< countOfCUs
; ++i
) {
1822 if ( cuInfoArray
[i
].lsdaAddress
== 0 )
1824 // append to cfiStartsArray if not already in that list
1826 for(uint32_t j
=0; j
< cfiStartsArrayCount
; ++j
) {
1827 if ( cfiStartsArray
[j
] == cuInfoArray
[i
].lsdaAddress
)
1831 cfiStartsArray
[cfiStartsArrayCount
++] = cuInfoArray
[i
].lsdaAddress
;
1835 if ( cfiStartsArrayCount
!= 0 ) {
1836 ::qsort(cfiStartsArray
, cfiStartsArrayCount
, sizeof(pint_t
), pointerSorter
);
1838 // scan for FDEs claming the same function
1839 for(uint32_t i
=1; i
< cfiStartsArrayCount
; ++i
) {
1840 assert( cfiStartsArray
[i
] != cfiStartsArray
[i
-1] );
1845 Section
<A
>** sections
= _file
->_sectionsArray
;
1846 uint32_t sectionsCount
= _file
->_sectionsArrayCount
;
1848 // figure out how many atoms will be allocated and allocate
1849 LabelAndCFIBreakIterator
breakIterator(sortedSymbolIndexes
, _symbolsInSections
, cfiStartsArray
,
1850 cfiStartsArrayCount
, _overlappingSymbols
);
1851 uint32_t computedAtomCount
= 0;
1852 for (uint32_t i
=0; i
< sectionsCount
; ++i
) {
1853 breakIterator
.beginSection();
1854 uint32_t count
= sections
[i
]->computeAtomCount(*this, breakIterator
, cfis
);
1855 //const macho_section<P>* sect = sections[i]->machoSection();
1856 //fprintf(stderr, "computed count=%u for section %s size=%llu\n", count, sect->sectname(), (sect != NULL) ? sect->size() : 0);
1857 computedAtomCount
+= count
;
1859 //fprintf(stderr, "allocating %d atoms * sizeof(Atom<A>)=%ld, sizeof(ld::Atom)=%ld\n", computedAtomCount, sizeof(Atom<A>), sizeof(ld::Atom));
1860 _file
->_atomsArray
= new uint8_t[computedAtomCount
*sizeof(Atom
<A
>)];
1861 _file
->_atomsArrayCount
= 0;
1863 // have each section append atoms to _atomsArray
1864 LabelAndCFIBreakIterator
breakIterator2(sortedSymbolIndexes
, _symbolsInSections
, cfiStartsArray
,
1865 cfiStartsArrayCount
, _overlappingSymbols
);
1866 for (uint32_t i
=0; i
< sectionsCount
; ++i
) {
1867 uint8_t* atoms
= _file
->_atomsArray
+ _file
->_atomsArrayCount
*sizeof(Atom
<A
>);
1868 breakIterator2
.beginSection();
1869 uint32_t count
= sections
[i
]->appendAtoms(*this, atoms
, breakIterator2
, cfis
);
1870 //fprintf(stderr, "append count=%u for section %s/%s\n", count, sections[i]->machoSection()->segname(), sections[i]->machoSection()->sectname());
1871 _file
->_atomsArrayCount
+= count
;
1873 assert( _file
->_atomsArrayCount
== computedAtomCount
&& "more atoms allocated than expected");
1876 // have each section add all fix-ups for its atoms
1877 _allFixups
.reserve(computedAtomCount
*5);
1878 for (uint32_t i
=0; i
< sectionsCount
; ++i
)
1879 sections
[i
]->makeFixups(*this, cfis
);
1881 // assign fixups start offset for each atom
1882 uint8_t* p
= _file
->_atomsArray
;
1883 uint32_t fixupOffset
= 0;
1884 for(int i
=_file
->_atomsArrayCount
; i
> 0; --i
) {
1885 Atom
<A
>* atom
= (Atom
<A
>*)p
;
1886 atom
->_fixupsStartIndex
= fixupOffset
;
1887 fixupOffset
+= atom
->_fixupsCount
;
1888 atom
->_fixupsCount
= 0;
1889 p
+= sizeof(Atom
<A
>);
1891 assert(fixupOffset
== _allFixups
.size());
1892 _file
->_fixups
.resize(fixupOffset
);
1894 // copy each fixup for each atom
1895 for(typename
std::vector
<FixupInAtom
>::iterator it
=_allFixups
.begin(); it
!= _allFixups
.end(); ++it
) {
1896 uint32_t slot
= it
->atom
->_fixupsStartIndex
+ it
->atom
->_fixupsCount
;
1897 _file
->_fixups
[slot
] = it
->fixup
;
1898 it
->atom
->_fixupsCount
++;
1901 // done with temp vector
1905 _file
->_unwindInfos
.reserve(countOfFDEs
+countOfCUs
);
1906 for(uint32_t i
=0; i
< countOfCFIs
; ++i
) {
1907 if ( cfiArray
[i
].isCIE
)
1909 if ( cfiArray
[i
].u
.fdeInfo
.function
.targetAddress
!= CFI_INVALID_ADDRESS
) {
1910 ld::Atom::UnwindInfo info
;
1911 info
.startOffset
= 0;
1912 info
.unwindInfo
= cfiArray
[i
].u
.fdeInfo
.compactUnwindInfo
;
1913 _file
->_unwindInfos
.push_back(info
);
1914 Atom
<A
>* func
= findAtomByAddress(cfiArray
[i
].u
.fdeInfo
.function
.targetAddress
);
1915 func
->setUnwindInfoRange(_file
->_unwindInfos
.size()-1, 1);
1916 //fprintf(stderr, "cu from dwarf =0x%08X, atom=%s\n", info.unwindInfo, func->name());
1919 // apply compact infos in __LD,__compact_unwind section to each function
1920 // if function also has dwarf unwind, CU will override it
1921 Atom
<A
>* lastFunc
= NULL
;
1922 uint32_t lastEnd
= 0;
1923 for(uint32_t i
=0; i
< countOfCUs
; ++i
) {
1924 typename CUSection
<A
>::Info
* info
= &cuInfoArray
[i
];
1925 assert(info
->function
!= NULL
);
1926 ld::Atom::UnwindInfo ui
;
1927 ui
.startOffset
= info
->functionStartAddress
- info
->function
->objectAddress();
1928 ui
.unwindInfo
= info
->compactUnwindInfo
;
1929 _file
->_unwindInfos
.push_back(ui
);
1930 // don't override with converted cu with "use dwarf" cu, if forcing dwarf conversion
1931 if ( !_forceDwarfConversion
|| !CUSection
<A
>::encodingMeansUseDwarf(info
->compactUnwindInfo
) ) {
1932 //fprintf(stderr, "cu=0x%08X, atom=%s\n", ui.unwindInfo, info->function->name());
1933 // if previous is for same function, extend range
1934 if ( info
->function
== lastFunc
) {
1935 if ( lastEnd
!= ui
.startOffset
) {
1936 if ( lastEnd
< ui
.startOffset
)
1937 warning("__LD,__compact_unwind entries for %s have a gap at offset 0x%0X", info
->function
->name(), lastEnd
);
1939 warning("__LD,__compact_unwind entries for %s overlap at offset 0x%0X", info
->function
->name(), lastEnd
);
1941 lastFunc
->extendUnwindInfoRange();
1944 info
->function
->setUnwindInfoRange(_file
->_unwindInfos
.size()-1, 1);
1945 lastFunc
= info
->function
;
1946 lastEnd
= ui
.startOffset
+ info
->rangeLength
;
1950 // process indirect symbols which become AliasAtoms
1951 _file
->_aliasAtomsArray
= NULL
;
1952 _file
->_aliasAtomsArrayCount
= 0;
1953 if ( _indirectSymbolCount
!= 0 ) {
1954 _file
->_aliasAtomsArrayCount
= _indirectSymbolCount
;
1955 _file
->_aliasAtomsArray
= new uint8_t[_file
->_aliasAtomsArrayCount
*sizeof(AliasAtom
)];
1956 this->appendAliasAtoms(_file
->_aliasAtomsArray
);
1960 // parse dwarf debug info to get line info
1961 this->parseDebugInfo();
1966 static void versionToString(uint32_t value
, char buffer
[32])
1969 sprintf(buffer
, "%d.%d.%d", value
>> 16, (value
>> 8) & 0xFF, value
& 0xFF);
1971 sprintf(buffer
, "%d.%d", value
>> 16, (value
>> 8) & 0xFF);
1974 template <> uint8_t Parser
<x86
>::loadCommandSizeMask() { return 0x03; }
1975 template <> uint8_t Parser
<x86_64
>::loadCommandSizeMask() { return 0x07; }
1976 template <> uint8_t Parser
<arm
>::loadCommandSizeMask() { return 0x03; }
1977 template <> uint8_t Parser
<arm64
>::loadCommandSizeMask() { return 0x07; }
1979 template <typename A
>
1980 bool Parser
<A
>::parseLoadCommands(Options::Platform platform
, uint32_t linkMinOSVersion
, bool simulator
, bool ignoreMismatchPlatform
)
1982 const macho_header
<P
>* header
= (const macho_header
<P
>*)_fileContent
;
1984 // set File attributes
1985 _file
->_canScatterAtoms
= (header
->flags() & MH_SUBSECTIONS_VIA_SYMBOLS
);
1986 _file
->_cpuSubType
= header
->cpusubtype();
1988 const macho_segment_command
<P
>* segment
= NULL
;
1989 const uint8_t* const endOfFile
= _fileContent
+ _fileLength
;
1990 const uint32_t cmd_count
= header
->ncmds();
1991 // <rdar://problem/5394172> an empty .o file with zero load commands will crash linker
1992 if ( cmd_count
== 0 )
1994 Options::Platform lcPlatform
= Options::kPlatformUnknown
;
1995 const macho_load_command
<P
>* const cmds
= (macho_load_command
<P
>*)((char*)header
+ sizeof(macho_header
<P
>));
1996 const macho_load_command
<P
>* const cmdsEnd
= (macho_load_command
<P
>*)((char*)header
+ sizeof(macho_header
<P
>) + header
->sizeofcmds());
1997 const macho_load_command
<P
>* cmd
= cmds
;
1998 for (uint32_t i
= 0; i
< cmd_count
; ++i
) {
1999 uint32_t size
= cmd
->cmdsize();
2000 if ( (size
& this->loadCommandSizeMask()) != 0 )
2001 throwf("load command #%d has a unaligned size", i
);
2002 const uint8_t* endOfCmd
= ((uint8_t*)cmd
)+cmd
->cmdsize();
2003 if ( endOfCmd
> (uint8_t*)cmdsEnd
)
2004 throwf("load command #%d extends beyond the end of the load commands", i
);
2005 if ( endOfCmd
> endOfFile
)
2006 throwf("load command #%d extends beyond the end of the file", i
);
2007 switch (cmd
->cmd()) {
2010 const macho_symtab_command
<P
>* symtab
= (macho_symtab_command
<P
>*)cmd
;
2011 _symbolCount
= symtab
->nsyms();
2012 _symbols
= (const macho_nlist
<P
>*)(_fileContent
+ symtab
->symoff());
2013 _strings
= (char*)_fileContent
+ symtab
->stroff();
2014 _stringsSize
= symtab
->strsize();
2015 if ( (symtab
->symoff() + _symbolCount
*sizeof(macho_nlist
<P
>)) > _fileLength
)
2016 throw "mach-o symbol table extends beyond end of file";
2017 if ( (_strings
+ _stringsSize
) > (char*)endOfFile
)
2018 throw "mach-o string pool extends beyond end of file";
2019 if ( _indirectTable
== NULL
) {
2020 if ( _undefinedEndIndex
== 0 ) {
2021 _undefinedStartIndex
= 0;
2022 _undefinedEndIndex
= symtab
->nsyms();
2029 const macho_dysymtab_command
<P
>* dsymtab
= (macho_dysymtab_command
<P
>*)cmd
;
2030 _indirectTable
= (uint32_t*)(_fileContent
+ dsymtab
->indirectsymoff());
2031 _indirectTableCount
= dsymtab
->nindirectsyms();
2032 if ( &_indirectTable
[_indirectTableCount
] > (uint32_t*)endOfFile
)
2033 throw "indirect symbol table extends beyond end of file";
2034 _undefinedStartIndex
= dsymtab
->iundefsym();
2035 _undefinedEndIndex
= _undefinedStartIndex
+ dsymtab
->nundefsym();
2041 case LC_DATA_IN_CODE
:
2043 const macho_linkedit_data_command
<P
>* dc
= (macho_linkedit_data_command
<P
>*)cmd
;
2044 _dataInCodeStart
= (macho_data_in_code_entry
<P
>*)(_fileContent
+ dc
->dataoff());
2045 _dataInCodeEnd
= (macho_data_in_code_entry
<P
>*)(_fileContent
+ dc
->dataoff() + dc
->datasize());
2046 if ( _dataInCodeEnd
> (macho_data_in_code_entry
<P
>*)endOfFile
)
2047 throw "LC_DATA_IN_CODE table extends beyond end of file";
2050 case LC_LINKER_OPTION
:
2052 const macho_linker_option_command
<P
>* loc
= (macho_linker_option_command
<P
>*)cmd
;
2053 const char* buffer
= loc
->buffer();
2054 _file
->_linkerOptions
.resize(_file
->_linkerOptions
.size() + 1);
2055 std::vector
<const char*>& vec
= _file
->_linkerOptions
.back();
2056 for (uint32_t j
=0; j
< loc
->count(); ++j
) {
2057 vec
.push_back(buffer
);
2058 buffer
+= strlen(buffer
) + 1;
2060 if ( buffer
> ((char*)cmd
+ loc
->cmdsize()) )
2061 throw "malformed LC_LINKER_OPTION";
2064 case LC_LINKER_OPTIMIZATION_HINTS
:
2066 const macho_linkedit_data_command
<P
>* loh
= (macho_linkedit_data_command
<P
>*)cmd
;
2067 _lohStart
= _fileContent
+ loh
->dataoff();
2068 _lohEnd
= _fileContent
+ loh
->dataoff() + loh
->datasize();
2069 if ( _lohEnd
> endOfFile
)
2070 throw "LC_LINKER_OPTIMIZATION_HINTS table extends beyond end of file";
2073 case LC_VERSION_MIN_MACOSX
:
2074 case LC_VERSION_MIN_IPHONEOS
:
2075 case LC_VERSION_MIN_WATCHOS
:
2076 #if SUPPORT_APPLE_TV
2077 case LC_VERSION_MIN_TVOS
:
2079 if ( ignoreMismatchPlatform
)
2081 _file
->_platform
= cmd
->cmd();
2082 lcPlatform
= Options::platformForLoadCommand(cmd
->cmd());
2083 _file
->_minOSVersion
= ((macho_version_min_command
<P
>*)cmd
)->version();
2085 case macho_segment_command
<P
>::CMD
:
2086 if ( segment
!= NULL
)
2087 throw "more than one LC_SEGMENT found in object file";
2088 segment
= (macho_segment_command
<P
>*)cmd
;
2091 // ignore unknown load commands
2094 cmd
= (const macho_load_command
<P
>*)(((char*)cmd
)+cmd
->cmdsize());
2095 if ( cmd
> cmdsEnd
)
2096 throwf("malformed mach-o file, load command #%d is outside size of load commands", i
);
2098 // arm/arm64 objects are default to ios platform if not set.
2099 // rdar://problem/21746314
2100 if (lcPlatform
== Options::kPlatformUnknown
&&
2101 (std::is_same
<A
, arm
>::value
|| std::is_same
<A
, arm64
>::value
))
2102 lcPlatform
= Options::kPlatformiOS
;
2104 // Check platform cross-linking.
2105 if ( !ignoreMismatchPlatform
) {
2106 if ( lcPlatform
!= platform
) {
2108 case Options::kPlatformOSX
:
2109 case Options::kPlatformiOS
:
2110 if ( lcPlatform
== Options::kPlatformUnknown
)
2112 // fall through if the Platform is not Unknown
2113 case Options::kPlatformWatchOS
:
2114 // Error when using bitcocde, warning otherwise.
2116 throwf("building for %s%s, but linking in object file built for %s,",
2117 Options::platformName(platform
), (simulator
? " simulator" : ""),
2118 Options::platformName(lcPlatform
));
2120 warning("URGENT: building for %s%s, but linking in object file (%s) built for %s. "
2121 "Note: This will be an error in the future.",
2122 Options::platformName(platform
), (simulator
? " simulator" : ""), path(),
2123 Options::platformName(lcPlatform
));
2125 #if SUPPORT_APPLE_TV
2126 case Options::kPlatform_tvOS
:
2127 // Error when using bitcocde, warning otherwise.
2129 throwf("building for %s%s, but linking in object file built for %s,",
2130 Options::platformName(platform
), (simulator
? " simulator" : ""),
2131 Options::platformName(lcPlatform
));
2133 warning("URGENT: building for %s%s, but linking in object file (%s) built for %s. "
2134 "Note: This will be an error in the future.",
2135 Options::platformName(platform
), (simulator
? " simulator" : ""), path(),
2136 Options::platformName(lcPlatform
));
2139 case Options::kPlatformUnknown
:
2140 // skip if the target platform is unknown
2144 if ( linkMinOSVersion
&& (_file
->_minOSVersion
> linkMinOSVersion
) ) {
2147 versionToString(_file
->_minOSVersion
, t1
);
2148 versionToString(linkMinOSVersion
, t2
);
2149 warning("object file (%s) was built for newer %s version (%s) than being linked (%s)",
2150 _path
, Options::platformName(lcPlatform
), t1
, t2
);
2155 // record range of sections
2156 if ( segment
== NULL
)
2157 throw "missing LC_SEGMENT";
2158 _sectionsStart
= (macho_section
<P
>*)((char*)segment
+ sizeof(macho_segment_command
<P
>));
2159 _machOSectionsCount
= segment
->nsects();
2160 if ( (sizeof(macho_segment_command
<P
>) + _machOSectionsCount
* sizeof(macho_section
<P
>)) > segment
->cmdsize() )
2161 throw "too many sections for size of LC_SEGMENT command";
2165 template <typename A
>
2166 Options::Platform Parser
<A
>::findPlatform(const macho_header
<P
>* header
)
2168 const uint32_t cmd_count
= header
->ncmds();
2169 if ( cmd_count
== 0 )
2170 return Options::kPlatformUnknown
;
2171 const macho_load_command
<P
>* const cmds
= (macho_load_command
<P
>*)((char*)header
+ sizeof(macho_header
<P
>));
2172 const macho_load_command
<P
>* const cmdsEnd
= (macho_load_command
<P
>*)((char*)header
+ sizeof(macho_header
<P
>) + header
->sizeofcmds());
2173 const macho_load_command
<P
>* cmd
= cmds
;
2174 for (uint32_t i
= 0; i
< cmd_count
; ++i
) {
2175 uint32_t size
= cmd
->cmdsize();
2176 if ( (size
& loadCommandSizeMask()) != 0 )
2177 throwf("load command #%d has a unaligned size", i
);
2178 const uint8_t* endOfCmd
= ((uint8_t*)cmd
)+cmd
->cmdsize();
2179 if ( endOfCmd
> (uint8_t*)cmdsEnd
)
2180 throwf("load command #%d extends beyond the end of the load commands", i
);
2181 switch (cmd
->cmd()) {
2182 case LC_VERSION_MIN_MACOSX
:
2183 return Options::kPlatformOSX
;
2184 case LC_VERSION_MIN_IPHONEOS
:
2185 return Options::kPlatformiOS
;
2187 cmd
= (const macho_load_command
<P
>*)(((char*)cmd
)+cmd
->cmdsize());
2188 if ( cmd
> cmdsEnd
)
2189 throwf("malformed mach-o file, load command #%d is outside size of load commands", i
);
2191 return Options::kPlatformUnknown
;
2195 template <typename A
>
2196 void Parser
<A
>::prescanSymbolTable()
2198 _tentativeDefinitionCount
= 0;
2199 _absoluteSymbolCount
= 0;
2200 _symbolsInSections
= 0;
2201 _hasDataInCodeLabels
= false;
2202 for (uint32_t i
=0; i
< this->_symbolCount
; ++i
) {
2203 const macho_nlist
<P
>& sym
= symbolFromIndex(i
);
2205 if ( (sym
.n_type() & N_STAB
) != 0 )
2208 // look at undefines
2209 const char* symbolName
= this->nameFromSymbol(sym
);
2210 if ( (sym
.n_type() & N_TYPE
) == N_UNDF
) {
2211 if ( sym
.n_value() != 0 ) {
2212 // count tentative definitions
2213 ++_tentativeDefinitionCount
;
2215 else if ( strncmp(symbolName
, "___dtrace_", 10) == 0 ) {
2216 // any undefined starting with __dtrace_*$ that is not ___dtrace_probe$* or ___dtrace_isenabled$*
2217 // is extra provider info
2218 if ( (strncmp(&symbolName
[10], "probe$", 6) != 0) && (strncmp(&symbolName
[10], "isenabled$", 10) != 0) ) {
2219 _dtraceProviderInfo
.push_back(symbolName
);
2224 else if ( ((sym
.n_type() & N_TYPE
) == N_INDR
) && ((sym
.n_type() & N_EXT
) != 0) ) {
2225 _indirectSymbolCount
++;
2229 // count absolute symbols
2230 if ( (sym
.n_type() & N_TYPE
) == N_ABS
) {
2231 const char* absName
= this->nameFromSymbol(sym
);
2232 // ignore .objc_class_name_* symbols
2233 if ( strncmp(absName
, ".objc_class_name_", 17) == 0 ) {
2237 // ignore .objc_class_name_* symbols
2238 if ( strncmp(absName
, ".objc_category_name_", 20) == 0 )
2240 // ignore empty *.eh symbols
2241 if ( strcmp(&absName
[strlen(absName
)-3], ".eh") == 0 )
2243 ++_absoluteSymbolCount
;
2246 // only look at definitions
2247 if ( (sym
.n_type() & N_TYPE
) != N_SECT
)
2250 // 'L' labels do not denote atom breaks
2251 if ( symbolName
[0] == 'L' ) {
2252 // <rdar://problem/9218847> Formalize data in code with L$start$ labels
2253 if ( strncmp(symbolName
, "L$start$", 8) == 0 )
2254 _hasDataInCodeLabels
= true;
2257 // how many def syms in each section
2258 if ( sym
.n_sect() > _machOSectionsCount
)
2259 throw "bad n_sect in symbol table";
2261 _symbolsInSections
++;
2265 template <typename A
>
2266 void Parser
<A
>::appendAliasAtoms(uint8_t* p
)
2268 for (uint32_t i
=0; i
< this->_symbolCount
; ++i
) {
2269 const macho_nlist
<P
>& sym
= symbolFromIndex(i
);
2271 if ( (sym
.n_type() & N_STAB
) != 0 )
2274 // only look at N_INDR symbols
2275 if ( (sym
.n_type() & N_TYPE
) != N_INDR
)
2278 // skip non-external aliases
2279 if ( (sym
.n_type() & N_EXT
) == 0 )
2282 const char* symbolName
= this->nameFromSymbol(sym
);
2283 const char* aliasOfName
= &_strings
[sym
.n_value()];
2284 bool isHiddenVisibility
= (sym
.n_type() & N_PEXT
);
2285 AliasAtom
* allocatedSpace
= (AliasAtom
*)p
;
2286 new (allocatedSpace
) AliasAtom(symbolName
, isHiddenVisibility
, _file
, aliasOfName
);
2287 p
+= sizeof(AliasAtom
);
2293 template <typename A
>
2294 int Parser
<A
>::sectionIndexSorter(void* extra
, const void* l
, const void* r
)
2296 Parser
<A
>* parser
= (Parser
<A
>*)extra
;
2297 const uint32_t* left
= (uint32_t*)l
;
2298 const uint32_t* right
= (uint32_t*)r
;
2299 const macho_section
<P
>* leftSect
= parser
->machOSectionFromSectionIndex(*left
);
2300 const macho_section
<P
>* rightSect
= parser
->machOSectionFromSectionIndex(*right
);
2302 // can't just return difference because 64-bit diff does not fit in 32-bit return type
2303 int64_t result
= leftSect
->addr() - rightSect
->addr();
2304 if ( result
== 0 ) {
2305 // two sections with same start address
2306 // one with zero size goes first
2307 bool leftEmpty
= ( leftSect
->size() == 0 );
2308 bool rightEmpty
= ( rightSect
->size() == 0 );
2309 if ( leftEmpty
!= rightEmpty
) {
2310 return ( rightEmpty
? 1 : -1 );
2312 if ( !leftEmpty
&& !rightEmpty
)
2313 throwf("overlapping sections");
2314 // both empty, so chose file order
2315 return ( rightSect
- leftSect
);
2317 else if ( result
< 0 )
2323 template <typename A
>
2324 void Parser
<A
>::makeSortedSectionsArray(uint32_t array
[])
2326 const bool log
= false;
2329 fprintf(stderr
, "unsorted sections:\n");
2330 for(unsigned int i
=0; i
< _machOSectionsCount
; ++i
)
2331 fprintf(stderr
, "0x%08llX %s %s\n", _sectionsStart
[i
].addr(), _sectionsStart
[i
].segname(), _sectionsStart
[i
].sectname());
2334 // sort by symbol table address
2335 for (uint32_t i
=0; i
< _machOSectionsCount
; ++i
)
2337 ::qsort_r(array
, _machOSectionsCount
, sizeof(uint32_t), this, §ionIndexSorter
);
2340 fprintf(stderr
, "sorted sections:\n");
2341 for(unsigned int i
=0; i
< _machOSectionsCount
; ++i
)
2342 fprintf(stderr
, "0x%08llX %s %s\n", _sectionsStart
[array
[i
]].addr(), _sectionsStart
[array
[i
]].segname(), _sectionsStart
[array
[i
]].sectname());
2348 template <typename A
>
2349 int Parser
<A
>::symbolIndexSorter(void* extra
, const void* l
, const void* r
)
2351 ParserAndSectionsArray
* extraInfo
= (ParserAndSectionsArray
*)extra
;
2352 Parser
<A
>* parser
= extraInfo
->parser
;
2353 const uint32_t* sortedSectionsArray
= extraInfo
->sortedSectionsArray
;
2354 const uint32_t* left
= (uint32_t*)l
;
2355 const uint32_t* right
= (uint32_t*)r
;
2356 const macho_nlist
<P
>& leftSym
= parser
->symbolFromIndex(*left
);
2357 const macho_nlist
<P
>& rightSym
= parser
->symbolFromIndex(*right
);
2358 // can't just return difference because 64-bit diff does not fit in 32-bit return type
2359 int64_t result
= leftSym
.n_value() - rightSym
.n_value();
2360 if ( result
== 0 ) {
2361 // two symbols with same address
2362 // if in different sections, sort earlier section first
2363 if ( leftSym
.n_sect() != rightSym
.n_sect() ) {
2364 for (uint32_t i
=0; i
< parser
->machOSectionCount(); ++i
) {
2365 if ( sortedSectionsArray
[i
]+1 == leftSym
.n_sect() )
2367 if ( sortedSectionsArray
[i
]+1 == rightSym
.n_sect() )
2371 // two symbols in same section, means one is an alias
2372 // if one is ltmp*, make it an alias (sort first)
2373 const char* leftName
= parser
->nameFromSymbol(leftSym
);
2374 const char* rightName
= parser
->nameFromSymbol(rightSym
);
2375 bool leftIsTmp
= strncmp(leftName
, "ltmp", 4);
2376 bool rightIsTmp
= strncmp(rightName
, "ltmp", 4);
2377 if ( leftIsTmp
!= rightIsTmp
) {
2378 return (rightIsTmp
? -1 : 1);
2381 // if only one is global, make the other an alias (sort first)
2382 if ( (leftSym
.n_type() & N_EXT
) != (rightSym
.n_type() & N_EXT
) ) {
2383 if ( (rightSym
.n_type() & N_EXT
) != 0 )
2388 // if both are global, sort alphabetically. earlier one will be the alias
2389 return ( strcmp(rightName
, leftName
) );
2391 else if ( result
< 0 )
2398 template <typename A
>
2399 void Parser
<A
>::makeSortedSymbolsArray(uint32_t array
[], const uint32_t sectionArray
[])
2401 const bool log
= false;
2403 uint32_t* p
= array
;
2404 for (uint32_t i
=0; i
< this->_symbolCount
; ++i
) {
2405 const macho_nlist
<P
>& sym
= symbolFromIndex(i
);
2407 if ( (sym
.n_type() & N_STAB
) != 0 )
2410 // only look at definitions
2411 if ( (sym
.n_type() & N_TYPE
) != N_SECT
)
2414 // 'L' labels do not denote atom breaks
2415 const char* symbolName
= this->nameFromSymbol(sym
);
2416 if ( symbolName
[0] == 'L' )
2419 // how many def syms in each section
2420 if ( sym
.n_sect() > _machOSectionsCount
)
2421 throw "bad n_sect in symbol table";
2426 assert(p
== &array
[_symbolsInSections
] && "second pass over symbol table yield a different number of symbols");
2428 // sort by symbol table address
2429 ParserAndSectionsArray extra
= { this, sectionArray
};
2430 ::qsort_r(array
, _symbolsInSections
, sizeof(uint32_t), &extra
, &symbolIndexSorter
);
2433 // look for two symbols at same address
2434 _overlappingSymbols
= false;
2435 for (unsigned int i
=1; i
< _symbolsInSections
; ++i
) {
2436 if ( symbolFromIndex(array
[i
-1]).n_value() == symbolFromIndex(array
[i
]).n_value() ) {
2437 //fprintf(stderr, "overlapping symbols at 0x%08llX\n", symbolFromIndex(array[i-1]).n_value());
2438 _overlappingSymbols
= true;
2444 fprintf(stderr
, "sorted symbols:\n");
2445 for(unsigned int i
=0; i
< _symbolsInSections
; ++i
)
2446 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
])) );
2450 template <typename A
>
2451 void Parser
<A
>::makeSections()
2453 // classify each section by type
2454 // compute how many Section objects will be needed and total size for all
2455 unsigned int totalSectionsSize
= 0;
2456 uint8_t machOSectsStorage
[sizeof(MachOSectionAndSectionClass
<P
>)*(_machOSectionsCount
+2)]; // also room for tentative-defs and absolute symbols
2457 // allocate raw storage for all section objects on stack
2458 MachOSectionAndSectionClass
<P
>* machOSects
= (MachOSectionAndSectionClass
<P
>*)machOSectsStorage
;
2459 unsigned int count
= 0;
2460 // local variable for bitcode parsing
2461 const macho_section
<P
>* bitcodeSect
= NULL
;
2462 const macho_section
<P
>* cmdlineSect
= NULL
;
2463 const macho_section
<P
>* swiftCmdlineSect
= NULL
;
2464 const macho_section
<P
>* bundleSect
= NULL
;
2465 bool bitcodeAsm
= false;
2467 for (uint32_t i
=0; i
< _machOSectionsCount
; ++i
) {
2468 const macho_section
<P
>* sect
= &_sectionsStart
[i
];
2469 uint8_t sectionType
= (sect
->flags() & SECTION_TYPE
);
2470 if ( (sect
->offset() + sect
->size() > _fileLength
) && (sectionType
!= S_ZEROFILL
) && (sectionType
!= S_THREAD_LOCAL_ZEROFILL
) )
2471 throwf("section %s/%s extends beyond end of file,", sect
->segname(), sect
->sectname());
2473 if ( (sect
->flags() & S_ATTR_DEBUG
) != 0 ) {
2474 if ( strcmp(sect
->segname(), "__DWARF") == 0 ) {
2475 // note that .o file has dwarf
2476 _file
->_debugInfoKind
= ld::relocatable::File::kDebugInfoDwarf
;
2477 // save off iteresting dwarf sections
2478 if ( strcmp(sect
->sectname(), "__debug_info") == 0 )
2479 _file
->_dwarfDebugInfoSect
= sect
;
2480 else if ( strcmp(sect
->sectname(), "__debug_abbrev") == 0 )
2481 _file
->_dwarfDebugAbbrevSect
= sect
;
2482 else if ( strcmp(sect
->sectname(), "__debug_line") == 0 )
2483 _file
->_dwarfDebugLineSect
= sect
;
2484 else if ( strcmp(sect
->sectname(), "__debug_str") == 0 )
2485 _file
->_dwarfDebugStringSect
= sect
;
2486 // linker does not propagate dwarf sections to output file
2489 else if ( strcmp(sect
->segname(), "__LD") == 0 ) {
2490 if ( strncmp(sect
->sectname(), "__compact_unwind", 16) == 0 ) {
2491 machOSects
[count
].sect
= sect
;
2492 totalSectionsSize
+= sizeof(CUSection
<A
>);
2493 machOSects
[count
++].type
= sectionTypeCompactUnwind
;
2498 if ( strcmp(sect
->segname(), "__LLVM") == 0 ) {
2499 // Process bitcode segement
2500 if ( strncmp(sect
->sectname(), "__bitcode", 9) == 0 ) {
2502 } else if ( strncmp(sect
->sectname(), "__cmdline", 9) == 0 ) {
2504 } else if ( strncmp(sect
->sectname(), "__swift_cmdline", 15) == 0 ) {
2505 swiftCmdlineSect
= sect
;
2506 } else if ( strncmp(sect
->sectname(), "__bundle", 8) == 0 ) {
2508 } else if ( strncmp(sect
->sectname(), "__asm", 5) == 0 ) {
2511 // If treat the bitcode as data, continue to parse as a normal section.
2512 if ( !_treateBitcodeAsData
)
2515 // ignore empty __OBJC sections
2516 if ( (sect
->size() == 0) && (strcmp(sect
->segname(), "__OBJC") == 0) )
2518 // objc image info section is really attributes and not content
2519 if ( ((strcmp(sect
->sectname(), "__image_info") == 0) && (strcmp(sect
->segname(), "__OBJC") == 0))
2520 || ((strncmp(sect
->sectname(), "__objc_imageinfo", 16) == 0) && (strcmp(sect
->segname(), "__DATA") == 0)) ) {
2521 // struct objc_image_info {
2522 // uint32_t version; // initially 0
2525 // #define OBJC_IMAGE_SUPPORTS_GC 2
2526 // #define OBJC_IMAGE_GC_ONLY 4
2527 // #define OBJC_IMAGE_IS_SIMULATED 32
2529 const uint32_t* contents
= (uint32_t*)(_file
->fileContent()+sect
->offset());
2530 if ( (sect
->size() >= 8) && (contents
[0] == 0) ) {
2531 uint32_t flags
= E::get32(contents
[1]);
2532 if ( (flags
& 4) == 4 )
2533 _file
->_objConstraint
= ld::File::objcConstraintGC
;
2534 else if ( (flags
& 2) == 2 )
2535 _file
->_objConstraint
= ld::File::objcConstraintRetainReleaseOrGC
;
2536 else if ( (flags
& 32) == 32 )
2537 _file
->_objConstraint
= ld::File::objcConstraintRetainReleaseForSimulator
;
2539 _file
->_objConstraint
= ld::File::objcConstraintRetainRelease
;
2540 _file
->_swiftVersion
= ((flags
>> 8) & 0xFF);
2541 if ( sect
->size() > 8 ) {
2542 warning("section %s/%s has unexpectedly large size %llu in %s",
2543 sect
->segname(), Section
<A
>::makeSectionName(sect
), sect
->size(), _file
->path());
2547 warning("can't parse %s/%s section in %s", sect
->segname(), Section
<A
>::makeSectionName(sect
), _file
->path());
2551 machOSects
[count
].sect
= sect
;
2552 switch ( sect
->flags() & SECTION_TYPE
) {
2553 case S_SYMBOL_STUBS
:
2554 if ( _stubsSectionNum
== 0 ) {
2555 _stubsSectionNum
= i
+1;
2556 _stubsMachOSection
= sect
;
2559 assert(1 && "multiple S_SYMBOL_STUBS sections");
2560 case S_LAZY_SYMBOL_POINTERS
:
2562 case S_4BYTE_LITERALS
:
2563 totalSectionsSize
+= sizeof(Literal4Section
<A
>);
2564 machOSects
[count
++].type
= sectionTypeLiteral4
;
2566 case S_8BYTE_LITERALS
:
2567 totalSectionsSize
+= sizeof(Literal8Section
<A
>);
2568 machOSects
[count
++].type
= sectionTypeLiteral8
;
2570 case S_16BYTE_LITERALS
:
2571 totalSectionsSize
+= sizeof(Literal16Section
<A
>);
2572 machOSects
[count
++].type
= sectionTypeLiteral16
;
2574 case S_NON_LAZY_SYMBOL_POINTERS
:
2575 totalSectionsSize
+= sizeof(NonLazyPointerSection
<A
>);
2576 machOSects
[count
++].type
= sectionTypeNonLazy
;
2578 case S_THREAD_LOCAL_VARIABLE_POINTERS
:
2579 totalSectionsSize
+= sizeof(TLVPointerSection
<A
>);
2580 machOSects
[count
++].type
= sectionTypeTLVPointers
;
2582 case S_LITERAL_POINTERS
:
2583 if ( (strcmp(sect
->segname(), "__OBJC") == 0) && (strcmp(sect
->sectname(), "__cls_refs") == 0) ) {
2584 totalSectionsSize
+= sizeof(Objc1ClassReferences
<A
>);
2585 machOSects
[count
++].type
= sectionTypeObjC1ClassRefs
;
2588 totalSectionsSize
+= sizeof(PointerToCStringSection
<A
>);
2589 machOSects
[count
++].type
= sectionTypeCStringPointer
;
2592 case S_CSTRING_LITERALS
:
2593 totalSectionsSize
+= sizeof(CStringSection
<A
>);
2594 machOSects
[count
++].type
= sectionTypeCString
;
2596 case S_MOD_INIT_FUNC_POINTERS
:
2597 case S_MOD_TERM_FUNC_POINTERS
:
2598 case S_THREAD_LOCAL_INIT_FUNCTION_POINTERS
:
2603 case S_THREAD_LOCAL_REGULAR
:
2604 case S_THREAD_LOCAL_ZEROFILL
:
2605 if ( (strcmp(sect
->segname(), "__TEXT") == 0) && (strcmp(sect
->sectname(), "__eh_frame") == 0) ) {
2606 totalSectionsSize
+= sizeof(CFISection
<A
>);
2607 machOSects
[count
++].type
= sectionTypeCFI
;
2609 else if ( (strcmp(sect
->segname(), "__DATA") == 0) && (strcmp(sect
->sectname(), "__cfstring") == 0) ) {
2610 totalSectionsSize
+= sizeof(CFStringSection
<A
>);
2611 machOSects
[count
++].type
= sectionTypeCFString
;
2613 else if ( (strcmp(sect
->segname(), "__TEXT") == 0) && (strcmp(sect
->sectname(), "__ustring") == 0) ) {
2614 totalSectionsSize
+= sizeof(UTF16StringSection
<A
>);
2615 machOSects
[count
++].type
= sectionTypeUTF16Strings
;
2617 else if ( (strcmp(sect
->segname(), "__DATA") == 0) && (strncmp(sect
->sectname(), "__objc_classrefs", 16) == 0) ) {
2618 totalSectionsSize
+= sizeof(ObjC2ClassRefsSection
<A
>);
2619 machOSects
[count
++].type
= sectionTypeObjC2ClassRefs
;
2621 else if ( (strcmp(sect
->segname(), "__DATA") == 0) && (strcmp(sect
->sectname(), "__objc_catlist") == 0) ) {
2622 totalSectionsSize
+= sizeof(ObjC2CategoryListSection
<A
>);
2623 machOSects
[count
++].type
= typeObjC2CategoryList
;
2625 else if ( _AppleObjc
&& (strcmp(sect
->segname(), "__OBJC") == 0) && (strcmp(sect
->sectname(), "__class") == 0) ) {
2626 totalSectionsSize
+= sizeof(ObjC1ClassSection
<A
>);
2627 machOSects
[count
++].type
= sectionTypeObjC1Classes
;
2630 totalSectionsSize
+= sizeof(SymboledSection
<A
>);
2631 machOSects
[count
++].type
= sectionTypeSymboled
;
2634 case S_THREAD_LOCAL_VARIABLES
:
2635 totalSectionsSize
+= sizeof(TLVDefsSection
<A
>);
2636 machOSects
[count
++].type
= sectionTypeTLVDefs
;
2639 throwf("unknown section type %d", sect
->flags() & SECTION_TYPE
);
2644 if ( bitcodeSect
!= NULL
) {
2645 if ( cmdlineSect
!= NULL
)
2646 _file
->_bitcode
= std::unique_ptr
<ld::Bitcode
>(new ld::ClangBitcode(&_fileContent
[bitcodeSect
->offset()], bitcodeSect
->size(),
2647 &_fileContent
[cmdlineSect
->offset()], cmdlineSect
->size()));
2648 else if ( swiftCmdlineSect
!= NULL
)
2649 _file
->_bitcode
= std::unique_ptr
<ld::Bitcode
>(new ld::SwiftBitcode(&_fileContent
[bitcodeSect
->offset()], bitcodeSect
->size(),
2650 &_fileContent
[swiftCmdlineSect
->offset()], swiftCmdlineSect
->size()));
2652 throwf("Object file with bitcode missing cmdline options: %s", _file
->path());
2654 else if ( bundleSect
!= NULL
)
2655 _file
->_bitcode
= std::unique_ptr
<ld::Bitcode
>(new ld::BundleBitcode(&_fileContent
[bundleSect
->offset()], bundleSect
->size()));
2656 else if ( bitcodeAsm
)
2657 _file
->_bitcode
= std::unique_ptr
<ld::Bitcode
>(new ld::AsmBitcode(_fileContent
, _fileLength
));
2659 // sort by address (mach-o object files don't aways have sections sorted)
2660 ::qsort(machOSects
, count
, sizeof(MachOSectionAndSectionClass
<P
>), MachOSectionAndSectionClass
<P
>::sorter
);
2662 // we will synthesize a dummy Section<A> object for tentative definitions
2663 if ( _tentativeDefinitionCount
> 0 ) {
2664 totalSectionsSize
+= sizeof(TentativeDefinitionSection
<A
>);
2665 machOSects
[count
++].type
= sectionTypeTentativeDefinitions
;
2668 // we will synthesize a dummy Section<A> object for Absolute symbols
2669 if ( _absoluteSymbolCount
> 0 ) {
2670 totalSectionsSize
+= sizeof(AbsoluteSymbolSection
<A
>);
2671 machOSects
[count
++].type
= sectionTypeAbsoluteSymbols
;
2674 // allocate one block for all Section objects as well as pointers to each
2675 uint8_t* space
= new uint8_t[totalSectionsSize
+count
*sizeof(Section
<A
>*)];
2676 _file
->_sectionsArray
= (Section
<A
>**)space
;
2677 _file
->_sectionsArrayCount
= count
;
2678 Section
<A
>** objects
= _file
->_sectionsArray
;
2679 space
+= count
*sizeof(Section
<A
>*);
2680 for (uint32_t i
=0; i
< count
; ++i
) {
2681 switch ( machOSects
[i
].type
) {
2682 case sectionTypeIgnore
:
2684 case sectionTypeLiteral4
:
2685 *objects
++ = new (space
) Literal4Section
<A
>(*this, *_file
, machOSects
[i
].sect
);
2686 space
+= sizeof(Literal4Section
<A
>);
2688 case sectionTypeLiteral8
:
2689 *objects
++ = new (space
) Literal8Section
<A
>(*this, *_file
, machOSects
[i
].sect
);
2690 space
+= sizeof(Literal8Section
<A
>);
2692 case sectionTypeLiteral16
:
2693 *objects
++ = new (space
) Literal16Section
<A
>(*this, *_file
, machOSects
[i
].sect
);
2694 space
+= sizeof(Literal16Section
<A
>);
2696 case sectionTypeNonLazy
:
2697 *objects
++ = new (space
) NonLazyPointerSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2698 space
+= sizeof(NonLazyPointerSection
<A
>);
2700 case sectionTypeTLVPointers
:
2701 *objects
++ = new (space
) TLVPointerSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2702 space
+= sizeof(TLVPointerSection
<A
>);
2704 case sectionTypeCFI
:
2705 _EHFrameSection
= new (space
) CFISection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2706 *objects
++ = _EHFrameSection
;
2707 space
+= sizeof(CFISection
<A
>);
2709 case sectionTypeCString
:
2710 *objects
++ = new (space
) CStringSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2711 space
+= sizeof(CStringSection
<A
>);
2713 case sectionTypeCStringPointer
:
2714 *objects
++ = new (space
) PointerToCStringSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2715 space
+= sizeof(PointerToCStringSection
<A
>);
2717 case sectionTypeObjC1ClassRefs
:
2718 *objects
++ = new (space
) Objc1ClassReferences
<A
>(*this, *_file
, machOSects
[i
].sect
);
2719 space
+= sizeof(Objc1ClassReferences
<A
>);
2721 case sectionTypeUTF16Strings
:
2722 *objects
++ = new (space
) UTF16StringSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2723 space
+= sizeof(UTF16StringSection
<A
>);
2725 case sectionTypeCFString
:
2726 *objects
++ = new (space
) CFStringSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2727 space
+= sizeof(CFStringSection
<A
>);
2729 case sectionTypeObjC2ClassRefs
:
2730 *objects
++ = new (space
) ObjC2ClassRefsSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2731 space
+= sizeof(ObjC2ClassRefsSection
<A
>);
2733 case typeObjC2CategoryList
:
2734 *objects
++ = new (space
) ObjC2CategoryListSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2735 space
+= sizeof(ObjC2CategoryListSection
<A
>);
2737 case sectionTypeObjC1Classes
:
2738 *objects
++ = new (space
) ObjC1ClassSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2739 space
+= sizeof(ObjC1ClassSection
<A
>);
2741 case sectionTypeSymboled
:
2742 *objects
++ = new (space
) SymboledSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2743 space
+= sizeof(SymboledSection
<A
>);
2745 case sectionTypeTLVDefs
:
2746 *objects
++ = new (space
) TLVDefsSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2747 space
+= sizeof(TLVDefsSection
<A
>);
2749 case sectionTypeCompactUnwind
:
2750 _compactUnwindSection
= new (space
) CUSection
<A
>(*this, *_file
, machOSects
[i
].sect
);
2751 *objects
++ = _compactUnwindSection
;
2752 space
+= sizeof(CUSection
<A
>);
2754 case sectionTypeTentativeDefinitions
:
2755 *objects
++ = new (space
) TentativeDefinitionSection
<A
>(*this, *_file
);
2756 space
+= sizeof(TentativeDefinitionSection
<A
>);
2758 case sectionTypeAbsoluteSymbols
:
2759 _absoluteSection
= new (space
) AbsoluteSymbolSection
<A
>(*this, *_file
);
2760 *objects
++ = _absoluteSection
;
2761 space
+= sizeof(AbsoluteSymbolSection
<A
>);
2764 throw "internal error uknown SectionType";
2770 template <typename A
>
2771 Section
<A
>* Parser
<A
>::sectionForAddress(typename
A::P::uint_t addr
)
2773 for (uint32_t i
=0; i
< _file
->_sectionsArrayCount
; ++i
) {
2774 const macho_section
<typename
A::P
>* sect
= _file
->_sectionsArray
[i
]->machoSection();
2775 // TentativeDefinitionSection and AbsoluteSymbolSection have no mach-o section
2776 if ( sect
!= NULL
) {
2777 if ( (sect
->addr() <= addr
) && (addr
< (sect
->addr()+sect
->size())) ) {
2778 return _file
->_sectionsArray
[i
];
2782 // not strictly in any section
2783 // may be in a zero length section
2784 for (uint32_t i
=0; i
< _file
->_sectionsArrayCount
; ++i
) {
2785 const macho_section
<typename
A::P
>* sect
= _file
->_sectionsArray
[i
]->machoSection();
2786 // TentativeDefinitionSection and AbsoluteSymbolSection have no mach-o section
2787 if ( sect
!= NULL
) {
2788 if ( (sect
->addr() == addr
) && (sect
->size() == 0) ) {
2789 return _file
->_sectionsArray
[i
];
2794 throwf("sectionForAddress(0x%llX) address not in any section", (uint64_t)addr
);
2797 template <typename A
>
2798 Section
<A
>* Parser
<A
>::sectionForNum(unsigned int num
)
2800 for (uint32_t i
=0; i
< _file
->_sectionsArrayCount
; ++i
) {
2801 const macho_section
<typename
A::P
>* sect
= _file
->_sectionsArray
[i
]->machoSection();
2802 // TentativeDefinitionSection and AbsoluteSymbolSection have no mach-o section
2803 if ( sect
!= NULL
) {
2804 if ( num
== (unsigned int)((sect
- _sectionsStart
)+1) )
2805 return _file
->_sectionsArray
[i
];
2808 throwf("sectionForNum(%u) section number not for any section", num
);
2811 template <typename A
>
2812 Atom
<A
>* Parser
<A
>::findAtomByAddress(pint_t addr
)
2814 Section
<A
>* section
= this->sectionForAddress(addr
);
2815 return section
->findAtomByAddress(addr
);
2818 template <typename A
>
2819 Atom
<A
>* Parser
<A
>::findAtomByAddressOrNullIfStub(pint_t addr
)
2821 if ( hasStubsSection() && (_stubsMachOSection
->addr() <= addr
) && (addr
< (_stubsMachOSection
->addr()+_stubsMachOSection
->size())) )
2823 return findAtomByAddress(addr
);
2826 template <typename A
>
2827 Atom
<A
>* Parser
<A
>::findAtomByAddressOrLocalTargetOfStub(pint_t addr
, uint32_t* offsetInAtom
)
2829 if ( hasStubsSection() && (_stubsMachOSection
->addr() <= addr
) && (addr
< (_stubsMachOSection
->addr()+_stubsMachOSection
->size())) ) {
2830 // target is a stub, remove indirection
2831 uint32_t symbolIndex
= this->symbolIndexFromIndirectSectionAddress(addr
, _stubsMachOSection
);
2832 assert(symbolIndex
!= INDIRECT_SYMBOL_LOCAL
);
2833 const macho_nlist
<P
>& sym
= this->symbolFromIndex(symbolIndex
);
2834 // can't be to external weak symbol
2835 assert( (this->combineFromSymbol(sym
) != ld::Atom::combineByName
) || (this->scopeFromSymbol(sym
) != ld::Atom::scopeGlobal
) );
2837 return this->findAtomByName(this->nameFromSymbol(sym
));
2839 Atom
<A
>* target
= this->findAtomByAddress(addr
);
2840 *offsetInAtom
= addr
- target
->_objAddress
;
2844 template <typename A
>
2845 Atom
<A
>* Parser
<A
>::findAtomByName(const char* name
)
2847 uint8_t* p
= _file
->_atomsArray
;
2848 for(int i
=_file
->_atomsArrayCount
; i
> 0; --i
) {
2849 Atom
<A
>* atom
= (Atom
<A
>*)p
;
2850 if ( strcmp(name
, atom
->name()) == 0 )
2852 p
+= sizeof(Atom
<A
>);
2857 template <typename A
>
2858 void Parser
<A
>::findTargetFromAddress(pint_t addr
, TargetDesc
& target
)
2860 if ( hasStubsSection() && (_stubsMachOSection
->addr() <= addr
) && (addr
< (_stubsMachOSection
->addr()+_stubsMachOSection
->size())) ) {
2861 // target is a stub, remove indirection
2862 uint32_t symbolIndex
= this->symbolIndexFromIndirectSectionAddress(addr
, _stubsMachOSection
);
2863 assert(symbolIndex
!= INDIRECT_SYMBOL_LOCAL
);
2864 const macho_nlist
<P
>& sym
= this->symbolFromIndex(symbolIndex
);
2866 target
.name
= this->nameFromSymbol(sym
);
2867 target
.weakImport
= this->weakImportFromSymbol(sym
);
2871 Section
<A
>* section
= this->sectionForAddress(addr
);
2872 target
.atom
= section
->findAtomByAddress(addr
);
2873 target
.addend
= addr
- target
.atom
->_objAddress
;
2874 target
.weakImport
= false;
2878 template <typename A
>
2879 void Parser
<A
>::findTargetFromAddress(pint_t baseAddr
, pint_t addr
, TargetDesc
& target
)
2881 findTargetFromAddress(baseAddr
, target
);
2882 target
.addend
= addr
- target
.atom
->_objAddress
;
2885 template <typename A
>
2886 void Parser
<A
>::findTargetFromAddressAndSectionNum(pint_t addr
, unsigned int sectNum
, TargetDesc
& target
)
2888 if ( sectNum
== R_ABS
) {
2889 // target is absolute symbol that corresponds to addr
2890 if ( _absoluteSection
!= NULL
) {
2891 target
.atom
= _absoluteSection
->findAbsAtomForValue(addr
);
2892 if ( target
.atom
!= NULL
) {
2894 target
.weakImport
= false;
2899 throwf("R_ABS reloc but no absolute symbol at target address");
2902 if ( hasStubsSection() && (stubsSectionNum() == sectNum
) ) {
2903 // target is a stub, remove indirection
2904 uint32_t symbolIndex
= this->symbolIndexFromIndirectSectionAddress(addr
, _stubsMachOSection
);
2905 assert(symbolIndex
!= INDIRECT_SYMBOL_LOCAL
);
2906 const macho_nlist
<P
>& sym
= this->symbolFromIndex(symbolIndex
);
2907 // use direct reference when stub is to a static function
2908 if ( ((sym
.n_type() & N_TYPE
) == N_SECT
) && (((sym
.n_type() & N_EXT
) == 0) || (this->nameFromSymbol(sym
)[0] == 'L')) ) {
2909 this->findTargetFromAddressAndSectionNum(sym
.n_value(), sym
.n_sect(), target
);
2913 target
.name
= this->nameFromSymbol(sym
);
2914 target
.weakImport
= this->weakImportFromSymbol(sym
);
2919 Section
<A
>* section
= this->sectionForNum(sectNum
);
2920 target
.atom
= section
->findAtomByAddress(addr
);
2921 if ( target
.atom
== NULL
) {
2922 typedef typename
A::P::sint_t sint_t
;
2923 sint_t a
= (sint_t
)addr
;
2924 sint_t sectStart
= (sint_t
)(section
->machoSection()->addr());
2925 sint_t sectEnd
= sectStart
+ section
->machoSection()->size();
2926 if ( a
< sectStart
) {
2927 // target address is before start of section, so must be negative addend
2928 target
.atom
= section
->findAtomByAddress(sectStart
);
2929 target
.addend
= a
- sectStart
;
2930 target
.weakImport
= false;
2934 else if ( a
>= sectEnd
) {
2935 target
.atom
= section
->findAtomByAddress(sectEnd
-1);
2936 target
.addend
= a
- sectEnd
;
2937 target
.weakImport
= false;
2942 assert(target
.atom
!= NULL
);
2943 target
.addend
= addr
- target
.atom
->_objAddress
;
2944 target
.weakImport
= false;
2948 template <typename A
>
2949 void Parser
<A
>::addDtraceExtraInfos(const SourceLocation
& src
, const char* providerName
)
2951 // for every ___dtrace_stability$* and ___dtrace_typedefs$* undefine with
2952 // a matching provider name, add a by-name kDtraceTypeReference at probe site
2953 const char* dollar
= strchr(providerName
, '$');
2954 if ( dollar
!= NULL
) {
2955 int providerNameLen
= dollar
-providerName
+1;
2956 for ( std::vector
<const char*>::iterator it
= _dtraceProviderInfo
.begin(); it
!= _dtraceProviderInfo
.end(); ++it
) {
2957 const char* typeDollar
= strchr(*it
, '$');
2958 if ( typeDollar
!= NULL
) {
2959 if ( strncmp(typeDollar
+1, providerName
, providerNameLen
) == 0 ) {
2960 addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindDtraceExtra
,false, *it
);
2967 template <typename A
>
2968 const char* Parser
<A
>::scanSymbolTableForAddress(uint64_t addr
)
2970 uint64_t closestSymAddr
= 0;
2971 const char* closestSymName
= NULL
;
2972 for (uint32_t i
=0; i
< this->_symbolCount
; ++i
) {
2973 const macho_nlist
<P
>& sym
= symbolFromIndex(i
);
2975 if ( (sym
.n_type() & N_STAB
) != 0 )
2978 // only look at definitions
2979 if ( (sym
.n_type() & N_TYPE
) != N_SECT
)
2982 // return with exact match
2983 if ( sym
.n_value() == addr
) {
2984 const char* name
= nameFromSymbol(sym
);
2985 if ( strncmp(name
, "ltmp", 4) != 0 )
2987 // treat 'ltmp*' labels as close match
2988 closestSymAddr
= sym
.n_value();
2989 closestSymName
= name
;
2992 // record closest seen so far
2993 if ( (sym
.n_value() < addr
) && ((sym
.n_value() > closestSymAddr
) || (closestSymName
== NULL
)) )
2994 closestSymName
= nameFromSymbol(sym
);
2997 return (closestSymName
!= NULL
) ? closestSymName
: "unknown";
3001 template <typename A
>
3002 void Parser
<A
>::addFixups(const SourceLocation
& src
, ld::Fixup::Kind setKind
, const TargetDesc
& target
)
3004 // some fixup pairs can be combined
3005 ld::Fixup::Cluster cl
= ld::Fixup::k1of3
;
3006 ld::Fixup::Kind firstKind
= ld::Fixup::kindSetTargetAddress
;
3007 bool combined
= false;
3008 if ( target
.addend
== 0 ) {
3009 cl
= ld::Fixup::k1of1
;
3011 switch ( setKind
) {
3012 case ld::Fixup::kindStoreLittleEndian32
:
3013 firstKind
= ld::Fixup::kindStoreTargetAddressLittleEndian32
;
3015 case ld::Fixup::kindStoreLittleEndian64
:
3016 firstKind
= ld::Fixup::kindStoreTargetAddressLittleEndian64
;
3018 case ld::Fixup::kindStoreBigEndian32
:
3019 firstKind
= ld::Fixup::kindStoreTargetAddressBigEndian32
;
3021 case ld::Fixup::kindStoreBigEndian64
:
3022 firstKind
= ld::Fixup::kindStoreTargetAddressBigEndian64
;
3024 case ld::Fixup::kindStoreX86BranchPCRel32
:
3025 firstKind
= ld::Fixup::kindStoreTargetAddressX86BranchPCRel32
;
3027 case ld::Fixup::kindStoreX86PCRel32
:
3028 firstKind
= ld::Fixup::kindStoreTargetAddressX86PCRel32
;
3030 case ld::Fixup::kindStoreX86PCRel32GOTLoad
:
3031 firstKind
= ld::Fixup::kindStoreTargetAddressX86PCRel32GOTLoad
;
3033 case ld::Fixup::kindStoreX86PCRel32TLVLoad
:
3034 firstKind
= ld::Fixup::kindStoreTargetAddressX86PCRel32TLVLoad
;
3036 case ld::Fixup::kindStoreX86Abs32TLVLoad
:
3037 firstKind
= ld::Fixup::kindStoreTargetAddressX86Abs32TLVLoad
;
3039 case ld::Fixup::kindStoreARMBranch24
:
3040 firstKind
= ld::Fixup::kindStoreTargetAddressARMBranch24
;
3042 case ld::Fixup::kindStoreThumbBranch22
:
3043 firstKind
= ld::Fixup::kindStoreTargetAddressThumbBranch22
;
3045 #if SUPPORT_ARCH_arm64
3046 case ld::Fixup::kindStoreARM64Branch26
:
3047 firstKind
= ld::Fixup::kindStoreTargetAddressARM64Branch26
;
3049 case ld::Fixup::kindStoreARM64Page21
:
3050 firstKind
= ld::Fixup::kindStoreTargetAddressARM64Page21
;
3052 case ld::Fixup::kindStoreARM64PageOff12
:
3053 firstKind
= ld::Fixup::kindStoreTargetAddressARM64PageOff12
;
3055 case ld::Fixup::kindStoreARM64GOTLoadPage21
:
3056 firstKind
= ld::Fixup::kindStoreTargetAddressARM64GOTLoadPage21
;
3058 case ld::Fixup::kindStoreARM64GOTLoadPageOff12
:
3059 firstKind
= ld::Fixup::kindStoreTargetAddressARM64GOTLoadPageOff12
;
3061 case ld::Fixup::kindStoreARM64TLVPLoadPage21
:
3062 firstKind
= ld::Fixup::kindStoreTargetAddressARM64TLVPLoadPage21
;
3064 case ld::Fixup::kindStoreARM64TLVPLoadPageOff12
:
3065 firstKind
= ld::Fixup::kindStoreTargetAddressARM64TLVPLoadPageOff12
;
3070 cl
= ld::Fixup::k1of2
;
3075 if ( target
.atom
!= NULL
) {
3076 if ( target
.atom
->scope() == ld::Atom::scopeTranslationUnit
) {
3077 addFixup(src
, cl
, firstKind
, target
.atom
);
3079 else if ( (target
.atom
->combine() == ld::Atom::combineByNameAndContent
) || (target
.atom
->combine() == ld::Atom::combineByNameAndReferences
) ) {
3080 addFixup(src
, cl
, firstKind
, ld::Fixup::bindingByContentBound
, target
.atom
);
3082 else if ( (src
.atom
->section().type() == ld::Section::typeCFString
) && (src
.offsetInAtom
!= 0) ) {
3083 // backing string in CFStrings should always be direct
3084 addFixup(src
, cl
, firstKind
, target
.atom
);
3086 else if ( (src
.atom
== target
.atom
) && (target
.atom
->combine() == ld::Atom::combineByName
) ) {
3087 // reference to self should always be direct
3088 addFixup(src
, cl
, firstKind
, target
.atom
);
3091 // change direct fixup to by-name fixup
3092 addFixup(src
, cl
, firstKind
, false, target
.atom
->name());
3096 addFixup(src
, cl
, firstKind
, target
.weakImport
, target
.name
);
3098 if ( target
.addend
== 0 ) {
3100 addFixup(src
, ld::Fixup::k2of2
, setKind
);
3103 addFixup(src
, ld::Fixup::k2of3
, ld::Fixup::kindAddAddend
, target
.addend
);
3104 addFixup(src
, ld::Fixup::k3of3
, setKind
);
3108 template <typename A
>
3109 void Parser
<A
>::addFixups(const SourceLocation
& src
, ld::Fixup::Kind kind
, const TargetDesc
& target
, const TargetDesc
& picBase
)
3111 ld::Fixup::Cluster cl
= (target
.addend
== 0) ? ld::Fixup::k1of4
: ld::Fixup::k1of5
;
3112 if ( target
.atom
!= NULL
) {
3113 if ( target
.atom
->scope() == ld::Atom::scopeTranslationUnit
) {
3114 addFixup(src
, cl
, ld::Fixup::kindSetTargetAddress
, target
.atom
);
3116 else if ( (target
.atom
->combine() == ld::Atom::combineByNameAndContent
) || (target
.atom
->combine() == ld::Atom::combineByNameAndReferences
) ) {
3117 addFixup(src
, cl
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, target
.atom
);
3120 addFixup(src
, cl
, ld::Fixup::kindSetTargetAddress
, false, target
.atom
->name());
3124 addFixup(src
, cl
, ld::Fixup::kindSetTargetAddress
, target
.weakImport
, target
.name
);
3126 if ( target
.addend
== 0 ) {
3127 assert(picBase
.atom
!= NULL
);
3128 addFixup(src
, ld::Fixup::k2of4
, ld::Fixup::kindSubtractTargetAddress
, picBase
.atom
);
3129 addFixup(src
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractAddend
, picBase
.addend
);
3130 addFixup(src
, ld::Fixup::k4of4
, kind
);
3133 addFixup(src
, ld::Fixup::k2of5
, ld::Fixup::kindAddAddend
, target
.addend
);
3134 addFixup(src
, ld::Fixup::k3of5
, ld::Fixup::kindSubtractTargetAddress
, picBase
.atom
);
3135 addFixup(src
, ld::Fixup::k4of5
, ld::Fixup::kindSubtractAddend
, picBase
.addend
);
3136 addFixup(src
, ld::Fixup::k5of5
, kind
);
3142 template <typename A
>
3143 uint32_t TentativeDefinitionSection
<A
>::computeAtomCount(class Parser
<A
>& parser
,
3144 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
3145 const struct Parser
<A
>::CFI_CU_InfoArrays
&)
3147 return parser
.tentativeDefinitionCount();
3150 template <typename A
>
3151 uint32_t TentativeDefinitionSection
<A
>::appendAtoms(class Parser
<A
>& parser
, uint8_t* p
,
3152 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
3153 const struct Parser
<A
>::CFI_CU_InfoArrays
&)
3155 this->_beginAtoms
= (Atom
<A
>*)p
;
3157 for (uint32_t i
=parser
.undefinedStartIndex(); i
< parser
.undefinedEndIndex(); ++i
) {
3158 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(i
);
3159 if ( ((sym
.n_type() & N_TYPE
) == N_UNDF
) && (sym
.n_value() != 0) ) {
3160 uint64_t size
= sym
.n_value();
3161 uint8_t alignP2
= GET_COMM_ALIGN(sym
.n_desc());
3162 if ( alignP2
== 0 ) {
3163 // common symbols align to their size
3164 // that is, a 4-byte common aligns to 4-bytes
3165 // if this size is not a power of two,
3166 // then round up to the next power of two
3167 alignP2
= 63 - (uint8_t)__builtin_clzll(size
);
3168 if ( size
!= (1ULL << alignP2
) )
3171 // limit alignment of extremely large commons to 2^15 bytes (8-page)
3172 if ( alignP2
> parser
.maxDefaultCommonAlignment() )
3173 alignP2
= parser
.maxDefaultCommonAlignment();
3174 Atom
<A
>* allocatedSpace
= (Atom
<A
>*)p
;
3175 new (allocatedSpace
) Atom
<A
>(*this, parser
.nameFromSymbol(sym
), (pint_t
)ULLONG_MAX
, size
,
3176 ld::Atom::definitionTentative
, ld::Atom::combineByName
,
3177 parser
.scopeFromSymbol(sym
), ld::Atom::typeZeroFill
, ld::Atom::symbolTableIn
,
3178 parser
.dontDeadStripFromSymbol(sym
), false, false, ld::Atom::Alignment(alignP2
) );
3179 p
+= sizeof(Atom
<A
>);
3183 this->_endAtoms
= (Atom
<A
>*)p
;
3188 template <typename A
>
3189 uint32_t AbsoluteSymbolSection
<A
>::computeAtomCount(class Parser
<A
>& parser
,
3190 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
3191 const struct Parser
<A
>::CFI_CU_InfoArrays
&)
3193 return parser
.absoluteSymbolCount();
3196 template <typename A
>
3197 uint32_t AbsoluteSymbolSection
<A
>::appendAtoms(class Parser
<A
>& parser
, uint8_t* p
,
3198 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
3199 const struct Parser
<A
>::CFI_CU_InfoArrays
&)
3201 this->_beginAtoms
= (Atom
<A
>*)p
;
3203 for (uint32_t i
=0; i
< parser
.symbolCount(); ++i
) {
3204 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(i
);
3205 if ( (sym
.n_type() & N_TYPE
) != N_ABS
)
3207 const char* absName
= parser
.nameFromSymbol(sym
);
3208 // ignore .objc_class_name_* symbols
3209 if ( strncmp(absName
, ".objc_class_name_", 17) == 0 )
3211 // ignore .objc_class_name_* symbols
3212 if ( strncmp(absName
, ".objc_category_name_", 20) == 0 )
3214 // ignore empty *.eh symbols
3215 if ( strcmp(&absName
[strlen(absName
)-3], ".eh") == 0 )
3218 Atom
<A
>* allocatedSpace
= (Atom
<A
>*)p
;
3219 new (allocatedSpace
) Atom
<A
>(*this, parser
, sym
, 0);
3220 p
+= sizeof(Atom
<A
>);
3223 this->_endAtoms
= (Atom
<A
>*)p
;
3227 template <typename A
>
3228 Atom
<A
>* AbsoluteSymbolSection
<A
>::findAbsAtomForValue(typename
A::P::uint_t value
)
3230 Atom
<A
>* end
= this->_endAtoms
;
3231 for(Atom
<A
>* p
= this->_beginAtoms
; p
< end
; ++p
) {
3232 if ( p
->_objAddress
== value
)
3239 template <typename A
>
3240 uint32_t Parser
<A
>::indirectSymbol(uint32_t indirectIndex
)
3242 if ( indirectIndex
>= _indirectTableCount
)
3243 throw "indirect symbol index out of range";
3244 return E::get32(_indirectTable
[indirectIndex
]);
3247 template <typename A
>
3248 const macho_nlist
<typename
A::P
>& Parser
<A
>::symbolFromIndex(uint32_t index
)
3250 if ( index
> _symbolCount
)
3251 throw "symbol index out of range";
3252 return _symbols
[index
];
3255 template <typename A
>
3256 const macho_section
<typename
A::P
>* Parser
<A
>::machOSectionFromSectionIndex(uint32_t index
)
3258 if ( index
>= _machOSectionsCount
)
3259 throw "section index out of range";
3260 return &_sectionsStart
[index
];
3263 template <typename A
>
3264 uint32_t Parser
<A
>::symbolIndexFromIndirectSectionAddress(pint_t addr
, const macho_section
<P
>* sect
)
3266 uint32_t elementSize
= 0;
3267 switch ( sect
->flags() & SECTION_TYPE
) {
3268 case S_SYMBOL_STUBS
:
3269 elementSize
= sect
->reserved2();
3271 case S_LAZY_SYMBOL_POINTERS
:
3272 case S_NON_LAZY_SYMBOL_POINTERS
:
3273 elementSize
= sizeof(pint_t
);
3276 throw "section does not use inirect symbol table";
3278 uint32_t indexInSection
= (addr
- sect
->addr()) / elementSize
;
3279 uint32_t indexIntoIndirectTable
= sect
->reserved1() + indexInSection
;
3280 return this->indirectSymbol(indexIntoIndirectTable
);
3285 template <typename A
>
3286 const char* Parser
<A
>::nameFromSymbol(const macho_nlist
<P
>& sym
)
3288 return &_strings
[sym
.n_strx()];
3291 template <typename A
>
3292 ld::Atom::Scope Parser
<A
>::scopeFromSymbol(const macho_nlist
<P
>& sym
)
3294 if ( (sym
.n_type() & N_EXT
) == 0 )
3295 return ld::Atom::scopeTranslationUnit
;
3296 else if ( (sym
.n_type() & N_PEXT
) != 0 )
3297 return ld::Atom::scopeLinkageUnit
;
3298 else if ( this->nameFromSymbol(sym
)[0] == 'l' ) // since all 'l' symbols will be remove, don't make them global
3299 return ld::Atom::scopeLinkageUnit
;
3301 return ld::Atom::scopeGlobal
;
3304 template <typename A
>
3305 ld::Atom::Definition Parser
<A
>::definitionFromSymbol(const macho_nlist
<P
>& sym
)
3307 switch ( sym
.n_type() & N_TYPE
) {
3309 return ld::Atom::definitionAbsolute
;
3311 return ld::Atom::definitionRegular
;
3313 if ( sym
.n_value() != 0 )
3314 return ld::Atom::definitionTentative
;
3316 throw "definitionFromSymbol() bad symbol";
3319 template <typename A
>
3320 ld::Atom::Combine Parser
<A
>::combineFromSymbol(const macho_nlist
<P
>& sym
)
3322 if ( sym
.n_desc() & N_WEAK_DEF
)
3323 return ld::Atom::combineByName
;
3325 return ld::Atom::combineNever
;
3329 template <typename A
>
3330 ld::Atom::SymbolTableInclusion Parser
<A
>::inclusionFromSymbol(const macho_nlist
<P
>& sym
)
3332 const char* symbolName
= nameFromSymbol(sym
);
3333 // labels beginning with 'l' (lowercase ell) are automatically removed in final linked images <rdar://problem/4571042>
3334 // labels beginning with 'L' should have been stripped by the assembler, so are stripped now
3335 if ( sym
.n_desc() & REFERENCED_DYNAMICALLY
)
3336 return ld::Atom::symbolTableInAndNeverStrip
;
3337 else if ( symbolName
[0] == 'l' )
3338 return ld::Atom::symbolTableNotInFinalLinkedImages
;
3339 else if ( symbolName
[0] == 'L' )
3340 return ld::Atom::symbolTableNotIn
;
3342 return ld::Atom::symbolTableIn
;
3345 template <typename A
>
3346 bool Parser
<A
>::dontDeadStripFromSymbol(const macho_nlist
<P
>& sym
)
3348 return ( (sym
.n_desc() & (N_NO_DEAD_STRIP
|REFERENCED_DYNAMICALLY
)) != 0 );
3351 template <typename A
>
3352 bool Parser
<A
>::isThumbFromSymbol(const macho_nlist
<P
>& sym
)
3354 return ( sym
.n_desc() & N_ARM_THUMB_DEF
);
3357 template <typename A
>
3358 bool Parser
<A
>::weakImportFromSymbol(const macho_nlist
<P
>& sym
)
3360 return ( ((sym
.n_type() & N_TYPE
) == N_UNDF
) && ((sym
.n_desc() & N_WEAK_REF
) != 0) );
3363 template <typename A
>
3364 bool Parser
<A
>::resolverFromSymbol(const macho_nlist
<P
>& sym
)
3366 return ( sym
.n_desc() & N_SYMBOL_RESOLVER
);
3369 template <typename A
>
3370 bool Parser
<A
>::altEntryFromSymbol(const macho_nlist
<P
>& sym
)
3372 return ( sym
.n_desc() & N_ALT_ENTRY
);
3376 /* Skip over a LEB128 value (signed or unsigned). */
3378 skip_leb128 (const uint8_t ** offset
, const uint8_t * end
)
3380 while (*offset
!= end
&& **offset
>= 0x80)
3386 /* Read a ULEB128 into a 64-bit word. Return (uint64_t)-1 on overflow
3387 or error. On overflow, skip past the rest of the uleb128. */
3389 read_uleb128 (const uint8_t ** offset
, const uint8_t * end
)
3391 uint64_t result
= 0;
3398 return (uint64_t) -1;
3400 b
= **offset
& 0x7f;
3402 if (bit
>= 64 || b
<< bit
>> bit
!= b
)
3403 result
= (uint64_t) -1;
3405 result
|= b
<< bit
, bit
+= 7;
3406 } while (*(*offset
)++ >= 0x80);
3411 /* Skip over a DWARF attribute of form FORM. */
3412 template <typename A
>
3413 bool Parser
<A
>::skip_form(const uint8_t ** offset
, const uint8_t * end
, uint64_t form
,
3414 uint8_t addr_size
, bool dwarf64
)
3424 case DW_FORM_block2
:
3425 if (end
- *offset
< 2)
3427 sz
= 2 + A::P::E::get16(*(uint16_t*)offset
);
3430 case DW_FORM_block4
:
3431 if (end
- *offset
< 4)
3433 sz
= 2 + A::P::E::get32(*(uint32_t*)offset
);
3451 case DW_FORM_string
:
3452 while (*offset
!= end
&& **offset
)
3461 sz
= read_uleb128 (offset
, end
);
3464 case DW_FORM_block1
:
3472 case DW_FORM_ref_udata
:
3473 skip_leb128 (offset
, end
);
3477 case DW_FORM_ref_addr
:
3481 case DW_FORM_sec_offset
:
3482 sz
= sizeof(typename
A::P::uint_t
);
3485 case DW_FORM_exprloc
:
3486 sz
= read_uleb128 (offset
, end
);
3489 case DW_FORM_flag_present
:
3493 case DW_FORM_ref_sig8
:
3500 if (end
- *offset
< sz
)
3507 template <typename A
>
3508 const char* Parser
<A
>::getDwarfString(uint64_t form
, const uint8_t*& di
)
3511 const char* dwarfStrings
;
3512 const char* result
= NULL
;
3514 case DW_FORM_string
:
3515 result
= (const char*)di
;
3516 di
+= strlen(result
) + 1;
3519 offset
= E::get32(*((uint32_t*)di
));
3520 dwarfStrings
= (char*)_file
->fileContent() + _file
->_dwarfDebugStringSect
->offset();
3521 if ( offset
< _file
->_dwarfDebugStringSect
->size() )
3522 result
= &dwarfStrings
[offset
];
3524 warning("dwarf DW_FORM_strp (offset=0x%08X) is too big in %s", offset
, this->_path
);
3528 warning("unknown dwarf string encoding (form=%lld) in %s", form
, this->_path
);
3534 template <typename A
>
3535 uint64_t Parser
<A
>::getDwarfOffset(uint64_t form
, const uint8_t*& di
, bool dwarf64
)
3537 if ( form
== DW_FORM_sec_offset
)
3538 form
= (dwarf64
? DW_FORM_data8
: DW_FORM_data4
);
3539 uint64_t result
= -1;
3542 result
= A::P::E::get32(*(uint32_t*)di
);
3546 result
= A::P::E::get64(*(uint64_t*)di
);
3550 warning("unknown dwarf DW_FORM_ for DW_AT_stmt_list in %s", this->_path
);
3556 template <typename A
>
3557 struct AtomAndLineInfo
{
3559 ld::Atom::LineInfo info
;
3563 // <rdar://problem/5591394> Add support to ld64 for N_FUN stabs when used for symbolic constants
3564 // Returns whether a stabStr belonging to an N_FUN stab represents a
3565 // symbolic constant rather than a function
3566 template <typename A
>
3567 bool Parser
<A
>::isConstFunStabs(const char *stabStr
)
3570 // N_FUN can be used for both constants and for functions. In case it's a constant,
3571 // the format of the stabs string is "symname:c=<value>;"
3572 // ':' cannot appear in the symbol name, except if it's an Objective-C method
3573 // (in which case the symbol name starts with + or -, and then it's definitely
3575 return (stabStr
!= NULL
) && (stabStr
[0] != '+') && (stabStr
[0] != '-')
3576 && ((colon
= strchr(stabStr
, ':')) != NULL
)
3577 && (colon
[1] == 'c') && (colon
[2] == '=');
3581 template <typename A
>
3582 void Parser
<A
>::parseDebugInfo()
3584 // check for dwarf __debug_info section
3585 if ( _file
->_dwarfDebugInfoSect
== NULL
) {
3586 // if no DWARF debug info, look for stabs
3590 if ( _file
->_dwarfDebugInfoSect
->size() == 0 )
3596 if ( !read_comp_unit(&tuName
, &tuDir
, &stmtList
) ) {
3597 // if can't parse dwarf, warn and give up
3598 _file
->_dwarfTranslationUnitPath
= NULL
;
3599 warning("can't parse dwarf compilation unit info in %s", _path
);
3600 _file
->_debugInfoKind
= ld::relocatable::File::kDebugInfoNone
;
3603 if ( (tuName
!= NULL
) && (tuName
[0] == '/') ) {
3604 _file
->_dwarfTranslationUnitPath
= tuName
;
3606 else if ( (tuDir
!= NULL
) && (tuName
!= NULL
) ) {
3607 asprintf((char**)&(_file
->_dwarfTranslationUnitPath
), "%s/%s", tuDir
, tuName
);
3609 else if ( tuDir
== NULL
) {
3610 _file
->_dwarfTranslationUnitPath
= tuName
;
3613 _file
->_dwarfTranslationUnitPath
= NULL
;
3616 // add line number info to atoms from dwarf
3617 std::vector
<AtomAndLineInfo
<A
> > entries
;
3618 entries
.reserve(64);
3619 if ( _file
->_debugInfoKind
== ld::relocatable::File::kDebugInfoDwarf
) {
3620 // file with just data will have no __debug_line info
3621 if ( (_file
->_dwarfDebugLineSect
!= NULL
) && (_file
->_dwarfDebugLineSect
->size() != 0) ) {
3622 // validate stmt_list
3623 if ( (stmtList
!= (uint64_t)-1) && (stmtList
< _file
->_dwarfDebugLineSect
->size()) ) {
3624 const uint8_t* debug_line
= (uint8_t*)_file
->fileContent() + _file
->_dwarfDebugLineSect
->offset();
3625 struct line_reader_data
* lines
= line_open(&debug_line
[stmtList
],
3626 _file
->_dwarfDebugLineSect
->size() - stmtList
, E::little_endian
);
3627 struct line_info result
;
3628 Atom
<A
>* curAtom
= NULL
;
3629 uint32_t curAtomOffset
= 0;
3630 uint32_t curAtomAddress
= 0;
3631 uint32_t curAtomSize
= 0;
3632 std::map
<uint32_t,const char*> dwarfIndexToFile
;
3633 if ( lines
!= NULL
) {
3634 while ( line_next(lines
, &result
, line_stop_pc
) ) {
3635 //fprintf(stderr, "curAtom=%p, result.pc=0x%llX, result.line=%llu, result.end_of_sequence=%d,"
3636 // " curAtomAddress=0x%X, curAtomSize=0x%X\n",
3637 // curAtom, result.pc, result.line, result.end_of_sequence, curAtomAddress, curAtomSize);
3638 // work around weird debug line table compiler generates if no functions in __text section
3639 if ( (curAtom
== NULL
) && (result
.pc
== 0) && result
.end_of_sequence
&& (result
.file
== 1))
3641 // for performance, see if in next pc is in current atom
3642 if ( (curAtom
!= NULL
) && (curAtomAddress
<= result
.pc
) && (result
.pc
< (curAtomAddress
+curAtomSize
)) ) {
3643 curAtomOffset
= result
.pc
- curAtomAddress
;
3645 // or pc at end of current atom
3646 else if ( result
.end_of_sequence
&& (curAtom
!= NULL
) && (result
.pc
== (curAtomAddress
+curAtomSize
)) ) {
3647 curAtomOffset
= result
.pc
- curAtomAddress
;
3649 // or only one function that is a one line function
3650 else if ( result
.end_of_sequence
&& (curAtom
== NULL
) && (this->findAtomByAddress(0) != NULL
) && (result
.pc
== this->findAtomByAddress(0)->size()) ) {
3651 curAtom
= this->findAtomByAddress(0);
3652 curAtomOffset
= result
.pc
- curAtom
->objectAddress();
3653 curAtomAddress
= curAtom
->objectAddress();
3654 curAtomSize
= curAtom
->size();
3657 // do slow look up of atom by address
3659 curAtom
= this->findAtomByAddress(result
.pc
);
3662 // in case of bug in debug info, don't abort link, just limp on
3665 if ( curAtom
== NULL
)
3666 break; // file has line info but no functions
3667 if ( result
.end_of_sequence
&& (curAtomAddress
+curAtomSize
< result
.pc
) ) {
3668 // a one line function can be returned by line_next() as one entry with pc at end of blob
3669 // look for alt atom starting at end of previous atom
3670 uint32_t previousEnd
= curAtomAddress
+curAtomSize
;
3671 Atom
<A
>* alt
= this->findAtomByAddressOrNullIfStub(previousEnd
);
3673 continue; // ignore spurious debug info for stubs
3674 if ( result
.pc
<= alt
->objectAddress() + alt
->size() ) {
3676 curAtomOffset
= result
.pc
- alt
->objectAddress();
3677 curAtomAddress
= alt
->objectAddress();
3678 curAtomSize
= alt
->size();
3681 curAtomOffset
= result
.pc
- curAtom
->objectAddress();
3682 curAtomAddress
= curAtom
->objectAddress();
3683 curAtomSize
= curAtom
->size();
3687 curAtomOffset
= result
.pc
- curAtom
->objectAddress();
3688 curAtomAddress
= curAtom
->objectAddress();
3689 curAtomSize
= curAtom
->size();
3692 const char* filename
;
3693 std::map
<uint32_t,const char*>::iterator pos
= dwarfIndexToFile
.find(result
.file
);
3694 if ( pos
== dwarfIndexToFile
.end() ) {
3695 filename
= line_file(lines
, result
.file
);
3696 dwarfIndexToFile
[result
.file
] = filename
;
3699 filename
= pos
->second
;
3701 // only record for ~8000 line info records per function
3702 if ( curAtom
->roomForMoreLineInfoCount() ) {
3703 AtomAndLineInfo
<A
> entry
;
3704 entry
.atom
= curAtom
;
3705 entry
.info
.atomOffset
= curAtomOffset
;
3706 entry
.info
.fileName
= filename
;
3707 entry
.info
.lineNumber
= result
.line
;
3708 //fprintf(stderr, "addr=0x%08llX, line=%lld, file=%s, atom=%s, atom.size=0x%X, end=%d\n",
3709 // result.pc, result.line, filename, curAtom->name(), curAtomSize, result.end_of_sequence);
3710 entries
.push_back(entry
);
3711 curAtom
->incrementLineInfoCount();
3713 if ( result
.end_of_sequence
) {
3723 // assign line info start offset for each atom
3724 uint8_t* p
= _file
->_atomsArray
;
3725 uint32_t liOffset
= 0;
3726 for(int i
=_file
->_atomsArrayCount
; i
> 0; --i
) {
3727 Atom
<A
>* atom
= (Atom
<A
>*)p
;
3728 atom
->_lineInfoStartIndex
= liOffset
;
3729 liOffset
+= atom
->_lineInfoCount
;
3730 atom
->_lineInfoCount
= 0;
3731 p
+= sizeof(Atom
<A
>);
3733 assert(liOffset
== entries
.size());
3734 _file
->_lineInfos
.resize(liOffset
);
3736 // copy each line info for each atom
3737 for (typename
std::vector
<AtomAndLineInfo
<A
> >::iterator it
= entries
.begin(); it
!= entries
.end(); ++it
) {
3738 uint32_t slot
= it
->atom
->_lineInfoStartIndex
+ it
->atom
->_lineInfoCount
;
3739 _file
->_lineInfos
[slot
] = it
->info
;
3740 it
->atom
->_lineInfoCount
++;
3743 // done with temp vector
3747 template <typename A
>
3748 void Parser
<A
>::parseStabs()
3750 // scan symbol table for stabs entries
3751 Atom
<A
>* currentAtom
= NULL
;
3752 pint_t currentAtomAddress
= 0;
3753 enum { start
, inBeginEnd
, inFun
} state
= start
;
3754 for (uint32_t symbolIndex
= 0; symbolIndex
< _symbolCount
; ++symbolIndex
) {
3755 const macho_nlist
<P
>& sym
= this->symbolFromIndex(symbolIndex
);
3756 bool useStab
= true;
3757 uint8_t type
= sym
.n_type();
3758 const char* symString
= (sym
.n_strx() != 0) ? this->nameFromSymbol(sym
) : NULL
;
3759 if ( (type
& N_STAB
) != 0 ) {
3760 _file
->_debugInfoKind
= (_hasUUID
? ld::relocatable::File::kDebugInfoStabsUUID
: ld::relocatable::File::kDebugInfoStabs
);
3761 ld::relocatable::File::Stab stab
;
3764 stab
.other
= sym
.n_sect();
3765 stab
.desc
= sym
.n_desc();
3766 stab
.value
= sym
.n_value();
3772 // beginning of function block
3774 // fall into case to lookup atom by addresss
3777 currentAtomAddress
= sym
.n_value();
3778 currentAtom
= this->findAtomByAddress(currentAtomAddress
);
3779 if ( currentAtom
!= NULL
) {
3780 stab
.atom
= currentAtom
;
3781 stab
.string
= symString
;
3784 fprintf(stderr
, "can't find atom for stabs BNSYM at %08llX in %s",
3785 (uint64_t)sym
.n_value(), _path
);
3795 // not associated with an atom, just copy
3796 stab
.string
= symString
;
3800 // n_value field is NOT atom address ;-(
3801 // need to find atom by name match
3802 const char* colon
= strchr(symString
, ':');
3803 if ( colon
!= NULL
) {
3804 // build underscore leading name
3805 int nameLen
= colon
- symString
;
3806 char symName
[nameLen
+2];
3807 strlcpy(&symName
[1], symString
, nameLen
+1);
3809 symName
[nameLen
+1] = '\0';
3810 currentAtom
= this->findAtomByName(symName
);
3811 if ( currentAtom
!= NULL
) {
3812 stab
.atom
= currentAtom
;
3813 stab
.string
= symString
;
3817 // might be a debug-note without trailing :G()
3818 currentAtom
= this->findAtomByName(symString
);
3819 if ( currentAtom
!= NULL
) {
3820 stab
.atom
= currentAtom
;
3821 stab
.string
= symString
;
3824 if ( stab
.atom
== NULL
) {
3825 // ld_classic added bogus GSYM stabs for old style dtrace probes
3826 if ( (strncmp(symString
, "__dtrace_probe$", 15) != 0) )
3827 warning("can't find atom for N_GSYM stabs %s in %s", symString
, _path
);
3833 if ( isConstFunStabs(symString
) ) {
3834 // constant not associated with a function
3835 stab
.string
= symString
;
3838 // old style stabs without BNSYM
3840 currentAtomAddress
= sym
.n_value();
3841 currentAtom
= this->findAtomByAddress(currentAtomAddress
);
3842 if ( currentAtom
!= NULL
) {
3843 stab
.atom
= currentAtom
;
3844 stab
.string
= symString
;
3847 warning("can't find atom for stabs FUN at %08llX in %s",
3848 (uint64_t)currentAtomAddress
, _path
);
3854 stab
.string
= symString
;
3860 stab
.string
= symString
;
3861 // -gfull built .o file
3864 warning("unknown stabs type 0x%X in %s", type
, _path
);
3868 stab
.atom
= currentAtom
;
3877 Atom
<A
>* nestedAtom
= this->findAtomByAddress(sym
.n_value());
3878 if ( nestedAtom
!= NULL
) {
3879 stab
.atom
= nestedAtom
;
3880 stab
.string
= symString
;
3883 warning("can't find atom for stabs 0x%X at %08llX in %s",
3884 type
, (uint64_t)sym
.n_value(), _path
);
3891 // adjust value to be offset in atom
3892 stab
.value
-= currentAtomAddress
;
3894 stab
.string
= symString
;
3901 if ( isConstFunStabs(symString
) ) {
3902 stab
.atom
= currentAtom
;
3903 stab
.string
= symString
;
3906 if ( sym
.n_sect() != 0 ) {
3907 // found another start stab, must be really old stabs...
3908 currentAtomAddress
= sym
.n_value();
3909 currentAtom
= this->findAtomByAddress(currentAtomAddress
);
3910 if ( currentAtom
!= NULL
) {
3911 stab
.atom
= currentAtom
;
3912 stab
.string
= symString
;
3915 warning("can't find atom for stabs FUN at %08llX in %s",
3916 (uint64_t)currentAtomAddress
, _path
);
3920 // found ending stab, switch back to start state
3921 stab
.string
= symString
;
3922 stab
.atom
= currentAtom
;
3931 // adjust value to be offset in atom
3932 stab
.value
-= currentAtomAddress
;
3933 stab
.atom
= currentAtom
;
3936 stab
.string
= symString
;
3940 stab
.atom
= currentAtom
;
3941 stab
.string
= symString
;
3946 // add to list of stabs for this .o file
3948 _file
->_stabs
.push_back(stab
);
3955 // Look at the compilation unit DIE and determine
3956 // its NAME, compilation directory (in COMP_DIR) and its
3957 // line number information offset (in STMT_LIST). NAME and COMP_DIR
3958 // may be NULL (especially COMP_DIR) if they are not in the .o file;
3959 // STMT_LIST will be (uint64_t) -1.
3961 // At present this assumes that there's only one compilation unit DIE.
3963 template <typename A
>
3964 bool Parser
<A
>::read_comp_unit(const char ** name
, const char ** comp_dir
,
3965 uint64_t *stmt_list
)
3967 const uint8_t * debug_info
;
3968 const uint8_t * debug_abbrev
;
3970 const uint8_t * next_cu
;
3972 const uint8_t * end
;
3973 const uint8_t * enda
;
3976 uint64_t abbrev_base
;
3978 uint8_t address_size
;
3983 *stmt_list
= (uint64_t) -1;
3985 if ( (_file
->_dwarfDebugInfoSect
== NULL
) || (_file
->_dwarfDebugAbbrevSect
== NULL
) )
3988 if (_file
->_dwarfDebugInfoSect
->size() < 12)
3989 /* Too small to be a real debug_info section. */
3992 debug_info
= (uint8_t*)_file
->fileContent() + _file
->_dwarfDebugInfoSect
->offset();
3993 debug_abbrev
= (uint8_t*)_file
->fileContent() + _file
->_dwarfDebugAbbrevSect
->offset();
3994 next_cu
= debug_info
;
3996 while ((uint64_t)(next_cu
- debug_info
) < _file
->_dwarfDebugInfoSect
->size()) {
3998 sz
= A::P::E::get32(*(uint32_t*)di
);
4000 dwarf64
= sz
== 0xffffffff;
4002 sz
= A::P::E::get64(*(uint64_t*)di
), di
+= 8;
4003 else if (sz
> 0xffffff00)
4004 /* Unknown dwarf format. */
4007 /* Verify claimed size. */
4008 if (sz
+ (di
- debug_info
) > _file
->_dwarfDebugInfoSect
->size() || sz
<= (dwarf64
? 23 : 11))
4013 vers
= A::P::E::get16(*(uint16_t*)di
);
4014 if (vers
< 2 || vers
> 4)
4015 /* DWARF version wrong for this code.
4016 Chances are we could continue anyway, but we don't know for sure. */
4020 /* Find the debug_abbrev section. */
4021 abbrev_base
= dwarf64
? A::P::E::get64(*(uint64_t*)di
) : A::P::E::get32(*(uint32_t*)di
);
4022 di
+= dwarf64
? 8 : 4;
4024 if (abbrev_base
> _file
->_dwarfDebugAbbrevSect
->size())
4026 da
= debug_abbrev
+ abbrev_base
;
4027 enda
= debug_abbrev
+ _file
->_dwarfDebugAbbrevSect
->size();
4029 address_size
= *di
++;
4031 /* Find the abbrev number we're looking for. */
4033 abbrev
= read_uleb128 (&di
, end
);
4034 if (abbrev
== (uint64_t) -1)
4037 /* Skip through the debug_abbrev section looking for that abbrev. */
4040 uint64_t this_abbrev
= read_uleb128 (&da
, enda
);
4043 if (this_abbrev
== abbrev
)
4044 /* This is almost always taken. */
4046 skip_leb128 (&da
, enda
); /* Skip the tag. */
4049 da
++; /* Skip the DW_CHILDREN_* value. */
4052 attr
= read_uleb128 (&da
, enda
);
4053 skip_leb128 (&da
, enda
);
4054 } while (attr
!= 0 && attr
!= (uint64_t) -1);
4059 /* Check that the abbrev is one for a DW_TAG_compile_unit. */
4060 if (read_uleb128 (&da
, enda
) != DW_TAG_compile_unit
)
4064 da
++; /* Skip the DW_CHILDREN_* value. */
4066 /* Now, go through the DIE looking for DW_AT_name,
4067 DW_AT_comp_dir, and DW_AT_stmt_list. */
4068 bool skip_to_next_cu
= false;
4069 while (!skip_to_next_cu
) {
4071 uint64_t attr
= read_uleb128 (&da
, enda
);
4072 uint64_t form
= read_uleb128 (&da
, enda
);
4074 if (attr
== (uint64_t) -1)
4078 if (form
== DW_FORM_indirect
)
4079 form
= read_uleb128 (&di
, end
);
4083 *name
= getDwarfString(form
, di
);
4084 /* Swift object files may contain two CUs: One
4085 describes the Swift code, one is created by the
4086 clang importer. Skip over the CU created by the
4087 clang importer as it may be empty. */
4088 if (std::string(*name
) == "<swift-imported-modules>")
4089 skip_to_next_cu
= true;
4091 case DW_AT_comp_dir
:
4092 *comp_dir
= getDwarfString(form
, di
);
4094 case DW_AT_stmt_list
:
4095 *stmt_list
= getDwarfOffset(form
, di
, dwarf64
);
4098 if (! skip_form (&di
, end
, form
, address_size
, dwarf64
))
4108 template <typename A
>
4111 free(_sectionsArray
);
4115 template <typename A
>
4116 const char* File
<A
>::translationUnitSource() const
4118 return _dwarfTranslationUnitPath
;
4121 template <typename A
>
4122 bool File
<A
>::forEachAtom(ld::File::AtomHandler
& handler
) const
4124 handler
.doFile(*this);
4125 uint8_t* p
= _atomsArray
;
4126 for(int i
=_atomsArrayCount
; i
> 0; --i
) {
4127 handler
.doAtom(*((Atom
<A
>*)p
));
4128 p
+= sizeof(Atom
<A
>);
4130 p
= _aliasAtomsArray
;
4131 for(int i
=_aliasAtomsArrayCount
; i
> 0; --i
) {
4132 handler
.doAtom(*((AliasAtom
*)p
));
4133 p
+= sizeof(AliasAtom
);
4136 return (_atomsArrayCount
!= 0) || (_aliasAtomsArrayCount
!= 0);
4139 template <typename A
>
4140 const char* Section
<A
>::makeSegmentName(const macho_section
<typename
A::P
>* sect
)
4142 // mach-o section record only has room for 16-byte seg/sect names
4143 // so a 16-byte name has no trailing zero
4144 const char* name
= sect
->segname();
4145 if ( strlen(name
) < 16 )
4147 char* tmp
= new char[17];
4148 strlcpy(tmp
, name
, 17);
4152 template <typename A
>
4153 const char* Section
<A
>::makeSectionName(const macho_section
<typename
A::P
>* sect
)
4155 const char* name
= sect
->sectname();
4156 if ( strlen(name
) < 16 )
4159 // special case common long section names so we don't have to malloc
4160 if ( strncmp(sect
->sectname(), "__objc_classrefs", 16) == 0 )
4161 return "__objc_classrefs";
4162 if ( strncmp(sect
->sectname(), "__objc_classlist", 16) == 0 )
4163 return "__objc_classlist";
4164 if ( strncmp(sect
->sectname(), "__objc_nlclslist", 16) == 0 )
4165 return "__objc_nlclslist";
4166 if ( strncmp(sect
->sectname(), "__objc_nlcatlist", 16) == 0 )
4167 return "__objc_nlcatlist";
4168 if ( strncmp(sect
->sectname(), "__objc_protolist", 16) == 0 )
4169 return "__objc_protolist";
4170 if ( strncmp(sect
->sectname(), "__objc_protorefs", 16) == 0 )
4171 return "__objc_protorefs";
4172 if ( strncmp(sect
->sectname(), "__objc_superrefs", 16) == 0 )
4173 return "__objc_superrefs";
4174 if ( strncmp(sect
->sectname(), "__objc_imageinfo", 16) == 0 )
4175 return "__objc_imageinfo";
4176 if ( strncmp(sect
->sectname(), "__objc_stringobj", 16) == 0 )
4177 return "__objc_stringobj";
4178 if ( strncmp(sect
->sectname(), "__gcc_except_tab", 16) == 0 )
4179 return "__gcc_except_tab";
4181 char* tmp
= new char[17];
4182 strlcpy(tmp
, name
, 17);
4186 template <typename A
>
4187 bool Section
<A
>::readable(const macho_section
<typename
A::P
>* sect
)
4192 template <typename A
>
4193 bool Section
<A
>::writable(const macho_section
<typename
A::P
>* sect
)
4195 // mach-o .o files do not contain segment permissions
4196 // we just know TEXT is special
4197 return ( strcmp(sect
->segname(), "__TEXT") != 0 );
4200 template <typename A
>
4201 bool Section
<A
>::exectuable(const macho_section
<typename
A::P
>* sect
)
4203 // mach-o .o files do not contain segment permissions
4204 // we just know TEXT is special
4205 return ( strcmp(sect
->segname(), "__TEXT") == 0 );
4209 template <typename A
>
4210 ld::Section::Type Section
<A
>::sectionType(const macho_section
<typename
A::P
>* sect
)
4212 switch ( sect
->flags() & SECTION_TYPE
) {
4214 return ld::Section::typeZeroFill
;
4215 case S_CSTRING_LITERALS
:
4216 if ( (strcmp(sect
->sectname(), "__cstring") == 0) && (strcmp(sect
->segname(), "__TEXT") == 0) )
4217 return ld::Section::typeCString
;
4219 return ld::Section::typeNonStdCString
;
4220 case S_4BYTE_LITERALS
:
4221 return ld::Section::typeLiteral4
;
4222 case S_8BYTE_LITERALS
:
4223 return ld::Section::typeLiteral8
;
4224 case S_LITERAL_POINTERS
:
4225 return ld::Section::typeCStringPointer
;
4226 case S_NON_LAZY_SYMBOL_POINTERS
:
4227 return ld::Section::typeNonLazyPointer
;
4228 case S_LAZY_SYMBOL_POINTERS
:
4229 return ld::Section::typeLazyPointer
;
4230 case S_SYMBOL_STUBS
:
4231 return ld::Section::typeStub
;
4232 case S_MOD_INIT_FUNC_POINTERS
:
4233 return ld::Section::typeInitializerPointers
;
4234 case S_MOD_TERM_FUNC_POINTERS
:
4235 return ld::Section::typeTerminatorPointers
;
4237 return ld::Section::typeUnclassified
;
4238 case S_16BYTE_LITERALS
:
4239 return ld::Section::typeLiteral16
;
4242 if ( sect
->flags() & S_ATTR_PURE_INSTRUCTIONS
) {
4243 return ld::Section::typeCode
;
4245 else if ( strcmp(sect
->segname(), "__TEXT") == 0 ) {
4246 if ( strcmp(sect
->sectname(), "__eh_frame") == 0 )
4247 return ld::Section::typeCFI
;
4248 else if ( strcmp(sect
->sectname(), "__ustring") == 0 )
4249 return ld::Section::typeUTF16Strings
;
4250 else if ( strcmp(sect
->sectname(), "__textcoal_nt") == 0 )
4251 return ld::Section::typeCode
;
4252 else if ( strcmp(sect
->sectname(), "__StaticInit") == 0 )
4253 return ld::Section::typeCode
;
4254 else if ( strcmp(sect
->sectname(), "__constructor") == 0 )
4255 return ld::Section::typeInitializerPointers
;
4257 else if ( strcmp(sect
->segname(), "__DATA") == 0 ) {
4258 if ( strcmp(sect
->sectname(), "__cfstring") == 0 )
4259 return ld::Section::typeCFString
;
4260 else if ( strcmp(sect
->sectname(), "__dyld") == 0 )
4261 return ld::Section::typeDyldInfo
;
4262 else if ( strcmp(sect
->sectname(), "__program_vars") == 0 )
4263 return ld::Section::typeDyldInfo
;
4264 else if ( strncmp(sect
->sectname(), "__objc_classrefs", 16) == 0 )
4265 return ld::Section::typeObjCClassRefs
;
4266 else if ( strcmp(sect
->sectname(), "__objc_catlist") == 0 )
4267 return ld::Section::typeObjC2CategoryList
;
4269 else if ( strcmp(sect
->segname(), "__OBJC") == 0 ) {
4270 if ( strcmp(sect
->sectname(), "__class") == 0 )
4271 return ld::Section::typeObjC1Classes
;
4274 case S_THREAD_LOCAL_REGULAR
:
4275 return ld::Section::typeTLVInitialValues
;
4276 case S_THREAD_LOCAL_ZEROFILL
:
4277 return ld::Section::typeTLVZeroFill
;
4278 case S_THREAD_LOCAL_VARIABLES
:
4279 return ld::Section::typeTLVDefs
;
4280 case S_THREAD_LOCAL_VARIABLE_POINTERS
:
4281 return ld::Section::typeTLVPointers
;
4282 case S_THREAD_LOCAL_INIT_FUNCTION_POINTERS
:
4283 return ld::Section::typeTLVInitializerPointers
;
4285 return ld::Section::typeUnclassified
;
4289 template <typename A
>
4290 Atom
<A
>* Section
<A
>::findContentAtomByAddress(pint_t addr
, class Atom
<A
>* start
, class Atom
<A
>* end
)
4292 // do a binary search of atom array
4293 uint32_t atomCount
= end
- start
;
4294 Atom
<A
>* base
= start
;
4295 for (uint32_t n
= atomCount
; n
> 0; n
/= 2) {
4296 Atom
<A
>* pivot
= &base
[n
/2];
4297 pint_t atomStartAddr
= pivot
->_objAddress
;
4298 pint_t atomEndAddr
= atomStartAddr
+ pivot
->_size
;
4299 if ( atomStartAddr
<= addr
) {
4300 // address in normal atom
4301 if (addr
< atomEndAddr
)
4303 // address in "end" label (but not in alias)
4304 if ( (pivot
->_size
== 0) && (addr
== atomEndAddr
) && !pivot
->isAlias() )
4307 if ( addr
>= atomEndAddr
) {
4309 // move base to atom after pivot
4321 template <typename A
>
4322 ld::Atom::Alignment Section
<A
>::alignmentForAddress(pint_t addr
)
4324 const uint32_t sectionAlignment
= this->_machOSection
->align();
4325 uint32_t modulus
= (addr
% (1 << sectionAlignment
));
4326 if ( modulus
> 0xFFFF )
4327 warning("alignment for symbol at address 0x%08llX in %s exceeds 2^16", (uint64_t)addr
, this->file().path());
4328 return ld::Atom::Alignment(sectionAlignment
, modulus
);
4331 template <typename A
>
4332 uint32_t Section
<A
>::sectionNum(class Parser
<A
>& parser
) const
4334 if ( _machOSection
== NULL
)
4337 return 1 + (this->_machOSection
- parser
.firstMachOSection());
4340 // arm does not have zero cost exceptions
4342 uint32_t CFISection
<arm
>::cfiCount(Parser
<arm
>& parser
)
4344 if ( parser
.armUsesZeroCostExceptions() ) {
4345 // create ObjectAddressSpace object for use by libunwind
4346 OAS
oas(*this, (uint8_t*)this->file().fileContent()+this->_machOSection
->offset());
4347 return libunwind::CFI_Parser
<OAS
>::getCFICount(oas
,
4348 this->_machOSection
->addr(), this->_machOSection
->size());
4353 template <typename A
>
4354 uint32_t CFISection
<A
>::cfiCount(Parser
<A
>& parser
)
4356 // create ObjectAddressSpace object for use by libunwind
4357 OAS
oas(*this, (uint8_t*)this->file().fileContent()+this->_machOSection
->offset());
4358 return libunwind::CFI_Parser
<OAS
>::getCFICount(oas
,
4359 this->_machOSection
->addr(), this->_machOSection
->size());
4362 template <typename A
>
4363 void CFISection
<A
>::warnFunc(void* ref
, uint64_t funcAddr
, const char* msg
)
4365 Parser
<A
>* parser
= (Parser
<A
>*)ref
;
4366 if ( ! parser
->warnUnwindConversionProblems() )
4368 if ( funcAddr
!= CFI_INVALID_ADDRESS
) {
4369 // atoms are not constructed yet, so scan symbol table for labels
4370 const char* name
= parser
->scanSymbolTableForAddress(funcAddr
);
4371 warning("could not create compact unwind for %s: %s", name
, msg
);
4374 warning("could not create compact unwind: %s", msg
);
4379 bool CFISection
<x86_64
>::needsRelocating()
4385 bool CFISection
<arm64
>::needsRelocating()
4390 template <typename A
>
4391 bool CFISection
<A
>::needsRelocating()
4397 void CFISection
<x86_64
>::cfiParse(class Parser
<x86_64
>& parser
, uint8_t* buffer
,
4398 libunwind::CFI_Atom_Info
<CFISection
<x86_64
>::OAS
>::CFI_Atom_Info cfiArray
[],
4399 uint32_t& count
, const pint_t cuStarts
[], uint32_t cuCount
)
4401 const uint32_t sectionSize
= this->_machOSection
->size();
4402 // copy __eh_frame data to buffer
4403 memcpy(buffer
, file().fileContent() + this->_machOSection
->offset(), sectionSize
);
4405 // and apply relocations
4406 const macho_relocation_info
<P
>* relocs
= (macho_relocation_info
<P
>*)(file().fileContent() + this->_machOSection
->reloff());
4407 const macho_relocation_info
<P
>* relocsEnd
= &relocs
[this->_machOSection
->nreloc()];
4408 for (const macho_relocation_info
<P
>* reloc
= relocs
; reloc
< relocsEnd
; ++reloc
) {
4410 switch ( reloc
->r_type() ) {
4411 case X86_64_RELOC_SUBTRACTOR
:
4412 value
= 0 - parser
.symbolFromIndex(reloc
->r_symbolnum()).n_value();
4414 if ( reloc
->r_extern() )
4415 value
+= parser
.symbolFromIndex(reloc
->r_symbolnum()).n_value();
4417 case X86_64_RELOC_UNSIGNED
:
4418 value
= parser
.symbolFromIndex(reloc
->r_symbolnum()).n_value();
4420 case X86_64_RELOC_GOT
:
4421 // this is used for the reference to the personality function in CIEs
4422 // store the symbol number of the personality function for later use as a Fixup
4423 value
= reloc
->r_symbolnum();
4426 fprintf(stderr
, "CFISection::cfiParse() unexpected relocation type at r_address=0x%08X\n", reloc
->r_address());
4429 if ( reloc
->r_address() > sectionSize
)
4430 throwf("malformed __eh_frame relocation, offset (0x%08X) is beyond end of section,", reloc
->r_address());
4433 switch ( reloc
->r_length() ) {
4435 p64
= (uint64_t*)&buffer
[reloc
->r_address()];
4436 E::set64(*p64
, value
+ E::get64(*p64
));
4439 p32
= (uint32_t*)&buffer
[reloc
->r_address()];
4440 E::set32(*p32
, value
+ E::get32(*p32
));
4443 fprintf(stderr
, "CFISection::cfiParse() unexpected relocation size at r_address=0x%08X\n", reloc
->r_address());
4448 // create ObjectAddressSpace object for use by libunwind
4449 OAS
oas(*this, buffer
);
4451 // use libuwind to parse __eh_frame data into array of CFI_Atom_Info
4453 msg
= libunwind::DwarfInstructions
<OAS
, libunwind::Registers_x86_64
>::parseCFIs(
4454 oas
, this->_machOSection
->addr(), this->_machOSection
->size(),
4455 cuStarts
, cuCount
, parser
.keepDwarfUnwind(), parser
.forceDwarfConversion(), parser
.neverConvertDwarf(),
4456 cfiArray
, count
, (void*)&parser
, warnFunc
);
4458 throwf("malformed __eh_frame section: %s", msg
);
4462 void CFISection
<x86
>::cfiParse(class Parser
<x86
>& parser
, uint8_t* buffer
,
4463 libunwind::CFI_Atom_Info
<CFISection
<x86
>::OAS
>::CFI_Atom_Info cfiArray
[],
4464 uint32_t& count
, const pint_t cuStarts
[], uint32_t cuCount
)
4466 // create ObjectAddressSpace object for use by libunwind
4467 OAS
oas(*this, (uint8_t*)this->file().fileContent()+this->_machOSection
->offset());
4469 // use libuwind to parse __eh_frame data into array of CFI_Atom_Info
4471 msg
= libunwind::DwarfInstructions
<OAS
, libunwind::Registers_x86
>::parseCFIs(
4472 oas
, this->_machOSection
->addr(), this->_machOSection
->size(),
4473 cuStarts
, cuCount
, parser
.keepDwarfUnwind(), parser
.forceDwarfConversion(), parser
.neverConvertDwarf(),
4474 cfiArray
, count
, (void*)&parser
, warnFunc
);
4476 throwf("malformed __eh_frame section: %s", msg
);
4483 void CFISection
<arm
>::cfiParse(class Parser
<arm
>& parser
, uint8_t* buffer
,
4484 libunwind::CFI_Atom_Info
<CFISection
<arm
>::OAS
>::CFI_Atom_Info cfiArray
[],
4485 uint32_t& count
, const pint_t cuStarts
[], uint32_t cuCount
)
4487 if ( !parser
.armUsesZeroCostExceptions() ) {
4488 // most arm do not use zero cost exceptions
4492 // create ObjectAddressSpace object for use by libunwind
4493 OAS
oas(*this, (uint8_t*)this->file().fileContent()+this->_machOSection
->offset());
4495 // use libuwind to parse __eh_frame data into array of CFI_Atom_Info
4497 msg
= libunwind::DwarfInstructions
<OAS
, libunwind::Registers_arm
>::parseCFIs(
4498 oas
, this->_machOSection
->addr(), this->_machOSection
->size(),
4499 cuStarts
, cuCount
, parser
.keepDwarfUnwind(), parser
.forceDwarfConversion(), parser
.neverConvertDwarf(),
4500 cfiArray
, count
, (void*)&parser
, warnFunc
);
4502 throwf("malformed __eh_frame section: %s", msg
);
4509 void CFISection
<arm64
>::cfiParse(class Parser
<arm64
>& parser
, uint8_t* buffer
,
4510 libunwind::CFI_Atom_Info
<CFISection
<arm64
>::OAS
>::CFI_Atom_Info cfiArray
[],
4511 uint32_t& count
, const pint_t cuStarts
[], uint32_t cuCount
)
4513 // copy __eh_frame data to buffer
4514 const uint32_t sectionSize
= this->_machOSection
->size();
4515 memcpy(buffer
, file().fileContent() + this->_machOSection
->offset(), sectionSize
);
4517 // and apply relocations
4518 const macho_relocation_info
<P
>* relocs
= (macho_relocation_info
<P
>*)(file().fileContent() + this->_machOSection
->reloff());
4519 const macho_relocation_info
<P
>* relocsEnd
= &relocs
[this->_machOSection
->nreloc()];
4520 for (const macho_relocation_info
<P
>* reloc
= relocs
; reloc
< relocsEnd
; ++reloc
) {
4521 uint64_t* p64
= (uint64_t*)&buffer
[reloc
->r_address()];
4522 uint32_t* p32
= (uint32_t*)&buffer
[reloc
->r_address()];
4523 uint32_t addend32
= E::get32(*p32
);
4524 uint64_t addend64
= E::get64(*p64
);
4526 switch ( reloc
->r_type() ) {
4527 case ARM64_RELOC_SUBTRACTOR
:
4528 value
= 0 - parser
.symbolFromIndex(reloc
->r_symbolnum()).n_value();
4530 if ( reloc
->r_extern() )
4531 value
+= parser
.symbolFromIndex(reloc
->r_symbolnum()).n_value();
4533 case ARM64_RELOC_UNSIGNED
:
4534 value
= parser
.symbolFromIndex(reloc
->r_symbolnum()).n_value();
4536 case ARM64_RELOC_POINTER_TO_GOT
:
4537 // this is used for the reference to the personality function in CIEs
4538 // store the symbol number of the personality function for later use as a Fixup
4539 value
= reloc
->r_symbolnum();
4544 fprintf(stderr
, "CFISection::cfiParse() unexpected relocation type at r_address=0x%08X\n", reloc
->r_address());
4547 if ( reloc
->r_address() > sectionSize
)
4548 throwf("malformed __eh_frame relocation, offset (0x%08X) is beyond end of section,", reloc
->r_address());
4549 switch ( reloc
->r_length() ) {
4551 E::set64(*p64
, value
+ addend64
);
4554 E::set32(*p32
, value
+ addend32
);
4557 fprintf(stderr
, "CFISection::cfiParse() unexpected relocation size at r_address=0x%08X\n", reloc
->r_address());
4563 // create ObjectAddressSpace object for use by libunwind
4564 OAS
oas(*this, buffer
);
4566 // use libuwind to parse __eh_frame data into array of CFI_Atom_Info
4568 msg
= libunwind::DwarfInstructions
<OAS
, libunwind::Registers_arm64
>::parseCFIs(
4569 oas
, this->_machOSection
->addr(), this->_machOSection
->size(),
4570 cuStarts
, cuCount
, parser
.keepDwarfUnwind(), parser
.forceDwarfConversion(), parser
.neverConvertDwarf(),
4571 cfiArray
, count
, (void*)&parser
, warnFunc
);
4573 throwf("malformed __eh_frame section: %s", msg
);
4577 template <typename A
>
4578 uint32_t CFISection
<A
>::computeAtomCount(class Parser
<A
>& parser
,
4579 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
4580 const struct Parser
<A
>::CFI_CU_InfoArrays
& cfis
)
4582 return cfis
.cfiCount
;
4587 template <typename A
>
4588 uint32_t CFISection
<A
>::appendAtoms(class Parser
<A
>& parser
, uint8_t* p
,
4589 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
4590 const struct Parser
<A
>::CFI_CU_InfoArrays
& cfis
)
4592 this->_beginAtoms
= (Atom
<A
>*)p
;
4593 // walk CFI_Atom_Info array and create atom for each entry
4594 const CFI_Atom_Info
* start
= &cfis
.cfiArray
[0];
4595 const CFI_Atom_Info
* end
= &cfis
.cfiArray
[cfis
.cfiCount
];
4596 for(const CFI_Atom_Info
* a
=start
; a
< end
; ++a
) {
4597 Atom
<A
>* space
= (Atom
<A
>*)p
;
4598 new (space
) Atom
<A
>(*this, (a
->isCIE
? "CIE" : "FDE"), a
->address
, a
->size
,
4599 ld::Atom::definitionRegular
, ld::Atom::combineNever
, ld::Atom::scopeTranslationUnit
,
4600 ld::Atom::typeCFI
, ld::Atom::symbolTableNotInFinalLinkedImages
,
4601 false, false, false, ld::Atom::Alignment(0));
4602 p
+= sizeof(Atom
<A
>);
4604 this->_endAtoms
= (Atom
<A
>*)p
;
4605 return cfis
.cfiCount
;
4609 template <> bool CFISection
<x86_64
>::bigEndian() { return false; }
4610 template <> bool CFISection
<x86
>::bigEndian() { return false; }
4611 template <> bool CFISection
<arm
>::bigEndian() { return false; }
4612 template <> bool CFISection
<arm64
>::bigEndian() { return false; }
4616 void CFISection
<x86_64
>::addCiePersonalityFixups(class Parser
<x86_64
>& parser
, const CFI_Atom_Info
* cieInfo
)
4618 uint8_t personalityEncoding
= cieInfo
->u
.cieInfo
.personality
.encodingOfTargetAddress
;
4619 if ( personalityEncoding
== 0x9B ) {
4620 // compiler always produces X86_64_RELOC_GOT with addend of 4 to personality function
4621 // CFISection<x86_64>::cfiParse() set targetAddress to be symbolIndex + 4 + addressInCIE
4622 uint32_t symbolIndex
= cieInfo
->u
.cieInfo
.personality
.targetAddress
- 4
4623 - cieInfo
->address
- cieInfo
->u
.cieInfo
.personality
.offsetInCFI
;
4624 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(symbolIndex
);
4625 const char* personalityName
= parser
.nameFromSymbol(sym
);
4627 Atom
<x86_64
>* cieAtom
= this->findAtomByAddress(cieInfo
->address
);
4628 Parser
<x86_64
>::SourceLocation
src(cieAtom
, cieInfo
->u
.cieInfo
.personality
.offsetInCFI
);
4629 parser
.addFixup(src
, ld::Fixup::k1of3
, ld::Fixup::kindSetTargetAddress
, false, personalityName
);
4630 parser
.addFixup(src
, ld::Fixup::k2of3
, ld::Fixup::kindAddAddend
, 4);
4631 parser
.addFixup(src
, ld::Fixup::k3of3
, ld::Fixup::kindStoreX86PCRel32GOT
);
4633 else if ( personalityEncoding
!= 0 ) {
4634 throwf("unsupported address encoding (%02X) of personality function in CIE",
4635 personalityEncoding
);
4640 void CFISection
<x86
>::addCiePersonalityFixups(class Parser
<x86
>& parser
, const CFI_Atom_Info
* cieInfo
)
4642 uint8_t personalityEncoding
= cieInfo
->u
.cieInfo
.personality
.encodingOfTargetAddress
;
4643 if ( (personalityEncoding
== 0x9B) || (personalityEncoding
== 0x90) ) {
4644 uint32_t offsetInCFI
= cieInfo
->u
.cieInfo
.personality
.offsetInCFI
;
4645 uint32_t nlpAddr
= cieInfo
->u
.cieInfo
.personality
.targetAddress
;
4646 Atom
<x86
>* cieAtom
= this->findAtomByAddress(cieInfo
->address
);
4647 Atom
<x86
>* nlpAtom
= parser
.findAtomByAddress(nlpAddr
);
4648 assert(nlpAtom
->contentType() == ld::Atom::typeNonLazyPointer
);
4649 Parser
<x86
>::SourceLocation
src(cieAtom
, cieInfo
->u
.cieInfo
.personality
.offsetInCFI
);
4651 parser
.addFixup(src
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, nlpAtom
);
4652 parser
.addFixup(src
, ld::Fixup::k2of4
, ld::Fixup::kindSubtractTargetAddress
, cieAtom
);
4653 parser
.addFixup(src
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractAddend
, offsetInCFI
);
4654 parser
.addFixup(src
, ld::Fixup::k4of4
, ld::Fixup::kindStoreLittleEndian32
);
4656 else if ( personalityEncoding
!= 0 ) {
4657 throwf("unsupported address encoding (%02X) of personality function in CIE", personalityEncoding
);
4661 #if SUPPORT_ARCH_arm64
4663 void CFISection
<arm64
>::addCiePersonalityFixups(class Parser
<arm64
>& parser
, const CFI_Atom_Info
* cieInfo
)
4665 uint8_t personalityEncoding
= cieInfo
->u
.cieInfo
.personality
.encodingOfTargetAddress
;
4666 if ( personalityEncoding
== 0x9B ) {
4667 // compiler always produces ARM64_RELOC_GOT r_pcrel=1 to personality function
4668 // CFISection<arm64>::cfiParse() set targetAddress to be symbolIndex + addressInCIE
4669 uint32_t symbolIndex
= cieInfo
->u
.cieInfo
.personality
.targetAddress
4670 - cieInfo
->address
- cieInfo
->u
.cieInfo
.personality
.offsetInCFI
;
4671 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(symbolIndex
);
4672 const char* personalityName
= parser
.nameFromSymbol(sym
);
4674 Atom
<arm64
>* cieAtom
= this->findAtomByAddress(cieInfo
->address
);
4675 Parser
<arm64
>::SourceLocation
src(cieAtom
, cieInfo
->u
.cieInfo
.personality
.offsetInCFI
);
4676 parser
.addFixup(src
, ld::Fixup::k1of2
, ld::Fixup::kindSetTargetAddress
, false, personalityName
);
4677 parser
.addFixup(src
, ld::Fixup::k2of2
, ld::Fixup::kindStoreARM64PCRelToGOT
);
4679 else if ( personalityEncoding
!= 0 ) {
4680 throwf("unsupported address encoding (%02X) of personality function in CIE",
4681 personalityEncoding
);
4687 void CFISection
<arm
>::addCiePersonalityFixups(class Parser
<arm
>& parser
, const CFI_Atom_Info
* cieInfo
)
4689 uint8_t personalityEncoding
= cieInfo
->u
.cieInfo
.personality
.encodingOfTargetAddress
;
4690 if ( (personalityEncoding
== 0x9B) || (personalityEncoding
== 0x90) ) {
4691 uint32_t offsetInCFI
= cieInfo
->u
.cieInfo
.personality
.offsetInCFI
;
4692 uint32_t nlpAddr
= cieInfo
->u
.cieInfo
.personality
.targetAddress
;
4693 Atom
<arm
>* cieAtom
= this->findAtomByAddress(cieInfo
->address
);
4694 Atom
<arm
>* nlpAtom
= parser
.findAtomByAddress(nlpAddr
);
4695 assert(nlpAtom
->contentType() == ld::Atom::typeNonLazyPointer
);
4696 Parser
<arm
>::SourceLocation
src(cieAtom
, cieInfo
->u
.cieInfo
.personality
.offsetInCFI
);
4698 parser
.addFixup(src
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, nlpAtom
);
4699 parser
.addFixup(src
, ld::Fixup::k2of4
, ld::Fixup::kindSubtractTargetAddress
, cieAtom
);
4700 parser
.addFixup(src
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractAddend
, offsetInCFI
);
4701 parser
.addFixup(src
, ld::Fixup::k4of4
, ld::Fixup::kindStoreLittleEndian32
);
4703 else if ( personalityEncoding
!= 0 ) {
4704 throwf("unsupported address encoding (%02X) of personality function in CIE", personalityEncoding
);
4710 template <typename A
>
4711 void CFISection
<A
>::addCiePersonalityFixups(class Parser
<A
>& parser
, const CFI_Atom_Info
* cieInfo
)
4713 assert(0 && "addCiePersonalityFixups() not implemented for arch");
4716 template <typename A
>
4717 void CFISection
<A
>::makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
& cfis
)
4719 ld::Fixup::Kind store32
= bigEndian() ? ld::Fixup::kindStoreBigEndian32
: ld::Fixup::kindStoreLittleEndian32
;
4720 ld::Fixup::Kind store64
= bigEndian() ? ld::Fixup::kindStoreBigEndian64
: ld::Fixup::kindStoreLittleEndian64
;
4722 // add all references for FDEs, including implicit group references
4723 const CFI_Atom_Info
* end
= &cfis
.cfiArray
[cfis
.cfiCount
];
4724 for(const CFI_Atom_Info
* p
= &cfis
.cfiArray
[0]; p
< end
; ++p
) {
4726 // add reference to personality function if used
4727 if ( p
->u
.cieInfo
.personality
.targetAddress
!= CFI_INVALID_ADDRESS
) {
4728 this->addCiePersonalityFixups(parser
, p
);
4733 Atom
<A
>* fdeAtom
= this->findAtomByAddress(p
->address
);
4734 // find function Atom
4735 Atom
<A
>* functionAtom
= parser
.findAtomByAddress(p
->u
.fdeInfo
.function
.targetAddress
);
4737 Atom
<A
>* cieAtom
= this->findAtomByAddress(p
->u
.fdeInfo
.cie
.targetAddress
);
4739 Atom
<A
>* lsdaAtom
= NULL
;
4740 if ( p
->u
.fdeInfo
.lsda
.targetAddress
!= CFI_INVALID_ADDRESS
) {
4741 lsdaAtom
= parser
.findAtomByAddress(p
->u
.fdeInfo
.lsda
.targetAddress
);
4743 // add reference from FDE to CIE (always 32-bit pc-rel)
4744 typename Parser
<A
>::SourceLocation
fdeToCieSrc(fdeAtom
, p
->u
.fdeInfo
.cie
.offsetInCFI
);
4745 parser
.addFixup(fdeToCieSrc
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, fdeAtom
);
4746 parser
.addFixup(fdeToCieSrc
, ld::Fixup::k2of4
, ld::Fixup::kindAddAddend
, p
->u
.fdeInfo
.cie
.offsetInCFI
);
4747 parser
.addFixup(fdeToCieSrc
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractTargetAddress
, cieAtom
);
4748 parser
.addFixup(fdeToCieSrc
, ld::Fixup::k4of4
, store32
, cieAtom
);
4750 // add reference from FDE to function
4751 typename Parser
<A
>::SourceLocation
fdeToFuncSrc(fdeAtom
, p
->u
.fdeInfo
.function
.offsetInCFI
);
4752 switch (p
->u
.fdeInfo
.function
.encodingOfTargetAddress
) {
4753 case DW_EH_PE_pcrel
|DW_EH_PE_ptr
:
4754 if ( sizeof(typename
A::P::uint_t
) == 8 ) {
4755 parser
.addFixup(fdeToFuncSrc
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, functionAtom
);
4756 parser
.addFixup(fdeToFuncSrc
, ld::Fixup::k2of4
, ld::Fixup::kindSubtractTargetAddress
, fdeAtom
);
4757 parser
.addFixup(fdeToFuncSrc
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractAddend
, p
->u
.fdeInfo
.function
.offsetInCFI
);
4758 parser
.addFixup(fdeToFuncSrc
, ld::Fixup::k4of4
, store64
);
4761 // else fall into 32-bit case
4762 case DW_EH_PE_pcrel
|DW_EH_PE_sdata4
:
4763 parser
.addFixup(fdeToFuncSrc
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, functionAtom
);
4764 parser
.addFixup(fdeToFuncSrc
, ld::Fixup::k2of4
, ld::Fixup::kindSubtractTargetAddress
, fdeAtom
);
4765 parser
.addFixup(fdeToFuncSrc
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractAddend
, p
->u
.fdeInfo
.function
.offsetInCFI
);
4766 parser
.addFixup(fdeToFuncSrc
, ld::Fixup::k4of4
, store32
);
4769 throw "unsupported encoding in FDE of pointer to function";
4772 // add reference from FDE to LSDA
4773 typename Parser
<A
>::SourceLocation
fdeToLsdaSrc(fdeAtom
, p
->u
.fdeInfo
.lsda
.offsetInCFI
);
4774 if ( lsdaAtom
!= NULL
) {
4775 switch (p
->u
.fdeInfo
.lsda
.encodingOfTargetAddress
) {
4776 case DW_EH_PE_pcrel
|DW_EH_PE_ptr
:
4777 if ( sizeof(typename
A::P::uint_t
) == 8 ) {
4778 parser
.addFixup(fdeToLsdaSrc
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, lsdaAtom
);
4779 parser
.addFixup(fdeToLsdaSrc
, ld::Fixup::k2of4
, ld::Fixup::kindSubtractTargetAddress
, fdeAtom
);
4780 parser
.addFixup(fdeToLsdaSrc
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractAddend
, p
->u
.fdeInfo
.lsda
.offsetInCFI
);
4781 parser
.addFixup(fdeToLsdaSrc
, ld::Fixup::k4of4
, store64
);
4784 // else fall into 32-bit case
4785 case DW_EH_PE_pcrel
|DW_EH_PE_sdata4
:
4786 parser
.addFixup(fdeToLsdaSrc
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, lsdaAtom
);
4787 parser
.addFixup(fdeToLsdaSrc
, ld::Fixup::k2of4
, ld::Fixup::kindSubtractTargetAddress
, fdeAtom
);
4788 parser
.addFixup(fdeToLsdaSrc
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractAddend
, p
->u
.fdeInfo
.lsda
.offsetInCFI
);
4789 parser
.addFixup(fdeToLsdaSrc
, ld::Fixup::k4of4
, store32
);
4792 throw "unsupported encoding in FDE of pointer to LSDA";
4796 // FDE is in group lead by function atom
4797 typename Parser
<A
>::SourceLocation
fdeSrc(functionAtom
,0);
4798 parser
.addFixup(fdeSrc
, ld::Fixup::k1of1
, ld::Fixup::kindNoneGroupSubordinateFDE
, fdeAtom
);
4800 // LSDA is in group lead by function atom
4801 if ( lsdaAtom
!= NULL
) {
4802 parser
.addFixup(fdeSrc
, ld::Fixup::k1of1
, ld::Fixup::kindNoneGroupSubordinateLSDA
, lsdaAtom
);
4811 template <typename A
>
4812 const void* CFISection
<A
>::OAS::mappedAddress(pint_t addr
)
4814 if ( (_ehFrameStartAddr
<= addr
) && (addr
< _ehFrameEndAddr
) )
4815 return &_ehFrameContent
[addr
-_ehFrameStartAddr
];
4817 // requested bytes are not in __eh_frame section
4818 // this can occur when examining the instruction bytes in the __text
4819 File
<A
>& file
= _ehFrameSection
.file();
4820 for (uint32_t i
=0; i
< file
._sectionsArrayCount
; ++i
) {
4821 const macho_section
<typename
A::P
>* sect
= file
._sectionsArray
[i
]->machoSection();
4822 // TentativeDefinitionSection and AbsoluteSymbolSection have no mach-o section
4823 if ( sect
!= NULL
) {
4824 if ( (sect
->addr() <= addr
) && (addr
< (sect
->addr()+sect
->size())) ) {
4825 return file
.fileContent() + sect
->offset() + addr
- sect
->addr();
4829 throwf("__eh_frame parsing problem. Can't find target of reference to address 0x%08llX", (uint64_t)addr
);
4834 template <typename A
>
4835 uint64_t CFISection
<A
>::OAS::getULEB128(pint_t
& logicalAddr
, pint_t end
)
4837 uintptr_t size
= (end
- logicalAddr
);
4838 libunwind::LocalAddressSpace::pint_t laddr
= (libunwind::LocalAddressSpace::pint_t
)mappedAddress(logicalAddr
);
4839 libunwind::LocalAddressSpace::pint_t sladdr
= laddr
;
4840 uint64_t result
= libunwind::LocalAddressSpace::getULEB128(laddr
, laddr
+size
);
4841 logicalAddr
+= (laddr
-sladdr
);
4845 template <typename A
>
4846 int64_t CFISection
<A
>::OAS::getSLEB128(pint_t
& logicalAddr
, pint_t end
)
4848 uintptr_t size
= (end
- logicalAddr
);
4849 libunwind::LocalAddressSpace::pint_t laddr
= (libunwind::LocalAddressSpace::pint_t
)mappedAddress(logicalAddr
);
4850 libunwind::LocalAddressSpace::pint_t sladdr
= laddr
;
4851 int64_t result
= libunwind::LocalAddressSpace::getSLEB128(laddr
, laddr
+size
);
4852 logicalAddr
+= (laddr
-sladdr
);
4856 template <typename A
>
4857 typename
A::P::uint_t CFISection
<A
>::OAS::getEncodedP(pint_t
& addr
, pint_t end
, uint8_t encoding
)
4859 pint_t startAddr
= addr
;
4864 switch (encoding
& 0x0F) {
4866 result
= getP(addr
);
4867 p
+= sizeof(pint_t
);
4870 case DW_EH_PE_uleb128
:
4871 result
= getULEB128(addr
, end
);
4873 case DW_EH_PE_udata2
:
4874 result
= get16(addr
);
4878 case DW_EH_PE_udata4
:
4879 result
= get32(addr
);
4883 case DW_EH_PE_udata8
:
4884 result
= get64(addr
);
4888 case DW_EH_PE_sleb128
:
4889 result
= getSLEB128(addr
, end
);
4891 case DW_EH_PE_sdata2
:
4892 result
= (int16_t)get16(addr
);
4896 case DW_EH_PE_sdata4
:
4897 result
= (int32_t)get32(addr
);
4901 case DW_EH_PE_sdata8
:
4902 result
= get64(addr
);
4907 throwf("ObjectFileAddressSpace<A>::getEncodedP() encoding 0x%08X not supported", encoding
);
4910 // then add relative offset
4911 switch ( encoding
& 0x70 ) {
4912 case DW_EH_PE_absptr
:
4915 case DW_EH_PE_pcrel
:
4916 result
+= startAddr
;
4918 case DW_EH_PE_textrel
:
4919 throw "DW_EH_PE_textrel pointer encoding not supported";
4921 case DW_EH_PE_datarel
:
4922 throw "DW_EH_PE_datarel pointer encoding not supported";
4924 case DW_EH_PE_funcrel
:
4925 throw "DW_EH_PE_funcrel pointer encoding not supported";
4927 case DW_EH_PE_aligned
:
4928 throw "DW_EH_PE_aligned pointer encoding not supported";
4931 throwf("ObjectFileAddressSpace<A>::getEncodedP() encoding 0x%08X not supported", encoding
);
4935 // Note: DW_EH_PE_indirect is only used in CIEs to refernce the personality pointer
4936 // When parsing .o files that pointer contains zero, so we don't to return that.
4937 // Instead we skip the dereference and return the address of the pointer.
4938 // if ( encoding & DW_EH_PE_indirect )
4939 // result = getP(result);
4945 const char* CUSection
<x86_64
>::personalityName(class Parser
<x86_64
>& parser
, const macho_relocation_info
<x86_64::P
>* reloc
)
4947 if ( reloc
->r_extern() ) {
4948 assert((reloc
->r_type() == X86_64_RELOC_UNSIGNED
) && "wrong reloc type on personality column in __compact_unwind section");
4949 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(reloc
->r_symbolnum());
4950 return parser
.nameFromSymbol(sym
);
4953 const pint_t
* content
= (pint_t
*)(this->file().fileContent() + this->_machOSection
->offset() + reloc
->r_address());
4954 pint_t personalityAddr
= *content
;
4955 assert((parser
.sectionForAddress(personalityAddr
)->type() == ld::Section::typeCode
) && "personality column in __compact_unwind section is not pointer to function");
4956 // atoms may not be constructed yet, so scan symbol table for labels
4957 const char* name
= parser
.scanSymbolTableForAddress(personalityAddr
);
4963 const char* CUSection
<x86
>::personalityName(class Parser
<x86
>& parser
, const macho_relocation_info
<x86::P
>* reloc
)
4965 if ( reloc
->r_extern() ) {
4966 assert((reloc
->r_type() == GENERIC_RELOC_VANILLA
) && "wrong reloc type on personality column in __compact_unwind section");
4967 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(reloc
->r_symbolnum());
4968 return parser
.nameFromSymbol(sym
);
4971 // support __LD, __compact_unwind personality entries which are pointer to personality non-lazy pointer
4972 const pint_t
* content
= (pint_t
*)(this->file().fileContent() + this->_machOSection
->offset() + reloc
->r_address());
4973 pint_t nlPointerAddr
= *content
;
4974 Section
<x86
>* nlSection
= parser
.sectionForAddress(nlPointerAddr
);
4975 if ( nlSection
->type() == ld::Section::typeCode
) {
4976 // personality function is defined in this .o file, so this is a direct reference to it
4977 // atoms may not be constructed yet, so scan symbol table for labels
4978 const char* name
= parser
.scanSymbolTableForAddress(nlPointerAddr
);
4982 uint32_t symIndex
= parser
.symbolIndexFromIndirectSectionAddress(nlPointerAddr
, nlSection
->machoSection());
4983 const macho_nlist
<P
>& nlSymbol
= parser
.symbolFromIndex(symIndex
);
4984 return parser
.nameFromSymbol(nlSymbol
);
4989 #if SUPPORT_ARCH_arm64
4991 const char* CUSection
<arm64
>::personalityName(class Parser
<arm64
>& parser
, const macho_relocation_info
<arm64::P
>* reloc
)
4993 if ( reloc
->r_extern() ) {
4994 assert((reloc
->r_type() == ARM64_RELOC_UNSIGNED
) && "wrong reloc type on personality column in __compact_unwind section");
4995 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(reloc
->r_symbolnum());
4996 return parser
.nameFromSymbol(sym
);
4999 const pint_t
* content
= (pint_t
*)(this->file().fileContent() + this->_machOSection
->offset() + reloc
->r_address());
5000 pint_t personalityAddr
= *content
;
5001 Section
<arm64
>* personalitySection
= parser
.sectionForAddress(personalityAddr
);
5002 (void)personalitySection
;
5003 assert((personalitySection
->type() == ld::Section::typeCode
) && "personality column in __compact_unwind section is not pointer to function");
5004 // atoms may not be constructed yet, so scan symbol table for labels
5005 const char* name
= parser
.scanSymbolTableForAddress(personalityAddr
);
5011 #if SUPPORT_ARCH_arm_any
5013 const char* CUSection
<arm
>::personalityName(class Parser
<arm
>& parser
, const macho_relocation_info
<arm::P
>* reloc
)
5015 if ( reloc
->r_extern() ) {
5016 assert((reloc
->r_type() == ARM_RELOC_VANILLA
) && "wrong reloc type on personality column in __compact_unwind section");
5017 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(reloc
->r_symbolnum());
5018 return parser
.nameFromSymbol(sym
);
5021 // support __LD, __compact_unwind personality entries which are pointer to personality non-lazy pointer
5022 const pint_t
* content
= (pint_t
*)(this->file().fileContent() + this->_machOSection
->offset() + reloc
->r_address());
5023 pint_t nlPointerAddr
= *content
;
5024 Section
<arm
>* nlSection
= parser
.sectionForAddress(nlPointerAddr
);
5025 if ( nlSection
->type() == ld::Section::typeCode
) {
5026 // personality function is defined in this .o file, so this is a direct reference to it
5027 // atoms may not be constructed yet, so scan symbol table for labels
5028 const char* name
= parser
.scanSymbolTableForAddress(nlPointerAddr
);
5032 uint32_t symIndex
= parser
.symbolIndexFromIndirectSectionAddress(nlPointerAddr
, nlSection
->machoSection());
5033 const macho_nlist
<P
>& nlSymbol
= parser
.symbolFromIndex(symIndex
);
5034 return parser
.nameFromSymbol(nlSymbol
);
5041 template <typename A
>
5042 const char* CUSection
<A
>::personalityName(class Parser
<A
>& parser
, const macho_relocation_info
<P
>* reloc
)
5048 bool CUSection
<x86
>::encodingMeansUseDwarf(compact_unwind_encoding_t enc
)
5050 return ((enc
& UNWIND_X86_MODE_MASK
) == UNWIND_X86_MODE_DWARF
);
5054 bool CUSection
<x86_64
>::encodingMeansUseDwarf(compact_unwind_encoding_t enc
)
5056 return ((enc
& UNWIND_X86_64_MODE_MASK
) == UNWIND_X86_64_MODE_DWARF
);
5059 #if SUPPORT_ARCH_arm_any
5061 bool CUSection
<arm
>::encodingMeansUseDwarf(compact_unwind_encoding_t enc
)
5063 return ((enc
& UNWIND_ARM_MODE_MASK
) == UNWIND_ARM_MODE_DWARF
);
5067 #if SUPPORT_ARCH_arm64
5069 bool CUSection
<arm64
>::encodingMeansUseDwarf(compact_unwind_encoding_t enc
)
5071 return ((enc
& UNWIND_ARM64_MODE_MASK
) == UNWIND_ARM64_MODE_DWARF
);
5075 template <typename A
>
5076 int CUSection
<A
>::infoSorter(const void* l
, const void* r
)
5078 // sort references by symbol index, then address
5079 const Info
* left
= (Info
*)l
;
5080 const Info
* right
= (Info
*)r
;
5081 if ( left
->functionSymbolIndex
== right
->functionSymbolIndex
)
5082 return (left
->functionStartAddress
- right
->functionStartAddress
);
5084 return (left
->functionSymbolIndex
- right
->functionSymbolIndex
);
5087 template <typename A
>
5088 void CUSection
<A
>::parse(class Parser
<A
>& parser
, uint32_t cnt
, Info array
[])
5090 // walk section content and copy to Info array
5091 const macho_compact_unwind_entry
<P
>* const entries
= (macho_compact_unwind_entry
<P
>*)(this->file().fileContent() + this->_machOSection
->offset());
5092 for (uint32_t i
=0; i
< cnt
; ++i
) {
5093 Info
* info
= &array
[i
];
5094 const macho_compact_unwind_entry
<P
>* entry
= &entries
[i
];
5095 info
->functionStartAddress
= entry
->codeStart();
5096 info
->functionSymbolIndex
= 0xFFFFFFFF;
5097 info
->rangeLength
= entry
->codeLen();
5098 info
->compactUnwindInfo
= entry
->compactUnwindInfo();
5099 info
->personality
= NULL
;
5100 info
->lsdaAddress
= entry
->lsda();
5101 info
->function
= NULL
;
5103 if ( (info
->compactUnwindInfo
& UNWIND_PERSONALITY_MASK
) != 0 )
5104 warning("no bits should be set in UNWIND_PERSONALITY_MASK of compact unwind encoding in __LD,__compact_unwind section");
5105 if ( info
->lsdaAddress
!= 0 ) {
5106 info
->compactUnwindInfo
|= UNWIND_HAS_LSDA
;
5110 // scan relocs, extern relocs are needed for personality references (possibly for function/lsda refs??)
5111 const uint32_t sectionSize
= this->_machOSection
->size();
5112 const macho_relocation_info
<P
>* relocs
= (macho_relocation_info
<P
>*)(this->file().fileContent() + this->_machOSection
->reloff());
5113 const macho_relocation_info
<P
>* relocsEnd
= &relocs
[this->_machOSection
->nreloc()];
5114 for (const macho_relocation_info
<P
>* reloc
= relocs
; reloc
< relocsEnd
; ++reloc
) {
5115 if ( reloc
->r_address() & R_SCATTERED
)
5117 if ( reloc
->r_address() > sectionSize
)
5118 throwf("malformed __compact_unwind relocation, offset (0x%08X) is beyond end of section,", reloc
->r_address());
5119 if ( reloc
->r_extern() ) {
5120 // only expect external relocs on some colummns
5121 if ( (reloc
->r_address() % sizeof(macho_compact_unwind_entry
<P
>)) == macho_compact_unwind_entry
<P
>::personalityFieldOffset() ) {
5122 uint32_t entryIndex
= reloc
->r_address() / sizeof(macho_compact_unwind_entry
<P
>);
5123 array
[entryIndex
].personality
= this->personalityName(parser
, reloc
);
5125 else if ( (reloc
->r_address() % sizeof(macho_compact_unwind_entry
<P
>)) == macho_compact_unwind_entry
<P
>::lsdaFieldOffset() ) {
5126 uint32_t entryIndex
= reloc
->r_address() / sizeof(macho_compact_unwind_entry
<P
>);
5127 const macho_nlist
<P
>& lsdaSym
= parser
.symbolFromIndex(reloc
->r_symbolnum());
5128 if ( (lsdaSym
.n_type() & N_TYPE
) == N_SECT
)
5129 array
[entryIndex
].lsdaAddress
= lsdaSym
.n_value();
5131 warning("unexpected extern relocation to lsda in __compact_unwind section");
5133 else if ( (reloc
->r_address() % sizeof(macho_compact_unwind_entry
<P
>)) == macho_compact_unwind_entry
<P
>::codeStartFieldOffset() ) {
5134 uint32_t entryIndex
= reloc
->r_address() / sizeof(macho_compact_unwind_entry
<P
>);
5135 array
[entryIndex
].functionSymbolIndex
= reloc
->r_symbolnum();
5136 array
[entryIndex
].functionStartAddress
+= parser
.symbolFromIndex(reloc
->r_symbolnum()).n_value();
5139 warning("unexpected extern relocation in __compact_unwind section");
5143 if ( (reloc
->r_address() % sizeof(macho_compact_unwind_entry
<P
>)) == macho_compact_unwind_entry
<P
>::personalityFieldOffset() ) {
5144 uint32_t entryIndex
= reloc
->r_address() / sizeof(macho_compact_unwind_entry
<P
>);
5145 array
[entryIndex
].personality
= this->personalityName(parser
, reloc
);
5150 // sort array by function start address so unwind infos will be contiguous for a given function
5151 ::qsort(array
, cnt
, sizeof(Info
), infoSorter
);
5154 template <typename A
>
5155 uint32_t CUSection
<A
>::count()
5157 const macho_section
<P
>* machoSect
= this->machoSection();
5158 if ( (machoSect
->size() % sizeof(macho_compact_unwind_entry
<P
>)) != 0 )
5159 throw "malformed __LD,__compact_unwind section, bad length";
5161 return machoSect
->size() / sizeof(macho_compact_unwind_entry
<P
>);
5164 template <typename A
>
5165 void CUSection
<A
>::makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
& cus
)
5167 Info
* const arrayStart
= cus
.cuArray
;
5168 Info
* const arrayEnd
= &cus
.cuArray
[cus
.cuCount
];
5169 for (Info
* info
=arrayStart
; info
< arrayEnd
; ++info
) {
5170 // find function atom from address
5171 info
->function
= parser
.findAtomByAddress(info
->functionStartAddress
);
5172 // find lsda atom from address
5173 if ( info
->lsdaAddress
!= 0 ) {
5174 info
->lsda
= parser
.findAtomByAddress(info
->lsdaAddress
);
5175 // add lsda subordinate
5176 typename Parser
<A
>::SourceLocation
src(info
->function
, info
->functionStartAddress
- info
->function
->objectAddress());
5177 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindNoneGroupSubordinateLSDA
, info
->lsda
);
5179 if ( info
->personality
!= NULL
) {
5180 // add personality subordinate
5181 typename Parser
<A
>::SourceLocation
src(info
->function
, info
->functionStartAddress
- info
->function
->objectAddress());
5182 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindNoneGroupSubordinatePersonality
, false, info
->personality
);
5188 template <typename A
>
5189 SymboledSection
<A
>::SymboledSection(Parser
<A
>& parser
, File
<A
>& f
, const macho_section
<typename
A::P
>* s
)
5190 : Section
<A
>(f
, s
), _type(ld::Atom::typeUnclassified
)
5192 switch ( s
->flags() & SECTION_TYPE
) {
5194 _type
= ld::Atom::typeZeroFill
;
5196 case S_MOD_INIT_FUNC_POINTERS
:
5197 _type
= ld::Atom::typeInitializerPointers
;
5199 case S_MOD_TERM_FUNC_POINTERS
:
5200 _type
= ld::Atom::typeTerminatorPointers
;
5202 case S_THREAD_LOCAL_VARIABLES
:
5203 _type
= ld::Atom::typeTLV
;
5205 case S_THREAD_LOCAL_ZEROFILL
:
5206 _type
= ld::Atom::typeTLVZeroFill
;
5208 case S_THREAD_LOCAL_REGULAR
:
5209 _type
= ld::Atom::typeTLVInitialValue
;
5211 case S_THREAD_LOCAL_INIT_FUNCTION_POINTERS
:
5212 _type
= ld::Atom::typeTLVInitializerPointers
;
5215 if ( strncmp(s
->sectname(), "__gcc_except_tab", 16) == 0 )
5216 _type
= ld::Atom::typeLSDA
;
5217 else if ( this->type() == ld::Section::typeInitializerPointers
)
5218 _type
= ld::Atom::typeInitializerPointers
;
5224 template <typename A
>
5225 bool SymboledSection
<A
>::dontDeadStrip()
5228 case ld::Atom::typeInitializerPointers
:
5229 case ld::Atom::typeTerminatorPointers
:
5232 // model an object file without MH_SUBSECTIONS_VIA_SYMBOLS as one in which nothing can be dead stripped
5233 if ( ! this->_file
.canScatterAtoms() )
5236 return Section
<A
>::dontDeadStrip();
5242 template <typename A
>
5243 uint32_t SymboledSection
<A
>::computeAtomCount(class Parser
<A
>& parser
,
5244 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
5245 const struct Parser
<A
>::CFI_CU_InfoArrays
&)
5247 const pint_t startAddr
= this->_machOSection
->addr();
5248 const pint_t endAddr
= startAddr
+ this->_machOSection
->size();
5249 const uint32_t sectNum
= this->sectionNum(parser
);
5254 const macho_nlist
<P
>* sym
;
5255 while ( it
.next(parser
, *this, sectNum
, startAddr
, endAddr
, &addr
, &size
, &sym
) ) {
5258 //fprintf(stderr, "computeAtomCount(%s,%s) => %d\n", this->segmentName(), this->sectionName(), count);
5262 template <typename A
>
5263 uint32_t SymboledSection
<A
>::appendAtoms(class Parser
<A
>& parser
, uint8_t* p
,
5264 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
5265 const struct Parser
<A
>::CFI_CU_InfoArrays
&)
5267 this->_beginAtoms
= (Atom
<A
>*)p
;
5269 //fprintf(stderr, "SymboledSection::appendAtoms() in section %s\n", this->_machOSection->sectname());
5270 const pint_t startAddr
= this->_machOSection
->addr();
5271 const pint_t endAddr
= startAddr
+ this->_machOSection
->size();
5272 const uint32_t sectNum
= this->sectionNum(parser
);
5277 const macho_nlist
<P
>* label
;
5278 while ( it
.next(parser
, *this, sectNum
, startAddr
, endAddr
, &addr
, &size
, &label
) ) {
5279 Atom
<A
>* allocatedSpace
= (Atom
<A
>*)p
;
5280 // is break because of label or CFI?
5281 if ( label
!= NULL
) {
5282 // The size is computed based on the address of the next label (or the end of the section for the last label)
5283 // If there are two labels at the same address, we want them one to be an alias of the other.
5284 // If the label is at the end of a section, it is has zero size, but is not an alias
5285 const bool isAlias
= ( (size
== 0) && (addr
< endAddr
) );
5286 new (allocatedSpace
) Atom
<A
>(*this, parser
, *label
, size
, isAlias
);
5288 this->_hasAliases
= true;
5289 if ( parser
.altEntryFromSymbol(*label
) )
5290 this->_altEntries
.insert(allocatedSpace
);
5293 ld::Atom::SymbolTableInclusion inclusion
= ld::Atom::symbolTableNotIn
;
5294 ld::Atom::ContentType ctype
= this->contentType();
5295 if ( ctype
== ld::Atom::typeLSDA
)
5296 inclusion
= ld::Atom::symbolTableInWithRandomAutoStripLabel
;
5297 new (allocatedSpace
) Atom
<A
>(*this, "anon", addr
, size
, ld::Atom::definitionRegular
, ld::Atom::combineNever
,
5298 ld::Atom::scopeTranslationUnit
, ctype
, inclusion
,
5299 this->dontDeadStrip(), false, false, this->alignmentForAddress(addr
));
5301 p
+= sizeof(Atom
<A
>);
5305 this->_endAtoms
= (Atom
<A
>*)p
;
5311 ld::Atom::SymbolTableInclusion ImplicitSizeSection
<arm64
>::symbolTableInclusion()
5313 return ld::Atom::symbolTableInWithRandomAutoStripLabel
;
5316 template <typename A
>
5317 ld::Atom::SymbolTableInclusion ImplicitSizeSection
<A
>::symbolTableInclusion()
5319 return ld::Atom::symbolTableNotIn
;
5323 template <typename A
>
5324 uint32_t ImplicitSizeSection
<A
>::computeAtomCount(class Parser
<A
>& parser
,
5325 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
5326 const struct Parser
<A
>::CFI_CU_InfoArrays
&)
5329 const macho_section
<P
>* sect
= this->machoSection();
5330 const pint_t startAddr
= sect
->addr();
5331 const pint_t endAddr
= startAddr
+ sect
->size();
5332 for (pint_t addr
= startAddr
; addr
< endAddr
; addr
+= elementSizeAtAddress(addr
) ) {
5333 if ( useElementAt(parser
, it
, addr
) )
5336 if ( it
.fileHasOverlappingSymbols
&& (sect
->size() != 0) && (this->combine(parser
, startAddr
) == ld::Atom::combineByNameAndContent
) ) {
5337 // if there are multiple labels in this section for the same address, then clone them into multi atoms
5338 pint_t prevSymbolAddr
= (pint_t
)(-1);
5339 uint8_t prevSymbolSectNum
= 0;
5340 bool prevIgnore
= false;
5341 for(uint32_t i
=0; i
< it
.sortedSymbolCount
; ++i
) {
5342 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(it
.sortedSymbolIndexes
[i
]);
5343 const pint_t symbolAddr
= sym
.n_value();
5344 const uint8_t symbolSectNum
= sym
.n_sect();
5345 const bool ignore
= this->ignoreLabel(parser
.nameFromSymbol(sym
));
5346 if ( !ignore
&& !prevIgnore
&& (symbolAddr
== prevSymbolAddr
) && (prevSymbolSectNum
== symbolSectNum
) && (symbolSectNum
== this->sectionNum(parser
)) ) {
5349 prevSymbolAddr
= symbolAddr
;
5350 prevSymbolSectNum
= symbolSectNum
;
5351 prevIgnore
= ignore
;
5357 template <typename A
>
5358 uint32_t ImplicitSizeSection
<A
>::appendAtoms(class Parser
<A
>& parser
, uint8_t* p
,
5359 struct Parser
<A
>::LabelAndCFIBreakIterator
& it
,
5360 const struct Parser
<A
>::CFI_CU_InfoArrays
&)
5362 this->_beginAtoms
= (Atom
<A
>*)p
;
5364 const macho_section
<P
>* sect
= this->machoSection();
5365 const pint_t startAddr
= sect
->addr();
5366 const pint_t endAddr
= startAddr
+ sect
->size();
5367 const uint32_t sectNum
= this->sectionNum(parser
);
5368 //fprintf(stderr, "ImplicitSizeSection::appendAtoms() in section %s\n", sect->sectname());
5372 const macho_nlist
<P
>* foundLabel
;
5373 Atom
<A
>* allocatedSpace
;
5374 while ( it
.next(parser
, *this, sectNum
, startAddr
, endAddr
, &foundAddr
, &size
, &foundLabel
) ) {
5375 if ( foundLabel
!= NULL
) {
5377 pint_t labeledAtomSize
= this->elementSizeAtAddress(foundAddr
);
5378 allocatedSpace
= (Atom
<A
>*)p
;
5379 if ( this->ignoreLabel(parser
.nameFromSymbol(*foundLabel
)) ) {
5381 // <rdar://problem/10018737>
5382 // a size of zero means there is another label at same location
5383 // and we are supposed to ignore this label
5387 //fprintf(stderr, " 0x%08llX make annon, size=%lld\n", (uint64_t)foundAddr, (uint64_t)size);
5388 new (allocatedSpace
) Atom
<A
>(*this, this->unlabeledAtomName(parser
, foundAddr
), foundAddr
,
5389 this->elementSizeAtAddress(foundAddr
), this->definition(),
5390 this->combine(parser
, foundAddr
), this->scopeAtAddress(parser
, foundAddr
),
5391 this->contentType(), this->symbolTableInclusion(),
5392 this->dontDeadStrip(), false, false, this->alignmentForAddress(foundAddr
));
5396 // make named atom for label
5397 //fprintf(stderr, " 0x%08llX make labeled\n", (uint64_t)foundAddr);
5398 new (allocatedSpace
) Atom
<A
>(*this, parser
, *foundLabel
, labeledAtomSize
);
5402 p
+= sizeof(Atom
<A
>);
5403 foundAddr
+= labeledAtomSize
;
5404 size
-= labeledAtomSize
;
5407 // some number of anonymous atoms
5408 for (pint_t addr
= foundAddr
; addr
< (foundAddr
+size
); addr
+= elementSizeAtAddress(addr
) ) {
5409 // make anon atoms for area before label
5410 if ( this->useElementAt(parser
, it
, addr
) ) {
5411 //fprintf(stderr, " 0x%08llX make annon, size=%lld\n", (uint64_t)addr, (uint64_t)elementSizeAtAddress(addr));
5412 allocatedSpace
= (Atom
<A
>*)p
;
5413 new (allocatedSpace
) Atom
<A
>(*this, this->unlabeledAtomName(parser
, addr
), addr
, this->elementSizeAtAddress(addr
),
5414 this->definition(), this->combine(parser
, addr
), this->scopeAtAddress(parser
, addr
),
5415 this->contentType(), this->symbolTableInclusion(),
5416 this->dontDeadStrip(), false, false, this->alignmentForAddress(addr
));
5418 p
+= sizeof(Atom
<A
>);
5423 this->_endAtoms
= (Atom
<A
>*)p
;
5428 template <typename A
>
5429 bool Literal4Section
<A
>::ignoreLabel(const char* label
) const
5431 return (label
[0] == 'L') || (label
[0] == 'l');
5434 template <typename A
>
5435 unsigned long Literal4Section
<A
>::contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
5437 const uint32_t* literalContent
= (uint32_t*)atom
->contentPointer();
5438 return *literalContent
;
5441 template <typename A
>
5442 bool Literal4Section
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
5443 const ld::IndirectBindingTable
& ind
) const
5445 assert(this->type() == rhs
.section().type());
5446 const uint32_t* literalContent
= (uint32_t*)atom
->contentPointer();
5448 const Atom
<A
>* rhsAtom
= dynamic_cast<const Atom
<A
>*>(&rhs
);
5449 assert(rhsAtom
!= NULL
);
5450 if ( rhsAtom
!= NULL
) {
5451 const uint32_t* rhsLiteralContent
= (uint32_t*)rhsAtom
->contentPointer();
5452 return (*literalContent
== *rhsLiteralContent
);
5458 template <typename A
>
5459 bool Literal8Section
<A
>::ignoreLabel(const char* label
) const
5461 return (label
[0] == 'L') || (label
[0] == 'l');
5464 template <typename A
>
5465 unsigned long Literal8Section
<A
>::contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
5468 const uint64_t* literalContent
= (uint64_t*)atom
->contentPointer();
5469 return *literalContent
;
5471 unsigned long hash
= 5381;
5472 const uint8_t* byteContent
= atom
->contentPointer();
5473 for (int i
=0; i
< 8; ++i
) {
5474 hash
= hash
* 33 + byteContent
[i
];
5480 template <typename A
>
5481 bool Literal8Section
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
5482 const ld::IndirectBindingTable
& ind
) const
5484 if ( rhs
.section().type() != ld::Section::typeLiteral8
)
5486 assert(this->type() == rhs
.section().type());
5487 const uint64_t* literalContent
= (uint64_t*)atom
->contentPointer();
5489 const Atom
<A
>* rhsAtom
= dynamic_cast<const Atom
<A
>*>(&rhs
);
5490 assert(rhsAtom
!= NULL
);
5491 if ( rhsAtom
!= NULL
) {
5492 const uint64_t* rhsLiteralContent
= (uint64_t*)rhsAtom
->contentPointer();
5493 return (*literalContent
== *rhsLiteralContent
);
5498 template <typename A
>
5499 bool Literal16Section
<A
>::ignoreLabel(const char* label
) const
5501 return (label
[0] == 'L') || (label
[0] == 'l');
5504 template <typename A
>
5505 unsigned long Literal16Section
<A
>::contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
5507 unsigned long hash
= 5381;
5508 const uint8_t* byteContent
= atom
->contentPointer();
5509 for (int i
=0; i
< 16; ++i
) {
5510 hash
= hash
* 33 + byteContent
[i
];
5515 template <typename A
>
5516 bool Literal16Section
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
5517 const ld::IndirectBindingTable
& ind
) const
5519 if ( rhs
.section().type() != ld::Section::typeLiteral16
)
5521 assert(this->type() == rhs
.section().type());
5522 const uint64_t* literalContent
= (uint64_t*)atom
->contentPointer();
5524 const Atom
<A
>* rhsAtom
= dynamic_cast<const Atom
<A
>*>(&rhs
);
5525 assert(rhsAtom
!= NULL
);
5526 if ( rhsAtom
!= NULL
) {
5527 const uint64_t* rhsLiteralContent
= (uint64_t*)rhsAtom
->contentPointer();
5528 return ((literalContent
[0] == rhsLiteralContent
[0]) && (literalContent
[1] == rhsLiteralContent
[1]));
5535 template <typename A
>
5536 typename
A::P::uint_t CStringSection
<A
>::elementSizeAtAddress(pint_t addr
)
5538 const macho_section
<P
>* sect
= this->machoSection();
5539 const char* stringContent
= (char*)(this->file().fileContent() + sect
->offset() + addr
- sect
->addr());
5540 return strlen(stringContent
) + 1;
5543 template <typename A
>
5544 bool CStringSection
<A
>::useElementAt(Parser
<A
>& parser
, struct Parser
<A
>::LabelAndCFIBreakIterator
& it
, pint_t addr
)
5549 template <typename A
>
5550 bool CStringSection
<A
>::ignoreLabel(const char* label
) const
5552 return (label
[0] == 'L') || (label
[0] == 'l');
5556 template <typename A
>
5557 Atom
<A
>* CStringSection
<A
>::findAtomByAddress(pint_t addr
)
5559 Atom
<A
>* result
= this->findContentAtomByAddress(addr
, this->_beginAtoms
, this->_endAtoms
);
5563 template <typename A
>
5564 unsigned long CStringSection
<A
>::contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
5566 unsigned long hash
= 5381;
5567 const char* stringContent
= (char*)atom
->contentPointer();
5568 for (const char* s
= stringContent
; *s
!= '\0'; ++s
) {
5569 hash
= hash
* 33 + *s
;
5575 template <typename A
>
5576 bool CStringSection
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
5577 const ld::IndirectBindingTable
& ind
) const
5579 if ( rhs
.section().type() != ld::Section::typeCString
)
5581 assert(this->type() == rhs
.section().type());
5582 assert(strcmp(this->sectionName(), rhs
.section().sectionName())== 0);
5583 assert(strcmp(this->segmentName(), rhs
.section().segmentName())== 0);
5584 const char* stringContent
= (char*)atom
->contentPointer();
5586 const Atom
<A
>* rhsAtom
= dynamic_cast<const Atom
<A
>*>(&rhs
);
5587 assert(rhsAtom
!= NULL
);
5588 if ( rhsAtom
!= NULL
) {
5589 if ( atom
->_size
!= rhsAtom
->_size
)
5591 const char* rhsStringContent
= (char*)rhsAtom
->contentPointer();
5592 return (strcmp(stringContent
, rhsStringContent
) == 0);
5599 ld::Fixup::Kind NonLazyPointerSection
<x86
>::fixupKind()
5601 return ld::Fixup::kindStoreLittleEndian32
;
5605 ld::Fixup::Kind NonLazyPointerSection
<arm
>::fixupKind()
5607 return ld::Fixup::kindStoreLittleEndian32
;
5611 ld::Fixup::Kind NonLazyPointerSection
<arm64
>::fixupKind()
5613 return ld::Fixup::kindStoreLittleEndian64
;
5618 void NonLazyPointerSection
<x86_64
>::makeFixups(class Parser
<x86_64
>& parser
, const struct Parser
<x86_64
>::CFI_CU_InfoArrays
&)
5620 assert(0 && "x86_64 should not have non-lazy-pointer sections in .o files");
5623 template <typename A
>
5624 void NonLazyPointerSection
<A
>::makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
&)
5626 // add references for each NLP atom based on indirect symbol table
5627 const macho_section
<P
>* sect
= this->machoSection();
5628 const pint_t endAddr
= sect
->addr() + sect
->size();
5629 for( pint_t addr
= sect
->addr(); addr
< endAddr
; addr
+= sizeof(pint_t
)) {
5630 typename Parser
<A
>::SourceLocation src
;
5631 typename Parser
<A
>::TargetDesc target
;
5632 src
.atom
= this->findAtomByAddress(addr
);
5633 src
.offsetInAtom
= 0;
5634 uint32_t symIndex
= parser
.symbolIndexFromIndirectSectionAddress(addr
, sect
);
5637 target
.weakImport
= false;
5639 if ( symIndex
== INDIRECT_SYMBOL_LOCAL
) {
5640 // use direct reference for local symbols
5641 const pint_t
* nlpContent
= (pint_t
*)(this->file().fileContent() + sect
->offset() + addr
- sect
->addr());
5642 pint_t targetAddr
= P::getP(*nlpContent
);
5643 target
.atom
= parser
.findAtomByAddress(targetAddr
);
5644 target
.weakImport
= false;
5645 target
.addend
= (targetAddr
- target
.atom
->objectAddress());
5646 // <rdar://problem/8385011> if pointer to thumb function, mask of thumb bit (not an addend of +1)
5647 if ( target
.atom
->isThumb() )
5648 target
.addend
&= (-2);
5649 assert(src
.atom
->combine() == ld::Atom::combineNever
);
5652 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(symIndex
);
5653 // use direct reference for local symbols
5654 if ( ((sym
.n_type() & N_TYPE
) == N_SECT
) && ((sym
.n_type() & N_EXT
) == 0) ) {
5655 parser
.findTargetFromAddressAndSectionNum(sym
.n_value(), sym
.n_sect(), target
);
5656 assert(src
.atom
->combine() == ld::Atom::combineNever
);
5659 target
.name
= parser
.nameFromSymbol(sym
);
5660 target
.weakImport
= parser
.weakImportFromSymbol(sym
);
5661 assert(src
.atom
->combine() == ld::Atom::combineByNameAndReferences
);
5664 parser
.addFixups(src
, this->fixupKind(), target
);
5668 template <typename A
>
5669 ld::Atom::Combine NonLazyPointerSection
<A
>::combine(Parser
<A
>& parser
, pint_t addr
)
5671 const macho_section
<P
>* sect
= this->machoSection();
5672 uint32_t symIndex
= parser
.symbolIndexFromIndirectSectionAddress(addr
, sect
);
5673 if ( symIndex
== INDIRECT_SYMBOL_LOCAL
)
5674 return ld::Atom::combineNever
;
5676 // don't coalesce non-lazy-pointers to local symbols
5677 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(symIndex
);
5678 if ( ((sym
.n_type() & N_TYPE
) == N_SECT
) && ((sym
.n_type() & N_EXT
) == 0) )
5679 return ld::Atom::combineNever
;
5681 return ld::Atom::combineByNameAndReferences
;
5684 template <typename A
>
5685 const char* NonLazyPointerSection
<A
>::targetName(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
)
5687 assert(atom
->combine() == ld::Atom::combineByNameAndReferences
);
5688 assert(atom
->fixupCount() == 1);
5689 ld::Fixup::iterator fit
= atom
->fixupsBegin();
5690 const char* name
= NULL
;
5691 switch ( fit
->binding
) {
5692 case ld::Fixup::bindingByNameUnbound
:
5695 case ld::Fixup::bindingByContentBound
:
5696 name
= fit
->u
.target
->name();
5698 case ld::Fixup::bindingsIndirectlyBound
:
5699 name
= ind
.indirectName(fit
->u
.bindingIndex
);
5704 assert(name
!= NULL
);
5708 template <typename A
>
5709 unsigned long NonLazyPointerSection
<A
>::contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
5711 assert(atom
->combine() == ld::Atom::combineByNameAndReferences
);
5712 unsigned long hash
= 9508;
5713 for (const char* s
= this->targetName(atom
, ind
); *s
!= '\0'; ++s
) {
5714 hash
= hash
* 33 + *s
;
5719 template <typename A
>
5720 bool NonLazyPointerSection
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
5721 const ld::IndirectBindingTable
& indirectBindingTable
) const
5723 if ( rhs
.section().type() != ld::Section::typeNonLazyPointer
)
5725 assert(this->type() == rhs
.section().type());
5726 // there can be many non-lazy pointer in different section names
5727 // we only want to coalesce in same section name
5728 if ( *this != rhs
.section() )
5730 const Atom
<A
>* rhsAtom
= dynamic_cast<const Atom
<A
>*>(&rhs
);
5731 assert(rhsAtom
!= NULL
);
5732 const char* thisName
= this->targetName(atom
, indirectBindingTable
);
5733 const char* rhsName
= this->targetName(rhsAtom
, indirectBindingTable
);
5734 return (strcmp(thisName
, rhsName
) == 0);
5737 template <typename A
>
5738 ld::Atom::Scope NonLazyPointerSection
<A
>::scopeAtAddress(Parser
<A
>& parser
, pint_t addr
)
5740 const macho_section
<P
>* sect
= this->machoSection();
5741 uint32_t symIndex
= parser
.symbolIndexFromIndirectSectionAddress(addr
, sect
);
5742 if ( symIndex
== INDIRECT_SYMBOL_LOCAL
)
5743 return ld::Atom::scopeTranslationUnit
;
5745 return ld::Atom::scopeLinkageUnit
;
5750 template <typename A
>
5751 ld::Atom::Combine TLVPointerSection
<A
>::combine(Parser
<A
>& parser
, pint_t addr
)
5753 return ld::Atom::combineByNameAndReferences
;
5757 template <typename A
>
5758 const char* TLVPointerSection
<A
>::targetName(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
, bool* isStatic
)
5760 assert(atom
->combine() == ld::Atom::combineByNameAndReferences
);
5761 assert(atom
->fixupCount() == 1);
5763 ld::Fixup::iterator fit
= atom
->fixupsBegin();
5764 const char* name
= NULL
;
5765 switch ( fit
->binding
) {
5766 case ld::Fixup::bindingByNameUnbound
:
5769 case ld::Fixup::bindingByContentBound
:
5770 name
= fit
->u
.target
->name();
5772 case ld::Fixup::bindingsIndirectlyBound
:
5773 name
= ind
.indirectName(fit
->u
.bindingIndex
);
5775 case ld::Fixup::bindingDirectlyBound
:
5776 name
= fit
->u
.target
->name();
5777 *isStatic
= (fit
->u
.target
->scope() == ld::Atom::scopeTranslationUnit
);
5782 assert(name
!= NULL
);
5786 template <typename A
>
5787 unsigned long TLVPointerSection
<A
>::contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
5789 assert(atom
->combine() == ld::Atom::combineByNameAndReferences
);
5790 unsigned long hash
= 9508;
5792 for (const char* s
= this->targetName(atom
, ind
, &isStatic
); *s
!= '\0'; ++s
) {
5793 hash
= hash
* 33 + *s
;
5798 template <typename A
>
5799 bool TLVPointerSection
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
5800 const ld::IndirectBindingTable
& indirectBindingTable
) const
5802 if ( rhs
.section().type() != ld::Section::typeTLVPointers
)
5804 assert(this->type() == rhs
.section().type());
5805 const Atom
<A
>* rhsAtom
= dynamic_cast<const Atom
<A
>*>(&rhs
);
5806 assert(rhsAtom
!= NULL
);
5809 const char* thisName
= this->targetName(atom
, indirectBindingTable
, &thisIsStatic
);
5810 const char* rhsName
= this->targetName(rhsAtom
, indirectBindingTable
, &rhsIsStatic
);
5811 return !thisIsStatic
&& !rhsIsStatic
&& (strcmp(thisName
, rhsName
) == 0);
5815 template <typename A
>
5816 const uint8_t* CFStringSection
<A
>::targetContent(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
,
5817 ContentType
* ct
, unsigned int* count
)
5819 *ct
= contentUnknown
;
5820 for (ld::Fixup::iterator fit
=atom
->fixupsBegin(), end
=atom
->fixupsEnd(); fit
!= end
; ++fit
) {
5821 const ld::Atom
* targetAtom
= NULL
;
5822 switch ( fit
->binding
) {
5823 case ld::Fixup::bindingByNameUnbound
:
5824 // ignore reference to ___CFConstantStringClassReference
5825 // we are just looking for reference to backing string data
5826 assert(fit
->offsetInAtom
== 0);
5827 assert(strcmp(fit
->u
.name
, "___CFConstantStringClassReference") == 0);
5829 case ld::Fixup::bindingDirectlyBound
:
5830 case ld::Fixup::bindingByContentBound
:
5831 targetAtom
= fit
->u
.target
;
5833 case ld::Fixup::bindingsIndirectlyBound
:
5834 targetAtom
= ind
.indirectAtom(fit
->u
.bindingIndex
);
5837 assert(0 && "bad binding type");
5839 assert(targetAtom
!= NULL
);
5840 const Atom
<A
>* target
= dynamic_cast<const Atom
<A
>*>(targetAtom
);
5841 if ( targetAtom
->section().type() == ld::Section::typeCString
) {
5843 *count
= targetAtom
->size();
5845 else if ( targetAtom
->section().type() == ld::Section::typeUTF16Strings
) {
5847 *count
= (targetAtom
->size()+1)/2; // round up incase of buggy compiler that has only one trailing zero byte
5850 *ct
= contentUnknown
;
5854 return target
->contentPointer();
5860 template <typename A
>
5861 unsigned long CFStringSection
<A
>::contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
5863 // base hash of CFString on hash of cstring it wraps
5866 unsigned int charCount
;
5867 const uint8_t* content
= this->targetContent(atom
, ind
, &cType
, &charCount
);
5871 for (const char* s
= (char*)content
; *s
!= '\0'; ++s
) {
5872 hash
= hash
* 33 + *s
;
5877 --charCount
; // don't add last 0x0000 to hash because some buggy compilers only have trailing single byte
5878 for (const uint16_t* s
= (uint16_t*)content
; charCount
> 0; ++s
, --charCount
) {
5879 hash
= hash
* 1025 + *s
;
5882 case contentUnknown
:
5883 // <rdar://problem/14134211> For malformed CFStrings, hash to address of atom so they have unique hashes
5884 return ULONG_MAX
- (unsigned long)(atom
);
5890 template <typename A
>
5891 bool CFStringSection
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
5892 const ld::IndirectBindingTable
& indirectBindingTable
) const
5896 if ( rhs
.section().type() != ld::Section::typeCFString
)
5898 assert(this->type() == rhs
.section().type());
5899 assert(strcmp(this->sectionName(), "__cfstring") == 0);
5901 ContentType thisType
;
5902 unsigned int charCount
;
5903 const uint8_t* cstringContent
= this->targetContent(atom
, indirectBindingTable
, &thisType
, &charCount
);
5904 ContentType rhsType
;
5905 const Atom
<A
>* rhsAtom
= dynamic_cast<const Atom
<A
>*>(&rhs
);
5906 assert(rhsAtom
!= NULL
);
5907 unsigned int rhsCharCount
;
5908 const uint8_t* rhsStringContent
= this->targetContent(rhsAtom
, indirectBindingTable
, &rhsType
, &rhsCharCount
);
5910 if ( thisType
!= rhsType
)
5913 if ( thisType
== contentUnknown
)
5916 if ( rhsType
== contentUnknown
)
5919 // no need to compare content of pointers are already the same
5920 if ( cstringContent
== rhsStringContent
)
5923 // no need to compare content if size is different
5924 if ( charCount
!= rhsCharCount
)
5927 switch ( thisType
) {
5929 return (strcmp((char*)cstringContent
, (char*)rhsStringContent
) == 0);
5932 const uint16_t* cstringContent16
= (uint16_t*)cstringContent
;
5933 const uint16_t* rhsStringContent16
= (uint16_t*)rhsStringContent
;
5934 for (unsigned int i
= 0; i
< charCount
; ++i
) {
5935 if ( cstringContent16
[i
] != rhsStringContent16
[i
] )
5940 case contentUnknown
:
5947 template <typename A
>
5948 typename
A::P::uint_t ObjC1ClassSection
<A
>::elementSizeAtAddress(pint_t addr
)
5950 // nominal size for each class is 48 bytes, but sometimes the compiler
5951 // over aligns and there is padding after class data
5952 const macho_section
<P
>* sct
= this->machoSection();
5953 uint32_t align
= 1 << sct
->align();
5954 uint32_t size
= ((12 * sizeof(pint_t
)) + align
-1) & (-align
);
5958 template <typename A
>
5959 const char* ObjC1ClassSection
<A
>::unlabeledAtomName(Parser
<A
>& parser
, pint_t addr
)
5961 // 8-bytes into class object is pointer to class name
5962 const macho_section
<P
>* sct
= this->machoSection();
5963 uint32_t classObjcFileOffset
= sct
->offset() - sct
->addr() + addr
;
5964 const uint8_t* mappedFileContent
= this->file().fileContent();
5965 pint_t nameAddr
= P::getP(*((pint_t
*)(mappedFileContent
+classObjcFileOffset
+2*sizeof(pint_t
))));
5967 // find section containing string address to get string bytes
5968 const macho_section
<P
>* const sections
= parser
.firstMachOSection();
5969 const uint32_t sectionCount
= parser
.machOSectionCount();
5970 for (uint32_t i
=0; i
< sectionCount
; ++i
) {
5971 const macho_section
<P
>* aSect
= §ions
[i
];
5972 if ( (aSect
->addr() <= nameAddr
) && (nameAddr
< (aSect
->addr()+aSect
->size())) ) {
5973 assert((aSect
->flags() & SECTION_TYPE
) == S_CSTRING_LITERALS
);
5974 uint32_t nameFileOffset
= aSect
->offset() - aSect
->addr() + nameAddr
;
5975 const char* name
= (char*)mappedFileContent
+ nameFileOffset
;
5976 // spin through symbol table to find absolute symbol corresponding to this class
5977 for (uint32_t s
=0; s
< parser
.symbolCount(); ++s
) {
5978 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(s
);
5979 if ( (sym
.n_type() & N_TYPE
) != N_ABS
)
5981 const char* absName
= parser
.nameFromSymbol(sym
);
5982 if ( strncmp(absName
, ".objc_class_name_", 17) == 0 ) {
5983 if ( strcmp(&absName
[17], name
) == 0 )
5987 assert(0 && "obj class name not found in symbol table");
5990 assert(0 && "obj class name not found");
5991 return "unknown objc class";
5995 template <typename A
>
5996 const char* ObjC2ClassRefsSection
<A
>::targetClassName(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
5998 assert(atom
->fixupCount() == 1);
5999 ld::Fixup::iterator fit
= atom
->fixupsBegin();
6000 const char* className
= NULL
;
6001 switch ( fit
->binding
) {
6002 case ld::Fixup::bindingByNameUnbound
:
6003 className
= fit
->u
.name
;
6005 case ld::Fixup::bindingDirectlyBound
:
6006 case ld::Fixup::bindingByContentBound
:
6007 className
= fit
->u
.target
->name();
6009 case ld::Fixup::bindingsIndirectlyBound
:
6010 className
= ind
.indirectName(fit
->u
.bindingIndex
);
6013 assert(0 && "unsupported binding in objc2 class ref section");
6015 assert(className
!= NULL
);
6020 template <typename A
>
6021 unsigned long ObjC2ClassRefsSection
<A
>::contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
6023 unsigned long hash
= 978;
6024 for (const char* s
= targetClassName(atom
, ind
); *s
!= '\0'; ++s
) {
6025 hash
= hash
* 33 + *s
;
6030 template <typename A
>
6031 bool ObjC2ClassRefsSection
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
6032 const ld::IndirectBindingTable
& indirectBindingTable
) const
6034 assert(this->type() == rhs
.section().type());
6035 const Atom
<A
>* rhsAtom
= dynamic_cast<const Atom
<A
>*>(&rhs
);
6036 assert(rhsAtom
!= NULL
);
6037 const char* thisClassName
= targetClassName(atom
, indirectBindingTable
);
6038 const char* rhsClassName
= targetClassName(rhsAtom
, indirectBindingTable
);
6039 return (strcmp(thisClassName
, rhsClassName
) == 0);
6043 template <typename A
>
6044 const char* Objc1ClassReferences
<A
>::targetCString(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
6046 assert(atom
->fixupCount() == 2);
6047 ld::Fixup::iterator fit
= atom
->fixupsBegin();
6048 if ( fit
->kind
== ld::Fixup::kindSetTargetAddress
)
6050 const ld::Atom
* targetAtom
= NULL
;
6051 switch ( fit
->binding
) {
6052 case ld::Fixup::bindingByContentBound
:
6053 targetAtom
= fit
->u
.target
;
6055 case ld::Fixup::bindingsIndirectlyBound
:
6056 targetAtom
= ind
.indirectAtom(fit
->u
.bindingIndex
);
6057 if ( targetAtom
== NULL
) {
6058 fprintf(stderr
, "missing target named %s\n", ind
.indirectName(fit
->u
.bindingIndex
));
6064 assert(targetAtom
!= NULL
);
6065 const Atom
<A
>* target
= dynamic_cast<const Atom
<A
>*>(targetAtom
);
6066 assert(target
!= NULL
);
6067 return (char*)target
->contentPointer();
6071 template <typename A
>
6072 const char* PointerToCStringSection
<A
>::targetCString(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
6074 assert(atom
->fixupCount() == 1);
6075 ld::Fixup::iterator fit
= atom
->fixupsBegin();
6076 const ld::Atom
* targetAtom
= NULL
;
6077 switch ( fit
->binding
) {
6078 case ld::Fixup::bindingByContentBound
:
6079 targetAtom
= fit
->u
.target
;
6081 case ld::Fixup::bindingsIndirectlyBound
:
6082 targetAtom
= ind
.indirectAtom(fit
->u
.bindingIndex
);
6084 case ld::Fixup::bindingDirectlyBound
:
6085 targetAtom
= fit
->u
.target
;
6088 assert(0 && "unsupported reference to selector");
6090 assert(targetAtom
!= NULL
);
6091 const Atom
<A
>* target
= dynamic_cast<const Atom
<A
>*>(targetAtom
);
6092 assert(target
!= NULL
);
6093 assert(target
->contentType() == ld::Atom::typeCString
);
6094 return (char*)target
->contentPointer();
6097 template <typename A
>
6098 unsigned long PointerToCStringSection
<A
>::contentHash(const class Atom
<A
>* atom
,
6099 const ld::IndirectBindingTable
& indirectBindingTable
) const
6101 // make hash from section name and target cstring name
6102 unsigned long hash
= 123;
6103 for (const char* s
= this->sectionName(); *s
!= '\0'; ++s
) {
6104 hash
= hash
* 33 + *s
;
6106 for (const char* s
= this->targetCString(atom
, indirectBindingTable
); *s
!= '\0'; ++s
) {
6107 hash
= hash
* 33 + *s
;
6112 template <typename A
>
6113 bool PointerToCStringSection
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
6114 const ld::IndirectBindingTable
& indirectBindingTable
) const
6116 assert(this->type() == rhs
.section().type());
6117 // there can be pointers-to-cstrings in different section names
6118 // we only want to coalesce in same section name
6119 if ( *this != rhs
.section() )
6122 // get string content for this
6123 const char* cstringContent
= this->targetCString(atom
, indirectBindingTable
);
6124 const Atom
<A
>* rhsAtom
= dynamic_cast<const Atom
<A
>*>(&rhs
);
6125 assert(rhsAtom
!= NULL
);
6126 const char* rhsCstringContent
= this->targetCString(rhsAtom
, indirectBindingTable
);
6128 assert(cstringContent
!= NULL
);
6129 assert(rhsCstringContent
!= NULL
);
6130 return (strcmp(cstringContent
, rhsCstringContent
) == 0);
6135 template <typename A
>
6136 unsigned long UTF16StringSection
<A
>::contentHash(const class Atom
<A
>* atom
, const ld::IndirectBindingTable
& ind
) const
6138 unsigned long hash
= 5381;
6139 const uint16_t* stringContent
= (uint16_t*)atom
->contentPointer();
6140 // some buggy compilers end utf16 data with single byte, so don't use last word in hash computation
6141 unsigned int count
= (atom
->size()/2) - 1;
6142 for (const uint16_t* s
= stringContent
; count
> 0; ++s
, --count
) {
6143 hash
= hash
* 33 + *s
;
6148 template <typename A
>
6149 bool UTF16StringSection
<A
>::canCoalesceWith(const class Atom
<A
>* atom
, const ld::Atom
& rhs
,
6150 const ld::IndirectBindingTable
& ind
) const
6152 if ( rhs
.section().type() != ld::Section::typeUTF16Strings
)
6165 uint32_t Section
<x86_64
>::x86_64PcRelOffset(uint8_t r_type
)
6168 case X86_64_RELOC_SIGNED
:
6170 case X86_64_RELOC_SIGNED_1
:
6172 case X86_64_RELOC_SIGNED_2
:
6174 case X86_64_RELOC_SIGNED_4
:
6182 bool Section
<x86_64
>::addRelocFixup(class Parser
<x86_64
>& parser
, const macho_relocation_info
<P
>* reloc
)
6184 const macho_section
<P
>* sect
= this->machoSection();
6185 uint64_t srcAddr
= sect
->addr() + reloc
->r_address();
6186 Parser
<x86_64
>::SourceLocation src
;
6187 Parser
<x86_64
>::TargetDesc target
;
6188 Parser
<x86_64
>::TargetDesc toTarget
;
6189 src
.atom
= this->findAtomByAddress(srcAddr
);
6190 src
.offsetInAtom
= srcAddr
- src
.atom
->_objAddress
;
6191 const uint8_t* fixUpPtr
= file().fileContent() + sect
->offset() + reloc
->r_address();
6192 uint64_t contentValue
= 0;
6193 const macho_relocation_info
<x86_64::P
>* nextReloc
= &reloc
[1];
6194 bool result
= false;
6195 bool useDirectBinding
;
6196 switch ( reloc
->r_length() ) {
6198 contentValue
= *fixUpPtr
;
6201 contentValue
= (int64_t)(int16_t)E::get16(*((uint16_t*)fixUpPtr
));
6204 contentValue
= (int64_t)(int32_t)E::get32(*((uint32_t*)fixUpPtr
));
6207 contentValue
= E::get64(*((uint64_t*)fixUpPtr
));
6212 target
.weakImport
= false;
6214 if ( reloc
->r_extern() ) {
6215 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(reloc
->r_symbolnum());
6216 // use direct reference for local symbols
6217 if ( ((sym
.n_type() & N_TYPE
) == N_SECT
) && (((sym
.n_type() & N_EXT
) == 0) || (parser
.nameFromSymbol(sym
)[0] == 'L')) ) {
6218 parser
.findTargetFromAddressAndSectionNum(sym
.n_value(), sym
.n_sect(), target
);
6219 target
.addend
+= contentValue
;
6222 target
.name
= parser
.nameFromSymbol(sym
);
6223 target
.weakImport
= parser
.weakImportFromSymbol(sym
);
6224 target
.addend
= contentValue
;
6226 // cfstrings should always use direct reference to backing store
6227 if ( (this->type() == ld::Section::typeCFString
) && (src
.offsetInAtom
!= 0) ) {
6228 parser
.findTargetFromAddressAndSectionNum(sym
.n_value(), sym
.n_sect(), target
);
6229 target
.addend
= contentValue
;
6233 if ( reloc
->r_pcrel() )
6234 contentValue
+= srcAddr
+ x86_64PcRelOffset(reloc
->r_type());
6235 parser
.findTargetFromAddressAndSectionNum(contentValue
, reloc
->r_symbolnum(), target
);
6237 switch ( reloc
->r_type() ) {
6238 case X86_64_RELOC_UNSIGNED
:
6239 if ( reloc
->r_pcrel() )
6240 throw "pcrel and X86_64_RELOC_UNSIGNED not supported";
6241 switch ( reloc
->r_length() ) {
6244 throw "length < 2 and X86_64_RELOC_UNSIGNED not supported";
6246 parser
.addFixups(src
, ld::Fixup::kindStoreLittleEndian32
, target
);
6249 parser
.addFixups(src
, ld::Fixup::kindStoreLittleEndian64
, target
);
6253 case X86_64_RELOC_SIGNED
:
6254 case X86_64_RELOC_SIGNED_1
:
6255 case X86_64_RELOC_SIGNED_2
:
6256 case X86_64_RELOC_SIGNED_4
:
6257 if ( ! reloc
->r_pcrel() )
6258 throw "not pcrel and X86_64_RELOC_SIGNED* not supported";
6259 if ( reloc
->r_length() != 2 )
6260 throw "length != 2 and X86_64_RELOC_SIGNED* not supported";
6261 switch ( reloc
->r_type() ) {
6262 case X86_64_RELOC_SIGNED
:
6263 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel32
, target
);
6265 case X86_64_RELOC_SIGNED_1
:
6266 if ( reloc
->r_extern() )
6268 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel32_1
, target
);
6270 case X86_64_RELOC_SIGNED_2
:
6271 if ( reloc
->r_extern() )
6273 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel32_2
, target
);
6275 case X86_64_RELOC_SIGNED_4
:
6276 if ( reloc
->r_extern() )
6278 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel32_4
, target
);
6282 case X86_64_RELOC_BRANCH
:
6283 if ( ! reloc
->r_pcrel() )
6284 throw "not pcrel and X86_64_RELOC_BRANCH not supported";
6285 switch ( reloc
->r_length() ) {
6287 if ( (target
.name
!= NULL
) && (strncmp(target
.name
, "___dtrace_probe$", 16) == 0) ) {
6288 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindStoreX86DtraceCallSiteNop
, false, target
.name
);
6289 parser
.addDtraceExtraInfos(src
, &target
.name
[16]);
6291 else if ( (target
.name
!= NULL
) && (strncmp(target
.name
, "___dtrace_isenabled$", 20) == 0) ) {
6292 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindStoreX86DtraceIsEnableSiteClear
, false, target
.name
);
6293 parser
.addDtraceExtraInfos(src
, &target
.name
[20]);
6296 parser
.addFixups(src
, ld::Fixup::kindStoreX86BranchPCRel32
, target
);
6300 parser
.addFixups(src
, ld::Fixup::kindStoreX86BranchPCRel8
, target
);
6303 throwf("length=%d and X86_64_RELOC_BRANCH not supported", reloc
->r_length());
6306 case X86_64_RELOC_GOT
:
6307 if ( ! reloc
->r_extern() )
6308 throw "not extern and X86_64_RELOC_GOT not supported";
6309 if ( ! reloc
->r_pcrel() )
6310 throw "not pcrel and X86_64_RELOC_GOT not supported";
6311 if ( reloc
->r_length() != 2 )
6312 throw "length != 2 and X86_64_RELOC_GOT not supported";
6313 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel32GOT
, target
);
6315 case X86_64_RELOC_GOT_LOAD
:
6316 if ( ! reloc
->r_extern() )
6317 throw "not extern and X86_64_RELOC_GOT_LOAD not supported";
6318 if ( ! reloc
->r_pcrel() )
6319 throw "not pcrel and X86_64_RELOC_GOT_LOAD not supported";
6320 if ( reloc
->r_length() != 2 )
6321 throw "length != 2 and X86_64_RELOC_GOT_LOAD not supported";
6322 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel32GOTLoad
, target
);
6324 case X86_64_RELOC_SUBTRACTOR
:
6325 if ( reloc
->r_pcrel() )
6326 throw "X86_64_RELOC_SUBTRACTOR cannot be pc-relative";
6327 if ( reloc
->r_length() < 2 )
6328 throw "X86_64_RELOC_SUBTRACTOR must have r_length of 2 or 3";
6329 if ( !reloc
->r_extern() )
6330 throw "X86_64_RELOC_SUBTRACTOR must have r_extern=1";
6331 if ( nextReloc
->r_type() != X86_64_RELOC_UNSIGNED
)
6332 throw "X86_64_RELOC_SUBTRACTOR must be followed by X86_64_RELOC_UNSIGNED";
6334 if ( nextReloc
->r_pcrel() )
6335 throw "X86_64_RELOC_UNSIGNED following a X86_64_RELOC_SUBTRACTOR cannot be pc-relative";
6336 if ( nextReloc
->r_length() != reloc
->r_length() )
6337 throw "X86_64_RELOC_UNSIGNED following a X86_64_RELOC_SUBTRACTOR must have same r_length";
6338 if ( nextReloc
->r_extern() ) {
6339 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(nextReloc
->r_symbolnum());
6340 // use direct reference for local symbols
6341 if ( ((sym
.n_type() & N_TYPE
) == N_SECT
) && (((sym
.n_type() & N_EXT
) == 0) || (parser
.nameFromSymbol(sym
)[0] == 'L')) ) {
6342 parser
.findTargetFromAddressAndSectionNum(sym
.n_value(), sym
.n_sect(), toTarget
);
6343 toTarget
.addend
= contentValue
;
6344 useDirectBinding
= true;
6347 toTarget
.name
= parser
.nameFromSymbol(sym
);
6348 toTarget
.weakImport
= parser
.weakImportFromSymbol(sym
);
6349 toTarget
.addend
= contentValue
;
6350 useDirectBinding
= false;
6354 parser
.findTargetFromAddressAndSectionNum(contentValue
, nextReloc
->r_symbolnum(), toTarget
);
6355 useDirectBinding
= (toTarget
.atom
->scope() == ld::Atom::scopeTranslationUnit
) || ((toTarget
.atom
->combine() == ld::Atom::combineByNameAndContent
) || (toTarget
.atom
->combine() == ld::Atom::combineByNameAndReferences
));
6357 if ( useDirectBinding
) {
6358 if ( (toTarget
.atom
->combine() == ld::Atom::combineByNameAndContent
) || (toTarget
.atom
->combine() == ld::Atom::combineByNameAndReferences
) )
6359 parser
.addFixup(src
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, toTarget
.atom
);
6361 parser
.addFixup(src
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, toTarget
.atom
);
6364 parser
.addFixup(src
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, toTarget
.weakImport
, toTarget
.name
);
6365 parser
.addFixup(src
, ld::Fixup::k2of4
, ld::Fixup::kindAddAddend
, toTarget
.addend
);
6366 if ( target
.atom
== NULL
)
6367 parser
.addFixup(src
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractTargetAddress
, false, target
.name
);
6369 parser
.addFixup(src
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractTargetAddress
, target
.atom
);
6370 if ( reloc
->r_length() == 2 )
6371 parser
.addFixup(src
, ld::Fixup::k4of4
, ld::Fixup::kindStoreLittleEndian32
);
6373 parser
.addFixup(src
, ld::Fixup::k4of4
, ld::Fixup::kindStoreLittleEndian64
);
6375 case X86_64_RELOC_TLV
:
6376 if ( ! reloc
->r_extern() )
6377 throw "not extern and X86_64_RELOC_TLV not supported";
6378 if ( ! reloc
->r_pcrel() )
6379 throw "not pcrel and X86_64_RELOC_TLV not supported";
6380 if ( reloc
->r_length() != 2 )
6381 throw "length != 2 and X86_64_RELOC_TLV not supported";
6382 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel32TLVLoad
, target
);
6385 throwf("unknown relocation type %d", reloc
->r_type());
6393 bool Section
<x86
>::addRelocFixup(class Parser
<x86
>& parser
, const macho_relocation_info
<P
>* reloc
)
6395 const macho_section
<P
>* sect
= this->machoSection();
6397 const uint8_t* fixUpPtr
;
6398 uint32_t contentValue
= 0;
6399 ld::Fixup::Kind kind
= ld::Fixup::kindNone
;
6400 Parser
<x86
>::SourceLocation src
;
6401 Parser
<x86
>::TargetDesc target
;
6403 if ( (reloc
->r_address() & R_SCATTERED
) == 0 ) {
6404 srcAddr
= sect
->addr() + reloc
->r_address();
6405 src
.atom
= this->findAtomByAddress(srcAddr
);
6406 src
.offsetInAtom
= srcAddr
- src
.atom
->_objAddress
;
6407 fixUpPtr
= file().fileContent() + sect
->offset() + reloc
->r_address();
6408 switch ( reloc
->r_type() ) {
6409 case GENERIC_RELOC_VANILLA
:
6410 switch ( reloc
->r_length() ) {
6412 contentValue
= (int32_t)(int8_t)*fixUpPtr
;
6413 if ( reloc
->r_pcrel() ) {
6414 kind
= ld::Fixup::kindStoreX86BranchPCRel8
;
6415 contentValue
+= srcAddr
+ sizeof(uint8_t);
6418 throw "r_length=0 and r_pcrel=0 not supported";
6421 contentValue
= (int32_t)(int16_t)E::get16(*((uint16_t*)fixUpPtr
));
6422 if ( reloc
->r_pcrel() ) {
6423 kind
= ld::Fixup::kindStoreX86PCRel16
;
6424 contentValue
+= srcAddr
+ sizeof(uint16_t);
6427 kind
= ld::Fixup::kindStoreLittleEndian16
;
6430 contentValue
= E::get32(*((uint32_t*)fixUpPtr
));
6431 if ( reloc
->r_pcrel() ) {
6432 kind
= ld::Fixup::kindStoreX86BranchPCRel32
;
6433 contentValue
+= srcAddr
+ sizeof(uint32_t);
6436 kind
= ld::Fixup::kindStoreLittleEndian32
;
6439 throw "r_length=3 not supported";
6441 if ( reloc
->r_extern() ) {
6443 const macho_nlist
<P
>& targetSymbol
= parser
.symbolFromIndex(reloc
->r_symbolnum());
6444 target
.name
= parser
.nameFromSymbol(targetSymbol
);
6445 target
.weakImport
= parser
.weakImportFromSymbol(targetSymbol
);
6446 target
.addend
= (int32_t)contentValue
;
6449 parser
.findTargetFromAddressAndSectionNum(contentValue
, reloc
->r_symbolnum(), target
);
6451 if ( (kind
== ld::Fixup::kindStoreX86BranchPCRel32
) && (target
.name
!= NULL
) ) {
6452 if ( strncmp(target
.name
, "___dtrace_probe$", 16) == 0 ) {
6453 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindStoreX86DtraceCallSiteNop
, false, target
.name
);
6454 parser
.addDtraceExtraInfos(src
, &target
.name
[16]);
6457 else if ( strncmp(target
.name
, "___dtrace_isenabled$", 20) == 0 ) {
6458 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindStoreX86DtraceIsEnableSiteClear
, false, target
.name
);
6459 parser
.addDtraceExtraInfos(src
, &target
.name
[20]);
6463 parser
.addFixups(src
, kind
, target
);
6466 case GENERIC_RLEOC_TLV
:
6468 if ( !reloc
->r_extern() )
6469 throw "r_extern=0 and r_type=GENERIC_RLEOC_TLV not supported";
6470 if ( reloc
->r_length() != 2 )
6471 throw "r_length!=2 and r_type=GENERIC_RLEOC_TLV not supported";
6472 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(reloc
->r_symbolnum());
6473 // use direct reference for local symbols
6474 if ( ((sym
.n_type() & N_TYPE
) == N_SECT
) && ((sym
.n_type() & N_EXT
) == 0) ) {
6475 parser
.findTargetFromAddressAndSectionNum(sym
.n_value(), sym
.n_sect(), target
);
6479 target
.name
= parser
.nameFromSymbol(sym
);
6480 target
.weakImport
= parser
.weakImportFromSymbol(sym
);
6482 target
.addend
= (int64_t)(int32_t)E::get32(*((uint32_t*)fixUpPtr
));
6483 if ( reloc
->r_pcrel() ) {
6484 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel32TLVLoad
, target
);
6487 parser
.addFixups(src
, ld::Fixup::kindStoreX86Abs32TLVLoad
, target
);
6493 throwf("unsupported i386 relocation type (%d)", reloc
->r_type());
6497 // scattered relocation
6498 const macho_scattered_relocation_info
<P
>* sreloc
= (macho_scattered_relocation_info
<P
>*)reloc
;
6499 srcAddr
= sect
->addr() + sreloc
->r_address();
6500 src
.atom
= this->findAtomByAddress(srcAddr
);
6501 assert(src
.atom
!= NULL
);
6502 src
.offsetInAtom
= srcAddr
- src
.atom
->_objAddress
;
6503 fixUpPtr
= file().fileContent() + sect
->offset() + sreloc
->r_address();
6504 uint32_t relocValue
= sreloc
->r_value();
6505 bool result
= false;
6506 // file format allows pair to be scattered or not
6507 const macho_scattered_relocation_info
<P
>* nextSReloc
= &sreloc
[1];
6508 const macho_relocation_info
<P
>* nextReloc
= &reloc
[1];
6509 bool nextRelocIsPair
= false;
6510 uint32_t nextRelocAddress
= 0;
6511 uint32_t nextRelocValue
= 0;
6512 if ( (nextReloc
->r_address() & R_SCATTERED
) == 0 ) {
6513 if ( nextReloc
->r_type() == GENERIC_RELOC_PAIR
) {
6514 nextRelocIsPair
= true;
6515 nextRelocAddress
= nextReloc
->r_address();
6516 result
= true; // iterator should skip next reloc, since we've consumed it here
6520 if ( nextSReloc
->r_type() == GENERIC_RELOC_PAIR
) {
6521 nextRelocIsPair
= true;
6522 nextRelocAddress
= nextSReloc
->r_address();
6523 nextRelocValue
= nextSReloc
->r_value();
6526 switch (sreloc
->r_type()) {
6527 case GENERIC_RELOC_VANILLA
:
6528 // with a scattered relocation we get both the target (sreloc->r_value()) and the target+offset (*fixUpPtr)
6529 target
.atom
= parser
.findAtomByAddress(relocValue
);
6530 if ( sreloc
->r_pcrel() ) {
6531 switch ( sreloc
->r_length() ) {
6533 contentValue
= srcAddr
+ 1 + *fixUpPtr
;
6534 target
.addend
= (int32_t)contentValue
- (int32_t)relocValue
;
6535 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel8
, target
);
6538 contentValue
= srcAddr
+ 2 + LittleEndian::get16(*((uint16_t*)fixUpPtr
));
6539 target
.addend
= (int32_t)contentValue
- (int32_t)relocValue
;
6540 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel16
, target
);
6543 contentValue
= srcAddr
+ 4 + LittleEndian::get32(*((uint32_t*)fixUpPtr
));
6544 target
.addend
= (int32_t)contentValue
- (int32_t)relocValue
;
6545 parser
.addFixups(src
, ld::Fixup::kindStoreX86PCRel32
, target
);
6548 throw "unsupported r_length=3 for scattered pc-rel vanilla reloc";
6553 if ( sreloc
->r_length() != 2 )
6554 throwf("unsupported r_length=%d for scattered vanilla reloc", sreloc
->r_length());
6555 contentValue
= LittleEndian::get32(*((uint32_t*)fixUpPtr
));
6556 target
.addend
= (int32_t)contentValue
- (int32_t)(target
.atom
->objectAddress());
6557 parser
.addFixups(src
, ld::Fixup::kindStoreLittleEndian32
, target
);
6560 case GENERIC_RELOC_SECTDIFF
:
6561 case GENERIC_RELOC_LOCAL_SECTDIFF
:
6563 if ( !nextRelocIsPair
)
6564 throw "GENERIC_RELOC_SECTDIFF missing following pair";
6565 switch ( sreloc
->r_length() ) {
6568 throw "bad length for GENERIC_RELOC_SECTDIFF";
6570 contentValue
= (int32_t)(int16_t)LittleEndian::get16(*((uint16_t*)fixUpPtr
));
6571 kind
= ld::Fixup::kindStoreLittleEndian16
;
6574 contentValue
= LittleEndian::get32(*((uint32_t*)fixUpPtr
));
6575 kind
= ld::Fixup::kindStoreLittleEndian32
;
6578 Atom
<x86
>* fromAtom
= parser
.findAtomByAddress(nextRelocValue
);
6579 uint32_t offsetInFrom
= nextRelocValue
- fromAtom
->_objAddress
;
6580 parser
.findTargetFromAddress(sreloc
->r_value(), target
);
6581 // check for addend encoded in the section content
6582 int64_t addend
= (int32_t)contentValue
- (int32_t)(sreloc
->r_value() - nextRelocValue
);
6584 // switch binding base on coalescing
6585 if ( target
.atom
== NULL
) {
6586 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, false, target
.name
);
6588 else if ( target
.atom
->scope() == ld::Atom::scopeTranslationUnit
) {
6589 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, target
.atom
);
6591 else if ( (target
.atom
->combine() == ld::Atom::combineByNameAndContent
) || (target
.atom
->combine() == ld::Atom::combineByNameAndReferences
) ) {
6592 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, target
.atom
);
6595 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, false, target
.atom
->name());
6597 parser
.addFixup(src
, ld::Fixup::k2of5
, ld::Fixup::kindAddAddend
, target
.addend
);
6598 parser
.addFixup(src
, ld::Fixup::k3of5
, ld::Fixup::kindSubtractTargetAddress
, fromAtom
);
6599 parser
.addFixup(src
, ld::Fixup::k4of5
, ld::Fixup::kindSubtractAddend
, offsetInFrom
-addend
);
6600 parser
.addFixup(src
, ld::Fixup::k5of5
, kind
);
6603 // switch binding base on coalescing
6604 if ( target
.atom
== NULL
) {
6605 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, false, target
.name
);
6607 else if ( target
.atom
->scope() == ld::Atom::scopeTranslationUnit
) {
6608 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, target
.atom
);
6610 else if ( (target
.atom
->combine() == ld::Atom::combineByNameAndContent
) || (target
.atom
->combine() == ld::Atom::combineByNameAndReferences
) ) {
6611 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, target
.atom
);
6614 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, false, target
.atom
->name());
6616 parser
.addFixup(src
, ld::Fixup::k2of5
, ld::Fixup::kindAddAddend
, target
.addend
+addend
);
6617 parser
.addFixup(src
, ld::Fixup::k3of5
, ld::Fixup::kindSubtractTargetAddress
, fromAtom
);
6618 parser
.addFixup(src
, ld::Fixup::k4of5
, ld::Fixup::kindSubtractAddend
, offsetInFrom
);
6619 parser
.addFixup(src
, ld::Fixup::k5of5
, kind
);
6632 #if SUPPORT_ARCH_arm_any
6634 bool Section
<arm
>::addRelocFixup(class Parser
<arm
>& parser
, const macho_relocation_info
<P
>* reloc
)
6636 const macho_section
<P
>* sect
= this->machoSection();
6637 bool result
= false;
6641 int32_t displacement
= 0;
6642 uint32_t instruction
= 0;
6643 pint_t contentValue
= 0;
6644 Parser
<arm
>::SourceLocation src
;
6645 Parser
<arm
>::TargetDesc target
;
6646 const macho_relocation_info
<P
>* nextReloc
;
6648 if ( (reloc
->r_address() & R_SCATTERED
) == 0 ) {
6649 bool externSymbolIsThumbDef
= false;
6650 srcAddr
= sect
->addr() + reloc
->r_address();
6651 src
.atom
= this->findAtomByAddress(srcAddr
);
6652 src
.offsetInAtom
= srcAddr
- src
.atom
->_objAddress
;
6653 fixUpPtr
= (uint32_t*)(file().fileContent() + sect
->offset() + reloc
->r_address());
6654 if ( reloc
->r_type() != ARM_RELOC_PAIR
)
6655 instruction
= LittleEndian::get32(*fixUpPtr
);
6656 if ( reloc
->r_extern() ) {
6657 const macho_nlist
<P
>& targetSymbol
= parser
.symbolFromIndex(reloc
->r_symbolnum());
6658 // use direct reference for local symbols
6659 if ( ((targetSymbol
.n_type() & N_TYPE
) == N_SECT
) && (((targetSymbol
.n_type() & N_EXT
) == 0) || (parser
.nameFromSymbol(targetSymbol
)[0] == 'L')) ) {
6660 parser
.findTargetFromAddressAndSectionNum(targetSymbol
.n_value(), targetSymbol
.n_sect(), target
);
6664 target
.name
= parser
.nameFromSymbol(targetSymbol
);
6665 target
.weakImport
= parser
.weakImportFromSymbol(targetSymbol
);
6666 if ( ((targetSymbol
.n_type() & N_TYPE
) == N_SECT
) && (targetSymbol
.n_desc() & N_ARM_THUMB_DEF
) )
6667 externSymbolIsThumbDef
= true;
6670 switch ( reloc
->r_type() ) {
6671 case ARM_RELOC_BR24
:
6672 // Sign-extend displacement
6673 displacement
= (instruction
& 0x00FFFFFF) << 2;
6674 if ( (displacement
& 0x02000000) != 0 )
6675 displacement
|= 0xFC000000;
6676 // The pc added will be +8 from the pc
6678 // If this is BLX add H << 1
6679 if ((instruction
& 0xFE000000) == 0xFA000000)
6680 displacement
+= ((instruction
& 0x01000000) >> 23);
6681 if ( reloc
->r_extern() ) {
6682 dstAddr
= srcAddr
+ displacement
;
6683 // <rdar://problem/16652542> support large .o files
6684 if ( srcAddr
> 0x2000000 ) {
6685 dstAddr
-= ((srcAddr
+ 0x1FFFFFF) & 0xFC000000);
6687 target
.addend
= dstAddr
;
6688 if ( externSymbolIsThumbDef
)
6689 target
.addend
&= -2; // remove thumb bit
6692 dstAddr
= srcAddr
+ displacement
;
6693 parser
.findTargetFromAddressAndSectionNum(dstAddr
, reloc
->r_symbolnum(), target
);
6695 // special case "calls" for dtrace
6696 if ( (target
.name
!= NULL
) && (strncmp(target
.name
, "___dtrace_probe$", 16) == 0) ) {
6697 parser
.addFixup(src
, ld::Fixup::k1of1
,
6698 ld::Fixup::kindStoreARMDtraceCallSiteNop
, false, target
.name
);
6699 parser
.addDtraceExtraInfos(src
, &target
.name
[16]);
6701 else if ( (target
.name
!= NULL
) && (strncmp(target
.name
, "___dtrace_isenabled$", 20) == 0) ) {
6702 parser
.addFixup(src
, ld::Fixup::k1of1
,
6703 ld::Fixup::kindStoreARMDtraceIsEnableSiteClear
, false, target
.name
);
6704 parser
.addDtraceExtraInfos(src
, &target
.name
[20]);
6707 parser
.addFixups(src
, ld::Fixup::kindStoreARMBranch24
, target
);
6710 case ARM_THUMB_RELOC_BR22
:
6711 // thumb2 added two more bits to displacement, complicating the displacement decoding
6713 uint32_t s
= (instruction
>> 10) & 0x1;
6714 uint32_t j1
= (instruction
>> 29) & 0x1;
6715 uint32_t j2
= (instruction
>> 27) & 0x1;
6716 uint32_t imm10
= instruction
& 0x3FF;
6717 uint32_t imm11
= (instruction
>> 16) & 0x7FF;
6718 uint32_t i1
= (j1
== s
);
6719 uint32_t i2
= (j2
== s
);
6720 uint32_t dis
= (s
<< 24) | (i1
<< 23) | (i2
<< 22) | (imm10
<< 12) | (imm11
<< 1);
6724 displacement
= sdis
;
6726 // The pc added will be +4 from the pc
6728 // If the instruction was blx, force the low 2 bits to be clear
6729 dstAddr
= srcAddr
+ displacement
;
6730 if ((instruction
& 0xD0000000) == 0xC0000000)
6731 dstAddr
&= 0xFFFFFFFC;
6733 if ( reloc
->r_extern() ) {
6734 // <rdar://problem/16652542> support large .o files
6735 if ( srcAddr
> 0x1000000 ) {
6736 dstAddr
-= ((srcAddr
+ 0xFFFFFF) & 0xFE000000);
6738 target
.addend
= (int64_t)(int32_t)dstAddr
;
6741 parser
.findTargetFromAddressAndSectionNum(dstAddr
, reloc
->r_symbolnum(), target
);
6743 // special case "calls" for dtrace
6744 if ( (target
.name
!= NULL
) && (strncmp(target
.name
, "___dtrace_probe$", 16) == 0) ) {
6745 parser
.addFixup(src
, ld::Fixup::k1of1
,
6746 ld::Fixup::kindStoreThumbDtraceCallSiteNop
, false, target
.name
);
6747 parser
.addDtraceExtraInfos(src
, &target
.name
[16]);
6749 else if ( (target
.name
!= NULL
) && (strncmp(target
.name
, "___dtrace_isenabled$", 20) == 0) ) {
6750 parser
.addFixup(src
, ld::Fixup::k1of1
,
6751 ld::Fixup::kindStoreThumbDtraceIsEnableSiteClear
, false, target
.name
);
6752 parser
.addDtraceExtraInfos(src
, &target
.name
[20]);
6755 parser
.addFixups(src
, ld::Fixup::kindStoreThumbBranch22
, target
);
6758 case ARM_RELOC_VANILLA
:
6759 if ( reloc
->r_length() != 2 )
6760 throw "bad length for ARM_RELOC_VANILLA";
6761 contentValue
= LittleEndian::get32(*fixUpPtr
);
6762 if ( reloc
->r_extern() ) {
6763 target
.addend
= (int32_t)contentValue
;
6764 if ( externSymbolIsThumbDef
)
6765 target
.addend
&= -2; // remove thumb bit
6768 parser
.findTargetFromAddressAndSectionNum(contentValue
, reloc
->r_symbolnum(), target
);
6769 // possible non-extern relocation turned into by-name ref because target is a weak-def
6770 if ( target
.atom
!= NULL
) {
6771 if ( target
.atom
->isThumb() )
6772 target
.addend
&= -2; // remove thumb bit
6773 // if reference to LSDA, add group subordinate fixup
6774 if ( target
.atom
->contentType() == ld::Atom::typeLSDA
) {
6775 Parser
<arm
>::SourceLocation src2
;
6776 src2
.atom
= src
.atom
;
6777 src2
.offsetInAtom
= 0;
6778 parser
.addFixup(src2
, ld::Fixup::k1of1
, ld::Fixup::kindNoneGroupSubordinateLSDA
, target
.atom
);
6782 parser
.addFixups(src
, ld::Fixup::kindStoreLittleEndian32
, target
);
6784 case ARM_THUMB_32BIT_BRANCH
:
6785 // silently ignore old unnecessary reloc
6787 case ARM_RELOC_HALF
:
6788 nextReloc
= &reloc
[1];
6789 if ( nextReloc
->r_type() == ARM_RELOC_PAIR
) {
6790 uint32_t instruction16
;
6791 uint32_t other16
= (nextReloc
->r_address() & 0xFFFF);
6793 if ( reloc
->r_length() & 2 ) {
6795 uint32_t i
= ((instruction
& 0x00000400) >> 10);
6796 uint32_t imm4
= (instruction
& 0x0000000F);
6797 uint32_t imm3
= ((instruction
& 0x70000000) >> 28);
6798 uint32_t imm8
= ((instruction
& 0x00FF0000) >> 16);
6799 instruction16
= (imm4
<< 12) | (i
<< 11) | (imm3
<< 8) | imm8
;
6803 uint32_t imm4
= ((instruction
& 0x000F0000) >> 16);
6804 uint32_t imm12
= (instruction
& 0x00000FFF);
6805 instruction16
= (imm4
<< 12) | imm12
;
6807 if ( reloc
->r_length() & 1 ) {
6809 dstAddr
= ((instruction16
<< 16) | other16
);
6810 if ( reloc
->r_extern() ) {
6811 target
.addend
= dstAddr
;
6812 if ( externSymbolIsThumbDef
)
6813 target
.addend
&= -2; // remove thumb bit
6816 parser
.findTargetFromAddress(dstAddr
, target
);
6817 if ( target
.atom
->isThumb() )
6818 target
.addend
&= (-2); // remove thumb bit
6820 parser
.addFixups(src
, (isThumb
? ld::Fixup::kindStoreThumbHigh16
: ld::Fixup::kindStoreARMHigh16
), target
);
6824 dstAddr
= (other16
<< 16) | instruction16
;
6825 if ( reloc
->r_extern() ) {
6826 target
.addend
= dstAddr
;
6827 if ( externSymbolIsThumbDef
)
6828 target
.addend
&= -2; // remove thumb bit
6831 parser
.findTargetFromAddress(dstAddr
, target
);
6832 if ( target
.atom
->isThumb() )
6833 target
.addend
&= (-2); // remove thumb bit
6835 parser
.addFixups(src
, (isThumb
? ld::Fixup::kindStoreThumbLow16
: ld::Fixup::kindStoreARMLow16
), target
);
6840 throw "for ARM_RELOC_HALF, next reloc is not ARM_RELOC_PAIR";
6843 throwf("unknown relocation type %d", reloc
->r_type());
6848 const macho_scattered_relocation_info
<P
>* sreloc
= (macho_scattered_relocation_info
<P
>*)reloc
;
6849 // file format allows pair to be scattered or not
6850 const macho_scattered_relocation_info
<P
>* nextSReloc
= &sreloc
[1];
6851 nextReloc
= &reloc
[1];
6852 srcAddr
= sect
->addr() + sreloc
->r_address();
6853 dstAddr
= sreloc
->r_value();
6854 fixUpPtr
= (uint32_t*)(file().fileContent() + sect
->offset() + sreloc
->r_address());
6855 instruction
= LittleEndian::get32(*fixUpPtr
);
6856 src
.atom
= this->findAtomByAddress(srcAddr
);
6857 src
.offsetInAtom
= srcAddr
- src
.atom
->_objAddress
;
6858 bool nextRelocIsPair
= false;
6859 uint32_t nextRelocAddress
= 0;
6860 uint32_t nextRelocValue
= 0;
6861 if ( (nextReloc
->r_address() & R_SCATTERED
) == 0 ) {
6862 if ( nextReloc
->r_type() == ARM_RELOC_PAIR
) {
6863 nextRelocIsPair
= true;
6864 nextRelocAddress
= nextReloc
->r_address();
6869 if ( nextSReloc
->r_type() == ARM_RELOC_PAIR
) {
6870 nextRelocIsPair
= true;
6871 nextRelocAddress
= nextSReloc
->r_address();
6872 nextRelocValue
= nextSReloc
->r_value();
6876 switch ( sreloc
->r_type() ) {
6877 case ARM_RELOC_VANILLA
:
6878 // with a scattered relocation we get both the target (sreloc->r_value()) and the target+offset (*fixUpPtr)
6879 if ( sreloc
->r_length() != 2 )
6880 throw "bad length for ARM_RELOC_VANILLA";
6881 target
.atom
= parser
.findAtomByAddress(sreloc
->r_value());
6882 if ( target
.atom
== NULL
)
6883 throwf("bad r_value (0x%08X) for ARM_RELOC_VANILLA\n", sreloc
->r_value());
6884 contentValue
= LittleEndian::get32(*fixUpPtr
);
6885 target
.addend
= contentValue
- target
.atom
->_objAddress
;
6886 if ( target
.atom
->isThumb() )
6887 target
.addend
&= -2; // remove thumb bit
6888 parser
.addFixups(src
, ld::Fixup::kindStoreLittleEndian32
, target
);
6890 case ARM_RELOC_BR24
:
6891 // Sign-extend displacement
6892 displacement
= (instruction
& 0x00FFFFFF) << 2;
6893 if ( (displacement
& 0x02000000) != 0 )
6894 displacement
|= 0xFC000000;
6895 // The pc added will be +8 from the pc
6897 // If this is BLX add H << 1
6898 if ((instruction
& 0xFE000000) == 0xFA000000)
6899 displacement
+= ((instruction
& 0x01000000) >> 23);
6900 target
.atom
= parser
.findAtomByAddress(sreloc
->r_value());
6901 target
.addend
= (int64_t)(srcAddr
+ displacement
) - (int64_t)(target
.atom
->_objAddress
);
6902 parser
.addFixups(src
, ld::Fixup::kindStoreARMBranch24
, target
);
6904 case ARM_THUMB_RELOC_BR22
:
6905 // thumb2 added two more bits to displacement, complicating the displacement decoding
6907 uint32_t s
= (instruction
>> 10) & 0x1;
6908 uint32_t j1
= (instruction
>> 29) & 0x1;
6909 uint32_t j2
= (instruction
>> 27) & 0x1;
6910 uint32_t imm10
= instruction
& 0x3FF;
6911 uint32_t imm11
= (instruction
>> 16) & 0x7FF;
6912 uint32_t i1
= (j1
== s
);
6913 uint32_t i2
= (j2
== s
);
6914 uint32_t dis
= (s
<< 24) | (i1
<< 23) | (i2
<< 22) | (imm10
<< 12) | (imm11
<< 1);
6918 displacement
= sdis
;
6920 // The pc added will be +4 from the pc
6922 dstAddr
= srcAddr
+displacement
;
6923 // If the instruction was blx, force the low 2 bits to be clear
6924 if ((instruction
& 0xF8000000) == 0xE8000000)
6925 dstAddr
&= 0xFFFFFFFC;
6926 target
.atom
= parser
.findAtomByAddress(sreloc
->r_value());
6927 target
.addend
= dstAddr
- target
.atom
->_objAddress
;
6928 parser
.addFixups(src
, ld::Fixup::kindStoreThumbBranch22
, target
);
6930 case ARM_RELOC_SECTDIFF
:
6931 case ARM_RELOC_LOCAL_SECTDIFF
:
6933 if ( ! nextRelocIsPair
)
6934 throw "ARM_RELOC_SECTDIFF missing following pair";
6935 if ( sreloc
->r_length() != 2 )
6936 throw "bad length for ARM_RELOC_SECTDIFF";
6937 contentValue
= LittleEndian::get32(*fixUpPtr
);
6938 Atom
<arm
>* fromAtom
= parser
.findAtomByAddress(nextRelocValue
);
6939 uint32_t offsetInFrom
= nextRelocValue
- fromAtom
->_objAddress
;
6940 uint32_t offsetInTarget
;
6941 Atom
<arm
>* targetAtom
= parser
.findAtomByAddressOrLocalTargetOfStub(sreloc
->r_value(), &offsetInTarget
);
6942 // check for addend encoded in the section content
6943 int64_t addend
= (int32_t)contentValue
- (int32_t)(sreloc
->r_value() - nextRelocValue
);
6944 if ( targetAtom
->isThumb() )
6945 addend
&= -2; // remove thumb bit
6946 // if reference to LSDA, add group subordinate fixup
6947 if ( targetAtom
->contentType() == ld::Atom::typeLSDA
) {
6948 Parser
<arm
>::SourceLocation src2
;
6949 src2
.atom
= src
.atom
;
6950 src2
.offsetInAtom
= 0;
6951 parser
.addFixup(src2
, ld::Fixup::k1of1
, ld::Fixup::kindNoneGroupSubordinateLSDA
, targetAtom
);
6954 // switch binding base on coalescing
6955 if ( targetAtom
->scope() == ld::Atom::scopeTranslationUnit
) {
6956 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, targetAtom
);
6958 else if ( (targetAtom
->combine() == ld::Atom::combineByNameAndContent
) || (targetAtom
->combine() == ld::Atom::combineByNameAndReferences
) ) {
6959 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, targetAtom
);
6962 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, false, targetAtom
->name());
6964 parser
.addFixup(src
, ld::Fixup::k2of5
, ld::Fixup::kindAddAddend
, offsetInTarget
);
6965 parser
.addFixup(src
, ld::Fixup::k3of5
, ld::Fixup::kindSubtractTargetAddress
, fromAtom
);
6966 parser
.addFixup(src
, ld::Fixup::k4of5
, ld::Fixup::kindSubtractAddend
, offsetInFrom
-addend
);
6967 parser
.addFixup(src
, ld::Fixup::k5of5
, ld::Fixup::kindStoreLittleEndian32
);
6970 if ( targetAtom
->scope() == ld::Atom::scopeTranslationUnit
) {
6971 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, targetAtom
);
6973 else if ( (targetAtom
->combine() == ld::Atom::combineByNameAndContent
) || (targetAtom
->combine() == ld::Atom::combineByNameAndReferences
) ) {
6974 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, targetAtom
);
6977 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, false, targetAtom
->name());
6979 parser
.addFixup(src
, ld::Fixup::k2of5
, ld::Fixup::kindAddAddend
, (uint32_t)(offsetInTarget
+addend
));
6980 parser
.addFixup(src
, ld::Fixup::k3of5
, ld::Fixup::kindSubtractTargetAddress
, fromAtom
);
6981 parser
.addFixup(src
, ld::Fixup::k4of5
, ld::Fixup::kindSubtractAddend
, offsetInFrom
);
6982 parser
.addFixup(src
, ld::Fixup::k5of5
, ld::Fixup::kindStoreLittleEndian32
);
6986 case ARM_RELOC_HALF_SECTDIFF
:
6987 if ( nextRelocIsPair
) {
6988 instruction
= LittleEndian::get32(*fixUpPtr
);
6989 Atom
<arm
>* fromAtom
= parser
.findAtomByAddress(nextRelocValue
);
6990 uint32_t offsetInFrom
= nextRelocValue
- fromAtom
->_objAddress
;
6991 Atom
<arm
>* targetAtom
= parser
.findAtomByAddress(sreloc
->r_value());
6992 uint32_t offsetInTarget
= sreloc
->r_value() - targetAtom
->_objAddress
;
6993 uint32_t instruction16
;
6994 uint32_t other16
= (nextRelocAddress
& 0xFFFF);
6996 if ( sreloc
->r_length() & 2 ) {
6998 uint32_t i
= ((instruction
& 0x00000400) >> 10);
6999 uint32_t imm4
= (instruction
& 0x0000000F);
7000 uint32_t imm3
= ((instruction
& 0x70000000) >> 28);
7001 uint32_t imm8
= ((instruction
& 0x00FF0000) >> 16);
7002 instruction16
= (imm4
<< 12) | (i
<< 11) | (imm3
<< 8) | imm8
;
7006 uint32_t imm4
= ((instruction
& 0x000F0000) >> 16);
7007 uint32_t imm12
= (instruction
& 0x00000FFF);
7008 instruction16
= (imm4
<< 12) | imm12
;
7010 if ( sreloc
->r_length() & 1 )
7011 dstAddr
= ((instruction16
<< 16) | other16
);
7013 dstAddr
= (other16
<< 16) | instruction16
;
7014 if ( targetAtom
->isThumb() )
7015 dstAddr
&= (-2); // remove thumb bit
7016 int32_t addend
= dstAddr
- (sreloc
->r_value() - nextRelocValue
);
7017 if ( targetAtom
->scope() == ld::Atom::scopeTranslationUnit
) {
7018 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, targetAtom
);
7020 else if ( (targetAtom
->combine() == ld::Atom::combineByNameAndContent
) || (targetAtom
->combine() == ld::Atom::combineByNameAndReferences
) ) {
7021 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, targetAtom
);
7024 parser
.addFixup(src
, ld::Fixup::k1of5
, ld::Fixup::kindSetTargetAddress
, false, targetAtom
->name());
7026 parser
.addFixup(src
, ld::Fixup::k2of5
, ld::Fixup::kindAddAddend
, (uint32_t)offsetInTarget
+addend
);
7027 parser
.addFixup(src
, ld::Fixup::k3of5
, ld::Fixup::kindSubtractTargetAddress
, fromAtom
);
7028 parser
.addFixup(src
, ld::Fixup::k4of5
, ld::Fixup::kindSubtractAddend
, offsetInFrom
);
7029 if ( sreloc
->r_length() & 1 ) {
7031 parser
.addFixup(src
, ld::Fixup::k5of5
, (isThumb
? ld::Fixup::kindStoreThumbHigh16
: ld::Fixup::kindStoreARMHigh16
));
7035 parser
.addFixup(src
, ld::Fixup::k5of5
, (isThumb
? ld::Fixup::kindStoreThumbLow16
: ld::Fixup::kindStoreARMLow16
));
7040 throw "ARM_RELOC_HALF_SECTDIFF reloc missing following pair";
7042 case ARM_RELOC_HALF
:
7043 if ( nextRelocIsPair
) {
7044 instruction
= LittleEndian::get32(*fixUpPtr
);
7045 Atom
<arm
>* targetAtom
= parser
.findAtomByAddress(sreloc
->r_value());
7046 uint32_t instruction16
;
7047 uint32_t other16
= (nextRelocAddress
& 0xFFFF);
7049 if ( sreloc
->r_length() & 2 ) {
7051 uint32_t i
= ((instruction
& 0x00000400) >> 10);
7052 uint32_t imm4
= (instruction
& 0x0000000F);
7053 uint32_t imm3
= ((instruction
& 0x70000000) >> 28);
7054 uint32_t imm8
= ((instruction
& 0x00FF0000) >> 16);
7055 instruction16
= (imm4
<< 12) | (i
<< 11) | (imm3
<< 8) | imm8
;
7059 uint32_t imm4
= ((instruction
& 0x000F0000) >> 16);
7060 uint32_t imm12
= (instruction
& 0x00000FFF);
7061 instruction16
= (imm4
<< 12) | imm12
;
7063 if ( sreloc
->r_length() & 1 )
7064 dstAddr
= ((instruction16
<< 16) | other16
);
7066 dstAddr
= (other16
<< 16) | instruction16
;
7067 if ( targetAtom
->scope() == ld::Atom::scopeTranslationUnit
) {
7068 parser
.addFixup(src
, ld::Fixup::k1of3
, ld::Fixup::kindSetTargetAddress
, targetAtom
);
7070 else if ( (targetAtom
->combine() == ld::Atom::combineByNameAndContent
) || (targetAtom
->combine() == ld::Atom::combineByNameAndReferences
) ) {
7071 parser
.addFixup(src
, ld::Fixup::k1of3
, ld::Fixup::kindSetTargetAddress
, ld::Fixup::bindingByContentBound
, targetAtom
);
7074 parser
.addFixup(src
, ld::Fixup::k1of3
, ld::Fixup::kindSetTargetAddress
, false, targetAtom
->name());
7076 parser
.addFixup(src
, ld::Fixup::k2of3
, ld::Fixup::kindAddAddend
, dstAddr
- targetAtom
->_objAddress
);
7077 if ( sreloc
->r_length() & 1 ) {
7079 parser
.addFixup(src
, ld::Fixup::k3of3
, (isThumb
? ld::Fixup::kindStoreThumbHigh16
: ld::Fixup::kindStoreARMHigh16
));
7083 parser
.addFixup(src
, ld::Fixup::k3of3
, (isThumb
? ld::Fixup::kindStoreThumbLow16
: ld::Fixup::kindStoreARMLow16
));
7088 throw "scattered ARM_RELOC_HALF reloc missing following pair";
7091 throwf("unknown ARM scattered relocation type %d", sreloc
->r_type());
7099 #if SUPPORT_ARCH_arm64
7101 bool Section
<arm64
>::addRelocFixup(class Parser
<arm64
>& parser
, const macho_relocation_info
<P
>* reloc
)
7103 bool result
= false;
7104 Parser
<arm64
>::SourceLocation src
;
7105 Parser
<arm64
>::TargetDesc target
= { NULL
, NULL
, false, 0 };
7106 Parser
<arm64
>::TargetDesc toTarget
;
7107 int32_t prefixRelocAddend
= 0;
7108 if ( reloc
->r_type() == ARM64_RELOC_ADDEND
) {
7109 uint32_t rawAddend
= reloc
->r_symbolnum();
7110 prefixRelocAddend
= rawAddend
;
7111 if ( rawAddend
& 0x00800000 )
7112 prefixRelocAddend
|= 0xFF000000; // sign extend 24-bit signed int to 32-bits
7113 uint32_t addendAddress
= reloc
->r_address();
7114 ++reloc
; //advance to next reloc record
7116 if ( reloc
->r_address() != addendAddress
)
7117 throw "ARM64_RELOC_ADDEND r_address does not match next reloc's r_address";
7119 const macho_section
<P
>* sect
= this->machoSection();
7120 uint64_t srcAddr
= sect
->addr() + reloc
->r_address();
7121 src
.atom
= this->findAtomByAddress(srcAddr
);
7122 src
.offsetInAtom
= srcAddr
- src
.atom
->_objAddress
;
7123 const uint8_t* fixUpPtr
= file().fileContent() + sect
->offset() + reloc
->r_address();
7124 uint64_t contentValue
= 0;
7125 const macho_relocation_info
<arm64::P
>* nextReloc
= &reloc
[1];
7126 bool useDirectBinding
;
7127 uint32_t instruction
;
7128 uint32_t encodedAddend
;
7129 switch ( reloc
->r_length() ) {
7131 contentValue
= *fixUpPtr
;
7134 contentValue
= (int64_t)(int16_t)E::get16(*((uint16_t*)fixUpPtr
));
7137 contentValue
= (int64_t)(int32_t)E::get32(*((uint32_t*)fixUpPtr
));
7140 contentValue
= E::get64(*((uint64_t*)fixUpPtr
));
7143 if ( reloc
->r_extern() ) {
7144 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(reloc
->r_symbolnum());
7145 const char* symbolName
= parser
.nameFromSymbol(sym
);
7146 if ( ((sym
.n_type() & N_TYPE
) == N_SECT
) && (((sym
.n_type() & N_EXT
) == 0) || (symbolName
[0] == 'L') || (symbolName
[0] == 'l')) ) {
7147 // use direct reference for local symbols
7148 parser
.findTargetFromAddressAndSectionNum(sym
.n_value(), sym
.n_sect(), target
);
7149 //target.addend += contentValue;
7151 else if ( ((sym
.n_type() & N_TYPE
) == N_SECT
) && (src
.atom
->_objAddress
<= sym
.n_value()) && (sym
.n_value() < (src
.atom
->_objAddress
+src
.atom
->size())) ) {
7152 // <rdar://problem/13700961> spurious warning when weak function has reference to itself
7153 // use direct reference when atom targets itself
7154 target
.atom
= src
.atom
;
7158 target
.name
= symbolName
;
7159 target
.weakImport
= parser
.weakImportFromSymbol(sym
);
7160 //target.addend = contentValue;
7162 // cfstrings should always use direct reference to backing store
7163 if ( (this->type() == ld::Section::typeCFString
) && (src
.offsetInAtom
!= 0) ) {
7164 parser
.findTargetFromAddressAndSectionNum(sym
.n_value(), sym
.n_sect(), target
);
7165 //target.addend = contentValue;
7169 if ( reloc
->r_pcrel() )
7170 contentValue
+= srcAddr
;
7171 parser
.findTargetFromAddressAndSectionNum(contentValue
, reloc
->r_symbolnum(), target
);
7173 switch ( reloc
->r_type() ) {
7174 case ARM64_RELOC_UNSIGNED
:
7175 if ( reloc
->r_pcrel() )
7176 throw "pcrel and ARM64_RELOC_UNSIGNED not supported";
7177 if ( reloc
->r_extern() )
7178 target
.addend
= contentValue
;
7179 switch ( reloc
->r_length() ) {
7182 throw "length < 2 and ARM64_RELOC_UNSIGNED not supported";
7184 parser
.addFixups(src
, ld::Fixup::kindStoreLittleEndian32
, target
);
7187 parser
.addFixups(src
, ld::Fixup::kindStoreLittleEndian64
, target
);
7191 case ARM64_RELOC_BRANCH26
:
7192 if ( ! reloc
->r_pcrel() )
7193 throw "not pcrel and ARM64_RELOC_BRANCH26 not supported";
7194 if ( ! reloc
->r_extern() )
7195 throw "r_extern == 0 and ARM64_RELOC_BRANCH26 not supported";
7196 if ( reloc
->r_length() != 2 )
7197 throw "r_length != 2 and ARM64_RELOC_BRANCH26 not supported";
7198 if ( (target
.name
!= NULL
) && (strncmp(target
.name
, "___dtrace_probe$", 16) == 0) ) {
7199 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindStoreARM64DtraceCallSiteNop
, false, target
.name
);
7200 parser
.addDtraceExtraInfos(src
, &target
.name
[16]);
7202 else if ( (target
.name
!= NULL
) && (strncmp(target
.name
, "___dtrace_isenabled$", 20) == 0) ) {
7203 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindStoreARM64DtraceIsEnableSiteClear
, false, target
.name
);
7204 parser
.addDtraceExtraInfos(src
, &target
.name
[20]);
7207 target
.addend
= prefixRelocAddend
;
7208 instruction
= contentValue
;
7209 encodedAddend
= (instruction
& 0x03FFFFFF) << 2;
7210 if ( encodedAddend
!= 0 ) {
7211 if ( prefixRelocAddend
== 0 ) {
7212 warning("branch26 instruction at 0x%08X has embedded addend. ARM64_RELOC_ADDEND should be used instead", reloc
->r_address());
7213 target
.addend
= encodedAddend
;
7216 throwf("branch26 instruction at 0x%08X has embedded addend and ARM64_RELOC_ADDEND also used", reloc
->r_address());
7219 parser
.addFixups(src
, ld::Fixup::kindStoreARM64Branch26
, target
);
7222 case ARM64_RELOC_PAGE21
:
7223 if ( ! reloc
->r_pcrel() )
7224 throw "not pcrel and ARM64_RELOC_PAGE21 not supported";
7225 if ( ! reloc
->r_extern() )
7226 throw "r_extern == 0 and ARM64_RELOC_PAGE21 not supported";
7227 if ( reloc
->r_length() != 2 )
7228 throw "length != 2 and ARM64_RELOC_PAGE21 not supported";
7229 target
.addend
= prefixRelocAddend
;
7230 instruction
= contentValue
;
7231 encodedAddend
= ((instruction
& 0x60000000) >> 29) | ((instruction
& 0x01FFFFE0) >> 3);
7232 encodedAddend
*= 4096; // internally addend is in bytes, so scale
7233 if ( encodedAddend
!= 0 ) {
7234 if ( prefixRelocAddend
== 0 ) {
7235 warning("adrp instruction at 0x%08X has embedded addend. ARM64_RELOC_ADDEND should be used instead", reloc
->r_address());
7236 target
.addend
= encodedAddend
;
7239 throwf("adrp instruction at 0x%08X has embedded addend and ARM64_RELOC_ADDEND also used", reloc
->r_address());
7242 parser
.addFixups(src
, ld::Fixup::kindStoreARM64Page21
, target
);
7244 case ARM64_RELOC_PAGEOFF12
:
7245 if ( reloc
->r_pcrel() )
7246 throw "pcrel and ARM64_RELOC_PAGEOFF12 not supported";
7247 if ( ! reloc
->r_extern() )
7248 throw "r_extern == 0 and ARM64_RELOC_PAGEOFF12 not supported";
7249 if ( reloc
->r_length() != 2 )
7250 throw "length != 2 and ARM64_RELOC_PAGEOFF12 not supported";
7251 target
.addend
= prefixRelocAddend
;
7252 instruction
= contentValue
;
7253 encodedAddend
= ((instruction
& 0x003FFC00) >> 10);
7254 // internally addend is in bytes. Some instructions have an implicit scale factor
7255 if ( (instruction
& 0x3B000000) == 0x39000000 ) {
7256 switch ( instruction
& 0xC0000000 ) {
7270 if ( encodedAddend
!= 0 ) {
7271 if ( prefixRelocAddend
== 0 ) {
7272 warning("pageoff12 instruction at 0x%08X has embedded addend. ARM64_RELOC_ADDEND should be used instead", reloc
->r_address());
7273 target
.addend
= encodedAddend
;
7276 throwf("pageoff12 instruction at 0x%08X has embedded addend and ARM64_RELOC_ADDEND also used", reloc
->r_address());
7279 parser
.addFixups(src
, ld::Fixup::kindStoreARM64PageOff12
, target
);
7281 case ARM64_RELOC_GOT_LOAD_PAGE21
:
7282 if ( ! reloc
->r_pcrel() )
7283 throw "not pcrel and ARM64_RELOC_GOT_LOAD_PAGE21 not supported";
7284 if ( ! reloc
->r_extern() )
7285 throw "r_extern == 0 and ARM64_RELOC_GOT_LOAD_PAGE21 not supported";
7286 if ( reloc
->r_length() != 2 )
7287 throw "length != 2 and ARM64_RELOC_GOT_LOAD_PAGE21 not supported";
7288 if ( prefixRelocAddend
!= 0 )
7289 throw "ARM64_RELOC_ADDEND followed by ARM64_RELOC_GOT_LOAD_PAGE21 not supported";
7290 instruction
= contentValue
;
7291 target
.addend
= ((instruction
& 0x60000000) >> 29) | ((instruction
& 0x01FFFFE0) >> 3);
7292 if ( target
.addend
!= 0 )
7293 throw "non-zero addend with ARM64_RELOC_GOT_LOAD_PAGE21 is not supported";
7294 parser
.addFixups(src
, ld::Fixup::kindStoreARM64GOTLoadPage21
, target
);
7296 case ARM64_RELOC_GOT_LOAD_PAGEOFF12
:
7297 if ( reloc
->r_pcrel() )
7298 throw "pcrel and ARM64_RELOC_GOT_LOAD_PAGEOFF12 not supported";
7299 if ( ! reloc
->r_extern() )
7300 throw "r_extern == 0 and ARM64_RELOC_GOT_LOAD_PAGEOFF12 not supported";
7301 if ( reloc
->r_length() != 2 )
7302 throw "length != 2 and ARM64_RELOC_GOT_LOAD_PAGEOFF12 not supported";
7303 if ( prefixRelocAddend
!= 0 )
7304 throw "ARM64_RELOC_ADDEND followed by ARM64_RELOC_GOT_LOAD_PAGEOFF12 not supported";
7305 instruction
= contentValue
;
7306 target
.addend
= ((instruction
& 0x003FFC00) >> 10);
7307 parser
.addFixups(src
, ld::Fixup::kindStoreARM64GOTLoadPageOff12
, target
);
7309 case ARM64_RELOC_TLVP_LOAD_PAGE21
:
7310 if ( ! reloc
->r_pcrel() )
7311 throw "not pcrel and ARM64_RELOC_TLVP_LOAD_PAGE21 not supported";
7312 if ( ! reloc
->r_extern() )
7313 throw "r_extern == 0 and ARM64_RELOC_TLVP_LOAD_PAGE21 not supported";
7314 if ( reloc
->r_length() != 2 )
7315 throw "length != 2 and ARM64_RELOC_TLVP_LOAD_PAGE21 not supported";
7316 if ( prefixRelocAddend
!= 0 )
7317 throw "ARM64_RELOC_ADDEND followed by ARM64_RELOC_TLVP_LOAD_PAGE21 not supported";
7318 instruction
= contentValue
;
7319 target
.addend
= ((instruction
& 0x60000000) >> 29) | ((instruction
& 0x01FFFFE0) >> 3);
7320 if ( target
.addend
!= 0 )
7321 throw "non-zero addend with ARM64_RELOC_GOT_LOAD_PAGE21 is not supported";
7322 parser
.addFixups(src
, ld::Fixup::kindStoreARM64TLVPLoadPage21
, target
);
7324 case ARM64_RELOC_TLVP_LOAD_PAGEOFF12
:
7325 if ( reloc
->r_pcrel() )
7326 throw "pcrel and ARM64_RELOC_TLVP_LOAD_PAGEOFF12 not supported";
7327 if ( ! reloc
->r_extern() )
7328 throw "r_extern == 0 and ARM64_RELOC_TLVP_LOAD_PAGEOFF12 not supported";
7329 if ( reloc
->r_length() != 2 )
7330 throw "length != 2 and ARM64_RELOC_TLVP_LOAD_PAGEOFF12 not supported";
7331 if ( prefixRelocAddend
!= 0 )
7332 throw "ARM64_RELOC_ADDEND followed by ARM64_RELOC_TLVP_LOAD_PAGEOFF12 not supported";
7333 instruction
= contentValue
;
7334 target
.addend
= ((instruction
& 0x003FFC00) >> 10);
7335 parser
.addFixups(src
, ld::Fixup::kindStoreARM64TLVPLoadPageOff12
, target
);
7337 case ARM64_RELOC_SUBTRACTOR
:
7338 if ( reloc
->r_pcrel() )
7339 throw "ARM64_RELOC_SUBTRACTOR cannot be pc-relative";
7340 if ( reloc
->r_length() < 2 )
7341 throw "ARM64_RELOC_SUBTRACTOR must have r_length of 2 or 3";
7342 if ( !reloc
->r_extern() )
7343 throw "ARM64_RELOC_SUBTRACTOR must have r_extern=1";
7344 if ( nextReloc
->r_type() != ARM64_RELOC_UNSIGNED
)
7345 throw "ARM64_RELOC_SUBTRACTOR must be followed by ARM64_RELOC_UNSIGNED";
7346 if ( prefixRelocAddend
!= 0 )
7347 throw "ARM64_RELOC_ADDEND followed by ARM64_RELOC_SUBTRACTOR not supported";
7349 if ( nextReloc
->r_pcrel() )
7350 throw "ARM64_RELOC_UNSIGNED following a ARM64_RELOC_SUBTRACTOR cannot be pc-relative";
7351 if ( nextReloc
->r_length() != reloc
->r_length() )
7352 throw "ARM64_RELOC_UNSIGNED following a ARM64_RELOC_SUBTRACTOR must have same r_length";
7353 if ( nextReloc
->r_extern() ) {
7354 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(nextReloc
->r_symbolnum());
7355 // use direct reference for local symbols
7356 if ( ((sym
.n_type() & N_TYPE
) == N_SECT
) && (((sym
.n_type() & N_EXT
) == 0) || (parser
.nameFromSymbol(sym
)[0] == 'L')) ) {
7357 parser
.findTargetFromAddressAndSectionNum(sym
.n_value(), sym
.n_sect(), toTarget
);
7358 toTarget
.addend
= contentValue
;
7359 useDirectBinding
= true;
7362 toTarget
.name
= parser
.nameFromSymbol(sym
);
7363 toTarget
.weakImport
= parser
.weakImportFromSymbol(sym
);
7364 toTarget
.addend
= contentValue
;
7365 useDirectBinding
= false;
7369 parser
.findTargetFromAddressAndSectionNum(contentValue
, nextReloc
->r_symbolnum(), toTarget
);
7370 useDirectBinding
= (toTarget
.atom
->scope() == ld::Atom::scopeTranslationUnit
);
7372 if ( useDirectBinding
)
7373 parser
.addFixup(src
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, toTarget
.atom
);
7375 parser
.addFixup(src
, ld::Fixup::k1of4
, ld::Fixup::kindSetTargetAddress
, toTarget
.weakImport
, toTarget
.name
);
7376 parser
.addFixup(src
, ld::Fixup::k2of4
, ld::Fixup::kindAddAddend
, toTarget
.addend
);
7377 if ( target
.atom
== NULL
)
7378 parser
.addFixup(src
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractTargetAddress
, false, target
.name
);
7380 parser
.addFixup(src
, ld::Fixup::k3of4
, ld::Fixup::kindSubtractTargetAddress
, target
.atom
);
7381 if ( reloc
->r_length() == 2 )
7382 parser
.addFixup(src
, ld::Fixup::k4of4
, ld::Fixup::kindStoreLittleEndian32
);
7384 parser
.addFixup(src
, ld::Fixup::k4of4
, ld::Fixup::kindStoreLittleEndian64
);
7386 case ARM64_RELOC_POINTER_TO_GOT
:
7387 if ( ! reloc
->r_extern() )
7388 throw "r_extern == 0 and ARM64_RELOC_POINTER_TO_GOT not supported";
7389 if ( prefixRelocAddend
!= 0 )
7390 throw "ARM64_RELOC_ADDEND followed by ARM64_RELOC_POINTER_TO_GOT not supported";
7391 if ( reloc
->r_pcrel() ) {
7392 if ( reloc
->r_length() != 2 )
7393 throw "r_length != 2 and r_extern = 1 and ARM64_RELOC_POINTER_TO_GOT not supported";
7394 parser
.addFixups(src
, ld::Fixup::kindStoreARM64PCRelToGOT
, target
);
7397 if ( reloc
->r_length() != 3 )
7398 throw "r_length != 3 and r_extern = 0 and ARM64_RELOC_POINTER_TO_GOT not supported";
7399 parser
.addFixups(src
, ld::Fixup::kindStoreARM64PointerToGOT
, target
);
7403 throwf("unknown relocation type %d", reloc
->r_type());
7409 template <typename A
>
7410 bool ObjC1ClassSection
<A
>::addRelocFixup(class Parser
<A
>& parser
, const macho_relocation_info
<P
>* reloc
)
7413 FixedSizeSection
<A
>::addRelocFixup(parser
, reloc
);
7415 assert(0 && "needs template specialization");
7420 bool ObjC1ClassSection
<x86
>::addRelocFixup(class Parser
<x86
>& parser
, const macho_relocation_info
<x86::P
>* reloc
)
7422 // if this is the reloc for the super class name string, add implicit reference to super class
7423 if ( ((reloc
->r_address() & R_SCATTERED
) == 0) && (reloc
->r_type() == GENERIC_RELOC_VANILLA
) ) {
7424 assert( reloc
->r_length() == 2 );
7425 assert( ! reloc
->r_pcrel() );
7427 const macho_section
<P
>* sect
= this->machoSection();
7428 Parser
<x86
>::SourceLocation src
;
7429 uint32_t srcAddr
= sect
->addr() + reloc
->r_address();
7430 src
.atom
= this->findAtomByAddress(srcAddr
);
7431 src
.offsetInAtom
= srcAddr
- src
.atom
->objectAddress();
7432 if ( src
.offsetInAtom
== 4 ) {
7433 Parser
<x86
>::TargetDesc stringTarget
;
7434 const uint8_t* fixUpPtr
= file().fileContent() + sect
->offset() + reloc
->r_address();
7435 uint32_t contentValue
= LittleEndian::get32(*((uint32_t*)fixUpPtr
));
7436 parser
.findTargetFromAddressAndSectionNum(contentValue
, reloc
->r_symbolnum(), stringTarget
);
7438 assert(stringTarget
.atom
!= NULL
);
7439 assert(stringTarget
.atom
->contentType() == ld::Atom::typeCString
);
7440 const char* superClassBaseName
= (char*)stringTarget
.atom
->rawContentPointer();
7441 char* superClassName
= new char[strlen(superClassBaseName
) + 20];
7442 strcpy(superClassName
, ".objc_class_name_");
7443 strcat(superClassName
, superClassBaseName
);
7445 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindSetTargetAddress
, false, superClassName
);
7449 return FixedSizeSection
<x86
>::addRelocFixup(parser
, reloc
);
7454 template <typename A
>
7455 bool Objc1ClassReferences
<A
>::addRelocFixup(class Parser
<A
>& parser
, const macho_relocation_info
<P
>* reloc
)
7458 PointerToCStringSection
<A
>::addRelocFixup(parser
, reloc
);
7460 assert(0 && "needs template specialization");
7467 bool Objc1ClassReferences
<x86
>::addRelocFixup(class Parser
<x86
>& parser
, const macho_relocation_info
<x86::P
>* reloc
)
7469 // add implict class refs, fixups not usable yet, so look at relocations
7470 assert( (reloc
->r_address() & R_SCATTERED
) == 0 );
7471 assert( reloc
->r_type() == GENERIC_RELOC_VANILLA
);
7472 assert( reloc
->r_length() == 2 );
7473 assert( ! reloc
->r_pcrel() );
7475 const macho_section
<P
>* sect
= this->machoSection();
7476 Parser
<x86
>::SourceLocation src
;
7477 uint32_t srcAddr
= sect
->addr() + reloc
->r_address();
7478 src
.atom
= this->findAtomByAddress(srcAddr
);
7479 src
.offsetInAtom
= srcAddr
- src
.atom
->objectAddress();
7480 Parser
<x86
>::TargetDesc stringTarget
;
7481 const uint8_t* fixUpPtr
= file().fileContent() + sect
->offset() + reloc
->r_address();
7482 uint32_t contentValue
= LittleEndian::get32(*((uint32_t*)fixUpPtr
));
7483 parser
.findTargetFromAddressAndSectionNum(contentValue
, reloc
->r_symbolnum(), stringTarget
);
7485 assert(stringTarget
.atom
!= NULL
);
7486 assert(stringTarget
.atom
->contentType() == ld::Atom::typeCString
);
7487 const char* baseClassName
= (char*)stringTarget
.atom
->rawContentPointer();
7488 char* objcClassName
= new char[strlen(baseClassName
) + 20];
7489 strcpy(objcClassName
, ".objc_class_name_");
7490 strcat(objcClassName
, baseClassName
);
7492 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindSetTargetAddress
, false, objcClassName
);
7495 return PointerToCStringSection
<x86
>::addRelocFixup(parser
, reloc
);
7498 #if SUPPORT_ARCH_arm64
7500 void Section
<arm64
>::addLOH(class Parser
<arm64
>& parser
, int kind
, int count
, const uint64_t addrs
[]) {
7502 case LOH_ARM64_ADRP_ADRP
:
7503 case LOH_ARM64_ADRP_LDR
:
7504 case LOH_ARM64_ADRP_ADD
:
7505 case LOH_ARM64_ADRP_LDR_GOT
:
7507 warning("arm64 Linker Optimiztion Hint %d has wrong number of arguments", kind
);
7509 case LOH_ARM64_ADRP_ADD_LDR
:
7510 case LOH_ARM64_ADRP_LDR_GOT_LDR
:
7511 case LOH_ARM64_ADRP_ADD_STR
:
7512 case LOH_ARM64_ADRP_LDR_GOT_STR
:
7514 warning("arm64 Linker Optimiztion Hint %d has wrong number of arguments", kind
);
7517 // pick lowest address in tuple for use as offsetInAtom
7518 uint64_t lowestAddress
= addrs
[0];
7519 for(int i
=1; i
< count
; ++i
) {
7520 if ( addrs
[i
] < lowestAddress
)
7521 lowestAddress
= addrs
[i
];
7523 // verify all other address are in same atom
7524 Atom
<arm64
>* inAtom
= parser
.findAtomByAddress(lowestAddress
);
7525 const uint64_t atomStartAddr
= inAtom
->objectAddress();
7526 const uint64_t atomEndAddr
= atomStartAddr
+ inAtom
->size();
7527 for(int i
=0; i
< count
; ++i
) {
7528 if ( (addrs
[i
] < atomStartAddr
) || (addrs
[i
] >= atomEndAddr
) ) {
7529 warning("arm64 Linker Optimiztion Hint addresses are not in same atom: 0x%08llX and 0x%08llX",
7530 lowestAddress
, addrs
[i
]);
7531 return; // skip this LOH
7533 if ( (addrs
[i
] & 0x3) != 0 ) {
7534 warning("arm64 Linker Optimiztion Hint address is not 4-byte aligned: 0x%08llX", addrs
[i
]);
7535 return; // skip this LOH
7537 if ( (addrs
[i
] - lowestAddress
) > 0xFFFF ) {
7538 if ( parser
.verboseOptimizationHints() ) {
7539 warning("arm64 Linker Optimiztion Hint addresses are too far apart: 0x%08llX and 0x%08llX",
7540 lowestAddress
, addrs
[i
]);
7542 return; // skip this LOH
7546 // encoded kind, count, and address deltas in 64-bit addend
7547 ld::Fixup::LOH_arm64 extra
;
7549 extra
.info
.kind
= kind
;
7550 extra
.info
.count
= count
-1;
7551 extra
.info
.delta1
= (addrs
[0] - lowestAddress
) >> 2;
7552 extra
.info
.delta2
= (count
> 1) ? ((addrs
[1] - lowestAddress
) >> 2) : 0;
7553 extra
.info
.delta3
= (count
> 2) ? ((addrs
[2] - lowestAddress
) >> 2) : 0;
7554 extra
.info
.delta4
= (count
> 3) ? ((addrs
[3] - lowestAddress
) >> 2) : 0;
7555 typename Parser
<arm64
>::SourceLocation
src(inAtom
, lowestAddress
- inAtom
->objectAddress());
7556 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindLinkerOptimizationHint
, extra
.addend
);
7560 template <typename A
>
7561 void Section
<A
>::addLOH(class Parser
<A
>& parser
, int kind
, int count
, const uint64_t addrs
[]) {
7565 template <typename A
>
7566 void Section
<A
>::makeFixups(class Parser
<A
>& parser
, const struct Parser
<A
>::CFI_CU_InfoArrays
&)
7568 const macho_section
<P
>* sect
= this->machoSection();
7569 if ( sect
->reloff() + (sect
->nreloc() * sizeof(macho_relocation_info
<P
>)) > parser
.fileLength() )
7570 throwf("relocations for section %s/%s extends beyond end of file,", sect
->segname(), Section
<A
>::makeSectionName(sect
) );
7571 const macho_relocation_info
<P
>* relocs
= (macho_relocation_info
<P
>*)(file().fileContent() + sect
->reloff());
7572 const uint32_t relocCount
= sect
->nreloc();
7573 for (uint32_t r
= 0; r
< relocCount
; ++r
) {
7575 if ( this->addRelocFixup(parser
, &relocs
[r
]) )
7578 catch (const char* msg
) {
7579 throwf("in section %s,%s reloc %u: %s", sect
->segname(), Section
<A
>::makeSectionName(sect
), r
, msg
);
7583 // add follow-on fixups if .o file is missing .subsections_via_symbols
7584 if ( this->addFollowOnFixups() ) {
7585 Atom
<A
>* end
= &_endAtoms
[-1];
7586 for(Atom
<A
>* p
= _beginAtoms
; p
< end
; ++p
) {
7587 typename Parser
<A
>::SourceLocation
src(p
, 0);
7588 Atom
<A
>* nextAtom
= &p
[1];
7589 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindNoneFollowOn
, nextAtom
);
7592 else if ( this->type() == ld::Section::typeCode
) {
7593 // if FDE broke text not at a symbol, use followOn to keep code together
7594 Atom
<A
>* end
= &_endAtoms
[-1];
7595 for(Atom
<A
>* p
= _beginAtoms
; p
< end
; ++p
) {
7596 typename Parser
<A
>::SourceLocation
src(p
, 0);
7597 Atom
<A
>* nextAtom
= &p
[1];
7598 if ( (p
->symbolTableInclusion() == ld::Atom::symbolTableIn
) && (nextAtom
->symbolTableInclusion() == ld::Atom::symbolTableNotIn
) ) {
7599 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindNoneFollowOn
, nextAtom
);
7603 if ( !this->_altEntries
.empty() && !this->addFollowOnFixups() ) {
7604 if ( _altEntries
.count(_beginAtoms
) != 0 )
7605 warning("N_ALT_ENTRY bit set on first atom in section %s/%s", sect
->segname(), Section
<A
>::makeSectionName(sect
));
7607 Atom
<A
>* end
= &_endAtoms
[-1];
7608 for(Atom
<A
>* p
= _beginAtoms
; p
< end
; ++p
) {
7609 Atom
<A
>* nextAtom
= &p
[1];
7610 // <rdar://problem/22960070> support alt_entry aliases (alias process already added followOn, don't repeat)
7611 if ( (_altEntries
.count(nextAtom
) != 0) && (p
->_objAddress
!= nextAtom
->_objAddress
) ) {
7612 typename Parser
<A
>::SourceLocation
src(p
, 0);
7613 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindNoneFollowOn
, nextAtom
);
7614 typename Parser
<A
>::SourceLocation
src2(nextAtom
, 0);
7615 parser
.addFixup(src2
, ld::Fixup::k1of1
, ld::Fixup::kindNoneGroupSubordinate
, p
);
7620 // <rdar://problem/9218847> track data-in-code
7621 if ( parser
.hasDataInCodeLabels() && (this->type() == ld::Section::typeCode
) ) {
7622 for (uint32_t i
=0; i
< parser
.symbolCount(); ++i
) {
7623 const macho_nlist
<P
>& sym
= parser
.symbolFromIndex(i
);
7625 if ( (sym
.n_type() & N_STAB
) != 0 )
7627 // ignore non-definitions
7628 if ( (sym
.n_type() & N_TYPE
) != N_SECT
)
7631 // 'L' labels do not denote atom breaks
7632 const char* symbolName
= parser
.nameFromSymbol(sym
);
7633 if ( symbolName
[0] == 'L' ) {
7634 if ( strncmp(symbolName
, "L$start$", 8) == 0 ) {
7635 ld::Fixup::Kind kind
= ld::Fixup::kindNone
;
7636 if ( strncmp(&symbolName
[8], "data$", 5) == 0 )
7637 kind
= ld::Fixup::kindDataInCodeStartData
;
7638 else if ( strncmp(&symbolName
[8], "code$", 5) == 0 )
7639 kind
= ld::Fixup::kindDataInCodeEnd
;
7640 else if ( strncmp(&symbolName
[8], "jt8$", 4) == 0 )
7641 kind
= ld::Fixup::kindDataInCodeStartJT8
;
7642 else if ( strncmp(&symbolName
[8], "jt16$", 4) == 0 )
7643 kind
= ld::Fixup::kindDataInCodeStartJT16
;
7644 else if ( strncmp(&symbolName
[8], "jt32$", 4) == 0 )
7645 kind
= ld::Fixup::kindDataInCodeStartJT32
;
7646 else if ( strncmp(&symbolName
[8], "jta32$", 4) == 0 )
7647 kind
= ld::Fixup::kindDataInCodeStartJTA32
;
7649 warning("unknown L$start$ label %s in file %s", symbolName
, this->file().path());
7650 if ( kind
!= ld::Fixup::kindNone
) {
7651 Atom
<A
>* inAtom
= parser
.findAtomByAddress(sym
.n_value());
7652 typename Parser
<A
>::SourceLocation
src(inAtom
, sym
.n_value() - inAtom
->objectAddress());
7653 parser
.addFixup(src
, ld::Fixup::k1of1
, kind
);
7660 // <rdar://problem/11150575> Handle LC_DATA_IN_CODE in object files
7661 if ( this->type() == ld::Section::typeCode
) {
7662 const pint_t startAddr
= this->_machOSection
->addr();
7663 const pint_t endAddr
= startAddr
+ this->_machOSection
->size();
7664 for ( const macho_data_in_code_entry
<P
>* p
= parser
.dataInCodeStart(); p
!= parser
.dataInCodeEnd(); ++p
) {
7665 if ( (p
->offset() >= startAddr
) && (p
->offset() < endAddr
) ) {
7666 ld::Fixup::Kind kind
= ld::Fixup::kindNone
;
7667 switch ( p
->kind() ) {
7668 case DICE_KIND_DATA
:
7669 kind
= ld::Fixup::kindDataInCodeStartData
;
7671 case DICE_KIND_JUMP_TABLE8
:
7672 kind
= ld::Fixup::kindDataInCodeStartJT8
;
7674 case DICE_KIND_JUMP_TABLE16
:
7675 kind
= ld::Fixup::kindDataInCodeStartJT16
;
7677 case DICE_KIND_JUMP_TABLE32
:
7678 kind
= ld::Fixup::kindDataInCodeStartJT32
;
7680 case DICE_KIND_ABS_JUMP_TABLE32
:
7681 kind
= ld::Fixup::kindDataInCodeStartJTA32
;
7684 kind
= ld::Fixup::kindDataInCodeStartData
;
7685 warning("uknown LC_DATA_IN_CODE kind (%d) at offset 0x%08X", p
->kind(), p
->offset());
7688 Atom
<A
>* inAtom
= parser
.findAtomByAddress(p
->offset());
7689 typename Parser
<A
>::SourceLocation
srcStart(inAtom
, p
->offset() - inAtom
->objectAddress());
7690 parser
.addFixup(srcStart
, ld::Fixup::k1of1
, kind
);
7691 typename Parser
<A
>::SourceLocation
srcEnd(inAtom
, p
->offset() + p
->length() - inAtom
->objectAddress());
7692 parser
.addFixup(srcEnd
, ld::Fixup::k1of1
, ld::Fixup::kindDataInCodeEnd
);
7697 // <rdar://problem/11945700> convert linker optimization hints into internal format
7698 if ( this->type() == ld::Section::typeCode
&& parser
.hasOptimizationHints() ) {
7699 const pint_t startAddr
= this->_machOSection
->addr();
7700 const pint_t endAddr
= startAddr
+ this->_machOSection
->size();
7701 for (const uint8_t* p
= parser
.optimizationHintsStart(); p
< parser
.optimizationHintsEnd(); ) {
7703 int32_t kind
= read_uleb128(&p
, parser
.optimizationHintsEnd());
7704 if ( kind
== 0 ) // padding at end of loh buffer
7707 warning("malformed uleb128 kind in LC_LINKER_OPTIMIZATION_HINTS");
7710 int32_t count
= read_uleb128(&p
, parser
.optimizationHintsEnd());
7711 if ( count
== -1 ) {
7712 warning("malformed uleb128 count in LC_LINKER_OPTIMIZATION_HINTS");
7716 warning("address count > 3 in LC_LINKER_OPTIMIZATION_HINTS");
7719 for (int32_t i
=0; i
< count
; ++i
) {
7720 addrs
[i
] = read_uleb128(&p
, parser
.optimizationHintsEnd());
7722 if ( (startAddr
<= addrs
[0]) && (addrs
[0] < endAddr
) ) {
7723 this->addLOH(parser
, kind
, count
, addrs
);
7724 //fprintf(stderr, "kind=%d", kind);
7725 //for (int32_t i=0; i < count; ++i) {
7726 // fprintf(stderr, ", addr=0x%08llX", addrs[i]);
7728 //fprintf(stderr, "\n");
7734 // add follow-on fixups for aliases
7735 if ( _hasAliases
) {
7736 for(Atom
<A
>* p
= _beginAtoms
; p
< _endAtoms
; ++p
) {
7737 if ( p
->isAlias() && ! this->addFollowOnFixups() ) {
7738 Atom
<A
>* targetOfAlias
= &p
[1];
7739 assert(p
< &_endAtoms
[-1]);
7740 assert(p
->_objAddress
== targetOfAlias
->_objAddress
);
7741 typename Parser
<A
>::SourceLocation
src(p
, 0);
7742 parser
.addFixup(src
, ld::Fixup::k1of1
, ld::Fixup::kindNoneFollowOn
, targetOfAlias
);
7751 // main function used by linker to instantiate ld::Files
7753 ld::relocatable::File
* parse(const uint8_t* fileContent
, uint64_t fileLength
,
7754 const char* path
, time_t modTime
, ld::File::Ordinal ordinal
, const ParserOptions
& opts
)
7756 switch ( opts
.architecture
) {
7757 #if SUPPORT_ARCH_x86_64
7758 case CPU_TYPE_X86_64
:
7759 if ( mach_o::relocatable::Parser
<x86_64
>::validFile(fileContent
) )
7760 return mach_o::relocatable::Parser
<x86_64
>::parse(fileContent
, fileLength
, path
, modTime
, ordinal
, opts
);
7763 #if SUPPORT_ARCH_i386
7765 if ( mach_o::relocatable::Parser
<x86
>::validFile(fileContent
) )
7766 return mach_o::relocatable::Parser
<x86
>::parse(fileContent
, fileLength
, path
, modTime
, ordinal
, opts
);
7769 #if SUPPORT_ARCH_arm_any
7771 if ( mach_o::relocatable::Parser
<arm
>::validFile(fileContent
, opts
.objSubtypeMustMatch
, opts
.subType
) )
7772 return mach_o::relocatable::Parser
<arm
>::parse(fileContent
, fileLength
, path
, modTime
, ordinal
, opts
);
7775 #if SUPPORT_ARCH_arm64
7776 case CPU_TYPE_ARM64
:
7777 if ( mach_o::relocatable::Parser
<arm64
>::validFile(fileContent
, opts
.objSubtypeMustMatch
, opts
.subType
) )
7778 return mach_o::relocatable::Parser
<arm64
>::parse(fileContent
, fileLength
, path
, modTime
, ordinal
, opts
);
7786 // used by archive reader to validate member object file
7788 bool isObjectFile(const uint8_t* fileContent
, uint64_t fileLength
, const ParserOptions
& opts
)
7790 switch ( opts
.architecture
) {
7791 case CPU_TYPE_X86_64
:
7792 return ( mach_o::relocatable::Parser
<x86_64
>::validFile(fileContent
) );
7794 return ( mach_o::relocatable::Parser
<x86
>::validFile(fileContent
) );
7796 return ( mach_o::relocatable::Parser
<arm
>::validFile(fileContent
, opts
.objSubtypeMustMatch
, opts
.subType
) );
7797 case CPU_TYPE_ARM64
:
7798 return ( mach_o::relocatable::Parser
<arm64
>::validFile(fileContent
, opts
.objSubtypeMustMatch
, opts
.subType
) );
7804 // used by linker to infer architecture when no -arch is on command line
7806 bool isObjectFile(const uint8_t* fileContent
, cpu_type_t
* result
, cpu_subtype_t
* subResult
, Options::Platform
* platform
)
7808 if ( mach_o::relocatable::Parser
<x86_64
>::validFile(fileContent
) ) {
7809 *result
= CPU_TYPE_X86_64
;
7810 const macho_header
<Pointer64
<LittleEndian
> >* header
= (const macho_header
<Pointer64
<LittleEndian
> >*)fileContent
;
7811 *subResult
= header
->cpusubtype();
7812 *platform
= Parser
<x86_64
>::findPlatform(header
);
7815 if ( mach_o::relocatable::Parser
<x86
>::validFile(fileContent
) ) {
7816 const macho_header
<Pointer32
<LittleEndian
> >* header
= (const macho_header
<Pointer32
<LittleEndian
> >*)fileContent
;
7817 *result
= CPU_TYPE_I386
;
7818 *subResult
= CPU_SUBTYPE_X86_ALL
;
7819 *platform
= Parser
<x86
>::findPlatform(header
);
7822 if ( mach_o::relocatable::Parser
<arm
>::validFile(fileContent
, false, 0) ) {
7823 const macho_header
<Pointer32
<LittleEndian
> >* header
= (const macho_header
<Pointer32
<LittleEndian
> >*)fileContent
;
7824 *result
= CPU_TYPE_ARM
;
7825 *subResult
= header
->cpusubtype();
7826 *platform
= Parser
<arm
>::findPlatform(header
);
7829 if ( mach_o::relocatable::Parser
<arm64
>::validFile(fileContent
, false, 0) ) {
7830 const macho_header
<Pointer64
<LittleEndian
> >* header
= (const macho_header
<Pointer64
<LittleEndian
> >*)fileContent
;
7831 *result
= CPU_TYPE_ARM64
;
7832 *subResult
= CPU_SUBTYPE_ARM64_ALL
;
7833 *platform
= Parser
<arm64
>::findPlatform(header
);
7840 // used by linker is error messages to describe bad .o file
7842 const char* archName(const uint8_t* fileContent
)
7844 if ( mach_o::relocatable::Parser
<x86_64
>::validFile(fileContent
) ) {
7845 return mach_o::relocatable::Parser
<x86_64
>::fileKind(fileContent
);
7847 if ( mach_o::relocatable::Parser
<x86
>::validFile(fileContent
) ) {
7848 return mach_o::relocatable::Parser
<x86
>::fileKind(fileContent
);
7850 if ( mach_o::relocatable::Parser
<arm
>::validFile(fileContent
, false, 0) ) {
7851 return mach_o::relocatable::Parser
<arm
>::fileKind(fileContent
);
7857 // Used by archive reader when -ObjC option is specified
7859 bool hasObjC2Categories(const uint8_t* fileContent
)
7861 if ( mach_o::relocatable::Parser
<x86_64
>::validFile(fileContent
) ) {
7862 return mach_o::relocatable::Parser
<x86_64
>::hasObjC2Categories(fileContent
);
7864 else if ( mach_o::relocatable::Parser
<arm
>::validFile(fileContent
, false, 0) ) {
7865 return mach_o::relocatable::Parser
<arm
>::hasObjC2Categories(fileContent
);
7867 else if ( mach_o::relocatable::Parser
<x86
>::validFile(fileContent
, false, 0) ) {
7868 return mach_o::relocatable::Parser
<x86
>::hasObjC2Categories(fileContent
);
7870 #if SUPPORT_ARCH_arm64
7871 else if ( mach_o::relocatable::Parser
<arm64
>::validFile(fileContent
, false, 0) ) {
7872 return mach_o::relocatable::Parser
<arm64
>::hasObjC2Categories(fileContent
);
7879 // Used by archive reader when -ObjC option is specified
7881 bool hasObjC1Categories(const uint8_t* fileContent
)
7883 if ( mach_o::relocatable::Parser
<x86
>::validFile(fileContent
, false, 0) ) {
7884 return mach_o::relocatable::Parser
<x86
>::hasObjC1Categories(fileContent
);
7890 // Used by bitcode obfuscator to get a list of non local symbols from object file
7892 bool getNonLocalSymbols(const uint8_t* fileContent
, std::vector
<const char*> &syms
)
7894 if ( mach_o::relocatable::Parser
<x86_64
>::validFile(fileContent
) ) {
7895 return mach_o::relocatable::Parser
<x86_64
>::getNonLocalSymbols(fileContent
, syms
);
7897 else if ( mach_o::relocatable::Parser
<arm
>::validFile(fileContent
, false, 0) ) {
7898 return mach_o::relocatable::Parser
<arm
>::getNonLocalSymbols(fileContent
, syms
);
7900 else if ( mach_o::relocatable::Parser
<x86
>::validFile(fileContent
, false, 0) ) {
7901 return mach_o::relocatable::Parser
<x86
>::getNonLocalSymbols(fileContent
, syms
);
7903 else if ( mach_o::relocatable::Parser
<arm64
>::validFile(fileContent
, false, 0) ) {
7904 return mach_o::relocatable::Parser
<arm64
>::getNonLocalSymbols(fileContent
, syms
);
7911 } // namespace relocatable
7912 } // namespace mach_o