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