kripken/emscripten — Issue #266: Multiple interdependent shared libs
Author: eclecticdave
Created: 2012-02-20 23:38:25+00:00
State: open

Hi,

Recently Emscripten has gained the ability to link shared objects in addition to object files. AFAICT it just statically links the .so file just as if it were an ordinary object file. This is probably fine for simple cases but fails when linking multiple shared objects which are dependent on each other.

For example, consider the situation where executable 'foobar' is dependent on shared libraries 'foo' and 'bar' and 'foo' is itself dependent on 'bar' :

foobar --------- foo
\ \
\------------ bar

What happens here is, when 'foo' is built, 'bar' gets statically linked into it. Then when 'foobar' is built, the linker attempts to statically link 'foo' (which already includes all of 'bar') together with a second copy of 'bar'. This results in the link failing due to multiply defined symbols.

This doesn't occur in native builds as the dynamic loader is intelligent enough to only link a single copy of each library.

I'm not sure what the fix should be however. One approach could be to ignore .so files when linking a shared library and only include them when linking an executable. That would work for my example, but would then fail to link all the required libraries in other situations (for example if 'bar' was dependent on 'baz' but that library wasn't specified on the link command for 'foobar').

The best approach would be to record the dependancies when linking shared objects, and then include these with duplicates removed when linking the executable. I had a look at the llvm docs and it does look like bitcode files can contain references to dependancies, but I can't figure out if there's a way to get llvm-ld to record them.

Cheers,

David.