[PATCH] RISC-V: Fix incorrect DW_AT_high_pc after linker relaxation

Yu-Cheng Liang yclwlcy@gmail.com
Mon Feb 10 08:16:24 GMT 2025


Fix the issue reported in https://sourceware.org/bugzilla/show_bug.cgi?id=28878

Signed-off-by: Yu-Cheng Liang <yclwlcy@gmail.com>
---
 gas/as.c              |  4 ++++
 gas/config/tc-riscv.c |  9 +++++++--
 gas/config/tc-riscv.h |  3 +++
 gas/dwarf2dbg.c       | 28 ++++++++++++++++++++++------
 4 files changed, 36 insertions(+), 8 deletions(-)

diff --git a/gas/as.c b/gas/as.c
index 449167db595..4f41029caa0 100644
--- a/gas/as.c
+++ b/gas/as.c
@@ -1464,6 +1464,10 @@ main (int argc, char ** argv)
      directives from the user or by the backend, emit it now.  */
   cfi_finish ();

+#ifdef md_insert_uleb128_fixes
+  md_insert_uleb128_fixes ();
+#endif
+
   keep_it = 0;
   if (seen_at_least_1_file ())
     {
diff --git a/gas/config/tc-riscv.c b/gas/config/tc-riscv.c
index a915c8b4995..d1142ec4075 100644
--- a/gas/config/tc-riscv.c
+++ b/gas/config/tc-riscv.c
@@ -5623,8 +5623,6 @@ void
 riscv_md_finish (void)
 {
   riscv_set_public_attributes ();
-  if (riscv_opts.relax)
-    bfd_map_over_sections (stdoutput, riscv_insert_uleb128_fixes, NULL);
 }

 /* Called just before the assembler exits.  */
@@ -5635,6 +5633,13 @@ riscv_md_end (void)
   htab_delete (riscv_pcrel_hi_fixup_hash);
 }

+void
+riscv_md_insert_uleb128_fixes (void)
+{
+  if (riscv_opts.relax)
+    bfd_map_over_sections (stdoutput, riscv_insert_uleb128_fixes, NULL);
+}
+
 /* Adjust the symbol table.  */

 void
diff --git a/gas/config/tc-riscv.h b/gas/config/tc-riscv.h
index 6b25a791da8..9702582dd47 100644
--- a/gas/config/tc-riscv.h
+++ b/gas/config/tc-riscv.h
@@ -147,6 +147,9 @@ bool riscv_parse_name (const char *, struct
expressionS *, enum expr_mode);
 extern void riscv_md_finish (void);
 extern int riscv_convert_symbolic_attribute (const char *);

+#define md_insert_uleb128_fixes riscv_md_insert_uleb128_fixes
+extern void riscv_md_insert_uleb128_fixes (void);
+
 /* Set mapping symbol states.  */
 #define md_cons_align(nbytes) riscv_mapping_state (MAP_DATA, 0, 0)
 void riscv_mapping_state (enum riscv_seg_mstate, int, bool);
diff --git a/gas/dwarf2dbg.c b/gas/dwarf2dbg.c
index 2963e52958a..b2f62824661 100644
--- a/gas/dwarf2dbg.c
+++ b/gas/dwarf2dbg.c
@@ -2991,14 +2991,30 @@ out_debug_info (segT info_seg, segT
abbrev_seg, segT line_seg, segT str_seg,
    if (DWARF2_VERSION < 4)
      {
        if (size.X_op == O_constant)
- size.X_op = O_symbol;
-       size.X_add_symbol = symp;
-       emit_expr (&size, sizeof_address);
+ {
+   exp.X_op = O_symbol;
+   exp.X_add_symbol = symp;
+   exp.X_add_number = size.X_add_number;
+   emit_expr (&exp, sizeof_address);
+ }
+       else
+ {
+   exp.X_op = O_symbol;
+   exp.X_add_symbol
+       = symbol_get_value_expression (size.X_op_symbol)
+     ->X_add_symbol;
+   exp.X_add_number = 0;
+   emit_expr (&exp, sizeof_address);
+ }
      }
-   else if (size.X_op == O_constant)
-     out_uleb128 (size.X_add_number);
    else
-     emit_leb128_expr (symbol_get_value_expression (size.X_op_symbol), 0);
+     {
+       if (size.X_op == O_constant)
+ out_uleb128 (size.X_add_number);
+       else
+ emit_leb128_expr (
+     symbol_get_value_expression (size.X_op_symbol), 0);
+     }
  }

       if (DWARF2_VERSION > 2)
-- 
2.47.1


More information about the Binutils mailing list