]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
RISC-V: Don't generate R_RISCV_RELATIVE if symbol referenced section is SH_ABS
authorNelson Chu <nelson@rivosinc.com>
Thu, 21 Aug 2025 04:33:04 +0000 (12:33 +0800)
committerNelson Chu <nelson@rivosinc.com>
Thu, 4 Sep 2025 08:33:50 +0000 (16:33 +0800)
It's werid if it's absolute symbol but have a relative reloc for got entry
under pie.  So don't generate relative reloc if symbol referenced section is
SH_ABS.  However, x86 allows the absolute symbol defined in linker script has
a relative reloc, not sure if risc-v needs this or not.

bfd/elfnn-riscv.c
ld/testsuite/ld-riscv-elf/absolute-no-relative.d [new file with mode: 0644]
ld/testsuite/ld-riscv-elf/absolute-no-relative.ld [new file with mode: 0644]
ld/testsuite/ld-riscv-elf/absolute-no-relative.s [new file with mode: 0644]
ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp

index 6ff64325f8583a238703dc52b04333cf68e74a1f..aac061fb43d15fe308e85945fb5426cc3fd9799e 100644 (file)
@@ -2880,7 +2880,8 @@ riscv_elf_relocate_section (bfd *output_bfd,
                      if (h->dynindx == -1
                          && !h->forced_local
                          && h->root.type != bfd_link_hash_undefweak
-                         && bfd_link_pic (info))
+                         && bfd_link_pic (info)
+                         && !bfd_is_abs_section(h->root.u.def.section))
                        relative_got = true;
 
                      bfd_put_NN (output_bfd, relocation,
diff --git a/ld/testsuite/ld-riscv-elf/absolute-no-relative.d b/ld/testsuite/ld-riscv-elf/absolute-no-relative.d
new file mode 100644 (file)
index 0000000..6699ac1
--- /dev/null
@@ -0,0 +1,9 @@
+#source: absolute-no-relative.s
+#as:
+#ld: -Tabsolute-no-relative.ld -pie
+#readelf: -Wr
+
+Relocation section '.rela.dyn' at .*
+[ ]+Offset[ ]+Info[ ]+Type[ ]+.*
+0+[    ]+0+[   ]+R_RISCV_NONE[         ]+0
+0+[    ]+0+[   ]+R_RISCV_NONE[         ]+0
diff --git a/ld/testsuite/ld-riscv-elf/absolute-no-relative.ld b/ld/testsuite/ld-riscv-elf/absolute-no-relative.ld
new file mode 100644 (file)
index 0000000..34bbcba
--- /dev/null
@@ -0,0 +1,6 @@
+ENTRY(_start)
+SECTIONS {
+       __symbol_abs__ = 0x1234;
+       .text 0x10000 : { *(.text*)}
+       .got  0x20000 : { *(.got*) }
+}
diff --git a/ld/testsuite/ld-riscv-elf/absolute-no-relative.s b/ld/testsuite/ld-riscv-elf/absolute-no-relative.s
new file mode 100644 (file)
index 0000000..4515661
--- /dev/null
@@ -0,0 +1,8 @@
+.text
+.global _start
+_start:
+lga    x1, symbol_abs
+lga    x1, __symbol_abs__
+
+.global symbol_abs
+.set symbol_abs, 0x100
index bae1105cad6a081a5dcb739bc994aca430095643..b0d510ac8daafa9d0a590bb191ea1de13120ca82 100644 (file)
@@ -338,4 +338,5 @@ if [istarget "riscv*-*-*"] {
     }
     run_dump_test "tls"
     run_dump_test "tlsbin"
+    run_dump_test "absolute-no-relative"
 }