2015-07-25 05:03:07 +08:00
|
|
|
//===- Driver.cpp ---------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Linker
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2015-08-06 07:24:46 +08:00
|
|
|
#include "Driver.h"
|
2015-08-06 23:08:23 +08:00
|
|
|
#include "Config.h"
|
|
|
|
#include "Error.h"
|
2015-08-06 07:24:46 +08:00
|
|
|
#include "InputFiles.h"
|
|
|
|
#include "SymbolTable.h"
|
2015-07-25 05:03:07 +08:00
|
|
|
#include "Writer.h"
|
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2015-09-12 05:18:56 +08:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2015-09-04 04:03:54 +08:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2015-09-28 20:52:21 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
2015-07-25 05:03:07 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
using namespace lld;
|
|
|
|
using namespace lld::elf2;
|
|
|
|
|
|
|
|
namespace lld {
|
|
|
|
namespace elf2 {
|
2015-10-01 01:06:09 +08:00
|
|
|
|
2015-07-25 05:03:07 +08:00
|
|
|
Configuration *Config;
|
2015-10-01 01:06:09 +08:00
|
|
|
std::vector<std::unique_ptr<MemoryBuffer>> *MemoryBufferPool;
|
2015-07-25 05:03:07 +08:00
|
|
|
|
|
|
|
void link(ArrayRef<const char *> Args) {
|
2015-08-18 17:13:25 +08:00
|
|
|
Configuration C;
|
|
|
|
Config = &C;
|
2015-10-01 01:06:09 +08:00
|
|
|
std::vector<std::unique_ptr<MemoryBuffer>> V;
|
|
|
|
MemoryBufferPool = &V;
|
2015-08-12 05:45:55 +08:00
|
|
|
LinkerDriver().link(Args.slice(1));
|
2015-07-25 05:03:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Opens a file. Path has to be resolved already.
|
|
|
|
// Newly created memory buffers are owned by this driver.
|
2015-10-01 01:06:09 +08:00
|
|
|
MemoryBufferRef openFile(StringRef Path) {
|
2015-07-25 05:03:07 +08:00
|
|
|
ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getFile(Path);
|
|
|
|
error(MBOrErr, Twine("cannot open ") + Path);
|
|
|
|
std::unique_ptr<MemoryBuffer> &MB = *MBOrErr;
|
|
|
|
MemoryBufferRef MBRef = MB->getMemBufferRef();
|
2015-10-01 01:06:09 +08:00
|
|
|
MemoryBufferPool->push_back(std::move(MB)); // transfer ownership
|
2015-07-25 05:03:07 +08:00
|
|
|
return MBRef;
|
|
|
|
}
|
2015-10-01 01:06:09 +08:00
|
|
|
}
|
2015-07-25 05:03:07 +08:00
|
|
|
}
|
|
|
|
|
2015-09-28 23:01:59 +08:00
|
|
|
// Makes a path by concatenating Dir and File.
|
2015-09-30 18:39:37 +08:00
|
|
|
// If Dir starts with '=' the result will be preceded by Sysroot,
|
2015-09-28 23:01:59 +08:00
|
|
|
// which can be set with --sysroot command line switch.
|
|
|
|
static std::string buildSysrootedPath(StringRef Dir, StringRef File) {
|
|
|
|
SmallString<128> Path;
|
2015-09-30 18:39:37 +08:00
|
|
|
if (Dir.startswith("="))
|
|
|
|
sys::path::append(Path, Config->Sysroot, Dir.substr(1), File);
|
|
|
|
else
|
|
|
|
sys::path::append(Path, Dir, File);
|
2015-09-28 23:01:59 +08:00
|
|
|
return Path.str().str();
|
|
|
|
}
|
|
|
|
|
2015-09-28 20:52:21 +08:00
|
|
|
// Searches a given library from input search paths, which are filled
|
|
|
|
// from -L command line switches. Returns a path to an existent library file.
|
|
|
|
static std::string searchLibrary(StringRef Path) {
|
|
|
|
std::vector<std::string> Names;
|
|
|
|
if (Path[0] == ':') {
|
|
|
|
Names.push_back(Path.drop_front().str());
|
|
|
|
} else {
|
|
|
|
Names.push_back((Twine("lib") + Path + ".so").str());
|
|
|
|
Names.push_back((Twine("lib") + Path + ".a").str());
|
|
|
|
}
|
|
|
|
for (StringRef Dir : Config->InputSearchPaths) {
|
|
|
|
for (const std::string &Name : Names) {
|
2015-09-28 23:01:59 +08:00
|
|
|
std::string FullPath = buildSysrootedPath(Dir, Name);
|
|
|
|
if (sys::fs::exists(FullPath))
|
|
|
|
return FullPath;
|
2015-09-28 20:52:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
error(Twine("Unable to find library -l") + Path);
|
|
|
|
}
|
|
|
|
|
2015-10-01 01:06:09 +08:00
|
|
|
// Returns true if MB looks like a linker script.
|
|
|
|
static bool isLinkerScript(MemoryBufferRef MB) {
|
|
|
|
using namespace llvm::sys::fs;
|
|
|
|
return identify_magic(MB.getBuffer()) == file_magic::unknown;
|
|
|
|
}
|
|
|
|
|
2015-07-25 05:03:07 +08:00
|
|
|
void LinkerDriver::link(ArrayRef<const char *> ArgsArr) {
|
|
|
|
// Parse command line options.
|
|
|
|
opt::InputArgList Args = Parser.parse(ArgsArr);
|
|
|
|
|
|
|
|
if (auto *Arg = Args.getLastArg(OPT_output))
|
|
|
|
Config->OutputFile = Arg->getValue();
|
|
|
|
|
2015-09-12 02:49:42 +08:00
|
|
|
if (auto *Arg = Args.getLastArg(OPT_dynamic_linker))
|
|
|
|
Config->DynamicLinker = Arg->getValue();
|
|
|
|
|
2015-09-28 23:01:59 +08:00
|
|
|
if (auto *Arg = Args.getLastArg(OPT_sysroot))
|
|
|
|
Config->Sysroot = Arg->getValue();
|
|
|
|
|
2015-09-12 05:18:56 +08:00
|
|
|
std::vector<StringRef> RPaths;
|
|
|
|
for (auto *Arg : Args.filtered(OPT_rpath))
|
|
|
|
RPaths.push_back(Arg->getValue());
|
|
|
|
if (!RPaths.empty())
|
|
|
|
Config->RPath = llvm::join(RPaths.begin(), RPaths.end(), ":");
|
|
|
|
|
2015-09-28 20:52:21 +08:00
|
|
|
for (auto *Arg : Args.filtered(OPT_L))
|
|
|
|
Config->InputSearchPaths.push_back(Arg->getValue());
|
|
|
|
|
2015-09-30 00:40:13 +08:00
|
|
|
if (auto *Arg = Args.getLastArg(OPT_entry))
|
|
|
|
Config->Entry = Arg->getValue();
|
|
|
|
|
2015-09-30 06:33:18 +08:00
|
|
|
Config->AllowMultipleDefinition = Args.hasArg(OPT_allow_multiple_definition);
|
|
|
|
Config->DiscardAll = Args.hasArg(OPT_discard_all);
|
|
|
|
Config->DiscardLocals = Args.hasArg(OPT_discard_locals);
|
|
|
|
Config->DiscardNone = Args.hasArg(OPT_discard_none);
|
|
|
|
Config->ExportDynamic = Args.hasArg(OPT_export_dynamic);
|
|
|
|
Config->NoInhibitExec = Args.hasArg(OPT_noinhibit_exec);
|
|
|
|
Config->Shared = Args.hasArg(OPT_shared);
|
|
|
|
|
2015-10-01 01:06:09 +08:00
|
|
|
// Create a symbol table.
|
|
|
|
SymbolTable Symtab;
|
2015-07-25 05:03:07 +08:00
|
|
|
|
2015-09-28 20:52:21 +08:00
|
|
|
for (auto *Arg : Args.filtered(OPT_l, OPT_INPUT)) {
|
2015-10-01 01:06:09 +08:00
|
|
|
std::string Path = Arg->getValue();
|
|
|
|
if (Arg->getOption().getID() == OPT_l)
|
|
|
|
Path = searchLibrary(Path);
|
|
|
|
MemoryBufferRef MB = openFile(Path);
|
|
|
|
if (isLinkerScript(MB)) {
|
|
|
|
// readLinkerScript may add files to the symbol table.
|
|
|
|
readLinkerScript(&Symtab, MB);
|
2015-09-28 20:52:21 +08:00
|
|
|
continue;
|
|
|
|
}
|
2015-10-01 01:06:09 +08:00
|
|
|
Symtab.addFile(createFile(MB));
|
2015-07-25 05:03:07 +08:00
|
|
|
}
|
|
|
|
|
2015-10-01 01:06:09 +08:00
|
|
|
if (Symtab.getObjectFiles().empty())
|
2015-07-25 05:03:07 +08:00
|
|
|
error("no input files.");
|
|
|
|
|
|
|
|
// Write the result.
|
2015-09-04 04:03:54 +08:00
|
|
|
const ELFFileBase *FirstObj = Symtab.getFirstELF();
|
|
|
|
switch (FirstObj->getELFKind()) {
|
2015-09-03 04:43:43 +08:00
|
|
|
case ELF32LEKind:
|
2015-08-06 07:51:50 +08:00
|
|
|
writeResult<object::ELF32LE>(&Symtab);
|
2015-08-05 23:08:40 +08:00
|
|
|
return;
|
2015-09-03 04:43:43 +08:00
|
|
|
case ELF32BEKind:
|
2015-08-06 07:51:50 +08:00
|
|
|
writeResult<object::ELF32BE>(&Symtab);
|
2015-08-05 23:08:40 +08:00
|
|
|
return;
|
2015-09-03 04:43:43 +08:00
|
|
|
case ELF64LEKind:
|
2015-08-06 07:51:50 +08:00
|
|
|
writeResult<object::ELF64LE>(&Symtab);
|
2015-08-05 23:08:40 +08:00
|
|
|
return;
|
2015-09-03 04:43:43 +08:00
|
|
|
case ELF64BEKind:
|
2015-08-06 07:51:50 +08:00
|
|
|
writeResult<object::ELF64BE>(&Symtab);
|
2015-08-05 23:08:40 +08:00
|
|
|
return;
|
|
|
|
}
|
2015-07-25 05:03:07 +08:00
|
|
|
}
|