]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/gdb_bfd.c
* symfile.c (symfile_bfd_open): Don't copy name. Call
[thirdparty/binutils-gdb.git] / gdb / gdb_bfd.c
1 /* Definitions for BFD wrappers used by GDB.
2
3 Copyright (C) 2011
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
26 /* See gdb_bfd.h. */
27
28 void
29 gdb_bfd_stash_filename (struct bfd *abfd)
30 {
31 char *name = bfd_get_filename (abfd);
32 char *data;
33
34 data = bfd_alloc (abfd, strlen (name) + 1);
35 strcpy (data, name);
36
37 /* Unwarranted chumminess with BFD. */
38 abfd->filename = data;
39 }
40
41 /* Close ABFD, and warn if that fails. */
42
43 static int
44 gdb_bfd_close_or_warn (struct bfd *abfd)
45 {
46 int ret;
47 char *name = bfd_get_filename (abfd);
48
49 ret = bfd_close (abfd);
50
51 if (!ret)
52 warning (_("cannot close \"%s\": %s"),
53 name, bfd_errmsg (bfd_get_error ()));
54
55 return ret;
56 }
57
58 /* Add reference to ABFD. Returns ABFD. */
59
60 struct bfd *
61 gdb_bfd_ref (struct bfd *abfd)
62 {
63 int *p_refcount;
64
65 if (abfd == NULL)
66 return NULL;
67
68 p_refcount = bfd_usrdata (abfd);
69
70 if (p_refcount != NULL)
71 {
72 *p_refcount += 1;
73 return abfd;
74 }
75
76 p_refcount = xmalloc (sizeof (*p_refcount));
77 *p_refcount = 1;
78 bfd_usrdata (abfd) = p_refcount;
79
80 return abfd;
81 }
82
83 /* Unreference and possibly close ABFD. */
84
85 void
86 gdb_bfd_unref (struct bfd *abfd)
87 {
88 int *p_refcount;
89 char *name;
90
91 if (abfd == NULL)
92 return;
93
94 p_refcount = bfd_usrdata (abfd);
95 gdb_assert (*p_refcount >= 1);
96
97 *p_refcount -= 1;
98 if (*p_refcount > 0)
99 return;
100
101 xfree (p_refcount);
102 bfd_usrdata (abfd) = NULL; /* Paranoia. */
103
104 name = bfd_get_filename (abfd);
105 gdb_bfd_close_or_warn (abfd);
106 }