Cast from uint8_t* to StringRef

244 Views Asked by At

I'm using ExecutionEngine.cpp. I want to get StringRef from found addresses by the following code:

const APInt &IntVal;

uint8_t *y2 = reinterpret_cast<uint8_t *>(const_cast<uint64_t *>(IntVal.getRawData()));

int v2 = *reinterpret_cast<int *>(y2);

StringRef* Src1V=cast<StringRef>(y2);

I got this error:

home/rasha/llvm/llvm/include/llvm/Support/Casting.h:236:3: required from ‘typename llvm::cast_retty<X, Y*>::ret_type llvm::cast(Y*) [with X = llvm::StringRef; Y = unsigned char; typename llvm::cast_retty<X, Y*>::ret_type = llvm::StringRef*]’

/home/rasha/llvm/llvm/lib/ExecutionEngine/Interpreter/Execution.cpp:1291:38: required from here

/home/rasha/llvm/llvm/include/llvm/Support/Casting.h:56:28: error: ‘classof’ is not a member of ‘llvm::StringRef’

return To::classof(&Val);

Would you please explain to me the error? Note that I used the casting with the same types in Execution.cpp, with no errors.

1

There are 1 best solutions below

0
On

Strings are all represented inside the LLVM IR as ConstantDataSequential, subclass of Constant. if you want stringref, you must explicitly tell LLVM to do that for you by calling corresponding APIs of ConstantDataSequential.
To be specific, you need to call one or more of the following LLVM API after casting the pointer you get:

  • isString (unsigned CharSize=8)
  • isCString ()
  • getAsString ()
  • getAsCString ()