[PATCH 1/2] MIPS64/BFD: Fix a crash with STN_UNDEF in relocation

Maciej W. Rozycki macro@mips.com
Fri Apr 6 14:48:00 GMT 2018
Prevent a null BFD pointer dereference and a resulting segmentation 
fault in `mips_elf64_write_rel' or `mips_elf64_write_rela':

Program received signal SIGSEGV, Segmentation fault.
0x0000000000437690 in mips_elf64_write_rela (abfd=0x71e130, sec=0x720700,
    rela_hdr=0x721ff8, count=0x7fffffffb82c, data=0x7fffffffb88c)
    at .../bfd/elf64-mips.c:4123
4123	      if ((*ptr->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec
4124		  && ! _bfd_elf_validate_reloc (abfd, ptr))


in the MIPS64 (n64 MIPS) ELF backend whenever the STN_UNDEF symbol index 
is retrieved from the `r_sym' field of a relocation seen in input while 
running `objcopy' or `strip'.  The reason for the null BFD pointer is 
that internally in BFD an STN_UNDEF symbol reference resolves to an 
absolute zero symbol that does not have a BFD associated.  Check the 
pointer then before using it, like the generic ELF backend does in 
`elf_write_relocs'.

This complements the same change made for generic ELF bundled with:

commit e35765a9a2eaff0df62757f3e6480c8ba5ab8ee8
Author: Ian Lance Taylor <ian@airs.com>
Date:   Sun Dec 15 19:59:18 1996 +0000

which (obviously due to a CVS -> GIT repository conversion inaccuracy) 
seems to be one corresponding to this ChangeLog entry:

	* elfcode.h (write_relocs): Handle absolute symbol.

from:

commit c86158e591edd8450f49f8cd75f82e4313d4b6d8
Author: Ian Lance Taylor <ian@airs.com>
Date:   Fri Aug 30 22:09:51 1996 +0000

("Add SH ELF support."), which also updated RELA only and not REL (which 
has been since fixed with: commit 947216bf8f34 ("ELF reloc code tidy"), 
<https://sourceware.org/ml/binutils/2002-11/msg00727.html>).

	bfd/
	* elf64-mips.c (mips_elf64_write_rel): Handle a NULL BFD pointer 
	in the BFD symbol referred by the relocation.
	(mips_elf64_write_rela): Likewise.
---
Hi,

 Self-approved and will commit it along with 2/2.

  Maciej
---
 bfd/elf64-mips.c |    6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

binutils-mips64-bfd-write-reloc-null-bfd.diff
Index: binutils/bfd/elf64-mips.c
===================================================================
--- binutils.orig/bfd/elf64-mips.c	2018-04-06 13:46:46.068090012 +0100
+++ binutils/bfd/elf64-mips.c	2018-04-06 13:49:45.341324912 +0100
@@ -4038,7 +4038,8 @@ mips_elf64_write_rel (bfd *abfd, asectio
       int_rel.r_sym = n;
       int_rel.r_ssym = RSS_UNDEF;
 
-      if ((*ptr->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec
+      if ((*ptr->sym_ptr_ptr)->the_bfd != NULL
+	  && (*ptr->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec
 	  && ! _bfd_elf_validate_reloc (abfd, ptr))
 	{
 	  *failedp = TRUE;
@@ -4137,7 +4138,8 @@ mips_elf64_write_rela (bfd *abfd, asecti
       int_rela.r_addend = ptr->addend;
       int_rela.r_ssym = RSS_UNDEF;
 
-      if ((*ptr->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec
+      if ((*ptr->sym_ptr_ptr)->the_bfd != NULL
+	  && (*ptr->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec
 	  && ! _bfd_elf_validate_reloc (abfd, ptr))
 	{
 	  *failedp = TRUE;



More information about the Binutils mailing list