racket-static/scripts/crimes.c

54 lines
1.1 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <unistd.h>
#include <fcntl.h>
#include <elf.h>
#define CRIMES_ELF_BITS 32
#include "crimes.h"
#undef CRIMES_ELF_BITS
#define CRIMES_ELF_BITS 64
#include "crimes.h"
#undef CRIMES_ELF_BITS
int main(int argc, char* argv[]) {
if (argc < 2) {
printf("usage: %s <filename>\n", argv[0]);
return -1;
}
char* fname = argv[1];
int fd = open(fname, O_RDWR);
if (fd < 0) {
perror("open");
return -1;
}
ssize_t sz = lseek(fd, 0, SEEK_END);
if (sz < 0) {
perror("lseek");
return -1;
}
if (lseek(fd, 0, SEEK_SET) < 0) {
perror("lseek");
return -1;
}
void* elf = mmap(NULL, sz, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (elf == MAP_FAILED) {
perror("mmap");
return -1;
}
close(fd);
// we're not going to emulate wacky technically nonstandard behavior here
if (((uint8_t*) elf)[EI_CLASS] < ELFCLASS64) {
crimes_32(elf);
} else {
crimes_64(elf);
}
munmap(elf, sz);
}