]> git.saurik.com Git - apple/ld64.git/blob - src/ld/OpaqueSection.hpp
cddd45c94745701dcecf19a54d9afa022859cb80
[apple/ld64.git] / src / ld / OpaqueSection.hpp
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 #ifndef __OPAQUE_SECTION__
26 #define __OPAQUE_SECTION__
27
28
29 #include <vector>
30
31 #include "ObjectFile.h"
32
33 namespace opaque_section {
34
35
36 class Segment : public ObjectFile::Segment
37 {
38 public:
39 Segment(const char* name) { fName = name; }
40 virtual const char* getName() const { return fName; }
41 virtual bool isContentReadable() const { return true; }
42 virtual bool isContentWritable() const { return false; }
43 virtual bool isContentExecutable() const { return (strcmp(fName, "__TEXT") == 0); }
44 private:
45 const char* fName;
46 };
47
48
49 class Reader : public ObjectFile::Reader
50 {
51 public:
52 Reader(const char* segmentName, const char* sectionName, const char* path,
53 const uint8_t fileContent[], uint64_t fileLength, uint32_t ordinal, const char* symbolName=NULL);
54 virtual ~Reader();
55
56 void addSectionReference(uint8_t kind, uint64_t offsetInSection, const ObjectFile::Atom* targetAtom,
57 uint64_t offsetInTarget, const ObjectFile::Atom* fromTargetAtom=NULL, uint64_t offsetInFromTarget=0);
58
59 virtual const char* getPath() { return fPath; }
60 virtual time_t getModificationTime() { return 0; }
61 virtual DebugInfoKind getDebugInfoKind() { return ObjectFile::Reader::kDebugInfoNone; }
62 virtual std::vector<class ObjectFile::Atom*>& getAtoms() { return fAtoms; }
63 virtual std::vector<class ObjectFile::Atom*>* getJustInTimeAtomsFor(const char* name) { return NULL; }
64 virtual std::vector<Stab>* getStabs() { return NULL; }
65
66 private:
67 const char* fPath;
68 std::vector<class ObjectFile::Atom*> fAtoms;
69 };
70
71 class Reference : public ObjectFile::Reference
72 {
73 public:
74 Reference(uint8_t kind, uint64_t fixupOffset, const ObjectFile::Atom* target, uint64_t targetOffset,
75 const ObjectFile::Atom* fromTarget=NULL, uint64_t fromTargetOffset=0)
76 : fFixUpOffset(fixupOffset), fTarget(target), fTargetOffset(targetOffset), fKind(kind),
77 fFromTarget(fromTarget), fFromTargetOffset(fromTargetOffset) {}
78 virtual ~Reference() {}
79
80
81 virtual ObjectFile::Reference::TargetBinding getTargetBinding() const { return ObjectFile::Reference::kBoundDirectly; }
82 virtual ObjectFile::Reference::TargetBinding getFromTargetBinding() const{ return ObjectFile::Reference::kDontBind; }
83 virtual uint8_t getKind() const { return fKind; }
84 virtual uint64_t getFixUpOffset() const { return fFixUpOffset; }
85 virtual const char* getTargetName() const { return fTarget->getName(); }
86 virtual ObjectFile::Atom& getTarget() const { return *((ObjectFile::Atom*)fTarget); }
87 virtual uint64_t getTargetOffset() const { return fTargetOffset; }
88 virtual ObjectFile::Atom& getFromTarget() const { return *((ObjectFile::Atom*)fFromTarget); }
89 virtual const char* getFromTargetName() const { return fFromTarget->getName(); }
90 virtual uint64_t getFromTargetOffset() const { return fFromTargetOffset; }
91 virtual void setTarget(ObjectFile::Atom&, uint64_t offset) { throw "can't set target"; }
92 virtual void setFromTarget(ObjectFile::Atom&) { throw "can't set from target"; }
93 virtual const char* getDescription() const { return "opaque section reference"; }
94
95 private:
96 uint64_t fFixUpOffset;
97 const ObjectFile::Atom* fTarget;
98 uint64_t fTargetOffset;
99 uint8_t fKind;
100 const ObjectFile::Atom* fFromTarget;
101 uint64_t fFromTargetOffset;
102 };
103
104
105 class Atom : public ObjectFile::Atom {
106 public:
107 virtual ObjectFile::Reader* getFile() const { return &fOwner; }
108 virtual bool getTranslationUnitSource(const char** dir, const char** name) const { return false; }
109 virtual const char* getName() const { return fName; }
110 virtual const char* getDisplayName() const;
111 virtual Scope getScope() const { return ObjectFile::Atom::scopeLinkageUnit; }
112 virtual DefinitionKind getDefinitionKind() const { return kRegularDefinition; }
113 virtual SymbolTableInclusion getSymbolTableInclusion() const { return ObjectFile::Atom::kSymbolTableNotIn; }
114 virtual bool dontDeadStrip() const { return true; }
115 virtual bool isZeroFill() const { return false; }
116 virtual bool isThumb() const { return false; }
117 virtual uint64_t getSize() const { return fFileLength; }
118 virtual std::vector<ObjectFile::Reference*>& getReferences() const { return (std::vector<ObjectFile::Reference*>&)(fReferences); }
119 virtual bool mustRemainInSection() const { return false; }
120 virtual const char* getSectionName() const { return fSectionName; }
121 virtual Segment& getSegment() const { return fSegment; }
122 virtual ObjectFile::Atom& getFollowOnAtom() const { return *((ObjectFile::Atom*)NULL); }
123 virtual uint32_t getOrdinal() const { return fOrdinal; }
124 virtual std::vector<ObjectFile::LineInfo>* getLineInfo() const { return NULL; }
125 virtual ObjectFile::Alignment getAlignment() const { return ObjectFile::Alignment(4); }
126 virtual void copyRawContent(uint8_t buffer[]) const;
127
128 virtual void setScope(Scope) { }
129
130 protected:
131 friend class Reader;
132
133 Atom(Reader& owner, Segment& segment, const char* sectionName,
134 const uint8_t fileContent[], uint64_t fileLength, uint32_t ordinal, const char* symbolName);
135 virtual ~Atom() {}
136
137 Reader& fOwner;
138 Segment& fSegment;
139 const char* fName;
140 const char* fSectionName;
141 const uint8_t* fFileContent;
142 uint32_t fOrdinal;
143 uint64_t fFileLength;
144 std::vector<ObjectFile::Reference*> fReferences;
145 };
146
147
148
149 Atom::Atom(Reader& owner, Segment& segment, const char* sectionName, const uint8_t fileContent[], uint64_t fileLength, uint32_t ordinal, const char* symbolName)
150 : fOwner(owner), fSegment(segment), fSectionName(sectionName), fFileContent(fileContent), fOrdinal(ordinal), fFileLength(fileLength)
151 {
152 if ( symbolName != NULL )
153 fName = strdup(symbolName);
154 else
155 asprintf((char**)&fName, "__section$%s%s", segment.getName(), sectionName);
156 }
157
158
159 Reader::Reader(const char* segmentName, const char* sectionName, const char* path, const uint8_t fileContent[],
160 uint64_t fileLength, uint32_t ordinal, const char* symbolName)
161 : fPath(path)
162 {
163 fAtoms.push_back(new Atom(*this, *(new Segment(segmentName)), strdup(sectionName), fileContent, fileLength, ordinal, symbolName));
164 }
165
166 Reader::~Reader()
167 {
168 }
169
170 void Reader::addSectionReference(uint8_t kind, uint64_t offsetInSection, const ObjectFile::Atom* targetAtom,
171 uint64_t offsetInTarget, const ObjectFile::Atom* fromTargetAtom, uint64_t offsetInFromTarget)
172 {
173 fAtoms[0]->getReferences().push_back(new Reference(kind, offsetInSection, targetAtom, offsetInTarget, fromTargetAtom, offsetInFromTarget));
174 }
175
176
177 const char* Atom::getDisplayName() const
178 {
179 static char name[64];
180 sprintf(name, "opaque section %s %s", fSegment.getName(), fSectionName);
181 return name;
182 }
183
184
185 void Atom::copyRawContent(uint8_t buffer[]) const
186 {
187 memcpy(buffer, fFileContent, fFileLength);
188 }
189
190
191
192 };
193
194
195
196 #endif // __OPAQUE_SECTION__
197
198
199