2015-07-25 05:03:07 +08:00
|
|
|
//===- Driver.cpp ---------------------------------------------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2015-07-25 05:03:07 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2017-01-26 09:52:05 +08:00
|
|
|
//
|
|
|
|
// The driver drives the entire linking process. It is responsible for
|
|
|
|
// parsing command line options and doing whatever it is instructed to do.
|
|
|
|
//
|
|
|
|
// One notable thing in the LLD's driver when compared to other linkers is
|
|
|
|
// that the LLD's driver is agnostic on the host operating system.
|
|
|
|
// Other linkers usually have implicit default values (such as a dynamic
|
|
|
|
// linker path or library paths) for each host OS.
|
|
|
|
//
|
|
|
|
// I don't think implicit default values are useful because they are
|
|
|
|
// usually explicitly specified by the compiler driver. They can even
|
|
|
|
// be harmful when you are doing cross-linking. Therefore, in LLD, we
|
2017-03-24 08:15:57 +08:00
|
|
|
// simply trust the compiler driver to pass all required options and
|
|
|
|
// don't try to make effort on our side.
|
2017-01-26 09:52:05 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2015-07-25 05:03:07 +08:00
|
|
|
|
2015-08-06 07:24:46 +08:00
|
|
|
#include "Driver.h"
|
2015-08-06 23:08:23 +08:00
|
|
|
#include "Config.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"
|
2018-02-21 06:09:59 +08:00
|
|
|
#include "MarkLive.h"
|
2017-03-20 18:09:58 +08:00
|
|
|
#include "OutputSections.h"
|
2017-04-05 13:07:39 +08:00
|
|
|
#include "ScriptParser.h"
|
2015-08-06 07:24:46 +08:00
|
|
|
#include "SymbolTable.h"
|
2017-12-10 00:56:18 +08:00
|
|
|
#include "Symbols.h"
|
2017-06-12 08:00:51 +08:00
|
|
|
#include "SyntheticSections.h"
|
2015-10-10 05:12:40 +08:00
|
|
|
#include "Target.h"
|
2015-07-25 05:03:07 +08:00
|
|
|
#include "Writer.h"
|
2017-11-29 03:58:45 +08:00
|
|
|
#include "lld/Common/Args.h"
|
2017-10-03 05:00:41 +08:00
|
|
|
#include "lld/Common/Driver.h"
|
[lld] unified COFF and ELF error handling on new Common/ErrorHandler
Summary:
The COFF linker and the ELF linker have long had similar but separate
Error.h and Error.cpp files to implement error handling. This change
introduces new error handling code in Common/ErrorHandler.h, changes the
COFF and ELF linkers to use it, and removes the old, separate
implementations.
Reviewers: ruiu
Reviewed By: ruiu
Subscribers: smeenai, jyknight, emaste, sdardis, nemanjai, nhaehnle, mgorny, javed.absar, kbarton, fedor.sergeev, llvm-commits
Differential Revision: https://reviews.llvm.org/D39259
llvm-svn: 316624
2017-10-26 06:28:38 +08:00
|
|
|
#include "lld/Common/ErrorHandler.h"
|
2019-03-12 00:30:55 +08:00
|
|
|
#include "lld/Common/Filesystem.h"
|
2017-11-29 04:39:17 +08:00
|
|
|
#include "lld/Common/Memory.h"
|
2018-03-01 01:38:19 +08:00
|
|
|
#include "lld/Common/Strings.h"
|
2018-02-06 20:20:05 +08:00
|
|
|
#include "lld/Common/TargetOptionsCommandFlags.h"
|
2017-10-14 02:22:55 +08:00
|
|
|
#include "lld/Common/Threads.h"
|
2017-10-03 05:00:41 +08:00
|
|
|
#include "lld/Common/Version.h"
|
2018-02-14 21:36:22 +08:00
|
|
|
#include "llvm/ADT/SetVector.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"
|
2017-04-17 16:58:12 +08:00
|
|
|
#include "llvm/Support/Compression.h"
|
2018-07-19 06:49:31 +08:00
|
|
|
#include "llvm/Support/LEB128.h"
|
2017-01-06 10:33:53 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
2017-01-09 09:42:02 +08:00
|
|
|
#include "llvm/Support/TarWriter.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;
|
2018-09-21 05:29:14 +08:00
|
|
|
using namespace llvm::support;
|
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
|
|
|
|
2018-02-06 04:55:46 +08:00
|
|
|
static void setConfigs(opt::InputArgList &Args);
|
2019-05-09 00:20:05 +08:00
|
|
|
static void readConfigs(opt::InputArgList &Args);
|
2017-03-18 07:29:01 +08:00
|
|
|
|
2016-10-27 02:59:00 +08:00
|
|
|
bool elf::link(ArrayRef<const char *> Args, bool CanExitEarly,
|
|
|
|
raw_ostream &Error) {
|
2018-08-27 14:18:10 +08:00
|
|
|
errorHandler().LogName = args::getFilenameWithoutExe(Args[0]);
|
[lld] unified COFF and ELF error handling on new Common/ErrorHandler
Summary:
The COFF linker and the ELF linker have long had similar but separate
Error.h and Error.cpp files to implement error handling. This change
introduces new error handling code in Common/ErrorHandler.h, changes the
COFF and ELF linkers to use it, and removes the old, separate
implementations.
Reviewers: ruiu
Reviewed By: ruiu
Subscribers: smeenai, jyknight, emaste, sdardis, nemanjai, nhaehnle, mgorny, javed.absar, kbarton, fedor.sergeev, llvm-commits
Differential Revision: https://reviews.llvm.org/D39259
llvm-svn: 316624
2017-10-26 06:28:38 +08:00
|
|
|
errorHandler().ErrorLimitExceededMsg =
|
|
|
|
"too many errors emitted, stopping now (use "
|
|
|
|
"-error-limit=0 to see all errors)";
|
|
|
|
errorHandler().ErrorOS = &Error;
|
2018-02-17 07:41:48 +08:00
|
|
|
errorHandler().ExitEarly = CanExitEarly;
|
[lld] unified COFF and ELF error handling on new Common/ErrorHandler
Summary:
The COFF linker and the ELF linker have long had similar but separate
Error.h and Error.cpp files to implement error handling. This change
introduces new error handling code in Common/ErrorHandler.h, changes the
COFF and ELF linkers to use it, and removes the old, separate
implementations.
Reviewers: ruiu
Reviewed By: ruiu
Subscribers: smeenai, jyknight, emaste, sdardis, nemanjai, nhaehnle, mgorny, javed.absar, kbarton, fedor.sergeev, llvm-commits
Differential Revision: https://reviews.llvm.org/D39259
llvm-svn: 316624
2017-10-26 06:28:38 +08:00
|
|
|
errorHandler().ColorDiagnostics = Error.has_colors();
|
2018-02-17 07:41:48 +08:00
|
|
|
|
2017-02-27 10:32:08 +08:00
|
|
|
InputSections.clear();
|
2017-09-25 22:42:15 +08:00
|
|
|
OutputSections.clear();
|
|
|
|
BinaryFiles.clear();
|
|
|
|
BitcodeFiles.clear();
|
|
|
|
ObjectFiles.clear();
|
|
|
|
SharedFiles.clear();
|
2016-04-21 04:13:41 +08:00
|
|
|
|
2016-12-09 01:48:52 +08:00
|
|
|
Config = make<Configuration>();
|
|
|
|
Driver = make<LinkerDriver>();
|
2017-03-22 07:03:09 +08:00
|
|
|
Script = make<LinkerScript>();
|
2017-07-27 02:42:48 +08:00
|
|
|
Symtab = make<SymbolTable>();
|
2018-09-26 03:26:58 +08:00
|
|
|
|
|
|
|
Tar = nullptr;
|
|
|
|
memset(&In, 0, sizeof(In));
|
|
|
|
|
2019-04-09 01:48:05 +08:00
|
|
|
SharedFile::VernauxNum = 0;
|
|
|
|
|
2018-02-07 06:37:05 +08:00
|
|
|
Config->ProgName = Args[0];
|
2016-04-21 04:13:41 +08:00
|
|
|
|
2018-02-17 07:41:48 +08:00
|
|
|
Driver->main(Args);
|
2017-10-04 08:50:11 +08:00
|
|
|
|
|
|
|
// Exit immediately if we don't need to return to the caller.
|
|
|
|
// This saves time because the overhead of calling destructors
|
|
|
|
// for all globally-allocated objects is not negligible.
|
2018-02-17 07:41:48 +08:00
|
|
|
if (CanExitEarly)
|
[lld] unified COFF and ELF error handling on new Common/ErrorHandler
Summary:
The COFF linker and the ELF linker have long had similar but separate
Error.h and Error.cpp files to implement error handling. This change
introduces new error handling code in Common/ErrorHandler.h, changes the
COFF and ELF linkers to use it, and removes the old, separate
implementations.
Reviewers: ruiu
Reviewed By: ruiu
Subscribers: smeenai, jyknight, emaste, sdardis, nemanjai, nhaehnle, mgorny, javed.absar, kbarton, fedor.sergeev, llvm-commits
Differential Revision: https://reviews.llvm.org/D39259
llvm-svn: 316624
2017-10-26 06:28:38 +08:00
|
|
|
exitLld(errorCount() ? 1 : 0);
|
2017-10-04 08:50:11 +08:00
|
|
|
|
2016-10-29 04:57:25 +08:00
|
|
|
freeArena();
|
[lld] unified COFF and ELF error handling on new Common/ErrorHandler
Summary:
The COFF linker and the ELF linker have long had similar but separate
Error.h and Error.cpp files to implement error handling. This change
introduces new error handling code in Common/ErrorHandler.h, changes the
COFF and ELF linkers to use it, and removes the old, separate
implementations.
Reviewers: ruiu
Reviewed By: ruiu
Subscribers: smeenai, jyknight, emaste, sdardis, nemanjai, nhaehnle, mgorny, javed.absar, kbarton, fedor.sergeev, llvm-commits
Differential Revision: https://reviews.llvm.org/D39259
llvm-svn: 316624
2017-10-26 06:28:38 +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)
|
2018-05-04 22:28:29 +08:00
|
|
|
.Cases("aarch64elf", "aarch64linux", "aarch64_elf64_le_vec",
|
|
|
|
{ELF64LEKind, EM_AARCH64})
|
2017-06-14 16:25:38 +08:00
|
|
|
.Cases("armelf", "armelf_linux_eabi", {ELF32LEKind, EM_ARM})
|
2016-07-13 07:28:33 +08:00
|
|
|
.Case("elf32_x86_64", {ELF32LEKind, EM_X86_64})
|
2017-01-30 09:50:16 +08:00
|
|
|
.Cases("elf32btsmip", "elf32btsmipn32", {ELF32BEKind, EM_MIPS})
|
|
|
|
.Cases("elf32ltsmip", "elf32ltsmipn32", {ELF32LEKind, EM_MIPS})
|
2018-08-10 01:59:56 +08:00
|
|
|
.Case("elf32lriscv", {ELF32LEKind, EM_RISCV})
|
2019-02-14 02:51:15 +08:00
|
|
|
.Cases("elf32ppc", "elf32ppclinux", {ELF32BEKind, EM_PPC})
|
2016-06-08 01:55:05 +08:00
|
|
|
.Case("elf64btsmip", {ELF64BEKind, EM_MIPS})
|
|
|
|
.Case("elf64ltsmip", {ELF64LEKind, EM_MIPS})
|
2018-08-10 01:59:56 +08:00
|
|
|
.Case("elf64lriscv", {ELF64LEKind, EM_RISCV})
|
2016-06-08 01:55:05 +08:00
|
|
|
.Case("elf64ppc", {ELF64BEKind, EM_PPC64})
|
2018-03-10 06:11:46 +08:00
|
|
|
.Case("elf64lppc", {ELF64LEKind, 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});
|
|
|
|
|
2017-09-12 01:02:59 +08:00
|
|
|
if (Ret.first == ELFNoneKind)
|
|
|
|
error("unknown emulation: " + Emul);
|
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.
|
2017-05-05 23:08:06 +08:00
|
|
|
std::vector<std::pair<MemoryBufferRef, uint64_t>> static getArchiveMembers(
|
|
|
|
MemoryBufferRef MB) {
|
2016-03-04 00:21:44 +08:00
|
|
|
std::unique_ptr<Archive> File =
|
2017-12-07 06:08:17 +08:00
|
|
|
CHECK(Archive::create(MB),
|
2016-11-21 17:28:07 +08:00
|
|
|
MB.getBufferIdentifier() + ": failed to parse archive");
|
2016-01-06 08:51:35 +08:00
|
|
|
|
2017-05-05 23:08:06 +08:00
|
|
|
std::vector<std::pair<MemoryBufferRef, uint64_t>> V;
|
2016-11-11 12:28:40 +08:00
|
|
|
Error Err = Error::success();
|
2017-09-21 06:59:50 +08:00
|
|
|
bool AddToTar = File->isThin() && Tar;
|
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 =
|
2017-12-07 06:08:17 +08:00
|
|
|
CHECK(COrErr, MB.getBufferIdentifier() +
|
2016-11-21 17:28:07 +08:00
|
|
|
": could not get the child of the archive");
|
2016-04-03 03:09:07 +08:00
|
|
|
MemoryBufferRef MBRef =
|
2017-12-07 06:08:17 +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");
|
2017-09-21 06:59:50 +08:00
|
|
|
if (AddToTar)
|
|
|
|
Tar->append(relativeToRoot(check(C.getFullName())), MBRef.getBuffer());
|
2017-05-05 23:08:06 +08:00
|
|
|
V.push_back(std::make_pair(MBRef, C.getChildOffset()));
|
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())
|
2016-12-23 11:19:09 +08:00
|
|
|
make<std::unique_ptr<MemoryBuffer>>(std::move(MB));
|
2016-04-01 07:12:18 +08:00
|
|
|
|
2016-01-06 08:51:35 +08:00
|
|
|
return V;
|
|
|
|
}
|
|
|
|
|
2017-05-04 07:10:33 +08:00
|
|
|
// Opens a file and create a file object. Path has to be resolved already.
|
2017-04-12 08:13:48 +08:00
|
|
|
void LinkerDriver::addFile(StringRef Path, bool WithLOption) {
|
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
|
|
|
|
2018-08-07 05:29:41 +08:00
|
|
|
if (Config->FormatBinary) {
|
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;
|
2017-05-04 05:03:08 +08:00
|
|
|
case file_magic::archive: {
|
|
|
|
// Handle -whole-archive.
|
2016-10-26 12:01:07 +08:00
|
|
|
if (InWholeArchive) {
|
2017-05-05 23:08:06 +08:00
|
|
|
for (const auto &P : getArchiveMembers(MBRef))
|
|
|
|
Files.push_back(createObjectFile(P.first, Path, P.second));
|
2015-10-10 05:07:25 +08:00
|
|
|
return;
|
|
|
|
}
|
2017-05-04 05:03:08 +08:00
|
|
|
|
|
|
|
std::unique_ptr<Archive> File =
|
2017-12-07 06:08:17 +08:00
|
|
|
CHECK(Archive::create(MBRef), Path + ": failed to parse archive");
|
2017-05-04 05:03:08 +08:00
|
|
|
|
|
|
|
// If an archive file has no symbol table, it is likely that a user
|
|
|
|
// is attempting LTO and using a default ar command that doesn't
|
|
|
|
// understand the LLVM bitcode file. It is a pretty common error, so
|
|
|
|
// we'll handle it as if it had a symbol table.
|
2017-06-09 20:26:57 +08:00
|
|
|
if (!File->isEmpty() && !File->hasSymbolTable()) {
|
2019-03-15 02:21:32 +08:00
|
|
|
// Check if all members are bitcode files. If not, ignore, which is the
|
|
|
|
// default action without the LTO hack described above.
|
|
|
|
for (const std::pair<MemoryBufferRef, uint64_t> &P :
|
|
|
|
getArchiveMembers(MBRef))
|
|
|
|
if (identify_magic(P.first.getBuffer()) != file_magic::bitcode)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (const std::pair<MemoryBufferRef, uint64_t> &P :
|
|
|
|
getArchiveMembers(MBRef))
|
2017-07-27 06:13:32 +08:00
|
|
|
Files.push_back(make<LazyObjFile>(P.first, Path, P.second));
|
2017-05-04 05:03:08 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle the regular case.
|
|
|
|
Files.push_back(make<ArchiveFile>(std::move(File)));
|
2015-10-01 23:23:09 +08:00
|
|
|
return;
|
2017-05-04 05:03:08 +08:00
|
|
|
}
|
2015-10-12 09:55:32 +08:00
|
|
|
case file_magic::elf_shared_object:
|
2018-12-19 06:30:23 +08:00
|
|
|
if (Config->Static || 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;
|
|
|
|
}
|
2017-06-21 11:05:08 +08:00
|
|
|
|
2017-04-13 08:23:32 +08:00
|
|
|
// DSOs usually have DT_SONAME tags in their ELF headers, and the
|
|
|
|
// sonames are used to identify DSOs. But if they are missing,
|
|
|
|
// they are identified by filenames. We don't know whether the new
|
|
|
|
// file has a DT_SONAME or not because we haven't parsed it yet.
|
|
|
|
// Here, we set the default soname for the file because we might
|
|
|
|
// need it later.
|
|
|
|
//
|
|
|
|
// If a file was specified by -lfoo, the directory part is not
|
|
|
|
// significant, as a user did not specify it. This behavior is
|
|
|
|
// compatible with GNU.
|
2017-06-21 11:05:08 +08:00
|
|
|
Files.push_back(
|
|
|
|
createSharedFile(MBRef, WithLOption ? path::filename(Path) : Path));
|
2015-10-01 23:23:09 +08:00
|
|
|
return;
|
2018-02-02 08:27:49 +08:00
|
|
|
case file_magic::bitcode:
|
|
|
|
case file_magic::elf_relocatable:
|
2016-04-08 03:24:51 +08:00
|
|
|
if (InLib)
|
2017-07-27 06:13:32 +08:00
|
|
|
Files.push_back(make<LazyObjFile>(MBRef, "", 0));
|
2016-04-08 03:24:51 +08:00
|
|
|
else
|
2016-10-29 04:57:25 +08:00
|
|
|
Files.push_back(createObjectFile(MBRef));
|
2018-02-02 08:27:49 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
error(Path + ": unknown file type");
|
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) {
|
2016-11-20 03:23:58 +08:00
|
|
|
if (Optional<std::string> Path = searchLibrary(Name))
|
2017-04-12 08:13:48 +08:00
|
|
|
addFile(*Path, /*WithLOption=*/true);
|
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.
|
2018-02-06 20:20:05 +08:00
|
|
|
static void initLLVM() {
|
2016-04-03 02:18:44 +08:00
|
|
|
InitializeAllTargets();
|
|
|
|
InitializeAllTargetMCs();
|
|
|
|
InitializeAllAsmPrinters();
|
|
|
|
InitializeAllAsmParsers();
|
|
|
|
}
|
|
|
|
|
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.
|
2018-11-16 02:09:41 +08:00
|
|
|
static void checkOptions() {
|
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)
|
2018-10-26 02:07:55 +08:00
|
|
|
error("the .gnu.hash section is not compatible with the MIPS target");
|
2016-01-08 01:33:25 +08:00
|
|
|
|
2017-12-05 23:59:05 +08:00
|
|
|
if (Config->FixCortexA53Errata843419 && Config->EMachine != EM_AARCH64)
|
2018-10-26 02:07:55 +08:00
|
|
|
error("--fix-cortex-a53-843419 is only supported on AArch64 targets");
|
2017-12-05 23:59:05 +08:00
|
|
|
|
2018-09-20 08:26:44 +08:00
|
|
|
if (Config->TocOptimize && Config->EMachine != EM_PPC64)
|
2018-10-26 02:07:55 +08:00
|
|
|
error("--toc-optimize is only supported on the PowerPC64 target");
|
2018-09-20 08:26:44 +08:00
|
|
|
|
2016-03-17 13:57:33 +08:00
|
|
|
if (Config->Pie && Config->Shared)
|
|
|
|
error("-shared and -pie may not be used together");
|
|
|
|
|
2017-07-17 17:43:18 +08:00
|
|
|
if (!Config->Shared && !Config->FilterList.empty())
|
|
|
|
error("-F may not be used without -shared");
|
|
|
|
|
2017-04-27 05:27:33 +08:00
|
|
|
if (!Config->Shared && !Config->AuxiliaryList.empty())
|
|
|
|
error("-f may not be used without -shared");
|
|
|
|
|
2017-12-01 04:46:33 +08:00
|
|
|
if (!Config->Relocatable && !Config->DefineCommon)
|
|
|
|
error("-no-define-common not supported in non relocatable output");
|
|
|
|
|
2019-05-14 23:25:21 +08:00
|
|
|
if (Config->ZText && Config->ZIfuncNoplt)
|
|
|
|
error("-z text and -z ifunc-noplt 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");
|
2018-07-19 06:02:48 +08:00
|
|
|
if (Config->GdbIndex)
|
|
|
|
error("-r and --gdb-index may not be used together");
|
2018-07-19 06:49:31 +08:00
|
|
|
if (Config->ICF != ICFLevel::None)
|
2016-04-03 02:52:23 +08:00
|
|
|
error("-r and --icf may not be used together");
|
|
|
|
if (Config->Pie)
|
|
|
|
error("-r and -pie may not be used together");
|
|
|
|
}
|
[AArch64] Support execute-only LOAD segments.
Summary:
This adds an LLD flag to mark executable LOAD segments execute-only for AArch64 targets.
In AArch64 the expectation is that code is execute-only compatible, so this just adds a linker option to enforce this.
Patch by: ivanlozano (Ivan Lozano)
Reviewers: srhines, echristo, peter.smith, eugenis, javed.absar, espindola, ruiu
Reviewed By: ruiu
Subscribers: dokyungs, emaste, arichardson, kristof.beyls, llvm-commits
Differential Revision: https://reviews.llvm.org/D49456
llvm-svn: 338271
2018-07-31 01:02:46 +08:00
|
|
|
|
|
|
|
if (Config->ExecuteOnly) {
|
|
|
|
if (Config->EMachine != EM_AARCH64)
|
|
|
|
error("-execute-only is only supported on AArch64 targets");
|
|
|
|
|
|
|
|
if (Config->SingleRoRx && !Script->HasSectionsCommand)
|
|
|
|
error("-execute-only and -no-rosegment cannot be used together");
|
|
|
|
}
|
2016-01-08 01:33:25 +08:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-04-21 05:24:08 +08:00
|
|
|
static bool getZFlag(opt::InputArgList &Args, StringRef K1, StringRef K2,
|
|
|
|
bool Default) {
|
|
|
|
for (auto *Arg : Args.filtered_reverse(OPT_z)) {
|
|
|
|
if (K1 == Arg->getValue())
|
|
|
|
return true;
|
|
|
|
if (K2 == Arg->getValue())
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return Default;
|
|
|
|
}
|
|
|
|
|
2018-09-21 05:40:38 +08:00
|
|
|
static bool isKnownZFlag(StringRef S) {
|
2018-06-27 15:22:27 +08:00
|
|
|
return S == "combreloc" || S == "copyreloc" || S == "defs" ||
|
2018-08-28 16:24:34 +08:00
|
|
|
S == "execstack" || S == "global" || S == "hazardplt" ||
|
2019-05-14 23:25:21 +08:00
|
|
|
S == "ifunc-noplt" || S == "initfirst" || S == "interpose" ||
|
2018-09-14 22:25:37 +08:00
|
|
|
S == "keep-text-section-prefix" || S == "lazy" || S == "muldefs" ||
|
2018-11-27 17:48:17 +08:00
|
|
|
S == "nocombreloc" || S == "nocopyreloc" || S == "nodefaultlib" ||
|
|
|
|
S == "nodelete" || S == "nodlopen" || S == "noexecstack" ||
|
2018-06-27 15:56:23 +08:00
|
|
|
S == "nokeep-text-section-prefix" || S == "norelro" || S == "notext" ||
|
|
|
|
S == "now" || S == "origin" || S == "relro" || S == "retpolineplt" ||
|
|
|
|
S == "rodynamic" || S == "text" || S == "wxneeded" ||
|
2019-05-14 00:01:26 +08:00
|
|
|
S.startswith("common-page-size") || S.startswith("max-page-size=") ||
|
|
|
|
S.startswith("stack-size=");
|
2018-06-27 15:22:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Report an error for an unknown -z option.
|
|
|
|
static void checkZOptions(opt::InputArgList &Args) {
|
|
|
|
for (auto *Arg : Args.filtered(OPT_z))
|
2018-09-21 05:40:38 +08:00
|
|
|
if (!isKnownZFlag(Arg->getValue()))
|
2018-06-27 15:22:27 +08:00
|
|
|
error("unknown -z value: " + StringRef(Arg->getValue()));
|
|
|
|
}
|
|
|
|
|
2018-02-17 07:41:48 +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-11-26 23:10:01 +08:00
|
|
|
|
2016-12-20 10:17:24 +08:00
|
|
|
// Interpret this flag early because error() depends on them.
|
2017-11-29 03:58:45 +08:00
|
|
|
errorHandler().ErrorLimit = args::getInteger(Args, OPT_error_limit, 20);
|
2019-02-14 02:48:39 +08:00
|
|
|
checkZOptions(Args);
|
2016-11-26 23:10:01 +08:00
|
|
|
|
|
|
|
// Handle -help
|
2016-02-28 11:18:09 +08:00
|
|
|
if (Args.hasArg(OPT_help)) {
|
2018-02-07 06:37:05 +08:00
|
|
|
printHelp();
|
2016-02-28 11:18:09 +08:00
|
|
|
return;
|
|
|
|
}
|
2016-11-20 02:14:24 +08:00
|
|
|
|
2017-03-23 02:04:57 +08:00
|
|
|
// Handle -v or -version.
|
|
|
|
//
|
|
|
|
// A note about "compatible with GNU linkers" message: this is a hack for
|
|
|
|
// scripts generated by GNU Libtool 2.4.6 (released in February 2014 and
|
|
|
|
// still the newest version in March 2017) or earlier to recognize LLD as
|
|
|
|
// a GNU compatible linker. As long as an output for the -v option
|
|
|
|
// contains "GNU" or "with BFD", they recognize us as GNU-compatible.
|
|
|
|
//
|
|
|
|
// This is somewhat ugly hack, but in reality, we had no choice other
|
|
|
|
// than doing this. Considering the very long release cycle of Libtool,
|
|
|
|
// it is not easy to improve it to recognize LLD as a GNU compatible
|
|
|
|
// linker in a timely manner. Even if we can make it, there are still a
|
|
|
|
// lot of "configure" scripts out there that are generated by old version
|
|
|
|
// of Libtool. We cannot convince every software developer to migrate to
|
|
|
|
// the latest version and re-generate scripts. So we have this hack.
|
|
|
|
if (Args.hasArg(OPT_v) || Args.hasArg(OPT_version))
|
|
|
|
message(getLLDVersion() + " (compatible with GNU linkers)");
|
|
|
|
|
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.
|
2017-01-06 10:33:53 +08:00
|
|
|
Expected<std::unique_ptr<TarWriter>> ErrOrWriter =
|
|
|
|
TarWriter::create(Path, path::stem(Path));
|
|
|
|
if (ErrOrWriter) {
|
2018-12-19 07:50:37 +08:00
|
|
|
Tar = std::move(*ErrOrWriter);
|
2017-01-06 10:33:53 +08:00
|
|
|
Tar->append("response.txt", createResponseFile(Args));
|
|
|
|
Tar->append("version.txt", getLLDVersion() + "\n");
|
|
|
|
} else {
|
2018-12-19 07:33:10 +08:00
|
|
|
error("--reproduce: " + toString(ErrOrWriter.takeError()));
|
2017-01-06 10:33:53 +08:00
|
|
|
}
|
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);
|
2018-10-15 22:21:43 +08:00
|
|
|
|
|
|
|
// The behavior of -v or --version is a bit strange, but this is
|
|
|
|
// needed for compatibility with GNU linkers.
|
|
|
|
if (Args.hasArg(OPT_v) && !Args.hasArg(OPT_INPUT))
|
|
|
|
return;
|
|
|
|
if (Args.hasArg(OPT_version))
|
|
|
|
return;
|
|
|
|
|
2018-02-06 20:20:05 +08:00
|
|
|
initLLVM();
|
2015-10-10 05:07:25 +08:00
|
|
|
createFiles(Args);
|
2018-06-06 00:13:40 +08:00
|
|
|
if (errorCount())
|
|
|
|
return;
|
|
|
|
|
2016-10-20 12:47:47 +08:00
|
|
|
inferMachineType();
|
2018-02-06 04:55:46 +08:00
|
|
|
setConfigs(Args);
|
2018-11-16 02:09:41 +08:00
|
|
|
checkOptions();
|
[lld] unified COFF and ELF error handling on new Common/ErrorHandler
Summary:
The COFF linker and the ELF linker have long had similar but separate
Error.h and Error.cpp files to implement error handling. This change
introduces new error handling code in Common/ErrorHandler.h, changes the
COFF and ELF linkers to use it, and removes the old, separate
implementations.
Reviewers: ruiu
Reviewed By: ruiu
Subscribers: smeenai, jyknight, emaste, sdardis, nemanjai, nhaehnle, mgorny, javed.absar, kbarton, fedor.sergeev, llvm-commits
Differential Revision: https://reviews.llvm.org/D39259
llvm-svn: 316624
2017-10-26 06:28:38 +08:00
|
|
|
if (errorCount())
|
2016-02-03 05:13:09 +08:00
|
|
|
return;
|
2015-11-13 02:54:15 +08:00
|
|
|
|
2019-05-09 09:45:53 +08:00
|
|
|
// The Target instance handles target-specific stuff, such as applying
|
|
|
|
// relocations or writing a PLT section. It also contains target-dependent
|
|
|
|
// values such as a default image base address.
|
|
|
|
Target = getTarget();
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-30 07:06:43 +08:00
|
|
|
static std::string getRpath(opt::InputArgList &Args) {
|
2017-11-29 03:58:45 +08:00
|
|
|
std::vector<StringRef> V = args::getStrings(Args, OPT_rpath);
|
2017-02-25 09:51:44 +08:00
|
|
|
return llvm::join(V.begin(), V.end(), ":");
|
|
|
|
}
|
|
|
|
|
2017-01-29 09:59:11 +08:00
|
|
|
// Determines what we should do if there are remaining unresolved
|
|
|
|
// symbols after the name resolution.
|
|
|
|
static UnresolvedPolicy getUnresolvedSymbolPolicy(opt::InputArgList &Args) {
|
2017-10-25 04:59:55 +08:00
|
|
|
UnresolvedPolicy ErrorOrWarn = Args.hasFlag(OPT_error_unresolved_symbols,
|
|
|
|
OPT_warn_unresolved_symbols, true)
|
2017-03-24 02:16:42 +08:00
|
|
|
? UnresolvedPolicy::ReportError
|
|
|
|
: UnresolvedPolicy::Warn;
|
2017-01-29 09:59:11 +08:00
|
|
|
|
|
|
|
// Process the last of -unresolved-symbols, -no-undefined or -z defs.
|
|
|
|
for (auto *Arg : llvm::reverse(Args)) {
|
|
|
|
switch (Arg->getOption().getID()) {
|
|
|
|
case 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 ErrorOrWarn;
|
|
|
|
error("unknown --unresolved-symbols value: " + S);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
case OPT_no_undefined:
|
|
|
|
return ErrorOrWarn;
|
|
|
|
case OPT_z:
|
|
|
|
if (StringRef(Arg->getValue()) == "defs")
|
|
|
|
return ErrorOrWarn;
|
|
|
|
continue;
|
|
|
|
}
|
2017-01-26 10:19:20 +08:00
|
|
|
}
|
|
|
|
|
2017-01-29 09:59:11 +08:00
|
|
|
// -shared implies -unresolved-symbols=ignore-all because missing
|
|
|
|
// symbols are likely to be resolved at runtime using other DSOs.
|
|
|
|
if (Config->Shared)
|
|
|
|
return UnresolvedPolicy::Ignore;
|
|
|
|
return ErrorOrWarn;
|
2016-06-29 20:35:04 +08:00
|
|
|
}
|
|
|
|
|
2017-02-25 10:23:28 +08:00
|
|
|
static Target2Policy getTarget2(opt::InputArgList &Args) {
|
2017-06-14 16:01:26 +08:00
|
|
|
StringRef S = Args.getLastArgValue(OPT_target2, "got-rel");
|
2017-04-30 06:56:38 +08:00
|
|
|
if (S == "rel")
|
|
|
|
return Target2Policy::Rel;
|
|
|
|
if (S == "abs")
|
|
|
|
return Target2Policy::Abs;
|
|
|
|
if (S == "got-rel")
|
|
|
|
return Target2Policy::GotRel;
|
|
|
|
error("unknown --target2 option: " + S);
|
2016-10-18 02:12:24 +08:00
|
|
|
return Target2Policy::GotRel;
|
|
|
|
}
|
|
|
|
|
2016-08-25 17:05:47 +08:00
|
|
|
static bool isOutputFormatBinary(opt::InputArgList &Args) {
|
2018-08-02 06:31:31 +08:00
|
|
|
StringRef S = Args.getLastArgValue(OPT_oformat, "elf");
|
|
|
|
if (S == "binary")
|
|
|
|
return true;
|
|
|
|
if (!S.startswith("elf"))
|
2016-08-25 17:05:47 +08:00
|
|
|
error("unknown --oformat value: " + S);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-02-25 10:23:28 +08:00
|
|
|
static DiscardPolicy getDiscard(opt::InputArgList &Args) {
|
2017-02-25 09:51:25 +08:00
|
|
|
if (Args.hasArg(OPT_relocatable))
|
2016-12-04 16:34:17 +08:00
|
|
|
return DiscardPolicy::None;
|
2017-02-25 10:12:37 +08:00
|
|
|
|
2016-08-31 16:46:30 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-02-25 10:23:28 +08:00
|
|
|
static StringRef getDynamicLinker(opt::InputArgList &Args) {
|
2017-02-24 16:26:18 +08:00
|
|
|
auto *Arg = Args.getLastArg(OPT_dynamic_linker, OPT_no_dynamic_linker);
|
|
|
|
if (!Arg || Arg->getOption().getID() == OPT_no_dynamic_linker)
|
|
|
|
return "";
|
|
|
|
return Arg->getValue();
|
|
|
|
}
|
|
|
|
|
2018-07-19 06:49:31 +08:00
|
|
|
static ICFLevel getICF(opt::InputArgList &Args) {
|
|
|
|
auto *Arg = Args.getLastArg(OPT_icf_none, OPT_icf_safe, OPT_icf_all);
|
|
|
|
if (!Arg || Arg->getOption().getID() == OPT_icf_none)
|
|
|
|
return ICFLevel::None;
|
|
|
|
if (Arg->getOption().getID() == OPT_icf_safe)
|
|
|
|
return ICFLevel::Safe;
|
|
|
|
return ICFLevel::All;
|
|
|
|
}
|
|
|
|
|
2017-02-25 10:23:28 +08:00
|
|
|
static StripPolicy getStrip(opt::InputArgList &Args) {
|
2017-02-25 10:12:37 +08:00
|
|
|
if (Args.hasArg(OPT_relocatable))
|
|
|
|
return StripPolicy::None;
|
|
|
|
|
|
|
|
auto *Arg = Args.getLastArg(OPT_strip_all, OPT_strip_debug);
|
|
|
|
if (!Arg)
|
|
|
|
return StripPolicy::None;
|
|
|
|
if (Arg->getOption().getID() == OPT_strip_all)
|
|
|
|
return StripPolicy::All;
|
|
|
|
return StripPolicy::Debug;
|
2016-08-31 16:38:11 +08:00
|
|
|
}
|
|
|
|
|
2017-12-06 00:50:46 +08:00
|
|
|
static uint64_t parseSectionAddress(StringRef S, const opt::Arg &Arg) {
|
2016-09-14 21:07:13 +08:00
|
|
|
uint64_t VA = 0;
|
|
|
|
if (S.startswith("0x"))
|
|
|
|
S = S.drop_front(2);
|
2017-05-16 16:19:25 +08:00
|
|
|
if (!to_integer(S, VA, 16))
|
2017-01-06 18:04:35 +08:00
|
|
|
error("invalid argument: " + toString(Arg));
|
2016-09-14 21:07:13 +08:00
|
|
|
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('=');
|
2017-12-06 00:50:46 +08:00
|
|
|
Ret[Name] = parseSectionAddress(Addr, *Arg);
|
2016-09-14 21:07:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (auto *Arg = Args.getLastArg(OPT_Ttext))
|
2017-12-06 00:50:46 +08:00
|
|
|
Ret[".text"] = parseSectionAddress(Arg->getValue(), *Arg);
|
2016-09-14 21:07:13 +08:00
|
|
|
if (auto *Arg = Args.getLastArg(OPT_Tdata))
|
2017-12-06 00:50:46 +08:00
|
|
|
Ret[".data"] = parseSectionAddress(Arg->getValue(), *Arg);
|
2016-09-14 21:07:13 +08:00
|
|
|
if (auto *Arg = Args.getLastArg(OPT_Tbss))
|
2017-12-06 00:50:46 +08:00
|
|
|
Ret[".bss"] = parseSectionAddress(Arg->getValue(), *Arg);
|
2016-09-14 21:07:13 +08:00
|
|
|
return Ret;
|
|
|
|
}
|
|
|
|
|
2017-02-25 10:23:28 +08:00
|
|
|
static SortSectionPolicy getSortSection(opt::InputArgList &Args) {
|
2017-06-14 16:01:26 +08:00
|
|
|
StringRef S = Args.getLastArgValue(OPT_sort_section);
|
2016-09-17 04:21:55 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-10-25 23:20:30 +08:00
|
|
|
static OrphanHandlingPolicy getOrphanHandling(opt::InputArgList &Args) {
|
|
|
|
StringRef S = Args.getLastArgValue(OPT_orphan_handling, "place");
|
|
|
|
if (S == "warn")
|
|
|
|
return OrphanHandlingPolicy::Warn;
|
|
|
|
if (S == "error")
|
|
|
|
return OrphanHandlingPolicy::Error;
|
|
|
|
if (S != "place")
|
|
|
|
error("unknown --orphan-handling mode: " + S);
|
|
|
|
return OrphanHandlingPolicy::Place;
|
|
|
|
}
|
|
|
|
|
2017-04-27 05:23:11 +08:00
|
|
|
// Parse --build-id or --build-id=<style>. We handle "tree" as a
|
|
|
|
// synonym for "sha1" because all our hash functions including
|
|
|
|
// -build-id=sha1 are actually tree hashes for performance reasons.
|
|
|
|
static std::pair<BuildIdKind, std::vector<uint8_t>>
|
|
|
|
getBuildId(opt::InputArgList &Args) {
|
2017-05-24 05:16:48 +08:00
|
|
|
auto *Arg = Args.getLastArg(OPT_build_id, OPT_build_id_eq);
|
|
|
|
if (!Arg)
|
|
|
|
return {BuildIdKind::None, {}};
|
|
|
|
|
|
|
|
if (Arg->getOption().getID() == OPT_build_id)
|
2017-04-27 05:23:11 +08:00
|
|
|
return {BuildIdKind::Fast, {}};
|
|
|
|
|
2017-05-24 05:16:48 +08:00
|
|
|
StringRef S = Arg->getValue();
|
2018-02-08 03:22:42 +08:00
|
|
|
if (S == "fast")
|
|
|
|
return {BuildIdKind::Fast, {}};
|
2017-04-27 05:23:11 +08:00
|
|
|
if (S == "md5")
|
|
|
|
return {BuildIdKind::Md5, {}};
|
|
|
|
if (S == "sha1" || S == "tree")
|
|
|
|
return {BuildIdKind::Sha1, {}};
|
|
|
|
if (S == "uuid")
|
|
|
|
return {BuildIdKind::Uuid, {}};
|
|
|
|
if (S.startswith("0x"))
|
|
|
|
return {BuildIdKind::Hexstring, parseHex(S.substr(2))};
|
|
|
|
|
|
|
|
if (S != "none")
|
|
|
|
error("unknown --build-id style: " + S);
|
|
|
|
return {BuildIdKind::None, {}};
|
|
|
|
}
|
|
|
|
|
2018-07-10 04:22:28 +08:00
|
|
|
static std::pair<bool, bool> getPackDynRelocs(opt::InputArgList &Args) {
|
|
|
|
StringRef S = Args.getLastArgValue(OPT_pack_dyn_relocs, "none");
|
|
|
|
if (S == "android")
|
|
|
|
return {true, false};
|
|
|
|
if (S == "relr")
|
|
|
|
return {false, true};
|
|
|
|
if (S == "android+relr")
|
|
|
|
return {true, true};
|
|
|
|
|
|
|
|
if (S != "none")
|
|
|
|
error("unknown -pack-dyn-relocs format: " + S);
|
|
|
|
return {false, false};
|
|
|
|
}
|
|
|
|
|
2018-04-18 07:30:05 +08:00
|
|
|
static void readCallGraph(MemoryBufferRef MB) {
|
|
|
|
// Build a map from symbol name to section
|
2018-10-26 23:07:02 +08:00
|
|
|
DenseMap<StringRef, Symbol *> Map;
|
2018-04-18 07:30:05 +08:00
|
|
|
for (InputFile *File : ObjectFiles)
|
|
|
|
for (Symbol *Sym : File->getSymbols())
|
2018-10-26 23:07:02 +08:00
|
|
|
Map[Sym->getName()] = Sym;
|
|
|
|
|
|
|
|
auto FindSection = [&](StringRef Name) -> InputSectionBase * {
|
|
|
|
Symbol *Sym = Map.lookup(Name);
|
|
|
|
if (!Sym) {
|
|
|
|
if (Config->WarnSymbolOrdering)
|
|
|
|
warn(MB.getBufferIdentifier() + ": no such symbol: " + Name);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2018-10-26 23:07:12 +08:00
|
|
|
maybeWarnUnorderableSymbol(Sym);
|
2018-08-04 15:31:19 +08:00
|
|
|
|
2018-10-26 23:07:02 +08:00
|
|
|
if (Defined *DR = dyn_cast_or_null<Defined>(Sym))
|
2018-08-04 15:31:19 +08:00
|
|
|
return dyn_cast_or_null<InputSectionBase>(DR->Section);
|
|
|
|
return nullptr;
|
|
|
|
};
|
|
|
|
|
2018-10-26 23:07:02 +08:00
|
|
|
for (StringRef Line : args::getLines(MB)) {
|
2018-04-18 07:30:05 +08:00
|
|
|
SmallVector<StringRef, 3> Fields;
|
2018-10-26 23:07:02 +08:00
|
|
|
Line.split(Fields, ' ');
|
2018-04-18 07:30:05 +08:00
|
|
|
uint64_t Count;
|
2018-08-04 15:31:19 +08:00
|
|
|
|
2018-10-26 23:07:02 +08:00
|
|
|
if (Fields.size() != 3 || !to_integer(Fields[2], Count)) {
|
|
|
|
error(MB.getBufferIdentifier() + ": parse error");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (InputSectionBase *From = FindSection(Fields[0]))
|
|
|
|
if (InputSectionBase *To = FindSection(Fields[1]))
|
|
|
|
Config->CallGraphProfile[std::make_pair(From, To)] += Count;
|
2018-04-18 07:30:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-02 08:17:15 +08:00
|
|
|
template <class ELFT> static void readCallGraphsFromObjectFiles() {
|
|
|
|
for (auto File : ObjectFiles) {
|
|
|
|
auto *Obj = cast<ObjFile<ELFT>>(File);
|
2018-10-26 23:07:02 +08:00
|
|
|
|
2018-10-02 08:17:15 +08:00
|
|
|
for (const Elf_CGProfile_Impl<ELFT> &CGPE : Obj->CGProfile) {
|
2018-10-23 07:43:53 +08:00
|
|
|
auto *FromSym = dyn_cast<Defined>(&Obj->getSymbol(CGPE.cgp_from));
|
|
|
|
auto *ToSym = dyn_cast<Defined>(&Obj->getSymbol(CGPE.cgp_to));
|
|
|
|
if (!FromSym || !ToSym)
|
2018-10-02 08:17:15 +08:00
|
|
|
continue;
|
2018-10-26 23:07:02 +08:00
|
|
|
|
|
|
|
auto *From = dyn_cast_or_null<InputSectionBase>(FromSym->Section);
|
|
|
|
auto *To = dyn_cast_or_null<InputSectionBase>(ToSym->Section);
|
|
|
|
if (From && To)
|
|
|
|
Config->CallGraphProfile[{From, To}] += CGPE.cgp_weight;
|
2018-10-02 08:17:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-17 16:58:12 +08:00
|
|
|
static bool getCompressDebugSections(opt::InputArgList &Args) {
|
2017-06-14 16:01:26 +08:00
|
|
|
StringRef S = Args.getLastArgValue(OPT_compress_debug_sections, "none");
|
2017-04-30 06:56:24 +08:00
|
|
|
if (S == "none")
|
|
|
|
return false;
|
|
|
|
if (S != "zlib")
|
|
|
|
error("unknown --compress-debug-sections value: " + S);
|
|
|
|
if (!zlib::isAvailable())
|
|
|
|
error("--compress-debug-sections: zlib is not available");
|
|
|
|
return true;
|
2017-04-17 16:58:12 +08:00
|
|
|
}
|
|
|
|
|
2018-05-22 10:53:11 +08:00
|
|
|
static std::pair<StringRef, StringRef> getOldNewOptions(opt::InputArgList &Args,
|
|
|
|
unsigned Id) {
|
|
|
|
auto *Arg = Args.getLastArg(Id);
|
|
|
|
if (!Arg)
|
|
|
|
return {"", ""};
|
|
|
|
|
|
|
|
StringRef S = Arg->getValue();
|
|
|
|
std::pair<StringRef, StringRef> Ret = S.split(';');
|
|
|
|
if (Ret.second.empty())
|
|
|
|
error(Arg->getSpelling() + " expects 'old;new' format, but got " + S);
|
|
|
|
return Ret;
|
2017-08-14 18:17:30 +08:00
|
|
|
}
|
|
|
|
|
2018-02-14 21:36:22 +08:00
|
|
|
// Parse the symbol ordering file and warn for any duplicate entries.
|
|
|
|
static std::vector<StringRef> getSymbolOrderingFile(MemoryBufferRef MB) {
|
|
|
|
SetVector<StringRef> Names;
|
|
|
|
for (StringRef S : args::getLines(MB))
|
|
|
|
if (!Names.insert(S) && Config->WarnSymbolOrdering)
|
|
|
|
warn(MB.getBufferIdentifier() + ": duplicate ordered symbol: " + S);
|
|
|
|
|
|
|
|
return Names.takeVector();
|
|
|
|
}
|
|
|
|
|
2018-03-31 01:22:44 +08:00
|
|
|
static void parseClangOption(StringRef Opt, const Twine &Msg) {
|
|
|
|
std::string Err;
|
|
|
|
raw_string_ostream OS(Err);
|
|
|
|
|
|
|
|
const char *Argv[] = {Config->ProgName.data(), Opt.data()};
|
|
|
|
if (cl::ParseCommandLineOptions(2, Argv, "", &OS))
|
|
|
|
return;
|
|
|
|
OS.flush();
|
|
|
|
error(Msg + ": " + StringRef(Err).trim());
|
|
|
|
}
|
|
|
|
|
2016-01-08 01:54:19 +08:00
|
|
|
// Initializes Config members by the command line options.
|
2019-05-09 00:20:05 +08:00
|
|
|
static void readConfigs(opt::InputArgList &Args) {
|
2018-02-09 09:43:59 +08:00
|
|
|
errorHandler().Verbose = Args.hasArg(OPT_verbose);
|
2018-02-09 07:52:09 +08:00
|
|
|
errorHandler().FatalWarnings =
|
|
|
|
Args.hasFlag(OPT_fatal_warnings, OPT_no_fatal_warnings, false);
|
2018-05-23 00:19:38 +08:00
|
|
|
ThreadsEnabled = Args.hasFlag(OPT_threads, OPT_no_threads, true);
|
2018-02-09 07:52:09 +08:00
|
|
|
|
2017-08-12 04:49:48 +08:00
|
|
|
Config->AllowMultipleDefinition =
|
2018-02-06 08:45:15 +08:00
|
|
|
Args.hasFlag(OPT_allow_multiple_definition,
|
|
|
|
OPT_no_allow_multiple_definition, false) ||
|
|
|
|
hasZOption(Args, "muldefs");
|
2019-02-02 08:34:28 +08:00
|
|
|
Config->AllowShlibUndefined =
|
|
|
|
Args.hasFlag(OPT_allow_shlib_undefined, OPT_no_allow_shlib_undefined,
|
|
|
|
Args.hasArg(OPT_shared));
|
2017-11-29 03:58:45 +08:00
|
|
|
Config->AuxiliaryList = args::getStrings(Args, OPT_auxiliary);
|
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);
|
2018-02-03 06:24:06 +08:00
|
|
|
Config->CheckSections =
|
|
|
|
Args.hasFlag(OPT_check_sections, OPT_no_check_sections, true);
|
2017-07-21 02:17:55 +08:00
|
|
|
Config->Chroot = Args.getLastArgValue(OPT_chroot);
|
2017-04-17 16:58:12 +08:00
|
|
|
Config->CompressDebugSections = getCompressDebugSections(Args);
|
2018-03-15 04:29:45 +08:00
|
|
|
Config->Cref = Args.hasFlag(OPT_cref, OPT_no_cref, false);
|
2017-10-25 04:59:55 +08:00
|
|
|
Config->DefineCommon = Args.hasFlag(OPT_define_common, OPT_no_define_common,
|
|
|
|
!Args.hasArg(OPT_relocatable));
|
|
|
|
Config->Demangle = Args.hasFlag(OPT_demangle, OPT_no_demangle, true);
|
2016-04-03 11:39:09 +08:00
|
|
|
Config->DisableVerify = Args.hasArg(OPT_disable_verify);
|
2017-02-25 10:23:28 +08:00
|
|
|
Config->Discard = getDiscard(Args);
|
2018-07-17 01:55:48 +08:00
|
|
|
Config->DwoDir = Args.getLastArgValue(OPT_plugin_opt_dwo_dir_eq);
|
2017-02-25 10:23:28 +08:00
|
|
|
Config->DynamicLinker = getDynamicLinker(Args);
|
2017-08-30 00:53:24 +08:00
|
|
|
Config->EhFrameHdr =
|
2017-10-25 04:59:55 +08:00
|
|
|
Args.hasFlag(OPT_eh_frame_hdr, OPT_no_eh_frame_hdr, false);
|
2018-12-15 05:58:49 +08:00
|
|
|
Config->EmitLLVM = Args.hasArg(OPT_plugin_opt_emit_llvm, false);
|
2017-02-25 09:51:25 +08:00
|
|
|
Config->EmitRelocs = Args.hasArg(OPT_emit_relocs);
|
2018-10-26 07:15:23 +08:00
|
|
|
Config->CallGraphProfileSort = Args.hasFlag(
|
|
|
|
OPT_call_graph_profile_sort, OPT_no_call_graph_profile_sort, true);
|
2018-03-02 06:23:51 +08:00
|
|
|
Config->EnableNewDtags =
|
|
|
|
Args.hasFlag(OPT_enable_new_dtags, OPT_disable_new_dtags, true);
|
2017-06-14 16:01:26 +08:00
|
|
|
Config->Entry = Args.getLastArgValue(OPT_entry);
|
[AArch64] Support execute-only LOAD segments.
Summary:
This adds an LLD flag to mark executable LOAD segments execute-only for AArch64 targets.
In AArch64 the expectation is that code is execute-only compatible, so this just adds a linker option to enforce this.
Patch by: ivanlozano (Ivan Lozano)
Reviewers: srhines, echristo, peter.smith, eugenis, javed.absar, espindola, ruiu
Reviewed By: ruiu
Subscribers: dokyungs, emaste, arichardson, kristof.beyls, llvm-commits
Differential Revision: https://reviews.llvm.org/D49456
llvm-svn: 338271
2018-07-31 01:02:46 +08:00
|
|
|
Config->ExecuteOnly =
|
|
|
|
Args.hasFlag(OPT_execute_only, OPT_no_execute_only, false);
|
2017-01-15 11:38:55 +08:00
|
|
|
Config->ExportDynamic =
|
2017-10-25 04:59:55 +08:00
|
|
|
Args.hasFlag(OPT_export_dynamic, OPT_no_export_dynamic, false);
|
2017-11-29 03:58:45 +08:00
|
|
|
Config->FilterList = args::getStrings(Args, OPT_filter);
|
2017-06-14 16:01:26 +08:00
|
|
|
Config->Fini = Args.getLastArgValue(OPT_fini, "_fini");
|
2017-12-05 23:59:05 +08:00
|
|
|
Config->FixCortexA53Errata843419 = Args.hasArg(OPT_fix_cortex_a53_843419);
|
2017-10-25 04:59:55 +08:00
|
|
|
Config->GcSections = Args.hasFlag(OPT_gc_sections, OPT_no_gc_sections, false);
|
2018-02-03 05:44:06 +08:00
|
|
|
Config->GnuUnique = Args.hasFlag(OPT_gnu_unique, OPT_no_gnu_unique, true);
|
2017-10-25 04:59:55 +08:00
|
|
|
Config->GdbIndex = Args.hasFlag(OPT_gdb_index, OPT_no_gdb_index, false);
|
2018-07-19 06:49:31 +08:00
|
|
|
Config->ICF = getICF(Args);
|
2018-01-10 09:37:36 +08:00
|
|
|
Config->IgnoreDataAddressEquality =
|
|
|
|
Args.hasArg(OPT_ignore_data_address_equality);
|
|
|
|
Config->IgnoreFunctionAddressEquality =
|
|
|
|
Args.hasArg(OPT_ignore_function_address_equality);
|
2017-06-14 16:01:26 +08:00
|
|
|
Config->Init = Args.getLastArgValue(OPT_init, "_init");
|
|
|
|
Config->LTOAAPipeline = Args.getLastArgValue(OPT_lto_aa_pipeline);
|
2019-03-12 06:51:38 +08:00
|
|
|
Config->LTOCSProfileGenerate = Args.hasArg(OPT_lto_cs_profile_generate);
|
|
|
|
Config->LTOCSProfileFile = Args.getLastArgValue(OPT_lto_cs_profile_file);
|
2018-04-10 01:56:07 +08:00
|
|
|
Config->LTODebugPassManager = Args.hasArg(OPT_lto_debug_pass_manager);
|
|
|
|
Config->LTONewPassManager = Args.hasArg(OPT_lto_new_pass_manager);
|
2017-06-14 16:01:26 +08:00
|
|
|
Config->LTONewPmPasses = Args.getLastArgValue(OPT_lto_newpm_passes);
|
2017-11-29 03:58:45 +08:00
|
|
|
Config->LTOO = args::getInteger(Args, OPT_lto_O, 2);
|
2018-05-22 10:53:11 +08:00
|
|
|
Config->LTOObjPath = Args.getLastArgValue(OPT_plugin_opt_obj_path_eq);
|
2017-11-29 03:58:45 +08:00
|
|
|
Config->LTOPartitions = args::getInteger(Args, OPT_lto_partitions, 1);
|
2018-04-10 01:56:07 +08:00
|
|
|
Config->LTOSampleProfile = Args.getLastArgValue(OPT_lto_sample_profile);
|
2017-06-14 16:01:26 +08:00
|
|
|
Config->MapFile = Args.getLastArgValue(OPT_Map);
|
2018-06-11 15:24:31 +08:00
|
|
|
Config->MipsGotSize = args::getInteger(Args, OPT_mips_got_size, 0xfff0);
|
2017-12-15 19:09:41 +08:00
|
|
|
Config->MergeArmExidx =
|
|
|
|
Args.hasFlag(OPT_merge_exidx_entries, OPT_no_merge_exidx_entries, true);
|
2019-05-14 00:01:26 +08:00
|
|
|
Config->Nmagic = Args.hasFlag(OPT_nmagic, OPT_no_nmagic, false);
|
2017-07-26 17:46:59 +08:00
|
|
|
Config->NoinhibitExec = Args.hasArg(OPT_noinhibit_exec);
|
2016-09-03 03:20:33 +08:00
|
|
|
Config->Nostdlib = Args.hasArg(OPT_nostdlib);
|
2017-02-25 09:51:25 +08:00
|
|
|
Config->OFormatBinary = isOutputFormatBinary(Args);
|
2017-11-01 10:04:43 +08:00
|
|
|
Config->Omagic = Args.hasFlag(OPT_omagic, OPT_no_omagic, false);
|
2017-06-14 16:01:26 +08:00
|
|
|
Config->OptRemarksFilename = Args.getLastArgValue(OPT_opt_remarks_filename);
|
2019-03-13 05:22:27 +08:00
|
|
|
Config->OptRemarksPasses = Args.getLastArgValue(OPT_opt_remarks_passes);
|
2017-02-14 01:49:18 +08:00
|
|
|
Config->OptRemarksWithHotness = Args.hasArg(OPT_opt_remarks_with_hotness);
|
2017-11-29 03:58:45 +08:00
|
|
|
Config->Optimize = args::getInteger(Args, OPT_O, 1);
|
2017-10-25 23:20:30 +08:00
|
|
|
Config->OrphanHandling = getOrphanHandling(Args);
|
2017-06-14 16:01:26 +08:00
|
|
|
Config->OutputFile = Args.getLastArgValue(OPT_o);
|
2018-02-02 08:31:05 +08:00
|
|
|
Config->Pie = Args.hasFlag(OPT_pie, OPT_no_pie, false);
|
2018-02-02 00:00:46 +08:00
|
|
|
Config->PrintIcfSections =
|
|
|
|
Args.hasFlag(OPT_print_icf_sections, OPT_no_print_icf_sections, false);
|
2017-11-01 10:04:43 +08:00
|
|
|
Config->PrintGcSections =
|
|
|
|
Args.hasFlag(OPT_print_gc_sections, OPT_no_print_gc_sections, false);
|
2019-03-28 07:52:22 +08:00
|
|
|
Config->PrintSymbolOrder =
|
|
|
|
Args.getLastArgValue(OPT_print_symbol_order);
|
2017-04-30 07:06:43 +08:00
|
|
|
Config->Rpath = getRpath(Args);
|
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);
|
2017-11-29 03:58:45 +08:00
|
|
|
Config->SearchPaths = args::getStrings(Args, OPT_library_path);
|
2017-02-25 09:51:25 +08:00
|
|
|
Config->SectionStartMap = getSectionStartMap(Args);
|
2015-09-30 06:33:18 +08:00
|
|
|
Config->Shared = Args.hasArg(OPT_shared);
|
2017-02-25 09:51:25 +08:00
|
|
|
Config->SingleRoRx = Args.hasArg(OPT_no_rosegment);
|
2017-06-14 16:01:26 +08:00
|
|
|
Config->SoName = Args.getLastArgValue(OPT_soname);
|
2017-02-25 10:23:28 +08:00
|
|
|
Config->SortSection = getSortSection(Args);
|
2018-10-17 01:13:01 +08:00
|
|
|
Config->SplitStackAdjustSize = args::getInteger(Args, OPT_split_stack_adjust_size, 16384);
|
2017-02-25 10:23:28 +08:00
|
|
|
Config->Strip = getStrip(Args);
|
2017-06-14 16:01:26 +08:00
|
|
|
Config->Sysroot = Args.getLastArgValue(OPT_sysroot);
|
2017-10-25 04:59:55 +08:00
|
|
|
Config->Target1Rel = Args.hasFlag(OPT_target1_rel, OPT_target1_abs, false);
|
2017-02-25 10:23:28 +08:00
|
|
|
Config->Target2 = getTarget2(Args);
|
2017-06-14 16:01:26 +08:00
|
|
|
Config->ThinLTOCacheDir = Args.getLastArgValue(OPT_thinlto_cache_dir);
|
2017-12-07 06:08:17 +08:00
|
|
|
Config->ThinLTOCachePolicy = CHECK(
|
2017-06-14 16:01:26 +08:00
|
|
|
parseCachePruningPolicy(Args.getLastArgValue(OPT_thinlto_cache_policy)),
|
|
|
|
"--thinlto-cache-policy: invalid cache policy");
|
2018-05-22 10:53:11 +08:00
|
|
|
Config->ThinLTOEmitImportsFiles =
|
|
|
|
Args.hasArg(OPT_plugin_opt_thinlto_emit_imports_files);
|
|
|
|
Config->ThinLTOIndexOnly = Args.hasArg(OPT_plugin_opt_thinlto_index_only) ||
|
|
|
|
Args.hasArg(OPT_plugin_opt_thinlto_index_only_eq);
|
|
|
|
Config->ThinLTOIndexOnlyArg =
|
|
|
|
Args.getLastArgValue(OPT_plugin_opt_thinlto_index_only_eq);
|
2017-11-29 03:58:45 +08:00
|
|
|
Config->ThinLTOJobs = args::getInteger(Args, OPT_thinlto_jobs, -1u);
|
2018-05-23 00:16:09 +08:00
|
|
|
Config->ThinLTOObjectSuffixReplace =
|
|
|
|
getOldNewOptions(Args, OPT_plugin_opt_thinlto_object_suffix_replace_eq);
|
|
|
|
Config->ThinLTOPrefixReplace =
|
|
|
|
getOldNewOptions(Args, OPT_plugin_opt_thinlto_prefix_replace_eq);
|
2016-03-29 16:45:40 +08:00
|
|
|
Config->Trace = Args.hasArg(OPT_trace);
|
2017-11-29 03:58:45 +08:00
|
|
|
Config->Undefined = args::getStrings(Args, OPT_undefined);
|
2018-02-03 05:44:06 +08:00
|
|
|
Config->UndefinedVersion =
|
|
|
|
Args.hasFlag(OPT_undefined_version, OPT_no_undefined_version, true);
|
2018-07-10 04:08:55 +08:00
|
|
|
Config->UseAndroidRelrTags = Args.hasFlag(
|
|
|
|
OPT_use_android_relr_tags, OPT_no_use_android_relr_tags, false);
|
2017-02-25 09:51:25 +08:00
|
|
|
Config->UnresolvedSymbols = getUnresolvedSymbolPolicy(Args);
|
Add --warn-backrefs to maintain compatibility with other linkers
I'm proposing a new command line flag, --warn-backrefs in this patch.
The flag and the feature proposed below don't exist in GNU linkers
nor the current lld.
--warn-backrefs is an option to detect reverse or cyclic dependencies
between static archives, and it can be used to keep your program
compatible with GNU linkers after you switch to lld. I'll explain the
feature and why you may find it useful below.
lld's symbol resolution semantics is more relaxed than traditional
Unix linkers. Therefore,
ld.lld foo.a bar.o
succeeds even if bar.o contains an undefined symbol that have to be
resolved by some object file in foo.a. Traditional Unix linkers
don't allow this kind of backward reference, as they visit each
file only once from left to right in the command line while
resolving all undefined symbol at the moment of visiting.
In the above case, since there's no undefined symbol when a linker
visits foo.a, no files are pulled out from foo.a, and because the
linker forgets about foo.a after visiting, it can't resolve
undefined symbols that could have been resolved otherwise.
That lld accepts more relaxed form means (besides it makes more
sense) that you can accidentally write a command line or a build
file that works only with lld, even if you have a plan to
distribute it to wider users who may be using GNU linkers. With
--check-library-dependency, you can detect a library order that
doesn't work with other Unix linkers.
The option is also useful to detect cyclic dependencies between
static archives. Again, lld accepts
ld.lld foo.a bar.a
even if foo.a and bar.a depend on each other. With --warn-backrefs
it is handled as an error.
Here is how the option works. We assign a group ID to each file. A
file with a smaller group ID can pull out object files from an
archive file with an equal or greater group ID. Otherwise, it is a
reverse dependency and an error.
A file outside --{start,end}-group gets a fresh ID when
instantiated. All files within the same --{start,end}-group get the
same group ID. E.g.
ld.lld A B --start-group C D --end-group E
A and B form group 0, C, D and their member object files form group
1, and E forms group 2. I think that you can see how this group
assignment rule simulates the traditional linker's semantics.
Differential Revision: https://reviews.llvm.org/D45195
llvm-svn: 329636
2018-04-10 07:05:48 +08:00
|
|
|
Config->WarnBackrefs =
|
|
|
|
Args.hasFlag(OPT_warn_backrefs, OPT_no_warn_backrefs, false);
|
2018-02-06 08:45:15 +08:00
|
|
|
Config->WarnCommon = Args.hasFlag(OPT_warn_common, OPT_no_warn_common, false);
|
Introduce a flag to warn when ifunc symbols are used with text relocations.
Summary:
This patch adds a new flag, --warn-ifunc-textrel, to work around a glibc bug. When a code with ifunc symbols is used to produce an object file with text relocations, lld always succeeds. However, if that object file is linked using an old version of glibc, the resultant binary just crashes with segmentation fault when it is run (The bug is going to be corrected as of glibc 2.19).
Since there is no way to tell beforehand what library the object file will be linked against in the future, there does not seem to be a fool-proof way for lld to give an error only in cases where the binary will crash. So, with this change (dated 2018-09-25), lld starts to give a warning, contingent on a new command line flag that does not have a gnu counter part. The default value for --warn-ifunc-textrel is false, so lld behaviour will not change unless the user explicitly asks lld to give a warning. Users that link with a glibc library with version 2.19 or newer, or does not use ifunc symbols, or does not generate object files with text relocations do not need to take any action. Other users may consider to start passing warn-ifunc-textrel to lld to get early warnings.
Reviewers: ruiu, espindola
Reviewed By: ruiu
Subscribers: grimar, MaskRay, markj, emaste, arichardson, llvm-commits
Differential Revision: https://reviews.llvm.org/D52430
llvm-svn: 343628
2018-10-03 04:30:22 +08:00
|
|
|
Config->WarnIfuncTextrel =
|
|
|
|
Args.hasFlag(OPT_warn_ifunc_textrel, OPT_no_warn_ifunc_textrel, false);
|
2018-02-14 21:36:22 +08:00
|
|
|
Config->WarnSymbolOrdering =
|
|
|
|
Args.hasFlag(OPT_warn_symbol_ordering, OPT_no_warn_symbol_ordering, true);
|
2018-04-21 05:24:08 +08:00
|
|
|
Config->ZCombreloc = getZFlag(Args, "combreloc", "nocombreloc", true);
|
|
|
|
Config->ZCopyreloc = getZFlag(Args, "copyreloc", "nocopyreloc", true);
|
|
|
|
Config->ZExecstack = getZFlag(Args, "execstack", "noexecstack", false);
|
2018-08-28 16:24:34 +08:00
|
|
|
Config->ZGlobal = hasZOption(Args, "global");
|
2018-02-21 07:49:17 +08:00
|
|
|
Config->ZHazardplt = hasZOption(Args, "hazardplt");
|
2019-05-14 23:25:21 +08:00
|
|
|
Config->ZIfuncNoplt = hasZOption(Args, "ifunc-noplt");
|
2018-06-20 10:06:01 +08:00
|
|
|
Config->ZInitfirst = hasZOption(Args, "initfirst");
|
2018-09-14 22:25:37 +08:00
|
|
|
Config->ZInterpose = hasZOption(Args, "interpose");
|
2018-05-09 07:19:50 +08:00
|
|
|
Config->ZKeepTextSectionPrefix = getZFlag(
|
|
|
|
Args, "keep-text-section-prefix", "nokeep-text-section-prefix", false);
|
2018-11-27 17:48:17 +08:00
|
|
|
Config->ZNodefaultlib = hasZOption(Args, "nodefaultlib");
|
2015-11-13 03:00:37 +08:00
|
|
|
Config->ZNodelete = hasZOption(Args, "nodelete");
|
2017-03-23 08:54:16 +08:00
|
|
|
Config->ZNodlopen = hasZOption(Args, "nodlopen");
|
2018-04-21 05:24:08 +08:00
|
|
|
Config->ZNow = getZFlag(Args, "now", "lazy", false);
|
2015-11-13 03:00:37 +08:00
|
|
|
Config->ZOrigin = hasZOption(Args, "origin");
|
2018-04-21 05:24:08 +08:00
|
|
|
Config->ZRelro = getZFlag(Args, "relro", "norelro", true);
|
Introduce the "retpoline" x86 mitigation technique for variant #2 of the speculative execution vulnerabilities disclosed today, specifically identified by CVE-2017-5715, "Branch Target Injection", and is one of the two halves to Spectre..
Summary:
First, we need to explain the core of the vulnerability. Note that this
is a very incomplete description, please see the Project Zero blog post
for details:
https://googleprojectzero.blogspot.com/2018/01/reading-privileged-memory-with-side.html
The basis for branch target injection is to direct speculative execution
of the processor to some "gadget" of executable code by poisoning the
prediction of indirect branches with the address of that gadget. The
gadget in turn contains an operation that provides a side channel for
reading data. Most commonly, this will look like a load of secret data
followed by a branch on the loaded value and then a load of some
predictable cache line. The attacker then uses timing of the processors
cache to determine which direction the branch took *in the speculative
execution*, and in turn what one bit of the loaded value was. Due to the
nature of these timing side channels and the branch predictor on Intel
processors, this allows an attacker to leak data only accessible to
a privileged domain (like the kernel) back into an unprivileged domain.
The goal is simple: avoid generating code which contains an indirect
branch that could have its prediction poisoned by an attacker. In many
cases, the compiler can simply use directed conditional branches and
a small search tree. LLVM already has support for lowering switches in
this way and the first step of this patch is to disable jump-table
lowering of switches and introduce a pass to rewrite explicit indirectbr
sequences into a switch over integers.
However, there is no fully general alternative to indirect calls. We
introduce a new construct we call a "retpoline" to implement indirect
calls in a non-speculatable way. It can be thought of loosely as
a trampoline for indirect calls which uses the RET instruction on x86.
Further, we arrange for a specific call->ret sequence which ensures the
processor predicts the return to go to a controlled, known location. The
retpoline then "smashes" the return address pushed onto the stack by the
call with the desired target of the original indirect call. The result
is a predicted return to the next instruction after a call (which can be
used to trap speculative execution within an infinite loop) and an
actual indirect branch to an arbitrary address.
On 64-bit x86 ABIs, this is especially easily done in the compiler by
using a guaranteed scratch register to pass the target into this device.
For 32-bit ABIs there isn't a guaranteed scratch register and so several
different retpoline variants are introduced to use a scratch register if
one is available in the calling convention and to otherwise use direct
stack push/pop sequences to pass the target address.
This "retpoline" mitigation is fully described in the following blog
post: https://support.google.com/faqs/answer/7625886
We also support a target feature that disables emission of the retpoline
thunk by the compiler to allow for custom thunks if users want them.
These are particularly useful in environments like kernels that
routinely do hot-patching on boot and want to hot-patch their thunk to
different code sequences. They can write this custom thunk and use
`-mretpoline-external-thunk` *in addition* to `-mretpoline`. In this
case, on x86-64 thu thunk names must be:
```
__llvm_external_retpoline_r11
```
or on 32-bit:
```
__llvm_external_retpoline_eax
__llvm_external_retpoline_ecx
__llvm_external_retpoline_edx
__llvm_external_retpoline_push
```
And the target of the retpoline is passed in the named register, or in
the case of the `push` suffix on the top of the stack via a `pushl`
instruction.
There is one other important source of indirect branches in x86 ELF
binaries: the PLT. These patches also include support for LLD to
generate PLT entries that perform a retpoline-style indirection.
The only other indirect branches remaining that we are aware of are from
precompiled runtimes (such as crt0.o and similar). The ones we have
found are not really attackable, and so we have not focused on them
here, but eventually these runtimes should also be replicated for
retpoline-ed configurations for completeness.
For kernels or other freestanding or fully static executables, the
compiler switch `-mretpoline` is sufficient to fully mitigate this
particular attack. For dynamic executables, you must compile *all*
libraries with `-mretpoline` and additionally link the dynamic
executable and all shared libraries with LLD and pass `-z retpolineplt`
(or use similar functionality from some other linker). We strongly
recommend also using `-z now` as non-lazy binding allows the
retpoline-mitigated PLT to be substantially smaller.
When manually apply similar transformations to `-mretpoline` to the
Linux kernel we observed very small performance hits to applications
running typical workloads, and relatively minor hits (approximately 2%)
even for extremely syscall-heavy applications. This is largely due to
the small number of indirect branches that occur in performance
sensitive paths of the kernel.
When using these patches on statically linked applications, especially
C++ applications, you should expect to see a much more dramatic
performance hit. For microbenchmarks that are switch, indirect-, or
virtual-call heavy we have seen overheads ranging from 10% to 50%.
However, real-world workloads exhibit substantially lower performance
impact. Notably, techniques such as PGO and ThinLTO dramatically reduce
the impact of hot indirect calls (by speculatively promoting them to
direct calls) and allow optimized search trees to be used to lower
switches. If you need to deploy these techniques in C++ applications, we
*strongly* recommend that you ensure all hot call targets are statically
linked (avoiding PLT indirection) and use both PGO and ThinLTO. Well
tuned servers using all of these techniques saw 5% - 10% overhead from
the use of retpoline.
We will add detailed documentation covering these components in
subsequent patches, but wanted to make the core functionality available
as soon as possible. Happy for more code review, but we'd really like to
get these patches landed and backported ASAP for obvious reasons. We're
planning to backport this to both 6.0 and 5.0 release streams and get
a 5.0 release with just this cherry picked ASAP for distros and vendors.
This patch is the work of a number of people over the past month: Eric, Reid,
Rui, and myself. I'm mailing it out as a single commit due to the time
sensitive nature of landing this and the need to backport it. Huge thanks to
everyone who helped out here, and everyone at Intel who helped out in
discussions about how to craft this. Also, credit goes to Paul Turner (at
Google, but not an LLVM contributor) for much of the underlying retpoline
design.
Reviewers: echristo, rnk, ruiu, craig.topper, DavidKreitzer
Subscribers: sanjoy, emaste, mcrosier, mgorny, mehdi_amini, hiraditya, llvm-commits
Differential Revision: https://reviews.llvm.org/D41723
llvm-svn: 323155
2018-01-23 06:05:25 +08:00
|
|
|
Config->ZRetpolineplt = hasZOption(Args, "retpolineplt");
|
2017-05-27 03:12:38 +08:00
|
|
|
Config->ZRodynamic = hasZOption(Args, "rodynamic");
|
2017-11-29 03:58:45 +08:00
|
|
|
Config->ZStackSize = args::getZOptionValue(Args, OPT_z, "stack-size", 0);
|
2018-04-21 05:24:08 +08:00
|
|
|
Config->ZText = getZFlag(Args, "text", "notext", true);
|
2016-10-14 18:34:36 +08:00
|
|
|
Config->ZWxneeded = hasZOption(Args, "wxneeded");
|
2015-11-13 03:00:37 +08:00
|
|
|
|
2018-05-23 00:16:09 +08:00
|
|
|
// Parse LTO options.
|
2018-05-22 10:53:11 +08:00
|
|
|
if (auto *Arg = Args.getLastArg(OPT_plugin_opt_mcpu_eq))
|
|
|
|
parseClangOption(Saver.save("-mcpu=" + StringRef(Arg->getValue())),
|
|
|
|
Arg->getSpelling());
|
|
|
|
|
|
|
|
for (auto *Arg : Args.filtered(OPT_plugin_opt))
|
|
|
|
parseClangOption(Arg->getValue(), Arg->getSpelling());
|
2018-03-31 01:22:44 +08:00
|
|
|
|
|
|
|
// Parse -mllvm options.
|
2018-02-06 20:20:05 +08:00
|
|
|
for (auto *Arg : Args.filtered(OPT_mllvm))
|
2018-03-31 01:22:44 +08:00
|
|
|
parseClangOption(Arg->getValue(), Arg->getSpelling());
|
2017-08-15 01:48:30 +08:00
|
|
|
|
2017-02-25 09:51:25 +08:00
|
|
|
if (Config->LTOO > 3)
|
2017-08-14 18:17:30 +08:00
|
|
|
error("invalid optimization level for LTO: " + Twine(Config->LTOO));
|
2017-02-25 09:51:25 +08:00
|
|
|
if (Config->LTOPartitions == 0)
|
|
|
|
error("--lto-partitions: number of threads must be > 0");
|
|
|
|
if (Config->ThinLTOJobs == 0)
|
|
|
|
error("--thinlto-jobs: number of threads must be > 0");
|
|
|
|
|
2018-10-17 01:13:01 +08:00
|
|
|
if (Config->SplitStackAdjustSize < 0)
|
|
|
|
error("--split-stack-adjust-size: size must be >= 0");
|
|
|
|
|
2017-08-15 01:48:30 +08:00
|
|
|
// Parse ELF{32,64}{LE,BE} and CPU type.
|
2017-02-25 09:51:25 +08:00
|
|
|
if (auto *Arg = Args.getLastArg(OPT_m)) {
|
|
|
|
StringRef S = Arg->getValue();
|
|
|
|
std::tie(Config->EKind, Config->EMachine, Config->OSABI) =
|
|
|
|
parseEmulation(S);
|
|
|
|
Config->MipsN32Abi = (S == "elf32btsmipn32" || S == "elf32ltsmipn32");
|
|
|
|
Config->Emulation = S;
|
|
|
|
}
|
2016-10-20 13:23:23 +08:00
|
|
|
|
2017-10-06 17:37:44 +08:00
|
|
|
// Parse -hash-style={sysv,gnu,both}.
|
|
|
|
if (auto *Arg = Args.getLastArg(OPT_hash_style)) {
|
|
|
|
StringRef S = Arg->getValue();
|
|
|
|
if (S == "sysv")
|
|
|
|
Config->SysvHash = true;
|
|
|
|
else if (S == "gnu")
|
|
|
|
Config->GnuHash = true;
|
|
|
|
else if (S == "both")
|
|
|
|
Config->SysvHash = Config->GnuHash = true;
|
|
|
|
else
|
|
|
|
error("unknown -hash-style: " + S);
|
|
|
|
}
|
|
|
|
|
2017-01-15 10:52:34 +08:00
|
|
|
if (Args.hasArg(OPT_print_map))
|
|
|
|
Config->MapFile = "-";
|
|
|
|
|
2019-05-14 00:01:26 +08:00
|
|
|
// Page alignment can be disabled by the -n (--nmagic) and -N (--omagic).
|
|
|
|
// As PT_GNU_RELRO relies on Paging, do not create it when we have disabled
|
|
|
|
// it.
|
|
|
|
if (Config->Nmagic || Config->Omagic)
|
2016-12-03 15:09:28 +08:00
|
|
|
Config->ZRelro = false;
|
|
|
|
|
2017-04-27 05:23:11 +08:00
|
|
|
std::tie(Config->BuildId, Config->BuildIdVector) = getBuildId(Args);
|
2016-04-08 06:49:21 +08:00
|
|
|
|
2018-07-10 04:22:28 +08:00
|
|
|
std::tie(Config->AndroidPackDynRelocs, Config->RelrPackDynRelocs) =
|
|
|
|
getPackDynRelocs(Args);
|
2017-10-28 01:49:40 +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()))
|
2018-02-14 21:36:22 +08:00
|
|
|
Config->SymbolOrderingFile = getSymbolOrderingFile(*Buffer);
|
2016-11-10 17:05:20 +08:00
|
|
|
|
2017-01-26 05:49:23 +08:00
|
|
|
// If --retain-symbol-file is used, we'll keep only the symbols listed in
|
2016-12-20 02:00:52 +08:00
|
|
|
// the file and discard all others.
|
|
|
|
if (auto *Arg = Args.getLastArg(OPT_retain_symbols_file)) {
|
2017-01-26 05:23:06 +08:00
|
|
|
Config->DefaultSymbolVersion = VER_NDX_LOCAL;
|
2016-12-20 02:00:52 +08:00
|
|
|
if (Optional<MemoryBufferRef> Buffer = readFile(Arg->getValue()))
|
2017-11-29 03:58:45 +08:00
|
|
|
for (StringRef S : args::getLines(*Buffer))
|
2017-01-26 05:23:06 +08:00
|
|
|
Config->VersionScriptGlobals.push_back(
|
|
|
|
{S, /*IsExternCpp*/ false, /*HasWildcard*/ false});
|
2016-12-20 02:00:52 +08:00
|
|
|
}
|
|
|
|
|
2017-04-26 01:40:12 +08:00
|
|
|
bool HasExportDynamic =
|
2017-10-25 04:59:55 +08:00
|
|
|
Args.hasFlag(OPT_export_dynamic, OPT_no_export_dynamic, false);
|
2016-04-23 04:21:26 +08:00
|
|
|
|
2017-04-26 01:40:12 +08:00
|
|
|
// Parses -dynamic-list and -export-dynamic-symbol. They make some
|
|
|
|
// symbols private. Note that -export-dynamic takes precedence over them
|
|
|
|
// as it says all symbols should be exported.
|
|
|
|
if (!HasExportDynamic) {
|
|
|
|
for (auto *Arg : Args.filtered(OPT_dynamic_list))
|
|
|
|
if (Optional<MemoryBufferRef> Buffer = readFile(Arg->getValue()))
|
|
|
|
readDynamicList(*Buffer);
|
|
|
|
|
2018-02-15 02:38:33 +08:00
|
|
|
for (auto *Arg : Args.filtered(OPT_export_dynamic_symbol))
|
2017-10-07 06:09:03 +08:00
|
|
|
Config->DynamicList.push_back(
|
2017-04-26 01:40:12 +08:00
|
|
|
{Arg->getValue(), /*IsExternCpp*/ false, /*HasWildcard*/ false});
|
|
|
|
}
|
2017-04-12 06:37:54 +08:00
|
|
|
|
2018-02-15 02:38:33 +08:00
|
|
|
// If --export-dynamic-symbol=foo is given and symbol foo is defined in
|
|
|
|
// an object file in an archive file, that object file should be pulled
|
|
|
|
// out and linked. (It doesn't have to behave like that from technical
|
|
|
|
// point of view, but this is needed for compatibility with GNU.)
|
|
|
|
for (auto *Arg : Args.filtered(OPT_export_dynamic_symbol))
|
|
|
|
Config->Undefined.push_back(Arg->getValue());
|
|
|
|
|
2017-12-07 11:25:39 +08:00
|
|
|
for (auto *Arg : Args.filtered(OPT_version_script))
|
2018-07-26 05:53:18 +08:00
|
|
|
if (Optional<std::string> Path = searchScript(Arg->getValue())) {
|
|
|
|
if (Optional<MemoryBufferRef> Buffer = readFile(*Path))
|
|
|
|
readVersionScript(*Buffer);
|
|
|
|
} else {
|
|
|
|
error(Twine("cannot find version script ") + Arg->getValue());
|
|
|
|
}
|
2016-01-08 01:54:19 +08:00
|
|
|
}
|
2015-10-20 01:35:12 +08:00
|
|
|
|
2017-03-18 07:29:01 +08:00
|
|
|
// Some Config members do not directly correspond to any particular
|
|
|
|
// command line options, but computed based on other Config values.
|
|
|
|
// This function initialize such members. See Config.h for the details
|
|
|
|
// of these values.
|
2018-02-06 04:55:46 +08:00
|
|
|
static void setConfigs(opt::InputArgList &Args) {
|
2018-09-21 05:29:14 +08:00
|
|
|
ELFKind K = Config->EKind;
|
|
|
|
uint16_t M = Config->EMachine;
|
2017-03-18 07:29:01 +08:00
|
|
|
|
|
|
|
Config->CopyRelocs = (Config->Relocatable || Config->EmitRelocs);
|
2018-09-21 05:29:14 +08:00
|
|
|
Config->Is64 = (K == ELF64LEKind || K == ELF64BEKind);
|
|
|
|
Config->IsLE = (K == ELF32LEKind || K == ELF64LEKind);
|
|
|
|
Config->Endianness = Config->IsLE ? endianness::little : endianness::big;
|
|
|
|
Config->IsMips64EL = (K == ELF64LEKind && M == EM_MIPS);
|
2017-03-18 07:29:01 +08:00
|
|
|
Config->Pic = Config->Pie || Config->Shared;
|
2019-01-16 20:09:13 +08:00
|
|
|
Config->PicThunk = Args.hasArg(OPT_pic_veneer, Config->Pic);
|
2017-03-22 08:01:11 +08:00
|
|
|
Config->Wordsize = Config->Is64 ? 8 : 4;
|
2018-06-08 08:18:32 +08:00
|
|
|
|
|
|
|
// ELF defines two different ways to store relocation addends as shown below:
|
|
|
|
//
|
|
|
|
// Rel: Addends are stored to the location where relocations are applied.
|
|
|
|
// Rela: Addends are stored as part of relocation entry.
|
|
|
|
//
|
|
|
|
// In other words, Rela makes it easy to read addends at the price of extra
|
|
|
|
// 4 or 8 byte for each relocation entry. We don't know why ELF defined two
|
|
|
|
// different mechanisms in the first place, but this is how the spec is
|
|
|
|
// defined.
|
|
|
|
//
|
|
|
|
// You cannot choose which one, Rel or Rela, you want to use. Instead each
|
|
|
|
// ABI defines which one you need to use. The following expression expresses
|
|
|
|
// that.
|
2018-09-28 22:09:16 +08:00
|
|
|
Config->IsRela = M == EM_AARCH64 || M == EM_AMDGPU || M == EM_HEXAGON ||
|
|
|
|
M == EM_PPC || M == EM_PPC64 || M == EM_RISCV ||
|
|
|
|
M == EM_X86_64;
|
2018-06-08 08:18:32 +08:00
|
|
|
|
2018-02-16 18:01:17 +08:00
|
|
|
// If the output uses REL relocations we must store the dynamic relocation
|
|
|
|
// addends to the output sections. We also store addends for RELA relocations
|
|
|
|
// if --apply-dynamic-relocs is used.
|
|
|
|
// We default to not writing the addends when using RELA relocations since
|
|
|
|
// any standard conforming tool can find it in r_addend.
|
2018-02-06 04:55:46 +08:00
|
|
|
Config->WriteAddends = Args.hasFlag(OPT_apply_dynamic_relocs,
|
|
|
|
OPT_no_apply_dynamic_relocs, false) ||
|
|
|
|
!Config->IsRela;
|
2018-09-20 08:26:44 +08:00
|
|
|
|
|
|
|
Config->TocOptimize =
|
2018-09-21 05:29:14 +08:00
|
|
|
Args.hasFlag(OPT_toc_optimize, OPT_no_toc_optimize, M == EM_PPC64);
|
2017-03-18 07:29:01 +08:00
|
|
|
}
|
|
|
|
|
2016-10-20 12:36:36 +08:00
|
|
|
// Returns a value of "-format" option.
|
2018-08-07 05:29:41 +08:00
|
|
|
static bool isFormatBinary(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) {
|
2018-05-31 21:00:25 +08:00
|
|
|
// For --{push,pop}-state.
|
|
|
|
std::vector<std::tuple<bool, bool, bool>> Stack;
|
|
|
|
|
|
|
|
// Iterate over argv to process input files and positional arguments.
|
2015-10-02 00:42:03 +08:00
|
|
|
for (auto *Arg : Args) {
|
2018-04-04 16:13:28 +08:00
|
|
|
switch (Arg->getOption().getUnaliasedOption().getID()) {
|
2017-07-22 00:27:26 +08:00
|
|
|
case OPT_library:
|
2016-02-03 05:13:09 +08:00
|
|
|
addLibrary(Arg->getValue());
|
2015-10-02 00:42:03 +08:00
|
|
|
break;
|
|
|
|
case OPT_INPUT:
|
2017-04-12 08:13:48 +08:00
|
|
|
addFile(Arg->getValue(), /*WithLOption=*/false);
|
2015-10-02 00:42:03 +08:00
|
|
|
break;
|
2018-01-17 18:24:49 +08:00
|
|
|
case OPT_defsym: {
|
|
|
|
StringRef From;
|
|
|
|
StringRef To;
|
|
|
|
std::tie(From, To) = StringRef(Arg->getValue()).split('=');
|
2018-08-10 14:32:39 +08:00
|
|
|
if (From.empty() || To.empty())
|
|
|
|
error("-defsym: syntax error: " + StringRef(Arg->getValue()));
|
|
|
|
else
|
|
|
|
readDefsym(From, MemoryBufferRef(To, "-defsym"));
|
2018-01-17 18:24:49 +08:00
|
|
|
break;
|
|
|
|
}
|
2016-09-10 06:08:04 +08:00
|
|
|
case OPT_script:
|
2018-07-26 05:53:18 +08:00
|
|
|
if (Optional<std::string> Path = searchScript(Arg->getValue())) {
|
2017-11-20 23:43:20 +08:00
|
|
|
if (Optional<MemoryBufferRef> MB = readFile(*Path))
|
|
|
|
readLinkerScript(*MB);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
error(Twine("cannot find linker script ") + Arg->getValue());
|
2016-09-10 06:08:04 +08:00
|
|
|
break;
|
2015-10-12 04:59:12 +08:00
|
|
|
case OPT_as_needed:
|
2018-04-04 16:13:28 +08:00
|
|
|
Config->AsNeeded = true;
|
2015-10-12 04:59:12 +08:00
|
|
|
break;
|
2016-10-20 12:36:36 +08:00
|
|
|
case OPT_format:
|
2018-08-07 05:29:41 +08:00
|
|
|
Config->FormatBinary = isFormatBinary(Arg->getValue());
|
2016-09-10 06:08:04 +08:00
|
|
|
break;
|
2018-04-04 16:13:28 +08:00
|
|
|
case OPT_no_as_needed:
|
|
|
|
Config->AsNeeded = false;
|
|
|
|
break;
|
2015-10-02 00:42:03 +08:00
|
|
|
case OPT_Bstatic:
|
2019-05-14 00:01:26 +08:00
|
|
|
case OPT_omagic:
|
|
|
|
case OPT_nmagic:
|
2018-04-04 16:13:28 +08:00
|
|
|
Config->Static = true;
|
|
|
|
break;
|
2015-10-02 00:42:03 +08:00
|
|
|
case OPT_Bdynamic:
|
2018-04-04 16:13:28 +08:00
|
|
|
Config->Static = false;
|
2015-10-02 00:42:03 +08:00
|
|
|
break;
|
2015-10-02 02:02:21 +08:00
|
|
|
case OPT_whole_archive:
|
2018-04-04 16:13:28 +08:00
|
|
|
InWholeArchive = true;
|
|
|
|
break;
|
2015-10-02 02:02:21 +08:00
|
|
|
case OPT_no_whole_archive:
|
2018-04-04 16:13:28 +08:00
|
|
|
InWholeArchive = false;
|
2015-10-02 02:02:21 +08:00
|
|
|
break;
|
2018-03-30 09:15:36 +08:00
|
|
|
case OPT_just_symbols:
|
|
|
|
if (Optional<MemoryBufferRef> MB = readFile(Arg->getValue())) {
|
|
|
|
Files.push_back(createObjectFile(*MB));
|
|
|
|
Files.back()->JustSymbols = true;
|
|
|
|
}
|
|
|
|
break;
|
Add --warn-backrefs to maintain compatibility with other linkers
I'm proposing a new command line flag, --warn-backrefs in this patch.
The flag and the feature proposed below don't exist in GNU linkers
nor the current lld.
--warn-backrefs is an option to detect reverse or cyclic dependencies
between static archives, and it can be used to keep your program
compatible with GNU linkers after you switch to lld. I'll explain the
feature and why you may find it useful below.
lld's symbol resolution semantics is more relaxed than traditional
Unix linkers. Therefore,
ld.lld foo.a bar.o
succeeds even if bar.o contains an undefined symbol that have to be
resolved by some object file in foo.a. Traditional Unix linkers
don't allow this kind of backward reference, as they visit each
file only once from left to right in the command line while
resolving all undefined symbol at the moment of visiting.
In the above case, since there's no undefined symbol when a linker
visits foo.a, no files are pulled out from foo.a, and because the
linker forgets about foo.a after visiting, it can't resolve
undefined symbols that could have been resolved otherwise.
That lld accepts more relaxed form means (besides it makes more
sense) that you can accidentally write a command line or a build
file that works only with lld, even if you have a plan to
distribute it to wider users who may be using GNU linkers. With
--check-library-dependency, you can detect a library order that
doesn't work with other Unix linkers.
The option is also useful to detect cyclic dependencies between
static archives. Again, lld accepts
ld.lld foo.a bar.a
even if foo.a and bar.a depend on each other. With --warn-backrefs
it is handled as an error.
Here is how the option works. We assign a group ID to each file. A
file with a smaller group ID can pull out object files from an
archive file with an equal or greater group ID. Otherwise, it is a
reverse dependency and an error.
A file outside --{start,end}-group gets a fresh ID when
instantiated. All files within the same --{start,end}-group get the
same group ID. E.g.
ld.lld A B --start-group C D --end-group E
A and B form group 0, C, D and their member object files form group
1, and E forms group 2. I think that you can see how this group
assignment rule simulates the traditional linker's semantics.
Differential Revision: https://reviews.llvm.org/D45195
llvm-svn: 329636
2018-04-10 07:05:48 +08:00
|
|
|
case OPT_start_group:
|
|
|
|
if (InputFile::IsInGroup)
|
|
|
|
error("nested --start-group");
|
|
|
|
InputFile::IsInGroup = true;
|
|
|
|
break;
|
|
|
|
case OPT_end_group:
|
|
|
|
if (!InputFile::IsInGroup)
|
|
|
|
error("stray --end-group");
|
|
|
|
InputFile::IsInGroup = false;
|
2018-04-20 07:23:23 +08:00
|
|
|
++InputFile::NextGroupId;
|
Add --warn-backrefs to maintain compatibility with other linkers
I'm proposing a new command line flag, --warn-backrefs in this patch.
The flag and the feature proposed below don't exist in GNU linkers
nor the current lld.
--warn-backrefs is an option to detect reverse or cyclic dependencies
between static archives, and it can be used to keep your program
compatible with GNU linkers after you switch to lld. I'll explain the
feature and why you may find it useful below.
lld's symbol resolution semantics is more relaxed than traditional
Unix linkers. Therefore,
ld.lld foo.a bar.o
succeeds even if bar.o contains an undefined symbol that have to be
resolved by some object file in foo.a. Traditional Unix linkers
don't allow this kind of backward reference, as they visit each
file only once from left to right in the command line while
resolving all undefined symbol at the moment of visiting.
In the above case, since there's no undefined symbol when a linker
visits foo.a, no files are pulled out from foo.a, and because the
linker forgets about foo.a after visiting, it can't resolve
undefined symbols that could have been resolved otherwise.
That lld accepts more relaxed form means (besides it makes more
sense) that you can accidentally write a command line or a build
file that works only with lld, even if you have a plan to
distribute it to wider users who may be using GNU linkers. With
--check-library-dependency, you can detect a library order that
doesn't work with other Unix linkers.
The option is also useful to detect cyclic dependencies between
static archives. Again, lld accepts
ld.lld foo.a bar.a
even if foo.a and bar.a depend on each other. With --warn-backrefs
it is handled as an error.
Here is how the option works. We assign a group ID to each file. A
file with a smaller group ID can pull out object files from an
archive file with an equal or greater group ID. Otherwise, it is a
reverse dependency and an error.
A file outside --{start,end}-group gets a fresh ID when
instantiated. All files within the same --{start,end}-group get the
same group ID. E.g.
ld.lld A B --start-group C D --end-group E
A and B form group 0, C, D and their member object files form group
1, and E forms group 2. I think that you can see how this group
assignment rule simulates the traditional linker's semantics.
Differential Revision: https://reviews.llvm.org/D45195
llvm-svn: 329636
2018-04-10 07:05:48 +08:00
|
|
|
break;
|
2016-04-08 03:24:51 +08:00
|
|
|
case OPT_start_lib:
|
2018-04-21 00:33:01 +08:00
|
|
|
if (InLib)
|
|
|
|
error("nested --start-lib");
|
|
|
|
if (InputFile::IsInGroup)
|
|
|
|
error("may not nest --start-lib in --start-group");
|
2018-04-04 16:13:28 +08:00
|
|
|
InLib = true;
|
2018-04-21 00:33:01 +08:00
|
|
|
InputFile::IsInGroup = true;
|
2018-04-04 16:13:28 +08:00
|
|
|
break;
|
2016-04-08 03:24:51 +08:00
|
|
|
case OPT_end_lib:
|
2018-04-21 00:33:01 +08:00
|
|
|
if (!InLib)
|
|
|
|
error("stray --end-lib");
|
2018-04-04 16:13:28 +08:00
|
|
|
InLib = false;
|
2018-04-21 00:33:01 +08:00
|
|
|
InputFile::IsInGroup = false;
|
|
|
|
++InputFile::NextGroupId;
|
2016-04-08 03:24:51 +08:00
|
|
|
break;
|
2018-05-31 21:00:25 +08:00
|
|
|
case OPT_push_state:
|
2018-05-31 21:24:01 +08:00
|
|
|
Stack.emplace_back(Config->AsNeeded, Config->Static, InWholeArchive);
|
2018-05-31 21:00:25 +08:00
|
|
|
break;
|
|
|
|
case OPT_pop_state:
|
|
|
|
if (Stack.empty()) {
|
|
|
|
error("unbalanced --push-state/--pop-state");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
std::tie(Config->AsNeeded, Config->Static, InWholeArchive) = Stack.back();
|
|
|
|
Stack.pop_back();
|
|
|
|
break;
|
2015-09-28 20:52:21 +08:00
|
|
|
}
|
2015-07-25 05:03:07 +08:00
|
|
|
}
|
|
|
|
|
[lld] unified COFF and ELF error handling on new Common/ErrorHandler
Summary:
The COFF linker and the ELF linker have long had similar but separate
Error.h and Error.cpp files to implement error handling. This change
introduces new error handling code in Common/ErrorHandler.h, changes the
COFF and ELF linkers to use it, and removes the old, separate
implementations.
Reviewers: ruiu
Reviewed By: ruiu
Subscribers: smeenai, jyknight, emaste, sdardis, nemanjai, nhaehnle, mgorny, javed.absar, kbarton, fedor.sergeev, llvm-commits
Differential Revision: https://reviews.llvm.org/D39259
llvm-svn: 316624
2017-10-26 06:28:38 +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-12-09 01:44:37 +08:00
|
|
|
// Parse -z max-page-size=<value>. The default value is defined by
|
|
|
|
// each target.
|
|
|
|
static uint64_t getMaxPageSize(opt::InputArgList &Args) {
|
2017-11-29 03:58:45 +08:00
|
|
|
uint64_t Val = args::getZOptionValue(Args, OPT_z, "max-page-size",
|
|
|
|
Target->DefaultMaxPageSize);
|
2016-12-09 01:44:37 +08:00
|
|
|
if (!isPowerOf2_64(Val))
|
|
|
|
error("max-page-size: value isn't a power of 2");
|
2019-05-14 00:01:26 +08:00
|
|
|
if (Config->Nmagic || Config->Omagic) {
|
|
|
|
if (Val != Target->DefaultMaxPageSize)
|
|
|
|
warn("-z max-page-size set, but paging disabled by omagic or nmagic");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return Val;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse -z common-page-size=<value>. The default value is defined by
|
|
|
|
// each target.
|
|
|
|
static uint64_t getCommonPageSize(opt::InputArgList &Args) {
|
|
|
|
uint64_t Val = args::getZOptionValue(Args, OPT_z, "common-page-size",
|
|
|
|
Target->DefaultCommonPageSize);
|
|
|
|
if (!isPowerOf2_64(Val))
|
|
|
|
error("common-page-size: value isn't a power of 2");
|
|
|
|
if (Config->Nmagic || Config->Omagic) {
|
|
|
|
if (Val != Target->DefaultCommonPageSize)
|
|
|
|
warn("-z common-page-size set, but paging disabled by omagic or nmagic");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
// CommonPageSize can't be larger than MaxPageSize.
|
|
|
|
if (Val > Config->MaxPageSize)
|
|
|
|
Val = Config->MaxPageSize;
|
2016-12-09 01:44:37 +08:00
|
|
|
return Val;
|
|
|
|
}
|
|
|
|
|
2016-10-26 12:34:16 +08:00
|
|
|
// Parses -image-base option.
|
2017-10-10 18:09:35 +08:00
|
|
|
static Optional<uint64_t> getImageBase(opt::InputArgList &Args) {
|
|
|
|
// Because we are using "Config->MaxPageSize" here, this function has to be
|
|
|
|
// called after the variable is initialized.
|
2016-10-26 12:34:16 +08:00
|
|
|
auto *Arg = Args.getLastArg(OPT_image_base);
|
|
|
|
if (!Arg)
|
2017-10-10 18:09:35 +08:00
|
|
|
return None;
|
2016-10-26 12:34:16 +08:00
|
|
|
|
|
|
|
StringRef S = Arg->getValue();
|
|
|
|
uint64_t V;
|
2017-05-16 16:19:25 +08:00
|
|
|
if (!to_integer(S, V)) {
|
2016-10-26 12:34:16 +08:00
|
|
|
error("-image-base: number expected, but got " + S);
|
|
|
|
return 0;
|
|
|
|
}
|
2016-12-08 04:29:46 +08:00
|
|
|
if ((V % Config->MaxPageSize) != 0)
|
2016-10-26 12:34:16 +08:00
|
|
|
warn("-image-base: address isn't multiple of page size: " + S);
|
|
|
|
return V;
|
|
|
|
}
|
|
|
|
|
2017-06-21 23:36:24 +08:00
|
|
|
// Parses `--exclude-libs=lib,lib,...`.
|
|
|
|
// The library names may be delimited by commas or colons.
|
|
|
|
static DenseSet<StringRef> getExcludeLibs(opt::InputArgList &Args) {
|
|
|
|
DenseSet<StringRef> Ret;
|
|
|
|
for (auto *Arg : Args.filtered(OPT_exclude_libs)) {
|
|
|
|
StringRef S = Arg->getValue();
|
|
|
|
for (;;) {
|
|
|
|
size_t Pos = S.find_first_of(",:");
|
|
|
|
if (Pos == StringRef::npos)
|
|
|
|
break;
|
|
|
|
Ret.insert(S.substr(0, Pos));
|
|
|
|
S = S.substr(Pos + 1);
|
|
|
|
}
|
|
|
|
Ret.insert(S);
|
|
|
|
}
|
|
|
|
return Ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handles the -exclude-libs option. If a static library file is specified
|
|
|
|
// by the -exclude-libs option, all public symbols from the archive become
|
|
|
|
// private unless otherwise specified by version scripts or something.
|
|
|
|
// A special library name "ALL" means all archive files.
|
|
|
|
//
|
|
|
|
// This is not a popular option, but some programs such as bionic libc use it.
|
2018-02-17 04:23:54 +08:00
|
|
|
static void excludeLibs(opt::InputArgList &Args) {
|
2017-06-21 23:36:24 +08:00
|
|
|
DenseSet<StringRef> Libs = getExcludeLibs(Args);
|
|
|
|
bool All = Libs.count("ALL");
|
|
|
|
|
2018-07-12 01:45:28 +08:00
|
|
|
auto Visit = [&](InputFile *File) {
|
2018-02-17 04:23:54 +08:00
|
|
|
if (!File->ArchiveName.empty())
|
|
|
|
if (All || Libs.count(path::filename(File->ArchiveName)))
|
2017-11-04 05:21:47 +08:00
|
|
|
for (Symbol *Sym : File->getSymbols())
|
2018-02-17 04:23:54 +08:00
|
|
|
if (!Sym->isLocal() && Sym->File == File)
|
2017-11-01 00:07:41 +08:00
|
|
|
Sym->VersionId = VER_NDX_LOCAL;
|
2018-07-12 01:45:28 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
for (InputFile *File : ObjectFiles)
|
|
|
|
Visit(File);
|
|
|
|
|
|
|
|
for (BitcodeFile *File : BitcodeFiles)
|
|
|
|
Visit(File);
|
2017-06-21 23:36:24 +08:00
|
|
|
}
|
|
|
|
|
2018-04-04 02:01:18 +08:00
|
|
|
// Force Sym to be entered in the output. Used for -u or equivalent.
|
|
|
|
template <class ELFT> static void handleUndefined(StringRef Name) {
|
|
|
|
Symbol *Sym = Symtab->find(Name);
|
|
|
|
if (!Sym)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Since symbol S may not be used inside the program, LTO may
|
|
|
|
// eliminate it. Mark the symbol as "used" to prevent it.
|
|
|
|
Sym->IsUsedInRegularObj = true;
|
|
|
|
|
|
|
|
if (Sym->isLazy())
|
|
|
|
Symtab->fetchLazy<ELFT>(Sym);
|
|
|
|
}
|
|
|
|
|
2018-08-09 07:48:12 +08:00
|
|
|
template <class ELFT> static void handleLibcall(StringRef Name) {
|
|
|
|
Symbol *Sym = Symtab->find(Name);
|
|
|
|
if (!Sym || !Sym->isLazy())
|
|
|
|
return;
|
|
|
|
|
|
|
|
MemoryBufferRef MB;
|
|
|
|
if (auto *LO = dyn_cast<LazyObject>(Sym))
|
|
|
|
MB = LO->File->MB;
|
|
|
|
else
|
|
|
|
MB = cast<LazyArchive>(Sym)->getMemberBuffer();
|
|
|
|
|
|
|
|
if (isBitcode(MB))
|
|
|
|
Symtab->fetchLazy<ELFT>(Sym);
|
|
|
|
}
|
|
|
|
|
2018-09-12 07:00:36 +08:00
|
|
|
// If all references to a DSO happen to be weak, the DSO is not added
|
|
|
|
// to DT_NEEDED. If that happens, we need to eliminate shared symbols
|
|
|
|
// created from the DSO. Otherwise, they become dangling references
|
|
|
|
// that point to a non-existent DSO.
|
2019-04-09 01:35:55 +08:00
|
|
|
static void demoteSharedSymbols() {
|
2018-04-25 08:29:13 +08:00
|
|
|
for (Symbol *Sym : Symtab->getSymbols()) {
|
2019-05-16 10:14:00 +08:00
|
|
|
auto *S = dyn_cast<SharedSymbol>(Sym);
|
|
|
|
if (!S || S->getFile().IsNeeded)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
bool Used = S->Used;
|
|
|
|
Undefined New(nullptr, S->getName(), STB_WEAK, S->StOther, S->Type);
|
|
|
|
replaceSymbol(S, &New);
|
|
|
|
S->Used = Used;
|
2018-04-25 08:29:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-21 10:14:59 +08:00
|
|
|
// The section referred to by S is considered address-significant. Set the
|
|
|
|
// KeepUnique flag on the section if appropriate.
|
|
|
|
static void markAddrsig(Symbol *S) {
|
|
|
|
if (auto *D = dyn_cast_or_null<Defined>(S))
|
|
|
|
if (D->Section)
|
|
|
|
// We don't need to keep text sections unique under --icf=all even if they
|
|
|
|
// are address-significant.
|
|
|
|
if (Config->ICF == ICFLevel::Safe || !(D->Section->Flags & SHF_EXECINSTR))
|
|
|
|
D->Section->KeepUnique = true;
|
2018-07-19 06:49:31 +08:00
|
|
|
}
|
|
|
|
|
2018-05-15 16:57:21 +08:00
|
|
|
// Record sections that define symbols mentioned in --keep-unique <symbol>
|
2018-07-19 06:49:31 +08:00
|
|
|
// and symbols referred to by address-significance tables. These sections are
|
|
|
|
// ineligible for ICF.
|
|
|
|
template <class ELFT>
|
2018-05-15 16:57:21 +08:00
|
|
|
static void findKeepUniqueSections(opt::InputArgList &Args) {
|
|
|
|
for (auto *Arg : Args.filtered(OPT_keep_unique)) {
|
|
|
|
StringRef Name = Arg->getValue();
|
2018-07-21 10:14:59 +08:00
|
|
|
auto *D = dyn_cast_or_null<Defined>(Symtab->find(Name));
|
|
|
|
if (!D || !D->Section) {
|
2018-05-15 16:57:21 +08:00
|
|
|
warn("could not find symbol " + Name + " to keep unique");
|
2018-07-21 10:14:59 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
D->Section->KeepUnique = true;
|
2018-05-15 16:57:21 +08:00
|
|
|
}
|
2018-07-19 06:49:31 +08:00
|
|
|
|
2018-07-21 10:14:59 +08:00
|
|
|
// --icf=all --ignore-data-address-equality means that we can ignore
|
|
|
|
// the dynsym and address-significance tables entirely.
|
|
|
|
if (Config->ICF == ICFLevel::All && Config->IgnoreDataAddressEquality)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Symbols in the dynsym could be address-significant in other executables
|
|
|
|
// or DSOs, so we conservatively mark them as address-significant.
|
|
|
|
for (Symbol *S : Symtab->getSymbols())
|
|
|
|
if (S->includeInDynsym())
|
|
|
|
markAddrsig(S);
|
|
|
|
|
|
|
|
// Visit the address-significance table in each object file and mark each
|
|
|
|
// referenced symbol as address-significant.
|
|
|
|
for (InputFile *F : ObjectFiles) {
|
|
|
|
auto *Obj = cast<ObjFile<ELFT>>(F);
|
|
|
|
ArrayRef<Symbol *> Syms = Obj->getSymbols();
|
|
|
|
if (Obj->AddrsigSec) {
|
|
|
|
ArrayRef<uint8_t> Contents =
|
|
|
|
check(Obj->getObj().getSectionContents(Obj->AddrsigSec));
|
|
|
|
const uint8_t *Cur = Contents.begin();
|
|
|
|
while (Cur != Contents.end()) {
|
|
|
|
unsigned Size;
|
|
|
|
const char *Err;
|
|
|
|
uint64_t SymIndex = decodeULEB128(Cur, &Size, Contents.end(), &Err);
|
|
|
|
if (Err)
|
|
|
|
fatal(toString(F) + ": could not decode addrsig section: " + Err);
|
|
|
|
markAddrsig(Syms[SymIndex]);
|
|
|
|
Cur += Size;
|
2018-07-19 06:49:31 +08:00
|
|
|
}
|
2018-07-21 10:14:59 +08:00
|
|
|
} else {
|
|
|
|
// If an object file does not have an address-significance table,
|
|
|
|
// conservatively mark all of its symbols as address-significant.
|
|
|
|
for (Symbol *S : Syms)
|
|
|
|
markAddrsig(S);
|
2018-07-19 06:49:31 +08:00
|
|
|
}
|
|
|
|
}
|
2018-05-15 16:57:21 +08:00
|
|
|
}
|
|
|
|
|
2018-10-12 04:34:29 +08:00
|
|
|
template <class ELFT> static Symbol *addUndefined(StringRef Name) {
|
2019-05-16 10:14:00 +08:00
|
|
|
return Symtab->addUndefined<ELFT>(
|
|
|
|
Undefined{nullptr, Name, STB_GLOBAL, STV_DEFAULT, 0});
|
2018-10-12 04:34:29 +08:00
|
|
|
}
|
|
|
|
|
Change how we handle -wrap.
We have an issue with -wrap that the option doesn't work well when
renamed symbols get PLT entries. I'll explain what is the issue and
how this patch solves it.
For one -wrap option, we have three symbols: foo, wrap_foo and real_foo.
Currently, we use memcpy to overwrite wrapped symbols so that they get
the same contents. This works in most cases but doesn't when the relocation
processor sets some flags in the symbol. memcpy'ed symbols are just
aliases, so they always have to have the same contents, but the
relocation processor breaks that assumption.
r336609 is an attempt to fix the issue by memcpy'ing again after
processing relocations, so that symbols that are out of sync get the
same contents again. That works in most cases as well, but it breaks
ASan build in a mysterious way.
We could probably fix the issue by choosing symbol attributes that need
to be copied after they are updated. But it feels too complicated to me.
So, in this patch, I fixed it once and for all. With this patch, we no
longer memcpy symbols. All references to renamed symbols point to new
symbols after wrapSymbols() is done.
Differential Revision: https://reviews.llvm.org/D50569
llvm-svn: 340387
2018-08-22 15:02:26 +08:00
|
|
|
// The --wrap option is a feature to rename symbols so that you can write
|
|
|
|
// wrappers for existing functions. If you pass `-wrap=foo`, all
|
|
|
|
// occurrences of symbol `foo` are resolved to `wrap_foo` (so, you are
|
|
|
|
// expected to write `wrap_foo` function as a wrapper). The original
|
|
|
|
// symbol becomes accessible as `real_foo`, so you can call that from your
|
|
|
|
// wrapper.
|
|
|
|
//
|
|
|
|
// This data structure is instantiated for each -wrap option.
|
|
|
|
struct WrappedSymbol {
|
|
|
|
Symbol *Sym;
|
|
|
|
Symbol *Real;
|
|
|
|
Symbol *Wrap;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Handles -wrap option.
|
|
|
|
//
|
|
|
|
// This function instantiates wrapper symbols. At this point, they seem
|
|
|
|
// like they are not being used at all, so we explicitly set some flags so
|
|
|
|
// that LTO won't eliminate them.
|
|
|
|
template <class ELFT>
|
|
|
|
static std::vector<WrappedSymbol> addWrappedSymbols(opt::InputArgList &Args) {
|
|
|
|
std::vector<WrappedSymbol> V;
|
|
|
|
DenseSet<StringRef> Seen;
|
|
|
|
|
|
|
|
for (auto *Arg : Args.filtered(OPT_wrap)) {
|
|
|
|
StringRef Name = Arg->getValue();
|
|
|
|
if (!Seen.insert(Name).second)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Symbol *Sym = Symtab->find(Name);
|
|
|
|
if (!Sym)
|
|
|
|
continue;
|
|
|
|
|
2018-10-12 04:34:29 +08:00
|
|
|
Symbol *Real = addUndefined<ELFT>(Saver.save("__real_" + Name));
|
|
|
|
Symbol *Wrap = addUndefined<ELFT>(Saver.save("__wrap_" + Name));
|
Change how we handle -wrap.
We have an issue with -wrap that the option doesn't work well when
renamed symbols get PLT entries. I'll explain what is the issue and
how this patch solves it.
For one -wrap option, we have three symbols: foo, wrap_foo and real_foo.
Currently, we use memcpy to overwrite wrapped symbols so that they get
the same contents. This works in most cases but doesn't when the relocation
processor sets some flags in the symbol. memcpy'ed symbols are just
aliases, so they always have to have the same contents, but the
relocation processor breaks that assumption.
r336609 is an attempt to fix the issue by memcpy'ing again after
processing relocations, so that symbols that are out of sync get the
same contents again. That works in most cases as well, but it breaks
ASan build in a mysterious way.
We could probably fix the issue by choosing symbol attributes that need
to be copied after they are updated. But it feels too complicated to me.
So, in this patch, I fixed it once and for all. With this patch, we no
longer memcpy symbols. All references to renamed symbols point to new
symbols after wrapSymbols() is done.
Differential Revision: https://reviews.llvm.org/D50569
llvm-svn: 340387
2018-08-22 15:02:26 +08:00
|
|
|
V.push_back({Sym, Real, Wrap});
|
|
|
|
|
|
|
|
// We want to tell LTO not to inline symbols to be overwritten
|
|
|
|
// because LTO doesn't know the final symbol contents after renaming.
|
|
|
|
Real->CanInline = false;
|
|
|
|
Sym->CanInline = false;
|
|
|
|
|
|
|
|
// Tell LTO not to eliminate these symbols.
|
|
|
|
Sym->IsUsedInRegularObj = true;
|
|
|
|
Wrap->IsUsedInRegularObj = true;
|
|
|
|
}
|
|
|
|
return V;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do renaming for -wrap by updating pointers to symbols.
|
|
|
|
//
|
|
|
|
// When this function is executed, only InputFiles and symbol table
|
|
|
|
// contain pointers to symbol objects. We visit them to replace pointers,
|
|
|
|
// so that wrapped symbols are swapped as instructed by the command line.
|
2019-03-15 14:58:23 +08:00
|
|
|
static void wrapSymbols(ArrayRef<WrappedSymbol> Wrapped) {
|
Change how we handle -wrap.
We have an issue with -wrap that the option doesn't work well when
renamed symbols get PLT entries. I'll explain what is the issue and
how this patch solves it.
For one -wrap option, we have three symbols: foo, wrap_foo and real_foo.
Currently, we use memcpy to overwrite wrapped symbols so that they get
the same contents. This works in most cases but doesn't when the relocation
processor sets some flags in the symbol. memcpy'ed symbols are just
aliases, so they always have to have the same contents, but the
relocation processor breaks that assumption.
r336609 is an attempt to fix the issue by memcpy'ing again after
processing relocations, so that symbols that are out of sync get the
same contents again. That works in most cases as well, but it breaks
ASan build in a mysterious way.
We could probably fix the issue by choosing symbol attributes that need
to be copied after they are updated. But it feels too complicated to me.
So, in this patch, I fixed it once and for all. With this patch, we no
longer memcpy symbols. All references to renamed symbols point to new
symbols after wrapSymbols() is done.
Differential Revision: https://reviews.llvm.org/D50569
llvm-svn: 340387
2018-08-22 15:02:26 +08:00
|
|
|
DenseMap<Symbol *, Symbol *> Map;
|
|
|
|
for (const WrappedSymbol &W : Wrapped) {
|
|
|
|
Map[W.Sym] = W.Wrap;
|
|
|
|
Map[W.Real] = W.Sym;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update pointers in input files.
|
|
|
|
parallelForEach(ObjectFiles, [&](InputFile *File) {
|
|
|
|
std::vector<Symbol *> &Syms = File->getMutableSymbols();
|
|
|
|
for (size_t I = 0, E = Syms.size(); I != E; ++I)
|
|
|
|
if (Symbol *S = Map.lookup(Syms[I]))
|
|
|
|
Syms[I] = S;
|
|
|
|
});
|
|
|
|
|
|
|
|
// Update pointers in the symbol table.
|
|
|
|
for (const WrappedSymbol &W : Wrapped)
|
|
|
|
Symtab->wrap(W.Sym, W.Real, W.Wrap);
|
|
|
|
}
|
|
|
|
|
2018-08-01 04:36:17 +08:00
|
|
|
static const char *LibcallRoutineNames[] = {
|
|
|
|
#define HANDLE_LIBCALL(code, name) name,
|
|
|
|
#include "llvm/IR/RuntimeLibcalls.def"
|
|
|
|
#undef HANDLE_LIBCALL
|
|
|
|
};
|
|
|
|
|
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) {
|
2017-10-06 17:37:44 +08:00
|
|
|
// If a -hash-style option was not given, set to a default value,
|
|
|
|
// which varies depending on the target.
|
|
|
|
if (!Args.hasArg(OPT_hash_style)) {
|
|
|
|
if (Config->EMachine == EM_MIPS)
|
|
|
|
Config->SysvHash = true;
|
|
|
|
else
|
|
|
|
Config->SysvHash = Config->GnuHash = true;
|
|
|
|
}
|
|
|
|
|
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";
|
|
|
|
|
2017-04-04 17:42:24 +08:00
|
|
|
// Fail early if the output file or map file is not writable. If a user has a
|
|
|
|
// long link, e.g. due to a large LTO link, they do not wish to run it and
|
|
|
|
// find that it failed because there was a mistake in their command-line.
|
2017-04-27 00:14:46 +08:00
|
|
|
if (auto E = tryCreateFile(Config->OutputFile))
|
|
|
|
error("cannot open output file " + Config->OutputFile + ": " + E.message());
|
|
|
|
if (auto E = tryCreateFile(Config->MapFile))
|
|
|
|
error("cannot open map file " + Config->MapFile + ": " + E.message());
|
[lld] unified COFF and ELF error handling on new Common/ErrorHandler
Summary:
The COFF linker and the ELF linker have long had similar but separate
Error.h and Error.cpp files to implement error handling. This change
introduces new error handling code in Common/ErrorHandler.h, changes the
COFF and ELF linkers to use it, and removes the old, separate
implementations.
Reviewers: ruiu
Reviewed By: ruiu
Subscribers: smeenai, jyknight, emaste, sdardis, nemanjai, nhaehnle, mgorny, javed.absar, kbarton, fedor.sergeev, llvm-commits
Differential Revision: https://reviews.llvm.org/D39259
llvm-svn: 316624
2017-10-26 06:28:38 +08:00
|
|
|
if (errorCount())
|
2017-03-14 07:23:40 +08:00
|
|
|
return;
|
|
|
|
|
2016-12-09 01:32:58 +08:00
|
|
|
// Use default entry point name if no name was given via the command
|
|
|
|
// line nor linker scripts. For some reason, MIPS entry point name is
|
2016-12-07 12:45:34 +08:00
|
|
|
// different from others.
|
2016-12-21 06:24:45 +08:00
|
|
|
Config->WarnMissingEntry =
|
|
|
|
(!Config->Entry.empty() || (!Config->Shared && !Config->Relocatable));
|
2016-12-09 01:32:58 +08:00
|
|
|
if (Config->Entry.empty() && !Config->Relocatable)
|
2016-12-07 12:45:34 +08:00
|
|
|
Config->Entry = (Config->EMachine == EM_MIPS) ? "__start" : "_start";
|
|
|
|
|
2016-07-18 01:50:09 +08:00
|
|
|
// Handle --trace-symbol.
|
|
|
|
for (auto *Arg : Args.filtered(OPT_trace_symbol))
|
2017-07-27 02:42:48 +08:00
|
|
|
Symtab->trace(Arg->getValue());
|
2016-07-18 01:50:09 +08:00
|
|
|
|
2016-12-07 12:45:34 +08:00
|
|
|
// Add all files to the symbol table. This will add almost all
|
|
|
|
// symbols that we need to the symbol table.
|
2016-09-14 08:05:51 +08:00
|
|
|
for (InputFile *F : Files)
|
2019-05-14 20:03:13 +08:00
|
|
|
parseFile<ELFT>(F);
|
2016-09-08 16:57:51 +08:00
|
|
|
|
2017-09-16 02:05:02 +08:00
|
|
|
// Now that we have every file, we can decide if we will need a
|
|
|
|
// dynamic symbol table.
|
|
|
|
// We need one if we were asked to export dynamic symbols or if we are
|
|
|
|
// producing a shared library.
|
|
|
|
// We also need one if any shared libraries are used and for pie executables
|
|
|
|
// (probably because the dynamic linker needs it).
|
2017-09-19 17:20:54 +08:00
|
|
|
Config->HasDynSymTab =
|
|
|
|
!SharedFiles.empty() || Config->Pic || Config->ExportDynamic;
|
2017-09-16 02:05:02 +08:00
|
|
|
|
2017-08-23 16:37:22 +08:00
|
|
|
// Some symbols (such as __ehdr_start) are defined lazily only when there
|
|
|
|
// are undefined symbols for them, so we add these to trigger that logic.
|
2018-10-12 04:34:29 +08:00
|
|
|
for (StringRef Name : Script->ReferencedSymbols)
|
|
|
|
addUndefined<ELFT>(Name);
|
2017-08-23 16:37:22 +08:00
|
|
|
|
2017-10-04 04:45:09 +08:00
|
|
|
// Handle the `--undefined <sym>` options.
|
|
|
|
for (StringRef S : Config->Undefined)
|
2018-04-04 02:01:18 +08:00
|
|
|
handleUndefined<ELFT>(S);
|
2017-10-04 04:45:09 +08:00
|
|
|
|
2018-08-01 04:36:17 +08:00
|
|
|
// If an entry symbol is in a static archive, pull out that file now.
|
2018-04-04 02:01:18 +08:00
|
|
|
handleUndefined<ELFT>(Config->Entry);
|
2016-09-08 16:57:51 +08:00
|
|
|
|
2018-08-01 04:36:17 +08:00
|
|
|
// If any of our inputs are bitcode files, the LTO code generator may create
|
|
|
|
// references to certain library functions that might not be explicit in the
|
|
|
|
// bitcode file's symbol table. If any of those library functions are defined
|
|
|
|
// in a bitcode file in an archive member, we need to arrange to use LTO to
|
|
|
|
// compile those archive members by adding them to the link beforehand.
|
|
|
|
//
|
2018-08-09 07:48:12 +08:00
|
|
|
// However, adding all libcall symbols to the link can have undesired
|
|
|
|
// consequences. For example, the libgcc implementation of
|
|
|
|
// __sync_val_compare_and_swap_8 on 32-bit ARM pulls in an .init_array entry
|
|
|
|
// that aborts the program if the Linux kernel does not support 64-bit
|
|
|
|
// atomics, which would prevent the program from running even if it does not
|
|
|
|
// use 64-bit atomics.
|
|
|
|
//
|
|
|
|
// Therefore, we only add libcall symbols to the link before LTO if we have
|
|
|
|
// to, i.e. if the symbol's definition is in bitcode. Any other required
|
|
|
|
// libcall symbols will be added to the link after LTO when we add the LTO
|
|
|
|
// object file to the link.
|
2018-08-01 04:36:17 +08:00
|
|
|
if (!BitcodeFiles.empty())
|
|
|
|
for (const char *S : LibcallRoutineNames)
|
2018-08-09 07:48:12 +08:00
|
|
|
handleLibcall<ELFT>(S);
|
2018-08-01 04:36:17 +08:00
|
|
|
|
2016-12-07 12:45:34 +08:00
|
|
|
// Return if there were name resolution errors.
|
[lld] unified COFF and ELF error handling on new Common/ErrorHandler
Summary:
The COFF linker and the ELF linker have long had similar but separate
Error.h and Error.cpp files to implement error handling. This change
introduces new error handling code in Common/ErrorHandler.h, changes the
COFF and ELF linkers to use it, and removes the old, separate
implementations.
Reviewers: ruiu
Reviewed By: ruiu
Subscribers: smeenai, jyknight, emaste, sdardis, nemanjai, nhaehnle, mgorny, javed.absar, kbarton, fedor.sergeev, llvm-commits
Differential Revision: https://reviews.llvm.org/D39259
llvm-svn: 316624
2017-10-26 06:28:38 +08:00
|
|
|
if (errorCount())
|
2016-12-07 12:45:34 +08:00
|
|
|
return;
|
2015-10-10 05:07:25 +08:00
|
|
|
|
2018-03-08 22:54:38 +08:00
|
|
|
// Now when we read all script files, we want to finalize order of linker
|
|
|
|
// script commands, which can be not yet final because of INSERT commands.
|
|
|
|
Script->processInsertCommands();
|
|
|
|
|
2018-02-27 15:18:07 +08:00
|
|
|
// We want to declare linker script's symbols early,
|
|
|
|
// so that we can version them.
|
|
|
|
// They also might be exported if referenced by DSOs.
|
|
|
|
Script->declareSymbols();
|
|
|
|
|
2017-06-21 23:36:24 +08:00
|
|
|
// Handle the -exclude-libs option.
|
|
|
|
if (Args.hasArg(OPT_exclude_libs))
|
2019-03-17 21:53:42 +08:00
|
|
|
excludeLibs(Args);
|
2017-06-21 23:36:24 +08:00
|
|
|
|
2017-12-12 01:23:28 +08:00
|
|
|
// Create ElfHeader early. We need a dummy section in
|
|
|
|
// addReservedSymbols to mark the created symbols as not absolute.
|
|
|
|
Out::ElfHeader = make<OutputSection>("", 0, SHF_ALLOC);
|
|
|
|
Out::ElfHeader->Size = sizeof(typename ELFT::Ehdr);
|
|
|
|
|
2019-01-03 03:28:00 +08:00
|
|
|
// Create wrapped symbols for -wrap option.
|
|
|
|
std::vector<WrappedSymbol> Wrapped = addWrappedSymbols<ELFT>(Args);
|
|
|
|
|
2017-12-12 01:23:28 +08:00
|
|
|
// We need to create some reserved symbols such as _end. Create them.
|
|
|
|
if (!Config->Relocatable)
|
2017-12-24 01:21:39 +08:00
|
|
|
addReservedSymbols();
|
2017-12-12 01:23:28 +08:00
|
|
|
|
2017-06-21 23:36:24 +08:00
|
|
|
// Apply version scripts.
|
2018-02-15 10:40:58 +08:00
|
|
|
//
|
|
|
|
// For a relocatable output, version scripts don't make sense, and
|
|
|
|
// parsing a symbol version string (e.g. dropping "@ver1" from a symbol
|
|
|
|
// name "foo@ver1") rather do harm, so we don't call this if -r is given.
|
|
|
|
if (!Config->Relocatable)
|
|
|
|
Symtab->scanVersionScript();
|
2016-04-23 02:47:52 +08:00
|
|
|
|
2018-05-08 06:11:34 +08:00
|
|
|
// Do link-time optimization if given files are LLVM bitcode files.
|
|
|
|
// This compiles bitcode files into real object files.
|
2018-08-09 07:48:12 +08:00
|
|
|
//
|
|
|
|
// With this the symbol table should be complete. After this, no new names
|
|
|
|
// except a few linker-synthesized ones will be added to the symbol table.
|
2017-07-27 02:42:48 +08:00
|
|
|
Symtab->addCombinedLTOObject<ELFT>();
|
[lld] unified COFF and ELF error handling on new Common/ErrorHandler
Summary:
The COFF linker and the ELF linker have long had similar but separate
Error.h and Error.cpp files to implement error handling. This change
introduces new error handling code in Common/ErrorHandler.h, changes the
COFF and ELF linkers to use it, and removes the old, separate
implementations.
Reviewers: ruiu
Reviewed By: ruiu
Subscribers: smeenai, jyknight, emaste, sdardis, nemanjai, nhaehnle, mgorny, javed.absar, kbarton, fedor.sergeev, llvm-commits
Differential Revision: https://reviews.llvm.org/D39259
llvm-svn: 316624
2017-10-26 06:28:38 +08:00
|
|
|
if (errorCount())
|
2016-05-16 03:29:38 +08:00
|
|
|
return;
|
2016-02-13 04:54:57 +08:00
|
|
|
|
2018-05-08 06:11:34 +08:00
|
|
|
// If -thinlto-index-only is given, we should create only "index
|
|
|
|
// files" and not object files. Index file creation is already done
|
|
|
|
// in addCombinedLTOObject, so we are done if that's the case.
|
|
|
|
if (Config->ThinLTOIndexOnly)
|
|
|
|
return;
|
|
|
|
|
2018-12-15 05:58:49 +08:00
|
|
|
// Likewise, --plugin-opt=emit-llvm is an option to make LTO create
|
|
|
|
// an output file in bitcode and exit, so that you can just get a
|
|
|
|
// combined bitcode file.
|
|
|
|
if (Config->EmitLLVM)
|
|
|
|
return;
|
|
|
|
|
2017-11-05 07:09:43 +08:00
|
|
|
// Apply symbol renames for -wrap.
|
Change how we handle -wrap.
We have an issue with -wrap that the option doesn't work well when
renamed symbols get PLT entries. I'll explain what is the issue and
how this patch solves it.
For one -wrap option, we have three symbols: foo, wrap_foo and real_foo.
Currently, we use memcpy to overwrite wrapped symbols so that they get
the same contents. This works in most cases but doesn't when the relocation
processor sets some flags in the symbol. memcpy'ed symbols are just
aliases, so they always have to have the same contents, but the
relocation processor breaks that assumption.
r336609 is an attempt to fix the issue by memcpy'ing again after
processing relocations, so that symbols that are out of sync get the
same contents again. That works in most cases as well, but it breaks
ASan build in a mysterious way.
We could probably fix the issue by choosing symbol attributes that need
to be copied after they are updated. But it feels too complicated to me.
So, in this patch, I fixed it once and for all. With this patch, we no
longer memcpy symbols. All references to renamed symbols point to new
symbols after wrapSymbols() is done.
Differential Revision: https://reviews.llvm.org/D50569
llvm-svn: 340387
2018-08-22 15:02:26 +08:00
|
|
|
if (!Wrapped.empty())
|
2019-03-15 14:58:23 +08:00
|
|
|
wrapSymbols(Wrapped);
|
2017-04-26 18:40:02 +08:00
|
|
|
|
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.
|
2017-09-19 17:20:54 +08:00
|
|
|
for (InputFile *F : ObjectFiles)
|
2017-02-23 10:28:28 +08:00
|
|
|
for (InputSectionBase *S : F->getSections())
|
2017-02-24 00:49:07 +08:00
|
|
|
if (S && S != &InputSection::Discarded)
|
2017-02-27 10:32:08 +08:00
|
|
|
InputSections.push_back(S);
|
2017-09-19 17:20:54 +08:00
|
|
|
for (BinaryFile *F : BinaryFiles)
|
2017-02-23 10:32:18 +08:00
|
|
|
for (InputSectionBase *S : F->getSections())
|
2017-02-27 10:32:08 +08:00
|
|
|
InputSections.push_back(cast<InputSection>(S));
|
2016-11-06 06:37:59 +08:00
|
|
|
|
2017-11-04 16:20:30 +08:00
|
|
|
// We do not want to emit debug sections if --strip-all
|
|
|
|
// or -strip-debug are given.
|
2019-04-10 18:37:10 +08:00
|
|
|
if (Config->Strip != StripPolicy::None)
|
|
|
|
llvm::erase_if(InputSections, [](InputSectionBase *S) { return S->Debug; });
|
2017-11-04 16:20:30 +08:00
|
|
|
|
2017-10-25 01:01:40 +08:00
|
|
|
Config->EFlags = Target->calcEFlags();
|
2019-05-14 00:01:26 +08:00
|
|
|
// MaxPageSize (sometimes called abi page size) is the maximum page size that
|
|
|
|
// the output can be run on. For example if the OS can use 4k or 64k page
|
|
|
|
// sizes then MaxPageSize must be 64 for the output to be useable on both.
|
|
|
|
// All important alignment decisions must use this value.
|
2019-03-29 01:38:53 +08:00
|
|
|
Config->MaxPageSize = getMaxPageSize(Args);
|
2019-05-14 00:01:26 +08:00
|
|
|
// CommonPageSize is the most common page size that the output will be run on.
|
|
|
|
// For example if an OS can use 4k or 64k page sizes and 4k is more common
|
|
|
|
// than 64k then CommonPageSize is set to 4k. CommonPageSize can be used for
|
|
|
|
// optimizations such as DATA_SEGMENT_ALIGN in linker scripts. LLD's use of it
|
|
|
|
// is limited to writing trap instructions on the last executable segment.
|
|
|
|
Config->CommonPageSize = getCommonPageSize(Args);
|
|
|
|
|
2019-03-29 01:38:53 +08:00
|
|
|
Config->ImageBase = getImageBase(Args);
|
2017-10-02 22:56:41 +08:00
|
|
|
|
2017-11-28 21:51:48 +08:00
|
|
|
if (Config->EMachine == EM_ARM) {
|
|
|
|
// FIXME: These warnings can be removed when lld only uses these features
|
|
|
|
// when the input objects have been compiled with an architecture that
|
|
|
|
// supports them.
|
|
|
|
if (Config->ARMHasBlx == false)
|
|
|
|
warn("lld uses blx instruction, no object with architecture supporting "
|
2018-10-26 02:07:55 +08:00
|
|
|
"feature detected");
|
2017-11-28 21:51:48 +08:00
|
|
|
}
|
|
|
|
|
2017-06-12 08:00:51 +08:00
|
|
|
// This adds a .comment section containing a version string. We have to add it
|
Avoid unnecessary buffer allocation and memcpy for compressed sections.
Previously, we uncompress all compressed sections before doing anything.
That works, and that is conceptually simple, but that could results in
a waste of CPU time and memory if uncompressed sections are then
discarded or just copied to the output buffer.
In particular, if .debug_gnu_pub{names,types} are compressed and if no
-gdb-index option is given, we wasted CPU and memory because we
uncompress them into newly allocated bufers and then memcpy the buffers
to the output buffer. That temporary buffer was redundant.
This patch changes how to uncompress sections. Now, compressed sections
are uncompressed lazily. To do that, `Data` member of `InputSectionBase`
is now hidden from outside, and `data()` accessor automatically expands
an compressed buffer if necessary.
If no one calls `data()`, then `writeTo()` directly uncompresses
compressed data into the output buffer. That eliminates the redundant
memory allocation and redundant memcpy.
This patch significantly reduces memory consumption (20 GiB max RSS to
15 Gib) for an executable whose .debug_gnu_pub{names,types} are in total
5 GiB in an uncompressed form.
Differential Revision: https://reviews.llvm.org/D52917
llvm-svn: 343979
2018-10-09 00:58:59 +08:00
|
|
|
// before mergeSections because the .comment section is a mergeable section.
|
2017-06-12 08:00:51 +08:00
|
|
|
if (!Config->Relocatable)
|
2017-12-21 09:21:59 +08:00
|
|
|
InputSections.push_back(createCommentSection());
|
2017-06-12 08:00:51 +08:00
|
|
|
|
|
|
|
// Do size optimizations: garbage collection, merging of SHF_MERGE sections
|
|
|
|
// and identical code folding.
|
2018-04-28 02:17:36 +08:00
|
|
|
splitSections<ELFT>();
|
2017-10-11 06:59:32 +08:00
|
|
|
markLive<ELFT>();
|
2019-04-09 01:35:55 +08:00
|
|
|
demoteSharedSymbols();
|
2017-10-11 11:12:53 +08:00
|
|
|
mergeSections();
|
2018-07-19 06:49:31 +08:00
|
|
|
if (Config->ICF != ICFLevel::None) {
|
|
|
|
findKeepUniqueSections<ELFT>(Args);
|
2016-05-03 03:30:42 +08:00
|
|
|
doIcf<ELFT>();
|
2018-05-15 16:57:21 +08:00
|
|
|
}
|
2016-05-24 00:55:43 +08:00
|
|
|
|
2018-04-18 07:30:05 +08:00
|
|
|
// Read the callgraph now that we know what was gced or icfed
|
2018-10-26 07:15:23 +08:00
|
|
|
if (Config->CallGraphProfileSort) {
|
|
|
|
if (auto *Arg = Args.getLastArg(OPT_call_graph_ordering_file))
|
|
|
|
if (Optional<MemoryBufferRef> Buffer = readFile(Arg->getValue()))
|
|
|
|
readCallGraph(*Buffer);
|
|
|
|
readCallGraphsFromObjectFiles<ELFT>();
|
|
|
|
}
|
2018-04-18 07:30:05 +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
|
|
|
}
|