2015-07-24 21:03:07 +00: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-05 23:24:46 +00:00
|
|
|
#include "Driver.h"
|
2015-08-06 15:08:23 +00:00
|
|
|
#include "Config.h"
|
|
|
|
#include "Error.h"
|
2015-08-05 23:24:46 +00:00
|
|
|
#include "InputFiles.h"
|
|
|
|
#include "SymbolTable.h"
|
2015-07-24 21:03:07 +00:00
|
|
|
#include "Writer.h"
|
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2015-09-11 21:18:56 +00:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2015-09-03 20:03:54 +00:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2015-09-28 12:52:21 +00:00
|
|
|
#include "llvm/Support/Path.h"
|
2015-07-24 21:03:07 +00:00
|
|
|
|
|
|
|
using namespace llvm;
|
2015-10-07 09:13:03 +00:00
|
|
|
using namespace llvm::ELF;
|
2015-07-24 21:03:07 +00:00
|
|
|
|
|
|
|
using namespace lld;
|
|
|
|
using namespace lld::elf2;
|
|
|
|
|
2015-10-01 15:23:09 +00:00
|
|
|
Configuration *lld::elf2::Config;
|
|
|
|
LinkerDriver *lld::elf2::Driver;
|
2015-09-30 17:06:09 +00:00
|
|
|
|
2015-10-01 15:23:09 +00:00
|
|
|
void lld::elf2::link(ArrayRef<const char *> Args) {
|
2015-08-18 09:13:25 +00:00
|
|
|
Configuration C;
|
2015-10-01 15:23:09 +00:00
|
|
|
LinkerDriver D;
|
2015-08-18 09:13:25 +00:00
|
|
|
Config = &C;
|
2015-10-01 15:23:09 +00:00
|
|
|
Driver = &D;
|
|
|
|
Driver->link(Args.slice(1));
|
2015-07-24 21:03:07 +00:00
|
|
|
}
|
|
|
|
|
2015-10-07 09:13:03 +00:00
|
|
|
static void setELFType(StringRef Emul) {
|
|
|
|
if (Emul == "elf_i386") {
|
|
|
|
Config->ElfKind = ELF32LEKind;
|
|
|
|
Config->EMachine = EM_386;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (Emul == "elf_x86_64") {
|
|
|
|
Config->ElfKind = ELF64LEKind;
|
|
|
|
Config->EMachine = EM_X86_64;
|
|
|
|
return;
|
|
|
|
}
|
2015-10-08 12:13:38 +00:00
|
|
|
if (Emul == "elf32ltsmip") {
|
|
|
|
Config->ElfKind = ELF32LEKind;
|
|
|
|
Config->EMachine = EM_MIPS;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (Emul == "elf32btsmip") {
|
|
|
|
Config->ElfKind = ELF32BEKind;
|
|
|
|
Config->EMachine = EM_MIPS;
|
|
|
|
return;
|
|
|
|
}
|
2015-10-07 09:13:03 +00:00
|
|
|
if (Emul == "elf32ppc") {
|
|
|
|
Config->ElfKind = ELF32BEKind;
|
|
|
|
Config->EMachine = EM_PPC;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (Emul == "elf64ppc") {
|
|
|
|
Config->ElfKind = ELF64BEKind;
|
|
|
|
Config->EMachine = EM_PPC64;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
error(Twine("Unknown emulation: ") + Emul);
|
|
|
|
}
|
|
|
|
|
2015-09-28 15:01:59 +00:00
|
|
|
// Makes a path by concatenating Dir and File.
|
2015-09-30 10:39:37 +00:00
|
|
|
// If Dir starts with '=' the result will be preceded by Sysroot,
|
2015-09-28 15:01:59 +00:00
|
|
|
// which can be set with --sysroot command line switch.
|
|
|
|
static std::string buildSysrootedPath(StringRef Dir, StringRef File) {
|
|
|
|
SmallString<128> Path;
|
2015-09-30 10:39:37 +00:00
|
|
|
if (Dir.startswith("="))
|
|
|
|
sys::path::append(Path, Config->Sysroot, Dir.substr(1), File);
|
|
|
|
else
|
|
|
|
sys::path::append(Path, Dir, File);
|
2015-09-28 15:01:59 +00:00
|
|
|
return Path.str().str();
|
|
|
|
}
|
|
|
|
|
2015-09-28 12:52:21 +00: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 {
|
2015-10-01 16:42:03 +00:00
|
|
|
if (!Config->Static)
|
|
|
|
Names.push_back((Twine("lib") + Path + ".so").str());
|
2015-09-28 12:52:21 +00:00
|
|
|
Names.push_back((Twine("lib") + Path + ".a").str());
|
|
|
|
}
|
|
|
|
for (StringRef Dir : Config->InputSearchPaths) {
|
|
|
|
for (const std::string &Name : Names) {
|
2015-09-28 15:01:59 +00:00
|
|
|
std::string FullPath = buildSysrootedPath(Dir, Name);
|
|
|
|
if (sys::fs::exists(FullPath))
|
|
|
|
return FullPath;
|
2015-09-28 12:52:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
error(Twine("Unable to find library -l") + Path);
|
|
|
|
}
|
|
|
|
|
2015-10-07 09:13:03 +00:00
|
|
|
template <template <class> class T>
|
|
|
|
std::unique_ptr<ELFFileBase>
|
|
|
|
LinkerDriver::createELFInputFile(MemoryBufferRef MB) {
|
|
|
|
std::unique_ptr<ELFFileBase> File = createELFFile<T>(MB);
|
|
|
|
const ELFKind ElfKind = File->getELFKind();
|
|
|
|
const uint16_t EMachine = File->getEMachine();
|
|
|
|
|
|
|
|
// Grab target from the first input file if wasn't set by -m option.
|
|
|
|
if (Config->ElfKind == ELFNoneKind) {
|
|
|
|
Config->ElfKind = ElfKind;
|
|
|
|
Config->EMachine = EMachine;
|
|
|
|
return File;
|
|
|
|
}
|
|
|
|
if (ElfKind == Config->ElfKind && EMachine == Config->EMachine)
|
|
|
|
return File;
|
|
|
|
|
|
|
|
if (const ELFFileBase *First = Symtab.getFirstELF())
|
|
|
|
error(MB.getBufferIdentifier() + " is incompatible with " +
|
|
|
|
First->getName());
|
|
|
|
error(MB.getBufferIdentifier() + " is incompatible with target architecture");
|
|
|
|
}
|
|
|
|
|
2015-10-01 15:23:09 +00:00
|
|
|
// Opens and parses a file. Path has to be resolved already.
|
|
|
|
// Newly created memory buffers are owned by this driver.
|
|
|
|
void LinkerDriver::addFile(StringRef Path) {
|
2015-09-30 17:06:09 +00:00
|
|
|
using namespace llvm::sys::fs;
|
2015-10-01 15:23:09 +00:00
|
|
|
auto MBOrErr = MemoryBuffer::getFile(Path);
|
|
|
|
error(MBOrErr, Twine("cannot open ") + Path);
|
|
|
|
std::unique_ptr<MemoryBuffer> &MB = *MBOrErr;
|
|
|
|
MemoryBufferRef MBRef = MB->getMemBufferRef();
|
|
|
|
OwningMBs.push_back(std::move(MB)); // take MB ownership
|
|
|
|
|
|
|
|
switch (identify_magic(MBRef.getBuffer())) {
|
|
|
|
case file_magic::unknown:
|
|
|
|
readLinkerScript(MBRef);
|
|
|
|
return;
|
|
|
|
case file_magic::archive:
|
|
|
|
Symtab.addFile(make_unique<ArchiveFile>(MBRef));
|
|
|
|
return;
|
|
|
|
case file_magic::elf_shared_object:
|
2015-10-07 09:13:03 +00:00
|
|
|
Symtab.addFile(createELFInputFile<SharedFile>(MBRef));
|
2015-10-01 15:23:09 +00:00
|
|
|
return;
|
|
|
|
default:
|
2015-10-07 09:13:03 +00:00
|
|
|
Symtab.addFile(createELFInputFile<ObjectFile>(MBRef));
|
2015-10-01 15:23:09 +00:00
|
|
|
}
|
2015-09-30 17:06:09 +00:00
|
|
|
}
|
|
|
|
|
2015-10-07 19:34:51 +00:00
|
|
|
static StringRef
|
|
|
|
getString(opt::InputArgList &Args, unsigned Key, StringRef Default = "") {
|
|
|
|
if (auto *Arg = Args.getLastArg(Key))
|
|
|
|
return Arg->getValue();
|
|
|
|
return Default;
|
|
|
|
}
|
|
|
|
|
2015-07-24 21:03:07 +00:00
|
|
|
void LinkerDriver::link(ArrayRef<const char *> ArgsArr) {
|
2015-10-07 23:46:11 +00:00
|
|
|
initSymbols();
|
|
|
|
|
2015-07-24 21:03:07 +00:00
|
|
|
// Parse command line options.
|
|
|
|
opt::InputArgList Args = Parser.parse(ArgsArr);
|
|
|
|
|
2015-10-07 19:34:51 +00:00
|
|
|
for (auto *Arg : Args.filtered(OPT_L))
|
|
|
|
Config->InputSearchPaths.push_back(Arg->getValue());
|
2015-09-28 15:01:59 +00:00
|
|
|
|
2015-09-11 21:18:56 +00: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-10-07 09:13:03 +00:00
|
|
|
if (auto *Arg = Args.getLastArg(OPT_m))
|
|
|
|
setELFType(Arg->getValue());
|
|
|
|
|
2015-09-29 22:33:18 +00: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);
|
2015-10-06 16:20:00 +00:00
|
|
|
Config->EnableNewDtags = !Args.hasArg(OPT_disable_new_dtags);
|
2015-09-29 22:33:18 +00:00
|
|
|
Config->ExportDynamic = Args.hasArg(OPT_export_dynamic);
|
|
|
|
Config->NoInhibitExec = Args.hasArg(OPT_noinhibit_exec);
|
2015-10-01 20:14:45 +00:00
|
|
|
Config->NoUndefined = Args.hasArg(OPT_no_undefined);
|
2015-09-29 22:33:18 +00:00
|
|
|
Config->Shared = Args.hasArg(OPT_shared);
|
|
|
|
|
2015-10-07 19:34:51 +00:00
|
|
|
Config->DynamicLinker = getString(Args, OPT_dynamic_linker);
|
|
|
|
Config->Entry = getString(Args, OPT_entry);
|
|
|
|
Config->Fini = getString(Args, OPT_fini, "_fini");
|
|
|
|
Config->Init = getString(Args, OPT_init, "_init");
|
2015-10-09 00:33:44 +00:00
|
|
|
Config->OutputFile = getString(Args, OPT_o);
|
2015-10-07 19:34:51 +00:00
|
|
|
Config->SoName = getString(Args, OPT_soname);
|
|
|
|
Config->Sysroot = getString(Args, OPT_sysroot);
|
|
|
|
|
2015-10-07 18:22:46 +00:00
|
|
|
for (auto *Arg : Args.filtered(OPT_z))
|
2015-10-07 15:00:21 +00:00
|
|
|
if (Arg->getValue() == StringRef("now"))
|
|
|
|
Config->ZNow = true;
|
|
|
|
|
2015-10-01 16:42:03 +00:00
|
|
|
for (auto *Arg : Args) {
|
|
|
|
switch (Arg->getOption().getID()) {
|
|
|
|
case OPT_l:
|
|
|
|
addFile(searchLibrary(Arg->getValue()));
|
|
|
|
break;
|
|
|
|
case OPT_INPUT:
|
|
|
|
addFile(Arg->getValue());
|
|
|
|
break;
|
|
|
|
case OPT_Bstatic:
|
|
|
|
Config->Static = true;
|
|
|
|
break;
|
|
|
|
case OPT_Bdynamic:
|
|
|
|
Config->Static = false;
|
|
|
|
break;
|
2015-10-01 18:02:21 +00:00
|
|
|
case OPT_whole_archive:
|
|
|
|
Config->WholeArchive = true;
|
|
|
|
break;
|
|
|
|
case OPT_no_whole_archive:
|
|
|
|
Config->WholeArchive = false;
|
|
|
|
break;
|
2015-09-28 12:52:21 +00:00
|
|
|
}
|
2015-07-24 21:03:07 +00:00
|
|
|
}
|
|
|
|
|
2015-09-30 17:06:09 +00:00
|
|
|
if (Symtab.getObjectFiles().empty())
|
2015-07-24 21:03:07 +00:00
|
|
|
error("no input files.");
|
|
|
|
|
2015-10-05 09:43:57 +00:00
|
|
|
for (auto *Arg : Args.filtered(OPT_undefined))
|
|
|
|
Symtab.addUndefinedSym(Arg->getValue());
|
|
|
|
|
2015-10-07 00:25:09 +00:00
|
|
|
if (Config->OutputFile.empty())
|
|
|
|
Config->OutputFile = "a.out";
|
|
|
|
|
2015-10-07 18:29:51 +00:00
|
|
|
// Write the result to the file.
|
|
|
|
writeResult(&Symtab);
|
2015-07-24 21:03:07 +00:00
|
|
|
}
|