Runtime linking

I’ve had a personally-compiled copy of vim7 sitting in my home directory at work for a while now. It was working really well. However, I then decided that it would make sense to make this available for all of the users. After speaking with David I set about compiling a copy to install in /export/local/sparc (which then gets mounted on /usr/local on the various SPARC SunRays).

Yesterday I finally found the right bits of source to hack to get it to compile properly. Once I’d tested that all was working well I went ahead and installed it. It worked fine on ebusy and for all of the people that I asked to test it. However, when Liam tried it, he received a runtime link error: ld.so.1 couldn’t find libncurses. On ebusy this was available in /usr/lib, and I just happened to have /opt/sfw/lib (where another libncurses lived) in my LD_LIBRARY_PATH.

It was obvious that Liam not having LD_LIBRARY_PATH=/opt/sfw/lib was the problem. I set about finding the best way to rectify this. My initial thought was to add a default library path. On Linux I’d edit the /etc/ld.so.conf file; under Solaris the default library path is changed with the crle(1) command. crle output on estale:

Default configuration file (/var/ld/ld.config) not found
Platform: 32-bit MSB SPARC
Default Library Path (ELF): /lib:/usr/lib (system default)
Trusted Directories (ELF): /lib/secure:/usr/lib/secure (system default)

This simply shows that libraries will be searched for in /lib and /usr/lib. So why then when I ran ldd(1) on my compiled vim binary was it reporting having found libraries in /usr/openwin/lib?

More digging with ldd and I found that the -s flag displayed the search path. Using this I quickly found that it was searching in more than the default paths that crle was telling me. There was also mention of RPATH: specifically a binary’s RPATH was telling the runtime linker to look in certain library paths. Excellent!

I then found a very interesting article at www.eyrie.org/~eagle/notes/rpath.html that covers shared library search paths in detail.

RPATH has much the same effect as LD_LIBRARY_PATH, except that it is resident in the binary, rather than as an environment variable. I recompiled vim with LDFLAGS=”-R/opt/sfw/lib” to add /opt/sfw/lib to the RPATH and Voila! The resulting binary now searches for libraries in /opt/sfw/lib. Needless to say it now finds libncurses and I’ve installed vim7 across all of our SPARC SunRay servers.

This was a very interesting discovery—I always struggle at getting source compiled “properly” and this is one big step in the right direction. Time to work on recompiling vim for x86 :)

Tags:

Leave a Reply