elf.c 127 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919
  1. /* elf.c -- Get debug data from an ELF file for backtraces.
  2. Copyright (C) 2012-2022 Free Software Foundation, Inc.
  3. Written by Ian Lance Taylor, Google.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are
  6. met:
  7. (1) Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. (2) Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in
  11. the documentation and/or other materials provided with the
  12. distribution.
  13. (3) The name of the author may not be used to
  14. endorse or promote products derived from this software without
  15. specific prior written permission.
  16. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  17. IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
  20. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  23. HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  24. STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  25. IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  26. POSSIBILITY OF SUCH DAMAGE. */
  27. #include "config.h"
  28. #include <errno.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <sys/types.h>
  32. #include <sys/stat.h>
  33. #include <unistd.h>
  34. #ifdef HAVE_DL_ITERATE_PHDR
  35. #include <link.h>
  36. #endif
  37. #include "backtrace.h"
  38. #include "internal.h"
  39. #ifndef S_ISLNK
  40. #ifndef S_IFLNK
  41. #define S_IFLNK 0120000
  42. #endif
  43. #ifndef S_IFMT
  44. #define S_IFMT 0170000
  45. #endif
  46. #define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
  47. #endif
  48. #ifndef __GNUC__
  49. #define __builtin_prefetch(p, r, l)
  50. #define unlikely(x) (x)
  51. #else
  52. #define unlikely(x) __builtin_expect(!!(x), 0)
  53. #endif
  54. #if !defined(HAVE_DECL_STRNLEN) || !HAVE_DECL_STRNLEN
  55. /* If strnlen is not declared, provide our own version. */
  56. static size_t
  57. xstrnlen (const char *s, size_t maxlen)
  58. {
  59. size_t i;
  60. for (i = 0; i < maxlen; ++i)
  61. if (s[i] == '\0')
  62. break;
  63. return i;
  64. }
  65. #define strnlen xstrnlen
  66. #endif
  67. #ifndef HAVE_LSTAT
  68. /* Dummy version of lstat for systems that don't have it. */
  69. static int
  70. xlstat (const char *path ATTRIBUTE_UNUSED, struct stat *st ATTRIBUTE_UNUSED)
  71. {
  72. return -1;
  73. }
  74. #define lstat xlstat
  75. #endif
  76. #ifndef HAVE_READLINK
  77. /* Dummy version of readlink for systems that don't have it. */
  78. static ssize_t
  79. xreadlink (const char *path ATTRIBUTE_UNUSED, char *buf ATTRIBUTE_UNUSED,
  80. size_t bufsz ATTRIBUTE_UNUSED)
  81. {
  82. return -1;
  83. }
  84. #define readlink xreadlink
  85. #endif
  86. #ifndef HAVE_DL_ITERATE_PHDR
  87. /* Dummy version of dl_iterate_phdr for systems that don't have it. */
  88. #define dl_phdr_info x_dl_phdr_info
  89. #define dl_iterate_phdr x_dl_iterate_phdr
  90. struct dl_phdr_info
  91. {
  92. uintptr_t dlpi_addr;
  93. const char *dlpi_name;
  94. };
  95. static int
  96. dl_iterate_phdr (int (*callback) (struct dl_phdr_info *,
  97. size_t, void *) ATTRIBUTE_UNUSED,
  98. void *data ATTRIBUTE_UNUSED)
  99. {
  100. return 0;
  101. }
  102. #endif /* ! defined (HAVE_DL_ITERATE_PHDR) */
  103. /* The configure script must tell us whether we are 32-bit or 64-bit
  104. ELF. We could make this code test and support either possibility,
  105. but there is no point. This code only works for the currently
  106. running executable, which means that we know the ELF mode at
  107. configure time. */
  108. #if BACKTRACE_ELF_SIZE != 32 && BACKTRACE_ELF_SIZE != 64
  109. #error "Unknown BACKTRACE_ELF_SIZE"
  110. #endif
  111. /* <link.h> might #include <elf.h> which might define our constants
  112. with slightly different values. Undefine them to be safe. */
  113. #undef EI_NIDENT
  114. #undef EI_MAG0
  115. #undef EI_MAG1
  116. #undef EI_MAG2
  117. #undef EI_MAG3
  118. #undef EI_CLASS
  119. #undef EI_DATA
  120. #undef EI_VERSION
  121. #undef ELF_MAG0
  122. #undef ELF_MAG1
  123. #undef ELF_MAG2
  124. #undef ELF_MAG3
  125. #undef ELFCLASS32
  126. #undef ELFCLASS64
  127. #undef ELFDATA2LSB
  128. #undef ELFDATA2MSB
  129. #undef EV_CURRENT
  130. #undef ET_DYN
  131. #undef EM_PPC64
  132. #undef EF_PPC64_ABI
  133. #undef SHN_LORESERVE
  134. #undef SHN_XINDEX
  135. #undef SHN_UNDEF
  136. #undef SHT_PROGBITS
  137. #undef SHT_SYMTAB
  138. #undef SHT_STRTAB
  139. #undef SHT_DYNSYM
  140. #undef SHF_COMPRESSED
  141. #undef STT_OBJECT
  142. #undef STT_FUNC
  143. #undef NT_GNU_BUILD_ID
  144. #undef ELFCOMPRESS_ZLIB
  145. /* Basic types. */
  146. typedef uint16_t b_elf_half; /* Elf_Half. */
  147. typedef uint32_t b_elf_word; /* Elf_Word. */
  148. typedef int32_t b_elf_sword; /* Elf_Sword. */
  149. #if BACKTRACE_ELF_SIZE == 32
  150. typedef uint32_t b_elf_addr; /* Elf_Addr. */
  151. typedef uint32_t b_elf_off; /* Elf_Off. */
  152. typedef uint32_t b_elf_wxword; /* 32-bit Elf_Word, 64-bit ELF_Xword. */
  153. #else
  154. typedef uint64_t b_elf_addr; /* Elf_Addr. */
  155. typedef uint64_t b_elf_off; /* Elf_Off. */
  156. typedef uint64_t b_elf_xword; /* Elf_Xword. */
  157. typedef int64_t b_elf_sxword; /* Elf_Sxword. */
  158. typedef uint64_t b_elf_wxword; /* 32-bit Elf_Word, 64-bit ELF_Xword. */
  159. #endif
  160. /* Data structures and associated constants. */
  161. #define EI_NIDENT 16
  162. typedef struct {
  163. unsigned char e_ident[EI_NIDENT]; /* ELF "magic number" */
  164. b_elf_half e_type; /* Identifies object file type */
  165. b_elf_half e_machine; /* Specifies required architecture */
  166. b_elf_word e_version; /* Identifies object file version */
  167. b_elf_addr e_entry; /* Entry point virtual address */
  168. b_elf_off e_phoff; /* Program header table file offset */
  169. b_elf_off e_shoff; /* Section header table file offset */
  170. b_elf_word e_flags; /* Processor-specific flags */
  171. b_elf_half e_ehsize; /* ELF header size in bytes */
  172. b_elf_half e_phentsize; /* Program header table entry size */
  173. b_elf_half e_phnum; /* Program header table entry count */
  174. b_elf_half e_shentsize; /* Section header table entry size */
  175. b_elf_half e_shnum; /* Section header table entry count */
  176. b_elf_half e_shstrndx; /* Section header string table index */
  177. } b_elf_ehdr; /* Elf_Ehdr. */
  178. #define EI_MAG0 0
  179. #define EI_MAG1 1
  180. #define EI_MAG2 2
  181. #define EI_MAG3 3
  182. #define EI_CLASS 4
  183. #define EI_DATA 5
  184. #define EI_VERSION 6
  185. #define ELFMAG0 0x7f
  186. #define ELFMAG1 'E'
  187. #define ELFMAG2 'L'
  188. #define ELFMAG3 'F'
  189. #define ELFCLASS32 1
  190. #define ELFCLASS64 2
  191. #define ELFDATA2LSB 1
  192. #define ELFDATA2MSB 2
  193. #define EV_CURRENT 1
  194. #define ET_DYN 3
  195. #define EM_PPC64 21
  196. #define EF_PPC64_ABI 3
  197. typedef struct {
  198. b_elf_word sh_name; /* Section name, index in string tbl */
  199. b_elf_word sh_type; /* Type of section */
  200. b_elf_wxword sh_flags; /* Miscellaneous section attributes */
  201. b_elf_addr sh_addr; /* Section virtual addr at execution */
  202. b_elf_off sh_offset; /* Section file offset */
  203. b_elf_wxword sh_size; /* Size of section in bytes */
  204. b_elf_word sh_link; /* Index of another section */
  205. b_elf_word sh_info; /* Additional section information */
  206. b_elf_wxword sh_addralign; /* Section alignment */
  207. b_elf_wxword sh_entsize; /* Entry size if section holds table */
  208. } b_elf_shdr; /* Elf_Shdr. */
  209. #define SHN_UNDEF 0x0000 /* Undefined section */
  210. #define SHN_LORESERVE 0xFF00 /* Begin range of reserved indices */
  211. #define SHN_XINDEX 0xFFFF /* Section index is held elsewhere */
  212. #define SHT_PROGBITS 1
  213. #define SHT_SYMTAB 2
  214. #define SHT_STRTAB 3
  215. #define SHT_DYNSYM 11
  216. #define SHF_COMPRESSED 0x800
  217. #if BACKTRACE_ELF_SIZE == 32
  218. typedef struct
  219. {
  220. b_elf_word st_name; /* Symbol name, index in string tbl */
  221. b_elf_addr st_value; /* Symbol value */
  222. b_elf_word st_size; /* Symbol size */
  223. unsigned char st_info; /* Symbol binding and type */
  224. unsigned char st_other; /* Visibility and other data */
  225. b_elf_half st_shndx; /* Symbol section index */
  226. } b_elf_sym; /* Elf_Sym. */
  227. #else /* BACKTRACE_ELF_SIZE != 32 */
  228. typedef struct
  229. {
  230. b_elf_word st_name; /* Symbol name, index in string tbl */
  231. unsigned char st_info; /* Symbol binding and type */
  232. unsigned char st_other; /* Visibility and other data */
  233. b_elf_half st_shndx; /* Symbol section index */
  234. b_elf_addr st_value; /* Symbol value */
  235. b_elf_xword st_size; /* Symbol size */
  236. } b_elf_sym; /* Elf_Sym. */
  237. #endif /* BACKTRACE_ELF_SIZE != 32 */
  238. #define STT_OBJECT 1
  239. #define STT_FUNC 2
  240. typedef struct
  241. {
  242. uint32_t namesz;
  243. uint32_t descsz;
  244. uint32_t type;
  245. char name[1];
  246. } b_elf_note;
  247. #define NT_GNU_BUILD_ID 3
  248. #if BACKTRACE_ELF_SIZE == 32
  249. typedef struct
  250. {
  251. b_elf_word ch_type; /* Compresstion algorithm */
  252. b_elf_word ch_size; /* Uncompressed size */
  253. b_elf_word ch_addralign; /* Alignment for uncompressed data */
  254. } b_elf_chdr; /* Elf_Chdr */
  255. #else /* BACKTRACE_ELF_SIZE != 32 */
  256. typedef struct
  257. {
  258. b_elf_word ch_type; /* Compression algorithm */
  259. b_elf_word ch_reserved; /* Reserved */
  260. b_elf_xword ch_size; /* Uncompressed size */
  261. b_elf_xword ch_addralign; /* Alignment for uncompressed data */
  262. } b_elf_chdr; /* Elf_Chdr */
  263. #endif /* BACKTRACE_ELF_SIZE != 32 */
  264. #define ELFCOMPRESS_ZLIB 1
  265. /* Names of sections, indexed by enum dwarf_section in internal.h. */
  266. static const char * const dwarf_section_names[DEBUG_MAX] =
  267. {
  268. ".debug_info",
  269. ".debug_line",
  270. ".debug_abbrev",
  271. ".debug_ranges",
  272. ".debug_str",
  273. ".debug_addr",
  274. ".debug_str_offsets",
  275. ".debug_line_str",
  276. ".debug_rnglists"
  277. };
  278. /* Information we gather for the sections we care about. */
  279. struct debug_section_info
  280. {
  281. /* Section file offset. */
  282. off_t offset;
  283. /* Section size. */
  284. size_t size;
  285. /* Section contents, after read from file. */
  286. const unsigned char *data;
  287. /* Whether the SHF_COMPRESSED flag is set for the section. */
  288. int compressed;
  289. };
  290. /* Information we keep for an ELF symbol. */
  291. struct elf_symbol
  292. {
  293. /* The name of the symbol. */
  294. const char *name;
  295. /* The address of the symbol. */
  296. uintptr_t address;
  297. /* The size of the symbol. */
  298. size_t size;
  299. };
  300. /* Information to pass to elf_syminfo. */
  301. struct elf_syminfo_data
  302. {
  303. /* Symbols for the next module. */
  304. struct elf_syminfo_data *next;
  305. /* The ELF symbols, sorted by address. */
  306. struct elf_symbol *symbols;
  307. /* The number of symbols. */
  308. size_t count;
  309. };
  310. /* A view that works for either a file or memory. */
  311. struct elf_view
  312. {
  313. struct backtrace_view view;
  314. int release; /* If non-zero, must call backtrace_release_view. */
  315. };
  316. /* Information about PowerPC64 ELFv1 .opd section. */
  317. struct elf_ppc64_opd_data
  318. {
  319. /* Address of the .opd section. */
  320. b_elf_addr addr;
  321. /* Section data. */
  322. const char *data;
  323. /* Size of the .opd section. */
  324. size_t size;
  325. /* Corresponding section view. */
  326. struct elf_view view;
  327. };
  328. /* Create a view of SIZE bytes from DESCRIPTOR/MEMORY at OFFSET. */
  329. static int
  330. elf_get_view (struct backtrace_state *state, int descriptor,
  331. const unsigned char *memory, size_t memory_size, off_t offset,
  332. uint64_t size, backtrace_error_callback error_callback,
  333. void *data, struct elf_view *view)
  334. {
  335. if (memory == NULL)
  336. {
  337. view->release = 1;
  338. return backtrace_get_view (state, descriptor, offset, size,
  339. error_callback, data, &view->view);
  340. }
  341. else
  342. {
  343. if ((uint64_t) offset + size > (uint64_t) memory_size)
  344. {
  345. error_callback (data, "out of range for in-memory file", 0);
  346. return 0;
  347. }
  348. view->view.data = (const void *) (memory + offset);
  349. view->view.base = NULL;
  350. view->view.len = size;
  351. view->release = 0;
  352. return 1;
  353. }
  354. }
  355. /* Release a view read by elf_get_view. */
  356. static void
  357. elf_release_view (struct backtrace_state *state, struct elf_view *view,
  358. backtrace_error_callback error_callback, void *data)
  359. {
  360. if (view->release)
  361. backtrace_release_view (state, &view->view, error_callback, data);
  362. }
  363. /* Compute the CRC-32 of BUF/LEN. This uses the CRC used for
  364. .gnu_debuglink files. */
  365. static uint32_t
  366. elf_crc32 (uint32_t crc, const unsigned char *buf, size_t len)
  367. {
  368. static const uint32_t crc32_table[256] =
  369. {
  370. 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419,
  371. 0x706af48f, 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4,
  372. 0xe0d5e91e, 0x97d2d988, 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07,
  373. 0x90bf1d91, 0x1db71064, 0x6ab020f2, 0xf3b97148, 0x84be41de,
  374. 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7, 0x136c9856,
  375. 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9,
  376. 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4,
  377. 0xa2677172, 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b,
  378. 0x35b5a8fa, 0x42b2986c, 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3,
  379. 0x45df5c75, 0xdcd60dcf, 0xabd13d59, 0x26d930ac, 0x51de003a,
  380. 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, 0xcfba9599,
  381. 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
  382. 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190,
  383. 0x01db7106, 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f,
  384. 0x9fbfe4a5, 0xe8b8d433, 0x7807c9a2, 0x0f00f934, 0x9609a88e,
  385. 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d, 0x91646c97, 0xe6635c01,
  386. 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, 0x6c0695ed,
  387. 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950,
  388. 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3,
  389. 0xfbd44c65, 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2,
  390. 0x4adfa541, 0x3dd895d7, 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a,
  391. 0x346ed9fc, 0xad678846, 0xda60b8d0, 0x44042d73, 0x33031de5,
  392. 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa, 0xbe0b1010,
  393. 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
  394. 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17,
  395. 0x2eb40d81, 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6,
  396. 0x03b6e20c, 0x74b1d29a, 0xead54739, 0x9dd277af, 0x04db2615,
  397. 0x73dc1683, 0xe3630b12, 0x94643b84, 0x0d6d6a3e, 0x7a6a5aa8,
  398. 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1, 0xf00f9344,
  399. 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb,
  400. 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a,
  401. 0x67dd4acc, 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5,
  402. 0xd6d6a3e8, 0xa1d1937e, 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1,
  403. 0xa6bc5767, 0x3fb506dd, 0x48b2364b, 0xd80d2bda, 0xaf0a1b4c,
  404. 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, 0x316e8eef,
  405. 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
  406. 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe,
  407. 0xb2bd0b28, 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31,
  408. 0x2cd99e8b, 0x5bdeae1d, 0x9b64c2b0, 0xec63f226, 0x756aa39c,
  409. 0x026d930a, 0x9c0906a9, 0xeb0e363f, 0x72076785, 0x05005713,
  410. 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, 0x92d28e9b,
  411. 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242,
  412. 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1,
  413. 0x18b74777, 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c,
  414. 0x8f659eff, 0xf862ae69, 0x616bffd3, 0x166ccf45, 0xa00ae278,
  415. 0xd70dd2ee, 0x4e048354, 0x3903b3c2, 0xa7672661, 0xd06016f7,
  416. 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc, 0x40df0b66,
  417. 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
  418. 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605,
  419. 0xcdd70693, 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8,
  420. 0x5d681b02, 0x2a6f2b94, 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b,
  421. 0x2d02ef8d
  422. };
  423. const unsigned char *end;
  424. crc = ~crc;
  425. for (end = buf + len; buf < end; ++ buf)
  426. crc = crc32_table[(crc ^ *buf) & 0xff] ^ (crc >> 8);
  427. return ~crc;
  428. }
  429. /* Return the CRC-32 of the entire file open at DESCRIPTOR. */
  430. static uint32_t
  431. elf_crc32_file (struct backtrace_state *state, int descriptor,
  432. backtrace_error_callback error_callback, void *data)
  433. {
  434. struct stat st;
  435. struct backtrace_view file_view;
  436. uint32_t ret;
  437. if (fstat (descriptor, &st) < 0)
  438. {
  439. error_callback (data, "fstat", errno);
  440. return 0;
  441. }
  442. if (!backtrace_get_view (state, descriptor, 0, st.st_size, error_callback,
  443. data, &file_view))
  444. return 0;
  445. ret = elf_crc32 (0, (const unsigned char *) file_view.data, st.st_size);
  446. backtrace_release_view (state, &file_view, error_callback, data);
  447. return ret;
  448. }
  449. /* A dummy callback function used when we can't find a symbol
  450. table. */
  451. static void
  452. elf_nosyms (struct backtrace_state *state ATTRIBUTE_UNUSED,
  453. uintptr_t addr ATTRIBUTE_UNUSED,
  454. backtrace_syminfo_callback callback ATTRIBUTE_UNUSED,
  455. backtrace_error_callback error_callback, void *data)
  456. {
  457. error_callback (data, "no symbol table in ELF executable", -1);
  458. }
  459. /* A callback function used when we can't find any debug info. */
  460. static int
  461. elf_nodebug (struct backtrace_state *state, uintptr_t pc,
  462. backtrace_full_callback callback,
  463. backtrace_error_callback error_callback, void *data)
  464. {
  465. if (state->syminfo_fn != NULL && state->syminfo_fn != elf_nosyms)
  466. {
  467. struct backtrace_call_full bdata;
  468. /* Fetch symbol information so that we can least get the
  469. function name. */
  470. bdata.full_callback = callback;
  471. bdata.full_error_callback = error_callback;
  472. bdata.full_data = data;
  473. bdata.ret = 0;
  474. state->syminfo_fn (state, pc, backtrace_syminfo_to_full_callback,
  475. backtrace_syminfo_to_full_error_callback, &bdata);
  476. return bdata.ret;
  477. }
  478. error_callback (data, "no debug info in ELF executable", -1);
  479. return 0;
  480. }
  481. /* Compare struct elf_symbol for qsort. */
  482. static int
  483. elf_symbol_compare (const void *v1, const void *v2)
  484. {
  485. const struct elf_symbol *e1 = (const struct elf_symbol *) v1;
  486. const struct elf_symbol *e2 = (const struct elf_symbol *) v2;
  487. if (e1->address < e2->address)
  488. return -1;
  489. else if (e1->address > e2->address)
  490. return 1;
  491. else
  492. return 0;
  493. }
  494. /* Compare an ADDR against an elf_symbol for bsearch. We allocate one
  495. extra entry in the array so that this can look safely at the next
  496. entry. */
  497. static int
  498. elf_symbol_search (const void *vkey, const void *ventry)
  499. {
  500. const uintptr_t *key = (const uintptr_t *) vkey;
  501. const struct elf_symbol *entry = (const struct elf_symbol *) ventry;
  502. uintptr_t addr;
  503. addr = *key;
  504. if (addr < entry->address)
  505. return -1;
  506. else if (addr >= entry->address + entry->size)
  507. return 1;
  508. else
  509. return 0;
  510. }
  511. /* Initialize the symbol table info for elf_syminfo. */
  512. static int
  513. elf_initialize_syminfo (struct backtrace_state *state,
  514. uintptr_t base_address,
  515. const unsigned char *symtab_data, size_t symtab_size,
  516. const unsigned char *strtab, size_t strtab_size,
  517. backtrace_error_callback error_callback,
  518. void *data, struct elf_syminfo_data *sdata,
  519. struct elf_ppc64_opd_data *opd)
  520. {
  521. size_t sym_count;
  522. const b_elf_sym *sym;
  523. size_t elf_symbol_count;
  524. size_t elf_symbol_size;
  525. struct elf_symbol *elf_symbols;
  526. size_t i;
  527. unsigned int j;
  528. sym_count = symtab_size / sizeof (b_elf_sym);
  529. /* We only care about function symbols. Count them. */
  530. sym = (const b_elf_sym *) symtab_data;
  531. elf_symbol_count = 0;
  532. for (i = 0; i < sym_count; ++i, ++sym)
  533. {
  534. int info;
  535. info = sym->st_info & 0xf;
  536. if ((info == STT_FUNC || info == STT_OBJECT)
  537. && sym->st_shndx != SHN_UNDEF)
  538. ++elf_symbol_count;
  539. }
  540. elf_symbol_size = elf_symbol_count * sizeof (struct elf_symbol);
  541. elf_symbols = ((struct elf_symbol *)
  542. backtrace_alloc (state, elf_symbol_size, error_callback,
  543. data));
  544. if (elf_symbols == NULL)
  545. return 0;
  546. sym = (const b_elf_sym *) symtab_data;
  547. j = 0;
  548. for (i = 0; i < sym_count; ++i, ++sym)
  549. {
  550. int info;
  551. info = sym->st_info & 0xf;
  552. if (info != STT_FUNC && info != STT_OBJECT)
  553. continue;
  554. if (sym->st_shndx == SHN_UNDEF)
  555. continue;
  556. if (sym->st_name >= strtab_size)
  557. {
  558. error_callback (data, "symbol string index out of range", 0);
  559. backtrace_free (state, elf_symbols, elf_symbol_size, error_callback,
  560. data);
  561. return 0;
  562. }
  563. elf_symbols[j].name = (const char *) strtab + sym->st_name;
  564. /* Special case PowerPC64 ELFv1 symbols in .opd section, if the symbol
  565. is a function descriptor, read the actual code address from the
  566. descriptor. */
  567. if (opd
  568. && sym->st_value >= opd->addr
  569. && sym->st_value < opd->addr + opd->size)
  570. elf_symbols[j].address
  571. = *(const b_elf_addr *) (opd->data + (sym->st_value - opd->addr));
  572. else
  573. elf_symbols[j].address = sym->st_value;
  574. elf_symbols[j].address += base_address;
  575. elf_symbols[j].size = sym->st_size;
  576. ++j;
  577. }
  578. backtrace_qsort (elf_symbols, elf_symbol_count, sizeof (struct elf_symbol),
  579. elf_symbol_compare);
  580. sdata->next = NULL;
  581. sdata->symbols = elf_symbols;
  582. sdata->count = elf_symbol_count;
  583. return 1;
  584. }
  585. /* Add EDATA to the list in STATE. */
  586. static void
  587. elf_add_syminfo_data (struct backtrace_state *state,
  588. struct elf_syminfo_data *edata)
  589. {
  590. if (!state->threaded)
  591. {
  592. struct elf_syminfo_data **pp;
  593. for (pp = (struct elf_syminfo_data **) (void *) &state->syminfo_data;
  594. *pp != NULL;
  595. pp = &(*pp)->next)
  596. ;
  597. *pp = edata;
  598. }
  599. else
  600. {
  601. while (1)
  602. {
  603. struct elf_syminfo_data **pp;
  604. pp = (struct elf_syminfo_data **) (void *) &state->syminfo_data;
  605. while (1)
  606. {
  607. struct elf_syminfo_data *p;
  608. p = backtrace_atomic_load_pointer (pp);
  609. if (p == NULL)
  610. break;
  611. pp = &p->next;
  612. }
  613. if (__sync_bool_compare_and_swap (pp, NULL, edata))
  614. break;
  615. }
  616. }
  617. }
  618. /* Return the symbol name and value for an ADDR. */
  619. static void
  620. elf_syminfo (struct backtrace_state *state, uintptr_t addr,
  621. backtrace_syminfo_callback callback,
  622. backtrace_error_callback error_callback ATTRIBUTE_UNUSED,
  623. void *data)
  624. {
  625. struct elf_syminfo_data *edata;
  626. struct elf_symbol *sym = NULL;
  627. if (!state->threaded)
  628. {
  629. for (edata = (struct elf_syminfo_data *) state->syminfo_data;
  630. edata != NULL;
  631. edata = edata->next)
  632. {
  633. sym = ((struct elf_symbol *)
  634. bsearch (&addr, edata->symbols, edata->count,
  635. sizeof (struct elf_symbol), elf_symbol_search));
  636. if (sym != NULL)
  637. break;
  638. }
  639. }
  640. else
  641. {
  642. struct elf_syminfo_data **pp;
  643. pp = (struct elf_syminfo_data **) (void *) &state->syminfo_data;
  644. while (1)
  645. {
  646. edata = backtrace_atomic_load_pointer (pp);
  647. if (edata == NULL)
  648. break;
  649. sym = ((struct elf_symbol *)
  650. bsearch (&addr, edata->symbols, edata->count,
  651. sizeof (struct elf_symbol), elf_symbol_search));
  652. if (sym != NULL)
  653. break;
  654. pp = &edata->next;
  655. }
  656. }
  657. if (sym == NULL)
  658. callback (data, addr, NULL, 0, 0);
  659. else
  660. callback (data, addr, sym->name, sym->address, sym->size);
  661. }
  662. /* Return whether FILENAME is a symlink. */
  663. static int
  664. elf_is_symlink (const char *filename)
  665. {
  666. struct stat st;
  667. if (lstat (filename, &st) < 0)
  668. return 0;
  669. return S_ISLNK (st.st_mode);
  670. }
  671. /* Return the results of reading the symlink FILENAME in a buffer
  672. allocated by backtrace_alloc. Return the length of the buffer in
  673. *LEN. */
  674. static char *
  675. elf_readlink (struct backtrace_state *state, const char *filename,
  676. backtrace_error_callback error_callback, void *data,
  677. size_t *plen)
  678. {
  679. size_t len;
  680. char *buf;
  681. len = 128;
  682. while (1)
  683. {
  684. ssize_t rl;
  685. buf = backtrace_alloc (state, len, error_callback, data);
  686. if (buf == NULL)
  687. return NULL;
  688. rl = readlink (filename, buf, len);
  689. if (rl < 0)
  690. {
  691. backtrace_free (state, buf, len, error_callback, data);
  692. return NULL;
  693. }
  694. if ((size_t) rl < len - 1)
  695. {
  696. buf[rl] = '\0';
  697. *plen = len;
  698. return buf;
  699. }
  700. backtrace_free (state, buf, len, error_callback, data);
  701. len *= 2;
  702. }
  703. }
  704. #define SYSTEM_BUILD_ID_DIR "/usr/lib/debug/.build-id/"
  705. /* Open a separate debug info file, using the build ID to find it.
  706. Returns an open file descriptor, or -1.
  707. The GDB manual says that the only place gdb looks for a debug file
  708. when the build ID is known is in /usr/lib/debug/.build-id. */
  709. static int
  710. elf_open_debugfile_by_buildid (struct backtrace_state *state,
  711. const char *buildid_data, size_t buildid_size,
  712. backtrace_error_callback error_callback,
  713. void *data)
  714. {
  715. const char * const prefix = SYSTEM_BUILD_ID_DIR;
  716. const size_t prefix_len = strlen (prefix);
  717. const char * const suffix = ".debug";
  718. const size_t suffix_len = strlen (suffix);
  719. size_t len;
  720. char *bd_filename;
  721. char *t;
  722. size_t i;
  723. int ret;
  724. int does_not_exist;
  725. len = prefix_len + buildid_size * 2 + suffix_len + 2;
  726. bd_filename = backtrace_alloc (state, len, error_callback, data);
  727. if (bd_filename == NULL)
  728. return -1;
  729. t = bd_filename;
  730. memcpy (t, prefix, prefix_len);
  731. t += prefix_len;
  732. for (i = 0; i < buildid_size; i++)
  733. {
  734. unsigned char b;
  735. unsigned char nib;
  736. b = (unsigned char) buildid_data[i];
  737. nib = (b & 0xf0) >> 4;
  738. *t++ = nib < 10 ? '0' + nib : 'a' + nib - 10;
  739. nib = b & 0x0f;
  740. *t++ = nib < 10 ? '0' + nib : 'a' + nib - 10;
  741. if (i == 0)
  742. *t++ = '/';
  743. }
  744. memcpy (t, suffix, suffix_len);
  745. t[suffix_len] = '\0';
  746. ret = backtrace_open (bd_filename, error_callback, data, &does_not_exist);
  747. backtrace_free (state, bd_filename, len, error_callback, data);
  748. /* gdb checks that the debuginfo file has the same build ID note.
  749. That seems kind of pointless to me--why would it have the right
  750. name but not the right build ID?--so skipping the check. */
  751. return ret;
  752. }
  753. /* Try to open a file whose name is PREFIX (length PREFIX_LEN)
  754. concatenated with PREFIX2 (length PREFIX2_LEN) concatenated with
  755. DEBUGLINK_NAME. Returns an open file descriptor, or -1. */
  756. static int
  757. elf_try_debugfile (struct backtrace_state *state, const char *prefix,
  758. size_t prefix_len, const char *prefix2, size_t prefix2_len,
  759. const char *debuglink_name,
  760. backtrace_error_callback error_callback, void *data)
  761. {
  762. size_t debuglink_len;
  763. size_t try_len;
  764. char *try;
  765. int does_not_exist;
  766. int ret;
  767. debuglink_len = strlen (debuglink_name);
  768. try_len = prefix_len + prefix2_len + debuglink_len + 1;
  769. try = backtrace_alloc (state, try_len, error_callback, data);
  770. if (try == NULL)
  771. return -1;
  772. memcpy (try, prefix, prefix_len);
  773. memcpy (try + prefix_len, prefix2, prefix2_len);
  774. memcpy (try + prefix_len + prefix2_len, debuglink_name, debuglink_len);
  775. try[prefix_len + prefix2_len + debuglink_len] = '\0';
  776. ret = backtrace_open (try, error_callback, data, &does_not_exist);
  777. backtrace_free (state, try, try_len, error_callback, data);
  778. return ret;
  779. }
  780. /* Find a separate debug info file, using the debuglink section data
  781. to find it. Returns an open file descriptor, or -1. */
  782. static int
  783. elf_find_debugfile_by_debuglink (struct backtrace_state *state,
  784. const char *filename,
  785. const char *debuglink_name,
  786. backtrace_error_callback error_callback,
  787. void *data)
  788. {
  789. int ret;
  790. char *alc;
  791. size_t alc_len;
  792. const char *slash;
  793. int ddescriptor;
  794. const char *prefix;
  795. size_t prefix_len;
  796. /* Resolve symlinks in FILENAME. Since FILENAME is fairly likely to
  797. be /proc/self/exe, symlinks are common. We don't try to resolve
  798. the whole path name, just the base name. */
  799. ret = -1;
  800. alc = NULL;
  801. alc_len = 0;
  802. while (elf_is_symlink (filename))
  803. {
  804. char *new_buf;
  805. size_t new_len;
  806. new_buf = elf_readlink (state, filename, error_callback, data, &new_len);
  807. if (new_buf == NULL)
  808. break;
  809. if (new_buf[0] == '/')
  810. filename = new_buf;
  811. else
  812. {
  813. slash = strrchr (filename, '/');
  814. if (slash == NULL)
  815. filename = new_buf;
  816. else
  817. {
  818. size_t clen;
  819. char *c;
  820. slash++;
  821. clen = slash - filename + strlen (new_buf) + 1;
  822. c = backtrace_alloc (state, clen, error_callback, data);
  823. if (c == NULL)
  824. goto done;
  825. memcpy (c, filename, slash - filename);
  826. memcpy (c + (slash - filename), new_buf, strlen (new_buf));
  827. c[slash - filename + strlen (new_buf)] = '\0';
  828. backtrace_free (state, new_buf, new_len, error_callback, data);
  829. filename = c;
  830. new_buf = c;
  831. new_len = clen;
  832. }
  833. }
  834. if (alc != NULL)
  835. backtrace_free (state, alc, alc_len, error_callback, data);
  836. alc = new_buf;
  837. alc_len = new_len;
  838. }
  839. /* Look for DEBUGLINK_NAME in the same directory as FILENAME. */
  840. slash = strrchr (filename, '/');
  841. if (slash == NULL)
  842. {
  843. prefix = "";
  844. prefix_len = 0;
  845. }
  846. else
  847. {
  848. slash++;
  849. prefix = filename;
  850. prefix_len = slash - filename;
  851. }
  852. ddescriptor = elf_try_debugfile (state, prefix, prefix_len, "", 0,
  853. debuglink_name, error_callback, data);
  854. if (ddescriptor >= 0)
  855. {
  856. ret = ddescriptor;
  857. goto done;
  858. }
  859. /* Look for DEBUGLINK_NAME in a .debug subdirectory of FILENAME. */
  860. ddescriptor = elf_try_debugfile (state, prefix, prefix_len, ".debug/",
  861. strlen (".debug/"), debuglink_name,
  862. error_callback, data);
  863. if (ddescriptor >= 0)
  864. {
  865. ret = ddescriptor;
  866. goto done;
  867. }
  868. /* Look for DEBUGLINK_NAME in /usr/lib/debug. */
  869. ddescriptor = elf_try_debugfile (state, "/usr/lib/debug/",
  870. strlen ("/usr/lib/debug/"), prefix,
  871. prefix_len, debuglink_name,
  872. error_callback, data);
  873. if (ddescriptor >= 0)
  874. ret = ddescriptor;
  875. done:
  876. if (alc != NULL && alc_len > 0)
  877. backtrace_free (state, alc, alc_len, error_callback, data);
  878. return ret;
  879. }
  880. /* Open a separate debug info file, using the debuglink section data
  881. to find it. Returns an open file descriptor, or -1. */
  882. static int
  883. elf_open_debugfile_by_debuglink (struct backtrace_state *state,
  884. const char *filename,
  885. const char *debuglink_name,
  886. uint32_t debuglink_crc,
  887. backtrace_error_callback error_callback,
  888. void *data)
  889. {
  890. int ddescriptor;
  891. ddescriptor = elf_find_debugfile_by_debuglink (state, filename,
  892. debuglink_name,
  893. error_callback, data);
  894. if (ddescriptor < 0)
  895. return -1;
  896. if (debuglink_crc != 0)
  897. {
  898. uint32_t got_crc;
  899. got_crc = elf_crc32_file (state, ddescriptor, error_callback, data);
  900. if (got_crc != debuglink_crc)
  901. {
  902. backtrace_close (ddescriptor, error_callback, data);
  903. return -1;
  904. }
  905. }
  906. return ddescriptor;
  907. }
  908. /* A function useful for setting a breakpoint for an inflation failure
  909. when this code is compiled with -g. */
  910. static void
  911. elf_uncompress_failed(void)
  912. {
  913. }
  914. /* *PVAL is the current value being read from the stream, and *PBITS
  915. is the number of valid bits. Ensure that *PVAL holds at least 15
  916. bits by reading additional bits from *PPIN, up to PINEND, as
  917. needed. Updates *PPIN, *PVAL and *PBITS. Returns 1 on success, 0
  918. on error. */
  919. static int
  920. elf_zlib_fetch (const unsigned char **ppin, const unsigned char *pinend,
  921. uint64_t *pval, unsigned int *pbits)
  922. {
  923. unsigned int bits;
  924. const unsigned char *pin;
  925. uint64_t val;
  926. uint32_t next;
  927. bits = *pbits;
  928. if (bits >= 15)
  929. return 1;
  930. pin = *ppin;
  931. val = *pval;
  932. if (unlikely (pinend - pin < 4))
  933. {
  934. elf_uncompress_failed ();
  935. return 0;
  936. }
  937. #if defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) \
  938. && defined(__ORDER_BIG_ENDIAN__) \
  939. && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ \
  940. || __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
  941. /* We've ensured that PIN is aligned. */
  942. next = *(const uint32_t *)pin;
  943. #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
  944. next = __builtin_bswap32 (next);
  945. #endif
  946. #else
  947. next = pin[0] | (pin[1] << 8) | (pin[2] << 16) | (pin[3] << 24);
  948. #endif
  949. val |= (uint64_t)next << bits;
  950. bits += 32;
  951. pin += 4;
  952. /* We will need the next four bytes soon. */
  953. __builtin_prefetch (pin, 0, 0);
  954. *ppin = pin;
  955. *pval = val;
  956. *pbits = bits;
  957. return 1;
  958. }
  959. /* Huffman code tables, like the rest of the zlib format, are defined
  960. by RFC 1951. We store a Huffman code table as a series of tables
  961. stored sequentially in memory. Each entry in a table is 16 bits.
  962. The first, main, table has 256 entries. It is followed by a set of
  963. secondary tables of length 2 to 128 entries. The maximum length of
  964. a code sequence in the deflate format is 15 bits, so that is all we
  965. need. Each secondary table has an index, which is the offset of
  966. the table in the overall memory storage.
  967. The deflate format says that all codes of a given bit length are
  968. lexicographically consecutive. Perhaps we could have 130 values
  969. that require a 15-bit code, perhaps requiring three secondary
  970. tables of size 128. I don't know if this is actually possible, but
  971. it suggests that the maximum size required for secondary tables is
  972. 3 * 128 + 3 * 64 ... == 768. The zlib enough program reports 660
  973. as the maximum. We permit 768, since in addition to the 256 for
  974. the primary table, with two bytes per entry, and with the two
  975. tables we need, that gives us a page.
  976. A single table entry needs to store a value or (for the main table
  977. only) the index and size of a secondary table. Values range from 0
  978. to 285, inclusive. Secondary table indexes, per above, range from
  979. 0 to 510. For a value we need to store the number of bits we need
  980. to determine that value (one value may appear multiple times in the
  981. table), which is 1 to 8. For a secondary table we need to store
  982. the number of bits used to index into the table, which is 1 to 7.
  983. And of course we need 1 bit to decide whether we have a value or a
  984. secondary table index. So each entry needs 9 bits for value/table
  985. index, 3 bits for size, 1 bit what it is. For simplicity we use 16
  986. bits per entry. */
  987. /* Number of entries we allocate to for one code table. We get a page
  988. for the two code tables we need. */
  989. #define HUFFMAN_TABLE_SIZE (1024)
  990. /* Bit masks and shifts for the values in the table. */
  991. #define HUFFMAN_VALUE_MASK 0x01ff
  992. #define HUFFMAN_BITS_SHIFT 9
  993. #define HUFFMAN_BITS_MASK 0x7
  994. #define HUFFMAN_SECONDARY_SHIFT 12
  995. /* For working memory while inflating we need two code tables, we need
  996. an array of code lengths (max value 15, so we use unsigned char),
  997. and an array of unsigned shorts used while building a table. The
  998. latter two arrays must be large enough to hold the maximum number
  999. of code lengths, which RFC 1951 defines as 286 + 30. */
  1000. #define ZDEBUG_TABLE_SIZE \
  1001. (2 * HUFFMAN_TABLE_SIZE * sizeof (uint16_t) \
  1002. + (286 + 30) * sizeof (uint16_t) \
  1003. + (286 + 30) * sizeof (unsigned char))
  1004. #define ZDEBUG_TABLE_CODELEN_OFFSET \
  1005. (2 * HUFFMAN_TABLE_SIZE * sizeof (uint16_t) \
  1006. + (286 + 30) * sizeof (uint16_t))
  1007. #define ZDEBUG_TABLE_WORK_OFFSET \
  1008. (2 * HUFFMAN_TABLE_SIZE * sizeof (uint16_t))
  1009. #ifdef BACKTRACE_GENERATE_FIXED_HUFFMAN_TABLE
  1010. /* Used by the main function that generates the fixed table to learn
  1011. the table size. */
  1012. static size_t final_next_secondary;
  1013. #endif
  1014. /* Build a Huffman code table from an array of lengths in CODES of
  1015. length CODES_LEN. The table is stored into *TABLE. ZDEBUG_TABLE
  1016. is the same as for elf_zlib_inflate, used to find some work space.
  1017. Returns 1 on success, 0 on error. */
  1018. static int
  1019. elf_zlib_inflate_table (unsigned char *codes, size_t codes_len,
  1020. uint16_t *zdebug_table, uint16_t *table)
  1021. {
  1022. uint16_t count[16];
  1023. uint16_t start[16];
  1024. uint16_t prev[16];
  1025. uint16_t firstcode[7];
  1026. uint16_t *next;
  1027. size_t i;
  1028. size_t j;
  1029. unsigned int code;
  1030. size_t next_secondary;
  1031. /* Count the number of code of each length. Set NEXT[val] to be the
  1032. next value after VAL with the same bit length. */
  1033. next = (uint16_t *) (((unsigned char *) zdebug_table)
  1034. + ZDEBUG_TABLE_WORK_OFFSET);
  1035. memset (&count[0], 0, 16 * sizeof (uint16_t));
  1036. for (i = 0; i < codes_len; ++i)
  1037. {
  1038. if (unlikely (codes[i] >= 16))
  1039. {
  1040. elf_uncompress_failed ();
  1041. return 0;
  1042. }
  1043. if (count[codes[i]] == 0)
  1044. {
  1045. start[codes[i]] = i;
  1046. prev[codes[i]] = i;
  1047. }
  1048. else
  1049. {
  1050. next[prev[codes[i]]] = i;
  1051. prev[codes[i]] = i;
  1052. }
  1053. ++count[codes[i]];
  1054. }
  1055. /* For each length, fill in the table for the codes of that
  1056. length. */
  1057. memset (table, 0, HUFFMAN_TABLE_SIZE * sizeof (uint16_t));
  1058. /* Handle the values that do not require a secondary table. */
  1059. code = 0;
  1060. for (j = 1; j <= 8; ++j)
  1061. {
  1062. unsigned int jcnt;
  1063. unsigned int val;
  1064. jcnt = count[j];
  1065. if (jcnt == 0)
  1066. continue;
  1067. if (unlikely (jcnt > (1U << j)))
  1068. {
  1069. elf_uncompress_failed ();
  1070. return 0;
  1071. }
  1072. /* There are JCNT values that have this length, the values
  1073. starting from START[j] continuing through NEXT[VAL]. Those
  1074. values are assigned consecutive values starting at CODE. */
  1075. val = start[j];
  1076. for (i = 0; i < jcnt; ++i)
  1077. {
  1078. uint16_t tval;
  1079. size_t ind;
  1080. unsigned int incr;
  1081. /* In the compressed bit stream, the value VAL is encoded as
  1082. J bits with the value C. */
  1083. if (unlikely ((val & ~HUFFMAN_VALUE_MASK) != 0))
  1084. {
  1085. elf_uncompress_failed ();
  1086. return 0;
  1087. }
  1088. tval = val | ((j - 1) << HUFFMAN_BITS_SHIFT);
  1089. /* The table lookup uses 8 bits. If J is less than 8, we
  1090. don't know what the other bits will be. We need to fill
  1091. in all possibilities in the table. Since the Huffman
  1092. code is unambiguous, those entries can't be used for any
  1093. other code. */
  1094. for (ind = code; ind < 0x100; ind += 1 << j)
  1095. {
  1096. if (unlikely (table[ind] != 0))
  1097. {
  1098. elf_uncompress_failed ();
  1099. return 0;
  1100. }
  1101. table[ind] = tval;
  1102. }
  1103. /* Advance to the next value with this length. */
  1104. if (i + 1 < jcnt)
  1105. val = next[val];
  1106. /* The Huffman codes are stored in the bitstream with the
  1107. most significant bit first, as is required to make them
  1108. unambiguous. The effect is that when we read them from
  1109. the bitstream we see the bit sequence in reverse order:
  1110. the most significant bit of the Huffman code is the least
  1111. significant bit of the value we read from the bitstream.
  1112. That means that to make our table lookups work, we need
  1113. to reverse the bits of CODE. Since reversing bits is
  1114. tedious and in general requires using a table, we instead
  1115. increment CODE in reverse order. That is, if the number
  1116. of bits we are currently using, here named J, is 3, we
  1117. count as 000, 100, 010, 110, 001, 101, 011, 111, which is
  1118. to say the numbers from 0 to 7 but with the bits
  1119. reversed. Going to more bits, aka incrementing J,
  1120. effectively just adds more zero bits as the beginning,
  1121. and as such does not change the numeric value of CODE.
  1122. To increment CODE of length J in reverse order, find the
  1123. most significant zero bit and set it to one while
  1124. clearing all higher bits. In other words, add 1 modulo
  1125. 2^J, only reversed. */
  1126. incr = 1U << (j - 1);
  1127. while ((code & incr) != 0)
  1128. incr >>= 1;
  1129. if (incr == 0)
  1130. code = 0;
  1131. else
  1132. {
  1133. code &= incr - 1;
  1134. code += incr;
  1135. }
  1136. }
  1137. }
  1138. /* Handle the values that require a secondary table. */
  1139. /* Set FIRSTCODE, the number at which the codes start, for each
  1140. length. */
  1141. for (j = 9; j < 16; j++)
  1142. {
  1143. unsigned int jcnt;
  1144. unsigned int k;
  1145. jcnt = count[j];
  1146. if (jcnt == 0)
  1147. continue;
  1148. /* There are JCNT values that have this length, the values
  1149. starting from START[j]. Those values are assigned
  1150. consecutive values starting at CODE. */
  1151. firstcode[j - 9] = code;
  1152. /* Reverse add JCNT to CODE modulo 2^J. */
  1153. for (k = 0; k < j; ++k)
  1154. {
  1155. if ((jcnt & (1U << k)) != 0)
  1156. {
  1157. unsigned int m;
  1158. unsigned int bit;
  1159. bit = 1U << (j - k - 1);
  1160. for (m = 0; m < j - k; ++m, bit >>= 1)
  1161. {
  1162. if ((code & bit) == 0)
  1163. {
  1164. code += bit;
  1165. break;
  1166. }
  1167. code &= ~bit;
  1168. }
  1169. jcnt &= ~(1U << k);
  1170. }
  1171. }
  1172. if (unlikely (jcnt != 0))
  1173. {
  1174. elf_uncompress_failed ();
  1175. return 0;
  1176. }
  1177. }
  1178. /* For J from 9 to 15, inclusive, we store COUNT[J] consecutive
  1179. values starting at START[J] with consecutive codes starting at
  1180. FIRSTCODE[J - 9]. In the primary table we need to point to the
  1181. secondary table, and the secondary table will be indexed by J - 9
  1182. bits. We count down from 15 so that we install the larger
  1183. secondary tables first, as the smaller ones may be embedded in
  1184. the larger ones. */
  1185. next_secondary = 0; /* Index of next secondary table (after primary). */
  1186. for (j = 15; j >= 9; j--)
  1187. {
  1188. unsigned int jcnt;
  1189. unsigned int val;
  1190. size_t primary; /* Current primary index. */
  1191. size_t secondary; /* Offset to current secondary table. */
  1192. size_t secondary_bits; /* Bit size of current secondary table. */
  1193. jcnt = count[j];
  1194. if (jcnt == 0)
  1195. continue;
  1196. val = start[j];
  1197. code = firstcode[j - 9];
  1198. primary = 0x100;
  1199. secondary = 0;
  1200. secondary_bits = 0;
  1201. for (i = 0; i < jcnt; ++i)
  1202. {
  1203. uint16_t tval;
  1204. size_t ind;
  1205. unsigned int incr;
  1206. if ((code & 0xff) != primary)
  1207. {
  1208. uint16_t tprimary;
  1209. /* Fill in a new primary table entry. */
  1210. primary = code & 0xff;
  1211. tprimary = table[primary];
  1212. if (tprimary == 0)
  1213. {
  1214. /* Start a new secondary table. */
  1215. if (unlikely ((next_secondary & HUFFMAN_VALUE_MASK)
  1216. != next_secondary))
  1217. {
  1218. elf_uncompress_failed ();
  1219. return 0;
  1220. }
  1221. secondary = next_secondary;
  1222. secondary_bits = j - 8;
  1223. next_secondary += 1 << secondary_bits;
  1224. table[primary] = (secondary
  1225. + ((j - 8) << HUFFMAN_BITS_SHIFT)
  1226. + (1U << HUFFMAN_SECONDARY_SHIFT));
  1227. }
  1228. else
  1229. {
  1230. /* There is an existing entry. It had better be a
  1231. secondary table with enough bits. */
  1232. if (unlikely ((tprimary & (1U << HUFFMAN_SECONDARY_SHIFT))
  1233. == 0))
  1234. {
  1235. elf_uncompress_failed ();
  1236. return 0;
  1237. }
  1238. secondary = tprimary & HUFFMAN_VALUE_MASK;
  1239. secondary_bits = ((tprimary >> HUFFMAN_BITS_SHIFT)
  1240. & HUFFMAN_BITS_MASK);
  1241. if (unlikely (secondary_bits < j - 8))
  1242. {
  1243. elf_uncompress_failed ();
  1244. return 0;
  1245. }
  1246. }
  1247. }
  1248. /* Fill in secondary table entries. */
  1249. tval = val | ((j - 8) << HUFFMAN_BITS_SHIFT);
  1250. for (ind = code >> 8;
  1251. ind < (1U << secondary_bits);
  1252. ind += 1U << (j - 8))
  1253. {
  1254. if (unlikely (table[secondary + 0x100 + ind] != 0))
  1255. {
  1256. elf_uncompress_failed ();
  1257. return 0;
  1258. }
  1259. table[secondary + 0x100 + ind] = tval;
  1260. }
  1261. if (i + 1 < jcnt)
  1262. val = next[val];
  1263. incr = 1U << (j - 1);
  1264. while ((code & incr) != 0)
  1265. incr >>= 1;
  1266. if (incr == 0)
  1267. code = 0;
  1268. else
  1269. {
  1270. code &= incr - 1;
  1271. code += incr;
  1272. }
  1273. }
  1274. }
  1275. #ifdef BACKTRACE_GENERATE_FIXED_HUFFMAN_TABLE
  1276. final_next_secondary = next_secondary;
  1277. #endif
  1278. return 1;
  1279. }
  1280. #ifdef BACKTRACE_GENERATE_FIXED_HUFFMAN_TABLE
  1281. /* Used to generate the fixed Huffman table for block type 1. */
  1282. #include <stdio.h>
  1283. static uint16_t table[ZDEBUG_TABLE_SIZE];
  1284. static unsigned char codes[288];
  1285. int
  1286. main ()
  1287. {
  1288. size_t i;
  1289. for (i = 0; i <= 143; ++i)
  1290. codes[i] = 8;
  1291. for (i = 144; i <= 255; ++i)
  1292. codes[i] = 9;
  1293. for (i = 256; i <= 279; ++i)
  1294. codes[i] = 7;
  1295. for (i = 280; i <= 287; ++i)
  1296. codes[i] = 8;
  1297. if (!elf_zlib_inflate_table (&codes[0], 288, &table[0], &table[0]))
  1298. {
  1299. fprintf (stderr, "elf_zlib_inflate_table failed\n");
  1300. exit (EXIT_FAILURE);
  1301. }
  1302. printf ("static const uint16_t elf_zlib_default_table[%#zx] =\n",
  1303. final_next_secondary + 0x100);
  1304. printf ("{\n");
  1305. for (i = 0; i < final_next_secondary + 0x100; i += 8)
  1306. {
  1307. size_t j;
  1308. printf (" ");
  1309. for (j = i; j < final_next_secondary + 0x100 && j < i + 8; ++j)
  1310. printf (" %#x,", table[j]);
  1311. printf ("\n");
  1312. }
  1313. printf ("};\n");
  1314. printf ("\n");
  1315. for (i = 0; i < 32; ++i)
  1316. codes[i] = 5;
  1317. if (!elf_zlib_inflate_table (&codes[0], 32, &table[0], &table[0]))
  1318. {
  1319. fprintf (stderr, "elf_zlib_inflate_table failed\n");
  1320. exit (EXIT_FAILURE);
  1321. }
  1322. printf ("static const uint16_t elf_zlib_default_dist_table[%#zx] =\n",
  1323. final_next_secondary + 0x100);
  1324. printf ("{\n");
  1325. for (i = 0; i < final_next_secondary + 0x100; i += 8)
  1326. {
  1327. size_t j;
  1328. printf (" ");
  1329. for (j = i; j < final_next_secondary + 0x100 && j < i + 8; ++j)
  1330. printf (" %#x,", table[j]);
  1331. printf ("\n");
  1332. }
  1333. printf ("};\n");
  1334. return 0;
  1335. }
  1336. #endif
  1337. /* The fixed tables generated by the #ifdef'ed out main function
  1338. above. */
  1339. static const uint16_t elf_zlib_default_table[0x170] =
  1340. {
  1341. 0xd00, 0xe50, 0xe10, 0xf18, 0xd10, 0xe70, 0xe30, 0x1230,
  1342. 0xd08, 0xe60, 0xe20, 0x1210, 0xe00, 0xe80, 0xe40, 0x1250,
  1343. 0xd04, 0xe58, 0xe18, 0x1200, 0xd14, 0xe78, 0xe38, 0x1240,
  1344. 0xd0c, 0xe68, 0xe28, 0x1220, 0xe08, 0xe88, 0xe48, 0x1260,
  1345. 0xd02, 0xe54, 0xe14, 0xf1c, 0xd12, 0xe74, 0xe34, 0x1238,
  1346. 0xd0a, 0xe64, 0xe24, 0x1218, 0xe04, 0xe84, 0xe44, 0x1258,
  1347. 0xd06, 0xe5c, 0xe1c, 0x1208, 0xd16, 0xe7c, 0xe3c, 0x1248,
  1348. 0xd0e, 0xe6c, 0xe2c, 0x1228, 0xe0c, 0xe8c, 0xe4c, 0x1268,
  1349. 0xd01, 0xe52, 0xe12, 0xf1a, 0xd11, 0xe72, 0xe32, 0x1234,
  1350. 0xd09, 0xe62, 0xe22, 0x1214, 0xe02, 0xe82, 0xe42, 0x1254,
  1351. 0xd05, 0xe5a, 0xe1a, 0x1204, 0xd15, 0xe7a, 0xe3a, 0x1244,
  1352. 0xd0d, 0xe6a, 0xe2a, 0x1224, 0xe0a, 0xe8a, 0xe4a, 0x1264,
  1353. 0xd03, 0xe56, 0xe16, 0xf1e, 0xd13, 0xe76, 0xe36, 0x123c,
  1354. 0xd0b, 0xe66, 0xe26, 0x121c, 0xe06, 0xe86, 0xe46, 0x125c,
  1355. 0xd07, 0xe5e, 0xe1e, 0x120c, 0xd17, 0xe7e, 0xe3e, 0x124c,
  1356. 0xd0f, 0xe6e, 0xe2e, 0x122c, 0xe0e, 0xe8e, 0xe4e, 0x126c,
  1357. 0xd00, 0xe51, 0xe11, 0xf19, 0xd10, 0xe71, 0xe31, 0x1232,
  1358. 0xd08, 0xe61, 0xe21, 0x1212, 0xe01, 0xe81, 0xe41, 0x1252,
  1359. 0xd04, 0xe59, 0xe19, 0x1202, 0xd14, 0xe79, 0xe39, 0x1242,
  1360. 0xd0c, 0xe69, 0xe29, 0x1222, 0xe09, 0xe89, 0xe49, 0x1262,
  1361. 0xd02, 0xe55, 0xe15, 0xf1d, 0xd12, 0xe75, 0xe35, 0x123a,
  1362. 0xd0a, 0xe65, 0xe25, 0x121a, 0xe05, 0xe85, 0xe45, 0x125a,
  1363. 0xd06, 0xe5d, 0xe1d, 0x120a, 0xd16, 0xe7d, 0xe3d, 0x124a,
  1364. 0xd0e, 0xe6d, 0xe2d, 0x122a, 0xe0d, 0xe8d, 0xe4d, 0x126a,
  1365. 0xd01, 0xe53, 0xe13, 0xf1b, 0xd11, 0xe73, 0xe33, 0x1236,
  1366. 0xd09, 0xe63, 0xe23, 0x1216, 0xe03, 0xe83, 0xe43, 0x1256,
  1367. 0xd05, 0xe5b, 0xe1b, 0x1206, 0xd15, 0xe7b, 0xe3b, 0x1246,
  1368. 0xd0d, 0xe6b, 0xe2b, 0x1226, 0xe0b, 0xe8b, 0xe4b, 0x1266,
  1369. 0xd03, 0xe57, 0xe17, 0xf1f, 0xd13, 0xe77, 0xe37, 0x123e,
  1370. 0xd0b, 0xe67, 0xe27, 0x121e, 0xe07, 0xe87, 0xe47, 0x125e,
  1371. 0xd07, 0xe5f, 0xe1f, 0x120e, 0xd17, 0xe7f, 0xe3f, 0x124e,
  1372. 0xd0f, 0xe6f, 0xe2f, 0x122e, 0xe0f, 0xe8f, 0xe4f, 0x126e,
  1373. 0x290, 0x291, 0x292, 0x293, 0x294, 0x295, 0x296, 0x297,
  1374. 0x298, 0x299, 0x29a, 0x29b, 0x29c, 0x29d, 0x29e, 0x29f,
  1375. 0x2a0, 0x2a1, 0x2a2, 0x2a3, 0x2a4, 0x2a5, 0x2a6, 0x2a7,
  1376. 0x2a8, 0x2a9, 0x2aa, 0x2ab, 0x2ac, 0x2ad, 0x2ae, 0x2af,
  1377. 0x2b0, 0x2b1, 0x2b2, 0x2b3, 0x2b4, 0x2b5, 0x2b6, 0x2b7,
  1378. 0x2b8, 0x2b9, 0x2ba, 0x2bb, 0x2bc, 0x2bd, 0x2be, 0x2bf,
  1379. 0x2c0, 0x2c1, 0x2c2, 0x2c3, 0x2c4, 0x2c5, 0x2c6, 0x2c7,
  1380. 0x2c8, 0x2c9, 0x2ca, 0x2cb, 0x2cc, 0x2cd, 0x2ce, 0x2cf,
  1381. 0x2d0, 0x2d1, 0x2d2, 0x2d3, 0x2d4, 0x2d5, 0x2d6, 0x2d7,
  1382. 0x2d8, 0x2d9, 0x2da, 0x2db, 0x2dc, 0x2dd, 0x2de, 0x2df,
  1383. 0x2e0, 0x2e1, 0x2e2, 0x2e3, 0x2e4, 0x2e5, 0x2e6, 0x2e7,
  1384. 0x2e8, 0x2e9, 0x2ea, 0x2eb, 0x2ec, 0x2ed, 0x2ee, 0x2ef,
  1385. 0x2f0, 0x2f1, 0x2f2, 0x2f3, 0x2f4, 0x2f5, 0x2f6, 0x2f7,
  1386. 0x2f8, 0x2f9, 0x2fa, 0x2fb, 0x2fc, 0x2fd, 0x2fe, 0x2ff,
  1387. };
  1388. static const uint16_t elf_zlib_default_dist_table[0x100] =
  1389. {
  1390. 0x800, 0x810, 0x808, 0x818, 0x804, 0x814, 0x80c, 0x81c,
  1391. 0x802, 0x812, 0x80a, 0x81a, 0x806, 0x816, 0x80e, 0x81e,
  1392. 0x801, 0x811, 0x809, 0x819, 0x805, 0x815, 0x80d, 0x81d,
  1393. 0x803, 0x813, 0x80b, 0x81b, 0x807, 0x817, 0x80f, 0x81f,
  1394. 0x800, 0x810, 0x808, 0x818, 0x804, 0x814, 0x80c, 0x81c,
  1395. 0x802, 0x812, 0x80a, 0x81a, 0x806, 0x816, 0x80e, 0x81e,
  1396. 0x801, 0x811, 0x809, 0x819, 0x805, 0x815, 0x80d, 0x81d,
  1397. 0x803, 0x813, 0x80b, 0x81b, 0x807, 0x817, 0x80f, 0x81f,
  1398. 0x800, 0x810, 0x808, 0x818, 0x804, 0x814, 0x80c, 0x81c,
  1399. 0x802, 0x812, 0x80a, 0x81a, 0x806, 0x816, 0x80e, 0x81e,
  1400. 0x801, 0x811, 0x809, 0x819, 0x805, 0x815, 0x80d, 0x81d,
  1401. 0x803, 0x813, 0x80b, 0x81b, 0x807, 0x817, 0x80f, 0x81f,
  1402. 0x800, 0x810, 0x808, 0x818, 0x804, 0x814, 0x80c, 0x81c,
  1403. 0x802, 0x812, 0x80a, 0x81a, 0x806, 0x816, 0x80e, 0x81e,
  1404. 0x801, 0x811, 0x809, 0x819, 0x805, 0x815, 0x80d, 0x81d,
  1405. 0x803, 0x813, 0x80b, 0x81b, 0x807, 0x817, 0x80f, 0x81f,
  1406. 0x800, 0x810, 0x808, 0x818, 0x804, 0x814, 0x80c, 0x81c,
  1407. 0x802, 0x812, 0x80a, 0x81a, 0x806, 0x816, 0x80e, 0x81e,
  1408. 0x801, 0x811, 0x809, 0x819, 0x805, 0x815, 0x80d, 0x81d,
  1409. 0x803, 0x813, 0x80b, 0x81b, 0x807, 0x817, 0x80f, 0x81f,
  1410. 0x800, 0x810, 0x808, 0x818, 0x804, 0x814, 0x80c, 0x81c,
  1411. 0x802, 0x812, 0x80a, 0x81a, 0x806, 0x816, 0x80e, 0x81e,
  1412. 0x801, 0x811, 0x809, 0x819, 0x805, 0x815, 0x80d, 0x81d,
  1413. 0x803, 0x813, 0x80b, 0x81b, 0x807, 0x817, 0x80f, 0x81f,
  1414. 0x800, 0x810, 0x808, 0x818, 0x804, 0x814, 0x80c, 0x81c,
  1415. 0x802, 0x812, 0x80a, 0x81a, 0x806, 0x816, 0x80e, 0x81e,
  1416. 0x801, 0x811, 0x809, 0x819, 0x805, 0x815, 0x80d, 0x81d,
  1417. 0x803, 0x813, 0x80b, 0x81b, 0x807, 0x817, 0x80f, 0x81f,
  1418. 0x800, 0x810, 0x808, 0x818, 0x804, 0x814, 0x80c, 0x81c,
  1419. 0x802, 0x812, 0x80a, 0x81a, 0x806, 0x816, 0x80e, 0x81e,
  1420. 0x801, 0x811, 0x809, 0x819, 0x805, 0x815, 0x80d, 0x81d,
  1421. 0x803, 0x813, 0x80b, 0x81b, 0x807, 0x817, 0x80f, 0x81f,
  1422. };
  1423. /* Inflate a zlib stream from PIN/SIN to POUT/SOUT. Return 1 on
  1424. success, 0 on some error parsing the stream. */
  1425. static int
  1426. elf_zlib_inflate (const unsigned char *pin, size_t sin, uint16_t *zdebug_table,
  1427. unsigned char *pout, size_t sout)
  1428. {
  1429. unsigned char *porigout;
  1430. const unsigned char *pinend;
  1431. unsigned char *poutend;
  1432. /* We can apparently see multiple zlib streams concatenated
  1433. together, so keep going as long as there is something to read.
  1434. The last 4 bytes are the checksum. */
  1435. porigout = pout;
  1436. pinend = pin + sin;
  1437. poutend = pout + sout;
  1438. while ((pinend - pin) > 4)
  1439. {
  1440. uint64_t val;
  1441. unsigned int bits;
  1442. int last;
  1443. /* Read the two byte zlib header. */
  1444. if (unlikely ((pin[0] & 0xf) != 8)) /* 8 is zlib encoding. */
  1445. {
  1446. /* Unknown compression method. */
  1447. elf_uncompress_failed ();
  1448. return 0;
  1449. }
  1450. if (unlikely ((pin[0] >> 4) > 7))
  1451. {
  1452. /* Window size too large. Other than this check, we don't
  1453. care about the window size. */
  1454. elf_uncompress_failed ();
  1455. return 0;
  1456. }
  1457. if (unlikely ((pin[1] & 0x20) != 0))
  1458. {
  1459. /* Stream expects a predefined dictionary, but we have no
  1460. dictionary. */
  1461. elf_uncompress_failed ();
  1462. return 0;
  1463. }
  1464. val = (pin[0] << 8) | pin[1];
  1465. if (unlikely (val % 31 != 0))
  1466. {
  1467. /* Header check failure. */
  1468. elf_uncompress_failed ();
  1469. return 0;
  1470. }
  1471. pin += 2;
  1472. /* Align PIN to a 32-bit boundary. */
  1473. val = 0;
  1474. bits = 0;
  1475. while ((((uintptr_t) pin) & 3) != 0)
  1476. {
  1477. val |= (uint64_t)*pin << bits;
  1478. bits += 8;
  1479. ++pin;
  1480. }
  1481. /* Read blocks until one is marked last. */
  1482. last = 0;
  1483. while (!last)
  1484. {
  1485. unsigned int type;
  1486. const uint16_t *tlit;
  1487. const uint16_t *tdist;
  1488. if (!elf_zlib_fetch (&pin, pinend, &val, &bits))
  1489. return 0;
  1490. last = val & 1;
  1491. type = (val >> 1) & 3;
  1492. val >>= 3;
  1493. bits -= 3;
  1494. if (unlikely (type == 3))
  1495. {
  1496. /* Invalid block type. */
  1497. elf_uncompress_failed ();
  1498. return 0;
  1499. }
  1500. if (type == 0)
  1501. {
  1502. uint16_t len;
  1503. uint16_t lenc;
  1504. /* An uncompressed block. */
  1505. /* If we've read ahead more than a byte, back up. */
  1506. while (bits >= 8)
  1507. {
  1508. --pin;
  1509. bits -= 8;
  1510. }
  1511. val = 0;
  1512. bits = 0;
  1513. if (unlikely ((pinend - pin) < 4))
  1514. {
  1515. /* Missing length. */
  1516. elf_uncompress_failed ();
  1517. return 0;
  1518. }
  1519. len = pin[0] | (pin[1] << 8);
  1520. lenc = pin[2] | (pin[3] << 8);
  1521. pin += 4;
  1522. lenc = ~lenc;
  1523. if (unlikely (len != lenc))
  1524. {
  1525. /* Corrupt data. */
  1526. elf_uncompress_failed ();
  1527. return 0;
  1528. }
  1529. if (unlikely (len > (unsigned int) (pinend - pin)
  1530. || len > (unsigned int) (poutend - pout)))
  1531. {
  1532. /* Not enough space in buffers. */
  1533. elf_uncompress_failed ();
  1534. return 0;
  1535. }
  1536. memcpy (pout, pin, len);
  1537. pout += len;
  1538. pin += len;
  1539. /* Align PIN. */
  1540. while ((((uintptr_t) pin) & 3) != 0)
  1541. {
  1542. val |= (uint64_t)*pin << bits;
  1543. bits += 8;
  1544. ++pin;
  1545. }
  1546. /* Go around to read the next block. */
  1547. continue;
  1548. }
  1549. if (type == 1)
  1550. {
  1551. tlit = elf_zlib_default_table;
  1552. tdist = elf_zlib_default_dist_table;
  1553. }
  1554. else
  1555. {
  1556. unsigned int nlit;
  1557. unsigned int ndist;
  1558. unsigned int nclen;
  1559. unsigned char codebits[19];
  1560. unsigned char *plenbase;
  1561. unsigned char *plen;
  1562. unsigned char *plenend;
  1563. /* Read a Huffman encoding table. The various magic
  1564. numbers here are from RFC 1951. */
  1565. if (!elf_zlib_fetch (&pin, pinend, &val, &bits))
  1566. return 0;
  1567. nlit = (val & 0x1f) + 257;
  1568. val >>= 5;
  1569. ndist = (val & 0x1f) + 1;
  1570. val >>= 5;
  1571. nclen = (val & 0xf) + 4;
  1572. val >>= 4;
  1573. bits -= 14;
  1574. if (unlikely (nlit > 286 || ndist > 30))
  1575. {
  1576. /* Values out of range. */
  1577. elf_uncompress_failed ();
  1578. return 0;
  1579. }
  1580. /* Read and build the table used to compress the
  1581. literal, length, and distance codes. */
  1582. memset(&codebits[0], 0, 19);
  1583. /* There are always at least 4 elements in the
  1584. table. */
  1585. if (!elf_zlib_fetch (&pin, pinend, &val, &bits))
  1586. return 0;
  1587. codebits[16] = val & 7;
  1588. codebits[17] = (val >> 3) & 7;
  1589. codebits[18] = (val >> 6) & 7;
  1590. codebits[0] = (val >> 9) & 7;
  1591. val >>= 12;
  1592. bits -= 12;
  1593. if (nclen == 4)
  1594. goto codebitsdone;
  1595. codebits[8] = val & 7;
  1596. val >>= 3;
  1597. bits -= 3;
  1598. if (nclen == 5)
  1599. goto codebitsdone;
  1600. if (!elf_zlib_fetch (&pin, pinend, &val, &bits))
  1601. return 0;
  1602. codebits[7] = val & 7;
  1603. val >>= 3;
  1604. bits -= 3;
  1605. if (nclen == 6)
  1606. goto codebitsdone;
  1607. codebits[9] = val & 7;
  1608. val >>= 3;
  1609. bits -= 3;
  1610. if (nclen == 7)
  1611. goto codebitsdone;
  1612. codebits[6] = val & 7;
  1613. val >>= 3;
  1614. bits -= 3;
  1615. if (nclen == 8)
  1616. goto codebitsdone;
  1617. codebits[10] = val & 7;
  1618. val >>= 3;
  1619. bits -= 3;
  1620. if (nclen == 9)
  1621. goto codebitsdone;
  1622. codebits[5] = val & 7;
  1623. val >>= 3;
  1624. bits -= 3;
  1625. if (nclen == 10)
  1626. goto codebitsdone;
  1627. if (!elf_zlib_fetch (&pin, pinend, &val, &bits))
  1628. return 0;
  1629. codebits[11] = val & 7;
  1630. val >>= 3;
  1631. bits -= 3;
  1632. if (nclen == 11)
  1633. goto codebitsdone;
  1634. codebits[4] = val & 7;
  1635. val >>= 3;
  1636. bits -= 3;
  1637. if (nclen == 12)
  1638. goto codebitsdone;
  1639. codebits[12] = val & 7;
  1640. val >>= 3;
  1641. bits -= 3;
  1642. if (nclen == 13)
  1643. goto codebitsdone;
  1644. codebits[3] = val & 7;
  1645. val >>= 3;
  1646. bits -= 3;
  1647. if (nclen == 14)
  1648. goto codebitsdone;
  1649. codebits[13] = val & 7;
  1650. val >>= 3;
  1651. bits -= 3;
  1652. if (nclen == 15)
  1653. goto codebitsdone;
  1654. if (!elf_zlib_fetch (&pin, pinend, &val, &bits))
  1655. return 0;
  1656. codebits[2] = val & 7;
  1657. val >>= 3;
  1658. bits -= 3;
  1659. if (nclen == 16)
  1660. goto codebitsdone;
  1661. codebits[14] = val & 7;
  1662. val >>= 3;
  1663. bits -= 3;
  1664. if (nclen == 17)
  1665. goto codebitsdone;
  1666. codebits[1] = val & 7;
  1667. val >>= 3;
  1668. bits -= 3;
  1669. if (nclen == 18)
  1670. goto codebitsdone;
  1671. codebits[15] = val & 7;
  1672. val >>= 3;
  1673. bits -= 3;
  1674. codebitsdone:
  1675. if (!elf_zlib_inflate_table (codebits, 19, zdebug_table,
  1676. zdebug_table))
  1677. return 0;
  1678. /* Read the compressed bit lengths of the literal,
  1679. length, and distance codes. We have allocated space
  1680. at the end of zdebug_table to hold them. */
  1681. plenbase = (((unsigned char *) zdebug_table)
  1682. + ZDEBUG_TABLE_CODELEN_OFFSET);
  1683. plen = plenbase;
  1684. plenend = plen + nlit + ndist;
  1685. while (plen < plenend)
  1686. {
  1687. uint16_t t;
  1688. unsigned int b;
  1689. uint16_t v;
  1690. if (!elf_zlib_fetch (&pin, pinend, &val, &bits))
  1691. return 0;
  1692. t = zdebug_table[val & 0xff];
  1693. /* The compression here uses bit lengths up to 7, so
  1694. a secondary table is never necessary. */
  1695. if (unlikely ((t & (1U << HUFFMAN_SECONDARY_SHIFT)) != 0))
  1696. {
  1697. elf_uncompress_failed ();
  1698. return 0;
  1699. }
  1700. b = (t >> HUFFMAN_BITS_SHIFT) & HUFFMAN_BITS_MASK;
  1701. val >>= b + 1;
  1702. bits -= b + 1;
  1703. v = t & HUFFMAN_VALUE_MASK;
  1704. if (v < 16)
  1705. *plen++ = v;
  1706. else if (v == 16)
  1707. {
  1708. unsigned int c;
  1709. unsigned int prev;
  1710. /* Copy previous entry 3 to 6 times. */
  1711. if (unlikely (plen == plenbase))
  1712. {
  1713. elf_uncompress_failed ();
  1714. return 0;
  1715. }
  1716. /* We used up to 7 bits since the last
  1717. elf_zlib_fetch, so we have at least 8 bits
  1718. available here. */
  1719. c = 3 + (val & 0x3);
  1720. val >>= 2;
  1721. bits -= 2;
  1722. if (unlikely ((unsigned int) (plenend - plen) < c))
  1723. {
  1724. elf_uncompress_failed ();
  1725. return 0;
  1726. }
  1727. prev = plen[-1];
  1728. switch (c)
  1729. {
  1730. case 6:
  1731. *plen++ = prev;
  1732. ATTRIBUTE_FALLTHROUGH;
  1733. case 5:
  1734. *plen++ = prev;
  1735. ATTRIBUTE_FALLTHROUGH;
  1736. case 4:
  1737. *plen++ = prev;
  1738. }
  1739. *plen++ = prev;
  1740. *plen++ = prev;
  1741. *plen++ = prev;
  1742. }
  1743. else if (v == 17)
  1744. {
  1745. unsigned int c;
  1746. /* Store zero 3 to 10 times. */
  1747. /* We used up to 7 bits since the last
  1748. elf_zlib_fetch, so we have at least 8 bits
  1749. available here. */
  1750. c = 3 + (val & 0x7);
  1751. val >>= 3;
  1752. bits -= 3;
  1753. if (unlikely ((unsigned int) (plenend - plen) < c))
  1754. {
  1755. elf_uncompress_failed ();
  1756. return 0;
  1757. }
  1758. switch (c)
  1759. {
  1760. case 10:
  1761. *plen++ = 0;
  1762. ATTRIBUTE_FALLTHROUGH;
  1763. case 9:
  1764. *plen++ = 0;
  1765. ATTRIBUTE_FALLTHROUGH;
  1766. case 8:
  1767. *plen++ = 0;
  1768. ATTRIBUTE_FALLTHROUGH;
  1769. case 7:
  1770. *plen++ = 0;
  1771. ATTRIBUTE_FALLTHROUGH;
  1772. case 6:
  1773. *plen++ = 0;
  1774. ATTRIBUTE_FALLTHROUGH;
  1775. case 5:
  1776. *plen++ = 0;
  1777. ATTRIBUTE_FALLTHROUGH;
  1778. case 4:
  1779. *plen++ = 0;
  1780. }
  1781. *plen++ = 0;
  1782. *plen++ = 0;
  1783. *plen++ = 0;
  1784. }
  1785. else if (v == 18)
  1786. {
  1787. unsigned int c;
  1788. /* Store zero 11 to 138 times. */
  1789. /* We used up to 7 bits since the last
  1790. elf_zlib_fetch, so we have at least 8 bits
  1791. available here. */
  1792. c = 11 + (val & 0x7f);
  1793. val >>= 7;
  1794. bits -= 7;
  1795. if (unlikely ((unsigned int) (plenend - plen) < c))
  1796. {
  1797. elf_uncompress_failed ();
  1798. return 0;
  1799. }
  1800. memset (plen, 0, c);
  1801. plen += c;
  1802. }
  1803. else
  1804. {
  1805. elf_uncompress_failed ();
  1806. return 0;
  1807. }
  1808. }
  1809. /* Make sure that the stop code can appear. */
  1810. plen = plenbase;
  1811. if (unlikely (plen[256] == 0))
  1812. {
  1813. elf_uncompress_failed ();
  1814. return 0;
  1815. }
  1816. /* Build the decompression tables. */
  1817. if (!elf_zlib_inflate_table (plen, nlit, zdebug_table,
  1818. zdebug_table))
  1819. return 0;
  1820. if (!elf_zlib_inflate_table (plen + nlit, ndist, zdebug_table,
  1821. zdebug_table + HUFFMAN_TABLE_SIZE))
  1822. return 0;
  1823. tlit = zdebug_table;
  1824. tdist = zdebug_table + HUFFMAN_TABLE_SIZE;
  1825. }
  1826. /* Inflate values until the end of the block. This is the
  1827. main loop of the inflation code. */
  1828. while (1)
  1829. {
  1830. uint16_t t;
  1831. unsigned int b;
  1832. uint16_t v;
  1833. unsigned int lit;
  1834. if (!elf_zlib_fetch (&pin, pinend, &val, &bits))
  1835. return 0;
  1836. t = tlit[val & 0xff];
  1837. b = (t >> HUFFMAN_BITS_SHIFT) & HUFFMAN_BITS_MASK;
  1838. v = t & HUFFMAN_VALUE_MASK;
  1839. if ((t & (1U << HUFFMAN_SECONDARY_SHIFT)) == 0)
  1840. {
  1841. lit = v;
  1842. val >>= b + 1;
  1843. bits -= b + 1;
  1844. }
  1845. else
  1846. {
  1847. t = tlit[v + 0x100 + ((val >> 8) & ((1U << b) - 1))];
  1848. b = (t >> HUFFMAN_BITS_SHIFT) & HUFFMAN_BITS_MASK;
  1849. lit = t & HUFFMAN_VALUE_MASK;
  1850. val >>= b + 8;
  1851. bits -= b + 8;
  1852. }
  1853. if (lit < 256)
  1854. {
  1855. if (unlikely (pout == poutend))
  1856. {
  1857. elf_uncompress_failed ();
  1858. return 0;
  1859. }
  1860. *pout++ = lit;
  1861. /* We will need to write the next byte soon. We ask
  1862. for high temporal locality because we will write
  1863. to the whole cache line soon. */
  1864. __builtin_prefetch (pout, 1, 3);
  1865. }
  1866. else if (lit == 256)
  1867. {
  1868. /* The end of the block. */
  1869. break;
  1870. }
  1871. else
  1872. {
  1873. unsigned int dist;
  1874. unsigned int len;
  1875. /* Convert lit into a length. */
  1876. if (lit < 265)
  1877. len = lit - 257 + 3;
  1878. else if (lit == 285)
  1879. len = 258;
  1880. else if (unlikely (lit > 285))
  1881. {
  1882. elf_uncompress_failed ();
  1883. return 0;
  1884. }
  1885. else
  1886. {
  1887. unsigned int extra;
  1888. if (!elf_zlib_fetch (&pin, pinend, &val, &bits))
  1889. return 0;
  1890. /* This is an expression for the table of length
  1891. codes in RFC 1951 3.2.5. */
  1892. lit -= 265;
  1893. extra = (lit >> 2) + 1;
  1894. len = (lit & 3) << extra;
  1895. len += 11;
  1896. len += ((1U << (extra - 1)) - 1) << 3;
  1897. len += val & ((1U << extra) - 1);
  1898. val >>= extra;
  1899. bits -= extra;
  1900. }
  1901. if (!elf_zlib_fetch (&pin, pinend, &val, &bits))
  1902. return 0;
  1903. t = tdist[val & 0xff];
  1904. b = (t >> HUFFMAN_BITS_SHIFT) & HUFFMAN_BITS_MASK;
  1905. v = t & HUFFMAN_VALUE_MASK;
  1906. if ((t & (1U << HUFFMAN_SECONDARY_SHIFT)) == 0)
  1907. {
  1908. dist = v;
  1909. val >>= b + 1;
  1910. bits -= b + 1;
  1911. }
  1912. else
  1913. {
  1914. t = tdist[v + 0x100 + ((val >> 8) & ((1U << b) - 1))];
  1915. b = (t >> HUFFMAN_BITS_SHIFT) & HUFFMAN_BITS_MASK;
  1916. dist = t & HUFFMAN_VALUE_MASK;
  1917. val >>= b + 8;
  1918. bits -= b + 8;
  1919. }
  1920. /* Convert dist to a distance. */
  1921. if (dist == 0)
  1922. {
  1923. /* A distance of 1. A common case, meaning
  1924. repeat the last character LEN times. */
  1925. if (unlikely (pout == porigout))
  1926. {
  1927. elf_uncompress_failed ();
  1928. return 0;
  1929. }
  1930. if (unlikely ((unsigned int) (poutend - pout) < len))
  1931. {
  1932. elf_uncompress_failed ();
  1933. return 0;
  1934. }
  1935. memset (pout, pout[-1], len);
  1936. pout += len;
  1937. }
  1938. else if (unlikely (dist > 29))
  1939. {
  1940. elf_uncompress_failed ();
  1941. return 0;
  1942. }
  1943. else
  1944. {
  1945. if (dist < 4)
  1946. dist = dist + 1;
  1947. else
  1948. {
  1949. unsigned int extra;
  1950. if (!elf_zlib_fetch (&pin, pinend, &val, &bits))
  1951. return 0;
  1952. /* This is an expression for the table of
  1953. distance codes in RFC 1951 3.2.5. */
  1954. dist -= 4;
  1955. extra = (dist >> 1) + 1;
  1956. dist = (dist & 1) << extra;
  1957. dist += 5;
  1958. dist += ((1U << (extra - 1)) - 1) << 2;
  1959. dist += val & ((1U << extra) - 1);
  1960. val >>= extra;
  1961. bits -= extra;
  1962. }
  1963. /* Go back dist bytes, and copy len bytes from
  1964. there. */
  1965. if (unlikely ((unsigned int) (pout - porigout) < dist))
  1966. {
  1967. elf_uncompress_failed ();
  1968. return 0;
  1969. }
  1970. if (unlikely ((unsigned int) (poutend - pout) < len))
  1971. {
  1972. elf_uncompress_failed ();
  1973. return 0;
  1974. }
  1975. if (dist >= len)
  1976. {
  1977. memcpy (pout, pout - dist, len);
  1978. pout += len;
  1979. }
  1980. else
  1981. {
  1982. while (len > 0)
  1983. {
  1984. unsigned int copy;
  1985. copy = len < dist ? len : dist;
  1986. memcpy (pout, pout - dist, copy);
  1987. len -= copy;
  1988. pout += copy;
  1989. }
  1990. }
  1991. }
  1992. }
  1993. }
  1994. }
  1995. }
  1996. /* We should have filled the output buffer. */
  1997. if (unlikely (pout != poutend))
  1998. {
  1999. elf_uncompress_failed ();
  2000. return 0;
  2001. }
  2002. return 1;
  2003. }
  2004. /* Verify the zlib checksum. The checksum is in the 4 bytes at
  2005. CHECKBYTES, and the uncompressed data is at UNCOMPRESSED /
  2006. UNCOMPRESSED_SIZE. Returns 1 on success, 0 on failure. */
  2007. static int
  2008. elf_zlib_verify_checksum (const unsigned char *checkbytes,
  2009. const unsigned char *uncompressed,
  2010. size_t uncompressed_size)
  2011. {
  2012. unsigned int i;
  2013. unsigned int cksum;
  2014. const unsigned char *p;
  2015. uint32_t s1;
  2016. uint32_t s2;
  2017. size_t hsz;
  2018. cksum = 0;
  2019. for (i = 0; i < 4; i++)
  2020. cksum = (cksum << 8) | checkbytes[i];
  2021. s1 = 1;
  2022. s2 = 0;
  2023. /* Minimize modulo operations. */
  2024. p = uncompressed;
  2025. hsz = uncompressed_size;
  2026. while (hsz >= 5552)
  2027. {
  2028. for (i = 0; i < 5552; i += 16)
  2029. {
  2030. /* Manually unroll loop 16 times. */
  2031. s1 = s1 + *p++;
  2032. s2 = s2 + s1;
  2033. s1 = s1 + *p++;
  2034. s2 = s2 + s1;
  2035. s1 = s1 + *p++;
  2036. s2 = s2 + s1;
  2037. s1 = s1 + *p++;
  2038. s2 = s2 + s1;
  2039. s1 = s1 + *p++;
  2040. s2 = s2 + s1;
  2041. s1 = s1 + *p++;
  2042. s2 = s2 + s1;
  2043. s1 = s1 + *p++;
  2044. s2 = s2 + s1;
  2045. s1 = s1 + *p++;
  2046. s2 = s2 + s1;
  2047. s1 = s1 + *p++;
  2048. s2 = s2 + s1;
  2049. s1 = s1 + *p++;
  2050. s2 = s2 + s1;
  2051. s1 = s1 + *p++;
  2052. s2 = s2 + s1;
  2053. s1 = s1 + *p++;
  2054. s2 = s2 + s1;
  2055. s1 = s1 + *p++;
  2056. s2 = s2 + s1;
  2057. s1 = s1 + *p++;
  2058. s2 = s2 + s1;
  2059. s1 = s1 + *p++;
  2060. s2 = s2 + s1;
  2061. s1 = s1 + *p++;
  2062. s2 = s2 + s1;
  2063. }
  2064. hsz -= 5552;
  2065. s1 %= 65521;
  2066. s2 %= 65521;
  2067. }
  2068. while (hsz >= 16)
  2069. {
  2070. /* Manually unroll loop 16 times. */
  2071. s1 = s1 + *p++;
  2072. s2 = s2 + s1;
  2073. s1 = s1 + *p++;
  2074. s2 = s2 + s1;
  2075. s1 = s1 + *p++;
  2076. s2 = s2 + s1;
  2077. s1 = s1 + *p++;
  2078. s2 = s2 + s1;
  2079. s1 = s1 + *p++;
  2080. s2 = s2 + s1;
  2081. s1 = s1 + *p++;
  2082. s2 = s2 + s1;
  2083. s1 = s1 + *p++;
  2084. s2 = s2 + s1;
  2085. s1 = s1 + *p++;
  2086. s2 = s2 + s1;
  2087. s1 = s1 + *p++;
  2088. s2 = s2 + s1;
  2089. s1 = s1 + *p++;
  2090. s2 = s2 + s1;
  2091. s1 = s1 + *p++;
  2092. s2 = s2 + s1;
  2093. s1 = s1 + *p++;
  2094. s2 = s2 + s1;
  2095. s1 = s1 + *p++;
  2096. s2 = s2 + s1;
  2097. s1 = s1 + *p++;
  2098. s2 = s2 + s1;
  2099. s1 = s1 + *p++;
  2100. s2 = s2 + s1;
  2101. s1 = s1 + *p++;
  2102. s2 = s2 + s1;
  2103. hsz -= 16;
  2104. }
  2105. for (i = 0; i < hsz; ++i)
  2106. {
  2107. s1 = s1 + *p++;
  2108. s2 = s2 + s1;
  2109. }
  2110. s1 %= 65521;
  2111. s2 %= 65521;
  2112. if (unlikely ((s2 << 16) + s1 != cksum))
  2113. {
  2114. elf_uncompress_failed ();
  2115. return 0;
  2116. }
  2117. return 1;
  2118. }
  2119. /* Inflate a zlib stream from PIN/SIN to POUT/SOUT, and verify the
  2120. checksum. Return 1 on success, 0 on error. */
  2121. static int
  2122. elf_zlib_inflate_and_verify (const unsigned char *pin, size_t sin,
  2123. uint16_t *zdebug_table, unsigned char *pout,
  2124. size_t sout)
  2125. {
  2126. if (!elf_zlib_inflate (pin, sin, zdebug_table, pout, sout))
  2127. return 0;
  2128. if (!elf_zlib_verify_checksum (pin + sin - 4, pout, sout))
  2129. return 0;
  2130. return 1;
  2131. }
  2132. /* Uncompress the old compressed debug format, the one emitted by
  2133. --compress-debug-sections=zlib-gnu. The compressed data is in
  2134. COMPRESSED / COMPRESSED_SIZE, and the function writes to
  2135. *UNCOMPRESSED / *UNCOMPRESSED_SIZE. ZDEBUG_TABLE is work space to
  2136. hold Huffman tables. Returns 0 on error, 1 on successful
  2137. decompression or if something goes wrong. In general we try to
  2138. carry on, by returning 1, even if we can't decompress. */
  2139. static int
  2140. elf_uncompress_zdebug (struct backtrace_state *state,
  2141. const unsigned char *compressed, size_t compressed_size,
  2142. uint16_t *zdebug_table,
  2143. backtrace_error_callback error_callback, void *data,
  2144. unsigned char **uncompressed, size_t *uncompressed_size)
  2145. {
  2146. size_t sz;
  2147. size_t i;
  2148. unsigned char *po;
  2149. *uncompressed = NULL;
  2150. *uncompressed_size = 0;
  2151. /* The format starts with the four bytes ZLIB, followed by the 8
  2152. byte length of the uncompressed data in big-endian order,
  2153. followed by a zlib stream. */
  2154. if (compressed_size < 12 || memcmp (compressed, "ZLIB", 4) != 0)
  2155. return 1;
  2156. sz = 0;
  2157. for (i = 0; i < 8; i++)
  2158. sz = (sz << 8) | compressed[i + 4];
  2159. if (*uncompressed != NULL && *uncompressed_size >= sz)
  2160. po = *uncompressed;
  2161. else
  2162. {
  2163. po = (unsigned char *) backtrace_alloc (state, sz, error_callback, data);
  2164. if (po == NULL)
  2165. return 0;
  2166. }
  2167. if (!elf_zlib_inflate_and_verify (compressed + 12, compressed_size - 12,
  2168. zdebug_table, po, sz))
  2169. return 1;
  2170. *uncompressed = po;
  2171. *uncompressed_size = sz;
  2172. return 1;
  2173. }
  2174. /* Uncompress the new compressed debug format, the official standard
  2175. ELF approach emitted by --compress-debug-sections=zlib-gabi. The
  2176. compressed data is in COMPRESSED / COMPRESSED_SIZE, and the
  2177. function writes to *UNCOMPRESSED / *UNCOMPRESSED_SIZE.
  2178. ZDEBUG_TABLE is work space as for elf_uncompress_zdebug. Returns 0
  2179. on error, 1 on successful decompression or if something goes wrong.
  2180. In general we try to carry on, by returning 1, even if we can't
  2181. decompress. */
  2182. static int
  2183. elf_uncompress_chdr (struct backtrace_state *state,
  2184. const unsigned char *compressed, size_t compressed_size,
  2185. uint16_t *zdebug_table,
  2186. backtrace_error_callback error_callback, void *data,
  2187. unsigned char **uncompressed, size_t *uncompressed_size)
  2188. {
  2189. const b_elf_chdr *chdr;
  2190. unsigned char *po;
  2191. *uncompressed = NULL;
  2192. *uncompressed_size = 0;
  2193. /* The format starts with an ELF compression header. */
  2194. if (compressed_size < sizeof (b_elf_chdr))
  2195. return 1;
  2196. chdr = (const b_elf_chdr *) compressed;
  2197. if (chdr->ch_type != ELFCOMPRESS_ZLIB)
  2198. {
  2199. /* Unsupported compression algorithm. */
  2200. return 1;
  2201. }
  2202. if (*uncompressed != NULL && *uncompressed_size >= chdr->ch_size)
  2203. po = *uncompressed;
  2204. else
  2205. {
  2206. po = (unsigned char *) backtrace_alloc (state, chdr->ch_size,
  2207. error_callback, data);
  2208. if (po == NULL)
  2209. return 0;
  2210. }
  2211. if (!elf_zlib_inflate_and_verify (compressed + sizeof (b_elf_chdr),
  2212. compressed_size - sizeof (b_elf_chdr),
  2213. zdebug_table, po, chdr->ch_size))
  2214. return 1;
  2215. *uncompressed = po;
  2216. *uncompressed_size = chdr->ch_size;
  2217. return 1;
  2218. }
  2219. /* This function is a hook for testing the zlib support. It is only
  2220. used by tests. */
  2221. int
  2222. backtrace_uncompress_zdebug (struct backtrace_state *state,
  2223. const unsigned char *compressed,
  2224. size_t compressed_size,
  2225. backtrace_error_callback error_callback,
  2226. void *data, unsigned char **uncompressed,
  2227. size_t *uncompressed_size)
  2228. {
  2229. uint16_t *zdebug_table;
  2230. int ret;
  2231. zdebug_table = ((uint16_t *) backtrace_alloc (state, ZDEBUG_TABLE_SIZE,
  2232. error_callback, data));
  2233. if (zdebug_table == NULL)
  2234. return 0;
  2235. ret = elf_uncompress_zdebug (state, compressed, compressed_size,
  2236. zdebug_table, error_callback, data,
  2237. uncompressed, uncompressed_size);
  2238. backtrace_free (state, zdebug_table, ZDEBUG_TABLE_SIZE,
  2239. error_callback, data);
  2240. return ret;
  2241. }
  2242. /* Number of LZMA states. */
  2243. #define LZMA_STATES (12)
  2244. /* Number of LZMA position states. The pb value of the property byte
  2245. is the number of bits to include in these states, and the maximum
  2246. value of pb is 4. */
  2247. #define LZMA_POS_STATES (16)
  2248. /* Number of LZMA distance states. These are used match distances
  2249. with a short match length: up to 4 bytes. */
  2250. #define LZMA_DIST_STATES (4)
  2251. /* Number of LZMA distance slots. LZMA uses six bits to encode larger
  2252. match lengths, so 1 << 6 possible probabilities. */
  2253. #define LZMA_DIST_SLOTS (64)
  2254. /* LZMA distances 0 to 3 are encoded directly, larger values use a
  2255. probability model. */
  2256. #define LZMA_DIST_MODEL_START (4)
  2257. /* The LZMA probability model ends at 14. */
  2258. #define LZMA_DIST_MODEL_END (14)
  2259. /* LZMA distance slots for distances less than 127. */
  2260. #define LZMA_FULL_DISTANCES (128)
  2261. /* LZMA uses four alignment bits. */
  2262. #define LZMA_ALIGN_SIZE (16)
  2263. /* LZMA match length is encoded with 4, 5, or 10 bits, some of which
  2264. are already known. */
  2265. #define LZMA_LEN_LOW_SYMBOLS (8)
  2266. #define LZMA_LEN_MID_SYMBOLS (8)
  2267. #define LZMA_LEN_HIGH_SYMBOLS (256)
  2268. /* LZMA literal encoding. */
  2269. #define LZMA_LITERAL_CODERS_MAX (16)
  2270. #define LZMA_LITERAL_CODER_SIZE (0x300)
  2271. /* LZMA is based on a large set of probabilities, each managed
  2272. independently. Each probability is an 11 bit number that we store
  2273. in a uint16_t. We use a single large array of probabilities. */
  2274. /* Lengths of entries in the LZMA probabilities array. The names used
  2275. here are copied from the Linux kernel implementation. */
  2276. #define LZMA_PROB_IS_MATCH_LEN (LZMA_STATES * LZMA_POS_STATES)
  2277. #define LZMA_PROB_IS_REP_LEN LZMA_STATES
  2278. #define LZMA_PROB_IS_REP0_LEN LZMA_STATES
  2279. #define LZMA_PROB_IS_REP1_LEN LZMA_STATES
  2280. #define LZMA_PROB_IS_REP2_LEN LZMA_STATES
  2281. #define LZMA_PROB_IS_REP0_LONG_LEN (LZMA_STATES * LZMA_POS_STATES)
  2282. #define LZMA_PROB_DIST_SLOT_LEN (LZMA_DIST_STATES * LZMA_DIST_SLOTS)
  2283. #define LZMA_PROB_DIST_SPECIAL_LEN (LZMA_FULL_DISTANCES - LZMA_DIST_MODEL_END)
  2284. #define LZMA_PROB_DIST_ALIGN_LEN LZMA_ALIGN_SIZE
  2285. #define LZMA_PROB_MATCH_LEN_CHOICE_LEN 1
  2286. #define LZMA_PROB_MATCH_LEN_CHOICE2_LEN 1
  2287. #define LZMA_PROB_MATCH_LEN_LOW_LEN (LZMA_POS_STATES * LZMA_LEN_LOW_SYMBOLS)
  2288. #define LZMA_PROB_MATCH_LEN_MID_LEN (LZMA_POS_STATES * LZMA_LEN_MID_SYMBOLS)
  2289. #define LZMA_PROB_MATCH_LEN_HIGH_LEN LZMA_LEN_HIGH_SYMBOLS
  2290. #define LZMA_PROB_REP_LEN_CHOICE_LEN 1
  2291. #define LZMA_PROB_REP_LEN_CHOICE2_LEN 1
  2292. #define LZMA_PROB_REP_LEN_LOW_LEN (LZMA_POS_STATES * LZMA_LEN_LOW_SYMBOLS)
  2293. #define LZMA_PROB_REP_LEN_MID_LEN (LZMA_POS_STATES * LZMA_LEN_MID_SYMBOLS)
  2294. #define LZMA_PROB_REP_LEN_HIGH_LEN LZMA_LEN_HIGH_SYMBOLS
  2295. #define LZMA_PROB_LITERAL_LEN \
  2296. (LZMA_LITERAL_CODERS_MAX * LZMA_LITERAL_CODER_SIZE)
  2297. /* Offsets into the LZMA probabilities array. This is mechanically
  2298. generated from the above lengths. */
  2299. #define LZMA_PROB_IS_MATCH_OFFSET 0
  2300. #define LZMA_PROB_IS_REP_OFFSET \
  2301. (LZMA_PROB_IS_MATCH_OFFSET + LZMA_PROB_IS_MATCH_LEN)
  2302. #define LZMA_PROB_IS_REP0_OFFSET \
  2303. (LZMA_PROB_IS_REP_OFFSET + LZMA_PROB_IS_REP_LEN)
  2304. #define LZMA_PROB_IS_REP1_OFFSET \
  2305. (LZMA_PROB_IS_REP0_OFFSET + LZMA_PROB_IS_REP0_LEN)
  2306. #define LZMA_PROB_IS_REP2_OFFSET \
  2307. (LZMA_PROB_IS_REP1_OFFSET + LZMA_PROB_IS_REP1_LEN)
  2308. #define LZMA_PROB_IS_REP0_LONG_OFFSET \
  2309. (LZMA_PROB_IS_REP2_OFFSET + LZMA_PROB_IS_REP2_LEN)
  2310. #define LZMA_PROB_DIST_SLOT_OFFSET \
  2311. (LZMA_PROB_IS_REP0_LONG_OFFSET + LZMA_PROB_IS_REP0_LONG_LEN)
  2312. #define LZMA_PROB_DIST_SPECIAL_OFFSET \
  2313. (LZMA_PROB_DIST_SLOT_OFFSET + LZMA_PROB_DIST_SLOT_LEN)
  2314. #define LZMA_PROB_DIST_ALIGN_OFFSET \
  2315. (LZMA_PROB_DIST_SPECIAL_OFFSET + LZMA_PROB_DIST_SPECIAL_LEN)
  2316. #define LZMA_PROB_MATCH_LEN_CHOICE_OFFSET \
  2317. (LZMA_PROB_DIST_ALIGN_OFFSET + LZMA_PROB_DIST_ALIGN_LEN)
  2318. #define LZMA_PROB_MATCH_LEN_CHOICE2_OFFSET \
  2319. (LZMA_PROB_MATCH_LEN_CHOICE_OFFSET + LZMA_PROB_MATCH_LEN_CHOICE_LEN)
  2320. #define LZMA_PROB_MATCH_LEN_LOW_OFFSET \
  2321. (LZMA_PROB_MATCH_LEN_CHOICE2_OFFSET + LZMA_PROB_MATCH_LEN_CHOICE2_LEN)
  2322. #define LZMA_PROB_MATCH_LEN_MID_OFFSET \
  2323. (LZMA_PROB_MATCH_LEN_LOW_OFFSET + LZMA_PROB_MATCH_LEN_LOW_LEN)
  2324. #define LZMA_PROB_MATCH_LEN_HIGH_OFFSET \
  2325. (LZMA_PROB_MATCH_LEN_MID_OFFSET + LZMA_PROB_MATCH_LEN_MID_LEN)
  2326. #define LZMA_PROB_REP_LEN_CHOICE_OFFSET \
  2327. (LZMA_PROB_MATCH_LEN_HIGH_OFFSET + LZMA_PROB_MATCH_LEN_HIGH_LEN)
  2328. #define LZMA_PROB_REP_LEN_CHOICE2_OFFSET \
  2329. (LZMA_PROB_REP_LEN_CHOICE_OFFSET + LZMA_PROB_REP_LEN_CHOICE_LEN)
  2330. #define LZMA_PROB_REP_LEN_LOW_OFFSET \
  2331. (LZMA_PROB_REP_LEN_CHOICE2_OFFSET + LZMA_PROB_REP_LEN_CHOICE2_LEN)
  2332. #define LZMA_PROB_REP_LEN_MID_OFFSET \
  2333. (LZMA_PROB_REP_LEN_LOW_OFFSET + LZMA_PROB_REP_LEN_LOW_LEN)
  2334. #define LZMA_PROB_REP_LEN_HIGH_OFFSET \
  2335. (LZMA_PROB_REP_LEN_MID_OFFSET + LZMA_PROB_REP_LEN_MID_LEN)
  2336. #define LZMA_PROB_LITERAL_OFFSET \
  2337. (LZMA_PROB_REP_LEN_HIGH_OFFSET + LZMA_PROB_REP_LEN_HIGH_LEN)
  2338. #define LZMA_PROB_TOTAL_COUNT \
  2339. (LZMA_PROB_LITERAL_OFFSET + LZMA_PROB_LITERAL_LEN)
  2340. /* Check that the number of LZMA probabilities is the same as the
  2341. Linux kernel implementation. */
  2342. #if LZMA_PROB_TOTAL_COUNT != 1846 + (1 << 4) * 0x300
  2343. #error Wrong number of LZMA probabilities
  2344. #endif
  2345. /* Expressions for the offset in the LZMA probabilities array of a
  2346. specific probability. */
  2347. #define LZMA_IS_MATCH(state, pos) \
  2348. (LZMA_PROB_IS_MATCH_OFFSET + (state) * LZMA_POS_STATES + (pos))
  2349. #define LZMA_IS_REP(state) \
  2350. (LZMA_PROB_IS_REP_OFFSET + (state))
  2351. #define LZMA_IS_REP0(state) \
  2352. (LZMA_PROB_IS_REP0_OFFSET + (state))
  2353. #define LZMA_IS_REP1(state) \
  2354. (LZMA_PROB_IS_REP1_OFFSET + (state))
  2355. #define LZMA_IS_REP2(state) \
  2356. (LZMA_PROB_IS_REP2_OFFSET + (state))
  2357. #define LZMA_IS_REP0_LONG(state, pos) \
  2358. (LZMA_PROB_IS_REP0_LONG_OFFSET + (state) * LZMA_POS_STATES + (pos))
  2359. #define LZMA_DIST_SLOT(dist, slot) \
  2360. (LZMA_PROB_DIST_SLOT_OFFSET + (dist) * LZMA_DIST_SLOTS + (slot))
  2361. #define LZMA_DIST_SPECIAL(dist) \
  2362. (LZMA_PROB_DIST_SPECIAL_OFFSET + (dist))
  2363. #define LZMA_DIST_ALIGN(dist) \
  2364. (LZMA_PROB_DIST_ALIGN_OFFSET + (dist))
  2365. #define LZMA_MATCH_LEN_CHOICE \
  2366. LZMA_PROB_MATCH_LEN_CHOICE_OFFSET
  2367. #define LZMA_MATCH_LEN_CHOICE2 \
  2368. LZMA_PROB_MATCH_LEN_CHOICE2_OFFSET
  2369. #define LZMA_MATCH_LEN_LOW(pos, sym) \
  2370. (LZMA_PROB_MATCH_LEN_LOW_OFFSET + (pos) * LZMA_LEN_LOW_SYMBOLS + (sym))
  2371. #define LZMA_MATCH_LEN_MID(pos, sym) \
  2372. (LZMA_PROB_MATCH_LEN_MID_OFFSET + (pos) * LZMA_LEN_MID_SYMBOLS + (sym))
  2373. #define LZMA_MATCH_LEN_HIGH(sym) \
  2374. (LZMA_PROB_MATCH_LEN_HIGH_OFFSET + (sym))
  2375. #define LZMA_REP_LEN_CHOICE \
  2376. LZMA_PROB_REP_LEN_CHOICE_OFFSET
  2377. #define LZMA_REP_LEN_CHOICE2 \
  2378. LZMA_PROB_REP_LEN_CHOICE2_OFFSET
  2379. #define LZMA_REP_LEN_LOW(pos, sym) \
  2380. (LZMA_PROB_REP_LEN_LOW_OFFSET + (pos) * LZMA_LEN_LOW_SYMBOLS + (sym))
  2381. #define LZMA_REP_LEN_MID(pos, sym) \
  2382. (LZMA_PROB_REP_LEN_MID_OFFSET + (pos) * LZMA_LEN_MID_SYMBOLS + (sym))
  2383. #define LZMA_REP_LEN_HIGH(sym) \
  2384. (LZMA_PROB_REP_LEN_HIGH_OFFSET + (sym))
  2385. #define LZMA_LITERAL(code, size) \
  2386. (LZMA_PROB_LITERAL_OFFSET + (code) * LZMA_LITERAL_CODER_SIZE + (size))
  2387. /* Read an LZMA varint from BUF, reading and updating *POFFSET,
  2388. setting *VAL. Returns 0 on error, 1 on success. */
  2389. static int
  2390. elf_lzma_varint (const unsigned char *compressed, size_t compressed_size,
  2391. size_t *poffset, uint64_t *val)
  2392. {
  2393. size_t off;
  2394. int i;
  2395. uint64_t v;
  2396. unsigned char b;
  2397. off = *poffset;
  2398. i = 0;
  2399. v = 0;
  2400. while (1)
  2401. {
  2402. if (unlikely (off >= compressed_size))
  2403. {
  2404. elf_uncompress_failed ();
  2405. return 0;
  2406. }
  2407. b = compressed[off];
  2408. v |= (b & 0x7f) << (i * 7);
  2409. ++off;
  2410. if ((b & 0x80) == 0)
  2411. {
  2412. *poffset = off;
  2413. *val = v;
  2414. return 1;
  2415. }
  2416. ++i;
  2417. if (unlikely (i >= 9))
  2418. {
  2419. elf_uncompress_failed ();
  2420. return 0;
  2421. }
  2422. }
  2423. }
  2424. /* Normalize the LZMA range decoder, pulling in an extra input byte if
  2425. needed. */
  2426. static void
  2427. elf_lzma_range_normalize (const unsigned char *compressed,
  2428. size_t compressed_size, size_t *poffset,
  2429. uint32_t *prange, uint32_t *pcode)
  2430. {
  2431. if (*prange < (1U << 24))
  2432. {
  2433. if (unlikely (*poffset >= compressed_size))
  2434. {
  2435. /* We assume this will be caught elsewhere. */
  2436. elf_uncompress_failed ();
  2437. return;
  2438. }
  2439. *prange <<= 8;
  2440. *pcode <<= 8;
  2441. *pcode += compressed[*poffset];
  2442. ++*poffset;
  2443. }
  2444. }
  2445. /* Read and return a single bit from the LZMA stream, reading and
  2446. updating *PROB. Each bit comes from the range coder. */
  2447. static int
  2448. elf_lzma_bit (const unsigned char *compressed, size_t compressed_size,
  2449. uint16_t *prob, size_t *poffset, uint32_t *prange,
  2450. uint32_t *pcode)
  2451. {
  2452. uint32_t bound;
  2453. elf_lzma_range_normalize (compressed, compressed_size, poffset,
  2454. prange, pcode);
  2455. bound = (*prange >> 11) * (uint32_t) *prob;
  2456. if (*pcode < bound)
  2457. {
  2458. *prange = bound;
  2459. *prob += ((1U << 11) - *prob) >> 5;
  2460. return 0;
  2461. }
  2462. else
  2463. {
  2464. *prange -= bound;
  2465. *pcode -= bound;
  2466. *prob -= *prob >> 5;
  2467. return 1;
  2468. }
  2469. }
  2470. /* Read an integer of size BITS from the LZMA stream, most significant
  2471. bit first. The bits are predicted using PROBS. */
  2472. static uint32_t
  2473. elf_lzma_integer (const unsigned char *compressed, size_t compressed_size,
  2474. uint16_t *probs, uint32_t bits, size_t *poffset,
  2475. uint32_t *prange, uint32_t *pcode)
  2476. {
  2477. uint32_t sym;
  2478. uint32_t i;
  2479. sym = 1;
  2480. for (i = 0; i < bits; i++)
  2481. {
  2482. int bit;
  2483. bit = elf_lzma_bit (compressed, compressed_size, probs + sym, poffset,
  2484. prange, pcode);
  2485. sym <<= 1;
  2486. sym += bit;
  2487. }
  2488. return sym - (1 << bits);
  2489. }
  2490. /* Read an integer of size BITS from the LZMA stream, least
  2491. significant bit first. The bits are predicted using PROBS. */
  2492. static uint32_t
  2493. elf_lzma_reverse_integer (const unsigned char *compressed,
  2494. size_t compressed_size, uint16_t *probs,
  2495. uint32_t bits, size_t *poffset, uint32_t *prange,
  2496. uint32_t *pcode)
  2497. {
  2498. uint32_t sym;
  2499. uint32_t val;
  2500. uint32_t i;
  2501. sym = 1;
  2502. val = 0;
  2503. for (i = 0; i < bits; i++)
  2504. {
  2505. int bit;
  2506. bit = elf_lzma_bit (compressed, compressed_size, probs + sym, poffset,
  2507. prange, pcode);
  2508. sym <<= 1;
  2509. sym += bit;
  2510. val += bit << i;
  2511. }
  2512. return val;
  2513. }
  2514. /* Read a length from the LZMA stream. IS_REP picks either LZMA_MATCH
  2515. or LZMA_REP probabilities. */
  2516. static uint32_t
  2517. elf_lzma_len (const unsigned char *compressed, size_t compressed_size,
  2518. uint16_t *probs, int is_rep, unsigned int pos_state,
  2519. size_t *poffset, uint32_t *prange, uint32_t *pcode)
  2520. {
  2521. uint16_t *probs_choice;
  2522. uint16_t *probs_sym;
  2523. uint32_t bits;
  2524. uint32_t len;
  2525. probs_choice = probs + (is_rep
  2526. ? LZMA_REP_LEN_CHOICE
  2527. : LZMA_MATCH_LEN_CHOICE);
  2528. if (elf_lzma_bit (compressed, compressed_size, probs_choice, poffset,
  2529. prange, pcode))
  2530. {
  2531. probs_choice = probs + (is_rep
  2532. ? LZMA_REP_LEN_CHOICE2
  2533. : LZMA_MATCH_LEN_CHOICE2);
  2534. if (elf_lzma_bit (compressed, compressed_size, probs_choice,
  2535. poffset, prange, pcode))
  2536. {
  2537. probs_sym = probs + (is_rep
  2538. ? LZMA_REP_LEN_HIGH (0)
  2539. : LZMA_MATCH_LEN_HIGH (0));
  2540. bits = 8;
  2541. len = 2 + 8 + 8;
  2542. }
  2543. else
  2544. {
  2545. probs_sym = probs + (is_rep
  2546. ? LZMA_REP_LEN_MID (pos_state, 0)
  2547. : LZMA_MATCH_LEN_MID (pos_state, 0));
  2548. bits = 3;
  2549. len = 2 + 8;
  2550. }
  2551. }
  2552. else
  2553. {
  2554. probs_sym = probs + (is_rep
  2555. ? LZMA_REP_LEN_LOW (pos_state, 0)
  2556. : LZMA_MATCH_LEN_LOW (pos_state, 0));
  2557. bits = 3;
  2558. len = 2;
  2559. }
  2560. len += elf_lzma_integer (compressed, compressed_size, probs_sym, bits,
  2561. poffset, prange, pcode);
  2562. return len;
  2563. }
  2564. /* Uncompress one LZMA block from a minidebug file. The compressed
  2565. data is at COMPRESSED + *POFFSET. Update *POFFSET. Store the data
  2566. into the memory at UNCOMPRESSED, size UNCOMPRESSED_SIZE. CHECK is
  2567. the stream flag from the xz header. Return 1 on successful
  2568. decompression. */
  2569. static int
  2570. elf_uncompress_lzma_block (const unsigned char *compressed,
  2571. size_t compressed_size, unsigned char check,
  2572. uint16_t *probs, unsigned char *uncompressed,
  2573. size_t uncompressed_size, size_t *poffset)
  2574. {
  2575. size_t off;
  2576. size_t block_header_offset;
  2577. size_t block_header_size;
  2578. unsigned char block_flags;
  2579. uint64_t header_compressed_size;
  2580. uint64_t header_uncompressed_size;
  2581. unsigned char lzma2_properties;
  2582. uint32_t computed_crc;
  2583. uint32_t stream_crc;
  2584. size_t uncompressed_offset;
  2585. size_t dict_start_offset;
  2586. unsigned int lc;
  2587. unsigned int lp;
  2588. unsigned int pb;
  2589. uint32_t range;
  2590. uint32_t code;
  2591. uint32_t lstate;
  2592. uint32_t dist[4];
  2593. off = *poffset;
  2594. block_header_offset = off;
  2595. /* Block header size is a single byte. */
  2596. if (unlikely (off >= compressed_size))
  2597. {
  2598. elf_uncompress_failed ();
  2599. return 0;
  2600. }
  2601. block_header_size = (compressed[off] + 1) * 4;
  2602. if (unlikely (off + block_header_size > compressed_size))
  2603. {
  2604. elf_uncompress_failed ();
  2605. return 0;
  2606. }
  2607. /* Block flags. */
  2608. block_flags = compressed[off + 1];
  2609. if (unlikely ((block_flags & 0x3c) != 0))
  2610. {
  2611. elf_uncompress_failed ();
  2612. return 0;
  2613. }
  2614. off += 2;
  2615. /* Optional compressed size. */
  2616. header_compressed_size = 0;
  2617. if ((block_flags & 0x40) != 0)
  2618. {
  2619. *poffset = off;
  2620. if (!elf_lzma_varint (compressed, compressed_size, poffset,
  2621. &header_compressed_size))
  2622. return 0;
  2623. off = *poffset;
  2624. }
  2625. /* Optional uncompressed size. */
  2626. header_uncompressed_size = 0;
  2627. if ((block_flags & 0x80) != 0)
  2628. {
  2629. *poffset = off;
  2630. if (!elf_lzma_varint (compressed, compressed_size, poffset,
  2631. &header_uncompressed_size))
  2632. return 0;
  2633. off = *poffset;
  2634. }
  2635. /* The recipe for creating a minidebug file is to run the xz program
  2636. with no arguments, so we expect exactly one filter: lzma2. */
  2637. if (unlikely ((block_flags & 0x3) != 0))
  2638. {
  2639. elf_uncompress_failed ();
  2640. return 0;
  2641. }
  2642. if (unlikely (off + 2 >= block_header_offset + block_header_size))
  2643. {
  2644. elf_uncompress_failed ();
  2645. return 0;
  2646. }
  2647. /* The filter ID for LZMA2 is 0x21. */
  2648. if (unlikely (compressed[off] != 0x21))
  2649. {
  2650. elf_uncompress_failed ();
  2651. return 0;
  2652. }
  2653. ++off;
  2654. /* The size of the filter properties for LZMA2 is 1. */
  2655. if (unlikely (compressed[off] != 1))
  2656. {
  2657. elf_uncompress_failed ();
  2658. return 0;
  2659. }
  2660. ++off;
  2661. lzma2_properties = compressed[off];
  2662. ++off;
  2663. if (unlikely (lzma2_properties > 40))
  2664. {
  2665. elf_uncompress_failed ();
  2666. return 0;
  2667. }
  2668. /* The properties describe the dictionary size, but we don't care
  2669. what that is. */
  2670. /* Block header padding. */
  2671. if (unlikely (off + 4 > compressed_size))
  2672. {
  2673. elf_uncompress_failed ();
  2674. return 0;
  2675. }
  2676. off = (off + 3) &~ (size_t) 3;
  2677. if (unlikely (off + 4 > compressed_size))
  2678. {
  2679. elf_uncompress_failed ();
  2680. return 0;
  2681. }
  2682. /* Block header CRC. */
  2683. computed_crc = elf_crc32 (0, compressed + block_header_offset,
  2684. block_header_size - 4);
  2685. stream_crc = ((uint32_t)compressed[off]
  2686. | ((uint32_t)compressed[off + 1] << 8)
  2687. | ((uint32_t)compressed[off + 2] << 16)
  2688. | ((uint32_t)compressed[off + 3] << 24));
  2689. if (unlikely (computed_crc != stream_crc))
  2690. {
  2691. elf_uncompress_failed ();
  2692. return 0;
  2693. }
  2694. off += 4;
  2695. /* Read a sequence of LZMA2 packets. */
  2696. uncompressed_offset = 0;
  2697. dict_start_offset = 0;
  2698. lc = 0;
  2699. lp = 0;
  2700. pb = 0;
  2701. lstate = 0;
  2702. while (off < compressed_size)
  2703. {
  2704. unsigned char control;
  2705. range = 0xffffffff;
  2706. code = 0;
  2707. control = compressed[off];
  2708. ++off;
  2709. if (unlikely (control == 0))
  2710. {
  2711. /* End of packets. */
  2712. break;
  2713. }
  2714. if (control == 1 || control >= 0xe0)
  2715. {
  2716. /* Reset dictionary to empty. */
  2717. dict_start_offset = uncompressed_offset;
  2718. }
  2719. if (control < 0x80)
  2720. {
  2721. size_t chunk_size;
  2722. /* The only valid values here are 1 or 2. A 1 means to
  2723. reset the dictionary (done above). Then we see an
  2724. uncompressed chunk. */
  2725. if (unlikely (control > 2))
  2726. {
  2727. elf_uncompress_failed ();
  2728. return 0;
  2729. }
  2730. /* An uncompressed chunk is a two byte size followed by
  2731. data. */
  2732. if (unlikely (off + 2 > compressed_size))
  2733. {
  2734. elf_uncompress_failed ();
  2735. return 0;
  2736. }
  2737. chunk_size = compressed[off] << 8;
  2738. chunk_size += compressed[off + 1];
  2739. ++chunk_size;
  2740. off += 2;
  2741. if (unlikely (off + chunk_size > compressed_size))
  2742. {
  2743. elf_uncompress_failed ();
  2744. return 0;
  2745. }
  2746. if (unlikely (uncompressed_offset + chunk_size > uncompressed_size))
  2747. {
  2748. elf_uncompress_failed ();
  2749. return 0;
  2750. }
  2751. memcpy (uncompressed + uncompressed_offset, compressed + off,
  2752. chunk_size);
  2753. uncompressed_offset += chunk_size;
  2754. off += chunk_size;
  2755. }
  2756. else
  2757. {
  2758. size_t uncompressed_chunk_start;
  2759. size_t uncompressed_chunk_size;
  2760. size_t compressed_chunk_size;
  2761. size_t limit;
  2762. /* An LZMA chunk. This starts with an uncompressed size and
  2763. a compressed size. */
  2764. if (unlikely (off + 4 >= compressed_size))
  2765. {
  2766. elf_uncompress_failed ();
  2767. return 0;
  2768. }
  2769. uncompressed_chunk_start = uncompressed_offset;
  2770. uncompressed_chunk_size = (control & 0x1f) << 16;
  2771. uncompressed_chunk_size += compressed[off] << 8;
  2772. uncompressed_chunk_size += compressed[off + 1];
  2773. ++uncompressed_chunk_size;
  2774. compressed_chunk_size = compressed[off + 2] << 8;
  2775. compressed_chunk_size += compressed[off + 3];
  2776. ++compressed_chunk_size;
  2777. off += 4;
  2778. /* Bit 7 (0x80) is set.
  2779. Bits 6 and 5 (0x40 and 0x20) are as follows:
  2780. 0: don't reset anything
  2781. 1: reset state
  2782. 2: reset state, read properties
  2783. 3: reset state, read properties, reset dictionary (done above) */
  2784. if (control >= 0xc0)
  2785. {
  2786. unsigned char props;
  2787. /* Bit 6 is set, read properties. */
  2788. if (unlikely (off >= compressed_size))
  2789. {
  2790. elf_uncompress_failed ();
  2791. return 0;
  2792. }
  2793. props = compressed[off];
  2794. ++off;
  2795. if (unlikely (props > (4 * 5 + 4) * 9 + 8))
  2796. {
  2797. elf_uncompress_failed ();
  2798. return 0;
  2799. }
  2800. pb = 0;
  2801. while (props >= 9 * 5)
  2802. {
  2803. props -= 9 * 5;
  2804. ++pb;
  2805. }
  2806. lp = 0;
  2807. while (props > 9)
  2808. {
  2809. props -= 9;
  2810. ++lp;
  2811. }
  2812. lc = props;
  2813. if (unlikely (lc + lp > 4))
  2814. {
  2815. elf_uncompress_failed ();
  2816. return 0;
  2817. }
  2818. }
  2819. if (control >= 0xa0)
  2820. {
  2821. size_t i;
  2822. /* Bit 5 or 6 is set, reset LZMA state. */
  2823. lstate = 0;
  2824. memset (&dist, 0, sizeof dist);
  2825. for (i = 0; i < LZMA_PROB_TOTAL_COUNT; i++)
  2826. probs[i] = 1 << 10;
  2827. range = 0xffffffff;
  2828. code = 0;
  2829. }
  2830. /* Read the range code. */
  2831. if (unlikely (off + 5 > compressed_size))
  2832. {
  2833. elf_uncompress_failed ();
  2834. return 0;
  2835. }
  2836. /* The byte at compressed[off] is ignored for some
  2837. reason. */
  2838. code = ((compressed[off + 1] << 24)
  2839. + (compressed[off + 2] << 16)
  2840. + (compressed[off + 3] << 8)
  2841. + compressed[off + 4]);
  2842. off += 5;
  2843. /* This is the main LZMA decode loop. */
  2844. limit = off + compressed_chunk_size;
  2845. *poffset = off;
  2846. while (*poffset < limit)
  2847. {
  2848. unsigned int pos_state;
  2849. if (unlikely (uncompressed_offset
  2850. == (uncompressed_chunk_start
  2851. + uncompressed_chunk_size)))
  2852. {
  2853. /* We've decompressed all the expected bytes. */
  2854. break;
  2855. }
  2856. pos_state = ((uncompressed_offset - dict_start_offset)
  2857. & ((1 << pb) - 1));
  2858. if (elf_lzma_bit (compressed, compressed_size,
  2859. probs + LZMA_IS_MATCH (lstate, pos_state),
  2860. poffset, &range, &code))
  2861. {
  2862. uint32_t len;
  2863. if (elf_lzma_bit (compressed, compressed_size,
  2864. probs + LZMA_IS_REP (lstate),
  2865. poffset, &range, &code))
  2866. {
  2867. int short_rep;
  2868. uint32_t next_dist;
  2869. /* Repeated match. */
  2870. short_rep = 0;
  2871. if (elf_lzma_bit (compressed, compressed_size,
  2872. probs + LZMA_IS_REP0 (lstate),
  2873. poffset, &range, &code))
  2874. {
  2875. if (elf_lzma_bit (compressed, compressed_size,
  2876. probs + LZMA_IS_REP1 (lstate),
  2877. poffset, &range, &code))
  2878. {
  2879. if (elf_lzma_bit (compressed, compressed_size,
  2880. probs + LZMA_IS_REP2 (lstate),
  2881. poffset, &range, &code))
  2882. {
  2883. next_dist = dist[3];
  2884. dist[3] = dist[2];
  2885. }
  2886. else
  2887. {
  2888. next_dist = dist[2];
  2889. }
  2890. dist[2] = dist[1];
  2891. }
  2892. else
  2893. {
  2894. next_dist = dist[1];
  2895. }
  2896. dist[1] = dist[0];
  2897. dist[0] = next_dist;
  2898. }
  2899. else
  2900. {
  2901. if (!elf_lzma_bit (compressed, compressed_size,
  2902. (probs
  2903. + LZMA_IS_REP0_LONG (lstate,
  2904. pos_state)),
  2905. poffset, &range, &code))
  2906. short_rep = 1;
  2907. }
  2908. if (lstate < 7)
  2909. lstate = short_rep ? 9 : 8;
  2910. else
  2911. lstate = 11;
  2912. if (short_rep)
  2913. len = 1;
  2914. else
  2915. len = elf_lzma_len (compressed, compressed_size,
  2916. probs, 1, pos_state, poffset,
  2917. &range, &code);
  2918. }
  2919. else
  2920. {
  2921. uint32_t dist_state;
  2922. uint32_t dist_slot;
  2923. uint16_t *probs_dist;
  2924. /* Match. */
  2925. if (lstate < 7)
  2926. lstate = 7;
  2927. else
  2928. lstate = 10;
  2929. dist[3] = dist[2];
  2930. dist[2] = dist[1];
  2931. dist[1] = dist[0];
  2932. len = elf_lzma_len (compressed, compressed_size,
  2933. probs, 0, pos_state, poffset,
  2934. &range, &code);
  2935. if (len < 4 + 2)
  2936. dist_state = len - 2;
  2937. else
  2938. dist_state = 3;
  2939. probs_dist = probs + LZMA_DIST_SLOT (dist_state, 0);
  2940. dist_slot = elf_lzma_integer (compressed,
  2941. compressed_size,
  2942. probs_dist, 6,
  2943. poffset, &range,
  2944. &code);
  2945. if (dist_slot < LZMA_DIST_MODEL_START)
  2946. dist[0] = dist_slot;
  2947. else
  2948. {
  2949. uint32_t limit;
  2950. limit = (dist_slot >> 1) - 1;
  2951. dist[0] = 2 + (dist_slot & 1);
  2952. if (dist_slot < LZMA_DIST_MODEL_END)
  2953. {
  2954. dist[0] <<= limit;
  2955. probs_dist = (probs
  2956. + LZMA_DIST_SPECIAL(dist[0]
  2957. - dist_slot
  2958. - 1));
  2959. dist[0] +=
  2960. elf_lzma_reverse_integer (compressed,
  2961. compressed_size,
  2962. probs_dist,
  2963. limit, poffset,
  2964. &range, &code);
  2965. }
  2966. else
  2967. {
  2968. uint32_t dist0;
  2969. uint32_t i;
  2970. dist0 = dist[0];
  2971. for (i = 0; i < limit - 4; i++)
  2972. {
  2973. uint32_t mask;
  2974. elf_lzma_range_normalize (compressed,
  2975. compressed_size,
  2976. poffset,
  2977. &range, &code);
  2978. range >>= 1;
  2979. code -= range;
  2980. mask = -(code >> 31);
  2981. code += range & mask;
  2982. dist0 <<= 1;
  2983. dist0 += mask + 1;
  2984. }
  2985. dist0 <<= 4;
  2986. probs_dist = probs + LZMA_DIST_ALIGN (0);
  2987. dist0 +=
  2988. elf_lzma_reverse_integer (compressed,
  2989. compressed_size,
  2990. probs_dist, 4,
  2991. poffset,
  2992. &range, &code);
  2993. dist[0] = dist0;
  2994. }
  2995. }
  2996. }
  2997. if (unlikely (uncompressed_offset
  2998. - dict_start_offset < dist[0] + 1))
  2999. {
  3000. elf_uncompress_failed ();
  3001. return 0;
  3002. }
  3003. if (unlikely (uncompressed_offset + len > uncompressed_size))
  3004. {
  3005. elf_uncompress_failed ();
  3006. return 0;
  3007. }
  3008. if (dist[0] == 0)
  3009. {
  3010. /* A common case, meaning repeat the last
  3011. character LEN times. */
  3012. memset (uncompressed + uncompressed_offset,
  3013. uncompressed[uncompressed_offset - 1],
  3014. len);
  3015. uncompressed_offset += len;
  3016. }
  3017. else if (dist[0] + 1 >= len)
  3018. {
  3019. memcpy (uncompressed + uncompressed_offset,
  3020. uncompressed + uncompressed_offset - dist[0] - 1,
  3021. len);
  3022. uncompressed_offset += len;
  3023. }
  3024. else
  3025. {
  3026. while (len > 0)
  3027. {
  3028. uint32_t copy;
  3029. copy = len < dist[0] + 1 ? len : dist[0] + 1;
  3030. memcpy (uncompressed + uncompressed_offset,
  3031. (uncompressed + uncompressed_offset
  3032. - dist[0] - 1),
  3033. copy);
  3034. len -= copy;
  3035. uncompressed_offset += copy;
  3036. }
  3037. }
  3038. }
  3039. else
  3040. {
  3041. unsigned char prev;
  3042. unsigned char low;
  3043. size_t high;
  3044. uint16_t *lit_probs;
  3045. unsigned int sym;
  3046. /* Literal value. */
  3047. if (uncompressed_offset > 0)
  3048. prev = uncompressed[uncompressed_offset - 1];
  3049. else
  3050. prev = 0;
  3051. low = prev >> (8 - lc);
  3052. high = (((uncompressed_offset - dict_start_offset)
  3053. & ((1 << lp) - 1))
  3054. << lc);
  3055. lit_probs = probs + LZMA_LITERAL (low + high, 0);
  3056. if (lstate < 7)
  3057. sym = elf_lzma_integer (compressed, compressed_size,
  3058. lit_probs, 8, poffset, &range,
  3059. &code);
  3060. else
  3061. {
  3062. unsigned int match;
  3063. unsigned int bit;
  3064. unsigned int match_bit;
  3065. unsigned int idx;
  3066. sym = 1;
  3067. if (uncompressed_offset >= dist[0] + 1)
  3068. match = uncompressed[uncompressed_offset - dist[0] - 1];
  3069. else
  3070. match = 0;
  3071. match <<= 1;
  3072. bit = 0x100;
  3073. do
  3074. {
  3075. match_bit = match & bit;
  3076. match <<= 1;
  3077. idx = bit + match_bit + sym;
  3078. sym <<= 1;
  3079. if (elf_lzma_bit (compressed, compressed_size,
  3080. lit_probs + idx, poffset,
  3081. &range, &code))
  3082. {
  3083. ++sym;
  3084. bit &= match_bit;
  3085. }
  3086. else
  3087. {
  3088. bit &= ~ match_bit;
  3089. }
  3090. }
  3091. while (sym < 0x100);
  3092. }
  3093. if (unlikely (uncompressed_offset >= uncompressed_size))
  3094. {
  3095. elf_uncompress_failed ();
  3096. return 0;
  3097. }
  3098. uncompressed[uncompressed_offset] = (unsigned char) sym;
  3099. ++uncompressed_offset;
  3100. if (lstate <= 3)
  3101. lstate = 0;
  3102. else if (lstate <= 9)
  3103. lstate -= 3;
  3104. else
  3105. lstate -= 6;
  3106. }
  3107. }
  3108. elf_lzma_range_normalize (compressed, compressed_size, poffset,
  3109. &range, &code);
  3110. off = *poffset;
  3111. }
  3112. }
  3113. /* We have reached the end of the block. Pad to four byte
  3114. boundary. */
  3115. off = (off + 3) &~ (size_t) 3;
  3116. if (unlikely (off > compressed_size))
  3117. {
  3118. elf_uncompress_failed ();
  3119. return 0;
  3120. }
  3121. switch (check)
  3122. {
  3123. case 0:
  3124. /* No check. */
  3125. break;
  3126. case 1:
  3127. /* CRC32 */
  3128. if (unlikely (off + 4 > compressed_size))
  3129. {
  3130. elf_uncompress_failed ();
  3131. return 0;
  3132. }
  3133. computed_crc = elf_crc32 (0, uncompressed, uncompressed_offset);
  3134. stream_crc = (compressed[off]
  3135. | (compressed[off + 1] << 8)
  3136. | (compressed[off + 2] << 16)
  3137. | (compressed[off + 3] << 24));
  3138. if (computed_crc != stream_crc)
  3139. {
  3140. elf_uncompress_failed ();
  3141. return 0;
  3142. }
  3143. off += 4;
  3144. break;
  3145. case 4:
  3146. /* CRC64. We don't bother computing a CRC64 checksum. */
  3147. if (unlikely (off + 8 > compressed_size))
  3148. {
  3149. elf_uncompress_failed ();
  3150. return 0;
  3151. }
  3152. off += 8;
  3153. break;
  3154. case 10:
  3155. /* SHA. We don't bother computing a SHA checksum. */
  3156. if (unlikely (off + 32 > compressed_size))
  3157. {
  3158. elf_uncompress_failed ();
  3159. return 0;
  3160. }
  3161. off += 32;
  3162. break;
  3163. default:
  3164. elf_uncompress_failed ();
  3165. return 0;
  3166. }
  3167. *poffset = off;
  3168. return 1;
  3169. }
  3170. /* Uncompress LZMA data found in a minidebug file. The minidebug
  3171. format is described at
  3172. https://sourceware.org/gdb/current/onlinedocs/gdb/MiniDebugInfo.html.
  3173. Returns 0 on error, 1 on successful decompression. For this
  3174. function we return 0 on failure to decompress, as the calling code
  3175. will carry on in that case. */
  3176. static int
  3177. elf_uncompress_lzma (struct backtrace_state *state,
  3178. const unsigned char *compressed, size_t compressed_size,
  3179. backtrace_error_callback error_callback, void *data,
  3180. unsigned char **uncompressed, size_t *uncompressed_size)
  3181. {
  3182. size_t header_size;
  3183. size_t footer_size;
  3184. unsigned char check;
  3185. uint32_t computed_crc;
  3186. uint32_t stream_crc;
  3187. size_t offset;
  3188. size_t index_size;
  3189. size_t footer_offset;
  3190. size_t index_offset;
  3191. uint64_t index_compressed_size;
  3192. uint64_t index_uncompressed_size;
  3193. unsigned char *mem;
  3194. uint16_t *probs;
  3195. size_t compressed_block_size;
  3196. /* The format starts with a stream header and ends with a stream
  3197. footer. */
  3198. header_size = 12;
  3199. footer_size = 12;
  3200. if (unlikely (compressed_size < header_size + footer_size))
  3201. {
  3202. elf_uncompress_failed ();
  3203. return 0;
  3204. }
  3205. /* The stream header starts with a magic string. */
  3206. if (unlikely (memcmp (compressed, "\375" "7zXZ\0", 6) != 0))
  3207. {
  3208. elf_uncompress_failed ();
  3209. return 0;
  3210. }
  3211. /* Next come stream flags. The first byte is zero, the second byte
  3212. is the check. */
  3213. if (unlikely (compressed[6] != 0))
  3214. {
  3215. elf_uncompress_failed ();
  3216. return 0;
  3217. }
  3218. check = compressed[7];
  3219. if (unlikely ((check & 0xf8) != 0))
  3220. {
  3221. elf_uncompress_failed ();
  3222. return 0;
  3223. }
  3224. /* Next comes a CRC of the stream flags. */
  3225. computed_crc = elf_crc32 (0, compressed + 6, 2);
  3226. stream_crc = ((uint32_t)compressed[8]
  3227. | ((uint32_t)compressed[9] << 8)
  3228. | ((uint32_t)compressed[10] << 16)
  3229. | ((uint32_t)compressed[11] << 24));
  3230. if (unlikely (computed_crc != stream_crc))
  3231. {
  3232. elf_uncompress_failed ();
  3233. return 0;
  3234. }
  3235. /* Now that we've parsed the header, parse the footer, so that we
  3236. can get the uncompressed size. */
  3237. /* The footer ends with two magic bytes. */
  3238. offset = compressed_size;
  3239. if (unlikely (memcmp (compressed + offset - 2, "YZ", 2) != 0))
  3240. {
  3241. elf_uncompress_failed ();
  3242. return 0;
  3243. }
  3244. offset -= 2;
  3245. /* Before that are the stream flags, which should be the same as the
  3246. flags in the header. */
  3247. if (unlikely (compressed[offset - 2] != 0
  3248. || compressed[offset - 1] != check))
  3249. {
  3250. elf_uncompress_failed ();
  3251. return 0;
  3252. }
  3253. offset -= 2;
  3254. /* Before that is the size of the index field, which precedes the
  3255. footer. */
  3256. index_size = (compressed[offset - 4]
  3257. | (compressed[offset - 3] << 8)
  3258. | (compressed[offset - 2] << 16)
  3259. | (compressed[offset - 1] << 24));
  3260. index_size = (index_size + 1) * 4;
  3261. offset -= 4;
  3262. /* Before that is a footer CRC. */
  3263. computed_crc = elf_crc32 (0, compressed + offset, 6);
  3264. stream_crc = ((uint32_t)compressed[offset - 4]
  3265. | ((uint32_t)compressed[offset - 3] << 8)
  3266. | ((uint32_t)compressed[offset - 2] << 16)
  3267. | ((uint32_t)compressed[offset - 1] << 24));
  3268. if (unlikely (computed_crc != stream_crc))
  3269. {
  3270. elf_uncompress_failed ();
  3271. return 0;
  3272. }
  3273. offset -= 4;
  3274. /* The index comes just before the footer. */
  3275. if (unlikely (offset < index_size + header_size))
  3276. {
  3277. elf_uncompress_failed ();
  3278. return 0;
  3279. }
  3280. footer_offset = offset;
  3281. offset -= index_size;
  3282. index_offset = offset;
  3283. /* The index starts with a zero byte. */
  3284. if (unlikely (compressed[offset] != 0))
  3285. {
  3286. elf_uncompress_failed ();
  3287. return 0;
  3288. }
  3289. ++offset;
  3290. /* Next is the number of blocks. We expect zero blocks for an empty
  3291. stream, and otherwise a single block. */
  3292. if (unlikely (compressed[offset] == 0))
  3293. {
  3294. *uncompressed = NULL;
  3295. *uncompressed_size = 0;
  3296. return 1;
  3297. }
  3298. if (unlikely (compressed[offset] != 1))
  3299. {
  3300. elf_uncompress_failed ();
  3301. return 0;
  3302. }
  3303. ++offset;
  3304. /* Next is the compressed size and the uncompressed size. */
  3305. if (!elf_lzma_varint (compressed, compressed_size, &offset,
  3306. &index_compressed_size))
  3307. return 0;
  3308. if (!elf_lzma_varint (compressed, compressed_size, &offset,
  3309. &index_uncompressed_size))
  3310. return 0;
  3311. /* Pad to a four byte boundary. */
  3312. offset = (offset + 3) &~ (size_t) 3;
  3313. /* Next is a CRC of the index. */
  3314. computed_crc = elf_crc32 (0, compressed + index_offset,
  3315. offset - index_offset);
  3316. stream_crc = ((uint32_t)compressed[offset]
  3317. | ((uint32_t)compressed[offset + 1] << 8)
  3318. | ((uint32_t)compressed[offset + 2] << 16)
  3319. | ((uint32_t)compressed[offset + 3] << 24));
  3320. if (unlikely (computed_crc != stream_crc))
  3321. {
  3322. elf_uncompress_failed ();
  3323. return 0;
  3324. }
  3325. offset += 4;
  3326. /* We should now be back at the footer. */
  3327. if (unlikely (offset != footer_offset))
  3328. {
  3329. elf_uncompress_failed ();
  3330. return 0;
  3331. }
  3332. /* Allocate space to hold the uncompressed data. If we succeed in
  3333. uncompressing the LZMA data, we never free this memory. */
  3334. mem = (unsigned char *) backtrace_alloc (state, index_uncompressed_size,
  3335. error_callback, data);
  3336. if (unlikely (mem == NULL))
  3337. return 0;
  3338. *uncompressed = mem;
  3339. *uncompressed_size = index_uncompressed_size;
  3340. /* Allocate space for probabilities. */
  3341. probs = ((uint16_t *)
  3342. backtrace_alloc (state,
  3343. LZMA_PROB_TOTAL_COUNT * sizeof (uint16_t),
  3344. error_callback, data));
  3345. if (unlikely (probs == NULL))
  3346. {
  3347. backtrace_free (state, mem, index_uncompressed_size, error_callback,
  3348. data);
  3349. return 0;
  3350. }
  3351. /* Uncompress the block, which follows the header. */
  3352. offset = 12;
  3353. if (!elf_uncompress_lzma_block (compressed, compressed_size, check, probs,
  3354. mem, index_uncompressed_size, &offset))
  3355. {
  3356. backtrace_free (state, mem, index_uncompressed_size, error_callback,
  3357. data);
  3358. return 0;
  3359. }
  3360. compressed_block_size = offset - 12;
  3361. if (unlikely (compressed_block_size
  3362. != ((index_compressed_size + 3) &~ (size_t) 3)))
  3363. {
  3364. elf_uncompress_failed ();
  3365. backtrace_free (state, mem, index_uncompressed_size, error_callback,
  3366. data);
  3367. return 0;
  3368. }
  3369. offset = (offset + 3) &~ (size_t) 3;
  3370. if (unlikely (offset != index_offset))
  3371. {
  3372. elf_uncompress_failed ();
  3373. backtrace_free (state, mem, index_uncompressed_size, error_callback,
  3374. data);
  3375. return 0;
  3376. }
  3377. return 1;
  3378. }
  3379. /* This function is a hook for testing the LZMA support. It is only
  3380. used by tests. */
  3381. int
  3382. backtrace_uncompress_lzma (struct backtrace_state *state,
  3383. const unsigned char *compressed,
  3384. size_t compressed_size,
  3385. backtrace_error_callback error_callback,
  3386. void *data, unsigned char **uncompressed,
  3387. size_t *uncompressed_size)
  3388. {
  3389. return elf_uncompress_lzma (state, compressed, compressed_size,
  3390. error_callback, data, uncompressed,
  3391. uncompressed_size);
  3392. }
  3393. /* Add the backtrace data for one ELF file. Returns 1 on success,
  3394. 0 on failure (in both cases descriptor is closed) or -1 if exe
  3395. is non-zero and the ELF file is ET_DYN, which tells the caller that
  3396. elf_add will need to be called on the descriptor again after
  3397. base_address is determined. */
  3398. static int
  3399. elf_add (struct backtrace_state *state, const char *filename, int descriptor,
  3400. const unsigned char *memory, size_t memory_size,
  3401. uintptr_t base_address, backtrace_error_callback error_callback,
  3402. void *data, fileline *fileline_fn, int *found_sym, int *found_dwarf,
  3403. struct dwarf_data **fileline_entry, int exe, int debuginfo,
  3404. const char *with_buildid_data, uint32_t with_buildid_size)
  3405. {
  3406. struct elf_view ehdr_view;
  3407. b_elf_ehdr ehdr;
  3408. off_t shoff;
  3409. unsigned int shnum;
  3410. unsigned int shstrndx;
  3411. struct elf_view shdrs_view;
  3412. int shdrs_view_valid;
  3413. const b_elf_shdr *shdrs;
  3414. const b_elf_shdr *shstrhdr;
  3415. size_t shstr_size;
  3416. off_t shstr_off;
  3417. struct elf_view names_view;
  3418. int names_view_valid;
  3419. const char *names;
  3420. unsigned int symtab_shndx;
  3421. unsigned int dynsym_shndx;
  3422. unsigned int i;
  3423. struct debug_section_info sections[DEBUG_MAX];
  3424. struct debug_section_info zsections[DEBUG_MAX];
  3425. struct elf_view symtab_view;
  3426. int symtab_view_valid;
  3427. struct elf_view strtab_view;
  3428. int strtab_view_valid;
  3429. struct elf_view buildid_view;
  3430. int buildid_view_valid;
  3431. const char *buildid_data;
  3432. uint32_t buildid_size;
  3433. struct elf_view debuglink_view;
  3434. int debuglink_view_valid;
  3435. const char *debuglink_name;
  3436. uint32_t debuglink_crc;
  3437. struct elf_view debugaltlink_view;
  3438. int debugaltlink_view_valid;
  3439. const char *debugaltlink_name;
  3440. const char *debugaltlink_buildid_data;
  3441. uint32_t debugaltlink_buildid_size;
  3442. struct elf_view gnu_debugdata_view;
  3443. int gnu_debugdata_view_valid;
  3444. size_t gnu_debugdata_size;
  3445. unsigned char *gnu_debugdata_uncompressed;
  3446. size_t gnu_debugdata_uncompressed_size;
  3447. off_t min_offset;
  3448. off_t max_offset;
  3449. off_t debug_size;
  3450. struct elf_view debug_view;
  3451. int debug_view_valid;
  3452. unsigned int using_debug_view;
  3453. uint16_t *zdebug_table;
  3454. struct elf_view split_debug_view[DEBUG_MAX];
  3455. unsigned char split_debug_view_valid[DEBUG_MAX];
  3456. struct elf_ppc64_opd_data opd_data, *opd;
  3457. struct dwarf_sections dwarf_sections;
  3458. if (!debuginfo)
  3459. {
  3460. *found_sym = 0;
  3461. *found_dwarf = 0;
  3462. }
  3463. shdrs_view_valid = 0;
  3464. names_view_valid = 0;
  3465. symtab_view_valid = 0;
  3466. strtab_view_valid = 0;
  3467. buildid_view_valid = 0;
  3468. buildid_data = NULL;
  3469. buildid_size = 0;
  3470. debuglink_view_valid = 0;
  3471. debuglink_name = NULL;
  3472. debuglink_crc = 0;
  3473. debugaltlink_view_valid = 0;
  3474. debugaltlink_name = NULL;
  3475. debugaltlink_buildid_data = NULL;
  3476. debugaltlink_buildid_size = 0;
  3477. gnu_debugdata_view_valid = 0;
  3478. gnu_debugdata_size = 0;
  3479. debug_view_valid = 0;
  3480. memset (&split_debug_view_valid[0], 0, sizeof split_debug_view_valid);
  3481. opd = NULL;
  3482. if (!elf_get_view (state, descriptor, memory, memory_size, 0, sizeof ehdr,
  3483. error_callback, data, &ehdr_view))
  3484. goto fail;
  3485. memcpy (&ehdr, ehdr_view.view.data, sizeof ehdr);
  3486. elf_release_view (state, &ehdr_view, error_callback, data);
  3487. if (ehdr.e_ident[EI_MAG0] != ELFMAG0
  3488. || ehdr.e_ident[EI_MAG1] != ELFMAG1
  3489. || ehdr.e_ident[EI_MAG2] != ELFMAG2
  3490. || ehdr.e_ident[EI_MAG3] != ELFMAG3)
  3491. {
  3492. error_callback (data, "executable file is not ELF", 0);
  3493. goto fail;
  3494. }
  3495. if (ehdr.e_ident[EI_VERSION] != EV_CURRENT)
  3496. {
  3497. error_callback (data, "executable file is unrecognized ELF version", 0);
  3498. goto fail;
  3499. }
  3500. #if BACKTRACE_ELF_SIZE == 32
  3501. #define BACKTRACE_ELFCLASS ELFCLASS32
  3502. #else
  3503. #define BACKTRACE_ELFCLASS ELFCLASS64
  3504. #endif
  3505. if (ehdr.e_ident[EI_CLASS] != BACKTRACE_ELFCLASS)
  3506. {
  3507. error_callback (data, "executable file is unexpected ELF class", 0);
  3508. goto fail;
  3509. }
  3510. if (ehdr.e_ident[EI_DATA] != ELFDATA2LSB
  3511. && ehdr.e_ident[EI_DATA] != ELFDATA2MSB)
  3512. {
  3513. error_callback (data, "executable file has unknown endianness", 0);
  3514. goto fail;
  3515. }
  3516. /* If the executable is ET_DYN, it is either a PIE, or we are running
  3517. directly a shared library with .interp. We need to wait for
  3518. dl_iterate_phdr in that case to determine the actual base_address. */
  3519. if (exe && ehdr.e_type == ET_DYN)
  3520. return -1;
  3521. shoff = ehdr.e_shoff;
  3522. shnum = ehdr.e_shnum;
  3523. shstrndx = ehdr.e_shstrndx;
  3524. if ((shnum == 0 || shstrndx == SHN_XINDEX)
  3525. && shoff != 0)
  3526. {
  3527. struct elf_view shdr_view;
  3528. const b_elf_shdr *shdr;
  3529. if (!elf_get_view (state, descriptor, memory, memory_size, shoff,
  3530. sizeof shdr, error_callback, data, &shdr_view))
  3531. goto fail;
  3532. shdr = (const b_elf_shdr *) shdr_view.view.data;
  3533. if (shnum == 0)
  3534. shnum = shdr->sh_size;
  3535. if (shstrndx == SHN_XINDEX)
  3536. {
  3537. shstrndx = shdr->sh_link;
  3538. /* Versions of the GNU binutils between 2.12 and 2.18 did
  3539. not handle objects with more than SHN_LORESERVE sections
  3540. correctly. All large section indexes were offset by
  3541. 0x100. There is more information at
  3542. http://sourceware.org/bugzilla/show_bug.cgi?id-5900 .
  3543. Fortunately these object files are easy to detect, as the
  3544. GNU binutils always put the section header string table
  3545. near the end of the list of sections. Thus if the
  3546. section header string table index is larger than the
  3547. number of sections, then we know we have to subtract
  3548. 0x100 to get the real section index. */
  3549. if (shstrndx >= shnum && shstrndx >= SHN_LORESERVE + 0x100)
  3550. shstrndx -= 0x100;
  3551. }
  3552. elf_release_view (state, &shdr_view, error_callback, data);
  3553. }
  3554. if (shnum == 0 || shstrndx == 0)
  3555. goto fail;
  3556. /* To translate PC to file/line when using DWARF, we need to find
  3557. the .debug_info and .debug_line sections. */
  3558. /* Read the section headers, skipping the first one. */
  3559. if (!elf_get_view (state, descriptor, memory, memory_size,
  3560. shoff + sizeof (b_elf_shdr),
  3561. (shnum - 1) * sizeof (b_elf_shdr),
  3562. error_callback, data, &shdrs_view))
  3563. goto fail;
  3564. shdrs_view_valid = 1;
  3565. shdrs = (const b_elf_shdr *) shdrs_view.view.data;
  3566. /* Read the section names. */
  3567. shstrhdr = &shdrs[shstrndx - 1];
  3568. shstr_size = shstrhdr->sh_size;
  3569. shstr_off = shstrhdr->sh_offset;
  3570. if (!elf_get_view (state, descriptor, memory, memory_size, shstr_off,
  3571. shstrhdr->sh_size, error_callback, data, &names_view))
  3572. goto fail;
  3573. names_view_valid = 1;
  3574. names = (const char *) names_view.view.data;
  3575. symtab_shndx = 0;
  3576. dynsym_shndx = 0;
  3577. memset (sections, 0, sizeof sections);
  3578. memset (zsections, 0, sizeof zsections);
  3579. /* Look for the symbol table. */
  3580. for (i = 1; i < shnum; ++i)
  3581. {
  3582. const b_elf_shdr *shdr;
  3583. unsigned int sh_name;
  3584. const char *name;
  3585. int j;
  3586. shdr = &shdrs[i - 1];
  3587. if (shdr->sh_type == SHT_SYMTAB)
  3588. symtab_shndx = i;
  3589. else if (shdr->sh_type == SHT_DYNSYM)
  3590. dynsym_shndx = i;
  3591. sh_name = shdr->sh_name;
  3592. if (sh_name >= shstr_size)
  3593. {
  3594. error_callback (data, "ELF section name out of range", 0);
  3595. goto fail;
  3596. }
  3597. name = names + sh_name;
  3598. for (j = 0; j < (int) DEBUG_MAX; ++j)
  3599. {
  3600. if (strcmp (name, dwarf_section_names[j]) == 0)
  3601. {
  3602. sections[j].offset = shdr->sh_offset;
  3603. sections[j].size = shdr->sh_size;
  3604. sections[j].compressed = (shdr->sh_flags & SHF_COMPRESSED) != 0;
  3605. break;
  3606. }
  3607. }
  3608. if (name[0] == '.' && name[1] == 'z')
  3609. {
  3610. for (j = 0; j < (int) DEBUG_MAX; ++j)
  3611. {
  3612. if (strcmp (name + 2, dwarf_section_names[j] + 1) == 0)
  3613. {
  3614. zsections[j].offset = shdr->sh_offset;
  3615. zsections[j].size = shdr->sh_size;
  3616. break;
  3617. }
  3618. }
  3619. }
  3620. /* Read the build ID if present. This could check for any
  3621. SHT_NOTE section with the right note name and type, but gdb
  3622. looks for a specific section name. */
  3623. if ((!debuginfo || with_buildid_data != NULL)
  3624. && !buildid_view_valid
  3625. && strcmp (name, ".note.gnu.build-id") == 0)
  3626. {
  3627. const b_elf_note *note;
  3628. if (!elf_get_view (state, descriptor, memory, memory_size,
  3629. shdr->sh_offset, shdr->sh_size, error_callback,
  3630. data, &buildid_view))
  3631. goto fail;
  3632. buildid_view_valid = 1;
  3633. note = (const b_elf_note *) buildid_view.view.data;
  3634. if (note->type == NT_GNU_BUILD_ID
  3635. && note->namesz == 4
  3636. && strncmp (note->name, "GNU", 4) == 0
  3637. && shdr->sh_size <= 12 + ((note->namesz + 3) & ~ 3) + note->descsz)
  3638. {
  3639. buildid_data = &note->name[0] + ((note->namesz + 3) & ~ 3);
  3640. buildid_size = note->descsz;
  3641. }
  3642. if (with_buildid_size != 0)
  3643. {
  3644. if (buildid_size != with_buildid_size)
  3645. goto fail;
  3646. if (memcmp (buildid_data, with_buildid_data, buildid_size) != 0)
  3647. goto fail;
  3648. }
  3649. }
  3650. /* Read the debuglink file if present. */
  3651. if (!debuginfo
  3652. && !debuglink_view_valid
  3653. && strcmp (name, ".gnu_debuglink") == 0)
  3654. {
  3655. const char *debuglink_data;
  3656. size_t crc_offset;
  3657. if (!elf_get_view (state, descriptor, memory, memory_size,
  3658. shdr->sh_offset, shdr->sh_size, error_callback,
  3659. data, &debuglink_view))
  3660. goto fail;
  3661. debuglink_view_valid = 1;
  3662. debuglink_data = (const char *) debuglink_view.view.data;
  3663. crc_offset = strnlen (debuglink_data, shdr->sh_size);
  3664. crc_offset = (crc_offset + 3) & ~3;
  3665. if (crc_offset + 4 <= shdr->sh_size)
  3666. {
  3667. debuglink_name = debuglink_data;
  3668. debuglink_crc = *(const uint32_t*)(debuglink_data + crc_offset);
  3669. }
  3670. }
  3671. if (!debugaltlink_view_valid
  3672. && strcmp (name, ".gnu_debugaltlink") == 0)
  3673. {
  3674. const char *debugaltlink_data;
  3675. size_t debugaltlink_name_len;
  3676. if (!elf_get_view (state, descriptor, memory, memory_size,
  3677. shdr->sh_offset, shdr->sh_size, error_callback,
  3678. data, &debugaltlink_view))
  3679. goto fail;
  3680. debugaltlink_view_valid = 1;
  3681. debugaltlink_data = (const char *) debugaltlink_view.view.data;
  3682. debugaltlink_name = debugaltlink_data;
  3683. debugaltlink_name_len = strnlen (debugaltlink_data, shdr->sh_size);
  3684. if (debugaltlink_name_len < shdr->sh_size)
  3685. {
  3686. /* Include terminating zero. */
  3687. debugaltlink_name_len += 1;
  3688. debugaltlink_buildid_data
  3689. = debugaltlink_data + debugaltlink_name_len;
  3690. debugaltlink_buildid_size = shdr->sh_size - debugaltlink_name_len;
  3691. }
  3692. }
  3693. if (!gnu_debugdata_view_valid
  3694. && strcmp (name, ".gnu_debugdata") == 0)
  3695. {
  3696. if (!elf_get_view (state, descriptor, memory, memory_size,
  3697. shdr->sh_offset, shdr->sh_size, error_callback,
  3698. data, &gnu_debugdata_view))
  3699. goto fail;
  3700. gnu_debugdata_size = shdr->sh_size;
  3701. gnu_debugdata_view_valid = 1;
  3702. }
  3703. /* Read the .opd section on PowerPC64 ELFv1. */
  3704. if (ehdr.e_machine == EM_PPC64
  3705. && (ehdr.e_flags & EF_PPC64_ABI) < 2
  3706. && shdr->sh_type == SHT_PROGBITS
  3707. && strcmp (name, ".opd") == 0)
  3708. {
  3709. if (!elf_get_view (state, descriptor, memory, memory_size,
  3710. shdr->sh_offset, shdr->sh_size, error_callback,
  3711. data, &opd_data.view))
  3712. goto fail;
  3713. opd = &opd_data;
  3714. opd->addr = shdr->sh_addr;
  3715. opd->data = (const char *) opd_data.view.view.data;
  3716. opd->size = shdr->sh_size;
  3717. }
  3718. }
  3719. if (symtab_shndx == 0)
  3720. symtab_shndx = dynsym_shndx;
  3721. if (symtab_shndx != 0 && !debuginfo)
  3722. {
  3723. const b_elf_shdr *symtab_shdr;
  3724. unsigned int strtab_shndx;
  3725. const b_elf_shdr *strtab_shdr;
  3726. struct elf_syminfo_data *sdata;
  3727. symtab_shdr = &shdrs[symtab_shndx - 1];
  3728. strtab_shndx = symtab_shdr->sh_link;
  3729. if (strtab_shndx >= shnum)
  3730. {
  3731. error_callback (data,
  3732. "ELF symbol table strtab link out of range", 0);
  3733. goto fail;
  3734. }
  3735. strtab_shdr = &shdrs[strtab_shndx - 1];
  3736. if (!elf_get_view (state, descriptor, memory, memory_size,
  3737. symtab_shdr->sh_offset, symtab_shdr->sh_size,
  3738. error_callback, data, &symtab_view))
  3739. goto fail;
  3740. symtab_view_valid = 1;
  3741. if (!elf_get_view (state, descriptor, memory, memory_size,
  3742. strtab_shdr->sh_offset, strtab_shdr->sh_size,
  3743. error_callback, data, &strtab_view))
  3744. goto fail;
  3745. strtab_view_valid = 1;
  3746. sdata = ((struct elf_syminfo_data *)
  3747. backtrace_alloc (state, sizeof *sdata, error_callback, data));
  3748. if (sdata == NULL)
  3749. goto fail;
  3750. if (!elf_initialize_syminfo (state, base_address,
  3751. symtab_view.view.data, symtab_shdr->sh_size,
  3752. strtab_view.view.data, strtab_shdr->sh_size,
  3753. error_callback, data, sdata, opd))
  3754. {
  3755. backtrace_free (state, sdata, sizeof *sdata, error_callback, data);
  3756. goto fail;
  3757. }
  3758. /* We no longer need the symbol table, but we hold on to the
  3759. string table permanently. */
  3760. elf_release_view (state, &symtab_view, error_callback, data);
  3761. symtab_view_valid = 0;
  3762. strtab_view_valid = 0;
  3763. *found_sym = 1;
  3764. elf_add_syminfo_data (state, sdata);
  3765. }
  3766. elf_release_view (state, &shdrs_view, error_callback, data);
  3767. shdrs_view_valid = 0;
  3768. elf_release_view (state, &names_view, error_callback, data);
  3769. names_view_valid = 0;
  3770. /* If the debug info is in a separate file, read that one instead. */
  3771. if (buildid_data != NULL)
  3772. {
  3773. int d;
  3774. d = elf_open_debugfile_by_buildid (state, buildid_data, buildid_size,
  3775. error_callback, data);
  3776. if (d >= 0)
  3777. {
  3778. int ret;
  3779. elf_release_view (state, &buildid_view, error_callback, data);
  3780. if (debuglink_view_valid)
  3781. elf_release_view (state, &debuglink_view, error_callback, data);
  3782. if (debugaltlink_view_valid)
  3783. elf_release_view (state, &debugaltlink_view, error_callback, data);
  3784. ret = elf_add (state, "", d, NULL, 0, base_address, error_callback,
  3785. data, fileline_fn, found_sym, found_dwarf, NULL, 0,
  3786. 1, NULL, 0);
  3787. if (ret < 0)
  3788. backtrace_close (d, error_callback, data);
  3789. else if (descriptor >= 0)
  3790. backtrace_close (descriptor, error_callback, data);
  3791. return ret;
  3792. }
  3793. }
  3794. if (buildid_view_valid)
  3795. {
  3796. elf_release_view (state, &buildid_view, error_callback, data);
  3797. buildid_view_valid = 0;
  3798. }
  3799. if (opd)
  3800. {
  3801. elf_release_view (state, &opd->view, error_callback, data);
  3802. opd = NULL;
  3803. }
  3804. if (debuglink_name != NULL)
  3805. {
  3806. int d;
  3807. d = elf_open_debugfile_by_debuglink (state, filename, debuglink_name,
  3808. debuglink_crc, error_callback,
  3809. data);
  3810. if (d >= 0)
  3811. {
  3812. int ret;
  3813. elf_release_view (state, &debuglink_view, error_callback, data);
  3814. if (debugaltlink_view_valid)
  3815. elf_release_view (state, &debugaltlink_view, error_callback, data);
  3816. ret = elf_add (state, "", d, NULL, 0, base_address, error_callback,
  3817. data, fileline_fn, found_sym, found_dwarf, NULL, 0,
  3818. 1, NULL, 0);
  3819. if (ret < 0)
  3820. backtrace_close (d, error_callback, data);
  3821. else if (descriptor >= 0)
  3822. backtrace_close(descriptor, error_callback, data);
  3823. return ret;
  3824. }
  3825. }
  3826. if (debuglink_view_valid)
  3827. {
  3828. elf_release_view (state, &debuglink_view, error_callback, data);
  3829. debuglink_view_valid = 0;
  3830. }
  3831. struct dwarf_data *fileline_altlink = NULL;
  3832. if (debugaltlink_name != NULL)
  3833. {
  3834. int d;
  3835. d = elf_open_debugfile_by_debuglink (state, filename, debugaltlink_name,
  3836. 0, error_callback, data);
  3837. if (d >= 0)
  3838. {
  3839. int ret;
  3840. ret = elf_add (state, filename, d, NULL, 0, base_address,
  3841. error_callback, data, fileline_fn, found_sym,
  3842. found_dwarf, &fileline_altlink, 0, 1,
  3843. debugaltlink_buildid_data, debugaltlink_buildid_size);
  3844. elf_release_view (state, &debugaltlink_view, error_callback, data);
  3845. debugaltlink_view_valid = 0;
  3846. if (ret < 0)
  3847. {
  3848. backtrace_close (d, error_callback, data);
  3849. return ret;
  3850. }
  3851. }
  3852. }
  3853. if (debugaltlink_view_valid)
  3854. {
  3855. elf_release_view (state, &debugaltlink_view, error_callback, data);
  3856. debugaltlink_view_valid = 0;
  3857. }
  3858. if (gnu_debugdata_view_valid)
  3859. {
  3860. int ret;
  3861. ret = elf_uncompress_lzma (state,
  3862. ((const unsigned char *)
  3863. gnu_debugdata_view.view.data),
  3864. gnu_debugdata_size, error_callback, data,
  3865. &gnu_debugdata_uncompressed,
  3866. &gnu_debugdata_uncompressed_size);
  3867. elf_release_view (state, &gnu_debugdata_view, error_callback, data);
  3868. gnu_debugdata_view_valid = 0;
  3869. if (ret)
  3870. {
  3871. ret = elf_add (state, filename, -1, gnu_debugdata_uncompressed,
  3872. gnu_debugdata_uncompressed_size, base_address,
  3873. error_callback, data, fileline_fn, found_sym,
  3874. found_dwarf, NULL, 0, 0, NULL, 0);
  3875. if (ret >= 0 && descriptor >= 0)
  3876. backtrace_close(descriptor, error_callback, data);
  3877. return ret;
  3878. }
  3879. }
  3880. /* Read all the debug sections in a single view, since they are
  3881. probably adjacent in the file. If any of sections are
  3882. uncompressed, we never release this view. */
  3883. min_offset = 0;
  3884. max_offset = 0;
  3885. debug_size = 0;
  3886. for (i = 0; i < (int) DEBUG_MAX; ++i)
  3887. {
  3888. off_t end;
  3889. if (sections[i].size != 0)
  3890. {
  3891. if (min_offset == 0 || sections[i].offset < min_offset)
  3892. min_offset = sections[i].offset;
  3893. end = sections[i].offset + sections[i].size;
  3894. if (end > max_offset)
  3895. max_offset = end;
  3896. debug_size += sections[i].size;
  3897. }
  3898. if (zsections[i].size != 0)
  3899. {
  3900. if (min_offset == 0 || zsections[i].offset < min_offset)
  3901. min_offset = zsections[i].offset;
  3902. end = zsections[i].offset + zsections[i].size;
  3903. if (end > max_offset)
  3904. max_offset = end;
  3905. debug_size += zsections[i].size;
  3906. }
  3907. }
  3908. if (min_offset == 0 || max_offset == 0)
  3909. {
  3910. if (descriptor >= 0)
  3911. {
  3912. if (!backtrace_close (descriptor, error_callback, data))
  3913. goto fail;
  3914. }
  3915. return 1;
  3916. }
  3917. /* If the total debug section size is large, assume that there are
  3918. gaps between the sections, and read them individually. */
  3919. if (max_offset - min_offset < 0x20000000
  3920. || max_offset - min_offset < debug_size + 0x10000)
  3921. {
  3922. if (!elf_get_view (state, descriptor, memory, memory_size, min_offset,
  3923. max_offset - min_offset, error_callback, data,
  3924. &debug_view))
  3925. goto fail;
  3926. debug_view_valid = 1;
  3927. }
  3928. else
  3929. {
  3930. memset (&split_debug_view[0], 0, sizeof split_debug_view);
  3931. for (i = 0; i < (int) DEBUG_MAX; ++i)
  3932. {
  3933. struct debug_section_info *dsec;
  3934. if (sections[i].size != 0)
  3935. dsec = &sections[i];
  3936. else if (zsections[i].size != 0)
  3937. dsec = &zsections[i];
  3938. else
  3939. continue;
  3940. if (!elf_get_view (state, descriptor, memory, memory_size,
  3941. dsec->offset, dsec->size, error_callback, data,
  3942. &split_debug_view[i]))
  3943. goto fail;
  3944. split_debug_view_valid[i] = 1;
  3945. if (sections[i].size != 0)
  3946. sections[i].data = ((const unsigned char *)
  3947. split_debug_view[i].view.data);
  3948. else
  3949. zsections[i].data = ((const unsigned char *)
  3950. split_debug_view[i].view.data);
  3951. }
  3952. }
  3953. /* We've read all we need from the executable. */
  3954. if (descriptor >= 0)
  3955. {
  3956. if (!backtrace_close (descriptor, error_callback, data))
  3957. goto fail;
  3958. descriptor = -1;
  3959. }
  3960. using_debug_view = 0;
  3961. if (debug_view_valid)
  3962. {
  3963. for (i = 0; i < (int) DEBUG_MAX; ++i)
  3964. {
  3965. if (sections[i].size == 0)
  3966. sections[i].data = NULL;
  3967. else
  3968. {
  3969. sections[i].data = ((const unsigned char *) debug_view.view.data
  3970. + (sections[i].offset - min_offset));
  3971. ++using_debug_view;
  3972. }
  3973. if (zsections[i].size == 0)
  3974. zsections[i].data = NULL;
  3975. else
  3976. zsections[i].data = ((const unsigned char *) debug_view.view.data
  3977. + (zsections[i].offset - min_offset));
  3978. }
  3979. }
  3980. /* Uncompress the old format (--compress-debug-sections=zlib-gnu). */
  3981. zdebug_table = NULL;
  3982. for (i = 0; i < (int) DEBUG_MAX; ++i)
  3983. {
  3984. if (sections[i].size == 0 && zsections[i].size > 0)
  3985. {
  3986. unsigned char *uncompressed_data;
  3987. size_t uncompressed_size;
  3988. if (zdebug_table == NULL)
  3989. {
  3990. zdebug_table = ((uint16_t *)
  3991. backtrace_alloc (state, ZDEBUG_TABLE_SIZE,
  3992. error_callback, data));
  3993. if (zdebug_table == NULL)
  3994. goto fail;
  3995. }
  3996. uncompressed_data = NULL;
  3997. uncompressed_size = 0;
  3998. if (!elf_uncompress_zdebug (state, zsections[i].data,
  3999. zsections[i].size, zdebug_table,
  4000. error_callback, data,
  4001. &uncompressed_data, &uncompressed_size))
  4002. goto fail;
  4003. sections[i].data = uncompressed_data;
  4004. sections[i].size = uncompressed_size;
  4005. sections[i].compressed = 0;
  4006. if (split_debug_view_valid[i])
  4007. {
  4008. elf_release_view (state, &split_debug_view[i],
  4009. error_callback, data);
  4010. split_debug_view_valid[i] = 0;
  4011. }
  4012. }
  4013. }
  4014. /* Uncompress the official ELF format
  4015. (--compress-debug-sections=zlib-gabi). */
  4016. for (i = 0; i < (int) DEBUG_MAX; ++i)
  4017. {
  4018. unsigned char *uncompressed_data;
  4019. size_t uncompressed_size;
  4020. if (sections[i].size == 0 || !sections[i].compressed)
  4021. continue;
  4022. if (zdebug_table == NULL)
  4023. {
  4024. zdebug_table = ((uint16_t *)
  4025. backtrace_alloc (state, ZDEBUG_TABLE_SIZE,
  4026. error_callback, data));
  4027. if (zdebug_table == NULL)
  4028. goto fail;
  4029. }
  4030. uncompressed_data = NULL;
  4031. uncompressed_size = 0;
  4032. if (!elf_uncompress_chdr (state, sections[i].data, sections[i].size,
  4033. zdebug_table, error_callback, data,
  4034. &uncompressed_data, &uncompressed_size))
  4035. goto fail;
  4036. sections[i].data = uncompressed_data;
  4037. sections[i].size = uncompressed_size;
  4038. sections[i].compressed = 0;
  4039. if (debug_view_valid)
  4040. --using_debug_view;
  4041. else if (split_debug_view_valid[i])
  4042. {
  4043. elf_release_view (state, &split_debug_view[i], error_callback, data);
  4044. split_debug_view_valid[i] = 0;
  4045. }
  4046. }
  4047. if (zdebug_table != NULL)
  4048. backtrace_free (state, zdebug_table, ZDEBUG_TABLE_SIZE,
  4049. error_callback, data);
  4050. if (debug_view_valid && using_debug_view == 0)
  4051. {
  4052. elf_release_view (state, &debug_view, error_callback, data);
  4053. debug_view_valid = 0;
  4054. }
  4055. for (i = 0; i < (int) DEBUG_MAX; ++i)
  4056. {
  4057. dwarf_sections.data[i] = sections[i].data;
  4058. dwarf_sections.size[i] = sections[i].size;
  4059. }
  4060. if (!backtrace_dwarf_add (state, base_address, &dwarf_sections,
  4061. ehdr.e_ident[EI_DATA] == ELFDATA2MSB,
  4062. fileline_altlink,
  4063. error_callback, data, fileline_fn,
  4064. fileline_entry))
  4065. goto fail;
  4066. *found_dwarf = 1;
  4067. return 1;
  4068. fail:
  4069. if (shdrs_view_valid)
  4070. elf_release_view (state, &shdrs_view, error_callback, data);
  4071. if (names_view_valid)
  4072. elf_release_view (state, &names_view, error_callback, data);
  4073. if (symtab_view_valid)
  4074. elf_release_view (state, &symtab_view, error_callback, data);
  4075. if (strtab_view_valid)
  4076. elf_release_view (state, &strtab_view, error_callback, data);
  4077. if (debuglink_view_valid)
  4078. elf_release_view (state, &debuglink_view, error_callback, data);
  4079. if (debugaltlink_view_valid)
  4080. elf_release_view (state, &debugaltlink_view, error_callback, data);
  4081. if (gnu_debugdata_view_valid)
  4082. elf_release_view (state, &gnu_debugdata_view, error_callback, data);
  4083. if (buildid_view_valid)
  4084. elf_release_view (state, &buildid_view, error_callback, data);
  4085. if (debug_view_valid)
  4086. elf_release_view (state, &debug_view, error_callback, data);
  4087. for (i = 0; i < (int) DEBUG_MAX; ++i)
  4088. {
  4089. if (split_debug_view_valid[i])
  4090. elf_release_view (state, &split_debug_view[i], error_callback, data);
  4091. }
  4092. if (opd)
  4093. elf_release_view (state, &opd->view, error_callback, data);
  4094. if (descriptor >= 0)
  4095. backtrace_close (descriptor, error_callback, data);
  4096. return 0;
  4097. }
  4098. /* Data passed to phdr_callback. */
  4099. struct phdr_data
  4100. {
  4101. struct backtrace_state *state;
  4102. backtrace_error_callback error_callback;
  4103. void *data;
  4104. fileline *fileline_fn;
  4105. int *found_sym;
  4106. int *found_dwarf;
  4107. const char *exe_filename;
  4108. int exe_descriptor;
  4109. };
  4110. /* Callback passed to dl_iterate_phdr. Load debug info from shared
  4111. libraries. */
  4112. static int
  4113. #ifdef __i386__
  4114. __attribute__ ((__force_align_arg_pointer__))
  4115. #endif
  4116. phdr_callback (struct dl_phdr_info *info, size_t size ATTRIBUTE_UNUSED,
  4117. void *pdata)
  4118. {
  4119. struct phdr_data *pd = (struct phdr_data *) pdata;
  4120. const char *filename;
  4121. int descriptor;
  4122. int does_not_exist;
  4123. fileline elf_fileline_fn;
  4124. int found_dwarf;
  4125. /* There is not much we can do if we don't have the module name,
  4126. unless executable is ET_DYN, where we expect the very first
  4127. phdr_callback to be for the PIE. */
  4128. if (info->dlpi_name == NULL || info->dlpi_name[0] == '\0')
  4129. {
  4130. if (pd->exe_descriptor == -1)
  4131. return 0;
  4132. filename = pd->exe_filename;
  4133. descriptor = pd->exe_descriptor;
  4134. pd->exe_descriptor = -1;
  4135. }
  4136. else
  4137. {
  4138. if (pd->exe_descriptor != -1)
  4139. {
  4140. backtrace_close (pd->exe_descriptor, pd->error_callback, pd->data);
  4141. pd->exe_descriptor = -1;
  4142. }
  4143. filename = info->dlpi_name;
  4144. descriptor = backtrace_open (info->dlpi_name, pd->error_callback,
  4145. pd->data, &does_not_exist);
  4146. if (descriptor < 0)
  4147. return 0;
  4148. }
  4149. if (elf_add (pd->state, filename, descriptor, NULL, 0, info->dlpi_addr,
  4150. pd->error_callback, pd->data, &elf_fileline_fn, pd->found_sym,
  4151. &found_dwarf, NULL, 0, 0, NULL, 0))
  4152. {
  4153. if (found_dwarf)
  4154. {
  4155. *pd->found_dwarf = 1;
  4156. *pd->fileline_fn = elf_fileline_fn;
  4157. }
  4158. }
  4159. return 0;
  4160. }
  4161. /* Initialize the backtrace data we need from an ELF executable. At
  4162. the ELF level, all we need to do is find the debug info
  4163. sections. */
  4164. int
  4165. backtrace_initialize (struct backtrace_state *state, const char *filename,
  4166. int descriptor, backtrace_error_callback error_callback,
  4167. void *data, fileline *fileline_fn)
  4168. {
  4169. int ret;
  4170. int found_sym;
  4171. int found_dwarf;
  4172. fileline elf_fileline_fn = elf_nodebug;
  4173. struct phdr_data pd;
  4174. ret = elf_add (state, filename, descriptor, NULL, 0, 0, error_callback, data,
  4175. &elf_fileline_fn, &found_sym, &found_dwarf, NULL, 1, 0, NULL,
  4176. 0);
  4177. if (!ret)
  4178. return 0;
  4179. pd.state = state;
  4180. pd.error_callback = error_callback;
  4181. pd.data = data;
  4182. pd.fileline_fn = &elf_fileline_fn;
  4183. pd.found_sym = &found_sym;
  4184. pd.found_dwarf = &found_dwarf;
  4185. pd.exe_filename = filename;
  4186. pd.exe_descriptor = ret < 0 ? descriptor : -1;
  4187. dl_iterate_phdr (phdr_callback, (void *) &pd);
  4188. if (!state->threaded)
  4189. {
  4190. if (found_sym)
  4191. state->syminfo_fn = elf_syminfo;
  4192. else if (state->syminfo_fn == NULL)
  4193. state->syminfo_fn = elf_nosyms;
  4194. }
  4195. else
  4196. {
  4197. if (found_sym)
  4198. backtrace_atomic_store_pointer (&state->syminfo_fn, elf_syminfo);
  4199. else
  4200. (void) __sync_bool_compare_and_swap (&state->syminfo_fn, NULL,
  4201. elf_nosyms);
  4202. }
  4203. if (!state->threaded)
  4204. *fileline_fn = state->fileline_fn;
  4205. else
  4206. *fileline_fn = backtrace_atomic_load_pointer (&state->fileline_fn);
  4207. if (*fileline_fn == NULL || *fileline_fn == elf_nodebug)
  4208. *fileline_fn = elf_fileline_fn;
  4209. return 1;
  4210. }