-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit_function_info.c
85 lines (76 loc) · 2.64 KB
/
init_function_info.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
74
75
76
77
78
79
80
81
82
83
84
85
#include "dns_ropob.h"
void make_global(char* globalname, FILE *outasm_fp) {
char buf[1000];
sprintf(buf, "\t.globl\t%s\n\t.data\n\t.align\t8\n\t.type\t%s, @object\n\t.size\t%s, 8\n%s:\n\t.quad\t0\n", globalname, globalname, globalname, globalname);
fwrite(buf, sizeof(char), strlen(buf), outasm_fp);
return;
}
void make_function_gadget_offsets(int funcnumber, int instruction_count,int func_count, char* funcname, FILE *outasm_fp, FILE *obj_fp) {
int gadget_table_size = instruction_count * 8;
if (funcnumber == 0) {
char buf[100] = "\t.globl\tfuncgadgetoffsets\n\t.align\t8\n\t.type\tfuncgadgetoffsets, @object\n";
fwrite(buf, sizeof(char), strlen(buf), outasm_fp);
sprintf(buf, "\t.size\tfuncgadgetoffsets, %d\nfuncgadgetoffsets:\n", 8*func_count);
fwrite(buf, sizeof(char), strlen(buf), outasm_fp);
for(int i = 0; i < func_count; i++) {
fwrite("\t.quad\t0\n", sizeof(char), 9, outasm_fp);
}
}
char buf[1000];
sprintf(buf, "\t.globl\tfunc%dgadgettable\n\t.align\t8\n\t.type\tfunc%dgadgettable, @object\n", funcnumber, funcnumber);
fwrite(buf, sizeof(char), strlen(buf), outasm_fp);
sprintf(buf, "\t.size\tfunc%dgadgettable, %d\nfunc%dgadgettable:\n", funcnumber, gadget_table_size, funcnumber);
fwrite(buf, sizeof(char), strlen(buf), outasm_fp);
make_function_gadget_table(funcnumber, instruction_count, funcname, outasm_fp, obj_fp);
return;
}
void init_function_info(FILE *asm_fp, FILE *outasm_fp, FILE *obj_fp) {
char buf[1001];
func_count = 0;
int funcnumber = 0;
int infunc = 0;
int instruction_count = 0;
rewind(asm_fp);
// functionの個数を確認
while(fgets(buf, 1000, asm_fp) != NULL) {
if (infunc == 1 && buf[1] == '.') {
infunc = 0;
}
if (strstr(buf, "@function") != NULL ) {
func_count++;
infunc = 1;
}
}
function_table = calloc(func_count, sizeof(int *));
funcname_table = calloc(func_count, sizeof(char *));
rewind(asm_fp);
char* funcname = NULL;
// functionの名前、instructionの数等を確認
// 各functionのgadget tableを作成
while(fgets(buf, 1000, asm_fp) != NULL){
if (infunc == 1 && buf[1] == '.') {
make_function_gadget_offsets(funcnumber ,instruction_count, func_count, funcname, outasm_fp, obj_fp);
funcnumber++;
infunc = 0;
}
if (infunc == 1 && buf[0] == '\t') {
instruction_count++;
}
if (strstr(buf, "@function") != NULL) {
infunc = 1;
instruction_count = 0;
char* address = strstr(buf, ".type");
address += 6;
funcname_table[funcnumber] = malloc(100);
funcname = (char *)funcname_table[funcnumber];
for(int i = 0; ; i++) {
if (address[i] == ',') {
funcname[i] = '\0';
break;
}
funcname[i] = address[i];
}
}
}
return;
}