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