Commit | Line | Data |
---|---|---|
cbb099e8 TT |
1 | /* Definitions for BFD wrappers used by GDB. |
2 | ||
6ec53d05 | 3 | Copyright (C) 2011, 2012 |
cbb099e8 TT |
4 | Free Software Foundation, Inc. |
5 | ||
6 | This file is part of GDB. | |
7 | ||
8 | This program is free software; you can redistribute it and/or modify | |
9 | it under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation; either version 3 of the License, or | |
11 | (at your option) any later version. | |
12 | ||
13 | This program is distributed in the hope that it will be useful, | |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
19 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ | |
20 | ||
21 | #include "defs.h" | |
22 | #include "gdb_bfd.h" | |
23 | #include "gdb_assert.h" | |
a4453b7e | 24 | #include "gdb_string.h" |
d6b28940 TT |
25 | #include "ui-out.h" |
26 | #include "gdbcmd.h" | |
6ec53d05 | 27 | #include "hashtab.h" |
4bf44c1c TT |
28 | #ifdef HAVE_ZLIB_H |
29 | #include <zlib.h> | |
30 | #endif | |
31 | #ifdef HAVE_MMAP | |
32 | #include <sys/mman.h> | |
33 | #ifndef MAP_FAILED | |
34 | #define MAP_FAILED ((void *) -1) | |
35 | #endif | |
36 | #endif | |
37 | ||
38 | /* An object of this type is stored in the section's user data when | |
39 | mapping a section. */ | |
40 | ||
41 | struct 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 | ||
58 | static htab_t all_bfds; | |
59 | ||
a4453b7e TT |
60 | /* See gdb_bfd.h. */ |
61 | ||
62 | void | |
63 | gdb_bfd_stash_filename (struct bfd *abfd) | |
64 | { | |
65 | char *name = bfd_get_filename (abfd); | |
66 | char *data; | |
67 | ||
68 | data = bfd_alloc (abfd, strlen (name) + 1); | |
69 | strcpy (data, name); | |
70 | ||
71 | /* Unwarranted chumminess with BFD. */ | |
72 | abfd->filename = data; | |
73 | } | |
cbb099e8 | 74 | |
6ec53d05 TT |
75 | /* An object of this type is stored in each BFD's user data. */ |
76 | ||
77 | struct gdb_bfd_data | |
78 | { | |
79 | /* The reference count. */ | |
80 | int refc; | |
81 | ||
82 | /* The mtime of the BFD at the point the cache entry was made. */ | |
83 | time_t mtime; | |
84 | }; | |
85 | ||
86 | /* A hash table storing all the BFDs maintained in the cache. */ | |
87 | ||
88 | static htab_t gdb_bfd_cache; | |
89 | ||
90 | /* The type of an object being looked up in gdb_bfd_cache. We use | |
91 | htab's capability of storing one kind of object (BFD in this case) | |
92 | and using a different sort of object for searching. */ | |
93 | ||
94 | struct gdb_bfd_cache_search | |
95 | { | |
96 | /* The filename. */ | |
97 | const char *filename; | |
98 | /* The mtime. */ | |
99 | time_t mtime; | |
100 | }; | |
101 | ||
102 | /* A hash function for BFDs. */ | |
103 | ||
104 | static hashval_t | |
105 | hash_bfd (const void *b) | |
106 | { | |
107 | const bfd *abfd = b; | |
108 | ||
109 | /* It is simplest to just hash the filename. */ | |
110 | return htab_hash_string (bfd_get_filename (abfd)); | |
111 | } | |
112 | ||
113 | /* An equality function for BFDs. Note that this expects the caller | |
114 | to search using struct gdb_bfd_cache_search only, not BFDs. */ | |
115 | ||
116 | static int | |
117 | eq_bfd (const void *a, const void *b) | |
118 | { | |
119 | const bfd *abfd = a; | |
120 | const struct gdb_bfd_cache_search *s = b; | |
121 | struct gdb_bfd_data *gdata = bfd_usrdata (abfd); | |
122 | ||
123 | return (gdata->mtime == s->mtime | |
124 | && strcmp (bfd_get_filename (abfd), s->filename) == 0); | |
125 | } | |
126 | ||
127 | /* See gdb_bfd.h. */ | |
128 | ||
129 | struct bfd * | |
130 | gdb_bfd_open (const char *name, const char *target, int fd) | |
131 | { | |
132 | hashval_t hash; | |
133 | void **slot; | |
134 | bfd *abfd; | |
135 | struct gdb_bfd_cache_search search; | |
136 | struct stat st; | |
137 | ||
138 | if (gdb_bfd_cache == NULL) | |
139 | gdb_bfd_cache = htab_create_alloc (1, hash_bfd, eq_bfd, NULL, | |
140 | xcalloc, xfree); | |
141 | ||
142 | if (fd == -1) | |
143 | { | |
144 | fd = open (name, O_RDONLY | O_BINARY); | |
145 | if (fd == -1) | |
146 | { | |
147 | bfd_set_error (bfd_error_system_call); | |
148 | return NULL; | |
149 | } | |
150 | } | |
151 | ||
152 | search.filename = name; | |
153 | if (fstat (fd, &st) < 0) | |
154 | { | |
155 | /* Weird situation here. */ | |
156 | search.mtime = 0; | |
157 | } | |
158 | else | |
159 | search.mtime = st.st_mtime; | |
160 | ||
161 | /* Note that this must compute the same result as hash_bfd. */ | |
162 | hash = htab_hash_string (name); | |
163 | /* Note that we cannot use htab_find_slot_with_hash here, because | |
164 | opening the BFD may fail; and this would violate hashtab | |
165 | invariants. */ | |
166 | abfd = htab_find_with_hash (gdb_bfd_cache, &search, hash); | |
167 | if (abfd != NULL) | |
168 | { | |
169 | close (fd); | |
520b0001 TT |
170 | gdb_bfd_ref (abfd); |
171 | return abfd; | |
6ec53d05 TT |
172 | } |
173 | ||
174 | abfd = bfd_fopen (name, target, FOPEN_RB, fd); | |
175 | if (abfd == NULL) | |
176 | return NULL; | |
177 | ||
178 | slot = htab_find_slot_with_hash (gdb_bfd_cache, &search, hash, INSERT); | |
179 | gdb_assert (!*slot); | |
180 | *slot = abfd; | |
181 | ||
182 | gdb_bfd_stash_filename (abfd); | |
520b0001 TT |
183 | gdb_bfd_ref (abfd); |
184 | return abfd; | |
6ec53d05 TT |
185 | } |
186 | ||
4bf44c1c TT |
187 | /* A helper function that releases any section data attached to the |
188 | BFD. */ | |
189 | ||
190 | static void | |
191 | free_one_bfd_section (bfd *abfd, asection *sectp, void *ignore) | |
192 | { | |
193 | struct gdb_bfd_section_data *sect = bfd_get_section_userdata (abfd, sectp); | |
194 | ||
195 | if (sect != NULL && sect->data != NULL) | |
196 | { | |
197 | #ifdef HAVE_MMAP | |
198 | if (sect->map_addr != NULL) | |
199 | { | |
200 | int res; | |
201 | ||
202 | res = munmap (sect->map_addr, sect->map_len); | |
203 | gdb_assert (res == 0); | |
204 | } | |
205 | else | |
206 | #endif | |
207 | xfree (sect->data); | |
208 | } | |
209 | } | |
210 | ||
cbb099e8 TT |
211 | /* Close ABFD, and warn if that fails. */ |
212 | ||
213 | static int | |
214 | gdb_bfd_close_or_warn (struct bfd *abfd) | |
215 | { | |
216 | int ret; | |
217 | char *name = bfd_get_filename (abfd); | |
218 | ||
4bf44c1c TT |
219 | bfd_map_over_sections (abfd, free_one_bfd_section, NULL); |
220 | ||
cbb099e8 TT |
221 | ret = bfd_close (abfd); |
222 | ||
223 | if (!ret) | |
224 | warning (_("cannot close \"%s\": %s"), | |
225 | name, bfd_errmsg (bfd_get_error ())); | |
226 | ||
227 | return ret; | |
228 | } | |
229 | ||
596f7d67 | 230 | /* See gdb_bfd.h. */ |
cbb099e8 | 231 | |
520b0001 | 232 | void |
cbb099e8 TT |
233 | gdb_bfd_ref (struct bfd *abfd) |
234 | { | |
6ec53d05 | 235 | struct gdb_bfd_data *gdata; |
d6b28940 | 236 | void **slot; |
cbb099e8 TT |
237 | |
238 | if (abfd == NULL) | |
520b0001 | 239 | return; |
cbb099e8 | 240 | |
6ec53d05 | 241 | gdata = bfd_usrdata (abfd); |
cbb099e8 | 242 | |
6ec53d05 | 243 | if (gdata != NULL) |
cbb099e8 | 244 | { |
6ec53d05 | 245 | gdata->refc += 1; |
520b0001 | 246 | return; |
cbb099e8 TT |
247 | } |
248 | ||
6ec53d05 TT |
249 | gdata = bfd_zalloc (abfd, sizeof (struct gdb_bfd_data)); |
250 | gdata->refc = 1; | |
251 | gdata->mtime = bfd_get_mtime (abfd); | |
252 | bfd_usrdata (abfd) = gdata; | |
d6b28940 TT |
253 | |
254 | /* This is the first we've seen it, so add it to the hash table. */ | |
255 | slot = htab_find_slot (all_bfds, abfd, INSERT); | |
256 | gdb_assert (slot && !*slot); | |
257 | *slot = abfd; | |
cbb099e8 TT |
258 | } |
259 | ||
596f7d67 | 260 | /* See gdb_bfd.h. */ |
cbb099e8 TT |
261 | |
262 | void | |
263 | gdb_bfd_unref (struct bfd *abfd) | |
264 | { | |
6ec53d05 TT |
265 | struct gdb_bfd_data *gdata; |
266 | struct gdb_bfd_cache_search search; | |
cbb099e8 TT |
267 | |
268 | if (abfd == NULL) | |
269 | return; | |
270 | ||
6ec53d05 TT |
271 | gdata = bfd_usrdata (abfd); |
272 | gdb_assert (gdata->refc >= 1); | |
cbb099e8 | 273 | |
6ec53d05 TT |
274 | gdata->refc -= 1; |
275 | if (gdata->refc > 0) | |
cbb099e8 TT |
276 | return; |
277 | ||
6ec53d05 TT |
278 | search.filename = bfd_get_filename (abfd); |
279 | ||
280 | if (gdb_bfd_cache && search.filename) | |
281 | { | |
282 | hashval_t hash = htab_hash_string (search.filename); | |
283 | void **slot; | |
284 | ||
285 | search.mtime = gdata->mtime; | |
286 | slot = htab_find_slot_with_hash (gdb_bfd_cache, &search, hash, | |
287 | NO_INSERT); | |
288 | ||
289 | if (slot && *slot) | |
290 | htab_clear_slot (gdb_bfd_cache, slot); | |
291 | } | |
292 | ||
cbb099e8 TT |
293 | bfd_usrdata (abfd) = NULL; /* Paranoia. */ |
294 | ||
d6b28940 TT |
295 | htab_remove_elt (all_bfds, abfd); |
296 | ||
cbb099e8 TT |
297 | gdb_bfd_close_or_warn (abfd); |
298 | } | |
4bf44c1c TT |
299 | |
300 | /* A helper function that returns the section data descriptor | |
301 | associated with SECTION. If no such descriptor exists, a new one | |
302 | is allocated and cleared. */ | |
303 | ||
304 | static struct gdb_bfd_section_data * | |
305 | get_section_descriptor (asection *section) | |
306 | { | |
307 | struct gdb_bfd_section_data *result; | |
308 | ||
309 | result = bfd_get_section_userdata (section->owner, section); | |
310 | ||
311 | if (result == NULL) | |
312 | { | |
313 | result = bfd_zalloc (section->owner, sizeof (*result)); | |
314 | bfd_set_section_userdata (section->owner, section, result); | |
315 | } | |
316 | ||
317 | return result; | |
318 | } | |
319 | ||
320 | /* Decompress a section that was compressed using zlib. Store the | |
321 | decompressed buffer, and its size, in DESCRIPTOR. */ | |
322 | ||
323 | static void | |
324 | zlib_decompress_section (asection *sectp, | |
325 | struct gdb_bfd_section_data *descriptor) | |
326 | { | |
327 | bfd *abfd = sectp->owner; | |
328 | #ifndef HAVE_ZLIB_H | |
329 | error (_("Support for zlib-compressed data (from '%s', section '%s') " | |
330 | "is disabled in this copy of GDB"), | |
331 | bfd_get_filename (abfd), | |
1634dcbe | 332 | bfd_get_section_name (abfd, sectp)); |
4bf44c1c TT |
333 | #else |
334 | bfd_size_type compressed_size = bfd_get_section_size (sectp); | |
335 | gdb_byte *compressed_buffer = xmalloc (compressed_size); | |
336 | struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer); | |
337 | struct cleanup *inner_cleanup; | |
338 | bfd_size_type uncompressed_size; | |
339 | gdb_byte *uncompressed_buffer; | |
340 | z_stream strm; | |
341 | int rc; | |
342 | int header_size = 12; | |
4bf44c1c TT |
343 | |
344 | if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0 | |
345 | || bfd_bread (compressed_buffer, | |
346 | compressed_size, abfd) != compressed_size) | |
347 | error (_("can't read data from '%s', section '%s'"), | |
348 | bfd_get_filename (abfd), | |
349 | bfd_get_section_name (abfd, sectp)); | |
350 | ||
351 | /* Read the zlib header. In this case, it should be "ZLIB" followed | |
352 | by the uncompressed section size, 8 bytes in big-endian order. */ | |
353 | if (compressed_size < header_size | |
354 | || strncmp (compressed_buffer, "ZLIB", 4) != 0) | |
355 | error (_("corrupt ZLIB header from '%s', section '%s'"), | |
356 | bfd_get_filename (abfd), | |
357 | bfd_get_section_name (abfd, sectp)); | |
358 | uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8; | |
359 | uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8; | |
360 | uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8; | |
361 | uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8; | |
362 | uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8; | |
363 | uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8; | |
364 | uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8; | |
365 | uncompressed_size += compressed_buffer[11]; | |
366 | ||
367 | /* It is possible the section consists of several compressed | |
368 | buffers concatenated together, so we uncompress in a loop. */ | |
369 | strm.zalloc = NULL; | |
370 | strm.zfree = NULL; | |
371 | strm.opaque = NULL; | |
372 | strm.avail_in = compressed_size - header_size; | |
373 | strm.next_in = (Bytef*) compressed_buffer + header_size; | |
374 | strm.avail_out = uncompressed_size; | |
375 | uncompressed_buffer = xmalloc (uncompressed_size); | |
376 | inner_cleanup = make_cleanup (xfree, uncompressed_buffer); | |
377 | rc = inflateInit (&strm); | |
378 | while (strm.avail_in > 0) | |
379 | { | |
380 | if (rc != Z_OK) | |
381 | error (_("setting up uncompression in '%s', section '%s': %d"), | |
382 | bfd_get_filename (abfd), | |
383 | bfd_get_section_name (abfd, sectp), | |
384 | rc); | |
385 | strm.next_out = ((Bytef*) uncompressed_buffer | |
386 | + (uncompressed_size - strm.avail_out)); | |
387 | rc = inflate (&strm, Z_FINISH); | |
388 | if (rc != Z_STREAM_END) | |
389 | error (_("zlib error uncompressing from '%s', section '%s': %d"), | |
390 | bfd_get_filename (abfd), | |
391 | bfd_get_section_name (abfd, sectp), | |
392 | rc); | |
393 | rc = inflateReset (&strm); | |
394 | } | |
395 | rc = inflateEnd (&strm); | |
396 | if (rc != Z_OK | |
397 | || strm.avail_out != 0) | |
398 | error (_("concluding uncompression in '%s', section '%s': %d"), | |
399 | bfd_get_filename (abfd), | |
400 | bfd_get_section_name (abfd, sectp), | |
401 | rc); | |
402 | ||
403 | discard_cleanups (inner_cleanup); | |
404 | do_cleanups (cleanup); | |
405 | ||
406 | /* Attach the data to the BFD section. */ | |
407 | descriptor->data = uncompressed_buffer; | |
408 | descriptor->size = uncompressed_size; | |
409 | #endif | |
410 | } | |
411 | ||
412 | /* See gdb_bfd.h. */ | |
413 | ||
414 | const gdb_byte * | |
415 | gdb_bfd_map_section (asection *sectp, bfd_size_type *size) | |
416 | { | |
417 | bfd *abfd; | |
4bf44c1c TT |
418 | unsigned char header[4]; |
419 | struct gdb_bfd_section_data *descriptor; | |
420 | ||
421 | gdb_assert ((sectp->flags & SEC_RELOC) == 0); | |
422 | gdb_assert (size != NULL); | |
423 | ||
424 | abfd = sectp->owner; | |
425 | ||
426 | descriptor = get_section_descriptor (sectp); | |
427 | ||
428 | /* If the data was already read for this BFD, just reuse it. */ | |
429 | if (descriptor->data != NULL) | |
430 | goto done; | |
431 | ||
432 | /* Check if the file has a 4-byte header indicating compression. */ | |
433 | if (bfd_get_section_size (sectp) > sizeof (header) | |
434 | && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0 | |
435 | && bfd_bread (header, sizeof (header), abfd) == sizeof (header)) | |
436 | { | |
437 | /* Upon decompression, update the buffer and its size. */ | |
438 | if (strncmp (header, "ZLIB", sizeof (header)) == 0) | |
439 | { | |
440 | zlib_decompress_section (sectp, descriptor); | |
441 | goto done; | |
442 | } | |
443 | } | |
444 | ||
445 | #ifdef HAVE_MMAP | |
446 | { | |
447 | /* The page size, used when mmapping. */ | |
448 | static int pagesize; | |
449 | ||
450 | if (pagesize == 0) | |
451 | pagesize = getpagesize (); | |
452 | ||
453 | /* Only try to mmap sections which are large enough: we don't want | |
454 | to waste space due to fragmentation. */ | |
455 | ||
456 | if (bfd_get_section_size (sectp) > 4 * pagesize) | |
457 | { | |
458 | descriptor->size = bfd_get_section_size (sectp); | |
459 | descriptor->data = bfd_mmap (abfd, 0, descriptor->size, PROT_READ, | |
460 | MAP_PRIVATE, sectp->filepos, | |
461 | &descriptor->map_addr, | |
462 | &descriptor->map_len); | |
463 | ||
464 | if ((caddr_t)descriptor->data != MAP_FAILED) | |
465 | { | |
466 | #if HAVE_POSIX_MADVISE | |
467 | posix_madvise (descriptor->map_addr, descriptor->map_len, | |
468 | POSIX_MADV_WILLNEED); | |
469 | #endif | |
470 | goto done; | |
471 | } | |
472 | ||
473 | /* On failure, clear out the section data and try again. */ | |
474 | memset (descriptor, 0, sizeof (*descriptor)); | |
475 | } | |
476 | } | |
477 | #endif /* HAVE_MMAP */ | |
478 | ||
479 | /* If we get here, we are a normal, not-compressed section. */ | |
480 | ||
481 | descriptor->size = bfd_get_section_size (sectp); | |
482 | descriptor->data = xmalloc (descriptor->size); | |
483 | ||
484 | if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0 | |
485 | || bfd_bread (descriptor->data, bfd_get_section_size (sectp), | |
486 | abfd) != bfd_get_section_size (sectp)) | |
487 | { | |
488 | xfree (descriptor->data); | |
489 | descriptor->data = NULL; | |
490 | error (_("Can't read data for section '%s'"), | |
491 | bfd_get_filename (abfd)); | |
492 | } | |
493 | ||
494 | done: | |
495 | gdb_assert (descriptor->data != NULL); | |
496 | *size = descriptor->size; | |
497 | return descriptor->data; | |
498 | } | |
64c31149 TT |
499 | |
500 | \f | |
501 | ||
502 | /* See gdb_bfd.h. */ | |
503 | ||
504 | bfd * | |
505 | gdb_bfd_fopen (const char *filename, const char *target, const char *mode, | |
506 | int fd) | |
507 | { | |
508 | bfd *result = bfd_fopen (filename, target, mode, fd); | |
509 | ||
510 | if (result) | |
511 | { | |
512 | gdb_bfd_stash_filename (result); | |
513 | gdb_bfd_ref (result); | |
514 | } | |
515 | ||
516 | return result; | |
517 | } | |
518 | ||
519 | /* See gdb_bfd.h. */ | |
520 | ||
521 | bfd * | |
522 | gdb_bfd_openr (const char *filename, const char *target) | |
523 | { | |
524 | bfd *result = bfd_openr (filename, target); | |
525 | ||
526 | if (result) | |
527 | { | |
528 | gdb_bfd_stash_filename (result); | |
529 | gdb_bfd_ref (result); | |
530 | } | |
531 | ||
532 | return result; | |
533 | } | |
534 | ||
535 | /* See gdb_bfd.h. */ | |
536 | ||
537 | bfd * | |
538 | gdb_bfd_openw (const char *filename, const char *target) | |
539 | { | |
540 | bfd *result = bfd_openw (filename, target); | |
541 | ||
542 | if (result) | |
543 | { | |
544 | gdb_bfd_stash_filename (result); | |
545 | gdb_bfd_ref (result); | |
546 | } | |
547 | ||
548 | return result; | |
549 | } | |
550 | ||
551 | /* See gdb_bfd.h. */ | |
552 | ||
553 | bfd * | |
554 | gdb_bfd_openr_iovec (const char *filename, const char *target, | |
555 | void *(*open_func) (struct bfd *nbfd, | |
556 | void *open_closure), | |
557 | void *open_closure, | |
558 | file_ptr (*pread_func) (struct bfd *nbfd, | |
559 | void *stream, | |
560 | void *buf, | |
561 | file_ptr nbytes, | |
562 | file_ptr offset), | |
563 | int (*close_func) (struct bfd *nbfd, | |
564 | void *stream), | |
565 | int (*stat_func) (struct bfd *abfd, | |
566 | void *stream, | |
567 | struct stat *sb)) | |
568 | { | |
569 | bfd *result = bfd_openr_iovec (filename, target, | |
570 | open_func, open_closure, | |
571 | pread_func, close_func, stat_func); | |
572 | ||
573 | if (result) | |
574 | { | |
575 | gdb_bfd_ref (result); | |
576 | gdb_bfd_stash_filename (result); | |
577 | } | |
578 | ||
579 | return result; | |
580 | } | |
581 | ||
582 | /* See gdb_bfd.h. */ | |
583 | ||
584 | bfd * | |
585 | gdb_bfd_openr_next_archived_file (bfd *archive, bfd *previous) | |
586 | { | |
587 | bfd *result = bfd_openr_next_archived_file (archive, previous); | |
588 | ||
589 | if (result) | |
590 | { | |
591 | gdb_bfd_ref (result); | |
592 | /* No need to stash the filename here. */ | |
593 | } | |
594 | ||
595 | return result; | |
596 | } | |
597 | ||
598 | /* See gdb_bfd.h. */ | |
599 | ||
600 | bfd * | |
601 | gdb_bfd_fdopenr (const char *filename, const char *target, int fd) | |
602 | { | |
603 | bfd *result = bfd_fdopenr (filename, target, fd); | |
604 | ||
605 | if (result) | |
606 | { | |
607 | gdb_bfd_ref (result); | |
608 | gdb_bfd_stash_filename (result); | |
609 | } | |
610 | ||
611 | return result; | |
612 | } | |
d6b28940 TT |
613 | |
614 | \f | |
615 | ||
616 | /* A callback for htab_traverse that prints a single BFD. */ | |
617 | ||
618 | static int | |
619 | print_one_bfd (void **slot, void *data) | |
620 | { | |
621 | bfd *abfd = *slot; | |
622 | struct gdb_bfd_data *gdata = bfd_usrdata (abfd); | |
623 | struct ui_out *uiout = data; | |
624 | struct cleanup *inner; | |
625 | ||
626 | inner = make_cleanup_ui_out_tuple_begin_end (uiout, NULL); | |
627 | ui_out_field_int (uiout, "refcount", gdata->refc); | |
628 | ui_out_field_string (uiout, "addr", host_address_to_string (abfd)); | |
629 | ui_out_field_string (uiout, "filename", bfd_get_filename (abfd)); | |
630 | ui_out_text (uiout, "\n"); | |
631 | do_cleanups (inner); | |
632 | ||
633 | return 1; | |
634 | } | |
635 | ||
636 | /* Implement the 'maint info bfd' command. */ | |
637 | ||
638 | static void | |
639 | maintenance_info_bfds (char *arg, int from_tty) | |
640 | { | |
641 | struct cleanup *cleanup; | |
642 | struct ui_out *uiout = current_uiout; | |
643 | ||
644 | cleanup = make_cleanup_ui_out_table_begin_end (uiout, 3, -1, "bfds"); | |
645 | ui_out_table_header (uiout, 10, ui_left, "refcount", "Refcount"); | |
646 | ui_out_table_header (uiout, 18, ui_left, "addr", "Address"); | |
647 | ui_out_table_header (uiout, 40, ui_left, "filename", "Filename"); | |
648 | ||
649 | ui_out_table_body (uiout); | |
650 | htab_traverse (all_bfds, print_one_bfd, uiout); | |
651 | ||
652 | do_cleanups (cleanup); | |
653 | } | |
654 | ||
655 | /* -Wmissing-prototypes */ | |
656 | extern initialize_file_ftype _initialize_gdb_bfd; | |
657 | ||
658 | void | |
659 | _initialize_gdb_bfd (void) | |
660 | { | |
661 | all_bfds = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer, | |
662 | NULL, xcalloc, xfree); | |
663 | ||
664 | add_cmd ("bfds", class_maintenance, maintenance_info_bfds, _("\ | |
665 | List the BFDs that are currently open."), | |
666 | &maintenanceinfolist); | |
667 | } |