Issue 244780
Summary [LLD][ELF][AArch64] LLD incompletely relaxes ADRP/LDR for split functions
Labels backend:AArch64, lld:ELF
Assignees
Reporter dhoekwater
    ### Summary
When two `ADRP`/`LDR` pairs share an `LDR` instruction (as may happen due to basic block tail merging), `LLD` relaxes one of the `ADRP` instructions but not the other if the `ADRP` instructions are in different input sections.

Although commit 8d590969badd9f3fed18a99fffbd9afdf3a975c6 (#208396) fixes #138254 for `ADRP` instructions in the _same_ `InputSection`, it's valid for `ADRP` instructions in _different_ input sections to transfer control flow to the same `LDR` instruction (as is the case with machine function splitting).

### Repro
We can reproduce this bug with the following assembly:
```
// multi_section_adrp.s
.data
.space 0x200000 // Keep `sym` >= 1 MiB from .text so ADRP+LDR relaxes to ADRP+ADD
.globl sym
.hidden sym
sym:
  .word 0x12345678

.section .text.hot,"ax",@progbits
.globl _start, cold_entry
_start:
  adrp x8, :got:sym
.Lshared_ldr:
  ldr  x8, [x8, :got_lo12:sym]
  ldr  w0, [x8]
 ret

.section .text.split,"ax",@progbits
cold_entry:
  adrp x8, :got:sym
  b    .Lshared_ldr
```

`cold_entry` currently links incorrectly when linked with `ld.lld --relax`:
```
$ clang --target=aarch64-linux-gnu -c repro.s -o repro.o

$ ld.lld -pie repro.o -o repro.lld
$ llvm-objdump -h repro.lld | grep -E "(Idx|\.text|\.got|\.data)"
$ llvm-objdump -d --no-show-raw-insn repro.lld

Idx Name           Size     VMA              Type
  6 .text 00000018 0000000000010260 TEXT
  8 .got           00000008 0000000000020348 DATA
 10 .data          00200004 0000000000030350 DATA   # sym is at 0x230350

0000000000010260 <_start>:
   10260:       adrp    x8, 0x230000
   10264:       add     x8, x8, #0x350              # relaxed from LDR [x8, :got_lo12:sym]
   10268:       ldr     w0, [x8]
   1026c: ret

0000000000010270 <cold_entry>:
   10270:       adrp    x8, 0x20000 # unrelaxed: still points at .got page!
   10274:       b 0x10264 <_start+0x4>        # jumps onto ADD -> loads from 0x20350 instead of 0x230350
```

Meanwhile, linking with `--no-relax` produces the right machine code:
```
$ ld.lld -pie --no-relax repro.o -o repro.lld_norelax
$ llvm-objdump -h repro.lld_norelax | grep -E "(Idx|\.text|\.got|\.data)"
$ llvm-objdump -d --no-show-raw-insn repro.lld_norelax

Idx Name           Size     VMA              Type
  6 .text          00000018 0000000000010260 TEXT
  8 .got           00000008 0000000000020348 DATA   # sym GOT slot at 0x20348
 10 .data 00200004 0000000000030350 DATA

0000000000010260 <_start>:
   10260: adrp    x8, 0x20000
   10264:       ldr     x8, [x8, #0x348]            # unrelaxed GOT load (0x20348)
   10268:       ldr     w0, [x8]
   1026c: ret

0000000000010270 <cold_entry>:
   10270:       adrp    x8, 0x20000 # points to same .got page
   10274:       b       0x10264 <_start+0x4>        # jumps onto LDR -> loads &sym from 0x20348
```

### Possible solutions
Although this assembly is technically valid input to the linker, the faulty behavior is only exposed by binaries where the same symbol can span multiple ELF sections (like machine function splitting, `basic-block-sections`). We could mitigate it in the compiler in a few ways:
-  Avoid splitting `ADRP` or `LDR` instructions that refer to the same relocation. This approach doesn't fully work though, since `-fbasic-block-sections=list` could split a function into more than two sections, and there's not a clear default section to assign to the `ADRP` / `LDR` instructions here.
- Defer `GOTload` pseudo-instruction expansion until after MFS and BBSections. I think this just works, but linker ICF could throw a wrench in things if it folds symbols that span multiple sections.

In the linker, we could potentially solve the mislink with some more sophisticated control flow analysis during relaxation, but I suspect it'd get a little hairy.

Tagging @nikic (who fixed #138254) and @MaskRay (the LLD ELF maintainer, per [`lld/Maintainers.md#elf-backend`](https://github.com/llvm/llvm-project/blob/main/lld/Maintainers.md#elf-backend)) for visibility.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to