Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Utility to load a file into the simulator. |
41ee5402 | 2 | Copyright (C) 1997, 1998, 2001, 2002, 2004 Free Software Foundation, Inc. |
c906108c SS |
3 | |
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 2, or (at your option) | |
7 | any later version. | |
8 | ||
9 | This program is distributed in the hope that it will be useful, | |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | GNU General Public License for more details. | |
13 | ||
14 | You should have received a copy of the GNU General Public License | |
15 | along with this program; if not, write to the Free Software Foundation, Inc., | |
16 | 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ | |
17 | ||
18 | /* This is a standalone loader, independent of the sim-basic.h machinery, | |
19 | as it is used by simulators that don't use it [though that doesn't mean | |
20 | to suggest that they shouldn't :-)]. */ | |
21 | ||
41ee5402 DJ |
22 | #ifdef HAVE_CONFIG_H |
23 | #include "cconfig.h" | |
24 | #endif | |
c906108c SS |
25 | #include "ansidecl.h" |
26 | #include <stdio.h> /* for NULL */ | |
27 | #ifdef ANSI_PROTOTYPES | |
28 | #include <stdarg.h> | |
29 | #else | |
30 | #include <varargs.h> | |
31 | #endif | |
32 | #ifdef HAVE_STDLIB_H | |
33 | #include <stdlib.h> | |
34 | #endif | |
35 | #include <time.h> | |
36 | ||
37 | #include "sim-basics.h" | |
38 | #include "bfd.h" | |
39 | #include "sim-utils.h" | |
40 | ||
3c25f8c7 AC |
41 | #include "gdb/callback.h" |
42 | #include "gdb/remote-sim.h" | |
c906108c SS |
43 | |
44 | static void eprintf PARAMS ((host_callback *, const char *, ...)); | |
45 | static void xprintf PARAMS ((host_callback *, const char *, ...)); | |
46 | static void report_transfer_performance | |
47 | PARAMS ((host_callback *, unsigned long, time_t, time_t)); | |
48 | static void xprintf_bfd_vma PARAMS ((host_callback *, bfd_vma)); | |
49 | ||
50 | /* Load program PROG into the simulator using the function DO_LOAD. | |
51 | If PROG_BFD is non-NULL, the file has already been opened. | |
52 | If VERBOSE_P is non-zero statistics are printed of each loaded section | |
53 | and the transfer rate (for consistency with gdb). | |
54 | If LMA_P is non-zero the program sections are loaded at the LMA | |
55 | rather than the VMA | |
56 | If this fails an error message is printed and NULL is returned. | |
57 | If it succeeds the bfd is returned. | |
58 | NOTE: For historical reasons, older hardware simulators incorrectly | |
59 | write the program sections at LMA interpreted as a virtual address. | |
60 | This is still accommodated for backward compatibility reasons. */ | |
61 | ||
62 | ||
63 | bfd * | |
64 | sim_load_file (sd, myname, callback, prog, prog_bfd, verbose_p, lma_p, do_write) | |
65 | SIM_DESC sd; | |
66 | const char *myname; | |
67 | host_callback *callback; | |
68 | char *prog; | |
69 | bfd *prog_bfd; | |
70 | int verbose_p; | |
71 | int lma_p; | |
72 | sim_write_fn do_write; | |
73 | { | |
74 | asection *s; | |
75 | /* Record separately as we don't want to close PROG_BFD if it was passed. */ | |
76 | bfd *result_bfd; | |
77 | time_t start_time = 0; /* Start and end times of download */ | |
78 | time_t end_time = 0; | |
79 | unsigned long data_count = 0; /* Number of bytes transferred to memory */ | |
80 | int found_loadable_section; | |
81 | ||
82 | if (prog_bfd != NULL) | |
83 | result_bfd = prog_bfd; | |
84 | else | |
85 | { | |
86 | result_bfd = bfd_openr (prog, 0); | |
87 | if (result_bfd == NULL) | |
88 | { | |
89 | eprintf (callback, "%s: can't open \"%s\": %s\n", | |
90 | myname, prog, bfd_errmsg (bfd_get_error ())); | |
91 | return NULL; | |
92 | } | |
93 | } | |
94 | ||
95 | if (!bfd_check_format (result_bfd, bfd_object)) | |
96 | { | |
97 | eprintf (callback, "%s: \"%s\" is not an object file: %s\n", | |
98 | myname, prog, bfd_errmsg (bfd_get_error ())); | |
99 | /* Only close if we opened it. */ | |
100 | if (prog_bfd == NULL) | |
101 | bfd_close (result_bfd); | |
102 | return NULL; | |
103 | } | |
104 | ||
105 | if (verbose_p) | |
106 | start_time = time (NULL); | |
107 | ||
108 | found_loadable_section = 0; | |
109 | for (s = result_bfd->sections; s; s = s->next) | |
110 | { | |
111 | if (s->flags & SEC_LOAD) | |
112 | { | |
113 | bfd_size_type size; | |
114 | ||
5f510f9c | 115 | size = bfd_get_section_size (s); |
c906108c SS |
116 | if (size > 0) |
117 | { | |
118 | char *buffer; | |
119 | bfd_vma lma; | |
120 | ||
121 | buffer = malloc (size); | |
122 | if (buffer == NULL) | |
123 | { | |
124 | eprintf (callback, | |
125 | "%s: insufficient memory to load \"%s\"\n", | |
126 | myname, prog); | |
127 | /* Only close if we opened it. */ | |
128 | if (prog_bfd == NULL) | |
129 | bfd_close (result_bfd); | |
130 | return NULL; | |
131 | } | |
132 | if (lma_p) | |
133 | lma = bfd_section_lma (result_bfd, s); | |
134 | else | |
135 | lma = bfd_section_vma (result_bfd, s); | |
136 | if (verbose_p) | |
137 | { | |
138 | xprintf (callback, "Loading section %s, size 0x%lx %s ", | |
139 | bfd_get_section_name (result_bfd, s), | |
140 | (unsigned long) size, | |
141 | (lma_p ? "lma" : "vma")); | |
142 | xprintf_bfd_vma (callback, lma); | |
143 | xprintf (callback, "\n"); | |
144 | } | |
145 | data_count += size; | |
146 | bfd_get_section_contents (result_bfd, s, buffer, 0, size); | |
147 | do_write (sd, lma, buffer, size); | |
148 | found_loadable_section = 1; | |
149 | free (buffer); | |
150 | } | |
151 | } | |
152 | } | |
153 | ||
154 | if (!found_loadable_section) | |
155 | { | |
156 | eprintf (callback, | |
157 | "%s: no loadable sections \"%s\"\n", | |
158 | myname, prog); | |
159 | return NULL; | |
160 | } | |
161 | ||
162 | if (verbose_p) | |
163 | { | |
164 | end_time = time (NULL); | |
165 | xprintf (callback, "Start address "); | |
166 | xprintf_bfd_vma (callback, bfd_get_start_address (result_bfd)); | |
167 | xprintf (callback, "\n"); | |
168 | report_transfer_performance (callback, data_count, start_time, end_time); | |
169 | } | |
170 | ||
2836ee25 FCE |
171 | bfd_cache_close (result_bfd); |
172 | ||
c906108c SS |
173 | return result_bfd; |
174 | } | |
175 | ||
176 | static void | |
177 | xprintf VPARAMS ((host_callback *callback, const char *fmt, ...)) | |
178 | { | |
179 | #ifndef ANSI_PROTOTYPES | |
180 | host_callback *callback; | |
181 | char *fmt; | |
182 | #endif | |
183 | va_list ap; | |
184 | ||
185 | VA_START (ap, fmt); | |
186 | #ifndef ANSI_PROTOTYPES | |
187 | callback = va_arg (ap, host_callback *); | |
188 | fmt = va_arg (ap, char *); | |
189 | #endif | |
190 | ||
191 | (*callback->vprintf_filtered) (callback, fmt, ap); | |
192 | ||
193 | va_end (ap); | |
194 | } | |
195 | ||
196 | static void | |
197 | eprintf VPARAMS ((host_callback *callback, const char *fmt, ...)) | |
198 | { | |
199 | #ifndef ANSI_PROTOTYPES | |
200 | host_callback *callback; | |
201 | char *fmt; | |
202 | #endif | |
203 | va_list ap; | |
204 | ||
205 | VA_START (ap, fmt); | |
206 | #ifndef ANSI_PROTOTYPES | |
207 | callback = va_arg (ap, host_callback *); | |
208 | fmt = va_arg (ap, char *); | |
209 | #endif | |
210 | ||
211 | (*callback->evprintf_filtered) (callback, fmt, ap); | |
212 | ||
213 | va_end (ap); | |
214 | } | |
215 | ||
216 | /* Report how fast the transfer went. */ | |
217 | ||
218 | static void | |
219 | report_transfer_performance (callback, data_count, start_time, end_time) | |
220 | host_callback *callback; | |
221 | unsigned long data_count; | |
222 | time_t start_time, end_time; | |
223 | { | |
224 | xprintf (callback, "Transfer rate: "); | |
225 | if (end_time != start_time) | |
226 | xprintf (callback, "%ld bits/sec", | |
227 | (data_count * 8) / (end_time - start_time)); | |
228 | else | |
229 | xprintf (callback, "%ld bits in <1 sec", (data_count * 8)); | |
230 | xprintf (callback, ".\n"); | |
231 | } | |
232 | ||
233 | /* Print a bfd_vma. | |
234 | This is intended to handle the vagaries of 32 vs 64 bits, etc. */ | |
235 | ||
236 | static void | |
237 | xprintf_bfd_vma (callback, vma) | |
238 | host_callback *callback; | |
239 | bfd_vma vma; | |
240 | { | |
241 | /* FIXME: for now */ | |
242 | xprintf (callback, "0x%lx", (unsigned long) vma); | |
243 | } |