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