1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
|
/* $NetBSD: loadfile.c,v 1.10 2000/12/03 02:53:04 tsutsui Exp $ */
/* $OpenBSD: loadfile.c,v 1.14 2007/06/26 10:32:50 tom Exp $ */
/*-
* Copyright (c) 1997 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
* NASA Ames Research Center and by Christos Zoulas.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the NetBSD
* Foundation, Inc. and its contributors.
* 4. Neither the name of The NetBSD Foundation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Copyright (c) 1992, 1993
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Ralph Campbell.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* @(#)boot.c 8.1 (Berkeley) 6/10/93
*/
#ifdef _STANDALONE
#include <lib/libkern/libkern.h>
#include <lib/libsa/stand.h>
#else
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <err.h>
#endif
#include <sys/param.h>
#include <sys/exec.h>
#include "loadfile.h"
#ifdef BOOT_ECOFF
#include <sys/exec_ecoff.h>
static int coff_exec(int, struct ecoff_exechdr *, u_long *, int);
#endif
#ifdef BOOT_AOUT
#include <sys/exec_aout.h>
static int aout_exec(int, struct exec *, u_long *, int);
#endif
#ifdef BOOT_ELF
#include <sys/exec_elf.h>
#if defined(BOOT_ELF32) && defined(BOOT_ELF64)
/*
* Both defined, so elf32_exec() and elf64_exec() need to be separately
* created (can't do it by including loadfile_elf.c here).
*/
int elf32_exec(int, Elf32_Ehdr *, u_long *, int);
int elf64_exec(int, Elf64_Ehdr *, u_long *, int);
#else
#include "loadfile_elf.c"
#endif
#endif
/*
* Open 'filename', read in program and return -1 on error otherwise fd,
* with file still open.
* Also fills in marks.
*/
int
loadfile(const char *fname, u_long *marks, int flags)
{
union {
#ifdef BOOT_ECOFF
struct ecoff_exechdr coff;
#endif
#if defined(BOOT_ELF32) || (defined(BOOT_ELF) && ELFSIZE == 32)
Elf32_Ehdr elf32;
#endif
#if defined(BOOT_ELF64) || (defined(BOOT_ELF) && ELFSIZE == 64)
Elf64_Ehdr elf64;
#endif
#ifdef BOOT_AOUT
struct exec aout;
#endif
} hdr;
ssize_t nr;
int fd, rval;
/* Open the file. */
if ((fd = open(fname, 0)) < 0) {
WARN(("open %s", fname ? fname : "<default>"));
return -1;
}
/* Read the exec header. */
if ((nr = read(fd, &hdr, sizeof(hdr))) != sizeof(hdr)) {
WARN(("read header"));
goto err;
}
#ifdef BOOT_ECOFF
if (!ECOFF_BADMAG(&hdr.coff)) {
rval = coff_exec(fd, &hdr.coff, marks, flags);
} else
#endif
#if defined(BOOT_ELF32) || (defined(BOOT_ELF) && ELFSIZE == 32)
if (memcmp(hdr.elf32.e_ident, ELFMAG, SELFMAG) == 0 &&
hdr.elf32.e_ident[EI_CLASS] == ELFCLASS32) {
rval = elf32_exec(fd, &hdr.elf32, marks, flags);
} else
#endif
#if defined(BOOT_ELF64) || (defined(BOOT_ELF) && ELFSIZE == 64)
if (memcmp(hdr.elf64.e_ident, ELFMAG, SELFMAG) == 0 &&
hdr.elf64.e_ident[EI_CLASS] == ELFCLASS64) {
rval = elf64_exec(fd, &hdr.elf64, marks, flags);
} else
#endif
#ifdef BOOT_AOUT
if (OKMAGIC(N_GETMAGIC(hdr.aout))
#ifndef NO_MID_CHECK
&& N_GETMID(hdr.aout) == MID_MACHINE
#endif
) {
rval = aout_exec(fd, &hdr.aout, marks, flags);
} else
#endif
{
rval = 1;
errno = EFTYPE;
WARN(("%s", fname ? fname : "<default>"));
}
if (rval == 0) {
PROGRESS(("=0x%lx\n", marks[MARK_END] - marks[MARK_START]));
return fd;
}
err:
(void)close(fd);
return -1;
}
#ifdef BOOT_ECOFF
static int
coff_exec(int fd, struct ecoff_exechdr *coff, u_long *marks, int flags)
{
paddr_t offset = marks[MARK_START];
paddr_t minp = ~0, maxp = 0, pos;
/* Read in text. */
if (lseek(fd, ECOFF_TXTOFF(coff), SEEK_SET) == -1) {
WARN(("lseek text"));
return 1;
}
if (coff->a.tsize != 0) {
if (flags & LOAD_TEXT) {
PROGRESS(("%lu", coff->a.tsize));
if (READ(fd, coff->a.text_start, coff->a.tsize) !=
coff->a.tsize) {
return 1;
}
}
else {
if (lseek(fd, coff->a.tsize, SEEK_CUR) == -1) {
WARN(("read text"));
return 1;
}
}
if (flags & (COUNT_TEXT|LOAD_TEXT)) {
pos = coff->a.text_start;
if (minp > pos)
minp = pos;
pos += coff->a.tsize;
if (maxp < pos)
maxp = pos;
}
}
/* Read in data. */
if (coff->a.dsize != 0) {
if (flags & LOAD_DATA) {
PROGRESS(("+%lu", coff->a.dsize));
if (READ(fd, coff->a.data_start, coff->a.dsize) !=
coff->a.dsize) {
WARN(("read data"));
return 1;
}
}
if (flags & (COUNT_DATA|LOAD_DATA)) {
pos = coff->a.data_start;
if (minp > pos)
minp = pos;
pos += coff->a.dsize;
if (maxp < pos)
maxp = pos;
}
}
/* Zero out bss. */
if (coff->a.bsize != 0) {
if (flags & LOAD_BSS) {
PROGRESS(("+%lu", coff->a.bsize));
BZERO(coff->a.bss_start, coff->a.bsize);
}
if (flags & (COUNT_BSS|LOAD_BSS)) {
pos = coff->a.bss_start;
if (minp > pos)
minp = pos;
pos = coff->a.bsize;
if (maxp < pos)
maxp = pos;
}
}
marks[MARK_START] = LOADADDR(minp);
marks[MARK_ENTRY] = LOADADDR(coff->a.entry);
marks[MARK_NSYM] = 1; /* XXX: Kernel needs >= 0 */
marks[MARK_SYM] = LOADADDR(maxp);
marks[MARK_END] = LOADADDR(maxp);
return 0;
}
#endif /* BOOT_ECOFF */
#ifdef BOOT_AOUT
static int
aout_exec(int fd, struct exec *x, u_long *marks, int flags)
{
u_long entry = x->a_entry;
paddr_t aoutp = 0;
paddr_t minp, maxp;
int cc;
paddr_t offset = marks[MARK_START];
u_long magic = N_GETMAGIC(*x);
int sub;
/* In OMAGIC and NMAGIC, exec header isn't part of text segment */
if (magic == OMAGIC || magic == NMAGIC)
sub = 0;
else
sub = sizeof(*x);
minp = maxp = ALIGNENTRY(entry);
if (lseek(fd, sizeof(*x), SEEK_SET) == -1) {
WARN(("lseek text"));
return 1;
}
/*
* Leave a copy of the exec header before the text.
* The kernel may use this to verify that the
* symbols were loaded by this boot program.
*/
if (magic == OMAGIC || magic == NMAGIC) {
if (flags & LOAD_HDR && maxp >= sizeof(*x))
BCOPY(x, maxp - sizeof(*x), sizeof(*x));
}
else {
if (flags & LOAD_HDR)
BCOPY(x, maxp, sizeof(*x));
if (flags & (LOAD_HDR|COUNT_HDR))
maxp += sizeof(*x);
}
/*
* Read in the text segment.
*/
if (flags & LOAD_TEXT) {
PROGRESS(("%ld", x->a_text));
if (READ(fd, maxp, x->a_text - sub) != x->a_text - sub) {
WARN(("read text"));
return 1;
}
} else {
if (lseek(fd, x->a_text - sub, SEEK_CUR) == -1) {
WARN(("seek text"));
return 1;
}
}
if (flags & (LOAD_TEXT|COUNT_TEXT))
maxp += x->a_text - sub;
/*
* Provide alignment if required
*/
if (magic == ZMAGIC || magic == NMAGIC) {
int size = -(unsigned int)maxp & (__LDPGSZ - 1);
if (flags & LOAD_TEXTA) {
PROGRESS(("/%d", size));
BZERO(maxp, size);
}
if (flags & (LOAD_TEXTA|COUNT_TEXTA))
maxp += size;
}
/*
* Read in the data segment.
*/
if (flags & LOAD_DATA) {
PROGRESS(("+%ld", x->a_data));
if (READ(fd, maxp, x->a_data) != x->a_data) {
WARN(("read data"));
return 1;
}
}
else {
if (lseek(fd, x->a_data, SEEK_CUR) == -1) {
WARN(("seek data"));
return 1;
}
}
if (flags & (LOAD_DATA|COUNT_DATA))
maxp += x->a_data;
/*
* Zero out the BSS section.
* (Kernel doesn't care, but do it anyway.)
*/
if (flags & LOAD_BSS) {
PROGRESS(("+%ld", x->a_bss));
BZERO(maxp, x->a_bss);
}
if (flags & (LOAD_BSS|COUNT_BSS))
maxp += x->a_bss;
/*
* Read in the symbol table and strings.
* (Always set the symtab size word.)
*/
if (flags & LOAD_SYM)
BCOPY(&x->a_syms, maxp, sizeof(x->a_syms));
if (flags & (LOAD_SYM|COUNT_SYM)) {
maxp += sizeof(x->a_syms);
aoutp = maxp;
}
if (x->a_syms > 0) {
/* Symbol table and string table length word. */
if (flags & LOAD_SYM) {
PROGRESS(("+[%ld", x->a_syms));
if (READ(fd, maxp, x->a_syms) != x->a_syms) {
WARN(("read symbols"));
return 1;
}
} else {
if (lseek(fd, x->a_syms, SEEK_CUR) == -1) {
WARN(("seek symbols"));
return 1;
}
}
if (flags & (LOAD_SYM|COUNT_SYM))
maxp += x->a_syms;
if (read(fd, &cc, sizeof(cc)) != sizeof(cc)) {
WARN(("read string table"));
return 1;
}
if (flags & LOAD_SYM) {
BCOPY(&cc, maxp, sizeof(cc));
/* String table. Length word includes itself. */
PROGRESS(("+%d]", cc));
}
if (flags & (LOAD_SYM|COUNT_SYM))
maxp += sizeof(cc);
cc -= sizeof(int);
if (cc <= 0) {
WARN(("symbol table too short"));
return 1;
}
if (flags & LOAD_SYM) {
if (READ(fd, maxp, cc) != cc) {
WARN(("read strings"));
return 1;
}
} else {
if (lseek(fd, cc, SEEK_CUR) == -1) {
WARN(("seek strings"));
return 1;
}
}
if (flags & (LOAD_SYM|COUNT_SYM))
maxp += cc;
}
marks[MARK_START] = LOADADDR(minp);
marks[MARK_ENTRY] = LOADADDR(entry);
marks[MARK_NSYM] = x->a_syms;
marks[MARK_SYM] = LOADADDR(aoutp);
marks[MARK_END] = LOADADDR(maxp);
return 0;
}
#endif /* BOOT_AOUT */
|