https://gcc.gnu.org/g:3ae0af136d30a72ca8f52a9fe273ca2f12a8f01a

commit r17-6613-g3ae0af136d30a72ca8f52a9fe273ca2f12a8f01a
Author: Oleg Tolmatcev <[email protected]>
Date:   Mon Nov 23 11:52:34 2026 -0700

    [PATCH] codeview: Use switch_to_section for .debug$S and .debug$T
    
    GCC records the assembler's current section in in_section, and
    switch_to_section emits no directive when the requested section is
    already current.  write_custom_types and codeview_debug_finish call
    targetm.asm_out.named_section directly, which changes the assembler's
    section without updating in_section, so later output can go into
    .debug$T.  With -gcodeview -g this fails with:
    
      Error: can't resolve .debug$T - .Ltext0
    
    Use switch_to_section so that in_section stays correct.
    
    gcc/ChangeLog:
    
            * dwarf2codeview.cc (write_custom_types): Use switch_to_section.
            (codeview_debug_finish): Likewise.
    
    gcc/testsuite/ChangeLog:
    
            * g++.dg/debug/codeview/eh-indirect-1.C: New test.
            * gcc.dg/debug/codeview/codeview-2.c: Likewise.
    
    Signed-off-by: Oleg Tolmatcev <[email protected]>

Diff:
---
 gcc/dwarf2codeview.cc                              |  4 ++--
 .../g++.dg/debug/codeview/eh-indirect-1.C          | 22 ++++++++++++++++++++++
 gcc/testsuite/gcc.dg/debug/codeview/codeview-2.c   |  9 +++++++++
 3 files changed, 33 insertions(+), 2 deletions(-)

diff --git a/gcc/dwarf2codeview.cc b/gcc/dwarf2codeview.cc
index 2252b32d917d..9507dc27e03a 100644
--- a/gcc/dwarf2codeview.cc
+++ b/gcc/dwarf2codeview.cc
@@ -5107,7 +5107,7 @@ write_lf_methodlist (codeview_custom_type *t)
 static void
 write_custom_types (void)
 {
-  targetm.asm_out.named_section (".debug$T", SECTION_DEBUG, NULL);
+  switch_to_section (get_section (".debug$T", SECTION_DEBUG, NULL));
 
   fputs (integer_asm_op (4, false), asm_out_file);
   fprint_whex (asm_out_file, CV_SIGNATURE_C13);
@@ -5194,7 +5194,7 @@ write_custom_types (void)
 void
 codeview_debug_finish (void)
 {
-  targetm.asm_out.named_section (".debug$S", SECTION_DEBUG, NULL);
+  switch_to_section (get_section (".debug$S", SECTION_DEBUG, NULL));
 
   fputs (integer_asm_op (4, false), asm_out_file);
   fprint_whex (asm_out_file, CV_SIGNATURE_C13);
diff --git a/gcc/testsuite/g++.dg/debug/codeview/eh-indirect-1.C 
b/gcc/testsuite/g++.dg/debug/codeview/eh-indirect-1.C
new file mode 100644
index 000000000000..309bb0c55ec1
--- /dev/null
+++ b/gcc/testsuite/g++.dg/debug/codeview/eh-indirect-1.C
@@ -0,0 +1,22 @@
+// { dg-do compile }
+// { dg-options "-O0 -gcodeview" }
+
+// codeview_debug_finish and write_custom_types must update in_section so
+// the indirect typeinfo constant is emitted in .data after the debug info.
+
+void f ();
+extern int t;
+
+int
+g ()
+{
+  try { f (); }
+  catch (int &) { return t; }
+  return 0;
+}
+
+int t = 1;
+
+// The initialized global must leave in_section pointing to .data.
+// { dg-final { scan-assembler {t:\n[ \t]+\.long[ \t]+1\n[ \t]+\.section[ 
\t]+\.debug\$S} } }
+// { dg-final { scan-assembler {\.data\n[ \t]+\.align[ \t]+[0-9]+\n\.LDFCM0:} 
} }
diff --git a/gcc/testsuite/gcc.dg/debug/codeview/codeview-2.c 
b/gcc/testsuite/gcc.dg/debug/codeview/codeview-2.c
new file mode 100644
index 000000000000..162e45b75e05
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/debug/codeview/codeview-2.c
@@ -0,0 +1,9 @@
+/* { dg-do assemble } */
+/* { dg-options "-gcodeview -g" } */
+
+/* Combined CodeView and DWARF output must assemble successfully.  */
+
+void
+f (void)
+{
+}

Reply via email to