DWARF calculate the number of indexed addresses.

Message ID 1526304984-22682-1-git-send-email-mark@klomp.org
State New
Headers show
Series
  • DWARF calculate the number of indexed addresses.
Related show

Commit Message

Mark Wielaard May 14, 2018, 1:36 p.m.
The length in the .debug_addr unit header was calculated using the number
of elements in the addr_index_table. This is wrong because the entries in
the table are refcounted and only those with a refcount > 0 are actually
put in the index. Add a helper function count_index_addrs to get the
correct number of addresses in the index.

gcc/ChangeLog:

	* dwarf2out.c (count_index_addrs): New function.
	(dwarf2out_finish): Use count_index_addrs to calculate addrs_length.

Comments

Jeff Law May 14, 2018, 9:07 p.m. | #1
On 05/14/2018 07:36 AM, Mark Wielaard wrote:
> The length in the .debug_addr unit header was calculated using the number
> of elements in the addr_index_table. This is wrong because the entries in
> the table are refcounted and only those with a refcount > 0 are actually
> put in the index. Add a helper function count_index_addrs to get the
> correct number of addresses in the index.
> 
> gcc/ChangeLog:
> 
> 	* dwarf2out.c (count_index_addrs): New function.
> 	(dwarf2out_finish): Use count_index_addrs to calculate addrs_length.
OK.
jeff

Patch

diff --git a/gcc/dwarf2out.c b/gcc/dwarf2out.c
index 9c1d7c4..c05bfe4 100644
--- a/gcc/dwarf2out.c
+++ b/gcc/dwarf2out.c
@@ -28898,6 +28898,19 @@  output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
   return 1;
 }
 
+/* A helper function for dwarf2out_finish.  Counts the number
+   of indexed addresses.  Must match the logic of the functions
+   output_addr_table_entry above.  */
+int
+count_index_addrs (addr_table_entry **slot, unsigned int *last_idx)
+{
+  addr_table_entry *entry = *slot;
+
+  if (entry->refcount > 0)
+    *last_idx += 1;
+  return 1;
+}
+
 /* Produce the .debug_addr section.  */
 
 static void
@@ -31393,8 +31406,12 @@  dwarf2out_finish (const char *)
 	 DWARF5 specifies a small header when address tables are used.  */
       if (dwarf_version >= 5)
 	{
-	  unsigned long addrs_length
-	    = addr_index_table->elements () * DWARF2_ADDR_SIZE + 4;
+	  unsigned int last_idx = 0;
+	  unsigned long addrs_length;
+
+	  addr_index_table->traverse_noresize
+	    <unsigned int *, count_index_addrs> (&last_idx);
+	  addrs_length = last_idx * DWARF2_ADDR_SIZE + 4;
 
 	  if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
 	    dw2_asm_output_data (4, 0xffffffff,