Skip to content

Commit e493ba6

Browse files
cloudspurslzshhxx
authored andcommitted
LoongArch: Fix linker generate PLT entry for data symbol
With old "medium" code model, we call a function with a pair of PCALAU12I and JIRL instructions. The assembler produces something like: 8: 1a00000c pcalau12i $t0, 0 8: R_LARCH_PCALA_HI20 g c: 4c000181 jirl $ra, $t0, 0 c: R_LARCH_PCALA_LO12 g The linker generates a "PLT entry" for data without any diagnostic. If "g" is a data symbol and ld with -shared option, it may load two instructions in the PLT. Without -shared option, loongarch_elf_adjust_dynamic_symbol can delete PLT entry. For R_LARCH_PCALA_HI20 relocation, linker only generate PLT entry for STT_FUNC and STT_GNU_IFUNC symbols.
1 parent b7a5722 commit e493ba6

File tree

4 files changed

+50
-1
lines changed

4 files changed

+50
-1
lines changed

bfd/elfnn-loongarch.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -891,8 +891,12 @@ loongarch_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
891891
h->non_got_ref = 1;
892892
break;
893893

894+
/* For normal cmodel, pcalau12i + addi.d/w used to data.
895+
For first version medium cmodel, pcalau12i + jirl are used to
896+
function call, it need to creat PLT entry for STT_FUNC and
897+
STT_GNU_IFUNC type symbol. */
894898
case R_LARCH_PCALA_HI20:
895-
if (h != NULL)
899+
if (h != NULL && (STT_FUNC == h->type || STT_GNU_IFUNC == h->type))
896900
{
897901
/* For pcalau12i + jirl. */
898902
h->needs_plt = 1;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# The first version medium codel model function call is: pcalau12i + jirl.
2+
# R_LARCH_PCALA_HI20 only need to generate PLT entry for function symbols.
3+
.text
4+
.globl a
5+
6+
.data
7+
.align 2
8+
.type a, @object
9+
.size a, 4
10+
a:
11+
.word 1
12+
13+
.text
14+
.align 2
15+
.globl test
16+
.type test, @function
17+
test:
18+
pcalau12i $r12,%pc_hi20(a)
19+
ld.w $r12,$r12,%pc_lo12(a)
20+
.size test, .-test

ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,30 @@ if [istarget "loongarch64-*-*"] {
5959
]
6060
}
6161

62+
# loongarch*-elf target do not support -shared option
63+
if [check_shared_lib_support] {
64+
run_ld_link_tests \
65+
[list \
66+
[list \
67+
"data plt" \
68+
"-shared" "" \
69+
"" \
70+
{data-plt.s} \
71+
{} \
72+
"data-plt.so" \
73+
] \
74+
]
75+
76+
if [file exist "tmpdir/data-plt.so"] {
77+
set objdump_output [run_host_cmd "objdump" "-d tmpdir/data-plt.so"]
78+
if { [ regexp "<a@plt>" $objdump_output] } {
79+
fail "data plt"
80+
} {
81+
pass "data plt"
82+
}
83+
}
84+
}
85+
6286
run_ld_link_tests \
6387
[list \
6488
[list \
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
.type func @function
12
pcalau12i $r12, %pc_hi20(func)
23
jirl $r1,$r12, %pc_lo12(func)

0 commit comments

Comments
 (0)