[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/boot/common/load_elf_obj.c

Version: -  FREEBSD  -  FREEBSD7  -  FREEBSD71  -  FREEBSD70  -  FREEBSD6  -  FREEBSD64  -  FREEBSD63  -  FREEBSD62  -  FREEBSD61  -  FREEBSD60  -  FREEBSD5  -  FREEBSD55  -  FREEBSD54  -  FREEBSD53  -  FREEBSD52  -  FREEBSD51  -  FREEBSD50  -  FREEBSD4  -  FREEBSD3  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  OPENSOLARIS  -  minix-3-1-1  -  TRUSTEDBSD-SEBSD  -  FREEBSD-LIBC  -  FREEBSD7-LIBC  -  FREEBSD6-LIBC  -  GLIBC27 
SearchContext: -  none  -  excerpts  -  bigexcerpts 

  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.
  6  *
  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.
 15  *
 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.
 27  */
 28 
 29 #include <sys/cdefs.h>
 30 __FBSDID("$FreeBSD: src/sys/boot/common/load_elf_obj.c,v 1.2 2005/12/18 04:52:35 marcel Exp $");
 31 
 32 #include <sys/param.h>
 33 #include <sys/exec.h>
 34 #include <sys/linker.h>
 35 #include <sys/module.h>
 36 #include <inttypes.h>
 37 #include <string.h>
 38 #include <machine/elf.h>
 39 #include <stand.h>
 40 #define FREEBSD_ELF
 41 #include <link.h>
 42 
 43 #include "bootstrap.h"
 44 
 45 #define COPYOUT(s,d,l)  archsw.arch_copyout((vm_offset_t)(s), d, l)
 46 
 47 #if defined(__i386__) && __ELF_WORD_SIZE == 64
 48 #undef ELF_TARG_CLASS
 49 #undef ELF_TARG_MACH
 50 #define ELF_TARG_CLASS  ELFCLASS64
 51 #define ELF_TARG_MACH   EM_X86_64
 52 #endif
 53 
 54 typedef struct elf_file {
 55         Elf_Ehdr        hdr;
 56         Elf_Shdr        *e_shdr;
 57 
 58         int             symtabindex;    /* Index of symbol table */
 59         int             shstrindex;     /* Index of section name string table */
 60 
 61         int             fd;
 62         vm_offset_t     off;
 63 } *elf_file_t;
 64 
 65 static int __elfN(obj_loadimage)(struct preloaded_file *mp, elf_file_t ef,
 66     u_int64_t loadaddr);
 67 static int __elfN(obj_lookup_set)(struct preloaded_file *mp, elf_file_t ef,
 68     const char *name, Elf_Addr *startp, Elf_Addr *stopp, int *countp);
 69 static int __elfN(obj_reloc_ptr)(struct preloaded_file *mp, elf_file_t ef,
 70     Elf_Addr p, void *val, size_t len);
 71 static int __elfN(obj_parse_modmetadata)(struct preloaded_file *mp,
 72     elf_file_t ef);
 73 static Elf_Addr __elfN(obj_symaddr)(struct elf_file *ef, Elf_Size symidx);
 74 
 75 const char      *__elfN(obj_kerneltype) = "elf kernel";
 76 const char      *__elfN(obj_moduletype) = "elf obj module";
 77 
 78 /*
 79  * Attempt to load the file (file) as an ELF module.  It will be stored at
 80  * (dest), and a pointer to a module structure describing the loaded object
 81  * will be saved in (result).
 82  */
 83 int
 84 __elfN(obj_loadfile)(char *filename, u_int64_t dest,
 85     struct preloaded_file **result)
 86 {
 87         struct preloaded_file *fp, *kfp;
 88         struct elf_file ef;
 89         Elf_Ehdr *hdr;
 90         int err;
 91         ssize_t bytes_read;
 92 
 93         fp = NULL;
 94         bzero(&ef, sizeof(struct elf_file));
 95 
 96         /*
 97          * Open the image, read and validate the ELF header
 98          */
 99         if (filename == NULL)   /* can't handle nameless */
100                 return(EFTYPE);
101         if ((ef.fd = open(filename, O_RDONLY)) == -1)
102                 return(errno);
103 
104         hdr = &ef.hdr;
105         bytes_read = read(ef.fd, hdr, sizeof(*hdr));
106         if (bytes_read != sizeof(*hdr)) {
107                 err = EFTYPE;   /* could be EIO, but may be small file */
108                 goto oerr;
109         }
110 
111         /* Is it ELF? */
112         if (!IS_ELF(*hdr)) {
113                 err = EFTYPE;
114                 goto oerr;
115         }
116         if (hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS || /* Layout ? */
117             hdr->e_ident[EI_DATA] != ELF_TARG_DATA ||
118             hdr->e_ident[EI_VERSION] != EV_CURRENT ||   /* Version ? */
119             hdr->e_version != EV_CURRENT ||
120             hdr->e_machine != ELF_TARG_MACH ||          /* Machine ? */
121             hdr->e_type != ET_REL) {
122                 err = EFTYPE;
123                 goto oerr;
124         }
125 
126         if (hdr->e_shnum * hdr->e_shentsize == 0 || hdr->e_shoff == 0 ||
127             hdr->e_shentsize != sizeof(Elf_Shdr)) {
128                 err = EFTYPE;
129                 goto oerr;
130         }
131 
132         kfp = file_findfile(NULL, NULL);
133         if (kfp == NULL) {
134                 printf("elf" __XSTRING(__ELF_WORD_SIZE)
135                     "_obj_loadfile: can't load module before kernel\n");
136                 err = EPERM;
137                 goto oerr;
138         }
139         if (strcmp(__elfN(obj_kerneltype), kfp->f_type)) {
140                 printf("elf" __XSTRING(__ELF_WORD_SIZE)
141                     "_obj_loadfile: can't load module with kernel type '%s'\n",
142                     kfp->f_type);
143                 err = EPERM;
144                 goto oerr;
145         }
146 
147         /* Page-align the load address */
148         dest = roundup(dest, PAGE_SIZE);
149 
150         /*
151          * Ok, we think we should handle this.
152          */
153         fp = file_alloc();
154         if (fp == NULL) {
155                 printf("elf" __XSTRING(__ELF_WORD_SIZE)
156                     "_obj_loadfile: cannot allocate module info\n");
157                 err = EPERM;
158                 goto out;
159         }
160         fp->f_name = strdup(filename);
161         fp->f_type = strdup(__elfN(obj_moduletype));
162 
163         printf("%s ", filename);
164 
165         fp->f_size = __elfN(obj_loadimage)(fp, &ef, dest);
166         if (fp->f_size == 0 || fp->f_addr == 0)
167                 goto ioerr;
168 
169         /* save exec header as metadata */
170         file_addmetadata(fp, MODINFOMD_ELFHDR, sizeof(*hdr), hdr);
171 
172         /* Load OK, return module pointer */
173         *result = (struct preloaded_file *)fp;
174         err = 0;
175         goto out;
176 
177 ioerr:
178         err = EIO;
179 oerr:
180         file_discard(fp);
181 out:
182         close(ef.fd);
183         if (ef.e_shdr != NULL)
184                 free(ef.e_shdr);
185 
186         return(err);
187 }
188 
189 /*
190  * With the file (fd) open on the image, and (ehdr) containing
191  * the Elf header, load the image at (off)
192  */
193 static int
194 __elfN(obj_loadimage)(struct preloaded_file *fp, elf_file_t ef, u_int64_t off)
195 {
196         Elf_Ehdr *hdr;
197         Elf_Shdr *shdr;
198         vm_offset_t firstaddr, lastaddr;
199         int i, nsym, res, ret, shdrbytes, symstrindex;
200 
201         ret = 0;
202         firstaddr = lastaddr = (vm_offset_t)off;
203         hdr = &ef->hdr;
204         ef->off = (vm_offset_t)off;
205 
206         /* Read in the section headers. */
207         shdrbytes = hdr->e_shnum * hdr->e_shentsize;
208         shdr = alloc_pread(ef->fd, (off_t)hdr->e_shoff, shdrbytes);
209         if (shdr == NULL) {
210                 printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
211                     "_obj_loadimage: read section headers failed\n");
212                 goto out;
213         }
214         ef->e_shdr = shdr;
215 
216         /*
217          * Decide where to load everything, but don't read it yet.
218          * We store the load address as a non-zero sh_addr value.
219          * Start with the code/data and bss.
220          */
221         for (i = 0; i < hdr->e_shnum; i++)
222                 shdr[i].sh_addr = 0;
223         for (i = 0; i < hdr->e_shnum; i++) {
224                 switch (shdr[i].sh_type) {
225                 case SHT_PROGBITS:
226                 case SHT_NOBITS:
227                         lastaddr = roundup(lastaddr, shdr[i].sh_addralign);
228                         shdr[i].sh_addr = (Elf_Addr)lastaddr;
229                         lastaddr += shdr[i].sh_size;
230                         break;
231                 }
232         }
233 
234         /* Symbols. */
235         nsym = 0;
236         for (i = 0; i < hdr->e_shnum; i++) {
237                 switch (shdr[i].sh_type) {
238                 case SHT_SYMTAB:
239                         nsym++;
240                         ef->symtabindex = i;
241                         shdr[i].sh_addr = (Elf_Addr)lastaddr;
242                         lastaddr += shdr[i].sh_size;
243                         break;
244                 }
245         }
246         if (nsym != 1) {
247                 printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
248                     "_obj_loadimage: file has no valid symbol table\n");
249                 goto out;
250         }
251         lastaddr = roundup(lastaddr, shdr[ef->symtabindex].sh_addralign);
252         shdr[ef->symtabindex].sh_addr = (Elf_Addr)lastaddr;
253         lastaddr += shdr[ef->symtabindex].sh_size;
254 
255         symstrindex = shdr[ef->symtabindex].sh_link;
256         if (symstrindex < 0 || symstrindex >= hdr->e_shnum ||
257             shdr[symstrindex].sh_type != SHT_STRTAB) {
258                 printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
259                     "_obj_loadimage: file has invalid symbol strings\n");
260                 goto out;
261         }
262         lastaddr = roundup(lastaddr, shdr[symstrindex].sh_addralign);
263         shdr[symstrindex].sh_addr = (Elf_Addr)lastaddr;
264         lastaddr += shdr[symstrindex].sh_size;
265 
266         /* Section names. */
267         if (hdr->e_shstrndx == 0 || hdr->e_shstrndx >= hdr->e_shnum ||
268             shdr[hdr->e_shstrndx].sh_type != SHT_STRTAB) {
269                 printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
270                     "_obj_loadimage: file has no section names\n");
271                 goto out;
272         }
273         ef->shstrindex = hdr->e_shstrndx;
274         lastaddr = roundup(lastaddr, shdr[ef->shstrindex].sh_addralign);
275         shdr[ef->shstrindex].sh_addr = (Elf_Addr)lastaddr;
276         lastaddr += shdr[ef->shstrindex].sh_size;
277 
278         /* Relocation tables. */
279         for (i = 0; i < hdr->e_shnum; i++) {
280                 switch (shdr[i].sh_type) {
281                 case SHT_REL:
282                 case SHT_RELA:
283                         lastaddr = roundup(lastaddr, shdr[i].sh_addralign);
284                         shdr[i].sh_addr = (Elf_Addr)lastaddr;
285                         lastaddr += shdr[i].sh_size;
286                         break;
287                 }
288         }
289 
290         /* Clear the whole area, including bss regions. */
291         kern_bzero(firstaddr, lastaddr - firstaddr);
292 
293         /* Now read it all in. */
294         for (i = 0; i < hdr->e_shnum; i++) {
295                 if (shdr[i].sh_addr == 0 || shdr[i].sh_type == SHT_NOBITS)
296                         continue;
297                 if (kern_pread(ef->fd, (vm_offset_t)shdr[i].sh_addr,
298                     shdr[i].sh_size, (off_t)shdr[i].sh_offset) != 0) {
299                         printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
300                             "_obj_loadimage: read failed\n");
301                         goto out;
302                 }
303         }
304 
305         file_addmetadata(fp, MODINFOMD_SHDR, shdrbytes, shdr);
306 
307         res = __elfN(obj_parse_modmetadata)(fp, ef);
308         if (res != 0)
309                 goto out;
310 
311         ret = lastaddr - firstaddr;
312         fp->f_addr = firstaddr;
313 
314         printf("size 0x%lx at 0x%lx", (u_long)ret, (u_long)firstaddr);
315 
316 out:
317         printf("\n");
318         return ret;
319 }
320 
321 #if defined(__i386__) && __ELF_WORD_SIZE == 64
322 struct mod_metadata64 {
323         int             md_version;     /* structure version MDTV_* */
324         int             md_type;        /* type of entry MDT_* */
325         u_int64_t       md_data;        /* specific data */
326         u_int64_t       md_cval;        /* common string label */
327 };
328 #endif
329 
330 int
331 __elfN(obj_parse_modmetadata)(struct preloaded_file *fp, elf_file_t ef)
332 {
333         struct mod_metadata md;
334 #if defined(__i386__) && __ELF_WORD_SIZE == 64
335         struct mod_metadata64 md64;
336 #endif
337         struct mod_depend *mdepend;
338         struct mod_version mver;
339         char *s;
340         int error, modcnt, minfolen;
341         Elf_Addr v, p, p_stop;
342 
343         if (__elfN(obj_lookup_set)(fp, ef, "modmetadata_set", &p, &p_stop,
344             &modcnt) != 0)
345                 return ENOENT;
346 
347         modcnt = 0;
348         while (p < p_stop) {
349                 COPYOUT(p, &v, sizeof(v));
350                 error = __elfN(obj_reloc_ptr)(fp, ef, p, &v, sizeof(v));
351                 if (error != 0)
352                         return (error);
353 #if defined(__i386__) && __ELF_WORD_SIZE == 64
354                 COPYOUT(v, &md64, sizeof(md64));
355                 error = __elfN(obj_reloc_ptr)(fp, ef, v, &md64, sizeof(md64));
356                 if (error != 0)
357                         return (error);
358                 md.md_version = md64.md_version;
359                 md.md_type = md64.md_type;
360                 md.md_cval = (const char *)(uintptr_t)md64.md_cval;
361                 md.md_data = (void *)(uintptr_t)md64.md_data;
362 #else
363                 COPYOUT(v, &md, sizeof(md));
364                 error = __elfN(obj_reloc_ptr)(fp, ef, v, &md, sizeof(md));
365                 if (error != 0)
366                         return (error);
367 #endif
368                 p += sizeof(Elf_Addr);
369                 switch(md.md_type) {
370                 case MDT_DEPEND:
371                         s = strdupout((vm_offset_t)md.md_cval);
372                         minfolen = sizeof(*mdepend) + strlen(s) + 1;
373                         mdepend = malloc(minfolen);
374                         if (mdepend == NULL)
375                                 return ENOMEM;
376                         COPYOUT((vm_offset_t)md.md_data, mdepend,
377                             sizeof(*mdepend));
378                         strcpy((char*)(mdepend + 1), s);
379                         free(s);
380                         file_addmetadata(fp, MODINFOMD_DEPLIST, minfolen,
381                             mdepend);
382                         free(mdepend);
383                         break;
384                 case MDT_VERSION:
385                         s = strdupout((vm_offset_t)md.md_cval);
386                         COPYOUT((vm_offset_t)md.md_data, &mver, sizeof(mver));
387                         file_addmodule(fp, s, mver.mv_version, NULL);
388                         free(s);
389                         modcnt++;
390                         break;
391                 case MDT_MODULE:
392                         break;
393                 default:
394                         printf("unknown type %d\n", md.md_type);
395                         break;
396                 }
397         }
398         return 0;
399 }
400 
401 static int
402 __elfN(obj_lookup_set)(struct preloaded_file *fp, elf_file_t ef,
403     const char* name, Elf_Addr *startp, Elf_Addr *stopp, int *countp)
404 {
405         Elf_Ehdr *hdr;
406         Elf_Shdr *shdr;
407         char *p;
408         vm_offset_t shstrtab;
409         int i;
410 
411         hdr = &ef->hdr;
412         shdr = ef->e_shdr;
413         shstrtab = shdr[ef->shstrindex].sh_addr;
414 
415         for (i = 0; i < hdr->e_shnum; i++) {
416                 if (shdr[i].sh_type != SHT_PROGBITS)
417                         continue;
418                 if (shdr[i].sh_name == 0)
419                         continue;
420                 p = strdupout(shstrtab + shdr[i].sh_name);
421                 if (strncmp(p, "set_", 4) == 0 && strcmp(p + 4, name) == 0) {
422                         *startp = shdr[i].sh_addr;
423                         *stopp = shdr[i].sh_addr +  shdr[i].sh_size;
424                         *countp = (*stopp - *startp) / sizeof(Elf_Addr);
425                         free(p);
426                         return (0);
427                 }
428                 free(p);
429         }
430 
431         return (ESRCH);
432 }
433 
434 /*
435  * Apply any intra-module relocations to the value. p is the load address
436  * of the value and val/len is the value to be modified. This does NOT modify
437  * the image in-place, because this is done by kern_linker later on.
438  */
439 static int
440 __elfN(obj_reloc_ptr)(struct preloaded_file *mp, elf_file_t ef, Elf_Addr p,
441     void *val, size_t len)
442 {
443         Elf_Ehdr *hdr;
444         Elf_Shdr *shdr;
445         Elf_Addr off = p;
446         Elf_Addr base;
447         Elf_Rela a, *abase;
448         Elf_Rel r, *rbase;
449         int error, i, j, nrel, nrela;
450 
451         hdr = &ef->hdr;
452         shdr = ef->e_shdr;
453 
454         for (i = 0; i < hdr->e_shnum; i++) {
455                 if (shdr[i].sh_type != SHT_RELA && shdr[i].sh_type != SHT_REL)
456                         continue;
457                 base = shdr[shdr[i].sh_info].sh_addr;
458                 if (base == 0 || shdr[i].sh_addr == 0)
459                         continue;
460                 if (off < base || off + len > base +
461                     shdr[shdr[i].sh_info].sh_size)
462                         continue;
463 
464                 switch (shdr[i].sh_type) {
465                 case SHT_RELA:
466                         abase = (Elf_Rela *)(intptr_t)shdr[i].sh_addr;
467 
468                         nrela = shdr[i].sh_size / sizeof(Elf_Rela);
469                         for (j = 0; j < nrela; j++) {
470                                 COPYOUT(abase + j, &a, sizeof(a));
471 
472                                 error = __elfN(reloc)(ef, __elfN(obj_symaddr),
473                                     &a, ELF_RELOC_RELA, base, off, val, len);
474                                 if (error != 0)
475                                         return (error);
476                         }
477                         break;
478                 case SHT_REL:
479                         rbase = (Elf_Rel *)(intptr_t)shdr[i].sh_addr;
480 
481                         nrel = shdr[i].sh_size / sizeof(Elf_Rel);
482                         for (j = 0; j < nrel; j++) {
483                                 COPYOUT(rbase + j, &r, sizeof(r));
484 
485                                 error = __elfN(reloc)(ef, __elfN(obj_symaddr),
486                                     &r, ELF_RELOC_REL, base, off, val, len);
487                                 if (error != 0)
488                                         return (error);
489                         }
490                         break;
491                 }
492         }
493         return (0);
494 }
495 
496 /* Look up the address of a specified symbol. */
497 static Elf_Addr
498 __elfN(obj_symaddr)(struct elf_file *ef, Elf_Size symidx)
499 {
500         Elf_Sym sym;
501         Elf_Addr base;
502         int symcnt;
503 
504         symcnt = ef->e_shdr[ef->symtabindex].sh_size / sizeof(Elf_Sym);
505         if (symidx >= symcnt)
506                 return (0);
507         COPYOUT(ef->e_shdr[ef->symtabindex].sh_addr + symidx * sizeof(Elf_Sym),
508             &sym, sizeof(sym));
509         if (sym.st_shndx == SHN_UNDEF || sym.st_shndx >= ef->hdr.e_shnum)
510                 return (0);
511         base = ef->e_shdr[sym.st_shndx].sh_addr;
512         if (base == 0)
513                 return (0);
514         return (base + sym.st_value);
515 }
516 

Cache object: 225b39a6e6efe910d2859a34089ecf02


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.