picolibc A C Library for Smaller Systems Keith Packard -9 Senior Principal Technologist Amazon [email protected] c Hello !y name is Keith Packard" # $ork in the De&ice 'S gro(p at Amazon as a Senior Principal )ngineer. Today" #*m going to talk abo(t picolibc" a C library designed for embedded +,- and ./- bit microcontrollers. picolibc ● 0hy start another C library pro1ect2 ● How picolibc $as de&eloped? ● 0hat are the results2 ● 0ho is using picolibc2 ● 0here is picolibc going in the future2 #n this presentation" #*ll describe $hat # see as the re3(irements for an embedded C library" the pieces from $hich picolibc $as b(ilt" ho$ de&elopment progressed" res(lts of that de&elopment along $ith se&eral pro1ects that ha&e picolibc s(pport integrated into them. Finally" #*ll brie5y to(ch on f(t(re $ork that # hope to see done in picolibc. Small System Considerations ● Small !emory 6think k7" not 879 – RA! is more constrained than :'! – A&oid using the heap ● Limited 5oating point – !ay have only +,-bit 5oats – !ay have none at all ● 8etting started is hard Small systems are small. They can be really small. The embedded hard$are # 5y (ses systems $ith +,k7 to ;,<k7 or :'! and /k7 to +,k7 of :A!. They often (se lo$-performance +,-bit cores" some have a +,-bit 4P=" b(t many ha&e no 4PU at all. De&elopers $riting long-r(nning embedded soft$are often $ant to a&oid depending on dynamic allocation as it*s easist to pro&e malloc $ill ne&er fail if malloc is not linked into the application. As a developer" one of the steepest learning c(rves $ith embedded de&elopment is getting the very >rst *hello-$orld* application compiled" linked" installed and r(nning. Ard(ino has had tremendo(s s(ccess by making that process easy eno(gh for me to teach to middle school kids. )mbedded Libc Needs ● String 4(nctions 6strcpy" memset9 – Ideally accelerated for architect(re ● !ath Functions 6acos" 1@" sqrt9 – 'ften for soft-5oat processors ● Stdio 6printf" sscanf9 – deb(gging – data eAchange ● Licensing – Share-alike licenses are challenging )mbedded systems generally make modest demands on the C library. The most commonly (sed f(nctions are string and memory AP#sB things like strcpy" memmove and the like. Applications doing comp(tation may need some math f(nctions" like s3rt" sin or e&en bessel f(nctions. Standard #C' $ill often be only (sed d(ring deb(gging to send o(tp(t to a serial console to help diagnose soft$are and hard$are iss(es. Ho$ever" applications may also (se these f(nctions to handle DSO?" EAML or other teAt interchange formats and so strict standards conformance can be critical. While # am a strong proponent of share-alike licenses" a C library needs a pragmatic approach to be (sef(l to all de&elopers. A clear !#T or 7SD license is best practice in this space. Developer First Steps ● Startup code – #nitialize RA!" setup clocks and interrupts ● Linker script – !emory layout ● %ebug output – Serial ports require dri&ers and $orking clocks To make starting $ith a ne$ embedded platform easier" de&elopers $ill bene>t from eAtra s(pport in the C library. First" the processor" memory and library need to be initialized. Second" the linker needs to kno$ ho$ to lay o(t the application in :'! and :AM. This incl(des hard$are-speci>c information" processor set(p instr(ctions and library initialization code. 4inally" providing deb(g o(tp(t from applications (sing semihosting or other architect(re-speci>c mechanism lets the de&eloper see deb(gging information before clocks and serial port dri&ers are $orking. With these three tools" a ne$ SoC can be bro(ght (p only kno$ing the memory layo(t of the device as $e*ll see later in some eAamples. Current 32-bit Libc Options ● ne$lib and ne$lib-nano – excellent AP# s(pport" incl(ding optimized assembly code and solid math f(nctions. – designed for systems $ith an 'S – development focused on Cyg$in target – libgloss $raps 'S f(nctions into P'SIF-like AP# – stdio is fast" b(t large and malloc-intensive ● vario(s proprietary options – closed so(rce – often bro(ght (p from <-bit environments When # started doing embedded development in the modern era in ,@@G" # $as (sing an <@H; SoC. The compiler" SDCC" came $ith a small C library so # didn*t ha&e to think abo(t it. #n ,@;," $hen $e b(ild o(r >rst corteA m+ board" # had to also >nd a ne$ C library. 4or this pro1ect" ne$lib $as too big and re3(ired too many s(pport ro(tines in the form of libgloss" incl(ding sbrk for malloc" $hich $as re3(ired for stdio. # initially settled on pdclib as that $as at least reasonably small. Ho$e&er" it (sed eAcessi&e amo(nts of stack space for printf" and had no math s(pport. This library seems to be an eAperiment in b(ilding a small C library" b(t not in b(ilding a C library for small systems. Some SoC &endors pro&ide proprietary C libraries. These are often ports from older <- and ;.- bit systems $hich can lack compliance $ith modern C standards in some areas. And" of co(rse" they*re not free soft$are" $hich makes them (ninteresting to me. After a co(ple of years (sing pdclib" # decided to ret(rn to ne$lib and see $hat co(ld be done to make that (sable for my $ork. newlib concerns ● Stdio – A lot of code and (ses a lot of :A! – :e3(ires a large n(mber of P'S#F(ish) AP#s ● Libgloss – A shim bet$een the P'S#F apis and the (nderlying 'S – ?ot rele&ant on embedded systems $ith no 'S ● struct Ireent – holds all per-thread state in the library – e&en for f(nctions yo( don*t (se – an (neApected malloc (ser ● 'ut-dated coding practice – ne$lib still has K&: f(nction de>nitions in places 6 29 – !any signedC(nsigned conf(sions" especially in $charIt and stdio – !any (many) $arnings ● ?o integrated test suite – 0itho(t testing" there*s no $ay to kno$ $hen things break # $ant to say first that newlib is a great pro1ectB the code is easy to work on" the comm(nity is welcoming and s(pportive and #*ve been happy to collaborate with them. )verything here relates to (sing newlib for my small embedded pro1ects" which is not what newlib is really designed for. #n looking at the changes # wanted to make" the first thing on my list $as stdio. ?ewlib*s stdio is designed to offer good performance and f(ll compatibility on systems with an (nderlying P'SIX api. #t has internal b(ffering" and allocates space on the heap for them. :eplacing the whole thing seemed like the best option. # also $anted to get rid of the libgloss 'S shim layer. libgloss was originally designed as an interface between newlib and whatever (nderlying operating system it $as r(n on. With bare-metal environments" there is no 'S, and so there really isn*t a reason to have this library any more. ?ewlib (ses P'SIF interfaces internally" porting to an :T'S is (s(ally a simple matter of making s(re the necessary P'SIX interfaces are available. Fort(nately" most of the common (ses for libgloss AP#s were in stdioB by replacing that" the b(lk of the problem was already solved. 'ne h(ge cons(mer of :A! in newlib is *str(ct _reent*. This str(ct holds all per-thread state for the entire library. :eplacing this with modern thread local storage techni3(es wo(ld free (p a lot of memory $hile still allowing applications to take advantage of library AP#s which needed persistent state. ?ewlib is an old code base that has seen de&elopment eAtend back to the pre-A?SI era of compilers. The C lang(age has changed a lot since then" and how we (se the lang(age has changed even more. #t*s been a long time since # defined a new AP# with a bare *int* typeB we know now that it*s best to (se a more speci>c type that won*t s(rprise yo( with o&erflow iss(es or sign errors on some ne$ platform. At a minim(m" the library sho(ld compile cleanly with -0all -0eAtra. Finally" it*s (nreasonable to expect code review alone to catch errors in changes to the library. An extensive and a(tomated test s(ite is table stakes for a pro1ect of this size. # got started hacking on the stdio changes that # wanted to make and sent them in to the newlib mailing list to see what feedback # might get. That was when the reality of what newlib was hit me. ?ewlib is not a library for embedded applications. ?ewlib is the Cygwin C library which happens to be (sed by many embedded developers lacking an alternative. #n partic(lar" radical changes to the AP# and A7# of the library co(ld never be integrated as those wo(ld break binary compatibility for Cygwin applications. So # decided to fork newlib and start a new pro1ect. picolibc ● newlib math" i;<n" strings – good performance" $ide s(pport ● stdio adapted from AK: libc ● *str(ct _reent* removed – All thread-local &al(es (se toolchain TLS ● sample start(p code" linker scripts ● semihosting s(pport ● 4ix $arnings" switch to C;< – 7(ilds $ith --stdLc;< -0all -0eAtra -0error ● ?e$ meson-based b(ild system ● Testing ● P(re 7SD licensed library code.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages38 Page
-
File Size-