15796c8dcSSimon Schubert /* Very simple "bfd" target, for GDB, the GNU debugger.
25796c8dcSSimon Schubert
3*ef5ccd6cSJohn Marino Copyright (C) 2003-2013 Free Software Foundation, Inc.
45796c8dcSSimon Schubert
55796c8dcSSimon Schubert This file is part of GDB.
65796c8dcSSimon Schubert
75796c8dcSSimon Schubert This program is free software; you can redistribute it and/or modify
85796c8dcSSimon Schubert it under the terms of the GNU General Public License as published by
95796c8dcSSimon Schubert the Free Software Foundation; either version 3 of the License, or
105796c8dcSSimon Schubert (at your option) any later version.
115796c8dcSSimon Schubert
125796c8dcSSimon Schubert This program is distributed in the hope that it will be useful,
135796c8dcSSimon Schubert but WITHOUT ANY WARRANTY; without even the implied warranty of
145796c8dcSSimon Schubert MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
155796c8dcSSimon Schubert GNU General Public License for more details.
165796c8dcSSimon Schubert
175796c8dcSSimon Schubert You should have received a copy of the GNU General Public License
185796c8dcSSimon Schubert along with this program. If not, see <http://www.gnu.org/licenses/>. */
195796c8dcSSimon Schubert
205796c8dcSSimon Schubert #include "defs.h"
215796c8dcSSimon Schubert #include "target.h"
225796c8dcSSimon Schubert #include "bfd-target.h"
235796c8dcSSimon Schubert #include "exec.h"
24*ef5ccd6cSJohn Marino #include "gdb_bfd.h"
255796c8dcSSimon Schubert
265796c8dcSSimon Schubert /* The object that is stored in the target_ops->to_data field has this
275796c8dcSSimon Schubert type. */
285796c8dcSSimon Schubert struct target_bfd_data
295796c8dcSSimon Schubert {
305796c8dcSSimon Schubert /* The BFD we're wrapping. */
315796c8dcSSimon Schubert struct bfd *bfd;
325796c8dcSSimon Schubert
335796c8dcSSimon Schubert /* The section table build from the ALLOC sections in BFD. Note
345796c8dcSSimon Schubert that we can't rely on extracting the BFD from a random section in
355796c8dcSSimon Schubert the table, since the table can be legitimately empty. */
365796c8dcSSimon Schubert struct target_section_table table;
375796c8dcSSimon Schubert };
385796c8dcSSimon Schubert
395796c8dcSSimon Schubert static LONGEST
target_bfd_xfer_partial(struct target_ops * ops,enum target_object object,const char * annex,gdb_byte * readbuf,const gdb_byte * writebuf,ULONGEST offset,LONGEST len)405796c8dcSSimon Schubert target_bfd_xfer_partial (struct target_ops *ops,
415796c8dcSSimon Schubert enum target_object object,
425796c8dcSSimon Schubert const char *annex, gdb_byte *readbuf,
435796c8dcSSimon Schubert const gdb_byte *writebuf,
445796c8dcSSimon Schubert ULONGEST offset, LONGEST len)
455796c8dcSSimon Schubert {
465796c8dcSSimon Schubert switch (object)
475796c8dcSSimon Schubert {
485796c8dcSSimon Schubert case TARGET_OBJECT_MEMORY:
495796c8dcSSimon Schubert {
505796c8dcSSimon Schubert struct target_bfd_data *data = ops->to_data;
515796c8dcSSimon Schubert return section_table_xfer_memory_partial (readbuf, writebuf,
525796c8dcSSimon Schubert offset, len,
535796c8dcSSimon Schubert data->table.sections,
545796c8dcSSimon Schubert data->table.sections_end,
555796c8dcSSimon Schubert NULL);
565796c8dcSSimon Schubert }
575796c8dcSSimon Schubert default:
585796c8dcSSimon Schubert return -1;
595796c8dcSSimon Schubert }
605796c8dcSSimon Schubert }
615796c8dcSSimon Schubert
625796c8dcSSimon Schubert static struct target_section_table *
target_bfd_get_section_table(struct target_ops * ops)635796c8dcSSimon Schubert target_bfd_get_section_table (struct target_ops *ops)
645796c8dcSSimon Schubert {
655796c8dcSSimon Schubert struct target_bfd_data *data = ops->to_data;
665796c8dcSSimon Schubert return &data->table;
675796c8dcSSimon Schubert }
685796c8dcSSimon Schubert
695796c8dcSSimon Schubert static void
target_bfd_xclose(struct target_ops * t,int quitting)705796c8dcSSimon Schubert target_bfd_xclose (struct target_ops *t, int quitting)
715796c8dcSSimon Schubert {
725796c8dcSSimon Schubert struct target_bfd_data *data = t->to_data;
735796c8dcSSimon Schubert
74*ef5ccd6cSJohn Marino gdb_bfd_unref (data->bfd);
755796c8dcSSimon Schubert xfree (data->table.sections);
765796c8dcSSimon Schubert xfree (data);
775796c8dcSSimon Schubert xfree (t);
785796c8dcSSimon Schubert }
795796c8dcSSimon Schubert
805796c8dcSSimon Schubert struct target_ops *
target_bfd_reopen(struct bfd * abfd)81a45ae5f8SJohn Marino target_bfd_reopen (struct bfd *abfd)
825796c8dcSSimon Schubert {
835796c8dcSSimon Schubert struct target_ops *t;
845796c8dcSSimon Schubert struct target_bfd_data *data;
855796c8dcSSimon Schubert
865796c8dcSSimon Schubert data = XZALLOC (struct target_bfd_data);
87a45ae5f8SJohn Marino data->bfd = abfd;
88*ef5ccd6cSJohn Marino gdb_bfd_ref (abfd);
89a45ae5f8SJohn Marino build_section_table (abfd, &data->table.sections, &data->table.sections_end);
905796c8dcSSimon Schubert
915796c8dcSSimon Schubert t = XZALLOC (struct target_ops);
925796c8dcSSimon Schubert t->to_shortname = "bfd";
935796c8dcSSimon Schubert t->to_longname = _("BFD backed target");
945796c8dcSSimon Schubert t->to_doc = _("You should never see this");
955796c8dcSSimon Schubert t->to_get_section_table = target_bfd_get_section_table;
965796c8dcSSimon Schubert t->to_xfer_partial = target_bfd_xfer_partial;
975796c8dcSSimon Schubert t->to_xclose = target_bfd_xclose;
985796c8dcSSimon Schubert t->to_data = data;
995796c8dcSSimon Schubert
1005796c8dcSSimon Schubert return t;
1015796c8dcSSimon Schubert }
102