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

时间:2017-02-15 15:32:50

标签: 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->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));
    }

但是,运行FunctionPass,我收到错误:

#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)

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

0 个答案:

没有答案