Skip to content

Commit 4fb7ba9

Browse files
authored
Merge pull request #452 from sa-llo/lab6
[LAB6] 313704803
2 parents dae9bde + 5ce876a commit 4fb7ba9

1 file changed

Lines changed: 27 additions & 1 deletion

File tree

lab6/llvm-pass.so.cc

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,35 @@ PreservedAnalyses LLVMPass::run(Module &M, ModuleAnalysisManager &MAM) {
1313
IntegerType *Int32Ty = IntegerType::getInt32Ty(Ctx);
1414
FunctionCallee debug_func = M.getOrInsertFunction("debug", Int32Ty);
1515
ConstantInt *debug_arg = ConstantInt::get(Int32Ty, 48763);
16+
PointerType *CharPtrTy = Type::getInt8PtrTy(Ctx);
17+
18+
1619

1720
for (auto &F : M) {
18-
errs() << "func: " << F.getName() << "\n";
21+
22+
23+
24+
// Find main function, call the debug function with argument 48763
25+
26+
if (F.getName() == "main"){
27+
IRBuilder<> Builder(&*F.getEntryBlock().getFirstInsertionPt());
28+
Builder.CreateCall(debug_func, {debug_arg});
29+
30+
// Overwrote argv1 with string "hayaku... motohayaku!"
31+
Argument *argvArg = F.getArg(1);
32+
Value *index1 = ConstantInt::get(Int32Ty, 1);
33+
Value *argv1_ptr = Builder.CreateInBoundsGEP(CharPtrTy, argvArg, index1);
34+
Value *hayakuStr = Builder.CreateGlobalStringPtr("hayaku... motohayaku!");
35+
Builder.CreateStore(hayakuStr, argv1_ptr);
36+
37+
// Overwrote argcArg with 48763
38+
Argument *argcArg = F.getArg(0);
39+
argcArg -> replaceAllUsesWith(debug_arg);
40+
41+
}
42+
43+
44+
1945

2046
}
2147
return PreservedAnalyses::none();

0 commit comments

Comments
 (0)