Commit Graph

14834 Commits

Author SHA1 Message Date
Fangrui Song 1d285f2de0 [ELF] Simplify and optimize ObjFile<ELFT>::parseLazy 2021-12-23 20:23:13 -08:00
Fangrui Song 1abbbc7b24 [ELF] scanVersionScript: remove unused variable 2021-12-23 18:18:25 -08:00
Fangrui Song a2baf634a1 [ELF] Simplify SymbolTable::insert. NFC 2021-12-23 17:59:25 -08:00
Fangrui Song 417cd2e5c5 [ELF] SymbolTable: change some vector<Symbol *> to SmallVector
The generated assembly for Symbol::insert is much shorter (std::vector resize is
inefficient) and enables some inlining.
2021-12-23 16:49:38 -08:00
Fangrui Song 464cc4c920 [ELF] Remove stale comment which was duplicated in MarkLive<ELFT>::run
Pointed out by thakis
2021-12-23 15:13:46 -08:00
Kristina Bessonova 81378f7e56 Revert "[DwarfDebug] Support emitting function-local declaration for a lexical block" & dependent patches
Try to revert D113741 once again.

This also reverts 0ac75e82ff (D114705)
as it causes LLDB's lldb-api.lang/cpp/nsimport.TestCppNsImport.py test
failure w/o D113741.

This reverts commit f9607d45f3.

Differential Revision: https://reviews.llvm.org/D116225
2021-12-24 00:47:04 +02:00
Fangrui Song bf45624ba0 [ELF][PPC32] Support .got2 in an output section description
I added `PPC32Got2Section` D62464 to support .got2 but did not implement .got2
in another output section.

PR52799 has a linker script placing .got2 in .rodata, which causes a null
pointer dereference because a MergeSyntheticSection's file is nullptr.
Add the support.
2021-12-23 11:32:44 -08:00
Fangrui Song 4374824ccf [ELF] --gc-sections: combine two iterations over inputSections
There is a slight speed-up.
2021-12-23 09:53:08 -08:00
Fangrui Song 33319dde2a [ELF] LTO: skip expensive usedStartStop initialization if bitcodeFiles.empty()
This may cost 1.3+% of total link time.
2021-12-23 01:52:54 -08:00
Fangrui Song 61312fd5aa [ELF] sortSections: delete unneeded outSecOff assignment
Related to D45368 but outSecOff is unneeded because resolveShfLinkOrder uses
stable_sort.
2021-12-23 01:24:32 -08:00
Fangrui Song 5d0be553fa [ELF] Optimize copyLocalSymbols. NFC 2021-12-23 00:59:29 -08:00
Fangrui Song ad26b0b233 Revert "[ELF] Make Partition/InStruct members unique_ptr and remove associate make<XXX>"
This reverts commit e48b1c8a27.
This reverts commit d019de23a1.

The changes caused memory leaks (non-final classes cannot use unique_ptr).
2021-12-22 23:55:11 -08:00
Fangrui Song ba948c5a9c [ELF] Use SmallVector for some global variables (*Files and *Sections). NFC
My lld executable is 26+KiB smaller.
2021-12-22 22:30:08 -08:00
Fangrui Song ba6973c89b [ELF] Change nonnull pointer parameters to references 2021-12-22 22:02:29 -08:00
Fangrui Song e48b1c8a27 [ELF] Make Partition members unique_ptr and remove associate make<XXX>
See D116143 for benefits. My lld executable (x86-64) is 103+KiB smaller.
2021-12-22 21:34:26 -08:00
Fangrui Song d019de23a1 [ELF] Make InStruct members unique_ptr and remove associate make<XXX>
See D116143 for benefits. My lld executable (x86-64) is 24+KiB smaller.
2021-12-22 21:11:26 -08:00
Fangrui Song 5c75cc51b3 [ELF] Change nonnull pointer parameters to references. NFC 2021-12-22 21:09:57 -08:00
Fangrui Song baa3eb0dd9 [ELF] Change some non-null pointer parameters to references. NFC 2021-12-22 20:51:11 -08:00
Fangrui Song 3a5fb57393 [ELF] Replace LazyObjFile with lazy ObjFile/BitcodeFile
The new `lazy` state is the inverse of the previous `LazyObjFile::extracted`.
There are many advantages:

* previously when a LazyObjFile was extracted, a new ObjFile/BitcodeFile was created; now the file is reused, just with `lazy` cleared
* avoid the confusing transfer of `symbols` from LazyObjFile to the new file
* the `incompatible file:` diagnostic is unified with `is incompatible with`
* simpler code, smaller executable (6200+ bytes smaller on x86-64)
* make eager parsing feasible (for parallel section/symbol table initialization)
2021-12-22 17:41:50 -08:00
Fangrui Song 5fc4323eda [ELF] Change some global pointers to unique_ptr
Currently the singleton `config` is assigned by `config = make<Configuration>()`
and (if `canExitEarly` is false) destroyed by `lld::freeArena`.

`make<Configuration>` allocates a stab with `malloc(4096)`. This both wastes
memory and bloats the executable (every type instantiates `BumpPtrAllocator`
which costs more than 1KiB code on x86-64).

(No need to worry about `clang::no_destroy`. Regular invocations (`canExitEarly`
is true) call `_Exit` via llvm::sys::Process::ExitNoCleanup.)

Reviewed By: lichray

Differential Revision: https://reviews.llvm.org/D116143
2021-12-22 14:36:14 -08:00
Fangrui Song eb37330ac7 [ELF] Change mipsGotIndex to uint32_t
This does not decrease sizeof(InputSection) (important for memory usage) on
ELF64 by itself but allows we to add another uint32_t.
2021-12-21 20:19:51 -08:00
Fangrui Song 48161b7490 [ELF] --gc-sections: Work around SHT_PROGBITS .init_array
Older Go cmd/link used SHT_PROGBITS for .init_array .
Work around the lack of https://golang.org/cl/373734 for a while.
It does not generate .fini_array or .preinit_array
2021-12-21 10:44:29 -08:00
Fangrui Song 6683099a0d [ELF] Optimize RelocationSection<ELFT>::writeTo
When linking a 1.2G output (nearly no debug info, 2846621 dynamic relocations) using `--threads=8`, I measured

```
9.131462 Total ExecuteLinker
1.449913 Total Write output file
1.445784 Total Write sections
0.657152 Write sections {"detail":".rela.dyn"}
```

This change decreases the .rela.dyn time to 0.25, leading to 4% speed up in the total time.

* The parallelSort is slow because of expensive r_sym/r_offset computation. Cache the values.
* The iteration is slow. Move r_sym/r_addend computation ahead of time and parallelize it.

With the change, the new encodeDynamicReloc is cheap (0.05s). So no need to parallelize it.

Reviewed By: ikudrin

Differential Revision: https://reviews.llvm.org/D115993
2021-12-21 09:43:44 -08:00
Fangrui Song c2f2bb066b [ELF] Remove unneeded SectionBase::repl indirection
sec->repl equals sec after rL371216.
2021-12-21 00:39:16 -08:00
Esme-Yi b66328701a [PowerPC][llvm-objdump] enable --symbolize-operands for PowerPC ELF/XCOFF.
Summary: When disassembling, symbolize a branch target operand
to print a label instead of a real address.

Reviewed By: shchenz

Differential Revision: https://reviews.llvm.org/D114492
2021-12-21 04:17:57 +00:00
Xu Mingjie cb63ad8d1d [LTO] Fix incomplete optimization remarks for dead functions when PreOptModuleHook or PostInternalizeModuleHook is defined
In 20a895c4be, we introduce `finalizeOptimizationRemarks()` to make sure we flush the diagnostic remarks file in case the linker doesn't call the global destructors before exiting.
In https://reviews.llvm.org/D73597, we add optimization remarks for removed functions for debugging or for detecting dead code.
But there is a case, if PreOptModuleHook or PostInternalizeModuleHook is defined (e.g. `--plugin-opt=emit-llvm` is passed to linker), we do not call `finalizeOptimizationRemarks()`, therefore we will get an incomplete optimization remarks file.
This patch make sure we flush the diagnostic remarks file when PreOptModuleHook or PostInternalizeModuleHook is defined.

Reviewed By: tejohnson, MaskRay

Differential Revision: https://reviews.llvm.org/D115417
2021-12-20 18:16:09 -08:00
Fangrui Song 8825ffdbde [ELF] --time-trace: Trace "Write sections"
writeSections is typically a bottleneck.
This was used to track down the following bottlenecks:

* Output section .rela.dyn (9115d75117)
* Output section .debug_str (3aae04c744)
* posix_fallocate is slow for Linux tmpfs: D115957

Reviewed By: ikudrin

Differential Revision: https://reviews.llvm.org/D115984
2021-12-20 10:51:24 -08:00
Fangrui Song bee5bc9075 [ELF] #undef PPC to support GCC powerpc32 build
GCC's powerpc32 port predefines `PPC` as a macro in GNU C++ mode in some configurations (Linux,
FreeBSD, and some others. See `builtin_define_std ("PPC"); ` in gcc/config/rs6000).

```
% powerpc-linux-gnu-g++ -E -dM -xc++ /dev/null -o - | grep -w PPC
#define PPC 1
```

Fixes https://bugs.gentoo.org/829599

Reviewed By: thesamesam

Differential Revision: https://reviews.llvm.org/D116017
2021-12-20 10:12:51 -08:00
Fangrui Song 3aae04c744 [ELF] Parallelize MergeNoTailSection::writeTo
With this patch, writing .debug_str is significantly for a program with
1.5G .debug_str:

* .debug_info 1.22s
* .debug_str 2.57s decreases to 0.66
2021-12-17 23:30:42 -08:00
Fangrui Song 552d84414d [ELF] Use SmallVector for many SyntheticSections. NFC
This decreases struct sizes and usually decreases the lld executable
size (39KiB for my x86-64 executable) (unless in some cases smaller
SmallVector leads to more inlining, e.g. StringTableBuilder).
For --gdb-index, there may be memory usage saving.
2021-12-17 19:22:16 -08:00
Vy Nguyen 4f90e67e2f [lld-macho] Handle $ld$hide[$os] symbols.
PR/52708

Differential Revision: https://reviews.llvm.org/D115775
2021-12-17 16:40:07 -05:00
Nico Weber c4b45eeb44 [lld/mac] Don't lose "weak ref" bit when doing LTO
Fixes #52778.

Probably fixes Chromium crashing on startup on macOS 10.15 (and older) systems
when building with LTO, but I haven't verified that yet.

Differential Revision: https://reviews.llvm.org/D115949
2021-12-17 15:26:35 -05:00
Nico Weber a3096ca9b4 [lld/test] List one test dep per line
Matches llvm's and clang's /test/CMakeLists.txt, makes it easier to
see in diffs which deps get added, and makes it easier to see if
a given dependency is present or not.

No behavior change.
2021-12-17 09:51:01 -05:00
Fangrui Song aa27bab5a1 [ELF] InputSection::writeTo: reorder type checks and add LLVM_UNLIKELY 2021-12-16 23:42:50 -08:00
Fangrui Song 054cdb34a2 [ELF] Optimize MergeInputSection::splitNonStrings. NFC 2021-12-16 21:23:00 -08:00
Fangrui Song 4c98d08841 [ELF] Speed up MergeInputSection::split*. NFC 2021-12-16 21:17:02 -08:00
Fangrui Song bf4fa3036a [ELF] Use SmallVector for MergeInputSection::pieces. NFC
sizeof(pieces) decreases from 24 to 16 on ELF64.
One BumpPtrAllocator can store more MergeInputSections.
The lld executable becomes smaller.
2021-12-16 21:07:39 -08:00
Fangrui Song 93558e575e [ELF] Internalize createMergeSynthetic. NFC
Only called once. Moving to OutputSections.cpp can make it inlined.
finalizeInputSections can be very hot, especially in -O1 links with much debug info.
2021-12-16 20:50:06 -08:00
Daniel Kiss 2b4e6052b3 [lld] Add cet-report and bti-report flags
Implement cet-report as supported in binutils.
bti-report has the same behaviour for AArch64-BTI.

Fixes https://github.com/llvm/llvm-project/issues/44828

Reviewed By: MaskRay

Differential Revision: https://reviews.llvm.org/D113901
2021-12-16 16:26:26 +01:00
Fangrui Song 8617996ac1 [ELF] maybeReportUndefined: move sym.isUndefined() check to the caller. NFC
Avoid a function call in the majority of cases.
2021-12-16 00:27:19 -08:00
Fangrui Song 101407bfaa [ELF] parseSymbolVersion: remove unussed pos == 0 check 2021-12-15 23:59:55 -08:00
Fangrui Song 60f5614931 [ELF] SharedFile::parse: cache symbols size for a loop. NFC 2021-12-15 22:45:28 -08:00
Fangrui Song 7b265e9791 [ELF] Move -l -L canonical and --library-path --library aliases
Everyone uses -l -L instead of the long option counterparts.
Make help messages attach to -L -l and (--reproduce) use them for response.txt
command line options.
2021-12-15 21:49:53 -08:00
Fangrui Song 159b948e43 [ELF] ObjFile<ELFT>::initializeSymbols: don't call Allocate when firstGlobal==0
Calling `Allocate` with 0 size (when .symtab is absent, e.g.
`invalid/mips-invalid-options-descriptor.test`) may return a nullptr, which will
crash with -fsanitize=null (the underlying `Allocate` function is
LLVM_ATTRIBUTE_RETURNS_NONNULL).
2021-12-15 18:21:48 -08:00
Fangrui Song b0211de5e3 [ELF] Change Symbol::verdefIndex from uint32_t to uint16_t
The SHT_GNU_version index is 16-bit, so the 32-bit value is a waste.
Technically non-default version index 0x7fff uses version index 0xffff,
but it is impossible in practice.

This change decreases sizeof(SymbolUnion) from 80 to 72 on ELF64 platforms.
Memory usage decreases by 1% when linking a large executable.
2021-12-15 17:59:30 -08:00
Fangrui Song 50187d2dd5 [ELF] Speed up ObjFile<ELFT>::createInputSection
* Group ".note" section name checks
* Move shouldMerge check to the caller
2021-12-15 17:15:32 -08:00
Vincent Lee d17b092fe6 [lld-macho] Make writing map file asynchronous
For large applications that write to map files, writing map files can take quite
a bit of time. Sorting the biggest contributors to link times, writing map files
ranks in at 2nd place, with load input files being the biggest contributor of
link times. Avoiding writing map files on the critical path (and having its own
thread) saves ~2-3 seconds when linking chromium framework on a 16-Core
Intel Xeon W.

```
           base            diff            difference (95% CI)
sys_time   1.617 ± 0.034   1.657 ± 0.026   [  +1.5% ..   +3.5%]
user_time  28.536 ± 0.245  28.609 ± 0.180  [  -0.1% ..   +0.7%]
wall_time  23.833 ± 0.271  21.684 ± 0.194  [  -9.5% ..   -8.5%]
samples    31              24
```

Reviewed By: #lld-macho, oontvoo, int3

Differential Revision: https://reviews.llvm.org/D115416
2021-12-15 16:37:04 -08:00
Fangrui Song 68009b78f2 [ELF] Symbol::replace: remove dead code 2021-12-15 16:08:18 -08:00
Fangrui Song b5805b7847 [ELF] ObjFile<ELFT>::initializeSymbols: avoid StringRefZ from undefined symbols 2021-12-15 15:30:18 -08:00
Fangrui Song 2bdad16303 [ELF] SymbolTable::insert: keep @@ in the name
* Avoid the name truncation quirk in SymbolTable::insert: the truncated name will be replaced by @@ again.
* Allow foo and foo@@v1 in different files to be diagnosed as duplicate definition error (GNU ld behavior)
* Avoid potential redundant strlen on symbol name due to StringRefZ in ObjFile<ELFT>::initializeSymbols
2021-12-15 15:19:35 -08:00
Fangrui Song a8d6d2614b [ELF] Replace make<Defined> with makeDefined. NFC
This removes SpecificAlloc<Defined> and makes my lld executable 1.5k smaller.
This drops the small memory waste due to the separate BumpPtrAllocator.
2021-12-15 13:15:03 -08:00
Fangrui Song a596a5fc12 [ELF] ObjFile<ELFT>::initializeSymbols: Simplify this->symbols[i]. NFC 2021-12-15 13:02:38 -08:00
Fangrui Song 509153f1e7 [ELF] ObjFile<ELFT>::initializeSymbols: Batch allocate local symbols
and detangle local/global symbol initialization.

My x86-64 lld executable is 8k smaller due to the removal of SpecificAlloc<Undefined>.
2021-12-15 12:54:39 -08:00
Fangrui Song 3534d26cc1 [ELF] Slightly speed up -z keep-text-section-prefix 2021-12-15 10:20:11 -08:00
Fangrui Song 7c0881a38f [ELF] --gc-sections: Change startwith(".jcr") to exact match
GNU ld's internal linker script keeps `.jcr`, but not other sections
starting with `.jcr`.
2021-12-15 01:27:08 -08:00
Fangrui Song 21dbfd4300 [ELF] --gc-sections: Change startwith(".init") (and ".fini") to exact match
GNU ld's internal linker script keeps `.init`, but not other sections starting
with `.init`. .fini is similar.
2021-12-15 01:16:26 -08:00
Fangrui Song 7a54ae9c1d [ELF] Change objectFiles to ELFFileBase *
This can sometimes avoid `cast<ObjFile<...>>`.

I intentionally do not touch postScanRelocations to wait for its stabilization.
2021-12-15 00:37:10 -08:00
Fangrui Song 3deb82cd07 [ELF] Adjust getOutputSectionName prefix order
Sorting the prefixes by decreasing frequency can improve performance.
.gcc_except_table is relatively frequent, so move it ahead.
.ctors and .dtors mostly disappear and should be the last.
2021-12-15 00:18:58 -08:00
Fangrui Song 5816f1855c [ELF] Slightly speed up getOutputSectionName. NFC 2021-12-14 23:43:00 -08:00
Fangrui Song 89661a0e89 [ELF] Remove dead code from SymbolTable::find 2021-12-14 22:41:52 -08:00
Fangrui Song c720b16aa5 [ELF] Use SmallVector for SharedFile and simplify parseVerdefs
SHT_GNU_verdef is typically small, so it's unnecessary to reserve the vector.

While here, fix a hypothetical issue when SHT_GNU_verdef has non-increasing
version indexes, which don't happen with GNU ld, gold, ld.lld's output.

My x86-64 lld executable is 256 bytes smaller.
2021-12-14 21:11:45 -08:00
Fangrui Song 1ff1d50d9f [ELF] Make InputFile smaller
sizeof(ObjFile<ELF64LE>) is decreased from 344 to 272 on an ELF64 system.
In a large link with 30000 ObjFiles, this may be 2+MiB saving.

Change std::vector members to SmallVector, and std::string members to
SmallString<0> (these members typically don't benefit from small string optimization).
On Linux x86-64 the lld executable is ~6k smaller.
2021-12-14 20:55:32 -08:00
Fangrui Song cf783be8d7 Reland D114783/D115603 [ELF] Split scanRelocations into scanRelocations/postScanRelocations
(Fixed an issue about GOT on a copy relocated alias.)
(Fixed an issue about not creating r_addend=0 IRELATIVE for unreferenced non-preemptible ifunc.)

The idea is to make scanRelocations mark some actions are needed (GOT/PLT/etc)
and postpone the real work to postScanRelocations. It gives some flexibility:

* Make it feasible to support .plt.got (PR32938): we need to know whether GLOB_DAT and JUMP_SLOT are both needed.
* Make non-preemptible IFUNC handling slightly cleaner: avoid setting/clearing sym.gotInIgot
* -z nocopyrel: report all copy relocation places for one symbol
* Make GOT deduplication feasible
* Make parallel relocation scanning feasible (if we can avoid all stateful operations and make Symbol attributes atomic), but parallelism may not be the appealing choice

Since this patch moves a large chunk of code out of ELFT templates. My x86-64
executable is actually a few hundred bytes smaller.

For ppc32-ifunc-nonpreemptible-pic.s: I remove absolute relocation references to non-preemptible ifunc
because absolute relocation references are incorrect in -fpie mode.

Reviewed By: peter.smith, ikudrin

Differential Revision: https://reviews.llvm.org/D114783
2021-12-14 16:28:41 -08:00
Fangrui Song 04cf411c94 [ELF][test] Test unreferenced non-preemptible ifunc
Add missing coverage exposed by D114783.
There should be no associated IRELATIVE, otherwise (a) glibc ld.so may
crash (b) it wastes space (c) unused IPLT causes confusion.
2021-12-14 16:25:50 -08:00
Fangrui Song ea15b862d7 Revert D114783 [ELF] Split scanRelocations into scanRelocations/postScanRelocations
May cause a failure for non-preemptible `bcmp` in a glibc -static link.
2021-12-14 14:33:50 -08:00
Stephan T. Lavavej 8bd106a891 [NFC] Fix typos in release notes.
Reviewed By: ldionne, Mordante, MaskRay

Differential Revision: https://reviews.llvm.org/D115685
2021-12-14 14:19:42 -08:00
Fangrui Song 6a44013b0e [ELF] -Map: Print symbols which needs canonical PLT entry/copy relocation just once
If a copy related symbol (say `copy`) is referenced in two .o
files, this change removes a duplicated line from the -Map output:

```
          202470           202470        1     1 .bss.rel.ro
          202470           202470        1     1         <internal>:(.bss.rel.ro)
          202470           202470        1     1                 copy
removed   202470           202470        1     1                 copy
```

Differential Revision: https://reviews.llvm.org/D115697
2021-12-14 10:31:06 -08:00
Fangrui Song b79686c6dc [ELF] Remove needsPltAddr in favor of needsCopy
needsPltAddr is equivalent to `needsCopy && isFunc`. In many places, it is
equivalent to `needsCopy` because the non-STT_FUNC cases are ruled out.

Reviewed By: ikudrin, peter.smith

Differential Revision: https://reviews.llvm.org/D115603
2021-12-14 09:52:43 -08:00
Fangrui Song e7a95b0674 Reland [ELF] Split scanRelocations into scanRelocations/postScanRelocations
(Fixed an issue about GOT on a copy relocated alias.)

The idea is to make scanRelocations mark some actions are needed (GOT/PLT/etc)
and postpone the real work to postScanRelocations. It gives some flexibility:

* Make it feasible to support .plt.got (PR32938): we need to know whether GLOB_DAT and JUMP_SLOT are both needed.
* Make non-preemptible IFUNC handling slightly cleaner: avoid setting/clearing sym.gotInIgot
* -z nocopyrel: report all copy relocation places for one symbol
* Make GOT deduplication feasible
* Make parallel relocation scanning feasible (if we can avoid all stateful operations and make Symbol attributes atomic), but parallelism may not be the appealing choice

Since this patch moves a large chunk of code out of ELFT templates. My x86-64
executable is actually a few hundred bytes smaller.

For ppc32-ifunc-nonpreemptible-pic.s: I remove absolute relocation references to non-preemptible ifunc
because absolute relocation references are incorrect in -fpie mode.

Reviewed By: peter.smith, ikudrin

Differential Revision: https://reviews.llvm.org/D114783
2021-12-13 20:11:24 -08:00
Fangrui Song d1014d9e6d [ELF] Improve test for copy relocations on aliases 2021-12-13 20:04:24 -08:00
Fangrui Song 0b8b86e30f Revert "[ELF] Split scanRelocations into scanRelocations/postScanRelocations"
This reverts commit fc33861d48.

`replaceWithDefined` should copy needsGot, otherwise an alias for a copy
relocated symbol may not have GOT entry if its needsGot was originally true.
2021-12-13 19:29:53 -08:00
Noah Shutty fb6b103daa [lld] Replace Symbolize.h with DIContext.h in lld's COFF lib
lld only needs DIContext.h which it gets through Symbolize.h -> SymbolizableModule.h -> DIContext.h. This replaces it with a direct include of DIContext.h to avoid any confusion and pulling in unnecessary headers.

Reviewed By: phosek

Differential Revision: https://reviews.llvm.org/D115659
2021-12-13 22:16:41 +00:00
Fangrui Song fc33861d48 [ELF] Split scanRelocations into scanRelocations/postScanRelocations
The idea is to make scanRelocations mark some actions are needed (GOT/PLT/etc)
and postpone the real work to postScanRelocations. It gives some flexibility:

* Make it feasible to support .plt.got (PR32938): we need to know whether GLOB_DAT and JUMP_SLOT are both needed.
* Make non-preemptible IFUNC handling slightly cleaner: avoid setting/clearing sym.gotInIgot
* -z nocopyrel: report all copy relocation places for one symbol
* Make parallel relocation scanning possible (if we can avoid all stateful operations and make Symbol attributes atomic), but parallelism may not be the appealing choice
* Make GOT deduplication feasible

Since this patch moves a large chunk of code out of ELFT templates. My x86-64
executable is actually a few hundred bytes smaller.

For ppc32-ifunc-nonpreemptible-pic.s: I remove absolute relocation references to non-preemptible ifunc
because absolute relocation references are incorrect in -fpie mode.

Reviewed By: peter.smith, ikudrin

Differential Revision: https://reviews.llvm.org/D114783
2021-12-13 09:56:52 -08:00
Fangrui Song 9115d75117 [ELF] Use parallelSort for .rela.dyn
An unstable sort suffices. In a large link (11.06s), this decreases .rela.dyn
writeTo time from 1.52s to 0.81s, resulting in 6% total time speedup (the
benefit will greatly dilute if --pack-dyn-relocs=relr becomes prevailing).

Encoding the dynamic relocations then sorting raw Elf_Rel/Elf_Rela doesn't seem
to improve much (doing that would require code duplicate because of
Elf_Rel/Elf_Rela plus unfortunate mips64le), so don't do that.
2021-12-12 20:53:06 -08:00
Fangrui Song 1eaa9b4374 [ELF] initializeSections: move SHT_LLVM_CALL_GRAPH_PROFILE check into SHF_EXCLUDE && !relocatable. NFC
Avoid a comparison in the majority of cases.
2021-12-12 20:05:21 -08:00
Fangrui Song d29766bb48 [ELF] relocateAlloc: remove variables type and expr. NFC 2021-12-12 19:31:30 -08:00
Fangrui Song 4cfff19b88 [ELF] Move adjustSplitStackFunctionPrologues's splitStack check to the caller. NFC
Avoid a function call in the majority of cases and make the output smaller.
2021-12-12 19:26:03 -08:00
Fangrui Song a8024dfc06 [ELF] Avoid mutable addend parameter. NFC 2021-12-12 19:12:01 -08:00
Fangrui Song af520fba2e [ELF][test] Remove unused/incorrect .got check line 2021-12-12 10:51:05 -08:00
Jez Ng 098430cd25 [lld-macho][nfc] Simplify LC_DATA_IN_CODE generation
1. After D113241, we have the section address easily accessible and no
   longer need to iterate across the LC_SEGMENT commands to emit
   LC_DATA_IN_CODE.

2. There's no need to store a pointer to the data in code entries during
   the parse step; we can just look it up as part of the output step.

Reviewed By: #lld-macho, thakis

Differential Revision: https://reviews.llvm.org/D115556
2021-12-11 01:01:57 -05:00
Jez Ng 40bcbe48e8 [lld-macho][nfc] InputSections don't need to track their total # of callsites
... only whether they have more than zero. This simplifies the code slightly.

I've also moved the field into the ConcatInputSection subclass since it doesn't
actually get used by the other InputSections.

Reviewed By: #lld-macho, oontvoo

Differential Revision: https://reviews.llvm.org/D115539
2021-12-11 01:01:57 -05:00
Jez Ng 8a1f2d6580 [lld-macho] Include archive name in bitcode files
Reviewed By: MaskRay

Differential Revision: https://reviews.llvm.org/D115281
2021-12-07 19:11:23 -05:00
Igor Kudrin ce25eb12dd [ELF] Do not report undefined weak references in shared libraries
This fixes an issue introduced in D101996.

A weak reference in a shared library could be incorrectly reported if
there is another library that has a strong reference to the same symbol.

Differential Revision: https://reviews.llvm.org/D115041
2021-12-07 10:10:51 +07:00
Chris Davis e4eb6216c2 Enable pdbpagesize to allow support for PDB file sizes > 4GB
Enable the pdbpagesize flag to allow linking of PDB files > 4GB.
Also includes a couple small fixes to change to uint64_t to support the
larger file sizes.  I updated the max file size check in MSFBuilder.cpp
to take into account the page size.

Differential Revision: https://reviews.llvm.org/D115051
2021-12-06 18:22:08 -05:00
Jez Ng 1b44364714 [lld-macho] Unreferenced weak dylib symbols shouldn't fetch archive symbols
We were fetching archive symbols too eagerly, bloating binary size as well as
just screwing up binaries that expected to look up certain symbols only at
runtime.

Reviewed By: #lld-macho, oontvoo

Differential Revision: https://reviews.llvm.org/D115092
2021-12-05 15:11:44 -05:00
Kristina Bessonova 0ac75e82ff Reland [DwarfDebug] Move emission of global vars, types and imports to endModule()
This patch proposes to move emission of global variables, types,
imported entities, etc from DwarfDebug::beginModule() to DwarfDebug::endModule().
Effectively, this changes nothing but the order of debug entities which
will be as follows:
* subprograms (including related context, local variables/labels,
  local imported entities; related types can be created as a part of
  the emission of local entities of an abstract subprogram);
* global variables (including related context and types);
* retained types and enums;
* non-local-scoped imported entities;
* basic types;
* other types left (as a part of local variables attributes emission).

Note that the order of emitted compile units may also be changed as now we emit
units that contain subprograms first and then all other non-empty units.

The motivation behind this change is the following:
(1) DwarfDebug::beginModule() is run at the very beginning of backend's pipeline,
    from this time IR can be significantly changed by target-specific passes.
    If it happens for debug metadata of global entities, those changes will not
    be reflected in the emitted DWARF.
(2) imported subprogram names should refer to an abstract subprogram if it exists,
    but it isn't known in DwarfDebug::beginModule() (it's possible to make some
    guesses based on location info, but it's not quite reliable);
(3) aforementioned entities if they are scoped within a bracketed block
    (subject of D113741) couldn't be emitted in DwarfDebug::beginModule()
    (they need parent emitted first). Another problem is if to try to gather
    some information about local entities and defer their emission
    (till subprogram's processing or DwarfDebug::endModule()) all the gathered
    details might be irrelevant / invalid by the time the entities are being
    emitted (because of (1)).

Reviewed By: dblaikie

Differential Revision: https://reviews.llvm.org/D114705
2021-12-05 13:56:45 +02:00
Leonard Grey 134275d994 [Support] Use final filename for Caching buffer identifier
Mach-O LLD uses the buffer identifier of the memory buffer backing an object
file to generate stabs which are used by `dsymutil` to find the object file for
dSYM generation.

When using thinLTO, these buffers are provided by the cache which initially
saves them to disk as temporary files beginning with "Thin-" but renames them
to persistent files beginning with "llvmcache-" before the buffer is provided
to the cache user.

However, the buffer is created before the file is renamed and is given the temp
file's name as an identifier. This causes the generated stabs to point to
nonexistent files.

This change names the buffer with the eventual persistent filename. I think
this is safe because failing to rename the temp file is a fatal error.

Differential Revision: https://reviews.llvm.org/D115055
2021-12-04 22:25:49 -05:00
Kristina Bessonova a961604819 Revert "[DwarfDebug] Support emitting function-local declaration for a lexical block"
This reverts commits
* ee691970a9 (D113741),
* 79d3132998 (D114705)

due to lldb and dexter test failures.
2021-12-04 18:06:57 +02:00
Kristina Bessonova 79d3132998 [DwarfDebug] Move emission of global vars, types and imports to endModule()
This patch proposes to move emission of global variables, types,
imported entities, etc from DwarfDebug::beginModule() to DwarfDebug::endModule().
Effectively, this changes nothing but the order of debug entities which
will be as follows:
* subprograms (including related context, local variables/labels,
  local imported entities; related types can be created as a part of
  the emission of local entities of an abstract subprogram);
* global variables (including related context and types);
* retained types and enums;
* non-local-scoped imported entities;
* basic types;
* other types left (as a part of local variables attributes emission).

Note that the order of emitted compile units may also be changed as now we emit
units that contain subprograms first and then all other non-empty units.

The motivation behind this change is the following:
(1) DwarfDebug::beginModule() is run at the very beginning of backend's pipeline,
    from this time IR can be significantly changed by target-specific passes.
    If it happens for debug metadata of global entities, those changes will not
    be reflected in the emitted DWARF.
(2) imported subprogram names should refer to an abstract subprogram if it exists,
    but it isn't known in DwarfDebug::beginModule() (it's possible to make some
    guesses based on location info, but it's not quite reliable);
(3) aforementioned entities if they are scoped within a bracketed block
    (subject of D113741) couldn't be emitted in DwarfDebug::beginModule()
    (they need parent emitted first). Another problem is if to try to gather
    some information about local entities and defer their emission
    (till subprogram's processing or DwarfDebug::endModule()) all the gathered
    details might be irrelevant / invalid by the time the entities are being
    emitted (because of (1)).

Reviewed By: dblaikie

Differential Revision: https://reviews.llvm.org/D114705
2021-12-04 14:10:01 +02:00
Fangrui Song 9bd6f6f6d5 [ELF][test] Fix typo in aarch64-cortex-a53-843419-recognize.s 2021-12-03 14:38:56 -08:00
George Koehler 885fb9a257 [ELF][PPC32] Make R_PPC32_PLTREL retain .got
PLT usage needs the first 12 bytes of the .got section. We need to keep .got and
DT_GOT_PPC even if .got/_GLOBAL_OFFSET_TABLE_ are not referenced (large PIC code
may only reference .got2), which is the case in OpenBSD's ld.so, leading
to a misleading error, "unsupported insecure BSS PLT object".

Fix this by adding R_PPC32_PLTREL to the list of hasGotOffRel.

Reviewed By: MaskRay

Differential Revision: https://reviews.llvm.org/D114982
2021-12-02 15:28:37 -08:00
Fangrui Song 353fe72ca3 [ELF] Hint -z nostart-stop-gc for __start_ undefined references
Make users aware what to do with ld.lld 13.0.0 / GNU ld<2015-10 --gc-sections
behavior.

Differential Revision: https://reviews.llvm.org/D114830
2021-12-02 11:58:25 -08:00
Keith Smiley 9e3552523e [lld-macho] Remove old macho darwin lld
During the llvm round table it was generally agreed that the newer macho
lld implementation is feature complete enough to replace the old
implementation entirely. This will reduce confusion for new users who
aren't aware of the history.

Differential Revision: https://reviews.llvm.org/D114842
2021-12-02 11:04:49 -08:00
Reid Kleckner 8270ff86a1 [ELF] Fix driver.test after 8c3641d0 when cwd is readonly 2021-12-02 10:25:04 -08:00
Sam Clegg 6f5c5cbe5f [lld][WebAssembly] Fix for debug relocations against undefined function symbols
This is very similar to https://reviews.llvm.org/D103557 but applies to
symbols which are undefined at link time rather than compile time.

We already have code that handles symbols which were defined at link
time but dead stripped by `--gc-sections` (See
`test/wasm/debug-removed-fn.ll`). In that case the symbols are not live
(!isLive()).  However, we can also have live symbols (which are
references by the program) but which are undefined at link time and are
imported by the linker.

In the test case here the symbol `undef` is used but is not defined
in the program but is imported by the linker due to the
`--import-undefined` flag.

Fixes: https://github.com/emscripten-core/emscripten/issues/15528

Differential Revision: https://reviews.llvm.org/D114921
2021-12-02 08:36:28 -08:00
Fangrui Song c5bfffed48 [ELF] Discard input .note.gnu.build-id even with default --build-id=none
binutils 2.38 will adopt this behavior
https://sourceware.org/bugzilla/show_bug.cgi?id=28639

Reviewed By: ikudrin

Differential Revision: https://reviews.llvm.org/D114910
2021-12-02 09:50:59 +00:00
Igor Kudrin b0ac68ccb7 [ELF] Prevent internalizing used comdat symbol
When a comdat symbol is defined in both bitcode and regular object
files, which are contained in the same archive, the linker could lose
the flag that the symbol is used in the regular object file and allow
LTO to internalize it, which led to "error: undefined symbol".

The issue was introduced in D79300.

Differential Revision: https://reviews.llvm.org/D114801
2021-12-02 12:10:06 +07:00
Fangrui Song ad45df91ad [ELF][PPC64] Remove unneeded PPC64PCRelLongBranchThunk
This reverts the PPC64PCRelLongBranchThunk part from D86706.
PPC64PCRelLongBranchThunk is the same as PPC64R12SetupStub.

Use `__gep_setup_` instead of `__long_branch_pcrel_` for the stub symbol name
as it more closely indicates the operation.
(Note: GNU ld uses `*.long_branch.*` and `*.plt_branch.*`).

Reviewed By: NeHuang, nemanjai

Differential Revision: https://reviews.llvm.org/D114656
2021-11-30 11:33:17 -08:00
Fangrui Song 8c3641d03e [ELF] Change -z unknown from error to warning
There is a trend of having more optional options (usually security
hardening related) like -z cet-report=, -z bti-report=, -z force-bti.
If ld.lld 14.0.0 uses a warning, in 15/16/17/... timeframe when people
add new options to software, they can worry less about linker errors on ld.lld 14.0.0.

In some cases `-z foo` does essential work where a silent ignore can be
problematic, but the user has received a warning. From my observation, the
doing-essential-work `-z foo` is much fewer than the converse. In addition,
the user who cares can use `--fatal-warnings` (Note: GNU ld doesn't upgrade warnings to errors).
It is unclear whether we need something like `clang -Wunknown-warning-option`.

If we ever run into unfortunate transition like `-z start-stop-gc`, the
affected software (e.g. ldc is a compiler which passes linker options to the underlying ld)
can blindly add the `-z` option, without worrying it may cause a linker error to LLD 14.0.0.

Reviewed By: jrtc27, peter.smith

Differential Revision: https://reviews.llvm.org/D114748
2021-11-30 11:06:28 -08:00
Vy Nguyen 74cbd71072 [lld-macho] Mark dylib symbols coming from -weak_framework as weak-ref.
PR:52564

Differential Revision: https://reviews.llvm.org/D114397
2021-11-30 09:54:59 -05:00