Skip to content

Commit 4cb7776

Browse files
Luffy-tsailzshhxx
authored andcommitted
LoongArch: Check PC-relative relocations for shared libraries
Building shared libraries should not be allowed for PC-relative relocations against external symbols. Currently LoongArch has no corresponding checks and silently generates wrong shared libraries. However, In the first version of the medium cmodel, pcalau12i+jirl was used for function calls, in which case PC-relative relocations were allowed.
1 parent 7721dca commit 4cb7776

11 files changed

+80
-0
lines changed

bfd/elfnn-loongarch.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1079,6 +1079,18 @@ loongarch_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
10791079
h->non_got_ref = 1;
10801080
break;
10811081

1082+
/* Since shared library global symbols interpose, any
1083+
PC-relative relocations against external symbols
1084+
should not be used to build shared libraries. */
1085+
case R_LARCH_PCREL20_S2:
1086+
if (bfd_link_pic (info)
1087+
&& (sec->flags & SEC_ALLOC) != 0
1088+
&& (sec->flags & SEC_READONLY) != 0
1089+
&& ! LARCH_REF_LOCAL (info, h))
1090+
return bad_static_reloc (abfd, rel, sec, r_type, h, NULL);
1091+
1092+
break;
1093+
10821094
/* For normal cmodel, pcalau12i + addi.d/w used to data.
10831095
For first version medium cmodel, pcalau12i + jirl are used to
10841096
function call, it need to creat PLT entry for STT_FUNC and
@@ -1096,6 +1108,15 @@ loongarch_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
10961108
h->pointer_equality_needed = 1;
10971109
}
10981110

1111+
/* PC-relative relocations are allowed For first version
1112+
medium cmodel function call. */
1113+
if (h != NULL && !h->needs_plt
1114+
&& bfd_link_pic (info)
1115+
&& (sec->flags & SEC_ALLOC) != 0
1116+
&& (sec->flags & SEC_READONLY) != 0
1117+
&& ! LARCH_REF_LOCAL (info, h))
1118+
return bad_static_reloc (abfd, rel, sec, r_type, h, NULL);
1119+
10991120
break;
11001121

11011122
case R_LARCH_B16:
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#name: PC-relative relocation making shared
2+
#source: bad_pcala_hi20_global.s
3+
#target: [check_shared_lib_support]
4+
#ld: -shared --defsym global_a=0x10 --defsym global_b=0x20
5+
#error: .*: relocation R_LARCH_PCALA_HI20 against `global_b` can not be used when making a shared object; recompile with -fPIC
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.hidden global_a
2+
.text
3+
.align 2
4+
main:
5+
# Symbols defined .hidden are bound local and
6+
# the linker should differenciate them.
7+
la.pcrel $a0, global_a
8+
la.pcrel $a0, global_b
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#name: PC-relative relocation making shared
2+
#source: bad_pcala_hi20_weak.s
3+
#target: [check_shared_lib_support]
4+
#ld: -shared --defsym global_a=0x10 --defsym global_b=0x20
5+
#error: .*: relocation R_LARCH_PCALA_HI20 against `global_b` can not be used when making a shared object; recompile with -fPIC
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
.hidden global_a
2+
.weak global_b
3+
.text
4+
.align 2
5+
main:
6+
# Symbols defined .hidden are bound local and
7+
# the linker should differenciate them.
8+
la.pcrel $a0, global_a
9+
la.pcrel $a0, global_b
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#name: PC-relative relocation making shared
2+
#source: bad_pcrel20_s2_global.s
3+
#target: [check_shared_lib_support]
4+
#ld: -shared --defsym global_a=0x10 --defsym global_b=0x20
5+
#error: .*: relocation R_LARCH_PCREL20_S2 against `global_b` can not be used when making a shared object; recompile with -fPIC
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.hidden global_a
2+
.text
3+
.align 2
4+
main:
5+
# Symbols defined .hidden are bound local and
6+
# the linker should differenciate them.
7+
pcaddi $a0, %pcrel_20(global_a)
8+
pcaddi $a0, %pcrel_20(global_b)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#name: PC-relative relocation making shared
2+
#source: bad_pcrel20_s2_weak.s
3+
#target: [check_shared_lib_support]
4+
#ld: -shared --defsym global_a=0x10 --defsym global_b=0x20
5+
#error: .*: relocation R_LARCH_PCREL20_S2 against `global_b` can not be used when making a shared object; recompile with -fPIC
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
.hidden global_a
2+
.weak global_b
3+
.text
4+
.align 2
5+
main:
6+
# Symbols defined .hidden are bound local and
7+
# the linker should differenciate them.
8+
pcaddi $a0, %pcrel_20(global_a)
9+
pcaddi $a0, %pcrel_20(global_b)

ld/testsuite/ld-loongarch-elf/data-plt.s

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# R_LARCH_PCALA_HI20 only need to generate PLT entry for function symbols.
33
.text
44
.globl a
5+
.hidden a
56

67
.data
78
.align 2

0 commit comments

Comments
 (0)