by cryptonector · 3y ago · 0 comments
Ah, that brings me to something else that someone like jart might want to take on:
# bringing static linking for C into the 21st century
What's wrong with static linking in C? Well, it's still stuck with 1970s semantics, while dynamic linking has much nicer semantics. Specifically:
- static linking flattens the dependency
tree, which causes
- symbol collision issues
- also, there's no RPATH equiv. for static
linking
My thinking is to: - write dependency metadata (-lfoo, rpath,
-L/...) into a "hidden" .o that has just
this metadata encoded in it
- make the static linker look for that
metadata in dependencies and resolve
conflicts the same way as in dynamic
linking
- for each link-edit, including the final
link-edit of the executable, require
only that direct -lfoo dependencies be
listed
Well, anyways, it's a lot to ask :(AFAIK, the gnu dynamic linker doesn't do any namespacing (as opposed to Solaris').
In the versioned symbols case this is done via decorating symbols in the dependent with the SONAME and version of the dependency that is expected to provide them. In direct binding this is done by decorating symbols in the dependent with just the SONAME of the dependency that is expected to provide them.
The versioned symbol technique allows a shared object to provide multiple versions of a symbol with the same name, which can be useful for some things, but direct binding is just much easier to use than versioned symbols: just add `-B direct` to the link-edits and you're done.
Even in the absence of symbol versioning and direct binding, the fact that each object records its dependencies means that symbol conflicts will be only among those dependencies, and this can be discovered at link-edit time.
LD_PRELOAD interposers, on the other hand, can cause symbol conflicts in spite of all the foregoing. For this you can use `-B protected` or similar, but you may not want to.
Tell us more?
On Linux you’ll have one global variable. On Windows each DLL will have its own copy.
Which is better? It depends. Personally I think it’s much better for each shared/dynamic library to be a black box with a clearly defined API boundary. Thus I think the Windows approach is better.
The root evil here is that C++ is underdefined and what should really really really be a language level rule is actually left up to the platform.
I also think globals are evil and should almost never be used. But that’s a separate topic.
> The root evil here is that C++ is underdefined and what should really really really be a language level rule is actually left up to the platform.
Well, the issue is that C++ shares linking semantics with C, and C's linking semantics are not that well specified, because there's static linking -broken- and ELF -which has evolved to fix the problem you mention-.