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