[binutils-gdb] gas: Name the CU after the primary source file

Jan Beulich jbeulich@sourceware.org
Fri Nov 6 07:19:05 GMT 2026


https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=347a0b1ae80652ea9e9a9ec5dbd7ae6da9b674c8

commit 347a0b1ae80652ea9e9a9ec5dbd7ae6da9b674c8
Author: Longjun Luo <luolongjuna@gmail.com>
Date:   Fri Nov 6 08:18:16 2026 +0100

    gas: Name the CU after the primary source file
    
    For assembler-generated DWARF (-g/--gdwarf-N), GAS takes the CU name
    from the first file with a line entry. With GAS 2.46.1, assembling
    Linux's arch/x86/lib/retpoline.S therefore produces a CU named
    ./arch/x86/include/asm/GEN-for-each-reg.h.
    
    Use the filename from the first top-level CPP line marker, falling
    back to the first input file, and register it before allocating file
    numbers from line entries.
    
    dwarf5-line-3.d now uses foo.S for the CU, matching its first CPP
    marker; its line entries still refer to dwarf5-line-3.S.
    
    Signed-off-by: Longjun Luo <luolongjuna@gmail.com>

Diff:
---
 gas/as.h                                     |  1 +
 gas/dwarf2dbg.c                              | 11 ++++++----
 gas/input-scrub.c                            | 32 ++++++++++++++++++++++++----
 gas/testsuite/gas/elf/dwarf-4-primary-file.d | 11 ++++++++++
 gas/testsuite/gas/elf/dwarf-5-primary-file.d | 12 +++++++++++
 gas/testsuite/gas/elf/dwarf-primary-file.s   |  6 ++++++
 gas/testsuite/gas/elf/elf.exp                |  2 ++
 gas/testsuite/gas/i386/dwarf5-line-3.d       |  8 ++++---
 8 files changed, 72 insertions(+), 11 deletions(-)

diff --git a/gas/as.h b/gas/as.h
index 2fe0a48a225..af34d379bd4 100644
--- a/gas/as.h
+++ b/gas/as.h
@@ -553,6 +553,7 @@ void   as_report_context (void);
 const char * as_where (unsigned int *);
 const char * as_where_top (unsigned int *);
 const char * as_where_physical (unsigned int *);
+const char * as_where_primary (void);
 void   predefine_symbol (const char *, valueT);
 void   bump_line_counters (void);
 void   do_scrub_begin (int);
diff --git a/gas/dwarf2dbg.c b/gas/dwarf2dbg.c
index c899eecd26b..2bc7f22ae6a 100644
--- a/gas/dwarf2dbg.c
+++ b/gas/dwarf2dbg.c
@@ -3044,10 +3044,9 @@ out_debug_str (segT str_seg, symbolS **name_sym, symbolS **comp_dir_sym,
 
   subseg_set (str_seg, 0);
 
-  /* DW_AT_name.  We don't have the actual file name that was present
-     on the command line, so assume files[first_file] is the main input file.
-     We're not supposed to get called unless at least one line number
-     entry was emitted, so this should always be defined.  */
+  /* DW_AT_name.  The first file-table entry names the CU.  For gas-generated
+     line info, dwarf2_finish registers the primary source before other files.
+     There is at least one line entry when we get here.  */
   *name_sym = symbol_temp_new_now_octets ();
 
   if (files[first_file].dir)
@@ -3192,6 +3191,10 @@ dwarf2_finish (void)
 			     SEC_READONLY | SEC_DEBUGGING | SEC_OCTETS);
     }
 
+  /* Register the primary source before files from line entries.  */
+  if (debug_type == DEBUG_DWARF2 && all_segs != NULL)
+    allocate_filenum (as_where_primary ());
+
   for (s = all_segs; s; s = s->next)
     {
       struct line_subseg *lss;
diff --git a/gas/input-scrub.c b/gas/input-scrub.c
index 95252b26c6e..ae735cbcc4c 100644
--- a/gas/input-scrub.c
+++ b/gas/input-scrub.c
@@ -99,6 +99,9 @@ int macro_nest;
 static const char *physical_input_file;
 static const char *logical_input_file;
 
+static const char *primary_input_file;
+static const char *primary_logical_file;
+
 /* 1-origin line number in a source file.  */
 /* A line ends in '\n' or eof.  */
 static unsigned int physical_input_line;
@@ -247,6 +250,8 @@ input_scrub_begin (void)
 	|| (AFTER_STRING[0] == '\0' && AFTER_SIZE == 1));
 
   physical_input_file = NULL;	/* No file read yet.  */
+  primary_input_file = NULL;
+  primary_logical_file = NULL;
   next_saved_file = NULL;	/* At EOF, don't pop to any other file */
   macro_nest = 0;
   input_scrub_reinit ();
@@ -269,6 +274,8 @@ input_scrub_new_file (const char *filename)
 {
   input_file_open (filename, !flag_no_comments);
   physical_input_file = filename[0] ? filename : _("{standard input}");
+  if (primary_input_file == NULL)
+    primary_input_file = physical_input_file;
   physical_input_line = 0;
 
   partial_size = 0;
@@ -521,10 +528,19 @@ new_logical_line_flags (const char *fname, /* DON'T destroy it!  We point to it!
       fname = NULL;
     }
 
-  if (fname
-      && (logical_input_file == NULL
-	  || filename_cmp (logical_input_file, fname)))
-    logical_input_file = fname;
+  if (fname)
+    {
+      if (logical_input_file == NULL
+	  || filename_cmp (logical_input_file, fname))
+	logical_input_file = fname;
+
+      /* The first top-level CPP line marker names the primary source.  */
+      if (primary_logical_file == NULL && *fname
+	  && next_saved_file == NULL
+	  && physical_input_file == primary_input_file
+	  && flags == 0)
+	primary_logical_file = fname;
+    }
 }
 
 void
@@ -560,6 +576,14 @@ as_report_context (void)
     }
 }
 

+/* Return the primary source file name, if known.  */
+
+const char *
+as_where_primary (void)
+{
+  return primary_logical_file ? primary_logical_file : primary_input_file;
+}
+
 /* Return the current physical input file name and line number, if known  */
 
 const char *
diff --git a/gas/testsuite/gas/elf/dwarf-4-primary-file.d b/gas/testsuite/gas/elf/dwarf-4-primary-file.d
new file mode 100644
index 00000000000..55f82769574
--- /dev/null
+++ b/gas/testsuite/gas/elf/dwarf-4-primary-file.d
@@ -0,0 +1,11 @@
+#source: dwarf-primary-file.s
+#as: --gdwarf-4
+#readelf: -W -wl -wi
+#name: DWARF4 primary source file
+
+#...
+[ \t]+1[ \t]+.*primary\.S
+[ \t]+2[ \t]+.*included\.h
+#...
+.*DW_AT_name.*: primary\.S
+#pass
diff --git a/gas/testsuite/gas/elf/dwarf-5-primary-file.d b/gas/testsuite/gas/elf/dwarf-5-primary-file.d
new file mode 100644
index 00000000000..4856c177684
--- /dev/null
+++ b/gas/testsuite/gas/elf/dwarf-5-primary-file.d
@@ -0,0 +1,12 @@
+#source: dwarf-primary-file.s
+#as: --gdwarf-5
+#readelf: -W -wl -wi
+#name: DWARF5 primary source file
+
+#...
+[ \t]+0[ \t]+.*primary\.S
+[ \t]+1[ \t]+.*primary\.S
+[ \t]+2[ \t]+.*included\.h
+#...
+.*DW_AT_name.*: primary\.S
+#pass
diff --git a/gas/testsuite/gas/elf/dwarf-primary-file.s b/gas/testsuite/gas/elf/dwarf-primary-file.s
new file mode 100644
index 00000000000..a8c3fc1c62c
--- /dev/null
+++ b/gas/testsuite/gas/elf/dwarf-primary-file.s
@@ -0,0 +1,6 @@
+# 0 "primary.S"
+	.text
+# 1 "included.h" 1
+	.nop
+# 4 "primary.S" 2
+	.nop
diff --git a/gas/testsuite/gas/elf/elf.exp b/gas/testsuite/gas/elf/elf.exp
index 5580c494c54..30e31dc4fcc 100644
--- a/gas/testsuite/gas/elf/elf.exp
+++ b/gas/testsuite/gas/elf/elf.exp
@@ -352,6 +352,8 @@ if { [is_elf_format] } then {
     run_dump_test "dwarf-5-irp" $dump_opts
     run_dump_test "dwarf-5-macro" $dump_opts
     run_dump_test "dwarf-5-macro-include" $dump_opts
+    run_dump_test "dwarf-4-primary-file" $dump_opts
+    run_dump_test "dwarf-5-primary-file" $dump_opts
     run_dump_test "dwarf-3-func" $dump_opts
     run_dump_test "dwarf-5-func" $dump_opts
     run_dump_test "dwarf-5-func-global" $dump_opts
diff --git a/gas/testsuite/gas/i386/dwarf5-line-3.d b/gas/testsuite/gas/i386/dwarf5-line-3.d
index 2a07a17c2db..9c715dbac1d 100644
--- a/gas/testsuite/gas/i386/dwarf5-line-3.d
+++ b/gas/testsuite/gas/i386/dwarf5-line-3.d
@@ -35,12 +35,14 @@ Raw dump of debug contents of section \.z?debug_line:
   Entry	Name
   0	\(indirect line string, offset: 0.*\): .*
 
- The File Name Table \(offset 0x.*, lines 2, columns 2\):
+ The File Name Table \(offset 0x.*, lines 3, columns 2\):
   Entry	Dir	Name
-  0	0	\(indirect line string, offset: 0x.*\): dwarf5-line-3.S
-  1	0	\(indirect line string, offset: 0x.*\): dwarf5-line-3.S
+  0	0	\(indirect line string, offset: 0x.*\): foo.S
+  1	0	\(indirect line string, offset: 0x.*\): foo.S
+  2	0	\(indirect line string, offset: 0x.*\): dwarf5-line-3.S
 
  Line Number Statements:
+  \[0x.*\]  Set File Name to entry 2 in the File Name Table
   \[0x.*\]  Extended opcode 2: set Address to (0x)?0
   \[0x.*\]  Special opcode 7: advance Address by 0 to (0x)?0 and Line by 2 to 3
   \[0x.*\]  Advance PC by 1 to 0x1


More information about the Binutils-cvs mailing list