14

I wrote a shared library named "libmyssl.so", I use some openssl function in my code, the make file looks like follows:

g++ -v -shared -lz -lssl -lcrypto -Wl,-soname,libmyssl.so.1,-o libmyssl.so.1.0 myssl.o

After that, I use ldd command to look if it depends on libssl.so:

ldd libmyssl.so.1.0

The result as follows:

linux-vdso.so.1 =>  (0x00007fff743fe000) 
libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f0bc963b000) 
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f0bc9276000) 
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f0bc8f6f000) 
/lib64/ld-linux-x86-64.so.2 (0x00007f0bc9ea0000) 
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f0bc8d59000)

It seems it didn't depend on libssl.so, am I right?

However, I use readelf -s command to see the symbols as follows:

readelf -s libmyssl.so.1.0

The reasult as follow:

......
259: 0000000000000000     0 NOTYPE  GLOBAL DEFAULT  UND SSL_new
260: 0000000000000000     0 NOTYPE  GLOBAL DEFAULT  UND SSL_get_shutdown
261: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND close@GLIBC_2.2.5 (4)
262: 0000000000000000     0 NOTYPE  GLOBAL DEFAULT  UND d2i_X509
263: 0000000000000000     0 NOTYPE  GLOBAL DEFAULT  UND X509_get_pubkey
264: 0000000000000000     0 NOTYPE  GLOBAL DEFAULT  UND SSL_set_info_callback
265: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND gmtime_r@GLIBC_2.2.5 (4)
......

It seems that the X509_get_putkey is not relocated. So it should depend on libssl.so. Maybe I didn't understand it well.

Hope someone can explain more about that, thanks very much!

Edgar Rokjān
  • 17,245
  • 4
  • 40
  • 67
joe
  • 235
  • 3
  • 10
  • 3
    A better way to check is would be to use `objdump` and looked for needed libraries as `objdump -p libmyssl.so.1.0 | grep NEEDED` – Inian Nov 22 '16 at 10:56
  • I try you command and get the same result - only libstdc++.so.6 and libc.so.6 in the list, libssl.so dosen't appear. – joe Nov 22 '16 at 13:40

1 Answers1

9

Some distributions would require you to list linked libraries after object files that reference them (to mimic how static libs work). So try to build like:

g++ -v -shared -Wl,-soname,libmyssl.so.1,-o libmyssl.so.1.0 myssl.o -lz -lssl -lcrypto
yugr
  • 19,769
  • 3
  • 51
  • 96
  • Thanks very much! You are right. I move the -lz -lssl -lcrypto to the end of the command line, after that, I found the result of "ldd libmyssl.so.1.0" contains "libssl.so". I try to remove "-lssl" from the command line, It still can build "myssl.so.1.0" successfully. So I want to know if I can remove all depends libraries when building a shared library? – joe Nov 22 '16 at 14:13
  • 3
    By default ld does not require to resolve missing symbols in shared library so it won't emit an error if you remove -lssl. You'll get an error at runtime though (loader won't know that it needs to load libssl and you'll get undefined symbol for SSL_new). I recommend to link with `-Wl,--no-undefined` to catch such errors at build time. – yugr Nov 22 '16 at 14:22
  • Understand.Thanks for your explanation. – joe Nov 22 '16 at 14:45