Skip to content

Commit 41a47d8

Browse files
committed
LoongArch: common sync from upstream
1 parent 4e73a0b commit 41a47d8

15 files changed

Lines changed: 2283 additions & 0 deletions

debian/changelog

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
binutils (2.41-6deepin11) unstable; urgency=medium
2+
3+
* LoongArch: common sync from upstream
4+
5+
-- Peng Fan <fanpeng@loongson.cn> Mon, 24 Nov 2025 01:50:41 +0000
6+
17
binutils (2.41-6deepin10) unstable; urgency=medium
28

39
* fix CVE-2024-57360 CVE-2024-53589 CVE-2025-0840 CVE-2025-1176 CVE-2025-1178
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
From 4b10a30d6d02675892fd0220fa1d722d9f342359 Mon Sep 17 00:00:00 2001
2+
From: WANG Xuerui <git@xen0n.name>
3+
Date: Sat, 19 Oct 2024 22:11:52 +0800
4+
Subject: [PATCH 01/12] LoongArch: Default to a maximum page size of 64KiB
5+
6+
As per the spec (Section 7.5.10, LoongArch Reference Manual Vol. 1),
7+
LoongArch machines are not limited in page size choices, and currently
8+
page sizes of 4KiB, 16KiB and 64KiB are supported by mainline Linux.
9+
While 16KiB is the most common, the current BFD code says it is the
10+
maximum; this is not correct, and as an effect, almost all existing
11+
binaries are incompatible with a 64KiB kernel because the sections are
12+
not sufficiently aligned, while being totally fine otherwise.
13+
This is needlessly complicating integration testing [1].
14+
15+
This patch fixes the inconsistency, and also brings BFD behavior in line
16+
with that of LLD [2].
17+
18+
[1] https://github.com/loongson-community/discussions/issues/47
19+
[2] https://github.com/llvm/llvm-project/blob/llvmorg-19.1.0/lld/ELF/Arch/LoongArch.cpp#L174-L183
20+
21+
bfd/
22+
* elfnn-loongarch.c (ELF_MAXPAGESIZE): Bump to 64KiB.
23+
(ELF_MINPAGESIZE): Define as 4KiB.
24+
(ELF_COMMONPAGESIZE): Define as 16KiB.
25+
26+
ld/
27+
* testsuite/ld-loongarch-elf/64_pcrel.d: Update assertions after
28+
changing the target max page size to 64KiB.
29+
* testsuite/ld-loongarch-elf/data-got.d: Likewise.
30+
* testsuite/ld-loongarch-elf/desc-relex.d: Likewise.
31+
* testsuite/ld-loongarch-elf/relax-align-ignore-start.d: Likewise.
32+
* testsuite/ld-loongarch-elf/tlsdesc_abs.d: Make the fuzzy match work
33+
as intended by not checking exact instruction words.
34+
* testsuite/ld-loongarch-elf/tlsdesc_extreme.d: Likewise.
35+
36+
Signed-off-by: WANG Xuerui <git@xen0n.name>
37+
---
38+
bfd/elfnn-loongarch.c | 4 +++-
39+
ld/testsuite/ld-loongarch-elf/64_pcrel.d | 2 +-
40+
ld/testsuite/ld-loongarch-elf/data-got.d | 2 +-
41+
ld/testsuite/ld-loongarch-elf/desc-relax.d | 2 +-
42+
.../ld-loongarch-elf/relax-align-ignore-start.d | 2 +-
43+
ld/testsuite/ld-loongarch-elf/tlsdesc_abs.d | 12 ++++++------
44+
ld/testsuite/ld-loongarch-elf/tlsdesc_extreme.d | 8 ++++----
45+
7 files changed, 17 insertions(+), 15 deletions(-)
46+
47+
diff --git a/bfd/elfnn-loongarch.c b/bfd/elfnn-loongarch.c
48+
index 13216ef9..8dfb5ea1 100644
49+
--- a/bfd/elfnn-loongarch.c
50+
+++ b/bfd/elfnn-loongarch.c
51+
@@ -6171,7 +6171,9 @@ elf_loongarch64_hash_symbol (struct elf_link_hash_entry *h)
52+
#define ELF_ARCH bfd_arch_loongarch
53+
#define ELF_TARGET_ID LARCH_ELF_DATA
54+
#define ELF_MACHINE_CODE EM_LOONGARCH
55+
-#define ELF_MAXPAGESIZE 0x4000
56+
+#define ELF_MINPAGESIZE 0x1000
57+
+#define ELF_MAXPAGESIZE 0x10000
58+
+#define ELF_COMMONPAGESIZE 0x4000
59+
#define bfd_elfNN_bfd_reloc_type_lookup loongarch_reloc_type_lookup
60+
#define bfd_elfNN_bfd_link_hash_table_create \
61+
loongarch_elf_link_hash_table_create
62+
diff --git a/ld/testsuite/ld-loongarch-elf/64_pcrel.d b/ld/testsuite/ld-loongarch-elf/64_pcrel.d
63+
index 2ea063b7..1f4664ea 100644
64+
--- a/ld/testsuite/ld-loongarch-elf/64_pcrel.d
65+
+++ b/ld/testsuite/ld-loongarch-elf/64_pcrel.d
66+
@@ -1,4 +1,4 @@
67+
#...
68+
-.*0xffffbffc.*
69+
+.*0xfffefffc.*
70+
.*0xffffffff.*
71+
#pass
72+
diff --git a/ld/testsuite/ld-loongarch-elf/data-got.d b/ld/testsuite/ld-loongarch-elf/data-got.d
73+
index a754478a..4a26c72d 100644
74+
--- a/ld/testsuite/ld-loongarch-elf/data-got.d
75+
+++ b/ld/testsuite/ld-loongarch-elf/data-got.d
76+
@@ -8,7 +8,7 @@
77+
#skip: loongarch32-*-*
78+
79+
#...
80+
- GNU_RELRO 0x003c10 0x0000000000007c10 0x0000000000007c10 0x0003f0 0x0003f0 R 0x1
81+
+ GNU_RELRO 0x00fc10 0x000000000001fc10 0x000000000001fc10 0x0003f0 0x0003f0 R 0x1
82+
#...
83+
01 .dynamic .got .got.plt .data
84+
#...
85+
diff --git a/ld/testsuite/ld-loongarch-elf/desc-relax.d b/ld/testsuite/ld-loongarch-elf/desc-relax.d
86+
index c885953c..48f2d1e3 100644
87+
--- a/ld/testsuite/ld-loongarch-elf/desc-relax.d
88+
+++ b/ld/testsuite/ld-loongarch-elf/desc-relax.d
89+
@@ -9,7 +9,7 @@
90+
Disassembly of section .text:
91+
92+
0+188 <.*>:
93+
- 188: 18020844 pcaddi \$a0, 4162
94+
+ 188: 18080844 pcaddi \$a0, 16450
95+
18c: 28c00081 ld.d \$ra, \$a0, 0
96+
190: 4c000021 jirl \$ra, \$ra, 0
97+
194: 0010888c add.d \$t0, \$a0, \$tp
98+
diff --git a/ld/testsuite/ld-loongarch-elf/relax-align-ignore-start.d b/ld/testsuite/ld-loongarch-elf/relax-align-ignore-start.d
99+
index 939cf427..70763ff0 100644
100+
--- a/ld/testsuite/ld-loongarch-elf/relax-align-ignore-start.d
101+
+++ b/ld/testsuite/ld-loongarch-elf/relax-align-ignore-start.d
102+
@@ -12,5 +12,5 @@ Disassembly of section bbb:
103+
0000000120000080 <bbb>:
104+
[ ]+120000080:[ ]+4c000020[ ]+ret
105+
Disassembly of section ccc:
106+
-0000000120000090 <__bss_start-0x4004>:
107+
+0000000120000090 <__bss_start-0x10004>:
108+
[ ]+120000090:[ ]+4c000020[ ]+ret
109+
diff --git a/ld/testsuite/ld-loongarch-elf/tlsdesc_abs.d b/ld/testsuite/ld-loongarch-elf/tlsdesc_abs.d
110+
index 62f5a2a0..7e23454b 100644
111+
--- a/ld/testsuite/ld-loongarch-elf/tlsdesc_abs.d
112+
+++ b/ld/testsuite/ld-loongarch-elf/tlsdesc_abs.d
113+
@@ -9,15 +9,15 @@
114+
Disassembly of section .text:
115+
116+
0+120000100 <.*>:
117+
- 120000100: 14400084 lu12i.w \$a0, .*
118+
- 120000104: 03850084 ori \$a0, \$a0, .*
119+
- 120000108: 16000024 lu32i.d \$a0, .*
120+
+ 120000100: ........ lu12i.w \$a0, .*
121+
+ 120000104: ........ ori \$a0, \$a0, .*
122+
+ 120000108: ........ lu32i.d \$a0, .*
123+
12000010c: 03000084 lu52i.d \$a0, \$a0, 0
124+
120000110: 28c00081 ld.d \$ra, \$a0, 0
125+
120000114: 4c000021 jirl \$ra, \$ra, 0
126+
- 120000118: 14400084 lu12i.w \$a0, .*
127+
- 12000011c: 03850084 ori \$a0, \$a0, .*
128+
- 120000120: 16000024 lu32i.d \$a0, .*
129+
+ 120000118: ........ lu12i.w \$a0, .*
130+
+ 12000011c: ........ ori \$a0, \$a0, .*
131+
+ 120000120: ........ lu32i.d \$a0, .*
132+
120000124: 03000084 lu52i.d \$a0, \$a0, 0
133+
120000128: 28c00081 ld.d \$ra, \$a0, 0
134+
12000012c: 4c000021 jirl \$ra, \$ra, 0
135+
diff --git a/ld/testsuite/ld-loongarch-elf/tlsdesc_extreme.d b/ld/testsuite/ld-loongarch-elf/tlsdesc_extreme.d
136+
index 55179997..fbc1d945 100644
137+
--- a/ld/testsuite/ld-loongarch-elf/tlsdesc_extreme.d
138+
+++ b/ld/testsuite/ld-loongarch-elf/tlsdesc_extreme.d
139+
@@ -9,15 +9,15 @@
140+
Disassembly of section .text:
141+
142+
0+120000100 <.*>:
143+
- 120000100: 1a000084 pcalau12i \$a0, .*
144+
- 120000104: 02c52001 li.d \$ra, .*
145+
+ 120000100: ........ pcalau12i \$a0, .*
146+
+ 120000104: ........ li.d \$ra, .*
147+
120000108: 16000001 lu32i.d \$ra, 0
148+
12000010c: 03000021 lu52i.d \$ra, \$ra, 0
149+
120000110: 00108484 add.d \$a0, \$a0, \$ra
150+
120000114: 28c00081 ld.d \$ra, \$a0, 0
151+
120000118: 4c000021 jirl \$ra, \$ra, 0
152+
- 12000011c: 1a000084 pcalau12i \$a0, .*
153+
- 120000120: 02c5200d li.d \$t1, .*
154+
+ 12000011c: ........ pcalau12i \$a0, .*
155+
+ 120000120: ........ li.d \$t1, .*
156+
120000124: 1600000d lu32i.d \$t1, 0
157+
120000128: 030001ad lu52i.d \$t1, \$t1, 0
158+
12000012c: 0010b484 add.d \$a0, \$a0, \$t1
159+
--
160+
2.47.3
161+
Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
From d63d442331671ff2e1a747d0d4bcf58d3cbe8802 Mon Sep 17 00:00:00 2001
2+
From: Lulu Cai <cailulu@loongson.cn>
3+
Date: Mon, 30 Sep 2024 16:08:59 +0800
4+
Subject: [PATCH 02/12] LoongArch: Check PC-relative relocations for shared
5+
libraries
6+
7+
Building shared libraries should not be allowed for PC-relative
8+
relocations against external symbols.
9+
Currently LoongArch has no corresponding checks and silently
10+
generates wrong shared libraries.
11+
12+
However, In the first version of the medium cmodel, pcalau12i+jirl was
13+
used for function calls, in which case PC-relative relocations were
14+
allowed.
15+
---
16+
bfd/elfnn-loongarch.c | 21 +++++++++++++++++++
17+
.../ld-loongarch-elf/bad_pcala_hi20_global.d | 5 +++++
18+
.../ld-loongarch-elf/bad_pcala_hi20_global.s | 8 +++++++
19+
.../ld-loongarch-elf/bad_pcala_hi20_weak.d | 5 +++++
20+
.../ld-loongarch-elf/bad_pcala_hi20_weak.s | 9 ++++++++
21+
.../ld-loongarch-elf/bad_pcrel20_s2_global.d | 5 +++++
22+
.../ld-loongarch-elf/bad_pcrel20_s2_global.s | 8 +++++++
23+
.../ld-loongarch-elf/bad_pcrel20_s2_weak.d | 5 +++++
24+
.../ld-loongarch-elf/bad_pcrel20_s2_weak.s | 9 ++++++++
25+
ld/testsuite/ld-loongarch-elf/data-plt.s | 1 +
26+
.../ld-loongarch-elf/ld-loongarch-elf.exp | 4 ++++
27+
11 files changed, 80 insertions(+)
28+
create mode 100644 ld/testsuite/ld-loongarch-elf/bad_pcala_hi20_global.d
29+
create mode 100644 ld/testsuite/ld-loongarch-elf/bad_pcala_hi20_global.s
30+
create mode 100644 ld/testsuite/ld-loongarch-elf/bad_pcala_hi20_weak.d
31+
create mode 100644 ld/testsuite/ld-loongarch-elf/bad_pcala_hi20_weak.s
32+
create mode 100644 ld/testsuite/ld-loongarch-elf/bad_pcrel20_s2_global.d
33+
create mode 100644 ld/testsuite/ld-loongarch-elf/bad_pcrel20_s2_global.s
34+
create mode 100644 ld/testsuite/ld-loongarch-elf/bad_pcrel20_s2_weak.d
35+
create mode 100644 ld/testsuite/ld-loongarch-elf/bad_pcrel20_s2_weak.s
36+
37+
diff --git a/bfd/elfnn-loongarch.c b/bfd/elfnn-loongarch.c
38+
index 8dfb5ea1..46fd1a11 100644
39+
--- a/bfd/elfnn-loongarch.c
40+
+++ b/bfd/elfnn-loongarch.c
41+
@@ -1079,6 +1079,18 @@ loongarch_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
42+
h->non_got_ref = 1;
43+
break;
44+
45+
+ /* Since shared library global symbols interpose, any
46+
+ PC-relative relocations against external symbols
47+
+ should not be used to build shared libraries. */
48+
+ case R_LARCH_PCREL20_S2:
49+
+ if (bfd_link_pic (info)
50+
+ && (sec->flags & SEC_ALLOC) != 0
51+
+ && (sec->flags & SEC_READONLY) != 0
52+
+ && ! LARCH_REF_LOCAL (info, h))
53+
+ return bad_static_reloc (abfd, rel, sec, r_type, h, NULL);
54+
+
55+
+ break;
56+
+
57+
/* For normal cmodel, pcalau12i + addi.d/w used to data.
58+
For first version medium cmodel, pcalau12i + jirl are used to
59+
function call, it need to creat PLT entry for STT_FUNC and
60+
@@ -1096,6 +1108,15 @@ loongarch_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
61+
h->pointer_equality_needed = 1;
62+
}
63+
64+
+ /* PC-relative relocations are allowed For first version
65+
+ medium cmodel function call. */
66+
+ if (h != NULL && !h->needs_plt
67+
+ && bfd_link_pic (info)
68+
+ && (sec->flags & SEC_ALLOC) != 0
69+
+ && (sec->flags & SEC_READONLY) != 0
70+
+ && ! LARCH_REF_LOCAL (info, h))
71+
+ return bad_static_reloc (abfd, rel, sec, r_type, h, NULL);
72+
+
73+
break;
74+
75+
case R_LARCH_B16:
76+
diff --git a/ld/testsuite/ld-loongarch-elf/bad_pcala_hi20_global.d b/ld/testsuite/ld-loongarch-elf/bad_pcala_hi20_global.d
77+
new file mode 100644
78+
index 00000000..6ecefd1d
79+
--- /dev/null
80+
+++ b/ld/testsuite/ld-loongarch-elf/bad_pcala_hi20_global.d
81+
@@ -0,0 +1,5 @@
82+
+#name: PC-relative relocation making shared
83+
+#source: bad_pcala_hi20_global.s
84+
+#target: [check_shared_lib_support]
85+
+#ld: -shared --defsym global_a=0x10 --defsym global_b=0x20
86+
+#error: .*: relocation R_LARCH_PCALA_HI20 against `global_b` can not be used when making a shared object; recompile with -fPIC
87+
diff --git a/ld/testsuite/ld-loongarch-elf/bad_pcala_hi20_global.s b/ld/testsuite/ld-loongarch-elf/bad_pcala_hi20_global.s
88+
new file mode 100644
89+
index 00000000..d8189e45
90+
--- /dev/null
91+
+++ b/ld/testsuite/ld-loongarch-elf/bad_pcala_hi20_global.s
92+
@@ -0,0 +1,8 @@
93+
+ .hidden global_a
94+
+ .text
95+
+ .align 2
96+
+main:
97+
+ # Symbols defined .hidden are bound local and
98+
+ # the linker should differenciate them.
99+
+ la.pcrel $a0, global_a
100+
+ la.pcrel $a0, global_b
101+
diff --git a/ld/testsuite/ld-loongarch-elf/bad_pcala_hi20_weak.d b/ld/testsuite/ld-loongarch-elf/bad_pcala_hi20_weak.d
102+
new file mode 100644
103+
index 00000000..cefc42cf
104+
--- /dev/null
105+
+++ b/ld/testsuite/ld-loongarch-elf/bad_pcala_hi20_weak.d
106+
@@ -0,0 +1,5 @@
107+
+#name: PC-relative relocation making shared
108+
+#source: bad_pcala_hi20_weak.s
109+
+#target: [check_shared_lib_support]
110+
+#ld: -shared --defsym global_a=0x10 --defsym global_b=0x20
111+
+#error: .*: relocation R_LARCH_PCALA_HI20 against `global_b` can not be used when making a shared object; recompile with -fPIC
112+
diff --git a/ld/testsuite/ld-loongarch-elf/bad_pcala_hi20_weak.s b/ld/testsuite/ld-loongarch-elf/bad_pcala_hi20_weak.s
113+
new file mode 100644
114+
index 00000000..73c6ec5f
115+
--- /dev/null
116+
+++ b/ld/testsuite/ld-loongarch-elf/bad_pcala_hi20_weak.s
117+
@@ -0,0 +1,9 @@
118+
+ .hidden global_a
119+
+ .weak global_b
120+
+ .text
121+
+ .align 2
122+
+main:
123+
+ # Symbols defined .hidden are bound local and
124+
+ # the linker should differenciate them.
125+
+ la.pcrel $a0, global_a
126+
+ la.pcrel $a0, global_b
127+
diff --git a/ld/testsuite/ld-loongarch-elf/bad_pcrel20_s2_global.d b/ld/testsuite/ld-loongarch-elf/bad_pcrel20_s2_global.d
128+
new file mode 100644
129+
index 00000000..8e063dfb
130+
--- /dev/null
131+
+++ b/ld/testsuite/ld-loongarch-elf/bad_pcrel20_s2_global.d
132+
@@ -0,0 +1,5 @@
133+
+#name: PC-relative relocation making shared
134+
+#source: bad_pcrel20_s2_global.s
135+
+#target: [check_shared_lib_support]
136+
+#ld: -shared --defsym global_a=0x10 --defsym global_b=0x20
137+
+#error: .*: relocation R_LARCH_PCREL20_S2 against `global_b` can not be used when making a shared object; recompile with -fPIC
138+
diff --git a/ld/testsuite/ld-loongarch-elf/bad_pcrel20_s2_global.s b/ld/testsuite/ld-loongarch-elf/bad_pcrel20_s2_global.s
139+
new file mode 100644
140+
index 00000000..39cedbd2
141+
--- /dev/null
142+
+++ b/ld/testsuite/ld-loongarch-elf/bad_pcrel20_s2_global.s
143+
@@ -0,0 +1,8 @@
144+
+ .hidden global_a
145+
+ .text
146+
+ .align 2
147+
+main:
148+
+ # Symbols defined .hidden are bound local and
149+
+ # the linker should differenciate them.
150+
+ pcaddi $a0, %pcrel_20(global_a)
151+
+ pcaddi $a0, %pcrel_20(global_b)
152+
diff --git a/ld/testsuite/ld-loongarch-elf/bad_pcrel20_s2_weak.d b/ld/testsuite/ld-loongarch-elf/bad_pcrel20_s2_weak.d
153+
new file mode 100644
154+
index 00000000..605df249
155+
--- /dev/null
156+
+++ b/ld/testsuite/ld-loongarch-elf/bad_pcrel20_s2_weak.d
157+
@@ -0,0 +1,5 @@
158+
+#name: PC-relative relocation making shared
159+
+#source: bad_pcrel20_s2_weak.s
160+
+#target: [check_shared_lib_support]
161+
+#ld: -shared --defsym global_a=0x10 --defsym global_b=0x20
162+
+#error: .*: relocation R_LARCH_PCREL20_S2 against `global_b` can not be used when making a shared object; recompile with -fPIC
163+
diff --git a/ld/testsuite/ld-loongarch-elf/bad_pcrel20_s2_weak.s b/ld/testsuite/ld-loongarch-elf/bad_pcrel20_s2_weak.s
164+
new file mode 100644
165+
index 00000000..82faa5bb
166+
--- /dev/null
167+
+++ b/ld/testsuite/ld-loongarch-elf/bad_pcrel20_s2_weak.s
168+
@@ -0,0 +1,9 @@
169+
+ .hidden global_a
170+
+ .weak global_b
171+
+ .text
172+
+ .align 2
173+
+main:
174+
+ # Symbols defined .hidden are bound local and
175+
+ # the linker should differenciate them.
176+
+ pcaddi $a0, %pcrel_20(global_a)
177+
+ pcaddi $a0, %pcrel_20(global_b)
178+
diff --git a/ld/testsuite/ld-loongarch-elf/data-plt.s b/ld/testsuite/ld-loongarch-elf/data-plt.s
179+
index faff052c..6c1a8eac 100644
180+
--- a/ld/testsuite/ld-loongarch-elf/data-plt.s
181+
+++ b/ld/testsuite/ld-loongarch-elf/data-plt.s
182+
@@ -2,6 +2,7 @@
183+
# R_LARCH_PCALA_HI20 only need to generate PLT entry for function symbols.
184+
.text
185+
.globl a
186+
+ .hidden a
187+
188+
.data
189+
.align 2
190+
diff --git a/ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp b/ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp
191+
index 555ebf92..615548dc 100644
192+
--- a/ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp
193+
+++ b/ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp
194+
@@ -166,6 +166,10 @@ if [istarget "loongarch64-*-*"] {
195+
run_dump_test "relr-got-shared"
196+
run_dump_test "relr-text-shared"
197+
run_dump_test "abssym_shared"
198+
+ run_dump_test "bad_pcala_hi20_global"
199+
+ run_dump_test "bad_pcala_hi20_weak"
200+
+ run_dump_test "bad_pcrel20_s2_global"
201+
+ run_dump_test "bad_pcrel20_s2_weak"
202+
}
203+
204+
if [check_pie_support] {
205+
--
206+
2.47.3
207+

0 commit comments

Comments
 (0)