Question

As we know, linux call ldconfig to load all *.so libraries and then link the applications who use the shared library. However, I am confused how the global variable is working in this case. Since there is only one copy of shared library across all these application, do they share the global variables in the shared library? If yes, then how they synchronize?

Thanks,

Was it helpful?

Solution

No it is not shared - the code/text section of the library is shared - the data portion is unique to each process that uses the library

OTHER TIPS

As I commented:

  • Levine's book on linkers and loaders is a useful reference.

  • Linux dynamic linker ld.so is free software, part of GNU libc and you can study and improve its source code

  • the dynamic linker is ld.so not ldconfig (which just updated cached information used by ld.so).

  • the ld.so linker is using the mmap(2) system call to project some .so segments into the virtual address space of the process; the "text" segment (for code and read-only constants) uses MAP_SHARED with PROT_READ. The "data" segment (for global or static variables in C or C++) uses MAP_PRIVATE with PROT_WRITE

  • you would learn a lot by strace-ing your program to get a feeling of the involved system calls.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top