scc

simple c99 compiler
git clone git://git.simple-cc.org/scc
Log | Files | Refs | Submodules | README | LICENSE

elf64probe.c (836B)


      1 #include <stdio.h>
      2 
      3 #include <scc/mach.h>
      4 
      5 #include "../libmach.h"
      6 #include "elf64.h"
      7 
      8 int
      9 elf64probe(unsigned char *buf, char **name)
     10 {
     11 	int endian;
     12 	Elf_Ehdr hdr;
     13 	struct arch *ap;
     14 
     15 	unpack(buf[EI_DATA] == ELFDATA2LSB ? LITTLE_ENDIAN : BIG_ENDIAN,
     16 	       buf,
     17 	       "'16sss",
     18 	       hdr.e_ident,
     19 	       &hdr.e_type,
     20 	       &hdr.e_machine,
     21 	       &hdr.e_version);
     22 
     23 	if (!IS_ELF(hdr)
     24 	||  buf[EI_CLASS] != ELFCLASS64
     25 	||  buf[EI_DATA] == ELFDATANONE
     26 	||  buf[EI_VERSION] != EV_CURRENT
     27 	||  (buf[EI_DATA] != ELFDATA2LSB && buf[EI_DATA] != ELFDATA2MSB)) {
     28 		return -1;
     29 	}
     30 
     31 	if (hdr.e_version != EV_CURRENT)
     32 		return -1;
     33 
     34 	endian = hdr.e_ident[EI_DATA];
     35 	for (ap = elf64archs; ap->name; ap++) {
     36 		if (ap->mach == hdr.e_machine &&  ap->endian == endian) {
     37 			if (name)
     38 				*name = ap->name;
     39 			return ap->type;
     40 		}
     41 	}
     42 
     43 	return -1;
     44 }