]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - sim/common/sim-load.c
sim: clean up C11 header includes
[thirdparty/binutils-gdb.git] / sim / common / sim-load.c
1 /* Utility to load a file into the simulator.
2 Copyright (C) 1997-2021 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 3 of the License, or
7 (at your option) 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, see <http://www.gnu.org/licenses/>. */
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
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24 #include "ansidecl.h"
25 #include <stdio.h> /* for NULL */
26 #include <stdarg.h>
27 #include <stdlib.h>
28 #include <time.h>
29
30 #include "sim-basics.h"
31 #include "bfd.h"
32 #include "sim-utils.h"
33
34 #include "gdb/callback.h"
35 #include "gdb/remote-sim.h"
36
37 static void eprintf (host_callback *, const char *, ...);
38 static void xprintf (host_callback *, const char *, ...);
39 static void report_transfer_performance
40 (host_callback *, unsigned long, time_t, time_t);
41 static void xprintf_bfd_vma (host_callback *, bfd_vma);
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 *
57 sim_load_file (SIM_DESC sd, const char *myname, host_callback *callback,
58 const char *prog, bfd *prog_bfd, int verbose_p, int lma_p,
59 sim_write_fn do_write)
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 {
76 eprintf (callback, "%s: can't open \"%s\": %s\n",
77 myname, prog, bfd_errmsg (bfd_get_error ()));
78 return NULL;
79 }
80 }
81
82 if (!bfd_check_format (result_bfd, bfd_object))
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;
96 for (s = result_bfd->sections; s; s = s->next)
97 {
98 if (s->flags & SEC_LOAD)
99 {
100 bfd_size_type size;
101
102 size = bfd_section_size (s);
103 if (size > 0)
104 {
105 unsigned char *buffer;
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)
120 lma = bfd_section_lma (s);
121 else
122 lma = bfd_section_vma (s);
123 if (verbose_p)
124 {
125 xprintf (callback, "Loading section %s, size 0x%lx %s ",
126 bfd_section_name (s),
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
158 bfd_cache_close (result_bfd);
159
160 return result_bfd;
161 }
162
163 static void
164 xprintf (host_callback *callback, const char *fmt, ...)
165 {
166 va_list ap;
167
168 va_start (ap, fmt);
169
170 (*callback->vprintf_filtered) (callback, fmt, ap);
171
172 va_end (ap);
173 }
174
175 static void
176 eprintf (host_callback *callback, const char *fmt, ...)
177 {
178 va_list ap;
179
180 va_start (ap, fmt);
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
190 report_transfer_performance (host_callback *callback, unsigned long data_count,
191 time_t start_time, time_t end_time)
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
206 xprintf_bfd_vma (host_callback *callback, bfd_vma vma)
207 {
208 /* FIXME: for now */
209 xprintf (callback, "0x%lx", (unsigned long) vma);
210 }