[PATCH 6/9] gas: Reuse the input file entry in the file table

Mark Wielaard mark@klomp.org
Sun Nov 15 16:55:44 GMT 2020


From: "H.J. Lu" <hjl.tools@gmail.com>

Some instructions can be emitted (dwarf2_emit_insn is called) before the
first .file <NUMBER> directive has been seen, which allocates the input
file as the first file entry.  Reuse the input file entry in the file
table.

	PR gas/25878
	PR gas/26740
	* dwarf2dbg.c (file_entry): Remove auto_assigned.
	(assign_file_to_slot): Remove the auto_assign argument.
	(allocate_filenum): Updated.
	(allocate_filename_to_slot): Reuse the input file entry in the
	file table.
	(dwarf2_where): Replace as_where with as_where_physical.
	* testsuite/gas/i386/dwarf5-line-1.d: New file.
	* testsuite/gas/i386/dwarf5-line-1.s: Likewise.
	* testsuite/gas/i386/i386.exp: Run dwarf5-line-1.

(cherry picked from commit 6915020bb134ae29fd772295c66fd67b5944962d)

gas: Always use as_where for preprocessed assembly codes

Always clear the slot 1 if it was assigned to the input file before the
first .file <NUMBER> directive has been seen.  Always use as_where to
generate the correct debug infor for preprocessed assembly codes.

	PR gas/25878
	PR gas/26740
	* dwarf2dbg.c (allocate_filename_to_slot): Don't reuse the slot 1
	here.
	(dwarf2_where): Restore as_where.
	(dwarf2_directive_filename): Clear the slot 1 if it was assigned
	to the input file.
	* testsuite/gas/i386/dwarf5-line-2.d: New file.
	* testsuite/gas/i386/dwarf5-line-2.s: Likewise.
	* testsuite/gas/i386/dwarf5-line-3.d: Likewise.
	* testsuite/gas/i386/dwarf5-line-3.s: Likewise.
	* testsuite/gas/i386/i386.exp: Run dwarf5-line-2 and
	dwarf5-line-3.

(cherry picked from commit bd0c565edbf4ba8121fded38e389530d7fa6f963)

gas: Clear all auto-assigned file slots

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 (num_of_auto_assigned): New.
	(allocate_filenum): Increment num_of_auto_assigned.
	(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.

(cherry picked from commit ae9d2233e61a98ff8dba56be10219aa5306ffc9a)
---
 gas/ChangeLog                          | 44 ++++++++++++++++++++
 gas/dwarf2dbg.c                        | 57 +++++++++++++-------------
 gas/testsuite/gas/i386/dwarf4-line-1.d | 50 ++++++++++++++++++++++
 gas/testsuite/gas/i386/dwarf4-line-1.s | 14 +++++++
 gas/testsuite/gas/i386/dwarf5-line-1.d | 50 ++++++++++++++++++++++
 gas/testsuite/gas/i386/dwarf5-line-1.s |  6 +++
 gas/testsuite/gas/i386/dwarf5-line-2.d | 49 ++++++++++++++++++++++
 gas/testsuite/gas/i386/dwarf5-line-2.s |  5 +++
 gas/testsuite/gas/i386/dwarf5-line-3.d | 49 ++++++++++++++++++++++
 gas/testsuite/gas/i386/dwarf5-line-3.s | 10 +++++
 gas/testsuite/gas/i386/i386.exp        |  4 ++
 11 files changed, 309 insertions(+), 29 deletions(-)
 create mode 100644 gas/testsuite/gas/i386/dwarf4-line-1.d
 create mode 100644 gas/testsuite/gas/i386/dwarf4-line-1.s
 create mode 100644 gas/testsuite/gas/i386/dwarf5-line-1.d
 create mode 100644 gas/testsuite/gas/i386/dwarf5-line-1.s
 create mode 100644 gas/testsuite/gas/i386/dwarf5-line-2.d
 create mode 100644 gas/testsuite/gas/i386/dwarf5-line-2.s
 create mode 100644 gas/testsuite/gas/i386/dwarf5-line-3.d
 create mode 100644 gas/testsuite/gas/i386/dwarf5-line-3.s

diff --git a/gas/ChangeLog b/gas/ChangeLog
index 377fbf752ee..3ac2f7b72e2 100644
--- a/gas/ChangeLog
+++ b/gas/ChangeLog
@@ -1,3 +1,47 @@
+2020-11-15  Mark Wielaard  <mark@klomp.org>
+
+	Backport from the mainline:
+	2020-10-16  H.J. Lu  <hongjiu.lu@intel.com>
+
+	PR gas/25878
+	PR gas/26740
+	* dwarf2dbg.c (file_entry): Remove auto_assigned.
+	(assign_file_to_slot): Remove the auto_assign argument.
+	(allocate_filenum): Updated.
+	(allocate_filename_to_slot): Reuse the input file entry in the
+	file table.
+	(dwarf2_where): Replace as_where with as_where_physical.
+	* testsuite/gas/i386/dwarf5-line-1.d: New file.
+	* testsuite/gas/i386/dwarf5-line-1.s: Likewise.
+	* testsuite/gas/i386/i386.exp: Run dwarf5-line-1.
+
+	2020-10-17  H.J. Lu  <hongjiu.lu@intel.com>
+
+	PR gas/25878
+	PR gas/26740
+	* dwarf2dbg.c (allocate_filename_to_slot): Don't reuse the slot 1
+	here.
+	(dwarf2_where): Restore as_where.
+	(dwarf2_directive_filename): Clear the slot 1 if it was assigned
+	to the input file.
+	* testsuite/gas/i386/dwarf5-line-2.d: New file.
+	* testsuite/gas/i386/dwarf5-line-2.s: Likewise.
+	* testsuite/gas/i386/dwarf5-line-3.d: Likewise.
+	* testsuite/gas/i386/dwarf5-line-3.s: Likewise.
+	* testsuite/gas/i386/i386.exp: Run dwarf5-line-2 and
+	dwarf5-line-3.
+
+	2020-10-26  H.J. Lu  <hongjiu.lu@intel.com>
+
+	PR gas/26778
+	* * dwarf2dbg.c (num_of_auto_assigned): New.
+	(allocate_filenum): Increment num_of_auto_assigned.
+	(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.
+
 2020-11-15  Mark Wielaard  <mark@klomp.org>
 
 	Backport from the mainline:
diff --git a/gas/dwarf2dbg.c b/gas/dwarf2dbg.c
index 6899a840eff..1160cafc13e 100644
--- a/gas/dwarf2dbg.c
+++ b/gas/dwarf2dbg.c
@@ -211,7 +211,6 @@ struct file_entry
 {
   const char *   filename;
   unsigned int   dir;
-  bfd_boolean    auto_assigned;
   unsigned char  md5[NUM_MD5_BYTES];
 };
 
@@ -219,6 +218,7 @@ struct file_entry
 static struct file_entry *files;
 static unsigned int files_in_use;
 static unsigned int files_allocated;
+static unsigned int num_of_auto_assigned;
 
 /* Table of directories used by .debug_line.  */
 static char **       dirs = NULL;
@@ -633,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, bfd_boolean auto_assign)
+assign_file_to_slot (unsigned long i, const char *file, unsigned int dir)
 {
   if (i >= files_allocated)
     {
@@ -653,7 +653,6 @@ assign_file_to_slot (unsigned long i, const char *file, unsigned int dir, bfd_bo
 
   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)
@@ -717,9 +716,11 @@ allocate_filenum (const char * pathname)
 	return i;
       }
 
-  if (!assign_file_to_slot (i, file, dir, TRUE))
+  if (!assign_file_to_slot (i, file, dir))
     return -1;
 
+  num_of_auto_assigned++;
+
   last_used = i;
   last_used_dir_len = dir_len;
 
@@ -792,30 +793,15 @@ allocate_filename_to_slot (const char *  dirname,
 	}
 
     fail:
-      /* If NUM was previously allocated automatically then
-	 choose another slot for it, so that we can reuse NUM.  */
-      if (files[num].auto_assigned)
-	{
-	  /* Find an unused slot.  */
-	  for (i = 1; i < files_in_use; ++i)
-	    if (files[i].filename == NULL)
-	      break;
-	  if (! assign_file_to_slot (i, files[num].filename, files[num].dir, TRUE))
-	    return FALSE;
-	  files[num].filename = NULL;
-	}
-      else
-	{
-	  as_bad (_("file table slot %u is already occupied by a different file (%s%s%s vs %s%s%s)"),
-		  num,
-		  dir == NULL ? "" : dir,
-		  dir == NULL ? "" : "/",
-		  files[num].filename,
-		  dirname == NULL ? "" : dirname,
-		  dirname == NULL ? "" : "/",
-		  filename);
-	  return FALSE;
-	}
+      as_bad (_("file table slot %u is already occupied by a different file (%s%s%s vs %s%s%s)"),
+	      num,
+	      dir == NULL ? "" : dir,
+	      dir == NULL ? "" : "/",
+	      files[num].filename,
+	      dirname == NULL ? "" : dirname,
+	      dirname == NULL ? "" : "/",
+	      filename);
+      return FALSE;
     }
 
   if (dirname == NULL)
@@ -833,7 +819,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, FALSE))
+  if (! assign_file_to_slot (i, file, d))
     return FALSE;
 
   if (with_md5)
@@ -1030,6 +1016,7 @@ dwarf2_directive_filename (void)
   char *filename;
   const char * dirname = NULL;
   int filename_len;
+  unsigned int i;
 
   /* Continue to accept a bare string and pass it off.  */
   SKIP_WHITESPACE ();
@@ -1096,6 +1083,18 @@ dwarf2_directive_filename (void)
       return NULL;
     }
 
+  if (num_of_auto_assigned)
+    {
+      /* Clear slots auto-assigned before the first .file <NUMBER>
+	 directive was seen.  */
+      if (files_in_use != (num_of_auto_assigned + 1))
+	abort ();
+      for (i = 1; i < files_in_use; i++)
+	files[i].filename = NULL;
+      files_in_use = 0;
+      num_of_auto_assigned = 0;
+    }
+
   if (! allocate_filename_to_slot (dirname, filename, (unsigned int) num,
 				   with_md5))
     return NULL;
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/dwarf5-line-1.d b/gas/testsuite/gas/i386/dwarf5-line-1.d
new file mode 100644
index 00000000000..7d602d0594f
--- /dev/null
+++ b/gas/testsuite/gas/i386/dwarf5-line-1.d
@@ -0,0 +1,50 @@
+#as: -gdwarf-5
+#readelf: -wl
+#name: DWARF5 .debug_line 1
+
+Raw dump of debug contents of section \.z?debug_line:
+
+  Offset:                      0x0
+  Length:                      .*
+  DWARF Version:               5
+  Address size \(bytes\):        .*
+  Segment selector \(bytes\):    0
+  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.*, lines 2, columns 1\):
+  Entry	Name
+  0	\(indirect line string, offset: 0x.*\): .*/gas/testsuite/gas/i386
+  1	\(indirect line string, offset: 0x.*\): .*/gas/testsuite/gas/i386
+
+ 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 0x00000000000000000000000000000000	\(indirect line string, offset: 0x.*\): types.h
+
+ 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
+
+
diff --git a/gas/testsuite/gas/i386/dwarf5-line-1.s b/gas/testsuite/gas/i386/dwarf5-line-1.s
new file mode 100644
index 00000000000..6e343ad0d36
--- /dev/null
+++ b/gas/testsuite/gas/i386/dwarf5-line-1.s
@@ -0,0 +1,6 @@
+	.text
+	.global kretprobe_trampoline
+kretprobe_trampoline:
+	ret
+	.file	0 "core.c" md5 0xbbd69fc03ce253b2dbaab2522dd519ae
+	.file	1 "types.h"
diff --git a/gas/testsuite/gas/i386/dwarf5-line-2.d b/gas/testsuite/gas/i386/dwarf5-line-2.d
new file mode 100644
index 00000000000..302a2d8fcc4
--- /dev/null
+++ b/gas/testsuite/gas/i386/dwarf5-line-2.d
@@ -0,0 +1,49 @@
+#as: -gdwarf-5
+#readelf: -wl
+#name: DWARF5 .debug_line 2
+
+Raw dump of debug contents of section \.z?debug_line:
+
+  Offset:                      0x0
+  Length:                      .*
+  DWARF Version:               5
+  Address size \(bytes\):        .*
+  Segment selector \(bytes\):    0
+  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.*, lines 2, columns 1\):
+  Entry	Name
+  0	\(indirect line string, offset: 0x.*\): .*/gas/testsuite/gas/i386
+  1	\(indirect line string, offset: 0x.*\): .*/gas/testsuite/gas/i386
+
+ The File Name Table \(offset 0x.*, lines 1, columns 3\):
+  Entry	Dir	MD5				Name
+  0	0 0xbbd69fc03ce253b2dbaab2522dd519ae	\(indirect line string, offset: 0x.*\): core.c
+
+ 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
+
+
diff --git a/gas/testsuite/gas/i386/dwarf5-line-2.s b/gas/testsuite/gas/i386/dwarf5-line-2.s
new file mode 100644
index 00000000000..4af7d7061c9
--- /dev/null
+++ b/gas/testsuite/gas/i386/dwarf5-line-2.s
@@ -0,0 +1,5 @@
+	.text
+	.global kretprobe_trampoline
+kretprobe_trampoline:
+	ret
+	.file	0 "core.c" md5 0xbbd69fc03ce253b2dbaab2522dd519ae
diff --git a/gas/testsuite/gas/i386/dwarf5-line-3.d b/gas/testsuite/gas/i386/dwarf5-line-3.d
new file mode 100644
index 00000000000..6f4ebf04718
--- /dev/null
+++ b/gas/testsuite/gas/i386/dwarf5-line-3.d
@@ -0,0 +1,49 @@
+#as: -g -gdwarf-5
+#readelf: -wl
+#name: DWARF5 .debug_line 2
+
+Raw dump of debug contents of section \.z?debug_line:
+
+  Offset:                      0x0
+  Length:                      .*
+  DWARF Version:               5
+  Address size \(bytes\):        .*
+  Segment selector \(bytes\):    0
+  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.*, lines 1, columns 1\):
+  Entry	Name
+  0	\(indirect line string, offset: 0x.*\): .*
+
+ The File Name Table \(offset 0x.*, lines 2, columns 2\):
+  Entry	Dir	Name
+  0	0	\(indirect line string, offset: 0x.*\): dwarf5-line-2.S
+  1	0	\(indirect line string, offset: 0x.*\): dwarf5-line-2.S
+
+ Line Number Statements:
+  \[0x.*\]  Extended opcode 2: set Address to 0x0
+  \[0x.*\]  Special opcode 7: advance Address by 0 to 0x0 and Line by 2 to 3
+  \[0x.*\]  Advance PC by 1 to 0x1
+  \[0x.*\]  Extended opcode 1: End of Sequence
+
+
diff --git a/gas/testsuite/gas/i386/dwarf5-line-3.s b/gas/testsuite/gas/i386/dwarf5-line-3.s
new file mode 100644
index 00000000000..52e259d1033
--- /dev/null
+++ b/gas/testsuite/gas/i386/dwarf5-line-3.s
@@ -0,0 +1,10 @@
+# 1 "foo.S"
+# 1 "<built-in>"
+# 1 "<command-line>"
+# 31 "<command-line>"
+# 1 "/usr/include/stdc-predef.h" 1 3 4
+# 32 "<command-line>" 2
+# 1 "dwarf5-line-2.S"
+ .text
+lbasename:
+ .nop
diff --git a/gas/testsuite/gas/i386/i386.exp b/gas/testsuite/gas/i386/i386.exp
index 3580d5d7913..3039c36a30a 100644
--- a/gas/testsuite/gas/i386/i386.exp
+++ b/gas/testsuite/gas/i386/i386.exp
@@ -596,6 +596,10 @@ if [expr ([istarget "i*86-*-*"] ||  [istarget "x86_64-*-*"]) && [gas_32_check]]
 	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"
 
 	run_dump_test "dw2-compress-2"
 	run_dump_test "dw2-compressed-2"
-- 
2.18.4



More information about the Binutils mailing list