2e9b04
diff --git a/gas/testsuite/gas/elf/dwarf-5-file0-2.d b/gas/testsuite/gas/elf/dwarf-5-file0-2.d
2e9b04
new file mode 100644
2e9b04
index 00000000000..4b3ed29f4c9
2e9b04
--- /dev/null
2e9b04
+++ b/gas/testsuite/gas/elf/dwarf-5-file0-2.d
2e9b04
@@ -0,0 +1,15 @@
2e9b04
+#as: --gdwarf-5
2e9b04
+#name: DWARF5 .file 0 dir file
2e9b04
+#readelf: -wl
2e9b04
+
2e9b04
+#...
2e9b04
+ The Directory Table \(offset 0x.*, lines 1, columns 1\):
2e9b04
+  Entry	Name
2e9b04
+#...
2e9b04
+  0	\(indirect line string, offset: 0x.*\): /example
2e9b04
+
2e9b04
+ The File Name Table \(offset 0x.*, lines 2, columns 2\):
2e9b04
+  Entry	Dir	Name
2e9b04
+  0	0	\(indirect line string, offset: 0x.*\): test.c
2e9b04
+  1	0	\(indirect line string, offset: 0x.*\): test.c
2e9b04
+#pass
2e9b04
diff --git a/gas/testsuite/gas/elf/dwarf-5-file0-2.s b/gas/testsuite/gas/elf/dwarf-5-file0-2.s
2e9b04
new file mode 100644
2e9b04
index 00000000000..135a03bf493
2e9b04
--- /dev/null
2e9b04
+++ b/gas/testsuite/gas/elf/dwarf-5-file0-2.s
2e9b04
@@ -0,0 +1,111 @@
2e9b04
+	.file	"test.c"
2e9b04
+	.text
2e9b04
+.Ltext0:
2e9b04
+	.file 0 "/example" "test.c"
2e9b04
+	.globl	x
2e9b04
+	.section	.bss
2e9b04
+	.balign 4
2e9b04
+	.type	x, @object
2e9b04
+	.size	x, 4
2e9b04
+x:
2e9b04
+	.zero	4
2e9b04
+	.text
2e9b04
+.Letext0:
2e9b04
+	.file 1 "test.c"
2e9b04
+	.section	.debug_info,"",%progbits
2e9b04
+.Ldebug_info0:
2e9b04
+	.long	0x32
2e9b04
+	.2byte	0x5
2e9b04
+	.byte	0x1
2e9b04
+	.byte	0x4
2e9b04
+	.long	.Ldebug_abbrev0
2e9b04
+	.uleb128 0x1
2e9b04
+	.long	.LASF2
2e9b04
+	.byte	0x1d
2e9b04
+	.long	.LASF0
2e9b04
+	.long	.LASF1
2e9b04
+	.long	.Ldebug_line0
2e9b04
+	.uleb128 0x2
2e9b04
+	.string	"x"
2e9b04
+	.byte	0x1
2e9b04
+	.byte	0x1
2e9b04
+	.byte	0x5
2e9b04
+	.long	0x2e
2e9b04
+	.uleb128 0x5
2e9b04
+	.byte	0x3
2e9b04
+	.long	x
2e9b04
+	.uleb128 0x3
2e9b04
+	.byte	0x4
2e9b04
+	.byte	0x5
2e9b04
+	.string	"int"
2e9b04
+	.byte	0
2e9b04
+	.section	.debug_abbrev,"",%progbits
2e9b04
+.Ldebug_abbrev0:
2e9b04
+	.uleb128 0x1
2e9b04
+	.uleb128 0x11
2e9b04
+	.byte	0x1
2e9b04
+	.uleb128 0x25
2e9b04
+	.uleb128 0xe
2e9b04
+	.uleb128 0x13
2e9b04
+	.uleb128 0xb
2e9b04
+	.uleb128 0x3
2e9b04
+	.uleb128 0x1f
2e9b04
+	.uleb128 0x1b
2e9b04
+	.uleb128 0x1f
2e9b04
+	.uleb128 0x10
2e9b04
+	.uleb128 0x17
2e9b04
+	.byte	0
2e9b04
+	.byte	0
2e9b04
+	.uleb128 0x2
2e9b04
+	.uleb128 0x34
2e9b04
+	.byte	0
2e9b04
+	.uleb128 0x3
2e9b04
+	.uleb128 0x8
2e9b04
+	.uleb128 0x3a
2e9b04
+	.uleb128 0xb
2e9b04
+	.uleb128 0x3b
2e9b04
+	.uleb128 0xb
2e9b04
+	.uleb128 0x39
2e9b04
+	.uleb128 0xb
2e9b04
+	.uleb128 0x49
2e9b04
+	.uleb128 0x13
2e9b04
+	.uleb128 0x3f
2e9b04
+	.uleb128 0x19
2e9b04
+	.uleb128 0x2
2e9b04
+	.uleb128 0x18
2e9b04
+	.byte	0
2e9b04
+	.byte	0
2e9b04
+	.uleb128 0x3
2e9b04
+	.uleb128 0x24
2e9b04
+	.byte	0
2e9b04
+	.uleb128 0xb
2e9b04
+	.uleb128 0xb
2e9b04
+	.uleb128 0x3e
2e9b04
+	.uleb128 0xb
2e9b04
+	.uleb128 0x3
2e9b04
+	.uleb128 0x8
2e9b04
+	.byte	0
2e9b04
+	.byte	0
2e9b04
+	.byte	0
2e9b04
+	.section	.debug_aranges,"",%progbits
2e9b04
+	.long	0x14
2e9b04
+	.2byte	0x2
2e9b04
+	.long	.Ldebug_info0
2e9b04
+	.byte	0x4
2e9b04
+	.byte	0
2e9b04
+	.2byte	0
2e9b04
+	.2byte	0
2e9b04
+	.long	0
2e9b04
+	.long	0
2e9b04
+	.section	.debug_line,"",%progbits
2e9b04
+.Ldebug_line0:
2e9b04
+	.section	.debug_str,"MS",%progbits,1
2e9b04
+.LASF2:
2e9b04
+	.string	"GNU C17 11.2.1 -g"
2e9b04
+	.section	.debug_line_str,"MS",%progbits,1
2e9b04
+.LASF1:
2e9b04
+	.string	"/example"
2e9b04
+.LASF0:
2e9b04
+	.string	"test.c"
2e9b04
+	.ident	"GCC: (GNU) 11.2.1"
2e9b04
+	.section	.note.GNU-stack,"",%progbits
2e9b04
diff --git a/gas/testsuite/gas/elf/dwarf-5-file0-2.s b/gas/testsuite/gas/elf/dwarf-5-file0-2.s
2e9b04
index 135a03bf493..bab4a16b56b 100644
2e9b04
--- a/gas/testsuite/gas/elf/dwarf-5-file0-2.s
2e9b04
+++ b/gas/testsuite/gas/elf/dwarf-5-file0-2.s
2e9b04
@@ -5,7 +5,7 @@
2e9b04
 	.globl	x
2e9b04
 	.section	.bss
2e9b04
 	.balign 4
2e9b04
-	.type	x, @object
2e9b04
+	.type	x, %object
2e9b04
 	.size	x, 4
2e9b04
 x:
2e9b04
 	.zero	4
2e9b04
@@ -14,30 +14,30 @@ x:
2e9b04
 	.file 1 "test.c"
2e9b04
 	.section	.debug_info,"",%progbits
2e9b04
 .Ldebug_info0:
2e9b04
-	.long	0x32
2e9b04
+	.4byte	0x32
2e9b04
 	.2byte	0x5
2e9b04
 	.byte	0x1
2e9b04
 	.byte	0x4
2e9b04
-	.long	.Ldebug_abbrev0
2e9b04
+	.4byte	.Ldebug_abbrev0
2e9b04
 	.uleb128 0x1
2e9b04
-	.long	.LASF2
2e9b04
+	.4byte	.LASF2
2e9b04
 	.byte	0x1d
2e9b04
-	.long	.LASF0
2e9b04
-	.long	.LASF1
2e9b04
-	.long	.Ldebug_line0
2e9b04
+	.4byte	.LASF0
2e9b04
+	.4byte	.LASF1
2e9b04
+	.4byte	.Ldebug_line0
2e9b04
 	.uleb128 0x2
2e9b04
-	.string	"x"
2e9b04
+	.asciz	"x"
2e9b04
 	.byte	0x1
2e9b04
 	.byte	0x1
2e9b04
 	.byte	0x5
2e9b04
-	.long	0x2e
2e9b04
+	.4byte	0x2e
2e9b04
 	.uleb128 0x5
2e9b04
 	.byte	0x3
2e9b04
-	.long	x
2e9b04
+	.4byte	x
2e9b04
 	.uleb128 0x3
2e9b04
 	.byte	0x4
2e9b04
 	.byte	0x5
2e9b04
-	.string	"int"
2e9b04
+	.asciz	"int"
2e9b04
 	.byte	0
2e9b04
 	.section	.debug_abbrev,"",%progbits
2e9b04
 .Ldebug_abbrev0:
2e9b04
@@ -88,24 +88,24 @@ x:
2e9b04
 	.byte	0
2e9b04
 	.byte	0
2e9b04
 	.section	.debug_aranges,"",%progbits
2e9b04
-	.long	0x14
2e9b04
+	.4byte	0x14
2e9b04
 	.2byte	0x2
2e9b04
-	.long	.Ldebug_info0
2e9b04
+	.4byte	.Ldebug_info0
2e9b04
 	.byte	0x4
2e9b04
 	.byte	0
2e9b04
 	.2byte	0
2e9b04
 	.2byte	0
2e9b04
-	.long	0
2e9b04
-	.long	0
2e9b04
+	.4byte	0
2e9b04
+	.4byte	0
2e9b04
 	.section	.debug_line,"",%progbits
2e9b04
 .Ldebug_line0:
2e9b04
 	.section	.debug_str,"MS",%progbits,1
2e9b04
 .LASF2:
2e9b04
-	.string	"GNU C17 11.2.1 -g"
2e9b04
+	.asciz	"GNU C17 11.2.1 -g"
2e9b04
 	.section	.debug_line_str,"MS",%progbits,1
2e9b04
 .LASF1:
2e9b04
-	.string	"/example"
2e9b04
+	.asciz	"/example"
2e9b04
 .LASF0:
2e9b04
-	.string	"test.c"
2e9b04
+	.asciz	"test.c"
2e9b04
 	.ident	"GCC: (GNU) 11.2.1"
2e9b04
 	.section	.note.GNU-stack,"",%progbits
2e9b04
diff -rup binutils.orig/gas/dwarf2dbg.c binutils-2.35.2/gas/dwarf2dbg.c
2e9b04
--- binutils.orig/gas/dwarf2dbg.c	2021-11-08 14:59:19.759524743 +0000
2e9b04
+++ binutils-2.35.2/gas/dwarf2dbg.c	2021-11-08 15:00:55.179909708 +0000
2e9b04
@@ -588,6 +588,7 @@ get_basename (const char * pathname)
2e9b04
 
2e9b04
 static unsigned int
2e9b04
 get_directory_table_entry (const char *  dirname,
2e9b04
+			   const char *  file0_dirname,
2e9b04
 			   size_t        dirlen,
2e9b04
 			   bfd_boolean   can_use_zero)
2e9b04
 {
2e9b04
@@ -617,7 +618,7 @@ get_directory_table_entry (const char *
2e9b04
     {
2e9b04
       if (dirs == NULL || dirs[0] == NULL)
2e9b04
 	{
2e9b04
-	  const char * pwd = getpwd ();
2e9b04
+	  const char * pwd = file0_dirname ? file0_dirname : getpwd ();
2e9b04
 
2e9b04
 	  if (dwarf_level >= 5 && strcmp (dirname, pwd) != 0)
2e9b04
 	    {
2e9b04
@@ -626,7 +627,7 @@ get_directory_table_entry (const char *
2e9b04
 		 directory).  Since we are about to create a directory entry that
2e9b04
 		 is not the same, allocate the current directory first.
2e9b04
 		 FIXME: Alternatively we could generate an error message here.  */
2e9b04
-	      (void) get_directory_table_entry (pwd, strlen (pwd), TRUE);
2e9b04
+	      (void) get_directory_table_entry (pwd, NULL, strlen (pwd), TRUE);
2e9b04
 	      d = 1;
2e9b04
 	    }
2e9b04
 	  else
2e9b04
@@ -722,7 +723,7 @@ allocate_filenum (const char * pathname)
2e9b04
   file = get_basename (pathname);
2e9b04
   dir_len = file - pathname;
2e9b04
 
2e9b04
-  dir = get_directory_table_entry (pathname, dir_len, FALSE);
2e9b04
+  dir = get_directory_table_entry (pathname, NULL, dir_len, FALSE);
2e9b04
 
2e9b04
   /* Do not use slot-0.  That is specifically reserved for use by
2e9b04
      the '.file 0 "name"' directive.  */
2e9b04
@@ -762,6 +763,7 @@ allocate_filename_to_slot (const char *
2e9b04
   const char *file;
2e9b04
   size_t dirlen;
2e9b04
   unsigned int i, d;
2e9b04
+  const char *file0_dirname = dirname;
2e9b04
 
2e9b04
   /* Short circuit the common case of adding the same pathname
2e9b04
      as last time.  */
2e9b04
@@ -836,7 +838,8 @@ allocate_filename_to_slot (const char *
2e9b04
       file = filename;
2e9b04
     }
2e9b04
 
2e9b04
-  d = get_directory_table_entry (dirname, dirlen, num == 0);
2e9b04
+  d = get_directory_table_entry (dirname, file0_dirname, dirlen,
2e9b04
+				 num == 0);
2e9b04
   i = num;
2e9b04
 
2e9b04
   if (! assign_file_to_slot (i, file, d))
2e9b04
diff -rup binutils.orig/gas/testsuite/gas/elf/elf.exp binutils-2.35.2/gas/testsuite/gas/elf/elf.exp
2e9b04
--- binutils.orig/gas/testsuite/gas/elf/elf.exp	2021-11-08 14:59:19.856524118 +0000
2e9b04
+++ binutils-2.35.2/gas/testsuite/gas/elf/elf.exp	2021-11-08 14:59:36.225418609 +0000
2e9b04
@@ -274,6 +274,7 @@ if { [is_elf_format] } then {
2e9b04
     run_dump_test "dwarf2-18" $dump_opts
2e9b04
     run_dump_test "dwarf2-19" $dump_opts
2e9b04
     run_dump_test "dwarf-5-file0" $dump_opts
2e9b04
+    run_dump_test "dwarf-5-file0-2" $dump_opts
2e9b04
     run_dump_test "dwarf-5-dir0" $dump_opts
2e9b04
     run_dump_test "dwarf-4-cu" $dump_opts
2e9b04
     run_dump_test "dwarf-5-cu" $dump_opts
2e9b04