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