7

I want to compile a shared library with an .interp segment.

#include <stdio.h>

int foo(int argc, char** argv) {

    printf("Hello, world!\n");
    return 0;

}

I'm using the following commands.

gcc -c -o test.o test.c
ld --dynamic-linker=blah -shared -o test.so test.o

I end up without an INTERP segment, as if I never passed the --dynamic-linker=blah option. Check with readelf -l test.so. When building an executable, the linker processes the option correctly and puts an INTERP segment in the program header. How to do I make it work for shared libraries too?

3 Answers 3

4

ld doesn't include a .interp section if -shared is used, as @MichaelDillon already said. You can however provide this section yourself.

const char interp_section[] __attribute__((section(".interp"))) = "/path/to/dynamic/linker";

The line above will save the string "/path/to/dynamic/linker" in the .interp section using GCC attributes.

If you're trying to build a shared object that's also executable by itself, check this question out. It has a more comprehensive description of the process.

2

The INTERP segment only goes into binaries which need to load the ELF interpreter (ld.so) in the first place. A shared library has no INTERP segment because the ELF interpreter is already loaded before the shared library is loaded.

1
  • 1
    This is only true by default. You can make a shared object executable on its own by adding an INTERP section as @jacwah writes in his reply to the OP. For example, the glibc shared object is also an executable as you can see by doing readelf -a /lib/x86_64-linux-gnu/libc.so.6 | grep INTERP. Jun 3, 2016 at 13:58
0

In most linux systems the ldconfig is run at every system boot and it looks definitions in /etc/ld.so.conf for looking in directories that have shared libraries. In the file /etc/ld.so.cache there are mappings for shared libraries sonames and the library full path. Consider reading this article: http://grahamwideman.wordpress.com/2009/02/09/the-linux-loader-and-how-it-finds-libraries/#comment-164

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.