This is the mail archive of the
binutils@sources.redhat.com
mailing list for the binutils project.
creating shared objects without using -fpic
- To: binutils at sourceware dot cygnus dot com
- Subject: creating shared objects without using -fpic
- From: Doug Evans <dje at transmeta dot com>
- Date: Thu, 3 Aug 2000 19:30:58 -0700
[target = i386-linux]
Suppose I create a shared object but I _don't_ compile the
object files in it with -fpic.
Seems to me that this can't work, but simple tests
reveal that the dynamic linker is smart enough to
get some things right.
Did I miss something, or is -fpic on linux no longer useful.
so.c:
int a = 3;
int foo () { return a; }
main () { return 0; }
use-so.c:
#include <assert.h>
#include <dlfcn.h>
int
main ()
{
void* f;
void* foo;
int a;
f = dlopen ("./so.so", RTLD_LAZY);
assert (f);
foo = dlsym (f, "foo");
assert (foo);
a = ((int (*)()) foo) ();
printf ("a = %d\n", a);
return 0;
}
casey:~/tmp$ gcc -shared so.c -o so.so
casey:~/tmp$ gcc use-so.c -ldl
casey:~/tmp$ ./a.out
a = 3