elf.c 127 KB

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