gdb_bfd.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160
  1. /* Definitions for BFD wrappers used by GDB.
  2. Copyright (C) 2011-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #include "defs.h"
  15. #include "gdb_bfd.h"
  16. #include "ui-out.h"
  17. #include "gdbcmd.h"
  18. #include "hashtab.h"
  19. #include "gdbsupport/filestuff.h"
  20. #ifdef HAVE_MMAP
  21. #include <sys/mman.h>
  22. #ifndef MAP_FAILED
  23. #define MAP_FAILED ((void *) -1)
  24. #endif
  25. #endif
  26. #include "target.h"
  27. #include "gdb/fileio.h"
  28. #include "inferior.h"
  29. #include "cli/cli-style.h"
  30. /* An object of this type is stored in the section's user data when
  31. mapping a section. */
  32. struct gdb_bfd_section_data
  33. {
  34. /* Size of the data. */
  35. bfd_size_type size;
  36. /* If the data was mmapped, this is the length of the map. */
  37. bfd_size_type map_len;
  38. /* The data. If NULL, the section data has not been read. */
  39. void *data;
  40. /* If the data was mmapped, this is the map address. */
  41. void *map_addr;
  42. };
  43. /* A hash table holding every BFD that gdb knows about. This is not
  44. to be confused with 'gdb_bfd_cache', which is used for sharing
  45. BFDs; in contrast, this hash is used just to implement
  46. "maint info bfd". */
  47. static htab_t all_bfds;
  48. /* An object of this type is stored in each BFD's user data. */
  49. struct gdb_bfd_data
  50. {
  51. /* Note that if ST is nullptr, then we simply fill in zeroes. */
  52. gdb_bfd_data (bfd *abfd, struct stat *st)
  53. : mtime (st == nullptr ? 0 : st->st_mtime),
  54. size (st == nullptr ? 0 : st->st_size),
  55. inode (st == nullptr ? 0 : st->st_ino),
  56. device_id (st == nullptr ? 0 : st->st_dev),
  57. relocation_computed (0),
  58. needs_relocations (0),
  59. crc_computed (0)
  60. {
  61. }
  62. ~gdb_bfd_data ()
  63. {
  64. }
  65. /* The reference count. */
  66. int refc = 1;
  67. /* The mtime of the BFD at the point the cache entry was made. */
  68. time_t mtime;
  69. /* The file size (in bytes) at the point the cache entry was made. */
  70. off_t size;
  71. /* The inode of the file at the point the cache entry was made. */
  72. ino_t inode;
  73. /* The device id of the file at the point the cache entry was made. */
  74. dev_t device_id;
  75. /* This is true if we have determined whether this BFD has any
  76. sections requiring relocation. */
  77. unsigned int relocation_computed : 1;
  78. /* This is true if any section needs relocation. */
  79. unsigned int needs_relocations : 1;
  80. /* This is true if we have successfully computed the file's CRC. */
  81. unsigned int crc_computed : 1;
  82. /* The file's CRC. */
  83. unsigned long crc = 0;
  84. /* If the BFD comes from an archive, this points to the archive's
  85. BFD. Otherwise, this is NULL. */
  86. bfd *archive_bfd = nullptr;
  87. /* Table of all the bfds this bfd has included. */
  88. std::vector<gdb_bfd_ref_ptr> included_bfds;
  89. /* The registry. */
  90. REGISTRY_FIELDS = {};
  91. };
  92. #define GDB_BFD_DATA_ACCESSOR(ABFD) \
  93. ((struct gdb_bfd_data *) bfd_usrdata (ABFD))
  94. DEFINE_REGISTRY (bfd, GDB_BFD_DATA_ACCESSOR)
  95. /* A hash table storing all the BFDs maintained in the cache. */
  96. static htab_t gdb_bfd_cache;
  97. /* When true gdb will reuse an existing bfd object if the filename,
  98. modification time, and file size all match. */
  99. static bool bfd_sharing = true;
  100. static void
  101. show_bfd_sharing (struct ui_file *file, int from_tty,
  102. struct cmd_list_element *c, const char *value)
  103. {
  104. gdb_printf (file, _("BFD sharing is %s.\n"), value);
  105. }
  106. /* When true debugging of the bfd caches is enabled. */
  107. static bool debug_bfd_cache;
  108. /* Print an "bfd-cache" debug statement. */
  109. #define bfd_cache_debug_printf(fmt, ...) \
  110. debug_prefixed_printf_cond (debug_bfd_cache, "bfd-cache", fmt, ##__VA_ARGS__)
  111. static void
  112. show_bfd_cache_debug (struct ui_file *file, int from_tty,
  113. struct cmd_list_element *c, const char *value)
  114. {
  115. gdb_printf (file, _("BFD cache debugging is %s.\n"), value);
  116. }
  117. /* The type of an object being looked up in gdb_bfd_cache. We use
  118. htab's capability of storing one kind of object (BFD in this case)
  119. and using a different sort of object for searching. */
  120. struct gdb_bfd_cache_search
  121. {
  122. /* The filename. */
  123. const char *filename;
  124. /* The mtime. */
  125. time_t mtime;
  126. /* The file size (in bytes). */
  127. off_t size;
  128. /* The inode of the file. */
  129. ino_t inode;
  130. /* The device id of the file. */
  131. dev_t device_id;
  132. };
  133. /* A hash function for BFDs. */
  134. static hashval_t
  135. hash_bfd (const void *b)
  136. {
  137. const bfd *abfd = (const struct bfd *) b;
  138. /* It is simplest to just hash the filename. */
  139. return htab_hash_string (bfd_get_filename (abfd));
  140. }
  141. /* An equality function for BFDs. Note that this expects the caller
  142. to search using struct gdb_bfd_cache_search only, not BFDs. */
  143. static int
  144. eq_bfd (const void *a, const void *b)
  145. {
  146. const bfd *abfd = (const struct bfd *) a;
  147. const struct gdb_bfd_cache_search *s
  148. = (const struct gdb_bfd_cache_search *) b;
  149. struct gdb_bfd_data *gdata = (struct gdb_bfd_data *) bfd_usrdata (abfd);
  150. return (gdata->mtime == s->mtime
  151. && gdata->size == s->size
  152. && gdata->inode == s->inode
  153. && gdata->device_id == s->device_id
  154. && strcmp (bfd_get_filename (abfd), s->filename) == 0);
  155. }
  156. /* See gdb_bfd.h. */
  157. int
  158. is_target_filename (const char *name)
  159. {
  160. return startswith (name, TARGET_SYSROOT_PREFIX);
  161. }
  162. /* See gdb_bfd.h. */
  163. int
  164. gdb_bfd_has_target_filename (struct bfd *abfd)
  165. {
  166. return is_target_filename (bfd_get_filename (abfd));
  167. }
  168. /* For `gdb_bfd_open_from_target_memory`. */
  169. struct target_buffer
  170. {
  171. CORE_ADDR base;
  172. ULONGEST size;
  173. };
  174. /* For `gdb_bfd_open_from_target_memory`. Opening the file is a no-op. */
  175. static void *
  176. mem_bfd_iovec_open (struct bfd *abfd, void *open_closure)
  177. {
  178. return open_closure;
  179. }
  180. /* For `gdb_bfd_open_from_target_memory`. Closing the file is just freeing the
  181. base/size pair on our side. */
  182. static int
  183. mem_bfd_iovec_close (struct bfd *abfd, void *stream)
  184. {
  185. xfree (stream);
  186. /* Zero means success. */
  187. return 0;
  188. }
  189. /* For `gdb_bfd_open_from_target_memory`. For reading the file, we just need to
  190. pass through to target_read_memory and fix up the arguments and return
  191. values. */
  192. static file_ptr
  193. mem_bfd_iovec_pread (struct bfd *abfd, void *stream, void *buf,
  194. file_ptr nbytes, file_ptr offset)
  195. {
  196. int err;
  197. struct target_buffer *buffer = (struct target_buffer *) stream;
  198. /* If this read will read all of the file, limit it to just the rest. */
  199. if (offset + nbytes > buffer->size)
  200. nbytes = buffer->size - offset;
  201. /* If there are no more bytes left, we've reached EOF. */
  202. if (nbytes == 0)
  203. return 0;
  204. err = target_read_memory (buffer->base + offset, (gdb_byte *) buf, nbytes);
  205. if (err)
  206. return -1;
  207. return nbytes;
  208. }
  209. /* For `gdb_bfd_open_from_target_memory`. For statting the file, we only
  210. support the st_size attribute. */
  211. static int
  212. mem_bfd_iovec_stat (struct bfd *abfd, void *stream, struct stat *sb)
  213. {
  214. struct target_buffer *buffer = (struct target_buffer*) stream;
  215. memset (sb, 0, sizeof (struct stat));
  216. sb->st_size = buffer->size;
  217. return 0;
  218. }
  219. /* See gdb_bfd.h. */
  220. gdb_bfd_ref_ptr
  221. gdb_bfd_open_from_target_memory (CORE_ADDR addr, ULONGEST size,
  222. const char *target,
  223. const char *filename)
  224. {
  225. struct target_buffer *buffer = XNEW (struct target_buffer);
  226. buffer->base = addr;
  227. buffer->size = size;
  228. return gdb_bfd_openr_iovec (filename ? filename : "<in-memory>", target,
  229. mem_bfd_iovec_open,
  230. buffer,
  231. mem_bfd_iovec_pread,
  232. mem_bfd_iovec_close,
  233. mem_bfd_iovec_stat);
  234. }
  235. /* Return the system error number corresponding to ERRNUM. */
  236. static int
  237. fileio_errno_to_host (int errnum)
  238. {
  239. switch (errnum)
  240. {
  241. case FILEIO_EPERM:
  242. return EPERM;
  243. case FILEIO_ENOENT:
  244. return ENOENT;
  245. case FILEIO_EINTR:
  246. return EINTR;
  247. case FILEIO_EIO:
  248. return EIO;
  249. case FILEIO_EBADF:
  250. return EBADF;
  251. case FILEIO_EACCES:
  252. return EACCES;
  253. case FILEIO_EFAULT:
  254. return EFAULT;
  255. case FILEIO_EBUSY:
  256. return EBUSY;
  257. case FILEIO_EEXIST:
  258. return EEXIST;
  259. case FILEIO_ENODEV:
  260. return ENODEV;
  261. case FILEIO_ENOTDIR:
  262. return ENOTDIR;
  263. case FILEIO_EISDIR:
  264. return EISDIR;
  265. case FILEIO_EINVAL:
  266. return EINVAL;
  267. case FILEIO_ENFILE:
  268. return ENFILE;
  269. case FILEIO_EMFILE:
  270. return EMFILE;
  271. case FILEIO_EFBIG:
  272. return EFBIG;
  273. case FILEIO_ENOSPC:
  274. return ENOSPC;
  275. case FILEIO_ESPIPE:
  276. return ESPIPE;
  277. case FILEIO_EROFS:
  278. return EROFS;
  279. case FILEIO_ENOSYS:
  280. return ENOSYS;
  281. case FILEIO_ENAMETOOLONG:
  282. return ENAMETOOLONG;
  283. }
  284. return -1;
  285. }
  286. /* bfd_openr_iovec OPEN_CLOSURE data for gdb_bfd_open. */
  287. struct gdb_bfd_open_closure
  288. {
  289. inferior *inf;
  290. bool warn_if_slow;
  291. };
  292. /* Wrapper for target_fileio_open suitable for passing as the
  293. OPEN_FUNC argument to gdb_bfd_openr_iovec. */
  294. static void *
  295. gdb_bfd_iovec_fileio_open (struct bfd *abfd, void *open_closure)
  296. {
  297. const char *filename = bfd_get_filename (abfd);
  298. int fd, target_errno;
  299. int *stream;
  300. gdb_bfd_open_closure *oclosure = (gdb_bfd_open_closure *) open_closure;
  301. gdb_assert (is_target_filename (filename));
  302. fd = target_fileio_open (oclosure->inf,
  303. filename + strlen (TARGET_SYSROOT_PREFIX),
  304. FILEIO_O_RDONLY, 0, oclosure->warn_if_slow,
  305. &target_errno);
  306. if (fd == -1)
  307. {
  308. errno = fileio_errno_to_host (target_errno);
  309. bfd_set_error (bfd_error_system_call);
  310. return NULL;
  311. }
  312. stream = XCNEW (int);
  313. *stream = fd;
  314. return stream;
  315. }
  316. /* Wrapper for target_fileio_pread suitable for passing as the
  317. PREAD_FUNC argument to gdb_bfd_openr_iovec. */
  318. static file_ptr
  319. gdb_bfd_iovec_fileio_pread (struct bfd *abfd, void *stream, void *buf,
  320. file_ptr nbytes, file_ptr offset)
  321. {
  322. int fd = *(int *) stream;
  323. int target_errno;
  324. file_ptr pos, bytes;
  325. pos = 0;
  326. while (nbytes > pos)
  327. {
  328. QUIT;
  329. bytes = target_fileio_pread (fd, (gdb_byte *) buf + pos,
  330. nbytes - pos, offset + pos,
  331. &target_errno);
  332. if (bytes == 0)
  333. /* Success, but no bytes, means end-of-file. */
  334. break;
  335. if (bytes == -1)
  336. {
  337. errno = fileio_errno_to_host (target_errno);
  338. bfd_set_error (bfd_error_system_call);
  339. return -1;
  340. }
  341. pos += bytes;
  342. }
  343. return pos;
  344. }
  345. /* Warn that it wasn't possible to close a bfd for file NAME, because
  346. of REASON. */
  347. static void
  348. gdb_bfd_close_warning (const char *name, const char *reason)
  349. {
  350. warning (_("cannot close \"%s\": %s"), name, reason);
  351. }
  352. /* Wrapper for target_fileio_close suitable for passing as the
  353. CLOSE_FUNC argument to gdb_bfd_openr_iovec. */
  354. static int
  355. gdb_bfd_iovec_fileio_close (struct bfd *abfd, void *stream)
  356. {
  357. int fd = *(int *) stream;
  358. int target_errno;
  359. xfree (stream);
  360. /* Ignore errors on close. These may happen with remote
  361. targets if the connection has already been torn down. */
  362. try
  363. {
  364. target_fileio_close (fd, &target_errno);
  365. }
  366. catch (const gdb_exception &ex)
  367. {
  368. /* Also avoid crossing exceptions over bfd. */
  369. gdb_bfd_close_warning (bfd_get_filename (abfd),
  370. ex.message->c_str ());
  371. }
  372. /* Zero means success. */
  373. return 0;
  374. }
  375. /* Wrapper for target_fileio_fstat suitable for passing as the
  376. STAT_FUNC argument to gdb_bfd_openr_iovec. */
  377. static int
  378. gdb_bfd_iovec_fileio_fstat (struct bfd *abfd, void *stream,
  379. struct stat *sb)
  380. {
  381. int fd = *(int *) stream;
  382. int target_errno;
  383. int result;
  384. result = target_fileio_fstat (fd, sb, &target_errno);
  385. if (result == -1)
  386. {
  387. errno = fileio_errno_to_host (target_errno);
  388. bfd_set_error (bfd_error_system_call);
  389. }
  390. return result;
  391. }
  392. /* A helper function to initialize the data that gdb attaches to each
  393. BFD. */
  394. static void
  395. gdb_bfd_init_data (struct bfd *abfd, struct stat *st)
  396. {
  397. struct gdb_bfd_data *gdata;
  398. void **slot;
  399. gdb_assert (bfd_usrdata (abfd) == nullptr);
  400. /* Ask BFD to decompress sections in bfd_get_full_section_contents. */
  401. abfd->flags |= BFD_DECOMPRESS;
  402. gdata = new gdb_bfd_data (abfd, st);
  403. bfd_set_usrdata (abfd, gdata);
  404. bfd_alloc_data (abfd);
  405. /* This is the first we've seen it, so add it to the hash table. */
  406. slot = htab_find_slot (all_bfds, abfd, INSERT);
  407. gdb_assert (slot && !*slot);
  408. *slot = abfd;
  409. }
  410. /* See gdb_bfd.h. */
  411. gdb_bfd_ref_ptr
  412. gdb_bfd_open (const char *name, const char *target, int fd,
  413. bool warn_if_slow)
  414. {
  415. hashval_t hash;
  416. void **slot;
  417. bfd *abfd;
  418. struct gdb_bfd_cache_search search;
  419. struct stat st;
  420. if (is_target_filename (name))
  421. {
  422. if (!target_filesystem_is_local ())
  423. {
  424. gdb_assert (fd == -1);
  425. gdb_bfd_open_closure open_closure { current_inferior (), warn_if_slow };
  426. return gdb_bfd_openr_iovec (name, target,
  427. gdb_bfd_iovec_fileio_open,
  428. &open_closure,
  429. gdb_bfd_iovec_fileio_pread,
  430. gdb_bfd_iovec_fileio_close,
  431. gdb_bfd_iovec_fileio_fstat);
  432. }
  433. name += strlen (TARGET_SYSROOT_PREFIX);
  434. }
  435. if (gdb_bfd_cache == NULL)
  436. gdb_bfd_cache = htab_create_alloc (1, hash_bfd, eq_bfd, NULL,
  437. xcalloc, xfree);
  438. if (fd == -1)
  439. {
  440. fd = gdb_open_cloexec (name, O_RDONLY | O_BINARY, 0).release ();
  441. if (fd == -1)
  442. {
  443. bfd_set_error (bfd_error_system_call);
  444. return NULL;
  445. }
  446. }
  447. if (fstat (fd, &st) < 0)
  448. {
  449. /* Weird situation here -- don't cache if we can't stat. */
  450. bfd_cache_debug_printf ("Could not stat %s - not caching", name);
  451. abfd = bfd_fopen (name, target, FOPEN_RB, fd);
  452. if (abfd == nullptr)
  453. return nullptr;
  454. return gdb_bfd_ref_ptr::new_reference (abfd);
  455. }
  456. search.filename = name;
  457. search.mtime = st.st_mtime;
  458. search.size = st.st_size;
  459. search.inode = st.st_ino;
  460. search.device_id = st.st_dev;
  461. /* Note that this must compute the same result as hash_bfd. */
  462. hash = htab_hash_string (name);
  463. /* Note that we cannot use htab_find_slot_with_hash here, because
  464. opening the BFD may fail; and this would violate hashtab
  465. invariants. */
  466. abfd = (struct bfd *) htab_find_with_hash (gdb_bfd_cache, &search, hash);
  467. if (bfd_sharing && abfd != NULL)
  468. {
  469. bfd_cache_debug_printf ("Reusing cached bfd %s for %s",
  470. host_address_to_string (abfd),
  471. bfd_get_filename (abfd));
  472. close (fd);
  473. return gdb_bfd_ref_ptr::new_reference (abfd);
  474. }
  475. abfd = bfd_fopen (name, target, FOPEN_RB, fd);
  476. if (abfd == NULL)
  477. return NULL;
  478. bfd_cache_debug_printf ("Creating new bfd %s for %s",
  479. host_address_to_string (abfd),
  480. bfd_get_filename (abfd));
  481. if (bfd_sharing)
  482. {
  483. slot = htab_find_slot_with_hash (gdb_bfd_cache, &search, hash, INSERT);
  484. gdb_assert (!*slot);
  485. *slot = abfd;
  486. }
  487. /* It's important to pass the already-computed stat info here,
  488. rather than, say, calling gdb_bfd_ref_ptr::new_reference. BFD by
  489. default will "stat" the file each time bfd_get_mtime is called --
  490. and since we already entered it into the hash table using this
  491. mtime, if the file changed at the wrong moment, the race would
  492. lead to a hash table corruption. */
  493. gdb_bfd_init_data (abfd, &st);
  494. return gdb_bfd_ref_ptr (abfd);
  495. }
  496. /* A helper function that releases any section data attached to the
  497. BFD. */
  498. static void
  499. free_one_bfd_section (asection *sectp)
  500. {
  501. struct gdb_bfd_section_data *sect
  502. = (struct gdb_bfd_section_data *) bfd_section_userdata (sectp);
  503. if (sect != NULL && sect->data != NULL)
  504. {
  505. #ifdef HAVE_MMAP
  506. if (sect->map_addr != NULL)
  507. {
  508. int res;
  509. res = munmap (sect->map_addr, sect->map_len);
  510. gdb_assert (res == 0);
  511. }
  512. else
  513. #endif
  514. xfree (sect->data);
  515. }
  516. }
  517. /* Close ABFD, and warn if that fails. */
  518. static int
  519. gdb_bfd_close_or_warn (struct bfd *abfd)
  520. {
  521. int ret;
  522. const char *name = bfd_get_filename (abfd);
  523. for (asection *sect : gdb_bfd_sections (abfd))
  524. free_one_bfd_section (sect);
  525. ret = bfd_close (abfd);
  526. if (!ret)
  527. gdb_bfd_close_warning (name,
  528. bfd_errmsg (bfd_get_error ()));
  529. return ret;
  530. }
  531. /* See gdb_bfd.h. */
  532. void
  533. gdb_bfd_ref (struct bfd *abfd)
  534. {
  535. struct gdb_bfd_data *gdata;
  536. if (abfd == NULL)
  537. return;
  538. gdata = (struct gdb_bfd_data *) bfd_usrdata (abfd);
  539. bfd_cache_debug_printf ("Increase reference count on bfd %s (%s)",
  540. host_address_to_string (abfd),
  541. bfd_get_filename (abfd));
  542. if (gdata != NULL)
  543. {
  544. gdata->refc += 1;
  545. return;
  546. }
  547. /* Caching only happens via gdb_bfd_open, so passing nullptr here is
  548. fine. */
  549. gdb_bfd_init_data (abfd, nullptr);
  550. }
  551. /* See gdb_bfd.h. */
  552. void
  553. gdb_bfd_unref (struct bfd *abfd)
  554. {
  555. struct gdb_bfd_data *gdata;
  556. struct gdb_bfd_cache_search search;
  557. bfd *archive_bfd;
  558. if (abfd == NULL)
  559. return;
  560. gdata = (struct gdb_bfd_data *) bfd_usrdata (abfd);
  561. gdb_assert (gdata->refc >= 1);
  562. gdata->refc -= 1;
  563. if (gdata->refc > 0)
  564. {
  565. bfd_cache_debug_printf ("Decrease reference count on bfd %s (%s)",
  566. host_address_to_string (abfd),
  567. bfd_get_filename (abfd));
  568. return;
  569. }
  570. bfd_cache_debug_printf ("Delete final reference count on bfd %s (%s)",
  571. host_address_to_string (abfd),
  572. bfd_get_filename (abfd));
  573. archive_bfd = gdata->archive_bfd;
  574. search.filename = bfd_get_filename (abfd);
  575. if (gdb_bfd_cache && search.filename)
  576. {
  577. hashval_t hash = htab_hash_string (search.filename);
  578. void **slot;
  579. search.mtime = gdata->mtime;
  580. search.size = gdata->size;
  581. search.inode = gdata->inode;
  582. search.device_id = gdata->device_id;
  583. slot = htab_find_slot_with_hash (gdb_bfd_cache, &search, hash,
  584. NO_INSERT);
  585. if (slot && *slot)
  586. htab_clear_slot (gdb_bfd_cache, slot);
  587. }
  588. bfd_free_data (abfd);
  589. delete gdata;
  590. bfd_set_usrdata (abfd, NULL); /* Paranoia. */
  591. htab_remove_elt (all_bfds, abfd);
  592. gdb_bfd_close_or_warn (abfd);
  593. gdb_bfd_unref (archive_bfd);
  594. }
  595. /* A helper function that returns the section data descriptor
  596. associated with SECTION. If no such descriptor exists, a new one
  597. is allocated and cleared. */
  598. static struct gdb_bfd_section_data *
  599. get_section_descriptor (asection *section)
  600. {
  601. struct gdb_bfd_section_data *result;
  602. result = (struct gdb_bfd_section_data *) bfd_section_userdata (section);
  603. if (result == NULL)
  604. {
  605. result = ((struct gdb_bfd_section_data *)
  606. bfd_zalloc (section->owner, sizeof (*result)));
  607. bfd_set_section_userdata (section, result);
  608. }
  609. return result;
  610. }
  611. /* See gdb_bfd.h. */
  612. const gdb_byte *
  613. gdb_bfd_map_section (asection *sectp, bfd_size_type *size)
  614. {
  615. bfd *abfd;
  616. struct gdb_bfd_section_data *descriptor;
  617. bfd_byte *data;
  618. gdb_assert ((sectp->flags & SEC_RELOC) == 0);
  619. gdb_assert (size != NULL);
  620. abfd = sectp->owner;
  621. descriptor = get_section_descriptor (sectp);
  622. /* If the data was already read for this BFD, just reuse it. */
  623. if (descriptor->data != NULL)
  624. goto done;
  625. #ifdef HAVE_MMAP
  626. if (!bfd_is_section_compressed (abfd, sectp))
  627. {
  628. /* The page size, used when mmapping. */
  629. static int pagesize;
  630. if (pagesize == 0)
  631. pagesize = getpagesize ();
  632. /* Only try to mmap sections which are large enough: we don't want
  633. to waste space due to fragmentation. */
  634. if (bfd_section_size (sectp) > 4 * pagesize)
  635. {
  636. descriptor->size = bfd_section_size (sectp);
  637. descriptor->data = bfd_mmap (abfd, 0, descriptor->size, PROT_READ,
  638. MAP_PRIVATE, sectp->filepos,
  639. &descriptor->map_addr,
  640. &descriptor->map_len);
  641. if ((caddr_t)descriptor->data != MAP_FAILED)
  642. {
  643. #if HAVE_POSIX_MADVISE
  644. posix_madvise (descriptor->map_addr, descriptor->map_len,
  645. POSIX_MADV_WILLNEED);
  646. #endif
  647. goto done;
  648. }
  649. /* On failure, clear out the section data and try again. */
  650. memset (descriptor, 0, sizeof (*descriptor));
  651. }
  652. }
  653. #endif /* HAVE_MMAP */
  654. /* Handle compressed sections, or ordinary uncompressed sections in
  655. the no-mmap case. */
  656. descriptor->size = bfd_section_size (sectp);
  657. descriptor->data = NULL;
  658. data = NULL;
  659. if (!bfd_get_full_section_contents (abfd, sectp, &data))
  660. {
  661. warning (_("Can't read data for section '%s' in file '%s'"),
  662. bfd_section_name (sectp),
  663. bfd_get_filename (abfd));
  664. /* Set size to 0 to prevent further attempts to read the invalid
  665. section. */
  666. *size = 0;
  667. return NULL;
  668. }
  669. descriptor->data = data;
  670. done:
  671. gdb_assert (descriptor->data != NULL);
  672. *size = descriptor->size;
  673. return (const gdb_byte *) descriptor->data;
  674. }
  675. /* Return 32-bit CRC for ABFD. If successful store it to *FILE_CRC_RETURN and
  676. return 1. Otherwise print a warning and return 0. ABFD seek position is
  677. not preserved. */
  678. static int
  679. get_file_crc (bfd *abfd, unsigned long *file_crc_return)
  680. {
  681. unsigned long file_crc = 0;
  682. if (bfd_seek (abfd, 0, SEEK_SET) != 0)
  683. {
  684. warning (_("Problem reading \"%s\" for CRC: %s"),
  685. bfd_get_filename (abfd), bfd_errmsg (bfd_get_error ()));
  686. return 0;
  687. }
  688. for (;;)
  689. {
  690. gdb_byte buffer[8 * 1024];
  691. bfd_size_type count;
  692. count = bfd_bread (buffer, sizeof (buffer), abfd);
  693. if (count == (bfd_size_type) -1)
  694. {
  695. warning (_("Problem reading \"%s\" for CRC: %s"),
  696. bfd_get_filename (abfd), bfd_errmsg (bfd_get_error ()));
  697. return 0;
  698. }
  699. if (count == 0)
  700. break;
  701. file_crc = bfd_calc_gnu_debuglink_crc32 (file_crc, buffer, count);
  702. }
  703. *file_crc_return = file_crc;
  704. return 1;
  705. }
  706. /* See gdb_bfd.h. */
  707. int
  708. gdb_bfd_crc (struct bfd *abfd, unsigned long *crc_out)
  709. {
  710. struct gdb_bfd_data *gdata = (struct gdb_bfd_data *) bfd_usrdata (abfd);
  711. if (!gdata->crc_computed)
  712. gdata->crc_computed = get_file_crc (abfd, &gdata->crc);
  713. if (gdata->crc_computed)
  714. *crc_out = gdata->crc;
  715. return gdata->crc_computed;
  716. }
  717. /* See gdb_bfd.h. */
  718. gdb_bfd_ref_ptr
  719. gdb_bfd_fopen (const char *filename, const char *target, const char *mode,
  720. int fd)
  721. {
  722. bfd *result = bfd_fopen (filename, target, mode, fd);
  723. return gdb_bfd_ref_ptr::new_reference (result);
  724. }
  725. /* See gdb_bfd.h. */
  726. gdb_bfd_ref_ptr
  727. gdb_bfd_openr (const char *filename, const char *target)
  728. {
  729. bfd *result = bfd_openr (filename, target);
  730. return gdb_bfd_ref_ptr::new_reference (result);
  731. }
  732. /* See gdb_bfd.h. */
  733. gdb_bfd_ref_ptr
  734. gdb_bfd_openw (const char *filename, const char *target)
  735. {
  736. bfd *result = bfd_openw (filename, target);
  737. return gdb_bfd_ref_ptr::new_reference (result);
  738. }
  739. /* See gdb_bfd.h. */
  740. gdb_bfd_ref_ptr
  741. gdb_bfd_openr_iovec (const char *filename, const char *target,
  742. void *(*open_func) (struct bfd *nbfd,
  743. void *open_closure),
  744. void *open_closure,
  745. file_ptr (*pread_func) (struct bfd *nbfd,
  746. void *stream,
  747. void *buf,
  748. file_ptr nbytes,
  749. file_ptr offset),
  750. int (*close_func) (struct bfd *nbfd,
  751. void *stream),
  752. int (*stat_func) (struct bfd *abfd,
  753. void *stream,
  754. struct stat *sb))
  755. {
  756. bfd *result = bfd_openr_iovec (filename, target,
  757. open_func, open_closure,
  758. pread_func, close_func, stat_func);
  759. return gdb_bfd_ref_ptr::new_reference (result);
  760. }
  761. /* See gdb_bfd.h. */
  762. void
  763. gdb_bfd_mark_parent (bfd *child, bfd *parent)
  764. {
  765. struct gdb_bfd_data *gdata;
  766. gdb_bfd_ref (child);
  767. /* No need to stash the filename here, because we also keep a
  768. reference on the parent archive. */
  769. gdata = (struct gdb_bfd_data *) bfd_usrdata (child);
  770. if (gdata->archive_bfd == NULL)
  771. {
  772. gdata->archive_bfd = parent;
  773. gdb_bfd_ref (parent);
  774. }
  775. else
  776. gdb_assert (gdata->archive_bfd == parent);
  777. }
  778. /* See gdb_bfd.h. */
  779. gdb_bfd_ref_ptr
  780. gdb_bfd_openr_next_archived_file (bfd *archive, bfd *previous)
  781. {
  782. bfd *result = bfd_openr_next_archived_file (archive, previous);
  783. if (result)
  784. gdb_bfd_mark_parent (result, archive);
  785. return gdb_bfd_ref_ptr (result);
  786. }
  787. /* See gdb_bfd.h. */
  788. void
  789. gdb_bfd_record_inclusion (bfd *includer, bfd *includee)
  790. {
  791. struct gdb_bfd_data *gdata;
  792. gdata = (struct gdb_bfd_data *) bfd_usrdata (includer);
  793. gdata->included_bfds.push_back (gdb_bfd_ref_ptr::new_reference (includee));
  794. }
  795. gdb_static_assert (ARRAY_SIZE (_bfd_std_section) == 4);
  796. /* See gdb_bfd.h. */
  797. int
  798. gdb_bfd_section_index (bfd *abfd, asection *section)
  799. {
  800. if (section == NULL)
  801. return -1;
  802. else if (section == bfd_com_section_ptr)
  803. return bfd_count_sections (abfd);
  804. else if (section == bfd_und_section_ptr)
  805. return bfd_count_sections (abfd) + 1;
  806. else if (section == bfd_abs_section_ptr)
  807. return bfd_count_sections (abfd) + 2;
  808. else if (section == bfd_ind_section_ptr)
  809. return bfd_count_sections (abfd) + 3;
  810. return section->index;
  811. }
  812. /* See gdb_bfd.h. */
  813. int
  814. gdb_bfd_count_sections (bfd *abfd)
  815. {
  816. return bfd_count_sections (abfd) + 4;
  817. }
  818. /* See gdb_bfd.h. */
  819. int
  820. gdb_bfd_requires_relocations (bfd *abfd)
  821. {
  822. struct gdb_bfd_data *gdata = (struct gdb_bfd_data *) bfd_usrdata (abfd);
  823. if (gdata->relocation_computed == 0)
  824. {
  825. asection *sect;
  826. for (sect = abfd->sections; sect != NULL; sect = sect->next)
  827. if ((sect->flags & SEC_RELOC) != 0)
  828. {
  829. gdata->needs_relocations = 1;
  830. break;
  831. }
  832. gdata->relocation_computed = 1;
  833. }
  834. return gdata->needs_relocations;
  835. }
  836. /* See gdb_bfd.h. */
  837. bool
  838. gdb_bfd_get_full_section_contents (bfd *abfd, asection *section,
  839. gdb::byte_vector *contents)
  840. {
  841. bfd_size_type section_size = bfd_section_size (section);
  842. contents->resize (section_size);
  843. return bfd_get_section_contents (abfd, section, contents->data (), 0,
  844. section_size);
  845. }
  846. #define AMBIGUOUS_MESS1 ".\nMatching formats:"
  847. #define AMBIGUOUS_MESS2 \
  848. ".\nUse \"set gnutarget format-name\" to specify the format."
  849. /* See gdb_bfd.h. */
  850. std::string
  851. gdb_bfd_errmsg (bfd_error_type error_tag, char **matching)
  852. {
  853. char **p;
  854. /* Check if errmsg just need simple return. */
  855. if (error_tag != bfd_error_file_ambiguously_recognized || matching == NULL)
  856. return bfd_errmsg (error_tag);
  857. std::string ret (bfd_errmsg (error_tag));
  858. ret += AMBIGUOUS_MESS1;
  859. for (p = matching; *p; p++)
  860. {
  861. ret += " ";
  862. ret += *p;
  863. }
  864. ret += AMBIGUOUS_MESS2;
  865. xfree (matching);
  866. return ret;
  867. }
  868. /* A callback for htab_traverse that prints a single BFD. */
  869. static int
  870. print_one_bfd (void **slot, void *data)
  871. {
  872. bfd *abfd = (struct bfd *) *slot;
  873. struct gdb_bfd_data *gdata = (struct gdb_bfd_data *) bfd_usrdata (abfd);
  874. struct ui_out *uiout = (struct ui_out *) data;
  875. ui_out_emit_tuple tuple_emitter (uiout, NULL);
  876. uiout->field_signed ("refcount", gdata->refc);
  877. uiout->field_string ("addr", host_address_to_string (abfd));
  878. uiout->field_string ("filename", bfd_get_filename (abfd),
  879. file_name_style.style ());
  880. uiout->text ("\n");
  881. return 1;
  882. }
  883. /* Implement the 'maint info bfd' command. */
  884. static void
  885. maintenance_info_bfds (const char *arg, int from_tty)
  886. {
  887. struct ui_out *uiout = current_uiout;
  888. ui_out_emit_table table_emitter (uiout, 3, -1, "bfds");
  889. uiout->table_header (10, ui_left, "refcount", "Refcount");
  890. uiout->table_header (18, ui_left, "addr", "Address");
  891. uiout->table_header (40, ui_left, "filename", "Filename");
  892. uiout->table_body ();
  893. htab_traverse (all_bfds, print_one_bfd, uiout);
  894. }
  895. void _initialize_gdb_bfd ();
  896. void
  897. _initialize_gdb_bfd ()
  898. {
  899. all_bfds = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
  900. NULL, xcalloc, xfree);
  901. add_cmd ("bfds", class_maintenance, maintenance_info_bfds, _("\
  902. List the BFDs that are currently open."),
  903. &maintenanceinfolist);
  904. add_setshow_boolean_cmd ("bfd-sharing", no_class,
  905. &bfd_sharing, _("\
  906. Set whether gdb will share bfds that appear to be the same file."), _("\
  907. Show whether gdb will share bfds that appear to be the same file."), _("\
  908. When enabled gdb will reuse existing bfds rather than reopening the\n\
  909. same file. To decide if two files are the same then gdb compares the\n\
  910. filename, file size, file modification time, and file inode."),
  911. NULL,
  912. &show_bfd_sharing,
  913. &maintenance_set_cmdlist,
  914. &maintenance_show_cmdlist);
  915. add_setshow_boolean_cmd ("bfd-cache", class_maintenance,
  916. &debug_bfd_cache,
  917. _("Set bfd cache debugging."),
  918. _("Show bfd cache debugging."),
  919. _("\
  920. When non-zero, bfd cache specific debugging is enabled."),
  921. NULL,
  922. &show_bfd_cache_debug,
  923. &setdebuglist, &showdebuglist);
  924. }