1 /* Utility to load a file into the simulator.
2 Copyright (C) 1997 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 2, or (at your option)
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, write to the Free Software Foundation, Inc.,
16 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /* This is a standalone loader, independent of the sim-basic.h machinery,
19 as it is used by simulators that don't use it [though that doesn't mean
20 to suggest that they shouldn't :-)]. */
24 #include <stdio.h> /* for NULL */
25 #ifdef ANSI_PROTOTYPES
35 #include "sim-basics.h"
37 #include "sim-utils.h"
39 #include "gdb/callback.h"
40 #include "gdb/remote-sim.h"
42 static void eprintf PARAMS ((host_callback *, const char *, ...));
43 static void xprintf PARAMS ((host_callback *, const char *, ...));
44 static void report_transfer_performance
45 PARAMS ((host_callback *, unsigned long, time_t, time_t));
46 static void xprintf_bfd_vma PARAMS ((host_callback *, bfd_vma));
48 /* Load program PROG into the simulator using the function DO_LOAD.
49 If PROG_BFD is non-NULL, the file has already been opened.
50 If VERBOSE_P is non-zero statistics are printed of each loaded section
51 and the transfer rate (for consistency with gdb).
52 If LMA_P is non-zero the program sections are loaded at the LMA
54 If this fails an error message is printed and NULL is returned.
55 If it succeeds the bfd is returned.
56 NOTE: For historical reasons, older hardware simulators incorrectly
57 write the program sections at LMA interpreted as a virtual address.
58 This is still accommodated for backward compatibility reasons. */
62 sim_load_file (sd, myname, callback, prog, prog_bfd, verbose_p, lma_p, do_write)
65 host_callback *callback;
70 sim_write_fn do_write;
73 /* Record separately as we don't want to close PROG_BFD if it was passed. */
75 time_t start_time = 0; /* Start and end times of download */
77 unsigned long data_count = 0; /* Number of bytes transferred to memory */
78 int found_loadable_section;
81 result_bfd = prog_bfd;
84 result_bfd = bfd_openr (prog, 0);
85 if (result_bfd == NULL)
87 eprintf (callback, "%s: can't open \"%s\": %s\n",
88 myname, prog, bfd_errmsg (bfd_get_error ()));
93 if (!bfd_check_format (result_bfd, bfd_object))
95 eprintf (callback, "%s: \"%s\" is not an object file: %s\n",
96 myname, prog, bfd_errmsg (bfd_get_error ()));
97 /* Only close if we opened it. */
99 bfd_close (result_bfd);
104 start_time = time (NULL);
106 found_loadable_section = 0;
107 for (s = result_bfd->sections; s; s = s->next)
109 if (s->flags & SEC_LOAD)
113 size = bfd_get_section_size_before_reloc (s);
119 buffer = malloc (size);
123 "%s: insufficient memory to load \"%s\"\n",
125 /* Only close if we opened it. */
126 if (prog_bfd == NULL)
127 bfd_close (result_bfd);
131 lma = bfd_section_lma (result_bfd, s);
133 lma = bfd_section_vma (result_bfd, s);
136 xprintf (callback, "Loading section %s, size 0x%lx %s ",
137 bfd_get_section_name (result_bfd, s),
138 (unsigned long) size,
139 (lma_p ? "lma" : "vma"));
140 xprintf_bfd_vma (callback, lma);
141 xprintf (callback, "\n");
144 bfd_get_section_contents (result_bfd, s, buffer, 0, size);
145 do_write (sd, lma, buffer, size);
146 found_loadable_section = 1;
152 if (!found_loadable_section)
155 "%s: no loadable sections \"%s\"\n",
162 end_time = time (NULL);
163 xprintf (callback, "Start address ");
164 xprintf_bfd_vma (callback, bfd_get_start_address (result_bfd));
165 xprintf (callback, "\n");
166 report_transfer_performance (callback, data_count, start_time, end_time);
169 bfd_cache_close (result_bfd);
175 xprintf VPARAMS ((host_callback *callback, const char *fmt, ...))
177 #ifndef ANSI_PROTOTYPES
178 host_callback *callback;
184 #ifndef ANSI_PROTOTYPES
185 callback = va_arg (ap, host_callback *);
186 fmt = va_arg (ap, char *);
189 (*callback->vprintf_filtered) (callback, fmt, ap);
195 eprintf VPARAMS ((host_callback *callback, const char *fmt, ...))
197 #ifndef ANSI_PROTOTYPES
198 host_callback *callback;
204 #ifndef ANSI_PROTOTYPES
205 callback = va_arg (ap, host_callback *);
206 fmt = va_arg (ap, char *);
209 (*callback->evprintf_filtered) (callback, fmt, ap);
214 /* Report how fast the transfer went. */
217 report_transfer_performance (callback, data_count, start_time, end_time)
218 host_callback *callback;
219 unsigned long data_count;
220 time_t start_time, end_time;
222 xprintf (callback, "Transfer rate: ");
223 if (end_time != start_time)
224 xprintf (callback, "%ld bits/sec",
225 (data_count * 8) / (end_time - start_time));
227 xprintf (callback, "%ld bits in <1 sec", (data_count * 8));
228 xprintf (callback, ".\n");
232 This is intended to handle the vagaries of 32 vs 64 bits, etc. */
235 xprintf_bfd_vma (callback, vma)
236 host_callback *callback;
240 xprintf (callback, "0x%lx", (unsigned long) vma);