]> git.saurik.com Git - apple/ld64.git/blame - src/ld/ObjectFile.h
ld64-95.2.12.tar.gz
[apple/ld64.git] / src / ld / ObjectFile.h
CommitLineData
d696c285 1/* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-
6e880c60 2 *
a61fdf0a 3 * Copyright (c) 2005-2007 Apple Inc. All rights reserved.
c2646906
A
4 *
5 * @APPLE_LICENSE_HEADER_START@
d696c285 6 *
c2646906
A
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.
d696c285 13 *
c2646906
A
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.
d696c285 21 *
c2646906
A
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>
55e3d2f6 32#include <set>
c2646906
A
33
34
35
d696c285
A
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
2f2f92e4 49
c2646906
A
50namespace ObjectFile {
51
d696c285
A
52
53struct LineInfo
c2646906 54{
d696c285
A
55 uint32_t atomOffset;
56 const char* fileName;
57 uint32_t lineNumber;
c2646906
A
58};
59
d696c285 60
c2646906
A
61class ReaderOptions
62{
63public:
2f2f92e4 64 ReaderOptions() : fFullyLoadArchives(false), fLoadAllObjcObjectsFromArchives(false), fFlatNamespace(false),
55e3d2f6
A
65 fLinkingMainExecutable(false),
66 fForFinalLinkedImage(false), fNoEHLabels(false), fForStatic(false), fForDyld(false), fMakeTentativeDefinitionsReal(false),
a61fdf0a 67 fWhyLoad(false), fRootSafe(false), fSetuidSafe(false),fDebugInfoStripping(kDebugInfoFull),
55e3d2f6
A
68 fImplicitlyLinkPublicDylibs(true),
69 fAddCompactUnwindEncoding(true),
70 fWarnCompactUnwind(false),
71 fRemoveDwarfUnwindIfCompactExists(false),
72 fMakeCompressedDyldInfo(false),
73 fAutoOrderInitializers(true),
74 fLogObjectFiles(false), fLogAllFiles(false),
a61fdf0a 75 fTraceDylibs(false), fTraceIndirectDylibs(false), fTraceArchives(false),
55e3d2f6 76 fTraceOutputFile(NULL), fMacVersionMin(kMinMacVersionUnset), fIPhoneVersionMin(kMinIPhoneVersionUnset) {}
d696c285 77 enum DebugInfoStripping { kDebugInfoNone, kDebugInfoMinimal, kDebugInfoFull };
55e3d2f6
A
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 };
c2646906 80
a61fdf0a
A
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;
55e3d2f6 91 bool fNoEHLabels;
a61fdf0a
A
92 bool fForStatic;
93 bool fForDyld;
94 bool fMakeTentativeDefinitionsReal;
95 bool fWhyLoad;
96 bool fRootSafe;
97 bool fSetuidSafe;
98 DebugInfoStripping fDebugInfoStripping;
2f2f92e4 99 bool fImplicitlyLinkPublicDylibs;
55e3d2f6
A
100 bool fAddCompactUnwindEncoding;
101 bool fWarnCompactUnwind;
102 bool fRemoveDwarfUnwindIfCompactExists;
103 bool fMakeCompressedDyldInfo;
104 bool fAutoOrderInitializers;
a61fdf0a
A
105 bool fLogObjectFiles;
106 bool fLogAllFiles;
107 bool fTraceDylibs;
108 bool fTraceIndirectDylibs;
109 bool fTraceArchives;
110 const char* fTraceOutputFile;
55e3d2f6
A
111 MacVersionMin fMacVersionMin;
112 IPhoneVersionMin fIPhoneVersionMin;
a61fdf0a 113 std::vector<AliasPair> fAliases;
c2646906
A
114};
115
116
117class Reader
118{
119public:
d696c285
A
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 };
a61fdf0a 130 enum ObjcConstraint { kObjcNone, kObjcRetainRelease, kObjcRetainReleaseOrGC, kObjcGC };
2f2f92e4 131 enum CpuConstraint { kCpuAny = 0 };
a61fdf0a
A
132
133 class DylibHander
134 {
135 public:
136 virtual ~DylibHander() {}
137 virtual Reader* findDylib(const char* installPath, const char* fromPath) = 0;
138 };
139
d696c285 140
c2646906 141 static Reader* createReader(const char* path, const ReaderOptions& options);
d696c285 142
c2646906 143 virtual const char* getPath() = 0;
d696c285
A
144 virtual time_t getModificationTime() = 0;
145 virtual DebugInfoKind getDebugInfoKind() = 0;
c2646906
A
146 virtual std::vector<class Atom*>& getAtoms() = 0;
147 virtual std::vector<class Atom*>* getJustInTimeAtomsFor(const char* name) = 0;
d696c285 148 virtual std::vector<Stab>* getStabs() = 0;
a61fdf0a 149 virtual ObjcConstraint getObjCConstraint() { return kObjcNone; }
2f2f92e4 150 virtual uint32_t updateCpuConstraint(uint32_t current) { return current; }
a61fdf0a
A
151 virtual bool objcReplacementClasses() { return false; }
152
153 // For relocatable object files only
154 virtual bool canScatterAtoms() { return true; }
55e3d2f6
A
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,
2f2f92e4
A
161 bool allGlobalsAReDeadStripRoots, int okind,
162 bool verbose, bool saveTemps, const char* outputFilePath,
55e3d2f6 163 bool pie, bool allowTextRelocs) { return false; }
2f2f92e4 164 virtual bool hasLongBranchStubs() { return false; }
d696c285 165
c2646906
A
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; }
a61fdf0a
A
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; }
d696c285
A
176 virtual const char* parentUmbrella() { return NULL; }
177 virtual std::vector<const char*>* getAllowableClients() { return NULL; }
2f2f92e4 178 virtual bool hasWeakExternals() { return false; }
55e3d2f6 179 virtual bool deadStrippable() { return false; }
2f2f92e4 180 virtual bool isLazyLoadedDylib() { return false; }
a61fdf0a 181
c2646906 182protected:
a61fdf0a 183 Reader() {}
6e880c60 184 virtual ~Reader() {}
c2646906
A
185};
186
187class Segment
188{
189public:
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;
d696c285 194
c2646906
A
195 uint64_t getBaseAddress() const { return fBaseAddress; }
196 void setBaseAddress(uint64_t addr) { fBaseAddress = addr; }
6e880c60 197 virtual bool hasFixedAddress() const { return false; }
c2646906
A
198
199protected:
6e880c60
A
200 Segment() : fBaseAddress(0) {}
201 virtual ~Segment() {}
c2646906
A
202 uint64_t fBaseAddress;
203};
204
205class Reference;
206
d696c285 207class Section
c2646906
A
208{
209public:
210 unsigned int getIndex() { return fIndex; }
211 uint64_t getBaseAddress() { return fBaseAddress; }
212 void setBaseAddress(uint64_t addr) { fBaseAddress = addr; }
213 void* fOther;
d696c285 214
c2646906
A
215protected:
216 Section() : fOther(NULL), fBaseAddress(0), fIndex(0) {}
217 uint64_t fBaseAddress;
218 unsigned int fIndex;
219};
220
221
74cfe461
A
222struct Alignment
223{
224 Alignment(int p2, int m=0) : powerOf2(p2), modulus(m) {}
a61fdf0a 225 uint8_t trailingZeros() const { return (modulus==0) ? powerOf2 : __builtin_ctz(modulus); }
74cfe461
A
226 uint16_t powerOf2;
227 uint16_t modulus;
228};
c2646906 229
55e3d2f6
A
230struct UnwindInfo
231{
232 uint32_t startOffset;
233 uint32_t unwindInfo;
234
235 typedef UnwindInfo* iterator;
236
237};
238
239
d696c285
A
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//
a61fdf0a
A
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//
d696c285 278class Atom
c2646906
A
279{
280public:
281 enum Scope { scopeTranslationUnit, scopeLinkageUnit, scopeGlobal };
a61fdf0a 282 enum DefinitionKind { kRegularDefinition, kWeakDefinition, kTentativeDefinition, kExternalDefinition, kExternalWeakDefinition, kAbsoluteSymbol };
55e3d2f6 283 enum ContentType { kUnclassifiedType, kCStringType, kCFIType, kLSDAType };
69a49097 284 enum SymbolTableInclusion { kSymbolTableNotIn, kSymbolTableIn, kSymbolTableInAndNeverStrip, kSymbolTableInAsAbsolute };
d696c285 285
c2646906 286 virtual Reader* getFile() const = 0;
d696c285 287 virtual bool getTranslationUnitSource(const char** dir, const char** name) const = 0;
c2646906
A
288 virtual const char* getName() const = 0;
289 virtual const char* getDisplayName() const = 0;
290 virtual Scope getScope() const = 0;
d696c285 291 virtual DefinitionKind getDefinitionKind() const = 0;
55e3d2f6 292 virtual ContentType getContentType() const { return kUnclassifiedType; }
d696c285 293 virtual SymbolTableInclusion getSymbolTableInclusion() const = 0;
69a49097 294 virtual bool dontDeadStrip() const = 0;
c2646906 295 virtual bool isZeroFill() const = 0;
2f2f92e4 296 virtual bool isThumb() const = 0;
c2646906
A
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;
c2646906 302 virtual Atom& getFollowOnAtom() const = 0;
a61fdf0a 303 virtual uint32_t getOrdinal() const = 0;
d696c285 304 virtual std::vector<LineInfo>* getLineInfo() const = 0;
74cfe461 305 virtual Alignment getAlignment() const = 0;
c2646906 306 virtual void copyRawContent(uint8_t buffer[]) const = 0;
c2646906 307 virtual void setScope(Scope) = 0;
55e3d2f6
A
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; }
d696c285 313
c2646906 314 uint64_t getSectionOffset() const { return fSectionOffset; }
c2646906 315 uint64_t getAddress() const { return fSection->getBaseAddress() + fSectionOffset; }
c2646906
A
316 class Section* getSection() const { return fSection; }
317
a61fdf0a
A
318 virtual void setSectionOffset(uint64_t offset) { fSectionOffset = offset; }
319 virtual void setSection(class Section* sect) { fSection = sect; }
c2646906
A
320
321protected:
a61fdf0a 322 Atom() : fSectionOffset(0), fSection(NULL) {}
6e880c60 323 virtual ~Atom() {}
d696c285 324
c2646906 325 uint64_t fSectionOffset;
c2646906
A
326 class Section* fSection;
327};
328
329
d696c285
A
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//
c2646906
A
352class Reference
353{
354public:
a61fdf0a 355 enum TargetBinding { kUnboundByName, kBoundDirectly, kBoundByName, kDontBind };
c2646906 356
a61fdf0a
A
357 virtual TargetBinding getTargetBinding() const = 0;
358 virtual TargetBinding getFromTargetBinding() const = 0;
d696c285 359 virtual uint8_t getKind() const = 0;
c2646906
A
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
6e880c60 368 virtual void setTarget(Atom&, uint64_t offset) = 0;
c2646906
A
369 virtual void setFromTarget(Atom&) = 0;
370 virtual const char* getDescription() const = 0;
55e3d2f6 371 virtual bool isBranch() const { return false; }
d696c285 372
6e880c60
A
373protected:
374 Reference() {}
375 virtual ~Reference() {}
c2646906
A
376};
377
378
379}; // namespace ObjectFile
380
381
382#endif // __OBJECTFILE__