(c) versus (C)
[helenos.git] / kernel / generic / src / lib / elf.c
blob833aacdbf7cd1e7a532d901a0eb31818cb00eaaf
1 /*
2 * Copyright (c) 2006 Sergey Bondari
3 * Copyright (c) 2006 Jakub Jermar
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - 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 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 /** @addtogroup generic
31 * @{
34 /**
35 * @file
36 * @brief Kernel ELF loader.
39 #include <lib/elf.h>
40 #include <debug.h>
41 #include <arch/types.h>
42 #include <typedefs.h>
43 #include <mm/as.h>
44 #include <mm/frame.h>
45 #include <mm/slab.h>
46 #include <align.h>
47 #include <memstr.h>
48 #include <macros.h>
49 #include <arch.h>
51 static char *error_codes[] = {
52 "no error",
53 "invalid image",
54 "address space error",
55 "incompatible image",
56 "unsupported image type",
57 "irrecoverable error"
60 static int segment_header(elf_segment_header_t *entry, elf_header_t *elf, as_t *as);
61 static int section_header(elf_section_header_t *entry, elf_header_t *elf, as_t *as);
62 static int load_segment(elf_segment_header_t *entry, elf_header_t *elf, as_t *as);
64 /** ELF loader
66 * @param header Pointer to ELF header in memory
67 * @param as Created and properly mapped address space
68 * @return EE_OK on success
70 int elf_load(elf_header_t *header, as_t * as)
72 int i, rc;
74 /* Identify ELF */
75 if (header->e_ident[EI_MAG0] != ELFMAG0 || header->e_ident[EI_MAG1] != ELFMAG1 ||
76 header->e_ident[EI_MAG2] != ELFMAG2 || header->e_ident[EI_MAG3] != ELFMAG3) {
77 return EE_INVALID;
80 /* Identify ELF compatibility */
81 if (header->e_ident[EI_DATA] != ELF_DATA_ENCODING || header->e_machine != ELF_MACHINE ||
82 header->e_ident[EI_VERSION] != EV_CURRENT || header->e_version != EV_CURRENT ||
83 header->e_ident[EI_CLASS] != ELF_CLASS) {
84 return EE_INCOMPATIBLE;
87 if (header->e_phentsize != sizeof(elf_segment_header_t))
88 return EE_INCOMPATIBLE;
90 if (header->e_shentsize != sizeof(elf_section_header_t))
91 return EE_INCOMPATIBLE;
93 /* Check if the object type is supported. */
94 if (header->e_type != ET_EXEC)
95 return EE_UNSUPPORTED;
97 /* Walk through all segment headers and process them. */
98 for (i = 0; i < header->e_phnum; i++) {
99 rc = segment_header(&((elf_segment_header_t *)(((uint8_t *) header) + header->e_phoff))[i], header, as);
100 if (rc != EE_OK)
101 return rc;
104 /* Inspect all section headers and proccess them. */
105 for (i = 0; i < header->e_shnum; i++) {
106 rc = section_header(&((elf_section_header_t *)(((uint8_t *) header) + header->e_shoff))[i], header, as);
107 if (rc != EE_OK)
108 return rc;
111 return EE_OK;
114 /** Print error message according to error code.
116 * @param rc Return code returned by elf_load().
118 * @return NULL terminated description of error.
120 char *elf_error(int rc)
122 ASSERT(rc < sizeof(error_codes)/sizeof(char *));
124 return error_codes[rc];
127 /** Process segment header.
129 * @param entry Segment header.
130 * @param elf ELF header.
131 * @param as Address space into wich the ELF is being loaded.
133 * @return EE_OK on success, error code otherwise.
135 static int segment_header(elf_segment_header_t *entry, elf_header_t *elf, as_t *as)
137 switch (entry->p_type) {
138 case PT_NULL:
139 case PT_PHDR:
140 break;
141 case PT_LOAD:
142 return load_segment(entry, elf, as);
143 break;
144 case PT_DYNAMIC:
145 case PT_INTERP:
146 case PT_SHLIB:
147 case PT_NOTE:
148 case PT_LOPROC:
149 case PT_HIPROC:
150 default:
151 return EE_UNSUPPORTED;
152 break;
154 return EE_OK;
157 /** Load segment described by program header entry.
159 * @param entry Program header entry describing segment to be loaded.
160 * @param elf ELF header.
161 * @param as Address space into wich the ELF is being loaded.
163 * @return EE_OK on success, error code otherwise.
165 int load_segment(elf_segment_header_t *entry, elf_header_t *elf, as_t *as)
167 as_area_t *a;
168 int flags = 0;
169 mem_backend_data_t backend_data;
171 backend_data.elf = elf;
172 backend_data.segment = entry;
174 if (entry->p_align > 1) {
175 if ((entry->p_offset % entry->p_align) != (entry->p_vaddr % entry->p_align)) {
176 return EE_INVALID;
180 if (entry->p_flags & PF_X)
181 flags |= AS_AREA_EXEC;
182 if (entry->p_flags & PF_W)
183 flags |= AS_AREA_WRITE;
184 if (entry->p_flags & PF_R)
185 flags |= AS_AREA_READ;
186 flags |= AS_AREA_CACHEABLE;
189 * Check if the virtual address starts on page boundary.
191 if (ALIGN_UP(entry->p_vaddr, PAGE_SIZE) != entry->p_vaddr)
192 return EE_UNSUPPORTED;
194 a = as_area_create(as, flags, entry->p_memsz, entry->p_vaddr, AS_AREA_ATTR_NONE,
195 &elf_backend, &backend_data);
196 if (!a)
197 return EE_MEMORY;
200 * The segment will be mapped on demand by elf_page_fault().
203 return EE_OK;
206 /** Process section header.
208 * @param entry Segment header.
209 * @param elf ELF header.
210 * @param as Address space into wich the ELF is being loaded.
212 * @return EE_OK on success, error code otherwise.
214 static int section_header(elf_section_header_t *entry, elf_header_t *elf, as_t *as)
216 switch (entry->sh_type) {
217 default:
218 break;
221 return EE_OK;
224 /** @}