3 static const unsigned char elf_ident[4] = { 0x7F, 'E', 'L', 'F' };
5 int load_elf (char * buf, int size) {
7 Elf32_Ehdr * elf_hdr = (Elf32_Ehdr *) buf;
8 Elf32_Shdr * elf_sec_hdrs = (Elf32_Shdr *) (buf + elf_hdr->e_shoff);
10 /* Sanity check on ELF file */
11 if (memcmp(elf_hdr->e_ident, elf_ident, sizeof(elf_ident))) return -1;
12 if (elf_hdr->e_type != ET_EXEC) return -1;
13 if (elf_hdr->e_machine != EM_386) return -1;
14 if (elf_hdr->e_version != EV_CURRENT) return -1;
15 if (size < sizeof(Elf32_Ehdr)) return -1;
16 if (((char *)&elf_sec_hdrs[elf_hdr->e_shnum]) > (buf + size)) return -1;
18 char * string_table = buf + elf_sec_hdrs[elf_hdr->e_shstrndx].sh_offset;
20 if (string_table > (buf + size)) return -1;
23 for (i = 0; i < elf_hdr->e_shnum; i++) {
25 if (elf_sec_hdrs[i].sh_name == SHN_UNDEF) {
29 char * section_name = string_table + elf_sec_hdrs[i].sh_name;
31 if ((elf_sec_hdrs[i].sh_type != SHT_PROGBITS) || !(elf_sec_hdrs[i].sh_flags & SHF_ALLOC)) {
42 memcpy(elf_sec_hdrs[i].sh_addr,
43 buf + elf_sec_hdrs[i].sh_offset,
44 elf_sec_hdrs[i].sh_size);