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"
|
2014-06-14 01:20:48 +08:00
|
|
|
#include "llvm/Support/Errc.h"
|
2013-01-22 10:15:30 +08:00
|
|
|
#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;
|
|
|
|
};
|
|
|
|
|
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-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");
|
2014-05-01 03:03:56 +08:00
|
|
|
if (llvm::sys::fs::exists(path.str()))
|
|
|
|
return StringRef(*new (_allocator) std::string(path.str()));
|
2013-04-05 11:50:15 +08:00
|
|
|
}
|
|
|
|
// Search for static libraries too
|
2014-05-01 03:03:56 +08:00
|
|
|
buildSearchPath(path, dir, _sysrootPath);
|
|
|
|
llvm::sys::path::append(path, Twine("lib") + libName + ".a");
|
|
|
|
if (llvm::sys::fs::exists(path.str()))
|
|
|
|
return StringRef(*new (_allocator) std::string(path.str()));
|
2013-04-05 02:59:24 +08:00
|
|
|
}
|
2013-09-04 06:44:37 +08:00
|
|
|
if (!llvm::sys::fs::exists(libName))
|
2014-06-14 01:20:48 +08:00
|
|
|
return make_error_code(llvm::errc::no_such_file_or_directory);
|
2013-09-04 06:44:37 +08:00
|
|
|
|
2013-09-26 06:12:14 +08:00
|
|
|
return libName;
|
2013-01-28 12:15:44 +08:00
|
|
|
}
|
|
|
|
|
[ELF] Fix the file look up algorithm used in the linker script GROUP command.
In general the linker scripts's GROUP command works like a pair
of command line options --start-group/--end-group. But there is
a difference in the files look up algorithm.
The --start-group/--end-group commands use a trivial approach:
a) If the path has '-l' prefix, add 'lib' prefix and '.a'/'.so'
suffix and search the path through library search directories.
b) Otherwise, use the path 'as-is'.
The GROUP command implements more compicated approach:
a) If the path has '-l' prefix, add 'lib' prefix and '.a'/'.so'
suffix and search the path through library search directories.
b) If the path does not have '-l' prefix, and sysroot is configured,
and the path starts with the / character, and the script being
processed is located inside the sysroot, search the path under
the sysroot. Otherwise, try to open the path in the current
directory. If it is not found, search through library search
directories.
https://www.sourceware.org/binutils/docs-2.24/ld/File-Commands.html
The patch reviewed by Shankar Easwaran, Rui Ueyama.
llvm-svn: 207769
2014-05-02 00:22:08 +08:00
|
|
|
ErrorOr<StringRef> ELFLinkingContext::searchFile(StringRef fileName,
|
|
|
|
bool isSysRooted) const {
|
|
|
|
SmallString<128> path;
|
|
|
|
if (llvm::sys::path::is_absolute(fileName) && isSysRooted) {
|
|
|
|
path.assign(_sysrootPath);
|
|
|
|
path.append(fileName);
|
|
|
|
if (llvm::sys::fs::exists(path.str()))
|
|
|
|
return StringRef(*new (_allocator) std::string(path.str()));
|
|
|
|
} else if (llvm::sys::fs::exists(fileName))
|
|
|
|
return fileName;
|
|
|
|
|
|
|
|
if (llvm::sys::path::is_absolute(fileName))
|
2014-06-14 01:20:48 +08:00
|
|
|
return make_error_code(llvm::errc::no_such_file_or_directory);
|
[ELF] Fix the file look up algorithm used in the linker script GROUP command.
In general the linker scripts's GROUP command works like a pair
of command line options --start-group/--end-group. But there is
a difference in the files look up algorithm.
The --start-group/--end-group commands use a trivial approach:
a) If the path has '-l' prefix, add 'lib' prefix and '.a'/'.so'
suffix and search the path through library search directories.
b) Otherwise, use the path 'as-is'.
The GROUP command implements more compicated approach:
a) If the path has '-l' prefix, add 'lib' prefix and '.a'/'.so'
suffix and search the path through library search directories.
b) If the path does not have '-l' prefix, and sysroot is configured,
and the path starts with the / character, and the script being
processed is located inside the sysroot, search the path under
the sysroot. Otherwise, try to open the path in the current
directory. If it is not found, search through library search
directories.
https://www.sourceware.org/binutils/docs-2.24/ld/File-Commands.html
The patch reviewed by Shankar Easwaran, Rui Ueyama.
llvm-svn: 207769
2014-05-02 00:22:08 +08:00
|
|
|
|
|
|
|
for (StringRef dir : _inputSearchPaths) {
|
|
|
|
buildSearchPath(path, dir, _sysrootPath);
|
|
|
|
llvm::sys::path::append(path, fileName);
|
|
|
|
if (llvm::sys::fs::exists(path.str()))
|
|
|
|
return StringRef(*new (_allocator) std::string(path.str()));
|
|
|
|
}
|
2014-06-14 01:20:48 +08:00
|
|
|
return make_error_code(llvm::errc::no_such_file_or_directory);
|
[ELF] Fix the file look up algorithm used in the linker script GROUP command.
In general the linker scripts's GROUP command works like a pair
of command line options --start-group/--end-group. But there is
a difference in the files look up algorithm.
The --start-group/--end-group commands use a trivial approach:
a) If the path has '-l' prefix, add 'lib' prefix and '.a'/'.so'
suffix and search the path through library search directories.
b) Otherwise, use the path 'as-is'.
The GROUP command implements more compicated approach:
a) If the path has '-l' prefix, add 'lib' prefix and '.a'/'.so'
suffix and search the path through library search directories.
b) If the path does not have '-l' prefix, and sysroot is configured,
and the path starts with the / character, and the script being
processed is located inside the sysroot, search the path under
the sysroot. Otherwise, try to open the path in the current
directory. If it is not found, search through library search
directories.
https://www.sourceware.org/binutils/docs-2.24/ld/File-Commands.html
The patch reviewed by Shankar Easwaran, Rui Ueyama.
llvm-svn: 207769
2014-05-02 00:22:08 +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)));
|
|
|
|
}
|
|
|
|
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
|