elf.c revision 1.7.24.1 1 /* $NetBSD: elf.c,v 1.7.24.1 2009/01/15 23:20:02 bouyer Exp $ */
2
3 /*-
4 * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Leo Weppelman.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 #ifdef TOSTOOLS
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <unistd.h>
43 #include <string.h>
44 #include <sys/types.h>
45 #include "exec_elf.h"
46
47 #define MALLOC(x) malloc(x)
48
49 #else
50
51 #include <lib/libsa/stand.h>
52 #include <atari_stand.h>
53 #include <libkern.h>
54 #include <sys/exec_elf.h>
55
56 #define MALLOC(x) alloc(x)
57 #endif
58
59 #include "libtos.h"
60 #include "tosdefs.h"
61 #include "kparamb.h"
62 #include "cread.h"
63
64 /*
65 * Load an elf image.
66 * Exit codes:
67 * -1 : Not an ELF file.
68 * 0 : OK
69 * error# : Error during load (*errp might contain error string).
70 */
71 #define ELFMAGIC ((ELFMAG0 << 24) | (ELFMAG1 << 16) | \
72 (ELFMAG2 << 8) | ELFMAG3)
73
74 int
75 elf_load(fd, od, errp, loadsyms)
76 int fd;
77 osdsc_t *od;
78 char **errp;
79 int loadsyms;
80 {
81 int i,j;
82 int err;
83 Elf32_Ehdr ehdr;
84 Elf32_Phdr *phdrs;
85 Elf32_Word symsize, symstart;
86 long kernsize;
87
88 *errp = NULL;
89 lseek(fd, (off_t)0, SEEK_SET);
90 if (read(fd, (char *)&ehdr, sizeof(ehdr)) != sizeof(ehdr))
91 return -1;
92
93 if (*((u_int *)ehdr.e_ident) != ELFMAGIC)
94 return -1;
95
96 /*
97 * calculate highest used address
98 */
99 i = ehdr.e_phnum * sizeof(Elf32_Phdr);
100 err = 1;
101 if ((phdrs = (Elf32_Phdr *)MALLOC(i)) == NULL)
102 goto error;
103 err = 2;
104 if (read(fd, phdrs, i) != i)
105 goto error;
106
107 kernsize = 0;
108 for (i = 0; i < ehdr.e_phnum; i++) {
109 Elf32_Word sum;
110
111 sum = phdrs[i].p_vaddr + phdrs[i].p_memsz;
112 if ((phdrs[i].p_flags & (PF_W|PF_X)) && (sum > kernsize))
113 kernsize = sum;
114 }
115
116 /*
117 * look for symbols and calculate the size
118 * XXX: This increases the load time by a factor 2 for gzipped
119 * images!
120 */
121 symsize = 0;
122 symstart = 0;
123 if (loadsyms) {
124 i = ehdr.e_shnum + 1;
125 err = 3;
126 if (lseek(fd, (off_t)ehdr.e_shoff, SEEK_SET) != ehdr.e_shoff)
127 goto error;
128 while (--i) {
129 Elf32_Shdr shdr;
130
131 err = 4;
132 if (read(fd, &shdr, sizeof(shdr)) != sizeof(shdr))
133 goto error;
134 if ((shdr.sh_type == SHT_SYMTAB) || (shdr.sh_type == SHT_STRTAB))
135 symsize += shdr.sh_size;
136 }
137 }
138
139 if (symsize) {
140 symstart = kernsize;
141 kernsize += symsize + sizeof(ehdr) + ehdr.e_shnum*sizeof(Elf32_Shdr);
142 }
143
144 /*
145 * Extract various sizes from the kernel executable
146 */
147 od->k_esym = symsize ? kernsize : 0;
148 od->ksize = kernsize;
149 od->kentry = ehdr.e_entry;
150
151 err = 5;
152 if ((od->kstart = (u_char *)MALLOC(od->ksize)) == NULL)
153 goto error;
154
155 /*
156 * Read text & data, clear bss
157 */
158 for (i = 0; i < ehdr.e_phnum; i++) {
159 u_char *p;
160 Elf32_Phdr *php = &phdrs[i];
161
162 if (php->p_flags & (PF_W|PF_X)) {
163 err = 6;
164 if (lseek(fd, (off_t)php->p_offset, SEEK_SET) != php->p_offset)
165 goto error;
166 p = (u_char *)(od->kstart) + php->p_vaddr;
167 err = 7;
168 if (read(fd, p, php->p_filesz) != php->p_filesz)
169 goto error;
170 if (php->p_memsz > php->p_filesz)
171 bzero(p + php->p_filesz, php->p_memsz - php->p_filesz);
172 }
173 }
174
175 /*
176 * Read symbols and strings
177 */
178 if (symsize) {
179 u_char *p, *symtab;
180 int nhdrs;
181 Elf32_Shdr *shp;
182
183 symtab = od->kstart + symstart;
184
185 p = symtab + sizeof(ehdr);
186 nhdrs = ehdr.e_shnum;
187 err = 8;
188 if (lseek(fd, (off_t)ehdr.e_shoff, SEEK_SET) != ehdr.e_shoff)
189 goto error;
190 err = 9;
191 if (read(fd, p, nhdrs * sizeof(*shp)) != nhdrs * sizeof(*shp))
192 goto error;
193 shp = (Elf32_Shdr*)p;
194 p += nhdrs * sizeof(*shp);
195 for (i = 0; i < nhdrs; i++) {
196 if (shp[i].sh_type == SHT_SYMTAB) {
197 if (shp[i].sh_offset == 0)
198 continue;
199 /* Got the symbol table. */
200 err = 10;
201 if (lseek(fd, (off_t)shp[i].sh_offset, SEEK_SET) !=
202 shp[i].sh_offset)
203 goto error;
204 err = 11;
205 if (read(fd, p, shp[i].sh_size) != shp[i].sh_size)
206 goto error;
207 shp[i].sh_offset = p - symtab;
208 /* Find the string table to go with it. */
209 j = shp[i].sh_link;
210 if (shp[j].sh_offset == 0)
211 continue;
212 p += shp[i].sh_size;
213 err = 12;
214 if (lseek(fd, (off_t)shp[j].sh_offset, SEEK_SET) !=
215 shp[j].sh_offset)
216 goto error;
217 err = 13;
218 if (read(fd, p, shp[j].sh_size) != shp[j].sh_size)
219 goto error;
220 shp[j].sh_offset = p - symtab;
221 /* There should only be one symbol table. */
222 break;
223 }
224 }
225 ehdr.e_shoff = sizeof(ehdr);
226 bcopy(&ehdr, symtab, sizeof(ehdr));
227 }
228 return 0;
229
230 error:
231 #ifdef TOSTOOLS
232 {
233 static char *errs[] = {
234 /* 1 */ "Cannot malloc Elf phdr storage space",
235 /* 2 */ "Cannot read Elf32_Phdrs",
236 /* 3 */ "Cannot seek to e_shoff location",
237 /* 4 */ "Cannot read Elf32_shdr",
238 /* 5 */ "Cannot malloc kernel image space",
239 /* 6 */ "Seek error while reading text segment\n",
240 /* 7 */ "Read error in text segment\n",
241 /* 8 */ "Error seeking to section headers",
242 /* 9 */ "Error reading section headers",
243 /* 10 */ "Error seeking to symbols",
244 /* 11 */ "Error reading symbols",
245 /* 12 */ "Error seeking to string table",
246 /* 13 */ "Error reading strings"
247 };
248 *errp = errs[err];
249 }
250 #endif /* TOSTOOLS */
251
252 return err;
253 }
254