loadfile.c revision 1.3 1 /* $NetBSD: loadfile.c,v 1.3 1999/10/08 03:55:06 itohy 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(Elf_e_ident, hdr.elf.e_ident, Elf_e_siz) == 0) {
153 rval = elf_exec(fd, &hdr.elf, marks, flags);
154 } else
155 #endif
156 #ifdef BOOT_AOUT
157 if (OKMAGIC(N_GETMAGIC(hdr.aout)) &&
158 N_GETMID(hdr.aout) == MID_MACHINE) {
159 rval = aout_exec(fd, &hdr.aout, marks, flags);
160 } else
161 #endif
162 {
163 rval = 1;
164 errno = EFTYPE;
165 WARN(("%s", fname ? fname : "<default>"));
166 }
167
168 if (rval == 0) {
169 PROGRESS(("=0x%lx\n", marks[MARK_END] - marks[MARK_START]));
170 return fd;
171 }
172 err:
173 (void)close(fd);
174 return -1;
175 }
176
177 #ifdef BOOT_ECOFF
178 static int
179 coff_exec(fd, coff, marks, flags)
180 int fd;
181 struct ecoff_exechdr *coff;
182 u_long *marks;
183 int flags;
184 {
185 paddr_t offset = marks[MARK_START];
186 paddr_t minp = ~0, maxp = 0, pos;
187
188 /* Read in text. */
189 if (lseek(fd, ECOFF_TXTOFF(coff), SEEK_SET) == -1) {
190 WARN(("lseek text"));
191 return 1;
192 }
193
194 if (coff->a.tsize != 0) {
195 if (flags & LOAD_TEXT) {
196 PROGRESS(("%lu", coff->a.tsize));
197 if (READ(fd, coff->a.text_start, coff->a.tsize) !=
198 coff->a.tsize) {
199 return 1;
200 }
201 }
202 else {
203 if (lseek(fd, coff->a.tsize, SEEK_CUR) == -1) {
204 WARN(("read text"));
205 return 1;
206 }
207 }
208 if (flags & (COUNT_TEXT|LOAD_TEXT)) {
209 pos = coff->a.text_start;
210 if (minp > pos)
211 minp = pos;
212 pos += coff->a.tsize;
213 if (maxp < pos)
214 maxp = pos;
215 }
216 }
217
218 /* Read in data. */
219 if (coff->a.dsize != 0) {
220 if (flags & LOAD_DATA) {
221 PROGRESS(("+%lu", coff->a.dsize));
222 if (READ(fd, coff->a.data_start, coff->a.dsize) !=
223 coff->a.dsize) {
224 WARN(("read data"));
225 return 1;
226 }
227 }
228 if (flags & (COUNT_DATA|LOAD_DATA)) {
229 pos = coff->a.data_start;
230 if (minp > pos)
231 minp = pos;
232 pos += coff->a.dsize;
233 if (maxp < pos)
234 maxp = pos;
235 }
236 }
237
238 /* Zero out bss. */
239 if (coff->a.bsize != 0) {
240 if (flags & LOAD_BSS) {
241 PROGRESS(("+%lu", coff->a.bsize));
242 BZERO(coff->a.bss_start, coff->a.bsize);
243 }
244 if (flags & (COUNT_BSS|LOAD_BSS)) {
245 pos = coff->a.bss_start;
246 if (minp > pos)
247 minp = pos;
248 pos = coff->a.bsize;
249 if (maxp < pos)
250 maxp = pos;
251 }
252 }
253
254 marks[MARK_START] = LOADADDR(minp);
255 marks[MARK_ENTRY] = LOADADDR(coff->a.entry);
256 marks[MARK_NSYM] = 1; /* XXX: Kernel needs >= 0 */
257 marks[MARK_SYM] = LOADADDR(maxp);
258 marks[MARK_END] = LOADADDR(maxp);
259 return 0;
260 }
261 #endif /* BOOT_ECOFF */
262
263 #ifdef BOOT_ELF
264 static int
265 elf_exec(fd, elf, marks, flags)
266 int fd;
267 Elf_Ehdr *elf;
268 u_long *marks;
269 int flags;
270 {
271 Elf_Shdr *shp;
272 Elf_Off off;
273 int i;
274 size_t sz;
275 int first;
276 int havesyms;
277 paddr_t minp = ~0, maxp = 0, pos;
278 paddr_t offset = marks[MARK_START], shpp, elfp;
279
280 for (first = 1, i = 0; i < elf->e_phnum; i++) {
281 Elf_Phdr phdr;
282 if (lseek(fd, elf->e_phoff + sizeof(phdr) * i, SEEK_SET)
283 == -1) {
284 WARN(("lseek phdr"));
285 return 1;
286 }
287 if (read(fd, (void *)&phdr, sizeof(phdr)) != sizeof(phdr)) {
288 WARN(("read phdr"));
289 return 1;
290 }
291 if (phdr.p_type != Elf_pt_load ||
292 (phdr.p_flags & (Elf_pf_w|Elf_pf_x)) == 0)
293 continue;
294
295 #define IS_TEXT(p) (p.p_type & Elf_pf_x)
296 #define IS_DATA(p) (p.p_type & Elf_pf_w)
297 #define IS_BSS(p) (p.p_filesz < p.p_memsz)
298 /*
299 * XXX: Assume first address is lowest
300 */
301 if ((IS_TEXT(phdr) && (flags & LOAD_TEXT)) ||
302 (IS_DATA(phdr) && (flags & LOAD_DATA))) {
303
304 /* Read in segment. */
305 PROGRESS(("%s%lu", first ? "" : "+",
306 (u_long)phdr.p_filesz));
307
308 if (lseek(fd, phdr.p_offset, SEEK_SET) == -1) {
309 WARN(("lseek text"));
310 return 1;
311 }
312 if (READ(fd, phdr.p_vaddr, phdr.p_filesz) !=
313 phdr.p_filesz) {
314 WARN(("read text"));
315 return 1;
316 }
317 first = 0;
318
319 }
320 if ((IS_TEXT(phdr) && (flags & (LOAD_TEXT|COUNT_TEXT))) ||
321 (IS_DATA(phdr) && (flags & (LOAD_DATA|COUNT_TEXT)))) {
322 pos = phdr.p_vaddr;
323 if (minp > pos)
324 minp = pos;
325 pos += phdr.p_filesz;
326 if (maxp < pos)
327 maxp = pos;
328 }
329
330 /* Zero out bss. */
331 if (IS_BSS(phdr) && (flags & LOAD_BSS)) {
332 PROGRESS(("+%lu",
333 (u_long)(phdr.p_memsz - phdr.p_filesz)));
334 BZERO((phdr.p_vaddr + phdr.p_filesz),
335 phdr.p_memsz - phdr.p_filesz);
336 }
337 if (IS_BSS(phdr) && (flags & (LOAD_BSS|COUNT_BSS))) {
338 pos += phdr.p_memsz - phdr.p_filesz;
339 if (maxp < pos)
340 maxp = pos;
341 }
342 }
343
344 /*
345 * Copy the ELF and section headers.
346 */
347 maxp = roundup(maxp, sizeof(long));
348 if (flags & (LOAD_HDR|COUNT_HDR)) {
349 elfp = maxp;
350 maxp += sizeof(Elf_Ehdr);
351 }
352
353 if (flags & (LOAD_SYM|COUNT_SYM)) {
354 if (lseek(fd, elf->e_shoff, SEEK_SET) == -1) {
355 WARN(("lseek section headers"));
356 return 1;
357 }
358 sz = elf->e_shnum * sizeof(Elf_Shdr);
359
360 shp = ALLOC(sz);
361
362 if (read(fd, shp, sz) != sz) {
363 WARN(("read section headers"));
364 return 1;
365 }
366
367 shpp = maxp;
368 maxp += roundup(sz, sizeof(long));
369
370 /*
371 * Now load the symbol sections themselves. Make sure the
372 * sections are aligned. Don't bother with string tables if
373 * there are no symbol sections.
374 */
375 off = roundup((sizeof(Elf_Ehdr) + sz), sizeof(long));
376
377 for (havesyms = i = 0; i < elf->e_shnum; i++)
378 if (shp[i].sh_type == Elf_sht_symtab)
379 havesyms = 1;
380
381 for (first = 1, i = 0; i < elf->e_shnum; i++) {
382 if (shp[i].sh_type == Elf_sht_symtab ||
383 shp[i].sh_type == Elf_sht_strtab) {
384 if (havesyms && (flags & LOAD_SYM)) {
385 PROGRESS(("%s%ld", first ? " [" : "+",
386 (u_long)shp[i].sh_size));
387 if (lseek(fd, shp[i].sh_offset,
388 SEEK_SET) == -1) {
389 WARN(("lseek symbols"));
390 FREE(shp, sz);
391 return 1;
392 }
393 if (READ(fd, maxp, shp[i].sh_size) !=
394 shp[i].sh_size) {
395 WARN(("read symbols"));
396 FREE(shp, sz);
397 return 1;
398 }
399 }
400 maxp += roundup(shp[i].sh_size,
401 sizeof(long));
402 shp[i].sh_offset = off;
403 off += roundup(shp[i].sh_size, sizeof(long));
404 first = 0;
405 }
406 }
407 if (flags & LOAD_SYM) {
408 BCOPY(shp, shpp, sz);
409 FREE(shp, sz);
410
411 if (first == 0)
412 PROGRESS(("]"));
413 }
414 }
415
416 /*
417 * Frob the copied ELF header to give information relative
418 * to elfp.
419 */
420 if (flags & LOAD_HDR) {
421 elf->e_phoff = 0;
422 elf->e_shoff = sizeof(Elf_Ehdr);
423 elf->e_phentsize = 0;
424 elf->e_phnum = 0;
425 BCOPY(elf, elfp, sizeof(*elf));
426 }
427
428 marks[MARK_START] = LOADADDR(minp);
429 marks[MARK_ENTRY] = LOADADDR(elf->e_entry);
430 marks[MARK_NSYM] = 1; /* XXX: Kernel needs >= 0 */
431 marks[MARK_SYM] = LOADADDR(elfp);
432 marks[MARK_END] = LOADADDR(maxp);
433 return 0;
434 }
435 #endif /* BOOT_ELF */
436
437 #ifdef BOOT_AOUT
438 static int
439 aout_exec(fd, x, marks, flags)
440 int fd;
441 struct exec *x;
442 u_long *marks;
443 int flags;
444 {
445 u_long entry = x->a_entry;
446 paddr_t aoutp = 0;
447 paddr_t minp, maxp;
448 int cc;
449 paddr_t offset = marks[MARK_START];
450 u_long magic = N_GETMAGIC(*x);
451 int sub;
452
453 /* In OMAGIC and NMAGIC, exec header isn't part of text segment */
454 if (magic == OMAGIC || magic == NMAGIC)
455 sub = 0;
456 else
457 sub = sizeof(*x);
458
459 minp = maxp = ALIGNENTRY(entry);
460
461 if (lseek(fd, sizeof(*x), SEEK_SET) == -1) {
462 WARN(("lseek text"));
463 return 1;
464 }
465
466 /*
467 * Leave a copy of the exec header before the text.
468 * The kernel may use this to verify that the
469 * symbols were loaded by this boot program.
470 */
471 if (magic == OMAGIC || magic == NMAGIC) {
472 if (flags & LOAD_HDR)
473 BCOPY(x, maxp - sizeof(*x), sizeof(*x));
474 }
475 else {
476 if (flags & LOAD_HDR)
477 BCOPY(x, maxp, sizeof(*x));
478 if (flags & (LOAD_HDR|COUNT_HDR))
479 maxp += sizeof(*x);
480 }
481
482 /*
483 * Read in the text segment.
484 */
485 if (flags & LOAD_TEXT) {
486 PROGRESS(("%ld", x->a_text));
487
488 if (READ(fd, maxp, x->a_text - sub) != x->a_text - sub) {
489 WARN(("read text"));
490 return 1;
491 }
492 } else {
493 if (lseek(fd, x->a_text - sub, SEEK_CUR) == -1) {
494 WARN(("seek text"));
495 return 1;
496 }
497 }
498 if (flags & (LOAD_TEXT|COUNT_TEXT))
499 maxp += x->a_text - sub;
500
501 /*
502 * Provide alignment if required
503 */
504 if (magic == ZMAGIC || magic == NMAGIC) {
505 int size = -(unsigned int)maxp & (__LDPGSZ - 1);
506
507 if (flags & LOAD_TEXTA) {
508 PROGRESS(("/%d", size));
509 BZERO(maxp, size);
510 }
511
512 if (flags & (LOAD_TEXTA|COUNT_TEXTA))
513 maxp += size;
514 }
515
516 /*
517 * Read in the data segment.
518 */
519 if (flags & LOAD_DATA) {
520 PROGRESS(("+%ld", x->a_data));
521
522 if (READ(fd, maxp, x->a_data) != x->a_data) {
523 WARN(("read data"));
524 return 1;
525 }
526 }
527 else {
528 if (lseek(fd, x->a_data, SEEK_CUR) == -1) {
529 WARN(("seek data"));
530 return 1;
531 }
532 }
533 if (flags & (LOAD_DATA|COUNT_DATA))
534 maxp += x->a_data;
535
536 /*
537 * Zero out the BSS section.
538 * (Kernel doesn't care, but do it anyway.)
539 */
540 if (flags & LOAD_BSS) {
541 PROGRESS(("+%ld", x->a_bss));
542
543 BZERO(maxp, x->a_bss);
544 }
545
546 if (flags & (LOAD_BSS|COUNT_BSS))
547 maxp += x->a_bss;
548
549 /*
550 * Read in the symbol table and strings.
551 * (Always set the symtab size word.)
552 */
553 if (flags & LOAD_SYM)
554 BCOPY(&x->a_syms, maxp, sizeof(x->a_syms));
555
556 if (flags & (LOAD_SYM|COUNT_SYM)) {
557 maxp += sizeof(x->a_syms);
558 aoutp = maxp;
559 }
560
561 if (x->a_syms > 0) {
562 /* Symbol table and string table length word. */
563
564 if (flags & LOAD_SYM) {
565 PROGRESS(("+[%ld", x->a_syms));
566
567 if (READ(fd, maxp, x->a_syms) != x->a_syms) {
568 WARN(("read symbols"));
569 return 1;
570 }
571 } else {
572 if (lseek(fd, x->a_syms, SEEK_CUR) == -1) {
573 WARN(("seek symbols"));
574 return 1;
575 }
576 }
577 if (flags & (LOAD_SYM|COUNT_SYM))
578 maxp += x->a_syms;
579
580 if (read(fd, &cc, sizeof(cc)) != sizeof(cc)) {
581 WARN(("read string table"));
582 return 1;
583 }
584
585 if (flags & LOAD_SYM) {
586 BCOPY(&cc, maxp, sizeof(cc));
587
588 /* String table. Length word includes itself. */
589
590 PROGRESS(("+%d]", cc));
591 }
592 if (flags & (LOAD_SYM|COUNT_SYM))
593 maxp += sizeof(cc);
594
595 cc -= sizeof(int);
596 if (cc <= 0) {
597 WARN(("symbol table too short"));
598 return 1;
599 }
600
601 if (flags & LOAD_SYM) {
602 if (READ(fd, maxp, cc) != cc) {
603 WARN(("read strings"));
604 return 1;
605 }
606 } else {
607 if (lseek(fd, cc, SEEK_CUR) == -1) {
608 WARN(("seek strings"));
609 return 1;
610 }
611 }
612 if (flags & (LOAD_SYM|COUNT_SYM))
613 maxp += cc;
614 }
615
616 marks[MARK_START] = LOADADDR(minp);
617 marks[MARK_ENTRY] = LOADADDR(entry);
618 marks[MARK_NSYM] = x->a_syms;
619 marks[MARK_SYM] = LOADADDR(aoutp);
620 marks[MARK_END] = LOADADDR(maxp);
621 return 0;
622 }
623 #endif /* BOOT_AOUT */
624