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