public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
From: Alexey Lapshin <alexey.lapshin@espressif.com>
To: "binutils@sourceware.org" <binutils@sourceware.org>
Subject: [RFC] ld: fix --non-contiguous-regions error checks
Date: Fri, 1 Mar 2024 18:07:14 +0000	[thread overview]
Message-ID: <58723310fe4171a607475930ffd35fbba7ca8aaf.camel@espressif.com> (raw)

- Fix segmentation fault in _bfd_riscv_relax_section():
sym_sec->output_sectioin nullptr dereference
https://github.com/bminor/binutils-gdb/blob/binutils-2_41-release/bfd/elfnn-riscv.c#L5207

- Fatal error only for sections that has SEC_RELOC flag when a segment
overflows. It allows to write sections with SEC_MERGE flag to next
segment.

In case changes are good to go there are also some workarounds in the code these should be removed.
Like this:
https://github.com/bminor/binutils-gdb/blob/binutils-2_41-release/bfd/elf32-arm.c#L5052-L5058


---
 ld/ldlang.c                                   | 22 ++++++++++++-------
 ld/testsuite/ld-arm/non-contiguous-arm.d      |  2 +-
 ld/testsuite/ld-arm/non-contiguous-arm7.err   |  3 +--
 .../non-contiguous-merged-sections.d          |  9 ++++++++
 .../non-contiguous-merged-sections.ld         | 18 +++++++++++++++
 .../non-contiguous-merged-sections.s          |  7 ++++++
 ld/testsuite/ld-powerpc/powerpc.exp           |  1 +
 7 files changed, 51 insertions(+), 11 deletions(-)
 create mode 100644 ld/testsuite/ld-powerpc/non-contiguous-merged-sections.d
 create mode 100644 ld/testsuite/ld-powerpc/non-contiguous-merged-sections.ld
 create mode 100644 ld/testsuite/ld-powerpc/non-contiguous-merged-sections.s

diff --git a/ld/ldlang.c b/ld/ldlang.c
index 54d1af62ebe..f2b2a9bf356 100644
--- a/ld/ldlang.c
+++ b/ld/ldlang.c
@@ -5506,7 +5506,7 @@ size_input_section
 			     "the linker-created stubs section `%pA'.\n"),
 			   i->output_section, i);
 
-		  if (i->rawsize && i->rawsize != i->size)
+		  if (i->flags & SEC_RELOC && i->rawsize && i->rawsize != i->size)
 		    einfo (_("%F%P: Relaxation not supported with "
 			     "--enable-non-contiguous-regions (section `%pA' "
 			     "would overflow `%pA' after it changed size).\n"),
@@ -8094,8 +8094,9 @@ lang_propagate_lma_regions (void)
 }
 
 static void
-warn_non_contiguous_discards (void)
+lang_check_non_contiguous_discards (void)
 {
+  unsigned long discard = 0;
   LANG_FOR_EACH_INPUT_STATEMENT (file)
     {
       if ((file->the_bfd->flags & (BFD_LINKER_CREATED | DYNAMIC)) != 0
@@ -8104,11 +8105,16 @@ warn_non_contiguous_discards (void)
 
       for (asection *s = file->the_bfd->sections; s != NULL; s = s->next)
 	if (s->output_section == NULL
-	    && (s->flags & SEC_LINKER_CREATED) == 0)
-	  einfo (_("%P: warning: --enable-non-contiguous-regions "
+	    && (s->flags & SEC_LINKER_CREATED) == 0) {
+	  einfo (_("%P: error: --enable-non-contiguous-regions "
 		   "discards section `%pA' from `%pB'\n"),
 		 s, file->the_bfd);
+	  discard += s->size;
+	}
     }
+  if (discard > 0)
+    einfo ("%F%P: error: Total discarded sections size is %lu bytes\n",
+	   discard);
 }
 
 static void
@@ -8436,6 +8442,10 @@ lang_process (void)
   /* Size up the sections.  */
   lang_size_sections (NULL, !RELAXATION_ENABLED);
 
+  /* Check if has no discarded sections.  */
+  if (link_info.non_contiguous_regions)
+    lang_check_non_contiguous_discards ();
+
   /* See if anything special should be done now we know how big
      everything is.  This is where relaxation is done.  */
   ldemul_after_allocation ();
@@ -8459,10 +8469,6 @@ lang_process (void)
   if (command_line.check_section_addresses)
     lang_check_section_addresses ();
 
-  if (link_info.non_contiguous_regions
-      && link_info.non_contiguous_regions_warnings)
-    warn_non_contiguous_discards ();
-
   /* Check any required symbols are known.  */
   ldlang_check_require_defined_symbols ();
 
diff --git a/ld/testsuite/ld-arm/non-contiguous-arm.d b/ld/testsuite/ld-arm/non-contiguous-arm.d
index 3479778991e..98140e4dd01 100644
--- a/ld/testsuite/ld-arm/non-contiguous-arm.d
+++ b/ld/testsuite/ld-arm/non-contiguous-arm.d
@@ -1,4 +1,4 @@
 #name: non-contiguous-arm
 #source: non-contiguous-arm.s
 #ld: --enable-non-contiguous-regions -T non-contiguous-arm.ld
-# error: .*Could not assign .?\.code\.4.? to an output section. Retry without --enable-non-contiguous-regions\.
+#error_output: non-contiguous-arm.err
diff --git a/ld/testsuite/ld-arm/non-contiguous-arm7.err b/ld/testsuite/ld-arm/non-contiguous-arm7.err
index 7b3a3d8c3c9..14cef67e0df 100644
--- a/ld/testsuite/ld-arm/non-contiguous-arm7.err
+++ b/ld/testsuite/ld-arm/non-contiguous-arm7.err
@@ -1,5 +1,4 @@
 .* may change behaviour for section .?\.bss.? from .*
 .* may change behaviour for section .?\.bss\.MY_BUF.? from .*
 .* discards section .?\.bss\.MY_BUF.? from .*
-.* unresolvable R_ARM_ABS32 relocation against symbol .?MY_BUF.?
-.* final link failed
+.* Total discarded sections size is [0-9]+ bytes
diff --git a/ld/testsuite/ld-powerpc/non-contiguous-merged-sections.d b/ld/testsuite/ld-powerpc/non-contiguous-merged-sections.d
new file mode 100644
index 00000000000..e01b5151085
--- /dev/null
+++ b/ld/testsuite/ld-powerpc/non-contiguous-merged-sections.d
@@ -0,0 +1,9 @@
+#name: non-contiguous-merged-sections
+#source: non-contiguous-merged-sections.s
+#ld: --enable-non-contiguous-regions -T non-contiguous-merged-sections.ld
+#readelf: -l --wide
+
+#...
+ +LOAD +0x[0-9a-f]+ +0x[0-9a-f]+ +0x[0-9a-f]+ 0x00012 0x00012 .*
+#...
+ +00 +\.rodata_ram2
diff --git a/ld/testsuite/ld-powerpc/non-contiguous-merged-sections.ld b/ld/testsuite/ld-powerpc/non-contiguous-merged-sections.ld
new file mode 100644
index 00000000000..79c4ae89cb8
--- /dev/null
+++ b/ld/testsuite/ld-powerpc/non-contiguous-merged-sections.ld
@@ -0,0 +1,18 @@
+MEMORY
+{
+  RAM1 (xrw)   : ORIGIN = 0x10000000,   LENGTH = 4b
+  RAM2 (xrw)   : ORIGIN = 0x20000000,   LENGTH = 1K
+}
+
+SECTIONS
+{
+  .rodata_ram1 :
+  {
+    *(.rodata) *(.rodata*);
+  } >RAM1
+
+  .rodata_ram2 :
+  {
+    *(.rodata) *(.rodata*);
+  } >RAM2
+}
diff --git a/ld/testsuite/ld-powerpc/non-contiguous-merged-sections.s b/ld/testsuite/ld-powerpc/non-contiguous-merged-sections.s
new file mode 100644
index 00000000000..49832b3b514
--- /dev/null
+++ b/ld/testsuite/ld-powerpc/non-contiguous-merged-sections.s
@@ -0,0 +1,7 @@
+	.file	"<artificial>"
+	.section	.rodata.str1.8,"aMS",@progbits,1
+.LC0:
+	.string	"string_1"
+	.section	.rodata.str2.8,"aMS",@progbits,1
+.LC1:
+	.string	"string_2"
diff --git a/ld/testsuite/ld-powerpc/powerpc.exp b/ld/testsuite/ld-powerpc/powerpc.exp
index 9989f5a329d..fc42118d862 100644
--- a/ld/testsuite/ld-powerpc/powerpc.exp
+++ b/ld/testsuite/ld-powerpc/powerpc.exp
@@ -519,6 +519,7 @@ run_dump_test "ppc476-shared"
 run_dump_test "ppc476-shared2"
 
 run_dump_test "non-contiguous-powerpc"
+run_dump_test "non-contiguous-merged-sections"
 
 run_dump_test "tprel32"
 run_dump_test "tprelbad"
-- 
2.34.1



                 reply	other threads:[~2024-03-01 18:07 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=58723310fe4171a607475930ffd35fbba7ca8aaf.camel@espressif.com \
    --to=alexey.lapshin@espressif.com \
    --cc=binutils@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).