[llvm-dev] How to get the value of an i8*?

Alberto Barbaro via llvm-dev llvm-dev at lists.llvm.org
Tue Jan 22 11:20:55 PST 2019


Hi all,
sorry to ask again but this problem is killing me :)

So I have the following C code:

#include<stdio.h>

int main(int argc, char *argv[]) {

    printf("%s\n", argv[0]);
    return argc;
}

That is translated into

; ModuleID = 'argv.bc'
source_filename = "argv.c"
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-pc-linux-gnu"

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

; Function Attrs: noinline nounwind optnone uwtable
define i32 @main(i32, i8**) #0 {
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i8**, align 8
  store i32 0, i32* %3, align 4
  store i32 %0, i32* %4, align 4
  store i8** %1, i8*** %5, align 8
  %6 = load i8**, i8*** %5, align 8
  %7 = getelementptr inbounds i8*, i8** %6, i64 0
  %8 = load i8*, i8** %7, align 8
  %9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4
x i8]* @.str, i32 0, i32 0), i8* %8)
  %10 = load i32, i32* %4, align 4
  ret i32 %10
}

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

attributes #0 = { noinline nounwind optnone uwtable
"correctly-rounded-divide-sqrt-fp-math"="false"
"disable-tail-calls"="false" "less-precise-fpmad"="false"
"no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf"
"no-infs-fp-math"="false" "no-jump-tables"="false"
"no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false"
"no-trapping-math"="false" "stack-protector-buffer-size"="8"
"target-cpu"="x86-64" "target-features"="+fxsr,+mmx,+sse,+sse2,+x87"
"unsafe-fp-math"="false" "use-soft-float"="false" }
attributes #1 = { "correctly-rounded-divide-sqrt-fp-math"="false"
"disable-tail-calls"="false" "less-precise-fpmad"="false"
"no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf"
"no-infs-fp-math"="false" "no-nans-fp-math"="false"
"no-signed-zeros-fp-math"="false" "no-trapping-math"="false"
"stack-protector-buffer-size"="8" "target-cpu"="x86-64"
"target-features"="+fxsr,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false"
"use-soft-float"="false" }

!llvm.module.flags = !{!0}
!llvm.ident = !{!1}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{!"clang version 6.0.1-svn334776-1~exp1~20181018152737.116
(branches/release_60)"}

I'm still experimenting with the Interpreter class...

I know that %8 holds the pointer to argv[0] and I would like to extract the
value. I though the right approach would have been to do what it is done
inside visitLoadInst:

ExecutionContext &SF = ECStack.back();
  GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
  GenericValue *Ptr = (GenericValue*)GVTOP(SRC);
  GenericValue Result;
  LoadValueFromMemory(Result, Ptr, I.getType());

Instead of I.getType() I used getInt32Ty(context) and instead of Ptr I used
the result returned by first LoadValueFromMemory. After that I print
"newResult.Int" but I always have 0 as a result. I cannot understand why.

Can someone help me to have argv[0] taken from the memory?

Thanks a lot
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20190122/fb11e973/attachment.html>


More information about the llvm-dev mailing list