[2/3] gdb: Have 'maint info sections' print all sections again

Message ID 2263b09864c9ffa3db462c222c5febf6cb39f40b.1567353237.git.andrew.burgess@embecosm.com
State New, archived
Headers

Commit Message

Andrew Burgess Sept. 1, 2019, 4:08 p.m. UTC
  In this commit:

  commit 6eac171f0624303d944ff1a1ae4d0e3b0a63c800
  Date:   Fri Aug 16 00:25:14 2019 +0200

      [gdb] Make maint info sections print relocated addresses

A couple of things broke with the 'maintenance info sections' command,
here is some before output:

  (gdb) maintenance info sections
  Exec file:
      `/path/to/gdb/build/gdb/testsuite/outputs/gdb.base/maint/maint',
          file type elf64-x86-64.
   [0]     0x00400238->0x00400254 at 0x00000238: .interp ALLOC LOAD READONLY DATA HAS_CONTENTS
   [1]     0x00400254->0x00400274 at 0x00000254: .note.ABI-tag ALLOC LOAD READONLY DATA HAS_CONTENTS
   [2]     0x00400274->0x00400298 at 0x00000274: .note.gnu.build-id ALLOC LOAD READONLY DATA HAS_CONTENTS
   [3]     0x00400298->0x004002bc at 0x00000298: .gnu.hash ALLOC LOAD READONLY DATA HAS_CONTENTS
   [4]     0x004002c0->0x00400380 at 0x000002c0: .dynsym ALLOC LOAD READONLY DATA HAS_CONTENTS
   [5]     0x00400380->0x004003e3 at 0x00000380: .dynstr ALLOC LOAD READONLY DATA HAS_CONTENTS
   [6]     0x004003e4->0x004003f4 at 0x000003e4: .gnu.version ALLOC LOAD READONLY DATA HAS_CONTENTS
   [7]     0x004003f8->0x00400418 at 0x000003f8: .gnu.version_r ALLOC LOAD READONLY DATA HAS_CONTENTS
   [8]     0x00400418->0x00400460 at 0x00000418: .rela.dyn ALLOC LOAD READONLY DATA HAS_CONTENTS
   [9]     0x00400460->0x004004c0 at 0x00000460: .rela.plt ALLOC LOAD READONLY DATA HAS_CONTENTS
   [10]     0x004004c0->0x004004d7 at 0x000004c0: .init ALLOC LOAD READONLY CODE HAS_CONTENTS
   [11]     0x004004e0->0x00400530 at 0x000004e0: .plt ALLOC LOAD READONLY CODE HAS_CONTENTS
   [12]     0x00400530->0x00400802 at 0x00000530: .text ALLOC LOAD READONLY CODE HAS_CONTENTS
   [13]     0x00400804->0x0040080d at 0x00000804: .fini ALLOC LOAD READONLY CODE HAS_CONTENTS
   [14]     0x00400810->0x0040084e at 0x00000810: .rodata ALLOC LOAD READONLY DATA HAS_CONTENTS
   [15]     0x00400850->0x004008c4 at 0x00000850: .eh_frame_hdr ALLOC LOAD READONLY DATA HAS_CONTENTS
   [16]     0x004008c8->0x00400ab8 at 0x000008c8: .eh_frame ALLOC LOAD READONLY DATA HAS_CONTENTS
   [17]     0x00600e00->0x00600e08 at 0x00000e00: .init_array ALLOC LOAD DATA HAS_CONTENTS
   [18]     0x00600e08->0x00600e10 at 0x00000e08: .fini_array ALLOC LOAD DATA HAS_CONTENTS
   [19]     0x00600e10->0x00600ff0 at 0x00000e10: .dynamic ALLOC LOAD DATA HAS_CONTENTS
   [20]     0x00600ff0->0x00601000 at 0x00000ff0: .got ALLOC LOAD DATA HAS_CONTENTS
   [21]     0x00601000->0x00601038 at 0x00001000: .got.plt ALLOC LOAD DATA HAS_CONTENTS
   [22]     0x00601038->0x0060103c at 0x00001038: .data ALLOC LOAD DATA HAS_CONTENTS
   [23]     0x00601040->0x006012c8 at 0x0000103c: .bss ALLOC
   [24]     0x00000000->0x0000002c at 0x0000103c: .comment READONLY HAS_CONTENTS
   [25]     0x00000000->0x00000060 at 0x00001068: .debug_aranges READONLY HAS_CONTENTS
   [26]     0x00000000->0x0000061b at 0x000010c8: .debug_info READONLY HAS_CONTENTS
   [27]     0x00000000->0x00000264 at 0x000016e3: .debug_abbrev READONLY HAS_CONTENTS
   [28]     0x00000000->0x000001e6 at 0x00001947: .debug_line READONLY HAS_CONTENTS
   [29]     0x00000000->0x00000487 at 0x00001b2d: .debug_str READONLY HAS_CONTENTS
  (gdb)

And here is the output after the above commit:

  (gdb) maintenance info sections
  +maintenance info sections
  Exec file:
      `/path/to/gdb/build/gdb/testsuite/outputs/gdb.base/maint/maint',
          file type elf64-x86-64.
      0x00400238->0x00400254 at 0x00000238: .interp ALLOC LOAD READONLY DATA HAS_CONTENTS
      0x00400254->0x00400274 at 0x00000254: .note.ABI-tag ALLOC LOAD READONLY DATA HAS_CONTENTS
      0x00400274->0x00400298 at 0x00000274: .note.gnu.build-id ALLOC LOAD READONLY DATA HAS_CONTENTS
      0x00400298->0x004002bc at 0x00000298: .gnu.hash ALLOC LOAD READONLY DATA HAS_CONTENTS
      0x004002c0->0x00400380 at 0x000002c0: .dynsym ALLOC LOAD READONLY DATA HAS_CONTENTS
      0x00400380->0x004003e3 at 0x00000380: .dynstr ALLOC LOAD READONLY DATA HAS_CONTENTS
      0x004003e4->0x004003f4 at 0x000003e4: .gnu.version ALLOC LOAD READONLY DATA HAS_CONTENTS
      0x004003f8->0x00400418 at 0x000003f8: .gnu.version_r ALLOC LOAD READONLY DATA HAS_CONTENTS
      0x00400418->0x00400460 at 0x00000418: .rela.dyn ALLOC LOAD READONLY DATA HAS_CONTENTS
      0x00400460->0x004004c0 at 0x00000460: .rela.plt ALLOC LOAD READONLY DATA HAS_CONTENTS
      0x004004c0->0x004004d7 at 0x000004c0: .init ALLOC LOAD READONLY CODE HAS_CONTENTS
      0x004004e0->0x00400530 at 0x000004e0: .plt ALLOC LOAD READONLY CODE HAS_CONTENTS
      0x00400530->0x00400802 at 0x00000530: .text ALLOC LOAD READONLY CODE HAS_CONTENTS
      0x00400804->0x0040080d at 0x00000804: .fini ALLOC LOAD READONLY CODE HAS_CONTENTS
      0x00400810->0x0040084e at 0x00000810: .rodata ALLOC LOAD READONLY DATA HAS_CONTENTS
      0x00400850->0x004008c4 at 0x00000850: .eh_frame_hdr ALLOC LOAD READONLY DATA HAS_CONTENTS
      0x004008c8->0x00400ab8 at 0x000008c8: .eh_frame ALLOC LOAD READONLY DATA HAS_CONTENTS
      0x00600e00->0x00600e08 at 0x00000e00: .init_array ALLOC LOAD DATA HAS_CONTENTS
      0x00600e08->0x00600e10 at 0x00000e08: .fini_array ALLOC LOAD DATA HAS_CONTENTS
      0x00600e10->0x00600ff0 at 0x00000e10: .dynamic ALLOC LOAD DATA HAS_CONTENTS
      0x00600ff0->0x00601000 at 0x00000ff0: .got ALLOC LOAD DATA HAS_CONTENTS
      0x00601000->0x00601038 at 0x00001000: .got.plt ALLOC LOAD DATA HAS_CONTENTS
      0x00601038->0x0060103c at 0x00001038: .data ALLOC LOAD DATA HAS_CONTENTS
      0x00601040->0x006012c8 at 0x0000103c: .bss ALLOC
      0x00000000->0x00000000 at 0x00000000: *COM* IS_COMMON
      0x00000000->0x00000000 at 0x00000000: *UND*
      0x00000000->0x00000000 at 0x00000000: *ABS*
      0x00000000->0x00000000 at 0x00000000: *IND*
  (gdb)

We lost the section index numbers, but more importantly, we lost the
information about the .debug* sections.  We also gained entries for
the "fake" sections *COM*, *UND*, *ABS*, and *IND*.

I noticed this when running:

  make check-gdb RUNTESTFLAGS="--target_board=cc-with-gdb-index gdb.base/maint.exp"

As this test relies on looking in the 'maint info sections' output to
see if we have a .debug_names or .gdb_index section, and these are
debug sections so they no longer show up in the 'main info sections'
output, the gdb.base/maint.exp test fails.

This commit restores the old behaviour while keeping the important
change that the above commit introduced, the addresses printed for
sections are the relocated addresses where appropriate.  The above
commit mentions using this test:

   make check-gdb RUNTESTFLAGS="CFLAGS_FOR_TARGET='-pie' gdb.base/compare-sections.exp"

And this still passes after this commit.

The output for 'maint info sections' now looks like this:

  (gdb) maintenance info sections
  Exec file:
      `/home/andrew/projects/binutils-gdb/build/gdb/testsuite/outputs/gdb.base/maint/maint',
          file type elf64-x86-64.
   [0]      0x00400238->0x00400254 at 0x00000238: .interp ALLOC LOAD READONLY DATA HAS_CONTENTS
   [1]      0x00400254->0x00400274 at 0x00000254: .note.ABI-tag ALLOC LOAD READONLY DATA HAS_CONTENTS
   [2]      0x00400274->0x00400298 at 0x00000274: .note.gnu.build-id ALLOC LOAD READONLY DATA HAS_CONTENTS
   [3]      0x00400298->0x004002bc at 0x00000298: .gnu.hash ALLOC LOAD READONLY DATA HAS_CONTENTS
   [4]      0x004002c0->0x00400380 at 0x000002c0: .dynsym ALLOC LOAD READONLY DATA HAS_CONTENTS
   [5]      0x00400380->0x004003e3 at 0x00000380: .dynstr ALLOC LOAD READONLY DATA HAS_CONTENTS
   [6]      0x004003e4->0x004003f4 at 0x000003e4: .gnu.version ALLOC LOAD READONLY DATA HAS_CONTENTS
   [7]      0x004003f8->0x00400418 at 0x000003f8: .gnu.version_r ALLOC LOAD READONLY DATA HAS_CONTENTS
   [8]      0x00400418->0x00400460 at 0x00000418: .rela.dyn ALLOC LOAD READONLY DATA HAS_CONTENTS
   [9]      0x00400460->0x004004c0 at 0x00000460: .rela.plt ALLOC LOAD READONLY DATA HAS_CONTENTS
   [10]     0x004004c0->0x004004d7 at 0x000004c0: .init ALLOC LOAD READONLY CODE HAS_CONTENTS
   [11]     0x004004e0->0x00400530 at 0x000004e0: .plt ALLOC LOAD READONLY CODE HAS_CONTENTS
   [12]     0x00400530->0x00400802 at 0x00000530: .text ALLOC LOAD READONLY CODE HAS_CONTENTS
   [13]     0x00400804->0x0040080d at 0x00000804: .fini ALLOC LOAD READONLY CODE HAS_CONTENTS
   [14]     0x00400810->0x0040084e at 0x00000810: .rodata ALLOC LOAD READONLY DATA HAS_CONTENTS
   [15]     0x00400850->0x004008c4 at 0x00000850: .eh_frame_hdr ALLOC LOAD READONLY DATA HAS_CONTENTS
   [16]     0x004008c8->0x00400ab8 at 0x000008c8: .eh_frame ALLOC LOAD READONLY DATA HAS_CONTENTS
   [17]     0x00600e00->0x00600e08 at 0x00000e00: .init_array ALLOC LOAD DATA HAS_CONTENTS
   [18]     0x00600e08->0x00600e10 at 0x00000e08: .fini_array ALLOC LOAD DATA HAS_CONTENTS
   [19]     0x00600e10->0x00600ff0 at 0x00000e10: .dynamic ALLOC LOAD DATA HAS_CONTENTS
   [20]     0x00600ff0->0x00601000 at 0x00000ff0: .got ALLOC LOAD DATA HAS_CONTENTS
   [21]     0x00601000->0x00601038 at 0x00001000: .got.plt ALLOC LOAD DATA HAS_CONTENTS
   [22]     0x00601038->0x0060103c at 0x00001038: .data ALLOC LOAD DATA HAS_CONTENTS
   [23]     0x00601040->0x006012c8 at 0x0000103c: .bss ALLOC
   [24]     0x00000000->0x0000002c at 0x0000103c: .comment READONLY HAS_CONTENTS
   [25]     0x00000000->0x00000060 at 0x00001068: .debug_aranges READONLY HAS_CONTENTS
   [26]     0x00000000->0x0000061b at 0x000010c8: .debug_info READONLY HAS_CONTENTS
   [27]     0x00000000->0x00000264 at 0x000016e3: .debug_abbrev READONLY HAS_CONTENTS
   [28]     0x00000000->0x000001e6 at 0x00001947: .debug_line READONLY HAS_CONTENTS
   [29]     0x00000000->0x00000487 at 0x00001b2d: .debug_str READONLY HAS_CONTENTS
  (gdb)

This is basically as it was before, except that the index numbers are
now padded so the section information all lines up.

When GDB has relocated a section then the relocated addresses will be
printed, otherwise the non-relocated addresses from the bfd will be
printed.

I've added a test to gdb.base/maint.exp to do some basic validation of
the output format.

gdb/ChangeLog:

	* maint.c: Add 'cmath' include.
	(struct maint_print_section_data): New structure.
	(print_section_index): New function.
	(print_bfd_section_info): Add header comment, small whitespace
	cleanup, and update to call new print_section_index function.
	(print_objfile_section_info): Likewise.
	(maint_obj_section_from_bfd_section): New function.
	(print_bfd_section_info_maybe_relocated): New function.
	(maintenance_info_sections): Add header comment, always use
	bfd_map_over_sections instead of ALL_OBJFILE_OSECTIONS.

gdb/testsuite/ChangeLog:

	* gdb.base/maint.exp: Add test for 'maint info sections'.
---
 gdb/ChangeLog                    |  13 ++++
 gdb/maint.c                      | 142 ++++++++++++++++++++++++++++++++++-----
 gdb/testsuite/ChangeLog          |   4 ++
 gdb/testsuite/gdb.base/maint.exp |  17 +++++
 4 files changed, 161 insertions(+), 15 deletions(-)
  

Patch

diff --git a/gdb/maint.c b/gdb/maint.c
index 837ed23cfb6..286ec310135 100644
--- a/gdb/maint.c
+++ b/gdb/maint.c
@@ -23,6 +23,7 @@ 
 #include "defs.h"
 #include "arch-utils.h"
 #include <ctype.h>
+#include <cmath>
 #include <signal.h>
 #include "command.h"
 #include "gdbcmd.h"
@@ -276,14 +277,68 @@  maint_print_section_info (const char *name, flagword flags,
   printf_filtered ("\n");
 }
 
+/* Information passed between the "maintenance info sections" command, and
+   the worker function that prints each section.  */
+struct maint_print_section_data
+{
+  /* The GDB objfile we're printing this section for.  */
+  struct objfile *objfile;
+
+  /* The argument string passed by the user to the top level maintenance
+     info sections command.  Used for filtering which sections are
+     printed.  */
+  const char *arg;
+
+  /* The number of digits in the highest section index for all sections
+     from the bfd object associated with OBJFILE.  Used when pretty
+     printing the index number to ensure all of the indexes line up.  */
+  int index_digits;
+
+  /* Constructor.  */
+  maint_print_section_data (struct objfile *objfile, const char *arg,
+			    bfd *abfd)
+    : objfile (objfile),
+      arg(arg)
+  {
+    int section_count = gdb_bfd_count_sections (abfd);
+    index_digits = ((int) log10 (section_count)) + 1;
+  }
+
+private:
+  maint_print_section_data () = delete;
+  maint_print_section_data (const maint_print_section_data &) = delete;
+};
+
+/* Helper function to pretty-print the section index of ASECT from ABFD.
+   The INDEX_DIGITS is the number of digits in the largest index that will
+   be printed, and is used to pretty-print the resulting string.  */
+
+static void
+print_section_index (bfd *abfd,
+		     asection *asect,
+		     int index_digits)
+{
+  std::string result
+    = string_printf (" [%d] ", gdb_bfd_section_index (abfd, asect));
+  /* The '+ 4' for the leading and trailing characters.  */
+  printf_filtered ("%-*s", (index_digits + 4), result.c_str ());
+}
+
+/* Print information about ASECT from ABFD.  DATUM holds a pointer to a
+   maint_print_section_data object.  The section will be printed using the
+   VMA's from the bfd, which will not be the relocated addresses for bfds
+   that should be relocated.  The information must be printed with the
+   same layout as PRINT_OBJFILE_SECTION_INFO below.  */
+
 static void
-print_bfd_section_info (bfd *abfd, 
-			asection *asect, 
+print_bfd_section_info (bfd *abfd,
+			asection *asect,
 			void *datum)
 {
   flagword flags = bfd_get_section_flags (abfd, asect);
   const char *name = bfd_section_name (abfd, asect);
-  const char *arg = (const char *) datum;
+  maint_print_section_data *print_data = (maint_print_section_data *) datum;
+  const char *arg = print_data->arg;
 
   if (arg == NULL || *arg == '\0'
       || match_substring (arg, name)
@@ -295,19 +350,25 @@  print_bfd_section_info (bfd *abfd,
 
       addr = bfd_section_vma (abfd, asect);
       endaddr = addr + bfd_section_size (abfd, asect);
-      printf_filtered (" [%d] ", gdb_bfd_section_index (abfd, asect));
+      print_section_index (abfd, asect, print_data->index_digits);
       maint_print_section_info (name, flags, addr, endaddr,
 				asect->filepos, addr_size);
     }
 }
 
+/* Print information about ASECT which is GDB's wrapper around a section
+   from ABFD.  The information must be printed with the same layout as
+   PRINT_BFD_SECTION_INFO above.  PRINT_DATA holds information used to
+   filter which sections are printed, and for formatting the output.  */
+
 static void
-print_objfile_section_info (bfd *abfd, 
-			    struct obj_section *asect, 
-			    const char *string)
+print_objfile_section_info (bfd *abfd,
+			    struct obj_section *asect,
+			    maint_print_section_data *print_data)
 {
   flagword flags = bfd_get_section_flags (abfd, asect->the_bfd_section);
   const char *name = bfd_section_name (abfd, asect->the_bfd_section);
+  const char *string = print_data->arg;
 
   if (string == NULL || *string == '\0'
       || match_substring (string, name)
@@ -316,6 +377,8 @@  print_objfile_section_info (bfd *abfd,
       struct gdbarch *gdbarch = gdbarch_from_bfd (abfd);
       int addr_size = gdbarch_addr_bit (gdbarch) / 8;
 
+      print_section_index (abfd, asect->the_bfd_section,
+			   print_data->index_digits);
       maint_print_section_info (name, flags,
 				obj_section_addr (asect),
 				obj_section_endaddr (asect),
@@ -324,12 +387,56 @@  print_objfile_section_info (bfd *abfd,
     }
 }
 
+/* Find an obj_section, GDB's wrapper around a bfd section for ASECTION
+   from ABFD.  It might be that no such wrapper exists (for example debug
+   sections don't have such wrappers) in which case nullptr is returned.  */
+
+static obj_section *
+maint_obj_section_from_bfd_section (bfd *abfd,
+				    asection *asection,
+				    objfile *ofile)
+{
+  if (ofile->sections == nullptr)
+    return nullptr;
+
+  obj_section *osect
+    = &ofile->sections[gdb_bfd_section_index (abfd, asection)];
+
+  if (osect >= ofile->sections_end)
+    return nullptr;
+
+  return osect;
+}
+
+/* Print information about ASECT from ABFD.  DATUM holds a pointer to a
+   maint_print_section_data object.  Where possible the information for
+   ASECT will print the relocated addresses of the section.  */
+
+static void
+print_bfd_section_info_maybe_relocated (bfd *abfd,
+					asection *asect,
+					void *datum)
+{
+  maint_print_section_data *print_data = (maint_print_section_data *) datum;
+  objfile *objfile = print_data->objfile;
+
+  gdb_assert (objfile->sections != NULL);
+  obj_section *osect
+    = maint_obj_section_from_bfd_section (abfd, asect, objfile);
+
+  if (osect->the_bfd_section == NULL)
+    print_bfd_section_info (abfd, asect, datum);
+  else
+    print_objfile_section_info (abfd, osect, print_data);
+}
+
+/* Implement the "maintenance info sections" command.  */
+
 static void
 maintenance_info_sections (const char *arg, int from_tty)
 {
   if (exec_bfd)
     {
-      struct obj_section *osect;
       bool allobj = false;
 
       printf_filtered (_("Exec file:\n"));
@@ -352,22 +459,27 @@  maintenance_info_sections (const char *arg, int from_tty)
 	  if (allobj)
 	    printf_filtered (_("  Object file: %s\n"),
 			     bfd_get_filename (ofile->obfd));
-	  ALL_OBJFILE_OSECTIONS (ofile, osect)
-	    {
-	      if (!allobj && ofile->obfd != exec_bfd)
-		continue;
-	      print_objfile_section_info (ofile->obfd, osect, arg);
-	    }
+	  else if (ofile->obfd != exec_bfd)
+	    continue;
+
+	  maint_print_section_data print_data (ofile, arg, ofile->obfd);
+
+	  bfd_map_over_sections (ofile->obfd,
+				 print_bfd_section_info_maybe_relocated,
+				 (void *) &print_data);
 	}
     }
 
   if (core_bfd)
     {
+      maint_print_section_data print_data (nullptr, arg, core_bfd);
+
       printf_filtered (_("Core file:\n"));
       printf_filtered ("    `%s', ", bfd_get_filename (core_bfd));
       wrap_here ("        ");
       printf_filtered (_("file type %s.\n"), bfd_get_target (core_bfd));
-      bfd_map_over_sections (core_bfd, print_bfd_section_info, (void *) arg);
+      bfd_map_over_sections (core_bfd, print_bfd_section_info,
+			     (void *) &print_data);
     }
 }
 
diff --git a/gdb/testsuite/gdb.base/maint.exp b/gdb/testsuite/gdb.base/maint.exp
index 36738f6eaa7..a5d5dacaba9 100644
--- a/gdb/testsuite/gdb.base/maint.exp
+++ b/gdb/testsuite/gdb.base/maint.exp
@@ -109,6 +109,23 @@  if ![runto_main] then {
         perror "tests suppressed"
 }
 
+# Check that 'maint info sections' output looks correct.  When
+# checking the lines for each section we reject section names starting
+# with a '*' character, the internal *COM*, *UND*, *ABS*, and *IND*
+# sections should not be displayed in this output.
+set test "check maint info sections output"
+gdb_test_multiple "maint info sections" $test {
+    -re "Exec file:\r\n\[\t ]+`\[^'\]+', file type \[^.\]+\.\r\n" {
+	exp_continue
+    }
+    -re "^ \\\[\[0-9\]+\\\]\[\t \]+$hex->$hex at $hex: \[^*\r\]+\r\n" {
+	exp_continue
+    }
+    -re "^$gdb_prompt $" {
+	pass $test
+    }
+}
+
 # If we're using .gdb_index or .debug_names there will be no psymtabs.
 set have_gdb_index 0
 gdb_test_multiple "maint info sections .gdb_index .debug_names" "check for .gdb_index" {