Commit Graph

6920 Commits

Author SHA1 Message Date
Nico Weber 87248ba5b1 [lld/elf] Use C++17 nested namespace syntax in most places
Like D131405, but for ELF.

No behavior change.

Differential Revision: https://reviews.llvm.org/D131612
2022-08-10 16:47:30 -04:00
Fangrui Song e3fcf2e06f [ELF] Simplify llvm::enumerate with structured binding. NFC 2022-08-09 21:52:08 -07:00
Fangrui Song 596f490ab9 [ELF] De-template BitcodeFile::parse. NFC 2022-08-09 21:46:28 -07:00
Fangrui Song dc55ab3840 [ELF] De-template createBitcodeSymbol. NFC 2022-08-09 21:43:36 -07:00
Alex Brachet dbd04b853b [ELF] Support --package-metadata
This was recently introduced in GNU linkers and it makes sense for
ld.lld to have the same support. This implementation omits checking if
the input string is valid json to reduce size bloat.

Differential Revision: https://reviews.llvm.org/D131439
2022-08-08 21:31:58 +00:00
Fangrui Song ec04e45c03 [lld] LLVM_FALLTHROUGH => [[fallthrough]]. NFC
With C++17 there is no Clang pedantic warning or MSVC C5051.
2022-08-07 00:02:35 +00:00
Fangrui Song e45a5696bb [ELF] toString(const InputFile *): synchronize toStringCache
The function may be called currently for diagnostics.
2022-08-06 01:00:06 -07:00
Fangrui Song d7cbfcf36a [ELF][AArch64] Fix potentially corrupted section content for PAC
D74537 introduced a bug: if `(config->andFeatures & GNU_PROPERTY_AARCH64_FEATURE_1_PAC) != 0`
with -z pac-plt unspecified, we incorrectly use AArch64BtiPac, whose writePlt will make
out-of-bounds write after the .plt section. This is often benign because the
output section after .plt will usually overwrite the content.

This is very difficult to test without D131247 (Parallelize writes of different OutputSections).
2022-08-05 18:24:54 -07:00
Fangrui Song e89d6d2ac5 [ELF] Keep only getTarget() call. NFC
The place from D61712 seems unneeded now. We can just use the place added by
D62609 (support AArch64 BTI/PAC).
2022-08-05 18:20:23 -07:00
Fangrui Song abd9807590 [ELF] mergeCmp: work around irreflexivity bug
Some tests (e.g. aarch64-feature-pac.s) segfault in libstdc++ _GLIBCXX_DEBUG
builds (enabled by LLVM_ENABLE_EXPENSIVE_CHECKS).

dyn_cast<ThunkSection> is incorrectly true for any SyntheticSection. std::merge
transitively calls mergeCmp(x, x) (due to __glibcxx_requires_irreflexive_pred)
and will segfault in `ta->getTargetInputSection()`. The dyn_cast<ThunkSection>
issue should be eventually fixed properly, bug `a != b` is robust enough for now.
2022-08-05 17:08:37 -07:00
Fangrui Song 28d05d6723 [ELF][PPC64] Fix potentially corrupted section content with empty .got
D91426 makes .got possibly empty while needed. If .got and .data have the same
address, and .got's content is written after .data, the first word of .data will
be corrupted.

The bug is not testable without D131247.
2022-08-05 15:22:57 -07:00
Fangrui Song 2515cb80cd [ELF] Parallelize input section initialization
This implements the last step of
https://discourse.llvm.org/t/parallel-input-file-parsing/60164 for the ELF port.

For an ELF object file, we previously did: parse, (parallel) initializeLocalSymbols, (parallel) postParseObjectFile.
Now we do: parse, (parallel) initSectionsAndLocalSyms, (parallel) postParseObjectFile.

initSectionsAndLocalSyms does most of input section initialization.
The sequential `parse` does SHT_ARM_ATTRIBUTES/SHT_RISCV_ATTRIBUTES/SHT_GROUP initialization for now.

Performance linking some programs with --threads=8 (glibc 2.33 malloc and mimalloc):

* clang: 1.05x as fast with glibc malloc, 1.03x as fast with mimalloc
* chrome: 1.04x as fast with glibc malloc, 1.03x as fast with mimalloc
* internal search program: 1.08x as fast with glibc malloc, 1.05x as fast with mimalloc

Reviewed By: peter.smith

Differential Revision: https://reviews.llvm.org/D130810
2022-08-04 11:47:52 -07:00
Fangrui Song f6bd0a8f2b [ELF] Add makeThreadLocal/makeThreadLocalN and remove InputFile::localSymStorage
makeThreadLocal/makeThreadLocalN are moved from D130810 ([ELF] Parallelize input
section initialization) here to make D130810 more focused on the refactor:

* COFF has some needs for multiple linker contexts. D108850 partially removed
  global states from lldCommon but left the global variable `lctx`.
* To the best of my knowledge, all multiple-linker-context feature requests to
  ELF are more from user convenience, with no very strong argument.
* In practice, ELF port is very difficult to remove global states without
  introducing significant performance regression/hurting code readability.
* Per-thread allocators from D122922/D123879 are too expensive and will not
  really benefit ELF.

This patch adds a simple thread_local based makeThreadLocal to
lld/Common/Memory.h. It will enable further optimization in ELF.
2022-08-04 11:09:40 -07:00
Fangrui Song 077b16aa6c [ELF] Remove unneeded make<InputSection>. NFC 2022-08-03 21:51:39 -07:00
Fangrui Song e2a932dd8a [ELF] Move updateARMVFPArgs/updateARMVFPArgs. NFC
To reduce diff for D130810.
2022-08-03 21:49:17 -07:00
Gabriel Ravier 5dbd8faad5 [lld] Fixed a number of typos
I went over the output of the following mess of a command:

`(ulimit -m 2000000; ulimit -v 2000000; git ls-files -z | parallel --xargs -0 cat | aspell list --mode=none --ignore-case | grep -E '^[A-Za-z][a-z]*$' | sort | uniq -c | sort -n | grep -vE '.{25}' | aspell pipe -W3 | grep : | cut -d' ' -f2 | less)`

and proceeded to spend a few days looking at it to find probable typos
and fixed a few hundred of them in all of the llvm project (note, the
ones I found are not anywhere near all of them, but it seems like a
good start).

Differential Revision: https://reviews.llvm.org/D130982
2022-08-02 09:52:31 -04:00
Fangrui Song dde41c6c56 [ELF] --reproduce: strip directories for --print-archive-stats= and --why-extract=
Similar to -o and -Map.
2022-08-01 22:06:46 -07:00
Fangrui Song d0cf7b2015 [ELF] EhInputSection::getParentOffset: fix out-of-bounds access for symbols relative to a non-empty .eh_frame
This has unclear semantics and can be considered invalid. Return an arbitrary value.
2022-08-01 01:10:51 -07:00
Fangrui Song b2559f2f5c [ELF] .eh_frame: remove config->wordsize padding
Linux Standard Base Core Specification says that CIE/FDE is padded to an
addressing unit size boundary, but in practice GNU assembler/LLVM integrated
assembler pad FDE/CIE to 4 and the last FDE to 8 on 64-bit systems.

In addition, GNU ld doesn't pad to 8, so let's drop excess padding, too.
If the assembler provides aligned pieces, the output will be aligned.

Noticed .eh_frame size reduction for 3 executables: 0.3% (chrome), 4.7% (clang),
7.6% (an internal program).
2022-07-31 23:35:44 -07:00
Fangrui Song 81ed005c4c [ELF] Remove EhFrameSection::addSection. NFC 2022-07-31 19:55:05 -07:00
Fangrui Song b3452f8f13 [ELF] redirectSymbols: skip versioned symbol combine if config->versionDefinitions.size() == 2 2022-07-31 17:47:09 -07:00
Fangrui Song af1328ef45 [ELF] Simplify EhInputSection::split. NFC
* Inline getReloc
* Fold the UINT32_MAX length check into the section size check.
  This transformation is valid because we don't support .eh_frame input sections
  larger than 32-bit (unrealistic even for large code models).
2022-07-31 16:59:57 -07:00
Fangrui Song 3e9adff456 [ELF] Split EhInputSection::pieces into cies and fdes
This simplifies code, removes a read32 (for id==0 check), and makes it feasible
to combine some operations in EhInputSection::split and EhFrameSection::addRecords.

Mostly NFC, but fixes "Relocation not in any piece" assertion failure in an
erroneous case when a relocation offset precedes all CIE/FDE pices.
2022-07-31 16:16:10 -07:00
Fangrui Song c09d323599 [ELF] Move EhInputSection out of inputSections. NFC
inputSections temporarily contains EhInputSection objects mainly for
combineEhSections. Place EhInputSection objects into a new vector
ehInputSections instead of inputSections.
2022-07-31 11:58:08 -07:00
Fangrui Song 4b2b68d5ab [lld] Change vector to SmallVector. NFC
My lld executable is 1.6KiB smaller and some functions are now more efficient.
2022-07-30 18:11:21 -07:00
Fangrui Song a465e79f19 [ELF] Move SyntheticSections to InputSection.h. NFC
Keep the main SectionBase hierarchy in InputSection.h.
And inline MergeInputSection::getParent.
2022-07-30 17:42:08 -07:00
Fangrui Song 0a28cfdff5 [ELF] Simplify getRankProximity. NFC 2022-07-30 16:32:42 -07:00
Fangrui Song 3046e7d8e7 [ELF] Support missing relocations in RISCV::getImplicitAddend() 2022-07-30 10:55:59 -07:00
Fangrui Song 97b767eb3b [ELF] Support missing relocations in PPC64::getImplicitAddend() 2022-07-30 10:46:58 -07:00
Kazu Hirata 729a76433a [lld] Remove unused forward declarations (NFC) 2022-07-30 10:35:46 -07:00
Fangrui Song efb7a71a13 [ELF] Support missing relocations in AArch64::getImplicitAddend() 2022-07-30 01:24:55 -07:00
Fangrui Song c4c68b676e [ELF] Write R_AARCH64_IRELATIVE addends with -z rel 2022-07-30 01:19:59 -07:00
Fangrui Song d6d4f631ac [ELF] Move addDependentLibrary/handleSectionGroup. NFC
To reduce diff for my upcoming parallel input section initialization patch.
2022-07-29 17:07:09 -07:00
Alex Brachet 7baf1db392 [ELF] Strip directories for -Map when emitting reproducer rsp
Similarly to -o output directories will not be created so -Map being
copied verbatim will likely cause ld.lld @response.txt to fail.

Differential Revision: https://reviews.llvm.org/D130681
2022-07-29 16:17:33 +00:00
Fangrui Song a041ce3eb1 [ELF] CallGraphSort: replace vector<int> with unique_ptr<int[]>. NFC
We can't use C++20 make_unique_for_overwrite yet.
2022-07-29 00:59:48 -07:00
Fangrui Song 2e2d5304f0 [ELF] Move combineEhSections from Writer to SyntheticSections. NFC
This not only places the function in the right place, but also allows inlining addSection.
2022-07-29 00:47:30 -07:00
Fangrui Song c72973608d [ELF] Combine EhInputSection removal and MergeInputSection removal. NFC 2022-07-29 00:39:57 -07:00
Fangrui Song 8d4b11b4f1 [ELF] Remove redundant isa<InputSection>(sec). NFC
combineEhSections has been called to remove EhInputSection.
2022-07-29 00:30:52 -07:00
Fangrui Song 4db7c7dbb4 [ELF] Remove one inputSections loop. NFC 2022-07-29 00:03:50 -07:00
Fangrui Song b3270888cb [ELF] addDependentLibrary: fix a use-after-free bug in archiveName 2022-07-26 14:52:07 -07:00
Fangrui Song 6d0b42745e [ELF] addLibrary: fix a use-after-free bug in archiveName
It manifests as an incorrect name in --print-archive-stats=.
2022-07-26 14:36:57 -07:00
Fangrui Song b89407153d [ELF] --reproduce: support --export-dynamic-symbol-list 2022-07-25 01:20:32 -07:00
Fangrui Song cbcdb5248d [ELF] Simplify --build-id/--color-diagnostics with AliasArgs. NFC 2022-07-25 01:14:53 -07:00
Fangrui Song 85cfd91723 [ELF] Optimize some non-constant alignTo with alignToPowerOf2. NFC
My x86-64 lld executable is 2KiB smaller. .eh_frame writing gets faster as there
were lots of divisions.
2022-07-24 11:20:49 -07:00
Kazu Hirata 1cc7f5bede Use static_assert instead of assert (NFC)
Identified with misc-static-assert.
2022-07-23 09:22:27 -07:00
Dmitri Gribenko aba43035bd Use llvm::sort instead of std::sort where possible
llvm::sort is beneficial even when we use the iterator-based overload,
since it can optionally shuffle the elements (to detect
non-determinism). However llvm::sort is not usable everywhere, for
example, in compiler-rt.

Reviewed By: nhaehnle

Differential Revision: https://reviews.llvm.org/D130406
2022-07-23 15:19:05 +02:00
Fangrui Song 50f5f37b07 [ELF] Internalize isBitcode. NFC 2022-07-22 11:56:46 -07:00
Fangrui Song e8fd49f24b [ELF] --fortran-common: remove unneeded identify_magic call. NFC 2022-07-22 11:54:27 -07:00
Fangrui Song 242316bc27 [ELF] Simplify createObjectFile/createLazyFile. NFC
And avoid redundant identify_magic test.
2022-07-22 01:26:12 -07:00
Kazu Hirata 5cff5142a8 Use value instead of getValue (NFC) 2022-07-15 20:03:13 -07:00