Commit | Line | Data |
---|---|---|
0f2811d1 DE |
1 | /* Utility to load a file into the simulator. |
2 | Copyright (C) 1997 Free Software Foundation, Inc. | |
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 | ||
d90f7aa5 DE |
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 | ||
0f2811d1 | 22 | #include "config.h" |
0f399b0c SG |
23 | #include "ansidecl.h" |
24 | #ifdef ANSI_PROTOTYPES | |
0f2811d1 DE |
25 | #include <stdarg.h> |
26 | #else | |
27 | #include <varargs.h> | |
28 | #endif | |
29 | #ifdef HAVE_STDLIB_H | |
30 | #include <stdlib.h> | |
31 | #endif | |
32 | #include <time.h> | |
0f2811d1 DE |
33 | #include "bfd.h" |
34 | #include "callback.h" | |
35 | #include "remote-sim.h" | |
36 | ||
37 | static void eprintf PARAMS ((host_callback *, const char *, ...)); | |
38 | static void xprintf PARAMS ((host_callback *, const char *, ...)); | |
39 | static void report_transfer_performance | |
40 | PARAMS ((host_callback *, unsigned long, time_t, time_t)); | |
41 | static void xprintf_bfd_vma PARAMS ((host_callback *, bfd_vma)); | |
42 | ||
43 | /* Load program PROG into the simulator. | |
44 | If PROG_BFD is non-NULL, the file has already been opened. | |
45 | If VERBOSE_P is non-zero statistics are printed of each loaded section | |
46 | and the transfer rate (for consistency with gdb). | |
47 | If this fails an error message is printed and NULL is returned. | |
48 | If it succeeds the bfd is returned. */ | |
49 | ||
50 | /* FIXME: Where can we put a prototype for this? */ | |
51 | ||
52 | bfd * | |
53 | sim_load_file (sd, myname, callback, prog, prog_bfd, verbose_p) | |
54 | SIM_DESC sd; | |
55 | char *myname; | |
56 | host_callback *callback; | |
57 | char *prog; | |
58 | bfd *prog_bfd; | |
59 | { | |
60 | asection *s; | |
61 | /* Record separately as we don't want to close PROG_BFD if it was passed. */ | |
62 | bfd *result_bfd; | |
63 | time_t start_time, end_time; /* Start and end times of download */ | |
64 | unsigned long data_count = 0; /* Number of bytes transferred to memory */ | |
65 | ||
66 | if (prog_bfd != NULL) | |
67 | result_bfd = prog_bfd; | |
68 | else | |
69 | { | |
70 | result_bfd = bfd_openr (prog, 0); | |
71 | if (result_bfd == NULL) | |
72 | { | |
73 | eprintf (callback, "%s: can't open \"%s\": %s\n", | |
74 | myname, prog, bfd_errmsg (bfd_get_error ())); | |
75 | return NULL; | |
76 | } | |
77 | } | |
78 | ||
79 | if (!bfd_check_format (result_bfd, bfd_object)) | |
80 | { | |
81 | eprintf (callback, "%s: \"%s\" is not an object file: %s\n", | |
82 | myname, prog, bfd_errmsg (bfd_get_error ())); | |
83 | /* Only close if we opened it. */ | |
84 | if (prog_bfd == NULL) | |
85 | bfd_close (result_bfd); | |
86 | return NULL; | |
87 | } | |
88 | ||
89 | if (verbose_p) | |
90 | start_time = time (NULL); | |
91 | ||
92 | for (s = result_bfd->sections; s; s = s->next) | |
93 | { | |
94 | if (s->flags & SEC_LOAD) | |
95 | { | |
96 | bfd_size_type size; | |
97 | ||
98 | size = bfd_get_section_size_before_reloc (s); | |
99 | if (size > 0) | |
100 | { | |
101 | char *buffer; | |
102 | bfd_vma lma; | |
103 | ||
104 | buffer = malloc (size); | |
105 | if (buffer == NULL) | |
106 | { | |
107 | eprintf (callback, | |
108 | "%s: insufficient memory to load \"%s\"\n", | |
109 | myname, prog); | |
110 | /* Only close if we opened it. */ | |
111 | if (prog_bfd == NULL) | |
112 | bfd_close (result_bfd); | |
113 | return NULL; | |
114 | } | |
115 | /* Before you change this to bfd_section_lma, make sure | |
116 | the arm-pe simulator still works. */ | |
117 | lma = bfd_section_vma (result_bfd, s); | |
118 | if (verbose_p) | |
119 | { | |
120 | xprintf (callback, "Loading section %s, size 0x%lx lma ", | |
121 | bfd_get_section_name (result_bfd, s), | |
122 | (unsigned long) size); | |
123 | xprintf_bfd_vma (callback, lma); | |
124 | xprintf (callback, "\n"); | |
125 | } | |
126 | data_count += size; | |
127 | bfd_get_section_contents (result_bfd, s, buffer, 0, size); | |
128 | sim_write (sd, lma, buffer, size); | |
129 | free (buffer); | |
130 | } | |
131 | } | |
132 | } | |
133 | ||
134 | if (verbose_p) | |
135 | { | |
136 | end_time = time (NULL); | |
137 | xprintf (callback, "Start address "); | |
138 | xprintf_bfd_vma (callback, bfd_get_start_address (result_bfd)); | |
139 | xprintf (callback, "\n"); | |
140 | report_transfer_performance (callback, data_count, start_time, end_time); | |
141 | } | |
142 | ||
143 | return result_bfd; | |
144 | } | |
145 | ||
146 | static void | |
147 | xprintf VPARAMS ((host_callback *callback, const char *fmt, ...)) | |
148 | { | |
0f399b0c | 149 | #ifndef ANSI_PROTOTYPES |
0f2811d1 DE |
150 | host_callback *callback; |
151 | char *fmt; | |
152 | #endif | |
153 | va_list ap; | |
154 | ||
155 | VA_START (ap, fmt); | |
0f399b0c | 156 | #ifndef ANSI_PROTOTYPES |
0f2811d1 DE |
157 | callback = va_arg (ap, host_callback *); |
158 | fmt = va_arg (ap, char *); | |
159 | #endif | |
160 | ||
161 | (*callback->vprintf_filtered) (callback, fmt, ap); | |
162 | ||
163 | va_end (ap); | |
164 | } | |
165 | ||
166 | static void | |
167 | eprintf VPARAMS ((host_callback *callback, const char *fmt, ...)) | |
168 | { | |
0f399b0c | 169 | #ifndef ANSI_PROTOTYPES |
0f2811d1 DE |
170 | host_callback *callback; |
171 | char *fmt; | |
172 | #endif | |
173 | va_list ap; | |
174 | ||
175 | VA_START (ap, fmt); | |
0f399b0c | 176 | #ifndef ANSI_PROTOTYPES |
0f2811d1 DE |
177 | callback = va_arg (ap, host_callback *); |
178 | fmt = va_arg (ap, char *); | |
179 | #endif | |
180 | ||
181 | (*callback->evprintf_filtered) (callback, fmt, ap); | |
182 | ||
183 | va_end (ap); | |
184 | } | |
185 | ||
186 | /* Report how fast the transfer went. */ | |
187 | ||
188 | static void | |
189 | report_transfer_performance (callback, data_count, start_time, end_time) | |
190 | host_callback *callback; | |
191 | unsigned long data_count; | |
192 | time_t start_time, end_time; | |
193 | { | |
194 | xprintf (callback, "Transfer rate: "); | |
195 | if (end_time != start_time) | |
196 | xprintf (callback, "%ld bits/sec", | |
197 | (data_count * 8) / (end_time - start_time)); | |
198 | else | |
199 | xprintf (callback, "%ld bits in <1 sec", (data_count * 8)); | |
200 | xprintf (callback, ".\n"); | |
201 | } | |
202 | ||
203 | /* Print a bfd_vma. | |
204 | This is intended to handle the vagaries of 32 vs 64 bits, etc. */ | |
205 | ||
206 | static void | |
207 | xprintf_bfd_vma (callback, vma) | |
208 | host_callback *callback; | |
209 | bfd_vma vma; | |
210 | { | |
211 | /* FIXME: for now */ | |
212 | xprintf (callback, "0x%lx", (unsigned long) vma); | |
213 | } |