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

From eLinux.org
Jump to: navigation, search
(Cross-compiling Hello World: Fixed crossCompile.sh to work with busybox)
m (Cross compiling Hello World: Updated compiler path)
 
(38 intermediate revisions by 4 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. 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.
+
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.
  
First we'll check everything by compiling the Hello World program, then we'll try the kernel and u-boot.
+
== 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.
  
== Cross-compiling 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 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'''
 
  host$ '''./a.out'''
  Hello, World! Main is executing at 0x400524
+
  Hello, World! Main is executing at 0x55f7f41f96aa
  This address (0x7fff8260bdf8) is in our stack frame
+
  This address (0x7ffd98d0ebd0) is in our stack frame
  This address (0x601038) is in our bss section
+
  This address (0x55f7f43fa018) is in our bss section
  This address (0x601020) is in our data section
+
  This address (0x55f7f43fa010) 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 figure out what version of the cross compiler was loaded.
  
  # add cross tools to your path
+
  host$ '''cd BeagleBoard/bb-kernel/dl'''
PATH=$PATH:~/BeagleBoard/oe/build/tmp-angstrom_2010_x-eglibc/sysroots/x86_64-linux/usr/bin
+
host$ '''ls'''
  PATH=~/BeagleBoard/oe/build/tmp-angstrom_2010_x-eglibc/sysroots/x86_64-linux/usr/bin/armv7a-angstrom-linux-gnueabi:$PATH 
+
  gcc-arm-8.3-2019.03-x86_64-arm-linux-gnueabihf
  export ARCH=arm
+
  gcc-arm-8.3-2019.03-x86_64-arm-linux-gnueabihf.tar.xz
export CROSS_COMPILE=arm-angstrom-linux-gnueabi-
 
  
 +
Here we see two versions of the compiler have been loaded.  Let's pick the newer one.
  
Here it is for 64-bit
+
Now set the paths to find the cross-compiler. Put the following in a file, call it '''~/crossCompileEnv.sh'''.
  
# add cross tools to your path
 
PATH=~/BeagleBoard/oe/build/tmp-angstrom_2010_x-eglibc/sysroots/''x86_64''-linux/usr/bin:~/BeagleBoard/oe/build/tmp-angstrom_2010_x-eglibc/sysroots/''x86_64''-linux/usr/bin/armv7a-angstrom-linux-gnueabi:$PATH 
 
 
  export ARCH=arm
 
  export ARCH=arm
  export CROSS_COMPILE=arm-angstrom-linux-gnueabi-
+
  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.)
 
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$ '''${CROSS_COMPILE}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, 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
 
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$ '''scp a.out debian@192.168.7.2:.'''
  host$ '''ssh root@beagle ./a.out'''
+
  host$ '''ssh debian@192.168.7.2 ./a.out'''
  Hello, World! Main is executing at 0x8374
+
  Hello, World! Main is executing at 0x103d5
  This address (0xbeb32d4c) is in our stack frame
+
  This address (0xbeb83c54) is in our stack frame
  This address (0x10650) is in our bss section
+
  This address (0x21030) is in our bss section
  This address (0x10648) is in our data 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 address are very different from the host version.
 
 
 
== Finding the Kernel and Installing It ==
 
 
 
After doing [[EBC_Exercise_01_Using_bitbake_for_Kernel_and_U-boot#Finding_the_Correct_Kernel | this]] exercise you have a Beagle kernel on your host computer. Let's see if it works.
 
 
 
Once compiled the kernel is put in a file called '''uImage'''.  You may have seen it in the FAT partition on your SD card.  Let's find it on your host. One way to find it is
 
 
 
host$ '''cd ~/BeagleBoard/oe'''
 
host$ '''find . -name "*uImage*" | grep 2.6.32 | grep boot'''
 
 
 
We are looking for uImage, but we only want the 2.6.32 version.  The last grep file the file in the '''boot''' directory. Several lines are printed, but the one I'm interested in is:
 
''./build/tmp-angstrom_2010_x-eglibc/work/beagleboard-angstrom-linux-gnueabi/linux-omap-psp-2.6.32-r110b+gitr5fc29e7b2a76a64a739f857858ef0b98294aa155/git''/arch/arm/boot/uImage
 
 
 
The first part of the path (in italics) is the path to the kernel.  Check it out.
 
 
 
host$ '''cd build/tmp-angstrom_2010_x-eglibc/work/beagleboard-angstrom-linux-gnueabi/linux-omap-psp-2.6.32-r110b+gitr5fc29e7b2a76a64a739f857858ef0b98294aa155/git'''
 
host$ '''ls -F'''
 
arch/          drivers/  Kbuild      modules.order  samples/    usr/
 
block/          firmware/  kernel/      Module.symvers  scripts/    virt/
 
COPYING        fs/        lib/        net/            security/  vmlinux*
 
CREDITS        include/  MAINTAINERS  patches/        sound/      vmlinux.o
 
crypto/        init/      Makefile    README          System.map
 
Documentation/  ipc/      mm/          REPORTING-BUGS  tools/
 
 
 
We'll be learning what's in many of these over the next couple of weeks.  Remember this location.  Let's find uImage.
 
 
 
host$ '''cd arch/arm/boot; ls -shF'''
 
total 13M
 
4.0K bootp/      6.3M Image*      4.0K Makefile  3.0M zImage*
 
4.0K compressed/  4.0K install.sh  3.0M uImage
 
host$ '''ls -l uImage'''
 
-rw-r--r-- 1 beagle beagle 3144300 2011-12-08 01:02 uImage
 
 
 
It should have the date that you did the bitbake.  Let's see if it runs on the Beagle
 
host$ '''scp uImage root@beagle:.'''
 
host$ '''ssh root@beagle'''
 
beagle$ '''cd /boot; ls -F'''
 
total 18M
 
4.0K MLO@
 
372K Module.symvers-2.6.32
 
1.4M System.map-2.6.32
 
  80K config-2.6.32
 
4.0K u-boot.bin@
 
    0 uImage@
 
3.1M uImage-2.6.32
 
8.9M vmlinux-2.6.32
 
beagle$ '''ls -l uImage'''
 
lrwxrwxrwx 1 www-data www-data 13 May 12  2011 uImage -> uImage-2.6.32
 
 
 
So the uImage that is there is a symbolic link to '''uImage-2.6.32'''.  Let's save the working uImage in a difference file and copy our new uImage in.
 
 
 
beagle$ '''mv uImage-2.6.32 uImage-2.6.32.orig'''
 
beagle$ '''mv ~/uImage uImage-2.6.32'''
 
 
 
And then reboot.
 
beagle$ '''shutdown -r now'''
 
 
 
After a couple of minutes your should be back and running again.  Check and see if you are really running the new kernel.
 
beagle$ '''uname -a'''
 
Linux beagleboard 2.6.32 #3 PREEMPT Thu Dec 8 01:02:13 EST 2011 armv7l GNU/Linux
 
 
 
It worked! That's the date I compiled mine on.
 
 
 
== Installing a New U-boot ==
 
 
 
While we're at it, let's install a new U-boot.  Note:  The new U-boot isn't working for me.
 
 
 
host$ '''cd ~/BeagleBoard/oe/build/tmp-angstrom_2010_x-eglibc/work/beagleboard-angstrom-linux-gnueabi/'''
 
host$ '''ls -F'''
 
linux-3.0.9-r110a/
 
linux-omap-psp-2.6.32-r110b+gitr5fc29e7b2a76a64a739f857858ef0b98294aa155/
 
shadow-4.1.4.3-r5/
 
u-boot-2011.09-r4/
 
 
 
There's the U-boot directory.
 
 
 
host$ cd u-boot-2011.09-r4/git/; ls -F
 
api/        COPYING    fs/          mkconfig*    post/            tools/
 
arch/      CREDITS    include/    mmc_spl/      README          u-boot*
 
board/      disk/      lib/        nand_spl/    rules.mk        '''u-boot.bin'''
 
boards.cfg  doc/      MAINTAINERS  net/          snapshot.commit  u-boot.lds
 
common/    drivers/  MAKEALL*    onenand_ipl/  spl/            u-boot.map
 
config.mk  examples/  Makefile    patches/      System.map      u-boot.srec
 
 
 
The file we want is right on the top level.
 
 
 
host$ '''scp u-boot.bin root@beagle:.'''
 
host$ '''ssh -X root@beagle'''
 
beagle$ '''cd /media/mmcblk0p1/; ls -F'''
 
MLO*        u-boot.bin.broken*  uEnv.txt*
 
U-BOOT.BIN*  UIMAGE*            uEnv/
 
 
 
Here we've changed to the FAT partition which is where u-boot lives.  Back it up and install the new one.
 
 
 
beagle$ '''mv U-BOOT-BIN u-boot-bin.orig'''
 
beagle$ '''mv ~/.u-boot-bin .'''
 
beagle$ '''shutdown -r now'''
 
 
 
You should now see the new u-boot running.  However I was unable to get the 2.6.32 kernel to boot.
 
 
 
== 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 like we did above. 
 
 
 
If you haven't already, ''source'' the file and ''cd'' to the kernel directory and try a ''make''.
 
 
 
host$ '''source ~/.oe/crossCompileEnv.sh'''
 
host$ '''cd ~/BeagleBoard/oe/build/tmp-angstrom_2008_1/work/beagleboard-angstrom-linux-gnueabi/linux-omap-psp-2.6.32-r88+gitra6bad4464f985fdd3bed72e1b82dcbfc004d7869/git'''
 
host$ '''make xconfig'''
 
 
 
([[EBC Exercise 13 Configuring the Kernel]] has details on configuring the kernel.)
 
 
 
To make the kernel run
 
  
host$ '''make uImage'''
+
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, follow the instructions above to install your freshly compiled kernel.
+
{{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