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

    <tr>
        <th>Summary</th>
        <td>
            [LLD][RISCV] LLD RISC-V relaxation bug
        </td>
    </tr>

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

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

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

<pre>
    While investigating #82752, @jwnrt and I found the following bug:

```shell-session
$ cat bug.s
  .text
  .global _start
_start:
 call _start
  .equ x, _start + 4

$ clang -target riscv32 -march=rv32i -mrelax -nostdlib -fuse-ld=lld -o bug bug.s && llvm-readelf -Ws bug

Symbol table '.symtab' contains 5 entries:
   Num:    Value  Size Type    Bind Vis       Ndx Name
     (...)
     3: 000110d4     0 NOTYPE  GLOBAL DEFAULT 1 _start
     4: 000110d4     0 NOTYPE  GLOBAL DEFAULT     1 x
```

The relaxation of the `call` (`auipc` + `jalr`) into just the `jalr` should not affect `x = _start + 4`, but it does. The symbol `x` gets assigned the final address `_start + 0` instead of `_start + 4`.

For completeness, we have the following combinations of `x = _start + offset` requested offsets and generated offsets:

| offset | LLD    | Binutils |
|--------|--------|----------|
|     -1 | -1 - 4 |       -1 |
|      0 | 0 |        0 |
|      1 |  1 - 4 |        1 |
|      4 |  4 - 4 | 4 |
|      5 |  5 - 4 |    5 - 4 |
|      8 |  8 - 4 |        8 |
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJyMVFFvozgQ_jXOywhkDITkgYem2ZxWirqna6-rezoZPIArY2cZk6b3608Gust27-EsJOxvPn8zY89YEunWIpYsP7D8uJGj79xQmlFTL4dvI9Kmcuqt_Nppg6DtFcnrVnptW2Ai3YkiF0zcA8v4y6sdPEir4DM0brQKfIfQOGPca6BXY8vSO8anb8vnjzo0JiIk0s4Gg8iglj6QY2L8DiD2ePPzrDWukgb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