Commit | Line | Data |
---|---|---|
cbb099e8 TT |
1 | /* Definitions for BFD wrappers used by GDB. |
2 | ||
32d0add0 | 3 | Copyright (C) 2011-2015 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_ZLIB_H |
28 | #include <zlib.h> | |
29 | #endif | |
30 | #ifdef HAVE_MMAP | |
31 | #include <sys/mman.h> | |
32 | #ifndef MAP_FAILED | |
33 | #define MAP_FAILED ((void *) -1) | |
34 | #endif | |
35 | #endif | |
36 | ||
13aaf454 DE |
37 | typedef bfd *bfdp; |
38 | DEF_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 | ||
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 | }; | |
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 | ||
60 | static htab_t all_bfds; | |
61 | ||
6ec53d05 TT |
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; | |
b82d08cd | 71 | |
1da77581 TT |
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 | ||
dccee2de TT |
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 | ||
b82d08cd TT |
85 | /* If the BFD comes from an archive, this points to the archive's |
86 | BFD. Otherwise, this is NULL. */ | |
87 | bfd *archive_bfd; | |
e992eda4 | 88 | |
13aaf454 DE |
89 | /* Table of all the bfds this bfd has included. */ |
90 | VEC (bfdp) *included_bfds; | |
91 | ||
e992eda4 TT |
92 | /* The registry. */ |
93 | REGISTRY_FIELDS; | |
6ec53d05 TT |
94 | }; |
95 | ||
e992eda4 TT |
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 | ||
6ec53d05 TT |
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 | struct bfd * | |
145 | gdb_bfd_open (const char *name, const char *target, int fd) | |
146 | { | |
147 | hashval_t hash; | |
148 | void **slot; | |
149 | bfd *abfd; | |
150 | struct gdb_bfd_cache_search search; | |
151 | struct stat st; | |
152 | ||
153 | if (gdb_bfd_cache == NULL) | |
154 | gdb_bfd_cache = htab_create_alloc (1, hash_bfd, eq_bfd, NULL, | |
155 | xcalloc, xfree); | |
156 | ||
157 | if (fd == -1) | |
158 | { | |
614c279d | 159 | fd = gdb_open_cloexec (name, O_RDONLY | O_BINARY, 0); |
6ec53d05 TT |
160 | if (fd == -1) |
161 | { | |
162 | bfd_set_error (bfd_error_system_call); | |
163 | return NULL; | |
164 | } | |
165 | } | |
166 | ||
167 | search.filename = name; | |
168 | if (fstat (fd, &st) < 0) | |
169 | { | |
170 | /* Weird situation here. */ | |
171 | search.mtime = 0; | |
172 | } | |
173 | else | |
174 | search.mtime = st.st_mtime; | |
175 | ||
176 | /* Note that this must compute the same result as hash_bfd. */ | |
177 | hash = htab_hash_string (name); | |
178 | /* Note that we cannot use htab_find_slot_with_hash here, because | |
179 | opening the BFD may fail; and this would violate hashtab | |
180 | invariants. */ | |
181 | abfd = htab_find_with_hash (gdb_bfd_cache, &search, hash); | |
182 | if (abfd != NULL) | |
183 | { | |
184 | close (fd); | |
520b0001 TT |
185 | gdb_bfd_ref (abfd); |
186 | return abfd; | |
6ec53d05 TT |
187 | } |
188 | ||
189 | abfd = bfd_fopen (name, target, FOPEN_RB, fd); | |
190 | if (abfd == NULL) | |
191 | return NULL; | |
192 | ||
193 | slot = htab_find_slot_with_hash (gdb_bfd_cache, &search, hash, INSERT); | |
194 | gdb_assert (!*slot); | |
195 | *slot = abfd; | |
196 | ||
520b0001 TT |
197 | gdb_bfd_ref (abfd); |
198 | return abfd; | |
6ec53d05 TT |
199 | } |
200 | ||
4bf44c1c TT |
201 | /* A helper function that releases any section data attached to the |
202 | BFD. */ | |
203 | ||
204 | static void | |
205 | free_one_bfd_section (bfd *abfd, asection *sectp, void *ignore) | |
206 | { | |
207 | struct gdb_bfd_section_data *sect = bfd_get_section_userdata (abfd, sectp); | |
208 | ||
209 | if (sect != NULL && sect->data != NULL) | |
210 | { | |
211 | #ifdef HAVE_MMAP | |
212 | if (sect->map_addr != NULL) | |
213 | { | |
214 | int res; | |
215 | ||
216 | res = munmap (sect->map_addr, sect->map_len); | |
217 | gdb_assert (res == 0); | |
218 | } | |
219 | else | |
220 | #endif | |
221 | xfree (sect->data); | |
222 | } | |
223 | } | |
224 | ||
cbb099e8 TT |
225 | /* Close ABFD, and warn if that fails. */ |
226 | ||
227 | static int | |
228 | gdb_bfd_close_or_warn (struct bfd *abfd) | |
229 | { | |
230 | int ret; | |
231 | char *name = bfd_get_filename (abfd); | |
232 | ||
4bf44c1c TT |
233 | bfd_map_over_sections (abfd, free_one_bfd_section, NULL); |
234 | ||
cbb099e8 TT |
235 | ret = bfd_close (abfd); |
236 | ||
237 | if (!ret) | |
238 | warning (_("cannot close \"%s\": %s"), | |
239 | name, bfd_errmsg (bfd_get_error ())); | |
240 | ||
241 | return ret; | |
242 | } | |
243 | ||
596f7d67 | 244 | /* See gdb_bfd.h. */ |
cbb099e8 | 245 | |
520b0001 | 246 | void |
cbb099e8 TT |
247 | gdb_bfd_ref (struct bfd *abfd) |
248 | { | |
6ec53d05 | 249 | struct gdb_bfd_data *gdata; |
d6b28940 | 250 | void **slot; |
cbb099e8 TT |
251 | |
252 | if (abfd == NULL) | |
520b0001 | 253 | return; |
cbb099e8 | 254 | |
6ec53d05 | 255 | gdata = bfd_usrdata (abfd); |
cbb099e8 | 256 | |
6ec53d05 | 257 | if (gdata != NULL) |
cbb099e8 | 258 | { |
6ec53d05 | 259 | gdata->refc += 1; |
520b0001 | 260 | return; |
cbb099e8 TT |
261 | } |
262 | ||
ea9f10bb TT |
263 | /* Ask BFD to decompress sections in bfd_get_full_section_contents. */ |
264 | abfd->flags |= BFD_DECOMPRESS; | |
265 | ||
6ec53d05 TT |
266 | gdata = bfd_zalloc (abfd, sizeof (struct gdb_bfd_data)); |
267 | gdata->refc = 1; | |
268 | gdata->mtime = bfd_get_mtime (abfd); | |
b82d08cd | 269 | gdata->archive_bfd = NULL; |
6ec53d05 | 270 | bfd_usrdata (abfd) = gdata; |
d6b28940 | 271 | |
e992eda4 TT |
272 | bfd_alloc_data (abfd); |
273 | ||
d6b28940 TT |
274 | /* This is the first we've seen it, so add it to the hash table. */ |
275 | slot = htab_find_slot (all_bfds, abfd, INSERT); | |
276 | gdb_assert (slot && !*slot); | |
277 | *slot = abfd; | |
cbb099e8 TT |
278 | } |
279 | ||
596f7d67 | 280 | /* See gdb_bfd.h. */ |
cbb099e8 TT |
281 | |
282 | void | |
283 | gdb_bfd_unref (struct bfd *abfd) | |
284 | { | |
13aaf454 | 285 | int ix; |
6ec53d05 TT |
286 | struct gdb_bfd_data *gdata; |
287 | struct gdb_bfd_cache_search search; | |
13aaf454 | 288 | bfd *archive_bfd, *included_bfd; |
cbb099e8 TT |
289 | |
290 | if (abfd == NULL) | |
291 | return; | |
292 | ||
6ec53d05 TT |
293 | gdata = bfd_usrdata (abfd); |
294 | gdb_assert (gdata->refc >= 1); | |
cbb099e8 | 295 | |
6ec53d05 TT |
296 | gdata->refc -= 1; |
297 | if (gdata->refc > 0) | |
cbb099e8 TT |
298 | return; |
299 | ||
b82d08cd | 300 | archive_bfd = gdata->archive_bfd; |
6ec53d05 TT |
301 | search.filename = bfd_get_filename (abfd); |
302 | ||
303 | if (gdb_bfd_cache && search.filename) | |
304 | { | |
305 | hashval_t hash = htab_hash_string (search.filename); | |
306 | void **slot; | |
307 | ||
308 | search.mtime = gdata->mtime; | |
309 | slot = htab_find_slot_with_hash (gdb_bfd_cache, &search, hash, | |
310 | NO_INSERT); | |
311 | ||
312 | if (slot && *slot) | |
313 | htab_clear_slot (gdb_bfd_cache, slot); | |
314 | } | |
315 | ||
13aaf454 DE |
316 | for (ix = 0; |
317 | VEC_iterate (bfdp, gdata->included_bfds, ix, included_bfd); | |
318 | ++ix) | |
319 | gdb_bfd_unref (included_bfd); | |
320 | VEC_free (bfdp, gdata->included_bfds); | |
321 | ||
e992eda4 | 322 | bfd_free_data (abfd); |
cbb099e8 TT |
323 | bfd_usrdata (abfd) = NULL; /* Paranoia. */ |
324 | ||
d6b28940 TT |
325 | htab_remove_elt (all_bfds, abfd); |
326 | ||
cbb099e8 | 327 | gdb_bfd_close_or_warn (abfd); |
b82d08cd TT |
328 | |
329 | gdb_bfd_unref (archive_bfd); | |
cbb099e8 | 330 | } |
4bf44c1c TT |
331 | |
332 | /* A helper function that returns the section data descriptor | |
333 | associated with SECTION. If no such descriptor exists, a new one | |
334 | is allocated and cleared. */ | |
335 | ||
336 | static struct gdb_bfd_section_data * | |
337 | get_section_descriptor (asection *section) | |
338 | { | |
339 | struct gdb_bfd_section_data *result; | |
340 | ||
341 | result = bfd_get_section_userdata (section->owner, section); | |
342 | ||
343 | if (result == NULL) | |
344 | { | |
345 | result = bfd_zalloc (section->owner, sizeof (*result)); | |
346 | bfd_set_section_userdata (section->owner, section, result); | |
347 | } | |
348 | ||
349 | return result; | |
350 | } | |
351 | ||
4bf44c1c TT |
352 | /* See gdb_bfd.h. */ |
353 | ||
354 | const gdb_byte * | |
355 | gdb_bfd_map_section (asection *sectp, bfd_size_type *size) | |
356 | { | |
357 | bfd *abfd; | |
4bf44c1c | 358 | struct gdb_bfd_section_data *descriptor; |
ea9f10bb | 359 | bfd_byte *data; |
4bf44c1c TT |
360 | |
361 | gdb_assert ((sectp->flags & SEC_RELOC) == 0); | |
362 | gdb_assert (size != NULL); | |
363 | ||
364 | abfd = sectp->owner; | |
365 | ||
366 | descriptor = get_section_descriptor (sectp); | |
367 | ||
368 | /* If the data was already read for this BFD, just reuse it. */ | |
369 | if (descriptor->data != NULL) | |
370 | goto done; | |
371 | ||
ea9f10bb TT |
372 | #ifdef HAVE_MMAP |
373 | if (!bfd_is_section_compressed (abfd, sectp)) | |
4bf44c1c | 374 | { |
ea9f10bb TT |
375 | /* The page size, used when mmapping. */ |
376 | static int pagesize; | |
4bf44c1c | 377 | |
ea9f10bb TT |
378 | if (pagesize == 0) |
379 | pagesize = getpagesize (); | |
380 | ||
381 | /* Only try to mmap sections which are large enough: we don't want | |
382 | to waste space due to fragmentation. */ | |
383 | ||
384 | if (bfd_get_section_size (sectp) > 4 * pagesize) | |
385 | { | |
386 | descriptor->size = bfd_get_section_size (sectp); | |
387 | descriptor->data = bfd_mmap (abfd, 0, descriptor->size, PROT_READ, | |
388 | MAP_PRIVATE, sectp->filepos, | |
389 | &descriptor->map_addr, | |
390 | &descriptor->map_len); | |
391 | ||
392 | if ((caddr_t)descriptor->data != MAP_FAILED) | |
393 | { | |
4bf44c1c | 394 | #if HAVE_POSIX_MADVISE |
ea9f10bb TT |
395 | posix_madvise (descriptor->map_addr, descriptor->map_len, |
396 | POSIX_MADV_WILLNEED); | |
4bf44c1c | 397 | #endif |
ea9f10bb TT |
398 | goto done; |
399 | } | |
4bf44c1c | 400 | |
ea9f10bb TT |
401 | /* On failure, clear out the section data and try again. */ |
402 | memset (descriptor, 0, sizeof (*descriptor)); | |
403 | } | |
404 | } | |
4bf44c1c TT |
405 | #endif /* HAVE_MMAP */ |
406 | ||
ea9f10bb TT |
407 | /* Handle compressed sections, or ordinary uncompressed sections in |
408 | the no-mmap case. */ | |
4bf44c1c TT |
409 | |
410 | descriptor->size = bfd_get_section_size (sectp); | |
ea9f10bb | 411 | descriptor->data = NULL; |
4bf44c1c | 412 | |
ea9f10bb TT |
413 | data = NULL; |
414 | if (!bfd_get_full_section_contents (abfd, sectp, &data)) | |
415 | error (_("Can't read data for section '%s' in file '%s'"), | |
416 | bfd_get_section_name (abfd, sectp), | |
417 | bfd_get_filename (abfd)); | |
418 | descriptor->data = data; | |
4bf44c1c TT |
419 | |
420 | done: | |
421 | gdb_assert (descriptor->data != NULL); | |
422 | *size = descriptor->size; | |
423 | return descriptor->data; | |
424 | } | |
64c31149 | 425 | |
dccee2de TT |
426 | /* Return 32-bit CRC for ABFD. If successful store it to *FILE_CRC_RETURN and |
427 | return 1. Otherwise print a warning and return 0. ABFD seek position is | |
428 | not preserved. */ | |
429 | ||
430 | static int | |
431 | get_file_crc (bfd *abfd, unsigned long *file_crc_return) | |
432 | { | |
433 | unsigned long file_crc = 0; | |
434 | ||
435 | if (bfd_seek (abfd, 0, SEEK_SET) != 0) | |
436 | { | |
437 | warning (_("Problem reading \"%s\" for CRC: %s"), | |
438 | bfd_get_filename (abfd), bfd_errmsg (bfd_get_error ())); | |
439 | return 0; | |
440 | } | |
441 | ||
442 | for (;;) | |
443 | { | |
444 | gdb_byte buffer[8 * 1024]; | |
445 | bfd_size_type count; | |
446 | ||
447 | count = bfd_bread (buffer, sizeof (buffer), abfd); | |
448 | if (count == (bfd_size_type) -1) | |
449 | { | |
450 | warning (_("Problem reading \"%s\" for CRC: %s"), | |
451 | bfd_get_filename (abfd), bfd_errmsg (bfd_get_error ())); | |
452 | return 0; | |
453 | } | |
454 | if (count == 0) | |
455 | break; | |
456 | file_crc = bfd_calc_gnu_debuglink_crc32 (file_crc, buffer, count); | |
457 | } | |
458 | ||
459 | *file_crc_return = file_crc; | |
460 | return 1; | |
461 | } | |
462 | ||
463 | /* See gdb_bfd.h. */ | |
464 | ||
465 | int | |
466 | gdb_bfd_crc (struct bfd *abfd, unsigned long *crc_out) | |
467 | { | |
468 | struct gdb_bfd_data *gdata = bfd_usrdata (abfd); | |
469 | ||
470 | if (!gdata->crc_computed) | |
471 | gdata->crc_computed = get_file_crc (abfd, &gdata->crc); | |
472 | ||
473 | if (gdata->crc_computed) | |
474 | *crc_out = gdata->crc; | |
475 | return gdata->crc_computed; | |
476 | } | |
477 | ||
64c31149 TT |
478 | \f |
479 | ||
480 | /* See gdb_bfd.h. */ | |
481 | ||
482 | bfd * | |
483 | gdb_bfd_fopen (const char *filename, const char *target, const char *mode, | |
484 | int fd) | |
485 | { | |
486 | bfd *result = bfd_fopen (filename, target, mode, fd); | |
487 | ||
488 | if (result) | |
adcf2eed | 489 | gdb_bfd_ref (result); |
64c31149 TT |
490 | |
491 | return result; | |
492 | } | |
493 | ||
494 | /* See gdb_bfd.h. */ | |
495 | ||
496 | bfd * | |
497 | gdb_bfd_openr (const char *filename, const char *target) | |
498 | { | |
499 | bfd *result = bfd_openr (filename, target); | |
500 | ||
501 | if (result) | |
adcf2eed | 502 | gdb_bfd_ref (result); |
64c31149 TT |
503 | |
504 | return result; | |
505 | } | |
506 | ||
507 | /* See gdb_bfd.h. */ | |
508 | ||
509 | bfd * | |
510 | gdb_bfd_openw (const char *filename, const char *target) | |
511 | { | |
512 | bfd *result = bfd_openw (filename, target); | |
513 | ||
514 | if (result) | |
adcf2eed | 515 | gdb_bfd_ref (result); |
64c31149 TT |
516 | |
517 | return result; | |
518 | } | |
519 | ||
520 | /* See gdb_bfd.h. */ | |
521 | ||
522 | bfd * | |
523 | gdb_bfd_openr_iovec (const char *filename, const char *target, | |
524 | void *(*open_func) (struct bfd *nbfd, | |
525 | void *open_closure), | |
526 | void *open_closure, | |
527 | file_ptr (*pread_func) (struct bfd *nbfd, | |
528 | void *stream, | |
529 | void *buf, | |
530 | file_ptr nbytes, | |
531 | file_ptr offset), | |
532 | int (*close_func) (struct bfd *nbfd, | |
533 | void *stream), | |
534 | int (*stat_func) (struct bfd *abfd, | |
535 | void *stream, | |
536 | struct stat *sb)) | |
537 | { | |
538 | bfd *result = bfd_openr_iovec (filename, target, | |
539 | open_func, open_closure, | |
540 | pread_func, close_func, stat_func); | |
541 | ||
542 | if (result) | |
adcf2eed | 543 | gdb_bfd_ref (result); |
64c31149 TT |
544 | |
545 | return result; | |
546 | } | |
547 | ||
548 | /* See gdb_bfd.h. */ | |
549 | ||
0cd61f44 TT |
550 | void |
551 | gdb_bfd_mark_parent (bfd *child, bfd *parent) | |
552 | { | |
553 | struct gdb_bfd_data *gdata; | |
554 | ||
555 | gdb_bfd_ref (child); | |
556 | /* No need to stash the filename here, because we also keep a | |
557 | reference on the parent archive. */ | |
558 | ||
559 | gdata = bfd_usrdata (child); | |
560 | if (gdata->archive_bfd == NULL) | |
561 | { | |
562 | gdata->archive_bfd = parent; | |
563 | gdb_bfd_ref (parent); | |
564 | } | |
565 | else | |
566 | gdb_assert (gdata->archive_bfd == parent); | |
567 | } | |
568 | ||
569 | /* See gdb_bfd.h. */ | |
570 | ||
64c31149 TT |
571 | bfd * |
572 | gdb_bfd_openr_next_archived_file (bfd *archive, bfd *previous) | |
573 | { | |
574 | bfd *result = bfd_openr_next_archived_file (archive, previous); | |
575 | ||
576 | if (result) | |
0cd61f44 | 577 | gdb_bfd_mark_parent (result, archive); |
64c31149 TT |
578 | |
579 | return result; | |
580 | } | |
581 | ||
582 | /* See gdb_bfd.h. */ | |
583 | ||
13aaf454 DE |
584 | void |
585 | gdb_bfd_record_inclusion (bfd *includer, bfd *includee) | |
586 | { | |
587 | struct gdb_bfd_data *gdata; | |
588 | ||
589 | gdb_bfd_ref (includee); | |
590 | gdata = bfd_usrdata (includer); | |
591 | VEC_safe_push (bfdp, gdata->included_bfds, includee); | |
592 | } | |
593 | ||
594 | /* See gdb_bfd.h. */ | |
595 | ||
64c31149 TT |
596 | bfd * |
597 | gdb_bfd_fdopenr (const char *filename, const char *target, int fd) | |
598 | { | |
599 | bfd *result = bfd_fdopenr (filename, target, fd); | |
600 | ||
601 | if (result) | |
adcf2eed | 602 | gdb_bfd_ref (result); |
64c31149 TT |
603 | |
604 | return result; | |
605 | } | |
d6b28940 TT |
606 | |
607 | \f | |
608 | ||
65cf3563 TT |
609 | gdb_static_assert (ARRAY_SIZE (_bfd_std_section) == 4); |
610 | ||
611 | /* See gdb_bfd.h. */ | |
612 | ||
613 | int | |
614 | gdb_bfd_section_index (bfd *abfd, asection *section) | |
615 | { | |
616 | if (section == NULL) | |
617 | return -1; | |
618 | else if (section == bfd_com_section_ptr) | |
619 | return bfd_count_sections (abfd) + 1; | |
620 | else if (section == bfd_und_section_ptr) | |
621 | return bfd_count_sections (abfd) + 2; | |
622 | else if (section == bfd_abs_section_ptr) | |
623 | return bfd_count_sections (abfd) + 3; | |
624 | else if (section == bfd_ind_section_ptr) | |
625 | return bfd_count_sections (abfd) + 4; | |
626 | return section->index; | |
627 | } | |
628 | ||
629 | /* See gdb_bfd.h. */ | |
630 | ||
631 | int | |
632 | gdb_bfd_count_sections (bfd *abfd) | |
633 | { | |
634 | return bfd_count_sections (abfd) + 4; | |
635 | } | |
636 | ||
1da77581 TT |
637 | /* See gdb_bfd.h. */ |
638 | ||
639 | int | |
640 | gdb_bfd_requires_relocations (bfd *abfd) | |
641 | { | |
642 | struct gdb_bfd_data *gdata = bfd_usrdata (abfd); | |
643 | ||
644 | if (gdata->relocation_computed == 0) | |
645 | { | |
646 | asection *sect; | |
647 | ||
648 | for (sect = abfd->sections; sect != NULL; sect = sect->next) | |
649 | if ((sect->flags & SEC_RELOC) != 0) | |
650 | { | |
651 | gdata->needs_relocations = 1; | |
652 | break; | |
653 | } | |
654 | ||
655 | gdata->relocation_computed = 1; | |
656 | } | |
657 | ||
658 | return gdata->needs_relocations; | |
659 | } | |
660 | ||
65cf3563 TT |
661 | \f |
662 | ||
d6b28940 TT |
663 | /* A callback for htab_traverse that prints a single BFD. */ |
664 | ||
665 | static int | |
666 | print_one_bfd (void **slot, void *data) | |
667 | { | |
668 | bfd *abfd = *slot; | |
669 | struct gdb_bfd_data *gdata = bfd_usrdata (abfd); | |
670 | struct ui_out *uiout = data; | |
671 | struct cleanup *inner; | |
672 | ||
673 | inner = make_cleanup_ui_out_tuple_begin_end (uiout, NULL); | |
674 | ui_out_field_int (uiout, "refcount", gdata->refc); | |
675 | ui_out_field_string (uiout, "addr", host_address_to_string (abfd)); | |
676 | ui_out_field_string (uiout, "filename", bfd_get_filename (abfd)); | |
677 | ui_out_text (uiout, "\n"); | |
678 | do_cleanups (inner); | |
679 | ||
680 | return 1; | |
681 | } | |
682 | ||
683 | /* Implement the 'maint info bfd' command. */ | |
684 | ||
685 | static void | |
686 | maintenance_info_bfds (char *arg, int from_tty) | |
687 | { | |
688 | struct cleanup *cleanup; | |
689 | struct ui_out *uiout = current_uiout; | |
690 | ||
691 | cleanup = make_cleanup_ui_out_table_begin_end (uiout, 3, -1, "bfds"); | |
692 | ui_out_table_header (uiout, 10, ui_left, "refcount", "Refcount"); | |
693 | ui_out_table_header (uiout, 18, ui_left, "addr", "Address"); | |
694 | ui_out_table_header (uiout, 40, ui_left, "filename", "Filename"); | |
695 | ||
696 | ui_out_table_body (uiout); | |
697 | htab_traverse (all_bfds, print_one_bfd, uiout); | |
698 | ||
699 | do_cleanups (cleanup); | |
700 | } | |
701 | ||
702 | /* -Wmissing-prototypes */ | |
703 | extern initialize_file_ftype _initialize_gdb_bfd; | |
704 | ||
705 | void | |
706 | _initialize_gdb_bfd (void) | |
707 | { | |
708 | all_bfds = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer, | |
709 | NULL, xcalloc, xfree); | |
710 | ||
711 | add_cmd ("bfds", class_maintenance, maintenance_info_bfds, _("\ | |
712 | List the BFDs that are currently open."), | |
713 | &maintenanceinfolist); | |
714 | } |