2015-07-25 05:03:07 +08:00
|
|
|
//===- InputFiles.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
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "InputFiles.h"
|
[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
|
|
|
#include "Driver.h"
|
2016-02-11 23:24:48 +08:00
|
|
|
#include "InputSection.h"
|
2016-08-13 03:56:57 +08:00
|
|
|
#include "LinkerScript.h"
|
ELF: New symbol table design.
This patch implements a new design for the symbol table that stores
SymbolBodies within a memory region of the Symbol object. Symbols are mutated
by constructing SymbolBodies in place over existing SymbolBodies, rather
than by mutating pointers. As mentioned in the initial proposal [1], this
memory layout helps reduce the cache miss rate by improving memory locality.
Performance numbers:
old(s) new(s)
Without debug info:
chrome 7.178 6.432 (-11.5%)
LLVMgold.so 0.505 0.502 (-0.5%)
clang 0.954 0.827 (-15.4%)
llvm-as 0.052 0.045 (-15.5%)
With debug info:
scylla 5.695 5.613 (-1.5%)
clang 14.396 14.143 (-1.8%)
Performance counter results show that the fewer required indirections is
indeed the cause of the improved performance. For example, when linking
chrome, stalled cycles decreases from 14,556,444,002 to 12,959,238,310, and
instructions per cycle increases from 0.78 to 0.83. We are also executing
many fewer instructions (15,516,401,933 down to 15,002,434,310), probably
because we spend less time allocating SymbolBodies.
The new mechanism by which symbols are added to the symbol table is by calling
add* functions on the SymbolTable.
In this patch, I handle local symbols by storing them inside "unparented"
SymbolBodies. This is suboptimal, but if we do want to try to avoid allocating
these SymbolBodies, we can probably do that separately.
I also removed a few members from the SymbolBody class that were only being
used to pass information from the input file to the symbol table.
This patch implements the new design for the ELF linker only. I intend to
prepare a similar patch for the COFF linker.
[1] http://lists.llvm.org/pipermail/llvm-dev/2016-April/098832.html
Differential Revision: http://reviews.llvm.org/D19752
llvm-svn: 268178
2016-05-01 12:55:03 +08:00
|
|
|
#include "SymbolTable.h"
|
2015-07-25 05:03:07 +08:00
|
|
|
#include "Symbols.h"
|
2016-12-14 18:36:12 +08:00
|
|
|
#include "SyntheticSections.h"
|
2019-11-15 06:16:21 +08:00
|
|
|
#include "lld/Common/DWARF.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"
|
2017-11-29 04:39:17 +08:00
|
|
|
#include "lld/Common/Memory.h"
|
2015-07-25 05:03:07 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2016-04-22 05:44:25 +08:00
|
|
|
#include "llvm/CodeGen/Analysis.h"
|
2016-02-13 04:54:57 +08:00
|
|
|
#include "llvm/IR/LLVMContext.h"
|
2016-03-02 23:43:50 +08:00
|
|
|
#include "llvm/IR/Module.h"
|
2016-09-29 08:40:08 +08:00
|
|
|
#include "llvm/LTO/LTO.h"
|
2016-09-10 06:08:04 +08:00
|
|
|
#include "llvm/MC/StringTableBuilder.h"
|
2016-11-01 17:17:50 +08:00
|
|
|
#include "llvm/Object/ELFObjectFile.h"
|
2017-11-28 21:51:48 +08:00
|
|
|
#include "llvm/Support/ARMAttributeParser.h"
|
|
|
|
#include "llvm/Support/ARMBuildAttributes.h"
|
2019-06-05 11:04:46 +08:00
|
|
|
#include "llvm/Support/Endian.h"
|
2016-09-09 05:18:38 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
2020-08-21 01:10:18 +08:00
|
|
|
#include "llvm/Support/RISCVAttributeParser.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/raw_ostream.h"
|
2015-07-25 05:03:07 +08:00
|
|
|
|
2015-09-05 06:28:10 +08:00
|
|
|
using namespace llvm;
|
2015-07-25 05:03:07 +08:00
|
|
|
using namespace llvm::ELF;
|
2015-09-04 04:03:54 +08:00
|
|
|
using namespace llvm::object;
|
2017-12-24 01:21:39 +08:00
|
|
|
using namespace llvm::sys;
|
2015-10-01 01:06:09 +08:00
|
|
|
using namespace llvm::sys::fs;
|
2019-06-05 11:04:46 +08:00
|
|
|
using namespace llvm::support::endian;
|
2020-05-15 13:18:58 +08:00
|
|
|
using namespace lld;
|
|
|
|
using namespace lld::elf;
|
|
|
|
|
|
|
|
bool InputFile::isInGroup;
|
|
|
|
uint32_t InputFile::nextGroupId;
|
|
|
|
|
|
|
|
std::vector<ArchiveFile *> elf::archiveFiles;
|
|
|
|
std::vector<BinaryFile *> elf::binaryFiles;
|
|
|
|
std::vector<BitcodeFile *> elf::bitcodeFiles;
|
|
|
|
std::vector<LazyObjFile *> elf::lazyObjFiles;
|
|
|
|
std::vector<InputFile *> elf::objectFiles;
|
|
|
|
std::vector<SharedFile *> elf::sharedFiles;
|
|
|
|
|
|
|
|
std::unique_ptr<TarWriter> elf::tar;
|
2015-07-25 05:03:07 +08:00
|
|
|
|
2019-10-07 16:31:18 +08:00
|
|
|
// Returns "<internal>", "foo.a(bar.o)" or "baz.o".
|
2020-05-15 13:18:58 +08:00
|
|
|
std::string lld::toString(const InputFile *f) {
|
2019-10-07 16:31:18 +08:00
|
|
|
if (!f)
|
|
|
|
return "<internal>";
|
2015-07-25 05:03:07 +08:00
|
|
|
|
2019-10-07 16:31:18 +08:00
|
|
|
if (f->toStringCache.empty()) {
|
|
|
|
if (f->archiveName.empty())
|
2020-01-29 03:23:46 +08:00
|
|
|
f->toStringCache = std::string(f->getName());
|
2019-10-07 16:31:18 +08:00
|
|
|
else
|
|
|
|
f->toStringCache = (f->archiveName + "(" + f->getName() + ")").str();
|
|
|
|
}
|
|
|
|
return f->toStringCache;
|
|
|
|
}
|
|
|
|
|
2019-05-27 15:26:13 +08:00
|
|
|
static ELFKind getELFKind(MemoryBufferRef mb, StringRef archiveName) {
|
|
|
|
unsigned char size;
|
|
|
|
unsigned char endian;
|
|
|
|
std::tie(size, endian) = getElfArchType(mb.getBuffer());
|
[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-07-03 14:11:50 +08:00
|
|
|
auto report = [&](StringRef msg) {
|
2019-05-27 15:26:13 +08:00
|
|
|
StringRef filename = mb.getBufferIdentifier();
|
|
|
|
if (archiveName.empty())
|
|
|
|
fatal(filename + ": " + msg);
|
|
|
|
else
|
|
|
|
fatal(archiveName + "(" + filename + "): " + msg);
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!mb.getBuffer().startswith(ElfMagic))
|
2019-07-03 14:11:50 +08:00
|
|
|
report("not an ELF file");
|
2019-05-27 15:26:13 +08:00
|
|
|
if (endian != ELFDATA2LSB && endian != ELFDATA2MSB)
|
2019-07-03 14:11:50 +08:00
|
|
|
report("corrupted ELF file: invalid data encoding");
|
2019-05-27 15:26:13 +08:00
|
|
|
if (size != ELFCLASS32 && size != ELFCLASS64)
|
2019-07-03 14:11:50 +08:00
|
|
|
report("corrupted ELF file: invalid file class");
|
[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-27 15:26:13 +08:00
|
|
|
size_t bufSize = mb.getBuffer().size();
|
|
|
|
if ((size == ELFCLASS32 && bufSize < sizeof(Elf32_Ehdr)) ||
|
|
|
|
(size == ELFCLASS64 && bufSize < sizeof(Elf64_Ehdr)))
|
2019-07-03 14:11:50 +08:00
|
|
|
report("corrupted ELF file: file is too short");
|
[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-27 15:26:13 +08:00
|
|
|
if (size == ELFCLASS32)
|
|
|
|
return (endian == ELFDATA2LSB) ? ELF32LEKind : ELF32BEKind;
|
|
|
|
return (endian == ELFDATA2LSB) ? ELF64LEKind : ELF64BEKind;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
InputFile::InputFile(Kind k, MemoryBufferRef m)
|
|
|
|
: mb(m), groupId(nextGroupId), fileKind(k) {
|
|
|
|
// All files within the same --{start,end}-group get the same group ID.
|
|
|
|
// Otherwise, a new file will get a new group ID.
|
|
|
|
if (!isInGroup)
|
|
|
|
++nextGroupId;
|
|
|
|
}
|
2017-03-31 05:13:00 +08:00
|
|
|
|
2020-05-15 13:18:58 +08:00
|
|
|
Optional<MemoryBufferRef> elf::readFile(StringRef path) {
|
2017-07-21 02:17:55 +08:00
|
|
|
// The --chroot option changes our virtual root directory.
|
|
|
|
// This is useful when you are dealing with files created by --reproduce.
|
|
|
|
if (!config->chroot.empty() && path.startswith("/"))
|
|
|
|
path = saver.save(config->chroot + path);
|
|
|
|
|
2017-02-22 07:22:56 +08:00
|
|
|
log(path);
|
2020-06-24 11:00:04 +08:00
|
|
|
config->dependencyFiles.insert(llvm::CachedHashString(path));
|
2017-07-21 02:17:55 +08:00
|
|
|
|
2018-04-27 23:32:04 +08:00
|
|
|
auto mbOrErr = MemoryBuffer::getFile(path, -1, false);
|
2017-01-09 09:42:02 +08:00
|
|
|
if (auto ec = mbOrErr.getError()) {
|
2017-01-13 06:18:04 +08:00
|
|
|
error("cannot open " + path + ": " + ec.message());
|
2017-01-09 09:42:02 +08:00
|
|
|
return None;
|
|
|
|
}
|
2017-02-22 07:22:56 +08:00
|
|
|
|
2017-01-09 09:42:02 +08:00
|
|
|
std::unique_ptr<MemoryBuffer> &mb = *mbOrErr;
|
|
|
|
MemoryBufferRef mbref = mb->getMemBufferRef();
|
|
|
|
make<std::unique_ptr<MemoryBuffer>>(std::move(mb)); // take MB ownership
|
|
|
|
|
|
|
|
if (tar)
|
|
|
|
tar->append(relativeToRoot(path), mbref.getBuffer());
|
|
|
|
return mbref;
|
|
|
|
}
|
|
|
|
|
2019-05-14 20:03:13 +08:00
|
|
|
// All input object files must be for the same architecture
|
|
|
|
// (e.g. it does not make sense to link x86 object files with
|
|
|
|
// MIPS object files.) This function checks for that error.
|
|
|
|
static bool isCompatible(InputFile *file) {
|
|
|
|
if (!file->isElf() && !isa<BitcodeFile>(file))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (file->ekind == config->ekind && file->emachine == config->emachine) {
|
|
|
|
if (config->emachine != EM_MIPS)
|
|
|
|
return true;
|
|
|
|
if (isMipsN32Abi(file) == config->mipsN32Abi)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
[ELF] Correct error message when OUTPUT_FORMAT is used
Any OUTPUT_FORMAT in a linker script overrides the emulation passed on
the command line, so record the passed bfdname and use that in the error
message about incompatible input files.
This prevents confusing error messages. For example, if you explicitly
pass `-m elf_x86_64` to LLD but accidentally include a linker script
which sets `OUTPUT_FORMAT(elf32-i386)`, LLD would previously complain
about your input files being compatible with elf_x86_64, which isn't the
actual issue, and is confusing because the input files are in fact
x86-64 ELF files.
Interestingly enough, this also prevents a segfault! When we don't pass
`-m` and we have an object file which is incompatible with the
`OUTPUT_FORMAT` set by a linker script, the object file is checked for
compatibility before it's added to the objectFiles vector.
config->emulation, objectFiles, and sharedFiles will all be empty, so
we'll attempt to access bitcodeFiles[0], but bitcodeFiles is also empty,
so we'll segfault. This commit prevents the segfault by adding
OUTPUT_FORMAT as a possible source of machine configuration, and it also
adds an llvm_unreachable to diagnose similar issues in the future.
Differential Revision: https://reviews.llvm.org/D76109
2020-03-13 06:25:36 +08:00
|
|
|
StringRef target =
|
|
|
|
!config->bfdname.empty() ? config->bfdname : config->emulation;
|
|
|
|
if (!target.empty()) {
|
|
|
|
error(toString(file) + " is incompatible with " + target);
|
2019-07-29 13:24:51 +08:00
|
|
|
return false;
|
2019-05-14 20:03:13 +08:00
|
|
|
}
|
|
|
|
|
2019-07-29 13:24:51 +08:00
|
|
|
InputFile *existing;
|
|
|
|
if (!objectFiles.empty())
|
|
|
|
existing = objectFiles[0];
|
|
|
|
else if (!sharedFiles.empty())
|
|
|
|
existing = sharedFiles[0];
|
[ELF] Correct error message when OUTPUT_FORMAT is used
Any OUTPUT_FORMAT in a linker script overrides the emulation passed on
the command line, so record the passed bfdname and use that in the error
message about incompatible input files.
This prevents confusing error messages. For example, if you explicitly
pass `-m elf_x86_64` to LLD but accidentally include a linker script
which sets `OUTPUT_FORMAT(elf32-i386)`, LLD would previously complain
about your input files being compatible with elf_x86_64, which isn't the
actual issue, and is confusing because the input files are in fact
x86-64 ELF files.
Interestingly enough, this also prevents a segfault! When we don't pass
`-m` and we have an object file which is incompatible with the
`OUTPUT_FORMAT` set by a linker script, the object file is checked for
compatibility before it's added to the objectFiles vector.
config->emulation, objectFiles, and sharedFiles will all be empty, so
we'll attempt to access bitcodeFiles[0], but bitcodeFiles is also empty,
so we'll segfault. This commit prevents the segfault by adding
OUTPUT_FORMAT as a possible source of machine configuration, and it also
adds an llvm_unreachable to diagnose similar issues in the future.
Differential Revision: https://reviews.llvm.org/D76109
2020-03-13 06:25:36 +08:00
|
|
|
else if (!bitcodeFiles.empty())
|
2019-07-29 13:24:51 +08:00
|
|
|
existing = bitcodeFiles[0];
|
[ELF] Correct error message when OUTPUT_FORMAT is used
Any OUTPUT_FORMAT in a linker script overrides the emulation passed on
the command line, so record the passed bfdname and use that in the error
message about incompatible input files.
This prevents confusing error messages. For example, if you explicitly
pass `-m elf_x86_64` to LLD but accidentally include a linker script
which sets `OUTPUT_FORMAT(elf32-i386)`, LLD would previously complain
about your input files being compatible with elf_x86_64, which isn't the
actual issue, and is confusing because the input files are in fact
x86-64 ELF files.
Interestingly enough, this also prevents a segfault! When we don't pass
`-m` and we have an object file which is incompatible with the
`OUTPUT_FORMAT` set by a linker script, the object file is checked for
compatibility before it's added to the objectFiles vector.
config->emulation, objectFiles, and sharedFiles will all be empty, so
we'll attempt to access bitcodeFiles[0], but bitcodeFiles is also empty,
so we'll segfault. This commit prevents the segfault by adding
OUTPUT_FORMAT as a possible source of machine configuration, and it also
adds an llvm_unreachable to diagnose similar issues in the future.
Differential Revision: https://reviews.llvm.org/D76109
2020-03-13 06:25:36 +08:00
|
|
|
else
|
|
|
|
llvm_unreachable("Must have -m, OUTPUT_FORMAT or existing input file to "
|
|
|
|
"determine target emulation");
|
2019-07-29 13:24:51 +08:00
|
|
|
|
|
|
|
error(toString(file) + " is incompatible with " + toString(existing));
|
2019-05-14 20:03:13 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-05-16 11:45:13 +08:00
|
|
|
template <class ELFT> static void doParseFile(InputFile *file) {
|
2019-05-14 20:03:13 +08:00
|
|
|
if (!isCompatible(file))
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Binary file
|
|
|
|
if (auto *f = dyn_cast<BinaryFile>(file)) {
|
|
|
|
binaryFiles.push_back(f);
|
|
|
|
f->parse();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// .a file
|
|
|
|
if (auto *f = dyn_cast<ArchiveFile>(file)) {
|
2020-04-28 10:30:09 +08:00
|
|
|
archiveFiles.push_back(f);
|
2019-05-16 11:45:13 +08:00
|
|
|
f->parse();
|
2019-05-14 20:03:13 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lazy object file
|
|
|
|
if (auto *f = dyn_cast<LazyObjFile>(file)) {
|
|
|
|
lazyObjFiles.push_back(f);
|
|
|
|
f->parse<ELFT>();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (config->trace)
|
|
|
|
message(toString(file));
|
|
|
|
|
|
|
|
// .so file
|
|
|
|
if (auto *f = dyn_cast<SharedFile>(file)) {
|
|
|
|
f->parse<ELFT>();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// LLVM bitcode file
|
|
|
|
if (auto *f = dyn_cast<BitcodeFile>(file)) {
|
|
|
|
bitcodeFiles.push_back(f);
|
2019-06-06 01:39:37 +08:00
|
|
|
f->parse<ELFT>();
|
2019-05-14 20:03:13 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Regular object file
|
|
|
|
objectFiles.push_back(file);
|
2019-06-06 01:39:37 +08:00
|
|
|
cast<ObjFile<ELFT>>(file)->parse();
|
2019-05-14 20:03:13 +08:00
|
|
|
}
|
|
|
|
|
2019-05-16 11:45:13 +08:00
|
|
|
// Add symbols in File to the symbol table.
|
2020-05-15 13:18:58 +08:00
|
|
|
void elf::parseFile(InputFile *file) {
|
2019-05-16 11:45:13 +08:00
|
|
|
switch (config->ekind) {
|
|
|
|
case ELF32LEKind:
|
|
|
|
doParseFile<ELF32LE>(file);
|
|
|
|
return;
|
|
|
|
case ELF32BEKind:
|
|
|
|
doParseFile<ELF32BE>(file);
|
|
|
|
return;
|
|
|
|
case ELF64LEKind:
|
|
|
|
doParseFile<ELF64LE>(file);
|
|
|
|
return;
|
|
|
|
case ELF64BEKind:
|
|
|
|
doParseFile<ELF64BE>(file);
|
|
|
|
return;
|
|
|
|
default:
|
|
|
|
llvm_unreachable("unknown ELFT");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-24 01:21:39 +08:00
|
|
|
// Concatenates arguments to construct a string representing an error location.
|
|
|
|
static std::string createFileLineMsg(StringRef path, unsigned line) {
|
2020-01-29 03:23:46 +08:00
|
|
|
std::string filename = std::string(path::filename(path));
|
2017-12-24 01:21:39 +08:00
|
|
|
std::string lineno = ":" + std::to_string(line);
|
|
|
|
if (filename == path)
|
|
|
|
return filename + lineno;
|
|
|
|
return filename + lineno + " (" + path.str() + lineno + ")";
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
|
|
|
static std::string getSrcMsgAux(ObjFile<ELFT> &file, const Symbol &sym,
|
|
|
|
InputSectionBase &sec, uint64_t offset) {
|
|
|
|
// In DWARF, functions and variables are stored to different places.
|
|
|
|
// First, lookup a function for a given offset.
|
|
|
|
if (Optional<DILineInfo> info = file.getDILineInfo(&sec, offset))
|
|
|
|
return createFileLineMsg(info->FileName, info->Line);
|
|
|
|
|
|
|
|
// If it failed, lookup again as a variable.
|
|
|
|
if (Optional<std::pair<std::string, unsigned>> fileLine =
|
|
|
|
file.getVariableLoc(sym.getName()))
|
|
|
|
return createFileLineMsg(fileLine->first, fileLine->second);
|
|
|
|
|
2019-07-16 13:50:45 +08:00
|
|
|
// File.sourceFile contains STT_FILE symbol, and that is a last resort.
|
2020-01-29 03:23:46 +08:00
|
|
|
return std::string(file.sourceFile);
|
2017-12-24 01:21:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string InputFile::getSrcMsg(const Symbol &sym, InputSectionBase &sec,
|
|
|
|
uint64_t offset) {
|
|
|
|
if (kind() != ObjKind)
|
|
|
|
return "";
|
|
|
|
switch (config->ekind) {
|
|
|
|
default:
|
|
|
|
llvm_unreachable("Invalid kind");
|
|
|
|
case ELF32LEKind:
|
|
|
|
return getSrcMsgAux(cast<ObjFile<ELF32LE>>(*this), sym, sec, offset);
|
|
|
|
case ELF32BEKind:
|
|
|
|
return getSrcMsgAux(cast<ObjFile<ELF32BE>>(*this), sym, sec, offset);
|
|
|
|
case ELF64LEKind:
|
|
|
|
return getSrcMsgAux(cast<ObjFile<ELF64LE>>(*this), sym, sec, offset);
|
|
|
|
case ELF64BEKind:
|
|
|
|
return getSrcMsgAux(cast<ObjFile<ELF64BE>>(*this), sym, sec, offset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-28 01:38:42 +08:00
|
|
|
template <class ELFT> DWARFCache *ObjFile<ELFT>::getDwarf() {
|
|
|
|
llvm::call_once(initDwarf, [this]() {
|
|
|
|
dwarf = std::make_unique<DWARFCache>(std::make_unique<DWARFContext>(
|
|
|
|
std::make_unique<LLDDwarfObj<ELFT>>(this), "",
|
|
|
|
[&](Error err) { warn(getName() + ": " + toString(std::move(err))); },
|
|
|
|
[&](Error warning) {
|
|
|
|
warn(getName() + ": " + toString(std::move(warning)));
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
|
|
|
|
return dwarf.get();
|
2017-11-01 15:42:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the pair of file name and line number describing location of data
|
|
|
|
// object (variable, array, etc) definition.
|
|
|
|
template <class ELFT>
|
|
|
|
Optional<std::pair<std::string, unsigned>>
|
|
|
|
ObjFile<ELFT>::getVariableLoc(StringRef name) {
|
2020-02-28 01:38:42 +08:00
|
|
|
return getDwarf()->getVariableLoc(name);
|
2016-10-26 19:07:09 +08:00
|
|
|
}
|
|
|
|
|
2016-11-03 02:42:13 +08:00
|
|
|
// Returns source line information for a given offset
|
|
|
|
// using DWARF debug info.
|
2016-11-01 17:17:50 +08:00
|
|
|
template <class ELFT>
|
2017-07-27 06:13:32 +08:00
|
|
|
Optional<DILineInfo> ObjFile<ELFT>::getDILineInfo(InputSectionBase *s,
|
|
|
|
uint64_t offset) {
|
2019-02-27 21:17:36 +08:00
|
|
|
// Detect SectionIndex for specified section.
|
|
|
|
uint64_t sectionIndex = object::SectionedAddress::UndefSection;
|
|
|
|
ArrayRef<InputSectionBase *> sections = s->file->getSections();
|
|
|
|
for (uint64_t curIndex = 0; curIndex < sections.size(); ++curIndex) {
|
|
|
|
if (s == sections[curIndex]) {
|
|
|
|
sectionIndex = curIndex;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-28 01:38:42 +08:00
|
|
|
return getDwarf()->getDILineInfo(offset, sectionIndex);
|
2017-03-31 03:13:47 +08:00
|
|
|
}
|
|
|
|
|
2019-05-28 13:17:21 +08:00
|
|
|
ELFFileBase::ELFFileBase(Kind k, MemoryBufferRef mb) : InputFile(k, mb) {
|
|
|
|
ekind = getELFKind(mb, "");
|
2019-04-06 04:16:26 +08:00
|
|
|
|
2019-05-28 13:17:21 +08:00
|
|
|
switch (ekind) {
|
|
|
|
case ELF32LEKind:
|
|
|
|
init<ELF32LE>();
|
|
|
|
break;
|
|
|
|
case ELF32BEKind:
|
|
|
|
init<ELF32BE>();
|
|
|
|
break;
|
|
|
|
case ELF64LEKind:
|
|
|
|
init<ELF64LE>();
|
|
|
|
break;
|
|
|
|
case ELF64BEKind:
|
|
|
|
init<ELF64BE>();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
llvm_unreachable("getELFKind");
|
|
|
|
}
|
|
|
|
}
|
2017-04-27 06:51:51 +08:00
|
|
|
|
2019-05-28 13:17:21 +08:00
|
|
|
template <typename Elf_Shdr>
|
|
|
|
static const Elf_Shdr *findSection(ArrayRef<Elf_Shdr> sections, uint32_t type) {
|
|
|
|
for (const Elf_Shdr &sec : sections)
|
|
|
|
if (sec.sh_type == type)
|
|
|
|
return &sec;
|
|
|
|
return nullptr;
|
2016-06-29 09:30:50 +08:00
|
|
|
}
|
|
|
|
|
2019-05-28 13:17:21 +08:00
|
|
|
template <class ELFT> void ELFFileBase::init() {
|
|
|
|
using Elf_Shdr = typename ELFT::Shdr;
|
|
|
|
using Elf_Sym = typename ELFT::Sym;
|
2016-11-04 00:55:44 +08:00
|
|
|
|
2019-05-28 13:17:21 +08:00
|
|
|
// Initialize trivial attributes.
|
|
|
|
const ELFFile<ELFT> &obj = getObj<ELFT>();
|
2020-09-09 22:03:53 +08:00
|
|
|
emachine = obj.getHeader().e_machine;
|
|
|
|
osabi = obj.getHeader().e_ident[llvm::ELF::EI_OSABI];
|
|
|
|
abiVersion = obj.getHeader().e_ident[llvm::ELF::EI_ABIVERSION];
|
2015-10-02 03:52:48 +08:00
|
|
|
|
2019-05-28 13:17:21 +08:00
|
|
|
ArrayRef<Elf_Shdr> sections = CHECK(obj.sections(), this);
|
|
|
|
|
|
|
|
// Find a symbol table.
|
|
|
|
bool isDSO =
|
|
|
|
(identify_magic(mb.getBuffer()) == file_magic::elf_shared_object);
|
|
|
|
const Elf_Shdr *symtabSec =
|
|
|
|
findSection(sections, isDSO ? SHT_DYNSYM : SHT_SYMTAB);
|
|
|
|
|
|
|
|
if (!symtabSec)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Initialize members corresponding to a symbol table.
|
|
|
|
firstGlobal = symtabSec->sh_info;
|
|
|
|
|
|
|
|
ArrayRef<Elf_Sym> eSyms = CHECK(obj.symbols(symtabSec), this);
|
|
|
|
if (firstGlobal == 0 || firstGlobal > eSyms.size())
|
|
|
|
fatal(toString(this) + ": invalid sh_info in symbol table");
|
|
|
|
|
|
|
|
elfSyms = reinterpret_cast<const void *>(eSyms.data());
|
|
|
|
numELFSyms = eSyms.size();
|
|
|
|
stringTable = CHECK(obj.getStringTableForSymtab(*symtabSec, sections), this);
|
2017-04-27 06:51:51 +08:00
|
|
|
}
|
2016-03-11 20:06:30 +08:00
|
|
|
|
2019-04-04 11:13:51 +08:00
|
|
|
template <class ELFT>
|
|
|
|
uint32_t ObjFile<ELFT>::getSectionIndex(const Elf_Sym &sym) const {
|
2019-04-06 04:16:26 +08:00
|
|
|
return CHECK(
|
2020-09-09 22:03:53 +08:00
|
|
|
this->getObj().getSectionIndex(sym, getELFSyms<ELFT>(), shndxTable),
|
2019-04-06 04:16:26 +08:00
|
|
|
this);
|
2019-04-04 11:13:51 +08:00
|
|
|
}
|
|
|
|
|
2017-11-04 05:21:47 +08:00
|
|
|
template <class ELFT> ArrayRef<Symbol *> ObjFile<ELFT>::getLocalSymbols() {
|
2017-08-04 19:07:42 +08:00
|
|
|
if (this->symbols.empty())
|
|
|
|
return {};
|
2018-03-29 06:55:40 +08:00
|
|
|
return makeArrayRef(this->symbols).slice(1, this->firstGlobal - 1);
|
2015-09-08 23:50:05 +08:00
|
|
|
}
|
|
|
|
|
2018-03-29 06:45:39 +08:00
|
|
|
template <class ELFT> ArrayRef<Symbol *> ObjFile<ELFT>::getGlobalSymbols() {
|
2018-03-29 06:55:40 +08:00
|
|
|
return makeArrayRef(this->symbols).slice(this->firstGlobal);
|
2018-03-29 06:45:39 +08:00
|
|
|
}
|
|
|
|
|
2019-06-06 01:39:37 +08:00
|
|
|
template <class ELFT> void ObjFile<ELFT>::parse(bool ignoreComdats) {
|
2019-07-16 13:50:45 +08:00
|
|
|
// Read a section table. justSymbols is usually false.
|
2018-03-30 09:15:36 +08:00
|
|
|
if (this->justSymbols)
|
|
|
|
initializeJustSymbols();
|
|
|
|
else
|
2019-06-06 01:39:37 +08:00
|
|
|
initializeSections(ignoreComdats);
|
2018-03-30 09:15:36 +08:00
|
|
|
|
|
|
|
// Read a symbol table.
|
2016-11-08 23:51:00 +08:00
|
|
|
initializeSymbols();
|
2015-07-25 05:03:07 +08:00
|
|
|
}
|
|
|
|
|
2015-12-24 16:41:12 +08:00
|
|
|
// Sections with SHT_GROUP and comdat bits define comdat section groups.
|
|
|
|
// They are identified and deduplicated by group name. This function
|
|
|
|
// returns a group name.
|
2015-10-10 03:25:07 +08:00
|
|
|
template <class ELFT>
|
2017-07-27 06:13:32 +08:00
|
|
|
StringRef ObjFile<ELFT>::getShtGroupSignature(ArrayRef<Elf_Shdr> sections,
|
|
|
|
const Elf_Shdr &sec) {
|
2019-07-15 19:47:54 +08:00
|
|
|
typename ELFT::SymRange symbols = this->getELFSyms<ELFT>();
|
|
|
|
if (sec.sh_info >= symbols.size())
|
|
|
|
fatal(toString(this) + ": invalid symbol index");
|
|
|
|
const typename ELFT::Sym &sym = symbols[sec.sh_info];
|
|
|
|
StringRef signature = CHECK(sym.getName(this->stringTable), this);
|
2017-06-13 02:46:33 +08:00
|
|
|
|
|
|
|
// As a special case, if a symbol is a section symbol and has no name,
|
|
|
|
// we use a section name as a signature.
|
|
|
|
//
|
|
|
|
// Such SHT_GROUP sections are invalid from the perspective of the ELF
|
2018-03-12 23:18:35 +08:00
|
|
|
// standard, but GNU gold 1.14 (the newest version as of July 2017) or
|
2017-06-13 02:46:33 +08:00
|
|
|
// older produce such sections as outputs for the -r option, so we need
|
|
|
|
// a bug-compatibility.
|
2019-07-15 19:47:54 +08:00
|
|
|
if (signature.empty() && sym.getType() == STT_SECTION)
|
2017-06-13 02:46:33 +08:00
|
|
|
return getSectionName(sec);
|
|
|
|
return signature;
|
2015-10-10 03:25:07 +08:00
|
|
|
}
|
|
|
|
|
2019-10-10 16:32:12 +08:00
|
|
|
template <class ELFT>
|
|
|
|
bool ObjFile<ELFT>::shouldMerge(const Elf_Shdr &sec, StringRef name) {
|
2020-04-03 13:10:43 +08:00
|
|
|
if (!(sec.sh_flags & SHF_MERGE))
|
|
|
|
return false;
|
|
|
|
|
2018-03-28 01:09:23 +08:00
|
|
|
// On a regular link we don't merge sections if -O0 (default is -O1). This
|
|
|
|
// sometimes makes the linker significantly faster, although the output will
|
|
|
|
// be bigger.
|
|
|
|
//
|
|
|
|
// Doing the same for -r would create a problem as it would combine sections
|
|
|
|
// with different sh_entsize. One option would be to just copy every SHF_MERGE
|
|
|
|
// section as is to the output. While this would produce a valid ELF file with
|
|
|
|
// usable SHF_MERGE sections, tools like (llvm-)?dwarfdump get confused when
|
|
|
|
// they see two .debug_str. We could have separate logic for combining
|
|
|
|
// SHF_MERGE sections based both on their name and sh_entsize, but that seems
|
|
|
|
// to be more trouble than it is worth. Instead, we just use the regular (-O1)
|
|
|
|
// logic for -r.
|
|
|
|
if (config->optimize == 0 && !config->relocatable)
|
2016-04-30 00:12:29 +08:00
|
|
|
return false;
|
|
|
|
|
2016-08-03 13:28:02 +08:00
|
|
|
// A mergeable section with size 0 is useless because they don't have
|
|
|
|
// any data to merge. A mergeable string section with size 0 can be
|
|
|
|
// argued as invalid because it doesn't end with a null character.
|
|
|
|
// We'll avoid a mess by handling them as if they were non-mergeable.
|
|
|
|
if (sec.sh_size == 0)
|
|
|
|
return false;
|
|
|
|
|
2016-09-21 11:22:18 +08:00
|
|
|
// Check for sh_entsize. The ELF spec is not clear about the zero
|
|
|
|
// sh_entsize. It says that "the member [sh_entsize] contains 0 if
|
|
|
|
// the section does not hold a table of fixed-size entries". We know
|
|
|
|
// that Rust 1.13 produces a string mergeable section with a zero
|
|
|
|
// sh_entsize. Here we just accept it rather than being picky about it.
|
2017-02-25 03:52:52 +08:00
|
|
|
uint64_t entSize = sec.sh_entsize;
|
2016-09-21 11:22:18 +08:00
|
|
|
if (entSize == 0)
|
|
|
|
return false;
|
|
|
|
if (sec.sh_size % entSize)
|
2019-10-10 16:32:12 +08:00
|
|
|
fatal(toString(this) + ":(" + name + "): SHF_MERGE section size (" +
|
|
|
|
Twine(sec.sh_size) + ") must be a multiple of sh_entsize (" +
|
|
|
|
Twine(entSize) + ")");
|
2016-09-21 11:22:18 +08:00
|
|
|
|
2020-04-03 13:10:43 +08:00
|
|
|
if (sec.sh_flags & SHF_WRITE)
|
2019-10-10 16:32:12 +08:00
|
|
|
fatal(toString(this) + ":(" + name +
|
|
|
|
"): writable SHF_MERGE section is not supported");
|
2015-10-25 06:51:01 +08:00
|
|
|
|
2017-11-16 01:31:27 +08:00
|
|
|
return true;
|
2015-10-25 06:51:01 +08:00
|
|
|
}
|
|
|
|
|
2018-03-30 09:15:36 +08:00
|
|
|
// This is for --just-symbols.
|
|
|
|
//
|
|
|
|
// --just-symbols is a very minor feature that allows you to link your
|
|
|
|
// output against other existing program, so that if you load both your
|
|
|
|
// program and the other program into memory, your output can refer the
|
|
|
|
// other program's symbols.
|
|
|
|
//
|
|
|
|
// When the option is given, we link "just symbols". The section table is
|
|
|
|
// initialized with null pointers.
|
|
|
|
template <class ELFT> void ObjFile<ELFT>::initializeJustSymbols() {
|
2019-05-28 13:17:21 +08:00
|
|
|
ArrayRef<Elf_Shdr> sections = CHECK(this->getObj().sections(), this);
|
|
|
|
this->sections.resize(sections.size());
|
2018-03-30 09:15:36 +08:00
|
|
|
}
|
|
|
|
|
[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
|
|
|
// An ELF object file may contain a `.deplibs` section. If it exists, the
|
|
|
|
// section contains a list of library specifiers such as `m` for libm. This
|
|
|
|
// function resolves a given name by finding the first matching library checking
|
|
|
|
// the various ways that a library can be specified to LLD. This ELF extension
|
|
|
|
// is a form of autolinking and is called `dependent libraries`. It is currently
|
|
|
|
// unique to LLVM and lld.
|
|
|
|
static void addDependentLibrary(StringRef specifier, const InputFile *f) {
|
|
|
|
if (!config->dependentLibraries)
|
|
|
|
return;
|
|
|
|
if (fs::exists(specifier))
|
2019-07-16 12:46:31 +08:00
|
|
|
driver->addFile(specifier, /*withLOption=*/false);
|
[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
|
|
|
else if (Optional<std::string> s = findFromSearchPaths(specifier))
|
2019-07-16 12:46:31 +08:00
|
|
|
driver->addFile(*s, /*withLOption=*/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
|
|
|
else if (Optional<std::string> s = searchLibraryBaseName(specifier))
|
2019-07-16 12:46:31 +08:00
|
|
|
driver->addFile(*s, /*withLOption=*/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
|
|
|
else
|
|
|
|
error(toString(f) +
|
|
|
|
": unable to find library from dependent library specifier: " +
|
|
|
|
specifier);
|
|
|
|
}
|
|
|
|
|
2019-12-13 01:23:06 +08:00
|
|
|
// Record the membership of a section group so that in the garbage collection
|
|
|
|
// pass, section group members are kept or discarded as a unit.
|
2019-12-07 08:26:55 +08:00
|
|
|
template <class ELFT>
|
|
|
|
static void handleSectionGroup(ArrayRef<InputSectionBase *> sections,
|
|
|
|
ArrayRef<typename ELFT::Word> entries) {
|
|
|
|
bool hasAlloc = false;
|
|
|
|
for (uint32_t index : entries.slice(1)) {
|
|
|
|
if (index >= sections.size())
|
|
|
|
return;
|
|
|
|
if (InputSectionBase *s = sections[index])
|
|
|
|
if (s != &InputSection::discarded && s->flags & SHF_ALLOC)
|
|
|
|
hasAlloc = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If any member has the SHF_ALLOC flag, the whole group is subject to garbage
|
|
|
|
// collection. See the comment in markLive(). This rule retains .debug_types
|
|
|
|
// and .rela.debug_types.
|
|
|
|
if (!hasAlloc)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Connect the members in a circular doubly-linked list via
|
|
|
|
// nextInSectionGroup.
|
|
|
|
InputSectionBase *head;
|
|
|
|
InputSectionBase *prev = nullptr;
|
|
|
|
for (uint32_t index : entries.slice(1)) {
|
|
|
|
InputSectionBase *s = sections[index];
|
|
|
|
if (!s || s == &InputSection::discarded)
|
|
|
|
continue;
|
|
|
|
if (prev)
|
|
|
|
prev->nextInSectionGroup = s;
|
|
|
|
else
|
|
|
|
head = s;
|
|
|
|
prev = s;
|
|
|
|
}
|
|
|
|
if (prev)
|
|
|
|
prev->nextInSectionGroup = head;
|
|
|
|
}
|
|
|
|
|
2015-10-10 03:25:07 +08:00
|
|
|
template <class ELFT>
|
2019-06-06 01:39:37 +08:00
|
|
|
void ObjFile<ELFT>::initializeSections(bool ignoreComdats) {
|
2017-05-26 10:17:30 +08:00
|
|
|
const ELFFile<ELFT> &obj = this->getObj();
|
|
|
|
|
2018-05-14 21:21:09 +08:00
|
|
|
ArrayRef<Elf_Shdr> objSections = CHECK(obj.sections(), this);
|
2016-11-02 22:42:20 +08:00
|
|
|
uint64_t size = objSections.size();
|
2017-03-21 16:44:25 +08:00
|
|
|
this->sections.resize(size);
|
2017-06-13 02:46:33 +08:00
|
|
|
this->sectionStringTable =
|
2017-12-07 11:24:57 +08:00
|
|
|
CHECK(obj.getSectionStringTable(objSections), this);
|
2017-05-26 10:17:30 +08:00
|
|
|
|
2019-11-19 13:56:58 +08:00
|
|
|
std::vector<ArrayRef<Elf_Word>> selectedGroups;
|
|
|
|
|
2019-07-19 00:47:29 +08:00
|
|
|
for (size_t i = 0, e = objSections.size(); i < e; ++i) {
|
2017-03-21 16:44:25 +08:00
|
|
|
if (this->sections[i] == &InputSection::discarded)
|
2015-10-10 03:25:07 +08:00
|
|
|
continue;
|
2017-05-26 10:17:30 +08:00
|
|
|
const Elf_Shdr &sec = objSections[i];
|
2015-10-10 03:25:07 +08:00
|
|
|
|
2018-10-02 08:17:15 +08:00
|
|
|
if (sec.sh_type == ELF::SHT_LLVM_CALL_GRAPH_PROFILE)
|
2019-01-26 05:25:25 +08:00
|
|
|
cgProfile =
|
2020-09-09 22:03:53 +08:00
|
|
|
check(obj.template getSectionContentsAsArray<Elf_CGProfile>(sec));
|
2018-10-02 08:17:15 +08:00
|
|
|
|
2016-10-05 00:47:49 +08:00
|
|
|
// SHF_EXCLUDE'ed sections are discarded by the linker. However,
|
|
|
|
// if -r is given, we'll let the final link discard such sections.
|
|
|
|
// This is compatible with GNU.
|
|
|
|
if ((sec.sh_flags & SHF_EXCLUDE) && !config->relocatable) {
|
2018-07-19 06:49:31 +08:00
|
|
|
if (sec.sh_type == SHT_LLVM_ADDRSIG) {
|
|
|
|
// We ignore the address-significance table if we know that the object
|
|
|
|
// file was created by objcopy or ld -r. This is because these tools
|
|
|
|
// will reorder the symbols in the symbol table, invalidating the data
|
|
|
|
// in the address-significance table, which refers to symbols by index.
|
|
|
|
if (sec.sh_link != 0)
|
|
|
|
this->addrsigSec = &sec;
|
|
|
|
else if (config->icf == ICFLevel::Safe)
|
|
|
|
warn(toString(this) + ": --icf=safe is incompatible with object "
|
|
|
|
"files created using objcopy or ld -r");
|
|
|
|
}
|
2017-03-21 16:44:25 +08:00
|
|
|
this->sections[i] = &InputSection::discarded;
|
2016-09-28 16:42:02 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-08-13 22:45:44 +08:00
|
|
|
switch (sec.sh_type) {
|
2017-05-29 16:37:50 +08:00
|
|
|
case SHT_GROUP: {
|
2017-06-09 10:42:20 +08:00
|
|
|
// De-duplicate section groups by their signatures.
|
|
|
|
StringRef signature = getShtGroupSignature(objSections, sec);
|
|
|
|
this->sections[i] = &InputSection::discarded;
|
|
|
|
|
2018-08-15 20:20:38 +08:00
|
|
|
|
2019-01-25 01:56:08 +08:00
|
|
|
ArrayRef<Elf_Word> entries =
|
2020-09-09 22:03:53 +08:00
|
|
|
CHECK(obj.template getSectionContentsAsArray<Elf_Word>(sec), this);
|
2019-01-25 01:56:08 +08:00
|
|
|
if (entries.empty())
|
|
|
|
fatal(toString(this) + ": empty SHT_GROUP");
|
|
|
|
|
|
|
|
// The first word of a SHT_GROUP section contains flags. Currently,
|
|
|
|
// the standard defines only "GRP_COMDAT" flag for the COMDAT group.
|
|
|
|
// An group with the empty flag doesn't define anything; such sections
|
|
|
|
// are just skipped.
|
|
|
|
if (entries[0] == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (entries[0] != GRP_COMDAT)
|
|
|
|
fatal(toString(this) + ": unsupported SHT_GROUP format");
|
|
|
|
|
2019-05-22 17:06:42 +08:00
|
|
|
bool isNew =
|
2019-06-06 01:39:37 +08:00
|
|
|
ignoreComdats ||
|
|
|
|
symtab->comdatGroups.try_emplace(CachedHashStringRef(signature), this)
|
|
|
|
.second;
|
2017-06-09 10:42:20 +08:00
|
|
|
if (isNew) {
|
|
|
|
if (config->relocatable)
|
2017-06-13 02:46:33 +08:00
|
|
|
this->sections[i] = createInputSection(sec);
|
2019-11-19 13:56:58 +08:00
|
|
|
selectedGroups.push_back(entries);
|
2019-01-25 02:17:17 +08:00
|
|
|
continue;
|
2017-06-09 10:42:20 +08:00
|
|
|
}
|
2017-05-29 16:37:50 +08:00
|
|
|
|
2017-06-09 10:42:20 +08:00
|
|
|
// Otherwise, discard group members.
|
2019-01-25 01:56:08 +08:00
|
|
|
for (uint32_t secIndex : entries.slice(1)) {
|
2015-10-10 03:25:07 +08:00
|
|
|
if (secIndex >= size)
|
2017-03-21 16:44:25 +08:00
|
|
|
fatal(toString(this) +
|
|
|
|
": invalid section index in group: " + Twine(secIndex));
|
|
|
|
this->sections[secIndex] = &InputSection::discarded;
|
2015-10-10 03:25:07 +08:00
|
|
|
}
|
|
|
|
break;
|
2017-05-29 16:37:50 +08:00
|
|
|
}
|
2016-03-04 00:21:44 +08:00
|
|
|
case SHT_SYMTAB_SHNDX:
|
2019-04-10 14:32:05 +08:00
|
|
|
shndxTable = CHECK(obj.getSHNDXTable(sec, objSections), this);
|
2015-08-25 05:43:25 +08:00
|
|
|
break;
|
2019-05-28 13:17:21 +08:00
|
|
|
case SHT_SYMTAB:
|
2015-08-13 22:45:44 +08:00
|
|
|
case SHT_STRTAB:
|
2020-07-09 18:08:13 +08:00
|
|
|
case SHT_REL:
|
|
|
|
case SHT_RELA:
|
2015-08-13 22:45:44 +08:00
|
|
|
case SHT_NULL:
|
2015-08-28 07:15:56 +08:00
|
|
|
break;
|
2015-11-22 06:19:32 +08:00
|
|
|
default:
|
2017-06-13 02:46:33 +08:00
|
|
|
this->sections[i] = createInputSection(sec);
|
2015-07-25 05:03:07 +08:00
|
|
|
}
|
2019-07-19 00:47:29 +08:00
|
|
|
}
|
|
|
|
|
2020-07-09 18:08:13 +08:00
|
|
|
// We have a second loop. It is used to:
|
|
|
|
// 1) handle SHF_LINK_ORDER sections.
|
|
|
|
// 2) create SHT_REL[A] sections. In some cases the section header index of a
|
|
|
|
// relocation section may be smaller than that of the relocated section. In
|
|
|
|
// such cases, the relocation section would attempt to reference a target
|
|
|
|
// section that has not yet been created. For simplicity, delay creation of
|
|
|
|
// relocation sections until now.
|
2019-07-19 00:47:29 +08:00
|
|
|
for (size_t i = 0, e = objSections.size(); i < e; ++i) {
|
|
|
|
if (this->sections[i] == &InputSection::discarded)
|
|
|
|
continue;
|
|
|
|
const Elf_Shdr &sec = objSections[i];
|
2020-07-09 18:08:13 +08:00
|
|
|
|
|
|
|
if (sec.sh_type == SHT_REL || sec.sh_type == SHT_RELA)
|
|
|
|
this->sections[i] = createInputSection(sec);
|
|
|
|
|
[ELF] Allow SHF_LINK_ORDER sections to have sh_link=0
Part of https://bugs.llvm.org/show_bug.cgi?id=41734
The semantics of SHF_LINK_ORDER have been extended to represent metadata
sections associated with some other sections (usually text).
The associated text section may be discarded (e.g. LTO) and we want the
metadata section to have sh_link=0 (D72899, D76802).
Normally the metadata section is only referenced by the associated text
section. sh_link=0 means the associated text section is discarded, and
the metadata section will be garbage collected. If there is another
section (.gc_root) referencing the metadata section, the metadata
section will be retained. It's the .gc_root consumer's job to validate
the metadata sections.
# This creates a SHF_LINK_ORDER .meta with sh_link=0
.section .meta,"awo",@progbits,0
1:
.section .meta,"awo",@progbits,foo
2:
.section .gc_root,"a",@progbits
.quad 1b
.quad 2b
Reviewed By: pcc, jhenderson
Differential Revision: https://reviews.llvm.org/D72904
2020-08-06 07:09:41 +08:00
|
|
|
// A SHF_LINK_ORDER section with sh_link=0 is handled as if it did not have
|
|
|
|
// the flag.
|
|
|
|
if (!(sec.sh_flags & SHF_LINK_ORDER) || !sec.sh_link)
|
2019-07-19 00:47:29 +08:00
|
|
|
continue;
|
2015-07-25 05:03:07 +08:00
|
|
|
|
2019-07-19 00:47:29 +08:00
|
|
|
InputSectionBase *linkSec = nullptr;
|
|
|
|
if (sec.sh_link < this->sections.size())
|
|
|
|
linkSec = this->sections[sec.sh_link];
|
|
|
|
if (!linkSec)
|
|
|
|
fatal(toString(this) + ": invalid sh_link index: " + Twine(sec.sh_link));
|
|
|
|
|
[ELF] Allow SHF_LINK_ORDER sections to have sh_link=0
Part of https://bugs.llvm.org/show_bug.cgi?id=41734
The semantics of SHF_LINK_ORDER have been extended to represent metadata
sections associated with some other sections (usually text).
The associated text section may be discarded (e.g. LTO) and we want the
metadata section to have sh_link=0 (D72899, D76802).
Normally the metadata section is only referenced by the associated text
section. sh_link=0 means the associated text section is discarded, and
the metadata section will be garbage collected. If there is another
section (.gc_root) referencing the metadata section, the metadata
section will be retained. It's the .gc_root consumer's job to validate
the metadata sections.
# This creates a SHF_LINK_ORDER .meta with sh_link=0
.section .meta,"awo",@progbits,0
1:
.section .meta,"awo",@progbits,foo
2:
.section .gc_root,"a",@progbits
.quad 1b
.quad 2b
Reviewed By: pcc, jhenderson
Differential Revision: https://reviews.llvm.org/D72904
2020-08-06 07:09:41 +08:00
|
|
|
// A SHF_LINK_ORDER section is discarded if its linked-to section is
|
|
|
|
// discarded.
|
2019-07-19 00:47:29 +08:00
|
|
|
InputSection *isec = cast<InputSection>(this->sections[i]);
|
|
|
|
linkSec->dependentSections.push_back(isec);
|
|
|
|
if (!isa<InputSection>(linkSec))
|
|
|
|
error("a section " + isec->name +
|
|
|
|
" with SHF_LINK_ORDER should not refer a non-regular section: " +
|
|
|
|
toString(linkSec));
|
2016-10-10 18:10:27 +08:00
|
|
|
}
|
2019-11-19 13:56:58 +08:00
|
|
|
|
2019-12-07 08:26:55 +08:00
|
|
|
for (ArrayRef<Elf_Word> entries : selectedGroups)
|
|
|
|
handleSectionGroup<ELFT>(this->sections, entries);
|
2016-10-10 18:10:27 +08:00
|
|
|
}
|
|
|
|
|
2018-07-31 21:41:59 +08:00
|
|
|
// For ARM only, to set the EF_ARM_ABI_FLOAT_SOFT or EF_ARM_ABI_FLOAT_HARD
|
|
|
|
// flag in the ELF Header we need to look at Tag_ABI_VFP_args to find out how
|
|
|
|
// the input objects have been compiled.
|
|
|
|
static void updateARMVFPArgs(const ARMAttributeParser &attributes,
|
|
|
|
const InputFile *f) {
|
2020-02-04 22:20:10 +08:00
|
|
|
Optional<unsigned> attr =
|
|
|
|
attributes.getAttributeValue(ARMBuildAttrs::ABI_VFP_args);
|
|
|
|
if (!attr.hasValue())
|
2018-07-31 21:41:59 +08:00
|
|
|
// If an ABI tag isn't present then it is implicitly given the value of 0
|
|
|
|
// which maps to ARMBuildAttrs::BaseAAPCS. However many assembler files,
|
|
|
|
// including some in glibc that don't use FP args (and should have value 3)
|
|
|
|
// don't have the attribute so we do not consider an implicit value of 0
|
|
|
|
// as a clash.
|
|
|
|
return;
|
|
|
|
|
2020-02-04 22:20:10 +08:00
|
|
|
unsigned vfpArgs = attr.getValue();
|
2018-07-31 21:41:59 +08:00
|
|
|
ARMVFPArgKind arg;
|
|
|
|
switch (vfpArgs) {
|
|
|
|
case ARMBuildAttrs::BaseAAPCS:
|
|
|
|
arg = ARMVFPArgKind::Base;
|
|
|
|
break;
|
|
|
|
case ARMBuildAttrs::HardFPAAPCS:
|
|
|
|
arg = ARMVFPArgKind::VFP;
|
|
|
|
break;
|
|
|
|
case ARMBuildAttrs::ToolChainFPPCS:
|
|
|
|
// Tool chain specific convention that conforms to neither AAPCS variant.
|
|
|
|
arg = ARMVFPArgKind::ToolChain;
|
|
|
|
break;
|
|
|
|
case ARMBuildAttrs::CompatibleFPAAPCS:
|
|
|
|
// Object compatible with all conventions.
|
|
|
|
return;
|
|
|
|
default:
|
|
|
|
error(toString(f) + ": unknown Tag_ABI_VFP_args value: " + Twine(vfpArgs));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Follow ld.bfd and error if there is a mix of calling conventions.
|
|
|
|
if (config->armVFPArgs != arg && config->armVFPArgs != ARMVFPArgKind::Default)
|
|
|
|
error(toString(f) + ": incompatible Tag_ABI_VFP_args");
|
|
|
|
else
|
|
|
|
config->armVFPArgs = arg;
|
|
|
|
}
|
|
|
|
|
2017-11-28 21:51:48 +08:00
|
|
|
// The ARM support in lld makes some use of instructions that are not available
|
|
|
|
// on all ARM architectures. Namely:
|
|
|
|
// - Use of BLX instruction for interworking between ARM and Thumb state.
|
|
|
|
// - Use of the extended Thumb branch encoding in relocation.
|
|
|
|
// - Use of the MOVT/MOVW instructions in Thumb Thunks.
|
|
|
|
// The ARM Attributes section contains information about the architecture chosen
|
|
|
|
// at compile time. We follow the convention that if at least one input object
|
|
|
|
// is compiled with an architecture that supports these features then lld is
|
|
|
|
// permitted to use them.
|
|
|
|
static void updateSupportedARMFeatures(const ARMAttributeParser &attributes) {
|
2020-02-04 22:20:10 +08:00
|
|
|
Optional<unsigned> attr =
|
|
|
|
attributes.getAttributeValue(ARMBuildAttrs::CPU_arch);
|
|
|
|
if (!attr.hasValue())
|
2017-11-28 21:51:48 +08:00
|
|
|
return;
|
2020-02-04 22:20:10 +08:00
|
|
|
auto arch = attr.getValue();
|
2017-11-28 21:51:48 +08:00
|
|
|
switch (arch) {
|
|
|
|
case ARMBuildAttrs::Pre_v4:
|
|
|
|
case ARMBuildAttrs::v4:
|
|
|
|
case ARMBuildAttrs::v4T:
|
|
|
|
// Architectures prior to v5 do not support BLX instruction
|
|
|
|
break;
|
|
|
|
case ARMBuildAttrs::v5T:
|
|
|
|
case ARMBuildAttrs::v5TE:
|
|
|
|
case ARMBuildAttrs::v5TEJ:
|
|
|
|
case ARMBuildAttrs::v6:
|
|
|
|
case ARMBuildAttrs::v6KZ:
|
|
|
|
case ARMBuildAttrs::v6K:
|
|
|
|
config->armHasBlx = true;
|
|
|
|
// Architectures used in pre-Cortex processors do not support
|
|
|
|
// The J1 = 1 J2 = 1 Thumb branch range extension, with the exception
|
|
|
|
// of Architecture v6T2 (arm1156t2-s and arm1156t2f-s) that do.
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// All other Architectures have BLX and extended branch encoding
|
|
|
|
config->armHasBlx = true;
|
|
|
|
config->armJ1J2BranchEncoding = true;
|
|
|
|
if (arch != ARMBuildAttrs::v6_M && arch != ARMBuildAttrs::v6S_M)
|
|
|
|
// All Architectures used in Cortex processors with the exception
|
|
|
|
// of v6-M and v6S-M have the MOVT and MOVW instructions.
|
|
|
|
config->armHasMovtMovw = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-05 11:04:46 +08:00
|
|
|
// If a source file is compiled with x86 hardware-assisted call flow control
|
|
|
|
// enabled, the generated object file contains feature flags indicating that
|
|
|
|
// fact. This function reads the feature flags and returns it.
|
|
|
|
//
|
|
|
|
// Essentially we want to read a single 32-bit value in this function, but this
|
|
|
|
// function is rather complicated because the value is buried deep inside a
|
|
|
|
// .note.gnu.property section.
|
|
|
|
//
|
|
|
|
// The section consists of one or more NOTE records. Each NOTE record consists
|
|
|
|
// of zero or more type-length-value fields. We want to find a field of a
|
|
|
|
// certain type. It seems a bit too much to just store a 32-bit value, perhaps
|
|
|
|
// the ABI is unnecessarily complicated.
|
2020-08-25 23:05:38 +08:00
|
|
|
template <class ELFT> static uint32_t readAndFeatures(const InputSection &sec) {
|
2019-06-05 11:04:46 +08:00
|
|
|
using Elf_Nhdr = typename ELFT::Nhdr;
|
|
|
|
using Elf_Note = typename ELFT::Note;
|
|
|
|
|
2019-06-05 17:31:45 +08:00
|
|
|
uint32_t featuresSet = 0;
|
2020-08-25 23:05:38 +08:00
|
|
|
ArrayRef<uint8_t> data = sec.data();
|
|
|
|
auto reportFatal = [&](const uint8_t *place, const char *msg) {
|
|
|
|
fatal(toString(sec.file) + ":(" + sec.name + "+0x" +
|
|
|
|
Twine::utohexstr(place - sec.data().data()) + "): " + msg);
|
|
|
|
};
|
2019-06-05 11:04:46 +08:00
|
|
|
while (!data.empty()) {
|
|
|
|
// Read one NOTE record.
|
|
|
|
auto *nhdr = reinterpret_cast<const Elf_Nhdr *>(data.data());
|
2020-08-25 23:05:38 +08:00
|
|
|
if (data.size() < sizeof(Elf_Nhdr) || data.size() < nhdr->getSize())
|
|
|
|
reportFatal(data.data(), "data is too short");
|
2019-06-05 11:04:46 +08:00
|
|
|
|
|
|
|
Elf_Note note(*nhdr);
|
|
|
|
if (nhdr->n_type != NT_GNU_PROPERTY_TYPE_0 || note.getName() != "GNU") {
|
|
|
|
data = data.slice(nhdr->getSize());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-06-07 21:00:17 +08:00
|
|
|
uint32_t featureAndType = config->emachine == EM_AARCH64
|
|
|
|
? GNU_PROPERTY_AARCH64_FEATURE_1_AND
|
|
|
|
: GNU_PROPERTY_X86_FEATURE_1_AND;
|
|
|
|
|
2019-06-05 11:04:46 +08:00
|
|
|
// Read a body of a NOTE record, which consists of type-length-value fields.
|
|
|
|
ArrayRef<uint8_t> desc = note.getDesc();
|
|
|
|
while (!desc.empty()) {
|
2020-08-25 23:05:38 +08:00
|
|
|
const uint8_t *place = desc.data();
|
2019-06-05 11:04:46 +08:00
|
|
|
if (desc.size() < 8)
|
2020-08-25 23:05:38 +08:00
|
|
|
reportFatal(place, "program property is too short");
|
|
|
|
uint32_t type = read32<ELFT::TargetEndianness>(desc.data());
|
|
|
|
uint32_t size = read32<ELFT::TargetEndianness>(desc.data() + 4);
|
|
|
|
desc = desc.slice(8);
|
|
|
|
if (desc.size() < size)
|
|
|
|
reportFatal(place, "program property is too short");
|
2019-06-05 11:04:46 +08:00
|
|
|
|
2019-06-07 21:00:17 +08:00
|
|
|
if (type == featureAndType) {
|
2019-06-05 17:31:45 +08:00
|
|
|
// We found a FEATURE_1_AND field. There may be more than one of these
|
2019-10-29 09:41:38 +08:00
|
|
|
// in a .note.gnu.property section, for a relocatable object we
|
2019-06-05 17:31:45 +08:00
|
|
|
// accumulate the bits set.
|
2020-08-25 23:05:38 +08:00
|
|
|
if (size < 4)
|
|
|
|
reportFatal(place, "FEATURE_1_AND entry is too short");
|
|
|
|
featuresSet |= read32<ELFT::TargetEndianness>(desc.data());
|
2019-06-05 11:04:46 +08:00
|
|
|
}
|
|
|
|
|
2020-08-25 23:05:38 +08:00
|
|
|
// Padding is present in the note descriptor, if necessary.
|
|
|
|
desc = desc.slice(alignTo<(ELFT::Is64Bits ? 8 : 4)>(size));
|
2019-06-05 11:04:46 +08:00
|
|
|
}
|
|
|
|
|
2019-06-05 17:31:45 +08:00
|
|
|
// Go to next NOTE record to look for more FEATURE_1_AND descriptions.
|
2019-06-05 11:04:46 +08:00
|
|
|
data = data.slice(nhdr->getSize());
|
|
|
|
}
|
|
|
|
|
2019-06-05 17:31:45 +08:00
|
|
|
return featuresSet;
|
2019-06-05 11:04:46 +08:00
|
|
|
}
|
|
|
|
|
2016-03-14 05:52:57 +08:00
|
|
|
template <class ELFT>
|
2017-07-27 06:13:32 +08:00
|
|
|
InputSectionBase *ObjFile<ELFT>::getRelocTarget(const Elf_Shdr &sec) {
|
2016-03-14 05:52:57 +08:00
|
|
|
uint32_t idx = sec.sh_info;
|
2017-03-21 16:44:25 +08:00
|
|
|
if (idx >= this->sections.size())
|
2016-11-24 02:07:33 +08:00
|
|
|
fatal(toString(this) + ": invalid relocated section index: " + Twine(idx));
|
2017-03-21 16:44:25 +08:00
|
|
|
InputSectionBase *target = this->sections[idx];
|
2016-03-14 05:52:57 +08:00
|
|
|
|
|
|
|
// Strictly speaking, a relocation section must be included in the
|
|
|
|
// group of the section it relocates. However, LLVM 3.3 and earlier
|
|
|
|
// would fail to do so, so we gracefully handle that case.
|
2017-02-24 00:49:07 +08:00
|
|
|
if (target == &InputSection::discarded)
|
2016-03-14 05:52:57 +08:00
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
if (!target)
|
2016-11-24 02:07:33 +08:00
|
|
|
fatal(toString(this) + ": unsupported relocation reference");
|
2016-03-14 05:52:57 +08:00
|
|
|
return target;
|
|
|
|
}
|
|
|
|
|
2017-05-02 04:49:09 +08:00
|
|
|
// Create a regular InputSection class that has the same contents
|
|
|
|
// as a given section.
|
2017-12-21 10:11:51 +08:00
|
|
|
static InputSection *toRegularSection(MergeInputSection *sec) {
|
|
|
|
return make<InputSection>(sec->file, sec->flags, sec->type, sec->alignment,
|
Avoid unnecessary buffer allocation and memcpy for compressed sections.
Previously, we uncompress all compressed sections before doing anything.
That works, and that is conceptually simple, but that could results in
a waste of CPU time and memory if uncompressed sections are then
discarded or just copied to the output buffer.
In particular, if .debug_gnu_pub{names,types} are compressed and if no
-gdb-index option is given, we wasted CPU and memory because we
uncompress them into newly allocated bufers and then memcpy the buffers
to the output buffer. That temporary buffer was redundant.
This patch changes how to uncompress sections. Now, compressed sections
are uncompressed lazily. To do that, `Data` member of `InputSectionBase`
is now hidden from outside, and `data()` accessor automatically expands
an compressed buffer if necessary.
If no one calls `data()`, then `writeTo()` directly uncompresses
compressed data into the output buffer. That eliminates the redundant
memory allocation and redundant memcpy.
This patch significantly reduces memory consumption (20 GiB max RSS to
15 Gib) for an executable whose .debug_gnu_pub{names,types} are in total
5 GiB in an uncompressed form.
Differential Revision: https://reviews.llvm.org/D52917
llvm-svn: 343979
2018-10-09 00:58:59 +08:00
|
|
|
sec->data(), sec->name);
|
2017-05-02 04:49:09 +08:00
|
|
|
}
|
|
|
|
|
2016-02-13 05:17:10 +08:00
|
|
|
template <class ELFT>
|
2017-07-27 06:13:32 +08:00
|
|
|
InputSectionBase *ObjFile<ELFT>::createInputSection(const Elf_Shdr &sec) {
|
2017-06-13 02:46:33 +08:00
|
|
|
StringRef name = getSectionName(sec);
|
2015-12-24 16:41:12 +08:00
|
|
|
|
2020-08-21 01:10:18 +08:00
|
|
|
if (config->emachine == EM_ARM && sec.sh_type == SHT_ARM_ATTRIBUTES) {
|
2017-11-28 21:51:48 +08:00
|
|
|
ARMAttributeParser attributes;
|
2020-09-09 22:03:53 +08:00
|
|
|
ArrayRef<uint8_t> contents = check(this->getObj().getSectionContents(sec));
|
2020-02-22 03:32:33 +08:00
|
|
|
if (Error e = attributes.parse(contents, config->ekind == ELF32LEKind
|
|
|
|
? support::little
|
|
|
|
: support::big)) {
|
|
|
|
auto *isec = make<InputSection>(*this, sec, name);
|
|
|
|
warn(toString(isec) + ": " + llvm::toString(std::move(e)));
|
2020-08-21 01:10:18 +08:00
|
|
|
} else {
|
|
|
|
updateSupportedARMFeatures(attributes);
|
|
|
|
updateARMVFPArgs(attributes, this);
|
|
|
|
|
|
|
|
// FIXME: Retain the first attribute section we see. The eglibc ARM
|
|
|
|
// dynamic loaders require the presence of an attribute section for dlopen
|
|
|
|
// to work. In a full implementation we would merge all attribute
|
|
|
|
// sections.
|
|
|
|
if (in.attributes == nullptr) {
|
|
|
|
in.attributes = make<InputSection>(*this, sec, name);
|
|
|
|
return in.attributes;
|
|
|
|
}
|
|
|
|
return &InputSection::discarded;
|
2020-02-22 03:32:33 +08:00
|
|
|
}
|
2020-08-21 01:10:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (config->emachine == EM_RISCV && sec.sh_type == SHT_RISCV_ATTRIBUTES) {
|
|
|
|
RISCVAttributeParser attributes;
|
2020-09-09 22:03:53 +08:00
|
|
|
ArrayRef<uint8_t> contents = check(this->getObj().getSectionContents(sec));
|
2020-08-21 01:10:18 +08:00
|
|
|
if (Error e = attributes.parse(contents, support::little)) {
|
|
|
|
auto *isec = make<InputSection>(*this, sec, name);
|
|
|
|
warn(toString(isec) + ": " + llvm::toString(std::move(e)));
|
|
|
|
} else {
|
|
|
|
// FIXME: Validate arch tag contains C if and only if EF_RISCV_RVC is
|
|
|
|
// present.
|
|
|
|
|
|
|
|
// FIXME: Retain the first attribute section we see. Tools such as
|
|
|
|
// llvm-objdump make use of the attribute section to determine which
|
|
|
|
// standard extensions to enable. In a full implementation we would merge
|
|
|
|
// all attribute sections.
|
|
|
|
if (in.attributes == nullptr) {
|
|
|
|
in.attributes = make<InputSection>(*this, sec, name);
|
|
|
|
return in.attributes;
|
|
|
|
}
|
|
|
|
return &InputSection::discarded;
|
2016-12-14 18:36:12 +08:00
|
|
|
}
|
2017-11-28 21:51:48 +08:00
|
|
|
}
|
2020-08-21 01:10:18 +08:00
|
|
|
|
|
|
|
switch (sec.sh_type) {
|
[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
|
|
|
case SHT_LLVM_DEPENDENT_LIBRARIES: {
|
|
|
|
if (config->relocatable)
|
|
|
|
break;
|
|
|
|
ArrayRef<char> data =
|
2020-09-09 22:03:53 +08:00
|
|
|
CHECK(this->getObj().template getSectionContentsAsArray<char>(sec), this);
|
[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
|
|
|
if (!data.empty() && data.back() != '\0') {
|
|
|
|
error(toString(this) +
|
|
|
|
": corrupted dependent libraries section (unterminated string): " +
|
|
|
|
name);
|
|
|
|
return &InputSection::discarded;
|
|
|
|
}
|
|
|
|
for (const char *d = data.begin(), *e = data.end(); d < e;) {
|
|
|
|
StringRef s(d);
|
|
|
|
addDependentLibrary(s, this);
|
|
|
|
d += s.size() + 1;
|
|
|
|
}
|
|
|
|
return &InputSection::discarded;
|
|
|
|
}
|
2016-09-08 22:06:08 +08:00
|
|
|
case SHT_RELA:
|
|
|
|
case SHT_REL: {
|
2018-03-27 10:52:58 +08:00
|
|
|
// Find a relocation target section and associate this section with that.
|
|
|
|
// Target may have been discarded if it is in a different section group
|
|
|
|
// and the group is discarded, even though it's a violation of the
|
|
|
|
// spec. We handle that situation gracefully by discarding dangling
|
|
|
|
// relocation sections.
|
2017-02-23 10:28:28 +08:00
|
|
|
InputSectionBase *target = getRelocTarget(sec);
|
2017-02-15 00:42:38 +08:00
|
|
|
if (!target)
|
|
|
|
return nullptr;
|
|
|
|
|
2019-10-25 01:52:19 +08:00
|
|
|
// ELF spec allows mergeable sections with relocations, but they are
|
|
|
|
// rare, and it is in practice hard to merge such sections by contents,
|
|
|
|
// because applying relocations at end of linking changes section
|
|
|
|
// contents. So, we simply handle such sections as non-mergeable ones.
|
|
|
|
// Degrading like this is acceptable because section merging is optional.
|
|
|
|
if (auto *ms = dyn_cast<MergeInputSection>(target)) {
|
|
|
|
target = toRegularSection(ms);
|
|
|
|
this->sections[sec.sh_info] = target;
|
|
|
|
}
|
|
|
|
|
2016-11-10 22:53:24 +08:00
|
|
|
if (target->firstRelocation)
|
2016-11-24 02:07:33 +08:00
|
|
|
fatal(toString(this) +
|
2016-11-10 22:53:24 +08:00
|
|
|
": multiple relocation sections to one section are not supported");
|
2017-05-02 04:49:09 +08:00
|
|
|
|
2016-11-10 22:53:24 +08:00
|
|
|
if (sec.sh_type == SHT_RELA) {
|
2020-09-09 22:03:53 +08:00
|
|
|
ArrayRef<Elf_Rela> rels = CHECK(getObj().relas(sec), this);
|
2016-11-10 22:53:24 +08:00
|
|
|
target->firstRelocation = rels.begin();
|
2018-03-27 10:53:08 +08:00
|
|
|
target->numRelocations = rels.size();
|
2016-11-10 22:53:24 +08:00
|
|
|
target->areRelocsRela = true;
|
|
|
|
} else {
|
2020-09-09 22:03:53 +08:00
|
|
|
ArrayRef<Elf_Rel> rels = CHECK(getObj().rels(sec), this);
|
2016-11-10 22:53:24 +08:00
|
|
|
target->firstRelocation = rels.begin();
|
2018-03-27 10:53:08 +08:00
|
|
|
target->numRelocations = rels.size();
|
2016-11-10 22:53:24 +08:00
|
|
|
target->areRelocsRela = false;
|
2016-09-08 22:06:08 +08:00
|
|
|
}
|
2018-03-27 10:53:08 +08:00
|
|
|
assert(isUInt<31>(target->numRelocations));
|
2017-02-09 00:18:10 +08:00
|
|
|
|
2020-07-23 23:16:01 +08:00
|
|
|
// Relocation sections are usually removed from the output, so return
|
|
|
|
// `nullptr` for the normal case. However, if -r or --emit-relocs is
|
|
|
|
// specified, we need to copy them to the output. (Some post link analysis
|
|
|
|
// tools specify --emit-relocs to obtain the information.)
|
|
|
|
if (!config->relocatable && !config->emitRelocs)
|
|
|
|
return nullptr;
|
|
|
|
InputSection *relocSec = make<InputSection>(*this, sec, name);
|
|
|
|
// If the relocated section is discarded (due to /DISCARD/ or
|
|
|
|
// --gc-sections), the relocation section should be discarded as well.
|
|
|
|
target->dependentSections.push_back(relocSec);
|
|
|
|
return relocSec;
|
2016-09-08 22:06:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-23 15:06:43 +08:00
|
|
|
// The GNU linker uses .note.GNU-stack section as a marker indicating
|
|
|
|
// that the code in the object file does not expect that the stack is
|
|
|
|
// executable (in terms of NX bit). If all input files have the marker,
|
|
|
|
// the GNU linker adds a PT_GNU_STACK segment to tells the loader to
|
2017-02-23 15:15:46 +08:00
|
|
|
// make the stack non-executable. Most object files have this section as
|
|
|
|
// of 2017.
|
2017-02-23 15:06:43 +08:00
|
|
|
//
|
|
|
|
// But making the stack non-executable is a norm today for security
|
2017-02-23 15:15:46 +08:00
|
|
|
// reasons. Failure to do so may result in a serious security issue.
|
|
|
|
// Therefore, we make LLD always add PT_GNU_STACK unless it is
|
2017-02-23 15:06:43 +08:00
|
|
|
// explicitly told to do otherwise (by -z execstack). Because the stack
|
|
|
|
// executable-ness is controlled solely by command line options,
|
|
|
|
// .note.GNU-stack sections are simply ignored.
|
2015-12-24 16:41:12 +08:00
|
|
|
if (name == ".note.GNU-stack")
|
2017-02-24 00:49:07 +08:00
|
|
|
return &InputSection::discarded;
|
2015-12-24 16:41:12 +08:00
|
|
|
|
2019-06-07 21:00:17 +08:00
|
|
|
// Object files that use processor features such as Intel Control-Flow
|
|
|
|
// Enforcement (CET) or AArch64 Branch Target Identification BTI, use a
|
|
|
|
// .note.gnu.property section containing a bitfield of feature bits like the
|
2019-06-05 11:04:46 +08:00
|
|
|
// GNU_PROPERTY_X86_FEATURE_1_IBT flag. Read a bitmap containing the flag.
|
|
|
|
//
|
|
|
|
// Since we merge bitmaps from multiple object files to create a new
|
|
|
|
// .note.gnu.property containing a single AND'ed bitmap, we discard an input
|
|
|
|
// file's .note.gnu.property section.
|
|
|
|
if (name == ".note.gnu.property") {
|
2020-08-25 23:05:38 +08:00
|
|
|
this->andFeatures = readAndFeatures<ELFT>(InputSection(*this, sec, name));
|
2019-06-05 11:04:46 +08:00
|
|
|
return &InputSection::discarded;
|
|
|
|
}
|
|
|
|
|
2018-07-18 07:16:02 +08:00
|
|
|
// Split stacks is a feature to support a discontiguous stack,
|
|
|
|
// commonly used in the programming language Go. For the details,
|
|
|
|
// see https://gcc.gnu.org/wiki/SplitStacks. An object file compiled
|
|
|
|
// for split stack will include a .note.GNU-split-stack section.
|
2016-04-08 05:04:51 +08:00
|
|
|
if (name == ".note.GNU-split-stack") {
|
2018-07-18 07:16:02 +08:00
|
|
|
if (config->relocatable) {
|
2018-09-26 01:12:50 +08:00
|
|
|
error("cannot mix split-stack and non-split-stack in a relocatable link");
|
2018-07-18 07:16:02 +08:00
|
|
|
return &InputSection::discarded;
|
|
|
|
}
|
|
|
|
this->splitStack = true;
|
|
|
|
return &InputSection::discarded;
|
|
|
|
}
|
|
|
|
|
|
|
|
// An object file cmpiled for split stack, but where some of the
|
|
|
|
// functions were compiled with the no_split_stack_attribute will
|
|
|
|
// include a .note.GNU-no-split-stack section.
|
|
|
|
if (name == ".note.GNU-no-split-stack") {
|
|
|
|
this->someNoSplitStack = true;
|
2017-02-24 00:49:07 +08:00
|
|
|
return &InputSection::discarded;
|
2016-04-08 05:04:51 +08:00
|
|
|
}
|
|
|
|
|
2017-01-10 04:26:33 +08:00
|
|
|
// The linkonce feature is a sort of proto-comdat. Some glibc i386 object
|
|
|
|
// files contain definitions of symbol "__x86.get_pc_thunk.bx" in linkonce
|
|
|
|
// sections. Drop those sections to avoid duplicate symbol errors.
|
|
|
|
// FIXME: This is glibc PR20543, we should remove this hack once that has been
|
|
|
|
// fixed for a while.
|
lld: elf: discard more specific .gnu.linkonce section
Summary:
lld discards .gnu.linonce.* sections work around a bug in glibc.
https://sourceware.org/bugzilla/show_bug.cgi?id=20543
Unfortunately, the Linux kernel uses a section named
.gnu.linkonce.this_module to store infomation about kernel modules. The
kernel reads data from this section when loading kernel modules, and
errors if it fails to find this section. The current behavior of lld
discards this section when kernel modules are linked, so kernel modules
linked with lld are unloadable by the linux kernel.
The Linux kernel should use a comdat section instead of .gnu.linkonce.
The minimum version of binutils supported by the kernel supports comdat
sections. The kernel is also not relying on the old linkonce behavior;
it seems to have chosen a name that contains a deprecated GNU feature.
Changing the section name now in the kernel would require all kernel
modules to be recompiled to make use of the new section name. Instead,
rather than discarding .gnu.linkonce.*, let's discard the more specific
section name to continue working around the glibc issue while supporting
linking Linux kernel modules.
Link: https://github.com/ClangBuiltLinux/linux/issues/329
Reviewers: pcc, espindola
Reviewed By: pcc
Subscribers: nathanchance, emaste, arichardson, void, srhines
Differential Revision: https://reviews.llvm.org/D57294
llvm-svn: 352302
2019-01-27 10:54:23 +08:00
|
|
|
if (name == ".gnu.linkonce.t.__x86.get_pc_thunk.bx" ||
|
|
|
|
name == ".gnu.linkonce.t.__i686.get_pc_thunk.bx")
|
2017-02-24 00:49:07 +08:00
|
|
|
return &InputSection::discarded;
|
2017-01-10 04:26:33 +08:00
|
|
|
|
2018-02-03 05:56:24 +08:00
|
|
|
// If we are creating a new .build-id section, strip existing .build-id
|
|
|
|
// sections so that the output won't have more than one .build-id.
|
|
|
|
// This is not usually a problem because input object files normally don't
|
|
|
|
// have .build-id sections, but you can create such files by
|
|
|
|
// "ld.{bfd,gold,lld} -r --build-id", and we want to guard against it.
|
|
|
|
if (name == ".note.gnu.build-id" && config->buildId != BuildIdKind::None)
|
|
|
|
return &InputSection::discarded;
|
|
|
|
|
2016-07-15 12:57:44 +08:00
|
|
|
// The linker merges EH (exception handling) frames and creates a
|
|
|
|
// .eh_frame_hdr section for runtime. So we handle them with a special
|
|
|
|
// class. For relocatable outputs, they are just passed through.
|
|
|
|
if (name == ".eh_frame" && !config->relocatable)
|
2017-12-21 10:03:39 +08:00
|
|
|
return make<EhInputSection>(*this, sec, name);
|
2016-07-15 12:57:44 +08:00
|
|
|
|
2019-10-10 16:32:12 +08:00
|
|
|
if (shouldMerge(sec, name))
|
2017-12-21 10:03:39 +08:00
|
|
|
return make<MergeInputSection>(*this, sec, name);
|
|
|
|
return make<InputSection>(*this, sec, name);
|
2015-12-24 16:41:12 +08:00
|
|
|
}
|
|
|
|
|
2017-06-13 02:46:33 +08:00
|
|
|
template <class ELFT>
|
2017-07-27 06:13:32 +08:00
|
|
|
StringRef ObjFile<ELFT>::getSectionName(const Elf_Shdr &sec) {
|
2020-09-09 22:03:53 +08:00
|
|
|
return CHECK(getObj().getSectionName(sec, sectionStringTable), this);
|
2017-06-13 02:46:33 +08:00
|
|
|
}
|
|
|
|
|
Speed up --start-lib and --end-lib.
--{start,end}-lib give files grouped by the options the archive file
semantics. That is, each object file between them acts as if it were
in an archive file whose sole member is the file.
Therefore, files between --{start,end}-lib are linked to the final
output only if they are needed to resolve some undefined symbols.
Previously, the feature was implemented this way:
1. We read a symbol table and insert defined symbols to the symbol
table as lazy symbols.
2. If an undefind symbol is resolved to a lazy symbol, that lazy
symbol instantiate ObjFile class for that symbol, which re-insert
all defined symbols to the symbol table.
So, if an ObjFile is instantiated, defined symbols are inserted to the
symbol table twice. Since inserting long symbol names is not cheap,
there's a room to optimize here.
This patch optimzies it. Now, LazyObjFile remembers symbol handles and
passed them over to a new ObjFile instance, so that the ObjFile
doesn't insert the same strings.
Here is a quick benchmark to link clang. "Original" is the original
lld with unmodified command line options. For "Case 1" and "Case 2", I
extracted all files from archive files and replace .a's in a command
line with .o's wrapped with --{start,end}-lib. I used the original lld
for Case 1" and use this patch for Case 2.
Original: 5.892
Case 1: 6.001 (+1.8%)
Case 2: 5.701 (-3.2%)
So, interestingly, --{start,end}-lib are now faster than the regular
linking scheme with archive files. That's perhaps not too surprising,
though, because for regular archive files, we look up the symbol table
with the same string twice.
Differential Revision: https://reviews.llvm.org/D62188
llvm-svn: 361473
2019-05-23 17:53:30 +08:00
|
|
|
// Initialize this->Symbols. this->Symbols is a parallel array as
|
|
|
|
// its corresponding ELF symbol table.
|
2017-07-27 06:13:32 +08:00
|
|
|
template <class ELFT> void ObjFile<ELFT>::initializeSymbols() {
|
Speed up --start-lib and --end-lib.
--{start,end}-lib give files grouped by the options the archive file
semantics. That is, each object file between them acts as if it were
in an archive file whose sole member is the file.
Therefore, files between --{start,end}-lib are linked to the final
output only if they are needed to resolve some undefined symbols.
Previously, the feature was implemented this way:
1. We read a symbol table and insert defined symbols to the symbol
table as lazy symbols.
2. If an undefind symbol is resolved to a lazy symbol, that lazy
symbol instantiate ObjFile class for that symbol, which re-insert
all defined symbols to the symbol table.
So, if an ObjFile is instantiated, defined symbols are inserted to the
symbol table twice. Since inserting long symbol names is not cheap,
there's a room to optimize here.
This patch optimzies it. Now, LazyObjFile remembers symbol handles and
passed them over to a new ObjFile instance, so that the ObjFile
doesn't insert the same strings.
Here is a quick benchmark to link clang. "Original" is the original
lld with unmodified command line options. For "Case 1" and "Case 2", I
extracted all files from archive files and replace .a's in a command
line with .o's wrapped with --{start,end}-lib. I used the original lld
for Case 1" and use this patch for Case 2.
Original: 5.892
Case 1: 6.001 (+1.8%)
Case 2: 5.701 (-3.2%)
So, interestingly, --{start,end}-lib are now faster than the regular
linking scheme with archive files. That's perhaps not too surprising,
though, because for regular archive files, we look up the symbol table
with the same string twice.
Differential Revision: https://reviews.llvm.org/D62188
llvm-svn: 361473
2019-05-23 17:53:30 +08:00
|
|
|
ArrayRef<Elf_Sym> eSyms = this->getELFSyms<ELFT>();
|
|
|
|
this->symbols.resize(eSyms.size());
|
|
|
|
|
2020-06-20 00:05:28 +08:00
|
|
|
// Fill in InputFile::symbols. Some entries have been initialized
|
Speed up --start-lib and --end-lib.
--{start,end}-lib give files grouped by the options the archive file
semantics. That is, each object file between them acts as if it were
in an archive file whose sole member is the file.
Therefore, files between --{start,end}-lib are linked to the final
output only if they are needed to resolve some undefined symbols.
Previously, the feature was implemented this way:
1. We read a symbol table and insert defined symbols to the symbol
table as lazy symbols.
2. If an undefind symbol is resolved to a lazy symbol, that lazy
symbol instantiate ObjFile class for that symbol, which re-insert
all defined symbols to the symbol table.
So, if an ObjFile is instantiated, defined symbols are inserted to the
symbol table twice. Since inserting long symbol names is not cheap,
there's a room to optimize here.
This patch optimzies it. Now, LazyObjFile remembers symbol handles and
passed them over to a new ObjFile instance, so that the ObjFile
doesn't insert the same strings.
Here is a quick benchmark to link clang. "Original" is the original
lld with unmodified command line options. For "Case 1" and "Case 2", I
extracted all files from archive files and replace .a's in a command
line with .o's wrapped with --{start,end}-lib. I used the original lld
for Case 1" and use this patch for Case 2.
Original: 5.892
Case 1: 6.001 (+1.8%)
Case 2: 5.701 (-3.2%)
So, interestingly, --{start,end}-lib are now faster than the regular
linking scheme with archive files. That's perhaps not too surprising,
though, because for regular archive files, we look up the symbol table
with the same string twice.
Differential Revision: https://reviews.llvm.org/D62188
llvm-svn: 361473
2019-05-23 17:53:30 +08:00
|
|
|
// because of LazyObjFile.
|
|
|
|
for (size_t i = 0, end = eSyms.size(); i != end; ++i) {
|
2020-06-20 00:05:28 +08:00
|
|
|
if (this->symbols[i])
|
|
|
|
continue;
|
Speed up --start-lib and --end-lib.
--{start,end}-lib give files grouped by the options the archive file
semantics. That is, each object file between them acts as if it were
in an archive file whose sole member is the file.
Therefore, files between --{start,end}-lib are linked to the final
output only if they are needed to resolve some undefined symbols.
Previously, the feature was implemented this way:
1. We read a symbol table and insert defined symbols to the symbol
table as lazy symbols.
2. If an undefind symbol is resolved to a lazy symbol, that lazy
symbol instantiate ObjFile class for that symbol, which re-insert
all defined symbols to the symbol table.
So, if an ObjFile is instantiated, defined symbols are inserted to the
symbol table twice. Since inserting long symbol names is not cheap,
there's a room to optimize here.
This patch optimzies it. Now, LazyObjFile remembers symbol handles and
passed them over to a new ObjFile instance, so that the ObjFile
doesn't insert the same strings.
Here is a quick benchmark to link clang. "Original" is the original
lld with unmodified command line options. For "Case 1" and "Case 2", I
extracted all files from archive files and replace .a's in a command
line with .o's wrapped with --{start,end}-lib. I used the original lld
for Case 1" and use this patch for Case 2.
Original: 5.892
Case 1: 6.001 (+1.8%)
Case 2: 5.701 (-3.2%)
So, interestingly, --{start,end}-lib are now faster than the regular
linking scheme with archive files. That's perhaps not too surprising,
though, because for regular archive files, we look up the symbol table
with the same string twice.
Differential Revision: https://reviews.llvm.org/D62188
llvm-svn: 361473
2019-05-23 17:53:30 +08:00
|
|
|
const Elf_Sym &eSym = eSyms[i];
|
|
|
|
uint32_t secIdx = getSectionIndex(eSym);
|
|
|
|
if (secIdx >= this->sections.size())
|
|
|
|
fatal(toString(this) + ": invalid section index: " + Twine(secIdx));
|
2020-06-20 00:05:28 +08:00
|
|
|
if (eSym.getBinding() != STB_LOCAL) {
|
|
|
|
if (i < firstGlobal)
|
|
|
|
error(toString(this) + ": non-local symbol (" + Twine(i) +
|
|
|
|
") found at index < .symtab's sh_info (" + Twine(firstGlobal) +
|
|
|
|
")");
|
|
|
|
this->symbols[i] =
|
|
|
|
symtab->insert(CHECK(eSyms[i].getName(this->stringTable), this));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle local symbols. Local symbols are not added to the symbol
|
|
|
|
// table because they are not visible from other object files. We
|
|
|
|
// allocate symbol instances and add their pointers to symbols.
|
|
|
|
if (i >= firstGlobal)
|
|
|
|
errorOrWarn(toString(this) + ": STB_LOCAL symbol (" + Twine(i) +
|
|
|
|
") found at index >= .symtab's sh_info (" +
|
|
|
|
Twine(firstGlobal) + ")");
|
[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
|
|
|
|
Speed up --start-lib and --end-lib.
--{start,end}-lib give files grouped by the options the archive file
semantics. That is, each object file between them acts as if it were
in an archive file whose sole member is the file.
Therefore, files between --{start,end}-lib are linked to the final
output only if they are needed to resolve some undefined symbols.
Previously, the feature was implemented this way:
1. We read a symbol table and insert defined symbols to the symbol
table as lazy symbols.
2. If an undefind symbol is resolved to a lazy symbol, that lazy
symbol instantiate ObjFile class for that symbol, which re-insert
all defined symbols to the symbol table.
So, if an ObjFile is instantiated, defined symbols are inserted to the
symbol table twice. Since inserting long symbol names is not cheap,
there's a room to optimize here.
This patch optimzies it. Now, LazyObjFile remembers symbol handles and
passed them over to a new ObjFile instance, so that the ObjFile
doesn't insert the same strings.
Here is a quick benchmark to link clang. "Original" is the original
lld with unmodified command line options. For "Case 1" and "Case 2", I
extracted all files from archive files and replace .a's in a command
line with .o's wrapped with --{start,end}-lib. I used the original lld
for Case 1" and use this patch for Case 2.
Original: 5.892
Case 1: 6.001 (+1.8%)
Case 2: 5.701 (-3.2%)
So, interestingly, --{start,end}-lib are now faster than the regular
linking scheme with archive files. That's perhaps not too surprising,
though, because for regular archive files, we look up the symbol table
with the same string twice.
Differential Revision: https://reviews.llvm.org/D62188
llvm-svn: 361473
2019-05-23 17:53:30 +08:00
|
|
|
InputSectionBase *sec = this->sections[secIdx];
|
2020-06-20 00:05:28 +08:00
|
|
|
uint8_t type = eSym.getType();
|
|
|
|
if (type == STT_FILE)
|
|
|
|
sourceFile = CHECK(eSym.getName(this->stringTable), this);
|
|
|
|
if (this->stringTable.size() <= eSym.st_name)
|
|
|
|
fatal(toString(this) + ": invalid symbol name offset");
|
|
|
|
StringRefZ name = this->stringTable.data() + eSym.st_name;
|
|
|
|
|
|
|
|
if (eSym.st_shndx == SHN_UNDEF)
|
|
|
|
this->symbols[i] =
|
|
|
|
make<Undefined>(this, name, STB_LOCAL, eSym.st_other, type);
|
|
|
|
else if (sec == &InputSection::discarded)
|
|
|
|
this->symbols[i] =
|
|
|
|
make<Undefined>(this, name, STB_LOCAL, eSym.st_other, type,
|
|
|
|
/*discardedSecIdx=*/secIdx);
|
|
|
|
else
|
|
|
|
this->symbols[i] = make<Defined>(this, name, STB_LOCAL, eSym.st_other,
|
|
|
|
type, eSym.st_value, eSym.st_size, sec);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Symbol resolution of non-local symbols.
|
|
|
|
for (size_t i = firstGlobal, end = eSyms.size(); i != end; ++i) {
|
|
|
|
const Elf_Sym &eSym = eSyms[i];
|
Speed up --start-lib and --end-lib.
--{start,end}-lib give files grouped by the options the archive file
semantics. That is, each object file between them acts as if it were
in an archive file whose sole member is the file.
Therefore, files between --{start,end}-lib are linked to the final
output only if they are needed to resolve some undefined symbols.
Previously, the feature was implemented this way:
1. We read a symbol table and insert defined symbols to the symbol
table as lazy symbols.
2. If an undefind symbol is resolved to a lazy symbol, that lazy
symbol instantiate ObjFile class for that symbol, which re-insert
all defined symbols to the symbol table.
So, if an ObjFile is instantiated, defined symbols are inserted to the
symbol table twice. Since inserting long symbol names is not cheap,
there's a room to optimize here.
This patch optimzies it. Now, LazyObjFile remembers symbol handles and
passed them over to a new ObjFile instance, so that the ObjFile
doesn't insert the same strings.
Here is a quick benchmark to link clang. "Original" is the original
lld with unmodified command line options. For "Case 1" and "Case 2", I
extracted all files from archive files and replace .a's in a command
line with .o's wrapped with --{start,end}-lib. I used the original lld
for Case 1" and use this patch for Case 2.
Original: 5.892
Case 1: 6.001 (+1.8%)
Case 2: 5.701 (-3.2%)
So, interestingly, --{start,end}-lib are now faster than the regular
linking scheme with archive files. That's perhaps not too surprising,
though, because for regular archive files, we look up the symbol table
with the same string twice.
Differential Revision: https://reviews.llvm.org/D62188
llvm-svn: 361473
2019-05-23 17:53:30 +08:00
|
|
|
uint8_t binding = eSym.getBinding();
|
2020-06-20 00:05:28 +08:00
|
|
|
if (binding == STB_LOCAL)
|
|
|
|
continue; // Errored above.
|
|
|
|
|
|
|
|
uint32_t secIdx = getSectionIndex(eSym);
|
|
|
|
InputSectionBase *sec = this->sections[secIdx];
|
Speed up --start-lib and --end-lib.
--{start,end}-lib give files grouped by the options the archive file
semantics. That is, each object file between them acts as if it were
in an archive file whose sole member is the file.
Therefore, files between --{start,end}-lib are linked to the final
output only if they are needed to resolve some undefined symbols.
Previously, the feature was implemented this way:
1. We read a symbol table and insert defined symbols to the symbol
table as lazy symbols.
2. If an undefind symbol is resolved to a lazy symbol, that lazy
symbol instantiate ObjFile class for that symbol, which re-insert
all defined symbols to the symbol table.
So, if an ObjFile is instantiated, defined symbols are inserted to the
symbol table twice. Since inserting long symbol names is not cheap,
there's a room to optimize here.
This patch optimzies it. Now, LazyObjFile remembers symbol handles and
passed them over to a new ObjFile instance, so that the ObjFile
doesn't insert the same strings.
Here is a quick benchmark to link clang. "Original" is the original
lld with unmodified command line options. For "Case 1" and "Case 2", I
extracted all files from archive files and replace .a's in a command
line with .o's wrapped with --{start,end}-lib. I used the original lld
for Case 1" and use this patch for Case 2.
Original: 5.892
Case 1: 6.001 (+1.8%)
Case 2: 5.701 (-3.2%)
So, interestingly, --{start,end}-lib are now faster than the regular
linking scheme with archive files. That's perhaps not too surprising,
though, because for regular archive files, we look up the symbol table
with the same string twice.
Differential Revision: https://reviews.llvm.org/D62188
llvm-svn: 361473
2019-05-23 17:53:30 +08:00
|
|
|
uint8_t stOther = eSym.st_other;
|
|
|
|
uint8_t type = eSym.getType();
|
|
|
|
uint64_t value = eSym.st_value;
|
|
|
|
uint64_t size = eSym.st_size;
|
|
|
|
StringRefZ name = this->stringTable.data() + eSym.st_name;
|
|
|
|
|
|
|
|
// Handle global undefined symbols.
|
|
|
|
if (eSym.st_shndx == SHN_UNDEF) {
|
2019-05-23 17:58:08 +08:00
|
|
|
this->symbols[i]->resolve(Undefined{this, name, binding, stOther, type});
|
[ELF] Make binding (weak or non-weak) logic consistent for Undefined and SharedSymbol
This is a case missed by D64136. If %t1.o has a weak reference on foo,
and %t2.so has a non-weak reference on foo:
```
0. ld.lld %t1.o %t2.so # ok; STB_WEAK; accepted since D64136
1. ld.lld %t2.so %t1.o # undefined symbol: foo; STB_GLOBAL
2. gold %t1.o %t2.so # ok; STB_WEAK
3. gold %t2.so %t1.o # undefined reference to 'foo'; STB_GLOBAL
4. ld.bfd %t1.o %t2.so # undefined reference to `foo'; STB_WEAK
5. ld.bfd %t2.so %t1.o # undefined reference to `foo'; STB_WEAK
```
It can be argued that in both cases, the binding of the undefined foo
should be set to STB_WEAK, because the binding should not be affected by
referenced from shared objects.
--allow-shlib-undefined doesn't suppress errors (3,4,5), but -shared or
--noinhibit-exec allows ld.bfd/gold to produce a binary:
```
3. gold -shared %t2.so %t1.o # ok; STB_GLOBAL
4. ld.bfd -shared %t2.so %t1.o # ok; STB_WEAK
5. ld.bfd -shared %t1.o %t1.o # ok; STB_WEAK
```
If %t2.so has DT_NEEDED entries, ld.bfd will load them (lld/gold don't
have the behavior). If one of the DSO defines foo and it is in the
link-time search path (e.g. DT_NEEDED entry is an absolute path, via
-rpath=, via -rpath-link=, etc),
`ld.bfd %t1.o %t2.so` and `ld.bfd %t1.o %t2.so` will not error.
In this patch, we make Undefined and SharedSymbol share the same binding
computing logic. Case 1 will be allowed:
```
0. ld.lld %t1.o %t2.so # ok; STB_WEAK; accepted since D64136
1. ld.lld %t2.so %t1.o # ok; STB_WEAK; changed by this patch
```
In the future, we can explore the option that turns both (0,1) into
errors if --no-allow-shlib-undefined (default when linking an
executable) is in action.
Reviewed By: ruiu
Differential Revision: https://reviews.llvm.org/D65584
llvm-svn: 368038
2019-08-06 22:03:45 +08:00
|
|
|
this->symbols[i]->referenced = true;
|
Speed up --start-lib and --end-lib.
--{start,end}-lib give files grouped by the options the archive file
semantics. That is, each object file between them acts as if it were
in an archive file whose sole member is the file.
Therefore, files between --{start,end}-lib are linked to the final
output only if they are needed to resolve some undefined symbols.
Previously, the feature was implemented this way:
1. We read a symbol table and insert defined symbols to the symbol
table as lazy symbols.
2. If an undefind symbol is resolved to a lazy symbol, that lazy
symbol instantiate ObjFile class for that symbol, which re-insert
all defined symbols to the symbol table.
So, if an ObjFile is instantiated, defined symbols are inserted to the
symbol table twice. Since inserting long symbol names is not cheap,
there's a room to optimize here.
This patch optimzies it. Now, LazyObjFile remembers symbol handles and
passed them over to a new ObjFile instance, so that the ObjFile
doesn't insert the same strings.
Here is a quick benchmark to link clang. "Original" is the original
lld with unmodified command line options. For "Case 1" and "Case 2", I
extracted all files from archive files and replace .a's in a command
line with .o's wrapped with --{start,end}-lib. I used the original lld
for Case 1" and use this patch for Case 2.
Original: 5.892
Case 1: 6.001 (+1.8%)
Case 2: 5.701 (-3.2%)
So, interestingly, --{start,end}-lib are now faster than the regular
linking scheme with archive files. That's perhaps not too surprising,
though, because for regular archive files, we look up the symbol table
with the same string twice.
Differential Revision: https://reviews.llvm.org/D62188
llvm-svn: 361473
2019-05-23 17:53:30 +08:00
|
|
|
continue;
|
|
|
|
}
|
2015-08-25 05:43:25 +08:00
|
|
|
|
Speed up --start-lib and --end-lib.
--{start,end}-lib give files grouped by the options the archive file
semantics. That is, each object file between them acts as if it were
in an archive file whose sole member is the file.
Therefore, files between --{start,end}-lib are linked to the final
output only if they are needed to resolve some undefined symbols.
Previously, the feature was implemented this way:
1. We read a symbol table and insert defined symbols to the symbol
table as lazy symbols.
2. If an undefind symbol is resolved to a lazy symbol, that lazy
symbol instantiate ObjFile class for that symbol, which re-insert
all defined symbols to the symbol table.
So, if an ObjFile is instantiated, defined symbols are inserted to the
symbol table twice. Since inserting long symbol names is not cheap,
there's a room to optimize here.
This patch optimzies it. Now, LazyObjFile remembers symbol handles and
passed them over to a new ObjFile instance, so that the ObjFile
doesn't insert the same strings.
Here is a quick benchmark to link clang. "Original" is the original
lld with unmodified command line options. For "Case 1" and "Case 2", I
extracted all files from archive files and replace .a's in a command
line with .o's wrapped with --{start,end}-lib. I used the original lld
for Case 1" and use this patch for Case 2.
Original: 5.892
Case 1: 6.001 (+1.8%)
Case 2: 5.701 (-3.2%)
So, interestingly, --{start,end}-lib are now faster than the regular
linking scheme with archive files. That's perhaps not too surprising,
though, because for regular archive files, we look up the symbol table
with the same string twice.
Differential Revision: https://reviews.llvm.org/D62188
llvm-svn: 361473
2019-05-23 17:53:30 +08:00
|
|
|
// Handle global common symbols.
|
|
|
|
if (eSym.st_shndx == SHN_COMMON) {
|
|
|
|
if (value == 0 || value >= UINT32_MAX)
|
|
|
|
fatal(toString(this) + ": common symbol '" + StringRef(name.data) +
|
|
|
|
"' has invalid alignment: " + Twine(value));
|
2019-05-23 17:58:08 +08:00
|
|
|
this->symbols[i]->resolve(
|
|
|
|
CommonSymbol{this, name, binding, stOther, type, value, size});
|
Speed up --start-lib and --end-lib.
--{start,end}-lib give files grouped by the options the archive file
semantics. That is, each object file between them acts as if it were
in an archive file whose sole member is the file.
Therefore, files between --{start,end}-lib are linked to the final
output only if they are needed to resolve some undefined symbols.
Previously, the feature was implemented this way:
1. We read a symbol table and insert defined symbols to the symbol
table as lazy symbols.
2. If an undefind symbol is resolved to a lazy symbol, that lazy
symbol instantiate ObjFile class for that symbol, which re-insert
all defined symbols to the symbol table.
So, if an ObjFile is instantiated, defined symbols are inserted to the
symbol table twice. Since inserting long symbol names is not cheap,
there's a room to optimize here.
This patch optimzies it. Now, LazyObjFile remembers symbol handles and
passed them over to a new ObjFile instance, so that the ObjFile
doesn't insert the same strings.
Here is a quick benchmark to link clang. "Original" is the original
lld with unmodified command line options. For "Case 1" and "Case 2", I
extracted all files from archive files and replace .a's in a command
line with .o's wrapped with --{start,end}-lib. I used the original lld
for Case 1" and use this patch for Case 2.
Original: 5.892
Case 1: 6.001 (+1.8%)
Case 2: 5.701 (-3.2%)
So, interestingly, --{start,end}-lib are now faster than the regular
linking scheme with archive files. That's perhaps not too surprising,
though, because for regular archive files, we look up the symbol table
with the same string twice.
Differential Revision: https://reviews.llvm.org/D62188
llvm-svn: 361473
2019-05-23 17:53:30 +08:00
|
|
|
continue;
|
|
|
|
}
|
2019-05-22 12:56:25 +08:00
|
|
|
|
Speed up --start-lib and --end-lib.
--{start,end}-lib give files grouped by the options the archive file
semantics. That is, each object file between them acts as if it were
in an archive file whose sole member is the file.
Therefore, files between --{start,end}-lib are linked to the final
output only if they are needed to resolve some undefined symbols.
Previously, the feature was implemented this way:
1. We read a symbol table and insert defined symbols to the symbol
table as lazy symbols.
2. If an undefind symbol is resolved to a lazy symbol, that lazy
symbol instantiate ObjFile class for that symbol, which re-insert
all defined symbols to the symbol table.
So, if an ObjFile is instantiated, defined symbols are inserted to the
symbol table twice. Since inserting long symbol names is not cheap,
there's a room to optimize here.
This patch optimzies it. Now, LazyObjFile remembers symbol handles and
passed them over to a new ObjFile instance, so that the ObjFile
doesn't insert the same strings.
Here is a quick benchmark to link clang. "Original" is the original
lld with unmodified command line options. For "Case 1" and "Case 2", I
extracted all files from archive files and replace .a's in a command
line with .o's wrapped with --{start,end}-lib. I used the original lld
for Case 1" and use this patch for Case 2.
Original: 5.892
Case 1: 6.001 (+1.8%)
Case 2: 5.701 (-3.2%)
So, interestingly, --{start,end}-lib are now faster than the regular
linking scheme with archive files. That's perhaps not too surprising,
though, because for regular archive files, we look up the symbol table
with the same string twice.
Differential Revision: https://reviews.llvm.org/D62188
llvm-svn: 361473
2019-05-23 17:53:30 +08:00
|
|
|
// If a defined symbol is in a discarded section, handle it as if it
|
|
|
|
// were an undefined symbol. Such symbol doesn't comply with the
|
|
|
|
// standard, but in practice, a .eh_frame often directly refer
|
|
|
|
// COMDAT member sections, and if a comdat group is discarded, some
|
|
|
|
// defined symbol in a .eh_frame becomes dangling symbols.
|
|
|
|
if (sec == &InputSection::discarded) {
|
2020-06-10 02:25:55 +08:00
|
|
|
Undefined und{this, name, binding, stOther, type, secIdx};
|
|
|
|
Symbol *sym = this->symbols[i];
|
|
|
|
// !ArchiveFile::parsed or LazyObjFile::fetched means that the file
|
|
|
|
// containing this object has not finished processing, i.e. this symbol is
|
|
|
|
// a result of a lazy symbol fetch. We should demote the lazy symbol to an
|
|
|
|
// Undefined so that any relocations outside of the group to it will
|
|
|
|
// trigger a discarded section error.
|
|
|
|
if ((sym->symbolKind == Symbol::LazyArchiveKind &&
|
|
|
|
!cast<ArchiveFile>(sym->file)->parsed) ||
|
|
|
|
(sym->symbolKind == Symbol::LazyObjectKind &&
|
|
|
|
cast<LazyObjFile>(sym->file)->fetched))
|
|
|
|
sym->replace(und);
|
|
|
|
else
|
|
|
|
sym->resolve(und);
|
Speed up --start-lib and --end-lib.
--{start,end}-lib give files grouped by the options the archive file
semantics. That is, each object file between them acts as if it were
in an archive file whose sole member is the file.
Therefore, files between --{start,end}-lib are linked to the final
output only if they are needed to resolve some undefined symbols.
Previously, the feature was implemented this way:
1. We read a symbol table and insert defined symbols to the symbol
table as lazy symbols.
2. If an undefind symbol is resolved to a lazy symbol, that lazy
symbol instantiate ObjFile class for that symbol, which re-insert
all defined symbols to the symbol table.
So, if an ObjFile is instantiated, defined symbols are inserted to the
symbol table twice. Since inserting long symbol names is not cheap,
there's a room to optimize here.
This patch optimzies it. Now, LazyObjFile remembers symbol handles and
passed them over to a new ObjFile instance, so that the ObjFile
doesn't insert the same strings.
Here is a quick benchmark to link clang. "Original" is the original
lld with unmodified command line options. For "Case 1" and "Case 2", I
extracted all files from archive files and replace .a's in a command
line with .o's wrapped with --{start,end}-lib. I used the original lld
for Case 1" and use this patch for Case 2.
Original: 5.892
Case 1: 6.001 (+1.8%)
Case 2: 5.701 (-3.2%)
So, interestingly, --{start,end}-lib are now faster than the regular
linking scheme with archive files. That's perhaps not too surprising,
though, because for regular archive files, we look up the symbol table
with the same string twice.
Differential Revision: https://reviews.llvm.org/D62188
llvm-svn: 361473
2019-05-23 17:53:30 +08:00
|
|
|
continue;
|
|
|
|
}
|
2019-05-22 17:06:42 +08:00
|
|
|
|
Speed up --start-lib and --end-lib.
--{start,end}-lib give files grouped by the options the archive file
semantics. That is, each object file between them acts as if it were
in an archive file whose sole member is the file.
Therefore, files between --{start,end}-lib are linked to the final
output only if they are needed to resolve some undefined symbols.
Previously, the feature was implemented this way:
1. We read a symbol table and insert defined symbols to the symbol
table as lazy symbols.
2. If an undefind symbol is resolved to a lazy symbol, that lazy
symbol instantiate ObjFile class for that symbol, which re-insert
all defined symbols to the symbol table.
So, if an ObjFile is instantiated, defined symbols are inserted to the
symbol table twice. Since inserting long symbol names is not cheap,
there's a room to optimize here.
This patch optimzies it. Now, LazyObjFile remembers symbol handles and
passed them over to a new ObjFile instance, so that the ObjFile
doesn't insert the same strings.
Here is a quick benchmark to link clang. "Original" is the original
lld with unmodified command line options. For "Case 1" and "Case 2", I
extracted all files from archive files and replace .a's in a command
line with .o's wrapped with --{start,end}-lib. I used the original lld
for Case 1" and use this patch for Case 2.
Original: 5.892
Case 1: 6.001 (+1.8%)
Case 2: 5.701 (-3.2%)
So, interestingly, --{start,end}-lib are now faster than the regular
linking scheme with archive files. That's perhaps not too surprising,
though, because for regular archive files, we look up the symbol table
with the same string twice.
Differential Revision: https://reviews.llvm.org/D62188
llvm-svn: 361473
2019-05-23 17:53:30 +08:00
|
|
|
// Handle global defined symbols.
|
|
|
|
if (binding == STB_GLOBAL || binding == STB_WEAK ||
|
|
|
|
binding == STB_GNU_UNIQUE) {
|
2019-05-23 17:58:08 +08:00
|
|
|
this->symbols[i]->resolve(
|
|
|
|
Defined{this, name, binding, stOther, type, value, size, sec});
|
Speed up --start-lib and --end-lib.
--{start,end}-lib give files grouped by the options the archive file
semantics. That is, each object file between them acts as if it were
in an archive file whose sole member is the file.
Therefore, files between --{start,end}-lib are linked to the final
output only if they are needed to resolve some undefined symbols.
Previously, the feature was implemented this way:
1. We read a symbol table and insert defined symbols to the symbol
table as lazy symbols.
2. If an undefind symbol is resolved to a lazy symbol, that lazy
symbol instantiate ObjFile class for that symbol, which re-insert
all defined symbols to the symbol table.
So, if an ObjFile is instantiated, defined symbols are inserted to the
symbol table twice. Since inserting long symbol names is not cheap,
there's a room to optimize here.
This patch optimzies it. Now, LazyObjFile remembers symbol handles and
passed them over to a new ObjFile instance, so that the ObjFile
doesn't insert the same strings.
Here is a quick benchmark to link clang. "Original" is the original
lld with unmodified command line options. For "Case 1" and "Case 2", I
extracted all files from archive files and replace .a's in a command
line with .o's wrapped with --{start,end}-lib. I used the original lld
for Case 1" and use this patch for Case 2.
Original: 5.892
Case 1: 6.001 (+1.8%)
Case 2: 5.701 (-3.2%)
So, interestingly, --{start,end}-lib are now faster than the regular
linking scheme with archive files. That's perhaps not too surprising,
though, because for regular archive files, we look up the symbol table
with the same string twice.
Differential Revision: https://reviews.llvm.org/D62188
llvm-svn: 361473
2019-05-23 17:53:30 +08:00
|
|
|
continue;
|
|
|
|
}
|
2015-08-25 05:43:25 +08:00
|
|
|
|
2019-05-16 10:14:00 +08:00
|
|
|
fatal(toString(this) + ": unexpected binding: " + Twine((int)binding));
|
2015-10-10 03:25:07 +08:00
|
|
|
}
|
2015-07-25 05:03:07 +08:00
|
|
|
}
|
|
|
|
|
2017-05-04 05:03:08 +08:00
|
|
|
ArchiveFile::ArchiveFile(std::unique_ptr<Archive> &&file)
|
|
|
|
: InputFile(ArchiveKind, file->getMemoryBufferRef()),
|
|
|
|
file(std::move(file)) {}
|
2015-09-05 06:28:10 +08:00
|
|
|
|
2019-05-16 11:45:13 +08:00
|
|
|
void ArchiveFile::parse() {
|
2017-05-04 05:03:08 +08:00
|
|
|
for (const Archive::Symbol &sym : file->symbols())
|
2019-05-17 09:55:20 +08:00
|
|
|
symtab->addSymbol(LazyArchive{*this, sym});
|
2020-06-10 02:25:55 +08:00
|
|
|
|
|
|
|
// Inform a future invocation of ObjFile<ELFT>::initializeSymbols() that this
|
|
|
|
// archive has been processed.
|
|
|
|
parsed = true;
|
2015-09-05 06:28:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns a buffer pointing to a member file containing a given symbol.
|
2019-05-23 18:15:12 +08:00
|
|
|
void ArchiveFile::fetch(const Archive::Symbol &sym) {
|
2016-03-04 00:21:44 +08:00
|
|
|
Archive::Child c =
|
2018-04-03 10:06:57 +08:00
|
|
|
CHECK(sym.getMember(), toString(this) +
|
|
|
|
": could not get the member for symbol " +
|
2019-07-24 03:00:01 +08:00
|
|
|
toELFString(sym));
|
2015-09-05 06:28:10 +08:00
|
|
|
|
2015-11-05 22:40:28 +08:00
|
|
|
if (!seen.insert(c.getChildOffset()).second)
|
2019-05-23 18:15:12 +08:00
|
|
|
return;
|
2015-09-09 04:36:20 +08:00
|
|
|
|
2018-04-03 10:06:57 +08:00
|
|
|
MemoryBufferRef mb =
|
2017-12-07 06:08:17 +08:00
|
|
|
CHECK(c.getMemoryBufferRef(),
|
2017-04-04 03:11:23 +08:00
|
|
|
toString(this) +
|
2017-03-31 05:13:00 +08:00
|
|
|
": could not get the buffer for the member defining symbol " +
|
2019-07-24 03:00:01 +08:00
|
|
|
toELFString(sym));
|
2018-04-03 10:06:57 +08:00
|
|
|
|
|
|
|
if (tar && c.getParent()->isThin())
|
|
|
|
tar->append(relativeToRoot(CHECK(c.getFullName(), this)), mb.getBuffer());
|
2016-05-02 21:54:10 +08:00
|
|
|
|
2020-05-12 13:39:59 +08:00
|
|
|
InputFile *file = createObjectFile(mb, getName(), c.getChildOffset());
|
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
|
|
|
file->groupId = groupId;
|
2019-05-23 18:15:12 +08:00
|
|
|
parseFile(file);
|
2015-09-05 06:28:10 +08:00
|
|
|
}
|
|
|
|
|
2020-04-28 10:30:09 +08:00
|
|
|
size_t ArchiveFile::getMemberCount() const {
|
|
|
|
size_t count = 0;
|
|
|
|
Error err = Error::success();
|
|
|
|
for (const Archive::Child &c : file->children(err)) {
|
|
|
|
(void)c;
|
|
|
|
++count;
|
|
|
|
}
|
|
|
|
// This function is used by --print-archive-stats=, where an error does not
|
|
|
|
// really matter.
|
|
|
|
consumeError(std::move(err));
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2019-04-09 01:48:05 +08:00
|
|
|
unsigned SharedFile::vernauxNum;
|
|
|
|
|
2019-04-09 01:35:55 +08:00
|
|
|
// Parse the version definitions in the object file if present, and return a
|
|
|
|
// vector whose nth element contains a pointer to the Elf_Verdef for version
|
|
|
|
// identifier n. Version identifiers that are not definitions map to nullptr.
|
|
|
|
template <typename ELFT>
|
|
|
|
static std::vector<const void *> parseVerdefs(const uint8_t *base,
|
|
|
|
const typename ELFT::Shdr *sec) {
|
|
|
|
if (!sec)
|
|
|
|
return {};
|
|
|
|
|
|
|
|
// We cannot determine the largest verdef identifier without inspecting
|
|
|
|
// every Elf_Verdef, but both bfd and gold assign verdef identifiers
|
|
|
|
// sequentially starting from 1, so we predict that the largest identifier
|
2019-07-16 13:50:45 +08:00
|
|
|
// will be verdefCount.
|
2019-04-09 01:35:55 +08:00
|
|
|
unsigned verdefCount = sec->sh_info;
|
|
|
|
std::vector<const void *> verdefs(verdefCount + 1);
|
|
|
|
|
|
|
|
// Build the Verdefs array by following the chain of Elf_Verdef objects
|
|
|
|
// from the start of the .gnu.version_d section.
|
|
|
|
const uint8_t *verdef = base + sec->sh_offset;
|
|
|
|
for (unsigned i = 0; i != verdefCount; ++i) {
|
|
|
|
auto *curVerdef = reinterpret_cast<const typename ELFT::Verdef *>(verdef);
|
|
|
|
verdef += curVerdef->vd_next;
|
|
|
|
unsigned verdefIndex = curVerdef->vd_ndx;
|
|
|
|
verdefs.resize(verdefIndex + 1);
|
|
|
|
verdefs[verdefIndex] = curVerdef;
|
|
|
|
}
|
|
|
|
return verdefs;
|
2019-04-06 04:16:26 +08:00
|
|
|
}
|
2015-09-08 23:50:05 +08:00
|
|
|
|
2020-05-16 11:36:41 +08:00
|
|
|
// Parse SHT_GNU_verneed to properly set the name of a versioned undefined
|
|
|
|
// symbol. We detect fatal issues which would cause vulnerabilities, but do not
|
|
|
|
// implement sophisticated error checking like in llvm-readobj because the value
|
|
|
|
// of such diagnostics is low.
|
|
|
|
template <typename ELFT>
|
|
|
|
std::vector<uint32_t> SharedFile::parseVerneed(const ELFFile<ELFT> &obj,
|
|
|
|
const typename ELFT::Shdr *sec) {
|
|
|
|
if (!sec)
|
|
|
|
return {};
|
|
|
|
std::vector<uint32_t> verneeds;
|
2020-09-09 22:03:53 +08:00
|
|
|
ArrayRef<uint8_t> data = CHECK(obj.getSectionContents(*sec), this);
|
2020-05-16 11:36:41 +08:00
|
|
|
const uint8_t *verneedBuf = data.begin();
|
|
|
|
for (unsigned i = 0; i != sec->sh_info; ++i) {
|
2020-05-27 02:06:07 +08:00
|
|
|
if (verneedBuf + sizeof(typename ELFT::Verneed) > data.end())
|
2020-05-16 11:36:41 +08:00
|
|
|
fatal(toString(this) + " has an invalid Verneed");
|
|
|
|
auto *vn = reinterpret_cast<const typename ELFT::Verneed *>(verneedBuf);
|
|
|
|
const uint8_t *vernauxBuf = verneedBuf + vn->vn_aux;
|
|
|
|
for (unsigned j = 0; j != vn->vn_cnt; ++j) {
|
2020-05-27 02:06:07 +08:00
|
|
|
if (vernauxBuf + sizeof(typename ELFT::Vernaux) > data.end())
|
2020-05-16 11:36:41 +08:00
|
|
|
fatal(toString(this) + " has an invalid Vernaux");
|
|
|
|
auto *aux = reinterpret_cast<const typename ELFT::Vernaux *>(vernauxBuf);
|
|
|
|
if (aux->vna_name >= this->stringTable.size())
|
|
|
|
fatal(toString(this) + " has a Vernaux with an invalid vna_name");
|
|
|
|
uint16_t version = aux->vna_other & VERSYM_VERSION;
|
|
|
|
if (version >= verneeds.size())
|
|
|
|
verneeds.resize(version + 1);
|
|
|
|
verneeds[version] = aux->vna_name;
|
|
|
|
vernauxBuf += aux->vna_next;
|
|
|
|
}
|
|
|
|
verneedBuf += vn->vn_next;
|
|
|
|
}
|
|
|
|
return verneeds;
|
|
|
|
}
|
|
|
|
|
2019-04-09 01:35:55 +08:00
|
|
|
// We do not usually care about alignments of data in shared object
|
|
|
|
// files because the loader takes care of it. However, if we promote a
|
|
|
|
// DSO symbol to point to .bss due to copy relocation, we need to keep
|
|
|
|
// the original alignment requirements. We infer it in this function.
|
|
|
|
template <typename ELFT>
|
|
|
|
static uint64_t getAlignment(ArrayRef<typename ELFT::Shdr> sections,
|
|
|
|
const typename ELFT::Sym &sym) {
|
|
|
|
uint64_t ret = UINT64_MAX;
|
|
|
|
if (sym.st_value)
|
|
|
|
ret = 1ULL << countTrailingZeros((uint64_t)sym.st_value);
|
|
|
|
if (0 < sym.st_shndx && sym.st_shndx < sections.size())
|
|
|
|
ret = std::min<uint64_t>(ret, sections[sym.st_shndx].sh_addralign);
|
|
|
|
return (ret > UINT32_MAX) ? 0 : ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fully parse the shared object file.
|
|
|
|
//
|
|
|
|
// This function parses symbol versions. If a DSO has version information,
|
|
|
|
// the file has a ".gnu.version_d" section which contains symbol version
|
|
|
|
// definitions. Each symbol is associated to one version through a table in
|
|
|
|
// ".gnu.version" section. That table is a parallel array for the symbol
|
|
|
|
// table, and each table entry contains an index in ".gnu.version_d".
|
|
|
|
//
|
|
|
|
// The special index 0 is reserved for VERF_NDX_LOCAL and 1 is for
|
|
|
|
// VER_NDX_GLOBAL. There's no table entry for these special versions in
|
|
|
|
// ".gnu.version_d".
|
|
|
|
//
|
|
|
|
// The file format for symbol versioning is perhaps a bit more complicated
|
|
|
|
// than necessary, but you can easily understand the code if you wrap your
|
|
|
|
// head around the data structure described above.
|
|
|
|
template <class ELFT> void SharedFile::parse() {
|
|
|
|
using Elf_Dyn = typename ELFT::Dyn;
|
|
|
|
using Elf_Shdr = typename ELFT::Shdr;
|
|
|
|
using Elf_Sym = typename ELFT::Sym;
|
|
|
|
using Elf_Verdef = typename ELFT::Verdef;
|
|
|
|
using Elf_Versym = typename ELFT::Versym;
|
|
|
|
|
2019-04-05 09:31:40 +08:00
|
|
|
ArrayRef<Elf_Dyn> dynamicTags;
|
2019-04-06 04:16:26 +08:00
|
|
|
const ELFFile<ELFT> obj = this->getObj<ELFT>();
|
2017-12-07 11:24:57 +08:00
|
|
|
ArrayRef<Elf_Shdr> sections = CHECK(obj.sections(), this);
|
2017-04-13 08:23:32 +08:00
|
|
|
|
2019-04-09 01:35:55 +08:00
|
|
|
const Elf_Shdr *versymSec = nullptr;
|
|
|
|
const Elf_Shdr *verdefSec = nullptr;
|
2020-05-16 11:36:41 +08:00
|
|
|
const Elf_Shdr *verneedSec = nullptr;
|
2019-04-09 01:35:55 +08:00
|
|
|
|
2017-04-13 08:23:32 +08:00
|
|
|
// Search for .dynsym, .dynamic, .symtab, .gnu.version and .gnu.version_d.
|
2016-11-03 20:21:00 +08:00
|
|
|
for (const Elf_Shdr &sec : sections) {
|
2015-11-03 22:13:40 +08:00
|
|
|
switch (sec.sh_type) {
|
|
|
|
default:
|
|
|
|
continue;
|
|
|
|
case SHT_DYNAMIC:
|
2019-04-05 09:31:40 +08:00
|
|
|
dynamicTags =
|
2020-09-09 22:03:53 +08:00
|
|
|
CHECK(obj.template getSectionContentsAsArray<Elf_Dyn>(sec), this);
|
2015-11-03 22:13:40 +08:00
|
|
|
break;
|
2016-04-28 04:22:31 +08:00
|
|
|
case SHT_GNU_versym:
|
2019-04-09 01:35:55 +08:00
|
|
|
versymSec = &sec;
|
2016-04-28 04:22:31 +08:00
|
|
|
break;
|
|
|
|
case SHT_GNU_verdef:
|
2019-04-09 01:35:55 +08:00
|
|
|
verdefSec = &sec;
|
2016-04-28 04:22:31 +08:00
|
|
|
break;
|
2020-05-16 11:36:41 +08:00
|
|
|
case SHT_GNU_verneed:
|
|
|
|
verneedSec = &sec;
|
|
|
|
break;
|
2015-11-03 22:13:40 +08:00
|
|
|
}
|
2015-09-08 23:50:05 +08:00
|
|
|
}
|
|
|
|
|
2019-05-28 13:17:21 +08:00
|
|
|
if (versymSec && numELFSyms == 0) {
|
2016-11-02 18:16:25 +08:00
|
|
|
error("SHT_GNU_versym should be associated with symbol table");
|
2019-04-09 01:35:55 +08:00
|
|
|
return;
|
|
|
|
}
|
2016-11-02 18:16:25 +08:00
|
|
|
|
2019-07-16 13:50:45 +08:00
|
|
|
// Search for a DT_SONAME tag to initialize this->soName.
|
2019-04-05 09:31:40 +08:00
|
|
|
for (const Elf_Dyn &dyn : dynamicTags) {
|
[ELF] Support --{,no-}allow-shlib-undefined
Summary:
In ld.bfd/gold, --no-allow-shlib-undefined is the default when linking
an executable. This patch implements a check to error on undefined
symbols in a shared object, if all of its DT_NEEDED entries are seen.
Our approach resembles the one used in gold, achieves a good balance to
be useful but not too smart (ld.bfd traces all DSOs and emulates the
behavior of a dynamic linker to catch more cases).
The error is issued based on the symbol table, different from undefined
reference errors issued for relocations. It is most effective when there
are DSOs that were not linked with -z defs (e.g. when static sanitizers
runtime is used).
gold has a comment that some system libraries on GNU/Linux may have
spurious undefined references and thus system libraries should be
excluded (https://sourceware.org/bugzilla/show_bug.cgi?id=6811). The
story may have changed now but we make --allow-shlib-undefined the
default for now. Its interaction with -shared can be discussed in the
future.
Reviewers: ruiu, grimar, pcc, espindola
Reviewed By: ruiu
Subscribers: joerg, emaste, arichardson, llvm-commits
Differential Revision: https://reviews.llvm.org/D57385
llvm-svn: 352826
2019-02-01 10:25:05 +08:00
|
|
|
if (dyn.d_tag == DT_NEEDED) {
|
|
|
|
uint64_t val = dyn.getVal();
|
|
|
|
if (val >= this->stringTable.size())
|
|
|
|
fatal(toString(this) + ": invalid DT_NEEDED entry");
|
|
|
|
dtNeeded.push_back(this->stringTable.data() + val);
|
|
|
|
} else if (dyn.d_tag == DT_SONAME) {
|
2017-02-25 03:52:52 +08:00
|
|
|
uint64_t val = dyn.getVal();
|
2015-10-12 23:49:02 +08:00
|
|
|
if (val >= this->stringTable.size())
|
2016-11-24 02:07:33 +08:00
|
|
|
fatal(toString(this) + ": invalid DT_SONAME entry");
|
2017-04-27 07:00:32 +08:00
|
|
|
soName = this->stringTable.data() + val;
|
2015-10-01 23:47:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-09 01:35:55 +08:00
|
|
|
// DSOs are uniquified not by filename but by soname.
|
|
|
|
DenseMap<StringRef, SharedFile *>::iterator it;
|
|
|
|
bool wasInserted;
|
|
|
|
std::tie(it, wasInserted) = symtab->soNames.try_emplace(soName, this);
|
|
|
|
|
|
|
|
// If a DSO appears more than once on the command line with and without
|
|
|
|
// --as-needed, --no-as-needed takes precedence over --as-needed because a
|
|
|
|
// user can add an extra DSO with --no-as-needed to force it to be added to
|
|
|
|
// the dependency list.
|
|
|
|
it->second->isNeeded |= isNeeded;
|
|
|
|
if (!wasInserted)
|
|
|
|
return;
|
2018-03-27 03:57:38 +08:00
|
|
|
|
2019-04-09 01:35:55 +08:00
|
|
|
sharedFiles.push_back(this);
|
2016-04-28 04:22:31 +08:00
|
|
|
|
2019-04-09 01:35:55 +08:00
|
|
|
verdefs = parseVerdefs<ELFT>(obj.base(), verdefSec);
|
2020-05-16 11:36:41 +08:00
|
|
|
std::vector<uint32_t> verneeds = parseVerneed<ELFT>(obj, verneedSec);
|
2016-04-28 04:22:31 +08:00
|
|
|
|
2019-04-09 01:35:55 +08:00
|
|
|
// Parse ".gnu.version" section which is a parallel array for the symbol
|
|
|
|
// table. If a given file doesn't have a ".gnu.version" section, we use
|
|
|
|
// VER_NDX_GLOBAL.
|
2019-05-28 13:17:21 +08:00
|
|
|
size_t size = numELFSyms - firstGlobal;
|
2020-05-16 11:36:41 +08:00
|
|
|
std::vector<uint16_t> versyms(size, VER_NDX_GLOBAL);
|
2019-04-09 01:35:55 +08:00
|
|
|
if (versymSec) {
|
|
|
|
ArrayRef<Elf_Versym> versym =
|
2020-09-09 22:03:53 +08:00
|
|
|
CHECK(obj.template getSectionContentsAsArray<Elf_Versym>(*versymSec),
|
2019-04-09 01:35:55 +08:00
|
|
|
this)
|
|
|
|
.slice(firstGlobal);
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
versyms[i] = versym[i].vs_index;
|
2016-04-28 04:22:31 +08:00
|
|
|
}
|
|
|
|
|
2018-04-07 04:53:06 +08:00
|
|
|
// System libraries can have a lot of symbols with versions. Using a
|
|
|
|
// fixed buffer for computing the versions name (foo@ver) can save a
|
|
|
|
// lot of allocations.
|
|
|
|
SmallString<0> versionedNameBuffer;
|
|
|
|
|
2017-10-29 04:15:56 +08:00
|
|
|
// Add symbols to the symbol table.
|
2019-04-06 04:16:26 +08:00
|
|
|
ArrayRef<Elf_Sym> syms = this->getGlobalELFSyms<ELFT>();
|
2018-03-27 03:57:38 +08:00
|
|
|
for (size_t i = 0; i < syms.size(); ++i) {
|
|
|
|
const Elf_Sym &sym = syms[i];
|
2016-04-30 01:46:07 +08:00
|
|
|
|
2018-03-16 01:10:50 +08:00
|
|
|
// ELF spec requires that all local symbols precede weak or global
|
|
|
|
// symbols in each symbol table, and the index of first non-local symbol
|
|
|
|
// is stored to sh_info. If a local symbol appears after some non-local
|
|
|
|
// symbol, that's a violation of the spec.
|
2018-10-04 07:53:11 +08:00
|
|
|
StringRef name = CHECK(sym.getName(this->stringTable), this);
|
2017-12-15 08:01:33 +08:00
|
|
|
if (sym.getBinding() == STB_LOCAL) {
|
2017-12-15 08:07:15 +08:00
|
|
|
warn("found local symbol '" + name +
|
2017-12-15 08:01:33 +08:00
|
|
|
"' in global part of symbol table in file " + toString(this));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-05-16 11:36:41 +08:00
|
|
|
uint16_t idx = versyms[i] & ~VERSYM_HIDDEN;
|
2018-10-04 07:53:11 +08:00
|
|
|
if (sym.isUndefined()) {
|
2020-05-16 11:36:41 +08:00
|
|
|
// For unversioned undefined symbols, VER_NDX_GLOBAL makes more sense but
|
|
|
|
// as of binutils 2.34, GNU ld produces VER_NDX_LOCAL.
|
|
|
|
if (idx != VER_NDX_LOCAL && idx != VER_NDX_GLOBAL) {
|
|
|
|
if (idx >= verneeds.size()) {
|
|
|
|
error("corrupt input file: version need index " + Twine(idx) +
|
|
|
|
" for symbol " + name + " is out of bounds\n>>> defined in " +
|
|
|
|
toString(this));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
StringRef verName = this->stringTable.data() + verneeds[idx];
|
|
|
|
versionedNameBuffer.clear();
|
|
|
|
name =
|
|
|
|
saver.save((name + "@" + verName).toStringRef(versionedNameBuffer));
|
|
|
|
}
|
2019-05-17 09:55:20 +08:00
|
|
|
Symbol *s = symtab->addSymbol(
|
2019-05-16 10:14:00 +08:00
|
|
|
Undefined{this, name, sym.getBinding(), sym.st_other, sym.getType()});
|
2018-10-04 07:53:11 +08:00
|
|
|
s->exportDynamic = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-03-24 06:48:17 +08:00
|
|
|
// MIPS BFD linker puts _gp_disp symbol into DSO files and incorrectly
|
|
|
|
// assigns VER_NDX_LOCAL to this section global symbol. Here is a
|
|
|
|
// workaround for this bug.
|
2018-03-27 03:57:38 +08:00
|
|
|
if (config->emachine == EM_MIPS && idx == VER_NDX_LOCAL &&
|
2018-03-24 06:48:17 +08:00
|
|
|
name == "_gp_disp")
|
|
|
|
continue;
|
2018-02-07 18:02:49 +08:00
|
|
|
|
2019-05-16 10:14:00 +08:00
|
|
|
uint32_t alignment = getAlignment<ELFT>(sections, sym);
|
|
|
|
if (!(versyms[i] & VERSYM_HIDDEN)) {
|
2019-05-17 09:55:20 +08:00
|
|
|
symtab->addSymbol(SharedSymbol{*this, name, sym.getBinding(),
|
2019-05-16 10:14:00 +08:00
|
|
|
sym.st_other, sym.getType(), sym.st_value,
|
|
|
|
sym.st_size, alignment, idx});
|
|
|
|
}
|
2017-01-07 06:30:35 +08:00
|
|
|
|
|
|
|
// Also add the symbol with the versioned name to handle undefined symbols
|
|
|
|
// with explicit versions.
|
2018-03-27 03:57:38 +08:00
|
|
|
if (idx == VER_NDX_GLOBAL)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (idx >= verdefs.size() || idx == VER_NDX_LOCAL) {
|
|
|
|
error("corrupt input file: version definition index " + Twine(idx) +
|
|
|
|
" for symbol " + name + " is out of bounds\n>>> defined in " +
|
|
|
|
toString(this));
|
|
|
|
continue;
|
2017-01-07 06:30:35 +08:00
|
|
|
}
|
2018-03-27 03:57:38 +08:00
|
|
|
|
|
|
|
StringRef verName =
|
2019-04-09 01:35:55 +08:00
|
|
|
this->stringTable.data() +
|
|
|
|
reinterpret_cast<const Elf_Verdef *>(verdefs[idx])->getAux()->vda_name;
|
2018-04-07 04:53:06 +08:00
|
|
|
versionedNameBuffer.clear();
|
|
|
|
name = (name + "@" + verName).toStringRef(versionedNameBuffer);
|
2019-05-17 09:55:20 +08:00
|
|
|
symtab->addSymbol(SharedSymbol{*this, saver.save(name), sym.getBinding(),
|
2019-05-16 10:14:00 +08:00
|
|
|
sym.st_other, sym.getType(), sym.st_value,
|
|
|
|
sym.st_size, alignment, idx});
|
2015-09-08 23:50:05 +08:00
|
|
|
}
|
|
|
|
}
|
2015-09-04 04:03:54 +08:00
|
|
|
|
2017-04-14 10:55:06 +08:00
|
|
|
static ELFKind getBitcodeELFKind(const Triple &t) {
|
2016-08-04 04:25:29 +08:00
|
|
|
if (t.isLittleEndian())
|
|
|
|
return t.isArch64Bit() ? ELF64LEKind : ELF32LEKind;
|
|
|
|
return t.isArch64Bit() ? ELF64BEKind : ELF32BEKind;
|
2016-06-29 14:12:39 +08:00
|
|
|
}
|
|
|
|
|
2017-04-14 10:55:06 +08:00
|
|
|
static uint8_t getBitcodeMachineKind(StringRef path, const Triple &t) {
|
2016-08-04 04:25:29 +08:00
|
|
|
switch (t.getArch()) {
|
2016-07-07 10:46:30 +08:00
|
|
|
case Triple::aarch64:
|
|
|
|
return EM_AARCH64;
|
2018-08-27 20:40:00 +08:00
|
|
|
case Triple::amdgcn:
|
|
|
|
case Triple::r600:
|
|
|
|
return EM_AMDGPU;
|
2016-07-07 10:46:30 +08:00
|
|
|
case Triple::arm:
|
2017-02-28 11:00:48 +08:00
|
|
|
case Triple::thumb:
|
2016-07-07 10:46:30 +08:00
|
|
|
return EM_ARM;
|
2017-06-15 10:27:50 +08:00
|
|
|
case Triple::avr:
|
|
|
|
return EM_AVR;
|
2016-07-07 10:46:30 +08:00
|
|
|
case Triple::mips:
|
|
|
|
case Triple::mipsel:
|
|
|
|
case Triple::mips64:
|
|
|
|
case Triple::mips64el:
|
|
|
|
return EM_MIPS;
|
2019-01-10 21:43:06 +08:00
|
|
|
case Triple::msp430:
|
|
|
|
return EM_MSP430;
|
2016-07-07 10:46:30 +08:00
|
|
|
case Triple::ppc:
|
|
|
|
return EM_PPC;
|
|
|
|
case Triple::ppc64:
|
2018-09-20 08:26:49 +08:00
|
|
|
case Triple::ppc64le:
|
2016-07-07 10:46:30 +08:00
|
|
|
return EM_PPC64;
|
2019-07-03 10:13:11 +08:00
|
|
|
case Triple::riscv32:
|
|
|
|
case Triple::riscv64:
|
|
|
|
return EM_RISCV;
|
2016-07-07 10:46:30 +08:00
|
|
|
case Triple::x86:
|
2016-08-04 04:25:29 +08:00
|
|
|
return t.isOSIAMCU() ? EM_IAMCU : EM_386;
|
2016-07-07 10:46:30 +08:00
|
|
|
case Triple::x86_64:
|
|
|
|
return EM_X86_64;
|
|
|
|
default:
|
2018-05-23 04:20:25 +08:00
|
|
|
error(path + ": could not infer e_machine from bitcode target triple " +
|
2017-04-14 10:55:06 +08:00
|
|
|
t.str());
|
2018-05-23 04:20:25 +08:00
|
|
|
return EM_NONE;
|
2016-06-29 14:12:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-14 10:55:06 +08:00
|
|
|
BitcodeFile::BitcodeFile(MemoryBufferRef mb, StringRef archiveName,
|
|
|
|
uint64_t offsetInArchive)
|
|
|
|
: InputFile(BitcodeKind, mb) {
|
2020-01-29 03:23:46 +08:00
|
|
|
this->archiveName = std::string(archiveName);
|
2017-04-14 10:55:06 +08:00
|
|
|
|
2018-05-17 05:04:08 +08:00
|
|
|
std::string path = mb.getBufferIdentifier().str();
|
|
|
|
if (config->thinLTOIndexOnly)
|
2018-05-18 02:27:12 +08:00
|
|
|
path = replaceThinLTOSuffix(mb.getBufferIdentifier());
|
|
|
|
|
|
|
|
// ThinLTO assumes that all MemoryBufferRefs given to it have a unique
|
|
|
|
// name. If two archives define two members with the same name, this
|
|
|
|
// causes a collision which result in only one of the objects being taken
|
|
|
|
// into consideration at LTO time (which very likely causes undefined
|
|
|
|
// symbols later in the link stage). So we append file offset to make
|
|
|
|
// filename unique.
|
2020-03-04 17:46:31 +08:00
|
|
|
StringRef name =
|
|
|
|
archiveName.empty()
|
|
|
|
? saver.save(path)
|
|
|
|
: saver.save(archiveName + "(" + path::filename(path) + " at " +
|
|
|
|
utostr(offsetInArchive) + ")");
|
2019-05-16 13:23:25 +08:00
|
|
|
MemoryBufferRef mbref(mb.getBuffer(), name);
|
2018-05-18 02:27:12 +08:00
|
|
|
|
2017-12-07 11:24:57 +08:00
|
|
|
obj = CHECK(lto::InputFile::create(mbref), this);
|
2017-04-14 10:55:06 +08:00
|
|
|
|
|
|
|
Triple t(obj->getTargetTriple());
|
|
|
|
ekind = getBitcodeELFKind(t);
|
|
|
|
emachine = getBitcodeMachineKind(mb.getBufferIdentifier(), t);
|
2016-06-29 14:12:39 +08:00
|
|
|
}
|
2016-02-13 04:54:57 +08:00
|
|
|
|
2016-09-29 08:40:08 +08:00
|
|
|
static uint8_t mapVisibility(GlobalValue::VisibilityTypes gvVisibility) {
|
|
|
|
switch (gvVisibility) {
|
2016-03-08 03:06:14 +08:00
|
|
|
case GlobalValue::DefaultVisibility:
|
|
|
|
return STV_DEFAULT;
|
2016-03-07 08:54:17 +08:00
|
|
|
case GlobalValue::HiddenVisibility:
|
|
|
|
return STV_HIDDEN;
|
|
|
|
case GlobalValue::ProtectedVisibility:
|
|
|
|
return STV_PROTECTED;
|
|
|
|
}
|
2016-03-12 16:31:34 +08:00
|
|
|
llvm_unreachable("unknown visibility");
|
2016-03-12 02:46:51 +08:00
|
|
|
}
|
|
|
|
|
ELF: New symbol table design.
This patch implements a new design for the symbol table that stores
SymbolBodies within a memory region of the Symbol object. Symbols are mutated
by constructing SymbolBodies in place over existing SymbolBodies, rather
than by mutating pointers. As mentioned in the initial proposal [1], this
memory layout helps reduce the cache miss rate by improving memory locality.
Performance numbers:
old(s) new(s)
Without debug info:
chrome 7.178 6.432 (-11.5%)
LLVMgold.so 0.505 0.502 (-0.5%)
clang 0.954 0.827 (-15.4%)
llvm-as 0.052 0.045 (-15.5%)
With debug info:
scylla 5.695 5.613 (-1.5%)
clang 14.396 14.143 (-1.8%)
Performance counter results show that the fewer required indirections is
indeed the cause of the improved performance. For example, when linking
chrome, stalled cycles decreases from 14,556,444,002 to 12,959,238,310, and
instructions per cycle increases from 0.78 to 0.83. We are also executing
many fewer instructions (15,516,401,933 down to 15,002,434,310), probably
because we spend less time allocating SymbolBodies.
The new mechanism by which symbols are added to the symbol table is by calling
add* functions on the SymbolTable.
In this patch, I handle local symbols by storing them inside "unparented"
SymbolBodies. This is suboptimal, but if we do want to try to avoid allocating
these SymbolBodies, we can probably do that separately.
I also removed a few members from the SymbolBody class that were only being
used to pass information from the input file to the symbol table.
This patch implements the new design for the ELF linker only. I intend to
prepare a similar patch for the COFF linker.
[1] http://lists.llvm.org/pipermail/llvm-dev/2016-April/098832.html
Differential Revision: http://reviews.llvm.org/D19752
llvm-svn: 268178
2016-05-01 12:55:03 +08:00
|
|
|
template <class ELFT>
|
2017-11-04 05:21:47 +08:00
|
|
|
static Symbol *createBitcodeSymbol(const std::vector<bool> &keptComdats,
|
|
|
|
const lto::InputFile::Symbol &objSym,
|
2017-12-21 00:16:40 +08:00
|
|
|
BitcodeFile &f) {
|
2018-05-23 04:20:25 +08:00
|
|
|
StringRef name = saver.save(objSym.getName());
|
2019-05-16 10:14:00 +08:00
|
|
|
uint8_t binding = objSym.isWeak() ? STB_WEAK : STB_GLOBAL;
|
2016-09-29 08:40:08 +08:00
|
|
|
uint8_t type = objSym.isTLS() ? STT_TLS : STT_NOTYPE;
|
|
|
|
uint8_t visibility = mapVisibility(objSym.getVisibility());
|
|
|
|
bool canOmitFromDynSym = objSym.canBeOmittedFromSymbolTable();
|
[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-31 12:47:07 +08:00
|
|
|
int c = objSym.getComdatIndex();
|
2019-05-16 10:14:00 +08:00
|
|
|
if (objSym.isUndefined() || (c != -1 && !keptComdats[c])) {
|
2019-08-13 14:19:39 +08:00
|
|
|
Undefined newSym(&f, name, binding, visibility, type);
|
2019-05-16 10:14:00 +08:00
|
|
|
if (canOmitFromDynSym)
|
2019-08-13 14:19:39 +08:00
|
|
|
newSym.exportDynamic = false;
|
2019-08-30 15:10:30 +08:00
|
|
|
Symbol *ret = symtab->addSymbol(newSym);
|
|
|
|
ret->referenced = true;
|
|
|
|
return ret;
|
2019-05-16 10:14:00 +08:00
|
|
|
}
|
2016-09-29 08:40:08 +08:00
|
|
|
|
2017-03-29 06:31:35 +08:00
|
|
|
if (objSym.isCommon())
|
2019-05-17 09:55:20 +08:00
|
|
|
return symtab->addSymbol(
|
2019-05-16 11:29:03 +08:00
|
|
|
CommonSymbol{&f, name, binding, visibility, STT_OBJECT,
|
|
|
|
objSym.getCommonAlignment(), objSym.getCommonSize()});
|
2019-05-16 10:14:00 +08:00
|
|
|
|
2019-08-13 14:19:39 +08:00
|
|
|
Defined newSym(&f, name, binding, visibility, type, 0, 0, nullptr);
|
2019-05-16 10:14:00 +08:00
|
|
|
if (canOmitFromDynSym)
|
2019-08-13 14:19:39 +08:00
|
|
|
newSym.exportDynamic = false;
|
|
|
|
return symtab->addSymbol(newSym);
|
2016-03-12 00:11:47 +08:00
|
|
|
}
|
|
|
|
|
2019-06-06 01:39:37 +08:00
|
|
|
template <class ELFT> void BitcodeFile::parse() {
|
2016-10-25 20:02:31 +08:00
|
|
|
std::vector<bool> keptComdats;
|
2017-03-31 12:47:07 +08:00
|
|
|
for (StringRef s : obj->getComdatTable())
|
2019-05-22 17:06:42 +08:00
|
|
|
keptComdats.push_back(
|
2019-06-06 01:39:37 +08:00
|
|
|
symtab->comdatGroups.try_emplace(CachedHashStringRef(s), this).second);
|
2016-10-25 20:02:31 +08:00
|
|
|
|
2016-09-29 08:40:08 +08:00
|
|
|
for (const lto::InputFile::Symbol &objSym : obj->symbols())
|
2017-12-21 00:16:40 +08:00
|
|
|
symbols.push_back(createBitcodeSymbol<ELFT>(keptComdats, objSym, *this));
|
[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
|
|
|
|
|
|
|
for (auto l : obj->getDependentLibraries())
|
|
|
|
addDependentLibrary(l, this);
|
2016-02-13 04:54:57 +08:00
|
|
|
}
|
|
|
|
|
2017-12-24 01:21:39 +08:00
|
|
|
void BinaryFile::parse() {
|
2018-10-22 16:35:39 +08:00
|
|
|
ArrayRef<uint8_t> data = arrayRefFromStringRef(mb.getBuffer());
|
2018-02-05 17:47:24 +08:00
|
|
|
auto *section = make<InputSection>(this, SHF_ALLOC | SHF_WRITE, SHT_PROGBITS,
|
|
|
|
8, data, ".data");
|
2016-10-28 01:45:40 +08:00
|
|
|
sections.push_back(section);
|
|
|
|
|
2017-04-27 12:01:36 +08:00
|
|
|
// For each input file foo that is embedded to a result as a binary
|
|
|
|
// blob, we define _binary_foo_{start,end,size} symbols, so that
|
|
|
|
// user programs can access blobs by name. Non-alphanumeric
|
|
|
|
// characters in a filename are replaced with underscore.
|
|
|
|
std::string s = "_binary_" + mb.getBufferIdentifier().str();
|
|
|
|
for (size_t i = 0; i < s.size(); ++i)
|
2017-10-04 16:50:34 +08:00
|
|
|
if (!isAlnum(s[i]))
|
2017-04-27 12:01:36 +08:00
|
|
|
s[i] = '_';
|
[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-17 09:55:20 +08:00
|
|
|
symtab->addSymbol(Defined{nullptr, saver.save(s + "_start"), STB_GLOBAL,
|
|
|
|
STV_DEFAULT, STT_OBJECT, 0, 0, section});
|
|
|
|
symtab->addSymbol(Defined{nullptr, saver.save(s + "_end"), STB_GLOBAL,
|
|
|
|
STV_DEFAULT, STT_OBJECT, data.size(), 0, section});
|
|
|
|
symtab->addSymbol(Defined{nullptr, saver.save(s + "_size"), STB_GLOBAL,
|
|
|
|
STV_DEFAULT, STT_OBJECT, data.size(), 0, nullptr});
|
2016-09-10 06:08:04 +08:00
|
|
|
}
|
|
|
|
|
2020-05-15 13:18:58 +08:00
|
|
|
InputFile *elf::createObjectFile(MemoryBufferRef mb, StringRef archiveName,
|
|
|
|
uint64_t offsetInArchive) {
|
2017-04-27 06:51:51 +08:00
|
|
|
if (isBitcode(mb))
|
|
|
|
return make<BitcodeFile>(mb, archiveName, offsetInArchive);
|
|
|
|
|
2019-03-12 08:24:34 +08:00
|
|
|
switch (getELFKind(mb, archiveName)) {
|
2017-04-27 06:51:51 +08:00
|
|
|
case ELF32LEKind:
|
2017-07-27 06:13:32 +08:00
|
|
|
return make<ObjFile<ELF32LE>>(mb, archiveName);
|
2017-04-27 06:51:51 +08:00
|
|
|
case ELF32BEKind:
|
2017-07-27 06:13:32 +08:00
|
|
|
return make<ObjFile<ELF32BE>>(mb, archiveName);
|
2017-04-27 06:51:51 +08:00
|
|
|
case ELF64LEKind:
|
2017-07-27 06:13:32 +08:00
|
|
|
return make<ObjFile<ELF64LE>>(mb, archiveName);
|
2017-04-27 06:51:51 +08:00
|
|
|
case ELF64BEKind:
|
2017-07-27 06:13:32 +08:00
|
|
|
return make<ObjFile<ELF64BE>>(mb, archiveName);
|
2017-04-27 06:51:51 +08:00
|
|
|
default:
|
|
|
|
llvm_unreachable("getELFKind");
|
|
|
|
}
|
2016-01-06 08:09:43 +08:00
|
|
|
}
|
|
|
|
|
2019-05-23 18:15:12 +08:00
|
|
|
void LazyObjFile::fetch() {
|
2020-06-10 02:25:55 +08:00
|
|
|
if (fetched)
|
2019-05-23 18:15:12 +08:00
|
|
|
return;
|
2020-06-10 02:25:55 +08:00
|
|
|
fetched = true;
|
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
|
|
|
|
2019-05-23 18:08:56 +08:00
|
|
|
InputFile *file = createObjectFile(mb, archiveName, offsetInArchive);
|
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
|
|
|
file->groupId = groupId;
|
Speed up --start-lib and --end-lib.
--{start,end}-lib give files grouped by the options the archive file
semantics. That is, each object file between them acts as if it were
in an archive file whose sole member is the file.
Therefore, files between --{start,end}-lib are linked to the final
output only if they are needed to resolve some undefined symbols.
Previously, the feature was implemented this way:
1. We read a symbol table and insert defined symbols to the symbol
table as lazy symbols.
2. If an undefind symbol is resolved to a lazy symbol, that lazy
symbol instantiate ObjFile class for that symbol, which re-insert
all defined symbols to the symbol table.
So, if an ObjFile is instantiated, defined symbols are inserted to the
symbol table twice. Since inserting long symbol names is not cheap,
there's a room to optimize here.
This patch optimzies it. Now, LazyObjFile remembers symbol handles and
passed them over to a new ObjFile instance, so that the ObjFile
doesn't insert the same strings.
Here is a quick benchmark to link clang. "Original" is the original
lld with unmodified command line options. For "Case 1" and "Case 2", I
extracted all files from archive files and replace .a's in a command
line with .o's wrapped with --{start,end}-lib. I used the original lld
for Case 1" and use this patch for Case 2.
Original: 5.892
Case 1: 6.001 (+1.8%)
Case 2: 5.701 (-3.2%)
So, interestingly, --{start,end}-lib are now faster than the regular
linking scheme with archive files. That's perhaps not too surprising,
though, because for regular archive files, we look up the symbol table
with the same string twice.
Differential Revision: https://reviews.llvm.org/D62188
llvm-svn: 361473
2019-05-23 17:53:30 +08:00
|
|
|
|
|
|
|
// Copy symbol vector so that the new InputFile doesn't have to
|
|
|
|
// insert the same defined symbols to the symbol table again.
|
|
|
|
file->symbols = std::move(symbols);
|
2019-05-23 18:15:12 +08:00
|
|
|
|
|
|
|
parseFile(file);
|
2017-05-04 22:54:48 +08:00
|
|
|
}
|
|
|
|
|
2017-07-27 06:13:32 +08:00
|
|
|
template <class ELFT> void LazyObjFile::parse() {
|
Speed up --start-lib and --end-lib.
--{start,end}-lib give files grouped by the options the archive file
semantics. That is, each object file between them acts as if it were
in an archive file whose sole member is the file.
Therefore, files between --{start,end}-lib are linked to the final
output only if they are needed to resolve some undefined symbols.
Previously, the feature was implemented this way:
1. We read a symbol table and insert defined symbols to the symbol
table as lazy symbols.
2. If an undefind symbol is resolved to a lazy symbol, that lazy
symbol instantiate ObjFile class for that symbol, which re-insert
all defined symbols to the symbol table.
So, if an ObjFile is instantiated, defined symbols are inserted to the
symbol table twice. Since inserting long symbol names is not cheap,
there's a room to optimize here.
This patch optimzies it. Now, LazyObjFile remembers symbol handles and
passed them over to a new ObjFile instance, so that the ObjFile
doesn't insert the same strings.
Here is a quick benchmark to link clang. "Original" is the original
lld with unmodified command line options. For "Case 1" and "Case 2", I
extracted all files from archive files and replace .a's in a command
line with .o's wrapped with --{start,end}-lib. I used the original lld
for Case 1" and use this patch for Case 2.
Original: 5.892
Case 1: 6.001 (+1.8%)
Case 2: 5.701 (-3.2%)
So, interestingly, --{start,end}-lib are now faster than the regular
linking scheme with archive files. That's perhaps not too surprising,
though, because for regular archive files, we look up the symbol table
with the same string twice.
Differential Revision: https://reviews.llvm.org/D62188
llvm-svn: 361473
2019-05-23 17:53:30 +08:00
|
|
|
using Elf_Sym = typename ELFT::Sym;
|
|
|
|
|
2018-03-08 09:05:58 +08:00
|
|
|
// A lazy object file wraps either a bitcode file or an ELF file.
|
|
|
|
if (isBitcode(this->mb)) {
|
|
|
|
std::unique_ptr<lto::InputFile> obj =
|
|
|
|
CHECK(lto::InputFile::create(this->mb), this);
|
2019-05-16 10:14:00 +08:00
|
|
|
for (const lto::InputFile::Symbol &sym : obj->symbols()) {
|
|
|
|
if (sym.isUndefined())
|
|
|
|
continue;
|
2019-05-17 09:55:20 +08:00
|
|
|
symtab->addSymbol(LazyObject{*this, saver.save(sym.getName())});
|
2019-05-16 10:14:00 +08:00
|
|
|
}
|
2018-03-08 09:05:58 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-03-12 08:24:34 +08:00
|
|
|
if (getELFKind(this->mb, archiveName) != config->ekind) {
|
2018-08-21 16:13:06 +08:00
|
|
|
error("incompatible file: " + this->mb.getBufferIdentifier());
|
2018-03-08 09:05:58 +08:00
|
|
|
return;
|
|
|
|
}
|
2016-04-08 03:24:51 +08:00
|
|
|
|
Speed up --start-lib and --end-lib.
--{start,end}-lib give files grouped by the options the archive file
semantics. That is, each object file between them acts as if it were
in an archive file whose sole member is the file.
Therefore, files between --{start,end}-lib are linked to the final
output only if they are needed to resolve some undefined symbols.
Previously, the feature was implemented this way:
1. We read a symbol table and insert defined symbols to the symbol
table as lazy symbols.
2. If an undefind symbol is resolved to a lazy symbol, that lazy
symbol instantiate ObjFile class for that symbol, which re-insert
all defined symbols to the symbol table.
So, if an ObjFile is instantiated, defined symbols are inserted to the
symbol table twice. Since inserting long symbol names is not cheap,
there's a room to optimize here.
This patch optimzies it. Now, LazyObjFile remembers symbol handles and
passed them over to a new ObjFile instance, so that the ObjFile
doesn't insert the same strings.
Here is a quick benchmark to link clang. "Original" is the original
lld with unmodified command line options. For "Case 1" and "Case 2", I
extracted all files from archive files and replace .a's in a command
line with .o's wrapped with --{start,end}-lib. I used the original lld
for Case 1" and use this patch for Case 2.
Original: 5.892
Case 1: 6.001 (+1.8%)
Case 2: 5.701 (-3.2%)
So, interestingly, --{start,end}-lib are now faster than the regular
linking scheme with archive files. That's perhaps not too surprising,
though, because for regular archive files, we look up the symbol table
with the same string twice.
Differential Revision: https://reviews.llvm.org/D62188
llvm-svn: 361473
2019-05-23 17:53:30 +08:00
|
|
|
// Find a symbol table.
|
2018-03-08 09:05:58 +08:00
|
|
|
ELFFile<ELFT> obj = check(ELFFile<ELFT>::create(mb.getBuffer()));
|
|
|
|
ArrayRef<typename ELFT::Shdr> sections = CHECK(obj.sections(), this);
|
2016-04-08 03:24:51 +08:00
|
|
|
|
2018-03-08 09:05:58 +08:00
|
|
|
for (const typename ELFT::Shdr &sec : sections) {
|
2016-04-08 03:24:51 +08:00
|
|
|
if (sec.sh_type != SHT_SYMTAB)
|
|
|
|
continue;
|
2017-03-31 05:13:00 +08:00
|
|
|
|
Speed up --start-lib and --end-lib.
--{start,end}-lib give files grouped by the options the archive file
semantics. That is, each object file between them acts as if it were
in an archive file whose sole member is the file.
Therefore, files between --{start,end}-lib are linked to the final
output only if they are needed to resolve some undefined symbols.
Previously, the feature was implemented this way:
1. We read a symbol table and insert defined symbols to the symbol
table as lazy symbols.
2. If an undefind symbol is resolved to a lazy symbol, that lazy
symbol instantiate ObjFile class for that symbol, which re-insert
all defined symbols to the symbol table.
So, if an ObjFile is instantiated, defined symbols are inserted to the
symbol table twice. Since inserting long symbol names is not cheap,
there's a room to optimize here.
This patch optimzies it. Now, LazyObjFile remembers symbol handles and
passed them over to a new ObjFile instance, so that the ObjFile
doesn't insert the same strings.
Here is a quick benchmark to link clang. "Original" is the original
lld with unmodified command line options. For "Case 1" and "Case 2", I
extracted all files from archive files and replace .a's in a command
line with .o's wrapped with --{start,end}-lib. I used the original lld
for Case 1" and use this patch for Case 2.
Original: 5.892
Case 1: 6.001 (+1.8%)
Case 2: 5.701 (-3.2%)
So, interestingly, --{start,end}-lib are now faster than the regular
linking scheme with archive files. That's perhaps not too surprising,
though, because for regular archive files, we look up the symbol table
with the same string twice.
Differential Revision: https://reviews.llvm.org/D62188
llvm-svn: 361473
2019-05-23 17:53:30 +08:00
|
|
|
// A symbol table is found.
|
|
|
|
ArrayRef<Elf_Sym> eSyms = CHECK(obj.symbols(&sec), this);
|
2018-03-29 06:55:40 +08:00
|
|
|
uint32_t firstGlobal = sec.sh_info;
|
Speed up --start-lib and --end-lib.
--{start,end}-lib give files grouped by the options the archive file
semantics. That is, each object file between them acts as if it were
in an archive file whose sole member is the file.
Therefore, files between --{start,end}-lib are linked to the final
output only if they are needed to resolve some undefined symbols.
Previously, the feature was implemented this way:
1. We read a symbol table and insert defined symbols to the symbol
table as lazy symbols.
2. If an undefind symbol is resolved to a lazy symbol, that lazy
symbol instantiate ObjFile class for that symbol, which re-insert
all defined symbols to the symbol table.
So, if an ObjFile is instantiated, defined symbols are inserted to the
symbol table twice. Since inserting long symbol names is not cheap,
there's a room to optimize here.
This patch optimzies it. Now, LazyObjFile remembers symbol handles and
passed them over to a new ObjFile instance, so that the ObjFile
doesn't insert the same strings.
Here is a quick benchmark to link clang. "Original" is the original
lld with unmodified command line options. For "Case 1" and "Case 2", I
extracted all files from archive files and replace .a's in a command
line with .o's wrapped with --{start,end}-lib. I used the original lld
for Case 1" and use this patch for Case 2.
Original: 5.892
Case 1: 6.001 (+1.8%)
Case 2: 5.701 (-3.2%)
So, interestingly, --{start,end}-lib are now faster than the regular
linking scheme with archive files. That's perhaps not too surprising,
though, because for regular archive files, we look up the symbol table
with the same string twice.
Differential Revision: https://reviews.llvm.org/D62188
llvm-svn: 361473
2019-05-23 17:53:30 +08:00
|
|
|
StringRef strtab = CHECK(obj.getStringTableForSymtab(sec, sections), this);
|
|
|
|
this->symbols.resize(eSyms.size());
|
|
|
|
|
|
|
|
// Get existing symbols or insert placeholder symbols.
|
|
|
|
for (size_t i = firstGlobal, end = eSyms.size(); i != end; ++i)
|
|
|
|
if (eSyms[i].st_shndx != SHN_UNDEF)
|
|
|
|
this->symbols[i] = symtab->insert(CHECK(eSyms[i].getName(strtab), this));
|
|
|
|
|
|
|
|
// Replace existing symbols with LazyObject symbols.
|
|
|
|
//
|
2019-05-23 17:58:08 +08:00
|
|
|
// resolve() may trigger this->fetch() if an existing symbol is an
|
|
|
|
// undefined symbol. If that happens, this LazyObjFile has served
|
|
|
|
// its purpose, and we can exit from the loop early.
|
Speed up --start-lib and --end-lib.
--{start,end}-lib give files grouped by the options the archive file
semantics. That is, each object file between them acts as if it were
in an archive file whose sole member is the file.
Therefore, files between --{start,end}-lib are linked to the final
output only if they are needed to resolve some undefined symbols.
Previously, the feature was implemented this way:
1. We read a symbol table and insert defined symbols to the symbol
table as lazy symbols.
2. If an undefind symbol is resolved to a lazy symbol, that lazy
symbol instantiate ObjFile class for that symbol, which re-insert
all defined symbols to the symbol table.
So, if an ObjFile is instantiated, defined symbols are inserted to the
symbol table twice. Since inserting long symbol names is not cheap,
there's a room to optimize here.
This patch optimzies it. Now, LazyObjFile remembers symbol handles and
passed them over to a new ObjFile instance, so that the ObjFile
doesn't insert the same strings.
Here is a quick benchmark to link clang. "Original" is the original
lld with unmodified command line options. For "Case 1" and "Case 2", I
extracted all files from archive files and replace .a's in a command
line with .o's wrapped with --{start,end}-lib. I used the original lld
for Case 1" and use this patch for Case 2.
Original: 5.892
Case 1: 6.001 (+1.8%)
Case 2: 5.701 (-3.2%)
So, interestingly, --{start,end}-lib are now faster than the regular
linking scheme with archive files. That's perhaps not too surprising,
though, because for regular archive files, we look up the symbol table
with the same string twice.
Differential Revision: https://reviews.llvm.org/D62188
llvm-svn: 361473
2019-05-23 17:53:30 +08:00
|
|
|
for (Symbol *sym : this->symbols) {
|
|
|
|
if (!sym)
|
2019-05-16 10:14:00 +08:00
|
|
|
continue;
|
2019-05-23 17:58:08 +08:00
|
|
|
sym->resolve(LazyObject{*this, sym->getName()});
|
2019-05-23 18:08:56 +08:00
|
|
|
|
2020-06-27 11:31:47 +08:00
|
|
|
// If fetched, stop iterating because this->symbols has been transferred
|
|
|
|
// to the instantiated ObjFile.
|
|
|
|
if (fetched)
|
Speed up --start-lib and --end-lib.
--{start,end}-lib give files grouped by the options the archive file
semantics. That is, each object file between them acts as if it were
in an archive file whose sole member is the file.
Therefore, files between --{start,end}-lib are linked to the final
output only if they are needed to resolve some undefined symbols.
Previously, the feature was implemented this way:
1. We read a symbol table and insert defined symbols to the symbol
table as lazy symbols.
2. If an undefind symbol is resolved to a lazy symbol, that lazy
symbol instantiate ObjFile class for that symbol, which re-insert
all defined symbols to the symbol table.
So, if an ObjFile is instantiated, defined symbols are inserted to the
symbol table twice. Since inserting long symbol names is not cheap,
there's a room to optimize here.
This patch optimzies it. Now, LazyObjFile remembers symbol handles and
passed them over to a new ObjFile instance, so that the ObjFile
doesn't insert the same strings.
Here is a quick benchmark to link clang. "Original" is the original
lld with unmodified command line options. For "Case 1" and "Case 2", I
extracted all files from archive files and replace .a's in a command
line with .o's wrapped with --{start,end}-lib. I used the original lld
for Case 1" and use this patch for Case 2.
Original: 5.892
Case 1: 6.001 (+1.8%)
Case 2: 5.701 (-3.2%)
So, interestingly, --{start,end}-lib are now faster than the regular
linking scheme with archive files. That's perhaps not too surprising,
though, because for regular archive files, we look up the symbol table
with the same string twice.
Differential Revision: https://reviews.llvm.org/D62188
llvm-svn: 361473
2019-05-23 17:53:30 +08:00
|
|
|
return;
|
2019-05-16 10:14:00 +08:00
|
|
|
}
|
2018-03-08 09:22:30 +08:00
|
|
|
return;
|
2017-04-27 06:51:51 +08:00
|
|
|
}
|
2016-04-08 03:24:51 +08:00
|
|
|
}
|
|
|
|
|
2020-05-15 13:18:58 +08:00
|
|
|
std::string elf::replaceThinLTOSuffix(StringRef path) {
|
2018-05-18 02:27:12 +08:00
|
|
|
StringRef suffix = config->thinLTOObjectSuffixReplace.first;
|
|
|
|
StringRef repl = config->thinLTOObjectSuffixReplace.second;
|
|
|
|
|
[ELF] -thinlto-object-suffix-replace=: don't error if the path does not end with old suffix
Summary:
For -thinlto-object-suffix-replace=old\;new, in
tools/gold/gold-plugin.cpp, the thinlto object filename is Path minus
optional old suffix.
static std::string getThinLTOObjectFileName(StringRef Path, StringRef OldSuffix,
StringRef NewSuffix) {
if (OldSuffix.empty() && NewSuffix.empty())
return Path;
StringRef NewPath = Path;
NewPath.consume_back(OldSuffix);
std::string NewNewPath = NewPath;
NewNewPath += NewSuffix;
return NewNewPath;
}
Currently lld will error that the path does not end with old suffix.
This patch makes lld accept such paths but only add new suffix if Path
ends with old suffix. This fixes a link error where bitcode members in
an archive are regular LTO objects without old suffix.
Acording to tejohnson, this will "enable supporting mix and match of
minimized ThinLTO bitcode files with normal ThinLTO bitcode files in a
single link (where we want to apply the suffix replacement to the
minimized files, and just ignore it for the normal ThinLTO files)."
Reviewers: ruiu, pcc, tejohnson, espindola
Reviewed By: tejohnson
Subscribers: emaste, inglorion, arichardson, eraman, steven_wu, dexonsmith, llvm-commits
Differential Revision: https://reviews.llvm.org/D51055
llvm-svn: 340364
2018-08-22 07:28:12 +08:00
|
|
|
if (path.consume_back(suffix))
|
|
|
|
return (path + repl).str();
|
2020-01-29 03:23:46 +08:00
|
|
|
return std::string(path);
|
2018-05-18 02:27:12 +08:00
|
|
|
}
|
|
|
|
|
2019-06-06 01:39:37 +08:00
|
|
|
template void BitcodeFile::parse<ELF32LE>();
|
|
|
|
template void BitcodeFile::parse<ELF32BE>();
|
|
|
|
template void BitcodeFile::parse<ELF64LE>();
|
|
|
|
template void BitcodeFile::parse<ELF64BE>();
|
ELF: New symbol table design.
This patch implements a new design for the symbol table that stores
SymbolBodies within a memory region of the Symbol object. Symbols are mutated
by constructing SymbolBodies in place over existing SymbolBodies, rather
than by mutating pointers. As mentioned in the initial proposal [1], this
memory layout helps reduce the cache miss rate by improving memory locality.
Performance numbers:
old(s) new(s)
Without debug info:
chrome 7.178 6.432 (-11.5%)
LLVMgold.so 0.505 0.502 (-0.5%)
clang 0.954 0.827 (-15.4%)
llvm-as 0.052 0.045 (-15.5%)
With debug info:
scylla 5.695 5.613 (-1.5%)
clang 14.396 14.143 (-1.8%)
Performance counter results show that the fewer required indirections is
indeed the cause of the improved performance. For example, when linking
chrome, stalled cycles decreases from 14,556,444,002 to 12,959,238,310, and
instructions per cycle increases from 0.78 to 0.83. We are also executing
many fewer instructions (15,516,401,933 down to 15,002,434,310), probably
because we spend less time allocating SymbolBodies.
The new mechanism by which symbols are added to the symbol table is by calling
add* functions on the SymbolTable.
In this patch, I handle local symbols by storing them inside "unparented"
SymbolBodies. This is suboptimal, but if we do want to try to avoid allocating
these SymbolBodies, we can probably do that separately.
I also removed a few members from the SymbolBody class that were only being
used to pass information from the input file to the symbol table.
This patch implements the new design for the ELF linker only. I intend to
prepare a similar patch for the COFF linker.
[1] http://lists.llvm.org/pipermail/llvm-dev/2016-April/098832.html
Differential Revision: http://reviews.llvm.org/D19752
llvm-svn: 268178
2016-05-01 12:55:03 +08:00
|
|
|
|
2017-07-27 06:13:32 +08:00
|
|
|
template void LazyObjFile::parse<ELF32LE>();
|
|
|
|
template void LazyObjFile::parse<ELF32BE>();
|
|
|
|
template void LazyObjFile::parse<ELF64LE>();
|
|
|
|
template void LazyObjFile::parse<ELF64BE>();
|
ELF: New symbol table design.
This patch implements a new design for the symbol table that stores
SymbolBodies within a memory region of the Symbol object. Symbols are mutated
by constructing SymbolBodies in place over existing SymbolBodies, rather
than by mutating pointers. As mentioned in the initial proposal [1], this
memory layout helps reduce the cache miss rate by improving memory locality.
Performance numbers:
old(s) new(s)
Without debug info:
chrome 7.178 6.432 (-11.5%)
LLVMgold.so 0.505 0.502 (-0.5%)
clang 0.954 0.827 (-15.4%)
llvm-as 0.052 0.045 (-15.5%)
With debug info:
scylla 5.695 5.613 (-1.5%)
clang 14.396 14.143 (-1.8%)
Performance counter results show that the fewer required indirections is
indeed the cause of the improved performance. For example, when linking
chrome, stalled cycles decreases from 14,556,444,002 to 12,959,238,310, and
instructions per cycle increases from 0.78 to 0.83. We are also executing
many fewer instructions (15,516,401,933 down to 15,002,434,310), probably
because we spend less time allocating SymbolBodies.
The new mechanism by which symbols are added to the symbol table is by calling
add* functions on the SymbolTable.
In this patch, I handle local symbols by storing them inside "unparented"
SymbolBodies. This is suboptimal, but if we do want to try to avoid allocating
these SymbolBodies, we can probably do that separately.
I also removed a few members from the SymbolBody class that were only being
used to pass information from the input file to the symbol table.
This patch implements the new design for the ELF linker only. I intend to
prepare a similar patch for the COFF linker.
[1] http://lists.llvm.org/pipermail/llvm-dev/2016-April/098832.html
Differential Revision: http://reviews.llvm.org/D19752
llvm-svn: 268178
2016-05-01 12:55:03 +08:00
|
|
|
|
2020-05-15 13:18:58 +08:00
|
|
|
template class elf::ObjFile<ELF32LE>;
|
|
|
|
template class elf::ObjFile<ELF32BE>;
|
|
|
|
template class elf::ObjFile<ELF64LE>;
|
|
|
|
template class elf::ObjFile<ELF64BE>;
|
2015-11-20 10:19:36 +08:00
|
|
|
|
2019-04-09 01:35:55 +08:00
|
|
|
template void SharedFile::parse<ELF32LE>();
|
|
|
|
template void SharedFile::parse<ELF32BE>();
|
|
|
|
template void SharedFile::parse<ELF64LE>();
|
|
|
|
template void SharedFile::parse<ELF64BE>();
|