loadfile.c revision 1.17 1 /* $NetBSD: loadfile.c,v 1.17 2001/07/31 22:11:57 bjh21 Exp $ */
2
3 /*-
4 * Copyright (c) 1997 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center and by Christos Zoulas.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 /*
41 * Copyright (c) 1992, 1993
42 * The Regents of the University of California. All rights reserved.
43 *
44 * This code is derived from software contributed to Berkeley by
45 * Ralph Campbell.
46 *
47 * Redistribution and use in source and binary forms, with or without
48 * modification, are permitted provided that the following conditions
49 * are met:
50 * 1. Redistributions of source code must retain the above copyright
51 * notice, this list of conditions and the following disclaimer.
52 * 2. Redistributions in binary form must reproduce the above copyright
53 * notice, this list of conditions and the following disclaimer in the
54 * documentation and/or other materials provided with the distribution.
55 * 3. All advertising materials mentioning features or use of this software
56 * must display the following acknowledgement:
57 * This product includes software developed by the University of
58 * California, Berkeley and its contributors.
59 * 4. Neither the name of the University nor the names of its contributors
60 * may be used to endorse or promote products derived from this software
61 * without specific prior written permission.
62 *
63 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
64 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
65 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
66 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
67 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
68 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
69 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
70 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
71 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
72 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
73 * SUCH DAMAGE.
74 *
75 * @(#)boot.c 8.1 (Berkeley) 6/10/93
76 */
77
78 #ifdef _STANDALONE
79 #include <lib/libsa/stand.h>
80 #include <lib/libkern/libkern.h>
81 #else
82 #include <stdio.h>
83 #include <string.h>
84 #include <errno.h>
85 #include <stdlib.h>
86 #include <unistd.h>
87 #include <fcntl.h>
88 #include <err.h>
89 #endif
90
91 #include <sys/param.h>
92 #include <sys/exec.h>
93
94 #include "loadfile.h"
95
96 #ifdef BOOT_ECOFF
97 #include <sys/exec_ecoff.h>
98 static int coff_exec __P((int, struct ecoff_exechdr *, u_long *, int));
99 #endif
100 #ifdef BOOT_ELF
101 #include <sys/exec_elf.h>
102 static int elf_exec __P((int, Elf_Ehdr *, u_long *, int));
103 #endif
104 #ifdef BOOT_AOUT
105 #include <sys/exec_aout.h>
106 static int aout_exec __P((int, struct exec *, u_long *, int));
107 #endif
108
109 /*
110 * Open 'filename', read in program and and return 0 if ok 1 on error.
111 * Fill in marks
112 */
113 int
114 loadfile(fname, marks, flags)
115 const char *fname;
116 u_long *marks;
117 int flags;
118 {
119 union {
120 #ifdef BOOT_ECOFF
121 struct ecoff_exechdr coff;
122 #endif
123 #ifdef BOOT_ELF
124 Elf_Ehdr elf;
125 #endif
126 #ifdef BOOT_AOUT
127 struct exec aout;
128 #endif
129
130 } hdr;
131 ssize_t nr;
132 int fd, rval;
133
134 /* Open the file. */
135 if ((fd = open(fname, 0)) < 0) {
136 WARN(("open %s", fname ? fname : "<default>"));
137 return -1;
138 }
139
140 /* Read the exec header. */
141 if ((nr = read(fd, &hdr, sizeof(hdr))) != sizeof(hdr)) {
142 WARN(("read header"));
143 goto err;
144 }
145
146 #ifdef BOOT_ECOFF
147 if (!ECOFF_BADMAG(&hdr.coff)) {
148 rval = coff_exec(fd, &hdr.coff, marks, flags);
149 } else
150 #endif
151 #ifdef BOOT_ELF
152 if (memcmp(hdr.elf.e_ident, ELFMAG, SELFMAG) == 0 &&
153 hdr.elf.e_ident[EI_CLASS] == ELFCLASS) {
154 rval = elf_exec(fd, &hdr.elf, marks, flags);
155 } else
156 #endif
157 #ifdef BOOT_AOUT
158 if (OKMAGIC(N_GETMAGIC(hdr.aout))
159 #ifndef NO_MID_CHECK
160 && N_GETMID(hdr.aout) == MID_MACHINE
161 #endif
162 ) {
163 rval = aout_exec(fd, &hdr.aout, marks, flags);
164 } else
165 #endif
166 {
167 rval = 1;
168 errno = EFTYPE;
169 WARN(("%s", fname ? fname : "<default>"));
170 }
171
172 if (rval == 0) {
173 PROGRESS(("=0x%lx\n", marks[MARK_END] - marks[MARK_START]));
174 return fd;
175 }
176 err:
177 (void)close(fd);
178 return -1;
179 }
180
181 #ifdef BOOT_ECOFF
182 static int
183 coff_exec(fd, coff, marks, flags)
184 int fd;
185 struct ecoff_exechdr *coff;
186 u_long *marks;
187 int flags;
188 {
189 paddr_t offset = marks[MARK_START];
190 paddr_t minp = ~0, maxp = 0, pos;
191
192 /* Read in text. */
193 if (lseek(fd, ECOFF_TXTOFF(coff), SEEK_SET) == -1) {
194 WARN(("lseek text"));
195 return 1;
196 }
197
198 if (coff->a.tsize != 0) {
199 if (flags & LOAD_TEXT) {
200 PROGRESS(("%lu", coff->a.tsize));
201 if (READ(fd, coff->a.text_start, coff->a.tsize) !=
202 coff->a.tsize) {
203 return 1;
204 }
205 }
206 else {
207 if (lseek(fd, coff->a.tsize, SEEK_CUR) == -1) {
208 WARN(("read text"));
209 return 1;
210 }
211 }
212 if (flags & (COUNT_TEXT|LOAD_TEXT)) {
213 pos = coff->a.text_start;
214 if (minp > pos)
215 minp = pos;
216 pos += coff->a.tsize;
217 if (maxp < pos)
218 maxp = pos;
219 }
220 }
221
222 /* Read in data. */
223 if (coff->a.dsize != 0) {
224 if (flags & LOAD_DATA) {
225 PROGRESS(("+%lu", coff->a.dsize));
226 if (READ(fd, coff->a.data_start, coff->a.dsize) !=
227 coff->a.dsize) {
228 WARN(("read data"));
229 return 1;
230 }
231 }
232 if (flags & (COUNT_DATA|LOAD_DATA)) {
233 pos = coff->a.data_start;
234 if (minp > pos)
235 minp = pos;
236 pos += coff->a.dsize;
237 if (maxp < pos)
238 maxp = pos;
239 }
240 }
241
242 /* Zero out bss. */
243 if (coff->a.bsize != 0) {
244 if (flags & LOAD_BSS) {
245 PROGRESS(("+%lu", coff->a.bsize));
246 BZERO(coff->a.bss_start, coff->a.bsize);
247 }
248 if (flags & (COUNT_BSS|LOAD_BSS)) {
249 pos = coff->a.bss_start;
250 if (minp > pos)
251 minp = pos;
252 pos = coff->a.bsize;
253 if (maxp < pos)
254 maxp = pos;
255 }
256 }
257
258 marks[MARK_START] = LOADADDR(minp);
259 marks[MARK_ENTRY] = LOADADDR(coff->a.entry);
260 marks[MARK_NSYM] = 1; /* XXX: Kernel needs >= 0 */
261 marks[MARK_SYM] = LOADADDR(maxp);
262 marks[MARK_END] = LOADADDR(maxp);
263 return 0;
264 }
265 #endif /* BOOT_ECOFF */
266
267 #ifdef BOOT_ELF
268 static int
269 elf_exec(fd, elf, marks, flags)
270 int fd;
271 Elf_Ehdr *elf;
272 u_long *marks;
273 int flags;
274 {
275 Elf_Shdr *shp;
276 int i, j;
277 size_t sz;
278 int first;
279 paddr_t minp = ~0, maxp = 0, pos = 0;
280 paddr_t offset = marks[MARK_START], shpp, elfp = NULL;
281
282 for (first = 1, i = 0; i < elf->e_phnum; i++) {
283 Elf_Phdr phdr;
284 if (lseek(fd, elf->e_phoff + sizeof(phdr) * i, SEEK_SET)
285 == -1) {
286 WARN(("lseek phdr"));
287 return 1;
288 }
289 if (read(fd, (void *)&phdr, sizeof(phdr)) != sizeof(phdr)) {
290 WARN(("read phdr"));
291 return 1;
292 }
293 if (phdr.p_type != PT_LOAD ||
294 (phdr.p_flags & (PF_W|PF_X)) == 0)
295 continue;
296
297 #define IS_TEXT(p) (p.p_flags & PF_X)
298 #define IS_DATA(p) (p.p_flags & PF_W)
299 #define IS_BSS(p) (p.p_filesz < p.p_memsz)
300 /*
301 * XXX: Assume first address is lowest
302 */
303 if ((IS_TEXT(phdr) && (flags & LOAD_TEXT)) ||
304 (IS_DATA(phdr) && (flags & LOAD_DATA))) {
305
306 /* Read in segment. */
307 PROGRESS(("%s%lu", first ? "" : "+",
308 (u_long)phdr.p_filesz));
309
310 if (lseek(fd, phdr.p_offset, SEEK_SET) == -1) {
311 WARN(("lseek text"));
312 return 1;
313 }
314 if (READ(fd, phdr.p_vaddr, phdr.p_filesz) !=
315 phdr.p_filesz) {
316 WARN(("read text"));
317 return 1;
318 }
319 first = 0;
320
321 }
322 if ((IS_TEXT(phdr) && (flags & (LOAD_TEXT|COUNT_TEXT))) ||
323 (IS_DATA(phdr) && (flags & (LOAD_DATA|COUNT_TEXT)))) {
324 pos = phdr.p_vaddr;
325 if (minp > pos)
326 minp = pos;
327 pos += phdr.p_filesz;
328 if (maxp < pos)
329 maxp = pos;
330 }
331
332 /* Zero out bss. */
333 if (IS_BSS(phdr) && (flags & LOAD_BSS)) {
334 PROGRESS(("+%lu",
335 (u_long)(phdr.p_memsz - phdr.p_filesz)));
336 BZERO((phdr.p_vaddr + phdr.p_filesz),
337 phdr.p_memsz - phdr.p_filesz);
338 }
339 if (IS_BSS(phdr) && (flags & (LOAD_BSS|COUNT_BSS))) {
340 pos += phdr.p_memsz - phdr.p_filesz;
341 if (maxp < pos)
342 maxp = pos;
343 }
344 }
345
346 /*
347 * Copy the ELF and section headers.
348 */
349 maxp = roundup(maxp, sizeof(long));
350 if (flags & (LOAD_HDR|COUNT_HDR)) {
351 elfp = maxp;
352 maxp += sizeof(Elf_Ehdr);
353 }
354
355 if (flags & (LOAD_SYM|COUNT_SYM)) {
356 if (lseek(fd, elf->e_shoff, SEEK_SET) == -1) {
357 WARN(("lseek section headers"));
358 return 1;
359 }
360 sz = elf->e_shnum * sizeof(Elf_Shdr);
361
362 shp = ALLOC(sz);
363
364 if (read(fd, shp, sz) != sz) {
365 WARN(("read section headers"));
366 return 1;
367 }
368
369 shpp = maxp;
370 maxp += roundup(sz, sizeof(long));
371
372 /*
373 * Now load the symbol sections themselves. Make sure
374 * the sections are aligned. Don't bother with any
375 * string table that isn't referenced by a symbol
376 * table.
377 */
378 for (first = 1, i = 0; i < elf->e_shnum; i++) {
379 switch (shp[i].sh_type) {
380 case SHT_STRTAB:
381 for (j = 0; j < elf->e_shnum; j++)
382 if (shp[j].sh_type == SHT_SYMTAB &&
383 shp[j].sh_link == i)
384 goto havesym;
385 /* FALLTHROUGH */
386 default:
387 /* Not loading this, so zero out the offset. */
388 shp[i].sh_offset = 0;
389 break;
390 havesym:
391 case SHT_SYMTAB:
392 if (flags & LOAD_SYM) {
393 PROGRESS(("%s%ld", first ? " [" : "+",
394 (u_long)shp[i].sh_size));
395 if (lseek(fd, shp[i].sh_offset,
396 SEEK_SET) == -1) {
397 WARN(("lseek symbols"));
398 FREE(shp, sz);
399 return 1;
400 }
401 if (READ(fd, maxp, shp[i].sh_size) !=
402 shp[i].sh_size) {
403 WARN(("read symbols"));
404 FREE(shp, sz);
405 return 1;
406 }
407 }
408 shp[i].sh_offset = maxp - elfp;
409 maxp += roundup(shp[i].sh_size,
410 sizeof(long));
411 first = 0;
412 }
413 /* Since we don't load .shstrtab, zero the name. */
414 shp[i].sh_name = 0;
415 }
416 if (flags & LOAD_SYM) {
417 BCOPY(shp, shpp, sz);
418
419 if (first == 0)
420 PROGRESS(("]"));
421 }
422 FREE(shp, sz);
423 }
424
425 /*
426 * Frob the copied ELF header to give information relative
427 * to elfp.
428 */
429 if (flags & LOAD_HDR) {
430 elf->e_phoff = 0;
431 elf->e_shoff = sizeof(Elf_Ehdr);
432 elf->e_phentsize = 0;
433 elf->e_phnum = 0;
434 elf->e_shstrndx = SHN_UNDEF;
435 BCOPY(elf, elfp, sizeof(*elf));
436 }
437
438 marks[MARK_START] = LOADADDR(minp);
439 marks[MARK_ENTRY] = LOADADDR(elf->e_entry);
440 /*
441 * Since there can be more than one symbol section in the code
442 * and we need to find strtab too in order to do anything
443 * useful with the symbols, we just pass the whole elf
444 * header back and we let the kernel debugger find the
445 * location and number of symbols by itself.
446 */
447 marks[MARK_NSYM] = 1; /* XXX: Kernel needs >= 0 */
448 marks[MARK_SYM] = LOADADDR(elfp);
449 marks[MARK_END] = LOADADDR(maxp);
450 return 0;
451 }
452 #endif /* BOOT_ELF */
453
454 #ifdef BOOT_AOUT
455 static int
456 aout_exec(fd, x, marks, flags)
457 int fd;
458 struct exec *x;
459 u_long *marks;
460 int flags;
461 {
462 u_long entry = x->a_entry;
463 paddr_t aoutp = 0;
464 paddr_t minp, maxp;
465 int cc;
466 paddr_t offset = marks[MARK_START];
467 u_long magic = N_GETMAGIC(*x);
468 int sub;
469
470 /* In OMAGIC and NMAGIC, exec header isn't part of text segment */
471 if (magic == OMAGIC || magic == NMAGIC)
472 sub = 0;
473 else
474 sub = sizeof(*x);
475
476 minp = maxp = ALIGNENTRY(entry);
477
478 if (lseek(fd, sizeof(*x), SEEK_SET) == -1) {
479 WARN(("lseek text"));
480 return 1;
481 }
482
483 /*
484 * Leave a copy of the exec header before the text.
485 * The kernel may use this to verify that the
486 * symbols were loaded by this boot program.
487 */
488 if (magic == OMAGIC || magic == NMAGIC) {
489 if (flags & LOAD_HDR && maxp >= sizeof(*x))
490 BCOPY(x, maxp - sizeof(*x), sizeof(*x));
491 }
492 else {
493 if (flags & LOAD_HDR)
494 BCOPY(x, maxp, sizeof(*x));
495 if (flags & (LOAD_HDR|COUNT_HDR))
496 maxp += sizeof(*x);
497 }
498
499 /*
500 * Read in the text segment.
501 */
502 if (flags & LOAD_TEXT) {
503 PROGRESS(("%ld", x->a_text));
504
505 if (READ(fd, maxp, x->a_text - sub) != x->a_text - sub) {
506 WARN(("read text"));
507 return 1;
508 }
509 } else {
510 if (lseek(fd, x->a_text - sub, SEEK_CUR) == -1) {
511 WARN(("seek text"));
512 return 1;
513 }
514 }
515 if (flags & (LOAD_TEXT|COUNT_TEXT))
516 maxp += x->a_text - sub;
517
518 /*
519 * Provide alignment if required
520 */
521 if (magic == ZMAGIC || magic == NMAGIC) {
522 int size = -(unsigned int)maxp & (__LDPGSZ - 1);
523
524 if (flags & LOAD_TEXTA) {
525 PROGRESS(("/%d", size));
526 BZERO(maxp, size);
527 }
528
529 if (flags & (LOAD_TEXTA|COUNT_TEXTA))
530 maxp += size;
531 }
532
533 /*
534 * Read in the data segment.
535 */
536 if (flags & LOAD_DATA) {
537 PROGRESS(("+%ld", x->a_data));
538
539 if (READ(fd, maxp, x->a_data) != x->a_data) {
540 WARN(("read data"));
541 return 1;
542 }
543 }
544 else {
545 if (lseek(fd, x->a_data, SEEK_CUR) == -1) {
546 WARN(("seek data"));
547 return 1;
548 }
549 }
550 if (flags & (LOAD_DATA|COUNT_DATA))
551 maxp += x->a_data;
552
553 /*
554 * Zero out the BSS section.
555 * (Kernel doesn't care, but do it anyway.)
556 */
557 if (flags & LOAD_BSS) {
558 PROGRESS(("+%ld", x->a_bss));
559
560 BZERO(maxp, x->a_bss);
561 }
562
563 if (flags & (LOAD_BSS|COUNT_BSS))
564 maxp += x->a_bss;
565
566 /*
567 * Read in the symbol table and strings.
568 * (Always set the symtab size word.)
569 */
570 if (flags & LOAD_SYM)
571 BCOPY(&x->a_syms, maxp, sizeof(x->a_syms));
572
573 if (flags & (LOAD_SYM|COUNT_SYM)) {
574 maxp += sizeof(x->a_syms);
575 aoutp = maxp;
576 }
577
578 if (x->a_syms > 0) {
579 /* Symbol table and string table length word. */
580
581 if (flags & LOAD_SYM) {
582 PROGRESS(("+[%ld", x->a_syms));
583
584 if (READ(fd, maxp, x->a_syms) != x->a_syms) {
585 WARN(("read symbols"));
586 return 1;
587 }
588 } else {
589 if (lseek(fd, x->a_syms, SEEK_CUR) == -1) {
590 WARN(("seek symbols"));
591 return 1;
592 }
593 }
594 if (flags & (LOAD_SYM|COUNT_SYM))
595 maxp += x->a_syms;
596
597 if (read(fd, &cc, sizeof(cc)) != sizeof(cc)) {
598 WARN(("read string table"));
599 return 1;
600 }
601
602 if (flags & LOAD_SYM) {
603 BCOPY(&cc, maxp, sizeof(cc));
604
605 /* String table. Length word includes itself. */
606
607 PROGRESS(("+%d]", cc));
608 }
609 if (flags & (LOAD_SYM|COUNT_SYM))
610 maxp += sizeof(cc);
611
612 cc -= sizeof(int);
613 if (cc <= 0) {
614 WARN(("symbol table too short"));
615 return 1;
616 }
617
618 if (flags & LOAD_SYM) {
619 if (READ(fd, maxp, cc) != cc) {
620 WARN(("read strings"));
621 return 1;
622 }
623 } else {
624 if (lseek(fd, cc, SEEK_CUR) == -1) {
625 WARN(("seek strings"));
626 return 1;
627 }
628 }
629 if (flags & (LOAD_SYM|COUNT_SYM))
630 maxp += cc;
631 }
632
633 marks[MARK_START] = LOADADDR(minp);
634 marks[MARK_ENTRY] = LOADADDR(entry);
635 marks[MARK_NSYM] = x->a_syms;
636 marks[MARK_SYM] = LOADADDR(aoutp);
637 marks[MARK_END] = LOADADDR(maxp);
638 return 0;
639 }
640 #endif /* BOOT_AOUT */
641