3

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?

Erik Eidt
  • 33,282
  • 5
  • 57
  • 91
Victor
  • 947
  • 7
  • 12
  • *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 Foth Jun 05 '13 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 Redl Jun 05 '13 at 12:40
  • @KilianFoth true. I will edit my question. – Victor Jun 05 '13 at 13:25
  • @SebastianRedl yes, I did mean to say global, thank you. – Victor Jun 05 '13 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 Starynkevitch Jan 03 '19 at 17:38

1 Answers1

1

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.

Basile Starynkevitch
  • 32,434
  • 6
  • 84
  • 125