]> git.saurik.com Git - apple/ld64.git/blame - src/ObjectFile.h
ld64-26.0.80.tar.gz
[apple/ld64.git] / src / ObjectFile.h
CommitLineData
6e880c60
A
1/* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-
2 *
c2646906
A
3 * Copyright (c) 2005 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
35namespace ObjectFile {
36
37struct StabsInfo
38{
39 uint64_t atomOffset;
40 const char* string;
41 uint8_t type;
42 uint8_t other;
43 uint16_t desc;
44};
45
46class ReaderOptions
47{
48public:
49 ReaderOptions() : fFullyLoadArchives(false), fLoadObjcClassesInArchives(false), fFlatNamespace(false),
50 fStripDebugInfo(false), fTraceDylibs(false), fTraceIndirectDylibs(false), fTraceArchives(false) {}
51
52 bool fFullyLoadArchives;
53 bool fLoadObjcClassesInArchives;
54 bool fFlatNamespace;
55 bool fStripDebugInfo;
56 bool fTraceDylibs;
57 bool fTraceIndirectDylibs;
58 bool fTraceArchives;
59};
60
61
62class Reader
63{
64public:
65 static Reader* createReader(const char* path, const ReaderOptions& options);
66
67 virtual const char* getPath() = 0;
68 virtual std::vector<class Atom*>& getAtoms() = 0;
69 virtual std::vector<class Atom*>* getJustInTimeAtomsFor(const char* name) = 0;
70 virtual std::vector<StabsInfo>* getStabsDebugInfo() = 0;
71
72 // For Dynamic Libraries only
73 virtual const char* getInstallPath() { return NULL; }
74 virtual uint32_t getTimestamp() { return 0; }
75 virtual uint32_t getCurrentVersion() { return 0; }
76 virtual uint32_t getCompatibilityVersion() { return 0; }
77 virtual std::vector<const char*>* getDependentLibraryPaths() { return NULL; }
78 virtual bool reExports(Reader*) { return false; }
79 virtual bool isDefinitionWeak(const Atom&){ return false; }
80
81
82
83protected:
84 Reader() {}
6e880c60 85 virtual ~Reader() {}
c2646906
A
86};
87
88class Segment
89{
90public:
91 virtual const char* getName() const = 0;
92 virtual bool isContentReadable() const = 0;
93 virtual bool isContentWritable() const = 0;
94 virtual bool isContentExecutable() const = 0;
95
96 uint64_t getBaseAddress() const { return fBaseAddress; }
97 void setBaseAddress(uint64_t addr) { fBaseAddress = addr; }
6e880c60 98 virtual bool hasFixedAddress() const { return false; }
c2646906
A
99
100protected:
6e880c60
A
101 Segment() : fBaseAddress(0) {}
102 virtual ~Segment() {}
c2646906
A
103 uint64_t fBaseAddress;
104};
105
106class Reference;
107
108class Section
109{
110public:
111 unsigned int getIndex() { return fIndex; }
112 uint64_t getBaseAddress() { return fBaseAddress; }
113 void setBaseAddress(uint64_t addr) { fBaseAddress = addr; }
114 void* fOther;
115
116protected:
117 Section() : fOther(NULL), fBaseAddress(0), fIndex(0) {}
118 uint64_t fBaseAddress;
119 unsigned int fIndex;
120};
121
122
123class ContentWriter
124{
125public:
126 virtual void write(uint64_t atomOffset, const void* buffer, uint64_t size) = 0;
6e880c60
A
127protected:
128 ContentWriter() {}
129 virtual ~ContentWriter() {}
c2646906
A
130};
131
132class Atom
133{
134public:
135 enum Scope { scopeTranslationUnit, scopeLinkageUnit, scopeGlobal };
136 enum WeakImportSetting { kWeakUnset, kWeakImport, kNonWeakImport };
137
138 virtual Reader* getFile() const = 0;
139 virtual const char* getName() const = 0;
140 virtual const char* getDisplayName() const = 0;
141 virtual Scope getScope() const = 0;
142 virtual bool isTentativeDefinition() const = 0;
143 virtual bool isWeakDefinition() const = 0;
144 virtual bool isCoalesableByName() const = 0;
145 virtual bool isCoalesableByValue() const = 0;
146 virtual bool isZeroFill() const = 0;
147 virtual bool dontDeadStrip() const = 0;
148 virtual bool dontStripName() const = 0; // referenced dynamically
149 virtual bool isImportProxy() const = 0;
150 virtual uint64_t getSize() const = 0;
151 virtual std::vector<ObjectFile::Reference*>& getReferences() const = 0;
152 virtual bool mustRemainInSection() const = 0;
153 virtual const char* getSectionName() const = 0;
154 virtual Segment& getSegment() const = 0;
155 virtual bool requiresFollowOnAtom() const = 0;
156 virtual Atom& getFollowOnAtom() const = 0;
157 virtual std::vector<StabsInfo>* getStabsDebugInfo() const = 0;
158 virtual uint8_t getAlignment() const = 0;
159 virtual WeakImportSetting getImportWeakness() const = 0;
160 virtual void copyRawContent(uint8_t buffer[]) const = 0;
161 virtual void writeContent(bool finalLinkedImage, ContentWriter&) const = 0;
162 virtual void setScope(Scope) = 0;
163 virtual void setImportWeakness(bool weakImport) = 0;
164
165
166 uint64_t getSectionOffset() const { return fSectionOffset; }
167 uint64_t getSegmentOffset() const { return fSegmentOffset; }
168 uint64_t getAddress() const { return fSection->getBaseAddress() + fSectionOffset; }
169 unsigned int getSortOrder() const { return fSortOrder; }
170 class Section* getSection() const { return fSection; }
171
172 void setSegmentOffset(uint64_t offset) { fSegmentOffset = offset; }
173 void setSectionOffset(uint64_t offset) { fSectionOffset = offset; }
174 void setSection(class Section* sect) { fSection = sect; }
175 unsigned int setSortOrder(unsigned int order); // recursively sets follow-on atoms
176
177protected:
6e880c60
A
178 Atom() : fSegmentOffset(0), fSectionOffset(0), fSortOrder(0), fSection(NULL) {}
179 virtual ~Atom() {}
c2646906
A
180
181 uint64_t fSegmentOffset;
182 uint64_t fSectionOffset;
183 unsigned int fSortOrder;
184 class Section* fSection;
185};
186
187
188
189// recursively sets follow-on atoms
190inline unsigned int Atom::setSortOrder(unsigned int order)
191{
192 if ( this->requiresFollowOnAtom() ) {
193 fSortOrder = order;
194 return this->getFollowOnAtom().setSortOrder(order+1);
195 }
196 else {
197 fSortOrder = order;
198 return (order + 1);
199 }
200}
201
202
203
204class Reference
205{
206public:
207 enum Kind { noFixUp, pointer, ppcFixupBranch24, ppcFixupBranch14,
208 ppcFixupPicBaseLow16, ppcFixupPicBaseLow14, ppcFixupPicBaseHigh16,
209 ppcFixupAbsLow16, ppcFixupAbsLow14, ppcFixupAbsHigh16, ppcFixupAbsHigh16AddLow,
210 pointer32Difference, pointer64Difference, x86FixupBranch32 };
211
6e880c60
A
212 virtual bool isTargetUnbound() const = 0;
213 virtual bool isFromTargetUnbound() const = 0;
214 virtual bool requiresRuntimeFixUp(bool slideable) const = 0;
c2646906 215 virtual bool isWeakReference() const = 0;
c2646906
A
216 virtual bool isLazyReference() const = 0;
217 virtual Kind getKind() const = 0;
218 virtual uint64_t getFixUpOffset() const = 0;
219 virtual const char* getTargetName() const = 0;
220 virtual Atom& getTarget() const = 0;
221 virtual uint64_t getTargetOffset() const = 0;
6e880c60 222 virtual bool hasFromTarget() const = 0;
c2646906
A
223 virtual Atom& getFromTarget() const = 0;
224 virtual const char* getFromTargetName() const = 0;
225 virtual uint64_t getFromTargetOffset() const = 0;
226
6e880c60 227 virtual void setTarget(Atom&, uint64_t offset) = 0;
c2646906
A
228 virtual void setFromTarget(Atom&) = 0;
229 virtual const char* getDescription() const = 0;
6e880c60
A
230
231protected:
232 Reference() {}
233 virtual ~Reference() {}
c2646906
A
234};
235
236
237}; // namespace ObjectFile
238
239
240#endif // __OBJECTFILE__
241
242
243
244
245
246