summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorAlasdair Armstrong2018-03-12 18:57:57 +0000
committerAlasdair Armstrong2018-03-12 19:01:19 +0000
commitcf8efbe63da569dadbd99eb35c1e4777fa06bc30 (patch)
tree936406a74fb05b55f0f329cc0ee9027806c330b7 /test
parenta6d59b97a4840b81481751e0e05b1da9ed28de86 (diff)
ELF loading for C backend
Add a flag to Sail that allows for an image of an elf file to be dumped in a simple format using linksem, used as sail -elf test.elf -o test.bin This image file can then be used by a compiled C version of a sail spec as with ocaml simply by ./a.out test.bin
Diffstat (limited to 'test')
-rw-r--r--test/c/sail.h89
1 files changed, 36 insertions, 53 deletions
diff --git a/test/c/sail.h b/test/c/sail.h
index 89aad9ba..b29e9535 100644
--- a/test/c/sail.h
+++ b/test/c/sail.h
@@ -23,22 +23,22 @@ typedef char *sail_string;
// occurs. Pattern match failures are always fatal.
void sail_match_failure(sail_string msg) {
fprintf(stderr, "Pattern match failure in %s\n", msg);
- exit(1);
+ exit(EXIT_FAILURE);
}
unit sail_assert(bool b, sail_string msg) {
if (b) return UNIT;
fprintf(stderr, "Assertion failed: %s\n", msg);
- exit(1);
+ exit(EXIT_FAILURE);
}
unit sail_exit(const unit u) {
fprintf(stderr, "exit\n");
- exit(0);
+ exit(EXIT_SUCCESS);
}
void elf_entry(mpz_t *rop, const unit u) {
- mpz_set_ui(*rop, 0x400130ul);
+ mpz_set_ui(*rop, 0x400168ul);
}
// Sail bits are mapped to uint64_t where bitzero = 0ul and bitone = 1ul
@@ -748,8 +748,8 @@ void write_mem(uint64_t address, uint64_t byte)
uint64_t offset = address & MASK;
struct block *prev = NULL;
- struct block *current = sail_memory;
-
+ struct block *current = sail_memory;
+
while (current != NULL) {
if (current->block_id == mask) {
current->mem[offset] = (uint8_t) byte;
@@ -760,7 +760,7 @@ void write_mem(uint64_t address, uint64_t byte)
}
current->next = sail_memory->next;
sail_memory = current;
-
+
return;
} else {
prev = current;
@@ -798,13 +798,13 @@ uint64_t read_mem(uint64_t address)
}
void kill_mem()
-{
+{
while (sail_memory != NULL) {
struct block *next = sail_memory->next;
free(sail_memory->mem);
free(sail_memory);
-
+
sail_memory = next;
}
}
@@ -863,59 +863,42 @@ void read_ram(bv_t *data,
mpz_clear(byte);
}
+void load_image(char *file) {
+ FILE *fp = fopen(file, "r");
+
+ if (!fp) {
+ fprintf(stderr, "Image file %s could not be loaded\n", file);
+ exit(EXIT_FAILURE);
+ }
+
+ char *addr = NULL;
+ char *data = NULL;
+ size_t len = 0;
+
+ while (true) {
+ ssize_t addr_len = getline(&addr, &len, fp);
+ if (addr_len == -1) break;
+ ssize_t data_len = getline(&data, &len, fp);
+ if (data_len == -1) break;
+
+ write_mem((uint64_t) atoll(addr), (uint64_t) atoll(data));
+ }
+
+ free(addr);
+ free(data);
+ fclose(fp);
+}
+
void load_instr(uint64_t addr, uint32_t instr) {
- write_mem(addr, instr & 0xFF);
+ write_mem(addr , instr & 0xFF);
write_mem(addr + 1, instr >> 8 & 0xFF);
write_mem(addr + 2, instr >> 16 & 0xFF);
write_mem(addr + 3, instr >> 24 & 0xFF);
}
-void elf_hack(void) {
- // print_char
- load_instr(0x400110ul, 0xd10043ffu);
- load_instr(0x400114ul, 0x39003fe0u);
- load_instr(0x400118ul, 0x39403fe0u);
- load_instr(0x40011cul, 0x580003e1u);
- load_instr(0x400120ul, 0x39000020u);
- load_instr(0x400124ul, 0xd503201fu);
- load_instr(0x400128ul, 0x910043ffu);
- load_instr(0x40012cul, 0xd65f03c0u);
- // _start
- load_instr(0x400130ul, 0xa9be7bfdu);
- load_instr(0x400134ul, 0x910003fdu);
- load_instr(0x400138ul, 0x94000007u);
- load_instr(0x40013cul, 0xb9001fa0u);
- load_instr(0x400140ul, 0x52800080u);
- load_instr(0x400144ul, 0x97fffff3u);
- load_instr(0x400148ul, 0xd503201fu);
- load_instr(0x40014cul, 0xa8c27bfdu);
- load_instr(0x400150ul, 0xd65f03c0u);
- // main
- load_instr(0x400154ul, 0xd10043ffu);
- load_instr(0x400158ul, 0xb9000fffu);
- load_instr(0x40015cul, 0xb9000bffu);
- load_instr(0x400160ul, 0x14000007u);
- load_instr(0x400164ul, 0xb9400fe0u);
- load_instr(0x400168ul, 0x11000400u);
- load_instr(0x40016cul, 0xb9000fe0u);
- load_instr(0x400170ul, 0xb9400be0u);
- load_instr(0x400174ul, 0x11000400u);
- load_instr(0x400178ul, 0xb9000be0u);
- load_instr(0x40017cul, 0xb9400be0u);
- load_instr(0x400180ul, 0x710fa01fu);
- load_instr(0x400184ul, 0x54ffff0du);
- load_instr(0x400188ul, 0xb9400fe0u);
- load_instr(0x40018cul, 0x910043ffu);
- load_instr(0x400190ul, 0xd65f03c0u);
- load_instr(0x400194ul, 0x00000000u);
- load_instr(0x400198ul, 0x13000000u);
- load_instr(0x40019cul, 0x00000000u);
-}
-
// ***** Setup and cleanup functions for library code *****
void setup_library(void) {
- elf_hack();
mpf_set_default_prec(FLOAT_PRECISION);
mpz_init(eq_bits_test);
}