Change global variable contents without influencing the other variable contents

41 Views Asked by At

I'm try to use the LLVM api (inside my own pass) to change the contents of the g_Meh1 variable.

const char g_Meh1[] = "++__BLA__";
const char g_Meh2[] = "++__BLA__";

int main(void)
{
    printf("%s", gMeh1);
    printf("%s", gMeh2);
}

I've created a LLVM pass and I want to change the body of one variable (ie. g_Meh1) but apparently those two global variable share the same initializer (var_data and var_org_data point to the same memory). When I am changing the data of the g_Meh1 the data of g_Meh2 changes as well - i don't want to do that.

Even though the function below will be only done one time the contents of both variables will be changed

ConstantDataSequential *var_data = dyn_cast<ConstantDataSequential>(glob_var1->getInitializer());
char *var_org_data              = (char*)var_data->getRawDataValues().data();
var_org_data[0] = 'A'; // change first byte

I've tried to provide new initializer for the each variable, but LLVM brings assert condition that the types does not match.

StringRef StrVal = StringRef("BLA12", 6);                 
char *Data = (char*)StrVal.begin();
int Size = StrVal.size();

                      
Constant *test = ConstantDataArray::getString(M.getContext(), StringRef(Data, Size), false);    

var_dupl->setInitializer(test); // assert triggered, type mismatch

Any ideas how to solve it?

0

There are 0 best solutions below