-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathllvm-pass.so.cc
More file actions
79 lines (62 loc) · 2.43 KB
/
llvm-pass.so.cc
File metadata and controls
79 lines (62 loc) · 2.43 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
#include "llvm/Passes/PassPlugin.h"
#include "llvm/Passes/PassBuilder.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/Instructions.h"
using namespace llvm;
namespace {
struct LLVMPass : public PassInfoMixin<LLVMPass> {
PreservedAnalyses run(Module &M, ModuleAnalysisManager &) {
//to get the LLVM context
LLVMContext &Context = M.getContext();
Function *MainFunction = M.getFunction("main");
if (!MainFunction || MainFunction->arg_size() < 2)
return PreservedAnalyses::all();
Function::arg_iterator ArgsIterator = MainFunction->arg_begin();
Argument *ArgcArgument = &*ArgsIterator++;
Argument *ArgvArgument = &*ArgsIterator;
BasicBlock &EntryBlock = MainFunction->getEntryBlock();
Instruction *FirstInstruction = &*EntryBlock.getFirstInsertionPt();
IRBuilder<> Builder(FirstInstruction);
//prep debug(48763)
Type *Int32Type = Type::getInt32Ty(Context);
ConstantInt *DebugValue = ConstantInt::get(Int32Type, 48763);
FunctionType *DebugFunctionType =
FunctionType::get(Type::getVoidTy(Context), {Int32Type}, false);
FunctionCallee DebugFunction = M.getOrInsertFunction("debug", DebugFunctionType);
//insert call debug(48763)
Builder.CreateCall(DebugFunction, {DebugValue});
// replace all uses of argc with 48763
if (!ArgcArgument->use_empty()) {
ArgcArgument->replaceAllUsesWith(DebugValue);
}
Value *hayakuString =
Builder.CreateGlobalStringPtr("hayaku... motohayaku!");
Value *Index1 = ConstantInt::get(Int32Type, 1);
Value *Argv1Pointer = Builder.CreateGEP(
ArgvArgument->getType()->getPointerElementType(),
ArgvArgument,
Index1
);
//stores the string in argv[1]
Builder.CreateStore(hayakuString, Argv1Pointer);
return PreservedAnalyses::none();
}
};
}
extern "C" LLVM_ATTRIBUTE_WEAK ::llvm::PassPluginLibraryInfo
llvmGetPassPluginInfo() {
return {LLVM_PLUGIN_API_VERSION, "LLVMPass", "1.0",
[](PassBuilder &PB) {
PB.registerPipelineParsingCallback(
[](StringRef Name, ModulePassManager &MPM,
ArrayRef<PassBuilder::PipelineElement>) {
if (Name == "llvm-pass") {
MPM.addPass(LLVMPass());
return true;
}
return false;
});
}
};
}