Use "new" to allocate gdb_bfd_data
[deliverable/binutils-gdb.git] / gdb / gdb_bfd.c
CommitLineData
cbb099e8
TT
1/* Definitions for BFD wrappers used by GDB.
2
61baf725 3 Copyright (C) 2011-2017 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"
614c279d 25#include "filestuff.h"
13aaf454 26#include "vec.h"
4bf44c1c
TT
27#ifdef HAVE_MMAP
28#include <sys/mman.h>
29#ifndef MAP_FAILED
30#define MAP_FAILED ((void *) -1)
31#endif
32#endif
f08e97fe
GB
33#include "target.h"
34#include "gdb/fileio.h"
07c138c8 35#include "inferior.h"
4bf44c1c 36
13aaf454
DE
37typedef bfd *bfdp;
38DEF_VEC_P (bfdp);
39
4bf44c1c
TT
40/* An object of this type is stored in the section's user data when
41 mapping a section. */
42
43struct gdb_bfd_section_data
44{
45 /* Size of the data. */
46 bfd_size_type size;
47 /* If the data was mmapped, this is the length of the map. */
48 bfd_size_type map_len;
49 /* The data. If NULL, the section data has not been read. */
50 void *data;
51 /* If the data was mmapped, this is the map address. */
52 void *map_addr;
53};
a4453b7e 54
d6b28940
TT
55/* A hash table holding every BFD that gdb knows about. This is not
56 to be confused with 'gdb_bfd_cache', which is used for sharing
57 BFDs; in contrast, this hash is used just to implement
58 "maint info bfd". */
59
60static htab_t all_bfds;
61
6ec53d05
TT
62/* An object of this type is stored in each BFD's user data. */
63
64struct gdb_bfd_data
65{
06d5bbc8
TT
66 gdb_bfd_data (bfd *abfd)
67 : mtime (bfd_get_mtime (abfd)),
68 size (bfd_get_size (abfd)),
69 relocation_computed (0),
70 needs_relocations (0),
71 crc_computed (0)
72 {
73 struct stat buf;
74
75 if (bfd_stat (abfd, &buf) == 0)
76 {
77 inode = buf.st_ino;
78 device_id = buf.st_dev;
79 }
80 else
81 {
82 /* The stat failed. */
83 inode = 0;
84 device_id = 0;
85 }
86 }
87
88 ~gdb_bfd_data ()
89 {
90 int ix;
91 bfd *included_bfd;
92
93 for (ix = 0;
94 VEC_iterate (bfdp, included_bfds, ix, included_bfd);
95 ++ix)
96 gdb_bfd_unref (included_bfd);
97 VEC_free (bfdp, included_bfds);
98 }
99
6ec53d05 100 /* The reference count. */
06d5bbc8 101 int refc = 1;
6ec53d05
TT
102
103 /* The mtime of the BFD at the point the cache entry was made. */
104 time_t mtime;
b82d08cd 105
c04fe68f
AB
106 /* The file size (in bytes) at the point the cache entry was made. */
107 off_t size;
108
109 /* The inode of the file at the point the cache entry was made. */
110 ino_t inode;
111
112 /* The device id of the file at the point the cache entry was made. */
113 dev_t device_id;
114
1da77581
TT
115 /* This is true if we have determined whether this BFD has any
116 sections requiring relocation. */
117 unsigned int relocation_computed : 1;
118
119 /* This is true if any section needs relocation. */
120 unsigned int needs_relocations : 1;
121
dccee2de
TT
122 /* This is true if we have successfully computed the file's CRC. */
123 unsigned int crc_computed : 1;
124
125 /* The file's CRC. */
06d5bbc8 126 unsigned long crc = 0;
dccee2de 127
b82d08cd
TT
128 /* If the BFD comes from an archive, this points to the archive's
129 BFD. Otherwise, this is NULL. */
06d5bbc8 130 bfd *archive_bfd = nullptr;
e992eda4 131
13aaf454 132 /* Table of all the bfds this bfd has included. */
06d5bbc8 133 VEC (bfdp) *included_bfds = nullptr;
13aaf454 134
e992eda4 135 /* The registry. */
06d5bbc8 136 REGISTRY_FIELDS = {};
6ec53d05
TT
137};
138
e992eda4
TT
139#define GDB_BFD_DATA_ACCESSOR(ABFD) \
140 ((struct gdb_bfd_data *) bfd_usrdata (ABFD))
141
142DEFINE_REGISTRY (bfd, GDB_BFD_DATA_ACCESSOR)
143
6ec53d05
TT
144/* A hash table storing all the BFDs maintained in the cache. */
145
146static htab_t gdb_bfd_cache;
147
18989b3c
AB
148/* When true gdb will reuse an existing bfd object if the filename,
149 modification time, and file size all match. */
150
151static int bfd_sharing = 1;
152static void
153show_bfd_sharing (struct ui_file *file, int from_tty,
154 struct cmd_list_element *c, const char *value)
155{
156 fprintf_filtered (file, _("BFD sharing is %s.\n"), value);
157}
158
566f5e3b
AB
159/* When non-zero debugging of the bfd caches is enabled. */
160
161static unsigned int debug_bfd_cache;
162static void
163show_bfd_cache_debug (struct ui_file *file, int from_tty,
164 struct cmd_list_element *c, const char *value)
165{
166 fprintf_filtered (file, _("BFD cache debugging is %s.\n"), value);
167}
168
6ec53d05
TT
169/* The type of an object being looked up in gdb_bfd_cache. We use
170 htab's capability of storing one kind of object (BFD in this case)
171 and using a different sort of object for searching. */
172
173struct gdb_bfd_cache_search
174{
175 /* The filename. */
176 const char *filename;
177 /* The mtime. */
178 time_t mtime;
c04fe68f
AB
179 /* The file size (in bytes). */
180 off_t size;
181 /* The inode of the file. */
182 ino_t inode;
183 /* The device id of the file. */
184 dev_t device_id;
6ec53d05
TT
185};
186
187/* A hash function for BFDs. */
188
189static hashval_t
190hash_bfd (const void *b)
191{
9a3c8263 192 const bfd *abfd = (const struct bfd *) b;
6ec53d05
TT
193
194 /* It is simplest to just hash the filename. */
195 return htab_hash_string (bfd_get_filename (abfd));
196}
197
198/* An equality function for BFDs. Note that this expects the caller
199 to search using struct gdb_bfd_cache_search only, not BFDs. */
200
201static int
202eq_bfd (const void *a, const void *b)
203{
9a3c8263
SM
204 const bfd *abfd = (const struct bfd *) a;
205 const struct gdb_bfd_cache_search *s
206 = (const struct gdb_bfd_cache_search *) b;
207 struct gdb_bfd_data *gdata = (struct gdb_bfd_data *) bfd_usrdata (abfd);
6ec53d05
TT
208
209 return (gdata->mtime == s->mtime
c04fe68f
AB
210 && gdata->size == s->size
211 && gdata->inode == s->inode
212 && gdata->device_id == s->device_id
6ec53d05
TT
213 && strcmp (bfd_get_filename (abfd), s->filename) == 0);
214}
215
216/* See gdb_bfd.h. */
217
f08e97fe
GB
218int
219is_target_filename (const char *name)
220{
221 return startswith (name, TARGET_SYSROOT_PREFIX);
222}
223
224/* See gdb_bfd.h. */
225
226int
227gdb_bfd_has_target_filename (struct bfd *abfd)
228{
229 return is_target_filename (bfd_get_filename (abfd));
230}
231
232
233/* Return the system error number corresponding to ERRNUM. */
234
235static int
236fileio_errno_to_host (int errnum)
237{
238 switch (errnum)
239 {
240 case FILEIO_EPERM:
241 return EPERM;
242 case FILEIO_ENOENT:
243 return ENOENT;
244 case FILEIO_EINTR:
245 return EINTR;
246 case FILEIO_EIO:
247 return EIO;
248 case FILEIO_EBADF:
249 return EBADF;
250 case FILEIO_EACCES:
251 return EACCES;
252 case FILEIO_EFAULT:
253 return EFAULT;
254 case FILEIO_EBUSY:
255 return EBUSY;
256 case FILEIO_EEXIST:
257 return EEXIST;
258 case FILEIO_ENODEV:
259 return ENODEV;
260 case FILEIO_ENOTDIR:
261 return ENOTDIR;
262 case FILEIO_EISDIR:
263 return EISDIR;
264 case FILEIO_EINVAL:
265 return EINVAL;
266 case FILEIO_ENFILE:
267 return ENFILE;
268 case FILEIO_EMFILE:
269 return EMFILE;
270 case FILEIO_EFBIG:
271 return EFBIG;
272 case FILEIO_ENOSPC:
273 return ENOSPC;
274 case FILEIO_ESPIPE:
275 return ESPIPE;
276 case FILEIO_EROFS:
277 return EROFS;
278 case FILEIO_ENOSYS:
279 return ENOSYS;
280 case FILEIO_ENAMETOOLONG:
281 return ENAMETOOLONG;
282 }
283 return -1;
284}
285
286/* Wrapper for target_fileio_open suitable for passing as the
287 OPEN_FUNC argument to gdb_bfd_openr_iovec. The supplied
288 OPEN_CLOSURE is unused. */
289
290static void *
07c138c8 291gdb_bfd_iovec_fileio_open (struct bfd *abfd, void *inferior)
f08e97fe
GB
292{
293 const char *filename = bfd_get_filename (abfd);
294 int fd, target_errno;
295 int *stream;
296
297 gdb_assert (is_target_filename (filename));
298
4313b8c0
GB
299 fd = target_fileio_open_warn_if_slow ((struct inferior *) inferior,
300 filename
301 + strlen (TARGET_SYSROOT_PREFIX),
302 FILEIO_O_RDONLY, 0,
303 &target_errno);
f08e97fe
GB
304 if (fd == -1)
305 {
306 errno = fileio_errno_to_host (target_errno);
307 bfd_set_error (bfd_error_system_call);
308 return NULL;
309 }
310
311 stream = XCNEW (int);
312 *stream = fd;
313 return stream;
314}
315
316/* Wrapper for target_fileio_pread suitable for passing as the
317 PREAD_FUNC argument to gdb_bfd_openr_iovec. */
318
319static file_ptr
320gdb_bfd_iovec_fileio_pread (struct bfd *abfd, void *stream, void *buf,
321 file_ptr nbytes, file_ptr offset)
322{
323 int fd = *(int *) stream;
324 int target_errno;
325 file_ptr pos, bytes;
326
327 pos = 0;
328 while (nbytes > pos)
329 {
2d7711a3
GB
330 QUIT;
331
f08e97fe
GB
332 bytes = target_fileio_pread (fd, (gdb_byte *) buf + pos,
333 nbytes - pos, offset + pos,
334 &target_errno);
335 if (bytes == 0)
336 /* Success, but no bytes, means end-of-file. */
337 break;
338 if (bytes == -1)
339 {
340 errno = fileio_errno_to_host (target_errno);
341 bfd_set_error (bfd_error_system_call);
342 return -1;
343 }
344
345 pos += bytes;
346 }
347
348 return pos;
349}
350
351/* Wrapper for target_fileio_close suitable for passing as the
352 CLOSE_FUNC argument to gdb_bfd_openr_iovec. */
353
354static int
355gdb_bfd_iovec_fileio_close (struct bfd *abfd, void *stream)
356{
357 int fd = *(int *) stream;
358 int target_errno;
359
360 xfree (stream);
361
362 /* Ignore errors on close. These may happen with remote
363 targets if the connection has already been torn down. */
364 target_fileio_close (fd, &target_errno);
365
366 /* Zero means success. */
367 return 0;
368}
369
370/* Wrapper for target_fileio_fstat suitable for passing as the
371 STAT_FUNC argument to gdb_bfd_openr_iovec. */
372
373static int
374gdb_bfd_iovec_fileio_fstat (struct bfd *abfd, void *stream,
375 struct stat *sb)
376{
377 int fd = *(int *) stream;
378 int target_errno;
379 int result;
380
381 result = target_fileio_fstat (fd, sb, &target_errno);
382 if (result == -1)
383 {
384 errno = fileio_errno_to_host (target_errno);
385 bfd_set_error (bfd_error_system_call);
386 }
387
388 return result;
389}
390
391/* See gdb_bfd.h. */
392
192b62ce 393gdb_bfd_ref_ptr
6ec53d05
TT
394gdb_bfd_open (const char *name, const char *target, int fd)
395{
396 hashval_t hash;
397 void **slot;
398 bfd *abfd;
399 struct gdb_bfd_cache_search search;
400 struct stat st;
401
f08e97fe
GB
402 if (is_target_filename (name))
403 {
404 if (!target_filesystem_is_local ())
405 {
406 gdb_assert (fd == -1);
407
e3dd7556 408 return gdb_bfd_openr_iovec (name, target,
07c138c8
GB
409 gdb_bfd_iovec_fileio_open,
410 current_inferior (),
f08e97fe
GB
411 gdb_bfd_iovec_fileio_pread,
412 gdb_bfd_iovec_fileio_close,
413 gdb_bfd_iovec_fileio_fstat);
f08e97fe
GB
414 }
415
416 name += strlen (TARGET_SYSROOT_PREFIX);
417 }
418
6ec53d05
TT
419 if (gdb_bfd_cache == NULL)
420 gdb_bfd_cache = htab_create_alloc (1, hash_bfd, eq_bfd, NULL,
421 xcalloc, xfree);
422
423 if (fd == -1)
424 {
614c279d 425 fd = gdb_open_cloexec (name, O_RDONLY | O_BINARY, 0);
6ec53d05
TT
426 if (fd == -1)
427 {
428 bfd_set_error (bfd_error_system_call);
429 return NULL;
430 }
431 }
432
433 search.filename = name;
434 if (fstat (fd, &st) < 0)
435 {
436 /* Weird situation here. */
437 search.mtime = 0;
c04fe68f
AB
438 search.size = 0;
439 search.inode = 0;
440 search.device_id = 0;
6ec53d05
TT
441 }
442 else
c04fe68f
AB
443 {
444 search.mtime = st.st_mtime;
445 search.size = st.st_size;
446 search.inode = st.st_ino;
447 search.device_id = st.st_dev;
448 }
6ec53d05
TT
449
450 /* Note that this must compute the same result as hash_bfd. */
451 hash = htab_hash_string (name);
452 /* Note that we cannot use htab_find_slot_with_hash here, because
453 opening the BFD may fail; and this would violate hashtab
454 invariants. */
9a3c8263 455 abfd = (struct bfd *) htab_find_with_hash (gdb_bfd_cache, &search, hash);
18989b3c 456 if (bfd_sharing && abfd != NULL)
6ec53d05 457 {
566f5e3b
AB
458 if (debug_bfd_cache)
459 fprintf_unfiltered (gdb_stdlog,
460 "Reusing cached bfd %s for %s\n",
461 host_address_to_string (abfd),
462 bfd_get_filename (abfd));
6ec53d05 463 close (fd);
2712ce2e 464 return new_bfd_ref (abfd);
6ec53d05
TT
465 }
466
467 abfd = bfd_fopen (name, target, FOPEN_RB, fd);
468 if (abfd == NULL)
469 return NULL;
470
566f5e3b
AB
471 if (debug_bfd_cache)
472 fprintf_unfiltered (gdb_stdlog,
473 "Creating new bfd %s for %s\n",
474 host_address_to_string (abfd),
475 bfd_get_filename (abfd));
476
18989b3c
AB
477 if (bfd_sharing)
478 {
479 slot = htab_find_slot_with_hash (gdb_bfd_cache, &search, hash, INSERT);
480 gdb_assert (!*slot);
481 *slot = abfd;
482 }
6ec53d05 483
2712ce2e 484 return new_bfd_ref (abfd);
6ec53d05
TT
485}
486
4bf44c1c
TT
487/* A helper function that releases any section data attached to the
488 BFD. */
489
490static void
491free_one_bfd_section (bfd *abfd, asection *sectp, void *ignore)
492{
9a3c8263
SM
493 struct gdb_bfd_section_data *sect
494 = (struct gdb_bfd_section_data *) bfd_get_section_userdata (abfd, sectp);
4bf44c1c
TT
495
496 if (sect != NULL && sect->data != NULL)
497 {
498#ifdef HAVE_MMAP
499 if (sect->map_addr != NULL)
500 {
501 int res;
502
503 res = munmap (sect->map_addr, sect->map_len);
504 gdb_assert (res == 0);
505 }
506 else
507#endif
508 xfree (sect->data);
509 }
510}
511
cbb099e8
TT
512/* Close ABFD, and warn if that fails. */
513
514static int
515gdb_bfd_close_or_warn (struct bfd *abfd)
516{
517 int ret;
518 char *name = bfd_get_filename (abfd);
519
4bf44c1c
TT
520 bfd_map_over_sections (abfd, free_one_bfd_section, NULL);
521
cbb099e8
TT
522 ret = bfd_close (abfd);
523
524 if (!ret)
525 warning (_("cannot close \"%s\": %s"),
526 name, bfd_errmsg (bfd_get_error ()));
527
528 return ret;
529}
530
596f7d67 531/* See gdb_bfd.h. */
cbb099e8 532
520b0001 533void
cbb099e8
TT
534gdb_bfd_ref (struct bfd *abfd)
535{
6ec53d05 536 struct gdb_bfd_data *gdata;
d6b28940 537 void **slot;
cbb099e8
TT
538
539 if (abfd == NULL)
520b0001 540 return;
cbb099e8 541
9a3c8263 542 gdata = (struct gdb_bfd_data *) bfd_usrdata (abfd);
cbb099e8 543
566f5e3b
AB
544 if (debug_bfd_cache)
545 fprintf_unfiltered (gdb_stdlog,
546 "Increase reference count on bfd %s (%s)\n",
547 host_address_to_string (abfd),
548 bfd_get_filename (abfd));
549
6ec53d05 550 if (gdata != NULL)
cbb099e8 551 {
6ec53d05 552 gdata->refc += 1;
520b0001 553 return;
cbb099e8
TT
554 }
555
ea9f10bb
TT
556 /* Ask BFD to decompress sections in bfd_get_full_section_contents. */
557 abfd->flags |= BFD_DECOMPRESS;
558
06d5bbc8 559 gdata = new gdb_bfd_data (abfd);
6ec53d05 560 bfd_usrdata (abfd) = gdata;
e992eda4
TT
561 bfd_alloc_data (abfd);
562
d6b28940
TT
563 /* This is the first we've seen it, so add it to the hash table. */
564 slot = htab_find_slot (all_bfds, abfd, INSERT);
565 gdb_assert (slot && !*slot);
566 *slot = abfd;
cbb099e8
TT
567}
568
596f7d67 569/* See gdb_bfd.h. */
cbb099e8
TT
570
571void
572gdb_bfd_unref (struct bfd *abfd)
573{
6ec53d05
TT
574 struct gdb_bfd_data *gdata;
575 struct gdb_bfd_cache_search search;
06d5bbc8 576 bfd *archive_bfd;
cbb099e8
TT
577
578 if (abfd == NULL)
579 return;
580
9a3c8263 581 gdata = (struct gdb_bfd_data *) bfd_usrdata (abfd);
6ec53d05 582 gdb_assert (gdata->refc >= 1);
cbb099e8 583
6ec53d05
TT
584 gdata->refc -= 1;
585 if (gdata->refc > 0)
566f5e3b
AB
586 {
587 if (debug_bfd_cache)
588 fprintf_unfiltered (gdb_stdlog,
589 "Decrease reference count on bfd %s (%s)\n",
590 host_address_to_string (abfd),
591 bfd_get_filename (abfd));
592 return;
593 }
594
595 if (debug_bfd_cache)
596 fprintf_unfiltered (gdb_stdlog,
597 "Delete final reference count on bfd %s (%s)\n",
598 host_address_to_string (abfd),
599 bfd_get_filename (abfd));
cbb099e8 600
b82d08cd 601 archive_bfd = gdata->archive_bfd;
6ec53d05
TT
602 search.filename = bfd_get_filename (abfd);
603
604 if (gdb_bfd_cache && search.filename)
605 {
606 hashval_t hash = htab_hash_string (search.filename);
607 void **slot;
608
609 search.mtime = gdata->mtime;
c04fe68f
AB
610 search.size = gdata->size;
611 search.inode = gdata->inode;
612 search.device_id = gdata->device_id;
6ec53d05
TT
613 slot = htab_find_slot_with_hash (gdb_bfd_cache, &search, hash,
614 NO_INSERT);
615
616 if (slot && *slot)
617 htab_clear_slot (gdb_bfd_cache, slot);
618 }
619
e992eda4 620 bfd_free_data (abfd);
06d5bbc8 621 delete gdata;
cbb099e8
TT
622 bfd_usrdata (abfd) = NULL; /* Paranoia. */
623
d6b28940
TT
624 htab_remove_elt (all_bfds, abfd);
625
cbb099e8 626 gdb_bfd_close_or_warn (abfd);
b82d08cd
TT
627
628 gdb_bfd_unref (archive_bfd);
cbb099e8 629}
4bf44c1c
TT
630
631/* A helper function that returns the section data descriptor
632 associated with SECTION. If no such descriptor exists, a new one
633 is allocated and cleared. */
634
635static struct gdb_bfd_section_data *
636get_section_descriptor (asection *section)
637{
638 struct gdb_bfd_section_data *result;
639
9a3c8263
SM
640 result = ((struct gdb_bfd_section_data *)
641 bfd_get_section_userdata (section->owner, section));
4bf44c1c
TT
642
643 if (result == NULL)
644 {
224c3ddb
SM
645 result = ((struct gdb_bfd_section_data *)
646 bfd_zalloc (section->owner, sizeof (*result)));
4bf44c1c
TT
647 bfd_set_section_userdata (section->owner, section, result);
648 }
649
650 return result;
651}
652
4bf44c1c
TT
653/* See gdb_bfd.h. */
654
655const gdb_byte *
656gdb_bfd_map_section (asection *sectp, bfd_size_type *size)
657{
658 bfd *abfd;
4bf44c1c 659 struct gdb_bfd_section_data *descriptor;
ea9f10bb 660 bfd_byte *data;
4bf44c1c
TT
661
662 gdb_assert ((sectp->flags & SEC_RELOC) == 0);
663 gdb_assert (size != NULL);
664
665 abfd = sectp->owner;
666
667 descriptor = get_section_descriptor (sectp);
668
669 /* If the data was already read for this BFD, just reuse it. */
670 if (descriptor->data != NULL)
671 goto done;
672
ea9f10bb
TT
673#ifdef HAVE_MMAP
674 if (!bfd_is_section_compressed (abfd, sectp))
4bf44c1c 675 {
ea9f10bb
TT
676 /* The page size, used when mmapping. */
677 static int pagesize;
4bf44c1c 678
ea9f10bb
TT
679 if (pagesize == 0)
680 pagesize = getpagesize ();
681
682 /* Only try to mmap sections which are large enough: we don't want
683 to waste space due to fragmentation. */
684
685 if (bfd_get_section_size (sectp) > 4 * pagesize)
686 {
687 descriptor->size = bfd_get_section_size (sectp);
688 descriptor->data = bfd_mmap (abfd, 0, descriptor->size, PROT_READ,
689 MAP_PRIVATE, sectp->filepos,
690 &descriptor->map_addr,
691 &descriptor->map_len);
692
693 if ((caddr_t)descriptor->data != MAP_FAILED)
694 {
4bf44c1c 695#if HAVE_POSIX_MADVISE
ea9f10bb
TT
696 posix_madvise (descriptor->map_addr, descriptor->map_len,
697 POSIX_MADV_WILLNEED);
4bf44c1c 698#endif
ea9f10bb
TT
699 goto done;
700 }
4bf44c1c 701
ea9f10bb
TT
702 /* On failure, clear out the section data and try again. */
703 memset (descriptor, 0, sizeof (*descriptor));
704 }
705 }
4bf44c1c
TT
706#endif /* HAVE_MMAP */
707
ea9f10bb
TT
708 /* Handle compressed sections, or ordinary uncompressed sections in
709 the no-mmap case. */
4bf44c1c
TT
710
711 descriptor->size = bfd_get_section_size (sectp);
ea9f10bb 712 descriptor->data = NULL;
4bf44c1c 713
ea9f10bb
TT
714 data = NULL;
715 if (!bfd_get_full_section_contents (abfd, sectp, &data))
716 error (_("Can't read data for section '%s' in file '%s'"),
717 bfd_get_section_name (abfd, sectp),
718 bfd_get_filename (abfd));
719 descriptor->data = data;
4bf44c1c
TT
720
721 done:
722 gdb_assert (descriptor->data != NULL);
723 *size = descriptor->size;
9a3c8263 724 return (const gdb_byte *) descriptor->data;
4bf44c1c 725}
64c31149 726
dccee2de
TT
727/* Return 32-bit CRC for ABFD. If successful store it to *FILE_CRC_RETURN and
728 return 1. Otherwise print a warning and return 0. ABFD seek position is
729 not preserved. */
730
731static int
732get_file_crc (bfd *abfd, unsigned long *file_crc_return)
733{
734 unsigned long file_crc = 0;
735
736 if (bfd_seek (abfd, 0, SEEK_SET) != 0)
737 {
738 warning (_("Problem reading \"%s\" for CRC: %s"),
739 bfd_get_filename (abfd), bfd_errmsg (bfd_get_error ()));
740 return 0;
741 }
742
743 for (;;)
744 {
745 gdb_byte buffer[8 * 1024];
746 bfd_size_type count;
747
748 count = bfd_bread (buffer, sizeof (buffer), abfd);
749 if (count == (bfd_size_type) -1)
750 {
751 warning (_("Problem reading \"%s\" for CRC: %s"),
752 bfd_get_filename (abfd), bfd_errmsg (bfd_get_error ()));
753 return 0;
754 }
755 if (count == 0)
756 break;
757 file_crc = bfd_calc_gnu_debuglink_crc32 (file_crc, buffer, count);
758 }
759
760 *file_crc_return = file_crc;
761 return 1;
762}
763
764/* See gdb_bfd.h. */
765
766int
767gdb_bfd_crc (struct bfd *abfd, unsigned long *crc_out)
768{
9a3c8263 769 struct gdb_bfd_data *gdata = (struct gdb_bfd_data *) bfd_usrdata (abfd);
dccee2de
TT
770
771 if (!gdata->crc_computed)
772 gdata->crc_computed = get_file_crc (abfd, &gdata->crc);
773
774 if (gdata->crc_computed)
775 *crc_out = gdata->crc;
776 return gdata->crc_computed;
777}
778
64c31149
TT
779\f
780
781/* See gdb_bfd.h. */
782
192b62ce 783gdb_bfd_ref_ptr
64c31149
TT
784gdb_bfd_fopen (const char *filename, const char *target, const char *mode,
785 int fd)
786{
787 bfd *result = bfd_fopen (filename, target, mode, fd);
788
2712ce2e 789 return new_bfd_ref (result);
64c31149
TT
790}
791
792/* See gdb_bfd.h. */
793
192b62ce 794gdb_bfd_ref_ptr
64c31149
TT
795gdb_bfd_openr (const char *filename, const char *target)
796{
797 bfd *result = bfd_openr (filename, target);
798
2712ce2e 799 return new_bfd_ref (result);
64c31149
TT
800}
801
802/* See gdb_bfd.h. */
803
192b62ce 804gdb_bfd_ref_ptr
64c31149
TT
805gdb_bfd_openw (const char *filename, const char *target)
806{
807 bfd *result = bfd_openw (filename, target);
808
2712ce2e 809 return new_bfd_ref (result);
64c31149
TT
810}
811
812/* See gdb_bfd.h. */
813
192b62ce 814gdb_bfd_ref_ptr
64c31149
TT
815gdb_bfd_openr_iovec (const char *filename, const char *target,
816 void *(*open_func) (struct bfd *nbfd,
817 void *open_closure),
818 void *open_closure,
819 file_ptr (*pread_func) (struct bfd *nbfd,
820 void *stream,
821 void *buf,
822 file_ptr nbytes,
823 file_ptr offset),
824 int (*close_func) (struct bfd *nbfd,
825 void *stream),
826 int (*stat_func) (struct bfd *abfd,
827 void *stream,
828 struct stat *sb))
829{
830 bfd *result = bfd_openr_iovec (filename, target,
831 open_func, open_closure,
832 pread_func, close_func, stat_func);
833
2712ce2e 834 return new_bfd_ref (result);
64c31149
TT
835}
836
837/* See gdb_bfd.h. */
838
0cd61f44
TT
839void
840gdb_bfd_mark_parent (bfd *child, bfd *parent)
841{
842 struct gdb_bfd_data *gdata;
843
844 gdb_bfd_ref (child);
845 /* No need to stash the filename here, because we also keep a
846 reference on the parent archive. */
847
9a3c8263 848 gdata = (struct gdb_bfd_data *) bfd_usrdata (child);
0cd61f44
TT
849 if (gdata->archive_bfd == NULL)
850 {
851 gdata->archive_bfd = parent;
852 gdb_bfd_ref (parent);
853 }
854 else
855 gdb_assert (gdata->archive_bfd == parent);
856}
857
858/* See gdb_bfd.h. */
859
192b62ce 860gdb_bfd_ref_ptr
64c31149
TT
861gdb_bfd_openr_next_archived_file (bfd *archive, bfd *previous)
862{
863 bfd *result = bfd_openr_next_archived_file (archive, previous);
864
865 if (result)
0cd61f44 866 gdb_bfd_mark_parent (result, archive);
64c31149 867
192b62ce 868 return gdb_bfd_ref_ptr (result);
64c31149
TT
869}
870
871/* See gdb_bfd.h. */
872
13aaf454
DE
873void
874gdb_bfd_record_inclusion (bfd *includer, bfd *includee)
875{
876 struct gdb_bfd_data *gdata;
877
878 gdb_bfd_ref (includee);
9a3c8263 879 gdata = (struct gdb_bfd_data *) bfd_usrdata (includer);
13aaf454
DE
880 VEC_safe_push (bfdp, gdata->included_bfds, includee);
881}
882
883/* See gdb_bfd.h. */
884
192b62ce 885gdb_bfd_ref_ptr
64c31149
TT
886gdb_bfd_fdopenr (const char *filename, const char *target, int fd)
887{
888 bfd *result = bfd_fdopenr (filename, target, fd);
889
2712ce2e 890 return new_bfd_ref (result);
64c31149 891}
d6b28940
TT
892
893\f
894
65cf3563
TT
895gdb_static_assert (ARRAY_SIZE (_bfd_std_section) == 4);
896
897/* See gdb_bfd.h. */
898
899int
900gdb_bfd_section_index (bfd *abfd, asection *section)
901{
902 if (section == NULL)
903 return -1;
904 else if (section == bfd_com_section_ptr)
ce9c0ca1 905 return bfd_count_sections (abfd);
65cf3563 906 else if (section == bfd_und_section_ptr)
ce9c0ca1 907 return bfd_count_sections (abfd) + 1;
65cf3563 908 else if (section == bfd_abs_section_ptr)
ce9c0ca1 909 return bfd_count_sections (abfd) + 2;
65cf3563 910 else if (section == bfd_ind_section_ptr)
ce9c0ca1 911 return bfd_count_sections (abfd) + 3;
65cf3563
TT
912 return section->index;
913}
914
915/* See gdb_bfd.h. */
916
917int
918gdb_bfd_count_sections (bfd *abfd)
919{
920 return bfd_count_sections (abfd) + 4;
921}
922
1da77581
TT
923/* See gdb_bfd.h. */
924
925int
926gdb_bfd_requires_relocations (bfd *abfd)
927{
9a3c8263 928 struct gdb_bfd_data *gdata = (struct gdb_bfd_data *) bfd_usrdata (abfd);
1da77581
TT
929
930 if (gdata->relocation_computed == 0)
931 {
932 asection *sect;
933
934 for (sect = abfd->sections; sect != NULL; sect = sect->next)
935 if ((sect->flags & SEC_RELOC) != 0)
936 {
937 gdata->needs_relocations = 1;
938 break;
939 }
940
941 gdata->relocation_computed = 1;
942 }
943
944 return gdata->needs_relocations;
945}
946
65cf3563
TT
947\f
948
d6b28940
TT
949/* A callback for htab_traverse that prints a single BFD. */
950
951static int
952print_one_bfd (void **slot, void *data)
953{
9a3c8263
SM
954 bfd *abfd = (struct bfd *) *slot;
955 struct gdb_bfd_data *gdata = (struct gdb_bfd_data *) bfd_usrdata (abfd);
956 struct ui_out *uiout = (struct ui_out *) data;
d6b28940 957
2e783024 958 ui_out_emit_tuple tuple_emitter (uiout, NULL);
112e8700
SM
959 uiout->field_int ("refcount", gdata->refc);
960 uiout->field_string ("addr", host_address_to_string (abfd));
961 uiout->field_string ("filename", bfd_get_filename (abfd));
962 uiout->text ("\n");
d6b28940
TT
963
964 return 1;
965}
966
967/* Implement the 'maint info bfd' command. */
968
969static void
e4e33335 970maintenance_info_bfds (const char *arg, int from_tty)
d6b28940 971{
d6b28940
TT
972 struct ui_out *uiout = current_uiout;
973
4a2b031d 974 ui_out_emit_table table_emitter (uiout, 3, -1, "bfds");
112e8700
SM
975 uiout->table_header (10, ui_left, "refcount", "Refcount");
976 uiout->table_header (18, ui_left, "addr", "Address");
977 uiout->table_header (40, ui_left, "filename", "Filename");
d6b28940 978
112e8700 979 uiout->table_body ();
d6b28940 980 htab_traverse (all_bfds, print_one_bfd, uiout);
d6b28940
TT
981}
982
d6b28940
TT
983void
984_initialize_gdb_bfd (void)
985{
986 all_bfds = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
987 NULL, xcalloc, xfree);
988
989 add_cmd ("bfds", class_maintenance, maintenance_info_bfds, _("\
990List the BFDs that are currently open."),
991 &maintenanceinfolist);
18989b3c
AB
992
993 add_setshow_boolean_cmd ("bfd-sharing", no_class,
994 &bfd_sharing, _("\
995Set whether gdb will share bfds that appear to be the same file."), _("\
996Show whether gdb will share bfds that appear to be the same file."), _("\
997When enabled gdb will reuse existing bfds rather than reopening the\n\
998same file. To decide if two files are the same then gdb compares the\n\
999filename, file size, file modification time, and file inode."),
1000 NULL,
1001 &show_bfd_sharing,
1002 &maintenance_set_cmdlist,
1003 &maintenance_show_cmdlist);
566f5e3b
AB
1004
1005 add_setshow_zuinteger_cmd ("bfd-cache", class_maintenance,
1006 &debug_bfd_cache, _("\
1007Set bfd cache debugging."), _("\
1008Show bfd cache debugging."), _("\
1009When non-zero, bfd cache specific debugging is enabled."),
1010 NULL,
1011 &show_bfd_cache_debug,
1012 &setdebuglist, &showdebuglist);
d6b28940 1013}
This page took 0.456005 seconds and 4 git commands to generate.