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