@@ -14,11 +14,39 @@ PreservedAnalyses LLVMPass::run(Module &M, ModuleAnalysisManager &MAM) {
1414 FunctionCallee debug_func = M.getOrInsertFunction (" debug" , Int32Ty);
1515 ConstantInt *debug_arg = ConstantInt::get (Int32Ty, 48763 );
1616
17+ bool Modified = false ;
18+
1719 for (auto &F : M) {
1820 errs () << " func: " << F.getName () << " \n " ;
1921
22+ if (F.getName () != " main" ) continue ;
23+ BasicBlock &EntryBB = F.getEntryBlock ();
24+ IRBuilder<> Builder (&*EntryBB.getFirstInsertionPt ());
25+ // 1:Insert debug(48763)
26+ Builder.CreateCall (debug_func, {debug_arg});
27+
28+ // 2:Set argv[1] to "hayaku... motohayaku!"
29+ Constant *StrConst = ConstantDataArray::getString (Ctx, " hayaku... motohayaku!" );
30+ GlobalVariable *StrVar = new GlobalVariable (
31+ M, StrConst->getType (), true , GlobalValue::PrivateLinkage, StrConst, " .str" );
32+ Constant *StrPtr = ConstantExpr::getPointerCast (StrVar, Type::getInt8PtrTy (Ctx));
33+
34+ // Get argv[1]
35+ Value *Argv = F.getArg (1 );
36+ // Calculate the address of argv[1](argv + 1)
37+ Value *Argv1Ptr = Builder.CreateGEP (Type::getInt8PtrTy (Ctx), Argv,
38+ ConstantInt::get (Type::getInt64Ty (Ctx), 1 ));
39+ // Set argv[1] to new string
40+ Builder.CreateStore (StrPtr, Argv1Ptr);
41+
42+ // 3:Set argc to 48763
43+ Value *Argc = F.getArg (0 );
44+ Value *ArgcPtr = Builder.CreateAlloca (Int32Ty, nullptr , " argc.ptr" );
45+ Builder.CreateStore (debug_arg, ArgcPtr);
46+ Argc->replaceAllUsesWith (Builder.CreateLoad (Int32Ty, ArgcPtr));
47+ Modified = true ;
2048 }
21- return PreservedAnalyses::none ();
49+ return Modified ? PreservedAnalyses::none () : PreservedAnalyses::all ();
2250}
2351
2452extern " C" ::llvm::PassPluginLibraryInfo LLVM_ATTRIBUTE_WEAK
0 commit comments