Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Tom Tromey <tom@tromey.com>
To: gdb-patches@sourceware.org
Cc: Tom Tromey <tom@tromey.com>
Subject: [PATCH 2/5] Hoist call to gdb_bfd_check_format_matches in exec.c
Date: Tue, 10 Nov 2026 19:06:56 -0700	[thread overview]
Message-ID: <20261110-minor-assert-v1-2-bb33660ca557@tromey.com> (raw)
In-Reply-To: <20261110-minor-assert-v1-0-bb33660ca557@tromey.com>

Right now, exec.c calls gdb_bfd_check_format_matches after setting the
program space's exec_file.  This means a call to exec_close is needed
before throwing an exception, to avoid leaving the exec BFD set in
this case.

It seems to me that hoisting this call is more straightforward, as now
the exec_close isn't needed.
---
 gdb/exec.c | 30 +++++++++++++-----------------
 1 file changed, 13 insertions(+), 17 deletions(-)

diff --git a/gdb/exec.c b/gdb/exec.c
index 4f0a0ca23e3..aa6fc42ebe7 100644
--- a/gdb/exec.c
+++ b/gdb/exec.c
@@ -459,18 +459,25 @@ exec_file_attach (const char *filename, int from_tty)
 	  canonical_pathname = canonical_storage.get ();
 	}
 
-      gdb_bfd_ref_ptr temp;
+      gdb_bfd_ref_ptr new_exec_bfd;
       if (write_files && !load_via_target)
-	temp = gdb_bfd_fopen (canonical_pathname, gnutarget,
-			      FOPEN_RUB, scratch_chan);
+	new_exec_bfd = gdb_bfd_fopen (canonical_pathname, gnutarget,
+				      FOPEN_RUB, scratch_chan);
       else
-	temp = gdb_bfd_open (canonical_pathname, gnutarget, scratch_chan);
-      if (temp == nullptr)
+	new_exec_bfd = gdb_bfd_open (canonical_pathname, gnutarget,
+				     scratch_chan);
+      if (new_exec_bfd == nullptr)
 	error (_("\"%ps\": could not open as an executable file: %s."),
 	       styled_string (file_name_style.style (), scratch_pathname),
 	       bfd_errmsg (bfd_get_error ()));
 
-      current_program_space->set_exec_bfd (std::move (temp));
+      if (!gdb_bfd_check_format_matches (new_exec_bfd.get (), bfd_object,
+					 &matching))
+	error (_("\"%ps\": not in executable format: %s"),
+	       styled_string (file_name_style.style (), scratch_pathname),
+	       gdb_bfd_errmsg (bfd_get_error (), matching).c_str ());
+
+      current_program_space->set_exec_bfd (std::move (new_exec_bfd));
 
       /* gdb_realpath_keepfile resolves symlinks on the local
 	 filesystem and so cannot be used for "target:" files.  */
@@ -484,17 +491,6 @@ exec_file_attach (const char *filename, int from_tty)
 	  (make_unique_xstrdup (gdb_realpath_keepfile
 				  (scratch_pathname).c_str ()));
 
-      if (!gdb_bfd_check_format_matches (current_program_space->exec_bfd (),
-				     bfd_object, &matching))
-	{
-	  /* Make sure to close exec_bfd, or else "run" might try to use
-	     it.  */
-	  current_program_space->exec_close ();
-	  error (_("\"%ps\": not in executable format: %s"),
-		 styled_string (file_name_style.style (), scratch_pathname),
-		 gdb_bfd_errmsg (bfd_get_error (), matching).c_str ());
-	}
-
       std::vector<target_section> sections
 	= build_section_table (current_program_space->exec_bfd ());
 

-- 
2.49.0


  parent reply	other threads:[~2026-11-11  2:07 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-11  2:06 [PATCH 0/5] Minor updates to program space and setting the exec file Tom Tromey
2026-11-11  2:06 ` [PATCH 1/5] Reject null pointers in program_space::set_exec_bfd Tom Tromey
2026-11-11  2:06 ` Tom Tromey [this message]
2026-11-11  2:06 ` [PATCH 3/5] Have program_space::set_exec_bfd do more work Tom Tromey
2026-11-11  2:06 ` [PATCH 4/5] Mark two program_space fields as private Tom Tromey
2026-11-11  2:06 ` [PATCH 5/5] Add some uses of bool to progspace and exec_file_attach Tom Tromey

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=20261110-minor-assert-v1-2-bb33660ca557@tromey.com \
    --to=tom@tromey.com \
    --cc=gdb-patches@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