Embedded Systems: Lecture 14: Introduction to GNU Toolchain (Binary Utilities)
ESSENTIAL Linux For EMBEDDED Developers · 2018. 9. 20. · • The usual development tools...
Transcript of ESSENTIAL Linux For EMBEDDED Developers · 2018. 9. 20. · • The usual development tools...
ESSENTIALLinuxForEMBEDDEDDevelopers
Theopensourceway!
AnoverviewofLinuxenvironment
LinuxEvolutionWhatisLinux?Distributions
LinuxVirtualizationLinuxEverywhere
TheLinuxkernelarchives
http://en.wikipedia.org/wiki/Linux
GNU/Linux
• Recursiveacronym“GNU’snotUNIX”• http://www.gnu.org/ nothttp://www.gnu.com/
• RichardStallman(1983)GoalafreeUnix• KnownforFreeSoftwaremovement,GNU,Emacs,gcc• NeverreallyreleasedGNUoperatingsystem
• Linus1992opensourcedLinuxKernelandnamedGNU/Linux
• FreeSoftwareFoundation• http://www.fsf.org/
http://en.wikipedia.org/wiki/GNU
TypicalLinuxSYSTEMLayout
SystemReserved
Rootfs
Kernel
Bootloader
Hardware
Linux Distributions
•Redhat/fedora/Centos• Most popular, good all around choice• Fedora – community supported• Enterprise Redhat – corporate supported
•Debian/Ubuntu/Mint• Completely noncommercial• Massive package selection and easy management• Not as user friendly, but improving
•SuSe• IBM Preferred Linux – z/Series Linux of choice
TheBlackfin uClinux DistributionbyAnalogDevices–aforkoftheuClinux distributionforBlackfin processors
EmbeddedAlley- seehttp://www.embeddedalley.com/
Lineo SolutionsuLinux
MontaVista Linux- seehttp://www.mvista.com/products_services.php
Pengutronix - seehttp://www.pengutronix.de/oselas/bsp/index_en.html
RidgeRun Linux- seehttp://www.ridgerun.com/sdk.shtml
TimeSys Linux- seehttp://www.timesys.com/embedded-linux/linuxlink
WindRiver- seehttp://www.windriver.com/products/linux/
Digi EmbeddedLinuxforDigi's ARMbasedmodules
EmbeddedLINUX
Virtualization
GuestOperatingSystemVirtualization SharedKernelVirtualization
KernelLevelVirtualization HypervisorVirtualizationXen,VMwareESXServerandMicrosoft'sHyper-Vtechnology(UML)and(KVM).
LinuxVServer,SolarisZonesandContainers,andOpenVZVMwareServerandVirtualBox.
SeeL4LinuxCoLinuxMkLinux
LinuxKERNELTODAY
Kernel.org
Linux Kernel Information
l The Linux kernel version numbers consist of three numbers separated by decimals, such as2.2.14. The first number is the major version number. The second number is the minor revisionnumber. The third number is the patch level version
l There are two stages of kernel releases: “stable” and “development”. Development kernels endin an odd number (2.3, 2.5, …), stable or production kernels end in an even number (2.4,2.6,3.0).
l Once a kernel is deemed stable, it will move from an odd to even second number for release(e.g., from 2.3.51 to 2.4.0).
l You can get a good sense of what the future production state of Linux will be by looking at thedevelopment kernel.
l http://www.kernel.org
ApplicationinteractionsinLinuxTheKernelArchitectureoverviewandaddressspaceview
Applicationandmodesofoperations,KernelandUseraddressSpacesSystemcall,entryandexitpoints,lowlevelview,strace,
parameterpassingandkernelimplementations
Structure:The“Core” LinuxKernel
Applications
SystemLibraries(libc)
SystemCallInterface
Hardware
Architecture-DependentCode
I/ORelated ProcessRelatedScheduler
MemoryManagement
IPC
FileSystems
Networking
DeviceDrivers
Mod
ules
OperativeModes
• To avoid having applications that constantly crashed,newer OSs were designed with 2 different operativemodes:
• Kernel Mode:• the machine operates with critical data structure, directhardware (IN/OUT or memory mapped), direct memory,IRQ, DMA, and so on.
• User Mode:• users can run applications.
Usermode&Kernelmode(32bit)
Init|
Mdm|
Mdm|
gnome-terminal|
Bash|
App.exe
Vmlinux KernelAddressSpace
UserAddressSpace
0xC10000000xBFFFFFFF
0xFFFFFFFF
0x00000000
3G
1G
OperativeModes
• Kernel Mode "prevents" User Mode applications fromdamaging the system or its features.
•Modern microprocessors implement in hardware atleast 2 different states. For ex. under Intel, 4 statesdetermine the PL (Privilege Level). It is possible to use0,1,2,3 states, with 0 used in Kernel Mode.
• Linux/UnixOSrequiresonly2privilegelevels,andwewillusesuchaparadigmaspointofreference
SwitchingfromUserModetoKernelMode-1
• Whendoweswitch?• Onceweunderstandthatthereare2differentmodes,wehavetoknow
whenweswitchfromonetotheother.
• Typically,thereare2pointsofswitching:
1. WhencallingaSystemCall:2. WhenanIRQ(orexception)comes
Let’sobservetheuser/kernelspace
• usevmstat tograsprecentprocesscontext
Librarycallvs SystemCallinLinuxWrapperandWrapperbased
Systemcall,EntryandExitPointsExampleofwrappertoasystemcallflow
Systemcalls
• Themaininterfacebetweenthekernelanduserspace isthesetofsystemcalls
• About~300systemcallsthatprovidesthemainkernelservices• Fileanddeviceoperations,networkingoperations,inter-
processcommunication,processmanagement,memorymapping,timers,threads,synchronizationprimitives,etc.
• Thisinterfaceisstableovertime:onlynewsystemcallscanbeaddedbythekerneldevelopers
• ThissystemcallinterfaceiswrappedbytheClibrary,anduserspace applicationsusuallynevermakeasystemcalldirectlybutratherusethecorrespondingClibraryfunction
SystemCalls:read• Cexample:
count=read(fd,buffer,nbyte)
• pushparametersonstack
• calllibrarycode
• putsystemcallnumberinregister
• callkernel(TRAP)• kernelexaminessystemcallnumber• findsrequestedsystemcallhandler• executerequestedoperation
• returntolibraryandcleanup• increaseinstructionpointer• removeparametersfromstack
• resumeprocess
application
user spacekernel space
count = read (fd , buffer , nbytes)
read libraryprocedure
register
memory (stack) buffer
nbytesbufferfd
X (read)
system callhandler
X
sys_read()
KernelEntryandExit
Kernel
Devices
Library Code
System Call Interface
appapp
trap /interrupt
table
systemcalltable
schedulerboot
80h
exceptions(error traps)
interrupt devicedialog
trap
page faults
SystemCallsvs.LibraryCalls
•man2• historicalevolutionof#ofcalls
• Unix6e(~50),Solaris7(~250)• Linux2.0(~160),Linux2.2(~190),Linux2.4(~220)
• librarycallsvs.systemcallpossibilities:• librarycallneverinvokessystemcall• librarycallsometimesinvokessystemcall• librarycallalwaysinvokessystemcall• systemcallnotavailablevialibrary
• caninvokesystemcall“directly” viaassemblycode
CostofCrossingthe“KernelBarrier”
•Morethanaprocedurecall• Lessthanacontextswitch• Costs:
• Establishingkernelstack• Validatingparameters• Kernelmappedtouseraddressspace?
ImplementationExample:“Hello,world!”
.data#sectiondeclaration
msg:.string"Hello,world!\n"#ourdearstringlen =.- msg #lengthofourdearstring
.text #sectiondeclaration
#wemustexporttheentrypointtotheELFlinkeror.global_start#loader.Theyconventionallyrecognize_startastheir
#entrypoint.Useld -efootooverridethedefault._start:#writeourstringtostdout
movl $len,%edx #thirdargument:messagelengthmovl $msg,%ecx #secondargument:pointertomessagetowritemovl $1,%ebx#firstargument:filehandle(stdout)movl $4,%eax#systemcallnumber(sys_write)int $0x80#callkernel
#andexit
movl $0,%ebx#firstargument:exitcodemovl $1,%eax#systemcallnumber(sys_exit)int $0x80#callkernel
LinuxSystemCalls(1)
Invokedbyexecutingint $0x80.• Programmedexceptionvectornumber128.• CPUswitchestokernelmode&executesakernelfunction.
• Callingprocesspassessyscall number identifyingsystemcallineax register(onIntelprocessors).
• Syscallhandlerresponsiblefor:• Savingregistersonkernelmodestack.• Invokingsyscallserviceroutine.• Exitingbycallingret_from_sys_call().
ParameterPassing
• Onthe32-bitIntel80x86:• 6registersareusedtostoresyscall parameters.
• eax (syscall number).• ebx,ecx,edx,esi,edi storeparameterstosyscall serviceroutine,identifiedbysyscallnumber.
InteractingwithModules
App_1 App_2 App_N
/dev/device_nodes
Vmlinuxmodule.ko
Basicutility,filter&developercommandessentials
UnderstandingRootFileSystemHierarchy
LinuxFilesystemsondesktop
• HistoricallyLinuxhadnofs ofitsownandformallyhadminix fs runningonit
• Lateradpoted thethesecondextendedfilesystemformallyknownasext2fs
• Hasbeenenhancedtoext3fs,ext4fs• NowtheLinux3.XislookingtohaveBtrfs
• Btrfs isanewcopyonwritefilesystem forLinuxaimedatimplementingadvancedfeatureswhilefocusingonfaulttolerance,repairandeasyadministration
LinuxTreeHierarchy
/
/root /bin /proc /usr /sbin /dev /opt /home
/home/ram
/home/sita
/src
linux
/ - (unnamed) the actual root
/root – a home area for root
/bin – utility commands
/proc – a bogus fs for Linux kernel
/usr – usr utilities and application/sbin – special commands /privileged /dev – device node for external devices /opt – optional applications /home – usually all you do is here/var – spool , log messages etc
TheShell
• CommandinterpreterinoriginalUnix
• Readcommand• Perhapspre-processcommand
• Fork/execute• Returnexitstatusofcommand
• Alittlehistoryrevisited• BourneShell(sh)• Cchell• Korn shell• BourneAgainshell(bash)
Entrylevelcommands
• man• which• whatis• info• apropos
• date• cal• who• w• id• Write• mesg• bc
PiPEs (|)&FILTER
• ps• nice• sleep• at• nohup• kill• ctrl+c• ctrl+z• fg• bg• top• vmstat
• grep• sort• tr• cut• paste• more• less• head• tail• nl• tee• wc
PROCESS
FILEs/misc
• pwd• mkdir• cd• Ls• rmdir• cat• cp• ln• mv• rm
• chmod• chgrp• Chown• comm• cmp• diff• Uniq• touch• stat• file• compgen
DIR/FILE
CSCOPE
• Lookatthefunctiondefinitionsstraightwayfromthefunctioncall• Lookoutforalltheplacesfromwhereaparticularfunctionisbeing
called.• Searchingstringsandpatterns• Determiningsourceswhichincludeaspecificheader• Besides,itdoeseditortaskslikeviewingthesourcefile,replacing
strings,etc
CSCOPE• $cscope-help• Usage:cscope[-bcCdehklLqRTuUvV][-ffile][-Ffile][-ifile][-Idir][-sdir]
• [-pnumber][-Ppath][-[0-8]pattern][sourcefiles]
• -bBuildthecross-referenceonly.• -CIgnorelettercasewhensearching.• -cUseonlyASCIIcharactersinthecross-reffile(don’tcompress).
• -dDonotupdatethecross-reference.• -eSuppressthe-ecommandpromptbetweenfiles.
• -Fsymfile Readsymbolreferencelinesfromsymfile.
• -freffile Usereffile ascross-reffilenameinsteadofcscope.out.
• -hThishelpscreen.• -Iincdir Lookinincdir forany#includefiles.• -i namefile Browsethroughfileslistedinnamefile,insteadofcscope.files
• -kKernelMode– don’tuse/usr/includefor#includefiles.
-LDoasinglesearchwithline-orientedoutput.
-lLine-orientedinterface.
-num patternGotoinputfieldnum (countingfrom0)andfindpattern.
-PpathPrependpathtorelativefilenamesinpre-builtcross-reffile.
-pnDisplaythelastnfilepathcomponents.
-qBuildaninvertedindexforquicksymbolsearching.
-RRecurse directoriesforfiles.
-sdir Lookindir foradditionalsourcefiles.
-TUseonlythefirsteightcharacterstomatchagainstCsymbols.
-UCheckfiletimestamps.
-uUnconditionallybuildthecross-referencefile.
-vBemoreverboseinlinemode.
-VPrinttheversionnumber.
SHELL• Internalcommandsvs externalcommands• Variables- local,exported,ENV• Arithmetic- expr [+,- ,\*,/,%]• Controls
• if[expression];then;…else…fi• caseexpressionin;1);;2);;*);;esac
• Looping:• while[expression]do…done• until[expression]do…done• forindexinexpressiondo…done
• CommandLine• $1,…$9• shift• $*,$#,$$,• Set
• Funtion:• hidesthecommandline• mustassigntoshellvars
BuildingApplicationsonLinuxToolChainOverviewandComponent
Binutils,Kernel-header,GCC,Library,Debugger,Vendor&PreBuilt
Definition
• TheusualdevelopmenttoolsavailableonaGNU/Linuxworkstationisanativetoolchain
• Thistoolchainrunsonyourworkstationandgeneratescodeforyourworkstation,usuallyx86
• Forembeddedsystemdevelopment,itisusuallyimpossibleornotinterestingtouseanativetoolchain• Thetargetistoorestrictedintermsofstorageand/ormemory• Thetargetisveryslowcomparedtoyourworkstation• Youmaynotwanttoinstallalldevelopmenttoolsonyourtarget.
• Therefore,cross-compilingtoolchains aregenerallyused.Theyrunonyourworkstationbutgeneratecodeforyourtarget.
Environment
x86
ARMx86
Compilationmachine
Execution machine
Sourcecode
NativetoolchainCross-compiling
toolchain
x86binary ARMbinary
Components
Binutils Kernelheaders
C/C++libraries GCCcompiler
GDBdebugger(optional)
Binutils
• Binutils isasetoftoolstogenerateandmanipulatebinariesforagivenCPUarchitecture• as,theassembler,thatgeneratesbinarycodefrom
assemblersourcecode• ld,thelinker• ar, ranlib,togenerate.a archives,usedfor
libraries• objdump, readelf, size, nm, strings,
toinspectbinaries.Veryusefulanalysistools!• strip,tostripuselesspartsofbinariesinorderto
reducetheirsize• http://www.gnu.org/software/binutils/• GPLlicense
Kernelheaders(1)
• TheClibraryandcompiledprogramsneedstointeractwiththekernel• Availablesystemcallsandtheir
numbers• Constantdefinitions• Datastructures,etc.
• Therefore,compilingtheClibraryrequireskernelheaders,andmanyapplicationsalsorequirethem.
Kernel
Kernelheaders
CLibrary
Applications
• Available in <linux/...> and <asm/...> and a few other directories corresponding to the ones visible in include/ in the kernel sources
Kernelheaders(2)
• Systemcallnumbers,in<asm/unistd.h>
• Constant definitions, here in <asm-generic/fcntl.h>, included from <asm/fcntl.h>, included from <linux/fcntl.h>
• Data structures, here in <asm/stat.h>
#define __NR_exit 1#define __NR_fork 2#define __NR_read 3
#define O_RDWR 00000002
struct stat {unsigned long st_dev;unsigned long st_ino;
[...]};
Kernelheaders(3)
• Thekernel-to-userspace ABIisbackwardcompatible• Binariesgeneratedwithatoolchain usingkernelheadersolder
thantherunningkernelwillworkwithoutproblem,butwon'tbeabletousethenewsystemcalls,datastructures,etc.
• Binariesgeneratedwithatoolchain usingkernelheadersnewerthantherunningkernelmightworkoniftheydon'tusetherecentfeatures,otherwisetheywillbreak
• Usingthelatestkernelheadersisnotnecessary,unlessaccesstothenewkernelfeaturesisneeded
• Thekernelheadersareextractedfromthekernelsourcesusingtheheaders_install kernelMakefile target
GCCcompiler
• GNUCCompiler,thefamousfreesoftwarecompiler
• CancompileC,C++,Ada,Fortran,Java,Objective-C,Objective-C++,andgeneratecodeforalargenumberofCPUarchitectures,includingARM,AVR,Blackfin,CRIS,FRV,M32,MIPS,MN10300,PowerPC,SH,v850,i386,x86_64,IA64,Xtensa,etc.
• http://gcc.gnu.org/• AvailableundertheGPLlicense,
librariesundertheLGPL.
The image part with relationship ID rId4 was not found in the file.
Clibrary
• TheClibraryisanessentialcomponentofaLinuxsystem• Interfacebetweentheapplications
andthekernel• Providesthewell-knownstandardC
APItoeaseapplicationdevelopment
• SeveralClibrariesareavailable:glibc,uClibc,eglibc,dietlibc,newlib,etc.
• ThechoiceoftheClibrarymustbemadeatthetimeofthecross-compilingtoolchaingeneration,astheGCCcompileriscompiledagainstaspecificClibrary.
Kernel
CLibrary
Applications
glibc
• http://www.gnu.org/software/libc/• License:LGPL• ClibraryfromtheGNU project• Designedforperformance,standardscomplianceand
portability• FoundonallGNU/Linux hostsystems• Ofcourse,activelymaintained• Quitebigforsmallembeddedsystems:approx2.5MBonarm (version2.9- libc:1.5MB,libm:750KB)
The image part with relationship ID rId4 was not found in the file.
Using the Toolchain
build host target
Native buildused to build the normal gcc of a
workstation
build host target
Cross buildused to build a toolchain that runs on
your workstation but generates binaries for the target
The most common solution in embeddedThe most common solution in Enterprise
Experimentonfollowing
• Gcc ccompiler• Ld gnulinker• As assembler• Size infooncs/ds• Nm symbolicinfo(var/codecommnets/…)• Readelf infoonfile• Strip removethedebugginginfo• Objdump disassemblyinfo• Strace systemcalltrace• Objcopy exe/binay copy/conversion• Ar createarchive(libraray)• Ranlib indeding ofarchive• Make automatethebuild• Ldd infoonlibraryfortheexe• Ldconfig pathsedits
Somesupportingutilitiescanbealsotriedoutstat,file,diff,cmp,comm,patch,wc,nl,bc,script,sdiff,vimeditorwithcscope isagoodchoicefordevelopers
BuildLifecycleofanImage
SourceProgram
Preprocessorcpp / gcc -E
Compilercc1 / gcc -S
AssemblyCode Assembler
as / gcc -c
ObjectProgram
Linkerld / gcc -l
ExecutablesLoader
ApplicationBuildstyles
• DynamicBuild• defaulttheprocessisbuildwithdynamicdependencies.• Itmeans,itusessharedlibraryfortheexetoloadcertainserviceatloadtime.Example:$gcc sample.c –osample.exe$ldd ./sample.exeLinux-gate.so.1=>(0xb7776000)libc.so.6=>/lib/i386-linux-gnu/libc.so.6(0xb75a9000) /lib/ld-linux.so.2(0xb7777000)
• StaticBuild• Iftheprocessisbuildwithstaticbuild,thereisnodynamicdependency.• Itmeans,itusesstaticlibraryfortheexe.
• Thisisfastbutoccupiedalotofspace.static build
• $gcc -staticsample.c -ossample.exe
ExecutableImageonstaticanddynamicbuild
App1 App2 AppN
lib1.a lib1.a lib1.a
lib2.a lib2.a lib2.a
App1 App2 AppN
linux-gate.so.1
libc.so.6
ld-linux.so.2
Programmer’sviewofProgram
.stack
.heap
Environmentvariables
.bss
.text
.rodata
.data
_end_edata_etext
ExecutableandLinkerFormat
MaintainingSTPsStaticlibrary
DynamiclibraryRealname
LD_LIBRARY_PATHProductionreadywithldconfig
SonameandversioningtechniquesUsingdynamiclinkerasanAPIusinglibdl.so
LD_PRELOADDo’sandDon’tinSharedLibraries
LIBRARIES
• Staticlibraries• special“archivefiles”• haveextension’.a’• createdfromobjectfilesusingGNUarchiver ‘ar’• usedbylinkertoresolvereferencestofunctionsatcompile
time
• Sharedlibraries• haveextension’.so’• preferredoverstaticlibraries• Memoryoptimises• Canbebuildintwoways
• Realname• Soname
LIBRARIES
• GNUar tocreateastaticlibrary:$gcc -Wall-c-ohello.o hello.c
• $ar cr libhello.a hello.o• The‘cr’standsfor“createandreplace”
• The“tableofcontents”option‘t’canlisttheobjectfilesinanexistinglibrary
• $ar tlibhello.ahello_fn.obye_fn.o
• $gcc main.c /tmp/tmp/test/libhello.a -omain• $./main• Hello,everyone!
LIBRARIES
• /*calc.c */• #include<math.h>• #include<stdio.h>
• int main(void){
• doublex=sqrt (2.0);printf ("Thesquarerootof2.0is%f\n",x);
• return0;• }
LIBRARIES
• $gcc -Wallcalc.c -ocalc• /tmp/ccbR6Ojm.o:Infunction‘main’:• /tmp/ccbR6Ojm.o(.text+0x19):undefinedreferenceto‘sqrt’•
• ‘/tmp/ccbR60jm.o’isatemporaryobjectfilecreatedforlinking• Alibraryshouldappearafteranysourcefilesorobjectfiles
• $gcc -Wallcalc.c /usr/lib/libm.a -ocalc
LIBRARIES
• -lNAME willlinkobjectfileswithlibraryfile‘libNAME.a’inthestandardlibrarydirectories
• $gcc -Wallcalc.c -lm-ocalc• Iflibrarylglpk usesanddependsonlmlibrary,itmustappearbeforelm
• $gcc -Walldata.c -lglpk -lm• Notallcompilerssearchalllibraries,soorderlibrariesfromlefttoright
Libraries
•Alibraryisacollectionofsubprogramsusedtodevelopsoftware.•Allowscodeanddatatobereused,shared andchanged inamodularfashion.
•Linking:Alinkerresolvesthereferencesbetweenexecutables andlibraries.
BenefitsofUsingLibraries
•SoftwareEngineeringPerspective:• Increasingthereusability ofcommonroutines.•Easytoupgradebychanging thelibrariesonly.
•SystemUtilizationPerspective:•Thecodesegmentcanbesharingatruntime;decreasetheconsumeofmemoryanddiskspace.
GNUBinaryUtilitiesusedwithlib
• strings:displayallprintablecharacters.• ar:createstatic-linkinglibraries.• nm:listsymbolsfromobjectfiles.• size:listsectionsizesandtotalsizes.• readelf:• objdump:• ldd• ldconfig
PrintSharedLibraryDependencies
• UNIXPlatform> ldd /bin/bash
linux-gate.so.1 => (0xffffe000)
libncurses.so.5 => /lib/libncurses.so.5 (0xb7f1d000)
libdl.so.2 => /lib/libdl.so.2 (0xb7f19000)
libc.so.6 => /lib/libc.so.6 (0xb7dfb000)
/lib/ld-linux.so.2 (0xb7f61000)
• WindowsPlatform• PEViewerorPEExplorer• PE:PortableExecutable
Smallerismoreefficient
objjectfile
functionlibrary
objectfile
pointer
staticlinking
objectfile
functionlibrary
dynamiclinking
objectfile
pointer
sharedfunctionlibrary
executablefiles
FromSourcetoExecution
SourceProgram
Preprocessorcpp / gcc -E
Compilercc1 / gcc -S
AssemblyCode Assembler
as / gcc -c
ObjectProgram
Linkerld / gcc -l
ExecutablesLoader
static-linking
dynamic-linking
CategoriesofLibraries(bylinkingtime)
•Staticlinkinglibraries•Dynamiclinkinglibraries
•Run-TimeEnvironmentlibraries•ProgrammingLanguagelibraries
StaticLinkingLibraries
•Thecodesegmentswillbecopytoeachexecutables
•Pros:•Easytouse;nodependencyproblemaftercompilation.
•Cons:•Theexecutablesizewillbelarger.•Requirere-linkingwhenlibrarieschanged.
DynamicLinkingLibraries(1/2)
• Allowmultipleprocessestosharethesamecodesegment.• Pros:
• Greaterflexibility• Possiblesupportforplugins.
• Cons:• Slowapplicationatstarttime.• Dependentonthelibrarieswhenexecution.
DynamicLinkingLibraries(2/2)
•Thereferencescanberesolvedeitherat:•Load-time•Run-time
LocationofLibraries
•UNIXPlatform• /lib:runtimeenvironmentlibraries• /usr/lib:forprogramdevelopment
LinkingwithCRuntimeLibraries
•StaticLinking• gcc–static –ohello-shelloworld.c/usr/lib/libc.a• hello-s:521,238bytes
•DynamicLinking• gcc–ohello-dhelloworld.c• hello-d:6,020bytes
•TheCRTlibrariesconsume515,218bytes.
Static-LinkingLibraries
•Build•gcc –cmystaticlib.c•ar –rmystaticlib.a mystaticlib.o
•Usage• (sameasusingCruntimelibraries)
Createtheobjectmodulesusing–fPIC
• $gcc -fPIC -g-c-Wallmod1.cmod2.cmod3.c• -fPIC (PositionIndependentCode)causescompilertogeneratecodethatcanbeloadedanywhereinvirtualmemoryatrun-time.
• Createthesharedlibrary:• $gcc -shared-olibfoo.so mod1.omod2.omod3.o
• Usualnamingconvention:libname.so[.version]
• Linkprogramagainstlibrary:• $gcc -g-Wall-oprog prog.c libfoo.so
• or:• $gcc -g-Wall-oprog prog.c -L.-lfoo
• Thisstepembedsnameofsharedlibraryinsideexecutablefile.
Run?
• Butwhenwetrytoexecutetheprogram:
• $./prog• ./prog:errorinloadingsharedlibraries:• libfoo.so:cannotopensharedobjectfile:• Nosuchfileordirectory
• Thelibrary"libfoo.so"couldnotberesolvedatrun-timebythedynamiclinker(aka:dynamiclinkingloader).
TheDynamicLinker
• Examinessharedlibrarydependenciesinexecutablefileand• Loadssharedlibrariesintomemory(iftheyarenotalready);• ItisitselfasharedlibrarywhichispartofeveryELFexecutableusing
sharedlibraries:/lib/ld-linux.so• Itappliesasetofrulestofindlibrariesreferencedbytheprogram.• Therulesspecifyasetofstandarddirectories.
• (Forexample,manysharedlibrariesresidein/liband/usr/lib.)
• Theerrormessageearlieroccurredbecauseourlibraryisinthecurrentdirectory,whichisnotpartofstandardlist
LD_LIBRARY_PATH
• Weneedtogivethedynamiclinkerinformationonhowtofindoursharedlibraryatrun-time
• Onesolution:LD_LIBRARY_PATH.• Setthisenvironmentvariabletoalistofcolon-separateddirectories.• Dynamiclinkersearchesthesedirectoriesbeforelookinginstandardlibrarydirectories.
• $LD_LIBRARY_PATH=../prog• Calledmod1-x1• Calledmod2-x2
• Note:productionapplicationsshouldnotuseLD_LIBRARY_PATHas• Usersshouldnothavetoknowwherethelibraryislocated
Thesharedlibrarysoname
• Intheearlierexample,weembeddedtheactualname(therealname)ofthesharedlibraryinanexecutablefile.
• Itispossibletocreateanalias,calledthesoname,whichwillbeembeddedinanexecutablefileinsteadoftherealname.
• Atrun-time,thedynamiclinkerwillusethesoname whensearchingforthelibrary.
• Thepurposeofthesoname istoprovidealevelofindirection.• Atrun-time,executablecanuseaversionofthesharedlibrarythat
isdifferent(butcompatible)fromthatagainstwhichitwaslinked.
howtouseasoname
• Specifysoname whencreatingsharedlibrary:• $gcc -fPIC -c-Wall-gmod1.cmod2.cmod3.c• $gcc -shared-Wl,-soname,libbar.so -olibfoo.so \• mod1.omod2.omod3.o• -Wl,-soname,libbar.so instructslinkertomarkthesharedlibrarylibfoo.sowiththesoname libbar.so.
• Createexecutable:• $gcc -g-Wall-oprog prog.c libfoo.so
• Linkerdetectsthatlibfoo.so containsthesoname libbar.so andembedsthelatternameinsidetheexecutable.
Runtheprogram
• $LD_LIBRARY_PATH=. ./prog• ./prog:errorinloadingsharedlibraries:• libbar.so:cannotopensharedobjectfile:• Nosuchfileordirectory• Dynamiclinkercannotfindanythingnamedlibbar.so.
• Createasymboliclinkfromthesoname totherealnameofthelibrary:
• $ln -slibfoo.so libbar.so• $LD_LIBRARY_PATH=. ./prog
• Calledmod1-x1• Calledmod2-x2
• Atrun-timethislinkcanpointtoaversionofthelibrarywhichisdifferentfromtheversionagainstwhichlinkingwasperformed.
Sharedlibraryversionsandnaming
• Ifanewversionofasharedlibraryiscompatible withanexistinglibrary,wecanmakeanewminorversion ofthelibrary.
• Ifanewversionofasharedlibraryisincompatible withanexistinglibrary,wemustmakeanewmajorversion ofthelibrary.
• Constraint:itmustbepossibletocontinuerunningprogramsrequiringtheolderversionofthelibrary.
• Solution:anamingconvention isusedforsharedlibraryrealnamesandsonames.
RealnameREALNAMEisNameofthefilecontaininglibrarycode.Format:libname.so.major-id.minor-id
• Majorversionidentifierisanumberwhichissequentiallyincrementedwitheachincompatiblereleaseofthelibrary.
• Minorversionidentifierdistinguishesdifferentcompatibleminorversionsofalibrarywithinthesamemajorversion.
• Usuallyeitheranumber,ortwonumbersseparatedbyadot,withfirstnumberidentifyingminorversion,andsecondnumberindicatingapatchlevelorrevisionnumberwithintheminorversion.
Examples:• libdemo.so.1.0.1• libdemo.so.1.0.2• libdemo.so.2.0.1• libreadline.so.4.0
SonameFormat:libname.so.major-id• soname includesthesamemajorversionidentifier ascorrespondingrealname,
butdoesnotincludeminorversion identifier.
Purpose:run-timeloadingisdependentonlyonmajorversionnumberofthelibrary• soname iscreatedasasymboliclink (usuallyinsamedirectoryasrealname).• Thesoname foreachmajorlibraryversionpointstomostrecentminorversion.• Sinceitisthesoname (nottherealname)thatisembeddedinexecutableby
linker:Atanytime,wecanchangesoname symboliclinktopointtoanewerminorversion.
• Differentmajorversionscanco-exist andbeaccessedbytheprogramsthatrequirethem.
Examplesofsonames (alongwiththerealnamestowhichtheymightbesymbolicallylinked):
• libdemo.so.1->libdemo.so.1.0.2• libdemo.so.2->libdemo.so.2.0.1• libreadline.so.4->libreadline.so.4.0
Linkername
LinkernameFormat:libname.so
Purpose:allowsustoconstructversion-independentlinkcommands whichautomaticallyoperatewiththeright(i.e.mostuptodate)versionofthesharedlibrary.
Createdasasymboliclink toeitherrealname orsoname ofmostrecentmajorversionofthelibrary.
Moreconvenienttohaveitpointtosoname.
Examples:
libdemo.so ->libdemo.so.2Libreadline.so ->libreadline.so.4
createasharedlibraryusingstandardnamingconventions
Createthesharedlibrarywithrealnamelibdemo.so.1.0.1andsoname libdemo.so.1.$gcc -fPIC -g-c-Wallmod1.cmod2.cmod3.c$gcc -shared-Wl,-soname,libdemo.so.1-olibdemo.so.1.0.1\
mod1.omod2.omod3.oCreatesymboliclinksforthesoname andlinkername:$ln -slibdemo.so.1.0.1libdemo.so.1$ln -slibdemo.so.1libdemo.so$ls -llibdemo.so*|cut-c1-11,55- #Verifythesetup
lrwxrwxrwx libdemo.so ->libdemo.so.1lrwxrwxrwx libdemo.so.1->libdemo.so.1.0.1-rwxr-xr-xlibdemo.so.1.0.1
Buildexecutableusingthelinkername:$gcc -g-Wall-o./prog prog.c -L.-ldemoRuntheprogramasusual:$LD_LIBRARY_PATH=../progCalledmod1-x1Calledmod2-x2
DynamicloadingwithLinux
Function Descriptiondlopen Makesanobjectfileaccessibletoaprogramdlsym Obtainstheaddressofasymbolwithinadlopened objectfiledlerror Returnsastringerrorofthelasterrorthatoccurreddlclose Closesanobjectfile
Description
#include<dlfcn.h>
void*dlopen(const char*file,int mode);WithahandletotheELFobject,youcanidentifyaddressestosymbolswithinthisobjectusingthedlsym call.Thisfunctiontakesasymbolname,suchasthenameofafunctioncontainedwithintheobject.Thereturnvalueisaresolvedaddresstothesymbolwithintheobject:void *dlsym(void *restrict handle,const char *restrict name );IfanerroroccursduringacallwiththisAPI,youcanusethedlerror functiontoreturnahuman-readablestringrepresentingtheerror.ThisfunctionhasnoargumentsandreturnsastringifapriorerroroccurredorreturnsNULLifnoerroroccurred:char *dlerror();Finally,whennoadditionalcallstothesharedobjectarenecessary,theapplicationcancalldlclose toinformtheoperatingsystemthatthehandleandobjectreferencesarenolongernecessary.Thisisproperlyreference-counted,sothatmultipleusersofasharedobjectdonotconflictwithoneanother(itremainsinmemoryaslongasthereisauserforit).Anysymbolsresolvedthroughdlsym fortheclosedobjectwillnolongerbeavailable.char*dlclose(void*handle);
DynamicallyLoaded(DL)Libraries
• Dynamicallyloaded(DL)librariesarelibrariesthatareloadedattimesotherthanduringthestartupofaprogram.
• They'reparticularlyusefulforimplementingpluginsormodules,becausetheypermitwaitingtoloadthepluginuntilit'sneeded.
• Forexample,thePluggableAuthenticationModules(PAM)systemusesDLlibrariestopermitadministratorstoconfigureandreconfigureauthentication.
Usingreadelf forinfo
$ldd$readelf -ldl
ElffiletypeisEXEC(Executablefile)Entrypoint0x8048618Thereare7programheaders,startingatoffset52
ProgramHeaders:TypeOffsetVirtAddr PhysAddr FileSiz MemSiz Flg AlignPHDR0x0000340x080480340x080480340x000e00x000e0RE0x4INTERP0x0001140x080481140x080481140x000130x00013R0x1[Requesting programinterpreter:/lib/ld-linux.so.2]
LOAD0x0000000x080480000x080480000x009580x00958RE0x1000LOAD0x0009580x080499580x080499580x001200x00128RW0x1000DYNAMIC0x00096c0x0804996c0x0804996c0x000d00x000d0RW0x4NOTE0x0001280x080481280x080481280x000200x00020R0x4GNU_STACK0x0000000x000000000x000000000x000000x00000RW0x4
$
TestingEmbeddedApplication
QEMUCrossApplications
Qemu
Greattoolforembeddeddevelopers
Whatisqemu
• QuickEMUlator”isaprocessoremulatorthatreliesondynamicbinarytranslationtoachieveareasonablespeedwhilebeingeasytoporttonewhostCPUarchitectures• Whenusedasamachineemulator,QEMUcanrunOSes andprograms
madeforonemachine• EX:ARMboardonadifferentmachine(e.g.yourownPC).• Byusingdynamictranslation,itachievesverygoodperformance.
• Whenusedasavirtualizer,QEMUachievesnearnativeperformancesbyexecutingtheguestcodedirectlyonthehostCPU.• WhenusingKVM,QEMUcanvirtualizex86,serverandembedded
PowerPC,andS390guests.
TypicalSetup
GetQEMUConfigureQEMUforKVM
Build&InstallTesttheQEMU
GettoolchainConfiguretoolchain
Build&InstallTesttoolchain
Requirement/BoardspecificconfigurationConfigure,build&testyourkernelConfigure,build,&testfilesystem
Build&testbareapps
1 2
3
TestApplicationonemulator
• arm-none-linux-gnueabi-gcc sample.c –osample.arm
• Qemu-arm–L/path_to_cross_sdk/arm…/libc/libsample.arm