1 From 1c611b40e6bfc8029bff7696814330b5bc0ee5c0 Mon Sep 17 00:00:00 2001
2 From: "H.J. Lu" <hjl.tools@gmail.com>
3 Date: Mon, 26 Jul 2021 05:59:55 -0700
4 Subject: [PATCH] bfd: Close the file descriptor if there is no archive fd
6 Close the file descriptor if there is no archive plugin file descriptor
7 to avoid running out of file descriptors on thin archives with many
13 * plugin.c (bfd_plugin_close_file_descriptor): Close the file
14 descriptor there is no archive plugin file descriptor.
19 * testsuite/ld-plugin/lto.exp: Run tmpdir/pr28138 only for
23 * testsuite/ld-plugin/lto.exp: Run ld/28138 tests.
24 * testsuite/ld-plugin/pr28138.c: New file.
25 * testsuite/ld-plugin/pr28138-1.c: Likewise.
26 * testsuite/ld-plugin/pr28138-2.c: Likewise.
27 * testsuite/ld-plugin/pr28138-3.c: Likewise.
28 * testsuite/ld-plugin/pr28138-4.c: Likewise.
29 * testsuite/ld-plugin/pr28138-5.c: Likewise.
30 * testsuite/ld-plugin/pr28138-6.c: Likewise.
31 * testsuite/ld-plugin/pr28138-7.c: Likewise.
33 (cherry picked from commit 5a98fb7513b559e20dfebdbaa2a471afda3b4742)
34 (cherry picked from commit 7dc37e1e1209c80e0bab784df6b6bac335e836f2)
36 Upstream-Status: Accepted [https://sourceware.org/git/?p=binutils-gdb.git;a=commit;h=1c611b40e6bfc8029bff7696814330b5bc0ee5c0]
38 bfd/plugin.c | 8 +++++++
39 ld/testsuite/ld-plugin/lto.exp | 34 ++++++++++++++++++++++++++++++
40 ld/testsuite/ld-plugin/pr28138-1.c | 6 ++++++
41 ld/testsuite/ld-plugin/pr28138-2.c | 6 ++++++
42 ld/testsuite/ld-plugin/pr28138-3.c | 6 ++++++
43 ld/testsuite/ld-plugin/pr28138-4.c | 6 ++++++
44 ld/testsuite/ld-plugin/pr28138-5.c | 6 ++++++
45 ld/testsuite/ld-plugin/pr28138-6.c | 6 ++++++
46 ld/testsuite/ld-plugin/pr28138-7.c | 6 ++++++
47 ld/testsuite/ld-plugin/pr28138.c | 20 ++++++++++++++++++
48 10 files changed, 104 insertions(+)
49 create mode 100644 ld/testsuite/ld-plugin/pr28138-1.c
50 create mode 100644 ld/testsuite/ld-plugin/pr28138-2.c
51 create mode 100644 ld/testsuite/ld-plugin/pr28138-3.c
52 create mode 100644 ld/testsuite/ld-plugin/pr28138-4.c
53 create mode 100644 ld/testsuite/ld-plugin/pr28138-5.c
54 create mode 100644 ld/testsuite/ld-plugin/pr28138-6.c
55 create mode 100644 ld/testsuite/ld-plugin/pr28138-7.c
56 create mode 100644 ld/testsuite/ld-plugin/pr28138.c
58 diff --git a/bfd/plugin.c b/bfd/plugin.c
59 index 6cfa2b66470..3bab8febe88 100644
62 @@ -291,6 +291,14 @@ bfd_plugin_close_file_descriptor (bfd *abfd, int fd)
63 && !bfd_is_thin_archive (abfd->my_archive))
64 abfd = abfd->my_archive;
66 + /* Close the file descriptor if there is no archive plugin file
68 + if (abfd->archive_plugin_fd == -1)
74 abfd->archive_plugin_fd_open_count--;
75 /* Dup the archive plugin file descriptor for later use, which
76 will be closed by _bfd_archive_close_and_cleanup. */
77 diff --git a/ld/testsuite/ld-plugin/lto.exp b/ld/testsuite/ld-plugin/lto.exp
78 index def69e43ab3..999d911ce6a 100644
79 --- a/ld/testsuite/ld-plugin/lto.exp
80 +++ b/ld/testsuite/ld-plugin/lto.exp
81 @@ -687,6 +687,40 @@ if { [is_elf_format] && [check_lto_shared_available] } {
85 +run_cc_link_tests [list \
89 + {pr28138-1.c pr28138-2.c pr28138-3.c pr28138-4.c pr28138-5.c \
90 + pr28138-6.c pr28138-7.c} {} "pr28138.a" \
99 +set exec_output [run_host_cmd "sh" \
100 + "-c \"ulimit -n 20; \
101 + $CC -Btmpdir/ld -o tmpdir/pr28138 \
102 + tmpdir/pr28138.o tmpdir/pr28138.a\""]
103 +set exec_output [prune_warnings $exec_output]
104 +if [string match "" $exec_output] then {
105 + if { [isnative] } {
106 + set exec_output [run_host_cmd "tmpdir/pr28138" ""]
107 + if [string match "PASS" $exec_output] then {
119 set testname "Build liblto-11.a"
120 remote_file host delete "tmpdir/liblto-11.a"
121 set catch_output [run_host_cmd "$ar" "rc $plug_opt tmpdir/liblto-11.a tmpdir/lto-11a.o tmpdir/lto-11b.o tmpdir/lto-11c.o"]
122 diff --git a/ld/testsuite/ld-plugin/pr28138-1.c b/ld/testsuite/ld-plugin/pr28138-1.c
124 index 00000000000..51d119e1642
126 +++ b/ld/testsuite/ld-plugin/pr28138-1.c
128 +extern int a0(void);
134 diff --git a/ld/testsuite/ld-plugin/pr28138-2.c b/ld/testsuite/ld-plugin/pr28138-2.c
136 index 00000000000..1120cd797e9
138 +++ b/ld/testsuite/ld-plugin/pr28138-2.c
140 +extern int a1(void);
146 diff --git a/ld/testsuite/ld-plugin/pr28138-3.c b/ld/testsuite/ld-plugin/pr28138-3.c
148 index 00000000000..ec464947ee6
150 +++ b/ld/testsuite/ld-plugin/pr28138-3.c
152 +extern int a2(void);
158 diff --git a/ld/testsuite/ld-plugin/pr28138-4.c b/ld/testsuite/ld-plugin/pr28138-4.c
160 index 00000000000..475701b2c5c
162 +++ b/ld/testsuite/ld-plugin/pr28138-4.c
164 +extern int a3(void);
170 diff --git a/ld/testsuite/ld-plugin/pr28138-5.c b/ld/testsuite/ld-plugin/pr28138-5.c
172 index 00000000000..e24f86c363e
174 +++ b/ld/testsuite/ld-plugin/pr28138-5.c
176 +extern int a4(void);
182 diff --git a/ld/testsuite/ld-plugin/pr28138-6.c b/ld/testsuite/ld-plugin/pr28138-6.c
184 index 00000000000..b5b938bdb21
186 +++ b/ld/testsuite/ld-plugin/pr28138-6.c
188 +extern int a5(void);
194 diff --git a/ld/testsuite/ld-plugin/pr28138-7.c b/ld/testsuite/ld-plugin/pr28138-7.c
196 index 00000000000..4ef75bf0f0c
198 +++ b/ld/testsuite/ld-plugin/pr28138-7.c
200 +extern int a6(void);
206 diff --git a/ld/testsuite/ld-plugin/pr28138.c b/ld/testsuite/ld-plugin/pr28138.c
208 index 00000000000..68252c9f382
210 +++ b/ld/testsuite/ld-plugin/pr28138.c
214 +extern int a7(void);