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

    <tr>
        <th>Summary</th>
        <td>
            Missing Linker Relaxation Relocations after Assembly Relocation
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            backend:RISC-V,
            mc
      </td>
    </tr>

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

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

<pre>
    The following example, when assembled with `llvm-mc --triple=riscv32 -mattr=+relax,+experimental-xqcilb` seems to have a bug:

```
.global foo

bar:
  jal x1, foo
  bne a0, a1, bar
  ret
```

Looking at the `llvm-objdump -dr` output (this has not been truncated)

```
jal.o:  file format elf32-littleriscv

Disassembly of section .text:

00000000 <bar>:
       0: c01f 0000 0000        qc.e.jal        0x0 <bar>
 00000000:  R_RISCV_VENDOR       QUALCOMM
 00000000:  R_RISCV_CUSTOM195    foo
                        00000000: R_RISCV_RELAX        *ABS*
       6: feb51de3      bne     a0, a1, 0x0 <bar>
       a: 8082          ret
```

The `bne` is branching over a relaxable instruction, but has been resolved instead of having a relocation on it. This means that if the linker relaxes the `qc.e.jal` instruction, the `bne` will be incorrect.

I think the sequence of what is happening is:
- `jal x1, foo` is parsed as `JAL`, as would be expected.
- `JAL` is compressed to `C_JAL` in `RISCVAsmParser::emitToStreamer`
- `C_JAL` is emitted, with a `RISCV::fixup_riscv_rvc_jump` fixup, which is not marked as linker relaxable.
- `C_JAL` is jumping to an undefined symbol, so will be changed with `relaxInstruction`
- `relaxInstruction` first relaxes to `JAL` and then the second time to `QC_E_JAL`
- `QC_E_JAL` has a `RISCV::fixup_riscv_qc_e_call_plt` fixup, which is marked as linker relaxable!
- nothing now marks the fragment and the section as linker relaxable.
- When it comes time to apply the fixup to `bne`, I think `RISCVAsmBackend::isPCRelFixupResolved` is returning that the fixup is fully resolved, when it shouldn't be.

I think the relaxation of `JAL` to `QC_E_JAL` might be the only one we have right now which changes the fixup from being non-linker-relaxable to being linker-relaxable. (This is not going to be the case indefinitely, as there are now linker relaxations for `R_RISCV_JAL`, so the `C_JAL` to `JAL` relaxation will have the same bug once we update `RISCVMCCodeEmitter::getImmOpValue` for new relaxations in the psABI since we did this last time)

I think this is still violating invariants of the code, that fragments (and their parent sections) with linker relaxable fixups should be marked as linker relaxable.

</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJx8VlFT47oO_jXpi6ad1KFAH_pQCsxwB-6eUzjc-9ZxEqUxOHawlbb8-zOyk7bssuvpQGvLsvR9n2RL79XWIC6S2U0yux3JjmrrFhqNdJ-j3Jafi5caobJa270yW8CDbFqNiVjBvkYD0ntsco0l7BXVkFymWu-acVPAeExOsWl265QvdpmAcSOJXJLdJuLGoZaHRKwScYOHFp1q0JDU48NHoXSeXKbgERsPZKGWOwQJebdNsmWShs9l2n_S5WSrbS41VNbGxVy6aAjwJjUcphxtXAXIDYJMeUaGeTbmeYf0k98kXT5a-85ZSwKq8Zidzd_KrmlhXDoO1HbUdgSJuKZaeailB2MJckQD5DpTSMIyEfNfQ3-TemKTbAlQKc0wu0YSoK4yMdaKSGOALm68Vb5H-xNsBR4LUtbAhPBAR2DSfkCSrQIOdwMUYaR8WJFOKwhG4U8_PooJThivwfRw7iRdwuA6xLverB-eV6-b17v_3v5Y91v-_mf5uPrx9PRb69U_zy8_nqbzGVsPjHw_zvcP29d3j8v_DwaJWC5vnhNxlt0lG1eYz6YlZnGK6eZxTvkvmcUhefd1ei1OQXwripeohNwgk6885E6aomad2B06kBC0LXONoIwn1wWigtg6CvII0nDord5hGYxQlkxqLXdBb-zCFjIQbA0omsALa6tBaTxQLQlUFTSplXlHF49EP8h0IDNE-DUG-hL-XmkNOQdaWOewoElM8gGoVuY9WHv86NAUyAHuw9Es8rZFw7EqHxU2Zq9f6y3C00rnsQTp2eA_y0cGkrnwsLedLvl07gAFYTkZ_EQz3l3YpnXo2QNZXl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