racket-static/scripts/crimes.h

44 lines
1.1 KiB
C
Raw Permalink Normal View History

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <elf.h>
#if CRIMES_ELF_BITS == 32
#define Elf_Ehdr Elf32_Ehdr
#define Elf_Phdr Elf32_Phdr
#define CRIMES_PATCH_FUNC crimes_32
#else
#define Elf_Ehdr Elf64_Ehdr
#define Elf_Phdr Elf64_Phdr
#define CRIMES_PATCH_FUNC crimes_64
#endif
#define crimes_str(x) #x
void CRIMES_PATCH_FUNC(Elf_Ehdr* elf) {
uint8_t* phdrs = ((uint8_t*) elf) + elf->e_phoff;
ssize_t src = -1;
ssize_t dst = -1;
for (size_t i = 0; i < elf->e_phnum; i++) {
Elf_Phdr* phdr = (Elf_Phdr*) (phdrs + elf->e_phentsize * i);
if (phdr->p_type == PT_INTERP) {
src = i + 1;
dst = i;
}
}
if (src < 0) {
printf("[%s] couldn't find PT_INTERP - skipping\n", crimes_str(CRIMES_PATCH_FUNC));
return;
}
memmove(phdrs + dst * elf->e_phentsize, phdrs + src * elf->e_phentsize,
(elf->e_phnum - dst) * elf->e_phentsize);
elf->e_phnum -= 1;
printf("[%s] patch complete\n", crimes_str(CRIMES_PATCH_FUNC));
}
#undef Elf_Ehdr
#undef Elf_Phdr
#undef CRIMES_PATCH_FUNC