Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Utility to load a file into the simulator. |
3666a048 | 2 | Copyright (C) 1997-2021 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 | |
4744ac1b JB |
6 | the Free Software Foundation; either version 3 of the License, or |
7 | (at your option) any later version. | |
c906108c SS |
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 | |
4744ac1b | 15 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
c906108c SS |
16 | |
17 | /* This is a standalone loader, independent of the sim-basic.h machinery, | |
18 | as it is used by simulators that don't use it [though that doesn't mean | |
19 | to suggest that they shouldn't :-)]. */ | |
20 | ||
41ee5402 | 21 | #ifdef HAVE_CONFIG_H |
936df756 | 22 | #include "config.h" |
41ee5402 | 23 | #endif |
c906108c SS |
24 | #include "ansidecl.h" |
25 | #include <stdio.h> /* for NULL */ | |
c906108c | 26 | #include <stdarg.h> |
c906108c | 27 | #include <stdlib.h> |
c906108c SS |
28 | #include <time.h> |
29 | ||
30 | #include "sim-basics.h" | |
31 | #include "bfd.h" | |
32 | #include "sim-utils.h" | |
33 | ||
3c25f8c7 AC |
34 | #include "gdb/callback.h" |
35 | #include "gdb/remote-sim.h" | |
c906108c | 36 | |
bdca5ee4 TT |
37 | static void eprintf (host_callback *, const char *, ...); |
38 | static void xprintf (host_callback *, const char *, ...); | |
c906108c | 39 | static void report_transfer_performance |
bdca5ee4 TT |
40 | (host_callback *, unsigned long, time_t, time_t); |
41 | static void xprintf_bfd_vma (host_callback *, bfd_vma); | |
c906108c SS |
42 | |
43 | /* Load program PROG into the simulator using the function DO_LOAD. | |
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 LMA_P is non-zero the program sections are loaded at the LMA | |
48 | rather than the VMA | |
49 | If this fails an error message is printed and NULL is returned. | |
50 | If it succeeds the bfd is returned. | |
51 | NOTE: For historical reasons, older hardware simulators incorrectly | |
52 | write the program sections at LMA interpreted as a virtual address. | |
53 | This is still accommodated for backward compatibility reasons. */ | |
54 | ||
55 | ||
56 | bfd * | |
1a8a700e | 57 | sim_load_file (SIM_DESC sd, const char *myname, host_callback *callback, |
b2b255bd | 58 | const char *prog, bfd *prog_bfd, int verbose_p, int lma_p, |
1a8a700e | 59 | sim_write_fn do_write) |
c906108c SS |
60 | { |
61 | asection *s; | |
62 | /* Record separately as we don't want to close PROG_BFD if it was passed. */ | |
63 | bfd *result_bfd; | |
64 | time_t start_time = 0; /* Start and end times of download */ | |
65 | time_t end_time = 0; | |
66 | unsigned long data_count = 0; /* Number of bytes transferred to memory */ | |
67 | int found_loadable_section; | |
68 | ||
69 | if (prog_bfd != NULL) | |
70 | result_bfd = prog_bfd; | |
71 | else | |
72 | { | |
73 | result_bfd = bfd_openr (prog, 0); | |
74 | if (result_bfd == NULL) | |
75 | { | |
028f6515 | 76 | eprintf (callback, "%s: can't open \"%s\": %s\n", |
c906108c SS |
77 | myname, prog, bfd_errmsg (bfd_get_error ())); |
78 | return NULL; | |
79 | } | |
80 | } | |
81 | ||
028f6515 | 82 | if (!bfd_check_format (result_bfd, bfd_object)) |
c906108c SS |
83 | { |
84 | eprintf (callback, "%s: \"%s\" is not an object file: %s\n", | |
85 | myname, prog, bfd_errmsg (bfd_get_error ())); | |
86 | /* Only close if we opened it. */ | |
87 | if (prog_bfd == NULL) | |
88 | bfd_close (result_bfd); | |
89 | return NULL; | |
90 | } | |
91 | ||
92 | if (verbose_p) | |
93 | start_time = time (NULL); | |
94 | ||
95 | found_loadable_section = 0; | |
028f6515 | 96 | for (s = result_bfd->sections; s; s = s->next) |
c906108c | 97 | { |
028f6515 | 98 | if (s->flags & SEC_LOAD) |
c906108c SS |
99 | { |
100 | bfd_size_type size; | |
101 | ||
fd361982 | 102 | size = bfd_section_size (s); |
c906108c SS |
103 | if (size > 0) |
104 | { | |
cc25892b | 105 | unsigned char *buffer; |
c906108c SS |
106 | bfd_vma lma; |
107 | ||
108 | buffer = malloc (size); | |
109 | if (buffer == NULL) | |
110 | { | |
111 | eprintf (callback, | |
112 | "%s: insufficient memory to load \"%s\"\n", | |
113 | myname, prog); | |
114 | /* Only close if we opened it. */ | |
115 | if (prog_bfd == NULL) | |
116 | bfd_close (result_bfd); | |
117 | return NULL; | |
118 | } | |
119 | if (lma_p) | |
fd361982 | 120 | lma = bfd_section_lma (s); |
c906108c | 121 | else |
fd361982 | 122 | lma = bfd_section_vma (s); |
c906108c SS |
123 | if (verbose_p) |
124 | { | |
125 | xprintf (callback, "Loading section %s, size 0x%lx %s ", | |
fd361982 | 126 | bfd_section_name (s), |
c906108c SS |
127 | (unsigned long) size, |
128 | (lma_p ? "lma" : "vma")); | |
129 | xprintf_bfd_vma (callback, lma); | |
130 | xprintf (callback, "\n"); | |
131 | } | |
132 | data_count += size; | |
133 | bfd_get_section_contents (result_bfd, s, buffer, 0, size); | |
134 | do_write (sd, lma, buffer, size); | |
135 | found_loadable_section = 1; | |
136 | free (buffer); | |
137 | } | |
138 | } | |
139 | } | |
140 | ||
141 | if (!found_loadable_section) | |
142 | { | |
143 | eprintf (callback, | |
144 | "%s: no loadable sections \"%s\"\n", | |
145 | myname, prog); | |
146 | return NULL; | |
147 | } | |
148 | ||
149 | if (verbose_p) | |
150 | { | |
151 | end_time = time (NULL); | |
152 | xprintf (callback, "Start address "); | |
153 | xprintf_bfd_vma (callback, bfd_get_start_address (result_bfd)); | |
154 | xprintf (callback, "\n"); | |
155 | report_transfer_performance (callback, data_count, start_time, end_time); | |
156 | } | |
157 | ||
2836ee25 FCE |
158 | bfd_cache_close (result_bfd); |
159 | ||
c906108c SS |
160 | return result_bfd; |
161 | } | |
162 | ||
163 | static void | |
bdca5ee4 | 164 | xprintf (host_callback *callback, const char *fmt, ...) |
c906108c | 165 | { |
c906108c SS |
166 | va_list ap; |
167 | ||
6104cb7a | 168 | va_start (ap, fmt); |
c906108c SS |
169 | |
170 | (*callback->vprintf_filtered) (callback, fmt, ap); | |
171 | ||
172 | va_end (ap); | |
173 | } | |
174 | ||
175 | static void | |
bdca5ee4 | 176 | eprintf (host_callback *callback, const char *fmt, ...) |
c906108c | 177 | { |
c906108c SS |
178 | va_list ap; |
179 | ||
6104cb7a | 180 | va_start (ap, fmt); |
c906108c SS |
181 | |
182 | (*callback->evprintf_filtered) (callback, fmt, ap); | |
183 | ||
184 | va_end (ap); | |
185 | } | |
186 | ||
187 | /* Report how fast the transfer went. */ | |
188 | ||
189 | static void | |
1a8a700e MF |
190 | report_transfer_performance (host_callback *callback, unsigned long data_count, |
191 | time_t start_time, time_t end_time) | |
c906108c SS |
192 | { |
193 | xprintf (callback, "Transfer rate: "); | |
194 | if (end_time != start_time) | |
195 | xprintf (callback, "%ld bits/sec", | |
196 | (data_count * 8) / (end_time - start_time)); | |
197 | else | |
198 | xprintf (callback, "%ld bits in <1 sec", (data_count * 8)); | |
199 | xprintf (callback, ".\n"); | |
200 | } | |
201 | ||
202 | /* Print a bfd_vma. | |
203 | This is intended to handle the vagaries of 32 vs 64 bits, etc. */ | |
204 | ||
205 | static void | |
1a8a700e | 206 | xprintf_bfd_vma (host_callback *callback, bfd_vma vma) |
c906108c SS |
207 | { |
208 | /* FIXME: for now */ | |
209 | xprintf (callback, "0x%lx", (unsigned long) vma); | |
210 | } |