elfprobe.c (1083B)
1 #include <stdio.h> 2 3 #include <scc/mach.h> 4 #include <scc/elf/elftypes.h> 5 #include <scc/elf/elfhdr.h> 6 #include <scc/elf.h> 7 8 #include "../libmach.h" 9 #include "fun.h" 10 11 int 12 elfprobe(unsigned char *buf, char **name) 13 { 14 int arch, data, endian, bits; 15 struct elfhdr32 hdr; 16 struct arch *ap; 17 18 data = buf[EI_DATA]; 19 endian = (data == ELFDATA2LSB) ? LITTLE_ENDIAN : BIG_ENDIAN; 20 arch = (buf[EI_CLASS] == ELFCLASS32) ? ARCHUNK32 : ARCHUNK64; 21 bits = (buf[EI_CLASS] == ELFCLASS32) ? OBJ32 : OBJ64; 22 23 unpack(endian, 24 buf, 25 "'16sss", 26 hdr.e_ident, 27 &hdr.e_type, 28 &hdr.e_machine, 29 &hdr.e_version); 30 31 if (!IS_ELF(hdr) 32 || buf[EI_VERSION] != EV_CURRENT 33 || hdr.e_version != EV_CURRENT 34 || buf[EI_CLASS] != ELFCLASS32 && buf[EI_CLASS] != ELFCLASS64 35 || (data != ELFDATA2LSB && data != ELFDATA2MSB)) { 36 return -1; 37 } 38 39 for (ap = elfarchs; ap->name; ap++) { 40 if (ap->mach == hdr.e_machine && ap->endian == data) { 41 if (name) 42 *name = ap->name; 43 return ap->type; 44 } 45 } 46 47 if (name) 48 *name = "elf-unknown"; 49 50 return OBJ(ELF, arch, endian, bits); 51 }