1From 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
36[Upstream:
37 https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=1c611b40e6bfc8029bff7696814330b5bc0ee5c0]
38Signed-off-by: Peter Seiderer <ps.report@gmx.net>
39---
40 bfd/plugin.c                       |  8 +++++++
41 ld/testsuite/ld-plugin/lto.exp     | 34 ++++++++++++++++++++++++++++++
42 ld/testsuite/ld-plugin/pr28138-1.c |  6 ++++++
43 ld/testsuite/ld-plugin/pr28138-2.c |  6 ++++++
44 ld/testsuite/ld-plugin/pr28138-3.c |  6 ++++++
45 ld/testsuite/ld-plugin/pr28138-4.c |  6 ++++++
46 ld/testsuite/ld-plugin/pr28138-5.c |  6 ++++++
47 ld/testsuite/ld-plugin/pr28138-6.c |  6 ++++++
48 ld/testsuite/ld-plugin/pr28138-7.c |  6 ++++++
49 ld/testsuite/ld-plugin/pr28138.c   | 20 ++++++++++++++++++
50 10 files changed, 104 insertions(+)
51 create mode 100644 ld/testsuite/ld-plugin/pr28138-1.c
52 create mode 100644 ld/testsuite/ld-plugin/pr28138-2.c
53 create mode 100644 ld/testsuite/ld-plugin/pr28138-3.c
54 create mode 100644 ld/testsuite/ld-plugin/pr28138-4.c
55 create mode 100644 ld/testsuite/ld-plugin/pr28138-5.c
56 create mode 100644 ld/testsuite/ld-plugin/pr28138-6.c
57 create mode 100644 ld/testsuite/ld-plugin/pr28138-7.c
58 create mode 100644 ld/testsuite/ld-plugin/pr28138.c
59
60diff --git a/bfd/plugin.c b/bfd/plugin.c
61index 6cfa2b66470..3bab8febe88 100644
62--- a/bfd/plugin.c
63+++ b/bfd/plugin.c
64@@ -291,6 +291,14 @@ bfd_plugin_close_file_descriptor (bfd *abfd, int fd)
65 	     && !bfd_is_thin_archive (abfd->my_archive))
66 	abfd = abfd->my_archive;
67
68+      /* Close the file descriptor if there is no archive plugin file
69+	 descriptor.  */
70+      if (abfd->archive_plugin_fd == -1)
71+	{
72+	  close (fd);
73+	  return;
74+	}
75+
76       abfd->archive_plugin_fd_open_count--;
77       /* Dup the archive plugin file descriptor for later use, which
78 	 will be closed by _bfd_archive_close_and_cleanup.  */
79diff --git a/ld/testsuite/ld-plugin/lto.exp b/ld/testsuite/ld-plugin/lto.exp
80index def69e43ab3..999d911ce6a 100644
81--- a/ld/testsuite/ld-plugin/lto.exp
82+++ b/ld/testsuite/ld-plugin/lto.exp
83@@ -687,6 +687,40 @@ if { [is_elf_format] && [check_lto_shared_available] } {
84     }
85 }
86
87+run_cc_link_tests [list \
88+    [list \
89+	"Build pr28138.a" \
90+	"-T" "" \
91+	{pr28138-1.c pr28138-2.c pr28138-3.c pr28138-4.c pr28138-5.c \
92+	 pr28138-6.c pr28138-7.c} {} "pr28138.a" \
93+    ] \
94+    [list \
95+	"Build pr28138.o" \
96+	"" "" \
97+	{pr28138.c} {} \
98+    ] \
99+]
100+
101+set exec_output [run_host_cmd "sh" \
102+			      "-c \"ulimit -n 20; \
103+			      $CC -Btmpdir/ld -o tmpdir/pr28138 \
104+			      tmpdir/pr28138.o tmpdir/pr28138.a\""]
105+set exec_output [prune_warnings $exec_output]
106+if [string match "" $exec_output] then {
107+    if { [isnative] } {
108+	set exec_output [run_host_cmd "tmpdir/pr28138" ""]
109+	if [string match "PASS" $exec_output] then {
110+	    pass "PR ld/28138"
111+	} else {
112+	    fail "PR ld/28138"
113+	}
114+    } else {
115+	pass "PR ld/28138"
116+    }
117+} else {
118+    fail "PR ld/28138"
119+}
120+
121 set testname "Build liblto-11.a"
122 remote_file host delete "tmpdir/liblto-11.a"
123 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"]
124diff --git a/ld/testsuite/ld-plugin/pr28138-1.c b/ld/testsuite/ld-plugin/pr28138-1.c
125new file mode 100644
126index 00000000000..51d119e1642
127--- /dev/null
128+++ b/ld/testsuite/ld-plugin/pr28138-1.c
129@@ -0,0 +1,6 @@
130+extern int a0(void);
131+int
132+a1(void)
133+{
134+  return 1 + a0();
135+}
136diff --git a/ld/testsuite/ld-plugin/pr28138-2.c b/ld/testsuite/ld-plugin/pr28138-2.c
137new file mode 100644
138index 00000000000..1120cd797e9
139--- /dev/null
140+++ b/ld/testsuite/ld-plugin/pr28138-2.c
141@@ -0,0 +1,6 @@
142+extern int a1(void);
143+int
144+a2(void)
145+{
146+  return 1 + a1();
147+}
148diff --git a/ld/testsuite/ld-plugin/pr28138-3.c b/ld/testsuite/ld-plugin/pr28138-3.c
149new file mode 100644
150index 00000000000..ec464947ee6
151--- /dev/null
152+++ b/ld/testsuite/ld-plugin/pr28138-3.c
153@@ -0,0 +1,6 @@
154+extern int a2(void);
155+int
156+a3(void)
157+{
158+  return 1 + a2();
159+}
160diff --git a/ld/testsuite/ld-plugin/pr28138-4.c b/ld/testsuite/ld-plugin/pr28138-4.c
161new file mode 100644
162index 00000000000..475701b2c5c
163--- /dev/null
164+++ b/ld/testsuite/ld-plugin/pr28138-4.c
165@@ -0,0 +1,6 @@
166+extern int a3(void);
167+int
168+a4(void)
169+{
170+  return 1 + a3();
171+}
172diff --git a/ld/testsuite/ld-plugin/pr28138-5.c b/ld/testsuite/ld-plugin/pr28138-5.c
173new file mode 100644
174index 00000000000..e24f86c363e
175--- /dev/null
176+++ b/ld/testsuite/ld-plugin/pr28138-5.c
177@@ -0,0 +1,6 @@
178+extern int a4(void);
179+int
180+a5(void)
181+{
182+  return 1 + a4();
183+}
184diff --git a/ld/testsuite/ld-plugin/pr28138-6.c b/ld/testsuite/ld-plugin/pr28138-6.c
185new file mode 100644
186index 00000000000..b5b938bdb21
187--- /dev/null
188+++ b/ld/testsuite/ld-plugin/pr28138-6.c
189@@ -0,0 +1,6 @@
190+extern int a5(void);
191+int
192+a6(void)
193+{
194+  return 1 + a5();
195+}
196diff --git a/ld/testsuite/ld-plugin/pr28138-7.c b/ld/testsuite/ld-plugin/pr28138-7.c
197new file mode 100644
198index 00000000000..4ef75bf0f0c
199--- /dev/null
200+++ b/ld/testsuite/ld-plugin/pr28138-7.c
201@@ -0,0 +1,6 @@
202+extern int a6(void);
203+int
204+a7(void)
205+{
206+  return 1 + a6();
207+}
208diff --git a/ld/testsuite/ld-plugin/pr28138.c b/ld/testsuite/ld-plugin/pr28138.c
209new file mode 100644
210index 00000000000..68252c9f382
211--- /dev/null
212+++ b/ld/testsuite/ld-plugin/pr28138.c
213@@ -0,0 +1,20 @@
214+#include <stdio.h>
215+
216+extern int a7(void);
217+
218+int
219+a0(void)
220+{
221+  return 0;
222+}
223+
224+int
225+main()
226+{
227+  if (a7() == 7)
228+    {
229+      printf ("PASS\n");
230+      return 0;
231+    }
232+  return 1;
233+}
234--
2352.34.1
236
237