blob: 8c7b0b7632452fe98c0a51e4aca71eede6d9b693 [file] [log] [blame]
Rui Ueyama0ca149f2013-08-06 22:31:59 +00001//===- lib/ReaderWriter/CoreLinkingContext.cpp ----------------------------===//
2//
3// The LLVM Linker
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "lld/ReaderWriter/CoreLinkingContext.h"
11
12#include "lld/Core/Pass.h"
13#include "lld/Core/PassManager.h"
14#include "lld/Passes/LayoutPass.h"
15
16#include "llvm/ADT/ArrayRef.h"
17
18using namespace lld;
19
20namespace {
21
22/// \brief Simple atom created by the stubs pass.
23class TestingStubAtom : public DefinedAtom {
24public:
25 TestingStubAtom(const File &F, const Atom &) : _file(F) {
26 static uint32_t lastOrdinal = 0;
27 _ordinal = lastOrdinal++;
28 }
29
30 virtual const File &file() const { return _file; }
31
32 virtual StringRef name() const { return StringRef(); }
33
34 virtual uint64_t ordinal() const { return _ordinal; }
35
36 virtual uint64_t size() const { return 0; }
37
38 virtual Scope scope() const { return DefinedAtom::scopeLinkageUnit; }
39
40 virtual Interposable interposable() const { return DefinedAtom::interposeNo; }
41
42 virtual Merge merge() const { return DefinedAtom::mergeNo; }
43
44 virtual ContentType contentType() const { return DefinedAtom::typeStub; }
45
46 virtual Alignment alignment() const { return Alignment(0, 0); }
47
48 virtual SectionChoice sectionChoice() const {
49 return DefinedAtom::sectionBasedOnContent;
50 }
51
52 virtual StringRef customSectionName() const { return StringRef(); }
53
54 virtual SectionPosition sectionPosition() const { return sectionPositionAny; }
55
56 virtual DeadStripKind deadStrip() const {
57 return DefinedAtom::deadStripNormal;
58 }
59
60 virtual ContentPermissions permissions() const {
61 return DefinedAtom::permR_X;
62 }
63
64 virtual bool isAlias() const { return false; }
65
66 virtual ArrayRef<uint8_t> rawContent() const { return ArrayRef<uint8_t>(); }
67
68 virtual reference_iterator begin() const {
69 return reference_iterator(*this, nullptr);
70 }
71
72 virtual reference_iterator end() const {
73 return reference_iterator(*this, nullptr);
74 }
75
76 virtual const Reference *derefIterator(const void *iter) const {
77 return nullptr;
78 }
79
80 virtual void incrementIterator(const void *&iter) const {}
81
82private:
83 const File &_file;
84 uint32_t _ordinal;
85};
86
87/// \brief Simple atom created by the GOT pass.
88class TestingGOTAtom : public DefinedAtom {
89public:
90 TestingGOTAtom(const File &F, const Atom &) : _file(F) {
91 static uint32_t lastOrdinal = 0;
92 _ordinal = lastOrdinal++;
93 }
94
95 virtual const File &file() const { return _file; }
96
97 virtual StringRef name() const { return StringRef(); }
98
99 virtual uint64_t ordinal() const { return _ordinal; }
100
101 virtual uint64_t size() const { return 0; }
102
103 virtual Scope scope() const { return DefinedAtom::scopeLinkageUnit; }
104
105 virtual Interposable interposable() const { return DefinedAtom::interposeNo; }
106
107 virtual Merge merge() const { return DefinedAtom::mergeNo; }
108
109 virtual ContentType contentType() const { return DefinedAtom::typeGOT; }
110
111 virtual Alignment alignment() const { return Alignment(3, 0); }
112
113 virtual SectionChoice sectionChoice() const {
114 return DefinedAtom::sectionBasedOnContent;
115 }
116
117 virtual StringRef customSectionName() const { return StringRef(); }
118
119 virtual SectionPosition sectionPosition() const { return sectionPositionAny; }
120
121 virtual DeadStripKind deadStrip() const {
122 return DefinedAtom::deadStripNormal;
123 }
124
125 virtual ContentPermissions permissions() const {
126 return DefinedAtom::permRW_;
127 }
128
129 virtual bool isAlias() const { return false; }
130
131 virtual ArrayRef<uint8_t> rawContent() const { return ArrayRef<uint8_t>(); }
132
133 virtual reference_iterator begin() const {
134 return reference_iterator(*this, nullptr);
135 }
136
137 virtual reference_iterator end() const {
138 return reference_iterator(*this, nullptr);
139 }
140
141 virtual const Reference *derefIterator(const void *iter) const {
142 return nullptr;
143 }
144
145 virtual void incrementIterator(const void *&iter) const {}
146
147private:
148 const File &_file;
149 uint32_t _ordinal;
150};
151
152class TestingPassFile : public MutableFile {
153public:
154 TestingPassFile(const LinkingContext &ti) : MutableFile(ti, "Testing pass") {}
155
156 virtual void addAtom(const Atom &atom) {
157 if (const DefinedAtom *defAtom = dyn_cast<DefinedAtom>(&atom))
158 _definedAtoms._atoms.push_back(defAtom);
159 else
160 llvm_unreachable("atom has unknown definition kind");
161 }
162
163 virtual DefinedAtomRange definedAtoms() {
164 return range<std::vector<const DefinedAtom *>::iterator>(
165 _definedAtoms._atoms.begin(), _definedAtoms._atoms.end());
166 }
167
168 virtual const atom_collection<DefinedAtom> &defined() const {
169 return _definedAtoms;
170 }
171 virtual const atom_collection<UndefinedAtom> &undefined() const {
172 return _undefinedAtoms;
173 }
174 virtual const atom_collection<SharedLibraryAtom> &sharedLibrary() const {
175 return _sharedLibraryAtoms;
176 }
177 virtual const atom_collection<AbsoluteAtom> &absolute() const {
178 return _absoluteAtoms;
179 }
180
181private:
182 atom_collection_vector<DefinedAtom> _definedAtoms;
183 atom_collection_vector<UndefinedAtom> _undefinedAtoms;
184 atom_collection_vector<SharedLibraryAtom> _sharedLibraryAtoms;
185 atom_collection_vector<AbsoluteAtom> _absoluteAtoms;
186};
187
188struct TestingKindMapping {
189 const char *string;
190 int32_t value;
191 bool isBranch;
192 bool isGotLoad;
193 bool isGotUse;
194};
195
196//
197// Table of fixup kinds in YAML documents used for testing
198//
199const TestingKindMapping sKinds[] = {
200 { "in-group", -3, false, false, false },
201 { "layout-after", -2, false, false, false },
202 { "layout-before", -1, false, false, false },
203 { "call32", 2, true, false, false }, { "pcrel32", 3, false, false, false },
204 { "gotLoad32", 7, false, true, true }, { "gotUse32", 9, false, false, true },
205 { "lea32wasGot", 8, false, false, false }, { nullptr, 0, false, false, false }
206};
207
208class TestingStubsPass : public StubsPass {
209public:
210 TestingStubsPass(const LinkingContext &ti) : _file(TestingPassFile(ti)) {}
211
212 virtual bool noTextRelocs() { return true; }
213
214 virtual bool isCallSite(int32_t kind) {
215 for (const TestingKindMapping *p = sKinds; p->string != nullptr; ++p) {
216 if (kind == p->value)
217 return p->isBranch;
218 }
219 return false;
220 }
221
222 virtual const DefinedAtom *getStub(const Atom &target) {
223 const DefinedAtom *result = new TestingStubAtom(_file, target);
224 _file.addAtom(*result);
225 return result;
226 }
227
228 virtual void addStubAtoms(MutableFile &mergedFile) {
229 for (const DefinedAtom *stub : _file.defined()) {
230 mergedFile.addAtom(*stub);
231 }
232 }
233
234private:
235 TestingPassFile _file;
236};
237
238class TestingGOTPass : public GOTPass {
239public:
240 TestingGOTPass(const LinkingContext &ti) : _file(TestingPassFile(ti)) {}
241
242 virtual bool noTextRelocs() { return true; }
243
244 virtual bool isGOTAccess(int32_t kind, bool &canBypassGOT) {
245 for (const TestingKindMapping *p = sKinds; p->string != nullptr; ++p) {
246 if (kind == p->value) {
247 canBypassGOT = p->isGotLoad;
248 return p->isGotUse || p->isGotLoad;
249 }
250 }
251 return false;
252 }
253
254 virtual void updateReferenceToGOT(const Reference *ref, bool targetIsNowGOT) {
255 if (targetIsNowGOT)
256 const_cast<Reference *>(ref)->setKind(3); // pcrel32
257 else
258 const_cast<Reference *>(ref)->setKind(8); // lea32wasGot
259 }
260
261 virtual const DefinedAtom *makeGOTEntry(const Atom &target) {
262 return new TestingGOTAtom(_file, target);
263 }
264
265private:
266 TestingPassFile _file;
267};
268
269} // anonymous namespace
270
271CoreLinkingContext::CoreLinkingContext() {}
272
273bool CoreLinkingContext::validateImpl(raw_ostream &diagnostics) {
274 return false;
275}
276
277void CoreLinkingContext::addPasses(PassManager &pm) const {
278 for (StringRef name : _passNames) {
279 if (name.equals("layout"))
280 pm.add(std::unique_ptr<Pass>((new LayoutPass())));
281 else if (name.equals("GOT"))
282 pm.add(std::unique_ptr<Pass>(new TestingGOTPass(*this)));
283 else if (name.equals("stubs"))
284 pm.add(std::unique_ptr<Pass>(new TestingStubsPass(*this)));
285 else
286 llvm_unreachable("bad pass name");
287 }
288}
289
290error_code CoreLinkingContext::parseFile(
291 std::unique_ptr<MemoryBuffer> &mb,
292 std::vector<std::unique_ptr<File>> &result) const {
293 if (!_reader)
294 _reader = createReaderYAML(*this);
295 return _reader->parseFile(mb, result);
296}
297
298Writer &CoreLinkingContext::writer() const {
299 if (!_writer)
300 _writer = createWriterYAML(*this);
301 return *_writer;
302}
303
304ErrorOr<Reference::Kind>
305CoreLinkingContext::relocKindFromString(StringRef str) const {
306 for (const TestingKindMapping *p = sKinds; p->string != nullptr; ++p) {
307 if (str.equals(p->string))
308 return p->value;
309 }
310 return make_error_code(yaml_reader_error::illegal_value);
311}
312
313ErrorOr<std::string>
314CoreLinkingContext::stringFromRelocKind(Reference::Kind kind) const {
315 for (const TestingKindMapping *p = sKinds; p->string != nullptr; ++p) {
316 if (kind == p->value)
317 return std::string(p->string);
318 }
319 return make_error_code(yaml_reader_error::illegal_value);
320}