Commit | Line | Data |
---|---|---|
bba2d28d AC |
1 | /* Very simple "bfd" target, for GDB, the GNU debugger. |
2 | ||
197e01b6 | 3 | Copyright (C) 2003, 2005 Free Software Foundation, Inc. |
bba2d28d AC |
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 2 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, write to the Free Software | |
197e01b6 EZ |
19 | Foundation, Inc., 51 Franklin Street, Fifth Floor, |
20 | Boston, MA 02110-1301, USA. */ | |
bba2d28d AC |
21 | |
22 | #include "defs.h" | |
23 | #include "target.h" | |
24 | #include "bfd-target.h" | |
25 | #include "gdb_assert.h" | |
26 | #include "gdb_string.h" | |
27 | ||
28 | /* Locate all mappable sections of a BFD file, filling in a target | |
29 | section for each. */ | |
30 | ||
31 | struct section_closure | |
32 | { | |
33 | struct section_table *end; | |
34 | }; | |
35 | ||
36 | static void | |
37 | add_to_section_table (struct bfd *abfd, struct bfd_section *asect, | |
38 | void *closure) | |
39 | { | |
40 | struct section_closure *pp = closure; | |
41 | flagword aflag; | |
42 | ||
43 | /* NOTE: cagney/2003-10-22: Is this pruning useful? */ | |
44 | aflag = bfd_get_section_flags (abfd, asect); | |
45 | if (!(aflag & SEC_ALLOC)) | |
46 | return; | |
47 | if (bfd_section_size (abfd, asect) == 0) | |
48 | return; | |
49 | pp->end->bfd = abfd; | |
50 | pp->end->the_bfd_section = asect; | |
51 | pp->end->addr = bfd_section_vma (abfd, asect); | |
52 | pp->end->endaddr = pp->end->addr + bfd_section_size (abfd, asect); | |
53 | pp->end++; | |
54 | } | |
55 | ||
56 | void | |
57 | build_target_sections_from_bfd (struct target_ops *targ, struct bfd *abfd) | |
58 | { | |
59 | unsigned count; | |
60 | struct section_table *start; | |
61 | struct section_closure cl; | |
62 | ||
63 | count = bfd_count_sections (abfd); | |
64 | target_resize_to_sections (targ, count); | |
65 | start = targ->to_sections; | |
66 | cl.end = targ->to_sections; | |
67 | bfd_map_over_sections (abfd, add_to_section_table, &cl); | |
68 | gdb_assert (cl.end - start <= count); | |
69 | } | |
70 | ||
71 | LONGEST | |
72 | target_bfd_xfer_partial (struct target_ops *ops, | |
73 | enum target_object object, | |
961cb7b5 MK |
74 | const char *annex, gdb_byte *readbuf, |
75 | const gdb_byte *writebuf, | |
76 | ULONGEST offset, LONGEST len) | |
bba2d28d AC |
77 | { |
78 | switch (object) | |
79 | { | |
80 | case TARGET_OBJECT_MEMORY: | |
81 | { | |
82 | struct section_table *s = target_section_by_addr (ops, offset); | |
83 | if (s == NULL) | |
84 | return -1; | |
85 | /* If the length extends beyond the section, truncate it. Be | |
86 | careful to not suffer from overflow (wish S contained a | |
87 | length). */ | |
88 | if ((offset - s->addr + len) > (s->endaddr - s->addr)) | |
89 | len = (s->endaddr - s->addr) - (offset - s->addr); | |
90 | if (readbuf != NULL | |
91 | && !bfd_get_section_contents (s->bfd, s->the_bfd_section, | |
92 | readbuf, offset - s->addr, len)) | |
93 | return -1; | |
94 | #if 1 | |
95 | if (writebuf != NULL) | |
96 | return -1; | |
97 | #else | |
98 | /* FIXME: cagney/2003-10-31: The BFD interface doesn't yet | |
99 | take a const buffer. */ | |
100 | if (writebuf != NULL | |
101 | && !bfd_set_section_contents (s->bfd, s->the_bfd_section, | |
102 | writebuf, offset - s->addr, len)) | |
103 | return -1; | |
104 | #endif | |
105 | return len; | |
106 | } | |
107 | default: | |
108 | return -1; | |
109 | } | |
110 | } | |
111 | ||
112 | void | |
113 | target_bfd_xclose (struct target_ops *t, int quitting) | |
114 | { | |
115 | bfd_close (t->to_data); | |
116 | xfree (t->to_sections); | |
117 | xfree (t); | |
118 | } | |
119 | ||
120 | struct target_ops * | |
121 | target_bfd_reopen (struct bfd *bfd) | |
122 | { | |
123 | struct target_ops *t = XZALLOC (struct target_ops); | |
124 | t->to_shortname = "bfd"; | |
3d263c1d BI |
125 | t->to_longname = _("BFD backed target"); |
126 | t->to_doc = _("You should never see this"); | |
bba2d28d AC |
127 | t->to_xfer_partial = target_bfd_xfer_partial; |
128 | t->to_xclose = target_bfd_xclose; | |
129 | t->to_data = bfd; | |
130 | build_target_sections_from_bfd (t, bfd); | |
131 | return t; | |
132 | } |