Skip to content

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

@jpalus

Description

@jpalus

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions