]> git.saurik.com Git - apple/ld64.git/blob - src/ObjectFile.h
ld64-85.2.2.tar.gz
[apple/ld64.git] / src / ObjectFile.h
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
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
49 namespace ObjectFile {
50
51
52 struct LineInfo
53 {
54 uint32_t atomOffset;
55 const char* fileName;
56 uint32_t lineNumber;
57 };
58
59
60 class ReaderOptions
61 {
62 public:
63 ReaderOptions() : fFullyLoadArchives(false), fLoadAllObjcObjectsFromArchives(false), fFlatNamespace(false),
64 fLinkingMainExecutable(false), fSlowx86Stubs(false),
65 fForFinalLinkedImage(false), fForStatic(false), fForDyld(false), fMakeTentativeDefinitionsReal(false),
66 fWhyLoad(false), fRootSafe(false), fSetuidSafe(false),fDebugInfoStripping(kDebugInfoFull),
67 fImplicitlyLinkPublicDylibs(true), fLogObjectFiles(false), fLogAllFiles(false),
68 fTraceDylibs(false), fTraceIndirectDylibs(false), fTraceArchives(false),
69 fTraceOutputFile(NULL), fVersionMin(kMinUnset) {}
70 enum DebugInfoStripping { kDebugInfoNone, kDebugInfoMinimal, kDebugInfoFull };
71 enum VersionMin { kMinUnset, k10_1, k10_2, k10_3, k10_4, k10_5, k10_6 };
72
73 struct AliasPair {
74 const char* realName;
75 const char* alias;
76 };
77
78 bool fFullyLoadArchives;
79 bool fLoadAllObjcObjectsFromArchives;
80 bool fFlatNamespace;
81 bool fLinkingMainExecutable;
82 bool fSlowx86Stubs;
83 bool fForFinalLinkedImage;
84 bool fForStatic;
85 bool fForDyld;
86 bool fMakeTentativeDefinitionsReal;
87 bool fWhyLoad;
88 bool fRootSafe;
89 bool fSetuidSafe;
90 DebugInfoStripping fDebugInfoStripping;
91 bool fImplicitlyLinkPublicDylibs;
92 bool fLogObjectFiles;
93 bool fLogAllFiles;
94 bool fTraceDylibs;
95 bool fTraceIndirectDylibs;
96 bool fTraceArchives;
97 const char* fTraceOutputFile;
98 VersionMin fVersionMin;
99 std::vector<AliasPair> fAliases;
100 };
101
102
103 class Reader
104 {
105 public:
106 enum DebugInfoKind { kDebugInfoNone=0, kDebugInfoStabs=1, kDebugInfoDwarf=2, kDebugInfoStabsUUID=3 };
107 struct Stab
108 {
109 class Atom* atom;
110 uint8_t type;
111 uint8_t other;
112 uint16_t desc;
113 uint32_t value;
114 const char* string;
115 };
116 enum ObjcConstraint { kObjcNone, kObjcRetainRelease, kObjcRetainReleaseOrGC, kObjcGC };
117 enum CpuConstraint { kCpuAny = 0 };
118
119 class DylibHander
120 {
121 public:
122 virtual ~DylibHander() {}
123 virtual Reader* findDylib(const char* installPath, const char* fromPath) = 0;
124 };
125
126
127 static Reader* createReader(const char* path, const ReaderOptions& options);
128
129 virtual const char* getPath() = 0;
130 virtual time_t getModificationTime() = 0;
131 virtual DebugInfoKind getDebugInfoKind() = 0;
132 virtual std::vector<class Atom*>& getAtoms() = 0;
133 virtual std::vector<class Atom*>* getJustInTimeAtomsFor(const char* name) = 0;
134 virtual std::vector<Stab>* getStabs() = 0;
135 virtual ObjcConstraint getObjCConstraint() { return kObjcNone; }
136 virtual uint32_t updateCpuConstraint(uint32_t current) { return current; }
137 virtual bool objcReplacementClasses() { return false; }
138
139 // For relocatable object files only
140 virtual bool canScatterAtoms() { return true; }
141 virtual void optimize(std::vector<ObjectFile::Atom*>&, std::vector<ObjectFile::Atom*>&,
142 std::vector<const char*>&, uint32_t, ObjectFile::Reader* writer,
143 bool allGlobalsAReDeadStripRoots, int okind,
144 bool verbose, bool saveTemps, const char* outputFilePath,
145 bool pie, bool allowTextRelocs) { }
146 virtual bool hasLongBranchStubs() { return false; }
147
148 // For Dynamic Libraries only
149 virtual const char* getInstallPath() { return NULL; }
150 virtual uint32_t getTimestamp() { return 0; }
151 virtual uint32_t getCurrentVersion() { return 0; }
152 virtual uint32_t getCompatibilityVersion() { return 0; }
153 virtual void processIndirectLibraries(DylibHander* handler) { }
154 virtual void setExplicitlyLinked() { }
155 virtual bool explicitlyLinked() { return false; }
156 virtual bool implicitlyLinked() { return false; }
157 virtual bool providedExportAtom() { return false; }
158 virtual const char* parentUmbrella() { return NULL; }
159 virtual std::vector<const char*>* getAllowableClients() { return NULL; }
160 virtual bool hasWeakExternals() { return false; }
161 virtual bool isLazyLoadedDylib() { return false; }
162
163 protected:
164 Reader() {}
165 virtual ~Reader() {}
166 };
167
168 class Segment
169 {
170 public:
171 virtual const char* getName() const = 0;
172 virtual bool isContentReadable() const = 0;
173 virtual bool isContentWritable() const = 0;
174 virtual bool isContentExecutable() const = 0;
175
176 uint64_t getBaseAddress() const { return fBaseAddress; }
177 void setBaseAddress(uint64_t addr) { fBaseAddress = addr; }
178 virtual bool hasFixedAddress() const { return false; }
179
180 protected:
181 Segment() : fBaseAddress(0) {}
182 virtual ~Segment() {}
183 uint64_t fBaseAddress;
184 };
185
186 class Reference;
187
188 class Section
189 {
190 public:
191 unsigned int getIndex() { return fIndex; }
192 uint64_t getBaseAddress() { return fBaseAddress; }
193 void setBaseAddress(uint64_t addr) { fBaseAddress = addr; }
194 void* fOther;
195
196 protected:
197 Section() : fOther(NULL), fBaseAddress(0), fIndex(0) {}
198 uint64_t fBaseAddress;
199 unsigned int fIndex;
200 };
201
202
203 struct Alignment
204 {
205 Alignment(int p2, int m=0) : powerOf2(p2), modulus(m) {}
206 uint8_t trailingZeros() const { return (modulus==0) ? powerOf2 : __builtin_ctz(modulus); }
207 uint16_t powerOf2;
208 uint16_t modulus;
209 };
210
211 //
212 // An atom is the fundamental unit of linking. A C function or global variable is an atom.
213 // An atom has content and some attributes. The content of a function atom is the instructions
214 // that implement the function. The content of a global variable atom is its initial bits.
215 //
216 // Name:
217 // The name of an atom is the label name generated by the compiler. A C compiler names foo()
218 // as _foo. A C++ compiler names foo() as __Z3foov.
219 // The name refers to the first byte of the content. An atom cannot have multiple entry points.
220 // Such code is modeled as multiple atoms, each having a "follow on" reference to the next.
221 // A "follow on" reference is a contraint to the linker to the atoms must be laid out contiguously.
222 //
223 // Scope:
224 // An atom is in one of three scopes: translation-unit, linkage-unit, or global. These correspond
225 // to the C visibility of static, hidden, default.
226 //
227 // DefinitionKind:
228 // An atom is one of five defintion kinds:
229 // regular Most atoms.
230 // weak C++ compiler makes some functions weak if there might be multiple copies
231 // that the linker needs to coalesce.
232 // tentative A straggler from ancient C when the extern did not exist. "int foo;" is ambiguous.
233 // It could be a prototype or it could be a definition.
234 // external This is a "proxy" atom produced by a dylib reader. It has no content. It exists
235 // so that all References can be resolved.
236 // external-weak Same as external, but the definition in the dylib is weak.
237 //
238 // SymbolTableInclusion:
239 // An atom may or may not be in the symbol table in an object file.
240 // in Most atoms for functions or global data
241 // not-in Anonymous atoms such literal c-strings, or other compiler generated data
242 // in-never-strip Atom whose name the strip tool should never remove (e.g. REFERENCED_DYNAMICALLY in mach-o)
243 //
244 // Ordinal:
245 // When a reader is created it is given a base ordinal number. All atoms created by the reader
246 // should return a contiguous range of ordinal values that start at the base ordinal. The ordinal
247 // values are used by the linker to sort the atom graph when producing the output file.
248 //
249 class Atom
250 {
251 public:
252 enum Scope { scopeTranslationUnit, scopeLinkageUnit, scopeGlobal };
253 enum DefinitionKind { kRegularDefinition, kWeakDefinition, kTentativeDefinition, kExternalDefinition, kExternalWeakDefinition, kAbsoluteSymbol };
254 enum SymbolTableInclusion { kSymbolTableNotIn, kSymbolTableIn, kSymbolTableInAndNeverStrip, kSymbolTableInAsAbsolute };
255
256 virtual Reader* getFile() const = 0;
257 virtual bool getTranslationUnitSource(const char** dir, const char** name) const = 0;
258 virtual const char* getName() const = 0;
259 virtual const char* getDisplayName() const = 0;
260 virtual Scope getScope() const = 0;
261 virtual DefinitionKind getDefinitionKind() const = 0;
262 virtual SymbolTableInclusion getSymbolTableInclusion() const = 0;
263 virtual bool dontDeadStrip() const = 0;
264 virtual bool isZeroFill() const = 0;
265 virtual bool isThumb() const = 0;
266 virtual uint64_t getSize() const = 0;
267 virtual std::vector<ObjectFile::Reference*>& getReferences() const = 0;
268 virtual bool mustRemainInSection() const = 0;
269 virtual const char* getSectionName() const = 0;
270 virtual Segment& getSegment() const = 0;
271 virtual Atom& getFollowOnAtom() const = 0;
272 virtual uint32_t getOrdinal() const = 0;
273 virtual std::vector<LineInfo>* getLineInfo() const = 0;
274 virtual Alignment getAlignment() const = 0;
275 virtual void copyRawContent(uint8_t buffer[]) const = 0;
276 virtual void setScope(Scope) = 0;
277
278
279 uint64_t getSectionOffset() const { return fSectionOffset; }
280 uint64_t getAddress() const { return fSection->getBaseAddress() + fSectionOffset; }
281 class Section* getSection() const { return fSection; }
282
283 virtual void setSectionOffset(uint64_t offset) { fSectionOffset = offset; }
284 virtual void setSection(class Section* sect) { fSection = sect; }
285
286 protected:
287 Atom() : fSectionOffset(0), fSection(NULL) {}
288 virtual ~Atom() {}
289
290 uint64_t fSectionOffset;
291 class Section* fSection;
292 };
293
294
295 //
296 // A Reference is a directed edge to another Atom. When an instruction in
297 // the content of an Atom refers to another Atom, that is represented by a
298 // Reference.
299 //
300 // There are two kinds of references: direct and by-name. With a direct Reference,
301 // the target is bound by the Reader that created it. For instance a reference to a
302 // static would produce a direct reference. A by-name reference requires the linker
303 // to find the target Atom with the required name in order to be bound.
304 //
305 // For a link to succeed all References must be bound.
306 //
307 // A Reference has an optional "from" target. This is used when the content to fix-up
308 // is the difference of two Atom address. For instance, if a pointer sized data Atom
309 // is to contain A - B, then the Atom would have on Reference with a target of "A" and
310 // a from-target of "B".
311 //
312 // A Reference also has a fix-up-offset. This is the offset into the content of the
313 // Atom holding the reference where the fix-up (relocation) will be applied.
314 //
315 //
316 //
317 class Reference
318 {
319 public:
320 enum TargetBinding { kUnboundByName, kBoundDirectly, kBoundByName, kDontBind };
321
322 virtual TargetBinding getTargetBinding() const = 0;
323 virtual TargetBinding getFromTargetBinding() const = 0;
324 virtual uint8_t getKind() const = 0;
325 virtual uint64_t getFixUpOffset() const = 0;
326 virtual const char* getTargetName() const = 0;
327 virtual Atom& getTarget() const = 0;
328 virtual uint64_t getTargetOffset() const = 0;
329 virtual Atom& getFromTarget() const = 0;
330 virtual const char* getFromTargetName() const = 0;
331 virtual uint64_t getFromTargetOffset() const = 0;
332
333 virtual void setTarget(Atom&, uint64_t offset) = 0;
334 virtual void setFromTarget(Atom&) = 0;
335 virtual const char* getDescription() const = 0;
336
337 protected:
338 Reference() {}
339 virtual ~Reference() {}
340 };
341
342
343 }; // namespace ObjectFile
344
345
346 #endif // __OBJECTFILE__