Cross-compiling ocaml apps for ARM

2019-04-23 03:15发布

问题:

I'm cross-compiling a touchscreen driver, which comes with an ocaml calibration application.

I'm trying to compile the driver and the application for ARM, in particular, the Beagleboard, running Angström.

It goes like this:

^_^[raziel@Bebop zytouch-driver-20081121]$ source /usr/local/angstrom/arm/environment-setup 
^_^[raziel@Bebop zytouch-driver-20081121]$ make CC=arm-angstrom-linux-gnueabi-gcc 
arm-angstrom-linux-gnueabi-gcc -std=gnu99 -g -O2 -Wall -Wextra -Werror -Wstrict-prototypes -Wmissing-prototypes   -c -o daemon/config.o daemon/config.c
arm-angstrom-linux-gnueabi-gcc -std=gnu99 -g -O2 -Wall -Wextra -Werror -Wstrict-prototypes -Wmissing-prototypes   -c -o daemon/util.o daemon/util.c
arm-angstrom-linux-gnueabi-gcc -std=gnu99 -g -O2 -Wall -Wextra -Werror -Wstrict-prototypes -Wmissing-prototypes   -c -o daemon/debug.o daemon/debug.c
arm-angstrom-linux-gnueabi-gcc -std=gnu99 -g -O2 -Wall -Wextra -Werror -Wstrict-prototypes -Wmissing-prototypes   -c -o daemon/zytouch_usb.o daemon/zytouch_usb.c
arm-angstrom-linux-gnueabi-gcc -std=gnu99 -g -O2 -Wall -Wextra -Werror -Wstrict-prototypes -Wmissing-prototypes   -c -o daemon/zytouchd.o daemon/zytouchd.c
arm-angstrom-linux-gnueabi-gcc daemon/config.o daemon/util.o daemon/debug.o daemon/zytouch_usb.o daemon/zytouchd.o -lX11 -lXtst -lusb -lm -o zytouch-daemon
ocamlopt -cc arm-angstrom-linux-gnueabi-gcc -cclib -lcairo -cclib -lextlib -cclib -llablgtk2 -warn-error A -c -I +lablgtk2 -I +cairo -I +extlib -I calibrate calibrate/version.ml -o calibrate/version.cmx
ocamlopt -cc arm-angstrom-linux-gnueabi-gcc -cclib -lcairo -cclib -lextlib -cclib -llablgtk2 -warn-error A -c -I +lablgtk2 -I +cairo -I +extlib -I calibrate calibrate/util.ml -o calibrate/util.cmx
ocamlopt -cc arm-angstrom-linux-gnueabi-gcc -cclib -lcairo -cclib -lextlib -cclib -llablgtk2 -warn-error A -c -I +lablgtk2 -I +cairo -I +extlib -I calibrate calibrate/config.ml -o calibrate/config.cmx
ocamlopt -cc arm-angstrom-linux-gnueabi-gcc -cclib -lcairo -cclib -lextlib -cclib -llablgtk2 -warn-error A -c -I +lablgtk2 -I +cairo -I +extlib -I calibrate calibrate/orientation.ml -o calibrate/orientation.cmx
ocamlopt -cc arm-angstrom-linux-gnueabi-gcc -cclib -lcairo -cclib -lextlib -cclib -llablgtk2 -warn-error A -c -I +lablgtk2 -I +cairo -I +extlib -I calibrate calibrate/tscalibrate.ml -o calibrate/tscalibrate.cmx
ocamlopt -cc arm-angstrom-linux-gnueabi-gcc -cclib -lcairo -cclib -lextlib -cclib -llablgtk2 -warn-error A -cc arm-angstrom-linux-gnueabi-gcc -cclib -lcairo -cclib -lextlib -cclib -llablgtk2 -warn-error A -I +lablgtk2 -I +cairo -I +extlib -I calibrate extLib.cmxa str.cmxa unix.cmxa lablgtk.cmxa cairo_lablgtk.cmxa cairo.cmxa gtkInit.cmx calibrate/version.cmx calibrate/util.cmx calibrate/config.cmx calibrate/orientation.cmx calibrate/tscalibrate.cmx -o zytouch-calibrate
/usr/local/angstrom/arm/lib/gcc/arm-angstrom-linux-gnueabi/4.3.3/../../../../arm-angstrom-linux-gnueabi/bin/ld: /tmp/camlstartup71ef32.o: Relocations in generic ELF (EM: 3)
/usr/local/angstrom/arm/lib/gcc/arm-angstrom-linux-gnueabi/4.3.3/../../../../arm-angstrom-linux-gnueabi/bin/ld: /tmp/camlstartup71ef32.o: Relocations in generic ELF (EM: 3)
/tmp/camlstartup71ef32.o: could not read symbols: File in wrong format
collect2: ld returned 1 exit status
File "caml_startup", line 1, characters 0-1:
Error: Error during linking
make: *** [zytouch-calibrate] Error 2

I'm stuck in that Relocations in generic ELF (EM: 3) error.

The Makefile looks like this:

(...)
OFLAGS = -cc ${CC} -cclib -lcairo -cclib -lextlib -cclib -llablgtk2 -warn-error A
OCAMLOPT = ocamlopt $(OFLAGS)

OCAML_INC =  -I +lablgtk2 -I +cairo -I +extlib -I calibrate
OCAML_LIBS = $(OCAML_INC) extLib.cmxa str.cmxa unix.cmxa lablgtk.cmxa cairo_lablgtk.cmxa cairo.cmxa gtkInit.cmx

(...)    
config.cmx: util.cmx
orientation.cmx: config.cmx
tscalibrate.cmx: version.cmx util.cmx orientation.cmx config.cmx

%.cmx : %.ml
        $(OCAMLOPT) -c $(OCAML_INC) $< -o $@

%.mli : %.ml
        $(OCAMLC) -i $(OCAML_INC) $+

As /usr/local/angstrom/arm/environment-setup leaves /usr/local/angstrom/arm/bin as the first element of my PATH, I tried to replace some programs with the ARM versions

sudo ln -s /usr/local/angstrom/arm/bin/arm-angstrom-linux-gnueabi-ar /usr/local/angstrom/arm/bin/ar
sudo ln -s /usr/local/angstrom/arm/bin/arm-angstrom-linux-gnueabi-as /usr/local/angstrom/arm/bin/as
sudo ln -s /usr/local/angstrom/arm/bin/arm-angstrom-linux-gnueabi-ld /usr/local/angstrom/arm/bin/ld

However, after replacing as, the modules don't even compile

ocamlopt -cc arm-angstrom-linux-gnueabi-gcc -cclib -lcairo -cclib -lextlib -cclib -llablgtk2 -warn-error A -c -I +lablgtk2 -I +cairo -I +extlib -I calibrate calibrate/version.ml -o calibrate/version.cmx
/tmp/camlasm41bb77.s: Assembler messages:
/tmp/camlasm41bb77.s:31: Error: alignment too large: 15 assumed
/tmp/camlasm41bb77.s:35: Error: bad instruction `movl $camlVersion__3,%eax'
/tmp/camlasm41bb77.s:36: Error: bad instruction `movl %eax,camlVersion'
/tmp/camlasm41bb77.s:37: Error: bad instruction `movl $camlVersion__2,%eax'
/tmp/camlasm41bb77.s:38: Error: bad instruction `movl %eax,camlVersion+4'
/tmp/camlasm41bb77.s:39: Error: bad instruction `movl $camlVersion__1,%eax'
/tmp/camlasm41bb77.s:40: Error: bad instruction `movl %eax,camlVersion+8'
/tmp/camlasm41bb77.s:41: Error: bad instruction `movl $1,%eax'
/tmp/camlasm41bb77.s:42: Error: bad instruction `ret'
/tmp/camlasm41bb77.s:43: Error: unrecognized symbol type ""
File "calibrate/version.ml", line 1, characters 0-1:
Error: Assembler error, input left in file /tmp/camlasm41bb77.s
make: *** [calibrate/version.cmx] Error 2

I'm pretty sure it must be a very stupid error, but I can't find documentation on how to do this properly. Does anyone know what could be failing?

The source code of the driver I'm trying to build can be found here.

回答1:

ocaml currently doesn't support cross-compilation, and passing -cc option will not make it magically cross-compile to arm. There are some patches floating around that make it possible, but nothing official. I remember using patched ocamlopt and it worked ok for simple programs. But in this case you will also need to cross-compile all dependent libraries and this can be quite a bit of a task.

I think your best solution is to either :

  • build natively in arm qemu (this is quite easy, there are prebuilt debian images available)

  • build a bytecode binary of calibration application (bytecode is portable between architectures but requires the same version of ocaml installed on the target) and install required stub libraries (the ones that contain C code for bindings to gtk,cairo,etc) on arm (built natively or from packages)



回答2:

It looks like you have not replaced all of the toolchain with the cross-compilation toolchain. movl %eax,camlVersion is a typical x86 instruction and not something you would see in ARM code. I usually get your errors when I forget to do a clean between building code for different architectures.