<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/202177>202177</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [RISC-V][lld] ld.lld PIE undefined weak absolute dynamic relocations dropped after 18
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            lld
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          XiaobingHou1219
      </td>
    </tr>
</table>

<pre>
    I ran into this while reducing a RISC-V linker testcase. The reproducer is small, and I have been seeing the same result consistently across three reruns.

### Summary
ld.lld 18.1.8 keeps the required weak-undef `R_RISCV_64/32` dynamic relocation in both the data and GOT PIE families, with and without `-z pack-relative-relocs`, but ld.lld 21.1.3 and 22.1.0 link the same legal PIEs successfully after dropping that weak-undef `.rela.dyn` entry and leaving only `R_RISCV_RELATIVE` and RELR-covered locations behind.

### Expected behavior
Link legal PIEs that materialize an undefined weak symbol through two meaningful patterns: raw data words that hold the optional symbol address and GOT-based `la` address materialization in startup code. A correct linker must keep the weak-undef dynamic relocation instead of eagerly collapsing the address to a purely relative form.

### Environment
- linker route: GNU as plus ld.lld version replay with Sourceware DT_RELR controls and non-RELR PIE controls
- march: rv32i/rv64i
- mabi: ilp32/lp64
- first failing stage: link
- local stability check: True

### Reduced testcase
These are the reduced input files from the minimized reproducer I used locally:

#### `relr-data.s`
```asm
.macro data value
.ifdef __64_bit__
        .quad \value
.else
        .long \value
.endif
.endm

.text
.global _start
_start:
        nop

sym_local:
        nop

.global sym_hidden
.hidden sym_hidden
sym_hidden:
        nop

.global sym_global
sym_global:
        nop

.global sym_global_abs
.set sym_global_abs, 42

.global sym_weak_undef
.weak sym_weak_undef

.section .data.unaligned_local
unaligned_local:
data sym_local

.section .data.unaligned_hidden
unaligned_hidden:
data sym_hidden

.section .data.unaligned_global
unaligned_global:
data sym_global

.section .data.unaligned_DYNAMIC
unaligned_DYNAMIC:
data _DYNAMIC

.section .data.aligned_local
.p2align 1
aligned_local:
data sym_local

.section .data.aligned_hidden
.p2align 1
aligned_hidden:
data sym_hidden

.section .data.aligned_global
.p2align 1
aligned_global:
data sym_global

.section .data.aligned_global_abs
.p2align 1
aligned_global_abs:
data sym_global_abs

.section .data.aligned_weak_undef
.p2align 1
aligned_weak_undef:
data sym_weak_undef

.section .data.aligned_DYNAMIC
.p2align 1
aligned_DYNAMIC:
data _DYNAMIC
```

#### `relr-got.s`
```asm
.text
.option norelax
.option pic
.global _start
_start:
        la      x1, sym_local
        la      x1, sym_hidden
        la      x1, sym_global
        la      x1, sym_global_abs
        la      x1, sym_weak_undef
        la      x1, _DYNAMIC

sym_local:
        nop

.global sym_hidden
.hidden sym_hidden
sym_hidden:
        nop

.global sym_global
sym_global:
        nop

.global sym_global_abs
.set sym_global_abs, 42

.global sym_weak_undef
.weak sym_weak_undef
```

#### `relr-relocs.ld`
```ld
OUTPUT_ARCH(riscv)
ENTRY(_start)
SECTIONS
{
 PROVIDE (__executable_start = 0x8000); . = 0x10000;
  .text : { *(.before) *(.text) *(.after) } =0
  . = 0x20000;
  .got : { *(.got) *(.got.plt) }
  . = 0x12340000;
  .far : { *(.far) }
 .riscv.attributes 0 : { *(.ARM.atttributes) }
  .data : { *(.data) }
}
```

### Reproduction notes
- This packaged root does not have a single canonical `run.ps1` wrapper.
- Use the reduced inputs under `case/` and follow the commands documented in `case/README.md`.
- Stable witness outputs, when present, are preserved under `verify/run1..run3/`.

### What I checked
- Reduced inputs are preserved under case/.
- Stable witness outputs are preserved under verify/run1..run3/.
- The strict recheck says stable normalized run signatures across three runs: True.
- Tracker guidance link: https://llvm.org/docs/HowToSubmitABug.html
- evidence summary: 3 clean reproductions under hunt/verify/lld_pie_weak_undef_dynamic_relocs_dropped_after_18/run1..run3 have identical combined readelf hashes (`a62c87c00f9e1c32`). In every run, `data.rv64.pie.18.readelf.txt`, `data.rv32.pie.18.readelf.txt`, `got.rv64.pie.18.readelf.txt`, and `got.rv32.pie.18.readelf.txt` keep `sym_weak_undef` in `.rela.dyn`, while the 21/22 counterparts drop it. The same old-versus-current split is reproduced again in the plain non-RELR RV64 controls `data.norelr.rv64.pie.18.readelf.txt` versus `data.norelr.rv64.pie.21.readelf.txt` and `got.norelr.rv64.pie.18.readelf.txt` versus `got.norelr.rv64.pie.21.readelf.txt`, confirming that this is a stable PIE linker regression rather than a one-off RELR formatting artifact.

### Notes
https://sourceware.org/pipermail/binutils/2025-June/141653.html; D:/search_ascii/hunt/cases/confirmed/lld_pie_weak_undef_dynamic_relocs_dropped_after_18/relr-got.s; D:/search_ascii/hunt/cases/confirmed/lld_pie_weak_undef_dynamic_relocs_dropped_after_18/relr-relocs.ld

Root key: `lld.riscv.pie_undefined_weak_absolute_dynamic_relocations_dropped_after_18`
Case id: `20260528-lld-pie-weak-undef-dynamic-relocs-dropped`

</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJzsWd1v4zgO_2vcFyKGrXw0eehD-rXbw-7MIO3M3T4FskXHusqSV5LTZv_6A2U73-nM7AH3dEAxY0nkjyJFUiLDnZMrjXgTjW-j8f0Vb3xp7M2_JDeZ1KtfTZOydHaVGbG5eQLLNUjtDfhSOngrpUKwKJpc6hVwWDw93w2-gZL6FS14dD7nDmN4KYmstkY0OVqQDlzFlYrYHXAt4AlKvkbIEDU4RMLyJYLjFbG5RnnIjXbSedRebYDn1jgHvrRIBLbRLo6S-yiZd_-yYfsHz01Vcbtpp5WIlRKQTuM0nsIrYu2CHIt_NtKigDfkr4NGCywgmiSLJanzbTkZRexxyKJJAmKjeSVzsKhMzr00ZA3IjC8DkOCeB4V--fwCX54eoOCVVBIdKfomfRkW6cM0nkQM_oKa568Di4p7ucZBAHbRJCGOrPHQ7ZmlcRoPAztjcRonwcY7KylccUUiHbgmz9G5olFkqcKjBWFNXbdW5f5Iy5hkx2KjST_U3m6CFIV8TRxGq82-MRYPv81fnr49EDXRLR5-Wwxys0ayX28UB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