cache.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  1. /* BFD library -- caching of file descriptors.
  2. Copyright (C) 1990-2022 Free Software Foundation, Inc.
  3. Hacked by Steve Chamberlain of Cygnus Support (steve@cygnus.com).
  4. This file is part of BFD, the Binary File Descriptor library.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  16. MA 02110-1301, USA. */
  17. /*
  18. SECTION
  19. File caching
  20. The file caching mechanism is embedded within BFD and allows
  21. the application to open as many BFDs as it wants without
  22. regard to the underlying operating system's file descriptor
  23. limit (often as low as 20 open files). The module in
  24. <<cache.c>> maintains a least recently used list of
  25. <<bfd_cache_max_open>> files, and exports the name
  26. <<bfd_cache_lookup>>, which runs around and makes sure that
  27. the required BFD is open. If not, then it chooses a file to
  28. close, closes it and opens the one wanted, returning its file
  29. handle.
  30. SUBSECTION
  31. Caching functions
  32. */
  33. #include "sysdep.h"
  34. #include "bfd.h"
  35. #include "libbfd.h"
  36. #include "libiberty.h"
  37. #ifdef HAVE_MMAP
  38. #include <sys/mman.h>
  39. #endif
  40. /* In some cases we can optimize cache operation when reopening files.
  41. For instance, a flush is entirely unnecessary if the file is already
  42. closed, so a flush would use CACHE_NO_OPEN. Similarly, a seek using
  43. SEEK_SET or SEEK_END need not first seek to the current position.
  44. For stat we ignore seek errors, just in case the file has changed
  45. while we weren't looking. If it has, then it's possible that the
  46. file is shorter and we don't want a seek error to prevent us doing
  47. the stat. */
  48. enum cache_flag {
  49. CACHE_NORMAL = 0,
  50. CACHE_NO_OPEN = 1,
  51. CACHE_NO_SEEK = 2,
  52. CACHE_NO_SEEK_ERROR = 4
  53. };
  54. /* The maximum number of files which the cache will keep open at
  55. one time. When needed call bfd_cache_max_open to initialize. */
  56. static int max_open_files = 0;
  57. /* Set max_open_files, if not already set, to 12.5% of the allowed open
  58. file descriptors, but at least 10, and return the value. */
  59. static int
  60. bfd_cache_max_open (void)
  61. {
  62. if (max_open_files == 0)
  63. {
  64. int max;
  65. #if defined(__sun) && !defined(__sparcv9) && !defined(__x86_64__)
  66. /* PR ld/19260: 32-bit Solaris has very inelegant handling of the 255
  67. file descriptor limit. The problem is that setrlimit(2) can raise
  68. RLIMIT_NOFILE to a value that is not supported by libc, resulting
  69. in "Too many open files" errors. This can happen here even though
  70. max_open_files is set to rlim.rlim_cur / 8. For example, if
  71. a parent process has set rlim.rlim_cur to 65536, then max_open_files
  72. will be computed as 8192.
  73. This check essentially reverts to the behavior from binutils 2.23.1
  74. for 32-bit Solaris only. (It is hoped that the 32-bit libc
  75. limitation will be removed soon). 64-bit Solaris libc does not have
  76. this limitation. */
  77. max = 16;
  78. #else
  79. #ifdef HAVE_GETRLIMIT
  80. struct rlimit rlim;
  81. if (getrlimit (RLIMIT_NOFILE, &rlim) == 0
  82. && rlim.rlim_cur != (rlim_t) RLIM_INFINITY)
  83. max = rlim.rlim_cur / 8;
  84. else
  85. #endif
  86. #ifdef _SC_OPEN_MAX
  87. max = sysconf (_SC_OPEN_MAX) / 8;
  88. #else
  89. max = 10;
  90. #endif
  91. #endif /* not 32-bit Solaris */
  92. max_open_files = max < 10 ? 10 : max;
  93. }
  94. return max_open_files;
  95. }
  96. /* The number of BFD files we have open. */
  97. static int open_files;
  98. /* Zero, or a pointer to the topmost BFD on the chain. This is
  99. used by the <<bfd_cache_lookup>> macro in @file{libbfd.h} to
  100. determine when it can avoid a function call. */
  101. static bfd *bfd_last_cache = NULL;
  102. /* Insert a BFD into the cache. */
  103. static void
  104. insert (bfd *abfd)
  105. {
  106. if (bfd_last_cache == NULL)
  107. {
  108. abfd->lru_next = abfd;
  109. abfd->lru_prev = abfd;
  110. }
  111. else
  112. {
  113. abfd->lru_next = bfd_last_cache;
  114. abfd->lru_prev = bfd_last_cache->lru_prev;
  115. abfd->lru_prev->lru_next = abfd;
  116. abfd->lru_next->lru_prev = abfd;
  117. }
  118. bfd_last_cache = abfd;
  119. }
  120. /* Remove a BFD from the cache. */
  121. static void
  122. snip (bfd *abfd)
  123. {
  124. abfd->lru_prev->lru_next = abfd->lru_next;
  125. abfd->lru_next->lru_prev = abfd->lru_prev;
  126. if (abfd == bfd_last_cache)
  127. {
  128. bfd_last_cache = abfd->lru_next;
  129. if (abfd == bfd_last_cache)
  130. bfd_last_cache = NULL;
  131. }
  132. }
  133. /* Close a BFD and remove it from the cache. */
  134. static bool
  135. bfd_cache_delete (bfd *abfd)
  136. {
  137. bool ret;
  138. if (fclose ((FILE *) abfd->iostream) == 0)
  139. ret = true;
  140. else
  141. {
  142. ret = false;
  143. bfd_set_error (bfd_error_system_call);
  144. }
  145. snip (abfd);
  146. abfd->iostream = NULL;
  147. --open_files;
  148. return ret;
  149. }
  150. /* We need to open a new file, and the cache is full. Find the least
  151. recently used cacheable BFD and close it. */
  152. static bool
  153. close_one (void)
  154. {
  155. register bfd *to_kill;
  156. if (bfd_last_cache == NULL)
  157. to_kill = NULL;
  158. else
  159. {
  160. for (to_kill = bfd_last_cache->lru_prev;
  161. ! to_kill->cacheable;
  162. to_kill = to_kill->lru_prev)
  163. {
  164. if (to_kill == bfd_last_cache)
  165. {
  166. to_kill = NULL;
  167. break;
  168. }
  169. }
  170. }
  171. if (to_kill == NULL)
  172. {
  173. /* There are no open cacheable BFD's. */
  174. return true;
  175. }
  176. to_kill->where = _bfd_real_ftell ((FILE *) to_kill->iostream);
  177. return bfd_cache_delete (to_kill);
  178. }
  179. /* Check to see if the required BFD is the same as the last one
  180. looked up. If so, then it can use the stream in the BFD with
  181. impunity, since it can't have changed since the last lookup;
  182. otherwise, it has to perform the complicated lookup function. */
  183. #define bfd_cache_lookup(x, flag) \
  184. ((x) == bfd_last_cache \
  185. ? (FILE *) (bfd_last_cache->iostream) \
  186. : bfd_cache_lookup_worker (x, flag))
  187. /* Called when the macro <<bfd_cache_lookup>> fails to find a
  188. quick answer. Find a file descriptor for @var{abfd}. If
  189. necessary, it open it. If there are already more than
  190. <<bfd_cache_max_open>> files open, it tries to close one first, to
  191. avoid running out of file descriptors. It will return NULL
  192. if it is unable to (re)open the @var{abfd}. */
  193. static FILE *
  194. bfd_cache_lookup_worker (bfd *abfd, enum cache_flag flag)
  195. {
  196. if ((abfd->flags & BFD_IN_MEMORY) != 0)
  197. abort ();
  198. if (abfd->my_archive != NULL
  199. && !bfd_is_thin_archive (abfd->my_archive))
  200. abort ();
  201. if (abfd->iostream != NULL)
  202. {
  203. /* Move the file to the start of the cache. */
  204. if (abfd != bfd_last_cache)
  205. {
  206. snip (abfd);
  207. insert (abfd);
  208. }
  209. return (FILE *) abfd->iostream;
  210. }
  211. if (flag & CACHE_NO_OPEN)
  212. return NULL;
  213. if (bfd_open_file (abfd) == NULL)
  214. ;
  215. else if (!(flag & CACHE_NO_SEEK)
  216. && _bfd_real_fseek ((FILE *) abfd->iostream,
  217. abfd->where, SEEK_SET) != 0
  218. && !(flag & CACHE_NO_SEEK_ERROR))
  219. bfd_set_error (bfd_error_system_call);
  220. else
  221. return (FILE *) abfd->iostream;
  222. /* xgettext:c-format */
  223. _bfd_error_handler (_("reopening %pB: %s\n"),
  224. abfd, bfd_errmsg (bfd_get_error ()));
  225. return NULL;
  226. }
  227. static file_ptr
  228. cache_btell (struct bfd *abfd)
  229. {
  230. FILE *f = bfd_cache_lookup (abfd, CACHE_NO_OPEN);
  231. if (f == NULL)
  232. return abfd->where;
  233. return _bfd_real_ftell (f);
  234. }
  235. static int
  236. cache_bseek (struct bfd *abfd, file_ptr offset, int whence)
  237. {
  238. FILE *f = bfd_cache_lookup (abfd, whence != SEEK_CUR ? CACHE_NO_SEEK : CACHE_NORMAL);
  239. if (f == NULL)
  240. return -1;
  241. return _bfd_real_fseek (f, offset, whence);
  242. }
  243. /* Note that archive entries don't have streams; they share their parent's.
  244. This allows someone to play with the iostream behind BFD's back.
  245. Also, note that the origin pointer points to the beginning of a file's
  246. contents (0 for non-archive elements). For archive entries this is the
  247. first octet in the file, NOT the beginning of the archive header. */
  248. static file_ptr
  249. cache_bread_1 (FILE *f, void *buf, file_ptr nbytes)
  250. {
  251. file_ptr nread;
  252. #if defined (__VAX) && defined (VMS)
  253. /* Apparently fread on Vax VMS does not keep the record length
  254. information. */
  255. nread = read (fileno (f), buf, nbytes);
  256. /* Set bfd_error if we did not read as much data as we expected. If
  257. the read failed due to an error set the bfd_error_system_call,
  258. else set bfd_error_file_truncated. */
  259. if (nread == (file_ptr)-1)
  260. {
  261. bfd_set_error (bfd_error_system_call);
  262. return nread;
  263. }
  264. #else
  265. nread = fread (buf, 1, nbytes, f);
  266. /* Set bfd_error if we did not read as much data as we expected. If
  267. the read failed due to an error set the bfd_error_system_call,
  268. else set bfd_error_file_truncated. */
  269. if (nread < nbytes && ferror (f))
  270. {
  271. bfd_set_error (bfd_error_system_call);
  272. return nread;
  273. }
  274. #endif
  275. if (nread < nbytes)
  276. /* This may or may not be an error, but in case the calling code
  277. bails out because of it, set the right error code. */
  278. bfd_set_error (bfd_error_file_truncated);
  279. return nread;
  280. }
  281. static file_ptr
  282. cache_bread (struct bfd *abfd, void *buf, file_ptr nbytes)
  283. {
  284. file_ptr nread = 0;
  285. FILE *f;
  286. f = bfd_cache_lookup (abfd, CACHE_NORMAL);
  287. if (f == NULL)
  288. return -1;
  289. /* Some filesystems are unable to handle reads that are too large
  290. (for instance, NetApp shares with oplocks turned off). To avoid
  291. hitting this limitation, we read the buffer in chunks of 8MB max. */
  292. while (nread < nbytes)
  293. {
  294. const file_ptr max_chunk_size = 0x800000;
  295. file_ptr chunk_size = nbytes - nread;
  296. file_ptr chunk_nread;
  297. if (chunk_size > max_chunk_size)
  298. chunk_size = max_chunk_size;
  299. chunk_nread = cache_bread_1 (f, (char *) buf + nread, chunk_size);
  300. /* Update the nread count.
  301. We just have to be careful of the case when cache_bread_1 returns
  302. a negative count: If this is our first read, then set nread to
  303. that negative count in order to return that negative value to the
  304. caller. Otherwise, don't add it to our total count, or we would
  305. end up returning a smaller number of bytes read than we actually
  306. did. */
  307. if (nread == 0 || chunk_nread > 0)
  308. nread += chunk_nread;
  309. if (chunk_nread < chunk_size)
  310. break;
  311. }
  312. return nread;
  313. }
  314. static file_ptr
  315. cache_bwrite (struct bfd *abfd, const void *from, file_ptr nbytes)
  316. {
  317. file_ptr nwrite;
  318. FILE *f = bfd_cache_lookup (abfd, CACHE_NORMAL);
  319. if (f == NULL)
  320. return 0;
  321. nwrite = fwrite (from, 1, nbytes, f);
  322. if (nwrite < nbytes && ferror (f))
  323. {
  324. bfd_set_error (bfd_error_system_call);
  325. return -1;
  326. }
  327. return nwrite;
  328. }
  329. static int
  330. cache_bclose (struct bfd *abfd)
  331. {
  332. return bfd_cache_close (abfd) - 1;
  333. }
  334. static int
  335. cache_bflush (struct bfd *abfd)
  336. {
  337. int sts;
  338. FILE *f = bfd_cache_lookup (abfd, CACHE_NO_OPEN);
  339. if (f == NULL)
  340. return 0;
  341. sts = fflush (f);
  342. if (sts < 0)
  343. bfd_set_error (bfd_error_system_call);
  344. return sts;
  345. }
  346. static int
  347. cache_bstat (struct bfd *abfd, struct stat *sb)
  348. {
  349. int sts;
  350. FILE *f = bfd_cache_lookup (abfd, CACHE_NO_SEEK_ERROR);
  351. if (f == NULL)
  352. return -1;
  353. sts = fstat (fileno (f), sb);
  354. if (sts < 0)
  355. bfd_set_error (bfd_error_system_call);
  356. return sts;
  357. }
  358. static void *
  359. cache_bmmap (struct bfd *abfd ATTRIBUTE_UNUSED,
  360. void *addr ATTRIBUTE_UNUSED,
  361. bfd_size_type len ATTRIBUTE_UNUSED,
  362. int prot ATTRIBUTE_UNUSED,
  363. int flags ATTRIBUTE_UNUSED,
  364. file_ptr offset ATTRIBUTE_UNUSED,
  365. void **map_addr ATTRIBUTE_UNUSED,
  366. bfd_size_type *map_len ATTRIBUTE_UNUSED)
  367. {
  368. void *ret = (void *) -1;
  369. if ((abfd->flags & BFD_IN_MEMORY) != 0)
  370. abort ();
  371. #ifdef HAVE_MMAP
  372. else
  373. {
  374. static uintptr_t pagesize_m1;
  375. FILE *f;
  376. file_ptr pg_offset;
  377. bfd_size_type pg_len;
  378. f = bfd_cache_lookup (abfd, CACHE_NO_SEEK_ERROR);
  379. if (f == NULL)
  380. return ret;
  381. if (pagesize_m1 == 0)
  382. pagesize_m1 = getpagesize () - 1;
  383. /* Align. */
  384. pg_offset = offset & ~pagesize_m1;
  385. pg_len = (len + (offset - pg_offset) + pagesize_m1) & ~pagesize_m1;
  386. ret = mmap (addr, pg_len, prot, flags, fileno (f), pg_offset);
  387. if (ret == (void *) -1)
  388. bfd_set_error (bfd_error_system_call);
  389. else
  390. {
  391. *map_addr = ret;
  392. *map_len = pg_len;
  393. ret = (char *) ret + (offset & pagesize_m1);
  394. }
  395. }
  396. #endif
  397. return ret;
  398. }
  399. static const struct bfd_iovec cache_iovec =
  400. {
  401. &cache_bread, &cache_bwrite, &cache_btell, &cache_bseek,
  402. &cache_bclose, &cache_bflush, &cache_bstat, &cache_bmmap
  403. };
  404. /*
  405. INTERNAL_FUNCTION
  406. bfd_cache_init
  407. SYNOPSIS
  408. bool bfd_cache_init (bfd *abfd);
  409. DESCRIPTION
  410. Add a newly opened BFD to the cache.
  411. */
  412. bool
  413. bfd_cache_init (bfd *abfd)
  414. {
  415. BFD_ASSERT (abfd->iostream != NULL);
  416. if (open_files >= bfd_cache_max_open ())
  417. {
  418. if (! close_one ())
  419. return false;
  420. }
  421. abfd->iovec = &cache_iovec;
  422. insert (abfd);
  423. ++open_files;
  424. return true;
  425. }
  426. /*
  427. INTERNAL_FUNCTION
  428. bfd_cache_close
  429. SYNOPSIS
  430. bool bfd_cache_close (bfd *abfd);
  431. DESCRIPTION
  432. Remove the BFD @var{abfd} from the cache. If the attached file is open,
  433. then close it too.
  434. RETURNS
  435. <<FALSE>> is returned if closing the file fails, <<TRUE>> is
  436. returned if all is well.
  437. */
  438. bool
  439. bfd_cache_close (bfd *abfd)
  440. {
  441. if (abfd->iovec != &cache_iovec)
  442. return true;
  443. if (abfd->iostream == NULL)
  444. /* Previously closed. */
  445. return true;
  446. return bfd_cache_delete (abfd);
  447. }
  448. /*
  449. FUNCTION
  450. bfd_cache_close_all
  451. SYNOPSIS
  452. bool bfd_cache_close_all (void);
  453. DESCRIPTION
  454. Remove all BFDs from the cache. If the attached file is open,
  455. then close it too.
  456. RETURNS
  457. <<FALSE>> is returned if closing one of the file fails, <<TRUE>> is
  458. returned if all is well.
  459. */
  460. bool
  461. bfd_cache_close_all (void)
  462. {
  463. bool ret = true;
  464. while (bfd_last_cache != NULL)
  465. ret &= bfd_cache_close (bfd_last_cache);
  466. return ret;
  467. }
  468. /*
  469. INTERNAL_FUNCTION
  470. bfd_open_file
  471. SYNOPSIS
  472. FILE* bfd_open_file (bfd *abfd);
  473. DESCRIPTION
  474. Call the OS to open a file for @var{abfd}. Return the <<FILE *>>
  475. (possibly <<NULL>>) that results from this operation. Set up the
  476. BFD so that future accesses know the file is open. If the <<FILE *>>
  477. returned is <<NULL>>, then it won't have been put in the
  478. cache, so it won't have to be removed from it.
  479. */
  480. FILE *
  481. bfd_open_file (bfd *abfd)
  482. {
  483. abfd->cacheable = true; /* Allow it to be closed later. */
  484. if (open_files >= bfd_cache_max_open ())
  485. {
  486. if (! close_one ())
  487. return NULL;
  488. }
  489. switch (abfd->direction)
  490. {
  491. case read_direction:
  492. case no_direction:
  493. abfd->iostream = _bfd_real_fopen (bfd_get_filename (abfd), FOPEN_RB);
  494. break;
  495. case both_direction:
  496. case write_direction:
  497. if (abfd->opened_once)
  498. {
  499. abfd->iostream = _bfd_real_fopen (bfd_get_filename (abfd),
  500. FOPEN_RUB);
  501. if (abfd->iostream == NULL)
  502. abfd->iostream = _bfd_real_fopen (bfd_get_filename (abfd),
  503. FOPEN_WUB);
  504. }
  505. else
  506. {
  507. /* Create the file.
  508. Some operating systems won't let us overwrite a running
  509. binary. For them, we want to unlink the file first.
  510. However, gcc 2.95 will create temporary files using
  511. O_EXCL and tight permissions to prevent other users from
  512. substituting other .o files during the compilation. gcc
  513. will then tell the assembler to use the newly created
  514. file as an output file. If we unlink the file here, we
  515. open a brief window when another user could still
  516. substitute a file.
  517. So we unlink the output file if and only if it has
  518. non-zero size. */
  519. #ifndef __MSDOS__
  520. /* Don't do this for MSDOS: it doesn't care about overwriting
  521. a running binary, but if this file is already open by
  522. another BFD, we will be in deep trouble if we delete an
  523. open file. In fact, objdump does just that if invoked with
  524. the --info option. */
  525. struct stat s;
  526. if (stat (bfd_get_filename (abfd), &s) == 0 && s.st_size != 0)
  527. unlink_if_ordinary (bfd_get_filename (abfd));
  528. #endif
  529. abfd->iostream = _bfd_real_fopen (bfd_get_filename (abfd),
  530. FOPEN_WUB);
  531. abfd->opened_once = true;
  532. }
  533. break;
  534. }
  535. if (abfd->iostream == NULL)
  536. bfd_set_error (bfd_error_system_call);
  537. else
  538. {
  539. if (! bfd_cache_init (abfd))
  540. return NULL;
  541. }
  542. return (FILE *) abfd->iostream;
  543. }