-
Notifications
You must be signed in to change notification settings - Fork 9
/
assemble.c
73 lines (57 loc) · 1.36 KB
/
assemble.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <stdint.h>
#include "utils.h"
char *shell_escape(const char *str) {
size_t len = strlen(str);
char *escaped = malloc(4 * len + 1);
size_t j = 0;
for(size_t i = 0; i <= len; i++) {
if(str[i] == '\'') {
strcpy(escaped + j, "'\\''");
j += 4;
} else {
escaped[j] = str[i];
j++;
}
}
escaped[j] = '\0';
return escaped;
}
// TODO: Actually use a library to assemble instead of calling out to rasm2
bool assemble_string(char *str, uint8_t bits, uint64_t address, unsigned char **output, size_t *output_size, bool att_syntax) {
*output_size = 0;
*output = malloc(16);
char *escaped = shell_escape(str);
char *cmd;
if(att_syntax) {
asprintf(&cmd, "rasm2 -s att -a x86.as -b %u '%s'",
bits, escaped);
} else {
asprintf(&cmd, "rasm2 -a x86.nasm -b %u -o 0x%llx '%s'",
bits, address, escaped);
}
free(escaped);
FILE *f = popen(cmd, "r");
free(cmd);
if(!f) {
return false;
}
size_t bytes_read = 0;
char buf[256];
while(fgets(buf, sizeof(buf), f)) {
size_t len = strlen(buf);
*output_size += (len - 1) / 2;
*output = realloc(*output, *output_size);
for(int i = 0; i < len; i += 2) {
(*output)[bytes_read++] = hex2int(buf[i]) * 0x10 + hex2int(buf[i + 1]);
}
}
pclose(f);
if(bytes_read == 0) {
return false;
}
return true;
}