From c619e92beb0784775552abe64d9a886a8cdc7236 Mon Sep 17 00:00:00 2001 From: Nick Clifton Date: Mon, 24 Feb 2025 13:44:45 +0000 Subject: [PATCH] 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 --- binutils/doc/binutils.texi | 3 ++ binutils/objdump.c | 80 +++++++++++++++++++++++++++++--------- 2 files changed, 64 insertions(+), 19 deletions(-) diff --git a/binutils/doc/binutils.texi b/binutils/doc/binutils.texi index af3ff8ab7a3..08a854fd84c 100644 --- a/binutils/doc/binutils.texi +++ b/binutils/doc/binutils.texi @@ -2817,6 +2817,9 @@ libraries. As for @option{-r}, if used with @option{-d} or @option{-D}, the relocations are printed interspersed with the disassembly. +Note: @command{objdump} does not support displaying RELR type +relocations. These can be displayed by the @command{readelf} program. + @item -s @itemx --full-contents @cindex sections, full contents diff --git a/binutils/objdump.c b/binutils/objdump.c index ebe586e0e3a..5ab29a719bc 100644 --- a/binutils/objdump.c +++ b/binutils/objdump.c @@ -5473,7 +5473,7 @@ dump_reloc_set (bfd *abfd, asection *sec, arelent **relpp, long relcount) static void dump_relocs_in_section (bfd *abfd, asection *section, - void *dummy ATTRIBUTE_UNUSED) + void *counter) { arelent **relpp; long relcount; @@ -5522,41 +5522,71 @@ dump_relocs_in_section (bfd *abfd, printf ("\n\n"); } free (relpp); + + * ((unsigned int *) counter) += 1; +} + +static void +is_relr_section (bfd *abfd ATTRIBUTE_UNUSED, + asection * section, void *data) +{ + if (section->flags & SEC_LINKER_CREATED) + return; + + struct bfd_elf_section_data * esd = elf_section_data (section); + if (esd == NULL) + return; + + if (esd->this_hdr.sh_type == SHT_RELR) + * ((bool *) data) = true; +} + +static bool +contains_relr_relocs (bfd *abfd) +{ + if (bfd_get_flavour (abfd) != bfd_target_elf_flavour) + return false; + + bool result = false; + + bfd_map_over_sections (abfd, is_relr_section, &result); + + return result; } static void dump_relocs (bfd *abfd) { - bfd_map_over_sections (abfd, dump_relocs_in_section, NULL); + unsigned int counter = 0; + + bfd_map_over_sections (abfd, dump_relocs_in_section, & counter); + + if (counter == 0 && contains_relr_relocs (abfd)) + { + printf (_("%s: This file does not contain any ordinary relocations.\n"), + sanitize_string (bfd_get_filename (abfd))); + + printf (_("%s: It does however contain RELR relocations. These can be displayed by the readelf program\n"), + sanitize_string (bfd_get_filename (abfd))); + } } static void dump_dynamic_relocs (bfd *abfd) { long relsize; - arelent **relpp; + arelent **relpp = NULL; long relcount; relsize = bfd_get_dynamic_reloc_upper_bound (abfd); printf ("DYNAMIC RELOCATION RECORDS"); - if (relsize == 0) - { - printf (" (none)\n\n"); - return; - } + if (relsize <= 0) + goto none; - if (relsize < 0) - { - relpp = NULL; - relcount = relsize; - } - else - { - relpp = (arelent **) xmalloc (relsize); - relcount = bfd_canonicalize_dynamic_reloc (abfd, relpp, dynsyms); - } + relpp = (arelent **) xmalloc (relsize); + relcount = bfd_canonicalize_dynamic_reloc (abfd, relpp, dynsyms); if (relcount < 0) { @@ -5566,7 +5596,7 @@ dump_dynamic_relocs (bfd *abfd) my_bfd_nonfatal (_("error message was")); } else if (relcount == 0) - printf (" (none)\n\n"); + goto none; else { printf ("\n"); @@ -5574,6 +5604,18 @@ dump_dynamic_relocs (bfd *abfd) printf ("\n\n"); } free (relpp); + return; + + none: + printf (" (none)\n\n"); + + if (contains_relr_relocs (abfd)) + printf (_("%s: contains RELR relocations which are not displayed by %s.\n\ +These can be displayed by the readelf program instead.\n"), + sanitize_string (bfd_get_filename (abfd)), + program_name); + + free (relpp); } /* Creates a table of paths, to search for source files. */ -- 2.47.2