MIPS/LD: Fix a segfault from ELF `e_flags' access with non-ELF output BFD
authorMaciej W. Rozycki <macro@imgtec.com>
Fri, 7 Jul 2017 16:58:03 +0000 (17:58 +0100)
committerMaciej W. Rozycki <macro@imgtec.com>
Fri, 7 Jul 2017 16:58:03 +0000 (17:58 +0100)
commite54cb31aa33a124f746ff40c134e20e6d2bc6c34
tree49bed9d86da222793d816cf7f0dd8ab396975774
parentc620a2b5471b9158e9e0da176e098ce3f4335b1f
MIPS/LD: Fix a segfault from ELF `e_flags' access with non-ELF output BFD

Fix a commit 861fb55ab50a ("Defer allocation of R_MIPS_REL32 GOT
slots"), <https://sourceware.org/ml/binutils/2008-08/msg00096.html>,
regression and a more recent:

FAIL: ld-unique/pr21529

new LD test case failure, observed with all the relevant MIPS targets
whenever the linker is invoked with one or more ELF inputs and the
output format set to `binary'.

The culprit is a segmentation fault caused in `mips_before_allocation'
by a null pointer dereference, where an attempt is made to access the
ELF file header's `e_flags' member, for the purpose of determining
whether to produce a PLT and copy relocations, without first checking
that the output BFD is ELF.  The `e_flags' member is stored in BFD's
private data pointed to by `tdep', which in the case of the `binary' BFD
is null, causing the segmentation fault.  With other non-ELF BFDs such
as SREC `tdep' is not null and consequently no crash may happen and in
that case random data will be interpreted as it was `e_flags'.

Disable the access to `e_flags' then and all the associated checks and
consequently never produce a PLT and copy relocations if output is not a
MIPS ELF BFD, matching `_bfd_mips_elf_merge_private_bfd_data' that does
not process `e_flags' in that case either and therefore does not let us
decide here anyway if all the input objects included in the link are
suitable for use with a PLT and copy relocations.

ld/
* emultempl/mipself.em (mips_before_allocation): Avoid ELF
processing if not MIPS ELF.
* testsuite/ld-mips-elf/binary.d: New test.
* testsuite/ld-mips-elf/binary.ld: New test linker script.
* testsuite/ld-mips-elf/binary.s: New test source.
* testsuite/ld-mips-elf/mips-elf.exp: Run the new test.
ld/ChangeLog
ld/emultempl/mipself.em
ld/testsuite/ld-mips-elf/binary.d [new file with mode: 0644]
ld/testsuite/ld-mips-elf/binary.ld [new file with mode: 0644]
ld/testsuite/ld-mips-elf/binary.s [new file with mode: 0644]
ld/testsuite/ld-mips-elf/mips-elf.exp
This page took 0.02728 seconds and 4 git commands to generate.