]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - sim/common/sim-load.c
sim: drop core libiberty.h include
[thirdparty/binutils-gdb.git] / sim / common / sim-load.c
CommitLineData
c906108c 1/* Utility to load a file into the simulator.
3666a048 2 Copyright (C) 1997-2021 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
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
df68e12b
MF
34#include "sim/callback.h"
35#include "sim/sim.h"
c906108c 36
bdca5ee4
TT
37static void eprintf (host_callback *, const char *, ...);
38static void xprintf (host_callback *, const char *, ...);
c906108c 39static void report_transfer_performance
bdca5ee4 40 (host_callback *, unsigned long, time_t, time_t);
c906108c
SS
41
42/* Load program PROG into the simulator using the function DO_LOAD.
43 If PROG_BFD is non-NULL, the file has already been opened.
44 If VERBOSE_P is non-zero statistics are printed of each loaded section
45 and the transfer rate (for consistency with gdb).
46 If LMA_P is non-zero the program sections are loaded at the LMA
47 rather than the VMA
48 If this fails an error message is printed and NULL is returned.
49 If it succeeds the bfd is returned.
50 NOTE: For historical reasons, older hardware simulators incorrectly
51 write the program sections at LMA interpreted as a virtual address.
52 This is still accommodated for backward compatibility reasons. */
53
54
55bfd *
1a8a700e 56sim_load_file (SIM_DESC sd, const char *myname, host_callback *callback,
b2b255bd 57 const char *prog, bfd *prog_bfd, int verbose_p, int lma_p,
1a8a700e 58 sim_write_fn do_write)
c906108c
SS
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 = 0; /* Start and end times of download */
64 time_t end_time = 0;
65 unsigned long data_count = 0; /* Number of bytes transferred to memory */
66 int found_loadable_section;
67
68 if (prog_bfd != NULL)
69 result_bfd = prog_bfd;
70 else
71 {
72 result_bfd = bfd_openr (prog, 0);
73 if (result_bfd == NULL)
74 {
028f6515 75 eprintf (callback, "%s: can't open \"%s\": %s\n",
c906108c
SS
76 myname, prog, bfd_errmsg (bfd_get_error ()));
77 return NULL;
78 }
79 }
80
028f6515 81 if (!bfd_check_format (result_bfd, bfd_object))
c906108c
SS
82 {
83 eprintf (callback, "%s: \"%s\" is not an object file: %s\n",
84 myname, prog, bfd_errmsg (bfd_get_error ()));
85 /* Only close if we opened it. */
86 if (prog_bfd == NULL)
87 bfd_close (result_bfd);
88 return NULL;
89 }
90
91 if (verbose_p)
92 start_time = time (NULL);
93
94 found_loadable_section = 0;
028f6515 95 for (s = result_bfd->sections; s; s = s->next)
c906108c 96 {
028f6515 97 if (s->flags & SEC_LOAD)
c906108c
SS
98 {
99 bfd_size_type size;
100
fd361982 101 size = bfd_section_size (s);
c906108c
SS
102 if (size > 0)
103 {
cc25892b 104 unsigned char *buffer;
c906108c
SS
105 bfd_vma lma;
106
107 buffer = malloc (size);
108 if (buffer == NULL)
109 {
110 eprintf (callback,
111 "%s: insufficient memory to load \"%s\"\n",
112 myname, prog);
113 /* Only close if we opened it. */
114 if (prog_bfd == NULL)
115 bfd_close (result_bfd);
116 return NULL;
117 }
118 if (lma_p)
fd361982 119 lma = bfd_section_lma (s);
c906108c 120 else
fd361982 121 lma = bfd_section_vma (s);
c906108c
SS
122 if (verbose_p)
123 {
5ee0bc23
MF
124 xprintf (callback,
125 "Loading section %s, size 0x%lx %s "
126 "%" BFD_VMA_FMT "x\n",
fd361982 127 bfd_section_name (s),
c906108c 128 (unsigned long) size,
5ee0bc23 129 (lma_p ? "lma" : "vma"), lma);
c906108c
SS
130 }
131 data_count += size;
132 bfd_get_section_contents (result_bfd, s, buffer, 0, size);
133 do_write (sd, lma, buffer, size);
134 found_loadable_section = 1;
135 free (buffer);
136 }
137 }
138 }
139
140 if (!found_loadable_section)
141 {
142 eprintf (callback,
143 "%s: no loadable sections \"%s\"\n",
144 myname, prog);
145 return NULL;
146 }
147
148 if (verbose_p)
149 {
150 end_time = time (NULL);
5ee0bc23
MF
151 xprintf (callback, "Start address %" BFD_VMA_FMT "x\n",
152 bfd_get_start_address (result_bfd));
c906108c
SS
153 report_transfer_performance (callback, data_count, start_time, end_time);
154 }
155
2836ee25
FCE
156 bfd_cache_close (result_bfd);
157
c906108c
SS
158 return result_bfd;
159}
160
161static void
bdca5ee4 162xprintf (host_callback *callback, const char *fmt, ...)
c906108c 163{
c906108c
SS
164 va_list ap;
165
6104cb7a 166 va_start (ap, fmt);
c906108c
SS
167
168 (*callback->vprintf_filtered) (callback, fmt, ap);
169
170 va_end (ap);
171}
172
173static void
bdca5ee4 174eprintf (host_callback *callback, const char *fmt, ...)
c906108c 175{
c906108c
SS
176 va_list ap;
177
6104cb7a 178 va_start (ap, fmt);
c906108c
SS
179
180 (*callback->evprintf_filtered) (callback, fmt, ap);
181
182 va_end (ap);
183}
184
185/* Report how fast the transfer went. */
186
187static void
1a8a700e
MF
188report_transfer_performance (host_callback *callback, unsigned long data_count,
189 time_t start_time, time_t end_time)
c906108c
SS
190{
191 xprintf (callback, "Transfer rate: ");
192 if (end_time != start_time)
193 xprintf (callback, "%ld bits/sec",
194 (data_count * 8) / (end_time - start_time));
195 else
196 xprintf (callback, "%ld bits in <1 sec", (data_count * 8));
197 xprintf (callback, ".\n");
198}