If a dynamic library exports the address to a global variable defined within the library, how are accesses to that variable translated during dynamic linking so that a running application can interact with it?
How does a dynamic library's references to a global variable get translated once in the running app?
-
Compiling does the same thing as always - it dumps its symbol table into the object code, translating variable names into offsets. Fixing up the address so that an already running process can read/write it is the job of the dynamic linker or loader.– Kilian FothJun 5, 2013 at 12:10
-
Do you mean a global variable? In C, which you tagged your question with, static variables have internal linkage - they're not visible outside the translation unit, much less outside a dynamic library.– Sebastian RedlJun 5, 2013 at 12:40
-
@KilianFoth true. I will edit my question.– VictorJun 5, 2013 at 13:25
-
@SebastianRedl yes, I did mean to say global, thank you.– VictorJun 5, 2013 at 13:26
-
Do you really mean: "If a dynamic library exports the address of a global variable defined within the library"? That changes the meaning of the question!– Basile StarynkevitchJan 3, 2019 at 17:38
1 Answer
Dynamic linking is operating system specific (and very different on Linux and on Windows; read Levine's Linkers and Loaders book).
For Linux, a good explanation happens in Drepper's How to write shared libraries paper.
In general, the access to such a global variable may involve some indirection. Read about the Global Offset Table.