]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
Avoid unused space in .rela.dyn if sec was discarded
authorJinyang He <hejinyang@loongson.cn>
Fri, 15 Sep 2023 03:52:14 +0000 (11:52 +0800)
committerliuzhensong <liuzhensong@loongson.cn>
Tue, 27 Feb 2024 03:58:33 +0000 (11:58 +0800)
The relsec size is still increased although sec is discarded, which
cause a lot of unused space allocated. Avoid size increased if sec
was discarded.

bfd/ChangeLog:

* bfd/elfnn-loongarch.c: (allocate_dynrelocs): Do not increase
sreloc size when discarded_section.

ld/ChangeLog:

* ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp: Add test.
* ld/testsuite/ld-loongarch-elf/pie_discard.d: New test.
* ld/testsuite/ld-loongarch-elf/pie_discard.s: New test.
* ld/testsuite/ld-loongarch-elf/pie_discard.t: New test.

bfd/elfnn-loongarch.c
ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp
ld/testsuite/ld-loongarch-elf/pie_discard.d [new file with mode: 0644]
ld/testsuite/ld-loongarch-elf/pie_discard.s [new file with mode: 0644]
ld/testsuite/ld-loongarch-elf/pie_discard.t [new file with mode: 0644]

index 1895699af06837f1ba1852bd245c98cda26ba60d..1b5009e13b28f05f6d062341e23a1f4e79fda873 100644 (file)
@@ -1368,6 +1368,8 @@ allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
 
   for (p = h->dyn_relocs; p != NULL; p = p->next)
     {
+      if (discarded_section (p->sec))
+       continue;
       asection *sreloc = elf_section_data (p->sec)->sreloc;
       sreloc->size += p->count * sizeof (ElfNN_External_Rela);
     }
index 2ad783fbae1be3c0afbd9ed89ce3386d077b5691..3a8629a07df1fd7210f3ed6dde375c8e42f4c123 100644 (file)
@@ -147,3 +147,4 @@ run_dump_test "underflow_b16"
 run_dump_test "underflow_b21"
 run_dump_test "underflow_b26"
 run_dump_test "underflow_pcrel20"
+run_dump_test "pie_discard"
diff --git a/ld/testsuite/ld-loongarch-elf/pie_discard.d b/ld/testsuite/ld-loongarch-elf/pie_discard.d
new file mode 100644 (file)
index 0000000..7b86309
--- /dev/null
@@ -0,0 +1,10 @@
+#source: pie_discard.s
+#ld: -pie -e 0 -T pie_discard.t
+#readelf: -rW
+
+#...
+Relocation section '\.rela\.dyn' .* 1 .*
+#...
+.*R_LARCH_RELATIVE.*
+#pass
+
diff --git a/ld/testsuite/ld-loongarch-elf/pie_discard.s b/ld/testsuite/ld-loongarch-elf/pie_discard.s
new file mode 100644 (file)
index 0000000..82b88fc
--- /dev/null
@@ -0,0 +1,9 @@
+  .text
+  .global sym
+sym: nop
+
+  .section .data,"aw"
+  .dword sym
+
+  .section .discard,"aw"
+  .dword sym
diff --git a/ld/testsuite/ld-loongarch-elf/pie_discard.t b/ld/testsuite/ld-loongarch-elf/pie_discard.t
new file mode 100644 (file)
index 0000000..49e52cd
--- /dev/null
@@ -0,0 +1,9 @@
+SECTIONS
+{
+  . = SEGMENT_START("text-segment", 0) + SIZEOF_HEADERS;
+  .rela.dyn : { *(.rela.*) }
+  .text : { *(.text) }
+  . = DATA_SEGMENT_ALIGN (CONSTANT (MAXPAGESIZE), CONSTANT (COMMONPAGESIZE));
+  .data : { *(.data) }
+  /DISCARD/ : { *(.discard) }
+}