1 |
/* |
2 |
* Copyright (C) 2003-2007 Anders Gavare. All rights reserved. |
3 |
* |
4 |
* Redistribution and use in source and binary forms, with or without |
5 |
* modification, are permitted provided that the following conditions are met: |
6 |
* |
7 |
* 1. Redistributions of source code must retain the above copyright |
8 |
* notice, this list of conditions and the following disclaimer. |
9 |
* 2. Redistributions in binary form must reproduce the above copyright |
10 |
* notice, this list of conditions and the following disclaimer in the |
11 |
* documentation and/or other materials provided with the distribution. |
12 |
* 3. The name of the author may not be used to endorse or promote products |
13 |
* derived from this software without specific prior written permission. |
14 |
* |
15 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
16 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
17 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
18 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
19 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
20 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
21 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
22 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
23 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
24 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
25 |
* SUCH DAMAGE. |
26 |
* |
27 |
* |
28 |
* $Id: file.c,v 1.5 2007/06/23 23:59:14 debug Exp $ |
29 |
* |
30 |
* This module contains functions which load executable images into (emulated) |
31 |
* memory. File formats recognized so far are: |
32 |
* |
33 |
* a.out traditional old-style Unix binary format |
34 |
* Mach-O MacOS X format, etc. |
35 |
* ecoff old format used by Ultrix, Windows NT, etc |
36 |
* srec Motorola SREC format |
37 |
* raw raw binaries, "address:[skiplen:[entrypoint:]]filename" |
38 |
* ELF 32-bit and 64-bit ELFs |
39 |
* |
40 |
* If a file is not of one of the above mentioned formats, it is assumed |
41 |
* to be symbol data generated by 'nm' or 'nm -S'. |
42 |
*/ |
43 |
|
44 |
#include <stdio.h> |
45 |
#include <stdlib.h> |
46 |
#include <string.h> |
47 |
#include <sys/types.h> |
48 |
|
49 |
#include "cpu.h" |
50 |
#include "machine.h" |
51 |
#include "memory.h" |
52 |
#include "misc.h" |
53 |
#include "symbol.h" |
54 |
|
55 |
|
56 |
extern int quiet_mode; |
57 |
extern int verbose; |
58 |
|
59 |
|
60 |
/* |
61 |
* This should be increased by every routine here that actually loads an |
62 |
* executable file into memory. (For example, loading a symbol file should |
63 |
* NOT increase this.) |
64 |
*/ |
65 |
static int n_executables_loaded = 0; |
66 |
|
67 |
|
68 |
#include "exec_elf.h" /* Ugly; needed for ELFDATA2LSB etc. */ |
69 |
|
70 |
#define unencode(var,dataptr,typ) { \ |
71 |
int Wi; unsigned char Wb; \ |
72 |
unsigned char *Wp = (unsigned char *) dataptr; \ |
73 |
int Wlen = sizeof(typ); \ |
74 |
var = 0; \ |
75 |
for (Wi=0; Wi<Wlen; Wi++) { \ |
76 |
if (encoding == ELFDATA2LSB) \ |
77 |
Wb = Wp[Wlen-1 - Wi]; \ |
78 |
else \ |
79 |
Wb = Wp[Wi]; \ |
80 |
if (Wi == 0 && (Wb & 0x80)) { \ |
81 |
var --; /* set var to -1 :-) */ \ |
82 |
var <<= 8; \ |
83 |
} \ |
84 |
var |= Wb; \ |
85 |
if (Wi < Wlen-1) \ |
86 |
var <<= 8; \ |
87 |
} \ |
88 |
} |
89 |
|
90 |
|
91 |
#include "file_aout.c" |
92 |
#include "file_ecoff.c" |
93 |
#include "file_elf.c" |
94 |
#include "file_macho.c" |
95 |
#include "file_raw.c" |
96 |
#include "file_srec.c" |
97 |
|
98 |
|
99 |
/* |
100 |
* file_n_executables_loaded(): |
101 |
* |
102 |
* Returns the number of executable files loaded into emulated memory. |
103 |
*/ |
104 |
int file_n_executables_loaded(void) |
105 |
{ |
106 |
return n_executables_loaded; |
107 |
} |
108 |
|
109 |
|
110 |
/* |
111 |
* file_load(): |
112 |
* |
113 |
* Sense the file format of a file (ELF, a.out, ecoff), and call the |
114 |
* right file_load_XXX() function. If the file isn't of a recognized |
115 |
* binary format, assume that it contains symbol definitions. |
116 |
* |
117 |
* If the filename doesn't exist, try to treat the name as |
118 |
* "address:filename" and load the file as a raw binary. |
119 |
*/ |
120 |
void file_load(struct machine *machine, struct memory *mem, |
121 |
char *filename, uint64_t *entrypointp, |
122 |
int arch, uint64_t *gpp, int *byte_orderp, uint64_t *tocp) |
123 |
{ |
124 |
int iadd = DEBUG_INDENTATION, old_quiet_mode; |
125 |
FILE *f; |
126 |
char *tmpdir = getenv("TMPDIR") == NULL? |
127 |
DEFAULT_TMP_DIR : getenv("TMPDIR"); |
128 |
unsigned char buf[12]; |
129 |
unsigned char buf2[2]; |
130 |
char tmpname[400]; |
131 |
size_t len, len2, i; |
132 |
off_t size; |
133 |
|
134 |
if (byte_orderp == NULL) { |
135 |
fprintf(stderr, "file_load(): byte_order == NULL\n"); |
136 |
exit(1); |
137 |
} |
138 |
|
139 |
if (arch == ARCH_NOARCH) { |
140 |
fprintf(stderr, "file_load(): FATAL ERROR: no arch?\n"); |
141 |
exit(1); |
142 |
} |
143 |
|
144 |
if (mem == NULL || filename == NULL) { |
145 |
fprintf(stderr, "file_load(): mem or filename is NULL\n"); |
146 |
exit(1); |
147 |
} |
148 |
|
149 |
/* Skip configuration files: */ |
150 |
if (filename[0] == '@') |
151 |
return; |
152 |
|
153 |
debug("loading %s%s\n", filename, verbose >= 2? ":" : ""); |
154 |
debug_indentation(iadd); |
155 |
|
156 |
old_quiet_mode = quiet_mode; |
157 |
if (verbose < 2) |
158 |
quiet_mode = 1; |
159 |
|
160 |
f = fopen(filename, "r"); |
161 |
if (f == NULL) { |
162 |
file_load_raw(machine, mem, filename, entrypointp); |
163 |
goto ret; |
164 |
} |
165 |
|
166 |
fseek(f, 0, SEEK_END); |
167 |
size = ftello(f); |
168 |
fseek(f, 0, SEEK_SET); |
169 |
|
170 |
memset(buf, 0, sizeof(buf)); |
171 |
len = fread(buf, 1, sizeof(buf), f); |
172 |
fseek(f, 510, SEEK_SET); |
173 |
len2 = fread(buf2, 1, sizeof(buf2), f); |
174 |
fclose(f); |
175 |
|
176 |
if (len < (signed int)sizeof(buf)) { |
177 |
fprintf(stderr, "\nThis file is too small to contain " |
178 |
"anything useful\n"); |
179 |
exit(1); |
180 |
} |
181 |
|
182 |
/* Is it an ELF? */ |
183 |
if (buf[0] == 0x7f && buf[1]=='E' && buf[2]=='L' && buf[3]=='F') { |
184 |
file_load_elf(machine, mem, filename, |
185 |
entrypointp, arch, gpp, byte_orderp, tocp); |
186 |
goto ret; |
187 |
} |
188 |
|
189 |
/* Is it an a.out? */ |
190 |
if (buf[0]==0x00 && buf[1]==0x8b && buf[2]==0x01 && buf[3]==0x07) { |
191 |
/* MIPS a.out */ |
192 |
file_load_aout(machine, mem, filename, 0, |
193 |
entrypointp, arch, byte_orderp); |
194 |
goto ret; |
195 |
} |
196 |
if (buf[0]==0x00 && buf[1]==0x87 && buf[2]==0x01 && buf[3]==0x08) { |
197 |
/* M68K a.out */ |
198 |
file_load_aout(machine, mem, filename, |
199 |
AOUT_FLAG_VADDR_ZERO_HACK /* for OpenBSD/mac68k */, |
200 |
entrypointp, arch, byte_orderp); |
201 |
goto ret; |
202 |
} |
203 |
if (buf[0]==0x00 && buf[1]==0x99 && buf[2]==0x01 && buf[3]==0x0b) { |
204 |
/* OpenBSD/M88K a.out */ |
205 |
file_load_aout(machine, mem, filename, AOUT_FLAG_FROM_BEGINNING, |
206 |
entrypointp, arch, byte_orderp); |
207 |
goto ret; |
208 |
} |
209 |
if (buf[0]==0x00 && buf[1]==0x8f && buf[2]==0x01 && buf[3]==0x0b) { |
210 |
/* ARM a.out */ |
211 |
file_load_aout(machine, mem, filename, AOUT_FLAG_FROM_BEGINNING, |
212 |
entrypointp, arch, byte_orderp); |
213 |
goto ret; |
214 |
} |
215 |
if (buf[0]==0x00 && buf[1]==0x86 && buf[2]==0x01 && buf[3]==0x0b) { |
216 |
/* i386 a.out (old OpenBSD and NetBSD etc) */ |
217 |
file_load_aout(machine, mem, filename, AOUT_FLAG_FROM_BEGINNING, |
218 |
entrypointp, arch, byte_orderp); |
219 |
goto ret; |
220 |
} |
221 |
if (buf[0]==0x01 && buf[1]==0x03 && buf[2]==0x01 && buf[3]==0x07) { |
222 |
/* SPARC a.out (old 32-bit NetBSD etc) */ |
223 |
file_load_aout(machine, mem, filename, AOUT_FLAG_NO_SIZES, |
224 |
entrypointp, arch, byte_orderp); |
225 |
goto ret; |
226 |
} |
227 |
if (buf[0]==0x00 && buf[2]==0x00 && buf[8]==0x7a && buf[9]==0x75) { |
228 |
/* DEC OSF1 on MIPS: */ |
229 |
file_load_aout(machine, mem, filename, AOUT_FLAG_DECOSF1, |
230 |
entrypointp, arch, byte_orderp); |
231 |
goto ret; |
232 |
} |
233 |
|
234 |
/* |
235 |
* Is it a Mach-O file? |
236 |
*/ |
237 |
if (buf[0] == 0xfe && buf[1] == 0xed && buf[2] == 0xfa && |
238 |
(buf[3] == 0xce || buf[3] == 0xcf)) { |
239 |
file_load_macho(machine, mem, filename, entrypointp, |
240 |
arch, byte_orderp, buf[3] == 0xcf, 0); |
241 |
goto ret; |
242 |
} |
243 |
if ((buf[0] == 0xce || buf[0] == 0xcf) && buf[1] == 0xfa && |
244 |
buf[2] == 0xed && buf[3] == 0xfe) { |
245 |
file_load_macho(machine, mem, filename, entrypointp, |
246 |
arch, byte_orderp, buf[0] == 0xcf, 1); |
247 |
goto ret; |
248 |
} |
249 |
|
250 |
/* |
251 |
* Is it an ecoff? |
252 |
* |
253 |
* TODO: What's the deal with the magic value's byte order? Sometimes |
254 |
* it seems to be reversed for BE when compared to LE, but not always? |
255 |
*/ |
256 |
if (buf[0]+256*buf[1] == ECOFF_MAGIC_MIPSEB || |
257 |
buf[0]+256*buf[1] == ECOFF_MAGIC_MIPSEL || |
258 |
buf[0]+256*buf[1] == ECOFF_MAGIC_MIPSEB2 || |
259 |
buf[0]+256*buf[1] == ECOFF_MAGIC_MIPSEL2 || |
260 |
buf[0]+256*buf[1] == ECOFF_MAGIC_MIPSEB3 || |
261 |
buf[0]+256*buf[1] == ECOFF_MAGIC_MIPSEL3 || |
262 |
buf[1]+256*buf[0] == ECOFF_MAGIC_MIPSEB || |
263 |
buf[1]+256*buf[0] == ECOFF_MAGIC_MIPSEL || |
264 |
buf[1]+256*buf[0] == ECOFF_MAGIC_MIPSEB2 || |
265 |
buf[1]+256*buf[0] == ECOFF_MAGIC_MIPSEL2 || |
266 |
buf[1]+256*buf[0] == ECOFF_MAGIC_MIPSEB3 || |
267 |
buf[1]+256*buf[0] == ECOFF_MAGIC_MIPSEL3) { |
268 |
file_load_ecoff(machine, mem, filename, entrypointp, |
269 |
arch, gpp, byte_orderp); |
270 |
goto ret; |
271 |
} |
272 |
|
273 |
/* Is it a Motorola SREC file? */ |
274 |
if ((buf[0]=='S' && buf[1]>='0' && buf[1]<='9')) { |
275 |
file_load_srec(machine, mem, filename, entrypointp); |
276 |
goto ret; |
277 |
} |
278 |
|
279 |
/* gzipped files are not supported: */ |
280 |
if (buf[0]==0x1f && buf[1]==0x8b) { |
281 |
fprintf(stderr, "\nYou need to gunzip the file before you" |
282 |
" try to use it.\n"); |
283 |
exit(1); |
284 |
} |
285 |
|
286 |
if (size > 24000000) { |
287 |
fprintf(stderr, "\nThis file is very large (%lli bytes)\n", |
288 |
(long long)size); |
289 |
fprintf(stderr, "Are you sure it is a kernel and not a disk " |
290 |
"image? (Use the -d option.)\n"); |
291 |
exit(1); |
292 |
} |
293 |
|
294 |
if (size == 1474560) |
295 |
fprintf(stderr, "Hm... this file is the size of a 1.44 MB " |
296 |
"floppy image. Maybe you forgot the\n-d switch?\n"); |
297 |
|
298 |
/* |
299 |
* Ugly hack for Dreamcast: When booting from a Dreamcast CDROM |
300 |
* image, a temporary file is extracted into /tmp/gxemul.*, but this |
301 |
* is a "scrambled" raw binary. This code unscrambles it, and loads |
302 |
* it as a raw binary. |
303 |
*/ |
304 |
snprintf(tmpname, sizeof(tmpname), "%s/gxemul.", tmpdir); |
305 |
if (machine->machine_type == MACHINE_DREAMCAST && |
306 |
strncmp(filename, tmpname, strlen(tmpname)) == 0) { |
307 |
char *tmp_filename = malloc(strlen(filename) + 100); |
308 |
snprintf(tmp_filename, strlen(filename) + 100, |
309 |
"%s.descrambled", filename); |
310 |
debug("descrambling into %s\n", tmp_filename); |
311 |
dreamcast_descramble(filename, tmp_filename); |
312 |
|
313 |
snprintf(tmp_filename, strlen(filename) + 100, |
314 |
"0x8c010000:%s.descrambled", filename); |
315 |
debug("loading descrambled Dreamcast binary\n"); |
316 |
file_load_raw(machine, mem, tmp_filename, entrypointp); |
317 |
|
318 |
snprintf(tmp_filename, strlen(filename) + 100, |
319 |
"%s.descrambled", filename); |
320 |
remove(tmp_filename); |
321 |
free(tmp_filename); |
322 |
|
323 |
/* Hack: Start a "boot from CDROM" sequence: */ |
324 |
*entrypointp = 0x8c000080; |
325 |
goto ret; |
326 |
} |
327 |
|
328 |
/* |
329 |
* Last resort: symbol definitions from nm (or nm -S): |
330 |
* |
331 |
* If the buf contains typical 'binary' characters, then print |
332 |
* an error message and quit instead of assuming that it is a |
333 |
* symbol file. |
334 |
*/ |
335 |
for (i=0; i<(signed)sizeof(buf); i++) |
336 |
if (buf[i] < 32 && buf[i] != '\t' && |
337 |
buf[i] != '\n' && buf[i] != '\r' && |
338 |
buf[i] != '\f') { |
339 |
fprintf(stderr, "\nThe file format of '%s' is " |
340 |
"unknown.\n\n ", filename); |
341 |
for (i=0; i<(signed)sizeof(buf); i++) |
342 |
fprintf(stderr, " %02x", buf[i]); |
343 |
|
344 |
if (len2 == 2 && buf2[0] == 0x55 && buf2[1] == 0xaa) |
345 |
fprintf(stderr, "\n\nIt has a PC-style " |
346 |
"bootsector marker."); |
347 |
|
348 |
fprintf(stderr, "\n\nPossible explanations:\n\n" |
349 |
" o) If this is a disk image, you forgot '-d' " |
350 |
"on the command line.\n" |
351 |
" o) You are attempting to load a raw binary " |
352 |
"into emulated memory,\n" |
353 |
" but forgot to add the address prefix.\n" |
354 |
" o) This is an unsupported binary format.\n\n"); |
355 |
exit(1); |
356 |
} |
357 |
|
358 |
symbol_readfile(&machine->symbol_context, filename); |
359 |
|
360 |
ret: |
361 |
debug_indentation(-iadd); |
362 |
quiet_mode = old_quiet_mode; |
363 |
} |
364 |
|