Commit Graph

797 Commits

Author SHA1 Message Date
Nobody ae2c13e275 Binutils 2.35 with MCST patches 2022-08-11 20:17:07 +03:00
H.J. Lu 3084d7a27b ELF: Add _bfd_elf_add_dynamic_tags
All ELF backends with shared library support need to add dynamic tags.
Add dt_pltgot_required and dt_jmprel_required to elf_link_hash_table to
indicate that DT_PLTGOT and DT_JMPREL are required dynamic tags.

1. Add _bfd_elf_add_dynamic_tags to add common dynamic tags.
2. Add _bfd_elf_maybe_vxworks_add_dynamic_tags to add common VxWorks
dynamic tags.

	* elf-bfd.h (elf_link_hash_table): Add dt_pltgot_required and
	dt_jmprel_required.
	(_bfd_elf_add_dynamic_tags): New.
	* elf-m10300.c (_bfd_mn10300_elf_size_dynamic_sections): Call
	_bfd_elf_add_dynamic_tags.
	* elf32-arc.c (elf_arc_size_dynamic_sections): Likewise.
	* elf32-bfin.c (elf32_bfinfdpic_size_dynamic_sections): Likewise.
	* elf32-cr16.c (_bfd_cr16_elf_size_dynamic_sections): Likewise.
	* elf32-frv.c (elf32_frvfdpic_size_dynamic_sections): Likewise.
	* elf32-lm32.c (lm32_elf_size_dynamic_sections): Likewise.
	* elf32-m32r.c (m32r_elf_size_dynamic_sections): Likewise.
	* elf32-m68k.c (elf_m68k_size_dynamic_sections): Likewise.
	* elf32-microblaze.c (microblaze_elf_size_dynamic_sections):
	Likewise.
	* elf32-nds32.c (nds32_elf_size_dynamic_sections): Likewise.
	* elf32-nios2.c (nios2_elf32_size_dynamic_sections): Likewise.
	* elf32-or1k.c (or1k_elf_size_dynamic_sections): Likewise.
	* elf32-s390.c (elf_s390_size_dynamic_sections): Likewise.
	* elf32-tilepro.c (tilepro_elf_size_dynamic_sections): Likewise.
	* elf32-vax.c (elf_vax_size_dynamic_sections): Likewise.
	* elf64-alpha.c (elf64_alpha_size_dynamic_sections): Likewise.
	* elf64-s390.c (elf_s390_size_dynamic_sections): Likewise.
	* elfnn-aarch64.c (elfNN_aarch64_size_dynamic_sections):
	Likewise.
	* elfnn-riscv.c (riscv_elf_size_dynamic_sections): Likewise.
	* elfxx-tilegx.c (tilegx_elf_size_dynamic_sections): Likewise.
	* elf32-arm.c (elf32_arm_size_dynamic_sections): Call
	_bfd_elf_maybe_vxworks_add_dynamic_tags.
	* elf32-ppc.c (ppc_elf_size_dynamic_sections): Likewise.
	* elfxx-sparc.c (_bfd_sparc_elf_size_dynamic_sections):
	Likewise.
	* elfxx-x86.c (_bfd_x86_elf_link_hash_table_create): Likewise.
	(_bfd_x86_elf_size_dynamic_sections): Likewise.
	* elfxx-x86.h (elf_x86_link_hash_table): Remove dt_reloc,
	dt_reloc_sz and dt_reloc_ent.
	* elf-vxworks.c (_bfd_elf_maybe_vxworks_add_dynamic_tags): New.
	* elf-vxworks.h (_bfd_elf_maybe_vxworks_add_dynamic_tags):
	Likewise.
	* elf32-hppa.c (elf32_hppa_link_hash_table_create): Set
	etab.dt_pltgot_required.
	(elf32_hppa_size_dynamic_sections): Call
	_bfd_elf_add_dynamic_tags.
	* elf32-metag.c (elf_metag_link_hash_table_create): Set
	etab.dt_pltgot_required.
	(elf_metag_size_dynamic_sections): Call _bfd_elf_add_dynamic_tags.
	* elf32-sh.c (sh_elf_link_hash_table_create): Set
	root.dt_pltgot_required for FDPIC output.
	(sh_elf_size_dynamic_sections): Call
	_bfd_elf_maybe_vxworks_add_dynamic_tags.
	* elf32-xtensa.c (elf_xtensa_link_hash_table_create): Set
	elf.dt_pltgot_required.
	(elf_xtensa_size_dynamic_sections): Call
	_bfd_elf_add_dynamic_tags.
	* elf64-hppa.c (elf64_hppa_hash_table_create): Set
	root.dt_pltgot_required.
	(elf64_hppa_size_dynamic_sections): Call
	_bfd_elf_add_dynamic_tags.
	* elfnn-ia64.c (elfNN_ia64_hash_table_create): Set
	root.dt_pltgot_required.
	(elfNN_ia64_size_dynamic_sections): Set root.dt_jmprel_required
	for rel_pltoff_sec.  Call _bfd_elf_add_dynamic_tags.
	* elflink.c (_bfd_elf_add_dynamic_tags): New.
2020-06-23 05:07:45 -07:00
Roland McGrath cae64165f4 gold, ld: Implement -z start-stop-visibility=... option.
gold/
	Implement -z start-stop-visibility=... option.
	* options.h (class General_options): Handle -z start-stop-visibility=.
	(General_options::start_stop_visibility_enum): New public method.
	(General_options::set_start_stop_visibility_enum): New private method.
	(General_options::start_stop_visibility_enum_): New private member.
	* options.cc (General_options::General_options): Add initializer.
	(General_options::finalize): Set this->start_stop_visibility_enum_
	from string value.
	* layout.cc (Layout::define_section_symbols): Use option setting.

bfd/
	* elflink.c (bfd_elf_define_start_stop): Use start_stop_visibility
	field of bfd_link_info.

include/
	* bfdlink.h (struct bfd_link_info): New field start_stop_visibility.

ld/
	* NEWS: Mention -z start-stop-visibility=... option for ELF.
	* ld.texi (Options): Document -z start-stop-visibility=... option.
	* ldmain.c (main): Initialize link_info.start_stop_visibility.
	* emultempl/elf.em (gld${EMULATION_NAME}_handle_option):
	Parse -z start-stop-visibility=... option.
2020-06-15 11:45:02 -07:00
Alan Modra 75cfe082c0 PR26103, Assertion failure with symbols defined in link-once sections
PR 26103
	* elflink.c (elf_link_add_archive_symbols): Exclude undefined
	symbols that were defined in discarded sections.
	* cofflink.c (coff_link_check_archive_element): Likewise.
	(coff_link_add_symbols): Set indx to -3 for symbols defined in
	discarded sections.
	(_bfd_coff_write_global_sym): Don't emit such symbols.
	libcoff-in.h (struct coff_link_hash_entry): Update indx comment.
	libcoff.h: Regenerate.
2020-06-15 12:10:06 +09:30
H.J. Lu e1b5d517d1 ELF: Properly handle section symbols
When defining the section symbol, __start_FOO, for the section FOO:

1. Treat the common symbol, __start_FOO, in input object file as
definition.
2. Clear verinfo.verdef.

bfd/

	PR ld/26094
	* elflink.c (bfd_elf_define_start_stop): Handle common symbols.
	Clear verinfo.verdef.

ld/

	PR ld/26094
	* testsuite/ld-elf/pr26094-1.ver: New fike.
	* testsuite/ld-elf/pr26094-1a.c: Likewise.
	* testsuite/ld-elf/pr26094-1a.rd: Likewise.
	* testsuite/ld-elf/pr26094-1b.c: Likewise.
	* testsuite/ld-elf/pr26094-1b.rd: Likewise.
	* testsuite/ld-elf/pr26094-1c.c: Likewise.
	* testsuite/ld-elf/shared.exp: Run ld/26094 tests.
2020-06-10 05:31:31 -07:00
H.J. Lu 90c14f0c3a ELF: Add target_os to elf_link_hash_table/elf_backend_data
Add target_os to elf_backend_data to identify target OS.  Add target_os,
to elf_link_hash_table to identify target OS for linker output.

	* elf-bfd.h (elf_target_os): New.
	(elf_link_hash_table): Add target_os.
	(elf_backend_data): Add target_os.
	* elf32-arm.c (elf32_arm_link_hash_table): Remove vxworks_p,
	symbian_p and nacl_p.
	(create_got_section): Updated.
	(elf32_arm_create_dynamic_sections): Likewise.
	(arm_type_of_stub): Likewise.
	(elf32_arm_create_or_find_stub_sec): Likewise.
	(elf32_arm_allocate_plt_entry): Likewise.
	(elf32_arm_populate_plt_entry): Likewise.
	(elf32_arm_final_link_relocate): Likewise.
	(elf32_arm_check_relocs): Likewise.
	(allocate_dynrelocs_for_symbol): Likewise.
	(elf32_arm_finish_dynamic_symbol): Likewise.
	(elf32_arm_finish_dynamic_sections): Likewise.
	(elf32_arm_output_plt_map_1): Likewise.
	(elf32_arm_output_arch_local_syms): Likewise.
	(elf32_arm_add_symbol_hook): Likewise.
	(elf32_arm_nacl_link_hash_table_create): Likewise.
	(elf32_arm_vxworks_link_hash_table_create): Likewise.
	(elf32_arm_symbian_link_hash_table_create): Likewise.
	(ELF_TARGET_OS): New.
	* elf32-i386.c (elf_i386_arch_bed): Removed.
	(elf_backend_arch_data): Likewise.
	(elf_i386_solaris_arch_bed): Likewise.
	(elf_i386_nacl_arch_bed): Likewise.
	(elf_i386_vxworks_arch_bed): Likewise.
	(elf_i386_relocate_section): Updated.
	(elf_i386_finish_dynamic_sections): Likewise.
	(elf_i386_get_synthetic_symtab): Likewise.
	(elf_i386_link_setup_gnu_properties): Likewise.
	(ELF_TARGET_OS): New.
	* elf32-mips.c (ELF_TARGET_OS): New.
	* elf32-ppc.c (ppc_elf_link_hash_table): Remove is_vxworks.
	(ppc_elf_create_got): Updated.
	(ppc_elf_create_dynamic_sections): Likewise.
	(ppc_elf_check_relocs): Likewise.
	(ppc_elf_adjust_dynamic_symbol): Likewise.
	(ppc_elf_size_dynamic_sections): Likewise.
	(ppc_elf_relocate_section): Likewise.
	(ppc_elf_finish_dynamic_sections): Likewise.
	(ppc_elf_vxworks_link_hash_table_create): Likewise.
	(ELF_TARGET_OS): New.
	* elf32-sh.c (elf_sh_link_hash_table): Remove vxworks_p.
	(sh_elf_link_hash_table_create): Updated.
	(sh_elf_create_dynamic_sections): Likewise.
	(allocate_dynrelocs): Likewise.
	(sh_elf_size_dynamic_sections): Likewise.
	(sh_elf_relocate_section): Likewise.
	(sh_elf_finish_dynamic_symbol): Likewise.
	(sh_elf_finish_dynamic_sections): Likewise.
	(ELF_TARGET_OS): New.
	* elf32-sparc.c (elf32_sparc_vxworks_link_hash_table_create):
	Removed.
	(bfd_elf32_bfd_link_hash_table_create): Likewise.
	(ELF_TARGET_OS): New.
	* elf64-x86-64.c (elf_x86_64_arch_bed): Removed.
	(elf_x86_64_solaris_arch_bed): Likewise.
	(elf_x86_64_nacl_arch_bed): Likewise.
	(elf_x86_64_finish_dynamic_sections): Updated.
	(elf_x86_64_get_synthetic_symtab): Likewise.
	(elf_x86_64_link_setup_gnu_properties): Likewise.
	(ELF_TARGET_OS): New.
	* elflink.c (_bfd_elf_link_hash_table_init): Initialize
	target_o.
	* elfxx-mips.c (mips_elf_link_hash_table): Remove is_vxworks.
	(MIPS_ELF_REL_DYN_NAME): Updated.
	(ELF_MIPS_GP_OFFSET): Likewise.
	(mips_elf_create_local_got_entry): Likewise.
	(mips_elf_allocate_dynamic_relocations): Likewise.
	(mips_elf_count_got_symbols): Likewise.
	(is_gott_symbol): Likewise.
	(mips_elf_calculate_relocation): Likewise.
	(mips_elf_create_dynamic_relocation): Likewise.
	(_bfd_mips_elf_check_relocs): Likewise.
	(allocate_dynrelocs): Likewise.
	(_bfd_mips_elf_adjust_dynamic_symbol): Likewise.
	(mips_elf_lay_out_got): Likewise.
	(mips_elf_set_plt_sym_value): Likewise.
	(_bfd_mips_elf_size_dynamic_sections): Likewise.
	(_bfd_mips_elf_finish_dynamic_symbol): Likewise.
	(_bfd_mips_elf_finish_dynamic_sections): Likewise.
	(_bfd_mips_elf_final_link): Likewise.
	(_bfd_mips_init_file_header): Likewise.
	* elfxx-sparc.c (_bfd_sparc_elf_create_dynamic_sections):
	Likewise.
	(allocate_dynrelocs): Likewise.
	(_bfd_sparc_elf_size_dynamic_sections): Likewise.
	(_bfd_sparc_elf_relocate_section): Likewise.
	(_bfd_sparc_elf_finish_dynamic_symbol): Likewise.
	(sparc_finish_dyn): Likewise.
	(_bfd_sparc_elf_finish_dynamic_sections): Likewise.
	* elfxx-target.h (ELF_TARGET_OS): New.
	(elfNN_bed): Add ELF_TARGET_OS.
	* elfxx-x86.c (elf_x86_allocate_dynrelocs): Updated.
	(_bfd_x86_elf_link_hash_table_create): Likewise.
	(_bfd_x86_elf_size_dynamic_sections): Likewise.
	(_bfd_x86_elf_finish_dynamic_sections): Likewise.
	(_bfd_x86_elf_adjust_dynamic_symbol): Likewise.
	(_bfd_x86_elf_link_setup_gnu_properties): Likewise.
	* elfxx-x86.h (elf_x86_target_os): Removed.
	(elf_x86_backend_data): Likewise.
	(get_elf_x86_backend_data): Likewise.
	(elf_x86_link_hash_table): Remove target_os.
2020-06-06 06:45:38 -07:00
H.J. Lu c4b126b87a ELF: Don't check relocations in non-loaded, non-alloced sections
Don't do anything special with non-loaded, non-alloced sections.
In particular, any relocs in such sections should not affect GOT
and PLT reference counting (ie. we don't allow them to create GOT
or PLT entries), there's no possibility or desire to optimize TLS
relocs, and there's not much point in propagating relocs to shared
libs that the dynamic linker won't relocate.

Since check_relocs is no longer called on non-loaded, non-alloced
sections, remove SEC_ALLOC check.  Resolve relocation in debug section
against symbol defined in shared library to 0.

bfd/

	PR ld/26080
	* elf-m10300.c (mn10300_elf_relocate_section): Resolve relocation
	in debug section against symbol defined in shared library to 0.
	* elf32-i386.c (elf_i386_check_relocs): Remove SEC_ALLOC check.
	* elf32-lm32.c (lm32_elf_check_relocs): Likewise.
	* elf32-m32r.c (m32r_elf_check_relocs): Likewise.
	* elf32-nds32.c (nds32_elf_check_relocs): Likewise.
	* elf32-nios2.c (nios2_elf32_check_relocs): Likewise.
	* elf32-or1k.c (or1k_elf_check_relocs): Likewise.
	* elf32-ppc.c (ppc_elf_check_relocs): Likewise.
	* elf32-sh.c (sh_elf_check_relocs): Likewise.
	* elf32-xtensa.c (elf_xtensa_check_relocs): Likewise.
	* elf64-alpha.c (elf64_alpha_check_relocs): Likewise.
	* elf64-ppc.c (ppc64_elf_check_relocs): Likewise.
	* elf64-x86-64.c (elf_x86_64_check_relocs): Likewise.
	* elfxx-mips.c (_bfd_mips_elf_check_relocs): Likewise.
	* elf32-vax.c (elf_vax_check_relocs): Set non_got_ref for non-GOT
	reference.
	(elf_vax_adjust_dynamic_symbol): Generate a copy reloc only if
	there is non-GOT reference.
	* elflink.c (_bfd_elf_link_check_relocs): Skip non-loaded,
	non-alloced sections.

ld/

	PR ld/26080
	* testsuite/ld-elf/comm-data.exp: Remove copy_reloc.
	* testsuite/ld-elf/comm-data2r.rd: Removed.
	* testsuite/ld-elf/comm-data2r.sd: Likewise.
	* testsuite/ld-elf/comm-data2r.xd: Likewise.
2020-06-04 05:58:47 -07:00
H.J. Lu d49e5065ed ELF: Consolidate maybe_set_textrel
All maybe_set_textrel implementations are the same.  Consolidate them
to a single _bfd_elf_maybe_set_textrel.

	* elf-bfd.h (_bfd_elf_maybe_set_textrel): New
	* elf32-arm.c (maybe_set_textrel): Removed.
	(elf32_arm_size_dynamic_sections): Replace maybe_set_textrel
	with _bfd_elf_maybe_set_textrel.
	* elf32-csky.c (maybe_set_textrel): Removed.
	(csky_elf_size_dynamic_sections): Replace maybe_set_textrel
	with _bfd_elf_maybe_set_textrel.
	* elf32-hppa.c (maybe_set_textrel): Removed.
	(elf32_hppa_size_dynamic_sections): Replace maybe_set_textrel
	with _bfd_elf_maybe_set_textrel.
	* elf32-lm32.c (maybe_set_textrel): Removed.
	(lm32_elf_size_dynamic_sections): Replace maybe_set_textrel
	with _bfd_elf_maybe_set_textrel.
	* elf32-m32r.c (maybe_set_textrel): Removed.
	(m32r_elf_size_dynamic_sections): Replace maybe_set_textrel
	with _bfd_elf_maybe_set_textrel.
	* elf32-metag.c (maybe_set_textrel): Removed.
	(elf_metag_size_dynamic_sections): Replace maybe_set_textrel
	with _bfd_elf_maybe_set_textrel.
	* elf32-nds32.c (maybe_set_textrel): Removed.
	(nds32_elf_size_dynamic_sections): Replace maybe_set_textrel
	with _bfd_elf_maybe_set_textrel.
	* elf32-or1k.c (maybe_set_textrel): Removed.
	(or1k_elf_size_dynamic_sections): Replace maybe_set_textrel
	with _bfd_elf_maybe_set_textrel.
	* elf32-ppc.c (maybe_set_textrel): Removed.
	(ppc_elf_size_dynamic_sections): Replace maybe_set_textrel
	with _bfd_elf_maybe_set_textrel.
	* elf32-s390.c (maybe_set_textrel): Removed.
	(elf_s390_size_dynamic_sections): Replace maybe_set_textrel
	with _bfd_elf_maybe_set_textrel.
	* elf32-sh.c (maybe_set_textrel): Removed.
	(sh_elf_size_dynamic_sections): Replace maybe_set_textrel
	with _bfd_elf_maybe_set_textrel.
	* elf32-tic6x.c (maybe_set_textrel): Removed.
	(elf32_tic6x_size_dynamic_sections): Replace maybe_set_textrel
	with _bfd_elf_maybe_set_textrel.
	* elf32-tilepro.c (maybe_set_textrel): Removed.
	(tilepro_elf_size_dynamic_sections): Replace maybe_set_textrel
	with _bfd_elf_maybe_set_textrel.
	* elf64-ppc.c (maybe_set_textrel): Removed.
	(ppc64_elf_size_dynamic_sections): Replace maybe_set_textrel
	with _bfd_elf_maybe_set_textrel.
	* elf64-s390.c (maybe_set_textrel): Removed.
	(elf_s390_size_dynamic_sections): Replace maybe_set_textrel
	with _bfd_elf_maybe_set_textrel.
	* elfnn-aarch64.c (maybe_set_textrel): Removed.
	(elfNN_aarch64_size_dynamic_sections): Replace maybe_set_textrel
	with _bfd_elf_maybe_set_textrel.
	* elfnn-riscv.c (maybe_set_textrel): Removed.
	(riscv_elf_size_dynamic_sections): Replace maybe_set_textrel
	with _bfd_elf_maybe_set_textrel.
	* elfxx-sparc.c (maybe_set_textrel): Removed.
	(_bfd_sparc_elf_size_dynamic_sections): Replace maybe_set_textrel
	with _bfd_elf_maybe_set_textrel.
	* elfxx-tilegx.c (maybe_set_textrel): Removed.
	(tilegx_elf_size_dynamic_sections): Replace maybe_set_textrel
	with _bfd_elf_maybe_set_textrel.
	* elfxx-x86.c (maybe_set_textrel): Removed.
	(_bfd_x86_elf_size_dynamic_sections): Replace maybe_set_textrel
	with _bfd_elf_maybe_set_textrel.
	* elflink.c (_bfd_elf_maybe_set_textrel): New.
2020-06-03 07:07:24 -07:00
H.J. Lu ad172eaa4f ELF: Copy dyn_relocs in _bfd_elf_link_hash_copy_indirect
Copy dyn_relocs in _bfd_elf_link_hash_copy_indirect instead of in each
target backend.

	PR ld/26067
	* elf32-arm.c (elf32_arm_copy_indirect_symbol): Don't copy
	dyn_relocs.
	* elf32-csky.c (csky_elf_copy_indirect_symbol): Likewise.
	* elf32-hppa.c (elf32_hppa_copy_indirect_symbol): Likewise.
	* elf32-metag.c (elf_metag_copy_indirect_symbol): Likewise.
	* elf32-microblaze.c (microblaze_elf_copy_indirect_symbol):
	Likewise.
	* elf32-nds32.c (nds32_elf_copy_indirect_symbol): Likewise.
	* elf32-nios2.c (nios2_elf32_copy_indirect_symbol): Likewise.
	* elf32-or1k.c (or1k_elf_copy_indirect_symbol): Likewise.
	* elf32-s390.c (elf_s390_copy_indirect_symbol): Likewise.
	* elf32-sh.c (sh_elf_copy_indirect_symbol): Likewise.
	* elf32-tilepro.c (tilepro_elf_copy_indirect_symbol): Likewise.
	* elf64-s390.c (elf_s390_copy_indirect_symbol): Likewise.
	* elfnn-aarch64.c (elfNN_aarch64_copy_indirect_symbol): Likewise.
	* elfnn-riscv.c (riscv_elf_copy_indirect_symbol): Likewise.
	* elfxx-sparc.c (_bfd_sparc_elf_copy_indirect_symbol): Likewise.
	* elfxx-tilegx.c (tilegx_elf_copy_indirect_symbol): Likewise.
	* elfxx-x86.c (_bfd_x86_elf_copy_indirect_symbol): Likewise.
	* elf32-lm32.c (lm32_elf_copy_indirect_symbol): Removed.
	(elf_backend_copy_indirect_symbol): Likewise.
	* elf32-m32r.c (m32r_elf_copy_indirect_symbol): Removed.
	(elf_backend_copy_indirect_symbol): Likewise.
	* elflink.c (_bfd_elf_link_hash_copy_indirect): Copy dyn_relocs.
2020-06-03 07:03:59 -07:00
H.J. Lu 5dbc8b372f ELF: Consolidate readonly_dynrelocs
All readonly_dynrelocs implementations are the same.  Consolidate them
to a single _bfd_elf_readonly_dynrelocs.

	PR ld/26067
	* elf-bfd.h (_bfd_elf_readonly_dynrelocs): New.
	* elf32-arm.c (readonly_dynrelocs): Removed.
	(maybe_set_textrel): Replace readonly_dynrelocs with
	_bfd_elf_readonly_dynrelocs.
	* elf32-csky.c (readonly_dynrelocs): Removed.
	(maybe_set_textrel): Replace readonly_dynrelocs with
	_bfd_elf_readonly_dynrelocs.
	*  elf32-hppa.c(readonly_dynrelocs): Removed.
	(alias_readonly_dynrelocs): Replace readonly_dynrelocs with
	_bfd_elf_readonly_dynrelocs.
	(maybe_set_textrel): Likewise.
	* elf32-lm32.c (readonly_dynrelocs): Removed.
	(lm32_elf_adjust_dynamic_symbol): Replace readonly_dynrelocs
	with _bfd_elf_readonly_dynrelocs.
	(maybe_set_textrel): Likewise.
	* elf32-m32r.c (readonly_dynrelocs): Removed.
	(m32r_elf_adjust_dynamic_symbol): Replace readonly_dynrelocs
	with _bfd_elf_readonly_dynrelocs.
	(maybe_set_textrel): Likewise.
	* elf32-metag.c (readonly_dynrelocs): Removed.
	(elf_metag_adjust_dynamic_symbol): Replace readonly_dynrelocs
	with _bfd_elf_readonly_dynrelocs.
	(maybe_set_textrel): Likewise.
	* elf32-microblaze.c (readonly_dynrelocs): Removed.
	(microblaze_elf_adjust_dynamic_symbol): Replace readonly_dynrelocs
	with _bfd_elf_readonly_dynrelocs.
	* elf32-nds32.c (readonly_dynrelocs): Removed.
	(nds32_elf_adjust_dynamic_symbol): Replace readonly_dynrelocs
	with _bfd_elf_readonly_dynrelocs.
	(maybe_set_textrel): Likewise.
	* elf32-or1k.c (readonly_dynrelocs): Removed.
	(or1k_elf_adjust_dynamic_symbol): Replace readonly_dynrelocs
	with _bfd_elf_readonly_dynrelocs.
	* elf32-ppc.c (readonly_dynrelocs): Removed.
	(alias_readonly_dynrelocs): Replace readonly_dynrelocs with
	_bfd_elf_readonly_dynrelocs.
	(ppc_elf_adjust_dynamic_symbol): Likewise.
	(maybe_set_textrel): Likewise.
	* elf32-s390.c (readonly_dynrelocs): Removed.
	(elf_s390_adjust_dynamic_symbol): Replace readonly_dynrelocs
	with _bfd_elf_readonly_dynrelocs.
	(maybe_set_textrel): Likewise.
	* elf32-sh.c (readonly_dynrelocs): Removed.
	(sh_elf_adjust_dynamic_symbol): Replace readonly_dynrelocs with
	_bfd_elf_readonly_dynrelocs.
	(maybe_set_textrel): Likewise.
	* elf32-tic6x.c (readonly_dynrelocs): Removed.
	(maybe_set_textrel): Replace readonly_dynrelocs with
	_bfd_elf_readonly_dynrelocs.
	* elf32-tilepro.c (readonly_dynrelocs): Removed.
	(tilepro_elf_adjust_dynamic_symbol): Replace readonly_dynrelocs
	with _bfd_elf_readonly_dynrelocs.
	(maybe_set_textrel): Likewise.
	* elf64-ppc.c (readonly_dynrelocs): Removed.
	(alias_readonly_dynrelocs): Replace readonly_dynrelocs with
	_bfd_elf_readonly_dynrelocs.
	(ppc64_elf_adjust_dynamic_symbol): Likewise.
	(maybe_set_textrel): Likewise.
	* elf64-s390.c (readonly_dynrelocs): Removed.
	(elf_s390_adjust_dynamic_symbol): Replace readonly_dynrelocs
	with _bfd_elf_readonly_dynrelocs.
	(maybe_set_textrel): Likewise.
	* elflink.c (_bfd_elf_readonly_dynrelocs): New.
	* elfnn-aarch64.c (readonly_dynrelocs): Removed.
	(maybe_set_textrel): Replace readonly_dynrelocs with
	_bfd_elf_readonly_dynrelocs.
	* elfnn-riscv.c (readonly_dynrelocs): Removed.
	(riscv_elf_adjust_dynamic_symbol): Replace readonly_dynrelocs
	with _bfd_elf_readonly_dynrelocs.
	(maybe_set_textrel): Likewise.
	* elfxx-sparc.c (readonly_dynrelocs): Removed.
	(_bfd_sparc_elf_adjust_dynamic_symbol): Replace
	readonly_dynrelocs with _bfd_elf_readonly_dynrelocs.
	(maybe_set_textrel): Likewise.
	* elfxx-tilegx.c (readonly_dynrelocs): Removed.
	(tilegx_elf_adjust_dynamic_symbol): Replace readonly_dynrelocs
	with _bfd_elf_readonly_dynrelocs.
	(maybe_set_textrel): Likewise.
	* elfxx-x86.c (readonly_dynrelocs): Removed.
	(maybe_set_textrel): Replace readonly_dynrelocs with
	_bfd_elf_readonly_dynrelocs.
	(_bfd_x86_elf_adjust_dynamic_symbol): Likewise.
2020-06-03 07:01:13 -07:00
H.J. Lu a6dbf402de ld: Add --warn-textrel and obsolete --warn-shared-textrel
--warn-shared-textrel and -z text apply to both shared object and PIE.
Add --warn-textrel and obsolete --warn-shared-textrel.  Consolidate
--warn-textrel and -z text/notext/textoff implementation.

bfd/

	PR ld/22909
	* elflink.c (bfd_elf_final_link): Use bfd_link_textrel_check.
	Check bfd_link_dll when issue a DT_TEXTREL warning.
	* elfxx-x86.c (maybe_set_textrel): Likewise.
	(_bfd_x86_elf_size_dynamic_sections): Likewise.

include/

	PR ld/22909
	* bfdlink.h (textrel_check_method): New enum.
	(bfd_link_textrel_check): New.
	(bfd_link_info): Replace warn_shared_textrel and error_textrel
	with textrel_check.

ld/

	PR ld/22909
	* NEWS: Mention --warn-textrel.
	* ld.texi: Update -z text/notext/textoff.  Add --warn-textrel.
	Remove --warn-shared-textrel.
	* ldlex.h (option_values): Rename OPTION_WARN_SHARED_TEXTREL to
	OPTION_WARN_TEXTREL.
	* lexsup.c (ld_options): Add --warn-textrel.  Obsolete
	--warn-shared-textrel.
	(parse_args): Updated.
	(elf_shlib_list_options): Check link_info.textrel_check.
	* emultempl/elf.em: Updated.
	* testsuite/ld-elf/pr19539.d: Replace -z notext with
	--warn-textrel.  Expect a warning.
	* testsuite/ld-i386/warn1.d: Update expected warning.
2020-05-27 04:54:10 -07:00
Alan Modra c95949892f Replace "if (x) free (x)" with "free (x)", bfd
* aoutx.h: Replace "if (x) free (x)" with "free (x)" throughout.
	* archive.c, * bfd.c, * bfdio.c, * coff-alpha.c, * coff-ppc.c,
	* coff-sh.c, * coff-stgo32.c, * coffcode.h, * coffgen.c,
	* cofflink.c, * cpu-arm.c, * doc/chew.c, * dwarf2.c, * ecoff.c,
	* ecofflink.c, * elf-eh-frame.c, * elf-m10200.c, * elf-m10300.c,
	* elf-strtab.c, * elf.c, * elf32-arc.c, * elf32-arm.c,
	* elf32-avr.c, * elf32-bfin.c, * elf32-cr16.c, * elf32-crx.c,
	* elf32-epiphany.c, * elf32-ft32.c, * elf32-h8300.c,
	* elf32-ip2k.c, * elf32-m32c.c, * elf32-m68hc11.c,
	* elf32-m68k.c, * elf32-microblaze.c, * elf32-msp430.c,
	* elf32-nds32.c, * elf32-nios2.c, * elf32-ppc.c, * elf32-pru.c,
	* elf32-rl78.c, * elf32-rx.c, * elf32-sh.c, * elf32-spu.c,
	* elf32-v850.c, * elf32-xtensa.c, * elf64-alpha.c,
	* elf64-hppa.c, * elf64-ia64-vms.c, * elf64-mips.c
	* elf64-mmix.c, * elf64-ppc.c, * elf64-sparc.c, * elfcode.h,
	* elflink.c, * elfnn-ia64.c, * elfnn-riscv.c, * elfxx-mips.c,
	* elfxx-x86.c, * format.c, * ihex.c, * libbfd.c, * linker.c,
	* mmo.c, * opncls.c, * pdp11.c, * peXXigen.c, * pef.c,
	* peicode.h, * simple.c, * som.c, * srec.c, * stabs.c, * syms.c,
	* targets.c, * vms-lib.c, * xcofflink.c, * xtensa-isa.c: Likewise.
2020-05-21 10:11:57 +09:30
Alan Modra 765cf5f623 Use bfd_get_filename throughout bfd
* aix5ppc-core.c (xcoff64_core_file_matches_executable_p): Use
	bfd_get_filename rather than accessing bfd->filename directly.
	* aout-target.h (MY (object_p)): Likewise.
	* aoutx.h (aout_find_nearest_line, aout_link_write_symbols): Likewise.
	* archive.c (find_nested_archive, _bfd_generic_read_ar_hdr_mag),
	(_bfd_construct_extended_name_table, _bfd_bsd44_write_ar_hdr),
	(_bfd_archive_bsd44_construct_extended_name_table),
	(_bfd_write_archive_contents, _bfd_compute_and_write_armap),
	(_bfd_bsd_write_armap): Likewise.
	* bfd.c (bfd_errmsg, _bfd_doprnt): Likewise.
	* cache.c (bfd_open_file): Likewise.
	* ecoff.c (_bfd_ecoff_write_armap): Likewise.
	* ecofflink.c (bfd_ecoff_debug_accumulate_other): Likewise.
	* elf32-bfin.c (bfinfdpic_relocate_section): Likewise.
	* elf32-frv.c (elf32_frv_relocate_section): Likewise.
	* elf32-hppa.c (elf32_hppa_final_link): Likewise.
	* elf32-nds32.c (nds32_elf_output_symbol_hook),
	(patch_tls_desc_to_ie): Likewise.
	* elf32-spu.c (sort_bfds, print_one_overlay_section),
	(spu_elf_auto_overlay): Likewise.
	* elf64-hppa.c (elf_hppa_final_link): Likewise.
	* elf64-ia64-vms.c (elf64_ia64_size_dynamic_sections): Likewise.
	* elfcore.h (elf_core_file_matches_executable_p): Likewise.
	* elflink.c (bfd_elf_size_dynamic_sections),
	(elf_link_input_bfd): Likewise.
	* linker.c (_bfd_generic_link_output_symbols): Likewise.
	* mach-o.c (bfd_mach_o_follow_dsym),
	(bfd_mach_o_close_and_cleanup): Likewise.
	* opncls.c (_bfd_delete_bfd, _maybe_make_executable),
	(find_separate_debug_file, get_build_id_name): Likewise.
	* pdp11.c (aout_find_nearest_line, aout_link_write_symbols): Likewise.
	* plugin.c (bfd_plugin_open_input): Likewise.
	* rs6000-core.c (rs6000coff_core_file_matches_executable_p): Likewise.
	* som.c (som_write_armap): Likewise.
	* srec.c (srec_write_record, srec_write_symbols): Likewise.
	* vms-lib.c (_bfd_vms_lib_get_imagelib_file),
	(_bfd_vms_lib_write_archive_contents): Likewise.
	* xcofflink.c (xcoff_link_add_dynamic_symbols): Likewise.
2020-05-19 12:35:03 +09:30
H.J. Lu 6f6fd151cb elf: Strip zero-sized dynamic sections
ELF size_dynamic_sections is called by the ELF backend linker after all
the linker input files have been seen but before the section sizes have
been set.  After the sections sizes have been set, target-specific,
global optimizations may make some dynamic sections zero-sized if they
are no longer needed.

Add ELF strip_zero_sized_dynamic_sections so that ELF backend linker can
strip zero-sized dynamic sections after the sections sizes have been set.

bfd/

	PR ld/25849
	* elf-bfd.h (elf_backend_data): Add
	elf_backend_strip_zero_sized_dynamic_sections.
	(_bfd_elf_strip_zero_sized_dynamic_sections): New prototype.
	* elf64-alpha.c (elf_backend_strip_zero_sized_dynamic_sections):
	New macro.
	* elflink.c (_bfd_elf_strip_zero_sized_dynamic_sections): New
	function.
	* elfxx-target.h (elf_backend_strip_zero_sized_dynamic_sections):
	New macro.
	(elfNN_bed): Add elf_backend_strip_zero_sized_dynamic_sections.

ld/

	PR ld/25849
	* ldelfgen.c (ldelf_map_segments): Call
	elf_backend_strip_zero_sized_dynamic_sections.
	* testsuite/ld-alpha/tlsbinr.rd: Updated.
2020-04-21 05:24:03 -07:00
Fangrui Song 95a5156812 Unify the behaviour of ld.bfd and ld.gold with respect to warning about unresolved symbol references. (PR 24613)
PR binutils/24613
include	* bfdlink.h (enum report_method): Delete RM_GENERATE_WARNING and
	RM_GENERATE_ERROR. Add RM_DIAGNOSE.
	(struct bfd_link_info): Add warn_unresolved_syms.

ld	* lexsup.c (parse_args): Change RM_GENERATE_WARNING and
	RM_GENERATE_ERROR to RM_DIAGNOSE.
	* emultempl/aix.em (ld_${EMULATION_NAME}_emulation): Change
	RM_GENERATE_ERROR to RM_DIAGNOSE.
	* emultempl/elf.em (ld_${EMULATION_NAME}_emulation): Likewise.

bfd	* coff-rs6000.c (xcoff_ppc_relocate_section): Change RM_GENERATE_ERROR
	to RM_DIAGNOSE plus a check of warn_unresolved_syms.
	* coff64-rs6000.c (xcoff_ppc_relocate_section): Likewise.
	* elf-bfd.h (_bfd_elf_large_com_section): Likewise.
	* elf32-m32r.c (m32r_elf_relocate_section): Likewise.
	* elf32-score.c (s3_bfd_score_elf_relocate_section): Likewise.
	* elf32-score7.c (s7_bfd_score_elf_relocate_section): Likewise.
	* elf32-sh.c (sh_elf_relocate_section): Likewise.
	* elf32-spu.c (spu_elf_relocate_section): Likewise.
	* elf64-hppa.c (elf64_hppa_relocate_section): Likewise.
	* elflink.c (elf_link_output_extsym): Likewise.
	* elfxx-mips.c (mips_elf_calculate_relocation): Likewise.
2020-04-15 14:25:08 +01:00
Sebastian Huber fdde2fb60c Mention .tdata in comment in _bfd_elf_tls_setup()
This helps to find code areas which deal with the .tdata section.

bfd/

    	* elflink.c (_bfd_elf_tls_setup): Mention .tdata in comment.
2020-03-23 17:04:28 +01:00
Christophe Lyon abf874aafe Add support for non-contiguous memory regions
2020-01-06  Christophe Lyon  <christophe.lyon@linaro.org>

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

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

	ld/
	* ldlang.c (lang_add_section): Add support for
	non_contiguous_regions.
	(size_input_section): Likewise.
	(lang_size_sections_1): Likewise.
	(process_insert_statements): Likewise.
	* ldlex.h (option_values): Add OPTION_NON_CONTIGUOUS_REGIONS and
	OPTION_NON_CONTIGUOUS_REGIONS_WARNINGS.
	* lexsup.c (ld_options): Add entries for
	--enable-non-contiguous-regions and
	--enable-non-contiguous-regions-warnings.
	(parse_args): Handle it.
	* NEWS: Add --enable-non-contiguous-regions and
	--enable-non-contiguous-regions-warnings.
	* ld.texi: Add --enable-non-contiguous-regions and
	--enable-non-contiguous-regions-warnings documentation.
	* emultempl/armelf.em (elf32_arm_add_stub_section): Add
	SEC_LINKER_CREATED flag.
	* emultempl/xtensaelf.em (ld_build_required_section_dependence):
	Emit an error when --enable-non-contiguous-regions is used.
	* testsuite/ld-elf/non-contiguous.d: New.
	* testsuite/ld-elf/non-contiguous.ld: New.
	* testsuite/ld-elf/non-contiguous.s: New.
	* testsuite/ld-arm/arm-elf.exp: Run the new tests.
	* testsuite/ld-arm/arm-elf/non-contiguous-arm.s: New.
	* testsuite/ld-arm/arm-elf/non-contiguous-arm.d: New.
	* testsuite/ld-arm/arm-elf/non-contiguous-arm.ld: New.
	* testsuite/ld-arm/arm-elf/non-contiguous-arm2.d: New.
	* testsuite/ld-arm/arm-elf/non-contiguous-arm3.ld: New.
	* testsuite/ld-arm/arm-elf/non-contiguous-arm3.d: New.
	* testsuite/ld-arm/arm-elf/non-contiguous-arm3.ld: New.
	* testsuite/ld-arm/arm-elf/non-contiguous-arm4.d: New.
	* testsuite/ld-arm/arm-elf/non-contiguous-arm4.ld: New.
	* testsuite/ld-arm/arm-elf/non-contiguous-arm5.d: New.
	* testsuite/ld-arm/arm-elf/non-contiguous-arm5.ld: New.
	* testsuite/ld-arm/arm-elf/non-contiguous-arm6.d: New.
	* testsuite/ld-arm/arm-elf/non-contiguous-arm6.ld: New.
	* testsuite/ld-powerpc/powerpc.exp: Run new tests.
	* testsuite/ld-powerpc/non-contiguous-powerpc.d: New.
	* testsuite/ld-powerpc/non-contiguous-powerpc.ld: New.
	* testsuite/ld-powerpc/non-contiguous-powerpc.sd: New.
	* testsuite/ld-powerpc/non-contiguous-powerpc64.d: New.
2020-03-13 14:44:45 +00:00
Christian Eggers 666318230c Fix several mix up between octets and bytes in ELF program headers
Fixes additional locations not handled in the first patch.

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

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

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

include/
	* elf/internal.h (struct elf_internal_phdr): Add unit (octets)
	to several member field comments.
	(Elf_Internal_Shdr): likewise.
bfd/
	* elf.c (_bfd_elf_make_section_from_shdr): Introduce new temp
	opb.  Divide Elf_Internal_Shdr::sh_addr by opb when setting
	section LMA/VMA.
	(_bfd_elf_make_section_from_phdr): Similarly.
	(elf_fake_sections): Fix calculation of
	Elf_Internal_shdr::sh_addr from section VMA.
	(_bfd_elf_map_sections_to_segments): Fix mixup between octets
	and bytes.
	(assign_file_positions_for_load_sections): Fix calculations of
	Elf_Internal_shdr::p_vaddr and p_paddr from section LMA/VMA.  Fix
	comparison between program header address and section LMA.
	(assign_file_positions_for_non_load_sections): Likewise.
	(rewrite_elf_program_header): Likewise.  Introduce new temp opb.
	(IS_CONTAINED_BY_VMA): Add parameter opb.
	(IS_CONTAINED_BY_LMA,IS_SECTION_IN_INPUT_SEGMENT,
	INCLUDE_SECTION_IN_SEGMENT): Likewise.
	(copy_elf_program_header): Update call to ELF_SECTION_IN_SEGMENT.
	Fix calculations of p_addr_valid and p_vaddr_offset.
	* elflink.c (elf_link_add_object_symbols): Multiply section VMA
	with octets per byte when comparing against p_vaddr.
ld/
	* ldexp.c (fold_name): Return SIZEOF_HEADERS in bytes.
2020-03-13 15:37:11 +10:30
Alan Modra 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 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 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 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
H.J. Lu 5242a0a000 ld: Issue an error for GC on __patchable_function_entries section
__patchable_function_entries section is generated by a compiler with
-fpatchable-function-entry=XX.  The assembly code looks like this:

---
	.text
	.globl	_start
	.type	_start, %function
_start:
	.section __patchable_function_entries,"aw",%progbits
	.dc.a	.LPFE1
	.text
.LPFE1:
	.byte 0
---

But --gc-sections will silently remove __patchable_function_entries
section and generate corrupt result.  This patch disallows garbage
collection on __patchable_function_entries section without linked-to
section.

bfd/

	PR ld/25490
	* elflink.c (_bfd_elf_gc_mark_extra_sections): Issue an error
	for garbage collection on __patchable_function_entries section
	without linked-to section.

ld/

	PR ld/25490
	* testsuite/ld-elf/pr25490-1.d: New file.
	* testsuite/ld-elf/pr25490-1.s: Likewise.
2020-02-06 19:40:38 -08:00
H.J. Lu b7d0721677 ELF: Support the section flag 'o' in .section directive
As shown in

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

--gc-sections will silently remove __patchable_function_entries section
and generate corrupt result.  This patch adds the section flag 'o' to
.section directive:

.section __patchable_function_entries,"awo",@progbits,foo
.section __patchable_function_entries,"awoG",@progbits,foo,foo,comdat
.section __patchable_function_entries,"awo",@progbits,bar,unique,4
.section __patchable_function_entries,"awoG",@progbits,foo,foo,comdat,unique,1

which specifies the symbol name which the section references.  Assmebler
will set its elf_linked_to_section to a local section where the symbol
is defined.

Linker is updated to call mark_hook if gc_mark of any of its linked-to
sections is set after all sections, except for backend specific ones,
have been garbage collected.

bfd/

	PR gas/25381
	* bfd-in2.h: Regenerated.
	* elflink.c (_bfd_elf_gc_mark_extra_sections): Call mark_hook
	on section if gc_mark of any of its linked-to sections is set
	and don't set gc_mark again.
	* section.c (asection): Add linked_to_symbol_name to map_head
	union.

gas/

	PR gas/25381
	* config/obj-elf.c (get_section): Also check
	linked_to_symbol_name.
	(obj_elf_change_section): Also set map_head.linked_to_symbol_name.
	(obj_elf_parse_section_letters): Handle the 'o' flag.
	(build_group_lists): Renamed to ...
	(build_additional_section_info): This.  Set elf_linked_to_section
	from map_head.linked_to_symbol_name.
	(elf_adjust_symtab): Updated.
	* config/obj-elf.h (elf_section_match): Add linked_to_symbol_name.
	* doc/as.texi: Document the 'o' flag.
	* testsuite/gas/elf/elf.exp: Run PR gas/25381 tests.
	* testsuite/gas/elf/section18.d: New file.
	* testsuite/gas/elf/section18.s: Likewise.
	* testsuite/gas/elf/section19.d: Likewise.
	* testsuite/gas/elf/section19.s: Likewise.
	* testsuite/gas/elf/section20.d: Likewise.
	* testsuite/gas/elf/section20.s: Likewise.
	* testsuite/gas/elf/section21.d: Likewise.
	* testsuite/gas/elf/section21.l: Likewise.
	* testsuite/gas/elf/section21.s: Likewise.

ld/

	PR ld/24526
	PR ld/25021
	PR ld/25490
	* testsuite/ld-elf/elf.exp: Run PR ld/25490 tests.
	* testsuite/ld-elf/pr24526.d: New file.
	* testsuite/ld-elf/pr24526.s: Likewise.
	* testsuite/ld-elf/pr25021.d: Likewise.
	* testsuite/ld-elf/pr25021.s: Likewise.
	* testsuite/ld-elf/pr25490-2-16.rd: Likewise.
	* testsuite/ld-elf/pr25490-2-32.rd: Likewise.
	* testsuite/ld-elf/pr25490-2-64.rd: Likewise.
	* testsuite/ld-elf/pr25490-2.s: Likewise.
	* testsuite/ld-elf/pr25490-3-16.rd: Likewise.
	* testsuite/ld-elf/pr25490-3-32.rd: Likewise.
	* testsuite/ld-elf/pr25490-3-64.rd: Likewise.
	* testsuite/ld-elf/pr25490-3.s: Likewise.
	* testsuite/ld-elf/pr25490-4-16.rd: Likewise.
	* testsuite/ld-elf/pr25490-4-32.rd: Likewise.
	* testsuite/ld-elf/pr25490-4-64.rd: Likewise.
	* testsuite/ld-elf/pr25490-4.s: Likewise.
	* testsuite/ld-elf/pr25490-5-16.rd: Likewise.
	* testsuite/ld-elf/pr25490-5-32.rd: Likewise.
	* testsuite/ld-elf/pr25490-5-64.rd: Likewise.
	* testsuite/ld-elf/pr25490-5.s: Likewise.
	* testsuite/ld-elf/pr25490-6-16.rd: Likewise.
	* testsuite/ld-elf/pr25490-6-32.rd: Likewise.
	* testsuite/ld-elf/pr25490-6-64.rd: Likewise.
	* testsuite/ld-elf/pr25490-6.s: Likewise.
2020-02-06 18:05:10 -08:00
Alan Modra 3024a17ae0 Mark all weak aliases for copy relocations
bfd/
	PR ld/25458
	* elflink.c (_bfd_elf_gc_mark_rsec): Mark all weak aliases.
ld/
	PR ld/25458
	* testsuite/ld-elf/pr25458.map: New file.
	* testsuite/ld-elf/pr25458.rd: Likewise.
	* testsuite/ld-elf/pr25458a.s: Likewise.
	* testsuite/ld-elf/pr25458b.s: Likewise.
	* testsuite/ld-elf/shared.exp: Run PR ld/25458 test.
2020-01-27 10:53:00 +10:30
Alan Modra 4c6ee6465a PR25351 .ARM.attributes not found for symbol
PR 25351
	* elflink.c (bfd_elf_final_link): Call _bfd_fix_excluded_sec_syms
	after removing sections.
2020-01-08 21:21:17 +10:30
Alan Modra b3adc24a07 Update year range in copyright notice of binutils files 2020-01-01 18:42:54 +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
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 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
Alan Modra 847d518374 Bogus "final link failed" messages
This patch is a result of noticing messages like the following:
tmpdir/tls32.o: in function `_start':
(.text+0x1c): unresolvable R_PPC_REL24 relocation against symbol `__tls_get_addr_opt'
./ld-new: final link failed: symbol needs debug section which does not exist

The "needs debug section" comes from attempting to use debug info to
find source line information to print the first error message.  That
error isn't of interest to the user, and any previous bfd_error value
which might be of interest is overwritten.  So save and restore
bfd_error around the fancy error reporting code.

That still doesn't leave us with a clean bfd_error.  Now we get
./ld-new: final link failed: nonrepresentable section on output
An unresolvable relocation surely doesn't mean there is some bfd
section that ld doesn't know how to output!  Digging into that showed
a _bfd_elf_section_from_bfd_section failure attempting to find an elf
section correcsponding to ".interp".  So don't go looking for elf
sections on linker created bfd sections.

And then fix the linker testsuite which expected the bogus message..

bfd/
	* elflink.c (elf_fixup_link_order): Don't attempt to find
	an elf_section for linker created bfd sections.
ld/
	* ldmisc.c (vfinfo): Save and restore bfd_error around bfd
	function calls that might set it.
	* testsuite/ld-elf/indirect.exp: Don't expect "nonrepresentable
	section" message.
2019-10-07 13:37:23 +10:30
Nick Alcock 1ff6de0312 bfd, ld: add CTF section linking
This is quite complicated because the CTF section's contents depend on
the final contents of the symtab and strtab, because it has two sections
whose contents are shuffled to be in 1:1 correspondence with the symtab,
and an internal strtab that gets deduplicated against the ELF strtab
(with offsets adjusted to point into the ELF strtab instead).  It is
also compressed if large enough, so its size depends on its contents!

So we cannot construct it as early as most sections: we cannot even
*begin* construction until after the symtab and strtab are finalized.
Thankfully there is already one section treated similarly: compressed
debugging sections: the only differences are that compressed debugging
sections have extra handling to deal with their changing name if
compressed (CTF sections are always called ".ctf" for now, though we
have reserved ".ctf.*" against future use), and that compressed
debugging sections have previously-uncompressed content which has to be
stashed away for later compression, while CTF sections have no content
at all until we generate it (very late).

BFD also cannot do the link itself: libctf knows how to do it, and BFD
cannot call libctf directly because libctf already depends on bfd for
file I/O.  So we have to use a pair of callbacks, one, examine_strtab,
which allows a caller to examine the symtab and strtab after
finalization (called from elf_link_swap_symbols_out(), right before the
symtabs are written, and after the strtab has been finalized), and one
which actually does the emission (called emit_ctf simply because it is
grouped with a bunch of section-specific late-emission function calls at
the bottom of bfd_elf_final_link, and a section-specific name seems best
for that).  emit_ctf is actually called *twice*: once from lang_process
if the emulation suggests that this bfd target does not examine the
symtab or strtab, and once via a bfd callback if it does.  (This means
that non-ELF targets still get CTF emitted, even though the late CTF
emission stage is never called for them).

v2: merged with non-ELF support patch: slight commit message
    adjustments.
v3: do not spend time merging CTF, or crash, if the CTF section is
    explicitly discarded.  Do not try to merge or compress CTF unless
    linking.
v4: add CTF_COMPRESSION_THRESHOLD.  Annul the freed input ctf_file_t's
    after writeout: set SEC_IN_MEMORY on the output contents so a future
    bfd enhancement knows it could free it.  Add SEC_LINKER_CREATED |
    SEC_KEEP to avoid having to add .ctf to the linker script.  Drop
    now-unnecessary ldlang.h-level elf-bfd.h include and hackery around
    it.  Adapt to elf32.em->elf.em and elf-generic.em->ldelf*.c
    changes.
v5: fix tabdamage.  Drop #inclusions in .h files: include in .c files,
    .em files, and use struct forwards instead.  Use bfd_section_is_ctf
    inline function rather than SECTION_IS_CTF macro.  Move a few
    comments.

	* Makefile.def (dependencies): all-ld depends on all-libctf.
	* Makefile.in: Regenerated.

include/
	* bfdlink.h (elf_strtab_hash): New forward.
	(elf_sym_strtab): Likewise.
	(struct bfd_link_callbacks <examine_strtab>): New.
	(struct bfd_link_callbacks <emit_ctf>): Likewise.

bfd/
	* elf-bfd.h (bfd_section_is_ctf): New inline function.
	* elf.c (special_sections_c): Add ".ctf".
	(assign_file_positions_for_non_load_sections): Note that
	compressed debugging sections etc are not assigned here.  Treat
	CTF sections like SEC_ELF_COMPRESS sections when is_linker_output:
	sh_offset -1.
	(assign_file_positions_except_relocs): Likewise.
	(find_section_in_list): Note that debugging and CTF sections, as
	well as reloc sections, are assigned later.
	(_bfd_elf_assign_file_positions_for_non_load): CTF sections get
	their size and contents updated.
	(_bfd_elf_set_section_contents): Skip CTF sections: unlike
	compressed sections, they have no uncompressed content to copy at
	this stage.
	* elflink.c (elf_link_swap_symbols_out): Call the examine_strtab
	callback right before the strtab is written out.
	(bfd_elf_final_link): Don't cache the section contents of CTF
	sections: they are not populated yet.  Call the emit_ctf callback
	right at the end, after all the symbols and strings are flushed
	out.

ld/
	* ldlang.h: (struct lang_input_statement_struct): Add the_ctf.
	(struct elf_sym_strtab): Add forward.
	(struct elf_strtab_hash): Likewise.
	(ldlang_ctf_apply_strsym): Declare.
	(ldlang_write_ctf_late): Likewise.
	* ldemul.h (ldemul_emit_ctf_early): New.
	(ldemul_examine_strtab_for_ctf): Likewise.
	(ld_emulation_xfer_type) <emit_ctf_early>: Likewise.
	(ld_emulation_xfer_type) <examine_strtab_for_ctf>: Likewise.
	* ldemul.c (ldemul_emit_ctf_early): New.
	(ldemul_examine_strtab_for_ctf): Likewise.
	* ldlang.c: Include ctf-api.h.
	(CTF_COMPRESSION_THRESHOLD): New.
	(ctf_output): New. Initialized in...
	(ldlang_open_ctf): ... this new function.  Open all the CTF
	sections in the input files: mark them non-loaded and empty
	so as not to copy their contents to the output, but linker-created
	so the section gets created in the target.
	(ldlang_merge_ctf): New, merge types via ctf_link_add_ctf and
	ctf_link.
	(ldlang_ctf_apply_strsym): New, an examine_strtab callback: wrap
	ldemul_examine_strtab_for_ctf.
	(lang_write_ctf): New, write out the CTF section.
	(ldlang_write_ctf_late): New, late call via bfd's emit_ctf hook.
	(lang_process): Call ldlang_open_ctf, ldlang_merge_ctf, and
	lang_write_ctf.
	* ldmain.c (link_callbacks): Add ldlang_ctf_apply_strsym,
	ldlang_write_ctf_late.
	* emultempl/aix.em: Add ctf-api.h.
	* emultempl/armcoff.em: Likewise.
	* emultempl/beos.em: Likewise.
	* emultempl/elf.em: Likewise.
	* emultempl/generic.em: Likewise.
	* emultempl/linux.em: Likewise.
	* emultempl/msp430.em: Likewise.
	* emultempl/pe.em: Likewise.
	* emultempl/pep.em: Likewise.
	* emultempl/ticoff.em: Likewise.
	* emultempl/vanilla.em: Likewise.
	* ldcref.c: Likewise.
	* ldctor.c: Likewise.
	* ldelf.c: Likewise.
	* ldelfgen.c: Likewise.
	* ldemul.c: Likewise.
	* ldexp.c: Likewise.
	* ldfile.c: Likewise.
	* ldgram.c: Likewise.
	* ldlex.l: Likewise.
	* ldmain.c: Likewise.
	* ldmisc.c: Likewise.
	* ldver.c: Likewise.
	* ldwrite.c: Likewise.
	* lexsup.c: Likewise.
	* mri.c: Likewise.
	* pe-dll.c: Likewise.
	* plugin.c: Likewise.

	* ldelfgen.c (ldelf_emit_ctf_early): New.
	(ldelf_examine_strtab_for_ctf): tell libctf about the symtab and
	strtab.
	(struct ctf_strsym_iter_cb_arg): New, state to do so.
	(ldelf_ctf_strtab_iter_cb): New: tell libctf about
	each string in the strtab in turn.
	(ldelf_ctf_symbols_iter_cb): New, tell libctf
	about each symbol in the symtab in turn.
	* ldelfgen.h (struct elf_sym_strtab): Add forward.
	(struct elf_strtab_hash): Likewise.
	(struct ctf_file): Likewise.
	(ldelf_emit_ctf_early): Declare.
	(ldelf_examine_strtab_for_ctf): Likewise.
	* emultempl/elf-generic.em (LDEMUL_EMIT_CTF_EARLY): Set it.
	(LDEMUL_EXAMINE_STRTAB_FOR_CTF): Likewise.
	* emultempl/aix.em (ld_${EMULATION_NAME}_emulation): Add
	emit_ctf_early and examine_strtab_for_ctf, NULL by default.
	* emultempl/armcoff.em (ld_${EMULATION_NAME}_emulation): Likewise.
	* emultempl/beos.em (ld_${EMULATION_NAME}_emulation): Likewise.
	* emultempl/elf.em (ld_${EMULATION_NAME}_emulation): Likewise.
	* emultempl/generic.em (ld_${EMULATION_NAME}_emulation): Likewise.
	* emultempl/linux.em (ld_${EMULATION_NAME}_emulation): Likewise.
	* emultempl/msp430.em (ld_${EMULATION_NAME}_emulation): Likewise.
	* emultempl/pe.em (ld_${EMULATION_NAME}_emulation): Likewise.
	* emultempl/pep.em (ld_${EMULATION_NAME}_emulation): Likewise.
	* emultempl/ticoff.em (ld_${EMULATION_NAME}_emulation): Likewise.
	* emultempl/vanilla.em (ld_vanilla_emulation): Likewise.

	* Makefile.am: Pull in libctf (and zlib, a transitive requirement
	for compressed CTF section emission).  Pass it on to DejaGNU.
	* configure.ac: Add AM_ZLIB.
	* aclocal.m4: Added zlib.m4.
	* Makefile.in: Regenerated.
	* testsuite/ld-bootstrap/bootstrap.exp: Use it when relinking ld.
2019-10-03 17:04:56 +01:00
Alan Modra fd3619828e bfd_section_* macros
This large patch removes the unnecessary bfd parameter from various
bfd section macros and functions.  The bfd is hardly ever used and if
needed for the bfd_set_section_* or bfd_rename_section functions can
be found via section->owner except for the com, und, abs, and ind
std_section special sections.  Those sections shouldn't be modified
anyway.

The patch also removes various bfd_get_section_<field> macros,
replacing their use with bfd_section_<field>, and adds
bfd_set_section_lma.  I've also fixed a minor bug in gas where
compressed section renaming was done directly rather than calling
bfd_rename_section.  This would have broken bfd_get_section_by_name
and similar functions, but that hardly mattered at such a late stage
in gas processing.

bfd/
	* bfd-in.h (bfd_get_section_name, bfd_get_section_vma),
	(bfd_get_section_lma, bfd_get_section_alignment),
	(bfd_get_section_size, bfd_get_section_flags),
	(bfd_get_section_userdata): Delete.
	(bfd_section_name, bfd_section_size, bfd_section_vma),
	(bfd_section_lma, bfd_section_alignment): Lose bfd parameter.
	(bfd_section_flags, bfd_section_userdata): New.
	(bfd_is_com_section): Rename parameter.
	* section.c (bfd_set_section_userdata, bfd_set_section_vma),
	(bfd_set_section_alignment, bfd_set_section_flags, bfd_rename_section),
	(bfd_set_section_size): Delete bfd parameter, rename section parameter.
	(bfd_set_section_lma): New.
	* bfd-in2.h: Regenerate.
	* mach-o.c (bfd_mach_o_init_section_from_mach_o): Delete bfd param,
	update callers.
	* aoutx.h, * bfd.c, * coff-alpha.c, * coff-arm.c, * coff-mips.c,
	* coff64-rs6000.c, * coffcode.h, * coffgen.c, * cofflink.c,
	* compress.c, * ecoff.c, * elf-eh-frame.c, * elf-hppa.h,
	* elf-ifunc.c, * elf-m10200.c, * elf-m10300.c, * elf-properties.c,
	* elf-s390-common.c, * elf-vxworks.c, * elf.c, * elf32-arc.c,
	* elf32-arm.c, * elf32-avr.c, * elf32-bfin.c, * elf32-cr16.c,
	* elf32-cr16c.c, * elf32-cris.c, * elf32-crx.c, * elf32-csky.c,
	* elf32-d10v.c, * elf32-epiphany.c, * elf32-fr30.c, * elf32-frv.c,
	* elf32-ft32.c, * elf32-h8300.c, * elf32-hppa.c, * elf32-i386.c,
	* elf32-ip2k.c, * elf32-iq2000.c, * elf32-lm32.c, * elf32-m32c.c,
	* elf32-m32r.c, * elf32-m68hc1x.c, * elf32-m68k.c, * elf32-mcore.c,
	* elf32-mep.c, * elf32-metag.c, * elf32-microblaze.c,
	* elf32-moxie.c, * elf32-msp430.c, * elf32-mt.c, * elf32-nds32.c,
	* elf32-nios2.c, * elf32-or1k.c, * elf32-ppc.c, * elf32-pru.c,
	* elf32-rl78.c, * elf32-rx.c, * elf32-s390.c, * elf32-score.c,
	* elf32-score7.c, * elf32-sh.c, * elf32-spu.c, * elf32-tic6x.c,
	* elf32-tilepro.c, * elf32-v850.c, * elf32-vax.c, * elf32-visium.c,
	* elf32-xstormy16.c, * elf32-xtensa.c, * elf64-alpha.c,
	* elf64-bpf.c, * elf64-hppa.c, * elf64-ia64-vms.c, * elf64-mmix.c,
	* elf64-ppc.c, * elf64-s390.c, * elf64-sparc.c, * elf64-x86-64.c,
	* elflink.c, * elfnn-aarch64.c, * elfnn-ia64.c, * elfnn-riscv.c,
	* elfxx-aarch64.c, * elfxx-mips.c, * elfxx-sparc.c,
	* elfxx-tilegx.c, * elfxx-x86.c, * i386msdos.c, * linker.c,
	* mach-o.c, * mmo.c, * opncls.c, * pdp11.c, * pei-x86_64.c,
	* peicode.h, * reloc.c, * section.c, * syms.c, * vms-alpha.c,
	* xcofflink.c: Update throughout for bfd section macro and function
	changes.
binutils/
	* addr2line.c, * bucomm.c, * coffgrok.c, * dlltool.c, * nm.c,
	* objcopy.c, * objdump.c, * od-elf32_avr.c, * od-macho.c,
	* od-xcoff.c, * prdbg.c, * rdcoff.c, * rddbg.c, * rescoff.c,
	* resres.c, * size.c, * srconv.c, * strings.c, * windmc.c: Update
	throughout for bfd section macro and function changes.
gas/
	* as.c, * as.h, * dw2gencfi.c, * dwarf2dbg.c, * ecoff.c,
	* read.c, * stabs.c, * subsegs.c, * subsegs.h, * write.c,
	* config/obj-coff-seh.c, * config/obj-coff.c, * config/obj-ecoff.c,
	* config/obj-elf.c, * config/obj-macho.c, * config/obj-som.c,
	* config/tc-aarch64.c, * config/tc-alpha.c, * config/tc-arc.c,
	* config/tc-arm.c, * config/tc-avr.c, * config/tc-bfin.c,
	* config/tc-bpf.c, * config/tc-d10v.c, * config/tc-d30v.c,
	* config/tc-epiphany.c, * config/tc-fr30.c, * config/tc-frv.c,
	* config/tc-h8300.c, * config/tc-hppa.c, * config/tc-i386.c,
	* config/tc-ia64.c, * config/tc-ip2k.c, * config/tc-iq2000.c,
	* config/tc-lm32.c, * config/tc-m32c.c, * config/tc-m32r.c,
	* config/tc-m68hc11.c, * config/tc-mep.c, * config/tc-microblaze.c,
	* config/tc-mips.c, * config/tc-mmix.c, * config/tc-mn10200.c,
	* config/tc-mn10300.c, * config/tc-msp430.c, * config/tc-mt.c,
	* config/tc-nds32.c, * config/tc-or1k.c, * config/tc-ppc.c,
	* config/tc-pru.c, * config/tc-rl78.c, * config/tc-rx.c,
	* config/tc-s12z.c, * config/tc-s390.c, * config/tc-score.c,
	* config/tc-score7.c, * config/tc-sh.c, * config/tc-sparc.c,
	* config/tc-spu.c, * config/tc-tic4x.c, * config/tc-tic54x.c,
	* config/tc-tic6x.c, * config/tc-tilegx.c, * config/tc-tilepro.c,
	* config/tc-v850.c, * config/tc-visium.c, * config/tc-wasm32.c,
	* config/tc-xc16x.c, * config/tc-xgate.c, * config/tc-xstormy16.c,
	* config/tc-xtensa.c, * config/tc-z8k.c: Update throughout for
	bfd section macro and function changes.
	* write.c (compress_debug): Use bfd_rename_section.
gdb/
	* aarch64-linux-tdep.c, * arm-tdep.c, * auto-load.c,
	* coff-pe-read.c, * coffread.c, * corelow.c, * dbxread.c,
	* dicos-tdep.c, * dwarf2-frame.c, * dwarf2read.c, * elfread.c,
	* exec.c, * fbsd-tdep.c, * gcore.c, * gdb_bfd.c, * gdb_bfd.h,
	* hppa-tdep.c, * i386-cygwin-tdep.c, * i386-fbsd-tdep.c,
	* i386-linux-tdep.c, * jit.c, * linux-tdep.c, * machoread.c,
	* maint.c, * mdebugread.c, * minidebug.c, * mips-linux-tdep.c,
	* mips-sde-tdep.c, * mips-tdep.c, * mipsread.c, * nto-tdep.c,
	* objfiles.c, * objfiles.h, * osabi.c, * ppc-linux-tdep.c,
	* ppc64-tdep.c, * record-btrace.c, * record-full.c, * remote.c,
	* rs6000-aix-tdep.c, * rs6000-tdep.c, * s390-linux-tdep.c,
	* s390-tdep.c, * solib-aix.c, * solib-dsbt.c, * solib-frv.c,
	* solib-spu.c, * solib-svr4.c, * solib-target.c,
	* spu-linux-nat.c, * spu-tdep.c, * symfile-mem.c, * symfile.c,
	* symmisc.c, * symtab.c, * target.c, * windows-nat.c,
	* xcoffread.c, * cli/cli-dump.c, * compile/compile-object-load.c,
	* mi/mi-interp.c: Update throughout for bfd section macro and
	function changes.
	* gcore (gcore_create_callback): Use bfd_set_section_lma.
	* spu-tdep.c (spu_overlay_new_objfile): Likewise.
gprof/
	* corefile.c, * symtab.c: Update throughout for bfd section
	macro and function changes.
ld/
	* ldcref.c, * ldctor.c, * ldelf.c, * ldlang.c, * pe-dll.c,
	* emultempl/aarch64elf.em, * emultempl/aix.em,
	* emultempl/armcoff.em, * emultempl/armelf.em,
	* emultempl/cr16elf.em, * emultempl/cskyelf.em,
	* emultempl/m68hc1xelf.em, * emultempl/m68kelf.em,
	* emultempl/mipself.em, * emultempl/mmix-elfnmmo.em,
	* emultempl/mmo.em, * emultempl/msp430.em,
	* emultempl/nios2elf.em, * emultempl/pe.em, * emultempl/pep.em,
	* emultempl/ppc64elf.em, * emultempl/xtensaelf.em: Update
	throughout for bfd section macro and function changes.
libctf/
	* ctf-open-bfd.c: Update throughout for bfd section macro changes.
opcodes/
	* arc-ext.c: Update throughout for bfd section macro changes.
sim/
	* common/sim-load.c, * common/sim-utils.c, * cris/sim-if.c,
	* erc32/func.c, * lm32/sim-if.c, * m32c/load.c, * m32c/trace.c,
	* m68hc11/interp.c, * ppc/hw_htab.c, * ppc/hw_init.c,
	* rl78/load.c, * rl78/trace.c, * rx/gdb-if.c, * rx/load.c,
	* rx/trace.c: Update throughout for bfd section macro changes.
2019-09-19 09:40:13 +09:30
Alan Modra ed48ec2e6e Don't use bfd_get_* macro to set bfd fields
* aoutx.h (slurp_symbol_table): Don't set symcount using bfd_get
	macros.
	* pdp11.c (slurp_symbol_table): Likewise.
	* som.c (som_slurp_symbol_table): Likewise.
	* coff-ppc.c (ppc_bfd_coff_final_link): Likewise.
	* coffcode.h (coff_slurp_symbol_table): Likewise.
	* cofflink.c (_bfd_coff_final_link): Likewise.
	* ecoff.c (ecoff_slurp_symbolic_header): Likewise.
	(_bfd_ecoff_slurp_symbolic_info): Likewise.
	(_bfd_ecoff_slurp_symbol_table): Likewise.
	(_bfd_ecoff_bfd_final_link): Likewise.
	* elf.c (_bfd_elf_canonicalize_symtab): Likewise.
	* elflink.c (elf_link_output_symstrtab): Likewise.
	(bfd_elf_final_link): Likewise.
	* peicode.h (pe_ILF_build_a_bfd): Likewise.
	* xcofflink.c (_bfd_xcoff_bfd_final_link): Likewise.
	* aoutx.h (some_aout_object_p, slurp_symbol_table): Don't set
	start_address or symcount using bfd_get macros.
	* coffgen.c (coff_real_object_p): Likewise.
	* pdp11.c (some_aout_object_p, slurp_symbol_table): Likewise.
	* som.c (som_object_setup, som_slurp_symbol_table): Likewise.
	* elfcore.h (elf_core_file_p): Don't set start_address using
	bfd_get macro.
	* elf.c (_bfd_elf_canonicalize_dynamic_symtab): Don't set dynsymcount
	using bfd_get macro.
	* bfd.c (bfd_set_file_flags): Don't set flags using bfd_get macro.
	* linker.c (bfd_generic_link_read_symbols): Don't set outsymbols
	or symcount using bfd_get macros.
	(_bfd_generic_final_link, generic_add_output_symbol): Likewise.
	* syms.c (bfd_set_symtab): Likewise.
	* vms-alpha.c (alpha_vms_bfd_final_link): Likewise.
	* archive.c (do_slurp_bsd_armap): Don't set has_armap using
	bfd_has_map macro.
	(do_slurp_coff_armap, bfd_slurp_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.
	* ecoff.c (_bfd_ecoff_slurp_armap): Likewise.
	* som.c (som_slurp_armap): Likewise.
2019-09-18 22:21:58 +09:30
Martin Liska b4c555cfc4
Fix detection of missing plugin for LTO objects.
2019-08-16  Martin Liska  <mliska@suse.cz>

	PR ld/24912
	* elflink.c: Report error only for not relocatable.
	* linker.c (_bfd_generic_link_add_one_symbol): Do not handle
	here lto_slim_object as it's handled in caller.
2019-08-16  Martin Liska  <mliska@suse.cz>

	PR ld/24912
	* object.cc (big_endian>::do_layout): Do not report error,
	but only set a flag.
	(big_endian>::do_add_symbols): Report error only for when
	relocatable.
2019-08-16 13:14:36 +02:00
Mihailo Stojanovic f16a9783c5 Add support for a MIPS specific .MIPS.xhash section.
This patch is a reimplementation of [1] which was submitted in 2015 by
Neil Schellenberger. Copyright issue was sorted out [2] last year.
It proposed a new section (.gnu.xhash) and related dynamic tag
(DT_GNU_XHASH). The new section would be virtually identical to the
existing .gnu.hash except for the translation table (xlat) which would
contain correct MIPS .dynsym indexes corresponding to the hashvals in
chains. This is because MIPS ABI imposes a different ordering on the
dynsyms than the one expected by the .gnu.hash section. Another addition
would be a leading word (ngnusyms) which would contain the number of
entries in the translation table.

  In this patch, the new section name and dynamic tag are changed to
reflect the fact that the section should be treated as MIPS-specific
(.MIPS.xhash and DT_MIPS_XHASH).

  This patch addresses the alignment issue as reported in [3], which is
caused by the leading word added to the .MIPS.xhash section. Leading word
is removed in this patch, and the number of entries in the translation
table is now calculated using DT_MIPS_SYMTABNO dynamic tag (this is
addressed by the corresponding glibc patch).

  Suggestions on coding style in [4] were taken into account. Existing
GNU hash testcase was covered, and another one was added in the MIPS
part of the testsuite.

  The other major change is reserving MIPS ABI version 5 for .MIPS.xhash,
marking the need of support for .MIPS.xhash in the dynamic linker (again,
addressed in the corresponding glibc patch). This is something which I
am not sure of, especially after reading [5]. I am confused on whether
this ABI version is reserved for IFUNC, or it can be used for this
purpose.

Already mentioned glibc patch is submitted at:
https://sourceware.org/ml/libc-alpha/2019-06/msg00456.html

[1] https://sourceware.org/ml/binutils/2015-10/msg00057.html
[2] https://sourceware.org/ml/binutils/2018-03/msg00025.html
[3] https://sourceware.org/ml/binutils/2016-01/msg00006.html
[4] https://sourceware.org/ml/binutils/2016-02/msg00097.html
[5] https://sourceware.org/ml/libc-alpha/2016-12/msg00853.html

ld      * emulparams/elf32bmip.sh: Add .MIPS.xhash section.
        * emulparams/elf32bmipn32-defs.sh: Add .MIPS.xhash section.
        * emulparams/elf64bmip-defs.sh: Add .MIPS.xhash section.
        * emultempl/mipself.em: Remove mips_after_parse function.
        * testsuite/ld-elf/hash.d: Update comment.
        * testsuite/ld-mips-elf/hash1.d: New test.
        * testsuite/ld-mips-elf/hash1.s: Ditto.
        * testsuite/ld-mips-elf/hash1a.d: Remove.
        * testsuite/ld-mips-elf/hash1b.d: Ditto.
        * testsuite/ld-mips-elf/hash1c.d: Ditto
        * testsuite/ld-mips-elf/hash2.d: New test.
        * testsuite/ld-mips-elf/mips-elf.exp: New tests.
        * testsuite/ld-mips-elf/start.s: New test.

bfd     * elf-bfd.h (struct elf_backend_data): New members.
        * elflink.c (_bfd_elf_link_create_dynamic_sections): Create
        .gnu.hash section if necessary.
        (struct collect_gnu_hash_codes): New member.
        (elf_gnu_hash_process_symidx): New function name.
        (elf_renumber_gnu_hash_syms): Ignore local and undefined
        symbols. Record xlat location for every symbol which should have
        a .MIPS.xhash entry.
        (bfd_elf_size_dynamic_sections): Add DT_GNU_HASH dynamic tag to
        dynamic section if necessary.
        (GNU_HASH_SECTION_NAME): New define.
        (bfd_elf_size_dynsym_hash_dynstr): Get .MIPS.xhash section.
        Update the section size info.
        * elfxx-mips.c (struct mips_elf_hash_sort_data): New members.
        (struct mips_elf_link_hash_entry): New member.
        (mips_elf_link_hash_newfunc): Initialize .MIPS.xhash translation
        table location.
        (mips_elf_sort_hash_table): Initialize the pointer to the
        .MIPS.xhash section.
        (mips_elf_sort_hash_table_f): Populate the .MIPS.xhash
        translation table entry with the symbol dynindx.
        (_bfd_mips_elf_section_from_shdr): Add SHT_MIPS_XHASH.
        (_bfd_mips_elf_fake_sections): Initialize .MIPS.xhash section
        info.
        (_bfd_mips_elf_create_dynamic_sections): Create .MIPS.xhash
        section.
        (_bfd_mips_elf_size_dynamic_sections): Add DT_MIPS_XHASH tag to
        dynamic section.
        (_bfd_mips_elf_finish_synamic_sections): Add DT_MIPS_XHASH.
        (_bfd_mips_elf_final_write_processing): Set .MIPS.xhash section
        sh_link info.
        (_bfd_mips_elf_get_target_dtag): Get DT_MIPS_XHASH tag.
        (MIPS_LIBC_ABI_XHASH): New ABI version enum value.
        (_bfd_mips_post_process_headers): Mark the ABI version as
        MIPS_LIBC_ABI_XHASH if there exists a .MIPS.xhash section,
        but not a .hash section.
        (_bfd_mips_elf_record_xhash_symbol): New function. Record a
        position in the translation table, associated with the hash
        entry.
        * elfxx-mips.h (literal_reloc_p): Define
        elf_backend_record_xhash_symbol backend hook.
        * elfxx-target.h: Initialize elf_backend_record_xhash_symbol
        backend hook.

include * elf/mips.h (SHT_GNU_XHASH): New define.
        (DT_GNU_XHASH): New define.

binutils * readelf.c (get_mips_dynamic_type): Return MIPS_XHASH dynamic type.
        (get_mips_section_type_name): Return MI{S_XHASH name string.
        (dynamic_section_mips_val): Initialize the .MIPS.xhash dynamic
        info.
        (process_symbol_table): Initialize the .MIPS.xhash section
        pointer. Adjust the readelf output to support the new section.
        (process_object): Set the .MIPS.xhash dynamic info to zero.
2019-08-09 11:06:37 +01:00
Martin Liska cc5277b173 Support .gnu.lto_.lto section in ELF files (PR 24768).
bfd/ChangeLog:

2019-07-22  Martin Liska  <mliska@suse.cz>

	PR 24768
	* archive.c (_bfd_compute_and_write_armap): Come up with
	report_plugin_err variable.
	* bfd-in2.h (struct bfd): Add lto_slim_object flag.
	* elf.c (struct lto_section): New.
	(_bfd_elf_make_section_from_shdr): Parse content of
	.gnu_lto_.lto section.
	* elflink.c: Report error for a missing LTO plugin.
	* linker.c (_bfd_generic_link_add_one_symbol): Likewise.

binutils/ChangeLog:

2019-07-22  Martin Liska  <mliska@suse.cz>

	PR 24768
	* nm.c (filter_symbols): Set report_plugin_err if
	error is reported.
	(display_rel_file): Report error for a missing LTO plugin.

gold/ChangeLog:

2019-07-22  Martin Liska  <mliska@suse.cz>

	PR 24768
	* layout.h (class Layout): Add is_lto_slim_object and
	set_lto_slim_object.
	* object.cc (struct lto_section): Add lto_slim_object_.
	(big_endian>::do_layout): Parse content of
	.gnu_lto_.lto section.
	(big_endian>::do_add_symbols): Report error for a missing
	LTO plugin.
2019-07-29 10:11:44 +02:00
Alan Modra 4538d1c7c4 PR24857, ld: error adding symbols: bad value
This fixes two cases where elf_link_add_object_symbols returns an
error, setting the catch-all bfd_error_bad_value without explaining
the error.  The second one is an internal error that can only be
caused by a target elf_add_symbol_hook, so make that one abort.  The
first one is my PR24339 fix.  PR24339 is another of those fuzzing bugs
and the fix I made catches the problem when loading symbols, rather
than when symbols are used in relocs.  While ld is correct to reject
the object file as not complying with the ELF standard, let's be a
little more forgiving for dynamic objects.

	PR 24857
	PR 24339
	* elflink.c (elf_link_add_object_symbols): Report an informative
	error on finding local symbols with index equal or greater than
	symbol table sh_info.  Correct comment.  Allow such symbols in
	dynamic objects.  Abort on NULL section for symbol.
2019-07-28 19:11:48 +09:30
Alan Modra 6dd17ae0f9 Begone elf_linker
This field effectively became usused a long time ago, perhaps as early
as 1994.

	* elf-bfd.h (struct output_elf_obj_tdata): Delete "linker" field.
	(elf_linker): Don't define.
	* elflink.c (bfd_elf_final_link): Don't set elf_linker.
2019-07-26 10:12:56 +09:30
Alan Modra 06f44071cc ELF final_write_processing
Move setting of OSABI to final_write_processing, after symbols are
written.  This allows OSABI to be set based on symbols actually
written to the output rather than assuming input symbols will be
output.

Corrects object files such as the one generated on powerpc64le-linux
by ld-powerpc/pr23927.s which has a local STT_GNU_IFUNC symbol but
prior to this patch the file was marked ELFOSABI_NONE.

	* elf-bfd.h (enum elf_gnu_osabi): Rename from elf_gnu_symbols.
	Remove none, any, all enums.
	(struct elf_obj_tdata): Rename has_gnu_symbols field to has_gnu_osabi.
	(_bfd_elf_final_write_processing): Declare.
	* elf.c (_bfd_elf_write_object_contents): Unconditionally call
	elf_backend_final_write_processing.
	(_bfd_elf_post_process_headers): Move body of function to..
	(_bfd_elf_final_write_processing): ..here, but set EI_OSABI byte
	only when not already set.  Adjust for rename.
	* elfxx-target.h (elf_backend_final_write_processing): Default to
	_bfd_elf_final_write_processing.
	* elf-hppa.h (elf_hppa_final_write_processing): Call
	_bfd_elf_final_write_processing.
	* elf-m10300.c (_bfd_mn10300_elf_final_write_processing): Likewise.
	* elf-nacl.c (nacl_final_write_processing): Likewise.
	* elf-vxworks.c (elf_vxworks_final_write_processing): Likewise.
	* elf32-arc.c (arc_elf_final_write_processing): Likewise.
	* elf32-avr.c (bfd_elf_avr_final_write_processing): Likewise.
	* elf32-bfin.c (elf32_bfin_final_write_processing): Likewise.
	* elf32-cr16.c (_bfd_cr16_elf_final_write_processing): Likewise.
	* elf32-cris.c (cris_elf_final_write_processing): Likewise.
	* elf32-h8300.c (elf32_h8_final_write_processing): Likewise.
	* elf32-lm32.c (lm32_elf_final_write_processing): Likewise.
	* elf32-m32r.c (m32r_elf_final_write_processing): Likewise.
	* elf32-m68k.c (elf_m68k_final_write_processing): Likewise.
	* elf32-msp430.c (bfd_elf_msp430_final_write_processing): Likewise.
	* elf32-nds32.c (nds32_elf_final_write_processing): Likewise.
	* elf32-or1k.c (or1k_elf_final_write_processing): Likewise.
	* elf32-pj.c (pj_elf_final_write_processing): Likewise.
	* elf32-v850.c (v850_elf_final_write_processing): Likewise.
	* elf32-xc16x.c (elf32_xc16x_final_write_processing): Likewise.
	* elf32-xtensa.c (elf_xtensa_final_write_processing): Likewise.
	* elf64-ia64-vms.c (elf64_vms_final_write_processing): Likewise.
	* elfnn-ia64.c (elfNN_ia64_final_write_processing): Likewise.
	* elf32-arm.c (arm_final_write_processing): Split out from..
	(elf32_arm_final_write_processing): ..here.  Call
	_bfd_elf_final_write_processing.
	(elf32_arm_nacl_final_write_processing): Adjust.
	* elfxx-mips.c (_bfd_mips_final_write_processing): Split out from..
	(_bfd_mips_elf_final_write_processing): ..here.  Call
	_bfd_elf_final_write_processing.
	* elfxx-mips.h (_bfd_mips_final_write_processing): Declare.
	* elf32-mips.c (mips_vxworks_final_write_processing): Adjust.
	* elf32-ppc.c (ppc_final_write_processing): Split out from..
	(ppc_elf_final_write_processing): ..here.  Call
	_bfd_elf_final_write_processing.
	(ppc_elf_vxworks_final_write_processing): Adjust.
	* elf32-sparc.c (sparc_final_write_processing): Split out from..
	(elf32_sparc_final_write_processing): ..here.  Call
	_bfd_elf_final_write_processing.
	(elf32_sparc_vxworks_final_write_processing): Adjust.
	* elf32-d10v.c (elf_backend_final_write_processing): Don't define.
	* elf32-d30v.c (elf_backend_final_write_processing): Don't define.
	* elf32-m68hc11.c (elf_backend_final_write_processing): Don't define.
	* elf32-m68hc12.c (elf_backend_final_write_processing): Don't define.
	* elf32-s12z.c (elf_backend_final_write_processing): Don't define.
	* elf32-i386.c (elf_i386_check_relocs): Don't set has_gnu_symbols.
	* elf64-x86-64.c (elf_x86_64_check_relocs): Likewise.
	* elflink.c (elf_link_add_object_symbols): Likewise.
	(elf_link_output_symstrtab): Set has_gnu_osabi for symbols here
	instead.
2019-07-23 18:22:02 +09:30
Alan Modra 5b9d7a9a64 Fix __bss_start assertion failure in _bfd_elf_fix_symbol_flags
> Building LLVM 6.0 on FreeBSD/powerpc (devel/llvm60 port) the assertion
> in the subject trips (displays twice) when linking libLTO.so.1.  The
> issue has been filed in FreeBSD's bugzilla, at
> https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=237068 .  It appears
> the 'llvm::hashing::detail::get_execution_seed()::seed@@JL_LLVM_6.0'
> symbol is being weakly aliased to an indirect symbol
> __bss_start@@JL_LLVM_6.0.  Since __bss_start@@JL_LLVM_6.0 is an
> indirect symbol, it fails the assertion.

I haven't looked under a debugger at your testcase but I think I know
what is going on here.  You have a shared library with a weakly
defined llvm::hashing::detail::get_execution_seed()::seed which
happens to be at the same location as __bss_start in that library.  At
the time the linker loads symbols for that library, it sees they are
both versioned and thus introduces non-versioned indirect symbols for
them.  The linker considers the symbols as possibly being aliases,
setting up h->u.alias and h->is_weakalias such that
__bss_start@@JL_LLVM_6.0 is the definition.  No real problem so far,
the definition is bfd_link_hash_defined, except that the zero size, no
type __bss_start symbol possibly should not be considered an alias in
the first place.

Later, __bss_start as defined by the linker script is entered into the
linker symbol table.  This is similar to __bss_start being defined by
a regular object file in that ELF symbol resolution rules say that the
value of __bss_start in the library is overridden by __bss_start in
the executable/library being produced.  So to accomplish the override,
ld flips __bss_start from being an indirect symbol pointing at
__bss_start@@JL_LLVM_6.0 to __bss_start@@JL_LLVM_6.0 being an indirect
symbol pointing at __bss_start.  That's how we get an unexpected
indirect symbol and hit the assert.

What should happen I think, is for the def->def_regular code above the
assert to run in this case.  The symbols are no longer aliases.

	* elflink.c (_bfd_elf_fix_symbol_flags): If the def for an
	alias is no longer bfd_link_hash_defined, clear the alias.
2019-07-15 16:02:42 +09:30
Alan Modra f26a32876b Dynamic TLS section symbols
It is possible to create shared libraries on PowerPC using
-ftls-model=inital-exec or -ftls-model=local-exec.  The first is half
reasonable, getting you a shared library that can't be dlopen'd but
otherwise is reasonable.  The second is quite bad.  Not only do you
lose being able to dlopen, the library also has dynamic text
relocations.  Worse, the TPREL16_LO, TPREL16_HA and other TPREL16
dynamic relocs emitted were wrong, resulting in wrong values being
applied by ld.so.

Using the first TLS section symbol in dynamic relocations for local
TLS symbols doesn't work.  It's wrong because TLS symbols used by TLS
relocs have values relative to the TLS segment, whereas the TLS
section symbols are addresses.  This patch instead uses a symbol index
of zero which is used elsewhere by PowerPC on dynamic TLS relocs.
It's not strictly ABI compliant to use a non-TLS symbol with TLS
relocs but symbol index zero can be interpreted as "no symbol".  Not
using the first TLS section symbol means it doesn't need to be dynamic.

The patch also fixes a further problem with PowerPC32 dynamic TPREL16*
relocs, which shouldn't have the symbol value in the addend as we do
for non-TLS symbols.

bfd/
	* elflink.c (_bfd_elf_omit_section_dynsym_default): Don't keep
	tls_sec.
	(_bfd_elf_init_1_index_section): Prefer not using TLS sections.
	(_bfd_elf_init_2_index_sections): Likewise.
	* elf64-ppc.c (ppc64_elf_relocate_section): When emitting dynamic
	relocations for local TLS symbols, use STN_UNDEF as the relocation
	symbol.
	* elf32-ppc.c (ppc_elf_relocate_section): Likewise, and don't
	leave TLS symbol value in the addend.
ld/
	* testsuite/ld-powerpc/tlsso.r: Update.
	* testsuite/ld-powerpc/tlsso32.g: Update.
	* testsuite/ld-powerpc/tlsso32.r: Update.
	* testsuite/ld-powerpc/tlstocso.r: Update.
	* testsuite/ld-cris/tls-dso-dtpoffd2.d: Update.
	* testsuite/ld-cris/tls-dso-dtpoffd4.d: Update.
	* testsuite/ld-cris/tls-dso-tpoffgotcomm1.d: Update.
	* testsuite/ld-cris/tls-gd-1.d: Update.
	* testsuite/ld-cris/tls-gd-1h.d: Update.
	* testsuite/ld-cris/tls-gd-2.d: Update.
	* testsuite/ld-cris/tls-gd-2h.d: Update.
	* testsuite/ld-cris/tls-ie-10.d: Update.
	* testsuite/ld-cris/tls-ie-11.d: Update.
	* testsuite/ld-cris/tls-ie-8.d: Update.
	* testsuite/ld-cris/tls-ie-9.d: Update.
	* testsuite/ld-cris/tls-js1.d: Update.
	* testsuite/ld-cris/tls-ld-4.d: Update.
	* testsuite/ld-cris/tls-ld-5.d: Update.
	* testsuite/ld-cris/tls-ld-6.d: Update.
	* testsuite/ld-cris/tls-ld-7.d: Update.
	* testsuite/ld-cris/tls-ldgd-14.d: Update.
	* testsuite/ld-cris/tls-ldgd-15.d: Update.
	* testsuite/ld-cris/tls-ldgdx-14.d: Update.
	* testsuite/ld-cris/tls-ldgdx-15.d: Update.
	* testsuite/ld-cris/tls-local-54.d: Update.
	* testsuite/ld-cris/tls-local-60.d: Update.
	* testsuite/ld-cris/tls-local-61.d: Update.
	* testsuite/ld-cris/tls-local-63.d: Update.
	* testsuite/ld-cris/tls-local-64.d: Update.
	* testsuite/ld-cris/tls-ok-30.d: Update.
	* testsuite/ld-cris/tls-ok-32.d: Update.
	* testsuite/ld-cris/tls-ok-34.d: Update.
	* testsuite/ld-mips-elf/tls-multi-got-1.got: Update.
	* testsuite/ld-mips-elf/tls-multi-got-1.r: Update.
	* testsuite/ld-mips-elf/tlsdyn-pie-o32.d: Update.
	* testsuite/ld-mips-elf/tlsdyn-pie-o32.got: Update.
	* testsuite/ld-mips-elf/tlslib-o32-hidden.got: Update.
	* testsuite/ld-mips-elf/tlslib-o32-ver.got: Update.
	* testsuite/ld-mips-elf/tlslib-o32.got: Update.
	* testsuite/ld-s390/tlspic.rd: Update.
	* testsuite/ld-s390/tlspic_64.rd: Update.
	* testsuite/ld-sparc/tlssunnopic32.rd: Update.
	* testsuite/ld-sparc/tlssunnopic64.rd: Update.
	* testsuite/ld-sparc/tlssunpic32.rd: Update.
	* testsuite/ld-sparc/tlssunpic64.rd: Update.
2019-07-13 09:57:50 +09:30
Alan Modra a0f6fd217f LM32 linker segmentation faults
PR 24596
	* elf32-lm32.c (lm32_elf_finish_dynamic_sections): Don't segfault
	on NULL output_section.
	* elflink.c (elf_final_link_free): Don't free -1 symshndxbuf.
2019-05-28 14:38:31 +09:30