Last active
July 29, 2018 02:48
-
-
Save fcamel/fdac4af71af1be58fc9439943b09411f to your computer and use it in GitHub Desktop.
ELF visibility test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ cat prog.c | |
#include <stdio.h> | |
void func(); | |
void xyz() { | |
printf("main-xyz\n"); | |
} | |
void xyz2() { | |
printf("main-xyz2\n"); | |
} | |
int main(void) { | |
func(); | |
return 0; | |
} | |
$ cat foo.c | |
#include <stdio.h> | |
void xyz() { | |
printf("foo-xyz\n"); | |
} | |
void xyz2() { | |
printf("foo-xyz2\n"); | |
} | |
void func() { | |
xyz(); | |
xyz2(); | |
} | |
# | |
# Default behavior | |
# | |
$ gcc -g -c -fPIC -Wall -c foo.c | |
$ gcc -g -shared -o libfoo.so foo.o | |
$ nm libfoo.so | grep xyz | |
0000000000000730 T xyz | |
0000000000000743 T xyz2 | |
$ gcc -g -o prog prog.c libfoo.so | |
$ LD_LIBRARY_PATH=. ./prog | |
main-xyz | |
main-xyz2 | |
# | |
# Use -Bsymbolic to reference global symbols internally. | |
# | |
$ gcc -g -shared -o libfoo.so foo.o -Wl,-Bsymbolic | |
$ nm libfoo.so | grep xyz | |
00000000000006e0 T xyz | |
00000000000006f3 T xyz2 | |
$ LD_LIBRARY_PATH=. ./prog | |
foo-xyz | |
foo-xyz2 | |
# | |
# Use version script to hide local symbols. | |
# | |
$ cat libfoo.map | |
FOO { | |
global: func; xyz2; # explicitly list symbols to be exported. | |
local: *; # hide the rest. | |
}; | |
$ gcc -g -shared -o libfoo.so foo.o -Wl,--version-script=libfoo.map | |
$ nm libfoo.so | grep xyz # T/t means global/local symbol | |
0000000000000690 t xyz | |
00000000000006a3 T xyz2 | |
$ LD_LIBRARY_PATH=. ./prog | |
foo-xyz | |
main-xyz2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment