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"
|
|
|
|
|
|
|
|
#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++;
|
|
|
|
}
|
|
|
|
|
2013-08-07 06:31:59 +08:00
|
|
|
virtual const File &file() const { return _file; }
|
2013-04-05 02:59:24 +08:00
|
|
|
|
2013-08-07 06:31:59 +08:00
|
|
|
virtual StringRef name() const { return StringRef(); }
|
2013-04-05 02:59:24 +08:00
|
|
|
|
2013-08-07 06:31:59 +08:00
|
|
|
virtual uint64_t ordinal() const { return _ordinal; }
|
2013-04-05 02:59:24 +08:00
|
|
|
|
2013-08-07 06:31:59 +08:00
|
|
|
virtual uint64_t size() const { return 0; }
|
2013-04-05 02:59:24 +08:00
|
|
|
|
2013-08-07 06:31:59 +08:00
|
|
|
virtual Scope scope() const { return DefinedAtom::scopeLinkageUnit; }
|
2013-04-05 02:59:24 +08:00
|
|
|
|
2013-08-07 06:31:59 +08:00
|
|
|
virtual Interposable interposable() const { return DefinedAtom::interposeNo; }
|
2013-04-05 02:59:24 +08:00
|
|
|
|
2013-08-07 06:31:59 +08:00
|
|
|
virtual Merge merge() const { return DefinedAtom::mergeNo; }
|
2013-04-05 02:59:24 +08:00
|
|
|
|
2013-08-07 06:31:59 +08:00
|
|
|
virtual ContentType contentType() const { return DefinedAtom::typeStub; }
|
2013-04-05 02:59:24 +08:00
|
|
|
|
2013-08-07 06:31:59 +08:00
|
|
|
virtual Alignment alignment() const { return Alignment(0, 0); }
|
2013-04-05 02:59:24 +08:00
|
|
|
|
|
|
|
virtual SectionChoice sectionChoice() const {
|
|
|
|
return DefinedAtom::sectionBasedOnContent;
|
|
|
|
}
|
|
|
|
|
2013-08-07 06:31:59 +08:00
|
|
|
virtual StringRef customSectionName() const { return StringRef(); }
|
|
|
|
|
|
|
|
virtual SectionPosition sectionPosition() const { return sectionPositionAny; }
|
|
|
|
|
2013-04-05 02:59:24 +08:00
|
|
|
virtual DeadStripKind deadStrip() const {
|
|
|
|
return DefinedAtom::deadStripNormal;
|
|
|
|
}
|
|
|
|
|
2013-08-07 06:31:59 +08:00
|
|
|
virtual ContentPermissions permissions() const {
|
2013-04-05 02:59:24 +08:00
|
|
|
return DefinedAtom::permR_X;
|
|
|
|
}
|
|
|
|
|
2013-08-07 06:31:59 +08:00
|
|
|
virtual bool isAlias() const { return false; }
|
2013-04-05 02:59:24 +08:00
|
|
|
|
2013-08-07 06:31:59 +08:00
|
|
|
virtual ArrayRef<uint8_t> rawContent() const { return ArrayRef<uint8_t>(); }
|
2013-04-05 02:59:24 +08:00
|
|
|
|
|
|
|
virtual reference_iterator begin() const {
|
|
|
|
return reference_iterator(*this, nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual reference_iterator end() const {
|
|
|
|
return reference_iterator(*this, nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual const Reference *derefIterator(const void *iter) const {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2013-08-07 06:31:59 +08:00
|
|
|
virtual void incrementIterator(const void *&iter) const {}
|
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++;
|
|
|
|
}
|
|
|
|
|
2013-08-07 06:31:59 +08:00
|
|
|
virtual const File &file() const { return _file; }
|
2013-04-05 02:59:24 +08:00
|
|
|
|
2013-08-07 06:31:59 +08:00
|
|
|
virtual StringRef name() const { return StringRef(); }
|
2013-04-05 02:59:24 +08:00
|
|
|
|
2013-08-07 06:31:59 +08:00
|
|
|
virtual uint64_t ordinal() const { return _ordinal; }
|
2013-04-05 02:59:24 +08:00
|
|
|
|
2013-08-07 06:31:59 +08:00
|
|
|
virtual uint64_t size() const { return 0; }
|
2013-04-05 02:59:24 +08:00
|
|
|
|
2013-08-07 06:31:59 +08:00
|
|
|
virtual Scope scope() const { return DefinedAtom::scopeLinkageUnit; }
|
2013-04-05 02:59:24 +08:00
|
|
|
|
2013-08-07 06:31:59 +08:00
|
|
|
virtual Interposable interposable() const { return DefinedAtom::interposeNo; }
|
2013-04-05 02:59:24 +08:00
|
|
|
|
2013-08-07 06:31:59 +08:00
|
|
|
virtual Merge merge() const { return DefinedAtom::mergeNo; }
|
2013-04-05 02:59:24 +08:00
|
|
|
|
2013-08-07 06:31:59 +08:00
|
|
|
virtual ContentType contentType() const { return DefinedAtom::typeGOT; }
|
2013-04-05 02:59:24 +08:00
|
|
|
|
2013-08-07 06:31:59 +08:00
|
|
|
virtual Alignment alignment() const { return Alignment(3, 0); }
|
2013-04-05 02:59:24 +08:00
|
|
|
|
|
|
|
virtual SectionChoice sectionChoice() const {
|
|
|
|
return DefinedAtom::sectionBasedOnContent;
|
|
|
|
}
|
|
|
|
|
2013-08-07 06:31:59 +08:00
|
|
|
virtual StringRef customSectionName() const { return StringRef(); }
|
2013-04-05 02:59:24 +08:00
|
|
|
|
2013-08-07 06:31:59 +08:00
|
|
|
virtual SectionPosition sectionPosition() const { return sectionPositionAny; }
|
2013-04-05 02:59:24 +08:00
|
|
|
|
|
|
|
virtual DeadStripKind deadStrip() const {
|
|
|
|
return DefinedAtom::deadStripNormal;
|
|
|
|
}
|
|
|
|
|
2013-08-07 06:31:59 +08:00
|
|
|
virtual ContentPermissions permissions() const {
|
2013-04-05 02:59:24 +08:00
|
|
|
return DefinedAtom::permRW_;
|
|
|
|
}
|
|
|
|
|
2013-08-07 06:31:59 +08:00
|
|
|
virtual bool isAlias() const { return false; }
|
2013-04-05 02:59:24 +08:00
|
|
|
|
2013-08-07 06:31:59 +08:00
|
|
|
virtual ArrayRef<uint8_t> rawContent() const { return ArrayRef<uint8_t>(); }
|
2013-04-05 02:59:24 +08:00
|
|
|
|
|
|
|
virtual reference_iterator begin() const {
|
|
|
|
return reference_iterator(*this, nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual reference_iterator end() const {
|
|
|
|
return reference_iterator(*this, nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual const Reference *derefIterator(const void *iter) const {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2013-08-07 06:31:59 +08:00
|
|
|
virtual void incrementIterator(const void *&iter) const {}
|
2013-04-05 02:59:24 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
const File &_file;
|
|
|
|
uint32_t _ordinal;
|
|
|
|
};
|
|
|
|
|
|
|
|
class TestingPassFile : public MutableFile {
|
|
|
|
public:
|
2013-08-27 08:04:42 +08:00
|
|
|
TestingPassFile(const LinkingContext &ctx)
|
|
|
|
: MutableFile(ctx, "Testing pass") {}
|
2013-04-05 02:59:24 +08:00
|
|
|
|
|
|
|
virtual void addAtom(const Atom &atom) {
|
|
|
|
if (const DefinedAtom *defAtom = dyn_cast<DefinedAtom>(&atom))
|
|
|
|
_definedAtoms._atoms.push_back(defAtom);
|
|
|
|
else
|
|
|
|
llvm_unreachable("atom has unknown definition kind");
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual DefinedAtomRange definedAtoms() {
|
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
|
|
|
|
2013-04-05 02:59:24 +08:00
|
|
|
virtual const atom_collection<DefinedAtom> &defined() const {
|
|
|
|
return _definedAtoms;
|
|
|
|
}
|
|
|
|
virtual const atom_collection<UndefinedAtom> &undefined() const {
|
|
|
|
return _undefinedAtoms;
|
|
|
|
}
|
|
|
|
virtual const atom_collection<SharedLibraryAtom> &sharedLibrary() const {
|
|
|
|
return _sharedLibraryAtoms;
|
|
|
|
}
|
|
|
|
virtual const atom_collection<AbsoluteAtom> &absolute() const {
|
|
|
|
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
|
|
|
};
|
|
|
|
|
|
|
|
struct TestingKindMapping {
|
2013-08-07 06:31:59 +08:00
|
|
|
const char *string;
|
|
|
|
int32_t value;
|
|
|
|
bool isBranch;
|
|
|
|
bool isGotLoad;
|
|
|
|
bool isGotUse;
|
2013-04-05 02:59:24 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
//
|
|
|
|
// Table of fixup kinds in YAML documents used for testing
|
|
|
|
//
|
|
|
|
const TestingKindMapping sKinds[] = {
|
2013-08-07 06:31:59 +08:00
|
|
|
{ "in-group", -3, false, false, false },
|
|
|
|
{ "layout-after", -2, false, false, false },
|
|
|
|
{ "layout-before", -1, false, false, false },
|
|
|
|
{ "call32", 2, true, false, false }, { "pcrel32", 3, false, false, false },
|
|
|
|
{ "gotLoad32", 7, false, true, true }, { "gotUse32", 9, false, false, true },
|
|
|
|
{ "lea32wasGot", 8, false, false, false }, { nullptr, 0, false, false, false }
|
|
|
|
};
|
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
|
|
|
|
2013-08-07 06:31:59 +08:00
|
|
|
virtual bool noTextRelocs() { return true; }
|
2013-04-05 02:59:24 +08:00
|
|
|
|
|
|
|
virtual bool isCallSite(int32_t kind) {
|
|
|
|
for (const TestingKindMapping *p = sKinds; p->string != nullptr; ++p) {
|
|
|
|
if (kind == p->value)
|
|
|
|
return p->isBranch;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual const DefinedAtom *getStub(const Atom &target) {
|
|
|
|
const DefinedAtom *result = new TestingStubAtom(_file, target);
|
|
|
|
_file.addAtom(*result);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void addStubAtoms(MutableFile &mergedFile) {
|
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
|
|
|
|
2013-08-07 06:31:59 +08:00
|
|
|
virtual bool noTextRelocs() { return true; }
|
2013-04-05 02:59:24 +08:00
|
|
|
|
|
|
|
virtual bool isGOTAccess(int32_t kind, bool &canBypassGOT) {
|
|
|
|
for (const TestingKindMapping *p = sKinds; p->string != nullptr; ++p) {
|
|
|
|
if (kind == p->value) {
|
|
|
|
canBypassGOT = p->isGotLoad;
|
|
|
|
return p->isGotUse || p->isGotLoad;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void updateReferenceToGOT(const Reference *ref, bool targetIsNowGOT) {
|
|
|
|
if (targetIsNowGOT)
|
2013-08-07 06:31:59 +08:00
|
|
|
const_cast<Reference *>(ref)->setKind(3); // pcrel32
|
2013-04-05 02:59:24 +08:00
|
|
|
else
|
2013-08-07 06:31:59 +08:00
|
|
|
const_cast<Reference *>(ref)->setKind(8); // lea32wasGot
|
2013-04-05 02:59:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual const DefinedAtom *makeGOTEntry(const Atom &target) {
|
|
|
|
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 &) {
|
2013-10-07 10:47:09 +08:00
|
|
|
_reader = createReaderYAML(*this);
|
2013-10-08 23:43:48 +08:00
|
|
|
_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-08-07 06:31:59 +08:00
|
|
|
void CoreLinkingContext::addPasses(PassManager &pm) const {
|
2013-04-05 02:59:24 +08:00
|
|
|
for (StringRef name : _passNames) {
|
2013-08-07 06:31:59 +08:00
|
|
|
if (name.equals("layout"))
|
2013-04-05 02:59:24 +08:00
|
|
|
pm.add(std::unique_ptr<Pass>((new LayoutPass())));
|
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
|
|
|
|
2013-08-07 06:31:59 +08:00
|
|
|
ErrorOr<Reference::Kind>
|
|
|
|
CoreLinkingContext::relocKindFromString(StringRef str) const {
|
2013-04-05 02:59:24 +08:00
|
|
|
for (const TestingKindMapping *p = sKinds; p->string != nullptr; ++p) {
|
|
|
|
if (str.equals(p->string))
|
|
|
|
return p->value;
|
|
|
|
}
|
2013-10-09 08:57:22 +08:00
|
|
|
return make_error_code(YamlReaderError::illegal_value);
|
2013-04-05 02:59:24 +08:00
|
|
|
}
|
|
|
|
|
2013-08-07 06:31:59 +08:00
|
|
|
ErrorOr<std::string>
|
|
|
|
CoreLinkingContext::stringFromRelocKind(Reference::Kind kind) const {
|
2013-04-05 02:59:24 +08:00
|
|
|
for (const TestingKindMapping *p = sKinds; p->string != nullptr; ++p) {
|
|
|
|
if (kind == p->value)
|
|
|
|
return std::string(p->string);
|
|
|
|
}
|
2013-10-09 08:57:22 +08:00
|
|
|
return make_error_code(YamlReaderError::illegal_value);
|
2013-04-05 02:59:24 +08:00
|
|
|
}
|