[LLVMdev] constructing global array of pointers?

ret val retval386 at gmail.com
Tue Nov 15 10:35:23 PST 2011


I'm trying to create a GlobalVariable(that is a ConstantArray) in its
own section. I want each element to be a pointer to a global thats
already in the program(a variable or function). The follow code makes
sense to me and compiles fine, but gives me the wrong output. When I
look at the section in the ELF file it is larger than I expected and
appears to be wrong. The contents of the array do not seem to be valid
offsets.

Am I constructing it wrong?
Thank you

void writeArray(Module &M, GlobalVariable *shadow, Function *val,
Function *func) {
                /* Build up contents */
                vector<Constant *> v_elements;
                Type *elm_type = Type::getInt32PtrTy(M.getContext());

                Constant *tmp = dyn_cast<Constant>(shadow);
                assert(tmp != NULL && "shadow");
                v_elements.push_back(ConstantExpr::getBitCast(tmp, elm_type));

                tmp = dyn_cast<Constant>(val);
                assert(tmp != NULL && "value");
                v_elements.push_back(ConstantExpr::getBitCast(tmp, elm_type));

                tmp = dyn_cast<Constant>(func);
                assert(tmp != NULL && "function");
                v_elements.push_back(ConstantExpr::getBitCast(tmp, elm_type));

                /* Create array */
                ArrayRef<Constant *> a_elements(v_elements);
                ArrayType *type = ArrayType::get(elm_type, 3);
                Constant *array = ConstantArray::get(type, a_elements);

                /* Make new GlobalVariable from array */
                GlobalVariable *global = new GlobalVariable(M, type, true,
                                                            my_linkage, array,
                                                            "my array");
                global->setSection(*my_section);
        }



More information about the llvm-dev mailing list