]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
gas: include .cfi_* generated data in listing
authorJan Beulich <jbeulich@suse.com>
Fri, 14 Mar 2025 09:30:18 +0000 (10:30 +0100)
committerJan Beulich <jbeulich@suse.com>
Fri, 14 Mar 2025 09:30:18 +0000 (10:30 +0100)
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.

gas/config/tc-alpha.c
gas/dw2gencfi.c
gas/dw2gencfi.h
gas/listing.c
gas/listing.h
gas/scfidw2gen.c
gas/testsuite/gas/cfi/cfi.exp
gas/testsuite/gas/cfi/listing.l [new file with mode: 0644]
gas/testsuite/gas/cfi/listing.s [new file with mode: 0644]

index d6245e5ed6c9aec0f0a6f6576b48b67be326267a..a90ceb4ed80e021d385cdb345eadbc9327159698 100644 (file)
@@ -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);
index 4f5622d6d8756718ee19eba207f2dda84f81823e..310cb56a34e4d70ff59a3fa17f79aa2b17f9b388 100644 (file)
@@ -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 */
index a9bd7b8f289315368c91f3c8e8297962322a1629..e9eec43457325b498186a9b6071c2ecfe681d6fb 100644 (file)
@@ -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;
index 66d8dac2330e604d91276c4d80e6d62d48c27511..c06e749493b5eea9c8699e17d39b478eba7073fb 100644 (file)
@@ -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
index cd4949861a9b3647db81b5e84c071710b1b9d307..308a01d62f9d1e65ad06debed04a0385356f6067 100644 (file)
@@ -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);
index 9b3ad4b13e01e7c5ffc4c2e9de5b33a50cde5e3d..4dfc8a217cfabbd3ccb5ce6a6b397c821ea846ff 100644 (file)
@@ -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 ();
 
index d8bbf0df6e80beeb218c3319f2213e359b455ad1..83d393cab7c0b6d84879d24b029983b1fe0db298 100644 (file)
@@ -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 (file)
index 0000000..b15c6ca
--- /dev/null
@@ -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 (file)
index 0000000..e091c54
--- /dev/null
@@ -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