gdb: Have 'maint info sections' print all sections again

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'.
4 files changed