This is the mail archive of the binutils@sources.redhat.com mailing list for the binutils project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

creating shared objects without using -fpic


[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

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]