]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - sim/common/sim-load.c
Update copyright year range in header of all files managed by GDB
[thirdparty/binutils-gdb.git] / sim / common / sim-load.c
CommitLineData
c906108c 1/* Utility to load a file into the simulator.
1d506c26 2 Copyright (C) 1997-2024 Free Software Foundation, Inc.
c906108c
SS
3
4This program is free software; you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
4744ac1b
JB
6the Free Software Foundation; either version 3 of the License, or
7(at your option) any later version.
c906108c
SS
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
4744ac1b 15along 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
6df01ab8
MF
21/* This must come before any other includes. */
22#include "defs.h"
23
c906108c 24#include <stdarg.h>
20a8e078 25#include <stdio.h> /* for NULL */
c906108c 26#include <stdlib.h>
c906108c
SS
27#include <time.h>
28
20a8e078 29#include "ansidecl.h"
c906108c 30#include "bfd.h"
c906108c 31
df68e12b
MF
32#include "sim/callback.h"
33#include "sim/sim.h"
e9307449 34#include "sim-utils.h"
c906108c 35
bdca5ee4
TT
36static void eprintf (host_callback *, const char *, ...);
37static void xprintf (host_callback *, const char *, ...);
c906108c 38static void report_transfer_performance
bdca5ee4 39 (host_callback *, unsigned long, time_t, time_t);
c906108c 40
8bd59ec1 41/* Load program PROG into the simulator using the function DO_WRITE.
c906108c
SS
42 If PROG_BFD is non-NULL, the file has already been opened.
43 If VERBOSE_P is non-zero statistics are printed of each loaded section
44 and the transfer rate (for consistency with gdb).
45 If LMA_P is non-zero the program sections are loaded at the LMA
46 rather than the VMA
47 If this fails an error message is printed and NULL is returned.
48 If it succeeds the bfd is returned.
49 NOTE: For historical reasons, older hardware simulators incorrectly
50 write the program sections at LMA interpreted as a virtual address.
51 This is still accommodated for backward compatibility reasons. */
52
53
54bfd *
1a8a700e 55sim_load_file (SIM_DESC sd, const char *myname, host_callback *callback,
b2b255bd 56 const char *prog, bfd *prog_bfd, int verbose_p, int lma_p,
1a8a700e 57 sim_write_fn do_write)
c906108c
SS
58{
59 asection *s;
60 /* Record separately as we don't want to close PROG_BFD if it was passed. */
61 bfd *result_bfd;
62 time_t start_time = 0; /* Start and end times of download */
63 time_t end_time = 0;
64 unsigned long data_count = 0; /* Number of bytes transferred to memory */
65 int found_loadable_section;
66
67 if (prog_bfd != NULL)
68 result_bfd = prog_bfd;
69 else
70 {
71 result_bfd = bfd_openr (prog, 0);
72 if (result_bfd == NULL)
73 {
028f6515 74 eprintf (callback, "%s: can't open \"%s\": %s\n",
c906108c
SS
75 myname, prog, bfd_errmsg (bfd_get_error ()));
76 return NULL;
77 }
78 }
79
028f6515 80 if (!bfd_check_format (result_bfd, bfd_object))
c906108c
SS
81 {
82 eprintf (callback, "%s: \"%s\" is not an object file: %s\n",
83 myname, prog, bfd_errmsg (bfd_get_error ()));
84 /* Only close if we opened it. */
85 if (prog_bfd == NULL)
86 bfd_close (result_bfd);
87 return NULL;
88 }
89
90 if (verbose_p)
91 start_time = time (NULL);
92
93 found_loadable_section = 0;
028f6515 94 for (s = result_bfd->sections; s; s = s->next)
c906108c 95 {
028f6515 96 if (s->flags & SEC_LOAD)
c906108c
SS
97 {
98 bfd_size_type size;
99
fd361982 100 size = bfd_section_size (s);
c906108c
SS
101 if (size > 0)
102 {
cc25892b 103 unsigned char *buffer;
c906108c
SS
104 bfd_vma lma;
105
106 buffer = malloc (size);
107 if (buffer == NULL)
108 {
109 eprintf (callback,
110 "%s: insufficient memory to load \"%s\"\n",
111 myname, prog);
112 /* Only close if we opened it. */
113 if (prog_bfd == NULL)
114 bfd_close (result_bfd);
115 return NULL;
116 }
117 if (lma_p)
fd361982 118 lma = bfd_section_lma (s);
c906108c 119 else
fd361982 120 lma = bfd_section_vma (s);
c906108c
SS
121 if (verbose_p)
122 {
5ee0bc23 123 xprintf (callback,
29136be7
AM
124 "Loading section %s, size 0x%" PRIx64
125 " %s %" PRIx64 "\n",
126 bfd_section_name (s), (uint64_t) size,
127 lma_p ? "lma" : "vma", (uint64_t) lma);
c906108c
SS
128 }
129 data_count += size;
130 bfd_get_section_contents (result_bfd, s, buffer, 0, size);
131 do_write (sd, lma, buffer, size);
132 found_loadable_section = 1;
133 free (buffer);
134 }
135 }
136 }
137
138 if (!found_loadable_section)
139 {
140 eprintf (callback,
141 "%s: no loadable sections \"%s\"\n",
142 myname, prog);
143 return NULL;
144 }
145
146 if (verbose_p)
147 {
148 end_time = time (NULL);
29136be7
AM
149 xprintf (callback, "Start address %" PRIx64 "\n",
150 (uint64_t) bfd_get_start_address (result_bfd));
c906108c
SS
151 report_transfer_performance (callback, data_count, start_time, end_time);
152 }
153
2836ee25
FCE
154 bfd_cache_close (result_bfd);
155
c906108c
SS
156 return result_bfd;
157}
158
159static void
bdca5ee4 160xprintf (host_callback *callback, const char *fmt, ...)
c906108c 161{
c906108c
SS
162 va_list ap;
163
6104cb7a 164 va_start (ap, fmt);
c906108c
SS
165
166 (*callback->vprintf_filtered) (callback, fmt, ap);
167
168 va_end (ap);
169}
170
171static void
bdca5ee4 172eprintf (host_callback *callback, const char *fmt, ...)
c906108c 173{
c906108c
SS
174 va_list ap;
175
6104cb7a 176 va_start (ap, fmt);
c906108c
SS
177
178 (*callback->evprintf_filtered) (callback, fmt, ap);
179
180 va_end (ap);
181}
182
183/* Report how fast the transfer went. */
184
185static void
1a8a700e
MF
186report_transfer_performance (host_callback *callback, unsigned long data_count,
187 time_t start_time, time_t end_time)
c906108c
SS
188{
189 xprintf (callback, "Transfer rate: ");
190 if (end_time != start_time)
191 xprintf (callback, "%ld bits/sec",
192 (data_count * 8) / (end_time - start_time));
193 else
194 xprintf (callback, "%ld bits in <1 sec", (data_count * 8));
195 xprintf (callback, ".\n");
196}