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"
|
2016-02-12 05:17:59 +08:00
|
|
|
#include "LinkerScript.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"
|
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2015-09-12 05:18:56 +08:00
|
|
|
#include "llvm/ADT/StringExtras.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"
|
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;
|
2015-07-25 05:03:07 +08:00
|
|
|
|
|
|
|
using namespace lld;
|
|
|
|
using namespace lld::elf2;
|
|
|
|
|
2016-01-07 04:11:55 +08:00
|
|
|
Configuration *elf2::Config;
|
|
|
|
LinkerDriver *elf2::Driver;
|
2015-10-01 01:06:09 +08:00
|
|
|
|
2016-02-03 06:49:32 +08:00
|
|
|
bool elf2::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;
|
2015-08-18 17:13:25 +08:00
|
|
|
Configuration C;
|
2015-10-01 23:23:09 +08:00
|
|
|
LinkerDriver D;
|
2016-02-12 05:17:59 +08:00
|
|
|
LinkerScript LS;
|
2015-08-18 17:13:25 +08:00
|
|
|
Config = &C;
|
2015-10-01 23:23:09 +08:00
|
|
|
Driver = &D;
|
2016-02-12 05:17:59 +08:00
|
|
|
Script = &LS;
|
2015-10-10 05:07:25 +08:00
|
|
|
Driver->main(Args.slice(1));
|
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
|
|
|
}
|
|
|
|
|
2015-10-11 10:22:31 +08:00
|
|
|
static std::pair<ELFKind, uint16_t> parseEmulation(StringRef S) {
|
2015-10-12 03:46:00 +08:00
|
|
|
if (S == "elf32btsmip")
|
|
|
|
return {ELF32BEKind, EM_MIPS};
|
|
|
|
if (S == "elf32ltsmip")
|
|
|
|
return {ELF32LEKind, EM_MIPS};
|
2016-01-12 10:58:59 +08:00
|
|
|
if (S == "elf32ppc" || S == "elf32ppc_fbsd")
|
2015-10-12 03:46:00 +08:00
|
|
|
return {ELF32BEKind, EM_PPC};
|
2016-01-12 10:58:59 +08:00
|
|
|
if (S == "elf64ppc" || S == "elf64ppc_fbsd")
|
2015-10-12 03:46:00 +08:00
|
|
|
return {ELF64BEKind, EM_PPC64};
|
|
|
|
if (S == "elf_i386")
|
|
|
|
return {ELF32LEKind, EM_386};
|
|
|
|
if (S == "elf_x86_64")
|
|
|
|
return {ELF64LEKind, EM_X86_64};
|
2015-11-20 10:48:53 +08:00
|
|
|
if (S == "aarch64linux")
|
|
|
|
return {ELF64LEKind, EM_AARCH64};
|
2015-11-25 02:55:36 +08:00
|
|
|
if (S == "i386pe" || S == "i386pep" || S == "thumb2pe")
|
2016-02-03 05:13:09 +08:00
|
|
|
error("Windows targets are not supported on the ELF frontend: " + S);
|
|
|
|
else
|
|
|
|
error("Unknown emulation: " + S);
|
|
|
|
return {ELFNoneKind, 0};
|
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.
|
|
|
|
static std::vector<MemoryBufferRef> getArchiveMembers(MemoryBufferRef MB) {
|
|
|
|
ErrorOr<std::unique_ptr<Archive>> FileOrErr = Archive::create(MB);
|
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
|
|
|
fatal(FileOrErr, "Failed to parse archive");
|
2016-01-06 08:51:35 +08:00
|
|
|
std::unique_ptr<Archive> File = std::move(*FileOrErr);
|
|
|
|
|
|
|
|
std::vector<MemoryBufferRef> V;
|
|
|
|
for (const ErrorOr<Archive::Child> &C : File->children()) {
|
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
|
|
|
fatal(C, "Could not get the child of the archive " + File->getFileName());
|
2016-01-06 08:51:35 +08:00
|
|
|
ErrorOr<MemoryBufferRef> MbOrErr = C->getMemoryBufferRef();
|
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
|
|
|
fatal(MbOrErr, "Could not get the buffer for a child of the archive " +
|
2016-01-06 08:51:35 +08:00
|
|
|
File->getFileName());
|
|
|
|
V.push_back(*MbOrErr);
|
|
|
|
}
|
|
|
|
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) {
|
2015-10-01 01:06:09 +08:00
|
|
|
using namespace llvm::sys::fs;
|
2015-10-11 10:03:03 +08:00
|
|
|
if (Config->Verbose)
|
|
|
|
llvm::outs() << Path << "\n";
|
2015-10-01 23:23:09 +08:00
|
|
|
auto MBOrErr = MemoryBuffer::getFile(Path);
|
2016-02-03 05:13:09 +08:00
|
|
|
if (error(MBOrErr, "cannot open " + Path))
|
|
|
|
return;
|
2015-10-01 23:23:09 +08:00
|
|
|
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:
|
2016-02-12 05:38:55 +08:00
|
|
|
Script->read(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-02-02 16:22:41 +08:00
|
|
|
StringRef S = MBRef.getBufferIdentifier();
|
2016-01-06 08:51:35 +08:00
|
|
|
for (MemoryBufferRef MB : getArchiveMembers(MBRef))
|
2016-02-02 16:22:41 +08:00
|
|
|
Files.push_back(createObjectFile(MB, S));
|
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-01-06 08:09:43 +08:00
|
|
|
Files.push_back(createSharedFile(MBRef));
|
2015-10-01 23:23:09 +08:00
|
|
|
return;
|
|
|
|
default:
|
2016-01-06 08:09:43 +08:00
|
|
|
Files.push_back(createObjectFile(MBRef));
|
2015-10-01 23:23:09 +08:00
|
|
|
}
|
2015-10-01 01:06:09 +08:00
|
|
|
}
|
|
|
|
|
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())
|
|
|
|
error("Unable to find library -l" + Name);
|
|
|
|
else
|
|
|
|
addFile(Path);
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
// Traditional linkers can generate re-linkable object files instead
|
|
|
|
// of executables or DSOs. We don't support that since the feature
|
|
|
|
// does not seem to provide more value than the static archiver.
|
|
|
|
if (Args.hasArg(OPT_relocatable))
|
2016-02-03 05:13:09 +08:00
|
|
|
error("-r option is not supported. Use 'ar' command instead.");
|
2016-01-08 01:33:25 +08:00
|
|
|
|
|
|
|
// 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-02-03 05:13:09 +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-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;
|
|
|
|
}
|
|
|
|
|
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) {
|
2015-11-25 02:55:36 +08:00
|
|
|
initSymbols();
|
2015-10-08 07:46:11 +08:00
|
|
|
|
2015-10-12 02:19:01 +08:00
|
|
|
opt::InputArgList Args = parseArgs(&Alloc, ArgsArr);
|
2016-01-08 01:54:19 +08:00
|
|
|
readConfigs(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-01-08 01:54:19 +08:00
|
|
|
// Initializes Config members by the command line options.
|
|
|
|
void LinkerDriver::readConfigs(opt::InputArgList &Args) {
|
2015-10-08 03:34:51 +08:00
|
|
|
for (auto *Arg : Args.filtered(OPT_L))
|
2015-10-11 11:28:42 +08:00
|
|
|
Config->SearchPaths.push_back(Arg->getValue());
|
2015-09-28 23:01:59 +08:00
|
|
|
|
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);
|
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);
|
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);
|
2015-09-30 06:33:18 +08:00
|
|
|
Config->NoInhibitExec = Args.hasArg(OPT_noinhibit_exec);
|
2015-10-02 04:14:45 +08:00
|
|
|
Config->NoUndefined = Args.hasArg(OPT_no_undefined);
|
2015-12-10 17:12:18 +08:00
|
|
|
Config->PrintGcSections = Args.hasArg(OPT_print_gc_sections);
|
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);
|
2015-10-11 10:03:03 +08:00
|
|
|
Config->Verbose = Args.hasArg(OPT_verbose);
|
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");
|
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);
|
|
|
|
|
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
|
|
|
|
2015-10-24 03:02:19 +08:00
|
|
|
if (auto *Arg = Args.getLastArg(OPT_O)) {
|
|
|
|
StringRef Val = Arg->getValue();
|
|
|
|
if (Val.getAsInteger(10, Config->Optimize))
|
2016-02-03 05:13:09 +08:00
|
|
|
error("Invalid optimization level");
|
2015-10-24 03:02:19 +08:00
|
|
|
}
|
|
|
|
|
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-02-03 05:13:09 +08:00
|
|
|
error("Unknown hash style: " + S);
|
2015-10-22 16:21:35 +08:00
|
|
|
}
|
|
|
|
|
2015-10-20 01:35:12 +08:00
|
|
|
for (auto *Arg : Args.filtered(OPT_undefined))
|
|
|
|
Config->Undefined.push_back(Arg->getValue());
|
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;
|
|
|
|
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;
|
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)
|
|
|
|
error("no input files.");
|
2015-10-10 05:07:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT> void LinkerDriver::link(opt::InputArgList &Args) {
|
2016-02-13 04:54:57 +08:00
|
|
|
// For LTO
|
|
|
|
InitializeAllTargets();
|
|
|
|
InitializeAllTargetMCs();
|
|
|
|
InitializeAllAsmPrinters();
|
|
|
|
|
2015-10-10 05:07:25 +08:00
|
|
|
SymbolTable<ELFT> Symtab;
|
2016-02-12 05:18:01 +08:00
|
|
|
std::unique_ptr<TargetInfo> TI(createTarget());
|
|
|
|
Target = TI.get();
|
2015-10-10 05:12:40 +08:00
|
|
|
|
|
|
|
if (!Config->Shared) {
|
|
|
|
// Add entry symbol.
|
2016-01-07 11:59:08 +08:00
|
|
|
//
|
|
|
|
// There is no entry symbol for AMDGPU binaries, so skip adding one to avoid
|
|
|
|
// having and undefined symbol.
|
|
|
|
if (Config->Entry.empty() && Config->EMachine != EM_AMDGPU)
|
2015-10-12 23:23:54 +08:00
|
|
|
Config->Entry = (Config->EMachine == EM_MIPS) ? "__start" : "_start";
|
2015-10-15 06:20:57 +08:00
|
|
|
|
2015-10-10 05:12:40 +08:00
|
|
|
// In the assembly for 32 bit x86 the _GLOBAL_OFFSET_TABLE_ symbol
|
|
|
|
// is magical and is used to produce a R_386_GOTPC relocation.
|
|
|
|
// The R_386_GOTPC relocation value doesn't actually depend on the
|
|
|
|
// symbol value, so it could use an index of STN_UNDEF which, according
|
|
|
|
// to the spec, means the symbol value is 0.
|
|
|
|
// Unfortunately both gas and MC keep the _GLOBAL_OFFSET_TABLE_ symbol in
|
|
|
|
// the object file.
|
|
|
|
// The situation is even stranger on x86_64 where the assembly doesn't
|
|
|
|
// need the magical symbol, but gas still puts _GLOBAL_OFFSET_TABLE_ as
|
|
|
|
// an undefined symbol in the .o files.
|
|
|
|
// Given that the symbol is effectively unused, we just create a dummy
|
|
|
|
// hidden one to avoid the undefined symbol error.
|
2015-12-17 06:31:14 +08:00
|
|
|
Symtab.addIgnored("_GLOBAL_OFFSET_TABLE_");
|
2015-10-10 05:12:40 +08:00
|
|
|
}
|
2015-10-10 05:07:25 +08:00
|
|
|
|
2015-12-12 01:46:46 +08:00
|
|
|
if (!Config->Entry.empty()) {
|
|
|
|
// Set either EntryAddr (if S is a number) or EntrySym (otherwise).
|
|
|
|
StringRef S = Config->Entry;
|
|
|
|
if (S.getAsInteger(0, Config->EntryAddr))
|
|
|
|
Config->EntrySym = Symtab.addUndefined(S);
|
|
|
|
}
|
|
|
|
|
2015-12-17 05:35:39 +08:00
|
|
|
if (Config->EMachine == EM_MIPS) {
|
|
|
|
// On MIPS O32 ABI, _gp_disp is a magic symbol designates offset between
|
2016-02-07 20:09:40 +08:00
|
|
|
// start of function and 'gp' pointer into GOT.
|
2016-01-19 08:05:54 +08:00
|
|
|
Config->MipsGpDisp = Symtab.addIgnored("_gp_disp");
|
2016-02-07 20:09:40 +08:00
|
|
|
// The __gnu_local_gp is a magic symbol equal to the current value of 'gp'
|
|
|
|
// pointer. This symbol is used in the code generated by .cpload pseudo-op
|
|
|
|
// in case of using -mno-shared option.
|
|
|
|
// https://sourceware.org/ml/binutils/2004-12/msg00094.html
|
2016-02-04 20:09:49 +08:00
|
|
|
Config->MipsLocalGp = Symtab.addIgnored("__gnu_local_gp");
|
2015-12-17 05:35:39 +08:00
|
|
|
|
|
|
|
// Define _gp for MIPS. st_value of _gp symbol will be updated by Writer
|
|
|
|
// so that it points to an absolute address which is relative to GOT.
|
|
|
|
// See "Global Data Symbols" in Chapter 6 in the following document:
|
|
|
|
// ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
|
2015-12-25 14:12:18 +08:00
|
|
|
Symtab.addAbsolute("_gp", ElfSym<ELFT>::MipsGp);
|
2015-12-17 05:35:39 +08:00
|
|
|
}
|
2015-11-06 15:43:03 +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
|
|
|
|
2015-10-20 04:55:28 +08:00
|
|
|
for (StringRef S : Config->Undefined)
|
|
|
|
Symtab.addUndefinedOpt(S);
|
2015-10-05 17:43:57 +08:00
|
|
|
|
2016-02-13 04:54:57 +08:00
|
|
|
Symtab.addCombinedLtoObject();
|
|
|
|
|
2016-01-08 01:20:07 +08:00
|
|
|
for (auto *Arg : Args.filtered(OPT_wrap))
|
|
|
|
Symtab.wrap(Arg->getValue());
|
|
|
|
|
2015-10-07 08:25:09 +08:00
|
|
|
if (Config->OutputFile.empty())
|
|
|
|
Config->OutputFile = "a.out";
|
|
|
|
|
2015-10-08 02:29:51 +08:00
|
|
|
// Write the result to the file.
|
2015-10-14 02:10:33 +08:00
|
|
|
Symtab.scanShlibUndefined();
|
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)
|
|
|
|
markLive<ELFT>(&Symtab);
|
2015-10-10 05:07:25 +08:00
|
|
|
writeResult<ELFT>(&Symtab);
|
2015-07-25 05:03:07 +08:00
|
|
|
}
|