PR28149 part 2, purge generated line info

Message ID YUQ+TrjMU2g4vaPg@squeak.grove.modra.org
State New
Headers show
Series
  • PR28149 part 2, purge generated line info
Related show

Commit Message

Alan Modra via Binutils Sept. 17, 2021, 7:05 a.m.
Mixing compiler generated line info with gas generated line info is
generally just confusing.  Also .loc directives with non-zero view
fields might reference a previous .loc.  It becomes a little more
tricky to locate that previous .loc if there might be gas generated
line info present too.  Mind you, we turn off gas generation of line
info on seeing compiler generated line info, so any reference back
won't hit gas generated line info.  At least, if the view info is
sane.  Unfortunately, gas needs to handle mangled source.

	PR 28149
	* dwarf2dbg.c (purge_generated_debug): New function.
	(dwarf2_directive_filename): Call the above.
	(out_debug_line): Don't segfault after purging.
	* testsuite/gas/i386/dwarf2-line-4.d: Update expected output.
	* testsuite/gas/i386/dwarf4-line-1.d: Likewise.
	* testsuite/gas/i386/dwarf5-line-1.d: Likewise.
	* testsuite/gas/i386/dwarf5-line-2.d: Likewise.


-- 
Alan Modra
Australia Development Lab, IBM

Comments

Alan Modra via Binutils Sept. 20, 2021, 2:54 a.m. | #1
Linaro TCWG CI sent me a love letter.  :)

Fixes segfaults when building aarch64-linux kernel, due to only doing
part of the work necessary when allocating file numbers late.  I'd
missed looping over subsegments, which resulted in some u.filename
entries left around and later interpreted as u.view.

	PR 28149
	* dwarf2dbg.c (purge_generated_debug): Iterate over subsegs too.
	(dwarf2_finish): Call do_allocate_filenum for all subsegs too,
	in a separate loop before subsegs are chained.

diff --git a/gas/dwarf2dbg.c b/gas/dwarf2dbg.c
index 1250fcef54f..e2abe19c88a 100644
--- a/gas/dwarf2dbg.c
+++ b/gas/dwarf2dbg.c
@@ -792,19 +792,23 @@ purge_generated_debug (void)
 
   for (s = all_segs; s; s = s->next)
     {
-      struct line_subseg *lss = s->head;
-      struct line_entry *e, *next;
+      struct line_subseg *lss;
 
-      for (e = lss->head; e; e = next)
+      for (lss = s->head; lss; lss = lss->next)
 	{
-	  know (e->loc.filenum == -1u);
-	  next = e->next;
-	  free (e);
-	}
+	  struct line_entry *e, *next;
+
+	  for (e = lss->head; e; e = next)
+	    {
+	      know (e->loc.filenum == -1u);
+	      next = e->next;
+	      free (e);
+	    }
 
-      lss->head = NULL;
-      lss->ptail = &lss->head;
-      lss->pmove_tail = &lss->head;
+	  lss->head = NULL;
+	  lss->ptail = &lss->head;
+	  lss->pmove_tail = &lss->head;
+	}
     }
 }
 
@@ -2887,15 +2891,21 @@ dwarf2_finish (void)
 			     SEC_READONLY | SEC_DEBUGGING | SEC_OCTETS);
     }
 
+  for (s = all_segs; s; s = s->next)
+    {
+      struct line_subseg *lss;
+
+      for (lss = s->head; lss; lss = lss->next)
+	if (lss->head)
+	  do_allocate_filenum (lss->head);
+    }
+
   /* For each subsection, chain the debug entries together.  */
   for (s = all_segs; s; s = s->next)
     {
       struct line_subseg *lss = s->head;
       struct line_entry **ptail = lss->ptail;
 
-      if (lss->head && SEG_NORMAL (s->seg))
-	do_allocate_filenum (lss->head);
-
       /* Reset the initial view of the first subsection of the
 	 section.  */
       if (lss->head && lss->head->loc.u.view)

-- 
Alan Modra
Australia Development Lab, IBM

Patch

diff --git a/gas/dwarf2dbg.c b/gas/dwarf2dbg.c
index c6303ba94a6..1250fcef54f 100644
--- a/gas/dwarf2dbg.c
+++ b/gas/dwarf2dbg.c
@@ -782,6 +782,32 @@  do_allocate_filenum (struct line_entry *e)
   while (e);
 }
 
+/* Remove any generated line entries.  These don't live comfortably
+   with compiler generated line info.  */
+
+static void
+purge_generated_debug (void)
+{
+  struct line_seg *s;
+
+  for (s = all_segs; s; s = s->next)
+    {
+      struct line_subseg *lss = s->head;
+      struct line_entry *e, *next;
+
+      for (e = lss->head; e; e = next)
+	{
+	  know (e->loc.filenum == -1u);
+	  next = e->next;
+	  free (e);
+	}
+
+      lss->head = NULL;
+      lss->ptail = &lss->head;
+      lss->pmove_tail = &lss->head;
+    }
+}
+
 /* Allocate slot NUM in the .debug_line file table to FILENAME.
    If DIRNAME is not NULL or there is a directory component to FILENAME
    then this will be stored in the directory table, if not already present.
@@ -1146,6 +1172,8 @@  dwarf2_directive_filename (void)
 
   /* A .file directive implies compiler generated debug information is
      being supplied.  Turn off gas generated debug info.  */
+  if (debug_type == DEBUG_DWARF2)
+    purge_generated_debug ();
   debug_type = DEBUG_NONE;
 
   if (num != (unsigned int) num
@@ -2414,7 +2442,7 @@  out_debug_line (segT line_seg)
   for (s = all_segs; s; s = s->next)
     /* Paranoia - this check should have already have
        been handled in dwarf2_gen_line_info_1().  */
-    if (SEG_NORMAL (s->seg))
+    if (s->head->head && SEG_NORMAL (s->seg))
       process_entries (s->seg, s->head->head);
 
   if (flag_dwarf_sections)
diff --git a/gas/testsuite/gas/i386/dwarf2-line-4.d b/gas/testsuite/gas/i386/dwarf2-line-4.d
index a01fd0540f3..0403c3e2faf 100644
--- a/gas/testsuite/gas/i386/dwarf2-line-4.d
+++ b/gas/testsuite/gas/i386/dwarf2-line-4.d
@@ -28,21 +28,15 @@  Raw dump of debug contents of section \.z?debug_line:
   Opcode 11 has 0 args
   Opcode 12 has 1 arg
 
- The Directory Table \(offset 0x.*\):
-  .*
+ The Directory Table is empty\.
 
  The File Name Table \(offset 0x.*\):
   Entry	Dir	Time	Size	Name
   1	0	0	0	dwarf2-test.c
-  2	1	0	0	dwarf2-line-4.s
 
  Line Number Statements:
-  \[0x.*\]  Set File Name to entry 2 in the File Name Table
-  \[0x.*\]  Extended opcode 2: set Address to 0x0
-  \[0x.*\]  Special opcode 13: advance Address by 0 to 0x0 and Line by 8 to 9
-  \[0x.*\]  Set File Name to entry 1 in the File Name Table
-  \[0x.*\]  Advance Line by -8 to 1
-  \[0x.*\]  Special opcode 19: advance Address by 1 to 0x1 and Line by 0 to 1
+  \[0x.*\]  Extended opcode 2: set Address to 0x1
+  \[0x.*\]  Copy
   \[0x.*\]  Advance PC by 1 to 0x2
   \[0x.*\]  Extended opcode 1: End of Sequence
 
diff --git a/gas/testsuite/gas/i386/dwarf4-line-1.d b/gas/testsuite/gas/i386/dwarf4-line-1.d
index 8199efbb0c2..762bdce9934 100644
--- a/gas/testsuite/gas/i386/dwarf4-line-1.d
+++ b/gas/testsuite/gas/i386/dwarf4-line-1.d
@@ -29,24 +29,11 @@  Raw dump of debug contents of section \.z?debug_line:
   Opcode 11 has 0 args
   Opcode 12 has 1 arg
 
- The Directory Table \(offset 0x.*\):
-  1	.*/gas/testsuite/gas/i386
+ The Directory Table is empty\.
 
  The File Name Table \(offset 0x.*\):
   Entry	Dir	Time	Size	Name
   1	0	0	0	foo.c
   2	0	0	0	foo.h
-  3	1	0	0	dwarf4-line-1.s
-
- Line Number Statements:
-  \[0x.*\]  Set File Name to entry 2 in the File Name Table
-  \[0x.*\]  Extended opcode 2: set Address to 0x0
-  \[0x.*\]  Advance Line by 81 to 82
-  \[0x.*\]  Copy
-  \[0x.*\]  Set File Name to entry 3 in the File Name Table
-  \[0x.*\]  Advance Line by -73 to 9
-  \[0x.*\]  Special opcode 19: advance Address by 1 to 0x1 and Line by 0 to 9
-  \[0x.*\]  Advance PC by 3 to 0x4
-  \[0x.*\]  Extended opcode 1: End of Sequence
-
 
+ No Line Number Statements\.
diff --git a/gas/testsuite/gas/i386/dwarf5-line-1.d b/gas/testsuite/gas/i386/dwarf5-line-1.d
index 2c2cf5696c4..6ec51912dd1 100644
--- a/gas/testsuite/gas/i386/dwarf5-line-1.d
+++ b/gas/testsuite/gas/i386/dwarf5-line-1.d
@@ -31,22 +31,13 @@  Raw dump of debug contents of section \.z?debug_line:
   Opcode 11 has 0 args
   Opcode 12 has 1 arg
 
- The Directory Table \(offset 0x.*, lines 2, columns 1\):
+ The Directory Table \(offset 0x.*, lines 1, columns 1\):
   Entry	Name
   0	\(indirect line string, offset: 0x.*\): .*/gas/testsuite
-  1	\(indirect line string, offset: 0x.*\): .*/gas/testsuite/gas/i386
 
- The File Name Table \(offset 0x.*, lines 3, columns 3\):
+ The File Name Table \(offset 0x.*, lines 2, columns 3\):
   Entry	Dir	MD5				Name
   0	0 0xbbd69fc03ce253b2dbaab2522dd519ae	\(indirect line string, offset: 0x.*\): core.c
   1	0 0x0	\(indirect line string, offset: 0x.*\): types.h
-  2	1 0x0	\(indirect line string, offset: 0x.*\): dwarf5-line-1.s
-
- Line Number Statements:
-  \[0x.*\]  Set File Name to entry 2 in the File Name Table
-  \[0x.*\]  Extended opcode 2: set Address to 0x0
-  \[0x.*\]  Special opcode 8: advance Address by 0 to 0x0 and Line by 3 to 4
-  \[0x.*\]  Advance PC by 1 to 0x1
-  \[0x.*\]  Extended opcode 1: End of Sequence
-
 
+ No Line Number Statements\.
diff --git a/gas/testsuite/gas/i386/dwarf5-line-2.d b/gas/testsuite/gas/i386/dwarf5-line-2.d
index 85f98c8ab9c..4bb849bba34 100644
--- a/gas/testsuite/gas/i386/dwarf5-line-2.d
+++ b/gas/testsuite/gas/i386/dwarf5-line-2.d
@@ -31,20 +31,12 @@  Raw dump of debug contents of section \.z?debug_line:
   Opcode 11 has 0 args
   Opcode 12 has 1 arg
 
- The Directory Table \(offset 0x.*, lines 2, columns 1\):
+ The Directory Table \(offset 0x.*, lines 1, columns 1\):
   Entry	Name
   0	\(indirect line string, offset: 0x.*\): .*/gas/testsuite
-  1	\(indirect line string, offset: 0x.*\): .*/gas/testsuite/gas/i386
 
- The File Name Table \(offset 0x.*, lines 2, columns 3\):
+ The File Name Table \(offset 0x.*, lines 1, columns 3\):
   Entry	Dir	MD5				Name
   0	0 0xbbd69fc03ce253b2dbaab2522dd519ae	\(indirect line string, offset: 0x.*\): core.c
-  1	1 0x0	\(indirect line string, offset: .*\): dwarf5-line-2.s
-
- Line Number Statements:
-  \[0x.*\]  Extended opcode 2: set Address to 0x0
-  \[0x.*\]  Special opcode 8: advance Address by 0 to 0x0 and Line by 3 to 4
-  \[0x.*\]  Advance PC by 1 to 0x1
-  \[0x.*\]  Extended opcode 1: End of Sequence
-
 
+ No Line Number Statements\.