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"
|
2016-02-26 02:43:51 +08:00
|
|
|
#include "ICF.h"
|
2015-08-06 07:24:46 +08:00
|
|
|
#include "InputFiles.h"
|
2016-05-24 00:55:43 +08:00
|
|
|
#include "InputSection.h"
|
2016-02-12 05:17:59 +08:00
|
|
|
#include "LinkerScript.h"
|
2016-10-29 04:57:25 +08:00
|
|
|
#include "Memory.h"
|
2016-06-29 16:01:32 +08:00
|
|
|
#include "Strings.h"
|
2015-08-06 07:24:46 +08:00
|
|
|
#include "SymbolTable.h"
|
2015-10-10 05:12:40 +08:00
|
|
|
#include "Target.h"
|
2015-07-25 05:03:07 +08:00
|
|
|
#include "Writer.h"
|
2016-10-20 04:53:07 +08:00
|
|
|
#include "lld/Config/Version.h"
|
Parallelize uncompress() and splitIntoPieces().
Uncompressing section contents and spliting mergeable section contents
into smaller chunks are heavy tasks. They scan entire section contents
and do CPU-intensive tasks such as uncompressing zlib-compressed data
or computing a hash value for each section piece.
Luckily, these tasks are independent to each other, so we can do that
in parallel_for_each. The number of input sections is large (as opposed
to the number of output sections), so there's a large parallelism here.
Actually the current design to call uncompress() and splitIntoPieces()
in batch was chosen with doing this in mind. Basically what we need to
do here is to replace `for` with `parallel_for_each`.
It seems this patch improves latency significantly if linked programs
contain debug info (which in turn contain lots of mergeable strings.)
For example, the latency to link Clang (debug build) improved by 20% on
my machine as shown below. Note that ld.gold took 19.2 seconds to do
the same thing.
Before:
30801.782712 task-clock (msec) # 3.652 CPUs utilized ( +- 2.59% )
104,084 context-switches # 0.003 M/sec ( +- 1.02% )
5,063 cpu-migrations # 0.164 K/sec ( +- 13.66% )
2,528,130 page-faults # 0.082 M/sec ( +- 0.47% )
85,317,809,130 cycles # 2.770 GHz ( +- 2.62% )
67,352,463,373 stalled-cycles-frontend # 78.94% frontend cycles idle ( +- 3.06% )
<not supported> stalled-cycles-backend
44,295,945,493 instructions # 0.52 insns per cycle
# 1.52 stalled cycles per insn ( +- 0.44% )
8,572,384,877 branches # 278.308 M/sec ( +- 0.66% )
141,806,726 branch-misses # 1.65% of all branches ( +- 0.13% )
8.433424003 seconds time elapsed ( +- 1.20% )
After:
35523.764575 task-clock (msec) # 5.265 CPUs utilized ( +- 2.67% )
159,107 context-switches # 0.004 M/sec ( +- 0.48% )
8,123 cpu-migrations # 0.229 K/sec ( +- 23.34% )
2,372,483 page-faults # 0.067 M/sec ( +- 0.36% )
98,395,342,152 cycles # 2.770 GHz ( +- 2.62% )
79,294,670,125 stalled-cycles-frontend # 80.59% frontend cycles idle ( +- 3.03% )
<not supported> stalled-cycles-backend
46,274,151,813 instructions # 0.47 insns per cycle
# 1.71 stalled cycles per insn ( +- 0.47% )
8,987,621,670 branches # 253.003 M/sec ( +- 0.60% )
148,900,624 branch-misses # 1.66% of all branches ( +- 0.27% )
6.747548004 seconds time elapsed ( +- 0.40% )
llvm-svn: 287946
2016-11-26 04:05:08 +08:00
|
|
|
#include "lld/Core/Parallel.h"
|
2016-03-03 03:08:05 +08:00
|
|
|
#include "lld/Driver/Driver.h"
|
2015-09-12 05:18:56 +08:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2016-06-08 01:55:05 +08:00
|
|
|
#include "llvm/ADT/StringSwitch.h"
|
2016-11-12 03:50:24 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2016-11-26 23:10:01 +08:00
|
|
|
#include "llvm/Support/Process.h"
|
2016-02-13 04:54:57 +08:00
|
|
|
#include "llvm/Support/TargetSelect.h"
|
2015-10-11 10:03:03 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2016-06-25 02:02:50 +08:00
|
|
|
#include <cstdlib>
|
2015-10-11 10:22:31 +08:00
|
|
|
#include <utility>
|
2015-07-25 05:03:07 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
2015-10-07 17:13:03 +08:00
|
|
|
using namespace llvm::ELF;
|
2015-10-10 05:07:25 +08:00
|
|
|
using namespace llvm::object;
|
2016-04-27 04:36:46 +08:00
|
|
|
using namespace llvm::sys;
|
2015-07-25 05:03:07 +08:00
|
|
|
|
|
|
|
using namespace lld;
|
2016-02-28 08:25:54 +08:00
|
|
|
using namespace lld::elf;
|
2015-07-25 05:03:07 +08:00
|
|
|
|
2016-02-28 08:25:54 +08:00
|
|
|
Configuration *elf::Config;
|
|
|
|
LinkerDriver *elf::Driver;
|
2015-10-01 01:06:09 +08:00
|
|
|
|
2016-10-27 02:59:00 +08:00
|
|
|
bool elf::link(ArrayRef<const char *> Args, bool CanExitEarly,
|
|
|
|
raw_ostream &Error) {
|
2016-11-24 02:15:37 +08:00
|
|
|
ErrorCount = 0;
|
2016-02-03 06:49:32 +08:00
|
|
|
ErrorOS = &Error;
|
2016-10-20 04:05:43 +08:00
|
|
|
Argv0 = Args[0];
|
2016-04-21 04:13:41 +08:00
|
|
|
|
2015-08-18 17:13:25 +08:00
|
|
|
Configuration C;
|
2015-10-01 23:23:09 +08:00
|
|
|
LinkerDriver D;
|
2016-04-21 04:13:41 +08:00
|
|
|
ScriptConfiguration SC;
|
2015-08-18 17:13:25 +08:00
|
|
|
Config = &C;
|
2015-10-01 23:23:09 +08:00
|
|
|
Driver = &D;
|
2016-04-21 04:13:41 +08:00
|
|
|
ScriptConfig = &SC;
|
|
|
|
|
2016-10-27 02:59:00 +08:00
|
|
|
Driver->main(Args, CanExitEarly);
|
2016-10-29 04:57:25 +08:00
|
|
|
freeArena();
|
2016-11-24 09:43:21 +08:00
|
|
|
return !ErrorCount;
|
2015-07-25 05:03:07 +08:00
|
|
|
}
|
|
|
|
|
2016-06-08 01:55:05 +08:00
|
|
|
// Parses a linker -m option.
|
2016-11-10 06:32:43 +08:00
|
|
|
static std::tuple<ELFKind, uint16_t, uint8_t> parseEmulation(StringRef Emul) {
|
2016-10-27 22:00:51 +08:00
|
|
|
uint8_t OSABI = 0;
|
2016-09-09 03:36:22 +08:00
|
|
|
StringRef S = Emul;
|
2016-10-27 22:00:51 +08:00
|
|
|
if (S.endswith("_fbsd")) {
|
2016-04-01 04:26:30 +08:00
|
|
|
S = S.drop_back(5);
|
2016-10-27 22:00:51 +08:00
|
|
|
OSABI = ELFOSABI_FREEBSD;
|
|
|
|
}
|
2016-06-08 01:55:05 +08:00
|
|
|
|
|
|
|
std::pair<ELFKind, uint16_t> Ret =
|
|
|
|
StringSwitch<std::pair<ELFKind, uint16_t>>(S)
|
2016-10-01 06:01:25 +08:00
|
|
|
.Cases("aarch64elf", "aarch64linux", {ELF64LEKind, EM_AARCH64})
|
2016-06-08 01:55:05 +08:00
|
|
|
.Case("armelf_linux_eabi", {ELF32LEKind, EM_ARM})
|
2016-07-13 07:28:33 +08:00
|
|
|
.Case("elf32_x86_64", {ELF32LEKind, EM_X86_64})
|
2016-06-08 01:55:05 +08:00
|
|
|
.Case("elf32btsmip", {ELF32BEKind, EM_MIPS})
|
|
|
|
.Case("elf32ltsmip", {ELF32LEKind, EM_MIPS})
|
2016-11-06 06:58:01 +08:00
|
|
|
.Case("elf32btsmipn32", {ELF32BEKind, EM_MIPS})
|
|
|
|
.Case("elf32ltsmipn32", {ELF32LEKind, EM_MIPS})
|
2016-06-08 01:55:05 +08:00
|
|
|
.Case("elf32ppc", {ELF32BEKind, EM_PPC})
|
|
|
|
.Case("elf64btsmip", {ELF64BEKind, EM_MIPS})
|
|
|
|
.Case("elf64ltsmip", {ELF64LEKind, EM_MIPS})
|
|
|
|
.Case("elf64ppc", {ELF64BEKind, EM_PPC64})
|
2016-10-01 06:01:25 +08:00
|
|
|
.Cases("elf_amd64", "elf_x86_64", {ELF64LEKind, EM_X86_64})
|
2016-06-08 01:55:05 +08:00
|
|
|
.Case("elf_i386", {ELF32LEKind, EM_386})
|
2016-08-04 04:15:56 +08:00
|
|
|
.Case("elf_iamcu", {ELF32LEKind, EM_IAMCU})
|
2016-06-08 01:55:05 +08:00
|
|
|
.Default({ELFNoneKind, EM_NONE});
|
|
|
|
|
|
|
|
if (Ret.first == ELFNoneKind) {
|
|
|
|
if (S == "i386pe" || S == "i386pep" || S == "thumb2pe")
|
2016-09-09 03:36:22 +08:00
|
|
|
error("Windows targets are not supported on the ELF frontend: " + Emul);
|
2016-06-08 01:55:05 +08:00
|
|
|
else
|
2016-09-09 03:36:22 +08:00
|
|
|
error("unknown emulation: " + Emul);
|
2016-06-08 01:55:05 +08:00
|
|
|
}
|
2016-11-10 06:32:43 +08:00
|
|
|
return std::make_tuple(Ret.first, Ret.second, OSABI);
|
2015-10-07 17:13:03 +08:00
|
|
|
}
|
|
|
|
|
2016-01-06 08:51:35 +08:00
|
|
|
// Returns slices of MB by parsing MB as an archive file.
|
|
|
|
// Each slice consists of a member file in the archive.
|
2016-04-01 07:12:18 +08:00
|
|
|
std::vector<MemoryBufferRef>
|
|
|
|
LinkerDriver::getArchiveMembers(MemoryBufferRef MB) {
|
2016-03-04 00:21:44 +08:00
|
|
|
std::unique_ptr<Archive> File =
|
2016-11-21 17:28:07 +08:00
|
|
|
check(Archive::create(MB),
|
|
|
|
MB.getBufferIdentifier() + ": failed to parse archive");
|
2016-01-06 08:51:35 +08:00
|
|
|
|
|
|
|
std::vector<MemoryBufferRef> V;
|
2016-11-11 12:28:40 +08:00
|
|
|
Error Err = Error::success();
|
2016-07-14 10:35:18 +08:00
|
|
|
for (const ErrorOr<Archive::Child> &COrErr : File->children(Err)) {
|
2016-11-21 17:28:07 +08:00
|
|
|
Archive::Child C =
|
|
|
|
check(COrErr, MB.getBufferIdentifier() +
|
|
|
|
": could not get the child of the archive");
|
2016-04-03 03:09:07 +08:00
|
|
|
MemoryBufferRef MBRef =
|
2016-03-04 06:24:39 +08:00
|
|
|
check(C.getMemoryBufferRef(),
|
2016-11-21 17:28:07 +08:00
|
|
|
MB.getBufferIdentifier() +
|
|
|
|
": could not get the buffer for a child of the archive");
|
2016-04-03 03:09:07 +08:00
|
|
|
V.push_back(MBRef);
|
2016-01-06 08:51:35 +08:00
|
|
|
}
|
2016-07-15 10:01:03 +08:00
|
|
|
if (Err)
|
2016-11-21 17:28:07 +08:00
|
|
|
fatal(MB.getBufferIdentifier() + ": Archive::children failed: " +
|
|
|
|
toString(std::move(Err)));
|
2016-04-01 07:12:18 +08:00
|
|
|
|
|
|
|
// Take ownership of memory buffers created for members of thin archives.
|
|
|
|
for (std::unique_ptr<MemoryBuffer> &MB : File->takeThinBuffers())
|
|
|
|
OwningMBs.push_back(std::move(MB));
|
|
|
|
|
2016-01-06 08:51:35 +08:00
|
|
|
return V;
|
|
|
|
}
|
|
|
|
|
2015-10-01 23:23:09 +08:00
|
|
|
// Opens and parses a file. Path has to be resolved already.
|
|
|
|
// Newly created memory buffers are owned by this driver.
|
2016-10-20 13:03:49 +08:00
|
|
|
void LinkerDriver::addFile(StringRef Path) {
|
2016-05-03 03:59:56 +08:00
|
|
|
using namespace sys::fs;
|
2016-04-26 08:22:24 +08:00
|
|
|
|
2016-04-14 02:51:11 +08:00
|
|
|
Optional<MemoryBufferRef> Buffer = readFile(Path);
|
|
|
|
if (!Buffer.hasValue())
|
2016-02-03 05:13:09 +08:00
|
|
|
return;
|
2016-04-14 02:51:11 +08:00
|
|
|
MemoryBufferRef MBRef = *Buffer;
|
2015-10-01 23:23:09 +08:00
|
|
|
|
2016-10-20 13:12:29 +08:00
|
|
|
if (InBinary) {
|
2016-11-02 06:53:18 +08:00
|
|
|
Files.push_back(make<BinaryFile>(MBRef));
|
2016-09-10 06:08:04 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-10-01 23:23:09 +08:00
|
|
|
switch (identify_magic(MBRef.getBuffer())) {
|
|
|
|
case file_magic::unknown:
|
2016-04-21 04:13:41 +08:00
|
|
|
readLinkerScript(MBRef);
|
2015-10-01 23:23:09 +08:00
|
|
|
return;
|
|
|
|
case file_magic::archive:
|
2016-10-26 12:01:07 +08:00
|
|
|
if (InWholeArchive) {
|
2016-01-06 08:51:35 +08:00
|
|
|
for (MemoryBufferRef MB : getArchiveMembers(MBRef))
|
2016-10-29 04:57:25 +08:00
|
|
|
Files.push_back(createObjectFile(MB, Path));
|
2015-10-10 05:07:25 +08:00
|
|
|
return;
|
|
|
|
}
|
2016-11-02 06:53:18 +08:00
|
|
|
Files.push_back(make<ArchiveFile>(MBRef));
|
2015-10-01 23:23:09 +08:00
|
|
|
return;
|
2015-10-12 09:55:32 +08:00
|
|
|
case file_magic::elf_shared_object:
|
2016-02-25 16:23:37 +08:00
|
|
|
if (Config->Relocatable) {
|
2016-03-12 16:31:34 +08:00
|
|
|
error("attempted static link of dynamic object " + Path);
|
2016-02-25 16:23:37 +08:00
|
|
|
return;
|
|
|
|
}
|
2016-10-29 04:57:25 +08:00
|
|
|
Files.push_back(createSharedFile(MBRef));
|
2015-10-01 23:23:09 +08:00
|
|
|
return;
|
|
|
|
default:
|
2016-04-08 03:24:51 +08:00
|
|
|
if (InLib)
|
2016-11-02 06:53:18 +08:00
|
|
|
Files.push_back(make<LazyObjectFile>(MBRef));
|
2016-04-08 03:24:51 +08:00
|
|
|
else
|
2016-10-29 04:57:25 +08:00
|
|
|
Files.push_back(createObjectFile(MBRef));
|
2015-10-01 23:23:09 +08:00
|
|
|
}
|
2015-10-01 01:06:09 +08:00
|
|
|
}
|
|
|
|
|
2016-04-14 02:51:11 +08:00
|
|
|
Optional<MemoryBufferRef> LinkerDriver::readFile(StringRef Path) {
|
2016-10-20 13:03:49 +08:00
|
|
|
if (Config->Verbose)
|
|
|
|
outs() << Path << "\n";
|
|
|
|
|
2016-04-14 02:51:11 +08:00
|
|
|
auto MBOrErr = MemoryBuffer::getFile(Path);
|
2016-07-15 09:38:54 +08:00
|
|
|
if (auto EC = MBOrErr.getError()) {
|
|
|
|
error(EC, "cannot open " + Path);
|
2016-04-14 02:51:11 +08:00
|
|
|
return None;
|
2016-07-15 09:38:54 +08:00
|
|
|
}
|
2016-04-14 02:51:11 +08:00
|
|
|
std::unique_ptr<MemoryBuffer> &MB = *MBOrErr;
|
|
|
|
MemoryBufferRef MBRef = MB->getMemBufferRef();
|
|
|
|
OwningMBs.push_back(std::move(MB)); // take MB ownership
|
2016-06-05 21:19:39 +08:00
|
|
|
|
|
|
|
if (Cpio)
|
|
|
|
Cpio->append(relativeToRoot(Path), MBRef.getBuffer());
|
|
|
|
|
2016-04-14 02:51:11 +08:00
|
|
|
return MBRef;
|
|
|
|
}
|
|
|
|
|
2016-02-03 05:13:09 +08:00
|
|
|
// Add a given library by searching it from input search paths.
|
|
|
|
void LinkerDriver::addLibrary(StringRef Name) {
|
2016-11-20 03:23:58 +08:00
|
|
|
if (Optional<std::string> Path = searchLibrary(Name))
|
|
|
|
addFile(*Path);
|
2016-04-25 02:23:21 +08:00
|
|
|
else
|
2016-11-20 03:23:58 +08:00
|
|
|
error("unable to find library -l" + Name);
|
2016-02-03 05:13:09 +08:00
|
|
|
}
|
|
|
|
|
2016-04-03 02:18:44 +08:00
|
|
|
// This function is called on startup. We need this for LTO since
|
|
|
|
// LTO calls LLVM functions to compile bitcode files to native code.
|
|
|
|
// Technically this can be delayed until we read bitcode files, but
|
|
|
|
// we don't bother to do lazily because the initialization is fast.
|
|
|
|
static void initLLVM(opt::InputArgList &Args) {
|
|
|
|
InitializeAllTargets();
|
|
|
|
InitializeAllTargetMCs();
|
|
|
|
InitializeAllAsmPrinters();
|
|
|
|
InitializeAllAsmParsers();
|
|
|
|
|
|
|
|
// Parse and evaluate -mllvm options.
|
|
|
|
std::vector<const char *> V;
|
|
|
|
V.push_back("lld (LLVM option parsing)");
|
|
|
|
for (auto *Arg : Args.filtered(OPT_mllvm))
|
|
|
|
V.push_back(Arg->getValue());
|
|
|
|
cl::ParseCommandLineOptions(V.size(), V.data());
|
|
|
|
}
|
|
|
|
|
2016-01-08 01:33:25 +08:00
|
|
|
// Some command line options or some combinations of them are not allowed.
|
|
|
|
// This function checks for such errors.
|
|
|
|
static void checkOptions(opt::InputArgList &Args) {
|
|
|
|
// The MIPS ABI as of 2016 does not support the GNU-style symbol lookup
|
|
|
|
// table which is a relatively new feature.
|
|
|
|
if (Config->EMachine == EM_MIPS && Config->GnuHash)
|
2016-03-12 16:31:34 +08:00
|
|
|
error("the .gnu.hash section is not compatible with the MIPS target.");
|
2016-01-08 01:33:25 +08:00
|
|
|
|
|
|
|
if (Config->EMachine == EM_AMDGPU && !Config->Entry.empty())
|
2016-02-03 05:13:09 +08:00
|
|
|
error("-e option is not valid for AMDGPU.");
|
2016-02-25 16:23:37 +08:00
|
|
|
|
2016-03-17 13:57:33 +08:00
|
|
|
if (Config->Pie && Config->Shared)
|
|
|
|
error("-shared and -pie may not be used together");
|
|
|
|
|
2016-04-03 02:52:23 +08:00
|
|
|
if (Config->Relocatable) {
|
|
|
|
if (Config->Shared)
|
|
|
|
error("-r and -shared may not be used together");
|
|
|
|
if (Config->GcSections)
|
|
|
|
error("-r and --gc-sections may not be used together");
|
|
|
|
if (Config->ICF)
|
|
|
|
error("-r and --icf may not be used together");
|
|
|
|
if (Config->Pie)
|
|
|
|
error("-r and -pie may not be used together");
|
|
|
|
}
|
2016-01-08 01:33:25 +08:00
|
|
|
}
|
|
|
|
|
2016-10-20 16:36:42 +08:00
|
|
|
static StringRef getString(opt::InputArgList &Args, unsigned Key,
|
|
|
|
StringRef Default = "") {
|
2015-10-08 03:34:51 +08:00
|
|
|
if (auto *Arg = Args.getLastArg(Key))
|
|
|
|
return Arg->getValue();
|
|
|
|
return Default;
|
|
|
|
}
|
|
|
|
|
2016-04-01 05:15:31 +08:00
|
|
|
static int getInteger(opt::InputArgList &Args, unsigned Key, int Default) {
|
|
|
|
int V = Default;
|
|
|
|
if (auto *Arg = Args.getLastArg(Key)) {
|
|
|
|
StringRef S = Arg->getValue();
|
|
|
|
if (S.getAsInteger(10, V))
|
|
|
|
error(Arg->getSpelling() + ": number expected, but got " + S);
|
|
|
|
}
|
|
|
|
return V;
|
|
|
|
}
|
|
|
|
|
2016-06-25 02:02:50 +08:00
|
|
|
static const char *getReproduceOption(opt::InputArgList &Args) {
|
2016-06-25 12:37:56 +08:00
|
|
|
if (auto *Arg = Args.getLastArg(OPT_reproduce))
|
|
|
|
return Arg->getValue();
|
2016-06-25 02:02:50 +08:00
|
|
|
return getenv("LLD_REPRODUCE");
|
|
|
|
}
|
|
|
|
|
2015-11-13 03:00:37 +08:00
|
|
|
static bool hasZOption(opt::InputArgList &Args, StringRef Key) {
|
|
|
|
for (auto *Arg : Args.filtered(OPT_z))
|
|
|
|
if (Key == Arg->getValue())
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-10-20 16:36:42 +08:00
|
|
|
static uint64_t getZOptionValue(opt::InputArgList &Args, StringRef Key,
|
|
|
|
uint64_t Default) {
|
2016-08-18 07:59:58 +08:00
|
|
|
for (auto *Arg : Args.filtered(OPT_z)) {
|
|
|
|
StringRef Value = Arg->getValue();
|
|
|
|
size_t Pos = Value.find("=");
|
2016-09-28 23:20:47 +08:00
|
|
|
if (Pos != StringRef::npos && Key == Value.substr(0, Pos)) {
|
|
|
|
Value = Value.substr(Pos + 1);
|
|
|
|
uint64_t Result;
|
|
|
|
if (Value.getAsInteger(0, Result))
|
|
|
|
error("invalid " + Key + ": " + Value);
|
|
|
|
return Result;
|
|
|
|
}
|
2016-08-18 07:59:58 +08:00
|
|
|
}
|
2016-09-28 23:20:47 +08:00
|
|
|
return Default;
|
2016-08-18 07:59:58 +08:00
|
|
|
}
|
|
|
|
|
2016-11-26 23:10:01 +08:00
|
|
|
// Parse -color-diagnostics={auto,always,never} or -no-color-diagnostics.
|
|
|
|
static bool getColorDiagnostics(opt::InputArgList &Args) {
|
|
|
|
bool Default = (ErrorOS == &errs() && Process::StandardErrHasColors());
|
|
|
|
|
|
|
|
auto *Arg = Args.getLastArg(OPT_color_diagnostics, OPT_no_color_diagnostics);
|
|
|
|
if (!Arg)
|
|
|
|
return Default;
|
|
|
|
if (Arg->getOption().getID() == OPT_no_color_diagnostics)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
StringRef S = Arg->getValue();
|
|
|
|
if (S == "auto")
|
|
|
|
return Default;
|
|
|
|
if (S == "always")
|
|
|
|
return true;
|
|
|
|
if (S != "never")
|
|
|
|
error("unknown -color-diagnostics value: " + S);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-10-27 02:59:00 +08:00
|
|
|
void LinkerDriver::main(ArrayRef<const char *> ArgsArr, bool CanExitEarly) {
|
2016-03-16 02:20:50 +08:00
|
|
|
ELFOptTable Parser;
|
|
|
|
opt::InputArgList Args = Parser.parse(ArgsArr.slice(1));
|
2016-11-26 23:10:01 +08:00
|
|
|
|
|
|
|
// Read some flags early because error() depends on them.
|
|
|
|
Config->ErrorLimit = getInteger(Args, OPT_error_limit, 20);
|
|
|
|
Config->ColorDiagnostics = getColorDiagnostics(Args);
|
|
|
|
|
|
|
|
// Handle -help
|
2016-02-28 11:18:09 +08:00
|
|
|
if (Args.hasArg(OPT_help)) {
|
|
|
|
printHelp(ArgsArr[0]);
|
|
|
|
return;
|
|
|
|
}
|
2016-11-20 02:14:24 +08:00
|
|
|
|
|
|
|
// GNU linkers disagree here. Though both -version and -v are mentioned
|
|
|
|
// in help to print the version information, GNU ld just normally exits,
|
|
|
|
// while gold can continue linking. We are compatible with ld.bfd here.
|
|
|
|
if (Args.hasArg(OPT_version) || Args.hasArg(OPT_v))
|
2016-10-20 04:53:07 +08:00
|
|
|
outs() << getLLDVersion() << "\n";
|
2016-11-20 02:14:24 +08:00
|
|
|
if (Args.hasArg(OPT_version))
|
|
|
|
return;
|
|
|
|
|
2016-10-27 02:59:00 +08:00
|
|
|
Config->ExitEarly = CanExitEarly && !Args.hasArg(OPT_full_shutdown);
|
2016-02-28 11:18:07 +08:00
|
|
|
|
2016-06-25 02:02:50 +08:00
|
|
|
if (const char *Path = getReproduceOption(Args)) {
|
2016-05-16 01:10:23 +08:00
|
|
|
// Note that --reproduce is a debug option so you can ignore it
|
|
|
|
// if you are trying to understand the whole picture of the code.
|
2016-07-26 10:00:42 +08:00
|
|
|
ErrorOr<CpioFile *> F = CpioFile::create(Path);
|
|
|
|
if (F) {
|
|
|
|
Cpio.reset(*F);
|
2016-05-16 01:10:23 +08:00
|
|
|
Cpio->append("response.txt", createResponseFile(Args));
|
2016-10-26 11:52:06 +08:00
|
|
|
Cpio->append("version.txt", getLLDVersion() + "\n");
|
2016-07-26 10:00:42 +08:00
|
|
|
} else
|
|
|
|
error(F.getError(),
|
|
|
|
Twine("--reproduce: failed to open ") + Path + ".cpio");
|
2016-05-04 01:30:44 +08:00
|
|
|
}
|
2016-04-26 08:22:24 +08:00
|
|
|
|
2016-06-05 21:19:39 +08:00
|
|
|
readConfigs(Args);
|
|
|
|
initLLVM(Args);
|
2015-10-10 05:07:25 +08:00
|
|
|
createFiles(Args);
|
2016-10-20 12:47:47 +08:00
|
|
|
inferMachineType();
|
2016-01-08 01:33:25 +08:00
|
|
|
checkOptions(Args);
|
2016-11-24 09:43:21 +08:00
|
|
|
if (ErrorCount)
|
2016-02-03 05:13:09 +08:00
|
|
|
return;
|
2015-11-13 02:54:15 +08:00
|
|
|
|
2015-10-14 00:20:50 +08:00
|
|
|
switch (Config->EKind) {
|
2015-10-10 05:07:25 +08:00
|
|
|
case ELF32LEKind:
|
|
|
|
link<ELF32LE>(Args);
|
|
|
|
return;
|
|
|
|
case ELF32BEKind:
|
|
|
|
link<ELF32BE>(Args);
|
|
|
|
return;
|
|
|
|
case ELF64LEKind:
|
|
|
|
link<ELF64LE>(Args);
|
|
|
|
return;
|
|
|
|
case ELF64BEKind:
|
|
|
|
link<ELF64BE>(Args);
|
|
|
|
return;
|
|
|
|
default:
|
2016-10-20 12:47:47 +08:00
|
|
|
llvm_unreachable("unknown Config->EKind");
|
2015-10-10 05:07:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-29 20:35:04 +08:00
|
|
|
static UnresolvedPolicy getUnresolvedSymbolOption(opt::InputArgList &Args) {
|
|
|
|
if (Args.hasArg(OPT_noinhibit_exec))
|
|
|
|
return UnresolvedPolicy::Warn;
|
|
|
|
if (Args.hasArg(OPT_no_undefined) || hasZOption(Args, "defs"))
|
|
|
|
return UnresolvedPolicy::NoUndef;
|
|
|
|
if (Config->Relocatable)
|
|
|
|
return UnresolvedPolicy::Ignore;
|
|
|
|
|
|
|
|
if (auto *Arg = Args.getLastArg(OPT_unresolved_symbols)) {
|
|
|
|
StringRef S = Arg->getValue();
|
|
|
|
if (S == "ignore-all" || S == "ignore-in-object-files")
|
|
|
|
return UnresolvedPolicy::Ignore;
|
|
|
|
if (S == "ignore-in-shared-libs" || S == "report-all")
|
2016-09-03 03:36:29 +08:00
|
|
|
return UnresolvedPolicy::ReportError;
|
2016-06-29 20:35:04 +08:00
|
|
|
error("unknown --unresolved-symbols value: " + S);
|
|
|
|
}
|
2016-09-03 03:36:29 +08:00
|
|
|
return UnresolvedPolicy::ReportError;
|
2016-06-29 20:35:04 +08:00
|
|
|
}
|
|
|
|
|
2016-10-18 02:12:24 +08:00
|
|
|
static Target2Policy getTarget2Option(opt::InputArgList &Args) {
|
|
|
|
if (auto *Arg = Args.getLastArg(OPT_target2)) {
|
|
|
|
StringRef S = Arg->getValue();
|
|
|
|
if (S == "rel")
|
|
|
|
return Target2Policy::Rel;
|
|
|
|
if (S == "abs")
|
|
|
|
return Target2Policy::Abs;
|
|
|
|
if (S == "got-rel")
|
|
|
|
return Target2Policy::GotRel;
|
|
|
|
error("unknown --target2 option: " + S);
|
|
|
|
}
|
|
|
|
return Target2Policy::GotRel;
|
|
|
|
}
|
|
|
|
|
2016-08-25 17:05:47 +08:00
|
|
|
static bool isOutputFormatBinary(opt::InputArgList &Args) {
|
|
|
|
if (auto *Arg = Args.getLastArg(OPT_oformat)) {
|
|
|
|
StringRef S = Arg->getValue();
|
|
|
|
if (S == "binary")
|
|
|
|
return true;
|
|
|
|
error("unknown --oformat value: " + S);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-08-31 16:53:21 +08:00
|
|
|
static bool getArg(opt::InputArgList &Args, unsigned K1, unsigned K2,
|
|
|
|
bool Default) {
|
|
|
|
if (auto *Arg = Args.getLastArg(K1, K2))
|
|
|
|
return Arg->getOption().getID() == K1;
|
|
|
|
return Default;
|
|
|
|
}
|
|
|
|
|
2016-08-31 16:46:30 +08:00
|
|
|
static DiscardPolicy getDiscardOption(opt::InputArgList &Args) {
|
|
|
|
auto *Arg =
|
|
|
|
Args.getLastArg(OPT_discard_all, OPT_discard_locals, OPT_discard_none);
|
|
|
|
if (!Arg)
|
|
|
|
return DiscardPolicy::Default;
|
2016-09-03 03:49:27 +08:00
|
|
|
if (Arg->getOption().getID() == OPT_discard_all)
|
2016-08-31 16:46:30 +08:00
|
|
|
return DiscardPolicy::All;
|
2016-09-03 03:49:27 +08:00
|
|
|
if (Arg->getOption().getID() == OPT_discard_locals)
|
2016-08-31 16:46:30 +08:00
|
|
|
return DiscardPolicy::Locals;
|
2016-09-03 03:49:27 +08:00
|
|
|
return DiscardPolicy::None;
|
2016-08-31 16:46:30 +08:00
|
|
|
}
|
|
|
|
|
2016-08-31 16:38:11 +08:00
|
|
|
static StripPolicy getStripOption(opt::InputArgList &Args) {
|
|
|
|
if (auto *Arg = Args.getLastArg(OPT_strip_all, OPT_strip_debug)) {
|
|
|
|
if (Arg->getOption().getID() == OPT_strip_all)
|
|
|
|
return StripPolicy::All;
|
|
|
|
return StripPolicy::Debug;
|
|
|
|
}
|
|
|
|
return StripPolicy::None;
|
|
|
|
}
|
|
|
|
|
2016-09-14 21:07:13 +08:00
|
|
|
static uint64_t parseSectionAddress(StringRef S, opt::Arg *Arg) {
|
|
|
|
uint64_t VA = 0;
|
|
|
|
if (S.startswith("0x"))
|
|
|
|
S = S.drop_front(2);
|
|
|
|
if (S.getAsInteger(16, VA))
|
|
|
|
error("invalid argument: " + stringize(Arg));
|
|
|
|
return VA;
|
|
|
|
}
|
|
|
|
|
|
|
|
static StringMap<uint64_t> getSectionStartMap(opt::InputArgList &Args) {
|
|
|
|
StringMap<uint64_t> Ret;
|
|
|
|
for (auto *Arg : Args.filtered(OPT_section_start)) {
|
|
|
|
StringRef Name;
|
|
|
|
StringRef Addr;
|
|
|
|
std::tie(Name, Addr) = StringRef(Arg->getValue()).split('=');
|
|
|
|
Ret[Name] = parseSectionAddress(Addr, Arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (auto *Arg = Args.getLastArg(OPT_Ttext))
|
|
|
|
Ret[".text"] = parseSectionAddress(Arg->getValue(), Arg);
|
|
|
|
if (auto *Arg = Args.getLastArg(OPT_Tdata))
|
|
|
|
Ret[".data"] = parseSectionAddress(Arg->getValue(), Arg);
|
|
|
|
if (auto *Arg = Args.getLastArg(OPT_Tbss))
|
|
|
|
Ret[".bss"] = parseSectionAddress(Arg->getValue(), Arg);
|
|
|
|
return Ret;
|
|
|
|
}
|
|
|
|
|
2016-09-17 04:21:55 +08:00
|
|
|
static SortSectionPolicy getSortKind(opt::InputArgList &Args) {
|
|
|
|
StringRef S = getString(Args, OPT_sort_section);
|
|
|
|
if (S == "alignment")
|
|
|
|
return SortSectionPolicy::Alignment;
|
|
|
|
if (S == "name")
|
|
|
|
return SortSectionPolicy::Name;
|
|
|
|
if (!S.empty())
|
|
|
|
error("unknown --sort-section rule: " + S);
|
2016-09-17 05:14:55 +08:00
|
|
|
return SortSectionPolicy::Default;
|
2016-09-17 04:21:55 +08:00
|
|
|
}
|
|
|
|
|
2016-11-10 17:05:20 +08:00
|
|
|
// Parse the --symbol-ordering-file argument. File has form:
|
|
|
|
// symbolName1
|
|
|
|
// [...]
|
|
|
|
// symbolNameN
|
|
|
|
static void parseSymbolOrderingList(MemoryBufferRef MB) {
|
|
|
|
unsigned I = 0;
|
|
|
|
SmallVector<StringRef, 0> Arr;
|
|
|
|
MB.getBuffer().split(Arr, '\n');
|
|
|
|
for (StringRef S : Arr)
|
|
|
|
Config->SymbolOrderingFile.insert({CachedHashStringRef(S.trim()), I++});
|
|
|
|
}
|
|
|
|
|
2016-01-08 01:54:19 +08:00
|
|
|
// Initializes Config members by the command line options.
|
|
|
|
void LinkerDriver::readConfigs(opt::InputArgList &Args) {
|
2016-03-09 01:13:12 +08:00
|
|
|
for (auto *Arg : Args.filtered(OPT_L))
|
|
|
|
Config->SearchPaths.push_back(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-11-25 02:55:36 +08:00
|
|
|
if (auto *Arg = Args.getLastArg(OPT_m)) {
|
2016-01-12 09:33:23 +08:00
|
|
|
// Parse ELF{32,64}{LE,BE} and CPU type.
|
2015-11-25 02:55:36 +08:00
|
|
|
StringRef S = Arg->getValue();
|
2016-11-10 06:32:43 +08:00
|
|
|
std::tie(Config->EKind, Config->EMachine, Config->OSABI) =
|
|
|
|
parseEmulation(S);
|
|
|
|
Config->MipsN32Abi = (S == "elf32btsmipn32" || S == "elf32ltsmipn32");
|
2015-11-25 02:55:36 +08:00
|
|
|
Config->Emulation = S;
|
|
|
|
}
|
|
|
|
|
2015-09-30 06:33:18 +08:00
|
|
|
Config->AllowMultipleDefinition = Args.hasArg(OPT_allow_multiple_definition);
|
2015-10-14 05:02:34 +08:00
|
|
|
Config->Bsymbolic = Args.hasArg(OPT_Bsymbolic);
|
2016-02-02 17:28:53 +08:00
|
|
|
Config->BsymbolicFunctions = Args.hasArg(OPT_Bsymbolic_functions);
|
2016-08-31 16:53:21 +08:00
|
|
|
Config->Demangle = getArg(Args, OPT_demangle, OPT_no_demangle, true);
|
2016-04-03 11:39:09 +08:00
|
|
|
Config->DisableVerify = Args.hasArg(OPT_disable_verify);
|
2016-08-31 16:46:30 +08:00
|
|
|
Config->Discard = getDiscardOption(Args);
|
2016-01-15 21:34:52 +08:00
|
|
|
Config->EhFrameHdr = Args.hasArg(OPT_eh_frame_hdr);
|
2015-10-07 00:20:00 +08:00
|
|
|
Config->EnableNewDtags = !Args.hasArg(OPT_disable_new_dtags);
|
2015-09-30 06:33:18 +08:00
|
|
|
Config->ExportDynamic = Args.hasArg(OPT_export_dynamic);
|
2016-07-04 21:43:12 +08:00
|
|
|
Config->FatalWarnings = Args.hasArg(OPT_fatal_warnings);
|
2016-09-07 02:28:49 +08:00
|
|
|
Config->GcSections = getArg(Args, OPT_gc_sections, OPT_no_gc_sections, false);
|
2016-10-20 17:19:48 +08:00
|
|
|
Config->GdbIndex = Args.hasArg(OPT_gdb_index);
|
2016-02-26 02:43:51 +08:00
|
|
|
Config->ICF = Args.hasArg(OPT_icf);
|
2016-04-08 04:41:41 +08:00
|
|
|
Config->NoGnuUnique = Args.hasArg(OPT_no_gnu_unique);
|
2016-06-28 16:07:26 +08:00
|
|
|
Config->NoUndefinedVersion = Args.hasArg(OPT_no_undefined_version);
|
2016-09-03 03:20:33 +08:00
|
|
|
Config->Nostdlib = Args.hasArg(OPT_nostdlib);
|
2016-10-17 22:42:11 +08:00
|
|
|
Config->Pie = getArg(Args, OPT_pie, OPT_nopie, false);
|
2015-12-10 17:12:18 +08:00
|
|
|
Config->PrintGcSections = Args.hasArg(OPT_print_gc_sections);
|
2016-02-25 16:23:37 +08:00
|
|
|
Config->Relocatable = Args.hasArg(OPT_relocatable);
|
2016-03-10 04:01:08 +08:00
|
|
|
Config->SaveTemps = Args.hasArg(OPT_save_temps);
|
2016-11-28 18:11:10 +08:00
|
|
|
Config->SingleRoRx = Args.hasArg(OPT_no_rosegment);
|
2015-09-30 06:33:18 +08:00
|
|
|
Config->Shared = Args.hasArg(OPT_shared);
|
2016-08-31 16:53:21 +08:00
|
|
|
Config->Target1Rel = getArg(Args, OPT_target1_rel, OPT_target1_abs, false);
|
2016-11-18 01:06:51 +08:00
|
|
|
Config->Threads = getArg(Args, OPT_threads, OPT_no_threads, true);
|
2016-03-29 16:45:40 +08:00
|
|
|
Config->Trace = Args.hasArg(OPT_trace);
|
2015-10-11 10:03:03 +08:00
|
|
|
Config->Verbose = Args.hasArg(OPT_verbose);
|
2016-03-14 17:19:30 +08:00
|
|
|
Config->WarnCommon = Args.hasArg(OPT_warn_common);
|
2015-09-30 06:33:18 +08:00
|
|
|
|
2016-11-23 13:25:02 +08:00
|
|
|
if (Config->EMachine == EM_MIPS)
|
|
|
|
// For now MipsGotSection class is not ready for concurent access
|
|
|
|
// from multiple thread. The problem is in the getPageEntryOffset
|
|
|
|
// method. So turn Threads off for this target.
|
|
|
|
Config->Threads = false;
|
|
|
|
|
2015-10-08 03:34:51 +08: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");
|
2016-11-26 13:37:04 +08:00
|
|
|
Config->LTOAAPipeline = getString(Args, OPT_lto_aa_pipeline);
|
|
|
|
Config->LTONewPmPasses = getString(Args, OPT_lto_newpm_passes);
|
2015-10-09 08:33:44 +08:00
|
|
|
Config->OutputFile = getString(Args, OPT_o);
|
2015-10-08 03:34:51 +08:00
|
|
|
Config->SoName = getString(Args, OPT_soname);
|
|
|
|
Config->Sysroot = getString(Args, OPT_sysroot);
|
|
|
|
|
2016-04-30 00:12:29 +08:00
|
|
|
Config->Optimize = getInteger(Args, OPT_O, 1);
|
2016-11-26 13:37:04 +08:00
|
|
|
Config->LTOO = getInteger(Args, OPT_lto_O, 2);
|
|
|
|
if (Config->LTOO > 3)
|
2016-04-03 10:41:15 +08:00
|
|
|
error("invalid optimization level for LTO: " + getString(Args, OPT_lto_O));
|
2016-11-26 13:37:04 +08:00
|
|
|
Config->LTOPartitions = getInteger(Args, OPT_lto_partitions, 1);
|
|
|
|
if (Config->LTOPartitions == 0)
|
2016-10-11 07:12:14 +08:00
|
|
|
error("--lto-partitions: number of threads must be > 0");
|
2016-11-26 13:37:04 +08:00
|
|
|
Config->ThinLTOJobs = getInteger(Args, OPT_thinlto_jobs, -1u);
|
|
|
|
if (Config->ThinLTOJobs == 0)
|
2016-10-11 07:12:14 +08:00
|
|
|
error("--thinlto-jobs: number of threads must be > 0");
|
2016-04-01 05:15:31 +08:00
|
|
|
|
2016-05-10 23:47:57 +08:00
|
|
|
Config->ZCombreloc = !hasZOption(Args, "nocombreloc");
|
2016-10-12 01:46:48 +08:00
|
|
|
Config->ZExecstack = hasZOption(Args, "execstack");
|
2015-11-13 03:00:37 +08:00
|
|
|
Config->ZNodelete = hasZOption(Args, "nodelete");
|
|
|
|
Config->ZNow = hasZOption(Args, "now");
|
|
|
|
Config->ZOrigin = hasZOption(Args, "origin");
|
2015-11-24 18:15:50 +08:00
|
|
|
Config->ZRelro = !hasZOption(Args, "norelro");
|
2016-10-20 13:23:23 +08:00
|
|
|
Config->ZStackSize = getZOptionValue(Args, "stack-size", -1);
|
2016-10-14 18:34:36 +08:00
|
|
|
Config->ZWxneeded = hasZOption(Args, "wxneeded");
|
2015-11-13 03:00:37 +08:00
|
|
|
|
2016-10-20 13:23:23 +08:00
|
|
|
Config->OFormatBinary = isOutputFormatBinary(Args);
|
|
|
|
Config->SectionStartMap = getSectionStartMap(Args);
|
|
|
|
Config->SortSection = getSortKind(Args);
|
|
|
|
Config->Target2 = getTarget2Option(Args);
|
|
|
|
Config->UnresolvedSymbols = getUnresolvedSymbolOption(Args);
|
|
|
|
|
2016-08-31 16:38:11 +08:00
|
|
|
if (!Config->Relocatable)
|
|
|
|
Config->Strip = getStripOption(Args);
|
|
|
|
|
2016-04-08 05:04:51 +08:00
|
|
|
// Config->Pic is true if we are generating position-independent code.
|
|
|
|
Config->Pic = Config->Pie || Config->Shared;
|
|
|
|
|
2015-10-22 16:21:35 +08:00
|
|
|
if (auto *Arg = Args.getLastArg(OPT_hash_style)) {
|
|
|
|
StringRef S = Arg->getValue();
|
|
|
|
if (S == "gnu") {
|
|
|
|
Config->GnuHash = true;
|
|
|
|
Config->SysvHash = false;
|
|
|
|
} else if (S == "both") {
|
|
|
|
Config->GnuHash = true;
|
|
|
|
} else if (S != "sysv")
|
2016-03-12 16:31:34 +08:00
|
|
|
error("unknown hash style: " + S);
|
2015-10-22 16:21:35 +08:00
|
|
|
}
|
|
|
|
|
2016-04-08 06:49:21 +08:00
|
|
|
// Parse --build-id or --build-id=<style>.
|
|
|
|
if (Args.hasArg(OPT_build_id))
|
2016-09-14 19:32:57 +08:00
|
|
|
Config->BuildId = BuildIdKind::Fast;
|
2016-04-08 06:49:21 +08:00
|
|
|
if (auto *Arg = Args.getLastArg(OPT_build_id_eq)) {
|
|
|
|
StringRef S = Arg->getValue();
|
2016-05-14 05:55:56 +08:00
|
|
|
if (S == "md5") {
|
2016-04-08 06:49:21 +08:00
|
|
|
Config->BuildId = BuildIdKind::Md5;
|
2016-11-17 01:14:11 +08:00
|
|
|
} else if (S == "sha1" || S == "tree") {
|
2016-04-08 07:51:56 +08:00
|
|
|
Config->BuildId = BuildIdKind::Sha1;
|
2016-08-26 17:55:37 +08:00
|
|
|
} else if (S == "uuid") {
|
|
|
|
Config->BuildId = BuildIdKind::Uuid;
|
2016-05-14 05:55:56 +08:00
|
|
|
} else if (S == "none") {
|
2016-05-04 04:55:47 +08:00
|
|
|
Config->BuildId = BuildIdKind::None;
|
2016-05-14 05:55:56 +08:00
|
|
|
} else if (S.startswith("0x")) {
|
|
|
|
Config->BuildId = BuildIdKind::Hexstring;
|
2016-06-29 16:01:32 +08:00
|
|
|
Config->BuildIdVector = parseHex(S.substr(2));
|
2016-05-14 05:55:56 +08:00
|
|
|
} else {
|
2016-04-08 06:49:21 +08:00
|
|
|
error("unknown --build-id style: " + S);
|
2016-05-14 05:55:56 +08:00
|
|
|
}
|
2016-04-08 06:49:21 +08:00
|
|
|
}
|
|
|
|
|
2016-09-02 17:13:05 +08:00
|
|
|
for (auto *Arg : Args.filtered(OPT_auxiliary))
|
|
|
|
Config->AuxiliaryList.push_back(Arg->getValue());
|
|
|
|
if (!Config->Shared && !Config->AuxiliaryList.empty())
|
|
|
|
error("-f may not be used without -shared");
|
|
|
|
|
2015-10-20 01:35:12 +08:00
|
|
|
for (auto *Arg : Args.filtered(OPT_undefined))
|
|
|
|
Config->Undefined.push_back(Arg->getValue());
|
2016-04-14 02:51:11 +08:00
|
|
|
|
2016-04-28 10:08:54 +08:00
|
|
|
if (auto *Arg = Args.getLastArg(OPT_dynamic_list))
|
|
|
|
if (Optional<MemoryBufferRef> Buffer = readFile(Arg->getValue()))
|
2016-04-23 04:21:26 +08:00
|
|
|
parseDynamicList(*Buffer);
|
2016-04-23 02:44:06 +08:00
|
|
|
|
2016-11-10 17:05:20 +08:00
|
|
|
if (auto *Arg = Args.getLastArg(OPT_symbol_ordering_file))
|
|
|
|
if (Optional<MemoryBufferRef> Buffer = readFile(Arg->getValue()))
|
|
|
|
parseSymbolOrderingList(*Buffer);
|
|
|
|
|
2016-04-23 02:44:06 +08:00
|
|
|
for (auto *Arg : Args.filtered(OPT_export_dynamic_symbol))
|
|
|
|
Config->DynamicList.push_back(Arg->getValue());
|
2016-04-23 04:21:26 +08:00
|
|
|
|
2016-07-18 01:23:17 +08:00
|
|
|
if (auto *Arg = Args.getLastArg(OPT_version_script))
|
2016-04-28 10:08:54 +08:00
|
|
|
if (Optional<MemoryBufferRef> Buffer = readFile(Arg->getValue()))
|
2016-08-31 17:08:26 +08:00
|
|
|
readVersionScript(*Buffer);
|
2016-01-08 01:54:19 +08:00
|
|
|
}
|
2015-10-20 01:35:12 +08:00
|
|
|
|
2016-10-20 12:36:36 +08:00
|
|
|
// Returns a value of "-format" option.
|
2016-10-20 12:47:45 +08:00
|
|
|
static bool getBinaryOption(StringRef S) {
|
2016-10-20 12:36:36 +08:00
|
|
|
if (S == "binary")
|
|
|
|
return true;
|
|
|
|
if (S == "elf" || S == "default")
|
|
|
|
return false;
|
2016-10-20 12:47:45 +08:00
|
|
|
error("unknown -format value: " + S +
|
2016-10-20 12:36:36 +08:00
|
|
|
" (supported formats: elf, default, binary)");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-01-08 01:54:19 +08:00
|
|
|
void LinkerDriver::createFiles(opt::InputArgList &Args) {
|
2015-10-02 00:42:03 +08:00
|
|
|
for (auto *Arg : Args) {
|
|
|
|
switch (Arg->getOption().getID()) {
|
|
|
|
case OPT_l:
|
2016-02-03 05:13:09 +08:00
|
|
|
addLibrary(Arg->getValue());
|
2015-10-02 00:42:03 +08:00
|
|
|
break;
|
|
|
|
case OPT_INPUT:
|
|
|
|
addFile(Arg->getValue());
|
|
|
|
break;
|
2016-09-10 06:08:04 +08:00
|
|
|
case OPT_alias_script_T:
|
|
|
|
case OPT_script:
|
2016-10-20 13:03:49 +08:00
|
|
|
if (Optional<MemoryBufferRef> MB = readFile(Arg->getValue()))
|
|
|
|
readLinkerScript(*MB);
|
2016-09-10 06:08:04 +08:00
|
|
|
break;
|
2015-10-12 04:59:12 +08:00
|
|
|
case OPT_as_needed:
|
|
|
|
Config->AsNeeded = true;
|
|
|
|
break;
|
2016-10-20 12:36:36 +08:00
|
|
|
case OPT_format:
|
2016-10-20 13:12:29 +08:00
|
|
|
InBinary = getBinaryOption(Arg->getValue());
|
2016-09-10 06:08:04 +08:00
|
|
|
break;
|
2015-10-12 04:59:12 +08:00
|
|
|
case OPT_no_as_needed:
|
|
|
|
Config->AsNeeded = false;
|
|
|
|
break;
|
2015-10-02 00:42:03 +08:00
|
|
|
case OPT_Bstatic:
|
|
|
|
Config->Static = true;
|
|
|
|
break;
|
|
|
|
case OPT_Bdynamic:
|
|
|
|
Config->Static = false;
|
|
|
|
break;
|
2015-10-02 02:02:21 +08:00
|
|
|
case OPT_whole_archive:
|
2016-10-26 12:01:07 +08:00
|
|
|
InWholeArchive = true;
|
2015-10-02 02:02:21 +08:00
|
|
|
break;
|
|
|
|
case OPT_no_whole_archive:
|
2016-10-26 12:01:07 +08:00
|
|
|
InWholeArchive = false;
|
2015-10-02 02:02:21 +08:00
|
|
|
break;
|
2016-04-08 03:24:51 +08:00
|
|
|
case OPT_start_lib:
|
|
|
|
InLib = true;
|
|
|
|
break;
|
|
|
|
case OPT_end_lib:
|
|
|
|
InLib = false;
|
|
|
|
break;
|
2015-09-28 20:52:21 +08:00
|
|
|
}
|
2015-07-25 05:03:07 +08:00
|
|
|
}
|
|
|
|
|
2016-11-24 09:43:21 +08:00
|
|
|
if (Files.empty() && ErrorCount == 0)
|
2016-10-20 02:09:52 +08:00
|
|
|
error("no input files");
|
2016-10-20 12:47:47 +08:00
|
|
|
}
|
2016-06-29 09:30:50 +08:00
|
|
|
|
2016-10-20 12:47:47 +08:00
|
|
|
// If -m <machine_type> was not given, infer it from object files.
|
|
|
|
void LinkerDriver::inferMachineType() {
|
|
|
|
if (Config->EKind != ELFNoneKind)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (InputFile *F : Files) {
|
|
|
|
if (F->EKind == ELFNoneKind)
|
|
|
|
continue;
|
|
|
|
Config->EKind = F->EKind;
|
|
|
|
Config->EMachine = F->EMachine;
|
2016-10-27 22:00:51 +08:00
|
|
|
Config->OSABI = F->OSABI;
|
2016-11-06 06:58:01 +08:00
|
|
|
Config->MipsN32Abi = Config->EMachine == EM_MIPS && isMipsN32Abi(F);
|
2016-10-20 12:47:47 +08:00
|
|
|
return;
|
2016-06-29 09:30:50 +08:00
|
|
|
}
|
2016-10-20 12:47:47 +08:00
|
|
|
error("target emulation unknown: -m or at least one .o file required");
|
2015-10-10 05:07:25 +08:00
|
|
|
}
|
|
|
|
|
2016-10-26 12:34:16 +08:00
|
|
|
// Parses -image-base option.
|
|
|
|
static uint64_t getImageBase(opt::InputArgList &Args) {
|
|
|
|
// Use default if no -image-base option is given.
|
|
|
|
// Because we are using "Target" here, this function
|
|
|
|
// has to be called after the variable is initialized.
|
|
|
|
auto *Arg = Args.getLastArg(OPT_image_base);
|
|
|
|
if (!Arg)
|
|
|
|
return Config->Pic ? 0 : Target->DefaultImageBase;
|
|
|
|
|
|
|
|
StringRef S = Arg->getValue();
|
|
|
|
uint64_t V;
|
|
|
|
if (S.getAsInteger(0, V)) {
|
|
|
|
error("-image-base: number expected, but got " + S);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if ((V % Target->MaxPageSize) != 0)
|
|
|
|
warn("-image-base: address isn't multiple of page size: " + S);
|
|
|
|
return V;
|
|
|
|
}
|
|
|
|
|
2016-04-23 03:58:47 +08:00
|
|
|
// Do actual linking. Note that when this function is called,
|
|
|
|
// all linker scripts have already been parsed.
|
2015-10-10 05:07:25 +08:00
|
|
|
template <class ELFT> void LinkerDriver::link(opt::InputArgList &Args) {
|
|
|
|
SymbolTable<ELFT> Symtab;
|
ELF: New symbol table design.
This patch implements a new design for the symbol table that stores
SymbolBodies within a memory region of the Symbol object. Symbols are mutated
by constructing SymbolBodies in place over existing SymbolBodies, rather
than by mutating pointers. As mentioned in the initial proposal [1], this
memory layout helps reduce the cache miss rate by improving memory locality.
Performance numbers:
old(s) new(s)
Without debug info:
chrome 7.178 6.432 (-11.5%)
LLVMgold.so 0.505 0.502 (-0.5%)
clang 0.954 0.827 (-15.4%)
llvm-as 0.052 0.045 (-15.5%)
With debug info:
scylla 5.695 5.613 (-1.5%)
clang 14.396 14.143 (-1.8%)
Performance counter results show that the fewer required indirections is
indeed the cause of the improved performance. For example, when linking
chrome, stalled cycles decreases from 14,556,444,002 to 12,959,238,310, and
instructions per cycle increases from 0.78 to 0.83. We are also executing
many fewer instructions (15,516,401,933 down to 15,002,434,310), probably
because we spend less time allocating SymbolBodies.
The new mechanism by which symbols are added to the symbol table is by calling
add* functions on the SymbolTable.
In this patch, I handle local symbols by storing them inside "unparented"
SymbolBodies. This is suboptimal, but if we do want to try to avoid allocating
these SymbolBodies, we can probably do that separately.
I also removed a few members from the SymbolBody class that were only being
used to pass information from the input file to the symbol table.
This patch implements the new design for the ELF linker only. I intend to
prepare a similar patch for the COFF linker.
[1] http://lists.llvm.org/pipermail/llvm-dev/2016-April/098832.html
Differential Revision: http://reviews.llvm.org/D19752
llvm-svn: 268178
2016-05-01 12:55:03 +08:00
|
|
|
elf::Symtab<ELFT>::X = &Symtab;
|
2016-04-21 04:13:41 +08:00
|
|
|
|
2016-02-12 05:18:01 +08:00
|
|
|
std::unique_ptr<TargetInfo> TI(createTarget());
|
|
|
|
Target = TI.get();
|
2016-04-21 04:13:41 +08:00
|
|
|
LinkerScript<ELFT> LS;
|
2016-09-08 16:19:13 +08:00
|
|
|
ScriptBase = Script<ELFT>::X = &LS;
|
2015-10-10 05:12:40 +08:00
|
|
|
|
2016-11-06 06:58:01 +08:00
|
|
|
Config->Rela =
|
|
|
|
ELFT::Is64Bits || Config->EMachine == EM_X86_64 || Config->MipsN32Abi;
|
2016-06-29 09:30:50 +08:00
|
|
|
Config->Mips64EL =
|
|
|
|
(Config->EMachine == EM_MIPS && Config->EKind == ELF64LEKind);
|
2016-10-26 12:34:16 +08:00
|
|
|
Config->ImageBase = getImageBase(Args);
|
2016-03-14 04:10:20 +08:00
|
|
|
|
2016-04-23 03:58:47 +08:00
|
|
|
// Default output filename is "a.out" by the Unix tradition.
|
|
|
|
if (Config->OutputFile.empty())
|
|
|
|
Config->OutputFile = "a.out";
|
|
|
|
|
2016-07-18 01:50:09 +08:00
|
|
|
// Handle --trace-symbol.
|
|
|
|
for (auto *Arg : Args.filtered(OPT_trace_symbol))
|
|
|
|
Symtab.trace(Arg->getValue());
|
|
|
|
|
2016-09-28 23:20:47 +08:00
|
|
|
// Initialize Config->MaxPageSize. The default value is defined by
|
|
|
|
// the target, but it can be overriden using the option.
|
|
|
|
Config->MaxPageSize =
|
|
|
|
getZOptionValue(Args, "max-page-size", Target->MaxPageSize);
|
|
|
|
if (!isPowerOf2_64(Config->MaxPageSize))
|
|
|
|
error("max-page-size: value isn't a power of 2");
|
|
|
|
|
2016-09-08 16:57:51 +08:00
|
|
|
// Add all files to the symbol table. After this, the symbol table
|
|
|
|
// contains all known names except a few linker-synthesized symbols.
|
2016-09-14 08:05:51 +08:00
|
|
|
for (InputFile *F : Files)
|
|
|
|
Symtab.addFile(F);
|
2016-09-08 16:57:51 +08:00
|
|
|
|
|
|
|
// Add the start symbol.
|
|
|
|
// It initializes either Config->Entry or Config->EntryAddr.
|
|
|
|
// Note that AMDGPU binaries have no entries.
|
|
|
|
if (!Config->Entry.empty()) {
|
|
|
|
// It is either "-e <addr>" or "-e <symbol>".
|
2016-10-20 08:07:36 +08:00
|
|
|
if (!Config->Entry.getAsInteger(0, Config->EntryAddr))
|
|
|
|
Config->Entry = "";
|
2016-09-08 16:57:51 +08:00
|
|
|
} else if (!Config->Shared && !Config->Relocatable &&
|
|
|
|
Config->EMachine != EM_AMDGPU) {
|
|
|
|
// -e was not specified. Use the default start symbol name
|
|
|
|
// if it is resolvable.
|
|
|
|
Config->Entry = (Config->EMachine == EM_MIPS) ? "__start" : "_start";
|
|
|
|
}
|
2016-10-20 08:07:36 +08:00
|
|
|
|
|
|
|
// If an object file defining the entry symbol is in an archive file,
|
|
|
|
// extract the file now.
|
|
|
|
if (Symtab.find(Config->Entry))
|
|
|
|
Symtab.addUndefined(Config->Entry);
|
2016-09-08 16:57:51 +08:00
|
|
|
|
2016-11-24 09:43:21 +08:00
|
|
|
if (ErrorCount)
|
2016-01-30 03:41:13 +08:00
|
|
|
return; // There were duplicate symbols or incompatible files
|
2015-10-10 05:07:25 +08:00
|
|
|
|
2016-04-27 08:05:03 +08:00
|
|
|
Symtab.scanUndefinedFlags();
|
2016-04-23 02:47:52 +08:00
|
|
|
Symtab.scanShlibUndefined();
|
|
|
|
Symtab.scanDynamicList();
|
2016-04-23 04:21:26 +08:00
|
|
|
Symtab.scanVersionScript();
|
2016-04-23 02:47:52 +08:00
|
|
|
|
2016-11-26 13:37:04 +08:00
|
|
|
Symtab.addCombinedLTOObject();
|
2016-11-24 09:43:21 +08:00
|
|
|
if (ErrorCount)
|
2016-05-16 03:29:38 +08:00
|
|
|
return;
|
2016-02-13 04:54:57 +08:00
|
|
|
|
2016-01-08 01:20:07 +08:00
|
|
|
for (auto *Arg : Args.filtered(OPT_wrap))
|
|
|
|
Symtab.wrap(Arg->getValue());
|
|
|
|
|
2016-11-06 06:37:59 +08:00
|
|
|
// Now that we have a complete list of input files.
|
|
|
|
// Beyond this point, no new files are added.
|
|
|
|
// Aggregate all input sections into one place.
|
|
|
|
for (elf::ObjectFile<ELFT> *F : Symtab.getObjectFiles())
|
|
|
|
for (InputSectionBase<ELFT> *S : F->getSections())
|
2016-11-09 02:23:02 +08:00
|
|
|
if (S && S != &InputSection<ELFT>::Discarded)
|
|
|
|
Symtab.Sections.push_back(S);
|
2016-11-06 06:37:59 +08:00
|
|
|
for (BinaryFile *F : Symtab.getBinaryFiles())
|
|
|
|
for (InputSectionData *S : F->getSections())
|
|
|
|
Symtab.Sections.push_back(cast<InputSection<ELFT>>(S));
|
|
|
|
|
2016-09-14 03:56:25 +08:00
|
|
|
// Do size optimizations: garbage collection and identical code folding.
|
ELF2: Implement --gc-sections.
Section garbage collection is a feature to remove unused sections
from outputs. Unused sections are sections that cannot be reachable
from known GC-root symbols or sections. Naturally the feature is
implemented as a mark-sweep garbage collector.
In this patch, I added Live bit to InputSectionBase. If and only
if Live bit is on, the section will be written to the output.
Starting from GC-root symbols or sections, a new function, markLive(),
visits all reachable sections and sets their Live bits. Writer then
ignores sections whose Live bit is off, so that such sections are
excluded from the output.
This change has small negative impact on performance if you use
the feature because making sections means more work. The time to
link Clang changes from 0.356s to 0.386s, or +8%.
It reduces Clang size from 57,764,984 bytes to 55,296,600 bytes.
That is 4.3% reduction.
http://reviews.llvm.org/D13950
llvm-svn: 251043
2015-10-23 02:49:53 +08:00
|
|
|
if (Config->GcSections)
|
2016-05-03 03:30:42 +08:00
|
|
|
markLive<ELFT>();
|
2016-02-26 02:43:51 +08:00
|
|
|
if (Config->ICF)
|
2016-05-03 03:30:42 +08:00
|
|
|
doIcf<ELFT>();
|
2016-05-24 00:55:43 +08:00
|
|
|
|
|
|
|
// MergeInputSection::splitIntoPieces needs to be called before
|
|
|
|
// any call of MergeInputSection::getOffset. Do that.
|
2016-11-26 04:20:57 +08:00
|
|
|
auto Fn = [](InputSectionBase<ELFT> *S) {
|
|
|
|
if (!S->Live)
|
|
|
|
return;
|
|
|
|
if (S->Compressed)
|
|
|
|
S->uncompress();
|
|
|
|
if (auto *MS = dyn_cast<MergeInputSection<ELFT>>(S))
|
|
|
|
MS->splitIntoPieces();
|
|
|
|
};
|
|
|
|
if (Config->Threads)
|
|
|
|
parallel_for_each(Symtab.Sections.begin(), Symtab.Sections.end(), Fn);
|
|
|
|
else
|
|
|
|
std::for_each(Symtab.Sections.begin(), Symtab.Sections.end(), Fn);
|
2016-05-24 00:55:43 +08:00
|
|
|
|
2016-09-14 03:56:25 +08:00
|
|
|
// Write the result to the file.
|
2016-08-09 11:38:23 +08:00
|
|
|
writeResult<ELFT>();
|
2015-07-25 05:03:07 +08:00
|
|
|
}
|