0

For many tasks on RISC-V, a sequence ofauipc and an instruction with a short offset is used for PC-relative addressing (including jumps). This needs a temporary register. The GNU linker is capable of replacing this instruction sequence with one instruction if the offset is short enough. That one instruction does not affect the temporary register.

Thus, the original code sequence and the one that replaces it have slightly different semantics. They perform the same address computation but one leaves some data in the temporary register while the other one does not.

In which cases is the linker allowed to perform such a transformation? If I wrote an auipc instruction myself, followed by another instruction, would the linker be allowed to rewrite these, or would it need special relocation information?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
David Monniaux
  • 1,948
  • 12
  • 23
  • 1
    Apparently, the `call` mnemonic generates a `R_RISCV_CALL` relocation record on the `auipc`, which enables relaxation of that and the subsequent instruction. This `_CALL` relocation does not happen if you write your own `auipc`. – Erik Eidt Feb 04 '23 at 15:48
  • The `call` mnemonic uses `ra` as the temporary register, which is clobbered by the `jalr` in making the function call, so it is scratch at that time. On my system at least, the `tail` mnemonic uses `t1` for temporary, and, when relaxation is applied, `t1` is not loaded with any value, so yes, the semantics have changed for that one, relaxed vs. not. – Erik Eidt Feb 04 '23 at 15:54
  • It appears that the linker doesn't relax an `auipc`/`jalr` sequence written out; it needs the `_CALL` relocation to relax. I don't know how one would get that without `call` or `tail`. What the rules are as to whether it is allowed or not is unknown to me. – Erik Eidt Feb 04 '23 at 16:05
  • Furthermore, it seems that `gas` has a `jump` mnemonic which seems to behave like `tail` except it allows choosing the register to be clobbered. https://stackoverflow.com/questions/75332258/in-risc-v-what-is-the-difference-between-jump-and-tail/75340800#75340800 – David Monniaux Feb 05 '23 at 14:57

0 Answers0