creating shared objects without using -fpic

Doug Evans dje@transmeta.com
Thu Aug 3 19:31:00 GMT 2000


[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


More information about the Binutils mailing list