Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mold links more libraries than necessary with --as-needed #1036

Closed
jpalus opened this issue Apr 26, 2023 · 5 comments
Closed

mold links more libraries than necessary with --as-needed #1036

jpalus opened this issue Apr 26, 2023 · 5 comments

Comments

@jpalus
Copy link
Contributor

jpalus commented Apr 26, 2023

Consider following chain of dependencies (libbar<-libfoo<-libtest):

libbar is standalone and has no dependencies:

$ gcc -fuse-ld=mold -o libbar.so -shared -x c - <<EOF                           
void bar() { }                    
EOF

libfoo needs libbar:

$ gcc -fuse-ld=mold -o libfoo.so -shared -x c - -Wl,--as-needed -L. -lbar <<EOF 
void bar();void foo() { bar(); } 
EOF
$ readelf -a libfoo.so|grep NEEDED
 0x0000000000000001 (NEEDED)             Shared library: [libbar.so]
 0x0000000000000001 (NEEDED)             Shared library: [libc.so.6]
 0x0000000000000001 (NEEDED)             Shared library: [ld-linux-aarch64.so.1]

libtest needs libfoo but has no direct dependency on libbar:

$ gcc -fuse-ld=mold -o libtest.so -shared -x c - -Wl,--as-needed -L. -lfoo -lbar <<EOF         
void foo();void test() { foo(); }
EOF

Result for mold 1.11.0:

$ readelf -a libtest.so|grep NEEDED
 0x0000000000000001 (NEEDED)             Shared library: [libfoo.so]
 0x0000000000000001 (NEEDED)             Shared library: [libbar.so]
 0x0000000000000001 (NEEDED)             Shared library: [libc.so.6]
 0x0000000000000001 (NEEDED)             Shared library: [ld-linux-aarch64.so.1]

Result for both bfd and lld:

$ readelf -a libtest.so|grep NEEDED                                                  
 0x0000000000000001 (NEEDED)             Shared library: [libfoo.so]

Since libtest does not have dependencies on libbar and libfoo requires libbar already I consider bfd and lld output to be correct.

@rui314
Copy link
Owner

rui314 commented Apr 27, 2023

Right, mold behaves exactly as you explained in this bug. It's not exactly the same as GNU ld, but in practice it didn't seem to cause any practical issues, so I settled with it. Is it causing an issue on your side?

@jpalus
Copy link
Contributor Author

jpalus commented Apr 27, 2023

Consider ABI change in libbar.so and subsequent SONAME change to reflect that. Now not only libfoo.so needs to be rebuilt against new libbar.so but also libtest.so. My primary use of mold is for packages building in linux distribution and that makes it enough of a reason to stop using it. Comparing to bfd and lld it results in many more packages needing rebuild when SONAME changes, users need to download more packages on upgrade and it's much harder to keep certain version of a program relying only on ABI stable libraries since some other libraries creep in during linking. In some way that makes mold slower than bfd and lld.

mold is of course free to choose semantics for its --as-needed flag so feel free to close it as not an issue. However if --as-needed filters only libraries that are not used by anything, so appear on command line somewhat by accident, then I'd say its usefulness is very limited.

@firasuke
Copy link

firasuke commented May 5, 2023

I was going to open a new issue with regards to a similar behavior when using mold as it adds libgcc_s.so.1 as a needed library to almost all executables when in fact they are not needed (example software attr).

@rui314
Copy link
Owner

rui314 commented May 6, 2023

Yeah, sorry for the trouble. Let me take a look. It's a bit risky to change this fundamental behavior at this point, but it's indeed an unnecessary churn compared to GNU ld.

@justanotheranonymoususer
Copy link
Contributor

I have the same problem, which makes Mold not practical in many cases as @jpalus explained, please consider fixing it

@rui314 rui314 closed this as completed in f02db0f Aug 17, 2023
VitalyAnkh pushed a commit to VitalyAnkh/mold that referenced this issue Dec 23, 2023
If an as-needed DSO is referenced only by other DSOs and not by
the output file we are creating, we won't mark it as "needed".

Fixes rui314#1036
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants