-
Notifications
You must be signed in to change notification settings - Fork 0
/
lo.cc
167 lines (146 loc) · 5.41 KB
/
lo.cc
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/LegacyPassManager.h"
#include "llvm/IR/Module.h"
#ifndef NDEBUG
# include "llvm/IR/Verifier.h"
#endif
#if LLVM_VERSION_MAJOR >= 14
# include "llvm/MC/TargetRegistry.h"
#else
# include "llvm/Support/TargetRegistry.h"
#endif
#include "llvm/Support/Host.h"
#include "llvm/Support/TargetSelect.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Passes/PassBuilder.h"
#include "llvm/Target/TargetMachine.h"
#include "llvm/Target/TargetOptions.h"
#include <cstring> /* memcpy() */
#include <unistd.h> /* sysconf(_SC_PAGESIZE) */
#include <sys/mman.h> /* mmap(), mprotect() */
#if defined __GNUC__ && !defined __clang__ && __GNUC__ == 4
namespace std { using llvm::make_unique; }
#endif
#if LLVM_VERSION_MAJOR < 10
namespace llvm { using Align = int; }
#endif
int main(int argc, char **argv)
{
llvm::InitializeNativeTarget();
llvm::InitializeNativeTargetAsmPrinter();
auto C = std::make_unique<llvm::LLVMContext>();
auto M = std::make_unique<llvm::Module>("hELFoVM", *C);
std::string Error;
const auto TargetTriple = llvm::sys::getDefaultTargetTriple();
const auto Target = llvm::TargetRegistry::lookupTarget(TargetTriple, Error);
if (!Target) {
llvm::errs() << Error;
return 1;
}
llvm::TargetOptions opt;
std::unique_ptr<llvm::TargetMachine>
TM(Target->createTargetMachine(TargetTriple, "generic", "", opt,
llvm::Optional<llvm::Reloc::Model>
(llvm::Reloc::PIC_)));
M->setDataLayout(TM->createDataLayout());
M->setTargetTriple(TargetTriple);
llvm::SmallVector<char, 0> ObjBufferSV;
{
const auto stringType = llvm::Type::getInt8PtrTy(*C);
const auto intType = llvm::Type::getInt32Ty(*C);
std::vector<llvm::Type *> PutsArgs{stringType};
llvm::FunctionType *PutsType =
llvm::FunctionType::get(intType, PutsArgs, false);
llvm::FunctionType *FT =
llvm::FunctionType::get(intType,
{stringType, PutsType->getPointerTo(),
stringType},
false);
llvm::Function *TheFunction =
llvm::Function::Create(FT, llvm::Function::ExternalLinkage,
"boo", M.get());
TheFunction->setDoesNotThrow();
{
llvm::IRBuilder<> builder(llvm::BasicBlock::Create(*C, "entry",
TheFunction));
auto Str = TheFunction->arg_begin();
auto F = Str;
llvm::FunctionCallee FC{PutsType, ++F};
auto c1 = builder.CreateCall(FC, Str);
auto c2 = builder.CreateCall(FC, ++F);
builder.CreateRet(builder.CreateAdd(c1, c2));
}
// M->dump();
assert(!llvm::verifyFunction(*TheFunction, &llvm::errs()));
{
// Create the analysis managers.
llvm::LoopAnalysisManager LAM;
llvm::FunctionAnalysisManager FAM;
llvm::CGSCCAnalysisManager CGAM;
llvm::ModuleAnalysisManager MAM;
// Create the new pass manager builder.
// Take a look at the PassBuilder constructor parameters for more
// customization, e.g. specifying a TargetMachine or various debugging
// options.
llvm::PassBuilder PB;
// Register all the basic analyses with the managers.
PB.registerModuleAnalyses(MAM);
PB.registerCGSCCAnalyses(CGAM);
PB.registerFunctionAnalyses(FAM);
PB.registerLoopAnalyses(LAM);
PB.crossRegisterProxies(LAM, FAM, CGAM, MAM);
using OptimizationLevel = llvm::
#if LLVM_VERSION_MAJOR < 14
PassBuilder::
#endif
OptimizationLevel;
PB.buildPerModuleDefaultPipeline(OptimizationLevel::O2).run(*M, MAM);
}
{
llvm::raw_svector_ostream ObjStream(ObjBufferSV);
llvm::legacy::PassManager PM;
llvm::MCContext *Ctx;
if (TM->addPassesToEmitMC(PM, Ctx, ObjStream))
return 2;
PM.run(*M);
}
}
const char *elf= ObjBufferSV.begin();
const size_t elfsize = ObjBufferSV.size();
if (FILE *f = fopen("lo.o", "wb")) {
fwrite(elf, elfsize, 1, f);
fclose(f);
}
assert(!memcmp(elf, "\177ELF", 4));
assert(elf[4] == 2); /*64-bit*/
assert(elf[6] == 1);
assert(*reinterpret_cast<const uint16_t*>(elf + 0x34) == 64);
/* number of sections */
assert(*reinterpret_cast<const uint16_t*>(elf + 0x3c) == 5 ||
*reinterpret_cast<const uint16_t*>(elf + 0x3c) == 6/* POWER */);
/* section header size */
assert(*reinterpret_cast<const uint16_t*>(elf + 0x3a) == 64);
const size_t *sections = reinterpret_cast<const size_t*>
(elf + *reinterpret_cast<const size_t*>(elf + 0x28));
assert(elf + elfsize > reinterpret_cast<const char*>(*sections + 8 * 7));
assert(reinterpret_cast<const uint32_t*>(§ions[8 * 2])[1] == 1);
char *text = const_cast<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(nullptr, size, PROT_READ|PROT_WRITE,
MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
if (buf == MAP_FAILED)
return 2;
memcpy(buf, text, textsize);
mprotect(buf, size, PROT_READ | PROT_EXEC);
typedef int (*callback)(const char*);
auto boo =
reinterpret_cast<int(*)(const char *, callback, const char *)>(buf);
int ret = boo("hello", puts, "world") + boo("goodbye", puts, "all");
munmap(buf, size);
return ret;
}