blob: ba2ee20d2d6792befeeeb8557ab08476e34fd8a1 [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"
Rui Ueyama0ca149f2013-08-06 22:31:59 +000011#include "lld/Core/Pass.h"
12#include "lld/Core/PassManager.h"
Rui Ueyamae05d3802014-06-11 21:47:51 +000013#include "lld/Core/Simple.h"
Rui Ueyama0ca149f2013-08-06 22:31:59 +000014#include "lld/Passes/LayoutPass.h"
Shankar Easwaran2bc24922013-10-29 05:12:14 +000015#include "lld/Passes/RoundTripYAMLPass.h"
Rui Ueyama0ca149f2013-08-06 22:31:59 +000016#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
Rui Ueyama16e543b2014-03-06 21:14:04 +000030 const File &file() const override { return _file; }
Rui Ueyama0ca149f2013-08-06 22:31:59 +000031
Rui Ueyama16e543b2014-03-06 21:14:04 +000032 StringRef name() const override { return StringRef(); }
Rui Ueyama0ca149f2013-08-06 22:31:59 +000033
Rui Ueyama16e543b2014-03-06 21:14:04 +000034 uint64_t ordinal() const override { return _ordinal; }
Rui Ueyama0ca149f2013-08-06 22:31:59 +000035
Rui Ueyama16e543b2014-03-06 21:14:04 +000036 uint64_t size() const override { return 0; }
Rui Ueyama0ca149f2013-08-06 22:31:59 +000037
Rui Ueyama16e543b2014-03-06 21:14:04 +000038 Scope scope() const override { return DefinedAtom::scopeLinkageUnit; }
Rui Ueyama0ca149f2013-08-06 22:31:59 +000039
Rui Ueyama16e543b2014-03-06 21:14:04 +000040 Interposable interposable() const override { return DefinedAtom::interposeNo; }
Rui Ueyama0ca149f2013-08-06 22:31:59 +000041
Rui Ueyama16e543b2014-03-06 21:14:04 +000042 Merge merge() const override { return DefinedAtom::mergeNo; }
Rui Ueyama0ca149f2013-08-06 22:31:59 +000043
Rui Ueyama16e543b2014-03-06 21:14:04 +000044 ContentType contentType() const override { return DefinedAtom::typeStub; }
Rui Ueyama0ca149f2013-08-06 22:31:59 +000045
Rui Ueyama16e543b2014-03-06 21:14:04 +000046 Alignment alignment() const override { return Alignment(0, 0); }
Rui Ueyama0ca149f2013-08-06 22:31:59 +000047
Rui Ueyama16e543b2014-03-06 21:14:04 +000048 SectionChoice sectionChoice() const override {
Rui Ueyama0ca149f2013-08-06 22:31:59 +000049 return DefinedAtom::sectionBasedOnContent;
50 }
51
Rui Ueyama16e543b2014-03-06 21:14:04 +000052 StringRef customSectionName() const override { return StringRef(); }
Rui Ueyama0ca149f2013-08-06 22:31:59 +000053
Rui Ueyama16e543b2014-03-06 21:14:04 +000054 SectionPosition sectionPosition() const override { return sectionPositionAny; }
Rui Ueyama0ca149f2013-08-06 22:31:59 +000055
Rui Ueyama16e543b2014-03-06 21:14:04 +000056 DeadStripKind deadStrip() const override {
Rui Ueyama0ca149f2013-08-06 22:31:59 +000057 return DefinedAtom::deadStripNormal;
58 }
59
Rui Ueyama16e543b2014-03-06 21:14:04 +000060 ContentPermissions permissions() const override {
Rui Ueyama0ca149f2013-08-06 22:31:59 +000061 return DefinedAtom::permR_X;
62 }
63
Rui Ueyama16e543b2014-03-06 21:14:04 +000064 ArrayRef<uint8_t> rawContent() const override { return ArrayRef<uint8_t>(); }
Rui Ueyama0ca149f2013-08-06 22:31:59 +000065
Rui Ueyama16e543b2014-03-06 21:14:04 +000066 reference_iterator begin() const override {
Rui Ueyama0ca149f2013-08-06 22:31:59 +000067 return reference_iterator(*this, nullptr);
68 }
69
Rui Ueyama16e543b2014-03-06 21:14:04 +000070 reference_iterator end() const override {
Rui Ueyama0ca149f2013-08-06 22:31:59 +000071 return reference_iterator(*this, nullptr);
72 }
73
Rui Ueyama16e543b2014-03-06 21:14:04 +000074 const Reference *derefIterator(const void *iter) const override {
Rui Ueyama0ca149f2013-08-06 22:31:59 +000075 return nullptr;
76 }
77
Rui Ueyama16e543b2014-03-06 21:14:04 +000078 void incrementIterator(const void *&iter) const override {}
Rui Ueyama0ca149f2013-08-06 22:31:59 +000079
80private:
81 const File &_file;
82 uint32_t _ordinal;
83};
84
85/// \brief Simple atom created by the GOT pass.
86class TestingGOTAtom : public DefinedAtom {
87public:
88 TestingGOTAtom(const File &F, const Atom &) : _file(F) {
89 static uint32_t lastOrdinal = 0;
90 _ordinal = lastOrdinal++;
91 }
92
Rui Ueyama16e543b2014-03-06 21:14:04 +000093 const File &file() const override { return _file; }
Rui Ueyama0ca149f2013-08-06 22:31:59 +000094
Rui Ueyama16e543b2014-03-06 21:14:04 +000095 StringRef name() const override { return StringRef(); }
Rui Ueyama0ca149f2013-08-06 22:31:59 +000096
Rui Ueyama16e543b2014-03-06 21:14:04 +000097 uint64_t ordinal() const override { return _ordinal; }
Rui Ueyama0ca149f2013-08-06 22:31:59 +000098
Rui Ueyama16e543b2014-03-06 21:14:04 +000099 uint64_t size() const override { return 0; }
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000100
Rui Ueyama16e543b2014-03-06 21:14:04 +0000101 Scope scope() const override { return DefinedAtom::scopeLinkageUnit; }
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000102
Rui Ueyama16e543b2014-03-06 21:14:04 +0000103 Interposable interposable() const override { return DefinedAtom::interposeNo; }
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000104
Rui Ueyama16e543b2014-03-06 21:14:04 +0000105 Merge merge() const override { return DefinedAtom::mergeNo; }
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000106
Rui Ueyama16e543b2014-03-06 21:14:04 +0000107 ContentType contentType() const override { return DefinedAtom::typeGOT; }
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000108
Rui Ueyama16e543b2014-03-06 21:14:04 +0000109 Alignment alignment() const override { return Alignment(3, 0); }
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000110
Rui Ueyama16e543b2014-03-06 21:14:04 +0000111 SectionChoice sectionChoice() const override {
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000112 return DefinedAtom::sectionBasedOnContent;
113 }
114
Rui Ueyama16e543b2014-03-06 21:14:04 +0000115 StringRef customSectionName() const override { return StringRef(); }
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000116
Rui Ueyama16e543b2014-03-06 21:14:04 +0000117 SectionPosition sectionPosition() const override { return sectionPositionAny; }
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000118
Rui Ueyama16e543b2014-03-06 21:14:04 +0000119 DeadStripKind deadStrip() const override {
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000120 return DefinedAtom::deadStripNormal;
121 }
122
Rui Ueyama16e543b2014-03-06 21:14:04 +0000123 ContentPermissions permissions() const override {
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000124 return DefinedAtom::permRW_;
125 }
126
Rui Ueyama16e543b2014-03-06 21:14:04 +0000127 ArrayRef<uint8_t> rawContent() const override { return ArrayRef<uint8_t>(); }
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000128
Rui Ueyama16e543b2014-03-06 21:14:04 +0000129 reference_iterator begin() const override {
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000130 return reference_iterator(*this, nullptr);
131 }
132
Rui Ueyama16e543b2014-03-06 21:14:04 +0000133 reference_iterator end() const override {
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000134 return reference_iterator(*this, nullptr);
135 }
136
Rui Ueyama16e543b2014-03-06 21:14:04 +0000137 const Reference *derefIterator(const void *iter) const override {
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000138 return nullptr;
139 }
140
Rui Ueyama16e543b2014-03-06 21:14:04 +0000141 void incrementIterator(const void *&iter) const override {}
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000142
143private:
144 const File &_file;
145 uint32_t _ordinal;
146};
147
Shankar Easwaran2bc24922013-10-29 05:12:14 +0000148class TestingPassFile : public SimpleFile {
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000149public:
Rui Ueyama170a1a82013-12-20 07:48:29 +0000150 TestingPassFile(const LinkingContext &ctx) : SimpleFile("Testing pass") {}
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000151
Rui Ueyama16e543b2014-03-06 21:14:04 +0000152 void addAtom(const Atom &atom) override {
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000153 if (const DefinedAtom *defAtom = dyn_cast<DefinedAtom>(&atom))
154 _definedAtoms._atoms.push_back(defAtom);
155 else
156 llvm_unreachable("atom has unknown definition kind");
157 }
158
Rui Ueyama16e543b2014-03-06 21:14:04 +0000159 DefinedAtomRange definedAtoms() override {
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000160 return range<std::vector<const DefinedAtom *>::iterator>(
161 _definedAtoms._atoms.begin(), _definedAtoms._atoms.end());
162 }
163
Rui Ueyama16e543b2014-03-06 21:14:04 +0000164 const atom_collection<DefinedAtom> &defined() const override {
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000165 return _definedAtoms;
166 }
Rui Ueyama16e543b2014-03-06 21:14:04 +0000167 const atom_collection<UndefinedAtom> &undefined() const override {
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000168 return _undefinedAtoms;
169 }
Rui Ueyama16e543b2014-03-06 21:14:04 +0000170 const atom_collection<SharedLibraryAtom> &sharedLibrary() const override {
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000171 return _sharedLibraryAtoms;
172 }
Rui Ueyama16e543b2014-03-06 21:14:04 +0000173 const atom_collection<AbsoluteAtom> &absolute() const override {
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000174 return _absoluteAtoms;
175 }
176
177private:
178 atom_collection_vector<DefinedAtom> _definedAtoms;
179 atom_collection_vector<UndefinedAtom> _undefinedAtoms;
180 atom_collection_vector<SharedLibraryAtom> _sharedLibraryAtoms;
181 atom_collection_vector<AbsoluteAtom> _absoluteAtoms;
182};
183
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000184
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000185
186} // anonymous namespace
187
188CoreLinkingContext::CoreLinkingContext() {}
189
Shankar Easwaran7f1146c2013-10-08 15:43:48 +0000190bool CoreLinkingContext::validateImpl(raw_ostream &) {
Shankar Easwaran7f1146c2013-10-08 15:43:48 +0000191 _writer = createWriterYAML(*this);
Rui Ueyama8db1edd2013-09-24 23:26:34 +0000192 return true;
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000193}
194
Shankar Easwaran2bc24922013-10-29 05:12:14 +0000195void CoreLinkingContext::addPasses(PassManager &pm) {
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000196 for (StringRef name : _passNames) {
197 if (name.equals("layout"))
Nico Rieckb9d84f42014-02-24 21:14:37 +0000198 pm.add(std::unique_ptr<Pass>(new LayoutPass(registry())));
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000199 else
200 llvm_unreachable("bad pass name");
201 }
202}
203
Shankar Easwaran7f1146c2013-10-08 15:43:48 +0000204Writer &CoreLinkingContext::writer() const { return *_writer; }