Issue 243688
Summary [lld][RISCV] ld.lld trusts the R_RISCV_ALIGN addend past the end of the section: finalizeRelax writes past the shrunk section
Labels backend:RISC-V, lld:ELF, crash
Assignees
Reporter AnantaSrikar
    During relaxation, `ld.lld` takes the padding length of an `R_RISCV_ALIGN` from its addend without checking that `r.offset + r.addend` is inside the section. When the section ends before the claimed padding does, `relax` still removes bytes as if the padding were there, the section is shrunk, and `finalizeRelax` writes the NOP sequence for the full addend into the smaller buffer. ASan reports a use-after-poison write in `RISCV::finalizeRelax` for static, `-pie` and `-shared` links. Without ASan, `ld.lld` exits 0 with no diagnostic, on main and on 21.1.8.

**Version**: main at c7014922f582 (2026-11-16), built from the Git repository with `-DCMAKE_BUILD_TYPE=Release -DLLVM_ENABLE_ASSERTIONS=ON -DLLVM_ENABLE_PROJECTS=lld -DLLVM_USE_SANITIZER="Address;Undefined"`, on x86_64 Linux.

### Reproducer

```sh
cat > align.yaml <<'YAML'
--- !ELF
FileHeader:
 Class:           ELFCLASS64
  Data:            ELFDATA2LSB
  Type: ET_REL
  Machine:         EM_RISCV
  Flags:           [ EF_RISCV_RVC, EF_RISCV_FLOAT_ABI_DOUBLE ]
Sections:
  - Name:            .text
    Type: SHT_PROGBITS
    Flags:           [ SHF_ALLOC, SHF_EXECINSTR ]
 AddressAlign:    0x4
    Content:         '1385150001001300'
  - Name: .rela.text
    Type:            SHT_RELA
    Flags:           [ SHF_INFO_LINK ]
    Link:            .symtab
    AddressAlign:    0x8
 Info:            .text
    Relocations:
      - Offset:          0x4
 Type:            R_RISCV_ALIGN
        Addend:          6
Symbols:
  - Name:            _start
    Type:            STT_FUNC
    Section: .text
    Binding:         STB_GLOBAL
    Size: 0x8
...
YAML
yaml2obj align.yaml -o align.o
ld.lld align.o -o align.out
```

`.text` is 8 bytes: an `addi` and 4 bytes of padding, with `R_RISCV_ALIGN` at offset 4 claiming 6. `relax` removes 2 bytes, so the section becomes 6 bytes, and `finalizeRelax` then writes a 4-byte `nop` at offset 4. With the full padding and a following instruction (`Content: '138515000100130000008280'`, `Size: 0xC`), the same object links cleanly. `-r` is not affected.

### Observed

```
==7==ERROR: AddressSanitizer: use-after-poison on address 0x7dd5387e17df at pc 0x55987e74b5a2 bp 0x7ffd8a7a8070 sp 0x7ffd8a7a8068
WRITE of size 4 at 0x7dd5387e17df thread T0
    #4 in write32le llvm/include/llvm/Support/Endian.h:456:3
    #5 in (anonymous namespace)::RISCV::finalizeRelax(int) const lld/ELF/Arch/RISCV.cpp:1328:15
    #6 in finalizeAddressDependentContent lld/ELF/Writer.cpp:1632:17
```

### Expected

An error that the `R_RISCV_ALIGN` padding extends past the end of the section.

### Analysis

The removal is computed from the addend; only a negative result is diagnosed:

https://github.com/llvm/llvm-project/blob/c7014922f5829426952a38d079220c21b0a8ebe8/lld/ELF/Arch/RISCV.cpp#L1050-L1065

and the NOPs are written for `r.addend - remove` bytes:

https://github.com/llvm/llvm-project/blob/c7014922f5829426952a38d079220c21b0a8ebe8/lld/ELF/Arch/RISCV.cpp#L1317-L1332

Rejecting an `R_RISCV_ALIGN` with `r.offset + r.addend > sec->content().size()` next to the existing "insufficient padding bytes" error would fix it.

This report was drafted with help from an AI assistant (Claude); I reviewed it and reproduced it myself.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to