Fix g++ 9.1 build breakage
[external/binutils.git] / gdb / bfd-target.c
1 /* Very simple "bfd" target, for GDB, the GNU debugger.
2
3    Copyright (C) 2003-2019 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
28 static const target_info target_bfd_target_info = {
29   "bfd",
30   N_("BFD backed target"),
31   N_("You should never see this")
32 };
33
34 class target_bfd : public target_ops
35 {
36 public:
37   explicit target_bfd (struct bfd *bfd);
38   ~target_bfd () override;
39
40   const target_info &info () const override
41   { return target_bfd_target_info; }
42
43   strata stratum () const override { return file_stratum; }
44
45   void close () override;
46
47   target_xfer_status
48     xfer_partial (target_object object,
49                   const char *annex, gdb_byte *readbuf,
50                   const gdb_byte *writebuf,
51                   ULONGEST offset, ULONGEST len,
52                   ULONGEST *xfered_len) override;
53
54   target_section_table *get_section_table () override;
55
56 private:
57   /* The BFD we're wrapping.  */
58   gdb_bfd_ref_ptr m_bfd;
59
60   /* The section table build from the ALLOC sections in BFD.  Note
61      that we can't rely on extracting the BFD from a random section in
62      the table, since the table can be legitimately empty.  */
63   struct target_section_table m_table;
64 };
65
66 target_xfer_status
67 target_bfd::xfer_partial (target_object object,
68                           const char *annex, gdb_byte *readbuf,
69                           const gdb_byte *writebuf,
70                           ULONGEST offset, ULONGEST len,
71                           ULONGEST *xfered_len)
72 {
73   switch (object)
74     {
75     case TARGET_OBJECT_MEMORY:
76       {
77         return section_table_xfer_memory_partial (readbuf, writebuf,
78                                                   offset, len, xfered_len,
79                                                   m_table.sections,
80                                                   m_table.sections_end,
81                                                   NULL);
82       }
83     default:
84       return TARGET_XFER_E_IO;
85     }
86 }
87
88 target_section_table *
89 target_bfd::get_section_table ()
90 {
91   return &m_table;
92 }
93
94 target_bfd::target_bfd (struct bfd *abfd)
95   : m_bfd (gdb_bfd_ref_ptr::new_reference (abfd))
96 {
97   m_table.sections = NULL;
98   m_table.sections_end = NULL;
99   build_section_table (abfd, &m_table.sections, &m_table.sections_end);
100 }
101
102 target_bfd::~target_bfd ()
103 {
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 }