LLVM:更改函数中的指令或基本块
LLVM: Change instruction or basic blocks in a function
我想通过重新排序一些指令或插入一个新的基本块来操作函数。
我的方法可以在下面看到。
我从LLVM IR中读取一个模块,然后迭代其内容。
我将如何更改其内容?
是否需要克隆,如果需要,我将如何插入新指令?
#include <iostream>
#include <llvm/IR/Module.h>
#include <llvm/IRReader/IRReader.h>
#include <llvm/IR/LLVMContext.h>
#include <llvm/Support/SourceMgr.h>
#include <llvm/Bitcode/ReaderWriter.h>
#include <llvm/Support/FileSystem.h>
#include <llvm/Transforms/Utils/Cloning.h>
using namespace llvm;
int main()
{
LLVMContext context;
SMDiagnostic error;
std::unique_ptr<Module> m = parseIRFile("test.ll", error, context);
std::error_code EC;
llvm::raw_fd_ostream OS("module", EC, llvm::sys::fs::F_None);
WriteBitcodeToFile(m.get(), OS);
OS.flush();
std::cout << "Successfully read Module:" << std::endl;
std::cout << " Name: " << m->getName().str() << std::endl;
std::cout << " Target triple: " << m->getTargetTriple() << std::endl;
Module* mod = new Module("obfus", getGlobalContext());
for (auto iter1 = m->getFunctionList().begin();
iter1 != m->getFunctionList().end(); iter1++) {
Function &f = *iter1;
std::cout << " Function: " << f.getName().str() << std::endl;
Function* duplicateFunction = CloneFunction(f, nullptr, /*ModuleLevelChanges=*/false);
f.getParent()->getFunctionList().push_back(duplicateFunction);
for (auto iter2 = f.getBasicBlockList().begin();
iter2 != f.getBasicBlockList().end(); iter2++) {
BasicBlock &bb = *iter2;
std::cout << " BasicBlock: " << bb.getName().str() << std::endl;
for (auto iter3 = bb.begin(); iter3 != bb.end(); iter3++) {
Instruction &inst = *iter3;
std::cout << " Instruction " << &inst << " : " << inst.getOpcodeName();
unsigned int i = 0;
unsigned int opnt_cnt = inst.getNumOperands();
for(; i < opnt_cnt; ++i)
{
Value *opnd = inst.getOperand(i);
std::string o;
// raw_string_ostream os(o);
// opnd->print(os);
//opnd->printAsOperand(os, true, m);
if (opnd->hasName()) {
o = opnd->getName();
std::cout << " " << o << "," ;
} else {
std::cout << " ptr" << opnd << ",";
}
}
std:: cout << std::endl;
}
}
}
return 0;
}
通常,除非您要保留原始版本,否则不需要克隆或复制任何内容。
基本块和指令都有用于插入、移动和替换的 API。
要操作块,您可以使用以下方法(请注意,将来可能会更改):
void insertInto(Function *Parent, BasicBlock *InsertBefore = nullptr);
void moveAfter(BasicBlock *MovePos);
void moveBefore(BasicBlock *MovePos);
SymbolTableList<BasicBlock>::iterator eraseFromParent();
void removeFromParent();
说明也是如此:
void removeFromParent();
SymbolTableList<Instruction>::iterator eraseFromParent();
void insertBefore(Instruction *InsertPos);
void insertAfter(Instruction *InsertPos);
void moveBefore(Instruction *MovePos);
void moveBefore(BasicBlock &BB, SymbolTableList<Instruction>::iterator I);
我绝对可以推荐查看BasiBlock和指令接口,看看它们还有什么。
相关文章:
- 使用C++库在Android项目中修改gradle中的cmake参数,用于插入指令的测试
- C++中高效的大型稀疏块压缩线性方程
- 孤立代码块在结构中引发异常
- 无法编译 rtmidi 测试 cmidiin.cpp 文件, 非法指令
- C++:对不存在的命名空间使用命名空间指令
- C++嵌套if语句,基本货币交换
- llvm构建器向基本块添加终止符
- 在运行基本 Avx512 代码时获取非法指令
- 如何从LLVM中的Phinode及其相应的基本块中获取标签
- 编译后如何计算某些二进制功能(或基本块)的校验和
- LLVM IR:具有可变基本块目标的分支指令
- LLVM:更改函数中的指令或基本块
- 在visualc++中更改变量的基本滑块
- gcov错误:单个基本块有多个源
- 代码块 13.12 C++基本的循环调试错误
- C++中用于字节块的随机R/W的基本文件I/O
- 汇编器消息:错误:拇指条件指令应该在 IT 块中 - "strexeq r1,r2,[r4]"
- LLVM基本块的前身LLVM pred_iterator超出范围
- 对[基本范围]的解释当非限定名称查找涉及到使用-指令时,隐藏]p2
- 重复代码块的预处理器指令(带条件)