aboutsummaryrefslogtreecommitdiff
path: root/bfd/plugin.c
diff options
context:
space:
mode:
authorH.J. Lu <hjl.tools@gmail.com>2021-07-02 13:42:32 -0700
committerH.J. Lu <hjl.tools@gmail.com>2021-07-05 08:51:35 -0700
commit918172470430ea6fa082c941e6789add88331197 (patch)
treed32fe8758c189052469f5b029bef5ebc3fc01a51 /bfd/plugin.c
parentc919d6be44913ed52d91c203e87e529ee17de805 (diff)
downloadgdb-918172470430ea6fa082c941e6789add88331197.zip
gdb-918172470430ea6fa082c941e6789add88331197.tar.gz
gdb-918172470430ea6fa082c941e6789add88331197.tar.bz2
ld: Cache and reuse the IR archive file descriptor
Linker plugin_object_p opens the IR archive for each IR archive member. For GCC plugin, plugin_object_p closes the archive file descriptor. But for LLVM plugin, the archive file descriptor remains open. If there are 3000 IR archive members, there are 3000 file descriptors for them. We can run out of file descriptors petty easily. 1. Add archive_plugin_fd and archive_plugin_fd_open_count to bfd so that we can cache and reuse the IR archive file descriptor for all IR archive members in the archive. 2. Add bfd_plugin_close_file_descriptor to properly close the IR archive file descriptor. bfd/ PR ld/28040 * archive.c (_bfd_archive_close_and_cleanup): Close the archive plugin file descriptor if needed. * bfd.c (bfd): Add archive_plugin_fd and archive_plugin_fd_open_count. * opncls.c (_bfd_new_bfd): Initialize to -1. * plugin.c (bfd_plugin_open_input): Cache and reuse the archive plugin file descriptor. (bfd_plugin_close_file_descriptor): New function. (try_claim): Call bfd_plugin_close_file_descriptor. * plugin.h (bfd_plugin_close_file_descriptor): New. * bfd-in2.h: Regenerated. ld/ PR ld/28040 * plugin.c (plugin_input_file): Add ibfd. (release_plugin_file_descriptor): New function. (release_input_file): Call release_plugin_file_descriptor to close input->fd. (plugin_object_p): Call release_plugin_file_descriptor to close input->fd. Also call release_plugin_file_descriptor if not claimed. * testsuite/config/default.exp (RANLIB): New. * testsuite/ld-plugin/lto.exp: Run ranlib test.
Diffstat (limited to 'bfd/plugin.c')
-rw-r--r--bfd/plugin.c105
1 files changed, 74 insertions, 31 deletions
diff --git a/bfd/plugin.c b/bfd/plugin.c
index 1fee4d0..b3d6739 100644
--- a/bfd/plugin.c
+++ b/bfd/plugin.c
@@ -192,6 +192,7 @@ int
bfd_plugin_open_input (bfd *ibfd, struct ld_plugin_input_file *file)
{
bfd *iobfd;
+ int fd;
iobfd = ibfd;
while (iobfd->my_archive
@@ -202,50 +203,60 @@ bfd_plugin_open_input (bfd *ibfd, struct ld_plugin_input_file *file)
if (!iobfd->iostream && !bfd_open_file (iobfd))
return 0;
- /* The plugin API expects that the file descriptor won't be closed
- and reused as done by the bfd file cache. So open it again.
- dup isn't good enough. plugin IO uses lseek/read while BFD uses
- fseek/fread. It isn't wise to mix the unistd and stdio calls on
- the same underlying file descriptor. */
- file->fd = open (file->name, O_RDONLY | O_BINARY);
- if (file->fd < 0)
+ /* Reuse the archive plugin file descriptor. */
+ if (iobfd != ibfd)
+ fd = iobfd->archive_plugin_fd;
+ else
+ fd = -1;
+
+ if (fd < 0)
{
+ /* The plugin API expects that the file descriptor won't be closed
+ and reused as done by the bfd file cache. So open it again.
+ dup isn't good enough. plugin IO uses lseek/read while BFD uses
+ fseek/fread. It isn't wise to mix the unistd and stdio calls on
+ the same underlying file descriptor. */
+ fd = open (file->name, O_RDONLY | O_BINARY);
+ if (fd < 0)
+ {
#ifndef EMFILE
- return 0;
+ return 0;
#else
- if (errno != EMFILE)
- return 0;
+ if (errno != EMFILE)
+ return 0;
#ifdef HAVE_GETRLIMIT
- struct rlimit lim;
-
- /* Complicated links involving lots of files and/or large archives
- can exhaust the number of file descriptors available to us.
- If possible, try to allocate more descriptors. */
- if (getrlimit (RLIMIT_NOFILE, & lim) == 0
- && lim.rlim_cur < lim.rlim_max)
- {
- lim.rlim_cur = lim.rlim_max;
- if (setrlimit (RLIMIT_NOFILE, &lim) == 0)
- file->fd = open (file->name, O_RDONLY | O_BINARY);
- }
+ struct rlimit lim;
+
+ /* Complicated links involving lots of files and/or large
+ archives can exhaust the number of file descriptors
+ available to us. If possible, try to allocate more
+ descriptors. */
+ if (getrlimit (RLIMIT_NOFILE, & lim) == 0
+ && lim.rlim_cur < lim.rlim_max)
+ {
+ lim.rlim_cur = lim.rlim_max;
+ if (setrlimit (RLIMIT_NOFILE, &lim) == 0)
+ fd = open (file->name, O_RDONLY | O_BINARY);
+ }
- if (file->fd < 0)
+ if (fd < 0)
#endif
- {
- _bfd_error_handler (_("plugin framework: out of file descriptors. Try using fewer objects/archives\n"));
- return 0;
- }
+ {
+ _bfd_error_handler (_("plugin framework: out of file descriptors. Try using fewer objects/archives\n"));
+ return 0;
+ }
#endif
- }
+ }
+ }
if (iobfd == ibfd)
{
struct stat stat_buf;
- if (fstat (file->fd, &stat_buf))
+ if (fstat (fd, &stat_buf))
{
- close(file->fd);
+ close (fd);
return 0;
}
@@ -254,12 +265,44 @@ bfd_plugin_open_input (bfd *ibfd, struct ld_plugin_input_file *file)
}
else
{
+ /* Cache the archive plugin file descriptor. */
+ iobfd->archive_plugin_fd = fd;
+ iobfd->archive_plugin_fd_open_count++;
+
file->offset = ibfd->origin;
file->filesize = arelt_size (ibfd);
}
+
+ file->fd = fd;
return 1;
}
+/* Close the plugin file descriptor. */
+
+void
+bfd_plugin_close_file_descriptor (bfd *abfd, int fd)
+{
+ bfd *iobfd;
+
+ iobfd = abfd;
+ while (iobfd->my_archive
+ && !bfd_is_thin_archive (iobfd->my_archive))
+ iobfd = iobfd->my_archive;
+ if (iobfd == abfd)
+ close (fd);
+ else
+ {
+ iobfd->archive_plugin_fd_open_count--;
+ /* Dup the archive plugin file descriptor for later use, which
+ will be closed by _bfd_archive_close_and_cleanup. */
+ if (iobfd->archive_plugin_fd_open_count == 0)
+ {
+ iobfd->archive_plugin_fd = dup (fd);
+ close (fd);
+ }
+ }
+}
+
static int
try_claim (bfd *abfd)
{
@@ -271,7 +314,7 @@ try_claim (bfd *abfd)
&& current_plugin->claim_file)
{
current_plugin->claim_file (&file, &claimed);
- close (file.fd);
+ bfd_plugin_close_file_descriptor (abfd, file.fd);
}
return claimed;