1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
|
/* Definitions for BFD wrappers used by GDB.
Copyright (C) 2011, 2012
Free Software Foundation, Inc.
This file is part of GDB.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#include "defs.h"
#include "gdb_bfd.h"
#include "gdb_assert.h"
#include "gdb_string.h"
#include "hashtab.h"
/* See gdb_bfd.h. */
void
gdb_bfd_stash_filename (struct bfd *abfd)
{
char *name = bfd_get_filename (abfd);
char *data;
data = bfd_alloc (abfd, strlen (name) + 1);
strcpy (data, name);
/* Unwarranted chumminess with BFD. */
abfd->filename = data;
}
/* An object of this type is stored in each BFD's user data. */
struct gdb_bfd_data
{
/* The reference count. */
int refc;
/* The mtime of the BFD at the point the cache entry was made. */
time_t mtime;
};
/* A hash table storing all the BFDs maintained in the cache. */
static htab_t gdb_bfd_cache;
/* The type of an object being looked up in gdb_bfd_cache. We use
htab's capability of storing one kind of object (BFD in this case)
and using a different sort of object for searching. */
struct gdb_bfd_cache_search
{
/* The filename. */
const char *filename;
/* The mtime. */
time_t mtime;
};
/* A hash function for BFDs. */
static hashval_t
hash_bfd (const void *b)
{
const bfd *abfd = b;
/* It is simplest to just hash the filename. */
return htab_hash_string (bfd_get_filename (abfd));
}
/* An equality function for BFDs. Note that this expects the caller
to search using struct gdb_bfd_cache_search only, not BFDs. */
static int
eq_bfd (const void *a, const void *b)
{
const bfd *abfd = a;
const struct gdb_bfd_cache_search *s = b;
struct gdb_bfd_data *gdata = bfd_usrdata (abfd);
return (gdata->mtime == s->mtime
&& strcmp (bfd_get_filename (abfd), s->filename) == 0);
}
/* See gdb_bfd.h. */
struct bfd *
gdb_bfd_open (const char *name, const char *target, int fd)
{
hashval_t hash;
void **slot;
bfd *abfd;
struct gdb_bfd_cache_search search;
struct stat st;
if (gdb_bfd_cache == NULL)
gdb_bfd_cache = htab_create_alloc (1, hash_bfd, eq_bfd, NULL,
xcalloc, xfree);
if (fd == -1)
{
fd = open (name, O_RDONLY | O_BINARY);
if (fd == -1)
{
bfd_set_error (bfd_error_system_call);
return NULL;
}
}
search.filename = name;
if (fstat (fd, &st) < 0)
{
/* Weird situation here. */
search.mtime = 0;
}
else
search.mtime = st.st_mtime;
/* Note that this must compute the same result as hash_bfd. */
hash = htab_hash_string (name);
/* Note that we cannot use htab_find_slot_with_hash here, because
opening the BFD may fail; and this would violate hashtab
invariants. */
abfd = htab_find_with_hash (gdb_bfd_cache, &search, hash);
if (abfd != NULL)
{
close (fd);
return gdb_bfd_ref (abfd);
}
abfd = bfd_fopen (name, target, FOPEN_RB, fd);
if (abfd == NULL)
return NULL;
slot = htab_find_slot_with_hash (gdb_bfd_cache, &search, hash, INSERT);
gdb_assert (!*slot);
*slot = abfd;
gdb_bfd_stash_filename (abfd);
return gdb_bfd_ref (abfd);
}
/* Close ABFD, and warn if that fails. */
static int
gdb_bfd_close_or_warn (struct bfd *abfd)
{
int ret;
char *name = bfd_get_filename (abfd);
ret = bfd_close (abfd);
if (!ret)
warning (_("cannot close \"%s\": %s"),
name, bfd_errmsg (bfd_get_error ()));
return ret;
}
/* Add reference to ABFD. Returns ABFD. */
struct bfd *
gdb_bfd_ref (struct bfd *abfd)
{
struct gdb_bfd_data *gdata;
if (abfd == NULL)
return NULL;
gdata = bfd_usrdata (abfd);
if (gdata != NULL)
{
gdata->refc += 1;
return abfd;
}
gdata = bfd_zalloc (abfd, sizeof (struct gdb_bfd_data));
gdata->refc = 1;
gdata->mtime = bfd_get_mtime (abfd);
bfd_usrdata (abfd) = gdata;
return abfd;
}
/* Unreference and possibly close ABFD. */
void
gdb_bfd_unref (struct bfd *abfd)
{
struct gdb_bfd_data *gdata;
struct gdb_bfd_cache_search search;
if (abfd == NULL)
return;
gdata = bfd_usrdata (abfd);
gdb_assert (gdata->refc >= 1);
gdata->refc -= 1;
if (gdata->refc > 0)
return;
search.filename = bfd_get_filename (abfd);
if (gdb_bfd_cache && search.filename)
{
hashval_t hash = htab_hash_string (search.filename);
void **slot;
search.mtime = gdata->mtime;
slot = htab_find_slot_with_hash (gdb_bfd_cache, &search, hash,
NO_INSERT);
if (slot && *slot)
htab_clear_slot (gdb_bfd_cache, slot);
}
bfd_usrdata (abfd) = NULL; /* Paranoia. */
gdb_bfd_close_or_warn (abfd);
}
|