Commit Graph

855 Commits

Author SHA1 Message Date
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
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
Nick Alcock 6d5944fca6 libctf, bfd: fix ctf_bfdopen_ctfsect opening symbol and string sections
The code in ctf_bfdopen_ctfsect (which is the ultimate place where you
end up if you use ctf_open to open a CTF file and pull in the ELF string
and symbol tables) was written before it was possible to actually test
it, since the linker was not written.  Now it is, it turns out that the
previous code was completely nonfunctional: it assumed that you could
load the symbol table via bfd_section_from_elf_index (...,elf_onesymtab())
and the string table via bfd_section_from_elf_index on the sh_link.

Unfortunately BFD loads neither of these sections in the conventional
fashion it uses for most others: the symbol table is immediately
converted into internal form (which is useless for our purposes, since
we also have to work in the absence of BFD for readelf, etc) and the
string table is loaded specially via bfd_elf_get_str_section which is
private to bfd/elf.c.

So make this function public, export it in elf-bfd.h, and use it from
libctf, which does something similar to what bfd_elf_sym_name and
bfd_elf_string_from_elf_section do.  Similarly, load the symbol table
manually using bfd_elf_get_elf_syms and throw away the internal form
it generates for us (we never use it).

BFD allocates the strtab for us via bfd_alloc, so we can leave BFD to
deallocate it: we allocate the symbol table ourselves before calling
bfd_elf_get_elf_syms, so we still have to free it.

Also change the rules around what you are allowed to provide: It is
useful to provide a string section but no symbol table, because CTF
sections can legitimately have no function info or data object sections
while relying on the ELF strtab for some of their strings.  So allow
that combination.

v4: adjust to upstream changes.  ctf_bfdopen_ctfsect's first parameter
    is potentially unused again (if BFD is not in use for this link
    due to not supporting an ELF target).
v5: fix tabdamage.

bfd/
	* elf-bfd.h (bfd_elf_get_str_section): Add.
	* elf.c (bfd_elf_get_str_section): No longer static.

libctf/
	* ctf-open-bfd.c: Add <assert.h>.
	(ctf_bfdopen_ctfsect): Open string and symbol tables using
	techniques borrowed from bfd_elf_sym_name.
	(ctf_new_archive_internal): Improve comment.
	* ctf-archive.c (ctf_arc_close): Do not free the ctfi_strsect.
	* ctf-open.c (ctf_bufopen): Allow opening with a string section but
	no symbol section, but not vice versa.
2019-10-03 17:04:55 +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 e6f7f6d14f bfd_asymbol_* macros
The main point of this patch is to give bfd_get_section a better name,
bfd_asymbol_section.

bfd/
	* bfd-in.h (bfd_asymbol_section): Rename from bfd_get_section.
	(bfd_get_output_section): Delete.
	(bfd_asymbol_base): Delete.
	(bfd_asymbol_section, bfd_asymbol_value, bfd_asymbol_name),
	(bfd_asymbol_bfd, bfd_asymbol_flavour): Tidy.
	(bfd_set_asymbol_name): New macro.
	* bfd-in2.h: Regenerate.
	* aout-cris.c: Update throughout to use bfd_asymbol_section.
	* aoutx.h: Likewise.
	* cofflink.c: Likewise.
	* dwarf2.c: Likewise.
	* ecoff.c: Likewise.
	* elf.c: Likewise.
	* elf32-arm.c: Likewise.
	* elf32-mips.c: Likewise.
	* elf32-score.c: Likewise.
	* elf32-score7.c: Likewise.
	* elfn32-mips.c: Likewise.
	* elfnn-aarch64.c: Likewise.
	* elfxx-mips.c: Likewise.
	* linker.c: Likewise.
	* pdp11.c: Likewise.
	* elf64-mmix.c (mmix_elf_reloc): Expand bfd_get_output_section.
binutils/
	* nm.c: Update bfd_get_section to bfd_asymbol_section throughout.
	* objcopy.c: Likewise.
	* objdump.c: Likewise.
	* rdcoff.c: Likewise.
	* objcopy.c (create_new_symbol): Use bfd_set_asymbol_name.
	(filter_symbols): Likewise.
gas/
	* symbols.c (S_IS_LOCAL): Update bfd_get_section to
	bfd_asymbol_section.
gdb/
	* arm-tdep.c (arm_record_special_symbol): Update bfd_get_section
	to bfd_asymbol_section.
ld/
	* ldcref.c (check_reloc_refs): Update bfd_get_section to
	bfd_asymbol_section.
2019-09-18 23:07:31 +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
Alan Modra cb7f4b298e Tidy ld/ldmisc.c
The idea here is to not use elf-bfd.h and coff-bfd.h in generic linker
code.

bfd/
	* targets.c (struct bfd_target): Add _bfd_group_name.
	(BFD_JUMP_TABLE): Likewise.
	* coffgen.c (bfd_coff_group_name): New function.
	* elf.c (bfd_elf_group_name): New function.
	* linker.c (_bfd_nolink_bfd_group_name): New function.
	* section.c (bfd_generic_group_name): New function.
	* elf-bfd.h (bfd_elf_group_name): Declare.
	* libbfd-in.h (_bfd_nolink_bfd_group_name): Declare.
	* libcoff-in.h (bfd_coff_group_name): Declare.
	* aout-target.h (MY_bfd_group_name): Define.
	* aout-tic30.c (MY_bfd_group_name): Define.
	* bfd.c (bfd_group_name): Define.
	* binary.c (binary_bfd_group_name): Define.
	* coff-alpha.c (_bfd_ecoff_bfd_group_name): Define.
	* coff-mips.c (_bfd_ecoff_bfd_group_name): Define.
	* coff-rs6000.c (_bfd_xcoff_bfd_group_name): Define.
	* coffcode.h (coff_bfd_group_name): Define.
	* elfxx-target.h (bfd_elfNN_bfd_group_name): Define.
	* i386msdos.c (msdos_bfd_group_name): Define.
	* ihex.c (ihex_bfd_group_name): Define.
	* mach-o-target.c (bfd_mach_o_bfd_group_name): Define.
	* mmo.c (mmo_bfd_group_name): Define.
	* pef.c (bfd_pef_bfd_group_name): Define.
	* plugin.c (bfd_plugin_bfd_group_name): Define.
	* ppcboot.c (ppcboot_bfd_group_name): Define.
	* som.c (som_bfd_group_name): Define.
	* srec.c (srec_bfd_group_name): Define.
	* tekhex.c (tekhex_bfd_group_name): Define.
	* verilog.c (verilog_bfd_group_name): Define.
	* vms-alpha.c (vms_bfd_group_name, alpha_vms_bfd_group_name): Define.
	* xsym.c (bfd_sym_bfd_group_name): Define.
	* coff64-rs6000.c (rs6000_xcoff64_vec): Init new field.
	(rs6000_xcoff64_aix_vec): Likewise.
	* bfd-in2.h: Regenerate.
	* libbfd.h: Regenerate.
	* libcoff.h: Regenerate.
ld/
	* ldmisc.c: Don't #include elf-bfd.h or coff-bfd.h.
	(vfinfo): Use bfd_group_name.
2019-09-11 13:43:29 +09:30
Nick Clifton a7ba389645 Stop the BFD library from failing when encountering a second set of relocs for the same section.
PR 24456
	* elf.c (bfd_section_from_shdr): Issue an informative warning
	message and continue processing other sections after encountering
	a reloc section for a section which already has other relocs
	associated with it.
2019-08-23 13:22:02 +01:00
Alan Modra 9defd221fe Remove _bfd_dwarf2_find_nearest_line addr_size parameter
This parameter might appear to be used to set up offset_size, but
since git commit 024b2372f5 offset_size is either set from the
debug_info data or is set to 4.

	* dwarf2.c (_bfd_dwarf2_find_nearest_line): Remove addr_size parameter.
	* libbfd-in.h  (_bfd_dwarf2_find_nearest_line): Update prototype.
	* coffgen.c (coff_find_nearest_line_with_names): Adjust
	_bfd_dwarf2_find_nearest_line calls.
	* elf.c (_bfd_elf_find_nearest_line, _bfd_elf_find_line): Likewise.
	* elf32-arm.c (elf32_arm_find_nearest_line): Likewise.
	* elf64-alpha.c (elf64_alpha_find_nearest_line): Likewise.
	* elfnn-aarch64.c (elfNN_aarch64_find_nearest_line): Likewise.
	* elfxx-mips.c (_bfd_mips_elf_find_nearest_line): Likewise.
	* mach-o.c (bfd_mach_o_find_nearest_line): Likewise.
	* libbfd.h: Regenerate.
2019-08-14 11:23:18 +09:30
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 cc364be6ff Complain about mbind, ifunc, and unique in final_write
It's not as good as complaining in gas/config/obj-elf.c since you lose
any reference to the source file.

bfd/
	* elf-bfd.h (struct elf_backend_data): Return bfd_boolean from
	elf_backend_final_write_processing, don't pass linker arg.
	(_bfd_elf_final_write_processing): Update prototype.
	* elf.c (_bfd_elf_write_object_contents): Adjust call.
	(_bfd_elf_final_write_processing): Return error on incompatible
	OSABI and has_gnu_osabi.  Remove linker arg.
	* elf-nacl.h (nacl_final_write_processing): Update prototype.
	* elf-vxworks.h (elf_vxworks_final_write_processing): Likewise.
	* elfxx-mips.h (_bfd_mips_final_write_processing): Likewise.
	(_bfd_mips_elf_final_write_processing): Likewise.
	* elf-hppa.h (elf_hppa_final_write_processing): Return status
	and remove linker arg.
	* 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-arm.c (arm_final_write_processing): Likewise.
	(elf32_arm_final_write_processing): Likewise.
	(elf32_arm_nacl_final_write_processing): Likewise.
	(elf32_arm_vxworks_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-mips.c (mips_vxworks_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-ppc.c (ppc_final_write_processing): Likewise.
	(ppc_elf_final_write_processing): Likewise.
	(ppc_elf_vxworks_final_write_processing): Likewise.
	* elf32-sparc.c (sparc_final_write_processing): Likewise.
	(elf32_sparc_final_write_processing): Likewise.
	(elf32_sparc_vxworks_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.
	* elfxx-mips.c (_bfd_mips_final_write_processing): Likewise.
	(_bfd_mips_elf_final_write_processing): Likewise.
gas/
	* config/obj-elf.c (obj_elf_section, obj_elf_type): Set has_gnu_osabi.
	* testsuite/gas/elf/section12a.d: Update xfails.
	* testsuite/gas/elf/section12b.d: Likewise.
2019-07-24 16:12:02 +09:30
Alan Modra df3a023bd6 SHF_GNU_MBIND requires ELFOSABI_GNU
When SHF_GNU_MBIND was added in the SHF_LOOS to SHF_HIOS range, it
should have required ELFOSABI_GNU since these flags are already in use
by other OSes.  HPUX SHF_HP_TLS in fact has the same value.  That
means no place in binutils should test SHF_GNU_MBIND without first
checking OSABI, and SHF_GNU_MBIND should not be set without also
setting OSABI.  At least, that's the ideal, but the patch accepts
SHF_GNU_MBIND on ELFOSABI_NONE object files since gas didn't always
set OSABI.  However, to reinforce the fact that SHF_GNU_MBIND isn't
proper without a non-zero OSABI, readelf will display the flag as
LOOS+0 if OSABI isn't set.

The clash with SHF_HP_TLS means that hppa64-linux either has that flag
on .tbss sections or supports GNU_MBIND, not both.  (hppa64-linux
users, if there are any, may have noticed that GNU ld since 2017
mysteriously aligned their .tbss sections to a 4k boundary.  That was
one consequence of SHF_HP_TLS being blindly interpreted as
SHF_GNU_MBIND.)  Since it seems that binutils, gdb, gcc, glibc, and
the linux kernel don't care about SHF_HP_TLS I took that flag out of
.tbss for hppa64-linux.

bfd/
	* elf-bfd.h (enum elf_gnu_osabi): Add elf_gnu_osabi_mbind.
	* elf.c (_bfd_elf_make_section_from_shdr): Set elf_gnu_osabi_mbind.
	(get_program_header_size): Formatting.  Only test SH_GNU_MBIND
	when elf_gnu_osabi_mbind is set.
	(_bfd_elf_map_sections_to_segments): Likewise.
	(_bfd_elf_init_private_section_data): Likewise.
	(_bfd_elf_final_write_processing): Update comment.
	* elf64-hppa.c (elf64_hppa_special_sections): Move .tbss entry.
	(elf_backend_special_sections): Define without .tbss for linux.
binutils/
	* readelf.c (get_parisc_segment_type): Split off hpux entries..
	(get_ia64_segment_type): ..and these..
	(get_hpux_segment_type): ..to here.
	(get_segment_type): Condition GNU_MBIND on osabi.  Use
	get_hpux_segment_type.
	(get_symbol_binding): Do not print UNIQUE for ELFOSABI_NONE.
	(get_symbol_type): Do not print IFUNC for ELFOSABI_NONE.
gas/
	* config/obj-elf.c (obj_elf_change_section): Don't emit a fatal
	error for non-SHF_ALLOC SHF_GNU_MBIND here.
	(obj_elf_parse_section_letters): Return SHF_GNU_MBIND in new
	gnu_attr param.
	(obj_elf_section): Adjust obj_elf_parse_section_letters call.
	Formatting.  Set SHF_GNU_MBIND and elf_osabi from gnu_attr.
	Emit normal error for non-SHF_ALLOC SHF_GNU_MBIND and wrong osabi.
	(obj_elf_type): Set elf_osabi for ifunc.
	* testsuite/gas/elf/section12a.d: xfail msp430 and hpux.
	* testsuite/gas/elf/section12b.d: Likewise.
	* testsuite/gas/elf/section13.d: Likewise.
	* testsuite/gas/elf/section13.l: Adjust expected error.
ld/
	* emultempl/elf32.em (gld${EMULATION_NAME}_place_orphan): Condition
	SHF_GNU_MBIND on osabi.  Set output elf_gnu_osabi_mbind.
2019-07-23 18:22:02 +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
Nick Clifton 1faa385ff6 Stop the BFD library from issuing a warning message when processing allocated sections in debuginfo files that lie outside of any loadable segment.
PR 24717
	* elf.c (is_debuginfo_file): New function.
	(assign_file_positions_for_non_load_sections): Do not warn about
	allocated sections outside of loadable segments if they are found
	in a debuginfo file.
	* elf-bfd.h (is_debuginfo_file): Prototype.
2019-07-02 15:58:29 +01:00
Nick Clifton 7e56c51c79 Prevent attempts to allocate excessive amounts of memory when parsing corrupt ELF files.
PR 24708
	* elf.c (_bfd_elf_slurp_version_tables): Check for an excessively
	large version reference section.
	* compress.c (bfd_get_full_section_contents): Check for an
	uncompressed section whose size is larger than the file size.
2019-06-28 15:30:43 +01:00
Christos Zoulas 58e07198f3 Add support for NetBSD/sh3 core file sections. Merge multiple copies of auxv section creation into one function.
PR 24650
	* elf.c (elfcore_make_auxv_note_section): New function.
	(elfcore_grok_note): Use it.
	(elfcore_grok_freebsd_note): Likewise.
	(elfcore_grok_openbsd_note): Likewise.
	(elfcore_grok_netbsd_note): Likewise.  Plus add support for
	NT_NETBSDCORE_AUXV notes.
2019-06-10 14:41:35 +01:00
Alan Modra ce5aecf873 Another generic ELF target assertion failure
After fixing the ld-elf/pr22836-1a segmentation fault we run into an
assertion failure due to the generic ELF target not removing empty
SHT_GROUP sections.  Avoid that.

	* elf.c (bfd_elf_set_group_contents): Exit on zero size section.
2019-05-27 13:58:27 +09:30
Alan Modra 4b24dd1aca BFD whitespace fixes
* coff-i386.c, * coff-x86_64.c, * coffgen.c, * dwarf2.c,
	* elf-m10200.c, * elf.c, * elf32-arm.c, * elf32-bfin.c,
	* elf32-csky.c, * elf32-m32r.c, * elf32-microblaze.c, * elf32-pru.c,
	* elf32-rx.c, * elf32-xgate.c, * elf64-sparc.c, * elfnn-ia64.c,
	* elfxx-riscv.c, * elfxx-sparc.c, * linker.c, * mach-o.c,
	* pe-x86_64.c, * pei-x86_64.c, * plugin.c: Whitespace fixes.
2019-04-03 12:50:39 +10:30
Alan Modra 242a115951 PR24392, Clang warning Wtautological-constant-out-of-range-compare
PR 24392
	* configure.ac: Invoke AC_CHECK_SIZEOF(int).
	* configure: Regenerate.
	* coffgen.c (coff_get_reloc_upper_bound): Replace gcc diagnostic
	workaround with SIZEOF_LONG vs. SIZEOF_INT check.
	* elf.c (_bfd_elf_get_reloc_upper_bound): Likewise.
	* elf64-sparc.c (elf64_sparc_get_reloc_upper_bound): Likewise.
	* mach-o.c (bfd_mach_o_get_reloc_upper_bound): Likewise.
2019-03-28 17:10:31 +10:30
Alan Modra 6835821be8 PR24337, segfault in _bfd_elf_rela_local_sym, again
Reverts commit 1ff31e135f, fixing the problem more generally.  There
are likely other place that will segfault on a NULL section.

	PR 24337
	* elf.c (_bfd_elf_rela_local_sym): Revert last change.
	(_bfd_elf_rel_local_sym): Likewise.
	* elflink.c (elf_link_input_bfd): Use bfd_und_section for
	section of symbols with unrecognized shndx.
2019-03-16 11:55:28 +10:30
Alan Modra 1ff31e135f PR24337, segfault in _bfd_elf_rela_local_sym
PR 24337
	* elf.c (_bfd_elf_rela_local_sym): Don't segfault on NULL sec.
	(_bfd_elf_rel_local_sym): Likewise.
2019-03-15 20:05:18 +10:30
Alan Modra 7a6e0d89bb Don't use bfd_get_file_size in objdump
Compressed debug sections can have uncompressed sizes that exceed the
original file size, so we can't use bfd_get_file_size.  objdump also
used bfd_get_file_size to limit reloc section size, but I believe the
underlying bug causing the PR22508 out of bounds buffer access was
that we had an integer overflow when calculating the reloc buffer
size.  I've fixed that instead in most of the backends, som and
vms-alpha being the exceptions.  SOM and vmd-alpha have rather more
serious bugs in their slurp_relocs routines that would need fixing
first if we want to fuss about making them safe against fuzzed object
files.

The patch also fixes a number of other potential overflows by using
the bfd_alloc2/malloc2/zalloc2 memory allocation functions.

bfd/
	* coffcode.h (buy_and_read): Delete unnecessary forward decl.  Add
	nmemb parameter.  Use bfd_alloc2.
	(coff_slurp_line_table): Use bfd_alloc2.  Update buy_and_read calls.
	Delete assertion.
	(coff_slurp_symbol_table): Use bfd_alloc2 and bfd_zalloc2.
	(coff_slurp_reloc_table): Use bfd_alloc2.  Update buy_and_read calls.
	* coffgen.c (coff_get_reloc_upper_bound): Ensure size calculation
	doesn't overflow.
	* elf.c (bfd_section_from_shdr): Use bfd_zalloc2.  Style fix.
	(assign_section_numbers): Style fix.
	(swap_out_syms): Use bfd_malloc2.
	(_bfd_elf_get_reloc_upper_bound): Ensure size calculation doesn't
	overflow.
	(_bfd_elf_make_empty_symbol): Style fix.
	(elfobj_grok_stapsdt_note_1): Formatting.
	* elfcode.h (elf_object_p): Use bfd_alloc2.
	(elf_write_relocs, elf_write_shdrs_and_ehdr): Likewise.
	(elf_slurp_symbol_table): Use bfd_zalloc2.
	(elf_slurp_reloc_table): Use bfd_alloc2.
	(_bfd_elf_bfd_from_remote_memory): Use bfd_malloc2.
	* elf64-sparc (elf64_sparc_get_reloc_upper_bound): Ensure
	size calculation doesn't overflow.
	(elf64_sparc_get_dynamic_reloc_upper_bound): Likewise.
	* mach-o.c (bfd_mach_o_get_reloc_upper_bound): Likewise.
	* pdp11.c (get_reloc_upper_bound): Copy aoutx.h version.
binutils/
	* objdump.c (load_specific_debug_section): Don't compare section
	size against file size.
	(dump_relocs_in_section): Don't compare reloc size against file size.
	Print "failed to read relocs" on bfd_get_reloc_upper_bound error.
2019-03-12 23:54:09 +10:30
Nick Clifton eed5def8d0 Prevent a buffer overrun error when attempting to parse a corrupt ELF file.
PR 24273
	* elf.c (bfd_elf_string_from_elf_section): Check for a string
	section that is not NUL terminated.
2019-02-28 14:30:20 +00:00
Alan Hayward e6c3b5bfb4 AArch64: Add pauth core file section
Used for the AArch64 pointer authentication code mask registers in Arm v8.3-a.

NT_ARM_PAC_MASK matches the value in Linux include/uapi/linux/elf.h

include/ChangeLog:

	* elf/common.h (NT_ARM_PAC_MASK): Add define.

bfd/ChangeLog:

	* elf-bfd.h (elfcore_write_aarch_pauth): Add declaration.
	* elf.c (elfcore_grok_aarch_pauth): New function.
	(elfcore_grok_note): Check for NT_ARM_PAC_MASK.
	(elfcore_write_aarch_pauth): New function.
	(elfcore_write_register_note): Check for AArch64 pauth section.
2019-02-20 10:39:28 +00:00
Alan Modra 827041555a Update year range in copyright notice of binutils files 2019-01-01 22:06:53 +10:30
Alan Modra 0e41bebb93 PR24015, glibc-2.28 on little-endian mips32 broken
Commit 2bf2bf23da exposed a bug on targets that create common sections
other than the standard ELF SHN_COMMON.  If these are output by ld -r,
then their type becomes SHT_PROGBITS unless the target handles them
specially (eg. by elf_backend_special_sections), and if they are
merged into .bss/.sbss by ld -r then that section becomes SHT_PROGBITS.

Worse, if they are output by ld -r, then their size is increased by
bfd_generic_define_common_symbol during final link, which leads to
bogus file contents being copied to output.

For mips, it seems to me that the .scommon section should not be
output for ld -r, but I haven't made that change in this patch.

	PR 24015
	* elf.c (bfd_elf_get_default_section_type): Make common sections
	SHT_NOBITS.
	* linker.c (bfd_generic_define_common_symbol): Clear
	SEC_HAS_CONTENTS.
2018-12-28 15:02:08 +10:30
Alan Modra 3a551c7a1b PR23994, libbfd integer overflow
PR 23994
	* aoutx.h: Include limits.h.
	(get_reloc_upper_bound): Detect long overflow and return a file
	too big error if it occurs.
	* elf.c: Include limits.h.
	(_bfd_elf_get_symtab_upper_bound): Detect long overflow and return
	a file too big error if it occurs.
	(_bfd_elf_get_dynamic_symtab_upper_bound): Likewise.
	(_bfd_elf_get_dynamic_reloc_upper_bound): Likewise.
2018-12-17 12:49:38 +10:30
H.J. Lu 0a59decbb8 elf: Add PT_GNU_PROPERTY segment type
Linkers group input note sections with the same name into one output
note section with the same name.  One output note section is placed in
one PT_NOTE segment.  New linkers merge all input .note.gnu.property
sections into one output .note.gnu.property section with a single
NT_GNU_PROPERTY_TYPE_0 note in a single PT_NOTE segment.  Since older
linkers treat input .note.gnu.property section as a generic note section
and just concatenate all input .note.gnu.property sections into one
output .note.gnu.property section without merging them, we may
see one or more NT_GNU_PROPERTY_TYPE_0 notes in PT_NOTE segment, which
are invalid.

GNU_PROPERTY_X86_UINT32_VALID was defined to address this issue such
that linker sets the bit for non-relocatable outputs.  But it isn't
sufficient:

1. It doesn't cover generic properties.
2. When -mx86-used-note=yes is passed to x86 assembler, the
GNU_PROPERTY_X86_UINT32_VALID bit is set in GNU_PROPERTY_X86_ISA_1_USED
property in object file and older linkers generate invalid
NT_GNU_PROPERTY_TYPE_0 notes with the GNU_PROPERTY_X86_UINT32_VALID bit
set.

I am proposing the following changes:

1. Add PT_GNU_PROPERTY segment type:

 # define PT_GNU_PROPERTY (PT_LOOS + 0x474e553)

which covers .note.gnu.property section.
2. Remove GNU_PROPERTY_X86_UINT32_VALID.

bfd/

	PR ld/23900
	* elf.c (get_program_header_size): Add a PT_GNU_PROPERTY
	segment for NOTE_GNU_PROPERTY_SECTION_NAME.
	(_bfd_elf_map_sections_to_segments): Create a PT_GNU_PROPERTY
	segment for NOTE_GNU_PROPERTY_SECTION_NAME.
	* elfxx-x86.c (_bfd_elf_link_setup_gnu_properties): Don't set
	GNU_PROPERTY_X86_UINT32_VALID.

binutils/

	PR ld/23900
	* readelf.c (get_segment_type): Support PT_GNU_PROPERTY.
	(decode_x86_isa): Don't check GNU_PROPERTY_X86_UINT32_VALID.
	(decode_x86_feature_1): Likewise.
	(decode_x86_feature_2): Likewise.
	(print_gnu_property_note): Remove GNU_PROPERTY_X86_UINT32_VALID
	check.
	* testsuite/binutils-all/i386/empty.d: Updated.
	* testsuite/binutils-all/x86-64/empty-x32.d: Likewise.
	* testsuite/binutils-all/x86-64/empty.d: Likewise.
	* testsuite/binutils-all/i386/pr21231b.s: Change
	GNU_PROPERTY_X86_ISA_1_USED bits to 0x7fffffff.
	* testsuite/binutils-all/x86-64/pr21231b.s: Likewise.

gas/

	PR ld/23900
	* config/tc-i386.c (x86_cleanup): Don't set
	GNU_PROPERTY_X86_UINT32_VALID.
	* testsuite/gas/i386/property-1.s: Change
	GNU_PROPERTY_X86_ISA_1_USED bits to 0.

include/

	PR ld/23900
	* elf/common.h (PT_GNU_PROPERTY): New.
	(GNU_PROPERTY_X86_UINT32_VALID): Removed.

ld/

	PR ld/23900
	* testsuite/ld-elf/elf.exp: Run PR ld/23900 test.
	* testsuite/ld-elf/pr23900-1-32.rd: New file.
	* testsuite/ld-elf/pr23900-1-64.rd: Likewise.
	* testsuite/ld-elf/pr23900-1.d: Likewise.
	* testsuite/ld-elf/pr23900-1.s: Likewise.
	* testsuite/ld-elf/pr23900-2.s: Likewise.
	* testsuite/ld-elf/pr23900-2a.d: Likewise.
	* testsuite/ld-elf/pr23900-2b.d: Likewise.
	* testsuite/ld-i386/ibt-plt-1.d: Adjusted.
	* testsuite/ld-i386/ibt-plt-2c.d: Likewise.
	* testsuite/ld-i386/ibt-plt-2d.d: Likewise.
	* testsuite/ld-i386/ibt-plt-3d.d: Likewise.
	* testsuite/ld-x86-64/ibt-plt-1-x32.d: Likewise.
	* testsuite/ld-x86-64/ibt-plt-1.d: Likewise.
	* testsuite/ld-x86-64/ibt-plt-2c-x32.d: Likewise.
	* testsuite/ld-x86-64/ibt-plt-2c.d: Likewise.
	* testsuite/ld-x86-64/ibt-plt-2d-x32.d: Likewise.
	* testsuite/ld-x86-64/ibt-plt-2c.d: Likewise.
	* testsuite/ld-x86-64/ibt-plt-3c-x32.d: Likewise.
	* testsuite/ld-x86-64/ibt-plt-3c.d: Likewise.
	* testsuite/ld-x86-64/ibt-plt-3d-x32.d: Likewise.
	* testsuite/ld-x86-64/ibt-plt-3d.d: Likewise.
	* testsuite/ld-i386/pr23372c.d: Expect <None>
	for GNU_PROPERTY_X86_ISA_1_USED.
	* testsuite/ld-x86-64/pr23372c-x32.d: Likewise.
	* testsuite/ld-x86-64/pr23372c.d: Likewise.
	* testsuite/ld-x86-64/pr23372d-x32.d: Likewise.
	* testsuite/ld-x86-64/pr23372d.d: Likewise.
	* testsuite/ld-x86-64/property-x86-5a.s: Change
	GNU_PROPERTY_X86_ISA_1_USED bits to 0.
	* testsuite/ld-x86-64/property-x86-5b.s: Likewise.
2018-12-14 04:55:34 -08:00
Nick Clifton beab453223 Remove an abort in the bfd library and add a check for an integer overflow when mapping sections to segments.
PR 23932
	* elf.c (IS_CONTAINED_BY_LMA): Add a check for a negative section
	size.
	(rewrite_elf_program_header): If no sections are mapped into a
	segment return an error.
2018-11-30 11:43:12 +00:00
Mark Wielaard 4207142d6a Handle ELF compressed header alignment correctly by setting up the section alignment correctly for the Elf32_Chdr or Elf64_Chdr type and respect the ch_addralign field when decompressing the section data.
PR binutils/23919
binutils* readelf.c (dump_sections_as_strings): Remove bogus addralign check.
	(dump_sections_as_bytes): Likewise.
	(load_specific_debug_sections): Likewise.
	* testsuite/binutils-all/dw2-3.rS: Adjust alignment.
	* testsuite/binutils-all/dw2-3.rt: Likewise.

bfd	* bfd.c (bfd_update_compression_header): Explicitly set alignment.
	(bfd_check_compression_header): Add uncompressed_alignment_power
	argument. Check ch_addralign is a power of 2.
	* bfd-in2.h: Regenerated.
	* compress.c (bfd_compress_section_contents): Get and set
	orig_uncompressed_alignment_pow if section is decompressed.
	(bfd_is_section_compressed_with_header): Add and get
	uncompressed_align_pow_p argument.
	(bfd_is_section_compressed): Add uncompressed_align_power argument
	to bfd_is_section_compressed_with_header call.
	(bfd_init_section_decompress_status): Get and set
	uncompressed_alignment_power.
	* elf.c (_bfd_elf_make_section_from_shdr): Add
	uncompressed_align_power argument to
	bfd_is_section_compressed_with_header call.
2018-11-27 11:59:10 +00:00
Alan Modra a4bcd73371 PR23850, strip should not discard/move .rela.plt in executable
strip/objcopy can't deal with alloc reloc sections, not .rela.dyn or
.rela.plt in a dynamic executable, or .rela.plt/.rela.iplt in a static
executable.  So, don't have BFD treat them as side-channel data
associated with the section they are relocating.

	PR 23850
	* elf.c (bfd_section_from_shdr): Treat SHF_ALLOC SHT_REL* sections
	in an executable or shared library as normal sections.
2018-11-02 13:48:08 +10:30
Alan Modra ac85e67c05 PR23788, objcopy: failed to find link section
Symbol tables can change when a number of objcopy options are used.
I figure string tables are similarly changeable.

	PR 23788
	* elf.c (section_match): Don't require a size match for SHT_SYMTAB
	or SHT_STRTAB.
2018-10-20 19:46:43 +10:30
Alan Modra 64029e9368 Separate header PT_LOAD for -z separate-code
This patch, along with previous patches in the series, supports
putting the ELF file header and program headers in a PT_LOAD without
sections.

Logic governing whether headers a loaded has changed a little:  The
primary reason to include headers is now the presence of
SIZEOF_HEADERS in a linker script.  However, to support scripts that
may have reserved space for headers by hand, we continue to add
headers whenever the first section address is past the end of headers
modulo page size.

include/
	* bfdlink.h (struct bfd_link_info): Add load_phdrs field.
bfd/
	* elf-nacl.c (nacl_modify_segment_map): Cope with header PT_LOAD
	lacking sections.
	* elf.c (_bfd_elf_map_sections_to_segments): Assume file and
	program headers are required when info->load_phdrs.  Reorganize
	code handling program headers.  Generate a mapping without
	sections just for file and program headers when -z separate-code
	would indicate they should be on a different page to the first
	section.
ld/
	* ldexp.c (fold_name <SIZEOF_HEADERS>): Set link_info.load_phdrs.
	* testsuite/ld-elf/loadaddr1.d: Pass -z noseparate-code.
	* testsuite/ld-elf/loadaddr2.d: Likewise.
	* testsuite/ld-i386/vxworks2.sd: Adjust expected output.
	* testsuite/ld-powerpc/vxworks2.sd: Likewise.
	* testsuite/ld-elf/overlay.d: Remove spu xfail.
	* testsuite/ld-spu/ovl.lnk: Don't use SIZEOF_HEADERS.
	* testsuite/ld-tic6x/dsbt-be.ld: Likewise.
	* testsuite/ld-tic6x/dsbt-inrange.ld: Likewise.
	* testsuite/ld-tic6x/dsbt-overflow.ld: Likewise.
	* testsuite/ld-tic6x/dsbt.ld: Likewise.
2018-10-08 20:26:08 +10:30
Alan Modra 5d69562788 Use p_vaddr_offset to set p_vaddr on segments without sections
p_vaddr is currently set from the first section vma if a segment has
sections, and to zero if a segment has no sections.  This means we
lose p_vaddr when objcopy'ing executables if a segment without
sections has a non-zero p_vaddr.

This patch saves p_vaddr to p_vaddr_offset, and to make the use of
p_vaddr_offset consistent, inverts the sign.  (It's now added to
section vma to get segment vaddr, and added to zero when there are no
sections.)

	* elf.c (assign_file_positions_for_load_sections): Set p_vaddr
	from m->p_vaddr_offset for segments without sections.  Invert
	sign of p_vaddr_offset.
	(rewrite_elf_program_header, copy_elf_program_header): Save
	old segment p_vaddr to p_vaddr_offset.  Invert sign of
	p_vaddr_offset.
2018-10-08 20:26:08 +10:30
Alan Modra 1b9e270b09 No PT_INTERP when .interp is zero size
Some targets don't set a default interpreter, resulting in an empty
.interp section unless --dynamic-linker is passed to ld.  A PT_INTERP
without a path is rather useless.

The testsuite change fixes a failure on microblaze-linux.

bfd/
	* elf.c (get_program_header_size): Don't count PT_INTERP if
	.interp is empty.
	(_bfd_elf_map_sections_to_segments): Don't create PT_INTERP if
	.interp is empty.
ld/
	* testsuite/ld-elf/pr22423.d: Pass --dynamic-linker to ld.
2018-10-08 20:26:08 +10:30
Alan Modra 00bee008e9 Tidy elf_segment_map allocation
This cleans up elf_segment_map allocation when the section array is
empty.  "amt += (to - from - 1) * sizeof (asection *)", when "to" and
"from" are unsigned int results in an unsigned value inside the
parentheses.  When "to" and "from" are equal on a 64-bit host,
0xffffffff * 8 is added to "amt", not -8 as desired.

The patch also renames a variable for consistency with other functions
using a similar index.

	* elf.c (make_mapping): Cope with zero size array at end of
	struct elf_segment_map.
	(_bfd_elf_map_sections_to_segments): Likewise.
	(rewrite_elf_program_header, copy_elf_program_header): Likewise.
	(_bfd_elf_map_sections_to_segments): Rename phdr_index to hdr_index.
2018-10-08 20:26:08 +10:30
H.J. Lu 23e463ed7c ELF: Group PT_NOTE segments by section alignments
Alignments of SHT_NOTE sections can be 8 bytes for 64-bit ELF files.  We
should put all adjacent SHT_NOTE sections with the same section alignment
into a single PT_NOTE segment even when the section alignment != 4 bytes.
Also check SHT_NOTE section type instead of section name.

	PR ld/23658
	* elf.c (get_program_header_size): Put all adjacent SHT_NOTE
	sections with the same section alignment into a single PT_NOTE
	segment.  Check SHT_NOTE section type instead of section name.
	(_bfd_elf_map_sections_to_segments): Likewise.
2018-10-03 13:22:40 -07:00
Jozef Lawrynowicz db72737006 Fix PR gdb/20948: --write option to GDB causes segmentation fault
When opening a BFD for update, as gdb --write does, modifications to
anything but the contents of sections is restricted.

Do not try to write back any ELF headers in this case.

bfd/ChangeLog
2018-09-24  Jozef Lawrynowicz  <jozef.l@mittosystems.com>

	PR gdb/20948
	* elf.c (_bfd_elf_write_object_contents): Return from function
	early if abfd->direction == both_direction.

gdb/testsuite/ChangeLog
2018-09-24  Jozef Lawrynowicz  <jozef.l@mittosystems.com>

	PR gdb/20948
	* gdb.base/write_mem.exp: New test.
	* gdb.base/write_mem.c: Likewise.
2018-09-24 06:20:17 -06:00
Jozef Lawrynowicz 2542e49e21 PR23595, simple objcopy of executable failure for msp430-elf
VMA of the first section in the segment containing the ELF file header
(and possibly section headers too) can't be used to reliably find the
size of the headers plus padding.  What's really needed is sh_offset
of the first section assuming it has contents (vma does have a
relationship to sh_offset, but is only guaranteed in demand paged
executables).

If the first section is SHT_NOBITS and it hasn't been converted to
have file contents by the existence of a following SHT_PROGBITS
section in the same segment, the sh_offset value also isn't reliable.

	PR 23595
	elf.c (copy_elf_program_header): When first segment contains
	only the headers and SHT_NOBITS sections, use segment p_filesz
	to calculate header and padding size.  Use filepos of the first
	section otherwise.
2018-09-03 15:50:54 +09:30
H.J. Lu 7a815dd566 elf: Check for corrupt symbol version info
The BFD linker with PR ld/23499 may generate shared libraries with
corrupt symbol version info which leads to linker error when the
corrupt shared library is used:

/usr/bin/ld: bin/libKF5Service.so.5.49.0: _edata: invalid version 21 (max 0)
/usr/bin/ld: bin/libKF5Service.so.5.49.0: error adding symbols: bad value

Add check for corrupt symbol version info to objdump:

00000000000af005 g    D  .data	0000000000000000  <corrupt>   _edata

and readelf:

   728: 00000000000af005     0 NOTYPE  GLOBAL DEFAULT   25 _edata@<corrupt> (5)

bfd/

	PR ld/23499
	* elf.c (_bfd_elf_get_symbol_version_string): Return
	_("<corrupt>") for corrupt symbol version info.

binutils/

	PR ld/23499
	* readelf.c (get_symbol_version_string): Return _("<corrupt>")
	for corrupt symbol version info.
2018-08-25 06:18:06 -07:00
Alan Modra 67ce483baa PR23430, Indices misspelled
PR 23430
include/
	* elf/common.h (SHT_SYMTAB_SHNDX): Fix comment typo.
bfd/
	* dwarf2.c (dwarf_debug_section_enum): Fix comment typo.
	* elf.c (bfd_section_from_shdr, elf_sort_sections): Likewise.
binutils/
	* elfcomm.h (struct archive_info): Rename uses_64bit_indicies
	to uses_64bit_indices.
	* elfcomm.c (setup_archive): Update uses of above.
	* readelf.c (process_archive): Likewise.
	(get_section_type_name): Rename indicies to indices.
	(get_32bit_elf_symbols, get_64bit_elf_symbols): Likewise.
	(process_section_groups): Likewise.
cpu/
	* or1kcommon.cpu (spr-reg-indices): Fix description typo.
opcodes/
	* or1k-desc.h: Regenerate.
2018-07-24 19:58:12 +09:30
Tom Tromey 01bcaf636a Avoid ubsan complaint in BFD
I built gdb with ubsan and ran the test suite.

One complaint was due to bfd_get_elf_phdrs passing NULL to memcpy.
This patch avoids the complaint.

bfd/ChangeLog
2018-07-23  Tom Tromey  <tom@tromey.com>

	* elf.c (bfd_get_elf_phdrs): Don't call memcpy with size 0.
2018-07-23 08:14:00 -06:00
Edjunior Barbosa Machado cb2366c198 Add grok/write functions for new ppc core note sections
This patch adds functions for grokking and writing more register core
note sections (NT_PPC_TAR, NT_PPC_PPR, NT_PPC_DSCR, NT_PPC_EBB,
NT_PPC_PMU, NT_PPC_TM_CGPR, NT_PPC_TM_CFPR, NT_PPC_TM_CVMX,
NT_PPC_TM_CVSX, NT_PPC_TM_SPR, NT_PPC_TM_CTAR, NT_PPC_TM_CPPR,
NT_PPC_TM_CDSCR).

2018-07-16  Edjunior Barbosa Machado  <emachado@linux.vnet.ibm.com>

bfd/
	* elf-bfd.h (elfcore_write_ppc_tar): Add prototype.
	(elfcore_write_ppc_ppr): Likewise.
	(elfcore_write_ppc_dscr): Likewise.
	(elfcore_write_ppc_ebb): Likewise.
	(elfcore_write_ppc_pmu): Likewise.
	(elfcore_write_ppc_tm_cgpr): Likewise.
	(elfcore_write_ppc_tm_cfpr): Likewise.
	(elfcore_write_ppc_tm_cvmx): Likewise.
	(elfcore_write_ppc_tm_cvsx): Likewise.
	(elfcore_write_ppc_tm_spr): Likewise.
	(elfcore_write_ppc_tm_ctar): Likewise.
	(elfcore_write_ppc_tm_cppr): Likewise.
	(elfcore_write_ppc_tm_cdscr): Likewise.
	* elf.c (elfcore_write_ppc_tar): New function.
	(elfcore_write_ppc_ppr): Likewise.
	(elfcore_write_ppc_dscr): Likewise.
	(elfcore_write_ppc_ebb): Likewise.
	(elfcore_write_ppc_pmu): Likewise.
	(elfcore_write_ppc_tm_cgpr): Likewise.
	(elfcore_write_ppc_tm_cfpr): Likewise.
	(elfcore_write_ppc_tm_cvmx): Likewise.
	(elfcore_write_ppc_tm_cvsx): Likewise.
	(elfcore_write_ppc_tm_spr): Likewise.
	(elfcore_write_ppc_tm_ctar): Likewise.
	(elfcore_write_ppc_tm_cppr): Likewise.
	(elfcore_write_ppc_tm_cdscr): Likewise.
	(elfcore_write_register_note): Call them.
	(elfcore_grok_ppc_tar): New function.
	(elfcore_grok_ppc_ppr): Likewise.
	(elfcore_grok_ppc_dscr): Likewise.
	(elfcore_grok_ppc_ebb): Likewise.
	(elfcore_grok_ppc_pmu): Likewise.
	(elfcore_grok_ppc_tm_cgpr): Likewise.
	(elfcore_grok_ppc_tm_cfpr): Likewise.
	(elfcore_grok_ppc_tm_cvmx): Likewise.
	(elfcore_grok_ppc_tm_cvsx): Likewise.
	(elfcore_grok_ppc_tm_spr): Likewise.
	(elfcore_grok_ppc_tm_ctar): Likewise.
	(elfcore_grok_ppc_tm_cppr): Likewise.
	(elfcore_grok_ppc_tm_cdscr): Likewise.
	(elfcore_grok_note): Call them.
2018-07-16 16:01:34 -03:00
Alan Hayward ad1cc4e492 Add checks for the NT_ARM_SVE section in a core file.
The NT_ARM_SVE section is documented here:
  https://github.com/torvalds/linux/blob/master/Documentation/arm64/sve.txt
  * A NT_ARM_SVE note will be added to each coredump for each thread of the
   dumped process.  The contents will be equivalent to the data that would have
   been read if a PTRACE_GETREGSET of NT_ARM_SVE were executed for each thread
   when the coredump was generated.

	* elf.c (elfcore_grok_aarch_sve): New function.
	(elfcore_grok_note): Check for Aarch64 SVE.
	(elfcore_write_aarch_sve): New function.
	(elfcore_write_register_note): Check for Aarch64 SVE.
	* elf-bfd.h(elfcore_grok_aarch_sve): New declaration.
2018-07-06 16:25:21 +01:00
H.J. Lu 1f6f5dba57 elf: Check if the first symbol version is base version
Check VER_FLG_BASE instead of assuming that the first symbol version is
base version.

bfd/

	PR binutils/23267
	* elf.c (_bfd_elf_get_symbol_version_string): Check if the first
	symbol version is base version.

binutils/

	PR binutils/23267
	* readelf.c (get_symbol_version_string): Check if the first
	symbol version is base version.
2018-06-14 05:37:33 -07:00
H.J. Lu bae363f114 Mark section in a section group with SHF_GROUP
All sections in a section group should be marked with SHF_GROUP.  But
some tools generate broken objects without SHF_GROUP.  This patch fixes
them up for objcopy and strip.

	PR binutils/23199
	* elf.c (setup_group): Mark section in a section group with
	SHF_GROUP.
2018-05-21 19:25:33 -07:00
Alan Modra 4bba0fb1c6 PR23199, Invalid SHT_GROUP entry leads to group confusion
This patch prevents elf_next_in_group list pointer confusion when
SHT_GROUP sections specify other SHT_GROUP sections in their list of
group sections.

	PR 23199
	* elf.c (setup_group): Formatting.  Check that SHT_GROUP entries
	don't point at other SHT_GROUP sections.  Set shdr corresponding
	to invalid entry, to NULL rather than section 0.  Identify
	SHT_GROUP section index when reporting an error.  Cope with NULL
	shdr pointer.
2018-05-18 18:32:35 +09:30
Alan Modra 9933dc5273 PR20659, Objcopy and change section lma failing
Sections may well belong in multiple segments.  The testcase in the PR
saw an allocated section being assigned to an ABIFLAGS segment, then
not being assigned to a LOAD segment because it had already been
handled.  To fix that particular problem this patch sets and tests
segment_mark only for LOAD segments.  I kept the segment_mark test for
LOAD segments because I think there may otherwise be a problem with
zero size sections.

A few other problems showed up with the testcase.  Some targets align
.dynamic, resulting in the test failing with "section .dynamic lma
0x800000c0 adjusted to 0x800000cc" and similar messages.  I've tried
to handle that with some more hacks to the segment lma, which do the
right thing for the testcase, but may well fail in other situations.

I've also removed the tests of segment lma (p_paddr) and code involved
in deciding that an adjusted segment no longer covers the file or
program headers.  Those test can't be correct in the face of objcopy
--change-section-lma.  It may be necessary to reinstate the tests but
do them modulo page size, but we'll see how this goes.

	PR 20659
bfd/
	* elf.c (rewrite_elf_program_header): Use segment_mark only for
	PT_LOAD headers.  Delete first_matching_lma and first_suggested_lma.
	Instead make matching_lma and suggested_lma pointers to the
	sections.  Align section vma and lma calculated from segment.
	Don't clear includes_phdrs or includes_filehdr based on p_paddr
	test.  Try to handle alignment padding before first section by
	adjusting new segment lma down.  Adjust PT_PHDR map p_paddr.
ld/
	* testsuite/ld-elf/changelma.d,
	* testsuite/ld-elf/changelma.lnk,
	* testsuite/ld-elf/changelma.s: New test.
2018-05-12 16:03:44 +09:30
Nick Clifton 95a6d23566 Prevent a memory exhaustion failure when running objdump on a fuzzed input file with corrupt string and attribute sections.
PR 22809
	* elf.c (bfd_elf_get_str_section): Check for an excessively large
	string section.
	* elf-attrs.c (_bfd_elf_parse_attributes): Issue an error if the
	attribute section is larger than the size of the file.
2018-05-08 12:51:06 +01:00
Alan Modra 602f165704 -Wstringop-truncation warnings
This patch is aimed at silencing gcc8 -Wstringop-truncation warnings.

Unfortunately adding  __attribute__ ((__nonstring)) doesn't work in a
number of the places patched here, (see
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85643) so if you have
recent glibc headers installed you'll need to configure binutils with
--disable-werror to compile using gcc8 or gcc9.

include/
	* ansidecl.h: Import from gcc.
	* coff/internal.h (struct internal_scnhdr): Add ATTRIBUTE_NONSTRING
	to s_name.
	(struct internal_syment): Add ATTRIBUTE_NONSTRING to _n_name.
bfd/
	* elf-linux-core.h (struct elf_external_linux_prpsinfo32_ugid32),
	(struct elf_external_linux_prpsinfo32_ugid16),
	(struct elf_external_linux_prpsinfo64_ugid32),
	(struct elf_external_linux_prpsinfo64_ugid16): Add ATTRIBUTE_NONSTRING
	to pr_fname and pr_psargs fields.  Remove GCC diagnostic pragmas.
	Move comment to..
	* elf.c (elfcore_write_prpsinfo): ..here.  Indent nested preprocessor
	directives.
	* elf32-arm.c (elf32_arm_nabi_write_core_note): Add ATTRIBUTE_NONSTRING
	to data.
	* elf32-ppc.c (ppc_elf_write_core_note): Likewise.
	* elf32-s390.c (elf_s390_write_core_note): Likewise.
	* elf64-s390.c (elf_s390_write_core_note): Likewise.
	* elfxx-aarch64.c (_bfd_aarch64_elf_write_core_note): Likewise.
	* elf64-x86-64.c (elf_x86_64_write_core_note): Add GCC diagnostic
	pragmas.
	* peXXigen.c (_bfd_XXi_swap_scnhdr_out): Use strnlen to avoid
	false positive gcc-8 warning.
gas/
	* config/obj-evax.c (shorten_identifier): Use memcpy in place
	of strncpy.
	* config/obj-macho.c (obj_mach_o_make_or_get_sect): Ensure
	segname and sectname fields are NUL terminated.
2018-05-04 18:58:23 +09:30
Alan Modra d99b4b92c8 Silence gcc-8 warnings
This seems to work with gcc-8 and a bunch of prior gcc versions I tested.

	* elf-linux-core.h: Disable gcc-8 string truncation warning.
	* elf.c (elfcore_write_prpsinfo): Likewise.
2018-04-25 17:25:57 +09:30
Nick Clifton db0c309f40 Fix an illegal memory access when trying to copy an ELF binary with corrupt section symbols.
PR 23113
	* elf.c (ignore_section_sym): Check for the output_section pointer
	being NULL before dereferencing it.
2018-04-24 16:57:04 +01:00
Alan Modra b9f26d2e29 Revert bfd part of "Silence gcc-8 warnings"
The gcc warning has been fixed, and the patch regressed builds with
some older versions of gcc.

	* elf-linux-core.h: Revert last change.
	* elf.c: Likewise.
2018-04-23 23:50:13 +09:30
Alan Modra 5a6312e8c0 Silence gcc-8 warnings
All of these warnings were false positives.  -Wstringop-truncation is
particularly annoying when it warns about strncpy used quite correctly.

bfd/
	* elf-linux-core.h (swap_linux_prpsinfo32_ugid32_out): Disable
	gcc-8 string truncation warning.
	(swap_linux_prpsinfo32_ugid16_out): Likewise.
	(swap_linux_prpsinfo64_ugid32_out): Likewise.
	(swap_linux_prpsinfo64_ugid16_out): Likewise.
	* elf.c (elfcore_write_prpsinfo): Likewise.
gas/
	* stabs.c (generate_asm_file): Use memcpy rather than strncpy.
	Remove call to strlen inside loop.
	* config/tc-cr16.c (getreg_image): Warning fix.
	* config/tc-crx.c (getreg_image): Warning fix.
2018-04-23 18:10:41 +09:30
Nick Clifton f3185997ac Have info_to_howto functions return a success/fail status. Check this result. Stop strip from completeing if one of these functions fails.
bfd	PR 22875
	* elf-bfd.h (struct elf_backend_data): Change the return type of
	the elf_info_to_howto and elf_info_to_howto_rel function pointers
	to bfd_boolean.
	* elfcode.h (elf_slurp_reloc_table_from_section): Check the return value from the info_to_howto function and fail if that function failed.
	* elf32-h8300.c (elf32_h8_relocate_section): Check return value from the info_to_howto function.
	(elf32_h8_relax_section): Likewise.
	* elf32-lm32.c (lm32_elf_relocate_section): Likewise.
	* elf32-m68hc1x.c (elf32_m68hc11_relocate_section): Likewise.
	* elf32-score.c (s3_bfd_score_elf_relocate_section): Likewise.
	* elf32-score7.c (s7_bfd_score_elf_relocate_section): Likewise.
	* elf32-tic6x.c (elf32_tic6x_relocate_section): Likewise.
	* elf64-ia64-vms.c (elf64_ia64_relocate_section): Likewise.
	* elf64-sparc.c (elf64_sparc_slurp_one_reloc_table): Likewise.
	* elf64-x86-64.c (elf_x86_64_tls_transition): Likewise.
	* elfnn-ia64.c (elfNN_ia64_relocate_section): Likewise.
	* elfnn-riscv.c (riscv_elf_check_relocs): Likewise.
	(riscv_elf_relocate_section): Likewise.
	* elf-hppa.h (elf_hppa_info_to_howto): Change return type to
	bfd_boolean.  Issue an error message, set an error value and
	return FALSE if the reloc is not recognized.
	(elf_hppa_info_to_howto_rel): Likewise.
	* elf-m10200.c (mn10200_info_to_howto): Likewise.
	* elf-m10300.c (mn10300_info_to_howto): Likewise.
	* elf.c (_bfd_elf_no_info_to_howto): Likewise.
	* elf32-arc.c (arc_info_to_howto_rel): Likewise.
	* elf32-arm.c (elf32_arm_info_to_howto): Likewise.
	* elf32-avr.c (avr_info_to_howto_rela): Likewise.
	* elf32-bfin.c (bfin_info_to_howto): Likewise.
	* elf32-cr16.c (elf_cr16_info_to_howto): Likewise.
	* elf32-cr16c.c (elf_cr16c_info_to_howto): Likewise.
	* elf32-cris.c (elf_cr16c_info_to_howto_rel, cris_info_to_howto_rela): Likewise.
	* elf32-crx.c (elf_crx_info_to_howto): Likewise.
	* elf32-d10v.c (d10v_info_to_howto_rel): Likewise.
	* elf32-d30v.c (d30v_info_to_howto_rel, d30v_info_to_howto_rela): Likewise.
	* elf32-dlx.c (dlx_rtype_to_howto, elf32_dlx_info_to_howto, elf32_dlx_info_to_howto_rel): Likewise.
	* elf32-epiphany.c (epiphany_info_to_howto_rela): Likewise.
	* elf32-fr30.c (fr30_info_to_howto_rela): Likewise.
	* elf32-frv.c (frv_info_to_howto_rela, 	frvfdpic_info_to_howto_rel): Likewise.
	* elf32-ft32.c (ft32_info_to_howto_rela): Likewise.
	* elf32-gen.c (elf_generic_info_to_howto, elf_generic_info_to_howto_rel): Likewise.
	* elf32-h8300.c (elf32_h8_info_to_howto, elf32_h8_info_to_howto_rel): Likewise.
	* elf32-i370.c (i370_elf_info_to_howto): Likewise.
	* elf32-i386.c (elf_i386_reloc_type_lookup, elf_i386_rtype_to_howto, elf_i386_info_to_howto_rel): Likewise.
	* elf32-i860.c (lookup_howto, elf32_i860_info_to_howto_rela): Likewise.
	* elf32-i960.c (elf32_i960_info_to_howto_rel): Likewise.
	* elf32-ip2k.c (ip2k_info_to_howto_rela): Likewise.
	* elf32-iq2000.c (iq2000_info_to_howto_rela): Likewise.
	* elf32-lm32.c (lm32_info_to_howto_rela): Likewise.
	* elf32-m32c.c (m32c_info_to_howto_rela): Likewise.
	* elf32-m32r.c (m32r_info_to_howto_rel, m32r_info_to_howto): Likewise.
	* elf32-m68hc11.c (m68hc11_info_to_howto_rel): Likewise.
	* elf32-m68hc12.c (m68hc11_info_to_howto_rel): Likewise.
	* elf32-m68k.c (rtype_to_howto): Likewise.
	* elf32-mcore.c (mcore_elf_info_to_howto): Likewise.
	* elf32-mep.c (mep_info_to_howto_rela): Likewise.
	* elf32-metag.c (metag_info_to_howto_rela): Likewise.
	* elf32-microblaze.c (microblaze_elf_info_to_howto): Likewise.
	* elf32-mips.c (mips_info_to_howto_rel, mips_info_to_howto_rela): Likewise.
	* elf32-moxie.c (moxie_info_to_howto_rela): Likewise.
	* elf32-msp430.c (msp430_info_to_howto_rela): Likewise.
	* elf32-mt.c (mt_info_to_howto_rela): Likewise.
	* elf32-nds32.c (nds32_info_to_howto_rel, nds32_info_to_howto): Likewise.
	* elf32-nios2.c (nios2_elf32_info_to_howto): Likewise.
	* elf32-or1k.c (or1k_info_to_howto_rela): Likewise.
	* elf32-pj.c (pj_elf_info_to_howto): Likewise.
	* elf32-ppc.c (ppc_elf_info_to_howto): Likewise.
	* elf32-pru.c (pru_elf32_info_to_howto): Likewise.
	* elf32-rl78.c (rl78_info_to_howto_rela): Likewise.
	* elf32-rx.c (rx_info_to_howto_rela): Likewise.
	* elf32-s390.c (elf_s390_info_to_howto): Likewise.
	* elf32-score.c (s3_bfd_score_info_to_howto, _bfd_score_info_to_howto): Likewise.
	* elf32-score7.c (s7_bfd_score_info_to_howto): Likewise.
	* elf32-sh.c (sh_elf_info_to_howto): Likewise.
	* elf32-spu.c (spu_elf_info_to_howto): Likewise.
	* elf32-tic6x.c (elf32_tic6x_info_to_howto, elf32_tic6x_info_to_howto_rel): Likewise.
	* elf32-tilepro.c (tilepro_info_to_howto_rela): Likewise.
	* elf32-v850.c (v850_elf_info_to_howto_rel, v850_elf_info_to_howto_rela, v800_elf_info_to_howto): Likewise.
	* elf32-vax.c (rtype_to_howto): Likewise.
	* elf32-visium.c (visium_info_to_howto_rela): Likewise.
	* elf32-wasm32.c (elf32_wasm32_rtype_to_howto, elf32_wasm32_info_to_howto_rela): Likewise.
	* elf32-xc16x.c (elf32_xc16x_info_to_howto): Likewise.
	* elf32-xgate.c (xgate_info_to_howto_rel): Likewise.
	* elf32-xstormy16.c (xstormy16_info_to_howto_rela): Likewise.
	* elf32-xtensa.c (elf_xtensa_info_to_howto_rela): Likewise.
	* elf64-alpha.c (elf64_alpha_info_to_howto): Likewise.
	* elf64-gen.c (elf_generic_info_to_howto, elf_generic_info_to_howto_rel): Likewise.
	* elf64-ia64-vms.c (elf64_ia64_info_to_howto): Likewise.
	* elf64-mips.c (mips_elf64_info_to_howto_rela): Likewise.
	* elf64-mmix.c (mmix_info_to_howto_rela): Likewise.
	* elf64-ppc.c (ppc64_elf_info_to_howto): Likewise.
	* elf64-s390.c (elf_s390_reloc_type_lookup): Likewise.
	* elf64-sh64.c (elf_s390_info_to_howto, sh_elf64_info_to_howto): Likewise.
	* elf64-x86-64.c (elf_x86_64_info_to_howto): Likewise.
	* elfn32-mips.c (mips_info_to_howto_rel, mips_info_to_howto_rela): Likewise.
	* elfnn-aarch64.c (elfNN_aarch64_info_to_howto): Likewise.
	* elfnn-ia64.c (elfNN_ia64_info_to_howto): Likewise.
	* elfnn-riscv.c (riscv_info_to_howto_rela): Likewise.
	* elfxx-ia64.c (ia64_elf_reloc_type_lookup): Likewise.
	* elfxx-sparc.c (_bfd_sparc_elf_info_to_howto): Likewise.
	* elfxx-tilegx.c (tilegx_info_to_howto_rela): Likewise.
	* elf32-score.h (s7_bfd_score_info_to_howto): Update prototype.
	* elfxx-sparc.h (_bfd_sparc_elf_info_to_howto): Update prototype.
	* elfxx-tilegx.h (tilegx_info_to_howto_rela): Update prototype.
	* elfxx-target.h (elf_info_to_howto, elf_info_to_howto_rel): Default to NULL.

binutils PR 22875
	* objcopy.c (copy_object): Check the error status after marking symbols used in relocations.
	* testsuite/binutils-all/strip-13.s: New test source file.
	* testsuite/binutils-all/strip-13.s: New test driver file.
	* testsuite/binutils-all/objcopy.exp: Run the new test.
2018-02-27 10:15:13 +00:00
Alan Modra 6838f2bed6 Segfault on phdrs allocated but not created
A number of ARM tests show results like:
ld-new: no address assigned to the veneers output section .gnu.sgstubs
ld-new: can not size stub section: invalid operation
ld-new: warning: cannot find entry symbol _start; defaulting to 0000000000010074
ld-new: tmpdir/cmse-veneers-no-gnu_sgstubs: warning: allocated section `.text' not in segment
Segmentation fault
PASS: Secure gateway veneers: no .gnu.sgstubs section

The PASS despite the segv shows the danger of using "#..." to match
error output.  This patch doesn't try to fix the testuite, but does
prevent the segfault.

	* elf.c (assign_file_positions_except_relocs): Don't segfault
	when actual number of phdrs is less than allocated.  Delete
	outdated comment.  Formatting.
2018-02-26 10:51:12 +10:30
Alan Modra 9793eb7792 ELF linker messages
This fixes a number of cases where we capitalized error messages or
printed a full-stop, and corrects "Dwarf" to "DWARF".

bfd/
	* dwarf2.c, * elf-attrs.c, * elf-eh-frame.c, * elf.c, * elf32-gen.c,
	* elflink.c: Standardize error/warning messages.  Replace use of
	linker callback einfo with _bfd_error_handler when possible.
ld/
	* testsuite/ld-elf/indirect.exp,
	* testsuite/ld-elf/pr22649-2ab-mips.msg,
	* testsuite/ld-elf/pr22649-2cd-mips.msg,
	* testsuite/ld-elf/pr22649.msg,
	* testsuite/ld-elf/tls_common.exp,
	* testsuite/ld-elfcomm/elfcomm.exp,
	* testsuite/ld-arm/attr-merge-incompatible.d,
	* testsuite/ld-tic6x/attr-compatibility-gnu-other.d,
	* testsuite/ld-tic6x/attr-compatibility-other-gnu.d,
	* testsuite/ld-tic6x/attr-compatibility-other-other.d,
	* testsuite/ld-i386/warn1.d: Update expected error/warning messages.
2018-02-26 09:29:15 +10:30
Alan Modra 0aa13feeeb unrecognized/unsupported reloc message
It must get boring translating all the variants we have of
unrecognized/unsupported/invalid/unexpected reloc number.  This patch
cuts down on the number of variations.

	* aoutx.h, * coff-alpha.c, * coff-i860.c, * coff-m68k.c,
	* coff-mcore.c, * coff-ppc.c, * coff-rs6000.c, * coff-sh.c,
	* coff-tic4x.c, * coff-tic54x.c, * coff-tic80.c, * coff-w65.c,
	* elf-bfd.h, * elf-m10300.c, * elf.c, * elf32-avr.c, * elf32-bfin.c,
	* elf32-cr16.c, * elf32-cr16c.c, * elf32-cris.c, * elf32-crx.c,
	* elf32-d10v.c, * elf32-d30v.c, * elf32-dlx.c, * elf32-epiphany.c,
	* elf32-fr30.c, * elf32-frv.c, * elf32-i370.c, * elf32-i386.c,
	* elf32-i960.c, * elf32-ip2k.c, * elf32-iq2000.c, * elf32-lm32.c,
	* elf32-m32c.c, * elf32-m32r.c, * elf32-m68hc11.c, * elf32-m68hc12.c,
	* elf32-m68k.c, * elf32-mcore.c, * elf32-mep.c, * elf32-metag.c,
	* elf32-microblaze.c, * elf32-mips.c, * elf32-moxie.c,
	* elf32-msp430.c, * elf32-mt.c, * elf32-nds32.c, * elf32-or1k.c,
	* elf32-pj.c, * elf32-ppc.c, * elf32-rl78.c, * elf32-rx.c,
	* elf32-s390.c, * elf32-sh.c, * elf32-spu.c, * elf32-tic6x.c,
	* elf32-v850.c, * elf32-vax.c, * elf32-visium.c, * elf32-wasm32.c,
	* elf32-xgate.c, * elf32-xtensa.c, * elf64-alpha.c,
	* elf64-ia64-vms.c, * elf64-mips.c, * elf64-mmix.c,
	* elf64-ppc.c, * elf64-s390.c, * elf64-sparc.c, * elf64-x86-64.c,
	* elfn32-mips.c, * elfnn-aarch64.c, * elfnn-ia64.c,
	* elfnn-riscv.c, * elfxx-mips.c, * elfxx-riscv.c, * elfxx-riscv.h,
	* elfxx-sparc.c, * elfxx-sparc.h, * reloc.c: Standardize
	unrecognized/unsupported reloc message.
2018-02-26 09:28:27 +10:30
Alan Modra 01f7e10cf2 PR22881, null pointer dereference in assign_file_positions_for_non_load_sections
PR 22881
	* elf.c (assign_file_positions_for_non_load_sections): Remove RELRO
	segment if no matching LOAD segment.
2018-02-23 21:40:48 +10:30
Alan Modra 2dcf00ce6c %L conversions
* bfd-in.h: Include inttypes.h or if not available define
	PRId64, PRIu64 and PRIx64.
	* bfd.c (_bfd_doprnt, _bfd_doprnt_scan): Remove support for L
	modifier to print bfd_vma.
	* coff-arm.c, * coff-mcore.c, * coff-ppc.c, * coff-rs6000.c,
	* coff-sh.c, * coff-tic80.c, * coffcode.h, * coffgen.c, * cofflink.c,
	* compress.c, * dwarf2.c, * elf-m10300.c, * elf.c, * elf32-arc.c,
	* elf32-arm.c, * elf32-bfin.c, * elf32-cris.c, * elf32-hppa.c,
	* elf32-i386.c, * elf32-ip2k.c, * elf32-lm32.c, * elf32-m32r.c,
	* elf32-m68k.c, * elf32-metag.c, * elf32-nds32.c, * elf32-nios2.c,
	* elf32-ppc.c, * elf32-rx.c, * elf32-s390.c, * elf32-score.c,
	* elf32-score7.c, * elf32-sh.c, * elf32-sh64.c, * elf32-spu.c,
	* elf32-tic6x.c, * elf32-tilepro.c, * elf32-v850.c, * elf32-vax.c,
	* elf32-xtensa.c, * elf64-alpha.c, * elf64-hppa.c, * elf64-ia64-vms.c,
	* elf64-mmix.c, * elf64-s390.c, * elf64-sh64.c, * elf64-x86-64.c,
	* elfcode.h, * elfcore.h, * elflink.c, * elfnn-aarch64.c,
	* elfnn-ia64.c, * elfnn-riscv.c, * elfxx-mips.c, * elfxx-sparc.c,
	* elfxx-tilegx.c, * ieee.c, * ihex.c, * mach-o.c, * merge.c, * mmo.c,
	* peXXigen.c, * xcofflink.c: Replace use of Lx modifier with PRIx64,
	and cast input to uint64_t, and similarly for Ld and Lu.
	* bfd-in2.h: Regenerate.
2018-02-19 23:55:54 +10:30
Alan Modra 871b3ab29e Use %pA and %pB in messages rather than %A and %B
First step towards compiler verification of _bfd_error_handler
arguments, and better verification of translated messages.

bfd/
	* bfd.c (_bfd_doprnt, _bfd_doprnt_scan): Handle %pA and %pB in place
	of %A and %B.
	* aout-adobe.c: Update all messages using %A and %B.
	* aout-cris.c: Likewise.
	* aoutx.h: Likewise.
	* archive.c: Likewise.
	* binary.c: Likewise.
	* cache.c: Likewise.
	* coff-alpha.c: Likewise.
	* coff-arm.c: Likewise.
	* coff-i860.c: Likewise.
	* coff-mcore.c: Likewise.
	* coff-ppc.c: Likewise.
	* coff-rs6000.c: Likewise.
	* coff-sh.c: Likewise.
	* coff-tic4x.c: Likewise.
	* coff-tic54x.c: Likewise.
	* coff-tic80.c: Likewise.
	* coff64-rs6000.c: Likewise.
	* coffcode.h: Likewise.
	* coffgen.c: Likewise.
	* cofflink.c: Likewise.
	* coffswap.h: Likewise.
	* compress.c: Likewise.
	* cpu-arm.c: Likewise.
	* ecoff.c: Likewise.
	* elf-attrs.c: Likewise.
	* elf-eh-frame.c: Likewise.
	* elf-ifunc.c: Likewise.
	* elf-m10300.c: Likewise.
	* elf-properties.c: Likewise.
	* elf-s390-common.c: Likewise.
	* elf.c: Likewise.
	* elf32-arc.c: Likewise.
	* elf32-arm.c: Likewise.
	* elf32-avr.c: Likewise.
	* elf32-bfin.c: Likewise.
	* elf32-cr16.c: Likewise.
	* elf32-cr16c.c: Likewise.
	* elf32-cris.c: Likewise.
	* elf32-crx.c: Likewise.
	* elf32-d10v.c: Likewise.
	* elf32-d30v.c: Likewise.
	* elf32-epiphany.c: Likewise.
	* elf32-fr30.c: Likewise.
	* elf32-frv.c: Likewise.
	* elf32-gen.c: Likewise.
	* elf32-hppa.c: Likewise.
	* elf32-i370.c: Likewise.
	* elf32-i386.c: Likewise.
	* elf32-i960.c: Likewise.
	* elf32-ip2k.c: Likewise.
	* elf32-iq2000.c: Likewise.
	* elf32-lm32.c: Likewise.
	* elf32-m32c.c: Likewise.
	* elf32-m32r.c: Likewise.
	* elf32-m68hc11.c: Likewise.
	* elf32-m68hc12.c: Likewise.
	* elf32-m68hc1x.c: Likewise.
	* elf32-m68k.c: Likewise.
	* elf32-mcore.c: Likewise.
	* elf32-mep.c: Likewise.
	* elf32-metag.c: Likewise.
	* elf32-microblaze.c: Likewise.
	* elf32-moxie.c: Likewise.
	* elf32-msp430.c: Likewise.
	* elf32-mt.c: Likewise.
	* elf32-nds32.c: Likewise.
	* elf32-nios2.c: Likewise.
	* elf32-or1k.c: Likewise.
	* elf32-pj.c: Likewise.
	* elf32-ppc.c: Likewise.
	* elf32-rl78.c: Likewise.
	* elf32-rx.c: Likewise.
	* elf32-s390.c: Likewise.
	* elf32-score.c: Likewise.
	* elf32-score7.c: Likewise.
	* elf32-sh-symbian.c: Likewise.
	* elf32-sh.c: Likewise.
	* elf32-sh64.c: Likewise.
	* elf32-sparc.c: Likewise.
	* elf32-spu.c: Likewise.
	* elf32-tic6x.c: Likewise.
	* elf32-tilepro.c: Likewise.
	* elf32-v850.c: Likewise.
	* elf32-vax.c: Likewise.
	* elf32-visium.c: Likewise.
	* elf32-wasm32.c: Likewise.
	* elf32-xgate.c: Likewise.
	* elf32-xtensa.c: Likewise.
	* elf64-alpha.c: Likewise.
	* elf64-gen.c: Likewise.
	* elf64-hppa.c: Likewise.
	* elf64-ia64-vms.c: Likewise.
	* elf64-mmix.c: Likewise.
	* elf64-ppc.c: Likewise.
	* elf64-s390.c: Likewise.
	* elf64-sh64.c: Likewise.
	* elf64-sparc.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-tilegx.c: Likewise.
	* elfxx-x86.c: Likewise.
	* hpux-core.c: Likewise.
	* ieee.c: Likewise.
	* ihex.c: Likewise.
	* libbfd.c: Likewise.
	* linker.c: Likewise.
	* mach-o.c: Likewise.
	* merge.c: Likewise.
	* mmo.c: Likewise.
	* oasys.c: Likewise.
	* pdp11.c: Likewise.
	* pe-mips.c: Likewise.
	* peXXigen.c: Likewise.
	* peicode.h: Likewise.
	* reloc.c: Likewise.
	* rs6000-core.c: Likewise.
	* srec.c: Likewise.
	* stabs.c: Likewise.
	* vms-alpha.c: Likewise.
	* xcofflink.c: Likewise.
ld/
	* ldmisc.c (vfinfo): Handle %pA and %pB in place of %A and %B.
	* ldcref.c: Update all messages using %A and %B.
	* ldexp.c: Likewise.
	* ldlang.c: Likewise.
	* ldmain.c: Likewise.
	* ldmisc.c: Likewise.
	* pe-dll.c: Likewise.
	* plugin.c: Likewise.
	* emultempl/beos.em: Likewise.
	* emultempl/cr16elf.em: Likewise.
	* emultempl/elf32.em: Likewise.
	* emultempl/m68kcoff.em: Likewise.
	* emultempl/m68kelf.em: Likewise.
	* emultempl/mmo.em: Likewise.
	* emultempl/nds32elf.em: Likewise.
	* emultempl/pe.em: Likewise.
	* emultempl/pep.em: Likewise.
	* emultempl/spuelf.em: Likewise.
	* emultempl/sunos.em: Likewise.
	* emultempl/xtensaelf.em: Likewise.
2018-02-19 23:55:53 +10:30
Alan Modra dbc88fc149 PT_LOAD and PT_GNU_RELRO segment overlap
Commit 325ba6fb34 excluded degenerate zero length PT_LOAD segments,
but that only fixed part of the problem, which was that the load
segment limits were not calculated properly.

	PR 22845
	* elf.c (IS_TBSS): Define.
	(_bfd_elf_map_sections_to_segments): Use IS_TBSS.
	(assign_file_positions_for_non_load_sections): Revert last change.
	Properly calculate load segment limits to compare against relro limits.
2018-02-19 10:40:25 +10:30
Alan Modra 325ba6fb34 Ignore degenerate PT_LOAD segments
Fixes a failure triggered by -z separate-code.  p_memsz is tested
rather than p_filesz for objcopy --only-keep-debug where p_filesz is
set to zero.

	PR 22845
	* elf.c (assign_file_positions_for_non_load_sections): Ignore
	degenerate zero size PT_LOAD segments when finding one overlapping
	the PT_GNU_RELRO segment.
2018-02-17 10:15:55 +10:30
Alan Modra d00dd7dc5e Remove bfd stub function casts.
This patch defines a bunch of new functions to use in the BFD target
structs rather than casting bfd_false or bfd_true and similar stub
functions.  I've also renamed the stub functions to reflect their
parameters and put "error" in the name if they set bfd_error.  The
latter change is important since there were quite a few uses of
bfd_false where setting bfd_error was inappropriate, for example in
elf_backend_allow_non_load_phdr and is_target_special_symbol.

	* libbfd.c (_bfd_bool_bfd_false_error): Rename from bfd_false.
	(_bfd_bool_bfd_true): Rename from bfd_true.
	(_bfd_ptr_bfd_null_error): Rename from bfd_nullvoidptr.
	(_bfd_int_bfd_0): Rename from bfd_0.
	(_bfd_uint_bfd_0): Rename from bfd_0u.
	(_bfd_long_bfd_0): Rename from bfd_0l.
	(_bfd_long_bfd_n1_error): Rename from _bfd_n1.
	(_bfd_void_bfd): Rename from bfd_void.
	(_bfd_bool_bfd_false, _bfd_bool_bfd_asymbol_false),
	(_bfd_bool_bfd_link_false_error),
	(_bfd_bool_bfd_link_true, _bfd_bool_bfd_bfd_true),
	(_bfd_bool_bfd_uint_true, _bfd_bool_bfd_ptr_true),
	(_bfd_bool_bfd_asection_bfd_asection_true),
	(_bfd_bool_bfd_asymbol_bfd_asymbol_true),
	(_bfd_void_bfd_link, _bfd_void_bfd_asection): New functions.
	* archive.c (_bfd_noarchive_get_elt_at_index),
	(_bfd_noarchive_openr_next_archived_file),
	(_bfd_noarchive_construct_extended_name_table),
	(_bfd_noarchive_write_ar_hdr, _bfd_noarchive_truncate_arname),
	(_bfd_noarchive_write_armap): New functions.
	* archures.c (_bfd_nowrite_set_arch_mach): New function.
	* coff-alpha.c (alpha_ecoff_swap_coff_aux_in),
	(alpha_ecoff_swap_coff_sym_in, alpha_ecoff_swap_coff_lineno_in),
	(alpha_ecoff_swap_coff_aux_out, alpha_ecoff_swap_coff_sym_out),
	(alpha_ecoff_swap_coff_lineno_out),
	(alpha_ecoff_swap_coff_reloc_out): New functions.
	* coff-mips.c (mips_ecoff_swap_coff_aux_in),
	(mips_ecoff_swap_coff_sym_in, mips_ecoff_swap_coff_lineno_in),
	(mips_ecoff_swap_coff_aux_out, mips_ecoff_swap_coff_sym_out),
	(mips_ecoff_swap_coff_lineno_out),
	(mips_ecoff_swap_coff_reloc_out): New functions.
	* coffcode.h (coff_set_alignment_hook): Replace define with
	new function.
	(symname_in_debug_hook): Likewise.
	* ecoff.c (_bfd_ecoff_set_alignment_hook): New function.
	* elfxx-target.h (elf_backend_allow_non_load_phdr): Default to 0.
	* elf.c (assign_file_positions_except_relocs): Test
	elf_backend_allow_non_load_phdr for NULL.
	* elflink.c (_bfd_elf_omit_section_dynsym_default): Rename from
	_bfd_elf_link_omit_section_dynsym.  Update uses.
	(_bfd_elf_omit_section_dynsym_all): New function.
	* elf-bfd.h (_bfd_elf_link_omit_section_dynsym): Delete.
	(_bfd_elf_omit_section_dynsym_default): Declare.
	(_bfd_elf_omit_section_dynsym_all): Declare.
	* linker.c (_bfd_nolink_sizeof_headers, _bfd_nolink_bfd_relax_section),
	(_bfd_nolink_bfd_get_relocated_section_contents),
	(_bfd_nolink_bfd_lookup_section_flags),
	(_bfd_nolink_bfd_is_group_section, _bfd_nolink_bfd_discard_group),
	(_bfd_nolink_bfd_link_hash_table_create),
	(_bfd_nolink_bfd_link_just_syms),
	(_bfd_nolink_bfd_copy_link_hash_symbol_type),
	(_bfd_nolink_bfd_link_split_section),
	(_bfd_nolink_section_already_linked),
	(_bfd_nolink_bfd_define_common_symbol),
	(_bfd_nolink_bfd_define_start_stop): New functions.
	* reloc.c (_bfd_norelocs_bfd_reloc_type_lookup),
	(_bfd_norelocs_bfd_reloc_name_lookup),
	(_bfd_nodynamic_canonicalize_dynamic_reloc): New functions.
	* section.c (_bfd_nowrite_set_section_contents): New function.
	* syms.c (_bfd_nosymbols_canonicalize_symtab),
	(_bfd_nosymbols_print_symbol, _bfd_nosymbols_get_symbol_info),
	(_bfd_nosymbols_get_symbol_version_string),
	(_bfd_nosymbols_bfd_is_local_label_name),
	(_bfd_nosymbols_get_lineno, _bfd_nosymbols_find_nearest_line),
	(_bfd_nosymbols_find_line, _bfd_nosymbols_find_inliner_info),
	(_bfd_nosymbols_bfd_make_debug_symbol),
	( _bfd_nosymbols_read_minisymbols),
	( _bfd_nosymbols_minisymbol_to_symbol),
	(_bfd_nodynamic_get_synthetic_symtab): New functions.
	* libbfd-in.h: Declare new functions.  Update existing defines,
	removing casts.
	* aix386-core.c: Update to use new hooks.  Formatting.
	* aout-adobe.c: Likewise.
	* aout-arm.c: Likewise.
	* aout-target.h: Likewise.
	* aout-tic30.c: Likewise.
	* aoutf1.h: Likewise.
	* binary.c: Likewise.
	* bout.c: Likewise.
	* cisco-core.c: Likewise.
	* coff-alpha.c: Likewise.
	* coff-i386.c: Likewise.
	* coff-i860.c: Likewise.
	* coff-i960.c: Likewise.
	* coff-ia64.c: Likewise.
	* coff-mips.c: Likewise.
	* coff-ppc.c: Likewise.
	* coff-rs6000.c: Likewise.
	* coff-sh.c: Likewise.
	* coff-tic30.c: Likewise.
	* coff-tic54x.c: Likewise.
	* coff-x86_64.c: Likewise.
	* coff64-rs6000.c: Likewise.
	* coffcode.h: Likewise.
	* elf-m10300.c: Likewise.
	* elf32-cr16.c: Likewise.
	* elf32-lm32.c: Likewise.
	* elf32-m32r.c: Likewise.
	* elf32-metag.c: Likewise.
	* elf32-score.c: Likewise.
	* elf32-score7.c: Likewise.
	* elf32-tilepro.c: Likewise.
	* elf32-xstormy16.c: Likewise.
	* elf32-xtensa.c: Likewise.
	* elf64-alpha.c: Likewise.
	* elf64-hppa.c: Likewise.
	* elf64-ia64-vms.c: Likewise.
	* elf64-mmix.c: Likewise.
	* elf64-sh64.c: Likewise.
	* elfnn-ia64.c: Likewise.
	* elfxx-sparc.c: Likewise.
	* elfxx-target.h: Likewise.
	* elfxx-tilegx.c: Likewise.
	* elfxx-x86.h: Likewise.
	* hp300hpux.c: Likewise.
	* hppabsd-core.c: Likewise.
	* hpux-core.c: Likewise.
	* i386msdos.c: Likewise.
	* i386os9k.c: Likewise.
	* ieee.c: Likewise.
	* ihex.c: Likewise.
	* irix-core.c: Likewise.
	* libaout.h: Likewise.
	* libecoff.h: Likewise.
	* mach-o-target.c: Likewise.
	* mach-o.c: Likewise.
	* mipsbsd.c: Likewise.
	* mmo.c: Likewise.
	* netbsd-core.c: Likewise.
	* nlm-target.h: Likewise.
	* oasys.c: Likewise.
	* osf-core.c: Likewise.
	* pdp11.c: Likewise.
	* pe-mips.c: Likewise.
	* pe-x86_64.c: Likewise.
	* pef.c: Likewise.
	* plugin.c: Likewise.
	* ppcboot.c: Likewise.
	* ptrace-core.c: Likewise.
	* sco5-core.c: Likewise.
	* som.c: Likewise.
	* sparclynx.c: Likewise.
	* srec.c: Likewise.
	* tekhex.c: Likewise.
	* trad-core.c: Likewise.
	* verilog.c: Likewise.
	* versados.c: Likewise.
	* vms-alpha.c: Likewise.
	* vms-lib.c: Likewise.
	* wasm-module.c: Likewise.
	* xsym.c: Likewise.
	* libbfd.h: Regenerate.
2018-02-16 19:09:23 +10:30
Alan Modra 6e5e9d58c1 PR22836, "-r -s" doesn't work with -g3 using GCC 7
This fixes the case where all of a group is removed with ld -r, the
situation in the PR, and failures where part of a group is removed
that contain relocs.

bfd/
	PR 22836
	* elf.c (_bfd_elf_fixup_group_sections): Account for removed
	relocation sections.  If size reduces to just the flag word,
	remove that too and mark with SEC_EXCLUDE.
	* elflink.c (bfd_elf_final_link): Strip empty group sections.
binutils/
	* testsuite/binutils-all/group-7.s,
	* testsuite/binutils-all/group-7a.d,
	* testsuite/binutils-all/group-7b.d,
	* testsuite/binutils-all/group-7c.d: New tests.
	* testsuite/binutils-all/objcopy.exp: Run them.
ld/
	* testsuite/ld-elf/pr22836-2.d,
	* testsuite/ld-elf/pr22836-2.s: New test.
2018-02-13 22:55:49 +10:30
Alan Modra f2731e0c37 PR22829, objcopy/strip removes PT_GNU_RELRO from lld binaries
lld lays out the relro segment differently to GNU ld, not bothering to
include the first few bytes of .got.plt and padding out to a page at
the end of the segment.  This patch teaches binutils to recognize the
different (and somewhat inferior) layout as valid.

bfd/
	PR 22829
	* elf.c (assign_file_positions_for_non_load_sections): Rewrite
	PT_GNU_RELRO setup.
ld/
	* testsuite/ld-x86-64/pr14207.d: Adjust relro p_filesz.
2018-02-13 19:30:47 +10:30
Nick Clifton ef135d4314 Fix a seg-fault in the ELF note parser when a note with an excessively large alignment is encountered.
PR 22788
	* elf.c (elf_parse_notes): Reject notes with excessuively large
	alignments.
2018-02-08 10:28:25 +00:00
Maciej W. Rozycki 7550610057 ELF/BFD: Propagate the return status from backend section processing
Fix the issue of any failure from `->elf_backend_section_processing'
not being propagated by `_bfd_elf_write_object_contents'.  The MIPS
`_bfd_mips_elf_section_processing' handler can actually potentially
fail, however the caller currently ignores that.

	bfd/
	* elf.c (_bfd_elf_write_object_contents): Propagate a failure
	status from `->elf_backend_section_processing'.
2018-02-05 14:00:21 +00:00
Alan Modra 76cb3a89a6 PR22758, FAIL: Run pr22393-2
We can't map different disk pages into the same memory page; The last
page mapped will simply overwrite any previous pages.  The
executable/non-executable new_segment test ignored this fact, leading
to a ld.so segfault on hppa when .dynamic is overwritten with zeros.

This patch moves existing tests for demand paging with lma on the same
memory page, to a new test performed before any case where we want a
new segment due to protection or loadable conflicts.

	PR 22758
	* elf.c (_bfd_elf_map_sections_to_segments): Don't start a new
	segment when demand paged with lma on the same page.  Test this
	before load/non-load, executable/non-executable,
	writable/non-writable tests and simplify.  Delete bogus relro
	condition in writable/non-writable test.  Delete outdated
	comment.  Formatting.
2018-01-30 18:58:51 +10:30
Jens Widell 564e11c9a9 Optimize the performance of the group_setup function.
When processing a section that is a member of a group, the group
that contains it is looked up using a linear search. The resulting
O(n^2) complexity causes significant performance issues when
dealing with object files with very many groups.

By remembering the index of the last found group and restarting
the next search from that index, the search instead becomes O(n)
in common cases.

	* elf.c (setup_group): Optimize search for group by remembering
	last found group and restarting search at that index.
	* elf-bfd.h (struct elf_obj_tdata): Add group_search_offset field.
2018-01-12 13:16:17 +00:00
H.J. Lu 2888249fc9 ld: Create a new LOAD segment for separate code segment
When generating separate code LOAD segment, create a new LOAD segment
if the previous section contains text and the current section doesn't
or vice versa:

Elf file type is DYN (Shared object file)
Entry point 0x200020
There are 7 program headers, starting at offset 52

Program Headers:
  Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  Flg Align
  LOAD           0x000000 0x00000000 0x00000000 0x00200 0x00200 R   0x200000
  LOAD           0x200000 0x00200000 0x00200000 0x00036 0x00036 R E 0x200000
  LOAD           0x400000 0x00400000 0x00400000 0x00064 0x00064 R   0x200000
  LOAD           0x400f80 0x00600f80 0x00600f80 0x000a0 0x000a0 RW  0x200000
  DYNAMIC        0x400f80 0x00600f80 0x00600f80 0x00080 0x00080 RW  0x4
  GNU_STACK      0x000000 0x00000000 0x00000000 0x00000 0x00000 RW  0x10
  GNU_RELRO      0x400f80 0x00600f80 0x00600f80 0x00080 0x00080 R   0x1

 Section to Segment mapping:
  Segment Sections...
   00     .hash .gnu.hash .dynsym .dynstr .rela.plt
   01     .plt .text
   02     .rodata .eh_frame
   03     .dynamic .got.plt
   04     .dynamic
   05
   06     .dynamic

to prevent fetching or executing data in code pages as instructions.

Also don't put a writable section in a read-only segment if there is a
RELRO segment.

Since code segment is aligned and padded to the maximum page size on
disk, the minimum file size is bigger than the maximum page size which
is 2MB (0x200000):

-rwxr-xr-x 1 hjl hjl 4201932 Jan 10 10:41 libfoo.so

"-z max-page-size=0x1000" can be used to reduce the maximum page size to
4KB (0x1000):

-rwxr-xr-x 1 hjl hjl   15820 Jan 10 10:44 libfoo.so

	PR ld/22393
	* elf.c (_bfd_elf_map_sections_to_segments): When generating
	separate code and read-only data LOAD segments, create a new
	LOAD segment if the previous section contains text and the
	current section doesn't or vice versa.  Don't put a writable
	section in a read-only segment if there is a RELRO segment.
2018-01-11 19:05:06 -08:00
John Baldwin ddb2bbcf06 Create pseudo sections for FreeBSD NT_PROCSTAT_(PROC|FILES|VMMAP) notes.
bfd/ChangeLog:

	* elf.c (elfcore_grok_freebsd_note): Handle
	NT_FREEBSD_PROCSTAT_PROC, NT_FREEBSD_PROCSTAT_FILES, and
	NT_FREEBSD_PROCSTAT_VMMAP.
2018-01-03 12:06:53 -08:00
Alan Modra 219d1afa89 Update year range in copyright notice of binutils files 2018-01-03 17:49:56 +10:30
Alan Modra 07d6d2b834 BFD whitespace fixes
Binutils is supposed to use tabs.  In my git config I have
whitespace = indent-with-non-tab,space-before-tab,trailing-space
and I got annoyed enough seeing red in "git diff" output to fix
the problems.

	* doc/header.sed: Trim trailing space when splitting lines.
	* aix386-core.c, * aout-adobe.c, * aout-arm.c, * aout-cris.c,
	* aout-ns32k.c, * aout-target.h, * aout-tic30.c, * aoutf1.h, * aoutx.h,
	* arc-got.h, * arc-plt.def, * arc-plt.h, * archive.c, * archive64.c,
	* archures.c, * armnetbsd.c, * bfd-in.h, * bfd.c, * bfdio.c, * binary.c,
	* bout.c, * cache.c, * cisco-core.c, * coff-alpha.c, * coff-apollo.c,
	* coff-arm.c, * coff-h8300.c, * coff-i386.c, * coff-i860.c,
	* coff-i960.c, * coff-m68k.c, * coff-m88k.c, * coff-mcore.c,
	* coff-mips.c, * coff-ppc.c, * coff-rs6000.c, * coff-sh.c,
	* coff-stgo32.c, * coff-tic4x.c, * coff-tic54x.c, * coff-tic80.c,
	* coff-we32k.c, * coff-x86_64.c, * coff-z80.c, * coff-z8k.c,
	* coff64-rs6000.c, * coffcode.h, * coffgen.c, * cofflink.c,
	* coffswap.h, * compress.c, * corefile.c, * cpu-alpha.c, * cpu-arm.c,
	* cpu-avr.c, * cpu-bfin.c, * cpu-cr16.c, * cpu-cr16c.c, * cpu-crx.c,
	* cpu-d10v.c, * cpu-frv.c, * cpu-ft32.c, * cpu-i370.c, * cpu-i960.c,
	* cpu-ia64-opc.c, * cpu-ip2k.c, * cpu-lm32.c, * cpu-m32r.c,
	* cpu-mcore.c, * cpu-microblaze.c, * cpu-mips.c, * cpu-moxie.c,
	* cpu-mt.c, * cpu-nios2.c, * cpu-ns32k.c, * cpu-or1k.c, * cpu-powerpc.c,
	* cpu-pru.c, * cpu-sh.c, * cpu-spu.c, * cpu-v850.c, * cpu-v850_rh850.c,
	* cpu-xgate.c, * cpu-z80.c, * dwarf1.c, * dwarf2.c, * ecoff.c,
	* ecofflink.c, * ecoffswap.h, * elf-bfd.h, * elf-eh-frame.c,
	* elf-hppa.h, * elf-m10200.c, * elf-m10300.c, * elf-s390-common.c,
	* elf-strtab.c, * elf-vxworks.c, * elf.c, * elf32-am33lin.c,
	* elf32-arc.c, * elf32-arm.c, * elf32-avr.c, * elf32-avr.h,
	* elf32-bfin.c, * elf32-cr16.c, * elf32-cr16c.c, * elf32-cris.c,
	* elf32-crx.c, * elf32-d10v.c, * elf32-d30v.c, * elf32-dlx.c,
	* elf32-epiphany.c, * elf32-fr30.c, * elf32-frv.c, * elf32-ft32.c,
	* elf32-h8300.c, * elf32-hppa.c, * elf32-i386.c, * elf32-i860.c,
	* elf32-i960.c, * elf32-ip2k.c, * elf32-lm32.c, * elf32-m32c.c,
	* elf32-m32r.c, * elf32-m68hc11.c, * elf32-m68hc12.c, * elf32-m68hc1x.c,
	* elf32-m68hc1x.h, * elf32-m68k.c, * elf32-m88k.c, * elf32-mcore.c,
	* elf32-mep.c, * elf32-metag.c, * elf32-microblaze.c, * elf32-mips.c,
	* elf32-moxie.c, * elf32-msp430.c, * elf32-mt.c, * elf32-nds32.c,
	* elf32-nds32.h, * elf32-nios2.c, * elf32-or1k.c, * elf32-pj.c,
	* elf32-ppc.c, * elf32-ppc.h, * elf32-pru.c, * elf32-rl78.c,
	* elf32-rx.c, * elf32-s390.c, * elf32-score.c, * elf32-score.h,
	* elf32-score7.c, * elf32-sh-symbian.c, * elf32-sh.c, * elf32-sh64.c,
	* elf32-sparc.c, * elf32-spu.c, * elf32-tic6x.c, * elf32-tilegx.c,
	* elf32-tilegx.h, * elf32-tilepro.c, * elf32-tilepro.h, * elf32-v850.c,
	* elf32-vax.c, * elf32-wasm32.c, * elf32-xc16x.c, * elf32-xgate.c,
	* elf32-xgate.h, * elf32-xstormy16.c, * elf32-xtensa.c, * elf64-alpha.c,
	* elf64-hppa.c, * elf64-ia64-vms.c, * elf64-mips.c, * elf64-mmix.c,
	* elf64-ppc.c, * elf64-s390.c, * elf64-sh64.c, * elf64-sparc.c,
	* elf64-tilegx.c, * elf64-tilegx.h, * elf64-x86-64.c, * elfcore.h,
	* elflink.c, * elfn32-mips.c, * elfnn-aarch64.c, * elfnn-ia64.c,
	* elfnn-riscv.c, * elfxx-aarch64.c, * elfxx-aarch64.h, * elfxx-ia64.c,
	* elfxx-ia64.h, * elfxx-mips.c, * elfxx-riscv.c, * elfxx-sparc.c,
	* elfxx-tilegx.c, * elfxx-x86.c, * elfxx-x86.h, * freebsd.h, * hash.c,
	* host-aout.c, * hp300hpux.c, * hppabsd-core.c, * hpux-core.c,
	* i386aout.c, * i386linux.c, * i386lynx.c, * i386mach3.c, * i386msdos.c,
	* i386netbsd.c, * ieee.c, * ihex.c, * irix-core.c, * libaout.h,
	* libbfd-in.h, * libbfd.c, * libcoff-in.h, * libnlm.h, * libpei.h,
	* libxcoff.h, * linker.c, * lynx-core.c, * m68k4knetbsd.c,
	* m68klinux.c, * m68knetbsd.c, * m88kmach3.c, * mach-o-aarch64.c,
	* mach-o-arm.c, * mach-o-i386.c, * mach-o-target.c, * mach-o-x86-64.c,
	* mach-o.c, * mach-o.h, * merge.c, * mipsbsd.c, * mmo.c, * netbsd.h,
	* netbsd-core.c, * newsos3.c, * nlm-target.h, * nlm32-ppc.c,
	* nlm32-sparc.c, * nlmcode.h, * ns32k.h, * ns32knetbsd.c, * oasys.c,
	* opncls.c, * pc532-mach.c, * pdp11.c, * pe-arm.c, * pe-i386.c,
	* pe-mcore.c, * pe-mips.c, * pe-x86_64.c, * peXXigen.c, * pef.c,
	* pef.h, * pei-arm.c, * pei-i386.c, * pei-mcore.c, * pei-x86_64.c,
	* peicode.h, * plugin.c, * ppcboot.c, * ptrace-core.c, * reloc.c,
	* riscix.c, * rs6000-core.c, * section.c, * som.c, * som.h,
	* sparclinux.c, * sparcnetbsd.c, * srec.c, * stabs.c, * sunos.c,
	* syms.c, * targets.c, * tekhex.c, * trad-core.c, * vax1knetbsd.c,
	* vaxnetbsd.c, * verilog.c, * versados.c, * vms-alpha.c, * vms-lib.c,
	* vms-misc.c, * wasm-module.c, * wasm-module.h, * xcofflink.c,
	* xsym.c, * xsym.h: Whitespace fixes.
	* bfd-in2.h, * libbfd.h, * libcoff.h: Regenerate.
2017-12-06 17:51:43 +10:30
H.J. Lu 276da9b31b Re-apply "elf: Properly compute offsets of note descriptor and next note"
CORE PT_NOTE segments may have p_align values of 0 or 1.  gABI specifies
that PT_NOTE alignment should be aligned to 4 bytes for 32-bit objects
and to 8 bytes for 64-bit objects.  If segment alignment is less than 4,
we use 4 byte alignment.
2017-11-24 14:52:15 -08:00
Simon Marchi 4e25ac038e Revert "elf: Properly compute offsets of note descriptor and next note"
This reverts commit 650444eb54.

With this patch, running the GDB test case gdb.base/auxv.exp is stuck in
an infinite loop, consuming memory to the point that it renders the
machine unusable. I am reverting it so we can take our time to
investigate while not killing all the developers' machines.
2017-11-24 15:43:51 -05:00
H.J. Lu 650444eb54 elf: Properly compute offsets of note descriptor and next note
According to gABI, in a note entry, the note name field, not note name
size, is padded for the note descriptor.  And the note descriptor field,
not note descriptor size, is padded for the next note entry.  Also notes
are aligned to 4 bytes in 32-bit objects and 8 bytes in 64-bit objects.
Since on Linux, .note.ABI-tag and .note.gnu.build-id notes are always
aligned to 4 bytes, we need to use alignment of note section or note
segment, instead of assuming alignment based on ELF file class.

Tested on i686 and x86-64.

bfd/

	PR binutils/22444
	* elf.c (elf_read_notes): Add an argument for note aligment.
	(elf_parse_notes): Likewise.
	(_bfd_elf_make_section_from_shdr): Pass section aligment to
	elf_parse_notes.
	(bfd_section_from_phdr): Pass segment aligment to elf_read_notes.
	(elf_parse_notes): Add an argument for note aligment.  Use
	ELF_NOTE_DESC_OFFSET to get the offset of the note descriptor.
	Use ELF_NOTE_NEXT_OFFSET to get the offset of the next note
	entry.
	(elf_read_notes): Add an argument for note aligment and pass it
	to elf_parse_notes.

binutils/

	PR binutils/22444
	* readelf.c (process_notes_at): Use ELF_NOTE_DESC_OFFSET to get
	the offset of the note descriptor.  Use ELF_NOTE_NEXT_OFFSET to
	get the offset of the next note entry.

include/

	PR binutils/22444
	* elf/external.h (ELF_ALIGN_UP): New.
	(ELF_NOTE_DESC_OFFSET): Likewise.
	(ELF_NOTE_NEXT_OFFSET): Likewise.
2017-11-24 06:41:52 -08:00
Alan Modra 28e07a0510 PR22451, strip no longer works on older object files
Setting SHF_GROUP unconditionally on rel/rela sections associated with
SHF_GROUP sections fails badly with objcopy/strip and ld -r if the
input file SHT_GROUP section didn't specify the rel/rela sections.
This patch rearranges where SHF_GROUP is set for rel/rela sections.

	PR 22451
	PR 22460
	* elf.c (_bfd_elf_init_reloc_shdr): Delete "sec_hdr" parameter
	and leave rel_hdr->sh_flags zero.  Update calls.
	(bfd_elf_set_group_contents): Check input rel/rela SHF_GROUP
	flag when !gas before adding rel/rela section to group.  Set
	output rel/rela SHF_GROUP flags.
2017-11-21 00:09:07 +10:30
Nick Clifton 80a0437873 Prevent illegal memory accesses when parsing incorrecctly formated core notes.
PR 22421
	* elf.c (elfcore_grok_netbsd_procinfo): Check that the note is big enough.
	(elfcore_grok_openbsd_procinfo): Likewise.
	(elfcore_grok_nto_status): Likewise.
2017-11-16 14:53:32 +00:00
H.J. Lu f882209d9a ld: Remove PF_X from PT_PHDR segment
It was reasonable to mark PT_PHDR segment with PF_X for compatibility
with UnixWare and Solaris linkers 20 years ago.  But it is inappropriate
today when the primary OS of GNU ld is Linux.  This patch removes PF_X
from PT_PHDR segment as gold does.

Tested natively on Linux/x86 as well as crosss-binutils for alpha-linux,
ia64-linux, powerpc64-linux, powerpc-linux, s390-linux, s390x-linux,
sparc64-linux and sparc-linux.

bfd/

	PR ld/22423
	* elf.c (_bfd_elf_map_sections_to_segments): Remove PF_X from
	PT_PHDR segment.

ld/

	PR ld/22423
	* testsuite/ld-alpha/tlsbin.rd: Replace "R E " with "R +" for
	PT_PHDR segment.
	* testsuite/ld-alpha/tlsbinr.rd: Likewise.
	* testsuite/ld-ia64/tlsbin.rd: Likewise.
	* testsuite/ld-powerpc/tlsexe.r: Likewise.
	* testsuite/ld-powerpc/tlsexe32.r: Likewise.
	* testsuite/ld-powerpc/tlsexetoc.r: Likewise.
	* testsuite/ld-s390/tlsbin.rd: Likewise.
	* testsuite/ld-s390/tlsbin_64.rd: Likewise.
	* testsuite/ld-sparc/tlssunbin32.rd: Likewise.
	* testsuite/ld-sparc/tlssunbin64.rd: Likewise.
	* testsuite/ld-elf/pr22423.d: New test.
2017-11-12 17:04:03 -08:00
Maciej W. Rozycki 3c9a7b0d6d ELF/BFD: Handle both variants of the 64-bit Linux core PRPSINFO note
Fix commit 70a38d42c5 ("New entry points for writing Linux NT_PRPSINFO
notes."), <https://sourceware.org/ml/binutils/2013-02/msg00023.html>,
and handle both variants of the 64-bit Linux core PRPSINFO note across
all targets.

The 64-bit Linux core PRPSINFO note matches the 64-bit kernel structure,
defined as follows:

(gdb) ptype struct elf_prpsinfo
type = struct elf_prpsinfo {
    char pr_state;
    char pr_sname;
    char pr_zomb;
    char pr_nice;
    unsigned long pr_flag;
    __kernel_uid_t pr_uid;
    __kernel_gid_t pr_gid;
    pid_t pr_pid;
    pid_t pr_ppid;
    pid_t pr_pgrp;
    pid_t pr_sid;
    char pr_fname[16];
    char pr_psargs[80];
}
(gdb)

where the individual data types of most members are the same across all
64-bit Linux ports, with the exception of `__kernel_uid_t' and
`__kernel_gid_t'.  These are defined in <asm-generic/posix_types.h> as
32-bit `unsigned int' by default, however overridden as 16-bit `unsigned
short' in port-specific <asm/posix_types.h> for the SuperH target.

The default is already handled, as from the commit referred.  Add the
other variant then and provide a backend flag to switch between the two
cases possible, with the 32-bit one being the default and the 16-bit one
explicitly selected.  Set the flag in the SuperH target backend.  This
arrangement is analogous to how the 32-bit Linux core PRPSINFO note is
handled.

	bfd/
	* elf-bfd.h (elf_backend_data): Add `linux_prpsinfo64_ugid16'
	member.
	* elf-linux-core.h (elf_external_linux_prpsinfo64): Rename to...
	(elf_external_linux_prpsinfo64_ugid32): ... this.
	(swap_linux_prpsinfo32_out): Rename to...
	(swap_linux_prpsinfo32_ugid32_out): ... this.
	(elf_external_linux_prpsinfo64_ugid16): New structure.
	(swap_linux_prpsinfo64_ugid16_out): New function.
	* elfxx-target.h [!elf_backend_linux_prpsinfo64_ugid16]
	(elf_backend_linux_prpsinfo64_ugid16): Define.
	(elfNN_bed): Initialize `linux_prpsinfo64_ugid16' member.
	* elf.c (elfcore_write_linux_prpsinfo64): Handle both variants
	of the 64-bit Linux core PRPSINFO note.
	* elf64-sh64.c (elf_backend_linux_prpsinfo64_ugid16): Define.
2017-10-11 15:01:41 +01:00
Maciej W. Rozycki a2f63b2e7a ELF/BFD,GDB: Handle both variants of the 32-bit Linux core PRPSINFO note
Fix commit 70a38d42c5 ("New entry points for writing Linux NT_PRPSINFO
notes."), <https://sourceware.org/ml/binutils/2013-02/msg00023.html>,
and commit b3ac9c7756 ("Put more info in NT_PRPSINFO Linux notes"),
<https://sourceware.org/ml/binutils/2013-02/msg00024.html>, and handle
both variants of the 32-bit Linux core PRPSINFO note across all targets.

The 32-bit Linux core PRPSINFO note matches the 32-bit kernel structure,
defined as follows:

(gdb) ptype struct elf_prpsinfo
type = struct elf_prpsinfo {
    char pr_state;
    char pr_sname;
    char pr_zomb;
    char pr_nice;
    unsigned long pr_flag;
    __kernel_uid_t pr_uid;
    __kernel_gid_t pr_gid;
    pid_t pr_pid;
    pid_t pr_ppid;
    pid_t pr_pgrp;
    pid_t pr_sid;
    char pr_fname[16];
    char pr_psargs[80];
}
(gdb)

where the individual data types of most members are the same across all
32-bit Linux ports, with the exception of `__kernel_uid_t' and
`__kernel_gid_t'.  These are defined in <asm-generic/posix_types.h> as
32-bit `unsigned int' by default, however overridden as 16-bit `unsigned
short' in port-specific <asm/posix_types.h> for a few targets, mostly
earlier ports of Linux, specifically: ARM, CRIS, FR-V, M32R, m68k,
MN10300/AM33, s390, SuperH, SPARC and i386.

The default is the same as the PowerPC variant already handled, as from
the commits referred.  Make the special PowerPC case generic then,
removing the GDB part, and provide a backend flag to switch between the
two cases possible, with the 32-bit one being the default and the 16-bit
one explicitly selected.  Set the flag in the target backends affected.

	bfd/
	* elf-bfd.h (elf_backend_data): Add `linux_prpsinfo32_ugid16'
	member.
	(elfcore_write_ppc_linux_prpsinfo32): Remove prototype.
	* elf32-ppc.c (elfcore_write_ppc_linux_prpsinfo32): Remove.
	(elf_external_ppc_linux_prpsinfo32)
	(swap_ppc_linux_prpsinfo32_out): Move to...
	* elf-linux-core.h (elf_external_linux_prpsinfo32_ugid32)
	(swap_linux_prpsinfo32_ugid32_out): ... these.
	(elf_external_linux_prpsinfo32): Rename to...
	(elf_external_linux_prpsinfo32_ugid16): ... this.
	(swap_linux_prpsinfo32_out): Rename to...
	(swap_linux_prpsinfo32_ugid16_out): ... this.
	* elfxx-target.h [!elf_backend_linux_prpsinfo32_ugid16]
	(elf_backend_linux_prpsinfo32_ugid16): Define.
	(elfNN_bed): Initialize `linux_prpsinfo32_ugid16' member.
	* elf.c (elfcore_write_linux_prpsinfo32): Handle both variants
	of the 32-bit Linux core PRPSINFO note.
	* elf32-am33lin.c (elf_backend_linux_prpsinfo32_ugid16): Define.
	* elf32-arm.c (elf_backend_linux_prpsinfo32_ugid16): Define.
	* elf32-cris.c (elf_backend_linux_prpsinfo32_ugid16): Define.
	* elf32-frv.c (elf_backend_linux_prpsinfo32_ugid16): Define.
	* elf32-i386.c (elf_backend_linux_prpsinfo32_ugid16): Define.
	* elf32-m32r.c (elf_backend_linux_prpsinfo32_ugid16): Define.
	* elf32-m68k.c (elf_backend_linux_prpsinfo32_ugid16): Define.
	* elf32-s390.c (elf_backend_linux_prpsinfo32_ugid16): Define.
	* elf32-sh.c (elf_backend_linux_prpsinfo32_ugid16): Define.
	* elf32-sparc.c (elf_backend_linux_prpsinfo32_ugid16): Define.

	gdb/
	* ppc-linux-tdep.c (ppc_linux_init_abi): Remove call to
	`set_gdbarch_elfcore_write_linux_prpsinfo'.
2017-10-11 15:01:40 +01:00
John Baldwin 544c67cda1 Account for padding in FreeBSD/mipsn32 NT_PRSTATUS notes.
Add a new ELF backend method to grok FreeBSD NT_PRSTATUS core dump
notes.  Define a method for MIPS N32 to override the default
elfcore_grok_freebsd_prstatus that accounts for additional padding
between pr_pid and pr_reg that is not present in other 32-bit FreeBSD
platforms.

	* elf-bfd.h (struct elf_backend_data): Add
	`elf_backend_grok_freebsd_prstatus'.
	* elf.c (elfcore_grok_freebsd_note): Call
	`elf_backend_grok_freebsd_prstatus' to handle NT_PRSTATUS if
	present.
	* elfn32-mips.c (elf_n32_mips_grok_freebsd_prstatus): New
	function.
	(elf_backend_grok_freebsd_prstatus): Define.
	* elfxx-target.h (elf_backend_grok_freebsd_prstatus): Define.
	(elfNN_bed): Initialize `elf_backend_grok_freebsd_prstatus'.
2017-10-06 11:43:07 -07:00
John Baldwin 6d5be5d6b8 Handle the NT_ARM_VFP core dump note on FreeBSD.
bfd/ChangeLog:

	* elf.c (elfcore_grok_freebsd_note): Handle NT_ARM_VFP.
2017-10-05 09:50:01 -07:00
Alan Modra db4677b8bd PR21167, relocation sections not included in groups
This fixes a wart I've known about for years, but haven't done
anything about because BFD treats relocation sections as an adjunct to
the section they relocate.  SHF_GROUP on the section thus implicitly
applies to its relocation section(s), but it is an error that the
reloc sections aren't part of the group.

Like many patches to gas, this wasn't as straightforward as it could
be due to a number of backends, i386, cr16 and others, removing relocs
in tc_get_reloc rather than marking them as "done" earlier in
md_apply_reloc.  So it isn't possible for the group support to
reliably detect the presence of relocs by looking at fixups earlier
than write_relocs.  However the group support needs to create
signature symbols, and that must be done before the symbol table is
frozen, before write_relocs.  So split off the group sizing from
elf_adjust_symtab and put it in elf_frob_file_after_relocs.

bfd/
	PR 21167
	* elf.c (_bfd_elf_setup_sections): Don't trim reloc sections from
	groups.
	(_bfd_elf_init_reloc_shdr): Pass sec_hdr, use it to copy SHF_GROUP
	flag from section.
	(elf_fake_sections): Adjust calls.  Exit immediately on failure.
	(bfd_elf_set_group_contents): Add associated reloc section indices
	to group contents
gas/
	PR 21167
	* config/obj-elf.c (struct group_list): Delete elt_count.
	(groups): New static.
	(build_group_lists): Don't count elements.
	(elf_adjust_symtab): Use groups rather than auto list.  Set up
	pointer from group member to SHT_GROUP section.  Don't size
	SHT_GROUP section or clean up here..
	(elf_frob_file_after_relocs): ..do so here instead.
	* testsuite/gas/arc/jli-1.d,
	* testsuite/gas/elf/groupautob.d,
	* testsuite/gas/mips/compact-eh-eb-2.d,
	* testsuite/gas/mips/compact-eh-eb-5.d,
	* testsuite/gas/mips/compact-eh-el-2.d,
	* testsuite/gas/mips/compact-eh-el-5.d: Adjust.
ld/
	PR 21167
	* testsuite/ld-elf/group9b.d: Adjust for relocs included in group.
2017-10-05 08:38:11 +10:30
Alan Modra bd61e13549 PR22166, SHT_GNU_verneed memory allocation
The sanity check covers the previous minimim size, plus that the size
is at least enough for sh_info verneed entries.

Also, since we write all verneed fields or exit with an error, there
isn't any need to zero the memory allocated for verneed entries.

	PR 22166
	* elf.c (_bfd_elf_slurp_version_tables): Test sh_info on
	SHT_GNU_verneed section for sanity.  Don't zalloc memory for
	verref.
2017-09-24 14:34:57 +09:30
Andreas Arnez 8fe09d7421 S/390: Fix Elf note swap s390_gs_bc vs. s390_gs_cb
Fix two typos that resulted in swapping the BFD names for the core note
register sections NT_S390_GS_CB and NT_S390_GS_BC.

bfd/ChangeLog:

	* elf.c (elfcore_grok_note): For the cases NT_S390_GS_CB and
	NT_S390_GS_BC, correct the previously swapped invocations of
	elfcore_grok_s390_gs_bc and elfcore_grok_s390_gs_cb.
2017-09-21 17:45:18 +02:00
Nick Clifton 0c54f69295 Fix null pointer dereference when parsing a corrupt ELF binary.
PR 21957
	* elf.c (setup_group): Check for an empty or very small group
	section.
	* po/bfd.pot: Regenerate.
2017-08-14 12:09:36 +01:00
Nick Clifton 957e1fc1c5 Fix out of bounds memory access when trying to allocate space for a note of size -1.
PR 21933
	* elf.c (elf_read_notes): Check for a note size of -1.
2017-08-10 09:37:36 +01:00
Nick Clifton 24d3e51bf0 Fix address violation problems when parsing corrupt ELF binaries.
PR 21916
	* elf-attrs.c (_bfd_elf_parse_attributes): Complain about very
	small section lengths.
	* elf.c (_bfd_elf_setup_sections): Skip empty entries in the group
	table.
	(elfcore_grok_freebsd_prstatus): Add checks to make sure that
	there is enough data present in the note.
2017-08-08 13:20:02 +01:00
Yuri Chornovian de194d8575 Fix spelling typos. 2017-07-18 16:58:14 +01:00
Alan Modra d42c267e9b bfd_error_handler bfd_vma and bfd_size_type args
This patch uses the new %L _bfd_error_handler support for printing
bfd_vma arguments, and fixes a many other format and/or argument
errors in error messages.

bfd/
	* binary.c (binary_set_section_contents): Don't print filepos in
	error message.
	(coff_write_object_contents): Cast size_t for error message.
	(coff_slurp_line_table): Don't use bfd_vma symndx.
	(coff_slurp_reloc_table): Remove unneeded cast.
	* dwarf2.c (read_section): Cast bfd_int64_t to long long for
	error message.
	(find_abstract_instance_name): Likewise.
	* elf32-arm.c (arm_type_of_stub): Correct error arg order.
	(bfd_elf32_arm_stm32l4xx_erratum_scan): Don't cast error arg.
	(elf32_arm_check_relocs): Make r_symndx an int.
	* elf32-cris.c (cris_elf_check_relocs): Delete extraneous %s in
	format string.
	* elf32-metag.c (elf_metag_relocate_section): Delete extra error
	message arg.
	* elf32-nds32.c (nds32_elf_ex9_build_hash_table): Rewrite bogus
	error message.
	* elf32-i386.c (elf_i386_check_relocs): Make r_symndx an int.
	* elf32-s390.c (elf_s390_check_relocs): Likewise.
	* elf32-tic6x.c (elf32_tic6x_check_relocs): Likewise.
	* elf32-tilepro.c (tilepro_elf_check_relocs): Likewise.
	* elf32-xtensa.c (elf_xtensa_check_relocs): Likewise.
	* elf64-s390.c (elf_s390_check_relocs): Likewise.
	* elf64-x86-64.c (elf_x86_64_check_relocs): Likewise.
	* elfnn-aarch64.c (elfNN_aarch64_check_relocs): Likewise.
	* elfnn-riscv.c (riscv_elf_check_relocs): Likewise.
	* elfxx-sparc.c (_bfd_sparc_elf_check_relocs): Likewise.
	* elfxx-tilegx.c (tilegx_elf_check_relocs): Likewise.
	* elf64-mmix.c (_bfd_mmix_after_linker_allocation): Cast size_t args
	and use %lu for error message.
	* elflink.c (elf_link_adjust_relocs): Delete extra error message arg.
	* mmo.c (mmo_scan): Make stab_loc a file_ptr.  Cast expression for
	error message.

	* elf32-arm.c (elf32_arm_tls_relax): Correct format string and args
	in error message.
	(elf32_arm_final_link_relocate): Likewise.
	* coff-arm.c (bfd_arm_process_before_allocation): Likewise.
	* coffcode.h (styp_to_sec_flags): Likewise.
	* cofflink.c (_bfd_coff_write_global_sym): Likewise.
	* ecoff.c (_bfd_ecoff_slurp_symbol_table): Likewise.
	* elf32-arc.c (arc_elf_merge_private_bfd_data): Likewise.
	* elf32-bfin.c (bfinfdpic_check_relocs): Likewise.
	(elf32_bfin_merge_private_bfd_data): Likewise.
	* elf32-cris.c (cris_elf_relocate_section): Likewise.
	* elf32-frv.c (frv_elf_merge_private_bfd_data): Likewise.
	* elf32-i370.c (i370_elf_merge_private_bfd_data): Likewise.
	(i370_elf_relocate_section): Likewise.
	* elf32-iq2000.c (iq2000_elf_merge_private_bfd_data): Likewise.
	* elf32-m32c.c (m32c_elf_merge_private_bfd_data): Likewise.
	* elf32-m68hc1x.c (_bfd_m68hc11_elf_merge_private_bfd_data): Likewise.
	* elf32-mcore.c (mcore_elf_relocate_section): Likewise.
	* elf32-mep.c (mep_elf_merge_private_bfd_data): Likewise.
	* elf32-mt.c (mt_elf_merge_private_bfd_data): Likewise.
	* elf64-sparc.c (elf64_sparc_merge_private_bfd_data): Likewise.
	* elfxx-mips.c (mips_elf_merge_obj_e_flags): Likewise.
	(_bfd_mips_elf_merge_private_bfd_data): Likewise.
	* ieee.c (ieee_write_id, read_id): Likewise.
	* mach-o.c (bfd_mach_o_write_contents): Likewise.
	(bfd_mach_o_layout_commands, bfd_mach_o_read_section_32): Likewise.
	(bfd_mach_o_read_section_64, bfd_mach_o_read_symtab_symbol): Likewise.
	(bfd_mach_o_read_command, bfd_mach_o_header_p): Likewise.
	* peXXigen.c (_bfd_XXi_swap_aouthdr_in): Likewise.
	* stabs.c (_bfd_link_section_stabs): Likewise.

	* coff-arm.c (coff_arm_relocate_section): Use L modifier in error
	format.
	* coff-mcore.c (coff_mcore_relocate_section): Likewise.
	* coff-ppc.c (coff_ppc_relocate_section): Likewise.
	* coff-rs6000.c (xcoff_reloc_type_toc): Likewise.
	* coff-sh.c (sh_relax_section): Likewise.
	(sh_relax_delete_bytes, sh_swap_insns): Likewise.
	* coff-tic80.c (coff_tic80_relocate_section): Likewise.
	* coffcode.h (coff_slurp_reloc_table): Likewise.
	* coffgen.c (_bfd_coff_get_external_symbols): Likewise.
	(_bfd_coff_read_string_table): Likewise.
	* cofflink.c (_bfd_coff_generic_relocate_section): Likewise.
	* compress.c (bfd_get_full_section_contents): Likewise.
	* dwarf2.c (read_formatted_entries, decode_line_info): Likewise.
	* elf-m10300.c (mn10300_elf_relocate_section): Likewise.
	* elf.c (bfd_elf_string_from_elf_section): Likewise.
	* elf32-arc.c (arc_special_overflow_checks): Likewise.
	* elf32-arm.c (elf32_arm_tls_relax): Likewise.
	(elf32_arm_final_link_relocate, elf32_arm_relocate_section): Likewise.
	(elf32_arm_write_section): Likewise.
	* elf32-bfin.c (bfin_relocate_section): Likewise.
	(bfinfdpic_relocate_section): Likewise.
	* elf32-hppa.c (hppa_build_one_stub): Likewise.
	(final_link_relocate, elf32_hppa_relocate_section): Likewise.
	* elf32-i386.c (elf_i386_tls_transition): Likewise.
	(elf_i386_relocate_section): Likewise.
	* elf32-ip2k.c (ip2k_final_link_relocate): Likewise.
	* elf32-lm32.c (lm32_elf_finish_dynamic_sections): Likewise.
	* elf32-m32r.c (m32r_elf_relocate_section): Likewise.
	* elf32-m68k.c (elf_m68k_relocate_section): Likewise.
	* elf32-metag.c (elf_metag_relocate_section): Likewise.
	* elf32-nds32.c (unrecognized_reloc_msg): Likewise.
	(nds32_elf_relax_longcall1, nds32_elf_relax_longcall2): Likewise.
	(nds32_elf_relax_longcall3, nds32_elf_relax_longjump1): Likewise.
	(nds32_elf_relax_longjump2, nds32_elf_relax_longjump3): Likewise.
	(nds32_elf_relax_longcall4, nds32_elf_relax_longcall5): Likewise.
	(nds32_elf_relax_longcall6, nds32_elf_relax_longjump4): Likewise.
	(nds32_elf_relax_longjump5, nds32_elf_relax_longjump6): Likewise.
	(nds32_elf_relax_longjump7, nds32_elf_relax_loadstore): Likewise.
	(nds32_elf_relax_ptr, nds32_elf_ex9_build_hash_table): Likewise.
	* elf32-nios2.c (nios2_elf32_relocate_section): Likewise.
	* elf32-rx.c (UNSAFE_FOR_PID): Likewise.
	* elf32-s390.c (invalid_tls_insn, elf_s390_relocate_section): Likewise.
	* elf32-score.c (s3_bfd_score_elf_check_relocs): Likewise.
	* elf32-score7.c (s7_bfd_score_elf_check_relocs): Likewise.
	* elf32-sh.c (sh_elf_relax_section): Likewise.
	(sh_elf_relax_delete_bytes, sh_elf_swap_insns): Likewise.
	(sh_elf_relocate_section): Likewise.
	* elf32-sh64.c (shmedia_prepare_reloc): Likewise.
	* elf32-spu.c (spu_elf_relocate_section): Likewise.
	* elf32-tic6x.c (elf32_tic6x_relocate_section): Likewise.
	* elf32-tilepro.c (tilepro_elf_relocate_section): Likewise.
	* elf32-v850.c (v850_elf_relax_section): Likewise.
	* elf32-vax.c (elf_vax_check_relocs): Likewise.
	(elf_vax_relocate_section): Likewise.
	* elf32-xtensa.c (elf_xtensa_relocate_section): Likewise.
	(extend_ebb_bounds_forward, extend_ebb_bounds_backward): Likewise.
	(compute_text_actions, compute_ebb_proposed_actions): Likewise.
	(do_fix_for_relocatable_link): Likewise.
	* elf64-alpha.c (elf64_alpha_relax_got_load): Likewise.
	(elf64_alpha_relax_with_lituse): Likewise.
	* elf64-hppa.c (elf64_hppa_finish_dynamic_symbol): Likewise.
	(elf_hppa_final_link_relocate): Likewise.
	* elf64-ia64-vms.c (elf64_ia64_relax_section): Likewise.
	(elf64_ia64_choose_gp, elf64_ia64_relocate_section): Likewise.
	(elf64_vms_link_add_object_symbols): Likewise.
	* elf64-mmix.c (mmix_elf_perform_relocation): Likewise.
	(mmix_final_link_relocate): Likewise.
	* elf64-s390.c (invalid_tls_insn): Likewise.
	(elf_s390_relocate_section): Likewise.
	* elf64-sh64.c (sh_elf64_relocate_section): Likewise.
	* elf64-x86-64.c (elf_x86_64_tls_transition): Likewise.
	(elf_x86_64_relocate_section): Likewise.
	* elfcode.h (elf_slurp_symbol_table): Likewise.
	* elfcore.h (elf_core_file_p): Likewise.
	* elflink.c (elf_link_read_relocs_from_section): Likewise.
	* elfnn-aarch64.c (elfNN_aarch64_final_link_relocate): Likewise.
	(elfNN_aarch64_relocate_section): Likewise.
	* elfnn-ia64.c (elfNN_ia64_relax_section): Likewise.
	(elfNN_ia64_choose_gp, elfNN_ia64_relocate_section): Likewise.
	* elfnn-riscv.c (riscv_elf_relocate_section): Likewise.
	* elfxx-mips.c (_bfd_mips_elf_check_relocs): Likewise.
	(_bfd_mips_elf_relocate_section): Likewise.
	(_bfd_mips_elf_finish_dynamic_symbol, mips_finish_exec_plt): Likewise.
	* elfxx-sparc.c (_bfd_sparc_elf_relocate_section): Likewise.
	* elfxx-tilegx.c (tilegx_elf_relocate_section): Likewise.
	* ieee.c (ieee_slurp_external_symbols): Likewise.
	* ihex.c (ihex_write_object_content): Likewise.
	* mach-o.c (bfd_mach_o_build_exec_seg_command): Likewise.
	* merge.c (_bfd_merged_section_offset): Likewise.
	* mmo.c (mmo_write_loc_chunk): Likewise.
	(mmo_write_object_contents): Likewise.
	* peXXigen.c (_bfd_XX_bfd_copy_private_bfd_data_common): Likewise.
	* stabs.c (_bfd_link_section_stabs): Likewise.
	* xcofflink.c (xcoff_link_add_symbols, xcoff_find_tc0): Likewise.
ld/
	* testsuite/ld-arc/nps-1b.err: Update.
	* testsuite/ld-x86-64/ilp32-11.d: Update.
2017-07-12 14:03:48 +09:30
John Baldwin e6f3b9c319 Create pseudo sections for FreeBSD NT_PTLWPINFO core notes.
bfd/ChangeLog:

	* elf.c (elfcore_grok_freebsd_note): Handle NT_FREEBSD_PTLWPINFO.
2017-07-07 16:11:42 -07:00
Alan Modra 76cfced5ad Support %Lx, %Lu, %Ld in _bfd_error_handler format
One way to print 64-bit bfd_vma or bfd_size_type values on 32-bit
hosts is to cast the value to long long and use the 'll' modifier in
printf format strings.  However, that's awkward because we also
support the Microsoft C library printf that uses 'I64' as a modifier
instead, and having variants of translated strings would not endear us
to the translation project.  So, rewrite the 'll' modifier in
_doprint for Microsoft.  Even with that capability it's not so nice
for 32-bit code to need casts to long long, so this patch makes 'L' a
modifier for bfd_vma rather than an alias for 'll'.

I've then used the new 'L' modifier to fix selected format strings.

	* bfd.c (_doprnt): Rewrite "ll" and "L" modifiers to "I64" for
	__MSVCRT__.  Support "L" modifier for bfd_vma.  Formatting.
	* elf.c (setup_group): Use "Lx" to print sh_size.
	(_bfd_elf_setup_sections): Remove unnecessary cast and print
	unknown section type in hex.
	(copy_special_section_fields): Style fix.
	(bfd_section_from_shdr): Correct format for sh_link.  Use a
	common error message for all the variants of unrecognized
	section types.
	(assign_file_positions_for_load_sections): Use "Lx" for lma
	adjust error message.
	(assign_file_positions_for_non_load_sections): Formatting.
	(rewrite_elf_program_header): Formatting.  Use "Lx" for
	bfd_vma values in error messages.
	* elfcode.h (elf_slurp_reloc_table_from_section): Cast
	ELF_R_SYM value to type expected by format.
	* elflink.c (elf_link_read_relocs_from_section): Use "Lx"
	in error messages.
	(elf_link_add_object_symbols): Use "Lu" for symbol sizes.
	(elf_link_input_bfd): Use "Lx" for r_info.
	(bfd_elf_gc_record_vtinherit): Use "Lx" for offset.
2017-07-03 22:03:49 +09:30
Alan Modra cd9af601e6 Miscellaneous format string fixes
* elf.c (_bfd_elf_print_private_bfd_data): Use BFD_VMA_FMT to
	print d_tag.
	(bfd_elf_print_symbol): Don't cast symbol->flags.
	(_bfd_elf_symbol_from_bfd_symbol): Likewise.
	* elf32-ppc.c (ppc_elf_begin_write_processing): Correct
	_bfd_error_handler argument order.
	(ppc_elf_merge_private_bfd_data): Don't cast flags.
2017-07-03 22:03:28 +09:30
Andreas Arnez 88ab90e860 S390: Support guarded-storage core note sections
Newer Linux kernel versions offer two new register sets in support of the
z/Architecture's guarded storage facility: NT_S390_GS_CB, the
"guarded-storage registers", and NT_S390_GS_BC, the "guarded-storage
broadcast control block".  This patch adds support for the respective core
notes sections to binutils.

bfd/ChangeLog:
	* elf-bfd.h (elfcore_write_s390_gs_cb): Add prototype.
	(elfcore_write_s390_gs_bc): Likewise.
	* elf.c (elfcore_grok_s390_gs_cb): New function.
	(elfcore_grok_s390_gs_bc): New function.
	(elfcore_grok_note): Call them.
	(elfcore_write_s390_gs_cb): New function.
	(elfcore_write_s390_gs_bc): New function.
	(elfcore_write_register_note): Call them.

binutils/ChangeLog:
	* readelf.c (get_note_type): Add NT_S390_GS_CB and NT_S390_GS_BC.

include/ChangeLog:
	* elf/common.h (NT_S390_GS_CB): New macro.
	(NT_S390_GS_BC): Likewise.
2017-06-29 10:30:09 +02:00
Sergei Trofimovich 5cc4ca837d fix out-of-bounds access in elf.c:find_link
The out-of-bounds access is reproducible on 'ia64-strip' command
(see sample from https://bugs.gentoo.org/show_bug.cgi?id=622500)

The output file contains less section than original one.
This tricks 'hint' access to go out-of-bounds:

	* elf.c (find_link): Bounds check "hint".
2017-06-25 10:29:57 +09:30
Nick Clifton ce49701009 Fix seg-fault reading a corrupt ELF binary.
PR binutils/21640
	* elf.c (setup_group): Zero the group section pointer list after
	allocation so that loops can be caught.  Check for NULL pointers
	when processing a group list.
2017-06-21 10:36:58 +01:00
Jose E. Marchesi 78e8a2ff5f bfd: support section groups with preceding SHF_GROUP sections
GAS always places section groups (SHT_GROUP) before the rest of the
sections in the output file.  However, other assemblers may place
section groups after the group members.

This patch fixes handlign such situations, and removes some duplicated
logic.

bfd/ChangeLog:

2017-06-06  Jose E. Marchesi  <jose.marchesi@oracle.com>

	* elf.c (setup_group): Make sure BFD sections are created for all
	group sections in the input file when processing SHF_GROUP
	sections.
	(bfd_section_from_shdr): Avoid duplicating logic already
	implemented in `setup_group'.
2017-06-06 11:19:06 -07:00
Andrew Burgess 7bdf4127c3 ld: Allow section groups to be resolved as part of a relocatable link
This commit adds a new linker feature: the ability to resolve section
groups as part of a relocatable link.

Currently section groups are automatically resolved when performing a
final link, and are carried through when performing a relocatable link.
By carried through this means that one copy of each section group (from
all the copies that might be found in all the input files) is placed
into the output file.  Sections that are part of a section group will
not match input section specifiers within a linker script and are
forcibly kept as separate sections.

There is a slight resemblance between section groups and common
section.  Like section groups, common sections are carried through when
performing a relocatable link, and resolved (allocated actual space)
only at final link time.

However, with common sections there is an ability to force the linker to
allocate space for the common sections when performing a relocatable
link, there's currently no such ability for section groups.

This commit adds such a mechanism.  This new facility can be accessed in
two ways, first there's a command line switch --force-group-allocation,
second, there's a new linker script command FORCE_GROUP_ALLOCATION.  If
one of these is used when performing a relocatable link then the linker
will resolve the section groups as though it were performing a final
link, the section group will be deleted, and the members of the group
will be placed like normal input sections.  If there are multiple copies
of the group (from multiple input files) then only one copy of the group
members will be placed, the duplicate copies will be discarded.

Unlike common sections that have the --no-define-common command line
flag, and INHIBIT_COMMON_ALLOCATION linker script command there is no
way to prevent group resolution during a final link, this is because the
ELF gABI specifically prohibits the presence of SHT_GROUP sections in a
fully linked executable.  However, the code as written should make
adding such a feature trivial, setting the new resolve_section_groups
flag to false during a final link should work as you'd expect.

bfd/ChangeLog:

	* elf.c (_bfd_elf_make_section_from_shdr): Don't initially mark
	SEC_GROUP sections as SEC_EXCLUDE.
	(bfd_elf_set_group_contents): Replace use of abort with an assert.
	(assign_section_numbers): Use resolve_section_groups flag instead
	of relocatable link type.
	(_bfd_elf_init_private_section_data): Use resolve_section_groups
	flag instead of checking the final_link flag for part of the
	checks in here.  Fix white space as a result.
	* elflink.c (elf_link_input_bfd): Use resolve_section_groups flag
	instead of relocatable link type.
	(bfd_elf_final_link): Likewise.

include/ChangeLog:

	* bfdlink.h (struct bfd_link_info): Add new resolve_section_groups
	flag.

ld/ChangeLog:

	* ld.h (struct args_type): Add force_group_allocation field.
	* ldgram.y: Add support for FORCE_GROUP_ALLOCATION.
	* ldlex.h: Likewise.
	* ldlex.l: Likewise.
	* lexsup.c: Likewise.
	* ldlang.c (unique_section_p): Check resolve_section_groups flag
	not the relaxable link flag.
	(lang_add_section): Discard section groups when we're resolving
	groups.  Clear the SEC_LINK_ONCE flag if we're resolving section
	groups.
	* ldmain.c (main): Initialise resolve_section_groups flag in
	link_info based on command line flags.
	* testsuite/ld-elf/group11.d: New file.
	* testsuite/ld-elf/group12.d: New file.
	* testsuite/ld-elf/group12.ld: New file.
	* NEWS: Mention new features.
	* ld.texinfo (Options): Document --force-group-allocation.
	(Miscellaneous Commands): Document FORCE_GROUP_ALLOCATION.
2017-06-06 09:53:38 +01:00