Commit Graph

4765 Commits

Author SHA1 Message Date
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
Nick Clifton
c619e92beb objdump: Inform users if RELR relocs are present in a file when using the -r or -R options and no regular relocs are present.
PR 32459
2025-02-24 13:44:45 +00:00
Jan Beulich
2b0a62b17f IQ2000: drop maintainer
After I found his email bouncing, Stan, via private communication which
Nick helped with, has indicated that - having retired - he won't any
longer fulfill the maintainer role here.
2025-02-21 10:28:46 +01:00
Tom Tromey
3fddd4e758 Fix "compilation unit" matching in dwarf-font-lock-keywords
Today I learned that, at least on my system (Fedora 40), the printf
"%#x" format will produce "0" rather than "0x0" when given 0 as an
argument.

This causes dwarf-mode.el to not correctly fontify the very first
"Compilation Unit" line it sees.

This patch adapts dwarf-mode.el.  As always, this patch bumps the
version number for easier installation.

I am checking this in.
2025-02-20 09:45:25 -07:00
Alan Modra
cb1861cb8f bintuils/dwarf.c indentation fixes
plus a few other formatting fixes.
2025-02-19 23:11:23 +10:30
Alan Modra
e51fdff7d2 binutils/dwarf.c debug_information leak
It is possible with fuzzed files to have num_debug_info_entries zero
after allocating space for debug_information, leading to multiple
allocations.

	* dwarf.c (process_debug_info): Don't test num_debug_info_entries
	to determine whether debug_information has been allocated,
	test alloc_num_debug_info_entries.
2025-02-19 23:03:11 +10:30
Alan Modra
ba6ad3a18c PR32716, objdump -i memory leak
PR binutils/32716
	* bucomm.c (display_info): Free arg.info.
2025-02-19 08:01:13 +10:30
Alan Modra
619f863c55 dlltool memory leaks
dlltool copies strings with strdup all over the place, seeming to take
the attitude that anything might be modified.  That leads to lots of
memory leaks.  Fixing the leaks by removing the strdup calls of course
means you need to take good care that strings *aren't* modified.  This
isn't as easy as it sounds due to functions like xlate that have
const char* params but then manage to modify the strings.  I've fixed
xlate, but if I've missed something somewhere then this patch likely
will break dlltool.  Testsuite coverage of dlltool isn't good.

The leaks in defparse.y are small.  It also is a little work to verify
that all the strings I'm freeing in defparse.y are in fact malloc'd,
which is no doubt why the leaks are there.

Using bfd_xalloc in make_one_lib_file and functions called from there
results in memory being freed automatically at the bfd_close in
make_one_lib_file, without any fuss.

The patch also makes use of xasprintf to replace xmalloc followed by
sprintf.

	* defparse.y (opt_name2): Free incoming ID strings after
	adding prefix/suffix.
	* dlltool.c (struct ifunct): Constify char* fields.
	(struct iheadt, struct dlist): Likewise.
	(set_dll_name_from_def,	def_heapsize, def_stacksize),
	(def_section, assemble_file): Use xasprintf.
	(def_name, def_library): Free dll_name and name.
	(def_description, new_directove): Don't strdup incoming args.
	(append_import): Likewise.
	(def_import): Free module after appending dllext.
	(run): Free temp_base.
	(scan_filtered_symbols): Don't segfault on NULL strchr return.
	Remove unnecessary strdup.
	(scan_drectve_symbols): Likewise.  Constify pointers.
	Use bfd_malloc_and_get_section.  Use xmemdup.
	(add_excludes): Use xasprintf and xstrdup.
	(gen_exp_file): Free xlate return.  Constify pointer to suit
	struct changes.  Free copy.
	(xlate): Always copy arg.  Use xasprintf and xstrdup.
	(make_imp_label): Add bfd arg.  Use bfd_xalloc.
	(gen_lib_file): Adjust to suit.
	(make_one_lib_file): Likewise.  Use bfd_xalloc for section data
	and relocs.  Simplify code calling xlate, and free xlate return.
	(dll_name_list_free_contents): Flatten recursion.
	(mangle_defs): Free d_export_vec.
	(main): Formatting.  Use xasprintf.
	* resres.c (write_res_id): Free section data.
2025-02-14 09:11:23 +10:30
Alan Modra
dd39a8c388 Fix typo in objdump info/man page 2025-02-10 09:03:06 +10:30
Nick Clifton
2470de6c0c Update with latest changes to src-release.sh 2025-02-04 11:50:32 +00:00
Nick Clifton
f5d5d53e80 Remove a couple of entries in the binutils MAINTAINERS file 2025-01-30 16:01:02 +00:00