128 Commits

Author SHA1 Message Date
Fangrui Song
94655dc8ae [ELF] -r: Synthesize R_RISCV_ALIGN at input section start" (#151639)
Clear `synthesizedAligns` to prevent stray relocations to an unrelated
text section. Enhance the test to check llvm-readelf -r output.

---

Without linker relaxation enabled for a particular relocatable file or
section (e.g., using .option norelax), the assembler will not generate
R_RISCV_ALIGN relocations for alignment directives. This becomes
problematic in a two-stage linking process:

```
ld -r a.o b.o -o ab.o
// b.o is norelax. Its alignment information is lost in ab.o.
ld ab.o -o ab
```

When ab.o is linked into an executable, the preceding relaxed section
(a.o's content) might shrink. Since there's no R_RISCV_ALIGN relocation
in b.o for the linker to act upon, the `.word 0x3a393837` data in b.o
may end up unaligned in the final executable.

To address the issue, this patch inserts NOP bytes and synthesizes an
R_RISCV_ALIGN relocation at the beginning of a text section when the
alignment >= 4.

For simplicity, when RVC is disabled, we synthesize an ALIGN relocation
(addend: 2) for a 4-byte aligned section, allowing the linker to trim
the excess 2 bytes.

See also https://sourceware.org/bugzilla/show_bug.cgi?id=33236
2025-08-12 22:38:17 -07:00
Fangrui Song
98164d4706 Revert "[ELF] -r: Synthesize R_RISCV_ALIGN at input section start" (#151639)
This reverts commit 6f53f1c8d2bdd13e30da7d1b85ed6a3ae4c4a856.

synthesiedAligns is not cleared, leading to stray relocations for
unrelated sections. Revert for now.
2025-08-12 22:18:15 -07:00
Fangrui Song
6f53f1c8d2
[ELF] -r: Synthesize R_RISCV_ALIGN at input section start
Without linker relaxation enabled for a particular relocatable file or
section (e.g., using .option norelax), the assembler will not generate
R_RISCV_ALIGN relocations for alignment directives. This becomes
problematic in a two-stage linking process:

```
ld -r a.o b.o -o ab.o
// b.o is norelax. Its alignment information is lost in ab.o.
ld ab.o -o ab
```

When ab.o is linked into an executable, the preceding relaxed section
(a.o's content) might shrink. Since there's no R_RISCV_ALIGN relocation
in b.o for the linker to act upon, the `.word 0x3a393837` data in b.o
may end up unaligned in the final executable.

To address the issue, this patch inserts NOP bytes and synthesizes an
R_RISCV_ALIGN relocation at the beginning of a text section when the
alignment >= 4.

For simplicity, when RVC is disabled, we synthesize an ALIGN relocation
(addend: 2) for a 4-byte aligned section, allowing the linker to trim
the excess 2 bytes.

See also https://sourceware.org/bugzilla/show_bug.cgi?id=33236

Pull Request: https://github.com/llvm/llvm-project/pull/151639
2025-08-08 18:40:40 -07:00
Fangrui Song
f9b68838f6
ELF: -r: Call assignAddresses only once
The fixed-point layout algorithm handles linker scripts, thunks, and
relaxOnce (to suppress out-of-range GOT-indirect-to-PC-relative
optimization). These passes are not needed for relocatable links because
they require address information that is not yet available.

Since we don't scan relocations for relocatable links, the
`createThunks` and `relaxOnce` functions are no-ops anyway, making these
passes redundant.

To prevent cluttering the line history, I place the `if (...) break;`
inside the for loop.

Pull Request: https://github.com/llvm/llvm-project/pull/152240
2025-08-07 08:31:27 -07:00
Fangrui Song
8957e64a20
[ELF,RISCV] Fix oscillation due to call relaxation
The new test (derived from riscv32 openssl/test/cmp_msg_test.c) revealed
oscillation in two R_RISCV_CALL_PLT jumps:

- First jump (~2^11 bytes away): alternated between 4 and 8 bytes.
- Second jump (~2^20 bytes away): alternated between 2 and 8 bytes.

The issue is not related to alignment. In 2019, GNU ld addressed a
similar problem by reducing the relaxation allowance for cross-section
relaxation (https://sourceware.org/bugzilla/show_bug.cgi?id=25181).
This approach would result in a suboptimal layout for the tight range
tested by riscv-relax-call.s.

This patch stabilizes the process by preventing `remove` increment after
a few passes, similar to integrated assembler's fragment relaxation.
(For the Android bit reproduce, `pass < 2` leads to non-optimal layout
while `pass < 3` and `pass < 4` output is identical.)

Fix https://github.com/llvm/llvm-project/issues/113838
Possibly fix https://github.com/llvm/llvm-project/issues/123248 (inputs
are bitcode, subject to ever-changing code generation, not reproducible)

Pull Request: https://github.com/llvm/llvm-project/pull/142899
2025-06-10 09:28:39 -07:00
Philip Reames
631769f2a0
[LLD][RISCV] Add relaxation for absolute int12 Hi20Lo12 (#86124)
If we have an absolute address whose high bits are known to be a sign
extend of the low 12 bits, we can avoid emitting the LUI entirely. This
is implemented in an analogous manner to the gp relative relocations -
defining an internal usage relocation type.

Since 12 bits (really 11 since the high bit must be zero in user code)
is less than one page, all of these offsets fit in the null page. As
such, the only application of these is likely to be undefined weak
symbols except for embedded use cases. I'm mostly posting this for
completeness sake.
2025-03-20 18:56:56 -07:00
Fangrui Song
84af3ee512 [ELF] Replace Fatal with Err 2025-01-26 16:13:52 -08:00
Sam Elliott
65ced158e9
[RISCV] Remove R_RISCV_RVC_LUI Relocation (#118714)
This was removed from the ABI in riscv-non-isa/riscv-elf-psabi-doc#398.
It is not emitted by LLVM, and seems to have been an internal
implementation detail in binutils.

This is a follow-up to 26ec5da744b8 which removed previous binutils
internal relocations when they were removed from the ABI.

The LLD implementation was not tested when it was added in
https://reviews.llvm.org/D39322
2024-12-05 10:10:27 +00:00
Fangrui Song
04996a28b7
[ELF] Rename target-specific RelExpr enumerators
RelExpr enumerators are named `R_*`, which can be confused with ELF
relocation type names. Rename the target-specific ones to `RE_*` to
avoid confusion.

For consistency, the target-independent ones can be renamed as well, but
that's not urgent. The relocation processing mechanism with RelExpr has
non-trivial overhead compared with mold's approach, and we might make
more code into Arch/*.cpp files and decrease the enumerators.

Pull Request: https://github.com/llvm/llvm-project/pull/118424
2024-12-03 09:17:17 -08:00
Fangrui Song
1cd627562b [ELF] Remove unneeded Twine in ELFSyncStream 2024-11-24 12:13:02 -08:00
Fangrui Song
c4dc5ed825 [ELF] Avoid some toStr and ErrAlways 2024-11-24 11:43:40 -08:00
Fangrui Song
a5af6214dd [ELF] Make SyntheticSection parameter order match InputSection
And rename `/*alignment=*/` to `/*addralign=*/`
2024-11-23 15:16:36 -08:00
Fangrui Song
2991a4e209 [ELF] Replace functions bAlloc/saver/uniqueSaver with member access 2024-11-16 22:34:13 -08:00
Fangrui Song
483516fd83 [ELF] Remove unneeded Twine() 2024-11-16 20:32:44 -08:00
Fangrui Song
c1a6defd9f [ELF] Make RelType a struct type
otherwise operator<<(const ELFSyncStream &s, RelType type) applies to
non-reloc-type uint32_t, which can be confusing.
2024-11-16 20:26:34 -08:00
Fangrui Song
a626eb2a2f [ELF] Pass ctx to bAlloc/saver/uniqueSaver 2024-11-16 15:20:21 -08:00
Fangrui Song
6c19fa4bfc [ELF] Remove unneeded toString(Error) when using ELFSyncStream 2024-11-16 13:31:05 -08:00
Fangrui Song
38870fe124 [ELF] Remove unneeded toString(Error) when using ELFSyncStream 2024-11-16 13:22:06 -08:00
Fangrui Song
be5dad012e [ELF] Replace internalLinkerError(getErrorLoc(ctx, buf) + ...) with InternalErr(ctx, buf)
and simplify `+ toStr(ctx, x)` to `<< x`.
The trailing '\n' << llvm::getBugReportMsg() is not very useful and
therefore removed.
2024-11-16 13:07:17 -08:00
Fangrui Song
58a971f42f [ELF] Replace contex-less toString(x) with toStr(ctx, x)
so that we can remove the global `ctx` from toString implementations.
Rename lld::toString (to lld:🧝:toStr) to simplify name lookup (we
have many llvm::toString and another lld::toString(const llvm::opt::Arg
&)).
2024-11-16 11:58:10 -08:00
Fangrui Song
47e6673006 [ELF] Replace toString(RelType) with operator<< while using ELFSyncStream 2024-11-16 10:12:08 -08:00
Fangrui Song
d69cc05bcf [ELF] Migrate away from global ctx 2024-11-14 22:30:29 -08:00
Fangrui Song
c13258ac49 [ELF] Replace log with Log(ctx) 2024-11-07 09:30:20 -08:00
Fangrui Song
9b058bb42d [ELF] Replace errorOrWarn(...) with Err 2024-11-06 22:33:51 -08:00
Fangrui Song
f8bae3af74 [ELF] Replace warn(...) with Warn 2024-11-06 22:19:31 -08:00
Fangrui Song
09c2c5e1e9 [ELF] Replace error(...) with ErrAlways or Err
Most are migrated to ErrAlways mechanically.
In the future we should change most to Err.
2024-11-06 22:04:52 -08:00
Fangrui Song
63c6fe4a0b [ELF] Replace fatal(...) with Fatal or Err 2024-11-06 21:17:26 -08:00
Fangrui Song
e6625a2c10 [ELF] Pass Ctx & 2024-10-19 21:08:50 -07:00
Fangrui Song
861bd36bce [ELF] Pass Ctx & to Symbol::getVA 2024-10-19 20:32:58 -07:00
Fangrui Song
2c5dd03f55 [ELF] Pass Ctx & to check* 2024-10-13 11:14:40 -07:00
Fangrui Song
38dfcd9ac9 [ELF] Pass Ctx & to read32/write32 2024-10-13 10:37:47 -07:00
Fangrui Song
81bd712f92 [ELF] Revert Ctx & parameters from SyntheticSection
Since Ctx &ctx is a member variable,
1f391a75af8685e6bba89421443d72ac6a186599
7a5b9ef54eb96abd8415fd893576c42e51fd95db
e2f0ec3a3a8a2981be8a1aac2004cfb9064c61e8 can be reverted.
2024-10-10 23:43:21 -07:00
Fangrui Song
25cda9e069 [ELF] Pass Ctx & to SyntheticSection 2024-10-10 23:07:02 -07:00
Fangrui Song
e1a073c9d9 [ELF] Change Ctx::target to unique_ptr (#111260)
also rename `TargetInfo *getXXXTargetInfo` to `void setXXXTargetInfo`
and change it to set `ctx.target`. This ensures that when `ctx` becomes
a local variable, two lld invocations will not reuse the function-local
static variable.

---

Reland after commit c35214c131c0bc7f54dc18ceb75c75cba89f58ee
([ELF] Initialize TargetInfo members).
2024-10-07 23:14:02 -07:00
Paul Kirth
2ca850111f
Revert "[ELF] Change Ctx::target to unique_ptr (#111260)" (#111449)
This patch seems to be breaking the windows build bots.
https://lab.llvm.org/buildbot/#/builders/63/builds/1953

We also see this in Fuchsia's Linux CI:
https://fxbug.dev/372010530

This reverts commit 4ec06b17435e32ece5e1aa2bc8a6d26dbf0bb312.
2024-10-07 15:43:01 -07:00
Fangrui Song
4ec06b1743
[ELF] Change Ctx::target to unique_ptr (#111260)
also rename `TargetInfo *getXXXTargetInfo` to `void setXXXTargetInfo`
and change it to set `ctx.target`. This ensures that when `ctx` becomes
a local variable, two lld invocations will not reuse the function-local
static variable.
2024-10-06 21:47:13 -07:00
Fangrui Song
cfd3289a1f [ELF] Pass Ctx & to some free functions 2024-10-06 19:36:21 -07:00
Fangrui Song
acb2b1e779 [ELF] Pass Ctx & to Symbols 2024-10-06 16:59:04 -07:00
Fangrui Song
2b5cb1bf62 [ELF] getRelocTargetVA: pass Ctx and Relocation. NFC 2024-10-06 16:34:09 -07:00
Fangrui Song
6d03a69034 [ELF] Pass Ctx & to Arch/ 2024-10-06 00:14:12 -07:00
Fangrui Song
e2f0ec3a3a [ELF] Pass Ctx & to SyntheticSection::getSize 2024-10-03 22:53:07 -07:00
Fangrui Song
7a5b9ef54e [ELF] Pass Ctx & to SyntheticSection::writeTo 2024-10-03 20:56:09 -07:00
Fangrui Song
079b8327ec [ELF] Pass Ctx & to InputFiles and SyntheticSections 2024-09-29 16:06:47 -07:00
Fangrui Song
c3e4998c0b [ELF] Pass Ctx & to TargetInfo. NFC 2024-09-28 21:48:26 -07:00
Fangrui Song
1dd9a565ea [ELF] Replace config-> with ctx.arg. in Arch/ 2024-09-21 12:03:18 -07:00
Fangrui Song
e88b7ff016 [ELF] Move InStruct into Ctx. NFC
Ctx was introduced in March 2022 as a more suitable place for such
singletons.

llvm/Support/thread.h includes <thread>, which transitively includes
sstream in libc++ and uses ios_base::in, so we cannot use `#define in ctx.sec`.

`symtab, config, ctx` are now the only variables using
LLVM_LIBRARY_VISIBILITY.
2024-09-15 22:15:02 -07:00
Fangrui Song
b4feb26606 [ELF] Move target to Ctx. NFC
Ctx was introduced in March 2022 as a more suitable place for such
singletons.

Follow-up to driver (2022-10) and script (2024-08).
2024-08-21 23:53:36 -07:00
Fangrui Song
c62fa63ff1 [ELF] Move mainPart to Ctx. NFC
Ctx was introduced in March 2022 as a more suitable place for such
singletons.
2024-08-21 20:08:11 -07:00
Fangrui Song
2fe3bbdf67 [ELF] Move outputSections into Ctx. NFC
Ctx was introduced in March 2022 as a more suitable place for such
singletons.
2024-08-03 11:50:48 -07:00
Fangrui Song
03be619d94 [ELF] Move ElfSym into Ctx. NFC
Ctx was introduced in March 2022 as a more suitable place for such
singletons. ctx's hidden visibility optimizes generated instructions.

This change fixes a pitfall: certain ElfSym members (e.g.
globalOffsetTable, tlsModuleBase) were not zeroed and might be stale
when lld:🧝:link was invoked the second time.
2024-08-03 11:20:32 -07:00