]> git.saurik.com Git - apple/ld64.git/blob - src/ObjectFile.h
5a80c26f352e7ea5da3a602eacd2bbca84a61cc8
[apple/ld64.git] / src / ObjectFile.h
1 /*
2 * Copyright (c) 2005 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24
25 #ifndef __OBJECTFILE__
26 #define __OBJECTFILE__
27
28 #include <stdint.h>
29 #include <vector>
30 #include <map>
31
32
33
34 namespace ObjectFile {
35
36 struct StabsInfo
37 {
38 uint64_t atomOffset;
39 const char* string;
40 uint8_t type;
41 uint8_t other;
42 uint16_t desc;
43 };
44
45 class ReaderOptions
46 {
47 public:
48 ReaderOptions() : fFullyLoadArchives(false), fLoadObjcClassesInArchives(false), fFlatNamespace(false),
49 fStripDebugInfo(false), fTraceDylibs(false), fTraceIndirectDylibs(false), fTraceArchives(false) {}
50
51 bool fFullyLoadArchives;
52 bool fLoadObjcClassesInArchives;
53 bool fFlatNamespace;
54 bool fStripDebugInfo;
55 bool fTraceDylibs;
56 bool fTraceIndirectDylibs;
57 bool fTraceArchives;
58 };
59
60
61 class Reader
62 {
63 public:
64 static Reader* createReader(const char* path, const ReaderOptions& options);
65
66 virtual const char* getPath() = 0;
67 virtual std::vector<class Atom*>& getAtoms() = 0;
68 virtual std::vector<class Atom*>* getJustInTimeAtomsFor(const char* name) = 0;
69 virtual std::vector<StabsInfo>* getStabsDebugInfo() = 0;
70
71 // For Dynamic Libraries only
72 virtual const char* getInstallPath() { return NULL; }
73 virtual uint32_t getTimestamp() { return 0; }
74 virtual uint32_t getCurrentVersion() { return 0; }
75 virtual uint32_t getCompatibilityVersion() { return 0; }
76 virtual std::vector<const char*>* getDependentLibraryPaths() { return NULL; }
77 virtual bool reExports(Reader*) { return false; }
78 virtual bool isDefinitionWeak(const Atom&){ return false; }
79
80
81
82 protected:
83 Reader() {}
84 };
85
86 class Segment
87 {
88 public:
89 virtual const char* getName() const = 0;
90 virtual bool isContentReadable() const = 0;
91 virtual bool isContentWritable() const = 0;
92 virtual bool isContentExecutable() const = 0;
93
94 uint64_t getBaseAddress() const { return fBaseAddress; }
95 void setBaseAddress(uint64_t addr) { fBaseAddress = addr; }
96
97 protected:
98 Segment() : fBaseAddress(0) {}
99 uint64_t fBaseAddress;
100 };
101
102 class Reference;
103
104 class Section
105 {
106 public:
107 unsigned int getIndex() { return fIndex; }
108 uint64_t getBaseAddress() { return fBaseAddress; }
109 void setBaseAddress(uint64_t addr) { fBaseAddress = addr; }
110 void* fOther;
111
112 protected:
113 Section() : fOther(NULL), fBaseAddress(0), fIndex(0) {}
114 uint64_t fBaseAddress;
115 unsigned int fIndex;
116 };
117
118
119 class ContentWriter
120 {
121 public:
122 virtual void write(uint64_t atomOffset, const void* buffer, uint64_t size) = 0;
123 };
124
125 class Atom
126 {
127 public:
128 enum Scope { scopeTranslationUnit, scopeLinkageUnit, scopeGlobal };
129 enum WeakImportSetting { kWeakUnset, kWeakImport, kNonWeakImport };
130
131 virtual Reader* getFile() const = 0;
132 virtual const char* getName() const = 0;
133 virtual const char* getDisplayName() const = 0;
134 virtual Scope getScope() const = 0;
135 virtual bool isTentativeDefinition() const = 0;
136 virtual bool isWeakDefinition() const = 0;
137 virtual bool isCoalesableByName() const = 0;
138 virtual bool isCoalesableByValue() const = 0;
139 virtual bool isZeroFill() const = 0;
140 virtual bool dontDeadStrip() const = 0;
141 virtual bool dontStripName() const = 0; // referenced dynamically
142 virtual bool isImportProxy() const = 0;
143 virtual uint64_t getSize() const = 0;
144 virtual std::vector<ObjectFile::Reference*>& getReferences() const = 0;
145 virtual bool mustRemainInSection() const = 0;
146 virtual const char* getSectionName() const = 0;
147 virtual Segment& getSegment() const = 0;
148 virtual bool requiresFollowOnAtom() const = 0;
149 virtual Atom& getFollowOnAtom() const = 0;
150 virtual std::vector<StabsInfo>* getStabsDebugInfo() const = 0;
151 virtual uint8_t getAlignment() const = 0;
152 virtual WeakImportSetting getImportWeakness() const = 0;
153 virtual void copyRawContent(uint8_t buffer[]) const = 0;
154 virtual void writeContent(bool finalLinkedImage, ContentWriter&) const = 0;
155 virtual void setScope(Scope) = 0;
156 virtual void setImportWeakness(bool weakImport) = 0;
157
158
159 uint64_t getSectionOffset() const { return fSectionOffset; }
160 uint64_t getSegmentOffset() const { return fSegmentOffset; }
161 uint64_t getAddress() const { return fSection->getBaseAddress() + fSectionOffset; }
162 unsigned int getSortOrder() const { return fSortOrder; }
163 class Section* getSection() const { return fSection; }
164
165 void setSegmentOffset(uint64_t offset) { fSegmentOffset = offset; }
166 void setSectionOffset(uint64_t offset) { fSectionOffset = offset; }
167 void setSection(class Section* sect) { fSection = sect; }
168 unsigned int setSortOrder(unsigned int order); // recursively sets follow-on atoms
169
170 protected:
171 Atom() : fSegmentOffset(0), fSectionOffset(0), fSortOrder(0), fSection(NULL) {}
172
173 uint64_t fSegmentOffset;
174 uint64_t fSectionOffset;
175 unsigned int fSortOrder;
176 class Section* fSection;
177 };
178
179
180
181 // recursively sets follow-on atoms
182 inline unsigned int Atom::setSortOrder(unsigned int order)
183 {
184 if ( this->requiresFollowOnAtom() ) {
185 fSortOrder = order;
186 return this->getFollowOnAtom().setSortOrder(order+1);
187 }
188 else {
189 fSortOrder = order;
190 return (order + 1);
191 }
192 }
193
194
195
196 class Reference
197 {
198 public:
199 enum Kind { noFixUp, pointer, ppcFixupBranch24, ppcFixupBranch14,
200 ppcFixupPicBaseLow16, ppcFixupPicBaseLow14, ppcFixupPicBaseHigh16,
201 ppcFixupAbsLow16, ppcFixupAbsLow14, ppcFixupAbsHigh16, ppcFixupAbsHigh16AddLow,
202 pointer32Difference, pointer64Difference, x86FixupBranch32 };
203
204 virtual bool isUnbound() const = 0;
205 virtual bool isWeakReference() const = 0;
206 virtual bool requiresRuntimeFixUp() const = 0;
207 virtual bool isLazyReference() const = 0;
208 virtual Kind getKind() const = 0;
209 virtual uint64_t getFixUpOffset() const = 0;
210 virtual const char* getTargetName() const = 0;
211 virtual Atom& getTarget() const = 0;
212 virtual uint64_t getTargetOffset() const = 0;
213 virtual Atom& getFromTarget() const = 0;
214 virtual const char* getFromTargetName() const = 0;
215 virtual uint64_t getFromTargetOffset() const = 0;
216
217 virtual void setTarget(Atom&) = 0;
218 virtual void setFromTarget(Atom&) = 0;
219 virtual const char* getDescription() const = 0;
220 };
221
222
223 }; // namespace ObjectFile
224
225
226 #endif // __OBJECTFILE__
227
228
229
230
231
232