arch/m68k-amiga: Provide dummy KrnObtainInput/KrnReleaseInput routines
[AROS.git] / bootstrap / elfloader.c
blobe16a4bdf1d173e7b1e23dae4cccd87b8f38583f4
1 /*
2 Copyright (C) 2006-2011 The AROS Development Team. All rights reserved.
3 $Id$
5 Desc: ELF loader extracted from our internal_load_seg_elf in dos.library.
6 Lang: English
7 */
9 #include <inttypes.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
14 /* Define this wrapper here, before loading AROS headers,
15 * so that the defines in <aros/system.h> do not
16 * confuse GCC's built-in substitutions for strcmp().
18 static inline int Strcmp(const char *a, const char *b) { return strcmp(a, b); }
20 #include <dos/elf.h>
21 #include <libraries/debug.h>
23 #include <elfloader.h>
24 #include <runtime.h>
26 #define D(x)
27 #define DREL(x)
28 #define DSYM(x)
30 /* Use own definitions because we may be compiled as 32-bit code but build structures for 64-bit code */
31 struct ELF_ModuleInfo_t
33 elf_uintptr_t Next;
34 elf_uintptr_t Name;
35 unsigned short Type;
36 unsigned short Pad0; /* On i386 we have different alignment, so do explicit padding */
37 #ifdef ELF_64BIT
38 unsigned int Pad1;
39 #endif
40 elf_uintptr_t eh;
41 elf_uintptr_t sh;
44 /* Our own definition of struct KernelBSS, to avoid excessive castings */
45 struct KernelBSS_t
47 elf_uintptr_t addr;
48 elf_uintptr_t len;
51 static elf_uintptr_t SysBase_ptr = 0;
54 * Test for correct ELF header here
56 static char *check_header(struct elfheader *eh)
58 if (eh->ident[0] != 0x7f || eh->ident[1] != 'E' ||
59 eh->ident[2] != 'L' || eh->ident[3] != 'F')
60 return "Not a ELF file";
62 if (eh->type != ET_REL || eh->machine != AROS_ELF_MACHINE)
63 return "Wrong object type or wrong architecture";
65 /* No error */
66 return NULL;
70 * Get the memory for chunk and load it
72 static void *load_hunk(void *file, struct sheader *sh, void *addr, struct KernelBSS_t **bss_tracker)
74 uintptr_t align;
76 /* empty chunk? Who cares :) */
77 if (!sh->size)
78 return addr;
80 D(kprintf("[ELF Loader] Chunk (%ld bytes, align=%ld (%p) @ ", sh->size, sh->addralign, (void *)sh->addralign));
81 align = sh->addralign - 1;
82 addr = (char *)(((uintptr_t)addr + align) & ~align);
84 D(kprintf("%p\n", addr));
85 sh->addr = (elf_ptr_t)(uintptr_t)addr;
87 /* copy block of memory from ELF file if it exists */
88 if (sh->type != SHT_NOBITS)
90 if (read_block(file, sh->offset, (void *)(uintptr_t)sh->addr, sh->size))
91 return NULL;
93 else
95 memset(addr, 0, sh->size);
97 (*bss_tracker)->addr = (uintptr_t)addr;
98 (*bss_tracker)->len = sh->size;
99 (*bss_tracker)++;
102 return addr + sh->size;
105 static void *copy_data(void *src, void *addr, uintptr_t len)
107 memcpy(addr, src, len);
108 return addr + len;
111 /* Perform relocations of given section */
112 static int relocate(struct elfheader *eh, struct sheader *sh, long shrel_idx, elf_uintptr_t DefSysBase)
114 struct sheader *shrel = &sh[shrel_idx];
115 struct sheader *shsymtab = &sh[SHINDEX(shrel->link)];
116 struct sheader *toreloc = &sh[SHINDEX(shrel->info)];
118 struct symbol *symtab = (struct symbol *)(uintptr_t)shsymtab->addr;
119 struct relo *rel = (struct relo *)(uintptr_t)shrel->addr;
120 /* Early cast to uintptr_t omits __udivdi3 call in x86-64 native bootstrap */
121 unsigned int numrel = (uintptr_t)shrel->size / (uintptr_t)shrel->entsize;
122 unsigned int i;
124 struct symbol *SysBase_sym = NULL;
127 * Ignore relocs if the target section has no allocation. that can happen
128 * eg. with a .debug PROGBITS and a .rel.debug section
130 if (!(toreloc->flags & SHF_ALLOC))
131 return 1;
133 DREL(kprintf("[ELF Loader] performing %d relocations\n", numrel));
135 for (i=0; i<numrel; i++, rel++)
137 struct symbol *sym = &symtab[ELF_R_SYM(rel->info)];
138 uintptr_t *p = (void *)(uintptr_t)toreloc->addr + rel->offset;
139 const char *name = (const char *)(uintptr_t)sh[shsymtab->link].addr + sym->name;
140 elf_uintptr_t s;
142 #ifdef __arm__
144 * R_ARM_V4BX are actually special marks for the linker.
145 * They even never have a target (shindex == SHN_UNDEF),
146 * so we simply ignore them before doing any checks.
148 if (ELF_R_TYPE(rel->info) == R_ARM_V4BX)
149 continue;
150 #endif
152 switch (sym->shindex)
154 case SHN_UNDEF:
155 if (Strcmp(name, "SysBase") == 0) {
156 if (!SysBase_ptr)
158 SysBase_ptr = DefSysBase;
159 D(kprintf("[ELF Loader] SysBase pointer set to default %p\n", (void *)SysBase_ptr));
162 s = SysBase_ptr;
163 } else {
164 DREL(kprintf("[ELF Loader] Undefined symbol '%s'\n", name));
165 return 0;
167 break;
169 case SHN_COMMON:
170 DREL(kprintf("[ELF Loader] COMMON symbol '%s'\n", name));
171 return 0;
173 case SHN_ABS:
174 if (SysBase_sym == NULL)
176 if (Strcmp(name, "SysBase") == 0)
178 DREL(kprintf("[ELF Loader] got SysBase\n"));
179 SysBase_sym = sym;
183 if (SysBase_sym == sym)
185 if (!SysBase_ptr)
187 SysBase_ptr = DefSysBase;
188 D(kprintf("[ELF Loader] SysBase pointer set to default %p\n", (void *)SysBase_ptr));
191 s = SysBase_ptr;
193 else
194 s = sym->value;
195 break;
197 default:
198 s = (uintptr_t)sh[sym->shindex].addr + sym->value;
200 if (!SysBase_ptr)
203 * The first global data symbol named SysBase becomes global SysBase.
204 * The idea behind: the first module (kernel.resource) contains global
205 * SysBase variable and all other modules are linked to it.
207 if (sym->info == ELF_S_INFO(STB_GLOBAL, STT_OBJECT))
209 if (Strcmp(name, "SysBase") == 0)
211 SysBase_ptr = s;
212 D(kprintf("[ELF Loader] SysBase pointer set to %p\n", (void *)SysBase_ptr));
218 DREL(kprintf("[ELF Loader] Relocating symbol %s type ", sym->name ? name : "<unknown>"));
219 switch (ELF_R_TYPE(rel->info))
221 #ifdef ELF_64BIT
222 case R_X86_64_64: /* 64bit direct/absolute */
223 *(uint64_t *)p = s + rel->addend;
224 break;
226 case R_X86_64_PC32: /* PC relative 32 bit signed */
227 *(uint32_t *)p = s + rel->addend - (uintptr_t) p;
228 break;
230 case R_X86_64_32:
231 *(uint32_t *)p = (uint64_t)s + (uint64_t)rel->addend;
232 break;
234 case R_X86_64_32S:
235 *(int32_t *)p = (int64_t)s + (int64_t)rel->addend;
236 break;
238 case R_X86_64_NONE: /* No reloc */
239 break;
240 #else
241 #ifdef __i386__
242 case R_386_32: /* 32bit absolute */
243 DREL(kprintf("R_386_32"));
244 *p += s;
245 break;
247 case R_386_PC32: /* 32bit PC relative */
248 DREL(kprintf("R_386_PC32"));
249 *p += (s - (uintptr_t)p);
250 break;
252 case R_386_NONE:
253 DREL(kprintf("R_386_NONE"));
254 break;
255 #endif
256 #endif
257 #ifdef __mc68000__
258 case R_68K_32:
259 *p = s + rel->addend;
260 break;
262 case R_68K_PC32:
263 *p = s + rel->addend - (uint32_t)p;
264 break;
266 case R_68k_NONE:
267 break;
268 #endif
269 #if defined(__ppc__) || defined(__powerpc__)
270 case R_PPC_ADDR32:
271 *p = s + rel->addend;
272 break;
274 case R_PPC_ADDR16_LO:
276 unsigned short *c = (unsigned short *) p;
277 *c = (s + rel->addend) & 0xffff;
279 break;
281 case R_PPC_ADDR16_HA:
283 unsigned short *c = (unsigned short *) p;
284 uint32_t temp = s + rel->addend;
285 *c = temp >> 16;
286 if ((temp & 0x8000) != 0)
287 (*c)++;
289 break;
291 case R_PPC_REL16_LO:
293 unsigned short *c = (unsigned short *) p;
294 *c = (s + rel->addend - (uint32_t)p) & 0xffff;
296 break;
298 case R_PPC_REL16_HA:
300 unsigned short *c = (unsigned short *) p;
301 uint32_t temp = s + rel->addend - (uint32_t)p;
302 *c = temp >> 16;
303 if ((temp & 0x8000) != 0)
304 (*c)++;
306 break;
308 case R_PPC_REL24:
309 *p &= ~0x3fffffc;
310 *p |= (s + rel->addend - (uint32_t)p) & 0x3fffffc;
311 break;
313 case R_PPC_REL32:
314 *p = s + rel->addend - (uint32_t)p;
315 break;
317 case R_PPC_NONE:
318 break;
319 #endif
320 #ifdef __arm__
321 case R_ARM_CALL:
322 case R_ARM_JUMP24:
323 case R_ARM_PC24:
324 case R_ARM_PREL31:
326 /* On ARM the 24 bit offset is shifted by 2 to the right */
327 signed long offset = (*p & 0x00ffffff) << 2;
328 /* If highest bit set, make offset negative */
329 if (offset & 0x02000000)
330 offset -= 0x04000000;
332 offset += s - (uint32_t)p;
334 offset >>= 2;
335 *p &= 0xff000000;
336 *p |= offset & 0x00ffffff;
338 break;
340 case R_ARM_MOVW_ABS_NC:
341 case R_ARM_MOVT_ABS:
343 signed long offset = *p;
344 offset = ((offset & 0xf0000) >> 4) | (offset & 0xfff);
345 offset = (offset ^ 0x8000) - 0x8000;
347 offset += s;
349 if (ELF_R_TYPE(rel->info) == R_ARM_MOVT_ABS)
350 offset >>= 16;
352 *p &= 0xfff0f000;
353 *p |= ((offset & 0xf000) << 4) | (offset & 0x0fff);
355 break;
357 case R_ARM_ABS32:
358 *p += s;
359 break;
361 case R_ARM_NONE:
362 break;
363 #endif
364 default:
365 kprintf("[ELF Loader] Unrecognized relocation type %d %ld\n", i, (long)ELF_R_TYPE(rel->info));
366 return 0;
368 DREL(kprintf(" -> %p\n", *p));
370 return 1;
373 int GetKernelSize(struct ELFNode *FirstELF, unsigned long *ro_size, unsigned long *rw_size, unsigned long *bss_size)
375 struct ELFNode *n;
376 unsigned long ksize = 0;
377 unsigned long rwsize = 0;
378 unsigned long bsize = sizeof(struct KernelBSS_t);
379 unsigned short i;
381 kprintf("[ELF Loader] Calculating kickstart size...\n");
383 for (n = FirstELF; n; n = n->Next)
385 void *file;
386 char *errstr = NULL;
387 unsigned int err;
389 D(kprintf("[ELF Loader] Checking file %s\n", n->Name));
391 file = open_file(n, &err);
392 if (err)
394 DisplayError("Failed to open file %s!\n", n->Name);
395 return 0;
398 /* Check the header of ELF file */
399 n->eh = load_block(file, 0, sizeof(struct elfheader), &err);
400 if (err)
402 errstr = "Failed to read file header";
404 else
406 errstr = check_header(n->eh);
407 if (!errstr)
409 n->sh = load_block(file, n->eh->shoff, n->eh->shnum * n->eh->shentsize, &err);
410 if (err)
412 errstr = "Failed to read section headers";
417 close_file(file);
418 if (errstr)
420 DisplayError("%s: %s\n", n->Name, errstr);
421 return 0;
425 * Debug data for the module includes:
426 * - Module descriptor (struct ELF_ModuleInfo_t)
427 * - ELF file header
428 * - ELF section header
429 * - File name
430 * - One empty pointer for alignment
432 ksize += (sizeof(struct ELF_ModuleInfo_t) + sizeof(struct elfheader) + n->eh->shnum * n->eh->shentsize +
433 strlen(n->Name) + sizeof(void *));
435 /* Go through all sections and calculate kernel size */
436 for(i = 0; i < n->eh->shnum; i++)
438 /* Ignore sections with zero lengths */
439 if (!n->sh[i].size)
440 continue;
443 * We will load:
444 * - Actual code and data (allocated sections)
445 * - String tables (for debug data)
446 * - Symbol tables (for debug data)
448 if ((n->sh[i].flags & SHF_ALLOC) || (n->sh[i].type == SHT_STRTAB) || (n->sh[i].type == SHT_SYMTAB))
450 /* Add maximum space for alignment */
451 unsigned long s = n->sh[i].size + n->sh[i].addralign - 1;
453 if (n->sh[i].flags & SHF_WRITE)
454 rwsize += s;
455 else
456 ksize += s;
458 if (n->sh[i].type == SHT_NOBITS)
459 bsize += sizeof(struct KernelBSS_t);
464 *ro_size = ksize;
465 *rw_size = rwsize;
467 if (bss_size)
468 *bss_size = bsize;
470 kprintf("[ELF Loader] Code %lu bytes, data %lu bytes, BSS array %lu bytes\n", ksize, rwsize, bsize);
472 return 1;
476 * This function loads the listed modules.
477 * It expects that ELF and section header pointers in the list are already set up by GetKernelSize().
479 * (elf_ptr_t)(uintptr_t) double-casting is needed because in some cases elf_ptr_t is an UQUAD,
480 * while in most cases it's a pointer (see dos/elf.h).
482 int LoadKernel(struct ELFNode *FirstELF, void *ptr_ro, void *ptr_rw, void *tracker, uintptr_t DefSysBase,
483 void **kick_end, kernel_entry_fun_t *kernel_entry, struct ELF_ModuleInfo **kernel_debug)
485 struct ELFNode *n;
486 unsigned int i;
487 unsigned char need_entry = 1;
488 struct ELF_ModuleInfo_t *mod;
489 struct ELF_ModuleInfo_t *prev_mod = NULL;
491 kprintf("[ELF Loader] Loading kickstart...\n");
493 for (n = FirstELF; n; n = n->Next)
495 void *file;
496 unsigned int err;
498 kprintf("[ELF Loader] Code %p, Data %p, Module %s...\n", ptr_ro, ptr_rw, n->Name);
500 file = open_file(n, &err);
501 if (err)
503 DisplayError("Failed to open file %s!\n", n->Name);
504 return 0;
507 /* Iterate over the section header in order to load some hunks */
508 for (i=0; i < n->eh->shnum; i++)
510 struct sheader *sh = n->sh;
512 D(kprintf("[ELF Loader] Section %u... ", i));
514 if ((sh[i].flags & SHF_ALLOC) || (sh[i].type == SHT_STRTAB) || (sh[i].type == SHT_SYMTAB))
516 /* Does the section require memory allcation? */
517 D(kprintf("Allocated section\n"));
519 if (sh[i].flags & SHF_WRITE)
521 ptr_rw = load_hunk(file, &sh[i], (void *)ptr_rw, (struct KernelBSS_t **)&tracker);
522 if (!ptr_rw)
524 DisplayError("%s: Error loading hunk %u!\n", n->Name, i);
525 return 0;
528 else
530 ptr_ro = load_hunk(file, &sh[i], (void *)ptr_ro, (struct KernelBSS_t **)&tracker);
531 if (!ptr_ro)
533 DisplayError("%s: Error loading hunk %u!\n", n->Name, i);
534 return 0;
538 /* Remember address of the first code section, this is our entry point */
539 if ((sh[i].flags & SHF_EXECINSTR) && need_entry)
541 *kernel_entry = (void *)(uintptr_t)sh[i].addr;
542 need_entry = 0;
545 D(else kprintf("Ignored\n");)
547 D(kprintf("[ELF Loader] Section address: %p, size: %lu\n", sh[i].addr, sh[i].size));
550 /* For every loaded section perform relocations */
551 D(kprintf("[ELF Loader] Relocating...\n"));
552 for (i=0; i < n->eh->shnum; i++)
554 struct sheader *sh = n->sh;
556 if ((sh[i].type == AROS_ELF_REL) && sh[sh[i].info].addr)
558 sh[i].addr = (elf_ptr_t)(uintptr_t)load_block(file, sh[i].offset, sh[i].size, &err);
559 if (err)
561 DisplayError("%s: Failed to load relocation section %u\n", n->Name, i);
562 return 0;
565 if (!relocate(n->eh, sh, i, (uintptr_t)DefSysBase))
567 DisplayError("%s: Relocation error in section %u!\n", n->Name, i);
568 return 0;
571 free_block((void *)(uintptr_t)sh[i].addr);
572 sh[i].addr = (elf_ptr_t)0;
576 close_file(file);
578 D(kprintf("[ELF Loader] Adding module debug information...\n"));
580 /* Align our pointer */
581 ptr_ro = (void *)(((uintptr_t)ptr_ro + sizeof(void *)) & ~(sizeof(void *) - 1));
583 /* Allocate module descriptor */
584 mod = ptr_ro;
585 ptr_ro += sizeof(struct ELF_ModuleInfo_t);
586 mod->Next = 0;
587 mod->Type = DEBUG_ELF;
589 /* Copy ELF header */
590 mod->eh = (uintptr_t)ptr_ro;
591 ptr_ro = copy_data(n->eh, ptr_ro, sizeof(struct elfheader));
593 /* Copy section header */
594 mod->sh = (uintptr_t)ptr_ro;
595 ptr_ro = copy_data(n->sh, ptr_ro, n->eh->shnum * n->eh->shentsize);
597 /* Copy module name */
598 mod->Name = (uintptr_t)ptr_ro;
599 ptr_ro = copy_data(n->Name, ptr_ro, strlen(n->Name) + 1);
601 /* Link the module descriptor with previous one */
602 if (prev_mod)
603 prev_mod->Next = (uintptr_t)mod;
604 else
605 *kernel_debug = (struct ELF_ModuleInfo *)mod;
606 prev_mod = mod;
608 free_block(n->sh);
609 free_block(n->eh);
612 /* Terminate the array of BSS sections */
613 ((struct KernelBSS_t *)tracker)->addr = 0;
614 ((struct KernelBSS_t *)tracker)->len = 0;
616 /* Return end of kickstart read-only area if requested */
617 if (kick_end)
618 *kick_end = ptr_ro;
620 return 1;