Cross compiling kernel for Raspberry Pi on Fedora 17 – Part 2

There are multiple ways to install a cross compiler on Fedora 17. The simplest way was described in Part 1. The problem is that the available cross-build binary utilities for ARM (sudo yum info gcc-arm-linux-gnu) does not support cross-building of user space programs. A slightly more complicated option is to use build frameworks like OpenEmbedded or crosstool-ng.

Install crosstool-ng
By the time of writing this post the latest stable version of crosstool-ng is 1.16.0. Installation of a stable version is quite straightforward and it is very clearly described in “Download and usage section” of crosstool-ng.org. For your reference I copy it here with slight modifications.

su - #change to root
#Install dependencies
yum install bison flex gperf libtool texinfo gcc gcc-c++ gmp-devel
#Download and install crosstool-ng
cd /opt
wget http://crosstool-ng.org/download/crosstool-ng/crosstool-ng-1.16.0.tar.bz2
bunzip2 crosstool-ng-1.16.0.tar.bz2
tar xf crosstool-ng-1.16.0.tar
cd crosstool-ng-1.16.0
#The --prefix parameter shall be set to the installation directory 
./configure --prefix=/opt/crosstool-ng-1.16.0
make
make install
#For auto-completion, do not forget to install 'ct-ng.comp' into your bash completion directory
cp ct-ng.comp /etc/bash_completion.d/    


The bash_completion will automatically pick up the auto completion parameters after restart. You can read more about autocopletion feature of bash here and here.

Edit the .bash_profile of root user and also your user and add the /opt/crosstool-ng-1.16.0/bin to PATH in order make the crostool-ng comands available from shell.

PATH=$PATH:/opt/crosstool-ng-1.16.0/bin

Configure the toolchain
The crosstool-ng has been installed and ready to build a toolchain for the Raspberry Pi (or other environment)

su -
mkdir /opt/crosstool-ng-1.16.0-config 
cd /opt/crosstool-ng-1.16.0-config
ct-ng menuconfig


The last command will pop up a kernel-like menuconfig configuration interface where the following changes needs to be made.

  • Paths and misc options
    • Enable “Try features marked as EXPERIMENTAL”
    • Set the “Prefix directory” from “${HOME}/x-tools/${CT_TARGET}” to “/opt/crosstool-ng-1.16.0-tools/${CT_TARGET}”, please note that the /opt/crosstool-ng-1.16.0-config directory will contain the configuration, downloaded tools, temporary build files and the /opt/crosstool-ng-1.16.0-tools will contain your actual toolchain.
  • Toolchain options
    • Tuple’s version string is set to “unknown” by default, if would like to change it please see the “Note” in the bottom of this post
  • Target options
    • Be sure that the “Target Architecture” is set to “arm”
    • Be sure that “Little Endian” and “32bit” are selected
    • Be sure that “Floating point” is set to “hardware (FPU)”
    • Be sure that “Use EABI” is selected
  • Operating system
    • Set “Target OS” to Linux
  • C compiler
    • Enable “Show Linaro versions”
    • The “gcc version” should automatically change to latest linaro version, if not then set it manually. Currently the latest is “linaro-4.7-2012.07”
    • Enable “C++” in order to have C++ compiler
    • Disable “Link libstdc++ statically into gcc binary” otherwise you will get the the following error “[ERROR] Static linking impossible on the host system ‘x86_64-build_unknown-linux-gnu'”
    • Disable “GRAPHITE loop optimisations” otherwise you will get the the following error “Installing PPL for host [ERROR] configure: error: Cannot find GMP version 4.1.3 or higher.”

Exit from the menuconfig save the changes and build it still as root

ct-ng build


The build takes a while, but after it is done the toolchain should appear in the /opt/crosstool-ng-1.16.0-tools directory.

Switch from root to your regular user and append the /opt/crostool-ng-1.16.0-tools/arm-unknown-linux-gnueabi/bin to PATH variable in .bash_profile to make the cross compiler binaries available in shell

PATH=$PATH:/opt/crosstool-ng-1.16.0/bin:/opt/crosstool-ng-1.16.0-tools/arm-unknown-linux-gnueabi/bin

Now you have a full featured cross-compile environment and you can build user space programs or you can compile kernel as described in Part 1. Of course in the CROSS_COMPILE parameter you need to specify the /opt/crosstool-ng-1.16.0-tools/arm-unknown-linux-gnueabi/bin/arm-unknown-linux-gnueabi- instead of /usr/bin/arm-linux-gnu-. For example:

make ARCH=arm CROSS_COMPILE=/opt/crosstool-ng-1.16.0-tools/arm-unknown-linux-gnueabi/bin/arm-unknown-linux-gnueabi- oldconfig

After you have executed every kernel compilation step described in the Part 1 you should see

pi@raspberrypi ~ $ uname -a
Linux raspberrypi 3.2.27-crosstool-ng #1 PREEMPT Tue Oct 2 20:28:03 CEST 2012 armv6l GNU/Linux

To test the cross-building capability of user space programs just creat a little hello world as hello.c

#include <stdio.h>

int main () {
  printf("Hello Cross Compiled Pi\n");
  return 0;
}

Compile it and check the result of compilation with file command

~ $ arm-unknown-linux-gnueabi-gcc -o arm-hello hello.c
~ $ file arm-hello
arm-hello: ELF 32-bit LSB executable, ARM, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 3.5.0, not stripped


After transfering it to Raspberry pi with scp

pi@raspberrypi ~ $ ./arm-hello
Hello Cross Compiled Pi

Note: The “arm-unknown-linux-gnueabi” name is crated based on form “arch-vendor-kernel-system”. You can set the vendor to whatever you think fits better. For that you can navigate to “Toolchain options” -> “Tuple’s vendor string” and change it from “unknown” to e.g. “rpi” and build the whole tool-chain as described above, after this modification your tools will be named like “arm-rpi-linux-gnueabi”.

Advertisements

7 Responses to Cross compiling kernel for Raspberry Pi on Fedora 17 – Part 2

  1. Bosstiger says:

    Reblogged this on Gigable – Tech Blog.

  2. Luca Paganotti says:

    Hi, I’ve setup correctly crosstool-ng-1.17.0 on my fedora 17 box, after ct-ng menuconfig and then ct-ng build I have my arm toolchain under /opt/crosstool-ng-1.17.0-tools folder.
    I wrote the hello.c file and then built it, put it on my raspi try to run it but I got this error:

    pi@raspberrypi:~/dev$ ./hello
    -bash: ./hello: No such file or directory

    file ./hello gives:

    pi@raspberrypi:~/dev$ file ./hello
    ./hello: ELF 32-bit LSB executable, ARM, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 3.6.3, not stripped

    ldd ./hello gives:

    pi@raspberrypi:~/dev$ ldd ./hello
    libc.so.6 => /lib/libc.so.6 (0x4018c000)
    /lib/ld-linux-armhf.so.3 => /lib/ld-linux.so.3 (0x4001e000)

    What am I missing?

  3. akanto says:

    Hi,

    That is strange. If there is some compilation or loading problem than I would expect a some *.so cannot be preloaded or segmentation fault error message, but not a bash related error message.

    I have upgraded to ct 1.17.0 (on Fedora 18) and the hello world just compiles fine

    pi@raspberrypi ~ $ ldd -r -v hello
    /usr/lib/arm-linux-gnueabihf/libcofi_rpi.so (0x4017f000)
    libc.so.6 => /lib/arm-linux-gnueabihf/libc.so.6 (0x40188000)
    /lib/ld-linux-armhf.so.3 (0x4005d000)

    Version information:
    ./hello:
    libc.so.6 (GLIBC_2.4) => /lib/arm-linux-gnueabihf/libc.so.6
    /usr/lib/arm-linux-gnueabihf/libcofi_rpi.so:
    libc.so.6 (GLIBC_2.4) => /lib/arm-linux-gnueabihf/libc.so.6
    /lib/arm-linux-gnueabihf/libc.so.6:
    ld-linux-armhf.so.3 (GLIBC_2.4) => /lib/ld-linux-armhf.so.3
    ld-linux-armhf.so.3 (GLIBC_PRIVATE) => /lib/ld-linux-armhf.so.3

    pi@raspberrypi ~ $ file hello
    hello: ELF 32-bit LSB executable, ARM, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 3.6.3, not stripped

    pi@raspberrypi ~ $ ./hello
    Hello Cross Compiled Pi

    • Luca Paganotti says:

      Hi, thanks for your answer,
      could the problem be that I’m not on a raspbian?
      Issuing lsb_release -a gives me:
      pi@raspberrypi:/$ lsb_release -a
      No LSB modules are available.
      Distributor ID: Debian
      Description: Debian GNU/Linux 6.0.6 (squeeze)
      Release: 6.0.6
      Codename: squeeze

      In the meantime I’ve tried to re-(ct-ng build) a couple of times changing some parameter in the menuconfig(for example the binutils version back to 2.21.1a and the gcc version to linaro-4.6-2012.04 (EXPERIMENTAL), but … I get the same results.

      I’ve seen some differences between my ldd issue and yours, for example I’ve no libcofi_rpi reference and no *arm-linux-gnueabihf* reference.

      Thanks again for your help

  4. akanto says:

    Hi,

    I am on Raspbian. If you are using the Soft-float Debian then probably it would worth to try to change the Target Option->Floating Point to software.This is more or less a guess from my side.

    You can try also to quickly install a Rasbian image and try to execute the file just to be sure that the crostool-ng produces a proper binary file.

  5. Luca Paganotti says:

    Hi akanto,

    I get it just now, I’ve configured crosstool-ng for soft-float and rebuilt it, now I get the correct output on screen, thank you for your help … I’m very sorry (embarassed) for the noise. I think I should go to raspbian or arch on the raspi 512 I will receive in the next days … thanks again

  6. akanto says:

    Hi Luca, I am glad to hear that it worked.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: