loadfile.c revision 1.14 1 /* $NetBSD: loadfile.c,v 1.14 2001/07/31 19:20:29 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 Elf_Off off;
277 int i, j;
278 size_t sz;
279 int first;
280 paddr_t minp = ~0, maxp = 0, pos = 0;
281 paddr_t offset = marks[MARK_START], shpp, elfp = NULL;
282
283 for (first = 1, i = 0; i < elf->e_phnum; i++) {
284 Elf_Phdr phdr;
285 if (lseek(fd, elf->e_phoff + sizeof(phdr) * i, SEEK_SET)
286 == -1) {
287 WARN(("lseek phdr"));
288 return 1;
289 }
290 if (read(fd, (void *)&phdr, sizeof(phdr)) != sizeof(phdr)) {
291 WARN(("read phdr"));
292 return 1;
293 }
294 if (phdr.p_type != PT_LOAD ||
295 (phdr.p_flags & (PF_W|PF_X)) == 0)
296 continue;
297
298 #define IS_TEXT(p) (p.p_flags & PF_X)
299 #define IS_DATA(p) (p.p_flags & PF_W)
300 #define IS_BSS(p) (p.p_filesz < p.p_memsz)
301 /*
302 * XXX: Assume first address is lowest
303 */
304 if ((IS_TEXT(phdr) && (flags & LOAD_TEXT)) ||
305 (IS_DATA(phdr) && (flags & LOAD_DATA))) {
306
307 /* Read in segment. */
308 PROGRESS(("%s%lu", first ? "" : "+",
309 (u_long)phdr.p_filesz));
310
311 if (lseek(fd, phdr.p_offset, SEEK_SET) == -1) {
312 WARN(("lseek text"));
313 return 1;
314 }
315 if (READ(fd, phdr.p_vaddr, phdr.p_filesz) !=
316 phdr.p_filesz) {
317 WARN(("read text"));
318 return 1;
319 }
320 first = 0;
321
322 }
323 if ((IS_TEXT(phdr) && (flags & (LOAD_TEXT|COUNT_TEXT))) ||
324 (IS_DATA(phdr) && (flags & (LOAD_DATA|COUNT_TEXT)))) {
325 pos = phdr.p_vaddr;
326 if (minp > pos)
327 minp = pos;
328 pos += phdr.p_filesz;
329 if (maxp < pos)
330 maxp = pos;
331 }
332
333 /* Zero out bss. */
334 if (IS_BSS(phdr) && (flags & LOAD_BSS)) {
335 PROGRESS(("+%lu",
336 (u_long)(phdr.p_memsz - phdr.p_filesz)));
337 BZERO((phdr.p_vaddr + phdr.p_filesz),
338 phdr.p_memsz - phdr.p_filesz);
339 }
340 if (IS_BSS(phdr) && (flags & (LOAD_BSS|COUNT_BSS))) {
341 pos += phdr.p_memsz - phdr.p_filesz;
342 if (maxp < pos)
343 maxp = pos;
344 }
345 }
346
347 /*
348 * Copy the ELF and section headers.
349 */
350 maxp = roundup(maxp, sizeof(long));
351 if (flags & (LOAD_HDR|COUNT_HDR)) {
352 elfp = maxp;
353 maxp += sizeof(Elf_Ehdr);
354 }
355
356 if (flags & (LOAD_SYM|COUNT_SYM)) {
357 if (lseek(fd, elf->e_shoff, SEEK_SET) == -1) {
358 WARN(("lseek section headers"));
359 return 1;
360 }
361 sz = elf->e_shnum * sizeof(Elf_Shdr);
362
363 shp = ALLOC(sz);
364
365 if (read(fd, shp, sz) != sz) {
366 WARN(("read section headers"));
367 return 1;
368 }
369
370 shpp = maxp;
371 maxp += roundup(sz, sizeof(long));
372
373 /*
374 * Now load the symbol sections themselves. Make sure
375 * the sections are aligned. Don't bother with any
376 * string table that isn't referenced by a symbol
377 * table.
378 */
379 off = roundup((sizeof(Elf_Ehdr) + sz), sizeof(long));
380
381 for (first = 1, i = 0; i < elf->e_shnum; i++) {
382 switch (shp[i].sh_type) {
383 case SHT_STRTAB:
384 for (j = 0; j < elf->e_shnum; j++)
385 if (shp[j].sh_type == SHT_SYMTAB &&
386 shp[j].sh_link == i)
387 goto havesym;
388 break;
389 havesym:
390 case SHT_SYMTAB:
391 if (flags & LOAD_SYM) {
392 PROGRESS(("%s%ld", first ? " [" : "+",
393 (u_long)shp[i].sh_size));
394 if (lseek(fd, shp[i].sh_offset,
395 SEEK_SET) == -1) {
396 WARN(("lseek symbols"));
397 FREE(shp, sz);
398 return 1;
399 }
400 if (READ(fd, maxp, shp[i].sh_size) !=
401 shp[i].sh_size) {
402 WARN(("read symbols"));
403 FREE(shp, sz);
404 return 1;
405 }
406 }
407 maxp += roundup(shp[i].sh_size,
408 sizeof(long));
409 shp[i].sh_offset = off;
410 off += roundup(shp[i].sh_size, sizeof(long));
411 first = 0;
412 }
413 }
414 if (flags & LOAD_SYM) {
415 BCOPY(shp, shpp, sz);
416 FREE(shp, sz);
417
418 if (first == 0)
419 PROGRESS(("]"));
420 }
421 }
422
423 /*
424 * Frob the copied ELF header to give information relative
425 * to elfp.
426 */
427 if (flags & LOAD_HDR) {
428 elf->e_phoff = 0;
429 elf->e_shoff = sizeof(Elf_Ehdr);
430 elf->e_phentsize = 0;
431 elf->e_phnum = 0;
432 BCOPY(elf, elfp, sizeof(*elf));
433 }
434
435 marks[MARK_START] = LOADADDR(minp);
436 marks[MARK_ENTRY] = LOADADDR(elf->e_entry);
437 /*
438 * Since there can be more than one symbol section in the code
439 * and we need to find strtab too in order to do anything
440 * useful with the symbols, we just pass the whole elf
441 * header back and we let the kernel debugger find the
442 * location and number of symbols by itself.
443 */
444 marks[MARK_NSYM] = 1; /* XXX: Kernel needs >= 0 */
445 marks[MARK_SYM] = LOADADDR(elfp);
446 marks[MARK_END] = LOADADDR(maxp);
447 return 0;
448 }
449 #endif /* BOOT_ELF */
450
451 #ifdef BOOT_AOUT
452 static int
453 aout_exec(fd, x, marks, flags)
454 int fd;
455 struct exec *x;
456 u_long *marks;
457 int flags;
458 {
459 u_long entry = x->a_entry;
460 paddr_t aoutp = 0;
461 paddr_t minp, maxp;
462 int cc;
463 paddr_t offset = marks[MARK_START];
464 u_long magic = N_GETMAGIC(*x);
465 int sub;
466
467 /* In OMAGIC and NMAGIC, exec header isn't part of text segment */
468 if (magic == OMAGIC || magic == NMAGIC)
469 sub = 0;
470 else
471 sub = sizeof(*x);
472
473 minp = maxp = ALIGNENTRY(entry);
474
475 if (lseek(fd, sizeof(*x), SEEK_SET) == -1) {
476 WARN(("lseek text"));
477 return 1;
478 }
479
480 /*
481 * Leave a copy of the exec header before the text.
482 * The kernel may use this to verify that the
483 * symbols were loaded by this boot program.
484 */
485 if (magic == OMAGIC || magic == NMAGIC) {
486 if (flags & LOAD_HDR && maxp >= sizeof(*x))
487 BCOPY(x, maxp - sizeof(*x), sizeof(*x));
488 }
489 else {
490 if (flags & LOAD_HDR)
491 BCOPY(x, maxp, sizeof(*x));
492 if (flags & (LOAD_HDR|COUNT_HDR))
493 maxp += sizeof(*x);
494 }
495
496 /*
497 * Read in the text segment.
498 */
499 if (flags & LOAD_TEXT) {
500 PROGRESS(("%ld", x->a_text));
501
502 if (READ(fd, maxp, x->a_text - sub) != x->a_text - sub) {
503 WARN(("read text"));
504 return 1;
505 }
506 } else {
507 if (lseek(fd, x->a_text - sub, SEEK_CUR) == -1) {
508 WARN(("seek text"));
509 return 1;
510 }
511 }
512 if (flags & (LOAD_TEXT|COUNT_TEXT))
513 maxp += x->a_text - sub;
514
515 /*
516 * Provide alignment if required
517 */
518 if (magic == ZMAGIC || magic == NMAGIC) {
519 int size = -(unsigned int)maxp & (__LDPGSZ - 1);
520
521 if (flags & LOAD_TEXTA) {
522 PROGRESS(("/%d", size));
523 BZERO(maxp, size);
524 }
525
526 if (flags & (LOAD_TEXTA|COUNT_TEXTA))
527 maxp += size;
528 }
529
530 /*
531 * Read in the data segment.
532 */
533 if (flags & LOAD_DATA) {
534 PROGRESS(("+%ld", x->a_data));
535
536 if (READ(fd, maxp, x->a_data) != x->a_data) {
537 WARN(("read data"));
538 return 1;
539 }
540 }
541 else {
542 if (lseek(fd, x->a_data, SEEK_CUR) == -1) {
543 WARN(("seek data"));
544 return 1;
545 }
546 }
547 if (flags & (LOAD_DATA|COUNT_DATA))
548 maxp += x->a_data;
549
550 /*
551 * Zero out the BSS section.
552 * (Kernel doesn't care, but do it anyway.)
553 */
554 if (flags & LOAD_BSS) {
555 PROGRESS(("+%ld", x->a_bss));
556
557 BZERO(maxp, x->a_bss);
558 }
559
560 if (flags & (LOAD_BSS|COUNT_BSS))
561 maxp += x->a_bss;
562
563 /*
564 * Read in the symbol table and strings.
565 * (Always set the symtab size word.)
566 */
567 if (flags & LOAD_SYM)
568 BCOPY(&x->a_syms, maxp, sizeof(x->a_syms));
569
570 if (flags & (LOAD_SYM|COUNT_SYM)) {
571 maxp += sizeof(x->a_syms);
572 aoutp = maxp;
573 }
574
575 if (x->a_syms > 0) {
576 /* Symbol table and string table length word. */
577
578 if (flags & LOAD_SYM) {
579 PROGRESS(("+[%ld", x->a_syms));
580
581 if (READ(fd, maxp, x->a_syms) != x->a_syms) {
582 WARN(("read symbols"));
583 return 1;
584 }
585 } else {
586 if (lseek(fd, x->a_syms, SEEK_CUR) == -1) {
587 WARN(("seek symbols"));
588 return 1;
589 }
590 }
591 if (flags & (LOAD_SYM|COUNT_SYM))
592 maxp += x->a_syms;
593
594 if (read(fd, &cc, sizeof(cc)) != sizeof(cc)) {
595 WARN(("read string table"));
596 return 1;
597 }
598
599 if (flags & LOAD_SYM) {
600 BCOPY(&cc, maxp, sizeof(cc));
601
602 /* String table. Length word includes itself. */
603
604 PROGRESS(("+%d]", cc));
605 }
606 if (flags & (LOAD_SYM|COUNT_SYM))
607 maxp += sizeof(cc);
608
609 cc -= sizeof(int);
610 if (cc <= 0) {
611 WARN(("symbol table too short"));
612 return 1;
613 }
614
615 if (flags & LOAD_SYM) {
616 if (READ(fd, maxp, cc) != cc) {
617 WARN(("read strings"));
618 return 1;
619 }
620 } else {
621 if (lseek(fd, cc, SEEK_CUR) == -1) {
622 WARN(("seek strings"));
623 return 1;
624 }
625 }
626 if (flags & (LOAD_SYM|COUNT_SYM))
627 maxp += cc;
628 }
629
630 marks[MARK_START] = LOADADDR(minp);
631 marks[MARK_ENTRY] = LOADADDR(entry);
632 marks[MARK_NSYM] = x->a_syms;
633 marks[MARK_SYM] = LOADADDR(aoutp);
634 marks[MARK_END] = LOADADDR(maxp);
635 return 0;
636 }
637 #endif /* BOOT_AOUT */
638