2 * ELF loading subroutines
3 * NetWatch multiboot loader
5 * Copyright (c) 2008 Jacob Potter and Joshua Wise. All rights reserved.
6 * This program is free software; you can redistribute and/or modify it under
7 * the terms found in the file LICENSE in the root of this source tree.
15 static const unsigned char elf_ident[4] = { 0x7F, 'E', 'L', 'F' };
17 int load_elf (char * buf, int size) {
19 Elf32_Ehdr * elf_hdr = (Elf32_Ehdr *) buf;
20 Elf32_Shdr * elf_sec_hdrs = (Elf32_Shdr *) (buf + elf_hdr->e_shoff);
22 /* Sanity check on ELF file */
23 if (memcmp((void *)elf_hdr->e_ident, (void *)elf_ident, sizeof(elf_ident))) return -1;
24 if (elf_hdr->e_type != ET_EXEC) return -1;
25 if (elf_hdr->e_machine != EM_386) return -1;
26 if (elf_hdr->e_version != EV_CURRENT) return -1;
27 if (size < sizeof(Elf32_Ehdr)) return -1;
28 if (((char *)&elf_sec_hdrs[elf_hdr->e_shnum]) > (buf + size)) return -1;
30 char * string_table = buf + elf_sec_hdrs[elf_hdr->e_shstrndx].sh_offset;
32 if (string_table > (buf + size)) return -1;
35 for (i = 0; i < elf_hdr->e_shnum; i++) {
37 if (elf_sec_hdrs[i].sh_name == SHN_UNDEF) {
41 char * section_name = string_table + elf_sec_hdrs[i].sh_name;
43 if ((elf_sec_hdrs[i].sh_type != SHT_PROGBITS) || !(elf_sec_hdrs[i].sh_flags & SHF_ALLOC)) {
44 outputf("Skipping %s", section_name);
48 outputf("Loading %s at %08x", section_name, elf_sec_hdrs[i].sh_addr);
50 memcpy((void *)elf_sec_hdrs[i].sh_addr,
51 buf + elf_sec_hdrs[i].sh_offset,
52 elf_sec_hdrs[i].sh_size);