1 /* Utility to load a file into the simulator.
2 Copyright (C) 1997-2015 Free Software Foundation, Inc.
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.
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.
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/>. */
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 :-)]. */
25 #include <stdio.h> /* for NULL */
32 #include "sim-basics.h"
34 #include "sim-utils.h"
36 #include "gdb/callback.h"
37 #include "gdb/remote-sim.h"
39 static void eprintf (host_callback *, const char *, ...);
40 static void xprintf (host_callback *, const char *, ...);
41 static void report_transfer_performance
42 (host_callback *, unsigned long, time_t, time_t);
43 static void xprintf_bfd_vma (host_callback *, bfd_vma);
45 /* Load program PROG into the simulator using the function DO_LOAD.
46 If PROG_BFD is non-NULL, the file has already been opened.
47 If VERBOSE_P is non-zero statistics are printed of each loaded section
48 and the transfer rate (for consistency with gdb).
49 If LMA_P is non-zero the program sections are loaded at the LMA
51 If this fails an error message is printed and NULL is returned.
52 If it succeeds the bfd is returned.
53 NOTE: For historical reasons, older hardware simulators incorrectly
54 write the program sections at LMA interpreted as a virtual address.
55 This is still accommodated for backward compatibility reasons. */
59 sim_load_file (SIM_DESC sd, const char *myname, host_callback *callback,
60 const char *prog, bfd *prog_bfd, int verbose_p, int lma_p,
61 sim_write_fn do_write)
64 /* Record separately as we don't want to close PROG_BFD if it was passed. */
66 time_t start_time = 0; /* Start and end times of download */
68 unsigned long data_count = 0; /* Number of bytes transferred to memory */
69 int found_loadable_section;
72 result_bfd = prog_bfd;
75 result_bfd = bfd_openr (prog, 0);
76 if (result_bfd == NULL)
78 eprintf (callback, "%s: can't open \"%s\": %s\n",
79 myname, prog, bfd_errmsg (bfd_get_error ()));
84 if (!bfd_check_format (result_bfd, bfd_object))
86 eprintf (callback, "%s: \"%s\" is not an object file: %s\n",
87 myname, prog, bfd_errmsg (bfd_get_error ()));
88 /* Only close if we opened it. */
90 bfd_close (result_bfd);
95 start_time = time (NULL);
97 found_loadable_section = 0;
98 for (s = result_bfd->sections; s; s = s->next)
100 if (s->flags & SEC_LOAD)
104 size = bfd_get_section_size (s);
107 unsigned char *buffer;
110 buffer = malloc (size);
114 "%s: insufficient memory to load \"%s\"\n",
116 /* Only close if we opened it. */
117 if (prog_bfd == NULL)
118 bfd_close (result_bfd);
122 lma = bfd_section_lma (result_bfd, s);
124 lma = bfd_section_vma (result_bfd, s);
127 xprintf (callback, "Loading section %s, size 0x%lx %s ",
128 bfd_get_section_name (result_bfd, s),
129 (unsigned long) size,
130 (lma_p ? "lma" : "vma"));
131 xprintf_bfd_vma (callback, lma);
132 xprintf (callback, "\n");
135 bfd_get_section_contents (result_bfd, s, buffer, 0, size);
136 do_write (sd, lma, buffer, size);
137 found_loadable_section = 1;
143 if (!found_loadable_section)
146 "%s: no loadable sections \"%s\"\n",
153 end_time = time (NULL);
154 xprintf (callback, "Start address ");
155 xprintf_bfd_vma (callback, bfd_get_start_address (result_bfd));
156 xprintf (callback, "\n");
157 report_transfer_performance (callback, data_count, start_time, end_time);
160 bfd_cache_close (result_bfd);
166 xprintf (host_callback *callback, const char *fmt, ...)
172 (*callback->vprintf_filtered) (callback, fmt, ap);
178 eprintf (host_callback *callback, const char *fmt, ...)
184 (*callback->evprintf_filtered) (callback, fmt, ap);
189 /* Report how fast the transfer went. */
192 report_transfer_performance (host_callback *callback, unsigned long data_count,
193 time_t start_time, time_t end_time)
195 xprintf (callback, "Transfer rate: ");
196 if (end_time != start_time)
197 xprintf (callback, "%ld bits/sec",
198 (data_count * 8) / (end_time - start_time));
200 xprintf (callback, "%ld bits in <1 sec", (data_count * 8));
201 xprintf (callback, ".\n");
205 This is intended to handle the vagaries of 32 vs 64 bits, etc. */
208 xprintf_bfd_vma (host_callback *callback, bfd_vma vma)
211 xprintf (callback, "0x%lx", (unsigned long) vma);