forked from llvm/llvm-project
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ELF][RISCV] Implement --emit-relocs with relaxation
Linker relaxation may change relocations (offsets and types). However, when --emit-relocs is used, relocations are simply copied from the input section causing a mismatch with the corresponding (relaxed) code section. This patch fixes this as follows: for non-relocatable RISC-V binaries, `InputSection::copyRelocations` reads relocations from the relocated section's `relocations` array (since this gets updated by the relaxation code). For all other cases, relocations are read from the input section directly as before. In order to reuse as much code as possible, and to keep the diff small, the original `InputSection::copyRelocations` is changed to accept the relocations as a range of `Relocation` objects. This means that, in the general case when reading from the input section, raw relocations need to be converted to `Relocation`s first, which introduces quite a bit of boiler plate. It also means there's a slight code size increase due to the extra instantiations of `copyRelocations` (for both range types). Reviewed By: MaskRay Differential Revision: https://reviews.llvm.org/D159082
- Loading branch information
1 parent
142a9f5
commit 9a45484
Showing
3 changed files
with
117 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# REQUIRES: riscv | ||
## Test that we can handle --emit-relocs while relaxing. | ||
|
||
# RUN: rm -rf %t && mkdir %t && cd %t | ||
|
||
# RUN: llvm-mc -filetype=obj -triple=riscv32 -mattr=+relax %s -o 32.o | ||
# RUN: ld.lld -Ttext=0x10000 --emit-relocs 32.o -o 32 | ||
# RUN: llvm-objdump -dr --no-show-raw-insn -M no-aliases 32 | FileCheck %s | ||
|
||
# RUN: llvm-mc -filetype=obj -triple=riscv64 -mattr=+relax %s -o 64.o | ||
# RUN: ld.lld -Ttext=0x10000 --emit-relocs 64.o -o 64 | ||
# RUN: llvm-objdump -dr --no-show-raw-insn -M no-aliases 64 | FileCheck %s | ||
|
||
## -r should keep original relocations. | ||
# RUN: ld.lld -r 64.o -o 64.r | ||
# RUN: llvm-objdump -dr --no-show-raw-insn -M no-aliases 64.r | FileCheck %s --check-prefix=CHECKR | ||
|
||
## --no-relax should keep original relocations. | ||
# RUN: ld.lld --emit-relocs --no-relax 64.o -o 64.norelax | ||
# RUN: llvm-objdump -dr --no-show-raw-insn -M no-aliases 64.norelax | FileCheck %s --check-prefix=CHECKNORELAX | ||
|
||
# CHECK: <_start>: | ||
# CHECK-NEXT: jal ra, 0x10008 <f> | ||
# CHECK-NEXT: R_RISCV_JAL f | ||
# CHECK-NEXT: R_RISCV_RELAX *ABS* | ||
# CHECK-NEXT: jal ra, 0x10008 <f> | ||
# CHECK-NEXT: R_RISCV_JAL f | ||
# CHECK-NEXT: R_RISCV_RELAX *ABS* | ||
# CHECK-EMPTY: | ||
# CHECK-NEXT: <f>: | ||
# CHECK-NEXT: jalr zero, 0(ra) | ||
# CHECK-NEXT: R_RISCV_ALIGN *ABS*+0x4 | ||
|
||
# CHECKR: <_start>: | ||
# CHECKR-NEXT: auipc ra, 0 | ||
# CHECKR-NEXT: R_RISCV_CALL_PLT f | ||
# CHECKR-NEXT: R_RISCV_RELAX *ABS* | ||
# CHECKR-NEXT: jalr ra, 0(ra) | ||
# CHECKR-NEXT: auipc ra, 0 | ||
# CHECKR-NEXT: R_RISCV_CALL_PLT f | ||
# CHECKR-NEXT: R_RISCV_RELAX *ABS* | ||
# CHECKR-NEXT: jalr ra, 0(ra) | ||
# CHECKR-NEXT: addi zero, zero, 0 | ||
# CHECKR-NEXT: R_RISCV_ALIGN *ABS*+0x4 | ||
# CHECKR-EMPTY: | ||
# CHECKR-NEXT: <f>: | ||
# CHECKR-NEXT: jalr zero, 0(ra) | ||
|
||
# CHECKNORELAX: <_start>: | ||
# CHECKNORELAX-NEXT: auipc ra, 0 | ||
# CHECKNORELAX-NEXT: R_RISCV_CALL_PLT f | ||
# CHECKNORELAX-NEXT: R_RISCV_RELAX *ABS* | ||
# CHECKNORELAX-NEXT: jalr ra, 16(ra) | ||
# CHECKNORELAX-NEXT: auipc ra, 0 | ||
# CHECKNORELAX-NEXT: R_RISCV_CALL_PLT f | ||
# CHECKNORELAX-NEXT: R_RISCV_RELAX *ABS* | ||
# CHECKNORELAX-NEXT: jalr ra, 8(ra) | ||
# CHECKNORELAX-EMPTY: | ||
# CHECKNORELAX-NEXT: <f>: | ||
# CHECKNORELAX-NEXT: jalr zero, 0(ra) | ||
# CHECKNORELAX-NEXT: R_RISCV_ALIGN *ABS*+0x4 | ||
|
||
.global _start | ||
_start: | ||
call f | ||
call f | ||
.balign 8 | ||
f: | ||
ret |