Commit Graph

8917 Commits

Author SHA1 Message Date
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
H.J. Lu 0aa99dcd70 Use GCC LTO wrapper to get real symbols from LTO IR objects
GCC LTO wrapper is needed to extract real symbols from LTO IR objects.
This patch does the following:

1. Set up GCC LTO wrapper for each LTO IR object.
2. Run GCC LTO wrapper to get the real object.
3. Extract symbol info from the real object.
4. Cleanup afterwards.

bfd/

	PR binutils/25355
	* configure.ac (HAVE_EXECUTABLE_SUFFIX): New AC_DEFINE.
	(EXECUTABLE_SUFFIX): Likewise.
	* config.in: Regenerated.
	* configure: Likewise.
	* plugin.c (bfd_plugin_close_and_cleanup): Removed.
	(plugin_list_entry): Add all_symbols_read, cleanup_handler,
	gcc, lto_wrapper, resolution_file, resolution_option, gcc_env,
	real_bfd, real_nsyms, real_syms, lto_nsyms and lto_syms.
	(get_lto_wrapper): New.
	(setup_lto_wrapper_env): Likewise.
	(current_plugin): Likewise.
	(register_all_symbols_read): Likewise.
	(register_cleanup): Likewise.
	(get_symbols): Likewise.
	(add_input_file): Likewise.
	(bfd_plugin_close_and_cleanup): Likewise.
	(claim_file): Removed.
	(register_claim_file): Set current_plugin->claim_file.
	(add_symbols): Make a copy of LTO symbols.  Set lto_nsyms and
	lto_syms in current_plugin.
	(try_claim): Use current_plugin->claim_file.  Call LTO plugin
	all_symbols_read handler.  Copy real symbols to plugin_data.
	Call LTO plugin cleanup handler.  Clean up for LTO wrapper.
	(try_load_plugin): Don't reuse the previous plugin for LTO
	wrapper.  Set up GCC LTO wrapper if possible.  Don't set
	plugin_list_iter->claim_file.
	(bfd_plugin_canonicalize_symtab): Use real LTO symbols if
	possible.
	* plugin.h (plugin_data_struct): Add real_bfd, real_nsyms and
	real_syms.

ld/

	PR binutils/25355
	* testsuite/ld-plugin/lto.exp: Run PR binutils/25355 test.
	* testsuite/ld-plugin/pr25355.c: New file.
	* testsuite/ld-plugin/pr25355.d: Likewise.
	* testsuite/lib/ld-lib.exp (run_cc_link_tests): Support compile
	only dump.
2020-02-10 19:01:53 -08:00