GDB (API)
|
00001 /* Definitions for BFD wrappers used by GDB. 00002 00003 Copyright (C) 2011-2013 Free Software Foundation, Inc. 00004 00005 This file is part of GDB. 00006 00007 This program is free software; you can redistribute it and/or modify 00008 it under the terms of the GNU General Public License as published by 00009 the Free Software Foundation; either version 3 of the License, or 00010 (at your option) any later version. 00011 00012 This program is distributed in the hope that it will be useful, 00013 but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 GNU General Public License for more details. 00016 00017 You should have received a copy of the GNU General Public License 00018 along with this program. If not, see <http://www.gnu.org/licenses/>. */ 00019 00020 #include "defs.h" 00021 #include "gdb_bfd.h" 00022 #include "gdb_assert.h" 00023 #include "gdb_string.h" 00024 #include "ui-out.h" 00025 #include "gdbcmd.h" 00026 #include "hashtab.h" 00027 #include "filestuff.h" 00028 #ifdef HAVE_ZLIB_H 00029 #include <zlib.h> 00030 #endif 00031 #ifdef HAVE_MMAP 00032 #include <sys/mman.h> 00033 #ifndef MAP_FAILED 00034 #define MAP_FAILED ((void *) -1) 00035 #endif 00036 #endif 00037 00038 /* An object of this type is stored in the section's user data when 00039 mapping a section. */ 00040 00041 struct gdb_bfd_section_data 00042 { 00043 /* Size of the data. */ 00044 bfd_size_type size; 00045 /* If the data was mmapped, this is the length of the map. */ 00046 bfd_size_type map_len; 00047 /* The data. If NULL, the section data has not been read. */ 00048 void *data; 00049 /* If the data was mmapped, this is the map address. */ 00050 void *map_addr; 00051 }; 00052 00053 /* A hash table holding every BFD that gdb knows about. This is not 00054 to be confused with 'gdb_bfd_cache', which is used for sharing 00055 BFDs; in contrast, this hash is used just to implement 00056 "maint info bfd". */ 00057 00058 static htab_t all_bfds; 00059 00060 /* See gdb_bfd.h. */ 00061 00062 void 00063 gdb_bfd_stash_filename (struct bfd *abfd) 00064 { 00065 char *name = bfd_get_filename (abfd); 00066 char *data; 00067 00068 data = bfd_alloc (abfd, strlen (name) + 1); 00069 strcpy (data, name); 00070 00071 /* Unwarranted chumminess with BFD. */ 00072 abfd->filename = data; 00073 } 00074 00075 /* An object of this type is stored in each BFD's user data. */ 00076 00077 struct gdb_bfd_data 00078 { 00079 /* The reference count. */ 00080 int refc; 00081 00082 /* The mtime of the BFD at the point the cache entry was made. */ 00083 time_t mtime; 00084 00085 /* This is true if we have determined whether this BFD has any 00086 sections requiring relocation. */ 00087 unsigned int relocation_computed : 1; 00088 00089 /* This is true if any section needs relocation. */ 00090 unsigned int needs_relocations : 1; 00091 00092 /* This is true if we have successfully computed the file's CRC. */ 00093 unsigned int crc_computed : 1; 00094 00095 /* The file's CRC. */ 00096 unsigned long crc; 00097 00098 /* If the BFD comes from an archive, this points to the archive's 00099 BFD. Otherwise, this is NULL. */ 00100 bfd *archive_bfd; 00101 00102 /* The registry. */ 00103 REGISTRY_FIELDS; 00104 }; 00105 00106 #define GDB_BFD_DATA_ACCESSOR(ABFD) \ 00107 ((struct gdb_bfd_data *) bfd_usrdata (ABFD)) 00108 00109 DEFINE_REGISTRY (bfd, GDB_BFD_DATA_ACCESSOR) 00110 00111 /* A hash table storing all the BFDs maintained in the cache. */ 00112 00113 static htab_t gdb_bfd_cache; 00114 00115 /* The type of an object being looked up in gdb_bfd_cache. We use 00116 htab's capability of storing one kind of object (BFD in this case) 00117 and using a different sort of object for searching. */ 00118 00119 struct gdb_bfd_cache_search 00120 { 00121 /* The filename. */ 00122 const char *filename; 00123 /* The mtime. */ 00124 time_t mtime; 00125 }; 00126 00127 /* A hash function for BFDs. */ 00128 00129 static hashval_t 00130 hash_bfd (const void *b) 00131 { 00132 const bfd *abfd = b; 00133 00134 /* It is simplest to just hash the filename. */ 00135 return htab_hash_string (bfd_get_filename (abfd)); 00136 } 00137 00138 /* An equality function for BFDs. Note that this expects the caller 00139 to search using struct gdb_bfd_cache_search only, not BFDs. */ 00140 00141 static int 00142 eq_bfd (const void *a, const void *b) 00143 { 00144 const bfd *abfd = a; 00145 const struct gdb_bfd_cache_search *s = b; 00146 struct gdb_bfd_data *gdata = bfd_usrdata (abfd); 00147 00148 return (gdata->mtime == s->mtime 00149 && strcmp (bfd_get_filename (abfd), s->filename) == 0); 00150 } 00151 00152 /* See gdb_bfd.h. */ 00153 00154 struct bfd * 00155 gdb_bfd_open (const char *name, const char *target, int fd) 00156 { 00157 hashval_t hash; 00158 void **slot; 00159 bfd *abfd; 00160 struct gdb_bfd_cache_search search; 00161 struct stat st; 00162 00163 if (gdb_bfd_cache == NULL) 00164 gdb_bfd_cache = htab_create_alloc (1, hash_bfd, eq_bfd, NULL, 00165 xcalloc, xfree); 00166 00167 if (fd == -1) 00168 { 00169 fd = gdb_open_cloexec (name, O_RDONLY | O_BINARY, 0); 00170 if (fd == -1) 00171 { 00172 bfd_set_error (bfd_error_system_call); 00173 return NULL; 00174 } 00175 } 00176 00177 search.filename = name; 00178 if (fstat (fd, &st) < 0) 00179 { 00180 /* Weird situation here. */ 00181 search.mtime = 0; 00182 } 00183 else 00184 search.mtime = st.st_mtime; 00185 00186 /* Note that this must compute the same result as hash_bfd. */ 00187 hash = htab_hash_string (name); 00188 /* Note that we cannot use htab_find_slot_with_hash here, because 00189 opening the BFD may fail; and this would violate hashtab 00190 invariants. */ 00191 abfd = htab_find_with_hash (gdb_bfd_cache, &search, hash); 00192 if (abfd != NULL) 00193 { 00194 close (fd); 00195 gdb_bfd_ref (abfd); 00196 return abfd; 00197 } 00198 00199 abfd = bfd_fopen (name, target, FOPEN_RB, fd); 00200 if (abfd == NULL) 00201 return NULL; 00202 00203 slot = htab_find_slot_with_hash (gdb_bfd_cache, &search, hash, INSERT); 00204 gdb_assert (!*slot); 00205 *slot = abfd; 00206 00207 gdb_bfd_stash_filename (abfd); 00208 gdb_bfd_ref (abfd); 00209 return abfd; 00210 } 00211 00212 /* A helper function that releases any section data attached to the 00213 BFD. */ 00214 00215 static void 00216 free_one_bfd_section (bfd *abfd, asection *sectp, void *ignore) 00217 { 00218 struct gdb_bfd_section_data *sect = bfd_get_section_userdata (abfd, sectp); 00219 00220 if (sect != NULL && sect->data != NULL) 00221 { 00222 #ifdef HAVE_MMAP 00223 if (sect->map_addr != NULL) 00224 { 00225 int res; 00226 00227 res = munmap (sect->map_addr, sect->map_len); 00228 gdb_assert (res == 0); 00229 } 00230 else 00231 #endif 00232 xfree (sect->data); 00233 } 00234 } 00235 00236 /* Close ABFD, and warn if that fails. */ 00237 00238 static int 00239 gdb_bfd_close_or_warn (struct bfd *abfd) 00240 { 00241 int ret; 00242 char *name = bfd_get_filename (abfd); 00243 00244 bfd_map_over_sections (abfd, free_one_bfd_section, NULL); 00245 00246 ret = bfd_close (abfd); 00247 00248 if (!ret) 00249 warning (_("cannot close \"%s\": %s"), 00250 name, bfd_errmsg (bfd_get_error ())); 00251 00252 return ret; 00253 } 00254 00255 /* See gdb_bfd.h. */ 00256 00257 void 00258 gdb_bfd_ref (struct bfd *abfd) 00259 { 00260 struct gdb_bfd_data *gdata; 00261 void **slot; 00262 00263 if (abfd == NULL) 00264 return; 00265 00266 gdata = bfd_usrdata (abfd); 00267 00268 if (gdata != NULL) 00269 { 00270 gdata->refc += 1; 00271 return; 00272 } 00273 00274 /* Ask BFD to decompress sections in bfd_get_full_section_contents. */ 00275 abfd->flags |= BFD_DECOMPRESS; 00276 00277 gdata = bfd_zalloc (abfd, sizeof (struct gdb_bfd_data)); 00278 gdata->refc = 1; 00279 gdata->mtime = bfd_get_mtime (abfd); 00280 gdata->archive_bfd = NULL; 00281 bfd_usrdata (abfd) = gdata; 00282 00283 bfd_alloc_data (abfd); 00284 00285 /* This is the first we've seen it, so add it to the hash table. */ 00286 slot = htab_find_slot (all_bfds, abfd, INSERT); 00287 gdb_assert (slot && !*slot); 00288 *slot = abfd; 00289 } 00290 00291 /* See gdb_bfd.h. */ 00292 00293 void 00294 gdb_bfd_unref (struct bfd *abfd) 00295 { 00296 struct gdb_bfd_data *gdata; 00297 struct gdb_bfd_cache_search search; 00298 bfd *archive_bfd; 00299 00300 if (abfd == NULL) 00301 return; 00302 00303 gdata = bfd_usrdata (abfd); 00304 gdb_assert (gdata->refc >= 1); 00305 00306 gdata->refc -= 1; 00307 if (gdata->refc > 0) 00308 return; 00309 00310 archive_bfd = gdata->archive_bfd; 00311 search.filename = bfd_get_filename (abfd); 00312 00313 if (gdb_bfd_cache && search.filename) 00314 { 00315 hashval_t hash = htab_hash_string (search.filename); 00316 void **slot; 00317 00318 search.mtime = gdata->mtime; 00319 slot = htab_find_slot_with_hash (gdb_bfd_cache, &search, hash, 00320 NO_INSERT); 00321 00322 if (slot && *slot) 00323 htab_clear_slot (gdb_bfd_cache, slot); 00324 } 00325 00326 bfd_free_data (abfd); 00327 bfd_usrdata (abfd) = NULL; /* Paranoia. */ 00328 00329 htab_remove_elt (all_bfds, abfd); 00330 00331 gdb_bfd_close_or_warn (abfd); 00332 00333 gdb_bfd_unref (archive_bfd); 00334 } 00335 00336 /* A helper function that returns the section data descriptor 00337 associated with SECTION. If no such descriptor exists, a new one 00338 is allocated and cleared. */ 00339 00340 static struct gdb_bfd_section_data * 00341 get_section_descriptor (asection *section) 00342 { 00343 struct gdb_bfd_section_data *result; 00344 00345 result = bfd_get_section_userdata (section->owner, section); 00346 00347 if (result == NULL) 00348 { 00349 result = bfd_zalloc (section->owner, sizeof (*result)); 00350 bfd_set_section_userdata (section->owner, section, result); 00351 } 00352 00353 return result; 00354 } 00355 00356 /* See gdb_bfd.h. */ 00357 00358 const gdb_byte * 00359 gdb_bfd_map_section (asection *sectp, bfd_size_type *size) 00360 { 00361 bfd *abfd; 00362 struct gdb_bfd_section_data *descriptor; 00363 bfd_byte *data; 00364 00365 gdb_assert ((sectp->flags & SEC_RELOC) == 0); 00366 gdb_assert (size != NULL); 00367 00368 abfd = sectp->owner; 00369 00370 descriptor = get_section_descriptor (sectp); 00371 00372 /* If the data was already read for this BFD, just reuse it. */ 00373 if (descriptor->data != NULL) 00374 goto done; 00375 00376 #ifdef HAVE_MMAP 00377 if (!bfd_is_section_compressed (abfd, sectp)) 00378 { 00379 /* The page size, used when mmapping. */ 00380 static int pagesize; 00381 00382 if (pagesize == 0) 00383 pagesize = getpagesize (); 00384 00385 /* Only try to mmap sections which are large enough: we don't want 00386 to waste space due to fragmentation. */ 00387 00388 if (bfd_get_section_size (sectp) > 4 * pagesize) 00389 { 00390 descriptor->size = bfd_get_section_size (sectp); 00391 descriptor->data = bfd_mmap (abfd, 0, descriptor->size, PROT_READ, 00392 MAP_PRIVATE, sectp->filepos, 00393 &descriptor->map_addr, 00394 &descriptor->map_len); 00395 00396 if ((caddr_t)descriptor->data != MAP_FAILED) 00397 { 00398 #if HAVE_POSIX_MADVISE 00399 posix_madvise (descriptor->map_addr, descriptor->map_len, 00400 POSIX_MADV_WILLNEED); 00401 #endif 00402 goto done; 00403 } 00404 00405 /* On failure, clear out the section data and try again. */ 00406 memset (descriptor, 0, sizeof (*descriptor)); 00407 } 00408 } 00409 #endif /* HAVE_MMAP */ 00410 00411 /* Handle compressed sections, or ordinary uncompressed sections in 00412 the no-mmap case. */ 00413 00414 descriptor->size = bfd_get_section_size (sectp); 00415 descriptor->data = NULL; 00416 00417 data = NULL; 00418 if (!bfd_get_full_section_contents (abfd, sectp, &data)) 00419 error (_("Can't read data for section '%s' in file '%s'"), 00420 bfd_get_section_name (abfd, sectp), 00421 bfd_get_filename (abfd)); 00422 descriptor->data = data; 00423 00424 done: 00425 gdb_assert (descriptor->data != NULL); 00426 *size = descriptor->size; 00427 return descriptor->data; 00428 } 00429 00430 /* Return 32-bit CRC for ABFD. If successful store it to *FILE_CRC_RETURN and 00431 return 1. Otherwise print a warning and return 0. ABFD seek position is 00432 not preserved. */ 00433 00434 static int 00435 get_file_crc (bfd *abfd, unsigned long *file_crc_return) 00436 { 00437 unsigned long file_crc = 0; 00438 00439 if (bfd_seek (abfd, 0, SEEK_SET) != 0) 00440 { 00441 warning (_("Problem reading \"%s\" for CRC: %s"), 00442 bfd_get_filename (abfd), bfd_errmsg (bfd_get_error ())); 00443 return 0; 00444 } 00445 00446 for (;;) 00447 { 00448 gdb_byte buffer[8 * 1024]; 00449 bfd_size_type count; 00450 00451 count = bfd_bread (buffer, sizeof (buffer), abfd); 00452 if (count == (bfd_size_type) -1) 00453 { 00454 warning (_("Problem reading \"%s\" for CRC: %s"), 00455 bfd_get_filename (abfd), bfd_errmsg (bfd_get_error ())); 00456 return 0; 00457 } 00458 if (count == 0) 00459 break; 00460 file_crc = bfd_calc_gnu_debuglink_crc32 (file_crc, buffer, count); 00461 } 00462 00463 *file_crc_return = file_crc; 00464 return 1; 00465 } 00466 00467 /* See gdb_bfd.h. */ 00468 00469 int 00470 gdb_bfd_crc (struct bfd *abfd, unsigned long *crc_out) 00471 { 00472 struct gdb_bfd_data *gdata = bfd_usrdata (abfd); 00473 00474 if (!gdata->crc_computed) 00475 gdata->crc_computed = get_file_crc (abfd, &gdata->crc); 00476 00477 if (gdata->crc_computed) 00478 *crc_out = gdata->crc; 00479 return gdata->crc_computed; 00480 } 00481 00482 00483 00484 /* See gdb_bfd.h. */ 00485 00486 bfd * 00487 gdb_bfd_fopen (const char *filename, const char *target, const char *mode, 00488 int fd) 00489 { 00490 bfd *result = bfd_fopen (filename, target, mode, fd); 00491 00492 if (result) 00493 { 00494 gdb_bfd_stash_filename (result); 00495 gdb_bfd_ref (result); 00496 } 00497 00498 return result; 00499 } 00500 00501 /* See gdb_bfd.h. */ 00502 00503 bfd * 00504 gdb_bfd_openr (const char *filename, const char *target) 00505 { 00506 bfd *result = bfd_openr (filename, target); 00507 00508 if (result) 00509 { 00510 gdb_bfd_stash_filename (result); 00511 gdb_bfd_ref (result); 00512 } 00513 00514 return result; 00515 } 00516 00517 /* See gdb_bfd.h. */ 00518 00519 bfd * 00520 gdb_bfd_openw (const char *filename, const char *target) 00521 { 00522 bfd *result = bfd_openw (filename, target); 00523 00524 if (result) 00525 { 00526 gdb_bfd_stash_filename (result); 00527 gdb_bfd_ref (result); 00528 } 00529 00530 return result; 00531 } 00532 00533 /* See gdb_bfd.h. */ 00534 00535 bfd * 00536 gdb_bfd_openr_iovec (const char *filename, const char *target, 00537 void *(*open_func) (struct bfd *nbfd, 00538 void *open_closure), 00539 void *open_closure, 00540 file_ptr (*pread_func) (struct bfd *nbfd, 00541 void *stream, 00542 void *buf, 00543 file_ptr nbytes, 00544 file_ptr offset), 00545 int (*close_func) (struct bfd *nbfd, 00546 void *stream), 00547 int (*stat_func) (struct bfd *abfd, 00548 void *stream, 00549 struct stat *sb)) 00550 { 00551 bfd *result = bfd_openr_iovec (filename, target, 00552 open_func, open_closure, 00553 pread_func, close_func, stat_func); 00554 00555 if (result) 00556 { 00557 gdb_bfd_ref (result); 00558 gdb_bfd_stash_filename (result); 00559 } 00560 00561 return result; 00562 } 00563 00564 /* See gdb_bfd.h. */ 00565 00566 void 00567 gdb_bfd_mark_parent (bfd *child, bfd *parent) 00568 { 00569 struct gdb_bfd_data *gdata; 00570 00571 gdb_bfd_ref (child); 00572 /* No need to stash the filename here, because we also keep a 00573 reference on the parent archive. */ 00574 00575 gdata = bfd_usrdata (child); 00576 if (gdata->archive_bfd == NULL) 00577 { 00578 gdata->archive_bfd = parent; 00579 gdb_bfd_ref (parent); 00580 } 00581 else 00582 gdb_assert (gdata->archive_bfd == parent); 00583 } 00584 00585 /* See gdb_bfd.h. */ 00586 00587 bfd * 00588 gdb_bfd_openr_next_archived_file (bfd *archive, bfd *previous) 00589 { 00590 bfd *result = bfd_openr_next_archived_file (archive, previous); 00591 00592 if (result) 00593 gdb_bfd_mark_parent (result, archive); 00594 00595 return result; 00596 } 00597 00598 /* See gdb_bfd.h. */ 00599 00600 bfd * 00601 gdb_bfd_fdopenr (const char *filename, const char *target, int fd) 00602 { 00603 bfd *result = bfd_fdopenr (filename, target, fd); 00604 00605 if (result) 00606 { 00607 gdb_bfd_ref (result); 00608 gdb_bfd_stash_filename (result); 00609 } 00610 00611 return result; 00612 } 00613 00614 00615 00616 gdb_static_assert (ARRAY_SIZE (_bfd_std_section) == 4); 00617 00618 /* See gdb_bfd.h. */ 00619 00620 int 00621 gdb_bfd_section_index (bfd *abfd, asection *section) 00622 { 00623 if (section == NULL) 00624 return -1; 00625 else if (section == bfd_com_section_ptr) 00626 return bfd_count_sections (abfd) + 1; 00627 else if (section == bfd_und_section_ptr) 00628 return bfd_count_sections (abfd) + 2; 00629 else if (section == bfd_abs_section_ptr) 00630 return bfd_count_sections (abfd) + 3; 00631 else if (section == bfd_ind_section_ptr) 00632 return bfd_count_sections (abfd) + 4; 00633 return section->index; 00634 } 00635 00636 /* See gdb_bfd.h. */ 00637 00638 int 00639 gdb_bfd_count_sections (bfd *abfd) 00640 { 00641 return bfd_count_sections (abfd) + 4; 00642 } 00643 00644 /* See gdb_bfd.h. */ 00645 00646 int 00647 gdb_bfd_requires_relocations (bfd *abfd) 00648 { 00649 struct gdb_bfd_data *gdata = bfd_usrdata (abfd); 00650 00651 if (gdata->relocation_computed == 0) 00652 { 00653 asection *sect; 00654 00655 for (sect = abfd->sections; sect != NULL; sect = sect->next) 00656 if ((sect->flags & SEC_RELOC) != 0) 00657 { 00658 gdata->needs_relocations = 1; 00659 break; 00660 } 00661 00662 gdata->relocation_computed = 1; 00663 } 00664 00665 return gdata->needs_relocations; 00666 } 00667 00668 00669 00670 /* A callback for htab_traverse that prints a single BFD. */ 00671 00672 static int 00673 print_one_bfd (void **slot, void *data) 00674 { 00675 bfd *abfd = *slot; 00676 struct gdb_bfd_data *gdata = bfd_usrdata (abfd); 00677 struct ui_out *uiout = data; 00678 struct cleanup *inner; 00679 00680 inner = make_cleanup_ui_out_tuple_begin_end (uiout, NULL); 00681 ui_out_field_int (uiout, "refcount", gdata->refc); 00682 ui_out_field_string (uiout, "addr", host_address_to_string (abfd)); 00683 ui_out_field_string (uiout, "filename", bfd_get_filename (abfd)); 00684 ui_out_text (uiout, "\n"); 00685 do_cleanups (inner); 00686 00687 return 1; 00688 } 00689 00690 /* Implement the 'maint info bfd' command. */ 00691 00692 static void 00693 maintenance_info_bfds (char *arg, int from_tty) 00694 { 00695 struct cleanup *cleanup; 00696 struct ui_out *uiout = current_uiout; 00697 00698 cleanup = make_cleanup_ui_out_table_begin_end (uiout, 3, -1, "bfds"); 00699 ui_out_table_header (uiout, 10, ui_left, "refcount", "Refcount"); 00700 ui_out_table_header (uiout, 18, ui_left, "addr", "Address"); 00701 ui_out_table_header (uiout, 40, ui_left, "filename", "Filename"); 00702 00703 ui_out_table_body (uiout); 00704 htab_traverse (all_bfds, print_one_bfd, uiout); 00705 00706 do_cleanups (cleanup); 00707 } 00708 00709 /* -Wmissing-prototypes */ 00710 extern initialize_file_ftype _initialize_gdb_bfd; 00711 00712 void 00713 _initialize_gdb_bfd (void) 00714 { 00715 all_bfds = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer, 00716 NULL, xcalloc, xfree); 00717 00718 add_cmd ("bfds", class_maintenance, maintenance_info_bfds, _("\ 00719 List the BFDs that are currently open."), 00720 &maintenanceinfolist); 00721 }