[binutils-gdb] gas: include .cfi_* generated data in listing

Jan Beulich jbeulich@sourceware.org
Fri Mar 14 09:32:47 GMT 2025


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

commit 5e713f7542b1e9860a84368231823ee5c0fbe08c
Author: Jan Beulich <jbeulich@suse.com>
Date:   Fri Mar 14 10:30:18 2025 +0100

    gas: include .cfi_* generated data in listing
    
    These are data generating directives not overly different from e.g.
    .byte and .long. Whatever (directly) results from should also be
    represented in the listing, if one was requested. It's just that the
    output data is generated much later than the parsing of the directive
    arguments.

Diff:
---
 gas/config/tc-alpha.c           |  3 ++-
 gas/dw2gencfi.c                 | 55 ++++++++++++++++++++++++++++++++++++++---
 gas/dw2gencfi.h                 |  9 ++++++-
 gas/listing.c                   | 45 +++++++++++++++++++++++++++++++++
 gas/listing.h                   |  1 +
 gas/scfidw2gen.c                |  2 +-
 gas/testsuite/gas/cfi/cfi.exp   |  5 ++++
 gas/testsuite/gas/cfi/listing.l | 27 ++++++++++++++++++++
 gas/testsuite/gas/cfi/listing.s | 22 +++++++++++++++++
 9 files changed, 162 insertions(+), 7 deletions(-)

diff --git a/gas/config/tc-alpha.c b/gas/config/tc-alpha.c
index d6245e5ed6c..a90ceb4ed80 100644
--- a/gas/config/tc-alpha.c
+++ b/gas/config/tc-alpha.c
@@ -4059,7 +4059,8 @@ alpha_elf_md_finish (void)
 	   function symbol.  This prevents problems with globals.  */
 	cfi_new_fde (symbol_temp_new (S_GET_SEGMENT (p->func_sym),
 				      symbol_get_frag (p->func_sym),
-				      S_GET_VALUE (p->func_sym)));
+				      S_GET_VALUE (p->func_sym)),
+		     false);
 
 	cfi_set_sections ();
 	cfi_set_return_column (p->ra_regno);
diff --git a/gas/dw2gencfi.c b/gas/dw2gencfi.c
index 4f5622d6d87..310cb56a34e 100644
--- a/gas/dw2gencfi.c
+++ b/gas/dw2gencfi.c
@@ -440,16 +440,25 @@ alloc_cfi_insn_data (void)
   *cur_fde_data->last = insn;
   cur_fde_data->last = &insn->next;
   SET_CUR_SEG (insn, is_now_linkonce_segment ());
+#ifndef NO_LISTING
+  insn->listing_ctxt = cur_fde_data->listing_ctxt ? listing_tail : NULL;
+#endif
   return insn;
 }
 
 /* Construct a new FDE structure that begins at LABEL.  */
 
 void
-cfi_new_fde (symbolS *label)
+cfi_new_fde (symbolS *label, bool do_listing)
 {
   struct fde_entry *fde = alloc_fde_entry ();
   fde->start_address = label;
+  if (do_listing)
+    {
+#ifndef NO_LISTING
+      fde->listing_ctxt = listing_tail;
+#endif
+    }
   frchain_now->frch_cfi_data->last_address = label;
 }
 
@@ -458,7 +467,12 @@ cfi_new_fde (symbolS *label)
 void
 cfi_end_fde (symbolS *label)
 {
-  frchain_now->frch_cfi_data->cur_fde_data->end_address = label;
+  struct fde_entry *cur_fde_data = frchain_now->frch_cfi_data->cur_fde_data;
+
+  cur_fde_data->end_address = label;
+#ifndef NO_LISTING
+  cur_fde_data->listing_end = cur_fde_data->listing_ctxt ? listing_tail : NULL;
+#endif
   frchain_now->frch_cfi_data = NULL;
 }
 
@@ -1279,7 +1293,7 @@ dot_cfi_startproc (int ignored ATTRIBUTE_UNUSED)
       return;
     }
 
-  cfi_new_fde (symbol_temp_new_now ());
+  cfi_new_fde (symbol_temp_new_now (), listing & LISTING_LISTING);
 
   SKIP_WHITESPACE ();
   if (is_name_beginner (*input_line_pointer) || *input_line_pointer == '"')
@@ -2058,7 +2072,19 @@ output_fde (struct fde_entry *fde, struct cie_entry *cie,
 
   for (; first; first = first->next)
     if (CUR_SEG (first) == CUR_SEG (fde))
-      output_cfi_insn (first);
+      {
+#ifndef NO_LISTING
+	if (eh_frame)
+	  listing_override_tail (first->listing_ctxt);
+#endif
+	output_cfi_insn (first);
+      }
+
+#ifndef NO_LISTING
+  /* Associate any padding with .cfi_endproc.  */
+  if (eh_frame)
+    listing_override_tail (fde->listing_end);
+#endif
 
   frag_align (align, DW_CFA_nop, 0);
   symbol_set_value_now (end_address);
@@ -2305,6 +2331,12 @@ cfi_finish (void)
   segT cfi_seg, ccseg;
   struct fde_entry *fde;
   struct cfi_insn_data *first;
+#ifndef NO_LISTING
+  /* We may temporarily replace listing_tail, which otherwise isn't supposed
+     to be changing anymore.  Play safe and restore the original value
+     afterwards.  */
+  struct list_info_struct *saved_listing_tail = NULL;
+#endif
   int save_flag_traditional_format, seek_next_seg;
 
   if (all_fde_data == 0)
@@ -2387,6 +2419,16 @@ cfi_finish (void)
 		  fde->end_address = fde->start_address;
 		}
 
+#ifndef NO_LISTING
+	      {
+		struct list_info_struct *listing_prev
+		  = listing_override_tail (fde->listing_ctxt);
+
+		if (!saved_listing_tail)
+		  saved_listing_tail = listing_prev;
+	      }
+#endif
+
 	      cie = select_cie_for_fde (fde, true, &first, 2);
 	      fde->eh_loc = symbol_temp_new_now ();
 	      output_fde (fde, cie, true, first,
@@ -2593,6 +2635,11 @@ cfi_finish (void)
       htab_delete (dwcfi_hash);
       dwcfi_hash = NULL;
     }
+
+#ifndef NO_LISTING
+  if (saved_listing_tail)
+    listing_tail = saved_listing_tail;
+#endif
 }
 
 #else /* TARGET_USE_CFIPOP */
diff --git a/gas/dw2gencfi.h b/gas/dw2gencfi.h
index a9bd7b8f289..e9eec434573 100644
--- a/gas/dw2gencfi.h
+++ b/gas/dw2gencfi.h
@@ -52,7 +52,7 @@ extern void dot_cfi_sections (int);
 extern void cfi_finish (void);
 
 /* Entry points for backends to add unwind information.  */
-extern void cfi_new_fde (struct symbol *);
+extern void cfi_new_fde (struct symbol *, bool);
 extern void cfi_end_fde (struct symbol *);
 extern void cfi_set_last_fde (struct fde_entry *fde);
 extern void cfi_set_return_column (unsigned);
@@ -105,6 +105,9 @@ struct cfi_insn_data
   struct cfi_insn_data *next;
 #if MULTIPLE_FRAME_SECTIONS
   segT cur_seg;
+#endif
+#ifndef NO_LISTING
+  struct list_info_struct *listing_ctxt;
 #endif
   int insn;
   union
@@ -193,6 +196,10 @@ struct fde_entry
   symbolS *end_address;
   struct cfi_insn_data *data;
   struct cfi_insn_data **last;
+#ifndef NO_LISTING
+  struct list_info_struct *listing_ctxt;
+  struct list_info_struct *listing_end;
+#endif
   unsigned char per_encoding;
   unsigned char lsda_encoding;
   int personality_id;
diff --git a/gas/listing.c b/gas/listing.c
index 66d8dac2330..c06e749493b 100644
--- a/gas/listing.c
+++ b/gas/listing.c
@@ -431,6 +431,51 @@ listing_newline (char *ps)
 #endif
 }
 
+/* Set listing context back to where it was when input was parsed, to allow
+   associating late code/data emission to segments with their origin.  */
+
+struct list_info_struct *listing_override_tail (struct list_info_struct *info)
+{
+  struct list_info_struct *prev = listing_tail;
+  const fragS *frag;
+
+  if (!info)
+    return NULL;
+
+  listing_tail = info;
+
+  /* The first frag created by listing_newline() is still associated with the
+     earlier line.  For the adjustment done below this property doesn't hold,
+     though.  */
+  frag = info->frag;
+  if (frag->line != info)
+    frag = frag->fr_next;
+
+  /* Check whether there's any other output data already for this line.  Replace
+     info->frag only if there's none.  This is to cover for contributions to
+     multiple sections from a single line not being properly represented in the
+     listing, at the time of writing.  Prefer the listing to show any "ordinary"
+     code/data contributions over any .eh_frame ones.  (This way multiple .cfi_*
+     on a single line will also have all their contributions listed, rather
+     than just those from the last such directive.)  */
+  for (; frag; frag = frag->fr_next)
+    if (frag->line != info
+	|| (frag->fr_type != rs_dummy
+	    && (frag->fr_type != rs_fill
+		|| frag->fr_fix
+		|| (frag->fr_var && frag->fr_offset))))
+      break;
+
+  if (!frag || frag->line != info)
+    {
+      new_frag ();
+      info->frag = frag_now;
+      new_frag ();
+    }
+
+  return prev;
+}
+
 /* Attach all current frags to the previous line instead of the
    current line.  This is called by the MIPS backend when it discovers
    that it needs to add some NOP instructions; the added NOP
diff --git a/gas/listing.h b/gas/listing.h
index cd4949861a9..308a01d62f9 100644
--- a/gas/listing.h
+++ b/gas/listing.h
@@ -47,6 +47,7 @@ void listing_error (const char *message);
 void listing_file (const char *name);
 void listing_list (int on);
 void listing_newline (char *ps);
+struct list_info_struct *listing_override_tail (struct list_info_struct *);
 void listing_prev_line (void);
 void listing_print (char *, char **);
 void listing_psize (int);
diff --git a/gas/scfidw2gen.c b/gas/scfidw2gen.c
index 9b3ad4b13e0..4dfc8a217cf 100644
--- a/gas/scfidw2gen.c
+++ b/gas/scfidw2gen.c
@@ -133,7 +133,7 @@ scfi_dot_cfi_startproc (const symbolS *start_sym)
       return;
     }
 
-  cfi_new_fde ((symbolS *)start_sym);
+  cfi_new_fde ((symbolS *)start_sym, false);
 
   cfi_set_sections ();
 
diff --git a/gas/testsuite/gas/cfi/cfi.exp b/gas/testsuite/gas/cfi/cfi.exp
index d8bbf0df6e8..83d393cab7c 100644
--- a/gas/testsuite/gas/cfi/cfi.exp
+++ b/gas/testsuite/gas/cfi/cfi.exp
@@ -116,6 +116,11 @@ if  { [istarget "i*86-*-*"] || [istarget "x86_64-*-*"] } then {
 
 run_list_test "cfi-diag-1" ""
 
+# ARC and HPPA use ';' as comment char rather than as statement separator.
+if { ![istarget "arc*-*-*"] && ![istarget "hppa*-*"] } then {
+    run_list_test "listing" "-aln --listing-cont-lines=1"
+}
+
 # HPPA64 uses 64-bit relocations, which results in all of the dump
 # offset numbers not matching up.
 if { ![istarget "hppa64*-*"] } then {
diff --git a/gas/testsuite/gas/cfi/listing.l b/gas/testsuite/gas/cfi/listing.l
new file mode 100644
index 00000000000..b15c6ca605c
--- /dev/null
+++ b/gas/testsuite/gas/cfi/listing.l
@@ -0,0 +1,27 @@
+[ 	]*[0-9]*[ 	]+\.text
+[ 	]*[0-9]*[ 	]+func:
+[ 	]*[0-9]*[ 	]+0000 (1[04]00 ?0000|0000 ?001[04])[ 	]+\.cfi_startproc
+[ 	]*[0-9]*[ 	]+0000 ?0000 *
+[ 	]*[0-9]*[ 	]+[0-9a-f]{4} 0A[ 	]+\.cfi_remember_state
+[ 	]*[0-9]*[ 	]+0000 .*[ 	]\.nop
+[ 	]*[0-9]*[ 	]+[0-9a-f]{4} 4.0A[ 	]+\.cfi_escape 0x0a
+[ 	]*[0-9]*[ 	]+[0-9a-f]{4} .*[ 	]\.nop
+[ 	]*[0-9]*[ 	]+[0-9a-f]{4} 4.02 ?0002[ 	]+\.cfi_escape 0x02, 0x00, 0x02, 0x00
+[ 	]*[0-9]*[ 	]+00
+[ 	]*[0-9]*[ 	]+[0-9a-f]{4} .*[ 	]\.nop
+[ 	]*[0-9]*[ 	]+[0-9a-f]{4} 4.03 ?0000[ 	]+\.cfi_escape 0x03; .cfi_escape 0x00, 0x00
+[ 	]*[0-9]*[ 	]+[0-9a-f]{4} 0400 ?0000[ 	]+\.cfi_escape 0x04; .cfi_escape 0x00, 0x00, 0x00, 0x00
+[ 	]*[0-9]*[ 	]+00
+[ 	]*[0-9]*[ 	]+[0-9a-f]{4} .*[ 	]\.nop
+[ 	]*[0-9]*[ 	]+[0-9a-f]{4} 4.0B[ 	]+\.cfi_escape 0x0b
+[ 	]*[0-9]*[ 	]+[0-9a-f]{4} .*[ 	]\.nop
+[ 	]*[0-9]*[ 	]+[0-9a-f]{4} 4.0B[ 	]+\.cfi_restore_state
+[ 	]*[0-9]*[ 	]+[0-9a-f]{4} .*[ 	]\.nop
+[ 	]*[0-9]*[ 	]+([0-9a-f]{4} [0 	]+)?\.cfi_endproc
+[ 	]*[0-9]*[ 	]*
+[ 	]*[0-9]*[ 	]+func2:
+[ 	]*[0-9]*[ 	]+[0-9a-f]{4} (1[048]00 ?0000|0000 ?001[048])[ 	]+\.cfi_startproc
+[ 	]*[0-9]*[ 	]+(4[048]00 ?0000|0000 ?004[048]) *
+[ 	]*[0-9]*[ 	]+[0-9a-f]{4} .*[ 	]\.nop
+[ 	]*[0-9]*[ 	]+([0-9a-f]{4} [0 	]+)?\.cfi_endproc
+#pass
diff --git a/gas/testsuite/gas/cfi/listing.s b/gas/testsuite/gas/cfi/listing.s
new file mode 100644
index 00000000000..e091c54fa56
--- /dev/null
+++ b/gas/testsuite/gas/cfi/listing.s
@@ -0,0 +1,22 @@
+	.text
+func:
+	.cfi_startproc
+	.cfi_remember_state
+	.nop
+	.cfi_escape 0x0a
+	.nop
+	.cfi_escape 0x02, 0x00, 0x02, 0x00
+	.nop
+	.cfi_escape 0x03; .cfi_escape 0x00, 0x00
+	.cfi_escape 0x04; .cfi_escape 0x00, 0x00, 0x00, 0x00
+	.nop
+	.cfi_escape 0x0b
+	.nop
+	.cfi_restore_state
+	.nop
+	.cfi_endproc
+
+func2:
+	.cfi_startproc
+	.nop
+	.cfi_endproc


More information about the Binutils-cvs mailing list