public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] objdump: Better objdump section headers in wide mode
@ 2017-01-18 12:21 Andrew Burgess
  2017-01-18 23:58 ` Alan Modra
  0 siblings, 1 reply; 4+ messages in thread
From: Andrew Burgess @ 2017-01-18 12:21 UTC (permalink / raw)
  To: binutils; +Cc: Andrew Burgess

When displaying the section headers table using objdump (-h), the column
containing the section header name is currently fixed at 13 characters.
A section name that is longer than 13 characters will overflow the
column causing the table to become miss-aligned.

In this commit I change the behaviour so that _in wide mode_ (-w -h) the
section name column is dynamically resized to fit the longest section
name we plan to display.  In wide mode the column still retains a
minimum width of 13 characters.

In non-wide more the behaviour is completely unchanged.

While I was changing the dump_headers function I have unified the two
printf lines that handled the different address widths into a single
printf, the address width is now passed into printf using the '*' field
width format character.

binutils/ChangeLog:

	* objdump.c (dump_section_header): Extract max section name length
	from data parameter, use this when formatting output.
	(find_longest_section_name): New function.
	(dump_headers): Calculate longest section name when in wide mode,
	reformat to unify printing of header line.

ld/ChangeLog:

	* testsuite/ld-elf/eh-frame-hdr.d: Update expected results.
---
 binutils/ChangeLog                 |  8 ++++++
 binutils/objdump.c                 | 54 ++++++++++++++++++++++++++++++++------
 ld/ChangeLog                       |  4 +++
 ld/testsuite/ld-elf/eh-frame-hdr.d |  2 +-
 4 files changed, 59 insertions(+), 9 deletions(-)

diff --git a/binutils/objdump.c b/binutils/objdump.c
index c03dfc5..b9fecef 100644
--- a/binutils/objdump.c
+++ b/binutils/objdump.c
@@ -438,11 +438,11 @@ free_only_list (void)
 
 \f
 static void
-dump_section_header (bfd *abfd, asection *section,
-		     void *ignored ATTRIBUTE_UNUSED)
+dump_section_header (bfd *abfd, asection *section, void *data)
 {
   char *comma = "";
   unsigned int opb = bfd_octets_per_byte (abfd);
+  int longest_section_name = *((int *) data);
 
   /* Ignore linker created section.  See elfNN_ia64_object_p in
      bfd/elfxx-ia64.c.  */
@@ -453,7 +453,7 @@ dump_section_header (bfd *abfd, asection *section,
   if (! process_section_p (section))
     return;
 
-  printf ("%3d %-13s %08lx  ", section->index,
+  printf ("%3d %-*s %08lx  ", section->index, longest_section_name,
 	  bfd_get_section_name (abfd, section),
 	  (unsigned long) bfd_section_size (abfd, section) / opb);
   bfd_printf_vma (abfd, bfd_get_section_vma (abfd, section));
@@ -536,26 +536,64 @@ dump_section_header (bfd *abfd, asection *section,
 #undef PF
 }
 
+/* Called on each SECTION in ABFD, update the int variable pointed to by
+   DATA which contains the string length of the longest section name.  */
+
+static void
+find_longest_section_name (bfd *abfd, asection *section, void *data)
+{
+  int *longest_so_far = (int *) data;
+  const char *name;
+  int len;
+
+  /* Ignore linker created section.  */
+  if (section->flags & SEC_LINKER_CREATED)
+    return;
+
+  /* Skip sections that we are ignoring.  */
+  if (! process_section_p (section))
+    return;
+
+  name = bfd_get_section_name (abfd, section);
+  len = (int) strlen (name);
+  if (len > *longest_so_far)
+    *longest_so_far = len;
+}
+
 static void
 dump_headers (bfd *abfd)
 {
-  printf (_("Sections:\n"));
+  /* The default width of 13 is just an arbitrary choice.  */
+  int max_section_name_length = 13;
+  int bfd_vma_width;
 
 #ifndef BFD64
-  printf (_("Idx Name          Size      VMA       LMA       File off  Algn"));
+  bfd_vma_width = 10;
 #else
   /* With BFD64, non-ELF returns -1 and wants always 64 bit addresses.  */
   if (bfd_get_arch_size (abfd) == 32)
-    printf (_("Idx Name          Size      VMA       LMA       File off  Algn"));
+    bfd_vma_width = 10;
   else
-    printf (_("Idx Name          Size      VMA               LMA               File off  Algn"));
+    bfd_vma_width = 18;
 #endif
 
+  printf (_("Sections:\n"));
+
+  if (wide_output)
+    bfd_map_over_sections (abfd, find_longest_section_name,
+                           &max_section_name_length);
+
+  printf (_("Idx %-*s Size      %-*s%-*sFile off  Algn"),
+	  max_section_name_length, "Name",
+	  bfd_vma_width, "VMA",
+	  bfd_vma_width, "LMA");
+
   if (wide_output)
     printf (_("  Flags"));
   printf ("\n");
 
-  bfd_map_over_sections (abfd, dump_section_header, NULL);
+  bfd_map_over_sections (abfd, dump_section_header,
+                         &max_section_name_length);
 }
 \f
 static asymbol **
diff --git a/ld/testsuite/ld-elf/eh-frame-hdr.d b/ld/testsuite/ld-elf/eh-frame-hdr.d
index a76ac73..08b12d8 100644
--- a/ld/testsuite/ld-elf/eh-frame-hdr.d
+++ b/ld/testsuite/ld-elf/eh-frame-hdr.d
@@ -5,5 +5,5 @@
 #xfail: avr*-*-* or1k*-*-elf or1k*-*-rtems* pru-*-* visium-*-*
 # These targets support CFI generation but not shared libraries.
 #...
-  [0-9] .eh_frame_hdr 0*[12][048c] .*
+  [0-9] .eh_frame_hdr +0*[12][048c] .*
 #pass
-- 
2.6.4

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] objdump: Better objdump section headers in wide mode
  2017-01-18 12:21 [PATCH] objdump: Better objdump section headers in wide mode Andrew Burgess
@ 2017-01-18 23:58 ` Alan Modra
  2017-01-19 13:28   ` Andrew Burgess
  0 siblings, 1 reply; 4+ messages in thread
From: Alan Modra @ 2017-01-18 23:58 UTC (permalink / raw)
  To: Andrew Burgess; +Cc: binutils

I like the patch but you didn't say how it was tested.  That leads me
to suspect the patch may introduce testsuite regressions.  A grep over
the testsuite shows occurrences of "objdump -hw" and
"objdump -h --wide" in target specific tests.

-- 
Alan Modra
Australia Development Lab, IBM

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] objdump: Better objdump section headers in wide mode
  2017-01-18 23:58 ` Alan Modra
@ 2017-01-19 13:28   ` Andrew Burgess
  2017-01-19 23:34     ` Alan Modra
  0 siblings, 1 reply; 4+ messages in thread
From: Andrew Burgess @ 2017-01-19 13:28 UTC (permalink / raw)
  To: Alan Modra; +Cc: binutils

* Alan Modra <amodra@gmail.com> [2017-01-19 10:27:55 +1030]:

> I like the patch but you didn't say how it was tested.  That leads me
> to suspect the patch may introduce testsuite regressions.  A grep over
> the testsuite shows occurrences of "objdump -hw" and
> "objdump -h --wide" in target specific tests.

Apologies.  I ran the patch against ~200 different build targets, most
just configured with --target=BLAH, but a few also built using -m32 in
the CFLAGS (including native x86-64 with --targets=all and -m32 in the
CFLAGS).

There was one failure, which I fixed in the patch, but otherwise it
was all fine.

I'd love to hear about any regressions, so I can increase my test
coverage, but otherwise I think I'm as confident in this as I can be.

Thanks,
Andrew

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] objdump: Better objdump section headers in wide mode
  2017-01-19 13:28   ` Andrew Burgess
@ 2017-01-19 23:34     ` Alan Modra
  0 siblings, 0 replies; 4+ messages in thread
From: Alan Modra @ 2017-01-19 23:34 UTC (permalink / raw)
  To: Andrew Burgess; +Cc: binutils

On Thu, Jan 19, 2017 at 01:28:13PM +0000, Andrew Burgess wrote:
> * Alan Modra <amodra@gmail.com> [2017-01-19 10:27:55 +1030]:
> 
> > I like the patch but you didn't say how it was tested.  That leads me
> > to suspect the patch may introduce testsuite regressions.  A grep over
> > the testsuite shows occurrences of "objdump -hw" and
> > "objdump -h --wide" in target specific tests.
> 
> Apologies.  I ran the patch against ~200 different build targets, most

Patch is OK then, thanks!

-- 
Alan Modra
Australia Development Lab, IBM

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2017-01-19 23:34 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-01-18 12:21 [PATCH] objdump: Better objdump section headers in wide mode Andrew Burgess
2017-01-18 23:58 ` Alan Modra
2017-01-19 13:28   ` Andrew Burgess
2017-01-19 23:34     ` Alan Modra

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).