-
Notifications
You must be signed in to change notification settings - Fork 13.5k
[lld][LoongArch] Relax call36/tail36: R_LARCH_CALL36 #123576
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
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
ea9fea2
Relax PCHi20Lo12.
ylzsx 95f4540
la.pcrel relax test modify.
ylzsx 7b133c2
Add test for PCHi20Lo12
ylzsx abc1a45
Add test for got symbols relaxation.
ylzsx 1b1804e
Modify test. NFC
ylzsx 30cb382
Add check for register.
ylzsx f1f995b
Relax call36/tail36.
ylzsx f227ae5
modify test for call36/tail36.
ylzsx f2aae15
Modify test. Add the option --relax.
ylzsx 924d511
Fixes for reviews.
ylzsx b9c2ea1
Revert "Modify test. Add the option --relax."
ylzsx 1101829
Fixes for reviews.
ylzsx b3900f6
Merge branch 'users/ylzsx/r-pchi20lo12' into users/ylzsx/r-call36
ylzsx a624904
Modify test.
ylzsx 03645ef
Merge branch 'main' into users/ylzsx/r-call36
ylzsx 52bec2b
Add an assert to ensure behavior consistency with ld.bfd.
ylzsx ff1567b
Revert "Add an assert to ensure behavior consistency with ld.bfd."
ylzsx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,65 @@ | ||
# REQUIRES: loongarch | ||
## Relax R_LARCH_CALL36. This test tests boundary cases and some special symbols. | ||
|
||
# RUN: rm -rf %t && split-file %s %t && cd %t | ||
# RUN: llvm-mc -filetype=obj -triple=loongarch64 -mattr=+relax a.s -o a.o | ||
|
||
# RUN: ld.lld -T lds a.o -o a | ||
# RUN: llvm-objdump -d --no-show-raw-insn a | FileCheck %s --check-prefixes=RELAX,RELAX-MID | ||
|
||
## Unsure whether this needs a diagnostic. GNU ld allows this. | ||
# RUN: ld.lld -T lds -pie a.o -o a.pie | ||
# RUN: llvm-objdump -d --no-show-raw-insn a.pie | FileCheck %s --check-prefixes=RELAX,RELAX-MID | ||
|
||
# RUN: ld.lld -T lds -pie -z notext -z ifunc-noplt a.o -o a.ifunc-noplt | ||
# RUN: llvm-objdump -d --no-show-raw-insn a.ifunc-noplt | FileCheck %s --check-prefixes=RELAX,NORELAX-MID | ||
|
||
# RELAX-LABEL: <_start>: | ||
## offset = 0x10000000 - 0x8000000 = 0x8000000(134217728), hi=512, lo18=0 | ||
# RELAX-NEXT: 8000000: pcaddu18i $ra, 512 | ||
# RELAX-NEXT: jirl $ra, $ra, 0 | ||
# RELAX-NEXT: bl 134217720 | ||
# RELAX-NEXT: bl -134217728 | ||
## offset = 12 - 0x8000010 = -0x8000004(-134217732), hi=512, lo18=-4 | ||
# RELAX-NEXT: 8000010: pcaddu18i $ra, -512 | ||
# RELAX-NEXT: jirl $ra, $ra, -4 | ||
# RELAX-EMPTY: | ||
|
||
# RELAX-MID-LABEL: <.mid>: | ||
## offset = 0x8010000 - 0x8008000 = 32768 | ||
# RELAX-MID-NEXT: 8008000: bl 32768 | ||
# RELAX-MID-NEXT: b 32764 | ||
# RELAX-MID-EMPTY: | ||
|
||
# NORELAX-MID-LABEL: <.mid>: | ||
# NORELAX-MID-NEXT: 8008000: pcaddu18i $ra, 0 | ||
# NORELAX-MID-NEXT: jirl $ra, $ra, 0 | ||
# NORELAX-MID-NEXT: pcaddu18i $t0, 0 | ||
# NORELAX-MID-NEXT: jr $t0 | ||
# NORELAX-MID-EMPTY: | ||
|
||
#--- a.s | ||
.global _start, ifunc | ||
_start: | ||
call36 pos # exceed positive range (.text+0x7fffffc), not relaxed | ||
call36 pos # relaxed | ||
call36 neg # relaxed | ||
call36 neg # exceed negative range (.text+16-0x8000000), not relaxed | ||
|
||
.section .mid,"ax",@progbits | ||
.balign 16 | ||
call36 ifunc@plt # enable ifunc, not relaxed | ||
tail36 $t0, ifunc@plt # enable ifunc, not relaxed | ||
|
||
.type ifunc, @gnu_indirect_function | ||
ifunc: | ||
ret | ||
|
||
#--- lds | ||
SECTIONS { | ||
.text 0x8000000 : { *(.text) } | ||
.mid 0x8008000 : { *(.mid) } | ||
.iplt 0x8010000 : { *(.iplt) } | ||
} | ||
neg = 12; | ||
pos = 0x10000000; |
This file contains hidden or 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,136 @@ | ||
# REQUIRES: loongarch | ||
ylzsx marked this conversation as resolved.
Show resolved
Hide resolved
|
||
## Relax R_LARCH_CALL36, which involves the macro instructions call36/tail36. | ||
|
||
# RUN: rm -rf %t && split-file %s %t && cd %t | ||
|
||
# RUN: llvm-mc -filetype=obj -triple=loongarch64 -mattr=+relax a.s -o a.64.o | ||
# RUN: llvm-mc -filetype=obj -triple=loongarch64 -mattr=+relax b.s -o b.64.o | ||
# RUN: ld.lld -shared -soname=b.so b.64.o -o b.64.so | ||
# RUN: ld.lld -T lds a.64.o b.64.so -o 64 | ||
# RUN: llvm-objdump -td --no-show-raw-insn 64 | FileCheck %s --check-prefix=RELAX | ||
|
||
## --no-relax disables relaxation. | ||
# RUN: ld.lld -T lds a.64.o b.64.so --no-relax -o 64.norelax | ||
# RUN: llvm-objdump -td --no-show-raw-insn 64.norelax | FileCheck %s --check-prefix=NORELAX | ||
|
||
# RELAX: {{0*}}00010000 g .text {{0*}}0000001c _start | ||
# RELAX: {{0*}}0001001c g .text {{0*}}00000000 _start_end | ||
# RELAX: {{0*}}00010808 g .mid {{0*}}00000000 mid_end | ||
# RELAX: {{0*}}10010010 g .high {{0*}}00000000 high_end | ||
|
||
# RELAX-LABEL: <_start>: | ||
## offset = 0x10018 - 0x10000 = 24 | ||
# RELAX-NEXT: 10000: bl 24 <a> | ||
# RELAX-NEXT: b 20 <a> | ||
# RELAX-NEXT: nop | ||
# RELAX-NEXT: nop | ||
## offset = .plt(0x10400)+32 - 0x10010 = 1040 | ||
# RELAX-NEXT: 10010: bl 1040 <bar+0x10420> | ||
# RELAX-NEXT: b 1036 <bar+0x10420> | ||
# RELAX-EMPTY: | ||
# RELAX-NEXT: <a>: | ||
# RELAX-NEXT: 10018: ret | ||
# RELAX-EMPTY: | ||
|
||
# RELAX-LABEL: <.mid>: | ||
## offset = 0x10000 - 0x10800 = -2048 | ||
# RELAX-NEXT: 10800: bl -2048 <_start> | ||
# RELAX-NEXT: b -2052 <_start> | ||
# RELAX-EMPTY: | ||
|
||
# RELAX-LABEL: <.mid2>: | ||
## offset = 0x10000 - 0x1010000 = -16777216 | ||
# RELAX-NEXT: 1010000: bl -16777216 <_start> | ||
# RELAX-NEXT: b -16777220 <_start> | ||
# RELAX-EMPTY: | ||
|
||
# RELAX-LABEL: <.high>: | ||
## offset = 0x10000 - 0x10010000 = -0x10000000, hi=-1024, lo18=0 | ||
# RELAX-NEXT: 10010000: pcaddu18i $ra, -1024 | ||
# RELAX-NEXT: jirl $ra, $ra, 0 | ||
# RELAX-NEXT: pcaddu18i $t0, -1024 | ||
# RELAX-NEXT: jirl $zero, $t0, -8 | ||
# RELAX-EMPTY: | ||
|
||
|
||
# NORELAX-LABEL: <_start>: | ||
## offset = 0x10020 - 0x10000 = 0x20, hi=0, lo18=32 | ||
# NORELAX-NEXT: 10000: pcaddu18i $ra, 0 | ||
# NORELAX-NEXT: jirl $ra, $ra, 32 | ||
## offset = 0x10020 - 0x10008 = 0x18, hi=0, lo18=24 | ||
# NORELAX-NEXT: 10008: pcaddu18i $t0, 0 | ||
# NORELAX-NEXT: jirl $zero, $t0, 24 | ||
## offset = .plt(0x10400)+32 - 0x10010 = 0x410, hi=0, lo18=1040 | ||
# NORELAX-NEXT: 10010: pcaddu18i $ra, 0 | ||
# NORELAX-NEXT: jirl $ra, $ra, 1040 | ||
## offset = .plt(0x10400)+32 - 0x10018 = 0x408, hi=0, lo18=1032 | ||
# NORELAX-NEXT: 10018: pcaddu18i $t0, 0 | ||
# NORELAX-NEXT: jirl $zero, $t0, 1032 | ||
# NORELAX-EMPTY: | ||
# NORELAX-NEXT: <a>: | ||
# NORELAX-NEXT: 10020: ret | ||
# NORELAX-EMPTY: | ||
|
||
# NORELAX-LABEL: <.mid>: | ||
## offset = 0x10000 - 0x10800 = -0x800, hi=0, lo18=-2048 | ||
# NORELAX-NEXT: 10800: pcaddu18i $ra, 0 | ||
# NORELAX-NEXT: jirl $ra, $ra, -2048 | ||
# NORELAX-NEXT: pcaddu18i $t0, 0 | ||
# NORELAX-NEXT: jirl $zero, $t0, -2056 | ||
# NORELAX-EMPTY: | ||
|
||
# NORELAX-LABEL: <.mid2>: | ||
## offset = 0x10000 - 0x1010000 = -0x1000000, hi=-64, lo18=0 | ||
# NORELAX-NEXT: 1010000: pcaddu18i $ra, -64 | ||
# NORELAX-NEXT: jirl $ra, $ra, 0 | ||
# NORELAX-NEXT: pcaddu18i $t0, -64 | ||
# NORELAX-NEXT: jirl $zero, $t0, -8 | ||
# NORELAX-EMPTY: | ||
|
||
# NORELAX-LABEL: <.high>: | ||
## offset = 0x10000 - 0x10010000 = -0x10000000, hi=-1024, lo18=0 | ||
# NORELAX-NEXT: 10010000: pcaddu18i $ra, -1024 | ||
# NORELAX-NEXT: jirl $ra, $ra, 0 | ||
# NORELAX-NEXT: pcaddu18i $t0, -1024 | ||
# NORELAX-NEXT: jirl $zero, $t0, -8 | ||
# NORELAX-EMPTY: | ||
|
||
#--- a.s | ||
.global _start, _start_end | ||
_start: | ||
call36 a # relaxed. la64: bl | ||
tail36 $t0, a@plt # relaxed. la64: b | ||
.balign 16 | ||
call36 bar # PLT call36 can be relaxed. la64: bl | ||
tail36 $t0, bar # PLT tail36 can be relaxed. la64: bl | ||
|
||
a: | ||
ret | ||
.size _start, . - _start | ||
_start_end: | ||
|
||
.section .mid,"ax",@progbits | ||
call36 _start@plt # relaxed. la64: bl | ||
tail36 $t0, _start@plt # relaxed. la64: b | ||
|
||
.section .mid2,"ax",@progbits | ||
call36 _start@plt # relaxed. la64: bl | ||
tail36 $t0, _start@plt # relaxed. la64: b | ||
|
||
.section .high,"ax",@progbits | ||
call36 _start@plt # exceed range, not relaxed | ||
tail36 $t0,_start@plt # exceed range, not relaxed | ||
|
||
#--- b.s | ||
.globl bar | ||
bar: | ||
ret | ||
|
||
#--- lds | ||
SECTIONS { | ||
.text 0x10000 : { *(.text) } | ||
.plt 0x10400 : { *(.plt) } | ||
.mid 0x10800 : { *(.mid); mid_end = .; } | ||
.mid2 0x1010000 : { *(.mid2) } | ||
.high 0x10010000 : { *(.high); high_end = .; } | ||
} |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
To match
ld.bfd
behavior we should checkisJirl(nextInsn)
before continuing. In #127312 (comment) I commented that we should probably add stronger checks forld.bfd
too, but let's make the current behavior aligned at first.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.
I believe this is not necessary. Here are the reasons:
It would be greatly appreciated if you could provide an example of an illegal case or present more substantial reasons.
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.
I think it's probably okay to not add more checks, but for
ld.bfd
interoperability the check onjirl
is necessary. If you think this should not be done either, theld.bfd
implementation likely needs change as well.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.
After further consideration and revisiting the manual again(https://github.com/loongson/la-abi-specs/blob/release/laelf.adoc),

the generated
pcaddu18i
andjirl
by GCC and Clang must be adjacent.As for the interoperability you mentioned,
lld
handles instruction sequences generated bygas
. Andgas
should not generate non-contiguous instruction sequences either. Therefore, we think that adding a check inlld
is unnecessary.Additionally, if
jirl
check is added here, other relaxations (such as R_LARCH_{PCALA,GOT_PC}_{HI20,LO12}, etc) would also need to check the instructions, which contradicts the principle of avoiding instruction checks unless absolutely necessary.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.
I'm not objecting to the current revision per se, but rather behavior consistency, no matter whether the input is well-formed or not -- the so-called "bug-for-bug compatibility". This means:
jirl
here in LLD and exactly replicateld.bfd
behavior, orld.bfd
side, with your comment here as justification,so eventually we're going to have 2 consistent implementations of LoongArch ELF psABI and happy users. What do you think here?
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.
We discussed with developers of ld's loongarch port, but they think the check is necessary in ld and lld don't need to align with it.
How about adding an assertion here as a compromise?
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.
Thanks. I have added an assertion to check jirl. 52bec2b
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.
Sorry for the late reply -- I didn't realize "assertion" is just
assert
before.But now debug builds of
lld
will error out on malformedR_LARCH_CALL36
underlying data -- this doesn't align withld.bfd
either because it will simply skip relaxing this piece instead of dying, so I fear the assertion may do more harm than good 🤷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.
@MaskRay Would you agree to adding a check for
jirl
in this situation?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.
I still prefer that we remove the checks. We should avoid assert, when it is actually reachable.
If BFD wants to be guard against malformed input, that's ok. It's fine to diverge from it.