caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] bytecode apps without stdlib/pervasives
@ 2005-11-15 22:45 Jonathan Roewen
  2005-11-16  1:55 ` skaller
  2005-11-17 16:15 ` Damien Doligez
  0 siblings, 2 replies; 3+ messages in thread
From: Jonathan Roewen @ 2005-11-15 22:45 UTC (permalink / raw)
  To: caml-list

Hi,

I have a simple ML file (test.ml):

external raise : exn -> 'a = "%raise";;

raise End_of_file;;

so I can check how the compiler does things (like if predefined ocaml
symbols are still present without stdlib present), and compile as:

ocamlc -nostdlib -nopervasives test.ml, which generates a.out.

when I run it, I get error: "-bash: ./a.out: cannot execute binary
file" (btw, generates expected behabiour without those flags)

why can't it execute it? can we only use -nostdlib as long as we
provide our own pervasives module?

Jonathan


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] bytecode apps without stdlib/pervasives
  2005-11-15 22:45 [Caml-list] bytecode apps without stdlib/pervasives Jonathan Roewen
@ 2005-11-16  1:55 ` skaller
  2005-11-17 16:15 ` Damien Doligez
  1 sibling, 0 replies; 3+ messages in thread
From: skaller @ 2005-11-16  1:55 UTC (permalink / raw)
  To: Jonathan Roewen; +Cc: caml-list

On Wed, 2005-11-16 at 11:45 +1300, Jonathan Roewen wrote:
> Hi,
> 
> I have a simple ML file (test.ml):
> 
> external raise : exn -> 'a = "%raise";;
> 
> raise End_of_file;;
> 
> so I can check how the compiler does things (like if predefined ocaml
> symbols are still present without stdlib present), and compile as:
> 
> ocamlc -nostdlib -nopervasives test.ml, which generates a.out.
> 
> when I run it, I get error: "-bash: ./a.out: cannot execute binary
> file" (btw, generates expected behabiour without those flags)
> 
> why can't it execute it? can we only use -nostdlib as long as we
> provide our own pervasives module?

You need to link in the C startup code manually, and pass whatever
flags are need to the linker to tell it how to make an executable.
My system has:

/usr/lib/gcc/x86_64-linux-gnu/4.0.2/crtbegin.o
/usr/lib/gcc/x86_64-linux-gnu/4.0.2/crtbeginS.o
/usr/lib/gcc/x86_64-linux-gnu/4.0.2/crtbeginT.o
/usr/lib/gcc/x86_64-linux-gnu/4.0.2/crtend.o
/usr/lib/gcc/x86_64-linux-gnu/4.0.2/crtendS.o

/usr/lib/crt1.o
/usr/lib/crti.o
/usr/lib/crtn.o
/usr/lib/gcrt1.o
/usr/lib/Mcrt1.o
/usr/lib/Scrt1.o

here you can see the unresolved external 'main':

skaller@rosella:/work/felix/flx$ nm /usr/lib/crt1.o
0000000000000000 D __data_start
0000000000000000 W data_start
0000000000000000 R _IO_stdin_used
                 U __libc_csu_fini
                 U __libc_csu_init
                 U __libc_start_main
                 U main
0000000000000000 T _start

Exactly how the linker 'marks' a generated binary 'executable'
I don't know -- just linking in the startup/termination
code may not be enough.

>From my ld manpage:

OPTIONS
       The linker supports a plethora of command-line options,  but
       in  actual  practice  few of them are used in any particular
       context.  For instance, a frequent use  of  ld  is  to  link
       standard  Unix  object  files  on a standard, supported Unix
       system.  On such a system, to link a file "hello.o":

               ld -o <output> /lib/crt0.o hello.o -lc

       This tells ld to produce a file called output as the  result
       of  linking  the  file  "/lib/crt0.o" with "hello.o" and the
       library "libc.a", which will come from the  standard  search
       directories.  (See the discussion of the -l option below.)


Note of course all this is for starting up C or C++ programs.
How Ocaml works is a mystery .. :)

-- 
John Skaller <skaller at users dot sf dot net>
Felix, successor to C++: http://felix.sf.net


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] bytecode apps without stdlib/pervasives
  2005-11-15 22:45 [Caml-list] bytecode apps without stdlib/pervasives Jonathan Roewen
  2005-11-16  1:55 ` skaller
@ 2005-11-17 16:15 ` Damien Doligez
  1 sibling, 0 replies; 3+ messages in thread
From: Damien Doligez @ 2005-11-17 16:15 UTC (permalink / raw)
  To: caml-list


On Nov 15, 2005, at 23:45, Jonathan Roewen wrote:

> Hi,
>
> I have a simple ML file (test.ml):
>
> external raise : exn -> 'a = "%raise";;
>
> raise End_of_file;;
>
> so I can check how the compiler does things (like if predefined ocaml
> symbols are still present without stdlib present), and compile as:
>
> ocamlc -nostdlib -nopervasives test.ml, which generates a.out.
>
> when I run it, I get error: "-bash: ./a.out: cannot execute binary
> file" (btw, generates expected behabiour without those flags)
>
> why can't it execute it? can we only use -nostdlib as long as we
> provide our own pervasives module?

Note that a.out is still a valid bytecode file:
   ocamlrun ./a.out
works.

What happens is that ocamlc tries to create an executable bytecode file,
by prepending the standard header (#!/usr/local/bin/ocamlrun) to your
bytecode file.  But it can't find this header because you have specified
-nostdlib, thus preventing it from looking in its library directory.

It explicitely ignores the "file not found" error, I'm not sure why.

What you can do is provide your own header file, named "camlheader", and
placed in one of the directories searched by ocamlc.  If you don't give
any -I option, this has to be the current directory:

$ echo '#!/bin/echo foo' >camlheader
$ ocamlc -nostdlib -nopervasives test.ml
$ ./a.out
foo ./a.out

-- Damien


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2005-11-17 16:14 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-11-15 22:45 [Caml-list] bytecode apps without stdlib/pervasives Jonathan Roewen
2005-11-16  1:55 ` skaller
2005-11-17 16:15 ` Damien Doligez

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).