Embedded Java with GCJ
Getting and Unpacking Crosstool
Crosstool is the creation of Dan Kegel. You can find out everything you want to know about crosstool by visiting kegel.com/crosstool. The page has a great quick start guide as well as complete documentation. This article used version 0.38 available at kegel.com/crosstool/crosstool-0.38.tar.gz.
On the crosstool home page, check out the buildlogs link (kegel.com/crosstool/crosstool-0.38/buildlogs) to see what combinations of glibc/gcc successfully build for your target architecture.
The first thing to compile with your newly minted cross compiler is the root filesystem. The root filesystem, in this case, is compliments of BusyBox. For the uninitiated, BusyBox is a single binary that encapsulates mini versions of the most popular UNIX utilities in a surpassingly small executable. Built for people that count bytes, BusyBox has hundreds of knobs to turn to create a filesystem with the utilities you need within your desired space constraints. For the purpose of this article, we change the BusyBox configuration so that it cross compiles, leaving size optimization as an exercise for the reader.
BusyBox is a mainstay of the embedded Linux world and is maintained by Erik Anderson. One way to get BusyBox is to download it at www.busybox.net/downloads/busybox-1.01.tar.bz2.
To create a BusyBox root filesystem, you need to invoke make menuconfig in the directory where BusyBox was untarred. The menuconfig program works just like the 2.4/2.6 menuconfig kernel configuration interface. Here's what you'll need to do to build the root filesystem.
First, select the build options. Check the Do you want to build BusyBox with a Cross Compiler? box. Fill in the prefix of the cross compiler in the input control that appears when you click this option, in this case, powerpc-750-linux-gnu-. The BusyBox build scripts concatenate the necessary tool name during compilation (gcc, ld and so on). Make sure that the compiler is on your $PATH.
Next, run make and install:
make make install
BusyBox puts the newly minted root filesystem at ./_install. You'll notice that BusyBox compiles in much less time than GCC.
Almost there! The root filesystem BusyBox creates does not contain any libraries. GCJ programs require some libraries and so does BusyBox, shown in Table 1.
Table 1. Libraries Required by GCJ and BusyBox
|ld.so.1||Dynamically linked file loader. Invoked when the program is run, loads required libraries and performs dynamic linking.|
|libdl.so.2||Helper functions for manipulating dynamic libraries.|
|libgcc_s.so.1||Defines interfaces for handling exceptions.|
|libgcj.so.6||The GCJ run-time library. Contains implementations of the standard Java library.|
|libm.so.6||Library of math functions.|
|libpthread.so.0||POSIX threads library.|
These libraries match those used by the cross compiler. In this case, the files are stored in the $RESULT_TOP/gcc-4.0.1-glibc-2.2.2/powerpc-750-linux-gnu/powerpc-750-linux-gnu/lib (not a typo!) directory. The easiest way to get them into the root filesystem is simply to copy them:
for f in ld.so.1 lib libdl.so.2 libgcc_s.so.1libgcj.so.6 libm.so.6 libpthread.so.0 ; do cp $RESULT_TOP/gcc-4.0.1-glibc-2.2.2/powerpc-750-linux-gnu/powerpc-750-linux-gnu/lib/$f <busybox install directory>/lib $RESULT_TOP/gcc-4.0.1-glibc-2.2.2/powerpc-750-linux-gnu/bin/power pc-750-linux-gnu-strip <busybox install directory>/lib/$f done
You also need to create a folder in the root filesystem, /proc, to use as a mountpoint for the proc filesystem. Keen eyes will notice that I'm not preserving the symlinks used to accommodate different versions of the libraries—that's a shortcut typical in embedded systems where library configuration won't change over the life of the device, unlike a desktop system. Running strip greatly reduces the amount of disk space required by the files, almost by 50%.
At this point, the root filesystem can be copied to the target system into the /tmp/bbox directory. To tell the system to use this as the root filesystem, start a terminal as root and execute the chroot command:
chroot /tmp/bbox /bin/ash
This command remaps the / mountpoint into /tmp/busybox and runs /bin/ash to get a terminal. Did it work? Congratulations! You've created a complete root filesystem for an embedded system from scratch. Pat yourself on the back.
GCJ also needs the proc filesystem mounted. After the chroot, you need to remount the proc filesystem into the current root filesystem by doing the following:
mkdir /proc mount -t proc none /proc
Although this root filesystem resides on a standard drive, the root filesystem deployed on a production embedded system wouldn't be much different. The only changes necessary would be creating inittab, so the board will run the right scripts at the start and add a /dev filesystem with the right device files for the target board.
Practical books for the most technical people on the planet. Newly available books include:
- Agile Product Development by Ted Schmidt
- Improve Business Processes with an Enterprise Job Scheduler by Mike Diehl
- Finding Your Way: Mapping Your Network to Improve Manageability by Bill Childers
- DIY Commerce Site by Reven Lerner
Plus many more.
- diff -u: What's New in Kernel Development
- What's New in 3D Printing, Part III: the Software
- Giving Silos Their Due
- Server Hardening
- February 2016 Issue of Linux Journal
- Controversy at the Linux Foundation
- 22 Years of Linux Journal on One DVD - Now Available
- Don't Burn Your Android Yet
- New Products