Rev 2996 | Rev 3010 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2928 | svoboda | 1 | /* |
2 | * Copyright (c) 2006 Sergey Bondari |
||
3 | * Copyright (c) 2006 Jakub Jermar |
||
4 | * Copyright (c) 2008 Jiri Svoboda |
||
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 | * |
||
11 | * - Redistributions of source code must retain the above copyright |
||
12 | * notice, this list of conditions and the following disclaimer. |
||
13 | * - Redistributions in binary form must reproduce the above copyright |
||
14 | * notice, this list of conditions and the following disclaimer in the |
||
15 | * documentation and/or other materials provided with the distribution. |
||
16 | * - The name of the author may not be used to endorse or promote products |
||
17 | * derived from this software without specific prior written permission. |
||
18 | * |
||
19 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
||
20 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
||
21 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
||
22 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
||
23 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
||
24 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||
25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||
26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||
27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
||
28 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
29 | */ |
||
30 | |||
31 | /** @addtogroup generic |
||
32 | * @{ |
||
33 | */ |
||
34 | |||
35 | /** |
||
36 | * @file |
||
37 | * @brief Kernel ELF loader. |
||
38 | */ |
||
39 | |||
40 | #include <stdio.h> |
||
41 | #include <sys/types.h> |
||
42 | #include <align.h> |
||
43 | #include <assert.h> |
||
44 | #include <as.h> |
||
2962 | svoboda | 45 | #include <unistd.h> |
46 | #include <fcntl.h> |
||
47 | |||
2928 | svoboda | 48 | #include "elf.h" |
2962 | svoboda | 49 | #include "pcb.h" |
50 | #include "elf_load.h" |
||
2928 | svoboda | 51 | |
52 | static char *error_codes[] = { |
||
53 | "no error", |
||
54 | "invalid image", |
||
55 | "address space error", |
||
56 | "incompatible image", |
||
57 | "unsupported image type", |
||
58 | "irrecoverable error" |
||
59 | }; |
||
60 | |||
2972 | svoboda | 61 | static unsigned int elf_load(elf_ld_t *elf, size_t so_bias); |
2964 | svoboda | 62 | static int segment_header(elf_ld_t *elf, elf_segment_header_t *entry); |
63 | static int section_header(elf_ld_t *elf, elf_section_header_t *entry); |
||
64 | static int load_segment(elf_ld_t *elf, elf_segment_header_t *entry); |
||
2928 | svoboda | 65 | |
2972 | svoboda | 66 | int elf_load_file(char *file_name, size_t so_bias, elf_info_t *info) |
2964 | svoboda | 67 | { |
68 | elf_ld_t elf; |
||
2928 | svoboda | 69 | |
2962 | svoboda | 70 | int fd; |
71 | int rc; |
||
72 | |||
73 | printf("open and read '%s'...\n", file_name); |
||
74 | |||
75 | fd = open(file_name, 0); |
||
76 | if (fd < 0) { |
||
77 | printf("failed opening file\n"); |
||
78 | return -1; |
||
79 | } |
||
80 | |||
2964 | svoboda | 81 | elf.fd = fd; |
82 | elf.info = info; |
||
83 | |||
2972 | svoboda | 84 | rc = elf_load(&elf, so_bias); |
2962 | svoboda | 85 | printf("elf_load() -> %d\n", rc); |
86 | |||
87 | close(fd); |
||
88 | |||
89 | return rc; |
||
90 | } |
||
91 | |||
2964 | svoboda | 92 | void elf_run(elf_info_t *info) |
2962 | svoboda | 93 | { |
2964 | svoboda | 94 | (*info->entry)(); |
2962 | svoboda | 95 | |
96 | /* not reached */ |
||
97 | } |
||
98 | |||
2964 | svoboda | 99 | int elf_create_pcb(elf_info_t *info) |
2962 | svoboda | 100 | { |
101 | pcb_t *pcb; |
||
102 | void *a; |
||
103 | |||
2990 | svoboda | 104 | pcb = __pcb_get(); |
2962 | svoboda | 105 | |
106 | a = as_area_create(pcb, sizeof(pcb_t), AS_AREA_READ | AS_AREA_WRITE); |
||
107 | if (a == (void *)(-1)) { |
||
108 | printf("elf_create_pcb: memory mapping failed\n"); |
||
109 | return EE_MEMORY; |
||
110 | } |
||
111 | |||
2964 | svoboda | 112 | pcb->entry = info->entry; |
2965 | svoboda | 113 | pcb->dynamic = info->dynamic; |
2962 | svoboda | 114 | |
115 | return 0; |
||
116 | } |
||
117 | |||
118 | |||
2928 | svoboda | 119 | /** ELF loader |
120 | * |
||
121 | * @param header Pointer to ELF header in memory |
||
122 | * @return EE_OK on success |
||
123 | */ |
||
2972 | svoboda | 124 | static unsigned int elf_load(elf_ld_t *elf, size_t so_bias) |
2928 | svoboda | 125 | { |
2964 | svoboda | 126 | elf_header_t header_buf; |
127 | elf_header_t *header = &header_buf; |
||
2928 | svoboda | 128 | int i, rc; |
129 | |||
2964 | svoboda | 130 | rc = read(elf->fd, header, sizeof(elf_header_t)); |
2928 | svoboda | 131 | if (rc < 0) { |
132 | printf("read error\n"); |
||
133 | return EE_INVALID; |
||
134 | } |
||
135 | |||
2964 | svoboda | 136 | elf->header = header; |
137 | |||
2928 | svoboda | 138 | printf("ELF-load:"); |
139 | /* Identify ELF */ |
||
140 | if (header->e_ident[EI_MAG0] != ELFMAG0 || |
||
141 | header->e_ident[EI_MAG1] != ELFMAG1 || |
||
142 | header->e_ident[EI_MAG2] != ELFMAG2 || |
||
143 | header->e_ident[EI_MAG3] != ELFMAG3) { |
||
144 | printf("invalid header\n"); |
||
145 | return EE_INVALID; |
||
146 | } |
||
147 | |||
148 | /* Identify ELF compatibility */ |
||
149 | if (header->e_ident[EI_DATA] != ELF_DATA_ENCODING || |
||
150 | header->e_machine != ELF_MACHINE || |
||
151 | header->e_ident[EI_VERSION] != EV_CURRENT || |
||
152 | header->e_version != EV_CURRENT || |
||
153 | header->e_ident[EI_CLASS] != ELF_CLASS) { |
||
154 | printf("incompatible data/version/class\n"); |
||
155 | return EE_INCOMPATIBLE; |
||
156 | } |
||
157 | |||
2992 | svoboda | 158 | if (header->e_phentsize != sizeof(elf_segment_header_t)) { |
159 | printf("e_phentsize:%d != %d\n", header->e_phentsize, |
||
160 | sizeof(elf_segment_header_t)); |
||
2928 | svoboda | 161 | return EE_INCOMPATIBLE; |
2992 | svoboda | 162 | } |
2928 | svoboda | 163 | |
2992 | svoboda | 164 | if (header->e_shentsize != sizeof(elf_section_header_t)) { |
165 | printf("e_shentsize:%d != %d\n", header->e_shentsize, |
||
166 | sizeof(elf_section_header_t)); |
||
2928 | svoboda | 167 | return EE_INCOMPATIBLE; |
2992 | svoboda | 168 | } |
2928 | svoboda | 169 | |
170 | /* Check if the object type is supported. */ |
||
171 | if (header->e_type != ET_EXEC && header->e_type != ET_DYN) { |
||
172 | printf("Object type %d is not supported\n", header->e_type); |
||
173 | return EE_UNSUPPORTED; |
||
174 | } |
||
175 | |||
2972 | svoboda | 176 | /* Shared objects can be loaded with a bias */ |
2971 | svoboda | 177 | printf("Object type: %d\n", header->e_type); |
2964 | svoboda | 178 | if (header->e_type == ET_DYN) |
2972 | svoboda | 179 | elf->bias = so_bias; |
2964 | svoboda | 180 | else |
181 | elf->bias = 0; |
||
182 | |||
2972 | svoboda | 183 | printf("Bias set to 0x%x\n", elf->bias); |
3004 | svoboda | 184 | elf->info->interp = NULL; |
185 | elf->info->dynamic = NULL; |
||
2971 | svoboda | 186 | |
2928 | svoboda | 187 | printf("parse segments\n"); |
188 | |||
189 | /* Walk through all segment headers and process them. */ |
||
190 | for (i = 0; i < header->e_phnum; i++) { |
||
2964 | svoboda | 191 | elf_segment_header_t segment_hdr; |
2928 | svoboda | 192 | |
2961 | svoboda | 193 | /* Seek to start of segment header */ |
2964 | svoboda | 194 | lseek(elf->fd, header->e_phoff |
195 | + i * sizeof(elf_segment_header_t), SEEK_SET); |
||
2928 | svoboda | 196 | |
2964 | svoboda | 197 | rc = read(elf->fd, &segment_hdr, sizeof(elf_segment_header_t)); |
198 | if (rc < 0) { printf("read error\n"); return EE_INVALID; } |
||
199 | |||
200 | rc = segment_header(elf, &segment_hdr); |
||
2928 | svoboda | 201 | if (rc != EE_OK) |
202 | return rc; |
||
203 | } |
||
204 | |||
205 | printf("parse sections\n"); |
||
206 | |||
207 | /* Inspect all section headers and proccess them. */ |
||
208 | for (i = 0; i < header->e_shnum; i++) { |
||
2964 | svoboda | 209 | elf_section_header_t section_hdr; |
2928 | svoboda | 210 | |
2961 | svoboda | 211 | /* Seek to start of section header */ |
2964 | svoboda | 212 | lseek(elf->fd, header->e_shoff |
213 | + i * sizeof(elf_section_header_t), SEEK_SET); |
||
2961 | svoboda | 214 | |
2964 | svoboda | 215 | rc = read(elf->fd, §ion_hdr, sizeof(elf_section_header_t)); |
216 | if (rc < 0) { printf("read error\n"); return EE_INVALID; } |
||
217 | |||
218 | rc = section_header(elf, §ion_hdr); |
||
2928 | svoboda | 219 | if (rc != EE_OK) |
2961 | svoboda | 220 | return rc; |
2928 | svoboda | 221 | } |
222 | |||
2964 | svoboda | 223 | elf->info->entry = |
224 | (entry_point_t)((uint8_t *)header->e_entry + elf->bias); |
||
225 | |||
2928 | svoboda | 226 | printf("done\n"); |
227 | |||
228 | return EE_OK; |
||
229 | } |
||
230 | |||
231 | /** Print error message according to error code. |
||
232 | * |
||
233 | * @param rc Return code returned by elf_load(). |
||
234 | * |
||
235 | * @return NULL terminated description of error. |
||
236 | */ |
||
237 | char *elf_error(unsigned int rc) |
||
238 | { |
||
239 | assert(rc < sizeof(error_codes) / sizeof(char *)); |
||
240 | |||
241 | return error_codes[rc]; |
||
242 | } |
||
243 | |||
244 | /** Process segment header. |
||
245 | * |
||
246 | * @param entry Segment header. |
||
247 | * @param elf ELF header. |
||
248 | * |
||
249 | * @return EE_OK on success, error code otherwise. |
||
250 | */ |
||
2964 | svoboda | 251 | static int segment_header(elf_ld_t *elf, elf_segment_header_t *entry) |
2928 | svoboda | 252 | { |
253 | switch (entry->p_type) { |
||
254 | case PT_NULL: |
||
255 | case PT_PHDR: |
||
256 | break; |
||
257 | case PT_LOAD: |
||
2964 | svoboda | 258 | return load_segment(elf, entry); |
2928 | svoboda | 259 | break; |
3004 | svoboda | 260 | case PT_INTERP: |
261 | /* Assume silently interp == "/rtld.so" */ |
||
262 | elf->info->interp = "/rtld.so"; |
||
263 | break; |
||
2928 | svoboda | 264 | case PT_DYNAMIC: |
265 | case PT_SHLIB: |
||
266 | case PT_NOTE: |
||
267 | case PT_LOPROC: |
||
268 | case PT_HIPROC: |
||
269 | default: |
||
270 | printf("segment p_type %d unknown\n", entry->p_type); |
||
271 | return EE_UNSUPPORTED; |
||
272 | break; |
||
273 | } |
||
274 | return EE_OK; |
||
275 | } |
||
276 | |||
277 | /** Load segment described by program header entry. |
||
278 | * |
||
279 | * @param entry Program header entry describing segment to be loaded. |
||
280 | * @param elf ELF header. |
||
281 | * |
||
282 | * @return EE_OK on success, error code otherwise. |
||
283 | */ |
||
2964 | svoboda | 284 | int load_segment(elf_ld_t *elf, elf_segment_header_t *entry) |
2928 | svoboda | 285 | { |
286 | void *a; |
||
287 | int flags = 0; |
||
2932 | svoboda | 288 | uintptr_t bias; |
2928 | svoboda | 289 | int rc; |
290 | |||
291 | printf("load segment at addr 0x%x, size 0x%x\n", entry->p_vaddr, |
||
292 | entry->p_memsz); |
||
293 | |||
2964 | svoboda | 294 | bias = elf->bias; |
2928 | svoboda | 295 | |
296 | if (entry->p_align > 1) { |
||
297 | if ((entry->p_offset % entry->p_align) != |
||
298 | (entry->p_vaddr % entry->p_align)) { |
||
299 | printf("align check 1 failed offset%%align=%d, vaddr%%align=%d\n", |
||
300 | entry->p_offset % entry->p_align, |
||
301 | entry->p_vaddr % entry->p_align |
||
302 | ); |
||
303 | return EE_INVALID; |
||
304 | } |
||
305 | } |
||
306 | |||
2992 | svoboda | 307 | /* Final flags that will be set for the memory area */ |
308 | |||
2985 | svoboda | 309 | if (entry->p_flags & PF_X) |
2928 | svoboda | 310 | flags |= AS_AREA_EXEC; |
311 | if (entry->p_flags & PF_W) |
||
312 | flags |= AS_AREA_WRITE; |
||
313 | if (entry->p_flags & PF_R) |
||
314 | flags |= AS_AREA_READ; |
||
315 | flags |= AS_AREA_CACHEABLE; |
||
2985 | svoboda | 316 | |
2928 | svoboda | 317 | /* |
318 | * Check if the virtual address starts on page boundary. |
||
319 | */ |
||
320 | if (ALIGN_UP(entry->p_vaddr, PAGE_SIZE) != entry->p_vaddr) { |
||
321 | printf("align check 2 failed - not page-aligned\n"); |
||
322 | printf("vaddr = 0x%x, should be 0x%x\n", |
||
323 | entry->p_vaddr, ALIGN_UP(entry->p_vaddr, PAGE_SIZE)); |
||
324 | return EE_UNSUPPORTED; |
||
325 | } |
||
326 | |||
2932 | svoboda | 327 | printf("map to p_vaddr=0x%x-0x%x...\n", entry->p_vaddr + bias, |
328 | entry->p_vaddr + bias + ALIGN_UP(entry->p_memsz, PAGE_SIZE)); |
||
2928 | svoboda | 329 | |
2985 | svoboda | 330 | /* |
331 | * For the course of loading, the area needs to be readable |
||
332 | * and writeable. |
||
333 | */ |
||
2932 | svoboda | 334 | a = as_area_create((uint8_t *)entry->p_vaddr + bias, |
2985 | svoboda | 335 | entry->p_memsz, AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE); |
2928 | svoboda | 336 | if (a == (void *)(-1)) { |
337 | printf("memory mapping failed\n"); |
||
338 | return EE_MEMORY; |
||
339 | } |
||
340 | |||
2992 | svoboda | 341 | printf("as_area_create(0x%lx, 0x%x, %d) -> 0x%lx\n", |
342 | entry->p_vaddr+bias, entry->p_memsz, flags, (uintptr_t)a); |
||
2928 | svoboda | 343 | |
344 | /* |
||
345 | * Load segment data |
||
346 | */ |
||
2949 | svoboda | 347 | printf("seek to %d\n", entry->p_offset); |
2964 | svoboda | 348 | rc = lseek(elf->fd, entry->p_offset, SEEK_SET); |
2928 | svoboda | 349 | if (rc < 0) { printf("seek error\n"); return EE_INVALID; } |
350 | |||
2949 | svoboda | 351 | printf("read 0x%x bytes to address 0x%x\n", entry->p_filesz, entry->p_vaddr+bias); |
352 | /* rc = read(fd, (void *)(entry->p_vaddr + bias), entry->p_filesz); |
||
353 | if (rc < 0) { printf("read error\n"); return EE_INVALID; }*/ |
||
354 | unsigned left, now; |
||
355 | uint8_t *dp; |
||
2928 | svoboda | 356 | |
2949 | svoboda | 357 | left = entry->p_filesz; |
358 | dp = (uint8_t *)(entry->p_vaddr + bias); |
||
359 | |||
360 | while (left > 0) { |
||
2977 | svoboda | 361 | now = 16384; |
2949 | svoboda | 362 | if (now > left) now=left; |
363 | printf("read %d...", now); |
||
2964 | svoboda | 364 | rc = read(elf->fd, dp, now); |
2949 | svoboda | 365 | if (rc < 0) { printf("read error\n"); return EE_INVALID; } |
366 | printf("->%d\n", rc); |
||
367 | left -= now; |
||
368 | dp += now; |
||
369 | } |
||
370 | |||
2985 | svoboda | 371 | printf("set area flags to %d\n", flags); |
372 | rc = as_area_change_flags((uint8_t *)entry->p_vaddr + bias, flags); |
||
373 | if (rc != 0) { |
||
374 | printf("failed to set memory area flags\n"); |
||
375 | return EE_MEMORY; |
||
376 | } |
||
377 | |||
2928 | svoboda | 378 | return EE_OK; |
379 | } |
||
380 | |||
381 | /** Process section header. |
||
382 | * |
||
383 | * @param entry Segment header. |
||
384 | * @param elf ELF header. |
||
385 | * |
||
386 | * @return EE_OK on success, error code otherwise. |
||
387 | */ |
||
2964 | svoboda | 388 | static int section_header(elf_ld_t *elf, elf_section_header_t *entry) |
2928 | svoboda | 389 | { |
390 | switch (entry->sh_type) { |
||
391 | case SHT_PROGBITS: |
||
392 | if (entry->sh_flags & SHF_TLS) { |
||
393 | /* .tdata */ |
||
394 | } |
||
395 | break; |
||
396 | case SHT_NOBITS: |
||
397 | if (entry->sh_flags & SHF_TLS) { |
||
398 | /* .tbss */ |
||
399 | } |
||
400 | break; |
||
2961 | svoboda | 401 | case SHT_DYNAMIC: |
2964 | svoboda | 402 | elf->info->dynamic = |
403 | (void *)((uint8_t *)entry->sh_addr + elf->bias); |
||
404 | printf("dynamic section found at 0x%x\n", |
||
405 | (uintptr_t)elf->info->dynamic); |
||
2961 | svoboda | 406 | break; |
2928 | svoboda | 407 | default: |
408 | break; |
||
409 | } |
||
410 | |||
411 | return EE_OK; |
||
412 | } |
||
413 | |||
414 | /** @} |
||
415 | */ |