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