public inbox for binutils-cvs@sourceware.org
 help / color / mirror / Atom feed
From: Alan Modra <amodra@sourceware.org>
To: bfd-cvs@sourceware.org
Subject: [binutils-gdb] Don't segfault in mips reloc special_functions
Date: Tue, 20 Jun 2023 02:58:50 +0000 (GMT)	[thread overview]
Message-ID: <20230620025850.656C13858423@sourceware.org> (raw)

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=75e73c6cadcc064c2a0fd03396666574cd5335ca

commit 75e73c6cadcc064c2a0fd03396666574cd5335ca
Author: Alan Modra <amodra@gmail.com>
Date:   Tue Jun 20 09:46:03 2023 +0930

    Don't segfault in mips reloc special_functions
    
    A symbol defined in a section from a shared library will have a NULL
    section->output_section during linking.
    
            * elf32-mips.c (gprel32_with_gp): Don't segfault on NULL
            symbol->section->output_section.
            * elf64-mips.c (mips_elf64_gprel32_reloc): Likewise.
            * elfn32-mips.c (mips_elf_gprel16_reloc): Likewise.
            (mips_elf_literal_reloc, mips_elf_gprel32_reloc): Likewise.
            (gprel32_with_gp, mips16_gprel_reloc): Likewise.
            * elfxx-mips.c (_bfd_mips_elf_gprel16_with_gp): Likewise.
            (_bfd_mips_elf_generic_reloc): Likewise.

Diff:
---
 bfd/elf32-mips.c  |  7 +++++--
 bfd/elf64-mips.c  |  7 +++++--
 bfd/elfn32-mips.c | 19 +++++++++++++------
 bfd/elfxx-mips.c  | 10 +++++++---
 4 files changed, 30 insertions(+), 13 deletions(-)

diff --git a/bfd/elf32-mips.c b/bfd/elf32-mips.c
index 34ffa67edec..03be42e845c 100644
--- a/bfd/elf32-mips.c
+++ b/bfd/elf32-mips.c
@@ -1858,8 +1858,11 @@ gprel32_with_gp (bfd *abfd, asymbol *symbol, arelent *reloc_entry,
   else
     relocation = symbol->value;
 
-  relocation += symbol->section->output_section->vma;
-  relocation += symbol->section->output_offset;
+  if (symbol->section->output_section != NULL)
+    {
+      relocation += symbol->section->output_section->vma;
+      relocation += symbol->section->output_offset;
+    }
 
   if (!_bfd_mips_reloc_offset_in_range (abfd, input_section, reloc_entry,
 					check_inplace))
diff --git a/bfd/elf64-mips.c b/bfd/elf64-mips.c
index 0530be1253f..9af4e4047e3 100644
--- a/bfd/elf64-mips.c
+++ b/bfd/elf64-mips.c
@@ -3577,8 +3577,11 @@ mips_elf64_gprel32_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
   else
     relocation = symbol->value;
 
-  relocation += symbol->section->output_section->vma;
-  relocation += symbol->section->output_offset;
+  if (symbol->section->output_section != NULL)
+    {
+      relocation += symbol->section->output_section->vma;
+      relocation += symbol->section->output_offset;
+    }
 
   if (!_bfd_mips_reloc_offset_in_range (abfd, input_section, reloc_entry,
 					check_inplace))
diff --git a/bfd/elfn32-mips.c b/bfd/elfn32-mips.c
index 3ae11e2f992..ab3b58994ba 100644
--- a/bfd/elfn32-mips.c
+++ b/bfd/elfn32-mips.c
@@ -3300,7 +3300,8 @@ mips_elf_gprel16_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
   else
     {
       relocatable = false;
-      output_bfd = symbol->section->output_section->owner;
+      if (symbol->section->output_section != NULL)
+	output_bfd = symbol->section->output_section->owner;
     }
 
   ret = mips_elf_final_gp (output_bfd, symbol, relocatable, error_message,
@@ -3340,7 +3341,8 @@ mips_elf_literal_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
   else
     {
       relocatable = false;
-      output_bfd = symbol->section->output_section->owner;
+      if (symbol->section->output_section != NULL)
+	output_bfd = symbol->section->output_section->owner;
     }
 
   ret = mips_elf_final_gp (output_bfd, symbol, relocatable, error_message,
@@ -3383,7 +3385,8 @@ mips_elf_gprel32_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
   else
     {
       relocatable = false;
-      output_bfd = symbol->section->output_section->owner;
+      if (symbol->section->output_section != NULL)
+	output_bfd = symbol->section->output_section->owner;
 
       ret = mips_elf_final_gp (output_bfd, symbol, relocatable,
 			       error_message, &gp);
@@ -3408,8 +3411,11 @@ gprel32_with_gp (bfd *abfd, asymbol *symbol, arelent *reloc_entry,
   else
     relocation = symbol->value;
 
-  relocation += symbol->section->output_section->vma;
-  relocation += symbol->section->output_offset;
+  if (symbol->section->output_section != NULL)
+    {
+      relocation += symbol->section->output_section->vma;
+      relocation += symbol->section->output_offset;
+    }
 
   if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd, input_section,
 				  reloc_entry->address))
@@ -3484,7 +3490,8 @@ mips16_gprel_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
   else
     {
       relocatable = false;
-      output_bfd = symbol->section->output_section->owner;
+       if (symbol->section->output_section != NULL)
+	 output_bfd = symbol->section->output_section->owner;
     }
 
   ret = mips_elf_final_gp (output_bfd, symbol, relocatable, error_message,
diff --git a/bfd/elfxx-mips.c b/bfd/elfxx-mips.c
index 4dfd8d04610..71f2dc9d779 100644
--- a/bfd/elfxx-mips.c
+++ b/bfd/elfxx-mips.c
@@ -2481,8 +2481,11 @@ _bfd_mips_elf_gprel16_with_gp (bfd *abfd, asymbol *symbol,
   else
     relocation = symbol->value;
 
-  relocation += symbol->section->output_section->vma;
-  relocation += symbol->section->output_offset;
+  if (symbol->section->output_section != NULL)
+    {
+      relocation += symbol->section->output_section->vma;
+      relocation += symbol->section->output_offset;
+    }
 
   /* Set val to the offset into the section or symbol.  */
   val = reloc_entry->addend;
@@ -2673,7 +2676,8 @@ _bfd_mips_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
 
   /* Build up the field adjustment in VAL.  */
   val = 0;
-  if (!relocatable || (symbol->flags & BSF_SECTION_SYM) != 0)
+  if ((!relocatable || (symbol->flags & BSF_SECTION_SYM) != 0)
+      && symbol->section->output_section != NULL)
     {
       /* Either we're calculating the final field value or we have a
 	 relocation against a section symbol.  Add in the section's

                 reply	other threads:[~2023-06-20  2:58 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=20230620025850.656C13858423@sourceware.org \
    --to=amodra@sourceware.org \
    --cc=bfd-cvs@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).