public inbox for debugedit@sourceware.org
 help / color / mirror / Atom feed
From: Mark Wielaard <mark@klomp.org>
To: debugedit@sourceware.org
Cc: Allan McRae <allan@archlinux.org>, Mark Wielaard <mark@klomp.org>
Subject: [PATCH v2] debugedit: Only write the ELF file when updating strings or build-id
Date: Thu, 21 Mar 2024 13:06:03 +0100	[thread overview]
Message-ID: <20240321120658.262490-1-mark@klomp.org> (raw)

Only open the ELF file read/write and write out the data if we
actually did any elf structure update or updating the build-id.

	* tools/debugedit.c (fdopen_dso): Call elf_begin with
	ELF_C_READ when not changing dest_dir or build_id,
	otherwise use ELF_C_RDWR.
	(main): Call open with O_RDONLY when not changing dest_dir
	or build_id, otherwise use O_RDWR. Call elf_update with
	ELF_C_WRITE when rewriting any string, updating any ELF
	structure or build_id.

https://sourceware.org/bugzilla/show_bug.cgi?id=31504

Signed-off-by: Mark Wielaard <mark@klomp.org>
---
 tools/debugedit.c | 21 ++++++++++++++++++---
 1 file changed, 18 insertions(+), 3 deletions(-)

v2: Only actually call elf_update with ELF_C_WRITE when rewriting any
    string, updating any ELF or build_id.

diff --git a/tools/debugedit.c b/tools/debugedit.c
index 7802f9fe03fc..f16eecd89a61 100644
--- a/tools/debugedit.c
+++ b/tools/debugedit.c
@@ -3261,7 +3261,10 @@ fdopen_dso (int fd, const char *name)
   DSO *dso = NULL;
   size_t phnum;
 
-  elf = elf_begin (fd, ELF_C_RDWR, NULL);
+  if (dest_dir == NULL && (!do_build_id || no_recompute_build_id))
+    elf = elf_begin (fd, ELF_C_READ, NULL);
+  else
+    elf = elf_begin (fd, ELF_C_RDWR, NULL);
   if (elf == NULL)
     {
       error (0, 0, "cannot open ELF file: %s", elf_errmsg (-1));
@@ -3600,7 +3603,10 @@ main (int argc, char *argv[])
   if (chmod (file, stat_buf.st_mode | S_IRUSR | S_IWUSR) != 0)
     error (0, errno, "Failed to chmod input file '%s' to make sure we can read and write", file);
 
-  fd = open (file, O_RDWR);
+  if (dest_dir == NULL && (!do_build_id || no_recompute_build_id))
+    fd = open (file, O_RDONLY);
+  else
+    fd = open (file, O_RDWR);
   if (fd < 0)
     {
       error (1, errno, "Failed to open input file '%s'", file);
@@ -3805,7 +3811,16 @@ main (int argc, char *argv[])
   if (do_build_id && build_id != NULL)
     handle_build_id (dso, build_id, build_id_offset, build_id_size);
 
-  if (elf_update (dso->elf, ELF_C_WRITE) < 0)
+  /* If we have done any string replacement or rewrote any section
+     data or did a build_id rewrite we need to write out the new ELF
+     image.  */
+  if ((need_string_replacement
+       || need_strp_update
+       || need_line_strp_update
+       || need_stmt_update
+       || dirty_elf
+       || (build_id && !no_recompute_build_id))
+      && elf_update (dso->elf, ELF_C_WRITE) < 0)
     {
       error (1, 0, "Failed to write file: %s", elf_errmsg (elf_errno()));
     }
-- 
2.44.0


                 reply	other threads:[~2024-03-21 12:07 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=20240321120658.262490-1-mark@klomp.org \
    --to=mark@klomp.org \
    --cc=allan@archlinux.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).