Commit Graph

16490 Commits

Author SHA1 Message Date
Martin Storsjö
28be6f670f [LLD] [MinGW] Implement the --lto-emit-asm and -plugin-opt=emit-llvm options (#81475)
These were implemented in the COFF linker in
3923e61b96 and
d12b99a431.

This matches the corresponding options in the ELF linker.

(cherry picked from commit d033366bd2189e33343ca93d276b40341dc39770)
2024-02-16 05:07:05 -08:00
Martin Storsjö
8391421532
[LLD] [docs] Add a release note for the SOURCE_DATE_EPOCH support (#81388) 2024-02-15 21:35:12 -08:00
Martin Storsjö
58b2a6d3bc [LLD] [test] Avoid printing timestamps past INT32_MAX with llvm-readobj (#81463)
If llvm-readobj is built with a 32 bit time_t, it can't print such
timestamps correctly.

(cherry picked from commit 0bf4ff29816c0eead99ba576a2df2e3c4d214b1f)
2024-02-13 11:44:00 -08:00
Martin Storsjö
0d656f0f12 [LLD] [COFF] Pick timestamps from the SOURCE_DATE_EPOCH variable (#81326)
The SOURCE_DATE_EPOCH environment variable can be set in order to get
reproducible build.

When linking PE/COFF modules with LLD, the timestamp field is set to the
current time, unless either the /timestamp: or /Brepro option is set. If
neither of them is set, check the SOURCE_DATE_EPOCH variable, before
resorting to using the actual current date and time.

See https://reproducible-builds.org/docs/source-date-epoch/ for reference
on the use of this variable.

(cherry picked from commit 0df8aed6c30f08ded526038a6bbb4daf113a31c1)
2024-02-10 17:48:47 -08:00
Martin Storsjö
d0149a593b
[docs] Add release notes for Windows specific changes in 18.x (#80011) 2024-02-06 16:20:10 -08:00
Fangrui Song
140ad18e1e
ReleaseNotes: add lld/ELF notes (#80393) 2024-02-03 10:48:15 -08:00
Fangrui Song
278f80ce95 [ELF] Fix compareSections assertion failure when OutputDescs in sectionCommands are non-contiguous
In a `--defsym y0=0 -T a.lds` link where a.lds contains only INSERT
commands, the `script->sectionCommands` layout may be:
```
orphan sections
SymbolAssignment due to --defsym
sections created by INSERT commands
```

The `OutputDesc` objects are not contiguous in sortInputSections, and
`compareSections` will be called with a SymbolAssignment argument,
leading to an assertion failure.

(cherry picked from commit dee8786f70a3d62b639113343fa36ef55bdbad63)
2024-02-03 10:37:52 -08:00
Fangrui Song
2fe0bca910 [ELF] --warn-backrefs: --defsym does not make a backward reference
The interaction between --warn-backrefs was not tested, but if
--defsym-created reference causes archive member extraction, it seems
reasonable to suppress the diagnostic, which was the behavior before #78944.

(cherry picked from commit 9a1ca245c8bc60b1ca12cd906fb31130801d977e)
2024-02-01 17:40:45 -08:00
Fangrui Song
e2521eaa1a [ELF] Implement R_RISCV_TLSDESC for RISC-V
Support
R_RISCV_TLSDESC_HI20/R_RISCV_TLSDESC_LOAD_LO12/R_RISCV_TLSDESC_ADD_LO12/R_RISCV_TLSDESC_CALL.
LOAD_LO12/ADD_LO12/CALL relocations reference a label at the HI20
location, which requires special handling. We save the value of HI20 to
be reused. Two interleaved TLSDESC code sequences, which compilers do
not generate, are unsupported.

For -no-pie/-pie links, TLSDESC to initial-exec or local-exec
optimizations are eligible. Implement the relevant hooks
(R_RELAX_TLS_GD_TO_LE, R_RELAX_TLS_GD_TO_IE): the first two instructions
are converted to NOP while the latter two are converted to a GOT load or
a lui+addi.

The first two instructions, which would be converted to NOP, are removed
instead in the presence of relaxation. Relaxation is eligible as long as
the R_RISCV_TLSDESC_HI20 relocation has a pairing R_RISCV_RELAX,
regardless of whether the following instructions have a R_RISCV_RELAX.
In addition, for the TLSDESC to LE optimization (`lui a0,<hi20>; addi a0,a0,<lo12>`),
`lui` can be removed (i.e. use the short form) if hi20 is 0.

```
// TLSDESC to LE/IE optimization
.Ltlsdesc_hi2:
  auipc a4, %tlsdesc_hi(c)                      # if relax: remove; otherwise, NOP
  load  a5, %tlsdesc_load_lo(.Ltlsdesc_hi2)(a4) # if relax: remove; otherwise, NOP
  addi  a0, a4, %tlsdesc_add_lo(.Ltlsdesc_hi2)  # if LE && !hi20 {if relax: remove; otherwise, NOP}
  jalr  t0, 0(a5), %tlsdesc_call(.Ltlsdesc_hi2)
  add   a0, a0, tp
```

The implementation carefully ensures that an instruction unrelated to
the current TLSDESC code sequence, if immediately follows a removable
instruction (HI20 or LOAD_LO12 OR (LE-specific) ADD_LO12), is not
converted to NOP.

* `riscv64-tlsdesc.s` is inspired by `i386-tlsdesc-gd.s` (https://reviews.llvm.org/D112582).
* `riscv64-tlsdesc-relax.s` tests linker relaxation.
* `riscv-tlsdesc-gd-mixed.s` is inspired by `x86-64-tlsdesc-gd-mixed.s` (https://reviews.llvm.org/D116900).

Link: https://github.com/riscv-non-isa/riscv-elf-psabi-doc/pull/373

Reviewed By: ilovepi

Pull Request: https://github.com/llvm/llvm-project/pull/79239

(cherry picked from commit 1117fdd7c16873eb389e988c6a39ad922bae0fd0)
2024-01-26 21:34:49 -08:00
Fangrui Song
3d02473ac5 [ELF] Fix terminology: TLS optimizations instead of TLS relaxation. NFC
(cherry picked from commit 849951f8759171cb6c74d3ccbcf154506fc1f0ae)
2024-01-26 21:34:49 -08:00
Fangrui Song
e9d99e5183 [ELF] Clean up R_RISCV_RELAX code. NFC
(cherry picked from commit ccb99f221422b8de5e1ae04d3427f15878f7cd93)
2024-01-26 21:34:48 -08:00
Fangrui Song
0991d3c7b5 [ELF] Don't resolve relocations referencing SHN_ABS to tombstone in non-SHF_ALLOC sections (#79238)
A SHN_ABS symbol has never been considered for
InputSection::relocateNonAlloc.
Before #74686, the code did made it work in the absence of `-z
dead-reloc-in-nonalloc=`.
There is now a report about such SHN_ABS uses

(https://github.com/llvm/llvm-project/pull/74686#issuecomment-1904101711)
and I think it makes sense for non-SHF_ALLOC to support SHN_ABS, like
SHF_ALLOC sections do.

```
// clang -g
__attribute__((weak)) int symbol;
int *foo() { return &symbol; }

0x00000023:   DW_TAG_variable [2]   (0x0000000c)
                ...
                DW_AT_location [DW_FORM_exprloc]        (DW_OP_addrx 0x0)

```

.debug_addr references `symbol`, which can be redefined by a symbol
assignment or --defsym to become a SHN_ABS symbol.

The problem is that `!sym.getOutputSection()` cannot discern SHN_ABS
from a symbol whose section has been discarded. Since commit
1981b1b6b9, a symbol relative to a
discarded section is changed to `Undefined`, so the `SHN_ABS` check
become trivial.

We currently apply tombstone for a relocation referencing
`SharedSymbol`. This patch does not change the behavior.

(cherry picked from commit 8abf8d124ae346016c56209de7f57b85671d4367)
2024-01-25 17:27:37 -08:00
Fangrui Song
c663c8b883 [ELF,test] Improve dead-reloc-in-nonalloc.s
Test an absolute relocation referencing a DSO symbol, relocating a
non-SHF_ALLOC section. Also test --gc-sections.
2024-01-23 17:23:52 -08:00
Fangrui Song
4fcd7cf22d [ELF,test] Actually fix defsym.ll 2024-01-23 12:52:36 -08:00
Fangrui Song
16343f0db2 [ELF,test] Fix defsym.ll 2024-01-23 12:49:57 -08:00
Fangrui Song
f7669ba3d9
[ELF] --save-temps --lto-emit-asm: derive ELF/asm file names from bitcode file names
Port COFF's https://reviews.llvm.org/D78221 and
https://reviews.llvm.org/D137217 to ELF. For the in-process ThinLTO
link, `ld.lld --save-temps a.o d/b.o -o out` will create
ELF relocatable files `out.lto.a.o`/`d/out.lto.b.o` instead of
`out1.lto.o`/`out2.lto.o`. Deriving the LTO-generated relocatable file
name from bitcode file names helps debugging.

The relocatable file name from the first regular LTO partition does not
change: `out.lto.o`. The second, if present due to `--lto-partition=`,
changes from `out1.lto.o` to `lto.1.o`.

For an archive member, e.g. `d/a.a(coll.o at 8)`,
the relocatable file is `d/out.lto.a.a(coll.o at 8).o`.

`--lto-emit-asm` file names are changed similarly. `--lto-emit-asm -o
out` now creates `out.lto.s` instead of `out`, therefore the
`--lto-emit-asm -o -` idiom no longer works. However, I think this new
behavior (which matches COFF) is better since keeping or removing
`--lto-emit-asm` will dump different files, instead of overwriting the
`-o` output file from an executable/shared object to an assembly file.

Reviewers: rnk, igorkudrin, xur-llvm, teresajohnson, ZequanWu

Reviewed By: teresajohnson

Pull Request: https://github.com/llvm/llvm-project/pull/78835
2024-01-23 11:38:15 -08:00
Fangrui Song
17bc449784 [ELF] Improve thin-archivecollision.ll 2024-01-23 11:26:22 -08:00
Martin Storsjö
50d33c62ad
[LLD] [COFF] Fix crashes for cfguard with undefined weak symbols (#79063)
When marking symbols as having their address taken, we can have the
sitaution where we have the address taken of a weak symbol. If there's
no strong definition of the symbol, the symbol ends up as an absolute
symbol with the value null. In those cases, we don't have any Chunk.
Skip such symbols from the cfguard tables.

This fixes https://github.com/llvm/llvm-project/issues/78619.
2024-01-23 20:37:03 +02:00
Kyungwoo Lee
77e204c7b0
[lld-macho][arm64] implement -objc_stubs_small (#78665)
This patch implements `-objc_stubs_small` targeting arm64, aiming to
align with ld64's behavior.
1. `-objc_stubs_fast`: As previously implemented, this always uses the
Global Offset Table (GOT) to invoke `objc_msgSend`. The alignment of the
objc stub is 32 bytes.
2. `-objc_stubs_small`: This behavior depends on whether `objc_msgSend`
is defined. If it is, it directly jumps to `objc_msgSend`. If not, it
creates another stub to indirectly jump to `objc_msgSend`, minimizing
the size. The alignment of the objc stub in this case is 4 bytes.
2024-01-23 07:31:34 -08:00
OldWorldOrdr
654131fab2
fix test (#79018)
Mistake with #78628 that got caught after being merged
2024-01-23 09:08:43 -05:00
Fangrui Song
39a1b280a1 [ELF] Improve ThinLTO tests 2024-01-22 22:51:44 -08:00
Fangrui Song
37efa7038d [ELF] Fix spurious warning for -z rel && -z rela
Fixes: 665f913e45
2024-01-22 18:18:21 -08:00
Fangrui Song
e390bda978 [ELF] Suppress --no-allow-shlib-undefined diagnostic when a SharedSymbol is overridden by a hidden visibility Defined which is later discarded
Commit 1981b1b6b9 unexpectedly strengthened
--no-allow-shlib-undefined to catch a kind of ODR violation.
More precisely, when all three conditions are met, the new
`--no-allow-shlib-undefined` code reports an error.

* There is a DSO undef that has been satisfied by a definition from
  another DSO.
* The `SharedSymbol` is overridden by a non-exported (usually of hidden
  visibility) definition in a relocatable object file (`Defined`).
* The section containing the `Defined` is garbage-collected (it is not
  part of `.dynsym` and is not marked as live).

Technically, the hidden Defined in the executable can be intentional: it
can be meant to remain non-exported and not interact with any dynamic
symbols of the same name that might exist in other DSOs. To allow for
such use cases, allocate a new bit in
Symbol and relax the --no-allow-shlib-undefined check to before
commit 1981b1b6b9.
2024-01-22 10:09:35 -08:00
Sam Clegg
19261390cc
[lld][WebAssembly] Implement --start-lib/--end-lib (#78821)
Fixes: #77960
2024-01-22 10:04:26 -08:00
Fangrui Song
43b13341fb
[ELF] Add internal InputFile (#78944)
Based on https://reviews.llvm.org/D45375 . Introduce a new InputFile
kind `InternalKind`, use it for

* `ctx.internalFile`: for linker-defined symbols and some synthesized
`Undefined`
* `createInternalFile`: for symbol assignments and --defsym

I picked "internal" instead of "synthetic" to avoid confusion with
SyntheticSection.

Currently a symbol's file is one of: nullptr, ObjKind, SharedKind,
BitcodeKind, BinaryKind. Now it's non-null (I plan to add an
`assert(file)` to Symbol::Symbol and change `toString(const InputFile
*)`
separately).

Debugging and error reporting gets improved. The immediate user-facing
difference is more descriptive "File" column in the --cref output. This
patch may unlock further simplification.

Currently each symbol assignment gets its own
`createInternalFile(cmd->location)`. Two symbol assignments in a linker
script do not share the same file. Making the file the same would be
nice, but would require non trivial code.
2024-01-22 09:09:46 -08:00
Emma Pilkington
4897b9888f
[AMDGPU] Make a few more tests default COV agnostic (#78926) 2024-01-22 11:22:57 -05:00
Kazu Hirata
21730eb49b [lld] Use SmallString::operator std::string (NFC) 2024-01-22 00:13:23 -08:00
Fangrui Song
665f913e45 [ELF] Reimplement unknown -z options using the isClaimed bit
Maintaining the long list of known -z options
(https://reviews.llvm.org/D48621) turns out to be cumbersome. Go the
D48433 route instead.

max-page-size/common-page-size are claimed when `target` is available.

Inspired by: https://reviews.llvm.org/D48433
2024-01-21 18:15:57 -08:00
Fangrui Song
2e30e31e1e [ELF] Claim recognized -z options. NFC
... so that we can reimplement `checkZOptions` using
https://reviews.llvm.org/D48433
2024-01-21 17:43:05 -08:00
Fangrui Song
3a4615c0fe [ELF] Clarify the first entry of .got.plt NFC
Differential Revision: https://reviews.llvm.org/D47053
2024-01-21 16:42:44 -08:00
OldWorldOrdr
46a9135d61
[lld-macho] Find objects in library search path (#78628)
Find object files in library search path just like Apple's linker, this
makes building with some older MacOS SDKs easier since clang runs with
`-lcrt1.10.6.o`
2024-01-20 13:53:55 -08:00
Martin Storsjö
d0986519d5
[LLD] [COFF] Preserve directives and export names from LTO objects (#78802)
The export names are saved as StringRefs pointing into the COFF
directives. In the case of LTO objects, this can be memory allocated
that is owned by the LTO InputFile, which gets destructed when doing the
compilation.

In the case of LTO objects from an older version of LLVM, which require
being upgraded when loaded, the directives string gets destructed, while
when using LTO objects of a matching version (the common case), the
directives string points into memory that doesn't get destructed on LTO
compilation.

Test this by linking a bundled binary LTO object file, from an older
version of LLVM.

This fixes issue #78591, and downstream issue
https://github.com/mstorsjo/llvm-mingw/issues/392.
2024-01-20 16:15:44 +02:00
Fangrui Song
4b500147f0 [ELF] Improve LTO tests
Make it easy to change --save-temps filenames to follow COFF
(https://reviews.llvm.org/D137217).
2024-01-19 23:44:40 -08:00
Fangrui Song
963d7b4b2a [ELF] Improve --thinlto-index-only and --thinlto-emit-index-files tests 2024-01-19 22:02:21 -08:00
Sam Clegg
58d5a486ec
[lld][WebAssembly] Fix regression in function signature checking (#78831)
Followup to #78658, which caused a regression in emscripten.

When a lazy symbol is added, which resolved and existing undefined
symbol, we don't need/want to replace the undefined symbol with the lazy
one. Instead we called extract, which replaces the undefined symbol with
the defined one.

The fact that we were first replacing the undefined symbol with a lazy
one before extracting the archive member doesn't normally matter but, in
the case of the function symbol, replacing the undefined symbol with a
lazy symbol means that `addDefinedFunction` sees the existing symbol as
lazy and simply replaces it.

Note that this is consistent with both the ELF code in
`Symbol::resolve(const LazySymbol &other)` and the wasm code prior to
 #78658, neither of which replace the existing symbol with the lazy one
in this case.
2024-01-19 21:45:30 -08:00
Sam Clegg
bcc9b9d80c
[lld][WebAssembly] Match the ELF linker in transitioning away from archive indexes. (#78658)
The ELF linker transitioned away from archive indexes in
https://reviews.llvm.org/D117284.

This paves the way for supporting `--start-lib`/`--end-lib` (See #77960)

The ELF linker unified library handling with `--start-lib`/`--end-lib` and removed
the ArchiveFile class in https://reviews.llvm.org/D119074.
2024-01-19 16:20:29 -08:00
Fangrui Song
7071a25d12
[ELF] Rename LazyObject to LazySymbol. NFC
LazySymbol (used by wasm port) is a more accurate name (the struct is
not about an object). However, the ELF port calls this LazyObject likely
because we used to have LazyArchive (removed by
https://reviews.llvm.org/D119074), and LazyObject has a similar naming
convention (LazyObjectSymbol would be better, but it is too long).

Reviewers: dschuff

Pull Request: https://github.com/llvm/llvm-project/pull/78809
2024-01-19 16:15:52 -08:00
Sam Clegg
39e024d9e2
[lld][WebAssembly] Use the archive offset with --whole-archive (#78791)
This essentially ports 0b1413a8 from the ELF linker.
2024-01-19 14:42:03 -08:00
Sam Clegg
2bfa5ca927
[lld][WebAssembly] Reset context object after each link (#78770)
This mirrors how the ELF linker works. I wasn't able to find anywhere
where this is currently tested.

Followup to #78640, which triggered a regression.
2024-01-19 13:51:35 -08:00
Sam Clegg
f5e58a0380
[lld][ELF] Simplify handleLibcall. NFC (#78659)
I noticed this while working on #78658
2024-01-19 12:39:35 -08:00
Sam Clegg
5b0e45c8ce
[lld][WebAssembly] Fix use of undefined funcs under --warn-unresolved-symbols (#78643)
When undefined functions exist in the final link we need to create
stub functions (otherwise direct calls to those functions could
not be generated).  We were creating those stub when
`--unresolved-symbols=ignore-all` was passed but overlooked the fact
that `--warn-unresolved-symbols` essentially has the same effect (i.e.
undefined function can exist in the final link).

Fixes: #53987
2024-01-19 09:32:22 -08:00
Simon Pilgrim
a2a0089ac3
[X86] movsd/movss/movd/movq - add support for constant comments (#78601)
If we're loading a constant value, print the constant (and the zero upper elements) instead of just the shuffle mask.

This did require me to move the shuffle mask handling into addConstantComments as we can't handle this in the MC layer.
2024-01-19 14:21:26 +00:00
Sam Clegg
3c5845703c
[lld][WebAssembly] Move input vectors from symtab to ctx. NFC (#78640)
Also convert from std::vector to SmallVector.

This matches the ELF linker where these were moved into the ctx object
in 9a572164d5 and converted to SmallVector in ba948c5a9c.
2024-01-18 15:53:13 -08:00
Sam Clegg
184c22dd3a
[lld][WebAssembly] Move linker global state in to context object. NFC (#78629)
See lld/ELF/Config.h
2024-01-18 15:01:21 -08:00
Sam Clegg
f268495914
[lld][WebAssembly] Rename fetch() to extract() to match ELF linker. NFC (#78625) 2024-01-18 14:39:38 -08:00
kyulee-com
5de1d007dd
[lld-macho] Fix for objc_msgSend stubs (#78557)
This commit corrects the address computation for objc_msgSend stubs.
Previously, the address computation was incidentally correct due to
objc_msgSend often being the first entry in the got section, resulting
in a 0 index. This commit ensures accurate address computation
regardless of the objc_msgSend stub's position in the got section.
2024-01-18 10:46:30 -08:00
Arthur Eubanks
1d4c0092a8
[lld/ELF] Hint if R_X86_64_PC32 overflows and references a SHF_X86_64_LARGE section (#73045)
Makes it clearer what the issue is when hand-written assembly doesn't
follow medium code model assumptions in a medium code model build.

Alternative to #71248 by only hinting on an overflow.
2024-01-17 15:38:59 -08:00
Philip Reames
987123e4f1
[LLD][RISCV] Report error for unsatisfiable RISCV_ALIGN (#74121)
If we have a RISCV_ALIGN relocation which can't be satisfied with the
available space provided, report an error rather than silently
continuing with a corrupt state.

For context, https://github.com/llvm/llvm-project/pull/73977 fixes an
LLD bug which can cause this effect, but that's not the only source of
such cases.

Another is our hard-to-fix set of LTO problems. We can have a single
function which was compiled without C in an otherwise entirely C module.
Until we have all of the mapping symbols and related mechanisms
implemented, this case can continue to arise.

I think it's very important from a user interface perspective to have
non-assertion builds report an error in this case. If we don't report an
error here, we can crash the linker (due to the fatal error at the
bottom of the function), or if we're less lucky silently produce a
malformed binary.

There's a couple of known defects with this patch.

First, there's no test case. I don't know how to write a stable test
case for this that doesn't involve hex editing an object file, or
abusing the LTO bug that we hope to fix.

Second, this will report an error on each relax iteration. I explored
trying to report an error only once after relaxation, but ended up
deciding I didn't have the context to implement it safely.

I would be thrilled if someone more knowledgeable of this code wants to
write a better version of this patch, but in the meantime, I believe we
should land this to address the user experience problem described above.
2024-01-17 14:32:20 -08:00
modiking
17c390fc78
[WPD][LLD] Allow glob matching of --lto-known-safe-vtables (#78505)
Makes it easier to exclude a pattern of safe vtable symbols

Testing:
ninja check-all
2024-01-17 14:31:12 -08:00
Derek Schuff
103fa3250c
[WebAssembly] Use ValType instead of integer types to model wasm tables (#78012)
LLVM models some features found in the binary format with raw integers
and others with nested or enumerated types. This PR switches modeling of
tables and segments to use wasm::ValType rather than uint32_t. This NFC
change is in preparation for modeling more reference types, but IMO is
also cleaner and closer to the spec.
2024-01-17 11:29:19 -08:00