I am new to LLVM APIs, I want to create and initialize a GlobalVariable with an arrary. I am able to create the GlobalVariable, but I cannot initialize it.
My code looks like this:
path = "../bitmap";
initcallfd.open(path.c_str());
std::vector<Constant *> fuzz_bitmap;
if (initcallfd.is_open()) {
while (initcallfd >> cfi_mark) {
Constant *ccfi_mark = ConstantInt::get(Int8Ty, cfi_mark, false);
fuzz_bitmap.push_back(ccfi_mark);
}
}
initcallfd.close();
ArrayRef<Constant *> blockArray1(fuzz_bitmap);
ArrayType *pArrTy1 = ArrayType::get(Int8Ty, fuzz_bitmap.size());
Constant *blockItems1 = ConstantArray::get(pArrTy1, blockArray1);
M.getOrInsertGlobal("__afl_area_ptr", blockItems1->getType());
GlobalVariable* AFLMapPtr = M.getNamedGlobal("__afl_area_ptr");
AFLMapPtr->setLinkage(GlobalValue::CommonLinkage);
AFLMapPtr->setInitializer(blockItems1);
When this pass is used, fatal error is reported:
'common' global must have a zero initializer!
[65536 x i8]* @__afl_area_ptr
fatal error: error in backend: Broken module found, compilation aborted!
clang-12: error: clang frontend command failed with exit code 70 (use -v to see invocation)
When I changed the CommonLinkage as ExternalLinkage like this:
AFLMapPtr->setLinkage(GlobalValue::ExternalLinkage);
it still does not work, and report the following error:
.../AFL/afl-llvm-rt.o:../afl-llvm-rt.o.c:61: multiple definition of `__afl_area_ptr'
/tmp/vulnerable-3fe810.o:(.data+0x0): first defined here
clang-12: error: linker command failed with exit code 1 (use -v to see invocation)
Can somebody help me out here?