2013-08-07 06:31:59 +08:00
|
|
|
//===- lib/ReaderWriter/ELF/ELFLinkingContext.cpp -------------------------===//
|
2013-01-22 10:15:30 +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/ELFLinkingContext.h"
|
2013-01-22 10:15:30 +08:00
|
|
|
|
2013-10-26 07:59:06 +08:00
|
|
|
#include "ArrayOrderPass.h"
|
2014-01-27 07:49:07 +08:00
|
|
|
#include "ELFFile.h"
|
2013-01-30 06:03:39 +08:00
|
|
|
#include "TargetHandler.h"
|
|
|
|
#include "Targets.h"
|
2013-01-30 03:53:41 +08:00
|
|
|
|
2013-05-07 08:28:07 +08:00
|
|
|
#include "lld/Core/Instrumentation.h"
|
2013-02-08 04:16:12 +08:00
|
|
|
#include "lld/Passes/LayoutPass.h"
|
2013-10-29 13:12:14 +08:00
|
|
|
#include "lld/Passes/RoundTripYAMLPass.h"
|
2013-01-22 10:15:30 +08:00
|
|
|
|
|
|
|
#include "llvm/ADT/Triple.h"
|
|
|
|
#include "llvm/Support/ELF.h"
|
2013-03-01 08:03:36 +08:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2013-04-05 02:59:24 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
2013-01-22 10:15:30 +08:00
|
|
|
|
|
|
|
namespace lld {
|
2013-08-31 13:27:38 +08:00
|
|
|
|
2014-03-29 03:02:06 +08:00
|
|
|
class CommandLineAbsoluteAtom : public AbsoluteAtom {
|
|
|
|
public:
|
|
|
|
CommandLineAbsoluteAtom(const File &file, StringRef name, uint64_t value)
|
|
|
|
: _file(file), _name(name), _value(value) {}
|
|
|
|
|
|
|
|
const File &file() const override { return _file; }
|
|
|
|
StringRef name() const override { return _name; }
|
|
|
|
uint64_t value() const override { return _value; }
|
|
|
|
Scope scope() const override { return scopeGlobal; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
const File &_file;
|
|
|
|
StringRef _name;
|
|
|
|
uint64_t _value;
|
|
|
|
};
|
|
|
|
|
2014-04-17 04:58:57 +08:00
|
|
|
// An AliasAtom is a zero-size atom representing an alias for other atom. It has
|
|
|
|
// a LayoutAfter reference to the target atom, so that this atom and the target
|
|
|
|
// atom will be layed out at the same location in the final result. Initially
|
|
|
|
// the target atom is an undefined atom. Resolver will replace it with a defined
|
|
|
|
// one.
|
|
|
|
//
|
|
|
|
// It does not have attributes itself. Most member function calls are forwarded
|
|
|
|
// to the target atom.
|
|
|
|
class AliasAtom : public SimpleDefinedAtom {
|
|
|
|
public:
|
|
|
|
AliasAtom(const File &file, StringRef name)
|
|
|
|
: SimpleDefinedAtom(file), _target(nullptr), _name(name) {}
|
|
|
|
|
|
|
|
StringRef name() const override { return _name; }
|
|
|
|
uint64_t size() const override { return 0; }
|
|
|
|
ArrayRef<uint8_t> rawContent() const override { return ArrayRef<uint8_t>(); }
|
|
|
|
|
|
|
|
Scope scope() const override {
|
|
|
|
getTarget();
|
|
|
|
return _target ? _target->scope() : scopeLinkageUnit;
|
|
|
|
}
|
|
|
|
|
|
|
|
Merge merge() const override {
|
|
|
|
getTarget();
|
|
|
|
return _target ? _target->merge() : mergeNo;
|
|
|
|
}
|
|
|
|
|
|
|
|
ContentType contentType() const override {
|
|
|
|
getTarget();
|
|
|
|
return _target ? _target->contentType() : typeUnknown;
|
|
|
|
}
|
|
|
|
|
|
|
|
Interposable interposable() const override {
|
|
|
|
getTarget();
|
|
|
|
return _target ? _target->interposable() : interposeNo;
|
|
|
|
}
|
|
|
|
|
|
|
|
SectionChoice sectionChoice() const override {
|
|
|
|
getTarget();
|
|
|
|
return _target ? _target->sectionChoice() : sectionBasedOnContent;
|
|
|
|
}
|
|
|
|
|
|
|
|
StringRef customSectionName() const override {
|
|
|
|
getTarget();
|
|
|
|
return _target ? _target->customSectionName() : StringRef("");
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void getTarget() const {
|
|
|
|
if (_target)
|
|
|
|
return;
|
|
|
|
for (const Reference *r : *this) {
|
|
|
|
if (r->kindNamespace() == lld::Reference::KindNamespace::all &&
|
|
|
|
r->kindValue() == lld::Reference::kindLayoutAfter) {
|
|
|
|
_target = dyn_cast<DefinedAtom>(r->target());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mutable const DefinedAtom *_target;
|
|
|
|
StringRef _name;
|
|
|
|
};
|
|
|
|
|
2013-08-31 13:27:38 +08:00
|
|
|
class CommandLineUndefinedAtom : public SimpleUndefinedAtom {
|
|
|
|
public:
|
|
|
|
CommandLineUndefinedAtom(const File &f, StringRef name)
|
|
|
|
: SimpleUndefinedAtom(f, name) {}
|
|
|
|
|
2014-03-07 05:14:04 +08:00
|
|
|
CanBeNull canBeNull() const override {
|
2013-08-31 13:27:38 +08:00
|
|
|
return CanBeNull::canBeNullAtBuildtime;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-12-20 15:48:29 +08:00
|
|
|
ELFLinkingContext::ELFLinkingContext(
|
|
|
|
llvm::Triple triple, std::unique_ptr<TargetHandlerBase> targetHandler)
|
2013-09-23 12:24:15 +08:00
|
|
|
: _outputELFType(elf::ET_EXEC), _triple(triple),
|
2013-08-07 06:31:59 +08:00
|
|
|
_targetHandler(std::move(targetHandler)), _baseAddress(0),
|
2013-09-23 12:24:15 +08:00
|
|
|
_isStaticExecutable(false), _noInhibitExec(false),
|
2013-08-07 06:31:59 +08:00
|
|
|
_mergeCommonStrings(false), _runLayoutPass(true),
|
2013-11-25 12:28:57 +08:00
|
|
|
_useShlibUndefines(true), _dynamicLinkerArg(false),
|
2013-09-04 00:29:02 +08:00
|
|
|
_noAllowDynamicLibraries(false), _outputMagic(OutputMagic::DEFAULT),
|
|
|
|
_sysrootPath("") {}
|
2013-04-05 02:59:24 +08:00
|
|
|
|
2013-08-07 06:31:59 +08:00
|
|
|
bool ELFLinkingContext::is64Bits() const { return getTriple().isArch64Bit(); }
|
2013-04-05 02:59:24 +08:00
|
|
|
|
2013-08-07 06:31:59 +08:00
|
|
|
bool ELFLinkingContext::isLittleEndian() const {
|
2013-04-05 02:59:24 +08:00
|
|
|
// TODO: Do this properly. It is not defined purely by arch.
|
|
|
|
return true;
|
2013-01-22 10:15:30 +08:00
|
|
|
}
|
|
|
|
|
2013-10-29 13:12:14 +08:00
|
|
|
void ELFLinkingContext::addPasses(PassManager &pm) {
|
2013-04-05 02:59:24 +08:00
|
|
|
if (_runLayoutPass)
|
2014-02-25 05:14:37 +08:00
|
|
|
pm.add(std::unique_ptr<Pass>(new LayoutPass(registry())));
|
2013-10-26 07:59:06 +08:00
|
|
|
pm.add(std::unique_ptr<Pass>(new elf::ArrayOrderPass()));
|
2013-02-08 04:16:12 +08:00
|
|
|
}
|
|
|
|
|
2013-08-07 06:31:59 +08:00
|
|
|
uint16_t ELFLinkingContext::getOutputMachine() const {
|
2013-01-22 10:15:30 +08:00
|
|
|
switch (getTriple().getArch()) {
|
|
|
|
case llvm::Triple::x86:
|
|
|
|
return llvm::ELF::EM_386;
|
|
|
|
case llvm::Triple::x86_64:
|
|
|
|
return llvm::ELF::EM_X86_64;
|
|
|
|
case llvm::Triple::hexagon:
|
|
|
|
return llvm::ELF::EM_HEXAGON;
|
2013-12-15 20:57:28 +08:00
|
|
|
case llvm::Triple::mipsel:
|
|
|
|
return llvm::ELF::EM_MIPS;
|
2013-01-22 10:15:30 +08:00
|
|
|
case llvm::Triple::ppc:
|
|
|
|
return llvm::ELF::EM_PPC;
|
|
|
|
default:
|
|
|
|
llvm_unreachable("Unhandled arch");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-31 13:27:38 +08:00
|
|
|
StringRef ELFLinkingContext::entrySymbolName() const {
|
2013-09-23 12:24:15 +08:00
|
|
|
if (_outputELFType == elf::ET_EXEC && _entrySymbolName.empty())
|
2013-08-31 13:27:38 +08:00
|
|
|
return "_start";
|
|
|
|
return _entrySymbolName;
|
|
|
|
}
|
2013-04-05 11:50:15 +08:00
|
|
|
|
2013-08-31 13:27:38 +08:00
|
|
|
bool ELFLinkingContext::validateImpl(raw_ostream &diagnostics) {
|
2013-09-23 12:24:15 +08:00
|
|
|
switch (outputFileType()) {
|
|
|
|
case LinkingContext::OutputFileType::YAML:
|
|
|
|
_writer = createWriterYAML(*this);
|
|
|
|
break;
|
|
|
|
case LinkingContext::OutputFileType::Native:
|
|
|
|
llvm_unreachable("Unimplemented");
|
|
|
|
break;
|
|
|
|
default:
|
2014-01-27 09:20:53 +08:00
|
|
|
_writer = createWriterELF(this->targetHandler());
|
2013-09-23 12:24:15 +08:00
|
|
|
break;
|
|
|
|
}
|
2013-09-25 07:26:34 +08:00
|
|
|
return true;
|
2013-04-05 02:59:24 +08:00
|
|
|
}
|
|
|
|
|
2013-08-07 06:31:59 +08:00
|
|
|
bool ELFLinkingContext::isDynamic() const {
|
2013-09-23 12:24:15 +08:00
|
|
|
switch (_outputELFType) {
|
2013-04-05 02:59:24 +08:00
|
|
|
case llvm::ELF::ET_EXEC:
|
2013-05-17 01:53:34 +08:00
|
|
|
return !_isStaticExecutable;
|
2013-04-05 02:59:24 +08:00
|
|
|
case llvm::ELF::ET_DYN:
|
|
|
|
return true;
|
2013-03-01 08:03:36 +08:00
|
|
|
}
|
2013-04-05 02:59:24 +08:00
|
|
|
return false;
|
|
|
|
}
|
2013-03-01 08:03:36 +08:00
|
|
|
|
2013-08-07 06:31:59 +08:00
|
|
|
bool ELFLinkingContext::isRelativeReloc(const Reference &) const {
|
|
|
|
return false;
|
|
|
|
}
|
2013-05-17 13:10:30 +08:00
|
|
|
|
2013-08-07 06:31:59 +08:00
|
|
|
Writer &ELFLinkingContext::writer() const { return *_writer; }
|
2013-04-05 02:59:24 +08:00
|
|
|
|
2013-08-07 06:31:59 +08:00
|
|
|
std::unique_ptr<ELFLinkingContext>
|
|
|
|
ELFLinkingContext::create(llvm::Triple triple) {
|
2013-04-05 02:59:24 +08:00
|
|
|
switch (triple.getArch()) {
|
2013-01-22 10:15:30 +08:00
|
|
|
case llvm::Triple::x86:
|
2013-08-07 06:31:59 +08:00
|
|
|
return std::unique_ptr<ELFLinkingContext>(
|
|
|
|
new lld::elf::X86LinkingContext(triple));
|
2013-01-22 10:15:30 +08:00
|
|
|
case llvm::Triple::x86_64:
|
2013-08-07 06:31:59 +08:00
|
|
|
return std::unique_ptr<ELFLinkingContext>(
|
|
|
|
new lld::elf::X86_64LinkingContext(triple));
|
2013-01-22 10:15:30 +08:00
|
|
|
case llvm::Triple::hexagon:
|
2013-08-07 06:31:59 +08:00
|
|
|
return std::unique_ptr<ELFLinkingContext>(
|
|
|
|
new lld::elf::HexagonLinkingContext(triple));
|
2013-12-15 20:57:28 +08:00
|
|
|
case llvm::Triple::mipsel:
|
|
|
|
return std::unique_ptr<ELFLinkingContext>(
|
|
|
|
new lld::elf::MipsLinkingContext(triple));
|
2013-01-22 10:15:30 +08:00
|
|
|
case llvm::Triple::ppc:
|
2013-08-07 06:31:59 +08:00
|
|
|
return std::unique_ptr<ELFLinkingContext>(
|
|
|
|
new lld::elf::PPCLinkingContext(triple));
|
2013-01-22 10:15:30 +08:00
|
|
|
default:
|
2013-05-17 01:53:34 +08:00
|
|
|
return nullptr;
|
2013-01-22 10:15:30 +08:00
|
|
|
}
|
|
|
|
}
|
2013-01-28 12:15:44 +08:00
|
|
|
|
2014-05-01 03:03:51 +08:00
|
|
|
static void buildSearchPath(SmallString<128> &path, StringRef dir,
|
|
|
|
StringRef sysRoot) {
|
|
|
|
if (!dir.startswith("=/"))
|
|
|
|
path.assign(dir);
|
|
|
|
else {
|
|
|
|
path.assign(sysRoot);
|
|
|
|
path.append(dir.substr(1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-25 11:55:34 +08:00
|
|
|
ErrorOr<StringRef> ELFLinkingContext::searchLibrary(StringRef libName) const {
|
2013-04-05 11:50:15 +08:00
|
|
|
bool foundFile = false;
|
|
|
|
StringRef pathref;
|
2013-09-26 06:12:14 +08:00
|
|
|
SmallString<128> path;
|
2013-11-25 11:55:34 +08:00
|
|
|
for (StringRef dir : _inputSearchPaths) {
|
2013-04-05 11:50:15 +08:00
|
|
|
// Search for dynamic library
|
|
|
|
if (!_isStaticExecutable) {
|
2014-05-01 03:03:51 +08:00
|
|
|
buildSearchPath(path, dir, _sysrootPath);
|
2013-09-26 06:12:14 +08:00
|
|
|
llvm::sys::path::append(path, Twine("lib") + libName + ".so");
|
|
|
|
pathref = path.str();
|
2013-04-05 11:50:15 +08:00
|
|
|
if (llvm::sys::fs::exists(pathref)) {
|
|
|
|
foundFile = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Search for static libraries too
|
|
|
|
if (!foundFile) {
|
2014-05-01 03:03:51 +08:00
|
|
|
buildSearchPath(path, dir, _sysrootPath);
|
2013-09-26 06:12:14 +08:00
|
|
|
llvm::sys::path::append(path, Twine("lib") + libName + ".a");
|
|
|
|
pathref = path.str();
|
2013-04-05 11:50:15 +08:00
|
|
|
if (llvm::sys::fs::exists(pathref)) {
|
|
|
|
foundFile = true;
|
|
|
|
}
|
|
|
|
}
|
2013-08-22 06:57:10 +08:00
|
|
|
if (foundFile)
|
2013-10-08 23:43:48 +08:00
|
|
|
return StringRef(*new (_allocator) std::string(pathref));
|
2013-04-05 02:59:24 +08:00
|
|
|
}
|
2013-09-04 06:44:37 +08:00
|
|
|
if (!llvm::sys::fs::exists(libName))
|
|
|
|
return llvm::make_error_code(llvm::errc::no_such_file_or_directory);
|
|
|
|
|
2013-09-26 06:12:14 +08:00
|
|
|
return libName;
|
2013-01-28 12:15:44 +08:00
|
|
|
}
|
|
|
|
|
2014-03-29 03:02:06 +08:00
|
|
|
void ELFLinkingContext::createInternalFiles(
|
|
|
|
std::vector<std::unique_ptr<File>> &files) const {
|
|
|
|
std::unique_ptr<SimpleFile> file(
|
|
|
|
new SimpleFile("<internal file for --defsym>"));
|
2014-03-29 03:23:13 +08:00
|
|
|
for (auto &i : getAbsoluteSymbols()) {
|
2014-03-29 03:02:06 +08:00
|
|
|
StringRef sym = i.first;
|
|
|
|
uint64_t val = i.second;
|
|
|
|
file->addAtom(*(new (_allocator) CommandLineAbsoluteAtom(*file, sym, val)));
|
|
|
|
}
|
2014-04-17 04:58:57 +08:00
|
|
|
for (auto &i : getAliases()) {
|
|
|
|
StringRef from = i.first;
|
|
|
|
StringRef to = i.second;
|
|
|
|
SimpleDefinedAtom *fromAtom = new (_allocator) AliasAtom(*file, from);
|
|
|
|
UndefinedAtom *toAtom = new (_allocator) SimpleUndefinedAtom(*file, to);
|
|
|
|
fromAtom->addReference(Reference::KindNamespace::all,
|
|
|
|
Reference::KindArch::all, Reference::kindLayoutAfter,
|
|
|
|
0, toAtom, 0);
|
|
|
|
file->addAtom(*fromAtom);
|
|
|
|
file->addAtom(*toAtom);
|
|
|
|
}
|
2014-03-29 03:02:06 +08:00
|
|
|
files.push_back(std::move(file));
|
|
|
|
LinkingContext::createInternalFiles(files);
|
|
|
|
}
|
|
|
|
|
2013-10-07 10:47:09 +08:00
|
|
|
std::unique_ptr<File> ELFLinkingContext::createUndefinedSymbolFile() const {
|
2013-08-31 13:27:38 +08:00
|
|
|
if (_initialUndefinedSymbols.empty())
|
|
|
|
return nullptr;
|
|
|
|
std::unique_ptr<SimpleFile> undefinedSymFile(
|
[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
|
|
|
new SimpleFile("command line option -u"));
|
2013-08-31 13:27:38 +08:00
|
|
|
for (auto undefSymStr : _initialUndefinedSymbols)
|
|
|
|
undefinedSymFile->addAtom(*(new (_allocator) CommandLineUndefinedAtom(
|
2014-03-29 03:02:06 +08:00
|
|
|
*undefinedSymFile, undefSymStr)));
|
2013-08-31 13:27:38 +08:00
|
|
|
return std::move(undefinedSymFile);
|
|
|
|
}
|
|
|
|
|
2013-01-22 10:15:30 +08:00
|
|
|
} // end namespace lld
|