Commit Graph

16341 Commits

Author SHA1 Message Date
Alexandre Ganea
b371ea35c1 [LLD][COFF] Fix /dependentloadflag tests with AArch64
This fixes: https://lab.llvm.org/buildbot/#/builders/65/builds/11819

Also see discussion in: https://github.com/llvm/llvm-project/pull/71537
2023-11-09 10:24:42 -05:00
Daniel Thornburgh
71de61259a
[lld][MachO] Prevent doubled N_SO when comp_dir and name absolute (#71608)
When forming MachO STABS, this change detects if the DW_AT_name of the
compile unit is already absolute (as allowed by DWARF), and if so, does
not prepend DW_AT_comp_dir.

Fixes #70995
2023-11-08 15:55:22 -08:00
spupyrev
ef6d187115
[ELF] Fix assertion in cdsort (#71708)
It seems that some functions (.text.unlikely.xxx) may have zero size,
which
makes some builds with enabled assertions fail. Removing the assertion
and
extending one test to fix the build.
The sorting can process such zero-sized functions so no changes there
are needed
2023-11-08 12:34:36 -08:00
Aleksei Nurmukhametov
76947e0405
[LLD][COFF] Support /DEPENDENTLOADFLAG[:flags] (#71537)
This should fix https://github.com/llvm/llvm-project/issues/43935
2023-11-08 15:21:05 -05:00
pvanhout
69cd4faad7 Reapply: [lld] Restore "REQUIRES: amdgpu" in amdgpu-abi-version
Accidentally deleted it in a previous commit.
2023-11-07 12:35:17 +01:00
Pierre van Houtryve
4428b01faa Reland: [AMDGPU] Remove Code Object V3 (#67118)
V3 has been deprecated for a while as well, so it can safely be removed
like V2 was removed.

- [Clang] Set minimum code object version to 4
- [lld] Fix tests using code object v3
- Remove code object V3 from the AMDGPU backend, and delete or port v3
tests to v4.
- Update docs to make it clear V3 can no longer be emitted.
2023-11-07 12:23:03 +01:00
Martin Storsjö
e8961969ec
[LLD] [COFF] Fix deducing the machine type from LTO objects for ARM/Thumb (#71335)
In practice, all the Windows ARMNT IR objects show the architecture type
Thumb, not ARM.

Most other switch cases for architecture in lld/COFF check for and treat
`arm` and `thumb` equally.
2023-11-07 12:00:31 +02:00
Martin Storsjö
7f9a0048fa
[LLD] [COFF] Error out if new LTO objects are pulled in after the main LTO compilation (#71337)
Normally, this shouldn't happen. It can happen in exceptional
circumstances, if the compiled output of a bitcode object file
references symbols that weren't listed as undefined in the bitcode
object file itself.

This can at least happen in the following cases:
- A custom SEH personality is set via asm()
- Compiler generated calls to builtin helper functions, such as
__chkstk, or __rt_sdiv on arm

Both of these produce undefined references to symbols after compiling to
a regular object file, that aren't visible on the level of the IR object
file.

This is only an issue if the referenced symbols are provided as LTO
objects themselves; loading regular object files after the LTO
compilation works fine.

Custom SEH personalities are rare, but one CRT startup file in mingw-w64
does this. The referenced pesonality function is usually provided via an
import library, but for WinStore targets, a local dummy reimplementation
in C is used, which can be an LTO object.

Generated calls to builtins is very common, but the builtins aren't
usually provided as LTO objects (compiler-rt's builtins explicitly pass
-fno-lto when building), and many of the builtins are provided as raw .S
assembly files, which don't get built as LTO objects anyway, even if
built with -flto.

If hitting this unusual, but possible, situation, error out cleanly with
a clear message rather than crashing.
2023-11-07 11:49:40 +02:00
Martin Storsjö
303370e871
[LLD] [COFF] Mark the symbol _tls_used as a GC root (#71336)
This is necessary if the object file containing _tls_used is built with
-fdata-sections; if this symbol is present, lld is going to include
references to it in the PE header, in the TLS_TABLE data directory.

Therefore, treat the symbol as a GC root, i.e. retain it (and whatever
other symbols the section chunk references) when doing doing GC
(/opt:ref), if the symbol is present.
2023-11-07 11:49:05 +02:00
Fangrui Song
339f5f727a [ELF] Set file for synthesized _binary_ symbols
Ensure the property that non-null `section` implies non-null `file`.
2023-11-06 12:05:13 -08:00
Martin Storsjö
1d95a071d6
[LLD] [COFF] Handle undefined weak symbols in LTO (#70430)
When reading the bitcode input, undefined weak symbols will show up as
undefined symbols - which fails the early pass of checking for missing
symbols in symtab.reportUnresolvable(), before doing the actual LTO
compilation.

Mark such symbols as deferUndefined (added in
3785a413fe /
https://reviews.llvm.org/D89004 for the -wrap option), to let them pass
through this LTO precheck. After the LTO compilation, the weak undefined
symbols will point towards an absolute null symbol as default.

Such weak undefined symbols are used for the TLS init function in the
Itanium C++ ABI, for TLS variables that potentially need to run a
constructor, when accessed across translation units.

This fixes https://github.com/llvm/llvm-project/issues/64513.
2023-11-06 00:00:24 +02:00
Fangrui Song
58ad3e86d7 [ELF] Improve .o preempting DSO tests 2023-11-04 15:49:54 -07:00
Martin Storsjö
3ab6209a3f
[LLD] [COFF] Handle manually defined __imp_ pointers in LTO (#70777)
Such pointers are often used by the core parts of mingw-w64, to locally
define a function that might have been referred to with dllimport.

(MSVC style linkers can automatically provide such pointers, if there
are undefined references to `__imp_<func>` left but a definition of
`<func>` is available - although this prints the warning LNK4217. GNU ld
doesn't do this, so in mingw-w64, such things are generally handled by
manually providing the relevant `__imp_` pointers.)

Make sure that a full LTO build, that does LTO of both the `__imp_`
pointer and the object file referencing it, successfully resolves such
symbols.

This solution admittedly probably reduces the effect of the LTO
compilation if there would happen to be `__imp_` prefixed symbols
included, in LTO objects, that aren't actually used. Such symbols are
mostly used in the base toolchain, not often in user code, and usually
only the relevant object files are linked in anyway.

This fixes https://github.com/llvm/llvm-project/issues/57982.
2023-11-04 23:49:38 +02:00
Fangrui Song
e6bd68c90b [ELF][test] Improve symver-non-default.s to test foo and foo@v1 combining for executable linking 2023-11-04 10:19:14 -07:00
Fangrui Song
5548bcdace [ELF] Refactor --version-script tests 2023-11-03 20:40:46 -07:00
Fangrui Song
946b33d375 [ELF][test] Improve internalize-exportdyn.ll 2023-11-03 17:08:32 -07:00
spupyrev
b53c04a8da Reapply [ELF] Making cdsort default for function reordering (#68638)
Edited lld/ELF/Options.td to cdsort as well

CDSort function reordering outperforms the existing default heuristic (
hfsort/C^3) in terms of the performance of generated binaries while
being (almost) as fast. Thus, the suggestion is to change the default.
The speedup is up to 1.5% perf for large front-end binaries, and can be
moderate/neutral for "small" benchmarks.

High-level **perf impact** on two selected binaries:
clang-10 binary (built with LTO+AutoFDO/CSSPGO): wins on top of C^3 in
[0.3%..0.8%]
rocksDB-8 binary (built with LTO+CSSPGO): wins on top of C^3 in
[0.8%..1.5%]

More detailed measurements on the clang binary is at
[here](https://reviews.llvm.org/D152834#4445042)
2023-11-03 16:03:06 -07:00
Fangrui Song
b169e7fedd
[ELF] Improve undefined symbol message w/ DW_TAG_variable of the enclosing symbol but w/o line number information (#70854)
The undefined symbol message suggests the source line when line number
information is available (see https://reviews.llvm.org/D31481).
When the undefined symbol is from a global variable, we won't get the
line information.
```
extern int undef;
namespace ns {
int *var[] = {
  &undef
};
// DW_TAG_variable(DW_AT_decl_file/DW_AT_decl_line) is available while
// line number information is unavailable.
}

ld.lld: error: undefined symbol: undef
>>> referenced by undef-debug2.cc
>>>               undef-debug2.o:(ns::var)
```

This patch utilizes `getEnclosingSymbol` to locate `var` and find
DW_TAG_variable for `var`:
```
ld.lld: error: undefined symbol: undef
>>> referenced by undef-debug2.cc:3 (/tmp/c/undef-debug2.cc:3)
>>>               undef-debug2.o:(ns::var)
```
2023-11-03 13:53:36 -07:00
Fangrui Song
56aa727907 [ELF] Add getEnclosingSymbol for code sharing. NFC 2023-11-03 13:43:28 -07:00
Fangrui Song
49168b2512
[ELF] Enhance --no-allow-shlib-undefined to report non-exported definition (#70769)
For a DSO with all DT_NEEDED entries accounted for, if it contains an
undefined non-weak symbol that shares a name with a non-exported
definition (hidden visibility or localized by a version script), and
there is no DSO definition, we should also report an error. Because the
definition is not exported, it cannot resolve the DSO reference at
runtime.

GNU ld introduced this error-checking in [April
2003](https://sourceware.org/pipermail/binutils/2003-April/026568.html).
The feature is available for executable links but not for -shared, and
it is
orthogonal to --no-allow-shlib-undefined. We make the feature part of
--no-allow-shlib-undefined and work with -shared when
--no-allow-shlib-undefined is specified.

A subset of this error-checking is covered by commit
1981b1b6b9 for --gc-sections discarded
sections. This patch covers non-discarded sections as well.

Internally, I have identified 2 bugs (which would fail with
LD_BIND_NOW=1) covered by commit
1981b1b6b9
2023-11-03 11:05:09 -07:00
Fangrui Song
af599389f5 [ELF] Improve --no-allow-shlib-undefined tests 2023-11-03 10:56:29 -07:00
Sam Clegg
89d5635f0a [lld][WebAssembly] Add --keep-section flag
This flag causes wasm-ld preserve a section even in the face of
`--strip-all`.  This is useful, for example, to preserve the
target_features section in the ase of clang (which can run wasm-opt
after linking), and emcc (which performs a bunch of post-link work).

Fixes: https://github.com/llvm/llvm-project/issues/60613
Fixes: https://github.com/llvm/llvm-project/issues/55781

Differential Revision: https://reviews.llvm.org/D149917
2023-11-02 14:23:45 -07:00
Yaxun (Sam) Liu
3594769f20
[ELF] Define NOMINMAX to fix zlib.h caused build failure on Windows (#70368)
On Windows when zlib is enabled, zlib header introduced some Windows
headers which defines max as a macro. Since OutputSections.cpp uses
std::max with template argument, this causes compilation error.

Define macro NOMINMAX to avoid this.
2023-11-02 08:59:54 -04:00
Jacek Caban
d61ab5e858
[lld] Align EC code region boundaries. (#69100)
Boundaries between code chunks of different architecture are always
aligned. 0x1000 seems to be a constant, this does not seem to be
affected by any command line alignment argument.
2023-11-02 12:16:02 +01:00
Fangrui Song
a40f651a06
[ELF] adjustOutputSections: don't copy SHF_EXECINSTR when an output does not contain input sections (#70911)
For an output section with no input section, GNU ld eliminates the
output section when there are only symbol assignments (e.g.
`.foo : { symbol = 42; }`) but not for `.foo : { . += 42; }`
(`SHF_ALLOC|SHF_WRITE`).

We choose to retain such an output section with a symbol assignment
(unless unreferenced `PROVIDE`). We copy the previous section flag (see
https://reviews.llvm.org/D37736) to hopefully make the current PT_LOAD
segment extend to the current output section:

* decrease the number of PT_LOAD segments
* If a new PT_LOAD segment is introduced without a page-size
  alignment as a separator, there may be a run-time crash.

However, this `flags` copying behavior is not suitable for
`.foo : { . += 42; }` when `flags` contains `SHF_EXECINSTR`. The
executable bit is surprising

(https://discourse.llvm.org/t/lld-output-section-flag-assignment-behavior/74359).

I think we should drop SHF_EXECINSTR when copying `flags`. The risk is a
code section followed by `.foo : { symbol = 42; }` will be broken, which
I believe is unrelated as such uses are almost always related to data
sections.

For data-command-only output sections (e.g. `.foo : { QUAD(42) }`), we
keep allowing copyable SHF_WRITE.

Some tests are updated to drop the SHF_EXECINSTR flag. GNU ld doesn't
set SHF_EXECINSTR as well, though it sets SHF_WRITE for some tests while
we don't.
2023-11-01 22:35:28 -07:00
Jacek Caban
4c1c32c377
[lld][NCF] Add missing llvm-mc invocation to arm64ec-codemap.test. (#70956)
Fixup for #70722.
2023-11-01 17:45:17 +01:00
Fangrui Song
9220e0e647 [ELF][test] Improve flag propagation when an output section does not contain input sections 2023-11-01 09:29:33 -07:00
Jacek Caban
c605431a40
[lld] Sort data chunks before code chunks on ARM64EC. (#70722) 2023-11-01 16:47:43 +01:00
Jacek Caban
14043d42e4
[lld] Implement getOutputCharacteristics for non-section code thunks. (#70721)
This will be useful for ARM64EC, but it also fixes MinGW export handling
when synthetic function symbols are exported.
2023-11-01 13:27:29 +01:00
Martin Storsjö
e58c4c7713
[LLD] [COFF] Recognize Itanium vtables for ICF (#70196)
The testcases are plain copies of the existing ICF vtable testcase, with
symbol names renamed to match the Itanium vtable name pattern.
2023-10-26 10:58:28 +03:00
Tobias Hieta
a6d509fadb
[Support] Better error msg when cache dir can't be created. (#69575)
On windows if you passed /lldltocache:D:\tmp to lld and you didn't have
D: mounted it fail to create the cache dir D:\tmp, but the error message
is pretty hard to understand:

```
c:\code\llvm\llvm-project\out\debug>bin\lld-link.exe /lldltocache:D:\tmp
hello.obj
LLVM ERROR: no such file or directory

PLEASE submit a bug report to
https://github.com/llvm/llvm-project/issues/ and include the crash
backtrace.
Exception Code: 0xC000001D
```

Which lead one of our users to report this as a crash. I have just added
a bit better message so it now says:

```
c:\code\llvm\llvm-project\out\debug>bin\lld-link.exe /lldltocache:D:\tmp
hello.obj
LLVM ERROR: Can't create cache directory: D:\tmp

PLEASE submit a bug report to
https://github.com/llvm/llvm-project/issues/ and include the crash
backtrace.
```

I am not sure this is a fatal error because it's not something that
really should be reported as a bug to LLVM. But at least this gives a
bit more visibility on what to change.
2023-10-26 08:31:33 +02:00
Martin Storsjö
6d66440c50
[LLD] [MinGW] Hook up --icf=safe to -opt:safeicf (#70037)
Back when the --icf= option was hooked up in the MinGW frontend in LLD,
in 2017, lld-link didn't support safe ICF, and mapping it to noicf was
suggested in review: https://reviews.llvm.org/D40019

In 2018, in ab038025a5,
lld-link did get support for handling address significance tables,
allowing the ICF to operate safely on more sections.

Later in 2021, lld-link did get support for a separate safe ICF mode in
5bdc5e7efd /
https://reviews.llvm.org/D97436.

Hook this up for the MinGW frontend as well.
2023-10-25 14:38:30 +03:00
Sam Clegg
e01c7d54b4
[LowerGlobalDtors] Skip __cxa_atexit call completely when arg0 is unused (#68758)
In emscripten we have a build mode (the default actually) where the
runtime never exits and therefore `__cxa_atexit` is a dummy/stub
function that does nothing. In this case we would like to be able
completely DCE any otherwise-unused global dtor functions.

Fixes: https://github.com/emscripten-core/emscripten/issues/19993
2023-10-23 10:08:08 -07:00
Martin Storsjö
241c290ad7 Reland [LLD] [COFF] Don't try to detect MSVC installations in mingw mode
In mingw mode, all linker paths are passed explicitly to the linker
by the compiler driver. Don't try to implicitly add linker paths
from the LIB environment variable or by detecting an MSVC
installation directory.

If the /winsysroot command line parameter is explicitly passed to
lld-link while /lldmingw is specified, it could be considered reasonable
to actually include those paths. However, modifying the code to
handle only the /winsysroot case but not the other ones, when the
mingw mode has been enabled, seems like much more code complexity
for a mostly hypothetical case.

Add a test for this when case when using LIB. (The code paths for
trying to detect an MSVC installation aren't really regression tested.)

Also fix an issue in the existing test for "Check that when /winsysroot
is specified, %LIB% is ignored.", where the LIB variable pointed
to a nonexistent directory, so the test would pass even if /winsysroot
wouldn't be specified.

Reland this after https://github.com/llvm/llvm-project/pull/68077 and
https://github.com/llvm/llvm-project/pull/69781 - the compiler-rt test
that used -lldmingw in MSVC environments has been updated to use a more
specific option.

Differential Revision: https://reviews.llvm.org/D144084
2023-10-21 22:36:11 +03:00
Martin Storsjö
a67ae8c0fd
[LLD] [COFF] Add a separate option for allowing duplicate weak symbols (#68077)
The MinGW mode (enabled with the flag -lldmingw) does allow duplicate
weak symbols. A test in
compiler-rt/test/profile/Windows/coverage-weak-lld.cpp does currently
enable the -lldmingw flag in an MSVC context, in order to deal with
duplicate weak symbols.

Add a new, separate, lld specific flag for enabling this. In MinGW mode,
this is enabled by default, otherwise it is disabled.

This allows making the MinGW mode more restrictive in adding libpaths
from the surrounding environment; in MinGW mode, all libpaths are passed
explicitly by the compiler driver to the linker, which is attempted in
https://reviews.llvm.org/D144084.
2023-10-20 23:44:44 +03:00
Jacek Caban
e24ac11fa7
[lld][NFC] Remove unnecessary else statements. (#69451) 2023-10-20 12:38:12 +02:00
Fangrui Song
3343d000a3 [ELF][test] Demonstrate --no-allow-shlib-undefined behavior with a hidden relocatable object file definition
1981b1b6b9 improved the check.
2023-10-19 10:58:52 -07:00
Fangrui Song
ab301d833a [ELF][test] --emit-relocs: test ALLOC sections discarded by --gc-sections and referenced by non-ALLOC
bbf7b9d805 accidentally caused a regression that
is fixed by #69425. Add test to prevent regression.
2023-10-18 18:14:11 -07:00
Fangrui Song
ec0e556e67
[ELF] Merge copyLocalSymbols and demoteLocalSymbolsInDiscardedSections (#69425)
Follow-up to #69295: In `Writer<ELFT>::run`, the symbol passes are
flexible:
they can be placed almost everywhere before `scanRelocations`, with a
constraint
that the `computeIsPreemptible` pass must be invoked for linker-defined
non-local symbols.

Merge copyLocalSymbols and demoteLocalSymbolsInDiscardedSections to
simplify
code:

* Demoting local symbols can be made unconditional, not constrainted to
/DISCARD/ uses due to performance concerns
* `includeInSymtab` can be made faster
* Make symbol passes close to each other
* Decrease data cache misses due to saving an iteration over local
symbols

There is no speedup, likely due to the unconditional `dr->section`
access in `demoteAndCopyLocalSymbols`.

`gc-sections-tls.s` no longer reports an error because the TLS symbol is
converted to an Undefined.
2023-10-18 08:56:17 -07:00
Jacek Caban
cbbb545c46
[lld] Sort code section chunks by range types on Arm64EC targets. (#69099) 2023-10-18 13:57:42 +02:00
pvanhout
868abf0961 Revert "[AMDGPU] Remove Code Object V3 (#67118)"
This reverts commit 544d91280c.
2023-10-18 12:55:36 +02:00
Fangrui Song
bbf7b9d805 [ELF] Remove unused setSymbolAndType after #69295. NFC
One use of setSymbolAndType (related to https://reviews.llvm.org/D53864
"Do not crash when -r output uses linker script with `/DISCARD/`")
is no longer needed after commit 1981b1b6b9
demotes symbols in discarded sections to Undefined.
2023-10-17 15:08:13 -07:00
Fangrui Song
1981b1b6b9 [ELF] Demote symbols in /DISCARD/ discarded sections to Undefined (#69295)
When an input section is matched by /DISCARD/ in a linker script, GNU ld
reports errors for relocations referencing symbols defined in the section:

    `.aaa' referenced in section `.bbb' of a.o: defined in discarded section `.aaa' of a.o

Implement the error by demoting eligible symbols to `Undefined` and changing
STB_WEAK to STB_GLOBAL. As a side benefit, in relocatable links, relocations
referencing symbols defined relative to /DISCARD/ discarded sections no longer
set symbol/type to zeros.

It's arguable whether a weak reference to a discarded symbol should lead to
errors. GNU ld reports an error and our demoting approach reports an error as
well.

Close #58891

Co-authored-by: Bevin Hansson <bevin.hansson@ericsson.com>
2023-10-17 14:10:52 -07:00
Fangrui Song
fc5d815d54 [ELF] Merge demoteSymbols and isPreemptible computation. NFC
Remove one iteration of symtab and slightly improve the performance.
2023-10-17 13:52:08 -07:00
Fangrui Song
e9b9a1d320 [ELF] Move demoteSymbols to Writer.cpp. NFC
History of demoteSharedSymbols:

* https://reviews.llvm.org/D45536 demotes SharedSymbol
* https://reviews.llvm.org/D111365 demotes lazy symbols
* The pending #69295 will demote symbols defined in discarded sections

The pass is placed after markLive just to be clear that it needs `isNeeded`
information computed by markLive. The remaining passes in Driver.cpp do not use
symbol information. Move the pass to Writer.cpp to be closer to other
symbol-related passes.
2023-10-17 13:16:50 -07:00
Fangrui Song
0996ceece6 [ELF][test] Improve relocatable link & /DISCARD/ test
Check that #69295 will fix symbols referenced by relocations that are
defined in discarded sections.
2023-10-17 12:49:17 -07:00
Jacek Caban
f6f944e77f
[lld][NFC] Factor out isCodeSection helper. (#69193) 2023-10-16 21:00:13 +02:00
Jacek Caban
47401b6173
[lld] Add support for relocations in x86_64 objects on Arm64EC targets. (#69098)
Since EC targets may combine various object types, we need to pick
relocation format based on chunk type instead of global config.
2023-10-16 20:50:15 +02:00
pvanhout
7571f27ed7 [lld] Restore "REQUIRES: amdgpu" in amdgpu-abi-version
Accidentally deleted it in a previous commit.
2023-10-16 08:41:26 +02:00
Pierre van Houtryve
544d91280c
[AMDGPU] Remove Code Object V3 (#67118)
V3 has been deprecated for a while as well, so it can safely be removed
like V2 was removed.

- [Clang] Set minimum code object version to 4
- [lld] Fix tests using code object v3
- Remove code object V3 from the AMDGPU backend, and delete or port v3
tests to v4.
- Update docs to make it clear V3 can no longer be emitted.
2023-10-16 08:21:48 +02:00