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"
30 gdb_bfd_stash_filename (struct bfd
*abfd
)
32 char *name
= bfd_get_filename (abfd
);
35 data
= bfd_alloc (abfd
, strlen (name
) + 1);
38 /* Unwarranted chumminess with BFD. */
39 abfd
->filename
= data
;
42 /* An object of this type is stored in each BFD's user data. */
46 /* The reference count. */
49 /* The mtime of the BFD at the point the cache entry was made. */
53 /* A hash table storing all the BFDs maintained in the cache. */
55 static htab_t gdb_bfd_cache
;
57 /* The type of an object being looked up in gdb_bfd_cache. We use
58 htab's capability of storing one kind of object (BFD in this case)
59 and using a different sort of object for searching. */
61 struct gdb_bfd_cache_search
69 /* A hash function for BFDs. */
72 hash_bfd (const void *b
)
76 /* It is simplest to just hash the filename. */
77 return htab_hash_string (bfd_get_filename (abfd
));
80 /* An equality function for BFDs. Note that this expects the caller
81 to search using struct gdb_bfd_cache_search only, not BFDs. */
84 eq_bfd (const void *a
, const void *b
)
87 const struct gdb_bfd_cache_search
*s
= b
;
88 struct gdb_bfd_data
*gdata
= bfd_usrdata (abfd
);
90 return (gdata
->mtime
== s
->mtime
91 && strcmp (bfd_get_filename (abfd
), s
->filename
) == 0);
97 gdb_bfd_open (const char *name
, const char *target
, int fd
)
102 struct gdb_bfd_cache_search search
;
105 if (gdb_bfd_cache
== NULL
)
106 gdb_bfd_cache
= htab_create_alloc (1, hash_bfd
, eq_bfd
, NULL
,
111 fd
= open (name
, O_RDONLY
| O_BINARY
);
114 bfd_set_error (bfd_error_system_call
);
119 search
.filename
= name
;
120 if (fstat (fd
, &st
) < 0)
122 /* Weird situation here. */
126 search
.mtime
= st
.st_mtime
;
128 /* Note that this must compute the same result as hash_bfd. */
129 hash
= htab_hash_string (name
);
130 /* Note that we cannot use htab_find_slot_with_hash here, because
131 opening the BFD may fail; and this would violate hashtab
133 abfd
= htab_find_with_hash (gdb_bfd_cache
, &search
, hash
);
137 return gdb_bfd_ref (abfd
);
140 abfd
= bfd_fopen (name
, target
, FOPEN_RB
, fd
);
144 slot
= htab_find_slot_with_hash (gdb_bfd_cache
, &search
, hash
, INSERT
);
148 gdb_bfd_stash_filename (abfd
);
149 return gdb_bfd_ref (abfd
);
152 /* Close ABFD, and warn if that fails. */
155 gdb_bfd_close_or_warn (struct bfd
*abfd
)
158 char *name
= bfd_get_filename (abfd
);
160 ret
= bfd_close (abfd
);
163 warning (_("cannot close \"%s\": %s"),
164 name
, bfd_errmsg (bfd_get_error ()));
169 /* Add reference to ABFD. Returns ABFD. */
172 gdb_bfd_ref (struct bfd
*abfd
)
174 struct gdb_bfd_data
*gdata
;
179 gdata
= bfd_usrdata (abfd
);
187 gdata
= bfd_zalloc (abfd
, sizeof (struct gdb_bfd_data
));
189 gdata
->mtime
= bfd_get_mtime (abfd
);
190 bfd_usrdata (abfd
) = gdata
;
195 /* Unreference and possibly close ABFD. */
198 gdb_bfd_unref (struct bfd
*abfd
)
200 struct gdb_bfd_data
*gdata
;
201 struct gdb_bfd_cache_search search
;
206 gdata
= bfd_usrdata (abfd
);
207 gdb_assert (gdata
->refc
>= 1);
213 search
.filename
= bfd_get_filename (abfd
);
215 if (gdb_bfd_cache
&& search
.filename
)
217 hashval_t hash
= htab_hash_string (search
.filename
);
220 search
.mtime
= gdata
->mtime
;
221 slot
= htab_find_slot_with_hash (gdb_bfd_cache
, &search
, hash
,
225 htab_clear_slot (gdb_bfd_cache
, slot
);
228 bfd_usrdata (abfd
) = NULL
; /* Paranoia. */
230 gdb_bfd_close_or_warn (abfd
);