Issue 243704
Summary [lld][LoongArch] ld.lld does not bound the R_LARCH_ALIGN removal by the section: finalizeRelax's new section size wraps
Labels lld:ELF, crash, backend:loongarch
Assignees
Reporter AnantaSrikar
    `relax` computes how many bytes an `R_LARCH_ALIGN` removes from its addend and the current address only, without checking that those bytes are in the section. When the removal is larger than what is left after the relocation, `LoongArch::finalizeRelax` computes `newSize = old.size() - relocDeltas[last]`, which wraps, and passes it to the bump allocator. An ASan build stops with a CHECK failure inside `__asan_unpoison_memory_region` called from `finalizeRelax`. Without ASan, `ld.lld` exits 0: `_start` gets size 18446744073709551612, and three identical links give three different outputs, 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_LOONGARCH
  Flags:           [ EF_LOONGARCH_ABI_DOUBLE_FLOAT, EF_LOONGARCH_OBJABI_V1 ]
Sections:
  - Name: .text
    Type:            SHT_PROGBITS
    Flags:           [ SHF_ALLOC, SHF_EXECINSTR ]
    AddressAlign:    0x4
    Content: '0000400300004003'
  - Name:            .text.f
    Type: SHT_PROGBITS
    Flags:           [ SHF_ALLOC, SHF_EXECINSTR ]
 AddressAlign:    0x4
    Content:         2000004C2000004C
  - Name: .rela.text
    Type:            SHT_RELA
    Flags:           [ SHF_INFO_LINK ]
    Link:            .symtab
    AddressAlign:    0x8
 Info:            .text
    Relocations:
      - Offset:          0x4
 Symbol:          _start
        Type:            R_LARCH_ALIGN
 Addend:          0x104
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 two `nop`s, with `R_LARCH_ALIGN` at offset 4 and addend 0x104 (16-byte alignment, at most 1 byte skipped). `relax` removes 12 bytes, but only 4 are left, so the new size is 8 - 12. With addend 0x103, the same object links and gives the same output every time. Without `.text.f`, release builds stop with `section .text ... of size 0xfffffffffffffffc exceeds available address space` instead; with it, the output section's size wraps back to a small value and the link succeeds.

### Observed

```
AddressSanitizer: CHECK failed: asan_poisoning.cpp:230 "((beg.chunk)) < ((end.chunk))" (0xfb8f99742fb, 0xfb8f99742fa) (tid=7)
    #2 in __asan_unpoison_memory_region
    #3 in Allocate llvm/include/llvm/Support/Allocator.h:186:7
    #7 in (anonymous namespace)::LoongArch::finalizeRelax(int) const lld/ELF/Arch/LoongArch.cpp:1753:31
    #8 in finalizeAddressDependentContent lld/ELF/Writer.cpp:1632:17
```

### Expected

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

### Analysis

The removal (only a negative value is diagnosed):

https://github.com/llvm/llvm-project/blob/c7014922f5829426952a38d079220c21b0a8ebe8/lld/ELF/Arch/LoongArch.cpp#L1301-L1323

The new size and allocation:

https://github.com/llvm/llvm-project/blob/c7014922f5829426952a38d079220c21b0a8ebe8/lld/ELF/Arch/LoongArch.cpp#L1751-L1753

Rejecting an `R_LARCH_ALIGN` whose removal is larger than `sec.content().size() - r.offset`, next to the existing "insufficient padding bytes" error, would fix it. RISC-V has the same missing check for `R_RISCV_ALIGN` (#243688).

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