Convert struct target_ops to C++
[external/binutils.git] / gdb / bfd-target.c
1 /* Very simple "bfd" target, for GDB, the GNU debugger.
2
3    Copyright (C) 2003-2018 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 #include "gdb_bfd.h"
25
26 /* A target that wraps a BFD.  */
27 class target_bfd : public target_ops
28 {
29 public:
30   explicit target_bfd (struct bfd *bfd);
31   ~target_bfd () override;
32
33   const char *shortname () override
34   { return "bfd"; }
35
36   const char *longname () override
37   { return _("BFD backed target"); }
38
39   const char *doc () override
40   { return _("You should never see this"); }
41
42   void close () override;
43
44   target_xfer_status
45     xfer_partial (target_object object,
46                   const char *annex, gdb_byte *readbuf,
47                   const gdb_byte *writebuf,
48                   ULONGEST offset, ULONGEST len,
49                   ULONGEST *xfered_len) override;
50
51   target_section_table *get_section_table () override;
52
53 private:
54   /* The BFD we're wrapping.  */
55   struct bfd *m_bfd;
56
57   /* The section table build from the ALLOC sections in BFD.  Note
58      that we can't rely on extracting the BFD from a random section in
59      the table, since the table can be legitimately empty.  */
60   struct target_section_table m_table;
61 };
62
63 target_xfer_status
64 target_bfd::xfer_partial (target_object object,
65                           const char *annex, gdb_byte *readbuf,
66                           const gdb_byte *writebuf,
67                           ULONGEST offset, ULONGEST len,
68                           ULONGEST *xfered_len)
69 {
70   switch (object)
71     {
72     case TARGET_OBJECT_MEMORY:
73       {
74         return section_table_xfer_memory_partial (readbuf, writebuf,
75                                                   offset, len, xfered_len,
76                                                   m_table.sections,
77                                                   m_table.sections_end,
78                                                   NULL);
79       }
80     default:
81       return TARGET_XFER_E_IO;
82     }
83 }
84
85 target_section_table *
86 target_bfd::get_section_table ()
87 {
88   return &m_table;
89 }
90
91 target_bfd::target_bfd (struct bfd *abfd)
92 {
93   this->to_stratum = file_stratum;
94   m_bfd = abfd;
95   gdb_bfd_ref (abfd);
96   m_table.sections = NULL;
97   m_table.sections_end = NULL;
98   build_section_table (abfd, &m_table.sections, &m_table.sections_end);
99 }
100
101 target_bfd::~target_bfd ()
102 {
103   gdb_bfd_unref (m_bfd);
104   xfree (m_table.sections);
105 }
106
107 target_ops *
108 target_bfd_reopen (struct bfd *abfd)
109 {
110   return new target_bfd (abfd);
111 }
112
113 void
114 target_bfd::close ()
115 {
116   delete this;
117 }