Commit Graph

9018 Commits

Author SHA1 Message Date
Alan Modra 89b599df37 alpha-coff: unitialised read
* coff-alpha.c (alpha_ecoff_get_elt_at_filepos): Correct bfd_bread
	return value check.
2020-03-31 15:04:21 +10:30
Alan Modra 8169954446 alpha-vms: sanity checks for image_write
* vms-alpha.c (image_write): Check bounds for sections without
	contents too.  Error on non-zero write to section without
	contents.
	(_bfd_vms_slurp_etir): Check return of image_write* functions.
2020-03-31 15:04:21 +10:30
Alan Modra b3b360dec7 tekhex: Uninitialised read
* tekhex.c (pass_over): Check is_eof before reading buffer.
2020-03-31 15:04:21 +10:30
Nick Clifton 00386881a3 Fix objcopy's --preserve-dates command line option so that it will work with PE format files.
PR binutils/pr25662
bfd	* libcoff-in.h (struct pe_tdata): Rename the insert_timestamp
	field to timestamp and make it an integer.
	* libcoff.h: Regenerate.
	* peXXigen.c (_bfd_XXi_only_swap_filehdr_out): Test the timestamp
	field in the pe_data structure rather than the insert_timestamp
	field.

binutils* objcopy.c (copy_object): When copying PE format files set the
	timestamp field in the pe_data structure if the preserve_dates
	flag is set.
	* testsuite/binutils-all/objcopy.exp (objcopy_test) Use
	--preserve-dates in place of the -p option, in order to make its
	effect more obvious.

ld	* emultempl/pe.em (after_open): Replace initialisation of the
	insert_timestamp field in the pe_data structure with an
	initialisation of the timestamp field.
	* emultemp/pep.em: Likewise.
	* pe-dll.c (fill_edata): Use the timestamp field in the pe_data
	structure instead of the insert_timestamp field.
2020-03-30 16:30:02 +01:00
Alan Modra 988b7300bc PR25745, powerpc64-ld overflows string buffer in --stats mode
PR 25745
	* elf64-ppc.c (ppc64_elf_build_stubs): Use asprintf to form
	statistics message.
2020-03-30 09:30:32 +10:30
Nick Clifton aa49fc22c1 Revert earlier delta adding bfd_coff_get_internal_extra_pe_aouthdr() function.
* cofflink.c (bfd_coff_get_internal_extra_pe_aouthdr): Delete.
	* libbfd-in.h (bfd_coff_get_internal_extra_pe_aouthdr): Remove
	prototype.
	* libbfd.h: Regenerate.
2020-03-26 10:46:25 +00:00
Alan Modra ff76851054 Re: i386msdos uninitialised read
Another fix.

	* i386msdos.c (msdos_object_p): Catch -1 return from bfd_bread.
2020-03-26 20:02:42 +10:30
Alan Modra f75fbe8ad2 alpha-vms: Sanity check ETIR__C_CTL_DFLOC index
I doubt anyone will want to create more than 16M debug location
entries.  If there is no bound the object format allows for 32-bit
indices and of course fuzzers find that and attempt allocation of up
to a 16G byte array.  The patch also fixes potential integer overflows
in calculating the array size.

	* vms-alpha.c (dst_define_location): Limit size of dst_ptr_offsets
	array.
	(_bfd_vms_slurp_object_records): Rename "err" to "ok".
2020-03-26 11:02:58 +10:30
Nick Clifton aac88046e6 Add a new function to the BFD library to allow users access to the COFF internal_extra_pe_outhdr structure.
* cofflink.c (bfd_coff_get_internal_extra_pe_aouthdr): New
	function.
	* libbfd-in.h (bfd_coff_get_internal_extra_pe_aouthdr): Prototype.
	* libbfd.h: Regenerate.
2020-03-25 11:58:49 +00:00
Shahab Vahedi cf2611febc arc: Use correct string when printing bfd DEBUG data
PRINT_DEBUG_RELOC_INFO_BEFORE() macro prints bunch of parameters
for debugging purposes.  Due to a seemingly copy/paste mistake,
the "input_section->vma" is printed under the field name
"symbol_section->vma".  This commit fixes that.

This fix is a courtesy of xiangzhai.

	* elf32-arc.c (PRINT_DEBUG_RELOC_INFO_BEFORE): Use the
	correct field name in the output string.
2020-03-25 15:40:49 +10:30
Alan Modra d16e3d2e5b PR25662, invalid sh_offset for first section in segment with phdrs
PR 25662
	* elf.c (assign_file_positions_for_load_sections): Adjust offset
	for SHT_NOBITS section if first in segment.
2020-03-25 14:51:41 +10:30
H.J. Lu 1081065c51 bfd: Add a bfd_boolean argument to bfd_get_symbol_version_string
We can't call _bfd_elf_get_symbol_version_name from nm.c since it isn't
available for all target configurations.  This patch add a bfd_boolean
argument to bfd_get_symbol_version_string instead.

bfd/

	PR binutils/25708
	* elf-bfd.h (_bfd_elf_get_symbol_version_name): Renamed to ...
	(_bfd_elf_get_symbol_version_string): This.
	* elf.c (_bfd_elf_get_symbol_version_name): Renamed to ...
	(_bfd_elf_get_symbol_version_string): This.
	(bfd_elf_print_symbol): Pass TRUE to
	_bfd_elf_get_symbol_version_string.
	* libbfd-in.h (_bfd_nosymbols_get_symbol_version_string): Add a
	bfd_boolean argument.
	* syms.c (_bfd_nosymbols_get_symbol_version_string): Likewise.
	* targets.c  (_bfd_get_symbol_version_string): Likewise.
	(bfd_get_symbol_version_string): Likewise.
	* bfd-in2.h: Regenerated.

binutils/

	PR binutils/25708
	* nm.c (print_symname): Replace _bfd_elf_get_symbol_version_name
	with bfd_get_symbol_version_string.
	(print_symbo): Pass TRUE to bfd_get_symbol_version_string.
	* objdump.c (objdump_print_symname): Likewise.
2020-03-24 15:37:26 -07:00
Nick Clifton e11cd7c491 Update changelog. 2020-03-24 15:25:36 +00:00
Nick Clifton 0b8448af68 Add code to the BFD library to handle opening files with pathnames longer than MAX_PATH on Win32 systems.
PR 25713
	* bfdio.c (_bfd_real_fopen): Add code to handle long filenames on
	Win32 systems.
2020-03-24 15:24:02 +00:00
Nick Clifton ec2e748ad3 Fix assertion failure in the BFD library when linking with --emit-relocs enabled.
PR 25681
	* elf.c (_bfd_elf_map_sections_to_segments): When looking for a
	segment to use for PT_GNU_RELRO, ignore empty sections in a
	segment's current list.
2020-03-24 13:35:53 +00:00
H.J. Lu cda7e5603f bfd: Change num_group to unsigned int
elf.c failed with to with GCC 10 as of

commit 906b3eb9df6c577d3f6e9c3ea5c9d7e4d1e90536
Author: Martin Liska <mliska@suse.cz>
Date:   Tue Mar 24 11:40:10 2020 +0100

    Improve endianess detection.

            PR lto/94249
            * plugin-api.h: Add more robust endianess detection.

binutils-gdb/bfd/elf.c: In function ‘setup_group’:
binutils-gdb/bfd/elf.c:740:35: error: overflow in conversion from ‘unsigned int’ to ‘int’ changes value from ‘num_group = 4294967295’ to ‘-1’ [-Werror=overflow]
  740 |     elf_tdata (abfd)->num_group = num_group = -1;
      |                                   ^~~~~~~~~
cc1: all warnings being treated as errors
make[2]: *** [Makefile:1608: elf.lo] Error 1

Change num_group in elf_obj_tdata to unsigned int to compile with GCC 10.

	PR binutils/25717
	* elf-bfd.h (elf_obj_tdata): Change num_group to unsigned int.
2020-03-24 04:52:39 -07:00
H.J. Lu 7e6e972f74 bfd: Display symbol version for nm -D
Extend _bfd_elf_get_symbol_version_string for nm -D to display symbol
version.  _bfd_elf_get_symbol_version_name is added to avoid updating
all XXX_get_symbol_version_string functions.

bfd/

	PR binutils/25708
	* elf-bfd.h (_bfd_elf_get_symbol_version_name): New.
	* elf.c (_bfd_elf_get_symbol_version_name): New function.  Based
	on the previous _bfd_elf_get_symbol_version_string.
	(_bfd_elf_get_symbol_version_string): Use it.

binutils/

	PR binutils/25708
	* nm.c (SYM_NAME): Removed.
	(print_symname): Add a pointer to struct extended_symbol_info
	argument.  Call _bfd_elf_get_symbol_version_name to get symbol
	version.
	(print_symdef_entry): Pass NULL to print_symname.
	(print_symbol_info_bsd): Update call to print_symname.
	(print_symbol_info_sysv): Likewise.
	(print_symbol_info_posix): Likewise.

ld/

	PR binutils/25708
	* testsuite/ld-elf/pr25708.d: New file.
2020-03-24 04:23:11 -07:00
Alan Modra 65109548f8 Overlarge allocation in _bfd_generic_read_ar_hdr_mag
* archive.c (_bfd_generic_read_ar_hdr_mag): Sanity check extended
	name size.  Use bfd_malloc rather than bfd_zmalloc, clearing just
	struct areltdata.
2020-03-24 19:00:35 +10:30
Sebastian Huber fdde2fb60c Mention .tdata in comment in _bfd_elf_tls_setup()
This helps to find code areas which deal with the .tdata section.

bfd/

    	* elflink.c (_bfd_elf_tls_setup): Mention .tdata in comment.
2020-03-23 17:04:28 +01:00
Alan Modra cf28cfef60 ECOFF archive uninitialised read
* ecoff.c (_bfd_ecoff_slurp_armap): Sanity check parsed_size and
	symbol count.  Allocate an extra byte to ensure name strings
	are terminated.  Sanity check name offsets.  Release memory on
	error return.
2020-03-23 23:26:12 +10:30
Alan Modra 5e737279c6 i386msdos uninitialised read
Also reinstate ld i386aout for i386-msdos target, which doesn't build
otherwise.

bfd/
	* i386msdos.c (msdos_object_p): Don't access e_lfanew when that
	field hasn't been read.  Remove unnecessary casts.
ld/
	* Makefile.am (ALL_EMULATION_SOURCES): Reinstate ei386aout.c.
	Include ei386aout dep file.
	* Makefile.in: Regenerate.
	* po/BLD-POTFILES.in: Regenerate.
2020-03-23 23:26:12 +10:30
Alan Modra c15a8f173e XCOFF64 uninitialised read
Like git commit 67338173a4.

	* coff64-rs6000.c (xcoff64_slurp_armap): Ensure size is large
	enough to read number of symbols.
2020-03-22 23:22:13 +10:30
H.J. Lu 3d98c46092 plugin: Don't invoke LTO-wrapper
Don't invoke LTO-wrapper since the LTO wrapper approach is not only
slow but also unreliable.  For GCC 10 or newer, LDPT_ADD_SYMBOLS_V2
will be used.

bfd/

	* configure.ac (HAVE_EXECUTABLE_SUFFIX): Removed.
	(EXECUTABLE_SUFFIX): Likewise.
	* config.in: Regenerated.
	* configure: Likewise.
	* plugin.c (bfd_plugin_close_and_cleanup): Defined as
	_bfd_generic_close_and_cleanup.
	(plugin_list_entry): Remove resolution_file, resolution_option,
	real_bfd, real_nsyms, real_syms, lto_nsyms, lto_syms, gcc,
	lto_wrapper, gcc_env and initialized,
	(need_lto_wrapper_p): Removed.
	(get_lto_wrapper): Likewise.
	(setup_lto_wrapper_env): Likewise.
	(register_all_symbols_read): Likewise.
	(egister_cleanup): Likewise.
	(get_symbols): Likewise.
	(add_input_file): Likewise.
	(bfd_plugin_set_program_name): Remove need_lto_wrapper.
	(add_symbols): Updated.
	(try_claim): Likewise.
	(try_load_plugin): Likewise.
	(bfd_plugin_canonicalize_symtab): Likewise.
	* plugin.h (bfd_plugin_set_program_name): Remove int argument.
	(plugin_data_struct): Remove real_bfd, real_nsyms and real_syms.

binutils/

	* ar.c (main): Update bfd_plugin_set_program_name call.
	* nm.c (main): Likewise.

ld/

	* testsuite/ld-plugin/lto.exp (lto_link_tests): Run PR ld/25355
	test only for GCC 10 or newer.
2020-03-20 03:55:30 -07:00
H.J. Lu c3a1714ce7 plugin: Use LDPT_ADD_SYMBOLS_V2 to get symbol type
Since LTO plugin may generate more than one ltrans.o file from one input
IR object as LTO wrapper ignores -flto-partition=none:

lto-wrapper.c:608:

   604          /* Drop arguments that we want to take from the link line.  */
   605          case OPT_flto_:
   606          case OPT_flto:
   607          case OPT_flto_partition_:
   608            continue;

the LTO wrapper approach is not only slow but also unreliable.  Since
the LTO plugin API has been extended to add LDPT_ADD_SYMBOLS_V2 with
symbol type and section kind, we can use LDPT_ADD_SYMBOLS_V2 to get
symbol type, instead of invoking the LTO wrapper.

	PR binutils/25640
	* plugin.c (plugin_list_entry): Add has_symbol_type.
	(add_symbols_v2): New function.
	(bfd_plugin_open_input): Don't invoke LTO wrapper if LTO plugin
	provides symbol type.
	(try_load_plugin): Add LDPT_ADD_SYMBOLS_V2.
	(bfd_plugin_canonicalize_symtab): Use LTO plugin symbol type if
	available.
2020-03-19 21:00:58 -07:00
Alan Modra 67338173a4 XCOFF uninitialized read
* coff-rs6000.c (_bfd_xcoff_slurp_armap): Ensure size is large
	enough to read number of symbols.
2020-03-20 12:35:51 +10:30
Alan Modra a859124df2 tidy elf_backend calls
Function pointers in elfNN_bed that are initialized by elfxx-target.h
to non-zero values generally don't need a non-NULL test before calling
them.  Targets don't set a non-NULL function to NULL.  The one
exception being elfnn-ia64.c and that exception is removed here.

	* elf.c (_bfd_elf_setup_sections): Don't test known non-NULL
	backend functions for NULL before calling.
	(copy_special_section_fields, _bfd_elf_copy_private_bfd_data),
	(bfd_section_from_shdr, assign_section_numbers): Likewise.
	* elfcode.h (elf_write_relocs, elf_slurp_reloc_table): Likewise.
	* elfnn-ia64.c (ignore_errors): New function.
	(elf_backend_link_order_error_handler): Redefine as ignore_errors.
2020-03-20 11:02:49 +10:30
Nick Clifton f3a08f7778 Fix discrepancies in nm's --line-number output by adding support for the DW_AT_specification DWARF Attttribute.
PR 25676
bfd	* dwarf2.c (struct varinfo): Add unit_offset field to record the
	location of the varinfo in the unit's debug info data.  Change the
	type of the stack field to a boolean.
	(lookup_var_by_offset): New function.  Returns the varinfo
	structure for the variable described at the given offset in the
	unit's debug info.
	(scan_unit_for_symbols): Add support for variables which have the
	DW_AT_specification attribute.

binutils* testsuite/binutils-all/dw4.s: New test source file.
	* testsuite/binutils-all/nm.exp: Run the new test.
2020-03-19 16:55:13 +00:00
Nick Clifton 6a541707f3 Fix assertion failure in the BFD library when called to parse a file containing corrupt ELF group information.
PR 25699
	* elf.c (bfd_elf_set_group_contents): Replace assertion with an
	error return.
2020-03-19 14:40:00 +00:00
Sebastian Huber effc14f54c Additional c99 elfxx-riscv.c fix
Similar to 2d0e121701.

bfd/

    	* elfxx-riscv.c (riscv_parse_subset): Don't use C99.
2020-03-19 07:07:11 +01:00
Nick Clifton ac4bf06ca2 Fix seg-fault in strip when copying a file containing corrupt secondary relocs.
PR 25673
	* elf.c (_bfd_elf_write_secondary_reloc_section): Fix illegal
	memory access when processing a corrupt secondary reloc section.
2020-03-18 12:12:07 +00:00
Christophe Lyon 53215f214c Non-contiguous memory regions support: Avoid calls to abort
Use '%F' format when printing error messages to exit cleanly rather
than by calling abort().

2020-03-18  Christophe Lyon  <christophe.lyon@linaro.org>

	bfd/
	* elf32-arm.c (arm_build_one_stub): Emit a fatal error message
	instead of calling abort.
	* elf32-csky.c (csky_build_one_stub): Likewise.
	* elf32-hppa.c (hppa_build_one_stub): Likewise.
	* elf32-m68hc11.c (m68hc11_elf_build_one_stub): Likewise.
	* elf32-m68hc12.c (m68hc12_elf_build_one_stub): Likewise.
	* elf32-metag.c (metag_build_one_stub): Likewise.
	* elf32-nios2.c (nios2_build_one_stub): Likewise.
	* elf64-ppc.c (ppc_build_one_stub): Likewise.
	(ppc_size_one_stub): Likewise.
	* elfnn-aarch64.c (aarch64_build_one_stub): Likewise.

	ld/
	* emultempl/xtensaelf.em: Emit a fatal error message
	instead of calling abort.
	* ldlang.c: Likewise.

Change-Id: I60deaeeee59d4e7cab06b8a40a3e51837c43a8ab
2020-03-18 10:09:43 +00:00
Nick Clifton d3c3c54293 Fix PR number in previous delta 2020-03-17 17:03:53 +00:00
Nick Clifton 327ef784ba Replace a couple of assertions in the BFD library that can be triggered by attempts to parse corrupt input files.
PR 25633
	* elf.c (_bfd_elf_copy_special_section_fields): Replace assertions
	with error messages.
2020-03-17 17:02:15 +00:00
Nick Clifton ecbbbdba71 Remove a double free in the BFD library triggered when parsing a corrupt file.
PR 25687
	* elf.c (_bfd_elf_slurp_secondary_reloc_section): Remove redundant
	free.  Add free on another failure path.
2020-03-17 16:45:07 +00:00
Alan Modra 4b3ecb3b91 PR25675: SIGSEGV in bfd_octets_per_byte
PR 25675
	* elf.c (elf_sort_segments): Don't call bfd_octets_per_byte unless
	we have a non-zero section count.  Do lma comparison in octets.
2020-03-16 19:35:12 +10:30
Alan Modra 7bac4137d7 asan: alpha-vms: null dereference
* vms-alpha.c (dst_restore_location): Validate index into
	dst_ptr_offsets array before accessing.  Return status.
	(dst_retrieve_location): Similarly, making "loc" parameter a
	pointer to return value.
	(_bfd_vms_slurp_etir): Update calls to above functions.
2020-03-16 10:51:53 +10:30
Kamil Rytarowski 8b5d0a4f6f Include netbsd-core.lo for all arm/mips NetBSD targets
bfd/ChangeLog:

	* configure.ac: Include netbsd-core.lo for all NetBSD arm and mips
	targets.
	* configure: Regenerated.
2020-03-14 12:01:55 +01:00
Alan Modra 48e81d7f63 BFD_FAKE_SECTIONS formatting
After the ld non-contiguous memory support my regen of bfd-in2.h
didn't match exactly what was committed, so I took the opportunity to
line up all the comments.

	* section.c (BFD_FAKE_SECTIONS): Formatting.
	* bfd-in2.h: Regenerate.
2020-03-14 17:23:56 +10:30
Kamil Rytarowski 06d949ec31 Implement NT_NETBSDCORE_LWPSTATUS (NetBSD-Core)
bfd/ChangeLog:

	* elf.c (elfcore_grok_netbsd_note): Add support for
	NT_NETBSDCORE_LWPSTATUS notes.

binutils/ChangeLog:

	* readelf.c (get_netbsd_elfcore_note_type): Add support for
	NT_NETBSDCORE_LWPSTATUS notes.

include/ChangeLog:

	* elf/common.h (NT_NETBSDCORE_LWPSTATUS): New define.
2020-03-14 00:31:16 +01:00
Christophe Lyon abf874aafe Add support for non-contiguous memory regions
2020-01-06  Christophe Lyon  <christophe.lyon@linaro.org>

	bfd/
	* bfd-in2.h: Regenerate.
	* section.c (asection): Add already_assigned field.
	(BFD_FAKE_SECTION): Add default initializer for it.
	* ecoff.c (bfd_debug_section): Initialize already_assigned field.
	* elf32-arm.c (arm_build_one_stub): Add support for
	non_contiguous_regions.
	* elf32-csky.c (csky_build_one_stub): Likewise.
	* elf32-hppa.c (hppa_build_one_stub): Likewise.
	* elf32-m68hc11.c (m68hc11_elf_build_one_stub): Likewise.
	* elf32-m68hc12.c (m68hc12_elf_build_one_stub): Likewise.
	* elf32-metag.c (metag_build_one_stub): Likewise.
	* elf32-nios2.c (nios2_build_one_stub): Likewise.
	* elf64-ppc.c (ppc_build_one_stub): Likewise.
	(ppc_size_one_stub): Likewise.
	* elfnn-aarch64.c (aarch64_build_one_stub): Likewise.
	* elflink.c (elf_link_input_bfd): Likewise.

	include/
	* bfdlink.h (bfd_link_info): Add non_contiguous_regions and
	non_contiguous_regions_warnings fields.

	ld/
	* ldlang.c (lang_add_section): Add support for
	non_contiguous_regions.
	(size_input_section): Likewise.
	(lang_size_sections_1): Likewise.
	(process_insert_statements): Likewise.
	* ldlex.h (option_values): Add OPTION_NON_CONTIGUOUS_REGIONS and
	OPTION_NON_CONTIGUOUS_REGIONS_WARNINGS.
	* lexsup.c (ld_options): Add entries for
	--enable-non-contiguous-regions and
	--enable-non-contiguous-regions-warnings.
	(parse_args): Handle it.
	* NEWS: Add --enable-non-contiguous-regions and
	--enable-non-contiguous-regions-warnings.
	* ld.texi: Add --enable-non-contiguous-regions and
	--enable-non-contiguous-regions-warnings documentation.
	* emultempl/armelf.em (elf32_arm_add_stub_section): Add
	SEC_LINKER_CREATED flag.
	* emultempl/xtensaelf.em (ld_build_required_section_dependence):
	Emit an error when --enable-non-contiguous-regions is used.
	* testsuite/ld-elf/non-contiguous.d: New.
	* testsuite/ld-elf/non-contiguous.ld: New.
	* testsuite/ld-elf/non-contiguous.s: New.
	* testsuite/ld-arm/arm-elf.exp: Run the new tests.
	* testsuite/ld-arm/arm-elf/non-contiguous-arm.s: New.
	* testsuite/ld-arm/arm-elf/non-contiguous-arm.d: New.
	* testsuite/ld-arm/arm-elf/non-contiguous-arm.ld: New.
	* testsuite/ld-arm/arm-elf/non-contiguous-arm2.d: New.
	* testsuite/ld-arm/arm-elf/non-contiguous-arm3.ld: New.
	* testsuite/ld-arm/arm-elf/non-contiguous-arm3.d: New.
	* testsuite/ld-arm/arm-elf/non-contiguous-arm3.ld: New.
	* testsuite/ld-arm/arm-elf/non-contiguous-arm4.d: New.
	* testsuite/ld-arm/arm-elf/non-contiguous-arm4.ld: New.
	* testsuite/ld-arm/arm-elf/non-contiguous-arm5.d: New.
	* testsuite/ld-arm/arm-elf/non-contiguous-arm5.ld: New.
	* testsuite/ld-arm/arm-elf/non-contiguous-arm6.d: New.
	* testsuite/ld-arm/arm-elf/non-contiguous-arm6.ld: New.
	* testsuite/ld-powerpc/powerpc.exp: Run new tests.
	* testsuite/ld-powerpc/non-contiguous-powerpc.d: New.
	* testsuite/ld-powerpc/non-contiguous-powerpc.ld: New.
	* testsuite/ld-powerpc/non-contiguous-powerpc.sd: New.
	* testsuite/ld-powerpc/non-contiguous-powerpc64.d: New.
2020-03-13 14:44:45 +00:00
H.J. Lu 74e10d1742 x86: Check static link of dynamic objects
On Linux/x86, when -static is passed to gcc, gcc passes it to linker
before all input files suitable for creating static executable.  X86
linker will report error for dynamic input objects if -static is passed
at command-line before all input files without --dynamic-linker unless
--no-dynamic-linker is used.

bfd/

	PR ld/24920
	* elf-linker-x86.h (elf_linker_x86_params): Add
	static_before_all_inputs and has_dynamic_linker.
	* elfxx-x86.c (_bfd_x86_elf_link_setup_gnu_properties): Report
	dynamic input objects if -static is passed at command-line
	before all input files without --dynamic-linker unless
	--no-dynamic-linker is used.

ld/

	PR ld/24920
	* emulparams/elf32_x86_64.sh: Use static.sh.
	* emulparams/elf_i386.sh: Likewise.
	* emulparams/elf_x86_64.sh: Likewise.
	* emulparams/static.sh: New file.
	* emultempl/elf-x86.em: Include "ldlex.h".
	* testsuite/ld-elf/pr24920.err: New file.
	* testsuite/ld-elf/linux-x86.exp: Run ld/24920 tests.
2020-03-13 07:39:06 -07:00
Kamil Rytarowski 015ec493d8 Recognize aarch64 PT_GETREGS and PT_GETFPREGS notes on NetBSD
* elf.c (elfcore_grok_netbsd_note): Add support for aarch64.
2020-03-13 14:16:35 +01:00
Christian Eggers 666318230c Fix several mix up between octets and bytes in ELF program headers
Fixes additional locations not handled in the first patch.

When converting between addresses in ELF headers [octets] and bfd
LMA/VMA [bytes], the number of octets per byte needs to be incorporated.

include/
	* bfdlink.h (struct bfd_link_order): Add unit (bytes/octets) to
	offset and size members.
	* elf/internal.h (struct elf_internal_phdr): Likewise for
	p_align member.
	(struct elf_segment_map): Likewise for p_paddr and p_size
	members
bfd/
	* bfd.c (bfd_record_phdr): New local "opb".  Fix assignment of
	"p_paddr" from "at".
	* elfcode.h (bfd_from_remote_memory): Add units to several
	parameters.  New local "opb".  Fix usage of p_align.  Fix
	calculation of "localbase" from "ehdr_vma" and "p_vaddr".  Fix
	call of target_read_memory.
	* elflink.c (elf_fixup_link_order): Fix scope of "s" local.  Fix
	calculation of "offset" and "output_offset".
	(bfd_elf_final_link): New local "opb".  Fix calculation of "size"
	from "offset" and fix calculation of "end" from "vma+size".  Fix
	comparison between "sh_addr" and "vma"/"output_offset".
	(bfd_elf_discard_info): Fix calculation of "eh_alignment".
	* elf-bfd.h (struct elf_link_hash_table): Add unit to tls_size
	member.
	* elf.c (_bfd_elf_map_sections_to_segments): Add unit (bytes/
	octets) to "wrap_to2 and "phdr_size" locals.  Fix calculation of
	"wrap_to" value.  Add unit (bytes) to phdr_lma variable.  Fix
	assignment of p_paddr from phdr_lma.  Fix comparison between
	"lma+size" and "next->lma".
	(elf_sort_segments): Fix assignment from p_paddr to lma.
	(assign_file_positions_for_load_sections): Add unit (bytes) to
	local "align".  Fix calculation of local "off_adjust".  Fix
	calculation of local "filehdr_vaddr".
	(assign_file_positions_for_non_load_sections): New local "opb".
	Fix calculation of "end" from "p_size". Fix comparison between
	"vma+SECTION_SIZE" and "start".  Fix calculation of "p_memsz"
	from "end" and "p_vaddr".
	(rewrite_elf_program_header): Fix comparison between p_vaddr and
	vma.  Fix assignment to p_paddr from lma.  Fix comparison between
	p_paddr and lma.  Fix assignment to p_paddr from lma.
	* merge.c (sec_merge_emit): New local "opb". Convert
	"alignment_power" to octets.
	(_bfd_add_merge_section): New locals "alignment_power" and
	"opb".  Fix comparison between "alignment_power" and
	"sizeof(align)".
	(_bfd_merge_sections): New local "opb".  Divide size by opb
	before checking align mask.
2020-03-13 15:48:01 +10:30
Christian Eggers 502794d432 Fix several mix up between octets and bytes in ELF program headers
When converting between addresses in ELF headers [octets] and bfd
LMA/VMA [bytes], the number of octets per byte needs to be
incorporated.

In ld, the SIZEOF_HEADERS linker script statement must be resolved to
bytes instead of octets.

include/
	* elf/internal.h (struct elf_internal_phdr): Add unit (octets)
	to several member field comments.
	(Elf_Internal_Shdr): likewise.
bfd/
	* elf.c (_bfd_elf_make_section_from_shdr): Introduce new temp
	opb.  Divide Elf_Internal_Shdr::sh_addr by opb when setting
	section LMA/VMA.
	(_bfd_elf_make_section_from_phdr): Similarly.
	(elf_fake_sections): Fix calculation of
	Elf_Internal_shdr::sh_addr from section VMA.
	(_bfd_elf_map_sections_to_segments): Fix mixup between octets
	and bytes.
	(assign_file_positions_for_load_sections): Fix calculations of
	Elf_Internal_shdr::p_vaddr and p_paddr from section LMA/VMA.  Fix
	comparison between program header address and section LMA.
	(assign_file_positions_for_non_load_sections): Likewise.
	(rewrite_elf_program_header): Likewise.  Introduce new temp opb.
	(IS_CONTAINED_BY_VMA): Add parameter opb.
	(IS_CONTAINED_BY_LMA,IS_SECTION_IN_INPUT_SEGMENT,
	INCLUDE_SECTION_IN_SEGMENT): Likewise.
	(copy_elf_program_header): Update call to ELF_SECTION_IN_SEGMENT.
	Fix calculations of p_addr_valid and p_vaddr_offset.
	* elflink.c (elf_link_add_object_symbols): Multiply section VMA
	with octets per byte when comparing against p_vaddr.
ld/
	* ldexp.c (fold_name): Return SIZEOF_HEADERS in bytes.
2020-03-13 15:37:11 +10:30
Alan Modra 8248d21a5b asan: som: unknown read
* som.c (setup_sections): Sanity check subspace.name.
2020-03-11 23:02:51 +10:30
Alan Modra 435edf0bf2 powerpc64-ld infinite loop
If this code dealing with possible conversion of inline plt sequences
is ever executed, ld will hang.  A binary with such sequences and of
code size larger than approximately 90% the reach of an unconditional
branch is the trigger.  Oops.

	* elf64-ppc.c (ppc64_elf_inline_plt): Do increment rel in for loop.
2020-03-11 15:01:15 +10:30
Alan Modra 6b5e16ffd3 PR25648, objcopy SIGSEGV in ihex_write_record
ihex_set_section_contents sorts records stored on the tdata.ihex_data
list by address, but ihex_write_object_contents went too far in
assuming they were not overlapping.  This patch fixes the problem by
not assuming anything about addresses in ihex_write_object_contents.

	PR 25648
	* ihex.c (ihex_write_object_contents): Don't assume ordering of
	addresses here.  Remove dead code.
2020-03-10 11:05:07 +10:30
Alan Modra 2f57795b8b asan: wasm: Out-of-memory
* wasm-module.c (wasm_scan): Sanity check file name length
	before allocating memory.  Move common section setup code.  Do
	without bfd_tell to calculate section size.
2020-03-09 10:10:36 +10:30
Nick Clifton a0dcf29705 Fix an abort triggered when objcopy is used to set the "share" section flag on an ELF section.
binutils* objcopy.c (check_new_section_flags): New function.  Reject the
	SEC_COFF_SHARED flag if the target is not a COFF binary.
	(copy_object): Call check_new_section_flags.
	(setup_section): Likewise.
	* doc/binutils.texi (objcopy): Add a note that the 'share' section
	flag cannot be applied to ELF binaries.

bfd	* elf.c (_bfd_elf_set_section_contents): Replace call to abort
	with error messages and failure return values.
2020-03-06 10:09:22 +00:00
Max Filippov e15a8da9c7 bfd: xtensa: fix PR ld/25630
bfd/
2020-03-05  Max Filippov  <jcmvbkbc@gmail.com>

	* elf32-xtensa.c (shrink_dynamic_reloc_sections): Shrink dynamic
	relocation sections for any removed reference to a dynamic symbol.
2020-03-05 19:48:08 -08:00
Nick Clifton a8e14f4cc2 Add support for ELF files which contain multiple reloc sections which all target the same section.
* elf-bfd.h (struct elf_backend_data): Add new fields:
	init_secondary_reloc_section, slurp_secondary_reloc_section,
	write_secondary_reloc_section.
	(_bfd_elf_init_secondary_reloc_section): Prototype.
	(_bfd_elf_slurp_secondary_reloc_section): Prototype.
	(_bfd_elf_write_secondary_reloc_section): Prototype.
	* elf.c ( bfd_section_from_shdr): Invoke the new
	init_secondary_reloc_section backend function, if defined, when a
	second reloc section is encountered.
	(swap_out_syms): Invoke the new symbol_section_index function, if
	defined, when computing the section index of an OS/PROC specific
	symbol.
	(_bfd_elf_init_secondary_reloc_section): New function.
	(_bfd_elf_slurp_secondary_reloc_section): New function.
	(_bfd_elf_write_secondary_reloc_section): New function.
	(_bfd_elf_copy_special_section_fields): New function.
	* elfcode.h (elf_write_relocs): Invoke the new
	write_secondary_relocs function, if defined, in order to emit
	secondary relocs.
	(elf_slurp_reloc_table): Invoke the new slurp_secondary_relocs
	function, if defined, in order to read in secondary relocs.
	* elfxx-target.h (elf_backend_copy_special_section_fields):
	Provide a non-NULL default definition.
	(elf_backend_init_secondary_reloc_section): Likewise.
	(elf_backend_slurp_secondary_reloc_section): Likewise.
	(elf_backend_write_secondary_reloc_section): Likewise.
	(struct elf_backend_data elfNN_bed): Add initialisers for the new
	fields.
        * configure.ac (score_elf32_[bl]e_vec): Add elf64.lo
        * configure: Regenerate.
2020-03-05 15:47:15 +00:00
Alan Modra 6f8f95b4c4 Large memory allocation reading fuzzed 64-bit archive
This patch adds a sanity check for the size of an armap.

	* archive64.c (_bfd_archive_64_bit_slurp_armap): Check parsed_size
	against file size before allocating memory.  Use bfd_alloc rather
	than bfd_zalloc for carsym/strings memory.
2020-03-05 11:15:55 +10:30
Alan Modra 233bf4f847 sh_addralign inconsistent with sh_addr
The ELF gABI says in part of sh_addralign:  "The value of sh_addr must
be congruent to 0, modulo the value of sh_addralign."

	* elf.c (elf_fake_sections): Ensure sh_addralign is such that
	sh_addr mod sh_addalign is zero.
2020-03-04 15:31:03 +10:30
Alan Modra 1039fd9ac2 Call cleanup on bfd_check_format_matches error exit
* format.c (bfd_check_format_matches): Call cleanup on error exit.
2020-03-04 11:40:01 +10:30
Alan Modra f57140990f bfd_check_format_matches preserving matches vs. cleanups
It didn't take long for oss-fuzz to find double frees due to a bug in
the cleanup logic.  It's seen when reading in any alpha-vms object
file except when alpha_vms_vec is the default.  But alpha_vms_vec is
of course the default when building for --target=alpha-dec-vms (and
naturally what I used to  test the cleanup support since that is the
only target with a cleanup that does anything currently).

Anyway, the bug is that if bfd_check_format_matches is to preserve a
match the cleanup for that match can't be run.  Quite obviously that
would destroy part of the match state.

	* format.c (struct bfd_preserve): Add cleanup field.
	(bfd_preserve_save): Add cleanup param and save.
	(bfd_preserve_restore): Return cleanup.
	(bfd_preserve_finish): Call the cleanup for the discarded match.
	(bfd_check_format_matches): Pass cleanup to bfd_preserve_save,
	and clear when preserving a match.  Restore cleanup too when
	restoring that match.
2020-03-03 21:45:01 +10:30
Alan Modra 40b35c78b6 Re: bfd_cleanup for object_p
I hate files that you can't compile.

	* cisco-core.c (cisco_core_file_p): Return bfd_cleanup.
	* hpux-core.c (hpux_core_core_file_p): Update prototype.
	* sco5-core.c (sco5_core_file_p): Return bfd_cleanup.
	(core_sco5_vec): Correct initialisers.
2020-03-03 00:12:44 +10:30
Alan Modra 601b73d500 Re: bfd_cleanup for object_p
More missing core file support changes.

	* aix386-core.c (aix386_core_file_p): Return bfd_cleanup.
	* aix5ppc-core.c (xcoff64_core_p): Likewise.
	* cisco-core.c (cisco_core_file_validate): Likewise.
	* hppabsd-core.c (hppabsd_core_core_file_p): Likewise.
	* hpux-core.c (hpux_core_core_file_p): Likewise.
	* irix-core.c (irix_core_core_file_p): Likewise.
	* lynx-core.c (lynx_core_file_p): Likewise.
	* netbsd-core.c (netbsd_core_file_p): Likewise.
	* osf-core.c (osf_core_core_file_p): Likewise.
	* ptrace-core.c (ptrace_unix_core_file_p): Likewise.
	* sco5-core.c (sco5_core_file_p): Likewise.
2020-03-02 23:49:03 +10:30
H.J. Lu 728d32c496 trad_unix_core_file_p: Return bfd_cleanup
* trad-core.c (trad_unix_core_file_p): Return bfd_cleanup.
2020-03-02 04:35:23 -08:00
Alan Modra cb001c0d28 bfd_cleanup for object_p
The object_p (and archive_p, core_file_p) functions are not supposed
to have any target specific malloc'd memory attached to the bfd on
their return.  This should be obvious on a failure return, but it's
also true for a successful return.  The reason is that even though the
object_p recognises the file, that particular target may not be used
and thus the bfd won't be closed calling close_and_cleanup for the
target that allocated the memory.

It turns out that the object_p bfd_target* return value isn't needed.
In all cases except ld/plugin.c the target is abfd->xvec and with
ld/plugin.c the target isn't used.  So this patch returns a cleanup
function from object_p instead, called in bfd_check_format_matches to
tidy the bfd before trying a different target match.  The only cleanup
that does anything at this stage is the alpha-vms one.

bfd/
	* targets.c (bfd_cleanup): New typedef.
	(struct bfd <_bfd_check_format>): Return a bfd_cleanup.
	* libbfd-in.h (_bfd_no_cleanup): Define.
	* format.c (bfd_reinit): Add cleanup parameter, call it.
	(bfd_check_format_matches): Set cleanup from _bfd_check_format
	call and pass to bfd_reinit.  Delete temp, use abfd->xvec instead.
	* aout-target.h (callback, object_p): Return bfd_cleanup.
	* aout-tic30.c (tic30_aout_callback, tic30_aout_object_p): Likewise.
	* archive.c (bfd_generic_archive_p): Likewise.
	* binary.c (binary_object_p): Likewise.
	* coff-alpha.c (alpha_ecoff_object_p): Likewise.
	* coff-ia64.c (ia64coff_object_p): Likewise.
	* coff-rs6000.c (_bfd_xcoff_archive_p, rs6000coff_core_p): Likewise.
	* coff-sh.c (coff_small_object_p): Likewise.
	* coff-stgo32.c (go32_check_format): Likewise.
	* coff64-rs6000.c (xcoff64_archive_p, rs6000coff_core_p),
	(xcoff64_core_p): Likewise.
	* coffgen.c (coff_real_object_p, coff_object_p): Likewise.
	* elf-bfd.h (bfd_elf32_object_p, bfd_elf32_core_file_p),
	(bfd_elf64_object_p, bfd_elf64_core_file_p): Likewise.
	* elfcode.h (elf_object_p): Likewise.
	* elfcore.h (elf_core_file_p): Likewise.
	* i386msdos.c (msdos_object_p): Likewise.
	* ihex.c (ihex_object_p): Likewise.
	* libaout.h (some_aout_object_p): Likewise.
	* libbfd-in.h (bfd_generic_archive_p, _bfd_dummy_target),
	(_bfd_vms_lib_alpha_archive_p, _bfd_vms_lib_ia64_archive_p): Likewise.
	* libbfd.c (_bfd_dummy_target): Likewise.
	* libcoff-in.h (coff_object_p): Likewise.
	* mach-o-aarch64.c (bfd_mach_o_arm64_object_p),
	(bfd_mach_o_arm64_core_p): Likewise.
	* mach-o-arm.c (bfd_mach_o_arm_object_p),
	(bfd_mach_o_arm_core_p): Likewise.
	* mach-o-i386.c (bfd_mach_o_i386_object_p),
	(bfd_mach_o_i386_core_p): Likewise.
	* mach-o-x86-64.c (bfd_mach_o_x86_64_object_p),
	(bfd_mach_o_x86_64_core_p): Likewise.
	* mach-o.c (bfd_mach_o_header_p, bfd_mach_o_gen_object_p),
	(bfd_mach_o_gen_core_p, bfd_mach_o_fat_archive_p): Likewise.
	* mach-o.h (bfd_mach_o_object_p, bfd_mach_o_core_p),
	(bfd_mach_o_fat_archive_p, bfd_mach_o_header_p): Likewise.
	* mmo.c (mmo_object_p): Likewise.
	* pef.c (bfd_pef_object_p, bfd_pef_xlib_object_p): Likewise.
	* peicode.h (coff_real_object_p, pe_ILF_object_p),
	(pe_bfd_object_p): Likewise.
	* plugin.c (ld_plugin_object_p, bfd_plugin_object_p): Likewise.
	* ppcboot.c (ppcboot_object_p): Likewise.
	* rs6000-core.c (rs6000coff_core_p): Likewise.
	* som.c (som_object_setup, som_object_p): Likewise.
	* srec.c (srec_object_p, symbolsrec_object_p): Likewise.
	* tekhex.c (tekhex_object_p): Likewise.
	* vms-alpha.c (alpha_vms_object_p): Likewise.
	* vms-lib.c (_bfd_vms_lib_archive_p, _bfd_vms_lib_alpha_archive_p),
	(_bfd_vms_lib_ia64_archive_p, _bfd_vms_lib_txt_archive_p): Likewise.
	* wasm-module.c (wasm_object_p): Likewise.
	* xsym.c (bfd_sym_object_p): Likewise.
	* xsym.h (bfd_sym_object_p): Likewise.
	* aoutx.h (some_aout_object_p): Likewise, and callback parameter
	return type.
	* pdp11.c (some_aout_object_p): Likewise.
	* plugin.c (register_ld_plugin_object_p): Update object_p
	parameter type.
	* plugin.h (register_ld_plugin_object_p): Likewise.
	* bfd-in2.h: Regenerate.
	* libbfd.h: Regenerate.
	* libcoff.h: Regenerate.
ld/
	* plugin.c (plugin_object_p): Return a bfd_cleanup.
	(plugin_cleanup): New function.
2020-03-02 19:30:48 +10:30
Alan Modra a4dd6c97bd miscellaneous SEC_SMALL_DATA
This patch arranges for symbols defined in .sdata and .sbss to be
reported by nm with 'g' and 's' flags, for coff targets that support
.sdata and .sbss.

The assembler changes regarding SEC_SMALL_DATA are really just
documentation.  As far as I'm aware, this won't change any assembler
output.

bfd/
	* coff-alpha.c (alpha_ecoff_le_vec): Add SEC_SMALL_DATA to
	applicable section flags.
	* coff-mips.c (mips_ecoff_le_vec, mips_ecoff_be_vec): Likewise.
	(mips_ecoff_bele_vec): Likewise.
	* coffcode.h (sec_to_styp_flags): Set SEC_SMALL_DATA for .sdata
	and .sbss sections.
	* ecoff.c (_bfd_ecoff_new_section_hook): Likewise.
	(_bfd_ecoff_styp_to_sec_flags): Likewise.
gas/
	* config/tc-m32r.c (md_begin): Set SEC_SMALL_DATA on .scommon section.
	* config/tc-mips.c (s_change_sec): Set SEC_SMALL_DATA for .sdata
	and .sbss sections.
	* config/tc-score.c: Delete !BFD_ASSEMBLER code throughout.
	(s3_s_change_sec): Set SEC_SMALL_DATA for .sbss section.
	(s3_s_score_lcomm): Likewise.
	* config/tc-score7.c: Similarly.
	* read.c (bss_alloc): Set SEC_SMALL_DATA for .sbss section.
2020-03-02 11:36:19 +10:30
Alan Modra bf57746745 ELF SEC_SMALL_DATA
For those ELF targets that have .sdata or .sbss sections, or similar
sections, arrange to mark the sections with the SEC_SMALL_DATA flag.
This fixes regressions in nm symbol type caused by removing .sdata
and .sbss from coff_section_type with commit 49d9fd42ac.

	* elf32-m32r.c (m32r_elf_section_flags): New function.
	(elf_backend_section_flags): Define.
	* elf32-nds32.c (nds32_elf_section_flags): New function.
	(elf_backend_section_flags): Define.
	* elf32-ppc.c (ppc_elf_section_from_shdr): Set SEC_SMALL_DATA for
	.sbss and .sdata sections.
	* elf32-v850.c (v850_elf_section_from_shdr): Set SEC_SMALL_DATA
	for SHF_V850_GPREL sections.
	* elf64-alpha.c (elf64_alpha_section_from_shdr): Delete outdated
	FIXME.
	* elf64-hppa.c (elf64_hppa_section_from_shdr): Set SEC_SMALL_DATA
	for SHF_PARISC_SHORT sections.
	* elf64-ppc.c (ppc64_elf_section_flags): New function.
	(elf_backend_section_flags): Define.
	* elfxx-mips.c (_bfd_mips_elf_section_from_shdr): Set SEC_SMALL_DATA
	for SHF_MIPS_GPREL sections.  Delete FIXME.
2020-03-02 11:36:19 +10:30
Alan Modra 8c803a2dd7 elf_backend_section_flags and _bfd_elf_init_private_section_data
I was looking at elf_backend_section_flags as a means of setting
SEC_SMALL_DATA for .sdata, .sbss and the like, and condidered adding
an asection* parameter to access the section name easily before
realising that hdr->bfd_section of course makes the section
available.  So no new parameter needed.  In fact the flagword*
parameter isn't needed either, so out it goes.

The patch also tidies some horrible code in _bfd_elf_new_section_hook
that can change whether known ABI sections have sh_type and sh_flags
set up depending on which of the bfd_make_section functions is used.
(Some of those set section flags before _bfd_elf_new_section_hook is
called, others leave the flags zero.)  The function also had some
hacks for .init_array and .fini_array to affect how
_bfd_elf_init_private_section_data behaved for those sections.  It's
cleaner to do that in _bfd_elf_init_private_section_data.  So that all
goes and we now init sh_type and sh_flags for all known ABI sections
in _bfd_elf_new_section_hook.  _bfd_elf_init_private_section_data is
changed to suit, and now doesn't just single out SHT_INIT_ARRAY and
SHT_FINI_ARRAY but rather any of the special section types.

The _bfd_elf_new_section_hook change resulting in
+FAIL: ld-aarch64/erratum835769-843419
exposing some errors in the aarch64 backend.  elfNN_aarch64_size_stubs
should not be looking at linker created sections in the stub bfd.  Nor
should code like "symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr" be
run without first checking that input_bfd is ELF.

	* elf-bfd.h (elf_backend_section_flags): Remove flagword* param.
	* elf.c (_bfd_elf_make_section_from_shdr): Set section flags before
	calling elf_backend_section_flags with adjusted params.  Use
	newsect->flags past that point.
	(_bfd_elf_new_section_hook): Always set sh_type and sh_flags for
	special sections.
	(_bfd_elf_init_private_section_data): Allow normal sh_type sections
	to have their type overridden, and all sh_flags but processor and
	os specific.
	* elf32-arm.c (elf32_arm_section_flags): Adjust for changed params.
	* elf32-mep.c (mep_elf_section_flags): Likewise.
	* elf32-nios2.c (nios2_elf32_section_flags): Likewise.
	* elf64-alpha.c (elf64_alpha_section_flags): Likewise.
	* elf64-ia64-vms.c (elf64_ia64_section_flags): Likewise.
	* elfnn-ia64.c (elfNN_ia64_section_flags): Likewise.
	* elfnn-aarch64.c (elfNN_aarch64_size_stubs): Exclude the linker
	stub BFD and non-aarch64 input files when scanning for stubs.
2020-03-02 11:36:19 +10:30
Alan Modra 7d4b2d2d29 alpha-coff: large memory allocation
* coff-alpha.c (alpha_ecoff_get_elt_at_filepos): Provide an upper
	limit to decompressed element size.
2020-03-02 11:36:19 +10:30
Alan Modra 26f60d5939 alpha-vms: prevent endless recursion
* vms-lib.c (vms_traverse_index): Add recur_count param and
	update calls.  Fail on excessive recursion.
2020-03-02 11:36:19 +10:30
Alan Modra 9cb56943d7 alpha-vms: error paths not freeing memory and malloc result checks
When realloc fails it doesn't free the old memory.  In BFD we usually
will exit with an error on a realloc fail, so want to tidy up memory
on error paths.  That's done by bfd_realloc_or_free.

	* vms-alpha.c (vms_get_remaining_object_record): Use
	bfd_realloc_or_free rather than bfd_realloc.
	(add_symbol_entry, vector_grow1, alpha_vms_slurp_relocs): Likewise.
	(dst_define_location, parse_module): Likewise, and check realloc
	return status before using memory.  Return status from function
	adjusting all callers.
2020-03-02 11:36:19 +10:30
Alan Modra 182ec6707c alpha-vms: memory leak
* vms-lib.c (_bfd_vms_lib_archive_p): Free memory on error paths.
2020-02-28 13:27:42 +10:30
Alan Modra 1b088c829e alpha-vms: large memory allocation
This patch simplifies reading of image headers.  It's really not worth
trying to avoid re-reading a 12 byte buffer and then read in
VMS_BLOCK_SIZE chunks, better just to throw the buffer away and use
_bfd_malloc_and_read which does checks against file size.

	* vms-alpha.c (alpha_vms_object_p): Use _bfd_malloc_and_read.
	Remove duplicate undersize check.
2020-02-28 13:26:30 +10:30
Alan Modra 5f60280291 mmix-mmo set SEC_DATA for .data section
With the bfd_decode_symclass change, ld SIZEOF test fails due to
data_end being reported as type '?'.

bfd/
	PR 24511
	* mmo.c (mmo_scan): Set SEC_DATA for .data.
ld/
	PR 24511
	* testsuite/ld-mmix/b-fixo2.d: Adjust for .data change.
	* testsuite/ld-mmix/sec-2.d: Likewise.
	* testsuite/ld-mmix/sec-3.d: Likewise.
	* testsuite/ld-mmix/sec-4.d: Likewise.
	* testsuite/ld-mmix/spec802.d: Likewise.
	* testsuite/ld-mmix/spec803.d: Likewise.
	* testsuite/ld-mmix/spec804.d: Likewise.
	* testsuite/ld-mmix/spec805.d: Likewise.
	* testsuite/ld-mmix/spec806.d: Likewise.
	* testsuite/ld-mmix/spec807.d: Likewise.
	* testsuite/ld-mmix/spec808.d: Likewise.
2020-02-28 08:19:42 +10:30
Alan Modra 49d9fd42ac .idata symbols should be reported with i or I flag by nm
PR 24511
	* syms.c (stt): Trim off all but 'e', 'i' and 'p' entries.
	(coff_section_type): Adjust comment.
	(decode_section_type): Likewise.  Call coff_section_type before
	decode_section_type.
	(bfd_decode_symclass): Use 'c' for common sections other than
	the standard one.
2020-02-28 08:19:42 +10:30
Alan Modra 05f52dc2e1 _bfd_xcoff_read_ar_hdr tidy
* coff-rs6000.c (_bfd_xcoff_read_ar_hdr): Put all data in one
	malloc'd block.
2020-02-27 17:05:08 +10:30
Alan Modra ff69a8949b bfd_stat_arch_elt buffer overflow
If you manage to put an xcoff object file into a non-xcoff archive
(created by first putting a non-xcoff object file into it), and have
xcoff support compiled into libbfd, then objdump -x on the archive
can segfault.  The problem is that _bfd_xcoff_stat_arch_elt expects
abfd->arelt_data->arch_header to be one of the xcoff variants, but
arelt_data is generated depending on the archive format, *not* the
element format.

	* bfd.c (bfd_stat_arch_elt): Use vector of containing archive,
	if file is an archive element.
	* bfd-in2.h: Regenerate.
2020-02-27 17:04:55 +10:30
Alan Modra 02f7e7eed9 Archive sanity checks
Adds some sanity checking to size values read from file.

	* archive.c (do_slurp_bsd_armap): Increase minimum parsed_size, and
	bfd_set_error on failing test.  Don't bother changing bfd_error on
	file read error.  Check symdef_count is multiple of BSD_SYMDEF_SIZE.
	Check sym name is within string buffer.  Use size_t for some vars.
	(do_slurp_coff_armap): Use size_t for some variables, fix size of
	int_buf.  Don't change bfd_error on file read error.  Use
	_bfd_mul_overflow when calculating carsym buffer size.  Reorder
	calculations to catch overflows before they occur.  malloc and
	free raw armap rather than using bfd_alloc.  Read raw armap before
	allocating carsym+strings buffer.
	(_bfd_slurp_extended_name_table): Localize variables.  Check
	name size against file size.
2020-02-26 20:51:33 +10:30
Alan Modra cc4c4f40a2 Re: vms buffer overflows and large memory allocation
git commit c893ce360a changed buffer management, in the process
introducing a bug on an error return path.

	* vms-lib.c (vms_lib_read_index): Release correct buffer.
2020-02-26 15:21:33 +10:30
Alan Modra e0b317de31 rx: memory allocation without checking for NULL return
* elf32-rx.c (rx_elf_relocate_section): Use bfd_malloc rather than
	malloc.  Check for NULL return from bfd_malloc.
	(rx_table_find, rx_table_map): Likewise.
	(rx_set_section_contents): Check bfd_alloc return.
	(rx_dump_symtab): Don't alloc internal_syms or external_syms.
2020-02-26 13:04:54 +10:30
Alan Modra dc1e8a474f Indent labels
Labels don't go in the first column according to standard emacs C
indent rules, and I got annoyed enough at seeing diff -p show a label
rather than the function name to fix this.

bfd/
	* aoutx.h: Indent labels correctly.  Format error strings.
	* archive.c: Likewise.
	* archive64.c: Likewise.
	* coff-arm.c: Likewise.
	* coff-rs6000.c: Likewise.
	* coff-stgo32.c: Likewise.
	* cpu-arm.c: Likewise.
	* dwarf2.c: Likewise.
	* elf-ifunc.c: Likewise.
	* elf-properties.c: Likewise.
	* elf-s390-common.c: Likewise.
	* elf-strtab.c: Likewise.
	* elf.c: Likewise.
	* elf32-arm.c: Likewise.
	* elf32-bfin.c: Likewise.
	* elf32-cr16.c: Likewise.
	* elf32-csky.c: Likewise.
	* elf32-i386.c: Likewise.
	* elf32-m68k.c: Likewise.
	* elf32-msp430.c: Likewise.
	* elf32-nds32.c: Likewise.
	* elf32-nios2.c: Likewise.
	* elf32-pru.c: Likewise.
	* elf32-xtensa.c: Likewise.
	* elf64-ia64-vms.c: Likewise.
	* elf64-x86-64.c: Likewise.
	* elfcode.h: Likewise.
	* elfcore.h: Likewise.
	* elflink.c: Likewise.
	* elfnn-aarch64.c: Likewise.
	* elfnn-ia64.c: Likewise.
	* elfnn-riscv.c: Likewise.
	* elfxx-mips.c: Likewise.
	* elfxx-sparc.c: Likewise.
	* elfxx-x86.c: Likewise.
	* i386lynx.c: Likewise.
	* merge.c: Likewise.
	* pdp11.c: Likewise.
	* plugin.c: Likewise.
	* reloc.c: Likewise.
binutils/
	* elfedit.c: Indent labels correctly.
	* readelf.c: Likewise.
	* resres.c: Likewise.
gas/
	* config/obj-elf.c: Indent labels correctly.
	* config/obj-macho.c: Likewise.
	* config/tc-aarch64.c: Likewise.
	* config/tc-alpha.c: Likewise.
	* config/tc-arm.c: Likewise.
	* config/tc-cr16.c: Likewise.
	* config/tc-crx.c: Likewise.
	* config/tc-frv.c: Likewise.
	* config/tc-i386-intel.c: Likewise.
	* config/tc-i386.c: Likewise.
	* config/tc-ia64.c: Likewise.
	* config/tc-mn10200.c: Likewise.
	* config/tc-mn10300.c: Likewise.
	* config/tc-nds32.c: Likewise.
	* config/tc-riscv.c: Likewise.
	* config/tc-s12z.c: Likewise.
	* config/tc-xtensa.c: Likewise.
	* config/tc-z80.c: Likewise.
	* read.c: Likewise.
	* symbols.c: Likewise.
	* write.c: Likewise.
ld/
	* emultempl/cskyelf.em: Indent labels correctly.
	* ldfile.c: Likewise.
	* ldlang.c: Likewise.
	* plugin.c: Likewise.
opcodes/
	* aarch64-asm.c: Indent labels correctly.
	* aarch64-dis.c: Likewise.
	* aarch64-gen.c: Likewise.
	* aarch64-opc.c: Likewise.
	* alpha-dis.c: Likewise.
	* i386-dis.c: Likewise.
	* nds32-asm.c: Likewise.
	* nfp-dis.c: Likewise.
	* visium-dis.c: Likewise.
2020-02-26 10:37:25 +10:30
Alan Modra e310298cf3 PR25593, --as-needed breaks DT_NEEDED order with linker plugin
This patch delays setting up DT_NEEDED dynamic tags until all object
files and libraries have been opened and their symbols processed,
rather than adding the tags while processing symbols.  Tags are
ordered according to the position of the associated library on the
command line and linker scripts.  It is still possible with
--as-needed libs that are mentioned more than once for tags to be
ordered according to which mention was needed.  For example with
"--as-needed a.so b.so c.so b.so" when b.so is not needed by a.so or
any other prior object file but is needed by c.so, the order of tags
will be "A C B".

bfd/
	PR 25593
	* elf-bfd.h (struct elf_link_hash_table): Rename "loaded" to
	"dyn_loaded".
	(bfd_elf_add_dt_needed_tag): Declare.
	* elf-strtab.c (_bfd_elf_strtab_restore): Handle NULL buf.
	* elflink.c (bfd_elf_add_dt_needed_tag): Make global and rename
	from elf_add_dt_needed_tag.  Remove soname and doit param.
	(elf_link_add_object_symbols): Don't use elf_add_dt_needed_tag
	to see whether as-needed lib is already loaded, use dyn_loaded
	list instead.  When saving and restoring around as-needed lib
	handle possibility that dynstr has not been initialised.  Don't
	add DT_NEEDED tags here.  Limit dyn_loaded list to dynamic libs.
	Mark libs loaded via DT_NEEDED entries of other libs with
	DYN_NO_NEEDED if they should not be mentioned in DT_NEEDED of
	the output.
	(elf_link_check_versioned_symbol): Remove now unneccesary
	DYNAMIC check when traversing dyn_loaded list.
ld/
	PR 25593
	* ldelf.c (ldelf_try_needed): Add DT_NEEDED lib to input_bfds.
	(ldelf_after_open): Save state of input_bfds list before loading
	DT_NEEDED libs.  Traverse input_bfds list adding DT_NEEDED tags.
	Restore input_bfds list.
	* testsuite/ld-cris/gotplt1.d: Adjust for changed .dynstr order.
2020-02-26 10:37:25 +10:30
Alan Modra b570b954bc Limit bogus archive parsed_size
Archive element size is given by data in the archive, and thus is
subject to attack by fuzzers.  The only harm this allows is allocation
of huge amounts of memory, but some systems don't handle that well.
So limit archive element size to archive file size.

	* bfdio.c (bfd_get_file_size): Ignore bogus archive element sizes.
2020-02-26 10:37:13 +10:30
H.J. Lu ecda90163e Don't call lto-wrapper for ar and ranlib
Since ar and ranlib don't need to know symbol types to work properly,
we should avoid calling lto-wrapper for them to speed them up.

bfd/

	PR binutils/25584
	* plugin.c (need_lto_wrapper_p): New.
	(bfd_plugin_set_program_name): Add an int argument to set
	need_lto_wrapper_p.
	(get_lto_wrapper): Return FALSE if need_lto_wrapper_p isn't
	set.
	* plugin.h (bfd_plugin_set_program_name): Add an int argument.

binutils/

	PR binutils/25584
	* ar.c (main): Pass 0 to bfd_plugin_set_program_name.
	* nm.c (main): Pass 1 to bfd_plugin_set_program_name.
2020-02-25 03:31:40 -08:00
Alan Modra a98c743fdf Re: vms buffer overflows and large memory allocation
The last patch wasn't quite correct.  I'd missed the fact that sbm_off
had been updated.

	* vms-lib.c (_bfd_vms_lib_archive_p): Correct overflow checks.
2020-02-24 13:21:48 +10:30
Alan Modra c893ce360a vms buffer overflows and large memory allocation
* vms-lib.c (struct carsym_mem): Add limit.
	(vms_add_index): Heed limit.
	(vms_traverse_index): Catch buffer overflows.  Remove outdated fixme.
	(vms_lib_read_index): Set up limit.  Catch 32-bit overflow.
	Always return actual number read.
	(_bfd_vms_lib_archive_p): Catch buffer overflows.  Replace
	assertion with error exit.
2020-02-24 12:35:05 +10:30
Alan Modra 7b3c27152b PR25585, PHDR segment not covered by LOAD segment
I closed this bug as invalid, but I think it is worth mentioning in NEWS
that older linkers didn't check PT_PHDR very well.  The patch also allows
people to force an output file with --noinhibit-exec after the error.

bfd/
	PR 25585
	* elf.c (assign_file_positions_for_load_sections): Continue linking
	on "PHDR segment not covered by LOAD segment" errors.
ld/
	PR 25585
	* NEWS: Mention better "PHDR segment not covered by LOAD segment"
	checking.
2020-02-22 12:53:52 +10:30
Alan Modra a4425a57c7 Fuzzers whining about mach-o support
It's very easy to make bfd/mach-o.c allocate huge amounts of memory
with fuzzed binaries.  This make it a little harder.

The patch also fixes a number of places where an attempt to detect
overflow of multiplication was done with code like
  if (x * 4 < x)
    /* overflow case */
That of course doesn't work.  There are plenty of values of x that
overflow x * 4 but (x * 4) mod 2^n is greater than x.  For example
with 16-bit types, 0x6000 * 4 = 0x18000 mod 2^16 = 0x8000.

	* mach-o.c (bfd_mach_o_canonicalize_relocs): Fix ineffective
	overflow check.
	(bfd_mach_o_canonicalize_reloc): Likewise.
	(bfd_mach_o_canonicalize_dynamic_reloc): Likewise.  Sanity check
	counts and offsets against file size.
	(bfd_mach_o_build_dysymtab): Fix ineffective overflow check.
	(bfd_mach_o_mangle_sections): Remove unnecessary overflow check.
	(bfd_mach_o_read_symtab_symbols): Sanity check count and offset
	against file size.  Delete symbol table error message.
	(bfd_mach_o_read_dysymtab): Sanity check counts and offsets
	against file size.
	(bfd_mach_o_read_symtab): Likewise.
	(bfd_mach_o_read_command): Pass file size.
	(bfd_mach_o_scan): Sanity check command count against file size.
2020-02-21 22:16:43 +10:30
Alan Modra dda2980f54 PR25569, PDP11 ld -s clobbers last data byte
This patch fixes an ancient wart in aout support, in that text and
data section sizes are rounded up for alignment rather that just the
corresponding header sizes.  Changing section sizes could conceivably
result in buffer overflows if section contents were held in memory.
Also, keeping the original section sizes allows this PR to be fixed
nicely.

bfd/
	PR 25569
	* aoutx.h (adjust_o_magic, adjust_z_magic, adjust_n_magic): Use
	"text", "data" and "bss" section pointer vars.  Don't update
	section size, just exec header sizes.
	(adjust_sizes_and_vmas): Don't update text section size.  Set
	initial exec header a_text.  Print exec headers sizes.
	* pdp11.c (adjust_o_magic, adjust_z_magic, adjust_n_magic),
	(adjust_sizes_and_vmas): Similarly.  Formatting.
	(final_link): Correct final file extension.
gas/
	PR 25569
	* config/obj-aout.c (obj_aout_frob_file_before_fix): Don't loop
	on section size adjustment, instead perform another write if
	exec header size is larger than section size.
2020-02-21 10:47:05 +10:30
Nick Clifton 00e49dff20 Stop the BFD library from automatically converting OS and PROC specific symbol section indicies to SHN_ABS, and provide a hook for backends to decide how such indicies should be processed.
* elf-bfd.h (struct elf_backend_data): Add symbol_section_index
	callback.
	* elfxx-target.h (elf_backend_symbol_section_index): Provide
	default definition.
	(elfNN_bed): Initialise the symbol_section_index field.
	* elf.c (swap_out_syms): Call symbol_section_index, if defined, on
	OS and PROC specific section indicies.  Warn if converting other
	reserved incidies to SHN_ABS.
2020-02-20 13:08:29 +00:00
Sergey Belyashov fcaaac0a0d Various fixes for the Z80 support.
PR 25537
ld	* emultempl/z80.em: Remove machine compatability checking.

	PR 25517
	* testsuite/ld-z80/arch_ez80_adl.d: Update command line.
	* testsuite/ld-z80/arch_ez80_z80.d: Likewise.
	* testsuite/ld-z80/arch_r800.d: Likewise.
	* testsuite/ld-z80/arch_z180.d: Likewise.
	* testsuite/ld-z80/arch_z80n.d: Likewise.
	* testsuite/ld-z80/comb_arch_ez80_z80.d: Likewise.
	* testsuite/ld-z80/comb_arch_z180_z80.d: Likewise.
	* testsuite/ld-z80/comb_arch_z80_ez80.d: Likewise.
	* testsuite/ld-z80/comb_arch_z80_z180.d: Likewise.
	* testsuite/ld-z80/comb_arch_z80_z80n.d: Likewise.
	* testsuite/ld-z80/relocs_b_ez80.d: Likewise.
	* testsuite/ld-z80/relocs_b_z80.d: Likewise.
	* testsuite/ld-z80/relocs_f_ez80.d: Likewise.
	* testsuite/ld-z80/relocs_f_z80.d: Likewise.
	* testsuite/ld-z80/relocs_f_z80n.d: Likewise.

bfd
	* cpu-z80.c: Add machine type compatibility checking.

gas
	* config/tc-z80.c (md_parse_option): Do not use an underscore
	prefix for local labels in SDCC compatability mode.
	(z80_start_line_hook): Remove SDCC dollar label support.
	* testsuite/gas/z80/sdcc.d: Update expected disassembly.
	* testsuite/gas/z80/sdcc.s: Likewise.

	* config/tc-z80.c: Add -march option.
	* doc/as.texi: Update Z80 documentation.
	* doc/c-z80.texi: Likewise.
	* testsuite/gas/z80/ez80_adl_all.d: Update command line.
	* testsuite/gas/z80/ez80_adl_suf.d: Likewise.
	* testsuite/gas/z80/ez80_pref_dis.d: Likewise.
	* testsuite/gas/z80/ez80_z80_all.d: Likewise.
	* testsuite/gas/z80/ez80_z80_suf.d: Likewise.
	* testsuite/gas/z80/gbz80_all.d: Likewise.
	* testsuite/gas/z80/r800_extra.d: Likewise.
	* testsuite/gas/z80/r800_ii8.d: Likewise.
	* testsuite/gas/z80/r800_z80_doc.d: Likewise.
	* testsuite/gas/z80/sdcc.d: Likewise.
	* testsuite/gas/z80/z180.d: Likewise.
	* testsuite/gas/z80/z180_z80_doc.d: Likewise.
	* testsuite/gas/z80/z80_doc.d: Likewise.
	* testsuite/gas/z80/z80_ii8.d: Likewise.
	* testsuite/gas/z80/z80_in_f_c.d: Likewise.
	* testsuite/gas/z80/z80_op_ii_ld.d: Likewise.
	* testsuite/gas/z80/z80_out_c_0.d: Likewise.
	* testsuite/gas/z80/z80_sli.d: Likewise.
	* testsuite/gas/z80/z80n_all.d: Likewise.
	* testsuite/gas/z80/z80n_reloc.d: Likewise.
2020-02-19 17:46:10 +00:00
H.J. Lu dcf06b89b9 plugin: Call dlclose before return in try_load_plugin
Since plugin can be used only once in try_load_plugin, call dlclose
before return.

	PR binutils/25355
	* plugin.c (plugin_list_entry): Remove handle.
	(try_load_plugin): Call dlclose before return.
2020-02-19 03:30:02 -08:00
Alan Modra 2c7c5554df file size check in _bfd_alloc_and_read
* coffgen.c (_bfd_coff_get_external_symbols): Remove file size check.
	* elf.c (bfd_elf_get_str_section): Likewise.
	(_bfd_elf_slurp_version_tables): Likewise.
	* libbfd-in.h (_bfd_constant_p): Define.
	(_bfd_alloc_and_read, _bfd_malloc_and_read): Check read size against
	file size before allocating memory.
	* libbfd.h: Regenerate.
2020-02-19 14:00:55 +10:30
Alan Modra 2bb3687ba8 _bfd_alloc_and_read
This patch provides two new inline functions that are then used in
places that allocate memory, read from file, and then deallocate on a
read failure.

	* libbfd-in.h (_bfd_alloc_and_read, _bfd_malloc_and_read): New.
	* aoutx.h (aout_get_external_symbols): Replace calls to
	bfd_[m]alloc and bfd_bread with call to _bfd_[m]alloc_and_read.
	(slurp_reloc_table): Likewise.
	* archive.c (do_slurp_bsd_armap): Likewise.
	(do_slurp_coff_armap): Likewise.
	* archive64.c (_bfd_archive_64_bit_slurp_armap): Likewise.
	* coff-rs6000.c (_bfd_xcoff_slurp_armap): Likewise.
	* coff64-rs6000.c (xcoff64_slurp_armap): Likewise.
	* coffcode.h (coff_set_arch_mach_hook, buy_and_read): Likewise.
	* coffgen.c (coff_real_object_p, coff_object_p, build_debug_section),
	(_bfd_coff_get_external_symbols): Likewise.
	* ecoff.c (ecoff_slurp_symbolic_header),
	(_bfd_ecoff_slurp_symbolic_info, ecoff_slurp_reloc_table),
	(_bfd_ecoff_slurp_armap, ecoff_link_add_object_symbols, READ),
	(ecoff_indirect_link_order): Likewise.
	* elf.c (bfd_elf_get_str_section, setup_group, elf_read_notes),
	(_bfd_elf_slurp_version_tables): Likewise.
	* elf32-m32c.c (m32c_elf_relax_section): Likewise.
	* elf32-rl78.c (rl78_elf_relax_section): Likewise.
	* elf32-rx.c (elf32_rx_relax_section): Likewise.
	* elf64-alpha.c (READ): Likewise.
	* elf64-mips.c (mips_elf64_slurp_one_reloc_table): Likewise.
	* elf64-sparc.c (elf64_sparc_slurp_one_reloc_table): Likewise.
	* elfcode.h (elf_slurp_symbol_table),
	(elf_slurp_reloc_table_from_section): Likewise.
	* elflink.c (elf_link_add_object_symbols),
	(elf_link_check_versioned_symbol): Likewise.
	* elfxx-mips.c (READ): Likewise.
	* i386lynx.c (slurp_reloc_table): Likewise.
	* lynx-core.c (lynx_core_file_p): Likewise.
	* mach-o.c (bfd_mach_o_canonicalize_relocs),
	(bfd_mach_o_read_symtab_strtab, bfd_mach_o_alloc_and_read),
	(bfd_mach_o_read_prebound_dylib, bfd_mach_o_read_dyld_content
	* pdp11.c (aout_get_external_symbols, slurp_reloc_table
	* pef.c (bfd_pef_print_loader_section, bfd_pef_scan_start_address),
	(bfd_pef_parse_symbols): Likewise.
	* peicode.h (pe_ILF_object_p, pe_bfd_object_p
	* som.c (setup_sections, som_slurp_string_table),
	(som_slurp_reloc_table, som_bfd_count_ar_symbols),
	(som_bfd_fill_in_ar_symbols): Likewise.
	* vms-alpha.c (module_find_nearest_line, evax_bfd_print_dst),
	(evax_bfd_print_image): Likewise.
	* vms-lib.c (_bfd_vms_lib_archive_p): Likewise.
	* wasm-module.c (wasm_scan): Likewise.
	* xcofflink.c (xcoff_link_add_symbols): Likewise.
	* xsym.c (bfd_sym_read_name_table),
	(bfd_sym_print_type_information_table_entry): Likewise.
	* libbfd.h: Regenerate.
2020-02-19 14:00:55 +10:30
Alan Modra 806470a219 Miscellaneous memory alloc related fixes
Some minor tidies.  Allocating memory for internal relocs and symbols
after reading external relocs is slightly better with fuzzed files.
You can at least do something about silly sizes that way.

	* aoutx.h (slurp_reloc_table): Allocate reloc_cache after
	reading external relocs.
	* ecoff.c (ecoff_slurp_reloc_table): Likewise.
	* archive.c (_bfd_write_archive_contents): Don't twiddle bfd_error
	after bfd_bread.
	* archive64.c (_bfd_archive_64_bit_slurp_armap): Remove unnecessary
	bfd_release.
	* elf32-m32c.c (m32c_offset_for_reloc): Make shndx_buf a bfd_byte*.
	(m32c_elf_relax_section): Likewise.
	* elf32-rl78.c (rl78_offset_for_reloc): Likewise.
	(rl78_elf_relax_section): Likewise.
	* elf32-rx.c (rx_offset_for_reloc): Likewise.
	(elf32_rx_relax_section): Likewise.
	* mach-o.c (bfd_mach_o_alloc_and_read): Move earlier with better
	parameter types and use..
	(bfd_mach_o_read_dylinker, bfd_mach_o_read_dylib),
	(bfd_mach_o_read_fvmlib, bfd_mach_o_read_str): ..in these functions.
	* peicode.h (pe_bfd_object_p): Don't zero the part of opthdr
	being read from file, just the extra.
	* som.c (som_slurp_symbol_table): Allocate internal symbol buffer
	after reading external syms.  Free on failure.
2020-02-19 13:15:20 +10:30
Alan Modra 1f4361a77b _bfd_mul_overflow
This patch removes the bfd_alloc2 series of memory allocation functions,
replacing them with __builtin_mul_overflow followed by bfd_alloc.  Why
do that?  Well, a followup patch will implement _bfd_alloc_and_read
and I don't want to implement alloc2 variants as well.

	* coffcode.h (buy_and_read, coff_slurp_line_table),
	(coff_slurp_symbol_table, coff_slurp_reloc_table): Replace
	bfd_[z][m]alloc2 calls with _bfd_mul_overflow followed by the
	corresponding bfd_alloc call.  Adjust variables to suit.
	* coffgen.c (_bfd_coff_get_external_symbols): Likewise.
	* ecoff.c (_bfd_ecoff_slurp_symbolic_info),
	(_bfd_ecoff_slurp_symbol_table, READ): Likewise.
	* elf.c (bfd_elf_get_elf_syms, setup_group, bfd_section_from_shdr),
	(swap_out_syms, _bfd_elf_slurp_version_tables): Likewise.
	* elf32-m32c.c (m32c_elf_relax_section): Likewise.
	* elf32-rl78.c (rl78_elf_relax_section): Likewise.
	* elf32-rx.c (elf32_rx_relax_section): Likewise.
	* elf64-alpha.c (READ): Likewise.
	* elfcode.h (elf_object_p, elf_write_relocs, elf_write_shdrs_and_ehdr),
	(elf_slurp_symbol_table, elf_slurp_reloc_table),
	(bfd_from_remote_memory): Likewise.
	* elfcore.h (core_find_build_id): Likewise.
	* elfxx-mips.c (READ): Likewise.
	* mach-o.c (bfd_mach_o_mangle_sections),
	(bfd_mach_o_read_symtab_symbols, bfd_mach_o_read_thread),
	(bfd_mach_o_read_dysymtab, bfd_mach_o_flatten_sections),
	(bfd_mach_o_scan, bfd_mach_o_fat_archive_p): Likewise.
	* som.c (setup_sections, som_prep_for_fixups)
	(som_build_and_write_symbol_table, som_slurp_symbol_table),
	(som_slurp_reloc_table, som_bfd_count_ar_symbols),
	(som_bfd_fill_in_ar_symbols, som_slurp_armap),
	(som_bfd_ar_write_symbol_stuff): Likewise.
	* vms-alpha.c (vector_grow1): Likewise.
	* vms-lib.c (vms_add_index): Likewise.
	* wasm-module.c (wasm_scan_name_function_section): Likewise.
	* libbfd.c (bfd_malloc2, bfd_realloc2, bfd_zmalloc2): Delete.
	* opncls.c (bfd_alloc2, bfd_zalloc2): Delete.
	* libbfd-in.h (bfd_malloc2, bfd_realloc2, bfd_zmalloc2),
	(bfd_alloc2, bfd_zalloc2): Delete.
	(_bfd_mul_overflow): Define.
	* libbfd.h: Regenerate.
2020-02-19 13:15:06 +10:30
Alan Modra 446f7ed5ab alloc2 used unnecessarily
The bfd_alloc2 series of functions were invented to handle cases where
nmemb * size can overflow.  This patch changes some places where the
calculation can't overflow.

	* elf.c (bfd_section_from_shdr): Use bfd_zalloc rather than
	bfd_zalloc2.
	(assign_section_numbers): Likewise.
	(elf_map_symbols): Likewise, and bfd_alloc rather than bfd_alloc2.
	(_bfd_elf_map_sections_to_segments): Use bfd_malloc rather than
	bfd_malloc2, size_t amt, and unsigned tls_count.
	(rewrite_elf_program_header): Use bfd_malloc and size_t amt.
	* elflink.c (elf_create_symbuf): Use bfd_malloc.
	(elf_output_implib): Use bfd_alloc.
2020-02-19 13:14:45 +10:30
Alan Modra b03202e32c bfd_get_size cache
We have calls to bfd_get_size when swapping in ELF section headers.
Since object files can have a large number of sections, it's worth
caching the file size rather than making lots of stat system calls.

	* bfd.c (struct bfd): Move format and direction to other
	bitfields.  Add "size".
	* bfdio.c (bfd_get_size): Cache size when not writing file.
	* opncls.c (bfd_get_debug_link_info_1): Allow for bfd_get_size
	returning zero, ie. unknown.
	(bfd_get_alt_debug_link_info): Likewise.
	* bfd-in2.h: Regenerate.
2020-02-19 13:14:28 +10:30
Alan Modra 7c5fa58ea9 bfd_get_file_size calls
bfd_get_file_size can return 0, meaning the file size is unknown.

	* coffgen.c (_bfd_coff_get_external_symbols): Don't call
	bfd_get_file_size twice.
	(_bfd_coff_read_string_table): Allow for bfd_get_file_size
	zero, ie. unknown, return.
	* elf-attrs.c (_bfd_elf_parse_attributes): Likewise.
	* elfcode.h (elf_swap_shdr_in): Likewise.
	(elf_object_p): Don't call bfd_get_file_size twice and correct
	file size check.
2020-02-19 13:14:05 +10:30
Alan Modra 96d3b80f54 Check return status of memory alloc functions
This fixes a number of places that call a memory allocation function
without checking for a NULL return before using.

	* mach-o.c (bfd_mach_o_flatten_sections): Return a bfd_boolean,
	FALSE if memory alloc fails.  Adjust calls.
	* som.c (som_prep_for_fixups): Likewise.
	* vms-alpha.c (alpha_vms_add_fixup_lp, alpha_vms_add_fixup_ca),
	(alpha_vms_add_fixup_qr, alpha_vms_add_fixup_lr),
	(alpha_vms_add_lw_reloc, alpha_vms_add_qw_reloc): Likewise.
	* som.c (som_build_and_write_symbol_table): Return via error_return
	on seek failure.
	* vms-alpha.c (VEC_APPEND): Adjust for vector_grow1 changes.
	(VEC_APPEND_EL): Delete.
	(vector_grow1): Return pointer to element.  Catch overflow.
	Return NULL on memory allocation failure.
	(alpha_vms_add_fixup_lp): Replace VEC_APPEND_EL with VEC_APPEND.
	(alpha_vms_add_fixup_ca): Likewise.
	(alpha_vms_link_add_object_symbols): Check VEC_APPEND result
	before using.
	* elf.c (bfd_section_from_shdr): Check bfd_zalloc2 result.
2020-02-19 13:12:52 +10:30
Alan Modra 986f078366 bfd_size_type to size_t
bfd_size_type was invented a long time ago in the K&R days.  Many
places in binutils ought to be using size_t instead (and there are
lots of places that use long or unsigned long that really ought to use
size_t too).  Note that you can't change everything over to size_t: A
32-bit host needs a larger type than size_t to support reading and
processing of 64-bit ELF object files.  This patch just tidies some
of the more obvious uses of bfd_size_type that could be size_t.  There
no doubt are more lurking in the source.  Incidentally, practically
all functions used for output of object files can use size_t and don't
need to worry about overflow of size expressions.  If you have
something like
  symcount * sizeof (void *)
when symcount is counting symbols already in memory then you know that
this expression can't overflow since the size of a symbol in memory is
larger by far than that of a pointer.

	* aix386-core.c (aix386_core_file_p): Use size_t for "amt".
	* aout-target.h (object_p): Likewise.
	* aout-tic30.c (tic30_aout_object_p): Likewise.
	* aoutx.h (some_aout_object_p, mkobject, make_empty_symbol),
	(emit_stringtab, write_syms, link_hash_table_create),
	(aout_link_write_other_symbol): Likewise.
	* archive.c (_bfd_generic_mkarchive, bfd_generic_archive_p),
	(bfd_ar_hdr_from_filesystem, _bfd_write_archive_contents),
	(_bfd_compute_and_write_armap): Likewise.
	* archures.c (bfd_arch_list): Likewise.
	* bfd.c (bfd_record_phdr): Likewise.
	* binary.c (binary_canonicalize_symtab): Likewise.
	* cisco-core.c (cisco_core_file_validate): Likewise.
	* coff-arm.c (coff_arm_link_hash_table_create, find_thumb_glue),
	(find_arm_glue, record_arm_to_thumb_glue),
	(record_thumb_to_arm_glue): Likewise.
	* coff-ppc.c (ppc_coff_link_hash_table_create, record_toc),
	(ppc_allocate_toc_section): Likewise.
	* coff-rs6000.c (_bfd_xcoff_mkobject, _bfd_xcoff_archive_p): Likewise.
	* coff-sh.c (sh_relax_section): Likewise.
	* coff64-rs6000.c (xcoff64_archive_p): Likewise.
	* coffcode.h (handle_COMDAT, coff_new_section_hook),
	(coff_set_alignment_hook, coff_mkobject),
	(coff_compute_section_file_positions): Likewise.
	* coffgen.c (coff_make_empty_symbol, coff_bfd_make_debug_symbol),
	(coff_find_nearest_line_with_names),
	( bfd_coff_set_symbol_class): Likewise.
	* cofflink.c (_bfd_coff_link_hash_table_create),
	(_bfd_coff_link_input_bfd): Likewise.
	* dwarf1.c (alloc_dwarf1_unit, alloc_dwarf1_func): Likewise.
	* dwarf2.c (read_abbrevs, read_attribute_value, add_line_info),
	(build_line_info_table, sort_line_sequences),
	(line_info_add_include_dir, line_info_add_file_name),
	(decode_line_info, scan_unit_for_symbols, parse_comp_unit),
	(place_sections, _bfd_dwarf2_slurp_debug_info): Likewise.
	* ecoff.c (_bfd_ecoff_mkobject, _bfd_ecoff_make_empty_symbol),
	(_bfd_ecoff_find_nearest_line),
	(_bfd_ecoff_bfd_link_hash_table_create): Likewise.
	* ecofflink.c (bfd_ecoff_debug_init): Likewise.
	* elf-hppa.h (_bfd_elf_hppa_gen_reloc_type): Likewise.
	* elf-m10300.c (mn10300_elf_relax_section),
	(elf32_mn10300_link_hash_table_create): Likewise.
	* elf-strtab.c (_bfd_elf_strtab_init): Likewise.
	* elf.c (make_mapping, copy_elf_program_header): Likewise.
	* elf32-arm.c (elf32_arm_link_hash_table_create),
	(elf32_arm_setup_section_lists, elf32_arm_check_relocs),
	(elf32_arm_new_section_hook): Likewise.
	* elf32-avr.c (elf_avr_new_section_hook),
	(elf32_avr_link_hash_table_create, get_local_syms),
	(elf32_avr_setup_section_lists): Likewise.
	* elf32-bfin.c (bfinfdpic_elf_link_hash_table_create),
	(bfin_link_hash_table_create): Likewise.
	* elf32-cr16.c (elf32_cr16_link_hash_table_create): Likewise.
	* elf32-cris.c (elf_cris_link_hash_table_create): Likewise.
	* elf32-csky.c (csky_elf_link_hash_table_create),
	(csky_elf_check_relocs, elf32_csky_setup_section_lists): Likewise.
	* elf32-frv.c (frvfdpic_elf_link_hash_table_create): Likewise.
	* elf32-hppa.c (elf32_hppa_link_hash_table_create),
	(elf32_hppa_setup_section_lists, get_local_syms): Likewise.
	* elf32-i386.c (elf_i386_check_relocs): Likewise.
	* elf32-lm32.c (lm32_elf_link_hash_table_create): Likewise.
	* elf32-m32r.c (m32r_elf_link_hash_table_create),
	(m32r_elf_check_relocs): Likewise.
	* elf32-m68hc1x.c (m68hc11_elf_hash_table_create),
	(elf32_m68hc11_setup_section_lists),
	(elf32_m68hc11_size_stubs): Likewise.
	* elf32-m68k.c (elf_m68k_link_hash_table_create): Likewise.
	* elf32-metag.c (elf_metag_link_hash_table_create),
	(elf_metag_setup_section_lists): Likewise.
	* elf32-microblaze.c (microblaze_elf_link_hash_table_create),
	(microblaze_elf_check_relocs): Likewise.
	* elf32-nds32.c (nds32_elf_link_hash_table_create),
	(nds32_elf_check_relocs): Likewise.
	* elf32-nios2.c (nios2_elf32_setup_section_lists),
	(get_local_syms, nios2_elf32_check_relocs),
	(nios2_elf32_link_hash_table_create): Likewise.
	* elf32-or1k.c (or1k_elf_link_hash_table_create),
	(or1k_elf_check_relocs): Likewise.
	* elf32-ppc.c (ppc_elf_modify_segment_map, update_plt_info): Likewise.
	* elf32-pru.c (pru_elf32_link_hash_table_create): Likewise.
	* elf32-s390.c (elf_s390_link_hash_table_create),
	(elf_s390_check_relocs): Likewise.
	* elf32-score.c (score_elf_create_got_section),
	(s3_elf32_score_new_section_hook),
	(elf32_score_link_hash_table_create): Likewise.
	* elf32-score7.c (score_elf_create_got_section),
	(s7_elf32_score_new_section_hook): Likewise.
	* elf32-sh.c (sh_elf_link_hash_table_create),
	(sh_elf_check_relocs): Likewise.
	* elf32-tic6x.c (elf32_tic6x_link_hash_table_create),
	(elf32_tic6x_new_section_hook, elf32_tic6x_check_relocs): Likewise.
	* elf32-tilepro.c (tilepro_elf_link_hash_table_create),
	(tilepro_elf_check_relocs): Likewise.
	* elf32-v850.c (remember_hi16s_reloc): Likewise.
	* elf32-vax.c (elf_vax_link_hash_table_create): Likewise.
	* elf32-xtensa.c (elf_xtensa_link_hash_table_create),
	(elf_xtensa_new_section_hook): Likewise.
	* elf64-alpha.c (elf64_alpha_bfd_link_hash_table_create),
	(get_got_entry, elf64_alpha_check_relocs): Likewise.
	* elf64-hppa.c (elf64_hppa_hash_table_create): Likewise.
	* elf64-ia64-vms.c (elf64_ia64_object_p): Likewise.
	* elf64-mmix.c (mmix_elf_new_section_hook): Likewise.
	* elf64-ppc.c (ppc64_elf_new_section_hook),
	(ppc64_elf_link_hash_table_create, update_local_sym_info),
	(update_plt_info, ppc64_elf_check_relocs): Likewise.
	* elf64-s390.c (elf_s390_link_hash_table_create),
	(elf_s390_check_relocs): Likewise.
	* elf64-x86-64.c (elf_x86_64_check_relocs): Likewise.
	* elflink.c (bfd_elf_link_record_local_dynamic_symbol),
	(_bfd_elf_link_find_version_dependencies, elf_link_add_object_symbols),
	(elf_link_add_archive_symbols, compute_bucket_count),
	(bfd_elf_size_dynsym_hash_dynstr, _bfd_elf_link_hash_table_create),
	(bfd_elf_get_bfd_needed_list, elf_link_swap_symbols_out),
	(bfd_elf_final_link): Likewise.
	* elfnn-aarch64.c (elfNN_aarch64_link_hash_table_create),
	(elfNN_aarch64_setup_section_lists, elfNN_aarch64_check_relocs),
	(elfNN_aarch64_new_section_hook): Likewise.
	* elfnn-ia64.c (elfNN_ia64_object_p): Likewise.
	* elfnn-riscv.c (riscv_elf_link_hash_table_create),
	(riscv_elf_check_relocs): Likewise.
	* elfxx-mips.c (_bfd_mips_elf_new_section_hook),
	(_bfd_mips_elf_add_symbol_hook, _bfd_mips_elf_check_relocs),
	(_bfd_mips_elf_modify_segment_map, _bfd_mips_elf_set_section_contents),
	(_bfd_mips_elf_link_hash_table_create): Likewise.
	* elfxx-sparc.c (_bfd_sparc_elf_link_hash_table_create),
	(_bfd_sparc_elf_check_relocs),
	(_bfd_sparc_elf_new_section_hook): Likewise.
	* elfxx-tilegx.c (tilegx_elf_link_hash_table_create),
	(tilegx_elf_check_relocs): Likewise.
	* elfxx-x86.c (_bfd_x86_elf_link_hash_table_create): Likewise.
	* format.c (bfd_check_format_matches): Likewise.
	* hash.c (_bfd_stringtab_init): Likewise.
	* ihex.c (ihex_scan): Likewise.
	* irix-core.c (irix_core_core_file_p): Likewise.
	* linker.c (bfd_wrapped_link_hash_lookup),
	(_bfd_generic_link_hash_table_create),
	(_bfd_generic_reloc_link_order): Likewise.
	* lynx-core.c (lynx_core_file_p): Likewise.
	* netbsd-core.c (netbsd_core_file_p): Likewise.
	* osf-core.c (osf_core_core_file_p): Likewise.
	* pdp11.c (some_aout_object_p, mkobject, make_empty_symbol),
	(link_hash_table_create, aout_link_write_other_symbol): Likewise.
	* peXXigen.c (_bfd_XX_bfd_copy_private_section_data): Likewise.
	* peicode.h (pe_mkobject): Likewise.
	* ppcboot.c (ppcboot_mkobject, ppcboot_canonicalize_symtab): Likewise.
	* ptrace-core.c (ptrace_unix_core_file_p): Likewise.
	* sco5-core.c (read_uarea): Likewise.
	* som.c (hppa_som_gen_reloc_type, som_object_p, som_prep_headers),
	(som_write_fixups, som_write_space_strings, som_write_symbol_strings),
	(som_finish_writing, som_canonicalize_symtab, som_new_section_hook),
	(som_bfd_copy_private_section_data, bfd_som_set_section_attributes),
	(bfd_som_attach_aux_hdr, som_write_armap): Likewise.
	* srec.c (srec_scan): Likewise.
	* syms.c (_bfd_generic_make_empty_symbol): Likewise.
	* targets.c (bfd_target_list): Likewise.
	* tekhex.c (first_phase, tekhex_sizeof_headers): Likewise.
	* trad-core.c (trad_unix_core_file_p): Likewise.
	* vms-alpha.c (vms_initialize, alpha_vms_bfd_link_hash_table_create),
	(vms_new_section_hook): Likewise.
	* wasm-module.c (wasm_make_empty_symbol): Likewise.
	* xcofflink.c (xcoff_get_section_contents),
	(_bfd_xcoff_bfd_link_hash_table_create, xcoff_set_import_path),
	(xcoff_find_function, bfd_xcoff_link_record_set, xcoff_build_ldsym),
	(bfd_xcoff_size_dynamic_sections, xcoff_link_input_bfd): Likewise.
2020-02-19 13:12:00 +10:30
Alan Modra 2d0e121701 c99 elfxx-riscv.c fix
We can't use c99 without enabling c99 support for older compilers
that don't enable c99 by default.  So if you want to use c99 contructs
in binutils you'll need to first arrange for -std=c99 to be passed to
older compilers.

	* elfxx-riscv.c (riscv_multi_letter_ext_valid_p): Don't use C99.
2020-02-19 13:11:17 +10:30
H.J. Lu 99845b3b77 plugin: Search bfd-plugins directories only once
try_load_plugin is updated to take either plugin name or plugin entry.
load_plugin is updated to search bfd-plugins directories first to build
a list of plugins and call try_load_plugin with each plugin on the list.
When --plugin is used, the plugin list only has one entry.

	* plugin.c (try_load_plugin): Make plugin_list_iter an argument
	and use it if it isn't NULL.  Remove has_plugin_p argument.  Add
	a build_list_p argument.  Don't search plugin_list.  Short circuit
	when building the plugin list.
	(has_plugin): Renamed to has_plugin_list.
	(bfd_plugin_set_plugin): Don't set has_plugin.
	(bfd_plugin_specified_p): Check plugin_list instead.
	(build_plugin_list): New function.
	(load_plugin): Call build_plugin_list and use plugin_list.
2020-02-13 03:18:13 -08:00
H.J. Lu 22fe7df8c9 Plugin: Treat each object as independent
Since plugin treats each object as independent, we must do a fresh dlopen
of plugin for each object.

	PR binutils/25355
	* plugin.c (try_claim): Always clean up for LTO wrapper.
	(try_load_plugin): Treat each object as independent.  Create a
	copy for plugin name.
2020-02-11 15:36:31 -08:00
Nick Clifton c675ec1e76 Fix building the bfd/elf32-msp430.c file on a 32-bit host.
* elf32-msp430.c (msp430_final_link_relocate): Always use longs
	for addresses in print statements.
	(msp430_elf_relax_delete_bytes): Likewise.
	(msp430_elf_relax_add_words): Likewise.
	(msp430_elf_relax_section): Likewise.
2020-02-11 12:38:41 +00:00
H.J. Lu 1d07a805c8 Clear plugin_data memory
Clear plugin_data memory since it may be uninitialized.

	* plugin.c (add_symbols): Clear plugin_data memory.
2020-02-11 04:27:27 -08:00