blob: 924980b4e834f7bd19860b2add1974958050d762 [file] [log] [blame]
Andrew Geisslerd159c7f2021-09-02 21:05:58 -05001From 1c611b40e6bfc8029bff7696814330b5bc0ee5c0 Mon Sep 17 00:00:00 2001
2From: "H.J. Lu" <hjl.tools@gmail.com>
3Date: Mon, 26 Jul 2021 05:59:55 -0700
4Subject: [PATCH] bfd: Close the file descriptor if there is no archive fd
5
6Close the file descriptor if there is no archive plugin file descriptor
7to avoid running out of file descriptors on thin archives with many
8archive members.
9
10bfd/
11
12 PR ld/28138
13 * plugin.c (bfd_plugin_close_file_descriptor): Close the file
14 descriptor there is no archive plugin file descriptor.
15
16ld/
17
18 PR ld/28138
19 * testsuite/ld-plugin/lto.exp: Run tmpdir/pr28138 only for
20 native build.
21
22 PR ld/28138
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.
32
33(cherry picked from commit 5a98fb7513b559e20dfebdbaa2a471afda3b4742)
34(cherry picked from commit 7dc37e1e1209c80e0bab784df6b6bac335e836f2)
35
Andrew Geissler595f6302022-01-24 19:11:47 +000036Upstream-Status: Accepted [https://sourceware.org/git/?p=binutils-gdb.git;a=commit;h=1c611b40e6bfc8029bff7696814330b5bc0ee5c0]
Andrew Geisslerd159c7f2021-09-02 21:05:58 -050037---
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
57
58diff --git a/bfd/plugin.c b/bfd/plugin.c
59index 6cfa2b66470..3bab8febe88 100644
60--- a/bfd/plugin.c
61+++ b/bfd/plugin.c
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;
65
66+ /* Close the file descriptor if there is no archive plugin file
67+ descriptor. */
68+ if (abfd->archive_plugin_fd == -1)
69+ {
70+ close (fd);
71+ return;
72+ }
73+
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. */
77diff --git a/ld/testsuite/ld-plugin/lto.exp b/ld/testsuite/ld-plugin/lto.exp
78index 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] } {
82 }
83 }
84
85+run_cc_link_tests [list \
86+ [list \
87+ "Build pr28138.a" \
88+ "-T" "" \
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" \
91+ ] \
92+ [list \
93+ "Build pr28138.o" \
94+ "" "" \
95+ {pr28138.c} {} \
96+ ] \
97+]
98+
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 {
108+ pass "PR ld/28138"
109+ } else {
110+ fail "PR ld/28138"
111+ }
112+ } else {
113+ pass "PR ld/28138"
114+ }
115+} else {
116+ fail "PR ld/28138"
117+}
118+
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"]
122diff --git a/ld/testsuite/ld-plugin/pr28138-1.c b/ld/testsuite/ld-plugin/pr28138-1.c
123new file mode 100644
124index 00000000000..51d119e1642
125--- /dev/null
126+++ b/ld/testsuite/ld-plugin/pr28138-1.c
127@@ -0,0 +1,6 @@
128+extern int a0(void);
129+int
130+a1(void)
131+{
132+ return 1 + a0();
133+}
134diff --git a/ld/testsuite/ld-plugin/pr28138-2.c b/ld/testsuite/ld-plugin/pr28138-2.c
135new file mode 100644
136index 00000000000..1120cd797e9
137--- /dev/null
138+++ b/ld/testsuite/ld-plugin/pr28138-2.c
139@@ -0,0 +1,6 @@
140+extern int a1(void);
141+int
142+a2(void)
143+{
144+ return 1 + a1();
145+}
146diff --git a/ld/testsuite/ld-plugin/pr28138-3.c b/ld/testsuite/ld-plugin/pr28138-3.c
147new file mode 100644
148index 00000000000..ec464947ee6
149--- /dev/null
150+++ b/ld/testsuite/ld-plugin/pr28138-3.c
151@@ -0,0 +1,6 @@
152+extern int a2(void);
153+int
154+a3(void)
155+{
156+ return 1 + a2();
157+}
158diff --git a/ld/testsuite/ld-plugin/pr28138-4.c b/ld/testsuite/ld-plugin/pr28138-4.c
159new file mode 100644
160index 00000000000..475701b2c5c
161--- /dev/null
162+++ b/ld/testsuite/ld-plugin/pr28138-4.c
163@@ -0,0 +1,6 @@
164+extern int a3(void);
165+int
166+a4(void)
167+{
168+ return 1 + a3();
169+}
170diff --git a/ld/testsuite/ld-plugin/pr28138-5.c b/ld/testsuite/ld-plugin/pr28138-5.c
171new file mode 100644
172index 00000000000..e24f86c363e
173--- /dev/null
174+++ b/ld/testsuite/ld-plugin/pr28138-5.c
175@@ -0,0 +1,6 @@
176+extern int a4(void);
177+int
178+a5(void)
179+{
180+ return 1 + a4();
181+}
182diff --git a/ld/testsuite/ld-plugin/pr28138-6.c b/ld/testsuite/ld-plugin/pr28138-6.c
183new file mode 100644
184index 00000000000..b5b938bdb21
185--- /dev/null
186+++ b/ld/testsuite/ld-plugin/pr28138-6.c
187@@ -0,0 +1,6 @@
188+extern int a5(void);
189+int
190+a6(void)
191+{
192+ return 1 + a5();
193+}
194diff --git a/ld/testsuite/ld-plugin/pr28138-7.c b/ld/testsuite/ld-plugin/pr28138-7.c
195new file mode 100644
196index 00000000000..4ef75bf0f0c
197--- /dev/null
198+++ b/ld/testsuite/ld-plugin/pr28138-7.c
199@@ -0,0 +1,6 @@
200+extern int a6(void);
201+int
202+a7(void)
203+{
204+ return 1 + a6();
205+}
206diff --git a/ld/testsuite/ld-plugin/pr28138.c b/ld/testsuite/ld-plugin/pr28138.c
207new file mode 100644
208index 00000000000..68252c9f382
209--- /dev/null
210+++ b/ld/testsuite/ld-plugin/pr28138.c
211@@ -0,0 +1,20 @@
212+#include <stdio.h>
213+
214+extern int a7(void);
215+
216+int
217+a0(void)
218+{
219+ return 0;
220+}
221+
222+int
223+main()
224+{
225+ if (a7() == 7)
226+ {
227+ printf ("PASS\n");
228+ return 0;
229+ }
230+ return 1;
231+}
232--
2332.31.1
234