[PATCH 5/5] readelf: Print CU, base address and unresolved .debug_loc entries.

Mark Wielaard mark@klomp.org
Wed Nov 29 16:06:00 GMT 2017


Also adjust the formatting for the resolved addresses to print them
on separate lines so they nicely line up even when the addresses are
resolved to symbol+offset names. And print the operands starting on
a new line.

Signed-off-by: Mark Wielaard <mark@klomp.org>
---
 src/ChangeLog                   |  5 ++++
 src/readelf.c                   | 51 ++++++++++++++++++++++++++++++-----------
 tests/ChangeLog                 |  6 +++++
 tests/run-readelf-loc.sh        | 51 +++++++++++++++++++++++++++++++++--------
 tests/run-readelf-zdebug-rel.sh | 17 +++++++++-----
 tests/run-readelf-zdebug.sh     | 17 +++++++++-----
 6 files changed, 112 insertions(+), 35 deletions(-)

diff --git a/src/ChangeLog b/src/ChangeLog
index 3dd80fb..612b365 100644
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,5 +1,10 @@
 2017-11-29  Mark Wielaard  <mark@klomp.org>
 
+	* readelf.c (print_debug_loc_section): Print CU base and unresolved
+	addresses. Adjust formatting.
+
+2017-11-29  Mark Wielaard  <mark@klomp.org>
+
 	* readelf.c (print_debug_ranges_section): Print CU base and unresolved
 	addresses. Adjust formatting.
 
diff --git a/src/readelf.c b/src/readelf.c
index 8e13462..bb48af9 100644
--- a/src/readelf.c
+++ b/src/readelf.c
@@ -7130,19 +7130,36 @@ print_debug_loc_section (Dwfl_Module *dwflmod,
   uint_fast8_t offset_size = 4;
 
   bool first = true;
-  struct Dwarf_CU *cu = NULL;
   Dwarf_Addr base = 0;
   unsigned char *readp = data->d_buf;
   unsigned char *const endp = (unsigned char *) data->d_buf + data->d_size;
+  Dwarf_CU *last_cu = NULL;
   while (readp < endp)
     {
       ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
+      Dwarf_CU *cu;
 
       if (first && skip_listptr_hole (&known_loclistptr, &listptr_idx,
 				      &address_size, &offset_size, &base,
 				      &cu, offset, &readp, endp))
 	continue;
 
+      if (last_cu != cu)
+       {
+	char *basestr = format_dwarf_addr (dwflmod, address_size,
+					   base, base);
+	Dwarf_Die cudie;
+	if (dwarf_cu_die (cu, &cudie,
+			  NULL, NULL, NULL, NULL,
+			  NULL, NULL) == NULL)
+	  printf (gettext ("\n Unknown CU base: %s\n"), basestr);
+	else
+	  printf (gettext ("\n CU [%6" PRIx64 "] base: %s\n"),
+		  dwarf_dieoffset (&cudie), basestr);
+	free (basestr);
+       }
+      last_cu = cu;
+
       if (unlikely (data->d_size - offset < (size_t) address_size * 2))
 	{
 	  printf (gettext (" [%6tx]  <INVALID DATA>\n"), offset);
@@ -7167,14 +7184,14 @@ print_debug_loc_section (Dwfl_Module *dwflmod,
       if (begin == (Dwarf_Addr) -1l) /* Base address entry.  */
 	{
 	  char *b = format_dwarf_addr (dwflmod, address_size, end, end);
-	  printf (gettext (" [%6tx]  base address %s\n"), offset, b);
+	  printf (gettext (" [%6tx] base address\n          %s\n"), offset, b);
 	  free (b);
 	  base = end;
 	}
       else if (begin == 0 && end == 0) /* End of list entry.  */
 	{
 	  if (first)
-	    printf (gettext (" [%6tx]  empty list\n"), offset);
+	    printf (gettext (" [%6tx] empty list\n"), offset);
 	  first = true;
 	}
       else
@@ -7182,18 +7199,23 @@ print_debug_loc_section (Dwfl_Module *dwflmod,
 	  /* We have a location expression entry.  */
 	  uint_fast16_t len = read_2ubyte_unaligned_inc (dbg, readp);
 
-	  char *b = format_dwarf_addr (dwflmod, address_size, base + begin,
-				       begin);
-	  char *e = format_dwarf_addr (dwflmod, address_size, base + end,
-				       end);
-
 	  if (first)		/* First entry in a list.  */
-	    printf (gettext (" [%6tx]  %s..%s"), offset, b, e);
+	    printf (" [%6tx] ", offset);
 	  else
-	    printf (gettext ("           %s..%s"), b, e);
+	    printf ("          ");
 
-	  free (b);
-	  free (e);
+	  printf ("range %" PRIx64 ", %" PRIx64 "\n", begin, end);
+	  if (! print_unresolved_addresses)
+	    {
+	      char *b = format_dwarf_addr (dwflmod, address_size, base + begin,
+					   base + begin);
+	      char *e = format_dwarf_addr (dwflmod, address_size,
+					   base + end - 1, base + end);
+	      printf ("          %s..\n", b);
+	      printf ("          %s\n", e);
+	      free (b);
+	      free (e);
+	    }
 
 	  if (endp - readp <= (ptrdiff_t) len)
 	    {
@@ -7201,8 +7223,9 @@ print_debug_loc_section (Dwfl_Module *dwflmod,
 	      break;
 	    }
 
-	  print_ops (dwflmod, dbg, 1, 18 + (address_size * 4),
-		     3 /*XXX*/, address_size, offset_size, cu, len, readp);
+	  print_ops (dwflmod, dbg, 11, 11,
+		     cu != NULL ? cu->version : 3,
+		     address_size, offset_size, cu, len, readp);
 
 	  first = false;
 	  readp += len;
diff --git a/tests/ChangeLog b/tests/ChangeLog
index 4666dcc..cb0c977 100644
--- a/tests/ChangeLog
+++ b/tests/ChangeLog
@@ -1,5 +1,11 @@
 2017-11-29  Mark Wielaard  <mark@klomp.org>
 
+	* run-readelf-loc.sh: Adjust expected loc list output.
+	* run-readelf-zdebug-rel.sh: Likewise.
+	* run-readelf-zdebug.sh: Likewise.
+
+2017-11-29  Mark Wielaard  <mark@klomp.org>
+
 	* run-readelf-loc.sh: Adjust expected range list output.
 	* run-readelf-zdebug.sh: Likewise.
 
diff --git a/tests/run-readelf-loc.sh b/tests/run-readelf-loc.sh
index 4b666cf..e5152df 100755
--- a/tests/run-readelf-loc.sh
+++ b/tests/run-readelf-loc.sh
@@ -63,9 +63,22 @@ testrun_compare ${abs_top_builddir}/src/readelf --debug-dump=loc --debug-dump=ra
   testfileloc<<\EOF
 
 DWARF section [33] '.debug_loc' at offset 0xd2a:
- [     0]  0x0000000000400480 <main>..0x000000000040048d <main+0xd> [ 0] reg5
- [    23]  0x0000000000400485 <main+0x5>..0x000000000040048d <main+0xd> [ 0] reg5
- [    46]  0x00000000004004b2 <say+0x12>..0x00000000004004ba <say+0x1a> [ 0] breg5 0
+
+ CU [     b] base: 0x0000000000400480 <main>
+ [     0] range 0, d
+          0x0000000000400480 <main>..
+          0x000000000040048c <main+0xc>
+           [ 0] reg5
+ [    23] range 5, d
+          0x0000000000400485 <main+0x5>..
+          0x000000000040048c <main+0xc>
+           [ 0] reg5
+
+ CU [    e0] base: 0x00000000004004a0 <say>
+ [    46] range 12, 1a
+          0x00000000004004b2 <say+0x12>..
+          0x00000000004004b9 <say+0x19>
+           [ 0] breg5 0
 
 DWARF section [34] '.debug_ranges' at offset 0xd94:
 
@@ -91,9 +104,22 @@ testrun_compare ${abs_top_builddir}/src/readelf -N --debug-dump=loc --debug-dump
   testfileloc<<\EOF
 
 DWARF section [33] '.debug_loc' at offset 0xd2a:
- [     0]  0x0000000000400480..0x000000000040048d [ 0] reg5
- [    23]  0x0000000000400485..0x000000000040048d [ 0] reg5
- [    46]  0x00000000004004b2..0x00000000004004ba [ 0] breg5 0
+
+ CU [     b] base: 0x0000000000400480
+ [     0] range 0, d
+          0x0000000000400480..
+          0x000000000040048c
+           [ 0] reg5
+ [    23] range 5, d
+          0x0000000000400485..
+          0x000000000040048c
+           [ 0] reg5
+
+ CU [    e0] base: 0x00000000004004a0
+ [    46] range 12, 1a
+          0x00000000004004b2..
+          0x00000000004004b9
+           [ 0] breg5 0
 
 DWARF section [34] '.debug_ranges' at offset 0xd94:
 
@@ -119,9 +145,16 @@ testrun_compare ${abs_top_builddir}/src/readelf -U --debug-dump=loc --debug-dump
   testfileloc<<\EOF
 
 DWARF section [33] '.debug_loc' at offset 0xd2a:
- [     0]  000000000000000000..0x000000000000000d [ 0] reg5
- [    23]  0x0000000000000005..0x000000000000000d [ 0] reg5
- [    46]  0x0000000000000012..0x000000000000001a [ 0] breg5 0
+
+ CU [     b] base: 0x0000000000400480
+ [     0] range 0, d
+           [ 0] reg5
+ [    23] range 5, d
+           [ 0] reg5
+
+ CU [    e0] base: 0x00000000004004a0
+ [    46] range 12, 1a
+           [ 0] breg5 0
 
 DWARF section [34] '.debug_ranges' at offset 0xd94:
 
diff --git a/tests/run-readelf-zdebug-rel.sh b/tests/run-readelf-zdebug-rel.sh
index 2b57d05..ccccd82 100755
--- a/tests/run-readelf-zdebug-rel.sh
+++ b/tests/run-readelf-zdebug-rel.sh
@@ -127,12 +127,17 @@ cat info.out | testrun_compare ${abs_top_builddir}/src/readelf -U --debug-dump=i
 cat > loc.out << \EOF
 
 DWARF section [ 7] '.debug_loc' at offset 0x185:
- [     0]  000000000000000000..0x0000000000000003 [ 0] reg5
-           0x0000000000000003..0x0000000000000010 [ 0] breg5 -42
-                                                  [ 2] stack_value
-           0x0000000000000010..0x0000000000000018 [ 0] GNU_entry_value:
-      [ 0] reg5
-                                                  [ 3] stack_value
+
+ CU [     b] base: 000000000000000000
+ [     0] range 0, 3
+           [ 0] reg5
+          range 3, 10
+           [ 0] breg5 -42
+           [ 2] stack_value
+          range 10, 18
+           [ 0] GNU_entry_value:
+                [ 0] reg5
+           [ 3] stack_value
 EOF
 
 cat loc.out | testrun_compare ${abs_top_builddir}/src/readelf -U --debug-dump=loc testfile-debug-rel.o
diff --git a/tests/run-readelf-zdebug.sh b/tests/run-readelf-zdebug.sh
index 7be9bee..28128ad 100755
--- a/tests/run-readelf-zdebug.sh
+++ b/tests/run-readelf-zdebug.sh
@@ -46,12 +46,17 @@ tempfiles loc.out aranges.out ranges.out macro.out line.out frame.out
 cat > loc.out << \EOF
 
 DWARF section [30] '.debug_loc' at offset 0xa17:
- [     0]  0x00000000004003c0..0x00000000004003c3 [ 0] reg5
-           0x00000000004003c3..0x00000000004003d6 [ 0] breg5 -42
-                                                  [ 2] stack_value
-           0x00000000004003d6..0x00000000004003d9 [ 0] GNU_entry_value:
-      [ 0] reg5
-                                                  [ 3] stack_value
+
+ CU [     b] base: 000000000000000000
+ [     0] range 4003c0, 4003c3
+           [ 0] reg5
+          range 4003c3, 4003d6
+           [ 0] breg5 -42
+           [ 2] stack_value
+          range 4003d6, 4003d9
+           [ 0] GNU_entry_value:
+                [ 0] reg5
+           [ 3] stack_value
 EOF
 
 cat loc.out | testrun_compare ${abs_top_builddir}/src/readelf -U --debug-dump=loc testfile-debug
-- 
1.8.3.1



More information about the Elfutils-devel mailing list