-
Notifications
You must be signed in to change notification settings - Fork 15.7k
[RISCV] Disable gp relaxation if part of object unreachable #72655
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -651,6 +651,20 @@ static void relaxHi20Lo12(const InputSection &sec, size_t i, uint64_t loc, | |
| if (!isInt<12>(r.sym->getVA(r.addend) - gp->getVA())) | ||
| return; | ||
|
|
||
| // The symbol may be accessed in multiple pieces. We need to make sure that | ||
| // all of the possible accesses are relaxed or none are. This prevents | ||
| // relaxing the hi relocation and being unable to relax one of the low | ||
| // relocations. The compiler will only access multiple pieces of an object | ||
| // with low relocations on the memory op if the alignment allows it. | ||
| // Therefore it should suffice to check that the smaller of the alignment | ||
| // and size can be reached from GP. | ||
| uint32_t alignAdjust = | ||
| r.sym->getOutputSection() ? r.sym->getOutputSection()->addralign : 0; | ||
| alignAdjust = std::min<uint32_t>(alignAdjust, r.sym->getSize()); | ||
|
||
|
|
||
| if (!isInt<12>(r.sym->getVA() + alignAdjust - gp->getVA())) | ||
|
||
| return; | ||
|
|
||
| switch (r.type) { | ||
| case R_RISCV_HI20: | ||
| // Remove lui rd, %hi20(x). | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| # REQUIRES: riscv | ||
| # RUN: rm -rf %t && split-file %s %t && cd %t | ||
|
|
||
| # RUN: llvm-mc -filetype=obj -triple=riscv32-unknown-elf -mattr=+relax a.s -o rv32.o | ||
| # RUN: llvm-mc -filetype=obj -triple=riscv64-unknown-elf -mattr=+relax a.s -o rv64.o | ||
|
|
||
| # RUN: ld.lld --relax-gp --undefined=__global_pointer$ rv32.o lds -o rv32 | ||
| # RUN: ld.lld --relax-gp --undefined=__global_pointer$ rv64.o lds -o rv64 | ||
| # RUN: llvm-objdump -td -M no-aliases --no-show-raw-insn rv32 | FileCheck %s | ||
| # RUN: llvm-objdump -td -M no-aliases --no-show-raw-insn rv64 | FileCheck %s | ||
|
|
||
| # CHECK: 00000080 l .data {{0+}}08 Var0 | ||
| # CHECK: 00001000 l .data {{0+}}80 Var1 | ||
| # CHECK: 00000815 g .sdata {{0+}}00 __global_pointer$ | ||
|
|
||
| # CHECK: <_start>: | ||
| # CHECK-NOT: lui | ||
| # CHECK-NEXT: lw a0, -1941(gp) | ||
| # CHECK-NEXT: lw a1, -1937(gp) | ||
| # CHECK-NEXT: lui a1, 1 | ||
| # CHECK-NEXT: lw a0, 0(a1) | ||
| # CHECK-NEXT: lw a1, 124(a1) | ||
|
|
||
| #--- a.s | ||
| .global _start | ||
| _start: | ||
| lui a1, %hi(Var0) | ||
| lw a0, %lo(Var0)(a1) | ||
| lw a1, %lo(Var0+4)(a1) | ||
| lui a1, %hi(Var1) | ||
| lw a0, %lo(Var1)(a1) # First part is reachable from gp | ||
| lw a1, %lo(Var1+124)(a1) # The second part is not reachable | ||
|
|
||
| .section .rodata | ||
| foo: | ||
| .space 1 | ||
| .section .sdata,"aw" | ||
| .space 1 | ||
| .section .data,"aw" | ||
| .p2align 3 | ||
| Var0: | ||
| .quad 0 | ||
| .size Var0, 8 | ||
| .space 3960 | ||
| .p2align 7 | ||
| Var1: | ||
| .quad 0 | ||
| .zero 120 | ||
| .size Var1, 128 | ||
|
|
||
| #--- lds | ||
| SECTIONS { | ||
|
||
| .text : { } | ||
| .rodata : { } | ||
| .sdata : { } | ||
| .sbss : { } | ||
| .data : { } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The symbol may be accessed in multiple pieces with different addends.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed. Thanks.