2013-08-07 06:31:59 +08:00
|
|
|
//===- lib/ReaderWriter/CoreLinkingContext.cpp ----------------------------===//
|
2013-04-05 02:59:24 +08:00
|
|
|
//
|
|
|
|
// The LLVM Linker
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2013-08-07 06:31:59 +08:00
|
|
|
#include "lld/ReaderWriter/CoreLinkingContext.h"
|
2013-04-05 02:59:24 +08:00
|
|
|
|
|
|
|
#include "lld/Core/Pass.h"
|
|
|
|
#include "lld/Core/PassManager.h"
|
|
|
|
#include "lld/Passes/LayoutPass.h"
|
2013-10-29 13:12:14 +08:00
|
|
|
#include "lld/Passes/RoundTripYAMLPass.h"
|
|
|
|
#include "lld/ReaderWriter/Simple.h"
|
2013-04-05 02:59:24 +08:00
|
|
|
|
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
|
|
|
|
|
|
|
using namespace lld;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
/// \brief Simple atom created by the stubs pass.
|
|
|
|
class TestingStubAtom : public DefinedAtom {
|
|
|
|
public:
|
2013-08-07 06:31:59 +08:00
|
|
|
TestingStubAtom(const File &F, const Atom &) : _file(F) {
|
2013-04-05 02:59:24 +08:00
|
|
|
static uint32_t lastOrdinal = 0;
|
|
|
|
_ordinal = lastOrdinal++;
|
|
|
|
}
|
|
|
|
|
2014-03-07 05:14:04 +08:00
|
|
|
const File &file() const override { return _file; }
|
2013-04-05 02:59:24 +08:00
|
|
|
|
2014-03-07 05:14:04 +08:00
|
|
|
StringRef name() const override { return StringRef(); }
|
2013-04-05 02:59:24 +08:00
|
|
|
|
2014-03-07 05:14:04 +08:00
|
|
|
uint64_t ordinal() const override { return _ordinal; }
|
2013-04-05 02:59:24 +08:00
|
|
|
|
2014-03-07 05:14:04 +08:00
|
|
|
uint64_t size() const override { return 0; }
|
2013-04-05 02:59:24 +08:00
|
|
|
|
2014-03-07 05:14:04 +08:00
|
|
|
Scope scope() const override { return DefinedAtom::scopeLinkageUnit; }
|
2013-04-05 02:59:24 +08:00
|
|
|
|
2014-03-07 05:14:04 +08:00
|
|
|
Interposable interposable() const override { return DefinedAtom::interposeNo; }
|
2013-04-05 02:59:24 +08:00
|
|
|
|
2014-03-07 05:14:04 +08:00
|
|
|
Merge merge() const override { return DefinedAtom::mergeNo; }
|
2013-04-05 02:59:24 +08:00
|
|
|
|
2014-03-07 05:14:04 +08:00
|
|
|
ContentType contentType() const override { return DefinedAtom::typeStub; }
|
2013-04-05 02:59:24 +08:00
|
|
|
|
2014-03-07 05:14:04 +08:00
|
|
|
Alignment alignment() const override { return Alignment(0, 0); }
|
2013-04-05 02:59:24 +08:00
|
|
|
|
2014-03-07 05:14:04 +08:00
|
|
|
SectionChoice sectionChoice() const override {
|
2013-04-05 02:59:24 +08:00
|
|
|
return DefinedAtom::sectionBasedOnContent;
|
|
|
|
}
|
|
|
|
|
2014-03-07 05:14:04 +08:00
|
|
|
StringRef customSectionName() const override { return StringRef(); }
|
2013-08-07 06:31:59 +08:00
|
|
|
|
2014-03-07 05:14:04 +08:00
|
|
|
SectionPosition sectionPosition() const override { return sectionPositionAny; }
|
2013-08-07 06:31:59 +08:00
|
|
|
|
2014-03-07 05:14:04 +08:00
|
|
|
DeadStripKind deadStrip() const override {
|
2013-04-05 02:59:24 +08:00
|
|
|
return DefinedAtom::deadStripNormal;
|
|
|
|
}
|
|
|
|
|
2014-03-07 05:14:04 +08:00
|
|
|
ContentPermissions permissions() const override {
|
2013-04-05 02:59:24 +08:00
|
|
|
return DefinedAtom::permR_X;
|
|
|
|
}
|
|
|
|
|
2014-03-07 05:14:04 +08:00
|
|
|
bool isAlias() const override { return false; }
|
2013-04-05 02:59:24 +08:00
|
|
|
|
2014-03-07 05:14:04 +08:00
|
|
|
ArrayRef<uint8_t> rawContent() const override { return ArrayRef<uint8_t>(); }
|
2013-04-05 02:59:24 +08:00
|
|
|
|
2014-03-07 05:14:04 +08:00
|
|
|
reference_iterator begin() const override {
|
2013-04-05 02:59:24 +08:00
|
|
|
return reference_iterator(*this, nullptr);
|
|
|
|
}
|
|
|
|
|
2014-03-07 05:14:04 +08:00
|
|
|
reference_iterator end() const override {
|
2013-04-05 02:59:24 +08:00
|
|
|
return reference_iterator(*this, nullptr);
|
|
|
|
}
|
|
|
|
|
2014-03-07 05:14:04 +08:00
|
|
|
const Reference *derefIterator(const void *iter) const override {
|
2013-04-05 02:59:24 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-03-07 05:14:04 +08:00
|
|
|
void incrementIterator(const void *&iter) const override {}
|
2013-04-05 02:59:24 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
const File &_file;
|
|
|
|
uint32_t _ordinal;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// \brief Simple atom created by the GOT pass.
|
|
|
|
class TestingGOTAtom : public DefinedAtom {
|
|
|
|
public:
|
2013-08-07 06:31:59 +08:00
|
|
|
TestingGOTAtom(const File &F, const Atom &) : _file(F) {
|
2013-04-05 02:59:24 +08:00
|
|
|
static uint32_t lastOrdinal = 0;
|
|
|
|
_ordinal = lastOrdinal++;
|
|
|
|
}
|
|
|
|
|
2014-03-07 05:14:04 +08:00
|
|
|
const File &file() const override { return _file; }
|
2013-04-05 02:59:24 +08:00
|
|
|
|
2014-03-07 05:14:04 +08:00
|
|
|
StringRef name() const override { return StringRef(); }
|
2013-04-05 02:59:24 +08:00
|
|
|
|
2014-03-07 05:14:04 +08:00
|
|
|
uint64_t ordinal() const override { return _ordinal; }
|
2013-04-05 02:59:24 +08:00
|
|
|
|
2014-03-07 05:14:04 +08:00
|
|
|
uint64_t size() const override { return 0; }
|
2013-04-05 02:59:24 +08:00
|
|
|
|
2014-03-07 05:14:04 +08:00
|
|
|
Scope scope() const override { return DefinedAtom::scopeLinkageUnit; }
|
2013-04-05 02:59:24 +08:00
|
|
|
|
2014-03-07 05:14:04 +08:00
|
|
|
Interposable interposable() const override { return DefinedAtom::interposeNo; }
|
2013-04-05 02:59:24 +08:00
|
|
|
|
2014-03-07 05:14:04 +08:00
|
|
|
Merge merge() const override { return DefinedAtom::mergeNo; }
|
2013-04-05 02:59:24 +08:00
|
|
|
|
2014-03-07 05:14:04 +08:00
|
|
|
ContentType contentType() const override { return DefinedAtom::typeGOT; }
|
2013-04-05 02:59:24 +08:00
|
|
|
|
2014-03-07 05:14:04 +08:00
|
|
|
Alignment alignment() const override { return Alignment(3, 0); }
|
2013-04-05 02:59:24 +08:00
|
|
|
|
2014-03-07 05:14:04 +08:00
|
|
|
SectionChoice sectionChoice() const override {
|
2013-04-05 02:59:24 +08:00
|
|
|
return DefinedAtom::sectionBasedOnContent;
|
|
|
|
}
|
|
|
|
|
2014-03-07 05:14:04 +08:00
|
|
|
StringRef customSectionName() const override { return StringRef(); }
|
2013-04-05 02:59:24 +08:00
|
|
|
|
2014-03-07 05:14:04 +08:00
|
|
|
SectionPosition sectionPosition() const override { return sectionPositionAny; }
|
2013-04-05 02:59:24 +08:00
|
|
|
|
2014-03-07 05:14:04 +08:00
|
|
|
DeadStripKind deadStrip() const override {
|
2013-04-05 02:59:24 +08:00
|
|
|
return DefinedAtom::deadStripNormal;
|
|
|
|
}
|
|
|
|
|
2014-03-07 05:14:04 +08:00
|
|
|
ContentPermissions permissions() const override {
|
2013-04-05 02:59:24 +08:00
|
|
|
return DefinedAtom::permRW_;
|
|
|
|
}
|
|
|
|
|
2014-03-07 05:14:04 +08:00
|
|
|
bool isAlias() const override { return false; }
|
2013-04-05 02:59:24 +08:00
|
|
|
|
2014-03-07 05:14:04 +08:00
|
|
|
ArrayRef<uint8_t> rawContent() const override { return ArrayRef<uint8_t>(); }
|
2013-04-05 02:59:24 +08:00
|
|
|
|
2014-03-07 05:14:04 +08:00
|
|
|
reference_iterator begin() const override {
|
2013-04-05 02:59:24 +08:00
|
|
|
return reference_iterator(*this, nullptr);
|
|
|
|
}
|
|
|
|
|
2014-03-07 05:14:04 +08:00
|
|
|
reference_iterator end() const override {
|
2013-04-05 02:59:24 +08:00
|
|
|
return reference_iterator(*this, nullptr);
|
|
|
|
}
|
|
|
|
|
2014-03-07 05:14:04 +08:00
|
|
|
const Reference *derefIterator(const void *iter) const override {
|
2013-04-05 02:59:24 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-03-07 05:14:04 +08:00
|
|
|
void incrementIterator(const void *&iter) const override {}
|
2013-04-05 02:59:24 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
const File &_file;
|
|
|
|
uint32_t _ordinal;
|
|
|
|
};
|
|
|
|
|
2013-10-29 13:12:14 +08:00
|
|
|
class TestingPassFile : public SimpleFile {
|
2013-04-05 02:59:24 +08:00
|
|
|
public:
|
2013-12-20 15:48:29 +08:00
|
|
|
TestingPassFile(const LinkingContext &ctx) : SimpleFile("Testing pass") {}
|
2013-04-05 02:59:24 +08:00
|
|
|
|
2014-03-07 05:14:04 +08:00
|
|
|
void addAtom(const Atom &atom) override {
|
2013-04-05 02:59:24 +08:00
|
|
|
if (const DefinedAtom *defAtom = dyn_cast<DefinedAtom>(&atom))
|
|
|
|
_definedAtoms._atoms.push_back(defAtom);
|
|
|
|
else
|
|
|
|
llvm_unreachable("atom has unknown definition kind");
|
|
|
|
}
|
|
|
|
|
2014-03-07 05:14:04 +08:00
|
|
|
DefinedAtomRange definedAtoms() override {
|
2013-08-07 06:31:59 +08:00
|
|
|
return range<std::vector<const DefinedAtom *>::iterator>(
|
|
|
|
_definedAtoms._atoms.begin(), _definedAtoms._atoms.end());
|
2013-04-05 02:59:24 +08:00
|
|
|
}
|
2013-08-07 06:31:59 +08:00
|
|
|
|
2014-03-07 05:14:04 +08:00
|
|
|
const atom_collection<DefinedAtom> &defined() const override {
|
2013-04-05 02:59:24 +08:00
|
|
|
return _definedAtoms;
|
|
|
|
}
|
2014-03-07 05:14:04 +08:00
|
|
|
const atom_collection<UndefinedAtom> &undefined() const override {
|
2013-04-05 02:59:24 +08:00
|
|
|
return _undefinedAtoms;
|
|
|
|
}
|
2014-03-07 05:14:04 +08:00
|
|
|
const atom_collection<SharedLibraryAtom> &sharedLibrary() const override {
|
2013-04-05 02:59:24 +08:00
|
|
|
return _sharedLibraryAtoms;
|
|
|
|
}
|
2014-03-07 05:14:04 +08:00
|
|
|
const atom_collection<AbsoluteAtom> &absolute() const override {
|
2013-04-05 02:59:24 +08:00
|
|
|
return _absoluteAtoms;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2013-08-07 06:31:59 +08:00
|
|
|
atom_collection_vector<DefinedAtom> _definedAtoms;
|
|
|
|
atom_collection_vector<UndefinedAtom> _undefinedAtoms;
|
2013-04-05 02:59:24 +08:00
|
|
|
atom_collection_vector<SharedLibraryAtom> _sharedLibraryAtoms;
|
2013-08-07 06:31:59 +08:00
|
|
|
atom_collection_vector<AbsoluteAtom> _absoluteAtoms;
|
2013-04-05 02:59:24 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class TestingStubsPass : public StubsPass {
|
|
|
|
public:
|
2013-08-27 08:04:42 +08:00
|
|
|
TestingStubsPass(const LinkingContext &ctx) : _file(TestingPassFile(ctx)) {}
|
2013-04-05 02:59:24 +08:00
|
|
|
|
2014-03-07 05:14:04 +08:00
|
|
|
bool noTextRelocs() override { return true; }
|
2013-04-05 02:59:24 +08:00
|
|
|
|
2014-03-07 05:14:04 +08:00
|
|
|
bool isCallSite(const Reference &ref) override {
|
[lld] Introduce registry and Reference kind tuple
The main changes are in:
include/lld/Core/Reference.h
include/lld/ReaderWriter/Reader.h
Everything else is details to support the main change.
1) Registration based Readers
Previously, lld had a tangled interdependency with all the Readers. It would
have been impossible to make a streamlined linker (say for a JIT) which
just supported one file format and one architecture (no yaml, no archives, etc).
The old model also required a LinkingContext to read an object file, which
would have made .o inspection tools awkward.
The new model is that there is a global Registry object. You programmatically
register the Readers you want with the registry object. Whenever you need to
read/parse a file, you ask the registry to do it, and the registry tries each
registered reader.
For ease of use with the existing lld code base, there is one Registry
object inside the LinkingContext object.
2) Changing kind value to be a tuple
Beside Readers, the registry also keeps track of the mapping for Reference
Kind values to and from strings. Along with that, this patch also fixes
an ambiguity with the previous Reference::Kind values. The problem was that
we wanted to reuse existing relocation type values as Reference::Kind values.
But then how can the YAML write know how to convert a value to a string? The
fix is to change the 32-bit Reference::Kind into a tuple with an 8-bit namespace
(e.g. ELF, COFFF, etc), an 8-bit architecture (e.g. x86_64, PowerPC, etc), and
a 16-bit value. This tuple system allows conversion to and from strings with
no ambiguities.
llvm-svn: 197727
2013-12-20 05:58:00 +08:00
|
|
|
if (ref.kindNamespace() != Reference::KindNamespace::testing)
|
|
|
|
return false;
|
|
|
|
return (ref.kindValue() == CoreLinkingContext::TEST_RELOC_CALL32);
|
2013-04-05 02:59:24 +08:00
|
|
|
}
|
|
|
|
|
2014-03-07 05:14:04 +08:00
|
|
|
const DefinedAtom *getStub(const Atom &target) override {
|
2013-04-05 02:59:24 +08:00
|
|
|
const DefinedAtom *result = new TestingStubAtom(_file, target);
|
|
|
|
_file.addAtom(*result);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2014-03-07 05:14:04 +08:00
|
|
|
void addStubAtoms(MutableFile &mergedFile) override {
|
2013-08-07 06:31:59 +08:00
|
|
|
for (const DefinedAtom *stub : _file.defined()) {
|
2013-04-05 02:59:24 +08:00
|
|
|
mergedFile.addAtom(*stub);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
TestingPassFile _file;
|
|
|
|
};
|
|
|
|
|
|
|
|
class TestingGOTPass : public GOTPass {
|
|
|
|
public:
|
2013-08-27 08:04:42 +08:00
|
|
|
TestingGOTPass(const LinkingContext &ctx) : _file(TestingPassFile(ctx)) {}
|
2013-04-05 02:59:24 +08:00
|
|
|
|
2014-03-07 05:14:04 +08:00
|
|
|
bool noTextRelocs() override { return true; }
|
2013-04-05 02:59:24 +08:00
|
|
|
|
2014-03-07 05:14:04 +08:00
|
|
|
bool isGOTAccess(const Reference &ref, bool &canBypassGOT) override {
|
[lld] Introduce registry and Reference kind tuple
The main changes are in:
include/lld/Core/Reference.h
include/lld/ReaderWriter/Reader.h
Everything else is details to support the main change.
1) Registration based Readers
Previously, lld had a tangled interdependency with all the Readers. It would
have been impossible to make a streamlined linker (say for a JIT) which
just supported one file format and one architecture (no yaml, no archives, etc).
The old model also required a LinkingContext to read an object file, which
would have made .o inspection tools awkward.
The new model is that there is a global Registry object. You programmatically
register the Readers you want with the registry object. Whenever you need to
read/parse a file, you ask the registry to do it, and the registry tries each
registered reader.
For ease of use with the existing lld code base, there is one Registry
object inside the LinkingContext object.
2) Changing kind value to be a tuple
Beside Readers, the registry also keeps track of the mapping for Reference
Kind values to and from strings. Along with that, this patch also fixes
an ambiguity with the previous Reference::Kind values. The problem was that
we wanted to reuse existing relocation type values as Reference::Kind values.
But then how can the YAML write know how to convert a value to a string? The
fix is to change the 32-bit Reference::Kind into a tuple with an 8-bit namespace
(e.g. ELF, COFFF, etc), an 8-bit architecture (e.g. x86_64, PowerPC, etc), and
a 16-bit value. This tuple system allows conversion to and from strings with
no ambiguities.
llvm-svn: 197727
2013-12-20 05:58:00 +08:00
|
|
|
if (ref.kindNamespace() != Reference::KindNamespace::testing)
|
|
|
|
return false;
|
|
|
|
switch (ref.kindValue()) {
|
|
|
|
case CoreLinkingContext::TEST_RELOC_GOT_LOAD32:
|
|
|
|
canBypassGOT = true;
|
|
|
|
return true;
|
|
|
|
case CoreLinkingContext::TEST_RELOC_GOT_USE32:
|
|
|
|
canBypassGOT = false;
|
|
|
|
return true;
|
2013-04-05 02:59:24 +08:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-03-07 05:14:04 +08:00
|
|
|
void updateReferenceToGOT(const Reference *ref, bool targetIsNowGOT) override {
|
2013-12-20 15:48:29 +08:00
|
|
|
const_cast<Reference *>(ref)->setKindValue(
|
|
|
|
targetIsNowGOT ? CoreLinkingContext::TEST_RELOC_PCREL32
|
|
|
|
: CoreLinkingContext::TEST_RELOC_LEA32_WAS_GOT);
|
|
|
|
}
|
2013-04-05 02:59:24 +08:00
|
|
|
|
2014-03-07 05:14:04 +08:00
|
|
|
const DefinedAtom *makeGOTEntry(const Atom &target) override {
|
2013-04-05 02:59:24 +08:00
|
|
|
return new TestingGOTAtom(_file, target);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
TestingPassFile _file;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
2013-08-07 06:31:59 +08:00
|
|
|
CoreLinkingContext::CoreLinkingContext() {}
|
2013-04-05 02:59:24 +08:00
|
|
|
|
2013-10-08 23:43:48 +08:00
|
|
|
bool CoreLinkingContext::validateImpl(raw_ostream &) {
|
|
|
|
_writer = createWriterYAML(*this);
|
2013-09-25 07:26:34 +08:00
|
|
|
return true;
|
2013-06-11 20:36:05 +08:00
|
|
|
}
|
2013-04-05 02:59:24 +08:00
|
|
|
|
2013-10-29 13:12:14 +08:00
|
|
|
void CoreLinkingContext::addPasses(PassManager &pm) {
|
2013-04-05 02:59:24 +08:00
|
|
|
for (StringRef name : _passNames) {
|
2013-08-07 06:31:59 +08:00
|
|
|
if (name.equals("layout"))
|
2014-02-25 05:14:37 +08:00
|
|
|
pm.add(std::unique_ptr<Pass>(new LayoutPass(registry())));
|
2013-08-07 06:31:59 +08:00
|
|
|
else if (name.equals("GOT"))
|
2013-04-05 02:59:24 +08:00
|
|
|
pm.add(std::unique_ptr<Pass>(new TestingGOTPass(*this)));
|
2013-08-07 06:31:59 +08:00
|
|
|
else if (name.equals("stubs"))
|
2013-04-05 02:59:24 +08:00
|
|
|
pm.add(std::unique_ptr<Pass>(new TestingStubsPass(*this)));
|
|
|
|
else
|
|
|
|
llvm_unreachable("bad pass name");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-08 23:43:48 +08:00
|
|
|
Writer &CoreLinkingContext::writer() const { return *_writer; }
|
2013-04-05 02:59:24 +08:00
|
|
|
|