Annotation of sys/arch/sparc64/stand/ofwboot/elfXX_exec.c, Revision 1.1.1.1
1.1 nbrk 1: /* $OpenBSD: elfXX_exec.c,v 1.6 2007/01/12 20:16:19 miod Exp $ */
2: /* $NetBSD: elfXX_exec.c,v 1.2 2001/08/15 20:08:15 eeh Exp $ */
3:
4: /*
5: * Copyright (c) 1998-2000 Eduardo Horvath. All rights reserved.
6: * Copyright (c) 1997 Jason R. Thorpe. All rights reserved.
7: * Copyright (C) 1995, 1996 Wolfgang Solfrank.
8: * Copyright (C) 1995, 1996 TooLs GmbH.
9: * All rights reserved.
10: *
11: * ELF support derived from NetBSD/alpha's boot loader, written
12: * by Christopher G. Demetriou.
13: *
14: * Redistribution and use in source and binary forms, with or without
15: * modification, are permitted provided that the following conditions
16: * are met:
17: * 1. Redistributions of source code must retain the above copyright
18: * notice, this list of conditions and the following disclaimer.
19: * 2. Redistributions in binary form must reproduce the above copyright
20: * notice, this list of conditions and the following disclaimer in the
21: * documentation and/or other materials provided with the distribution.
22: * 3. All advertising materials mentioning features or use of this software
23: * must display the following acknowledgement:
24: * This product includes software developed by TooLs GmbH.
25: * 4. The name of TooLs GmbH may not be used to endorse or promote products
26: * derived from this software without specific prior written permission.
27: *
28: * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
29: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
30: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
31: * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
32: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
33: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
34: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
35: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
36: * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
37: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38: */
39:
40: /*
41: * First try for the boot code
42: *
43: * Input syntax is:
44: * [promdev[{:|,}partition]]/[filename] [flags]
45: */
46:
47: #define CONCAT(x,y) __CONCAT(x,y)
48: #define CAT3(s,m,e) CONCAT(s,CONCAT(m,e))
49: #define MEG (1024*1024)
50:
51: #if 0
52: int CAT3(elf,ELFSIZE,_exec)(int, CAT3(Elf,ELFSIZE,_Ehdr) *, u_int64_t *, void **, void **);
53: #endif
54: #define ELF_ALIGN(x) (((x)+7)&(~7))
55:
56: int
57: CAT3(elf, ELFSIZE, _exec)(fd, elf, entryp, ssymp, esymp)
58: int fd;
59: CAT3(Elf,ELFSIZE,_Ehdr) *elf;
60: u_int64_t *entryp;
61: void **ssymp;
62: void **esymp;
63: {
64: CAT3(Elf,ELFSIZE,_Shdr) *shp;
65: CAT3(Elf,ELFSIZE,_Off) off;
66: void *addr;
67: size_t size;
68: u_int align;
69: int i, first = 1;
70: int n;
71:
72: /*
73: * Don't display load address for ELF; it's encoded in
74: * each section.
75: */
76: #ifdef DEBUG
77: printf("elf%d_exec: ", ELFSIZE);
78: #endif
79: printf("Booting %s\n", opened_name);
80:
81: for (i = 0; i < elf->e_phnum; i++) {
82: CAT3(Elf,ELFSIZE,_Phdr) phdr;
83: size = lseek(fd, (size_t)(elf->e_phoff + sizeof(phdr) * i), SEEK_SET);
84: if (read(fd, (void *)&phdr, sizeof(phdr)) != sizeof(phdr)) {
85: printf("read phdr: %s\n", strerror(errno));
86: return (1);
87: }
88: if (phdr.p_type != PT_LOAD ||
89: (phdr.p_flags & (PF_W|PF_X)) == 0)
90: continue;
91:
92: /* Read in segment. */
93: printf("%s%lu@0x%lx", first ? "" : "+", (u_long)phdr.p_filesz,
94: (u_long)phdr.p_vaddr);
95: (void)lseek(fd, (size_t)phdr.p_offset, SEEK_SET);
96: /*
97: * If the segment's VA is aligned on a 4MB boundary, align its
98: * request 4MB aligned physical memory. Otherwise use default
99: * alignment. Make sure BSS is extended to a 4MB boundary, too.
100: */
101: align = phdr.p_align;
102: if ((phdr.p_vaddr & (4*MEG-1)) == 0)
103: align = 4*MEG;
104: if (phdr.p_filesz < phdr.p_memsz)
105: phdr.p_memsz = (phdr.p_memsz + 4*MEG) & ~(4*MEG-1);
106: if (OF_claim((void *)(long)phdr.p_vaddr, phdr.p_memsz, align) ==
107: (void *)-1)
108: panic("cannot claim memory");
109: if (read(fd, (void *)(long)phdr.p_vaddr, phdr.p_filesz) !=
110: phdr.p_filesz) {
111: printf("read segment: %s\n", strerror(errno));
112: return (1);
113: }
114: syncicache((void *)(long)phdr.p_vaddr, phdr.p_filesz);
115:
116: /* Zero BSS. */
117: if (phdr.p_filesz < phdr.p_memsz) {
118: printf("+%lu@0x%lx", (u_long)phdr.p_memsz - phdr.p_filesz,
119: (u_long)(phdr.p_vaddr + phdr.p_filesz));
120: bzero((void *)(long)phdr.p_vaddr + phdr.p_filesz,
121: (size_t)phdr.p_memsz - phdr.p_filesz);
122: }
123: first = 0;
124: }
125:
126: printf(" \n");
127:
128: /*
129: * Compute the size of the symbol table.
130: */
131: size = sizeof(CAT3(Elf,ELFSIZE,_Ehdr)) + (elf->e_shnum * sizeof(CAT3(Elf,ELFSIZE,_Shdr)));
132: shp = addr = alloc(elf->e_shnum * sizeof(CAT3(Elf,ELFSIZE,_Shdr)));
133: (void)lseek(fd, (off_t)elf->e_shoff, SEEK_SET);
134: if (read(fd, addr, (size_t)(elf->e_shnum * sizeof(CAT3(Elf,ELFSIZE,_Shdr)))) !=
135: elf->e_shnum * sizeof(CAT3(Elf,ELFSIZE,_Shdr))) {
136: printf("read section headers: %s\n", strerror(errno));
137: return (1);
138: }
139: for (i = 0; i < elf->e_shnum; i++, shp++) {
140: if (shp->sh_type == SHT_NULL)
141: continue;
142: if (shp->sh_type != SHT_SYMTAB
143: && shp->sh_type != SHT_STRTAB) {
144: shp->sh_offset = 0;
145: continue;
146: }
147: size += shp->sh_size;
148: }
149: shp = addr;
150:
151: /*
152: * Reserve memory for the symbols.
153: */
154: if ((addr = OF_claim(0, size, NBPG)) == (void *)-1)
155: panic("no space for symbol table");
156:
157: /*
158: * Copy the headers.
159: */
160: elf->e_phoff = 0;
161: elf->e_shoff = sizeof(CAT3(Elf,ELFSIZE,_Ehdr));
162: elf->e_phentsize = 0;
163: elf->e_phnum = 0;
164: bcopy(elf, addr, sizeof(CAT3(Elf,ELFSIZE,_Ehdr)));
165: bcopy(shp, addr + sizeof(CAT3(Elf,ELFSIZE,_Ehdr)),
166: elf->e_shnum * sizeof(CAT3(Elf,ELFSIZE,_Shdr)));
167: free(shp, elf->e_shnum * sizeof(CAT3(Elf,ELFSIZE,_Shdr)));
168: *ssymp = addr;
169:
170: /*
171: * Now load the symbol sections themselves.
172: */
173: shp = addr + sizeof(CAT3(Elf,ELFSIZE,_Ehdr));
174: size = sizeof(CAT3(Elf,ELFSIZE,_Ehdr)) +
175: (elf->e_shnum * sizeof(CAT3(Elf,ELFSIZE,_Shdr)));
176: size = ELF_ALIGN(size);
177: addr += size;
178: off = size;
179: for (first = 1, i = 0; i < elf->e_shnum; i++, shp++) {
180: if (shp->sh_type == SHT_SYMTAB
181: || shp->sh_type == SHT_STRTAB) {
182: if (first)
183: printf("symbols @ 0x%lx ", (u_long)addr);
184: printf("%s%d", first ? "" : "+", (int)shp->sh_size);
185: (void)lseek(fd, shp->sh_offset, SEEK_SET);
186: if (read(fd, addr, shp->sh_size) != shp->sh_size) {
187: printf("read symbols: %s\n", strerror(errno));
188: return (1);
189: }
190: addr += ELF_ALIGN(shp->sh_size);
191: shp->sh_offset = off;
192: off += ELF_ALIGN(shp->sh_size);
193: first = 0;
194: }
195: }
196: *esymp = addr;
197:
198: *entryp = elf->e_entry;
199: return (0);
200: }
201:
202: #undef ELF_ALIGN
CVSweb