gdb/bfd-target.c - gdb

Data types defined

Functions defined

Source code

  1. /* Very simple "bfd" target, for GDB, the GNU debugger.

  2.    Copyright (C) 2003-2015 Free Software Foundation, Inc.

  3.    This file is part of GDB.

  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 3 of the License, or
  7.    (at your option) any later version.

  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.

  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  14. #include "defs.h"
  15. #include "target.h"
  16. #include "bfd-target.h"
  17. #include "exec.h"
  18. #include "gdb_bfd.h"

  19. /* The object that is stored in the target_ops->to_data field has this
  20.    type.  */
  21. struct target_bfd_data
  22. {
  23.   /* The BFD we're wrapping.  */
  24.   struct bfd *bfd;

  25.   /* The section table build from the ALLOC sections in BFD.  Note
  26.      that we can't rely on extracting the BFD from a random section in
  27.      the table, since the table can be legitimately empty.  */
  28.   struct target_section_table table;
  29. };

  30. static enum target_xfer_status
  31. target_bfd_xfer_partial (struct target_ops *ops,
  32.                          enum target_object object,
  33.                          const char *annex, gdb_byte *readbuf,
  34.                          const gdb_byte *writebuf,
  35.                          ULONGEST offset, ULONGEST len,
  36.                          ULONGEST *xfered_len)
  37. {
  38.   switch (object)
  39.     {
  40.     case TARGET_OBJECT_MEMORY:
  41.       {
  42.         struct target_bfd_data *data = ops->to_data;
  43.         return section_table_xfer_memory_partial (readbuf, writebuf,
  44.                                                   offset, len, xfered_len,
  45.                                                   data->table.sections,
  46.                                                   data->table.sections_end,
  47.                                                   NULL);
  48.       }
  49.     default:
  50.       return TARGET_XFER_E_IO;
  51.     }
  52. }

  53. static struct target_section_table *
  54. target_bfd_get_section_table (struct target_ops *ops)
  55. {
  56.   struct target_bfd_data *data = ops->to_data;
  57.   return &data->table;
  58. }

  59. static void
  60. target_bfd_xclose (struct target_ops *t)
  61. {
  62.   struct target_bfd_data *data = t->to_data;

  63.   gdb_bfd_unref (data->bfd);
  64.   xfree (data->table.sections);
  65.   xfree (data);
  66.   xfree (t);
  67. }

  68. struct target_ops *
  69. target_bfd_reopen (struct bfd *abfd)
  70. {
  71.   struct target_ops *t;
  72.   struct target_bfd_data *data;

  73.   data = XCNEW (struct target_bfd_data);
  74.   data->bfd = abfd;
  75.   gdb_bfd_ref (abfd);
  76.   build_section_table (abfd, &data->table.sections, &data->table.sections_end);

  77.   t = XCNEW (struct target_ops);
  78.   t->to_shortname = "bfd";
  79.   t->to_longname = _("BFD backed target");
  80.   t->to_doc = _("You should never see this");
  81.   t->to_get_section_table = target_bfd_get_section_table;
  82.   t->to_xfer_partial = target_bfd_xfer_partial;
  83.   t->to_xclose = target_bfd_xclose;
  84.   t->to_data = data;
  85.   t->to_magic = OPS_MAGIC;

  86.   return t;
  87. }