]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - sim/common/sim-load.c
Update years in copyright notice for the GDB files.
[thirdparty/binutils-gdb.git] / sim / common / sim-load.c
CommitLineData
c906108c 1/* Utility to load a file into the simulator.
8acc9f48 2 Copyright (C) 1997-2013 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
41ee5402
DJ
21#ifdef HAVE_CONFIG_H
22#include "cconfig.h"
23#endif
c906108c
SS
24#include "ansidecl.h"
25#include <stdio.h> /* for NULL */
c906108c 26#include <stdarg.h>
c906108c
SS
27#ifdef HAVE_STDLIB_H
28#include <stdlib.h>
29#endif
30#include <time.h>
31
32#include "sim-basics.h"
33#include "bfd.h"
34#include "sim-utils.h"
35
3c25f8c7
AC
36#include "gdb/callback.h"
37#include "gdb/remote-sim.h"
c906108c
SS
38
39static void eprintf PARAMS ((host_callback *, const char *, ...));
40static void xprintf PARAMS ((host_callback *, const char *, ...));
41static void report_transfer_performance
42 PARAMS ((host_callback *, unsigned long, time_t, time_t));
43static void xprintf_bfd_vma PARAMS ((host_callback *, bfd_vma));
44
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
50 rather than the VMA
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. */
56
57
58bfd *
59sim_load_file (sd, myname, callback, prog, prog_bfd, verbose_p, lma_p, do_write)
60 SIM_DESC sd;
61 const char *myname;
62 host_callback *callback;
63 char *prog;
64 bfd *prog_bfd;
65 int verbose_p;
66 int lma_p;
67 sim_write_fn do_write;
68{
69 asection *s;
70 /* Record separately as we don't want to close PROG_BFD if it was passed. */
71 bfd *result_bfd;
72 time_t start_time = 0; /* Start and end times of download */
73 time_t end_time = 0;
74 unsigned long data_count = 0; /* Number of bytes transferred to memory */
75 int found_loadable_section;
76
77 if (prog_bfd != NULL)
78 result_bfd = prog_bfd;
79 else
80 {
81 result_bfd = bfd_openr (prog, 0);
82 if (result_bfd == NULL)
83 {
028f6515 84 eprintf (callback, "%s: can't open \"%s\": %s\n",
c906108c
SS
85 myname, prog, bfd_errmsg (bfd_get_error ()));
86 return NULL;
87 }
88 }
89
028f6515 90 if (!bfd_check_format (result_bfd, bfd_object))
c906108c
SS
91 {
92 eprintf (callback, "%s: \"%s\" is not an object file: %s\n",
93 myname, prog, bfd_errmsg (bfd_get_error ()));
94 /* Only close if we opened it. */
95 if (prog_bfd == NULL)
96 bfd_close (result_bfd);
97 return NULL;
98 }
99
100 if (verbose_p)
101 start_time = time (NULL);
102
103 found_loadable_section = 0;
028f6515 104 for (s = result_bfd->sections; s; s = s->next)
c906108c 105 {
028f6515 106 if (s->flags & SEC_LOAD)
c906108c
SS
107 {
108 bfd_size_type size;
109
5f510f9c 110 size = bfd_get_section_size (s);
c906108c
SS
111 if (size > 0)
112 {
cc25892b 113 unsigned char *buffer;
c906108c
SS
114 bfd_vma lma;
115
116 buffer = malloc (size);
117 if (buffer == NULL)
118 {
119 eprintf (callback,
120 "%s: insufficient memory to load \"%s\"\n",
121 myname, prog);
122 /* Only close if we opened it. */
123 if (prog_bfd == NULL)
124 bfd_close (result_bfd);
125 return NULL;
126 }
127 if (lma_p)
128 lma = bfd_section_lma (result_bfd, s);
129 else
130 lma = bfd_section_vma (result_bfd, s);
131 if (verbose_p)
132 {
133 xprintf (callback, "Loading section %s, size 0x%lx %s ",
134 bfd_get_section_name (result_bfd, s),
135 (unsigned long) size,
136 (lma_p ? "lma" : "vma"));
137 xprintf_bfd_vma (callback, lma);
138 xprintf (callback, "\n");
139 }
140 data_count += size;
141 bfd_get_section_contents (result_bfd, s, buffer, 0, size);
142 do_write (sd, lma, buffer, size);
143 found_loadable_section = 1;
144 free (buffer);
145 }
146 }
147 }
148
149 if (!found_loadable_section)
150 {
151 eprintf (callback,
152 "%s: no loadable sections \"%s\"\n",
153 myname, prog);
154 return NULL;
155 }
156
157 if (verbose_p)
158 {
159 end_time = time (NULL);
160 xprintf (callback, "Start address ");
161 xprintf_bfd_vma (callback, bfd_get_start_address (result_bfd));
162 xprintf (callback, "\n");
163 report_transfer_performance (callback, data_count, start_time, end_time);
164 }
165
2836ee25
FCE
166 bfd_cache_close (result_bfd);
167
c906108c
SS
168 return result_bfd;
169}
170
171static void
172xprintf VPARAMS ((host_callback *callback, const char *fmt, ...))
173{
c906108c
SS
174 va_list ap;
175
176 VA_START (ap, fmt);
c906108c
SS
177
178 (*callback->vprintf_filtered) (callback, fmt, ap);
179
180 va_end (ap);
181}
182
183static void
184eprintf VPARAMS ((host_callback *callback, const char *fmt, ...))
185{
c906108c
SS
186 va_list ap;
187
188 VA_START (ap, fmt);
c906108c
SS
189
190 (*callback->evprintf_filtered) (callback, fmt, ap);
191
192 va_end (ap);
193}
194
195/* Report how fast the transfer went. */
196
197static void
198report_transfer_performance (callback, data_count, start_time, end_time)
199 host_callback *callback;
200 unsigned long data_count;
201 time_t start_time, end_time;
202{
203 xprintf (callback, "Transfer rate: ");
204 if (end_time != start_time)
205 xprintf (callback, "%ld bits/sec",
206 (data_count * 8) / (end_time - start_time));
207 else
208 xprintf (callback, "%ld bits in <1 sec", (data_count * 8));
209 xprintf (callback, ".\n");
210}
211
212/* Print a bfd_vma.
213 This is intended to handle the vagaries of 32 vs 64 bits, etc. */
214
215static void
216xprintf_bfd_vma (callback, vma)
217 host_callback *callback;
218 bfd_vma vma;
219{
220 /* FIXME: for now */
221 xprintf (callback, "0x%lx", (unsigned long) vma);
222}