-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlo.c
More file actions
153 lines (129 loc) · 4.73 KB
/
lo.c
File metadata and controls
153 lines (129 loc) · 4.73 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include "llvm-c/Core.h"
#include "llvm-c/Target.h"
#include "llvm-c/Transforms/Scalar.h"
#include "llvm-c/Transforms/Utils.h"
#include "llvm-c/ExecutionEngine.h"
#include <assert.h>
#ifndef NDEBUG
# include "llvm-c/Analysis.h"
# include <string.h> /* memcmp() */
#endif
#include <string.h> /* memcpy() */
#include <stdio.h> /* puts(), fopen() */
#include <stdlib.h> /* posix_memalign() */
#include <unistd.h> /* sysconf(_SC_PAGESIZE) */
#include <sys/mman.h> /* mmap(), mprotect() */
#define FALSE 0
#define TRUE 1
int main(int argc, char **argv)
{
LLVMInitializeNativeTarget();
LLVMInitializeNativeAsmPrinter();
LLVMContextRef C = LLVMContextCreate();
LLVMModuleRef M = LLVMModuleCreateWithNameInContext("hELFoVM-C", C);
char *TargetTriple = LLVMGetDefaultTargetTriple();
char *errors = 0;
LLVMTargetRef Target;
LLVMGetTargetFromTriple(TargetTriple, &Target, &errors);
if (!Target) {
fail:
puts(errors);
LLVMDisposeMessage(errors);
LLVMDisposeMessage(TargetTriple);
LLVMDisposeModule(M);
LLVMContextDispose(C);
return 1;
}
LLVMTargetMachineRef TM =
LLVMCreateTargetMachine(Target, TargetTriple, "generic", "",
LLVMCodeGenLevelDefault, LLVMRelocPIC,
LLVMCodeModelDefault);
LLVMSetTarget(M, TargetTriple);
LLVMDisposeMessage(TargetTriple);
{
LLVMTypeRef stringType = LLVMPointerType(LLVMInt8TypeInContext(C), 0);
LLVMTypeRef intType = LLVMInt32TypeInContext(C);
LLVMTypeRef PutsType = LLVMFunctionType(intType, &stringType, 1, FALSE);
LLVMTypeRef PutsArgs[3];
PutsArgs[0] = stringType;
PutsArgs[1] = LLVMPointerType(PutsType, 0);
PutsArgs[2] = stringType;
LLVMTypeRef FT = LLVMFunctionType(intType, PutsArgs, 3, FALSE);
LLVMValueRef TheFunction = LLVMAddFunction(M, "boo", FT);
LLVMSetLinkage(TheFunction, LLVMExternalLinkage);
{
LLVMBuilderRef builder = LLVMCreateBuilderInContext(C);
LLVMBasicBlockRef BB =
LLVMAppendBasicBlockInContext(C, TheFunction, "entry");
LLVMPositionBuilderAtEnd(builder, BB);
LLVMValueRef args[3];
assert(LLVMCountParams(TheFunction) == 3);
LLVMGetParams(TheFunction, args);
LLVMValueRef c1 = LLVMBuildCall2(builder, PutsType, args[1],
args, 1, "");
LLVMValueRef c2 = LLVMBuildCall2(builder, PutsType, args[1],
&args[2], 1, "");
LLVMBuildRet(builder, LLVMBuildAdd(builder, c1, c2, ""));
LLVMDisposeBuilder(builder);
}
/* LLVMDumpModule(M); */
assert(!LLVMVerifyFunction(TheFunction, LLVMPrintMessageAction));
{
LLVMPassManagerRef PM = LLVMCreateFunctionPassManagerForModule(M);
LLVMAddPromoteMemoryToRegisterPass(PM);
LLVMAddInstructionCombiningPass(PM);
LLVMAddReassociatePass(PM);
LLVMAddGVNPass(PM);
LLVMAddCFGSimplificationPass(PM);
LLVMInitializeFunctionPassManager(PM);
LLVMRunFunctionPassManager(PM, TheFunction);
LLVMDisposePassManager(PM);
}
}
LLVMMemoryBufferRef ObjBuffer;
if (LLVMTargetMachineEmitToMemoryBuffer(TM, M, LLVMObjectFile, &errors,
&ObjBuffer))
goto fail;
LLVMDisposeTargetMachine(TM);
LLVMDisposeModule(M);
LLVMContextDispose(C);
const char *elf= LLVMGetBufferStart(ObjBuffer);
const size_t elfsize = LLVMGetBufferSize(ObjBuffer);
FILE *f = fopen("lo-c.o", "wb");
if (f) {
fwrite(elf, elfsize, 1, f);
fclose(f);
}
assert(!memcmp(elf, "\177ELF", 4));
assert(elf[4] == 2); /*64-bit*/
assert(elf[6] == 1);
assert(*(const uint16_t*) &elf[0x34] == 64);
/* number of sections */
assert(*(const uint16_t*) &elf[0x3c] == 7 ||
*(const uint16_t*) &elf[0x3c] == 8/* POWER */);
/* section header size */
assert(*(const uint16_t*) &elf[0x3a] == 64);
const size_t *sections = (const size_t*)(elf + *(const size_t*) &elf[0x28]);
assert(elf + elfsize > (char*)(*sections + 8 * 7));
assert(((const uint32_t*) §ions[8 * 2])[1] == 1);
char *text = (char*) &elf[sections[8 * 2 + 3]];
size_t textsize = sections[8 * 2 + 4];
printf("size: %zu\n", textsize);
long sz = sysconf(_SC_PAGESIZE);
size_t size = (textsize + (sz - 1)) & ~(sz - 1);
void *buf =
mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
if (buf == MAP_FAILED) {
LLVMDisposeMemoryBuffer(ObjBuffer);
return 2;
}
memcpy(buf, text, textsize);
LLVMDisposeMemoryBuffer(ObjBuffer);
mprotect(buf, size, PROT_READ | PROT_EXEC);
typedef int (*callback)(const char*);
int (*boo) (const char *, callback, const char *) =
((int (*)(const char *, callback, const char *)) buf);
int ret = boo("hello", puts, "world") + boo("goodbye", puts, "all");
munmap(buf, size);
return ret;
}