scc

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

elf64probe.c (857B)


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