You are on page 1of 33

EmbeddedLinuxsystemdevelopment

Crosscompiling toolchains
ThomasPetazzoni MichaelOpdenacker FreeElectrons

Copyright20042010,FreeElectrons. CreativeCommonsBYSA3.0license Latestupdate:Jan19,2011, Documentsources,updatesandtranslations: http://freeelectrons.com/docs/toolchains Corrections,suggestions,contributionsandtranslationsarewelcome!

FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com

Definition(1)
TheusualdevelopmenttoolsavailableonaGNU/Linuxworkstationisa nativetoolchain Thistoolchainrunsonyourworkstationandgeneratescodeforyour workstation,usuallyx86 Forembeddedsystemdevelopment,itisusuallyimpossibleornot interestingtouseanativetoolchain Thetargetistoorestrictedintermsofstorageand/ormemory Thetargetisveryslowcomparedtoyourworkstation Youmaynotwanttoinstallalldevelopmenttoolsonyourtarget. Therefore,crosscompilingtoolchainsaregenerallyused.Theyrun onyourworkstationbutgeneratecodeforyourtarget.

2
FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com

Definition(2)
Sourcecode Crosscompiling toolchain x86 Execution machine ARM Compilation machine

Nativetoolchain

x86binary x86

ARMbinary

3
FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com

Components

Binutils

Kernelheaders

C/C++libraries

GCCcompiler

GDBdebugger (optional)

4
FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com

Binutils
Binutilsisasetoftoolstogenerateandmanipulatebinariesfora givenCPUarchitecture
as,theassembler,thatgeneratesbinarycodefromassembler sourcecode ld,thelinker ar,ranlib,togenerate.aarchives,usedforlibraries objdump,readelf,size,nm,strings,toinspectbinaries. Veryusefulanalysistools! strip,tostripuselesspartsofbinariesinordertoreducetheirsize

http://www.gnu.org/software/binutils/ GPLlicense

5
FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com

Kernelheaders(1)
TheClibraryandcompiledprograms needstointeractwiththekernel
Availablesystemcallsandtheir numbers Constantdefinitions Datastructures,etc.

Kernel Kernelheaders CLibrary Applications

Therefore,compilingtheClibrary requireskernelheaders,andmany applicationsalsorequirethem.

Availablein<linux/...>and<asm/...>andafewother directoriescorrespondingtotheonesvisibleininclude/inthe kernelsources


6
FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com

Kernelheaders(2)
Systemcallnumbers,in<asm/unistd.h>
#define__NR_exit1 #define__NR_fork2 #define__NR_read3

Constantdefinitions,herein<asmgeneric/fcntl.h>,included from<asm/fcntl.h>,includedfrom<linux/fcntl.h>
#defineO_RDWR00000002

Datastructures,herein<asm/stat.h>
structstat{ unsignedlongst_dev; unsignedlongst_ino; [...] };

7
FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com

Kernelheaders(3)
ThekerneltouserspaceABIisbackwardcompatible
Binariesgeneratedwithatoolchainusingkernelheadersolderthan therunningkernelwillworkwithoutproblem,butwon'tbeableto usethenewsystemcalls,datastructures,etc. Binariesgeneratedwithatoolchainusingkernelheadersnewer thantherunningkernelmightworkoniftheydon'tusetherecent features,otherwisetheywillbreak Usingthelatestkernelheadersisnotnecessary,unlessaccessto thenewkernelfeaturesisneeded

Thekernelheadersareextractedfromthekernelsourcesusing theheaders_installkernelMakefiletarget.

8
FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com

GCCcompiler
GNUCCompiler,thefamousfreesoftwarecompiler CancompileC,C++,Ada,Fortran,Java,Objective C,ObjectiveC++,andgeneratecodeforalarge numberofCPUarchitectures,includingARM,AVR, Blackfin,CRIS,FRV,M32,MIPS,MN10300, PowerPC,SH,v850,i386,x86_64,IA64,Xtensa,etc. http://gcc.gnu.org/ AvailableundertheGPLlicense, librariesundertheLGPL.

9
FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com

Clibrary
TheClibraryisanessentialcomponentofa Linuxsystem
Interfacebetweentheapplicationsandthe kernel ProvidesthewellknownstandardCAPIto easeapplicationdevelopment

Kernel

SeveralClibrariesareavailable: glibc,uClibc,eglibc,dietlibc,newlib,etc. ThechoiceoftheClibrarymustbemadeat thetimeofthecrosscompilingtoolchain generation,astheGCCcompileriscompiled againstaspecificClibrary.

CLibrary Applications

10
FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com

glibc
http://www.gnu.org/software/libc/ License:LGPL ClibraryfromtheGNUproject Designedforperformance,standardscomplianceand portability FoundonallGNU/Linuxhostsystems Ofcourse,activelymaintained Quitebigforsmallembeddedsystems:approx2.5MB onarm(version2.9libc:1.5MB,libm:750KB)

11
FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com

uClibc
http://www.uclibc.org/fromCodePoetConsulting License:LGPL LightweightClibraryforsmallembeddedsystems Highconfigurability:manyfeaturescanbeenabledor disabledthroughamenuconfiginterface WorksonlywithLinux/uClinux,worksonmostembedded architectures NostableABI,differentABIdependingonthelibrary configuration Focusonsizeratherthanperformance Smallcompiletime
12
FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com

uClibc(2)
MostoftheapplicationscompilewithuClibc.Thisappliestoall applicationsusedinembeddedsystems. Size(arm):4timessmallerthanglibc! uClibc0.9.30.1:approx.600KB(libuClibc:460KB,libm:96KB) glibc2.9:approx2.5MB Usedonalargenumberofproductionembeddedproducts,including consumerelectronicdevices Activelymaintained,largedeveloperanduserbase NowsupportedbyMontaVista,TimeSysandWindRiver.

13
FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com

uClibc(3)
Aftercompilationandinstallation,thefollowingcomponentsareavailable Standardheaders,stdio.h,stdlib.h,unistd.handothers, andLinuxkernelheaders,integratedwiththeClibraryheaders. Thelibrariesthemselves,withmainly libuClibc,theClibraryitself lduClibc,thedynamicloader,responsibleforloadingthe sharedlibrariesatthebeginningofaprogram'sexecution librt,thelibraryimplementingtherealtimerelatedfunctions libstdc++,theC++standardlibrary libpthread,thethreadslibrary libm,themathematiclibrary
14
FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com

Honey,Ishrunktheprograms!

Cprogram Plainhelloworld (stripped) Busybox (stripped)

Compiledwithsharedlibraries glibc uClibc 5.6K 5.4K (glibc2.9) (uClibc0.9.30.1) 245K (olderglibc) 231K (olderuClibc)

Compiledstatically glibc 472K (glibc2.9) 843K (olderglibc) uClibc 18K (uClibc 0.9.30.1) 311K (olderuClibc)

ExecutablesizecomparisononARM

15
FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com

eglibc
Embeddedglibc,undertheLGPL VariantoftheGNUCLibrary(GLIBC)designedtoworkwellon embeddedsystems StrivestobesourceandbinarycompatiblewithGLIBC eglibc'sgoalsincludereducedfootprint,configurablecomponents, bettersupportforcrosscompilationandcrosstesting. CanbebuiltwithoutsupportforNIS,locales,IPv6,andmanyother features. Supportedbyaconsortium,withFreescale,MIPS,MontaVistaand WindRiverasmembers. TheDebiandistributionisswitchingtoeglibctoo: http://blog.aurel32.net/?p=47 http://www.eglibc.org
16
FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com

OthersmallerClibraries
SeveralothersmallerClibrarieshavebeendeveloped,but noneofthemhavethegoalofallowingthecompilationof largeexistingapplications Theyneedspeciallywrittenprogramsandapplications Choices:
Dietlibc,http://www.fefe.de/dietlibc/.Approximately70KB. Newlib,http://sourceware.org/newlib/

Klibc,http://www.kernel.org/pub/linux/libs/klibc/,designed foruseinaninitramfsorinitrdatboottime.

17
FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com

Buildingatoolchain

Threemachinesmustbedistinguishedwhendiscussing toolchaincreation
Thebuildmachine,wherethetoolchainisbuilt. Thehostmachine,wherethetoolchainwillbeexecuted. Thetargetmachine,wherethebinariescreatedbythe toolchainwillbeexecuted.

Fourbuildtypesarepossible

18
FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com

Buildingatoolchain(2)
build host Nativebuild
usedtobuildthenormalgccofa workstation

target

build

host Crossbuild

target

usedtobuildatoolchainthatrunsonyour workstationbutgeneratesbinariesforthe target Themostcommonsolutioninembedded

build

host

target

build

host

target

Crossnativebuild
usedtobuildatoolchainthatrunsonyour targetandgeneratesbinariesforthe target

Canadianbuild
usedtobuildonarchitectureAatoolchain thatrunsonarchitectureBandgenerates binariesforarchitectureC 19

FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com

Buildingatoolchain(3)
Manydecisionsmustbemadewhenbuildingatoolchain ChoosingtheClibrary Choosingtheversionofthedifferentcomponents Choosingtheconfigurationofthetoolchain
WhichABIshouldbeused?ToolchainsfortheARM architectureforexample,cangeneratebinariesusingthe OABI(OldABI)ortheEABI(EmbeddedABI),thatare incompatible Shouldthetoolchainsupportsoftwarefloatingpoint,ordoes thehardwaresupportfloatingpointoperations? Shouldthetoolchainsupportlocales,IPv6,orotherspecific features?
20
FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com

Buildingatoolchain(4)
Crosstoolbuildreports:http://kegel.com/crosstool/crosstool0.43/buildlogs

21
FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com

Basicsteps
Extractandinstallkernelheaders Extract,configure,compileandinstallbinutils Extract,configureandcompileafirstversiongccthat generatesbinariesforthetarget.Itwillbeusedtocross compiletheClibrary. Extract,configureandcompiletheClibraryusingthe previouslygeneratedcompiler. Reconfigureandcompilethefinalgcccrosscompiler.

22
FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com

Homebuilttoolchains
Buildingacrosscompilingtoolchainbyyourselfisadifficult andpainfultask!Cantakedaysorweeks!
Lotsofdetailstolearn.Severalcomponentstobuild. Lotsofdecisionstomake (suchasClibraryversionandconfigurationforyourplatform) NeedkernelheadersandClibrarysources Needtobefamiliarwithcurrentgccissuesandpatchesonyour platform Usefultobefamiliarwithbuildingandconfiguringtools
http://www.aleph1.co.uk/armlinux/docs/toolchain/toolchHOWTO.pdf canshowyouhowfunitcanbe!
23
FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com

Getaprecompiledtoolchain
Solutionthatmostpeoplechoose,becauseitisthesimplest andmostconvenientsolution First,determinewhattoolchainyouneed:CPU,endianism,C library,componentversions,ABI,softfloatorhardfloat,etc. Manyprecompiledtoolchainsarefreelyavailable: CodeSourcery,http://www.codesourcery.com,isa referenceinthatarea,buttheyonlyprovideglibc toolchains. SeethenextpageforLinarotoolchains optimizedforARM. Seealsohttp://elinux.org/Toolchains
24
FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com

Linarotoolchains
https://wiki.linaro.org/WorkingGroups/ToolChain
Linarocontributestoimprovingmainstream gcconARM,inparticularbyhiring CodeSourcerydevelopers. Forpeoplewhocan'twaitforthenextreleasesofgcc,Linaro releasesmodifiedsourcesofstablereleasesofgcc,withthese optimizationsforARM(mainlyforrecentCortexACPUs). Asanygccrelease,thesesourcescanbeusedbybuildtoolsto buildtheirownbinarytoolchains(Buildroot,OpenEmbedded...) Thisallowstosupportglibc,uClibcandeglibc. BinarypackagesareavailableforUbuntuusers: Seehttps://launchpad.net/~linaromaintainers/+archive/toolchain
25
FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com

Installingandusingaprecompiledtoolchain
Followtheinstallationprocedureproposedbythevendor Usually,itissimplyamatterofextractingatarballatthe properplace Toolchainsusednottoberelocatable! Youmustinstalltheminthelocationtheywerebuiltfor. Thisisnolongertruewithgcc4.x,thankstosysroot support,butitisstillmoreconvenienttoinstallthemat theproperplace. Then,addthepathtotoolchainbinariesinyourPATH: exportPATH=/path/to/toolchain/bin/:$PATH

26
FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com

Toolchainbuildingutilities
Anothersolutionistouseutilitiesthatautomatetheprocessof buildingthetoolchain Sameadvantageastheprecompiledtoolchains:youdon'tneed tomessupwithallthedetailsofthebuildprocess Butalsooffersmoreflexibilityintermsoftoolchainconfiguration, componentversionselection,etc. Theyalsousuallycontainseveralpatchesthatfixknownissues withthedifferentcomponentsonsomearchitectures Identicalprinciple:shellscriptsorMakefilethatautomatically fetch,extract,configure,compileandinstallthedifferent components

27
FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com

Toolchainbuildingutilities(2)
Crosstool
Theprecursor,writtenbyDanKegel Setofscriptsandpatches,glibconly Notreallymaintainedanymore http://www.kegel.com/crosstool

Crosstoolng
RewriteofCrosstool,withamenuconfiglikeconfigurationsystem Featurefull:supportsuClibc,glibc,eglibc,hardandsoftfloat,many architectures Activelymaintained http://ymorin.isageek.org/dokuwiki/projects/crosstool

28
FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com

Toolchainbuildingutilities(3)
Manyrootfilesystembuildingsystemsalsoallowtheconstructionof crosscompilingtoolchain Buildroot Makefilebased,uClibconly,maintainedbythecommunity http://buildroot.uclibc.org PTXdist Makefilebased,uClibcorglibc,maintainedmainlyby Pengutronix http://www.pengutronix.de/software/ptxdist/index_en.html OpenEmbedded Thefeaturefull,butcomplexbuildingsystem http://www.openembedded.org/
29
FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com

PracticallabUsingCrosstoolNG

Timetobuildyourtoolchain ConfigureCrosstoolNG Runittobuildyourowncross compilingtoolchain

30
FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com

Relateddocuments

Allourtechnicalpresentations onhttp://freeelectrons.com/docs Linuxkernel Devicedrivers Architecturespecifics EmbeddedLinuxsystemdevelopment


FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com

Howtohelp
Youcanhelpustoimproveandmaintainthisdocument... Bysendingcorrections,suggestions,contributionsand translations Byaskingyourorganizationtoorderdevelopment,consulting andtrainingservicesperformedbytheauthorsofthese documents(seehttp://freeelectrons.com/). Bysharingthisdocumentwithyourfriends,colleagues andwiththelocalFreeSoftwarecommunity. Byaddinglinksonyourwebsitetoouronlinematerials, toincreasetheirvisibilityinsearchengineresults.

FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com

Linuxkernel Linuxdevicedrivers Boardsupportcode Mainstreamingkernelcode Kerneldebugging EmbeddedLinuxTraining Allmaterialsreleasedwithafreelicense! UnixandGNU/Linuxbasics Linuxkernelanddriversdevelopment RealtimeLinux,uClinux Developmentandprofilingtools Lightweighttoolsforembeddedsystems Rootfilesystemcreation Audioandmultimedia Systemoptimization

FreeElectrons
Ourservices
CustomDevelopment Systemintegration EmbeddedLinuxdemosandprototypes Systemoptimization Applicationandinterfacedevelopment Consultingandtechnicalsupport Helpindecisionmaking Systemarchitecture Systemdesignandperformancereview Developmenttoolandapplicationsupport Investigatingissuesandfixingtoolbugs

You might also like