]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
LoongArch: ld: Move .got .got.plt before .data and protect .got with relro
authormengqinggang <mengqinggang@loongson.cn>
Wed, 3 Apr 2024 02:20:27 +0000 (10:20 +0800)
committerliuzhensong <liuzhensong@loongson.cn>
Tue, 9 Apr 2024 06:31:04 +0000 (14:31 +0800)
Move .got .got.plt before .data so .got can be protected with -zrelro.
And the first two entries of .got.plt (_dl_runtime_resolve and link map)
are placed within the relro region.

bfd/elfnn-loongarch.c
ld/emulparams/elf64loongarch-defs.sh
ld/testsuite/ld-loongarch-elf/data-got.d [new file with mode: 0644]
ld/testsuite/ld-loongarch-elf/data-got.s [new file with mode: 0644]
ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp

index eb70799b06a3acfd41833244be615252177cb830..e86329050198ab87a7aa3f43365579cbd803975e 100644 (file)
@@ -127,6 +127,8 @@ struct loongarch_elf_link_hash_table
 
 #define GOT_ENTRY_SIZE (LARCH_ELF_WORD_BYTES)
 
+/* Reserve two entries of GOTPLT for ld.so, one is used for PLT
+   resolver _dl_runtime_resolve, the other is used for link map.  */
 #define GOTPLT_HEADER_SIZE (GOT_ENTRY_SIZE * 2)
 
 #define elf_backend_want_got_plt 1
index c793f5d8388a934f1ed08a1ef12ba23c712bc007..a8147bf71d7a5bd2bfcd9272e330901f5d5fe5c6 100644 (file)
@@ -34,6 +34,9 @@ TEXT_START_ADDR=0x120000000
 MAXPAGESIZE="CONSTANT (MAXPAGESIZE)"
 COMMONPAGESIZE="CONSTANT (COMMONPAGESIZE)"
 
-SEPARATE_GOTPLT=0
+# Put .got before .data
+DATA_GOT=" "
+# First two entries for PLT resolver _dl_runtime_resolve and link map.
+SEPARATE_GOTPLT="SIZEOF (.got.plt) >= 16 ? 16 : 0"
 INITIAL_READONLY_SECTIONS=".interp         : { *(.interp) } ${CREATE_PIE-${INITIAL_READONLY_SECTIONS}}"
 INITIAL_READONLY_SECTIONS="${RELOCATING+${CREATE_SHLIB-${INITIAL_READONLY_SECTIONS}}}"
diff --git a/ld/testsuite/ld-loongarch-elf/data-got.d b/ld/testsuite/ld-loongarch-elf/data-got.d
new file mode 100644 (file)
index 0000000..a754478
--- /dev/null
@@ -0,0 +1,16 @@
+# line 11 test the first two entries of .got.plt in relro region
+# relro segment size is .dynamic size + .got size + 0x10
+# line 13 test .got .got.plt before .got
+# line 15 test .got in relro segment
+#as:
+#ld: -shared -z relro --hash-style=both
+#readelf: -l --wide
+#skip: loongarch32-*-*
+
+#...
+  GNU_RELRO      0x003c10 0x0000000000007c10 0x0000000000007c10 0x0003f0 0x0003f0 R   0x1
+#...
+   01     .dynamic .got .got.plt .data 
+#...
+   03     .dynamic .got 
+#pass
diff --git a/ld/testsuite/ld-loongarch-elf/data-got.s b/ld/testsuite/ld-loongarch-elf/data-got.s
new file mode 100644 (file)
index 0000000..364fcf6
--- /dev/null
@@ -0,0 +1,6 @@
+.text
+b foo
+.section .got
+.space 0x2a8, 4
+.data
+.zero 24
index 759acab80d40cb303e5f7d114dbf270670f65e01..c2d616b8d0add982d94ee572dd7a489ff750c22a 100644 (file)
@@ -133,6 +133,7 @@ if [istarget "loongarch64-*-*"] {
     run_dump_test "tlsdesc-dso"
     run_dump_test "desc-norelax"
     run_dump_test "desc-relax"
+    run_dump_test "data-got"
   }
 
   if [check_pie_support] {