This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Creating executable shared libraries ?
mandag 16 august 2004, 17:42, skrev Segher Boessenkool:
> See http://gcc.gnu.org/ml/gcc-help/2003-07/msg00232.html .
Thanks, but I'm still having problems. The shared library can now be run and
call functions without a problem, but argc and argv are garbage. This is a
problem, because the main function needs to know its invocation name, and it
also takes arguments. I suppose I can use readlink with /proc/self/exe to
get the executable name at least, and possibly /proc/self/cmdline for the
arguments (gotta try that), but is there another (better) way ? (Also,
returning from main segfaults, is that normal ? Not that *that*'s a problem,
setting return type to void and just using exit works fine).
Also, I had to change "/lib/ld.so.1" to "/lib/ld-linux.so.2" since the first
one doesn't exist on my box ("ld-linux.so.2" is a symlink to "ld-2.3.2.so").
Is there a way to have this work with both (or any ld.so's) without a
recompile ? The lib is open source, so compile-time config isn't a problem,
but it'd be nice if pre-compiled binaries could support both. (Tried using
multiple .interp strings, but that didn't seem to work.)
Here's a test program, compiled with (seem to remember that -DPIC from
somewhere, don't know if it's necessary ?)
gcc -W -Wall -fPIC -DPIC -shared test.c -o test -Wl,-e,my_main
-- 8< --
#include <stdio.h>
#include <stdlib.h>
const char interp[] __attribute__((section(".interp"))) =
"/lib/ld-linux.so.2";
void my_main (int argc, char *argv[])
{
printf("argc = %i (%u)\nargv = %p\n", argc, argc, argv);
printf("*argv = %s\n", *argv);
exit(0);
}
-- 8< --
Output (the numbers at argc/argv are constant for same arguments, but change
for different args -- the difference in values seems to hint that the args
are stored, but the address isn't readable):
-- 8< --
$ ./test
argc = -1073744275 (3221223021)
argv = (nil)
Segmentation fault
$ ./test 1
argc = -1073744277 (3221223019)
argv = 0xbffff672
Segmentation fault
$ ./test 1 2
argc = -1073744279 (3221223017)
argv = 0xbffff670
Segmentation fault
$ ./test 1 23
argc = -1073744280 (3221223016)
argv = 0xbffff66f
Segmentation fault
-- 8< --
Any ideas ? (Thought about using mprotect, but that seems a bit, eh, hackish
and insecure..)
- Gerry