|
1 | 1 | #include "llvm/Passes/PassPlugin.h"
|
2 | 2 | #include "llvm/Passes/PassBuilder.h"
|
3 |
| -#include "llvm/IR/PassManager.h" |
4 | 3 | #include "llvm/IR/IRBuilder.h"
|
5 |
| -#include "llvm/IR/Instructions.h" |
6 |
| -#include "llvm/IR/Constants.h" |
7 |
| -#include "llvm/IR/Module.h" |
8 | 4 |
|
9 | 5 | using namespace llvm;
|
10 | 6 |
|
11 | 7 | struct LLVMPass : public PassInfoMixin<LLVMPass> {
|
12 |
| - PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM) { |
13 |
| - LLVMContext &Ctx = M.getContext(); |
14 |
| - IntegerType *Int32Ty = Type::getInt32Ty(Ctx); |
15 |
| - PointerType *Int8PtrTy = Type::getInt8PtrTy(Ctx); |
16 |
| - |
17 |
| - // Declare: void debug(int) |
18 |
| - FunctionCallee debugFunc = M.getOrInsertFunction("debug", FunctionType::get(Type::getVoidTy(Ctx), {Int32Ty}, false)); |
19 |
| - ConstantInt *debugID = ConstantInt::get(Int32Ty, 48763); |
20 |
| - |
21 |
| - for (auto &F : M) { |
22 |
| - if (F.getName() == "main") { |
23 |
| - IRBuilder<> Builder(&*F.getEntryBlock().getFirstInsertionPt()); |
24 |
| - |
25 |
| - // Inject debug(48763); |
26 |
| - Builder.CreateCall(debugFunc, {debugID}); |
27 |
| - |
28 |
| - // Get main args |
29 |
| - auto args = F.args().begin(); |
30 |
| - Argument *argcArg = args++; |
31 |
| - Argument *argvArg = args; |
32 |
| - |
33 |
| - // === Overwrite argc to 48763 === |
34 |
| - // Allocate space and overwrite |
35 |
| - AllocaInst *argcAlloca = Builder.CreateAlloca(Int32Ty, nullptr, "argc.fake"); |
36 |
| - Builder.CreateStore(ConstantInt::get(Int32Ty, 48763), argcAlloca); |
| 8 | + PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM); |
| 9 | +}; |
37 | 10 |
|
38 |
| - // Replace all uses of argcArg with loaded value |
39 |
| - for (auto &BB : F) { |
40 |
| - for (auto &I : BB) { |
41 |
| - for (unsigned i = 0; i < I.getNumOperands(); ++i) { |
42 |
| - if (I.getOperand(i) == argcArg) { |
43 |
| - IRBuilder<> tmpBuilder(&I); |
44 |
| - LoadInst *argcLoad = tmpBuilder.CreateLoad(Int32Ty, argcAlloca); |
45 |
| - I.setOperand(i, argcLoad); |
46 |
| - } |
47 |
| - } |
48 |
| - } |
49 |
| - } |
| 11 | +PreservedAnalyses LLVMPass::run(Module &M, ModuleAnalysisManager &MAM) { |
| 12 | + LLVMContext &Ctx = M.getContext(); |
| 13 | + IntegerType *Int32Ty = IntegerType::getInt32Ty(Ctx); |
| 14 | + FunctionCallee debug_func = M.getOrInsertFunction("debug", Int32Ty); |
| 15 | + ConstantInt *debug_arg = ConstantInt::get(Int32Ty, 48763); |
50 | 16 |
|
51 |
| - // === Overwrite argv[1] === |
52 |
| - Value *index1 = ConstantInt::get(Int32Ty, 1); |
53 |
| - Value *argv1Ptr = Builder.CreateGEP(argvArg->getType()->getPointerElementType(), argvArg, index1); |
54 |
| - Value *newStr = Builder.CreateGlobalStringPtr("hayaku... motohayaku!"); |
55 |
| - Builder.CreateStore(newStr, argv1Ptr); |
56 |
| - } |
57 |
| - } |
| 17 | + for (auto &F : M) { |
| 18 | + errs() << "func: " << F.getName() << "\n"; |
58 | 19 |
|
59 |
| - return PreservedAnalyses::none(); |
60 | 20 | }
|
61 |
| -}; |
| 21 | + return PreservedAnalyses::none(); |
| 22 | +} |
62 | 23 |
|
63 |
| -extern "C" LLVM_ATTRIBUTE_WEAK ::llvm::PassPluginLibraryInfo llvmGetPassPluginInfo() { |
| 24 | +extern "C" ::llvm::PassPluginLibraryInfo LLVM_ATTRIBUTE_WEAK |
| 25 | +llvmGetPassPluginInfo() { |
64 | 26 | return {LLVM_PLUGIN_API_VERSION, "LLVMPass", "1.0",
|
65 |
| - [](PassBuilder &PB) { |
66 |
| - PB.registerOptimizerLastEPCallback( |
67 |
| - [](ModulePassManager &MPM, OptimizationLevel OL) { |
68 |
| - MPM.addPass(LLVMPass()); |
69 |
| - }); |
70 |
| - }}; |
| 27 | + [](PassBuilder &PB) { |
| 28 | + PB.registerOptimizerLastEPCallback( |
| 29 | + [](ModulePassManager &MPM, OptimizationLevel OL) { |
| 30 | + MPM.addPass(LLVMPass()); |
| 31 | + }); |
| 32 | + }}; |
71 | 33 | }
|
| 34 | + |
0 commit comments