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

    <tr>
        <th>Summary</th>
        <td>
            [lld] [risc-v] absolute values with pcrel relocation cause relocation overflows
        </td>
    </tr>

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

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

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

<pre>
    This test case when run with lld produces a relocation overflow error ðŸ‘Ž 

`
cat > t.c << \!
__thread int a = 0;
typedef unsigned long long size_t;
__attribute__((visibility("hidden"))) extern char __tbss_align[];
size_t foo() { return (size_t)&__tbss_align; }
!

cat > tls.t << \!
MEMORY {
  RAM (rwx) : ORIGIN = 0x80002a50,  LENGTH = ((262144) << 10)
}

SECTIONS {
  text : { *(.text) } > RAM
  data : { *(.data); *(.sdata) } > RAM
  tbss : { *(.tbss) } > RAM
  PROVIDE(__tbss_align = ALIGNOF(tbss));
}
!

clang -target riscv64 -c t.c -fno-asynchronous-unwind-tables -fPIC -mno-relax
ld.lld t.o -T script.t 
`

This produces an error :

ld.lld: error: t.o:(function foo: .text+0x8): relocation R_RISCV_PCREL_HI20 out of range: -524291 is not in [-524288, 524287]; references '__tbss_align'

The same test when tried with GNU linker transforms : auipc to lui, in certain cases converts lui to li as well.

It also appears that GNU linker tries to preserve auipc if certain circumstances when the ORIGIN value for example is 0x900.

The customer in this case is Zephyr, and needs this issue to be fixed. 

</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJx0Vdtu4zYQ_Rr6ZSCBpnyRHvzg-JI1sEkW3nSB9kWgpJHFLk0a5Mhx-vUFKSfrpFtAoHgZzpkznENK79XBIC7Y9I5N1yPZU2fdwnfS_JTuJ44q27wunjvlgdAT1NIjvHRowPUGXhR1oHUDJ2ebvkYPEhxqW0tS1oA9o2u1fQF0zjpgW86KLSvGLN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