Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ld64.so present in ldd, missing at runtime

Tags:

c++

linux

gcc

ldd

I am compiling some code, and for various reasons I'm doing it statically. On my Ubuntu 12.04 / gcc 4.6.3 machine it compiles executes fine, and is fully static:

> ldd mycode
    not a dynamic executable

So far so good. But I also need to run it on another machine, a Scientific Linux 5 system, running gcc 4.5.3. For some reason here, ldd has some dynamic libs left over:

> ldd mycode
    linux-vdso.so.1 =>  (0x00007fffd75fd000)
    libstdc++.so.6 => /usr/local/swift/gcc-4.5.3/lib64/libstdc++.so.6 (0x00002b4bafab2000)
    libm.so.6 => /lib64/libm.so.6 (0x000000398ca00000)
    libc.so.6 => /lib64/libc.so.6 (0x000000398c600000)
    /lib/ld64.so.1 => /lib64/ld-linux-x86-64.so.2 (0x000000398c200000)
    libgcc_s.so.1 => /usr/local/swift/gcc-4.5.3/lib64/libgcc_s.so.1 (0x00002b4bafdb8000)

Which is, in itself, fine. The code compiles and links OK, and as you can see from ldd, all the dependencies are resolved. However, when I try to execute it on the SL machine, it fails:

> ./mycode
  /lib/ld64.so.1: bad ELF interpreter: No such file or directory

As far as I can see therefore, at execute time for some reason the /lib/ld64.so.1 => /lib64/ld-linux-x86-64.so.2 link is not being resolved, even though ldd does it. Of course, on machines where I have root access I can solve this by making a sym link from ld-linux-x84..blah to /lib/ld64.so.1 but this is a rather pants solution and I can't apply it to our cluster. If I compile the whole thing dynamically it works fine, but this means installing all the 3rd party libraries I'm compiling against on a bunch of machines which I wanted to avoid. Finally, all the dependencies against which I compile are also used by another project, also compiled with cmake, and in that case I have no problem, and ldd actually lists the /lib64/ld-linux...blah direct, not a call to /lib/ld64.so.

So - why is this happening? Why can I compile and link the code OK, ldd it OK, and yet not execute it? Any ideas would be gratefully received!

like image 381
Phil Evans Avatar asked May 12 '14 08:05

Phil Evans


1 Answers

OK, I think I cracked this in the end. It was basically the linker arguments being passed by cmake, which were a nasty combination of static and dynamic ones, However, adding the following lines to my CMakeLists.txt fixed it:

SET_TARGET_PROPERTIES (mytarget PROPERTIES LINK_SEARCH_START_STATIC 1)
SET_TARGET_PROPERTIES (mytarget PROPERTIES LINK_SEARCH_END_STATIC 1)
like image 165
Phil Evans Avatar answered Oct 05 '22 18:10

Phil Evans