llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[lld] [risc-v] absolute values with pcrel relocation cause relocation overflows

Open shankarke opened this issue 2 years ago • 6 comments

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.

shankarke avatar Jan 16 '24 21:01 shankarke

@llvm/issue-subscribers-backend-risc-v

Author: None (shankarke)

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.

llvmbot avatar Jan 16 '24 22:01 llvmbot

__tbss_align is an absolute symbol of value 4. auipc+addi referencing __tbss_align is out-of-range at a high address around 0x0000000080002a50. GNU ld papers over the problem by converting auipc to lui, which is discouraged. We decide not to support it (https://reviews.llvm.org/D149751).

MaskRay avatar Jan 22 '24 04:01 MaskRay

This is blocking zephyr team from making progress and there are some tests that use this feature from GNU linker as a dependency. Do we have any workaround ?

shankarke avatar Jan 22 '24 20:01 shankarke

Inline assembly? Make it a weak symbol in the C (which will force use of the GOT for medany and PIE code to allow for it to be 0) and use PROVIDE_HIDDEN in the linker script (if you care about visibility)?

jrtc27 avatar Jan 22 '24 22:01 jrtc27

Thanks @MaskRay and @jrtc27 for the pointers and workaround suggestions! Inline asm does work as a workaround for our use case, but I was hoping to ask a few follow-up questions on whether we should consider this as our long-term solution. Generally, I don't have a strong opinion on if or how this should be resolved--I'm just trying to get a feeling for the possible options and whether certain solutions might be accepted into LLVM, lld, etc.

First, to clarify on the original test case: for our real use case (I was working with @shankarke on this) we're building with -mcmodel=medany, -fno-pic/-fno-pie, -static, --no-pie, and both --relax and --no-relax depending on what is being built. Apologies for the confusion here.

My understanding from https://reviews.llvm.org/D149751, https://github.com/riscv-non-isa/riscv-elf-psabi-doc/issues/126, and https://github.com/riscv-non-isa/riscv-elf-psabi-doc/issues/197 is that the case of undefined weak references is generally settled and was not sufficient justification for the auipc -> lui relaxation. But, I'm wondering if there is a (somewhat) meaningful distinction with our case here.

IIUC, for our case where we're explicitly specifying no pic/pie, I feel a bit odd trying to introduce a GOT to workaround this, so we're left with using inline assembly whenever there is a value coming from a linker script that may be out-of-range of the auipc/addi pair. Inline assembly again does work, but this general guideline (edit: just to be clear, I don't mean to say that jrtc27's suggestions were guidelines, but rather that I/@shankarke would put forth this guideline to our users) of using inline assembly in these cases seems a bit unfortunate, especially when migrating from GNU tools which (for better or worse) seem to handle this well enough. I saw the comment from @jrtc27 at https://github.com/ClangBuiltLinux/linux/issues/1409#issuecomment-871787726 which I think sounds similar to what we're bumping into (apologies in advance if I'm misunderstanding though), so I was curious if there was still some interest in a different solution. @jrtc27 have you continued seeing cases similar to this/in your opinion is this a "meaningful" issue worth trying to fix/clarify in LLVM long-term? (Or, in the psABI?)

A second thing, there was a comment from @MaskRay in https://github.com/riscv-non-isa/riscv-elf-psabi-doc/issues/197#issuecomment-1205954486 which I understood as saying that the auipc -> lui relaxation made some sense in the --no-pie/--relax case. @MaskRay, would you be open to allowing the auipc -> lui relaxation in LLD specifically in this situation? I think we roughly mirror what you're describing there besides a) the weak undef part and b) that we care about this transformation for both the --relax and --no-relax cases. I believe you mentioned that this relaxation didn't make sense without --relax/R_RISCV_RELAX though and that you were generally opposed to requiring instruction rewriting for correctness, so we might end up needing to stay with inline assembly anyway.

As a final question, if inline assembly is the proper long-term solution for cases similar to ours (at least, as far as LLVM is concerned), would it make sense for us to document this somewhere in LLVM? I'm assuming we won't be the last people to hit issues similar to this and it would be helpful to have something "formal" to point to when asking others to change their code.

Also, if the psABI issue (https://github.com/riscv-non-isa/riscv-elf-psabi-doc/issues/197) would be a better forum for this, I'm happy to try to revive that thread/continue the dicussion there as well. Thanks!

jonathonpenix avatar Feb 05 '24 22:02 jonathonpenix

IIUC, for our case where we're explicitly specifying no pic/pie, I feel a bit odd trying to introduce a GOT to workaround this, so we're left with using inline assembly whenever there is a value coming from a linker script that may be out-of-range of the auipc/addi pair.

I don't think using GOT is a workaround. It's a proper solution to avoid an out-of-bounds direct reference. If I change ORIGIN in tls.t to be larger than 2**32, I'll get a similar relocation R_AARCH64_ADR_PREL_PG_HI21 out of range for aarch64. aarch32/powerpc/mips may sidestep the problem by using GOT when it is not absolutely necessary.

If you use default visibility for extern char __tbss_align[]; and PIC codegen, the compiler will generate a GOT code sequence. It's your hidden visibility causing the trouble and I am not sure why adding weak to counter the harm of the hidden visibility is a workaround. Adding the auipc to lui relaxation would not only add a questionable behavior, introduce some unneeded complexity, and also encourage the users relying on a wrong behavior. I find it nice that we take time pondering on a feature and ensuring the semantics are ideal, avoiding many issues found with GNU ld in many ports including RISC-V.

MaskRay avatar Feb 06 '24 08:02 MaskRay