]> git.saurik.com Git - apple/ld64.git/blob - src/ObjectFile.h
69aeac62f871d0c09a31f16ee2ef8e18cacf4214
[apple/ld64.git] / src / ObjectFile.h
1 /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-
2 *
3 * Copyright (c) 2005-2006 Apple Computer, 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
33
34
35 //
36 // These classes represent the abstract Atoms and References that are the basis of the linker.
37 // An Atom and a Reference correspond to a Node and Edge in graph theory.
38 //
39 // A Reader is a class which parses an object file and presents it as Atoms and References.
40 // All linking operations are done on Atoms and References. This makes the linker file
41 // format independent.
42 //
43 // A Writer takes a vector of Atoms with all References resolved and produces an executable file.
44 //
45 //
46
47
48 namespace ObjectFile {
49
50
51 struct LineInfo
52 {
53 uint32_t atomOffset;
54 const char* fileName;
55 uint32_t lineNumber;
56 };
57
58
59 class ReaderOptions
60 {
61 public:
62 ReaderOptions() : fFullyLoadArchives(false), fLoadObjcClassesInArchives(false), fFlatNamespace(false),
63 fForFinalLinkedImage(false), fWhyLoad(false), fDebugInfoStripping(kDebugInfoFull),
64 fTraceDylibs(false), fTraceIndirectDylibs(false), fTraceArchives(false), fTraceOutputFile(NULL) {}
65 enum DebugInfoStripping { kDebugInfoNone, kDebugInfoMinimal, kDebugInfoFull };
66
67 bool fFullyLoadArchives;
68 bool fLoadObjcClassesInArchives;
69 bool fFlatNamespace;
70 bool fForFinalLinkedImage;
71 bool fWhyLoad;
72 DebugInfoStripping fDebugInfoStripping;
73 bool fTraceDylibs;
74 bool fTraceIndirectDylibs;
75 bool fTraceArchives;
76 const char* fTraceOutputFile;
77 };
78
79
80 class Reader
81 {
82 public:
83 enum DebugInfoKind { kDebugInfoNone=0, kDebugInfoStabs=1, kDebugInfoDwarf=2, kDebugInfoStabsUUID=3 };
84 struct Stab
85 {
86 class Atom* atom;
87 uint8_t type;
88 uint8_t other;
89 uint16_t desc;
90 uint32_t value;
91 const char* string;
92 };
93
94 static Reader* createReader(const char* path, const ReaderOptions& options);
95
96 virtual const char* getPath() = 0;
97 virtual time_t getModificationTime() = 0;
98 virtual DebugInfoKind getDebugInfoKind() = 0;
99 virtual std::vector<class Atom*>& getAtoms() = 0;
100 virtual std::vector<class Atom*>* getJustInTimeAtomsFor(const char* name) = 0;
101 virtual std::vector<Stab>* getStabs() = 0;
102 unsigned int getSortOrder() const { return fSortOrder; }
103 void setSortOrder(unsigned int order) { fSortOrder=order; }
104
105 // For Dynamic Libraries only
106 virtual const char* getInstallPath() { return NULL; }
107 virtual uint32_t getTimestamp() { return 0; }
108 virtual uint32_t getCurrentVersion() { return 0; }
109 virtual uint32_t getCompatibilityVersion() { return 0; }
110 virtual std::vector<const char*>* getDependentLibraryPaths() { return NULL; }
111 virtual bool reExports(Reader*) { return false; }
112 virtual const char* parentUmbrella() { return NULL; }
113 virtual std::vector<const char*>* getAllowableClients() { return NULL; }
114
115 protected:
116 Reader() : fSortOrder(0) {}
117 virtual ~Reader() {}
118
119 unsigned int fSortOrder;
120 };
121
122 class Segment
123 {
124 public:
125 virtual const char* getName() const = 0;
126 virtual bool isContentReadable() const = 0;
127 virtual bool isContentWritable() const = 0;
128 virtual bool isContentExecutable() const = 0;
129
130 uint64_t getBaseAddress() const { return fBaseAddress; }
131 void setBaseAddress(uint64_t addr) { fBaseAddress = addr; }
132 virtual bool hasFixedAddress() const { return false; }
133
134 protected:
135 Segment() : fBaseAddress(0) {}
136 virtual ~Segment() {}
137 uint64_t fBaseAddress;
138 };
139
140 class Reference;
141
142 class Section
143 {
144 public:
145 unsigned int getIndex() { return fIndex; }
146 uint64_t getBaseAddress() { return fBaseAddress; }
147 void setBaseAddress(uint64_t addr) { fBaseAddress = addr; }
148 void* fOther;
149
150 protected:
151 Section() : fOther(NULL), fBaseAddress(0), fIndex(0) {}
152 uint64_t fBaseAddress;
153 unsigned int fIndex;
154 };
155
156
157 struct Alignment
158 {
159 Alignment(int p2, int m=0) : powerOf2(p2), modulus(m) {}
160 uint8_t leadingZeros() const { return (modulus==0) ? powerOf2 : __builtin_clz(modulus); }
161 uint16_t powerOf2;
162 uint16_t modulus;
163 };
164
165 //
166 // An atom is the fundamental unit of linking. A C function or global variable is an atom.
167 // An atom has content and some attributes. The content of a function atom is the instructions
168 // that implement the function. The content of a global variable atom is its initial bits.
169 //
170 // Name:
171 // The name of an atom is the label name generated by the compiler. A C compiler names foo()
172 // as _foo. A C++ compiler names foo() as __Z3foov.
173 // The name refers to the first byte of the content. An atom cannot have multiple entry points.
174 // Such code is modeled as multiple atoms, each having a "follow on" reference to the next.
175 // A "follow on" reference is a contraint to the linker to the atoms must be laid out contiguously.
176 //
177 // Scope:
178 // An atom is in one of three scopes: translation-unit, linkage-unit, or global. These correspond
179 // to the C visibility of static, hidden, default.
180 //
181 // DefinitionKind:
182 // An atom is one of five defintion kinds:
183 // regular Most atoms.
184 // weak C++ compiler makes some functions weak if there might be multiple copies
185 // that the linker needs to coalesce.
186 // tentative A straggler from ancient C when the extern did not exist. "int foo;" is ambiguous.
187 // It could be a prototype or it could be a definition.
188 // external This is a "proxy" atom produced by a dylib reader. It has no content. It exists
189 // so that all References can be resolved.
190 // external-weak Same as external, but the definition in the dylib is weak.
191 //
192 // SymbolTableInclusion:
193 // An atom may or may not be in the symbol table in an object file.
194 // in Most atoms for functions or global data
195 // not-in Anonymous atoms such literal c-strings, or other compiler generated data
196 // in-never-strip Atom whose name the strip tool should never remove (e.g. REFERENCED_DYNAMICALLY in mach-o)
197 //
198 class Atom
199 {
200 public:
201 enum Scope { scopeTranslationUnit, scopeLinkageUnit, scopeGlobal };
202 enum DefinitionKind { kRegularDefinition, kWeakDefinition, kTentativeDefinition, kExternalDefinition, kExternalWeakDefinition };
203 enum SymbolTableInclusion { kSymbolTableNotIn, kSymbolTableIn, kSymbolTableInAndNeverStrip, kSymbolTableInAsAbsolute };
204
205 virtual Reader* getFile() const = 0;
206 virtual bool getTranslationUnitSource(const char** dir, const char** name) const = 0;
207 virtual const char* getName() const = 0;
208 virtual const char* getDisplayName() const = 0;
209 virtual Scope getScope() const = 0;
210 virtual DefinitionKind getDefinitionKind() const = 0;
211 virtual SymbolTableInclusion getSymbolTableInclusion() const = 0;
212 virtual bool dontDeadStrip() const = 0;
213 virtual bool isZeroFill() const = 0;
214 virtual uint64_t getSize() const = 0;
215 virtual std::vector<ObjectFile::Reference*>& getReferences() const = 0;
216 virtual bool mustRemainInSection() const = 0;
217 virtual const char* getSectionName() const = 0;
218 virtual Segment& getSegment() const = 0;
219 virtual bool requiresFollowOnAtom() const = 0;
220 virtual Atom& getFollowOnAtom() const = 0;
221 virtual std::vector<LineInfo>* getLineInfo() const = 0;
222 virtual Alignment getAlignment() const = 0;
223 virtual void copyRawContent(uint8_t buffer[]) const = 0;
224 virtual void setScope(Scope) = 0;
225
226
227 uint64_t getSectionOffset() const { return fSectionOffset; }
228 uint64_t getSegmentOffset() const { return fSegmentOffset; }
229 uint64_t getAddress() const { return fSection->getBaseAddress() + fSectionOffset; }
230 unsigned int getSortOrder() const { return fSortOrder; }
231 class Section* getSection() const { return fSection; }
232
233 void setSegmentOffset(uint64_t offset) { fSegmentOffset = offset; }
234 void setSectionOffset(uint64_t offset) { fSectionOffset = offset; }
235 void setSection(class Section* sect) { fSection = sect; }
236 unsigned int setSortOrder(unsigned int order); // recursively sets follow-on atoms
237
238 protected:
239 Atom() : fSegmentOffset(0), fSectionOffset(0), fSortOrder(0), fSection(NULL) {}
240 virtual ~Atom() {}
241
242 uint64_t fSegmentOffset;
243 uint64_t fSectionOffset;
244 unsigned int fSortOrder;
245 class Section* fSection;
246 };
247
248
249
250 // recursively sets follow-on atoms
251 inline unsigned int Atom::setSortOrder(unsigned int order)
252 {
253 if ( this->requiresFollowOnAtom() ) {
254 fSortOrder = order;
255 return this->getFollowOnAtom().setSortOrder(order+1);
256 }
257 else {
258 fSortOrder = order;
259 return (order + 1);
260 }
261 }
262
263
264
265 //
266 // A Reference is a directed edge to another Atom. When an instruction in
267 // the content of an Atom refers to another Atom, that is represented by a
268 // Reference.
269 //
270 // There are two kinds of references: direct and by-name. With a direct Reference,
271 // the target is bound by the Reader that created it. For instance a reference to a
272 // static would produce a direct reference. A by-name reference requires the linker
273 // to find the target Atom with the required name in order to be bound.
274 //
275 // For a link to succeed all References must be bound.
276 //
277 // A Reference has an optional "from" target. This is used when the content to fix-up
278 // is the difference of two Atom address. For instance, if a pointer sized data Atom
279 // is to contain A - B, then the Atom would have on Reference with a target of "A" and
280 // a from-target of "B".
281 //
282 // A Reference also has a fix-up-offset. This is the offset into the content of the
283 // Atom holding the reference where the fix-up (relocation) will be applied.
284 //
285 //
286 //
287 class Reference
288 {
289 public:
290
291 virtual bool isTargetUnbound() const = 0;
292 virtual bool isFromTargetUnbound() const = 0;
293 virtual uint8_t getKind() const = 0;
294 virtual uint64_t getFixUpOffset() const = 0;
295 virtual const char* getTargetName() const = 0;
296 virtual Atom& getTarget() const = 0;
297 virtual uint64_t getTargetOffset() const = 0;
298 virtual bool hasFromTarget() const = 0;
299 virtual Atom& getFromTarget() const = 0;
300 virtual const char* getFromTargetName() const = 0;
301 virtual uint64_t getFromTargetOffset() const = 0;
302
303 virtual void setTarget(Atom&, uint64_t offset) = 0;
304 virtual void setFromTarget(Atom&) = 0;
305 virtual const char* getDescription() const = 0;
306
307 protected:
308 Reference() {}
309 virtual ~Reference() {}
310 };
311
312
313 }; // namespace ObjectFile
314
315
316 #endif // __OBJECTFILE__