Compiling for CRIS HOWTO

This document describes how to use the gcc-cris compiler to cross-compile c programs for the CRIS architecture. This document also deals with how to get the executable running on the target system. For information on how to include an application in the software distribution read the apps howto document.

Requirements

To be able to compile the software for the CRIS architecture the gcc-cris compiler has to be properly installed. Have a look at compiler install for instructions on how to install the compiler. Note that there are two different compilers, one for crisv10 (ETRAX 100LX based designs) and one for crisv32 (ETRAX FS based designs).

Text within angle brackets ”<>” should be read as a placeholder for something else, for example, linux-<kernel version> could be linux-2.6.19.

Introduction

Creating an Linux executable aimed to run on an ETRAX based product involves cross compiling and linking the source for the CRIS architecture. Then the executable must be transfered to the target system. Let us break this down in a few few steps. We start with writing a simple application for the host system (a Linux PC). Then we port it to the target architecture (the ETRAX based system). Then we build the application and finally we discuss how to get the application into the target system.

Lets start!

"Hello World!" program for host

It is now time to write a simple test program. First we need a directory for our new application. Let us call our application hello which makes it natural to create a directory named hello:

mkdir ~/hello
cd ~/hello

Copy the source code below and save it to a file named hello.c in the hello directory that we just created:

#include <stdio.h>
int main(int argc, char *argv[]) {
  printf("Hello World!\n");
  return 0;
}

Now it is time to compile it for the host system:

gcc -o hello_host hello.c

This will create an executable named hello_host for your host in the current directory. For more information about gcc run man gcc on your host. Make sure that it works as expected:

./hello_host

The output should be:

Hello World!

"Hello World!" for target

Now that we know that the code work for host, let us proceed and make an executable for the target system.

Use the right compiler

As you probably know there are two versions of the CRIS architecture, crisv10 and crisv32. crisv10 is used for the ETRAX 100LX (and of course for the ETRAX 100LX MCM) and crisv32 for the ETRAX FS. The cris compiler is usually installed in /usr/local/cris. To make sure that you are using the right one run:

/<path to compiler>/bin/gcc-cris --version

You should get something like this for the crisv10 compiler:

cris-axis-elf-gcc (GCC) 3.2.1 Axis release R63/1.63
Copyright (C) 2002 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

And something like this for the crisv32 compiler:

cris-axis-elf-gcc (GCC) 3.2.1 Axis release R63/1.63-v32
Copyright (C) 2002 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Add the path to the right compiler for your target system to the PATH environment variable on the host system.

export PATH=$PATH:/<path to compiler>/bin

Make sure that you can use the compiler and that it is the right one!

gcc-cris --version

As you can see above the default output format for gcc-cris is elf (look at the compiler name cris-axis-elf-gcc). To compile for our Linux target platform we must provide a flag:

-mlinux

If you are about to compile for v32 architecture you will have to add another flag. Simply put; you will have to specify that you wish to compile for the v32 architecture. Consequently, add this flag if you are compiling for v32:

-march=v32

Providing these flags make the gcc-cris script select the correct compiler for you. For crisv10 that would be cris-axis-linux-gnu-gcc and for crisv32 that would be crisv32-axis-linux-gnu-gcc. Consequently you could either use the gcc-cris script with the appropriate flags or the compiler executable directly.

C library

Almost all C programs make use of functions provided by the standard C library. The software distribution, used to to produce firmware images for ETRAX based products, comes with two different standard C libraries; glibc and uClibc. These two are API1) compatible with each other but not ABI2) compatible. Consequently you have to know what standard C library your target system is using or link the program statically (which will create a huge binary). Otherwise it is likely that the only thing you will see is a not found error.

To find out which standard C library your target system is using log in on the target platform and have a look in the /lib directory. If you can see a file named libc.so.6 your product is using glibc. If you can see a file named ld-uClibc-<version>.so your product is using uClibc. If your target platform use glibc continue to the compile the program section.

uClibc

If your target system use uClibc you will have to have the software distribution installed since uClibc is distributed as a part of it. Please observe that the uClibc does not provide a stable ABI between different versions, consequently make sure that you are using the same version when compiling the application as used on the target platform.

To compile a program that use the uClibc standard C library the compiler needs several flags. The most straightforward way of providing these is by running the gcc_cris script found in the software distribution. This script digs out these flags and run gcc-cris. The gcc_cris script is located in the tools/build/bin directory which is added to the path when sourcing the init_env file found in the software distribution root directory.

To compile a program using the gcc_cris wrapper script run:

gcc_cris -muclibc=<path> <other flags> <program to compile>

Where:

  • <path> is the the path to the root of the build tree. For example, if the SDK is installed in /home/karljope/devboard-R2_10 and is configured for crisv10 with uClibc the <path> would be /home/karljope/devboard-R2_10/target/cris-axis-linux-gnuuclibc.
  • <other flags> could be -mlinux and -march=v32

Compile the program

Now when you know what compiler, flags, and standard C library to use we can compile the hello application.

For a glibc target system run:

gcc-cris <flags> -o hello_target hello.c

For a uClibc target system run:

gcc_cris -muclibc=<path> <flags> -o hello_target hello.c

This will create an executable named hello_target for your target system. Since this program can not be executed on your host you will have to upload it to the target system to verify if it works or not.

Getting the executable into the target system

So far in this guide we have compiled the “Hello World” application for the target system. Now it is time to get the application into the target system. This can be achieved by uploading the executable with for example FTP or SCP. The target must, of course, support the transfer method and the executable can only be written to a writable file system.

To upload a executable file to the target using FTP do the following:

Start your FTP client and connect to the target system. The default root password is pass.

$ftp <ip adress>

Make sure you transfer the file in binary mode (as opposed to ASCII mode).

ftp> binary  

The executable can either be placed on the writeable flash partition (/mnt/flash) or on the tmpfs partition (/tmp) in RAM. Obviously, if you place the executable in RAM it will not be there the next time you reboot the system. Go to the /mnt/flash directory

ftp> cd /mnt/flash

Upload the file.

put <file name>

Since transferring an file using FTP will not make the file executable you must set the execute bits manually. You could use the chmod command from the FTP client.

ftp> chmod 755 <file name>

Then disconnect

ftp> quit

Test the executable on the target system

Now when the executable is uploaded to the target platform we can verify if it work as expected. Connect to the target system via telnet, ssh, or the serial terminal (debug port).

When logged in try the “Hello World!” application.

cd /mnt/flash
./hello

The output should be:

Hello World!

Discussion

In this guide it has been shown how to compile a very simple program for the CRIS architecture and how to get it into the target system to verify if it works or not. Using this method can be considered “safe” since it does not involve any changes of the target system files. On the other hand this method has some limitations. For example, a more complex application might need additional libraries that are not present on the target system. And since the root (/) file system is mounted read only additional libraries can not be easily added. Have a look here FIXME for circumventing this.

Instead of using this method when writing your own applications we recommend the method suggested in the apps howto guide.

1) Application programming interface
2) Application binary interface
 
axis/compiling_for_cris_howto.txt · Last modified: 2007/03/15 20:23 by kj
 
All text is available under the terms of the GNU Free Documentation License (see Copyrights for details).