1 /* Utility to load a file into the simulator.
2 Copyright (C) 1997-2021 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 */
30 #include "sim-basics.h"
32 #include "sim-utils.h"
34 #include "gdb/callback.h"
35 #include "gdb/remote-sim.h"
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
);
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
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. */
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
)
62 /* Record separately as we don't want to close PROG_BFD if it was passed. */
64 time_t start_time
= 0; /* Start and end times of download */
66 unsigned long data_count
= 0; /* Number of bytes transferred to memory */
67 int found_loadable_section
;
70 result_bfd
= prog_bfd
;
73 result_bfd
= bfd_openr (prog
, 0);
74 if (result_bfd
== NULL
)
76 eprintf (callback
, "%s: can't open \"%s\": %s\n",
77 myname
, prog
, bfd_errmsg (bfd_get_error ()));
82 if (!bfd_check_format (result_bfd
, bfd_object
))
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. */
88 bfd_close (result_bfd
);
93 start_time
= time (NULL
);
95 found_loadable_section
= 0;
96 for (s
= result_bfd
->sections
; s
; s
= s
->next
)
98 if (s
->flags
& SEC_LOAD
)
102 size
= bfd_section_size (s
);
105 unsigned char *buffer
;
108 buffer
= malloc (size
);
112 "%s: insufficient memory to load \"%s\"\n",
114 /* Only close if we opened it. */
115 if (prog_bfd
== NULL
)
116 bfd_close (result_bfd
);
120 lma
= bfd_section_lma (s
);
122 lma
= bfd_section_vma (s
);
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");
133 bfd_get_section_contents (result_bfd
, s
, buffer
, 0, size
);
134 do_write (sd
, lma
, buffer
, size
);
135 found_loadable_section
= 1;
141 if (!found_loadable_section
)
144 "%s: no loadable sections \"%s\"\n",
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
);
158 bfd_cache_close (result_bfd
);
164 xprintf (host_callback
*callback
, const char *fmt
, ...)
170 (*callback
->vprintf_filtered
) (callback
, fmt
, ap
);
176 eprintf (host_callback
*callback
, const char *fmt
, ...)
182 (*callback
->evprintf_filtered
) (callback
, fmt
, ap
);
187 /* Report how fast the transfer went. */
190 report_transfer_performance (host_callback
*callback
, unsigned long data_count
,
191 time_t start_time
, time_t end_time
)
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
));
198 xprintf (callback
, "%ld bits in <1 sec", (data_count
* 8));
199 xprintf (callback
, ".\n");
203 This is intended to handle the vagaries of 32 vs 64 bits, etc. */
206 xprintf_bfd_vma (host_callback
*callback
, bfd_vma vma
)
209 xprintf (callback
, "0x%lx", (unsigned long) vma
);