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

Extra link added with -shared are useless #228

Closed
bioinfornatics opened this issue Nov 8, 2012 · 9 comments
Closed

Extra link added with -shared are useless #228

bioinfornatics opened this issue Nov 8, 2012 · 9 comments

Comments

@bioinfornatics
Copy link
Contributor

a D file : libFoo.d

size_t add( size_t a , size_t b ){ return a + b ; }
$ ldc2 -shared libFoo.d -of libFoo.so.1 -soname=libFoo.so.1
$ ldd libFoo.so.1
    linux-vdso.so.1 =>  (0x00007fff14000000)
    libphobos-ldc.so.59 => /lib64/libphobos-ldc.so.59 (0x00007fb01a820000)
    librt.so.1 => /lib64/librt.so.1 (0x00007fb01a618000)
    libdl.so.2 => /lib64/libdl.so.2 (0x00007fb01a410000)
    libpthread.so.0 => /lib64/libpthread.so.0 (0x00007fb01a1f0000)
    libm.so.6 => /lib64/libm.so.6 (0x00007fb019ef0000)
    libc.so.6 => /lib64/libc.so.6 (0x00007fb019b38000)
    libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x00007fb019920000)
    /lib64/ld-linux-x86-64.so.2 (0x0000003e6c000000)

At least

  • libdl
  • librt
  • libm
  • libpthread

are useless as libFoo will never call a function inside these libraries

@bioinfornatics
Copy link
Contributor Author

with gcc instead to do -shared you can do -Wl,--as-needed and linker will add only link needed
if i remember ldc use gcc to link. To fix maybe just check when ggc is call to link

@bioinfornatics
Copy link
Contributor Author

I think these link is hardcoded somewhere into ldc

as by using gcc i do not have this extra link

$ ldc2  -c -relocation-model=pic libFoo.d -oflibFoo.o
$ gcc libFoo.o --shared -o libFoo.so.1
$ ldd libFoo.so.1
    linux-vdso.so.1 =>  (0x00007fff53400000)
    libc.so.6 => /lib64/libc.so.6 (0x00007f3ef3b08000)
    /lib64/ld-linux-x86-64.so.2 (0x0000003e6c000000)

@dnadlinger
Copy link
Member

There is no need to guess; linking is not magic. Use the -v switch to LDC in order to have it print the exact GCC command used for linking. And you are correct, librt, libpthread, etc. are linked in by default (see linker.cpp), because they are needed by some D applications, especially if statically linking against Phobos (which is still the only supported option). The question is just why --as-needed doesn't cause them to be dropped if the executable actually doesn't reference them.

@bioinfornatics
Copy link
Contributor Author

ok so we could use the boolean var sharedLib to add these link when they are needed no ?

@bioinfornatics
Copy link
Contributor Author

diff --git a/driver/linker.cpp b/driver/linker.cpp
index 72e16e6..f8d9581 100644
--- a/driver/linker.cpp
+++ b/driver/linker.cpp
@@ -196,16 +196,19 @@ int linkObjToBinaryGcc(bool sharedLib)
     bool addSoname = false;
     switch(global.params.os) {
     case OSLinux:
-        addSoname = true;
-        args.push_back("-lrt");
+        if (!sharedLib)
+            args.push_back("-lrt");
         // fallthrough
     case OSMacOSX:
-        args.push_back("-ldl");
+        if (!sharedLib)
+            args.push_back("-ldl");
         // fallthrough
     case OSFreeBSD:
         addSoname = true;
-        args.push_back("-lpthread");
-        args.push_back("-lm");
+        if (!sharedLib){
+            args.push_back("-lpthread");
+            args.push_back("-lm");
+        }
         break;

     case OSSolaris:

addSoname = true; line 199 is removed as it is set line line 208 to same value

@dnadlinger
Copy link
Member

I think this patch is wrong. What matters is not whether you are building a shared library, but whether Phobos is linked in as a shared library. Besides, I'm quite sure that it is possible for a typical application to reference symbols e.g. from libm without even explicitly invoking the functions, so in this case the »extra« linked library would be needed.

I think if you want to reduce the number of linked libraries, you should investigate --as-needed more closely, as it is supposed to handle exactly this use case. Also note that ldd -r -u <someldcexecutabele> actually reports no unused direct dependencies on my system when building with ldc2 -L--as-needed.

@dnadlinger
Copy link
Member

Hm, it doesn't seem to strip out the direct dependencies on librt and libdl for shared libraries though…

@bioinfornatics
Copy link
Contributor Author

$ ldc2 -shared libFoo.d -of libFoo.so.1 -soname=libFoo.so.1
$ ldd libFoo.so.1
    linux-vdso.so.1 =>  (0x00007fff5a730000)
    libphobos-ldc.so.59 => /lib64/libphobos-ldc.so.59 (0x00007ff1da0e8000)
    librt.so.1 => /lib64/librt.so.1 (0x00007ff1d9ee0000)
    libdl.so.2 => /lib64/libdl.so.2 (0x00007ff1d9cd8000)
    libpthread.so.0 => /lib64/libpthread.so.0 (0x00007ff1d9ab8000)
    libm.so.6 => /lib64/libm.so.6 (0x00007ff1d97b8000)
    libc.so.6 => /lib64/libc.so.6 (0x00007ff1d9400000)
    libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x00007ff1d91e8000)
    /lib64/ld-linux-x86-64.so.2 (0x0000003e6c000000)
$ ldd -r -u libFoo.so.1
Unused direct dependencies:
    /lib64/librt.so.1
    /lib64/libdl.so.2
    /lib64/libpthread.so.0
    /lib64/libm.so.6

@dnadlinger
Copy link
Member

The output for -L--as-needed looks like this now:

$ ldd libFoo.so.1
    linux-vdso.so.1 (0x00007fff142fc000)
    libdruntime-ldc.so.65 => /home/aule/Build/Work/ldc2-llvm3.4-release-shared/lib/libdruntime-ldc.so.65 (0x00007fa5093c8000)
    libc.so.6 => /usr/lib/libc.so.6 (0x00007fa50900e000)
    libm.so.6 => /usr/lib/libm.so.6 (0x00007fa508d0a000)
    libpthread.so.0 => /usr/lib/libpthread.so.0 (0x00007fa508aeb000)
    libdl.so.2 => /usr/lib/libdl.so.2 (0x00007fa5088e7000)
    libgcc_s.so.1 => /usr/lib/libgcc_s.so.1 (0x00007fa5086d1000)
    /usr/lib64/ld-linux-x86-64.so.2 (0x00007fa50948d000)

None of these is unneeded, as every D shared library depends on druntime, which in turn pulls in libc and so on.

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

No branches or pull requests

2 participants