1#include <stdio.h>23#include <scc/mach.h>4#include <scc/elf/elftypes.h>5#include <scc/elf/elfhdr.h>6#include <scc/elf.h>78#include "../libmach.h"9#include "fun.h"1011int12elfprobe(unsigned char *buf, char **name)13{14 int arch, data, endian, bits;15 struct elfhdr32 hdr;16 struct arch *ap;1718 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;2223 unpack(endian,24 buf,25 "'16sss",26 hdr.e_ident,27 &hdr.e_type,28 &hdr.e_machine,29 &hdr.e_version);3031 if (!IS_ELF(hdr)32 || buf[EI_VERSION] != EV_CURRENT33 || hdr.e_version != EV_CURRENT34 || buf[EI_CLASS] != ELFCLASS32 && buf[EI_CLASS] != ELFCLASS6435 || (data != ELFDATA2LSB && data != ELFDATA2MSB)) {36 return -1;37 }3839 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 }4647 if (name)48 *name = "elf-unknown";4950 return OBJ(ELF, arch, endian, bits);51}