I have a legacy system which declares symbols in a shared memory region. The addresses of the are statically defined to absolute addresses in memory using gcc's linker command script function. I can build a static test executable in C which references these symbols correctly when I pass the static object file and linker command script during compilation, and attach to the shared memory region (shmat()
, etc).
What I am trying to do is build a Python extension library which can access the same symbols. Being able to access the library from Python requires the -shared
and -fPIC
options from what I understand. When I compile this way, the symbols are no longer located at the correct addresses. Compiling the C test program with -shared -fPIC
causes the addresses to be wrong as well.
I can define the symbols within the extension library directly:
int *pA = 0x030A0000;
and this works when compiled using -shared -fPIC
. But should I? There are hundreds of symbols I need and it would be unmaintainable if the addresses ever changed. It seems like there should be a way to indicate to the linker that these symbols should not be relocated, but nothing I have tried has worked, including compiling as below:
In the code for Python extension:
extern int symbol_i_need;
...
printf("%d", symbol_i_need); // Seg fault
Compile commands:
gcc -fPIC -o my_module.o my_module.c /path/static_object.o /path/linker_command_script.ld
gcc -shared -fPIC -o my_module.so my_module.o -Wl,-Bstatic,/path/static_defined_object.o,/path/linker_command_script.ld,-Bdynamic -rdynamic