0017-bfd-Close-the-file-descriptor-if-there-is-no-archive.patch 6.3 KB

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