gdb_bfd_unref.
(free_dwo_file): Use gdb_bfd_unref.
* cli/cli-dump.c: Include gdb_bfd.h.
(bfd_openw_with_cleanup): Use gdb_bfd_ref.
(bfd_openr_with_cleanup): Likewise.
* windows-nat.c (windows_make_so): Use gdb_bfd_ref,
gdb_bfd_unref.
* utils.c: Include gdb_bfd.h.
(do_bfd_close_cleanup): Use gdb_bfd_unref.
* symfile.c: Include gdb_bfd.h.
(separate_debug_file_exists): Use gdb_bfd_unref.
(bfd_open_maybe_remote): Use gdb_bfd_ref.
(symfile_bfd_open): Use gdb_bfd_ref, gdb_bfd_unref.
(generic_load): Use gdb_bfd_ref.
(reread_symbols): Use gdb_bfd_unref.
* symfile-mem.c: Include gdb_bfd.h.
(symbol_file_add_from_memory): Use make_cleanup_bfd_close.
* spu-linux-nat.c (spu_bfd_open): Use gdb_bfd_ref, gdb_bfd_unref.
* solib.c: Include gdb_bfd.h.
(solib_bfd_fopen): Use gdb_bfd_ref.
(solib_bfd_open): Use gdb_bfd_unref.
(free_so_symbols): Use gdb_bfd_unref.
(reload_shared_libraries_1): Use gdb_bfd_unref.
* solib-spu.c: Include gdb_bfd.h.
(spu_bfd_fopen): Use gdb_bfd_ref, gdb_bfd_unref.
* solib-pa64.c (pa64_solib_create_inferior_hook): Use gdb_bfd_ref,
gdb_bfd_unref.
* solib-frv.c: Include gdb_bfd.h.
(enable_break2): Use gdb_bfd_unref.
* solib-dsbt.c: Include gdb_bfd.h.
(enable_break2): Use gdb_bfd_unref.
* solib-darwin.c: Include gdb_bfd.h.
(darwin_solib_get_all_image_info_addr_at_init): Use gdb_bfd_ref,
gdb_bfd_unref.
(darwin_bfd_open): Use gdb_bfd_unref.
* rs6000-nat.c (add_vmap): Use gdb_bfd_ref, gdb_bfd_unref.
* remote-mips.c: Include gdb_bfd.h.
(mips_load_srec): Use gdb_bfd_ref.
(pmon_load_fast): Use gdb_bfd_ref.
* remote-m32r-sdi.c: Include gdb_bfd.h.
(m32r_load): Use gdb_bfd_ref.
* record.c: Include gdb_bfd.h.
(record_save_cleanups): Use gdb_bfd_unref.
(cmd_record_save): Use gdb_bfd_unref.
* procfs.c (insert_dbx_link_bpt_in_file): Use gdb_bfd_ref,
gdb_bfd_unref.
* objfiles.h (gdb_bfd_close_or_warn): Remove.
(gdb_bfd_ref, gdb_bfd_unref): Move to gdb_bfd.h.
* objfiles.c: Include gdb_bfd.h.
(free_objfile): Use gdb_bfd_unref.
(gdb_bfd_close_or_warn, gdb_bfd_ref, gdb_bfd_unref): Move to
gdb_bfd.c.
* machoread.c (macho_add_oso_symfile): Use gdb_bfd_unref.
(macho_symfile_read_all_oso): Use gdb_bfd_ref, gdb_bfd_unref.
(macho_check_dsym): Likewise.
* m32r-rom.c: Include gdb_bfd.h.
(m32r_load): Use gdb_bfd_ref.
(m32r_upload_command): Use gdb_bfd_ref.
* jit.c: Include gdb_bfd.h.
(jit_bfd_try_read_symtab): Use gdb_bfd_ref, gdb_bfd_unref.
* gdb_bfd.h: New file.
* gdb_bfd.c: New file.
* gcore.c: Include gdb_bfd.h.
(create_gcore_bfd): Use gdb_bfd_ref.
(do_bfd_delete_cleanup): Use gdb_bfd_unref.
(gcore_command): Use gdb_bfd_unref.
* exec.c: Include gdb_bfd.h.
(exec_close): Use gdb_bfd_unref.
(exec_close_1): Use gdb_bfd_unref.
(exec_file_attach): Use gdb_bfd_ref.
* elfread.c: Include gdb_bfd.h.
(build_id_verify): Use gdb_bfd_unref.
* dsrec.c: Include gdb_bfd.h.
(load_srec): Use gdb_bfd_ref.
* corelow.c: Include gdb_bfd.h.
(core_close): Use gdb_bfd_unref.
(core_open): Use gdb_bfd_ref.
* bfd-target.c: Include gdb_bfd.h.
(target_bfd_xclose): Use gdb_bfd_unref.
(target_bfd_reopen): Use gdb_bfd_ref.
* Makefile.in (SFILES): Add gdb_bfd.c.
(HFILES_NO_SRCDIR): Add gdb_bfd.h.
(COMMON_OBS): Add gdb_bfd.o.
+2012-07-18 Tom Tromey <tromey@redhat.com>
+
+ * dwarf2read.c (try_open_dwo_file): Use gdb_bfd_ref and
+ gdb_bfd_unref.
+ (free_dwo_file): Use gdb_bfd_unref.
+ * cli/cli-dump.c: Include gdb_bfd.h.
+ (bfd_openw_with_cleanup): Use gdb_bfd_ref.
+ (bfd_openr_with_cleanup): Likewise.
+ * windows-nat.c (windows_make_so): Use gdb_bfd_ref,
+ gdb_bfd_unref.
+ * utils.c: Include gdb_bfd.h.
+ (do_bfd_close_cleanup): Use gdb_bfd_unref.
+ * symfile.c: Include gdb_bfd.h.
+ (separate_debug_file_exists): Use gdb_bfd_unref.
+ (bfd_open_maybe_remote): Use gdb_bfd_ref.
+ (symfile_bfd_open): Use gdb_bfd_ref, gdb_bfd_unref.
+ (generic_load): Use gdb_bfd_ref.
+ (reread_symbols): Use gdb_bfd_unref.
+ * symfile-mem.c: Include gdb_bfd.h.
+ (symbol_file_add_from_memory): Use make_cleanup_bfd_close.
+ * spu-linux-nat.c (spu_bfd_open): Use gdb_bfd_ref, gdb_bfd_unref.
+ * solib.c: Include gdb_bfd.h.
+ (solib_bfd_fopen): Use gdb_bfd_ref.
+ (solib_bfd_open): Use gdb_bfd_unref.
+ (free_so_symbols): Use gdb_bfd_unref.
+ (reload_shared_libraries_1): Use gdb_bfd_unref.
+ * solib-spu.c: Include gdb_bfd.h.
+ (spu_bfd_fopen): Use gdb_bfd_ref, gdb_bfd_unref.
+ * solib-pa64.c (pa64_solib_create_inferior_hook): Use gdb_bfd_ref,
+ gdb_bfd_unref.
+ * solib-frv.c: Include gdb_bfd.h.
+ (enable_break2): Use gdb_bfd_unref.
+ * solib-dsbt.c: Include gdb_bfd.h.
+ (enable_break2): Use gdb_bfd_unref.
+ * solib-darwin.c: Include gdb_bfd.h.
+ (darwin_solib_get_all_image_info_addr_at_init): Use gdb_bfd_ref,
+ gdb_bfd_unref.
+ (darwin_bfd_open): Use gdb_bfd_unref.
+ * rs6000-nat.c (add_vmap): Use gdb_bfd_ref, gdb_bfd_unref.
+ * remote-mips.c: Include gdb_bfd.h.
+ (mips_load_srec): Use gdb_bfd_ref.
+ (pmon_load_fast): Use gdb_bfd_ref.
+ * remote-m32r-sdi.c: Include gdb_bfd.h.
+ (m32r_load): Use gdb_bfd_ref.
+ * record.c: Include gdb_bfd.h.
+ (record_save_cleanups): Use gdb_bfd_unref.
+ (cmd_record_save): Use gdb_bfd_unref.
+ * procfs.c (insert_dbx_link_bpt_in_file): Use gdb_bfd_ref,
+ gdb_bfd_unref.
+ * objfiles.h (gdb_bfd_close_or_warn): Remove.
+ (gdb_bfd_ref, gdb_bfd_unref): Move to gdb_bfd.h.
+ * objfiles.c: Include gdb_bfd.h.
+ (free_objfile): Use gdb_bfd_unref.
+ (gdb_bfd_close_or_warn, gdb_bfd_ref, gdb_bfd_unref): Move to
+ gdb_bfd.c.
+ * machoread.c (macho_add_oso_symfile): Use gdb_bfd_unref.
+ (macho_symfile_read_all_oso): Use gdb_bfd_ref, gdb_bfd_unref.
+ (macho_check_dsym): Likewise.
+ * m32r-rom.c: Include gdb_bfd.h.
+ (m32r_load): Use gdb_bfd_ref.
+ (m32r_upload_command): Use gdb_bfd_ref.
+ * jit.c: Include gdb_bfd.h.
+ (jit_bfd_try_read_symtab): Use gdb_bfd_ref, gdb_bfd_unref.
+ * gdb_bfd.h: New file.
+ * gdb_bfd.c: New file.
+ * gcore.c: Include gdb_bfd.h.
+ (create_gcore_bfd): Use gdb_bfd_ref.
+ (do_bfd_delete_cleanup): Use gdb_bfd_unref.
+ (gcore_command): Use gdb_bfd_unref.
+ * exec.c: Include gdb_bfd.h.
+ (exec_close): Use gdb_bfd_unref.
+ (exec_close_1): Use gdb_bfd_unref.
+ (exec_file_attach): Use gdb_bfd_ref.
+ * elfread.c: Include gdb_bfd.h.
+ (build_id_verify): Use gdb_bfd_unref.
+ * dsrec.c: Include gdb_bfd.h.
+ (load_srec): Use gdb_bfd_ref.
+ * corelow.c: Include gdb_bfd.h.
+ (core_close): Use gdb_bfd_unref.
+ (core_open): Use gdb_bfd_ref.
+ * bfd-target.c: Include gdb_bfd.h.
+ (target_bfd_xclose): Use gdb_bfd_unref.
+ (target_bfd_reopen): Use gdb_bfd_ref.
+ * Makefile.in (SFILES): Add gdb_bfd.c.
+ (HFILES_NO_SRCDIR): Add gdb_bfd.h.
+ (COMMON_OBS): Add gdb_bfd.o.
+
2012-07-18 Keith Seitz <keiths@redhat.com>
* breakpoint.c (find_condition_and_thread): Initialize
exceptions.c expprint.c \
f-exp.y f-lang.c f-typeprint.c f-valprint.c filesystem.c \
findcmd.c findvar.c frame.c frame-base.c frame-unwind.c \
- gdbarch.c arch-utils.c gdbtypes.c gnu-v2-abi.c gnu-v3-abi.c \
+ gdbarch.c arch-utils.c gdb_bfd.c gdbtypes.c gnu-v2-abi.c gnu-v3-abi.c \
go-exp.y go-lang.c go-typeprint.c go-valprint.c \
inf-loop.c \
infcall.c \
gnulib/import/stddef.in.h gnulib/import/inttypes.in.h inline-frame.h skip.h \
common/common-utils.h common/xml-utils.h common/buffer.h common/ptid.h \
common/format.h \
-common/linux-osdata.h gdb-dlfcn.h auto-load.h probe.h stap-probe.h
+common/linux-osdata.h gdb-dlfcn.h auto-load.h probe.h stap-probe.h gdb_bfd.h
# Header files that already have srcdir in them, or which are in objdir.
macrotab.o macrocmd.o macroexp.o macroscope.o \
mi-common.o \
event-loop.o event-top.o inf-loop.o completer.o \
- gdbarch.o arch-utils.o gdbtypes.o osabi.o copying.o \
+ gdbarch.o arch-utils.o gdbtypes.o gdb_bfd.o osabi.o copying.o \
memattr.o mem-break.o target.o parse.o language.o buildsym.o \
findcmd.o \
std-regs.o \
#include "target.h"
#include "bfd-target.h"
#include "exec.h"
+#include "gdb_bfd.h"
/* The object that is stored in the target_ops->to_data field has this
type. */
{
struct target_bfd_data *data = t->to_data;
- bfd_close (data->bfd);
+ gdb_bfd_unref (data->bfd);
xfree (data->table.sections);
xfree (data);
xfree (t);
struct target_bfd_data *data;
data = XZALLOC (struct target_bfd_data);
- data->bfd = abfd;
+ data->bfd = gdb_bfd_ref (abfd);
build_section_table (abfd, &data->table.sections, &data->table.sections_end);
t = XZALLOC (struct target_ops);
#include "readline/readline.h"
#include "gdbcore.h"
#include "cli/cli-utils.h"
+#include "gdb_bfd.h"
#define XMALLOC(TYPE) ((TYPE*) xmalloc (sizeof (TYPE)))
{
bfd *ibfd;
- ibfd = bfd_openr (filename, target);
+ ibfd = gdb_bfd_ref (bfd_openr (filename, target));
if (ibfd == NULL)
error (_("Failed to open %s: %s."), filename,
bfd_errmsg (bfd_get_error ()));
if (*mode == 'w') /* Write: create new file */
{
- obfd = bfd_openw (filename, target);
+ obfd = gdb_bfd_ref (bfd_openw (filename, target));
if (obfd == NULL)
error (_("Failed to open %s: %s."), filename,
bfd_errmsg (bfd_get_error ()));
#include "filenames.h"
#include "progspace.h"
#include "objfiles.h"
+#include "gdb_bfd.h"
#ifndef O_LARGEFILE
#define O_LARGEFILE 0
}
name = bfd_get_filename (core_bfd);
- gdb_bfd_close_or_warn (core_bfd);
+ gdb_bfd_unref (core_bfd);
xfree (name);
core_bfd = NULL;
}
if (scratch_chan < 0)
perror_with_name (filename);
- temp_bfd = bfd_fopen (filename, gnutarget,
- write_files ? FOPEN_RUB : FOPEN_RB,
- scratch_chan);
+ temp_bfd = gdb_bfd_ref (bfd_fopen (filename, gnutarget,
+ write_files ? FOPEN_RUB : FOPEN_RB,
+ scratch_chan));
if (temp_bfd == NULL)
perror_with_name (filename);
#include <time.h>
#include "gdb_assert.h"
#include "gdb_string.h"
+#include "gdb_bfd.h"
extern void report_transfer_performance (unsigned long, time_t, time_t);
srec = (char *) alloca (maxrecsize + 1);
- abfd = bfd_openr (file, 0);
+ abfd = gdb_bfd_ref (bfd_openr (file, 0));
if (!abfd)
{
printf_filtered (_("Unable to open file %s\n"), file);
#include "gdbcore.h" /* for gnutarget */
#include "gdb/gdb-index.h"
#include <ctype.h>
+#include "gdb_bfd.h"
#include <fcntl.h>
#include "gdb_string.h"
if (desc < 0)
return NULL;
- sym_bfd = bfd_fopen (absolute_name, gnutarget, FOPEN_RB, desc);
+ sym_bfd = gdb_bfd_ref (bfd_fopen (absolute_name, gnutarget, FOPEN_RB, desc));
if (!sym_bfd)
{
xfree (absolute_name);
if (!bfd_check_format (sym_bfd, bfd_object))
{
- bfd_close (sym_bfd); /* This also closes desc. */
xfree (absolute_name);
+ gdb_bfd_unref (sym_bfd); /* This also closes desc. */
return NULL;
}
- /* bfd_usrdata exists for applications and libbfd must not touch it. */
- gdb_assert (bfd_usrdata (sym_bfd) == NULL);
-
return sym_bfd;
}
struct dwarf2_section_info *section;
gdb_assert (dwo_file->dwo_bfd != objfile->obfd);
- bfd_close (dwo_file->dwo_bfd);
+ gdb_bfd_unref (dwo_file->dwo_bfd);
munmap_section_buffer (&dwo_file->sections.abbrev);
munmap_section_buffer (&dwo_file->sections.info);
#include "gdbthread.h"
#include "regcache.h"
#include "bcache.h"
+#include "gdb_bfd.h"
extern void _initialize_elfread (void);
else
retval = 1;
- gdb_bfd_close_or_warn (abfd);
+ gdb_bfd_unref (abfd);
xfree (found);
#include "arch-utils.h"
#include "gdbthread.h"
#include "progspace.h"
+#include "gdb_bfd.h"
#include <fcntl.h>
#include "readline/readline.h"
bfd *abfd = exec_bfd;
char *name = bfd_get_filename (abfd);
- gdb_bfd_close_or_warn (abfd);
+ gdb_bfd_unref (abfd);
xfree (name);
/* Removing target sections may close the exec_ops target.
need_symtab_cleanup = 1;
}
else if (vp->bfd != exec_bfd)
- /* FIXME-leak: We should be freeing vp->name too, I think. */
- gdb_bfd_close_or_warn (vp->bfd);
+ gdb_bfd_unref (vp->bfd);
xfree (vp);
}
#endif
if (scratch_chan < 0)
perror_with_name (filename);
- exec_bfd = bfd_fopen (scratch_pathname, gnutarget,
- write_files ? FOPEN_RUB : FOPEN_RB,
- scratch_chan);
+ exec_bfd = gdb_bfd_ref (bfd_fopen (scratch_pathname, gnutarget,
+ write_files ? FOPEN_RUB : FOPEN_RB,
+ scratch_chan));
if (!exec_bfd)
{
#include <fcntl.h>
#include "regcache.h"
#include "regset.h"
+#include "gdb_bfd.h"
/* The largest amount of memory to read from the target at once. We
must throttle it to limit the amount of memory used by GDB during
bfd *
create_gcore_bfd (char *filename)
{
- bfd *obfd = bfd_openw (filename, default_gcore_target ());
+ bfd *obfd = gdb_bfd_ref (bfd_openw (filename, default_gcore_target ()));
if (!obfd)
error (_("Failed to open '%s' for output."), filename);
bfd *obfd = arg;
const char *filename = obfd->filename;
- bfd_close (arg);
+ gdb_bfd_unref (arg);
unlink (filename);
}
fprintf_filtered (gdb_stdout, "Saved corefile %s\n", corefilename);
discard_cleanups (old_chain);
- bfd_close (obfd);
+ gdb_bfd_unref (obfd);
}
static unsigned long
--- /dev/null
+/* Definitions for BFD wrappers used by GDB.
+
+ Copyright (C) 2011
+ 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"
+
+/* 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)
+{
+ int *p_refcount;
+
+ if (abfd == NULL)
+ return NULL;
+
+ p_refcount = bfd_usrdata (abfd);
+
+ if (p_refcount != NULL)
+ {
+ *p_refcount += 1;
+ return abfd;
+ }
+
+ p_refcount = xmalloc (sizeof (*p_refcount));
+ *p_refcount = 1;
+ bfd_usrdata (abfd) = p_refcount;
+
+ return abfd;
+}
+
+/* Unreference and possibly close ABFD. */
+
+void
+gdb_bfd_unref (struct bfd *abfd)
+{
+ int *p_refcount;
+ char *name;
+
+ if (abfd == NULL)
+ return;
+
+ p_refcount = bfd_usrdata (abfd);
+ gdb_assert (*p_refcount >= 1);
+
+ *p_refcount -= 1;
+ if (*p_refcount > 0)
+ return;
+
+ xfree (p_refcount);
+ bfd_usrdata (abfd) = NULL; /* Paranoia. */
+
+ name = bfd_get_filename (abfd);
+ gdb_bfd_close_or_warn (abfd);
+}
--- /dev/null
+/* Definitions for BFD wrappers used by GDB.
+
+ Copyright (C) 2011
+ 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/>. */
+
+#ifndef GDB_BFD_H
+#define GDB_BFD_H
+
+/* Acquire a new reference to ABFD. Returns ABFD for convenience.
+ It is fine for ABFD to be NULL; in this case the function does
+ nothing and returns NULL. */
+
+struct bfd *gdb_bfd_ref (struct bfd *abfd);
+
+/* Release a reference to ABFD. If this is the last reference, ABFD
+ will be freed. If ABFD is NULL, this function does nothing. */
+
+void gdb_bfd_unref (struct bfd *abfd);
+
+#endif /* GDB_BFD_H */
#include "gdb-dlfcn.h"
#include "gdb_stat.h"
#include "exceptions.h"
+#include "gdb_bfd.h"
static const char *jit_reader_dir = NULL;
puts_unfiltered (_("Error opening JITed symbol file, ignoring it.\n"));
return;
}
+ nbfd = gdb_bfd_ref (nbfd);
/* Check the format. NOTE: This initializes important data that GDB uses!
We would segfault later without this line. */
{
printf_unfiltered (_("\
JITed symbol file is not an object file, ignoring it.\n"));
- bfd_close (nbfd);
+ gdb_bfd_unref (nbfd);
return;
}
#include "inferior.h"
#include <ctype.h>
#include "regcache.h"
+#include "gdb_bfd.h"
/*
* All this stuff just to get my host computer's IP address!
if (filename == NULL || filename[0] == 0)
filename = get_exec_file (1);
- abfd = bfd_openr (filename, 0);
+ abfd = gdb_bfd_ref (bfd_openr (filename, 0));
if (!abfd)
error (_("Unable to open file %s."), filename);
if (bfd_check_format (abfd, bfd_object) == 0)
printf_filtered (" -- Ethernet load complete.\n");
gettimeofday (&end_time, NULL);
- abfd = bfd_openr (args, 0);
+ abfd = gdb_bfd_ref (bfd_openr (args, 0));
if (abfd != NULL)
{ /* Download is done -- print section statistics. */
if (bfd_check_format (abfd, bfd_object) == 0)
{
warning (_("`%s': can't read symbols: %s."), oso->name,
bfd_errmsg (bfd_get_error ()));
- bfd_close (abfd);
+ gdb_bfd_unref (abfd);
return;
}
if (abfd->my_archive == NULL && oso->mtime != bfd_get_mtime (abfd))
{
warning (_("`%s': file time stamp mismatch."), oso->name);
- bfd_close (abfd);
+ gdb_bfd_unref (abfd);
return;
}
oso->nbr_syms))
{
warning (_("`%s': can't create hash table"), oso->name);
- bfd_close (abfd);
+ gdb_bfd_unref (abfd);
return;
}
}
/* Open the archive and check the format. */
- archive_bfd = bfd_openr (archive_name, gnutarget);
+ archive_bfd = gdb_bfd_ref (bfd_openr (archive_name, gnutarget));
if (archive_bfd == NULL)
{
warning (_("Could not open OSO archive file \"%s\""),
{
warning (_("OSO archive file \"%s\" not an archive."),
archive_name);
- bfd_close (archive_bfd);
+ gdb_bfd_unref (archive_bfd);
ix = last_ix;
continue;
}
- member_bfd = bfd_openr_next_archived_file (archive_bfd, NULL);
+ member_bfd = gdb_bfd_ref (bfd_openr_next_archived_file (archive_bfd,
+ NULL));
if (member_bfd == NULL)
{
warning (_("Could not read archive members out of "
"OSO archive \"%s\""), archive_name);
- bfd_close (archive_bfd);
+ gdb_bfd_unref (archive_bfd);
ix = last_ix;
continue;
}
}
prev = member_bfd;
- member_bfd = bfd_openr_next_archived_file
- (archive_bfd, member_bfd);
+ member_bfd
+ = gdb_bfd_ref (bfd_openr_next_archived_file (archive_bfd,
+ member_bfd));
/* Free previous member if not referenced by an oso. */
if (ix2 >= last_ix)
- bfd_close (prev);
+ gdb_bfd_unref (prev);
}
for (ix2 = ix; ix2 < last_ix; ix2++)
{
{
bfd *abfd;
- abfd = bfd_openr (oso->name, gnutarget);
+ abfd = gdb_bfd_ref (bfd_openr (oso->name, gnutarget));
if (!abfd)
warning (_("`%s': can't open to read symbols: %s."), oso->name,
bfd_errmsg (bfd_get_error ()));
return NULL;
}
dsym_filename = xstrdup (dsym_filename);
- dsym_bfd = bfd_openr (dsym_filename, gnutarget);
+ dsym_bfd = gdb_bfd_ref (bfd_openr (dsym_filename, gnutarget));
if (dsym_bfd == NULL)
{
warning (_("can't open dsym file %s"), dsym_filename);
if (!bfd_check_format (dsym_bfd, bfd_object))
{
- bfd_close (dsym_bfd);
+ gdb_bfd_unref (dsym_bfd);
warning (_("bad dsym file format: %s"), bfd_errmsg (bfd_get_error ()));
xfree (dsym_filename);
return NULL;
BFD_MACH_O_LC_UUID, &dsym_uuid) == 0)
{
warning (_("can't find UUID in %s"), dsym_filename);
- bfd_close (dsym_bfd);
+ gdb_bfd_unref (dsym_bfd);
xfree (dsym_filename);
return NULL;
}
sizeof (main_uuid->command.uuid.uuid)))
{
warning (_("dsym file UUID doesn't match the one in %s"), objfile->name);
- bfd_close (dsym_bfd);
+ gdb_bfd_unref (dsym_bfd);
xfree (dsym_filename);
return NULL;
}
#include "complaints.h"
#include "psymtab.h"
#include "solist.h"
+#include "gdb_bfd.h"
/* Prototypes for local functions */
/* Update the per-objfile information that comes from the bfd, ensuring
that any data that is reference is saved in the per-objfile data
- region. */
+ region. Note that we steal a reference to ABFD. */
- objfile->obfd = gdb_bfd_ref (abfd);
+ objfile->obfd = abfd;
if (abfd != NULL)
{
/* Look up the gdbarch associated with the BFD. */
get_objfile_pspace_data (current_program_space)->objfiles_changed_p = 1;
}
-/* Close ABFD, and warn if that fails. */
-
-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)
-{
- int *p_refcount;
-
- if (abfd == NULL)
- return NULL;
-
- p_refcount = bfd_usrdata (abfd);
-
- if (p_refcount != NULL)
- {
- *p_refcount += 1;
- return abfd;
- }
-
- p_refcount = xmalloc (sizeof (*p_refcount));
- *p_refcount = 1;
- bfd_usrdata (abfd) = p_refcount;
-
- return abfd;
-}
-
-/* Unreference and possibly close ABFD. */
-void
-gdb_bfd_unref (struct bfd *abfd)
-{
- int *p_refcount;
- char *name;
-
- if (abfd == NULL)
- return;
-
- p_refcount = bfd_usrdata (abfd);
-
- /* Valid range for p_refcount: a pointer to int counter, which has a
- value of 1 (single owner) or 2 (shared). */
- gdb_assert (*p_refcount == 1 || *p_refcount == 2);
-
- *p_refcount -= 1;
- if (*p_refcount > 0)
- return;
-
- xfree (p_refcount);
- bfd_usrdata (abfd) = NULL; /* Paranoia. */
-
- name = bfd_get_filename (abfd);
- gdb_bfd_close_or_warn (abfd);
- xfree (name);
-}
-
/* The default implementation for the "iterate_over_objfiles_in_search_order"
gdbarch method. It is equivalent to use the ALL_OBJFILES macro,
searching the objfiles in the order they are stored internally,
extern void *objfile_data (struct objfile *objfile,
const struct objfile_data *data);
-extern struct bfd *gdb_bfd_ref (struct bfd *abfd);
-extern void gdb_bfd_unref (struct bfd *abfd);
-extern int gdb_bfd_close_or_warn (struct bfd *abfd);
-
extern void default_iterate_over_objfiles_in_search_order
(struct gdbarch *gdbarch,
iterate_over_objfiles_in_search_order_cb_ftype *cb,
long storage_needed;
CORE_ADDR sym_addr;
- abfd = bfd_fdopenr ("unamed", 0, fd);
+ abfd = gdb_bfd_ref (bfd_fdopenr ("unamed", 0, fd));
if (abfd == NULL)
{
warning (_("Failed to create a bfd: %s."), bfd_errmsg (bfd_get_error ()));
{
/* Not the correct format, so we can not possibly find the dbx_link
symbol in it. */
- bfd_close (abfd);
+ gdb_bfd_unref (abfd);
return 0;
}
if (dbx_link_bpt == NULL)
{
warning (_("Failed to insert dbx_link breakpoint."));
- bfd_close (abfd);
+ gdb_bfd_unref (abfd);
return 0;
}
- bfd_close (abfd);
+ gdb_bfd_unref (abfd);
return 1;
}
- bfd_close (abfd);
+ gdb_bfd_unref (abfd);
return 0;
}
#include "gcore.h"
#include "event-loop.h"
#include "inf-loop.h"
+#include "gdb_bfd.h"
#include <signal.h>
bfd *obfd = data;
char *pathname = xstrdup (bfd_get_filename (obfd));
- bfd_close (obfd);
+ gdb_bfd_unref (obfd);
unlink (pathname);
xfree (pathname);
}
}
do_cleanups (set_cleanups);
- bfd_close (obfd);
+ gdb_bfd_unref (obfd);
discard_cleanups (old_cleanups);
/* Succeeded. */
#include <sys/time.h>
#include <signal.h>
#include <time.h>
+#include "gdb_bfd.h"
#include "serial.h"
if (!filename)
filename = get_exec_file (1);
- pbfd = bfd_openr (filename, gnutarget);
+ pbfd = gdb_bfd_ref (bfd_openr (filename, gnutarget));
if (pbfd == NULL)
{
perror_with_name (filename);
#include <ctype.h>
#include "mips-tdep.h"
#include "gdbthread.h"
+#include "gdb_bfd.h"
\f
/* Breakpoint types. Values 0, 1, and 2 must agree with the watch
buffer = alloca (srec_frame * 2 + 256);
- abfd = bfd_openr (args, 0);
+ abfd = gdb_bfd_ref (bfd_openr (args, 0));
if (!abfd)
{
printf_filtered ("Unable to open file %s\n", args);
buffer = (char *) xmalloc (MAXRECSIZE + 1);
binbuf = (unsigned char *) xmalloc (BINCHUNK);
- abfd = bfd_openr (file, 0);
+ abfd = gdb_bfd_ref (bfd_openr (file, 0));
if (!abfd)
{
printf_filtered ("Unable to open file %s\n", file);
abfd = bfd_openr (objname, gnutarget);
else
abfd = bfd_fdopenr (objname, gnutarget, fd);
+ abfd = gdb_bfd_ref (abfd);
if (!abfd)
{
warning (_("Could not open `%s' as an executable file: %s"),
{
last = 0;
/* FIXME??? am I tossing BFDs? bfd? */
- while ((last = bfd_openr_next_archived_file (abfd, last)))
+ while ((last = gdb_bfd_ref (bfd_openr_next_archived_file (abfd, last))))
if (strcmp (mem, last->filename) == 0)
break;
if (!last)
{
warning (_("\"%s\": member \"%s\" missing."), objname, mem);
- bfd_close (abfd);
+ gdb_bfd_unref (abfd);
return NULL;
}
{
warning (_("\"%s\": member \"%s\" not in executable format: %s."),
objname, mem, bfd_errmsg (bfd_get_error ()));
- bfd_close (last);
- bfd_close (abfd);
+ gdb_bfd_unref (last);
+ gdb_bfd_unref (abfd);
return NULL;
}
{
warning (_("\"%s\": not in executable format: %s."),
objname, bfd_errmsg (bfd_get_error ()));
- bfd_close (abfd);
+ gdb_bfd_unref (abfd);
return NULL;
}
- obj = allocate_objfile (vp->bfd, 0);
+ obj = allocate_objfile (gdb_bfd_ref (vp->bfd), 0);
vp->objfile = obj;
/* Always add symbols for the main objfile. */
#include "inferior.h"
#include "regcache.h"
#include "gdbthread.h"
+#include "gdb_bfd.h"
#include "gdb_assert.h"
return;
/* Create a bfd for the interpreter. */
- dyld_bfd = bfd_openr (interp_name, gnutarget);
+ dyld_bfd = gdb_bfd_ref (bfd_openr (interp_name, gnutarget));
if (dyld_bfd)
{
bfd *sub;
dyld_bfd = sub;
else
{
- bfd_close (dyld_bfd);
+ gdb_bfd_unref (dyld_bfd);
dyld_bfd = NULL;
}
}
info->all_image_addr =
lookup_symbol_from_bfd (dyld_bfd, "_dyld_all_image_infos");
- bfd_close (dyld_bfd);
+ gdb_bfd_unref (dyld_bfd);
if (info->all_image_addr == 0)
return;
gdbarch_bfd_arch_info (target_gdbarch));
if (!res)
{
- bfd_close (abfd);
+ gdb_bfd_unref (abfd);
make_cleanup (xfree, found_pathname);
error (_("`%s': not a shared-library: %s"),
found_pathname, bfd_errmsg (bfd_get_error ()));
#include "gdbcmd.h"
#include "elf-bfd.h"
#include "exceptions.h"
+#include "gdb_bfd.h"
#define GOT_MODULE_OFFSET 4
{
warning (_("Could not find symbol _dl_debug_addr in dynamic linker"));
enable_break_failure_warning ();
- bfd_close (tmp_bfd);
+ gdb_bfd_unref (tmp_bfd);
return 0;
}
"(at address %s) from dynamic linker"),
hex_string_custom (addr + 8, 8));
enable_break_failure_warning ();
- bfd_close (tmp_bfd);
+ gdb_bfd_unref (tmp_bfd);
return 0;
}
addr = extract_unsigned_integer (addr_buf, sizeof addr_buf, byte_order);
/* We're done with the temporary bfd. */
- bfd_close (tmp_bfd);
+ gdb_bfd_unref (tmp_bfd);
/* We're also done with the loadmap. */
xfree (ldm);
#include "gdbcmd.h"
#include "elf/frv.h"
#include "exceptions.h"
+#include "gdb_bfd.h"
/* Flag which indicates whether internal debug messages should be printed. */
static int solib_frv_debug;
{
warning (_("Unable to determine dynamic linker loadmap address."));
enable_break_failure_warning ();
- bfd_close (tmp_bfd);
+ gdb_bfd_unref (tmp_bfd);
return 0;
}
warning (_("Unable to load dynamic linker loadmap at address %s."),
hex_string_custom (interp_loadmap_addr, 8));
enable_break_failure_warning ();
- bfd_close (tmp_bfd);
+ gdb_bfd_unref (tmp_bfd);
return 0;
}
warning (_("Could not find symbol _dl_debug_addr "
"in dynamic linker"));
enable_break_failure_warning ();
- bfd_close (tmp_bfd);
+ gdb_bfd_unref (tmp_bfd);
return 0;
}
"(at address %s) from dynamic linker"),
hex_string_custom (addr + 8, 8));
enable_break_failure_warning ();
- bfd_close (tmp_bfd);
+ gdb_bfd_unref (tmp_bfd);
return 0;
}
addr = extract_unsigned_integer (addr_buf, sizeof addr_buf, byte_order);
"(at address %s) from dynamic linker"),
hex_string_custom (addr, 8));
enable_break_failure_warning ();
- bfd_close (tmp_bfd);
+ gdb_bfd_unref (tmp_bfd);
return 0;
}
addr = extract_unsigned_integer (addr_buf, sizeof addr_buf, byte_order);
/* We're done with the temporary bfd. */
- bfd_close (tmp_bfd);
+ gdb_bfd_unref (tmp_bfd);
/* We're also done with the loadmap. */
xfree (ldm);
to find any magic formula to find it for Solaris (appears to
be trivial on GNU/Linux). Therefore, we have to try an alternate
mechanism to find the dynamic linker's base address. */
- tmp_bfd = bfd_openr (buf, gnutarget);
+ tmp_bfd = gdb_bfd_ref (bfd_openr (buf, gnutarget));
if (tmp_bfd == NULL)
return;
{
warning (_("Unable to grok dynamic linker %s as an object file"),
buf);
- bfd_close (tmp_bfd);
+ gdb_bfd_unref (tmp_bfd);
return;
}
}
/* We're done with the temporary bfd. */
- bfd_close (tmp_bfd);
+ gdb_bfd_unref (tmp_bfd);
}
}
#include "breakpoint.h"
#include "gdbthread.h"
#include "exceptions.h"
+#include "gdb_bfd.h"
#include "spu-tdep.h"
CORE_ADDR *open_closure = xmalloc (sizeof (CORE_ADDR));
*open_closure = addr;
- nbfd = bfd_openr_iovec (xstrdup (name), "elf32-spu",
- spu_bfd_iovec_open, open_closure,
- spu_bfd_iovec_pread, spu_bfd_iovec_close,
- spu_bfd_iovec_stat);
+ nbfd = gdb_bfd_ref (bfd_openr_iovec (xstrdup (name), "elf32-spu",
+ spu_bfd_iovec_open, open_closure,
+ spu_bfd_iovec_pread, spu_bfd_iovec_close,
+ spu_bfd_iovec_stat));
if (!nbfd)
return NULL;
if (!bfd_check_format (nbfd, bfd_object))
{
- bfd_close (nbfd);
+ gdb_bfd_unref (nbfd);
return NULL;
}
#include "solib.h"
#include "interps.h"
#include "filesystem.h"
+#include "gdb_bfd.h"
/* Architecture-specific operations. */
pathname, bfd_errmsg (bfd_get_error ()));
}
- return abfd;
+ return gdb_bfd_ref (abfd);
}
/* Find shared library PATHNAME and open a BFD for it. */
/* Check bfd format. */
if (!bfd_check_format (abfd, bfd_object))
{
- bfd_close (abfd);
+ gdb_bfd_unref (abfd);
make_cleanup (xfree, found_pathname);
error (_("`%s': not in executable format: %s"),
found_pathname, bfd_errmsg (bfd_get_error ()));
return 0;
/* Leave bfd open, core_xfer_memory and "info files" need it. */
- so->abfd = gdb_bfd_ref (abfd);
+ so->abfd = abfd;
/* copy full path name into so_name, so that later symbol_file_add
can find it. */
sap = build_section_addr_info_from_section_table (so->sections,
so->sections_end);
- so->objfile = symbol_file_add_from_bfd (so->abfd,
+ so->objfile = symbol_file_add_from_bfd (gdb_bfd_ref (so->abfd),
flags, sap, OBJF_SHARED,
NULL);
so->objfile->addr_low = so->addr_low;
{
found_pathname = xstrdup (bfd_get_filename (abfd));
make_cleanup (xfree, found_pathname);
- gdb_bfd_close_or_warn (abfd);
+ gdb_bfd_unref (abfd);
}
/* If this shared library is no longer associated with its previous
spu_bfd_iovec_stat);
if (!nbfd)
return NULL;
+ nbfd = gdb_bfd_ref (nbfd);
if (!bfd_check_format (nbfd, bfd_object))
{
- bfd_close (nbfd);
+ gdb_bfd_unref (nbfd);
return NULL;
}
#include "observer.h"
#include "auxv.h"
#include "elf/common.h"
+#include "gdb_bfd.h"
/* Verify parameters of target_read_memory_bfd and target_read_memory are
compatible. */
if (nbfd == NULL)
error (_("Failed to read a valid object file image from memory."));
+ gdb_bfd_ref (nbfd);
if (name == NULL)
nbfd->filename = xstrdup ("shared object read from target memory");
else
if (!bfd_check_format (nbfd, bfd_object))
{
- /* FIXME: should be checking for errors from bfd_close (for one thing,
- on error it does not free all the storage associated with the
- bfd). */
- bfd_close (nbfd);
+ make_cleanup_bfd_close (nbfd);
error (_("Got object file from memory but can't read symbols: %s."),
bfd_errmsg (bfd_get_error ()));
}
#include "solib.h"
#include "remote.h"
#include "stack.h"
+#include "gdb_bfd.h"
#include <sys/types.h>
#include <fcntl.h>
if (abfd_stat.st_dev == parent_stat.st_dev
&& abfd_stat.st_ino == parent_stat.st_ino)
{
- bfd_close (abfd);
+ gdb_bfd_unref (abfd);
return 0;
}
verified_as_different = 1;
file_crc_p = get_file_crc (abfd, &file_crc);
- bfd_close (abfd);
+ gdb_bfd_unref (abfd);
if (!file_crc_p)
return 0;
}
/* If NAME is a remote name open the file using remote protocol, otherwise
- open it normally. */
+ open it normally. Returns a new reference to the BFD. On error,
+ returns NULL with the BFD error set. */
bfd *
bfd_open_maybe_remote (const char *name)
{
if (remote_filename_p (name))
- return remote_bfd_open (name, gnutarget);
+ return gdb_bfd_ref (remote_bfd_open (name, gnutarget));
else
- return bfd_openr (name, gnutarget);
+ return gdb_bfd_ref (bfd_openr (name, gnutarget));
}
if (remote_filename_p (name))
{
name = xstrdup (name);
- sym_bfd = remote_bfd_open (name, gnutarget);
+ sym_bfd = gdb_bfd_ref (remote_bfd_open (name, gnutarget));
if (!sym_bfd)
{
make_cleanup (xfree, name);
if (!bfd_check_format (sym_bfd, bfd_object))
{
- bfd_close (sym_bfd);
+ gdb_bfd_unref (sym_bfd);
make_cleanup (xfree, name);
error (_("`%s': can't read symbols: %s."), name,
bfd_errmsg (bfd_get_error ()));
xfree (name);
name = absolute_name;
- sym_bfd = bfd_fopen (name, gnutarget, FOPEN_RB, desc);
+ sym_bfd = gdb_bfd_ref (bfd_fopen (name, gnutarget, FOPEN_RB, desc));
if (!sym_bfd)
{
make_cleanup (xfree, name);
if (!bfd_check_format (sym_bfd, bfd_object))
{
- /* FIXME: should be checking for errors from bfd_close (for one
- thing, on error it does not free all the storage associated
- with the bfd). */
- bfd_close (sym_bfd); /* This also closes desc. */
+ make_cleanup_bfd_close (sym_bfd);
make_cleanup (xfree, name);
error (_("`%s': can't read symbols: %s."), name,
bfd_errmsg (bfd_get_error ()));
}
- /* bfd_usrdata exists for applications and libbfd must not touch it. */
- gdb_assert (bfd_usrdata (sym_bfd) == NULL);
-
return sym_bfd;
}
}
/* Open the file for loading. */
- loadfile_bfd = bfd_openr (filename, gnutarget);
+ loadfile_bfd = gdb_bfd_ref (bfd_openr (filename, gnutarget));
if (loadfile_bfd == NULL)
{
perror_with_name (filename);
return;
}
- /* FIXME: should be checking for errors from bfd_close (for one thing,
- on error it does not free all the storage associated with the
- bfd). */
make_cleanup_bfd_close (loadfile_bfd);
if (!bfd_check_format (loadfile_bfd, bfd_object))
to close the descriptor but BFD lacks a way of closing the
BFD without closing the descriptor. */
obfd_filename = bfd_get_filename (objfile->obfd);
- if (!bfd_close (objfile->obfd))
- error (_("Can't close BFD for %s: %s"), objfile->name,
- bfd_errmsg (bfd_get_error ()));
+ gdb_bfd_unref (objfile->obfd);
objfile->obfd = bfd_open_maybe_remote (obfd_filename);
if (objfile->obfd == NULL)
error (_("Can't open %s to read symbols."), objfile->name);
- else
- objfile->obfd = gdb_bfd_ref (objfile->obfd);
/* bfd_openr sets cacheable to true, which is what we want. */
if (!bfd_check_format (objfile->obfd, bfd_object))
error (_("Can't read symbols from %s: %s."), objfile->name,
#include "exceptions.h"
#include "gdbthread.h"
#include "fnmatch.h"
+#include "gdb_bfd.h"
#ifdef HAVE_SYS_RESOURCE_H
#include <sys/resource.h>
#endif /* HAVE_SYS_RESOURCE_H */
static void
do_bfd_close_cleanup (void *arg)
{
- bfd_close (arg);
+ gdb_bfd_unref (arg);
}
struct cleanup *
asection *text = NULL;
CORE_ADDR text_vma;
- abfd = bfd_openr (so->so_name, "pei-i386");
+ abfd = gdb_bfd_ref (bfd_openr (so->so_name, "pei-i386"));
if (!abfd)
return so;
if (!text)
{
- bfd_close (abfd);
+ gdb_bfd_unref (abfd);
return so;
}
load_addr + 0x1000);
cygwin_load_end = cygwin_load_start + bfd_section_size (abfd, text);
- bfd_close (abfd);
+ gdb_bfd_unref (abfd);
}
#endif