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