Difference between revisions of "EBC Exercise 08a Cross-Compiling"

From eLinux.org
Jump to: navigation, search
m (Step 4 - Start building)
m (Cross-compiling Hello World: Removed some items from apt-get install)
(167 intermediate revisions by 21 users not shown)
Line 1: Line 1:
'''This page isn't finished yet, please wait before doing it.'''
+
[[Category:ECE497]]
 +
[[Category: BeagleBoard]]
 +
{{YoderHead}}
  
This class is about developing software for embedded Linux. The eLinux site [http://elinux.org/Main_Page] is a good source for embedded Linux in general. There are many on going embedded efforts going on many platforms. Poke around the site a while to get a feel for what's happening.
+
This class is about developing software for embedded Linux. So far we have been doing all of our development on the Beagle. This works well for small (and not so small) programs. However, we are now moving into kernel development and that's best done on a more powerful host computer. In [[EBC Exercise 08 Installing Development Tools]] you learned how to download and install the cross-compilers and the source for kernel and u-boot. Now we'll use those tools.
  
We are going to use the Ångström Distribution [http://www.angstrom-distribution.org].  It's available many platforms.  Look around the site, you may recognize some of them.
+
== Cross-compiling Hello World ==
 +
This shows how to cross-compile with compiler loaded with
  
Instructions for building Ångström are given here [http://www.angstrom-distribution.org/building-angstrom]; however I'm going to present a Beagle-tuned version of those instructions on this page.
+
host$ '''sudo apt-get install gcc-arm-linux-gnueabi'''
  
== Step 1 - get Open Embedded metadata ==
+
Listing 2-4 on page 29 of the text is an embedded version of Hello World. If you've set up your git repository you will find it in '''helloWorld.c''' when you do a''' git pull'''.  Compile and run it on your host to be sure it works.
  
First install <code>git</code> by running the following on your host computer.
+
host$ '''gcc helloWorld.c'''
<pre>
+
host$ '''file a.out'''
sudo apt-get install git-core
+
a.out: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV),
</pre>
+
  dynamically linked (uses shared libs), for GNU/Linux 2.6.24,
 +
  BuildID[sha1]=0x357e34e90f7c32d414368d69cc06d0aed59acf1c, not stripped
 +
host$ '''./a.out'''
 +
Hello, World! Main is executing at 0x400524
 +
This address (0x7fff8260bdf8) is in our stack frame
 +
This address (0x601038) is in our bss section
 +
This address (0x601020) is in our data section
  
Then run the following to load the meta data.
+
Now that you know it's working, let's cross compile it. First set the paths to find the cross-compiler. Put the following in a file, call it '''~/crossCompileEnv.sh'''.
<pre>
 
export OETREE="${HOME}/oe"
 
mkdir -p ${OETREE} && cd ${OETREE}
 
git clone git://git.openembedded.org/openembedded.git openembedded
 
cd openembedded
 
git checkout origin/stable/2009 -b stable/2009
 
</pre>
 
The first git transfers some 336,000 object and takes about 18 minutes with the network running at 600 some KiB/s.  Keep an eye on it, mine stopped about 23% in and I had to restart it. The second git takes almost no time.
 
  
Now run the following to update the metadata:
+
export ARCH=arm
<pre>
+
  export CROSS_COMPILE=arm-linux-gnueabi-
cd ${OETREE}/openembedded
 
git pull
 
</pre>
 
You've created a directory called <code>oe</code>. Go explore around it to see what is there.  Be sure to look in <code>oe/openembedded/recipes</code>.  These folders contain instructions on where to get and how to build various things.  Look in <code>recipes/Linux</code>.  Here are instructions for building various Linux kernels.  We'll be using <code>linux-omap-2.6.*</code>.  What's the highest version you can find?
 
  
== Step 2 - Installing bitbake and friends ==
+
Now ''source'' the file and compile again. (Note: you only have to source once per terminal session.)
 +
host$ '''source ~/crossCompileEnv.sh'''
 +
host$ '''${CROSS_COMPILE}gcc helloWorld.c'''
 +
host$ '''file a.out'''
 +
a.out: ELF 32-bit LSB executable, ARM, version 1 (SYSV),
 +
  dynamically linked (uses shared libs), for GNU/Linux 2.6.31,
 +
  BuildID[sha1]=0x10182364352b9f3cb15d1aa61395aeede11a52ad, not stripped
  
bitbake is the workhorse that knows where to get everything and how to compile it.  The following will install bitbake and additional programs that bitbake needs.  This may take 5 minutes.
+
The '''file''' command tells what's in the file. In this case we have an ARM executable. Success!  Now copy to your Beagle and run
<pre>
 
sudo apt-get install bitbake
 
sudo apt-get install g++
 
sudo apt-get install help2man diffstat texi2html cvs texinfo subversion gawk
 
sudo apt-get autoremove
 
</pre>
 
If you are running Ubuntu you will have to also do the following:
 
<pre>
 
cd /bin
 
sudo mv sh sh.old
 
sudo ln -s bash sh
 
sudo sh -c "echo 0 > /proc/sys/vm/mmap_min_addr"
 
</pre>
 
Finally edit the file <code>/etc/sysctl.conf</code> using:
 
<pre>
 
sudo gedit /etc/sysctl.conf
 
</pre>
 
Add the following at the end and save.
 
<pre>
 
# This is for bitbake
 
vm.mmap_min_addr = 0
 
</pre>
 
Now you should be ready to run bitbake.
 
  
== Step 3 - Setting up for the BeagleBoard ==
+
host$ '''scp a.out root@beagle:.'''
 +
host$ '''ssh root@beagle ./a.out'''
 +
Hello, World! Main is executing at 0x8374
 +
This address (0xbeb32d4c) is in our stack frame
 +
This address (0x10650) is in our bss section
 +
This address (0x10648) is in our data section
  
We need a small script to setup the environment, so download <code>source-me.txt</code> [http://www.rose-hulman.edu/~yoder/eLinux/files/source-me.txt] to ${OETREE}.
+
The '''scp''' copies a.out to the beagle and the '''ssh''' runs the a.out on the beagle. Notice the addresses are very different from the host version.
  
Now let's setup <code>local.conf</code> for our needs:
+
{{YoderFoot}}
<pre>
 
mkdir -p ${OETREE}/build/conf
 
cp ${OETREE}/openembedded/contrib/angstrom/local.conf ${OETREE}/build/conf/
 
</pre>
 
Open <code>${OETREE}/build/conf/local.conf</code> in your favourite editor and add the following to the end of the file.
 
<pre>
 
MACHINE ?= "beagleboard"
 
</pre>
 
 
 
== Step 4 - Start building ==
 
Do the following...
 
<pre>
 
# set environment variables
 
cd ${OETREE}
 
source source-me.txt
 
 
 
#Go to the OE tree
 
cd ${OETREE}/openembedded
 
 
 
#Make sure it's up to date
 
git pull --rebase
 
 
 
#Start building
 
bikebake nano
 
</pre>
 
 
 
{{Give
 
|title=Psyco JIT compiler
 
|tip=bitbake suggest loading a compiler.  Load and test the compiler.  If it's worth using, write instructions.
 
}}
 
 
 
This will take a while.  bitbake is installing everything that is needed to compile the system.  This includes cross compilers, assemblers, source, everything.  I started at 10am and ended around 5:30pm.  It was running on just one of the two cores on my laptop.  How long did it take on your machine?  I notice that an additional 600M of disk space is being used.
 
 
 
Up to this point all we have done is load all the infrastructure needed and compiled the simple '''nano''' text editor.  We don't even have the kernel yet. Do this to build a basic console image:
 
<pre>
 
bitbake console-image
 
</pre>
 
How did I know to use '''console-image'''?  I ran the following to find what images were out there:
 
<pre>
 
locate image | grep /oe/
 
</pre>
 
This found every file with '''image''' in the name it that also had '''/oe/''' in the path.  From this I see that '''oe/openbmebeed/recipes/image''' has a bunch of files ending in '''-image'''.  Take a look at '''console-image.bb''' and see what you can figure out.
 
[[Category:ECE597]]
 

Revision as of 13:37, 30 August 2013

thumb‎ Embedded Linux Class by Mark A. Yoder


This class is about developing software for embedded Linux. So far we have been doing all of our development on the Beagle. This works well for small (and not so small) programs. However, we are now moving into kernel development and that's best done on a more powerful host computer. In EBC Exercise 08 Installing Development Tools you learned how to download and install the cross-compilers and the source for kernel and u-boot. Now we'll use those tools.

Cross-compiling Hello World

This shows how to cross-compile with compiler loaded with

host$ sudo apt-get install gcc-arm-linux-gnueabi

Listing 2-4 on page 29 of the text is an embedded version of Hello World. If you've set up your git repository you will find it in helloWorld.c when you do a git pull. Compile and run it on your host to be sure it works.

host$ gcc helloWorld.c
host$ file a.out
a.out: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), 
 dynamically linked (uses shared libs), for GNU/Linux 2.6.24, 
 BuildID[sha1]=0x357e34e90f7c32d414368d69cc06d0aed59acf1c, not stripped
host$ ./a.out
Hello, World! Main is executing at 0x400524
This address (0x7fff8260bdf8) is in our stack frame
This address (0x601038) is in our bss section
This address (0x601020) is in our data section

Now that you know it's working, let's cross compile it. First set the paths to find the cross-compiler. Put the following in a file, call it ~/crossCompileEnv.sh.

export ARCH=arm
export CROSS_COMPILE=arm-linux-gnueabi-

Now source the file and compile again. (Note: you only have to source once per terminal session.)

host$ source ~/crossCompileEnv.sh
host$ ${CROSS_COMPILE}gcc helloWorld.c
host$ file a.out
a.out: ELF 32-bit LSB executable, ARM, version 1 (SYSV), 
 dynamically linked (uses shared libs), for GNU/Linux 2.6.31, 
 BuildID[sha1]=0x10182364352b9f3cb15d1aa61395aeede11a52ad, not stripped

The file command tells what's in the file. In this case we have an ARM executable. Success! Now copy to your Beagle and run

host$ scp a.out root@beagle:.
host$ ssh root@beagle ./a.out
Hello, World! Main is executing at 0x8374
This address (0xbeb32d4c) is in our stack frame
This address (0x10650) is in our bss section
This address (0x10648) is in our data section

The scp copies a.out to the beagle and the ssh runs the a.out on the beagle. Notice the addresses are very different from the host version.




thumb‎ Embedded Linux Class by Mark A. Yoder