c++ - 如何在 LLVM IR 中获取字符串文字的值?

标签 c++ llvm llvm-ir

我是 LLVM 的新手。我正在尝试编写一个基本的 Pass,当它被赋予中间表示时,它将检查 printf 调用的参数。
如果格式字符串不是字符串文字,那么我当然无法检查它。但通常是这样。

我要检查的示例 IR 是:

@.str = private unnamed_addr constant [7 x i8] c"Hi %u\0A\00", align 1

define i32 @main() nounwind {
entry:
  %retval = alloca i32, align 4
  store i32 0, i32* %retval
  %call = call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([7 x i8]* @.str, i32 0, i32 0), i32 1)
  ret i32 0
}

declare i32 @printf(i8*, ...)

我找到了名为 ExternalFunctionsPassedConstants 的预先存在的 Pass,这似乎是相关的:

struct ExternalFunctionsPassedConstants : public ModulePass {
  static char ID; // Pass ID, replacement for typeid
  ExternalFunctionsPassedConstants() : ModulePass(ID) {}
  virtual bool runOnModule(Module &M) {
    for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
      if (!I->isDeclaration()) continue;

      bool PrintedFn = false;
      for (Value::use_iterator UI = I->use_begin(), E = I->use_end();
           UI != E; ++UI) {
        Instruction *User = dyn_cast<Instruction>(*UI);
        if (!User) continue;

        CallSite CS(cast<Value>(User));
        if (!CS) continue;

        ...

所以我添加了代码:

        if (I->getName() == "printf") {
          errs() << "printf() arg0 type: "
                 << CS.getArgument(0)->getType()->getTypeID() << "\n";
        }

到目前为止一切顺利 -- 我看到类型 ID 是 14,这意味着它是一个 PointerTyID

但是现在,我如何获取作为参数传递的字符串文字的内容,以便我可以根据实际给定的数字验证预期参数的数量?

最佳答案

CS.getArgument(0)  

代表GetElementPtrConstantExpr

i8* getelementptr inbounds ([7 x i8]* @.str, i32 0, i32 0)

,它是一个用户对象。您想要的字符串(即 @.str)是此 GetElementPtrConstantExpr 的第一个操作数。

所以,可以通过

获取字符串字面量
CS.getArgument(0).getOperand(0)

不过,我还没有测试过这段代码。如果有任何错误,请告诉我。

关于c++ - 如何在 LLVM IR 中获取字符串文字的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12127137/

相关文章:

c++ - fstream EOF 意外抛出异常

iphone - LLVM 2.0 无法为 iPhone 模拟器构建。 GCC 4.2 工作正常

llvm - 使用 CostModel 获取 LLVM IR 的 CPU 周期

LLVM:我必须设置目标布局还是设置目标三元组更好?

C++,find_if 不工作

c++ - 了解 const 自动指针

c++ - 在Qt中的TextEdit中写在同一行上

macos - clang 插件编译错误: undefined symbol llvm::Registry<clang::PluginASTAction,llvm::RegistryTraits<clang::PluginASTAction>>

compilation - 我可以将多种语言的代码与 LLVM 一起编译吗?

pointers - 如何比较 LLVM-IR 中的指针?