Commit Graph

337 Commits

Author SHA1 Message Date
Alan Modra
daafebb58d COFF swap_aux_in
A low level function like coff_swap_aux_in really has no business
concatenating multiple auxents for the old PE multi-aux scheme of
handling long file names.  In doing so, it assumes multiple internal
auxent buffers are available, which they are not in most calls to
bfd_coff_swap_aux_in, both inside BFD and outside, eg. GDB.  Buffer
overflow fun.  Concatenating multiple auxents belongs at a higher
level.

This required some changes to coff_get_normalized_symtab, which now
uses the external auxents to access the concatenated file name.
(Internal auxents are larger than the x_fname array, so the pieces of
the file name are not adjacent as they are in the external auxents.)

	* coffswap.h (coff_swap_aux_in): Do not write more than one
	internal auxent.
	* coffcode.h (coff_bigobj_swap_aux_in): Likewise.
	* coffgen.c (coff_get_normalized_symtab): Normalize strings
	after swapping in each symbol so that external auxents are
	available.  Use external auxents for multi-aux long file
	names.  Formatting.  Wrap long lines.  Remove excess parens
	and unnecessary casts.  Don't zalloc when only the string
	terminator needs zeroing, and memcpy rather than strncpy.
	Delete unnecessary sanity check with unsigned _n_offset.
	Return with failure if debug section can't be read, to avoid
	trying to read it multiple times.  Correct sanity check
	against debug section size.
2023-08-28 23:10:57 +09:30
Alan Modra
54d57acf61 Re: comdat_hash memory leaks
I missed another field that needs freeing.  Also, oss-fuzz found a
case with a C_FILE sym using multiple auxents for a long file name
which overflowed the single auxent buffer.  I'm going to fix that
problem in swap_aux_in too, but we may as well avoid it here too,
saving unnecessary work.

	* coffcode.h (comdat_delf): Free comdat_name.
	(fill_comdat_hash): Only look at symbols with one auxent.
2023-08-28 22:05:29 +09:30
Alan Modra
8606b47e94 comdat_hash memory leaks
Entries added to the hash table with bfd_malloc ought to be freed when
the hash table is deleted.  This patch adds the necessary del_f to the
htab_create call, and delays creating the table until an
IMAGE_SCN_LNK_COMDAT symbol is read.

	* peicode.h (pe_mkobject): Move comdat_hash creation..
	(htab_hash_flags, htab_eq_flags): ..and these support functions..
	* coffcode.h (handle_COMDAT): ..to here, renaming support to
	(comdat_hashf, comdat_eqf): ..this and adding..
	(comdat_delf): ..this new function.
2023-08-27 13:57:42 +09:30
Alan Modra
398f1ddf5e sanity check n_numaux
Sanity check aux entries used by PE to extend a C_FILE name.  See
coffswap.h:coff_swap_aux_in.  The existing check only catered for
n_numaux == 1.

	* coffcode.h (fill_comdat_hash): Properly sanity check n_numaux.
	Formatting.
	(handle_COMDAT): Formatting.
2023-08-27 13:57:41 +09:30
Jan Beulich
67694446f7 bfd: correct relocation handling for objcopy COFF -> ELF
While documented to not be reliable, it is still odd for objcopy to
silently produce bad output when converting COFF/PE object files to ELF
ones. The issue there is that relocation addends all are screwed up by
subtracting the symbol's section offset. In the COFF/PE world, to my
knowledge, section contents stores the addends alone, not the result of
symbol value plus addend. Hence the compensation talked about in a
comment ahead of the sole use site of CALC_ADDEND() may need to account
for the VMA (which is always zero for object files anyway), but not for
the symbol value.

The coff-sh.c adjustment is based upon guessing that behavior there is
the same. Note also how coff-aarch64.c short-circuits CALC_ADDEND()
altogether, which may suggest that a much simpler macro might do for the
COFF_WITH_PE case in the three arch-specific files touched here.

For (at least) Arm/WinCE this actually results in more appropriate
objdump output as well, as can be seen in the one testcase which has its
expectations adjusted (the generated binary doesn't change).
2023-08-25 14:56:44 +02:00
Oleg Tolmatcev
6aadf8a04d optimize handle_COMDAT
Signed-off-by: Oleg Tolmatcev <oleg.tolmatcev@gmail.com>
2023-08-24 15:54:39 +09:30
Alan Modra
e416bd75c3 warn unused result for bfd IO functions
This patch fixes all the warnings I found in bfd, binutils and ld,
plus some bitrotted COFF_GO32 code that tried to allocate -168ul
bytes.  When the malloc fail was reported these testsuite fails
resulted:

i386-go32  +FAIL: go32 stub
i386-go32  +ERROR: tcl error sourcing /home/alan/src/binutils-gdb/ld/testsuite/ld-i386/i386.exp.
i386-go32  +ERROR: couldn't open "tmpdir/go32stub": no such file or directory
i386-go32  +FAIL: ld-scripts/sane1
i386-go32  +FAIL: ld-scripts/assign-loc
i386-go32  +FAIL: ld-scripts/pr18963

This does result in some warnings in gdb which are fixed in a followup
patch.

bfd/
	* bfdio.c (bfd_read, bfd_write): Add ATTRIBUTE_WARN_UNUSED_RESULT.
	(bfd_tell, bfd_stat, bfd_seek, bfd_mmap): Likewise.
	* bfd-in2.h: Regenerate.
	* coff-rs6000.c (xcoff_write_armap_big) Don't ignore bfd_write
	return value.
	(xcoff_generate_rtinit): Likewise.  Also free data_buffer and
	string_table before returning.
	* coff64-rs6000.c (xcoff64_generate_rtinit): Likewise.
	* coff-stgo32.c (go32exe_check_format): Don't ignore bfd_seek
	return value.
	* coffcode.h (coff_apply_checksum): Don't ignore bfd_write return.
	(coff_write_object_contents <COFF_GO32>): Likewise, and bfd_malloc.
	Fix bitrotted code to look for first section with non-zero filepos.
	* elf64-ia64-vms.c (elf64_vms_write_shdrs_and_ehdr): Don't ignore
	bfd_seek or bfd_write return values.
	* pef.c (bfd_pef_scan_section): Likewise.
	(bfd_pef_read_header, bfd_pef_xlib_read_header): Likewise.
	* vms-misc.c (_bfd_vms_output_end): Likewise.  Return status.
	* vms.h (_bfd_vms_output_end): Update prototype.
	* vms-alpha.c: Pass _bfd_vms_output_end status up call chains.
	* wasm-module.c (wasm_compute_custom_section_file_position): Don't
	ignore bfd_seek or bfd_write return values.
	(wasm_compute_section_file_positions): Likewise.
	* xsym.c (bfd_sym_scan): Don't ignore bfd_seek return value.
	(bfd_sym_read_name_table): Likewise.
binutils/
	* ar.c (print_contents, extract_file): Don't ignore bfd_seek
	return value.
ld/
	* pdb.c (create_section_contrib_substream): Don't ignore bfd_seek
	return value.
	(create_section_header_stream): Likewise.
	* pe-dll.c (pe_get16, pe_get32): Add fail param to return results
	from bfd_seek and bfd_read.
	(pe_implied_import_dll): Handle these fails, and other bfd_seek
	and bfd_read return values.
2023-08-11 16:36:11 +09:30
Alan Modra
226f9f4fad Rename bfd_bread and bfd_bwrite
These were renamed from bfd_read and bfd_write back in 2001 when they
lost an unnecessary parameter.  Rename them back, and get rid of a few
casts that are only needed without prototyped functions (K&R C).
2023-08-09 08:48:09 +09:30
Sam James
b5c37946cc Revert "2.41 Release sources"
This reverts commit 675b9d612c.

See https://sourceware.org/pipermail/binutils/2023-August/128761.html.
2023-08-02 12:06:23 +01:00
Nick Clifton
675b9d612c 2.41 Release sources 2023-08-02 09:23:36 +01:00
Alan Modra
41c8b9f097 coff/pe/xcoff and --extract-symbols
This fixes failure of the "extract symbols" test for rs6000, where
--extract-symbols generates a non-zero sized .text.  By the look of
coffcode.h the same problem might occur for coff/pe too, but doesn't
happen to trigger a test failure.

bfd/
	* coffcode.h (coff_compute_section_file_positions): Don't
	adjust size of !SEC_LOAD sections.
binutils/
	* objcopy.c (setup_section): Clear SEC_LOAD for --extract-symbol.
2023-07-28 14:11:27 +09:30
Alan Modra
478409b71d AIX_WEAK_SUPPORT
Making target code depend on a host define like _AIX52 is never
correct, so out it goes.  Also, sort some config.bfd entries a little
to make it more obvious there is a config difference between aix5.1
and aix5.2.  These two changes should make no difference to anything
in binutils.  The gas define of AIX_WEAK_SUPPORT on the other hand was
wrong, so fix that.  Finally, fix some testsuite fails on aix < 5.2 by
simply not running the tests.

include/
	* coff/internal.h (C_WEAKEXT): Don't depend on _AIX52.
bfd/
	* coffcode.h (coff_slurp_symbol_table): Don't depend on _AIX52.
	(coff_classify_symbol): Likewise.
	* config.bfd: Sort some entries.
gas/
	* configure.ac (AIX_WEAK_SUPPORT): Don't set for aix5.[01].
	* configure: Regenerate.
	* testsuite/gas/ppc/aix.exp (xcoff-visibility-1*) Don't run
	for aix < 5.2.
2023-07-14 11:43:52 +09:30
Alan Modra
ba75d1c55c _bfd_free_cached_info
doc/bfdint.texi and comments in the aout and som code about this
function are just wrong, and its name is not very apt.  Better would
be _bfd_mostly_destroy, and we certainly should not be saying anything
about the possibility of later recreating anything lost by this
function.  What's more, if _bfd_free_cached_info is called when
creating an archive map to reduce memory usage by throwing away
symbols, the target _close_and_cleanup function won't have access to
tdata or section bfd_user_data to tidy memory.  This means most of the
target _close_and_cleanup function won't do anything, and therefore
sometimes will result in memory leaks.

This patch fixes the documentation problems and moves most of the
target _close_and_cleanup code to target _bfd_free_cached_info.
Another notable change is that bfd_generic_bfd_free_cached_info is now
defined as _bfd_free_cached_info rather than _bfd_bool_bfd_true,
ie. the default now frees objalloc memory.
2023-06-07 14:16:06 +09:30
Tristan Gingold
768d1d879b pe/coff - add support for base64 encoded long section names
PR 30444
  * coffcode.h (coff_write_object_contents): Handle base64 encoding on PE.  Also check for too large string table.
  * coffgen.c (extract_long_section_name): New function extracted from ... (make_a_section_from_file): ... here.  Add support for base64 long section names. (decode_base64): New function.
2023-05-31 11:20:55 +01:00
Alan Modra
cb3f0ff479 coffcode.h handle_COMDAT tidy
I started down the path of attempting to fix
https://sourceware.org/pipermail/binutils/2023-April/127263.html but
decided after a while that I didn't want to mess with this code..

This patch is a just a few things that I thought worth doing, the main
one being reporting of errors up the call chain.  The while loop to
for loop change is shamelessly stolen from Oleg.

	* coffcode.h (handle_COMDAT): Return bool.  Make sec_flags a
	flagword*, and adjust to suit.  Replace while loop with for
	loop.  Check isym.n_numaux before reading aux entries.  Alloc
	coff_comdat_info and name in one call to bfd_alloc.  Remove
	goto breakloop.
	(styp_to_sec_flags): Adjust handle_COMDAT call.
2023-05-20 10:49:19 +09:30
Alan Modra
880853ed94 tic54x set_arch_mach
The tic54x backend provides its own coff_set_arch_mach, but wants to
use the standard coff_set_section_contents.  BFD_JUMP_TABLE_WRITE
defines both of these functions, so the code also provides a wrapper
for coff_set_section_contents.  This is all quite OK, but I was on a
mission to remove unnecessary declarations in coffcode.h, and on
deleting the one for coff_set_arch_mach ran into a warning about the
function being unused.  I could have kept that declaration with its
ATTRIBUTE_UNUSED or written "static bool ATTRIBUTE_UNUSED" on the
definition but the latter is not usual and looks odd to me.  So I
had a closer look at tic54x_set_arch_mach and decided the function is
very likely wrong to allow bfd_arch_unknown.  Thus the backend should
be using the standard coff_set_arch_mach.

	* coff-tic54x.c: Use BFD_JUMP_TABLE_WRITE (coff) in target vecs.
	(tic54x_coff_set_arch_mach): Delete.
	(tic54x_set_section_contents): Delete.
	* coffcode.h: Delete unnecessary forward declarations.
2023-05-20 10:49:19 +09:30
Alan Modra
204d627e83 Re: Add section caches to coff_data_type
Another thing, section target_index is renumbered in
coff_compute_section_file_positions and _bfd_xcoff_bfd_final_link.  I
don't know that there is currently any way that the output bfd
section_by_target_index could be populated before this point but
clear them out so no one need worry about it.

	* coffcode.h (coff_compute_section_file_positions): Clear
	section_by_target_index hash table when changing target_index.
	(_bfd_xcoff_bfd_final_link): Likewise.
2023-05-18 18:01:49 +09:30
Alan Modra
0cc8cc5e6f Re: Add section caches to coff_data_type
Commit 0e759f232b regressed these tests:
rs6000-aix7.2  +FAIL: Garbage collection test 1 (32-bit)
rs6000-aix7.2  +FAIL: Garbage collection test 1 (64-bit)
rs6000-aix7.2  +FAIL: Glink test 1 (32-bit)
rs6000-aix7.2  +FAIL: Glink test 1 (64-bit)

Investigation showed segfaults in coff_section_from_bfd_index called
by xcoff_write_global_symbol due to the hash table pointer being
NULL.  Well, yes, the hash table isn't initialised for the output bfd.
mkobject_hook is the wrong place to do that.

	* coffcode.h: Revert 0e759f232b changes.
	* peicode.h: Likewise.
	* coff-x86_64.c (htab_hash_section_index, htab_eq_section_index):
	Moved here from coffcode.h.
	(coff_amd64_rtype_to_howto): Create section_by_index htab.
	* coffgen.c (htab_hash_section_target_index),
	(htab_eq_section_target_index): Moved here from coffcode.h.
	(coff_section_from_bfd_index): Create section_by_target_index
	htab.  Stash newly created sections in htab.
2023-05-18 12:21:40 +09:30
Oleg Tolmatcev
0e759f232b Add section caches to coff_data_type
* libcoff-in.h (struct coff_tdata): Add section_by_index and section_by_target_index hash tables.
  * libcoff.h: Regenerate.
  * coffcode.h (htab_hash_section_index): New function. (htab_eq_section_index): New function. (htab_hash_section_target_index): New function. (htab_eq_section_target_index): New function. (coff_mkobject_hool): Create the hash tables.
  * peicode.h: Add the same new functions. (pe_mkobject_hook): Create the hash tables.
  * coff-x86_64.c (coff_amd64_rtype_to_howto): Use the new tables to speed up lookups.
  * coffgen.c (coff_section_from_bfd_index): Likewise. (_bfd_coff_close_and_cleanup): Delete the hash tables.
2023-05-16 14:25:32 +01:00
Oleg Tolmatcev
7e1b588764 Improve the speed of computing checksums for COFF binaries.
* coffcode.h (coff_read_word_from_buffer): New function.
 * coffcode.h (COFF_CHECKSUM_BUFFER_SIZE): New constant.
 * coffcode.h (coff_compute_checksum): Improve speed by reducing the number of seeks and reads used.
2023-05-03 15:36:43 +01:00
Alan Modra
717d4bd6d1 Generated docs and include files
bfd/doc/chew.c extracts documentation from source code comments
annotated with keywords, and generates much of bfd.h and libbfd.h from
those same comments.  The docs have suffered from people (me too)
adding things like CODE_FRAGMENT to the source to put code into bfd.h
without realising that CODE_FRAGMENT also puts @example around said
code into the docs.  So we have random senseless things in the docs.
This patch fixes that problem (well, the senseless things from
CODE_FRAGMENT), moves most of the code out of bfd-in.h, and improves a
few chew.c features.  libbfd.h now automatically gets ATTRIBUTE_HIDDEN
prototypes, and indentation in bfd.h and libbfd.h is better.
2023-05-03 15:00:05 +09:30
Alan Modra
2043ddb218 asan: segfault in coff_mangle_symbols
The testcase managed to trigger creation of a wild pointer in
coff_slurp_symbol_table.  Stop that happening, and fix an unrelated
problem I happened to see in bfd_coff_get_syment.

	* coff-bfd.c (bfd_coff_get_syment): Clear fix_value after
	converting n_value from a pointer to an index.
	* coffcode.h (coff_slurp_symbol_table <C_BSTAT>): Sanity check
	symbol value before converting to a pointer.
2023-04-24 20:59:38 +09:30
Jan Beulich
ae272fb8a5 bfd: fix STRICT_PE_FORMAT build
A semicolon was missing and "name" needs to be pointer-to-const. While
adding "const" there, also add it for "sec".
2023-04-21 12:05:56 +02:00
Alan Modra
a2c7ca15a5 Use stdint types in coff internal_auxent
long is a poor choice of type to store 32-bit values read from
objects files by H_GET_32.  H_GET_32 doesn't sign extend so tests like
that in gdb/coffread.c for "negative" values won't work if long is
larger than 32 bits.  If long is 32-bit then code needs to be careful
to not accidentally index negative array elements.  (I'd rather see a
segfault on an unmapped 4G array index than silently reading bogus
data.)  long is also a poor choice for x_sect.s_scnlen, which might
have 64-bit values.  It's better to use unsigned exact width types to
avoid surprises.

I decided to change the field names too, which makes most of this
patch simply renaming.  Besides that there are a few places where
casts are no longer needed, and where printf format strings or tests
need adjusting.

include/
	* coff/internal.h (union internal_auxent): Use unsigned stdint
	types.  Rename l fields to u32 and u64 as appropriate.
bfd/
	* coff-bfd.c,
	* coff-rs6000.c,
	* coff64-rs6000.c,
	* coffcode.h,
	* coffgen.c,
	* cofflink.c,
	* coffswap.h,
	* peXXigen.c,
	* xcofflink.c: Adjust to suit internal_auxent changes.
binutils/
	* rdcoff.c: Adjust to suit internal_auxent changes.
gas/
	* config/obj-coff.h,
	* config/tc-ppc.c: Adjust to suit internal_auxent changes.
gdb/
	* coffread.c,
	* xcoffread.c: Adjust to suit internal_auxent changes.
ld/
	* pe-dll.c: Adjust to suit internal_auxent changes.
2023-03-27 21:58:46 +10:30
Alan Modra
695c322803 coffgrok access of u.auxent.x_sym.x_tagndx.p
u.auxent.x_sym.x_tagndx is a union.  The p field is only valid when
fix_tag is set.  This patch fixes code in coffgrok.c that accessed the
field without first checking fix_tag, and removes a whole lot of code
validating bogus pointers to prevent segfaults (which no longer
happen, I checked the referenced PR 17512 testcases).  The patch also
documents this in the fix_tag comment, makes is_sym a bitfield, and
sorts the selecter fields a little.

bfd/
	* coffcode.h (combined_entry_type): Make is_sym a bitfield.
	Sort and comment on union selectors.
	* libcoff.h: Regenerate.
binutils/
	* coffgrok.c (do_type): Make aux a combined_entry_type.  Test
	fix_tag before accessing u.auxent.x_sym.x_tagndx.p.  Remove
	now unnecessary pointer bounds checking.
2023-03-27 21:58:46 +10:30
Alan Modra
58c4c6a0bf XCOFF sanity check
* coffcode.h (coff_pointerize_aux_hook): Sanity check
	x_csect.x_scnlen against raw_syment_count.
2023-03-27 21:58:46 +10:30
Alan Modra
d64c8f7181 z8 and z80 coff_reloc16_extra_cases sanity checks
* reloc16.c (bfd_coff_reloc16_get_relocated_section_contents):
	Use size_t variables.  Sanity check reloc address.  Handle
	errors from bfd_coff_reloc16_extra_cases.
	* coffcode.h (_bfd_coff_reloc16_extra_cases): Return bool, take
	size_t* args.
	(dummy_reloc16_extra_cases): Adjust to suit.  Don't abort.
	* coff-z80.c (extra_case): Sanity check reloc address.  Return
	errors.  Tidy formatting.  Use bfd_signed_vma temp var to
	check for reloc overflow.  Don't abort on unexpected reloc type,
	instead print an error and return false.
	* coff-z8k.c (extra_case): Likewise.
	* libcoff.h: Regenerate.
2023-03-08 11:24:50 +10:30
Alan Modra
4a3ab0855d COFF CALC_ADDEND comment
Old COFF (and AOUT) targets have unusual relocation addends.

	* coffcode.h (<Reading relocations>): Describe COFF addends.
2023-01-16 23:25:26 +10:30
Alan Modra
22a95e1a37 Make coff backend data read-only
The bfd_coff_backend_data struct should be read-only, the only thing
preventing this is that objcopy writes to one of the fields,
_bfd_coff_long_section_names.  This patch creates a copy of the field
in bfd coff_obj_tdata, which makes more sense anyway.  When enabling
long section names the intent is to do so for a particular bfd, not
for all bfds that might happen to be using the target xvec.

bfd/
	* coffcode.h: Update coff long section name comment.
	(bfd_coff_set_long_section_names_allowed): Use macro accessor
	to set flag.
	(bfd_coff_set_long_section_names_disallowed): Tidy.
	(coff_backend_info): Return a const pointer.
	(bfd_coff_std_swap_table, ticoff0_swap_table, ticoff1_swap_table),
	(bigobj_swap_table): Make const.
	(bfd_coff_long_section_names): Use tdata copy.
	(coff_mkobject): Set long_section_names from coff_backend_info.
	* coff-go32.c (_bfd_go32_mkobject): Likewise.
	* peicode.h (pe_mkobject): Likewise.
	* coff-sh.c (bfd_coff_small_swap_table): Make const.
	* libcoff-in.h (struct coff_tdata): Add long_section_names,
	reorder fields.
	* libcoff.h: Regenerate.
binutils/
	* objcopy.c (set_long_section_mode): Move earlier in file.
	(copy_object): Call set_long_section_mode here, after setting
	output format.
	(copy_file): Don't call set_long_section_mode.
2023-01-06 21:06:15 +10:30
Alan Modra
d87bef3a7b Update year range in copyright notice of binutils files
The newer update-copyright.py fixes file encoding too, removing cr/lf
on binutils/bfdtest2.c and ld/testsuite/ld-cygwin/exe-export.exp, and
embedded cr in binutils/testsuite/binutils-all/ar.exp string match.
2023-01-01 21:50:11 +10:30
Alan Modra
ef186fe54a PR29482 - strip: heap-buffer-overflow
PR 29482
	* coffcode.h (coff_set_section_contents): Sanity check _LIB.
2022-08-13 15:32:47 +09:30
Youling Tang
31f6009538 bfd: Add support for LoongArch64 EFI (efi-*-loongarch64).
This adds support for efi-loongarch64 by virtue of adding a new PEI target
pei-loongarch64.  This is not a full target and only exists to support EFI at
this time.

This means that this target does not support relocation processing and is mostly
a container format.  This format has been added to elf based loongarch64 targets
such that efi images can be made natively on Linux.

However this target is not valid for use with gas but only with objcopy.

We should't limit addresses to 32-bits for 64-bit vma, otherwise there will be
"RVA truncated" error when using objcopy on loongarch64.

With these changes the resulting file is recognized as an efi image.

Any magic number is based on the Microsoft PE specification [1].

The test results are as follows:
$ make check-binutils RUNTESTFLAGS='loongarch64.exp'
  PASS: Check if efi app format is recognized

$ objdump -h -f tmpdir/loongarch64copy.o
  tmpdir/loongarch64copy.o:     file format pei-loongarch64
  architecture: Loongarch64, flags 0x00000132:
  EXEC_P, HAS_SYMS, HAS_LOCALS, D_PAGED
  start address 0x0000000000000000

  Sections:
  Idx Name          Size      VMA               LMA               File off  Algn
    0 .text         0000003c  00000000200000b0  00000000200000b0  00000200  2**2
                    CONTENTS, ALLOC, LOAD, READONLY, CODE

[1] https://docs.microsoft.com/en-us/windows/win32/debug/pe-format

bfd:
  * .gitignore (pe-loongarch64igen.c): New.
  * Makefile.am (pei-loongarch64.lo, pe-loongarch64igen.lo, pei-loongarch64.c,
  pe-loongarch64igen.c): Add support.
  * Makefile.in: Likewise.
  * bfd.c (bfd_get_sign_extend_vma): Add pei-loongarch64.
  * coff-loongarch64.c: New file.
  * coffcode.h (coff_set_arch_mach_hook, coff_set_flags,
  coff_write_object_contents) Add loongarch64 (loongarch64_pei_vec) support.
  * config.bfd: Likewise.
  * configure: Likewise.
  * configure.ac: Likewise.
  * libpei.h (GET_OPTHDR_IMAGE_BASE, PUT_OPTHDR_IMAGE_BASE,
  GET_OPTHDR_SIZE_OF_STACK_RESERVE, PUT_OPTHDR_SIZE_OF_STACK_RESERVE,
  GET_OPTHDR_SIZE_OF_STACK_COMMIT, PUT_OPTHDR_SIZE_OF_STACK_COMMIT,
  GET_OPTHDR_SIZE_OF_HEAP_RESERVE, PUT_OPTHDR_SIZE_OF_HEAP_RESERVE,
  GET_OPTHDR_SIZE_OF_HEAP_COMMIT, PUT_OPTHDR_SIZE_OF_HEAP_COMMIT,
  GET_PDATA_ENTRY, _bfd_peLoongArch64_bfd_copy_private_bfd_data_common,
  _bfd_peLoongArch64_bfd_copy_private_section_data,
  _bfd_peLoongArch64_get_symbol_info, _bfd_peLoongArch64_only_swap_filehdr_out,
  _bfd_peLoongArch64_print_private_bfd_data_common,
  _bfd_peLoongArch64i_final_link_postscript,
  _bfd_peLoongArch64i_only_swap_filehdr_out, _bfd_peLoongArch64i_swap_aouthdr_in,
  _bfd_peLoongArch64i_swap_aouthdr_out, _bfd_peLoongArch64i_swap_aux_in,
  _bfd_peLoongArch64i_swap_aux_out, _bfd_peLoongArch64i_swap_lineno_in,
  _bfd_peLoongArch64i_swap_lineno_out, _bfd_peLoongArch64i_swap_scnhdr_out,
  _bfd_peLoongArch64i_swap_sym_in, _bfd_peLoongArch64i_swap_sym_out,
  _bfd_peLoongArch64i_swap_debugdir_in, _bfd_peLoongArch64i_swap_debugdir_out,
  _bfd_peLoongArch64i_write_codeview_record,
  _bfd_peLoongArch64i_slurp_codeview_record,
  _bfd_peLoongArch64_print_ce_compressed_pdata): New.
  * peXXigen.c (_bfd_XXi_swap_aouthdr_in, _bfd_XXi_swap_aouthdr_out,
  _bfd_XXi_swap_scnhdr_out, pe_print_pdata, _bfd_XX_print_private_bfd_data_common,
  _bfd_XX_bfd_copy_private_section_data, _bfd_XXi_final_link_postscript):
  Support COFF_WITH_peLoongArch64,
  * pei-loongarch64.c: New file.
  * peicode.h (coff_swap_scnhdr_in, pe_ILF_build_a_bfd, pe_ILF_object_p):
  Support COFF_WITH_peLoongArch64.
  (jtab): Add dummy entry that traps.
  * targets.c (loongarch64_pei_vec): New.

binutils
  * testsuite/binutils-all/loongarch64/loongarch64.exp: New file.
  * testsuite/binutils-all/loongarch64/pei-loongarch64.d: New test.
  * testsuite/binutils-all/loongarch64/pei-loongarch64.s: New test.

include
  * coff/loongarch64.h: New file.
  * coff/pe.h (IMAGE_FILE_MACHINE_LOONGARCH64): New.

Signed-off-by: Youling Tang <tangyouling@loongson.cn>
2022-08-10 09:26:25 +08:00
Alan Modra
578a7392c3 objcopy section alignment
bfd_set_section_alignment currently always returns true.  This patch
changes it to return false on silly alignment values, avoiding yet
another way to trigger ubsan errors like coffcode.h:3192:12: runtime
error: shift exponent 299 is too large for 32-bit type 'int'.  We'll
catch that one in objcopy.c:setup_sections.  However, setup_sections
gives up on other setup operations that are necessary even after an
error of some sort.  Change that to keep going, which might change the
error message but that shouldn't matter in the least.

bfd/
	* section.c (bfd_set_section_alignment): Return false and
	don't set alignment_power for stupidly large alignments.
	* bfd-in2.h: Regenerate.
	* coffcode.h (coff_compute_section_file_positions): Don't use
	an int constant when calculating alignment.
binutils/
	* objcopy.c (setup_section): Keep on going after hitting
	non-fatal errors.
2022-08-06 18:43:24 +09:30
Alan Modra
b82817674f Don't use BFD_VMA_FMT in binutils
BFD_VMA_FMT can't be used in format strings that need to be
translated, because the translation won't work when the type of
bfd_vma differs from the machine used to compile .pot files.  We've
known about this for a long time, but patches slip through review.

So just get rid of BFD_VMA_FMT, instead using the appropriate PRId64,
PRIu64, PRIx64 or PRIo64 and SCN variants for scanf.  The patch is
mostly mechanical, the only thing requiring any thought is casts
needed to preserve PRId64 output from bfd_vma values, or to preserve
one of the unsigned output formats from bfd_signed_vma values.
2022-08-04 12:22:39 +09:30
Alan Modra
f66d30a6e5 asan: null deref in coff_write_relocs
* coffcode.h (coff_write_relocs): Don't deref NULL howto.
2022-06-02 16:31:42 +09:30
Alan Modra
6015985895 Replace bfd_hostptr_t with uintptr_t
bfd_hostptr_t is defined as a type large enough to hold either a long
or a pointer.  It mostly appears in the coff backend code in casts.
include/coff/internal.h struct internal_syment and union
internal_auxent have the only uses in data structures, where
comparison with include/coff/external.h and other code reveals that
the type only needs to be large enough for a 32-bit integer or a
pointer.  That should mean replacing with uintptr_t is OK.
2022-05-27 22:08:59 +09:30
Jan Beulich
ce462d0435 COFF: make objcopy / strip honor --keep-file-symbols
So far this option had no effect when used together with e.g.
--strip-debug. Set BSF_FILE on these symbols to change that.

While altering this also join two adjacent blocks of case labeled
statements with identical code.
2022-05-19 12:44:08 +02:00
Jan Beulich
bc5baa9f13 don't over-align file positions of PE executable sections
When a sufficiently small alignment was specified via --file-alignment,
individual section alignment shouldn't affect placement within the file.
This involves first of all clearing D_PAGED for images when section and
file alignment together don't permit paging of the image. The involved
comparison against COFF_PAGE_SIZE in turn helped point out (through a
compiler warning) that 'page_size' should be of unsigned type (as in
particular FileAlignment is). This yet in turn pointed out a dubious
error condition (which is being deleted).

For the D_PAGED case I think the enforced file alignment may still be
too high, but I'm wary of changing that logic without knowing of
possible corner cases.

Furthermore file positions in PE should be independent of the alignment
recorded in section headers anyway. Otherwise there are e.g. anomalies
following commit 6f8f6017a0 ("PR27567, Linking PE files adds alignment
section flags to executables") in that linking would use information a
subsequent processing step (e.g. stripping) wouldn't have available
anymore, and hence a binary could change in that 2nd step for no actual
reason. (Similarly stripping a binary linked with a linker pre-dating
that commit would change the binary again when stripping it a 2nd time.)
2022-05-19 12:43:10 +02:00
Alan Modra
f3b9cfd195 bfd: remove use of PTR
* coffcode.h (coff_write_object_contents): Don't cast to PTR.
	* elf32-csky.c (csky_elf_link_hash_traverse): Remove use of PTR
	and PARAMS.
	(csky_allocate_dynrelocs): Don't use PTR cast.
	* elf32-nios2.c (adjust_dynrelocs, allocate_dynrelocs): Replace
	PTR with void *.
	* elf32-visium.c (visium_elf_howto_parity_reloc): Likewise.
	* elfxx-ia64.c (ia64_elf_reloc): Likewise.
	* plugin.c (bfd_plugin_bfd_print_private_bfd_data): Likewise.
2022-05-10 11:21:37 +09:30
Clément Chigot
add588a8ef gas: add visibility support for XCOFF
XCOFF assembly defines the visibility using an additional argument
on several pseudo-ops: .globl, .weak, .extern and .comm.
This implies that .globl and .weak syntax is different than the
usual GNU syntax. But we want to provide compatibility with AIX
assembler, especially because GCC is generating the visibility
using this XCOFF syntax.

PR 22085

bfd/ChangeLog:

        * coffcode.h (coff_write_object_contents): Change XCOFF header
        vstamp field to 2.
        * coffgen.c (coff_print_symbol): Increase the size for n_type.

gas/ChangeLog:

        * config/tc-ppc.c (ppc_xcoff_get_visibility): New function.
        (ppc_globl): New function.
        (ppc_weak): New function.
        (ppc_comm): Add visibility field support.
        (ppc_extern): Likewise.
        * testsuite/gas/all/cofftag.d: Adjust to new n_type size
        providing by objdump.
        * testsuite/gas/ppc/test1xcoff32.d: Likewise.
        * testsuite/gas/ppc/aix.exp: Add new tests.
        * testsuite/gas/ppc/xcoff-visibility-1-32.d: New test.
        * testsuite/gas/ppc/xcoff-visibility-1-64.d: New test.
        * testsuite/gas/ppc/xcoff-visibility-1.s: New test.

include/ChangeLog:

        * coff/internal.h (SYM_V_INTERNAL, SYM_V_HIDDEN,
        SYM_V_PROTECTED, SYM_V_EXPORTED, SYM_V_MASK): New defines.
        * coff/xcoff.h (struct xcoff_link_hash_entry): Add visibility
        field.

ld/ChangeLog:

        * testsuite/ld-pe/pr19803.d: Adjust to new n_type size
        providing by objdump.
2022-01-12 09:08:11 +01:00
Alan Modra
a2c5833233 Update year range in copyright notice of binutils files
The result of running etc/update-copyright.py --this-year, fixing all
the files whose mode is changed by the script, plus a build with
--enable-maintainer-mode --enable-cgen-maint=yes, then checking
out */po/*.pot which we don't update frequently.

The copy of cgen was with commit d1dd5fcc38ead reverted as that commit
breaks building of bfp opcodes files.
2022-01-02 12:04:28 +10:30
Alan Modra
eda0ddeb26 XCOFF C_STSYM test failure on 32-bit host
This test was failing here and on another similar symbol:
[  4](sec  1)(fl 0x00)(ty   0)(scl 143) (nx 0) 0x05d1745d11745d21 .bs
where correct output is
[  4](sec  1)(fl 0x00)(ty   0)(scl 143) (nx 0) 0x000000000000000a .bs

The problem is caused by a 32-bit host pointer being sign-extended
when stored into a 64-bit bfd_vma, and then that value not being
trimmed back to 32 bits when used.  The following belt-and-braces
patch fixes both the store and subsequent reads.

	* coffcode.h (coff_slurp_symbol_table): Do not sign extend
	when storing a host pointer to syment.n_value.
	* coffgen.c (coff_get_symbol_info): Cast syment.n_value to a
	bfd_hostptr_t before using in arithmetic.
	(coff_print_symbol): Likewise.
2021-12-15 08:23:10 +10:30
Tamar Christina
b69c9d41e8 AArch64: Add support for AArch64 EFI (efi-*-aarch64).
This adds support for efi-*-aarch64 by virtue of adding a new PEI target
pei-aarch64-little.  This is not a full target and only exists to support EFI
at this time.

This means that this target does not support relocation processing and is mostly
a container format.  This format has been added to elf based aarch64 targets
such that efi images can be made natively on Linux.

However this target is not valid for use with gas but only with objcopy.

With these changes the resulting file is recognized as an efi image by
third party tools:

>  pecli info hello.efi

Metadata
================================================================================
MD5:            598c32a778b0f0deebe977fef8578c4e
SHA1:           4580121edd5cb4dc40f51b28f171fd15250df84c
SHA256:         3154bd7cf42433d1c957f6bf55a17ad8c57ed41b29df2d485703349fd6ff1d5c
Imphash:
Size:           47561 bytes
Type:           PE32+ executable (EFI application) (stripped to external PDB), for MS Windows
Compile Time:   1970-01-01 00:00:00 (UTC - 0x0       )
Entry point:    0x2000 (section .text)

Sections
================================================================================
Name      RWX  VirtSize   VirtAddr   RawAddr   RawSize   Entropy  md5
.text     R-X  0x5bb0     0x2000     0x400     0x5c00      6.39 551fbc264256a3f387de8a891500ae0d
.reloc    R--  0xc        0x8000     0x6000    0x200       0.02 0c45f6d812d079821c1d54c09ab89e1d
.data     RW-  0x1d88     0x9000     0x6200    0x1e00      4.18 5d1137c09f01289dc62bf754f7290db3
.dynamic  RW-  0xf0       0xb000     0x8000    0x200       0.34 5c94ed3206f05a277e6f04fbf131f131
.rela     R--  0xe58      0xc000     0x8200    0x1000      1.87 8b5c6bc30f3acb7ca7bf2e6789d68519
.dynsym   R--  0x138      0xd000     0x9200    0x200       0.96 bdcf5101da51aadc663ca8859f88138c

Imports
================================================================================

Any magic number is based on the Microsoft PE specification [1].

[1] https://docs.microsoft.com/en-us/windows/win32/debug/pe-format

bfd/ChangeLog:

2021-10-21  Tamar Christina  <tamar.christina@arm.com>

	PR binutils/26206
	* .gitignore (pe-aarch64igen.c): New.
	* Makefile.am (pei-aarch64.lo, pe-aarch64igen.lo, pei-aarch64.c,
	pe-aarch64igen.c): Add support.
	* Makefile.in: Likewise.
	* bfd.c (bfd_get_sign_extend_vma): Add pei-aarch64-little.
	* coff-aarch64.c: New file.
	* coffcode.h (coff_set_arch_mach_hook, coff_set_flags,
	coff_write_object_contents) Add aarch64 (aarch64_pei_vec) support.
	* config.bfd: Likewise.
	* configure: Likewise.
	* configure.ac: Likewise.
	* libpei.h (GET_OPTHDR_IMAGE_BASE, PUT_OPTHDR_IMAGE_BASE,
	GET_OPTHDR_SIZE_OF_STACK_RESERVE, PUT_OPTHDR_SIZE_OF_STACK_RESERVE,
	GET_OPTHDR_SIZE_OF_STACK_COMMIT, PUT_OPTHDR_SIZE_OF_STACK_COMMIT,
	GET_OPTHDR_SIZE_OF_HEAP_RESERVE, PUT_OPTHDR_SIZE_OF_HEAP_RESERVE,
	GET_OPTHDR_SIZE_OF_HEAP_COMMIT, PUT_OPTHDR_SIZE_OF_HEAP_COMMIT,
	GET_PDATA_ENTRY, _bfd_peAArch64_bfd_copy_private_bfd_data_common,
	_bfd_peAArch64_bfd_copy_private_section_data,
	_bfd_peAArch64_get_symbol_info, _bfd_peAArch64_only_swap_filehdr_out,
	_bfd_peAArch64_print_private_bfd_data_common,
	_bfd_peAArch64i_final_link_postscript,
	_bfd_peAArch64i_only_swap_filehdr_out, _bfd_peAArch64i_swap_aouthdr_in,
	_bfd_peAArch64i_swap_aouthdr_out, _bfd_peAArch64i_swap_aux_in,
	_bfd_peAArch64i_swap_aux_out, _bfd_peAArch64i_swap_lineno_in,
	_bfd_peAArch64i_swap_lineno_out, _bfd_peAArch64i_swap_scnhdr_out,
	_bfd_peAArch64i_swap_sym_in, _bfd_peAArch64i_swap_sym_out,
	_bfd_peAArch64i_swap_debugdir_in, _bfd_peAArch64i_swap_debugdir_out,
	_bfd_peAArch64i_write_codeview_record,
	_bfd_peAArch64i_slurp_codeview_record,
	_bfd_peAArch64_print_ce_compressed_pdata): New.
	* peXXigen.c (_bfd_XXi_swap_aouthdr_in, _bfd_XXi_swap_aouthdr_out,
	pe_print_pdata, _bfd_XX_print_private_bfd_data_common,
	_bfd_XX_bfd_copy_private_section_data, _bfd_XXi_final_link_postscript):
	Support COFF_WITH_peAArch64,
	* pei-aarch64.c: New file.
	* peicode.h (coff_swap_scnhdr_in, pe_ILF_build_a_bfd, pe_ILF_object_p):
	Support COFF_WITH_peAArch64.
	(jtab): Add dummy entry that traps.
	* targets.c (aarch64_pei_vec): New.

binutils/ChangeLog:

2021-10-21  Tamar Christina  <tamar.christina@arm.com>

	PR binutils/26206
	* NEWS: Add new support.
	* objcopy.c (convert_efi_target): Add efi-*-aarch64 support.
	* testsuite/binutils-all/aarch64/pei-aarch64-little.d: New test.
	* testsuite/binutils-all/aarch64/pei-aarch64-little.s: New test.

include/ChangeLog:

2021-10-21  Tamar Christina  <tamar.christina@arm.com>

	PR binutils/26206
	* coff/aarch64.h: New file.
	* coff/pe.h (IMAGE_FILE_MACHINE_ARM64): New.
2021-11-23 09:36:39 +00:00
Clément Chigot
e86fc4a5bc PR 28447: implement multiple parameters for .file on XCOFF
On XCOFF, ".file" pseudo-op allows 3 extras parameters to provide
additional information to AIX linker, or its debugger. These are
stored in auxiliary entries of the C_FILE symbol.

bfd/
	PR 28447
	* coffcode.h (combined_entry_type): Add extrap field.
	(coff_bigobj_swap_aux_in): Adjust names of x_file fields.
	(coff_bigobj_swap_aux_out): Likewise.
	* coffgen.c (coff_write_auxent_fname): New function.
	(coff_fix_symbol_name): Write x_file using
	 coff_write_auxent_fname.
	(coff_write_symbol): Likewise.
	(coff_write_symbols): Add C_FILE auxiliary entries to
	string table if needed.
	(coff_get_normalized_symtab): Adjust names of x_file fields.
	Normalize C_FILE auxiliary entries.
	(coff_print_symbol): Print C_FILE auxiliary entries.
	* coff-rs6000.c (_bfd_xcoff_swap_aux_in): Adjust names of
	x_file fields.
	(_bfd_xcoff_swap_aux_out): Likewise.
	* coff64-rs6000.c (_bfd_xcoff64_swap_aux_in): Likewise.
	(_bfd_xcoff64_swap_aux_out): Likewise.
	* cofflink.c (_bfd_coff_final_link): Likewise.
	(_bfd_coff_link_input_bfd): Likewise.
	* coffswap.h (coff_swap_aux_in): Likewise.
	* peXXigen.c (_bfd_XXi_swap_aux_in): Likewise.
	(_bfd_XXi_swap_aux_out): Likewise.
	* xcofflink.c (xcoff_link_input_bfd): Likewise.
	* libcoff.h: Regenerate.
gas/
	* config/tc-ppc.c (ppc_file): New function.
	* config/tc-ppc.h (OBJ_COFF_MAX_AUXENTRIES): Change to 4.
	* testsuite/gas/ppc/aix.exp: Add tests.
	* testsuite/gas/ppc/xcoff-file-32.d: New test.
	* testsuite/gas/ppc/xcoff-file-64.d: New test.
	* testsuite/gas/ppc/xcoff-file.s: New test.
include/
	* coff/internal.h (union internal_auxent): Change x_file to be a
	  struct instead of a union. Add x_ftype field.
	* coff/rs6000.h (union external_auxent): Add x_resv field.
	* coff/xcoff.h (XFT_FN): New define.
	(XFT_CT): Likewise.
	(XFT_CV): Likewise.
	(XFT_CD): Likewise.
2021-11-10 14:43:24 +10:30
Alan Modra
1ed0032b40 asan: c4x, c54x coff_canonicalize_reloc buffer overflow
Sometimes the investigation of a fuzzing bug report leads into areas
you'd rather not go.  In this instance by the time I'd figured out the
real cause was a target variant that had never been properly supported
in binutils, the time needed to fix it was less than the time needed
to rip it out.

	* coffcode.h (coff_set_alignment_hook): Call bfd_coff_swap_reloc_in
	not coff_swap_reloc_in.
	(coff_slurp_reloc_table): Likewise.  Don't use RELOC type.
	(ticoff0_swap_table): Use coff_swap_reloc_v0_out and
	coff_swap_reloc_v0_in.
	* coffswap.h (coff_swap_reloc_v0_in, coff_swap_reloc_v0_out): New.
	* coff-tic54x.c (tic54x_lookup_howto): Don't abort.
	* coffgen.c (coff_get_normalized_symtab): Use PTR_ADD.
	* bfd-in.h (PTR_ADD, NPTR_ADD): Avoid warnings when passing an
	expression.
	* bfd-in2.h: Regenerate.
2021-10-24 22:05:51 +10:30
Alan Modra
1808483c2f PR28402, fail to allocate line number array
This fixes a situation where the COFF code allocated memory for
internal representaion arrays before reading the external file data.
That meant the allocation didn't have any sanity check against file
size.

	PR 28402
	* coffcode.h (buy_and_read): Malloc rather than alloc memory.
	(coff_slurp_line_table): Read native line number info before
	allocating memory for internal line number array.  Adjust error
	paths to suit.  Remove now unnecessary line number count check.
	(coff_slurp_reloc_table): Adjust to suit buy_and_read change.
2021-10-06 15:30:23 +10:30
Alan Modra
29daccc9f3 revise PE IMAGE_SCN_LNK_NRELOC_OVFL test
* coffcode.h (coff_set_alignment_hook): Test that the resulting
	reloc count is not less than 0xffff.
2021-08-04 12:34:52 +09:30
Alan Modra
6329d1e13e IMAGE_SCN_LNK_NRELOC_OVFL
From microsoft docs: It is an error if IMAGE_SCN_LNK_NRELOC_OVFL is
set and there are fewer than 0xffff relocations in the section.

	* coffcode.h (coff_set_alignment_hook): Sanity check overflow
	reloc count.
2021-07-30 13:15:58 +09:30
Clément Chigot
51d29b8cac objdump: add DWARF support for AIX
DWARF sections have special names on AIX which need be handled
by objdump in order to correctly print them.
This patch also adds the correlation in bfd for future uses.

bfd/
	* libxcoff.h (struct xcoff_dwsect_name): Add DWARF name.
	* coff-rs6000.c (xcoff_dwsect_names): Update.
	* coffcode.h (sec_to_styp_flags): Likewise.
	(coff_new_section_hook): Likewise.
binutils/
	* dwarf.h (struct dwarf_section): Add XCOFF name.
	* dwarf.c (struct dwarf_section_display): Update.
	* objdump.c (load_debug_section): Add XCOFF name handler.
	(dump_dwarf_section): Likewise.
gas/
	* config/tc-ppc.c (ppc_change_debug_section): Update to
	match new name's field.
2021-07-14 21:40:05 +09:30
Alan Modra
6f8f6017a0 PR27567, Linking PE files adds alignment section flags to executables
So don't set those flags for an executable.  In the patch I also test
DYNAMIC even though the PE bfd code doesn't appear to set it for dlls.
I figure it doesn't hurt to include that flag too.

	PR 27567
bfd/
	* coffcode.h (styp_to_sec_flags): Use an unsigned long styp_flags.
	(coff_write_object_contents): Pass bfd to COFF_ENCODE_ALIGNMENT,
	ignore alignment checks when return is false.  Formatting.
include/
	* coff/internal.h (struct internal_scnhdr): Make s_flags unsigned long.
	* coff/pe.h (COFF_ENCODE_ALIGNMENT): Don't set align flags for an
	executable and return false.  Do so for a relocatable object and
	evaluate to true.
	* coff/ti.h (COFF_ENCODE_ALIGNMENT): Add bfd arg and evaluate to true.
	(COFF_DECODE_ALIGNMENT): Formatting.
	* coff/z80.h (COFF_ENCODE_ALIGNMENT): Similarly.
	(COFF_DECODE_ALIGNMENT): Similarly.
2021-04-16 23:21:44 +09:30