]> git.saurik.com Git - apple/ld64.git/blame - src/ld/OpaqueSection.hpp
ld64-97.17.tar.gz
[apple/ld64.git] / src / ld / OpaqueSection.hpp
CommitLineData
d696c285
A
1/* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-
2 *
3 * Copyright (c) 2005-2006 Apple Computer, Inc. All rights reserved.
c2646906
A
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
a61fdf0a
A
25#ifndef __OPAQUE_SECTION__
26#define __OPAQUE_SECTION__
27
28
c2646906
A
29#include <vector>
30
31#include "ObjectFile.h"
32
a61fdf0a 33namespace opaque_section {
c2646906
A
34
35
36class Segment : public ObjectFile::Segment
37{
38public:
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; }
a61fdf0a 43 virtual bool isContentExecutable() const { return (strcmp(fName, "__TEXT") == 0); }
c2646906
A
44private:
45 const char* fName;
46};
47
48
49class Reader : public ObjectFile::Reader
50{
51public:
a61fdf0a
A
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);
c2646906
A
54 virtual ~Reader();
55
a61fdf0a
A
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
c2646906 59 virtual const char* getPath() { return fPath; }
d696c285
A
60 virtual time_t getModificationTime() { return 0; }
61 virtual DebugInfoKind getDebugInfoKind() { return ObjectFile::Reader::kDebugInfoNone; }
c2646906
A
62 virtual std::vector<class ObjectFile::Atom*>& getAtoms() { return fAtoms; }
63 virtual std::vector<class ObjectFile::Atom*>* getJustInTimeAtomsFor(const char* name) { return NULL; }
d696c285 64 virtual std::vector<Stab>* getStabs() { return NULL; }
c2646906
A
65
66private:
67 const char* fPath;
68 std::vector<class ObjectFile::Atom*> fAtoms;
69};
70
a61fdf0a
A
71class Reference : public ObjectFile::Reference
72{
73public:
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
95private:
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
c2646906
A
104
105class Atom : public ObjectFile::Atom {
106public:
107 virtual ObjectFile::Reader* getFile() const { return &fOwner; }
d696c285 108 virtual bool getTranslationUnitSource(const char** dir, const char** name) const { return false; }
a61fdf0a 109 virtual const char* getName() const { return fName; }
c2646906 110 virtual const char* getDisplayName() const;
a61fdf0a 111 virtual Scope getScope() const { return ObjectFile::Atom::scopeLinkageUnit; }
d696c285
A
112 virtual DefinitionKind getDefinitionKind() const { return kRegularDefinition; }
113 virtual SymbolTableInclusion getSymbolTableInclusion() const { return ObjectFile::Atom::kSymbolTableNotIn; }
69a49097 114 virtual bool dontDeadStrip() const { return true; }
c2646906 115 virtual bool isZeroFill() const { return false; }
2f2f92e4 116 virtual bool isThumb() const { return false; }
c2646906 117 virtual uint64_t getSize() const { return fFileLength; }
a61fdf0a 118 virtual std::vector<ObjectFile::Reference*>& getReferences() const { return (std::vector<ObjectFile::Reference*>&)(fReferences); }
c2646906
A
119 virtual bool mustRemainInSection() const { return false; }
120 virtual const char* getSectionName() const { return fSectionName; }
121 virtual Segment& getSegment() const { return fSegment; }
c2646906 122 virtual ObjectFile::Atom& getFollowOnAtom() const { return *((ObjectFile::Atom*)NULL); }
a61fdf0a 123 virtual uint32_t getOrdinal() const { return fOrdinal; }
d696c285 124 virtual std::vector<ObjectFile::LineInfo>* getLineInfo() const { return NULL; }
74cfe461 125 virtual ObjectFile::Alignment getAlignment() const { return ObjectFile::Alignment(4); }
c2646906 126 virtual void copyRawContent(uint8_t buffer[]) const;
c2646906
A
127
128 virtual void setScope(Scope) { }
c2646906
A
129
130protected:
131 friend class Reader;
132
a61fdf0a
A
133 Atom(Reader& owner, Segment& segment, const char* sectionName,
134 const uint8_t fileContent[], uint64_t fileLength, uint32_t ordinal, const char* symbolName);
c2646906
A
135 virtual ~Atom() {}
136
137 Reader& fOwner;
138 Segment& fSegment;
a61fdf0a 139 const char* fName;
c2646906
A
140 const char* fSectionName;
141 const uint8_t* fFileContent;
a61fdf0a 142 uint32_t fOrdinal;
c2646906 143 uint64_t fFileLength;
a61fdf0a 144 std::vector<ObjectFile::Reference*> fReferences;
c2646906
A
145};
146
147
a61fdf0a
A
148
149Atom::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}
c2646906
A
157
158
a61fdf0a
A
159Reader::Reader(const char* segmentName, const char* sectionName, const char* path, const uint8_t fileContent[],
160 uint64_t fileLength, uint32_t ordinal, const char* symbolName)
c2646906
A
161 : fPath(path)
162{
a61fdf0a 163 fAtoms.push_back(new Atom(*this, *(new Segment(segmentName)), strdup(sectionName), fileContent, fileLength, ordinal, symbolName));
c2646906
A
164}
165
166Reader::~Reader()
167{
168}
169
a61fdf0a
A
170void 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
c2646906
A
176
177const char* Atom::getDisplayName() const
178{
179 static char name[64];
a61fdf0a 180 sprintf(name, "opaque section %s %s", fSegment.getName(), fSectionName);
c2646906
A
181 return name;
182}
183
184
185void Atom::copyRawContent(uint8_t buffer[]) const
186{
187 memcpy(buffer, fFileContent, fFileLength);
188}
189
c2646906
A
190
191
192};
193
194
195
a61fdf0a 196#endif // __OPAQUE_SECTION__
c2646906
A
197
198
199