MIPS64/BFD: Fix a crash with STN_UNDEF in relocation

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 e35765a9a2
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 c86158e591
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 947216bf8f ("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.
This commit is contained in:
Maciej W. Rozycki 2018-04-09 13:42:00 +01:00
parent 3f97ba9fc8
commit aec2e0d252
2 changed files with 10 additions and 2 deletions

View File

@ -1,3 +1,9 @@
2018-04-09 Maciej W. Rozycki <macro@mips.com>
* 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.
2018-04-09 Maciej W. Rozycki <macro@mips.com>
* elf64-mips.c (mips_elf64_slurp_one_reloc_table): Issue an

View File

@ -4038,7 +4038,8 @@ mips_elf64_write_rel (bfd *abfd, asection *sec,
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, asection *sec,
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;