Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to statically link against a shared object?

My question is not the same as this question.

I'm working on a project with a standalone binary that has no dynamic/external linkage, and runs in a *nix environment.

I'm attempting to move to a newer toolset to build with, but some of the static libraries that are available with the older toolset aren't available now -- for example, the crt libraries that provided _start aren't provided in this toolset.

I've been digging through the files provided with the vendor's toolset and found some shared objects with the symbols I needed from the crt libraries (eg, _start, _fini, etc) but I'm unsure whether there's a straightforward way to statically link a shared object into a binary, and further have that binary be executable.

Short version: Can a non-shared-object binary be statically linked with a shared object without the result becoming another shared object?

like image 469
Brian Vandenberg Avatar asked Apr 19 '11 17:04

Brian Vandenberg


People also ask

What are two disadvantages of static linking of shared libraries?

Slower execution time compared to static libraries. Potential compatibility issues if a library is changed without recompiling the library into memory.

Which is better static or dynamic linking?

A: Dynamic linking became the default for Solaris 1 in 1988 with the advent of SunOS 4.0, and is, of course, the default for Solaris 2. It has several advantages and, in many cases, offers better performance than static linking.

What is the difference between static and dynamic linking?

Definition. Static linking is the process of copying all library modules used in the program into the final executable image. In contrast, dynamic linking is the process of loading the external shared libraries into the program and then binds those shared libraries dynamically to the program.

What does it mean to statically link?

Static linking means that the code for all routines called by your program becomes part of the executable file. Statically linked programs can be moved to run on systems without the XL Fortran runtime libraries.


2 Answers

There's a fundamental difference between a shared library and a static library. First off, do search this site for previous discussions, and check out this question too (and the answers therein).

Basically, a static library is just a collection of objects, and the linker resolves the symbol names into fixed addresses -- this is required for static linking. On the other hand, a shared library is much more like an independent executable, which is loaded into memory by the loader and has entry point addresses to which the program jumps. However, relocation tables that static libraries have are generally not preserved when a shared library is being linked, so it's in general not possible to extract linkable object code from inside a linked shared library.

like image 167
Kerrek SB Avatar answered Sep 29 '22 18:09

Kerrek SB


Yeah, I know this is an 6 year-old question. I was told that it was possible to statically link against a shared-object library, but I've also discovered that it is not.

To actually demonstrate that statically linking a shared-object library is not possible with ld (gcc's linker), use the following gcc command:

gcc -o executablename objectname.o -Wl,-Bstatic -l:libnamespec.so

(Of course you'll have to compile objectname.o from sourcename.c, and you should probably make up your own shared-object library as well. If you do, use -Wl,--library-path,. so that ld can find your library in the local directory.)

The actual error you receive is:

/usr/bin/ld: attempted static link of dynamic object `libnamespec.so'
collect2: error: ld returned 1 exit status

Clearly, attempting to pull the object out of the shared-object library is something about which ld will balk.

There were some suggestions made here, but my interest in this question was merely academic.

Hope that helps.

like image 29
Ian Moote Avatar answered Sep 29 '22 18:09

Ian Moote