Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 1 | //===- 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 | |
| 18 | using namespace lld; |
| 19 | |
| 20 | namespace { |
| 21 | |
| 22 | /// \brief Simple atom created by the stubs pass. |
| 23 | class TestingStubAtom : public DefinedAtom { |
| 24 | public: |
| 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 | |
| 82 | private: |
| 83 | const File &_file; |
| 84 | uint32_t _ordinal; |
| 85 | }; |
| 86 | |
| 87 | /// \brief Simple atom created by the GOT pass. |
| 88 | class TestingGOTAtom : public DefinedAtom { |
| 89 | public: |
| 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 | |
| 147 | private: |
| 148 | const File &_file; |
| 149 | uint32_t _ordinal; |
| 150 | }; |
| 151 | |
| 152 | class TestingPassFile : public MutableFile { |
| 153 | public: |
Rui Ueyama | 352e730 | 2013-08-27 00:04:42 +0000 | [diff] [blame] | 154 | TestingPassFile(const LinkingContext &ctx) |
| 155 | : MutableFile(ctx, "Testing pass") {} |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 156 | |
| 157 | virtual void addAtom(const Atom &atom) { |
| 158 | if (const DefinedAtom *defAtom = dyn_cast<DefinedAtom>(&atom)) |
| 159 | _definedAtoms._atoms.push_back(defAtom); |
| 160 | else |
| 161 | llvm_unreachable("atom has unknown definition kind"); |
| 162 | } |
| 163 | |
| 164 | virtual DefinedAtomRange definedAtoms() { |
| 165 | return range<std::vector<const DefinedAtom *>::iterator>( |
| 166 | _definedAtoms._atoms.begin(), _definedAtoms._atoms.end()); |
| 167 | } |
| 168 | |
| 169 | virtual const atom_collection<DefinedAtom> &defined() const { |
| 170 | return _definedAtoms; |
| 171 | } |
| 172 | virtual const atom_collection<UndefinedAtom> &undefined() const { |
| 173 | return _undefinedAtoms; |
| 174 | } |
| 175 | virtual const atom_collection<SharedLibraryAtom> &sharedLibrary() const { |
| 176 | return _sharedLibraryAtoms; |
| 177 | } |
| 178 | virtual const atom_collection<AbsoluteAtom> &absolute() const { |
| 179 | return _absoluteAtoms; |
| 180 | } |
| 181 | |
| 182 | private: |
| 183 | atom_collection_vector<DefinedAtom> _definedAtoms; |
| 184 | atom_collection_vector<UndefinedAtom> _undefinedAtoms; |
| 185 | atom_collection_vector<SharedLibraryAtom> _sharedLibraryAtoms; |
| 186 | atom_collection_vector<AbsoluteAtom> _absoluteAtoms; |
| 187 | }; |
| 188 | |
| 189 | struct TestingKindMapping { |
| 190 | const char *string; |
| 191 | int32_t value; |
| 192 | bool isBranch; |
| 193 | bool isGotLoad; |
| 194 | bool isGotUse; |
| 195 | }; |
| 196 | |
| 197 | // |
| 198 | // Table of fixup kinds in YAML documents used for testing |
| 199 | // |
| 200 | const TestingKindMapping sKinds[] = { |
| 201 | { "in-group", -3, false, false, false }, |
| 202 | { "layout-after", -2, false, false, false }, |
| 203 | { "layout-before", -1, false, false, false }, |
| 204 | { "call32", 2, true, false, false }, { "pcrel32", 3, false, false, false }, |
| 205 | { "gotLoad32", 7, false, true, true }, { "gotUse32", 9, false, false, true }, |
| 206 | { "lea32wasGot", 8, false, false, false }, { nullptr, 0, false, false, false } |
| 207 | }; |
| 208 | |
| 209 | class TestingStubsPass : public StubsPass { |
| 210 | public: |
Rui Ueyama | 352e730 | 2013-08-27 00:04:42 +0000 | [diff] [blame] | 211 | TestingStubsPass(const LinkingContext &ctx) : _file(TestingPassFile(ctx)) {} |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 212 | |
| 213 | virtual bool noTextRelocs() { return true; } |
| 214 | |
| 215 | virtual bool isCallSite(int32_t kind) { |
| 216 | for (const TestingKindMapping *p = sKinds; p->string != nullptr; ++p) { |
| 217 | if (kind == p->value) |
| 218 | return p->isBranch; |
| 219 | } |
| 220 | return false; |
| 221 | } |
| 222 | |
| 223 | virtual const DefinedAtom *getStub(const Atom &target) { |
| 224 | const DefinedAtom *result = new TestingStubAtom(_file, target); |
| 225 | _file.addAtom(*result); |
| 226 | return result; |
| 227 | } |
| 228 | |
| 229 | virtual void addStubAtoms(MutableFile &mergedFile) { |
| 230 | for (const DefinedAtom *stub : _file.defined()) { |
| 231 | mergedFile.addAtom(*stub); |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | private: |
| 236 | TestingPassFile _file; |
| 237 | }; |
| 238 | |
| 239 | class TestingGOTPass : public GOTPass { |
| 240 | public: |
Rui Ueyama | 352e730 | 2013-08-27 00:04:42 +0000 | [diff] [blame] | 241 | TestingGOTPass(const LinkingContext &ctx) : _file(TestingPassFile(ctx)) {} |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 242 | |
| 243 | virtual bool noTextRelocs() { return true; } |
| 244 | |
| 245 | virtual bool isGOTAccess(int32_t kind, bool &canBypassGOT) { |
| 246 | for (const TestingKindMapping *p = sKinds; p->string != nullptr; ++p) { |
| 247 | if (kind == p->value) { |
| 248 | canBypassGOT = p->isGotLoad; |
| 249 | return p->isGotUse || p->isGotLoad; |
| 250 | } |
| 251 | } |
| 252 | return false; |
| 253 | } |
| 254 | |
| 255 | virtual void updateReferenceToGOT(const Reference *ref, bool targetIsNowGOT) { |
| 256 | if (targetIsNowGOT) |
| 257 | const_cast<Reference *>(ref)->setKind(3); // pcrel32 |
| 258 | else |
| 259 | const_cast<Reference *>(ref)->setKind(8); // lea32wasGot |
| 260 | } |
| 261 | |
| 262 | virtual const DefinedAtom *makeGOTEntry(const Atom &target) { |
| 263 | return new TestingGOTAtom(_file, target); |
| 264 | } |
| 265 | |
| 266 | private: |
| 267 | TestingPassFile _file; |
| 268 | }; |
| 269 | |
| 270 | } // anonymous namespace |
| 271 | |
| 272 | CoreLinkingContext::CoreLinkingContext() {} |
| 273 | |
| 274 | bool CoreLinkingContext::validateImpl(raw_ostream &diagnostics) { |
Shankar Easwaran | a96f3a3 | 2013-10-07 02:47:09 +0000 | [diff] [blame^] | 275 | _reader = createReaderYAML(*this); |
Rui Ueyama | 8db1edd | 2013-09-24 23:26:34 +0000 | [diff] [blame] | 276 | return true; |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | void CoreLinkingContext::addPasses(PassManager &pm) const { |
| 280 | for (StringRef name : _passNames) { |
| 281 | if (name.equals("layout")) |
| 282 | pm.add(std::unique_ptr<Pass>((new LayoutPass()))); |
| 283 | else if (name.equals("GOT")) |
| 284 | pm.add(std::unique_ptr<Pass>(new TestingGOTPass(*this))); |
| 285 | else if (name.equals("stubs")) |
| 286 | pm.add(std::unique_ptr<Pass>(new TestingStubsPass(*this))); |
| 287 | else |
| 288 | llvm_unreachable("bad pass name"); |
| 289 | } |
| 290 | } |
| 291 | |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 292 | Writer &CoreLinkingContext::writer() const { |
| 293 | if (!_writer) |
| 294 | _writer = createWriterYAML(*this); |
| 295 | return *_writer; |
| 296 | } |
| 297 | |
| 298 | ErrorOr<Reference::Kind> |
| 299 | CoreLinkingContext::relocKindFromString(StringRef str) const { |
| 300 | for (const TestingKindMapping *p = sKinds; p->string != nullptr; ++p) { |
| 301 | if (str.equals(p->string)) |
| 302 | return p->value; |
| 303 | } |
| 304 | return make_error_code(yaml_reader_error::illegal_value); |
| 305 | } |
| 306 | |
| 307 | ErrorOr<std::string> |
| 308 | CoreLinkingContext::stringFromRelocKind(Reference::Kind kind) const { |
| 309 | for (const TestingKindMapping *p = sKinds; p->string != nullptr; ++p) { |
| 310 | if (kind == p->value) |
| 311 | return std::string(p->string); |
| 312 | } |
| 313 | return make_error_code(yaml_reader_error::illegal_value); |
| 314 | } |