[LLVMdev] differences in IR and ELF?
ret val
retval386 at gmail.com
Wed Nov 23 18:19:32 PST 2011
I'm trying to create a GlobalVariable that is a ConstantArray. Id like
each element to be a pointer to other things in the program(global
variables, functions). So that they all have the same type Id like to
make the elements void pointers.
I think I am going about this wrong, heres how I am doing it:
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);
}
The IR looks like this:
@"my array" = constant [3 x i32*] [i32* bitcast (i32 (i32)** @"Shadow
Variable for ptr1" to i32*), i32* bitcast (i32 (i32)* @f2 to i32*),
i32* bitcast (i32 (i32)* @f1 to i32*)], section "my_section"
@"my array2" = constant [3 x i32*] [i32* bitcast (i32 (i32)** @"Shadow
Variable for ptr1" to i32*), i32* bitcast (i32 (i32)* @f1 to i32*),
i32* bitcast (i32 ()* @main to i32*)], section "my_section"
The ELF section is too large. It does not look like padding, it looks
like more are being created and I don't know why.
More information about the llvm-dev
mailing list