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