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

From eLinux.org
Jump to: navigation, search
m (Cross-compiling Hello World)
m (Cross-compiling Hello World: Moved crossCompileEnv.sh out of .oe)
(43 intermediate revisions by 4 users not shown)
Line 1: Line 1:
 
[[Category:ECE497]]
 
[[Category:ECE497]]
 
[[Category: BeagleBoard]]
 
[[Category: BeagleBoard]]
 +
{{YoderHead}}
  
I'm still working on this...
+
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.
  
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. [[EBC_Exercise_01_Using_bitbake_for_Kernel_and_U-boot | Here]] 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
First we'll check everything by compiling the Hello World program, then we'll try the kernel and u-boot.
 
  
== Cross-compiling Hello World ==
+
host$ '''sudo apt-get install -y git lzop gcc-arm-linux-gnueabi uboot-mkimage'''
  
 
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.
 
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$ '''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'''
 
  host$ '''./a.out'''
 
  Hello, World! Main is executing at 0x400524
 
  Hello, World! Main is executing at 0x400524
Line 19: Line 23:
 
  This address (0x601020) is in our data 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-compilers. Put the following in a file, call it ~/.oe/'''crossCompileEnv.sh'''. Make sure the path is correct for your system. This is for a 32-bit linux.
+
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>
+
 
# add cross tools to your path
+
export ARCH=arm
PATH=~/BeagleBoard/oe/build/tmp-angstrom_2010_x-eglibc/sysroots/i686-linux/usr/libexec/armv7a-angstrom-linux-gnueabi/gcc/arm-angstrom-linux-gnueabi/4.5.4:$PATH
+
  export CROSS_COMPILE=arm-linux-gnueabi-
export ARCH=arm
+
 
export CROSS_COMPILE=arm-angstrom-linux-gnueabi-
 
</pre>
 
Here is for 64-bit
 
<pre>
 
# add cross tools to your path
 
PATH=~/BeagleBoard/oe/build/tmp-angstrom_2010_x-eglibc/sysroots/x86_64-linux/usr/libexec/armv7a-angstrom-linux-gnueabi/gcc/arm-angstrom-linux-gnueabi/4.5.4:$PATH  
 
export ARCH=arm
 
export CROSS_COMPILE=arm-angstrom-linux-gnueabi-
 
</pre>
 
 
Now ''source'' the file and compile again. (Note: you only have to source once per terminal session.)
 
Now ''source'' the file and compile again. (Note: you only have to source once per terminal session.)
  host$ '''source ~/.oe/'''crossCompileEnv.sh'''
+
  host$ '''source ~/crossCompileEnv.sh'''
  host$ '''gcc helloWorld.c'''
+
  host$ '''${CROSS_COMPILE}gcc helloWorld.c'''
 
  host$ '''file a.out'''
 
  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.16, not stripped
+
  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
 
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
Line 48: Line 45:
 
  This address (0x10648) is in our data 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 address are very different from the host version.
+
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.
 
 
== Finding the Kernel to copy it ==
 
 
 
Once your compile has finished, where is uImage?  One way to find it is
 
 
 
<pre>
 
$ cd ~/BeagleBoard/oe
 
$ find . -name "*uImage*"
 
</pre>
 
You'll find several files with uImage in the name.  The one you want is
 
 
 
<pre>
 
$ cd ~/BeagleBoard/oe/build/tmp-angstrom_2008
 
$ ls
 
</pre>
 
 
 
Here you'll see several directories here.  If you <code>cd work</code> you will find the source code.  We are going to go this way.
 
<pre>
 
$ cd deploy/glibc
 
</pre>
 
 
 
Here you will find some more directories worth exploring.  What do you find in <code>sources</code>?  Finally
 
<pre>
 
$ cd images/beagleboard
 
</pre>
 
Now you have found uImage.  Load away.
 
 
 
== Compile via make ==
 
 
 
When you use bitbake it sets up all the paths to use the correct cross compilers.  You can also build the kernel or u-boot by using '''make''' if you set the paths yourself.  Here's what you need to do.  Put the following in a file, call it ~/BeagleBoard/oe/'''crossCompileEnv.sh'''. Make sure the path is correct for your system.
 
<pre>
 
PATH=~/BeagleBoard/oe/build/tmp-angstrom_2008_1/sysroots/i686-linux/usr/armv7a/bin:$PATH  # add cross tools to your path
 
export ARCH=arm
 
export CROSS_COMPILE=arm-angstrom-linux-gnueabi-
 
</pre>
 
Note from Brian: On my machine I had to add ~/BeagleBoard/oe/build/tmp-angstrom_2008_1/sysroots/x86_64-linux/usr/armv7a/bin to $PATH
 
 
 
Then ''source'' the file and ''cd'' to the kernel directory and try a ''make''.
 
<pre>
 
$ source ~/BeagleBoard/oe/crossCompileEnv.sh
 
$ cd ~/BeagleBoard/oe/build/tmp-angstrom_2008_1/work/beagleboard-angstrom-linux-gnueabi/linux-omap-psp-2.6.32-r88+gitra6bad4464f985fdd3bed72e1b82dcbfc004d7869/git
 
$ make xconfig
 
</pre>
 
To make the kernel and u-boot run
 
<pre>
 
$ make uImage
 
$ make u-boot
 
</pre>
 
Where do these put the new uImage?  It's not where bitbake puts them.  Here's how I found them.
 
<pre>
 
$ find . -name uImage
 
</pre>
 
  
If you would prefer to maintain your own kernel source tree outside of OE, see these directions:
+
{{YoderFoot}}
[[BeagleBoardLinuxKernel]] Alternatively it is possible to run the official omap branch of the linux kernel.  Take a peek at this page:
 
[[BeagleBoard#Linux_kernel]].
 

Revision as of 18:26, 27 May 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 -y git lzop gcc-arm-linux-gnueabi uboot-mkimage

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