]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
LoongArch: Disable linker relaxation if set the address of section or segment
authormengqinggang <mengqinggang@loongson.cn>
Thu, 30 May 2024 11:52:34 +0000 (19:52 +0800)
committerliuzhensong <liuzhensong@loongson.cn>
Tue, 4 Jun 2024 11:47:27 +0000 (19:47 +0800)
If set the address of section or segment, the offset from pc to symbol
may become bigger and cause overflow.

ld/emultempl/loongarchelf.em
ld/testsuite/ld-loongarch-elf/relax-ttext.s [new file with mode: 0644]
ld/testsuite/ld-loongarch-elf/relax.exp

index 3bb5ddf0db490621d10810099d95c8951db1764d..2da4058200877074b4bf670a8164cebbe52a527c 100644 (file)
@@ -25,6 +25,22 @@ fragment <<EOF
 #include "elf/loongarch.h"
 #include "elfxx-loongarch.h"
 
+EOF
+
+# Disable linker relaxation if set address of section or segment.
+PARSE_AND_LIST_ARGS_CASES=${PARSE_AND_LIST_ARGS_CASES}'
+    case OPTION_SECTION_START:
+    case OPTION_TTEXT:
+    case OPTION_TBSS:
+    case OPTION_TDATA:
+    case OPTION_TTEXT_SEGMENT:
+    case OPTION_TRODATA_SEGMENT:
+    case OPTION_TLDATA_SEGMENT:
+      link_info.disable_target_specific_optimizations = 2;
+      return false;
+'
+
+fragment <<EOF
 static void
 larch_elf_before_allocation (void)
 {
diff --git a/ld/testsuite/ld-loongarch-elf/relax-ttext.s b/ld/testsuite/ld-loongarch-elf/relax-ttext.s
new file mode 100644 (file)
index 0000000..1bbd85a
--- /dev/null
@@ -0,0 +1,13 @@
+# At relax pass 0, offset is 0x120204000-0x12000bff8=0x1f8008 < 0x200000
+# At relax pass 1, delete 0x7ff8 bytes NOP,
+# offset is 0x120204000-0x120004000=0x200000 >= 0x200000, overflow
+.text
+.align 14 # delete at relax pass 1
+.fill 0x4000
+.align 14 # delete at relax pass 1
+la.local $t2, a # relax to pcaddi at relax pass 0
+
+.section ".text1", "ax"
+  .fill 0x4000
+a:          # 0x120204000
+  ret
index 35caa73c11d2ebc6ab529244bf9eec1cae76aef7..4771f1eb4296a051a37f4855f0d9d26a882d36d8 100644 (file)
@@ -51,6 +51,18 @@ if [istarget loongarch64-*-*] {
     run_dump_test "relax-align-ignore-start"
     run_partial_linking_align_test
 
+    run_ld_link_tests \
+       [list \
+           [list \
+               "loongarch relax ttext" \
+               "" "" \
+               "" \
+               {relax-ttext.s} \
+               {} \
+               "relax-ttext" \
+           ] \
+       ]
+
     set testname "loongarch relax .exe build"
     set pre_builds [list \
       [list \