]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/gdb_bfd.c
gdbsupport: convert FILEIO_* macros to an enum
[thirdparty/binutils-gdb.git] / gdb / gdb_bfd.c
CommitLineData
cbb099e8
TT
1/* Definitions for BFD wrappers used by GDB.
2
4a94e368 3 Copyright (C) 2011-2022 Free Software Foundation, Inc.
cbb099e8
TT
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20#include "defs.h"
21#include "gdb_bfd.h"
d6b28940
TT
22#include "ui-out.h"
23#include "gdbcmd.h"
6ec53d05 24#include "hashtab.h"
268a13a5 25#include "gdbsupport/filestuff.h"
4bf44c1c
TT
26#ifdef HAVE_MMAP
27#include <sys/mman.h>
28#ifndef MAP_FAILED
29#define MAP_FAILED ((void *) -1)
30#endif
31#endif
f08e97fe 32#include "target.h"
198f946f 33#include "gdbsupport/fileio.h"
07c138c8 34#include "inferior.h"
972f7a4b 35#include "cli/cli-style.h"
84e60555 36#include <unordered_map>
4bf44c1c
TT
37
38/* An object of this type is stored in the section's user data when
39 mapping a section. */
40
41struct gdb_bfd_section_data
42{
43 /* Size of the data. */
44 bfd_size_type size;
45 /* If the data was mmapped, this is the length of the map. */
46 bfd_size_type map_len;
47 /* The data. If NULL, the section data has not been read. */
48 void *data;
49 /* If the data was mmapped, this is the map address. */
50 void *map_addr;
51};
a4453b7e 52
d6b28940
TT
53/* A hash table holding every BFD that gdb knows about. This is not
54 to be confused with 'gdb_bfd_cache', which is used for sharing
55 BFDs; in contrast, this hash is used just to implement
56 "maint info bfd". */
57
58static htab_t all_bfds;
59
6ec53d05
TT
60/* An object of this type is stored in each BFD's user data. */
61
62struct gdb_bfd_data
63{
3cae4447
TT
64 /* Note that if ST is nullptr, then we simply fill in zeroes. */
65 gdb_bfd_data (bfd *abfd, struct stat *st)
66 : mtime (st == nullptr ? 0 : st->st_mtime),
67 size (st == nullptr ? 0 : st->st_size),
68 inode (st == nullptr ? 0 : st->st_ino),
69 device_id (st == nullptr ? 0 : st->st_dev),
06d5bbc8
TT
70 relocation_computed (0),
71 needs_relocations (0),
72 crc_computed (0)
73 {
06d5bbc8
TT
74 }
75
76 ~gdb_bfd_data ()
77 {
06d5bbc8
TT
78 }
79
6ec53d05 80 /* The reference count. */
06d5bbc8 81 int refc = 1;
6ec53d05
TT
82
83 /* The mtime of the BFD at the point the cache entry was made. */
84 time_t mtime;
b82d08cd 85
c04fe68f
AB
86 /* The file size (in bytes) at the point the cache entry was made. */
87 off_t size;
88
89 /* The inode of the file at the point the cache entry was made. */
90 ino_t inode;
91
92 /* The device id of the file at the point the cache entry was made. */
93 dev_t device_id;
94
1da77581
TT
95 /* This is true if we have determined whether this BFD has any
96 sections requiring relocation. */
97 unsigned int relocation_computed : 1;
98
99 /* This is true if any section needs relocation. */
100 unsigned int needs_relocations : 1;
101
dccee2de
TT
102 /* This is true if we have successfully computed the file's CRC. */
103 unsigned int crc_computed : 1;
104
105 /* The file's CRC. */
06d5bbc8 106 unsigned long crc = 0;
dccee2de 107
b82d08cd
TT
108 /* If the BFD comes from an archive, this points to the archive's
109 BFD. Otherwise, this is NULL. */
06d5bbc8 110 bfd *archive_bfd = nullptr;
e992eda4 111
13aaf454 112 /* Table of all the bfds this bfd has included. */
d5833c62 113 std::vector<gdb_bfd_ref_ptr> included_bfds;
13aaf454 114
e992eda4 115 /* The registry. */
08b8a139 116 registry<bfd> registry_fields;
6ec53d05
TT
117};
118
08b8a139
TT
119registry<bfd> *
120registry_accessor<bfd>::get (bfd *abfd)
121{
122 struct gdb_bfd_data *gdata = (struct gdb_bfd_data *) bfd_usrdata (abfd);
123 return &gdata->registry_fields;
124}
e992eda4 125
6ec53d05
TT
126/* A hash table storing all the BFDs maintained in the cache. */
127
128static htab_t gdb_bfd_cache;
129
18989b3c
AB
130/* When true gdb will reuse an existing bfd object if the filename,
131 modification time, and file size all match. */
132
491144b5 133static bool bfd_sharing = true;
18989b3c
AB
134static void
135show_bfd_sharing (struct ui_file *file, int from_tty,
136 struct cmd_list_element *c, const char *value)
137{
6cb06a8c 138 gdb_printf (file, _("BFD sharing is %s.\n"), value);
18989b3c
AB
139}
140
d4dd4fca
SM
141/* When true debugging of the bfd caches is enabled. */
142
143static bool debug_bfd_cache;
566f5e3b 144
1e15fcac
SM
145/* Print an "bfd-cache" debug statement. */
146
147#define bfd_cache_debug_printf(fmt, ...) \
148 debug_prefixed_printf_cond (debug_bfd_cache, "bfd-cache", fmt, ##__VA_ARGS__)
149
566f5e3b
AB
150static void
151show_bfd_cache_debug (struct ui_file *file, int from_tty,
152 struct cmd_list_element *c, const char *value)
153{
6cb06a8c 154 gdb_printf (file, _("BFD cache debugging is %s.\n"), value);
566f5e3b
AB
155}
156
6ec53d05
TT
157/* The type of an object being looked up in gdb_bfd_cache. We use
158 htab's capability of storing one kind of object (BFD in this case)
159 and using a different sort of object for searching. */
160
161struct gdb_bfd_cache_search
162{
163 /* The filename. */
164 const char *filename;
165 /* The mtime. */
166 time_t mtime;
c04fe68f
AB
167 /* The file size (in bytes). */
168 off_t size;
169 /* The inode of the file. */
170 ino_t inode;
171 /* The device id of the file. */
172 dev_t device_id;
6ec53d05
TT
173};
174
175/* A hash function for BFDs. */
176
177static hashval_t
178hash_bfd (const void *b)
179{
9a3c8263 180 const bfd *abfd = (const struct bfd *) b;
6ec53d05
TT
181
182 /* It is simplest to just hash the filename. */
183 return htab_hash_string (bfd_get_filename (abfd));
184}
185
186/* An equality function for BFDs. Note that this expects the caller
187 to search using struct gdb_bfd_cache_search only, not BFDs. */
188
189static int
190eq_bfd (const void *a, const void *b)
191{
9a3c8263
SM
192 const bfd *abfd = (const struct bfd *) a;
193 const struct gdb_bfd_cache_search *s
194 = (const struct gdb_bfd_cache_search *) b;
195 struct gdb_bfd_data *gdata = (struct gdb_bfd_data *) bfd_usrdata (abfd);
6ec53d05
TT
196
197 return (gdata->mtime == s->mtime
c04fe68f
AB
198 && gdata->size == s->size
199 && gdata->inode == s->inode
200 && gdata->device_id == s->device_id
6ec53d05
TT
201 && strcmp (bfd_get_filename (abfd), s->filename) == 0);
202}
203
204/* See gdb_bfd.h. */
205
f08e97fe
GB
206int
207is_target_filename (const char *name)
208{
209 return startswith (name, TARGET_SYSROOT_PREFIX);
210}
211
212/* See gdb_bfd.h. */
213
214int
215gdb_bfd_has_target_filename (struct bfd *abfd)
216{
217 return is_target_filename (bfd_get_filename (abfd));
218}
219
15cc148f
MS
220/* For `gdb_bfd_open_from_target_memory`. */
221
222struct target_buffer
223{
224 CORE_ADDR base;
225 ULONGEST size;
226};
227
228/* For `gdb_bfd_open_from_target_memory`. Opening the file is a no-op. */
229
230static void *
231mem_bfd_iovec_open (struct bfd *abfd, void *open_closure)
232{
233 return open_closure;
234}
235
236/* For `gdb_bfd_open_from_target_memory`. Closing the file is just freeing the
237 base/size pair on our side. */
238
239static int
240mem_bfd_iovec_close (struct bfd *abfd, void *stream)
241{
242 xfree (stream);
243
244 /* Zero means success. */
245 return 0;
246}
247
248/* For `gdb_bfd_open_from_target_memory`. For reading the file, we just need to
249 pass through to target_read_memory and fix up the arguments and return
250 values. */
251
252static file_ptr
253mem_bfd_iovec_pread (struct bfd *abfd, void *stream, void *buf,
254 file_ptr nbytes, file_ptr offset)
255{
256 int err;
257 struct target_buffer *buffer = (struct target_buffer *) stream;
258
259 /* If this read will read all of the file, limit it to just the rest. */
260 if (offset + nbytes > buffer->size)
261 nbytes = buffer->size - offset;
262
263 /* If there are no more bytes left, we've reached EOF. */
264 if (nbytes == 0)
265 return 0;
266
267 err = target_read_memory (buffer->base + offset, (gdb_byte *) buf, nbytes);
268 if (err)
269 return -1;
270
271 return nbytes;
272}
273
274/* For `gdb_bfd_open_from_target_memory`. For statting the file, we only
275 support the st_size attribute. */
276
277static int
278mem_bfd_iovec_stat (struct bfd *abfd, void *stream, struct stat *sb)
279{
280 struct target_buffer *buffer = (struct target_buffer*) stream;
281
282 memset (sb, 0, sizeof (struct stat));
283 sb->st_size = buffer->size;
284 return 0;
285}
286
287/* See gdb_bfd.h. */
288
289gdb_bfd_ref_ptr
290gdb_bfd_open_from_target_memory (CORE_ADDR addr, ULONGEST size,
291 const char *target,
292 const char *filename)
293{
294 struct target_buffer *buffer = XNEW (struct target_buffer);
295
296 buffer->base = addr;
297 buffer->size = size;
298 return gdb_bfd_openr_iovec (filename ? filename : "<in-memory>", target,
299 mem_bfd_iovec_open,
300 buffer,
301 mem_bfd_iovec_pread,
302 mem_bfd_iovec_close,
303 mem_bfd_iovec_stat);
304}
f08e97fe
GB
305
306/* Return the system error number corresponding to ERRNUM. */
307
308static int
b872057a 309fileio_errno_to_host (fileio_error errnum)
f08e97fe
GB
310{
311 switch (errnum)
312 {
313 case FILEIO_EPERM:
dda83cd7 314 return EPERM;
f08e97fe 315 case FILEIO_ENOENT:
dda83cd7 316 return ENOENT;
f08e97fe 317 case FILEIO_EINTR:
dda83cd7 318 return EINTR;
f08e97fe 319 case FILEIO_EIO:
dda83cd7 320 return EIO;
f08e97fe 321 case FILEIO_EBADF:
dda83cd7 322 return EBADF;
f08e97fe 323 case FILEIO_EACCES:
dda83cd7 324 return EACCES;
f08e97fe 325 case FILEIO_EFAULT:
dda83cd7 326 return EFAULT;
f08e97fe 327 case FILEIO_EBUSY:
dda83cd7 328 return EBUSY;
f08e97fe 329 case FILEIO_EEXIST:
dda83cd7 330 return EEXIST;
f08e97fe 331 case FILEIO_ENODEV:
dda83cd7 332 return ENODEV;
f08e97fe 333 case FILEIO_ENOTDIR:
dda83cd7 334 return ENOTDIR;
f08e97fe 335 case FILEIO_EISDIR:
dda83cd7 336 return EISDIR;
f08e97fe 337 case FILEIO_EINVAL:
dda83cd7 338 return EINVAL;
f08e97fe 339 case FILEIO_ENFILE:
dda83cd7 340 return ENFILE;
f08e97fe 341 case FILEIO_EMFILE:
dda83cd7 342 return EMFILE;
f08e97fe 343 case FILEIO_EFBIG:
dda83cd7 344 return EFBIG;
f08e97fe 345 case FILEIO_ENOSPC:
dda83cd7 346 return ENOSPC;
f08e97fe 347 case FILEIO_ESPIPE:
dda83cd7 348 return ESPIPE;
f08e97fe 349 case FILEIO_EROFS:
dda83cd7 350 return EROFS;
f08e97fe 351 case FILEIO_ENOSYS:
dda83cd7 352 return ENOSYS;
f08e97fe 353 case FILEIO_ENAMETOOLONG:
dda83cd7 354 return ENAMETOOLONG;
f08e97fe
GB
355 }
356 return -1;
357}
358
98c59b52
PA
359/* bfd_openr_iovec OPEN_CLOSURE data for gdb_bfd_open. */
360struct gdb_bfd_open_closure
361{
362 inferior *inf;
363 bool warn_if_slow;
364};
365
f08e97fe 366/* Wrapper for target_fileio_open suitable for passing as the
98c59b52 367 OPEN_FUNC argument to gdb_bfd_openr_iovec. */
f08e97fe
GB
368
369static void *
98c59b52 370gdb_bfd_iovec_fileio_open (struct bfd *abfd, void *open_closure)
f08e97fe
GB
371{
372 const char *filename = bfd_get_filename (abfd);
b872057a
SM
373 int fd;
374 fileio_error target_errno;
f08e97fe 375 int *stream;
98c59b52 376 gdb_bfd_open_closure *oclosure = (gdb_bfd_open_closure *) open_closure;
f08e97fe
GB
377
378 gdb_assert (is_target_filename (filename));
379
98c59b52 380 fd = target_fileio_open (oclosure->inf,
4111f652 381 filename + strlen (TARGET_SYSROOT_PREFIX),
98c59b52 382 FILEIO_O_RDONLY, 0, oclosure->warn_if_slow,
4111f652 383 &target_errno);
f08e97fe
GB
384 if (fd == -1)
385 {
386 errno = fileio_errno_to_host (target_errno);
387 bfd_set_error (bfd_error_system_call);
388 return NULL;
389 }
390
391 stream = XCNEW (int);
392 *stream = fd;
393 return stream;
394}
395
396/* Wrapper for target_fileio_pread suitable for passing as the
397 PREAD_FUNC argument to gdb_bfd_openr_iovec. */
398
399static file_ptr
400gdb_bfd_iovec_fileio_pread (struct bfd *abfd, void *stream, void *buf,
401 file_ptr nbytes, file_ptr offset)
402{
403 int fd = *(int *) stream;
b872057a 404 fileio_error target_errno;
f08e97fe
GB
405 file_ptr pos, bytes;
406
407 pos = 0;
408 while (nbytes > pos)
409 {
2d7711a3
GB
410 QUIT;
411
f08e97fe
GB
412 bytes = target_fileio_pread (fd, (gdb_byte *) buf + pos,
413 nbytes - pos, offset + pos,
414 &target_errno);
415 if (bytes == 0)
dda83cd7
SM
416 /* Success, but no bytes, means end-of-file. */
417 break;
f08e97fe
GB
418 if (bytes == -1)
419 {
420 errno = fileio_errno_to_host (target_errno);
421 bfd_set_error (bfd_error_system_call);
422 return -1;
423 }
424
425 pos += bytes;
426 }
427
428 return pos;
429}
430
3a76f8f4
PA
431/* Warn that it wasn't possible to close a bfd for file NAME, because
432 of REASON. */
433
434static void
435gdb_bfd_close_warning (const char *name, const char *reason)
436{
437 warning (_("cannot close \"%s\": %s"), name, reason);
438}
439
f08e97fe
GB
440/* Wrapper for target_fileio_close suitable for passing as the
441 CLOSE_FUNC argument to gdb_bfd_openr_iovec. */
442
443static int
444gdb_bfd_iovec_fileio_close (struct bfd *abfd, void *stream)
445{
446 int fd = *(int *) stream;
b872057a 447 fileio_error target_errno;
f08e97fe
GB
448
449 xfree (stream);
450
451 /* Ignore errors on close. These may happen with remote
452 targets if the connection has already been torn down. */
3a76f8f4
PA
453 try
454 {
455 target_fileio_close (fd, &target_errno);
456 }
457 catch (const gdb_exception &ex)
458 {
459 /* Also avoid crossing exceptions over bfd. */
460 gdb_bfd_close_warning (bfd_get_filename (abfd),
461 ex.message->c_str ());
462 }
f08e97fe
GB
463
464 /* Zero means success. */
465 return 0;
466}
467
468/* Wrapper for target_fileio_fstat suitable for passing as the
469 STAT_FUNC argument to gdb_bfd_openr_iovec. */
470
471static int
472gdb_bfd_iovec_fileio_fstat (struct bfd *abfd, void *stream,
473 struct stat *sb)
474{
475 int fd = *(int *) stream;
b872057a 476 fileio_error target_errno;
f08e97fe
GB
477 int result;
478
479 result = target_fileio_fstat (fd, sb, &target_errno);
480 if (result == -1)
481 {
482 errno = fileio_errno_to_host (target_errno);
483 bfd_set_error (bfd_error_system_call);
484 }
485
486 return result;
487}
488
3cae4447
TT
489/* A helper function to initialize the data that gdb attaches to each
490 BFD. */
491
492static void
493gdb_bfd_init_data (struct bfd *abfd, struct stat *st)
494{
495 struct gdb_bfd_data *gdata;
496 void **slot;
497
498 gdb_assert (bfd_usrdata (abfd) == nullptr);
499
500 /* Ask BFD to decompress sections in bfd_get_full_section_contents. */
501 abfd->flags |= BFD_DECOMPRESS;
502
503 gdata = new gdb_bfd_data (abfd, st);
504 bfd_set_usrdata (abfd, gdata);
3cae4447
TT
505
506 /* This is the first we've seen it, so add it to the hash table. */
507 slot = htab_find_slot (all_bfds, abfd, INSERT);
508 gdb_assert (slot && !*slot);
509 *slot = abfd;
510}
511
f08e97fe
GB
512/* See gdb_bfd.h. */
513
192b62ce 514gdb_bfd_ref_ptr
98c59b52
PA
515gdb_bfd_open (const char *name, const char *target, int fd,
516 bool warn_if_slow)
6ec53d05
TT
517{
518 hashval_t hash;
519 void **slot;
520 bfd *abfd;
521 struct gdb_bfd_cache_search search;
522 struct stat st;
523
f08e97fe
GB
524 if (is_target_filename (name))
525 {
526 if (!target_filesystem_is_local ())
527 {
528 gdb_assert (fd == -1);
529
98c59b52 530 gdb_bfd_open_closure open_closure { current_inferior (), warn_if_slow };
e3dd7556 531 return gdb_bfd_openr_iovec (name, target,
07c138c8 532 gdb_bfd_iovec_fileio_open,
98c59b52 533 &open_closure,
f08e97fe
GB
534 gdb_bfd_iovec_fileio_pread,
535 gdb_bfd_iovec_fileio_close,
536 gdb_bfd_iovec_fileio_fstat);
f08e97fe
GB
537 }
538
539 name += strlen (TARGET_SYSROOT_PREFIX);
540 }
541
6ec53d05
TT
542 if (gdb_bfd_cache == NULL)
543 gdb_bfd_cache = htab_create_alloc (1, hash_bfd, eq_bfd, NULL,
544 xcalloc, xfree);
545
546 if (fd == -1)
547 {
13084383 548 fd = gdb_open_cloexec (name, O_RDONLY | O_BINARY, 0).release ();
6ec53d05
TT
549 if (fd == -1)
550 {
551 bfd_set_error (bfd_error_system_call);
552 return NULL;
553 }
554 }
555
6ec53d05
TT
556 if (fstat (fd, &st) < 0)
557 {
3cae4447 558 /* Weird situation here -- don't cache if we can't stat. */
1e15fcac 559 bfd_cache_debug_printf ("Could not stat %s - not caching", name);
03b0a45f
TT
560 abfd = bfd_fopen (name, target, FOPEN_RB, fd);
561 if (abfd == nullptr)
562 return nullptr;
3cae4447 563 return gdb_bfd_ref_ptr::new_reference (abfd);
c04fe68f 564 }
6ec53d05 565
3cae4447
TT
566 search.filename = name;
567 search.mtime = st.st_mtime;
568 search.size = st.st_size;
569 search.inode = st.st_ino;
570 search.device_id = st.st_dev;
571
6ec53d05
TT
572 /* Note that this must compute the same result as hash_bfd. */
573 hash = htab_hash_string (name);
574 /* Note that we cannot use htab_find_slot_with_hash here, because
575 opening the BFD may fail; and this would violate hashtab
576 invariants. */
9a3c8263 577 abfd = (struct bfd *) htab_find_with_hash (gdb_bfd_cache, &search, hash);
18989b3c 578 if (bfd_sharing && abfd != NULL)
6ec53d05 579 {
1e15fcac
SM
580 bfd_cache_debug_printf ("Reusing cached bfd %s for %s",
581 host_address_to_string (abfd),
582 bfd_get_filename (abfd));
6ec53d05 583 close (fd);
1831a9f9 584 return gdb_bfd_ref_ptr::new_reference (abfd);
6ec53d05
TT
585 }
586
587 abfd = bfd_fopen (name, target, FOPEN_RB, fd);
588 if (abfd == NULL)
589 return NULL;
590
1e15fcac
SM
591 bfd_cache_debug_printf ("Creating new bfd %s for %s",
592 host_address_to_string (abfd),
593 bfd_get_filename (abfd));
566f5e3b 594
18989b3c
AB
595 if (bfd_sharing)
596 {
597 slot = htab_find_slot_with_hash (gdb_bfd_cache, &search, hash, INSERT);
598 gdb_assert (!*slot);
599 *slot = abfd;
600 }
6ec53d05 601
3cae4447
TT
602 /* It's important to pass the already-computed stat info here,
603 rather than, say, calling gdb_bfd_ref_ptr::new_reference. BFD by
604 default will "stat" the file each time bfd_get_mtime is called --
605 and since we already entered it into the hash table using this
606 mtime, if the file changed at the wrong moment, the race would
607 lead to a hash table corruption. */
608 gdb_bfd_init_data (abfd, &st);
609 return gdb_bfd_ref_ptr (abfd);
6ec53d05
TT
610}
611
4bf44c1c
TT
612/* A helper function that releases any section data attached to the
613 BFD. */
614
615static void
1ce51eb5 616free_one_bfd_section (asection *sectp)
4bf44c1c 617{
9a3c8263 618 struct gdb_bfd_section_data *sect
fd361982 619 = (struct gdb_bfd_section_data *) bfd_section_userdata (sectp);
4bf44c1c
TT
620
621 if (sect != NULL && sect->data != NULL)
622 {
623#ifdef HAVE_MMAP
624 if (sect->map_addr != NULL)
625 {
626 int res;
627
628 res = munmap (sect->map_addr, sect->map_len);
629 gdb_assert (res == 0);
630 }
631 else
632#endif
633 xfree (sect->data);
634 }
635}
636
cbb099e8
TT
637/* Close ABFD, and warn if that fails. */
638
639static int
640gdb_bfd_close_or_warn (struct bfd *abfd)
641{
642 int ret;
b16c44de 643 const char *name = bfd_get_filename (abfd);
cbb099e8 644
1ce51eb5
TT
645 for (asection *sect : gdb_bfd_sections (abfd))
646 free_one_bfd_section (sect);
4bf44c1c 647
cbb099e8
TT
648 ret = bfd_close (abfd);
649
650 if (!ret)
3a76f8f4
PA
651 gdb_bfd_close_warning (name,
652 bfd_errmsg (bfd_get_error ()));
cbb099e8
TT
653
654 return ret;
655}
656
596f7d67 657/* See gdb_bfd.h. */
cbb099e8 658
520b0001 659void
cbb099e8
TT
660gdb_bfd_ref (struct bfd *abfd)
661{
6ec53d05 662 struct gdb_bfd_data *gdata;
cbb099e8
TT
663
664 if (abfd == NULL)
520b0001 665 return;
cbb099e8 666
9a3c8263 667 gdata = (struct gdb_bfd_data *) bfd_usrdata (abfd);
cbb099e8 668
1e15fcac
SM
669 bfd_cache_debug_printf ("Increase reference count on bfd %s (%s)",
670 host_address_to_string (abfd),
671 bfd_get_filename (abfd));
566f5e3b 672
6ec53d05 673 if (gdata != NULL)
cbb099e8 674 {
6ec53d05 675 gdata->refc += 1;
520b0001 676 return;
cbb099e8
TT
677 }
678
3cae4447
TT
679 /* Caching only happens via gdb_bfd_open, so passing nullptr here is
680 fine. */
681 gdb_bfd_init_data (abfd, nullptr);
cbb099e8
TT
682}
683
596f7d67 684/* See gdb_bfd.h. */
cbb099e8
TT
685
686void
687gdb_bfd_unref (struct bfd *abfd)
688{
6ec53d05
TT
689 struct gdb_bfd_data *gdata;
690 struct gdb_bfd_cache_search search;
06d5bbc8 691 bfd *archive_bfd;
cbb099e8
TT
692
693 if (abfd == NULL)
694 return;
695
9a3c8263 696 gdata = (struct gdb_bfd_data *) bfd_usrdata (abfd);
6ec53d05 697 gdb_assert (gdata->refc >= 1);
cbb099e8 698
6ec53d05
TT
699 gdata->refc -= 1;
700 if (gdata->refc > 0)
566f5e3b 701 {
1e15fcac
SM
702 bfd_cache_debug_printf ("Decrease reference count on bfd %s (%s)",
703 host_address_to_string (abfd),
704 bfd_get_filename (abfd));
566f5e3b
AB
705 return;
706 }
707
1e15fcac
SM
708 bfd_cache_debug_printf ("Delete final reference count on bfd %s (%s)",
709 host_address_to_string (abfd),
710 bfd_get_filename (abfd));
cbb099e8 711
b82d08cd 712 archive_bfd = gdata->archive_bfd;
6ec53d05
TT
713 search.filename = bfd_get_filename (abfd);
714
715 if (gdb_bfd_cache && search.filename)
716 {
717 hashval_t hash = htab_hash_string (search.filename);
718 void **slot;
719
720 search.mtime = gdata->mtime;
c04fe68f
AB
721 search.size = gdata->size;
722 search.inode = gdata->inode;
723 search.device_id = gdata->device_id;
6ec53d05
TT
724 slot = htab_find_slot_with_hash (gdb_bfd_cache, &search, hash,
725 NO_INSERT);
726
727 if (slot && *slot)
728 htab_clear_slot (gdb_bfd_cache, slot);
729 }
730
06d5bbc8 731 delete gdata;
00f93c44 732 bfd_set_usrdata (abfd, NULL); /* Paranoia. */
cbb099e8 733
d6b28940
TT
734 htab_remove_elt (all_bfds, abfd);
735
cbb099e8 736 gdb_bfd_close_or_warn (abfd);
b82d08cd
TT
737
738 gdb_bfd_unref (archive_bfd);
cbb099e8 739}
4bf44c1c
TT
740
741/* A helper function that returns the section data descriptor
742 associated with SECTION. If no such descriptor exists, a new one
743 is allocated and cleared. */
744
745static struct gdb_bfd_section_data *
746get_section_descriptor (asection *section)
747{
748 struct gdb_bfd_section_data *result;
749
fd361982 750 result = (struct gdb_bfd_section_data *) bfd_section_userdata (section);
4bf44c1c
TT
751
752 if (result == NULL)
753 {
224c3ddb
SM
754 result = ((struct gdb_bfd_section_data *)
755 bfd_zalloc (section->owner, sizeof (*result)));
fd361982 756 bfd_set_section_userdata (section, result);
4bf44c1c
TT
757 }
758
759 return result;
760}
761
4bf44c1c
TT
762/* See gdb_bfd.h. */
763
764const gdb_byte *
765gdb_bfd_map_section (asection *sectp, bfd_size_type *size)
766{
767 bfd *abfd;
4bf44c1c 768 struct gdb_bfd_section_data *descriptor;
ea9f10bb 769 bfd_byte *data;
4bf44c1c
TT
770
771 gdb_assert ((sectp->flags & SEC_RELOC) == 0);
772 gdb_assert (size != NULL);
773
774 abfd = sectp->owner;
775
776 descriptor = get_section_descriptor (sectp);
777
778 /* If the data was already read for this BFD, just reuse it. */
779 if (descriptor->data != NULL)
780 goto done;
781
ea9f10bb
TT
782#ifdef HAVE_MMAP
783 if (!bfd_is_section_compressed (abfd, sectp))
4bf44c1c 784 {
ea9f10bb
TT
785 /* The page size, used when mmapping. */
786 static int pagesize;
4bf44c1c 787
ea9f10bb
TT
788 if (pagesize == 0)
789 pagesize = getpagesize ();
790
791 /* Only try to mmap sections which are large enough: we don't want
792 to waste space due to fragmentation. */
793
fd361982 794 if (bfd_section_size (sectp) > 4 * pagesize)
ea9f10bb 795 {
fd361982 796 descriptor->size = bfd_section_size (sectp);
ea9f10bb
TT
797 descriptor->data = bfd_mmap (abfd, 0, descriptor->size, PROT_READ,
798 MAP_PRIVATE, sectp->filepos,
799 &descriptor->map_addr,
800 &descriptor->map_len);
801
802 if ((caddr_t)descriptor->data != MAP_FAILED)
803 {
4bf44c1c 804#if HAVE_POSIX_MADVISE
ea9f10bb
TT
805 posix_madvise (descriptor->map_addr, descriptor->map_len,
806 POSIX_MADV_WILLNEED);
4bf44c1c 807#endif
ea9f10bb
TT
808 goto done;
809 }
4bf44c1c 810
ea9f10bb
TT
811 /* On failure, clear out the section data and try again. */
812 memset (descriptor, 0, sizeof (*descriptor));
813 }
814 }
4bf44c1c
TT
815#endif /* HAVE_MMAP */
816
ea9f10bb
TT
817 /* Handle compressed sections, or ordinary uncompressed sections in
818 the no-mmap case. */
4bf44c1c 819
fd361982 820 descriptor->size = bfd_section_size (sectp);
ea9f10bb 821 descriptor->data = NULL;
4bf44c1c 822
ea9f10bb
TT
823 data = NULL;
824 if (!bfd_get_full_section_contents (abfd, sectp, &data))
41667530
MG
825 {
826 warning (_("Can't read data for section '%s' in file '%s'"),
fd361982 827 bfd_section_name (sectp),
41667530
MG
828 bfd_get_filename (abfd));
829 /* Set size to 0 to prevent further attempts to read the invalid
830 section. */
831 *size = 0;
cafb3438 832 return NULL;
41667530 833 }
ea9f10bb 834 descriptor->data = data;
4bf44c1c
TT
835
836 done:
837 gdb_assert (descriptor->data != NULL);
838 *size = descriptor->size;
9a3c8263 839 return (const gdb_byte *) descriptor->data;
4bf44c1c 840}
64c31149 841
dccee2de
TT
842/* Return 32-bit CRC for ABFD. If successful store it to *FILE_CRC_RETURN and
843 return 1. Otherwise print a warning and return 0. ABFD seek position is
844 not preserved. */
845
846static int
847get_file_crc (bfd *abfd, unsigned long *file_crc_return)
848{
849 unsigned long file_crc = 0;
850
851 if (bfd_seek (abfd, 0, SEEK_SET) != 0)
852 {
853 warning (_("Problem reading \"%s\" for CRC: %s"),
854 bfd_get_filename (abfd), bfd_errmsg (bfd_get_error ()));
855 return 0;
856 }
857
858 for (;;)
859 {
860 gdb_byte buffer[8 * 1024];
861 bfd_size_type count;
862
863 count = bfd_bread (buffer, sizeof (buffer), abfd);
864 if (count == (bfd_size_type) -1)
865 {
866 warning (_("Problem reading \"%s\" for CRC: %s"),
867 bfd_get_filename (abfd), bfd_errmsg (bfd_get_error ()));
868 return 0;
869 }
870 if (count == 0)
871 break;
872 file_crc = bfd_calc_gnu_debuglink_crc32 (file_crc, buffer, count);
873 }
874
875 *file_crc_return = file_crc;
876 return 1;
877}
878
879/* See gdb_bfd.h. */
880
881int
882gdb_bfd_crc (struct bfd *abfd, unsigned long *crc_out)
883{
9a3c8263 884 struct gdb_bfd_data *gdata = (struct gdb_bfd_data *) bfd_usrdata (abfd);
dccee2de
TT
885
886 if (!gdata->crc_computed)
887 gdata->crc_computed = get_file_crc (abfd, &gdata->crc);
888
889 if (gdata->crc_computed)
890 *crc_out = gdata->crc;
891 return gdata->crc_computed;
892}
893
64c31149
TT
894\f
895
896/* See gdb_bfd.h. */
897
192b62ce 898gdb_bfd_ref_ptr
64c31149
TT
899gdb_bfd_fopen (const char *filename, const char *target, const char *mode,
900 int fd)
901{
902 bfd *result = bfd_fopen (filename, target, mode, fd);
903
1831a9f9 904 return gdb_bfd_ref_ptr::new_reference (result);
64c31149
TT
905}
906
907/* See gdb_bfd.h. */
908
192b62ce 909gdb_bfd_ref_ptr
64c31149
TT
910gdb_bfd_openr (const char *filename, const char *target)
911{
912 bfd *result = bfd_openr (filename, target);
913
1831a9f9 914 return gdb_bfd_ref_ptr::new_reference (result);
64c31149
TT
915}
916
917/* See gdb_bfd.h. */
918
192b62ce 919gdb_bfd_ref_ptr
64c31149
TT
920gdb_bfd_openw (const char *filename, const char *target)
921{
922 bfd *result = bfd_openw (filename, target);
923
1831a9f9 924 return gdb_bfd_ref_ptr::new_reference (result);
64c31149
TT
925}
926
927/* See gdb_bfd.h. */
928
192b62ce 929gdb_bfd_ref_ptr
64c31149
TT
930gdb_bfd_openr_iovec (const char *filename, const char *target,
931 void *(*open_func) (struct bfd *nbfd,
932 void *open_closure),
933 void *open_closure,
934 file_ptr (*pread_func) (struct bfd *nbfd,
935 void *stream,
936 void *buf,
937 file_ptr nbytes,
938 file_ptr offset),
939 int (*close_func) (struct bfd *nbfd,
940 void *stream),
941 int (*stat_func) (struct bfd *abfd,
942 void *stream,
943 struct stat *sb))
944{
945 bfd *result = bfd_openr_iovec (filename, target,
946 open_func, open_closure,
947 pread_func, close_func, stat_func);
948
1831a9f9 949 return gdb_bfd_ref_ptr::new_reference (result);
64c31149
TT
950}
951
952/* See gdb_bfd.h. */
953
0cd61f44
TT
954void
955gdb_bfd_mark_parent (bfd *child, bfd *parent)
956{
957 struct gdb_bfd_data *gdata;
958
959 gdb_bfd_ref (child);
960 /* No need to stash the filename here, because we also keep a
961 reference on the parent archive. */
962
9a3c8263 963 gdata = (struct gdb_bfd_data *) bfd_usrdata (child);
0cd61f44
TT
964 if (gdata->archive_bfd == NULL)
965 {
966 gdata->archive_bfd = parent;
967 gdb_bfd_ref (parent);
968 }
969 else
970 gdb_assert (gdata->archive_bfd == parent);
971}
972
973/* See gdb_bfd.h. */
974
192b62ce 975gdb_bfd_ref_ptr
64c31149
TT
976gdb_bfd_openr_next_archived_file (bfd *archive, bfd *previous)
977{
978 bfd *result = bfd_openr_next_archived_file (archive, previous);
979
980 if (result)
0cd61f44 981 gdb_bfd_mark_parent (result, archive);
64c31149 982
192b62ce 983 return gdb_bfd_ref_ptr (result);
64c31149
TT
984}
985
986/* See gdb_bfd.h. */
987
13aaf454
DE
988void
989gdb_bfd_record_inclusion (bfd *includer, bfd *includee)
990{
991 struct gdb_bfd_data *gdata;
992
9a3c8263 993 gdata = (struct gdb_bfd_data *) bfd_usrdata (includer);
1831a9f9 994 gdata->included_bfds.push_back (gdb_bfd_ref_ptr::new_reference (includee));
13aaf454
DE
995}
996
d6b28940
TT
997\f
998
65cf3563
TT
999gdb_static_assert (ARRAY_SIZE (_bfd_std_section) == 4);
1000
1001/* See gdb_bfd.h. */
1002
1003int
1004gdb_bfd_section_index (bfd *abfd, asection *section)
1005{
1006 if (section == NULL)
1007 return -1;
1008 else if (section == bfd_com_section_ptr)
ce9c0ca1 1009 return bfd_count_sections (abfd);
65cf3563 1010 else if (section == bfd_und_section_ptr)
ce9c0ca1 1011 return bfd_count_sections (abfd) + 1;
65cf3563 1012 else if (section == bfd_abs_section_ptr)
ce9c0ca1 1013 return bfd_count_sections (abfd) + 2;
65cf3563 1014 else if (section == bfd_ind_section_ptr)
ce9c0ca1 1015 return bfd_count_sections (abfd) + 3;
65cf3563
TT
1016 return section->index;
1017}
1018
1019/* See gdb_bfd.h. */
1020
1021int
1022gdb_bfd_count_sections (bfd *abfd)
1023{
1024 return bfd_count_sections (abfd) + 4;
1025}
1026
1da77581
TT
1027/* See gdb_bfd.h. */
1028
1029int
1030gdb_bfd_requires_relocations (bfd *abfd)
1031{
9a3c8263 1032 struct gdb_bfd_data *gdata = (struct gdb_bfd_data *) bfd_usrdata (abfd);
1da77581
TT
1033
1034 if (gdata->relocation_computed == 0)
1035 {
1036 asection *sect;
1037
1038 for (sect = abfd->sections; sect != NULL; sect = sect->next)
1039 if ((sect->flags & SEC_RELOC) != 0)
1040 {
1041 gdata->needs_relocations = 1;
1042 break;
1043 }
1044
1045 gdata->relocation_computed = 1;
1046 }
1047
1048 return gdata->needs_relocations;
1049}
1050
e0fc5c3f
SM
1051/* See gdb_bfd.h. */
1052
1053bool
1054gdb_bfd_get_full_section_contents (bfd *abfd, asection *section,
1055 gdb::byte_vector *contents)
1056{
1057 bfd_size_type section_size = bfd_section_size (section);
1058
1059 contents->resize (section_size);
1060
1061 return bfd_get_section_contents (abfd, section, contents->data (), 0,
1062 section_size);
1063}
65cf3563 1064
34b965f7
TT
1065#define AMBIGUOUS_MESS1 ".\nMatching formats:"
1066#define AMBIGUOUS_MESS2 \
1067 ".\nUse \"set gnutarget format-name\" to specify the format."
1068
1069/* See gdb_bfd.h. */
1070
1071std::string
1072gdb_bfd_errmsg (bfd_error_type error_tag, char **matching)
1073{
1074 char **p;
1075
1076 /* Check if errmsg just need simple return. */
1077 if (error_tag != bfd_error_file_ambiguously_recognized || matching == NULL)
1078 return bfd_errmsg (error_tag);
1079
1080 std::string ret (bfd_errmsg (error_tag));
1081 ret += AMBIGUOUS_MESS1;
1082
1083 for (p = matching; *p; p++)
1084 {
1085 ret += " ";
1086 ret += *p;
1087 }
1088 ret += AMBIGUOUS_MESS2;
1089
1090 xfree (matching);
1091
1092 return ret;
1093}
1094
d6b28940
TT
1095/* A callback for htab_traverse that prints a single BFD. */
1096
1097static int
1098print_one_bfd (void **slot, void *data)
1099{
9a3c8263
SM
1100 bfd *abfd = (struct bfd *) *slot;
1101 struct gdb_bfd_data *gdata = (struct gdb_bfd_data *) bfd_usrdata (abfd);
1102 struct ui_out *uiout = (struct ui_out *) data;
d6b28940 1103
2e783024 1104 ui_out_emit_tuple tuple_emitter (uiout, NULL);
381befee 1105 uiout->field_signed ("refcount", gdata->refc);
112e8700 1106 uiout->field_string ("addr", host_address_to_string (abfd));
972f7a4b
TT
1107 uiout->field_string ("filename", bfd_get_filename (abfd),
1108 file_name_style.style ());
112e8700 1109 uiout->text ("\n");
d6b28940
TT
1110
1111 return 1;
1112}
1113
1114/* Implement the 'maint info bfd' command. */
1115
1116static void
e4e33335 1117maintenance_info_bfds (const char *arg, int from_tty)
d6b28940 1118{
d6b28940
TT
1119 struct ui_out *uiout = current_uiout;
1120
4a2b031d 1121 ui_out_emit_table table_emitter (uiout, 3, -1, "bfds");
112e8700
SM
1122 uiout->table_header (10, ui_left, "refcount", "Refcount");
1123 uiout->table_header (18, ui_left, "addr", "Address");
1124 uiout->table_header (40, ui_left, "filename", "Filename");
d6b28940 1125
112e8700 1126 uiout->table_body ();
d6b28940 1127 htab_traverse (all_bfds, print_one_bfd, uiout);
d6b28940
TT
1128}
1129
84e60555
KB
1130/* BFD related per-inferior data. */
1131
1132struct bfd_inferior_data
1133{
1134 std::unordered_map<std::string, unsigned long> bfd_error_string_counts;
1135};
1136
1137/* Per-inferior data key. */
1138
1139static const registry<inferior>::key<bfd_inferior_data> bfd_inferior_data_key;
1140
1141/* Fetch per-inferior BFD data. It always returns a valid pointer to
1142 a bfd_inferior_data struct. */
1143
1144static struct bfd_inferior_data *
1145get_bfd_inferior_data (struct inferior *inf)
1146{
1147 struct bfd_inferior_data *data;
1148
1149 data = bfd_inferior_data_key.get (inf);
1150 if (data == nullptr)
1151 data = bfd_inferior_data_key.emplace (inf);
1152
1153 return data;
1154}
1155
1156/* Increment the BFD error count for STR and return the updated
1157 count. */
1158
1159static unsigned long
1160increment_bfd_error_count (std::string str)
1161{
1162 struct bfd_inferior_data *bid = get_bfd_inferior_data (current_inferior ());
1163
1164 auto &map = bid->bfd_error_string_counts;
1165 return ++map[std::move (str)];
1166}
1167
1168static bfd_error_handler_type default_bfd_error_handler;
1169
1170/* Define a BFD error handler which will suppress the printing of
1171 messages which have been printed once already. This is done on a
1172 per-inferior basis. */
1173
77b7377b 1174static void ATTRIBUTE_PRINTF (1, 0)
84e60555
KB
1175gdb_bfd_error_handler (const char *fmt, va_list ap)
1176{
1177 va_list ap_copy;
1178
1179 va_copy(ap_copy, ap);
1180 const std::string str = string_vprintf (fmt, ap_copy);
1181 va_end (ap_copy);
1182
1183 if (increment_bfd_error_count (std::move (str)) > 1)
1184 return;
1185
1186 /* We must call the BFD mechanism for printing format strings since
1187 it supports additional format specifiers that GDB's vwarning() doesn't
1188 recognize. It also outputs additional text, i.e. "BFD: ", which
1189 makes it clear that it's a BFD warning/error. */
1190 (*default_bfd_error_handler) (fmt, ap);
1191}
1192
6c265988 1193void _initialize_gdb_bfd ();
d6b28940 1194void
6c265988 1195_initialize_gdb_bfd ()
d6b28940
TT
1196{
1197 all_bfds = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
1198 NULL, xcalloc, xfree);
1199
1200 add_cmd ("bfds", class_maintenance, maintenance_info_bfds, _("\
1201List the BFDs that are currently open."),
1202 &maintenanceinfolist);
18989b3c
AB
1203
1204 add_setshow_boolean_cmd ("bfd-sharing", no_class,
1205 &bfd_sharing, _("\
1206Set whether gdb will share bfds that appear to be the same file."), _("\
1207Show whether gdb will share bfds that appear to be the same file."), _("\
1208When enabled gdb will reuse existing bfds rather than reopening the\n\
1209same file. To decide if two files are the same then gdb compares the\n\
1210filename, file size, file modification time, and file inode."),
1211 NULL,
1212 &show_bfd_sharing,
1213 &maintenance_set_cmdlist,
1214 &maintenance_show_cmdlist);
566f5e3b 1215
d4dd4fca
SM
1216 add_setshow_boolean_cmd ("bfd-cache", class_maintenance,
1217 &debug_bfd_cache,
1218 _("Set bfd cache debugging."),
1219 _("Show bfd cache debugging."),
1220 _("\
566f5e3b 1221When non-zero, bfd cache specific debugging is enabled."),
d4dd4fca
SM
1222 NULL,
1223 &show_bfd_cache_debug,
1224 &setdebuglist, &showdebuglist);
84e60555
KB
1225
1226 /* Hook the BFD error/warning handler to limit amount of output. */
1227 default_bfd_error_handler = bfd_set_error_handler (gdb_bfd_error_handler);
d6b28940 1228}