Hawkboard/Programming Guide -Linux(ARM)

From eLinux.org
< Hawkboard
Revision as of 17:51, 30 April 2010 by Alokprasad (Talk | contribs) (Static Vs Dynamic Linking)

Jump to: navigation, search

Linux Programmer’s Guide

Prerequitive

This guide assume ,basic knowlege of Kernel(uImage) and RootFS Please go through Beginners guide ,if you arent aware of this term

Introduction to Toolchain

Toolchain are Chain or set of programming tools for building an apps , it could include following :

  • Editor for writing program
  • Assembler
  • Compiler
  • Preprocessor
  • Linker loader
  • Debugger
  • Binutils etc.


Toolchain Type

a)Native toolchain

Native toolchains are tools which makes binaries for the environment where toolchain itself is being run.i.e Host and target are same platform. This type of Compilation process is called Native compilation .Meaning that the program is compiled on same platform where it has to be run. Similar to normal PC based program ,they are compiled and run on same machine.

It is much easier to program using this method as it doesn’t involve any Makefile changes.

Earlier embedded devices had very less memory and space and also limited supports of Input-output peripherals, hence Native compilation was not possible or very difficult. But with the advent of powerful embedded ARM processor its now possible to make application on the platform itself. Same can be done in Hawk board too. But still compilation will be slower as compare to that in x86 machine.

e.g. Normal GCC Toolchain in x86 Linux machine is a Native toolchain.

b)Croos Toolchain

Toolchain which are used to compile the application that will run of different machine(architecture) .This is usually used for embedded apps (e.g ARM programming) You program in x86 environment and run in ARM Board. This type of compiling is called cross compiling

Since native compilation is either not possible or very slow ,Cross compilation present a better alternative for faster development .Only drawback is to properly set build environment and Makefiles.

c)Hybrid Toolchain

It similar to native toolchain,but infact toolchain run in virtualised environment on Host.Simple example is QEMU -ARM Running on X86 Linux & you are developing application on this Virtual environment.

This has advantage of both Native and Cross compilation.Speed of cross compilation & Ease of Native development.

Various solution exists, One is scratchbox ,another is ChrootEABI feature in Ubuntu.

Native Toolchain & Compilation

There are various ways solution to compile the application natively on hawkboard.

1. Using Ubuntu Rootfs

Rootstock script in Ubuntu provides a easy way of making Root Filesystem give gcc,vi,g++ as seed parameter to rootstock command and you will have a GNU Toolchain inside the rootfs.

Then write/compile/run as you do for x86 program.

This needs rootstock to installed on Ubuntu (9.10)

 apt-get install rootstock
 sudo rootstock --fqdn localhost.com --login <user> --password <password> --imagesize 2G --dist jaunty --serial ttyS2 --seed    ubuntu-minimal,gcc,vim,g++,openssh,subversion,apt

<user> is user name <password> is password that will be required once this rootfs is booted on hawkboard

imagesize is max size allocated to rootfs.you can keep it same as ur USB Drive /SD card .

Seed parameter tells which packages to be included in rootfs.include atleast gcc,vim (any editor),build-essential packages.

This will take couple of hours to completely download packages you have mentioned and to make a rootfs.

After completion of command it will create some file such as

armel-rootfs-2010xxxxxxxx.tgz 

This is compressed rootfilesytem extract it on a SD card or NFS Folder and boot using one of the method described in beginners/guide

Set proper user group and rights of File

sudo tar xfp armel-rootfs-**********.tgz
chown -R root.root *
chmod -R 777 *
chmod 440  etc/sudoers
chown root:root usr/bin/sudo
chmod 4111 usr/bin/sudo

2.Angstrom Root FS

Select Hawkboard from the List http://www.angstrom-distribution.org/narcissus/

Angstrom Distribution provides following dev packages

  1. Python
  2. Perl
  3. Mono (C#, .NET)
  4. Toolchain
  5. OProfile
  6. GDB
  7. Busybox replacements

Select atleast Toolchain to get GCC.

3. Fedora ARM RootFS

Fedora like ubuntu's rootstock provide a script rfsbuild that generates rootfs you can include gcc in package list to include it as native toolchain.

More info http://fedoraproject.org/wiki/Architectures/ARM/RfsBuild

4. Rootfs without GCC

Even though you havent selected gcc as packages in any of the above distribution you can use the native gcc toolchain from Impactlinux. Download from here

 http://impactlinux.com/fwl/downloads/binaries/native-compiler-armv5l.tar.bz2

Extract and properly export the path of native-compiler-armv5l/bin and you can compile the apps on hawkboard. use [Static linking] instead of dynamic linking .

 gcc hello-world.c -static 

This toolchain uses uClibc instead of standard glibc as library which better suited for embedded environment.

Cross Toolchain & Compilation

There are Lots of readily available tool chains that you can extract and start compiling apps on x86 architecture and finally load and run on hawkboard.

1.Codesourcery Toolchain

This is the most common toolchain used for OMAP.Codesourcery provides both a free version and licensed release toolchain. Free version can be downloaded from the link below


2.ImpactLinux Toolchain

3.Cross Toolchain by Fedora Arm Project

If you are using a Fedora as host environment than easiest way is to use Fedora ARM Toolchain. Fedora provides arm toolchain packages for i386 and x86_64 that are built from the same sources as the Fedora native toolchain packages (binutils, gcc, gdb, glibc.) To install this toolchain on your Fedora machine, do:

# cd /etc/yum.repos.d/
# wget http://fedora-arm.wantstofly.org/cross/cross.repo
# yum install armv5tel-redhat-linux-gnueabi-gcc

This will install everything necessary to run the C compiler and cross-build ARM libraries and binaries that are entirely binary compatible with the native Fedora/ARM libraries and binaries.

Cross-compiling ARM userland binaries

To cross-compile the canonical Hello World example for ARM, do:

 $ armv5tel-redhat-linux-gnueabi-gcc -Wall -o hello hello.c

Cross-compiling ARM kernels

You can also use the cross-toolchain to cross-build an ARM Linux kernel, by changing these two lines in the top-level Makefile:

 ARCH            ?= $(SUBARCH)
 CROSS_COMPILE   ?=

to:

 ARCH            ?= arm
 CROSS_COMPILE   ?= armv5tel-redhat-linux-gnueabi-

and then building your kernel as usual.

4. Custom Toolchain Crosstool-NG & Scratchbox

Hybrid Toolchain & Compilation

There is module in ubuntu that allows binaries of other machine(ARM,MIPS etc) to allowed to run along with X86 binaries

sudo apt-get update
sudo apt-get install qemu-arm-static
sudo build-arm-chroot jaunty eabi-chroot

This creates a chroot environment in folder /home/<user>/eabi-chroot

ogra@osiris:~$ sudo chroot eabi-chroot root@osiris:/# uname -m armv5tel

Chroot-eabi2.JPG Chroot-eabi1.JPG

Now you can build/test packages etc, the chroot works like any other x86 chroot on your system as long as you have the qemu-arm-static package installed with the advantage that you can make use of all resources your host system provides (CPU speed, RAM, diskspeed etc)

Appendix

C libraries - uClibc & GlibC

A C library is precompiled object file of Standard C funtions.

GNU Toolchain i.e GCC uses Glibc as Standard C library.

While most of the Embedded Devices uses Size optimize Library uClibc. Hence Statically Linked binaries using uClibc are much smaller than Glibc Linked Binary

Below shows a comparison of Hello-World Program Statically combined on two Libraries

  • Left uses Codesoucery Toolchain that uses Glibc (634kB)
  • Right uses Impactlinux Toolchain that uses uClibc (22kB)


Glibc.JPG UClibc.JPG

Static Vs Dynamic Linking

Static dyanamic.JPG