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