This is the mail archive of the binutils@sourceware.org mailing list for the binutils project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH] Fix up readelf -wo for GCC 4.6+ output


Hi!

GCC 4.6 and above (starting with my 2010-10-12
http://gcc.gnu.org/ml/gcc-patches/2010-10/msg00736.html
optimize_location_lists changes) no longer outputs
.debug_loc references in CUs in strictly ascending order.
It reuses .debug_loc entries if they can be reused to make
.debug_loc smaller.

This patch adjusts dwarf.c, so that readelf -wo doesn't
spit thousands of

readelf: Error: Location lists in .debug_info section aren't in ascending order!
readelf: Warning: There is an overlap [0xa9f - 0x6a6] in .debug_loc section.
readelf: Warning: There is a hole [0x748 - 0xa9f] in .debug_loc section.
readelf: Warning: There is an overlap [0xd31 - 0xbd1] in .debug_loc section.
readelf: Warning: There is a hole [0xc73 - 0xd31] in .debug_loc section.

errors/warnings on all binaries.
The reason I decided to sort an array of indexes instead of
debug_information [i].loc_offsets directly is that
debug_information [i].has_frame_base needs to match that.  We could put that
into a struct instead, but that would consume more memory and could slow
down qsort too.

Ok?

2011-05-20  Jakub Jelinek  <jakub@redhat.com>

	* dwarf.c (loc_offsets): New variable.
	(loc_offsets_compar): New routine.
	(display_debug_loc): Handle loc_offsets not being in ascending order
	and also a single .debug_loc entry being used multiple times.

--- binutils/dwarf.c.jj	2011-05-20 16:52:45.000000000 +0200
+++ binutils/dwarf.c	2011-05-20 17:46:20.000000000 +0200
@@ -3488,6 +3488,19 @@ display_debug_abbrev (struct dwarf_secti
   return 1;
 }
 
+/* Sort array of indexes in ascending order of loc_offsets[idx].  */
+
+static dwarf_vma *loc_offsets;
+
+static int
+loc_offsets_compar (const void *ap, const void *bp)
+{
+  dwarf_vma a = loc_offsets[*(const unsigned int *) ap];
+  dwarf_vma b = loc_offsets[*(const unsigned int *) bp];
+
+  return (a > b) - (b > a);
+}
+
 static int
 display_debug_loc (struct dwarf_section *section, void *file)
 {
@@ -3500,9 +3513,11 @@ display_debug_loc (struct dwarf_section 
   unsigned int first = 0;
   unsigned int i;
   unsigned int j;
+  unsigned int k;
   int seen_first_offset = 0;
-  int use_debug_info = 1;
+  int locs_sorted = 1;
   unsigned char *next;
+  unsigned int *array = NULL;
 
   bytes = section->size;
   section_end = start + bytes;
@@ -3528,10 +3543,11 @@ display_debug_loc (struct dwarf_section 
       unsigned int num;
 
       num = debug_information [i].num_loc_offsets;
-      num_loc_list += num;
+      if (num > num_loc_list)
+	num_loc_list = num;
 
       /* Check if we can use `debug_information' directly.  */
-      if (use_debug_info && num != 0)
+      if (locs_sorted && num != 0)
 	{
 	  if (!seen_first_offset)
 	    {
@@ -3549,7 +3565,7 @@ display_debug_loc (struct dwarf_section 
 	      if (last_offset >
 		  debug_information [i].loc_offsets [j])
 		{
-		  use_debug_info = 0;
+		  locs_sorted = 0;
 		  break;
 		}
 	      last_offset = debug_information [i].loc_offsets [j];
@@ -3557,10 +3573,6 @@ display_debug_loc (struct dwarf_section 
 	}
     }
 
-  if (!use_debug_info)
-    /* FIXME: Should we handle this case?  */
-    error (_("Location lists in .debug_info section aren't in ascending order!\n"));
-
   if (!seen_first_offset)
     error (_("No location lists in .debug_info section!\n"));
 
@@ -3571,6 +3583,8 @@ display_debug_loc (struct dwarf_section 
 	  section->name,
 	  dwarf_vmatoa ("x", debug_information [first].loc_offsets [0]));
 
+  if (!locs_sorted)
+    array = (unsigned int *) xcmalloc (num_loc_list, sizeof (unsigned int));
   printf (_("Contents of the %s section:\n\n"), section->name);
   printf (_("    Offset   Begin    End      Expression\n"));
 
@@ -3593,9 +3607,23 @@ display_debug_loc (struct dwarf_section 
       cu_offset = debug_information [i].cu_offset;
       offset_size = debug_information [i].offset_size;
       dwarf_version = debug_information [i].dwarf_version;
-
-      for (j = 0; j < debug_information [i].num_loc_offsets; j++)
+      if (!locs_sorted)
 	{
+	  for (k = 0; k < debug_information [i].num_loc_offsets; k++)
+	    array[k] = k;
+	  loc_offsets = debug_information [i].loc_offsets;
+	  qsort (array, debug_information [i].num_loc_offsets,
+		 sizeof (*array), loc_offsets_compar);
+	}
+
+      for (k = 0; k < debug_information [i].num_loc_offsets; k++)
+	{
+	  j = locs_sorted ? k : array[k];
+	  if (k
+	      && debug_information [i].loc_offsets [locs_sorted
+						    ? k - 1 : array [k - 1]]
+		 == debug_information [i].loc_offsets [j])
+	    continue;
 	  has_frame_base = debug_information [i].have_frame_base [j];
 	  /* DWARF sections under Mach-O have non-zero addresses.  */
 	  offset = debug_information [i].loc_offsets [j] - section->address;
@@ -3709,6 +3737,7 @@ display_debug_loc (struct dwarf_section 
     warn (_("There are %ld unused bytes at the end of section %s\n"),
 	  (long) (section_end - start), section->name);
   putchar ('\n');
+  free (array);
   return 1;
 }
 


	Jakub


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]