Commit Graph

510 Commits

Author SHA1 Message Date
Indu Bhagat
83318af493 binutils: make read_cie aware of new augmentation char 'G'
This allows objdump/readelf to dump DWARF/EH Frame info when the stack
frame makes use of MTE tagging.

ChangeLog:

        * binutils/dwarf.c (is_aarch64_augmentation): Add handling for augmentation
	char 'G'.

---
[No change in V3]
2025-07-12 11:33:25 +02:00
Jose E. Marchesi
11d33617e0 binutils: factorize handling of arch-specific DWARF augmentations
This patch factorizes the handling of architecture/machine specific
augmentation characters in CIEs.

Based on an idea proposed by Richard Earnshaw.

binutils/ChangeLog:

	* dwarf.c (is_mach_augmentation_ftype): New type.
	(is_mach_augmentation): New variable.
	(is_nomach_augmentation): New function.
	(is_aarch64_augmentation): Likewise.
	(init_dwarf_by_elf_machine_code): Set is_mach_augmentation as
	appropriate.
	(init_dwarf_by_bfd_arch_and_mach): Likewise.
	(read_cie): Handle architecture-specific augmentation characters
	in a generic way.
2025-07-12 11:33:25 +02:00
Jose E. Marchesi
3e97860c29 binutils: generalize init_dwarf_regnames_by_* functions
This patch renames the functions:

  init_dwarf_regnames_by_elf_machine_code
  init_dwarf_regnames_by_bfd_arch_and_mach

to

  init_dwarf_by_elf_machine_code
  init_dwarf_by_bfd_arch_and_mach

The idea is to start using these functions to perform general
architecture/machine specific initializations beyond register names.

Regtested in x86_64-linux-gnu and aarch64-linux-gnu targets.

binutils/ChangeLog:

	* dwarf.c (init_dwarf_regnames_by_elf_machine_code): Rename to
	init_dwarf_by_elf_machine_code.
	(init_dwarf_regnames_by_bfd_arch_and_mach): Rename to
	init_dwarf_by_bfd_arch_and_mach.
	* dwarf.h: Adjust prototypes accordingly.
	* readelf.c (process_file_header): Adjust call to
	init_dwarf_regnames_by_elf_machine_code accordingly.
	* objdump.c (dump_dwarf): Adjust call to
	init_dwarf_regnames_by_bfd_arch_and_mach accordingly.
2025-07-12 11:33:25 +02:00
Indu Bhagat
0b1bf2fc98 bfd: gas: ld: libsframe: adopt new encoding for FDE func start addr field
This patch convenes a set of changes in bfd, gas, ld, libsframe towards
moving to the new encoding for the 'sfde_func_start_address' field in
SFrame FDE.

First, gas must now mark all SFrame sections with the new flag
SFRAME_F_FDE_FUNC_START_PCREL.  gas was already emitting the field
in the said encoding.

	* gas/gen-sframe.c (output_sframe_internal): Emit the flag
	SFRAME_F_FDE_FUNC_START_PCREL.

Similarly for ld, adopt the new semantics of sfde_func_start_address
consistently.  This means:
  - When merging SFrame sections, check that all input SFrame sections
    have the SFRAME_F_FDE_FUNC_START_PCREL flag set.  If the check
    fails, ld errors out.
  - When merging SFrame sections, keep even the in-memory contents of
    the FDE function start address (buffer passed to libsframe
    sframe_encoder_write () for writing out) encoded in the new
    semantics.  While it is, in theory, possible that instead of doing this
    change here, we adjust the value of sfde_func_start_address at the final
    write (sframe_encoder_write) time.  But latter is not favorable for
    maintenanance and may be generally confusing for developers.
  - When creating SFrame for PLT entries, emit flag
    SFRAME_F_FDE_FUNC_START_PCREL.

include/
        * sframe-api.h (SFRAME_F_LD_MUSTHAVE_FLAGS): New definition.
bfd/
	* elf-sframe.c (_bfd_elf_merge_section_sframe): Check for flag
	combinatation SFRAME_F_LD_MUSTHAVE_FLAGS set for all input and
	output SFrame sections.  If not, error out.  Also, adopt the new
        semantics of function start address encoding.
	* bfd/elfxx-x86.c (_bfd_x86_elf_create_sframe_plt): Emit flag
	SFRAME_F_FDE_FUNC_START_PCREL.

Next, for dumping SFrame sections, now that we are emitting the same
encoding in GAS, non-relocatable and relocatable SFrame links, it is the
time to set relocate to TRUE in debug_displays[].

binutils/
	* dwarf.c (struct dwarf_section_display): Allow sframe sections
	  to now be relocated.
gas/testsuite/
	* gas/cfi-sframe/cfi-sframe-aarch64-pac-ab-key-1.d: Update the
	test.  Relocatable SFrame sections now display non-zero value
	(appropriate function start address).

Now, as the SFrame sections on-disk and in-memory use the new semantics of
sfde_func_start_address encoding (i.e., function start address is the
offset from the sfde_func_start_address field to the start PC), the
calculation to make it human readable (i.e., relatable to the addresses
in .text sections) needs adjustment.

libsframe/
	* sframe-dump.c (dump_sframe_func_with_fres): Adjust the
	function start address for dumping.

Now that both the emission of the new encoding, and the relocation of
sections before dumping them is in place, it is time to adjust the
testcases.

gas/testsuite/
	* gas/cfi-sframe/cfi-sframe-aarch64-1.d: Update expected output
	to include SFRAME_F_FDE_FUNC_START_PCREL instead of NONE.
	* gas/cfi-sframe/cfi-sframe-aarch64-2.d: Likewise.
	* gas/cfi-sframe/cfi-sframe-aarch64-3.d: Likewise.
	* gas/cfi-sframe/cfi-sframe-aarch64-4.d: Likewise.
	* gas/cfi-sframe/cfi-sframe-common-1.d: Likewise.
	* gas/cfi-sframe/cfi-sframe-common-10.d: Likewise.
	* gas/cfi-sframe/cfi-sframe-common-11.d: Likewise.
	* gas/cfi-sframe/cfi-sframe-common-2.d: Likewise.
	* gas/cfi-sframe/cfi-sframe-common-3.d: Likewise.
	* gas/cfi-sframe/cfi-sframe-common-4.d: Likewise.
	* gas/cfi-sframe/cfi-sframe-common-5.d: Likewise.
	* gas/cfi-sframe/cfi-sframe-common-6.d: Likewise.
	* gas/cfi-sframe/cfi-sframe-common-7.d: Likewise.
	* gas/cfi-sframe/cfi-sframe-common-8.d: Likewise.
	* gas/cfi-sframe/cfi-sframe-common-9.d: Likewise.
	* gas/cfi-sframe/cfi-sframe-x86_64-1.d: Likewise.
	* gas/cfi-sframe/cfi-sframe-x86_64-2.d: Likewise.
	* gas/cfi-sframe/cfi-sframe-x86_64-empty-1.d: Likewise.
	* gas/cfi-sframe/cfi-sframe-x86_64-empty-2.d: Likewise.
	* gas/cfi-sframe/cfi-sframe-x86_64-empty-3.d: Likewise.
	* gas/cfi-sframe/cfi-sframe-x86_64-empty-4.d: Likewise.
	* gas/cfi-sframe/common-empty-1.d: Likewise.
	* gas/cfi-sframe/common-empty-2.d: Likewise.
	* gas/cfi-sframe/common-empty-3.d: Likewise.
	* gas/scfi/x86_64/scfi-cfi-sections-1.d: Likewise.
	* gas/scfi/x86_64/scfi-dyn-stack-1.d: Likewise.
ld/testsuite/
	* ld-aarch64/sframe-simple-1.d: Update expected output to
	include SFRAME_F_FDE_FUNC_START_PCREL.
	* ld-x86-64/sframe-ibt-plt-1.d: Likewise.
	* ld-x86-64/sframe-plt-1.d: Likewise.
	* ld-x86-64/sframe-pltgot-1.d: Likewise.
	* ld-x86-64/sframe-pltgot-2.d: Likewise.
	* ld-x86-64/sframe-simple-1.d: Likewise.

Naturally, the change of semantics for 'SFrame FDE function start address'
has consequences on the implementation in libsframe.  As per the new
semantics:
  - Function start address in the SFrame FDE (sfde_func_start_address)
    is an offset from the FDE function start address field to the start
    PC of the associated function.

Note that, the libsframe library brings the SFrame section contents into
its own memory to create a sframe_decoder_ctx object via sframe_decode
().  Many internal and user-interfacing APIs then may use
sframe_decoder_ctx object to interact and fulfill the work.

In context of changing semantics for sfde_func_start_address, following
relevant examples may help understand the impact:
  - sframe_find_fre () finds a the SFrame stack trace data (SFrame FRE)
    given a lookup offset (offset of lookup_pc from the start of SFrame
    section).  Now that the sfde_func_start_address includes the
    distance from the sfde_func_start_address field to the start of
    SFrame section itself, the comparison checks of
    sfde_func_start_address with the incoming lookup offset need
    adjustment.
  - Some internal functions (sframe_get_funcdesc_with_addr_internal ()
    finds SFrame FDE by using binary seach comparing
    sfde_func_start_address fields, etc.) need adjustments.
  - sframe_encoder_write () sorts the SFrame FDEs before writing out
    the SFrame data.  Sorting of SFrame FDE via the internal function
    sframe_sort_funcdesc() needs adjustments: the new encoding of
    sfde_func_start_address means the distances are not from the same
    anchor, so cannot be sorted directly.

This patch takes the approach of adding a new internal function:
  - sframe_decoder_get_secrel_func_start_addr (): This function returns
    the offset of the start PC of the function from the start of SFrame
    section, i.e., it gives a section-relative offset.

As the sframe_decoder_get_secrel_func_start_addr () API needs the value
of the function index in the FDE list, another internal API needs
sframe_fre_check_range_p () adjustments too.

Sorting the FDEs (via sframe_sort_funcdesc ()) is done by first bringing
all offsets in sfde_func_start_address relative to start of SFrame
section, followed by sorting, and then readjusting the offsets accroding
to the new position in the FDE list.

libsframe/
	* sframe.c (sframe_decoder_get_secrel_func_start_addr): New
	static function.
        (sframe_fre_check_range_p): Adjust the interface a bit.
	(sframe_get_funcdesc_with_addr_internal): Use
	sframe_decoder_get_secrel_func_start_addr () when comparing
	sfde_func_start_address with user input offset.
        (sframe_find_fre): Adopt the new semantics.
        (sframe_sort_funcdesc): Likewise.

For the libsframe testsuite, use the new encoding for FDE func start
addr: distance between the FDE sfde_func_start_address field and the
start PC of the function itself.

Use SFRAME_F_FDE_FUNC_START_PCREL flag, though the sframe_encode ()
interface in libsframe applies no sanity checks for the encoding itself.

libsframe/testsuite/
	* libsframe.find/findfre-1.c: Adjust to use the new
	SFRAME_F_FDE_FUNC_START_PCREL specific encoding.
	* libsframe.find/findfunc-1.c: Likewise.
	* libsframe.find/plt-findfre-1.c: Likewise.
	* libsframe/testsuite/libsframe.decode/DATA2: Update data file
	due to usage of new SFRAME_F_FDE_FUNC_START_PCREL flag.
	* libsframe/testsuite/libsframe.encode/encode-1.c: Use flag
	SFRAME_F_FDE_FUNC_START_PCREL.
2025-07-06 12:53:03 -07:00
Indu Bhagat
c915c2c95a objdump, readelf: sframe: apply relocations before textual dump
PR libsframe/32589 - function start address is zero in SFrame section dump

Currently, readelf and objdump display the SFrame sections in ET_REL
object files with function start addresses of each function as 0.  This
makes it difficult to correlate SFrame stack trace information with the
individual functions in the object file.

For objdump, use the dump_dwarf () interface to dump SFrame section.
Similarly, for readelf, use the display_debug_section () interface to
dump SFrame section.  These existing interfaces (for DWARF debug
sections) already support relocating the section contents before
dumping, so lets use them for SFrame sections as well.

When adding a new entry for SFrame in debug_option_table[], use char
'nil' and the option name of "sframe-internal-only".  This is done so
that there is no additional (unnecessary) user-exposed ways of dumping
SFrame sections.  Additionally, we explicitly disallow the
"sframe-internal-only" from external/user input in --dwarf (objdump).
Similarly, "sframe-internal-only" is explicitly matched and disallowed
from --debug-dump (readelf).

For objdump and readelf, we continue to keep the same error messaging as
earlier:

  $ objdump --sframe=sframe bubble_sort.o
  ...
  No sframe section present

  $ objdump --sframe=.sfram bubble_sort.o
  ...
  No .sfram section present

  $ objdump --sframe=sframe-internal-only sort
  ...
  No sframe-internal-only section present

Similarly for readelf:

  $ readelf --sframe= bubble_sort.o
  readelf: Error: Section name must be provided
  $ readelf --sframe=.sfram bubble_sort.o
  readelf: Warning: Section '.sfram' was not dumped because it does not exist
  $ readelf --sframe=sframe bubble_sort.o
  readelf: Warning: Section 'sframe' was not dumped because it does not exist

PS: Note how this patch adds a new entry to debug_displays[] with a
    relocate value set to FALSE.  This will be set to TRUE in a subsequent
    patch ("bfd: gas: ld: libsframe: emit func start addr field as an offset
    from FDE") when fixes are made to emit the value of the
    'sfde_func_start_address' field in the new encoding
    SFRAME_F_FDE_FUNC_START_PCREL across gas and ld.

binutils/
	* dwarf.c (display_sframe): New definition.
	(dwarf_select_sections_all): Enable SFrame section too.
	(struct dwarf_section_display): Add entry for SFrame section.
	* dwarf.h (enum dwarf_section_display_enum): Add enumerator for
	SFrame.
	* objdump.c (dump_section_sframe): Remove.
	(dump_sframe_section): Add new definition.
	(dump_bfd): Use dump_sframe_section.
	* binutils/readelf.c (dump_section_as_sframe): Remove.
2025-07-06 12:53:03 -07:00
Vladimir Mezentsev
88c66eca24 Fix typos in binutils/dwarf.c
binutils/ChangeLog
2025-06-29  Vladimir Mezentsev  <vladimir.mezentsev@oracle.com>

	* dwarf.c: Change "/usrlib64/debug/usr" to "/usr/lib64/debug/usr/" and
	.gun_debugaltlink to .gnu_debugaltlink.
2025-06-30 00:25:34 -07: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
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
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
Matthieu Longo
c4ab4dd7bd aarch64 DWARF: add new CFI directive for PAuth_LR
This patch adds a new CFI directive (cfi_negate_ra_state_with_pc) which
set an additional bit in the RA state to inform that RA was signed with
SP but also PC as an additional diversifier.

RA state | Description
0b00     | Return address not signed (default if no cfi_negate_ra_state*)
0b01     | Return address signed with SP (cfi_negate_ra_state)
0b10     | Invalid state
0b11     | Return address signed with SP+PC (cfi_negate_ra_state_with_pc)

Approved-by: Indu Bhagat <indu.bhagat@oracle.com>
Approved-by: Jan Beulich <jbeulich@suse.com>
2025-01-14 10:53:21 +00:00
Alan Modra
94e2b2a770 readelf memory leaks
This fixes multiple readelf memory leaks:
- The check functions used to validate separate debug info files
  opened and read file data but didn't release the memory nor close
  the file.
- A string table was being re-read into a buffer, leaking the old
  contents.
- Decompressed section contents leaked.

	* dwarf.c (check_gnu_debuglink): Always call close_debug_file.
	(check_gnu_debugaltlink): Likewise.
	* readelf.c (process_section_headers): Don't read string_table
	again if we already have it.
	(maybe_expand_or_relocate_section): Add decomp_buf param to
	return new uncompressed buffer.
	(dump_section_as_strings, filedata->string_table): Free any
	uncompressed buffer.
	(process_file): Call close_debug_file rather than freeing
	various filedata components.
2025-01-01 22:52:59 +10:30
Alan Modra
e8e7cf2abe Update year range in copyright notice of binutils files 2025-01-01 18:29:57 +10:30
Alexandra Hájková
d05b8f0180 display_lang: Add descriptions for post DWARF5 constants
Describe all the new post DWARF5 language codes from the latest sync
of include/dwarf.h with gcc.
2024-12-14 16:06:46 +01:00
Lulu Cai
3d75969bd0 LoongArch: Assign DWARF register numbers to register aliases
.cfi directives only support the use of register numbers and not
register names or aliases.

This commit adds support for 4 formats, for example:
  .cfi_offset r1, 8
  .cfi_offset ra, 8
  .cfi_offset $r1,8
  .cfi_offset $ra,8

The above .cfi directives are equivalent and all represent dwarf
register number 1.

Display register aliases as specified in the psABI during disassembly.
2024-12-09 12:01:02 +08:00
oltolm
227146c286 Remove more remnants of old Mach-O workaround
Remove another adjustment for section address, this time for the
offset into .debug_str{,.dwo} read from .debug_str_offsets{,.dwo} by
fetch_indexed_string.

Signed-off-by: oltolm <oleg.tolmatcev@gmail.com>
2024-12-01 16:41:11 +10:30
Alan Modra
86b26b453f Replace uses of asprintf with xasprintf
xasprintf has a nicer interface and behaves like xmalloc as far as
memory is concerned, ie. no need to check a return status and the
program exits with an error on OOM.

binutils/
	* dwarf.c (load_debug_sup_file): Replace asprintf with xasprintf.
	* nm.c (get_elf_symbol_type, get_coff_symbol_type): Likewise.
	* objdump.c (dump_ctf_indent_lines): Likewise.
	* readelf.c (display_lto_symtab, dump_ctf_indent_lines): Likewise.
	* windres.c (main): Likewise.
	* configure.ac: Remove asprintf from AC_CHECK_DECLS.
	* config.in: Regenerate.
	* configure: Regenerate.
gas/
	* config/tc-kvx.c (kvx_emit_single_noop): Simplify.
	* config/tc-riscv.c (md_assemblef): Replace asprintf with xasprintf.
	* read.c (s_nop, do_s_func): Likewise.
	* stabs.c (stabs_generate_asm_func): Likewise.
	(stabs_generate_asm_endfunc): Likewise.
	* configure.ac: Remove asprintf from AC_CHECK_DECLS.
	* config.in: Regenerate.
	* configure: Regenerate.
ld/
	* ldlang.c (lang_leave_overlay_section): Replace xmalloc+sprintf
	with xasprintf.  Localise vars.
	* lexsup.c (parse_args): Replace asprintf with xasprintf.
	* pe-dll.c (make_head, make_tail, make_one): Likewise.
	(make_singleton_name_thunk, make_import_fixup_entry): Likewise.
	(make_runtime_pseudo_reloc): Likewise.
	(pe_create_runtime_relocator_reference): Likewise.
	* configure.ac: Remove asprintf from AC_CHECK_DECLS.
	* config.in: Regenerate.
	* configure: Regenerate.
2024-10-24 17:58:00 +10:30
Alan Modra
f2a15e7cde get_type_abbrev_from_form tidy
* dwarf.c (get_type_abbrev_from_form): Make uvalue param a
	uint64_t.  Localise variables.  Don't bother clearing *data_return
	and *addrev_num_return for a NULL return value.
2024-08-29 21:05:57 +09:30
Alan Modra
479c611636 skip_attr_bytes assertion (data) <= (end) fail
get_type_abbrev_from_form is lax in not limiting data for a uleb to
the current CU, because DW_FORM_ref_addr allows access to other CU's
data.  This can lead to an assertion fail when skipping or reading
attributes in get_type_signedness.

	* dwarf.c (get_type_abbrev_from_form): Limit uleb data to map end
	for ref_addr, cu_end otherwise.
2024-08-01 21:52:34 +09:30
Alan Modra
4b8c2aaf2d Assertion `(data) <= (end)' failed in read_bases
* dwarf.c (skip_attribute): Don't increment data past end.
	Use SKIP_{S,U}LEB rather than READ_{S,U}LEB.
2024-06-30 14:41:37 +09:30
Alan Modra
1b19566b18 PR31898 bug in processing DW_RLE_startx_endx
PR 31898
	* dwarf.c (display_debug_rnglists_list): Correct fetch of "end"
	indexed address.  Remove excess parens.
2024-06-17 15:57:50 +09:30
Tom de Vries
e548840a03 [binutils/readelf] Fix handling of DW_MACRO_define_strx in dwo file
When printing a DW_MACRO_define_strx entry in a .debug_macro.dwo section, we
run into:
...
 DW_MACRO_define_strx lineno : 0 macro : <no .debug_str_offsets section>
...

Fix this in display_debug_macro by passing the correct dwo argument to a
fetch_indexed_string call.

That works fine for readelf -w, with with readelf -wm we have:
...
 DW_MACRO_define_strx lineno : 0 macro : <no .debug_str_offsets.dwo section>
...

Fix this in display_debug_macro by doing load_debug_section_with_follow for
str_dwo / str_index_dwo sections instead of str / str_index sections when
handling .debug_macro.dwo.

PR 31735
2024-05-15 09:45:55 +02:00
Tom de Vries
7e4f1f4bc7 [binutils/readelf] Fix printing of dwarf4 .debug_str_offsets.dwo
When compiling a hello world with dwarf4 split dwarf:
...
$ gcc -gdwarf-4 -gsplit-dwarf hello.c -save-temps -dA
...
we have in a-hello.s these three initial entries in .debug_str_offsets:
...
	.section        .debug_str_offsets.dwo,"e",@progbits
	.4byte  0       // indexed string 0x0: short int
	.4byte  0xa     // indexed string 0x1: /home/vries/binutils
	.4byte  0x1f    // indexed string 0x2: main
...
but "readelf -ws a.out" starts at the third entry:
...
Contents of the .debug_str_offsets.dwo section (loaded from a-hello.dwo):

    Length: 0x30
       Index   Offset [String]
           0 00000000  main
...

This is a regression since commit 407115429b ("Modified changes for
split-dwarf and dwarf-5."), which introduced a variable
debug_str_offsets_hdr_len in display_debug_str_offsets.

Fix this by setting display_debug_str_offsets to 0 for the dwarf4 case.

PR 31734
2024-05-15 09:45:55 +02:00
Nick Clifton
2b34e06765 Add missing \n to the end of warning messages in dwarf.c.
PR 31722
2024-05-10 13:23:41 +01:00
Alan Modra
0f8adbf77d Re: Fix null pointer dereference in process_debug_info()
read_bases has a potential null-pointer deref too, and without a
debug_info_p there isn't any point in calling read_bases.

	* dwarf.c (process_debug_info): Don't call read_bases when
	debug_info_p is NULL.
2024-04-12 09:00:52 +09:30
Nick Clifton
301bfc45ab Fix null pointer dereference in process_debug_info() 2024-04-03 11:19:33 +01:00
Nick Clifton
709b8d140b Fix seg-fault in the DWARF reader code when accessing an abbreviatuin table with a corrupt entry offset.
PR 31456
2024-03-19 14:16:47 +00:00
Jan Beulich
a40a04601f x86: also permit YMM/ZMM use in CFI directives
Next to code using %ymm<N> or %zmm<N> it is more natural to have .cfi_*
directives also reference those, not the corresponding %xmm<N>. Accept
their names as kind of aliases, i.e. resolving to the same numbers.

While extending the respective 64-bit testcase, also add %bnd<N> there
(should have happened right with 633789901c ["x86-64: Dwarf2 register
numbers for %bnd<N>"], sorry), requiring binutils/dwarf.c to be adjusted
accordingly as well.
2024-02-23 11:59:09 +01:00
Alan Modra
e14f7abaf8 Assertion failure dumping .eh_frame_hdr
dwarf.c can hit "Assertion '(start) <= (end)' failed" on truncated
sections, due to get_encoded_eh_value wrongly returning a full count
for truncated words.

	* dwarf.c (get_encoded_eh_value): Return zero for truncated words.
2024-01-26 17:19:45 +10:30
Nick Clifton
1878f44b70 Update readelf's and objdump's debug frame displaying feature to include the contents of the .eh_frame_hdr section, if present. 2024-01-19 14:39:08 +00:00
Alan Modra
fd67aa1129 Update year range in copyright notice of binutils files
Adds two new external authors to etc/update-copyright.py to cover
bfd/ax_tls.m4, and adds gprofng to dirs handled automatically, then
updates copyright messages as follows:

1) Update cgen/utils.scm emitted copyrights.
2) Run "etc/update-copyright.py --this-year" with an extra external
   author I haven't committed, 'Kalray SA.', to cover gas testsuite
   files (which should have their copyright message removed).
3) Build with --enable-maintainer-mode --enable-cgen-maint=yes.
4) Check out */po/*.pot which we don't update frequently.
2024-01-04 22:58:12 +10:30
Alan Modra
e838a672aa Re: readelf..debug-dump=loc displays bogus base addresses
Commit b05efa39b4 removed checks I added in commit f22f27f46c to
prevent segfaults when debug_info_p is NULL, which can be the case
with fuzzed objects.  Restore those checks.  Also, for dwo look at
rnglists_dwo rather than rnglists.
2023-12-15 12:56:45 +10:30
Alan Modra
1024d59cb2 memory leak in display_debug_addr
* dwarf.c (display_debug_addr): Free dummy debug_addr_info entry.
	Don't return without freeing debug_addr_info on error paths.
2023-12-05 18:20:51 +10:30
Tom Tromey
e0a874120a Fix two buglets in .debug_names dumping
While working on gdb's .debug_names writer, I found a couple of small
bugs in binutils .debug_names dumping.

First, the DWARF spec (section 6.1.1.4.6 Name Table) says:

    These two arrays are indexed starting at 1, [...]

I think it is clearer for binutils to follow this, particularly
because DW_IDX_parent refers to this number.

Second, I think the handling of an empty hash table is slightly wrong.
Currently the dumping code assumes there is always an array of hashes.
However, section 6.1.1.4.5 Hash Lookup Table says:

    The optional hash lookup table immediately follows the list of
    type signatures.

and then:

    The hash lookup table is actually two separate arrays: an array of
    buckets, followed immediately by an array of hashes.

My reading of this is that the hash table as a whole is optional, and
so the hashes will not exist in this case.  (This also makes sense
because the hashes are not useful without the buckets anyway.)

This patch fixes both of these problems.  FWIW I have some gdb patches
in progress that change gdb both to omit the hash table and to use
DW_IDX_parent.

2023-12-04  Tom Tromey  <tom@tromey.com>

	* dwarf.c (display_debug_names): Handle empty .debug_names hash
	table.  Name entries start at 1.
2023-12-04 11:20:44 -07:00
Jan Beulich
236b484749 binutils/Dwarf: avoid "shadowing" of glibc function name
Yet once again: Old enough glibc has an (unguarded) declaration of
index() in string.h, which triggers a "shadows a global declaration"
warning with at least some gcc versions.
2023-12-01 10:13:17 +01:00
Tom Tromey
8116169676 Restore .gdb_index v9 display in readelf
An earlier patch (commit b05efa39 "readelf..debug-dump=loc displays
bogus base addresses") inadvertently removed support for displaying
.gdb_index v9 sections.

This patch corrects the oversight.  I tested this by using readelf on
an appropriate file.

	* dwarf.c (display_gdb_index): Restore v9 display code.
2023-11-20 09:31:32 -07:00
Vsevolod Alekseyev
b05efa39b4 readelf..debug-dump=loc displays bogus base addresses
PR 30880
  * dwarf.c (read_and_display_attr_value): Fix loclist handling. (display_loclists_list): Likewise.
2023-11-10 15:26:48 +00:00
Tom de Vries
07a9e70911 [readelf] Handle unknown name of main in .gdb_index section
When compiling hello world and adding a v9 .gdb-index section:
...
$ gcc -g hello.c
$ gdb-add-index a.out
...
readelf shows it as:
...
Shortcut table:
Language of main: unknown: 0
Name of main: ^A
...

The documentation of gdb says about the "Name of main" that:
...
This value must be ignored if the value for the language of main is zero.
...

Implement this approach in display_gdb_index, such that we have instead:
...
Shortcut table:
Language of main: unknown: 0
Name of main: <unknown>
...

Tested on x86_64-linux.

Approved-By: Jan Beulich <jbeulich@suse.com>
2023-10-24 12:35:08 +02:00
Alan Modra
f22f27f46c asan: null dereference in read_and_display_attr_value
This fixes multiple places in read_and_display_attr_value dealing with
range and location lists that can segfault when debug_info_p is NULL.
Fuzzed object files can contain arbitrary DW_FORMs.

	* dwarf.c (read_and_display_attr_value): Don't dereference NULL
	debug_info_p.
2023-10-10 22:17:29 +10:30
Tom de Vries
9847ba8f08 [readelf] Handle .gdb_index section version 9
Add the abilitity to print a v9 .gdb_index section.

The v9 section contains an extra table, which is printed as follows:
...
Shortcut table:
Language of main: Fortran 95
Name of main: contains_keyword
...

[ For the example, I used the exec of gdb test-case
gdb.fortran/nested-funcs-2-exp when running the test-case with target board
cc-with-gdb-index. ]

Tested on x86_64-linux.

Approved-By: Nick Clifton <nickc@redhat.com>
2023-10-10 10:26:40 +02:00
Vsevolod Alekseyev
8c7125feaa Fix: readelf..info misreports DW_FORM_loclistx, DW_FORM_rnglistx
PR 29267
  * dwarf.c (fetch_indexed_value): Delete. (fetch_indexed_offset): Correct base address calculation. (read_and_display_attr_value): Replace uses of fetch_indexed_value with fetch_indexed_offset.
2023-10-03 09:27:27 +01:00
Vsevolod Alekseyev
84102ebc29 Fix readelf's display of dwarf v5 range lists
PR 30792
  * dwarf.h (struct debug_info): Remove range_versions field.
  * dwarf.c (fetch_indexed_offset): New function. (read_and_display_attr_value): Use it for DW_FORM_rnglistx. Remove code to initialise range_versions. (skip_attribute): New function. (read_bases): Read and reccord all range and address bases in a CU. (process_debug_info): Call read_bases. (display_debug_rnglists): Rename to display_debug_rnglists_unit_header and only display the range list header information. (display_debug_ranges): Adjust.
2023-09-25 18:01:31 +01:00
Alan Modra
0283863037 binutils/dwarf.c abbrev list leak
* dwarf.c (process_debug_info): Call free_abrev_list on
	return paths.
2023-08-30 11:22:23 +09:30
Alan Modra
a422bb9db1 Re: readelf/objdump: Handle DWARF info with mixed types of range section
PR 30791
	* dwarf.c (free_debug_information): Free range_versions.
2023-08-30 11:22:23 +09:30
Nick Clifton
589d38af0d readelf/objdump: Handle DWARF info with mixed types of range section.
PR 30791
  * dwarf.h (debug_info): Add range_versions field.
  * dwarf.c (read_and_display_attr_value): When recording a range arribute also ecord the dwarf version number.
  (is_range_list_for_this_section): New function.
  (display_debug_ranges): Only show debug ranges whose version is suitable for the secction being displayed.
2023-08-23 11:36:25 +01:00
Alan Modra
8032f75b29 objdump: file name table entry count check
Fuzzers have found that objdump -W takes a really long time if
the entry count uleb is ridiculously large, and format attributes
don't consume data (which doesn't make sense for a table of names).

	* dwarf.c (display_formatted_table): Sanity check count of
	table entries.
2023-08-22 11:41:37 +09:30
Alan Modra
ca5321d49a readelf/objdump remember_state memory leaks
* dwarf.c (display_debug_frames <DW_CFA_restore_state>): Do free
	invalid remember_state.
2023-06-09 12:56:12 +09:30
Alan Modra
329dd2b6fc ubsan: signed integer overflow in display_debug_lines_raw
This one was caused by me unnecessarily promoting an "int adv" to
"int64_t adv".  The expression overflowing was 4259 + 9223372036854775807
with the left number being unsigned int.

	* dwarf.h (DWARF2_Internal_LineInfo): Replace unsigned short
	with uint16_t and unsigned char with uint8_t.  Make li_line_base
	an int8_t.
	* dwarf.c (display_debug_lines_raw): Revert "adv" back to an int.
2023-04-20 09:03:53 +09:30
Nick Clifton
28ab94f51d Fix: readelf: loc_offset XX too big
PR 30355
  * dwarf.c (read_and_display_attr_value): Correctly handle DW_loclistx attributes that index a version 5 .debug_loclists section.
2023-04-19 11:48:24 +01:00