gpt4 book ai didi

c++ - 在LLVM中获取全局字符串值

转载 作者:行者123 更新时间:2023-12-02 10:39:26 30 4
gpt4 key购买 nike

我遇到这样的情况,我在源代码中有一个带有恒定字符串参数的函数调用,并且我想在C++的LLVM Pass中访问该字符串(作为CString或char *或诸如此类)。
来源示例:

void some_function(){
give_txt("abcd 1234");

}

功能
give_text

是一个伪函数,在.hpp文件中定义为:
const char* give_txt(char*)
{
return ".";
}

因此: 如何在通行证内访问此“abcd 1234”?
我已经走了这么远:
bool runOnModule(Module &M) override    
{
for(auto &F : M)
{
std::string name = F.getName();
if(name.find("some_function") != std::string::npos)
{
auto &B = *(F.begin());
for(auto &I : B)
{
if (auto* op = dyn_cast<CallInst>(&I))
{
std::string mangled_name = op->getCalledFunction()->getName();
std::string name = demangle(mangled_name);

if(name.find("give_text") != std::string::npos)
{
Value* a = op->getArgOperand(0);
auto a2 = op->arg_begin()->get();
auto a3 = a->getType();
auto a4 = dyn_cast<ConstantDataArray>(a);
auto a5 = a4->getAsCString();

}

但这显然不起作用,并且编译会在最后一条指令(分配a5)时崩溃。

最佳答案

您的代码看起来不错,但a4a5行应为

auto a4 = dyn_cast<GlobalVariable>(a);
if (a4)
auto a5 = dyn_cast<ConstantDataArray>(a4->getInitializer());
if (a5)
auto a6 = a5->getAsCString();

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

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com