Site Name

Copyright © 2019 All rights reserved | This template is made with ♥ by Gulshan

Why some shared libraries are executable?21

That library has a main() function or equivalent entry point, and was compiled in such a way that it is useful both as an executable and as a shared object. Here's one suggestion about how to do this, although it does not work for me. Here's another in an answer to a similar question on S.O, which I'll shamelessly plagiarize, tweak, and add a bit of explanation. First, source for our example library, test.c: #include void sayHello (char *tag) { printf("%s: Hello!\n", tag); } int main (int argc, char *argv[]) { sayHello(argv[0]); return 0; } Compile that: gcc -fPIC -pie -o libtest.so test.c -Wl,-E Here, we are compiling a shared library (-fPIC), but telling the linker that it's a regular executable (-pie), and to make its symbol table exportable (-Wl,-E), such that it can be usefully linked against. And, although file will say it's a shared object, it does work as an executable: > ./libtest.so ./libtest.so: Hello! Now we need to see if it can really be dynamically linked. An example program, program.c: #include extern void sayHello (char*); int main (int argc, char *argv[]) { puts("Test program."); sayHello(argv[0]); return 0; } Using extern saves us having to create a header. Now compile that: gcc program.c -L. -ltest Before we can execute it, we need to add the path of libtest.so for the dynamic loader: export LD_LIBRARY_PATH=./ Now: > ./a.out Test program. ./a.out: Hello! And ldd a.out will show the linkage to libtest.so. Note that I doubt this is how glibc is actually compiled, since it is probably not as portable as glibc itself (see man gcc with regard to the -fPIC and -pie switches), but it demonstrates the basic mechanism. For the real details you'd have to look at the source makefile. Answered by goldilocks on unix.stackexhange.com in response to this question

published by gulshan on 2019-12-08 07:30:38

1 likes 0 dislikes

www.000webhost.com