-
Notifications
You must be signed in to change notification settings - Fork 11.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[llvm][MC][ARM][Assembly] Emit relocations for LDRs (#72873)
It's possible (though inadvisable) to use LDR and refer to labels in different sections. In the Arm state, the assembler resolves the LDR instruction without emitting a relocation. That's incorrect because the assembler cannot make any assumptions about the relative position of the sections and the compiler output is therefore wrong. This patch ensures relocations are generated for all `LDR <Rt...>, label` instructions in the Arm state (little endian). This is not necessary when the label is in the same section but the relocation is now generated regardless. Instructions that now generate relocations have been removed from the pcrel-global.s test. Fortunately, LLD already implements the generated relocations and can fix LDR instructions when the symbol is in a different section, or report an error if the offset is too large for the immediate field in the particular LDR's encoding. The patch to address this problem for big endian targets will follow, as well as a fix for ADR that exhibits a similar behavior.
- Loading branch information
1 parent
385304e
commit bbc5d9f
Showing
7 changed files
with
141 additions
and
10 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,42 @@ | ||
@ RUN: llvm-mc -filetype=obj -triple=armv7 %s -o %t | ||
@ RUN: llvm-readelf -r %t | FileCheck %s --check-prefix=ARM | ||
@ RUN: llvm-objdump -d --triple=armv7 %t | FileCheck %s --check-prefix=ARM_ADDEND | ||
|
||
@ ARM: R_ARM_LDRS_PC_G0 | ||
@ ARM: R_ARM_LDRS_PC_G0 | ||
@ ARM: R_ARM_LDRS_PC_G0 | ||
@ ARM: R_ARM_LDRS_PC_G0 | ||
@ ARM: R_ARM_LDRS_PC_G0 | ||
@ ARM: R_ARM_LDRS_PC_G0 | ||
|
||
// The value format is decimal in these specific cases, but it's hex for other | ||
// ldr instructions. These checks are valid for both formats. | ||
|
||
@ ARM_ADDEND: r0, [pc, #-{{(0x)?}}8] | ||
@ ARM_ADDEND: r0, [pc, #-{{(0x)?}}8] | ||
@ ARM_ADDEND: r0, [pc, #-{{(0x)?}}8] | ||
@ ARM_ADDEND: r0, [pc, #-{{16|0x10}}] | ||
@ ARM_ADDEND: r0, [pc, #-{{16|0x10}}] | ||
@ ARM_ADDEND: r0, [pc] | ||
|
||
.arm | ||
.section .text.bar, "ax" | ||
.balign 4 | ||
.global bar | ||
.type bar, %function | ||
bar: | ||
ldrh r0, foo | ||
ldrsb r0, foo | ||
ldrsh r0, foo | ||
ldrh r0, just_after-8 | ||
ldrsb r0, just_after-8 | ||
ldrsh r0, foo+8 | ||
bx lr | ||
|
||
.section .data.foo, "a", %progbits | ||
.balign 4 | ||
.global foo | ||
foo: | ||
.word 0x11223344, 0x55667788 | ||
just_after: | ||
.word 0x9900aabb, 0xccddeeff |
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,46 @@ | ||
@ RUN: llvm-mc -filetype=obj -triple=armv7 %s -o %t | ||
@ RUN: llvm-readelf -r %t | FileCheck %s --check-prefix=ARM | ||
@ RUN: llvm-objdump -d --triple=armv7 %t | FileCheck %s --check-prefix=ARM_ADDEND | ||
@ RUN: llvm-mc -filetype=obj -triple=thumbv7 %s -o %t | ||
@ RUN: llvm-readelf -r %t | FileCheck %s --check-prefix=THUMB | ||
@ RUN: llvm-objdump -d --triple=thumbv7 %t | FileCheck %s --check-prefix=THUMB_ADDEND | ||
|
||
@ ARM: R_ARM_LDR_PC_G0 | ||
@ ARM: R_ARM_LDR_PC_G0 | ||
@ ARM: R_ARM_LDR_PC_G0 | ||
@ ARM: R_ARM_LDR_PC_G0 | ||
|
||
@ ARM_ADDEND: r0, [pc, #-0x8] | ||
@ ARM_ADDEND: r0, [pc, #-0x8] | ||
@ ARM_ADDEND: r0, [pc, #-0x10] | ||
@ ARM_ADDEND: r0, [pc] | ||
|
||
@ THUMB: R_ARM_THM_PC12 | ||
@ THUMB: R_ARM_THM_PC12 | ||
@ THUMB: R_ARM_THM_PC12 | ||
@ THUMB: R_ARM_THM_PC12 | ||
|
||
@ THUMB_ADDEND: r0, [pc, #-0x4] | ||
@ THUMB_ADDEND: r0, [pc, #-0x4] | ||
@ THUMB_ADDEND: r0, [pc, #-0xc] | ||
@ THUMB_ADDEND: r0, [pc, #0x4] | ||
|
||
.section .text.bar, "ax" | ||
.balign 4 | ||
.global bar | ||
.type bar, %function | ||
bar: | ||
ldr r0, foo1 | ||
ldrb r0, foo1 | ||
ldr r0, foo2-8 | ||
ldrb r0, foo1+8 | ||
bx lr | ||
|
||
.section .data.foo, "a", %progbits | ||
.balign 4 | ||
.global foo1 | ||
.global foo2 | ||
foo1: | ||
.word 0x11223344, 0x55667788 | ||
foo2: | ||
.word 0x99aabbcc, 0xddeeff00 |
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,41 @@ | ||
@ RUN: llvm-mc -filetype=obj -triple=thumbv7 %s -o %t | ||
@ RUN: llvm-readelf -r %t | FileCheck %s --check-prefix=THUMB | ||
@ RUN: llvm-objdump -d --triple=thumbv7 %t | FileCheck %s --check-prefix=THUMB_ADDEND | ||
|
||
@ All the ldr variants produce a relocation | ||
@ THUMB: R_ARM_THM_PC12 | ||
@ THUMB: R_ARM_THM_PC12 | ||
@ THUMB: R_ARM_THM_PC12 | ||
@ THUMB: R_ARM_THM_PC12 | ||
@ THUMB: R_ARM_THM_PC12 | ||
@ THUMB: R_ARM_THM_PC12 | ||
|
||
@ THUMB_ADDEND: r0, [pc, #-0x4] | ||
@ THUMB_ADDEND: r0, [pc, #-0x4] | ||
@ THUMB_ADDEND: r0, [pc, #-0x4] | ||
@ THUMB_ADDEND: r0, [pc, #0x4] | ||
@ THUMB_ADDEND: r0, [pc, #-0xc] | ||
@ THUMB_ADDEND: r0, [pc, #0x4] | ||
|
||
.thumb | ||
.section .text.bar, "ax" | ||
.balign 4 | ||
.global bar | ||
.type bar, %function | ||
bar: | ||
ldrh r0, foo1 | ||
ldrsb r0, foo1 | ||
ldrsh r0, foo1 | ||
ldrh r0, foo1+8 | ||
ldrsb r0, foo2-8 | ||
ldrsh r0, foo1+8 | ||
bx lr | ||
|
||
.section .data.foo, "a", %progbits | ||
.balign 4 | ||
.global foo1 | ||
.global foo2 | ||
foo1: | ||
.word 0x11223344, 0x55667788 | ||
foo2: | ||
.word 0x9900aabb, 0xccddeeff |
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