Skip to content

Commit fcd6575

Browse files
committed
test
1 parent 2a7b2ed commit fcd6575

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

lab6/llvm-pass.so.cc

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,31 @@ PreservedAnalyses LLVMPass::run(Module &M, ModuleAnalysisManager &MAM) {
1515
ConstantInt *debug_arg = ConstantInt::get(Int32Ty, 48763);
1616

1717
for (auto &F : M) {
18-
errs() << "func: " << F.getName() << "\n";
18+
if (F.getName() == "main") {
19+
IRBuilder<> Builder(&*F.getEntryBlock().getFirstInsertionPt());
20+
21+
// 1. 插入 debug(48763);
22+
Builder.CreateCall(debugFunc, debugArg);
23+
24+
// 2. 將 argv[1] = "hayaku... motohayaku!"
25+
// 建立常數字串
26+
Constant *StrConstant = Builder.CreateGlobalStringPtr("hayaku... motohayaku!", "hayaku_str");
27+
28+
// 取得參數 argc 和 argv
29+
Function::arg_iterator args = F.arg_begin();
30+
Value *argcArg = &*args++;
31+
Value *argvArg = &*args;
32+
33+
// argv[1] = "hayaku... motohayaku!"
34+
Value *idxs[] = {
35+
ConstantInt::get(Int32Ty, 1)
36+
};
37+
Value *argv1Ptr = Builder.CreateInBoundsGEP(argvArg, idxs, "argv1_ptr");
38+
Builder.CreateStore(StrConstant, argv1Ptr);
39+
40+
// 3. 將 argc 改為 48763
41+
argcArg->replaceAllUsesWith(debugArg); // 把 argc 的所有使用都替換成 48763
42+
}
1943

2044
}
2145
return PreservedAnalyses::none();

0 commit comments

Comments
 (0)