Hi all-
I'm writing a shared library and I'd like to have it be executable like the libc or ld libraries such that it prints some version info when executed. I found an example (referenced at the end of this post) but I've been unable to make it work correctly. I did update the string with the correct path to ld.
From what I can gather, there are two things that I need to do for the library to be executable:
- define an entry point
- reference an interpreter in the .interp section
but I'm obviously missing something critical here because the simplist example does exactly this and fails.
The referenced example compiles without error, but will not execute
Code:
$ lsl dop.so
-rwxr-xr-x 1 cparker users 7.9K 2009-11-04 09:06 dop.so
$ ./dop.so
bash: ./dop.so: No such file or directory
$ /lib64/ld-2.9.so dop.so
dop.so: error while loading shared libraries: dop.so: cannot open shared object file
when I'm working with my own library, I just get segmentation faults
Code:
const char my_interp[] __attribute__((section(".interp"))) = "/lib64/ld-linux-x86_64.so.2";
...
...
// entry point to display the library information if executed
// extern "C" required to prevent the symbol from being mangled
extern "C" void lcipacket_main (int argc, char * argv[]) {
printf("liblcipacket version %d.%d.%d\n", MAJOR_VERSION, MINOR_VERSION,
REVISION);
} // function main
compiling and executing results in:
Code:
$ make
gcc -fPIC -Wall -g -c liblcipacket.cpp
gcc -lc -shared -Wl,-soname,liblcipacket.so.0,-e,lcipacket_main -o liblcipacket.so.0.0.0 liblcipacket.o
$ ./liblcipacket.so.0
Segmentation fault
Can anyone offer some wisdom?
Thanks,
--Chad
for reference:
gcc 4.3.3
ld/libc 2.9
arch x86_64
from
http://gcc.gnu.org/ml/gcc-help/2003-07/msg00232.html
Makefile:
Code:
dop.so: dop.c
gcc -Wall -W -fPIC -shared -o dop.so dop.c -lc -Wl,-e,my_main
Source file (dop.c):
Code:
const char my_interp[] __attribute__((section(".interp"))) = "/lib/ld.so.1";
void my_main(int argc, char **argv)
{
int i;
printf("Called as:");
for (i = 0; i < argc; i++)
printf(" %s", argv[i]);
printf("\n");
exit(0);
}