]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/bootstrap/server/src/exec.c
update
[l4.git] / l4 / pkg / bootstrap / server / src / exec.c
1 /**
2  * \file        bootstrap/server/src/exec.c
3  * \brief       ELF loader
4  * 
5  * \date        2004
6  * \author      Frank Mehnert <fm3@os.inf.tu-dresden.de>,
7  *              Torsten Frenzel <frenzel@os.inf.tu-dresden.de> */
8
9 /*
10  * (c) 2005-2009 Author(s)
11  *     economic rights: Technische Universität Dresden (Germany)
12  *
13  * This file is part of TUD:OS and distributed under the terms of the
14  * GNU General Public License 2.
15  * Please see the COPYING-GPL-2 file for details.
16  */
17
18 #include <stdio.h>
19 #include <string.h>
20 #include <stdlib.h>
21 #include <alloca.h>
22
23 #include <l4/util/elf.h>
24
25 #include "exec.h"
26
27 int
28 exec_load_elf(exec_handler_func_t *handler,
29               void *handle, const char **error_msg, l4_addr_t *entry)
30 {
31   exec_task_t *t = handle;
32   ElfW(Ehdr) *x = t->mod_start;
33   ElfW(Phdr) *phdr, *ph;
34   int i;
35
36   /* Read the ELF header.  */
37
38   if (!l4util_elf_check_magic(x))
39     return *error_msg="no ELF executable", -1;
40
41   /* Make sure the file is of the right architecture.  */
42   if (!l4util_elf_check_arch(x))
43     return *error_msg="wrong ELF architecture", -1;
44
45   *entry = (l4_addr_t) x->e_entry;
46
47   phdr   = l4util_elf_phdr(x);
48
49   for (i = 0; i < x->e_phnum; i++)
50     {
51       ph = (ElfW(Phdr)*)((l4_addr_t)phdr + i * x->e_phentsize);
52       if (ph->p_type == PT_LOAD)
53         {
54           exec_sectype_t type = EXEC_SECTYPE_ALLOC |
55             EXEC_SECTYPE_LOAD;
56           if (ph->p_flags & PF_R) type |= EXEC_SECTYPE_READ;
57           if (ph->p_flags & PF_W) type |= EXEC_SECTYPE_WRITE;
58           if (ph->p_flags & PF_X) type |= EXEC_SECTYPE_EXECUTE;
59           (*handler)(handle,
60                     ph->p_offset, ph->p_filesz,
61                     ph->p_paddr, ph->p_vaddr, ph->p_memsz, type);
62         }
63     }
64
65   return *error_msg="", 0;
66 }