600-Close_the_file_descriptor.patch 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. From: H.J. Lu <[email protected]>
  2. Date: Mon, 26 Jul 2021 12:59:55 +0000 (-0700)
  3. Subject: bfd: Close the file descriptor if there is no archive fd
  4. X-Git-Url: https://sourceware.org/git/?p=binutils-gdb.git;a=commitdiff_plain;h=1c611b40e6bfc8029bff7696814330b5bc0ee5c0
  5. 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
  8. archive members.
  9. bfd/
  10. PR ld/28138
  11. * plugin.c (bfd_plugin_close_file_descriptor): Close the file
  12. descriptor there is no archive plugin file descriptor.
  13. ld/
  14. PR ld/28138
  15. * testsuite/ld-plugin/lto.exp: Run tmpdir/pr28138 only for
  16. native build.
  17. PR ld/28138
  18. * testsuite/ld-plugin/lto.exp: Run ld/28138 tests.
  19. * testsuite/ld-plugin/pr28138.c: New file.
  20. * testsuite/ld-plugin/pr28138-1.c: Likewise.
  21. * testsuite/ld-plugin/pr28138-2.c: Likewise.
  22. * testsuite/ld-plugin/pr28138-3.c: Likewise.
  23. * testsuite/ld-plugin/pr28138-4.c: Likewise.
  24. * testsuite/ld-plugin/pr28138-5.c: Likewise.
  25. * testsuite/ld-plugin/pr28138-6.c: Likewise.
  26. * testsuite/ld-plugin/pr28138-7.c: Likewise.
  27. (cherry picked from commit 5a98fb7513b559e20dfebdbaa2a471afda3b4742)
  28. (cherry picked from commit 7dc37e1e1209c80e0bab784df6b6bac335e836f2)
  29. ---
  30. --- a/bfd/plugin.c
  31. +++ b/bfd/plugin.c
  32. @@ -291,6 +291,14 @@ bfd_plugin_close_file_descriptor (bfd *a
  33. && !bfd_is_thin_archive (abfd->my_archive))
  34. abfd = abfd->my_archive;
  35. + /* Close the file descriptor if there is no archive plugin file
  36. + descriptor. */
  37. + if (abfd->archive_plugin_fd == -1)
  38. + {
  39. + close (fd);
  40. + return;
  41. + }
  42. +
  43. abfd->archive_plugin_fd_open_count--;
  44. /* Dup the archive plugin file descriptor for later use, which
  45. will be closed by _bfd_archive_close_and_cleanup. */
  46. --- a/ld/testsuite/ld-plugin/lto.exp
  47. +++ b/ld/testsuite/ld-plugin/lto.exp
  48. @@ -687,6 +687,40 @@ if { [is_elf_format] && [check_lto_share
  49. }
  50. }
  51. +run_cc_link_tests [list \
  52. + [list \
  53. + "Build pr28138.a" \
  54. + "-T" "" \
  55. + {pr28138-1.c pr28138-2.c pr28138-3.c pr28138-4.c pr28138-5.c \
  56. + pr28138-6.c pr28138-7.c} {} "pr28138.a" \
  57. + ] \
  58. + [list \
  59. + "Build pr28138.o" \
  60. + "" "" \
  61. + {pr28138.c} {} \
  62. + ] \
  63. +]
  64. +
  65. +set exec_output [run_host_cmd "sh" \
  66. + "-c \"ulimit -n 20; \
  67. + $CC -Btmpdir/ld -o tmpdir/pr28138 \
  68. + tmpdir/pr28138.o tmpdir/pr28138.a\""]
  69. +set exec_output [prune_warnings $exec_output]
  70. +if [string match "" $exec_output] then {
  71. + if { [isnative] } {
  72. + set exec_output [run_host_cmd "tmpdir/pr28138" ""]
  73. + if [string match "PASS" $exec_output] then {
  74. + pass "PR ld/28138"
  75. + } else {
  76. + fail "PR ld/28138"
  77. + }
  78. + } else {
  79. + pass "PR ld/28138"
  80. + }
  81. +} else {
  82. + fail "PR ld/28138"
  83. +}
  84. +
  85. set testname "Build liblto-11.a"
  86. remote_file host delete "tmpdir/liblto-11.a"
  87. 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"]
  88. --- /dev/null
  89. +++ b/ld/testsuite/ld-plugin/pr28138-1.c
  90. @@ -0,0 +1,6 @@
  91. +extern int a0(void);
  92. +int
  93. +a1(void)
  94. +{
  95. + return 1 + a0();
  96. +}
  97. --- /dev/null
  98. +++ b/ld/testsuite/ld-plugin/pr28138-2.c
  99. @@ -0,0 +1,6 @@
  100. +extern int a1(void);
  101. +int
  102. +a2(void)
  103. +{
  104. + return 1 + a1();
  105. +}
  106. --- /dev/null
  107. +++ b/ld/testsuite/ld-plugin/pr28138-3.c
  108. @@ -0,0 +1,6 @@
  109. +extern int a2(void);
  110. +int
  111. +a3(void)
  112. +{
  113. + return 1 + a2();
  114. +}
  115. --- /dev/null
  116. +++ b/ld/testsuite/ld-plugin/pr28138-4.c
  117. @@ -0,0 +1,6 @@
  118. +extern int a3(void);
  119. +int
  120. +a4(void)
  121. +{
  122. + return 1 + a3();
  123. +}
  124. --- /dev/null
  125. +++ b/ld/testsuite/ld-plugin/pr28138-5.c
  126. @@ -0,0 +1,6 @@
  127. +extern int a4(void);
  128. +int
  129. +a5(void)
  130. +{
  131. + return 1 + a4();
  132. +}
  133. --- /dev/null
  134. +++ b/ld/testsuite/ld-plugin/pr28138-6.c
  135. @@ -0,0 +1,6 @@
  136. +extern int a5(void);
  137. +int
  138. +a6(void)
  139. +{
  140. + return 1 + a5();
  141. +}
  142. --- /dev/null
  143. +++ b/ld/testsuite/ld-plugin/pr28138-7.c
  144. @@ -0,0 +1,6 @@
  145. +extern int a6(void);
  146. +int
  147. +a7(void)
  148. +{
  149. + return 1 + a6();
  150. +}
  151. --- /dev/null
  152. +++ b/ld/testsuite/ld-plugin/pr28138.c
  153. @@ -0,0 +1,20 @@
  154. +#include <stdio.h>
  155. +
  156. +extern int a7(void);
  157. +
  158. +int
  159. +a0(void)
  160. +{
  161. + return 0;
  162. +}
  163. +
  164. +int
  165. +main()
  166. +{
  167. + if (a7() == 7)
  168. + {
  169. + printf ("PASS\n");
  170. + return 0;
  171. + }
  172. + return 1;
  173. +}