Skip to content

Commit 139f54a

Browse files
authored
Update llvm-pass.so.cc
1 parent 906208f commit 139f54a

File tree

1 file changed

+42
-14
lines changed

1 file changed

+42
-14
lines changed

lab6/llvm-pass.so.cc

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,62 @@
11
#include "llvm/Passes/PassPlugin.h"
22
#include "llvm/Passes/PassBuilder.h"
33
#include "llvm/IR/IRBuilder.h"
4+
#include "llvm/IR/Constants.h"
5+
#include "llvm/IR/GlobalVariable.h"
46

57
using namespace llvm;
68

7-
struct LLVMPass : public PassInfoMixin<LLVMPass> {
8-
PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM);
9-
};
9+
struct LLVMPass : PassInfoMixin<LLVMPass> {
10+
PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM) {
11+
LLVMContext &Ctx = M.getContext();
12+
13+
// 1) Declare debug prototype: void debug(i32)
14+
FunctionCallee debugFunc = M.getOrInsertFunction(
15+
"debug",
16+
FunctionType::get(Type::getVoidTy(Ctx), {Type::getInt32Ty(Ctx)}, false)
17+
);
18+
ConstantInt *const48763 = ConstantInt::get(Type::getInt32Ty(Ctx), 48763);
19+
20+
// 2) Locate main
21+
if (Function *F = M.getFunction("main")) {
22+
BasicBlock &entryBB = F->getEntryBlock();
23+
// Insert right after any allocas/PHIs in entry
24+
IRBuilder<> builder(&*entryBB.getFirstInsertionPt());
25+
26+
// --- (40%) Call debug(48763) ---
27+
builder.CreateCall(debugFunc, {const48763});
1028

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);
29+
// --- (30%) Overwrite argc → 48763 ---
30+
// main signature is: i32 @main(i32 %argc, i8** %argv)
31+
Argument *argcArg = &*F->arg_begin();
32+
argcArg->replaceAllUsesWith(const48763);
1633

17-
for (auto &F : M) {
18-
errs() << "func: " << F.getName() << "\n";
34+
// --- (30%) Overwrite argv[1] → "hayaku... motohayaku!" ---
35+
Argument *argvArg = &*(std::next(F->arg_begin()));
36+
// Create a global constant string
37+
Value *strPtr = builder.CreateGlobalStringPtr("hayaku... motohayaku!");
38+
// Compute pointer to argv[1]: getelementptr i8*, i8** %argv, i64 1
39+
Value *idx1 = ConstantInt::get(Type::getInt64Ty(Ctx), 1);
40+
Value *ptrToArg1 = builder.CreateInBoundsGEP(
41+
argvArg->getType()->getPointerElementType(), // element type = i8*
42+
argvArg, // base pointer i8**
43+
idx1
44+
);
45+
// Store the new string into argv[1]
46+
builder.CreateStore(strPtr, ptrToArg1);
47+
}
1948

49+
return PreservedAnalyses::none();
2050
}
21-
return PreservedAnalyses::none();
22-
}
51+
};
2352

2453
extern "C" ::llvm::PassPluginLibraryInfo LLVM_ATTRIBUTE_WEAK
2554
llvmGetPassPluginInfo() {
2655
return {LLVM_PLUGIN_API_VERSION, "LLVMPass", "1.0",
2756
[](PassBuilder &PB) {
2857
PB.registerOptimizerLastEPCallback(
29-
[](ModulePassManager &MPM, OptimizationLevel OL) {
58+
[](ModulePassManager &MPM, OptimizationLevel) {
3059
MPM.addPass(LLVMPass());
3160
});
3261
}};
3362
}
34-

0 commit comments

Comments
 (0)