fa66961
commit 58f3b6a3495074da39ba6e19935e4401dcbacf88
fa66961
Author: H.J. Lu <hjl.tools@gmail.com>
fa66961
Date:   Thu Sep 9 09:14:42 2021 -0700
fa66961
fa66961
    gas: Use the directory name in .file 0
fa66961
    
fa66961
    DWARF5 allows .file 0 to take an optional directory name.  Set the entry
fa66961
    0 of the directory table to the directory name in .file 0.
fa66961
    
fa66961
            PR gas/28266
fa66961
            * dwarf2dbg.c (get_directory_table_entry): Add an argument for
fa66961
            the directory name in .file 0 and use it, instead of PWD.
fa66961
            (allocate_filenum): Pass NULL to get_directory_table_entry.
fa66961
            (allocate_filename_to_slot): Pass the incoming dirname to
fa66961
            get_directory_table_entry.
fa66961
            * testsuite/gas/elf/dwarf-5-file0-2.d: New file.
fa66961
            * testsuite/gas/elf/dwarf-5-file0-2.s: Likewise.
fa66961
            * testsuite/gas/elf/elf.exp: Run dwarf-5-file0-2.
fa66961
fa66961
diff --git a/gas/dwarf2dbg.c b/gas/dwarf2dbg.c
fa66961
index 8f5248534ad..9e3437b8948 100644
fa66961
--- a/gas/dwarf2dbg.c
fa66961
+++ b/gas/dwarf2dbg.c
fa66961
@@ -592,6 +592,7 @@ get_basename (const char * pathname)
fa66961
 
fa66961
 static unsigned int
fa66961
 get_directory_table_entry (const char *dirname,
fa66961
+			   const char *file0_dirname,
fa66961
 			   size_t dirlen,
fa66961
 			   bool can_use_zero)
fa66961
 {
fa66961
@@ -621,7 +622,7 @@ get_directory_table_entry (const char *dirname,
fa66961
     {
fa66961
       if (dirs == NULL || dirs[0] == NULL)
fa66961
 	{
fa66961
-	  const char * pwd = getpwd ();
fa66961
+	  const char * pwd = file0_dirname ? file0_dirname : getpwd ();
fa66961
 
fa66961
 	  if (dwarf_level >= 5 && strcmp (dirname, pwd) != 0)
fa66961
 	    {
fa66961
@@ -630,7 +631,8 @@ get_directory_table_entry (const char *dirname,
fa66961
 		 directory).  Since we are about to create a directory entry that
fa66961
 		 is not the same, allocate the current directory first.
fa66961
 		 FIXME: Alternatively we could generate an error message here.  */
fa66961
-	      (void) get_directory_table_entry (pwd, strlen (pwd), true);
fa66961
+	      (void) get_directory_table_entry (pwd, NULL, strlen (pwd),
fa66961
+						true);
fa66961
 	      d = 1;
fa66961
 	    }
fa66961
 	  else
fa66961
@@ -726,7 +728,7 @@ allocate_filenum (const char * pathname)
fa66961
   file = get_basename (pathname);
fa66961
   dir_len = file - pathname;
fa66961
 
fa66961
-  dir = get_directory_table_entry (pathname, dir_len, false);
fa66961
+  dir = get_directory_table_entry (pathname, NULL, dir_len, false);
fa66961
 
fa66961
   /* Do not use slot-0.  That is specifically reserved for use by
fa66961
      the '.file 0 "name"' directive.  */
fa66961
@@ -766,6 +768,7 @@ allocate_filename_to_slot (const char *dirname,
fa66961
   const char *file;
fa66961
   size_t dirlen;
fa66961
   unsigned int i, d;
fa66961
+  const char *file0_dirname = dirname;
fa66961
 
fa66961
   /* Short circuit the common case of adding the same pathname
fa66961
      as last time.  */
fa66961
@@ -856,7 +859,8 @@ allocate_filename_to_slot (const char *dirname,
fa66961
       file = filename;
fa66961
     }
fa66961
 
fa66961
-  d = get_directory_table_entry (dirname, dirlen, num == 0);
fa66961
+  d = get_directory_table_entry (dirname, file0_dirname, dirlen,
fa66961
+				 num == 0);
fa66961
   i = num;
fa66961
 
fa66961
   if (! assign_file_to_slot (i, file, d))
fa66961
diff --git a/gas/testsuite/gas/elf/dwarf-5-file0-2.d b/gas/testsuite/gas/elf/dwarf-5-file0-2.d
fa66961
new file mode 100644
fa66961
index 00000000000..4b3ed29f4c9
fa66961
--- /dev/null
fa66961
+++ b/gas/testsuite/gas/elf/dwarf-5-file0-2.d
fa66961
@@ -0,0 +1,15 @@
fa66961
+#as: --gdwarf-5
fa66961
+#name: DWARF5 .file 0 dir file
fa66961
+#readelf: -wl
fa66961
+
fa66961
+#...
fa66961
+ The Directory Table \(offset 0x.*, lines 1, columns 1\):
fa66961
+  Entry	Name
fa66961
+#...
fa66961
+  0	\(indirect line string, offset: 0x.*\): /example
fa66961
+
fa66961
+ The File Name Table \(offset 0x.*, lines 2, columns 2\):
fa66961
+  Entry	Dir	Name
fa66961
+  0	0	\(indirect line string, offset: 0x.*\): test.c
fa66961
+  1	0	\(indirect line string, offset: 0x.*\): test.c
fa66961
+#pass
fa66961
diff --git a/gas/testsuite/gas/elf/dwarf-5-file0-2.s b/gas/testsuite/gas/elf/dwarf-5-file0-2.s
fa66961
new file mode 100644
fa66961
index 00000000000..135a03bf493
fa66961
--- /dev/null
fa66961
+++ b/gas/testsuite/gas/elf/dwarf-5-file0-2.s
fa66961
@@ -0,0 +1,111 @@
fa66961
+	.file	"test.c"
fa66961
+	.text
fa66961
+.Ltext0:
fa66961
+	.file 0 "/example" "test.c"
fa66961
+	.globl	x
fa66961
+	.section	.bss
fa66961
+	.balign 4
fa66961
+	.type	x, @object
fa66961
+	.size	x, 4
fa66961
+x:
fa66961
+	.zero	4
fa66961
+	.text
fa66961
+.Letext0:
fa66961
+	.file 1 "test.c"
fa66961
+	.section	.debug_info,"",%progbits
fa66961
+.Ldebug_info0:
fa66961
+	.long	0x32
fa66961
+	.2byte	0x5
fa66961
+	.byte	0x1
fa66961
+	.byte	0x4
fa66961
+	.long	.Ldebug_abbrev0
fa66961
+	.uleb128 0x1
fa66961
+	.long	.LASF2
fa66961
+	.byte	0x1d
fa66961
+	.long	.LASF0
fa66961
+	.long	.LASF1
fa66961
+	.long	.Ldebug_line0
fa66961
+	.uleb128 0x2
fa66961
+	.string	"x"
fa66961
+	.byte	0x1
fa66961
+	.byte	0x1
fa66961
+	.byte	0x5
fa66961
+	.long	0x2e
fa66961
+	.uleb128 0x5
fa66961
+	.byte	0x3
fa66961
+	.long	x
fa66961
+	.uleb128 0x3
fa66961
+	.byte	0x4
fa66961
+	.byte	0x5
fa66961
+	.string	"int"
fa66961
+	.byte	0
fa66961
+	.section	.debug_abbrev,"",%progbits
fa66961
+.Ldebug_abbrev0:
fa66961
+	.uleb128 0x1
fa66961
+	.uleb128 0x11
fa66961
+	.byte	0x1
fa66961
+	.uleb128 0x25
fa66961
+	.uleb128 0xe
fa66961
+	.uleb128 0x13
fa66961
+	.uleb128 0xb
fa66961
+	.uleb128 0x3
fa66961
+	.uleb128 0x1f
fa66961
+	.uleb128 0x1b
fa66961
+	.uleb128 0x1f
fa66961
+	.uleb128 0x10
fa66961
+	.uleb128 0x17
fa66961
+	.byte	0
fa66961
+	.byte	0
fa66961
+	.uleb128 0x2
fa66961
+	.uleb128 0x34
fa66961
+	.byte	0
fa66961
+	.uleb128 0x3
fa66961
+	.uleb128 0x8
fa66961
+	.uleb128 0x3a
fa66961
+	.uleb128 0xb
fa66961
+	.uleb128 0x3b
fa66961
+	.uleb128 0xb
fa66961
+	.uleb128 0x39
fa66961
+	.uleb128 0xb
fa66961
+	.uleb128 0x49
fa66961
+	.uleb128 0x13
fa66961
+	.uleb128 0x3f
fa66961
+	.uleb128 0x19
fa66961
+	.uleb128 0x2
fa66961
+	.uleb128 0x18
fa66961
+	.byte	0
fa66961
+	.byte	0
fa66961
+	.uleb128 0x3
fa66961
+	.uleb128 0x24
fa66961
+	.byte	0
fa66961
+	.uleb128 0xb
fa66961
+	.uleb128 0xb
fa66961
+	.uleb128 0x3e
fa66961
+	.uleb128 0xb
fa66961
+	.uleb128 0x3
fa66961
+	.uleb128 0x8
fa66961
+	.byte	0
fa66961
+	.byte	0
fa66961
+	.byte	0
fa66961
+	.section	.debug_aranges,"",%progbits
fa66961
+	.long	0x14
fa66961
+	.2byte	0x2
fa66961
+	.long	.Ldebug_info0
fa66961
+	.byte	0x4
fa66961
+	.byte	0
fa66961
+	.2byte	0
fa66961
+	.2byte	0
fa66961
+	.long	0
fa66961
+	.long	0
fa66961
+	.section	.debug_line,"",%progbits
fa66961
+.Ldebug_line0:
fa66961
+	.section	.debug_str,"MS",%progbits,1
fa66961
+.LASF2:
fa66961
+	.string	"GNU C17 11.2.1 -g"
fa66961
+	.section	.debug_line_str,"MS",%progbits,1
fa66961
+.LASF1:
fa66961
+	.string	"/example"
fa66961
+.LASF0:
fa66961
+	.string	"test.c"
fa66961
+	.ident	"GCC: (GNU) 11.2.1"
fa66961
+	.section	.note.GNU-stack,"",%progbits
fa66961
diff --git a/gas/testsuite/gas/elf/elf.exp b/gas/testsuite/gas/elf/elf.exp
fa66961
index 18bc1db8c70..2485008d569 100644
fa66961
--- a/gas/testsuite/gas/elf/elf.exp
fa66961
+++ b/gas/testsuite/gas/elf/elf.exp
fa66961
@@ -297,6 +297,7 @@ if { [is_elf_format] } then {
fa66961
     run_dump_test "dwarf2-19" $dump_opts
fa66961
     run_dump_test "dwarf2-20" $dump_opts
fa66961
     run_dump_test "dwarf-5-file0" $dump_opts
fa66961
+    run_dump_test "dwarf-5-file0-2" $dump_opts
fa66961
     run_dump_test "dwarf-5-dir0" $dump_opts
fa66961
     run_dump_test "dwarf-4-cu" $dump_opts
fa66961
     run_dump_test "dwarf-5-cu" $dump_opts
fa66961
commit 9f81b99e2426d19760c20c07f8cd3ae5cd85e8df
fa66961
Author: Alan Modra <amodra@gmail.com>
fa66961
Date:   Fri Sep 10 18:01:43 2021 +0930
fa66961
fa66961
    Re: gas: Use the directory name in .file 0
fa66961
    
fa66961
            PR gas/28266
fa66961
            * testsuite/gas/elf/dwarf-5-file0-2.s: Use %object rather than
fa66961
            @object, .4byte instead of .long, and .asciz instead of .string.
fa66961
fa66961
diff --git a/gas/testsuite/gas/elf/dwarf-5-file0-2.s b/gas/testsuite/gas/elf/dwarf-5-file0-2.s
fa66961
index 135a03bf493..bab4a16b56b 100644
fa66961
--- a/gas/testsuite/gas/elf/dwarf-5-file0-2.s
fa66961
+++ b/gas/testsuite/gas/elf/dwarf-5-file0-2.s
fa66961
@@ -5,7 +5,7 @@
fa66961
 	.globl	x
fa66961
 	.section	.bss
fa66961
 	.balign 4
fa66961
-	.type	x, @object
fa66961
+	.type	x, %object
fa66961
 	.size	x, 4
fa66961
 x:
fa66961
 	.zero	4
fa66961
@@ -14,30 +14,30 @@ x:
fa66961
 	.file 1 "test.c"
fa66961
 	.section	.debug_info,"",%progbits
fa66961
 .Ldebug_info0:
fa66961
-	.long	0x32
fa66961
+	.4byte	0x32
fa66961
 	.2byte	0x5
fa66961
 	.byte	0x1
fa66961
 	.byte	0x4
fa66961
-	.long	.Ldebug_abbrev0
fa66961
+	.4byte	.Ldebug_abbrev0
fa66961
 	.uleb128 0x1
fa66961
-	.long	.LASF2
fa66961
+	.4byte	.LASF2
fa66961
 	.byte	0x1d
fa66961
-	.long	.LASF0
fa66961
-	.long	.LASF1
fa66961
-	.long	.Ldebug_line0
fa66961
+	.4byte	.LASF0
fa66961
+	.4byte	.LASF1
fa66961
+	.4byte	.Ldebug_line0
fa66961
 	.uleb128 0x2
fa66961
-	.string	"x"
fa66961
+	.asciz	"x"
fa66961
 	.byte	0x1
fa66961
 	.byte	0x1
fa66961
 	.byte	0x5
fa66961
-	.long	0x2e
fa66961
+	.4byte	0x2e
fa66961
 	.uleb128 0x5
fa66961
 	.byte	0x3
fa66961
-	.long	x
fa66961
+	.4byte	x
fa66961
 	.uleb128 0x3
fa66961
 	.byte	0x4
fa66961
 	.byte	0x5
fa66961
-	.string	"int"
fa66961
+	.asciz	"int"
fa66961
 	.byte	0
fa66961
 	.section	.debug_abbrev,"",%progbits
fa66961
 .Ldebug_abbrev0:
fa66961
@@ -88,24 +88,24 @@ x:
fa66961
 	.byte	0
fa66961
 	.byte	0
fa66961
 	.section	.debug_aranges,"",%progbits
fa66961
-	.long	0x14
fa66961
+	.4byte	0x14
fa66961
 	.2byte	0x2
fa66961
-	.long	.Ldebug_info0
fa66961
+	.4byte	.Ldebug_info0
fa66961
 	.byte	0x4
fa66961
 	.byte	0
fa66961
 	.2byte	0
fa66961
 	.2byte	0
fa66961
-	.long	0
fa66961
-	.long	0
fa66961
+	.4byte	0
fa66961
+	.4byte	0
fa66961
 	.section	.debug_line,"",%progbits
fa66961
 .Ldebug_line0:
fa66961
 	.section	.debug_str,"MS",%progbits,1
fa66961
 .LASF2:
fa66961
-	.string	"GNU C17 11.2.1 -g"
fa66961
+	.asciz	"GNU C17 11.2.1 -g"
fa66961
 	.section	.debug_line_str,"MS",%progbits,1
fa66961
 .LASF1:
fa66961
-	.string	"/example"
fa66961
+	.asciz	"/example"
fa66961
 .LASF0:
fa66961
-	.string	"test.c"
fa66961
+	.asciz	"test.c"
fa66961
 	.ident	"GCC: (GNU) 11.2.1"
fa66961
 	.section	.note.GNU-stack,"",%progbits