Commit Graph

4775 Commits

Author SHA1 Message Date
Alan Modra
f2172071e6 elfedit: segv with --enable-x86-feature
PR 33024
	PR 33025
	* elfedit.c (update_gnu_property): Sanity check program headers.
2025-05-28 22:34:50 +09:30
Alan Modra
5e3176dd79 PR 33023 memory leak in objdump when specifying --endian
* objdump.c (disassemble_data): Free modified xvec and replace
	original.
2025-05-28 15:55:27 +09:30
Collin Funk
5328074d84 binutils/doc: Remove '.info' suffix in @ref, etc
Texinfo 7.2 began showing warnings like:

    binutils.texi:882: warning: do not set .info suffix in reference for manual `ld.info'
    binutils.texi:1365: warning: do not set .info suffix in reference for manual `ld.info'

The Texinfo developers plan to stop removing the '.info' suffix
internally in a future release so without this patch the references will
break in the future.

Signed-off-by: Collin Funk <collin.funk1@gmail.com>
2025-05-16 10:37:04 +02:00
H.J. Lu
d1851edfe9 binutils: Don't complain plugin with all LTO sections removed
When all LTO sections have been removed, the BFD lto_type is set to
lto_non_ir_object by bfd_set_lto_type.  In this case, don't complain
needing a plugin when seeing a LTO slim symbol.

bfd/

	PR binutils/32967
	* archive.c (_bfd_compute_and_write_armap): Call
	bfd_lto_slim_symbol_p to check LTO slim symbol.
	* bfd-in2.h: Generated.
	* bfd.c (bfd_lto_slim_symbol_p): New.

binutils/

	PR binutils/32967
	* nm.c (filter_symbols): Call bfd_lto_slim_symbol_p to check
	LTO slim symbol.

ld/

	PR binutils/32967
	* testsuite/ld-plugin/lto-binutils.exp: Run PR binutils/32967
	tests.
	* testsuite/ld-plugin/strip-1a-s-all.nd: New file.

Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
2025-05-15 08:43:58 +08:00
Alan Modra
c6c8d0b821 resbin: don't pass NULL as printf %s arg
Fix three place where a NULL could be passed to "toosmall".
2025-05-14 21:55:11 +09:30
Richard Earnshaw
3222beac6e Remove Marcus Shawcroft from the MAINTAINERS file
Marcus has resigned from the project.
2025-05-14 11:45:25 +01:00
Matthieu Longo
c5a0d25737 Remove annoying spaces from objcopy.exp 2025-05-14 10:38:13 +01:00
H.J. Lu
717a38e9a0 strip: Add GCC LTO IR support
Add GCC LTO IR support to strip by copying GCC LTO IR input as unknown
object file.  Don't enable LTO plugin in strip unless all LTO sections
should be removed, assuming all LTO sections will be removed with
-R .gnu.lto_.*.  Add linker LTO tests for strip with --strip-unneeded
and GCC LTO IR inputs.

binutils/

	PR binutils/21479
	* objcopy.c: Include "plugin-api.h" and "plugin.h".
	(lto_sections_removed): New.
	(command_line_switch): Add OPTION_PLUGIN.
	(strip_options): Likewise.
	(strip_usage): Display "--plugin NAME".
	(copy_unknown_file): New function.
	(copy_unknown_object): Call copy_unknown_file.
	(copy_archive): Copy input LTO IR member as unknown object.
	(copy_file): Set input target to "plugin" for strip if it is
	unset unless all LTO sections should be removed.  Copy input
	LTO IR file as unknown file.
	(strip_main): Call bfd_plugin_set_program_name. Handle
	OPTION_PLUGIN.  Set lto_sections_removed to true if all GCC
	LTO sections should be removed.
	* doc/binutils.texi: Document --plugin for strip.

ld/

	PR binutils/21479
	* testsuite/ld-plugin/lto-binutils.exp: New file.
	* testsuite/ld-plugin/strip-1a-fat.c: Likewise.
	* testsuite/ld-plugin/strip-1a-fat.rd: Likewise.
	* testsuite/ld-plugin/strip-1b-fat.c: Likewise.
	* testsuite/ld-plugin/strip-1b-fat.rd: Likewise.
	* testsuite/ld-plugin/strip-1a.c: Likewise.
	* testsuite/ld-plugin/strip-1b.c: Likewise.
	* testsuite/lib/ld-lib.exp (run_cc_link_tests): Add optional
	trailing ld options.

Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
2025-05-14 15:09:40 +08:00
Peter Bergner
61a6e3c7bf MAINTAINERS: Update my email address
Update my email address and move up Surya's name as the main PPC contact.

Signed-off-by: Peter Bergner <bergner@tenstorrent.com>
2025-05-09 18:01:15 -05:00
Jiawei
433372af69 RISC-V: Add Privileged Architecture 1.13 CSRs.
This patch support RISC-V Privileged Architecture 1.13 CSRs 'medelegh' and
'hedelegh'. More details between 1.12 and 1.13 see [1].

[1] https://github.com/riscv/riscv-isa-manual/blob/main/src/priv-preface.adoc

Version log: Remove gas/po changes.

bfd/ChangeLog:

        * cpu-riscv.c: New option.
        * cpu-riscv.h (enum riscv_spec_class): Ditto.

binutils/ChangeLog:

        * doc/binutils.texi: New option.

gas/ChangeLog:

        * NEWS: Add priv-1.13 support.
        * config/tc-riscv.c: New option.
        * configure: Ditto.
        * configure.ac: Ditto.
        * testsuite/gas/riscv/csr-version-1p10.d: New CSR.
        * testsuite/gas/riscv/csr-version-1p10.l: New warning.
        * testsuite/gas/riscv/csr-version-1p11.d: New CSR.
        * testsuite/gas/riscv/csr-version-1p11.l: New warning.
        * testsuite/gas/riscv/csr-version-1p12.d: New CSR.
        * testsuite/gas/riscv/csr-version-1p12.l: New warning.
        * testsuite/gas/riscv/csr.s: New CSR.
        * testsuite/gas/riscv/attribute-15.d: New test.
        * testsuite/gas/riscv/attribute-16.d: New test.
        * testsuite/gas/riscv/csr-version-1p13.d: New test.
        * testsuite/gas/riscv/csr-version-1p13.l: New test.

include/ChangeLog:

        * opcode/riscv-opc.h (CSR_MEDELEGH): New CSR.
        (CSR_HEDELEGH): Ditto.
        (DECLARE_CSR): Ditto.
2025-05-09 12:28:42 +08:00
Alan Modra
4aaf663ded windres: buffer overflow
bin_to_res_menuexitems can be called with random data offsets (and thus
remaining lengths), confusing code that expects 4-byte aligned data.
Prevent an item length adjustment for alignment exceeding the
remaining length and then overflowing.
2025-05-08 09:26:56 +09:30
Alan Modra
a7cae7faf1 windres_get_* functions
windres_get_32 and similar have a length parameter that in most cases
is just the required length, so it is redundant.  The few cases where
a variable length is passed are all in resrc.c.  So, get rid of the
length parameter and introduce wrappers in resrc.c to check the
length.
2025-05-06 17:05:40 +09:30
Jan Beulich
36e66b193b Arm/COFF: accept .def outside of CCS mode
There's no reason to reject this common COFF directive when it doesn't
have any other meaning.
2025-05-02 10:05:12 +02:00
H.J. Lu
f72c4fa3d5 dwarf: Properly check holes in .debug_ranges/debug_rnglists
Don't warn if the offset of the first entry in .debug_rnglists starts
right after the header.  Warn holes in .debug_ranges and debug_rnglists
sections only if the last end pointer isn't the same as the current
start pointer.

	PR binutils/32927
	* dwarf.c (display_debug_ranges_list): Return the pointer to the
	end.
	(display_debug_ranges): Don't warn if the offset of the first
	entry in .debug_rnglists starts right after the header.  Warn a
	hole only if the last end pointer is the same as the next pointer.
	* testsuite/binutils-all/x86-64/dwarf4.s: New file.
	* testsuite/binutils-all/x86-64/dwarf5.s: Likewise.
	* testsuite/binutils-all/x86-64/pr32927-1.d: Likewise.
	* testsuite/binutils-all/x86-64/pr32927-2.d: Likewise.

Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
Co-Authored-By: Alan Modra <amodra@gmail.com>
2025-05-01 13:52:15 +09:30
H.J. Lu
82bdc396a4 dwarf: Dump .debug_loclists only for DWARF-5
.debug_loclists section is loaded into debug_information as DWARF-5 debug
info and .debug_loc section is loaded into debug_information as pre-DWARF-5
debug info.  When dumping .debug_loc section, we should only process
pre-DWARF-5 debug info in debug_information.  When dumping .debug_loclists
section, we should only process DWARF-5 info in debug_information.

binutils/

	PR binutils/32809
	* dwarf.c (display_debug_loc): Dump .debug_loclists only for
	DWARF-5.

ld/

	PR binutils/32809
	* testsuite/ld-x86-64/dwarf4.s: New file.
	* testsuite/ld-x86-64/dwarf5a.s: Likewise.
	* testsuite/ld-x86-64/dwarf5b.s: Likewise.
	* testsuite/ld-x86-64/pr32809.d: Likewise.
	* testsuite/ld-x86-64/x86-64.exp: Run pr32809.

Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
2025-04-29 14:25:38 +08:00
Surya Kumari Jangala
f7745f8cef Update binutils/MAINTAINERS for PPC
binutils/
	* MAINTAINERS: Add myself as PPC maintainer.
2025-04-28 03:37:07 -05:00
Alan Modra
d2533f464f rescoff: close bfd on failure paths
Also free malloc'd relocs.
2025-04-21 09:01:50 +09:30
Alan Modra
46efd5fb30 buffer overrun in read_coff_res_dir
* rescoff.c (read_coff_res_dir): Add more sanity checking.
	Tidy and correct existing checks.
2025-04-16 14:37:17 +09:30
Alan Modra
c3fd596022 resbin.c formatting fixes
Also remove unnecessary casts on memory alloc function returns.
2025-04-16 14:37:17 +09:30
Alan Modra
d4c57933d6 Re: windres: buffer overflow in bin_to_res_toolbar
Commit 9e68cae4fd broke the check I added in commit 4846e543de.
Add missing "return NULL".
2025-04-16 14:37:17 +09:30
Marek Pikuła
88891208c3 RISC-V: Add missing disassembler option max
The flag already exists but it's not been exposed to user.

Signed-off-by: Marek Pikuła <m.pikula@partner.samsung.com>
2025-04-15 10:59:51 +08:00
Alan Modra
9e68cae4fd windres: don't exit so much on errors in read_coff_rsrc
windres code has the habit of exiting on any error.  That's not so
bad, but it does make oss-fuzz ineffective when testing windres.  Fix
many places that print errors and exit to instead print the error and
pass status up the call chain.  In the process of doing this, I
noticed write_res_file was calling bfd_close without checking return
status.  Fixing that resulted in lots of testsuite failures.  The
problem was a lack of bfd_set_format in windres_open_as_binary, which
leaves the output file as bfd_unknown format.  As it happens this
doesn't make any difference in writing the output binary file, except
for the bfd_close return status.
2025-04-15 06:59:15 +09:30
Alan Modra
4846e543de windres: buffer overflow in bin_to_res_toolbar
oss-fuzz testcase manages to hit a buffer overflow.  Sanity check
by passing the buffer length to bin_to_res_toolbar and ensuring reads
don't go off the end of the buffer.
2025-04-15 06:59:15 +09:30
Jan Beulich
e71f5f09ce nm: fall back to heuristic when ELF symbol has zero size
Size being set for a symbol isn't a strict requirement in ELF. For ones
not having their size set, fall back to the same logic as used for non-
ELF, non-COFF symbols.

While there switch to using elf_symbol_from() instead of kind of open-
coding it.
2025-04-07 12:45:30 +02:00
Jan Beulich
6fe5e01b13 nm: also retrieve size for COFF function symbols
Like ELF for all symbols, COFF can record size for at least function
ones. Use that - if available - in preference to the distance-to-next-
symbol heuristic.

To be able to use the new test there, make TI C54x follow TI C4x in
providing .sdef to cover for .def already having different meaning.
2025-04-07 12:45:11 +02:00
Jan Beulich
4fa9476ad2 objcopy: also check --file-alignment option argument
... to be a power of two, just like --section-alignment does.
2025-04-04 10:25:31 +02:00
Jan Beulich
379535a2e6 binutils: run objcopy set-section-alignment also for COFF
There's no reason to limit this to just ELF. TI C30 and Z8k don't encode
section alignment in the section entries though (which can't be quite
right, or there would need to be another means by which to express
alignment needs), so --set-section-alignment simply has no effect there.
2025-04-04 10:25:16 +02:00
Jan Beulich
43ea7d00a7 objcopy: constrain --section-alignment to PE binaries again
PR binutils/32732

The --set-section-alignment option is what ought to be used on object
files; --section-alignment should be affecting PE binaries only, and
only the value stored in the header. Sections don't individually have
alignment recorded there; see 6f8f6017a0 ("PR27567, Linking PE files
adds alignment section flags to executables").

Undo the core part of 121a3f4b4f ("Update objcopy's
--section-alignment option so that it sets the alignment flag on..."),
which includes removing the testcase again, while leaving all secondary
changes in place. (Note that the testcase did fail anyway for
i?86-interix, with objdump saying "option -P/--private not supported by
this file".)
2025-04-04 10:24:56 +02:00
Jan Beulich
cc0693d394 ar/objcopy: harmonize .exe suffix stripping
With it only being the tail of the name which wants checking, using
lbasename() isn't helpful. Mirror what objcopy.c:main() does to ar.c,
merely chaning the plain int of the local variable to size_t.
2025-04-04 10:20:31 +02:00
Jan Beulich
25a0668a95 binutils: properly split ar and ranlib
By not linking the exact same object file twice, in particular ranlib can
benefit quite a bit from the compiler eliminating dead code.
2025-04-04 10:20:14 +02:00
Jan Beulich
2fb3d389d8 binutils: properly split objcopy and strip
By not linking the exact same object file twice, in particular strip can
benefit quite a bit from the compiler eliminating dead code.
2025-04-04 10:19:51 +02:00
Alan Modra
6c3458a8b7 PR32829, SEGV on objdump function debug_type_samep
u.kenum is always non-NULL, see debug_make_enum_type.

	PR 32829
	* debug.c (debug_type_samep): Correct incomplete enum test.
	(debug_write_type): Remove dead code.
2025-04-01 23:45:46 +10:30
Clément Chigot
71320db650 binutils/testsuite: don't tail the same input and output file
The output file could be created before the input is gathered by tail,
erasing the later before it's being proceeded.

This happened on rare cases when performing remote tests on
Ubuntu 24.04.
2025-04-01 10:14:54 +02:00
Clément Chigot
c27e4f5273 binutils/testsuite: move objdump test output into tmpdir
"objdump.out" is a testsuite trace and thus should be created within the
tmpdir.
2025-04-01 10:14:54 +02:00
Marek Pikuła
b667765b2c doc/riscv: Add description of disassembler options
Up to this point, no mention of RISC-V-specific disassembler options was
mentioned in binutils documentation. This patch includes description for
all of the currently supported options.

Signed-off-by: Marek Pikuła <m.pikula@partner.samsung.com>
2025-03-28 09:20:41 +08:00
WANG Xuerui
6d1823dc89 LoongArch: Fix disassembly option parsing stopping at the first option
Turns out the return value of parse_loongarch_dis_option acts as an
error code, and previously the function always signified failure with
a non-zero return value, making only the first disassembly option get
to take effect.

Fix by adding the missing `return 0`'s to the two success code paths.

Signed-off-by: WANG Xuerui <git@xen0n.name>
2025-03-26 15:49:58 +08:00
Jan Beulich
daf53ffeaa strip: don't corrupt PE binary's section/file alignment
Section and file alignment are supposed to remain unaltered when PE
binaries are stripped. While this is the case when they're strip-ed
individually, passing multiple such files to strip would reset the
two values to their defaults in all but the first of those binaries.
2025-03-21 08:33:58 +01:00
Nick Clifton
7f6cdc6a9e Updated translations for BFD and BINUTILS sub-directories 2025-03-18 12:25:19 +00:00
Nelson Chu
614d1b72ce gas/NEW: Updated news related to mapping symbol and extensions for risc-v 2025-03-18 13:42:02 +08:00
Jan Beulich
cdd8492b05 objdump: permit disassembling multiple individual functions
Compilers may split functions, e.g. into a "hot" and "cold" part, or
they may emit special case instantiations (e.g. as a result of IPA). It
can be helpful to be able to disassemble all of the parts or clones in
one go. Permit using "--disassemble=" multiple times.
2025-03-07 11:24:19 +01:00
Jan Beulich
60e254b701 objdump: properly disassemble successive functions of the same name
... when only their symbol was requested for disassembly. Addressing the
respective FIXME is as easy as coverting the "else" there to an if()
with the opposite condition, thus accounting for the disabling the
original if() may have effected.
2025-03-07 11:23:13 +01:00
Matthieu Longo
3f8f380190 clean-up readelf: simplify and flatten body of process_attributes
- use find_section_by_type() instead of a for-loop.
- reindent the whole function accordingly.
- move declaration of variables nearer from their usage.
- prune else branch by using a goto in the error case.

diff --git a/binutils/readelf.c b/binutils/readelf.c
index 6d3ec65a8a1..878012da8f0 100644
--- a/binutils/readelf.c
+++ b/binutils/readelf.c
@@ -19268,42 +19268,32 @@ process_attributes (Filedata * filedata,
                    unsigned char * (* display_pub_attribute) (unsigned char *, const unsigned char * const),
                    unsigned char * (* display_proc_gnu_attribute) (unsigned char *, unsigned int, const unsigned char * const))
 {
-  Elf_Internal_Shdr * sect;
-  unsigned i;
-  bool res = true;
-
   /* Find the section header so that we get the size.  */
-  for (i = 0, sect = filedata->section_headers;
-       i < filedata->file_header.e_shnum;
-       i++, sect++)
-    {
-      unsigned char * contents;
-      unsigned char * p;
+  Elf_Internal_Shdr * sect = find_section_by_type (filedata, proc_type);
+  if (sect == NULL)
+    sect = find_section_by_type (filedata, SHT_GNU_ATTRIBUTES);

-      if (sect->sh_type != proc_type && sect->sh_type != SHT_GNU_ATTRIBUTES)
-       continue;
+  if (sect == NULL)
+    /* No section, exit without error.  */
+    return true;

-      contents = (unsigned char *) get_data (NULL, filedata, sect->sh_offset, 1,
-                                             sect->sh_size, _("attributes"));
+  unsigned char * contents = (unsigned char *)
+    get_data (NULL, filedata, sect->sh_offset, 1, sect->sh_size, _("attributes"));
   if (contents == NULL)
-       {
-         res = false;
-         continue;
-       }
+    return false;

-      p = contents;
+  bool res = true;
+  unsigned char * p = contents;
   /* The first character is the version of the attributes.
      Currently only version 1, (aka 'A') is recognised here.  */
   if (*p != 'A')
     {
       printf (_("Unknown attributes version '%c'(%d) - expecting 'A'\n"), *p, *p);
       res = false;
+      goto free_data;
     }
-      else
-       {
-         uint64_t section_len;

-         section_len = sect->sh_size - 1;
+  uint64_t section_len = sect->sh_size - 1;
   p++;

   while (section_len > 0)
@@ -19456,10 +19446,9 @@ process_attributes (Filedata * filedata,
            attr_len = 0;
        }
     }
-       }

+free_data:
   free (contents);
-    }

   return res;
 }
2025-03-04 11:02:03 +00:00
Matthieu Longo
1efbe98a4d clean-up: fix conflicting symbol with unknown from bfd/elf-bfd.h 2025-03-04 11:02:02 +00:00
Matthieu Longo
5d526bdf5f clean-up: fix annoying spaces in binutils/readelf.c 2025-03-04 11:02:02 +00:00
Tom Tromey
ef8975b494 Display entry offset for .debug_names
Since commit ad6dde5aaa ("gdb/dwarf: write offset to parent entry for
DW_IDX_parent"), gdb now emits a .debug_names where the DW_IDX_parent
attribute refers to the parent entry's offset -- previously, due to
some confusion in the standard, gdb used the index of the parent's
name table entry.

This patch changes the .debug_names display code to display each
entry's offset.  This makes it easy to refer from a DW_IDX_parent to
the correct entry.

The new output looks like this:

[...]
Symbol table:
[  1] circular1: <0><1> DW_TAG_module DW_IDX_compile_unit=1 DW_IDX_die_offset=<0x19> DW_IDX_GNU_language=19
[...]
[  6] found: <0x28><2> DW_TAG_subprogram DW_IDX_compile_unit=1 DW_IDX_die_offset=<0x38> DW_IDX_GNU_language=19 DW_IDX_parent=<0x0>

Here you can see that DW_IDX_parent=0 refers to "circular1: <0>".
2025-03-03 21:04:07 -07:00
Alan Modra
a8a9fc7429 objdump: is_same_section
This fixes a deficiency in commit 660df28acf, which should have used
the same logic as that in sym_ok.  Ideally both places would not
compare section names, but it can be a little tricky to match a
section in the real object file with a section in a debug file.
Extend commit 39f0547e55 to use section name, vma and size.

	* objcopy (is_same_section): New function.
	(compare_symbols, sym_ok): Use it here.
2025-03-04 08:23:16 +10:30
Alan Modra
7166166ec0 rescoff: ensure file is PE
read_coff_rsrc makes one check on object file contents, the existence
of a .rsrc section.  It doesn't check that the file is PE but blindly
accesses bfd pe_data.  Fix that by adding the necessary checks.
Also, the "resources nest too deep" error isn't an overrun, ie. the
"address out of bounds" message isn't correct.  Fix that too.
2025-03-04 08:23:16 +10:30
Alan Modra
11b6914ce6 windres: delete function forward declaraions
Most of these were not needed, and moving a few functions around
removes the need for any.
2025-03-04 08:23:16 +10:30
Nick Alcock
bf89fce0cf readelf, objdump: fix ctf dict leak
ctf_archive_next returns an opened dict, which must be closed by the caller.

Thanks to Alan Modra for spotting this.

binutils/
	* objdump.c (dump_ctf): Close dict.
	* readelf.c (dump_section_as_ctf): Likewise.
2025-02-28 14:16:29 +00:00
Jonas 'Sortie' Termansen
a3177d19fb Remove unnecessary non-standard & unportable inclusions.
<memory.h> is not needed and not standardized and is just an alias for
<string.h>.

<sys/param.h> is not needed and not standardized and contains a kitchen
sink of various unportable definitions not agreed upon and best done
manually or through other headers.

These fixes are needed to compile binutils on Sortix and other operating
systems with a strict POSIX.1-2024 libc without obsolete features.

Signed-off-by: Jonas 'Sortie' Termansen <sortie@maxsi.org>
2025-02-28 13:24:50 +00:00