Skip to content

Instantly share code, notes, and snippets.

@fcamel
Created July 21, 2018 16:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fcamel/0631ae5855e0a68d35ddf006a6e58524 to your computer and use it in GitHub Desktop.
Save fcamel/0631ae5855e0a68d35ddf006a6e58524 to your computer and use it in GitHub Desktop.
ELF shared library order
$ cat prog.c
#include <stdio.h>
void foo();
void bar();
int main(void) {
foo();
bar();
return 0;
}
$ cat foo.c
#include <stdio.h>
void xyz() {
printf("foo-xyz\n");
}
void foo() {
printf("foo\n");
xyz();
}
$ cat bar.c
#include <stdio.h>
void xyz() {
printf("bar-xyz\n");
}
void bar() {
printf("bar\n");
xyz();
}
#
# foo before bar.
#
$ gcc -g -c -fPIC -Wall -c foo.c
$ gcc -g -c -fPIC -Wall -c bar.c
$ gcc -g -shared -o libfoo.so foo.o
$ gcc -g -shared -o libbar.so bar.o
$ gcc -g -o prog prog.c libfoo.so libbar.so
$ objdump -p prog | grep NEEDED
NEEDED libfoo.so
NEEDED libbar.so
NEEDED libc.so.6
$ ldd prog
linux-vdso.so.1 => (0x00007ffcc27ac000)
libfoo.so => not found
libbar.so => not found
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f5b651ab000)
/lib64/ld-linux-x86-64.so.2 (0x00007f5b65575000)
$ LD_LIBRARY_PATH=. ./prog
foo
foo-xyz
bar
foo-xyz
#
# bar before foo
#
$ gcc -g -o prog prog.c libbar.so libfoo.so
$ objdump -p prog | grep NEEDED
NEEDED libbar.so
NEEDED libfoo.so
NEEDED libc.so.6
$ ldd prog
linux-vdso.so.1 => (0x00007ffe888e9000)
libbar.so => not found
libfoo.so => not found
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fbf103ad000)
/lib64/ld-linux-x86-64.so.2 (0x00007fbf10777000)
$ LD_LIBRARY_PATH=. ./prog
foo
bar-xyz
bar
bar-xyz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment