[binutils-gdb] elflink.c: _bfd_elf_make_dynamic_reloc_section sanity checks

Alan Modra via Binutils-cvs <[email protected]>
Newsgroups gmane.comp.gnu.binutils.cvs
Message-ID <[email protected]>
https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=53fd57102aa8f37fa604ef40a1f58bd86315832f

commit 53fd57102aa8f37fa604ef40a1f58bd86315832f
Author: Alan Modra <[email protected]>
Date:   Thu Aug 6 11:15:41 2026 +0930

    elflink.c: _bfd_elf_make_dynamic_reloc_section sanity checks
    
    Add a check that the byte order for an input object needing dynamic
    relocs matches the byte order of the output.  Also free up a little
    memory if the string created by get_dynamic_reloc_section_name is
    unused.  The change in get_dynamic_reloc_section_name return value
    is to avoid a cast when calling bfd_release.
    
            * elflink.c (get_dynamic_reloc_section_name): Return char*.
            (_bfd_elf_make_dynamic_reloc_section): Sanity check that
            dynobj exists, and that relocs and byte order are as expected.
            Release name if section already exists.

Diff:
---
 bfd/elflink.c | 26 ++++++++++++++++++++------
 1 file changed, 20 insertions(+), 6 deletions(-)

diff --git a/bfd/elflink.c b/bfd/elflink.c
index e60df0475c0..5a3ee89142b 100644
--- a/bfd/elflink.c
+++ b/bfd/elflink.c
@@ -15522,9 +15522,9 @@ _bfd_elf_default_got_elt_size (struct bfd_link_info *info,
 
 /* Returns the name of the dynamic reloc section associated with SEC.  */
 
-static const char *
-get_dynamic_reloc_section_name (bfd *       abfd,
-				asection *  sec,
+static char *
+get_dynamic_reloc_section_name (bfd *abfd,
+				asection *sec,
 				bool is_rela)
 {
   const char *prefix = is_rela ? ".rela" : ".rel";
@@ -15561,14 +15561,28 @@ _bfd_elf_make_dynamic_reloc_section (asection *sec,
 
   if (reloc_sec == NULL)
     {
-      const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
+      bool known = (dynobj != NULL
+		    && (get_elf_backend_data (dynobj)
+			->relocs_compatible (dynobj->xvec, abfd->xvec)));
+      BFD_ASSERT (known);
+      if (!known
+	  || dynobj->xvec->byteorder != abfd->xvec->byteorder)
+	{
+	  _bfd_error_handler (_("%s dynamic relocs incompatible with%s%s output"),
+			      bfd_get_target (abfd),
+			      dynobj ? " " : "",
+			      dynobj ? bfd_get_target (dynobj) : "");
+	  return NULL;
+	}
 
+      char *name = get_dynamic_reloc_section_name (dynobj, sec, is_rela);
       if (name == NULL)
 	return NULL;
 
       reloc_sec = bfd_get_linker_section (dynobj, name);
-
-      if (reloc_sec == NULL)
+      if (reloc_sec != NULL)
+	bfd_release (dynobj, name);
+      else
 	{
 	  flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
 			    | SEC_IN_MEMORY | SEC_LINKER_CREATED);
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.