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

From eLinux.org
Jump to: navigation, search
m (Step 1: Get my setup from dfs)
m (Cross compiling Hello World: Updated compiler path)
 
(81 intermediate revisions by 8 users not shown)
Line 1: Line 1:
 
[[Category:ECE497]]
 
[[Category:ECE497]]
 
[[Category: BeagleBoard]]
 
[[Category: BeagleBoard]]
 +
{{YoderHead}}
  
This class is about developing software for embedded Linux. The [http://elinux.org/Main_Page eLinux site] is a good source for embedded Linux in general. There are many ongoing 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_4.4]] 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 [http://www.angstrom-distribution.org Ångström Distribution]It's available many platforms. Look around the site, you may recognize some of them.
+
== Cross compiling Hello World ==
 +
Normally when you compile you compile on the machine that will run the code.  You can compile and run on the Bone, but sometimes (like when compiling the kernel) it's better to use a more powerful machine for the compilingFirst we'll compile ''helloWorld.c'' on the host computer and run it there, then we'll cross compile it on the host to run on the Bone.
  
== Get my setup from dfs ==
+
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.
  
I like to keep all things Beagle in a BeagleBoard directory. Here's how to match my setup:
+
host$ '''gcc helloWorld.c'''
<pre>
+
host$ '''file a.out'''
$ cd
+
a.out: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked,
$ mkdir -p BeagleBoard/oe
+
interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 3.2.0,
$ cd BeagleBoard
+
BuildID[sha1]=049e80612d5735fda0966f6e23c637d345447f49, not stripped
$ sftp username@dfs.rose-hulman.edu
+
  host$ '''./a.out'''
Connecting to dfs.rose-hulman.edu...
+
Hello, World! Main is executing at 0x55f7f41f96aa
username@rose-hulman.edu's password:
+
This address (0x7ffd98d0ebd0) is in our stack frame
sftp> cd Users/Y/yoder/Shared/BeagleBoard/oe
+
This address (0x55f7f43fa018) is in our bss section
sftp> ls
+
  This address (0x55f7f43fa010) is in our data section
sftp> get oebb.sh.tar.gz
 
sftp> get sources.tar.gz
 
sftp> get build2.tar.gz (I'm still tar'ing the real build file)
 
exit
 
$ cd oe
 
$ tar xvf oebb.sh.tar.gz
 
$ tar xvf sources.tar.gz
 
$ tar xvf build2.tar.gz
 
</pre>
 
Since I'm still tar'ing the build directory you won't get all the sources, but you should have enough to compile the kernel, thought it will have to download it from the cloud. Building the kernel will take a bit long since it has to pull all the files off the internet.
 
  
Once you have everything in place you need to create the script that will get the environmental variables.
+
Now that you know it's working, let's cross compile it. First figure out what version of the cross compiler was loaded.
<pre>
 
$ ./oebb.sh config beagleboard
 
</pre>
 
This will create the file <code>~/.oe/environment-2008</code> and make sure your repositories are up to date.  Before bitbaking be sure to source this file:
 
<pre>
 
$ source ~/.oe/environment-2008
 
</pre>
 
Now you can now compile your own kernel and u-boot:
 
<pre>
 
$ time bitbake linux-omap-psp-2.6.32
 
$ time bitbake u-boot.bin
 
</pre>
 
I've added the <code>time</code> command to the bitbake to see how long it takes.  Tell me how long it takes on your system.
 
  
== Step N: Get the setup script and correct it ==
+
host$ '''cd BeagleBoard/bb-kernel/dl'''
 +
host$ '''ls'''
 +
gcc-arm-8.3-2019.03-x86_64-arm-linux-gnueabihf
 +
gcc-arm-8.3-2019.03-x86_64-arm-linux-gnueabihf.tar.xz
  
Instructions for building Ångström are given [http://www.angstrom-distribution.org/building-angstrom here]; however there are a few changes you have to makeHere's what I did.  I have everything in a <code>~/BeagleBoard</code>, so here is how I setup things:
+
Here we see two versions of the compiler have been loadedLet's pick the newer one.
<pre>
 
$ cd
 
$ mkdir -p BeagleBoard
 
$ cd BeagleBoard
 
$ git clone git://git.angstrom-distribution.org/setup-scripts oe
 
$ cd oe
 
</pre>
 
This creates a directory for open embedded (<code>oe</code>) and gets the script to download Ångström. What files do you see?
 
  
Edit <code>oebb.sh</code> and replace every place you find
+
Now set the paths to find the cross-compiler. Put the following in a file, call it '''~/crossCompileEnv.sh'''.
  
git://git.openembedded.org/openembedded
+
export ARCH=arm
 +
export CROSS_COMPILE=arm-linux-gnueabihf-
 +
export PATH=$PATH:~/BeagleBoard/bb-kernel/dl/gcc-arm-8.3-2019.03-x86_64-arm-linux-gnueabihf/bin
  
with
+
Make sure the PATH you use goes to the bin directory where the cross compiler is installed.
  
git://git.openembedded.org/openembedded'''.git'''
+
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, EABI5 version 1 (SYSV),
 +
dynamically linked, interpreter /lib/ld-linux-armhf.so.3,
 +
for GNU/Linux 3.2.0, BuildID[sha1]=17ab3588195851d9eb444f70e5069376cae3bdec,
 +
with debug_info, not stripped
  
Save the file and then 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
  
<pre>
+
host$ '''scp a.out debian@192.168.7.2:.'''
MACHINE=beagleboard ./oebb.sh update
+
host$ '''ssh debian@192.168.7.2 ./a.out'''
</pre>
+
Hello, World! Main is executing at 0x103d5
This will bring in all the tools you need.
+
This address (0xbeb83c54) is in our stack frame
 +
This address (0x21030) is in our bss section
 +
This address (0x21028) 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.
 +
 
 +
{{YoderFoot}}

Latest revision as of 12:21, 20 September 2019

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_4.4 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

Normally when you compile you compile on the machine that will run the code. You can compile and run on the Bone, but sometimes (like when compiling the kernel) it's better to use a more powerful machine for the compiling. First we'll compile helloWorld.c on the host computer and run it there, then we'll cross compile it on the host to run on the Bone.

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 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked,
interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 3.2.0,
BuildID[sha1]=049e80612d5735fda0966f6e23c637d345447f49, not stripped
host$ ./a.out
Hello, World! Main is executing at 0x55f7f41f96aa
This address (0x7ffd98d0ebd0) is in our stack frame
This address (0x55f7f43fa018) is in our bss section
This address (0x55f7f43fa010) is in our data section

Now that you know it's working, let's cross compile it. First figure out what version of the cross compiler was loaded.

host$ cd BeagleBoard/bb-kernel/dl
host$ ls
gcc-arm-8.3-2019.03-x86_64-arm-linux-gnueabihf
gcc-arm-8.3-2019.03-x86_64-arm-linux-gnueabihf.tar.xz

Here we see two versions of the compiler have been loaded. Let's pick the newer one.

Now 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-gnueabihf-
export PATH=$PATH:~/BeagleBoard/bb-kernel/dl/gcc-arm-8.3-2019.03-x86_64-arm-linux-gnueabihf/bin

Make sure the PATH you use goes to the bin directory where the cross compiler is installed.

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, EABI5 version 1 (SYSV),
dynamically linked, interpreter /lib/ld-linux-armhf.so.3,
for GNU/Linux 3.2.0, BuildID[sha1]=17ab3588195851d9eb444f70e5069376cae3bdec,
with debug_info, 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 debian@192.168.7.2:.
host$ ssh debian@192.168.7.2 ./a.out
Hello, World! Main is executing at 0x103d5
This address (0xbeb83c54) is in our stack frame
This address (0x21030) is in our bss section
This address (0x21028) 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