Commit d80cf57d authored by Julian Kranz's avatar Julian Kranz

---

parent a85e7880
val registers-live-map = let
val add map r = do
reg-sem <- return (semantic-register-of r);
return (fmap-add-range map reg-sem.id reg-sem.size reg-sem.offset)
end
in do
map <- return (fmap-empty {});
map <- add map R0;
map <- add map R1;
map <- add map R2;
map <- add map R3;
map <- add map R4;
map <- add map R5;
map <- add map R6;
map <- add map R7;
map <- add map R8;
map <- add map R9;
map <- add map R10;
map <- add map R11;
map <- add map R12;
map <- add map R13;
map <- add map R14;
map <- add map R15;
map <- add map R16;
map <- add map R17;
map <- add map R18;
map <- add map R19;
map <- add map R20;
map <- add map R21;
map <- add map R22;
map <- add map R23;
map <- add map R24;
map <- add map R25;
map <- add map R26;
map <- add map R27;
map <- add map R28;
map <- add map R29;
map <- add map R30;
map <- add map R31;
map <- add map IO0;
map <- add map IO1;
map <- add map IO2;
map <- add map IO3;
map <- add map IO4;
map <- add map IO5;
map <- add map IO6;
map <- add map IO7;
map <- add map IO8;
map <- add map IO9;
map <- add map IO10;
map <- add map IO11;
map <- add map IO12;
map <- add map IO13;
map <- add map IO14;
map <- add map IO15;
map <- add map IO16;
map <- add map IO17;
map <- add map IO18;
map <- add map IO19;
map <- add map IO20;
map <- add map IO21;
map <- add map IO22;
map <- add map IO23;
map <- add map IO24;
map <- add map IO25;
map <- add map IO26;
map <- add map IO27;
map <- add map IO28;
map <- add map IO29;
map <- add map IO30;
map <- add map IO31;
map <- add map IO32;
map <- add map IO33;
map <- add map IO34;
map <- add map IO35;
map <- add map IO36;
map <- add map IO37;
map <- add map IO38;
map <- add map IO39;
map <- add map IO40;
map <- add map IO41;
map <- add map IO42;
map <- add map IO43;
map <- add map IO44;
map <- add map IO45;
map <- add map IO46;
map <- add map IO47;
map <- add map IO48;
map <- add map IO49;
map <- add map IO50;
map <- add map IO51;
map <- add map IO52;
map <- add map IO53;
map <- add map IO54;
map <- add map IO55;
map <- add map RAMPD;
map <- add map RAMPX;
map <- add map RAMPY;
map <- add map RAMPZ;
map <- add map EIND;
map <- add map SPL;
map <- add map SPH;
map <- add map SREG;
map <- add map PC;
map <- add map SP;
return map
end end
/* vim:cindent:ts=2:sw=2:expandtab */
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <gdsl.h>
#include <sys/resource.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <time.h>
#include <getopt.h>
#include <err.h>
#include <fcntl.h>
#include <gelf.h>
#include <string.h>
#include <sysexits.h>
#define NANOS 1000000000LL
char elf_section_boundary_get(char *path, size_t *offset, size_t *size) {
char retval = 0;
int fd = open(path, O_RDONLY);
if(!fd) {
retval = 8;
goto end_0;
}
if(elf_version(EV_CURRENT) == EV_NONE) {
retval = 2;
goto end_0;
}
Elf *e = elf_begin(fd, ELF_C_READ, NULL);
if(!e) {
retval = 3;
goto end_0;
}
if(elf_kind(e) != ELF_K_ELF) {
retval = 4;
goto end_1;
}
size_t shstrndx;
if(elf_getshdrstrndx(e, &shstrndx) != 0) {
retval = 5;
goto end_1;
}
Elf_Scn *scn = NULL;
char found = 0;
while((scn = elf_nextscn(e, scn)) != NULL) {
GElf_Shdr shdr;
if(gelf_getshdr(scn, &shdr) != &shdr) {
retval = 6;
goto end_1;
}
char *name = elf_strptr(e, shstrndx, shdr.sh_name);
if(!name) {
retval = 7;
goto end_1;
}
if(!strcmp(name, ".text")) {
*offset = shdr.sh_offset;
*size = shdr.sh_size;
found = 1;
break;
}
// printf("%s - %zu:%zu\n", name, shdr.sh_offset, shdr.sh_size);
}
if(!found)
retval = 1;
end_1: elf_end(e);
end_0: close(fd);
return retval;
}
struct options {
long preservation;
char elf;
char *file;
size_t offset;
size_t length;
};
enum p_option {
OPTION_ELF, OPTION_FILE, OPTION_OFFSET, OPTION_LENGTH, OPTION_PRESERVATION
};
static char args_parse(int argc, char **argv, struct options *options) {
options->elf = 0;
options->file = NULL;
options->preservation = CON_SEM_PRESERVATION_EVERYWHERE;
options->offset = 0;
options->length = 0;
struct option long_options[] = { { "elf", no_argument, NULL, OPTION_ELF }, { "file", required_argument, NULL,
OPTION_FILE }, { "offset", required_argument, NULL, OPTION_FILE }, { "length", required_argument, NULL,
OPTION_LENGTH }, { "preserve", required_argument, NULL, OPTION_PRESERVATION } };
while(1) {
int result = getopt_long(argc, argv, "", long_options, NULL);
if(result < 0)
break;
switch(result) {
case OPTION_ELF: {
options->elf = 1;
break;
}
case OPTION_FILE: {
options->file = optarg;
break;
}
case OPTION_OFFSET: {
sscanf(optarg, "%lu", &options->offset);
break;
}
case OPTION_LENGTH: {
sscanf(optarg, "%lu", &options->length);
break;
}
case OPTION_PRESERVATION: {
if(!strcmp("everywhere", optarg)) {
options->preservation = CON_SEM_PRESERVATION_EVERYWHERE;
break;
}
if(!strcmp("block", optarg)) {
options->preservation = CON_SEM_PRESERVATION_BLOCK;
break;
}
if(!strcmp("context", optarg)) {
options->preservation = CON_SEM_PRESERVATION_CONTEXT;
break;
}
return 2;
}
case '?':
return 1;
}
}
return 0;
}
int main(int argc, char** argv) {
const rlim_t kStackSize = 64L * 1024L * 1024L; // min stack size = 64 Mb
struct rlimit rl;
int result;
result = getrlimit(RLIMIT_STACK, &rl);
if(result == 0) {
if(rl.rlim_cur < kStackSize) {
rl.rlim_cur = kStackSize;
result = setrlimit(RLIMIT_STACK, &rl);
if(result != 0) {
fprintf(stderr, "setrlimit returned result = %d\n", result);
}
}
}
struct options options;
if(args_parse(argc, argv, &options)) {
printf(
"Usage: semantics-opt [--elf] [--offset offset] [--length length] --file file [--preserve everywhere|block|context]\n");
return 1;
}
if(options.elf) {
if(elf_section_boundary_get(options.file, &options.offset, &options.length))
exit(2);
} else if(!options.length) {
struct stat buf;
stat(options.file, &buf);
options.length = buf.st_size;
}
FILE *f = fopen(options.file, "r");
if(!f) {
printf("Unable to open file.\n");
return 1;
}
fseek(f, options.offset, SEEK_SET);
size_t buffer_size = options.length + 15;
char *buffer = (char*)malloc(buffer_size);
size_t buffer_length = fread(buffer, 1, buffer_size, f);
state_t state = gdsl_init();
gdsl_set_code(state, buffer, buffer_length, 0);
if(setjmp(*gdsl_err_tgt(state))) {
fprintf(stderr, "failure: %s\n", gdsl_get_error_message(state));
exit(1);
}
//uint64_t consumed = 0;
size_t last_offset = 0;
while(last_offset < options.length) {
obj_t rreil = gdsl_decode_translate_block_optimized(state, gdsl_config_default(state), gdsl_int_max(state),
options.preservation);
string_t fmt = gdsl_merge_rope(state, gdsl_rreil_pretty(state, rreil));
puts(fmt);
gdsl_reset_heap(state);
last_offset = gdsl_get_ip_offset(state);
}
gdsl_destroy(state);
return 0;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment