This is the mail archive of the binutils-cvs@sourceware.org mailing list for the binutils project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[binutils-gdb] Don't include libbfd.h outside of bfd, part 6


https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=7d0b9ebc1e0079271a7c7737b53bc026525eab64

commit 7d0b9ebc1e0079271a7c7737b53bc026525eab64
Author: Alan Modra <amodra@gmail.com>
Date:   Sat Jul 16 13:32:16 2016 +0930

    Don't include libbfd.h outside of bfd, part 6
    
    Some messing with plugin code in order to not need arelt_size in
    ld code.  File descriptor handling in ld/plugin.c is tidied too,
    simply duping the open fd rather than opening the file again.
    
    bfd/
    	* elflink.c: Include plugin-api.h.
    	* plugin.c (bfd_plugin_open_input): New function, extracted from..
    	(try_claim): ..here.
    	* plugin.h: Don't include bfd.h.
    	(bfd_plugin_open_input): Declare.
    binutils/
    	* ar.c: Include plugin-api.h.
    	* nm.c: Likewise.
    ld/
    	* plugin.c: Don't include libbfd.h.  Include plugin-api.h
    	before bfd/plugin.h.
    	(plugin_object_p): Use bfd_plugin_open_input.

Diff:
---
 bfd/ChangeLog      |  8 ++++++++
 bfd/elflink.c      |  1 +
 bfd/plugin.c       | 57 +++++++++++++++++++++++++++---------------------------
 bfd/plugin.h       |  3 +--
 binutils/ChangeLog |  5 +++++
 binutils/ar.c      |  1 +
 binutils/nm.c      |  1 +
 ld/ChangeLog       |  6 ++++++
 ld/plugin.c        | 53 +++++++++++++++-----------------------------------
 9 files changed, 68 insertions(+), 67 deletions(-)

diff --git a/bfd/ChangeLog b/bfd/ChangeLog
index bf79618..9be7d89 100644
--- a/bfd/ChangeLog
+++ b/bfd/ChangeLog
@@ -1,5 +1,13 @@
 2016-07-16  Alan Modra  <amodra@gmail.com>
 
+	* elflink.c: Include plugin-api.h.
+	* plugin.c (bfd_plugin_open_input): New function, extracted from..
+	(try_claim): ..here.
+	* plugin.h: Don't include bfd.h.
+	(bfd_plugin_open_input): Declare.
+
+2016-07-16  Alan Modra  <amodra@gmail.com>
+
 	* targets.c (bfd_seach_for_target): Rename to..
 	(bfd_iterate_over_targets): ..this.  Rewrite doc.
 	* bfd-in2.h: Regenerate.
diff --git a/bfd/elflink.c b/bfd/elflink.c
index b2a814f..a994b83 100644
--- a/bfd/elflink.c
+++ b/bfd/elflink.c
@@ -29,6 +29,7 @@
 #include "libiberty.h"
 #include "objalloc.h"
 #if BFD_SUPPORTS_PLUGINS
+#include "plugin-api.h"
 #include "plugin.h"
 #endif
 
diff --git a/bfd/plugin.c b/bfd/plugin.c
index c66d95e..3931d27 100644
--- a/bfd/plugin.c
+++ b/bfd/plugin.c
@@ -158,49 +158,50 @@ bfd_plugin_set_program_name (const char *program_name)
   plugin_program_name = program_name;
 }
 
-static int
-try_claim (bfd *abfd)
+int
+bfd_plugin_open_input (bfd *ibfd, struct ld_plugin_input_file *file)
 {
-  int claimed = 0;
-  struct ld_plugin_input_file file;
   bfd *iobfd;
 
-  file.name = abfd->filename;
-
-  if (abfd->my_archive && !bfd_is_thin_archive (abfd->my_archive))
-    {
-      iobfd = abfd->my_archive;
-      file.offset = abfd->origin;
-      file.filesize = arelt_size (abfd);
-    }
-  else
-    {
-      iobfd = abfd;
-      file.offset = 0;
-      file.filesize = 0;
-    }
+  iobfd = ibfd;
+  if (ibfd->my_archive && !bfd_is_thin_archive (ibfd->my_archive))
+    iobfd = ibfd->my_archive;
+  file->name = iobfd->filename;
 
   if (!iobfd->iostream && !bfd_open_file (iobfd))
     return 0;
 
-  file.fd = fileno ((FILE *) iobfd->iostream);
+  file->fd = fileno ((FILE *) iobfd->iostream);
 
-  if (!abfd->my_archive || bfd_is_thin_archive (abfd->my_archive))
+  if (iobfd == ibfd)
     {
       struct stat stat_buf;
-      if (fstat (file.fd, &stat_buf))
+      if (fstat (file->fd, &stat_buf))
         return 0;
-      file.filesize = stat_buf.st_size;
+      file->offset = 0;
+      file->filesize = stat_buf.st_size;
+    }
+  else
+    {
+      file->offset = ibfd->origin;
+      file->filesize = arelt_size (ibfd);
     }
+  return 1;
+}
+
+static int
+try_claim (bfd *abfd)
+{
+  int claimed = 0;
+  struct ld_plugin_input_file file;
 
+  if (!bfd_plugin_open_input (abfd, &file))
+    return 0;
   file.handle = abfd;
-  off_t cur_offset = lseek(file.fd, 0, SEEK_CUR);
+  off_t cur_offset = lseek (file.fd, 0, SEEK_CUR);
   claim_file (&file, &claimed);
-  lseek(file.fd, cur_offset, SEEK_SET);
-  if (!claimed)
-    return 0;
-
-  return 1;
+  lseek (file.fd, cur_offset, SEEK_SET);
+  return claimed;
 }
 
 static int
diff --git a/bfd/plugin.h b/bfd/plugin.h
index 529f8c1..0867122 100644
--- a/bfd/plugin.h
+++ b/bfd/plugin.h
@@ -21,9 +21,8 @@
 #ifndef _PLUGIN_H_
 #define _PLUGIN_H_
 
-#include "bfd.h"
-
 void bfd_plugin_set_program_name (const char *);
+int bfd_plugin_open_input (bfd *, struct ld_plugin_input_file *);
 void bfd_plugin_set_plugin (const char *);
 bfd_boolean bfd_plugin_target_p (const bfd_target *);
 bfd_boolean bfd_plugin_specified_p (void);
diff --git a/binutils/ChangeLog b/binutils/ChangeLog
index 8cf3188..fed99fc 100644
--- a/binutils/ChangeLog
+++ b/binutils/ChangeLog
@@ -1,5 +1,10 @@
 2016-07-16  Alan Modra  <amodra@gmail.com>
 
+	* ar.c: Include plugin-api.h.
+	* nm.c: Likewise.
+
+2016-07-16  Alan Modra  <amodra@gmail.com>
+
 	* bucomm.c: Don't include libbfd.h.
 	(endian_string, display_target_list): Delete forward declaration.
 	(display_info_table, display_target_tables): Likewise.
diff --git a/binutils/ar.c b/binutils/ar.c
index 1337710..ba0d581 100644
--- a/binutils/ar.c
+++ b/binutils/ar.c
@@ -33,6 +33,7 @@
 #include "arsup.h"
 #include "filenames.h"
 #include "binemul.h"
+#include "plugin-api.h"
 #include "plugin.h"
 
 #ifdef __GO32___
diff --git a/binutils/nm.c b/binutils/nm.c
index 5ca4d34..766564d 100644
--- a/binutils/nm.c
+++ b/binutils/nm.c
@@ -36,6 +36,7 @@
 #include "coff/internal.h"
 #include "libcoff.h"
 #include "bucomm.h"
+#include "plugin-api.h"
 #include "plugin.h"
 
 /* When sorting by size, we use this structure to hold the size and a
diff --git a/ld/ChangeLog b/ld/ChangeLog
index e4026ec..758d7ad 100644
--- a/ld/ChangeLog
+++ b/ld/ChangeLog
@@ -1,5 +1,11 @@
 2016-07-16  Alan Modra  <amodra@gmail.com>
 
+	* plugin.c: Don't include libbfd.h.  Include plugin-api.h
+	before bfd/plugin.h.
+	(plugin_object_p): Use bfd_plugin_open_input.
+
+2016-07-16  Alan Modra  <amodra@gmail.com>
+
 	* ldlang.c (open_output): Replace bfd_search_for_target with
 	bfd_iterate_over_targets.  Localize vars.
 
diff --git a/ld/plugin.c b/ld/plugin.c
index 924b59c..ffa0dd3 100644
--- a/ld/plugin.c
+++ b/ld/plugin.c
@@ -21,7 +21,6 @@
 #include "sysdep.h"
 #include "libiberty.h"
 #include "bfd.h"
-#include "libbfd.h"
 #include "bfdlink.h"
 #include "bfdver.h"
 #include "ld.h"
@@ -30,9 +29,9 @@
 #include "ldexp.h"
 #include "ldlang.h"
 #include "ldfile.h"
+#include "plugin-api.h"
 #include "../bfd/plugin.h"
 #include "plugin.h"
-#include "plugin-api.h"
 #include "elf-bfd.h"
 #if HAVE_MMAP
 # include <sys/mman.h>
@@ -1083,12 +1082,8 @@ plugin_object_p (bfd *ibfd)
 {
   int claimed;
   plugin_input_file_t *input;
-  off_t offset, filesize;
   struct ld_plugin_input_file file;
   bfd *abfd;
-  bfd_boolean inarchive;
-  const char *name;
-  int fd;
 
   /* Don't try the dummy object file.  */
   if ((ibfd->flags & BFD_PLUGIN) != 0)
@@ -1102,14 +1097,6 @@ plugin_object_p (bfd *ibfd)
 	return NULL;
     }
 
-  inarchive = (ibfd->my_archive != NULL
-	       && !bfd_is_thin_archive (ibfd->my_archive));
-  name = inarchive ? ibfd->my_archive->filename : ibfd->filename;
-  fd = open (name, O_RDONLY | O_BINARY);
-
-  if (fd < 0)
-    return NULL;
-
   /* We create a dummy BFD, initially empty, to house whatever symbols
      the plugin may want to add.  */
   abfd = plugin_get_ir_dummy_bfd (ibfd->filename, ibfd);
@@ -1119,39 +1106,31 @@ plugin_object_p (bfd *ibfd)
     einfo (_("%P%F: plugin failed to allocate memory for input: %s\n"),
 	   bfd_get_error ());
 
-  if (inarchive)
-    {
-      /* Offset and filesize must refer to the individual archive
-	 member, not the whole file, and must exclude the header.
-	 Fortunately for us, that is how the data is stored in the
-	 origin field of the bfd and in the arelt_data.  */
-      offset = ibfd->origin;
-      filesize = arelt_size (ibfd);
-    }
-  else
-    {
-      offset = 0;
-      filesize = lseek (fd, 0, SEEK_END);
+  if (!bfd_plugin_open_input (ibfd, &file))
+    return NULL;
 
+  if (file.name == ibfd->filename)
+    {
       /* We must copy filename attached to ibfd if it is not an archive
 	 member since it may be freed by bfd_close below.  */
-      name = plugin_strdup (abfd, name);
+      file.name = plugin_strdup (abfd, file.name);
     }
 
-  file.name = name;
-  file.offset = offset;
-  file.filesize = filesize;
-  file.fd = fd;
   file.handle = input;
+  /* The plugin API expects that the file descriptor won't be closed
+     and reused as done by the bfd file cache.  So dup one.  */
+  file.fd = dup (file.fd);
+  if (file.fd < 0)
+    return NULL;
 
   input->abfd = abfd;
   input->view_buffer.addr = NULL;
   input->view_buffer.filesize = 0;
   input->view_buffer.offset = 0;
-  input->fd = fd;
+  input->fd = file.fd;
   input->use_mmap = FALSE;
-  input->offset = offset;
-  input->filesize = filesize;
+  input->offset = file.offset;
+  input->filesize = file.filesize;
   input->name = plugin_strdup (abfd, ibfd->filename);
 
   claimed = 0;
@@ -1160,7 +1139,7 @@ plugin_object_p (bfd *ibfd)
     einfo (_("%P%F: %s: plugin reported error claiming file\n"),
 	   plugin_error_plugin ());
 
-  if (input->fd != -1 && ! bfd_plugin_target_p (ibfd->xvec))
+  if (input->fd != -1 && !bfd_plugin_target_p (ibfd->xvec))
     {
       /* FIXME: fd belongs to us, not the plugin.  GCC plugin, which
 	 doesn't need fd after plugin_call_claim_file, doesn't use
@@ -1170,7 +1149,7 @@ plugin_object_p (bfd *ibfd)
 	 release_input_file after it is done, uses BFD plugin target
 	 vector.  This scheme doesn't work when a plugin needs fd and
 	 doesn't use BFD plugin target vector neither.  */
-      close (fd);
+      close (input->fd);
       input->fd = -1;
     }


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]