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"
|
2019-09-17 02:49:57 +08:00
|
|
|
#include "llvm/LTO/LTO.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"
|
2019-06-14 22:00:59 +08:00
|
|
|
#include "llvm/Support/GlobPattern.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;
|
2019-08-07 16:08:17 +08:00
|
|
|
enableColors(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-05-29 11:55:20 +08:00
|
|
|
partitions = {Partition()};
|
|
|
|
|
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");
|
[Coding style change] Rename variables so that they start with a lowercase letter
This patch is mechanically generated by clang-llvm-rename tool that I wrote
using Clang Refactoring Engine just for creating this patch. You can see the
source code of the tool at https://reviews.llvm.org/D64123. There's no manual
post-processing; you can generate the same patch by re-running the tool against
lld's code base.
Here is the main discussion thread to change the LLVM coding style:
https://lists.llvm.org/pipermail/llvm-dev/2019-February/130083.html
In the discussion thread, I proposed we use lld as a testbed for variable
naming scheme change, and this patch does that.
I chose to rename variables so that they are in camelCase, just because that
is a minimal change to make variables to start with a lowercase letter.
Note to downstream patch maintainers: if you are maintaining a downstream lld
repo, just rebasing ahead of this commit would cause massive merge conflicts
because this patch essentially changes every line in the lld subdirectory. But
there's a remedy.
clang-llvm-rename tool is a batch tool, so you can rename variables in your
downstream repo with the tool. Given that, here is how to rebase your repo to
a commit after the mass renaming:
1. rebase to the commit just before the mass variable renaming,
2. apply the tool to your downstream repo to mass-rename variables locally, and
3. rebase again to the head.
Most changes made by the tool should be identical for a downstream repo and
for the head, so at the step 3, almost all changes should be merged and
disappear. I'd expect that there would be some lines that you need to merge by
hand, but that shouldn't be too many.
Differential Revision: https://reviews.llvm.org/D64121
llvm-svn: 365595
2019-07-10 13:00:37 +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))
|
2019-07-03 10:29:02 +08:00
|
|
|
if (identify_magic(p.first.getBuffer()) != file_magic::bitcode) {
|
|
|
|
error(path + ": archive has no index; run ranlib to add one");
|
2019-03-15 02:21:32 +08:00
|
|
|
return;
|
2019-07-03 10:29:02 +08:00
|
|
|
}
|
2019-03-15 02:21:32 +08:00
|
|
|
|
|
|
|
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->isStatic || 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(
|
2019-05-27 15:26:13 +08:00
|
|
|
make<SharedFile>(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))
|
2019-07-16 12:46:31 +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
|
|
|
|
2019-09-16 17:38:38 +08:00
|
|
|
if (config->fixCortexA8 && config->emachine != EM_ARM)
|
|
|
|
error("--fix-cortex-a8 is only supported on ARM targets");
|
|
|
|
|
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-08-26 14:23:53 +08:00
|
|
|
if (config->strip == StripPolicy::All && config->emitRelocs)
|
|
|
|
error("--strip-all and --emit-relocs may not be used together");
|
|
|
|
|
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");
|
|
|
|
}
|
2019-06-05 11:04:46 +08:00
|
|
|
|
|
|
|
if (config->zRetpolineplt && config->requireCET)
|
|
|
|
error("--require-cet may not be used with -z retpolineplt");
|
2019-06-07 21:00:17 +08:00
|
|
|
|
|
|
|
if (config->emachine != EM_AARCH64) {
|
|
|
|
if (config->pacPlt)
|
|
|
|
error("--pac-plt only supported on AArch64");
|
|
|
|
if (config->forceBTI)
|
|
|
|
error("--force-bti only supported on AArch64");
|
|
|
|
}
|
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" ||
|
[ELF] Add -z separate-code and pad the last page of last PF_X PT_LOAD with traps only if -z separate-code is specified
This patch
1) adds -z separate-code and -z noseparate-code (default).
2) changes the condition that the last page of last PF_X PT_LOAD is
padded with trap instructions.
Current condition (after D33630): if there is no `SECTIONS` commands.
After this change: if -z separate-code is specified.
-z separate-code was introduced to ld.bfd in 2018, to place the text
segment in its own pages. There is no overlap in pages between an
executable segment and a non-executable segment:
1) RX cannot load initial contents from R or RW(or non-SHF_ALLOC).
2) R and RW(or non-SHF_ALLOC) cannot load initial contents from RX.
lld's current status:
- Between R and RX: in `Writer<ELFT>::fixSectionAlignments()`, the start of a
segment is always aligned to maxPageSize, so the initial contents loaded by R
and RX do not overlap. I plan to allow overlaps in D64906 if -z noseparate-code
is in effect.
- Between RX and RW(or non-SHF_ALLOC if RW doesn't exist):
we currently unconditionally pad the last page to commonPageSize
(defaults to 4096 on all targets we support).
This patch will make it effective only if -z separate-code is specified.
-z separate-code is a dubious feature that intends to reduce the number
of ROP gadgets (which is actually ineffective because attackers can find
plenty of gadgets in the text segment, no need to find gadgets in
non-code regions).
With the overlapping PT_LOAD technique D64906, -z noseparate-code
removes two more alignments at segment boundaries than -z separate-code.
This saves at most defaultCommonPageSize*2 bytes, which are significant
on targets with large defaultCommonPageSize (AArch64/MIPS/PPC: 65536).
Issues/feedback on alignment at segment boundaries to help understand
the implication:
* binutils PR24490 (the situation on ld.bfd is worse because they have
two R-- on both sides of R-E so more alignments.)
* In binutils, the 2018-02-27 commit "ld: Add --enable-separate-code" made -z separate-code the default on Linux.
https://github.com/richfelker/musl-cross-make/commit/d969dea983a2cc54a1e0308a0cdeb6c3307e4bfa
In musl-cross-make, binutils is configured with --disable-separate-code
to address size regressions caused by -z separate-code. (lld actually has the same
issue, which I plan to fix in a future patch. The ld.bfd x86 status is
worse because they default to max-page-size=0x200000).
* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=237676 people want
smaller code size. This patch will remove one alignment boundary.
* Stef O'Rear: I'm opposed to any kind of page alignment at the
text/rodata line (having a partial page of text aliased as rodata and
vice versa has no demonstrable harm, and I actually care about small
systems).
So, make -z noseparate-code the default.
Reviewed By: ruiu
Differential Revision: https://reviews.llvm.org/D64903
llvm-svn: 367537
2019-08-01 17:58:25 +08:00
|
|
|
s == "separate-code" || s == "nocombreloc" || s == "nocopyreloc" ||
|
|
|
|
s == "nodefaultlib" || s == "nodelete" || s == "nodlopen" ||
|
|
|
|
s == "noexecstack" || s == "nokeep-text-section-prefix" ||
|
|
|
|
s == "norelro" || s == "noseparate-code" || s == "notext" ||
|
2018-06-27 15:56:23 +08:00
|
|
|
s == "now" || s == "origin" || s == "relro" || s == "retpolineplt" ||
|
2019-09-12 16:55:17 +08:00
|
|
|
s == "rodynamic" || s == "text" || s == "undefs" || s == "wxneeded" ||
|
2019-09-12 16:59:17 +08:00
|
|
|
s.startswith("common-page-size=") || s.startswith("max-page-size=") ||
|
2019-05-14 00:01:26 +08:00
|
|
|
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;
|
2019-09-12 16:55:17 +08:00
|
|
|
if (StringRef(arg->getValue()) == "undefs")
|
|
|
|
return UnresolvedPolicy::Ignore;
|
2017-01-29 09:59:11 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
Let unaliased Args track which Alias they were created from, and use that in Arg::getAsString() for diagnostics
With this, `clang-cl /source-charset:utf-16 test.cc` now prints `invalid
value 'utf-16' in '/source-charset:utf-16'` instead of `invalid value
'utf-16' in '-finput-charset=utf-16'` before, and several other clang-cl
flags produce much less confusing output as well.
Fixes PR29106.
Since an arg and its alias can have different arg types (joined vs not)
and different values (because of AliasArgs<>), I chose to give the Alias
its own Arg object. For convenience, I just store the alias directly in
the unaliased arg – there aren't many arg objects at runtime, so that
seems ok.
Finally, I changed Arg::getAsString() to use the alias's representation
if it's present – that function was already documented as being the
suitable function for diagnostics, and most callers already used it for
diagnostics.
Implementation-wise, Arg::accept() previously used to parse things as
the unaliased option. The core of that switch is now extracted into a
new function acceptInternal() which parses as the _aliased_ option, and
the previously-intermingled unaliasing is now done as an explicit step
afterwards.
(This also changes one place in lld that didn't use getAsString() for
diagnostics, so that that one place now also prints the flag as the user
wrote it, not as it looks after it went through unaliasing.)
Differential Revision: https://reviews.llvm.org/D64253
llvm-svn: 365413
2019-07-09 08:34:08 +08:00
|
|
|
static uint64_t parseSectionAddress(StringRef s, opt::InputArgList &args,
|
|
|
|
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))
|
Let unaliased Args track which Alias they were created from, and use that in Arg::getAsString() for diagnostics
With this, `clang-cl /source-charset:utf-16 test.cc` now prints `invalid
value 'utf-16' in '/source-charset:utf-16'` instead of `invalid value
'utf-16' in '-finput-charset=utf-16'` before, and several other clang-cl
flags produce much less confusing output as well.
Fixes PR29106.
Since an arg and its alias can have different arg types (joined vs not)
and different values (because of AliasArgs<>), I chose to give the Alias
its own Arg object. For convenience, I just store the alias directly in
the unaliased arg – there aren't many arg objects at runtime, so that
seems ok.
Finally, I changed Arg::getAsString() to use the alias's representation
if it's present – that function was already documented as being the
suitable function for diagnostics, and most callers already used it for
diagnostics.
Implementation-wise, Arg::accept() previously used to parse things as
the unaliased option. The core of that switch is now extracted into a
new function acceptInternal() which parses as the _aliased_ option, and
the previously-intermingled unaliasing is now done as an explicit step
afterwards.
(This also changes one place in lld that didn't use getAsString() for
diagnostics, so that that one place now also prints the flag as the user
wrote it, not as it looks after it went through unaliasing.)
Differential Revision: https://reviews.llvm.org/D64253
llvm-svn: 365413
2019-07-09 08:34:08 +08:00
|
|
|
error("invalid argument: " + arg.getAsString(args));
|
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('=');
|
Let unaliased Args track which Alias they were created from, and use that in Arg::getAsString() for diagnostics
With this, `clang-cl /source-charset:utf-16 test.cc` now prints `invalid
value 'utf-16' in '/source-charset:utf-16'` instead of `invalid value
'utf-16' in '-finput-charset=utf-16'` before, and several other clang-cl
flags produce much less confusing output as well.
Fixes PR29106.
Since an arg and its alias can have different arg types (joined vs not)
and different values (because of AliasArgs<>), I chose to give the Alias
its own Arg object. For convenience, I just store the alias directly in
the unaliased arg – there aren't many arg objects at runtime, so that
seems ok.
Finally, I changed Arg::getAsString() to use the alias's representation
if it's present – that function was already documented as being the
suitable function for diagnostics, and most callers already used it for
diagnostics.
Implementation-wise, Arg::accept() previously used to parse things as
the unaliased option. The core of that switch is now extracted into a
new function acceptInternal() which parses as the _aliased_ option, and
the previously-intermingled unaliasing is now done as an explicit step
afterwards.
(This also changes one place in lld that didn't use getAsString() for
diagnostics, so that that one place now also prints the flag as the user
wrote it, not as it looks after it went through unaliasing.)
Differential Revision: https://reviews.llvm.org/D64253
llvm-svn: 365413
2019-07-09 08:34:08 +08:00
|
|
|
ret[name] = parseSectionAddress(addr, args, *arg);
|
2016-09-14 21:07:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (auto *arg = args.getLastArg(OPT_Ttext))
|
Let unaliased Args track which Alias they were created from, and use that in Arg::getAsString() for diagnostics
With this, `clang-cl /source-charset:utf-16 test.cc` now prints `invalid
value 'utf-16' in '/source-charset:utf-16'` instead of `invalid value
'utf-16' in '-finput-charset=utf-16'` before, and several other clang-cl
flags produce much less confusing output as well.
Fixes PR29106.
Since an arg and its alias can have different arg types (joined vs not)
and different values (because of AliasArgs<>), I chose to give the Alias
its own Arg object. For convenience, I just store the alias directly in
the unaliased arg – there aren't many arg objects at runtime, so that
seems ok.
Finally, I changed Arg::getAsString() to use the alias's representation
if it's present – that function was already documented as being the
suitable function for diagnostics, and most callers already used it for
diagnostics.
Implementation-wise, Arg::accept() previously used to parse things as
the unaliased option. The core of that switch is now extracted into a
new function acceptInternal() which parses as the _aliased_ option, and
the previously-intermingled unaliasing is now done as an explicit step
afterwards.
(This also changes one place in lld that didn't use getAsString() for
diagnostics, so that that one place now also prints the flag as the user
wrote it, not as it looks after it went through unaliasing.)
Differential Revision: https://reviews.llvm.org/D64253
llvm-svn: 365413
2019-07-09 08:34:08 +08:00
|
|
|
ret[".text"] = parseSectionAddress(arg->getValue(), args, *arg);
|
2016-09-14 21:07:13 +08:00
|
|
|
if (auto *arg = args.getLastArg(OPT_Tdata))
|
Let unaliased Args track which Alias they were created from, and use that in Arg::getAsString() for diagnostics
With this, `clang-cl /source-charset:utf-16 test.cc` now prints `invalid
value 'utf-16' in '/source-charset:utf-16'` instead of `invalid value
'utf-16' in '-finput-charset=utf-16'` before, and several other clang-cl
flags produce much less confusing output as well.
Fixes PR29106.
Since an arg and its alias can have different arg types (joined vs not)
and different values (because of AliasArgs<>), I chose to give the Alias
its own Arg object. For convenience, I just store the alias directly in
the unaliased arg – there aren't many arg objects at runtime, so that
seems ok.
Finally, I changed Arg::getAsString() to use the alias's representation
if it's present – that function was already documented as being the
suitable function for diagnostics, and most callers already used it for
diagnostics.
Implementation-wise, Arg::accept() previously used to parse things as
the unaliased option. The core of that switch is now extracted into a
new function acceptInternal() which parses as the _aliased_ option, and
the previously-intermingled unaliasing is now done as an explicit step
afterwards.
(This also changes one place in lld that didn't use getAsString() for
diagnostics, so that that one place now also prints the flag as the user
wrote it, not as it looks after it went through unaliasing.)
Differential Revision: https://reviews.llvm.org/D64253
llvm-svn: 365413
2019-07-09 08:34:08 +08:00
|
|
|
ret[".data"] = parseSectionAddress(arg->getValue(), args, *arg);
|
2016-09-14 21:07:13 +08:00
|
|
|
if (auto *arg = args.getLastArg(OPT_Tbss))
|
Let unaliased Args track which Alias they were created from, and use that in Arg::getAsString() for diagnostics
With this, `clang-cl /source-charset:utf-16 test.cc` now prints `invalid
value 'utf-16' in '/source-charset:utf-16'` instead of `invalid value
'utf-16' in '-finput-charset=utf-16'` before, and several other clang-cl
flags produce much less confusing output as well.
Fixes PR29106.
Since an arg and its alias can have different arg types (joined vs not)
and different values (because of AliasArgs<>), I chose to give the Alias
its own Arg object. For convenience, I just store the alias directly in
the unaliased arg – there aren't many arg objects at runtime, so that
seems ok.
Finally, I changed Arg::getAsString() to use the alias's representation
if it's present – that function was already documented as being the
suitable function for diagnostics, and most callers already used it for
diagnostics.
Implementation-wise, Arg::accept() previously used to parse things as
the unaliased option. The core of that switch is now extracted into a
new function acceptInternal() which parses as the _aliased_ option, and
the previously-intermingled unaliasing is now done as an explicit step
afterwards.
(This also changes one place in lld that didn't use getAsString() for
diagnostics, so that that one place now also prints the flag as the user
wrote it, not as it looks after it went through unaliasing.)
Differential Revision: https://reviews.llvm.org/D64253
llvm-svn: 365413
2019-07-09 08:34:08 +08:00
|
|
|
ret[".bss"] = parseSectionAddress(arg->getValue(), args, *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;
|
[Coding style change] Rename variables so that they start with a lowercase letter
This patch is mechanically generated by clang-llvm-rename tool that I wrote
using Clang Refactoring Engine just for creating this patch. You can see the
source code of the tool at https://reviews.llvm.org/D64123. There's no manual
post-processing; you can generate the same patch by re-running the tool against
lld's code base.
Here is the main discussion thread to change the LLVM coding style:
https://lists.llvm.org/pipermail/llvm-dev/2019-February/130083.html
In the discussion thread, I proposed we use lld as a testbed for variable
naming scheme change, and this patch does that.
I chose to rename variables so that they are in camelCase, just because that
is a minimal change to make variables to start with a lowercase letter.
Note to downstream patch maintainers: if you are maintaining a downstream lld
repo, just rebasing ahead of this commit would cause massive merge conflicts
because this patch essentially changes every line in the lld subdirectory. But
there's a remedy.
clang-llvm-rename tool is a batch tool, so you can rename variables in your
downstream repo with the tool. Given that, here is how to rebase your repo to
a commit after the mass renaming:
1. rebase to the commit just before the mass variable renaming,
2. apply the tool to your downstream repo to mass-rename variables locally, and
3. rebase again to the head.
Most changes made by the tool should be identical for a downstream repo and
for the head, so at the step 3, almost all changes should be merged and
disappear. I'd expect that there would be some lines that you need to merge by
hand, but that shouldn't be too many.
Differential Revision: https://reviews.llvm.org/D64121
llvm-svn: 365595
2019-07-10 13:00:37 +08:00
|
|
|
|
2018-10-26 23:07:02 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
[ELF] accept thinlto options without --plugin-opt= prefix
Summary:
When support for ThinLTO was first added to lld, the options that
control it were prefixed with --plugin-opt= for compatibility with
an existing implementation as a linker plugin. This change enables
shorter versions of the options to be used, as follows:
New Existing
-thinlto-emit-imports-files --plugin-opt=thinlto-emit-imports-files
-thinlto-index-only --plugin-opt=thinlto-index-only
-thinlto-index-only= --plugin-opt=thinlto-index-only=
-thinlto-object-suffix-replace= --plugin-opt=thinlto-object-suffix-replace=
-thinlto-prefix-replace= --plugin-opt=thinlto-prefix-replace=
-lto-obj-path= --plugin-opt=obj-path=
The options with the --plugin-opt= prefix have been retained as aliases
for the shorter variants so that they continue to be accepted.
Reviewers: tejohnson, ruiu, espindola
Reviewed By: ruiu
Subscribers: emaste, arichardson, MaskRay, steven_wu, dexonsmith, arphaman, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D67782
llvm-svn: 372798
2019-09-25 09:19:48 +08:00
|
|
|
static StringRef getAliasSpelling(opt::Arg *arg) {
|
|
|
|
if (const opt::Arg *alias = arg->getAlias())
|
|
|
|
return alias->getSpelling();
|
|
|
|
return arg->getSpelling();
|
|
|
|
}
|
|
|
|
|
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())
|
[ELF] accept thinlto options without --plugin-opt= prefix
Summary:
When support for ThinLTO was first added to lld, the options that
control it were prefixed with --plugin-opt= for compatibility with
an existing implementation as a linker plugin. This change enables
shorter versions of the options to be used, as follows:
New Existing
-thinlto-emit-imports-files --plugin-opt=thinlto-emit-imports-files
-thinlto-index-only --plugin-opt=thinlto-index-only
-thinlto-index-only= --plugin-opt=thinlto-index-only=
-thinlto-object-suffix-replace= --plugin-opt=thinlto-object-suffix-replace=
-thinlto-prefix-replace= --plugin-opt=thinlto-prefix-replace=
-lto-obj-path= --plugin-opt=obj-path=
The options with the --plugin-opt= prefix have been retained as aliases
for the shorter variants so that they continue to be accepted.
Reviewers: tejohnson, ruiu, espindola
Reviewed By: ruiu
Subscribers: emaste, arichardson, MaskRay, steven_wu, dexonsmith, arphaman, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D67782
llvm-svn: 372798
2019-09-25 09:19:48 +08:00
|
|
|
error(getAliasSpelling(arg) + " expects 'old;new' format, but got " + s);
|
2018-05-22 10:53:11 +08:00
|
|
|
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);
|
2019-07-17 22:54:02 +08:00
|
|
|
errorHandler().vsDiagnostics =
|
|
|
|
args.hasArg(OPT_visual_studio_diagnostics_format, 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);
|
[ELF] Implement Dependent Libraries Feature
This patch implements a limited form of autolinking primarily designed to allow
either the --dependent-library compiler option, or "comment lib" pragmas (
https://docs.microsoft.com/en-us/cpp/preprocessor/comment-c-cpp?view=vs-2017) in
C/C++ e.g. #pragma comment(lib, "foo"), to cause an ELF linker to automatically
add the specified library to the link when processing the input file generated
by the compiler.
Currently this extension is unique to LLVM and LLD. However, care has been taken
to design this feature so that it could be supported by other ELF linkers.
The design goals were to provide:
- A simple linking model for developers to reason about.
- The ability to to override autolinking from the linker command line.
- Source code compatibility, where possible, with "comment lib" pragmas in other
environments (MSVC in particular).
Dependent library support is implemented differently for ELF platforms than on
the other platforms. Primarily this difference is that on ELF we pass the
dependent library specifiers directly to the linker without manipulating them.
This is in contrast to other platforms where they are mapped to a specific
linker option by the compiler. This difference is a result of the greater
variety of ELF linkers and the fact that ELF linkers tend to handle libraries in
a more complicated fashion than on other platforms. This forces us to defer
handling the specifiers to the linker.
In order to achieve a level of source code compatibility with other platforms
we have restricted this feature to work with libraries that meet the following
"reasonable" requirements:
1. There are no competing defined symbols in a given set of libraries, or
if they exist, the program owner doesn't care which is linked to their
program.
2. There may be circular dependencies between libraries.
The binary representation is a mergeable string section (SHF_MERGE,
SHF_STRINGS), called .deplibs, with custom type SHT_LLVM_DEPENDENT_LIBRARIES
(0x6fff4c04). The compiler forms this section by concatenating the arguments of
the "comment lib" pragmas and --dependent-library options in the order they are
encountered. Partial (-r, -Ur) links are handled by concatenating .deplibs
sections with the normal mergeable string section rules. As an example, #pragma
comment(lib, "foo") would result in:
.section ".deplibs","MS",@llvm_dependent_libraries,1
.asciz "foo"
For LTO, equivalent information to the contents of a the .deplibs section can be
retrieved by the LLD for bitcode input files.
LLD processes the dependent library specifiers in the following way:
1. Dependent libraries which are found from the specifiers in .deplibs sections
of relocatable object files are added when the linker decides to include that
file (which could itself be in a library) in the link. Dependent libraries
behave as if they were appended to the command line after all other options. As
a consequence the set of dependent libraries are searched last to resolve
symbols.
2. It is an error if a file cannot be found for a given specifier.
3. Any command line options in effect at the end of the command line parsing apply
to the dependent libraries, e.g. --whole-archive.
4. The linker tries to add a library or relocatable object file from each of the
strings in a .deplibs section by; first, handling the string as if it was
specified on the command line; second, by looking for the string in each of the
library search paths in turn; third, by looking for a lib<string>.a or
lib<string>.so (depending on the current mode of the linker) in each of the
library search paths.
5. A new command line option --no-dependent-libraries tells LLD to ignore the
dependent libraries.
Rationale for the above points:
1. Adding the dependent libraries last makes the process simple to understand
from a developers perspective. All linkers are able to implement this scheme.
2. Error-ing for libraries that are not found seems like better behavior than
failing the link during symbol resolution.
3. It seems useful for the user to be able to apply command line options which
will affect all of the dependent libraries. There is a potential problem of
surprise for developers, who might not realize that these options would apply
to these "invisible" input files; however, despite the potential for surprise,
this is easy for developers to reason about and gives developers the control
that they may require.
4. This algorithm takes into account all of the different ways that ELF linkers
find input files. The different search methods are tried by the linker in most
obvious to least obvious order.
5. I considered adding finer grained control over which dependent libraries were
ignored (e.g. MSVC has /nodefaultlib:<library>); however, I concluded that this
is not necessary: if finer control is required developers can fall back to using
the command line directly.
RFC thread: http://lists.llvm.org/pipermail/llvm-dev/2019-March/131004.html.
Differential Revision: https://reviews.llvm.org/D60274
llvm-svn: 360984
2019-05-17 11:44:15 +08:00
|
|
|
config->dependentLibraries = args.hasFlag(OPT_dependent_libraries, OPT_no_dependent_libraries, 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);
|
2019-09-16 17:38:38 +08:00
|
|
|
config->fixCortexA8 = args.hasArg(OPT_fix_cortex_a8);
|
2019-06-07 21:00:17 +08:00
|
|
|
config->forceBTI = args.hasArg(OPT_force_bti);
|
2019-06-05 11:04:46 +08:00
|
|
|
config->requireCET = args.hasArg(OPT_require_cet);
|
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);
|
[ELF] accept thinlto options without --plugin-opt= prefix
Summary:
When support for ThinLTO was first added to lld, the options that
control it were prefixed with --plugin-opt= for compatibility with
an existing implementation as a linker plugin. This change enables
shorter versions of the options to be used, as follows:
New Existing
-thinlto-emit-imports-files --plugin-opt=thinlto-emit-imports-files
-thinlto-index-only --plugin-opt=thinlto-index-only
-thinlto-index-only= --plugin-opt=thinlto-index-only=
-thinlto-object-suffix-replace= --plugin-opt=thinlto-object-suffix-replace=
-thinlto-prefix-replace= --plugin-opt=thinlto-prefix-replace=
-lto-obj-path= --plugin-opt=obj-path=
The options with the --plugin-opt= prefix have been retained as aliases
for the shorter variants so that they continue to be accepted.
Reviewers: tejohnson, ruiu, espindola
Reviewed By: ruiu
Subscribers: emaste, arichardson, MaskRay, steven_wu, dexonsmith, arphaman, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D67782
llvm-svn: 372798
2019-09-25 09:19:48 +08:00
|
|
|
config->ltoObjPath = args.getLastArgValue(OPT_lto_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);
|
2019-06-18 00:06:00 +08:00
|
|
|
config->optRemarksFormat = args.getLastArgValue(OPT_opt_remarks_format);
|
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);
|
2019-06-07 21:00:17 +08:00
|
|
|
config->pacPlt = args.hasArg(OPT_pac_plt);
|
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");
|
[ELF] accept thinlto options without --plugin-opt= prefix
Summary:
When support for ThinLTO was first added to lld, the options that
control it were prefixed with --plugin-opt= for compatibility with
an existing implementation as a linker plugin. This change enables
shorter versions of the options to be used, as follows:
New Existing
-thinlto-emit-imports-files --plugin-opt=thinlto-emit-imports-files
-thinlto-index-only --plugin-opt=thinlto-index-only
-thinlto-index-only= --plugin-opt=thinlto-index-only=
-thinlto-object-suffix-replace= --plugin-opt=thinlto-object-suffix-replace=
-thinlto-prefix-replace= --plugin-opt=thinlto-prefix-replace=
-lto-obj-path= --plugin-opt=obj-path=
The options with the --plugin-opt= prefix have been retained as aliases
for the shorter variants so that they continue to be accepted.
Reviewers: tejohnson, ruiu, espindola
Reviewed By: ruiu
Subscribers: emaste, arichardson, MaskRay, steven_wu, dexonsmith, arphaman, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D67782
llvm-svn: 372798
2019-09-25 09:19:48 +08:00
|
|
|
config->thinLTOEmitImportsFiles = args.hasArg(OPT_thinlto_emit_imports_files);
|
|
|
|
config->thinLTOIndexOnly = args.hasArg(OPT_thinlto_index_only) ||
|
|
|
|
args.hasArg(OPT_thinlto_index_only_eq);
|
|
|
|
config->thinLTOIndexOnlyArg = args.getLastArgValue(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 =
|
[ELF] accept thinlto options without --plugin-opt= prefix
Summary:
When support for ThinLTO was first added to lld, the options that
control it were prefixed with --plugin-opt= for compatibility with
an existing implementation as a linker plugin. This change enables
shorter versions of the options to be used, as follows:
New Existing
-thinlto-emit-imports-files --plugin-opt=thinlto-emit-imports-files
-thinlto-index-only --plugin-opt=thinlto-index-only
-thinlto-index-only= --plugin-opt=thinlto-index-only=
-thinlto-object-suffix-replace= --plugin-opt=thinlto-object-suffix-replace=
-thinlto-prefix-replace= --plugin-opt=thinlto-prefix-replace=
-lto-obj-path= --plugin-opt=obj-path=
The options with the --plugin-opt= prefix have been retained as aliases
for the shorter variants so that they continue to be accepted.
Reviewers: tejohnson, ruiu, espindola
Reviewed By: ruiu
Subscribers: emaste, arichardson, MaskRay, steven_wu, dexonsmith, arphaman, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D67782
llvm-svn: 372798
2019-09-25 09:19:48 +08:00
|
|
|
getOldNewOptions(args, OPT_thinlto_object_suffix_replace_eq);
|
2018-05-23 00:16:09 +08:00
|
|
|
config->thinLTOPrefixReplace =
|
[ELF] accept thinlto options without --plugin-opt= prefix
Summary:
When support for ThinLTO was first added to lld, the options that
control it were prefixed with --plugin-opt= for compatibility with
an existing implementation as a linker plugin. This change enables
shorter versions of the options to be used, as follows:
New Existing
-thinlto-emit-imports-files --plugin-opt=thinlto-emit-imports-files
-thinlto-index-only --plugin-opt=thinlto-index-only
-thinlto-index-only= --plugin-opt=thinlto-index-only=
-thinlto-object-suffix-replace= --plugin-opt=thinlto-object-suffix-replace=
-thinlto-prefix-replace= --plugin-opt=thinlto-prefix-replace=
-lto-obj-path= --plugin-opt=obj-path=
The options with the --plugin-opt= prefix have been retained as aliases
for the shorter variants so that they continue to be accepted.
Reviewers: tejohnson, ruiu, espindola
Reviewed By: ruiu
Subscribers: emaste, arichardson, MaskRay, steven_wu, dexonsmith, arphaman, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D67782
llvm-svn: 372798
2019-09-25 09:19:48 +08:00
|
|
|
getOldNewOptions(args, 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");
|
[ELF] Add -z separate-code and pad the last page of last PF_X PT_LOAD with traps only if -z separate-code is specified
This patch
1) adds -z separate-code and -z noseparate-code (default).
2) changes the condition that the last page of last PF_X PT_LOAD is
padded with trap instructions.
Current condition (after D33630): if there is no `SECTIONS` commands.
After this change: if -z separate-code is specified.
-z separate-code was introduced to ld.bfd in 2018, to place the text
segment in its own pages. There is no overlap in pages between an
executable segment and a non-executable segment:
1) RX cannot load initial contents from R or RW(or non-SHF_ALLOC).
2) R and RW(or non-SHF_ALLOC) cannot load initial contents from RX.
lld's current status:
- Between R and RX: in `Writer<ELFT>::fixSectionAlignments()`, the start of a
segment is always aligned to maxPageSize, so the initial contents loaded by R
and RX do not overlap. I plan to allow overlaps in D64906 if -z noseparate-code
is in effect.
- Between RX and RW(or non-SHF_ALLOC if RW doesn't exist):
we currently unconditionally pad the last page to commonPageSize
(defaults to 4096 on all targets we support).
This patch will make it effective only if -z separate-code is specified.
-z separate-code is a dubious feature that intends to reduce the number
of ROP gadgets (which is actually ineffective because attackers can find
plenty of gadgets in the text segment, no need to find gadgets in
non-code regions).
With the overlapping PT_LOAD technique D64906, -z noseparate-code
removes two more alignments at segment boundaries than -z separate-code.
This saves at most defaultCommonPageSize*2 bytes, which are significant
on targets with large defaultCommonPageSize (AArch64/MIPS/PPC: 65536).
Issues/feedback on alignment at segment boundaries to help understand
the implication:
* binutils PR24490 (the situation on ld.bfd is worse because they have
two R-- on both sides of R-E so more alignments.)
* In binutils, the 2018-02-27 commit "ld: Add --enable-separate-code" made -z separate-code the default on Linux.
https://github.com/richfelker/musl-cross-make/commit/d969dea983a2cc54a1e0308a0cdeb6c3307e4bfa
In musl-cross-make, binutils is configured with --disable-separate-code
to address size regressions caused by -z separate-code. (lld actually has the same
issue, which I plan to fix in a future patch. The ld.bfd x86 status is
worse because they default to max-page-size=0x200000).
* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=237676 people want
smaller code size. This patch will remove one alignment boundary.
* Stef O'Rear: I'm opposed to any kind of page alignment at the
text/rodata line (having a partial page of text aliased as rodata and
vice versa has no demonstrable harm, and I actually care about small
systems).
So, make -z noseparate-code the default.
Reviewed By: ruiu
Differential Revision: https://reviews.llvm.org/D64903
llvm-svn: 367537
2019-08-01 17:58:25 +08:00
|
|
|
config->zSeparateCode = getZFlag(args, "separate-code", "noseparate-code", false);
|
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);
|
2019-09-24 04:32:43 +08:00
|
|
|
config->mipsN32Abi =
|
|
|
|
(s.startswith("elf32btsmipn32") || s.startswith("elf32ltsmipn32"));
|
2017-02-25 09:51:25 +08:00
|
|
|
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
|
|
|
|
2019-05-21 03:13:34 +08:00
|
|
|
if (auto *arg = args.getLastArg(OPT_symbol_ordering_file)){
|
|
|
|
if (args.hasArg(OPT_call_graph_ordering_file))
|
|
|
|
error("--symbol-ordering-file and --call-graph-order-file "
|
|
|
|
"may not be used together");
|
|
|
|
if (Optional<MemoryBufferRef> buffer = readFile(arg->getValue())){
|
2018-02-14 21:36:22 +08:00
|
|
|
config->symbolOrderingFile = getSymbolOrderingFile(*buffer);
|
2019-05-21 03:13:34 +08:00
|
|
|
// Also need to disable CallGraphProfileSort to prevent
|
|
|
|
// LLD order symbols with CGProfile
|
|
|
|
config->callGraphProfileSort = false;
|
|
|
|
}
|
|
|
|
}
|
2016-11-10 17:05:20 +08:00
|
|
|
|
2019-08-05 22:31:39 +08:00
|
|
|
assert(config->versionDefinitions.empty());
|
|
|
|
config->versionDefinitions.push_back({"local", (uint16_t)VER_NDX_LOCAL, {}});
|
|
|
|
config->versionDefinitions.push_back(
|
|
|
|
{"global", (uint16_t)VER_NDX_GLOBAL, {}});
|
|
|
|
|
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)) {
|
2019-08-05 22:31:39 +08:00
|
|
|
config->versionDefinitions[VER_NDX_LOCAL].patterns.push_back(
|
|
|
|
{"*", /*isExternCpp=*/false, /*hasWildcard=*/true});
|
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))
|
2019-08-05 22:31:39 +08:00
|
|
|
config->versionDefinitions[VER_NDX_GLOBAL].patterns.push_back(
|
|
|
|
{s, /*isExternCpp=*/false, /*hasWildcard=*/false});
|
2016-12-20 02:00:52 +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.
|
2019-08-17 18:04:18 +08:00
|
|
|
if (!config->exportDynamic) {
|
2017-04-26 01:40:12 +08:00
|
|
|
for (auto *arg : args.filtered(OPT_dynamic_list))
|
|
|
|
if (Optional<MemoryBufferRef> buffer = readFile(arg->getValue()))
|
|
|
|
readDynamicList(*buffer);
|
[Coding style change] Rename variables so that they start with a lowercase letter
This patch is mechanically generated by clang-llvm-rename tool that I wrote
using Clang Refactoring Engine just for creating this patch. You can see the
source code of the tool at https://reviews.llvm.org/D64123. There's no manual
post-processing; you can generate the same patch by re-running the tool against
lld's code base.
Here is the main discussion thread to change the LLVM coding style:
https://lists.llvm.org/pipermail/llvm-dev/2019-February/130083.html
In the discussion thread, I proposed we use lld as a testbed for variable
naming scheme change, and this patch does that.
I chose to rename variables so that they are in camelCase, just because that
is a minimal change to make variables to start with a lowercase letter.
Note to downstream patch maintainers: if you are maintaining a downstream lld
repo, just rebasing ahead of this commit would cause massive merge conflicts
because this patch essentially changes every line in the lld subdirectory. But
there's a remedy.
clang-llvm-rename tool is a batch tool, so you can rename variables in your
downstream repo with the tool. Given that, here is how to rebase your repo to
a commit after the mass renaming:
1. rebase to the commit just before the mass variable renaming,
2. apply the tool to your downstream repo to mass-rename variables locally, and
3. rebase again to the head.
Most changes made by the tool should be identical for a downstream repo and
for the head, so at the step 3, almost all changes should be merged and
disappear. I'd expect that there would be some lines that you need to merge by
hand, but that shouldn't be too many.
Differential Revision: https://reviews.llvm.org/D64121
llvm-svn: 365595
2019-07-10 13:00:37 +08:00
|
|
|
|
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(
|
2019-08-17 18:04:18 +08:00
|
|
|
{arg->getValue(), /*isExternCpp=*/false, /*hasWildcard=*/false});
|
2017-04-26 01:40:12 +08:00
|
|
|
}
|
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;
|
[Coding style change] Rename variables so that they start with a lowercase letter
This patch is mechanically generated by clang-llvm-rename tool that I wrote
using Clang Refactoring Engine just for creating this patch. You can see the
source code of the tool at https://reviews.llvm.org/D64123. There's no manual
post-processing; you can generate the same patch by re-running the tool against
lld's code base.
Here is the main discussion thread to change the LLVM coding style:
https://lists.llvm.org/pipermail/llvm-dev/2019-February/130083.html
In the discussion thread, I proposed we use lld as a testbed for variable
naming scheme change, and this patch does that.
I chose to rename variables so that they are in camelCase, just because that
is a minimal change to make variables to start with a lowercase letter.
Note to downstream patch maintainers: if you are maintaining a downstream lld
repo, just rebasing ahead of this commit would cause massive merge conflicts
because this patch essentially changes every line in the lld subdirectory. But
there's a remedy.
clang-llvm-rename tool is a batch tool, so you can rename variables in your
downstream repo with the tool. Given that, here is how to rebase your repo to
a commit after the mass renaming:
1. rebase to the commit just before the mass variable renaming,
2. apply the tool to your downstream repo to mass-rename variables locally, and
3. rebase again to the head.
Most changes made by the tool should be identical for a downstream repo and
for the head, so at the step 3, almost all changes should be merged and
disappear. I'd expect that there would be some lines that you need to merge by
hand, but that shouldn't be too many.
Differential Revision: https://reviews.llvm.org/D64121
llvm-svn: 365595
2019-07-10 13:00:37 +08:00
|
|
|
|
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->isPic = config->pie || config->shared;
|
2019-01-16 20:09:13 +08:00
|
|
|
config->picThunk = args.hasArg(OPT_pic_veneer, config->isPic);
|
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) {
|
Make joined instances of JoinedOrSeparate flags point to the unaliased args, like all other arg types do
This fixes an 8-year-old regression. r105763 made it so that aliases
always refer to the unaliased option – but it missed the "joined" branch
of JoinedOrSeparate flags. (r162231 then made the Args classes
non-virtual, and r169344 moved them from clang to llvm.)
Back then, there was no JoinedOrSeparate flag that was an alias, so it
wasn't observable. Now /U in CLCompatOptions is a JoinedOrSeparate alias
in clang, and warn_slash_u_filename incorrectly used the aliased arg id
(using the unaliased one isn't really a regression since that warning
checks if the undefined macro contains slash or backslash and only then
emits the warning – and no valid use will pass "-Ufoo/bar" or similar).
Also, lld has many JoinedOrSeparate aliases, and due to this bug it had
to explicitly call `getUnaliasedOption()` in a bunch of places, even
though that shouldn't be necessary by design. After this fix in Option,
these calls really don't have an effect any more, so remove them.
No intended behavior change.
(I accidentally fixed this bug while working on PR29106 but then
wondered why the warn_slash_u_filename broke. When I figured it out, I
thought it would make sense to land this in a separate commit.)
Differential Revision: https://reviews.llvm.org/D64156
llvm-svn: 365186
2019-07-05 19:45:24 +08:00
|
|
|
switch (arg->getOption().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:
|
2019-07-16 12:46:31 +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->isStatic = true;
|
|
|
|
break;
|
2015-10-02 00:42:03 +08:00
|
|
|
case OPT_Bdynamic:
|
2018-04-04 16:13:28 +08:00
|
|
|
config->isStatic = 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->isStatic, 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->isStatic, 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;
|
|
|
|
}
|
2019-07-16 13:50:45 +08:00
|
|
|
// commonPageSize can't be larger than maxPageSize.
|
2019-05-14 00:01:26 +08:00
|
|
|
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) {
|
2019-07-16 13:50:45 +08:00
|
|
|
// Because we are using "Config->maxPageSize" here, this function has to be
|
2017-10-10 18:09:35 +08:00
|
|
|
// 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");
|
[Coding style change] Rename variables so that they start with a lowercase letter
This patch is mechanically generated by clang-llvm-rename tool that I wrote
using Clang Refactoring Engine just for creating this patch. You can see the
source code of the tool at https://reviews.llvm.org/D64123. There's no manual
post-processing; you can generate the same patch by re-running the tool against
lld's code base.
Here is the main discussion thread to change the LLVM coding style:
https://lists.llvm.org/pipermail/llvm-dev/2019-February/130083.html
In the discussion thread, I proposed we use lld as a testbed for variable
naming scheme change, and this patch does that.
I chose to rename variables so that they are in camelCase, just because that
is a minimal change to make variables to start with a lowercase letter.
Note to downstream patch maintainers: if you are maintaining a downstream lld
repo, just rebasing ahead of this commit would cause massive merge conflicts
because this patch essentially changes every line in the lld subdirectory. But
there's a remedy.
clang-llvm-rename tool is a batch tool, so you can rename variables in your
downstream repo with the tool. Given that, here is how to rebase your repo to
a commit after the mass renaming:
1. rebase to the commit just before the mass variable renaming,
2. apply the tool to your downstream repo to mass-rename variables locally, and
3. rebase again to the head.
Most changes made by the tool should be identical for a downstream repo and
for the head, so at the step 3, almost all changes should be merged and
disappear. I'd expect that there would be some lines that you need to merge by
hand, but that shouldn't be too many.
Differential Revision: https://reviews.llvm.org/D64121
llvm-svn: 365595
2019-07-10 13:00:37 +08:00
|
|
|
|
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.
|
2019-06-14 22:00:59 +08:00
|
|
|
static void handleUndefined(Symbol *sym) {
|
|
|
|
// Since a symbol may not be used inside the program, LTO may
|
2018-04-04 02:01:18 +08:00
|
|
|
// eliminate it. Mark the symbol as "used" to prevent it.
|
|
|
|
sym->isUsedInRegularObj = true;
|
|
|
|
|
|
|
|
if (sym->isLazy())
|
2019-05-23 17:58:08 +08:00
|
|
|
sym->fetch();
|
2018-04-04 02:01:18 +08:00
|
|
|
}
|
|
|
|
|
2019-06-14 22:00:59 +08:00
|
|
|
// As an extention to GNU linkers, lld supports a variant of `-u`
|
|
|
|
// which accepts wildcard patterns. All symbols that match a given
|
|
|
|
// pattern are handled as if they were given by `-u`.
|
|
|
|
static void handleUndefinedGlob(StringRef arg) {
|
|
|
|
Expected<GlobPattern> pat = GlobPattern::create(arg);
|
|
|
|
if (!pat) {
|
|
|
|
error("--undefined-glob: " + toString(pat.takeError()));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<Symbol *> syms;
|
|
|
|
symtab->forEachSymbol([&](Symbol *sym) {
|
|
|
|
// Calling Sym->fetch() from here is not safe because it may
|
|
|
|
// add new symbols to the symbol table, invalidating the
|
|
|
|
// current iterator. So we just keep a note.
|
|
|
|
if (pat->match(sym->getName()))
|
|
|
|
syms.push_back(sym);
|
|
|
|
});
|
|
|
|
|
|
|
|
for (Symbol *sym : syms)
|
|
|
|
handleUndefined(sym);
|
|
|
|
}
|
|
|
|
|
2019-05-16 11:45:13 +08:00
|
|
|
static void handleLibcall(StringRef name) {
|
2018-08-09 07:48:12 +08:00
|
|
|
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))
|
2019-05-23 17:58:08 +08:00
|
|
|
sym->fetch();
|
2018-08-09 07:48:12 +08:00
|
|
|
}
|
|
|
|
|
2019-05-16 11:29:03 +08:00
|
|
|
// Replaces common symbols with defined symbols reside in .bss sections.
|
|
|
|
// This function is called after all symbol names are resolved. As a
|
|
|
|
// result, the passes after the symbol resolution won't see any
|
|
|
|
// symbols of type CommonSymbol.
|
|
|
|
static void replaceCommonSymbols() {
|
2019-05-28 14:33:06 +08:00
|
|
|
symtab->forEachSymbol([](Symbol *sym) {
|
2019-05-16 11:29:03 +08:00
|
|
|
auto *s = dyn_cast<CommonSymbol>(sym);
|
|
|
|
if (!s)
|
2019-05-28 14:33:06 +08:00
|
|
|
return;
|
2019-05-16 11:29:03 +08:00
|
|
|
|
|
|
|
auto *bss = make<BssSection>("COMMON", s->size, s->alignment);
|
|
|
|
bss->file = s->file;
|
2019-05-29 11:55:20 +08:00
|
|
|
bss->markDead();
|
2019-05-16 11:29:03 +08:00
|
|
|
inputSections.push_back(bss);
|
2019-05-20 11:36:33 +08:00
|
|
|
s->replace(Defined{s->file, s->getName(), s->binding, s->stOther, s->type,
|
2019-07-16 12:46:31 +08:00
|
|
|
/*value=*/0, s->size, bss});
|
2019-05-28 14:33:06 +08:00
|
|
|
});
|
2019-05-16 11:29:03 +08:00
|
|
|
}
|
|
|
|
|
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() {
|
2019-05-28 14:33:06 +08:00
|
|
|
symtab->forEachSymbol([](Symbol *sym) {
|
2019-05-16 10:14:00 +08:00
|
|
|
auto *s = dyn_cast<SharedSymbol>(sym);
|
|
|
|
if (!s || s->getFile().isNeeded)
|
2019-05-28 14:33:06 +08:00
|
|
|
return;
|
2019-05-16 10:14:00 +08:00
|
|
|
|
|
|
|
bool used = s->used;
|
2019-05-20 11:36:33 +08:00
|
|
|
s->replace(Undefined{nullptr, s->getName(), STB_WEAK, s->stOther, s->type});
|
2019-05-16 10:14:00 +08:00
|
|
|
s->used = used;
|
2019-05-28 14:33:06 +08:00
|
|
|
});
|
2018-04-25 08:29:13 +08:00
|
|
|
}
|
|
|
|
|
2019-07-16 13:50:45 +08:00
|
|
|
// The section referred to by `s` is considered address-significant. Set the
|
|
|
|
// keepUnique flag on the section if appropriate.
|
2018-07-21 10:14:59 +08:00
|
|
|
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.
|
2019-05-28 14:33:06 +08:00
|
|
|
symtab->forEachSymbol([&](Symbol *sym) {
|
|
|
|
if (sym->includeInDynsym())
|
|
|
|
markAddrsig(sym);
|
|
|
|
});
|
2018-07-21 10:14:59 +08:00
|
|
|
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2019-05-29 11:55:20 +08:00
|
|
|
// This function reads a symbol partition specification section. These sections
|
|
|
|
// are used to control which partition a symbol is allocated to. See
|
|
|
|
// https://lld.llvm.org/Partitions.html for more details on partitions.
|
|
|
|
template <typename ELFT>
|
|
|
|
static void readSymbolPartitionSection(InputSectionBase *s) {
|
|
|
|
// Read the relocation that refers to the partition's entry point symbol.
|
|
|
|
Symbol *sym;
|
|
|
|
if (s->areRelocsRela)
|
|
|
|
sym = &s->getFile<ELFT>()->getRelocTargetSym(s->template relas<ELFT>()[0]);
|
|
|
|
else
|
|
|
|
sym = &s->getFile<ELFT>()->getRelocTargetSym(s->template rels<ELFT>()[0]);
|
|
|
|
if (!isa<Defined>(sym) || !sym->includeInDynsym())
|
|
|
|
return;
|
|
|
|
|
|
|
|
StringRef partName = reinterpret_cast<const char *>(s->data().data());
|
|
|
|
for (Partition &part : partitions) {
|
|
|
|
if (part.name == partName) {
|
|
|
|
sym->partition = part.getNumber();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Forbid partitions from being used on incompatible targets, and forbid them
|
|
|
|
// from being used together with various linker features that assume a single
|
|
|
|
// set of output sections.
|
|
|
|
if (script->hasSectionsCommand)
|
|
|
|
error(toString(s->file) +
|
|
|
|
": partitions cannot be used with the SECTIONS command");
|
|
|
|
if (script->hasPhdrsCommands())
|
|
|
|
error(toString(s->file) +
|
|
|
|
": partitions cannot be used with the PHDRS command");
|
|
|
|
if (!config->sectionStartMap.empty())
|
|
|
|
error(toString(s->file) + ": partitions cannot be used with "
|
|
|
|
"--section-start, -Ttext, -Tdata or -Tbss");
|
|
|
|
if (config->emachine == EM_MIPS)
|
|
|
|
error(toString(s->file) + ": partitions cannot be used on this target");
|
|
|
|
|
|
|
|
// Impose a limit of no more than 254 partitions. This limit comes from the
|
|
|
|
// sizes of the Partition fields in InputSectionBase and Symbol, as well as
|
|
|
|
// the amount of space devoted to the partition number in RankFlags.
|
|
|
|
if (partitions.size() == 254)
|
|
|
|
fatal("may not have more than 254 partitions");
|
|
|
|
|
|
|
|
partitions.emplace_back();
|
|
|
|
Partition &newPart = partitions.back();
|
|
|
|
newPart.name = partName;
|
|
|
|
sym->partition = newPart.getNumber();
|
|
|
|
}
|
|
|
|
|
2019-05-30 22:50:10 +08:00
|
|
|
static Symbol *addUndefined(StringRef name) {
|
2019-05-17 09:55:20 +08:00
|
|
|
return symtab->addSymbol(
|
2019-05-16 10:14:00 +08:00
|
|
|
Undefined{nullptr, name, STB_GLOBAL, STV_DEFAULT, 0});
|
2018-10-12 04:34:29 +08:00
|
|
|
}
|
|
|
|
|
2019-05-23 17:26:27 +08:00
|
|
|
// This function is where all the optimizations of link-time
|
|
|
|
// optimization takes place. When LTO is in use, some input files are
|
|
|
|
// not in native object file format but in the LLVM bitcode format.
|
|
|
|
// This function compiles bitcode files into a few big native files
|
|
|
|
// using LLVM functions and replaces bitcode symbols with the results.
|
|
|
|
// Because all bitcode files that the program consists of are passed to
|
|
|
|
// the compiler at once, it can do a whole-program optimization.
|
|
|
|
template <class ELFT> void LinkerDriver::compileBitcodeFiles() {
|
|
|
|
// Compile bitcode files and replace bitcode symbols.
|
|
|
|
lto.reset(new BitcodeCompiler);
|
|
|
|
for (BitcodeFile *file : bitcodeFiles)
|
|
|
|
lto->add(*file);
|
[Coding style change] Rename variables so that they start with a lowercase letter
This patch is mechanically generated by clang-llvm-rename tool that I wrote
using Clang Refactoring Engine just for creating this patch. You can see the
source code of the tool at https://reviews.llvm.org/D64123. There's no manual
post-processing; you can generate the same patch by re-running the tool against
lld's code base.
Here is the main discussion thread to change the LLVM coding style:
https://lists.llvm.org/pipermail/llvm-dev/2019-February/130083.html
In the discussion thread, I proposed we use lld as a testbed for variable
naming scheme change, and this patch does that.
I chose to rename variables so that they are in camelCase, just because that
is a minimal change to make variables to start with a lowercase letter.
Note to downstream patch maintainers: if you are maintaining a downstream lld
repo, just rebasing ahead of this commit would cause massive merge conflicts
because this patch essentially changes every line in the lld subdirectory. But
there's a remedy.
clang-llvm-rename tool is a batch tool, so you can rename variables in your
downstream repo with the tool. Given that, here is how to rebase your repo to
a commit after the mass renaming:
1. rebase to the commit just before the mass variable renaming,
2. apply the tool to your downstream repo to mass-rename variables locally, and
3. rebase again to the head.
Most changes made by the tool should be identical for a downstream repo and
for the head, so at the step 3, almost all changes should be merged and
disappear. I'd expect that there would be some lines that you need to merge by
hand, but that shouldn't be too many.
Differential Revision: https://reviews.llvm.org/D64121
llvm-svn: 365595
2019-07-10 13:00:37 +08:00
|
|
|
|
2019-05-23 17:26:27 +08:00
|
|
|
for (InputFile *file : lto->compile()) {
|
|
|
|
auto *obj = cast<ObjFile<ELFT>>(file);
|
2019-07-16 12:46:31 +08:00
|
|
|
obj->parse(/*ignoreComdats=*/true);
|
2019-05-23 17:26:27 +08:00
|
|
|
for (Symbol *sym : obj->getGlobalSymbols())
|
|
|
|
sym->parseSymbolVersion();
|
|
|
|
objectFiles.push_back(file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
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;
|
|
|
|
|
2019-05-30 22:50:10 +08:00
|
|
|
Symbol *real = addUndefined(saver.save("__real_" + name));
|
|
|
|
Symbol *wrap = addUndefined(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) {
|
2019-05-24 22:14:25 +08:00
|
|
|
MutableArrayRef<Symbol *> syms = file->getMutableSymbols();
|
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
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2019-06-05 11:04:46 +08:00
|
|
|
// To enable CET (x86's hardware-assited control flow enforcement), each
|
|
|
|
// source file must be compiled with -fcf-protection. Object files compiled
|
|
|
|
// with the flag contain feature flags indicating that they are compatible
|
|
|
|
// with CET. We enable the feature only when all object files are compatible
|
|
|
|
// with CET.
|
|
|
|
//
|
|
|
|
// This function returns the merged feature flags. If 0, we cannot enable CET.
|
2019-06-07 21:00:17 +08:00
|
|
|
// This is also the case with AARCH64's BTI and PAC which use the similar
|
|
|
|
// GNU_PROPERTY_AARCH64_FEATURE_1_AND mechanism.
|
2019-06-05 11:04:46 +08:00
|
|
|
//
|
|
|
|
// Note that the CET-aware PLT is not implemented yet. We do error
|
|
|
|
// check only.
|
|
|
|
template <class ELFT> static uint32_t getAndFeatures() {
|
2019-06-07 21:00:17 +08:00
|
|
|
if (config->emachine != EM_386 && config->emachine != EM_X86_64 &&
|
|
|
|
config->emachine != EM_AARCH64)
|
2019-06-05 11:04:46 +08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
uint32_t ret = -1;
|
|
|
|
for (InputFile *f : objectFiles) {
|
|
|
|
uint32_t features = cast<ObjFile<ELFT>>(f)->andFeatures;
|
2019-06-07 21:00:17 +08:00
|
|
|
if (config->forceBTI && !(features & GNU_PROPERTY_AARCH64_FEATURE_1_BTI)) {
|
|
|
|
warn(toString(f) + ": --force-bti: file does not have BTI property");
|
|
|
|
features |= GNU_PROPERTY_AARCH64_FEATURE_1_BTI;
|
|
|
|
} else if (!features && config->requireCET)
|
2019-06-05 11:04:46 +08:00
|
|
|
error(toString(f) + ": --require-cet: file is not compatible with CET");
|
|
|
|
ret &= features;
|
|
|
|
}
|
2019-06-07 21:00:17 +08:00
|
|
|
|
|
|
|
// Force enable pointer authentication Plt, we don't warn in this case as
|
|
|
|
// this does not require support in the object for correctness.
|
|
|
|
if (config->pacPlt)
|
|
|
|
ret |= GNU_PROPERTY_AARCH64_FEATURE_1_PAC;
|
|
|
|
|
2019-06-05 11:04:46 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
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))
|
2019-05-28 14:33:06 +08:00
|
|
|
symtab->insert(arg->getValue())->traced = true;
|
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
|
[ELF] Implement Dependent Libraries Feature
This patch implements a limited form of autolinking primarily designed to allow
either the --dependent-library compiler option, or "comment lib" pragmas (
https://docs.microsoft.com/en-us/cpp/preprocessor/comment-c-cpp?view=vs-2017) in
C/C++ e.g. #pragma comment(lib, "foo"), to cause an ELF linker to automatically
add the specified library to the link when processing the input file generated
by the compiler.
Currently this extension is unique to LLVM and LLD. However, care has been taken
to design this feature so that it could be supported by other ELF linkers.
The design goals were to provide:
- A simple linking model for developers to reason about.
- The ability to to override autolinking from the linker command line.
- Source code compatibility, where possible, with "comment lib" pragmas in other
environments (MSVC in particular).
Dependent library support is implemented differently for ELF platforms than on
the other platforms. Primarily this difference is that on ELF we pass the
dependent library specifiers directly to the linker without manipulating them.
This is in contrast to other platforms where they are mapped to a specific
linker option by the compiler. This difference is a result of the greater
variety of ELF linkers and the fact that ELF linkers tend to handle libraries in
a more complicated fashion than on other platforms. This forces us to defer
handling the specifiers to the linker.
In order to achieve a level of source code compatibility with other platforms
we have restricted this feature to work with libraries that meet the following
"reasonable" requirements:
1. There are no competing defined symbols in a given set of libraries, or
if they exist, the program owner doesn't care which is linked to their
program.
2. There may be circular dependencies between libraries.
The binary representation is a mergeable string section (SHF_MERGE,
SHF_STRINGS), called .deplibs, with custom type SHT_LLVM_DEPENDENT_LIBRARIES
(0x6fff4c04). The compiler forms this section by concatenating the arguments of
the "comment lib" pragmas and --dependent-library options in the order they are
encountered. Partial (-r, -Ur) links are handled by concatenating .deplibs
sections with the normal mergeable string section rules. As an example, #pragma
comment(lib, "foo") would result in:
.section ".deplibs","MS",@llvm_dependent_libraries,1
.asciz "foo"
For LTO, equivalent information to the contents of a the .deplibs section can be
retrieved by the LLD for bitcode input files.
LLD processes the dependent library specifiers in the following way:
1. Dependent libraries which are found from the specifiers in .deplibs sections
of relocatable object files are added when the linker decides to include that
file (which could itself be in a library) in the link. Dependent libraries
behave as if they were appended to the command line after all other options. As
a consequence the set of dependent libraries are searched last to resolve
symbols.
2. It is an error if a file cannot be found for a given specifier.
3. Any command line options in effect at the end of the command line parsing apply
to the dependent libraries, e.g. --whole-archive.
4. The linker tries to add a library or relocatable object file from each of the
strings in a .deplibs section by; first, handling the string as if it was
specified on the command line; second, by looking for the string in each of the
library search paths in turn; third, by looking for a lib<string>.a or
lib<string>.so (depending on the current mode of the linker) in each of the
library search paths.
5. A new command line option --no-dependent-libraries tells LLD to ignore the
dependent libraries.
Rationale for the above points:
1. Adding the dependent libraries last makes the process simple to understand
from a developers perspective. All linkers are able to implement this scheme.
2. Error-ing for libraries that are not found seems like better behavior than
failing the link during symbol resolution.
3. It seems useful for the user to be able to apply command line options which
will affect all of the dependent libraries. There is a potential problem of
surprise for developers, who might not realize that these options would apply
to these "invisible" input files; however, despite the potential for surprise,
this is easy for developers to reason about and gives developers the control
that they may require.
4. This algorithm takes into account all of the different ways that ELF linkers
find input files. The different search methods are tried by the linker in most
obvious to least obvious order.
5. I considered adding finer grained control over which dependent libraries were
ignored (e.g. MSVC has /nodefaultlib:<library>); however, I concluded that this
is not necessary: if finer control is required developers can fall back to using
the command line directly.
RFC thread: http://lists.llvm.org/pipermail/llvm-dev/2019-March/131004.html.
Differential Revision: https://reviews.llvm.org/D60274
llvm-svn: 360984
2019-05-17 11:44:15 +08:00
|
|
|
// symbols that we need to the symbol table. This process might
|
|
|
|
// add files to the link, via autolinking, these files are always
|
|
|
|
// appended to the Files vector.
|
|
|
|
for (size_t i = 0; i < files.size(); ++i)
|
|
|
|
parseFile(files[i]);
|
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->isPic || 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)
|
2019-05-30 22:50:10 +08:00
|
|
|
addUndefined(name);
|
2017-08-23 16:37:22 +08:00
|
|
|
|
2017-10-04 04:45:09 +08:00
|
|
|
// Handle the `--undefined <sym>` options.
|
2019-06-14 22:00:59 +08:00
|
|
|
for (StringRef arg : config->undefined)
|
|
|
|
if (Symbol *sym = symtab->find(arg))
|
|
|
|
handleUndefined(sym);
|
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.
|
2019-06-14 22:00:59 +08:00
|
|
|
if (Symbol *sym = symtab->find(config->entry))
|
|
|
|
handleUndefined(sym);
|
|
|
|
|
|
|
|
// Handle the `--undefined-glob <pattern>` options.
|
|
|
|
for (StringRef pat : args::getStrings(args, OPT_undefined_glob))
|
|
|
|
handleUndefinedGlob(pat);
|
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())
|
2019-09-17 02:49:57 +08:00
|
|
|
for (auto *s : lto::LTO::getRuntimeLibcallSymbols())
|
2019-05-16 11:45:13 +08:00
|
|
|
handleLibcall(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
|
|
|
|
2019-07-16 13:50:45 +08:00
|
|
|
// Create elfHeader early. We need a dummy section in
|
2017-12-12 01:23:28 +08:00
|
|
|
// 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.
|
2019-05-30 22:50:10 +08:00
|
|
|
std::vector<WrappedSymbol> wrapped = addWrappedSymbols(args);
|
2019-01-03 03:28:00 +08:00
|
|
|
|
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.
|
2019-05-23 17:26:27 +08:00
|
|
|
compileBitcodeFiles<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));
|
[Coding style change] Rename variables so that they start with a lowercase letter
This patch is mechanically generated by clang-llvm-rename tool that I wrote
using Clang Refactoring Engine just for creating this patch. You can see the
source code of the tool at https://reviews.llvm.org/D64123. There's no manual
post-processing; you can generate the same patch by re-running the tool against
lld's code base.
Here is the main discussion thread to change the LLVM coding style:
https://lists.llvm.org/pipermail/llvm-dev/2019-February/130083.html
In the discussion thread, I proposed we use lld as a testbed for variable
naming scheme change, and this patch does that.
I chose to rename variables so that they are in camelCase, just because that
is a minimal change to make variables to start with a lowercase letter.
Note to downstream patch maintainers: if you are maintaining a downstream lld
repo, just rebasing ahead of this commit would cause massive merge conflicts
because this patch essentially changes every line in the lld subdirectory. But
there's a remedy.
clang-llvm-rename tool is a batch tool, so you can rename variables in your
downstream repo with the tool. Given that, here is how to rebase your repo to
a commit after the mass renaming:
1. rebase to the commit just before the mass variable renaming,
2. apply the tool to your downstream repo to mass-rename variables locally, and
3. rebase again to the head.
Most changes made by the tool should be identical for a downstream repo and
for the head, so at the step 3, almost all changes should be merged and
disappear. I'd expect that there would be some lines that you need to merge by
hand, but that shouldn't be too many.
Differential Revision: https://reviews.llvm.org/D64121
llvm-svn: 365595
2019-07-10 13:00:37 +08:00
|
|
|
|
2019-05-29 11:55:20 +08:00
|
|
|
llvm::erase_if(inputSections, [](InputSectionBase *s) {
|
|
|
|
if (s->type == SHT_LLVM_SYMPART) {
|
|
|
|
readSymbolPartitionSection<ELFT>(s);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We do not want to emit debug sections if --strip-all
|
|
|
|
// or -strip-debug are given.
|
|
|
|
return config->strip != StripPolicy::None &&
|
|
|
|
(s->name.startswith(".debug") || s->name.startswith(".zdebug"));
|
|
|
|
});
|
2017-11-04 16:20:30 +08:00
|
|
|
|
2019-06-08 01:57:58 +08:00
|
|
|
// Now that the number of partitions is fixed, save a pointer to the main
|
|
|
|
// partition.
|
|
|
|
mainPart = &partitions[0];
|
|
|
|
|
2019-06-05 11:04:46 +08:00
|
|
|
// Read .note.gnu.property sections from input object files which
|
|
|
|
// contain a hint to tweak linker's and loader's behaviors.
|
|
|
|
config->andFeatures = getAndFeatures<ELFT>();
|
|
|
|
|
2019-06-07 21:00:17 +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();
|
|
|
|
|
2017-10-25 01:01:40 +08:00
|
|
|
config->eflags = target->calcEFlags();
|
2019-07-16 13:50:45 +08:00
|
|
|
// maxPageSize (sometimes called abi page size) is the maximum page size that
|
2019-05-14 00:01:26 +08:00
|
|
|
// the output can be run on. For example if the OS can use 4k or 64k page
|
2019-07-16 13:50:45 +08:00
|
|
|
// sizes then maxPageSize must be 64k for the output to be useable on both.
|
2019-05-14 00:01:26 +08:00
|
|
|
// All important alignment decisions must use this value.
|
2019-03-29 01:38:53 +08:00
|
|
|
config->maxPageSize = getMaxPageSize(args);
|
2019-07-16 13:50:45 +08:00
|
|
|
// commonPageSize is the most common page size that the output will be run on.
|
2019-05-14 00:01:26 +08:00
|
|
|
// For example if an OS can use 4k or 64k page sizes and 4k is more common
|
2019-07-16 13:50:45 +08:00
|
|
|
// than 64k then commonPageSize is set to 4k. commonPageSize can be used for
|
2019-05-14 00:01:26 +08:00
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2019-09-24 19:48:31 +08:00
|
|
|
// This adds a .comment section containing a version string.
|
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
|
|
|
|
2019-05-16 11:29:03 +08:00
|
|
|
// Replace common symbols with regular symbols.
|
|
|
|
replaceCommonSymbols();
|
|
|
|
|
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();
|
2019-09-06 23:57:24 +08:00
|
|
|
|
|
|
|
// Make copies of any input sections that need to be copied into each
|
|
|
|
// partition.
|
|
|
|
copySectionsIntoPartitions();
|
|
|
|
|
|
|
|
// Create synthesized sections such as .got and .plt. This is called before
|
|
|
|
// processSectionCommands() so that they can be placed by SECTIONS commands.
|
|
|
|
createSyntheticSections<ELFT>();
|
|
|
|
|
|
|
|
// Some input sections that are used for exception handling need to be moved
|
|
|
|
// into synthetic sections. Do that now so that they aren't assigned to
|
|
|
|
// output sections in the usual way.
|
|
|
|
if (!config->relocatable)
|
|
|
|
combineEhSections();
|
|
|
|
|
|
|
|
// Create output sections described by SECTIONS commands.
|
|
|
|
script->processSectionCommands();
|
|
|
|
|
2019-09-06 23:57:44 +08:00
|
|
|
// Linker scripts control how input sections are assigned to output sections.
|
|
|
|
// Input sections that were not handled by scripts are called "orphans", and
|
|
|
|
// they are assigned to output sections by the default rule. Process that.
|
|
|
|
script->addOrphanSections();
|
|
|
|
|
2019-09-24 19:48:31 +08:00
|
|
|
// Migrate InputSectionDescription::sectionBases to sections. This includes
|
|
|
|
// merging MergeInputSections into a single MergeSyntheticSection. From this
|
|
|
|
// point onwards InputSectionDescription::sections should be used instead of
|
|
|
|
// sectionBases.
|
|
|
|
for (BaseCommand *base : script->sectionCommands)
|
|
|
|
if (auto *sec = dyn_cast<OutputSection>(base))
|
|
|
|
sec->finalizeInputSections();
|
|
|
|
llvm::erase_if(inputSections,
|
|
|
|
[](InputSectionBase *s) { return isa<MergeInputSection>(s); });
|
|
|
|
|
2019-09-06 23:57:24 +08:00
|
|
|
// Two input sections with different output sections should not be folded.
|
|
|
|
// ICF runs after processSectionCommands() so that we know the output sections.
|
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
|
|
|
}
|