[PATCH] elf: Avoid relocating non-allocated code sections

"H.J. Lu" <[email protected]> Thu, 30 Jul 2026 08:33:19 +0800
Newsgroups gmane.comp.gnu.binutils
Message-ID <CAMe9rOrbnsbN5dYb17PZdHvoXY+03X5WsuBfHuTmHMATwA53qA@mail.gmail.com>
Since non-allocated code sections can't be properly relocated, issue an
error on such input.

PR ld/34444
* elflink.c (elf_link_input_bfd): Issue an error on a non-allocated
code section.

-- 
H.J.
0001-elf-Avoid-relocating-non-allocated-code-sections.patch (text/x-patch, 1.2 KB)
From f3a455bc2cda2d28a451688bbf6af88ae88c2693 Mon Sep 17 00:00:00 2001
From: "H.J. Lu" <[email protected]>
Date: Thu, 30 Jul 2026 08:27:35 +0800
Subject: [PATCH] elf: Avoid relocating non-allocated code sections

Since non-allocated code sections can't be properly relocated, issue an
error on such input.

	PR ld/34444
	* elflink.c (elf_link_input_bfd): Issue an error on non-allocated
	code section.

Signed-off-by: H.J. Lu <[email protected]>
---
 bfd/elflink.c | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/bfd/elflink.c b/bfd/elflink.c
index 0af9837a28c..e0d7cd9d171 100644
--- a/bfd/elflink.c
+++ b/bfd/elflink.c
@@ -11903,6 +11903,17 @@ elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
 		}
 	    }
 
+	  if ((o->flags & SEC_ALLOC) == 0
+	      && (o->flags & SEC_CODE) != 0)
+	    {
+	      _bfd_error_handler
+		/* xgettext:c-format */
+		(_("%pB: can't relocate non-allocated code section `%pA'"),
+		 input_bfd, o);
+	      bfd_set_error (bfd_error_bad_value);
+	      return false;
+	    }
+
 	  /* Relocate the section by invoking a back end routine.
 
 	     The back end routine is responsible for adjusting the
-- 
2.55.0