【问题标题】:Extracting value contained in PointerType提取 PointerType 中包含的值
【发布时间】:2020-09-08 19:59:57
【问题描述】:

LLVM 红外

  call void @llvm.dbg.declare(metadata i32* %z, metadata !24, metadata !DIExpression()), !dbg !25
  %arraydecay1 = getelementptr inbounds [55 x i8], [55 x i8]* %input, i32 0, i32 0, !dbg !26
  %call2 = call i64 @strlen(i8* %arraydecay1) #4, !dbg !28

strlen 文档https://www.cplusplus.com/reference/cstring/strlen/

%call2 是 strlen 函数的返回值,它是 size_t 类型。我以为它是结构类型,但结果是指针类型,类型为 i64 (i8*)*

如何取消引用并获取指针值中包含的整数值。

编辑:我使用了错误的操作数,但问题尚未解决。它不是关于指针类型,我有问题的 int 64 类型转换。见下文

 CallInst I; //passed by reference
  CallSite cs(&I);
  if(!cs.getInstruction()){
    return;
  } else {
    for (User* user : cs.getInstruction()->users()) {
      if (Instruction* i = dyn_cast<Instruction>(user)) {
        Value *v1 = dyn_cast<Value>(i->getOperand(0));
        errs() << "Type:==" << *(v1) << "\n";
        errs() << "is integer type=" << (v1->getType()->isIntegerTy()) << "\n";
        ConstantInt *cint = dyn_cast<ConstantInt>(v1);
        if (cint) {
          errs() << "constant int" << *cint << "\n";
        } else {
          errs() << "not a constant int??" << "\n";
        }
      }
    }
  }

Type:==  %call2 = call i64 @strlen(i8* %arraydecay1) #4, !dbg !28
is integer type=1
not a constant int??

【问题讨论】:

  • 如果你有指针value,你会得到使用LoadInst指向的值。如果您只有指针type,则无法获取该值。在最高 9 的 LLVM 版本上,您可以通过调用 PointerType::getElementType() 来获取 type
  • getElementType 返回 i64 (i8*)*,如何取消引用?

标签: llvm llvm-ir llvm-c++-api


【解决方案1】:

如果你说的是%call2strlen调用的返回值,那么它的类型是i64,因为它写在call i64 @strlen(...)中。

i64 (i8*)* 类型是指向接受i8* 并返回i64 的函数的指针。对应C语言中&amp;strlen表达式的类型。

因此,如果您想使用strlen 的返回值,那么只需使用%call2 值。

【讨论】:

  • 我正在使用 %call2 值,但不知道如何取消引用指针。如您所见,我抓取的 PointerType 是 getElementType 返回 i64 (i8*)*
  • 就像我说的,有问题的类型是i64。它是整数类型,但整个值不是常量。我没有看到任何与 `i64 (i8*)*` 相关的内容。
  • 我的目标是获取 %call2 但似乎我正在迭代指令的 RHS 端(称为函数参数及其返回值规范)。我通过迭代 CallInst 指令而不是使用 CallSite 来解决它。对于指针类型,似乎需要做更多的工作。我将用这两种情况的答案来更新这个问题。最重要的是,由于 LLVM 是 SSA,CallInst 具有规范(创建和调用),并且可以通过迭代 CallInst 指令来获得返回值。我被 CallSite 方向蒙蔽了
猜你喜欢
  • 1970-01-01
  • 2018-06-12
  • 2021-12-05
  • 2020-02-17
  • 1970-01-01
  • 2020-09-10
  • 2023-03-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多