How does the linker allow duplicate characters in dynamically loaded libraries?

I have two dynamically loaded libraries lib_smtp.so and libpop.so etc. Both have a global variable called protocol , which is initialized to β€œSMTP” and β€œPOP,” respectively. I have another static library libhttp.a where protocol initialized with "HTTP".

Now for some reason I need to compile all dynamically linked and loadable libraries statically and include in the executable. In doing so, I get a " multiple character definition " error when linking static libraries.

I am curious to know how the linker allows duplicate characters during dynamic linking, where all three of the mentioned libraries are connected?

Is there a way that I can do the same thing statically as the linker executing in the dynamic link, i.e. without any conflict, add all the static libraries to the executable that have the same characters? if not, why is the process different for statically linked libraries.

+1
c linux linker build dynamic-linking
May 6 '15 at 2:59
source share
2 answers

Dynamic linking in modern Linux and several other operating systems is based on the binary ELF format. Dynamic Libraries (ELFs) that an executable or other shared library relies on take precedence. To enable a given character, the dynamic linker checks each library in priority order until it finds the one that defines the character.

This can be risky when several dynamic objects define the same symbol, and also several dynamic objects use this symbol. Then it may happen that the symbol is differently allowed in different dynamic objects.

Full details are not suitable for SO, but I don’t know a better technical explanation than the Ulrich Drepper document β€œ How to write shared libraries .”

+2
May 6 '15 at 15:16
source share

In a dynamic connection, a tool called "symbol visibility" is launched. In fact, this allows you to expose only certain characters across the boundaries of the object (an object in the sense of a common object). A good style is to compile and associate common objects with characters hidden by default and only expose them explicitly, which are required by the called persons.

Character rendering is used for linking and so far is only used in dynamic linkers. Of course, it is also possible to have it in a static connection, the Apple GCC variant implements the so-called Mach-O relocatable files, which can be statically related to the visibility applied. But I don't know if vanilla GCC, binutils ld or the gold linker can do this for a plain old ELF.

+2
May 08 '15 at 8:27
source share



All Articles