From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail4-relais-sop.national.inria.fr (mail4-relais-sop.national.inria.fr [192.134.164.105]) by yquem.inria.fr (Postfix) with ESMTP id EA379BBAF for ; Wed, 2 Dec 2009 13:33:14 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgoFAMLtFUtdurFZ/2dsb2JhbACBTtVlgXqCNwSBaQ X-IronPort-AV: E=Sophos;i="4.47,327,1257116400"; d="scan'208";a="51369133" Received: from turin.teluna.org ([93.186.177.89]) by mail4-smtp-sop.national.inria.fr with ESMTP; 02 Dec 2009 13:33:14 +0100 Received: from localhost (localhost.localdomain [127.0.0.1]) by turin.teluna.org (Postfix) with ESMTP id 6308314535; Wed, 2 Dec 2009 13:24:51 +0100 (CET) X-Virus-Scanned: Debian amavisd-new at turin.teluna.org Received: from turin.teluna.org ([127.0.0.1]) by localhost (turin.teluna.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id PB4xie2QsPG7; Wed, 2 Dec 2009 13:24:49 +0100 (CET) Received: from glaurung.localnet (d54C25275.access.telenet.be [84.194.82.117]) (Authenticated sender: joost@damad.be) by turin.teluna.org (Postfix) with ESMTPSA id EB15C14436; Wed, 2 Dec 2009 13:24:48 +0100 (CET) From: Joost Yervante Damad To: caml-list@inria.fr Subject: Embedding the ocaml runtime in a shared library on amd64/x86_64 Date: Wed, 2 Dec 2009 13:33:11 +0100 User-Agent: KMail/1.12.2 (Linux/2.6.29-2-amd64; KDE/4.3.2; x86_64; ; ) Cc: toolslive@yahoo.com MIME-Version: 1.0 Content-Type: Text/Plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Message-Id: <200912021333.11599.joost@damad.be> X-Spam: no; 0.00; ocaml:01 runtime:01 ocaml:01 runtime:01 libasmrun:01 rebuilding:01 libasmrun:01 -fpic:01 usr:01 usr:01 lib:01 recompile:01 -fpic:01 ocamlopt:01 -wl:01 Hello, 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/