1 /* Definitions for BFD wrappers used by GDB.
3 Copyright (C) 2011, 2012
4 Free Software Foundation, Inc.
6 This file is part of GDB.
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.
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.
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/>. */
23 #include "gdb_assert.h"
24 #include "gdb_string.h"
32 #define MAP_FAILED ((void *) -1)
36 /* An object of this type is stored in the section's user data when
39 struct gdb_bfd_section_data
41 /* Size of the data. */
43 /* If the data was mmapped, this is the length of the map. */
44 bfd_size_type map_len;
45 /* The data. If NULL, the section data has not been read. */
47 /* If the data was mmapped, this is the map address. */
54 gdb_bfd_stash_filename (struct bfd *abfd)
56 char *name = bfd_get_filename (abfd);
59 data = bfd_alloc (abfd, strlen (name) + 1);
62 /* Unwarranted chumminess with BFD. */
63 abfd->filename = data;
66 /* An object of this type is stored in each BFD's user data. */
70 /* The reference count. */
73 /* The mtime of the BFD at the point the cache entry was made. */
77 /* A hash table storing all the BFDs maintained in the cache. */
79 static htab_t gdb_bfd_cache;
81 /* The type of an object being looked up in gdb_bfd_cache. We use
82 htab's capability of storing one kind of object (BFD in this case)
83 and using a different sort of object for searching. */
85 struct gdb_bfd_cache_search
93 /* A hash function for BFDs. */
96 hash_bfd (const void *b)
100 /* It is simplest to just hash the filename. */
101 return htab_hash_string (bfd_get_filename (abfd));
104 /* An equality function for BFDs. Note that this expects the caller
105 to search using struct gdb_bfd_cache_search only, not BFDs. */
108 eq_bfd (const void *a, const void *b)
111 const struct gdb_bfd_cache_search *s = b;
112 struct gdb_bfd_data *gdata = bfd_usrdata (abfd);
114 return (gdata->mtime == s->mtime
115 && strcmp (bfd_get_filename (abfd), s->filename) == 0);
121 gdb_bfd_open (const char *name, const char *target, int fd)
126 struct gdb_bfd_cache_search search;
129 if (gdb_bfd_cache == NULL)
130 gdb_bfd_cache = htab_create_alloc (1, hash_bfd, eq_bfd, NULL,
135 fd = open (name, O_RDONLY | O_BINARY);
138 bfd_set_error (bfd_error_system_call);
143 search.filename = name;
144 if (fstat (fd, &st) < 0)
146 /* Weird situation here. */
150 search.mtime = st.st_mtime;
152 /* Note that this must compute the same result as hash_bfd. */
153 hash = htab_hash_string (name);
154 /* Note that we cannot use htab_find_slot_with_hash here, because
155 opening the BFD may fail; and this would violate hashtab
157 abfd = htab_find_with_hash (gdb_bfd_cache, &search, hash);
165 abfd = bfd_fopen (name, target, FOPEN_RB, fd);
169 slot = htab_find_slot_with_hash (gdb_bfd_cache, &search, hash, INSERT);
173 gdb_bfd_stash_filename (abfd);
178 /* A helper function that releases any section data attached to the
182 free_one_bfd_section (bfd *abfd, asection *sectp, void *ignore)
184 struct gdb_bfd_section_data *sect = bfd_get_section_userdata (abfd, sectp);
186 if (sect != NULL && sect->data != NULL)
189 if (sect->map_addr != NULL)
193 res = munmap (sect->map_addr, sect->map_len);
194 gdb_assert (res == 0);
202 /* Close ABFD, and warn if that fails. */
205 gdb_bfd_close_or_warn (struct bfd *abfd)
208 char *name = bfd_get_filename (abfd);
210 bfd_map_over_sections (abfd, free_one_bfd_section, NULL);
212 ret = bfd_close (abfd);
215 warning (_("cannot close \"%s\": %s"),
216 name, bfd_errmsg (bfd_get_error ()));
224 gdb_bfd_ref (struct bfd *abfd)
226 struct gdb_bfd_data *gdata;
231 gdata = bfd_usrdata (abfd);
239 gdata = bfd_zalloc (abfd, sizeof (struct gdb_bfd_data));
241 gdata->mtime = bfd_get_mtime (abfd);
242 bfd_usrdata (abfd) = gdata;
248 gdb_bfd_unref (struct bfd *abfd)
250 struct gdb_bfd_data *gdata;
251 struct gdb_bfd_cache_search search;
256 gdata = bfd_usrdata (abfd);
257 gdb_assert (gdata->refc >= 1);
263 search.filename = bfd_get_filename (abfd);
265 if (gdb_bfd_cache && search.filename)
267 hashval_t hash = htab_hash_string (search.filename);
270 search.mtime = gdata->mtime;
271 slot = htab_find_slot_with_hash (gdb_bfd_cache, &search, hash,
275 htab_clear_slot (gdb_bfd_cache, slot);
278 bfd_usrdata (abfd) = NULL; /* Paranoia. */
280 gdb_bfd_close_or_warn (abfd);
283 /* A helper function that returns the section data descriptor
284 associated with SECTION. If no such descriptor exists, a new one
285 is allocated and cleared. */
287 static struct gdb_bfd_section_data *
288 get_section_descriptor (asection *section)
290 struct gdb_bfd_section_data *result;
292 result = bfd_get_section_userdata (section->owner, section);
296 result = bfd_zalloc (section->owner, sizeof (*result));
297 bfd_set_section_userdata (section->owner, section, result);
303 /* Decompress a section that was compressed using zlib. Store the
304 decompressed buffer, and its size, in DESCRIPTOR. */
307 zlib_decompress_section (asection *sectp,
308 struct gdb_bfd_section_data *descriptor)
310 bfd *abfd = sectp->owner;
312 error (_("Support for zlib-compressed data (from '%s', section '%s') "
313 "is disabled in this copy of GDB"),
314 bfd_get_filename (abfd),
315 bfd_get_section_name (sectp));
317 bfd_size_type compressed_size = bfd_get_section_size (sectp);
318 gdb_byte *compressed_buffer = xmalloc (compressed_size);
319 struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
320 struct cleanup *inner_cleanup;
321 bfd_size_type uncompressed_size;
322 gdb_byte *uncompressed_buffer;
325 int header_size = 12;
326 struct dwarf2_per_bfd_section *section_data;
328 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
329 || bfd_bread (compressed_buffer,
330 compressed_size, abfd) != compressed_size)
331 error (_("can't read data from '%s', section '%s'"),
332 bfd_get_filename (abfd),
333 bfd_get_section_name (abfd, sectp));
335 /* Read the zlib header. In this case, it should be "ZLIB" followed
336 by the uncompressed section size, 8 bytes in big-endian order. */
337 if (compressed_size < header_size
338 || strncmp (compressed_buffer, "ZLIB", 4) != 0)
339 error (_("corrupt ZLIB header from '%s', section '%s'"),
340 bfd_get_filename (abfd),
341 bfd_get_section_name (abfd, sectp));
342 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
343 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
344 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
345 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
346 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
347 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
348 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
349 uncompressed_size += compressed_buffer[11];
351 /* It is possible the section consists of several compressed
352 buffers concatenated together, so we uncompress in a loop. */
356 strm.avail_in = compressed_size - header_size;
357 strm.next_in = (Bytef*) compressed_buffer + header_size;
358 strm.avail_out = uncompressed_size;
359 uncompressed_buffer = xmalloc (uncompressed_size);
360 inner_cleanup = make_cleanup (xfree, uncompressed_buffer);
361 rc = inflateInit (&strm);
362 while (strm.avail_in > 0)
365 error (_("setting up uncompression in '%s', section '%s': %d"),
366 bfd_get_filename (abfd),
367 bfd_get_section_name (abfd, sectp),
369 strm.next_out = ((Bytef*) uncompressed_buffer
370 + (uncompressed_size - strm.avail_out));
371 rc = inflate (&strm, Z_FINISH);
372 if (rc != Z_STREAM_END)
373 error (_("zlib error uncompressing from '%s', section '%s': %d"),
374 bfd_get_filename (abfd),
375 bfd_get_section_name (abfd, sectp),
377 rc = inflateReset (&strm);
379 rc = inflateEnd (&strm);
381 || strm.avail_out != 0)
382 error (_("concluding uncompression in '%s', section '%s': %d"),
383 bfd_get_filename (abfd),
384 bfd_get_section_name (abfd, sectp),
387 discard_cleanups (inner_cleanup);
388 do_cleanups (cleanup);
390 /* Attach the data to the BFD section. */
391 descriptor->data = uncompressed_buffer;
392 descriptor->size = uncompressed_size;
399 gdb_bfd_map_section (asection *sectp, bfd_size_type *size)
402 gdb_byte *buf, *retbuf;
403 unsigned char header[4];
404 struct gdb_bfd_section_data *descriptor;
406 gdb_assert ((sectp->flags & SEC_RELOC) == 0);
407 gdb_assert (size != NULL);
411 descriptor = get_section_descriptor (sectp);
413 /* If the data was already read for this BFD, just reuse it. */
414 if (descriptor->data != NULL)
417 /* Check if the file has a 4-byte header indicating compression. */
418 if (bfd_get_section_size (sectp) > sizeof (header)
419 && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
420 && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
422 /* Upon decompression, update the buffer and its size. */
423 if (strncmp (header, "ZLIB", sizeof (header)) == 0)
425 zlib_decompress_section (sectp, descriptor);
432 /* The page size, used when mmapping. */
436 pagesize = getpagesize ();
438 /* Only try to mmap sections which are large enough: we don't want
439 to waste space due to fragmentation. */
441 if (bfd_get_section_size (sectp) > 4 * pagesize)
443 descriptor->size = bfd_get_section_size (sectp);
444 descriptor->data = bfd_mmap (abfd, 0, descriptor->size, PROT_READ,
445 MAP_PRIVATE, sectp->filepos,
446 &descriptor->map_addr,
447 &descriptor->map_len);
449 if ((caddr_t)descriptor->data != MAP_FAILED)
451 #if HAVE_POSIX_MADVISE
452 posix_madvise (descriptor->map_addr, descriptor->map_len,
453 POSIX_MADV_WILLNEED);
458 /* On failure, clear out the section data and try again. */
459 memset (descriptor, 0, sizeof (*descriptor));
462 #endif /* HAVE_MMAP */
464 /* If we get here, we are a normal, not-compressed section. */
466 descriptor->size = bfd_get_section_size (sectp);
467 descriptor->data = xmalloc (descriptor->size);
469 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
470 || bfd_bread (descriptor->data, bfd_get_section_size (sectp),
471 abfd) != bfd_get_section_size (sectp))
473 xfree (descriptor->data);
474 descriptor->data = NULL;
475 error (_("Can't read data for section '%s'"),
476 bfd_get_filename (abfd));
480 gdb_assert (descriptor->data != NULL);
481 *size = descriptor->size;
482 return descriptor->data;
490 gdb_bfd_fopen (const char *filename, const char *target, const char *mode,
493 bfd *result = bfd_fopen (filename, target, mode, fd);
497 gdb_bfd_stash_filename (result);
498 gdb_bfd_ref (result);
507 gdb_bfd_openr (const char *filename, const char *target)
509 bfd *result = bfd_openr (filename, target);
513 gdb_bfd_stash_filename (result);
514 gdb_bfd_ref (result);
523 gdb_bfd_openw (const char *filename, const char *target)
525 bfd *result = bfd_openw (filename, target);
529 gdb_bfd_stash_filename (result);
530 gdb_bfd_ref (result);
539 gdb_bfd_openr_iovec (const char *filename, const char *target,
540 void *(*open_func) (struct bfd *nbfd,
543 file_ptr (*pread_func) (struct bfd *nbfd,
548 int (*close_func) (struct bfd *nbfd,
550 int (*stat_func) (struct bfd *abfd,
554 bfd *result = bfd_openr_iovec (filename, target,
555 open_func, open_closure,
556 pread_func, close_func, stat_func);
560 gdb_bfd_ref (result);
561 gdb_bfd_stash_filename (result);
570 gdb_bfd_openr_next_archived_file (bfd *archive, bfd *previous)
572 bfd *result = bfd_openr_next_archived_file (archive, previous);
576 gdb_bfd_ref (result);
577 /* No need to stash the filename here. */
586 gdb_bfd_fdopenr (const char *filename, const char *target, int fd)
588 bfd *result = bfd_fdopenr (filename, target, fd);
592 gdb_bfd_ref (result);
593 gdb_bfd_stash_filename (result);