Rev 3131 | Rev 3157 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 3131 | Rev 3155 | ||
---|---|---|---|
Line 90... | Line 90... | ||
90 | elf_ld_t elf; |
90 | elf_ld_t elf; |
91 | 91 | ||
92 | int fd; |
92 | int fd; |
93 | int rc; |
93 | int rc; |
94 | 94 | ||
95 | printf("open and read '%s'...\n", file_name); |
95 | // printf("open and read '%s'...\n", file_name); |
96 | 96 | ||
97 | fd = open(file_name, O_RDONLY); |
97 | fd = open(file_name, O_RDONLY); |
98 | if (fd < 0) { |
98 | if (fd < 0) { |
99 | printf("failed opening file\n"); |
99 | printf("failed opening file\n"); |
100 | return -1; |
100 | return -1; |
Line 102... | Line 102... | ||
102 | 102 | ||
103 | elf.fd = fd; |
103 | elf.fd = fd; |
104 | elf.info = info; |
104 | elf.info = info; |
105 | 105 | ||
106 | rc = elf_load(&elf, so_bias); |
106 | rc = elf_load(&elf, so_bias); |
107 | printf("elf_load() -> %d\n", rc); |
- | |
108 | 107 | ||
109 | close(fd); |
108 | close(fd); |
110 | 109 | ||
111 | return rc; |
110 | return rc; |
112 | } |
111 | } |
Line 118... | Line 117... | ||
118 | * |
117 | * |
119 | * @param info Info structure filled earlier by elf_load_file() |
118 | * @param info Info structure filled earlier by elf_load_file() |
120 | */ |
119 | */ |
121 | void elf_run(elf_info_t *info) |
120 | void elf_run(elf_info_t *info) |
122 | { |
121 | { |
123 | printf("entry point: 0x%llx\n", info->entry); |
- | |
124 | //(*info->entry)(); |
- | |
125 | program_run(info->entry); |
122 | program_run(info->entry); |
126 | 123 | ||
127 | /* not reached */ |
124 | /* not reached */ |
128 | } |
125 | } |
129 | 126 | ||
Line 177... | Line 174... | ||
177 | return EE_INVALID; |
174 | return EE_INVALID; |
178 | } |
175 | } |
179 | 176 | ||
180 | elf->header = header; |
177 | elf->header = header; |
181 | 178 | ||
182 | printf("ELF-load:"); |
179 | // printf("ELF-load:"); |
183 | /* Identify ELF */ |
180 | /* Identify ELF */ |
184 | if (header->e_ident[EI_MAG0] != ELFMAG0 || |
181 | if (header->e_ident[EI_MAG0] != ELFMAG0 || |
185 | header->e_ident[EI_MAG1] != ELFMAG1 || |
182 | header->e_ident[EI_MAG1] != ELFMAG1 || |
186 | header->e_ident[EI_MAG2] != ELFMAG2 || |
183 | header->e_ident[EI_MAG2] != ELFMAG2 || |
187 | header->e_ident[EI_MAG3] != ELFMAG3) { |
184 | header->e_ident[EI_MAG3] != ELFMAG3) { |
Line 216... | Line 213... | ||
216 | printf("Object type %d is not supported\n", header->e_type); |
213 | printf("Object type %d is not supported\n", header->e_type); |
217 | return EE_UNSUPPORTED; |
214 | return EE_UNSUPPORTED; |
218 | } |
215 | } |
219 | 216 | ||
220 | /* Shared objects can be loaded with a bias */ |
217 | /* Shared objects can be loaded with a bias */ |
221 | printf("Object type: %d\n", header->e_type); |
218 | // printf("Object type: %d\n", header->e_type); |
222 | if (header->e_type == ET_DYN) |
219 | if (header->e_type == ET_DYN) |
223 | elf->bias = so_bias; |
220 | elf->bias = so_bias; |
224 | else |
221 | else |
225 | elf->bias = 0; |
222 | elf->bias = 0; |
226 | 223 | ||
227 | printf("Bias set to 0x%x\n", elf->bias); |
224 | // printf("Bias set to 0x%x\n", elf->bias); |
228 | elf->info->interp = NULL; |
225 | elf->info->interp = NULL; |
229 | elf->info->dynamic = NULL; |
226 | elf->info->dynamic = NULL; |
230 | 227 | ||
231 | printf("parse segments\n"); |
228 | // printf("parse segments\n"); |
232 | 229 | ||
233 | /* Walk through all segment headers and process them. */ |
230 | /* Walk through all segment headers and process them. */ |
234 | for (i = 0; i < header->e_phnum; i++) { |
231 | for (i = 0; i < header->e_phnum; i++) { |
235 | elf_segment_header_t segment_hdr; |
232 | elf_segment_header_t segment_hdr; |
236 | 233 | ||
Line 244... | Line 241... | ||
244 | rc = segment_header(elf, &segment_hdr); |
241 | rc = segment_header(elf, &segment_hdr); |
245 | if (rc != EE_OK) |
242 | if (rc != EE_OK) |
246 | return rc; |
243 | return rc; |
247 | } |
244 | } |
248 | 245 | ||
249 | printf("parse sections\n"); |
246 | // printf("parse sections\n"); |
250 | 247 | ||
251 | /* Inspect all section headers and proccess them. */ |
248 | /* Inspect all section headers and proccess them. */ |
252 | for (i = 0; i < header->e_shnum; i++) { |
249 | for (i = 0; i < header->e_shnum; i++) { |
253 | elf_section_header_t section_hdr; |
250 | elf_section_header_t section_hdr; |
254 | 251 | ||
Line 265... | Line 262... | ||
265 | } |
262 | } |
266 | 263 | ||
267 | elf->info->entry = |
264 | elf->info->entry = |
268 | (entry_point_t)((uint8_t *)header->e_entry + elf->bias); |
265 | (entry_point_t)((uint8_t *)header->e_entry + elf->bias); |
269 | 266 | ||
270 | printf("done\n"); |
267 | // printf("done\n"); |
271 | 268 | ||
272 | return EE_OK; |
269 | return EE_OK; |
273 | } |
270 | } |
274 | 271 | ||
275 | /** Print error message according to error code. |
272 | /** Print error message according to error code. |
Line 331... | Line 328... | ||
331 | uintptr_t bias; |
328 | uintptr_t bias; |
332 | uintptr_t base; |
329 | uintptr_t base; |
333 | size_t mem_sz; |
330 | size_t mem_sz; |
334 | int rc; |
331 | int rc; |
335 | 332 | ||
336 | printf("load segment at addr 0x%x, size 0x%x\n", entry->p_vaddr, |
333 | // printf("load segment at addr 0x%x, size 0x%x\n", entry->p_vaddr, |
337 | entry->p_memsz); |
334 | // entry->p_memsz); |
338 | 335 | ||
339 | bias = elf->bias; |
336 | bias = elf->bias; |
340 | 337 | ||
341 | if (entry->p_align > 1) { |
338 | if (entry->p_align > 1) { |
342 | if ((entry->p_offset % entry->p_align) != |
339 | if ((entry->p_offset % entry->p_align) != |
Line 360... | Line 357... | ||
360 | flags |= AS_AREA_CACHEABLE; |
357 | flags |= AS_AREA_CACHEABLE; |
361 | 358 | ||
362 | base = ALIGN_DOWN(entry->p_vaddr, PAGE_SIZE); |
359 | base = ALIGN_DOWN(entry->p_vaddr, PAGE_SIZE); |
363 | mem_sz = entry->p_memsz + (entry->p_vaddr - base); |
360 | mem_sz = entry->p_memsz + (entry->p_vaddr - base); |
364 | 361 | ||
365 | printf("map to p_vaddr=0x%x-0x%x...\n", entry->p_vaddr + bias, |
362 | // printf("map to p_vaddr=0x%x-0x%x...\n", entry->p_vaddr + bias, |
366 | entry->p_vaddr + bias + ALIGN_UP(entry->p_memsz, PAGE_SIZE)); |
363 | // entry->p_vaddr + bias + ALIGN_UP(entry->p_memsz, PAGE_SIZE)); |
367 | 364 | ||
368 | /* |
365 | /* |
369 | * For the course of loading, the area needs to be readable |
366 | * For the course of loading, the area needs to be readable |
370 | * and writeable. |
367 | * and writeable. |
371 | */ |
368 | */ |
Line 374... | Line 371... | ||
374 | if (a == (void *)(-1)) { |
371 | if (a == (void *)(-1)) { |
375 | printf("memory mapping failed\n"); |
372 | printf("memory mapping failed\n"); |
376 | return EE_MEMORY; |
373 | return EE_MEMORY; |
377 | } |
374 | } |
378 | 375 | ||
379 | printf("as_area_create(0x%lx, 0x%x, %d) -> 0x%lx\n", |
376 | // printf("as_area_create(0x%lx, 0x%x, %d) -> 0x%lx\n", |
380 | entry->p_vaddr+bias, entry->p_memsz, flags, (uintptr_t)a); |
377 | // entry->p_vaddr+bias, entry->p_memsz, flags, (uintptr_t)a); |
381 | 378 | ||
382 | /* |
379 | /* |
383 | * Load segment data |
380 | * Load segment data |
384 | */ |
381 | */ |
385 | printf("seek to %d\n", entry->p_offset); |
382 | // printf("seek to %d\n", entry->p_offset); |
386 | rc = lseek(elf->fd, entry->p_offset, SEEK_SET); |
383 | rc = lseek(elf->fd, entry->p_offset, SEEK_SET); |
387 | if (rc < 0) { printf("seek error\n"); return EE_INVALID; } |
384 | if (rc < 0) { printf("seek error\n"); return EE_INVALID; } |
388 | 385 | ||
389 | printf("read 0x%x bytes to address 0x%x\n", entry->p_filesz, entry->p_vaddr+bias); |
386 | // printf("read 0x%x bytes to address 0x%x\n", entry->p_filesz, entry->p_vaddr+bias); |
390 | /* rc = read(fd, (void *)(entry->p_vaddr + bias), entry->p_filesz); |
387 | /* rc = read(fd, (void *)(entry->p_vaddr + bias), entry->p_filesz); |
391 | if (rc < 0) { printf("read error\n"); return EE_INVALID; }*/ |
388 | if (rc < 0) { printf("read error\n"); return EE_INVALID; }*/ |
392 | 389 | ||
393 | /* Long reads are not possible yet. Load segment picewise */ |
390 | /* Long reads are not possible yet. Load segment picewise */ |
394 | 391 | ||
Line 400... | Line 397... | ||
400 | 397 | ||
401 | while (left > 0) { |
398 | while (left > 0) { |
402 | now = 16384; |
399 | now = 16384; |
403 | if (now > left) now = left; |
400 | if (now > left) now = left; |
404 | 401 | ||
405 | printf("read %d...", now); |
402 | // printf("read %d...", now); |
406 | rc = read(elf->fd, dp, now); |
403 | rc = read(elf->fd, dp, now); |
407 | printf("->%d\n", rc); |
404 | // printf("->%d\n", rc); |
408 | 405 | ||
409 | if (rc < 0) { printf("read error\n"); return EE_INVALID; } |
406 | if (rc < 0) { printf("read error\n"); return EE_INVALID; } |
410 | 407 | ||
411 | left -= now; |
408 | left -= now; |
412 | dp += now; |
409 | dp += now; |
413 | } |
410 | } |
414 | 411 | ||
415 | printf("set area flags to %d\n", flags); |
412 | // printf("set area flags to %d\n", flags); |
416 | rc = as_area_change_flags((uint8_t *)entry->p_vaddr + bias, flags); |
413 | rc = as_area_change_flags((uint8_t *)entry->p_vaddr + bias, flags); |
417 | if (rc != 0) { |
414 | if (rc != 0) { |
418 | printf("failed to set memory area flags\n"); |
415 | printf("failed to set memory area flags\n"); |
419 | return EE_MEMORY; |
416 | return EE_MEMORY; |
420 | } |
417 | } |