* gdb_bfd.c (struct gdb_bfd_data): New.
[external/binutils.git] / gdb / gdb_bfd.c
1 /* Definitions for BFD wrappers used by GDB.
2
3    Copyright (C) 2011, 2012
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"
24 #include "gdb_string.h"
25 #include "hashtab.h"
26
27 /* See gdb_bfd.h.  */
28
29 void
30 gdb_bfd_stash_filename (struct bfd *abfd)
31 {
32   char *name = bfd_get_filename (abfd);
33   char *data;
34
35   data = bfd_alloc (abfd, strlen (name) + 1);
36   strcpy (data, name);
37
38   /* Unwarranted chumminess with BFD.  */
39   abfd->filename = data;
40 }
41
42 /* An object of this type is stored in each BFD's user data.  */
43
44 struct gdb_bfd_data
45 {
46   /* The reference count.  */
47   int refc;
48
49   /* The mtime of the BFD at the point the cache entry was made.  */
50   time_t mtime;
51 };
52
53 /* A hash table storing all the BFDs maintained in the cache.  */
54
55 static htab_t gdb_bfd_cache;
56
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.  */
60
61 struct gdb_bfd_cache_search
62 {
63   /* The filename.  */
64   const char *filename;
65   /* The mtime.  */
66   time_t mtime;
67 };
68
69 /* A hash function for BFDs.  */
70
71 static hashval_t
72 hash_bfd (const void *b)
73 {
74   const bfd *abfd = b;
75
76   /* It is simplest to just hash the filename.  */
77   return htab_hash_string (bfd_get_filename (abfd));
78 }
79
80 /* An equality function for BFDs.  Note that this expects the caller
81    to search using struct gdb_bfd_cache_search only, not BFDs.  */
82
83 static int
84 eq_bfd (const void *a, const void *b)
85 {
86   const bfd *abfd = a;
87   const struct gdb_bfd_cache_search *s = b;
88   struct gdb_bfd_data *gdata = bfd_usrdata (abfd);
89
90   return (gdata->mtime == s->mtime
91           && strcmp (bfd_get_filename (abfd), s->filename) == 0);
92 }
93
94 /* See gdb_bfd.h.  */
95
96 struct bfd *
97 gdb_bfd_open (const char *name, const char *target, int fd)
98 {
99   hashval_t hash;
100   void **slot;
101   bfd *abfd;
102   struct gdb_bfd_cache_search search;
103   struct stat st;
104
105   if (gdb_bfd_cache == NULL)
106     gdb_bfd_cache = htab_create_alloc (1, hash_bfd, eq_bfd, NULL,
107                                        xcalloc, xfree);
108
109   if (fd == -1)
110     {
111       fd = open (name, O_RDONLY | O_BINARY);
112       if (fd == -1)
113         {
114           bfd_set_error (bfd_error_system_call);
115           return NULL;
116         }
117     }
118
119   search.filename = name;
120   if (fstat (fd, &st) < 0)
121     {
122       /* Weird situation here.  */
123       search.mtime = 0;
124     }
125   else
126     search.mtime = st.st_mtime;
127
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
132      invariants.  */
133   abfd = htab_find_with_hash (gdb_bfd_cache, &search, hash);
134   if (abfd != NULL)
135     {
136       close (fd);
137       return gdb_bfd_ref (abfd);
138     }
139
140   abfd = bfd_fopen (name, target, FOPEN_RB, fd);
141   if (abfd == NULL)
142     return NULL;
143
144   slot = htab_find_slot_with_hash (gdb_bfd_cache, &search, hash, INSERT);
145   gdb_assert (!*slot);
146   *slot = abfd;
147
148   gdb_bfd_stash_filename (abfd);
149   return gdb_bfd_ref (abfd);
150 }
151
152 /* Close ABFD, and warn if that fails.  */
153
154 static int
155 gdb_bfd_close_or_warn (struct bfd *abfd)
156 {
157   int ret;
158   char *name = bfd_get_filename (abfd);
159
160   ret = bfd_close (abfd);
161
162   if (!ret)
163     warning (_("cannot close \"%s\": %s"),
164              name, bfd_errmsg (bfd_get_error ()));
165
166   return ret;
167 }
168
169 /* Add reference to ABFD.  Returns ABFD.  */
170
171 struct bfd *
172 gdb_bfd_ref (struct bfd *abfd)
173 {
174   struct gdb_bfd_data *gdata;
175
176   if (abfd == NULL)
177     return NULL;
178
179   gdata = bfd_usrdata (abfd);
180
181   if (gdata != NULL)
182     {
183       gdata->refc += 1;
184       return abfd;
185     }
186
187   gdata = bfd_zalloc (abfd, sizeof (struct gdb_bfd_data));
188   gdata->refc = 1;
189   gdata->mtime = bfd_get_mtime (abfd);
190   bfd_usrdata (abfd) = gdata;
191
192   return abfd;
193 }
194
195 /* Unreference and possibly close ABFD.  */
196
197 void
198 gdb_bfd_unref (struct bfd *abfd)
199 {
200   struct gdb_bfd_data *gdata;
201   struct gdb_bfd_cache_search search;
202
203   if (abfd == NULL)
204     return;
205
206   gdata = bfd_usrdata (abfd);
207   gdb_assert (gdata->refc >= 1);
208
209   gdata->refc -= 1;
210   if (gdata->refc > 0)
211     return;
212
213   search.filename = bfd_get_filename (abfd);
214
215   if (gdb_bfd_cache && search.filename)
216     {
217       hashval_t hash = htab_hash_string (search.filename);
218       void **slot;
219
220       search.mtime = gdata->mtime;
221       slot = htab_find_slot_with_hash (gdb_bfd_cache, &search, hash,
222                                        NO_INSERT);
223
224       if (slot && *slot)
225         htab_clear_slot (gdb_bfd_cache, slot);
226     }
227
228   bfd_usrdata (abfd) = NULL;  /* Paranoia.  */
229
230   gdb_bfd_close_or_warn (abfd);
231 }