public inbox for debugedit@sourceware.org
 help / color / mirror / Atom feed
From: Mark Wielaard <mark@klomp.org>
To: debugedit@sourceware.org
Cc: Mark Wielaard <mark@klomp.org>
Subject: [RFC] debugedit: Update relocation addend or symbol value for debug strings
Date: Wed, 21 Jan 2026 13:31:52 +0100	[thread overview]
Message-ID: <20260121123342.137521-1-mark@klomp.org> (raw)

RISC-V (ET_REL) object files don't use STT_SECTION symbols to relocate
against sections containing string pools, but creates local symbols to
each string with a relocation addend of zero. Detect this and update
the symbol st_value instead of the r_addend in that case.

	   * tools/debugedit.c (setup_relbuf): Check symbol is either
	   STT_SECTION with st_value zero or not STT_SECTION with
	   r_addend zero. Use r_addend in the first case, st_value in
	   the second.
	   (update_rela_data): Update either r_added if relocation
	   symbol is STT_SECTION, or st_value if the symbol is not.

https://sourceware.org/bugzilla/show_bug.cgi?id=33789
---
 tools/debugedit.c | 59 ++++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 51 insertions(+), 8 deletions(-)

Currently testing https://builder.sourceware.org/buildbot/#/changes/108020
But doesn't have an good testcase yet.

diff --git a/tools/debugedit.c b/tools/debugedit.c
index c7cd778209a3..b3f637230100 100644
--- a/tools/debugedit.c
+++ b/tools/debugedit.c
@@ -1,5 +1,5 @@
 /* Copyright (C) 2001-2003, 2005, 2007, 2009-2011, 2016, 2017 Red Hat, Inc.
-   Copyright (C) 2022, 2023, 2024, 2025 Mark J. Wielaard <mark@klomp.org>
+   Copyright (C) 2022, 2023, 2024, 2025, 2026 Mark J. Wielaard <mark@klomp.org>
    Written by Alexander Larsson <alexl@redhat.com>, 2002
    Based on code by Jakub Jelinek <jakub@redhat.com>, 2001.
    String/Line table rewriting by Mark Wielaard <mjw@redhat.com>, 2017.
@@ -647,8 +647,24 @@ setup_relbuf (DSO *dso, debug_section *sec)
 	   && sym.st_shndx != debug_sections[DEBUG_MACRO].sec
 	   && sym.st_shndx != debug_sections[DEBUG_ABBREV].sec))
 	continue;
-      rela.r_addend += sym.st_value;
-      rtype = ELF64_R_TYPE (rela.r_info);
+
+      rtype = GELF_R_TYPE (rela.r_info);
+
+      /* We normally expect the relocation to be against an
+	 STT_SECTION symbol, which has an st_value of zero. So only
+	 the relocation r_addend counts.  But some archves (riscv) use
+	 relocations with zero r_addend, and symbols where the
+	 st_value has the (relative to the start of the section)
+	 offset.  */
+      if (GELF_ST_TYPE (sym.st_info) == STT_SECTION
+	  && sym.st_value == 0)
+	; /* rela.r_addend already contains the offset.  */
+      else if (GELF_ST_TYPE (sym.st_info) != STT_SECTION
+	       && rela.r_addend == 0)
+	rela.r_addend = sym.st_value; /* offset is in st_value.  */
+      else
+	goto fail; /* This is not the relocation you are looking for.  */
+
       switch (dso->ehdr.e_machine)
 	{
 	case EM_SPARC:
@@ -769,6 +785,8 @@ update_rela_data (DSO *dso, struct debug_section *sec)
 
   REL *relptr = sec->relbuf;
   REL *relend = sec->relend;
+  bool rel_updated = false;
+  bool sym_updated = false;
   while (relptr < relend)
     {
       GElf_Sym sym;
@@ -783,15 +801,40 @@ update_rela_data (DSO *dso, struct debug_section *sec)
 		       &sym) == NULL)
 	error (1, 0, "Couldn't get symbol: %s", elf_errmsg (-1));
 
-      rela.r_addend = relptr->addend - sym.st_value;
+      if (GELF_ST_TYPE (sym.st_info) == STT_SECTION)
+	{
+	  if (rela.r_addend != relptr->addend)
+	    {
+	      rela.r_addend = relptr->addend;
+
+	      if (gelf_update_rela (data, ndx, &rela) == 0)
+		error (1, 0, "Couldn't update relocations: %s",
+		       elf_errmsg (-1));
 
-      if (gelf_update_rela (data, ndx, &rela) == 0)
-	error (1, 0, "Couldn't update relocations: %s",
-	       elf_errmsg (-1));
+	      rel_updated = true;
+	    }
+	}
+      else
+	{
+	  if (sym.st_value != relptr->addend)
+	    {
+	      sym.st_value = relptr->addend;
 
+	      if (gelf_update_sym (symdata,
+				   GELF_R_SYM (rela.r_info), &sym) == 0)
+		error (1, 0, "Couldn't update relocation symbols: %s",
+		       elf_errmsg (-1));
+
+	      sym_updated = true;
+	    }
+	}
       ++relptr;
     }
-  elf_flagdata (data, ELF_C_SET, ELF_F_DIRTY);
+
+  if (rel_updated)
+    elf_flagdata (data, ELF_C_SET, ELF_F_DIRTY);
+  if (sym_updated)
+    elf_flagdata (symdata, ELF_C_SET, ELF_F_DIRTY);
 
   free (sec->relbuf);
 }
-- 
2.52.0


                 reply	other threads:[~2026-01-21 12:33 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260121123342.137521-1-mark@klomp.org \
    --to=mark@klomp.org \
    --cc=debugedit@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).