Hi everybody.
I’ve just started learning about LLVM and didn’t get too far studying the core.
I couldn’t find the solution to my problem (if it has one) in the mailing list or the source code. The problem is: how can I redefine a function that’s been called already by some other function?
Suppose I have 3 files, all compiled to bytecode through llvm-gcc (I think it could be clang instead).
File1.c:
void do_print() { print(); }
File2.c:
void print() { printf(“File2.c\n”); }
File3.c:
void print() { printf(“File3.c\n”); }
Then, I have the main file compiled to executable like this:
int main() {
// initialize and get context (not shown)
Module *file1(LoadFile(“file1.bc”,Context));
Module *file2(LoadFile(“file2.bc”,Context));
Module *file3(LoadFile(“file3.bc”,Context));
Linker::LinkModules(file1, file2, NULL);
EngineBuilder builder(file1);
ExecutionEngine *EE = builder.create();
EE->runStaticConstructorsDestructors(false);
func = EE->FindFunctionNamed(“do_print”);
EE->runFunction(func, std::vector());
//swap the definition of the function “print” from the one in File2.c to File3.c
swap (file1, file2, file3);
EE->runFunction(func, std::vector());
EE->runStaticConstructorsDestructors(true);
return 0;
}
I can get everything before the swap working (if I comment the rest, the output is OK). I’ve tried to build the “swap” function many times but I can’t get it to work.
The expected output is:
File2.c
File3.c
If someone know how to do this or knows it’s impossible, I would be very thankful. I don’t even know if this is the best way to do it.
Miranda