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 #ifdef ANSI_PROTOTYPES
35 #include "remote-sim.h"
37 static void eprintf
PARAMS ((host_callback
*, const char *, ...));
38 static void xprintf
PARAMS ((host_callback
*, const char *, ...));
39 static void report_transfer_performance
40 PARAMS ((host_callback
*, unsigned long, time_t, time_t));
41 static void xprintf_bfd_vma
PARAMS ((host_callback
*, bfd_vma
));
43 /* Load program PROG into the simulator.
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 this fails an error message is printed and NULL is returned.
48 If it succeeds the bfd is returned. */
50 /* FIXME: Where can we put a prototype for this? */
53 sim_load_file (sd
, myname
, callback
, prog
, prog_bfd
, verbose_p
)
56 host_callback
*callback
;
61 /* Record separately as we don't want to close PROG_BFD if it was passed. */
63 time_t start_time
, end_time
; /* Start and end times of download */
64 unsigned long data_count
= 0; /* Number of bytes transferred to memory */
67 result_bfd
= prog_bfd
;
70 result_bfd
= bfd_openr (prog
, 0);
71 if (result_bfd
== NULL
)
73 eprintf (callback
, "%s: can't open \"%s\": %s\n",
74 myname
, prog
, bfd_errmsg (bfd_get_error ()));
79 if (!bfd_check_format (result_bfd
, bfd_object
))
81 eprintf (callback
, "%s: \"%s\" is not an object file: %s\n",
82 myname
, prog
, bfd_errmsg (bfd_get_error ()));
83 /* Only close if we opened it. */
85 bfd_close (result_bfd
);
90 start_time
= time (NULL
);
92 for (s
= result_bfd
->sections
; s
; s
= s
->next
)
94 if (s
->flags
& SEC_LOAD
)
98 size
= bfd_get_section_size_before_reloc (s
);
104 buffer
= malloc (size
);
108 "%s: insufficient memory to load \"%s\"\n",
110 /* Only close if we opened it. */
111 if (prog_bfd
== NULL
)
112 bfd_close (result_bfd
);
115 /* Before you change this to bfd_section_lma, make sure
116 the arm-pe simulator still works. */
117 lma
= bfd_section_vma (result_bfd
, s
);
120 xprintf (callback
, "Loading section %s, size 0x%lx lma ",
121 bfd_get_section_name (result_bfd
, s
),
122 (unsigned long) size
);
123 xprintf_bfd_vma (callback
, lma
);
124 xprintf (callback
, "\n");
127 bfd_get_section_contents (result_bfd
, s
, buffer
, 0, size
);
128 sim_write (sd
, lma
, buffer
, size
);
136 end_time
= time (NULL
);
137 xprintf (callback
, "Start address ");
138 xprintf_bfd_vma (callback
, bfd_get_start_address (result_bfd
));
139 xprintf (callback
, "\n");
140 report_transfer_performance (callback
, data_count
, start_time
, end_time
);
147 xprintf
VPARAMS ((host_callback
*callback
, const char *fmt
, ...))
149 #ifndef ANSI_PROTOTYPES
150 host_callback
*callback
;
156 #ifndef ANSI_PROTOTYPES
157 callback
= va_arg (ap
, host_callback
*);
158 fmt
= va_arg (ap
, char *);
161 (*callback
->vprintf_filtered
) (callback
, fmt
, ap
);
167 eprintf
VPARAMS ((host_callback
*callback
, const char *fmt
, ...))
169 #ifndef ANSI_PROTOTYPES
170 host_callback
*callback
;
176 #ifndef ANSI_PROTOTYPES
177 callback
= va_arg (ap
, host_callback
*);
178 fmt
= va_arg (ap
, char *);
181 (*callback
->evprintf_filtered
) (callback
, fmt
, ap
);
186 /* Report how fast the transfer went. */
189 report_transfer_performance (callback
, data_count
, start_time
, end_time
)
190 host_callback
*callback
;
191 unsigned long data_count
;
192 time_t start_time
, end_time
;
194 xprintf (callback
, "Transfer rate: ");
195 if (end_time
!= start_time
)
196 xprintf (callback
, "%ld bits/sec",
197 (data_count
* 8) / (end_time
- start_time
));
199 xprintf (callback
, "%ld bits in <1 sec", (data_count
* 8));
200 xprintf (callback
, ".\n");
204 This is intended to handle the vagaries of 32 vs 64 bits, etc. */
207 xprintf_bfd_vma (callback
, vma
)
208 host_callback
*callback
;
212 xprintf (callback
, "0x%lx", (unsigned long) vma
);