kernel: Handle zero-length ELF sections better
[dragonfly.git] / sys / boot / common / load_elf_obj.c
blob91da6369886befb0941a6b04a8d868e6068a35b9
1 /*-
2 * Copyright (c) 2004 Ian Dowse <iedowse@freebsd.org>
3 * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
4 * Copyright (c) 1998 Peter Wemm <peter@freebsd.org>
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
28 * $FreeBSD: src/sys/boot/common/load_elf_obj.c,v 1.2 2005/12/18 04:52:35 marcel Exp $
31 #include <sys/param.h>
32 #include <sys/exec.h>
33 #include <sys/linker.h>
34 #include <sys/module.h>
35 #include <inttypes.h>
36 #include <string.h>
37 #include <machine/elf.h>
38 #include <stand.h>
39 #define FREEBSD_ELF
40 #include <link.h>
42 #include "bootstrap.h"
44 #define COPYOUT(s,d,l) archsw.arch_copyout((vm_offset_t)(s), d, l)
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
53 typedef struct elf_file {
54 Elf_Ehdr hdr;
55 Elf_Shdr *e_shdr;
57 int symtabindex; /* Index of symbol table */
58 int shstrindex; /* Index of section name string table */
60 int fd;
61 vm_offset_t off;
62 } *elf_file_t;
64 static int __elfN(obj_loadimage)(struct preloaded_file *mp, elf_file_t ef,
65 u_int64_t loadaddr);
66 static int __elfN(obj_lookup_set)(struct preloaded_file *mp, elf_file_t ef,
67 const char *name, Elf_Addr *startp, Elf_Addr *stopp, int *countp);
68 static int __elfN(obj_reloc_ptr)(struct preloaded_file *mp, elf_file_t ef,
69 Elf_Addr p, void *val, size_t len);
70 static int __elfN(obj_parse_modmetadata)(struct preloaded_file *mp,
71 elf_file_t ef);
72 static Elf_Addr __elfN(obj_symaddr)(struct elf_file *ef, Elf_Size symidx);
74 const char *__elfN(obj_kerneltype) = "elf kernel";
75 const char *__elfN(obj_moduletype) = "elf obj module";
78 * Attempt to load the file (file) as an ELF module. It will be stored at
79 * (dest), and a pointer to a module structure describing the loaded object
80 * will be saved in (result).
82 int
83 __elfN(obj_loadfile)(char *filename, u_int64_t dest,
84 struct preloaded_file **result)
86 struct preloaded_file *fp, *kfp;
87 struct elf_file ef;
88 Elf_Ehdr *hdr;
89 int err;
90 ssize_t bytes_read;
92 fp = NULL;
93 bzero(&ef, sizeof(struct elf_file));
96 * Open the image, read and validate the ELF header
98 if (filename == NULL) /* can't handle nameless */
99 return(EFTYPE);
100 if ((ef.fd = rel_open(filename, NULL, O_RDONLY)) == -1)
101 return(errno);
103 hdr = &ef.hdr;
104 bytes_read = read(ef.fd, hdr, sizeof(*hdr));
105 if (bytes_read != sizeof(*hdr)) {
106 err = EFTYPE; /* could be EIO, but may be small file */
107 goto oerr;
110 /* Is it ELF? */
111 if (!IS_ELF(*hdr)) {
112 err = EFTYPE;
113 goto oerr;
115 if (hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS || /* Layout ? */
116 hdr->e_ident[EI_DATA] != ELF_TARG_DATA ||
117 hdr->e_ident[EI_VERSION] != EV_CURRENT || /* Version ? */
118 hdr->e_version != EV_CURRENT ||
119 hdr->e_machine != ELF_TARG_MACH || /* Machine ? */
120 hdr->e_type != ET_REL) {
121 err = EFTYPE;
122 goto oerr;
125 if (hdr->e_shnum * hdr->e_shentsize == 0 || hdr->e_shoff == 0 ||
126 hdr->e_shentsize != sizeof(Elf_Shdr)) {
127 err = EFTYPE;
128 goto oerr;
131 kfp = file_findfile(NULL, NULL);
132 if (kfp == NULL) {
133 printf("elf" __XSTRING(__ELF_WORD_SIZE)
134 "_obj_loadfile: can't load module before kernel\n");
135 err = EPERM;
136 goto oerr;
138 if (strcmp(__elfN(obj_kerneltype), kfp->f_type)) {
139 printf("elf" __XSTRING(__ELF_WORD_SIZE)
140 "_obj_loadfile: can't load module with kernel type '%s'\n",
141 kfp->f_type);
142 err = EPERM;
143 goto oerr;
146 /* Page-align the load address */
147 dest = roundup(dest, PAGE_SIZE);
150 * Ok, we think we should handle this.
152 fp = file_alloc();
153 if (fp == NULL) {
154 printf("elf" __XSTRING(__ELF_WORD_SIZE)
155 "_obj_loadfile: cannot allocate module info\n");
156 err = EPERM;
157 goto out;
159 fp->f_name = strdup(filename);
160 fp->f_type = strdup(__elfN(obj_moduletype));
162 printf("%s ", filename);
164 fp->f_size = __elfN(obj_loadimage)(fp, &ef, dest);
165 if (fp->f_size == 0 || fp->f_addr == 0)
166 goto ioerr;
168 /* save exec header as metadata */
169 file_addmetadata(fp, MODINFOMD_ELFHDR, sizeof(*hdr), hdr);
171 /* Load OK, return module pointer */
172 *result = (struct preloaded_file *)fp;
173 err = 0;
174 goto out;
176 ioerr:
177 err = EIO;
178 oerr:
179 file_discard(fp);
180 out:
181 close(ef.fd);
182 if (ef.e_shdr != NULL)
183 free(ef.e_shdr);
185 return(err);
189 * With the file (fd) open on the image, and (ehdr) containing
190 * the Elf header, load the image at (off)
192 static int
193 __elfN(obj_loadimage)(struct preloaded_file *fp, elf_file_t ef, u_int64_t off)
195 Elf_Ehdr *hdr;
196 Elf_Shdr *shdr;
197 vm_offset_t firstaddr, lastaddr;
198 int i, nsym, res, ret, shdrbytes, symstrindex;
200 ret = 0;
201 firstaddr = lastaddr = (vm_offset_t)off;
202 hdr = &ef->hdr;
203 ef->off = (vm_offset_t)off;
205 /* Read in the section headers. */
206 shdrbytes = hdr->e_shnum * hdr->e_shentsize;
207 shdr = alloc_pread(ef->fd, (off_t)hdr->e_shoff, shdrbytes);
208 if (shdr == NULL) {
209 printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
210 "_obj_loadimage: read section headers failed\n");
211 goto out;
213 ef->e_shdr = shdr;
216 * Decide where to load everything, but don't read it yet.
217 * We store the load address as a non-zero sh_addr value.
218 * Start with the code/data and bss.
220 for (i = 0; i < hdr->e_shnum; i++)
221 shdr[i].sh_addr = 0;
222 for (i = 0; i < hdr->e_shnum; i++) {
223 if (shdr[i].sh_size == 0)
224 continue;
225 switch (shdr[i].sh_type) {
226 case SHT_PROGBITS:
227 case SHT_NOBITS:
228 lastaddr = roundup(lastaddr, shdr[i].sh_addralign);
229 shdr[i].sh_addr = (Elf_Addr)lastaddr;
230 lastaddr += shdr[i].sh_size;
231 break;
235 /* Symbols. */
236 nsym = 0;
237 for (i = 0; i < hdr->e_shnum; i++) {
238 switch (shdr[i].sh_type) {
239 case SHT_SYMTAB:
240 nsym++;
241 ef->symtabindex = i;
242 shdr[i].sh_addr = (Elf_Addr)lastaddr;
243 lastaddr += shdr[i].sh_size;
244 break;
247 if (nsym != 1) {
248 printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
249 "_obj_loadimage: file has no valid symbol table\n");
250 goto out;
252 lastaddr = roundup(lastaddr, shdr[ef->symtabindex].sh_addralign);
253 shdr[ef->symtabindex].sh_addr = (Elf_Addr)lastaddr;
254 lastaddr += shdr[ef->symtabindex].sh_size;
256 symstrindex = shdr[ef->symtabindex].sh_link;
257 if (symstrindex < 0 || symstrindex >= hdr->e_shnum ||
258 shdr[symstrindex].sh_type != SHT_STRTAB) {
259 printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
260 "_obj_loadimage: file has invalid symbol strings\n");
261 goto out;
263 lastaddr = roundup(lastaddr, shdr[symstrindex].sh_addralign);
264 shdr[symstrindex].sh_addr = (Elf_Addr)lastaddr;
265 lastaddr += shdr[symstrindex].sh_size;
267 /* Section names. */
268 if (hdr->e_shstrndx == 0 || hdr->e_shstrndx >= hdr->e_shnum ||
269 shdr[hdr->e_shstrndx].sh_type != SHT_STRTAB) {
270 printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
271 "_obj_loadimage: file has no section names\n");
272 goto out;
274 ef->shstrindex = hdr->e_shstrndx;
275 lastaddr = roundup(lastaddr, shdr[ef->shstrindex].sh_addralign);
276 shdr[ef->shstrindex].sh_addr = (Elf_Addr)lastaddr;
277 lastaddr += shdr[ef->shstrindex].sh_size;
279 /* Relocation tables. */
280 for (i = 0; i < hdr->e_shnum; i++) {
281 switch (shdr[i].sh_type) {
282 case SHT_REL:
283 case SHT_RELA:
284 lastaddr = roundup(lastaddr, shdr[i].sh_addralign);
285 shdr[i].sh_addr = (Elf_Addr)lastaddr;
286 lastaddr += shdr[i].sh_size;
287 break;
291 /* Clear the whole area, including bss regions. */
292 kern_bzero(firstaddr, lastaddr - firstaddr);
294 /* Now read it all in. */
295 for (i = 0; i < hdr->e_shnum; i++) {
296 if (shdr[i].sh_addr == 0 || shdr[i].sh_type == SHT_NOBITS)
297 continue;
298 if (kern_pread(ef->fd, (vm_offset_t)shdr[i].sh_addr,
299 shdr[i].sh_size, (off_t)shdr[i].sh_offset) != 0) {
300 printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
301 "_obj_loadimage: read failed\n");
302 goto out;
306 file_addmetadata(fp, MODINFOMD_SHDR, shdrbytes, shdr);
308 res = __elfN(obj_parse_modmetadata)(fp, ef);
309 if (res != 0)
310 goto out;
312 ret = lastaddr - firstaddr;
313 fp->f_addr = firstaddr;
315 printf("size 0x%lx at 0x%lx", (u_long)ret, (u_long)firstaddr);
317 out:
318 printf("\n");
319 return ret;
322 #if defined(__i386__) && __ELF_WORD_SIZE == 64
323 struct mod_metadata64 {
324 int md_version; /* structure version MDTV_* */
325 int md_type; /* type of entry MDT_* */
326 u_int64_t md_data; /* specific data */
327 u_int64_t md_cval; /* common string label */
329 #endif
332 __elfN(obj_parse_modmetadata)(struct preloaded_file *fp, elf_file_t ef)
334 struct mod_metadata md;
335 #if defined(__i386__) && __ELF_WORD_SIZE == 64
336 struct mod_metadata64 md64;
337 #endif
338 struct mod_depend *mdepend;
339 struct mod_version mver;
340 char *s;
341 int error, modcnt, minfolen;
342 Elf_Addr v, p, p_stop;
344 if (__elfN(obj_lookup_set)(fp, ef, "modmetadata_set", &p, &p_stop,
345 &modcnt) != 0)
346 return ENOENT;
348 modcnt = 0;
349 while (p < p_stop) {
350 COPYOUT(p, &v, sizeof(v));
351 error = __elfN(obj_reloc_ptr)(fp, ef, p, &v, sizeof(v));
352 if (error != 0)
353 return (error);
354 #if defined(__i386__) && __ELF_WORD_SIZE == 64
355 COPYOUT(v, &md64, sizeof(md64));
356 error = __elfN(obj_reloc_ptr)(fp, ef, v, &md64, sizeof(md64));
357 if (error != 0)
358 return (error);
359 md.md_version = md64.md_version;
360 md.md_type = md64.md_type;
361 md.md_cval = (const char *)(uintptr_t)md64.md_cval;
362 md.md_data = (void *)(uintptr_t)md64.md_data;
363 #else
364 COPYOUT(v, &md, sizeof(md));
365 error = __elfN(obj_reloc_ptr)(fp, ef, v, &md, sizeof(md));
366 if (error != 0)
367 return (error);
368 #endif
369 p += sizeof(Elf_Addr);
370 switch(md.md_type) {
371 case MDT_DEPEND:
372 s = strdupout((vm_offset_t)md.md_cval);
373 minfolen = sizeof(*mdepend) + strlen(s) + 1;
374 mdepend = malloc(minfolen);
375 if (mdepend == NULL)
376 return ENOMEM;
377 COPYOUT((vm_offset_t)md.md_data, mdepend,
378 sizeof(*mdepend));
379 strcpy((char*)(mdepend + 1), s);
380 free(s);
381 file_addmetadata(fp, MODINFOMD_DEPLIST, minfolen,
382 mdepend);
383 free(mdepend);
384 break;
385 case MDT_VERSION:
386 s = strdupout((vm_offset_t)md.md_cval);
387 COPYOUT((vm_offset_t)md.md_data, &mver, sizeof(mver));
388 file_addmodule(fp, s, mver.mv_version, NULL);
389 free(s);
390 modcnt++;
391 break;
392 case MDT_MODULE:
393 break;
394 default:
395 printf("unknown type %d\n", md.md_type);
396 break;
399 return 0;
402 static int
403 __elfN(obj_lookup_set)(struct preloaded_file *fp, elf_file_t ef,
404 const char* name, Elf_Addr *startp, Elf_Addr *stopp, int *countp)
406 Elf_Ehdr *hdr;
407 Elf_Shdr *shdr;
408 char *p;
409 vm_offset_t shstrtab;
410 int i;
412 hdr = &ef->hdr;
413 shdr = ef->e_shdr;
414 shstrtab = shdr[ef->shstrindex].sh_addr;
416 for (i = 0; i < hdr->e_shnum; i++) {
417 if (shdr[i].sh_type != SHT_PROGBITS)
418 continue;
419 if (shdr[i].sh_name == 0)
420 continue;
421 p = strdupout(shstrtab + shdr[i].sh_name);
422 if (strncmp(p, "set_", 4) == 0 && strcmp(p + 4, name) == 0) {
423 *startp = shdr[i].sh_addr;
424 *stopp = shdr[i].sh_addr + shdr[i].sh_size;
425 *countp = (*stopp - *startp) / sizeof(Elf_Addr);
426 free(p);
427 return (0);
429 free(p);
432 return (ESRCH);
436 * Apply any intra-module relocations to the value. p is the load address
437 * of the value and val/len is the value to be modified. This does NOT modify
438 * the image in-place, because this is done by kern_linker later on.
440 static int
441 __elfN(obj_reloc_ptr)(struct preloaded_file *mp, elf_file_t ef, Elf_Addr p,
442 void *val, size_t len)
444 Elf_Ehdr *hdr;
445 Elf_Shdr *shdr;
446 Elf_Addr off = p;
447 Elf_Addr base;
448 Elf_Rela a, *abase;
449 Elf_Rel r, *rbase;
450 int error, i, j, nrel, nrela;
452 hdr = &ef->hdr;
453 shdr = ef->e_shdr;
455 for (i = 0; i < hdr->e_shnum; i++) {
456 if (shdr[i].sh_type != SHT_RELA && shdr[i].sh_type != SHT_REL)
457 continue;
458 base = shdr[shdr[i].sh_info].sh_addr;
459 if (base == 0 || shdr[i].sh_addr == 0)
460 continue;
461 if (off < base || off + len > base +
462 shdr[shdr[i].sh_info].sh_size)
463 continue;
465 switch (shdr[i].sh_type) {
466 case SHT_RELA:
467 abase = (Elf_Rela *)(intptr_t)shdr[i].sh_addr;
469 nrela = shdr[i].sh_size / sizeof(Elf_Rela);
470 for (j = 0; j < nrela; j++) {
471 COPYOUT(abase + j, &a, sizeof(a));
473 error = __elfN(reloc)(ef, __elfN(obj_symaddr),
474 &a, ELF_RELOC_RELA, base, off, val, len);
475 if (error != 0)
476 return (error);
478 break;
479 case SHT_REL:
480 rbase = (Elf_Rel *)(intptr_t)shdr[i].sh_addr;
482 nrel = shdr[i].sh_size / sizeof(Elf_Rel);
483 for (j = 0; j < nrel; j++) {
484 COPYOUT(rbase + j, &r, sizeof(r));
486 error = __elfN(reloc)(ef, __elfN(obj_symaddr),
487 &r, ELF_RELOC_REL, base, off, val, len);
488 if (error != 0)
489 return (error);
491 break;
494 return (0);
497 /* Look up the address of a specified symbol. */
498 static Elf_Addr
499 __elfN(obj_symaddr)(struct elf_file *ef, Elf_Size symidx)
501 Elf_Sym sym;
502 Elf_Addr base;
503 int symcnt;
505 symcnt = ef->e_shdr[ef->symtabindex].sh_size / sizeof(Elf_Sym);
506 if (symidx >= symcnt)
507 return (0);
508 COPYOUT(ef->e_shdr[ef->symtabindex].sh_addr + symidx * sizeof(Elf_Sym),
509 &sym, sizeof(sym));
510 if (sym.st_shndx == SHN_UNDEF || sym.st_shndx >= ef->hdr.e_shnum)
511 return (0);
512 base = ef->e_shdr[sym.st_shndx].sh_addr;
513 if (base == 0)
514 return (0);
515 return (base + sym.st_value);