Menu

#29 static libphobos and amd64

open
nobody
None
5
2007-09-06
2007-09-06
Anonymous
No

Hi,

i don't know if im exactly right here, but:
i use gdc on my gentoo system (core2duo with amd64 profile) and i try to build a shared library that is linked against phobos. The problem is, that libphobos.a is build as non-PIC code and can't be loaded in a shared library on amd64 - on x86 this is allowed.

I get the following error:

/usr/lib/gcc/x86_64-pc-linux-gnu/4.1.2/../../../../x86_64-pc-linux-gnu/bin/ld: /usr/lib/gcc/x86_64-pc-linux-gnu/4.1.2/libgphobos.a(loader.o): relocation R_X86_64_32S against `a local symbol' can not be used when making a shared object; recompile with -fPIC
/usr/lib/gcc/x86_64-pc-linux-gnu/4.1.2/libgphobos.a: could not read symbols: Bad value

I've tried in vain to patch the make system by myself and wrote a bug report on bugs.gentoo.org, too. But they said i should ask here.

Can you give me any hint how i fix this?

Thank you!

Discussion

  • David Friedman

    David Friedman - 2007-09-07

    Logged In: YES
    user_id=1378044
    Originator: NO

    If you want to build a D shared library that will either be loaded at run time or dynamically by a D program...

    Link the shared library using gcc (instead of gdc). This will leave symbol references to libgphobos undefined; they will be resolved correctly when the library is loaded.

    Note that if you load the library dynamically (i.e., dlopen), there is no support (yet) for module static constructors and destructors.

    Example, using libtool:
    $ libtool --tag=silly --mode=compile gdc -c -o dslib.lo dslib.d
    $ libtool --tag=silly --mode=link gcc -o libdslib.la -rpath `pwd` dslib.lo
    $ gdc -o test stest.d -L.libs -ldslib

     
  • Nobody/Anonymous

    Logged In: NO

    But is there a cause why phobos gets build as a static library and non-PIC?

    Unfortunately, I want to built a plugin and it is loaded by dlopen. :(

    As far as i understand building phobos as PIC would solve my problem and
    it should not be a big deal.

    Thank you!

     
  • David Friedman

    David Friedman - 2007-09-07

    Logged In: YES
    user_id=1378044
    Originator: NO

    Phobos has been built as a static library as a hedge against incompatible changes. Because it is built static, there isn't usually a need to build it with -fpic.

    You will need to build gdc from the sources. *Before* running setup-gcc.sh, edit d/phobos/Makefile.in: Add "-fpic" to DFLAGS and OUR_CFLAGS. It should be possible to link the resultant libgphobos.a into a shared library.

    A few notes:
    1. You will need to initialize Phobos manually. See d/phobos/internal/dgccmain2.d

    2. Because Phobos is private to the shared library, there can only be one such library loaded in an executable. Otherwise, the garbage collectors will conflict.

     

Log in to post a comment.

Auth0 Logo