Can someone enlighten me as to what the rules are for when a GlobalVariable needs to have external linkage and when it can get away with having internal linkage?
For some context (NOTE: I do not intend this to be an XY-problem, just that I have a way to resolve the problem I’m having; also, this is not homework):
I’m working on trying to target MiniJava (from CSEP-501) to the LLVM IR and I have implemented dynamic polymorphism as follows: I have a set of global variables, each containing a list of function pointers (so basically, these global variables serve as vtables). Whenever a MiniJava class is instantiated, an appropriate vtable is picked and its pointer is assigned to the first field in the instantiated class. I also have a function that is supposed to be called at the start of the program to populate the vtables with the pointers to the class methods.
The problem I’m having is that if I set the vtable variables to have internal linkage, verifyModule() fails (saying that the variables are external but do not have external or weak linkage) and I cannot dump object code (for reference, I use the code from Kaleidoscope for this). If I set the linkage to be external, I can dump object code but I cannot link it to my main stub (due to undefined references). And if I set the linkage to ‘weak external’, everything works - except that the resulting executable segfaults when run. It is worth pointing out that when I run the executable under LLDB, the segfault happens when the vtables are being populated (as opposed to, say, when a method is called using them).
I’m clearly not aware of the nuances of the different linkages but I suspect that they might not be the only problem here. Nevertheless, I would appreciate any light shed on this matter.