5 static const unsigned char elf_ident[4] = { 0x7F, 'E', 'L', 'F' };
7 int load_elf (char * buf, int size) {
9 Elf32_Ehdr * elf_hdr = (Elf32_Ehdr *) buf;
10 Elf32_Shdr * elf_sec_hdrs = (Elf32_Shdr *) (buf + elf_hdr->e_shoff);
12 /* Sanity check on ELF file */
13 if (memcmp((void *)elf_hdr->e_ident, (void *)elf_ident, sizeof(elf_ident))) return -1;
14 if (elf_hdr->e_type != ET_EXEC) return -1;
15 if (elf_hdr->e_machine != EM_386) return -1;
16 if (elf_hdr->e_version != EV_CURRENT) return -1;
17 if (size < sizeof(Elf32_Ehdr)) return -1;
18 if (((char *)&elf_sec_hdrs[elf_hdr->e_shnum]) > (buf + size)) return -1;
20 char * string_table = buf + elf_sec_hdrs[elf_hdr->e_shstrndx].sh_offset;
22 if (string_table > (buf + size)) return -1;
25 for (i = 0; i < elf_hdr->e_shnum; i++) {
27 if (elf_sec_hdrs[i].sh_name == SHN_UNDEF) {
31 char * section_name = string_table + elf_sec_hdrs[i].sh_name;
33 if ((elf_sec_hdrs[i].sh_type != SHT_PROGBITS) || !(elf_sec_hdrs[i].sh_flags & SHF_ALLOC)) {
34 outputf("Skipping %s", section_name);
38 outputf("Loading %s at %08x", section_name, elf_sec_hdrs[i].sh_addr);
40 memcpy((void *)elf_sec_hdrs[i].sh_addr,
41 buf + elf_sec_hdrs[i].sh_offset,
42 elf_sec_hdrs[i].sh_size);