[PATCH] gas: Clear all auto-assigned file slots

H.J. Lu hjl.tools@gmail.com
Fri Oct 23 19:50:35 GMT 2020


Since a file slot is auto-assigned for the #APP marker appeared before
the first .file <NUMBER> directive has been seen, clear all auto-assigned
file slots when seeing the first .file <NUMBER> directive.

	PR gas/26778
	* * dwarf2dbg.c (file_entry): Restore auto_assigned.
	(assign_file_to_slot): Restore the auto_assign argument.
	(allocate_filenum): Pass TRUE to file_entry.
	(allocate_filename_to_slot): Pass FALSE to file_entry.
	(dwarf2_directive_filename): Clear the slots auto-assigned
	before the first .file <NUMBER> directive was seen.
	* testsuite/gas/i386/dwarf4-line-1.d: New file.
	* testsuite/gas/i386/dwarf4-line-1.s: Likewise.
	* testsuite/gas/i386/i386.exp: Run dwarf4-line-1.
---
 gas/dwarf2dbg.c                        | 32 ++++++++---------
 gas/testsuite/gas/i386/dwarf4-line-1.d | 50 ++++++++++++++++++++++++++
 gas/testsuite/gas/i386/dwarf4-line-1.s | 14 ++++++++
 gas/testsuite/gas/i386/i386.exp        |  1 +
 4 files changed, 81 insertions(+), 16 deletions(-)
 create mode 100644 gas/testsuite/gas/i386/dwarf4-line-1.d
 create mode 100644 gas/testsuite/gas/i386/dwarf4-line-1.s

diff --git a/gas/dwarf2dbg.c b/gas/dwarf2dbg.c
index 75ef7c43e39..610d38ae6cc 100644
--- a/gas/dwarf2dbg.c
+++ b/gas/dwarf2dbg.c
@@ -211,6 +211,7 @@ struct file_entry
 {
   const char *   filename;
   unsigned int   dir;
+  bfd_boolean    auto_assigned;
   unsigned char  md5[NUM_MD5_BYTES];
 };
 
@@ -632,7 +633,7 @@ get_directory_table_entry (const char *  dirname,
 }
 
 static bfd_boolean
-assign_file_to_slot (unsigned long i, const char *file, unsigned int dir)
+assign_file_to_slot (unsigned long i, const char *file, unsigned int dir, bfd_boolean auto_assign)
 {
   if (i >= files_allocated)
     {
@@ -652,6 +653,7 @@ assign_file_to_slot (unsigned long i, const char *file, unsigned int dir)
 
   files[i].filename = file;
   files[i].dir = dir;
+  files[i].auto_assigned = auto_assign;
   memset (files[i].md5, 0, NUM_MD5_BYTES);
 
   if (files_in_use < i + 1)
@@ -715,7 +717,7 @@ allocate_filenum (const char * pathname)
 	return i;
       }
 
-  if (!assign_file_to_slot (i, file, dir))
+  if (!assign_file_to_slot (i, file, dir, TRUE))
     return -1;
 
   last_used = i;
@@ -816,7 +818,7 @@ allocate_filename_to_slot (const char *  dirname,
   d = get_directory_table_entry (dirname, dirlen, num == 0);
   i = num;
 
-  if (! assign_file_to_slot (i, file, d))
+  if (! assign_file_to_slot (i, file, d, FALSE))
     return FALSE;
 
   if (with_md5)
@@ -1013,6 +1015,7 @@ dwarf2_directive_filename (void)
   char *filename;
   const char * dirname = NULL;
   int filename_len;
+  unsigned int i, num_of_auto_assigns;
 
   /* Continue to accept a bare string and pass it off.  */
   SKIP_WHITESPACE ();
@@ -1079,19 +1082,16 @@ dwarf2_directive_filename (void)
       return NULL;
     }
 
-  if (files_in_use == 2)
-    {
-      /* Clear the slot 1 if it was assigned to the input file before
-	 the first .file <NUMBER> directive was seen.  */
-      unsigned int lineno;
-      const char *file = as_where (&lineno);
-      file = get_basename (file);
-      if (filename_cmp (file, files[1].filename) == 0)
-	{
-	  files[1].filename = NULL;
-	  files_in_use = 0;
-	}
-    }
+  num_of_auto_assigns = 0;
+  for (i = 1; i < files_in_use; i++)
+    if (files[i].auto_assigned)
+      {
+	/* Clear the slot auto-assigned before the first .file <NUMBER>
+	   directive was seen.  */
+	files[i].filename = NULL;
+	num_of_auto_assigns++;
+      }
+  files_in_use -= num_of_auto_assigns;
 
   if (! allocate_filename_to_slot (dirname, filename, (unsigned int) num,
 				   with_md5))
diff --git a/gas/testsuite/gas/i386/dwarf4-line-1.d b/gas/testsuite/gas/i386/dwarf4-line-1.d
new file mode 100644
index 00000000000..4f8321e9bfd
--- /dev/null
+++ b/gas/testsuite/gas/i386/dwarf4-line-1.d
@@ -0,0 +1,50 @@
+#as: -gdwarf-4
+#readelf: -wl
+#name: DWARF4 .debug_line 1
+
+Raw dump of debug contents of section \.z?debug_line:
+
+  Offset:                      0x0
+  Length:                      .*
+  DWARF Version:               4
+  Prologue Length:             .*
+  Minimum Instruction Length:  1
+  Maximum Ops per Instruction: 1
+  Initial value of 'is_stmt':  1
+  Line Base:                   -5
+  Line Range:                  14
+  Opcode Base:                 13
+
+ Opcodes:
+  Opcode 1 has 0 args
+  Opcode 2 has 1 arg
+  Opcode 3 has 1 arg
+  Opcode 4 has 1 arg
+  Opcode 5 has 1 arg
+  Opcode 6 has 0 args
+  Opcode 7 has 0 args
+  Opcode 8 has 0 args
+  Opcode 9 has 1 arg
+  Opcode 10 has 0 args
+  Opcode 11 has 0 args
+  Opcode 12 has 1 arg
+
+ The Directory Table \(offset 0x.*\):
+  1	.*/gas/testsuite/gas/i386
+
+ The File Name Table \(offset 0x.*\):
+  Entry	Dir	Time	Size	Name
+  1	0	0	0	foo.c
+  2	0	0	0	foo.h
+
+ Line Number Statements:
+  \[0x.*\]  Extended opcode 2: set Address to 0x0
+  \[0x.*\]  Advance Line by 81 to 82
+  \[0x.*\]  Copy
+  \[0x.*\]  Set File Name to entry 2 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
+
+
diff --git a/gas/testsuite/gas/i386/dwarf4-line-1.s b/gas/testsuite/gas/i386/dwarf4-line-1.s
new file mode 100644
index 00000000000..e558fdc0507
--- /dev/null
+++ b/gas/testsuite/gas/i386/dwarf4-line-1.s
@@ -0,0 +1,14 @@
+	.file	"foo.c"
+	.text
+bar:
+#APP
+# 82 "foo.h" 1
+	nop
+# 0 "" 2
+#NO_APP
+	ret
+foo:
+	.file 1 "foo.c"
+	nop
+	.file 2 "foo.h"
+	ret
diff --git a/gas/testsuite/gas/i386/i386.exp b/gas/testsuite/gas/i386/i386.exp
index 1e761e99d6b..c85ced1c918 100644
--- a/gas/testsuite/gas/i386/i386.exp
+++ b/gas/testsuite/gas/i386/i386.exp
@@ -619,6 +619,7 @@ if [gas_32_check] then {
 	run_dump_test "dwarf2-line-2"
 	run_dump_test "dwarf2-line-3"
 	run_dump_test "dwarf2-line-4"
+	run_dump_test "dwarf4-line-1"
 	run_dump_test "dwarf5-line-1"
 	run_dump_test "dwarf5-line-2"
 	run_dump_test "dwarf5-line-3"
-- 
2.26.2



More information about the Binutils mailing list