caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Broken 32 bit cross-compiler on 64bit host
@ 2015-10-08  9:43 Stefan Hellermann
  2015-10-08  9:52 ` Nicolas Ojeda Bar
  0 siblings, 1 reply; 5+ messages in thread
From: Stefan Hellermann @ 2015-10-08  9:43 UTC (permalink / raw)
  To: caml-list


[-- Attachment #1.1: Type: text/plain, Size: 2461 bytes --]

Hello,

currently I'm trying to add OCaml to the openwrt router distribution [1]
[2]. This would mean you could run your favorite OCaml applications on your
router. I would like to run unison [3] on my router.

So I first build native OCaml 4.02.3 on my x86_64 linux host, and then
build a OCaml cross-compiler for the 32 bit target, e.g.
mips-openwrt-linux. Building OCaml went fine after adding a small patch to
manually set Endianness and Bitness in configure script, please have a look
at attached patch.

host OCaml configure line:
./configure -prefix somepath -no-pthread -no-debugger -no-ocamldoc
-no-graph -no-cfi
cross OCaml configure line:
./configure -prefix someotherpath -target-bindir /usr/bin -host
x86_64-unknown-linux-gnu -target mips-openwrt-linux-musl -cc
"mips-openwrt-linux-musl-gcc some target cflags" -as
"mips-openwrt-linux-musl-as some target as flags" -no-pthread
-no-shared-libs -no-debugger -no-ocamldoc -no-graph -no-cfi -big-endian


Now the bug: When running OCaml applicantions (build with -custom), they
crash on startup on the target:

Fatal error: exception Failure("input_value: integer too large")


This is similar to OCaml Bug #5977 [4]

There is a workaround:
Build the host OCaml as 32 bit OCaml, then build the cross compiler with
this 32 bit host OCaml. The resulting binaries work on the target.
This is what I did for openwrt [2], but it fails on 64 bit hosts where no
32 bit development tools are installed. It only catches the case when
building on x86_64 hosts, it will break when building on say powerpc64 or
64 bit arm. It's also problematic for openwrt, as only the cross compiler
but not the host tools are recompiled if the target arch changes, e.g. from
64 bit arm to 32 bit mips.

Now my questions:
- Is building a 32 bit cross compiler on a 64 bit host supposed to work?
- Is my patch to configure ok, or is there a better way to configure the
cross-compiler?
- Should I run some test suite on the target and post the results?

[WARNING] Something went wrong while checking native division and modulus
please report it at http://http://caml.inria.fr/mantis/
Do I have to care?
I tried defining and undefining NONSTANDARD_DIV_MOD in m.h. without any
visible difference. Running the test-prog divmod.c on a few targets always
returns 0.

[1] https://openwrt.org/
[2] https://patchwork.ozlabs.org/patch/518982/
[3] https://www.cis.upenn.edu/~bcpierce/unison/
[4] http://caml.inria.fr/mantis/view.php?id=5977

[-- Attachment #1.2: Type: text/html, Size: 3088 bytes --]

[-- Attachment #2: 001-crosscompile.patch --]
[-- Type: application/octet-stream, Size: 2074 bytes --]

--- ocaml-4.02.3.old/configure	2015-05-12 16:46:37.000000000 +0200
+++ ocaml-4.02.3/configure	2015-10-07 09:50:14.992000000 +0200
@@ -47,6 +47,8 @@
 no_naked_pointers=false
 TOOLPREF=""
 with_cfi=true
+arch64=false
+big_endian=1
 
 # Try to turn internationalization off, can cause config.guess to malfunction!
 unset LANG
@@ -154,6 +156,10 @@
         no_naked_pointers=true;;
     -no-cfi|--no-cfi)
         with_cfi=false;;
+    -arch64|--arch64)
+        arch64=true;;
+    -big-endian|--big-endian)
+        big_endian=0;;
     *) if echo "$1" | grep -q -e '^--\?[a-zA-Z0-9-]\+='; then
          err "configure expects arguments of the form '-prefix /foo/bar'," \
              "not '-prefix=/foo/bar' (note the '=')."
@@ -531,20 +537,13 @@
   esac
 else
   # For cross-compilation, runtest always fails: add special handling.
-  case "$target" in
-    i686-*-mingw*) inf "OK, this is a regular 32 bit architecture."
-                   echo "#undef ARCH_SIXTYFOUR" >> m.h
-                   set 4 4 4 2 8
-                   arch64=false;;
-    x86_64-*-mingw*) inf "Wow! A 64 bit architecture!"
-                     echo "#define ARCH_SIXTYFOUR" >> m.h
-                     set 4 4 8 2 8
-                     arch64=true;;
-    *) err "Since datatype sizes cannot be guessed when cross-compiling,\n" \
-           "a hardcoded list is used but your architecture isn't known yet.\n" \
-           "You need to determine the sizes yourself.\n" \
-           "Please submit a bug report in order to expand the list." ;;
-  esac
+  if $arch64; then
+    echo "#define ARCH_SIXTYFOUR" >> m.h
+    set 4 4 8 2 8
+  else
+    echo "#undef ARCH_SIXTYFOUR" >> m.h
+    set 4 4 4 2 8
+  fi
 fi
 
 if test $1 != 4 && test $2 != 4 && test $4 != 4; then
@@ -567,8 +566,11 @@
 
 # Determine endianness
 
-sh ./runtest endian.c
-case $? in
+if ! $cross_compiler; then
+  sh ./runtest endian.c
+  big_endian=$?
+fi
+case $big_endian in
   0) inf "This is a big-endian architecture."
      echo "#define ARCH_BIG_ENDIAN" >> m.h;;
   1) inf "This is a little-endian architecture."

[-- Attachment #3: host configure.log --]
[-- Type: application/octet-stream, Size: 3742 bytes --]

./configure  -prefix /home/stefan/openwrt/staging_dir/host -no-pthread -no-debugger -no-ocamldoc -no-graph -no-cfi
Configuring for host x86_64-unknown-linux-gnu ...
Configuring for target x86_64-unknown-linux-gnu ...
Using compiler gcc.
The C compiler is ANSI-compliant.
Checking the sizes of integers and pointers...
Wow! A 64 bit architecture!
This is a little-endian architecture.
Doubles can be word-aligned.
64-bit integers can be word-aligned.
Native division and modulus have round-towards-zero semantics, will use them.
ranlib found
#! appears to work in shell scripts.
POSIX signal handling found.
expm1(), log1p(), hypot(), copysign() found.
getrusage() found.
times() found.
termcap functions found (with libraries '-lcurses')
You have BSD sockets.
socklen_t is defined in <sys/socket.h>
inet_aton() found.
IPv6 is supported.
unistd.h found.
off_t is defined in <sys/types.h>
dirent.h found.
rewinddir() found.
lockf() found.
mkfifo() found.
getcwd() found.
getwd() found.
getpriority() found.
utime() found.
utimes() found.
dup2() found.
fchmod() found.
truncate() found.
sys/select.h found.
select() found.
symlink() found.
waitpid() found.
wait4() found.
getgroups() found.
setgroups() found.
initgroups() found.
POSIX termios found.
Asynchronous I/O are supported.
setitimer() found.
gethostname() found.
uname() found.
gettimeofday() found.
mktime() found.
setsid() found.
putenv() found.
setlocale() and <locale.h> found.
dlopen() found in -ldl.
Dynamic loading of shared libraries is supported.
mmap() found.
pwrite() found
stat() supports nanosecond precision.
gethostbyname_r() found (with 6 arguments).
gethostbyaddr_r() found (with 8 arguments).
mkstemp() found
nice() found
System stack overflow can be detected.
Bytecode threads library supported.
[WARNING] Cannot compile X11 program.
[WARNING] X11 not found, the "graph" library will not be supported.
[WARNING] BFD library not found, 'objinfo' will be unable to display info on .cmxs files.
CFI support: disabled by command-line option -no-cfi

** Configuration summary **

Directories where OCaml will be installed:
        binaries.................. /home/stefan/openwrt/staging_dir/host/bin
        standard library.......... /home/stefan/openwrt/staging_dir/host/lib/ocaml
        manual pages.............. /home/stefan/openwrt/staging_dir/host/man (with extension .1)
Configuration for the bytecode compiler:
        C compiler used........... gcc
        options for compiling..... -O -fno-defer-pop -Wall -D_FILE_OFFSET_BITS=64
        options for linking.......  -Wl,-E  -lm  -ldl -lcurses
        shared libraries are supported
        options for compiling..... -O -fPIC -O -fno-defer-pop -Wall -D_FILE_OFFSET_BITS=64
        command for building...... gcc -shared -o lib.so -Wl,-rpath,/a/path objs
Configuration for the native-code compiler:
        hardware architecture..... amd64
        OS variant................ linux
        C compiler used........... gcc
        options for compiling..... -O -Wall -D_FILE_OFFSET_BITS=64
        options for linking.......   -lm
        assembler ................ as
        preprocessed assembler ... gcc -c
        assembler supports CFI ... no
        with frame pointers....... no
        naked pointers forbidden.. no
        native dynlink ........... true
        profiling with gprof ..... supported
Source-level replay debugger: not supported
Additional libraries supported:
        unix str num dynlink bigarray threads
Configuration for the "num" library:
        target architecture ...... amd64 (asm level 1)
The "graph" library: not supported

** OCaml configuration completed successfully **

[-- Attachment #4: cross configure.log --]
[-- Type: application/octet-stream, Size: 4425 bytes --]

./configure  -prefix /home/stefan/openwrt/staging_dir/toolchain-mips_34kc_gcc-5.2.0_musl-1.1.11/ocaml -target-bindir /usr/bin -target mips-openwrt-linux-musl -cc "mips-openwrt-linux-musl-gcc -Os -pipe -mno-branch-likely -mips32r2 -mtune=34kc -fno-caller-saves -fhonour-copts -Wno-error=unused-but-set-variable -msoft-float" -as "mips-openwrt-linux-musl-as -Os -pipe -mno-branch-likely -mips32r2 -mtune=34kc -fno-caller-saves -fhonour-copts -Wno-error=unused-but-set-variable -msoft-float" -no-pthread -no-shared-libs -no-debugger -no-ocamldoc -no-graph -no-cfi -host x86_64-linux-gnu -big-endian ; fi )
Configuring for host x86_64-pc-linux-gnu ...
Configuring for target mips-openwrt-linux-musl ...
Using compiler mips-openwrt-linux-musl-gcc -Os -pipe -mno-branch-likely -mips32r2 -mtune=34kc -fno-caller-saves -fhonour-copts -Wno-error=unused-but-set-variable -msoft-float.
./tst: 1: ./tst: Syntax error: "(" unexpected
[WARNING] Unable to compile the test program.
 This failure is expected for cross-compilation:
 we will assume the C compiler is ANSI-compliant.
Checking the sizes of integers and pointers...
./tst: 1: ./tst:EL@P4: not found
./tst: 2: ./tst: Syntax error: "(" unexpected
This is a big-endian architecture.
Doubles must be doubleword-aligned.
64-bit integers must be doubleword-aligned.
PuTTY./tst: 1: ./tst: Syntax error: "(" unexpected
[WARNING] Something went wrong while checking native division and modulus please report it at http://http://caml.inria.fr/mantis/
ranlib found
#! appears to work in shell scripts.
POSIX signal handling found.
expm1(), log1p(), hypot(), copysign() found.
getrusage() found.
times() found.
termcap functions found (with libraries '-lcurses')
You have BSD sockets.
socklen_t is defined in <sys/socket.h>
inet_aton() found.
IPv6 is supported.
unistd.h found.
off_t is defined in <sys/types.h>
dirent.h found.
rewinddir() found.
lockf() found.
mkfifo() found.
getcwd() found.
getpriority() found.
utime() found.
utimes() found.
dup2() found.
fchmod() found.
truncate() found.
sys/select.h found.
select() found.
symlink() found.
waitpid() found.
wait4() found.
./tst: 1: ./tst: Syntax error: "(" unexpected
./tst: 1: ./tst:EL@¦4: not found
./tst: 2: ./tst: Syntax error: "(" unexpected
PuTTY./tst: 1: ./tst:EL@¦4: not found
./tst: 2: ./tst: Syntax error: "(" unexpected
PuTTYPOSIX termios found.
./tst: 1: ./tst: Syntax error: "(" unexpected
setitimer() found.
gethostname() found.
uname() found.
gettimeofday() found.
mktime() found.
setsid() found.
putenv() found.
setlocale() and <locale.h> found.
dlopen() found.
mmap() found.
pwrite() found
stat() supports nanosecond precision.
gethostbyname_r() found (with 6 arguments).
gethostbyaddr_r() found (with 8 arguments).
mkstemp() found
nice() found
Cannot detect system stack overflow.
Bytecode threads library supported.
[WARNING] Cannot compile X11 program.
[WARNING] X11 not found, the "graph" library will not be supported.
[WARNING] BFD library not found, 'objinfo' will be unable to display info on .cmxs files.
CFI support: disabled by command-line option -no-cfi

** Configuration summary **

Directories where OCaml will be installed:
        binaries.................. /home/stefan/openwrt/staging_dir/toolchain-mips_34kc_gcc-5.2.0_musl-1.1.11/ocaml/bin
        standard library.......... /home/stefan/openwrt/staging_dir/toolchain-mips_34kc_gcc-5.2.0_musl-1.1.11/ocaml/lib/ocaml
        manual pages.............. /home/stefan/openwrt/staging_dir/toolchain-mips_34kc_gcc-5.2.0_musl-1.1.11/ocaml/man (with extension .1)
Configuration for the bytecode compiler:
        C compiler used........... mips-openwrt-linux-musl-gcc -Os -pipe -mno-branch-likely -mips32r2 -mtune=34kc -fno-caller-saves -fhonour-copts -Wno-error=unused-but-set-variable -msoft-float
        options for compiling..... -O -fno-defer-pop -Wall -D_FILE_OFFSET_BITS=64
        options for linking.......   -lm  -lcurses
        shared libraries not supported
Configuration for the native-code compiler:
        (not supported on this platform)
Source-level replay debugger: not supported
Additional libraries supported:
        unix str num dynlink bigarray threads
Configuration for the "num" library:
        target architecture ...... generic (asm level 0)
The "graph" library: not supported

** OCaml configuration completed successfully **

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

end of thread, other threads:[~2015-10-13 16:22 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-10-08  9:43 [Caml-list] Broken 32 bit cross-compiler on 64bit host Stefan Hellermann
2015-10-08  9:52 ` Nicolas Ojeda Bar
2015-10-08 16:42   ` Gerd Stolpmann
2015-10-09 12:27     ` Stefan Hellermann
2015-10-13 16:22       ` Adrien Nader

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