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-04 04:03:54 +08:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2015-07-25 05:03:07 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
using namespace lld;
|
|
|
|
using namespace lld::elf2;
|
|
|
|
|
|
|
|
namespace lld {
|
|
|
|
namespace elf2 {
|
|
|
|
Configuration *Config;
|
|
|
|
|
|
|
|
void link(ArrayRef<const char *> Args) {
|
2015-08-18 17:13:25 +08:00
|
|
|
Configuration C;
|
|
|
|
Config = &C;
|
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.
|
|
|
|
MemoryBufferRef LinkerDriver::openFile(StringRef Path) {
|
|
|
|
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();
|
|
|
|
OwningMBs.push_back(std::move(MB)); // take ownership
|
|
|
|
return MBRef;
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::unique_ptr<InputFile> createFile(MemoryBufferRef MB) {
|
2015-09-04 04:03:54 +08:00
|
|
|
using namespace llvm::sys::fs;
|
|
|
|
file_magic Magic = identify_magic(MB.getBuffer());
|
|
|
|
|
2015-09-05 06:28:10 +08:00
|
|
|
if (Magic == file_magic::archive)
|
|
|
|
return make_unique<ArchiveFile>(MB);
|
|
|
|
|
|
|
|
if (Magic == file_magic::elf_shared_object)
|
|
|
|
return createELFFile<SharedFile>(MB);
|
2015-08-04 23:45:54 +08:00
|
|
|
|
2015-09-05 06:28:10 +08:00
|
|
|
return createELFFile<ObjectFile>(MB);
|
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);
|
|
|
|
|
|
|
|
// Handle -o
|
|
|
|
if (auto *Arg = Args.getLastArg(OPT_output))
|
|
|
|
Config->OutputFile = Arg->getValue();
|
|
|
|
if (Config->OutputFile.empty())
|
|
|
|
error("-o must be specified.");
|
|
|
|
|
2015-09-12 02:49:42 +08:00
|
|
|
// Handle -dynamic-linker
|
|
|
|
if (auto *Arg = Args.getLastArg(OPT_dynamic_linker))
|
|
|
|
Config->DynamicLinker = Arg->getValue();
|
|
|
|
|
2015-07-25 05:03:07 +08:00
|
|
|
// Create a list of input files.
|
|
|
|
std::vector<MemoryBufferRef> Inputs;
|
|
|
|
|
|
|
|
for (auto *Arg : Args.filtered(OPT_INPUT)) {
|
|
|
|
StringRef Path = Arg->getValue();
|
|
|
|
Inputs.push_back(openFile(Path));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Inputs.empty())
|
|
|
|
error("no input files.");
|
|
|
|
|
|
|
|
// Create a symbol table.
|
2015-08-04 22:29:01 +08:00
|
|
|
SymbolTable Symtab;
|
2015-07-25 05:03:07 +08:00
|
|
|
|
|
|
|
// Parse all input files and put all symbols to the symbol table.
|
|
|
|
// The symbol table will take care of name resolution.
|
|
|
|
for (MemoryBufferRef MB : Inputs) {
|
|
|
|
std::unique_ptr<InputFile> File = createFile(MB);
|
|
|
|
Symtab.addFile(std::move(File));
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
}
|