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