English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
Embedding the ocaml runtime in a shared library on amd64/x86_64
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2009-12-02 (12:33)
From: Joost Yervante Damad <joost@d...>
Subject: Embedding the ocaml runtime in a shared library on amd64/x86_64

while trying to get the ocaml runtime embedded in a shared library, I ran into issues similar to [1]. 
Basically there are quite some symbols that have a R_X86_64_32 relocation in libasmrun.a.
I worked around that by rebuilding libasmrun.a with -fPIC, however there still is the file amd64.S which gives errors like this:

usr/bin/ld: /usr/lib/ocaml/libasmrun.a(amd64.o): relocation R_X86_64_PC32 against symbol `caml_last_return_address' can not be used when making a shared 
object; recompile with -fPIC

For that I found the following workaround: add the -Bsymbolic linker option to the command that creates the shared library, something like this:

ocamlopt -ccopt -Wl,-Bsymbolic -verbose -o libmod.so -ccopt -shared mod.cmx modwrap.o

Then my C application links with libmod.so and runs.
It should be possible to adapt amd64.S to generate the correct relocation entries by adding @PLT to some of the symbols, something like this:

movq    %r15, G(caml_young_ptr)@PLT(%rip)

However while it removed the need to use -Bsymbolic when linking, the resulting library seems to be faulty because the application segfaults.

I think compiling with -fPIC by default on the amd64/x86_64 arch would be a good start to making it more usable for embedding.
Also it would be very interesting if someone with good knowledge of ocaml internals could fix amd64.S to be position independent.

Greetings, Joost Damad

[1] http://groups.google.com/group/fa.caml/browse_thread/thread/d734c80bc3fc5a1d/395cd691e481ceb7

Joost Yervante Damad - http://damad.be/joost/