[PATCH v12 16/32] gdb_bfd_open_from_target: Optionally do not close fd

Jan Kratochvil jan.kratochvil@redhat.com
Fri Aug 21 21:22:00 GMT 2015


Hi,

gdb_bfd_open_from_target currently always embedded passed fd into returned BFD
and closed that fd after closing that BFD.  As one cannot do dup() for target
fileio FDs one could not use FD after BFD using it has been closed.

Jan


gdb/ChangeLog
2015-08-18  Jan Kratochvil  <jan.kratochvil@redhat.com>

	* gdb_bfd.c (gdb_bfd_iovec_fileio_close_nop): New function.
	(gdb_bfd_open_from_target): Add parameter do_close.  Optionally pass
	gdb_bfd_iovec_fileio_close_nop.
	(gdb_bfd_open): Update caller.
	* gdb_bfd.h (gdb_bfd_open_from_target): Update prototype.
---
 0 files changed

diff --git a/gdb/gdb_bfd.c b/gdb/gdb_bfd.c
index 51395f9..1224695 100644
--- a/gdb/gdb_bfd.c
+++ b/gdb/gdb_bfd.c
@@ -345,6 +345,18 @@ gdb_bfd_iovec_fileio_close (struct bfd *abfd, void *stream)
   return 0;
 }
 
+/* Helper for gdb_bfd_open_from_target when caller did not wish to close
+   ABFD's FD.  */
+
+static int
+gdb_bfd_iovec_fileio_close_nop (struct bfd *abfd, void *stream)
+{
+  xfree (stream);
+
+  /* Zero means success.  */
+  return 0;
+}
+
 /* Wrapper for target_fileio_fstat suitable for passing as the
    STAT_FUNC argument to gdb_bfd_openr_iovec.  */
 
@@ -371,7 +383,8 @@ gdb_bfd_iovec_fileio_fstat (struct bfd *abfd, void *stream,
    gdb_bfd_openr_iovec.  */
 
 struct bfd *
-gdb_bfd_open_from_target (const char *name, const char *target, int fd)
+gdb_bfd_open_from_target (const char *name, const char *target, int fd,
+			  int do_close)
 {
   gdb_assert (is_target_filename (name));
   gdb_assert (!target_filesystem_is_local ());
@@ -382,7 +395,8 @@ gdb_bfd_open_from_target (const char *name, const char *target, int fd)
 			      (fd == -1 ? (void *) current_inferior ()
 					: (void *) &fd),
 			      gdb_bfd_iovec_fileio_pread,
-			      gdb_bfd_iovec_fileio_close,
+			      (do_close ? gdb_bfd_iovec_fileio_close
+					: gdb_bfd_iovec_fileio_close_nop),
 			      gdb_bfd_iovec_fileio_fstat);
 }
 
@@ -400,7 +414,7 @@ gdb_bfd_open (const char *name, const char *target, int fd)
   if (is_target_filename (name))
     {
       if (!target_filesystem_is_local ())
-	return gdb_bfd_open_from_target (name, target, fd);
+	return gdb_bfd_open_from_target (name, target, fd, 1 /* do_close */);
 
       name += strlen (TARGET_SYSROOT_PREFIX);
     }
diff --git a/gdb/gdb_bfd.h b/gdb/gdb_bfd.h
index 0e38d62..dcc6755 100644
--- a/gdb/gdb_bfd.h
+++ b/gdb/gdb_bfd.h
@@ -54,7 +54,7 @@ int gdb_bfd_has_target_filename (struct bfd *abfd);
 struct bfd *gdb_bfd_open (const char *name, const char *target, int fd);
 
 struct bfd *gdb_bfd_open_from_target (const char *name, const char *target,
-				      int fd);
+				      int fd, int do_close);
 
 /* Increment the reference count of ABFD.  It is fine for ABFD to be
    NULL; in this case the function does nothing.  */



More information about the Gdb-patches mailing list