detect if the compiler supports -fno-builtin and -fno-builtin-vsnprintf. (NicJA)
[AROS.git] / arch / armeb-raspi / boot / elf.c
blobce94243d65d3183970e9682b6eb9d755a36c36f6
1 /*
2 Copyright � 2013, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc: elf.c
6 Lang: english
7 */
9 #include "elf.h"
10 #include "boot.h"
12 #include <dos/elf.h>
13 #include <stdlib.h>
14 #include <string.h>
16 #define DELF(x) /* x */
18 uint32_t int_shnum;
19 uint32_t int_shstrndx;
21 int checkHeader(struct elfheader *eh)
23 if (eh->ident[0] != 0x7f || eh->ident[1] != 'E' ||
24 eh->ident[2] != 'L' || eh->ident[3] != 'F')
26 return 0;
29 int_shnum = eh->shnum;
30 int_shstrndx = eh->shstrndx;
32 /* the ELF header only uses 16 bits to store the count of section headers,
33 * so it can't handle more than 65535 headers. if the count is 0, and an
34 * offset is defined, then the real count can be found in the first
35 * section header (which always exists).
37 * similarly, if the string table index is SHN_XINDEX, then the actual
38 * index is found in the first section header also.
40 * see the System V ABI 2001-04-24 draft for more details.
42 if (int_shnum == 0 || int_shstrndx == SHN_XINDEX)
44 if (eh->shoff == 0)
46 return 0;
49 /* Get section header. I hope it's there, in memory already. */
50 struct sheader *sh = (struct sheader *)((intptr_t)eh + eh->shoff);
52 /* wider section header count is in the size field */
53 if (int_shnum == 0)
54 int_shnum = sh->size;
56 /* wider string table index is in the link field */
57 if (int_shstrndx == SHN_XINDEX)
58 int_shstrndx = sh->link;
60 /* sanity, if they're still invalid then this isn't elf */
61 if (int_shnum == 0 || int_shstrndx == SHN_XINDEX)
63 return 0;
69 eh->ident[EI_CLASS] != ELFCLASS32 ||
70 eh->ident[EI_VERSION] != EV_CURRENT ||
71 eh->type != ET_REL ||
72 eh->ident[EI_DATA] != ELFDATA2MSB ||
73 eh->machine != EM_ARM
76 return 0;
79 return 1;
82 int getElfSize(void *elf_file, uint32_t *size_rw, uint32_t *size_ro)
84 struct elfheader *eh = (struct elfheader *)elf_file;
85 uint32_t s_ro = 0;
86 uint32_t s_rw = 0;
88 DELF(kprintf("[BOOT:ELF] getElfSize(%p)", eh));
90 if (checkHeader(eh))
92 struct sheader *sh = (struct sheader *)((intptr_t)elf_file + eh->shoff);
93 int i;
95 for (i = 0; i < int_shnum; i++)
97 /* Does the section require memoy allcation? */
98 if (sh[i].flags & SHF_ALLOC)
100 uint32_t size = (sh[i].size + sh[i].addralign - 1) & ~(sh[i].addralign - 1);
103 * I extend the section size according to the alignment requirement. However, also the already
104 * measured size has to be aligned poperly. It is so, because the loader has to align the load address later on.
106 if (sh[i].flags & SHF_WRITE)
108 s_rw = (s_rw + sh[i].addralign - 1) & ~(sh[i].addralign - 1);
109 s_rw += size;
111 else
113 s_ro = (s_ro + sh[i].addralign - 1) & ~(sh[i].addralign - 1);
114 s_ro += size;
119 DELF(kprintf(": ro=%p, rw=%p\n", s_ro, s_rw));
121 if (size_ro)
122 *size_ro = s_ro;
123 if (size_rw)
124 *size_rw = s_rw;
126 return 1;
129 static uintptr_t ptr_ro;
130 static uintptr_t ptr_rw;
131 static uintptr_t virtoffset;
133 void initAllocator(uintptr_t addr_ro, uintptr_t addr_rw, uintptr_t virtoff)
135 ptr_ro = addr_ro;
136 ptr_rw = addr_rw;
137 virtoffset = virtoff;
140 struct bss_tracker tracker[MAX_BSS_SECTIONS];
141 static struct bss_tracker *bss_tracker = &tracker[0];
144 * read_block function copies the range of memory within ELF file to any specified location.
146 static inline void read_block(void *file, long offset, void *dest, long length)
148 memcpy(dest, (void *)((unsigned long)file + offset), length);
152 * Get the memory for chunk and load it
154 static int load_hunk(void *file, struct sheader *sh)
156 void *ptr = (void *)0;
158 /* empty chunk? Who cares :) */
159 if (!sh->size)
160 return 1;
162 /* Allocate a chunk with write access */
163 if (sh->flags & SHF_WRITE)
165 ptr_rw = (((unsigned long)ptr_rw
166 + (unsigned long)sh->addralign - 1)
167 & ~((unsigned long)sh->addralign - 1));
168 ptr = (APTR)ptr_rw;
169 ptr_rw = ptr_rw + sh->size;
171 else
173 /* Read-Only mode? Get the memory from the kernel space, align it accorting to the demand */
174 ptr_ro = (((unsigned long)ptr_ro
175 + (unsigned long)sh->addralign - 1)
176 & ~((unsigned long)sh->addralign - 1));
177 ptr = (APTR)ptr_ro;
178 ptr_ro = ptr_ro + sh->size;
181 sh->addr = ptr;
183 /* copy block of memory from ELF file if it exists */
184 if (sh->type != SHT_NOBITS)
186 read_block(file, sh->offset, (void *)((unsigned long)sh->addr),
187 sh->size);
189 else
191 bzero(ptr, sh->size);
192 bss_tracker->addr =
193 (void *)((unsigned long)ptr + virtoffset);
194 bss_tracker->length = sh->size;
195 bss_tracker++;
197 * empty the subsequent tracker in case it's the last one. We did that in case a buggy firmare
198 * forgot to clear our .bss section
200 bss_tracker->addr = (void*)0;
201 bss_tracker->length = 0;
204 return 1;
207 /* Perform relocations of given section */
208 static int relocate(struct elfheader *eh, struct sheader *sh, long shrel_idx,
209 uint32_t virt)
211 struct sheader *shrel = &sh[shrel_idx];
212 struct sheader *shsymtab = &sh[shrel->link];
213 struct sheader *toreloc = &sh[shrel->info];
215 struct symbol *symtab =
216 (struct symbol *)((unsigned long)shsymtab->addr);
217 struct relo *rel = (struct relo *)((unsigned long)shrel->addr);
218 char *section = (char *)((unsigned long)toreloc->addr);
220 unsigned int numrel = (unsigned long)shrel->size
221 / (unsigned long)shrel->entsize;
222 unsigned int i;
224 uint32_t virtoffset;
226 struct symbol *SysBase_sym = (void *)0;
228 for (i = 0; i < numrel; i++, rel++)
230 struct symbol *sym = &symtab[ELF32_R_SYM(rel->info)];
231 uint32_t *p = (uint32_t *) & section[rel->offset];
232 uint32_t s;
233 virtoffset = virt;
236 * R_ARM_V4BX are actually special marks for the linker.
237 * They even never have a target (shindex == SHN_UNDEF),
238 * so we simply ignore them before doing any checks.
240 if (ELF_R_TYPE(rel->info) == R_ARM_V4BX)
241 continue;
243 switch (sym->shindex)
245 case SHN_UNDEF:
246 kprintf
247 ("[BOOT:ELF] Undefined symbol '%s' in section '%s'\n",
248 (char *)((uint32_t) sh[shsymtab->link].addr) +
249 sym->name,
250 (char *)((uint32_t) sh[eh->shstrndx].addr) +
251 toreloc->name);
252 return 0;
254 case SHN_COMMON:
255 kprintf
256 ("[BOOT:ELF] COMMON symbol '%s' in section '%s'\n",
257 (char *)((uint32_t) sh[shsymtab->link].addr) +
258 sym->name,
259 (char *)((uint32_t) sh[eh->shstrndx].addr) +
260 toreloc->name);
262 return 0;
264 case SHN_ABS:
265 if (SysBase_sym == (void *)0) {
266 if (strncmp
267 ((char *)((uint32_t) sh[shsymtab->link].
268 addr) + sym->name, "SysBase",
269 8) == 0) {
270 SysBase_sym = sym;
271 goto SysBase_yes;
272 } else
273 goto SysBase_no;
274 } else if (SysBase_sym == sym) {
275 SysBase_yes: s = (uint32_t) 4UL;
276 virtoffset = 0;
277 } else
278 SysBase_no: s = sym->value;
279 break;
280 default:
281 s = (uint32_t) sh[sym->shindex].addr + sym->value;
283 switch (ELF32_R_TYPE(rel->info)) {
285 // case R_386_32: /* 32bit direct/absolute */
286 // *p += s + virtoffset;
287 // break;
289 case R_ARM_CALL:
290 case R_ARM_JUMP24:
291 case R_ARM_PC24:
293 /* On ARM the 24 bit offset is shifted by 2 to the right */
294 signed long offset = (AROS_LE2LONG(*p) & 0x00ffffff) << 2;
295 /* If highest bit set, make offset negative */
296 if (offset & 0x02000000)
297 offset -= 0x04000000;
299 offset += s - (ULONG)p;
301 offset >>= 2;
302 *p &= AROS_LONG2LE(0xff000000);
303 *p |= AROS_LONG2LE(offset & 0x00ffffff);
305 break;
308 case R_ARM_MOVW_ABS_NC:
309 case R_ARM_MOVT_ABS:
311 signed long offset = AROS_LE2LONG(*p);
312 offset = ((offset & 0xf0000) >> 4) | (offset & 0xfff);
313 offset = (offset ^ 0x8000) - 0x8000;
315 offset += s + virtoffset;
317 if (ELF_R_TYPE(rel->info) == R_ARM_MOVT_ABS)
318 offset >>= 16;
320 *p &= AROS_LONG2LE(0xfff0f000);
321 *p |= AROS_LONG2LE(((offset & 0xf000) << 4) | (offset & 0x0fff));
323 break;
325 case R_ARM_ABS32: /* PC relative 32 bit signed */
326 *p += s + virtoffset;
327 break;
329 case R_ARM_NONE: /* No reloc */
330 break;
332 default:
333 kprintf("[BOOT:ELF] Unknown relocation %d in ELF file\n",
334 ELF32_R_TYPE(rel->info));
335 return 0;
338 return 1;
342 int loadElf(void *elf_file)
344 struct elfheader *eh = (struct elfheader *)elf_file;
345 //uint32_t s_ro = 0;
346 //uint32_t s_rw = 0;
348 DELF(kprintf("[BOOT] loadElf(%p)\n", eh));
350 if (checkHeader(eh))
352 struct sheader *sh = (struct sheader *)((intptr_t)elf_file + eh->shoff);
353 int i;
355 for (i = 0; i < int_shnum; i++)
357 /* Load the symbol and string tables */
358 if (sh[i].type == SHT_SYMTAB || sh[i].type == SHT_STRTAB)
360 sh[i].addr = (APTR)((unsigned long)elf_file + sh[i].offset);
362 /* Does the section require memoy allcation? */
363 else if (sh[i].flags & SHF_ALLOC)
365 /* Yup, it does. Load the hunk */
366 if (!load_hunk(elf_file, &sh[i]))
368 return 0;
370 else
372 if (sh[i].size)
374 DELF(kprintf("[BOOT:ELF] %s section loaded at %p (Virtual addr: %p)\n",
375 sh[i].flags & SHF_WRITE ? "RW":"RO",
376 sh[i].addr,
377 sh[i].addr + virtoffset));
383 /* For every loaded section perform the relocations */
384 for (i = 0; i < int_shnum; i++)
386 if (sh[i].type == SHT_REL && sh[sh[i].info].addr)
388 sh[i].addr = (APTR)((uint32_t) elf_file + sh[i].offset);
389 if (!sh[i].addr || !relocate(eh, sh, i, virtoffset))
391 return 0;
396 return 1;