caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Joost Yervante Damad <joost@damad.be>
To: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] Embedding the ocaml runtime in a shared library on amd64/x86_64 [update]
Date: Tue, 5 Jan 2010 13:52:15 +0100	[thread overview]
Message-ID: <201001051352.16612.joost@damad.be> (raw)
In-Reply-To: <200912031157.00187.joost@damad.be>

Hello all,

a short update on my efforts to embed the ocaml runtime on amd64:

static linking the ocaml runtime works rather fine like this:

1. -fPIC

Make sure all files in asmrun are compiled with -fPIC by adapting the Makefile.

2) asmrun/amd64.S

    use an adapted amd64.S where 3 symbols are marked with @PLT:
    caml_program, caml_apply2, caml_apply3 like this:
        leaq    G(caml_program)@PLT(%rip), %r12
        leaq    G(caml_apply2)@PLT(%rip), %r12  /* code pointer */
        leaq    G(caml_apply3)@PLT(%rip), %r12  /* code pointer */

3) link the shared library

    # ocaml test code:
    ocamlopt -verbose -g -c mod.ml
    # wrapper code in C:
    ocamlopt -verbose -g -ccopt -fPIC -c mod_wrap.c
    # linking of lib, including needed packages
    ocamlfind ocamlopt -ccopt -Wl,-Bsymbolic -verbose -o modlib.native.so -
ccopt -shared mod.cmx mod_wrap.o -package unix -package bigarray -linkpkg

4) using the shared library
    # compile a main:
    ocamlopt -verbose -g -ccopt -Iinclude -c main.c
    # link a main:
    gcc -g -o main main.o -l:modlib.native.so -L. -Wl,-rpath=.

However, dynamic linking has more troubles:

I used an approach simular to the one already used for libcamlrun_shared.so to 
end up with a libasmrun_shared.so.

Whenever I try to use this by means of using -output-obj and then linking with 
g++ I always end
up with a program that crashes in caml_oldify_local_roots
during GC in a non-trivial program.

I finally got it working correctly by running ocamlopt with the verbose flag and 
adapting
the output to do something like this:

gcc -o 'libembedded.so'   '-L/usr/lib/ocaml/threads' '-L/usr/lib/ocaml' 
'/tmp/camlstartup.o' '/usr/lib/ocaml/std_exit.o' 'client.o' 'client.a' 
'/usr/lib/ocaml/bigarray.a' '/usr/lib/ocaml/dbm.a' '/usr/lib/ocaml/str.a' 
'/usr/lib/ocaml/threads/threads.a' '/usr/lib/ocaml/unix.a' 
'/usr/lib/ocaml/stdlib.a' '-lbigarray' '-lmldbm' '-lgdbm_compat' '-lgdbm' '-
lstr' '-lthreadsnat' '-lpthread' '-lunix' 'client_wrap.o' -L/usr/lib/ocaml -
lasmrun_shared -lm  -ldl -L../../../target/foo/ -lfoo -L. -lbar -L. baz.o -
shared -Wl,-Bsymbolic -ggdb3 -Wall -Wextra
Where I first used on of the old camlstartup*.s that was still lying around and 
compiling that to camlstartup.o:
as -o /tmp/camlstartup.o /tmp/camlstartupaf7c1e.s

The end result seems to work without crashing. It seems to me that in order to 
have a usable libasmrun_shared.so some changes to the toolchain (ocamlopt) are 
needed.

Given this result I'll stick to statically linking in libasmrun.a until there 
is a proper solution within the toolchain. Unless someone steps in and tells 
me I did something wrong ;-)

Greetings and happy new year, Joost Damad

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


      parent reply	other threads:[~2010-01-05 12:58 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-12-02 12:33 Embedding the ocaml runtime in a shared library on amd64/x86_64 Joost Yervante Damad
2009-12-02 16:51 ` [Caml-list] " Richard Jones
2009-12-02 22:37   ` Stefano Zacchiroli
2009-12-03 10:56     ` Joost Yervante Damad
2009-12-03 11:53       ` Alain Frisch
2009-12-03 12:47       ` Stefano Zacchiroli
2010-01-05 12:52       ` Joost Yervante Damad [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=201001051352.16612.joost@damad.be \
    --to=joost@damad.be \
    --cc=caml-list@yquem.inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).