Commit Graph

8780 Commits

Author SHA1 Message Date
Joel Brobecker 1c47c133d2 Set development mode to "off" by default.
bfd/ChangeLog:

	* development.sh (development): Set to false.
2019-12-11 21:37:55 +01:00
Alan Modra 13c9c48599 bfd signed overflow fixes
Aimed at quietening ubsan.

include/
	* opcode/mmix.h (PUSHGO_INSN_BYTE): Make unsigned.
	(GO_INSN_BYTE, SETL_INSN_BYTE, INCML_INSN_BYTE, INCMH_INSN_BYTE),
	(INCH_INSN_BYTE, SWYM_INSN_BYTE, JMP_INSN_BYTE): Likewise.
bfd/
	* elf32-rx.c (elf32_rx_relax_section): Avoid signed overflow.
	* libaout.h (N_SET_INFO, N_SET_FLAGS): Likewise.
	* netbsd.h (write_object_contents): Likewise.
	* elf32-arm.c (bfd_elf32_arm_vfp11_erratum_scan): Likewise.
	* libhppa.h (HPPA_R_CONSTANT): Don't signed extend with shifts.
	(stm32l4xx_create_replacing_stub_vldm): Don't truncate high bits
	with shifts.
	* elf32-nds32.h (R_NDS32_RELAX_ENTRY_DISABLE_RELAX_FLAG): Define
	using 1u shifted left.  Ditto for other macros.
	* mmo.c (LOP): Make unsigned.
2019-12-11 21:14:19 +10:30
Alan Modra 09c7848734 Make bfd_get_8 return a bfd_vma like other bfd_get_* functions
And similarly for bfd_get_signed_8.

bfd/
	* libbfd.c (bfd_get_8): Return a bfd_vma.
	(bfd_get_signed_8): Return a bfd_signed_vma.
	* bfd-in2.h: Regenerate.
binutils/
	* od-xcoff.c (dump_dumpx_core): Adjust for bfd_h_get_8 change.
2019-12-11 21:14:19 +10:30
Alan Modra 567607c11f ubsan: xtensa: left shift cannot be represented in type 'int'
In Operand_soffsetx4_decode:
  soffsetx4_0 = 0x4 + ((((int) offset_0 << 14) >> 14) << 2);
and other places.

Don't sign extend with shifts!  This file also has many occurrences of
truncation via shifts, which aren't a problem due to using uint32, but
I dislike on principle enough to fix.

	* xtensa-modules.c (Field_* functions): Don't mask using shifts.
	(Operand_soffsetx4_decode, Operand_simm4_decode),
	(Operand_simm8_decode, Operand_simm8x256_decode),
	(Operand_simm12b_decode, Operand_label8_decode),
	(Operand_label12_decode, Operand_soffset_decode),
	(Operand_xt_wbr15_label_decode, Operand_xt_wbr18_label_decode): Don't
	sign extend using shifts.
	(Operand_immrx4_decode, Operand_uimm16x4_decode): Avoid UB in
	constant.
2019-12-11 11:35:14 +10:30
Alan Modra 8ff23dba80 ubsan: ia64: left shift of negative value
Here, since val is signed:
   *valuep = (val << scale);

	* cpu-ia64-opc.c (ext_imms_scaled): Avoid undefined left shift
	of negative values by using unsigned vars.
2019-12-11 11:33:36 +10:30
Alan Modra 5fa370e437 PR25236, common sym versioning
In cases where a relocatable object file has a common symbol, no other
file has a definition, and there is a matching common symbol found in
a shared library then ld will output a definition using the largest of
size and alignment for the commons.  This patch fixes a bug in ld that
ignored common symbols when assigning versions, resulting in such
symbols being given VER_NDX_LOCAL versions.

	PR 25236
	* elflink.c (_bfd_elf_link_assign_sym_version): Assign versions
	for ELF_COMMON_DEF_P symbols.
	(elf_link_output_extsym, _bfd_elf_add_default_symbol): Adjust to
	suit.
2019-12-07 16:21:01 +10:30
Sandra Loosemore bce7c9d6fb Only give FDE encoding warnings if --eh-frame-hdr was specified.
This bug was observed on nios2-linux-gnu with some C++ programs
linked with -pie or -shared.  The nios2 ABI doesn't include appropriate
relocations in this instance and GCC is also being patched not to pass
--eh-frame-hdr to the linker in those cases.

2019-12-05  Sandra Loosemore  <sandra@codesourcery.com>

        bfd/
        * elf-eh-frame.c (_bfd_elf_discard_section_eh_frame): Make
        FDE encoding warning conditional.
2019-12-05 13:20:12 -08:00
Nick Clifton a23e9ba17f Fix a problem computing the size fields in the PE format header.
PR 25029
	* peXXigen.c (_bfd_XXi_swap_aouthdr_out): Ignore empty sections
	when computing the sizes stored in the headers.
2019-12-05 13:56:07 +00:00
Alan Modra e63ef0954d PR25230, addr2line fails on dwz output
This patch remedies the following DW_FORM_GNU_ref_alt related problem:
      /* FIXME: Do we need to locate the correct CU, in a similar
        fashion to the code in the DW_FORM_ref_addr case above ?  */
Without the correct CU the wrong abbrevs are used, resulting in
errors and/or wrong file names.

There is scope for further work here.  Parsing of CUs should be a two
step process, with the first stage just finding the bounds of the CU.
This would allow find_abstract_instance to quickly find the CU
referenced by DW_FORM_ref_addr or DW_FORM_GNU_ref_alt, then take the
second stage of CU parsing where abbrevs, ranges and suchlike consume
time and memory.  As it is, we just process CUs from the start of
.debug_info until we find the one of interest.  The testcase in the PR
takes 98G of virtual memory.

	PR 25230
	* dwarf2.c (struct dwarf2_debug_file): Add line_table and
	abbrev_offsets.
	(struct abbrev_offset_entry): New.
	(hash_abbrev, eq_abbrev, del_abbrev): New functions.
	(read_abbrevs): Check whether we have already read abbrevs at
	given offset, and add new offset/abbrev to hash table.
	(decode_line_info): Keep line table at offset zero in file struct.
	Return this for a cu reusing the same dir/file list.
	(find_abstract_instance): Find cu for DW_FORM_GNU_ref_alt.
	(_bfd_dwarf2_slurp_debug_info): Create offset/abbrev hash tables.
	(_bfd_dwarf2_cleanup_debug_info): Adjust deletion of lines and
	abbrevs.
2019-12-03 17:30:58 +10:30
Alan Modra 99b06c600f PR25230, dwarf2.c per file stash
This is just moving things around, in preparation for parsing alt
file debug_info.

	PR 25230
	* dwarf2.c (struct dwarf2_debug_file): New struct.
	(struct dwarf2_debug): Delete fields now in dwarf2_debug_file.
	Add f, alt fields.
	(struct comp_unit): Add file field.
	(read_indirect_string, read_indirect_line_string): Adjust to suit.
	(read_alt_indirect_string, read_alt_indirect_ref): Likewise.
	(read_debug_ranges, find_abstract_instance, read_rangelist): Likewise.
	(_bfd_dwarf2_stash_syms, place_sections): Likewise.
	(stash_maybe_update_info_hash_tablse): Likewise.
	(stash_verify_info_hash_table): Likewise.
	(_bfd_dwarf2_slurp_debug_info): Likewise.
	(_bfd_dwarf2_find_symbol_bias): Likewise.
	(_bfd_dwarf2_find_nearest_line): Likewise.
	(_bfd_dwarf2_cleanup_debug_info): Likewise.
	(read_abbrevs): Add file param and adjust.  Update calls.
	(stash_comp_unit): Likewise.
	(decode_line_info): Delete stash param and adjust.  Update calls.
	(comp_unit_find_nearest_line): Likewise.
	(comp_unit_maybe_decode_line_info): Likewise.
	(comp_unit_find_line): Likewise.
	(parse_comp_unit): Add file and info_ptr param and adjust.  Update
	calls.
2019-12-03 16:27:29 +10:30
Alan Modra 019cc8758a dwarf2.c: read_abbrevs fail cleanup, and offset checking
read_section does offset checking, reporting an error on out of
bounds.  There's no need to duplicate the check in functions calling
read_section.  Also, I spotted a place where a pointer difference
expression was being cast to unsigned int, possibly truncating
relevant bits on a 64-bit host.

	* dwarf2.c (read_indirect_string): Don't duplicate offset check
	done in read_section.
	(read_indirect_line_string): Likewise.
	(read_alt_indirect_string): Likewise.
	(read_alt_indirect_ref): Likewise.
	(read_abbrevs): Likewise.  Free memory on all failure paths.
	Use correct unsigned type for pointer difference comparison.
2019-12-03 16:05:40 +10:30
Alan Modra 1b8e12713b dwarf2.c stash->sec_info_ptr and stash->sec
These are unused.  Remove them.  Also fix the wrong sort of 0 being
returned from read_alt_indirect_ref.

	* dwarf2.c (struct dwarf2_debug): Update comments.  Remove sec
	and sec_info_ptr.
	(_bfd_dwarf2_slurp_debug_info): Don't set sec or sec_info_ptr.
	(stash_comp_unit): Likewise.
	(read_alt_indirect_ref): Return NULL not FALSE.
2019-12-03 16:03:20 +10:30
Alan Modra 2247a609df _bfd_dwarf2_find_nearest_line comment fix
These lines should have been removed along with the addr_size parameter.

	* dwarf2.c (_bfd_dwarf2_find_nearest_line): Correct function comment.
2019-12-03 16:00:48 +10:30
Alan Modra 7f3bf38453 PR23652, Use symbols from debug bfd for _bfd_elf_find_function properly
There were a number of problems with the previous patch.  Firstly,
_bfd_dwarf2_stash_syms didn't do anything when the original file had
dynamic symbols, and secondly, info found by the symbol search didn't
make it out of _bfd_elf_find_nearest_line except in the case of DWARF
functions without external linkage.

	PR 23652
	* dwarf2.c (_bfd_dwarf2_stash_syms): Break out of loop on finding
	matching section.
	(_bfd_dwarf2_find_nearest_line): Return an int, with value 2 when
	returning info from the symbol table.  Do the _bfd_elf_find_function
	search also when !found.  Call _bfd_dwarf2_stash_syms regardless of
	symbols.
	* elf64-alpha.c (elf64_alpha_find_nearest_line): Accept dwarf2
	result of 1 only.
	* elfxx-mips.c (_bfd_mips_elf_find_nearest_line): Likewise.
	* libbfd-in.h (_bfd_dwarf2_find_nearest_line): Update prototype.
	* libbfd.h: Regenerate.
2019-11-27 19:10:11 +10:30
Alan Modra e76790600a PR23652, Use symbols from debug bfd for _bfd_elf_find_function
Sometimes DWARF info for a function is incomplete, and the function
can be retrieved by examining symbols.  However, when separate debug
files are used it may be that the original file is completely
stripped of symbols.  This patch teaches BFD to look at symbols from
the debug file in that case.

The patch also removes arm_elf_find_function, instead implementing
elf_backend_maybe_function_sym.  arm_elf_find_function was written
before the generic _bfd_elf_find_function called maybe_function_sym.
aarch64 copied arm, so that file gets the same treatment.  There is
some chance this will speed up arm and aarch64 lookup of function/line.

	PR 23652
	* dwarf2.c (_bfd_dwarf2_stash_syms): New function.
	(_bfd_dwarf2_find_nearest_line): Use it here, passing syms to
	_bfd_elf_find_function.  Call _bfd_elf_find_function in cases
	where _bfd_elf_find_nearest_line would do so.
	* elf.c (_bfd_elf_find_nearest_line): Omit _bfd_elf_find_function
	for dwarf2.
	* elfxx-mips.c (_bfd_mips_elf_find_nearest_line): Similarly.  Tidy.
	* elf32-arm.c (elf32_arm_maybe_function_sym): New function.
	(elf_backend_maybe_function_sym): Define.
	(arm_elf_find_function, elf32_arm_find_nearest_line): Delete.
	(bfd_elf32_find_nearest_line): Don't define.
	* elfnn-aarch64.c (elfNN_aarch64_maybe_function_sym): New function.
	(elf_backend_maybe_function_sym): Define.
	(aarch64_elf_find_function, elfNN_aarch64_find_nearest_line): Delete.
	(bfd_elfNN_find_nearest_line): Don't define.
2019-11-27 12:06:29 +10:30
Alan Modra 96e2dbda08 Correct R_SH_IND12W handling
Using bfd_vma for insn is to avoid having to worry about sign
propagation in expressions involving insn and sym_value when bfd_vma
is not the same as unsigned long.

	* elf32-sh.c (sh_reloc): Use a bfd_vma insn.
	(sh_reloc <R_SH_IND12W>): Divide calculated relocation value
	by two before applying to insn.  Correct overflow test.
	* coff-sh.c (sh_reloc): Likewise.
2019-11-27 11:58:17 +10:30
Nick Clifton 239b426b11 Fix comparison operations in SH code that trigger warning in clang.
* elf32-sh.c (sh_elf_reloc): Use a signed_vma when checking for a
	negative relocated value.
	* coff-sh.c (sh_reloc): Likewise.
2019-11-26 14:06:12 +00:00
Alan Modra bb2942085c Pass section when available to bfd_octets_per_byte
and other tidies.  I think it's better to default to passing the
section to bfd_octets_per_byte, even in cases where we know it won't
make a difference.

A number of the coff reloc functions used bfd_octets_per_byte wrongly,
not factoring it into the offset into the data buffer.  As it happens,
the targets using those files always had bfd_octets_per_byte equal to
one, so there wasn't any detectable wrong behaviour.  However, it is
wrong in the source and might cause trouble for anyone creating a new
target.  Besides fixing that, the patch also defines OCTETS_PER_BYTE
as one in target files where that is appropriate.

bfd/
	* archures.c (bfd_octets_per_byte): Tail call
	bfd_arch_mach_octets_per_byte.
	* coff-arm.c (OCTETS_PER_BYTE): Define.
	(coff_arm_reloc): Introduce new "octets" temp.  Use OCTETS_PER_BYTE
	with section.  Correct "addr".  Remove ATTRIBUTE_UNUSED.
	* coff-i386.c (coff_i386_reloc): Similarly.
	* coff-mips.c (mips_reflo_reloc): Similarly.
	* coff-x86_64.c (coff_amd64_reloc): Similarly.
	* elf32-msp430.c (OCTETS_PER_BYTE): Define.
	(rl78_sym_diff_handler): Use OCTETS_PER_BYTE, with section.
	* elf32-nds32.c (nds32_elf_get_relocated_section_contents): Similarly.
	* elf32-ppc.c (ppc_elf_addr16_ha_reloc): Similarly.
	* elf32-pru.c (pru_elf32_do_ldi32_relocate): Similarly.
	* elf32-s12z.c (opru18_reloc): Similarly.
	* elf32-sh.c (sh_elf_reloc): Similarly.
	* elf32-spu.c (spu_elf_rel9): Similarly.
	* elf32-xtensa.c (bfd_elf_xtensa_reloc): Similarly.
	* elf64-ppc.c (ppc64_elf_ha_reloc, ppc64_elf_brtaken_reloc),
	(ppc64_elf_toc64_reloc): Similarly.
	* bfd.c (bfd_get_section_limit): Pass section to bfd_octets_per_byte.
	* cofflink.c (_bfd_coff_link_input_bfd),
	(_bfd_coff_reloc_link_order): Likewise.
	* elf.c (_bfd_elf_section_offset): Likewise.
	* elflink.c (resolve_section, bfd_elf_perform_complex_relocation),
	(elf_link_input_bfd, elf_reloc_link_order, elf_fixup_link_order),
	(bfd_elf_final_link): Likewise.
	* elf.c (_bfd_elf_make_section_from_shdr): Don't strncmp twice
	to set SEC_ELF_OCTETS.
	* reloc.c (bfd_perform_relocation): Tidy SEC_ELF_OCTETS special case.
	(bfd_install_relocation): Likewise.
	(_bfd_final_link_relocate): Don't recalculate octets.
	* syms.c (_bfd_stab_section_find_nearest_line): Introduc new
	"octets" temp.
	* bfd-in2.h: Regenerate.
ld/
	* ldexp.c (fold_name): Pass section to bfd_octets_per_byte.
	* ldlang.c (init_opb): Don't call bfd_arch_mach_octets_per_byte
	unnecessarily.
2019-11-25 14:32:19 +10:30
Christian Eggers 618265039f Introduce new section flag: SEC_ELF_OCTETS
All symbols, sizes and relocations in this section are octets instead of
bytes.  Required for DWARF debug sections as DWARF information is
organized in octets, not bytes.

bfd/
	* section.c (struct bfd_section): New flag SEC_ELF_OCTETS.
	* archures.c (bfd_octets_per_byte): New parameter sec.
	If section is not NULL and SEC_ELF_OCTETS is set, one octet es
	returned [ELF targets only].
	* bfd.c (bfd_get_section_limit): Provide section parameter to
	bfd_octets_per_byte.
	* bfd-in2.h: regenerate.
	* binary.c (binary_set_section_contents): Move call to
	bfd_octets_per_byte into section loop. Provide section parameter
	to bfd_octets_per_byte.
	* coff-arm.c (coff_arm_reloc): Provide section parameter
	to bfd_octets_per_byte.
	* coff-i386.c (coff_i386_reloc): likewise.
	* coff-mips.c (mips_reflo_reloc): likewise.
	* coff-x86_64.c (coff_amd64_reloc): likewise.
	* cofflink.c (_bfd_coff_link_input_bfd): likewise.
	(_bfd_coff_reloc_link_order): likewise.
	* elf.c (_bfd_elf_section_offset): likewise.
	(_bfd_elf_make_section_from_shdr): likewise.
	Set SEC_ELF_OCTETS for sections with names .gnu.build.attributes,
	.debug*, .zdebug* and .note.gnu*.
	* elf32-msp430.c (rl78_sym_diff_handler): Provide section parameter
	to bfd_octets_per_byte.
	* elf32-nds.c (nds32_elf_get_relocated_section_contents): likewise.
	* elf32-ppc.c (ppc_elf_addr16_ha_reloc): likewise.
	* elf32-pru.c (pru_elf32_do_ldi32_relocate): likewise.
	* elf32-s12z.c (opru18_reloc): likewise.
	* elf32-sh.c (sh_elf_reloc): likewise.
	* elf32-spu.c (spu_elf_rel9): likewise.
	* elf32-xtensa.c (bfd_elf_xtensa_reloc): likewise
	* elf64-ppc.c (ppc64_elf_brtaken_reloc): likewise.
	(ppc64_elf_addr16_ha_reloc): likewise.
	(ppc64_elf_toc64_reloc): likewise.
	* elflink.c (bfd_elf_final_link): likewise.
	(bfd_elf_perform_complex_relocation): likewise.
	(elf_fixup_link_order): likewise.
	(elf_link_input_bfd): likewise.
	(elf_link_sort_relocs): likewise.
	(elf_reloc_link_order): likewise.
	(resolve_section): likewise.
	* linker.c (_bfd_generic_reloc_link_order): likewise.
	(bfd_generic_define_common_symbol): likewise.
	(default_data_link_order): likewise.
	(default_indirect_link_order): likewise.
	* srec.c (srec_set_section_contents): likewise.
	(srec_write_section): likewise.
	* syms.c (_bfd_stab_section_find_nearest_line): likewise.
	* reloc.c (_bfd_final_link_relocate): likewise.
	(bfd_generic_get_relocated_section_contents): likewise.
	(bfd_install_relocation): likewise.
	For section which have SEC_ELF_OCTETS set, multiply output_base
	and output_offset with bfd_octets_per_byte.
	(bfd_perform_relocation): likewise.
include/
	* coff/ti.h (GET_SCNHDR_SIZE, PUT_SCNHDR_SIZE, GET_SCN_SCNLEN),
	(PUT_SCN_SCNLEN): Adjust bfd_octets_per_byte calls.
binutils/
	* objdump.c (disassemble_data): Provide section parameter to
	bfd_octets_per_byte.
	(dump_section): likewise
	(dump_section_header): likewise. Show SEC_ELF_OCTETS flag if set.
gas/
	* as.h: Define SEC_OCTETS as SEC_ELF_OCTETS if OBJ_ELF.
	* dwarf2dbg.c: (dwarf2_finish): Set section flag SEC_OCTETS for
	.debug_line, .debug_info, .debug_abbrev, .debug_aranges, .debug_str
	and .debug_ranges sections.
	* write.c (maybe_generate_build_notes): Set section flag
	SEC_OCTETS for .gnu.build.attributes section.
	* frags.c (frag_now_fix): Don't divide by OCTETS_PER_BYTE if
	SEC_OCTETS is set.
	* symbols.c (resolve_symbol_value): Likewise.
ld/
	* ldexp.c (fold_name): Provide section parameter to
	bfd_octets_per_byte.
	* ldlang (init_opb): New argument s. Set opb_shift to 0 if
	SEC_ELF_OCTETS for the current section is set.
	(print_input_section): Pass current section to init_opb.
	(print_data_statement,print_reloc_statement,
	print_padding_statement): Likewise.
	(lang_check_section_addresses): Call init_opb for each
	section.
	(lang_size_sections_1,lang_size_sections_1,
	lang_do_assignments_1): Likewise.
	(lang_process): Pass NULL to init_opb.
2019-11-25 14:32:19 +10:30
Alan Modra 73d5efd7e1 ARM cmse_scan segfault
This code in elf_link_add_object_symbols:

      ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
      if (ret < 0)
	goto error_return;

      /* If we have already included this dynamic object in the
	 link, just ignore it.  There is no reason to include a
	 particular dynamic object more than once.  */
      if (ret > 0)
	return TRUE;

prevents a shared library from being loaded twice by ensuring that any
library soname doesn't match the soname of one already loaded.  This
happens before sym_hashes are allocated, which leaves sym_hashes NULL.

cmse_scan looks at library symbols, and when attempting to look up a
global symbol will segfault if sym_hashes is zero.

	* elf32-arm.c (elf32_arm_size_stubs): Exclude dynamic library
	BFDs that have not been loaded.
2019-11-21 21:08:30 +10:30
Alan Modra f14080d440 PR25197, assertion fail coffgen.c
The testcase in this PR triggered "BFD_ASSERT (p2->is_sym)" by
sneakily generating a C_FILE sym whose value pointed into auxents.
The fix then is in the last changed line of this patch, to check
p->is_sym as well as p->u.syment.n_sclass.  The other changes fix
various overflow checks that weren't as solid as they could be.

	PR 25197
	* coffgen.c (coff_find_nearest_line_with_names): Check that C_FILE
	u.syment.n_value does point at another C_FILE sym and not into
	some auxent that happens to look like a C_FILE.  Properly check
	for integer overflow and avoid possible pointer wrap-around.
	Simplify pr17512 checks.
2019-11-19 12:31:22 +10:30
Alan Modra 94698d0198 PR25200, SIGSEGV in _bfd_elf_validate_reloc
PR 25200
	* reloc.c (bfd_default_reloc_type_lookup): Don't BFD_FAIL.
	* elf.c (_bfd_elf_validate_reloc): Don't segfault on NULL howto.
2019-11-19 07:35:05 +10:30
Alan Modra ed7e9d0bda elf_backend_init_file_header
This patch renames elf_backend_post_process_headers and moves the
prep_headers code into the new function.  Naming the backend functions
elf_backend_init_file_header and elf_backend_modify_headers makes it
clear which function is called first.

	* elf-bfd.h (struct elf_backend_data <elf_backend_init_file_header>):
	Rename from elf_backend_post_process_headers.
	(_bfd_elf_post_process_headers): Delete.
	(_bfd_elf_init_file_header): Declare.
	* elf.c (_bfd_elf_compute_section_file_positions): Call new function
	in place of prep_headers and elf_backend_post_process_headers.
	(_bfd_elf_init_file_header): Renamed from prep_headers with
	updated args and made global.  Delete dead code.
	(_bfd_elf_post_process_headers): Delete.
	* elf32-arm.c (elf32_arm_init_file_header): Rename from
	elf32_arm_post_process_headers and call _bfd_elf_init_file_header.
	Return status.
	(elf_backend_init_file_header): Define.
	(elf_backend_post_process_headers): Don't define.
	* elf32-i386.c (elf_i386_fbsd_init_file_header): Similarly.
	* elf32-m68hc1x.c (elf32_m68hc11_init_file_header): Similarly.
	* elf32-metag.c (elf_metag_init_file_header): Similarly.
	* elf32-spu.c (spu_elf_init_file_header
	* elf32-visium.c (visium_elf_init_file_header
	* elf64-alpha.c (elf64_alpha_fbsd_init_file_header
	* elf64-hppa.c (elf64_hppa_init_file_header
	* elf64-ia64-vms.c (elf64_vms_init_file_header
	* elfnn-aarch64.c (elfNN_aarch64_init_file_header
	* elfnn-ia64.c (elfNN_hpux_init_file_header
	* elfxx-mips.c (_bfd_mips_init_file_header
	* elfxx-mips.h (_bfd_mips_post_process_headers): Delete.
	(_bfd_mips_init_file_header): Declare.
	(elf_backend_post_process_headers): Delete.
	(elf_backend_init_file_header): Define.
	* elfxx-target.h (elf_backend_post_process_headers): Delete.
	(elf_backend_init_file_header): Define and use.
	* elf32-m68hc12.c (elf_backend_init_file_header): Define.
	(elf_backend_post_process_headers): Don't define.
	* elf32-m68hc1x.h (elf32_m68hc11_post_process_headers): Delete.
	(elf32_m68hc11_init_file_header): Declare.
	* elf32-ppc.c (elf_backend_post_process_headers): Remove
	unnecessary undef.
2019-11-18 22:06:09 +10:30
Alan Modra 6d6c25c8ea elf_backend_modify_headers
This patch renames elf_backend_modify_program_headers and moves the
elf.c code tweaking the ELF file header for -pie -Ttext-segment to a
new function, _bfd_elf_modify_headers, which then becomes the default
elf_backed_modify_headers and is called from any other target
elf_backed_modify_headers.

	* elf-bfd.h (struct elf_backend_data <elf_backend_modify_headers>):
	Rename from elf_backend_modify_program_headers.
	(_bfd_elf_modify_headers): Declare.
	* elf.c (assign_file_positions_except_relocs): Set
	elf_program_header_size.  Always call elf_backend_modify_headers.
	Extract code modifying file header..
	(_bfd_elf_modify_headers): ..to here.  New function.
	* elf32-arm.c (elf_backend_modify_headers): Renamed from
	elf_backend_modify_program_headers.
	* elf32-i386.c: Similarly.
	* elf64-x86-64.c: Similarly.
	* elfxx-target.h: Similarly.  Default elf_backend_modify_headers
	to _bfd_elf_modify_headers.
	* elf-nacl.h (nacl_modify_headers): Rename from
	nacl_modify_program_headers.
	* elf-nacl.c (nacl_modify_headers): Rename from
	nacl_modify_program_headers and call _bfd_elf_modify_headers.
	* elf32-rx.c (elf32_rx_modify_headers): Similarly.
	* elf32-spu.c (spu_elf_modify_headers): Similarly.
	* elfnn-ia64.c (elfNN_ia64_modify_headers): Similarly.
	* elf32-sh.c (elf_backend_modify_program_headers): Don't undef.
2019-11-18 22:06:09 +10:30
Alan Modra 9aea1e3137 PR25196, abort in rewrite_elf_program_header
This patch introduces a new "sorry, cannot handle this file" bfd error
status.  The idea is to use this error in cases where bfd hasn't found
a bfd_bad_value error, ie. an input file or set of options that are
invalid, but rather an input file that is simply too difficult to
process.  Typically this might happen with fuzzed object files such as
the one in the PR, a wildly improbable core file.  Some things are
just not worth wasting time over to fix "properly".

	PR 25196
	* bfd.c (bfd_error_type): Add bfd_error_sorry.
	(bfd_errmsgs): Likewise.
	* elf.c (rewrite_elf_program_header): Don't abort on confused
	lma/alignment.  Replace bfd_error_bad_value with bfd_error_sorry.
	(_bfd_elf_validate_reloc): Use bfd_error_sorry.
	(_bfd_elf_final_write_processing): Likewise.
	* bfd-in2.h: Regenerate.
2019-11-18 22:06:09 +10:30
Jim Wilson c6261a00c3 RISC-V: Fix ld relax failure with calls and align directives.
Make _bfd_riscv_relax_call handle section alignment padding same as
the _bfd_riscv_relax_lui and _bfd_riscv_relax_pc functions already
do.  Use the max section alignment if section boundaries are crossed,
otherwise the alignment of the containing section.

	bfd/
	PR 25181
	* elfnn-riscv.c (_bfd_riscv_relax_call): Always add max_alignment to
	foff.  If sym_sec->output_section and sec->output_section are the same
	and not *ABS* then set max_alignment to that section's alignment.

	ld/
	PR 25181
	* testsuite/ld-riscv-elf/call-relax-0.s: New file.
	* testsuite/ld-riscv-elf/call-relax-1.s: New file.
	* testsuite/ld-riscv-elf/call-relax-2.s: New file.
	* testsuite/ld-riscv-elf/call-relax-3.s: New file.
	* testsuite/ld-riscv-elf/call-relax.d: New test.
	* testsuite/ld-riscv-elf/ld-riscv-elf.exp: Run call-relax test.

Change-Id: Iaf65cee52345abf1955f36e8e72c4f6cc0db8d9a
2019-11-12 15:53:22 -08:00
Alan Modra 595d3787e9 Remove CR16C support
I think it is past time to remove CR16C support.  CR16C was added in
2004, and only for ld.  gas and binutils support is lacking, and there
have been no commits to bfd/elf32-cr16c.c other than warning fixes or
global maintainers making changes to all targets.  I see no maintainer
listed for CR16C, and no commits from anyone at NSC supporting the
target.  Furthermore, at the time the CR16 support was added in 2007,
config.sub was changed upstream to no longer recognise cr16c as a
valid cpu.  That means the CR16C ld support is only available as a
secondary target by configuring with, for example,
--enable-targets=all or --enable-targets=cr16c-unknown-elf.  No
testing of the CR16C target is possible.

include/
	* elf/cr16c.h: Delete.
bfd/
	* cpu-cr16c.c: Delete.
	* elf32-cr16c.c: Delete.
	* Makefile.am,
	* archures.c,
	* config.bfd,
	* configure.ac,
	* reloc.c,
	* targets.c: Remove cr16c support.
	* Makefile.in,
	* bfd-in2.h,
	* configure,
	* libbfd.h,
	* po/SRC-POTFILES.in: Regenerate.
ld/
	* emulparams/elf32cr16c.sh: Delete.
	* scripttempl/elf32cr16c.sc: Delete.
	* Makefile.am,
	* configure.tgt: Remove cr16c support.
	* NEWS: Mention removal of cr16c.
	* Makefile.in,
	* po/BLD-POTFILES.in: Regenerate.
2019-11-07 20:09:20 +10:30
Tim R?hsen 3a70f7e8e3 Fix memory allocation and release problems in the bfd documentation processor.
* doc/chew.c (add_to_definition): Use correct type when
	calculating size of array reallocation.
	(nextword): Always initialise the word return parameter.
	(compile): Check return value of nextword().
2019-11-05 16:03:07 +00:00
Keith Seitz 864619bb2e Add the ability to the BFD library to read build-ids from core flies.
* elf-bfd.h (elf_backend_data) <elf_backend_core_find_build_id>:
	New field.
	(_bfd_elf32_core_find_build_id, _bfd_elf64_core_find_build_id):
	New functions.
	(elf_read_notes): Add declaration.
	* elf.c (elf_read_notes): Move elf-bfd.h.
	(_bfd_elf_core_find_build_id): New function.
	(bfd_section_from_phdr): Scan core file PT_LOAD segments for
	build-id if none is known.
	(elf_parse_notes): For core files, scan for notes.
	* elfcore.h (elf_core_file_matches_executable_p): If both
	BFDs have identical build-ids, then they match.
	(_bfd_elf_core_find_build_id): New function.
	* elfxx-target.h (elf_backend_core_find_build_id): Define.
	(elfNN_bed): Add elf_backend_core_find_build_id.
2019-10-30 12:23:16 +00:00
Andrew Eikum 70cf683455 When copying pe format files, copy the dos_message array, rather than re-initiialising it.
* libcoff-in.h (struct pe_tdata): Add dos_message field.
	* libcoff.h: Regenerate.
	* peXXigen.c (_bfd_XXi_only_swap_filehdr_out): Copy the
	dos_message field rather than initialising it.
	(_bfd_XX_bfd_copy_private_bfd_data_common): Copy the dos_message
	field.
	* peicode.h (pe_mkobject): Initialise the dos_message field.
	(pe_mkobject_hook): Copy the dos_message field.
	(pe_bfd_object_p): Copy the dos_message field.
2019-10-29 17:07:03 +00:00
Alan Modra 30fe183248 PR4499, assign file positions assumes segment offsets increasing
This rewrites much of assign_file_positions_for_non_load_sections to
allow objcopy and strip to handle cases like that in PR4499 where
program headers were not in their usual position immediately after the
ELF file header, and PT_LOAD headers were not sorted by paddr.

	PR 4499
include/
	* elf/internal.h (struct elf_segment_map): Delete header_size.
	Add no_sort_lma and idx.
bfd/
	* elf-nacl.c (nacl_modify_segment_map): Set no_sort_lma for all
	PT_LOAD segments.
	* elf32-spu.c (spu_elf_modify_segment_map): Likewise on overlay
	PT_LOAD segments.
	* elf.c (elf_sort_segments): New function.
	(assign_file_positions_except_relocs): Use shortcuts to elfheader
	and elf_tdata.  Seek to e_phoff not sizeof_ehdr to write program
	headers.  Move PT_PHDR check..
	(assign_file_positions_for_non_load_sections): ..and code setting
	PT_PHDR p_vaddr and p_paddr, and code setting __ehdr_start value..
	(assign_file_positions_for_load_sections): ..to here.  Sort
	PT_LOAD headers.  Delete header_pad code.  Use actual number of
	headers rather than allocated in calculating size for program
	headers.  Don't assume program headers follow ELF file header.
	Simplify pt_load_count code.  Only set "off" for PT_LOAD or
	PT_NOTE in cores.
	(rewrite_elf_program_header): Set p_vaddr_offset for segments
	that include file and program headers.
	(copy_elf_program_header): Likewise, replacing header_size code.
2019-10-25 13:30:05 +10:30
Alan Modra 95cc7c169c ar P support
This patch extends "ar P" to allow creation of normal (as distinct
from thin) archives with full path names.

	PR 452
	PR 25104
bfd/
	* archive.c (normalize): Return file unchanged when
	BFD_ARCHIVE_FULL_PATH.
	(_bfd_construct_extended_name_table): Pass abfd, the output
	bfd, to normalize.
	(_bfd_archive_bsd44_construct_extended_name_table): Likewise.
	* bfd.c (struct bfd): Make flags a full flagword.
	(BFD_ARCHIVE_FULL_PATH): Define.
	* bfd-in2.h: Regenerate.
binutils/
	* ar.c (write_archive): Set BFD_ARCHIVE_FULL_PATH.
	* doc/binutils.texi (extract from archive): Mention
	restrictions when extracting from archives with full paths.
	(ar P): Update to current P support.
	(ar -X32_64): Fix spelling.
2019-10-21 16:13:39 +10:30
John David Anglin 2c9e9550ca [bfd] Provide 8-byte minimum alignment for .plt section
This change increases the default alignment for the .plt section
	from 4 bytes to 8 bytes.  When function descriptors are 8-byte
	aligned, they can be updated atomically on 32-bit hppa.  This
	helps with ordering issues on SMP machines.  It also ensures
	that descriptors reside on the same cache line.  This reduces
	the probability of a double TLB miss in a call.

	2019-10-20  John David Anglin  <danglin@gcc.gnu.org>

		* elf32-hppa.c (elf32_hppa_size_dynamic_sections): Provide 8-byte
		minimum alignment for .plt section.
2019-10-20 12:01:58 -04:00
John David Anglin 18338fcee6 [bfd] Revise import stubs on hppa.
This commit updates the import stubs to leave the pointer to the
	function descriptor in register %r22.  This provides a backup
	mechanism for _dl_runtime_resolve to fixup descriptors during
	lazy binding.

	bfd/ChangeLog
	2019-10-19  John David Anglin  <danglin@gcc.gnu.org>

		* elf32-hppa.c: Revise import stub sequences.
		(LONG_BRANCH_STUB_SIZE): Define.
		(LONG_BRANCH_SHARED_STUB_SIZE): Define.
		(IMPORT_STUB_SIZE): Define.
		(IMPORT_SHARED_STUB_SIZE): Define.
		(EXPORT_STUB_SIZE): Define.
		(plt_stub): Revise to not use register %r22.
		(LDO_R1_R22): Define.
		(LDW_R22_R21): Define.
		(LDW_R22_R19): Define.
		(hppa_build_one_stub): Update stub generation and use new defines.
		(hppa_size_one_stub): Likewise.
2019-10-19 13:52:23 -04:00
Jim Wilson 330a6637a5 RISC-V: Report unresolved relocation error via linker's callback function.
Two patches from Nelson Chu.

It is better to use the linker's callback functions to handle the link time
error when relocating.  The unresolved relocation error can be regarded as
an unsupported relocation.  To make user easier to understand different errors,
we need to extend the current error message format of the callback function
since the format is fixed.

	bfd/
	* elfnn-riscv.c (riscv_elf_relocate_section): Use asprintf to extend
	the error message if needed, and then store the result into the
	`msg_buf`.  Finally, remember to free the unused `msg_buf`.  All error
	message for the dangerous relocation should be set before we call the
	callback function.  If we miss the error message since linker runs out
	of memory, we should set the default error message for the error.

	ld/
	* testsuite/ld-riscv-elf/lib-nopic-01a.s: Create the shared library
	lib-nopic-01a.so, it will be linked with lib-nopic-01b.s.
	* testsuite/ld-riscv-elf/lib-nopic-01b.s: Add new test for the
	unresolved relocation.  Link the non-pic code into a shared library
	may cause the error.
	* testsuite/ld-riscv-elf/lib-nopic-01b.d: Likewise.
	* testsuite/ld-riscv-elf/ld-riscv-elf.exp: Run the new test only when
	the shared library is supported.

R_RISCV_CALL, R_RISCV_JAL and R_RISCV_RVC_JUMP are pc-relative relocation.
For now, we do not allow the object with these relocation links into a shared
library since the referenced symbols may be loaded to the places that too far
from the pc.  We can improve the error message for these unsupported relocation
to notice user that they should recompile their code with `fPIC`.

	bfd/
	* elfnn-riscv.c (riscv_elf_relocate_section): Report the error message
	that user should recompile their code with `fPIC` when linking non-pic
	code into shared library.

	ld/
	* testsuite/ld-riscv-elf/lib-nopic-01b.d: Update the error message.

Change-Id: Ib3347a0a6fa1c2b20a9647c314d5bec2c322ff04
2019-10-17 15:38:27 -07:00
Alan Modra 22216541c1 PR13616, linker should pad executable sections with nops, not zeros
This implements padding of orphan executable sections for PowerPC.
Of course, the simple implementation of bfd_arch_ppc_nop_fill and
removing the NOP definition didn't work, with powerpc64 hitting a
testsuite failure linking to S-records.  That's because the srec
target is BFD_ENDIAN_UNKNOWN so the test of bfd_big_endian (abfd) in
default_data_link_order therefore returned false, resulting in a
little-endian nop pattern.  The rest of the patch fixes that problem
by adding a new field to bfd_link_info that can be used to determine
actual endianness on targets like srec.

	PR 13616
include/
	* bfdlink.h (struct bfd_link_info <big_endian>): New field.
bfd/
	* cpu-powerpc.c (bfd_arch_ppc_nop_fill): New function, use it
	for all ppc arch info.
	* linker.c (default_data_link_order): Pass info->big_endian to
	arch_info->fill function.
ld/
	* emulparams/elf64lppc.sh (NOP): Don't define.
	* emulparams/elf64ppc.sh (NOP): Don't define.
	* ldwrite.c (build_link_order): Use link_info.big_endian.  Move
	code determining endian to use for data_statement to..
	* ldemul.c (after_open_default): ..here.  Set link_info.big_endian.
2019-10-16 23:07:27 +10:30
Alan Modra 9b142ddb4a m68hc1x: better arg checking for reloc_warning
* elf32-m68hc1x.c (reloc_warning): Add printf attribute.
	(elf32_m68hc11_relocate_section): Don't use a variable for format
	strings.  Delete some unnecessary xgettext:c-format comments.
2019-10-15 23:25:11 +10:30
Alan Modra 9a73315161 remove more xmalloc in bfd
Also fixes m68hc1x printf arguments which would have bombed when
compiling on a 32-bit host with --enable-64-bit-bfd.

bfd/
	PR 24955
	* elf32-arm.c (set_cmse_veneer_addr_from_implib): Use bfd_malloc
	rather than xmalloc.
	* elf32-m68hc1x.c (reloc_warning): New function.
	(elf32_m68hc11_relocate_section): Use it here.  Cast bfd_vma values
	corresponding to %lx in format strings.
	* elf32-nds32.c (nds32_insertion_sort): Use a stack temporary.
gas/
	* config/tc-nds32.c (nds32_set_section_relocs): Use relocs and n
	parameters rather than equivalent sec->orelocation and
	sec->reloc_count.  Don't sort for n <= 1.  Tidy.
2019-10-15 16:50:32 +10:30
Alan Modra 779f2ae733 PR25100, Compile fails in elf64-ppc.c because of single equal sign instead of double equal for comparison
PR 25100
	* elf64-ppc.c (sfpr_define): Delete dead code that triggered a warning.
2019-10-15 16:01:05 +10:30
Alan Modra ed481f9799 Simplify power of two test
* bfd.c (bfd_check_compression_header): Check for powers of two
	with x == (x & -x).
2019-10-15 16:00:54 +10:30
Alan Modra 47f6ff2f9e qsort: syms.c stab sorting
* syms.c (struct indexentry): Add idx field.
	(cmpindexentry): Final sort on idx.
	(_bfd_stab_section_find_nearest_line): Set idx.
2019-10-14 16:47:13 +10:30
Alan Modra 8025b0554c qsort: dwarf2.c
This patch ensures qsort stability in line and function sorting done
in dwarf2.c.  For the line sequences we make use of an existing field
that isn't used until later, as a monotonic counter for the qsort.

	* dwarf2.c (struct lookup_funcinfo): Add idx field.
	(compare_lookup_funcinfos): Perform final sort on idx.
	(build_lookup_funcinfo_table): Set idx.
	(compare_sequences): Perform final sort on num_lines.
	(build_line_info_table): Set num_lines and line_info_lookup earlier.
	(sort_line_sequences): Set num_lines for sort.
2019-10-14 16:47:13 +10:30
Alan Modra 3a3f4bf76a qsort: elf_link_add_object_symbols weak aliases
This particular sort almost certainly does not need to be stable for
the ELF linker to work correctly.  However it is conceivable that an
unstable sort could affect linker output, and thus different output be
seen with differing qsort implementations.  The argument goes like
this:  Given more than one strong alias symbol of equal section, value,
and size, the aliases will compare equal by elf_sort_symbol and thus
which one is chosen as the "real" symbol to be made dynamic depends on
qsort.  Why would anyone define two symbols at the same address?
Well, sometimes the fact that there are more than one strong alias
symbol is due to linker script symbols like __bss_start being made
dynamic.  This will match the first symbol defined in .bss if it
doesn't have correct size, and forgetting to properly set size and
type of symbols isn't as rare as it should be.

This patch adds some more heuristics to elf_sort_symbol.

	* elflink.c (elf_sort_symbol): Sort on type and name as well.
	(elf_link_add_object_symbols): Style fix.
2019-10-14 16:47:13 +10:30
Alan Modra 48db329734 qsort: elf_sort_sections use of target_index
elf_sort_sections tried to ensure a stable qsort by using target_index
as the final comparison, but target_index hasn't been set by anything
at the time elf_sort_sections was run.  This patch arrange to have
target_index set.

	* elf.c (_bfd_elf_map_sections_to_segments): Init target_index
	for sections about to be sorted.
	(assign_file_positions_for_load_sections): Likewise.
	(elf_sort_sections): Don't bother optimising both TOEND case.
	* elflink.c (bfd_elf_final_link): Reset target_index.
2019-10-14 16:47:13 +10:30
Alan Modra 8c1c5e5de4 qsort: SHF_LINK_ORDER section sort
The linker SHF_LINK_ORDER section sorting had a number of defects.
1) The ordering was by VMA, which won't work with overlays.  LMA is
   better.
2) Zero size sections can result in two sections at the same LMA/VMA.
   When only one of the two sections at the same LMA is zero size,
   that one must be first.
3) Warnings given by elf_get_linked_section_vma won't ever be emitted
   since elf_object_p warns and excludes objects with zero sh_link on
   a SHF_LINK_ORDER section.
4) Section offset was adjusted down rather than up by section
   alignment, possibly creating overlapping sections.
5) Finding the linked section did so the hard way, rather than simply
   using elf_linked_to_section.

	* elflink.c (elf_get_linked_section_vma): Delete.
	(compare_link_order): Use elf_linked_to_section and sort by lma,
	size, and id.
	(elf_fixup_link_order): Use size_t variables where appropriate.
	Make use of elf_linked_to_section.  Formatting.  Properly align
	sections.
2019-10-14 16:47:13 +10:30
Alan Modra dcea6a95d7 qsort issues
qsort isn't guaranteed to be a stable sort, that is, elements
comparing equal according to the comparison function may be reordered
relative to their original ordering.  Of course sometimes you may not
care, but even in those cases it is good to force some ordering
(ie. not have the comparison function return 0) so that linker output
is reproducible over different libc qsort implementations.

One way to make qsort stable (which the glibc manual incorrectly says
is the only way) is to augment the elements being sorted with a
monotonic counter of some kind, and use that counter as the final
arbiter of ordering in the comparison function.

Another way is to set up an array of pointers into the array of
elements, first pointer to first element, second pointer to second
element and so so, and sort the pointer array rather than the element
array.  Final arbiter in the comparison function then is the pointer
difference.  This works well with, for example, the symbol pointers
returned by _bfd_elf_canonicalize_symtab which point into a symbol
array.

This patch fixes a few places where sorting by symbol pointers is
appropriate, and adds comments where qsort stability is a non-issue.

	* elf-strtab.c (strrevcmp): Comment.
	* merge.c (strrevcmp): Likewise.
	* elf64-ppc.c (compare_symbols): Correct final pointer comparison.
	Comment on why comparing pointers ensures a stable sort.
	* elflink.c (struct elf_symbol): Add void* to union.
	(elf_sort_elf_symbol): Ensure a stable sort with pointer comparison.
	(elf_sym_name_compare): Likewise.
	(bfd_elf_match_symbols_in_sections): Style fix.
	(elf_link_sort_cmp1): Comment.
2019-10-14 16:47:12 +10:30
Alan Modra ec9bd0a22d PR24955, libbfd terminating program on out of memory (part2)
PR 24955
	* elflink.c (elf_output_implib): Don't use xmalloc.  Don't ignore
	return value of bfd_alloc2.
	* peXXigen.c (_bfd_XXi_write_codeview_record): Don't use xmalloc.
	* pef.c (bfd_pef_print_symbol): Likewise.  Don't ignore return
	value of bfd_get_section_contents.
	* som.c (som_write_space_strings): Don't use xmalloc.
	(som_write_symbol_strings): Likewise.
2019-10-14 16:47:12 +10:30
Max Filippov e168da45a2 bfd/dwarf2.c: fix assertion failure in comp_unit_hash_info
stash_maybe_enable_info_hash_tables sets
stash->info_hash_status = STASH_INFO_HASH_ON;
regardless of the result of stash_maybe_update_info_hash_tables call. In
case it fails this results in repeated invocation of comp_unit_hash_info
for the same comp unit and assertion failure in this function.

Only set stash->info_hash_status = STASH_INFO_HASH_ON; when
stash_maybe_update_info_hash_tables is successful.

bfd/
2019-10-11  Max Filippov  <jcmvbkbc@gmail.com>

	* dwarf2.c (stash_maybe_enable_info_hash_tables): Only set
	stash->info_hash_status = STASH_INFO_HASH_ON when
	stash_maybe_update_info_hash_tables succeeds.
2019-10-11 09:02:37 -07:00
Alan Modra 336bfbeb18 PR25070, SEGV in function _bfd_dwarf2_find_nearest_line
Evil testcase with two debug info sections, with sizes of 2aaaabac4ec1
and ffffd5555453b140 result in a total size of 1.  Reading the first
section of course overflows the buffer and tramples on other memory.

	PR 25070
	* dwarf2.c (_bfd_dwarf2_slurp_debug_info): Catch overflow of
	total_size calculation.
2019-10-09 13:28:20 +10:30
Alan Modra 063c511bd7 PR25078, stack overflow in function find_abstract_instance
PR 25078
	* dwarf2.c (find_abstract_instance): Delete orig_info_ptr, add
	recur_count.  Error on recur_count reaching 100 rather than
	info_ptr matching orig_info_ptr.  Adjust calls.
2019-10-09 00:20:28 +10:30