]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/gdb_bfd.c
* dwarf2read.c (try_open_dwo_file): Use gdb_bfd_ref and
[thirdparty/binutils-gdb.git] / gdb / gdb_bfd.c
CommitLineData
cbb099e8
TT
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
25/* Close ABFD, and warn if that fails. */
26
27static int
28gdb_bfd_close_or_warn (struct bfd *abfd)
29{
30 int ret;
31 char *name = bfd_get_filename (abfd);
32
33 ret = bfd_close (abfd);
34
35 if (!ret)
36 warning (_("cannot close \"%s\": %s"),
37 name, bfd_errmsg (bfd_get_error ()));
38
39 return ret;
40}
41
42/* Add reference to ABFD. Returns ABFD. */
43
44struct bfd *
45gdb_bfd_ref (struct bfd *abfd)
46{
47 int *p_refcount;
48
49 if (abfd == NULL)
50 return NULL;
51
52 p_refcount = bfd_usrdata (abfd);
53
54 if (p_refcount != NULL)
55 {
56 *p_refcount += 1;
57 return abfd;
58 }
59
60 p_refcount = xmalloc (sizeof (*p_refcount));
61 *p_refcount = 1;
62 bfd_usrdata (abfd) = p_refcount;
63
64 return abfd;
65}
66
67/* Unreference and possibly close ABFD. */
68
69void
70gdb_bfd_unref (struct bfd *abfd)
71{
72 int *p_refcount;
73 char *name;
74
75 if (abfd == NULL)
76 return;
77
78 p_refcount = bfd_usrdata (abfd);
79 gdb_assert (*p_refcount >= 1);
80
81 *p_refcount -= 1;
82 if (*p_refcount > 0)
83 return;
84
85 xfree (p_refcount);
86 bfd_usrdata (abfd) = NULL; /* Paranoia. */
87
88 name = bfd_get_filename (abfd);
89 gdb_bfd_close_or_warn (abfd);
90}