ffc81ff
diff -rup binutils.orig/gas/dwarf2dbg.c binutils-2.37/gas/dwarf2dbg.c
ffc81ff
--- binutils.orig/gas/dwarf2dbg.c	2021-08-09 17:28:17.743318315 +0100
ffc81ff
+++ binutils-2.37/gas/dwarf2dbg.c	2021-08-09 17:28:27.043264112 +0100
ffc81ff
@@ -620,7 +620,22 @@ get_directory_table_entry (const char *d
ffc81ff
   if (can_use_zero)
ffc81ff
     {
ffc81ff
       if (dirs == NULL || dirs[0] == NULL)
ffc81ff
-	d = 0;
ffc81ff
+	{
ffc81ff
+	  const char * pwd = getpwd ();
ffc81ff
+
ffc81ff
+	  if (dwarf_level >= 5 && strcmp (dirname, pwd) != 0)
ffc81ff
+	    {
ffc81ff
+	      /* In DWARF-5 the 0 entry in the directory table is expected to be
ffc81ff
+		 the same as the DW_AT_comp_dir (which is set to the current build
ffc81ff
+		 directory).  Since we are about to create a directory entry that
ffc81ff
+		 is not the same, allocate the current directory first.
ffc81ff
+		 FIXME: Alternatively we could generate an error message here.  */
ffc81ff
+	      (void) get_directory_table_entry (pwd, strlen (pwd), true);
ffc81ff
+	      d = 1;
ffc81ff
+	    }
ffc81ff
+	  else
ffc81ff
+	    d = 0;
ffc81ff
+	}
ffc81ff
     }
ffc81ff
   else if (d == 0)
ffc81ff
     d = 1;
ffc81ff
@@ -628,8 +643,8 @@ get_directory_table_entry (const char *d
ffc81ff
   if (d >= dirs_allocated)
ffc81ff
     {
ffc81ff
       unsigned int old = dirs_allocated;
ffc81ff
-
ffc81ff
-      dirs_allocated = d + 32;
ffc81ff
+#define DIR_TABLE_INCREMENT 32
ffc81ff
+      dirs_allocated = d + DIR_TABLE_INCREMENT;
ffc81ff
       dirs = XRESIZEVEC (char *, dirs, dirs_allocated);
ffc81ff
       memset (dirs + old, 0, (dirs_allocated - old) * sizeof (char *));
ffc81ff
     }
ffc81ff
@@ -779,7 +794,7 @@ allocate_filename_to_slot (const char *d
ffc81ff
 	    {
ffc81ff
 	      if (dirs == NULL)
ffc81ff
 		{
ffc81ff
-		  dirs_allocated = files[num].dir + 32;
ffc81ff
+		  dirs_allocated = files[num].dir + DIR_TABLE_INCREMENT;
ffc81ff
 		  dirs = XCNEWVEC (char *, dirs_allocated);
ffc81ff
 		}
ffc81ff
 	      
ffc81ff
@@ -807,7 +822,7 @@ allocate_filename_to_slot (const char *d
ffc81ff
 		{
ffc81ff
 		  if (dirs == NULL)
ffc81ff
 		    {
ffc81ff
-		      dirs_allocated = files[num].dir + 32;
ffc81ff
+		      dirs_allocated = files[num].dir + DIR_TABLE_INCREMENT;
ffc81ff
 		      dirs = XCNEWVEC (char *, dirs_allocated);
ffc81ff
 		    }
ffc81ff
 
ffc81ff
@@ -840,7 +855,7 @@ allocate_filename_to_slot (const char *d
ffc81ff
       dirlen = strlen (dirname);
ffc81ff
       file = filename;
ffc81ff
     }
ffc81ff
-  
ffc81ff
+
ffc81ff
   d = get_directory_table_entry (dirname, dirlen, num == 0);
ffc81ff
   i = num;
ffc81ff
 
ffc81ff
@@ -2082,7 +2097,12 @@ out_dir_and_file_list (segT line_seg, in
ffc81ff
 	 Otherwise use pwd as main file directory.  */
ffc81ff
       if (dirs_in_use > 0 && dirs != NULL && dirs[0] != NULL)
ffc81ff
 	dir = remap_debug_filename (dirs[0]);
ffc81ff
-      else if (dirs_in_use > 1 && dirs != NULL && dirs[1] != NULL)
ffc81ff
+      else if (dirs_in_use > 1
ffc81ff
+	       && dirs != NULL
ffc81ff
+	       && dirs[1] != NULL
ffc81ff
+	       /* DWARF-5 directory tables expect dir[0] to be the same as
ffc81ff
+		  DW_AT_comp_dir, which is the same as pwd.  */
ffc81ff
+	       && dwarf_level < 5)
ffc81ff
 	dir = remap_debug_filename (dirs[1]);
ffc81ff
       else
ffc81ff
 	dir = remap_debug_filename (getpwd ());
ffc81ff
@@ -2185,8 +2205,8 @@ out_dir_and_file_list (segT line_seg, in
ffc81ff
 	     uses slot zero, but that is only set explicitly using a
ffc81ff
 	     .file 0 directive.  If that isn't used, but file 1 is,
ffc81ff
 	     then use that as main file name.  */
ffc81ff
-	  if (DWARF2_LINE_VERSION >= 5 && i == 0 && files_in_use >= 1)
ffc81ff
-	      files[0].filename = files[1].filename;
ffc81ff
+	  if (DWARF2_LINE_VERSION >= 5 && i == 0 && files_in_use >= 1 && files[0].filename == NULL)
ffc81ff
+	    files[0].filename = files[1].filename;
ffc81ff
 	  else
ffc81ff
 	    files[i].filename = "";
ffc81ff
 	  if (DWARF2_LINE_VERSION < 5 || i != 0)
ffc81ff
Only in binutils-2.37/gas/testsuite/gas/elf: dwarf-5-dir0.d
ffc81ff
Only in binutils-2.37/gas/testsuite/gas/elf: dwarf-5-dir0.s
ffc81ff
diff -rup binutils.orig/gas/testsuite/gas/elf/dwarf-5-file0.d binutils-2.37/gas/testsuite/gas/elf/dwarf-5-file0.d
ffc81ff
--- binutils.orig/gas/testsuite/gas/elf/dwarf-5-file0.d	2021-08-09 17:28:17.817317884 +0100
ffc81ff
+++ binutils-2.37/gas/testsuite/gas/elf/dwarf-5-file0.d	2021-08-09 17:28:27.043264112 +0100
ffc81ff
@@ -3,17 +3,18 @@
ffc81ff
 #readelf: -wl
ffc81ff
 
ffc81ff
 #...
ffc81ff
- The Directory Table \(offset 0x.*, lines 3, columns 1\):
ffc81ff
+ The Directory Table \(offset 0x.*, lines 4, columns 1\):
ffc81ff
   Entry	Name
ffc81ff
-  0	\(indirect line string, offset: 0x.*\): master directory
ffc81ff
-  1	\(indirect line string, offset: 0x.*\): secondary directory
ffc81ff
-  2	\(indirect line string, offset: 0x.*\): /tmp
ffc81ff
+#...
ffc81ff
+  1	\(indirect line string, offset: 0x.*\): master directory
ffc81ff
+  2	\(indirect line string, offset: 0x.*\): secondary directory
ffc81ff
+  3	\(indirect line string, offset: 0x.*\): /tmp
ffc81ff
 
ffc81ff
  The File Name Table \(offset 0x.*, lines 3, columns 3\):
ffc81ff
   Entry	Dir	MD5				Name
ffc81ff
-  0	0 0x0	\(indirect line string, offset: 0x.*\): master source file
ffc81ff
-  1	1 0x0	\(indirect line string, offset: 0x.*\): secondary source file
ffc81ff
-  2	2 0x95828e8bc4f7404dbf7526fb7bd0f192	\(indirect line string, offset: 0x.*\): foo.c
ffc81ff
+  0	1 0x0	\(indirect line string, offset: 0x.*\): master source file
ffc81ff
+  1	2 0x0	\(indirect line string, offset: 0x.*\): secondary source file
ffc81ff
+  2	3 0x95828e8bc4f7404dbf7526fb7bd0f192	\(indirect line string, offset: 0x.*\): foo.c
ffc81ff
 #pass
ffc81ff
 
ffc81ff
 
ffc81ff
diff -rup binutils.orig/gas/testsuite/gas/elf/elf.exp binutils-2.37/gas/testsuite/gas/elf/elf.exp
ffc81ff
--- binutils.orig/gas/testsuite/gas/elf/elf.exp	2021-08-09 17:28:17.817317884 +0100
ffc81ff
+++ binutils-2.37/gas/testsuite/gas/elf/elf.exp	2021-08-09 17:28:27.044264106 +0100
ffc81ff
@@ -297,6 +297,7 @@ if { [is_elf_format] } then {
ffc81ff
     run_dump_test "dwarf2-19" $dump_opts
ffc81ff
     run_dump_test "dwarf2-20" $dump_opts
ffc81ff
     run_dump_test "dwarf-5-file0" $dump_opts
ffc81ff
+    run_dump_test "dwarf-5-dir0" $dump_opts
ffc81ff
     run_dump_test "dwarf-4-cu" $dump_opts
ffc81ff
     run_dump_test "dwarf-5-cu" $dump_opts
ffc81ff
     run_dump_test "dwarf-5-nop-for-line-table" $dump_opts
ffc81ff
diff -rup binutils.orig/gas/testsuite/gas/i386/dwarf5-line-1.d binutils-2.37/gas/testsuite/gas/i386/dwarf5-line-1.d
ffc81ff
--- binutils.orig/gas/testsuite/gas/i386/dwarf5-line-1.d	2021-08-09 17:28:17.782318088 +0100
ffc81ff
+++ binutils-2.37/gas/testsuite/gas/i386/dwarf5-line-1.d	2021-08-09 17:28:27.044264106 +0100
ffc81ff
@@ -33,7 +33,7 @@ Raw dump of debug contents of section \.
ffc81ff
 
ffc81ff
  The Directory Table \(offset 0x.*, lines 2, columns 1\):
ffc81ff
   Entry	Name
ffc81ff
-  0	\(indirect line string, offset: 0x.*\): .*/gas/testsuite/gas/i386
ffc81ff
+  0	\(indirect line string, offset: 0x.*\): .*/gas/testsuite
ffc81ff
   1	\(indirect line string, offset: 0x.*\): .*/gas/testsuite/gas/i386
ffc81ff
 
ffc81ff
  The File Name Table \(offset 0x.*, lines 2, columns 3\):
ffc81ff
diff -rup binutils.orig/gas/testsuite/gas/i386/dwarf5-line-2.d binutils-2.37/gas/testsuite/gas/i386/dwarf5-line-2.d
ffc81ff
--- binutils.orig/gas/testsuite/gas/i386/dwarf5-line-2.d	2021-08-09 17:28:17.785318070 +0100
ffc81ff
+++ binutils-2.37/gas/testsuite/gas/i386/dwarf5-line-2.d	2021-08-09 17:28:27.044264106 +0100
ffc81ff
@@ -33,7 +33,7 @@ Raw dump of debug contents of section \.
ffc81ff
 
ffc81ff
  The Directory Table \(offset 0x.*, lines 2, columns 1\):
ffc81ff
   Entry	Name
ffc81ff
-  0	\(indirect line string, offset: 0x.*\): .*/gas/testsuite/gas/i386
ffc81ff
+  0	\(indirect line string, offset: 0x.*\): .*/gas/testsuite
ffc81ff
   1	\(indirect line string, offset: 0x.*\): .*/gas/testsuite/gas/i386
ffc81ff
 
ffc81ff
  The File Name Table \(offset 0x.*, lines 1, columns 3\):
ffc81ff
--- /dev/null	2021-08-09 07:51:33.817495606 +0100
ffc81ff
+++ binutils-2.37/gas/testsuite/gas/elf/dwarf-5-dir0.s	2021-08-09 17:28:54.787102415 +0100
ffc81ff
@@ -0,0 +1,19 @@
ffc81ff
+	.section	.debug_info,"",%progbits
ffc81ff
+	.4byte	0x8a
ffc81ff
+	.2byte  0x2
ffc81ff
+	.4byte	.Ldebug_abbrev0
ffc81ff
+	.byte	0x4
ffc81ff
+	.uleb128 0x1
ffc81ff
+
ffc81ff
+	.file 0 "../not-the-build-directory/master-source-file.c"
ffc81ff
+	.line 1
ffc81ff
+	.text
ffc81ff
+	.octa 0x12345678901234567890123456789012
ffc81ff
+
ffc81ff
+	.file 1 "secondary directory/secondary source file"
ffc81ff
+	.line 2
ffc81ff
+	.word 2
ffc81ff
+
ffc81ff
+	.file 2 "/tmp" "foo.c" md5 0x95828e8bc4f7404dbf7526fb7bd0f192
ffc81ff
+	.line 5
ffc81ff
+	.word 6
ffc81ff
--- /dev/null	2021-08-09 07:51:33.817495606 +0100
ffc81ff
+++ binutils-2.37/gas/testsuite/gas/elf/dwarf-5-dir0.d	2021-08-09 17:28:54.787102415 +0100
ffc81ff
@@ -0,0 +1,20 @@
ffc81ff
+#as: --gdwarf-5
ffc81ff
+#name: DWARF5 dir[0]
ffc81ff
+#readelf: -wl
ffc81ff
+
ffc81ff
+#...
ffc81ff
+ The Directory Table \(offset 0x.*, lines 4, columns 1\):
ffc81ff
+  Entry	Name
ffc81ff
+  0	\(indirect line string, offset: 0x0\): .*/gas/testsuite
ffc81ff
+  1	\(indirect line string, offset: 0x.*\): ../not-the-build-directory
ffc81ff
+  2	\(indirect line string, offset: 0x.*\): secondary directory
ffc81ff
+  3	\(indirect line string, offset: 0x.*\): /tmp
ffc81ff
+
ffc81ff
+ The File Name Table \(offset 0x.*, lines 3, columns 3\):
ffc81ff
+  Entry	Dir	MD5				Name
ffc81ff
+  0	1 0x0	\(indirect line string, offset: 0x.*\): master-source-file.c
ffc81ff
+  1	2 0x0	\(indirect line string, offset: 0x.*\): secondary source file
ffc81ff
+  2	3 0x95828e8bc4f7404dbf7526fb7bd0f192	\(indirect line string, offset: 0x.*\): foo.c
ffc81ff
+#pass
ffc81ff
+
ffc81ff
+