1

Suppose we have a program (executable) prog that links to libA and libB. Both libA and libB in turn link to libX, which contains a global variables.

Will the global variable have a single instance, or two different instances within the prog process in the following cases?

  1. prog is linking to libA and libB is dynamically, but both of those link to libX statically. (I assume two instances?)
  2. prog is linking to libA and libB is dynamically, and both of those link to libX dynamically. (I assume one instance?)
  3. prog is linking to libA and libB is statically, and both of those link to libX statically. (I assume one instance again?)

Additionally:

  • Does it make a difference if the global was declared static (local to the translation unit)? In my use case, it is.
  • Does the answer to these questions differ between operating systems? (macOS, Linux, Windows)
  • Can you recommend some reading which explains the fundamentals that I need to be able to answer similar questions myself?
13
  • Why not check it?
    – KamilCuk
    Mar 9, 2021 at 10:21
  • @KamilCuk I am looking to get a deeper understanding of the topic. Checking this on a single platform won't give me that.
    – Szabolcs
    Mar 9, 2021 at 10:22
  • The "link to ... statically" is strange to me. You can't "link statically" with something. A static library is collection of object files, you can't "link with it". You can compile it into executable or into share library. Linking statically basically means compiling.
    – KamilCuk
    Mar 9, 2021 at 10:23
  • @KamilCuk I don't really want to nitpick about terminology. Do you need clarifications to the question? If yes, I can try to improve it.
    – Szabolcs
    Mar 9, 2021 at 10:30
  • 2
    @KamilCuk Static linking is not compilation. I can compile an object library on one computer and link it to code that I compile on another computer. The library may even be compiled in a language for which the second computer doesn't have a compiler. Static and dynamic linking are two separate concepts (the former at build time, the latter at run time). Mar 9, 2021 at 10:32

2 Answers 2

3

prog is linking to libA and libB is dynamically, but both of those link to libX statically. (I assume two instances?)

In this case, the answer depends on which symbols are exported from libA.so and libB.so.

If the variable (let's call it glob) has static linkage, then it will not be exported and you will have two separate instances.

Likewise, if the variable doesn't have static linkage, but libX is compiled with e.g. -fvisibility-hidden, or if either libA.so or libB.so is linked with a linker script which prevents the glob from being exported, you will have two separate instances.

However, if the variable has global linkage and its visibility is not restricted via one of the above mechanisms, then (by default) it will be exported from both libA.so and libB.so, and in that case all references to that variable will bind to whichever library is loaded first.

Update:

will there be two instances of that variable in memory, but just the first one is accessible, or the linker will not reserve any space at all for the second variable?

There will be two instances in memory.

When the linker builds libA.so, or libB.so, it has no idea what other libraries exist, and so it must reserve space in the readable and writable segment (the segment into which .data and .bss sections usually go) of the corresponding library.

At runtime, the loader mmaps the entire segment, and thus has no chance of not reserving memory space for the variable in each library.

But when the code references the variable at runtime, the loader will resolve all such references to the first symbol it encounters.

Note: above is the default behavior on ELF systems. Windows DLLs behave differently, and linking libraries with -Bsymbolic may change the outcome of symbol resolution as well.

prog is linking to libA and libB is dynamically, and both of those link to libX dynamically. (I assume one instance?)

Correct.

prog is linking to libA and libB is statically, and both of those link to libX statically. (I assume one instance again?)

This is an impossible scenario: you can't link libA.a against libX.a.

But when linking prog against libA.a, libB.a and libX.a, yes: you will end up with one instance of glob.

1
  • I'm curious it will be exported from both libA.so and libB.so, and in that case all references to that variable will bind to whichever library is loaded first in such case: will there be two instances of that variable in memory, but just the first one is accessible, or the linker will not reserve any space at all for the second variable?
    – KamilCuk
    Mar 14, 2021 at 1:42
-1

C does not have a global namespace. It has file scope (and others) and external linkage (and internal and none). In the situations described, using normal link methods, a static object defined in libX will have one instance in the program, regardless of whether its linkage is internal (declared with static) or external (without).

2
  • Is this the case even when libA and libB (both being shared libraries) were linked statically to libX? I would have expected this to create two separate global variable instances, one in libA and one in libB.
    – Szabolcs
    Mar 9, 2021 at 11:23
  • @Szabolcs: If you link libX to libA, making new combined object modules (either standalone or in a new library file) and remove the external symbols of libX and do the same with libX to libB, then link these two new object modules or libraries to one executable, you could produce an executable with two copies of libX and its internal data. So it can be possible with special link techniques. (If you do not remove the external symbols in at least one of those, the link to one executable should fail due to multiple definitions.) Mar 9, 2021 at 11:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.