| 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 | bfd_cache_close (result_bfd); |
| 170 | |
| 171 | return result_bfd; |
| 172 | } |
| 173 | |
| 174 | static void |
| 175 | xprintf VPARAMS ((host_callback *callback, const char *fmt, ...)) |
| 176 | { |
| 177 | #ifndef ANSI_PROTOTYPES |
| 178 | host_callback *callback; |
| 179 | char *fmt; |
| 180 | #endif |
| 181 | va_list ap; |
| 182 | |
| 183 | VA_START (ap, fmt); |
| 184 | #ifndef ANSI_PROTOTYPES |
| 185 | callback = va_arg (ap, host_callback *); |
| 186 | fmt = va_arg (ap, char *); |
| 187 | #endif |
| 188 | |
| 189 | (*callback->vprintf_filtered) (callback, fmt, ap); |
| 190 | |
| 191 | va_end (ap); |
| 192 | } |
| 193 | |
| 194 | static void |
| 195 | eprintf VPARAMS ((host_callback *callback, const char *fmt, ...)) |
| 196 | { |
| 197 | #ifndef ANSI_PROTOTYPES |
| 198 | host_callback *callback; |
| 199 | char *fmt; |
| 200 | #endif |
| 201 | va_list ap; |
| 202 | |
| 203 | VA_START (ap, fmt); |
| 204 | #ifndef ANSI_PROTOTYPES |
| 205 | callback = va_arg (ap, host_callback *); |
| 206 | fmt = va_arg (ap, char *); |
| 207 | #endif |
| 208 | |
| 209 | (*callback->evprintf_filtered) (callback, fmt, ap); |
| 210 | |
| 211 | va_end (ap); |
| 212 | } |
| 213 | |
| 214 | /* Report how fast the transfer went. */ |
| 215 | |
| 216 | static void |
| 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; |
| 221 | { |
| 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)); |
| 226 | else |
| 227 | xprintf (callback, "%ld bits in <1 sec", (data_count * 8)); |
| 228 | xprintf (callback, ".\n"); |
| 229 | } |
| 230 | |
| 231 | /* Print a bfd_vma. |
| 232 | This is intended to handle the vagaries of 32 vs 64 bits, etc. */ |
| 233 | |
| 234 | static void |
| 235 | xprintf_bfd_vma (callback, vma) |
| 236 | host_callback *callback; |
| 237 | bfd_vma vma; |
| 238 | { |
| 239 | /* FIXME: for now */ |
| 240 | xprintf (callback, "0x%lx", (unsigned long) vma); |
| 241 | } |