* dwarf2read.c (try_open_dwo_file): use gdb_bfd_open.
[deliverable/binutils-gdb.git] / gdb / gdb_bfd.c
CommitLineData
cbb099e8
TT
1/* Definitions for BFD wrappers used by GDB.
2
6ec53d05 3 Copyright (C) 2011, 2012
cbb099e8
TT
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"
a4453b7e 24#include "gdb_string.h"
6ec53d05 25#include "hashtab.h"
a4453b7e
TT
26
27/* See gdb_bfd.h. */
28
29void
30gdb_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}
cbb099e8 41
6ec53d05
TT
42/* An object of this type is stored in each BFD's user data. */
43
44struct 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
55static 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
61struct 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
71static hashval_t
72hash_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
83static int
84eq_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
96struct bfd *
97gdb_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
cbb099e8
TT
152/* Close ABFD, and warn if that fails. */
153
154static int
155gdb_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
171struct bfd *
172gdb_bfd_ref (struct bfd *abfd)
173{
6ec53d05 174 struct gdb_bfd_data *gdata;
cbb099e8
TT
175
176 if (abfd == NULL)
177 return NULL;
178
6ec53d05 179 gdata = bfd_usrdata (abfd);
cbb099e8 180
6ec53d05 181 if (gdata != NULL)
cbb099e8 182 {
6ec53d05 183 gdata->refc += 1;
cbb099e8
TT
184 return abfd;
185 }
186
6ec53d05
TT
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;
cbb099e8
TT
191
192 return abfd;
193}
194
195/* Unreference and possibly close ABFD. */
196
197void
198gdb_bfd_unref (struct bfd *abfd)
199{
6ec53d05
TT
200 struct gdb_bfd_data *gdata;
201 struct gdb_bfd_cache_search search;
cbb099e8
TT
202
203 if (abfd == NULL)
204 return;
205
6ec53d05
TT
206 gdata = bfd_usrdata (abfd);
207 gdb_assert (gdata->refc >= 1);
cbb099e8 208
6ec53d05
TT
209 gdata->refc -= 1;
210 if (gdata->refc > 0)
cbb099e8
TT
211 return;
212
6ec53d05
TT
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
cbb099e8
TT
228 bfd_usrdata (abfd) = NULL; /* Paranoia. */
229
cbb099e8
TT
230 gdb_bfd_close_or_warn (abfd);
231}
This page took 0.03212 seconds and 4 git commands to generate.