* bfd-target.c (target_bfd_xclose): Only close the bfd if the
[external/binutils.git] / gdb / bfd-target.c
1 /* Very simple "bfd" target, for GDB, the GNU debugger.
2
3    Copyright (C) 2003, 2005, 2007, 2008, 2009 Free Software Foundation, Inc.
4
5    This file is part of GDB.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19
20 #include "defs.h"
21 #include "target.h"
22 #include "bfd-target.h"
23 #include "exec.h"
24
25 static LONGEST
26 target_bfd_xfer_partial (struct target_ops *ops,
27                          enum target_object object,
28                          const char *annex, gdb_byte *readbuf,
29                          const gdb_byte *writebuf,
30                          ULONGEST offset, LONGEST len)
31 {
32   switch (object)
33     {
34     case TARGET_OBJECT_MEMORY:
35       {
36         struct target_section_table *table = ops->to_data;
37         return section_table_xfer_memory_partial (readbuf, writebuf, offset, len,
38                                                   table->sections,
39                                                   table->sections_end,
40                                                   NULL);
41       }
42     default:
43       return -1;
44     }
45 }
46
47 static struct target_section_table *
48 target_bfd_get_section_table (struct target_ops *ops)
49 {
50   return ops->to_data;
51 }
52
53 static void
54 target_bfd_xclose (struct target_ops *t, int quitting)
55 {
56   struct target_section_table *table = t->to_data;
57
58   /* If the target sections table is empty, the bfd had already been
59      closed.  */
60   if (table->sections != table->sections_end)
61     bfd_close (table->sections->bfd);
62   xfree (table->sections);
63   xfree (table);
64   xfree (t);
65 }
66
67 struct target_ops *
68 target_bfd_reopen (struct bfd *bfd)
69 {
70   struct target_ops *t;
71   struct target_section_table *table;
72
73   table = XZALLOC (struct target_section_table);
74   build_section_table (bfd, &table->sections, &table->sections_end);
75
76   /* No use keeping the bfd open if there are no target sections we
77      care about.  This way, we avoid keeping the bfd pointer stored
78      somewhere so that target_bfd_xclose could use it.  */
79   if (table->sections == table->sections_end)
80     bfd_close (bfd);
81
82   t = XZALLOC (struct target_ops);
83   t->to_shortname = "bfd";
84   t->to_longname = _("BFD backed target");
85   t->to_doc = _("You should never see this");
86   t->to_get_section_table = target_bfd_get_section_table;
87   t->to_xfer_partial = target_bfd_xfer_partial;
88   t->to_xclose = target_bfd_xclose;
89   t->to_data = table;
90
91   return t;
92 }