]> git.saurik.com Git - apple/ld64.git/blob - src/ld/parsers/macho_relocatable_file.cpp
00d5feb0839c45e6ecee4f81689d506aa369a961
[apple/ld64.git] / src / ld / parsers / macho_relocatable_file.cpp
1 /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-
2 *
3 * Copyright (c) 2009-2010 Apple Inc. All rights reserved.
4 *
5 * @APPLE_LICENSE_HEADER_START@
6 *
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
12 * file.
13 *
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.
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24
25
26 #include <stdint.h>
27 #include <stdlib.h>
28 #include <math.h>
29 #include <unistd.h>
30 #include <fcntl.h>
31 #include <sys/param.h>
32 #include <sys/stat.h>
33 #include <sys/mman.h>
34
35 #include "MachOFileAbstraction.hpp"
36
37 #include "libunwind/DwarfInstructions.hpp"
38 #include "libunwind/AddressSpace.hpp"
39 #include "libunwind/Registers.hpp"
40
41 #include <vector>
42 #include <set>
43 #include <map>
44 #include <algorithm>
45 #include <type_traits>
46
47 #include "dwarf2.h"
48 #include "debugline.h"
49
50 #include "Architectures.hpp"
51 #include "Bitcode.hpp"
52 #include "ld.hpp"
53 #include "macho_relocatable_file.h"
54
55
56
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)));
59
60 namespace mach_o {
61 namespace relocatable {
62
63
64 // forward reference
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;
70
71 template <typename A>
72 class File : public ld::relocatable::File
73 {
74 public:
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 _hasObjC(false),
84 _swiftVersion(0),
85 _cpuSubType(0),
86 _minOSVersion(0),
87 _canScatterAtoms(false),
88 _hasllvmProfiling(false),
89 _objcHasCategoryClassPropertiesField(false),
90 _srcKind(kSourceUnknown) { }
91 virtual ~File();
92
93 // overrides of ld::File
94 virtual bool forEachAtom(ld::File::AtomHandler&) const;
95 virtual bool justInTimeforEachAtom(const char* name, ld::File::AtomHandler&) const
96 { return false; }
97 virtual const ld::VersionSet& platforms() const { return _platforms; }
98
99 // overrides of ld::relocatable::File
100 virtual bool hasObjC() const { return _hasObjC; }
101 virtual bool objcHasCategoryClassPropertiesField() const
102 { return _objcHasCategoryClassPropertiesField; }
103 virtual uint32_t cpuSubType() const { return _cpuSubType; }
104 virtual DebugInfoKind debugInfo() const { return _debugInfoKind; }
105 virtual const std::vector<ld::relocatable::File::Stab>* stabs() const { return &_stabs; }
106 virtual bool canScatterAtoms() const { return _canScatterAtoms; }
107 virtual bool hasllvmProfiling() const { return _hasllvmProfiling; }
108 virtual const char* translationUnitSource() const;
109 virtual LinkerOptionsList* linkerOptions() const { return &_linkerOptions; }
110 virtual const ToolVersionList& toolVersions() const { return _toolVersions; }
111 virtual uint8_t swiftVersion() const { return _swiftVersion; }
112 virtual ld::Bitcode* getBitcode() const { return _bitcode.get(); }
113 virtual SourceKind sourceKind() const { return _srcKind; }
114
115 virtual const uint8_t* fileContent() const { return _fileContent; }
116 virtual const std::vector<AstTimeAndPath>* astFiles() const { return &_astFiles; }
117
118 void setHasllvmProfiling() { _hasllvmProfiling = true; }
119 private:
120 friend class Atom<A>;
121 friend class Section<A>;
122 friend class Parser<A>;
123 friend class CFISection<A>::OAS;
124
125 typedef typename A::P P;
126
127 const uint8_t* _fileContent;
128 Section<A>** _sectionsArray;
129 uint8_t* _atomsArray;
130 uint8_t* _aliasAtomsArray;
131 uint32_t _sectionsArrayCount;
132 uint32_t _atomsArrayCount;
133 uint32_t _aliasAtomsArrayCount;
134 std::vector<ld::Fixup> _fixups;
135 std::vector<ld::Atom::UnwindInfo> _unwindInfos;
136 std::vector<ld::Atom::LineInfo> _lineInfos;
137 std::vector<ld::relocatable::File::Stab>_stabs;
138 std::vector<AstTimeAndPath> _astFiles;
139 ld::relocatable::File::DebugInfoKind _debugInfoKind;
140 const char* _dwarfTranslationUnitPath;
141 const macho_section<P>* _dwarfDebugInfoSect;
142 const macho_section<P>* _dwarfDebugAbbrevSect;
143 const macho_section<P>* _dwarfDebugLineSect;
144 const macho_section<P>* _dwarfDebugStringSect;
145 bool _hasObjC;
146 uint8_t _swiftVersion;
147 uint32_t _cpuSubType;
148 uint32_t _minOSVersion;
149 ld::VersionSet _platforms;
150 bool _canScatterAtoms;
151 bool _hasllvmProfiling;
152 bool _objcHasCategoryClassPropertiesField;
153 std::vector<std::vector<const char*> > _linkerOptions;
154 std::unique_ptr<ld::Bitcode> _bitcode;
155 SourceKind _srcKind;
156 ToolVersionList _toolVersions;
157 };
158
159
160 template <typename A>
161 class Section : public ld::Section
162 {
163 public:
164 typedef typename A::P::uint_t pint_t;
165 typedef typename A::P P;
166 typedef typename A::P::E E;
167
168 virtual ~Section() { }
169 class File<A>& file() const { return _file; }
170 const macho_section<P>* machoSection() const { return _machOSection; }
171 uint32_t sectionNum(class Parser<A>&) const;
172 virtual ld::Atom::Alignment alignmentForAddress(pint_t addr);
173 virtual ld::Atom::ContentType contentType() { return ld::Atom::typeUnclassified; }
174 virtual bool dontDeadStrip() { return (this->_machOSection->flags() & S_ATTR_NO_DEAD_STRIP); }
175 virtual bool dontDeadStripIfReferencesLive() { return ( (this->_machOSection != NULL) && (this->_machOSection->flags() & S_ATTR_LIVE_SUPPORT) ); }
176 virtual Atom<A>* findAtomByAddress(pint_t addr) { return this->findContentAtomByAddress(addr, this->_beginAtoms, this->_endAtoms); }
177 virtual bool addFollowOnFixups() const { return ! _file.canScatterAtoms(); }
178 virtual uint32_t appendAtoms(class Parser<A>& parser, uint8_t* buffer,
179 struct Parser<A>::LabelAndCFIBreakIterator& it,
180 const struct Parser<A>::CFI_CU_InfoArrays&) = 0;
181 virtual uint32_t computeAtomCount(class Parser<A>& parser,
182 struct Parser<A>::LabelAndCFIBreakIterator& it,
183 const struct Parser<A>::CFI_CU_InfoArrays&) = 0;
184 virtual void makeFixups(class Parser<A>& parser, const struct Parser<A>::CFI_CU_InfoArrays&);
185 virtual bool addRelocFixup(class Parser<A>& parser, const macho_relocation_info<P>*);
186 virtual unsigned long contentHash(const class Atom<A>* atom, const ld::IndirectBindingTable& ind) const { return 0; }
187 virtual bool canCoalesceWith(const class Atom<A>* atom, const ld::Atom& rhs,
188 const ld::IndirectBindingTable& ind) const { return false; }
189 virtual bool ignoreLabel(const char* label) const { return false; }
190 static const char* makeSectionName(const macho_section<typename A::P>* s);
191
192 protected:
193 Section(File<A>& f, const macho_section<typename A::P>* s)
194 : ld::Section(makeSegmentName(s), makeSectionName(s), sectionType(s)),
195 _file(f), _machOSection(s), _beginAtoms(NULL), _endAtoms(NULL), _hasAliases(false) { }
196 Section(File<A>& f, const char* segName, const char* sectName, ld::Section::Type t, bool hidden=false)
197 : ld::Section(segName, sectName, t, hidden), _file(f), _machOSection(NULL),
198 _beginAtoms(NULL), _endAtoms(NULL), _hasAliases(false) { }
199
200
201 Atom<A>* findContentAtomByAddress(pint_t addr, class Atom<A>* start, class Atom<A>* end);
202 uint32_t x86_64PcRelOffset(uint8_t r_type);
203 void addLOH(class Parser<A>& parser, int kind, int count, const uint64_t addrs[]);
204 static const char* makeSegmentName(const macho_section<typename A::P>* s);
205 static bool readable(const macho_section<typename A::P>* s);
206 static bool writable(const macho_section<typename A::P>* s);
207 static bool exectuable(const macho_section<typename A::P>* s);
208 static ld::Section::Type sectionType(const macho_section<typename A::P>* s);
209
210 File<A>& _file;
211 const macho_section<P>* _machOSection;
212 class Atom<A>* _beginAtoms;
213 class Atom<A>* _endAtoms;
214 bool _hasAliases;
215 std::set<const class Atom<A>*> _altEntries;
216 };
217
218
219 template <typename A>
220 class CFISection : public Section<A>
221 {
222 public:
223 CFISection(Parser<A>& parser, File<A>& f, const macho_section<typename A::P>* s)
224 : Section<A>(f, s) { }
225 uint32_t cfiCount(Parser<A>& parser);
226
227 virtual ld::Atom::ContentType contentType() { return ld::Atom::typeCFI; }
228 virtual uint32_t computeAtomCount(class Parser<A>& parser, struct Parser<A>::LabelAndCFIBreakIterator& it, const struct Parser<A>::CFI_CU_InfoArrays&);
229 virtual uint32_t appendAtoms(class Parser<A>& parser, uint8_t* buffer, struct Parser<A>::LabelAndCFIBreakIterator& it, const struct Parser<A>::CFI_CU_InfoArrays&);
230 virtual void makeFixups(class Parser<A>& parser, const struct Parser<A>::CFI_CU_InfoArrays&);
231 virtual bool addFollowOnFixups() const { return false; }
232
233
234 ///
235 /// ObjectFileAddressSpace is used as a template parameter to UnwindCursor for parsing
236 /// dwarf CFI information in an object file.
237 ///
238 class OAS
239 {
240 public:
241 typedef typename A::P::uint_t pint_t;
242 typedef typename A::P P;
243 typedef typename A::P::E E;
244 typedef typename A::P::uint_t sint_t;
245
246 OAS(CFISection<A>& ehFrameSection, const uint8_t* ehFrameBuffer) :
247 _ehFrameSection(ehFrameSection),
248 _ehFrameContent(ehFrameBuffer),
249 _ehFrameStartAddr(ehFrameSection.machoSection()->addr()),
250 _ehFrameEndAddr(ehFrameSection.machoSection()->addr()+ehFrameSection.machoSection()->size()) {}
251
252 uint8_t get8(pint_t addr) { return *((uint8_t*)mappedAddress(addr)); }
253 uint16_t get16(pint_t addr) { return E::get16(*((uint16_t*)mappedAddress(addr))); }
254 uint32_t get32(pint_t addr) { return E::get32(*((uint32_t*)mappedAddress(addr))); }
255 uint64_t get64(pint_t addr) { return E::get64(*((uint64_t*)mappedAddress(addr))); }
256 pint_t getP(pint_t addr) { return P::getP(*((pint_t*)mappedAddress(addr))); }
257 uint64_t getULEB128(pint_t& addr, pint_t end);
258 int64_t getSLEB128(pint_t& addr, pint_t end);
259 pint_t getEncodedP(pint_t& addr, pint_t end, uint8_t encoding);
260 private:
261 const void* mappedAddress(pint_t addr);
262
263 CFISection<A>& _ehFrameSection;
264 const uint8_t* _ehFrameContent;
265 pint_t _ehFrameStartAddr;
266 pint_t _ehFrameEndAddr;
267 };
268
269
270 typedef typename A::P::uint_t pint_t;
271 typedef libunwind::CFI_Atom_Info<OAS> CFI_Atom_Info;
272
273 void cfiParse(class Parser<A>& parser, uint8_t* buffer, CFI_Atom_Info cfiArray[], uint32_t& cfiCount, const pint_t cuStarts[], uint32_t cuCount);
274 bool needsRelocating();
275
276 static bool bigEndian();
277 private:
278 void addCiePersonalityFixups(class Parser<A>& parser, const CFI_Atom_Info* cieInfo);
279 static void warnFunc(void* ref, uint64_t funcAddr, const char* msg);
280 };
281
282
283 template <typename A>
284 class CUSection : public Section<A>
285 {
286 public:
287 CUSection(Parser<A>& parser, File<A>& f, const macho_section<typename A::P>* s)
288 : Section<A>(f, s) { }
289
290 typedef typename A::P::uint_t pint_t;
291 typedef typename A::P P;
292 typedef typename A::P::E E;
293
294 virtual uint32_t computeAtomCount(class Parser<A>& parser, struct Parser<A>::LabelAndCFIBreakIterator& it, const struct Parser<A>::CFI_CU_InfoArrays&) { return 0; }
295 virtual uint32_t appendAtoms(class Parser<A>& parser, uint8_t* buffer, struct Parser<A>::LabelAndCFIBreakIterator& it, const struct Parser<A>::CFI_CU_InfoArrays&) { return 0; }
296 virtual void makeFixups(class Parser<A>& parser, const struct Parser<A>::CFI_CU_InfoArrays&);
297 virtual bool addFollowOnFixups() const { return false; }
298
299 struct Info {
300 pint_t functionStartAddress;
301 uint32_t functionSymbolIndex;
302 uint32_t rangeLength;
303 uint32_t compactUnwindInfo;
304 const char* personality;
305 pint_t lsdaAddress;
306 Atom<A>* function;
307 Atom<A>* lsda;
308 };
309
310 uint32_t count();
311 void parse(class Parser<A>& parser, uint32_t cnt, Info array[]);
312 static bool encodingMeansUseDwarf(compact_unwind_encoding_t enc);
313
314
315 private:
316
317 const char* personalityName(class Parser<A>& parser, const macho_relocation_info<P>* reloc);
318
319 static int infoSorter(const void* l, const void* r);
320
321 };
322
323
324 template <typename A>
325 class TentativeDefinitionSection : public Section<A>
326 {
327 public:
328 TentativeDefinitionSection(Parser<A>& parser, File<A>& f)
329 : Section<A>(f, "__DATA", "__comm/tent", ld::Section::typeTentativeDefs) {}
330
331 virtual ld::Atom::ContentType contentType() { return ld::Atom::typeZeroFill; }
332 virtual bool addFollowOnFixups() const { return false; }
333 virtual Atom<A>* findAtomByAddress(typename A::P::uint_t addr) { throw "TentativeDefinitionSection::findAtomByAddress() should never be called"; }
334 virtual uint32_t computeAtomCount(class Parser<A>& parser, struct Parser<A>::LabelAndCFIBreakIterator& it,
335 const struct Parser<A>::CFI_CU_InfoArrays&);
336 virtual uint32_t appendAtoms(class Parser<A>& parser, uint8_t* buffer,
337 struct Parser<A>::LabelAndCFIBreakIterator& it,
338 const struct Parser<A>::CFI_CU_InfoArrays&);
339 virtual void makeFixups(class Parser<A>& parser, const struct Parser<A>::CFI_CU_InfoArrays&) {}
340 private:
341 typedef typename A::P::uint_t pint_t;
342 typedef typename A::P P;
343 };
344
345
346 template <typename A>
347 class AbsoluteSymbolSection : public Section<A>
348 {
349 public:
350 AbsoluteSymbolSection(Parser<A>& parser, File<A>& f)
351 : Section<A>(f, "__DATA", "__abs", ld::Section::typeAbsoluteSymbols, true) {}
352
353 virtual ld::Atom::ContentType contentType() { return ld::Atom::typeUnclassified; }
354 virtual bool dontDeadStrip() { return false; }
355 virtual ld::Atom::Alignment alignmentForAddress(typename A::P::uint_t addr) { return ld::Atom::Alignment(0); }
356 virtual bool addFollowOnFixups() const { return false; }
357 virtual Atom<A>* findAtomByAddress(typename A::P::uint_t addr) { throw "AbsoluteSymbolSection::findAtomByAddress() should never be called"; }
358 virtual uint32_t computeAtomCount(class Parser<A>& parser, struct Parser<A>::LabelAndCFIBreakIterator& it,
359 const struct Parser<A>::CFI_CU_InfoArrays&);
360 virtual uint32_t appendAtoms(class Parser<A>& parser, uint8_t* buffer,
361 struct Parser<A>::LabelAndCFIBreakIterator& it,
362 const struct Parser<A>::CFI_CU_InfoArrays&);
363 virtual void makeFixups(class Parser<A>& parser, const struct Parser<A>::CFI_CU_InfoArrays&) {}
364 virtual Atom<A>* findAbsAtomForValue(typename A::P::uint_t);
365
366 private:
367 typedef typename A::P::uint_t pint_t;
368 typedef typename A::P P;
369 };
370
371
372 template <typename A>
373 class SymboledSection : public Section<A>
374 {
375 public:
376 SymboledSection(Parser<A>& parser, File<A>& f, const macho_section<typename A::P>* s);
377 virtual ld::Atom::ContentType contentType() { return _type; }
378 virtual bool dontDeadStrip();
379 virtual uint32_t computeAtomCount(class Parser<A>& parser, struct Parser<A>::LabelAndCFIBreakIterator& it,
380 const struct Parser<A>::CFI_CU_InfoArrays&);
381 virtual uint32_t appendAtoms(class Parser<A>& parser, uint8_t* buffer,
382 struct Parser<A>::LabelAndCFIBreakIterator& it,
383 const struct Parser<A>::CFI_CU_InfoArrays&);
384 protected:
385 typedef typename A::P::uint_t pint_t;
386 typedef typename A::P P;
387
388 ld::Atom::ContentType _type;
389 };
390
391
392 template <typename A>
393 class TLVDefsSection : public SymboledSection<A>
394 {
395 public:
396 TLVDefsSection(Parser<A>& parser, File<A>& f, const macho_section<typename A::P>* s) :
397 SymboledSection<A>(parser, f, s) { }
398
399 typedef typename A::P::uint_t pint_t;
400
401 virtual ld::Atom::Alignment alignmentForAddress(pint_t addr) { return ld::Atom::Alignment(log2(sizeof(pint_t))); }
402
403 private:
404
405 };
406
407
408 template <typename A>
409 class ImplicitSizeSection : public Section<A>
410 {
411 public:
412 ImplicitSizeSection(Parser<A>& parser, File<A>& f, const macho_section<typename A::P>* s)
413 : Section<A>(f, s) { }
414 virtual uint32_t computeAtomCount(class Parser<A>& parser, struct Parser<A>::LabelAndCFIBreakIterator& it, const struct Parser<A>::CFI_CU_InfoArrays&);
415 virtual uint32_t appendAtoms(class Parser<A>& parser, uint8_t* buffer, struct Parser<A>::LabelAndCFIBreakIterator& it, const struct Parser<A>::CFI_CU_InfoArrays&);
416 protected:
417 typedef typename A::P::uint_t pint_t;
418 typedef typename A::P P;
419
420 virtual bool addFollowOnFixups() const { return false; }
421 virtual const char* unlabeledAtomName(Parser<A>& parser, pint_t addr) = 0;
422 virtual ld::Atom::SymbolTableInclusion symbolTableInclusion();
423 virtual pint_t elementSizeAtAddress(pint_t addr) = 0;
424 virtual ld::Atom::Scope scopeAtAddress(Parser<A>& parser, pint_t addr) { return ld::Atom::scopeLinkageUnit; }
425 virtual bool useElementAt(Parser<A>& parser,
426 struct Parser<A>::LabelAndCFIBreakIterator& it, pint_t addr) = 0;
427 virtual ld::Atom::Definition definition() { return ld::Atom::definitionRegular; }
428 virtual ld::Atom::Combine combine(Parser<A>& parser, pint_t addr) = 0;
429 virtual bool ignoreLabel(const char* label) const { return (label[0] == 'L'); }
430 };
431
432
433 template <typename A>
434 class FixedSizeSection : public ImplicitSizeSection<A>
435 {
436 public:
437 FixedSizeSection(Parser<A>& parser, File<A>& f, const macho_section<typename A::P>* s)
438 : ImplicitSizeSection<A>(parser, f, s) { }
439 protected:
440 typedef typename A::P::uint_t pint_t;
441 typedef typename A::P P;
442 typedef typename A::P::E E;
443
444 virtual bool useElementAt(Parser<A>& parser,
445 struct Parser<A>::LabelAndCFIBreakIterator& it, pint_t addr)
446 { return true; }
447 };
448
449
450 template <typename A>
451 class Literal4Section : public FixedSizeSection<A>
452 {
453 public:
454 Literal4Section(Parser<A>& parser, File<A>& f, const macho_section<typename A::P>* s)
455 : FixedSizeSection<A>(parser, f, s) {}
456 protected:
457 typedef typename A::P::uint_t pint_t;
458 typedef typename A::P P;
459
460 virtual ld::Atom::Alignment alignmentForAddress(pint_t addr) { return ld::Atom::Alignment(2); }
461 virtual const char* unlabeledAtomName(Parser<A>&, pint_t) { return "4-byte-literal"; }
462 virtual pint_t elementSizeAtAddress(pint_t addr) { return 4; }
463 virtual ld::Atom::Combine combine(Parser<A>&, pint_t) { return ld::Atom::combineByNameAndContent; }
464 virtual unsigned long contentHash(const class Atom<A>* atom, const ld::IndirectBindingTable& ind) const;
465 virtual bool canCoalesceWith(const class Atom<A>* atom, const ld::Atom& rhs,
466 const ld::IndirectBindingTable& ind) const;
467 virtual bool ignoreLabel(const char* label) const;
468 };
469
470 template <typename A>
471 class Literal8Section : public FixedSizeSection<A>
472 {
473 public:
474 Literal8Section(Parser<A>& parser, File<A>& f, const macho_section<typename A::P>* s)
475 : FixedSizeSection<A>(parser, f, s) {}
476 protected:
477 typedef typename A::P::uint_t pint_t;
478 typedef typename A::P P;
479
480 virtual ld::Atom::Alignment alignmentForAddress(pint_t addr) { return ld::Atom::Alignment(3); }
481 virtual const char* unlabeledAtomName(Parser<A>&, pint_t) { return "8-byte-literal"; }
482 virtual pint_t elementSizeAtAddress(pint_t addr) { return 8; }
483 virtual ld::Atom::Combine combine(Parser<A>&, pint_t) { return ld::Atom::combineByNameAndContent; }
484 virtual unsigned long contentHash(const class Atom<A>* atom, const ld::IndirectBindingTable& ind) const;
485 virtual bool canCoalesceWith(const class Atom<A>* atom, const ld::Atom& rhs,
486 const ld::IndirectBindingTable& ind) const;
487 virtual bool ignoreLabel(const char* label) const;
488 };
489
490 template <typename A>
491 class Literal16Section : public FixedSizeSection<A>
492 {
493 public:
494 Literal16Section(Parser<A>& parser, File<A>& f, const macho_section<typename A::P>* s)
495 : FixedSizeSection<A>(parser, f, s) {}
496 protected:
497 typedef typename A::P::uint_t pint_t;
498 typedef typename A::P P;
499
500 virtual ld::Atom::Alignment alignmentForAddress(pint_t addr) { return ld::Atom::Alignment(4); }
501 virtual const char* unlabeledAtomName(Parser<A>&, pint_t) { return "16-byte-literal"; }
502 virtual pint_t elementSizeAtAddress(pint_t addr) { return 16; }
503 virtual ld::Atom::Combine combine(Parser<A>&, pint_t) { return ld::Atom::combineByNameAndContent; }
504 virtual unsigned long contentHash(const class Atom<A>* atom, const ld::IndirectBindingTable& ind) const;
505 virtual bool canCoalesceWith(const class Atom<A>* atom, const ld::Atom& rhs,
506 const ld::IndirectBindingTable& ind) const;
507 virtual bool ignoreLabel(const char* label) const;
508 };
509
510
511 template <typename A>
512 class NonLazyPointerSection : public FixedSizeSection<A>
513 {
514 public:
515 NonLazyPointerSection(Parser<A>& parser, File<A>& f, const macho_section<typename A::P>* s)
516 : FixedSizeSection<A>(parser, f, s) {}
517 protected:
518 typedef typename A::P::uint_t pint_t;
519 typedef typename A::P P;
520
521 virtual void makeFixups(class Parser<A>& parser, const struct Parser<A>::CFI_CU_InfoArrays&);
522 virtual ld::Atom::ContentType contentType() { return ld::Atom::typeNonLazyPointer; }
523 virtual ld::Atom::Alignment alignmentForAddress(pint_t addr) { return ld::Atom::Alignment(log2(sizeof(pint_t))); }
524 virtual const char* unlabeledAtomName(Parser<A>&, pint_t) { return "non_lazy_ptr"; }
525 virtual pint_t elementSizeAtAddress(pint_t addr) { return sizeof(pint_t); }
526 virtual ld::Atom::Scope scopeAtAddress(Parser<A>& parser, pint_t addr);
527 virtual ld::Atom::Combine combine(Parser<A>&, pint_t);
528 virtual bool ignoreLabel(const char* label) const { return true; }
529 virtual unsigned long contentHash(const class Atom<A>* atom, const ld::IndirectBindingTable& ind) const;
530 virtual bool canCoalesceWith(const class Atom<A>* atom, const ld::Atom& rhs,
531 const ld::IndirectBindingTable& ind) const;
532
533 private:
534 static const char* targetName(const class Atom<A>* atom, const ld::IndirectBindingTable& ind);
535 static ld::Fixup::Kind fixupKind();
536 };
537
538 template <typename A>
539 class TLVPointerSection : public FixedSizeSection<A>
540 {
541 public:
542 TLVPointerSection(Parser<A>& parser, File<A>& f, const macho_section<typename A::P>* s)
543 : FixedSizeSection<A>(parser, f, s) {}
544 protected:
545 typedef typename A::P::uint_t pint_t;
546 typedef typename A::P P;
547
548 virtual void makeFixups(class Parser<A>& parser, const struct Parser<A>::CFI_CU_InfoArrays&);
549 virtual ld::Atom::ContentType contentType() { return ld::Atom::typeTLVPointer; }
550 virtual ld::Atom::Alignment alignmentForAddress(pint_t addr) { return ld::Atom::Alignment(log2(sizeof(pint_t))); }
551 virtual const char* unlabeledAtomName(Parser<A>&, pint_t) { return "tlv_lazy_ptr"; }
552 virtual pint_t elementSizeAtAddress(pint_t addr) { return sizeof(pint_t); }
553 virtual ld::Atom::Combine combine(Parser<A>&, pint_t);
554 virtual bool ignoreLabel(const char* label) const { return true; }
555 virtual unsigned long contentHash(const class Atom<A>* atom, const ld::IndirectBindingTable& ind) const;
556 virtual bool canCoalesceWith(const class Atom<A>* atom, const ld::Atom& rhs,
557 const ld::IndirectBindingTable& ind) const;
558
559 private:
560 static const char* targetName(const class Atom<A>* atom, const ld::IndirectBindingTable& ind, bool* isStatic);
561 };
562
563
564 template <typename A>
565 class CFStringSection : public FixedSizeSection<A>
566 {
567 public:
568 CFStringSection(Parser<A>& parser, File<A>& f, const macho_section<typename A::P>* s)
569 : FixedSizeSection<A>(parser, f, s) {}
570 protected:
571 typedef typename A::P::uint_t pint_t;
572
573 virtual ld::Atom::Alignment alignmentForAddress(pint_t addr) { return ld::Atom::Alignment(log2(sizeof(pint_t))); }
574 virtual const char* unlabeledAtomName(Parser<A>&, pint_t) { return "CFString"; }
575 virtual pint_t elementSizeAtAddress(pint_t addr) { return 4*sizeof(pint_t); }
576 virtual ld::Atom::Combine combine(Parser<A>&, pint_t) { return ld::Atom::combineByNameAndReferences; }
577 virtual bool ignoreLabel(const char* label) const { return true; }
578 virtual unsigned long contentHash(const class Atom<A>* atom, const ld::IndirectBindingTable& ind) const;
579 virtual bool canCoalesceWith(const class Atom<A>* atom, const ld::Atom& rhs,
580 const ld::IndirectBindingTable& ind) const;
581 private:
582 enum ContentType { contentUTF8, contentUTF16, contentUnknown };
583 static const uint8_t* targetContent(const class Atom<A>* atom, const ld::IndirectBindingTable& ind,
584 ContentType* ct, unsigned int* count);
585 };
586
587
588 template <typename A>
589 class ObjC1ClassSection : public FixedSizeSection<A>
590 {
591 public:
592 ObjC1ClassSection(Parser<A>& parser, File<A>& f, const macho_section<typename A::P>* s)
593 : FixedSizeSection<A>(parser, f, s) {}
594 protected:
595 typedef typename A::P::uint_t pint_t;
596 typedef typename A::P P;
597 typedef typename A::P::E E;
598
599 virtual ld::Atom::Scope scopeAtAddress(Parser<A>& , pint_t ) { return ld::Atom::scopeGlobal; }
600 virtual ld::Atom::Alignment alignmentForAddress(pint_t addr) { return ld::Atom::Alignment(2); }
601 virtual const char* unlabeledAtomName(Parser<A>&, pint_t);
602 virtual ld::Atom::SymbolTableInclusion symbolTableInclusion() { return ld::Atom::symbolTableIn; }
603 virtual pint_t elementSizeAtAddress(pint_t addr);
604 virtual ld::Atom::Combine combine(Parser<A>&, pint_t) { return ld::Atom::combineNever; }
605 virtual bool ignoreLabel(const char* label) const { return true; }
606 virtual unsigned long contentHash(const class Atom<A>* atom, const ld::IndirectBindingTable& ind) const
607 { return 0; }
608 virtual bool canCoalesceWith(const class Atom<A>* atom, const ld::Atom& rhs,
609 const ld::IndirectBindingTable& ind) const { return false; }
610 virtual bool addRelocFixup(class Parser<A>& parser, const macho_relocation_info<P>*);
611 };
612
613
614 template <typename A>
615 class ObjC2ClassRefsSection : public FixedSizeSection<A>
616 {
617 public:
618 ObjC2ClassRefsSection(Parser<A>& parser, File<A>& f, const macho_section<typename A::P>* s)
619 : FixedSizeSection<A>(parser, f, s) {}
620 protected:
621 typedef typename A::P::uint_t pint_t;
622
623 virtual ld::Atom::Alignment alignmentForAddress(pint_t addr) { return ld::Atom::Alignment(log2(sizeof(pint_t))); }
624 virtual const char* unlabeledAtomName(Parser<A>&, pint_t) { return "objc-class-ref"; }
625 virtual pint_t elementSizeAtAddress(pint_t addr) { return sizeof(pint_t); }
626 virtual ld::Atom::Combine combine(Parser<A>&, pint_t) { return ld::Atom::combineByNameAndReferences; }
627 virtual bool ignoreLabel(const char* label) const { return true; }
628 virtual unsigned long contentHash(const class Atom<A>* atom, const ld::IndirectBindingTable& ind) const;
629 virtual bool canCoalesceWith(const class Atom<A>* atom, const ld::Atom& rhs,
630 const ld::IndirectBindingTable& ind) const;
631 private:
632 const char* targetClassName(const class Atom<A>* atom, const ld::IndirectBindingTable& ind) const;
633 };
634
635
636 template <typename A>
637 class ObjC2CategoryListSection : public FixedSizeSection<A>
638 {
639 public:
640 ObjC2CategoryListSection(Parser<A>& parser, File<A>& f, const macho_section<typename A::P>* s)
641 : FixedSizeSection<A>(parser, f, s) {}
642 protected:
643 typedef typename A::P::uint_t pint_t;
644
645 virtual ld::Atom::Alignment alignmentForAddress(pint_t addr) { return ld::Atom::Alignment(log2(sizeof(pint_t))); }
646 virtual ld::Atom::Scope scopeAtAddress(Parser<A>& parser, pint_t addr) { return ld::Atom::scopeTranslationUnit; }
647 virtual const char* unlabeledAtomName(Parser<A>&, pint_t) { return "objc-cat-list"; }
648 virtual pint_t elementSizeAtAddress(pint_t addr) { return sizeof(pint_t); }
649 virtual ld::Atom::Combine combine(Parser<A>&, pint_t) { return ld::Atom::combineNever; }
650 virtual bool ignoreLabel(const char* label) const { return true; }
651 private:
652 const char* targetClassName(const class Atom<A>* atom, const ld::IndirectBindingTable& ind) const;
653 };
654
655
656 template <typename A>
657 class PointerToCStringSection : public FixedSizeSection<A>
658 {
659 public:
660 PointerToCStringSection(Parser<A>& parser, File<A>& f, const macho_section<typename A::P>* s)
661 : FixedSizeSection<A>(parser, f, s) {}
662 protected:
663 typedef typename A::P::uint_t pint_t;
664
665 virtual ld::Atom::Alignment alignmentForAddress(pint_t addr) { return ld::Atom::Alignment(log2(sizeof(pint_t))); }
666 virtual const char* unlabeledAtomName(Parser<A>&, pint_t) { return "pointer-to-literal-cstring"; }
667 virtual pint_t elementSizeAtAddress(pint_t addr) { return sizeof(pint_t); }
668 virtual ld::Atom::Combine combine(Parser<A>&, pint_t) { return ld::Atom::combineByNameAndReferences; }
669 virtual bool ignoreLabel(const char* label) const { return true; }
670 virtual unsigned long contentHash(const class Atom<A>* atom, const ld::IndirectBindingTable& ind) const;
671 virtual bool canCoalesceWith(const class Atom<A>* atom, const ld::Atom& rhs,
672 const ld::IndirectBindingTable& ind) const;
673 virtual const char* targetCString(const class Atom<A>* atom, const ld::IndirectBindingTable& ind) const;
674 };
675
676
677 template <typename A>
678 class Objc1ClassReferences : public PointerToCStringSection<A>
679 {
680 public:
681 Objc1ClassReferences(Parser<A>& parser, File<A>& f, const macho_section<typename A::P>* s)
682 : PointerToCStringSection<A>(parser, f, s) {}
683
684 typedef typename A::P::uint_t pint_t;
685 typedef typename A::P P;
686
687 virtual const char* unlabeledAtomName(Parser<A>&, pint_t) { return "pointer-to-literal-objc-class-name"; }
688 virtual bool addRelocFixup(class Parser<A>& parser, const macho_relocation_info<P>*);
689 virtual const char* targetCString(const class Atom<A>* atom, const ld::IndirectBindingTable& ind) const;
690 };
691
692
693 template <typename A>
694 class CStringSection : public ImplicitSizeSection<A>
695 {
696 public:
697 CStringSection(Parser<A>& parser, File<A>& f, const macho_section<typename A::P>* s)
698 : ImplicitSizeSection<A>(parser, f, s) {}
699 protected:
700 typedef typename A::P::uint_t pint_t;
701 typedef typename A::P P;
702
703 virtual ld::Atom::ContentType contentType() { return ld::Atom::typeCString; }
704 virtual Atom<A>* findAtomByAddress(pint_t addr);
705 virtual const char* unlabeledAtomName(Parser<A>&, pint_t) { return "cstring"; }
706 virtual pint_t elementSizeAtAddress(pint_t addr);
707 virtual bool ignoreLabel(const char* label) const;
708 virtual bool useElementAt(Parser<A>& parser,
709 struct Parser<A>::LabelAndCFIBreakIterator& it, pint_t addr);
710 virtual ld::Atom::Combine combine(Parser<A>&, pint_t) { return ld::Atom::combineByNameAndContent; }
711 virtual unsigned long contentHash(const class Atom<A>* atom, const ld::IndirectBindingTable& ind) const;
712 virtual bool canCoalesceWith(const class Atom<A>* atom, const ld::Atom& rhs,
713 const ld::IndirectBindingTable& ind) const;
714
715 };
716
717
718 template <typename A>
719 class UTF16StringSection : public SymboledSection<A>
720 {
721 public:
722 UTF16StringSection(Parser<A>& parser, File<A>& f, const macho_section<typename A::P>* s)
723 : SymboledSection<A>(parser, f, s) {}
724 protected:
725 typedef typename A::P::uint_t pint_t;
726 typedef typename A::P P;
727
728 virtual ld::Atom::Combine combine(Parser<A>&, pint_t) { return ld::Atom::combineByNameAndContent; }
729 virtual unsigned long contentHash(const class Atom<A>* atom, const ld::IndirectBindingTable& ind) const;
730 virtual bool canCoalesceWith(const class Atom<A>* atom, const ld::Atom& rhs,
731 const ld::IndirectBindingTable& ind) const;
732 };
733
734
735 //
736 // Atoms in mach-o files
737 //
738 template <typename A>
739 class Atom : public ld::Atom
740 {
741 public:
742 // overrides of ld::Atom
743 virtual const ld::File* file() const;
744 virtual const char* translationUnitSource() const
745 { return sect().file().translationUnitSource(); }
746 virtual const char* name() const { return _name; }
747 virtual uint64_t size() const { return _size; }
748 virtual uint64_t objectAddress() const { return _objAddress; }
749 virtual void copyRawContent(uint8_t buffer[]) const;
750 virtual const uint8_t* rawContentPointer() const { return contentPointer(); }
751 virtual unsigned long contentHash(const ld::IndirectBindingTable& ind) const
752 { if ( _hash == 0 ) _hash = sect().contentHash(this, ind); return _hash; }
753 virtual bool canCoalesceWith(const ld::Atom& rhs, const ld::IndirectBindingTable& ind) const
754 { return sect().canCoalesceWith(this, rhs, ind); }
755 virtual ld::Fixup::iterator fixupsBegin() const { return &machofile()._fixups[_fixupsStartIndex]; }
756 virtual ld::Fixup::iterator fixupsEnd() const { return &machofile()._fixups[_fixupsStartIndex+_fixupsCount]; }
757 virtual ld::Atom::UnwindInfo::iterator beginUnwind() const { return &machofile()._unwindInfos[_unwindInfoStartIndex]; }
758 virtual ld::Atom::UnwindInfo::iterator endUnwind() const { return &machofile()._unwindInfos[_unwindInfoStartIndex+_unwindInfoCount]; }
759 virtual ld::Atom::LineInfo::iterator beginLineInfo() const{ return &machofile()._lineInfos[_lineInfoStartIndex]; }
760 virtual ld::Atom::LineInfo::iterator endLineInfo() const { return &machofile()._lineInfos[_lineInfoStartIndex+_lineInfoCount]; }
761 virtual void setFile(const ld::File* f);
762
763 private:
764
765 enum { kFixupStartIndexBits = 32,
766 kLineInfoStartIndexBits = 32,
767 kUnwindInfoStartIndexBits = 24,
768 kFixupCountBits = 24,
769 kLineInfoCountBits = 12,
770 kUnwindInfoCountBits = 4
771 }; // must sum to 128
772
773 public:
774 // methods for all atoms from mach-o object file
775 Section<A>& sect() const { return (Section<A>&)section(); }
776 File<A>& machofile() const { return ((Section<A>*)(this->_section))->file(); }
777 void setFixupsRange(uint32_t s, uint32_t c);
778 void setUnwindInfoRange(uint32_t s, uint32_t c);
779 void extendUnwindInfoRange();
780 void setLineInfoRange(uint32_t s, uint32_t c);
781 bool roomForMoreLineInfoCount() { return (_lineInfoCount < ((1<<kLineInfoCountBits)-1)); }
782 void incrementLineInfoCount() { assert(roomForMoreLineInfoCount()); ++_lineInfoCount; }
783 void incrementFixupCount() { if (_fixupsCount == ((1 << kFixupCountBits)-1))
784 throwf("too may fixups in %s", name()); ++_fixupsCount; }
785 const uint8_t* contentPointer() const;
786 uint32_t fixupCount() const { return _fixupsCount; }
787 void verifyAlignment(const macho_section<typename A::P>&) const;
788
789 typedef typename A::P P;
790 typedef typename A::P::E E;
791 typedef typename A::P::uint_t pint_t;
792 // constuct via all attributes
793 Atom(Section<A>& sct, const char* nm, pint_t addr, uint64_t sz,
794 ld::Atom::Definition d, ld::Atom::Combine c, ld::Atom::Scope s,
795 ld::Atom::ContentType ct, ld::Atom::SymbolTableInclusion i,
796 bool dds, bool thumb, bool al, ld::Atom::Alignment a)
797 : ld::Atom((ld::Section&)sct, d, c, s, ct, i, dds, thumb, al, a),
798 _size(sz), _objAddress(addr), _name(nm), _hash(0),
799 _fixupsStartIndex(0), _lineInfoStartIndex(0),
800 _unwindInfoStartIndex(0), _fixupsCount(0),
801 _lineInfoCount(0), _unwindInfoCount(0) { }
802 // construct via symbol table entry
803 Atom(Section<A>& sct, Parser<A>& parser, const macho_nlist<P>& sym,
804 uint64_t sz, bool alias=false)
805 : ld::Atom((ld::Section&)sct, parser.definitionFromSymbol(sym),
806 parser.combineFromSymbol(sym), parser.scopeFromSymbol(sym),
807 parser.resolverFromSymbol(sym) ? ld::Atom::typeResolver : sct.contentType(),
808 parser.inclusionFromSymbol(sym),
809 (parser.dontDeadStripFromSymbol(sym) && !sct.dontDeadStripIfReferencesLive()) || sct.dontDeadStrip(),
810 parser.isThumbFromSymbol(sym), alias,
811 sct.alignmentForAddress(sym.n_value())),
812 _size(sz), _objAddress(sym.n_value()),
813 _name(parser.nameFromSymbol(sym)), _hash(0),
814 _fixupsStartIndex(0), _lineInfoStartIndex(0),
815 _unwindInfoStartIndex(0), _fixupsCount(0),
816 _lineInfoCount(0), _unwindInfoCount(0) {
817 // <rdar://problem/6783167> support auto-hidden weak symbols
818 if ( _scope == ld::Atom::scopeGlobal &&
819 (sym.n_desc() & (N_WEAK_DEF|N_WEAK_REF)) == (N_WEAK_DEF|N_WEAK_REF) )
820 this->setAutoHide();
821 this->verifyAlignment(*sct.machoSection());
822 if ( sct.dontDeadStripIfReferencesLive() )
823 this->setDontDeadStripIfReferencesLive();
824 }
825
826 private:
827 friend class Parser<A>;
828 friend class Section<A>;
829 friend class CStringSection<A>;
830 friend class AbsoluteSymbolSection<A>;
831
832 pint_t _size;
833 pint_t _objAddress;
834 const char* _name;
835 mutable unsigned long _hash;
836
837 uint64_t _fixupsStartIndex : kFixupStartIndexBits,
838 _lineInfoStartIndex : kLineInfoStartIndexBits,
839 _unwindInfoStartIndex : kUnwindInfoStartIndexBits,
840 _fixupsCount : kFixupCountBits,
841 _lineInfoCount : kLineInfoCountBits,
842 _unwindInfoCount : kUnwindInfoCountBits;
843
844 static std::map<const ld::Atom*, const ld::File*> _s_fileOverride;
845 };
846
847 template <typename A>
848 std::map<const ld::Atom*, const ld::File*> Atom<A>::_s_fileOverride;
849
850 template <typename A>
851 void Atom<A>::setFile(const ld::File* f) {
852 _s_fileOverride[this] = f;
853 }
854
855 template <typename A>
856 const ld::File* Atom<A>::file() const
857 {
858 std::map<const ld::Atom*, const ld::File*>::iterator pos = _s_fileOverride.find(this);
859 if ( pos != _s_fileOverride.end() )
860 return pos->second;
861
862 return &sect().file();
863 }
864
865 template <typename A>
866 void Atom<A>::setFixupsRange(uint32_t startIndex, uint32_t count)
867 {
868 if ( count >= (1 << kFixupCountBits) )
869 throwf("too many fixups in function %s", this->name());
870 if ( startIndex >= (1 << kFixupStartIndexBits) )
871 throwf("too many fixups in file");
872 assert(((startIndex+count) <= sect().file()._fixups.size()) && "fixup index out of range");
873 _fixupsStartIndex = startIndex;
874 _fixupsCount = count;
875 }
876
877 template <typename A>
878 void Atom<A>::setUnwindInfoRange(uint32_t startIndex, uint32_t count)
879 {
880 if ( count >= (1 << kUnwindInfoCountBits) )
881 throwf("too many compact unwind infos in function %s", this->name());
882 if ( startIndex >= (1 << kUnwindInfoStartIndexBits) )
883 throwf("too many compact unwind infos (%d) in file", startIndex);
884 assert((startIndex+count) <= sect().file()._unwindInfos.size() && "unwindinfo index out of range");
885 _unwindInfoStartIndex = startIndex;
886 _unwindInfoCount = count;
887 }
888
889 template <typename A>
890 void Atom<A>::extendUnwindInfoRange()
891 {
892 if ( _unwindInfoCount+1 >= (1 << kUnwindInfoCountBits) )
893 throwf("too many compact unwind infos in function %s", this->name());
894 _unwindInfoCount += 1;
895 }
896
897 template <typename A>
898 void Atom<A>::setLineInfoRange(uint32_t startIndex, uint32_t count)
899 {
900 assert((count < (1 << kLineInfoCountBits)) && "too many line infos");
901 assert((startIndex+count) < sect().file()._lineInfos.size() && "line info index out of range");
902 _lineInfoStartIndex = startIndex;
903 _lineInfoCount = count;
904 }
905
906 template <typename A>
907 const uint8_t* Atom<A>::contentPointer() const
908 {
909 const macho_section<P>* sct = this->sect().machoSection();
910 if ( this->_objAddress > sct->addr() + sct->size() )
911 throwf("malformed .o file, symbol has address 0x%0llX which is outside range of its section", (uint64_t)this->_objAddress);
912 uint32_t fileOffset = sct->offset() - sct->addr() + this->_objAddress;
913 return this->sect().file().fileContent()+fileOffset;
914 }
915
916
917 template <typename A>
918 void Atom<A>::copyRawContent(uint8_t buffer[]) const
919 {
920 // copy base bytes
921 if ( this->contentType() == ld::Atom::typeZeroFill ) {
922 bzero(buffer, _size);
923 }
924 else if ( _size != 0 ) {
925 memcpy(buffer, this->contentPointer(), _size);
926 }
927 }
928
929 template <>
930 void Atom<arm>::verifyAlignment(const macho_section<P>&) const
931 {
932 if ( (this->section().type() == ld::Section::typeCode) && ! isThumb() ) {
933 if ( ((_objAddress % 4) != 0) || (this->alignment().powerOf2 < 2) )
934 warning("ARM function not 4-byte aligned: %s from %s", this->name(), this->file()->path());
935 }
936 }
937
938 #if SUPPORT_ARCH_arm64
939 template <>
940 void Atom<arm64>::verifyAlignment(const macho_section<P>& sect) const
941 {
942 if ( (this->section().type() == ld::Section::typeCode) && (sect.size() != 0) ) {
943 if ( ((_objAddress % 4) != 0) || (this->alignment().powerOf2 < 2) )
944 warning("arm64 function not 4-byte aligned: %s from %s", this->name(), this->file()->path());
945 }
946 }
947 #endif
948
949
950 template <typename A>
951 void Atom<A>::verifyAlignment(const macho_section<P>&) const
952 {
953 }
954
955
956 class AliasAtom : public ld::Atom
957 {
958 public:
959 AliasAtom(const char* name, bool hidden, const ld::File* file, const char* aliasOfName) :
960 ld::Atom(_s_section, ld::Atom::definitionRegular, ld::Atom::combineNever,
961 (hidden ? ld::Atom::scopeLinkageUnit : ld::Atom::scopeGlobal),
962 ld::Atom::typeUnclassified, ld::Atom::symbolTableIn,
963 false, false, true, 0),
964 _file(file),
965 _name(name),
966 _fixup(0, ld::Fixup::k1of1, ld::Fixup::kindNoneFollowOn, ld::Fixup::bindingByNameUnbound, aliasOfName) { }
967
968 virtual const ld::File* file() const { return _file; }
969 virtual const char* translationUnitSource() const
970 { return NULL; }
971 virtual const char* name() const { return _name; }
972 virtual uint64_t size() const { return 0; }
973 virtual uint64_t objectAddress() const { return 0; }
974 virtual void copyRawContent(uint8_t buffer[]) const { }
975 virtual ld::Fixup::iterator fixupsBegin() const { return &((ld::Fixup*)&_fixup)[0]; }
976 virtual ld::Fixup::iterator fixupsEnd() const { return &((ld::Fixup*)&_fixup)[1]; }
977
978 private:
979 static ld::Section _s_section;
980
981 const ld::File* _file;
982 const char* _name;
983 ld::Fixup _fixup;
984 };
985
986 ld::Section AliasAtom::_s_section("__LD", "__aliases", ld::Section::typeTempAlias, true);
987
988
989 template <typename A>
990 class Parser
991 {
992 public:
993 static bool validFile(const uint8_t* fileContent, bool subtypeMustMatch=false,
994 cpu_subtype_t subtype=0);
995 static const char* fileKind(const uint8_t* fileContent);
996 static ld::Platform findPlatform(const macho_header<typename A::P>* header, uint32_t* minOsVers);
997 static bool hasObjC2Categories(const uint8_t* fileContent);
998 static bool hasObjC1Categories(const uint8_t* fileContent);
999 static bool getNonLocalSymbols(const uint8_t* fileContnet, std::vector<const char*> &syms);
1000 static ld::relocatable::File* parse(const uint8_t* fileContent, uint64_t fileLength,
1001 const char* path, time_t modTime, ld::File::Ordinal ordinal,
1002 const ParserOptions& opts) {
1003 Parser p(fileContent, fileLength, path, modTime,
1004 ordinal, opts.warnUnwindConversionProblems,
1005 opts.keepDwarfUnwind, opts.forceDwarfConversion,
1006 opts.neverConvertDwarf, opts.verboseOptimizationHints,
1007 opts.ignoreMismatchPlatform);
1008 return p.parse(opts);
1009 }
1010
1011 typedef typename A::P P;
1012 typedef typename A::P::E E;
1013 typedef typename A::P::uint_t pint_t;
1014
1015 struct SourceLocation {
1016 SourceLocation() {}
1017 SourceLocation(Atom<A>* a, uint32_t o) : atom(a), offsetInAtom(o) {}
1018 Atom<A>* atom;
1019 uint32_t offsetInAtom;
1020 };
1021
1022 struct TargetDesc {
1023 Atom<A>* atom;
1024 const char* name; // only used if targetAtom is NULL
1025 int64_t addend;
1026 bool weakImport; // only used if targetAtom is NULL
1027 #if SUPPORT_ARCH_arm64e
1028 ld::Fixup::AuthData authData; // only used for authenticated pointers
1029 #endif
1030 };
1031
1032 struct FixupInAtom {
1033 FixupInAtom(const SourceLocation& src, ld::Fixup::Cluster c, ld::Fixup::Kind k, Atom<A>* target) :
1034 fixup(src.offsetInAtom, c, k, target), atom(src.atom) { src.atom->incrementFixupCount(); }
1035
1036 FixupInAtom(const SourceLocation& src, ld::Fixup::Cluster c, ld::Fixup::Kind k, ld::Fixup::TargetBinding b, Atom<A>* target) :
1037 fixup(src.offsetInAtom, c, k, b, target), atom(src.atom) { src.atom->incrementFixupCount(); }
1038
1039 FixupInAtom(const SourceLocation& src, ld::Fixup::Cluster c, ld::Fixup::Kind k, bool wi, const char* name) :
1040 fixup(src.offsetInAtom, c, k, wi, name), atom(src.atom) { src.atom->incrementFixupCount(); }
1041
1042 FixupInAtom(const SourceLocation& src, ld::Fixup::Cluster c, ld::Fixup::Kind k, ld::Fixup::TargetBinding b, const char* name) :
1043 fixup(src.offsetInAtom, c, k, b, name), atom(src.atom) { src.atom->incrementFixupCount(); }
1044
1045 FixupInAtom(const SourceLocation& src, ld::Fixup::Cluster c, ld::Fixup::Kind k, uint64_t addend) :
1046 fixup(src.offsetInAtom, c, k, addend), atom(src.atom) { src.atom->incrementFixupCount(); }
1047
1048 #if SUPPORT_ARCH_arm64e
1049 FixupInAtom(const SourceLocation& src, ld::Fixup::Cluster c, ld::Fixup::Kind k, ld::Fixup::AuthData authData) :
1050 fixup(src.offsetInAtom, c, k, authData), atom(src.atom) { src.atom->incrementFixupCount(); }
1051 #endif
1052
1053 FixupInAtom(const SourceLocation& src, ld::Fixup::Cluster c, ld::Fixup::Kind k) :
1054 fixup(src.offsetInAtom, c, k, (uint64_t)0), atom(src.atom) { src.atom->incrementFixupCount(); }
1055
1056 ld::Fixup fixup;
1057 Atom<A>* atom;
1058 };
1059
1060 void addFixup(const SourceLocation& src, ld::Fixup::Cluster c, ld::Fixup::Kind k, Atom<A>* target) {
1061 _allFixups.push_back(FixupInAtom(src, c, k, target));
1062 }
1063
1064 void addFixup(const SourceLocation& src, ld::Fixup::Cluster c, ld::Fixup::Kind k, ld::Fixup::TargetBinding b, Atom<A>* target) {
1065 _allFixups.push_back(FixupInAtom(src, c, k, b, target));
1066 }
1067
1068 void addFixup(const SourceLocation& src, ld::Fixup::Cluster c, ld::Fixup::Kind k, bool wi, const char* name) {
1069 _allFixups.push_back(FixupInAtom(src, c, k, wi, name));
1070 }
1071
1072 void addFixup(const SourceLocation& src, ld::Fixup::Cluster c, ld::Fixup::Kind k, ld::Fixup::TargetBinding b, const char* name) {
1073 _allFixups.push_back(FixupInAtom(src, c, k, b, name));
1074 }
1075
1076 void addFixup(const SourceLocation& src, ld::Fixup::Cluster c, ld::Fixup::Kind k, uint64_t addend) {
1077 _allFixups.push_back(FixupInAtom(src, c, k, addend));
1078 }
1079
1080 #if SUPPORT_ARCH_arm64e
1081 void addFixup(const SourceLocation& src, ld::Fixup::Cluster c, ld::Fixup::Kind k, ld::Fixup::AuthData authData) {
1082 _allFixups.push_back(FixupInAtom(src, c, k, authData));
1083 }
1084 #endif
1085
1086 void addFixup(const SourceLocation& src, ld::Fixup::Cluster c, ld::Fixup::Kind k) {
1087 _allFixups.push_back(FixupInAtom(src, c, k));
1088 }
1089
1090 const char* path() { return _path; }
1091 uint32_t symbolCount() { return _symbolCount; }
1092 uint32_t indirectSymbol(uint32_t indirectIndex);
1093 const macho_nlist<P>& symbolFromIndex(uint32_t index);
1094 const char* nameFromSymbol(const macho_nlist<P>& sym);
1095 ld::Atom::Scope scopeFromSymbol(const macho_nlist<P>& sym);
1096 static ld::Atom::Definition definitionFromSymbol(const macho_nlist<P>& sym);
1097 static ld::Atom::Combine combineFromSymbol(const macho_nlist<P>& sym);
1098 ld::Atom::SymbolTableInclusion inclusionFromSymbol(const macho_nlist<P>& sym);
1099 static bool dontDeadStripFromSymbol(const macho_nlist<P>& sym);
1100 static bool isThumbFromSymbol(const macho_nlist<P>& sym);
1101 static bool weakImportFromSymbol(const macho_nlist<P>& sym);
1102 static bool resolverFromSymbol(const macho_nlist<P>& sym);
1103 static bool altEntryFromSymbol(const macho_nlist<P>& sym);
1104 uint32_t symbolIndexFromIndirectSectionAddress(pint_t,const macho_section<P>*);
1105 const macho_section<P>* firstMachOSection() { return _sectionsStart; }
1106 const macho_section<P>* machOSectionFromSectionIndex(uint32_t index);
1107 uint32_t machOSectionCount() { return _machOSectionsCount; }
1108 uint32_t undefinedStartIndex() { return _undefinedStartIndex; }
1109 uint32_t undefinedEndIndex() { return _undefinedEndIndex; }
1110 void addFixup(FixupInAtom f) { _allFixups.push_back(f); }
1111 Section<A>* sectionForNum(unsigned int sectNum);
1112 Section<A>* sectionForAddress(pint_t addr);
1113 Atom<A>* findAtomByAddress(pint_t addr);
1114 Atom<A>* findAtomByAddressOrNullIfStub(pint_t addr);
1115 Atom<A>* findAtomByAddressOrLocalTargetOfStub(pint_t addr, uint32_t* offsetInAtom);
1116 Atom<A>* findAtomByName(const char* name); // slow!
1117 void findTargetFromAddress(pint_t addr, TargetDesc& target);
1118 void findTargetFromAddress(pint_t baseAddr, pint_t addr, TargetDesc& target);
1119 void findTargetFromAddressAndSectionNum(pint_t addr, unsigned int sectNum,
1120 TargetDesc& target);
1121 uint32_t tentativeDefinitionCount() { return _tentativeDefinitionCount; }
1122 uint32_t absoluteSymbolCount() { return _absoluteSymbolCount; }
1123
1124 uint32_t fileLength() const { return _fileLength; }
1125 bool hasStubsSection() { return (_stubsSectionNum != 0); }
1126 unsigned int stubsSectionNum() { return _stubsSectionNum; }
1127 void addDtraceExtraInfos(const SourceLocation& src, const char* provider);
1128 const char* scanSymbolTableForAddress(uint64_t addr);
1129 bool warnUnwindConversionProblems() { return _warnUnwindConversionProblems; }
1130 bool hasDataInCodeLabels() { return _hasDataInCodeLabels; }
1131 bool keepDwarfUnwind() { return _keepDwarfUnwind; }
1132 bool forceDwarfConversion() { return _forceDwarfConversion; }
1133 bool verboseOptimizationHints() { return _verboseOptimizationHints; }
1134 bool neverConvertDwarf() { return _neverConvertDwarf; }
1135 bool armUsesZeroCostExceptions() { return _armUsesZeroCostExceptions; }
1136 uint8_t maxDefaultCommonAlignment() { return _maxDefaultCommonAlignment; }
1137
1138
1139 macho_data_in_code_entry<P>* dataInCodeStart() { return _dataInCodeStart; }
1140 macho_data_in_code_entry<P>* dataInCodeEnd() { return _dataInCodeEnd; }
1141 const uint8_t* optimizationHintsStart() { return _lohStart; }
1142 const uint8_t* optimizationHintsEnd() { return _lohEnd; }
1143 bool hasOptimizationHints() { return _lohStart != _lohEnd; }
1144
1145
1146 void addFixups(const SourceLocation& src, ld::Fixup::Kind kind, const TargetDesc& target);
1147 void addFixups(const SourceLocation& src, ld::Fixup::Kind kind, const TargetDesc& target, const TargetDesc& picBase);
1148
1149
1150
1151 struct LabelAndCFIBreakIterator {
1152 typedef typename CFISection<A>::CFI_Atom_Info CFI_Atom_Info;
1153 LabelAndCFIBreakIterator(const uint32_t* ssa, uint32_t ssc, const pint_t* cfisa,
1154 uint32_t cfisc, bool ols)
1155 : sortedSymbolIndexes(ssa), sortedSymbolCount(ssc), cfiStartsArray(cfisa),
1156 cfiStartsCount(cfisc), fileHasOverlappingSymbols(ols),
1157 newSection(false), cfiIndex(0), symIndex(0) {}
1158 bool next(Parser<A>& parser, const Section<A>& sect, uint32_t sectNum, pint_t startAddr, pint_t endAddr,
1159 pint_t* addr, pint_t* size, const macho_nlist<P>** sym);
1160 pint_t peek(Parser<A>& parser, pint_t startAddr, pint_t endAddr);
1161 void beginSection() { newSection = true; symIndex = 0; }
1162
1163 const uint32_t* const sortedSymbolIndexes;
1164 const uint32_t sortedSymbolCount;
1165 const pint_t* cfiStartsArray;
1166 const uint32_t cfiStartsCount;
1167 const bool fileHasOverlappingSymbols;
1168 bool newSection;
1169 uint32_t cfiIndex;
1170 uint32_t symIndex;
1171 };
1172
1173 struct CFI_CU_InfoArrays {
1174 typedef typename CFISection<A>::CFI_Atom_Info CFI_Atom_Info;
1175 typedef typename CUSection<A>::Info CU_Info;
1176 CFI_CU_InfoArrays(const CFI_Atom_Info* cfiAr, uint32_t cfiC, CU_Info* cuAr, uint32_t cuC)
1177 : cfiArray(cfiAr), cuArray(cuAr), cfiCount(cfiC), cuCount(cuC) {}
1178 const CFI_Atom_Info* const cfiArray;
1179 CU_Info* const cuArray;
1180 const uint32_t cfiCount;
1181 const uint32_t cuCount;
1182 };
1183
1184
1185
1186 private:
1187 friend class Section<A>;
1188
1189 enum SectionType { sectionTypeIgnore, sectionTypeLiteral4, sectionTypeLiteral8, sectionTypeLiteral16,
1190 sectionTypeNonLazy, sectionTypeCFI, sectionTypeCString, sectionTypeCStringPointer,
1191 sectionTypeUTF16Strings, sectionTypeCFString, sectionTypeObjC2ClassRefs, typeObjC2CategoryList,
1192 sectionTypeObjC1Classes, sectionTypeSymboled, sectionTypeObjC1ClassRefs,
1193 sectionTypeTentativeDefinitions, sectionTypeAbsoluteSymbols, sectionTypeTLVDefs,
1194 sectionTypeCompactUnwind, sectionTypeTLVPointers};
1195
1196 template <typename P>
1197 struct MachOSectionAndSectionClass
1198 {
1199 const macho_section<P>* sect;
1200 SectionType type;
1201
1202 static int sorter(const void* l, const void* r) {
1203 const MachOSectionAndSectionClass<P>* left = (MachOSectionAndSectionClass<P>*)l;
1204 const MachOSectionAndSectionClass<P>* right = (MachOSectionAndSectionClass<P>*)r;
1205 int64_t diff = left->sect->addr() - right->sect->addr();
1206 if ( diff == 0 )
1207 return 0;
1208 if ( diff < 0 )
1209 return -1;
1210 else
1211 return 1;
1212 }
1213 };
1214
1215 struct ParserAndSectionsArray { Parser* parser; const uint32_t* sortedSectionsArray; };
1216
1217
1218 Parser(const uint8_t* fileContent, uint64_t fileLength,
1219 const char* path, time_t modTime, ld::File::Ordinal ordinal,
1220 bool warnUnwindConversionProblems, bool keepDwarfUnwind,
1221 bool forceDwarfConversion, bool neverConvertDwarf,
1222 bool verboseOptimizationHints, bool ignoreMismatchPlatform);
1223 ld::relocatable::File* parse(const ParserOptions& opts);
1224 static uint8_t loadCommandSizeMask();
1225 static bool useSimulatorVariant();
1226 bool parseLoadCommands(ld::VersionSet platforms, bool simulator, bool ignoreMismatchPlatform);
1227 void makeSections();
1228 void prescanSymbolTable();
1229 void makeSortedSymbolsArray(uint32_t symArray[], const uint32_t sectionArray[]);
1230 void makeSortedSectionsArray(uint32_t array[]);
1231 static int pointerSorter(const void* l, const void* r);
1232 static int symbolIndexSorter(void* extra, const void* l, const void* r);
1233 static int sectionIndexSorter(void* extra, const void* l, const void* r);
1234
1235 void parseDebugInfo();
1236 void parseStabs();
1237 void addAstFiles();
1238 void appendAliasAtoms(uint8_t* atomBuffer);
1239 static bool isConstFunStabs(const char *stabStr);
1240 bool read_comp_unit(const char ** name, const char ** comp_dir,
1241 uint64_t *stmt_list);
1242 pint_t realAddr(pint_t addr);
1243 const char* getDwarfString(uint64_t form, const uint8_t*& p);
1244 uint64_t getDwarfOffset(uint64_t form, const uint8_t*& di, bool dwarf64);
1245 bool skip_form(const uint8_t ** offset, const uint8_t * end,
1246 uint64_t form, uint8_t addr_size, bool dwarf64);
1247
1248
1249 // filled in by constructor
1250 const uint8_t* _fileContent;
1251 uint32_t _fileLength;
1252 const char* _path;
1253 time_t _modTime;
1254 ld::File::Ordinal _ordinal;
1255
1256 // filled in by parseLoadCommands()
1257 File<A>* _file;
1258 const macho_nlist<P>* _symbols;
1259 uint32_t _symbolCount;
1260 uint32_t _indirectSymbolCount;
1261 const char* _strings;
1262 uint32_t _stringsSize;
1263 const uint32_t* _indirectTable;
1264 uint32_t _indirectTableCount;
1265 uint32_t _undefinedStartIndex;
1266 uint32_t _undefinedEndIndex;
1267 const macho_section<P>* _sectionsStart;
1268 uint32_t _machOSectionsCount;
1269 bool _hasUUID;
1270 macho_data_in_code_entry<P>* _dataInCodeStart;
1271 macho_data_in_code_entry<P>* _dataInCodeEnd;
1272 const uint8_t* _lohStart;
1273 const uint8_t* _lohEnd;
1274
1275 // filled in by parse()
1276 CFISection<A>* _EHFrameSection;
1277 CUSection<A>* _compactUnwindSection;
1278 AbsoluteSymbolSection<A>* _absoluteSection;
1279 uint32_t _tentativeDefinitionCount;
1280 uint32_t _absoluteSymbolCount;
1281 uint32_t _symbolsInSections;
1282 bool _hasLongBranchStubs;
1283 bool _AppleObjc; // FSF has objc that uses different data layout
1284 bool _overlappingSymbols;
1285 bool _warnUnwindConversionProblems;
1286 bool _hasDataInCodeLabels;
1287 bool _keepDwarfUnwind;
1288 bool _forceDwarfConversion;
1289 bool _neverConvertDwarf;
1290 bool _verboseOptimizationHints;
1291 bool _armUsesZeroCostExceptions;
1292 bool _ignoreMismatchPlatform;
1293 bool _treateBitcodeAsData;
1294 bool _usingBitcode;
1295 uint8_t _maxDefaultCommonAlignment;
1296 unsigned int _stubsSectionNum;
1297 const macho_section<P>* _stubsMachOSection;
1298 std::vector<const char*> _dtraceProviderInfo;
1299 std::vector<FixupInAtom> _allFixups;
1300 #if SUPPORT_ARCH_arm64e
1301 bool _supportsAuthenticatedPointers;
1302 #endif
1303 };
1304
1305
1306
1307 template <typename A>
1308 Parser<A>::Parser(const uint8_t* fileContent, uint64_t fileLength, const char* path, time_t modTime,
1309 ld::File::Ordinal ordinal, bool convertDUI, bool keepDwarfUnwind, bool forceDwarfConversion,
1310 bool neverConvertDwarf, bool verboseOptimizationHints, bool ignoreMismatchPlatform)
1311 : _fileContent(fileContent), _fileLength(fileLength), _path(path), _modTime(modTime),
1312 _ordinal(ordinal), _file(NULL),
1313 _symbols(NULL), _symbolCount(0), _indirectSymbolCount(0), _strings(NULL), _stringsSize(0),
1314 _indirectTable(NULL), _indirectTableCount(0),
1315 _undefinedStartIndex(0), _undefinedEndIndex(0),
1316 _sectionsStart(NULL), _machOSectionsCount(0), _hasUUID(false),
1317 _dataInCodeStart(NULL), _dataInCodeEnd(NULL),
1318 _lohStart(NULL), _lohEnd(NULL),
1319 _EHFrameSection(NULL), _compactUnwindSection(NULL), _absoluteSection(NULL),
1320 _tentativeDefinitionCount(0), _absoluteSymbolCount(0),
1321 _symbolsInSections(0), _hasLongBranchStubs(false), _AppleObjc(false),
1322 _overlappingSymbols(false), _warnUnwindConversionProblems(convertDUI), _hasDataInCodeLabels(false),
1323 _keepDwarfUnwind(keepDwarfUnwind), _forceDwarfConversion(forceDwarfConversion),
1324 _neverConvertDwarf(neverConvertDwarf),
1325 _verboseOptimizationHints(verboseOptimizationHints),
1326 _ignoreMismatchPlatform(ignoreMismatchPlatform),
1327 _stubsSectionNum(0), _stubsMachOSection(NULL)
1328 {
1329 }
1330
1331
1332 template <>
1333 bool Parser<x86>::validFile(const uint8_t* fileContent, bool, cpu_subtype_t)
1334 {
1335 const macho_header<P>* header = (const macho_header<P>*)fileContent;
1336 if ( header->magic() != MH_MAGIC )
1337 return false;
1338 if ( header->cputype() != CPU_TYPE_I386 )
1339 return false;
1340 if ( header->filetype() != MH_OBJECT )
1341 return false;
1342 return true;
1343 }
1344
1345 template <>
1346 bool Parser<x86_64>::validFile(const uint8_t* fileContent, bool, cpu_subtype_t)
1347 {
1348 const macho_header<P>* header = (const macho_header<P>*)fileContent;
1349 if ( header->magic() != MH_MAGIC_64 )
1350 return false;
1351 if ( header->cputype() != CPU_TYPE_X86_64 )
1352 return false;
1353 if ( header->filetype() != MH_OBJECT )
1354 return false;
1355 return true;
1356 }
1357
1358 template <>
1359 bool Parser<arm>::validFile(const uint8_t* fileContent, bool subtypeMustMatch, cpu_subtype_t subtype)
1360 {
1361 const macho_header<P>* header = (const macho_header<P>*)fileContent;
1362 if ( header->magic() != MH_MAGIC )
1363 return false;
1364 if ( header->cputype() != CPU_TYPE_ARM )
1365 return false;
1366 if ( header->filetype() != MH_OBJECT )
1367 return false;
1368 if ( subtypeMustMatch ) {
1369 if ( (cpu_subtype_t)header->cpusubtype() == subtype )
1370 return true;
1371 // hack until libcc_kext.a is made fat
1372 if ( header->cpusubtype() == CPU_SUBTYPE_ARM_ALL )
1373 return true;
1374 return false;
1375 }
1376 return true;
1377 }
1378
1379
1380 template <>
1381 bool Parser<arm64>::validFile(const uint8_t* fileContent, bool subtypeMustMatch, cpu_subtype_t subtype)
1382 {
1383 const macho_header<P>* header = (const macho_header<P>*)fileContent;
1384 if ( header->magic() != MH_MAGIC_64 )
1385 return false;
1386 if ( header->cputype() != CPU_TYPE_ARM64 )
1387 return false;
1388 if ( header->filetype() != MH_OBJECT )
1389 return false;
1390 if ( subtypeMustMatch && (header->cpusubtype() != (uint32_t)subtype) )
1391 return false;
1392 return true;
1393 }
1394
1395
1396 template <>
1397 const char* Parser<x86>::fileKind(const uint8_t* fileContent)
1398 {
1399 const macho_header<P>* header = (const macho_header<P>*)fileContent;
1400 if ( header->magic() != MH_MAGIC )
1401 return NULL;
1402 if ( header->cputype() != CPU_TYPE_I386 )
1403 return NULL;
1404 return "i386";
1405 }
1406
1407 template <>
1408 const char* Parser<x86_64>::fileKind(const uint8_t* fileContent)
1409 {
1410 const macho_header<P>* header = (const macho_header<P>*)fileContent;
1411 if ( header->magic() != MH_MAGIC_64 )
1412 return NULL;
1413 if ( header->cputype() != CPU_TYPE_X86_64 )
1414 return NULL;
1415 return "x86_64";
1416 }
1417
1418 template <>
1419 const char* Parser<arm>::fileKind(const uint8_t* fileContent)
1420 {
1421 const macho_header<P>* header = (const macho_header<P>*)fileContent;
1422 if ( header->magic() != MH_MAGIC )
1423 return NULL;
1424 if ( header->cputype() != CPU_TYPE_ARM )
1425 return NULL;
1426 for (const ArchInfo* t=archInfoArray; t->archName != NULL; ++t) {
1427 if ( (t->cpuType == CPU_TYPE_ARM) && ((cpu_subtype_t)header->cpusubtype() == t->cpuSubType) ) {
1428 return t->archName;
1429 }
1430 }
1431 return "arm???";
1432 }
1433
1434 #if SUPPORT_ARCH_arm64
1435 template <>
1436 const char* Parser<arm64>::fileKind(const uint8_t* fileContent)
1437 {
1438 const macho_header<P>* header = (const macho_header<P>*)fileContent;
1439 if ( header->magic() != MH_MAGIC_64 )
1440 return NULL;
1441 if ( header->cputype() != CPU_TYPE_ARM64 )
1442 return NULL;
1443 return "arm64";
1444 }
1445 #endif
1446
1447
1448 template <typename A>
1449 bool Parser<A>::hasObjC2Categories(const uint8_t* fileContent)
1450 {
1451 const macho_header<P>* header = (const macho_header<P>*)fileContent;
1452 const uint32_t cmd_count = header->ncmds();
1453 const macho_load_command<P>* const cmds = (macho_load_command<P>*)((char*)header + sizeof(macho_header<P>));
1454 const macho_load_command<P>* const cmdsEnd = (macho_load_command<P>*)((char*)header + sizeof(macho_header<P>) + header->sizeofcmds());
1455 const macho_load_command<P>* cmd = cmds;
1456 for (uint32_t i = 0; i < cmd_count; ++i) {
1457 if ( cmd->cmd() == macho_segment_command<P>::CMD ) {
1458 const macho_segment_command<P>* segment = (macho_segment_command<P>*)cmd;
1459 const macho_section<P>* sectionsStart = (macho_section<P>*)((char*)segment + sizeof(macho_segment_command<P>));
1460 for (uint32_t si=0; si < segment->nsects(); ++si) {
1461 const macho_section<P>* sect = &sectionsStart[si];
1462 if ( (sect->size() > 0)
1463 && (strcmp(sect->sectname(), "__objc_catlist") == 0)
1464 && (strcmp(sect->segname(), "__DATA") == 0) ) {
1465 return true;
1466 }
1467 }
1468 }
1469 cmd = (const macho_load_command<P>*)(((char*)cmd)+cmd->cmdsize());
1470 if ( cmd > cmdsEnd )
1471 throwf("malformed mach-o file, load command #%d is outside size of load commands", i);
1472 }
1473 return false;
1474 }
1475
1476
1477 template <typename A>
1478 bool Parser<A>::hasObjC1Categories(const uint8_t* fileContent)
1479 {
1480 const macho_header<P>* header = (const macho_header<P>*)fileContent;
1481 const uint32_t cmd_count = header->ncmds();
1482 const macho_load_command<P>* const cmds = (macho_load_command<P>*)((char*)header + sizeof(macho_header<P>));
1483 const macho_load_command<P>* const cmdsEnd = (macho_load_command<P>*)((char*)header + sizeof(macho_header<P>) + header->sizeofcmds());
1484 const macho_load_command<P>* cmd = cmds;
1485 for (uint32_t i = 0; i < cmd_count; ++i) {
1486 if ( cmd->cmd() == macho_segment_command<P>::CMD ) {
1487 const macho_segment_command<P>* segment = (macho_segment_command<P>*)cmd;
1488 const macho_section<P>* sectionsStart = (macho_section<P>*)((char*)segment + sizeof(macho_segment_command<P>));
1489 for (uint32_t si=0; si < segment->nsects(); ++si) {
1490 const macho_section<P>* sect = &sectionsStart[si];
1491 if ( (sect->size() > 0)
1492 && (strcmp(sect->sectname(), "__category") == 0)
1493 && (strcmp(sect->segname(), "__OBJC") == 0) ) {
1494 return true;
1495 }
1496 }
1497 }
1498 cmd = (const macho_load_command<P>*)(((char*)cmd)+cmd->cmdsize());
1499 if ( cmd > cmdsEnd )
1500 throwf("malformed mach-o file, load command #%d is outside size of load commands", i);
1501 }
1502 return false;
1503 }
1504
1505
1506 template <typename A>
1507 bool Parser<A>::getNonLocalSymbols(const uint8_t* fileContent, std::vector<const char*> &syms)
1508 {
1509 const macho_header<P>* header = (const macho_header<P>*)fileContent;
1510 const uint32_t cmd_count = header->ncmds();
1511 const macho_load_command<P>* const cmds = (macho_load_command<P>*)((char*)header + sizeof(macho_header<P>));
1512 const macho_load_command<P>* const cmdsEnd = (macho_load_command<P>*)((char*)header + sizeof(macho_header<P>) + header->sizeofcmds());
1513 const macho_load_command<P>* cmd = cmds;
1514 for (uint32_t i = 0; i < cmd_count; ++i) {
1515 if ( cmd->cmd() == LC_SYMTAB ) {
1516 const macho_symtab_command<P>* symtab = (macho_symtab_command<P>*)cmd;
1517 uint32_t symbolCount = symtab->nsyms();
1518 const macho_nlist<P>* symbols = (const macho_nlist<P>*)(fileContent + symtab->symoff());
1519 const char* strings = (char*)fileContent + symtab->stroff();
1520 for (uint32_t j = 0; j < symbolCount; ++j) {
1521 // ignore stabs and count only ext symbols
1522 if ( (symbols[j].n_type() & N_STAB) == 0 &&
1523 (symbols[j].n_type() & N_EXT) != 0 ) {
1524 const char* symName = &strings[symbols[j].n_strx()];
1525 syms.push_back(symName);
1526 }
1527 }
1528 return true;
1529 }
1530 cmd = (const macho_load_command<P>*)(((char*)cmd)+cmd->cmdsize());
1531 if ( cmd > cmdsEnd )
1532 throwf("malformed mach-o file, load command #%d is outside size of load commands", i);
1533 }
1534 return false;
1535 }
1536
1537
1538 template <typename A>
1539 int Parser<A>::pointerSorter(const void* l, const void* r)
1540 {
1541 // sort references by address
1542 const pint_t* left = (pint_t*)l;
1543 const pint_t* right = (pint_t*)r;
1544 return (*left - *right);
1545 }
1546
1547 template <typename A>
1548 typename A::P::uint_t Parser<A>::LabelAndCFIBreakIterator::peek(Parser<A>& parser, pint_t startAddr, pint_t endAddr)
1549 {
1550 pint_t symbolAddr;
1551 if ( symIndex < sortedSymbolCount )
1552 symbolAddr = parser.symbolFromIndex(sortedSymbolIndexes[symIndex]).n_value();
1553 else
1554 symbolAddr = endAddr;
1555 pint_t cfiAddr;
1556 if ( cfiIndex < cfiStartsCount )
1557 cfiAddr = cfiStartsArray[cfiIndex];
1558 else
1559 cfiAddr = endAddr;
1560 if ( (cfiAddr < symbolAddr) && (cfiAddr >= startAddr) ) {
1561 if ( cfiAddr < endAddr )
1562 return cfiAddr;
1563 else
1564 return endAddr;
1565 }
1566 else {
1567 if ( symbolAddr < endAddr )
1568 return symbolAddr;
1569 else
1570 return endAddr;
1571 }
1572 }
1573
1574 //
1575 // Parses up a section into chunks based on labels and CFI information.
1576 // Each call returns the next chunk address and size, and (if the break
1577 // was becuase of a label, the symbol). Returns false when no more chunks.
1578 //
1579 template <typename A>
1580 bool Parser<A>::LabelAndCFIBreakIterator::next(Parser<A>& parser, const Section<A>& sect, uint32_t sectNum, pint_t startAddr, pint_t endAddr,
1581 pint_t* addr, pint_t* size, const macho_nlist<P>** symbol)
1582 {
1583 bool cfiApplicable = (sect.machoSection()->flags() & (S_ATTR_PURE_INSTRUCTIONS | S_ATTR_SOME_INSTRUCTIONS));
1584 // may not be a label on start of section, but need atom demarcation there
1585 if ( newSection ) {
1586 newSection = false;
1587 // advance symIndex until we get to the first label at or past the start of this section
1588 while ( symIndex < sortedSymbolCount ) {
1589 const macho_nlist<P>* sym = &parser.symbolFromIndex(sortedSymbolIndexes[symIndex]);
1590 // if compile threw in "ltmp*" symbol at start of section and there is another real label at same location, ignore ltmp one
1591 if ( symIndex+1 < sortedSymbolCount ) {
1592 const macho_nlist<P>* sym2 = &parser.symbolFromIndex(sortedSymbolIndexes[symIndex+1]);
1593 if ( (sym->n_sect() == sym2->n_sect()) && (sym->n_value() == sym2->n_value()) ) {
1594 if ( strncmp(parser.nameFromSymbol(*sym), "ltmp", 4) == 0 ) {
1595 ++symIndex;
1596 sym = sym2;
1597 }
1598 }
1599 }
1600 if ( ! sect.ignoreLabel(parser.nameFromSymbol(*sym)) ) {
1601 pint_t nextSymbolAddr = sym->n_value();
1602 //fprintf(stderr, "sectNum=%d, nextSymbolAddr=0x%08llX, name=%s\n", sectNum, (uint64_t)nextSymbolAddr, parser.nameFromSymbol(sym));
1603 if ( (nextSymbolAddr > startAddr) || ((nextSymbolAddr == startAddr) && (sym->n_sect() == sectNum)) )
1604 break;
1605 }
1606 ++symIndex;
1607 }
1608 if ( symIndex < sortedSymbolCount ) {
1609 const macho_nlist<P>& sym = parser.symbolFromIndex(sortedSymbolIndexes[symIndex]);
1610 pint_t nextSymbolAddr = sym.n_value();
1611 // if next symbol found is not in this section
1612 if ( sym.n_sect() != sectNum ) {
1613 // check for CFI break instead of symbol break
1614 if ( cfiIndex < cfiStartsCount ) {
1615 pint_t nextCfiAddr = cfiStartsArray[cfiIndex];
1616 if ( nextCfiAddr < endAddr ) {
1617 // use cfi
1618 ++cfiIndex;
1619 *addr = nextCfiAddr;
1620 *size = peek(parser, startAddr, endAddr) - nextCfiAddr;
1621 *symbol = NULL;
1622 return true;
1623 }
1624 }
1625 *addr = startAddr;
1626 *size = endAddr - startAddr;
1627 *symbol = NULL;
1628 if ( startAddr == endAddr )
1629 return false; // zero size section
1630 else
1631 return true; // whole section is one atom with no label
1632 }
1633 // if also CFI break here, eat it
1634 if ( cfiIndex < cfiStartsCount ) {
1635 if ( cfiStartsArray[cfiIndex] == nextSymbolAddr )
1636 ++cfiIndex;
1637 }
1638 if ( nextSymbolAddr == startAddr ) {
1639 // label at start of section, return it as chunk
1640 ++symIndex;
1641 *addr = startAddr;
1642 *size = peek(parser, startAddr, endAddr) - startAddr;
1643 *symbol = &sym;
1644 return true;
1645 }
1646 // return chunk before first symbol
1647 *addr = startAddr;
1648 *size = nextSymbolAddr - startAddr;
1649 *symbol = NULL;
1650 return true;
1651 }
1652 // no symbols in section, check CFI
1653 if ( cfiApplicable && (cfiIndex < cfiStartsCount) ) {
1654 pint_t nextCfiAddr = cfiStartsArray[cfiIndex];
1655 if ( nextCfiAddr < endAddr ) {
1656 // use cfi
1657 ++cfiIndex;
1658 *addr = nextCfiAddr;
1659 *size = peek(parser, startAddr, endAddr) - nextCfiAddr;
1660 *symbol = NULL;
1661 return true;
1662 }
1663 }
1664 // no cfi, so whole section is one chunk
1665 *addr = startAddr;
1666 *size = endAddr - startAddr;
1667 *symbol = NULL;
1668 if ( startAddr == endAddr )
1669 return false; // zero size section
1670 else
1671 return true; // whole section is one atom with no label
1672 }
1673
1674 while ( (symIndex < sortedSymbolCount) && (cfiIndex < cfiStartsCount) ) {
1675 const macho_nlist<P>& sym = parser.symbolFromIndex(sortedSymbolIndexes[symIndex]);
1676 pint_t nextSymbolAddr = sym.n_value();
1677 pint_t nextCfiAddr = cfiStartsArray[cfiIndex];
1678 if ( nextSymbolAddr < nextCfiAddr ) {
1679 if ( nextSymbolAddr >= endAddr )
1680 return false;
1681 ++symIndex;
1682 if ( nextSymbolAddr < startAddr )
1683 continue;
1684 *addr = nextSymbolAddr;
1685 *size = peek(parser, startAddr, endAddr) - nextSymbolAddr;
1686 *symbol = &sym;
1687 return true;
1688 }
1689 else if ( nextCfiAddr < nextSymbolAddr ) {
1690 if ( nextCfiAddr >= endAddr )
1691 return false;
1692 ++cfiIndex;
1693 if ( nextCfiAddr < startAddr )
1694 continue;
1695 *addr = nextCfiAddr;
1696 *size = peek(parser, startAddr, endAddr) - nextCfiAddr;
1697 *symbol = NULL;
1698 return true;
1699 }
1700 else {
1701 if ( nextCfiAddr >= endAddr )
1702 return false;
1703 ++symIndex;
1704 ++cfiIndex;
1705 if ( nextCfiAddr < startAddr )
1706 continue;
1707 *addr = nextCfiAddr;
1708 *size = peek(parser, startAddr, endAddr) - nextCfiAddr;
1709 *symbol = &sym;
1710 return true;
1711 }
1712 }
1713 while ( symIndex < sortedSymbolCount ) {
1714 const macho_nlist<P>& sym = parser.symbolFromIndex(sortedSymbolIndexes[symIndex]);
1715 pint_t nextSymbolAddr = sym.n_value();
1716 // if next symbol found is not in this section, then done with iteration
1717 if ( sym.n_sect() != sectNum )
1718 return false;
1719 ++symIndex;
1720 if ( nextSymbolAddr < startAddr )
1721 continue;
1722 *addr = nextSymbolAddr;
1723 *size = peek(parser, startAddr, endAddr) - nextSymbolAddr;
1724 *symbol = &sym;
1725 return true;
1726 }
1727 while ( cfiIndex < cfiStartsCount ) {
1728 pint_t nextCfiAddr = cfiStartsArray[cfiIndex];
1729 if ( nextCfiAddr >= endAddr )
1730 return false;
1731 ++cfiIndex;
1732 if ( nextCfiAddr < startAddr )
1733 continue;
1734 *addr = nextCfiAddr;
1735 *size = peek(parser, startAddr, endAddr) - nextCfiAddr;
1736 *symbol = NULL;
1737 return true;
1738 }
1739 return false;
1740 }
1741
1742 template <>
1743 typename arm::P::uint_t Parser<arm>::realAddr(typename arm::P::uint_t addr)
1744 {
1745 return addr & (-2);
1746 }
1747
1748 template <typename A>
1749 typename A::P::uint_t Parser<A>::realAddr(typename A::P::uint_t addr)
1750 {
1751 return addr;
1752 }
1753
1754 #define STACK_ALLOC_IF_SMALL(_type, _name, _actual_count, _maxCount) \
1755 _type* _name = NULL; \
1756 uint32_t _name##_count = 1; \
1757 uint32_t _name##_stack_count = _actual_count; \
1758 if ( _actual_count > _maxCount ) { \
1759 _name = (_type*)malloc(sizeof(_type) * _actual_count); \
1760 _name##_stack_count = 1; \
1761 } \
1762 else \
1763 _name##_count = _actual_count; \
1764 _type _name##_buffer[_name##_stack_count]; \
1765 if ( _name == NULL ) \
1766 _name = _name##_buffer;
1767
1768
1769 template <typename A>
1770 ld::relocatable::File* Parser<A>::parse(const ParserOptions& opts)
1771 {
1772 // create file object
1773 _file = new File<A>(_path, _modTime, _fileContent, _ordinal);
1774
1775 // set sourceKind
1776 _file->_srcKind = opts.srcKind;
1777 // set treatBitcodeAsData
1778 _treateBitcodeAsData = opts.treateBitcodeAsData;
1779 _usingBitcode = opts.usingBitcode;
1780
1781 #if SUPPORT_ARCH_arm64e
1782 _supportsAuthenticatedPointers = opts.supportsAuthenticatedPointers;
1783 #endif
1784
1785 // respond to -t option
1786 if ( opts.logAllFiles )
1787 printf("%s\n", _path);
1788
1789 _armUsesZeroCostExceptions = opts.armUsesZeroCostExceptions;
1790 _maxDefaultCommonAlignment = opts.maxDefaultCommonAlignment;
1791
1792 // parse start of mach-o file
1793 if ( ! parseLoadCommands(opts.platforms, opts.simulator, opts.ignoreMismatchPlatform) )
1794 return _file;
1795
1796 // make array of
1797 uint32_t sortedSectionIndexes[_machOSectionsCount];
1798 this->makeSortedSectionsArray(sortedSectionIndexes);
1799
1800 // make symbol table sorted by address
1801 this->prescanSymbolTable();
1802 uint32_t sortedSymbolIndexes[_symbolsInSections];
1803 this->makeSortedSymbolsArray(sortedSymbolIndexes, sortedSectionIndexes);
1804
1805 // allocate Section<A> object for each mach-o section
1806 makeSections();
1807
1808 // if it exists, do special early parsing of __compact_unwind section
1809 uint32_t countOfCUs = 0;
1810 if ( _compactUnwindSection != NULL )
1811 countOfCUs = _compactUnwindSection->count();
1812 // stack allocate (if not too large) cuInfoBuffer
1813 STACK_ALLOC_IF_SMALL(typename CUSection<A>::Info, cuInfoArray, countOfCUs, 1024);
1814 if ( countOfCUs != 0 )
1815 _compactUnwindSection->parse(*this, countOfCUs, cuInfoArray);
1816
1817 // create lists of address that already have compact unwind and thus don't need the dwarf parsed
1818 unsigned cuLsdaCount = 0;
1819 STACK_ALLOC_IF_SMALL(pint_t, cuStarts, countOfCUs, 1024);
1820 for (uint32_t i=0; i < countOfCUs; ++i) {
1821 if ( CUSection<A>::encodingMeansUseDwarf(cuInfoArray[i].compactUnwindInfo) )
1822 cuStarts[i] = -1;
1823 else
1824 cuStarts[i] = cuInfoArray[i].functionStartAddress;
1825 if ( cuInfoArray[i].lsdaAddress != 0 )
1826 ++cuLsdaCount;
1827 }
1828
1829
1830 // if it exists, do special early parsing of __eh_frame section
1831 // stack allocate (if not too large) array of CFI_Atom_Info
1832 uint32_t countOfCFIs = 0;
1833 if ( _EHFrameSection != NULL )
1834 countOfCFIs = _EHFrameSection->cfiCount(*this);
1835 STACK_ALLOC_IF_SMALL(typename CFISection<A>::CFI_Atom_Info, cfiArray, countOfCFIs, 1024);
1836
1837 // stack allocate (if not too large) a copy of __eh_frame to apply relocations to
1838 uint32_t sectSize = 4;
1839 if ( (countOfCFIs != 0) && _EHFrameSection->needsRelocating() )
1840 sectSize = _EHFrameSection->machoSection()->size()+4;
1841 STACK_ALLOC_IF_SMALL(uint8_t, ehBuffer, sectSize, 50*1024);
1842 uint32_t cfiStartsCount = 0;
1843 if ( countOfCFIs != 0 ) {
1844 _EHFrameSection->cfiParse(*this, ehBuffer, cfiArray, countOfCFIs, cuStarts, countOfCUs);
1845 // count functions and lsdas
1846 for(uint32_t i=0; i < countOfCFIs; ++i) {
1847 if ( cfiArray[i].isCIE )
1848 continue;
1849 //fprintf(stderr, "cfiArray[i].func = 0x%08llX, cfiArray[i].lsda = 0x%08llX, encoding=0x%08X\n",
1850 // (uint64_t)cfiArray[i].u.fdeInfo.function.targetAddress,
1851 // (uint64_t)cfiArray[i].u.fdeInfo.lsda.targetAddress,
1852 // cfiArray[i].u.fdeInfo.compactUnwindInfo);
1853 if ( cfiArray[i].u.fdeInfo.function.targetAddress != CFI_INVALID_ADDRESS )
1854 ++cfiStartsCount;
1855 if ( cfiArray[i].u.fdeInfo.lsda.targetAddress != CFI_INVALID_ADDRESS )
1856 ++cfiStartsCount;
1857 }
1858 }
1859 CFI_CU_InfoArrays cfis(cfiArray, countOfCFIs, cuInfoArray, countOfCUs);
1860
1861 // create sorted array of function starts and lsda starts
1862 pint_t cfiStartsArray[cfiStartsCount+cuLsdaCount];
1863 uint32_t countOfFDEs = 0;
1864 uint32_t cfiStartsArrayCount = 0;
1865 if ( countOfCFIs != 0 ) {
1866 for(uint32_t i=0; i < countOfCFIs; ++i) {
1867 if ( cfiArray[i].isCIE )
1868 continue;
1869 if ( cfiArray[i].u.fdeInfo.function.targetAddress != CFI_INVALID_ADDRESS )
1870 cfiStartsArray[cfiStartsArrayCount++] = realAddr(cfiArray[i].u.fdeInfo.function.targetAddress);
1871 if ( cfiArray[i].u.fdeInfo.lsda.targetAddress != CFI_INVALID_ADDRESS )
1872 cfiStartsArray[cfiStartsArrayCount++] = cfiArray[i].u.fdeInfo.lsda.targetAddress;
1873 ++countOfFDEs;
1874 }
1875 }
1876 if ( cuLsdaCount != 0 ) {
1877 // merge in an lsda info from compact unwind
1878 for (uint32_t i=0; i < countOfCUs; ++i) {
1879 if ( cuInfoArray[i].lsdaAddress == 0 )
1880 continue;
1881 // append to cfiStartsArray if not already in that list
1882 bool found = false;
1883 for(uint32_t j=0; j < cfiStartsArrayCount; ++j) {
1884 if ( cfiStartsArray[j] == cuInfoArray[i].lsdaAddress )
1885 found = true;
1886 }
1887 if ( ! found ) {
1888 cfiStartsArray[cfiStartsArrayCount++] = cuInfoArray[i].lsdaAddress;
1889 }
1890 }
1891 }
1892 if ( cfiStartsArrayCount != 0 ) {
1893 ::qsort(cfiStartsArray, cfiStartsArrayCount, sizeof(pint_t), pointerSorter);
1894 #ifndef NDEBUG
1895 // scan for FDEs claming the same function
1896 for(uint32_t i=1; i < cfiStartsArrayCount; ++i) {
1897 assert( cfiStartsArray[i] != cfiStartsArray[i-1] );
1898 }
1899 #endif
1900 }
1901
1902 Section<A>** sections = _file->_sectionsArray;
1903 uint32_t sectionsCount = _file->_sectionsArrayCount;
1904
1905 // figure out how many atoms will be allocated and allocate
1906 LabelAndCFIBreakIterator breakIterator(sortedSymbolIndexes, _symbolsInSections, cfiStartsArray,
1907 cfiStartsArrayCount, _overlappingSymbols);
1908 uint32_t computedAtomCount = 0;
1909 for (uint32_t i=0; i < sectionsCount; ++i ) {
1910 breakIterator.beginSection();
1911 uint32_t count = sections[i]->computeAtomCount(*this, breakIterator, cfis);
1912 //const macho_section<P>* sect = sections[i]->machoSection();
1913 //fprintf(stderr, "computed count=%u for section %s size=%llu\n", count, sect->sectname(), (sect != NULL) ? sect->size() : 0);
1914 computedAtomCount += count;
1915 }
1916 //fprintf(stderr, "allocating %d atoms * sizeof(Atom<A>)=%ld, sizeof(ld::Atom)=%ld\n", computedAtomCount, sizeof(Atom<A>), sizeof(ld::Atom));
1917 _file->_atomsArray = new uint8_t[computedAtomCount*sizeof(Atom<A>)];
1918 _file->_atomsArrayCount = 0;
1919
1920 // have each section append atoms to _atomsArray
1921 LabelAndCFIBreakIterator breakIterator2(sortedSymbolIndexes, _symbolsInSections, cfiStartsArray,
1922 cfiStartsArrayCount, _overlappingSymbols);
1923 for (uint32_t i=0; i < sectionsCount; ++i ) {
1924 uint8_t* atoms = _file->_atomsArray + _file->_atomsArrayCount*sizeof(Atom<A>);
1925 breakIterator2.beginSection();
1926 uint32_t count = sections[i]->appendAtoms(*this, atoms, breakIterator2, cfis);
1927 //fprintf(stderr, "append count=%u for section %s/%s\n", count, sections[i]->machoSection()->segname(), sections[i]->machoSection()->sectname());
1928 _file->_atomsArrayCount += count;
1929 }
1930 assert( _file->_atomsArrayCount == computedAtomCount && "more atoms allocated than expected");
1931
1932
1933 // have each section add all fix-ups for its atoms
1934 _allFixups.reserve(computedAtomCount*5);
1935 for (uint32_t i=0; i < sectionsCount; ++i )
1936 sections[i]->makeFixups(*this, cfis);
1937
1938 // assign fixups start offset for each atom
1939 uint8_t* p = _file->_atomsArray;
1940 uint32_t fixupOffset = 0;
1941 for(int i=_file->_atomsArrayCount; i > 0; --i) {
1942 Atom<A>* atom = (Atom<A>*)p;
1943 atom->_fixupsStartIndex = fixupOffset;
1944 fixupOffset += atom->_fixupsCount;
1945 atom->_fixupsCount = 0;
1946 p += sizeof(Atom<A>);
1947 }
1948 assert(fixupOffset == _allFixups.size());
1949 _file->_fixups.resize(fixupOffset);
1950
1951 // copy each fixup for each atom
1952 for(typename std::vector<FixupInAtom>::iterator it=_allFixups.begin(); it != _allFixups.end(); ++it) {
1953 uint32_t slot = it->atom->_fixupsStartIndex + it->atom->_fixupsCount;
1954 _file->_fixups[slot] = it->fixup;
1955 it->atom->_fixupsCount++;
1956 }
1957
1958 // done with temp vector
1959 _allFixups.clear();
1960
1961 // add unwind info
1962 _file->_unwindInfos.reserve(countOfFDEs+countOfCUs);
1963 for(uint32_t i=0; i < countOfCFIs; ++i) {
1964 if ( cfiArray[i].isCIE )
1965 continue;
1966 if ( cfiArray[i].u.fdeInfo.function.targetAddress != CFI_INVALID_ADDRESS ) {
1967 ld::Atom::UnwindInfo info;
1968 info.startOffset = 0;
1969 info.unwindInfo = cfiArray[i].u.fdeInfo.compactUnwindInfo;
1970 _file->_unwindInfos.push_back(info);
1971 Atom<A>* func = findAtomByAddress(cfiArray[i].u.fdeInfo.function.targetAddress);
1972 func->setUnwindInfoRange(_file->_unwindInfos.size()-1, 1);
1973 //fprintf(stderr, "cu from dwarf =0x%08X, atom=%s\n", info.unwindInfo, func->name());
1974 }
1975 }
1976 // apply compact infos in __LD,__compact_unwind section to each function
1977 // if function also has dwarf unwind, CU will override it
1978 Atom<A>* lastFunc = NULL;
1979 uint32_t lastEnd = 0;
1980 for(uint32_t i=0; i < countOfCUs; ++i) {
1981 typename CUSection<A>::Info* info = &cuInfoArray[i];
1982 assert(info->function != NULL);
1983 ld::Atom::UnwindInfo ui;
1984 ui.startOffset = info->functionStartAddress - info->function->objectAddress();
1985 ui.unwindInfo = info->compactUnwindInfo;
1986 _file->_unwindInfos.push_back(ui);
1987 // don't override with converted cu with "use dwarf" cu, if forcing dwarf conversion
1988 if ( !_forceDwarfConversion || !CUSection<A>::encodingMeansUseDwarf(info->compactUnwindInfo) ) {
1989 //fprintf(stderr, "cu=0x%08X, atom=%s\n", ui.unwindInfo, info->function->name());
1990 // if previous is for same function, extend range
1991 if ( info->function == lastFunc ) {
1992 if ( lastEnd != ui.startOffset ) {
1993 if ( lastEnd < ui.startOffset )
1994 warning("__LD,__compact_unwind entries for %s have a gap at offset 0x%0X", info->function->name(), lastEnd);
1995 else
1996 warning("__LD,__compact_unwind entries for %s overlap at offset 0x%0X", info->function->name(), lastEnd);
1997 }
1998 lastFunc->extendUnwindInfoRange();
1999 }
2000 else
2001 info->function->setUnwindInfoRange(_file->_unwindInfos.size()-1, 1);
2002 lastFunc = info->function;
2003 lastEnd = ui.startOffset + info->rangeLength;
2004 }
2005 }
2006
2007 // process indirect symbols which become AliasAtoms
2008 _file->_aliasAtomsArray = NULL;
2009 _file->_aliasAtomsArrayCount = 0;
2010 if ( _indirectSymbolCount != 0 ) {
2011 _file->_aliasAtomsArrayCount = _indirectSymbolCount;
2012 _file->_aliasAtomsArray = new uint8_t[_file->_aliasAtomsArrayCount*sizeof(AliasAtom)];
2013 this->appendAliasAtoms(_file->_aliasAtomsArray);
2014 }
2015
2016
2017 // parse dwarf debug info to get line info
2018 this->parseDebugInfo();
2019
2020 return _file;
2021 }
2022
2023 static void versionToString(uint32_t value, char buffer[32])
2024 {
2025 if ( value & 0xFF )
2026 sprintf(buffer, "%d.%d.%d", value >> 16, (value >> 8) & 0xFF, value & 0xFF);
2027 else
2028 sprintf(buffer, "%d.%d", value >> 16, (value >> 8) & 0xFF);
2029 }
2030
2031 template <> uint8_t Parser<x86>::loadCommandSizeMask() { return 0x03; }
2032 template <> uint8_t Parser<x86_64>::loadCommandSizeMask() { return 0x07; }
2033 template <> uint8_t Parser<arm>::loadCommandSizeMask() { return 0x03; }
2034 template <> uint8_t Parser<arm64>::loadCommandSizeMask() { return 0x07; }
2035 template <> bool Parser<x86>::useSimulatorVariant() { return true; }
2036 template <> bool Parser<x86_64>::useSimulatorVariant() { return true; }
2037 template <typename A> bool Parser<A>::useSimulatorVariant() { return false; }
2038
2039
2040 template <typename A>
2041 bool Parser<A>::parseLoadCommands(ld::VersionSet platforms, bool simulator, bool ignoreMismatchPlatform)
2042 {
2043 const macho_header<P>* header = (const macho_header<P>*)_fileContent;
2044
2045 // set File attributes
2046 _file->_canScatterAtoms = (header->flags() & MH_SUBSECTIONS_VIA_SYMBOLS);
2047 _file->_cpuSubType = header->cpusubtype();
2048
2049 const macho_segment_command<P>* segment = NULL;
2050 const uint8_t* const endOfFile = _fileContent + _fileLength;
2051 const uint32_t cmd_count = header->ncmds();
2052 // <rdar://problem/5394172> an empty .o file with zero load commands will crash linker
2053 if ( cmd_count == 0 )
2054 return false;
2055 ld::VersionSet lcPlatforms;
2056 const macho_load_command<P>* const cmds = (macho_load_command<P>*)((char*)header + sizeof(macho_header<P>));
2057 const macho_load_command<P>* const cmdsEnd = (macho_load_command<P>*)((char*)header + sizeof(macho_header<P>) + header->sizeofcmds());
2058 const macho_load_command<P>* cmd = cmds;
2059 for (uint32_t i = 0; i < cmd_count; ++i) {
2060 uint32_t size = cmd->cmdsize();
2061 if ( (size & this->loadCommandSizeMask()) != 0 )
2062 throwf("load command #%d has a unaligned size", i);
2063 const uint8_t* endOfCmd = ((uint8_t*)cmd)+cmd->cmdsize();
2064 if ( endOfCmd > (uint8_t*)cmdsEnd )
2065 throwf("load command #%d extends beyond the end of the load commands", i);
2066 if ( endOfCmd > endOfFile )
2067 throwf("load command #%d extends beyond the end of the file", i);
2068 switch (cmd->cmd()) {
2069 case LC_SYMTAB:
2070 {
2071 const macho_symtab_command<P>* symtab = (macho_symtab_command<P>*)cmd;
2072 _symbolCount = symtab->nsyms();
2073 _symbols = (const macho_nlist<P>*)(_fileContent + symtab->symoff());
2074 _strings = (char*)_fileContent + symtab->stroff();
2075 _stringsSize = symtab->strsize();
2076 if ( (symtab->symoff() + _symbolCount*sizeof(macho_nlist<P>)) > _fileLength )
2077 throw "mach-o symbol table extends beyond end of file";
2078 if ( (_strings + _stringsSize) > (char*)endOfFile )
2079 throw "mach-o string pool extends beyond end of file";
2080 if ( _indirectTable == NULL ) {
2081 if ( _undefinedEndIndex == 0 ) {
2082 _undefinedStartIndex = 0;
2083 _undefinedEndIndex = symtab->nsyms();
2084 }
2085 }
2086 }
2087 break;
2088 case LC_DYSYMTAB:
2089 {
2090 const macho_dysymtab_command<P>* dsymtab = (macho_dysymtab_command<P>*)cmd;
2091 _indirectTable = (uint32_t*)(_fileContent + dsymtab->indirectsymoff());
2092 _indirectTableCount = dsymtab->nindirectsyms();
2093 if ( &_indirectTable[_indirectTableCount] > (uint32_t*)endOfFile )
2094 throw "indirect symbol table extends beyond end of file";
2095 _undefinedStartIndex = dsymtab->iundefsym();
2096 _undefinedEndIndex = _undefinedStartIndex + dsymtab->nundefsym();
2097 }
2098 break;
2099 case LC_UUID:
2100 _hasUUID = true;
2101 break;
2102 case LC_DATA_IN_CODE:
2103 {
2104 const macho_linkedit_data_command<P>* dc = (macho_linkedit_data_command<P>*)cmd;
2105 _dataInCodeStart = (macho_data_in_code_entry<P>*)(_fileContent + dc->dataoff());
2106 _dataInCodeEnd = (macho_data_in_code_entry<P>*)(_fileContent + dc->dataoff() + dc->datasize());
2107 if ( _dataInCodeEnd > (macho_data_in_code_entry<P>*)endOfFile )
2108 throw "LC_DATA_IN_CODE table extends beyond end of file";
2109 }
2110 break;
2111 case LC_LINKER_OPTION:
2112 {
2113 const macho_linker_option_command<P>* loc = (macho_linker_option_command<P>*)cmd;
2114 const char* buffer = loc->buffer();
2115 _file->_linkerOptions.resize(_file->_linkerOptions.size() + 1);
2116 std::vector<const char*>& vec = _file->_linkerOptions.back();
2117 for (uint32_t j=0; j < loc->count(); ++j) {
2118 vec.push_back(buffer);
2119 buffer += strlen(buffer) + 1;
2120 }
2121 if ( buffer > ((char*)cmd + loc->cmdsize()) )
2122 throw "malformed LC_LINKER_OPTION";
2123 }
2124 break;
2125 case LC_LINKER_OPTIMIZATION_HINTS:
2126 {
2127 const macho_linkedit_data_command<P>* loh = (macho_linkedit_data_command<P>*)cmd;
2128 _lohStart = _fileContent + loh->dataoff();
2129 _lohEnd = _fileContent + loh->dataoff() + loh->datasize();
2130 if ( _lohEnd > endOfFile )
2131 throw "LC_LINKER_OPTIMIZATION_HINTS table extends beyond end of file";
2132 }
2133 break;
2134 case LC_VERSION_MIN_MACOSX:
2135 case LC_VERSION_MIN_IPHONEOS:
2136 case LC_VERSION_MIN_WATCHOS:
2137 case LC_VERSION_MIN_TVOS:
2138 if ( ignoreMismatchPlatform )
2139 break;
2140 lcPlatforms.add({Options::platformForLoadCommand(cmd->cmd(), useSimulatorVariant()), ((macho_version_min_command<P>*)cmd)->version()});
2141 _file->_platforms.add({Options::platformForLoadCommand(cmd->cmd(), useSimulatorVariant()), ((macho_version_min_command<P>*)cmd)->version()});
2142 break;
2143 case LC_BUILD_VERSION:
2144 {
2145 const macho_build_version_command<P>* buildVersCmd = (macho_build_version_command<P>*)cmd;
2146 if ( ignoreMismatchPlatform )
2147 break;
2148 lcPlatforms.add({(ld::Platform)buildVersCmd->platform(), buildVersCmd->minos()});
2149 _file->_platforms.add({(ld::Platform)buildVersCmd->platform(), buildVersCmd->minos()});
2150 const macho_build_tool_version<P>* entry = (macho_build_tool_version<P>*)((uint8_t*)cmd + sizeof(macho_build_version_command<P>));
2151 for (uint32_t t=0; t < buildVersCmd->ntools(); ++t) {
2152 _file->_toolVersions.push_back(std::make_pair(entry->tool(), entry->version()));
2153 ++entry;
2154 }
2155 }
2156 break;
2157 case macho_segment_command<P>::CMD:
2158 if ( segment != NULL )
2159 throw "more than one LC_SEGMENT found in object file";
2160 segment = (macho_segment_command<P>*)cmd;
2161 break;
2162 default:
2163 // ignore unknown load commands
2164 break;
2165 }
2166 cmd = (const macho_load_command<P>*)(((char*)cmd)+cmd->cmdsize());
2167 if ( cmd > cmdsEnd )
2168 throwf("malformed mach-o file, load command #%d is outside size of load commands", i);
2169 }
2170
2171 // arm/arm64 objects are default to ios platform if not set.
2172 // rdar://problem/21746314
2173 if (lcPlatforms.empty() &&
2174 (std::is_same<A, arm>::value || std::is_same<A, arm64>::value))
2175 lcPlatforms.add({ld::kPlatform_iOS,0});
2176
2177 // Check platform cross-linking.
2178 if ( !ignoreMismatchPlatform ) {
2179 __block bool warned = false;
2180 platforms.forEach(^(ld::Platform platform, uint32_t version, bool &stop) {
2181 if ( !warned && !lcPlatforms.contains(platform) ) {
2182 if (_usingBitcode)
2183 throwf("building for %s, but linking in object file built for %s,",
2184 platforms.to_str().c_str(), lcPlatforms.to_str().c_str());
2185 #if 0
2186 // FIXME: Re-enable once clang supports zippering
2187 // <rdar://problem/36749415> Turn off "urgent:" linker warning about iOSMac / macOS mismatch
2188 else
2189 warning("URGENT: building for %s, but linking in object file (%s) built for %s. "
2190 "Note: This will be an error in the future.",
2191 platforms.to_str().c_str(), path(), lcPlatforms.to_str().c_str());
2192 #endif
2193 warned = true;
2194 }
2195 if ( version && (lcPlatforms.minOS(platform) > version) ) {
2196 char t1[32];
2197 char t2[32];
2198 versionToString(lcPlatforms.minOS(platform), t1);
2199 versionToString(version, t2);
2200 warning("object file (%s) was built for newer %s version (%s) than being linked (%s)",
2201 _path, Options::platformName(platform), t1, t2);
2202 }
2203 });
2204 }
2205
2206 // validate just one segment
2207 if ( segment == NULL )
2208 throw "missing LC_SEGMENT";
2209 if ( segment->filesize() > _fileLength )
2210 throw "LC_SEGMENT filesize too large";
2211
2212 // record and validate sections
2213 _sectionsStart = (macho_section<P>*)((char*)segment + sizeof(macho_segment_command<P>));
2214 _machOSectionsCount = segment->nsects();
2215 if ( (sizeof(macho_segment_command<P>) + _machOSectionsCount * sizeof(macho_section<P>)) > segment->cmdsize() )
2216 throw "too many sections for size of LC_SEGMENT command";
2217
2218 return true;
2219 }
2220
2221 template <typename A>
2222 ld::Platform Parser<A>::findPlatform(const macho_header<P>* header, uint32_t* minOsVers)
2223 {
2224 const uint32_t cmd_count = header->ncmds();
2225 if ( cmd_count == 0 )
2226 return ld::kPlatform_unknown;
2227 const macho_load_command<P>* const cmds = (macho_load_command<P>*)((char*)header + sizeof(macho_header<P>));
2228 const macho_load_command<P>* const cmdsEnd = (macho_load_command<P>*)((char*)header + sizeof(macho_header<P>) + header->sizeofcmds());
2229 const macho_load_command<P>* cmd = cmds;
2230 for (uint32_t i = 0; i < cmd_count; ++i) {
2231 uint32_t size = cmd->cmdsize();
2232 if ( (size & loadCommandSizeMask()) != 0 )
2233 throwf("load command #%d has a unaligned size", i);
2234 const uint8_t* endOfCmd = ((uint8_t*)cmd)+cmd->cmdsize();
2235 if ( endOfCmd > (uint8_t*)cmdsEnd )
2236 throwf("load command #%d extends beyond the end of the load commands", i);
2237 const macho_version_min_command<P>* versCmd = (macho_version_min_command<P>*)cmd;
2238 const macho_build_version_command<P>* buildVersCmd = (macho_build_version_command<P>*)cmd;
2239 *minOsVers = versCmd->version();
2240 switch (cmd->cmd()) {
2241 case LC_VERSION_MIN_MACOSX:
2242 return ld::kPlatform_macOS;
2243 case LC_VERSION_MIN_IPHONEOS:
2244 if (useSimulatorVariant())
2245 return ld::kPlatform_iOSSimulator;
2246 else
2247 return ld::kPlatform_iOS;
2248 case LC_VERSION_MIN_WATCHOS:
2249 if (useSimulatorVariant())
2250 return ld::kPlatform_watchOSSimulator;
2251 else
2252 return ld::kPlatform_watchOS;
2253 case LC_VERSION_MIN_TVOS:
2254 if (useSimulatorVariant())
2255 return ld::kPlatform_tvOSSimulator;
2256 else
2257 return ld::kPlatform_tvOS;
2258 case LC_BUILD_VERSION:
2259 *minOsVers = buildVersCmd->minos();
2260 return (ld::Platform)buildVersCmd->platform();
2261 }
2262 cmd = (const macho_load_command<P>*)(((char*)cmd)+cmd->cmdsize());
2263 if ( cmd > cmdsEnd )
2264 throwf("malformed mach-o file, load command #%d is outside size of load commands", i);
2265 }
2266 *minOsVers = 0;
2267 return ld::kPlatform_unknown;
2268 }
2269
2270
2271 template <typename A>
2272 void Parser<A>::prescanSymbolTable()
2273 {
2274 _tentativeDefinitionCount = 0;
2275 _absoluteSymbolCount = 0;
2276 _symbolsInSections = 0;
2277 _hasDataInCodeLabels = false;
2278 for (uint32_t i=0; i < this->_symbolCount; ++i) {
2279 const macho_nlist<P>& sym = symbolFromIndex(i);
2280 // ignore stabs
2281 if ( (sym.n_type() & N_STAB) != 0 )
2282 continue;
2283
2284 // look at undefines
2285 const char* symbolName = this->nameFromSymbol(sym);
2286 if ( (sym.n_type() & N_TYPE) == N_UNDF ) {
2287 if ( sym.n_value() != 0 ) {
2288 // count tentative definitions
2289 ++_tentativeDefinitionCount;
2290 }
2291 else if ( strncmp(symbolName, "___dtrace_", 10) == 0 ) {
2292 // any undefined starting with __dtrace_*$ that is not ___dtrace_probe$* or ___dtrace_isenabled$*
2293 // is extra provider info
2294 if ( (strncmp(&symbolName[10], "probe$", 6) != 0) && (strncmp(&symbolName[10], "isenabled$", 10) != 0) ) {
2295 _dtraceProviderInfo.push_back(symbolName);
2296 }
2297 }
2298 continue;
2299 }
2300 else if ( ((sym.n_type() & N_TYPE) == N_INDR) && ((sym.n_type() & N_EXT) != 0) ) {
2301 _indirectSymbolCount++;
2302 continue;
2303 }
2304
2305 // count absolute symbols
2306 if ( (sym.n_type() & N_TYPE) == N_ABS ) {
2307 const char* absName = this->nameFromSymbol(sym);
2308 // ignore .objc_class_name_* symbols
2309 if ( strncmp(absName, ".objc_class_name_", 17) == 0 ) {
2310 _AppleObjc = true;
2311 continue;
2312 }
2313 // ignore .objc_class_name_* symbols
2314 if ( strncmp(absName, ".objc_category_name_", 20) == 0 )
2315 continue;
2316 // ignore empty *.eh symbols
2317 if ( strcmp(&absName[strlen(absName)-3], ".eh") == 0 )
2318 continue;
2319 ++_absoluteSymbolCount;
2320 }
2321
2322 // only look at definitions
2323 if ( (sym.n_type() & N_TYPE) != N_SECT )
2324 continue;
2325
2326 // 'L' labels do not denote atom breaks
2327 if ( symbolName[0] == 'L' ) {
2328 // <rdar://problem/9218847> Formalize data in code with L$start$ labels
2329 if ( strncmp(symbolName, "L$start$", 8) == 0 )
2330 _hasDataInCodeLabels = true;
2331 continue;
2332 }
2333 // how many def syms in each section
2334 if ( sym.n_sect() > _machOSectionsCount )
2335 throw "bad n_sect in symbol table";
2336
2337 _symbolsInSections++;
2338 }
2339 }
2340
2341 template <typename A>
2342 void Parser<A>::appendAliasAtoms(uint8_t* p)
2343 {
2344 for (uint32_t i=0; i < this->_symbolCount; ++i) {
2345 const macho_nlist<P>& sym = symbolFromIndex(i);
2346 // ignore stabs
2347 if ( (sym.n_type() & N_STAB) != 0 )
2348 continue;
2349
2350 // only look at N_INDR symbols
2351 if ( (sym.n_type() & N_TYPE) != N_INDR )
2352 continue;
2353
2354 // skip non-external aliases
2355 if ( (sym.n_type() & N_EXT) == 0 )
2356 continue;
2357
2358 const char* symbolName = this->nameFromSymbol(sym);
2359 const char* aliasOfName = &_strings[sym.n_value()];
2360 bool isHiddenVisibility = (sym.n_type() & N_PEXT);
2361 AliasAtom* allocatedSpace = (AliasAtom*)p;
2362 new (allocatedSpace) AliasAtom(symbolName, isHiddenVisibility, _file, aliasOfName);
2363 p += sizeof(AliasAtom);
2364 }
2365 }
2366
2367
2368
2369 template <typename A>
2370 int Parser<A>::sectionIndexSorter(void* extra, const void* l, const void* r)
2371 {
2372 Parser<A>* parser = (Parser<A>*)extra;
2373 const uint32_t* left = (uint32_t*)l;
2374 const uint32_t* right = (uint32_t*)r;
2375 const macho_section<P>* leftSect = parser->machOSectionFromSectionIndex(*left);
2376 const macho_section<P>* rightSect = parser->machOSectionFromSectionIndex(*right);
2377
2378 // can't just return difference because 64-bit diff does not fit in 32-bit return type
2379 int64_t result = leftSect->addr() - rightSect->addr();
2380 if ( result == 0 ) {
2381 // two sections with same start address
2382 // one with zero size goes first
2383 bool leftEmpty = ( leftSect->size() == 0 );
2384 bool rightEmpty = ( rightSect->size() == 0 );
2385 if ( leftEmpty != rightEmpty ) {
2386 return ( rightEmpty ? 1 : -1 );
2387 }
2388 if ( !leftEmpty && !rightEmpty )
2389 throwf("overlapping sections");
2390 // both empty, so chose file order
2391 return ( rightSect - leftSect );
2392 }
2393 else if ( result < 0 )
2394 return -1;
2395 else
2396 return 1;
2397 }
2398
2399 template <typename A>
2400 void Parser<A>::makeSortedSectionsArray(uint32_t array[])
2401 {
2402 const bool log = false;
2403
2404 if ( log ) {
2405 fprintf(stderr, "unsorted sections:\n");
2406 for(unsigned int i=0; i < _machOSectionsCount; ++i )
2407 fprintf(stderr, "0x%08llX %s %s\n", _sectionsStart[i].addr(), _sectionsStart[i].segname(), _sectionsStart[i].sectname());
2408 }
2409
2410 // sort by symbol table address
2411 for (uint32_t i=0; i < _machOSectionsCount; ++i)
2412 array[i] = i;
2413 ::qsort_r(array, _machOSectionsCount, sizeof(uint32_t), this, &sectionIndexSorter);
2414
2415 if ( log ) {
2416 fprintf(stderr, "sorted sections:\n");
2417 for(unsigned int i=0; i < _machOSectionsCount; ++i )
2418 fprintf(stderr, "0x%08llX %s %s\n", _sectionsStart[array[i]].addr(), _sectionsStart[array[i]].segname(), _sectionsStart[array[i]].sectname());
2419 }
2420 }
2421
2422
2423
2424 template <typename A>
2425 int Parser<A>::symbolIndexSorter(void* extra, const void* l, const void* r)
2426 {
2427 ParserAndSectionsArray* extraInfo = (ParserAndSectionsArray*)extra;
2428 Parser<A>* parser = extraInfo->parser;
2429 const uint32_t* sortedSectionsArray = extraInfo->sortedSectionsArray;
2430 const uint32_t* left = (uint32_t*)l;
2431 const uint32_t* right = (uint32_t*)r;
2432 const macho_nlist<P>& leftSym = parser->symbolFromIndex(*left);
2433 const macho_nlist<P>& rightSym = parser->symbolFromIndex(*right);
2434 // can't just return difference because 64-bit diff does not fit in 32-bit return type
2435 int64_t result = leftSym.n_value() - rightSym.n_value();
2436 if ( result == 0 ) {
2437 // two symbols with same address
2438 // if in different sections, sort earlier section first
2439 if ( leftSym.n_sect() != rightSym.n_sect() ) {
2440 for (uint32_t i=0; i < parser->machOSectionCount(); ++i) {
2441 if ( sortedSectionsArray[i]+1 == leftSym.n_sect() )
2442 return -1;
2443 if ( sortedSectionsArray[i]+1 == rightSym.n_sect() )
2444 return 1;
2445 }
2446 }
2447 // two symbols in same section, means one is an alias
2448 // if one is ltmp*, make it an alias (sort first)
2449 const char* leftName = parser->nameFromSymbol(leftSym);
2450 const char* rightName = parser->nameFromSymbol(rightSym);
2451 bool leftIsTmp = strncmp(leftName, "ltmp", 4);
2452 bool rightIsTmp = strncmp(rightName, "ltmp", 4);
2453 if ( leftIsTmp != rightIsTmp ) {
2454 return (rightIsTmp ? -1 : 1);
2455 }
2456
2457 // if only one is global, make the other an alias (sort first)
2458 if ( (leftSym.n_type() & N_EXT) != (rightSym.n_type() & N_EXT) ) {
2459 if ( (rightSym.n_type() & N_EXT) != 0 )
2460 return -1;
2461 else
2462 return 1;
2463 }
2464 // if both are global, sort alphabetically. earlier one will be the alias
2465 return ( strcmp(rightName, leftName) );
2466 }
2467 else if ( result < 0 )
2468 return -1;
2469 else
2470 return 1;
2471 }
2472
2473
2474 template <typename A>
2475 void Parser<A>::makeSortedSymbolsArray(uint32_t array[], const uint32_t sectionArray[])
2476 {
2477 const bool log = false;
2478
2479 uint32_t* p = array;
2480 for (uint32_t i=0; i < this->_symbolCount; ++i) {
2481 const macho_nlist<P>& sym = symbolFromIndex(i);
2482 // ignore stabs
2483 if ( (sym.n_type() & N_STAB) != 0 )
2484 continue;
2485
2486 // only look at definitions
2487 if ( (sym.n_type() & N_TYPE) != N_SECT )
2488 continue;
2489
2490 // 'L' labels do not denote atom breaks
2491 const char* symbolName = this->nameFromSymbol(sym);
2492 if ( symbolName[0] == 'L' )
2493 continue;
2494
2495 // how many def syms in each section
2496 if ( sym.n_sect() > _machOSectionsCount )
2497 throw "bad n_sect in symbol table";
2498
2499 // append to array
2500 *p++ = i;
2501 }
2502 assert(p == &array[_symbolsInSections] && "second pass over symbol table yield a different number of symbols");
2503
2504 // sort by symbol table address
2505 ParserAndSectionsArray extra = { this, sectionArray };
2506 ::qsort_r(array, _symbolsInSections, sizeof(uint32_t), &extra, &symbolIndexSorter);
2507
2508
2509 // look for two symbols at same address
2510 _overlappingSymbols = false;
2511 for (unsigned int i=1; i < _symbolsInSections; ++i) {
2512 if ( symbolFromIndex(array[i-1]).n_value() == symbolFromIndex(array[i]).n_value() ) {
2513 //fprintf(stderr, "overlapping symbols at 0x%08llX\n", symbolFromIndex(array[i-1]).n_value());
2514 _overlappingSymbols = true;
2515 break;
2516 }
2517 }
2518
2519 if ( log ) {
2520 fprintf(stderr, "sorted symbols:\n");
2521 for(unsigned int i=0; i < _symbolsInSections; ++i )
2522 fprintf(stderr, "0x%09llX symIndex=%d sectNum=%2d, %s\n", symbolFromIndex(array[i]).n_value(), array[i], symbolFromIndex(array[i]).n_sect(), nameFromSymbol(symbolFromIndex(array[i])) );
2523 }
2524 }
2525
2526 template <typename A>
2527 void Parser<A>::makeSections()
2528 {
2529 // classify each section by type
2530 // compute how many Section objects will be needed and total size for all
2531 unsigned int totalSectionsSize = 0;
2532 uint8_t machOSectsStorage[sizeof(MachOSectionAndSectionClass<P>)*(_machOSectionsCount+2)]; // also room for tentative-defs and absolute symbols
2533 // allocate raw storage for all section objects on stack
2534 MachOSectionAndSectionClass<P>* machOSects = (MachOSectionAndSectionClass<P>*)machOSectsStorage;
2535 unsigned int count = 0;
2536 // local variable for bitcode parsing
2537 const macho_section<P>* bitcodeSect = NULL;
2538 const macho_section<P>* cmdlineSect = NULL;
2539 const macho_section<P>* swiftCmdlineSect = NULL;
2540 const macho_section<P>* bundleSect = NULL;
2541 bool bitcodeAsm = false;
2542
2543 for (uint32_t i=0; i < _machOSectionsCount; ++i) {
2544 const macho_section<P>* sect = &_sectionsStart[i];
2545 uint8_t sectionType = (sect->flags() & SECTION_TYPE);
2546 if ( (sect->offset() + sect->size() > _fileLength) && (sectionType != S_ZEROFILL) && (sectionType != S_THREAD_LOCAL_ZEROFILL) )
2547 throwf("section %s/%s extends beyond end of file,", sect->segname(), sect->sectname());
2548
2549 if ( (sect->flags() & S_ATTR_DEBUG) != 0 ) {
2550 if ( strcmp(sect->segname(), "__DWARF") == 0 ) {
2551 // note that .o file has dwarf
2552 _file->_debugInfoKind = ld::relocatable::File::kDebugInfoDwarf;
2553 // save off iteresting dwarf sections
2554 if ( strcmp(sect->sectname(), "__debug_info") == 0 )
2555 _file->_dwarfDebugInfoSect = sect;
2556 else if ( strcmp(sect->sectname(), "__debug_abbrev") == 0 )
2557 _file->_dwarfDebugAbbrevSect = sect;
2558 else if ( strcmp(sect->sectname(), "__debug_line") == 0 )
2559 _file->_dwarfDebugLineSect = sect;
2560 else if ( strcmp(sect->sectname(), "__debug_str") == 0 )
2561 _file->_dwarfDebugStringSect = sect;
2562 // linker does not propagate dwarf sections to output file
2563 continue;
2564 }
2565 else if ( strcmp(sect->segname(), "__LD") == 0 ) {
2566 if ( strncmp(sect->sectname(), "__compact_unwind", 16) == 0 ) {
2567 machOSects[count].sect = sect;
2568 totalSectionsSize += sizeof(CUSection<A>);
2569 machOSects[count++].type = sectionTypeCompactUnwind;
2570 continue;
2571 }
2572 }
2573 }
2574 if ( strcmp(sect->segname(), "__LLVM") == 0 ) {
2575 // Process bitcode segement
2576 if ( strncmp(sect->sectname(), "__bitcode", 9) == 0 ) {
2577 bitcodeSect = sect;
2578 } else if ( strncmp(sect->sectname(), "__cmdline", 9) == 0 ) {
2579 cmdlineSect = sect;
2580 } else if ( strncmp(sect->sectname(), "__swift_cmdline", 15) == 0 ) {
2581 swiftCmdlineSect = sect;
2582 } else if ( strncmp(sect->sectname(), "__bundle", 8) == 0 ) {
2583 bundleSect = sect;
2584 } else if ( strncmp(sect->sectname(), "__asm", 5) == 0 ) {
2585 bitcodeAsm = true;
2586 }
2587 // If treat the bitcode as data, continue to parse as a normal section.
2588 if ( !_treateBitcodeAsData )
2589 continue;
2590 }
2591 // ignore empty __OBJC sections
2592 if ( (sect->size() == 0) && (strcmp(sect->segname(), "__OBJC") == 0) )
2593 continue;
2594 // objc image info section is really attributes and not content
2595 if ( ((strcmp(sect->sectname(), "__image_info") == 0) && (strcmp(sect->segname(), "__OBJC") == 0))
2596 || ((strncmp(sect->sectname(), "__objc_imageinfo", 16) == 0) && (strcmp(sect->segname(), "__DATA") == 0)) ) {
2597 // struct objc_image_info {
2598 // uint32_t version; // initially 0
2599 // uint32_t flags;
2600 // };
2601 // #define OBJC_IMAGE_SUPPORTS_GC 2
2602 // #define OBJC_IMAGE_GC_ONLY 4
2603 // #define OBJC_IMAGE_IS_SIMULATED 32
2604 // #define OBJC_IMAGE_HAS_CATEGORY_CLASS_PROPERTIES 64
2605 //
2606 const uint32_t* contents = (uint32_t*)(_file->fileContent()+sect->offset());
2607 if ( (sect->size() >= 8) && (contents[0] == 0) ) {
2608 uint32_t flags = E::get32(contents[1]);
2609 _file->_hasObjC = true;
2610 _file->_swiftVersion = ((flags >> 8) & 0xFF);
2611 _file->_objcHasCategoryClassPropertiesField = (flags & 64);
2612 if ( sect->size() > 8 ) {
2613 warning("section %s/%s has unexpectedly large size %llu in %s",
2614 sect->segname(), Section<A>::makeSectionName(sect), sect->size(), _file->path());
2615 }
2616 }
2617 else {
2618 warning("can't parse %s/%s section in %s", sect->segname(), Section<A>::makeSectionName(sect), _file->path());
2619 }
2620 continue;
2621 }
2622 machOSects[count].sect = sect;
2623 switch ( sect->flags() & SECTION_TYPE ) {
2624 case S_SYMBOL_STUBS:
2625 if ( _stubsSectionNum == 0 ) {
2626 _stubsSectionNum = i+1;
2627 _stubsMachOSection = sect;
2628 }
2629 else
2630 assert(1 && "multiple S_SYMBOL_STUBS sections");
2631 case S_LAZY_SYMBOL_POINTERS:
2632 break;
2633 case S_4BYTE_LITERALS:
2634 totalSectionsSize += sizeof(Literal4Section<A>);
2635 machOSects[count++].type = sectionTypeLiteral4;
2636 break;
2637 case S_8BYTE_LITERALS:
2638 totalSectionsSize += sizeof(Literal8Section<A>);
2639 machOSects[count++].type = sectionTypeLiteral8;
2640 break;
2641 case S_16BYTE_LITERALS:
2642 totalSectionsSize += sizeof(Literal16Section<A>);
2643 machOSects[count++].type = sectionTypeLiteral16;
2644 break;
2645 case S_NON_LAZY_SYMBOL_POINTERS:
2646 totalSectionsSize += sizeof(NonLazyPointerSection<A>);
2647 machOSects[count++].type = sectionTypeNonLazy;
2648 break;
2649 case S_THREAD_LOCAL_VARIABLE_POINTERS:
2650 totalSectionsSize += sizeof(TLVPointerSection<A>);
2651 machOSects[count++].type = sectionTypeTLVPointers;
2652 break;
2653 case S_LITERAL_POINTERS:
2654 if ( (strcmp(sect->segname(), "__OBJC") == 0) && (strcmp(sect->sectname(), "__cls_refs") == 0) ) {
2655 totalSectionsSize += sizeof(Objc1ClassReferences<A>);
2656 machOSects[count++].type = sectionTypeObjC1ClassRefs;
2657 }
2658 else {
2659 totalSectionsSize += sizeof(PointerToCStringSection<A>);
2660 machOSects[count++].type = sectionTypeCStringPointer;
2661 }
2662 break;
2663 case S_CSTRING_LITERALS:
2664 totalSectionsSize += sizeof(CStringSection<A>);
2665 machOSects[count++].type = sectionTypeCString;
2666 break;
2667 case S_MOD_INIT_FUNC_POINTERS:
2668 case S_MOD_TERM_FUNC_POINTERS:
2669 case S_THREAD_LOCAL_INIT_FUNCTION_POINTERS:
2670 case S_INTERPOSING:
2671 case S_ZEROFILL:
2672 case S_REGULAR:
2673 case S_COALESCED:
2674 case S_THREAD_LOCAL_REGULAR:
2675 case S_THREAD_LOCAL_ZEROFILL:
2676 if ( (strcmp(sect->segname(), "__TEXT") == 0) && (strcmp(sect->sectname(), "__eh_frame") == 0) ) {
2677 totalSectionsSize += sizeof(CFISection<A>);
2678 machOSects[count++].type = sectionTypeCFI;
2679 }
2680 else if ( (strcmp(sect->segname(), "__DATA") == 0) && (strcmp(sect->sectname(), "__cfstring") == 0) ) {
2681 totalSectionsSize += sizeof(CFStringSection<A>);
2682 machOSects[count++].type = sectionTypeCFString;
2683 }
2684 else if ( (strcmp(sect->segname(), "__TEXT") == 0) && (strcmp(sect->sectname(), "__ustring") == 0) ) {
2685 totalSectionsSize += sizeof(UTF16StringSection<A>);
2686 machOSects[count++].type = sectionTypeUTF16Strings;
2687 }
2688 else if ( (strcmp(sect->segname(), "__DATA") == 0) && (strncmp(sect->sectname(), "__objc_classrefs", 16) == 0) ) {
2689 totalSectionsSize += sizeof(ObjC2ClassRefsSection<A>);
2690 machOSects[count++].type = sectionTypeObjC2ClassRefs;
2691 }
2692 else if ( (strcmp(sect->segname(), "__DATA") == 0) && (strcmp(sect->sectname(), "__objc_catlist") == 0) ) {
2693 totalSectionsSize += sizeof(ObjC2CategoryListSection<A>);
2694 machOSects[count++].type = typeObjC2CategoryList;
2695 }
2696 else if ( _AppleObjc && (strcmp(sect->segname(), "__OBJC") == 0) && (strcmp(sect->sectname(), "__class") == 0) ) {
2697 totalSectionsSize += sizeof(ObjC1ClassSection<A>);
2698 machOSects[count++].type = sectionTypeObjC1Classes;
2699 }
2700 else {
2701 totalSectionsSize += sizeof(SymboledSection<A>);
2702 machOSects[count++].type = sectionTypeSymboled;
2703 }
2704 break;
2705 case S_THREAD_LOCAL_VARIABLES:
2706 totalSectionsSize += sizeof(TLVDefsSection<A>);
2707 machOSects[count++].type = sectionTypeTLVDefs;
2708 break;
2709 default:
2710 throwf("unknown section type %d", sect->flags() & SECTION_TYPE);
2711 }
2712 }
2713
2714 // Create bitcode
2715 if ( bitcodeSect != NULL ) {
2716 if ( cmdlineSect != NULL )
2717 _file->_bitcode = std::unique_ptr<ld::Bitcode>(new ld::ClangBitcode(&_fileContent[bitcodeSect->offset()], bitcodeSect->size(),
2718 &_fileContent[cmdlineSect->offset()], cmdlineSect->size()));
2719 else if ( swiftCmdlineSect != NULL )
2720 _file->_bitcode = std::unique_ptr<ld::Bitcode>(new ld::SwiftBitcode(&_fileContent[bitcodeSect->offset()], bitcodeSect->size(),
2721 &_fileContent[swiftCmdlineSect->offset()], swiftCmdlineSect->size()));
2722 else
2723 throwf("Object file with bitcode missing cmdline options: %s", _file->path());
2724 }
2725 else if ( bundleSect != NULL )
2726 _file->_bitcode = std::unique_ptr<ld::Bitcode>(new ld::BundleBitcode(&_fileContent[bundleSect->offset()], bundleSect->size()));
2727 else if ( bitcodeAsm )
2728 _file->_bitcode = std::unique_ptr<ld::Bitcode>(new ld::AsmBitcode(_fileContent, _fileLength));
2729
2730 // sort by address (mach-o object files don't aways have sections sorted)
2731 ::qsort(machOSects, count, sizeof(MachOSectionAndSectionClass<P>), MachOSectionAndSectionClass<P>::sorter);
2732
2733 // we will synthesize a dummy Section<A> object for tentative definitions
2734 if ( _tentativeDefinitionCount > 0 ) {
2735 totalSectionsSize += sizeof(TentativeDefinitionSection<A>);
2736 machOSects[count++].type = sectionTypeTentativeDefinitions;
2737 }
2738
2739 // we will synthesize a dummy Section<A> object for Absolute symbols
2740 if ( _absoluteSymbolCount > 0 ) {
2741 totalSectionsSize += sizeof(AbsoluteSymbolSection<A>);
2742 machOSects[count++].type = sectionTypeAbsoluteSymbols;
2743 }
2744
2745 // allocate one block for all Section objects as well as pointers to each
2746 uint8_t* space = new uint8_t[totalSectionsSize+count*sizeof(Section<A>*)];
2747 _file->_sectionsArray = (Section<A>**)space;
2748 _file->_sectionsArrayCount = count;
2749 Section<A>** objects = _file->_sectionsArray;
2750 space += count*sizeof(Section<A>*);
2751 for (uint32_t i=0; i < count; ++i) {
2752 switch ( machOSects[i].type ) {
2753 case sectionTypeIgnore:
2754 break;
2755 case sectionTypeLiteral4:
2756 *objects++ = new (space) Literal4Section<A>(*this, *_file, machOSects[i].sect);
2757 space += sizeof(Literal4Section<A>);
2758 break;
2759 case sectionTypeLiteral8:
2760 *objects++ = new (space) Literal8Section<A>(*this, *_file, machOSects[i].sect);
2761 space += sizeof(Literal8Section<A>);
2762 break;
2763 case sectionTypeLiteral16:
2764 *objects++ = new (space) Literal16Section<A>(*this, *_file, machOSects[i].sect);
2765 space += sizeof(Literal16Section<A>);
2766 break;
2767 case sectionTypeNonLazy:
2768 *objects++ = new (space) NonLazyPointerSection<A>(*this, *_file, machOSects[i].sect);
2769 space += sizeof(NonLazyPointerSection<A>);
2770 break;
2771 case sectionTypeTLVPointers:
2772 *objects++ = new (space) TLVPointerSection<A>(*this, *_file, machOSects[i].sect);
2773 space += sizeof(TLVPointerSection<A>);
2774 break;
2775 case sectionTypeCFI:
2776 _EHFrameSection = new (space) CFISection<A>(*this, *_file, machOSects[i].sect);
2777 *objects++ = _EHFrameSection;
2778 space += sizeof(CFISection<A>);
2779 break;
2780 case sectionTypeCString:
2781 *objects++ = new (space) CStringSection<A>(*this, *_file, machOSects[i].sect);
2782 space += sizeof(CStringSection<A>);
2783 break;
2784 case sectionTypeCStringPointer:
2785 *objects++ = new (space) PointerToCStringSection<A>(*this, *_file, machOSects[i].sect);
2786 space += sizeof(PointerToCStringSection<A>);
2787 break;
2788 case sectionTypeObjC1ClassRefs:
2789 *objects++ = new (space) Objc1ClassReferences<A>(*this, *_file, machOSects[i].sect);
2790 space += sizeof(Objc1ClassReferences<A>);
2791 break;
2792 case sectionTypeUTF16Strings:
2793 *objects++ = new (space) UTF16StringSection<A>(*this, *_file, machOSects[i].sect);
2794 space += sizeof(UTF16StringSection<A>);
2795 break;
2796 case sectionTypeCFString:
2797 *objects++ = new (space) CFStringSection<A>(*this, *_file, machOSects[i].sect);
2798 space += sizeof(CFStringSection<A>);
2799 break;
2800 case sectionTypeObjC2ClassRefs:
2801 *objects++ = new (space) ObjC2ClassRefsSection<A>(*this, *_file, machOSects[i].sect);
2802 space += sizeof(ObjC2ClassRefsSection<A>);
2803 break;
2804 case typeObjC2CategoryList:
2805 *objects++ = new (space) ObjC2CategoryListSection<A>(*this, *_file, machOSects[i].sect);
2806 space += sizeof(ObjC2CategoryListSection<A>);
2807 break;
2808 case sectionTypeObjC1Classes:
2809 *objects++ = new (space) ObjC1ClassSection<A>(*this, *_file, machOSects[i].sect);
2810 space += sizeof(ObjC1ClassSection<A>);
2811 break;
2812 case sectionTypeSymboled:
2813 *objects++ = new (space) SymboledSection<A>(*this, *_file, machOSects[i].sect);
2814 space += sizeof(SymboledSection<A>);
2815 break;
2816 case sectionTypeTLVDefs:
2817 *objects++ = new (space) TLVDefsSection<A>(*this, *_file, machOSects[i].sect);
2818 space += sizeof(TLVDefsSection<A>);
2819 break;
2820 case sectionTypeCompactUnwind:
2821 _compactUnwindSection = new (space) CUSection<A>(*this, *_file, machOSects[i].sect);
2822 *objects++ = _compactUnwindSection;
2823 space += sizeof(CUSection<A>);
2824 break;
2825 case sectionTypeTentativeDefinitions:
2826 *objects++ = new (space) TentativeDefinitionSection<A>(*this, *_file);
2827 space += sizeof(TentativeDefinitionSection<A>);
2828 break;
2829 case sectionTypeAbsoluteSymbols:
2830 _absoluteSection = new (space) AbsoluteSymbolSection<A>(*this, *_file);
2831 *objects++ = _absoluteSection;
2832 space += sizeof(AbsoluteSymbolSection<A>);
2833 break;
2834 default:
2835 throw "internal error uknown SectionType";
2836 }
2837 }
2838 }
2839
2840
2841 template <typename A>
2842 Section<A>* Parser<A>::sectionForAddress(typename A::P::uint_t addr)
2843 {
2844 for (uint32_t i=0; i < _file->_sectionsArrayCount; ++i ) {
2845 const macho_section<typename A::P>* sect = _file->_sectionsArray[i]->machoSection();
2846 // TentativeDefinitionSection and AbsoluteSymbolSection have no mach-o section
2847 if ( sect != NULL ) {
2848 if ( (sect->addr() <= addr) && (addr < (sect->addr()+sect->size())) ) {
2849 return _file->_sectionsArray[i];
2850 }
2851 }
2852 }
2853 // not strictly in any section
2854 // may be in a zero length section
2855 for (uint32_t i=0; i < _file->_sectionsArrayCount; ++i ) {
2856 const macho_section<typename A::P>* sect = _file->_sectionsArray[i]->machoSection();
2857 // TentativeDefinitionSection and AbsoluteSymbolSection have no mach-o section
2858 if ( sect != NULL ) {
2859 if ( (sect->addr() == addr) && (sect->size() == 0) ) {
2860 return _file->_sectionsArray[i];
2861 }
2862 }
2863 }
2864
2865 throwf("sectionForAddress(0x%llX) address not in any section", (uint64_t)addr);
2866 }
2867
2868 template <typename A>
2869 Section<A>* Parser<A>::sectionForNum(unsigned int num)
2870 {
2871 for (uint32_t i=0; i < _file->_sectionsArrayCount; ++i ) {
2872 const macho_section<typename A::P>* sect = _file->_sectionsArray[i]->machoSection();
2873 // TentativeDefinitionSection and AbsoluteSymbolSection have no mach-o section
2874 if ( sect != NULL ) {
2875 if ( num == (unsigned int)((sect - _sectionsStart)+1) )
2876 return _file->_sectionsArray[i];
2877 }
2878 }
2879 throwf("sectionForNum(%u) section number not for any section", num);
2880 }
2881
2882 template <typename A>
2883 Atom<A>* Parser<A>::findAtomByAddress(pint_t addr)
2884 {
2885 Section<A>* section = this->sectionForAddress(addr);
2886 return section->findAtomByAddress(addr);
2887 }
2888
2889 template <typename A>
2890 Atom<A>* Parser<A>::findAtomByAddressOrNullIfStub(pint_t addr)
2891 {
2892 if ( hasStubsSection() && (_stubsMachOSection->addr() <= addr) && (addr < (_stubsMachOSection->addr()+_stubsMachOSection->size())) )
2893 return NULL;
2894 return findAtomByAddress(addr);
2895 }
2896
2897 template <typename A>
2898 Atom<A>* Parser<A>::findAtomByAddressOrLocalTargetOfStub(pint_t addr, uint32_t* offsetInAtom)
2899 {
2900 if ( hasStubsSection() && (_stubsMachOSection->addr() <= addr) && (addr < (_stubsMachOSection->addr()+_stubsMachOSection->size())) ) {
2901 // target is a stub, remove indirection
2902 uint32_t symbolIndex = this->symbolIndexFromIndirectSectionAddress(addr, _stubsMachOSection);
2903 assert(symbolIndex != INDIRECT_SYMBOL_LOCAL);
2904 const macho_nlist<P>& sym = this->symbolFromIndex(symbolIndex);
2905 // can't be to external weak symbol
2906 assert( (this->combineFromSymbol(sym) != ld::Atom::combineByName) || (this->scopeFromSymbol(sym) != ld::Atom::scopeGlobal) );
2907 *offsetInAtom = 0;
2908 return this->findAtomByName(this->nameFromSymbol(sym));
2909 }
2910 Atom<A>* target = this->findAtomByAddress(addr);
2911 *offsetInAtom = addr - target->_objAddress;
2912 return target;
2913 }
2914
2915 template <typename A>
2916 Atom<A>* Parser<A>::findAtomByName(const char* name)
2917 {
2918 uint8_t* p = _file->_atomsArray;
2919 for(int i=_file->_atomsArrayCount; i > 0; --i) {
2920 Atom<A>* atom = (Atom<A>*)p;
2921 if ( strcmp(name, atom->name()) == 0 )
2922 return atom;
2923 p += sizeof(Atom<A>);
2924 }
2925 return NULL;
2926 }
2927
2928 template <typename A>
2929 void Parser<A>::findTargetFromAddress(pint_t addr, TargetDesc& target)
2930 {
2931 if ( hasStubsSection() && (_stubsMachOSection->addr() <= addr) && (addr < (_stubsMachOSection->addr()+_stubsMachOSection->size())) ) {
2932 // target is a stub, remove indirection
2933 uint32_t symbolIndex = this->symbolIndexFromIndirectSectionAddress(addr, _stubsMachOSection);
2934 assert(symbolIndex != INDIRECT_SYMBOL_LOCAL);
2935 const macho_nlist<P>& sym = this->symbolFromIndex(symbolIndex);
2936 target.atom = NULL;
2937 target.name = this->nameFromSymbol(sym);
2938 target.weakImport = this->weakImportFromSymbol(sym);
2939 target.addend = 0;
2940 return;
2941 }
2942 Section<A>* section = this->sectionForAddress(addr);
2943 target.atom = section->findAtomByAddress(addr);
2944 target.addend = addr - target.atom->_objAddress;
2945 target.weakImport = false;
2946 target.name = NULL;
2947 }
2948
2949 template <typename A>
2950 void Parser<A>::findTargetFromAddress(pint_t baseAddr, pint_t addr, TargetDesc& target)
2951 {
2952 findTargetFromAddress(baseAddr, target);
2953 target.addend = addr - target.atom->_objAddress;
2954 }
2955
2956 template <typename A>
2957 void Parser<A>::findTargetFromAddressAndSectionNum(pint_t addr, unsigned int sectNum, TargetDesc& target)
2958 {
2959 if ( sectNum == R_ABS ) {
2960 // target is absolute symbol that corresponds to addr
2961 if ( _absoluteSection != NULL ) {
2962 target.atom = _absoluteSection->findAbsAtomForValue(addr);
2963 if ( target.atom != NULL ) {
2964 target.name = NULL;
2965 target.weakImport = false;
2966 target.addend = 0;
2967 return;
2968 }
2969 }
2970 throwf("R_ABS reloc but no absolute symbol at target address");
2971 }
2972
2973 if ( hasStubsSection() && (stubsSectionNum() == sectNum) ) {
2974 // target is a stub, remove indirection
2975 uint32_t symbolIndex = this->symbolIndexFromIndirectSectionAddress(addr, _stubsMachOSection);
2976 assert(symbolIndex != INDIRECT_SYMBOL_LOCAL);
2977 const macho_nlist<P>& sym = this->symbolFromIndex(symbolIndex);
2978 // use direct reference when stub is to a static function
2979 if ( ((sym.n_type() & N_TYPE) == N_SECT) && (((sym.n_type() & N_EXT) == 0) || (this->nameFromSymbol(sym)[0] == 'L')) ) {
2980 this->findTargetFromAddressAndSectionNum(sym.n_value(), sym.n_sect(), target);
2981 }
2982 else {
2983 target.atom = NULL;
2984 target.name = this->nameFromSymbol(sym);
2985 target.weakImport = this->weakImportFromSymbol(sym);
2986 target.addend = 0;
2987 }
2988 return;
2989 }
2990 Section<A>* section = this->sectionForNum(sectNum);
2991 target.atom = section->findAtomByAddress(addr);
2992 if ( target.atom == NULL ) {
2993 typedef typename A::P::sint_t sint_t;
2994 sint_t a = (sint_t)addr;
2995 sint_t sectStart = (sint_t)(section->machoSection()->addr());
2996 sint_t sectEnd = sectStart + section->machoSection()->size();
2997 if ( a < sectStart ) {
2998 // target address is before start of section, so must be negative addend
2999 target.atom = section->findAtomByAddress(sectStart);
3000 target.addend = a - sectStart;
3001 target.weakImport = false;
3002 target.name = NULL;
3003 return;
3004 }
3005 else if ( a >= sectEnd ) {
3006 target.atom = section->findAtomByAddress(sectEnd-1);
3007 target.addend = a - sectEnd;
3008 target.weakImport = false;
3009 target.name = NULL;
3010 return;
3011 }
3012 }
3013 assert(target.atom != NULL);
3014 target.addend = addr - target.atom->_objAddress;
3015 target.weakImport = false;
3016 target.name = NULL;
3017 }
3018
3019 template <typename A>
3020 void Parser<A>::addDtraceExtraInfos(const SourceLocation& src, const char* providerName)
3021 {
3022 // for every ___dtrace_stability$* and ___dtrace_typedefs$* undefine with
3023 // a matching provider name, add a by-name kDtraceTypeReference at probe site
3024 const char* dollar = strchr(providerName, '$');
3025 if ( dollar != NULL ) {
3026 int providerNameLen = dollar-providerName+1;
3027 for ( std::vector<const char*>::iterator it = _dtraceProviderInfo.begin(); it != _dtraceProviderInfo.end(); ++it) {
3028 const char* typeDollar = strchr(*it, '$');
3029 if ( typeDollar != NULL ) {
3030 if ( strncmp(typeDollar+1, providerName, providerNameLen) == 0 ) {
3031 addFixup(src, ld::Fixup::k1of1, ld::Fixup::kindDtraceExtra,false, *it);
3032 }
3033 }
3034 }
3035 }
3036 }
3037
3038 template <typename A>
3039 const char* Parser<A>::scanSymbolTableForAddress(uint64_t addr)
3040 {
3041 uint64_t closestSymAddr = 0;
3042 const char* closestSymName = NULL;
3043 for (uint32_t i=0; i < this->_symbolCount; ++i) {
3044 const macho_nlist<P>& sym = symbolFromIndex(i);
3045 // ignore stabs
3046 if ( (sym.n_type() & N_STAB) != 0 )
3047 continue;
3048
3049 // only look at definitions
3050 if ( (sym.n_type() & N_TYPE) != N_SECT )
3051 continue;
3052
3053 // return with exact match
3054 if ( sym.n_value() == addr ) {
3055 const char* name = nameFromSymbol(sym);
3056 if ( strncmp(name, "ltmp", 4) != 0 )
3057 return name;
3058 // treat 'ltmp*' labels as close match
3059 closestSymAddr = sym.n_value();
3060 closestSymName = name;
3061 }
3062
3063 // record closest seen so far
3064 if ( (sym.n_value() < addr) && ((sym.n_value() > closestSymAddr) || (closestSymName == NULL)) )
3065 closestSymName = nameFromSymbol(sym);
3066 }
3067
3068 return (closestSymName != NULL) ? closestSymName : "unknown";
3069 }
3070
3071
3072 template <typename A>
3073 void Parser<A>::addFixups(const SourceLocation& src, ld::Fixup::Kind setKind, const TargetDesc& target)
3074 {
3075 // some fixup pairs can be combined
3076 ld::Fixup::Cluster cl = ld::Fixup::k1of3;
3077 ld::Fixup::Kind firstKind = ld::Fixup::kindSetTargetAddress;
3078 bool combined = false;
3079
3080 #if SUPPORT_ARCH_arm64e
3081 bool isAuthenticated = setKind == ld::Fixup::kindStoreLittleEndianAuth64;
3082 // Authenticated pointers need an extra fixup for the auth data.
3083 if (isAuthenticated)
3084 cl = ld::Fixup::k2of4;
3085 #endif
3086 if ( target.addend == 0 ) {
3087 cl = ld::Fixup::k1of1;
3088 combined = true;
3089 switch ( setKind ) {
3090 case ld::Fixup::kindStoreLittleEndian32:
3091 firstKind = ld::Fixup::kindStoreTargetAddressLittleEndian32;
3092 break;
3093 case ld::Fixup::kindStoreLittleEndian64:
3094 firstKind = ld::Fixup::kindStoreTargetAddressLittleEndian64;
3095 break;
3096 case ld::Fixup::kindStoreBigEndian32:
3097 firstKind = ld::Fixup::kindStoreTargetAddressBigEndian32;
3098 break;
3099 case ld::Fixup::kindStoreBigEndian64:
3100 firstKind = ld::Fixup::kindStoreTargetAddressBigEndian64;
3101 break;
3102 case ld::Fixup::kindStoreX86BranchPCRel32:
3103 firstKind = ld::Fixup::kindStoreTargetAddressX86BranchPCRel32;
3104 break;
3105 case ld::Fixup::kindStoreX86PCRel32:
3106 firstKind = ld::Fixup::kindStoreTargetAddressX86PCRel32;
3107 break;
3108 case ld::Fixup::kindStoreX86PCRel32GOTLoad:
3109 firstKind = ld::Fixup::kindStoreTargetAddressX86PCRel32GOTLoad;
3110 break;
3111 case ld::Fixup::kindStoreX86PCRel32TLVLoad:
3112 firstKind = ld::Fixup::kindStoreTargetAddressX86PCRel32TLVLoad;
3113 break;
3114 case ld::Fixup::kindStoreX86Abs32TLVLoad:
3115 firstKind = ld::Fixup::kindStoreTargetAddressX86Abs32TLVLoad;
3116 break;
3117 case ld::Fixup::kindStoreARMBranch24:
3118 firstKind = ld::Fixup::kindStoreTargetAddressARMBranch24;
3119 break;
3120 case ld::Fixup::kindStoreThumbBranch22:
3121 firstKind = ld::Fixup::kindStoreTargetAddressThumbBranch22;
3122 break;
3123 #if SUPPORT_ARCH_arm64
3124 case ld::Fixup::kindStoreARM64Branch26:
3125 firstKind = ld::Fixup::kindStoreTargetAddressARM64Branch26;
3126 break;
3127 case ld::Fixup::kindStoreARM64Page21:
3128 firstKind = ld::Fixup::kindStoreTargetAddressARM64Page21;
3129 break;
3130 case ld::Fixup::kindStoreARM64PageOff12:
3131 firstKind = ld::Fixup::kindStoreTargetAddressARM64PageOff12;
3132 break;
3133 case ld::Fixup::kindStoreARM64GOTLoadPage21:
3134 firstKind = ld::Fixup::kindStoreTargetAddressARM64GOTLoadPage21;
3135 break;
3136 case ld::Fixup::kindStoreARM64GOTLoadPageOff12:
3137 firstKind = ld::Fixup::kindStoreTargetAddressARM64GOTLoadPageOff12;
3138 break;
3139 case ld::Fixup::kindStoreARM64TLVPLoadPage21:
3140 firstKind = ld::Fixup::kindStoreTargetAddressARM64TLVPLoadPage21;
3141 break;
3142 case ld::Fixup::kindStoreARM64TLVPLoadPageOff12:
3143 firstKind = ld::Fixup::kindStoreTargetAddressARM64TLVPLoadPageOff12;
3144 break;
3145 #endif
3146 #if SUPPORT_ARCH_arm64e
3147 case ld::Fixup::kindStoreLittleEndianAuth64:
3148 firstKind = ld::Fixup::kindStoreTargetAddressLittleEndianAuth64;
3149 cl = ld::Fixup::k2of2;
3150 break;
3151 #endif
3152 default:
3153 combined = false;
3154 cl = ld::Fixup::k1of2;
3155 break;
3156 }
3157 }
3158
3159 #if SUPPORT_ARCH_arm64e
3160 // As the auth data is independent of the addend and target, we can just always
3161 // put it first.
3162 if (isAuthenticated) {
3163 if (cl == ld::Fixup::k2of2) {
3164 addFixup(src, ld::Fixup::k1of2, ld::Fixup::kindSetAuthData, target.authData);
3165 } else {
3166 assert(cl == ld::Fixup::k2of4);
3167 addFixup(src, ld::Fixup::k1of4, ld::Fixup::kindSetAuthData, target.authData);
3168 }
3169 }
3170 #endif
3171
3172 if ( target.atom != NULL ) {
3173 if ( target.atom->scope() == ld::Atom::scopeTranslationUnit ) {
3174 addFixup(src, cl, firstKind, target.atom);
3175 }
3176 else if ( (target.atom->combine() == ld::Atom::combineByNameAndContent) || (target.atom->combine() == ld::Atom::combineByNameAndReferences) ) {
3177 addFixup(src, cl, firstKind, ld::Fixup::bindingByContentBound, target.atom);
3178 }
3179 else if ( (src.atom->section().type() == ld::Section::typeCFString) && (src.offsetInAtom != 0) ) {
3180 // backing string in CFStrings should always be direct
3181 addFixup(src, cl, firstKind, target.atom);
3182 }
3183 else if ( (src.atom == target.atom) && (target.atom->combine() == ld::Atom::combineByName) ) {
3184 // reference to self should always be direct
3185 addFixup(src, cl, firstKind, target.atom);
3186 }
3187 else {
3188 // change direct fixup to by-name fixup
3189 addFixup(src, cl, firstKind, false, target.atom->name());
3190 }
3191 }
3192 else {
3193 addFixup(src, cl, firstKind, target.weakImport, target.name);
3194 }
3195 if ( target.addend == 0 ) {
3196 #if SUPPORT_ARCH_arm64e
3197 if (isAuthenticated)
3198 assert(combined);
3199 #endif
3200 if ( ! combined )
3201 addFixup(src, ld::Fixup::k2of2, setKind);
3202 }
3203 else {
3204 #if SUPPORT_ARCH_arm64e
3205 if (isAuthenticated) {
3206 addFixup(src, ld::Fixup::k3of4, ld::Fixup::kindAddAddend, target.addend);
3207 addFixup(src, ld::Fixup::k4of4, setKind);
3208 } else
3209 #endif
3210 {
3211 addFixup(src, ld::Fixup::k2of3, ld::Fixup::kindAddAddend, target.addend);
3212 addFixup(src, ld::Fixup::k3of3, setKind);
3213 }
3214 }
3215 }
3216
3217 template <typename A>
3218 void Parser<A>::addFixups(const SourceLocation& src, ld::Fixup::Kind kind, const TargetDesc& target, const TargetDesc& picBase)
3219 {
3220 ld::Fixup::Cluster cl = (target.addend == 0) ? ld::Fixup::k1of4 : ld::Fixup::k1of5;
3221 if ( target.atom != NULL ) {
3222 if ( target.atom->scope() == ld::Atom::scopeTranslationUnit ) {
3223 addFixup(src, cl, ld::Fixup::kindSetTargetAddress, target.atom);
3224 }
3225 else if ( (target.atom->combine() == ld::Atom::combineByNameAndContent) || (target.atom->combine() == ld::Atom::combineByNameAndReferences) ) {
3226 addFixup(src, cl, ld::Fixup::kindSetTargetAddress, ld::Fixup::bindingByContentBound, target.atom);
3227 }
3228 else {
3229 addFixup(src, cl, ld::Fixup::kindSetTargetAddress, false, target.atom->name());
3230 }
3231 }
3232 else {
3233 addFixup(src, cl, ld::Fixup::kindSetTargetAddress, target.weakImport, target.name);
3234 }
3235 if ( target.addend == 0 ) {
3236 assert(picBase.atom != NULL);
3237 addFixup(src, ld::Fixup::k2of4, ld::Fixup::kindSubtractTargetAddress, picBase.atom);
3238 addFixup(src, ld::Fixup::k3of4, ld::Fixup::kindSubtractAddend, picBase.addend);
3239 addFixup(src, ld::Fixup::k4of4, kind);
3240 }
3241 else {
3242 addFixup(src, ld::Fixup::k2of5, ld::Fixup::kindAddAddend, target.addend);
3243 addFixup(src, ld::Fixup::k3of5, ld::Fixup::kindSubtractTargetAddress, picBase.atom);
3244 addFixup(src, ld::Fixup::k4of5, ld::Fixup::kindSubtractAddend, picBase.addend);
3245 addFixup(src, ld::Fixup::k5of5, kind);
3246 }
3247 }
3248
3249
3250
3251 template <typename A>
3252 uint32_t TentativeDefinitionSection<A>::computeAtomCount(class Parser<A>& parser,
3253 struct Parser<A>::LabelAndCFIBreakIterator& it,
3254 const struct Parser<A>::CFI_CU_InfoArrays&)
3255 {
3256 return parser.tentativeDefinitionCount();
3257 }
3258
3259 template <typename A>
3260 uint32_t TentativeDefinitionSection<A>::appendAtoms(class Parser<A>& parser, uint8_t* p,
3261 struct Parser<A>::LabelAndCFIBreakIterator& it,
3262 const struct Parser<A>::CFI_CU_InfoArrays&)
3263 {
3264 this->_beginAtoms = (Atom<A>*)p;
3265 uint32_t count = 0;
3266 for (uint32_t i=parser.undefinedStartIndex(); i < parser.undefinedEndIndex(); ++i) {
3267 const macho_nlist<P>& sym = parser.symbolFromIndex(i);
3268 if ( ((sym.n_type() & N_TYPE) == N_UNDF) && (sym.n_value() != 0) ) {
3269 uint64_t size = sym.n_value();
3270 uint8_t alignP2 = GET_COMM_ALIGN(sym.n_desc());
3271 if ( alignP2 == 0 ) {
3272 // common symbols align to their size
3273 // that is, a 4-byte common aligns to 4-bytes
3274 // if this size is not a power of two,
3275 // then round up to the next power of two
3276 alignP2 = 63 - (uint8_t)__builtin_clzll(size);
3277 if ( size != (1ULL << alignP2) )
3278 ++alignP2;
3279 // <rdar://problem/24871389> limit default alignment of large commons
3280 if ( alignP2 > parser.maxDefaultCommonAlignment() )
3281 alignP2 = parser.maxDefaultCommonAlignment();
3282 }
3283 Atom<A>* allocatedSpace = (Atom<A>*)p;
3284 new (allocatedSpace) Atom<A>(*this, parser.nameFromSymbol(sym), (pint_t)ULLONG_MAX, size,
3285 ld::Atom::definitionTentative, ld::Atom::combineByName,
3286 parser.scopeFromSymbol(sym), ld::Atom::typeZeroFill, ld::Atom::symbolTableIn,
3287 parser.dontDeadStripFromSymbol(sym), false, false, ld::Atom::Alignment(alignP2) );
3288 p += sizeof(Atom<A>);
3289 ++count;
3290 }
3291 }
3292 this->_endAtoms = (Atom<A>*)p;
3293 return count;
3294 }
3295
3296
3297 template <typename A>
3298 uint32_t AbsoluteSymbolSection<A>::computeAtomCount(class Parser<A>& parser,
3299 struct Parser<A>::LabelAndCFIBreakIterator& it,
3300 const struct Parser<A>::CFI_CU_InfoArrays&)
3301 {
3302 return parser.absoluteSymbolCount();
3303 }
3304
3305 template <typename A>
3306 uint32_t AbsoluteSymbolSection<A>::appendAtoms(class Parser<A>& parser, uint8_t* p,
3307 struct Parser<A>::LabelAndCFIBreakIterator& it,
3308 const struct Parser<A>::CFI_CU_InfoArrays&)
3309 {
3310 this->_beginAtoms = (Atom<A>*)p;
3311 uint32_t count = 0;
3312 for (uint32_t i=0; i < parser.symbolCount(); ++i) {
3313 const macho_nlist<P>& sym = parser.symbolFromIndex(i);
3314 if ( (sym.n_type() & N_TYPE) != N_ABS )
3315 continue;
3316 const char* absName = parser.nameFromSymbol(sym);
3317 // ignore .objc_class_name_* symbols
3318 if ( strncmp(absName, ".objc_class_name_", 17) == 0 )
3319 continue;
3320 // ignore .objc_class_name_* symbols
3321 if ( strncmp(absName, ".objc_category_name_", 20) == 0 )
3322 continue;
3323 // ignore empty *.eh symbols
3324 if ( strcmp(&absName[strlen(absName)-3], ".eh") == 0 )
3325 continue;
3326
3327 Atom<A>* allocatedSpace = (Atom<A>*)p;
3328 new (allocatedSpace) Atom<A>(*this, parser, sym, 0);
3329 p += sizeof(Atom<A>);
3330 ++count;
3331 }
3332 this->_endAtoms = (Atom<A>*)p;
3333 return count;
3334 }
3335
3336 template <typename A>
3337 Atom<A>* AbsoluteSymbolSection<A>::findAbsAtomForValue(typename A::P::uint_t value)
3338 {
3339 Atom<A>* end = this->_endAtoms;
3340 for(Atom<A>* p = this->_beginAtoms; p < end; ++p) {
3341 if ( p->_objAddress == value )
3342 return p;
3343 }
3344 return NULL;
3345 }
3346
3347
3348 template <typename A>
3349 uint32_t Parser<A>::indirectSymbol(uint32_t indirectIndex)
3350 {
3351 if ( indirectIndex >= _indirectTableCount )
3352 throw "indirect symbol index out of range";
3353 return E::get32(_indirectTable[indirectIndex]);
3354 }
3355
3356 template <typename A>
3357 const macho_nlist<typename A::P>& Parser<A>::symbolFromIndex(uint32_t index)
3358 {
3359 if ( index > _symbolCount )
3360 throw "symbol index out of range";
3361 return _symbols[index];
3362 }
3363
3364 template <typename A>
3365 const macho_section<typename A::P>* Parser<A>::machOSectionFromSectionIndex(uint32_t index)
3366 {
3367 if ( index >= _machOSectionsCount )
3368 throw "section index out of range";
3369 return &_sectionsStart[index];
3370 }
3371
3372 template <typename A>
3373 uint32_t Parser<A>::symbolIndexFromIndirectSectionAddress(pint_t addr, const macho_section<P>* sect)
3374 {
3375 uint32_t elementSize = 0;
3376 switch ( sect->flags() & SECTION_TYPE ) {
3377 case S_SYMBOL_STUBS:
3378 elementSize = sect->reserved2();
3379 break;
3380 case S_LAZY_SYMBOL_POINTERS:
3381 case S_NON_LAZY_SYMBOL_POINTERS:
3382 case S_THREAD_LOCAL_VARIABLE_POINTERS:
3383 elementSize = sizeof(pint_t);
3384 break;
3385 default:
3386 throw "section does not use indirect symbol table";
3387 }
3388 uint32_t indexInSection = (addr - sect->addr()) / elementSize;
3389 uint32_t indexIntoIndirectTable = sect->reserved1() + indexInSection;
3390 return this->indirectSymbol(indexIntoIndirectTable);
3391 }
3392
3393
3394
3395 template <typename A>
3396 const char* Parser<A>::nameFromSymbol(const macho_nlist<P>& sym)
3397 {
3398 return &_strings[sym.n_strx()];
3399 }
3400
3401 template <typename A>
3402 ld::Atom::Scope Parser<A>::scopeFromSymbol(const macho_nlist<P>& sym)
3403 {
3404 if ( (sym.n_type() & N_EXT) == 0 )
3405 return ld::Atom::scopeTranslationUnit;
3406 else if ( (sym.n_type() & N_PEXT) != 0 )
3407 return ld::Atom::scopeLinkageUnit;
3408 else if ( this->nameFromSymbol(sym)[0] == 'l' ) // since all 'l' symbols will be remove, don't make them global
3409 return ld::Atom::scopeLinkageUnit;
3410 else
3411 return ld::Atom::scopeGlobal;
3412 }
3413
3414 template <typename A>
3415 ld::Atom::Definition Parser<A>::definitionFromSymbol(const macho_nlist<P>& sym)
3416 {
3417 switch ( sym.n_type() & N_TYPE ) {
3418 case N_ABS:
3419 return ld::Atom::definitionAbsolute;
3420 case N_SECT:
3421 return ld::Atom::definitionRegular;
3422 case N_UNDF:
3423 if ( sym.n_value() != 0 )
3424 return ld::Atom::definitionTentative;
3425 }
3426 throw "definitionFromSymbol() bad symbol";
3427 }
3428
3429 template <typename A>
3430 ld::Atom::Combine Parser<A>::combineFromSymbol(const macho_nlist<P>& sym)
3431 {
3432 if ( sym.n_desc() & N_WEAK_DEF )
3433 return ld::Atom::combineByName;
3434 else
3435 return ld::Atom::combineNever;
3436 }
3437
3438
3439 template <typename A>
3440 ld::Atom::SymbolTableInclusion Parser<A>::inclusionFromSymbol(const macho_nlist<P>& sym)
3441 {
3442 const char* symbolName = nameFromSymbol(sym);
3443 // labels beginning with 'l' (lowercase ell) are automatically removed in final linked images <rdar://problem/4571042>
3444 // labels beginning with 'L' should have been stripped by the assembler, so are stripped now
3445 if ( sym.n_desc() & REFERENCED_DYNAMICALLY )
3446 return ld::Atom::symbolTableInAndNeverStrip;
3447 else if ( symbolName[0] == 'l' )
3448 return ld::Atom::symbolTableNotInFinalLinkedImages;
3449 else if ( symbolName[0] == 'L' )
3450 return ld::Atom::symbolTableNotIn;
3451 else
3452 return ld::Atom::symbolTableIn;
3453 }
3454
3455 template <typename A>
3456 bool Parser<A>::dontDeadStripFromSymbol(const macho_nlist<P>& sym)
3457 {
3458 return ( (sym.n_desc() & (N_NO_DEAD_STRIP|REFERENCED_DYNAMICALLY)) != 0 );
3459 }
3460
3461 template <typename A>
3462 bool Parser<A>::isThumbFromSymbol(const macho_nlist<P>& sym)
3463 {
3464 return ( sym.n_desc() & N_ARM_THUMB_DEF );
3465 }
3466
3467 template <typename A>
3468 bool Parser<A>::weakImportFromSymbol(const macho_nlist<P>& sym)
3469 {
3470 return ( ((sym.n_type() & N_TYPE) == N_UNDF) && ((sym.n_desc() & N_WEAK_REF) != 0) );
3471 }
3472
3473 template <typename A>
3474 bool Parser<A>::resolverFromSymbol(const macho_nlist<P>& sym)
3475 {
3476 return ( sym.n_desc() & N_SYMBOL_RESOLVER );
3477 }
3478
3479 template <typename A>
3480 bool Parser<A>::altEntryFromSymbol(const macho_nlist<P>& sym)
3481 {
3482 return ( sym.n_desc() & N_ALT_ENTRY );
3483 }
3484
3485
3486 /* Skip over a LEB128 value (signed or unsigned). */
3487 static void
3488 skip_leb128 (const uint8_t ** offset, const uint8_t * end)
3489 {
3490 while (*offset != end && **offset >= 0x80)
3491 (*offset)++;
3492 if (*offset != end)
3493 (*offset)++;
3494 }
3495
3496 /* Read a ULEB128 into a 64-bit word. Return (uint64_t)-1 on overflow
3497 or error. On overflow, skip past the rest of the uleb128. */
3498 static uint64_t
3499 read_uleb128 (const uint8_t ** offset, const uint8_t * end)
3500 {
3501 uint64_t result = 0;
3502 int bit = 0;
3503
3504 do {
3505 uint64_t b;
3506
3507 if (*offset == end)
3508 return (uint64_t) -1;
3509
3510 b = **offset & 0x7f;
3511
3512 if (bit >= 64 || b << bit >> bit != b)
3513 result = (uint64_t) -1;
3514 else
3515 result |= b << bit, bit += 7;
3516 } while (*(*offset)++ >= 0x80);
3517 return result;
3518 }
3519
3520
3521 /* Skip over a DWARF attribute of form FORM. */
3522 template <typename A>
3523 bool Parser<A>::skip_form(const uint8_t ** offset, const uint8_t * end, uint64_t form,
3524 uint8_t addr_size, bool dwarf64)
3525 {
3526 int64_t sz=0;
3527
3528 switch (form)
3529 {
3530 case DW_FORM_addr:
3531 sz = addr_size;
3532 break;
3533
3534 case DW_FORM_block2:
3535 if (end - *offset < 2)
3536 return false;
3537 sz = 2 + A::P::E::get16(*(uint16_t*)offset);
3538 break;
3539
3540 case DW_FORM_block4:
3541 if (end - *offset < 4)
3542 return false;
3543 sz = 2 + A::P::E::get32(*(uint32_t*)offset);
3544 break;
3545
3546 case DW_FORM_data2:
3547 case DW_FORM_ref2:
3548 sz = 2;
3549 break;
3550
3551 case DW_FORM_data4:
3552 case DW_FORM_ref4:
3553 sz = 4;
3554 break;
3555
3556 case DW_FORM_data8:
3557 case DW_FORM_ref8:
3558 sz = 8;
3559 break;
3560
3561 case DW_FORM_string:
3562 while (*offset != end && **offset)
3563 ++*offset;
3564 case DW_FORM_data1:
3565 case DW_FORM_flag:
3566 case DW_FORM_ref1:
3567 sz = 1;
3568 break;
3569
3570 case DW_FORM_block:
3571 sz = read_uleb128 (offset, end);
3572 break;
3573
3574 case DW_FORM_block1:
3575 if (*offset == end)
3576 return false;
3577 sz = 1 + **offset;
3578 break;
3579
3580 case DW_FORM_sdata:
3581 case DW_FORM_udata:
3582 case DW_FORM_ref_udata:
3583 skip_leb128 (offset, end);
3584 return true;
3585
3586 case DW_FORM_strp:
3587 case DW_FORM_ref_addr:
3588 sz = 4;
3589 break;
3590
3591 case DW_FORM_sec_offset:
3592 sz = sizeof(typename A::P::uint_t);
3593 break;
3594
3595 case DW_FORM_exprloc:
3596 sz = read_uleb128 (offset, end);
3597 break;
3598
3599 case DW_FORM_flag_present:
3600 sz = 0;
3601 break;
3602
3603 case DW_FORM_ref_sig8:
3604 sz = 8;
3605 break;
3606
3607 default:
3608 return false;
3609 }
3610 if (end - *offset < sz)
3611 return false;
3612 *offset += sz;
3613 return true;
3614 }
3615
3616
3617 template <typename A>
3618 const char* Parser<A>::getDwarfString(uint64_t form, const uint8_t*& di)
3619 {
3620 uint32_t offset;
3621 const char* dwarfStrings;
3622 const char* result = NULL;
3623 switch (form) {
3624 case DW_FORM_string:
3625 result = (const char*)di;
3626 di += strlen(result) + 1;
3627 break;
3628 case DW_FORM_strp:
3629 offset = E::get32(*((uint32_t*)di));
3630 dwarfStrings = (char*)_file->fileContent() + _file->_dwarfDebugStringSect->offset();
3631 if ( offset < _file->_dwarfDebugStringSect->size() )
3632 result = &dwarfStrings[offset];
3633 else
3634 warning("dwarf DW_FORM_strp (offset=0x%08X) is too big in %s", offset, this->_path);
3635 di += 4;
3636 break;
3637 default:
3638 warning("unknown dwarf string encoding (form=%lld) in %s", form, this->_path);
3639 break;
3640 }
3641 return result;
3642 }
3643
3644 template <typename A>
3645 uint64_t Parser<A>::getDwarfOffset(uint64_t form, const uint8_t*& di, bool dwarf64)
3646 {
3647 if ( form == DW_FORM_sec_offset )
3648 form = (dwarf64 ? DW_FORM_data8 : DW_FORM_data4);
3649 uint64_t result = -1;
3650 switch (form) {
3651 case DW_FORM_data4:
3652 result = A::P::E::get32(*(uint32_t*)di);
3653 di += 4;
3654 break;
3655 case DW_FORM_data8:
3656 result = A::P::E::get64(*(uint64_t*)di);
3657 di += 8;
3658 break;
3659 default:
3660 warning("unknown dwarf DW_FORM_ for DW_AT_stmt_list in %s", this->_path);
3661 }
3662 return result;
3663 }
3664
3665
3666 template <typename A>
3667 struct AtomAndLineInfo {
3668 Atom<A>* atom;
3669 ld::Atom::LineInfo info;
3670 };
3671
3672
3673 // <rdar://problem/5591394> Add support to ld64 for N_FUN stabs when used for symbolic constants
3674 // Returns whether a stabStr belonging to an N_FUN stab represents a
3675 // symbolic constant rather than a function
3676 template <typename A>
3677 bool Parser<A>::isConstFunStabs(const char *stabStr)
3678 {
3679 const char* colon;
3680 // N_FUN can be used for both constants and for functions. In case it's a constant,
3681 // the format of the stabs string is "symname:c=<value>;"
3682 // ':' cannot appear in the symbol name, except if it's an Objective-C method
3683 // (in which case the symbol name starts with + or -, and then it's definitely
3684 // not a constant)
3685 return (stabStr != NULL) && (stabStr[0] != '+') && (stabStr[0] != '-')
3686 && ((colon = strchr(stabStr, ':')) != NULL)
3687 && (colon[1] == 'c') && (colon[2] == '=');
3688 }
3689
3690
3691 template <typename A>
3692 void Parser<A>::parseDebugInfo()
3693 {
3694 addAstFiles();
3695
3696 // check for dwarf __debug_info section
3697 if ( _file->_dwarfDebugInfoSect == NULL ) {
3698 // if no DWARF debug info, look for stabs
3699 this->parseStabs();
3700 return;
3701 }
3702 if ( _file->_dwarfDebugInfoSect->size() == 0 )
3703 return;
3704
3705 uint64_t stmtList;
3706 const char* tuDir;
3707 const char* tuName;
3708 if ( !read_comp_unit(&tuName, &tuDir, &stmtList) ) {
3709 // if can't parse dwarf, warn and give up
3710 _file->_dwarfTranslationUnitPath = NULL;
3711 warning("can't parse dwarf compilation unit info in %s", _path);
3712 _file->_debugInfoKind = ld::relocatable::File::kDebugInfoNone;
3713 return;
3714 }
3715 if ( (tuName != NULL) && (tuName[0] == '/') ) {
3716 _file->_dwarfTranslationUnitPath = tuName;
3717 }
3718 else if ( (tuDir != NULL) && (tuName != NULL) ) {
3719 asprintf((char**)&(_file->_dwarfTranslationUnitPath), "%s/%s", tuDir, tuName);
3720 }
3721 else if ( tuDir == NULL ) {
3722 _file->_dwarfTranslationUnitPath = tuName;
3723 }
3724 else {
3725 _file->_dwarfTranslationUnitPath = NULL;
3726 }
3727
3728 // add line number info to atoms from dwarf
3729 std::vector<AtomAndLineInfo<A> > entries;
3730 entries.reserve(64);
3731 if ( _file->_debugInfoKind == ld::relocatable::File::kDebugInfoDwarf ) {
3732 // file with just data will have no __debug_line info
3733 if ( (_file->_dwarfDebugLineSect != NULL) && (_file->_dwarfDebugLineSect->size() != 0) ) {
3734 // validate stmt_list
3735 if ( (stmtList != (uint64_t)-1) && (stmtList < _file->_dwarfDebugLineSect->size()) ) {
3736 const uint8_t* debug_line = (uint8_t*)_file->fileContent() + _file->_dwarfDebugLineSect->offset();
3737 struct line_reader_data* lines = line_open(&debug_line[stmtList],
3738 _file->_dwarfDebugLineSect->size() - stmtList, E::little_endian);
3739 struct line_info result;
3740 Atom<A>* curAtom = NULL;
3741 uint32_t curAtomOffset = 0;
3742 uint32_t curAtomAddress = 0;
3743 uint32_t curAtomSize = 0;
3744 std::map<uint32_t,const char*> dwarfIndexToFile;
3745 if ( lines != NULL ) {
3746 while ( line_next(lines, &result, line_stop_pc) ) {
3747 //fprintf(stderr, "curAtom=%p, result.pc=0x%llX, result.line=%llu, result.end_of_sequence=%d,"
3748 // " curAtomAddress=0x%X, curAtomSize=0x%X\n",
3749 // curAtom, result.pc, result.line, result.end_of_sequence, curAtomAddress, curAtomSize);
3750 // work around weird debug line table compiler generates if no functions in __text section
3751 if ( (curAtom == NULL) && (result.pc == 0) && result.end_of_sequence && (result.file == 1))
3752 continue;
3753 // for performance, see if in next pc is in current atom
3754 if ( (curAtom != NULL) && (curAtomAddress <= result.pc) && (result.pc < (curAtomAddress+curAtomSize)) ) {
3755 curAtomOffset = result.pc - curAtomAddress;
3756 }
3757 // or pc at end of current atom
3758 else if ( result.end_of_sequence && (curAtom != NULL) && (result.pc == (curAtomAddress+curAtomSize)) ) {
3759 curAtomOffset = result.pc - curAtomAddress;
3760 }
3761 // or only one function that is a one line function
3762 else if ( result.end_of_sequence && (curAtom == NULL) && (this->findAtomByAddress(0) != NULL) && (result.pc == this->findAtomByAddress(0)->size()) ) {
3763 curAtom = this->findAtomByAddress(0);
3764 curAtomOffset = result.pc - curAtom->objectAddress();
3765 curAtomAddress = curAtom->objectAddress();
3766 curAtomSize = curAtom->size();
3767 }
3768 else {
3769 // do slow look up of atom by address
3770 try {
3771 curAtom = this->findAtomByAddress(result.pc);
3772 }
3773 catch (...) {
3774 // in case of bug in debug info, don't abort link, just limp on
3775 curAtom = NULL;
3776 }
3777 if ( curAtom == NULL )
3778 break; // file has line info but no functions
3779 if ( result.end_of_sequence && (curAtomAddress+curAtomSize < result.pc) ) {
3780 // a one line function can be returned by line_next() as one entry with pc at end of blob
3781 // look for alt atom starting at end of previous atom
3782 uint32_t previousEnd = curAtomAddress+curAtomSize;
3783 Atom<A>* alt = this->findAtomByAddressOrNullIfStub(previousEnd);
3784 if ( alt == NULL )
3785 continue; // ignore spurious debug info for stubs
3786 if ( result.pc <= alt->objectAddress() + alt->size() ) {
3787 curAtom = alt;
3788 curAtomOffset = result.pc - alt->objectAddress();
3789 curAtomAddress = alt->objectAddress();
3790 curAtomSize = alt->size();
3791 }
3792 else {
3793 curAtomOffset = result.pc - curAtom->objectAddress();
3794 curAtomAddress = curAtom->objectAddress();
3795 curAtomSize = curAtom->size();
3796 }
3797 }
3798 else {
3799 curAtomOffset = result.pc - curAtom->objectAddress();
3800 curAtomAddress = curAtom->objectAddress();
3801 curAtomSize = curAtom->size();
3802 }
3803 }
3804 const char* filename;
3805 std::map<uint32_t,const char*>::iterator pos = dwarfIndexToFile.find(result.file);
3806 if ( pos == dwarfIndexToFile.end() ) {
3807 filename = line_file(lines, result.file);
3808 dwarfIndexToFile[result.file] = filename;
3809 }
3810 else {
3811 filename = pos->second;
3812 }
3813 // only record for ~8000 line info records per function
3814 if ( curAtom->roomForMoreLineInfoCount() ) {
3815 AtomAndLineInfo<A> entry;
3816 entry.atom = curAtom;
3817 entry.info.atomOffset = curAtomOffset;
3818 entry.info.fileName = filename;
3819 entry.info.lineNumber = result.line;
3820 //fprintf(stderr, "addr=0x%08llX, line=%lld, file=%s, atom=%s, atom.size=0x%X, end=%d\n",
3821 // result.pc, result.line, filename, curAtom->name(), curAtomSize, result.end_of_sequence);
3822 entries.push_back(entry);
3823 curAtom->incrementLineInfoCount();
3824 }
3825 if ( result.end_of_sequence ) {
3826 curAtom = NULL;
3827 }
3828 }
3829 line_free(lines);
3830 }
3831 }
3832 }
3833 }
3834
3835 // assign line info start offset for each atom
3836 uint8_t* p = _file->_atomsArray;
3837 uint32_t liOffset = 0;
3838 for(int i=_file->_atomsArrayCount; i > 0; --i) {
3839 Atom<A>* atom = (Atom<A>*)p;
3840 atom->_lineInfoStartIndex = liOffset;
3841 liOffset += atom->_lineInfoCount;
3842 atom->_lineInfoCount = 0;
3843 p += sizeof(Atom<A>);
3844 }
3845 assert(liOffset == entries.size());
3846 _file->_lineInfos.resize(liOffset);
3847
3848 // copy each line info for each atom
3849 for (typename std::vector<AtomAndLineInfo<A> >::iterator it = entries.begin(); it != entries.end(); ++it) {
3850 uint32_t slot = it->atom->_lineInfoStartIndex + it->atom->_lineInfoCount;
3851 _file->_lineInfos[slot] = it->info;
3852 it->atom->_lineInfoCount++;
3853 }
3854
3855 // done with temp vector
3856 entries.clear();
3857 }
3858
3859 template <typename A>
3860 void Parser<A>::parseStabs()
3861 {
3862 // scan symbol table for stabs entries
3863 Atom<A>* currentAtom = NULL;
3864 pint_t currentAtomAddress = 0;
3865 enum { start, inBeginEnd, inFun } state = start;
3866 for (uint32_t symbolIndex = 0; symbolIndex < _symbolCount; ++symbolIndex ) {
3867 const macho_nlist<P>& sym = this->symbolFromIndex(symbolIndex);
3868 bool useStab = true;
3869 uint8_t type = sym.n_type();
3870 const char* symString = (sym.n_strx() != 0) ? this->nameFromSymbol(sym) : NULL;
3871 if ( (type & N_STAB) != 0 ) {
3872 _file->_debugInfoKind = (_hasUUID ? ld::relocatable::File::kDebugInfoStabsUUID : ld::relocatable::File::kDebugInfoStabs);
3873 ld::relocatable::File::Stab stab;
3874 stab.atom = NULL;
3875 stab.type = type;
3876 stab.other = sym.n_sect();
3877 stab.desc = sym.n_desc();
3878 stab.value = sym.n_value();
3879 stab.string = NULL;
3880 switch (state) {
3881 case start:
3882 switch (type) {
3883 case N_BNSYM:
3884 // beginning of function block
3885 state = inBeginEnd;
3886 // fall into case to lookup atom by addresss
3887 case N_LCSYM:
3888 case N_STSYM:
3889 currentAtomAddress = sym.n_value();
3890 currentAtom = this->findAtomByAddress(currentAtomAddress);
3891 if ( currentAtom != NULL ) {
3892 stab.atom = currentAtom;
3893 stab.string = symString;
3894 }
3895 else {
3896 fprintf(stderr, "can't find atom for stabs BNSYM at %08llX in %s",
3897 (uint64_t)sym.n_value(), _path);
3898 }
3899 break;
3900 case N_SO:
3901 case N_OSO:
3902 case N_OPT:
3903 case N_LSYM:
3904 case N_RSYM:
3905 case N_PSYM:
3906 case N_AST:
3907 // not associated with an atom, just copy
3908 stab.string = symString;
3909 break;
3910 case N_GSYM:
3911 {
3912 // n_value field is NOT atom address ;-(
3913 // need to find atom by name match
3914 const char* colon = strchr(symString, ':');
3915 if ( colon != NULL ) {
3916 // build underscore leading name
3917 int nameLen = colon - symString;
3918 char symName[nameLen+2];
3919 strlcpy(&symName[1], symString, nameLen+1);
3920 symName[0] = '_';
3921 symName[nameLen+1] = '\0';
3922 currentAtom = this->findAtomByName(symName);
3923 if ( currentAtom != NULL ) {
3924 stab.atom = currentAtom;
3925 stab.string = symString;
3926 }
3927 }
3928 else {
3929 // might be a debug-note without trailing :G()
3930 currentAtom = this->findAtomByName(symString);
3931 if ( currentAtom != NULL ) {
3932 stab.atom = currentAtom;
3933 stab.string = symString;
3934 }
3935 }
3936 if ( stab.atom == NULL ) {
3937 // ld_classic added bogus GSYM stabs for old style dtrace probes
3938 if ( (strncmp(symString, "__dtrace_probe$", 15) != 0) )
3939 warning("can't find atom for N_GSYM stabs %s in %s", symString, _path);
3940 useStab = false;
3941 }
3942 break;
3943 }
3944 case N_FUN:
3945 if ( isConstFunStabs(symString) ) {
3946 // constant not associated with a function
3947 stab.string = symString;
3948 }
3949 else {
3950 // old style stabs without BNSYM
3951 state = inFun;
3952 currentAtomAddress = sym.n_value();
3953 currentAtom = this->findAtomByAddress(currentAtomAddress);
3954 if ( currentAtom != NULL ) {
3955 stab.atom = currentAtom;
3956 stab.string = symString;
3957 }
3958 else {
3959 warning("can't find atom for stabs FUN at %08llX in %s",
3960 (uint64_t)currentAtomAddress, _path);
3961 }
3962 }
3963 break;
3964 case N_SOL:
3965 case N_SLINE:
3966 stab.string = symString;
3967 // old stabs
3968 break;
3969 case N_BINCL:
3970 case N_EINCL:
3971 case N_EXCL:
3972 stab.string = symString;
3973 // -gfull built .o file
3974 break;
3975 default:
3976 warning("unknown stabs type 0x%X in %s", type, _path);
3977 }
3978 break;
3979 case inBeginEnd:
3980 stab.atom = currentAtom;
3981 switch (type) {
3982 case N_ENSYM:
3983 state = start;
3984 currentAtom = NULL;
3985 break;
3986 case N_LCSYM:
3987 case N_STSYM:
3988 {
3989 Atom<A>* nestedAtom = this->findAtomByAddress(sym.n_value());
3990 if ( nestedAtom != NULL ) {
3991 stab.atom = nestedAtom;
3992 stab.string = symString;
3993 }
3994 else {
3995 warning("can't find atom for stabs 0x%X at %08llX in %s",
3996 type, (uint64_t)sym.n_value(), _path);
3997 }
3998 break;
3999 }
4000 case N_LBRAC:
4001 case N_RBRAC:
4002 case N_SLINE:
4003 // adjust value to be offset in atom
4004 stab.value -= currentAtomAddress;
4005 default:
4006 stab.string = symString;
4007 break;
4008 }
4009 break;
4010 case inFun:
4011 switch (type) {
4012 case N_FUN:
4013 if ( isConstFunStabs(symString) ) {
4014 stab.atom = currentAtom;
4015 stab.string = symString;
4016 }
4017 else {
4018 if ( sym.n_sect() != 0 ) {
4019 // found another start stab, must be really old stabs...
4020 currentAtomAddress = sym.n_value();
4021 currentAtom = this->findAtomByAddress(currentAtomAddress);
4022 if ( currentAtom != NULL ) {
4023 stab.atom = currentAtom;
4024 stab.string = symString;
4025 }
4026 else {
4027 warning("can't find atom for stabs FUN at %08llX in %s",
4028 (uint64_t)currentAtomAddress, _path);
4029 }
4030 }
4031 else {
4032 // found ending stab, switch back to start state
4033 stab.string = symString;
4034 stab.atom = currentAtom;
4035 state = start;
4036 currentAtom = NULL;
4037 }
4038 }
4039 break;
4040 case N_LBRAC:
4041 case N_RBRAC:
4042 case N_SLINE:
4043 // adjust value to be offset in atom
4044 stab.value -= currentAtomAddress;
4045 stab.atom = currentAtom;
4046 break;
4047 case N_SO:
4048 stab.string = symString;
4049 state = start;
4050 break;
4051 default:
4052 stab.atom = currentAtom;
4053 stab.string = symString;
4054 break;
4055 }
4056 break;
4057 }
4058 // add to list of stabs for this .o file
4059 if ( useStab )
4060 _file->_stabs.push_back(stab);
4061 }
4062 }
4063 }
4064
4065
4066 template <typename A>
4067 void Parser<A>::addAstFiles()
4068 {
4069 // scan symbol table for N_AST entries
4070 for (uint32_t symbolIndex = 0; symbolIndex < _symbolCount; ++symbolIndex ) {
4071 const macho_nlist<P>& sym = this->symbolFromIndex(symbolIndex);
4072 if ( (sym.n_type() == N_AST) && (sym.n_strx() != 0) ) {
4073 const char* symString = this->nameFromSymbol(sym);
4074 ld::relocatable::File::AstTimeAndPath entry;
4075 entry.time = sym.n_value();
4076 entry.path = symString;
4077 _file->_astFiles.push_back(entry);
4078 }
4079 }
4080 }
4081
4082
4083 // Look at the compilation unit DIE and determine
4084 // its NAME, compilation directory (in COMP_DIR) and its
4085 // line number information offset (in STMT_LIST). NAME and COMP_DIR
4086 // may be NULL (especially COMP_DIR) if they are not in the .o file;
4087 // STMT_LIST will be (uint64_t) -1.
4088 //
4089 // At present this assumes that there's only one compilation unit DIE.
4090 //
4091 template <typename A>
4092 bool Parser<A>::read_comp_unit(const char ** name, const char ** comp_dir,
4093 uint64_t *stmt_list)
4094 {
4095 const uint8_t * debug_info;
4096 const uint8_t * debug_abbrev;
4097 const uint8_t * di;
4098 const uint8_t * next_cu;
4099 const uint8_t * da;
4100 const uint8_t * end;
4101 const uint8_t * enda;
4102 uint64_t sz;
4103 uint16_t vers;
4104 uint64_t abbrev_base;
4105 uint64_t abbrev;
4106 uint8_t address_size;
4107 bool dwarf64;
4108
4109 *name = NULL;
4110 *comp_dir = NULL;
4111 *stmt_list = (uint64_t) -1;
4112
4113 if ( (_file->_dwarfDebugInfoSect == NULL) || (_file->_dwarfDebugAbbrevSect == NULL) )
4114 return false;
4115
4116 if (_file->_dwarfDebugInfoSect->size() < 12)
4117 /* Too small to be a real debug_info section. */
4118 return false;
4119
4120 debug_info = (uint8_t*)_file->fileContent() + _file->_dwarfDebugInfoSect->offset();
4121 debug_abbrev = (uint8_t*)_file->fileContent() + _file->_dwarfDebugAbbrevSect->offset();
4122 next_cu = debug_info;
4123
4124 while ((uint64_t)(next_cu - debug_info) < _file->_dwarfDebugInfoSect->size()) {
4125 di = next_cu;
4126 sz = A::P::E::get32(*(uint32_t*)di);
4127 di += 4;
4128 dwarf64 = sz == 0xffffffff;
4129 if (dwarf64)
4130 sz = A::P::E::get64(*(uint64_t*)di), di += 8;
4131 else if (sz > 0xffffff00)
4132 /* Unknown dwarf format. */
4133 return false;
4134
4135 /* Verify claimed size. */
4136 if (sz + (di - debug_info) > _file->_dwarfDebugInfoSect->size() || sz <= (dwarf64 ? 23 : 11))
4137 return false;
4138
4139 next_cu = di + sz;
4140
4141 vers = A::P::E::get16(*(uint16_t*)di);
4142 if (vers < 2 || vers > 4)
4143 /* DWARF version wrong for this code.
4144 Chances are we could continue anyway, but we don't know for sure. */
4145 return false;
4146 di += 2;
4147
4148 /* Find the debug_abbrev section. */
4149 abbrev_base = dwarf64 ? A::P::E::get64(*(uint64_t*)di) : A::P::E::get32(*(uint32_t*)di);
4150 di += dwarf64 ? 8 : 4;
4151
4152 if (abbrev_base > _file->_dwarfDebugAbbrevSect->size())
4153 return false;
4154 da = debug_abbrev + abbrev_base;
4155 enda = debug_abbrev + _file->_dwarfDebugAbbrevSect->size();
4156
4157 address_size = *di++;
4158
4159 /* Find the abbrev number we're looking for. */
4160 end = di + sz;
4161 abbrev = read_uleb128 (&di, end);
4162 if (abbrev == (uint64_t) -1)
4163 return false;
4164
4165 /* Skip through the debug_abbrev section looking for that abbrev. */
4166 for (;;)
4167 {
4168 uint64_t this_abbrev = read_uleb128 (&da, enda);
4169 uint64_t attr;
4170
4171 if (this_abbrev == abbrev)
4172 /* This is almost always taken. */
4173 break;
4174 skip_leb128 (&da, enda); /* Skip the tag. */
4175 if (da == enda)
4176 return false;
4177 da++; /* Skip the DW_CHILDREN_* value. */
4178
4179 do {
4180 attr = read_uleb128 (&da, enda);
4181 skip_leb128 (&da, enda);
4182 } while (attr != 0 && attr != (uint64_t) -1);
4183 if (attr != 0)
4184 return false;
4185 }
4186
4187 /* Check that the abbrev is one for a DW_TAG_compile_unit. */
4188 if (read_uleb128 (&da, enda) != DW_TAG_compile_unit)
4189 return false;
4190 if (da == enda)
4191 return false;
4192 da++; /* Skip the DW_CHILDREN_* value. */
4193
4194 /* Now, go through the DIE looking for DW_AT_name,
4195 DW_AT_comp_dir, and DW_AT_stmt_list. */
4196 bool skip_to_next_cu = false;
4197 while (!skip_to_next_cu) {
4198
4199 uint64_t attr = read_uleb128 (&da, enda);
4200 uint64_t form = read_uleb128 (&da, enda);
4201
4202 if (attr == (uint64_t) -1)
4203 return false;
4204 else if (attr == 0)
4205 return true;
4206 if (form == DW_FORM_indirect)
4207 form = read_uleb128 (&di, end);
4208
4209 switch (attr) {
4210 case DW_AT_name:
4211 *name = getDwarfString(form, di);
4212 /* Swift object files may contain two CUs: One
4213 describes the Swift code, one is created by the
4214 clang importer. Skip over the CU created by the
4215 clang importer as it may be empty. */
4216 if (std::string(*name) == "<swift-imported-modules>")
4217 skip_to_next_cu = true;
4218 break;
4219 case DW_AT_comp_dir:
4220 *comp_dir = getDwarfString(form, di);
4221 break;
4222 case DW_AT_stmt_list:
4223 *stmt_list = getDwarfOffset(form, di, dwarf64);
4224 break;
4225 default:
4226 if (! skip_form (&di, end, form, address_size, dwarf64))
4227 return false;
4228 }
4229 }
4230 }
4231 return false;
4232 }
4233
4234
4235
4236 template <typename A>
4237 File<A>::~File()
4238 {
4239 free(_sectionsArray);
4240 free(_atomsArray);
4241 }
4242
4243 template <typename A>
4244 const char* File<A>::translationUnitSource() const
4245 {
4246 return _dwarfTranslationUnitPath;
4247 }
4248
4249 template <typename A>
4250 bool File<A>::forEachAtom(ld::File::AtomHandler& handler) const
4251 {
4252 handler.doFile(*this);
4253 uint8_t* p = _atomsArray;
4254 for(int i=_atomsArrayCount; i > 0; --i) {
4255 handler.doAtom(*((Atom<A>*)p));
4256 p += sizeof(Atom<A>);
4257 }
4258 p = _aliasAtomsArray;
4259 for(int i=_aliasAtomsArrayCount; i > 0; --i) {
4260 handler.doAtom(*((AliasAtom*)p));
4261 p += sizeof(AliasAtom);
4262 }
4263
4264 return (_atomsArrayCount != 0) || (_aliasAtomsArrayCount != 0);
4265 }
4266
4267 template <typename A>
4268 const char* Section<A>::makeSegmentName(const macho_section<typename A::P>* sect)
4269 {
4270 // mach-o section record only has room for 16-byte seg/sect names
4271 // so a 16-byte name has no trailing zero
4272 const char* name = sect->segname();
4273 if ( strlen(name) < 16 )
4274 return name;
4275 char* tmp = new char[17];
4276 strlcpy(tmp, name, 17);
4277 return tmp;
4278 }
4279
4280 template <typename A>
4281 const char* Section<A>::makeSectionName(const macho_section<typename A::P>* sect)
4282 {
4283 const char* name = sect->sectname();
4284 if ( strlen(name) < 16 )
4285 return name;
4286
4287 // special case common long section names so we don't have to malloc
4288 if ( strncmp(sect->sectname(), "__objc_classrefs", 16) == 0 )
4289 return "__objc_classrefs";
4290 if ( strncmp(sect->sectname(), "__objc_classlist", 16) == 0 )
4291 return "__objc_classlist";
4292 if ( strncmp(sect->sectname(), "__objc_nlclslist", 16) == 0 )
4293 return "__objc_nlclslist";
4294 if ( strncmp(sect->sectname(), "__objc_nlcatlist", 16) == 0 )
4295 return "__objc_nlcatlist";
4296 if ( strncmp(sect->sectname(), "__objc_protolist", 16) == 0 )
4297 return "__objc_protolist";
4298 if ( strncmp(sect->sectname(), "__objc_protorefs", 16) == 0 )
4299 return "__objc_protorefs";
4300 if ( strncmp(sect->sectname(), "__objc_superrefs", 16) == 0 )
4301 return "__objc_superrefs";
4302 if ( strncmp(sect->sectname(), "__objc_imageinfo", 16) == 0 )
4303 return "__objc_imageinfo";
4304 if ( strncmp(sect->sectname(), "__objc_stringobj", 16) == 0 )
4305 return "__objc_stringobj";
4306 if ( strncmp(sect->sectname(), "__gcc_except_tab", 16) == 0 )
4307 return "__gcc_except_tab";
4308
4309 char* tmp = new char[17];
4310 strlcpy(tmp, name, 17);
4311 return tmp;
4312 }
4313
4314 template <typename A>
4315 bool Section<A>::readable(const macho_section<typename A::P>* sect)
4316 {
4317 return true;
4318 }
4319
4320 template <typename A>
4321 bool Section<A>::writable(const macho_section<typename A::P>* sect)
4322 {
4323 // mach-o .o files do not contain segment permissions
4324 // we just know TEXT is special
4325 return ( strcmp(sect->segname(), "__TEXT") != 0 );
4326 }
4327
4328 template <typename A>
4329 bool Section<A>::exectuable(const macho_section<typename A::P>* sect)
4330 {
4331 // mach-o .o files do not contain segment permissions
4332 // we just know TEXT is special
4333 return ( strcmp(sect->segname(), "__TEXT") == 0 );
4334 }
4335
4336
4337 template <typename A>
4338 ld::Section::Type Section<A>::sectionType(const macho_section<typename A::P>* sect)
4339 {
4340 switch ( sect->flags() & SECTION_TYPE ) {
4341 case S_ZEROFILL:
4342 return ld::Section::typeZeroFill;
4343 case S_CSTRING_LITERALS:
4344 if ( (strcmp(sect->sectname(), "__cstring") == 0) && (strcmp(sect->segname(), "__TEXT") == 0) )
4345 return ld::Section::typeCString;
4346 else
4347 return ld::Section::typeNonStdCString;
4348 case S_4BYTE_LITERALS:
4349 return ld::Section::typeLiteral4;
4350 case S_8BYTE_LITERALS:
4351 return ld::Section::typeLiteral8;
4352 case S_LITERAL_POINTERS:
4353 return ld::Section::typeCStringPointer;
4354 case S_NON_LAZY_SYMBOL_POINTERS:
4355 return ld::Section::typeNonLazyPointer;
4356 case S_LAZY_SYMBOL_POINTERS:
4357 return ld::Section::typeLazyPointer;
4358 case S_SYMBOL_STUBS:
4359 return ld::Section::typeStub;
4360 case S_MOD_INIT_FUNC_POINTERS:
4361 return ld::Section::typeInitializerPointers;
4362 case S_MOD_TERM_FUNC_POINTERS:
4363 return ld::Section::typeTerminatorPointers;
4364 case S_INTERPOSING:
4365 return ld::Section::typeUnclassified;
4366 case S_16BYTE_LITERALS:
4367 return ld::Section::typeLiteral16;
4368 case S_REGULAR:
4369 case S_COALESCED:
4370 if ( sect->flags() & S_ATTR_PURE_INSTRUCTIONS ) {
4371 return ld::Section::typeCode;
4372 }
4373 else if ( strcmp(sect->segname(), "__TEXT") == 0 ) {
4374 if ( strcmp(sect->sectname(), "__eh_frame") == 0 )
4375 return ld::Section::typeCFI;
4376 else if ( strcmp(sect->sectname(), "__ustring") == 0 )
4377 return ld::Section::typeUTF16Strings;
4378 else if ( strcmp(sect->sectname(), "__textcoal_nt") == 0 )
4379 return ld::Section::typeCode;
4380 else if ( strcmp(sect->sectname(), "__StaticInit") == 0 )
4381 return ld::Section::typeCode;
4382 else if ( strcmp(sect->sectname(), "__constructor") == 0 )
4383 return ld::Section::typeInitializerPointers;
4384 }
4385 else if ( strcmp(sect->segname(), "__DATA") == 0 ) {
4386 if ( strcmp(sect->sectname(), "__cfstring") == 0 )
4387 return ld::Section::typeCFString;
4388 else if ( strcmp(sect->sectname(), "__dyld") == 0 )
4389 return ld::Section::typeDyldInfo;
4390 else if ( strcmp(sect->sectname(), "__program_vars") == 0 )
4391 return ld::Section::typeDyldInfo;
4392 else if ( strncmp(sect->sectname(), "__objc_classrefs", 16) == 0 )
4393 return ld::Section::typeObjCClassRefs;
4394 else if ( strcmp(sect->sectname(), "__objc_catlist") == 0 )
4395 return ld::Section::typeObjC2CategoryList;
4396 }
4397 else if ( strcmp(sect->segname(), "__OBJC") == 0 ) {
4398 if ( strcmp(sect->sectname(), "__class") == 0 )
4399 return ld::Section::typeObjC1Classes;
4400 }
4401 break;
4402 case S_THREAD_LOCAL_REGULAR:
4403 return ld::Section::typeTLVInitialValues;
4404 case S_THREAD_LOCAL_ZEROFILL:
4405 return ld::Section::typeTLVZeroFill;
4406 case S_THREAD_LOCAL_VARIABLES:
4407 return ld::Section::typeTLVDefs;
4408 case S_THREAD_LOCAL_VARIABLE_POINTERS:
4409 return ld::Section::typeTLVPointers;
4410 case S_THREAD_LOCAL_INIT_FUNCTION_POINTERS:
4411 return ld::Section::typeTLVInitializerPointers;
4412 }
4413 return ld::Section::typeUnclassified;
4414 }
4415
4416
4417 template <typename A>
4418 Atom<A>* Section<A>::findContentAtomByAddress(pint_t addr, class Atom<A>* start, class Atom<A>* end)
4419 {
4420 // do a binary search of atom array
4421 uint32_t atomCount = end - start;
4422 Atom<A>* base = start;
4423 for (uint32_t n = atomCount; n > 0; n /= 2) {
4424 Atom<A>* pivot = &base[n/2];
4425 pint_t atomStartAddr = pivot->_objAddress;
4426 pint_t atomEndAddr = atomStartAddr + pivot->_size;
4427 if ( atomStartAddr <= addr ) {
4428 // address in normal atom
4429 if (addr < atomEndAddr)
4430 return pivot;
4431 // address in "end" label (but not in alias)
4432 if ( (pivot->_size == 0) && (addr == atomEndAddr) && !pivot->isAlias() )
4433 return pivot;
4434 }
4435 if ( addr >= atomEndAddr ) {
4436 // key > pivot
4437 // move base to atom after pivot
4438 base = &pivot[1];
4439 --n;
4440 }
4441 else {
4442 // key < pivot
4443 // keep same base
4444 }
4445 }
4446 return NULL;
4447 }
4448
4449 template <typename A>
4450 ld::Atom::Alignment Section<A>::alignmentForAddress(pint_t addr)
4451 {
4452 const uint32_t sectionAlignment = this->_machOSection->align();
4453 uint32_t modulus = (addr % (1 << sectionAlignment));
4454 if ( modulus > 0xFFFF )
4455 warning("alignment for symbol at address 0x%08llX in %s exceeds 2^16", (uint64_t)addr, this->file().path());
4456 return ld::Atom::Alignment(sectionAlignment, modulus);
4457 }
4458
4459 template <typename A>
4460 uint32_t Section<A>::sectionNum(class Parser<A>& parser) const
4461 {
4462 if ( _machOSection == NULL )
4463 return 0;
4464 else
4465 return 1 + (this->_machOSection - parser.firstMachOSection());
4466 }
4467
4468 // arm does not have zero cost exceptions
4469 template <>
4470 uint32_t CFISection<arm>::cfiCount(Parser<arm>& parser)
4471 {
4472 if ( parser.armUsesZeroCostExceptions() ) {
4473 // create ObjectAddressSpace object for use by libunwind
4474 OAS oas(*this, (uint8_t*)this->file().fileContent()+this->_machOSection->offset());
4475 return libunwind::CFI_Parser<OAS>::getCFICount(oas,
4476 this->_machOSection->addr(), this->_machOSection->size());
4477 }
4478 return 0;
4479 }
4480
4481 template <typename A>
4482 uint32_t CFISection<A>::cfiCount(Parser<A>& parser)
4483 {
4484 // create ObjectAddressSpace object for use by libunwind
4485 OAS oas(*this, (uint8_t*)this->file().fileContent()+this->_machOSection->offset());
4486 return libunwind::CFI_Parser<OAS>::getCFICount(oas,
4487 this->_machOSection->addr(), this->_machOSection->size());
4488 }
4489
4490 template <typename A>
4491 void CFISection<A>::warnFunc(void* ref, uint64_t funcAddr, const char* msg)
4492 {
4493 Parser<A>* parser = (Parser<A>*)ref;
4494 if ( ! parser->warnUnwindConversionProblems() )
4495 return;
4496 if ( funcAddr != CFI_INVALID_ADDRESS ) {
4497 // atoms are not constructed yet, so scan symbol table for labels
4498 const char* name = parser->scanSymbolTableForAddress(funcAddr);
4499 warning("could not create compact unwind for %s: %s", name, msg);
4500 }
4501 else {
4502 warning("could not create compact unwind: %s", msg);
4503 }
4504 }
4505
4506 template <>
4507 bool CFISection<x86_64>::needsRelocating()
4508 {
4509 return true;
4510 }
4511
4512 template <>
4513 bool CFISection<arm64>::needsRelocating()
4514 {
4515 return true;
4516 }
4517
4518
4519 template <typename A>
4520 bool CFISection<A>::needsRelocating()
4521 {
4522 return false;
4523 }
4524
4525 template <>
4526 void CFISection<x86_64>::cfiParse(class Parser<x86_64>& parser, uint8_t* buffer,
4527 libunwind::CFI_Atom_Info<CFISection<x86_64>::OAS> cfiArray[],
4528 uint32_t& count, const pint_t cuStarts[], uint32_t cuCount)
4529 {
4530 const uint32_t sectionSize = this->_machOSection->size();
4531 // copy __eh_frame data to buffer
4532 memcpy(buffer, file().fileContent() + this->_machOSection->offset(), sectionSize);
4533
4534 // and apply relocations
4535 const macho_relocation_info<P>* relocs = (macho_relocation_info<P>*)(file().fileContent() + this->_machOSection->reloff());
4536 const macho_relocation_info<P>* relocsEnd = &relocs[this->_machOSection->nreloc()];
4537 for (const macho_relocation_info<P>* reloc = relocs; reloc < relocsEnd; ++reloc) {
4538 uint64_t value = 0;
4539 switch ( reloc->r_type() ) {
4540 case X86_64_RELOC_SUBTRACTOR:
4541 value = 0 - parser.symbolFromIndex(reloc->r_symbolnum()).n_value();
4542 ++reloc;
4543 if ( reloc->r_extern() )
4544 value += parser.symbolFromIndex(reloc->r_symbolnum()).n_value();
4545 break;
4546 case X86_64_RELOC_UNSIGNED:
4547 value = parser.symbolFromIndex(reloc->r_symbolnum()).n_value();
4548 break;
4549 case X86_64_RELOC_GOT:
4550 // this is used for the reference to the personality function in CIEs
4551 // store the symbol number of the personality function for later use as a Fixup
4552 value = reloc->r_symbolnum();
4553 break;
4554 default:
4555 fprintf(stderr, "CFISection::cfiParse() unexpected relocation type at r_address=0x%08X\n", reloc->r_address());
4556 break;
4557 }
4558 if ( reloc->r_address() > sectionSize )
4559 throwf("malformed __eh_frame relocation, offset (0x%08X) is beyond end of section,", reloc->r_address());
4560 uint64_t* p64;
4561 uint32_t* p32;
4562 switch ( reloc->r_length() ) {
4563 case 3:
4564 p64 = (uint64_t*)&buffer[reloc->r_address()];
4565 E::set64(*p64, value + E::get64(*p64));
4566 break;
4567 case 2:
4568 p32 = (uint32_t*)&buffer[reloc->r_address()];
4569 E::set32(*p32, value + E::get32(*p32));
4570 break;
4571 default:
4572 fprintf(stderr, "CFISection::cfiParse() unexpected relocation size at r_address=0x%08X\n", reloc->r_address());
4573 break;
4574 }
4575 }
4576
4577 // create ObjectAddressSpace object for use by libunwind
4578 OAS oas(*this, buffer);
4579
4580 // use libuwind to parse __eh_frame data into array of CFI_Atom_Info
4581 const char* msg;
4582 msg = libunwind::DwarfInstructions<OAS, libunwind::Registers_x86_64>::parseCFIs(
4583 oas, this->_machOSection->addr(), this->_machOSection->size(),
4584 cuStarts, cuCount, parser.keepDwarfUnwind(), parser.forceDwarfConversion(), parser.neverConvertDwarf(),
4585 cfiArray, count, (void*)&parser, warnFunc);
4586 if ( msg != NULL )
4587 throwf("malformed __eh_frame section: %s", msg);
4588 }
4589
4590 template <>
4591 void CFISection<x86>::cfiParse(class Parser<x86>& parser, uint8_t* buffer,
4592 libunwind::CFI_Atom_Info<CFISection<x86>::OAS> cfiArray[],
4593 uint32_t& count, const pint_t cuStarts[], uint32_t cuCount)
4594 {
4595 // create ObjectAddressSpace object for use by libunwind
4596 OAS oas(*this, (uint8_t*)this->file().fileContent()+this->_machOSection->offset());
4597
4598 // use libuwind to parse __eh_frame data into array of CFI_Atom_Info
4599 const char* msg;
4600 msg = libunwind::DwarfInstructions<OAS, libunwind::Registers_x86>::parseCFIs(
4601 oas, this->_machOSection->addr(), this->_machOSection->size(),
4602 cuStarts, cuCount, parser.keepDwarfUnwind(), parser.forceDwarfConversion(), parser.neverConvertDwarf(),
4603 cfiArray, count, (void*)&parser, warnFunc);
4604 if ( msg != NULL )
4605 throwf("malformed __eh_frame section: %s", msg);
4606 }
4607
4608
4609
4610
4611 template <>
4612 void CFISection<arm>::cfiParse(class Parser<arm>& parser, uint8_t* buffer,
4613 libunwind::CFI_Atom_Info<CFISection<arm>::OAS> cfiArray[],
4614 uint32_t& count, const pint_t cuStarts[], uint32_t cuCount)
4615 {
4616 if ( !parser.armUsesZeroCostExceptions() ) {
4617 // most arm do not use zero cost exceptions
4618 assert(count == 0);
4619 return;
4620 }
4621 // create ObjectAddressSpace object for use by libunwind
4622 OAS oas(*this, (uint8_t*)this->file().fileContent()+this->_machOSection->offset());
4623
4624 // use libuwind to parse __eh_frame data into array of CFI_Atom_Info
4625 const char* msg;
4626 msg = libunwind::DwarfInstructions<OAS, libunwind::Registers_arm>::parseCFIs(
4627 oas, this->_machOSection->addr(), this->_machOSection->size(),
4628 cuStarts, cuCount, parser.keepDwarfUnwind(), parser.forceDwarfConversion(), parser.neverConvertDwarf(),
4629 cfiArray, count, (void*)&parser, warnFunc);
4630 if ( msg != NULL )
4631 throwf("malformed __eh_frame section: %s", msg);
4632 }
4633
4634
4635
4636
4637 template <>
4638 void CFISection<arm64>::cfiParse(class Parser<arm64>& parser, uint8_t* buffer,
4639 libunwind::CFI_Atom_Info<CFISection<arm64>::OAS> cfiArray[],
4640 uint32_t& count, const pint_t cuStarts[], uint32_t cuCount)
4641 {
4642 // copy __eh_frame data to buffer
4643 const uint32_t sectionSize = this->_machOSection->size();
4644 memcpy(buffer, file().fileContent() + this->_machOSection->offset(), sectionSize);
4645
4646 // and apply relocations
4647 const macho_relocation_info<P>* relocs = (macho_relocation_info<P>*)(file().fileContent() + this->_machOSection->reloff());
4648 const macho_relocation_info<P>* relocsEnd = &relocs[this->_machOSection->nreloc()];
4649 for (const macho_relocation_info<P>* reloc = relocs; reloc < relocsEnd; ++reloc) {
4650 uint64_t* p64 = (uint64_t*)&buffer[reloc->r_address()];
4651 uint32_t* p32 = (uint32_t*)&buffer[reloc->r_address()];
4652 uint32_t addend32 = E::get32(*p32);
4653 uint64_t addend64 = E::get64(*p64);
4654 uint64_t value = 0;
4655 switch ( reloc->r_type() ) {
4656 case ARM64_RELOC_SUBTRACTOR:
4657 value = 0 - parser.symbolFromIndex(reloc->r_symbolnum()).n_value();
4658 ++reloc;
4659 if ( reloc->r_extern() )
4660 value += parser.symbolFromIndex(reloc->r_symbolnum()).n_value();
4661 break;
4662 case ARM64_RELOC_UNSIGNED:
4663 value = parser.symbolFromIndex(reloc->r_symbolnum()).n_value();
4664 break;
4665 case ARM64_RELOC_POINTER_TO_GOT:
4666 // this is used for the reference to the personality function in CIEs
4667 // store the symbol number of the personality function for later use as a Fixup
4668 value = reloc->r_symbolnum();
4669 addend32 = 0;
4670 addend64 = 0;
4671 break;
4672 default:
4673 fprintf(stderr, "CFISection::cfiParse() unexpected relocation type at r_address=0x%08X\n", reloc->r_address());
4674 break;
4675 }
4676 if ( reloc->r_address() > sectionSize )
4677 throwf("malformed __eh_frame relocation, offset (0x%08X) is beyond end of section,", reloc->r_address());
4678 switch ( reloc->r_length() ) {
4679 case 3:
4680 E::set64(*p64, value + addend64);
4681 break;
4682 case 2:
4683 E::set32(*p32, value + addend32);
4684 break;
4685 default:
4686 fprintf(stderr, "CFISection::cfiParse() unexpected relocation size at r_address=0x%08X\n", reloc->r_address());
4687 break;
4688 }
4689 }
4690
4691
4692 // create ObjectAddressSpace object for use by libunwind
4693 OAS oas(*this, buffer);
4694
4695 // use libuwind to parse __eh_frame data into array of CFI_Atom_Info
4696 const char* msg;
4697 msg = libunwind::DwarfInstructions<OAS, libunwind::Registers_arm64>::parseCFIs(
4698 oas, this->_machOSection->addr(), this->_machOSection->size(),
4699 cuStarts, cuCount, parser.keepDwarfUnwind(), parser.forceDwarfConversion(), parser.neverConvertDwarf(),
4700 cfiArray, count, (void*)&parser, warnFunc);
4701 if ( msg != NULL )
4702 throwf("malformed __eh_frame section: %s", msg);
4703 }
4704
4705
4706 template <typename A>
4707 uint32_t CFISection<A>::computeAtomCount(class Parser<A>& parser,
4708 struct Parser<A>::LabelAndCFIBreakIterator& it,
4709 const struct Parser<A>::CFI_CU_InfoArrays& cfis)
4710 {
4711 return cfis.cfiCount;
4712 }
4713
4714
4715
4716 template <typename A>
4717 uint32_t CFISection<A>::appendAtoms(class Parser<A>& parser, uint8_t* p,
4718 struct Parser<A>::LabelAndCFIBreakIterator& it,
4719 const struct Parser<A>::CFI_CU_InfoArrays& cfis)
4720 {
4721 this->_beginAtoms = (Atom<A>*)p;
4722 // walk CFI_Atom_Info array and create atom for each entry
4723 const CFI_Atom_Info* start = &cfis.cfiArray[0];
4724 const CFI_Atom_Info* end = &cfis.cfiArray[cfis.cfiCount];
4725 for(const CFI_Atom_Info* a=start; a < end; ++a) {
4726 Atom<A>* space = (Atom<A>*)p;
4727 new (space) Atom<A>(*this, (a->isCIE ? "CIE" : "FDE"), a->address, a->size,
4728 ld::Atom::definitionRegular, ld::Atom::combineNever, ld::Atom::scopeTranslationUnit,
4729 ld::Atom::typeCFI, ld::Atom::symbolTableNotInFinalLinkedImages,
4730 false, false, false, ld::Atom::Alignment(0));
4731 p += sizeof(Atom<A>);
4732 }
4733 this->_endAtoms = (Atom<A>*)p;
4734 return cfis.cfiCount;
4735 }
4736
4737
4738 template <> bool CFISection<x86_64>::bigEndian() { return false; }
4739 template <> bool CFISection<x86>::bigEndian() { return false; }
4740 template <> bool CFISection<arm>::bigEndian() { return false; }
4741 template <> bool CFISection<arm64>::bigEndian() { return false; }
4742
4743 template <>
4744 void CFISection<x86_64>::addCiePersonalityFixups(class Parser<x86_64>& parser, const CFI_Atom_Info* cieInfo)
4745 {
4746 uint8_t personalityEncoding = cieInfo->u.cieInfo.personality.encodingOfTargetAddress;
4747 if ( personalityEncoding == 0x9B ) {
4748 // compiler always produces X86_64_RELOC_GOT with addend of 4 to personality function
4749 // CFISection<x86_64>::cfiParse() set targetAddress to be symbolIndex + 4 + addressInCIE
4750 uint32_t symbolIndex = cieInfo->u.cieInfo.personality.targetAddress - 4
4751 - cieInfo->address - cieInfo->u.cieInfo.personality.offsetInCFI;
4752 const macho_nlist<P>& sym = parser.symbolFromIndex(symbolIndex);
4753 const char* personalityName = parser.nameFromSymbol(sym);
4754
4755 Atom<x86_64>* cieAtom = this->findAtomByAddress(cieInfo->address);
4756 Parser<x86_64>::SourceLocation src(cieAtom, cieInfo->u.cieInfo.personality.offsetInCFI);
4757 parser.addFixup(src, ld::Fixup::k1of3, ld::Fixup::kindSetTargetAddress, false, personalityName);
4758 parser.addFixup(src, ld::Fixup::k2of3, ld::Fixup::kindAddAddend, 4);
4759 parser.addFixup(src, ld::Fixup::k3of3, ld::Fixup::kindStoreX86PCRel32GOT);
4760 }
4761 else if ( personalityEncoding != 0 ) {
4762 throwf("unsupported address encoding (%02X) of personality function in CIE",
4763 personalityEncoding);
4764 }
4765 }
4766
4767 template <>
4768 void CFISection<x86>::addCiePersonalityFixups(class Parser<x86>& parser, const CFI_Atom_Info* cieInfo)
4769 {
4770 uint8_t personalityEncoding = cieInfo->u.cieInfo.personality.encodingOfTargetAddress;
4771 if ( (personalityEncoding == 0x9B) || (personalityEncoding == 0x90) ) {
4772 uint32_t offsetInCFI = cieInfo->u.cieInfo.personality.offsetInCFI;
4773 uint32_t nlpAddr = cieInfo->u.cieInfo.personality.targetAddress;
4774 Atom<x86>* cieAtom = this->findAtomByAddress(cieInfo->address);
4775 Atom<x86>* nlpAtom = parser.findAtomByAddress(nlpAddr);
4776 assert(nlpAtom->contentType() == ld::Atom::typeNonLazyPointer);
4777 Parser<x86>::SourceLocation src(cieAtom, cieInfo->u.cieInfo.personality.offsetInCFI);
4778
4779 parser.addFixup(src, ld::Fixup::k1of4, ld::Fixup::kindSetTargetAddress, ld::Fixup::bindingByContentBound, nlpAtom);
4780 parser.addFixup(src, ld::Fixup::k2of4, ld::Fixup::kindSubtractTargetAddress, cieAtom);
4781 parser.addFixup(src, ld::Fixup::k3of4, ld::Fixup::kindSubtractAddend, offsetInCFI);
4782 parser.addFixup(src, ld::Fixup::k4of4, ld::Fixup::kindStoreLittleEndian32);
4783 }
4784 else if ( personalityEncoding != 0 ) {
4785 throwf("unsupported address encoding (%02X) of personality function in CIE", personalityEncoding);
4786 }
4787 }
4788
4789 #if SUPPORT_ARCH_arm64
4790 template <>
4791 void CFISection<arm64>::addCiePersonalityFixups(class Parser<arm64>& parser, const CFI_Atom_Info* cieInfo)
4792 {
4793 uint8_t personalityEncoding = cieInfo->u.cieInfo.personality.encodingOfTargetAddress;
4794 if ( personalityEncoding == 0x9B ) {
4795 // compiler always produces ARM64_RELOC_GOT r_pcrel=1 to personality function
4796 // CFISection<arm64>::cfiParse() set targetAddress to be symbolIndex + addressInCIE
4797 uint32_t symbolIndex = cieInfo->u.cieInfo.personality.targetAddress
4798 - cieInfo->address - cieInfo->u.cieInfo.personality.offsetInCFI;
4799 const macho_nlist<P>& sym = parser.symbolFromIndex(symbolIndex);
4800 const char* personalityName = parser.nameFromSymbol(sym);
4801
4802 Atom<arm64>* cieAtom = this->findAtomByAddress(cieInfo->address);
4803 Parser<arm64>::SourceLocation src(cieAtom, cieInfo->u.cieInfo.personality.offsetInCFI);
4804 parser.addFixup(src, ld::Fixup::k1of2, ld::Fixup::kindSetTargetAddress, false, personalityName);
4805 parser.addFixup(src, ld::Fixup::k2of2, ld::Fixup::kindStoreARM64PCRelToGOT);
4806 }
4807 else if ( personalityEncoding != 0 ) {
4808 throwf("unsupported address encoding (%02X) of personality function in CIE",
4809 personalityEncoding);
4810 }
4811 }
4812 #endif
4813
4814
4815 template <>
4816 void CFISection<arm>::addCiePersonalityFixups(class Parser<arm>& parser, const CFI_Atom_Info* cieInfo)
4817 {
4818 uint8_t personalityEncoding = cieInfo->u.cieInfo.personality.encodingOfTargetAddress;
4819 if ( (personalityEncoding == 0x9B) || (personalityEncoding == 0x90) ) {
4820 uint32_t offsetInCFI = cieInfo->u.cieInfo.personality.offsetInCFI;
4821 uint32_t nlpAddr = cieInfo->u.cieInfo.personality.targetAddress;
4822 Atom<arm>* cieAtom = this->findAtomByAddress(cieInfo->address);
4823 Atom<arm>* nlpAtom = parser.findAtomByAddress(nlpAddr);
4824 assert(nlpAtom->contentType() == ld::Atom::typeNonLazyPointer);
4825 Parser<arm>::SourceLocation src(cieAtom, cieInfo->u.cieInfo.personality.offsetInCFI);
4826
4827 parser.addFixup(src, ld::Fixup::k1of4, ld::Fixup::kindSetTargetAddress, ld::Fixup::bindingByContentBound, nlpAtom);
4828 parser.addFixup(src, ld::Fixup::k2of4, ld::Fixup::kindSubtractTargetAddress, cieAtom);
4829 parser.addFixup(src, ld::Fixup::k3of4, ld::Fixup::kindSubtractAddend, offsetInCFI);
4830 parser.addFixup(src, ld::Fixup::k4of4, ld::Fixup::kindStoreLittleEndian32);
4831 }
4832 else if ( personalityEncoding != 0 ) {
4833 throwf("unsupported address encoding (%02X) of personality function in CIE", personalityEncoding);
4834 }
4835 }
4836
4837
4838
4839 template <typename A>
4840 void CFISection<A>::addCiePersonalityFixups(class Parser<A>& parser, const CFI_Atom_Info* cieInfo)
4841 {
4842 assert(0 && "addCiePersonalityFixups() not implemented for arch");
4843 }
4844
4845 template <typename A>
4846 void CFISection<A>::makeFixups(class Parser<A>& parser, const struct Parser<A>::CFI_CU_InfoArrays& cfis)
4847 {
4848 ld::Fixup::Kind store32 = bigEndian() ? ld::Fixup::kindStoreBigEndian32 : ld::Fixup::kindStoreLittleEndian32;
4849 ld::Fixup::Kind store64 = bigEndian() ? ld::Fixup::kindStoreBigEndian64 : ld::Fixup::kindStoreLittleEndian64;
4850
4851 // add all references for FDEs, including implicit group references
4852 const CFI_Atom_Info* end = &cfis.cfiArray[cfis.cfiCount];
4853 for(const CFI_Atom_Info* p = &cfis.cfiArray[0]; p < end; ++p) {
4854 if ( p->isCIE ) {
4855 // add reference to personality function if used
4856 if ( p->u.cieInfo.personality.targetAddress != CFI_INVALID_ADDRESS ) {
4857 this->addCiePersonalityFixups(parser, p);
4858 }
4859 }
4860 else {
4861 // find FDE Atom
4862 Atom<A>* fdeAtom = this->findAtomByAddress(p->address);
4863 // find function Atom
4864 Atom<A>* functionAtom = parser.findAtomByAddress(p->u.fdeInfo.function.targetAddress);
4865 // find CIE Atom
4866 Atom<A>* cieAtom = this->findAtomByAddress(p->u.fdeInfo.cie.targetAddress);
4867 // find LSDA Atom
4868 Atom<A>* lsdaAtom = NULL;
4869 if ( p->u.fdeInfo.lsda.targetAddress != CFI_INVALID_ADDRESS ) {
4870 lsdaAtom = parser.findAtomByAddress(p->u.fdeInfo.lsda.targetAddress);
4871 }
4872 // add reference from FDE to CIE (always 32-bit pc-rel)
4873 typename Parser<A>::SourceLocation fdeToCieSrc(fdeAtom, p->u.fdeInfo.cie.offsetInCFI);
4874 parser.addFixup(fdeToCieSrc, ld::Fixup::k1of4, ld::Fixup::kindSetTargetAddress, fdeAtom);
4875 parser.addFixup(fdeToCieSrc, ld::Fixup::k2of4, ld::Fixup::kindAddAddend, p->u.fdeInfo.cie.offsetInCFI);
4876 parser.addFixup(fdeToCieSrc, ld::Fixup::k3of4, ld::Fixup::kindSubtractTargetAddress, cieAtom);
4877 parser.addFixup(fdeToCieSrc, ld::Fixup::k4of4, store32, cieAtom);
4878
4879 // add reference from FDE to function
4880 typename Parser<A>::SourceLocation fdeToFuncSrc(fdeAtom, p->u.fdeInfo.function.offsetInCFI);
4881 switch (p->u.fdeInfo.function.encodingOfTargetAddress) {
4882 case DW_EH_PE_pcrel|DW_EH_PE_ptr:
4883 if ( sizeof(typename A::P::uint_t) == 8 ) {
4884 parser.addFixup(fdeToFuncSrc, ld::Fixup::k1of4, ld::Fixup::kindSetTargetAddress, functionAtom);
4885 parser.addFixup(fdeToFuncSrc, ld::Fixup::k2of4, ld::Fixup::kindSubtractTargetAddress, fdeAtom);
4886 parser.addFixup(fdeToFuncSrc, ld::Fixup::k3of4, ld::Fixup::kindSubtractAddend, p->u.fdeInfo.function.offsetInCFI);
4887 parser.addFixup(fdeToFuncSrc, ld::Fixup::k4of4, store64);
4888 break;
4889 }
4890 // else fall into 32-bit case
4891 case DW_EH_PE_pcrel|DW_EH_PE_sdata4:
4892 parser.addFixup(fdeToFuncSrc, ld::Fixup::k1of4, ld::Fixup::kindSetTargetAddress, functionAtom);
4893 parser.addFixup(fdeToFuncSrc, ld::Fixup::k2of4, ld::Fixup::kindSubtractTargetAddress, fdeAtom);
4894 parser.addFixup(fdeToFuncSrc, ld::Fixup::k3of4, ld::Fixup::kindSubtractAddend, p->u.fdeInfo.function.offsetInCFI);
4895 parser.addFixup(fdeToFuncSrc, ld::Fixup::k4of4, store32);
4896 break;
4897 default:
4898 throw "unsupported encoding in FDE of pointer to function";
4899 }
4900
4901 // add reference from FDE to LSDA
4902 typename Parser<A>::SourceLocation fdeToLsdaSrc(fdeAtom, p->u.fdeInfo.lsda.offsetInCFI);
4903 if ( lsdaAtom != NULL ) {
4904 switch (p->u.fdeInfo.lsda.encodingOfTargetAddress) {
4905 case DW_EH_PE_pcrel|DW_EH_PE_ptr:
4906 if ( sizeof(typename A::P::uint_t) == 8 ) {
4907 parser.addFixup(fdeToLsdaSrc, ld::Fixup::k1of4, ld::Fixup::kindSetTargetAddress, lsdaAtom);
4908 parser.addFixup(fdeToLsdaSrc, ld::Fixup::k2of4, ld::Fixup::kindSubtractTargetAddress, fdeAtom);
4909 parser.addFixup(fdeToLsdaSrc, ld::Fixup::k3of4, ld::Fixup::kindSubtractAddend, p->u.fdeInfo.lsda.offsetInCFI);
4910 parser.addFixup(fdeToLsdaSrc, ld::Fixup::k4of4, store64);
4911 break;
4912 }
4913 // else fall into 32-bit case
4914 case DW_EH_PE_pcrel|DW_EH_PE_sdata4:
4915 parser.addFixup(fdeToLsdaSrc, ld::Fixup::k1of4, ld::Fixup::kindSetTargetAddress, lsdaAtom);
4916 parser.addFixup(fdeToLsdaSrc, ld::Fixup::k2of4, ld::Fixup::kindSubtractTargetAddress, fdeAtom);
4917 parser.addFixup(fdeToLsdaSrc, ld::Fixup::k3of4, ld::Fixup::kindSubtractAddend, p->u.fdeInfo.lsda.offsetInCFI);
4918 parser.addFixup(fdeToLsdaSrc, ld::Fixup::k4of4, store32);
4919 break;
4920 default:
4921 throw "unsupported encoding in FDE of pointer to LSDA";
4922 }
4923 }
4924
4925 // FDE is in group lead by function atom
4926 typename Parser<A>::SourceLocation fdeSrc(functionAtom,0);
4927 parser.addFixup(fdeSrc, ld::Fixup::k1of1, ld::Fixup::kindNoneGroupSubordinateFDE, fdeAtom);
4928
4929 // LSDA is in group lead by function atom
4930 if ( lsdaAtom != NULL ) {
4931 parser.addFixup(fdeSrc, ld::Fixup::k1of1, ld::Fixup::kindNoneGroupSubordinateLSDA, lsdaAtom);
4932 }
4933 }
4934 }
4935 }
4936
4937
4938
4939
4940 template <typename A>
4941 const void* CFISection<A>::OAS::mappedAddress(pint_t addr)
4942 {
4943 if ( (_ehFrameStartAddr <= addr) && (addr < _ehFrameEndAddr) )
4944 return &_ehFrameContent[addr-_ehFrameStartAddr];
4945 else {
4946 // requested bytes are not in __eh_frame section
4947 // this can occur when examining the instruction bytes in the __text
4948 File<A>& file = _ehFrameSection.file();
4949 for (uint32_t i=0; i < file._sectionsArrayCount; ++i ) {
4950 const macho_section<typename A::P>* sect = file._sectionsArray[i]->machoSection();
4951 // TentativeDefinitionSection and AbsoluteSymbolSection have no mach-o section
4952 if ( sect != NULL ) {
4953 if ( (sect->addr() <= addr) && (addr < (sect->addr()+sect->size())) ) {
4954 return file.fileContent() + sect->offset() + addr - sect->addr();
4955 }
4956 }
4957 }
4958 throwf("__eh_frame parsing problem. Can't find target of reference to address 0x%08llX", (uint64_t)addr);
4959 }
4960 }
4961
4962
4963 template <typename A>
4964 uint64_t CFISection<A>::OAS::getULEB128(pint_t& logicalAddr, pint_t end)
4965 {
4966 uintptr_t size = (end - logicalAddr);
4967 libunwind::LocalAddressSpace::pint_t laddr = (libunwind::LocalAddressSpace::pint_t)mappedAddress(logicalAddr);
4968 libunwind::LocalAddressSpace::pint_t sladdr = laddr;
4969 uint64_t result = libunwind::LocalAddressSpace::getULEB128(laddr, laddr+size);
4970 logicalAddr += (laddr-sladdr);
4971 return result;
4972 }
4973
4974 template <typename A>
4975 int64_t CFISection<A>::OAS::getSLEB128(pint_t& logicalAddr, pint_t end)
4976 {
4977 uintptr_t size = (end - logicalAddr);
4978 libunwind::LocalAddressSpace::pint_t laddr = (libunwind::LocalAddressSpace::pint_t)mappedAddress(logicalAddr);
4979 libunwind::LocalAddressSpace::pint_t sladdr = laddr;
4980 int64_t result = libunwind::LocalAddressSpace::getSLEB128(laddr, laddr+size);
4981 logicalAddr += (laddr-sladdr);
4982 return result;
4983 }
4984
4985 template <typename A>
4986 typename A::P::uint_t CFISection<A>::OAS::getEncodedP(pint_t& addr, pint_t end, uint8_t encoding)
4987 {
4988 pint_t startAddr = addr;
4989 pint_t p = addr;
4990 pint_t result;
4991
4992 // first get value
4993 switch (encoding & 0x0F) {
4994 case DW_EH_PE_ptr:
4995 result = getP(addr);
4996 p += sizeof(pint_t);
4997 addr = (pint_t)p;
4998 break;
4999 case DW_EH_PE_uleb128:
5000 result = getULEB128(addr, end);
5001 break;
5002 case DW_EH_PE_udata2:
5003 result = get16(addr);
5004 p += 2;
5005 addr = (pint_t)p;
5006 break;
5007 case DW_EH_PE_udata4:
5008 result = get32(addr);
5009 p += 4;
5010 addr = (pint_t)p;
5011 break;
5012 case DW_EH_PE_udata8:
5013 result = get64(addr);
5014 p += 8;
5015 addr = (pint_t)p;
5016 break;
5017 case DW_EH_PE_sleb128:
5018 result = getSLEB128(addr, end);
5019 break;
5020 case DW_EH_PE_sdata2:
5021 result = (int16_t)get16(addr);
5022 p += 2;
5023 addr = (pint_t)p;
5024 break;
5025 case DW_EH_PE_sdata4:
5026 result = (int32_t)get32(addr);
5027 p += 4;
5028 addr = (pint_t)p;
5029 break;
5030 case DW_EH_PE_sdata8:
5031 result = get64(addr);
5032 p += 8;
5033 addr = (pint_t)p;
5034 break;
5035 default:
5036 throwf("ObjectFileAddressSpace<A>::getEncodedP() encoding 0x%08X not supported", encoding);
5037 }
5038
5039 // then add relative offset
5040 switch ( encoding & 0x70 ) {
5041 case DW_EH_PE_absptr:
5042 // do nothing
5043 break;
5044 case DW_EH_PE_pcrel:
5045 result += startAddr;
5046 break;
5047 case DW_EH_PE_textrel:
5048 throw "DW_EH_PE_textrel pointer encoding not supported";
5049 break;
5050 case DW_EH_PE_datarel:
5051 throw "DW_EH_PE_datarel pointer encoding not supported";
5052 break;
5053 case DW_EH_PE_funcrel:
5054 throw "DW_EH_PE_funcrel pointer encoding not supported";
5055 break;
5056 case DW_EH_PE_aligned:
5057 throw "DW_EH_PE_aligned pointer encoding not supported";
5058 break;
5059 default:
5060 throwf("ObjectFileAddressSpace<A>::getEncodedP() encoding 0x%08X not supported", encoding);
5061 break;
5062 }
5063
5064 // Note: DW_EH_PE_indirect is only used in CIEs to refernce the personality pointer
5065 // When parsing .o files that pointer contains zero, so we don't to return that.
5066 // Instead we skip the dereference and return the address of the pointer.
5067 // if ( encoding & DW_EH_PE_indirect )
5068 // result = getP(result);
5069
5070 return result;
5071 }
5072
5073 template <>
5074 const char* CUSection<x86_64>::personalityName(class Parser<x86_64>& parser, const macho_relocation_info<x86_64::P>* reloc)
5075 {
5076 if ( reloc->r_extern() ) {
5077 assert((reloc->r_type() == X86_64_RELOC_UNSIGNED) && "wrong reloc type on personality column in __compact_unwind section");
5078 const macho_nlist<P>& sym = parser.symbolFromIndex(reloc->r_symbolnum());
5079 return parser.nameFromSymbol(sym);
5080 }
5081 else {
5082 const pint_t* content = (pint_t*)(this->file().fileContent() + this->_machOSection->offset() + reloc->r_address());
5083 pint_t personalityAddr = *content;
5084 assert((parser.sectionForAddress(personalityAddr)->type() == ld::Section::typeCode) && "personality column in __compact_unwind section is not pointer to function");
5085 // atoms may not be constructed yet, so scan symbol table for labels
5086 const char* name = parser.scanSymbolTableForAddress(personalityAddr);
5087 return name;
5088 }
5089 }
5090
5091 template <>
5092 const char* CUSection<x86>::personalityName(class Parser<x86>& parser, const macho_relocation_info<x86::P>* reloc)
5093 {
5094 if ( reloc->r_extern() ) {
5095 assert((reloc->r_type() == GENERIC_RELOC_VANILLA) && "wrong reloc type on personality column in __compact_unwind section");
5096 const macho_nlist<P>& sym = parser.symbolFromIndex(reloc->r_symbolnum());
5097 return parser.nameFromSymbol(sym);
5098 }
5099 else {
5100 // support __LD, __compact_unwind personality entries which are pointer to personality non-lazy pointer
5101 const pint_t* content = (pint_t*)(this->file().fileContent() + this->_machOSection->offset() + reloc->r_address());
5102 pint_t nlPointerAddr = *content;
5103 Section<x86>* nlSection = parser.sectionForAddress(nlPointerAddr);
5104 if ( nlSection->type() == ld::Section::typeCode ) {
5105 // personality function is defined in this .o file, so this is a direct reference to it
5106 // atoms may not be constructed yet, so scan symbol table for labels
5107 const char* name = parser.scanSymbolTableForAddress(nlPointerAddr);
5108 return name;
5109 }
5110 else {
5111 uint32_t symIndex = parser.symbolIndexFromIndirectSectionAddress(nlPointerAddr, nlSection->machoSection());
5112 const macho_nlist<P>& nlSymbol = parser.symbolFromIndex(symIndex);
5113 return parser.nameFromSymbol(nlSymbol);
5114 }
5115 }
5116 }
5117
5118 #if SUPPORT_ARCH_arm64
5119 template <>
5120 const char* CUSection<arm64>::personalityName(class Parser<arm64>& parser, const macho_relocation_info<arm64::P>* reloc)
5121 {
5122 if ( reloc->r_extern() ) {
5123 assert((reloc->r_type() == ARM64_RELOC_UNSIGNED) && "wrong reloc type on personality column in __compact_unwind section");
5124 const macho_nlist<P>& sym = parser.symbolFromIndex(reloc->r_symbolnum());
5125 return parser.nameFromSymbol(sym);
5126 }
5127 else {
5128 const pint_t* content = (pint_t*)(this->file().fileContent() + this->_machOSection->offset() + reloc->r_address());
5129 pint_t personalityAddr = *content;
5130 Section<arm64>* personalitySection = parser.sectionForAddress(personalityAddr);
5131 (void)personalitySection;
5132 assert((personalitySection->type() == ld::Section::typeCode) && "personality column in __compact_unwind section is not pointer to function");
5133 // atoms may not be constructed yet, so scan symbol table for labels
5134 const char* name = parser.scanSymbolTableForAddress(personalityAddr);
5135 return name;
5136 }
5137 }
5138 #endif
5139
5140
5141 #if SUPPORT_ARCH_arm_any
5142 template <>
5143 const char* CUSection<arm>::personalityName(class Parser<arm>& parser, const macho_relocation_info<arm::P>* reloc)
5144 {
5145 if ( reloc->r_extern() ) {
5146 assert((reloc->r_type() == ARM_RELOC_VANILLA) && "wrong reloc type on personality column in __compact_unwind section");
5147 const macho_nlist<P>& sym = parser.symbolFromIndex(reloc->r_symbolnum());
5148 return parser.nameFromSymbol(sym);
5149 }
5150 else {
5151 // support __LD, __compact_unwind personality entries which are pointer to personality non-lazy pointer
5152 const pint_t* content = (pint_t*)(this->file().fileContent() + this->_machOSection->offset() + reloc->r_address());
5153 pint_t nlPointerAddr = *content;
5154 Section<arm>* nlSection = parser.sectionForAddress(nlPointerAddr);
5155 if ( nlSection->type() == ld::Section::typeCode ) {
5156 // personality function is defined in this .o file, so this is a direct reference to it
5157 // atoms may not be constructed yet, so scan symbol table for labels
5158 const char* name = parser.scanSymbolTableForAddress(nlPointerAddr);
5159 return name;
5160 }
5161 else {
5162 uint32_t symIndex = parser.symbolIndexFromIndirectSectionAddress(nlPointerAddr, nlSection->machoSection());
5163 const macho_nlist<P>& nlSymbol = parser.symbolFromIndex(symIndex);
5164 return parser.nameFromSymbol(nlSymbol);
5165 }
5166 }
5167 }
5168 #endif
5169
5170
5171 template <typename A>
5172 const char* CUSection<A>::personalityName(class Parser<A>& parser, const macho_relocation_info<P>* reloc)
5173 {
5174 return NULL;
5175 }
5176
5177 template <>
5178 bool CUSection<x86>::encodingMeansUseDwarf(compact_unwind_encoding_t enc)
5179 {
5180 return ((enc & UNWIND_X86_MODE_MASK) == UNWIND_X86_MODE_DWARF);
5181 }
5182
5183 template <>
5184 bool CUSection<x86_64>::encodingMeansUseDwarf(compact_unwind_encoding_t enc)
5185 {
5186 return ((enc & UNWIND_X86_64_MODE_MASK) == UNWIND_X86_64_MODE_DWARF);
5187 }
5188
5189 #if SUPPORT_ARCH_arm_any
5190 template <>
5191 bool CUSection<arm>::encodingMeansUseDwarf(compact_unwind_encoding_t enc)
5192 {
5193 return ((enc & UNWIND_ARM_MODE_MASK) == UNWIND_ARM_MODE_DWARF);
5194 }
5195 #endif
5196
5197 #if SUPPORT_ARCH_arm64
5198 template <>
5199 bool CUSection<arm64>::encodingMeansUseDwarf(compact_unwind_encoding_t enc)
5200 {
5201 return ((enc & UNWIND_ARM64_MODE_MASK) == UNWIND_ARM64_MODE_DWARF);
5202 }
5203 #endif
5204
5205
5206 template <typename A>
5207 int CUSection<A>::infoSorter(const void* l, const void* r)
5208 {
5209 // sort references by symbol index, then address
5210 const Info* left = (Info*)l;
5211 const Info* right = (Info*)r;
5212 if ( left->functionSymbolIndex == right->functionSymbolIndex )
5213 return (left->functionStartAddress - right->functionStartAddress);
5214 else
5215 return (left->functionSymbolIndex - right->functionSymbolIndex);
5216 }
5217
5218 template <typename A>
5219 void CUSection<A>::parse(class Parser<A>& parser, uint32_t cnt, Info array[])
5220 {
5221 // walk section content and copy to Info array
5222 const macho_compact_unwind_entry<P>* const entries = (macho_compact_unwind_entry<P>*)(this->file().fileContent() + this->_machOSection->offset());
5223 for (uint32_t i=0; i < cnt; ++i) {
5224 Info* info = &array[i];
5225 const macho_compact_unwind_entry<P>* entry = &entries[i];
5226 info->functionStartAddress = entry->codeStart();
5227 info->functionSymbolIndex = 0xFFFFFFFF;
5228 info->rangeLength = entry->codeLen();
5229 info->compactUnwindInfo = entry->compactUnwindInfo();
5230 info->personality = NULL;
5231 info->lsdaAddress = entry->lsda();
5232 info->function = NULL;
5233 info->lsda = NULL;
5234 if ( (info->compactUnwindInfo & UNWIND_PERSONALITY_MASK) != 0 )
5235 warning("no bits should be set in UNWIND_PERSONALITY_MASK of compact unwind encoding in __LD,__compact_unwind section");
5236 if ( info->lsdaAddress != 0 ) {
5237 info->compactUnwindInfo |= UNWIND_HAS_LSDA;
5238 }
5239 }
5240
5241 // scan relocs, extern relocs are needed for personality references (possibly for function/lsda refs??)
5242 const uint32_t sectionSize = this->_machOSection->size();
5243 const macho_relocation_info<P>* relocs = (macho_relocation_info<P>*)(this->file().fileContent() + this->_machOSection->reloff());
5244 const macho_relocation_info<P>* relocsEnd = &relocs[this->_machOSection->nreloc()];
5245 for (const macho_relocation_info<P>* reloc = relocs; reloc < relocsEnd; ++reloc) {
5246 if ( reloc->r_address() & R_SCATTERED )
5247 continue;
5248 if ( reloc->r_address() > sectionSize )
5249 throwf("malformed __compact_unwind relocation, offset (0x%08X) is beyond end of section,", reloc->r_address());
5250 if ( reloc->r_extern() ) {
5251 // only expect external relocs on some colummns
5252 if ( (reloc->r_address() % sizeof(macho_compact_unwind_entry<P>)) == macho_compact_unwind_entry<P>::personalityFieldOffset() ) {
5253 uint32_t entryIndex = reloc->r_address() / sizeof(macho_compact_unwind_entry<P>);
5254 array[entryIndex].personality = this->personalityName(parser, reloc);
5255 }
5256 else if ( (reloc->r_address() % sizeof(macho_compact_unwind_entry<P>)) == macho_compact_unwind_entry<P>::lsdaFieldOffset() ) {
5257 uint32_t entryIndex = reloc->r_address() / sizeof(macho_compact_unwind_entry<P>);
5258 const macho_nlist<P>& lsdaSym = parser.symbolFromIndex(reloc->r_symbolnum());
5259 if ( (lsdaSym.n_type() & N_TYPE) == N_SECT )
5260 array[entryIndex].lsdaAddress = lsdaSym.n_value();
5261 else
5262 warning("unexpected extern relocation to lsda in __compact_unwind section");
5263 }
5264 else if ( (reloc->r_address() % sizeof(macho_compact_unwind_entry<P>)) == macho_compact_unwind_entry<P>::codeStartFieldOffset() ) {
5265 uint32_t entryIndex = reloc->r_address() / sizeof(macho_compact_unwind_entry<P>);
5266 array[entryIndex].functionSymbolIndex = reloc->r_symbolnum();
5267 array[entryIndex].functionStartAddress += parser.symbolFromIndex(reloc->r_symbolnum()).n_value();
5268 }
5269 else {
5270 warning("unexpected extern relocation in __compact_unwind section");
5271 }
5272 }
5273 else {
5274 if ( (reloc->r_address() % sizeof(macho_compact_unwind_entry<P>)) == macho_compact_unwind_entry<P>::personalityFieldOffset() ) {
5275 uint32_t entryIndex = reloc->r_address() / sizeof(macho_compact_unwind_entry<P>);
5276 array[entryIndex].personality = this->personalityName(parser, reloc);
5277 }
5278 }
5279 }
5280
5281 // sort array by function start address so unwind infos will be contiguous for a given function
5282 ::qsort(array, cnt, sizeof(Info), infoSorter);
5283 }
5284
5285 template <typename A>
5286 uint32_t CUSection<A>::count()
5287 {
5288 const macho_section<P>* machoSect = this->machoSection();
5289 if ( (machoSect->size() % sizeof(macho_compact_unwind_entry<P>)) != 0 )
5290 throw "malformed __LD,__compact_unwind section, bad length";
5291
5292 return machoSect->size() / sizeof(macho_compact_unwind_entry<P>);
5293 }
5294
5295 template <typename A>
5296 void CUSection<A>::makeFixups(class Parser<A>& parser, const struct Parser<A>::CFI_CU_InfoArrays& cus)
5297 {
5298 Info* const arrayStart = cus.cuArray;
5299 Info* const arrayEnd = &cus.cuArray[cus.cuCount];
5300 for (Info* info=arrayStart; info < arrayEnd; ++info) {
5301 // find function atom from address
5302 info->function = parser.findAtomByAddress(info->functionStartAddress);
5303 // find lsda atom from address
5304 if ( info->lsdaAddress != 0 ) {
5305 info->lsda = parser.findAtomByAddress(info->lsdaAddress);
5306 // add lsda subordinate
5307 typename Parser<A>::SourceLocation src(info->function, info->functionStartAddress - info->function->objectAddress());
5308 parser.addFixup(src, ld::Fixup::k1of1, ld::Fixup::kindNoneGroupSubordinateLSDA, info->lsda);
5309 }
5310 if ( info->personality != NULL ) {
5311 // add personality subordinate
5312 typename Parser<A>::SourceLocation src(info->function, info->functionStartAddress - info->function->objectAddress());
5313 parser.addFixup(src, ld::Fixup::k1of1, ld::Fixup::kindNoneGroupSubordinatePersonality, false, info->personality);
5314 }
5315 }
5316
5317 }
5318
5319 template <typename A>
5320 SymboledSection<A>::SymboledSection(Parser<A>& parser, File<A>& f, const macho_section<typename A::P>* s)
5321 : Section<A>(f, s), _type(ld::Atom::typeUnclassified)
5322 {
5323 switch ( s->flags() & SECTION_TYPE ) {
5324 case S_ZEROFILL:
5325 _type = ld::Atom::typeZeroFill;
5326 break;
5327 case S_MOD_INIT_FUNC_POINTERS:
5328 _type = ld::Atom::typeInitializerPointers;
5329 break;
5330 case S_MOD_TERM_FUNC_POINTERS:
5331 _type = ld::Atom::typeTerminatorPointers;
5332 break;
5333 case S_THREAD_LOCAL_VARIABLES:
5334 _type = ld::Atom::typeTLV;
5335 break;
5336 case S_THREAD_LOCAL_ZEROFILL:
5337 _type = ld::Atom::typeTLVZeroFill;
5338 break;
5339 case S_THREAD_LOCAL_REGULAR:
5340 _type = ld::Atom::typeTLVInitialValue;
5341 break;
5342 case S_THREAD_LOCAL_INIT_FUNCTION_POINTERS:
5343 _type = ld::Atom::typeTLVInitializerPointers;
5344 break;
5345 case S_REGULAR:
5346 if ( strncmp(s->sectname(), "__gcc_except_tab", 16) == 0 )
5347 _type = ld::Atom::typeLSDA;
5348 else if ( this->type() == ld::Section::typeInitializerPointers )
5349 _type = ld::Atom::typeInitializerPointers;
5350 // <rdar://problem/34716321> don't warn about static initializers in dylibs built for profiling
5351 if ( strncmp(s->sectname(), "__llvm_prf_", 11) == 0 )
5352 this->_file.setHasllvmProfiling();
5353 break;
5354 }
5355 }
5356
5357
5358 template <typename A>
5359 bool SymboledSection<A>::dontDeadStrip()
5360 {
5361 switch ( _type ) {
5362 case ld::Atom::typeInitializerPointers:
5363 case ld::Atom::typeTerminatorPointers:
5364 return true;
5365 default:
5366 // model an object file without MH_SUBSECTIONS_VIA_SYMBOLS as one in which nothing can be dead stripped
5367 if ( ! this->_file.canScatterAtoms() )
5368 return true;
5369 // call inherited
5370 return Section<A>::dontDeadStrip();
5371 }
5372 return false;
5373 }
5374
5375
5376 template <typename A>
5377 uint32_t SymboledSection<A>::computeAtomCount(class Parser<A>& parser,
5378 struct Parser<A>::LabelAndCFIBreakIterator& it,
5379 const struct Parser<A>::CFI_CU_InfoArrays&)
5380 {
5381 const pint_t startAddr = this->_machOSection->addr();
5382 const pint_t endAddr = startAddr + this->_machOSection->size();
5383 const uint32_t sectNum = this->sectionNum(parser);
5384
5385 uint32_t count = 0;
5386 pint_t addr;
5387 pint_t size;
5388 const macho_nlist<P>* sym;
5389 while ( it.next(parser, *this, sectNum, startAddr, endAddr, &addr, &size, &sym) ) {
5390 ++count;
5391 }
5392 //fprintf(stderr, "computeAtomCount(%s,%s) => %d\n", this->segmentName(), this->sectionName(), count);
5393 return count;
5394 }
5395
5396 template <typename A>
5397 uint32_t SymboledSection<A>::appendAtoms(class Parser<A>& parser, uint8_t* p,
5398 struct Parser<A>::LabelAndCFIBreakIterator& it,
5399 const struct Parser<A>::CFI_CU_InfoArrays&)
5400 {
5401 this->_beginAtoms = (Atom<A>*)p;
5402
5403 //fprintf(stderr, "SymboledSection::appendAtoms() in section %s\n", this->_machOSection->sectname());
5404 const pint_t startAddr = this->_machOSection->addr();
5405 const pint_t endAddr = startAddr + this->_machOSection->size();
5406 const uint32_t sectNum = this->sectionNum(parser);
5407
5408 uint32_t count = 0;
5409 pint_t addr;
5410 pint_t size;
5411 const macho_nlist<P>* label;
5412 while ( it.next(parser, *this, sectNum, startAddr, endAddr, &addr, &size, &label) ) {
5413 Atom<A>* allocatedSpace = (Atom<A>*)p;
5414 // is break because of label or CFI?
5415 if ( label != NULL ) {
5416 // The size is computed based on the address of the next label (or the end of the section for the last label)
5417 // If there are two labels at the same address, we want them one to be an alias of the other.
5418 // If the label is at the end of a section, it is has zero size, but is not an alias
5419 const bool isAlias = ( (size == 0) && (addr < endAddr) );
5420 new (allocatedSpace) Atom<A>(*this, parser, *label, size, isAlias);
5421 if ( isAlias )
5422 this->_hasAliases = true;
5423 if ( parser.altEntryFromSymbol(*label) )
5424 this->_altEntries.insert(allocatedSpace);
5425 }
5426 else {
5427 ld::Atom::SymbolTableInclusion inclusion = ld::Atom::symbolTableNotIn;
5428 ld::Atom::ContentType ctype = this->contentType();
5429 if ( ctype == ld::Atom::typeLSDA )
5430 inclusion = ld::Atom::symbolTableInWithRandomAutoStripLabel;
5431 new (allocatedSpace) Atom<A>(*this, "anon", addr, size, ld::Atom::definitionRegular, ld::Atom::combineNever,
5432 ld::Atom::scopeTranslationUnit, ctype, inclusion,
5433 this->dontDeadStrip(), false, false, this->alignmentForAddress(addr));
5434 }
5435 p += sizeof(Atom<A>);
5436 ++count;
5437 }
5438
5439 this->_endAtoms = (Atom<A>*)p;
5440 return count;
5441 }
5442
5443
5444 template <>
5445 ld::Atom::SymbolTableInclusion ImplicitSizeSection<arm64>::symbolTableInclusion()
5446 {
5447 return ld::Atom::symbolTableInWithRandomAutoStripLabel;
5448 }
5449
5450
5451 template <typename A>
5452 ld::Atom::SymbolTableInclusion ImplicitSizeSection<A>::symbolTableInclusion()
5453 {
5454 return ld::Atom::symbolTableNotIn;
5455 }
5456
5457
5458 template <typename A>
5459 uint32_t ImplicitSizeSection<A>::computeAtomCount(class Parser<A>& parser,
5460 struct Parser<A>::LabelAndCFIBreakIterator& it,
5461 const struct Parser<A>::CFI_CU_InfoArrays&)
5462 {
5463 uint32_t count = 0;
5464 const macho_section<P>* sect = this->machoSection();
5465 const pint_t startAddr = sect->addr();
5466 const pint_t endAddr = startAddr + sect->size();
5467 for (pint_t addr = startAddr; addr < endAddr; addr += elementSizeAtAddress(addr) ) {
5468 if ( useElementAt(parser, it, addr) )
5469 ++count;
5470 }
5471 if ( it.fileHasOverlappingSymbols && (sect->size() != 0) && (this->combine(parser, startAddr) == ld::Atom::combineByNameAndContent) ) {
5472 // if there are multiple labels in this section for the same address, then clone them into multi atoms
5473 pint_t prevSymbolAddr = (pint_t)(-1);
5474 uint8_t prevSymbolSectNum = 0;
5475 bool prevIgnore = false;
5476 for(uint32_t i=0; i < it.sortedSymbolCount; ++i) {
5477 const macho_nlist<P>& sym = parser.symbolFromIndex(it.sortedSymbolIndexes[i]);
5478 const pint_t symbolAddr = sym.n_value();
5479 const uint8_t symbolSectNum = sym.n_sect();
5480 const bool ignore = this->ignoreLabel(parser.nameFromSymbol(sym));
5481 if ( !ignore && !prevIgnore && (symbolAddr == prevSymbolAddr) && (prevSymbolSectNum == symbolSectNum) && (symbolSectNum == this->sectionNum(parser)) ) {
5482 ++count;
5483 }
5484 prevSymbolAddr = symbolAddr;
5485 prevSymbolSectNum = symbolSectNum;
5486 prevIgnore = ignore;
5487 }
5488 }
5489 return count;
5490 }
5491
5492 template <typename A>
5493 uint32_t ImplicitSizeSection<A>::appendAtoms(class Parser<A>& parser, uint8_t* p,
5494 struct Parser<A>::LabelAndCFIBreakIterator& it,
5495 const struct Parser<A>::CFI_CU_InfoArrays&)
5496 {
5497 this->_beginAtoms = (Atom<A>*)p;
5498
5499 const macho_section<P>* sect = this->machoSection();
5500 const pint_t startAddr = sect->addr();
5501 const pint_t endAddr = startAddr + sect->size();
5502 const uint32_t sectNum = this->sectionNum(parser);
5503 //fprintf(stderr, "ImplicitSizeSection::appendAtoms() in section %s\n", sect->sectname());
5504 uint32_t count = 0;
5505 pint_t foundAddr;
5506 pint_t size;
5507 const macho_nlist<P>* foundLabel;
5508 Atom<A>* allocatedSpace;
5509 while ( it.next(parser, *this, sectNum, startAddr, endAddr, &foundAddr, &size, &foundLabel) ) {
5510 if ( foundLabel != NULL ) {
5511 bool skip = false;
5512 pint_t labeledAtomSize = this->elementSizeAtAddress(foundAddr);
5513 allocatedSpace = (Atom<A>*)p;
5514 if ( this->ignoreLabel(parser.nameFromSymbol(*foundLabel)) ) {
5515 if ( size == 0 ) {
5516 // <rdar://problem/10018737>
5517 // a size of zero means there is another label at same location
5518 // and we are supposed to ignore this label
5519 skip = true;
5520 }
5521 else {
5522 //fprintf(stderr, " 0x%08llX make annon, size=%lld\n", (uint64_t)foundAddr, (uint64_t)size);
5523 new (allocatedSpace) Atom<A>(*this, this->unlabeledAtomName(parser, foundAddr), foundAddr,
5524 this->elementSizeAtAddress(foundAddr), this->definition(),
5525 this->combine(parser, foundAddr), this->scopeAtAddress(parser, foundAddr),
5526 this->contentType(), this->symbolTableInclusion(),
5527 this->dontDeadStrip(), false, false, this->alignmentForAddress(foundAddr));
5528 }
5529 }
5530 else {
5531 // make named atom for label
5532 //fprintf(stderr, " 0x%08llX make labeled: %s\n", (uint64_t)foundAddr, parser.nameFromSymbol(*foundLabel));
5533 new (allocatedSpace) Atom<A>(*this, parser, *foundLabel, labeledAtomSize);
5534 }
5535 if ( !skip ) {
5536 ++count;
5537 p += sizeof(Atom<A>);
5538 foundAddr += labeledAtomSize;
5539 size -= labeledAtomSize;
5540 }
5541 }
5542 // some number of anonymous atoms
5543 for (pint_t addr = foundAddr; addr < (foundAddr+size); addr += elementSizeAtAddress(addr) ) {
5544 // make anon atoms for area before label
5545 if ( this->useElementAt(parser, it, addr) ) {
5546 //fprintf(stderr, " 0x%08llX make annon, size=%lld\n", (uint64_t)addr, (uint64_t)elementSizeAtAddress(addr));
5547 allocatedSpace = (Atom<A>*)p;
5548 new (allocatedSpace) Atom<A>(*this, this->unlabeledAtomName(parser, addr), addr, this->elementSizeAtAddress(addr),
5549 this->definition(), this->combine(parser, addr), this->scopeAtAddress(parser, addr),
5550 this->contentType(), this->symbolTableInclusion(),
5551 this->dontDeadStrip(), false, false, this->alignmentForAddress(addr));
5552 ++count;
5553 p += sizeof(Atom<A>);
5554 }
5555 }
5556 }
5557
5558 this->_endAtoms = (Atom<A>*)p;
5559
5560 return count;
5561 }
5562
5563 template <typename A>
5564 bool Literal4Section<A>::ignoreLabel(const char* label) const
5565 {
5566 return (label[0] == 'L') || (label[0] == 'l');
5567 }
5568
5569 template <typename A>
5570 unsigned long Literal4Section<A>::contentHash(const class Atom<A>* atom, const ld::IndirectBindingTable& ind) const
5571 {
5572 const uint32_t* literalContent = (uint32_t*)atom->contentPointer();
5573 return *literalContent;
5574 }
5575
5576 template <typename A>
5577 bool Literal4Section<A>::canCoalesceWith(const class Atom<A>* atom, const ld::Atom& rhs,
5578 const ld::IndirectBindingTable& ind) const
5579 {
5580 assert(this->type() == rhs.section().type());
5581 const uint32_t* literalContent = (uint32_t*)atom->contentPointer();
5582
5583 const Atom<A>* rhsAtom = dynamic_cast<const Atom<A>*>(&rhs);
5584 assert(rhsAtom != NULL);
5585 if ( rhsAtom != NULL ) {
5586 const uint32_t* rhsLiteralContent = (uint32_t*)rhsAtom->contentPointer();
5587 return (*literalContent == *rhsLiteralContent);
5588 }
5589 return false;
5590 }
5591
5592
5593 template <typename A>
5594 bool Literal8Section<A>::ignoreLabel(const char* label) const
5595 {
5596 return (label[0] == 'L') || (label[0] == 'l');
5597 }
5598
5599 template <typename A>
5600 unsigned long Literal8Section<A>::contentHash(const class Atom<A>* atom, const ld::IndirectBindingTable& ind) const
5601 {
5602 #if __LP64__
5603 const uint64_t* literalContent = (uint64_t*)atom->contentPointer();
5604 return *literalContent;
5605 #else
5606 unsigned long hash = 5381;
5607 const uint8_t* byteContent = atom->contentPointer();
5608 for (int i=0; i < 8; ++i) {
5609 hash = hash * 33 + byteContent[i];
5610 }
5611 return hash;
5612 #endif
5613 }
5614
5615 template <typename A>
5616 bool Literal8Section<A>::canCoalesceWith(const class Atom<A>* atom, const ld::Atom& rhs,
5617 const ld::IndirectBindingTable& ind) const
5618 {
5619 if ( rhs.section().type() != ld::Section::typeLiteral8 )
5620 return false;
5621 assert(this->type() == rhs.section().type());
5622 const uint64_t* literalContent = (uint64_t*)atom->contentPointer();
5623
5624 const Atom<A>* rhsAtom = dynamic_cast<const Atom<A>*>(&rhs);
5625 assert(rhsAtom != NULL);
5626 if ( rhsAtom != NULL ) {
5627 const uint64_t* rhsLiteralContent = (uint64_t*)rhsAtom->contentPointer();
5628 return (*literalContent == *rhsLiteralContent);
5629 }
5630 return false;
5631 }
5632
5633 template <typename A>
5634 bool Literal16Section<A>::ignoreLabel(const char* label) const
5635 {
5636 return (label[0] == 'L') || (label[0] == 'l');
5637 }
5638
5639 template <typename A>
5640 unsigned long Literal16Section<A>::contentHash(const class Atom<A>* atom, const ld::IndirectBindingTable& ind) const
5641 {
5642 unsigned long hash = 5381;
5643 const uint8_t* byteContent = atom->contentPointer();
5644 for (int i=0; i < 16; ++i) {
5645 hash = hash * 33 + byteContent[i];
5646 }
5647 return hash;
5648 }
5649
5650 template <typename A>
5651 bool Literal16Section<A>::canCoalesceWith(const class Atom<A>* atom, const ld::Atom& rhs,
5652 const ld::IndirectBindingTable& ind) const
5653 {
5654 if ( rhs.section().type() != ld::Section::typeLiteral16 )
5655 return false;
5656 assert(this->type() == rhs.section().type());
5657 const uint64_t* literalContent = (uint64_t*)atom->contentPointer();
5658
5659 const Atom<A>* rhsAtom = dynamic_cast<const Atom<A>*>(&rhs);
5660 assert(rhsAtom != NULL);
5661 if ( rhsAtom != NULL ) {
5662 const uint64_t* rhsLiteralContent = (uint64_t*)rhsAtom->contentPointer();
5663 return ((literalContent[0] == rhsLiteralContent[0]) && (literalContent[1] == rhsLiteralContent[1]));
5664 }
5665 return false;
5666 }
5667
5668
5669
5670 template <typename A>
5671 typename A::P::uint_t CStringSection<A>::elementSizeAtAddress(pint_t addr)
5672 {
5673 const macho_section<P>* sect = this->machoSection();
5674 const char* stringContent = (char*)(this->file().fileContent() + sect->offset() + addr - sect->addr());
5675 return strlen(stringContent) + 1;
5676 }
5677
5678 template <typename A>
5679 bool CStringSection<A>::useElementAt(Parser<A>& parser, struct Parser<A>::LabelAndCFIBreakIterator& it, pint_t addr)
5680 {
5681 return true;
5682 }
5683
5684 template <typename A>
5685 bool CStringSection<A>::ignoreLabel(const char* label) const
5686 {
5687 return (label[0] == 'L') || (label[0] == 'l');
5688 }
5689
5690
5691 template <typename A>
5692 Atom<A>* CStringSection<A>::findAtomByAddress(pint_t addr)
5693 {
5694 Atom<A>* result = this->findContentAtomByAddress(addr, this->_beginAtoms, this->_endAtoms);
5695 return result;
5696 }
5697
5698 template <typename A>
5699 unsigned long CStringSection<A>::contentHash(const class Atom<A>* atom, const ld::IndirectBindingTable& ind) const
5700 {
5701 unsigned long hash = 5381;
5702 const char* stringContent = (char*)atom->contentPointer();
5703 for (const char* s = stringContent; *s != '\0'; ++s) {
5704 hash = hash * 33 + *s;
5705 }
5706 return hash;
5707 }
5708
5709
5710 template <typename A>
5711 bool CStringSection<A>::canCoalesceWith(const class Atom<A>* atom, const ld::Atom& rhs,
5712 const ld::IndirectBindingTable& ind) const
5713 {
5714 if ( rhs.section().type() != ld::Section::typeCString )
5715 return false;
5716 assert(this->type() == rhs.section().type());
5717 assert(strcmp(this->sectionName(), rhs.section().sectionName())== 0);
5718 assert(strcmp(this->segmentName(), rhs.section().segmentName())== 0);
5719 const char* stringContent = (char*)atom->contentPointer();
5720
5721 const Atom<A>* rhsAtom = dynamic_cast<const Atom<A>*>(&rhs);
5722 assert(rhsAtom != NULL);
5723 if ( rhsAtom != NULL ) {
5724 if ( atom->_size != rhsAtom->_size )
5725 return false;
5726 const char* rhsStringContent = (char*)rhsAtom->contentPointer();
5727 return (strcmp(stringContent, rhsStringContent) == 0);
5728 }
5729 return false;
5730 }
5731
5732
5733 template <>
5734 ld::Fixup::Kind NonLazyPointerSection<x86>::fixupKind()
5735 {
5736 return ld::Fixup::kindStoreLittleEndian32;
5737 }
5738
5739 template <>
5740 ld::Fixup::Kind NonLazyPointerSection<arm>::fixupKind()
5741 {
5742 return ld::Fixup::kindStoreLittleEndian32;
5743 }
5744
5745 template <>
5746 ld::Fixup::Kind NonLazyPointerSection<arm64>::fixupKind()
5747 {
5748 return ld::Fixup::kindStoreLittleEndian64;
5749 }
5750
5751
5752 template <>
5753 void NonLazyPointerSection<x86_64>::makeFixups(class Parser<x86_64>& parser, const struct Parser<x86_64>::CFI_CU_InfoArrays&)
5754 {
5755 assert(0 && "x86_64 should not have non-lazy-pointer sections in .o files");
5756 }
5757
5758 template <typename A>
5759 void NonLazyPointerSection<A>::makeFixups(class Parser<A>& parser, const struct Parser<A>::CFI_CU_InfoArrays&)
5760 {
5761 // add references for each NLP atom based on indirect symbol table
5762 const macho_section<P>* sect = this->machoSection();
5763 const pint_t endAddr = sect->addr() + sect->size();
5764 for( pint_t addr = sect->addr(); addr < endAddr; addr += sizeof(pint_t)) {
5765 typename Parser<A>::SourceLocation src;
5766 typename Parser<A>::TargetDesc target;
5767 src.atom = this->findAtomByAddress(addr);
5768 src.offsetInAtom = 0;
5769 uint32_t symIndex = parser.symbolIndexFromIndirectSectionAddress(addr, sect);
5770 target.atom = NULL;
5771 target.name = NULL;
5772 target.weakImport = false;
5773 target.addend = 0;
5774 if ( symIndex == INDIRECT_SYMBOL_LOCAL ) {
5775 // use direct reference for local symbols
5776 const pint_t* nlpContent = (pint_t*)(this->file().fileContent() + sect->offset() + addr - sect->addr());
5777 pint_t targetAddr = P::getP(*nlpContent);
5778 target.atom = parser.findAtomByAddress(targetAddr);
5779 target.weakImport = false;
5780 target.addend = (targetAddr - target.atom->objectAddress());
5781 // <rdar://problem/8385011> if pointer to thumb function, mask of thumb bit (not an addend of +1)
5782 if ( target.atom->isThumb() )
5783 target.addend &= (-2);
5784 assert(src.atom->combine() == ld::Atom::combineNever);
5785 }
5786 else {
5787 const macho_nlist<P>& sym = parser.symbolFromIndex(symIndex);
5788 // use direct reference for local symbols
5789 if ( ((sym.n_type() & N_TYPE) == N_SECT) && ((sym.n_type() & N_EXT) == 0) ) {
5790 parser.findTargetFromAddressAndSectionNum(sym.n_value(), sym.n_sect(), target);
5791 assert(src.atom->combine() == ld::Atom::combineNever);
5792 }
5793 else {
5794 target.name = parser.nameFromSymbol(sym);
5795 target.weakImport = parser.weakImportFromSymbol(sym);
5796 assert(src.atom->combine() == ld::Atom::combineByNameAndReferences);
5797 }
5798 }
5799 parser.addFixups(src, this->fixupKind(), target);
5800 }
5801 }
5802
5803 template <typename A>
5804 ld::Atom::Combine NonLazyPointerSection<A>::combine(Parser<A>& parser, pint_t addr)
5805 {
5806 const macho_section<P>* sect = this->machoSection();
5807 uint32_t symIndex = parser.symbolIndexFromIndirectSectionAddress(addr, sect);
5808 if ( symIndex == INDIRECT_SYMBOL_LOCAL)
5809 return ld::Atom::combineNever;
5810
5811 // don't coalesce non-lazy-pointers to local symbols
5812 const macho_nlist<P>& sym = parser.symbolFromIndex(symIndex);
5813 if ( ((sym.n_type() & N_TYPE) == N_SECT) && ((sym.n_type() & N_EXT) == 0) )
5814 return ld::Atom::combineNever;
5815
5816 return ld::Atom::combineByNameAndReferences;
5817 }
5818
5819 template <typename A>
5820 const char* NonLazyPointerSection<A>::targetName(const class Atom<A>* atom, const ld::IndirectBindingTable& ind)
5821 {
5822 assert(atom->combine() == ld::Atom::combineByNameAndReferences);
5823 assert(atom->fixupCount() == 1);
5824 ld::Fixup::iterator fit = atom->fixupsBegin();
5825 const char* name = NULL;
5826 switch ( fit->binding ) {
5827 case ld::Fixup::bindingByNameUnbound:
5828 name = fit->u.name;
5829 break;
5830 case ld::Fixup::bindingByContentBound:
5831 name = fit->u.target->name();
5832 break;
5833 case ld::Fixup::bindingsIndirectlyBound:
5834 name = ind.indirectName(fit->u.bindingIndex);
5835 break;
5836 default:
5837 assert(0);
5838 }
5839 assert(name != NULL);
5840 return name;
5841 }
5842
5843 template <typename A>
5844 unsigned long NonLazyPointerSection<A>::contentHash(const class Atom<A>* atom, const ld::IndirectBindingTable& ind) const
5845 {
5846 assert(atom->combine() == ld::Atom::combineByNameAndReferences);
5847 unsigned long hash = 9508;
5848 for (const char* s = this->targetName(atom, ind); *s != '\0'; ++s) {
5849 hash = hash * 33 + *s;
5850 }
5851 return hash;
5852 }
5853
5854 template <typename A>
5855 bool NonLazyPointerSection<A>::canCoalesceWith(const class Atom<A>* atom, const ld::Atom& rhs,
5856 const ld::IndirectBindingTable& indirectBindingTable) const
5857 {
5858 if ( rhs.section().type() != ld::Section::typeNonLazyPointer )
5859 return false;
5860 assert(this->type() == rhs.section().type());
5861 // there can be many non-lazy pointer in different section names
5862 // we only want to coalesce in same section name
5863 if ( *this != rhs.section() )
5864 return false;
5865 const Atom<A>* rhsAtom = dynamic_cast<const Atom<A>*>(&rhs);
5866 assert(rhsAtom != NULL);
5867 const char* thisName = this->targetName(atom, indirectBindingTable);
5868 const char* rhsName = this->targetName(rhsAtom, indirectBindingTable);
5869 return (strcmp(thisName, rhsName) == 0);
5870 }
5871
5872 template <typename A>
5873 ld::Atom::Scope NonLazyPointerSection<A>::scopeAtAddress(Parser<A>& parser, pint_t addr)
5874 {
5875 const macho_section<P>* sect = this->machoSection();
5876 uint32_t symIndex = parser.symbolIndexFromIndirectSectionAddress(addr, sect);
5877 if ( symIndex == INDIRECT_SYMBOL_LOCAL)
5878 return ld::Atom::scopeTranslationUnit;
5879 else
5880 return ld::Atom::scopeLinkageUnit;
5881 }
5882
5883
5884
5885 template <typename A>
5886 ld::Atom::Combine TLVPointerSection<A>::combine(Parser<A>& parser, pint_t addr)
5887 {
5888 return ld::Atom::combineByNameAndReferences;
5889 }
5890
5891 template <>
5892 void TLVPointerSection<arm>::makeFixups(class Parser<arm>& parser, const struct Parser<arm>::CFI_CU_InfoArrays&)
5893 {
5894 // add references for each thread local pointer atom based on indirect symbol table
5895 const macho_section<P>* sect = this->machoSection();
5896 const pint_t endAddr = sect->addr() + sect->size();
5897 for (pint_t addr = sect->addr(); addr < endAddr; addr += sizeof(pint_t)) {
5898 typename Parser<arm>::SourceLocation src;
5899 typename Parser<arm>::TargetDesc target;
5900 src.atom = this->findAtomByAddress(addr);
5901 src.offsetInAtom = 0;
5902 uint32_t symIndex = parser.symbolIndexFromIndirectSectionAddress(addr, sect);
5903 target.atom = NULL;
5904 target.name = NULL;
5905 target.weakImport = false;
5906 target.addend = 0;
5907 if ( symIndex == INDIRECT_SYMBOL_LOCAL ) {
5908 throwf("unexpected INDIRECT_SYMBOL_LOCAL in section %s", this->sectionName());
5909 }
5910 else {
5911 const macho_nlist<P>& sym = parser.symbolFromIndex(symIndex);
5912 // use direct reference for local symbols
5913 if ( ((sym.n_type() & N_TYPE) == N_SECT) && ((sym.n_type() & N_EXT) == 0) ) {
5914 throwf("unexpected pointer to local symbol in section %s", this->sectionName());
5915 }
5916 else {
5917 target.name = parser.nameFromSymbol(sym);
5918 target.weakImport = parser.weakImportFromSymbol(sym);
5919 assert(src.atom->combine() == ld::Atom::combineByNameAndReferences);
5920 }
5921 }
5922 parser.addFixups(src, ld::Fixup::kindStoreLittleEndian32, target);
5923 }
5924 }
5925
5926 template <typename A>
5927 void TLVPointerSection<A>::makeFixups(class Parser<A>& parser, const struct Parser<A>::CFI_CU_InfoArrays&)
5928 {
5929 assert(0 && "should not have thread-local-pointer sections in .o files");
5930 }
5931
5932
5933 template <typename A>
5934 const char* TLVPointerSection<A>::targetName(const class Atom<A>* atom, const ld::IndirectBindingTable& ind, bool* isStatic)
5935 {
5936 assert(atom->combine() == ld::Atom::combineByNameAndReferences);
5937 assert(atom->fixupCount() == 1);
5938 *isStatic = false;
5939 ld::Fixup::iterator fit = atom->fixupsBegin();
5940 const char* name = NULL;
5941 switch ( fit->binding ) {
5942 case ld::Fixup::bindingByNameUnbound:
5943 name = fit->u.name;
5944 break;
5945 case ld::Fixup::bindingByContentBound:
5946 name = fit->u.target->name();
5947 break;
5948 case ld::Fixup::bindingsIndirectlyBound:
5949 name = ind.indirectName(fit->u.bindingIndex);
5950 break;
5951 case ld::Fixup::bindingDirectlyBound:
5952 name = fit->u.target->name();
5953 *isStatic = (fit->u.target->scope() == ld::Atom::scopeTranslationUnit);
5954 break;
5955 default:
5956 assert(0);
5957 }
5958 assert(name != NULL);
5959 return name;
5960 }
5961
5962 template <typename A>
5963 unsigned long TLVPointerSection<A>::contentHash(const class Atom<A>* atom, const ld::IndirectBindingTable& ind) const
5964 {
5965 assert(atom->combine() == ld::Atom::combineByNameAndReferences);
5966 unsigned long hash = 9508;
5967 bool isStatic;
5968 for (const char* s = this->targetName(atom, ind, &isStatic); *s != '\0'; ++s) {
5969 hash = hash * 33 + *s;
5970 }
5971 return hash;
5972 }
5973
5974 template <typename A>
5975 bool TLVPointerSection<A>::canCoalesceWith(const class Atom<A>* atom, const ld::Atom& rhs,
5976 const ld::IndirectBindingTable& indirectBindingTable) const
5977 {
5978 if ( rhs.section().type() != ld::Section::typeTLVPointers )
5979 return false;
5980 assert(this->type() == rhs.section().type());
5981 const Atom<A>* rhsAtom = dynamic_cast<const Atom<A>*>(&rhs);
5982 assert(rhsAtom != NULL);
5983 bool thisIsStatic;
5984 bool rhsIsStatic;
5985 const char* thisName = this->targetName(atom, indirectBindingTable, &thisIsStatic);
5986 const char* rhsName = this->targetName(rhsAtom, indirectBindingTable, &rhsIsStatic);
5987 return !thisIsStatic && !rhsIsStatic && (strcmp(thisName, rhsName) == 0);
5988 }
5989
5990
5991 template <typename A>
5992 const uint8_t* CFStringSection<A>::targetContent(const class Atom<A>* atom, const ld::IndirectBindingTable& ind,
5993 ContentType* ct, unsigned int* count)
5994 {
5995 *ct = contentUnknown;
5996 for (ld::Fixup::iterator fit=atom->fixupsBegin(), end=atom->fixupsEnd(); fit != end; ++fit) {
5997 const ld::Atom* targetAtom = NULL;
5998 switch ( fit->binding ) {
5999 case ld::Fixup::bindingByNameUnbound:
6000 // ignore reference to ___CFConstantStringClassReference
6001 // we are just looking for reference to backing string data
6002 assert(fit->offsetInAtom == 0);
6003 assert(strcmp(fit->u.name, "___CFConstantStringClassReference") == 0);
6004 break;
6005 case ld::Fixup::bindingDirectlyBound:
6006 case ld::Fixup::bindingByContentBound:
6007 targetAtom = fit->u.target;
6008 break;
6009 case ld::Fixup::bindingsIndirectlyBound:
6010 targetAtom = ind.indirectAtom(fit->u.bindingIndex);
6011 break;
6012 default:
6013 assert(0 && "bad binding type");
6014 }
6015 assert(targetAtom != NULL);
6016 const Atom<A>* target = dynamic_cast<const Atom<A>*>(targetAtom);
6017 if ( targetAtom->section().type() == ld::Section::typeCString ) {
6018 *ct = contentUTF8;
6019 *count = targetAtom->size();
6020 }
6021 else if ( targetAtom->section().type() == ld::Section::typeUTF16Strings ) {
6022 *ct = contentUTF16;
6023 *count = (targetAtom->size()+1)/2; // round up incase of buggy compiler that has only one trailing zero byte
6024 }
6025 else {
6026 *ct = contentUnknown;
6027 *count = 0;
6028 return NULL;
6029 }
6030 return target->contentPointer();
6031 }
6032 assert(0);
6033 return NULL;
6034 }
6035
6036 template <typename A>
6037 unsigned long CFStringSection<A>::contentHash(const class Atom<A>* atom, const ld::IndirectBindingTable& ind) const
6038 {
6039 // base hash of CFString on hash of cstring it wraps
6040 ContentType cType;
6041 unsigned long hash;
6042 unsigned int charCount;
6043 const uint8_t* content = this->targetContent(atom, ind, &cType, &charCount);
6044 switch ( cType ) {
6045 case contentUTF8:
6046 hash = 9408;
6047 for (const char* s = (char*)content; *s != '\0'; ++s) {
6048 hash = hash * 33 + *s;
6049 }
6050 return hash;
6051 case contentUTF16:
6052 hash = 407955;
6053 --charCount; // don't add last 0x0000 to hash because some buggy compilers only have trailing single byte
6054 for (const uint16_t* s = (uint16_t*)content; charCount > 0; ++s, --charCount) {
6055 hash = hash * 1025 + *s;
6056 }
6057 return hash;
6058 case contentUnknown:
6059 // <rdar://problem/14134211> For malformed CFStrings, hash to address of atom so they have unique hashes
6060 return ULONG_MAX - (unsigned long)(atom);
6061 }
6062 return 0;
6063 }
6064
6065
6066 template <typename A>
6067 bool CFStringSection<A>::canCoalesceWith(const class Atom<A>* atom, const ld::Atom& rhs,
6068 const ld::IndirectBindingTable& indirectBindingTable) const
6069 {
6070 if ( atom == &rhs )
6071 return true;
6072 if ( rhs.section().type() != ld::Section::typeCFString)
6073 return false;
6074 assert(this->type() == rhs.section().type());
6075 assert(strcmp(this->sectionName(), "__cfstring") == 0);
6076
6077 ContentType thisType;
6078 unsigned int charCount;
6079 const uint8_t* cstringContent = this->targetContent(atom, indirectBindingTable, &thisType, &charCount);
6080 ContentType rhsType;
6081 const Atom<A>* rhsAtom = dynamic_cast<const Atom<A>*>(&rhs);
6082 assert(rhsAtom != NULL);
6083 unsigned int rhsCharCount;
6084 const uint8_t* rhsStringContent = this->targetContent(rhsAtom, indirectBindingTable, &rhsType, &rhsCharCount);
6085
6086 if ( thisType != rhsType )
6087 return false;
6088
6089 if ( thisType == contentUnknown )
6090 return false;
6091
6092 if ( rhsType == contentUnknown )
6093 return false;
6094
6095 // no need to compare content of pointers are already the same
6096 if ( cstringContent == rhsStringContent )
6097 return true;
6098
6099 // no need to compare content if size is different
6100 if ( charCount != rhsCharCount )
6101 return false;
6102
6103 switch ( thisType ) {
6104 case contentUTF8:
6105 return (strcmp((char*)cstringContent, (char*)rhsStringContent) == 0);
6106 case contentUTF16:
6107 {
6108 const uint16_t* cstringContent16 = (uint16_t*)cstringContent;
6109 const uint16_t* rhsStringContent16 = (uint16_t*)rhsStringContent;
6110 for (unsigned int i = 0; i < charCount; ++i) {
6111 if ( cstringContent16[i] != rhsStringContent16[i] )
6112 return false;
6113 }
6114 return true;
6115 }
6116 case contentUnknown:
6117 return false;
6118 }
6119 return false;
6120 }
6121
6122
6123 template <typename A>
6124 typename A::P::uint_t ObjC1ClassSection<A>::elementSizeAtAddress(pint_t addr)
6125 {
6126 // nominal size for each class is 48 bytes, but sometimes the compiler
6127 // over aligns and there is padding after class data
6128 const macho_section<P>* sct = this->machoSection();
6129 uint32_t align = 1 << sct->align();
6130 uint32_t size = ((12 * sizeof(pint_t)) + align-1) & (-align);
6131 return size;
6132 }
6133
6134 template <typename A>
6135 const char* ObjC1ClassSection<A>::unlabeledAtomName(Parser<A>& parser, pint_t addr)
6136 {
6137 // 8-bytes into class object is pointer to class name
6138 const macho_section<P>* sct = this->machoSection();
6139 uint32_t classObjcFileOffset = sct->offset() - sct->addr() + addr;
6140 const uint8_t* mappedFileContent = this->file().fileContent();
6141 pint_t nameAddr = P::getP(*((pint_t*)(mappedFileContent+classObjcFileOffset+2*sizeof(pint_t))));
6142
6143 // find section containing string address to get string bytes
6144 const macho_section<P>* const sections = parser.firstMachOSection();
6145 const uint32_t sectionCount = parser.machOSectionCount();
6146 for (uint32_t i=0; i < sectionCount; ++i) {
6147 const macho_section<P>* aSect = &sections[i];
6148 if ( (aSect->addr() <= nameAddr) && (nameAddr < (aSect->addr()+aSect->size())) ) {
6149 assert((aSect->flags() & SECTION_TYPE) == S_CSTRING_LITERALS);
6150 uint32_t nameFileOffset = aSect->offset() - aSect->addr() + nameAddr;
6151 const char* name = (char*)mappedFileContent + nameFileOffset;
6152 // spin through symbol table to find absolute symbol corresponding to this class
6153 for (uint32_t s=0; s < parser.symbolCount(); ++s) {
6154 const macho_nlist<P>& sym = parser.symbolFromIndex(s);
6155 if ( (sym.n_type() & N_TYPE) != N_ABS )
6156 continue;
6157 const char* absName = parser.nameFromSymbol(sym);
6158 if ( strncmp(absName, ".objc_class_name_", 17) == 0 ) {
6159 if ( strcmp(&absName[17], name) == 0 )
6160 return absName;
6161 }
6162 }
6163 assert(0 && "obj class name not found in symbol table");
6164 }
6165 }
6166 assert(0 && "obj class name not found");
6167 return "unknown objc class";
6168 }
6169
6170
6171 template <typename A>
6172 const char* ObjC2ClassRefsSection<A>::targetClassName(const class Atom<A>* atom, const ld::IndirectBindingTable& ind) const
6173 {
6174 assert(atom->fixupCount() == 1);
6175 ld::Fixup::iterator fit = atom->fixupsBegin();
6176 const char* className = NULL;
6177 switch ( fit->binding ) {
6178 case ld::Fixup::bindingByNameUnbound:
6179 className = fit->u.name;
6180 break;
6181 case ld::Fixup::bindingDirectlyBound:
6182 case ld::Fixup::bindingByContentBound:
6183 className = fit->u.target->name();
6184 break;
6185 case ld::Fixup::bindingsIndirectlyBound:
6186 className = ind.indirectName(fit->u.bindingIndex);
6187 break;
6188 default:
6189 assert(0 && "unsupported binding in objc2 class ref section");
6190 }
6191 assert(className != NULL);
6192 return className;
6193 }
6194
6195
6196 template <typename A>
6197 unsigned long ObjC2ClassRefsSection<A>::contentHash(const class Atom<A>* atom, const ld::IndirectBindingTable& ind) const
6198 {
6199 unsigned long hash = 978;
6200 for (const char* s = targetClassName(atom, ind); *s != '\0'; ++s) {
6201 hash = hash * 33 + *s;
6202 }
6203 return hash;
6204 }
6205
6206 template <typename A>
6207 bool ObjC2ClassRefsSection<A>::canCoalesceWith(const class Atom<A>* atom, const ld::Atom& rhs,
6208 const ld::IndirectBindingTable& indirectBindingTable) const
6209 {
6210 assert(this->type() == rhs.section().type());
6211 const Atom<A>* rhsAtom = dynamic_cast<const Atom<A>*>(&rhs);
6212 assert(rhsAtom != NULL);
6213 const char* thisClassName = targetClassName(atom, indirectBindingTable);
6214 const char* rhsClassName = targetClassName(rhsAtom, indirectBindingTable);
6215 return (strcmp(thisClassName, rhsClassName) == 0);
6216 }
6217
6218
6219 template <typename A>
6220 const char* Objc1ClassReferences<A>::targetCString(const class Atom<A>* atom, const ld::IndirectBindingTable& ind) const
6221 {
6222 assert(atom->fixupCount() == 2);
6223 ld::Fixup::iterator fit = atom->fixupsBegin();
6224 if ( fit->kind == ld::Fixup::kindSetTargetAddress )
6225 ++fit;
6226 const ld::Atom* targetAtom = NULL;
6227 switch ( fit->binding ) {
6228 case ld::Fixup::bindingByContentBound:
6229 targetAtom = fit->u.target;
6230 break;
6231 case ld::Fixup::bindingsIndirectlyBound:
6232 targetAtom = ind.indirectAtom(fit->u.bindingIndex);
6233 if ( targetAtom == NULL ) {
6234 fprintf(stderr, "missing target named %s\n", ind.indirectName(fit->u.bindingIndex));
6235 }
6236 break;
6237 default:
6238 assert(0);
6239 }
6240 assert(targetAtom != NULL);
6241 const Atom<A>* target = dynamic_cast<const Atom<A>*>(targetAtom);
6242 assert(target != NULL);
6243 return (char*)target->contentPointer();
6244 }
6245
6246
6247 template <typename A>
6248 const char* PointerToCStringSection<A>::targetCString(const class Atom<A>* atom, const ld::IndirectBindingTable& ind) const
6249 {
6250 assert(atom->fixupCount() == 1);
6251 ld::Fixup::iterator fit = atom->fixupsBegin();
6252 const ld::Atom* targetAtom = NULL;
6253 switch ( fit->binding ) {
6254 case ld::Fixup::bindingByContentBound:
6255 targetAtom = fit->u.target;
6256 break;
6257 case ld::Fixup::bindingsIndirectlyBound:
6258 targetAtom = ind.indirectAtom(fit->u.bindingIndex);
6259 break;
6260 case ld::Fixup::bindingDirectlyBound:
6261 targetAtom = fit->u.target;
6262 break;
6263 default:
6264 assert(0 && "unsupported reference to selector");
6265 }
6266 assert(targetAtom != NULL);
6267 const Atom<A>* target = dynamic_cast<const Atom<A>*>(targetAtom);
6268 assert(target != NULL);
6269 assert(target->contentType() == ld::Atom::typeCString);
6270 return (char*)target->contentPointer();
6271 }
6272
6273 template <typename A>
6274 unsigned long PointerToCStringSection<A>::contentHash(const class Atom<A>* atom,
6275 const ld::IndirectBindingTable& indirectBindingTable) const
6276 {
6277 // make hash from section name and target cstring name
6278 unsigned long hash = 123;
6279 for (const char* s = this->sectionName(); *s != '\0'; ++s) {
6280 hash = hash * 33 + *s;
6281 }
6282 for (const char* s = this->targetCString(atom, indirectBindingTable); *s != '\0'; ++s) {
6283 hash = hash * 33 + *s;
6284 }
6285 return hash;
6286 }
6287
6288 template <typename A>
6289 bool PointerToCStringSection<A>::canCoalesceWith(const class Atom<A>* atom, const ld::Atom& rhs,
6290 const ld::IndirectBindingTable& indirectBindingTable) const
6291 {
6292 assert(this->type() == rhs.section().type());
6293 // there can be pointers-to-cstrings in different section names
6294 // we only want to coalesce in same section name
6295 if ( *this != rhs.section() )
6296 return false;
6297
6298 // get string content for this
6299 const char* cstringContent = this->targetCString(atom, indirectBindingTable);
6300 const Atom<A>* rhsAtom = dynamic_cast<const Atom<A>*>(&rhs);
6301 assert(rhsAtom != NULL);
6302 const char* rhsCstringContent = this->targetCString(rhsAtom, indirectBindingTable);
6303
6304 assert(cstringContent != NULL);
6305 assert(rhsCstringContent != NULL);
6306 return (strcmp(cstringContent, rhsCstringContent) == 0);
6307 }
6308
6309
6310
6311 template <typename A>
6312 unsigned long UTF16StringSection<A>::contentHash(const class Atom<A>* atom, const ld::IndirectBindingTable& ind) const
6313 {
6314 unsigned long hash = 5381;
6315 const uint16_t* stringContent = (uint16_t*)atom->contentPointer();
6316 // some buggy compilers end utf16 data with single byte, so don't use last word in hash computation
6317 unsigned int count = (atom->size()/2) - 1;
6318 for (const uint16_t* s = stringContent; count > 0; ++s, --count) {
6319 hash = hash * 33 + *s;
6320 }
6321 return hash;
6322 }
6323
6324 template <typename A>
6325 bool UTF16StringSection<A>::canCoalesceWith(const class Atom<A>* atom, const ld::Atom& rhs,
6326 const ld::IndirectBindingTable& ind) const
6327 {
6328 if ( rhs.section().type() != ld::Section::typeUTF16Strings )
6329 return false;
6330 assert(0);
6331 return false;
6332 }
6333
6334
6335
6336
6337
6338
6339
6340 template <>
6341 uint32_t Section<x86_64>::x86_64PcRelOffset(uint8_t r_type)
6342 {
6343 switch ( r_type ) {
6344 case X86_64_RELOC_SIGNED:
6345 return 4;
6346 case X86_64_RELOC_SIGNED_1:
6347 return 5;
6348 case X86_64_RELOC_SIGNED_2:
6349 return 6;
6350 case X86_64_RELOC_SIGNED_4:
6351 return 8;
6352 }
6353 return 0;
6354 }
6355
6356
6357 template <>
6358 bool Section<x86_64>::addRelocFixup(class Parser<x86_64>& parser, const macho_relocation_info<P>* reloc)
6359 {
6360 const macho_section<P>* sect = this->machoSection();
6361 if ( sect == NULL ) {
6362 warning("malformed mach-o, relocations not supported on section %s", this->sectionName());
6363 return false;
6364 }
6365 uint64_t srcAddr = sect->addr() + reloc->r_address();
6366 Parser<x86_64>::SourceLocation src;
6367 Parser<x86_64>::TargetDesc target;
6368 Parser<x86_64>::TargetDesc toTarget;
6369 src.atom = this->findAtomByAddress(srcAddr);
6370 if ( src.atom == NULL )
6371 throwf("malformed mach-o, reloc addr 0x%llX not in any atom", srcAddr);
6372 src.offsetInAtom = srcAddr - src.atom->_objAddress;
6373 const uint8_t* fixUpPtr = file().fileContent() + sect->offset() + reloc->r_address();
6374 uint64_t contentValue = 0;
6375 const macho_relocation_info<x86_64::P>* nextReloc = &reloc[1];
6376 bool result = false;
6377 bool useDirectBinding;
6378 switch ( reloc->r_length() ) {
6379 case 0:
6380 contentValue = *fixUpPtr;
6381 break;
6382 case 1:
6383 contentValue = (int64_t)(int16_t)E::get16(*((uint16_t*)fixUpPtr));
6384 break;
6385 case 2:
6386 contentValue = (int64_t)(int32_t)E::get32(*((uint32_t*)fixUpPtr));
6387 break;
6388 case 3:
6389 contentValue = E::get64(*((uint64_t*)fixUpPtr));
6390 break;
6391 }
6392 target.atom = NULL;
6393 target.name = NULL;
6394 target.weakImport = false;
6395 target.addend = 0;
6396 if ( reloc->r_extern() ) {
6397 const macho_nlist<P>& sym = parser.symbolFromIndex(reloc->r_symbolnum());
6398 // use direct reference for local symbols
6399 if ( ((sym.n_type() & N_TYPE) == N_SECT) && (((sym.n_type() & N_EXT) == 0) || (parser.nameFromSymbol(sym)[0] == 'L')) ) {
6400 parser.findTargetFromAddressAndSectionNum(sym.n_value(), sym.n_sect(), target);
6401 target.addend += contentValue;
6402 }
6403 else {
6404 target.name = parser.nameFromSymbol(sym);
6405 target.weakImport = parser.weakImportFromSymbol(sym);
6406 target.addend = contentValue;
6407 }
6408 // cfstrings should always use direct reference to backing store
6409 if ( (this->type() == ld::Section::typeCFString) && (src.offsetInAtom != 0) ) {
6410 parser.findTargetFromAddressAndSectionNum(sym.n_value(), sym.n_sect(), target);
6411 target.addend = contentValue;
6412 }
6413 }
6414 else {
6415 if ( reloc->r_pcrel() )
6416 contentValue += srcAddr + x86_64PcRelOffset(reloc->r_type());
6417 parser.findTargetFromAddressAndSectionNum(contentValue, reloc->r_symbolnum(), target);
6418 }
6419 switch ( reloc->r_type() ) {
6420 case X86_64_RELOC_UNSIGNED:
6421 if ( reloc->r_pcrel() )
6422 throw "pcrel and X86_64_RELOC_UNSIGNED not supported";
6423 switch ( reloc->r_length() ) {
6424 case 0:
6425 case 1:
6426 throw "length < 2 and X86_64_RELOC_UNSIGNED not supported";
6427 case 2:
6428 parser.addFixups(src, ld::Fixup::kindStoreLittleEndian32, target);
6429 break;
6430 case 3:
6431 parser.addFixups(src, ld::Fixup::kindStoreLittleEndian64, target);
6432 break;
6433 }
6434 break;
6435 case X86_64_RELOC_SIGNED:
6436 case X86_64_RELOC_SIGNED_1:
6437 case X86_64_RELOC_SIGNED_2:
6438 case X86_64_RELOC_SIGNED_4:
6439 if ( ! reloc->r_pcrel() )
6440 throw "not pcrel and X86_64_RELOC_SIGNED* not supported";
6441 if ( reloc->r_length() != 2 )
6442 throw "length != 2 and X86_64_RELOC_SIGNED* not supported";
6443 switch ( reloc->r_type() ) {
6444 case X86_64_RELOC_SIGNED:
6445 parser.addFixups(src, ld::Fixup::kindStoreX86PCRel32, target);
6446 break;
6447 case X86_64_RELOC_SIGNED_1:
6448 if ( reloc->r_extern() )
6449 target.addend += 1;
6450 parser.addFixups(src, ld::Fixup::kindStoreX86PCRel32_1, target);
6451 break;
6452 case X86_64_RELOC_SIGNED_2:
6453 if ( reloc->r_extern() )
6454 target.addend += 2;
6455 parser.addFixups(src, ld::Fixup::kindStoreX86PCRel32_2, target);
6456 break;
6457 case X86_64_RELOC_SIGNED_4:
6458 if ( reloc->r_extern() )
6459 target.addend += 4;
6460 parser.addFixups(src, ld::Fixup::kindStoreX86PCRel32_4, target);
6461 break;
6462 }
6463 break;
6464 case X86_64_RELOC_BRANCH:
6465 if ( ! reloc->r_pcrel() )
6466 throw "not pcrel and X86_64_RELOC_BRANCH not supported";
6467 switch ( reloc->r_length() ) {
6468 case 2:
6469 if ( (target.name != NULL) && (strncmp(target.name, "___dtrace_probe$", 16) == 0) ) {
6470 parser.addFixup(src, ld::Fixup::k1of1, ld::Fixup::kindStoreX86DtraceCallSiteNop, false, target.name);
6471 parser.addDtraceExtraInfos(src, &target.name[16]);
6472 }
6473 else if ( (target.name != NULL) && (strncmp(target.name, "___dtrace_isenabled$", 20) == 0) ) {
6474 parser.addFixup(src, ld::Fixup::k1of1, ld::Fixup::kindStoreX86DtraceIsEnableSiteClear, false, target.name);
6475 parser.addDtraceExtraInfos(src, &target.name[20]);
6476 }
6477 else {
6478 parser.addFixups(src, ld::Fixup::kindStoreX86BranchPCRel32, target);
6479 }
6480 break;
6481 case 0:
6482 parser.addFixups(src, ld::Fixup::kindStoreX86BranchPCRel8, target);
6483 break;
6484 default:
6485 throwf("length=%d and X86_64_RELOC_BRANCH not supported", reloc->r_length());
6486 }
6487 break;
6488 case X86_64_RELOC_GOT:
6489 if ( ! reloc->r_extern() )
6490 throw "not extern and X86_64_RELOC_GOT not supported";
6491 if ( ! reloc->r_pcrel() )
6492 throw "not pcrel and X86_64_RELOC_GOT not supported";
6493 if ( reloc->r_length() != 2 )
6494 throw "length != 2 and X86_64_RELOC_GOT not supported";
6495 parser.addFixups(src, ld::Fixup::kindStoreX86PCRel32GOT, target);
6496 break;
6497 case X86_64_RELOC_GOT_LOAD:
6498 if ( ! reloc->r_extern() )
6499 throw "not extern and X86_64_RELOC_GOT_LOAD not supported";
6500 if ( ! reloc->r_pcrel() )
6501 throw "not pcrel and X86_64_RELOC_GOT_LOAD not supported";
6502 if ( reloc->r_length() != 2 )
6503 throw "length != 2 and X86_64_RELOC_GOT_LOAD not supported";
6504 parser.addFixups(src, ld::Fixup::kindStoreX86PCRel32GOTLoad, target);
6505 break;
6506 case X86_64_RELOC_SUBTRACTOR:
6507 if ( reloc->r_pcrel() )
6508 throw "X86_64_RELOC_SUBTRACTOR cannot be pc-relative";
6509 if ( reloc->r_length() < 2 )
6510 throw "X86_64_RELOC_SUBTRACTOR must have r_length of 2 or 3";
6511 if ( !reloc->r_extern() )
6512 throw "X86_64_RELOC_SUBTRACTOR must have r_extern=1";
6513 if ( nextReloc->r_type() != X86_64_RELOC_UNSIGNED )
6514 throw "X86_64_RELOC_SUBTRACTOR must be followed by X86_64_RELOC_UNSIGNED";
6515 result = true;
6516 if ( nextReloc->r_pcrel() )
6517 throw "X86_64_RELOC_UNSIGNED following a X86_64_RELOC_SUBTRACTOR cannot be pc-relative";
6518 if ( nextReloc->r_length() != reloc->r_length() )
6519 throw "X86_64_RELOC_UNSIGNED following a X86_64_RELOC_SUBTRACTOR must have same r_length";
6520 if ( nextReloc->r_extern() ) {
6521 const macho_nlist<P>& sym = parser.symbolFromIndex(nextReloc->r_symbolnum());
6522 // use direct reference for local symbols
6523 if ( ((sym.n_type() & N_TYPE) == N_SECT) && (((sym.n_type() & N_EXT) == 0) || (parser.nameFromSymbol(sym)[0] == 'L')) ) {
6524 parser.findTargetFromAddressAndSectionNum(sym.n_value(), sym.n_sect(), toTarget);
6525 toTarget.addend = contentValue;
6526 useDirectBinding = true;
6527 }
6528 else {
6529 toTarget.name = parser.nameFromSymbol(sym);
6530 toTarget.weakImport = parser.weakImportFromSymbol(sym);
6531 toTarget.addend = contentValue;
6532 useDirectBinding = false;
6533 }
6534 }
6535 else {
6536 parser.findTargetFromAddressAndSectionNum(contentValue, nextReloc->r_symbolnum(), toTarget);
6537 useDirectBinding = (toTarget.atom->scope() == ld::Atom::scopeTranslationUnit) || ((toTarget.atom->combine() == ld::Atom::combineByNameAndContent) || (toTarget.atom->combine() == ld::Atom::combineByNameAndReferences));
6538 }
6539 if ( useDirectBinding ) {
6540 if ( (toTarget.atom->combine() == ld::Atom::combineByNameAndContent) || (toTarget.atom->combine() == ld::Atom::combineByNameAndReferences) )
6541 parser.addFixup(src, ld::Fixup::k1of4, ld::Fixup::kindSetTargetAddress, ld::Fixup::bindingByContentBound, toTarget.atom);
6542 else
6543 parser.addFixup(src, ld::Fixup::k1of4, ld::Fixup::kindSetTargetAddress, toTarget.atom);
6544 }
6545 else
6546 parser.addFixup(src, ld::Fixup::k1of4, ld::Fixup::kindSetTargetAddress, toTarget.weakImport, toTarget.name);
6547 parser.addFixup(src, ld::Fixup::k2of4, ld::Fixup::kindAddAddend, toTarget.addend);
6548 if ( target.atom == NULL )
6549 parser.addFixup(src, ld::Fixup::k3of4, ld::Fixup::kindSubtractTargetAddress, false, target.name);
6550 else
6551 parser.addFixup(src, ld::Fixup::k3of4, ld::Fixup::kindSubtractTargetAddress, target.atom);
6552 if ( reloc->r_length() == 2 )
6553 parser.addFixup(src, ld::Fixup::k4of4, ld::Fixup::kindStoreLittleEndian32);
6554 else
6555 parser.addFixup(src, ld::Fixup::k4of4, ld::Fixup::kindStoreLittleEndian64);
6556 break;
6557 case X86_64_RELOC_TLV:
6558 if ( ! reloc->r_extern() )
6559 throw "not extern and X86_64_RELOC_TLV not supported";
6560 if ( ! reloc->r_pcrel() )
6561 throw "not pcrel and X86_64_RELOC_TLV not supported";
6562 if ( reloc->r_length() != 2 )
6563 throw "length != 2 and X86_64_RELOC_TLV not supported";
6564 parser.addFixups(src, ld::Fixup::kindStoreX86PCRel32TLVLoad, target);
6565 break;
6566 default:
6567 throwf("unknown relocation type %d", reloc->r_type());
6568 }
6569 return result;
6570 }
6571
6572
6573
6574 template <>
6575 bool Section<x86>::addRelocFixup(class Parser<x86>& parser, const macho_relocation_info<P>* reloc)
6576 {
6577 const macho_section<P>* sect = this->machoSection();
6578 uint32_t srcAddr;
6579 const uint8_t* fixUpPtr;
6580 uint32_t contentValue = 0;
6581 ld::Fixup::Kind kind = ld::Fixup::kindNone;
6582 Parser<x86>::SourceLocation src;
6583 Parser<x86>::TargetDesc target;
6584
6585 if ( (reloc->r_address() & R_SCATTERED) == 0 ) {
6586 srcAddr = sect->addr() + reloc->r_address();
6587 src.atom = this->findAtomByAddress(srcAddr);
6588 src.offsetInAtom = srcAddr - src.atom->_objAddress;
6589 fixUpPtr = file().fileContent() + sect->offset() + reloc->r_address();
6590 switch ( reloc->r_type() ) {
6591 case GENERIC_RELOC_VANILLA:
6592 switch ( reloc->r_length() ) {
6593 case 0:
6594 contentValue = (int32_t)(int8_t)*fixUpPtr;
6595 if ( reloc->r_pcrel() ) {
6596 kind = ld::Fixup::kindStoreX86BranchPCRel8;
6597 contentValue += srcAddr + sizeof(uint8_t);
6598 }
6599 else
6600 throw "r_length=0 and r_pcrel=0 not supported";
6601 break;
6602 case 1:
6603 contentValue = (int32_t)(int16_t)E::get16(*((uint16_t*)fixUpPtr));
6604 if ( reloc->r_pcrel() ) {
6605 kind = ld::Fixup::kindStoreX86PCRel16;
6606 contentValue += srcAddr + sizeof(uint16_t);
6607 }
6608 else
6609 kind = ld::Fixup::kindStoreLittleEndian16;
6610 break;
6611 case 2:
6612 contentValue = E::get32(*((uint32_t*)fixUpPtr));
6613 if ( reloc->r_pcrel() ) {
6614 kind = ld::Fixup::kindStoreX86BranchPCRel32;
6615 contentValue += srcAddr + sizeof(uint32_t);
6616 }
6617 else
6618 kind = ld::Fixup::kindStoreLittleEndian32;
6619 break;
6620 case 3:
6621 throw "r_length=3 not supported";
6622 }
6623 if ( reloc->r_extern() ) {
6624 target.atom = NULL;
6625 const macho_nlist<P>& targetSymbol = parser.symbolFromIndex(reloc->r_symbolnum());
6626 target.name = parser.nameFromSymbol(targetSymbol);
6627 target.weakImport = parser.weakImportFromSymbol(targetSymbol);
6628 target.addend = (int32_t)contentValue;
6629 }
6630 else {
6631 parser.findTargetFromAddressAndSectionNum(contentValue, reloc->r_symbolnum(), target);
6632 }
6633 if ( (kind == ld::Fixup::kindStoreX86BranchPCRel32) && (target.name != NULL) ) {
6634 if ( strncmp(target.name, "___dtrace_probe$", 16) == 0 ) {
6635 parser.addFixup(src, ld::Fixup::k1of1, ld::Fixup::kindStoreX86DtraceCallSiteNop, false, target.name);
6636 parser.addDtraceExtraInfos(src, &target.name[16]);
6637 return false;
6638 }
6639 else if ( strncmp(target.name, "___dtrace_isenabled$", 20) == 0 ) {
6640 parser.addFixup(src, ld::Fixup::k1of1, ld::Fixup::kindStoreX86DtraceIsEnableSiteClear, false, target.name);
6641 parser.addDtraceExtraInfos(src, &target.name[20]);
6642 return false;
6643 }
6644 }
6645 parser.addFixups(src, kind, target);
6646 return false;
6647 break;
6648 case GENERIC_RLEOC_TLV:
6649 {
6650 if ( !reloc->r_extern() )
6651 throw "r_extern=0 and r_type=GENERIC_RLEOC_TLV not supported";
6652 if ( reloc->r_length() != 2 )
6653 throw "r_length!=2 and r_type=GENERIC_RLEOC_TLV not supported";
6654 const macho_nlist<P>& sym = parser.symbolFromIndex(reloc->r_symbolnum());
6655 // use direct reference for local symbols
6656 if ( ((sym.n_type() & N_TYPE) == N_SECT) && ((sym.n_type() & N_EXT) == 0) ) {
6657 parser.findTargetFromAddressAndSectionNum(sym.n_value(), sym.n_sect(), target);
6658 }
6659 else {
6660 target.atom = NULL;
6661 target.name = parser.nameFromSymbol(sym);
6662 target.weakImport = parser.weakImportFromSymbol(sym);
6663 }
6664 target.addend = (int64_t)(int32_t)E::get32(*((uint32_t*)fixUpPtr));
6665 if ( reloc->r_pcrel() ) {
6666 parser.addFixups(src, ld::Fixup::kindStoreX86PCRel32TLVLoad, target);
6667 }
6668 else {
6669 parser.addFixups(src, ld::Fixup::kindStoreX86Abs32TLVLoad, target);
6670 }
6671 return false;
6672 }
6673 break;
6674 default:
6675 throwf("unsupported i386 relocation type (%d)", reloc->r_type());
6676 }
6677 }
6678 else {
6679 // scattered relocation
6680 const macho_scattered_relocation_info<P>* sreloc = (macho_scattered_relocation_info<P>*)reloc;
6681 srcAddr = sect->addr() + sreloc->r_address();
6682 src.atom = this->findAtomByAddress(srcAddr);
6683 assert(src.atom != NULL);
6684 src.offsetInAtom = srcAddr - src.atom->_objAddress;
6685 fixUpPtr = file().fileContent() + sect->offset() + sreloc->r_address();
6686 uint32_t relocValue = sreloc->r_value();
6687 bool result = false;
6688 // file format allows pair to be scattered or not
6689 const macho_scattered_relocation_info<P>* nextSReloc = &sreloc[1];
6690 const macho_relocation_info<P>* nextReloc = &reloc[1];
6691 bool nextRelocIsPair = false;
6692 uint32_t nextRelocAddress = 0;
6693 uint32_t nextRelocValue = 0;
6694 if ( (nextReloc->r_address() & R_SCATTERED) == 0 ) {
6695 if ( nextReloc->r_type() == GENERIC_RELOC_PAIR ) {
6696 nextRelocIsPair = true;
6697 nextRelocAddress = nextReloc->r_address();
6698 result = true; // iterator should skip next reloc, since we've consumed it here
6699 }
6700 }
6701 else {
6702 if ( nextSReloc->r_type() == GENERIC_RELOC_PAIR ) {
6703 nextRelocIsPair = true;
6704 nextRelocAddress = nextSReloc->r_address();
6705 nextRelocValue = nextSReloc->r_value();
6706 }
6707 }
6708 switch (sreloc->r_type()) {
6709 case GENERIC_RELOC_VANILLA:
6710 // with a scattered relocation we get both the target (sreloc->r_value()) and the target+offset (*fixUpPtr)
6711 target.atom = parser.findAtomByAddress(relocValue);
6712 if ( sreloc->r_pcrel() ) {
6713 switch ( sreloc->r_length() ) {
6714 case 0:
6715 contentValue = srcAddr + 1 + *fixUpPtr;
6716 target.addend = (int32_t)contentValue - (int32_t)relocValue;
6717 parser.addFixups(src, ld::Fixup::kindStoreX86PCRel8, target);
6718 break;
6719 case 1:
6720 contentValue = srcAddr + 2 + LittleEndian::get16(*((uint16_t*)fixUpPtr));
6721 target.addend = (int32_t)contentValue - (int32_t)relocValue;
6722 parser.addFixups(src, ld::Fixup::kindStoreX86PCRel16, target);
6723 break;
6724 case 2:
6725 contentValue = srcAddr + 4 + LittleEndian::get32(*((uint32_t*)fixUpPtr));
6726 target.addend = (int32_t)contentValue - (int32_t)relocValue;
6727 parser.addFixups(src, ld::Fixup::kindStoreX86PCRel32, target);
6728 break;
6729 case 3:
6730 throw "unsupported r_length=3 for scattered pc-rel vanilla reloc";
6731 break;
6732 }
6733 }
6734 else {
6735 if ( sreloc->r_length() != 2 )
6736 throwf("unsupported r_length=%d for scattered vanilla reloc", sreloc->r_length());
6737 contentValue = LittleEndian::get32(*((uint32_t*)fixUpPtr));
6738 target.addend = (int32_t)contentValue - (int32_t)(target.atom->objectAddress());
6739 parser.addFixups(src, ld::Fixup::kindStoreLittleEndian32, target);
6740 }
6741 break;
6742 case GENERIC_RELOC_SECTDIFF:
6743 case GENERIC_RELOC_LOCAL_SECTDIFF:
6744 {
6745 if ( !nextRelocIsPair )
6746 throw "GENERIC_RELOC_SECTDIFF missing following pair";
6747 switch ( sreloc->r_length() ) {
6748 case 0:
6749 case 3:
6750 throw "bad length for GENERIC_RELOC_SECTDIFF";
6751 case 1:
6752 contentValue = (int32_t)(int16_t)LittleEndian::get16(*((uint16_t*)fixUpPtr));
6753 kind = ld::Fixup::kindStoreLittleEndian16;
6754 break;
6755 case 2:
6756 contentValue = LittleEndian::get32(*((uint32_t*)fixUpPtr));
6757 kind = ld::Fixup::kindStoreLittleEndian32;
6758 break;
6759 }
6760 Atom<x86>* fromAtom = parser.findAtomByAddress(nextRelocValue);
6761 uint32_t offsetInFrom = nextRelocValue - fromAtom->_objAddress;
6762 parser.findTargetFromAddress(sreloc->r_value(), target);
6763 // check for addend encoded in the section content
6764 int64_t addend = (int32_t)contentValue - (int32_t)(sreloc->r_value() - nextRelocValue);
6765 if ( addend < 0 ) {
6766 // switch binding base on coalescing
6767 if ( target.atom == NULL ) {
6768 parser.addFixup(src, ld::Fixup::k1of5, ld::Fixup::kindSetTargetAddress, false, target.name);
6769 }
6770 else if ( target.atom->scope() == ld::Atom::scopeTranslationUnit ) {
6771 parser.addFixup(src, ld::Fixup::k1of5, ld::Fixup::kindSetTargetAddress, target.atom);
6772 }
6773 else if ( (target.atom->combine() == ld::Atom::combineByNameAndContent) || (target.atom->combine() == ld::Atom::combineByNameAndReferences) ) {
6774 parser.addFixup(src, ld::Fixup::k1of5, ld::Fixup::kindSetTargetAddress, ld::Fixup::bindingByContentBound, target.atom);
6775 }
6776 else {
6777 parser.addFixup(src, ld::Fixup::k1of5, ld::Fixup::kindSetTargetAddress, false, target.atom->name());
6778 }
6779 parser.addFixup(src, ld::Fixup::k2of5, ld::Fixup::kindAddAddend, target.addend);
6780 parser.addFixup(src, ld::Fixup::k3of5, ld::Fixup::kindSubtractTargetAddress, fromAtom);
6781 parser.addFixup(src, ld::Fixup::k4of5, ld::Fixup::kindSubtractAddend, offsetInFrom-addend);
6782 parser.addFixup(src, ld::Fixup::k5of5, kind);
6783 }
6784 else {
6785 // switch binding base on coalescing
6786 if ( target.atom == NULL ) {
6787 parser.addFixup(src, ld::Fixup::k1of5, ld::Fixup::kindSetTargetAddress, false, target.name);
6788 }
6789 else if ( target.atom->scope() == ld::Atom::scopeTranslationUnit ) {
6790 parser.addFixup(src, ld::Fixup::k1of5, ld::Fixup::kindSetTargetAddress, target.atom);
6791 }
6792 else if ( (target.atom->combine() == ld::Atom::combineByNameAndContent) || (target.atom->combine() == ld::Atom::combineByNameAndReferences) ) {
6793 parser.addFixup(src, ld::Fixup::k1of5, ld::Fixup::kindSetTargetAddress, ld::Fixup::bindingByContentBound, target.atom);
6794 }
6795 else {
6796 parser.addFixup(src, ld::Fixup::k1of5, ld::Fixup::kindSetTargetAddress, false, target.atom->name());
6797 }
6798 parser.addFixup(src, ld::Fixup::k2of5, ld::Fixup::kindAddAddend, target.addend+addend);
6799 parser.addFixup(src, ld::Fixup::k3of5, ld::Fixup::kindSubtractTargetAddress, fromAtom);
6800 parser.addFixup(src, ld::Fixup::k4of5, ld::Fixup::kindSubtractAddend, offsetInFrom);
6801 parser.addFixup(src, ld::Fixup::k5of5, kind);
6802 }
6803 }
6804 break;
6805 }
6806 return result;
6807 }
6808 }
6809
6810
6811
6812
6813
6814 #if SUPPORT_ARCH_arm_any
6815 template <>
6816 bool Section<arm>::addRelocFixup(class Parser<arm>& parser, const macho_relocation_info<P>* reloc)
6817 {
6818 const macho_section<P>* sect = this->machoSection();
6819 bool result = false;
6820 uint32_t srcAddr;
6821 uint32_t dstAddr;
6822 uint32_t* fixUpPtr;
6823 int32_t displacement = 0;
6824 uint32_t instruction = 0;
6825 pint_t contentValue = 0;
6826 Parser<arm>::SourceLocation src;
6827 Parser<arm>::TargetDesc target;
6828 const macho_relocation_info<P>* nextReloc;
6829
6830 if ( (reloc->r_address() & R_SCATTERED) == 0 ) {
6831 bool externSymbolIsThumbDef = false;
6832 srcAddr = sect->addr() + reloc->r_address();
6833 src.atom = this->findAtomByAddress(srcAddr);
6834 src.offsetInAtom = srcAddr - src.atom->_objAddress;
6835 fixUpPtr = (uint32_t*)(file().fileContent() + sect->offset() + reloc->r_address());
6836 if ( reloc->r_type() != ARM_RELOC_PAIR )
6837 instruction = LittleEndian::get32(*fixUpPtr);
6838 if ( reloc->r_extern() ) {
6839 const macho_nlist<P>& targetSymbol = parser.symbolFromIndex(reloc->r_symbolnum());
6840 // use direct reference for local symbols
6841 if ( ((targetSymbol.n_type() & N_TYPE) == N_SECT) && (((targetSymbol.n_type() & N_EXT) == 0) || (parser.nameFromSymbol(targetSymbol)[0] == 'L')) ) {
6842 parser.findTargetFromAddressAndSectionNum(targetSymbol.n_value(), targetSymbol.n_sect(), target);
6843 }
6844 else {
6845 target.atom = NULL;
6846 target.name = parser.nameFromSymbol(targetSymbol);
6847 target.weakImport = parser.weakImportFromSymbol(targetSymbol);
6848 if ( ((targetSymbol.n_type() & N_TYPE) == N_SECT) && (targetSymbol.n_desc() & N_ARM_THUMB_DEF) )
6849 externSymbolIsThumbDef = true;
6850 }
6851 }
6852 switch ( reloc->r_type() ) {
6853 case ARM_RELOC_BR24:
6854 // Sign-extend displacement
6855 displacement = (instruction & 0x00FFFFFF) << 2;
6856 if ( (displacement & 0x02000000) != 0 )
6857 displacement |= 0xFC000000;
6858 // The pc added will be +8 from the pc
6859 displacement += 8;
6860 // If this is BLX add H << 1
6861 if ((instruction & 0xFE000000) == 0xFA000000)
6862 displacement += ((instruction & 0x01000000) >> 23);
6863 if ( reloc->r_extern() ) {
6864 dstAddr = srcAddr + displacement;
6865 // <rdar://problem/16652542> support large .o files
6866 if ( srcAddr > 0x2000000 ) {
6867 dstAddr -= ((srcAddr + 0x1FFFFFF) & 0xFC000000);
6868 }
6869 target.addend = dstAddr;
6870 if ( externSymbolIsThumbDef )
6871 target.addend &= -2; // remove thumb bit
6872 }
6873 else {
6874 dstAddr = srcAddr + displacement;
6875 parser.findTargetFromAddressAndSectionNum(dstAddr, reloc->r_symbolnum(), target);
6876 }
6877 // special case "calls" for dtrace
6878 if ( (target.name != NULL) && (strncmp(target.name, "___dtrace_probe$", 16) == 0) ) {
6879 parser.addFixup(src, ld::Fixup::k1of1,
6880 ld::Fixup::kindStoreARMDtraceCallSiteNop, false, target.name);
6881 parser.addDtraceExtraInfos(src, &target.name[16]);
6882 }
6883 else if ( (target.name != NULL) && (strncmp(target.name, "___dtrace_isenabled$", 20) == 0) ) {
6884 parser.addFixup(src, ld::Fixup::k1of1,
6885 ld::Fixup::kindStoreARMDtraceIsEnableSiteClear, false, target.name);
6886 parser.addDtraceExtraInfos(src, &target.name[20]);
6887 }
6888 else {
6889 parser.addFixups(src, ld::Fixup::kindStoreARMBranch24, target);
6890 }
6891 break;
6892 case ARM_THUMB_RELOC_BR22:
6893 // thumb2 added two more bits to displacement, complicating the displacement decoding
6894 {
6895 uint32_t s = (instruction >> 10) & 0x1;
6896 uint32_t j1 = (instruction >> 29) & 0x1;
6897 uint32_t j2 = (instruction >> 27) & 0x1;
6898 uint32_t imm10 = instruction & 0x3FF;
6899 uint32_t imm11 = (instruction >> 16) & 0x7FF;
6900 uint32_t i1 = (j1 == s);
6901 uint32_t i2 = (j2 == s);
6902 uint32_t dis = (s << 24) | (i1 << 23) | (i2 << 22) | (imm10 << 12) | (imm11 << 1);
6903 int32_t sdis = dis;
6904 if ( s )
6905 sdis |= 0xFE000000;
6906 displacement = sdis;
6907 }
6908 // The pc added will be +4 from the pc
6909 displacement += 4;
6910 // If the instruction was blx, force the low 2 bits to be clear
6911 dstAddr = srcAddr + displacement;
6912 if ((instruction & 0xD0000000) == 0xC0000000)
6913 dstAddr &= 0xFFFFFFFC;
6914
6915 if ( reloc->r_extern() ) {
6916 // <rdar://problem/16652542> support large .o files
6917 if ( srcAddr > 0x1000000 ) {
6918 dstAddr -= ((srcAddr + 0xFFFFFF) & 0xFE000000);
6919 }
6920 target.addend = (int64_t)(int32_t)dstAddr;
6921 }
6922 else {
6923 parser.findTargetFromAddressAndSectionNum(dstAddr, reloc->r_symbolnum(), target);
6924 }
6925 // special case "calls" for dtrace
6926 if ( (target.name != NULL) && (strncmp(target.name, "___dtrace_probe$", 16) == 0) ) {
6927 parser.addFixup(src, ld::Fixup::k1of1,
6928 ld::Fixup::kindStoreThumbDtraceCallSiteNop, false, target.name);
6929 parser.addDtraceExtraInfos(src, &target.name[16]);
6930 }
6931 else if ( (target.name != NULL) && (strncmp(target.name, "___dtrace_isenabled$", 20) == 0) ) {
6932 parser.addFixup(src, ld::Fixup::k1of1,
6933 ld::Fixup::kindStoreThumbDtraceIsEnableSiteClear, false, target.name);
6934 parser.addDtraceExtraInfos(src, &target.name[20]);
6935 }
6936 else {
6937 parser.addFixups(src, ld::Fixup::kindStoreThumbBranch22, target);
6938 }
6939 break;
6940 case ARM_RELOC_VANILLA:
6941 if ( reloc->r_length() != 2 )
6942 throw "bad length for ARM_RELOC_VANILLA";
6943 contentValue = LittleEndian::get32(*fixUpPtr);
6944 if ( reloc->r_extern() ) {
6945 target.addend = (int32_t)contentValue;
6946 if ( externSymbolIsThumbDef )
6947 target.addend &= -2; // remove thumb bit
6948 }
6949 else {
6950 parser.findTargetFromAddressAndSectionNum(contentValue, reloc->r_symbolnum(), target);
6951 // possible non-extern relocation turned into by-name ref because target is a weak-def
6952 if ( target.atom != NULL ) {
6953 if ( target.atom->isThumb() )
6954 target.addend &= -2; // remove thumb bit
6955 // if reference to LSDA, add group subordinate fixup
6956 if ( target.atom->contentType() == ld::Atom::typeLSDA ) {
6957 Parser<arm>::SourceLocation src2;
6958 src2.atom = src.atom;
6959 src2.offsetInAtom = 0;
6960 parser.addFixup(src2, ld::Fixup::k1of1, ld::Fixup::kindNoneGroupSubordinateLSDA, target.atom);
6961 }
6962 }
6963 }
6964 parser.addFixups(src, ld::Fixup::kindStoreLittleEndian32, target);
6965 break;
6966 case ARM_THUMB_32BIT_BRANCH:
6967 // silently ignore old unnecessary reloc
6968 break;
6969 case ARM_RELOC_HALF:
6970 nextReloc = &reloc[1];
6971 if ( nextReloc->r_type() == ARM_RELOC_PAIR ) {
6972 uint32_t instruction16;
6973 uint32_t other16 = (nextReloc->r_address() & 0xFFFF);
6974 bool isThumb;
6975 if ( reloc->r_length() & 2 ) {
6976 isThumb = true;
6977 uint32_t i = ((instruction & 0x00000400) >> 10);
6978 uint32_t imm4 = (instruction & 0x0000000F);
6979 uint32_t imm3 = ((instruction & 0x70000000) >> 28);
6980 uint32_t imm8 = ((instruction & 0x00FF0000) >> 16);
6981 instruction16 = (imm4 << 12) | (i << 11) | (imm3 << 8) | imm8;
6982 }
6983 else {
6984 isThumb = false;
6985 uint32_t imm4 = ((instruction & 0x000F0000) >> 16);
6986 uint32_t imm12 = (instruction & 0x00000FFF);
6987 instruction16 = (imm4 << 12) | imm12;
6988 }
6989 if ( reloc->r_length() & 1 ) {
6990 // high 16
6991 dstAddr = ((instruction16 << 16) | other16);
6992 if ( reloc->r_extern() ) {
6993 target.addend = dstAddr;
6994 if ( externSymbolIsThumbDef )
6995 target.addend &= -2; // remove thumb bit
6996 }
6997 else {
6998 parser.findTargetFromAddress(dstAddr, target);
6999 if ( target.atom->isThumb() )
7000 target.addend &= (-2); // remove thumb bit
7001 }
7002 parser.addFixups(src, (isThumb ? ld::Fixup::kindStoreThumbHigh16 : ld::Fixup::kindStoreARMHigh16), target);
7003 }
7004 else {
7005 // low 16
7006 dstAddr = (other16 << 16) | instruction16;
7007 if ( reloc->r_extern() ) {
7008 target.addend = dstAddr;
7009 if ( externSymbolIsThumbDef )
7010 target.addend &= -2; // remove thumb bit
7011 }
7012 else {
7013 parser.findTargetFromAddress(dstAddr, target);
7014 if ( target.atom->isThumb() )
7015 target.addend &= (-2); // remove thumb bit
7016 }
7017 parser.addFixups(src, (isThumb ? ld::Fixup::kindStoreThumbLow16 : ld::Fixup::kindStoreARMLow16), target);
7018 }
7019 result = true;
7020 }
7021 else
7022 throw "for ARM_RELOC_HALF, next reloc is not ARM_RELOC_PAIR";
7023 break;
7024 default:
7025 throwf("unknown relocation type %d", reloc->r_type());
7026 break;
7027 }
7028 }
7029 else {
7030 const macho_scattered_relocation_info<P>* sreloc = (macho_scattered_relocation_info<P>*)reloc;
7031 // file format allows pair to be scattered or not
7032 const macho_scattered_relocation_info<P>* nextSReloc = &sreloc[1];
7033 nextReloc = &reloc[1];
7034 srcAddr = sect->addr() + sreloc->r_address();
7035 dstAddr = sreloc->r_value();
7036 fixUpPtr = (uint32_t*)(file().fileContent() + sect->offset() + sreloc->r_address());
7037 instruction = LittleEndian::get32(*fixUpPtr);
7038 src.atom = this->findAtomByAddress(srcAddr);
7039 src.offsetInAtom = srcAddr - src.atom->_objAddress;
7040 bool nextRelocIsPair = false;
7041 uint32_t nextRelocAddress = 0;
7042 uint32_t nextRelocValue = 0;
7043 if ( (nextReloc->r_address() & R_SCATTERED) == 0 ) {
7044 if ( nextReloc->r_type() == ARM_RELOC_PAIR ) {
7045 nextRelocIsPair = true;
7046 nextRelocAddress = nextReloc->r_address();
7047 result = true;
7048 }
7049 }
7050 else {
7051 if ( nextSReloc->r_type() == ARM_RELOC_PAIR ) {
7052 nextRelocIsPair = true;
7053 nextRelocAddress = nextSReloc->r_address();
7054 nextRelocValue = nextSReloc->r_value();
7055 result = true;
7056 }
7057 }
7058 switch ( sreloc->r_type() ) {
7059 case ARM_RELOC_VANILLA:
7060 // with a scattered relocation we get both the target (sreloc->r_value()) and the target+offset (*fixUpPtr)
7061 if ( sreloc->r_length() != 2 )
7062 throw "bad length for ARM_RELOC_VANILLA";
7063 target.atom = parser.findAtomByAddress(sreloc->r_value());
7064 if ( target.atom == NULL )
7065 throwf("bad r_value (0x%08X) for ARM_RELOC_VANILLA\n", sreloc->r_value());
7066 contentValue = LittleEndian::get32(*fixUpPtr);
7067 target.addend = contentValue - target.atom->_objAddress;
7068 if ( target.atom->isThumb() )
7069 target.addend &= -2; // remove thumb bit
7070 parser.addFixups(src, ld::Fixup::kindStoreLittleEndian32, target);
7071 break;
7072 case ARM_RELOC_BR24:
7073 // Sign-extend displacement
7074 displacement = (instruction & 0x00FFFFFF) << 2;
7075 if ( (displacement & 0x02000000) != 0 )
7076 displacement |= 0xFC000000;
7077 // The pc added will be +8 from the pc
7078 displacement += 8;
7079 // If this is BLX add H << 1
7080 if ((instruction & 0xFE000000) == 0xFA000000)
7081 displacement += ((instruction & 0x01000000) >> 23);
7082 target.atom = parser.findAtomByAddress(sreloc->r_value());
7083 target.addend = (int64_t)(srcAddr + displacement) - (int64_t)(target.atom->_objAddress);
7084 parser.addFixups(src, ld::Fixup::kindStoreARMBranch24, target);
7085 break;
7086 case ARM_THUMB_RELOC_BR22:
7087 // thumb2 added two more bits to displacement, complicating the displacement decoding
7088 {
7089 uint32_t s = (instruction >> 10) & 0x1;
7090 uint32_t j1 = (instruction >> 29) & 0x1;
7091 uint32_t j2 = (instruction >> 27) & 0x1;
7092 uint32_t imm10 = instruction & 0x3FF;
7093 uint32_t imm11 = (instruction >> 16) & 0x7FF;
7094 uint32_t i1 = (j1 == s);
7095 uint32_t i2 = (j2 == s);
7096 uint32_t dis = (s << 24) | (i1 << 23) | (i2 << 22) | (imm10 << 12) | (imm11 << 1);
7097 int32_t sdis = dis;
7098 if ( s )
7099 sdis |= 0xFE000000;
7100 displacement = sdis;
7101 }
7102 // The pc added will be +4 from the pc
7103 displacement += 4;
7104 dstAddr = srcAddr+displacement;
7105 // If the instruction was blx, force the low 2 bits to be clear
7106 if ((instruction & 0xF8000000) == 0xE8000000)
7107 dstAddr &= 0xFFFFFFFC;
7108 target.atom = parser.findAtomByAddress(sreloc->r_value());
7109 target.addend = dstAddr - target.atom->_objAddress;
7110 parser.addFixups(src, ld::Fixup::kindStoreThumbBranch22, target);
7111 break;
7112 case ARM_RELOC_SECTDIFF:
7113 case ARM_RELOC_LOCAL_SECTDIFF:
7114 {
7115 if ( ! nextRelocIsPair )
7116 throw "ARM_RELOC_SECTDIFF missing following pair";
7117 if ( sreloc->r_length() != 2 )
7118 throw "bad length for ARM_RELOC_SECTDIFF";
7119 contentValue = LittleEndian::get32(*fixUpPtr);
7120 Atom<arm>* fromAtom = parser.findAtomByAddress(nextRelocValue);
7121 uint32_t offsetInFrom = nextRelocValue - fromAtom->_objAddress;
7122 uint32_t offsetInTarget;
7123 Atom<arm>* targetAtom = parser.findAtomByAddressOrLocalTargetOfStub(sreloc->r_value(), &offsetInTarget);
7124 // check for addend encoded in the section content
7125 int64_t addend = (int32_t)contentValue - (int32_t)(sreloc->r_value() - nextRelocValue);
7126 if ( targetAtom->isThumb() )
7127 addend &= -2; // remove thumb bit
7128 // if reference to LSDA, add group subordinate fixup
7129 if ( targetAtom->contentType() == ld::Atom::typeLSDA ) {
7130 Parser<arm>::SourceLocation src2;
7131 src2.atom = src.atom;
7132 src2.offsetInAtom = 0;
7133 parser.addFixup(src2, ld::Fixup::k1of1, ld::Fixup::kindNoneGroupSubordinateLSDA, targetAtom);
7134 }
7135 if ( addend < 0 ) {
7136 // switch binding base on coalescing
7137 if ( targetAtom->scope() == ld::Atom::scopeTranslationUnit ) {
7138 parser.addFixup(src, ld::Fixup::k1of5, ld::Fixup::kindSetTargetAddress, targetAtom);
7139 }
7140 else if ( (targetAtom->combine() == ld::Atom::combineByNameAndContent) || (targetAtom->combine() == ld::Atom::combineByNameAndReferences) ) {
7141 parser.addFixup(src, ld::Fixup::k1of5, ld::Fixup::kindSetTargetAddress, ld::Fixup::bindingByContentBound, targetAtom);
7142 }
7143 else {
7144 parser.addFixup(src, ld::Fixup::k1of5, ld::Fixup::kindSetTargetAddress, false, targetAtom->name());
7145 }
7146 parser.addFixup(src, ld::Fixup::k2of5, ld::Fixup::kindAddAddend, offsetInTarget);
7147 parser.addFixup(src, ld::Fixup::k3of5, ld::Fixup::kindSubtractTargetAddress, fromAtom);
7148 parser.addFixup(src, ld::Fixup::k4of5, ld::Fixup::kindSubtractAddend, offsetInFrom-addend);
7149 parser.addFixup(src, ld::Fixup::k5of5, ld::Fixup::kindStoreLittleEndian32);
7150 }
7151 else {
7152 if ( targetAtom->scope() == ld::Atom::scopeTranslationUnit ) {
7153 parser.addFixup(src, ld::Fixup::k1of5, ld::Fixup::kindSetTargetAddress, targetAtom);
7154 }
7155 else if ( (targetAtom->combine() == ld::Atom::combineByNameAndContent) || (targetAtom->combine() == ld::Atom::combineByNameAndReferences) ) {
7156 parser.addFixup(src, ld::Fixup::k1of5, ld::Fixup::kindSetTargetAddress, ld::Fixup::bindingByContentBound, targetAtom);
7157 }
7158 else {
7159 parser.addFixup(src, ld::Fixup::k1of5, ld::Fixup::kindSetTargetAddress, false, targetAtom->name());
7160 }
7161 parser.addFixup(src, ld::Fixup::k2of5, ld::Fixup::kindAddAddend, (uint32_t)(offsetInTarget+addend));
7162 parser.addFixup(src, ld::Fixup::k3of5, ld::Fixup::kindSubtractTargetAddress, fromAtom);
7163 parser.addFixup(src, ld::Fixup::k4of5, ld::Fixup::kindSubtractAddend, offsetInFrom);
7164 parser.addFixup(src, ld::Fixup::k5of5, ld::Fixup::kindStoreLittleEndian32);
7165 }
7166 }
7167 break;
7168 case ARM_RELOC_HALF_SECTDIFF:
7169 if ( nextRelocIsPair ) {
7170 instruction = LittleEndian::get32(*fixUpPtr);
7171 Atom<arm>* fromAtom = parser.findAtomByAddress(nextRelocValue);
7172 uint32_t offsetInFrom = nextRelocValue - fromAtom->_objAddress;
7173 Atom<arm>* targetAtom = parser.findAtomByAddress(sreloc->r_value());
7174 uint32_t offsetInTarget = sreloc->r_value() - targetAtom->_objAddress;
7175 uint32_t instruction16;
7176 uint32_t other16 = (nextRelocAddress & 0xFFFF);
7177 bool isThumb;
7178 if ( sreloc->r_length() & 2 ) {
7179 isThumb = true;
7180 uint32_t i = ((instruction & 0x00000400) >> 10);
7181 uint32_t imm4 = (instruction & 0x0000000F);
7182 uint32_t imm3 = ((instruction & 0x70000000) >> 28);
7183 uint32_t imm8 = ((instruction & 0x00FF0000) >> 16);
7184 instruction16 = (imm4 << 12) | (i << 11) | (imm3 << 8) | imm8;
7185 }
7186 else {
7187 isThumb = false;
7188 uint32_t imm4 = ((instruction & 0x000F0000) >> 16);
7189 uint32_t imm12 = (instruction & 0x00000FFF);
7190 instruction16 = (imm4 << 12) | imm12;
7191 }
7192 if ( sreloc->r_length() & 1 )
7193 dstAddr = ((instruction16 << 16) | other16);
7194 else
7195 dstAddr = (other16 << 16) | instruction16;
7196 if ( targetAtom->isThumb() )
7197 dstAddr &= (-2); // remove thumb bit
7198 int32_t addend = dstAddr - (sreloc->r_value() - nextRelocValue);
7199 if ( targetAtom->scope() == ld::Atom::scopeTranslationUnit ) {
7200 parser.addFixup(src, ld::Fixup::k1of5, ld::Fixup::kindSetTargetAddress, targetAtom);
7201 }
7202 else if ( (targetAtom->combine() == ld::Atom::combineByNameAndContent) || (targetAtom->combine() == ld::Atom::combineByNameAndReferences) ) {
7203 parser.addFixup(src, ld::Fixup::k1of5, ld::Fixup::kindSetTargetAddress, ld::Fixup::bindingByContentBound, targetAtom);
7204 }
7205 else {
7206 parser.addFixup(src, ld::Fixup::k1of5, ld::Fixup::kindSetTargetAddress, false, targetAtom->name());
7207 }
7208 parser.addFixup(src, ld::Fixup::k2of5, ld::Fixup::kindAddAddend, (uint32_t)offsetInTarget+addend);
7209 parser.addFixup(src, ld::Fixup::k3of5, ld::Fixup::kindSubtractTargetAddress, fromAtom);
7210 parser.addFixup(src, ld::Fixup::k4of5, ld::Fixup::kindSubtractAddend, offsetInFrom);
7211 if ( sreloc->r_length() & 1 ) {
7212 // high 16
7213 parser.addFixup(src, ld::Fixup::k5of5, (isThumb ? ld::Fixup::kindStoreThumbHigh16 : ld::Fixup::kindStoreARMHigh16));
7214 }
7215 else {
7216 // low 16
7217 parser.addFixup(src, ld::Fixup::k5of5, (isThumb ? ld::Fixup::kindStoreThumbLow16 : ld::Fixup::kindStoreARMLow16));
7218 }
7219 result = true;
7220 }
7221 else
7222 throw "ARM_RELOC_HALF_SECTDIFF reloc missing following pair";
7223 break;
7224 case ARM_RELOC_HALF:
7225 if ( nextRelocIsPair ) {
7226 instruction = LittleEndian::get32(*fixUpPtr);
7227 Atom<arm>* targetAtom = parser.findAtomByAddress(sreloc->r_value());
7228 uint32_t instruction16;
7229 uint32_t other16 = (nextRelocAddress & 0xFFFF);
7230 bool isThumb;
7231 if ( sreloc->r_length() & 2 ) {
7232 isThumb = true;
7233 uint32_t i = ((instruction & 0x00000400) >> 10);
7234 uint32_t imm4 = (instruction & 0x0000000F);
7235 uint32_t imm3 = ((instruction & 0x70000000) >> 28);
7236 uint32_t imm8 = ((instruction & 0x00FF0000) >> 16);
7237 instruction16 = (imm4 << 12) | (i << 11) | (imm3 << 8) | imm8;
7238 }
7239 else {
7240 isThumb = false;
7241 uint32_t imm4 = ((instruction & 0x000F0000) >> 16);
7242 uint32_t imm12 = (instruction & 0x00000FFF);
7243 instruction16 = (imm4 << 12) | imm12;
7244 }
7245 if ( sreloc->r_length() & 1 )
7246 dstAddr = ((instruction16 << 16) | other16);
7247 else
7248 dstAddr = (other16 << 16) | instruction16;
7249 if ( targetAtom->scope() == ld::Atom::scopeTranslationUnit ) {
7250 parser.addFixup(src, ld::Fixup::k1of3, ld::Fixup::kindSetTargetAddress, targetAtom);
7251 }
7252 else if ( (targetAtom->combine() == ld::Atom::combineByNameAndContent) || (targetAtom->combine() == ld::Atom::combineByNameAndReferences) ) {
7253 parser.addFixup(src, ld::Fixup::k1of3, ld::Fixup::kindSetTargetAddress, ld::Fixup::bindingByContentBound, targetAtom);
7254 }
7255 else {
7256 parser.addFixup(src, ld::Fixup::k1of3, ld::Fixup::kindSetTargetAddress, false, targetAtom->name());
7257 }
7258 parser.addFixup(src, ld::Fixup::k2of3, ld::Fixup::kindAddAddend, dstAddr - targetAtom->_objAddress);
7259 if ( sreloc->r_length() & 1 ) {
7260 // high 16
7261 parser.addFixup(src, ld::Fixup::k3of3, (isThumb ? ld::Fixup::kindStoreThumbHigh16 : ld::Fixup::kindStoreARMHigh16));
7262 }
7263 else {
7264 // low 16
7265 parser.addFixup(src, ld::Fixup::k3of3, (isThumb ? ld::Fixup::kindStoreThumbLow16 : ld::Fixup::kindStoreARMLow16));
7266 }
7267 result = true;
7268 }
7269 else
7270 throw "scattered ARM_RELOC_HALF reloc missing following pair";
7271 break;
7272 default:
7273 throwf("unknown ARM scattered relocation type %d", sreloc->r_type());
7274 }
7275 }
7276 return result;
7277 }
7278 #endif
7279
7280
7281 #if SUPPORT_ARCH_arm64
7282 template <>
7283 bool Section<arm64>::addRelocFixup(class Parser<arm64>& parser, const macho_relocation_info<P>* reloc)
7284 {
7285 bool result = false;
7286 Parser<arm64>::SourceLocation src;
7287 Parser<arm64>::TargetDesc target = { NULL, NULL, false, 0 };
7288 Parser<arm64>::TargetDesc toTarget;
7289 int32_t prefixRelocAddend = 0;
7290 if ( reloc->r_type() == ARM64_RELOC_ADDEND ) {
7291 uint32_t rawAddend = reloc->r_symbolnum();
7292 prefixRelocAddend = rawAddend;
7293 if ( rawAddend & 0x00800000 )
7294 prefixRelocAddend |= 0xFF000000; // sign extend 24-bit signed int to 32-bits
7295 uint32_t addendAddress = reloc->r_address();
7296 ++reloc; //advance to next reloc record
7297 result = true;
7298 if ( reloc->r_address() != addendAddress )
7299 throw "ARM64_RELOC_ADDEND r_address does not match next reloc's r_address";
7300 }
7301 const macho_section<P>* sect = this->machoSection();
7302 uint64_t srcAddr = sect->addr() + reloc->r_address();
7303 src.atom = this->findAtomByAddress(srcAddr);
7304 src.offsetInAtom = srcAddr - src.atom->_objAddress;
7305 const uint8_t* fixUpPtr = file().fileContent() + sect->offset() + reloc->r_address();
7306 uint64_t contentValue = 0;
7307 const macho_relocation_info<arm64::P>* nextReloc = &reloc[1];
7308 bool useDirectBinding;
7309 uint32_t instruction;
7310 uint32_t encodedAddend;
7311 switch ( reloc->r_length() ) {
7312 case 0:
7313 contentValue = *fixUpPtr;
7314 break;
7315 case 1:
7316 contentValue = (int64_t)(int16_t)E::get16(*((uint16_t*)fixUpPtr));
7317 break;
7318 case 2:
7319 contentValue = (int64_t)(int32_t)E::get32(*((uint32_t*)fixUpPtr));
7320 break;
7321 case 3:
7322 contentValue = E::get64(*((uint64_t*)fixUpPtr));
7323 break;
7324 }
7325 if ( reloc->r_extern() ) {
7326 const macho_nlist<P>& sym = parser.symbolFromIndex(reloc->r_symbolnum());
7327 const char* symbolName = parser.nameFromSymbol(sym);
7328 if ( ((sym.n_type() & N_TYPE) == N_SECT) && (((sym.n_type() & N_EXT) == 0) || (symbolName[0] == 'L') || (symbolName[0] == 'l')) ) {
7329 // use direct reference for local symbols
7330 parser.findTargetFromAddressAndSectionNum(sym.n_value(), sym.n_sect(), target);
7331 //target.addend += contentValue;
7332 }
7333 else if ( ((sym.n_type() & N_TYPE) == N_SECT) && (src.atom->_objAddress <= sym.n_value()) && (sym.n_value() < (src.atom->_objAddress+src.atom->size())) ) {
7334 // <rdar://problem/13700961> spurious warning when weak function has reference to itself
7335 // use direct reference when atom targets itself
7336 target.atom = src.atom;
7337 target.name = NULL;
7338 }
7339 else {
7340 target.name = symbolName;
7341 target.weakImport = parser.weakImportFromSymbol(sym);
7342 //target.addend = contentValue;
7343 }
7344 // cfstrings should always use direct reference to backing store
7345 if ( (this->type() == ld::Section::typeCFString) && (src.offsetInAtom != 0) ) {
7346 parser.findTargetFromAddressAndSectionNum(sym.n_value(), sym.n_sect(), target);
7347 //target.addend = contentValue;
7348 }
7349 }
7350 else {
7351 if ( reloc->r_pcrel() )
7352 contentValue += srcAddr;
7353 parser.findTargetFromAddressAndSectionNum(contentValue, reloc->r_symbolnum(), target);
7354 }
7355 switch ( reloc->r_type() ) {
7356 case ARM64_RELOC_UNSIGNED:
7357 if ( reloc->r_pcrel() )
7358 throw "pcrel and ARM64_RELOC_UNSIGNED not supported";
7359 if ( reloc->r_extern() )
7360 target.addend = contentValue;
7361 switch ( reloc->r_length() ) {
7362 case 0:
7363 case 1:
7364 throw "length < 2 and ARM64_RELOC_UNSIGNED not supported";
7365 case 2:
7366 parser.addFixups(src, ld::Fixup::kindStoreLittleEndian32, target);
7367 break;
7368 case 3:
7369 parser.addFixups(src, ld::Fixup::kindStoreLittleEndian64, target);
7370 break;
7371 }
7372 break;
7373 case ARM64_RELOC_BRANCH26:
7374 if ( ! reloc->r_pcrel() )
7375 throw "not pcrel and ARM64_RELOC_BRANCH26 not supported";
7376 if ( ! reloc->r_extern() )
7377 throw "r_extern == 0 and ARM64_RELOC_BRANCH26 not supported";
7378 if ( reloc->r_length() != 2 )
7379 throw "r_length != 2 and ARM64_RELOC_BRANCH26 not supported";
7380 if ( (target.name != NULL) && (strncmp(target.name, "___dtrace_probe$", 16) == 0) ) {
7381 parser.addFixup(src, ld::Fixup::k1of1, ld::Fixup::kindStoreARM64DtraceCallSiteNop, false, target.name);
7382 parser.addDtraceExtraInfos(src, &target.name[16]);
7383 }
7384 else if ( (target.name != NULL) && (strncmp(target.name, "___dtrace_isenabled$", 20) == 0) ) {
7385 parser.addFixup(src, ld::Fixup::k1of1, ld::Fixup::kindStoreARM64DtraceIsEnableSiteClear, false, target.name);
7386 parser.addDtraceExtraInfos(src, &target.name[20]);
7387 }
7388 else {
7389 target.addend = prefixRelocAddend;
7390 instruction = contentValue;
7391 encodedAddend = (instruction & 0x03FFFFFF) << 2;
7392 if ( encodedAddend != 0 ) {
7393 if ( prefixRelocAddend == 0 ) {
7394 warning("branch26 instruction at 0x%08X has embedded addend. ARM64_RELOC_ADDEND should be used instead", reloc->r_address());
7395 target.addend = encodedAddend;
7396 }
7397 else {
7398 throwf("branch26 instruction at 0x%08X has embedded addend and ARM64_RELOC_ADDEND also used", reloc->r_address());
7399 }
7400 }
7401 parser.addFixups(src, ld::Fixup::kindStoreARM64Branch26, target);
7402 }
7403 break;
7404 case ARM64_RELOC_PAGE21:
7405 if ( ! reloc->r_pcrel() )
7406 throw "not pcrel and ARM64_RELOC_PAGE21 not supported";
7407 if ( ! reloc->r_extern() )
7408 throw "r_extern == 0 and ARM64_RELOC_PAGE21 not supported";
7409 if ( reloc->r_length() != 2 )
7410 throw "length != 2 and ARM64_RELOC_PAGE21 not supported";
7411 target.addend = prefixRelocAddend;
7412 instruction = contentValue;
7413 encodedAddend = ((instruction & 0x60000000) >> 29) | ((instruction & 0x01FFFFE0) >> 3);
7414 encodedAddend *= 4096; // internally addend is in bytes, so scale
7415 if ( encodedAddend != 0 ) {
7416 if ( prefixRelocAddend == 0 ) {
7417 warning("adrp instruction at 0x%08X has embedded addend. ARM64_RELOC_ADDEND should be used instead", reloc->r_address());
7418 target.addend = encodedAddend;
7419 }
7420 else {
7421 throwf("adrp instruction at 0x%08X has embedded addend and ARM64_RELOC_ADDEND also used", reloc->r_address());
7422 }
7423 }
7424 parser.addFixups(src, ld::Fixup::kindStoreARM64Page21, target);
7425 break;
7426 case ARM64_RELOC_PAGEOFF12:
7427 if ( reloc->r_pcrel() )
7428 throw "pcrel and ARM64_RELOC_PAGEOFF12 not supported";
7429 if ( ! reloc->r_extern() )
7430 throw "r_extern == 0 and ARM64_RELOC_PAGEOFF12 not supported";
7431 if ( reloc->r_length() != 2 )
7432 throw "length != 2 and ARM64_RELOC_PAGEOFF12 not supported";
7433 target.addend = prefixRelocAddend;
7434 instruction = contentValue;
7435 encodedAddend = ((instruction & 0x003FFC00) >> 10);
7436 // internally addend is in bytes. Some instructions have an implicit scale factor
7437 if ( (instruction & 0x3B000000) == 0x39000000 ) {
7438 switch ( instruction & 0xC0000000 ) {
7439 case 0x00000000:
7440 break;
7441 case 0x40000000:
7442 encodedAddend *= 2;
7443 break;
7444 case 0x80000000:
7445 encodedAddend *= 4;
7446 break;
7447 case 0xC0000000:
7448 encodedAddend *= 8;
7449 break;
7450 }
7451 }
7452 if ( encodedAddend != 0 ) {
7453 if ( prefixRelocAddend == 0 ) {
7454 warning("pageoff12 instruction at 0x%08X has embedded addend. ARM64_RELOC_ADDEND should be used instead", reloc->r_address());
7455 target.addend = encodedAddend;
7456 }
7457 else {
7458 throwf("pageoff12 instruction at 0x%08X has embedded addend and ARM64_RELOC_ADDEND also used", reloc->r_address());
7459 }
7460 }
7461 parser.addFixups(src, ld::Fixup::kindStoreARM64PageOff12, target);
7462 break;
7463 case ARM64_RELOC_GOT_LOAD_PAGE21:
7464 if ( ! reloc->r_pcrel() )
7465 throw "not pcrel and ARM64_RELOC_GOT_LOAD_PAGE21 not supported";
7466 if ( ! reloc->r_extern() )
7467 throw "r_extern == 0 and ARM64_RELOC_GOT_LOAD_PAGE21 not supported";
7468 if ( reloc->r_length() != 2 )
7469 throw "length != 2 and ARM64_RELOC_GOT_LOAD_PAGE21 not supported";
7470 if ( prefixRelocAddend != 0 )
7471 throw "ARM64_RELOC_ADDEND followed by ARM64_RELOC_GOT_LOAD_PAGE21 not supported";
7472 instruction = contentValue;
7473 target.addend = ((instruction & 0x60000000) >> 29) | ((instruction & 0x01FFFFE0) >> 3);
7474 if ( target.addend != 0 )
7475 throw "non-zero addend with ARM64_RELOC_GOT_LOAD_PAGE21 is not supported";
7476 parser.addFixups(src, ld::Fixup::kindStoreARM64GOTLoadPage21, target);
7477 break;
7478 case ARM64_RELOC_GOT_LOAD_PAGEOFF12:
7479 if ( reloc->r_pcrel() )
7480 throw "pcrel and ARM64_RELOC_GOT_LOAD_PAGEOFF12 not supported";
7481 if ( ! reloc->r_extern() )
7482 throw "r_extern == 0 and ARM64_RELOC_GOT_LOAD_PAGEOFF12 not supported";
7483 if ( reloc->r_length() != 2 )
7484 throw "length != 2 and ARM64_RELOC_GOT_LOAD_PAGEOFF12 not supported";
7485 if ( prefixRelocAddend != 0 )
7486 throw "ARM64_RELOC_ADDEND followed by ARM64_RELOC_GOT_LOAD_PAGEOFF12 not supported";
7487 instruction = contentValue;
7488 target.addend = ((instruction & 0x003FFC00) >> 10);
7489 parser.addFixups(src, ld::Fixup::kindStoreARM64GOTLoadPageOff12, target);
7490 break;
7491 case ARM64_RELOC_TLVP_LOAD_PAGE21:
7492 if ( ! reloc->r_pcrel() )
7493 throw "not pcrel and ARM64_RELOC_TLVP_LOAD_PAGE21 not supported";
7494 if ( ! reloc->r_extern() )
7495 throw "r_extern == 0 and ARM64_RELOC_TLVP_LOAD_PAGE21 not supported";
7496 if ( reloc->r_length() != 2 )
7497 throw "length != 2 and ARM64_RELOC_TLVP_LOAD_PAGE21 not supported";
7498 if ( prefixRelocAddend != 0 )
7499 throw "ARM64_RELOC_ADDEND followed by ARM64_RELOC_TLVP_LOAD_PAGE21 not supported";
7500 instruction = contentValue;
7501 target.addend = ((instruction & 0x60000000) >> 29) | ((instruction & 0x01FFFFE0) >> 3);
7502 if ( target.addend != 0 )
7503 throw "non-zero addend with ARM64_RELOC_GOT_LOAD_PAGE21 is not supported";
7504 parser.addFixups(src, ld::Fixup::kindStoreARM64TLVPLoadPage21, target);
7505 break;
7506 case ARM64_RELOC_TLVP_LOAD_PAGEOFF12:
7507 if ( reloc->r_pcrel() )
7508 throw "pcrel and ARM64_RELOC_TLVP_LOAD_PAGEOFF12 not supported";
7509 if ( ! reloc->r_extern() )
7510 throw "r_extern == 0 and ARM64_RELOC_TLVP_LOAD_PAGEOFF12 not supported";
7511 if ( reloc->r_length() != 2 )
7512 throw "length != 2 and ARM64_RELOC_TLVP_LOAD_PAGEOFF12 not supported";
7513 if ( prefixRelocAddend != 0 )
7514 throw "ARM64_RELOC_ADDEND followed by ARM64_RELOC_TLVP_LOAD_PAGEOFF12 not supported";
7515 instruction = contentValue;
7516 target.addend = ((instruction & 0x003FFC00) >> 10);
7517 parser.addFixups(src, ld::Fixup::kindStoreARM64TLVPLoadPageOff12, target);
7518 break;
7519 case ARM64_RELOC_SUBTRACTOR:
7520 if ( reloc->r_pcrel() )
7521 throw "ARM64_RELOC_SUBTRACTOR cannot be pc-relative";
7522 if ( reloc->r_length() < 2 )
7523 throw "ARM64_RELOC_SUBTRACTOR must have r_length of 2 or 3";
7524 if ( !reloc->r_extern() )
7525 throw "ARM64_RELOC_SUBTRACTOR must have r_extern=1";
7526 if ( nextReloc->r_type() != ARM64_RELOC_UNSIGNED )
7527 throw "ARM64_RELOC_SUBTRACTOR must be followed by ARM64_RELOC_UNSIGNED";
7528 if ( prefixRelocAddend != 0 )
7529 throw "ARM64_RELOC_ADDEND followed by ARM64_RELOC_SUBTRACTOR not supported";
7530 result = true;
7531 if ( nextReloc->r_pcrel() )
7532 throw "ARM64_RELOC_UNSIGNED following a ARM64_RELOC_SUBTRACTOR cannot be pc-relative";
7533 if ( nextReloc->r_length() != reloc->r_length() )
7534 throw "ARM64_RELOC_UNSIGNED following a ARM64_RELOC_SUBTRACTOR must have same r_length";
7535 if ( nextReloc->r_extern() ) {
7536 const macho_nlist<P>& sym = parser.symbolFromIndex(nextReloc->r_symbolnum());
7537 // use direct reference for local symbols
7538 if ( ((sym.n_type() & N_TYPE) == N_SECT) && (((sym.n_type() & N_EXT) == 0) || (parser.nameFromSymbol(sym)[0] == 'L')) ) {
7539 parser.findTargetFromAddressAndSectionNum(sym.n_value(), sym.n_sect(), toTarget);
7540 toTarget.addend = contentValue;
7541 useDirectBinding = true;
7542 }
7543 else {
7544 toTarget.name = parser.nameFromSymbol(sym);
7545 toTarget.weakImport = parser.weakImportFromSymbol(sym);
7546 toTarget.addend = contentValue;
7547 useDirectBinding = false;
7548 }
7549 }
7550 else {
7551 parser.findTargetFromAddressAndSectionNum(contentValue, nextReloc->r_symbolnum(), toTarget);
7552 useDirectBinding = (toTarget.atom->scope() == ld::Atom::scopeTranslationUnit);
7553 }
7554 if ( useDirectBinding ) {
7555 if ( (toTarget.atom->combine() == ld::Atom::combineByNameAndContent) || (toTarget.atom->combine() == ld::Atom::combineByNameAndReferences) )
7556 parser.addFixup(src, ld::Fixup::k1of4, ld::Fixup::kindSetTargetAddress, ld::Fixup::bindingByContentBound, toTarget.atom);
7557 else
7558 parser.addFixup(src, ld::Fixup::k1of4, ld::Fixup::kindSetTargetAddress, toTarget.atom);
7559 }
7560 else
7561 parser.addFixup(src, ld::Fixup::k1of4, ld::Fixup::kindSetTargetAddress, toTarget.weakImport, toTarget.name);
7562 parser.addFixup(src, ld::Fixup::k2of4, ld::Fixup::kindAddAddend, toTarget.addend);
7563 if ( target.atom == NULL )
7564 parser.addFixup(src, ld::Fixup::k3of4, ld::Fixup::kindSubtractTargetAddress, false, target.name);
7565 else
7566 parser.addFixup(src, ld::Fixup::k3of4, ld::Fixup::kindSubtractTargetAddress, target.atom);
7567 if ( reloc->r_length() == 2 )
7568 parser.addFixup(src, ld::Fixup::k4of4, ld::Fixup::kindStoreLittleEndian32);
7569 else
7570 parser.addFixup(src, ld::Fixup::k4of4, ld::Fixup::kindStoreLittleEndian64);
7571 break;
7572 case ARM64_RELOC_POINTER_TO_GOT:
7573 if ( ! reloc->r_extern() )
7574 throw "r_extern == 0 and ARM64_RELOC_POINTER_TO_GOT not supported";
7575 if ( prefixRelocAddend != 0 )
7576 throw "ARM64_RELOC_ADDEND followed by ARM64_RELOC_POINTER_TO_GOT not supported";
7577 if ( reloc->r_pcrel() ) {
7578 if ( reloc->r_length() != 2 )
7579 throw "r_length != 2 and r_extern = 1 and ARM64_RELOC_POINTER_TO_GOT not supported";
7580 parser.addFixups(src, ld::Fixup::kindStoreARM64PCRelToGOT, target);
7581 }
7582 else {
7583 if ( reloc->r_length() != 3 )
7584 throw "r_length != 3 and r_extern = 0 and ARM64_RELOC_POINTER_TO_GOT not supported";
7585 parser.addFixups(src, ld::Fixup::kindStoreARM64PointerToGOT, target);
7586 }
7587 break;
7588 #if SUPPORT_ARCH_arm64e
7589 case ARM64_RELOC_AUTHENTICATED_POINTER: {
7590 if ( reloc->r_pcrel() )
7591 throw "pcrel and ARM64_RELOC_AUTHENTICATED_POINTER not supported";
7592 if ( ! reloc->r_extern() )
7593 throw "r_extern == 0 and ARM64_RELOC_AUTHENTICATED_POINTER not supported";
7594 // An authenticated pointer is:
7595 // {
7596 // int32_t addend;
7597 // uint16_t diversityData;
7598 // uint16_t hasAddressDiversity : 1;
7599 // uint16_t key : 2;
7600 // uint16_t zeroes : 11;
7601 // uint16_t zero : 1;
7602 // uint16_t authenticated : 1;
7603 // }
7604 target.addend = (int32_t)(contentValue & 0xFFFFFFFF);
7605 if (parser._supportsAuthenticatedPointers) {
7606 target.authData.discriminator = (uint16_t)(contentValue >> 32);
7607 target.authData.hasAddressDiversity = (contentValue & (1ULL << 48)) != 0;
7608 target.authData.key = (ld::Fixup::AuthData::ptrauth_key)((contentValue >> 49) & 0x3);
7609 } else {
7610 static bool emittedWarning = false;
7611 if (!emittedWarning) {
7612 emittedWarning = true;
7613 warning("stripping authenticated relocation as image uses -preload or -static");
7614 }
7615 }
7616 bool isAuthenticated = (contentValue & (1ULL << 63)) != 0;
7617 if (!isAuthenticated)
7618 throw "ARM64_RELOC_AUTHENTICATED_POINTER value must have authenticated bit set";
7619 switch ( reloc->r_length() ) {
7620 case 0:
7621 case 1:
7622 case 2:
7623 throw "length < 3 and ARM64_RELOC_AUTHENTICATED_POINTER not supported";
7624 case 3:
7625 if (parser._supportsAuthenticatedPointers)
7626 parser.addFixups(src, ld::Fixup::kindStoreLittleEndianAuth64, target);
7627 else
7628 parser.addFixups(src, ld::Fixup::kindStoreLittleEndian64, target);
7629 break;
7630 }
7631 break;
7632 }
7633 #endif
7634 default:
7635 throwf("unknown relocation type %d", reloc->r_type());
7636 }
7637 return result;
7638 }
7639 #endif
7640
7641
7642 template <typename A>
7643 bool ObjC1ClassSection<A>::addRelocFixup(class Parser<A>& parser, const macho_relocation_info<P>* reloc)
7644 {
7645 // inherited
7646 FixedSizeSection<A>::addRelocFixup(parser, reloc);
7647
7648 assert(0 && "needs template specialization");
7649 return false;
7650 }
7651
7652 template <>
7653 bool ObjC1ClassSection<x86>::addRelocFixup(class Parser<x86>& parser, const macho_relocation_info<x86::P>* reloc)
7654 {
7655 // if this is the reloc for the super class name string, add implicit reference to super class
7656 if ( ((reloc->r_address() & R_SCATTERED) == 0) && (reloc->r_type() == GENERIC_RELOC_VANILLA) ) {
7657 assert( reloc->r_length() == 2 );
7658 assert( ! reloc->r_pcrel() );
7659
7660 const macho_section<P>* sect = this->machoSection();
7661 Parser<x86>::SourceLocation src;
7662 uint32_t srcAddr = sect->addr() + reloc->r_address();
7663 src.atom = this->findAtomByAddress(srcAddr);
7664 src.offsetInAtom = srcAddr - src.atom->objectAddress();
7665 if ( src.offsetInAtom == 4 ) {
7666 Parser<x86>::TargetDesc stringTarget;
7667 const uint8_t* fixUpPtr = file().fileContent() + sect->offset() + reloc->r_address();
7668 uint32_t contentValue = LittleEndian::get32(*((uint32_t*)fixUpPtr));
7669 parser.findTargetFromAddressAndSectionNum(contentValue, reloc->r_symbolnum(), stringTarget);
7670
7671 assert(stringTarget.atom != NULL);
7672 assert(stringTarget.atom->contentType() == ld::Atom::typeCString);
7673 const char* superClassBaseName = (char*)stringTarget.atom->rawContentPointer();
7674 char* superClassName = new char[strlen(superClassBaseName) + 20];
7675 strcpy(superClassName, ".objc_class_name_");
7676 strcat(superClassName, superClassBaseName);
7677
7678 parser.addFixup(src, ld::Fixup::k1of1, ld::Fixup::kindSetTargetAddress, false, superClassName);
7679 }
7680 }
7681 // inherited
7682 return FixedSizeSection<x86>::addRelocFixup(parser, reloc);
7683 }
7684
7685
7686
7687 template <typename A>
7688 bool Objc1ClassReferences<A>::addRelocFixup(class Parser<A>& parser, const macho_relocation_info<P>* reloc)
7689 {
7690 // inherited
7691 PointerToCStringSection<A>::addRelocFixup(parser, reloc);
7692
7693 assert(0 && "needs template specialization");
7694 return false;
7695 }
7696
7697
7698
7699 template <>
7700 bool Objc1ClassReferences<x86>::addRelocFixup(class Parser<x86>& parser, const macho_relocation_info<x86::P>* reloc)
7701 {
7702 // add implict class refs, fixups not usable yet, so look at relocations
7703 assert( (reloc->r_address() & R_SCATTERED) == 0 );
7704 assert( reloc->r_type() == GENERIC_RELOC_VANILLA );
7705 assert( reloc->r_length() == 2 );
7706 assert( ! reloc->r_pcrel() );
7707
7708 const macho_section<P>* sect = this->machoSection();
7709 Parser<x86>::SourceLocation src;
7710 uint32_t srcAddr = sect->addr() + reloc->r_address();
7711 src.atom = this->findAtomByAddress(srcAddr);
7712 src.offsetInAtom = srcAddr - src.atom->objectAddress();
7713 Parser<x86>::TargetDesc stringTarget;
7714 const uint8_t* fixUpPtr = file().fileContent() + sect->offset() + reloc->r_address();
7715 uint32_t contentValue = LittleEndian::get32(*((uint32_t*)fixUpPtr));
7716 parser.findTargetFromAddressAndSectionNum(contentValue, reloc->r_symbolnum(), stringTarget);
7717
7718 assert(stringTarget.atom != NULL);
7719 assert(stringTarget.atom->contentType() == ld::Atom::typeCString);
7720 const char* baseClassName = (char*)stringTarget.atom->rawContentPointer();
7721 char* objcClassName = new char[strlen(baseClassName) + 20];
7722 strcpy(objcClassName, ".objc_class_name_");
7723 strcat(objcClassName, baseClassName);
7724
7725 parser.addFixup(src, ld::Fixup::k1of1, ld::Fixup::kindSetTargetAddress, false, objcClassName);
7726
7727 // inherited
7728 return PointerToCStringSection<x86>::addRelocFixup(parser, reloc);
7729 }
7730
7731 #if SUPPORT_ARCH_arm64
7732 template <>
7733 void Section<arm64>::addLOH(class Parser<arm64>& parser, int kind, int count, const uint64_t addrs[]) {
7734 switch (kind) {
7735 case LOH_ARM64_ADRP_ADRP:
7736 case LOH_ARM64_ADRP_LDR:
7737 case LOH_ARM64_ADRP_ADD:
7738 case LOH_ARM64_ADRP_LDR_GOT:
7739 if ( count != 2 )
7740 warning("arm64 Linker Optimiztion Hint %d has wrong number of arguments", kind);
7741 break;
7742 case LOH_ARM64_ADRP_ADD_LDR:
7743 case LOH_ARM64_ADRP_LDR_GOT_LDR:
7744 case LOH_ARM64_ADRP_ADD_STR:
7745 case LOH_ARM64_ADRP_LDR_GOT_STR:
7746 if ( count != 3 )
7747 warning("arm64 Linker Optimiztion Hint %d has wrong number of arguments", kind);
7748 }
7749
7750 // pick lowest address in tuple for use as offsetInAtom
7751 uint64_t lowestAddress = addrs[0];
7752 for(int i=1; i < count; ++i) {
7753 if ( addrs[i] < lowestAddress )
7754 lowestAddress = addrs[i];
7755 }
7756 // verify all other address are in same atom
7757 Atom<arm64>* inAtom = parser.findAtomByAddress(lowestAddress);
7758 const uint64_t atomStartAddr = inAtom->objectAddress();
7759 const uint64_t atomEndAddr = atomStartAddr + inAtom->size();
7760 for(int i=0; i < count; ++i) {
7761 if ( (addrs[i] < atomStartAddr) || (addrs[i] >= atomEndAddr) ) {
7762 warning("arm64 Linker Optimiztion Hint addresses are not in same atom: 0x%08llX and 0x%08llX",
7763 lowestAddress, addrs[i]);
7764 return; // skip this LOH
7765 }
7766 if ( (addrs[i] & 0x3) != 0 ) {
7767 warning("arm64 Linker Optimiztion Hint address is not 4-byte aligned: 0x%08llX", addrs[i]);
7768 return; // skip this LOH
7769 }
7770 if ( (addrs[i] - lowestAddress) > 0xFFFF ) {
7771 if ( parser.verboseOptimizationHints() ) {
7772 warning("arm64 Linker Optimiztion Hint addresses are too far apart: 0x%08llX and 0x%08llX",
7773 lowestAddress, addrs[i]);
7774 }
7775 return; // skip this LOH
7776 }
7777 }
7778
7779 // encoded kind, count, and address deltas in 64-bit addend
7780 ld::Fixup::LOH_arm64 extra;
7781 extra.addend = 0;
7782 extra.info.kind = kind;
7783 extra.info.count = count-1;
7784 extra.info.delta1 = (addrs[0] - lowestAddress) >> 2;
7785 extra.info.delta2 = (count > 1) ? ((addrs[1] - lowestAddress) >> 2) : 0;
7786 extra.info.delta3 = (count > 2) ? ((addrs[2] - lowestAddress) >> 2) : 0;
7787 extra.info.delta4 = (count > 3) ? ((addrs[3] - lowestAddress) >> 2) : 0;
7788 typename Parser<arm64>::SourceLocation src(inAtom, lowestAddress- inAtom->objectAddress());
7789 parser.addFixup(src, ld::Fixup::k1of1, ld::Fixup::kindLinkerOptimizationHint, extra.addend);
7790 }
7791 #endif
7792
7793
7794 template <typename A>
7795 void Section<A>::addLOH(class Parser<A>& parser, int kind, int count, const uint64_t addrs[]) {
7796
7797 }
7798
7799 template <typename A>
7800 void Section<A>::makeFixups(class Parser<A>& parser, const struct Parser<A>::CFI_CU_InfoArrays&)
7801 {
7802 const macho_section<P>* sect = this->machoSection();
7803 if ( sect->reloff() + (sect->nreloc() * sizeof(macho_relocation_info<P>)) > parser.fileLength() )
7804 throwf("relocations for section %s/%s extends beyond end of file,", sect->segname(), Section<A>::makeSectionName(sect) );
7805 const macho_relocation_info<P>* relocs = (macho_relocation_info<P>*)(file().fileContent() + sect->reloff());
7806 const uint32_t relocCount = sect->nreloc();
7807 for (uint32_t r = 0; r < relocCount; ++r) {
7808 try {
7809 if ( this->addRelocFixup(parser, &relocs[r]) )
7810 ++r; // skip next
7811 }
7812 catch (const char* msg) {
7813 throwf("in section %s,%s reloc %u: %s", sect->segname(), Section<A>::makeSectionName(sect), r, msg);
7814 }
7815 }
7816
7817 // add follow-on fixups if .o file is missing .subsections_via_symbols
7818 if ( this->addFollowOnFixups() ) {
7819 Atom<A>* end = &_endAtoms[-1];
7820 for(Atom<A>* p = _beginAtoms; p < end; ++p) {
7821 typename Parser<A>::SourceLocation src(p, 0);
7822 Atom<A>* nextAtom = &p[1];
7823 parser.addFixup(src, ld::Fixup::k1of1, ld::Fixup::kindNoneFollowOn, nextAtom);
7824 }
7825 }
7826 else if ( this->type() == ld::Section::typeCode ) {
7827 // if FDE broke text not at a symbol, use followOn to keep code together
7828 Atom<A>* end = &_endAtoms[-1];
7829 for(Atom<A>* p = _beginAtoms; p < end; ++p) {
7830 typename Parser<A>::SourceLocation src(p, 0);
7831 Atom<A>* nextAtom = &p[1];
7832 if ( (p->symbolTableInclusion() == ld::Atom::symbolTableIn) && (nextAtom->symbolTableInclusion() == ld::Atom::symbolTableNotIn) ) {
7833 parser.addFixup(src, ld::Fixup::k1of1, ld::Fixup::kindNoneFollowOn, nextAtom);
7834 }
7835 }
7836 }
7837 if ( !this->_altEntries.empty() && !this->addFollowOnFixups() ) {
7838 if ( _altEntries.count(_beginAtoms) != 0 )
7839 warning("N_ALT_ENTRY bit set on first atom in section %s/%s", sect->segname(), Section<A>::makeSectionName(sect));
7840
7841 Atom<A>* end = &_endAtoms[-1];
7842 for(Atom<A>* p = _beginAtoms; p < end; ++p) {
7843 Atom<A>* nextAtom = &p[1];
7844 // <rdar://problem/22960070> support alt_entry aliases (alias process already added followOn, don't repeat)
7845 if ( (_altEntries.count(nextAtom) != 0) && (p->_objAddress != nextAtom->_objAddress) ) {
7846 typename Parser<A>::SourceLocation src(p, 0);
7847 parser.addFixup(src, ld::Fixup::k1of1, ld::Fixup::kindNoneFollowOn, nextAtom);
7848 typename Parser<A>::SourceLocation src2(nextAtom, 0);
7849 parser.addFixup(src2, ld::Fixup::k1of1, ld::Fixup::kindNoneGroupSubordinate, p);
7850 }
7851 }
7852 }
7853
7854 // <rdar://problem/9218847> track data-in-code
7855 if ( parser.hasDataInCodeLabels() && (this->type() == ld::Section::typeCode) ) {
7856 for (uint32_t i=0; i < parser.symbolCount(); ++i) {
7857 const macho_nlist<P>& sym = parser.symbolFromIndex(i);
7858 // ignore stabs
7859 if ( (sym.n_type() & N_STAB) != 0 )
7860 continue;
7861 // ignore non-definitions
7862 if ( (sym.n_type() & N_TYPE) != N_SECT )
7863 continue;
7864
7865 // 'L' labels do not denote atom breaks
7866 const char* symbolName = parser.nameFromSymbol(sym);
7867 if ( symbolName[0] == 'L' ) {
7868 if ( strncmp(symbolName, "L$start$", 8) == 0 ) {
7869 ld::Fixup::Kind kind = ld::Fixup::kindNone;
7870 if ( strncmp(&symbolName[8], "data$", 5) == 0 )
7871 kind = ld::Fixup::kindDataInCodeStartData;
7872 else if ( strncmp(&symbolName[8], "code$", 5) == 0 )
7873 kind = ld::Fixup::kindDataInCodeEnd;
7874 else if ( strncmp(&symbolName[8], "jt8$", 4) == 0 )
7875 kind = ld::Fixup::kindDataInCodeStartJT8;
7876 else if ( strncmp(&symbolName[8], "jt16$", 4) == 0 )
7877 kind = ld::Fixup::kindDataInCodeStartJT16;
7878 else if ( strncmp(&symbolName[8], "jt32$", 4) == 0 )
7879 kind = ld::Fixup::kindDataInCodeStartJT32;
7880 else if ( strncmp(&symbolName[8], "jta32$", 4) == 0 )
7881 kind = ld::Fixup::kindDataInCodeStartJTA32;
7882 else
7883 warning("unknown L$start$ label %s in file %s", symbolName, this->file().path());
7884 if ( kind != ld::Fixup::kindNone ) {
7885 Atom<A>* inAtom = parser.findAtomByAddress(sym.n_value());
7886 typename Parser<A>::SourceLocation src(inAtom, sym.n_value() - inAtom->objectAddress());
7887 parser.addFixup(src, ld::Fixup::k1of1, kind);
7888 }
7889 }
7890 }
7891 }
7892 }
7893
7894 // <rdar://problem/11150575> Handle LC_DATA_IN_CODE in object files
7895 if ( this->type() == ld::Section::typeCode ) {
7896 const pint_t startAddr = this->_machOSection->addr();
7897 const pint_t endAddr = startAddr + this->_machOSection->size();
7898 for ( const macho_data_in_code_entry<P>* p = parser.dataInCodeStart(); p != parser.dataInCodeEnd(); ++p ) {
7899 if ( (p->offset() >= startAddr) && (p->offset() < endAddr) ) {
7900 ld::Fixup::Kind kind = ld::Fixup::kindNone;
7901 switch ( p->kind() ) {
7902 case DICE_KIND_DATA:
7903 kind = ld::Fixup::kindDataInCodeStartData;
7904 break;
7905 case DICE_KIND_JUMP_TABLE8:
7906 kind = ld::Fixup::kindDataInCodeStartJT8;
7907 break;
7908 case DICE_KIND_JUMP_TABLE16:
7909 kind = ld::Fixup::kindDataInCodeStartJT16;
7910 break;
7911 case DICE_KIND_JUMP_TABLE32:
7912 kind = ld::Fixup::kindDataInCodeStartJT32;
7913 break;
7914 case DICE_KIND_ABS_JUMP_TABLE32:
7915 kind = ld::Fixup::kindDataInCodeStartJTA32;
7916 break;
7917 default:
7918 kind = ld::Fixup::kindDataInCodeStartData;
7919 warning("uknown LC_DATA_IN_CODE kind (%d) at offset 0x%08X", p->kind(), p->offset());
7920 break;
7921 }
7922 Atom<A>* inAtom = parser.findAtomByAddress(p->offset());
7923 typename Parser<A>::SourceLocation srcStart(inAtom, p->offset() - inAtom->objectAddress());
7924 parser.addFixup(srcStart, ld::Fixup::k1of1, kind);
7925 typename Parser<A>::SourceLocation srcEnd(inAtom, p->offset() + p->length() - inAtom->objectAddress());
7926 parser.addFixup(srcEnd, ld::Fixup::k1of1, ld::Fixup::kindDataInCodeEnd);
7927 }
7928 }
7929 }
7930
7931 // <rdar://problem/11945700> convert linker optimization hints into internal format
7932 if ( this->type() == ld::Section::typeCode && parser.hasOptimizationHints() ) {
7933 const pint_t startAddr = this->_machOSection->addr();
7934 const pint_t endAddr = startAddr + this->_machOSection->size();
7935 for (const uint8_t* p = parser.optimizationHintsStart(); p < parser.optimizationHintsEnd(); ) {
7936 uint64_t addrs[4];
7937 int32_t kind = read_uleb128(&p, parser.optimizationHintsEnd());
7938 if ( kind == 0 ) // padding at end of loh buffer
7939 break;
7940 if ( kind == -1 ) {
7941 warning("malformed uleb128 kind in LC_LINKER_OPTIMIZATION_HINTS");
7942 break;
7943 }
7944 int32_t count = read_uleb128(&p, parser.optimizationHintsEnd());
7945 if ( count == -1 ) {
7946 warning("malformed uleb128 count in LC_LINKER_OPTIMIZATION_HINTS");
7947 break;
7948 }
7949 if ( count > 3 ) {
7950 warning("address count > 3 in LC_LINKER_OPTIMIZATION_HINTS");
7951 break;
7952 }
7953 for (int32_t i=0; i < count; ++i) {
7954 addrs[i] = read_uleb128(&p, parser.optimizationHintsEnd());
7955 }
7956 if ( (startAddr <= addrs[0]) && (addrs[0] < endAddr) ) {
7957 this->addLOH(parser, kind, count, addrs);
7958 //fprintf(stderr, "kind=%d", kind);
7959 //for (int32_t i=0; i < count; ++i) {
7960 // fprintf(stderr, ", addr=0x%08llX", addrs[i]);
7961 //}
7962 //fprintf(stderr, "\n");
7963 }
7964 }
7965 }
7966
7967
7968 // add follow-on fixups for aliases
7969 if ( _hasAliases ) {
7970 for(Atom<A>* p = _beginAtoms; p < _endAtoms; ++p) {
7971 if ( p->isAlias() && ! this->addFollowOnFixups() ) {
7972 Atom<A>* targetOfAlias = &p[1];
7973 assert(p < &_endAtoms[-1]);
7974 assert(p->_objAddress == targetOfAlias->_objAddress);
7975 typename Parser<A>::SourceLocation src(p, 0);
7976 parser.addFixup(src, ld::Fixup::k1of1, ld::Fixup::kindNoneFollowOn, targetOfAlias);
7977 }
7978 }
7979 }
7980 }
7981
7982
7983
7984 //
7985 // main function used by linker to instantiate ld::Files
7986 //
7987 ld::relocatable::File* parse(const uint8_t* fileContent, uint64_t fileLength,
7988 const char* path, time_t modTime, ld::File::Ordinal ordinal, const ParserOptions& opts)
7989 {
7990 switch ( opts.architecture ) {
7991 #if SUPPORT_ARCH_x86_64
7992 case CPU_TYPE_X86_64:
7993 if ( mach_o::relocatable::Parser<x86_64>::validFile(fileContent) )
7994 return mach_o::relocatable::Parser<x86_64>::parse(fileContent, fileLength, path, modTime, ordinal, opts);
7995 break;
7996 #endif
7997 #if SUPPORT_ARCH_i386
7998 case CPU_TYPE_I386:
7999 if ( mach_o::relocatable::Parser<x86>::validFile(fileContent) )
8000 return mach_o::relocatable::Parser<x86>::parse(fileContent, fileLength, path, modTime, ordinal, opts);
8001 break;
8002 #endif
8003 #if SUPPORT_ARCH_arm_any
8004 case CPU_TYPE_ARM:
8005 if ( mach_o::relocatable::Parser<arm>::validFile(fileContent, opts.objSubtypeMustMatch, opts.subType) )
8006 return mach_o::relocatable::Parser<arm>::parse(fileContent, fileLength, path, modTime, ordinal, opts);
8007 break;
8008 #endif
8009 #if SUPPORT_ARCH_arm64
8010 case CPU_TYPE_ARM64:
8011 if ( mach_o::relocatable::Parser<arm64>::validFile(fileContent, opts.objSubtypeMustMatch, opts.subType) )
8012 return mach_o::relocatable::Parser<arm64>::parse(fileContent, fileLength, path, modTime, ordinal, opts);
8013 break;
8014 #endif
8015 }
8016 return NULL;
8017 }
8018
8019 //
8020 // used by archive reader to validate member object file
8021 //
8022 bool isObjectFile(const uint8_t* fileContent, uint64_t fileLength, const ParserOptions& opts)
8023 {
8024 switch ( opts.architecture ) {
8025 case CPU_TYPE_X86_64:
8026 return ( mach_o::relocatable::Parser<x86_64>::validFile(fileContent) );
8027 case CPU_TYPE_I386:
8028 return ( mach_o::relocatable::Parser<x86>::validFile(fileContent) );
8029 case CPU_TYPE_ARM:
8030 return ( mach_o::relocatable::Parser<arm>::validFile(fileContent, opts.objSubtypeMustMatch, opts.subType) );
8031 case CPU_TYPE_ARM64:
8032 return ( mach_o::relocatable::Parser<arm64>::validFile(fileContent, opts.objSubtypeMustMatch, opts.subType) );
8033 }
8034 return false;
8035 }
8036
8037 //
8038 // used by linker to infer architecture when no -arch is on command line
8039 //
8040 bool isObjectFile(const uint8_t* fileContent, cpu_type_t* result, cpu_subtype_t* subResult, ld::Platform* platform, uint32_t* minOsVers)
8041 {
8042 if ( mach_o::relocatable::Parser<x86_64>::validFile(fileContent) ) {
8043 *result = CPU_TYPE_X86_64;
8044 const macho_header<Pointer64<LittleEndian> >* header = (const macho_header<Pointer64<LittleEndian> >*)fileContent;
8045 *subResult = header->cpusubtype();
8046 *platform = Parser<x86_64>::findPlatform(header, minOsVers);
8047 return true;
8048 }
8049 if ( mach_o::relocatable::Parser<x86>::validFile(fileContent) ) {
8050 const macho_header<Pointer32<LittleEndian> >* header = (const macho_header<Pointer32<LittleEndian> >*)fileContent;
8051 *result = CPU_TYPE_I386;
8052 *subResult = CPU_SUBTYPE_X86_ALL;
8053 *platform = Parser<x86>::findPlatform(header, minOsVers);
8054 return true;
8055 }
8056 if ( mach_o::relocatable::Parser<arm>::validFile(fileContent, false, 0) ) {
8057 const macho_header<Pointer32<LittleEndian> >* header = (const macho_header<Pointer32<LittleEndian> >*)fileContent;
8058 *result = CPU_TYPE_ARM;
8059 *subResult = header->cpusubtype();
8060 *platform = Parser<arm>::findPlatform(header, minOsVers);
8061 return true;
8062 }
8063 if ( mach_o::relocatable::Parser<arm64>::validFile(fileContent, false, 0) ) {
8064 const macho_header<Pointer64<LittleEndian> >* header = (const macho_header<Pointer64<LittleEndian> >*)fileContent;
8065 *result = CPU_TYPE_ARM64;
8066 *subResult = header->cpusubtype();
8067 *platform = Parser<arm64>::findPlatform(header, minOsVers);
8068 return true;
8069 }
8070 return false;
8071 }
8072
8073 //
8074 // used by linker is error messages to describe bad .o file
8075 //
8076 const char* archName(const uint8_t* fileContent)
8077 {
8078 if ( mach_o::relocatable::Parser<x86_64>::validFile(fileContent) ) {
8079 return mach_o::relocatable::Parser<x86_64>::fileKind(fileContent);
8080 }
8081 if ( mach_o::relocatable::Parser<x86>::validFile(fileContent) ) {
8082 return mach_o::relocatable::Parser<x86>::fileKind(fileContent);
8083 }
8084 if ( mach_o::relocatable::Parser<arm>::validFile(fileContent, false, 0) ) {
8085 return mach_o::relocatable::Parser<arm>::fileKind(fileContent);
8086 }
8087 return NULL;
8088 }
8089
8090 //
8091 // Used by archive reader when -ObjC option is specified
8092 //
8093 bool hasObjC2Categories(const uint8_t* fileContent)
8094 {
8095 if ( mach_o::relocatable::Parser<x86_64>::validFile(fileContent) ) {
8096 return mach_o::relocatable::Parser<x86_64>::hasObjC2Categories(fileContent);
8097 }
8098 else if ( mach_o::relocatable::Parser<arm>::validFile(fileContent, false, 0) ) {
8099 return mach_o::relocatable::Parser<arm>::hasObjC2Categories(fileContent);
8100 }
8101 else if ( mach_o::relocatable::Parser<x86>::validFile(fileContent, false, 0) ) {
8102 return mach_o::relocatable::Parser<x86>::hasObjC2Categories(fileContent);
8103 }
8104 #if SUPPORT_ARCH_arm64
8105 else if ( mach_o::relocatable::Parser<arm64>::validFile(fileContent, false, 0) ) {
8106 return mach_o::relocatable::Parser<arm64>::hasObjC2Categories(fileContent);
8107 }
8108 #endif
8109 return false;
8110 }
8111
8112 //
8113 // Used by archive reader when -ObjC option is specified
8114 //
8115 bool hasObjC1Categories(const uint8_t* fileContent)
8116 {
8117 if ( mach_o::relocatable::Parser<x86>::validFile(fileContent, false, 0) ) {
8118 return mach_o::relocatable::Parser<x86>::hasObjC1Categories(fileContent);
8119 }
8120 return false;
8121 }
8122
8123 //
8124 // Used by bitcode obfuscator to get a list of non local symbols from object file
8125 //
8126 bool getNonLocalSymbols(const uint8_t* fileContent, std::vector<const char*> &syms)
8127 {
8128 if ( mach_o::relocatable::Parser<x86_64>::validFile(fileContent) ) {
8129 return mach_o::relocatable::Parser<x86_64>::getNonLocalSymbols(fileContent, syms);
8130 }
8131 else if ( mach_o::relocatable::Parser<arm>::validFile(fileContent, false, 0) ) {
8132 return mach_o::relocatable::Parser<arm>::getNonLocalSymbols(fileContent, syms);
8133 }
8134 else if ( mach_o::relocatable::Parser<x86>::validFile(fileContent, false, 0) ) {
8135 return mach_o::relocatable::Parser<x86>::getNonLocalSymbols(fileContent, syms);
8136 }
8137 #if SUPPORT_ARCH_arm64
8138 else if ( mach_o::relocatable::Parser<arm64>::validFile(fileContent, false, 0) ) {
8139 return mach_o::relocatable::Parser<arm64>::getNonLocalSymbols(fileContent, syms);
8140 }
8141 #endif
8142 return false;
8143 }
8144
8145
8146
8147 } // namespace relocatable
8148 } // namespace mach_o
8149
8150