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