Top Banner
Embedded Systems Programming OS Linux - Toolchain Iwona Kocha´ nska Gdansk University of Technology
34

Embedded Systems Programming - git.eti.pg.gda.plgit.eti.pg.gda.pl/intel-grant/pliki/esp/ESP_Toolchain_ENG.pdf · GUT – Intel 2015/16 2/34 Embedded software Toolchain compiler and

Aug 31, 2019

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Embedded Systems Programming - git.eti.pg.gda.plgit.eti.pg.gda.pl/intel-grant/pliki/esp/ESP_Toolchain_ENG.pdf · GUT – Intel 2015/16 2/34 Embedded software Toolchain compiler and

GUT – Intel 2015/16 1/34

Embedded Systems ProgrammingOS Linux - Toolchain

Iwona Kochanska

Gdansk University of Technology

Page 2: Embedded Systems Programming - git.eti.pg.gda.plgit.eti.pg.gda.pl/intel-grant/pliki/esp/ESP_Toolchain_ENG.pdf · GUT – Intel 2015/16 2/34 Embedded software Toolchain compiler and

GUT – Intel 2015/16 2/34

Embedded software

Toolchaincompiler and tools for hardware-

dependent software developement

Boot-loader

initializes hardwareand loads OS kernel

Kernela heart of a system, man-ages resources and com-municates with hardware

Rootfilesys-

temlibraries and applications

Page 3: Embedded Systems Programming - git.eti.pg.gda.plgit.eti.pg.gda.pl/intel-grant/pliki/esp/ESP_Toolchain_ENG.pdf · GUT – Intel 2015/16 2/34 Embedded software Toolchain compiler and

GUT – Intel 2015/16 3/34

What is toolchain?

Toolchain - set of tools that compiles source code into executablesthat can run on the target device, and includes:

I a compiler,I a linker,I run-time libraries

Toolchain is needed to build the other three elements of anembedded Linux system:

I the bootloader,I the kernel,I the root filesystem.

Page 4: Embedded Systems Programming - git.eti.pg.gda.plgit.eti.pg.gda.pl/intel-grant/pliki/esp/ESP_Toolchain_ENG.pdf · GUT – Intel 2015/16 2/34 Embedded software Toolchain compiler and

GUT – Intel 2015/16 4/34

What is Toolchain?

I Toolchain has to be able to compile code written in assembly, C,and C++ since these are the languages used in the base opensource packages

I Usually, toolchains for Linux are based on components from theGNU project (http://www.gnu.org)

I Alternative: Clang compiler and the associated LLVM project(http://llvm.org)

Page 5: Embedded Systems Programming - git.eti.pg.gda.plgit.eti.pg.gda.pl/intel-grant/pliki/esp/ESP_Toolchain_ENG.pdf · GUT – Intel 2015/16 2/34 Embedded software Toolchain compiler and

GUT – Intel 2015/16 5/34

Toolchain components

Standard GNU toolchain consists of three main components:I Binutils: binary utilities including assembler, linker, ld. It is

available at http://www.gnu.org/software/binutils/.I GNU Compiler Collection (GCC): the compilers for C and other

languages, include C++, Objective-C, Objective-C++, Java,Fortran, Ada, and Go. It is available at http://gcc.gnu.org/.

I C library: a standardized API based on the POSIX specificationwhich is the principle interface to the operating system kernelfrom applications.

... and a copy of the Linux kernel headers.

Page 6: Embedded Systems Programming - git.eti.pg.gda.plgit.eti.pg.gda.pl/intel-grant/pliki/esp/ESP_Toolchain_ENG.pdf · GUT – Intel 2015/16 2/34 Embedded software Toolchain compiler and

GUT – Intel 2015/16 6/34

Cross-compilation

Basic definitions:I build: Also called the local platform, the platform that you

physicaly perform the compiling on (for example PC with Debiandistribution)

I target: The destination platform (for example Raspberry Pi)I host: OS providing virtual environmentI guest: OS in virtual environment

Page 7: Embedded Systems Programming - git.eti.pg.gda.plgit.eti.pg.gda.pl/intel-grant/pliki/esp/ESP_Toolchain_ENG.pdf · GUT – Intel 2015/16 2/34 Embedded software Toolchain compiler and

GUT – Intel 2015/16 7/34

Cross-compilation

There are two main problems in cross-compiling:I All C/C++ include files and libraries for the target must be

available on the build platform.I The cross-compiler and related tools must generate code

suitable for the target platform.

Page 8: Embedded Systems Programming - git.eti.pg.gda.plgit.eti.pg.gda.pl/intel-grant/pliki/esp/ESP_Toolchain_ENG.pdf · GUT – Intel 2015/16 2/34 Embedded software Toolchain compiler and

GUT – Intel 2015/16 8/34

Types of toolchains

There are several types of toolchains, based on the architecture ofbuild, host and target machines:

I Native (common in desktop computers)

build machine host machine target machinearch. A ⇒ arch. A ⇒ arch. A

I Cross-compilation

build machine host machine target machinearch. A ⇒ arch. A ⇒ arch. B

Page 9: Embedded Systems Programming - git.eti.pg.gda.plgit.eti.pg.gda.pl/intel-grant/pliki/esp/ESP_Toolchain_ENG.pdf · GUT – Intel 2015/16 2/34 Embedded software Toolchain compiler and

GUT – Intel 2015/16 9/34

Types of toolchains

I Cross-nativebuild machine host machine target machine

arch. A ⇒ arch. B ⇒ arch. B

I Canadianbuild machine host machine target machine

arch. A ⇒ arch. B ⇒ arch. C

Page 10: Embedded Systems Programming - git.eti.pg.gda.plgit.eti.pg.gda.pl/intel-grant/pliki/esp/ESP_Toolchain_ENG.pdf · GUT – Intel 2015/16 2/34 Embedded software Toolchain compiler and

GUT – Intel 2015/16 10/34

CPU architectures

The toolchain has to be built according to the capabilities of the targetCPU, which includes:

I CPU architecture: arm, mips, x86_64, and so onI Big- or little-endian operation: Some CPUs can operate in

both modes, but the machine code is different for eachI Floating point support: Not all versions of embedded

processors implement a hardware floating point unit, in whichcase, the toolchain can be configured to call a software floatingpoint library instead

I Application Binary Interface (ABI): The calling conventionused for passing parameters between function calls

Page 11: Embedded Systems Programming - git.eti.pg.gda.plgit.eti.pg.gda.pl/intel-grant/pliki/esp/ESP_Toolchain_ENG.pdf · GUT – Intel 2015/16 2/34 Embedded software Toolchain compiler and

GUT – Intel 2015/16 11/34

ARM ABI architectures

I OABI - Old Application Binary Interface (OABI) - discontinued2011

I EABI - Extended Application Binary Interface (EABI) - generalpurpose (integer) registers

I EABIHF - Hard Float Extended Application Binary Interface -uses floating point registers.

Page 12: Embedded Systems Programming - git.eti.pg.gda.plgit.eti.pg.gda.pl/intel-grant/pliki/esp/ESP_Toolchain_ENG.pdf · GUT – Intel 2015/16 2/34 Embedded software Toolchain compiler and

GUT – Intel 2015/16 12/34

GNU prefix

GNU uses a prefix consisting of a tuple of three or four componentsseparated by dashes:

I CPU: arm, mips, x86_64, el = little-endian, eb = big-endian;e.g. armeb = big-endian ARM

I Vendor, e.g. pokyI Kernel, e.g. linuxI Operating system: a name for the user space component,

which might be gnu or uclibcgnu. The ABI may be appended(gnueabi, gnueabihf, uclibcgnueabi, or uclibcgnueabihf).

$ gcc −dumpmachinex86_64−l i nux−gnu

Page 13: Embedded Systems Programming - git.eti.pg.gda.plgit.eti.pg.gda.pl/intel-grant/pliki/esp/ESP_Toolchain_ENG.pdf · GUT – Intel 2015/16 2/34 Embedded software Toolchain compiler and

GUT – Intel 2015/16 13/34

C library

I The programming interface to theUnix operating system is defined inthe C language (POSIX standards).

I The C library is the implementationof that interface (gateway to thekernel for Linux programs).

I Whenever the C library needs theservices of the kernel it will use thekernel system call interface totransition between user space andkernel space.

Page 14: Embedded Systems Programming - git.eti.pg.gda.plgit.eti.pg.gda.pl/intel-grant/pliki/esp/ESP_Toolchain_ENG.pdf · GUT – Intel 2015/16 2/34 Embedded software Toolchain compiler and

GUT – Intel 2015/16 14/34

C libraries

I glibc - standard GNU C library, most complete implementation ofthe POSIX API

I eglibc - embedded GLIBC; a series of patches to glibc whichadded configuration options and support for architectures notcovered by glibc; merged back into glibc as of version 2.20

I uClibc - micro controller C library; first developed to work withuClinux (Linux for CPUs without memory management units), buthas since been adapted to be used with full Linux

I musl libc - new C library designed for embedded systems.

Page 15: Embedded Systems Programming - git.eti.pg.gda.plgit.eti.pg.gda.pl/intel-grant/pliki/esp/ESP_Toolchain_ENG.pdf · GUT – Intel 2015/16 2/34 Embedded software Toolchain compiler and

GUT – Intel 2015/16 15/34

Building a toolchain using crosstool-NG

Crosstool-NG (2007) - a way to create a stand-alone cross toolchainfrom sourceSoC or board vendorInstall:

$ sudo apt−get i n s t a l l automake bison chrpath f l e x g++ g i t gper f gawk l ibexpa t1−dev l ibncurses5−dev l i b s d l 1 .2−dev l i b t o o lpython2.7−dev t e x i n f o

Download sources from http://crosstool-ng.org/download/crosstool-ngand:

$ t a r x f c ross too l−ng−1.9 .3 . t a r . bz2$ cd cross too l−ng−1.9.3$ . / con f igu re$ make$ make i n s t a l l

–enable-local option - program will be installed into the currentdirectory, instead of /usr/local/bin.

Page 16: Embedded Systems Programming - git.eti.pg.gda.plgit.eti.pg.gda.pl/intel-grant/pliki/esp/ESP_Toolchain_ENG.pdf · GUT – Intel 2015/16 2/34 Embedded software Toolchain compiler and

GUT – Intel 2015/16 16/34

Building a toolchain using crosstool-NG

Launch the crosstool menu:

$ . / c t−ng

List of sample configurations

$ . / c t−ng l i s t −samples

Select target configuration:

$ . / c t−ng arm−cortex_a8−l i nux−gnueabi

Make changes using the configuration menu:

$ . / c t−ng menuconfig

The configuration data is saved into a file named .config.Build the toolchain:

$ . / c t−ng b u i l d

Page 17: Embedded Systems Programming - git.eti.pg.gda.plgit.eti.pg.gda.pl/intel-grant/pliki/esp/ESP_Toolchain_ENG.pdf · GUT – Intel 2015/16 2/34 Embedded software Toolchain compiler and

GUT – Intel 2015/16 17/34

Building a toolchain using crosstool-NG

Toolchain directory:

arm−cortex_a8−l i nux−gnueabi / b in /

Add directory to the cross compiler to PATH:

$ PATH = / . . . / arm−cortex_a8−l i nux−gnueabihf / b in :$PATH

Compile helloworld.c:

$ arm−cortex_a8−l i nux−gnueabihf−gcc he l l owo r l d . c −o he l l owo r l d

Check the type of the executable:

$ f i l e he l l owo r l dhe l l owo r l d : ELF 32− b i t LSB executable , ARM, vers ion 1 (SYSV) , dynamica l ly l i n k e d ( uses shared l i b s ) , f o r GNU/ Linux 3 .15 .4 , not s t r i p ped

Page 18: Embedded Systems Programming - git.eti.pg.gda.plgit.eti.pg.gda.pl/intel-grant/pliki/esp/ESP_Toolchain_ENG.pdf · GUT – Intel 2015/16 2/34 Embedded software Toolchain compiler and

GUT – Intel 2015/16 18/34

Building a toolchain using crosstool-NG

Check the cross-compiler version:

$ arm−cortex_a8−l i nux−gnueabi−gcc −−vers ion

Check the cross-compiler configuration:

$ arm−cortex_a8−l i nux−gnueabi−gcc −v

Print out the the range of architecture-specific options available:

$ arm−cortex_a8−l i nux−gnueabihf−gcc −−t a rge t−help

Page 19: Embedded Systems Programming - git.eti.pg.gda.plgit.eti.pg.gda.pl/intel-grant/pliki/esp/ESP_Toolchain_ENG.pdf · GUT – Intel 2015/16 2/34 Embedded software Toolchain compiler and

GUT – Intel 2015/16 19/34

Building a toolchain using crosstool-NG

The toolchain sysroot - a directory which contains subdirectories forlibraries, header files, and other configuration files.

I It can be set when the toolchain is configured through–with-sysroot=or it can be set on the command line, using –sysroot=.

I Check the location of the default sysroot:

$ arm−cortex_a8−l i nux−gnueabi−gcc −p r i n t−sysroo t

Page 20: Embedded Systems Programming - git.eti.pg.gda.plgit.eti.pg.gda.pl/intel-grant/pliki/esp/ESP_Toolchain_ENG.pdf · GUT – Intel 2015/16 2/34 Embedded software Toolchain compiler and

GUT – Intel 2015/16 20/34

Building a toolchain using crosstool-NG

Sysroot contains:I lib: shared objects for the C library and the dynamic linker/loader,I ld-linux usr/lib: the static library archives for the C library and

any other libraries that may be installed subsequentlyI usr/include: headers for all the librariesI usr/bin: utility programs that run on the target, such as the ldd

commandI usr/share: used for localization and internationalizationI sbin: Provides the ldconfig utility, used to optimize library loading

pathsSome of these are needed on the development host to compileprograms, others are needed on the target at runtime.

Page 21: Embedded Systems Programming - git.eti.pg.gda.plgit.eti.pg.gda.pl/intel-grant/pliki/esp/ESP_Toolchain_ENG.pdf · GUT – Intel 2015/16 2/34 Embedded software Toolchain compiler and

GUT – Intel 2015/16 21/34

Tools of a toolchain

Command Descriptionaddr2line Converts program addresses into filenames and numbersar Archive utility used to create static libraries.as GNU assemblerc++filt demangle C++ and Java symbolscpp C preprocessorelfedit update the ELF header of ELF filesg++ GNU C++ front-endgcc GNU C front-endgcov code coverage toolgdb GNU debuggergprof program profiling tool

Page 22: Embedded Systems Programming - git.eti.pg.gda.plgit.eti.pg.gda.pl/intel-grant/pliki/esp/ESP_Toolchain_ENG.pdf · GUT – Intel 2015/16 2/34 Embedded software Toolchain compiler and

GUT – Intel 2015/16 22/34

Tools of a toolchain

Command Descriptionld GNU linkernm list of symbols from object filesobjcopy used to copy and translate object filesobjfump display information from object filesranlib creates or modifies an index in a static library, making the linking stage fasterreadelf displays information about files in ELF object formatsize lists section sizes and the total sizestrings display strings of printable characters in filesstrip strip an object file of debug symbol tables

Page 23: Embedded Systems Programming - git.eti.pg.gda.plgit.eti.pg.gda.pl/intel-grant/pliki/esp/ESP_Toolchain_ENG.pdf · GUT – Intel 2015/16 2/34 Embedded software Toolchain compiler and

GUT – Intel 2015/16 23/34

C library

Four main parts implement POSIX standardI libc: main C library that contains the well-known POSIX

functions (printf, open, close, read, write)I libm: maths functions (cos, exp, log)I libpthread: all the POSIX thread functions with names beginning

with pthread_I librt: The real-time extensions to POSIX, including shared

memory and asynchronous I/Olibc is always linked in but the others have to be explicitly linked withthe -l option

arm−cortex_a8−l i nux−gnueabihf−gcc myprog . c −o myprog −lm

Page 24: Embedded Systems Programming - git.eti.pg.gda.plgit.eti.pg.gda.pl/intel-grant/pliki/esp/ESP_Toolchain_ENG.pdf · GUT – Intel 2015/16 2/34 Embedded software Toolchain compiler and

GUT – Intel 2015/16 24/34

C library

Which libraries have been linked?

$ arm−cortex_a8−l i nux−gnueabihf−r e a d e l f −a myprog | grep " Shared l i b r a r y "0x00000001 (NEEDED) Shared l i b r a r y : [ l ibm . so . 6 ]0x00000001 (NEEDED) Shared l i b r a r y : [ l i b c . so . 6 ]

Check the run-time linker:

$ arm−cortex_a8−l i nux−gnueabihf−r e a d e l f −a myprog | grep " program i n t e r p r e t e r " [ Requesting program i n t e r p r e t e r : / l i b / ld−l i nux−armhf . so . 3 ]

Page 25: Embedded Systems Programming - git.eti.pg.gda.plgit.eti.pg.gda.pl/intel-grant/pliki/esp/ESP_Toolchain_ENG.pdf · GUT – Intel 2015/16 2/34 Embedded software Toolchain compiler and

GUT – Intel 2015/16 25/34

Static and dynamic linking

The library code can be linked in two different ways:I statically - all the library functions the application calls and their

dependencies are pulled from the library archive and bound intothe executable;

I dynamically - references to the library files and functions inthose files are generated in the code but the actual linking isdone dynamically at runtime

Page 26: Embedded Systems Programming - git.eti.pg.gda.plgit.eti.pg.gda.pl/intel-grant/pliki/esp/ESP_Toolchain_ENG.pdf · GUT – Intel 2015/16 2/34 Embedded software Toolchain compiler and

GUT – Intel 2015/16 26/34

Static libraries

Static linking is useful:I when building a small system which consists of only BusyBox

and some script files, it is simpler to link BusyBox statically andavoid having to copy the runtime library files and linker

I when running a program before the filesystem that holds theruntime libraries is available

Tell gcc to link all libraries statically:

$ arm−cortex_a8−l i nux−gnueabihf−gcc −s t a t i c he l l owo r l d . c −o he l lowor ld−s t a t i c

Page 27: Embedded Systems Programming - git.eti.pg.gda.plgit.eti.pg.gda.pl/intel-grant/pliki/esp/ESP_Toolchain_ENG.pdf · GUT – Intel 2015/16 2/34 Embedded software Toolchain compiler and

GUT – Intel 2015/16 27/34

Static libraries

Creating a static library:

$ arm−cortex_a8−l i nux−gnueabihf−gcc −c tes t1 . c$ arm−cortex_a8−l i nux−gnueabihf−gcc −c tes t2 . c$ arm−cortex_a8−l i nux−gnueabihf−ar rc l i b t e s t . a t es t1 . o tes t2 . o

Linking libtest into helloworld program:

$ arm−cortex_a8−l i nux−gnueabihf−gcc he l l owo r l d . c − l t e s t −L . . / l i b s − I . . / l i b s −o he l l owo r l d

Page 28: Embedded Systems Programming - git.eti.pg.gda.plgit.eti.pg.gda.pl/intel-grant/pliki/esp/ESP_Toolchain_ENG.pdf · GUT – Intel 2015/16 2/34 Embedded software Toolchain compiler and

GUT – Intel 2015/16 28/34

Shared libraries

I Shared objects linked at runtime,I Efficient use of storage and system memory, since only one copy

of the code needs to be loaded.I Easy to update library files without having to re-link all the

programs that use them.

Page 29: Embedded Systems Programming - git.eti.pg.gda.plgit.eti.pg.gda.pl/intel-grant/pliki/esp/ESP_Toolchain_ENG.pdf · GUT – Intel 2015/16 2/34 Embedded software Toolchain compiler and

GUT – Intel 2015/16 29/34

Shared libraries

Creating a shared library:

$ arm−cortex_a8−l i nux−gnueabihf−gcc −fPIC −c tes t1 . c$ arm−cortex_a8−l i nux−gnueabihf−gcc −fPIC −c tes t2 . c$ arm−cortex_a8−l i nux−gnueabihf−gcc −shared −o l i b t e s t . so tes t1 . o tes t2 . o

PIC flag tells GCC to produce code that does not contain referencesto specific memory locations for functions and variablesLinking libtest into helloworld program:

$ arm−cortex_a8−l i nux−gnueabihf−gcc he l l owo r l d . c − l t e s t −L . . / l i b s − I . . / l i b s −o he l l owo r l d

The linker will look for libtest.so in the default search path: /lib and/usr/lib.Other directories with shared libraries - place a colon-separated list ofpaths in the shell variable LD_LIBRARY_PATH

Page 30: Embedded Systems Programming - git.eti.pg.gda.plgit.eti.pg.gda.pl/intel-grant/pliki/esp/ESP_Toolchain_ENG.pdf · GUT – Intel 2015/16 2/34 Embedded software Toolchain compiler and

GUT – Intel 2015/16 30/34

Shared libraries names

ldd - searches the standard system library paths and shows whichlibrary versions would be used by a particular program.

ldd he l l owo r l d

Shared libraries names:I libjpeg.a: library archive used for static linkingI libjpeg.so -> libjpeg.so.8.0.2 : symbolic link, used for dynamic

linkingI libjpeg.so.8 -> libjpeg.so.8.0.2: symbolic link used when loading

the library at runtimeI libjpeg.so.8.0.2: actual shared library, used at both compile time

and runtime

Page 31: Embedded Systems Programming - git.eti.pg.gda.plgit.eti.pg.gda.pl/intel-grant/pliki/esp/ESP_Toolchain_ENG.pdf · GUT – Intel 2015/16 2/34 Embedded software Toolchain compiler and

GUT – Intel 2015/16 31/34

Build systems for cross compiling

I Simple makefiles where the toolchain is controlled by the makevariable CROSS_COMPILE

I The GNU build system known as AutotoolsI CMake (https://cmake.org)

Page 32: Embedded Systems Programming - git.eti.pg.gda.plgit.eti.pg.gda.pl/intel-grant/pliki/esp/ESP_Toolchain_ENG.pdf · GUT – Intel 2015/16 2/34 Embedded software Toolchain compiler and

GUT – Intel 2015/16 32/34

Simple makefiles

I For packages simple to cross compile (the Linux kernel, theU-Boot bootloader, Busybox)

I put the toolchain prefix in the make variable CROSS_COMPILE,for example:

$ make CROSS_COMPILE=arm−cortex_a8−l i nux−gnueabi−

I Or set it as a shell variable:

$ expor t CROSS_COMPILE=arm−cortex_a8−l i nux−gnueabi−$ make

Page 33: Embedded Systems Programming - git.eti.pg.gda.plgit.eti.pg.gda.pl/intel-grant/pliki/esp/ESP_Toolchain_ENG.pdf · GUT – Intel 2015/16 2/34 Embedded software Toolchain compiler and

GUT – Intel 2015/16 33/34

GNU Autotools

Autotools refers to a group of tools that are used as the build systemin many open source projects. The components:

I GNU AutoconfI GNU AutomakeI GNU LibtoolI Gnulib

Packages that use Autotools come with a script named configurethat checks dependencies and generates makefiles.To configure, build, and install a package for the native operatingsystem

$ . / con f igu re$ make$ sudo make i n s t a l l

Page 34: Embedded Systems Programming - git.eti.pg.gda.plgit.eti.pg.gda.pl/intel-grant/pliki/esp/ESP_Toolchain_ENG.pdf · GUT – Intel 2015/16 2/34 Embedded software Toolchain compiler and

GUT – Intel 2015/16 34/34

For Further Reading I

C. Simmonds. Mastering Embedded Linux Programming.Packt Publishing, 201