public inbox for debugedit@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] debugedit: Only write the ELF file when updating strings or build-id
@ 2024-03-18 22:37 Mark Wielaard
  0 siblings, 0 replies; only message in thread
From: Mark Wielaard @ 2024-03-18 22:37 UTC (permalink / raw)
  To: debugedit; +Cc: Mark Wielaard

Only open the ELF file read/write and write out the data if we are
changing a dest_dir 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 changing dest_dir or build_id.

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

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

diff --git a/tools/debugedit.c b/tools/debugedit.c
index 7802f9fe03fc..5886d0d8e50f 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,8 @@ 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 ((dest_dir != NULL || (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.39.3


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2024-03-18 22:37 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-03-18 22:37 [PATCH] debugedit: Only write the ELF file when updating strings or build-id Mark Wielaard

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).