Commit Graph

17161 Commits

Author SHA1 Message Date
Alan Modra
590abc168f Don't include coff/pe.h in coff-x86_64.c
This (and other) code from coffcode.h is broken for x86_64_coff_vec,
and has been ever since support was added in 2006 commit 99ad839030
Here, bfd_coff_aoutsz must match coff_swap_aouthdr_out otherwise we
end up writing garbage.

      /* Note that peicode.h fills in a PEAOUTHDR, not an AOUTHDR.
	 include/coff/pe.h sets AOUTSZ == sizeof (PEAOUTHDR)).  */
      char * buff;
      bfd_size_type amount = bfd_coff_aoutsz (abfd);

      buff = (char *) bfd_malloc (amount);
      if (buff == NULL)
	return false;

      coff_swap_aouthdr_out (abfd, & internal_a, buff);
      amount = bfd_bwrite (buff, amount, abfd);

We have removed support for --target=x86_64-coff, likely because it
never worked properly, but still produce coff-x86_64.o with
--enable-targets=all.  This means objcopy can recognize x86_64 COFF
files but will write garbage to the output file, a fact found by
fuzzers.  I suspect x86_64 COFF is still broken after this fix, and
mention of coff-x86_64.* should be removed from bfd/Makefile.am.

	* coff-x86_64.c: Don't include coff/pe.h.
	(COFF_WITH_pex64): Don't define here.
	* pe-x86_64.c: Include coff/pe.h and other headers.
	(PEI_HEADERS): Define.
2021-10-31 11:42:52 +10:30
Alan Modra
1c5e9d8d8c Re: PR28420, ecoff fuzzing failures
sym_ptr_ptr NULL results in segfaults.

	PR 28420
	* ecoff.c (ecoff_slurp_reloc_table): Don't leave sym_ptr_ptr NULL.
2021-10-31 11:42:08 +10:30
Alan Modra
9216910ed8 ubsan: alpha-vms: undefined shift
* vms-alpha.c (evax_bfd_print_image): Shift left 1u.
2021-10-31 11:41:39 +10:30
Alan Modra
3c8ed65a5d PR28518: signed integer overflow & free on unmalloced address
PR 28518
	* vms-alpha.c (build_module_list): Don't lose malloc buffer address.
	Use unsigned variables.
2021-10-31 10:57:52 +10:30
GDB Administrator
fb9f44d410 Automatic date update in version.in 2021-10-31 00:00:09 +00:00
GDB Administrator
4a3a56c5f3 Automatic date update in version.in 2021-10-30 00:00:26 +00:00
Alan Modra
c45c3dba8c ELF core file size checks
Catch fuzzed segments where p_offset + p_filesz wraps, and limit error
output.

	* elfcore.h (elf_core_file_p): Rewrite segment checks using
	bfd_get_file_size.  Set read_only on file size errors.
	* elfcode.h (elf_swap_shdr_in): Don't repeat error message.
2021-10-29 17:22:32 +10:30
Alan Modra
c82ebeb7e6 obcopy vs. files with silly section alignment
We already ignore stupid segment alignment when rewriting headers,
ignore section alignment too.

	* elf.c (rewrite_elf_program_header): Ignore section alignment
	power greater than 62.
2021-10-29 15:48:22 +10:30
GDB Administrator
208eb58158 Automatic date update in version.in 2021-10-29 00:00:18 +00:00
Alan Modra
0a0ff9d931 asan: mmo: NULL dereferenc in mmo_xore_32
mmo_get_loc can return NULL.  It's commented even, and that the caller
then must handle a split field.  mmo_xore_* don't handle split fields,
instead just segfault.  Stop that happening, and refuse to recognise
fuzzed mmo files that trigger this problem.

	* mmo.c (mmo_get_loc): Don't declare inline.
	(mmo_xore_64, mmo_xore_32, mmo_xore_16): Remove forward decls.
	Return pointer, don't dereference NULL.
	(mmo_scan): Return error on mmo_get_loc returning NULL.
2021-10-28 13:25:57 +10:30
Alan Modra
42eec46f23 bfd: remove use of INLINE
No need to use anything fancy, plain inline works just as well.

	* bfd-in.h (INLINE): Don't define.
	* bfd-in2.h: Regenerate.
	* aoutx.h: Replace use of INLINE with inline.
	* elf-eh-frame.c: Likewise.
	* elf32-score7.c: Likewise.
	* elfxx-mips.c: Likewise.
	* ihex.c: Likewise.
	* mach-o.c: Likewise.
	* mmo.c: Likewise.
2021-10-28 13:24:37 +10:30
GDB Administrator
9fc9dbc28b Automatic date update in version.in 2021-10-28 00:00:16 +00:00
Alan Modra
94852430d6 asan: alpha-vms: buffer overflows
Yet more anti-fuzzer sanity checking

	* vms-alpha.c (evax_bfd_print_egsd): Sanity check record and
	name lengths before access.
	(evax_bfd_print_etir_stc_ir, evax_bfd_print_etir): Likewise.
2021-10-28 07:40:49 +10:30
GDB Administrator
28c26ce5fd Automatic date update in version.in 2021-10-27 00:00:18 +00:00
GDB Administrator
3976e96a02 Automatic date update in version.in 2021-10-26 00:00:12 +00:00
Alan Modra
1e5a28be0f ubsan: _bfd_xcoff64_swap_aux_in left shift of negative value
* coff64-rs6000.c (_bfd_xcoff64_swap_aux_in): Use bfd_vma for h.
2021-10-25 11:41:52 +10:30
Alan Modra
b920bc3722 asan: evax_bfd_print_image buffer overflow
* vms-alpha.c (evax_bfd_print_image): Sanity check printing of
	"image activator fixup" section.
	(evax_bfd_print_relocation_records): Sanity check buffer offsets.
	(evax_bfd_print_address_fixups): Likewise.
	(evax_bfd_print_reference_fixups): Likewise.
2021-10-25 10:33:26 +10:30
GDB Administrator
30459fe4ef Automatic date update in version.in 2021-10-25 00:00:17 +00:00
Alan Modra
1ed0032b40 asan: c4x, c54x coff_canonicalize_reloc buffer overflow
Sometimes the investigation of a fuzzing bug report leads into areas
you'd rather not go.  In this instance by the time I'd figured out the
real cause was a target variant that had never been properly supported
in binutils, the time needed to fix it was less than the time needed
to rip it out.

	* coffcode.h (coff_set_alignment_hook): Call bfd_coff_swap_reloc_in
	not coff_swap_reloc_in.
	(coff_slurp_reloc_table): Likewise.  Don't use RELOC type.
	(ticoff0_swap_table): Use coff_swap_reloc_v0_out and
	coff_swap_reloc_v0_in.
	* coffswap.h (coff_swap_reloc_v0_in, coff_swap_reloc_v0_out): New.
	* coff-tic54x.c (tic54x_lookup_howto): Don't abort.
	* coffgen.c (coff_get_normalized_symtab): Use PTR_ADD.
	* bfd-in.h (PTR_ADD, NPTR_ADD): Avoid warnings when passing an
	expression.
	* bfd-in2.h: Regenerate.
2021-10-24 22:05:51 +10:30
Alan Modra
e028124942 asan: arm-darwin: buffer overflow
PR 21813
	* mach-o-arm.c (bfd_mach_o_arm_canonicalize_one_reloc): Sanity
	check PAIR reloc in other branch of condition as was done for
	PR21813.  Formatting.  Delete debug printf.
2021-10-24 21:45:23 +10:30
Alan Modra
3f9661f12a asan: aout: heap buffer overflow
* aoutx.h (aout_get_external_symbols): Sanity check before writing
	zero index entry.  Remove outdated comment.
	* pdp11.c (aout_get_external_symbols): Likewise.
2021-10-24 21:36:32 +10:30
liuzhensong
e214f8db56 LoongArch bfd support
2021-10-22  Chenghua Xu  <xuchenghua@loongson.cn>
	    Zhensong Liu  <liuzhensong@loongson.cn>
	    Weinan Liu  <liuweinan@loongson.cn>
bfd/
	* Makefile.am: Add LoongArch.
	* archures.c: Likewise.
	* config.bfd: Likewise.
	* configure.ac: Likewise.
	* cpu-loongarch.c: New.
	* elf-bfd.h: Add LoongArch.
	* elf.c: Add LoongArch elfcore_grok_xxx.
	* elfnn-loongarch.c: New.
	* elfxx-loongarch.c: New.
	* elfxx-loongarch.h: New.
	* reloc.c: Add LoongArch BFD RELOC ENUM.
	* targets.c: Add LoongArch target.
	* Makefile.in: Regenerate.
	* bfd-in2.h: Regenerate.
	* configure: Regenerate.
	* libbfd.h: Regenerate.
	* po/BLD-POTFILES.in: Regenerate.
	* po/SRC-POTFILES.in: Regenerate.

include/
	* elf/common.h: Add NT_LARCH_{CPUCFG,CSR,LSX,LASX}.
	* elf/loongarch.h: New.
2021-10-24 21:36:31 +10:30
GDB Administrator
e515d93264 Automatic date update in version.in 2021-10-24 00:00:16 +00:00
GDB Administrator
eaf4a97352 Automatic date update in version.in 2021-10-23 00:00:18 +00:00
Lewis Revill
9abcdc10b2 RISC-V: Don't separate pcgp relaxation to another relax pass.
Commit abd20cb637 and
ebdcad3fdd introduced additional
complexity into the paths run by the RISC-V relaxation pass in order to
resolve the issue of accurately keeping track of pcrel_hi and pcrel_lo
pairs. The first commit split up relaxation of these relocs into a pass
which occurred after other relaxations in order to prevent the situation
where bytes were deleted in between a pcrel_lo/pcrel_hi pair, inhibiting
our ability to find the corresponding pcrel_hi relocation from the
address attached to the pcrel_lo.

Since the relaxation was split into two passes the 'again' parameter
could not be used to perform the entire relaxation process again and so
the second commit added a way to restart ldelf_map_segments, thus
starting the whole process again.

Unfortunately this process could not account for the fact that we were
not finished with the relaxation process so in some cases - such as the
case where code would not fit in a memory region before the
R_RISCV_ALIGN relocation was relaxed - sanity checks in generic code
would fail.

This patch fixes all three of these concerns by reverting back to a
system of having only one target relax pass but updating entries in the
table of pcrel_hi/pcrel_lo relocs every time any bytes are deleted. Thus
we can keep track of the pairs accurately, and we can use the 'again'
parameter to restart the entire target relax pass, behaving in the way
that generic code expects. Unfortunately we must still have an
additional pass to delay deleting AUIPC bytes to avoid ambiguity between
pcrel_hi relocs stored in the table after deletion. This pass can only
be run once so we may potentially miss out on relaxation opportunities
but this is likely to be rare.

https://sourceware.org/bugzilla/show_bug.cgi?id=28410

bfd/
	* elfnn-riscv.c (riscv_elf_link_hash_table): Removed restart_relax.
	(riscv_elf_link_hash_table_create): Updated.
	(riscv_relax_delete_bytes): Moved after the riscv_update_pcgp_relocs.
	Update the pcgp_relocs table whenever bytes are deleted.
	(riscv_update_pcgp_relocs): Add function to update the section
	offset of pcrel_hi and pcrel_lo, and also update the symbol value
	of pcrel_hi.
	(_bfd_riscv_relax_call): Need to update the pcgp_relocs table
	when deleting codes.
	(_bfd_riscv_relax_lui): Likewise.
	(_bfd_riscv_relax_tls_le): Likewise.
	(_bfd_riscv_relax_align): Once we've handled an R_RISCV_ALIGN,
	we can't relax anything else, so set the sec->sec_flg0 to true.
	Besides, we don't need to update the pcgp_relocs table at this
	stage, so just pass NULL pointer as the pcgp_relocs table for
	riscv_relax_delete_bytes.
	(_bfd_riscv_relax_section): Use only one pass for all target
	relaxations.
	(_bfd_riscv_relax_delete): Likewise, we don't need to update
	the pcgp_relocs table at this stage, and don't need to set
	the `again' since restart_relax mechanism is abandoned.
	(bfd_elfNN_riscv_restart_relax_sections): Removed.
	(_bfd_riscv_relax_section): Updated.
	* elfxx-riscv.h (bfd_elf32_riscv_restart_relax_sections): Removed.
	(bfd_elf64_riscv_restart_relax_sections): Likewise.
ld/
	* emultempl/riscvelf.em: Revert restart_relax changes and set
	relax_pass to 3.
	* testsuite/ld-riscv-elf/align-small-region.d: New testcase.
	* testsuite/ld-riscv-elf/align-small-region.ld: Likewise.
	* testsuite/ld-riscv-elf/align-small-region.s: Likewise.
	* testsuite/ld-riscv-elf/restart-relax.d: Removed sine the
	restart_relax mechanism is abandoned.
	* testsuite/ld-riscv-elf/restart-relax.s: Likewise.
	* testsuite/ld-riscv-elf/ld-riscv-elf.exp: Updated.
2021-10-22 16:44:37 +08:00
GDB Administrator
46276d3ca2 Automatic date update in version.in 2021-10-22 00:00:17 +00:00
GDB Administrator
f4efb229b7 Automatic date update in version.in 2021-10-21 00:00:22 +00:00
GDB Administrator
e43321cab2 Automatic date update in version.in 2021-10-20 00:00:09 +00:00
Nick Clifton
cf487499e0 Fix a potential illegal memory access when testing for a special LTO symbol name.
bfd	* linker.c (_bfd_generic_link_add_one_symbol): Test for a NULL
	name before checking to see if the symbol is __gnu_lto_slim.
	* archive.c (_bfd_compute_and_write_armap): Likewise.
binutils
	* nm.c (filter_symbols): Test for a NULL name before checking to
	see if the symbol is __gnu_lto_slim.
	* objcopy.c (filter_symbols): Likewise.
2021-10-19 16:02:49 +01:00
GDB Administrator
d4ef5e75c7 Automatic date update in version.in 2021-10-19 00:00:14 +00:00
GDB Administrator
19b9612448 Automatic date update in version.in 2021-10-18 00:00:08 +00:00
GDB Administrator
31629daee5 Automatic date update in version.in 2021-10-17 00:00:19 +00:00
GDB Administrator
8457023a80 Automatic date update in version.in 2021-10-16 00:00:15 +00:00
GDB Administrator
f9ebf60b6f Automatic date update in version.in 2021-10-15 00:00:12 +00:00
GDB Administrator
cdb6026064 Automatic date update in version.in 2021-10-14 00:00:12 +00:00
GDB Administrator
777b054cf9 Automatic date update in version.in 2021-10-13 00:00:06 +00:00
GDB Administrator
255a531196 Automatic date update in version.in 2021-10-12 00:00:15 +00:00
Michael Forney
b6fca8a3d5 bfd: Remove use of void pointer arithmetic
This is not valid in ISO C. Instead, use a pointer to bfd_byte.

	* peicode.h (pe_bfd_object_p): Remove use of void pointer
	arithmetic.
2021-10-11 19:13:41 +10:30
GDB Administrator
88b3223704 Automatic date update in version.in 2021-10-11 00:00:13 +00:00
GDB Administrator
902ad3d703 Automatic date update in version.in 2021-10-10 00:00:09 +00:00
Guillermo E. Martinez
0161bdd47c PR28415, invalid read in xtensa_read_table_entries
PR 28415
	PR 28416
	* elf32-xtensa.c (xtensa_read_table_entries): Handle error
	return from retrieve_contents.
2021-10-09 14:02:07 +10:30
GDB Administrator
851a4f24d7 Automatic date update in version.in 2021-10-09 00:00:08 +00:00
GDB Administrator
fc6e66cd18 Automatic date update in version.in 2021-10-08 00:00:11 +00:00
Philipp Tomsich
9455c91957 RISC-V: Add support for Zbs instructions
This change adds the Zbs instructions from the Zbs 1.0.0 specification.
See
  https://github.com/riscv/riscv-bitmanip/releases/tag/1.0.0
for the frozen specification.

2021-01-09  Philipp Tomsich  <philipp.tomsich@vrull.eu>

    bfd/
	* elfxx-riscv.c (riscv_supported_std_z_ext): Added zbs.
    gas/
	* config/tc-riscv.c (riscv_multi_subset_supports): Handle INSN_CLASS_ZBS.
	* testsuite/gas/riscv/b-ext.d: Test Zbs instructions.
	* testsuite/gas/riscv/b-ext.s: Likewise.
	* testsuite/gas/riscv/b-ext-64.d: Likewise.
	* testsuite/gas/riscv/b-ext-64.s: Likewise.
    include/
	* opcode/riscv-opc.h: Added MASK/MATCH/DECLARE_INSN for Zbs.
	* opcode/riscv.h (riscv_insn_class): Added INSN_CLASS_ZBS.
    opcodes/
	* riscv-opc.c (riscv_supported_std_z_ext): Add zbs.

Signed-off-by: Philipp Tomsich <philipp.tomsich@vrull.eu>
2021-10-07 17:09:25 +08:00
Philipp Tomsich
586dcfc81e RISC-V: Update extension version for Zb[abc] to 1.0.0
2021-10-06  Philipp Tomsich  <philipp.tomsich@vrull.eu>

    bfd/
	* elfxx-riscv.c (riscv_supported_std_z_ext): Update the version
	number for zba, zbb and zbc to 1.0.0

Signed-off-by: Philipp Tomsich <philipp.tomsich@vrull.eu>

Version-changes: 3
- Updated version numbers for zba, zbb and zbc to 1.0.0
2021-10-07 17:09:21 +08:00
Alan Modra
6646834337 PR28423, use-after-free in objdump
XCOFF archives use a bi-directional linked list for file members.  So
one member points to both the previous member and the next member.
Members may not be sequentially ordered in the file.  This of course
is over-engineered nonsense and an attractive target for fuzzers.
(There is even a free list of members!)  The testcase in PR28423 is an
XCOFF archive with one member pointing to itself, which results in
lots of bad behaviour.  For example, "ar t" never terminates.

The use-after-free with "objdump -r" happens like this:  The first
archive element is opened, its symbols are read and "canonicalized"
for objdump, then relocations are read and printed.  Those relocations
use the canonicalized symbols, and also happen to be cached by the
coff bfd backend support.  objdump frees the symbols.  The next
archive element is then opened.  This must be done before the first
element is closed, because finding the next element uses data held in
the currect element.  Unfortunately the next element happens to be the
original, so we aren't opening, we're reopening a bfd which has cached
data.  When the relocations are printed they use the cached copy
containing references to the freed canonical symbols.

This patch adds a little sanity checking to the XCOFF "open next
archive file" support, so that it rejects archive members pointing at
themselves.  That is sufficient to cure this problem.  Anything more
is overkill.  If someone deliberately fuzzes an XCOFF archive with an
element loop then reports an "ar" bug when it runs forever, they will
find their bug report closed WONTFIX.

	PR 28423
	* coff-rs6000.c (_bfd_xcoff_read_ar_hdr): Save size occupied
	by member name in areltdata.extra_size.
	(_bfd_xcoff_openr_next_archived_file): Sanity check nextoff.
	* coff64-rs6000.c (xcoff64_openr_next_archived_file): Call
	_bfd_xcoff_openr_next_archived_file.
2021-10-07 14:23:14 +10:30
Alan Modra
6d661cdc5b PR28422, build_id use-after-free
This fixes a bug in commit 5d9bbb73c1.  All fields preserved from a
bfd in struct bfd_preserve need to be cleared in bfd_reinit.

	PR 28422
	* format.c (bfd_reinit): Clear build_id.
2021-10-07 14:23:14 +10:30
Alan Modra
89e62f7454 Change ridiculous section size error
Rather than reporting "memory exhausted", report "file truncated".
You can hit this error on small fuzzed object files, or on files that
are actually truncated.  In either case sizes can be such that an out
of memory error is a little confusing.

	* compress.c (bfd_get_full_section_contents): Set
	bfd_error_file_truncated rather than bfd_error_no_memory when
	section size exceeds file size.
2021-10-07 14:23:14 +10:30
GDB Administrator
82f2f39d81 Automatic date update in version.in 2021-10-07 00:00:09 +00:00
Alan Modra
1992e26ec4 PR28420, ecoff fuzzing failures
PR 28420
	* coff-mips.c (mips_adjust_reloc_in): Replace abort with error
	message and return.
	* ecoff.c (ecoff_slurp_reloc_table): Remove assertion and aborts,
	instead handle errors gracefully.
2021-10-06 18:00:00 +10:30