gas: Use the directory name in .file 0

Message ID 20210909162800.2362059-1-hjl.tools@gmail.com
State New
Headers show
Series
  • gas: Use the directory name in .file 0
Related show

Commit Message

Alan Modra via Binutils Sept. 9, 2021, 4:28 p.m.
DWARF5 allows .file 0 to take an optional directory name.  Set the entry
0 of the directory table to the directory name in .file 0.

	PR gas/28266
	* dwarf2dbg.c (get_directory_table_entry): Add an argument for
	the directory name in .file 0 and use it, instead of PWD.
	(allocate_filenum): Pass NULL to get_directory_table_entry.
	(allocate_filename_to_slot): Pass the incoming dirname to
	get_directory_table_entry.
	* testsuite/gas/elf/dwarf-5-file0-2.d: New file.
	* testsuite/gas/elf/dwarf-5-file0-2.s: Likewise.
	* testsuite/gas/elf/elf.exp: Run dwarf-5-file0-2.
---
 gas/dwarf2dbg.c                         |  12 ++-
 gas/testsuite/gas/elf/dwarf-5-file0-2.d |  15 ++++
 gas/testsuite/gas/elf/dwarf-5-file0-2.s | 111 ++++++++++++++++++++++++
 gas/testsuite/gas/elf/elf.exp           |   1 +
 4 files changed, 135 insertions(+), 4 deletions(-)
 create mode 100644 gas/testsuite/gas/elf/dwarf-5-file0-2.d
 create mode 100644 gas/testsuite/gas/elf/dwarf-5-file0-2.s

-- 
2.31.1

Comments

Alan Modra via Binutils Sept. 10, 2021, 1:54 a.m. | #1
On Thu, Sep 09, 2021 at 09:28:00AM -0700, H.J. Lu wrote:
> DWARF5 allows .file 0 to take an optional directory name.  Set the entry

> 0 of the directory table to the directory name in .file 0.

> 

> 	PR gas/28266

> 	* dwarf2dbg.c (get_directory_table_entry): Add an argument for

> 	the directory name in .file 0 and use it, instead of PWD.

> 	(allocate_filenum): Pass NULL to get_directory_table_entry.

> 	(allocate_filename_to_slot): Pass the incoming dirname to

> 	get_directory_table_entry.

> 	* testsuite/gas/elf/dwarf-5-file0-2.d: New file.

> 	* testsuite/gas/elf/dwarf-5-file0-2.s: Likewise.

> 	* testsuite/gas/elf/elf.exp: Run dwarf-5-file0-2.


OK.

-- 
Alan Modra
Australia Development Lab, IBM
Alan Modra via Binutils Sept. 10, 2021, 8:39 a.m. | #2
On Fri, Sep 10, 2021 at 11:24:52AM +0930, Alan Modra wrote:
> On Thu, Sep 09, 2021 at 09:28:00AM -0700, H.J. Lu wrote:

> > DWARF5 allows .file 0 to take an optional directory name.  Set the entry

> > 0 of the directory table to the directory name in .file 0.

> > 

> > 	PR gas/28266

> > 	* dwarf2dbg.c (get_directory_table_entry): Add an argument for

> > 	the directory name in .file 0 and use it, instead of PWD.

> > 	(allocate_filenum): Pass NULL to get_directory_table_entry.

> > 	(allocate_filename_to_slot): Pass the incoming dirname to

> > 	get_directory_table_entry.

> > 	* testsuite/gas/elf/dwarf-5-file0-2.d: New file.

> > 	* testsuite/gas/elf/dwarf-5-file0-2.s: Likewise.

> > 	* testsuite/gas/elf/elf.exp: Run dwarf-5-file0-2.


HJ, new testcases need testing on more targets.

alpha-linux  +FAIL: DWARF5 .file 0 dir file
alpha-netbsd  +FAIL: DWARF5 .file 0 dir file
alpha-unknown-freebsd4.7  +FAIL: DWARF5 .file 0 dir file
armeb-linuxeabi  +FAIL: DWARF5 .file 0 dir file
arm-elf  +FAIL: DWARF5 .file 0 dir file
arm-linuxeabi  +FAIL: DWARF5 .file 0 dir file
arm-nacl  +FAIL: DWARF5 .file 0 dir file
arm-netbsdelf  +FAIL: DWARF5 .file 0 dir file
arm-nto  +FAIL: DWARF5 .file 0 dir file
hppa64-hp-hpux11.23  +FAIL: DWARF5 .file 0 dir file
hppa64-linux  +FAIL: DWARF5 .file 0 dir file
hppa-linux  +FAIL: DWARF5 .file 0 dir file
ia64-elf  +FAIL: DWARF5 .file 0 dir file
ia64-freebsd5  +FAIL: DWARF5 .file 0 dir file
ia64-hpux  +FAIL: DWARF5 .file 0 dir file
ia64-linux  +FAIL: DWARF5 .file 0 dir file
ia64-netbsd  +FAIL: DWARF5 .file 0 dir file
mips64el-openbsd  +FAIL: DWARF5 .file 0 dir file
mips64-linux  +FAIL: DWARF5 .file 0 dir file
mips64-openbsd  +FAIL: DWARF5 .file 0 dir file
mipsel-linux-gnu  +FAIL: DWARF5 .file 0 dir file
mipsisa32el-linux  +FAIL: DWARF5 .file 0 dir file
mipsisa32r2el-elf  +FAIL: DWARF5 .file 0 dir file
mips-linux  +FAIL: DWARF5 .file 0 dir file
mips-sgi-irix6  +FAIL: DWARF5 .file 0 dir file
mipstx39-elf  +FAIL: DWARF5 .file 0 dir file
nios2-linux  +FAIL: DWARF5 .file 0 dir file
shle-unknown-netbsdelf  +FAIL: DWARF5 .file 0 dir file
sh-linux  +FAIL: DWARF5 .file 0 dir file
sh-nto  +FAIL: DWARF5 .file 0 dir file
sh-rtems  +FAIL: DWARF5 .file 0 dir file

Fixed with the following.

	PR gas/28266
	* testsuite/gas/elf/dwarf-5-file0-2.s: Use %object rather than
	@object, .4byte instead of .long, and .asciz instead of .string.

diff --git a/gas/testsuite/gas/elf/dwarf-5-file0-2.s b/gas/testsuite/gas/elf/dwarf-5-file0-2.s
index 135a03bf493..bab4a16b56b 100644
--- a/gas/testsuite/gas/elf/dwarf-5-file0-2.s
+++ b/gas/testsuite/gas/elf/dwarf-5-file0-2.s
@@ -5,7 +5,7 @@
 	.globl	x
 	.section	.bss
 	.balign 4
-	.type	x, @object
+	.type	x, %object
 	.size	x, 4
 x:
 	.zero	4
@@ -14,30 +14,30 @@ x:
 	.file 1 "test.c"
 	.section	.debug_info,"",%progbits
 .Ldebug_info0:
-	.long	0x32
+	.4byte	0x32
 	.2byte	0x5
 	.byte	0x1
 	.byte	0x4
-	.long	.Ldebug_abbrev0
+	.4byte	.Ldebug_abbrev0
 	.uleb128 0x1
-	.long	.LASF2
+	.4byte	.LASF2
 	.byte	0x1d
-	.long	.LASF0
-	.long	.LASF1
-	.long	.Ldebug_line0
+	.4byte	.LASF0
+	.4byte	.LASF1
+	.4byte	.Ldebug_line0
 	.uleb128 0x2
-	.string	"x"
+	.asciz	"x"
 	.byte	0x1
 	.byte	0x1
 	.byte	0x5
-	.long	0x2e
+	.4byte	0x2e
 	.uleb128 0x5
 	.byte	0x3
-	.long	x
+	.4byte	x
 	.uleb128 0x3
 	.byte	0x4
 	.byte	0x5
-	.string	"int"
+	.asciz	"int"
 	.byte	0
 	.section	.debug_abbrev,"",%progbits
 .Ldebug_abbrev0:
@@ -88,24 +88,24 @@ x:
 	.byte	0
 	.byte	0
 	.section	.debug_aranges,"",%progbits
-	.long	0x14
+	.4byte	0x14
 	.2byte	0x2
-	.long	.Ldebug_info0
+	.4byte	.Ldebug_info0
 	.byte	0x4
 	.byte	0
 	.2byte	0
 	.2byte	0
-	.long	0
-	.long	0
+	.4byte	0
+	.4byte	0
 	.section	.debug_line,"",%progbits
 .Ldebug_line0:
 	.section	.debug_str,"MS",%progbits,1
 .LASF2:
-	.string	"GNU C17 11.2.1 -g"
+	.asciz	"GNU C17 11.2.1 -g"
 	.section	.debug_line_str,"MS",%progbits,1
 .LASF1:
-	.string	"/example"
+	.asciz	"/example"
 .LASF0:
-	.string	"test.c"
+	.asciz	"test.c"
 	.ident	"GCC: (GNU) 11.2.1"
 	.section	.note.GNU-stack,"",%progbits


-- 
Alan Modra
Australia Development Lab, IBM
Alan Modra via Binutils Sept. 10, 2021, 1:12 p.m. | #3
On Fri, Sep 10, 2021 at 1:39 AM Alan Modra <amodra@gmail.com> wrote:
>

> On Fri, Sep 10, 2021 at 11:24:52AM +0930, Alan Modra wrote:

> > On Thu, Sep 09, 2021 at 09:28:00AM -0700, H.J. Lu wrote:

> > > DWARF5 allows .file 0 to take an optional directory name.  Set the entry

> > > 0 of the directory table to the directory name in .file 0.

> > >

> > >     PR gas/28266

> > >     * dwarf2dbg.c (get_directory_table_entry): Add an argument for

> > >     the directory name in .file 0 and use it, instead of PWD.

> > >     (allocate_filenum): Pass NULL to get_directory_table_entry.

> > >     (allocate_filename_to_slot): Pass the incoming dirname to

> > >     get_directory_table_entry.

> > >     * testsuite/gas/elf/dwarf-5-file0-2.d: New file.

> > >     * testsuite/gas/elf/dwarf-5-file0-2.s: Likewise.

> > >     * testsuite/gas/elf/elf.exp: Run dwarf-5-file0-2.

>

> HJ, new testcases need testing on more targets.

>

> alpha-linux  +FAIL: DWARF5 .file 0 dir file

> alpha-netbsd  +FAIL: DWARF5 .file 0 dir file

> alpha-unknown-freebsd4.7  +FAIL: DWARF5 .file 0 dir file

> armeb-linuxeabi  +FAIL: DWARF5 .file 0 dir file

> arm-elf  +FAIL: DWARF5 .file 0 dir file

> arm-linuxeabi  +FAIL: DWARF5 .file 0 dir file

> arm-nacl  +FAIL: DWARF5 .file 0 dir file

> arm-netbsdelf  +FAIL: DWARF5 .file 0 dir file

> arm-nto  +FAIL: DWARF5 .file 0 dir file

> hppa64-hp-hpux11.23  +FAIL: DWARF5 .file 0 dir file

> hppa64-linux  +FAIL: DWARF5 .file 0 dir file

> hppa-linux  +FAIL: DWARF5 .file 0 dir file

> ia64-elf  +FAIL: DWARF5 .file 0 dir file

> ia64-freebsd5  +FAIL: DWARF5 .file 0 dir file

> ia64-hpux  +FAIL: DWARF5 .file 0 dir file

> ia64-linux  +FAIL: DWARF5 .file 0 dir file

> ia64-netbsd  +FAIL: DWARF5 .file 0 dir file

> mips64el-openbsd  +FAIL: DWARF5 .file 0 dir file

> mips64-linux  +FAIL: DWARF5 .file 0 dir file

> mips64-openbsd  +FAIL: DWARF5 .file 0 dir file

> mipsel-linux-gnu  +FAIL: DWARF5 .file 0 dir file

> mipsisa32el-linux  +FAIL: DWARF5 .file 0 dir file

> mipsisa32r2el-elf  +FAIL: DWARF5 .file 0 dir file

> mips-linux  +FAIL: DWARF5 .file 0 dir file

> mips-sgi-irix6  +FAIL: DWARF5 .file 0 dir file

> mipstx39-elf  +FAIL: DWARF5 .file 0 dir file

> nios2-linux  +FAIL: DWARF5 .file 0 dir file

> shle-unknown-netbsdelf  +FAIL: DWARF5 .file 0 dir file

> sh-linux  +FAIL: DWARF5 .file 0 dir file

> sh-nto  +FAIL: DWARF5 .file 0 dir file

> sh-rtems  +FAIL: DWARF5 .file 0 dir file

>

> Fixed with the following.

>

>         PR gas/28266

>         * testsuite/gas/elf/dwarf-5-file0-2.s: Use %object rather than

>         @object, .4byte instead of .long, and .asciz instead of .string.

>


Thanks.

-- 
H.J.

Patch

diff --git a/gas/dwarf2dbg.c b/gas/dwarf2dbg.c
index 8f5248534ad..9e3437b8948 100644
--- a/gas/dwarf2dbg.c
+++ b/gas/dwarf2dbg.c
@@ -592,6 +592,7 @@  get_basename (const char * pathname)
 
 static unsigned int
 get_directory_table_entry (const char *dirname,
+			   const char *file0_dirname,
 			   size_t dirlen,
 			   bool can_use_zero)
 {
@@ -621,7 +622,7 @@  get_directory_table_entry (const char *dirname,
     {
       if (dirs == NULL || dirs[0] == NULL)
 	{
-	  const char * pwd = getpwd ();
+	  const char * pwd = file0_dirname ? file0_dirname : getpwd ();
 
 	  if (dwarf_level >= 5 && strcmp (dirname, pwd) != 0)
 	    {
@@ -630,7 +631,8 @@  get_directory_table_entry (const char *dirname,
 		 directory).  Since we are about to create a directory entry that
 		 is not the same, allocate the current directory first.
 		 FIXME: Alternatively we could generate an error message here.  */
-	      (void) get_directory_table_entry (pwd, strlen (pwd), true);
+	      (void) get_directory_table_entry (pwd, NULL, strlen (pwd),
+						true);
 	      d = 1;
 	    }
 	  else
@@ -726,7 +728,7 @@  allocate_filenum (const char * pathname)
   file = get_basename (pathname);
   dir_len = file - pathname;
 
-  dir = get_directory_table_entry (pathname, dir_len, false);
+  dir = get_directory_table_entry (pathname, NULL, dir_len, false);
 
   /* Do not use slot-0.  That is specifically reserved for use by
      the '.file 0 "name"' directive.  */
@@ -766,6 +768,7 @@  allocate_filename_to_slot (const char *dirname,
   const char *file;
   size_t dirlen;
   unsigned int i, d;
+  const char *file0_dirname = dirname;
 
   /* Short circuit the common case of adding the same pathname
      as last time.  */
@@ -856,7 +859,8 @@  allocate_filename_to_slot (const char *dirname,
       file = filename;
     }
 
-  d = get_directory_table_entry (dirname, dirlen, num == 0);
+  d = get_directory_table_entry (dirname, file0_dirname, dirlen,
+				 num == 0);
   i = num;
 
   if (! assign_file_to_slot (i, file, d))
diff --git a/gas/testsuite/gas/elf/dwarf-5-file0-2.d b/gas/testsuite/gas/elf/dwarf-5-file0-2.d
new file mode 100644
index 00000000000..4b3ed29f4c9
--- /dev/null
+++ b/gas/testsuite/gas/elf/dwarf-5-file0-2.d
@@ -0,0 +1,15 @@ 
+#as: --gdwarf-5
+#name: DWARF5 .file 0 dir file
+#readelf: -wl
+
+#...
+ The Directory Table \(offset 0x.*, lines 1, columns 1\):
+  Entry	Name
+#...
+  0	\(indirect line string, offset: 0x.*\): /example
+
+ The File Name Table \(offset 0x.*, lines 2, columns 2\):
+  Entry	Dir	Name
+  0	0	\(indirect line string, offset: 0x.*\): test.c
+  1	0	\(indirect line string, offset: 0x.*\): test.c
+#pass
diff --git a/gas/testsuite/gas/elf/dwarf-5-file0-2.s b/gas/testsuite/gas/elf/dwarf-5-file0-2.s
new file mode 100644
index 00000000000..1176b3c2499
--- /dev/null
+++ b/gas/testsuite/gas/elf/dwarf-5-file0-2.s
@@ -0,0 +1,111 @@ 
+	.file	"test.c"
+	.text
+.Ltext0:
+	.file 0 "/example" "test.c"
+	.globl	x
+	.section .bss
+	.balign 4
+	.type	x, @object
+	.size	x, 4
+x:
+	.zero	4
+	.text
+.Letext0:
+	.file 1 "test.c"
+	.section	.debug_info,"",%progbits
+.Ldebug_info0:
+	.long	0x36
+	.2byte	0x5
+	.byte	0x1
+	.byte	0x8
+	.long	.Ldebug_abbrev0
+	.uleb128 0x1
+	.long	.LASF2
+	.byte	0x1d
+	.long	.LASF0
+	.long	.LASF1
+	.long	.Ldebug_line0
+	.uleb128 0x2
+	.string	"x"
+	.byte	0x1
+	.byte	0x1
+	.byte	0x5
+	.long	0x32
+	.uleb128 0x9
+	.byte	0x3
+	.quad	x
+	.uleb128 0x3
+	.byte	0x4
+	.byte	0x5
+	.string	"int"
+	.byte	0
+	.section	.debug_abbrev,"",%progbits
+.Ldebug_abbrev0:
+	.uleb128 0x1
+	.uleb128 0x11
+	.byte	0x1
+	.uleb128 0x25
+	.uleb128 0xe
+	.uleb128 0x13
+	.uleb128 0xb
+	.uleb128 0x3
+	.uleb128 0x1f
+	.uleb128 0x1b
+	.uleb128 0x1f
+	.uleb128 0x10
+	.uleb128 0x17
+	.byte	0
+	.byte	0
+	.uleb128 0x2
+	.uleb128 0x34
+	.byte	0
+	.uleb128 0x3
+	.uleb128 0x8
+	.uleb128 0x3a
+	.uleb128 0xb
+	.uleb128 0x3b
+	.uleb128 0xb
+	.uleb128 0x39
+	.uleb128 0xb
+	.uleb128 0x49
+	.uleb128 0x13
+	.uleb128 0x3f
+	.uleb128 0x19
+	.uleb128 0x2
+	.uleb128 0x18
+	.byte	0
+	.byte	0
+	.uleb128 0x3
+	.uleb128 0x24
+	.byte	0
+	.uleb128 0xb
+	.uleb128 0xb
+	.uleb128 0x3e
+	.uleb128 0xb
+	.uleb128 0x3
+	.uleb128 0x8
+	.byte	0
+	.byte	0
+	.byte	0
+	.section	.debug_aranges,"",%progbits
+	.long	0x1c
+	.2byte	0x2
+	.long	.Ldebug_info0
+	.byte	0x8
+	.byte	0
+	.2byte	0
+	.2byte	0
+	.quad	0
+	.quad	0
+	.section	.debug_line,"",%progbits
+.Ldebug_line0:
+	.section	.debug_str,"MS",%progbits,1
+.LASF2:
+	.string	"GNU C17 11.2.1 -g"
+	.section	.debug_line_str,"MS",%progbits,1
+.LASF1:
+	.string	"/example"
+.LASF0:
+	.string	"test.c"
+	.ident	"GCC: (GNU) 11.2.1"
+	.section	.note.GNU-stack,"",%progbits
diff --git a/gas/testsuite/gas/elf/elf.exp b/gas/testsuite/gas/elf/elf.exp
index 18bc1db8c70..2485008d569 100644
--- a/gas/testsuite/gas/elf/elf.exp
+++ b/gas/testsuite/gas/elf/elf.exp
@@ -297,6 +297,7 @@  if { [is_elf_format] } then {
     run_dump_test "dwarf2-19" $dump_opts
     run_dump_test "dwarf2-20" $dump_opts
     run_dump_test "dwarf-5-file0" $dump_opts
+    run_dump_test "dwarf-5-file0-2" $dump_opts
     run_dump_test "dwarf-5-dir0" $dump_opts
     run_dump_test "dwarf-4-cu" $dump_opts
     run_dump_test "dwarf-5-cu" $dump_opts