]>
Commit | Line | Data |
---|---|---|
1 | /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*- | |
2 | * | |
3 | * Copyright (c) 2005-2007 Apple Inc. All rights reserved. | |
4 | * | |
5 | * @APPLE_LICENSE_HEADER_START@ | |
6 | * | |
7 | * This file contains Original Code and/or Modifications of Original Code | |
8 | * as defined in and that are subject to the Apple Public Source License | |
9 | * Version 2.0 (the 'License'). You may not use this file except in | |
10 | * compliance with the License. Please obtain a copy of the License at | |
11 | * http://www.opensource.apple.com/apsl/ and read it before using this | |
12 | * file. | |
13 | * | |
14 | * The Original Code and all software distributed under the License are | |
15 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
16 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, | |
17 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
18 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. | |
19 | * Please see the License for the specific language governing rights and | |
20 | * limitations under the License. | |
21 | * | |
22 | * @APPLE_LICENSE_HEADER_END@ | |
23 | */ | |
24 | ||
25 | ||
26 | #ifndef __OBJECTFILE__ | |
27 | #define __OBJECTFILE__ | |
28 | ||
29 | #include <stdint.h> | |
30 | #include <vector> | |
31 | #include <map> | |
32 | #include <set> | |
33 | ||
34 | ||
35 | ||
36 | // | |
37 | // These classes represent the abstract Atoms and References that are the basis of the linker. | |
38 | // An Atom and a Reference correspond to a Node and Edge in graph theory. | |
39 | // | |
40 | // A Reader is a class which parses an object file and presents it as Atoms and References. | |
41 | // All linking operations are done on Atoms and References. This makes the linker file | |
42 | // format independent. | |
43 | // | |
44 | // A Writer takes a vector of Atoms with all References resolved and produces an executable file. | |
45 | // | |
46 | // | |
47 | ||
48 | ||
49 | ||
50 | namespace ObjectFile { | |
51 | ||
52 | ||
53 | struct LineInfo | |
54 | { | |
55 | uint32_t atomOffset; | |
56 | const char* fileName; | |
57 | uint32_t lineNumber; | |
58 | }; | |
59 | ||
60 | ||
61 | class ReaderOptions | |
62 | { | |
63 | public: | |
64 | ReaderOptions() : fFullyLoadArchives(false), fLoadAllObjcObjectsFromArchives(false), fFlatNamespace(false), | |
65 | fLinkingMainExecutable(false), | |
66 | fForFinalLinkedImage(false), fNoEHLabels(false), fForStatic(false), fForDyld(false), fMakeTentativeDefinitionsReal(false), | |
67 | fWhyLoad(false), fRootSafe(false), fSetuidSafe(false),fDebugInfoStripping(kDebugInfoFull), | |
68 | fImplicitlyLinkPublicDylibs(true), | |
69 | fAddCompactUnwindEncoding(true), | |
70 | fWarnCompactUnwind(false), | |
71 | fRemoveDwarfUnwindIfCompactExists(false), | |
72 | fMakeCompressedDyldInfo(false), | |
73 | fAutoOrderInitializers(true), | |
74 | fLogObjectFiles(false), fLogAllFiles(false), | |
75 | fTraceDylibs(false), fTraceIndirectDylibs(false), fTraceArchives(false), | |
76 | fTraceOutputFile(NULL), fMacVersionMin(kMinMacVersionUnset), fIPhoneVersionMin(kMinIPhoneVersionUnset) {} | |
77 | enum DebugInfoStripping { kDebugInfoNone, kDebugInfoMinimal, kDebugInfoFull }; | |
78 | enum MacVersionMin { kMinMacVersionUnset, k10_1, k10_2, k10_3, k10_4, k10_5, k10_6 }; | |
79 | enum IPhoneVersionMin { kMinIPhoneVersionUnset, k2_0, k2_1, k2_2, k3_0 }; | |
80 | ||
81 | struct AliasPair { | |
82 | const char* realName; | |
83 | const char* alias; | |
84 | }; | |
85 | ||
86 | bool fFullyLoadArchives; | |
87 | bool fLoadAllObjcObjectsFromArchives; | |
88 | bool fFlatNamespace; | |
89 | bool fLinkingMainExecutable; | |
90 | bool fForFinalLinkedImage; | |
91 | bool fNoEHLabels; | |
92 | bool fForStatic; | |
93 | bool fForDyld; | |
94 | bool fMakeTentativeDefinitionsReal; | |
95 | bool fWhyLoad; | |
96 | bool fRootSafe; | |
97 | bool fSetuidSafe; | |
98 | DebugInfoStripping fDebugInfoStripping; | |
99 | bool fImplicitlyLinkPublicDylibs; | |
100 | bool fAddCompactUnwindEncoding; | |
101 | bool fWarnCompactUnwind; | |
102 | bool fRemoveDwarfUnwindIfCompactExists; | |
103 | bool fMakeCompressedDyldInfo; | |
104 | bool fAutoOrderInitializers; | |
105 | bool fLogObjectFiles; | |
106 | bool fLogAllFiles; | |
107 | bool fTraceDylibs; | |
108 | bool fTraceIndirectDylibs; | |
109 | bool fTraceArchives; | |
110 | const char* fTraceOutputFile; | |
111 | MacVersionMin fMacVersionMin; | |
112 | IPhoneVersionMin fIPhoneVersionMin; | |
113 | std::vector<AliasPair> fAliases; | |
114 | }; | |
115 | ||
116 | ||
117 | class Reader | |
118 | { | |
119 | public: | |
120 | enum DebugInfoKind { kDebugInfoNone=0, kDebugInfoStabs=1, kDebugInfoDwarf=2, kDebugInfoStabsUUID=3 }; | |
121 | struct Stab | |
122 | { | |
123 | class Atom* atom; | |
124 | uint8_t type; | |
125 | uint8_t other; | |
126 | uint16_t desc; | |
127 | uint32_t value; | |
128 | const char* string; | |
129 | }; | |
130 | enum ObjcConstraint { kObjcNone, kObjcRetainRelease, kObjcRetainReleaseOrGC, kObjcGC }; | |
131 | enum CpuConstraint { kCpuAny = 0 }; | |
132 | ||
133 | class DylibHander | |
134 | { | |
135 | public: | |
136 | virtual ~DylibHander() {} | |
137 | virtual Reader* findDylib(const char* installPath, const char* fromPath) = 0; | |
138 | }; | |
139 | ||
140 | ||
141 | static Reader* createReader(const char* path, const ReaderOptions& options); | |
142 | ||
143 | virtual const char* getPath() = 0; | |
144 | virtual time_t getModificationTime() = 0; | |
145 | virtual DebugInfoKind getDebugInfoKind() = 0; | |
146 | virtual std::vector<class Atom*>& getAtoms() = 0; | |
147 | virtual std::vector<class Atom*>* getJustInTimeAtomsFor(const char* name) = 0; | |
148 | virtual std::vector<Stab>* getStabs() = 0; | |
149 | virtual ObjcConstraint getObjCConstraint() { return kObjcNone; } | |
150 | virtual uint32_t updateCpuConstraint(uint32_t current) { return current; } | |
151 | virtual bool objcReplacementClasses() { return false; } | |
152 | ||
153 | // For relocatable object files only | |
154 | virtual bool canScatterAtoms() { return true; } | |
155 | virtual bool optimize(const std::vector<ObjectFile::Atom*>&, std::vector<ObjectFile::Atom*>&, | |
156 | std::vector<const char*>&, const std::set<ObjectFile::Atom*>&, | |
157 | std::vector<ObjectFile::Atom*>&, | |
158 | uint32_t, ObjectFile::Reader* writer, | |
159 | ObjectFile::Atom* entryPointAtom, | |
160 | const std::vector<const char*>& llvmOptions, | |
161 | bool allGlobalsAReDeadStripRoots, int okind, | |
162 | bool verbose, bool saveTemps, const char* outputFilePath, | |
163 | bool pie, bool allowTextRelocs) { return false; } | |
164 | virtual bool hasLongBranchStubs() { return false; } | |
165 | ||
166 | // For Dynamic Libraries only | |
167 | virtual const char* getInstallPath() { return NULL; } | |
168 | virtual uint32_t getTimestamp() { return 0; } | |
169 | virtual uint32_t getCurrentVersion() { return 0; } | |
170 | virtual uint32_t getCompatibilityVersion() { return 0; } | |
171 | virtual void processIndirectLibraries(DylibHander* handler) { } | |
172 | virtual void setExplicitlyLinked() { } | |
173 | virtual bool explicitlyLinked() { return false; } | |
174 | virtual bool implicitlyLinked() { return false; } | |
175 | virtual bool providedExportAtom() { return false; } | |
176 | virtual const char* parentUmbrella() { return NULL; } | |
177 | virtual std::vector<const char*>* getAllowableClients() { return NULL; } | |
178 | virtual bool hasWeakExternals() { return false; } | |
179 | virtual bool deadStrippable() { return false; } | |
180 | virtual bool isLazyLoadedDylib() { return false; } | |
181 | ||
182 | protected: | |
183 | Reader() {} | |
184 | virtual ~Reader() {} | |
185 | }; | |
186 | ||
187 | class Segment | |
188 | { | |
189 | public: | |
190 | virtual const char* getName() const = 0; | |
191 | virtual bool isContentReadable() const = 0; | |
192 | virtual bool isContentWritable() const = 0; | |
193 | virtual bool isContentExecutable() const = 0; | |
194 | ||
195 | uint64_t getBaseAddress() const { return fBaseAddress; } | |
196 | void setBaseAddress(uint64_t addr) { fBaseAddress = addr; } | |
197 | virtual bool hasFixedAddress() const { return false; } | |
198 | ||
199 | protected: | |
200 | Segment() : fBaseAddress(0) {} | |
201 | virtual ~Segment() {} | |
202 | uint64_t fBaseAddress; | |
203 | }; | |
204 | ||
205 | class Reference; | |
206 | ||
207 | class Section | |
208 | { | |
209 | public: | |
210 | unsigned int getIndex() { return fIndex; } | |
211 | uint64_t getBaseAddress() { return fBaseAddress; } | |
212 | void setBaseAddress(uint64_t addr) { fBaseAddress = addr; } | |
213 | void* fOther; | |
214 | ||
215 | protected: | |
216 | Section() : fOther(NULL), fBaseAddress(0), fIndex(0) {} | |
217 | uint64_t fBaseAddress; | |
218 | unsigned int fIndex; | |
219 | }; | |
220 | ||
221 | ||
222 | struct Alignment | |
223 | { | |
224 | Alignment(int p2, int m=0) : powerOf2(p2), modulus(m) {} | |
225 | uint8_t trailingZeros() const { return (modulus==0) ? powerOf2 : __builtin_ctz(modulus); } | |
226 | uint16_t powerOf2; | |
227 | uint16_t modulus; | |
228 | }; | |
229 | ||
230 | struct UnwindInfo | |
231 | { | |
232 | uint32_t startOffset; | |
233 | uint32_t unwindInfo; | |
234 | ||
235 | typedef UnwindInfo* iterator; | |
236 | ||
237 | }; | |
238 | ||
239 | ||
240 | // | |
241 | // An atom is the fundamental unit of linking. A C function or global variable is an atom. | |
242 | // An atom has content and some attributes. The content of a function atom is the instructions | |
243 | // that implement the function. The content of a global variable atom is its initial bits. | |
244 | // | |
245 | // Name: | |
246 | // The name of an atom is the label name generated by the compiler. A C compiler names foo() | |
247 | // as _foo. A C++ compiler names foo() as __Z3foov. | |
248 | // The name refers to the first byte of the content. An atom cannot have multiple entry points. | |
249 | // Such code is modeled as multiple atoms, each having a "follow on" reference to the next. | |
250 | // A "follow on" reference is a contraint to the linker to the atoms must be laid out contiguously. | |
251 | // | |
252 | // Scope: | |
253 | // An atom is in one of three scopes: translation-unit, linkage-unit, or global. These correspond | |
254 | // to the C visibility of static, hidden, default. | |
255 | // | |
256 | // DefinitionKind: | |
257 | // An atom is one of five defintion kinds: | |
258 | // regular Most atoms. | |
259 | // weak C++ compiler makes some functions weak if there might be multiple copies | |
260 | // that the linker needs to coalesce. | |
261 | // tentative A straggler from ancient C when the extern did not exist. "int foo;" is ambiguous. | |
262 | // It could be a prototype or it could be a definition. | |
263 | // external This is a "proxy" atom produced by a dylib reader. It has no content. It exists | |
264 | // so that all References can be resolved. | |
265 | // external-weak Same as external, but the definition in the dylib is weak. | |
266 | // | |
267 | // SymbolTableInclusion: | |
268 | // An atom may or may not be in the symbol table in an object file. | |
269 | // in Most atoms for functions or global data | |
270 | // not-in Anonymous atoms such literal c-strings, or other compiler generated data | |
271 | // in-never-strip Atom whose name the strip tool should never remove (e.g. REFERENCED_DYNAMICALLY in mach-o) | |
272 | // | |
273 | // Ordinal: | |
274 | // When a reader is created it is given a base ordinal number. All atoms created by the reader | |
275 | // should return a contiguous range of ordinal values that start at the base ordinal. The ordinal | |
276 | // values are used by the linker to sort the atom graph when producing the output file. | |
277 | // | |
278 | class Atom | |
279 | { | |
280 | public: | |
281 | enum Scope { scopeTranslationUnit, scopeLinkageUnit, scopeGlobal }; | |
282 | enum DefinitionKind { kRegularDefinition, kWeakDefinition, kTentativeDefinition, kExternalDefinition, kExternalWeakDefinition, kAbsoluteSymbol }; | |
283 | enum ContentType { kUnclassifiedType, kCStringType, kCFIType, kLSDAType }; | |
284 | enum SymbolTableInclusion { kSymbolTableNotIn, kSymbolTableIn, kSymbolTableInAndNeverStrip, kSymbolTableInAsAbsolute }; | |
285 | ||
286 | virtual Reader* getFile() const = 0; | |
287 | virtual bool getTranslationUnitSource(const char** dir, const char** name) const = 0; | |
288 | virtual const char* getName() const = 0; | |
289 | virtual const char* getDisplayName() const = 0; | |
290 | virtual Scope getScope() const = 0; | |
291 | virtual DefinitionKind getDefinitionKind() const = 0; | |
292 | virtual ContentType getContentType() const { return kUnclassifiedType; } | |
293 | virtual SymbolTableInclusion getSymbolTableInclusion() const = 0; | |
294 | virtual bool dontDeadStrip() const = 0; | |
295 | virtual bool isZeroFill() const = 0; | |
296 | virtual bool isThumb() const = 0; | |
297 | virtual uint64_t getSize() const = 0; | |
298 | virtual std::vector<ObjectFile::Reference*>& getReferences() const = 0; | |
299 | virtual bool mustRemainInSection() const = 0; | |
300 | virtual const char* getSectionName() const = 0; | |
301 | virtual Segment& getSegment() const = 0; | |
302 | virtual Atom& getFollowOnAtom() const = 0; | |
303 | virtual uint32_t getOrdinal() const = 0; | |
304 | virtual std::vector<LineInfo>* getLineInfo() const = 0; | |
305 | virtual Alignment getAlignment() const = 0; | |
306 | virtual void copyRawContent(uint8_t buffer[]) const = 0; | |
307 | virtual void setScope(Scope) = 0; | |
308 | virtual UnwindInfo::iterator beginUnwind() { return NULL; } | |
309 | virtual UnwindInfo::iterator endUnwind() { return NULL; } | |
310 | virtual Reference* getLSDA() { return NULL; } | |
311 | virtual Reference* getFDE() { return NULL; } | |
312 | virtual Atom* getPersonalityPointer() { return NULL; } | |
313 | ||
314 | uint64_t getSectionOffset() const { return fSectionOffset; } | |
315 | uint64_t getAddress() const { return fSection->getBaseAddress() + fSectionOffset; } | |
316 | class Section* getSection() const { return fSection; } | |
317 | ||
318 | virtual void setSectionOffset(uint64_t offset) { fSectionOffset = offset; } | |
319 | virtual void setSection(class Section* sect) { fSection = sect; } | |
320 | ||
321 | protected: | |
322 | Atom() : fSectionOffset(0), fSection(NULL) {} | |
323 | virtual ~Atom() {} | |
324 | ||
325 | uint64_t fSectionOffset; | |
326 | class Section* fSection; | |
327 | }; | |
328 | ||
329 | ||
330 | // | |
331 | // A Reference is a directed edge to another Atom. When an instruction in | |
332 | // the content of an Atom refers to another Atom, that is represented by a | |
333 | // Reference. | |
334 | // | |
335 | // There are two kinds of references: direct and by-name. With a direct Reference, | |
336 | // the target is bound by the Reader that created it. For instance a reference to a | |
337 | // static would produce a direct reference. A by-name reference requires the linker | |
338 | // to find the target Atom with the required name in order to be bound. | |
339 | // | |
340 | // For a link to succeed all References must be bound. | |
341 | // | |
342 | // A Reference has an optional "from" target. This is used when the content to fix-up | |
343 | // is the difference of two Atom address. For instance, if a pointer sized data Atom | |
344 | // is to contain A - B, then the Atom would have on Reference with a target of "A" and | |
345 | // a from-target of "B". | |
346 | // | |
347 | // A Reference also has a fix-up-offset. This is the offset into the content of the | |
348 | // Atom holding the reference where the fix-up (relocation) will be applied. | |
349 | // | |
350 | // | |
351 | // | |
352 | class Reference | |
353 | { | |
354 | public: | |
355 | enum TargetBinding { kUnboundByName, kBoundDirectly, kBoundByName, kDontBind }; | |
356 | ||
357 | virtual TargetBinding getTargetBinding() const = 0; | |
358 | virtual TargetBinding getFromTargetBinding() const = 0; | |
359 | virtual uint8_t getKind() const = 0; | |
360 | virtual uint64_t getFixUpOffset() const = 0; | |
361 | virtual const char* getTargetName() const = 0; | |
362 | virtual Atom& getTarget() const = 0; | |
363 | virtual uint64_t getTargetOffset() const = 0; | |
364 | virtual Atom& getFromTarget() const = 0; | |
365 | virtual const char* getFromTargetName() const = 0; | |
366 | virtual uint64_t getFromTargetOffset() const = 0; | |
367 | ||
368 | virtual void setTarget(Atom&, uint64_t offset) = 0; | |
369 | virtual void setFromTarget(Atom&) = 0; | |
370 | virtual const char* getDescription() const = 0; | |
371 | virtual bool isBranch() const { return false; } | |
372 | ||
373 | protected: | |
374 | Reference() {} | |
375 | virtual ~Reference() {} | |
376 | }; | |
377 | ||
378 | ||
379 | }; // namespace ObjectFile | |
380 | ||
381 | ||
382 | #endif // __OBJECTFILE__ |