]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - sim/common/sim-load.c
Initial creation of sourceware repository
[thirdparty/binutils-gdb.git] / sim / common / sim-load.c
1 /* Utility to load a file into the simulator.
2 Copyright (C) 1997 Free Software Foundation, Inc.
3
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)
7 any later version.
8
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.
13
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. */
17
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 :-)]. */
21
22 #include "config.h"
23 #include "ansidecl.h"
24 #include <stdio.h> /* for NULL */
25 #ifdef ANSI_PROTOTYPES
26 #include <stdarg.h>
27 #else
28 #include <varargs.h>
29 #endif
30 #ifdef HAVE_STDLIB_H
31 #include <stdlib.h>
32 #endif
33 #include <time.h>
34
35 #include "sim-basics.h"
36 #include "bfd.h"
37 #include "sim-utils.h"
38
39 #include "callback.h"
40 #include "remote-sim.h"
41
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));
47
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
53 rather than the VMA
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. */
59
60
61 bfd *
62 sim_load_file (sd, myname, callback, prog, prog_bfd, verbose_p, lma_p, do_write)
63 SIM_DESC sd;
64 const char *myname;
65 host_callback *callback;
66 char *prog;
67 bfd *prog_bfd;
68 int verbose_p;
69 int lma_p;
70 sim_write_fn do_write;
71 {
72 asection *s;
73 /* Record separately as we don't want to close PROG_BFD if it was passed. */
74 bfd *result_bfd;
75 time_t start_time = 0; /* Start and end times of download */
76 time_t end_time = 0;
77 unsigned long data_count = 0; /* Number of bytes transferred to memory */
78 int found_loadable_section;
79
80 if (prog_bfd != NULL)
81 result_bfd = prog_bfd;
82 else
83 {
84 result_bfd = bfd_openr (prog, 0);
85 if (result_bfd == NULL)
86 {
87 eprintf (callback, "%s: can't open \"%s\": %s\n",
88 myname, prog, bfd_errmsg (bfd_get_error ()));
89 return NULL;
90 }
91 }
92
93 if (!bfd_check_format (result_bfd, bfd_object))
94 {
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. */
98 if (prog_bfd == NULL)
99 bfd_close (result_bfd);
100 return NULL;
101 }
102
103 if (verbose_p)
104 start_time = time (NULL);
105
106 found_loadable_section = 0;
107 for (s = result_bfd->sections; s; s = s->next)
108 {
109 if (s->flags & SEC_LOAD)
110 {
111 bfd_size_type size;
112
113 size = bfd_get_section_size_before_reloc (s);
114 if (size > 0)
115 {
116 char *buffer;
117 bfd_vma lma;
118
119 buffer = malloc (size);
120 if (buffer == NULL)
121 {
122 eprintf (callback,
123 "%s: insufficient memory to load \"%s\"\n",
124 myname, prog);
125 /* Only close if we opened it. */
126 if (prog_bfd == NULL)
127 bfd_close (result_bfd);
128 return NULL;
129 }
130 if (lma_p)
131 lma = bfd_section_lma (result_bfd, s);
132 else
133 lma = bfd_section_vma (result_bfd, s);
134 if (verbose_p)
135 {
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");
142 }
143 data_count += size;
144 bfd_get_section_contents (result_bfd, s, buffer, 0, size);
145 do_write (sd, lma, buffer, size);
146 found_loadable_section = 1;
147 free (buffer);
148 }
149 }
150 }
151
152 if (!found_loadable_section)
153 {
154 eprintf (callback,
155 "%s: no loadable sections \"%s\"\n",
156 myname, prog);
157 return NULL;
158 }
159
160 if (verbose_p)
161 {
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);
167 }
168
169 return result_bfd;
170 }
171
172 static void
173 xprintf VPARAMS ((host_callback *callback, const char *fmt, ...))
174 {
175 #ifndef ANSI_PROTOTYPES
176 host_callback *callback;
177 char *fmt;
178 #endif
179 va_list ap;
180
181 VA_START (ap, fmt);
182 #ifndef ANSI_PROTOTYPES
183 callback = va_arg (ap, host_callback *);
184 fmt = va_arg (ap, char *);
185 #endif
186
187 (*callback->vprintf_filtered) (callback, fmt, ap);
188
189 va_end (ap);
190 }
191
192 static void
193 eprintf VPARAMS ((host_callback *callback, const char *fmt, ...))
194 {
195 #ifndef ANSI_PROTOTYPES
196 host_callback *callback;
197 char *fmt;
198 #endif
199 va_list ap;
200
201 VA_START (ap, fmt);
202 #ifndef ANSI_PROTOTYPES
203 callback = va_arg (ap, host_callback *);
204 fmt = va_arg (ap, char *);
205 #endif
206
207 (*callback->evprintf_filtered) (callback, fmt, ap);
208
209 va_end (ap);
210 }
211
212 /* Report how fast the transfer went. */
213
214 static void
215 report_transfer_performance (callback, data_count, start_time, end_time)
216 host_callback *callback;
217 unsigned long data_count;
218 time_t start_time, end_time;
219 {
220 xprintf (callback, "Transfer rate: ");
221 if (end_time != start_time)
222 xprintf (callback, "%ld bits/sec",
223 (data_count * 8) / (end_time - start_time));
224 else
225 xprintf (callback, "%ld bits in <1 sec", (data_count * 8));
226 xprintf (callback, ".\n");
227 }
228
229 /* Print a bfd_vma.
230 This is intended to handle the vagaries of 32 vs 64 bits, etc. */
231
232 static void
233 xprintf_bfd_vma (callback, vma)
234 host_callback *callback;
235 bfd_vma vma;
236 {
237 /* FIXME: for now */
238 xprintf (callback, "0x%lx", (unsigned long) vma);
239 }