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