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