Deleted Added
full compact
load_elf.c (215811) load_elf.c (220311)
1/*-
2 * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
3 * Copyright (c) 1998 Peter Wemm <peter@freebsd.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28#include <sys/cdefs.h>
1/*-
2 * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
3 * Copyright (c) 1998 Peter Wemm <peter@freebsd.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28#include <sys/cdefs.h>
29__FBSDID("$FreeBSD: head/sys/boot/common/load_elf.c 215811 2010-11-25 03:16:31Z emaste $");
29__FBSDID("$FreeBSD: head/sys/boot/common/load_elf.c 220311 2011-04-03 22:31:51Z marcel $");
30
31#include <sys/param.h>
32#include <sys/exec.h>
33#include <sys/linker.h>
34#include <sys/module.h>
35#include <sys/stdint.h>
36#include <string.h>
37#include <machine/elf.h>
38#include <stand.h>
39#define FREEBSD_ELF
40#include <link.h>
41
42#include "bootstrap.h"
43
44#define COPYOUT(s,d,l) archsw.arch_copyout((vm_offset_t)(s), d, l)
45
46#if defined(__i386__) && __ELF_WORD_SIZE == 64
47#undef ELF_TARG_CLASS
48#undef ELF_TARG_MACH
49#define ELF_TARG_CLASS ELFCLASS64
50#define ELF_TARG_MACH EM_X86_64
51#endif
52
53typedef struct elf_file {
54 Elf_Phdr *ph;
55 Elf_Ehdr *ehdr;
56 Elf_Sym *symtab;
57 Elf_Hashelt *hashtab;
58 Elf_Hashelt nbuckets;
59 Elf_Hashelt nchains;
60 Elf_Hashelt *buckets;
61 Elf_Hashelt *chains;
62 Elf_Rel *rel;
63 size_t relsz;
64 Elf_Rela *rela;
65 size_t relasz;
66 char *strtab;
67 size_t strsz;
68 int fd;
69 caddr_t firstpage;
70 size_t firstlen;
71 int kernel;
72 u_int64_t off;
73} *elf_file_t;
74
75static int __elfN(loadimage)(struct preloaded_file *mp, elf_file_t ef, u_int64_t loadaddr);
76static int __elfN(lookup_symbol)(struct preloaded_file *mp, elf_file_t ef, const char* name, Elf_Sym* sym);
77static int __elfN(reloc_ptr)(struct preloaded_file *mp, elf_file_t ef,
78 Elf_Addr p, void *val, size_t len);
79static int __elfN(parse_modmetadata)(struct preloaded_file *mp, elf_file_t ef);
80static symaddr_fn __elfN(symaddr);
81static char *fake_modname(const char *name);
82
83const char *__elfN(kerneltype) = "elf kernel";
84const char *__elfN(moduletype) = "elf module";
85
86u_int64_t __elfN(relocation_offset) = 0;
87
88/*
89 * Attempt to load the file (file) as an ELF module. It will be stored at
90 * (dest), and a pointer to a module structure describing the loaded object
91 * will be saved in (result).
92 */
93int
94__elfN(loadfile)(char *filename, u_int64_t dest, struct preloaded_file **result)
95{
96 struct preloaded_file *fp, *kfp;
97 struct elf_file ef;
98 Elf_Ehdr *ehdr;
99 int err;
30
31#include <sys/param.h>
32#include <sys/exec.h>
33#include <sys/linker.h>
34#include <sys/module.h>
35#include <sys/stdint.h>
36#include <string.h>
37#include <machine/elf.h>
38#include <stand.h>
39#define FREEBSD_ELF
40#include <link.h>
41
42#include "bootstrap.h"
43
44#define COPYOUT(s,d,l) archsw.arch_copyout((vm_offset_t)(s), d, l)
45
46#if defined(__i386__) && __ELF_WORD_SIZE == 64
47#undef ELF_TARG_CLASS
48#undef ELF_TARG_MACH
49#define ELF_TARG_CLASS ELFCLASS64
50#define ELF_TARG_MACH EM_X86_64
51#endif
52
53typedef struct elf_file {
54 Elf_Phdr *ph;
55 Elf_Ehdr *ehdr;
56 Elf_Sym *symtab;
57 Elf_Hashelt *hashtab;
58 Elf_Hashelt nbuckets;
59 Elf_Hashelt nchains;
60 Elf_Hashelt *buckets;
61 Elf_Hashelt *chains;
62 Elf_Rel *rel;
63 size_t relsz;
64 Elf_Rela *rela;
65 size_t relasz;
66 char *strtab;
67 size_t strsz;
68 int fd;
69 caddr_t firstpage;
70 size_t firstlen;
71 int kernel;
72 u_int64_t off;
73} *elf_file_t;
74
75static int __elfN(loadimage)(struct preloaded_file *mp, elf_file_t ef, u_int64_t loadaddr);
76static int __elfN(lookup_symbol)(struct preloaded_file *mp, elf_file_t ef, const char* name, Elf_Sym* sym);
77static int __elfN(reloc_ptr)(struct preloaded_file *mp, elf_file_t ef,
78 Elf_Addr p, void *val, size_t len);
79static int __elfN(parse_modmetadata)(struct preloaded_file *mp, elf_file_t ef);
80static symaddr_fn __elfN(symaddr);
81static char *fake_modname(const char *name);
82
83const char *__elfN(kerneltype) = "elf kernel";
84const char *__elfN(moduletype) = "elf module";
85
86u_int64_t __elfN(relocation_offset) = 0;
87
88/*
89 * Attempt to load the file (file) as an ELF module. It will be stored at
90 * (dest), and a pointer to a module structure describing the loaded object
91 * will be saved in (result).
92 */
93int
94__elfN(loadfile)(char *filename, u_int64_t dest, struct preloaded_file **result)
95{
96 struct preloaded_file *fp, *kfp;
97 struct elf_file ef;
98 Elf_Ehdr *ehdr;
99 int err;
100 u_int pad;
101 ssize_t bytes_read;
102
103 fp = NULL;
104 bzero(&ef, sizeof(struct elf_file));
105
106 /*
107 * Open the image, read and validate the ELF header
108 */
109 if (filename == NULL) /* can't handle nameless */
110 return(EFTYPE);
111 if ((ef.fd = open(filename, O_RDONLY)) == -1)
112 return(errno);
113 ef.firstpage = malloc(PAGE_SIZE);
114 if (ef.firstpage == NULL) {
115 close(ef.fd);
116 return(ENOMEM);
117 }
118 bytes_read = read(ef.fd, ef.firstpage, PAGE_SIZE);
119 ef.firstlen = (size_t)bytes_read;
120 if (bytes_read < 0 || ef.firstlen <= sizeof(Elf_Ehdr)) {
121 err = EFTYPE; /* could be EIO, but may be small file */
122 goto oerr;
123 }
124 ehdr = ef.ehdr = (Elf_Ehdr *)ef.firstpage;
125
126 /* Is it ELF? */
127 if (!IS_ELF(*ehdr)) {
128 err = EFTYPE;
129 goto oerr;
130 }
131 if (ehdr->e_ident[EI_CLASS] != ELF_TARG_CLASS || /* Layout ? */
132 ehdr->e_ident[EI_DATA] != ELF_TARG_DATA ||
133 ehdr->e_ident[EI_VERSION] != EV_CURRENT || /* Version ? */
134 ehdr->e_version != EV_CURRENT ||
135 ehdr->e_machine != ELF_TARG_MACH) { /* Machine ? */
136 err = EFTYPE;
137 goto oerr;
138 }
139
140
141 /*
142 * Check to see what sort of module we are.
143 */
144 kfp = file_findfile(NULL, NULL);
145 if (ehdr->e_type == ET_DYN) {
146 /* Looks like a kld module */
147 if (kfp == NULL) {
148 printf("elf" __XSTRING(__ELF_WORD_SIZE) "_loadfile: can't load module before kernel\n");
149 err = EPERM;
150 goto oerr;
151 }
152 if (strcmp(__elfN(kerneltype), kfp->f_type)) {
153 printf("elf" __XSTRING(__ELF_WORD_SIZE) "_loadfile: can't load module with kernel type '%s'\n", kfp->f_type);
154 err = EPERM;
155 goto oerr;
156 }
157 /* Looks OK, got ahead */
158 ef.kernel = 0;
159
100 ssize_t bytes_read;
101
102 fp = NULL;
103 bzero(&ef, sizeof(struct elf_file));
104
105 /*
106 * Open the image, read and validate the ELF header
107 */
108 if (filename == NULL) /* can't handle nameless */
109 return(EFTYPE);
110 if ((ef.fd = open(filename, O_RDONLY)) == -1)
111 return(errno);
112 ef.firstpage = malloc(PAGE_SIZE);
113 if (ef.firstpage == NULL) {
114 close(ef.fd);
115 return(ENOMEM);
116 }
117 bytes_read = read(ef.fd, ef.firstpage, PAGE_SIZE);
118 ef.firstlen = (size_t)bytes_read;
119 if (bytes_read < 0 || ef.firstlen <= sizeof(Elf_Ehdr)) {
120 err = EFTYPE; /* could be EIO, but may be small file */
121 goto oerr;
122 }
123 ehdr = ef.ehdr = (Elf_Ehdr *)ef.firstpage;
124
125 /* Is it ELF? */
126 if (!IS_ELF(*ehdr)) {
127 err = EFTYPE;
128 goto oerr;
129 }
130 if (ehdr->e_ident[EI_CLASS] != ELF_TARG_CLASS || /* Layout ? */
131 ehdr->e_ident[EI_DATA] != ELF_TARG_DATA ||
132 ehdr->e_ident[EI_VERSION] != EV_CURRENT || /* Version ? */
133 ehdr->e_version != EV_CURRENT ||
134 ehdr->e_machine != ELF_TARG_MACH) { /* Machine ? */
135 err = EFTYPE;
136 goto oerr;
137 }
138
139
140 /*
141 * Check to see what sort of module we are.
142 */
143 kfp = file_findfile(NULL, NULL);
144 if (ehdr->e_type == ET_DYN) {
145 /* Looks like a kld module */
146 if (kfp == NULL) {
147 printf("elf" __XSTRING(__ELF_WORD_SIZE) "_loadfile: can't load module before kernel\n");
148 err = EPERM;
149 goto oerr;
150 }
151 if (strcmp(__elfN(kerneltype), kfp->f_type)) {
152 printf("elf" __XSTRING(__ELF_WORD_SIZE) "_loadfile: can't load module with kernel type '%s'\n", kfp->f_type);
153 err = EPERM;
154 goto oerr;
155 }
156 /* Looks OK, got ahead */
157 ef.kernel = 0;
158
160 /* Page-align the load address */
161 pad = (u_int)dest & PAGE_MASK;
162 if (pad != 0) {
163 pad = PAGE_SIZE - pad;
164 dest += pad;
165 }
166 } else if (ehdr->e_type == ET_EXEC) {
167 /* Looks like a kernel */
168 if (kfp != NULL) {
169 printf("elf" __XSTRING(__ELF_WORD_SIZE) "_loadfile: kernel already loaded\n");
170 err = EPERM;
171 goto oerr;
172 }
173 /*
174 * Calculate destination address based on kernel entrypoint
175 */
159 } else if (ehdr->e_type == ET_EXEC) {
160 /* Looks like a kernel */
161 if (kfp != NULL) {
162 printf("elf" __XSTRING(__ELF_WORD_SIZE) "_loadfile: kernel already loaded\n");
163 err = EPERM;
164 goto oerr;
165 }
166 /*
167 * Calculate destination address based on kernel entrypoint
168 */
176 dest = ehdr->e_entry;
169 dest = (ehdr->e_entry & ~PAGE_MASK);
177 if (dest == 0) {
178 printf("elf" __XSTRING(__ELF_WORD_SIZE) "_loadfile: not a kernel (maybe static binary?)\n");
179 err = EPERM;
180 goto oerr;
181 }
182 ef.kernel = 1;
183
184 } else {
185 err = EFTYPE;
186 goto oerr;
187 }
188
170 if (dest == 0) {
171 printf("elf" __XSTRING(__ELF_WORD_SIZE) "_loadfile: not a kernel (maybe static binary?)\n");
172 err = EPERM;
173 goto oerr;
174 }
175 ef.kernel = 1;
176
177 } else {
178 err = EFTYPE;
179 goto oerr;
180 }
181
182 if (archsw.arch_loadaddr != NULL)
183 dest = archsw.arch_loadaddr(LOAD_ELF, ehdr, dest);
184 else
185 dest = roundup(dest, PAGE_SIZE);
186
189 /*
190 * Ok, we think we should handle this.
191 */
192 fp = file_alloc();
193 if (fp == NULL) {
194 printf("elf" __XSTRING(__ELF_WORD_SIZE) "_loadfile: cannot allocate module info\n");
195 err = EPERM;
196 goto out;
197 }
198 if (ef.kernel)
199 setenv("kernelname", filename, 1);
200 fp->f_name = strdup(filename);
201 fp->f_type = strdup(ef.kernel ? __elfN(kerneltype) : __elfN(moduletype));
202
203#ifdef ELF_VERBOSE
204 if (ef.kernel)
187 /*
188 * Ok, we think we should handle this.
189 */
190 fp = file_alloc();
191 if (fp == NULL) {
192 printf("elf" __XSTRING(__ELF_WORD_SIZE) "_loadfile: cannot allocate module info\n");
193 err = EPERM;
194 goto out;
195 }
196 if (ef.kernel)
197 setenv("kernelname", filename, 1);
198 fp->f_name = strdup(filename);
199 fp->f_type = strdup(ef.kernel ? __elfN(kerneltype) : __elfN(moduletype));
200
201#ifdef ELF_VERBOSE
202 if (ef.kernel)
205 printf("%s entry at 0x%jx\n", filename, (uintmax_t)dest);
203 printf("%s entry at 0x%jx\n", filename, (uintmax_t)ehdr->e_entry);
206#else
207 printf("%s ", filename);
208#endif
209
210 fp->f_size = __elfN(loadimage)(fp, &ef, dest);
211 if (fp->f_size == 0 || fp->f_addr == 0)
212 goto ioerr;
213
214 /* save exec header as metadata */
215 file_addmetadata(fp, MODINFOMD_ELFHDR, sizeof(*ehdr), ehdr);
216
217 /* Load OK, return module pointer */
218 *result = (struct preloaded_file *)fp;
219 err = 0;
220 goto out;
221
222 ioerr:
223 err = EIO;
224 oerr:
225 file_discard(fp);
226 out:
227 if (ef.firstpage)
228 free(ef.firstpage);
229 close(ef.fd);
230 return(err);
231}
232
233/*
234 * With the file (fd) open on the image, and (ehdr) containing
235 * the Elf header, load the image at (off)
236 */
237static int
238__elfN(loadimage)(struct preloaded_file *fp, elf_file_t ef, u_int64_t off)
239{
240 int i;
241 u_int j;
242 Elf_Ehdr *ehdr;
243 Elf_Phdr *phdr, *php;
244 Elf_Shdr *shdr;
245 int ret;
246 vm_offset_t firstaddr;
247 vm_offset_t lastaddr;
248 size_t chunk;
249 ssize_t result;
250 Elf_Addr ssym, esym;
251 Elf_Dyn *dp;
252 Elf_Addr adp;
253 int ndp;
254 int symstrindex;
255 int symtabindex;
256 Elf_Size size;
257 u_int fpcopy;
258
259 dp = NULL;
260 shdr = NULL;
261 ret = 0;
262 firstaddr = lastaddr = 0;
263 ehdr = ef->ehdr;
264 if (ef->kernel) {
265#ifdef __i386__
266#if __ELF_WORD_SIZE == 64
267 off = - (off & 0xffffffffff000000ull);/* x86_64 relocates after locore */
268#else
269 off = - (off & 0xff000000u); /* i386 relocates after locore */
270#endif
271#elif defined(__powerpc__)
272 /*
273 * On the purely virtual memory machines like e500, the kernel is
274 * linked against its final VA range, which is most often not
275 * available at the loader stage, but only after kernel initializes
276 * and completes its VM settings. In such cases we cannot use p_vaddr
277 * field directly to load ELF segments, but put them at some
278 * 'load-time' locations.
279 */
280 if (off & 0xf0000000u) {
281 off = -(off & 0xf0000000u);
282 /*
283 * XXX the physical load address should not be hardcoded. Note
284 * that the Book-E kernel assumes that it's loaded at a 16MB
285 * boundary for now...
286 */
287 off += 0x01000000;
288 ehdr->e_entry += off;
289#ifdef ELF_VERBOSE
290 printf("Converted entry 0x%08x\n", ehdr->e_entry);
291#endif
292 } else
293 off = 0;
294#elif defined(__arm__)
295 if (off & 0xf0000000u) {
296 off = -(off & 0xf0000000u);
297 ehdr->e_entry += off;
298#ifdef ELF_VERBOSE
299 printf("Converted entry 0x%08x\n", ehdr->e_entry);
300#endif
301 } else
302 off = 0;
303#else
304 off = 0; /* other archs use direct mapped kernels */
305#endif
306 __elfN(relocation_offset) = off;
307 }
308 ef->off = off;
309
310 if ((ehdr->e_phoff + ehdr->e_phnum * sizeof(*phdr)) > ef->firstlen) {
311 printf("elf" __XSTRING(__ELF_WORD_SIZE) "_loadimage: program header not within first page\n");
312 goto out;
313 }
314 phdr = (Elf_Phdr *)(ef->firstpage + ehdr->e_phoff);
315
316 for (i = 0; i < ehdr->e_phnum; i++) {
317 /* We want to load PT_LOAD segments only.. */
318 if (phdr[i].p_type != PT_LOAD)
319 continue;
320
321#ifdef ELF_VERBOSE
322 printf("Segment: 0x%lx@0x%lx -> 0x%lx-0x%lx",
323 (long)phdr[i].p_filesz, (long)phdr[i].p_offset,
324 (long)(phdr[i].p_vaddr + off),
325 (long)(phdr[i].p_vaddr + off + phdr[i].p_memsz - 1));
326#else
327 if ((phdr[i].p_flags & PF_W) == 0) {
328 printf("text=0x%lx ", (long)phdr[i].p_filesz);
329 } else {
330 printf("data=0x%lx", (long)phdr[i].p_filesz);
331 if (phdr[i].p_filesz < phdr[i].p_memsz)
332 printf("+0x%lx", (long)(phdr[i].p_memsz -phdr[i].p_filesz));
333 printf(" ");
334 }
335#endif
336 fpcopy = 0;
337 if (ef->firstlen > phdr[i].p_offset) {
338 fpcopy = ef->firstlen - phdr[i].p_offset;
339 archsw.arch_copyin(ef->firstpage + phdr[i].p_offset,
340 phdr[i].p_vaddr + off, fpcopy);
341 }
342 if (phdr[i].p_filesz > fpcopy) {
343 if (kern_pread(ef->fd, phdr[i].p_vaddr + off + fpcopy,
344 phdr[i].p_filesz - fpcopy, phdr[i].p_offset + fpcopy) != 0) {
345 printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
346 "_loadimage: read failed\n");
347 goto out;
348 }
349 }
350 /* clear space from oversized segments; eg: bss */
351 if (phdr[i].p_filesz < phdr[i].p_memsz) {
352#ifdef ELF_VERBOSE
353 printf(" (bss: 0x%lx-0x%lx)",
354 (long)(phdr[i].p_vaddr + off + phdr[i].p_filesz),
355 (long)(phdr[i].p_vaddr + off + phdr[i].p_memsz - 1));
356#endif
357
358 kern_bzero(phdr[i].p_vaddr + off + phdr[i].p_filesz,
359 phdr[i].p_memsz - phdr[i].p_filesz);
360 }
361#ifdef ELF_VERBOSE
362 printf("\n");
363#endif
364
204#else
205 printf("%s ", filename);
206#endif
207
208 fp->f_size = __elfN(loadimage)(fp, &ef, dest);
209 if (fp->f_size == 0 || fp->f_addr == 0)
210 goto ioerr;
211
212 /* save exec header as metadata */
213 file_addmetadata(fp, MODINFOMD_ELFHDR, sizeof(*ehdr), ehdr);
214
215 /* Load OK, return module pointer */
216 *result = (struct preloaded_file *)fp;
217 err = 0;
218 goto out;
219
220 ioerr:
221 err = EIO;
222 oerr:
223 file_discard(fp);
224 out:
225 if (ef.firstpage)
226 free(ef.firstpage);
227 close(ef.fd);
228 return(err);
229}
230
231/*
232 * With the file (fd) open on the image, and (ehdr) containing
233 * the Elf header, load the image at (off)
234 */
235static int
236__elfN(loadimage)(struct preloaded_file *fp, elf_file_t ef, u_int64_t off)
237{
238 int i;
239 u_int j;
240 Elf_Ehdr *ehdr;
241 Elf_Phdr *phdr, *php;
242 Elf_Shdr *shdr;
243 int ret;
244 vm_offset_t firstaddr;
245 vm_offset_t lastaddr;
246 size_t chunk;
247 ssize_t result;
248 Elf_Addr ssym, esym;
249 Elf_Dyn *dp;
250 Elf_Addr adp;
251 int ndp;
252 int symstrindex;
253 int symtabindex;
254 Elf_Size size;
255 u_int fpcopy;
256
257 dp = NULL;
258 shdr = NULL;
259 ret = 0;
260 firstaddr = lastaddr = 0;
261 ehdr = ef->ehdr;
262 if (ef->kernel) {
263#ifdef __i386__
264#if __ELF_WORD_SIZE == 64
265 off = - (off & 0xffffffffff000000ull);/* x86_64 relocates after locore */
266#else
267 off = - (off & 0xff000000u); /* i386 relocates after locore */
268#endif
269#elif defined(__powerpc__)
270 /*
271 * On the purely virtual memory machines like e500, the kernel is
272 * linked against its final VA range, which is most often not
273 * available at the loader stage, but only after kernel initializes
274 * and completes its VM settings. In such cases we cannot use p_vaddr
275 * field directly to load ELF segments, but put them at some
276 * 'load-time' locations.
277 */
278 if (off & 0xf0000000u) {
279 off = -(off & 0xf0000000u);
280 /*
281 * XXX the physical load address should not be hardcoded. Note
282 * that the Book-E kernel assumes that it's loaded at a 16MB
283 * boundary for now...
284 */
285 off += 0x01000000;
286 ehdr->e_entry += off;
287#ifdef ELF_VERBOSE
288 printf("Converted entry 0x%08x\n", ehdr->e_entry);
289#endif
290 } else
291 off = 0;
292#elif defined(__arm__)
293 if (off & 0xf0000000u) {
294 off = -(off & 0xf0000000u);
295 ehdr->e_entry += off;
296#ifdef ELF_VERBOSE
297 printf("Converted entry 0x%08x\n", ehdr->e_entry);
298#endif
299 } else
300 off = 0;
301#else
302 off = 0; /* other archs use direct mapped kernels */
303#endif
304 __elfN(relocation_offset) = off;
305 }
306 ef->off = off;
307
308 if ((ehdr->e_phoff + ehdr->e_phnum * sizeof(*phdr)) > ef->firstlen) {
309 printf("elf" __XSTRING(__ELF_WORD_SIZE) "_loadimage: program header not within first page\n");
310 goto out;
311 }
312 phdr = (Elf_Phdr *)(ef->firstpage + ehdr->e_phoff);
313
314 for (i = 0; i < ehdr->e_phnum; i++) {
315 /* We want to load PT_LOAD segments only.. */
316 if (phdr[i].p_type != PT_LOAD)
317 continue;
318
319#ifdef ELF_VERBOSE
320 printf("Segment: 0x%lx@0x%lx -> 0x%lx-0x%lx",
321 (long)phdr[i].p_filesz, (long)phdr[i].p_offset,
322 (long)(phdr[i].p_vaddr + off),
323 (long)(phdr[i].p_vaddr + off + phdr[i].p_memsz - 1));
324#else
325 if ((phdr[i].p_flags & PF_W) == 0) {
326 printf("text=0x%lx ", (long)phdr[i].p_filesz);
327 } else {
328 printf("data=0x%lx", (long)phdr[i].p_filesz);
329 if (phdr[i].p_filesz < phdr[i].p_memsz)
330 printf("+0x%lx", (long)(phdr[i].p_memsz -phdr[i].p_filesz));
331 printf(" ");
332 }
333#endif
334 fpcopy = 0;
335 if (ef->firstlen > phdr[i].p_offset) {
336 fpcopy = ef->firstlen - phdr[i].p_offset;
337 archsw.arch_copyin(ef->firstpage + phdr[i].p_offset,
338 phdr[i].p_vaddr + off, fpcopy);
339 }
340 if (phdr[i].p_filesz > fpcopy) {
341 if (kern_pread(ef->fd, phdr[i].p_vaddr + off + fpcopy,
342 phdr[i].p_filesz - fpcopy, phdr[i].p_offset + fpcopy) != 0) {
343 printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
344 "_loadimage: read failed\n");
345 goto out;
346 }
347 }
348 /* clear space from oversized segments; eg: bss */
349 if (phdr[i].p_filesz < phdr[i].p_memsz) {
350#ifdef ELF_VERBOSE
351 printf(" (bss: 0x%lx-0x%lx)",
352 (long)(phdr[i].p_vaddr + off + phdr[i].p_filesz),
353 (long)(phdr[i].p_vaddr + off + phdr[i].p_memsz - 1));
354#endif
355
356 kern_bzero(phdr[i].p_vaddr + off + phdr[i].p_filesz,
357 phdr[i].p_memsz - phdr[i].p_filesz);
358 }
359#ifdef ELF_VERBOSE
360 printf("\n");
361#endif
362
363 if (archsw.arch_loadseg != NULL)
364 archsw.arch_loadseg(ehdr, phdr + i, off);
365
365 if (firstaddr == 0 || firstaddr > (phdr[i].p_vaddr + off))
366 firstaddr = phdr[i].p_vaddr + off;
367 if (lastaddr == 0 || lastaddr < (phdr[i].p_vaddr + off + phdr[i].p_memsz))
368 lastaddr = phdr[i].p_vaddr + off + phdr[i].p_memsz;
369 }
370 lastaddr = roundup(lastaddr, sizeof(long));
371
372 /*
373 * Now grab the symbol tables. This isn't easy if we're reading a
374 * .gz file. I think the rule is going to have to be that you must
375 * strip a file to remove symbols before gzipping it so that we do not
376 * try to lseek() on it.
377 */
378 chunk = ehdr->e_shnum * ehdr->e_shentsize;
379 if (chunk == 0 || ehdr->e_shoff == 0)
380 goto nosyms;
381 shdr = alloc_pread(ef->fd, ehdr->e_shoff, chunk);
382 if (shdr == NULL) {
383 printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
384 "_loadimage: failed to read section headers");
385 goto nosyms;
386 }
387 symtabindex = -1;
388 symstrindex = -1;
389 for (i = 0; i < ehdr->e_shnum; i++) {
390 if (shdr[i].sh_type != SHT_SYMTAB)
391 continue;
392 for (j = 0; j < ehdr->e_phnum; j++) {
393 if (phdr[j].p_type != PT_LOAD)
394 continue;
395 if (shdr[i].sh_offset >= phdr[j].p_offset &&
396 (shdr[i].sh_offset + shdr[i].sh_size <=
397 phdr[j].p_offset + phdr[j].p_filesz)) {
398 shdr[i].sh_offset = 0;
399 shdr[i].sh_size = 0;
400 break;
401 }
402 }
403 if (shdr[i].sh_offset == 0 || shdr[i].sh_size == 0)
404 continue; /* alread loaded in a PT_LOAD above */
405 /* Save it for loading below */
406 symtabindex = i;
407 symstrindex = shdr[i].sh_link;
408 }
409 if (symtabindex < 0 || symstrindex < 0)
410 goto nosyms;
411
412 /* Ok, committed to a load. */
413#ifndef ELF_VERBOSE
414 printf("syms=[");
415#endif
416 ssym = lastaddr;
417 for (i = symtabindex; i >= 0; i = symstrindex) {
418#ifdef ELF_VERBOSE
419 char *secname;
420
421 switch(shdr[i].sh_type) {
422 case SHT_SYMTAB: /* Symbol table */
423 secname = "symtab";
424 break;
425 case SHT_STRTAB: /* String table */
426 secname = "strtab";
427 break;
428 default:
429 secname = "WHOA!!";
430 break;
431 }
432#endif
433
434 size = shdr[i].sh_size;
435 archsw.arch_copyin(&size, lastaddr, sizeof(size));
436 lastaddr += sizeof(size);
437
438#ifdef ELF_VERBOSE
439 printf("\n%s: 0x%jx@0x%jx -> 0x%jx-0x%jx", secname,
440 (uintmax_t)shdr[i].sh_size, (uintmax_t)shdr[i].sh_offset,
441 (uintmax_t)lastaddr, (uintmax_t)(lastaddr + shdr[i].sh_size));
442#else
443 if (i == symstrindex)
444 printf("+");
445 printf("0x%lx+0x%lx", (long)sizeof(size), (long)size);
446#endif
447
448 if (lseek(ef->fd, (off_t)shdr[i].sh_offset, SEEK_SET) == -1) {
449 printf("\nelf" __XSTRING(__ELF_WORD_SIZE) "_loadimage: could not seek for symbols - skipped!");
450 lastaddr = ssym;
451 ssym = 0;
452 goto nosyms;
453 }
454 result = archsw.arch_readin(ef->fd, lastaddr, shdr[i].sh_size);
455 if (result < 0 || (size_t)result != shdr[i].sh_size) {
456 printf("\nelf" __XSTRING(__ELF_WORD_SIZE) "_loadimage: could not read symbols - skipped! (%ju != %ju)", (uintmax_t)result,
457 (uintmax_t)shdr[i].sh_size);
458 lastaddr = ssym;
459 ssym = 0;
460 goto nosyms;
461 }
462 /* Reset offsets relative to ssym */
463 lastaddr += shdr[i].sh_size;
464 lastaddr = roundup(lastaddr, sizeof(size));
465 if (i == symtabindex)
466 symtabindex = -1;
467 else if (i == symstrindex)
468 symstrindex = -1;
469 }
470 esym = lastaddr;
471#ifndef ELF_VERBOSE
472 printf("]");
473#endif
474
475 file_addmetadata(fp, MODINFOMD_SSYM, sizeof(ssym), &ssym);
476 file_addmetadata(fp, MODINFOMD_ESYM, sizeof(esym), &esym);
477
478nosyms:
479 printf("\n");
480
481 ret = lastaddr - firstaddr;
482 fp->f_addr = firstaddr;
483
484 php = NULL;
485 for (i = 0; i < ehdr->e_phnum; i++) {
486 if (phdr[i].p_type == PT_DYNAMIC) {
487 php = phdr + i;
488 adp = php->p_vaddr;
489 file_addmetadata(fp, MODINFOMD_DYNAMIC, sizeof(adp), &adp);
490 break;
491 }
492 }
493
494 if (php == NULL) /* this is bad, we cannot get to symbols or _DYNAMIC */
495 goto out;
496
497 ndp = php->p_filesz / sizeof(Elf_Dyn);
498 if (ndp == 0)
499 goto out;
500 dp = malloc(php->p_filesz);
501 if (dp == NULL)
502 goto out;
503 archsw.arch_copyout(php->p_vaddr + off, dp, php->p_filesz);
504
505 ef->strsz = 0;
506 for (i = 0; i < ndp; i++) {
507 if (dp[i].d_tag == 0)
508 break;
509 switch (dp[i].d_tag) {
510 case DT_HASH:
511 ef->hashtab = (Elf_Hashelt*)(uintptr_t)(dp[i].d_un.d_ptr + off);
512 break;
513 case DT_STRTAB:
514 ef->strtab = (char *)(uintptr_t)(dp[i].d_un.d_ptr + off);
515 break;
516 case DT_STRSZ:
517 ef->strsz = dp[i].d_un.d_val;
518 break;
519 case DT_SYMTAB:
520 ef->symtab = (Elf_Sym*)(uintptr_t)(dp[i].d_un.d_ptr + off);
521 break;
522 case DT_REL:
523 ef->rel = (Elf_Rel *)(uintptr_t)(dp[i].d_un.d_ptr + off);
524 break;
525 case DT_RELSZ:
526 ef->relsz = dp[i].d_un.d_val;
527 break;
528 case DT_RELA:
529 ef->rela = (Elf_Rela *)(uintptr_t)(dp[i].d_un.d_ptr + off);
530 break;
531 case DT_RELASZ:
532 ef->relasz = dp[i].d_un.d_val;
533 break;
534 default:
535 break;
536 }
537 }
538 if (ef->hashtab == NULL || ef->symtab == NULL ||
539 ef->strtab == NULL || ef->strsz == 0)
540 goto out;
541 COPYOUT(ef->hashtab, &ef->nbuckets, sizeof(ef->nbuckets));
542 COPYOUT(ef->hashtab + 1, &ef->nchains, sizeof(ef->nchains));
543 ef->buckets = ef->hashtab + 2;
544 ef->chains = ef->buckets + ef->nbuckets;
545 if (__elfN(parse_modmetadata)(fp, ef) == 0)
546 goto out;
547
548 if (ef->kernel) /* kernel must not depend on anything */
549 goto out;
550
551out:
552 if (dp)
553 free(dp);
554 if (shdr)
555 free(shdr);
556 return ret;
557}
558
559static char invalid_name[] = "bad";
560
561char *
562fake_modname(const char *name)
563{
564 const char *sp, *ep;
565 char *fp;
566 size_t len;
567
568 sp = strrchr(name, '/');
569 if (sp)
570 sp++;
571 else
572 sp = name;
573 ep = strrchr(name, '.');
574 if (ep) {
575 if (ep == name) {
576 sp = invalid_name;
577 ep = invalid_name + sizeof(invalid_name) - 1;
578 }
579 } else
580 ep = name + strlen(name);
581 len = ep - sp;
582 fp = malloc(len + 1);
583 if (fp == NULL)
584 return NULL;
585 memcpy(fp, sp, len);
586 fp[len] = '\0';
587 return fp;
588}
589
590#if defined(__i386__) && __ELF_WORD_SIZE == 64
591struct mod_metadata64 {
592 int md_version; /* structure version MDTV_* */
593 int md_type; /* type of entry MDT_* */
594 u_int64_t md_data; /* specific data */
595 u_int64_t md_cval; /* common string label */
596};
597#endif
598
599int
600__elfN(parse_modmetadata)(struct preloaded_file *fp, elf_file_t ef)
601{
602 struct mod_metadata md;
603#if defined(__i386__) && __ELF_WORD_SIZE == 64
604 struct mod_metadata64 md64;
605#endif
606 struct mod_depend *mdepend;
607 struct mod_version mver;
608 Elf_Sym sym;
609 char *s;
610 int error, modcnt, minfolen;
611 Elf_Addr v, p, p_stop;
612
613 if (__elfN(lookup_symbol)(fp, ef, "__start_set_modmetadata_set", &sym) != 0)
614 return ENOENT;
615 p = sym.st_value + ef->off;
616 if (__elfN(lookup_symbol)(fp, ef, "__stop_set_modmetadata_set", &sym) != 0)
617 return ENOENT;
618 p_stop = sym.st_value + ef->off;
619
620 modcnt = 0;
621 while (p < p_stop) {
622 COPYOUT(p, &v, sizeof(v));
623 error = __elfN(reloc_ptr)(fp, ef, p, &v, sizeof(v));
624 if (error == EOPNOTSUPP)
625 v += ef->off;
626 else if (error != 0)
627 return (error);
628#if defined(__i386__) && __ELF_WORD_SIZE == 64
629 COPYOUT(v, &md64, sizeof(md64));
630 error = __elfN(reloc_ptr)(fp, ef, v, &md64, sizeof(md64));
631 if (error == EOPNOTSUPP) {
632 md64.md_cval += ef->off;
633 md64.md_data += ef->off;
634 } else if (error != 0)
635 return (error);
636 md.md_version = md64.md_version;
637 md.md_type = md64.md_type;
638 md.md_cval = (const char *)(uintptr_t)md64.md_cval;
639 md.md_data = (void *)(uintptr_t)md64.md_data;
640#else
641 COPYOUT(v, &md, sizeof(md));
642 error = __elfN(reloc_ptr)(fp, ef, v, &md, sizeof(md));
643 if (error == EOPNOTSUPP) {
644 md.md_cval += ef->off;
645 md.md_data += ef->off;
646 } else if (error != 0)
647 return (error);
648#endif
649 p += sizeof(Elf_Addr);
650 switch(md.md_type) {
651 case MDT_DEPEND:
652 if (ef->kernel) /* kernel must not depend on anything */
653 break;
654 s = strdupout((vm_offset_t)md.md_cval);
655 minfolen = sizeof(*mdepend) + strlen(s) + 1;
656 mdepend = malloc(minfolen);
657 if (mdepend == NULL)
658 return ENOMEM;
659 COPYOUT((vm_offset_t)md.md_data, mdepend, sizeof(*mdepend));
660 strcpy((char*)(mdepend + 1), s);
661 free(s);
662 file_addmetadata(fp, MODINFOMD_DEPLIST, minfolen, mdepend);
663 free(mdepend);
664 break;
665 case MDT_VERSION:
666 s = strdupout((vm_offset_t)md.md_cval);
667 COPYOUT((vm_offset_t)md.md_data, &mver, sizeof(mver));
668 file_addmodule(fp, s, mver.mv_version, NULL);
669 free(s);
670 modcnt++;
671 break;
672 }
673 }
674 if (modcnt == 0) {
675 s = fake_modname(fp->f_name);
676 file_addmodule(fp, s, 1, NULL);
677 free(s);
678 }
679 return 0;
680}
681
682static unsigned long
683elf_hash(const char *name)
684{
685 const unsigned char *p = (const unsigned char *) name;
686 unsigned long h = 0;
687 unsigned long g;
688
689 while (*p != '\0') {
690 h = (h << 4) + *p++;
691 if ((g = h & 0xf0000000) != 0)
692 h ^= g >> 24;
693 h &= ~g;
694 }
695 return h;
696}
697
698static const char __elfN(bad_symtable)[] = "elf" __XSTRING(__ELF_WORD_SIZE) "_lookup_symbol: corrupt symbol table\n";
699int
700__elfN(lookup_symbol)(struct preloaded_file *fp, elf_file_t ef, const char* name,
701 Elf_Sym *symp)
702{
703 Elf_Hashelt symnum;
704 Elf_Sym sym;
705 char *strp;
706 unsigned long hash;
707
708 hash = elf_hash(name);
709 COPYOUT(&ef->buckets[hash % ef->nbuckets], &symnum, sizeof(symnum));
710
711 while (symnum != STN_UNDEF) {
712 if (symnum >= ef->nchains) {
713 printf(__elfN(bad_symtable));
714 return ENOENT;
715 }
716
717 COPYOUT(ef->symtab + symnum, &sym, sizeof(sym));
718 if (sym.st_name == 0) {
719 printf(__elfN(bad_symtable));
720 return ENOENT;
721 }
722
723 strp = strdupout((vm_offset_t)(ef->strtab + sym.st_name));
724 if (strcmp(name, strp) == 0) {
725 free(strp);
726 if (sym.st_shndx != SHN_UNDEF ||
727 (sym.st_value != 0 &&
728 ELF_ST_TYPE(sym.st_info) == STT_FUNC)) {
729 *symp = sym;
730 return 0;
731 }
732 return ENOENT;
733 }
734 free(strp);
735 COPYOUT(&ef->chains[symnum], &symnum, sizeof(symnum));
736 }
737 return ENOENT;
738}
739
740/*
741 * Apply any intra-module relocations to the value. p is the load address
742 * of the value and val/len is the value to be modified. This does NOT modify
743 * the image in-place, because this is done by kern_linker later on.
744 *
745 * Returns EOPNOTSUPP if no relocation method is supplied.
746 */
747static int
748__elfN(reloc_ptr)(struct preloaded_file *mp, elf_file_t ef,
749 Elf_Addr p, void *val, size_t len)
750{
751 size_t n;
752 Elf_Rela a;
753 Elf_Rel r;
754 int error;
755
756 /*
757 * The kernel is already relocated, but we still want to apply
758 * offset adjustments.
759 */
760 if (ef->kernel)
761 return (EOPNOTSUPP);
762
763 for (n = 0; n < ef->relsz / sizeof(r); n++) {
764 COPYOUT(ef->rel + n, &r, sizeof(r));
765
766 error = __elfN(reloc)(ef, __elfN(symaddr), &r, ELF_RELOC_REL,
767 ef->off, p, val, len);
768 if (error != 0)
769 return (error);
770 }
771 for (n = 0; n < ef->relasz / sizeof(a); n++) {
772 COPYOUT(ef->rela + n, &a, sizeof(a));
773
774 error = __elfN(reloc)(ef, __elfN(symaddr), &a, ELF_RELOC_RELA,
775 ef->off, p, val, len);
776 if (error != 0)
777 return (error);
778 }
779
780 return (0);
781}
782
783static Elf_Addr
784__elfN(symaddr)(struct elf_file *ef, Elf_Size symidx)
785{
786
787 /* Symbol lookup by index not required here. */
788 return (0);
789}
366 if (firstaddr == 0 || firstaddr > (phdr[i].p_vaddr + off))
367 firstaddr = phdr[i].p_vaddr + off;
368 if (lastaddr == 0 || lastaddr < (phdr[i].p_vaddr + off + phdr[i].p_memsz))
369 lastaddr = phdr[i].p_vaddr + off + phdr[i].p_memsz;
370 }
371 lastaddr = roundup(lastaddr, sizeof(long));
372
373 /*
374 * Now grab the symbol tables. This isn't easy if we're reading a
375 * .gz file. I think the rule is going to have to be that you must
376 * strip a file to remove symbols before gzipping it so that we do not
377 * try to lseek() on it.
378 */
379 chunk = ehdr->e_shnum * ehdr->e_shentsize;
380 if (chunk == 0 || ehdr->e_shoff == 0)
381 goto nosyms;
382 shdr = alloc_pread(ef->fd, ehdr->e_shoff, chunk);
383 if (shdr == NULL) {
384 printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
385 "_loadimage: failed to read section headers");
386 goto nosyms;
387 }
388 symtabindex = -1;
389 symstrindex = -1;
390 for (i = 0; i < ehdr->e_shnum; i++) {
391 if (shdr[i].sh_type != SHT_SYMTAB)
392 continue;
393 for (j = 0; j < ehdr->e_phnum; j++) {
394 if (phdr[j].p_type != PT_LOAD)
395 continue;
396 if (shdr[i].sh_offset >= phdr[j].p_offset &&
397 (shdr[i].sh_offset + shdr[i].sh_size <=
398 phdr[j].p_offset + phdr[j].p_filesz)) {
399 shdr[i].sh_offset = 0;
400 shdr[i].sh_size = 0;
401 break;
402 }
403 }
404 if (shdr[i].sh_offset == 0 || shdr[i].sh_size == 0)
405 continue; /* alread loaded in a PT_LOAD above */
406 /* Save it for loading below */
407 symtabindex = i;
408 symstrindex = shdr[i].sh_link;
409 }
410 if (symtabindex < 0 || symstrindex < 0)
411 goto nosyms;
412
413 /* Ok, committed to a load. */
414#ifndef ELF_VERBOSE
415 printf("syms=[");
416#endif
417 ssym = lastaddr;
418 for (i = symtabindex; i >= 0; i = symstrindex) {
419#ifdef ELF_VERBOSE
420 char *secname;
421
422 switch(shdr[i].sh_type) {
423 case SHT_SYMTAB: /* Symbol table */
424 secname = "symtab";
425 break;
426 case SHT_STRTAB: /* String table */
427 secname = "strtab";
428 break;
429 default:
430 secname = "WHOA!!";
431 break;
432 }
433#endif
434
435 size = shdr[i].sh_size;
436 archsw.arch_copyin(&size, lastaddr, sizeof(size));
437 lastaddr += sizeof(size);
438
439#ifdef ELF_VERBOSE
440 printf("\n%s: 0x%jx@0x%jx -> 0x%jx-0x%jx", secname,
441 (uintmax_t)shdr[i].sh_size, (uintmax_t)shdr[i].sh_offset,
442 (uintmax_t)lastaddr, (uintmax_t)(lastaddr + shdr[i].sh_size));
443#else
444 if (i == symstrindex)
445 printf("+");
446 printf("0x%lx+0x%lx", (long)sizeof(size), (long)size);
447#endif
448
449 if (lseek(ef->fd, (off_t)shdr[i].sh_offset, SEEK_SET) == -1) {
450 printf("\nelf" __XSTRING(__ELF_WORD_SIZE) "_loadimage: could not seek for symbols - skipped!");
451 lastaddr = ssym;
452 ssym = 0;
453 goto nosyms;
454 }
455 result = archsw.arch_readin(ef->fd, lastaddr, shdr[i].sh_size);
456 if (result < 0 || (size_t)result != shdr[i].sh_size) {
457 printf("\nelf" __XSTRING(__ELF_WORD_SIZE) "_loadimage: could not read symbols - skipped! (%ju != %ju)", (uintmax_t)result,
458 (uintmax_t)shdr[i].sh_size);
459 lastaddr = ssym;
460 ssym = 0;
461 goto nosyms;
462 }
463 /* Reset offsets relative to ssym */
464 lastaddr += shdr[i].sh_size;
465 lastaddr = roundup(lastaddr, sizeof(size));
466 if (i == symtabindex)
467 symtabindex = -1;
468 else if (i == symstrindex)
469 symstrindex = -1;
470 }
471 esym = lastaddr;
472#ifndef ELF_VERBOSE
473 printf("]");
474#endif
475
476 file_addmetadata(fp, MODINFOMD_SSYM, sizeof(ssym), &ssym);
477 file_addmetadata(fp, MODINFOMD_ESYM, sizeof(esym), &esym);
478
479nosyms:
480 printf("\n");
481
482 ret = lastaddr - firstaddr;
483 fp->f_addr = firstaddr;
484
485 php = NULL;
486 for (i = 0; i < ehdr->e_phnum; i++) {
487 if (phdr[i].p_type == PT_DYNAMIC) {
488 php = phdr + i;
489 adp = php->p_vaddr;
490 file_addmetadata(fp, MODINFOMD_DYNAMIC, sizeof(adp), &adp);
491 break;
492 }
493 }
494
495 if (php == NULL) /* this is bad, we cannot get to symbols or _DYNAMIC */
496 goto out;
497
498 ndp = php->p_filesz / sizeof(Elf_Dyn);
499 if (ndp == 0)
500 goto out;
501 dp = malloc(php->p_filesz);
502 if (dp == NULL)
503 goto out;
504 archsw.arch_copyout(php->p_vaddr + off, dp, php->p_filesz);
505
506 ef->strsz = 0;
507 for (i = 0; i < ndp; i++) {
508 if (dp[i].d_tag == 0)
509 break;
510 switch (dp[i].d_tag) {
511 case DT_HASH:
512 ef->hashtab = (Elf_Hashelt*)(uintptr_t)(dp[i].d_un.d_ptr + off);
513 break;
514 case DT_STRTAB:
515 ef->strtab = (char *)(uintptr_t)(dp[i].d_un.d_ptr + off);
516 break;
517 case DT_STRSZ:
518 ef->strsz = dp[i].d_un.d_val;
519 break;
520 case DT_SYMTAB:
521 ef->symtab = (Elf_Sym*)(uintptr_t)(dp[i].d_un.d_ptr + off);
522 break;
523 case DT_REL:
524 ef->rel = (Elf_Rel *)(uintptr_t)(dp[i].d_un.d_ptr + off);
525 break;
526 case DT_RELSZ:
527 ef->relsz = dp[i].d_un.d_val;
528 break;
529 case DT_RELA:
530 ef->rela = (Elf_Rela *)(uintptr_t)(dp[i].d_un.d_ptr + off);
531 break;
532 case DT_RELASZ:
533 ef->relasz = dp[i].d_un.d_val;
534 break;
535 default:
536 break;
537 }
538 }
539 if (ef->hashtab == NULL || ef->symtab == NULL ||
540 ef->strtab == NULL || ef->strsz == 0)
541 goto out;
542 COPYOUT(ef->hashtab, &ef->nbuckets, sizeof(ef->nbuckets));
543 COPYOUT(ef->hashtab + 1, &ef->nchains, sizeof(ef->nchains));
544 ef->buckets = ef->hashtab + 2;
545 ef->chains = ef->buckets + ef->nbuckets;
546 if (__elfN(parse_modmetadata)(fp, ef) == 0)
547 goto out;
548
549 if (ef->kernel) /* kernel must not depend on anything */
550 goto out;
551
552out:
553 if (dp)
554 free(dp);
555 if (shdr)
556 free(shdr);
557 return ret;
558}
559
560static char invalid_name[] = "bad";
561
562char *
563fake_modname(const char *name)
564{
565 const char *sp, *ep;
566 char *fp;
567 size_t len;
568
569 sp = strrchr(name, '/');
570 if (sp)
571 sp++;
572 else
573 sp = name;
574 ep = strrchr(name, '.');
575 if (ep) {
576 if (ep == name) {
577 sp = invalid_name;
578 ep = invalid_name + sizeof(invalid_name) - 1;
579 }
580 } else
581 ep = name + strlen(name);
582 len = ep - sp;
583 fp = malloc(len + 1);
584 if (fp == NULL)
585 return NULL;
586 memcpy(fp, sp, len);
587 fp[len] = '\0';
588 return fp;
589}
590
591#if defined(__i386__) && __ELF_WORD_SIZE == 64
592struct mod_metadata64 {
593 int md_version; /* structure version MDTV_* */
594 int md_type; /* type of entry MDT_* */
595 u_int64_t md_data; /* specific data */
596 u_int64_t md_cval; /* common string label */
597};
598#endif
599
600int
601__elfN(parse_modmetadata)(struct preloaded_file *fp, elf_file_t ef)
602{
603 struct mod_metadata md;
604#if defined(__i386__) && __ELF_WORD_SIZE == 64
605 struct mod_metadata64 md64;
606#endif
607 struct mod_depend *mdepend;
608 struct mod_version mver;
609 Elf_Sym sym;
610 char *s;
611 int error, modcnt, minfolen;
612 Elf_Addr v, p, p_stop;
613
614 if (__elfN(lookup_symbol)(fp, ef, "__start_set_modmetadata_set", &sym) != 0)
615 return ENOENT;
616 p = sym.st_value + ef->off;
617 if (__elfN(lookup_symbol)(fp, ef, "__stop_set_modmetadata_set", &sym) != 0)
618 return ENOENT;
619 p_stop = sym.st_value + ef->off;
620
621 modcnt = 0;
622 while (p < p_stop) {
623 COPYOUT(p, &v, sizeof(v));
624 error = __elfN(reloc_ptr)(fp, ef, p, &v, sizeof(v));
625 if (error == EOPNOTSUPP)
626 v += ef->off;
627 else if (error != 0)
628 return (error);
629#if defined(__i386__) && __ELF_WORD_SIZE == 64
630 COPYOUT(v, &md64, sizeof(md64));
631 error = __elfN(reloc_ptr)(fp, ef, v, &md64, sizeof(md64));
632 if (error == EOPNOTSUPP) {
633 md64.md_cval += ef->off;
634 md64.md_data += ef->off;
635 } else if (error != 0)
636 return (error);
637 md.md_version = md64.md_version;
638 md.md_type = md64.md_type;
639 md.md_cval = (const char *)(uintptr_t)md64.md_cval;
640 md.md_data = (void *)(uintptr_t)md64.md_data;
641#else
642 COPYOUT(v, &md, sizeof(md));
643 error = __elfN(reloc_ptr)(fp, ef, v, &md, sizeof(md));
644 if (error == EOPNOTSUPP) {
645 md.md_cval += ef->off;
646 md.md_data += ef->off;
647 } else if (error != 0)
648 return (error);
649#endif
650 p += sizeof(Elf_Addr);
651 switch(md.md_type) {
652 case MDT_DEPEND:
653 if (ef->kernel) /* kernel must not depend on anything */
654 break;
655 s = strdupout((vm_offset_t)md.md_cval);
656 minfolen = sizeof(*mdepend) + strlen(s) + 1;
657 mdepend = malloc(minfolen);
658 if (mdepend == NULL)
659 return ENOMEM;
660 COPYOUT((vm_offset_t)md.md_data, mdepend, sizeof(*mdepend));
661 strcpy((char*)(mdepend + 1), s);
662 free(s);
663 file_addmetadata(fp, MODINFOMD_DEPLIST, minfolen, mdepend);
664 free(mdepend);
665 break;
666 case MDT_VERSION:
667 s = strdupout((vm_offset_t)md.md_cval);
668 COPYOUT((vm_offset_t)md.md_data, &mver, sizeof(mver));
669 file_addmodule(fp, s, mver.mv_version, NULL);
670 free(s);
671 modcnt++;
672 break;
673 }
674 }
675 if (modcnt == 0) {
676 s = fake_modname(fp->f_name);
677 file_addmodule(fp, s, 1, NULL);
678 free(s);
679 }
680 return 0;
681}
682
683static unsigned long
684elf_hash(const char *name)
685{
686 const unsigned char *p = (const unsigned char *) name;
687 unsigned long h = 0;
688 unsigned long g;
689
690 while (*p != '\0') {
691 h = (h << 4) + *p++;
692 if ((g = h & 0xf0000000) != 0)
693 h ^= g >> 24;
694 h &= ~g;
695 }
696 return h;
697}
698
699static const char __elfN(bad_symtable)[] = "elf" __XSTRING(__ELF_WORD_SIZE) "_lookup_symbol: corrupt symbol table\n";
700int
701__elfN(lookup_symbol)(struct preloaded_file *fp, elf_file_t ef, const char* name,
702 Elf_Sym *symp)
703{
704 Elf_Hashelt symnum;
705 Elf_Sym sym;
706 char *strp;
707 unsigned long hash;
708
709 hash = elf_hash(name);
710 COPYOUT(&ef->buckets[hash % ef->nbuckets], &symnum, sizeof(symnum));
711
712 while (symnum != STN_UNDEF) {
713 if (symnum >= ef->nchains) {
714 printf(__elfN(bad_symtable));
715 return ENOENT;
716 }
717
718 COPYOUT(ef->symtab + symnum, &sym, sizeof(sym));
719 if (sym.st_name == 0) {
720 printf(__elfN(bad_symtable));
721 return ENOENT;
722 }
723
724 strp = strdupout((vm_offset_t)(ef->strtab + sym.st_name));
725 if (strcmp(name, strp) == 0) {
726 free(strp);
727 if (sym.st_shndx != SHN_UNDEF ||
728 (sym.st_value != 0 &&
729 ELF_ST_TYPE(sym.st_info) == STT_FUNC)) {
730 *symp = sym;
731 return 0;
732 }
733 return ENOENT;
734 }
735 free(strp);
736 COPYOUT(&ef->chains[symnum], &symnum, sizeof(symnum));
737 }
738 return ENOENT;
739}
740
741/*
742 * Apply any intra-module relocations to the value. p is the load address
743 * of the value and val/len is the value to be modified. This does NOT modify
744 * the image in-place, because this is done by kern_linker later on.
745 *
746 * Returns EOPNOTSUPP if no relocation method is supplied.
747 */
748static int
749__elfN(reloc_ptr)(struct preloaded_file *mp, elf_file_t ef,
750 Elf_Addr p, void *val, size_t len)
751{
752 size_t n;
753 Elf_Rela a;
754 Elf_Rel r;
755 int error;
756
757 /*
758 * The kernel is already relocated, but we still want to apply
759 * offset adjustments.
760 */
761 if (ef->kernel)
762 return (EOPNOTSUPP);
763
764 for (n = 0; n < ef->relsz / sizeof(r); n++) {
765 COPYOUT(ef->rel + n, &r, sizeof(r));
766
767 error = __elfN(reloc)(ef, __elfN(symaddr), &r, ELF_RELOC_REL,
768 ef->off, p, val, len);
769 if (error != 0)
770 return (error);
771 }
772 for (n = 0; n < ef->relasz / sizeof(a); n++) {
773 COPYOUT(ef->rela + n, &a, sizeof(a));
774
775 error = __elfN(reloc)(ef, __elfN(symaddr), &a, ELF_RELOC_RELA,
776 ef->off, p, val, len);
777 if (error != 0)
778 return (error);
779 }
780
781 return (0);
782}
783
784static Elf_Addr
785__elfN(symaddr)(struct elf_file *ef, Elf_Size symidx)
786{
787
788 /* Symbol lookup by index not required here. */
789 return (0);
790}