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

    <tr>
        <th>Summary</th>
        <td>
            [RISC-V][lld] ld.lld local IFUNC PIE data references emit spurious relative
        </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, 21.1.3, and 22.1.0 inject a spurious `R_RISCV_RELATIVE` for local IFUNC data materializations in PIE mode: `ifunc-reloc-data` links with one `R_RISCV_RELATIVE` plus one `R_RISCV_IRELATIVE`, and `ifunc-overwrite` links with one `R_RISCV_RELATIVE` plus three `R_RISCV_IRELATIVE` entries.

### Expected behavior
Link local GNU IFUNC definitions through the official GNU PIE data-address samples `ifunc-reloc-data` and `ifunc-overwrite`, where the data references must stay on the IFUNC relocation protocol.

### Environment
- linker route: llvm-mc plus ld.lld version replay with official GNU ld PIE IFUNC controls
- march: rv64i
- mabi: lp64
- first failing stage: link
- local stability check: True

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

#### `ifunc-overwrite.s`
```asm
        .text

        .type   foo_resolver, @function
foo_resolver:
        ret
        .size   foo_resolver, .-foo_resolver

        .globl  foo1
        .type   foo1, %gnu_indirect_function
        .set    foo1, foo_resolver

        .globl  foo2
        .type   foo2, %gnu_indirect_function
        .set    foo2, foo_resolver

        .globl  foo3
        .type   foo3, %gnu_indirect_function
        .set    foo3, foo_resolver

        .globl  bar
        .type   bar, @function
bar:
.L1:
        auipc   x1, %got_pcrel_hi (foo1)
.ifdef __64_bit__
        ld      x1, %pcrel_lo (.L1) (x1)
.else
        lw      x1, %pcrel_lo (.L1) (x1)
.endif

        call    foo1
        call    foo1@plt

.L2:
        auipc   x2, %got_pcrel_hi (foo2)
.ifdef __64_bit__
        ld      x2, %pcrel_lo (.L2) (x2)
.else
        lw      x2, %pcrel_lo (.L2) (x2)
.endif
        ret
        .size   bar, .-bar

        .data
foo3_addr:
.ifdef __64_bit__
        .quad   foo3
.else
        .long   foo3
.endif
```

#### `ifunc-reloc-data.s`
```asm
        .text

        .type   foo_resolver, @function
foo_resolver:
        ret
        .size   foo_resolver, .-foo_resolver

        .globl  foo
        .type   foo, %gnu_indirect_function
        .set    foo, foo_resolver

        .globl  bar
        .type   bar, @function
bar:
.L1:
        auipc   x1, %pcrel_hi (foo_addr)
.ifdef __64_bit__
        ld      x1, %pcrel_lo (.L1) (x1)
.else
        lw      x1, %pcrel_lo (.L1) (x1)
.endif
        ret
        .size   bar, .-bar

        .data
foo_addr:
.ifdef __64_bit__
        .quad   foo
.else
        .long   foo
.endif
```

### 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_local_ifunc_pie_data_spurious_relative/run1..run3 show stable hashes for `data.pie.21.relocs.txt`; every run contains the same extra `R_RISCV_RELATIVE` before the IFUNC relocation. The copied GNU controls `ifunc-reloc-data-pie.rd` and `ifunc-overwrite-pie.rd` require only IFUNC-protocol relocations, with no extra `R_RISCV_RELATIVE` data relocation for the local IFUNC address materialization.

### Notes
upstream///binutils-gdb/ld/testsuite/ld-riscv-elf/ifunc-reloc-data.s; D:/search_ascii/upstream///binutils-gdb/ld/testsuite/ld-riscv-elf/ifunc-overwrite.s; D:/search_ascii/upstream///binutils-gdb/ld/testsuite/ld-riscv-elf/ifunc-reloc-data-pie.rd; D:/search_ascii/upstream///binutils-gdb/ld/testsuite/ld-riscv-elf/ifunc-overwrite-pie.rd

Root key: `lld.riscv.local_ifunc_pie_data_references_emit_spurious_relative`
Case id: `20260528-lld-local-ifunc-pie-data-spurious-relative`

</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJzUWFtv2zoS_jXyC2FBlmwnfvBD0sR7AnSLRZp2902gyJE1JxSpw4sT99cvRhdfUrlt9r6A0Qs1nMs3nOF85M7hVgOso8VttLib8OArY9d_Q24K1NvfTJils9WkMHK_fmCWa4baG-YrdOylQgXMggwC9ZZx9vjw-cP0K1Oon8EyD84L7iBmTxWJNdbIIMAydMzVXKko_cC4luyBVXwHrADQzAGQLl8Bc7ymbS4oz4TRDp0H7dWecWGNc8xXFkjABu3iKLmLkpv-zzTrfuxzqGtu992ykrFSks2u41l8TbbTWTyLs8GLNI1nccJQ_w7CM85cEyya4Fi0TB5zCu1r_nj_8ebp4et9tExYaSxTRnDFHjZfPn1gknvOau7BIlf4jXs02jHU7C8P96w2EqLshnRhGbSYWlBGTGkP6SLEHHtBXzGj4ZLFRgX39vvDicAQycGI2YF9sejhnTY6ZC9YYaC9RbgE-f1rA8KDZAVUfIfGdgIfUT_3cP3p05cBMihRYweUr6wJ26rNvClLFNiLEnyE05RLacE5OheNAncJy4sIEDwvFVhobbTpslCCBS3AsTo4z5zne2Z0K9C52Opuc8kaa7wRRl0KXO_QGl2D9t2n6VAI1gTfZl-pXT2tRQdyfxx3YB1pt9Aovu_zcxq_ki0EnTvCaG-NcoOFmltRkWq7W87xuFpga69Zzoe1Eq3zrOSoqL6c59vOJdTPB3fb9DjPC1To90xUIJ5J6MkGGA_6kYof5KHWu-9PFThgvEfa9jKom-BZiZS70pq6_Vijxhq_gTxtEA8sOJCdP2ofZTejxsn-94mOHaW6k1wm3Y-7etCwij28-jN9tLZvIEpWpTG5BWfUDiydlmiekHLKfid7JnB0a2XBH5U5_DamLJ6erbzxYKtMobpdszG_Z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