Clang 如何使用LLVM添加和初始化全局数组?

Clang 如何使用LLVM添加和初始化全局数组?,clang,llvm,llvm-ir,Clang,Llvm,Llvm Ir,我想向模块添加一个全局数组。为此,我使用以下代码: // Insert global array if(!Init) { Module* Mod = F.getParent(); Mod->getOrInsertGlobal("my_array", ArrayType::get(Type::getInt32Ty(F.getContext()), 12)); GlobalVariable* GArray = Mod->ge

我想向模块添加一个全局数组。为此,我使用以下代码:

    // Insert global array
    if(!Init) {
        Module* Mod = F.getParent();
        Mod->getOrInsertGlobal("my_array", ArrayType::get(Type::getInt32Ty(F.getContext()), 12));
        GlobalVariable* GArray = Mod->getNamedGlobal("my_array");
        GArray->setLinkage(GlobalValue::LinkageTypes::CommonLinkage);
        GArray->setAlignment(4);

        ConstantInt* const_int_val = ConstantInt::get(Mod->getContext(), APInt(32, 0));
        GArray->setInitializer(ConstantArray::get(ArrayType::get(Type::getInt32Ty(F.getContext()), 12), 0));
    }
但是,运行
函数pass
,我得到错误:

#0 0x0000000001f4eb8e (opt+0x1f4eb8e)
#1 0x0000000001f4ef2d (opt+0x1f4ef2d)
#2 0x0000000001f4cfd5 (opt+0x1f4cfd5)
#3 0x0000000001f4e3d5 (opt+0x1f4e3d5)
#4 0x00007f4520750390 __restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x11390)
#5 0x0000000000f8b792 (opt+0xf8b792)
#6 0x000000000187ab4b (opt+0x187ab4b)
#7 0x000000000187aa55 (opt+0x187aa55)
#8 0x00007f451f497b10 (anonymous namespace)::FlattenO::runOnFunction(llvm::Function&) /home/nlykkei/llvm-project/llvm-ir-obfuscation/flatten/FlattenOPass.cpp:48:0
#9 0x0000000001968478 (opt+0x1968478)
#10 0x0000000001968611 (opt+0x1968611)
#11 0x000000000196898c (opt+0x196898c)
#12 0x00000000019690a1 (opt+0x19690a1)
#13 0x00000000019692ad (opt+0x19692ad)
#14 0x0000000000f54a08 (opt+0xf54a08)
#15 0x00007f451f6c8830 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x20830)
#16 0x0000000000f38b79 (opt+0xf38b79)
Stack dump:
0.  Program arguments: opt -load build/flatten/libFlattenOPass.so -flattenO programs/ll/prime_test.ll -o test.ll -S 
1.  Running pass 'Function Pass Manager' on module 'programs/ll/prime_test.ll'.
2.  Running pass 'Flattens the CFG by means of switching' on function '@main'
Segmentation fault (core dumped)

如何将全局数组正确插入模块并将其初始化为选定的值集?

Segfault(很可能)表示您正在尝试取消对空指针的引用。检查代码中的每个指针。我将使用什么例程来设置其值?如果要初始化值null,则可以使用GArray->setInitializer(常量::getNullValue(ArrayType::get(Type::getInt32Ty(F.getContext()),12)))