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-06-29 16:01:32 +08:00
|
|
|
#include "Strings.h"
|
2016-04-23 04:21:26 +08:00
|
|
|
#include "SymbolListFile.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-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-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-02-28 08:25:54 +08:00
|
|
|
bool elf::link(ArrayRef<const char *> Args, raw_ostream &Error) {
|
ELF: Rename error -> fatal and redefine error as a non-noreturn function.
In many situations, we don't want to exit at the first error even in the
process model. For example, it is better to report all undefined symbols
rather than reporting the first one that the linker picked up randomly.
In order to handle such errors, we don't need to wrap everything with
ErrorOr (thanks for David Blaikie for pointing this out!) Instead, we
can set a flag to record the fact that we found an error and keep it
going until it reaches a reasonable checkpoint.
This idea should be applicable to other places. For example, we can
ignore broken relocations and check for errors after visiting all relocs.
In this patch, I rename error to fatal, and introduce another version of
error which doesn't call exit. That function instead sets HasError to true.
Once HasError becomes true, it stays true, so that we know that there
was an error if it is true.
I think introducing a non-noreturn error reporting function is by itself
a good idea, and it looks to me that this also provides a gradual path
towards lld-as-a-library (or at least embed-lld-to-your-program) without
sacrificing code readability with lots of ErrorOr's.
http://reviews.llvm.org/D16641
llvm-svn: 259069
2016-01-29 02:40:06 +08:00
|
|
|
HasError = false;
|
2016-02-03 06:49:32 +08:00
|
|
|
ErrorOS = &Error;
|
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-02-28 11:18:09 +08:00
|
|
|
Driver->main(Args);
|
ELF: Rename error -> fatal and redefine error as a non-noreturn function.
In many situations, we don't want to exit at the first error even in the
process model. For example, it is better to report all undefined symbols
rather than reporting the first one that the linker picked up randomly.
In order to handle such errors, we don't need to wrap everything with
ErrorOr (thanks for David Blaikie for pointing this out!) Instead, we
can set a flag to record the fact that we found an error and keep it
going until it reaches a reasonable checkpoint.
This idea should be applicable to other places. For example, we can
ignore broken relocations and check for errors after visiting all relocs.
In this patch, I rename error to fatal, and introduce another version of
error which doesn't call exit. That function instead sets HasError to true.
Once HasError becomes true, it stays true, so that we know that there
was an error if it is true.
I think introducing a non-noreturn error reporting function is by itself
a good idea, and it looks to me that this also provides a gradual path
towards lld-as-a-library (or at least embed-lld-to-your-program) without
sacrificing code readability with lots of ErrorOr's.
http://reviews.llvm.org/D16641
llvm-svn: 259069
2016-01-29 02:40:06 +08:00
|
|
|
return !HasError;
|
2015-07-25 05:03:07 +08:00
|
|
|
}
|
|
|
|
|
2016-06-08 01:55:05 +08:00
|
|
|
// Parses a linker -m option.
|
2015-10-11 10:22:31 +08:00
|
|
|
static std::pair<ELFKind, uint16_t> parseEmulation(StringRef S) {
|
2016-04-01 04:26:30 +08:00
|
|
|
if (S.endswith("_fbsd"))
|
|
|
|
S = S.drop_back(5);
|
2016-06-08 01:55:05 +08:00
|
|
|
|
|
|
|
std::pair<ELFKind, uint16_t> Ret =
|
|
|
|
StringSwitch<std::pair<ELFKind, uint16_t>>(S)
|
|
|
|
.Case("aarch64linux", {ELF64LEKind, EM_AARCH64})
|
|
|
|
.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})
|
|
|
|
.Case("elf32ppc", {ELF32BEKind, EM_PPC})
|
|
|
|
.Case("elf64btsmip", {ELF64BEKind, EM_MIPS})
|
|
|
|
.Case("elf64ltsmip", {ELF64LEKind, EM_MIPS})
|
|
|
|
.Case("elf64ppc", {ELF64BEKind, EM_PPC64})
|
|
|
|
.Case("elf_i386", {ELF32LEKind, EM_386})
|
|
|
|
.Case("elf_x86_64", {ELF64LEKind, EM_X86_64})
|
|
|
|
.Default({ELFNoneKind, EM_NONE});
|
|
|
|
|
|
|
|
if (Ret.first == ELFNoneKind) {
|
|
|
|
if (S == "i386pe" || S == "i386pep" || S == "thumb2pe")
|
|
|
|
error("Windows targets are not supported on the ELF frontend: " + S);
|
|
|
|
else
|
|
|
|
error("unknown emulation: " + S);
|
|
|
|
}
|
|
|
|
return Ret;
|
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-03-15 05:31:07 +08:00
|
|
|
check(Archive::create(MB), "failed to parse archive");
|
2016-01-06 08:51:35 +08:00
|
|
|
|
|
|
|
std::vector<MemoryBufferRef> V;
|
2016-07-14 10:35:18 +08:00
|
|
|
Error Err;
|
|
|
|
for (const ErrorOr<Archive::Child> &COrErr : File->children(Err)) {
|
2016-03-15 05:31:07 +08:00
|
|
|
Archive::Child C = check(COrErr, "could not get the child of the archive " +
|
2016-03-04 00:21:44 +08:00
|
|
|
File->getFileName());
|
2016-04-03 03:09:07 +08:00
|
|
|
MemoryBufferRef MBRef =
|
2016-03-04 06:24:39 +08:00
|
|
|
check(C.getMemoryBufferRef(),
|
2016-03-15 05:31:07 +08:00
|
|
|
"could not get the buffer for a child of the archive " +
|
2016-03-04 00:21:44 +08:00
|
|
|
File->getFileName());
|
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)
|
|
|
|
Error(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.
|
|
|
|
void LinkerDriver::addFile(StringRef Path) {
|
2016-05-03 03:59:56 +08:00
|
|
|
using namespace sys::fs;
|
2016-04-14 02:07:57 +08:00
|
|
|
if (Config->Verbose)
|
2016-05-03 03:59:56 +08:00
|
|
|
outs() << Path << "\n";
|
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
|
|
|
|
|
|
|
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:
|
2015-10-10 05:07:25 +08:00
|
|
|
if (WholeArchive) {
|
2016-01-06 08:51:35 +08:00
|
|
|
for (MemoryBufferRef MB : getArchiveMembers(MBRef))
|
2016-03-10 05:15:17 +08:00
|
|
|
Files.push_back(createObjectFile(MB, Path));
|
2015-10-10 05:07:25 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
Files.push_back(make_unique<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-01-06 08:09:43 +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)
|
|
|
|
Files.push_back(make_unique<LazyObjectFile>(MBRef));
|
|
|
|
else
|
|
|
|
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) {
|
|
|
|
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) {
|
|
|
|
std::string Path = searchLibrary(Name);
|
|
|
|
if (Path.empty())
|
2016-03-12 16:31:34 +08:00
|
|
|
error("unable to find library -l" + Name);
|
2016-04-25 02:23:21 +08:00
|
|
|
else
|
|
|
|
addFile(Path);
|
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();
|
|
|
|
|
2016-04-29 03:30:41 +08:00
|
|
|
// This is a flag to discard all but GlobalValue names.
|
|
|
|
// We want to enable it by default because it saves memory.
|
|
|
|
// Disable it only when a developer option (-save-temps) is given.
|
|
|
|
Driver->Context.setDiscardValueNames(!Config->SaveTemps);
|
|
|
|
Driver->Context.enableDebugTypeODRUniquing();
|
|
|
|
|
2016-04-03 02:18:44 +08:00
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2015-10-08 03:34:51 +08:00
|
|
|
static StringRef
|
|
|
|
getString(opt::InputArgList &Args, unsigned Key, StringRef Default = "") {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-10-10 05:07:25 +08:00
|
|
|
void LinkerDriver::main(ArrayRef<const char *> ArgsArr) {
|
2016-03-16 02:20:50 +08:00
|
|
|
ELFOptTable Parser;
|
|
|
|
opt::InputArgList Args = Parser.parse(ArgsArr.slice(1));
|
2016-02-28 11:18:09 +08:00
|
|
|
if (Args.hasArg(OPT_help)) {
|
|
|
|
printHelp(ArgsArr[0]);
|
|
|
|
return;
|
|
|
|
}
|
2016-02-28 11:18:07 +08:00
|
|
|
if (Args.hasArg(OPT_version)) {
|
2016-06-06 23:34:37 +08:00
|
|
|
outs() << getVersionString();
|
2016-02-28 11:18:07 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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-06-06 23:34:37 +08:00
|
|
|
Cpio->append("version.txt", getVersionString());
|
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-01-08 01:33:25 +08:00
|
|
|
checkOptions(Args);
|
2016-02-03 05:13:09 +08:00
|
|
|
if (HasError)
|
|
|
|
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-02-03 05:13:09 +08:00
|
|
|
error("-m or at least a .o file required");
|
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")
|
|
|
|
return UnresolvedPolicy::Error;
|
|
|
|
error("unknown --unresolved-symbols value: " + S);
|
|
|
|
}
|
|
|
|
return UnresolvedPolicy::Error;
|
|
|
|
}
|
|
|
|
|
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-01-12 09:33:23 +08:00
|
|
|
std::tie(Config->EKind, Config->EMachine) = parseEmulation(S);
|
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-01-14 02:55:39 +08:00
|
|
|
Config->Demangle = !Args.hasArg(OPT_no_demangle);
|
2016-04-03 11:39:09 +08:00
|
|
|
Config->DisableVerify = Args.hasArg(OPT_disable_verify);
|
2015-09-30 06:33:18 +08:00
|
|
|
Config->DiscardAll = Args.hasArg(OPT_discard_all);
|
|
|
|
Config->DiscardLocals = Args.hasArg(OPT_discard_locals);
|
|
|
|
Config->DiscardNone = Args.hasArg(OPT_discard_none);
|
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);
|
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
|
|
|
Config->GcSections = Args.hasArg(OPT_gc_sections);
|
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-03-17 13:57:33 +08:00
|
|
|
Config->Pie = Args.hasArg(OPT_pie);
|
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);
|
2015-09-30 06:33:18 +08:00
|
|
|
Config->Shared = Args.hasArg(OPT_shared);
|
2015-10-24 16:52:46 +08:00
|
|
|
Config->StripAll = Args.hasArg(OPT_strip_all);
|
2016-04-08 05:04:51 +08:00
|
|
|
Config->StripDebug = Args.hasArg(OPT_strip_debug);
|
2016-03-11 12:23:12 +08:00
|
|
|
Config->Threads = Args.hasArg(OPT_threads);
|
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
|
|
|
|
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-06-03 06:58:11 +08:00
|
|
|
Config->LtoAAPipeline = getString(Args, OPT_lto_aa_pipeline);
|
2016-05-16 03:29:38 +08:00
|
|
|
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-04-01 05:15:31 +08:00
|
|
|
Config->LtoO = getInteger(Args, OPT_lto_O, 2);
|
2016-04-03 10:41:15 +08:00
|
|
|
if (Config->LtoO > 3)
|
|
|
|
error("invalid optimization level for LTO: " + getString(Args, OPT_lto_O));
|
2016-04-16 06:38:10 +08:00
|
|
|
Config->LtoJobs = getInteger(Args, OPT_lto_jobs, 1);
|
|
|
|
if (Config->LtoJobs == 0)
|
|
|
|
error("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");
|
2015-11-25 02:48:16 +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");
|
2015-11-13 03:00:37 +08:00
|
|
|
|
2016-02-25 16:23:37 +08:00
|
|
|
if (Config->Relocatable)
|
|
|
|
Config->StripAll = false;
|
|
|
|
|
2016-04-08 05:04:51 +08:00
|
|
|
// --strip-all implies --strip-debug.
|
|
|
|
if (Config->StripAll)
|
|
|
|
Config->StripDebug = true;
|
|
|
|
|
|
|
|
// 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))
|
|
|
|
Config->BuildId = BuildIdKind::Fnv1;
|
|
|
|
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-05-14 05:55:56 +08:00
|
|
|
} else if (S == "sha1") {
|
2016-04-08 07:51:56 +08:00
|
|
|
Config->BuildId = BuildIdKind::Sha1;
|
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
|
|
|
}
|
|
|
|
|
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-06-29 20:35:04 +08:00
|
|
|
Config->UnresolvedSymbols = getUnresolvedSymbolOption(Args);
|
|
|
|
|
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
|
|
|
|
|
|
|
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-04-23 04:21:26 +08:00
|
|
|
parseVersionScript(*Buffer);
|
2016-01-08 01:54:19 +08:00
|
|
|
}
|
2015-10-20 01:35:12 +08:00
|
|
|
|
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;
|
2016-06-01 14:17:27 +08:00
|
|
|
case OPT_alias_script_T:
|
2015-10-02 00:42:03 +08:00
|
|
|
case OPT_INPUT:
|
2015-10-11 11:53:36 +08:00
|
|
|
case OPT_script:
|
2015-10-02 00:42:03 +08:00
|
|
|
addFile(Arg->getValue());
|
|
|
|
break;
|
2015-10-12 04:59:12 +08:00
|
|
|
case OPT_as_needed:
|
|
|
|
Config->AsNeeded = true;
|
|
|
|
break;
|
|
|
|
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:
|
2015-10-10 05:07:25 +08:00
|
|
|
WholeArchive = true;
|
2015-10-02 02:02:21 +08:00
|
|
|
break;
|
|
|
|
case OPT_no_whole_archive:
|
2015-10-10 05:07:25 +08:00
|
|
|
WholeArchive = 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-02-03 05:13:09 +08:00
|
|
|
if (Files.empty() && !HasError)
|
2016-03-11 22:43:02 +08:00
|
|
|
error("no input files.");
|
2016-06-29 09:30:50 +08:00
|
|
|
|
|
|
|
// If -m <machine_type> was not given, infer it from object files.
|
|
|
|
if (Config->EKind == ELFNoneKind) {
|
|
|
|
for (std::unique_ptr<InputFile> &F : Files) {
|
|
|
|
if (F->EKind == ELFNoneKind)
|
|
|
|
continue;
|
|
|
|
Config->EKind = F->EKind;
|
|
|
|
Config->EMachine = F->EMachine;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-10-10 05:07:25 +08:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
Script<ELFT>::X = &LS;
|
2015-10-10 05:12:40 +08:00
|
|
|
|
2016-07-13 07:28:33 +08:00
|
|
|
Config->Rela = ELFT::Is64Bits || Config->EMachine == EM_X86_64;
|
2016-06-29 09:30:50 +08:00
|
|
|
Config->Mips64EL =
|
|
|
|
(Config->EMachine == EM_MIPS && Config->EKind == ELF64LEKind);
|
2016-03-14 04:10:20 +08:00
|
|
|
|
2016-04-23 03:58:47 +08:00
|
|
|
// Add entry symbol. Note that AMDGPU binaries have no entry points.
|
2016-03-16 06:24:58 +08:00
|
|
|
if (Config->Entry.empty() && !Config->Shared && !Config->Relocatable &&
|
2016-03-17 19:00:27 +08:00
|
|
|
Config->EMachine != EM_AMDGPU)
|
2016-04-23 03:58:47 +08:00
|
|
|
Config->Entry = (Config->EMachine == EM_MIPS) ? "__start" : "_start";
|
2015-10-15 06:20:57 +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-04-23 03:58:47 +08:00
|
|
|
// Set either EntryAddr (if S is a number) or EntrySym (otherwise).
|
2015-12-12 01:46:46 +08:00
|
|
|
if (!Config->Entry.empty()) {
|
|
|
|
StringRef S = Config->Entry;
|
|
|
|
if (S.getAsInteger(0, Config->EntryAddr))
|
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
|
|
|
Config->EntrySym = Symtab.addUndefined(S);
|
2015-12-12 01:46:46 +08:00
|
|
|
}
|
|
|
|
|
2016-07-15 01:43:28 +08:00
|
|
|
// Initialize Config->ImageBase.
|
2016-07-13 03:37:53 +08:00
|
|
|
if (auto *Arg = Args.getLastArg(OPT_image_base)) {
|
|
|
|
StringRef S = Arg->getValue();
|
2016-07-14 02:40:59 +08:00
|
|
|
if (S.getAsInteger(0, Config->ImageBase))
|
2016-07-13 03:37:53 +08:00
|
|
|
error(Arg->getSpelling() + ": number expected, but got " + S);
|
2016-07-14 02:40:59 +08:00
|
|
|
else if ((Config->ImageBase % Target->PageSize) != 0)
|
2016-07-13 03:37:53 +08:00
|
|
|
warning(Arg->getSpelling() + ": address isn't multiple of page size");
|
|
|
|
} else {
|
2016-07-15 01:43:28 +08:00
|
|
|
Config->ImageBase = Config->Pic ? 0 : Target->DefaultImageBase;
|
2016-07-13 03:37:53 +08:00
|
|
|
}
|
|
|
|
|
2015-10-10 05:07:25 +08:00
|
|
|
for (std::unique_ptr<InputFile> &F : Files)
|
|
|
|
Symtab.addFile(std::move(F));
|
2016-01-30 03:41:13 +08:00
|
|
|
if (HasError)
|
|
|
|
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-02-13 04:54:57 +08:00
|
|
|
Symtab.addCombinedLtoObject();
|
2016-05-16 03:29:38 +08:00
|
|
|
if (HasError)
|
|
|
|
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());
|
|
|
|
|
2015-10-08 02:29:51 +08:00
|
|
|
// Write the result to the file.
|
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.
|
|
|
|
for (const std::unique_ptr<elf::ObjectFile<ELFT>> &F :
|
|
|
|
Symtab.getObjectFiles())
|
2016-06-24 19:18:44 +08:00
|
|
|
for (InputSectionBase<ELFT> *S : F->getSections()) {
|
|
|
|
if (!S || S == &InputSection<ELFT>::Discarded || !S->Live)
|
|
|
|
continue;
|
|
|
|
if (S->Compressed)
|
|
|
|
S->uncompress();
|
|
|
|
if (auto *MS = dyn_cast<MergeInputSection<ELFT>>(S))
|
|
|
|
MS->splitIntoPieces();
|
|
|
|
}
|
2016-05-24 00:55:43 +08:00
|
|
|
|
2015-10-10 05:07:25 +08:00
|
|
|
writeResult<ELFT>(&Symtab);
|
2015-07-25 05:03:07 +08:00
|
|
|
}
|