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