elf.c 188 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465
  1. /* elf.c -- Get debug data from an ELF file for backtraces.
  2. Copyright (C) 2012-2024 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. #undef ELFCOMPRESS_ZSTD
  151. /* Basic types. */
  152. typedef uint16_t b_elf_half; /* Elf_Half. */
  153. typedef uint32_t b_elf_word; /* Elf_Word. */
  154. typedef int32_t b_elf_sword; /* Elf_Sword. */
  155. #if BACKTRACE_ELF_SIZE == 32
  156. typedef uint32_t b_elf_addr; /* Elf_Addr. */
  157. typedef uint32_t b_elf_off; /* Elf_Off. */
  158. typedef uint32_t b_elf_wxword; /* 32-bit Elf_Word, 64-bit ELF_Xword. */
  159. #else
  160. typedef uint64_t b_elf_addr; /* Elf_Addr. */
  161. typedef uint64_t b_elf_off; /* Elf_Off. */
  162. typedef uint64_t b_elf_xword; /* Elf_Xword. */
  163. typedef int64_t b_elf_sxword; /* Elf_Sxword. */
  164. typedef uint64_t b_elf_wxword; /* 32-bit Elf_Word, 64-bit ELF_Xword. */
  165. #endif
  166. /* Data structures and associated constants. */
  167. #define EI_NIDENT 16
  168. typedef struct {
  169. unsigned char e_ident[EI_NIDENT]; /* ELF "magic number" */
  170. b_elf_half e_type; /* Identifies object file type */
  171. b_elf_half e_machine; /* Specifies required architecture */
  172. b_elf_word e_version; /* Identifies object file version */
  173. b_elf_addr e_entry; /* Entry point virtual address */
  174. b_elf_off e_phoff; /* Program header table file offset */
  175. b_elf_off e_shoff; /* Section header table file offset */
  176. b_elf_word e_flags; /* Processor-specific flags */
  177. b_elf_half e_ehsize; /* ELF header size in bytes */
  178. b_elf_half e_phentsize; /* Program header table entry size */
  179. b_elf_half e_phnum; /* Program header table entry count */
  180. b_elf_half e_shentsize; /* Section header table entry size */
  181. b_elf_half e_shnum; /* Section header table entry count */
  182. b_elf_half e_shstrndx; /* Section header string table index */
  183. } b_elf_ehdr; /* Elf_Ehdr. */
  184. #define EI_MAG0 0
  185. #define EI_MAG1 1
  186. #define EI_MAG2 2
  187. #define EI_MAG3 3
  188. #define EI_CLASS 4
  189. #define EI_DATA 5
  190. #define EI_VERSION 6
  191. #define ELFMAG0 0x7f
  192. #define ELFMAG1 'E'
  193. #define ELFMAG2 'L'
  194. #define ELFMAG3 'F'
  195. #define ELFCLASS32 1
  196. #define ELFCLASS64 2
  197. #define ELFDATA2LSB 1
  198. #define ELFDATA2MSB 2
  199. #define EV_CURRENT 1
  200. #define ET_DYN 3
  201. #define EM_PPC64 21
  202. #define EF_PPC64_ABI 3
  203. typedef struct {
  204. b_elf_word sh_name; /* Section name, index in string tbl */
  205. b_elf_word sh_type; /* Type of section */
  206. b_elf_wxword sh_flags; /* Miscellaneous section attributes */
  207. b_elf_addr sh_addr; /* Section virtual addr at execution */
  208. b_elf_off sh_offset; /* Section file offset */
  209. b_elf_wxword sh_size; /* Size of section in bytes */
  210. b_elf_word sh_link; /* Index of another section */
  211. b_elf_word sh_info; /* Additional section information */
  212. b_elf_wxword sh_addralign; /* Section alignment */
  213. b_elf_wxword sh_entsize; /* Entry size if section holds table */
  214. } b_elf_shdr; /* Elf_Shdr. */
  215. #define SHN_UNDEF 0x0000 /* Undefined section */
  216. #define SHN_LORESERVE 0xFF00 /* Begin range of reserved indices */
  217. #define SHN_XINDEX 0xFFFF /* Section index is held elsewhere */
  218. #define SHT_PROGBITS 1
  219. #define SHT_SYMTAB 2
  220. #define SHT_STRTAB 3
  221. #define SHT_DYNSYM 11
  222. #define SHF_COMPRESSED 0x800
  223. #if BACKTRACE_ELF_SIZE == 32
  224. typedef struct
  225. {
  226. b_elf_word st_name; /* Symbol name, index in string tbl */
  227. b_elf_addr st_value; /* Symbol value */
  228. b_elf_word st_size; /* Symbol size */
  229. unsigned char st_info; /* Symbol binding and type */
  230. unsigned char st_other; /* Visibility and other data */
  231. b_elf_half st_shndx; /* Symbol section index */
  232. } b_elf_sym; /* Elf_Sym. */
  233. #else /* BACKTRACE_ELF_SIZE != 32 */
  234. typedef struct
  235. {
  236. b_elf_word st_name; /* Symbol name, index in string tbl */
  237. unsigned char st_info; /* Symbol binding and type */
  238. unsigned char st_other; /* Visibility and other data */
  239. b_elf_half st_shndx; /* Symbol section index */
  240. b_elf_addr st_value; /* Symbol value */
  241. b_elf_xword st_size; /* Symbol size */
  242. } b_elf_sym; /* Elf_Sym. */
  243. #endif /* BACKTRACE_ELF_SIZE != 32 */
  244. #define STT_OBJECT 1
  245. #define STT_FUNC 2
  246. typedef struct
  247. {
  248. uint32_t namesz;
  249. uint32_t descsz;
  250. uint32_t type;
  251. char name[1];
  252. } b_elf_note;
  253. #define NT_GNU_BUILD_ID 3
  254. #if BACKTRACE_ELF_SIZE == 32
  255. typedef struct
  256. {
  257. b_elf_word ch_type; /* Compresstion algorithm */
  258. b_elf_word ch_size; /* Uncompressed size */
  259. b_elf_word ch_addralign; /* Alignment for uncompressed data */
  260. } b_elf_chdr; /* Elf_Chdr */
  261. #else /* BACKTRACE_ELF_SIZE != 32 */
  262. typedef struct
  263. {
  264. b_elf_word ch_type; /* Compression algorithm */
  265. b_elf_word ch_reserved; /* Reserved */
  266. b_elf_xword ch_size; /* Uncompressed size */
  267. b_elf_xword ch_addralign; /* Alignment for uncompressed data */
  268. } b_elf_chdr; /* Elf_Chdr */
  269. #endif /* BACKTRACE_ELF_SIZE != 32 */
  270. #define ELFCOMPRESS_ZLIB 1
  271. #define ELFCOMPRESS_ZSTD 2
  272. /* Names of sections, indexed by enum dwarf_section in internal.h. */
  273. static const char * const dwarf_section_names[DEBUG_MAX] =
  274. {
  275. ".debug_info",
  276. ".debug_line",
  277. ".debug_abbrev",
  278. ".debug_ranges",
  279. ".debug_str",
  280. ".debug_addr",
  281. ".debug_str_offsets",
  282. ".debug_line_str",
  283. ".debug_rnglists"
  284. };
  285. /* Information we gather for the sections we care about. */
  286. struct debug_section_info
  287. {
  288. /* Section file offset. */
  289. off_t offset;
  290. /* Section size. */
  291. size_t size;
  292. /* Section contents, after read from file. */
  293. const unsigned char *data;
  294. /* Whether the SHF_COMPRESSED flag is set for the section. */
  295. int compressed;
  296. };
  297. /* Information we keep for an ELF symbol. */
  298. struct elf_symbol
  299. {
  300. /* The name of the symbol. */
  301. const char *name;
  302. /* The address of the symbol. */
  303. uintptr_t address;
  304. /* The size of the symbol. */
  305. size_t size;
  306. };
  307. /* Information to pass to elf_syminfo. */
  308. struct elf_syminfo_data
  309. {
  310. /* Symbols for the next module. */
  311. struct elf_syminfo_data *next;
  312. /* The ELF symbols, sorted by address. */
  313. struct elf_symbol *symbols;
  314. /* The number of symbols. */
  315. size_t count;
  316. };
  317. /* A view that works for either a file or memory. */
  318. struct elf_view
  319. {
  320. struct backtrace_view view;
  321. int release; /* If non-zero, must call backtrace_release_view. */
  322. };
  323. /* Information about PowerPC64 ELFv1 .opd section. */
  324. struct elf_ppc64_opd_data
  325. {
  326. /* Address of the .opd section. */
  327. b_elf_addr addr;
  328. /* Section data. */
  329. const char *data;
  330. /* Size of the .opd section. */
  331. size_t size;
  332. /* Corresponding section view. */
  333. struct elf_view view;
  334. };
  335. /* Create a view of SIZE bytes from DESCRIPTOR/MEMORY at OFFSET. */
  336. static int
  337. elf_get_view (struct backtrace_state *state, int descriptor,
  338. const unsigned char *memory, size_t memory_size, off_t offset,
  339. uint64_t size, backtrace_error_callback error_callback,
  340. void *data, struct elf_view *view)
  341. {
  342. if (memory == NULL)
  343. {
  344. view->release = 1;
  345. return backtrace_get_view (state, descriptor, offset, size,
  346. error_callback, data, &view->view);
  347. }
  348. else
  349. {
  350. if ((uint64_t) offset + size > (uint64_t) memory_size)
  351. {
  352. error_callback (data, "out of range for in-memory file", 0);
  353. return 0;
  354. }
  355. view->view.data = (const void *) (memory + offset);
  356. view->view.base = NULL;
  357. view->view.len = size;
  358. view->release = 0;
  359. return 1;
  360. }
  361. }
  362. /* Release a view read by elf_get_view. */
  363. static void
  364. elf_release_view (struct backtrace_state *state, struct elf_view *view,
  365. backtrace_error_callback error_callback, void *data)
  366. {
  367. if (view->release)
  368. backtrace_release_view (state, &view->view, error_callback, data);
  369. }
  370. /* Compute the CRC-32 of BUF/LEN. This uses the CRC used for
  371. .gnu_debuglink files. */
  372. static uint32_t
  373. elf_crc32 (uint32_t crc, const unsigned char *buf, size_t len)
  374. {
  375. static const uint32_t crc32_table[256] =
  376. {
  377. 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419,
  378. 0x706af48f, 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4,
  379. 0xe0d5e91e, 0x97d2d988, 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07,
  380. 0x90bf1d91, 0x1db71064, 0x6ab020f2, 0xf3b97148, 0x84be41de,
  381. 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7, 0x136c9856,
  382. 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9,
  383. 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4,
  384. 0xa2677172, 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b,
  385. 0x35b5a8fa, 0x42b2986c, 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3,
  386. 0x45df5c75, 0xdcd60dcf, 0xabd13d59, 0x26d930ac, 0x51de003a,
  387. 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, 0xcfba9599,
  388. 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
  389. 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190,
  390. 0x01db7106, 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f,
  391. 0x9fbfe4a5, 0xe8b8d433, 0x7807c9a2, 0x0f00f934, 0x9609a88e,
  392. 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d, 0x91646c97, 0xe6635c01,
  393. 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, 0x6c0695ed,
  394. 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950,
  395. 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3,
  396. 0xfbd44c65, 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2,
  397. 0x4adfa541, 0x3dd895d7, 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a,
  398. 0x346ed9fc, 0xad678846, 0xda60b8d0, 0x44042d73, 0x33031de5,
  399. 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa, 0xbe0b1010,
  400. 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
  401. 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17,
  402. 0x2eb40d81, 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6,
  403. 0x03b6e20c, 0x74b1d29a, 0xead54739, 0x9dd277af, 0x04db2615,
  404. 0x73dc1683, 0xe3630b12, 0x94643b84, 0x0d6d6a3e, 0x7a6a5aa8,
  405. 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1, 0xf00f9344,
  406. 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb,
  407. 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a,
  408. 0x67dd4acc, 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5,
  409. 0xd6d6a3e8, 0xa1d1937e, 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1,
  410. 0xa6bc5767, 0x3fb506dd, 0x48b2364b, 0xd80d2bda, 0xaf0a1b4c,
  411. 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, 0x316e8eef,
  412. 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
  413. 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe,
  414. 0xb2bd0b28, 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31,
  415. 0x2cd99e8b, 0x5bdeae1d, 0x9b64c2b0, 0xec63f226, 0x756aa39c,
  416. 0x026d930a, 0x9c0906a9, 0xeb0e363f, 0x72076785, 0x05005713,
  417. 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, 0x92d28e9b,
  418. 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242,
  419. 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1,
  420. 0x18b74777, 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c,
  421. 0x8f659eff, 0xf862ae69, 0x616bffd3, 0x166ccf45, 0xa00ae278,
  422. 0xd70dd2ee, 0x4e048354, 0x3903b3c2, 0xa7672661, 0xd06016f7,
  423. 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc, 0x40df0b66,
  424. 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
  425. 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605,
  426. 0xcdd70693, 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8,
  427. 0x5d681b02, 0x2a6f2b94, 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b,
  428. 0x2d02ef8d
  429. };
  430. const unsigned char *end;
  431. crc = ~crc;
  432. for (end = buf + len; buf < end; ++ buf)
  433. crc = crc32_table[(crc ^ *buf) & 0xff] ^ (crc >> 8);
  434. return ~crc;
  435. }
  436. /* Return the CRC-32 of the entire file open at DESCRIPTOR. */
  437. static uint32_t
  438. elf_crc32_file (struct backtrace_state *state, int descriptor,
  439. backtrace_error_callback error_callback, void *data)
  440. {
  441. struct stat st;
  442. struct backtrace_view file_view;
  443. uint32_t ret;
  444. if (fstat (descriptor, &st) < 0)
  445. {
  446. error_callback (data, "fstat", errno);
  447. return 0;
  448. }
  449. if (!backtrace_get_view (state, descriptor, 0, st.st_size, error_callback,
  450. data, &file_view))
  451. return 0;
  452. ret = elf_crc32 (0, (const unsigned char *) file_view.data, st.st_size);
  453. backtrace_release_view (state, &file_view, error_callback, data);
  454. return ret;
  455. }
  456. /* A dummy callback function used when we can't find a symbol
  457. table. */
  458. static void
  459. elf_nosyms (struct backtrace_state *state ATTRIBUTE_UNUSED,
  460. uintptr_t addr ATTRIBUTE_UNUSED,
  461. backtrace_syminfo_callback callback ATTRIBUTE_UNUSED,
  462. backtrace_error_callback error_callback, void *data)
  463. {
  464. error_callback (data, "no symbol table in ELF executable", -1);
  465. }
  466. /* A callback function used when we can't find any debug info. */
  467. static int
  468. elf_nodebug (struct backtrace_state *state, uintptr_t pc,
  469. backtrace_full_callback callback,
  470. backtrace_error_callback error_callback, void *data)
  471. {
  472. if (state->syminfo_fn != NULL && state->syminfo_fn != elf_nosyms)
  473. {
  474. struct backtrace_call_full bdata;
  475. /* Fetch symbol information so that we can least get the
  476. function name. */
  477. bdata.full_callback = callback;
  478. bdata.full_error_callback = error_callback;
  479. bdata.full_data = data;
  480. bdata.ret = 0;
  481. state->syminfo_fn (state, pc, backtrace_syminfo_to_full_callback,
  482. backtrace_syminfo_to_full_error_callback, &bdata);
  483. return bdata.ret;
  484. }
  485. error_callback (data, "no debug info in ELF executable (make sure to compile with -g)", -1);
  486. return 0;
  487. }
  488. /* Compare struct elf_symbol for qsort. */
  489. static int
  490. elf_symbol_compare (const void *v1, const void *v2)
  491. {
  492. const struct elf_symbol *e1 = (const struct elf_symbol *) v1;
  493. const struct elf_symbol *e2 = (const struct elf_symbol *) v2;
  494. if (e1->address < e2->address)
  495. return -1;
  496. else if (e1->address > e2->address)
  497. return 1;
  498. else
  499. return 0;
  500. }
  501. /* Compare an ADDR against an elf_symbol for bsearch. We allocate one
  502. extra entry in the array so that this can look safely at the next
  503. entry. */
  504. static int
  505. elf_symbol_search (const void *vkey, const void *ventry)
  506. {
  507. const uintptr_t *key = (const uintptr_t *) vkey;
  508. const struct elf_symbol *entry = (const struct elf_symbol *) ventry;
  509. uintptr_t addr;
  510. addr = *key;
  511. if (addr < entry->address)
  512. return -1;
  513. else if (addr >= entry->address + entry->size)
  514. return 1;
  515. else
  516. return 0;
  517. }
  518. /* Initialize the symbol table info for elf_syminfo. */
  519. static int
  520. elf_initialize_syminfo (struct backtrace_state *state,
  521. struct libbacktrace_base_address base_address,
  522. const unsigned char *symtab_data, size_t symtab_size,
  523. const unsigned char *strtab, size_t strtab_size,
  524. backtrace_error_callback error_callback,
  525. void *data, struct elf_syminfo_data *sdata,
  526. struct elf_ppc64_opd_data *opd)
  527. {
  528. size_t sym_count;
  529. const b_elf_sym *sym;
  530. size_t elf_symbol_count;
  531. size_t elf_symbol_size;
  532. struct elf_symbol *elf_symbols;
  533. size_t i;
  534. unsigned int j;
  535. sym_count = symtab_size / sizeof (b_elf_sym);
  536. /* We only care about function symbols. Count them. */
  537. sym = (const b_elf_sym *) symtab_data;
  538. elf_symbol_count = 0;
  539. for (i = 0; i < sym_count; ++i, ++sym)
  540. {
  541. int info;
  542. info = sym->st_info & 0xf;
  543. if ((info == STT_FUNC || info == STT_OBJECT)
  544. && sym->st_shndx != SHN_UNDEF)
  545. ++elf_symbol_count;
  546. }
  547. elf_symbol_size = elf_symbol_count * sizeof (struct elf_symbol);
  548. elf_symbols = ((struct elf_symbol *)
  549. backtrace_alloc (state, elf_symbol_size, error_callback,
  550. data));
  551. if (elf_symbols == NULL)
  552. return 0;
  553. sym = (const b_elf_sym *) symtab_data;
  554. j = 0;
  555. for (i = 0; i < sym_count; ++i, ++sym)
  556. {
  557. int info;
  558. info = sym->st_info & 0xf;
  559. if (info != STT_FUNC && info != STT_OBJECT)
  560. continue;
  561. if (sym->st_shndx == SHN_UNDEF)
  562. continue;
  563. if (sym->st_name >= strtab_size)
  564. {
  565. error_callback (data, "symbol string index out of range", 0);
  566. backtrace_free (state, elf_symbols, elf_symbol_size, error_callback,
  567. data);
  568. return 0;
  569. }
  570. elf_symbols[j].name = (const char *) strtab + sym->st_name;
  571. /* Special case PowerPC64 ELFv1 symbols in .opd section, if the symbol
  572. is a function descriptor, read the actual code address from the
  573. descriptor. */
  574. if (opd
  575. && sym->st_value >= opd->addr
  576. && sym->st_value < opd->addr + opd->size)
  577. elf_symbols[j].address
  578. = *(const b_elf_addr *) (opd->data + (sym->st_value - opd->addr));
  579. else
  580. elf_symbols[j].address = sym->st_value;
  581. elf_symbols[j].address =
  582. libbacktrace_add_base (elf_symbols[j].address, base_address);
  583. elf_symbols[j].size = sym->st_size;
  584. ++j;
  585. }
  586. backtrace_qsort (elf_symbols, elf_symbol_count, sizeof (struct elf_symbol),
  587. elf_symbol_compare);
  588. sdata->next = NULL;
  589. sdata->symbols = elf_symbols;
  590. sdata->count = elf_symbol_count;
  591. return 1;
  592. }
  593. /* Add EDATA to the list in STATE. */
  594. static void
  595. elf_add_syminfo_data (struct backtrace_state *state,
  596. struct elf_syminfo_data *edata)
  597. {
  598. if (!state->threaded)
  599. {
  600. struct elf_syminfo_data **pp;
  601. for (pp = (struct elf_syminfo_data **) (void *) &state->syminfo_data;
  602. *pp != NULL;
  603. pp = &(*pp)->next)
  604. ;
  605. *pp = edata;
  606. }
  607. else
  608. {
  609. while (1)
  610. {
  611. struct elf_syminfo_data **pp;
  612. pp = (struct elf_syminfo_data **) (void *) &state->syminfo_data;
  613. while (1)
  614. {
  615. struct elf_syminfo_data *p;
  616. p = backtrace_atomic_load_pointer (pp);
  617. if (p == NULL)
  618. break;
  619. pp = &p->next;
  620. }
  621. if (__sync_bool_compare_and_swap (pp, NULL, edata))
  622. break;
  623. }
  624. }
  625. }
  626. /* Return the symbol name and value for an ADDR. */
  627. static void
  628. elf_syminfo (struct backtrace_state *state, uintptr_t addr,
  629. backtrace_syminfo_callback callback,
  630. backtrace_error_callback error_callback ATTRIBUTE_UNUSED,
  631. void *data)
  632. {
  633. struct elf_syminfo_data *edata;
  634. struct elf_symbol *sym = NULL;
  635. if (!state->threaded)
  636. {
  637. for (edata = (struct elf_syminfo_data *) state->syminfo_data;
  638. edata != NULL;
  639. edata = edata->next)
  640. {
  641. sym = ((struct elf_symbol *)
  642. bsearch (&addr, edata->symbols, edata->count,
  643. sizeof (struct elf_symbol), elf_symbol_search));
  644. if (sym != NULL)
  645. break;
  646. }
  647. }
  648. else
  649. {
  650. struct elf_syminfo_data **pp;
  651. pp = (struct elf_syminfo_data **) (void *) &state->syminfo_data;
  652. while (1)
  653. {
  654. edata = backtrace_atomic_load_pointer (pp);
  655. if (edata == NULL)
  656. break;
  657. sym = ((struct elf_symbol *)
  658. bsearch (&addr, edata->symbols, edata->count,
  659. sizeof (struct elf_symbol), elf_symbol_search));
  660. if (sym != NULL)
  661. break;
  662. pp = &edata->next;
  663. }
  664. }
  665. if (sym == NULL)
  666. callback (data, addr, NULL, 0, 0);
  667. else
  668. callback (data, addr, sym->name, sym->address, sym->size);
  669. }
  670. /* Return whether FILENAME is a symlink. */
  671. static int
  672. elf_is_symlink (const char *filename)
  673. {
  674. struct stat st;
  675. if (lstat (filename, &st) < 0)
  676. return 0;
  677. return S_ISLNK (st.st_mode);
  678. }
  679. /* Return the results of reading the symlink FILENAME in a buffer
  680. allocated by backtrace_alloc. Return the length of the buffer in
  681. *LEN. */
  682. static char *
  683. elf_readlink (struct backtrace_state *state, const char *filename,
  684. backtrace_error_callback error_callback, void *data,
  685. size_t *plen)
  686. {
  687. size_t len;
  688. char *buf;
  689. len = 128;
  690. while (1)
  691. {
  692. ssize_t rl;
  693. buf = backtrace_alloc (state, len, error_callback, data);
  694. if (buf == NULL)
  695. return NULL;
  696. rl = readlink (filename, buf, len);
  697. if (rl < 0)
  698. {
  699. backtrace_free (state, buf, len, error_callback, data);
  700. return NULL;
  701. }
  702. if ((size_t) rl < len - 1)
  703. {
  704. buf[rl] = '\0';
  705. *plen = len;
  706. return buf;
  707. }
  708. backtrace_free (state, buf, len, error_callback, data);
  709. len *= 2;
  710. }
  711. }
  712. #define SYSTEM_BUILD_ID_DIR "/usr/lib/debug/.build-id/"
  713. /* Open a separate debug info file, using the build ID to find it.
  714. Returns an open file descriptor, or -1.
  715. The GDB manual says that the only place gdb looks for a debug file
  716. when the build ID is known is in /usr/lib/debug/.build-id. */
  717. static int
  718. elf_open_debugfile_by_buildid (struct backtrace_state *state,
  719. const char *buildid_data, size_t buildid_size,
  720. backtrace_error_callback error_callback,
  721. void *data)
  722. {
  723. const char * const prefix = SYSTEM_BUILD_ID_DIR;
  724. const size_t prefix_len = strlen (prefix);
  725. const char * const suffix = ".debug";
  726. const size_t suffix_len = strlen (suffix);
  727. size_t len;
  728. char *bd_filename;
  729. char *t;
  730. size_t i;
  731. int ret;
  732. int does_not_exist;
  733. len = prefix_len + buildid_size * 2 + suffix_len + 2;
  734. bd_filename = backtrace_alloc (state, len, error_callback, data);
  735. if (bd_filename == NULL)
  736. return -1;
  737. t = bd_filename;
  738. memcpy (t, prefix, prefix_len);
  739. t += prefix_len;
  740. for (i = 0; i < buildid_size; i++)
  741. {
  742. unsigned char b;
  743. unsigned char nib;
  744. b = (unsigned char) buildid_data[i];
  745. nib = (b & 0xf0) >> 4;
  746. *t++ = nib < 10 ? '0' + nib : 'a' + nib - 10;
  747. nib = b & 0x0f;
  748. *t++ = nib < 10 ? '0' + nib : 'a' + nib - 10;
  749. if (i == 0)
  750. *t++ = '/';
  751. }
  752. memcpy (t, suffix, suffix_len);
  753. t[suffix_len] = '\0';
  754. ret = backtrace_open (bd_filename, error_callback, data, &does_not_exist);
  755. backtrace_free (state, bd_filename, len, error_callback, data);
  756. /* gdb checks that the debuginfo file has the same build ID note.
  757. That seems kind of pointless to me--why would it have the right
  758. name but not the right build ID?--so skipping the check. */
  759. return ret;
  760. }
  761. /* Try to open a file whose name is PREFIX (length PREFIX_LEN)
  762. concatenated with PREFIX2 (length PREFIX2_LEN) concatenated with
  763. DEBUGLINK_NAME. Returns an open file descriptor, or -1. */
  764. static int
  765. elf_try_debugfile (struct backtrace_state *state, const char *prefix,
  766. size_t prefix_len, const char *prefix2, size_t prefix2_len,
  767. const char *debuglink_name,
  768. backtrace_error_callback error_callback, void *data)
  769. {
  770. size_t debuglink_len;
  771. size_t try_len;
  772. char *try;
  773. int does_not_exist;
  774. int ret;
  775. debuglink_len = strlen (debuglink_name);
  776. try_len = prefix_len + prefix2_len + debuglink_len + 1;
  777. try = backtrace_alloc (state, try_len, error_callback, data);
  778. if (try == NULL)
  779. return -1;
  780. memcpy (try, prefix, prefix_len);
  781. memcpy (try + prefix_len, prefix2, prefix2_len);
  782. memcpy (try + prefix_len + prefix2_len, debuglink_name, debuglink_len);
  783. try[prefix_len + prefix2_len + debuglink_len] = '\0';
  784. ret = backtrace_open (try, error_callback, data, &does_not_exist);
  785. backtrace_free (state, try, try_len, error_callback, data);
  786. return ret;
  787. }
  788. /* Find a separate debug info file, using the debuglink section data
  789. to find it. Returns an open file descriptor, or -1. */
  790. static int
  791. elf_find_debugfile_by_debuglink (struct backtrace_state *state,
  792. const char *filename,
  793. const char *debuglink_name,
  794. backtrace_error_callback error_callback,
  795. void *data)
  796. {
  797. int ret;
  798. char *alc;
  799. size_t alc_len;
  800. const char *slash;
  801. int ddescriptor;
  802. const char *prefix;
  803. size_t prefix_len;
  804. /* Resolve symlinks in FILENAME. Since FILENAME is fairly likely to
  805. be /proc/self/exe, symlinks are common. We don't try to resolve
  806. the whole path name, just the base name. */
  807. ret = -1;
  808. alc = NULL;
  809. alc_len = 0;
  810. while (elf_is_symlink (filename))
  811. {
  812. char *new_buf;
  813. size_t new_len;
  814. new_buf = elf_readlink (state, filename, error_callback, data, &new_len);
  815. if (new_buf == NULL)
  816. break;
  817. if (new_buf[0] == '/')
  818. filename = new_buf;
  819. else
  820. {
  821. slash = strrchr (filename, '/');
  822. if (slash == NULL)
  823. filename = new_buf;
  824. else
  825. {
  826. size_t clen;
  827. char *c;
  828. slash++;
  829. clen = slash - filename + strlen (new_buf) + 1;
  830. c = backtrace_alloc (state, clen, error_callback, data);
  831. if (c == NULL)
  832. goto done;
  833. memcpy (c, filename, slash - filename);
  834. memcpy (c + (slash - filename), new_buf, strlen (new_buf));
  835. c[slash - filename + strlen (new_buf)] = '\0';
  836. backtrace_free (state, new_buf, new_len, error_callback, data);
  837. filename = c;
  838. new_buf = c;
  839. new_len = clen;
  840. }
  841. }
  842. if (alc != NULL)
  843. backtrace_free (state, alc, alc_len, error_callback, data);
  844. alc = new_buf;
  845. alc_len = new_len;
  846. }
  847. /* Look for DEBUGLINK_NAME in the same directory as FILENAME. */
  848. slash = strrchr (filename, '/');
  849. if (slash == NULL)
  850. {
  851. prefix = "";
  852. prefix_len = 0;
  853. }
  854. else
  855. {
  856. slash++;
  857. prefix = filename;
  858. prefix_len = slash - filename;
  859. }
  860. ddescriptor = elf_try_debugfile (state, prefix, prefix_len, "", 0,
  861. debuglink_name, error_callback, data);
  862. if (ddescriptor >= 0)
  863. {
  864. ret = ddescriptor;
  865. goto done;
  866. }
  867. /* Look for DEBUGLINK_NAME in a .debug subdirectory of FILENAME. */
  868. ddescriptor = elf_try_debugfile (state, prefix, prefix_len, ".debug/",
  869. strlen (".debug/"), debuglink_name,
  870. error_callback, data);
  871. if (ddescriptor >= 0)
  872. {
  873. ret = ddescriptor;
  874. goto done;
  875. }
  876. /* Look for DEBUGLINK_NAME in /usr/lib/debug. */
  877. ddescriptor = elf_try_debugfile (state, "/usr/lib/debug/",
  878. strlen ("/usr/lib/debug/"), prefix,
  879. prefix_len, debuglink_name,
  880. error_callback, data);
  881. if (ddescriptor >= 0)
  882. ret = ddescriptor;
  883. done:
  884. if (alc != NULL && alc_len > 0)
  885. backtrace_free (state, alc, alc_len, error_callback, data);
  886. return ret;
  887. }
  888. /* Open a separate debug info file, using the debuglink section data
  889. to find it. Returns an open file descriptor, or -1. */
  890. static int
  891. elf_open_debugfile_by_debuglink (struct backtrace_state *state,
  892. const char *filename,
  893. const char *debuglink_name,
  894. uint32_t debuglink_crc,
  895. backtrace_error_callback error_callback,
  896. void *data)
  897. {
  898. int ddescriptor;
  899. ddescriptor = elf_find_debugfile_by_debuglink (state, filename,
  900. debuglink_name,
  901. error_callback, data);
  902. if (ddescriptor < 0)
  903. return -1;
  904. if (debuglink_crc != 0)
  905. {
  906. uint32_t got_crc;
  907. got_crc = elf_crc32_file (state, ddescriptor, error_callback, data);
  908. if (got_crc != debuglink_crc)
  909. {
  910. backtrace_close (ddescriptor, error_callback, data);
  911. return -1;
  912. }
  913. }
  914. return ddescriptor;
  915. }
  916. /* A function useful for setting a breakpoint for an inflation failure
  917. when this code is compiled with -g. */
  918. static void
  919. elf_uncompress_failed(void)
  920. {
  921. }
  922. /* *PVAL is the current value being read from the stream, and *PBITS
  923. is the number of valid bits. Ensure that *PVAL holds at least 15
  924. bits by reading additional bits from *PPIN, up to PINEND, as
  925. needed. Updates *PPIN, *PVAL and *PBITS. Returns 1 on success, 0
  926. on error. */
  927. static int
  928. elf_fetch_bits (const unsigned char **ppin, const unsigned char *pinend,
  929. uint64_t *pval, unsigned int *pbits)
  930. {
  931. unsigned int bits;
  932. const unsigned char *pin;
  933. uint64_t val;
  934. uint32_t next;
  935. bits = *pbits;
  936. if (bits >= 15)
  937. return 1;
  938. pin = *ppin;
  939. val = *pval;
  940. if (unlikely (pinend - pin < 4))
  941. {
  942. elf_uncompress_failed ();
  943. return 0;
  944. }
  945. #if defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) \
  946. && defined(__ORDER_BIG_ENDIAN__) \
  947. && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ \
  948. || __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
  949. /* We've ensured that PIN is aligned. */
  950. next = *(const uint32_t *)pin;
  951. #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
  952. next = __builtin_bswap32 (next);
  953. #endif
  954. #else
  955. next = pin[0] | (pin[1] << 8) | (pin[2] << 16) | (pin[3] << 24);
  956. #endif
  957. val |= (uint64_t)next << bits;
  958. bits += 32;
  959. pin += 4;
  960. /* We will need the next four bytes soon. */
  961. __builtin_prefetch (pin, 0, 0);
  962. *ppin = pin;
  963. *pval = val;
  964. *pbits = bits;
  965. return 1;
  966. }
  967. /* This is like elf_fetch_bits, but it fetchs the bits backward, and ensures at
  968. least 16 bits. This is for zstd. */
  969. static int
  970. elf_fetch_bits_backward (const unsigned char **ppin,
  971. const unsigned char *pinend,
  972. uint64_t *pval, unsigned int *pbits)
  973. {
  974. unsigned int bits;
  975. const unsigned char *pin;
  976. uint64_t val;
  977. uint32_t next;
  978. bits = *pbits;
  979. if (bits >= 16)
  980. return 1;
  981. pin = *ppin;
  982. val = *pval;
  983. if (unlikely (pin <= pinend))
  984. return 1;
  985. pin -= 4;
  986. #if defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) \
  987. && defined(__ORDER_BIG_ENDIAN__) \
  988. && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ \
  989. || __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
  990. /* We've ensured that PIN is aligned. */
  991. next = *(const uint32_t *)pin;
  992. #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
  993. next = __builtin_bswap32 (next);
  994. #endif
  995. #else
  996. next = pin[0] | (pin[1] << 8) | (pin[2] << 16) | (pin[3] << 24);
  997. #endif
  998. val <<= 32;
  999. val |= next;
  1000. bits += 32;
  1001. if (unlikely (pin < pinend))
  1002. {
  1003. val >>= (pinend - pin) * 8;
  1004. bits -= (pinend - pin) * 8;
  1005. }
  1006. *ppin = pin;
  1007. *pval = val;
  1008. *pbits = bits;
  1009. return 1;
  1010. }
  1011. /* Initialize backward fetching when the bitstream starts with a 1 bit in the
  1012. last byte in memory (which is the first one that we read). This is used by
  1013. zstd decompression. Returns 1 on success, 0 on error. */
  1014. static int
  1015. elf_fetch_backward_init (const unsigned char **ppin,
  1016. const unsigned char *pinend,
  1017. uint64_t *pval, unsigned int *pbits)
  1018. {
  1019. const unsigned char *pin;
  1020. unsigned int stream_start;
  1021. uint64_t val;
  1022. unsigned int bits;
  1023. pin = *ppin;
  1024. stream_start = (unsigned int)*pin;
  1025. if (unlikely (stream_start == 0))
  1026. {
  1027. elf_uncompress_failed ();
  1028. return 0;
  1029. }
  1030. val = 0;
  1031. bits = 0;
  1032. /* Align to a 32-bit boundary. */
  1033. while ((((uintptr_t)pin) & 3) != 0)
  1034. {
  1035. val <<= 8;
  1036. val |= (uint64_t)*pin;
  1037. bits += 8;
  1038. --pin;
  1039. }
  1040. val <<= 8;
  1041. val |= (uint64_t)*pin;
  1042. bits += 8;
  1043. *ppin = pin;
  1044. *pval = val;
  1045. *pbits = bits;
  1046. if (!elf_fetch_bits_backward (ppin, pinend, pval, pbits))
  1047. return 0;
  1048. *pbits -= __builtin_clz (stream_start) - (sizeof (unsigned int) - 1) * 8 + 1;
  1049. if (!elf_fetch_bits_backward (ppin, pinend, pval, pbits))
  1050. return 0;
  1051. return 1;
  1052. }
  1053. /* Huffman code tables, like the rest of the zlib format, are defined
  1054. by RFC 1951. We store a Huffman code table as a series of tables
  1055. stored sequentially in memory. Each entry in a table is 16 bits.
  1056. The first, main, table has 256 entries. It is followed by a set of
  1057. secondary tables of length 2 to 128 entries. The maximum length of
  1058. a code sequence in the deflate format is 15 bits, so that is all we
  1059. need. Each secondary table has an index, which is the offset of
  1060. the table in the overall memory storage.
  1061. The deflate format says that all codes of a given bit length are
  1062. lexicographically consecutive. Perhaps we could have 130 values
  1063. that require a 15-bit code, perhaps requiring three secondary
  1064. tables of size 128. I don't know if this is actually possible, but
  1065. it suggests that the maximum size required for secondary tables is
  1066. 3 * 128 + 3 * 64 ... == 768. The zlib enough program reports 660
  1067. as the maximum. We permit 768, since in addition to the 256 for
  1068. the primary table, with two bytes per entry, and with the two
  1069. tables we need, that gives us a page.
  1070. A single table entry needs to store a value or (for the main table
  1071. only) the index and size of a secondary table. Values range from 0
  1072. to 285, inclusive. Secondary table indexes, per above, range from
  1073. 0 to 510. For a value we need to store the number of bits we need
  1074. to determine that value (one value may appear multiple times in the
  1075. table), which is 1 to 8. For a secondary table we need to store
  1076. the number of bits used to index into the table, which is 1 to 7.
  1077. And of course we need 1 bit to decide whether we have a value or a
  1078. secondary table index. So each entry needs 9 bits for value/table
  1079. index, 3 bits for size, 1 bit what it is. For simplicity we use 16
  1080. bits per entry. */
  1081. /* Number of entries we allocate to for one code table. We get a page
  1082. for the two code tables we need. */
  1083. #define ZLIB_HUFFMAN_TABLE_SIZE (1024)
  1084. /* Bit masks and shifts for the values in the table. */
  1085. #define ZLIB_HUFFMAN_VALUE_MASK 0x01ff
  1086. #define ZLIB_HUFFMAN_BITS_SHIFT 9
  1087. #define ZLIB_HUFFMAN_BITS_MASK 0x7
  1088. #define ZLIB_HUFFMAN_SECONDARY_SHIFT 12
  1089. /* For working memory while inflating we need two code tables, we need
  1090. an array of code lengths (max value 15, so we use unsigned char),
  1091. and an array of unsigned shorts used while building a table. The
  1092. latter two arrays must be large enough to hold the maximum number
  1093. of code lengths, which RFC 1951 defines as 286 + 30. */
  1094. #define ZLIB_TABLE_SIZE \
  1095. (2 * ZLIB_HUFFMAN_TABLE_SIZE * sizeof (uint16_t) \
  1096. + (286 + 30) * sizeof (uint16_t) \
  1097. + (286 + 30) * sizeof (unsigned char))
  1098. #define ZLIB_TABLE_CODELEN_OFFSET \
  1099. (2 * ZLIB_HUFFMAN_TABLE_SIZE * sizeof (uint16_t) \
  1100. + (286 + 30) * sizeof (uint16_t))
  1101. #define ZLIB_TABLE_WORK_OFFSET \
  1102. (2 * ZLIB_HUFFMAN_TABLE_SIZE * sizeof (uint16_t))
  1103. #ifdef BACKTRACE_GENERATE_FIXED_HUFFMAN_TABLE
  1104. /* Used by the main function that generates the fixed table to learn
  1105. the table size. */
  1106. static size_t final_next_secondary;
  1107. #endif
  1108. /* Build a Huffman code table from an array of lengths in CODES of
  1109. length CODES_LEN. The table is stored into *TABLE. ZDEBUG_TABLE
  1110. is the same as for elf_zlib_inflate, used to find some work space.
  1111. Returns 1 on success, 0 on error. */
  1112. static int
  1113. elf_zlib_inflate_table (unsigned char *codes, size_t codes_len,
  1114. uint16_t *zdebug_table, uint16_t *table)
  1115. {
  1116. uint16_t count[16];
  1117. uint16_t start[16];
  1118. uint16_t prev[16];
  1119. uint16_t firstcode[7];
  1120. uint16_t *next;
  1121. size_t i;
  1122. size_t j;
  1123. unsigned int code;
  1124. size_t next_secondary;
  1125. /* Count the number of code of each length. Set NEXT[val] to be the
  1126. next value after VAL with the same bit length. */
  1127. next = (uint16_t *) (((unsigned char *) zdebug_table)
  1128. + ZLIB_TABLE_WORK_OFFSET);
  1129. memset (&count[0], 0, 16 * sizeof (uint16_t));
  1130. for (i = 0; i < codes_len; ++i)
  1131. {
  1132. if (unlikely (codes[i] >= 16))
  1133. {
  1134. elf_uncompress_failed ();
  1135. return 0;
  1136. }
  1137. if (count[codes[i]] == 0)
  1138. {
  1139. start[codes[i]] = i;
  1140. prev[codes[i]] = i;
  1141. }
  1142. else
  1143. {
  1144. next[prev[codes[i]]] = i;
  1145. prev[codes[i]] = i;
  1146. }
  1147. ++count[codes[i]];
  1148. }
  1149. /* For each length, fill in the table for the codes of that
  1150. length. */
  1151. memset (table, 0, ZLIB_HUFFMAN_TABLE_SIZE * sizeof (uint16_t));
  1152. /* Handle the values that do not require a secondary table. */
  1153. code = 0;
  1154. for (j = 1; j <= 8; ++j)
  1155. {
  1156. unsigned int jcnt;
  1157. unsigned int val;
  1158. jcnt = count[j];
  1159. if (jcnt == 0)
  1160. continue;
  1161. if (unlikely (jcnt > (1U << j)))
  1162. {
  1163. elf_uncompress_failed ();
  1164. return 0;
  1165. }
  1166. /* There are JCNT values that have this length, the values
  1167. starting from START[j] continuing through NEXT[VAL]. Those
  1168. values are assigned consecutive values starting at CODE. */
  1169. val = start[j];
  1170. for (i = 0; i < jcnt; ++i)
  1171. {
  1172. uint16_t tval;
  1173. size_t ind;
  1174. unsigned int incr;
  1175. /* In the compressed bit stream, the value VAL is encoded as
  1176. J bits with the value C. */
  1177. if (unlikely ((val & ~ZLIB_HUFFMAN_VALUE_MASK) != 0))
  1178. {
  1179. elf_uncompress_failed ();
  1180. return 0;
  1181. }
  1182. tval = val | ((j - 1) << ZLIB_HUFFMAN_BITS_SHIFT);
  1183. /* The table lookup uses 8 bits. If J is less than 8, we
  1184. don't know what the other bits will be. We need to fill
  1185. in all possibilities in the table. Since the Huffman
  1186. code is unambiguous, those entries can't be used for any
  1187. other code. */
  1188. for (ind = code; ind < 0x100; ind += 1 << j)
  1189. {
  1190. if (unlikely (table[ind] != 0))
  1191. {
  1192. elf_uncompress_failed ();
  1193. return 0;
  1194. }
  1195. table[ind] = tval;
  1196. }
  1197. /* Advance to the next value with this length. */
  1198. if (i + 1 < jcnt)
  1199. val = next[val];
  1200. /* The Huffman codes are stored in the bitstream with the
  1201. most significant bit first, as is required to make them
  1202. unambiguous. The effect is that when we read them from
  1203. the bitstream we see the bit sequence in reverse order:
  1204. the most significant bit of the Huffman code is the least
  1205. significant bit of the value we read from the bitstream.
  1206. That means that to make our table lookups work, we need
  1207. to reverse the bits of CODE. Since reversing bits is
  1208. tedious and in general requires using a table, we instead
  1209. increment CODE in reverse order. That is, if the number
  1210. of bits we are currently using, here named J, is 3, we
  1211. count as 000, 100, 010, 110, 001, 101, 011, 111, which is
  1212. to say the numbers from 0 to 7 but with the bits
  1213. reversed. Going to more bits, aka incrementing J,
  1214. effectively just adds more zero bits as the beginning,
  1215. and as such does not change the numeric value of CODE.
  1216. To increment CODE of length J in reverse order, find the
  1217. most significant zero bit and set it to one while
  1218. clearing all higher bits. In other words, add 1 modulo
  1219. 2^J, only reversed. */
  1220. incr = 1U << (j - 1);
  1221. while ((code & incr) != 0)
  1222. incr >>= 1;
  1223. if (incr == 0)
  1224. code = 0;
  1225. else
  1226. {
  1227. code &= incr - 1;
  1228. code += incr;
  1229. }
  1230. }
  1231. }
  1232. /* Handle the values that require a secondary table. */
  1233. /* Set FIRSTCODE, the number at which the codes start, for each
  1234. length. */
  1235. for (j = 9; j < 16; j++)
  1236. {
  1237. unsigned int jcnt;
  1238. unsigned int k;
  1239. jcnt = count[j];
  1240. if (jcnt == 0)
  1241. continue;
  1242. /* There are JCNT values that have this length, the values
  1243. starting from START[j]. Those values are assigned
  1244. consecutive values starting at CODE. */
  1245. firstcode[j - 9] = code;
  1246. /* Reverse add JCNT to CODE modulo 2^J. */
  1247. for (k = 0; k < j; ++k)
  1248. {
  1249. if ((jcnt & (1U << k)) != 0)
  1250. {
  1251. unsigned int m;
  1252. unsigned int bit;
  1253. bit = 1U << (j - k - 1);
  1254. for (m = 0; m < j - k; ++m, bit >>= 1)
  1255. {
  1256. if ((code & bit) == 0)
  1257. {
  1258. code += bit;
  1259. break;
  1260. }
  1261. code &= ~bit;
  1262. }
  1263. jcnt &= ~(1U << k);
  1264. }
  1265. }
  1266. if (unlikely (jcnt != 0))
  1267. {
  1268. elf_uncompress_failed ();
  1269. return 0;
  1270. }
  1271. }
  1272. /* For J from 9 to 15, inclusive, we store COUNT[J] consecutive
  1273. values starting at START[J] with consecutive codes starting at
  1274. FIRSTCODE[J - 9]. In the primary table we need to point to the
  1275. secondary table, and the secondary table will be indexed by J - 9
  1276. bits. We count down from 15 so that we install the larger
  1277. secondary tables first, as the smaller ones may be embedded in
  1278. the larger ones. */
  1279. next_secondary = 0; /* Index of next secondary table (after primary). */
  1280. for (j = 15; j >= 9; j--)
  1281. {
  1282. unsigned int jcnt;
  1283. unsigned int val;
  1284. size_t primary; /* Current primary index. */
  1285. size_t secondary; /* Offset to current secondary table. */
  1286. size_t secondary_bits; /* Bit size of current secondary table. */
  1287. jcnt = count[j];
  1288. if (jcnt == 0)
  1289. continue;
  1290. val = start[j];
  1291. code = firstcode[j - 9];
  1292. primary = 0x100;
  1293. secondary = 0;
  1294. secondary_bits = 0;
  1295. for (i = 0; i < jcnt; ++i)
  1296. {
  1297. uint16_t tval;
  1298. size_t ind;
  1299. unsigned int incr;
  1300. if ((code & 0xff) != primary)
  1301. {
  1302. uint16_t tprimary;
  1303. /* Fill in a new primary table entry. */
  1304. primary = code & 0xff;
  1305. tprimary = table[primary];
  1306. if (tprimary == 0)
  1307. {
  1308. /* Start a new secondary table. */
  1309. if (unlikely ((next_secondary & ZLIB_HUFFMAN_VALUE_MASK)
  1310. != next_secondary))
  1311. {
  1312. elf_uncompress_failed ();
  1313. return 0;
  1314. }
  1315. secondary = next_secondary;
  1316. secondary_bits = j - 8;
  1317. next_secondary += 1 << secondary_bits;
  1318. table[primary] = (secondary
  1319. + ((j - 8) << ZLIB_HUFFMAN_BITS_SHIFT)
  1320. + (1U << ZLIB_HUFFMAN_SECONDARY_SHIFT));
  1321. }
  1322. else
  1323. {
  1324. /* There is an existing entry. It had better be a
  1325. secondary table with enough bits. */
  1326. if (unlikely ((tprimary
  1327. & (1U << ZLIB_HUFFMAN_SECONDARY_SHIFT))
  1328. == 0))
  1329. {
  1330. elf_uncompress_failed ();
  1331. return 0;
  1332. }
  1333. secondary = tprimary & ZLIB_HUFFMAN_VALUE_MASK;
  1334. secondary_bits = ((tprimary >> ZLIB_HUFFMAN_BITS_SHIFT)
  1335. & ZLIB_HUFFMAN_BITS_MASK);
  1336. if (unlikely (secondary_bits < j - 8))
  1337. {
  1338. elf_uncompress_failed ();
  1339. return 0;
  1340. }
  1341. }
  1342. }
  1343. /* Fill in secondary table entries. */
  1344. tval = val | ((j - 8) << ZLIB_HUFFMAN_BITS_SHIFT);
  1345. for (ind = code >> 8;
  1346. ind < (1U << secondary_bits);
  1347. ind += 1U << (j - 8))
  1348. {
  1349. if (unlikely (table[secondary + 0x100 + ind] != 0))
  1350. {
  1351. elf_uncompress_failed ();
  1352. return 0;
  1353. }
  1354. table[secondary + 0x100 + ind] = tval;
  1355. }
  1356. if (i + 1 < jcnt)
  1357. val = next[val];
  1358. incr = 1U << (j - 1);
  1359. while ((code & incr) != 0)
  1360. incr >>= 1;
  1361. if (incr == 0)
  1362. code = 0;
  1363. else
  1364. {
  1365. code &= incr - 1;
  1366. code += incr;
  1367. }
  1368. }
  1369. }
  1370. #ifdef BACKTRACE_GENERATE_FIXED_HUFFMAN_TABLE
  1371. final_next_secondary = next_secondary;
  1372. #endif
  1373. return 1;
  1374. }
  1375. #ifdef BACKTRACE_GENERATE_FIXED_HUFFMAN_TABLE
  1376. /* Used to generate the fixed Huffman table for block type 1. */
  1377. #include <stdio.h>
  1378. static uint16_t table[ZLIB_TABLE_SIZE];
  1379. static unsigned char codes[288];
  1380. int
  1381. main ()
  1382. {
  1383. size_t i;
  1384. for (i = 0; i <= 143; ++i)
  1385. codes[i] = 8;
  1386. for (i = 144; i <= 255; ++i)
  1387. codes[i] = 9;
  1388. for (i = 256; i <= 279; ++i)
  1389. codes[i] = 7;
  1390. for (i = 280; i <= 287; ++i)
  1391. codes[i] = 8;
  1392. if (!elf_zlib_inflate_table (&codes[0], 288, &table[0], &table[0]))
  1393. {
  1394. fprintf (stderr, "elf_zlib_inflate_table failed\n");
  1395. exit (EXIT_FAILURE);
  1396. }
  1397. printf ("static const uint16_t elf_zlib_default_table[%#zx] =\n",
  1398. final_next_secondary + 0x100);
  1399. printf ("{\n");
  1400. for (i = 0; i < final_next_secondary + 0x100; i += 8)
  1401. {
  1402. size_t j;
  1403. printf (" ");
  1404. for (j = i; j < final_next_secondary + 0x100 && j < i + 8; ++j)
  1405. printf (" %#x,", table[j]);
  1406. printf ("\n");
  1407. }
  1408. printf ("};\n");
  1409. printf ("\n");
  1410. for (i = 0; i < 32; ++i)
  1411. codes[i] = 5;
  1412. if (!elf_zlib_inflate_table (&codes[0], 32, &table[0], &table[0]))
  1413. {
  1414. fprintf (stderr, "elf_zlib_inflate_table failed\n");
  1415. exit (EXIT_FAILURE);
  1416. }
  1417. printf ("static const uint16_t elf_zlib_default_dist_table[%#zx] =\n",
  1418. final_next_secondary + 0x100);
  1419. printf ("{\n");
  1420. for (i = 0; i < final_next_secondary + 0x100; i += 8)
  1421. {
  1422. size_t j;
  1423. printf (" ");
  1424. for (j = i; j < final_next_secondary + 0x100 && j < i + 8; ++j)
  1425. printf (" %#x,", table[j]);
  1426. printf ("\n");
  1427. }
  1428. printf ("};\n");
  1429. return 0;
  1430. }
  1431. #endif
  1432. /* The fixed tables generated by the #ifdef'ed out main function
  1433. above. */
  1434. static const uint16_t elf_zlib_default_table[0x170] =
  1435. {
  1436. 0xd00, 0xe50, 0xe10, 0xf18, 0xd10, 0xe70, 0xe30, 0x1230,
  1437. 0xd08, 0xe60, 0xe20, 0x1210, 0xe00, 0xe80, 0xe40, 0x1250,
  1438. 0xd04, 0xe58, 0xe18, 0x1200, 0xd14, 0xe78, 0xe38, 0x1240,
  1439. 0xd0c, 0xe68, 0xe28, 0x1220, 0xe08, 0xe88, 0xe48, 0x1260,
  1440. 0xd02, 0xe54, 0xe14, 0xf1c, 0xd12, 0xe74, 0xe34, 0x1238,
  1441. 0xd0a, 0xe64, 0xe24, 0x1218, 0xe04, 0xe84, 0xe44, 0x1258,
  1442. 0xd06, 0xe5c, 0xe1c, 0x1208, 0xd16, 0xe7c, 0xe3c, 0x1248,
  1443. 0xd0e, 0xe6c, 0xe2c, 0x1228, 0xe0c, 0xe8c, 0xe4c, 0x1268,
  1444. 0xd01, 0xe52, 0xe12, 0xf1a, 0xd11, 0xe72, 0xe32, 0x1234,
  1445. 0xd09, 0xe62, 0xe22, 0x1214, 0xe02, 0xe82, 0xe42, 0x1254,
  1446. 0xd05, 0xe5a, 0xe1a, 0x1204, 0xd15, 0xe7a, 0xe3a, 0x1244,
  1447. 0xd0d, 0xe6a, 0xe2a, 0x1224, 0xe0a, 0xe8a, 0xe4a, 0x1264,
  1448. 0xd03, 0xe56, 0xe16, 0xf1e, 0xd13, 0xe76, 0xe36, 0x123c,
  1449. 0xd0b, 0xe66, 0xe26, 0x121c, 0xe06, 0xe86, 0xe46, 0x125c,
  1450. 0xd07, 0xe5e, 0xe1e, 0x120c, 0xd17, 0xe7e, 0xe3e, 0x124c,
  1451. 0xd0f, 0xe6e, 0xe2e, 0x122c, 0xe0e, 0xe8e, 0xe4e, 0x126c,
  1452. 0xd00, 0xe51, 0xe11, 0xf19, 0xd10, 0xe71, 0xe31, 0x1232,
  1453. 0xd08, 0xe61, 0xe21, 0x1212, 0xe01, 0xe81, 0xe41, 0x1252,
  1454. 0xd04, 0xe59, 0xe19, 0x1202, 0xd14, 0xe79, 0xe39, 0x1242,
  1455. 0xd0c, 0xe69, 0xe29, 0x1222, 0xe09, 0xe89, 0xe49, 0x1262,
  1456. 0xd02, 0xe55, 0xe15, 0xf1d, 0xd12, 0xe75, 0xe35, 0x123a,
  1457. 0xd0a, 0xe65, 0xe25, 0x121a, 0xe05, 0xe85, 0xe45, 0x125a,
  1458. 0xd06, 0xe5d, 0xe1d, 0x120a, 0xd16, 0xe7d, 0xe3d, 0x124a,
  1459. 0xd0e, 0xe6d, 0xe2d, 0x122a, 0xe0d, 0xe8d, 0xe4d, 0x126a,
  1460. 0xd01, 0xe53, 0xe13, 0xf1b, 0xd11, 0xe73, 0xe33, 0x1236,
  1461. 0xd09, 0xe63, 0xe23, 0x1216, 0xe03, 0xe83, 0xe43, 0x1256,
  1462. 0xd05, 0xe5b, 0xe1b, 0x1206, 0xd15, 0xe7b, 0xe3b, 0x1246,
  1463. 0xd0d, 0xe6b, 0xe2b, 0x1226, 0xe0b, 0xe8b, 0xe4b, 0x1266,
  1464. 0xd03, 0xe57, 0xe17, 0xf1f, 0xd13, 0xe77, 0xe37, 0x123e,
  1465. 0xd0b, 0xe67, 0xe27, 0x121e, 0xe07, 0xe87, 0xe47, 0x125e,
  1466. 0xd07, 0xe5f, 0xe1f, 0x120e, 0xd17, 0xe7f, 0xe3f, 0x124e,
  1467. 0xd0f, 0xe6f, 0xe2f, 0x122e, 0xe0f, 0xe8f, 0xe4f, 0x126e,
  1468. 0x290, 0x291, 0x292, 0x293, 0x294, 0x295, 0x296, 0x297,
  1469. 0x298, 0x299, 0x29a, 0x29b, 0x29c, 0x29d, 0x29e, 0x29f,
  1470. 0x2a0, 0x2a1, 0x2a2, 0x2a3, 0x2a4, 0x2a5, 0x2a6, 0x2a7,
  1471. 0x2a8, 0x2a9, 0x2aa, 0x2ab, 0x2ac, 0x2ad, 0x2ae, 0x2af,
  1472. 0x2b0, 0x2b1, 0x2b2, 0x2b3, 0x2b4, 0x2b5, 0x2b6, 0x2b7,
  1473. 0x2b8, 0x2b9, 0x2ba, 0x2bb, 0x2bc, 0x2bd, 0x2be, 0x2bf,
  1474. 0x2c0, 0x2c1, 0x2c2, 0x2c3, 0x2c4, 0x2c5, 0x2c6, 0x2c7,
  1475. 0x2c8, 0x2c9, 0x2ca, 0x2cb, 0x2cc, 0x2cd, 0x2ce, 0x2cf,
  1476. 0x2d0, 0x2d1, 0x2d2, 0x2d3, 0x2d4, 0x2d5, 0x2d6, 0x2d7,
  1477. 0x2d8, 0x2d9, 0x2da, 0x2db, 0x2dc, 0x2dd, 0x2de, 0x2df,
  1478. 0x2e0, 0x2e1, 0x2e2, 0x2e3, 0x2e4, 0x2e5, 0x2e6, 0x2e7,
  1479. 0x2e8, 0x2e9, 0x2ea, 0x2eb, 0x2ec, 0x2ed, 0x2ee, 0x2ef,
  1480. 0x2f0, 0x2f1, 0x2f2, 0x2f3, 0x2f4, 0x2f5, 0x2f6, 0x2f7,
  1481. 0x2f8, 0x2f9, 0x2fa, 0x2fb, 0x2fc, 0x2fd, 0x2fe, 0x2ff,
  1482. };
  1483. static const uint16_t elf_zlib_default_dist_table[0x100] =
  1484. {
  1485. 0x800, 0x810, 0x808, 0x818, 0x804, 0x814, 0x80c, 0x81c,
  1486. 0x802, 0x812, 0x80a, 0x81a, 0x806, 0x816, 0x80e, 0x81e,
  1487. 0x801, 0x811, 0x809, 0x819, 0x805, 0x815, 0x80d, 0x81d,
  1488. 0x803, 0x813, 0x80b, 0x81b, 0x807, 0x817, 0x80f, 0x81f,
  1489. 0x800, 0x810, 0x808, 0x818, 0x804, 0x814, 0x80c, 0x81c,
  1490. 0x802, 0x812, 0x80a, 0x81a, 0x806, 0x816, 0x80e, 0x81e,
  1491. 0x801, 0x811, 0x809, 0x819, 0x805, 0x815, 0x80d, 0x81d,
  1492. 0x803, 0x813, 0x80b, 0x81b, 0x807, 0x817, 0x80f, 0x81f,
  1493. 0x800, 0x810, 0x808, 0x818, 0x804, 0x814, 0x80c, 0x81c,
  1494. 0x802, 0x812, 0x80a, 0x81a, 0x806, 0x816, 0x80e, 0x81e,
  1495. 0x801, 0x811, 0x809, 0x819, 0x805, 0x815, 0x80d, 0x81d,
  1496. 0x803, 0x813, 0x80b, 0x81b, 0x807, 0x817, 0x80f, 0x81f,
  1497. 0x800, 0x810, 0x808, 0x818, 0x804, 0x814, 0x80c, 0x81c,
  1498. 0x802, 0x812, 0x80a, 0x81a, 0x806, 0x816, 0x80e, 0x81e,
  1499. 0x801, 0x811, 0x809, 0x819, 0x805, 0x815, 0x80d, 0x81d,
  1500. 0x803, 0x813, 0x80b, 0x81b, 0x807, 0x817, 0x80f, 0x81f,
  1501. 0x800, 0x810, 0x808, 0x818, 0x804, 0x814, 0x80c, 0x81c,
  1502. 0x802, 0x812, 0x80a, 0x81a, 0x806, 0x816, 0x80e, 0x81e,
  1503. 0x801, 0x811, 0x809, 0x819, 0x805, 0x815, 0x80d, 0x81d,
  1504. 0x803, 0x813, 0x80b, 0x81b, 0x807, 0x817, 0x80f, 0x81f,
  1505. 0x800, 0x810, 0x808, 0x818, 0x804, 0x814, 0x80c, 0x81c,
  1506. 0x802, 0x812, 0x80a, 0x81a, 0x806, 0x816, 0x80e, 0x81e,
  1507. 0x801, 0x811, 0x809, 0x819, 0x805, 0x815, 0x80d, 0x81d,
  1508. 0x803, 0x813, 0x80b, 0x81b, 0x807, 0x817, 0x80f, 0x81f,
  1509. 0x800, 0x810, 0x808, 0x818, 0x804, 0x814, 0x80c, 0x81c,
  1510. 0x802, 0x812, 0x80a, 0x81a, 0x806, 0x816, 0x80e, 0x81e,
  1511. 0x801, 0x811, 0x809, 0x819, 0x805, 0x815, 0x80d, 0x81d,
  1512. 0x803, 0x813, 0x80b, 0x81b, 0x807, 0x817, 0x80f, 0x81f,
  1513. 0x800, 0x810, 0x808, 0x818, 0x804, 0x814, 0x80c, 0x81c,
  1514. 0x802, 0x812, 0x80a, 0x81a, 0x806, 0x816, 0x80e, 0x81e,
  1515. 0x801, 0x811, 0x809, 0x819, 0x805, 0x815, 0x80d, 0x81d,
  1516. 0x803, 0x813, 0x80b, 0x81b, 0x807, 0x817, 0x80f, 0x81f,
  1517. };
  1518. /* Inflate a zlib stream from PIN/SIN to POUT/SOUT. Return 1 on
  1519. success, 0 on some error parsing the stream. */
  1520. static int
  1521. elf_zlib_inflate (const unsigned char *pin, size_t sin, uint16_t *zdebug_table,
  1522. unsigned char *pout, size_t sout)
  1523. {
  1524. unsigned char *porigout;
  1525. const unsigned char *pinend;
  1526. unsigned char *poutend;
  1527. /* We can apparently see multiple zlib streams concatenated
  1528. together, so keep going as long as there is something to read.
  1529. The last 4 bytes are the checksum. */
  1530. porigout = pout;
  1531. pinend = pin + sin;
  1532. poutend = pout + sout;
  1533. while ((pinend - pin) > 4)
  1534. {
  1535. uint64_t val;
  1536. unsigned int bits;
  1537. int last;
  1538. /* Read the two byte zlib header. */
  1539. if (unlikely ((pin[0] & 0xf) != 8)) /* 8 is zlib encoding. */
  1540. {
  1541. /* Unknown compression method. */
  1542. elf_uncompress_failed ();
  1543. return 0;
  1544. }
  1545. if (unlikely ((pin[0] >> 4) > 7))
  1546. {
  1547. /* Window size too large. Other than this check, we don't
  1548. care about the window size. */
  1549. elf_uncompress_failed ();
  1550. return 0;
  1551. }
  1552. if (unlikely ((pin[1] & 0x20) != 0))
  1553. {
  1554. /* Stream expects a predefined dictionary, but we have no
  1555. dictionary. */
  1556. elf_uncompress_failed ();
  1557. return 0;
  1558. }
  1559. val = (pin[0] << 8) | pin[1];
  1560. if (unlikely (val % 31 != 0))
  1561. {
  1562. /* Header check failure. */
  1563. elf_uncompress_failed ();
  1564. return 0;
  1565. }
  1566. pin += 2;
  1567. /* Align PIN to a 32-bit boundary. */
  1568. val = 0;
  1569. bits = 0;
  1570. while ((((uintptr_t) pin) & 3) != 0)
  1571. {
  1572. val |= (uint64_t)*pin << bits;
  1573. bits += 8;
  1574. ++pin;
  1575. }
  1576. /* Read blocks until one is marked last. */
  1577. last = 0;
  1578. while (!last)
  1579. {
  1580. unsigned int type;
  1581. const uint16_t *tlit;
  1582. const uint16_t *tdist;
  1583. if (!elf_fetch_bits (&pin, pinend, &val, &bits))
  1584. return 0;
  1585. last = val & 1;
  1586. type = (val >> 1) & 3;
  1587. val >>= 3;
  1588. bits -= 3;
  1589. if (unlikely (type == 3))
  1590. {
  1591. /* Invalid block type. */
  1592. elf_uncompress_failed ();
  1593. return 0;
  1594. }
  1595. if (type == 0)
  1596. {
  1597. uint16_t len;
  1598. uint16_t lenc;
  1599. /* An uncompressed block. */
  1600. /* If we've read ahead more than a byte, back up. */
  1601. while (bits >= 8)
  1602. {
  1603. --pin;
  1604. bits -= 8;
  1605. }
  1606. val = 0;
  1607. bits = 0;
  1608. if (unlikely ((pinend - pin) < 4))
  1609. {
  1610. /* Missing length. */
  1611. elf_uncompress_failed ();
  1612. return 0;
  1613. }
  1614. len = pin[0] | (pin[1] << 8);
  1615. lenc = pin[2] | (pin[3] << 8);
  1616. pin += 4;
  1617. lenc = ~lenc;
  1618. if (unlikely (len != lenc))
  1619. {
  1620. /* Corrupt data. */
  1621. elf_uncompress_failed ();
  1622. return 0;
  1623. }
  1624. if (unlikely (len > (unsigned int) (pinend - pin)
  1625. || len > (unsigned int) (poutend - pout)))
  1626. {
  1627. /* Not enough space in buffers. */
  1628. elf_uncompress_failed ();
  1629. return 0;
  1630. }
  1631. memcpy (pout, pin, len);
  1632. pout += len;
  1633. pin += len;
  1634. /* Align PIN. */
  1635. while ((((uintptr_t) pin) & 3) != 0)
  1636. {
  1637. val |= (uint64_t)*pin << bits;
  1638. bits += 8;
  1639. ++pin;
  1640. }
  1641. /* Go around to read the next block. */
  1642. continue;
  1643. }
  1644. if (type == 1)
  1645. {
  1646. tlit = elf_zlib_default_table;
  1647. tdist = elf_zlib_default_dist_table;
  1648. }
  1649. else
  1650. {
  1651. unsigned int nlit;
  1652. unsigned int ndist;
  1653. unsigned int nclen;
  1654. unsigned char codebits[19];
  1655. unsigned char *plenbase;
  1656. unsigned char *plen;
  1657. unsigned char *plenend;
  1658. /* Read a Huffman encoding table. The various magic
  1659. numbers here are from RFC 1951. */
  1660. if (!elf_fetch_bits (&pin, pinend, &val, &bits))
  1661. return 0;
  1662. nlit = (val & 0x1f) + 257;
  1663. val >>= 5;
  1664. ndist = (val & 0x1f) + 1;
  1665. val >>= 5;
  1666. nclen = (val & 0xf) + 4;
  1667. val >>= 4;
  1668. bits -= 14;
  1669. if (unlikely (nlit > 286 || ndist > 30))
  1670. {
  1671. /* Values out of range. */
  1672. elf_uncompress_failed ();
  1673. return 0;
  1674. }
  1675. /* Read and build the table used to compress the
  1676. literal, length, and distance codes. */
  1677. memset(&codebits[0], 0, 19);
  1678. /* There are always at least 4 elements in the
  1679. table. */
  1680. if (!elf_fetch_bits (&pin, pinend, &val, &bits))
  1681. return 0;
  1682. codebits[16] = val & 7;
  1683. codebits[17] = (val >> 3) & 7;
  1684. codebits[18] = (val >> 6) & 7;
  1685. codebits[0] = (val >> 9) & 7;
  1686. val >>= 12;
  1687. bits -= 12;
  1688. if (nclen == 4)
  1689. goto codebitsdone;
  1690. codebits[8] = val & 7;
  1691. val >>= 3;
  1692. bits -= 3;
  1693. if (nclen == 5)
  1694. goto codebitsdone;
  1695. if (!elf_fetch_bits (&pin, pinend, &val, &bits))
  1696. return 0;
  1697. codebits[7] = val & 7;
  1698. val >>= 3;
  1699. bits -= 3;
  1700. if (nclen == 6)
  1701. goto codebitsdone;
  1702. codebits[9] = val & 7;
  1703. val >>= 3;
  1704. bits -= 3;
  1705. if (nclen == 7)
  1706. goto codebitsdone;
  1707. codebits[6] = val & 7;
  1708. val >>= 3;
  1709. bits -= 3;
  1710. if (nclen == 8)
  1711. goto codebitsdone;
  1712. codebits[10] = val & 7;
  1713. val >>= 3;
  1714. bits -= 3;
  1715. if (nclen == 9)
  1716. goto codebitsdone;
  1717. codebits[5] = val & 7;
  1718. val >>= 3;
  1719. bits -= 3;
  1720. if (nclen == 10)
  1721. goto codebitsdone;
  1722. if (!elf_fetch_bits (&pin, pinend, &val, &bits))
  1723. return 0;
  1724. codebits[11] = val & 7;
  1725. val >>= 3;
  1726. bits -= 3;
  1727. if (nclen == 11)
  1728. goto codebitsdone;
  1729. codebits[4] = val & 7;
  1730. val >>= 3;
  1731. bits -= 3;
  1732. if (nclen == 12)
  1733. goto codebitsdone;
  1734. codebits[12] = val & 7;
  1735. val >>= 3;
  1736. bits -= 3;
  1737. if (nclen == 13)
  1738. goto codebitsdone;
  1739. codebits[3] = val & 7;
  1740. val >>= 3;
  1741. bits -= 3;
  1742. if (nclen == 14)
  1743. goto codebitsdone;
  1744. codebits[13] = val & 7;
  1745. val >>= 3;
  1746. bits -= 3;
  1747. if (nclen == 15)
  1748. goto codebitsdone;
  1749. if (!elf_fetch_bits (&pin, pinend, &val, &bits))
  1750. return 0;
  1751. codebits[2] = val & 7;
  1752. val >>= 3;
  1753. bits -= 3;
  1754. if (nclen == 16)
  1755. goto codebitsdone;
  1756. codebits[14] = val & 7;
  1757. val >>= 3;
  1758. bits -= 3;
  1759. if (nclen == 17)
  1760. goto codebitsdone;
  1761. codebits[1] = val & 7;
  1762. val >>= 3;
  1763. bits -= 3;
  1764. if (nclen == 18)
  1765. goto codebitsdone;
  1766. codebits[15] = val & 7;
  1767. val >>= 3;
  1768. bits -= 3;
  1769. codebitsdone:
  1770. if (!elf_zlib_inflate_table (codebits, 19, zdebug_table,
  1771. zdebug_table))
  1772. return 0;
  1773. /* Read the compressed bit lengths of the literal,
  1774. length, and distance codes. We have allocated space
  1775. at the end of zdebug_table to hold them. */
  1776. plenbase = (((unsigned char *) zdebug_table)
  1777. + ZLIB_TABLE_CODELEN_OFFSET);
  1778. plen = plenbase;
  1779. plenend = plen + nlit + ndist;
  1780. while (plen < plenend)
  1781. {
  1782. uint16_t t;
  1783. unsigned int b;
  1784. uint16_t v;
  1785. if (!elf_fetch_bits (&pin, pinend, &val, &bits))
  1786. return 0;
  1787. t = zdebug_table[val & 0xff];
  1788. /* The compression here uses bit lengths up to 7, so
  1789. a secondary table is never necessary. */
  1790. if (unlikely ((t & (1U << ZLIB_HUFFMAN_SECONDARY_SHIFT))
  1791. != 0))
  1792. {
  1793. elf_uncompress_failed ();
  1794. return 0;
  1795. }
  1796. b = (t >> ZLIB_HUFFMAN_BITS_SHIFT) & ZLIB_HUFFMAN_BITS_MASK;
  1797. val >>= b + 1;
  1798. bits -= b + 1;
  1799. v = t & ZLIB_HUFFMAN_VALUE_MASK;
  1800. if (v < 16)
  1801. *plen++ = v;
  1802. else if (v == 16)
  1803. {
  1804. unsigned int c;
  1805. unsigned int prev;
  1806. /* Copy previous entry 3 to 6 times. */
  1807. if (unlikely (plen == plenbase))
  1808. {
  1809. elf_uncompress_failed ();
  1810. return 0;
  1811. }
  1812. /* We used up to 7 bits since the last
  1813. elf_fetch_bits, so we have at least 8 bits
  1814. available here. */
  1815. c = 3 + (val & 0x3);
  1816. val >>= 2;
  1817. bits -= 2;
  1818. if (unlikely ((unsigned int) (plenend - plen) < c))
  1819. {
  1820. elf_uncompress_failed ();
  1821. return 0;
  1822. }
  1823. prev = plen[-1];
  1824. switch (c)
  1825. {
  1826. case 6:
  1827. *plen++ = prev;
  1828. ATTRIBUTE_FALLTHROUGH;
  1829. case 5:
  1830. *plen++ = prev;
  1831. ATTRIBUTE_FALLTHROUGH;
  1832. case 4:
  1833. *plen++ = prev;
  1834. }
  1835. *plen++ = prev;
  1836. *plen++ = prev;
  1837. *plen++ = prev;
  1838. }
  1839. else if (v == 17)
  1840. {
  1841. unsigned int c;
  1842. /* Store zero 3 to 10 times. */
  1843. /* We used up to 7 bits since the last
  1844. elf_fetch_bits, so we have at least 8 bits
  1845. available here. */
  1846. c = 3 + (val & 0x7);
  1847. val >>= 3;
  1848. bits -= 3;
  1849. if (unlikely ((unsigned int) (plenend - plen) < c))
  1850. {
  1851. elf_uncompress_failed ();
  1852. return 0;
  1853. }
  1854. switch (c)
  1855. {
  1856. case 10:
  1857. *plen++ = 0;
  1858. ATTRIBUTE_FALLTHROUGH;
  1859. case 9:
  1860. *plen++ = 0;
  1861. ATTRIBUTE_FALLTHROUGH;
  1862. case 8:
  1863. *plen++ = 0;
  1864. ATTRIBUTE_FALLTHROUGH;
  1865. case 7:
  1866. *plen++ = 0;
  1867. ATTRIBUTE_FALLTHROUGH;
  1868. case 6:
  1869. *plen++ = 0;
  1870. ATTRIBUTE_FALLTHROUGH;
  1871. case 5:
  1872. *plen++ = 0;
  1873. ATTRIBUTE_FALLTHROUGH;
  1874. case 4:
  1875. *plen++ = 0;
  1876. }
  1877. *plen++ = 0;
  1878. *plen++ = 0;
  1879. *plen++ = 0;
  1880. }
  1881. else if (v == 18)
  1882. {
  1883. unsigned int c;
  1884. /* Store zero 11 to 138 times. */
  1885. /* We used up to 7 bits since the last
  1886. elf_fetch_bits, so we have at least 8 bits
  1887. available here. */
  1888. c = 11 + (val & 0x7f);
  1889. val >>= 7;
  1890. bits -= 7;
  1891. if (unlikely ((unsigned int) (plenend - plen) < c))
  1892. {
  1893. elf_uncompress_failed ();
  1894. return 0;
  1895. }
  1896. memset (plen, 0, c);
  1897. plen += c;
  1898. }
  1899. else
  1900. {
  1901. elf_uncompress_failed ();
  1902. return 0;
  1903. }
  1904. }
  1905. /* Make sure that the stop code can appear. */
  1906. plen = plenbase;
  1907. if (unlikely (plen[256] == 0))
  1908. {
  1909. elf_uncompress_failed ();
  1910. return 0;
  1911. }
  1912. /* Build the decompression tables. */
  1913. if (!elf_zlib_inflate_table (plen, nlit, zdebug_table,
  1914. zdebug_table))
  1915. return 0;
  1916. if (!elf_zlib_inflate_table (plen + nlit, ndist, zdebug_table,
  1917. (zdebug_table
  1918. + ZLIB_HUFFMAN_TABLE_SIZE)))
  1919. return 0;
  1920. tlit = zdebug_table;
  1921. tdist = zdebug_table + ZLIB_HUFFMAN_TABLE_SIZE;
  1922. }
  1923. /* Inflate values until the end of the block. This is the
  1924. main loop of the inflation code. */
  1925. while (1)
  1926. {
  1927. uint16_t t;
  1928. unsigned int b;
  1929. uint16_t v;
  1930. unsigned int lit;
  1931. if (!elf_fetch_bits (&pin, pinend, &val, &bits))
  1932. return 0;
  1933. t = tlit[val & 0xff];
  1934. b = (t >> ZLIB_HUFFMAN_BITS_SHIFT) & ZLIB_HUFFMAN_BITS_MASK;
  1935. v = t & ZLIB_HUFFMAN_VALUE_MASK;
  1936. if ((t & (1U << ZLIB_HUFFMAN_SECONDARY_SHIFT)) == 0)
  1937. {
  1938. lit = v;
  1939. val >>= b + 1;
  1940. bits -= b + 1;
  1941. }
  1942. else
  1943. {
  1944. t = tlit[v + 0x100 + ((val >> 8) & ((1U << b) - 1))];
  1945. b = (t >> ZLIB_HUFFMAN_BITS_SHIFT) & ZLIB_HUFFMAN_BITS_MASK;
  1946. lit = t & ZLIB_HUFFMAN_VALUE_MASK;
  1947. val >>= b + 8;
  1948. bits -= b + 8;
  1949. }
  1950. if (lit < 256)
  1951. {
  1952. if (unlikely (pout == poutend))
  1953. {
  1954. elf_uncompress_failed ();
  1955. return 0;
  1956. }
  1957. *pout++ = lit;
  1958. /* We will need to write the next byte soon. We ask
  1959. for high temporal locality because we will write
  1960. to the whole cache line soon. */
  1961. __builtin_prefetch (pout, 1, 3);
  1962. }
  1963. else if (lit == 256)
  1964. {
  1965. /* The end of the block. */
  1966. break;
  1967. }
  1968. else
  1969. {
  1970. unsigned int dist;
  1971. unsigned int len;
  1972. /* Convert lit into a length. */
  1973. if (lit < 265)
  1974. len = lit - 257 + 3;
  1975. else if (lit == 285)
  1976. len = 258;
  1977. else if (unlikely (lit > 285))
  1978. {
  1979. elf_uncompress_failed ();
  1980. return 0;
  1981. }
  1982. else
  1983. {
  1984. unsigned int extra;
  1985. if (!elf_fetch_bits (&pin, pinend, &val, &bits))
  1986. return 0;
  1987. /* This is an expression for the table of length
  1988. codes in RFC 1951 3.2.5. */
  1989. lit -= 265;
  1990. extra = (lit >> 2) + 1;
  1991. len = (lit & 3) << extra;
  1992. len += 11;
  1993. len += ((1U << (extra - 1)) - 1) << 3;
  1994. len += val & ((1U << extra) - 1);
  1995. val >>= extra;
  1996. bits -= extra;
  1997. }
  1998. if (!elf_fetch_bits (&pin, pinend, &val, &bits))
  1999. return 0;
  2000. t = tdist[val & 0xff];
  2001. b = (t >> ZLIB_HUFFMAN_BITS_SHIFT) & ZLIB_HUFFMAN_BITS_MASK;
  2002. v = t & ZLIB_HUFFMAN_VALUE_MASK;
  2003. if ((t & (1U << ZLIB_HUFFMAN_SECONDARY_SHIFT)) == 0)
  2004. {
  2005. dist = v;
  2006. val >>= b + 1;
  2007. bits -= b + 1;
  2008. }
  2009. else
  2010. {
  2011. t = tdist[v + 0x100 + ((val >> 8) & ((1U << b) - 1))];
  2012. b = ((t >> ZLIB_HUFFMAN_BITS_SHIFT)
  2013. & ZLIB_HUFFMAN_BITS_MASK);
  2014. dist = t & ZLIB_HUFFMAN_VALUE_MASK;
  2015. val >>= b + 8;
  2016. bits -= b + 8;
  2017. }
  2018. /* Convert dist to a distance. */
  2019. if (dist == 0)
  2020. {
  2021. /* A distance of 1. A common case, meaning
  2022. repeat the last character LEN times. */
  2023. if (unlikely (pout == porigout))
  2024. {
  2025. elf_uncompress_failed ();
  2026. return 0;
  2027. }
  2028. if (unlikely ((unsigned int) (poutend - pout) < len))
  2029. {
  2030. elf_uncompress_failed ();
  2031. return 0;
  2032. }
  2033. memset (pout, pout[-1], len);
  2034. pout += len;
  2035. }
  2036. else if (unlikely (dist > 29))
  2037. {
  2038. elf_uncompress_failed ();
  2039. return 0;
  2040. }
  2041. else
  2042. {
  2043. if (dist < 4)
  2044. dist = dist + 1;
  2045. else
  2046. {
  2047. unsigned int extra;
  2048. if (!elf_fetch_bits (&pin, pinend, &val, &bits))
  2049. return 0;
  2050. /* This is an expression for the table of
  2051. distance codes in RFC 1951 3.2.5. */
  2052. dist -= 4;
  2053. extra = (dist >> 1) + 1;
  2054. dist = (dist & 1) << extra;
  2055. dist += 5;
  2056. dist += ((1U << (extra - 1)) - 1) << 2;
  2057. dist += val & ((1U << extra) - 1);
  2058. val >>= extra;
  2059. bits -= extra;
  2060. }
  2061. /* Go back dist bytes, and copy len bytes from
  2062. there. */
  2063. if (unlikely ((unsigned int) (pout - porigout) < dist))
  2064. {
  2065. elf_uncompress_failed ();
  2066. return 0;
  2067. }
  2068. if (unlikely ((unsigned int) (poutend - pout) < len))
  2069. {
  2070. elf_uncompress_failed ();
  2071. return 0;
  2072. }
  2073. if (dist >= len)
  2074. {
  2075. memcpy (pout, pout - dist, len);
  2076. pout += len;
  2077. }
  2078. else
  2079. {
  2080. while (len > 0)
  2081. {
  2082. unsigned int copy;
  2083. copy = len < dist ? len : dist;
  2084. memcpy (pout, pout - dist, copy);
  2085. len -= copy;
  2086. pout += copy;
  2087. }
  2088. }
  2089. }
  2090. }
  2091. }
  2092. }
  2093. }
  2094. /* We should have filled the output buffer. */
  2095. if (unlikely (pout != poutend))
  2096. {
  2097. elf_uncompress_failed ();
  2098. return 0;
  2099. }
  2100. return 1;
  2101. }
  2102. /* Verify the zlib checksum. The checksum is in the 4 bytes at
  2103. CHECKBYTES, and the uncompressed data is at UNCOMPRESSED /
  2104. UNCOMPRESSED_SIZE. Returns 1 on success, 0 on failure. */
  2105. static int
  2106. elf_zlib_verify_checksum (const unsigned char *checkbytes,
  2107. const unsigned char *uncompressed,
  2108. size_t uncompressed_size)
  2109. {
  2110. unsigned int i;
  2111. unsigned int cksum;
  2112. const unsigned char *p;
  2113. uint32_t s1;
  2114. uint32_t s2;
  2115. size_t hsz;
  2116. cksum = 0;
  2117. for (i = 0; i < 4; i++)
  2118. cksum = (cksum << 8) | checkbytes[i];
  2119. s1 = 1;
  2120. s2 = 0;
  2121. /* Minimize modulo operations. */
  2122. p = uncompressed;
  2123. hsz = uncompressed_size;
  2124. while (hsz >= 5552)
  2125. {
  2126. for (i = 0; i < 5552; i += 16)
  2127. {
  2128. /* Manually unroll loop 16 times. */
  2129. s1 = s1 + *p++;
  2130. s2 = s2 + s1;
  2131. s1 = s1 + *p++;
  2132. s2 = s2 + s1;
  2133. s1 = s1 + *p++;
  2134. s2 = s2 + s1;
  2135. s1 = s1 + *p++;
  2136. s2 = s2 + s1;
  2137. s1 = s1 + *p++;
  2138. s2 = s2 + s1;
  2139. s1 = s1 + *p++;
  2140. s2 = s2 + s1;
  2141. s1 = s1 + *p++;
  2142. s2 = s2 + s1;
  2143. s1 = s1 + *p++;
  2144. s2 = s2 + s1;
  2145. s1 = s1 + *p++;
  2146. s2 = s2 + s1;
  2147. s1 = s1 + *p++;
  2148. s2 = s2 + s1;
  2149. s1 = s1 + *p++;
  2150. s2 = s2 + s1;
  2151. s1 = s1 + *p++;
  2152. s2 = s2 + s1;
  2153. s1 = s1 + *p++;
  2154. s2 = s2 + s1;
  2155. s1 = s1 + *p++;
  2156. s2 = s2 + s1;
  2157. s1 = s1 + *p++;
  2158. s2 = s2 + s1;
  2159. s1 = s1 + *p++;
  2160. s2 = s2 + s1;
  2161. }
  2162. hsz -= 5552;
  2163. s1 %= 65521;
  2164. s2 %= 65521;
  2165. }
  2166. while (hsz >= 16)
  2167. {
  2168. /* Manually unroll loop 16 times. */
  2169. s1 = s1 + *p++;
  2170. s2 = s2 + s1;
  2171. s1 = s1 + *p++;
  2172. s2 = s2 + s1;
  2173. s1 = s1 + *p++;
  2174. s2 = s2 + s1;
  2175. s1 = s1 + *p++;
  2176. s2 = s2 + s1;
  2177. s1 = s1 + *p++;
  2178. s2 = s2 + s1;
  2179. s1 = s1 + *p++;
  2180. s2 = s2 + s1;
  2181. s1 = s1 + *p++;
  2182. s2 = s2 + s1;
  2183. s1 = s1 + *p++;
  2184. s2 = s2 + s1;
  2185. s1 = s1 + *p++;
  2186. s2 = s2 + s1;
  2187. s1 = s1 + *p++;
  2188. s2 = s2 + s1;
  2189. s1 = s1 + *p++;
  2190. s2 = s2 + s1;
  2191. s1 = s1 + *p++;
  2192. s2 = s2 + s1;
  2193. s1 = s1 + *p++;
  2194. s2 = s2 + s1;
  2195. s1 = s1 + *p++;
  2196. s2 = s2 + s1;
  2197. s1 = s1 + *p++;
  2198. s2 = s2 + s1;
  2199. s1 = s1 + *p++;
  2200. s2 = s2 + s1;
  2201. hsz -= 16;
  2202. }
  2203. for (i = 0; i < hsz; ++i)
  2204. {
  2205. s1 = s1 + *p++;
  2206. s2 = s2 + s1;
  2207. }
  2208. s1 %= 65521;
  2209. s2 %= 65521;
  2210. if (unlikely ((s2 << 16) + s1 != cksum))
  2211. {
  2212. elf_uncompress_failed ();
  2213. return 0;
  2214. }
  2215. return 1;
  2216. }
  2217. /* Inflate a zlib stream from PIN/SIN to POUT/SOUT, and verify the
  2218. checksum. Return 1 on success, 0 on error. */
  2219. static int
  2220. elf_zlib_inflate_and_verify (const unsigned char *pin, size_t sin,
  2221. uint16_t *zdebug_table, unsigned char *pout,
  2222. size_t sout)
  2223. {
  2224. if (!elf_zlib_inflate (pin, sin, zdebug_table, pout, sout))
  2225. return 0;
  2226. if (!elf_zlib_verify_checksum (pin + sin - 4, pout, sout))
  2227. return 0;
  2228. return 1;
  2229. }
  2230. /* For working memory during zstd compression, we need
  2231. - a literal length FSE table: 512 64-bit values == 4096 bytes
  2232. - a match length FSE table: 512 64-bit values == 4096 bytes
  2233. - a offset FSE table: 256 64-bit values == 2048 bytes
  2234. - a Huffman tree: 2048 uint16_t values == 4096 bytes
  2235. - scratch space, one of
  2236. - to build an FSE table: 512 uint16_t values == 1024 bytes
  2237. - to build a Huffman tree: 512 uint16_t + 256 uint32_t == 2048 bytes
  2238. */
  2239. #define ZSTD_TABLE_SIZE \
  2240. (2 * 512 * sizeof (struct elf_zstd_fse_baseline_entry) \
  2241. + 256 * sizeof (struct elf_zstd_fse_baseline_entry) \
  2242. + 2048 * sizeof (uint16_t) \
  2243. + 512 * sizeof (uint16_t) + 256 * sizeof (uint32_t))
  2244. #define ZSTD_TABLE_LITERAL_FSE_OFFSET (0)
  2245. #define ZSTD_TABLE_MATCH_FSE_OFFSET \
  2246. (512 * sizeof (struct elf_zstd_fse_baseline_entry))
  2247. #define ZSTD_TABLE_OFFSET_FSE_OFFSET \
  2248. (ZSTD_TABLE_MATCH_FSE_OFFSET \
  2249. + 512 * sizeof (struct elf_zstd_fse_baseline_entry))
  2250. #define ZSTD_TABLE_HUFFMAN_OFFSET \
  2251. (ZSTD_TABLE_OFFSET_FSE_OFFSET \
  2252. + 256 * sizeof (struct elf_zstd_fse_baseline_entry))
  2253. #define ZSTD_TABLE_WORK_OFFSET \
  2254. (ZSTD_TABLE_HUFFMAN_OFFSET + 2048 * sizeof (uint16_t))
  2255. /* An entry in a zstd FSE table. */
  2256. struct elf_zstd_fse_entry
  2257. {
  2258. /* The value that this FSE entry represents. */
  2259. unsigned char symbol;
  2260. /* The number of bits to read to determine the next state. */
  2261. unsigned char bits;
  2262. /* Add the bits to this base to get the next state. */
  2263. uint16_t base;
  2264. };
  2265. static int
  2266. elf_zstd_build_fse (const int16_t *, int, uint16_t *, int,
  2267. struct elf_zstd_fse_entry *);
  2268. /* Read a zstd FSE table and build the decoding table in *TABLE, updating *PPIN
  2269. as it reads. ZDEBUG_TABLE is scratch space; it must be enough for 512
  2270. uint16_t values (1024 bytes). MAXIDX is the maximum number of symbols
  2271. permitted. *TABLE_BITS is the maximum number of bits for symbols in the
  2272. table: the size of *TABLE is at least 1 << *TABLE_BITS. This updates
  2273. *TABLE_BITS to the actual number of bits. Returns 1 on success, 0 on
  2274. error. */
  2275. static int
  2276. elf_zstd_read_fse (const unsigned char **ppin, const unsigned char *pinend,
  2277. uint16_t *zdebug_table, int maxidx,
  2278. struct elf_zstd_fse_entry *table, int *table_bits)
  2279. {
  2280. const unsigned char *pin;
  2281. int16_t *norm;
  2282. uint16_t *next;
  2283. uint64_t val;
  2284. unsigned int bits;
  2285. int accuracy_log;
  2286. uint32_t remaining;
  2287. uint32_t threshold;
  2288. int bits_needed;
  2289. int idx;
  2290. int prev0;
  2291. pin = *ppin;
  2292. norm = (int16_t *) zdebug_table;
  2293. next = zdebug_table + 256;
  2294. if (unlikely (pin + 3 >= pinend))
  2295. {
  2296. elf_uncompress_failed ();
  2297. return 0;
  2298. }
  2299. /* Align PIN to a 32-bit boundary. */
  2300. val = 0;
  2301. bits = 0;
  2302. while ((((uintptr_t) pin) & 3) != 0)
  2303. {
  2304. val |= (uint64_t)*pin << bits;
  2305. bits += 8;
  2306. ++pin;
  2307. }
  2308. if (!elf_fetch_bits (&pin, pinend, &val, &bits))
  2309. return 0;
  2310. accuracy_log = (val & 0xf) + 5;
  2311. if (accuracy_log > *table_bits)
  2312. {
  2313. elf_uncompress_failed ();
  2314. return 0;
  2315. }
  2316. *table_bits = accuracy_log;
  2317. val >>= 4;
  2318. bits -= 4;
  2319. /* This code is mostly copied from the reference implementation. */
  2320. /* The number of remaining probabilities, plus 1. This sets the number of
  2321. bits that need to be read for the next value. */
  2322. remaining = (1 << accuracy_log) + 1;
  2323. /* The current difference between small and large values, which depends on
  2324. the number of remaining values. Small values use one less bit. */
  2325. threshold = 1 << accuracy_log;
  2326. /* The number of bits used to compute threshold. */
  2327. bits_needed = accuracy_log + 1;
  2328. /* The next character value. */
  2329. idx = 0;
  2330. /* Whether the last count was 0. */
  2331. prev0 = 0;
  2332. while (remaining > 1 && idx <= maxidx)
  2333. {
  2334. uint32_t max;
  2335. int32_t count;
  2336. if (!elf_fetch_bits (&pin, pinend, &val, &bits))
  2337. return 0;
  2338. if (prev0)
  2339. {
  2340. int zidx;
  2341. /* Previous count was 0, so there is a 2-bit repeat flag. If the
  2342. 2-bit flag is 0b11, it adds 3 and then there is another repeat
  2343. flag. */
  2344. zidx = idx;
  2345. while ((val & 0xfff) == 0xfff)
  2346. {
  2347. zidx += 3 * 6;
  2348. val >>= 12;
  2349. bits -= 12;
  2350. if (!elf_fetch_bits (&pin, pinend, &val, &bits))
  2351. return 0;
  2352. }
  2353. while ((val & 3) == 3)
  2354. {
  2355. zidx += 3;
  2356. val >>= 2;
  2357. bits -= 2;
  2358. if (!elf_fetch_bits (&pin, pinend, &val, &bits))
  2359. return 0;
  2360. }
  2361. /* We have at least 13 bits here, don't need to fetch. */
  2362. zidx += val & 3;
  2363. val >>= 2;
  2364. bits -= 2;
  2365. if (unlikely (zidx > maxidx))
  2366. {
  2367. elf_uncompress_failed ();
  2368. return 0;
  2369. }
  2370. for (; idx < zidx; idx++)
  2371. norm[idx] = 0;
  2372. prev0 = 0;
  2373. continue;
  2374. }
  2375. max = (2 * threshold - 1) - remaining;
  2376. if ((val & (threshold - 1)) < max)
  2377. {
  2378. /* A small value. */
  2379. count = (int32_t) ((uint32_t) val & (threshold - 1));
  2380. val >>= bits_needed - 1;
  2381. bits -= bits_needed - 1;
  2382. }
  2383. else
  2384. {
  2385. /* A large value. */
  2386. count = (int32_t) ((uint32_t) val & (2 * threshold - 1));
  2387. if (count >= (int32_t) threshold)
  2388. count -= (int32_t) max;
  2389. val >>= bits_needed;
  2390. bits -= bits_needed;
  2391. }
  2392. count--;
  2393. if (count >= 0)
  2394. remaining -= count;
  2395. else
  2396. remaining--;
  2397. if (unlikely (idx >= 256))
  2398. {
  2399. elf_uncompress_failed ();
  2400. return 0;
  2401. }
  2402. norm[idx] = (int16_t) count;
  2403. ++idx;
  2404. prev0 = count == 0;
  2405. while (remaining < threshold)
  2406. {
  2407. bits_needed--;
  2408. threshold >>= 1;
  2409. }
  2410. }
  2411. if (unlikely (remaining != 1))
  2412. {
  2413. elf_uncompress_failed ();
  2414. return 0;
  2415. }
  2416. /* If we've read ahead more than a byte, back up. */
  2417. while (bits >= 8)
  2418. {
  2419. --pin;
  2420. bits -= 8;
  2421. }
  2422. *ppin = pin;
  2423. for (; idx <= maxidx; idx++)
  2424. norm[idx] = 0;
  2425. return elf_zstd_build_fse (norm, idx, next, *table_bits, table);
  2426. }
  2427. /* Build the FSE decoding table from a list of probabilities. This reads from
  2428. NORM of length IDX, uses NEXT as scratch space, and writes to *TABLE, whose
  2429. size is TABLE_BITS. */
  2430. static int
  2431. elf_zstd_build_fse (const int16_t *norm, int idx, uint16_t *next,
  2432. int table_bits, struct elf_zstd_fse_entry *table)
  2433. {
  2434. int table_size;
  2435. int high_threshold;
  2436. int i;
  2437. int pos;
  2438. int step;
  2439. int mask;
  2440. table_size = 1 << table_bits;
  2441. high_threshold = table_size - 1;
  2442. for (i = 0; i < idx; i++)
  2443. {
  2444. int16_t n;
  2445. n = norm[i];
  2446. if (n >= 0)
  2447. next[i] = (uint16_t) n;
  2448. else
  2449. {
  2450. table[high_threshold].symbol = (unsigned char) i;
  2451. high_threshold--;
  2452. next[i] = 1;
  2453. }
  2454. }
  2455. pos = 0;
  2456. step = (table_size >> 1) + (table_size >> 3) + 3;
  2457. mask = table_size - 1;
  2458. for (i = 0; i < idx; i++)
  2459. {
  2460. int n;
  2461. int j;
  2462. n = (int) norm[i];
  2463. for (j = 0; j < n; j++)
  2464. {
  2465. table[pos].symbol = (unsigned char) i;
  2466. pos = (pos + step) & mask;
  2467. while (unlikely (pos > high_threshold))
  2468. pos = (pos + step) & mask;
  2469. }
  2470. }
  2471. if (unlikely (pos != 0))
  2472. {
  2473. elf_uncompress_failed ();
  2474. return 0;
  2475. }
  2476. for (i = 0; i < table_size; i++)
  2477. {
  2478. unsigned char sym;
  2479. uint16_t next_state;
  2480. int high_bit;
  2481. int bits;
  2482. sym = table[i].symbol;
  2483. next_state = next[sym];
  2484. ++next[sym];
  2485. if (next_state == 0)
  2486. {
  2487. elf_uncompress_failed ();
  2488. return 0;
  2489. }
  2490. high_bit = 31 - __builtin_clz (next_state);
  2491. bits = table_bits - high_bit;
  2492. table[i].bits = (unsigned char) bits;
  2493. table[i].base = (uint16_t) ((next_state << bits) - table_size);
  2494. }
  2495. return 1;
  2496. }
  2497. /* Encode the baseline and bits into a single 32-bit value. */
  2498. #define ZSTD_ENCODE_BASELINE_BITS(baseline, basebits) \
  2499. ((uint32_t)(baseline) | ((uint32_t)(basebits) << 24))
  2500. #define ZSTD_DECODE_BASELINE(baseline_basebits) \
  2501. ((uint32_t)(baseline_basebits) & 0xffffff)
  2502. #define ZSTD_DECODE_BASEBITS(baseline_basebits) \
  2503. ((uint32_t)(baseline_basebits) >> 24)
  2504. /* Given a literal length code, we need to read a number of bits and add that
  2505. to a baseline. For states 0 to 15 the baseline is the state and the number
  2506. of bits is zero. */
  2507. #define ZSTD_LITERAL_LENGTH_BASELINE_OFFSET (16)
  2508. static const uint32_t elf_zstd_literal_length_base[] =
  2509. {
  2510. ZSTD_ENCODE_BASELINE_BITS(16, 1),
  2511. ZSTD_ENCODE_BASELINE_BITS(18, 1),
  2512. ZSTD_ENCODE_BASELINE_BITS(20, 1),
  2513. ZSTD_ENCODE_BASELINE_BITS(22, 1),
  2514. ZSTD_ENCODE_BASELINE_BITS(24, 2),
  2515. ZSTD_ENCODE_BASELINE_BITS(28, 2),
  2516. ZSTD_ENCODE_BASELINE_BITS(32, 3),
  2517. ZSTD_ENCODE_BASELINE_BITS(40, 3),
  2518. ZSTD_ENCODE_BASELINE_BITS(48, 4),
  2519. ZSTD_ENCODE_BASELINE_BITS(64, 6),
  2520. ZSTD_ENCODE_BASELINE_BITS(128, 7),
  2521. ZSTD_ENCODE_BASELINE_BITS(256, 8),
  2522. ZSTD_ENCODE_BASELINE_BITS(512, 9),
  2523. ZSTD_ENCODE_BASELINE_BITS(1024, 10),
  2524. ZSTD_ENCODE_BASELINE_BITS(2048, 11),
  2525. ZSTD_ENCODE_BASELINE_BITS(4096, 12),
  2526. ZSTD_ENCODE_BASELINE_BITS(8192, 13),
  2527. ZSTD_ENCODE_BASELINE_BITS(16384, 14),
  2528. ZSTD_ENCODE_BASELINE_BITS(32768, 15),
  2529. ZSTD_ENCODE_BASELINE_BITS(65536, 16)
  2530. };
  2531. /* The same applies to match length codes. For states 0 to 31 the baseline is
  2532. the state + 3 and the number of bits is zero. */
  2533. #define ZSTD_MATCH_LENGTH_BASELINE_OFFSET (32)
  2534. static const uint32_t elf_zstd_match_length_base[] =
  2535. {
  2536. ZSTD_ENCODE_BASELINE_BITS(35, 1),
  2537. ZSTD_ENCODE_BASELINE_BITS(37, 1),
  2538. ZSTD_ENCODE_BASELINE_BITS(39, 1),
  2539. ZSTD_ENCODE_BASELINE_BITS(41, 1),
  2540. ZSTD_ENCODE_BASELINE_BITS(43, 2),
  2541. ZSTD_ENCODE_BASELINE_BITS(47, 2),
  2542. ZSTD_ENCODE_BASELINE_BITS(51, 3),
  2543. ZSTD_ENCODE_BASELINE_BITS(59, 3),
  2544. ZSTD_ENCODE_BASELINE_BITS(67, 4),
  2545. ZSTD_ENCODE_BASELINE_BITS(83, 4),
  2546. ZSTD_ENCODE_BASELINE_BITS(99, 5),
  2547. ZSTD_ENCODE_BASELINE_BITS(131, 7),
  2548. ZSTD_ENCODE_BASELINE_BITS(259, 8),
  2549. ZSTD_ENCODE_BASELINE_BITS(515, 9),
  2550. ZSTD_ENCODE_BASELINE_BITS(1027, 10),
  2551. ZSTD_ENCODE_BASELINE_BITS(2051, 11),
  2552. ZSTD_ENCODE_BASELINE_BITS(4099, 12),
  2553. ZSTD_ENCODE_BASELINE_BITS(8195, 13),
  2554. ZSTD_ENCODE_BASELINE_BITS(16387, 14),
  2555. ZSTD_ENCODE_BASELINE_BITS(32771, 15),
  2556. ZSTD_ENCODE_BASELINE_BITS(65539, 16)
  2557. };
  2558. /* An entry in an FSE table used for literal/match/length values. For these we
  2559. have to map the symbol to a baseline value, and we have to read zero or more
  2560. bits and add that value to the baseline value. Rather than look the values
  2561. up in a separate table, we grow the FSE table so that we get better memory
  2562. caching. */
  2563. struct elf_zstd_fse_baseline_entry
  2564. {
  2565. /* The baseline for the value that this FSE entry represents.. */
  2566. uint32_t baseline;
  2567. /* The number of bits to read to add to the baseline. */
  2568. unsigned char basebits;
  2569. /* The number of bits to read to determine the next state. */
  2570. unsigned char bits;
  2571. /* Add the bits to this base to get the next state. */
  2572. uint16_t base;
  2573. };
  2574. /* Convert the literal length FSE table FSE_TABLE to an FSE baseline table at
  2575. BASELINE_TABLE. Note that FSE_TABLE and BASELINE_TABLE will overlap. */
  2576. static int
  2577. elf_zstd_make_literal_baseline_fse (
  2578. const struct elf_zstd_fse_entry *fse_table,
  2579. int table_bits,
  2580. struct elf_zstd_fse_baseline_entry *baseline_table)
  2581. {
  2582. size_t count;
  2583. const struct elf_zstd_fse_entry *pfse;
  2584. struct elf_zstd_fse_baseline_entry *pbaseline;
  2585. /* Convert backward to avoid overlap. */
  2586. count = 1U << table_bits;
  2587. pfse = fse_table + count;
  2588. pbaseline = baseline_table + count;
  2589. while (pfse > fse_table)
  2590. {
  2591. unsigned char symbol;
  2592. unsigned char bits;
  2593. uint16_t base;
  2594. --pfse;
  2595. --pbaseline;
  2596. symbol = pfse->symbol;
  2597. bits = pfse->bits;
  2598. base = pfse->base;
  2599. if (symbol < ZSTD_LITERAL_LENGTH_BASELINE_OFFSET)
  2600. {
  2601. pbaseline->baseline = (uint32_t)symbol;
  2602. pbaseline->basebits = 0;
  2603. }
  2604. else
  2605. {
  2606. unsigned int idx;
  2607. uint32_t basebits;
  2608. if (unlikely (symbol > 35))
  2609. {
  2610. elf_uncompress_failed ();
  2611. return 0;
  2612. }
  2613. idx = symbol - ZSTD_LITERAL_LENGTH_BASELINE_OFFSET;
  2614. basebits = elf_zstd_literal_length_base[idx];
  2615. pbaseline->baseline = ZSTD_DECODE_BASELINE(basebits);
  2616. pbaseline->basebits = ZSTD_DECODE_BASEBITS(basebits);
  2617. }
  2618. pbaseline->bits = bits;
  2619. pbaseline->base = base;
  2620. }
  2621. return 1;
  2622. }
  2623. /* Convert the offset length FSE table FSE_TABLE to an FSE baseline table at
  2624. BASELINE_TABLE. Note that FSE_TABLE and BASELINE_TABLE will overlap. */
  2625. static int
  2626. elf_zstd_make_offset_baseline_fse (
  2627. const struct elf_zstd_fse_entry *fse_table,
  2628. int table_bits,
  2629. struct elf_zstd_fse_baseline_entry *baseline_table)
  2630. {
  2631. size_t count;
  2632. const struct elf_zstd_fse_entry *pfse;
  2633. struct elf_zstd_fse_baseline_entry *pbaseline;
  2634. /* Convert backward to avoid overlap. */
  2635. count = 1U << table_bits;
  2636. pfse = fse_table + count;
  2637. pbaseline = baseline_table + count;
  2638. while (pfse > fse_table)
  2639. {
  2640. unsigned char symbol;
  2641. unsigned char bits;
  2642. uint16_t base;
  2643. --pfse;
  2644. --pbaseline;
  2645. symbol = pfse->symbol;
  2646. bits = pfse->bits;
  2647. base = pfse->base;
  2648. if (unlikely (symbol > 31))
  2649. {
  2650. elf_uncompress_failed ();
  2651. return 0;
  2652. }
  2653. /* The simple way to write this is
  2654. pbaseline->baseline = (uint32_t)1 << symbol;
  2655. pbaseline->basebits = symbol;
  2656. That will give us an offset value that corresponds to the one
  2657. described in the RFC. However, for offset values > 3, we have to
  2658. subtract 3. And for offset values 1, 2, 3 we use a repeated offset.
  2659. The baseline is always a power of 2, and is never 0, so for these low
  2660. values we will see one entry that is baseline 1, basebits 0, and one
  2661. entry that is baseline 2, basebits 1. All other entries will have
  2662. baseline >= 4 and basebits >= 2.
  2663. So we can check for RFC offset <= 3 by checking for basebits <= 1.
  2664. And that means that we can subtract 3 here and not worry about doing
  2665. it in the hot loop. */
  2666. pbaseline->baseline = (uint32_t)1 << symbol;
  2667. if (symbol >= 2)
  2668. pbaseline->baseline -= 3;
  2669. pbaseline->basebits = symbol;
  2670. pbaseline->bits = bits;
  2671. pbaseline->base = base;
  2672. }
  2673. return 1;
  2674. }
  2675. /* Convert the match length FSE table FSE_TABLE to an FSE baseline table at
  2676. BASELINE_TABLE. Note that FSE_TABLE and BASELINE_TABLE will overlap. */
  2677. static int
  2678. elf_zstd_make_match_baseline_fse (
  2679. const struct elf_zstd_fse_entry *fse_table,
  2680. int table_bits,
  2681. struct elf_zstd_fse_baseline_entry *baseline_table)
  2682. {
  2683. size_t count;
  2684. const struct elf_zstd_fse_entry *pfse;
  2685. struct elf_zstd_fse_baseline_entry *pbaseline;
  2686. /* Convert backward to avoid overlap. */
  2687. count = 1U << table_bits;
  2688. pfse = fse_table + count;
  2689. pbaseline = baseline_table + count;
  2690. while (pfse > fse_table)
  2691. {
  2692. unsigned char symbol;
  2693. unsigned char bits;
  2694. uint16_t base;
  2695. --pfse;
  2696. --pbaseline;
  2697. symbol = pfse->symbol;
  2698. bits = pfse->bits;
  2699. base = pfse->base;
  2700. if (symbol < ZSTD_MATCH_LENGTH_BASELINE_OFFSET)
  2701. {
  2702. pbaseline->baseline = (uint32_t)symbol + 3;
  2703. pbaseline->basebits = 0;
  2704. }
  2705. else
  2706. {
  2707. unsigned int idx;
  2708. uint32_t basebits;
  2709. if (unlikely (symbol > 52))
  2710. {
  2711. elf_uncompress_failed ();
  2712. return 0;
  2713. }
  2714. idx = symbol - ZSTD_MATCH_LENGTH_BASELINE_OFFSET;
  2715. basebits = elf_zstd_match_length_base[idx];
  2716. pbaseline->baseline = ZSTD_DECODE_BASELINE(basebits);
  2717. pbaseline->basebits = ZSTD_DECODE_BASEBITS(basebits);
  2718. }
  2719. pbaseline->bits = bits;
  2720. pbaseline->base = base;
  2721. }
  2722. return 1;
  2723. }
  2724. #ifdef BACKTRACE_GENERATE_ZSTD_FSE_TABLES
  2725. /* Used to generate the predefined FSE decoding tables for zstd. */
  2726. #include <stdio.h>
  2727. /* These values are straight from RFC 8878. */
  2728. static int16_t lit[36] =
  2729. {
  2730. 4, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1,
  2731. 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 2, 1, 1, 1, 1, 1,
  2732. -1,-1,-1,-1
  2733. };
  2734. static int16_t match[53] =
  2735. {
  2736. 1, 4, 3, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1,
  2737. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  2738. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,-1,-1,
  2739. -1,-1,-1,-1,-1
  2740. };
  2741. static int16_t offset[29] =
  2742. {
  2743. 1, 1, 1, 1, 1, 1, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1,
  2744. 1, 1, 1, 1, 1, 1, 1, 1,-1,-1,-1,-1,-1
  2745. };
  2746. static uint16_t next[256];
  2747. static void
  2748. print_table (const struct elf_zstd_fse_baseline_entry *table, size_t size)
  2749. {
  2750. size_t i;
  2751. printf ("{\n");
  2752. for (i = 0; i < size; i += 3)
  2753. {
  2754. int j;
  2755. printf (" ");
  2756. for (j = 0; j < 3 && i + j < size; ++j)
  2757. printf (" { %u, %d, %d, %d },", table[i + j].baseline,
  2758. table[i + j].basebits, table[i + j].bits,
  2759. table[i + j].base);
  2760. printf ("\n");
  2761. }
  2762. printf ("};\n");
  2763. }
  2764. int
  2765. main ()
  2766. {
  2767. struct elf_zstd_fse_entry lit_table[64];
  2768. struct elf_zstd_fse_baseline_entry lit_baseline[64];
  2769. struct elf_zstd_fse_entry match_table[64];
  2770. struct elf_zstd_fse_baseline_entry match_baseline[64];
  2771. struct elf_zstd_fse_entry offset_table[32];
  2772. struct elf_zstd_fse_baseline_entry offset_baseline[32];
  2773. if (!elf_zstd_build_fse (lit, sizeof lit / sizeof lit[0], next,
  2774. 6, lit_table))
  2775. {
  2776. fprintf (stderr, "elf_zstd_build_fse failed\n");
  2777. exit (EXIT_FAILURE);
  2778. }
  2779. if (!elf_zstd_make_literal_baseline_fse (lit_table, 6, lit_baseline))
  2780. {
  2781. fprintf (stderr, "elf_zstd_make_literal_baseline_fse failed\n");
  2782. exit (EXIT_FAILURE);
  2783. }
  2784. printf ("static const struct elf_zstd_fse_baseline_entry "
  2785. "elf_zstd_lit_table[64] =\n");
  2786. print_table (lit_baseline,
  2787. sizeof lit_baseline / sizeof lit_baseline[0]);
  2788. printf ("\n");
  2789. if (!elf_zstd_build_fse (match, sizeof match / sizeof match[0], next,
  2790. 6, match_table))
  2791. {
  2792. fprintf (stderr, "elf_zstd_build_fse failed\n");
  2793. exit (EXIT_FAILURE);
  2794. }
  2795. if (!elf_zstd_make_match_baseline_fse (match_table, 6, match_baseline))
  2796. {
  2797. fprintf (stderr, "elf_zstd_make_match_baseline_fse failed\n");
  2798. exit (EXIT_FAILURE);
  2799. }
  2800. printf ("static const struct elf_zstd_fse_baseline_entry "
  2801. "elf_zstd_match_table[64] =\n");
  2802. print_table (match_baseline,
  2803. sizeof match_baseline / sizeof match_baseline[0]);
  2804. printf ("\n");
  2805. if (!elf_zstd_build_fse (offset, sizeof offset / sizeof offset[0], next,
  2806. 5, offset_table))
  2807. {
  2808. fprintf (stderr, "elf_zstd_build_fse failed\n");
  2809. exit (EXIT_FAILURE);
  2810. }
  2811. if (!elf_zstd_make_offset_baseline_fse (offset_table, 5, offset_baseline))
  2812. {
  2813. fprintf (stderr, "elf_zstd_make_offset_baseline_fse failed\n");
  2814. exit (EXIT_FAILURE);
  2815. }
  2816. printf ("static const struct elf_zstd_fse_baseline_entry "
  2817. "elf_zstd_offset_table[32] =\n");
  2818. print_table (offset_baseline,
  2819. sizeof offset_baseline / sizeof offset_baseline[0]);
  2820. printf ("\n");
  2821. return 0;
  2822. }
  2823. #endif
  2824. /* The fixed tables generated by the #ifdef'ed out main function
  2825. above. */
  2826. static const struct elf_zstd_fse_baseline_entry elf_zstd_lit_table[64] =
  2827. {
  2828. { 0, 0, 4, 0 }, { 0, 0, 4, 16 }, { 1, 0, 5, 32 },
  2829. { 3, 0, 5, 0 }, { 4, 0, 5, 0 }, { 6, 0, 5, 0 },
  2830. { 7, 0, 5, 0 }, { 9, 0, 5, 0 }, { 10, 0, 5, 0 },
  2831. { 12, 0, 5, 0 }, { 14, 0, 6, 0 }, { 16, 1, 5, 0 },
  2832. { 20, 1, 5, 0 }, { 22, 1, 5, 0 }, { 28, 2, 5, 0 },
  2833. { 32, 3, 5, 0 }, { 48, 4, 5, 0 }, { 64, 6, 5, 32 },
  2834. { 128, 7, 5, 0 }, { 256, 8, 6, 0 }, { 1024, 10, 6, 0 },
  2835. { 4096, 12, 6, 0 }, { 0, 0, 4, 32 }, { 1, 0, 4, 0 },
  2836. { 2, 0, 5, 0 }, { 4, 0, 5, 32 }, { 5, 0, 5, 0 },
  2837. { 7, 0, 5, 32 }, { 8, 0, 5, 0 }, { 10, 0, 5, 32 },
  2838. { 11, 0, 5, 0 }, { 13, 0, 6, 0 }, { 16, 1, 5, 32 },
  2839. { 18, 1, 5, 0 }, { 22, 1, 5, 32 }, { 24, 2, 5, 0 },
  2840. { 32, 3, 5, 32 }, { 40, 3, 5, 0 }, { 64, 6, 4, 0 },
  2841. { 64, 6, 4, 16 }, { 128, 7, 5, 32 }, { 512, 9, 6, 0 },
  2842. { 2048, 11, 6, 0 }, { 0, 0, 4, 48 }, { 1, 0, 4, 16 },
  2843. { 2, 0, 5, 32 }, { 3, 0, 5, 32 }, { 5, 0, 5, 32 },
  2844. { 6, 0, 5, 32 }, { 8, 0, 5, 32 }, { 9, 0, 5, 32 },
  2845. { 11, 0, 5, 32 }, { 12, 0, 5, 32 }, { 15, 0, 6, 0 },
  2846. { 18, 1, 5, 32 }, { 20, 1, 5, 32 }, { 24, 2, 5, 32 },
  2847. { 28, 2, 5, 32 }, { 40, 3, 5, 32 }, { 48, 4, 5, 32 },
  2848. { 65536, 16, 6, 0 }, { 32768, 15, 6, 0 }, { 16384, 14, 6, 0 },
  2849. { 8192, 13, 6, 0 },
  2850. };
  2851. static const struct elf_zstd_fse_baseline_entry elf_zstd_match_table[64] =
  2852. {
  2853. { 3, 0, 6, 0 }, { 4, 0, 4, 0 }, { 5, 0, 5, 32 },
  2854. { 6, 0, 5, 0 }, { 8, 0, 5, 0 }, { 9, 0, 5, 0 },
  2855. { 11, 0, 5, 0 }, { 13, 0, 6, 0 }, { 16, 0, 6, 0 },
  2856. { 19, 0, 6, 0 }, { 22, 0, 6, 0 }, { 25, 0, 6, 0 },
  2857. { 28, 0, 6, 0 }, { 31, 0, 6, 0 }, { 34, 0, 6, 0 },
  2858. { 37, 1, 6, 0 }, { 41, 1, 6, 0 }, { 47, 2, 6, 0 },
  2859. { 59, 3, 6, 0 }, { 83, 4, 6, 0 }, { 131, 7, 6, 0 },
  2860. { 515, 9, 6, 0 }, { 4, 0, 4, 16 }, { 5, 0, 4, 0 },
  2861. { 6, 0, 5, 32 }, { 7, 0, 5, 0 }, { 9, 0, 5, 32 },
  2862. { 10, 0, 5, 0 }, { 12, 0, 6, 0 }, { 15, 0, 6, 0 },
  2863. { 18, 0, 6, 0 }, { 21, 0, 6, 0 }, { 24, 0, 6, 0 },
  2864. { 27, 0, 6, 0 }, { 30, 0, 6, 0 }, { 33, 0, 6, 0 },
  2865. { 35, 1, 6, 0 }, { 39, 1, 6, 0 }, { 43, 2, 6, 0 },
  2866. { 51, 3, 6, 0 }, { 67, 4, 6, 0 }, { 99, 5, 6, 0 },
  2867. { 259, 8, 6, 0 }, { 4, 0, 4, 32 }, { 4, 0, 4, 48 },
  2868. { 5, 0, 4, 16 }, { 7, 0, 5, 32 }, { 8, 0, 5, 32 },
  2869. { 10, 0, 5, 32 }, { 11, 0, 5, 32 }, { 14, 0, 6, 0 },
  2870. { 17, 0, 6, 0 }, { 20, 0, 6, 0 }, { 23, 0, 6, 0 },
  2871. { 26, 0, 6, 0 }, { 29, 0, 6, 0 }, { 32, 0, 6, 0 },
  2872. { 65539, 16, 6, 0 }, { 32771, 15, 6, 0 }, { 16387, 14, 6, 0 },
  2873. { 8195, 13, 6, 0 }, { 4099, 12, 6, 0 }, { 2051, 11, 6, 0 },
  2874. { 1027, 10, 6, 0 },
  2875. };
  2876. static const struct elf_zstd_fse_baseline_entry elf_zstd_offset_table[32] =
  2877. {
  2878. { 1, 0, 5, 0 }, { 61, 6, 4, 0 }, { 509, 9, 5, 0 },
  2879. { 32765, 15, 5, 0 }, { 2097149, 21, 5, 0 }, { 5, 3, 5, 0 },
  2880. { 125, 7, 4, 0 }, { 4093, 12, 5, 0 }, { 262141, 18, 5, 0 },
  2881. { 8388605, 23, 5, 0 }, { 29, 5, 5, 0 }, { 253, 8, 4, 0 },
  2882. { 16381, 14, 5, 0 }, { 1048573, 20, 5, 0 }, { 1, 2, 5, 0 },
  2883. { 125, 7, 4, 16 }, { 2045, 11, 5, 0 }, { 131069, 17, 5, 0 },
  2884. { 4194301, 22, 5, 0 }, { 13, 4, 5, 0 }, { 253, 8, 4, 16 },
  2885. { 8189, 13, 5, 0 }, { 524285, 19, 5, 0 }, { 2, 1, 5, 0 },
  2886. { 61, 6, 4, 16 }, { 1021, 10, 5, 0 }, { 65533, 16, 5, 0 },
  2887. { 268435453, 28, 5, 0 }, { 134217725, 27, 5, 0 }, { 67108861, 26, 5, 0 },
  2888. { 33554429, 25, 5, 0 }, { 16777213, 24, 5, 0 },
  2889. };
  2890. /* Read a zstd Huffman table and build the decoding table in *TABLE, reading
  2891. and updating *PPIN. This sets *PTABLE_BITS to the number of bits of the
  2892. table, such that the table length is 1 << *TABLE_BITS. ZDEBUG_TABLE is
  2893. scratch space; it must be enough for 512 uint16_t values + 256 32-bit values
  2894. (2048 bytes). Returns 1 on success, 0 on error. */
  2895. static int
  2896. elf_zstd_read_huff (const unsigned char **ppin, const unsigned char *pinend,
  2897. uint16_t *zdebug_table, uint16_t *table, int *ptable_bits)
  2898. {
  2899. const unsigned char *pin;
  2900. unsigned char hdr;
  2901. unsigned char *weights;
  2902. size_t count;
  2903. uint32_t *weight_mark;
  2904. size_t i;
  2905. uint32_t weight_mask;
  2906. size_t table_bits;
  2907. pin = *ppin;
  2908. if (unlikely (pin >= pinend))
  2909. {
  2910. elf_uncompress_failed ();
  2911. return 0;
  2912. }
  2913. hdr = *pin;
  2914. ++pin;
  2915. weights = (unsigned char *) zdebug_table;
  2916. if (hdr < 128)
  2917. {
  2918. /* Table is compressed using FSE. */
  2919. struct elf_zstd_fse_entry *fse_table;
  2920. int fse_table_bits;
  2921. uint16_t *scratch;
  2922. const unsigned char *pfse;
  2923. const unsigned char *pback;
  2924. uint64_t val;
  2925. unsigned int bits;
  2926. unsigned int state1, state2;
  2927. /* SCRATCH is used temporarily by elf_zstd_read_fse. It overlaps
  2928. WEIGHTS. */
  2929. scratch = zdebug_table;
  2930. fse_table = (struct elf_zstd_fse_entry *) (scratch + 512);
  2931. fse_table_bits = 6;
  2932. pfse = pin;
  2933. if (!elf_zstd_read_fse (&pfse, pinend, scratch, 255, fse_table,
  2934. &fse_table_bits))
  2935. return 0;
  2936. if (unlikely (pin + hdr > pinend))
  2937. {
  2938. elf_uncompress_failed ();
  2939. return 0;
  2940. }
  2941. /* We no longer need SCRATCH. Start recording weights. We need up to
  2942. 256 bytes of weights and 64 bytes of rank counts, so it won't overlap
  2943. FSE_TABLE. */
  2944. pback = pin + hdr - 1;
  2945. if (!elf_fetch_backward_init (&pback, pfse, &val, &bits))
  2946. return 0;
  2947. bits -= fse_table_bits;
  2948. state1 = (val >> bits) & ((1U << fse_table_bits) - 1);
  2949. bits -= fse_table_bits;
  2950. state2 = (val >> bits) & ((1U << fse_table_bits) - 1);
  2951. /* There are two independent FSE streams, tracked by STATE1 and STATE2.
  2952. We decode them alternately. */
  2953. count = 0;
  2954. while (1)
  2955. {
  2956. struct elf_zstd_fse_entry *pt;
  2957. uint64_t v;
  2958. pt = &fse_table[state1];
  2959. if (unlikely (pin < pinend) && bits < pt->bits)
  2960. {
  2961. if (unlikely (count >= 254))
  2962. {
  2963. elf_uncompress_failed ();
  2964. return 0;
  2965. }
  2966. weights[count] = (unsigned char) pt->symbol;
  2967. weights[count + 1] = (unsigned char) fse_table[state2].symbol;
  2968. count += 2;
  2969. break;
  2970. }
  2971. if (unlikely (pt->bits == 0))
  2972. v = 0;
  2973. else
  2974. {
  2975. if (!elf_fetch_bits_backward (&pback, pfse, &val, &bits))
  2976. return 0;
  2977. bits -= pt->bits;
  2978. v = (val >> bits) & (((uint64_t)1 << pt->bits) - 1);
  2979. }
  2980. state1 = pt->base + v;
  2981. if (unlikely (count >= 255))
  2982. {
  2983. elf_uncompress_failed ();
  2984. return 0;
  2985. }
  2986. weights[count] = pt->symbol;
  2987. ++count;
  2988. pt = &fse_table[state2];
  2989. if (unlikely (pin < pinend && bits < pt->bits))
  2990. {
  2991. if (unlikely (count >= 254))
  2992. {
  2993. elf_uncompress_failed ();
  2994. return 0;
  2995. }
  2996. weights[count] = (unsigned char) pt->symbol;
  2997. weights[count + 1] = (unsigned char) fse_table[state1].symbol;
  2998. count += 2;
  2999. break;
  3000. }
  3001. if (unlikely (pt->bits == 0))
  3002. v = 0;
  3003. else
  3004. {
  3005. if (!elf_fetch_bits_backward (&pback, pfse, &val, &bits))
  3006. return 0;
  3007. bits -= pt->bits;
  3008. v = (val >> bits) & (((uint64_t)1 << pt->bits) - 1);
  3009. }
  3010. state2 = pt->base + v;
  3011. if (unlikely (count >= 255))
  3012. {
  3013. elf_uncompress_failed ();
  3014. return 0;
  3015. }
  3016. weights[count] = pt->symbol;
  3017. ++count;
  3018. }
  3019. pin += hdr;
  3020. }
  3021. else
  3022. {
  3023. /* Table is not compressed. Each weight is 4 bits. */
  3024. count = hdr - 127;
  3025. if (unlikely (pin + ((count + 1) / 2) >= pinend))
  3026. {
  3027. elf_uncompress_failed ();
  3028. return 0;
  3029. }
  3030. for (i = 0; i < count; i += 2)
  3031. {
  3032. unsigned char b;
  3033. b = *pin;
  3034. ++pin;
  3035. weights[i] = b >> 4;
  3036. weights[i + 1] = b & 0xf;
  3037. }
  3038. }
  3039. weight_mark = (uint32_t *) (weights + 256);
  3040. memset (weight_mark, 0, 13 * sizeof (uint32_t));
  3041. weight_mask = 0;
  3042. for (i = 0; i < count; ++i)
  3043. {
  3044. unsigned char w;
  3045. w = weights[i];
  3046. if (unlikely (w > 12))
  3047. {
  3048. elf_uncompress_failed ();
  3049. return 0;
  3050. }
  3051. ++weight_mark[w];
  3052. if (w > 0)
  3053. weight_mask += 1U << (w - 1);
  3054. }
  3055. if (unlikely (weight_mask == 0))
  3056. {
  3057. elf_uncompress_failed ();
  3058. return 0;
  3059. }
  3060. table_bits = 32 - __builtin_clz (weight_mask);
  3061. if (unlikely (table_bits > 11))
  3062. {
  3063. elf_uncompress_failed ();
  3064. return 0;
  3065. }
  3066. /* Work out the last weight value, which is omitted because the weights must
  3067. sum to a power of two. */
  3068. {
  3069. uint32_t left;
  3070. uint32_t high_bit;
  3071. left = ((uint32_t)1 << table_bits) - weight_mask;
  3072. if (left == 0)
  3073. {
  3074. elf_uncompress_failed ();
  3075. return 0;
  3076. }
  3077. high_bit = 31 - __builtin_clz (left);
  3078. if (((uint32_t)1 << high_bit) != left)
  3079. {
  3080. elf_uncompress_failed ();
  3081. return 0;
  3082. }
  3083. if (unlikely (count >= 256))
  3084. {
  3085. elf_uncompress_failed ();
  3086. return 0;
  3087. }
  3088. weights[count] = high_bit + 1;
  3089. ++count;
  3090. ++weight_mark[high_bit + 1];
  3091. }
  3092. if (weight_mark[1] < 2 || (weight_mark[1] & 1) != 0)
  3093. {
  3094. elf_uncompress_failed ();
  3095. return 0;
  3096. }
  3097. /* Change WEIGHT_MARK from a count of weights to the index of the first
  3098. symbol for that weight. We shift the indexes to also store how many we
  3099. have seen so far, below. */
  3100. {
  3101. uint32_t next;
  3102. next = 0;
  3103. for (i = 0; i < table_bits; ++i)
  3104. {
  3105. uint32_t cur;
  3106. cur = next;
  3107. next += weight_mark[i + 1] << i;
  3108. weight_mark[i + 1] = cur;
  3109. }
  3110. }
  3111. for (i = 0; i < count; ++i)
  3112. {
  3113. unsigned char weight;
  3114. uint32_t length;
  3115. uint16_t tval;
  3116. size_t start;
  3117. uint32_t j;
  3118. weight = weights[i];
  3119. if (weight == 0)
  3120. continue;
  3121. length = 1U << (weight - 1);
  3122. tval = (i << 8) | (table_bits + 1 - weight);
  3123. start = weight_mark[weight];
  3124. for (j = 0; j < length; ++j)
  3125. table[start + j] = tval;
  3126. weight_mark[weight] += length;
  3127. }
  3128. *ppin = pin;
  3129. *ptable_bits = (int)table_bits;
  3130. return 1;
  3131. }
  3132. /* Read and decompress the literals and store them ending at POUTEND. This
  3133. works because we are going to use all the literals in the output, so they
  3134. must fit into the output buffer. HUFFMAN_TABLE, and PHUFFMAN_TABLE_BITS
  3135. store the Huffman table across calls. SCRATCH is used to read a Huffman
  3136. table. Store the start of the decompressed literals in *PPLIT. Update
  3137. *PPIN. Return 1 on success, 0 on error. */
  3138. static int
  3139. elf_zstd_read_literals (const unsigned char **ppin,
  3140. const unsigned char *pinend,
  3141. unsigned char *pout,
  3142. unsigned char *poutend,
  3143. uint16_t *scratch,
  3144. uint16_t *huffman_table,
  3145. int *phuffman_table_bits,
  3146. unsigned char **pplit)
  3147. {
  3148. const unsigned char *pin;
  3149. unsigned char *plit;
  3150. unsigned char hdr;
  3151. uint32_t regenerated_size;
  3152. uint32_t compressed_size;
  3153. int streams;
  3154. uint32_t total_streams_size;
  3155. unsigned int huffman_table_bits;
  3156. uint64_t huffman_mask;
  3157. pin = *ppin;
  3158. if (unlikely (pin >= pinend))
  3159. {
  3160. elf_uncompress_failed ();
  3161. return 0;
  3162. }
  3163. hdr = *pin;
  3164. ++pin;
  3165. if ((hdr & 3) == 0 || (hdr & 3) == 1)
  3166. {
  3167. int raw;
  3168. /* Raw_Literals_Block or RLE_Literals_Block */
  3169. raw = (hdr & 3) == 0;
  3170. switch ((hdr >> 2) & 3)
  3171. {
  3172. case 0: case 2:
  3173. regenerated_size = hdr >> 3;
  3174. break;
  3175. case 1:
  3176. if (unlikely (pin >= pinend))
  3177. {
  3178. elf_uncompress_failed ();
  3179. return 0;
  3180. }
  3181. regenerated_size = (hdr >> 4) + ((uint32_t)(*pin) << 4);
  3182. ++pin;
  3183. break;
  3184. case 3:
  3185. if (unlikely (pin + 1 >= pinend))
  3186. {
  3187. elf_uncompress_failed ();
  3188. return 0;
  3189. }
  3190. regenerated_size = ((hdr >> 4)
  3191. + ((uint32_t)*pin << 4)
  3192. + ((uint32_t)pin[1] << 12));
  3193. pin += 2;
  3194. break;
  3195. default:
  3196. elf_uncompress_failed ();
  3197. return 0;
  3198. }
  3199. if (unlikely ((size_t)(poutend - pout) < regenerated_size))
  3200. {
  3201. elf_uncompress_failed ();
  3202. return 0;
  3203. }
  3204. plit = poutend - regenerated_size;
  3205. if (raw)
  3206. {
  3207. if (unlikely (pin + regenerated_size >= pinend))
  3208. {
  3209. elf_uncompress_failed ();
  3210. return 0;
  3211. }
  3212. memcpy (plit, pin, regenerated_size);
  3213. pin += regenerated_size;
  3214. }
  3215. else
  3216. {
  3217. if (pin >= pinend)
  3218. {
  3219. elf_uncompress_failed ();
  3220. return 0;
  3221. }
  3222. memset (plit, *pin, regenerated_size);
  3223. ++pin;
  3224. }
  3225. *ppin = pin;
  3226. *pplit = plit;
  3227. return 1;
  3228. }
  3229. /* Compressed_Literals_Block or Treeless_Literals_Block */
  3230. switch ((hdr >> 2) & 3)
  3231. {
  3232. case 0: case 1:
  3233. if (unlikely (pin + 1 >= pinend))
  3234. {
  3235. elf_uncompress_failed ();
  3236. return 0;
  3237. }
  3238. regenerated_size = (hdr >> 4) | ((uint32_t)(*pin & 0x3f) << 4);
  3239. compressed_size = (uint32_t)*pin >> 6 | ((uint32_t)pin[1] << 2);
  3240. pin += 2;
  3241. streams = ((hdr >> 2) & 3) == 0 ? 1 : 4;
  3242. break;
  3243. case 2:
  3244. if (unlikely (pin + 2 >= pinend))
  3245. {
  3246. elf_uncompress_failed ();
  3247. return 0;
  3248. }
  3249. regenerated_size = (((uint32_t)hdr >> 4)
  3250. | ((uint32_t)*pin << 4)
  3251. | (((uint32_t)pin[1] & 3) << 12));
  3252. compressed_size = (((uint32_t)pin[1] >> 2)
  3253. | ((uint32_t)pin[2] << 6));
  3254. pin += 3;
  3255. streams = 4;
  3256. break;
  3257. case 3:
  3258. if (unlikely (pin + 3 >= pinend))
  3259. {
  3260. elf_uncompress_failed ();
  3261. return 0;
  3262. }
  3263. regenerated_size = (((uint32_t)hdr >> 4)
  3264. | ((uint32_t)*pin << 4)
  3265. | (((uint32_t)pin[1] & 0x3f) << 12));
  3266. compressed_size = (((uint32_t)pin[1] >> 6)
  3267. | ((uint32_t)pin[2] << 2)
  3268. | ((uint32_t)pin[3] << 10));
  3269. pin += 4;
  3270. streams = 4;
  3271. break;
  3272. default:
  3273. elf_uncompress_failed ();
  3274. return 0;
  3275. }
  3276. if (unlikely (pin + compressed_size > pinend))
  3277. {
  3278. elf_uncompress_failed ();
  3279. return 0;
  3280. }
  3281. pinend = pin + compressed_size;
  3282. *ppin = pinend;
  3283. if (unlikely ((size_t)(poutend - pout) < regenerated_size))
  3284. {
  3285. elf_uncompress_failed ();
  3286. return 0;
  3287. }
  3288. plit = poutend - regenerated_size;
  3289. *pplit = plit;
  3290. total_streams_size = compressed_size;
  3291. if ((hdr & 3) == 2)
  3292. {
  3293. const unsigned char *ptable;
  3294. /* Compressed_Literals_Block. Read Huffman tree. */
  3295. ptable = pin;
  3296. if (!elf_zstd_read_huff (&ptable, pinend, scratch, huffman_table,
  3297. phuffman_table_bits))
  3298. return 0;
  3299. if (unlikely (total_streams_size < (size_t)(ptable - pin)))
  3300. {
  3301. elf_uncompress_failed ();
  3302. return 0;
  3303. }
  3304. total_streams_size -= ptable - pin;
  3305. pin = ptable;
  3306. }
  3307. else
  3308. {
  3309. /* Treeless_Literals_Block. Reuse previous Huffman tree. */
  3310. if (unlikely (*phuffman_table_bits == 0))
  3311. {
  3312. elf_uncompress_failed ();
  3313. return 0;
  3314. }
  3315. }
  3316. /* Decompress COMPRESSED_SIZE bytes of data at PIN using the huffman table,
  3317. storing REGENERATED_SIZE bytes of decompressed data at PLIT. */
  3318. huffman_table_bits = (unsigned int)*phuffman_table_bits;
  3319. huffman_mask = ((uint64_t)1 << huffman_table_bits) - 1;
  3320. if (streams == 1)
  3321. {
  3322. const unsigned char *pback;
  3323. const unsigned char *pbackend;
  3324. uint64_t val;
  3325. unsigned int bits;
  3326. uint32_t i;
  3327. pback = pin + total_streams_size - 1;
  3328. pbackend = pin;
  3329. if (!elf_fetch_backward_init (&pback, pbackend, &val, &bits))
  3330. return 0;
  3331. /* This is one of the inner loops of the decompression algorithm, so we
  3332. put some effort into optimization. We can't get more than 64 bytes
  3333. from a single call to elf_fetch_bits_backward, and we can't subtract
  3334. more than 11 bits at a time. */
  3335. if (regenerated_size >= 64)
  3336. {
  3337. unsigned char *plitstart;
  3338. unsigned char *plitstop;
  3339. plitstart = plit;
  3340. plitstop = plit + regenerated_size - 64;
  3341. while (plit < plitstop)
  3342. {
  3343. uint16_t t;
  3344. if (!elf_fetch_bits_backward (&pback, pbackend, &val, &bits))
  3345. return 0;
  3346. if (bits < 16)
  3347. break;
  3348. while (bits >= 33)
  3349. {
  3350. t = huffman_table[(val >> (bits - huffman_table_bits))
  3351. & huffman_mask];
  3352. *plit = t >> 8;
  3353. ++plit;
  3354. bits -= t & 0xff;
  3355. t = huffman_table[(val >> (bits - huffman_table_bits))
  3356. & huffman_mask];
  3357. *plit = t >> 8;
  3358. ++plit;
  3359. bits -= t & 0xff;
  3360. t = huffman_table[(val >> (bits - huffman_table_bits))
  3361. & huffman_mask];
  3362. *plit = t >> 8;
  3363. ++plit;
  3364. bits -= t & 0xff;
  3365. }
  3366. while (bits > 11)
  3367. {
  3368. t = huffman_table[(val >> (bits - huffman_table_bits))
  3369. & huffman_mask];
  3370. *plit = t >> 8;
  3371. ++plit;
  3372. bits -= t & 0xff;
  3373. }
  3374. }
  3375. regenerated_size -= plit - plitstart;
  3376. }
  3377. for (i = 0; i < regenerated_size; ++i)
  3378. {
  3379. uint16_t t;
  3380. if (!elf_fetch_bits_backward (&pback, pbackend, &val, &bits))
  3381. return 0;
  3382. if (unlikely (bits < huffman_table_bits))
  3383. {
  3384. t = huffman_table[(val << (huffman_table_bits - bits))
  3385. & huffman_mask];
  3386. if (unlikely (bits < (t & 0xff)))
  3387. {
  3388. elf_uncompress_failed ();
  3389. return 0;
  3390. }
  3391. }
  3392. else
  3393. t = huffman_table[(val >> (bits - huffman_table_bits))
  3394. & huffman_mask];
  3395. *plit = t >> 8;
  3396. ++plit;
  3397. bits -= t & 0xff;
  3398. }
  3399. return 1;
  3400. }
  3401. {
  3402. uint32_t stream_size1, stream_size2, stream_size3, stream_size4;
  3403. uint32_t tot;
  3404. const unsigned char *pback1, *pback2, *pback3, *pback4;
  3405. const unsigned char *pbackend1, *pbackend2, *pbackend3, *pbackend4;
  3406. uint64_t val1, val2, val3, val4;
  3407. unsigned int bits1, bits2, bits3, bits4;
  3408. unsigned char *plit1, *plit2, *plit3, *plit4;
  3409. uint32_t regenerated_stream_size;
  3410. uint32_t regenerated_stream_size4;
  3411. uint16_t t1, t2, t3, t4;
  3412. uint32_t i;
  3413. uint32_t limit;
  3414. /* Read jump table. */
  3415. if (unlikely (pin + 5 >= pinend))
  3416. {
  3417. elf_uncompress_failed ();
  3418. return 0;
  3419. }
  3420. stream_size1 = (uint32_t)*pin | ((uint32_t)pin[1] << 8);
  3421. pin += 2;
  3422. stream_size2 = (uint32_t)*pin | ((uint32_t)pin[1] << 8);
  3423. pin += 2;
  3424. stream_size3 = (uint32_t)*pin | ((uint32_t)pin[1] << 8);
  3425. pin += 2;
  3426. tot = stream_size1 + stream_size2 + stream_size3;
  3427. if (unlikely (tot > total_streams_size - 6))
  3428. {
  3429. elf_uncompress_failed ();
  3430. return 0;
  3431. }
  3432. stream_size4 = total_streams_size - 6 - tot;
  3433. pback1 = pin + stream_size1 - 1;
  3434. pbackend1 = pin;
  3435. pback2 = pback1 + stream_size2;
  3436. pbackend2 = pback1 + 1;
  3437. pback3 = pback2 + stream_size3;
  3438. pbackend3 = pback2 + 1;
  3439. pback4 = pback3 + stream_size4;
  3440. pbackend4 = pback3 + 1;
  3441. if (!elf_fetch_backward_init (&pback1, pbackend1, &val1, &bits1))
  3442. return 0;
  3443. if (!elf_fetch_backward_init (&pback2, pbackend2, &val2, &bits2))
  3444. return 0;
  3445. if (!elf_fetch_backward_init (&pback3, pbackend3, &val3, &bits3))
  3446. return 0;
  3447. if (!elf_fetch_backward_init (&pback4, pbackend4, &val4, &bits4))
  3448. return 0;
  3449. regenerated_stream_size = (regenerated_size + 3) / 4;
  3450. plit1 = plit;
  3451. plit2 = plit1 + regenerated_stream_size;
  3452. plit3 = plit2 + regenerated_stream_size;
  3453. plit4 = plit3 + regenerated_stream_size;
  3454. regenerated_stream_size4 = regenerated_size - regenerated_stream_size * 3;
  3455. /* We can't get more than 64 literal bytes from a single call to
  3456. elf_fetch_bits_backward. The fourth stream can be up to 3 bytes less,
  3457. so use as the limit. */
  3458. limit = regenerated_stream_size4 <= 64 ? 0 : regenerated_stream_size4 - 64;
  3459. i = 0;
  3460. while (i < limit)
  3461. {
  3462. if (!elf_fetch_bits_backward (&pback1, pbackend1, &val1, &bits1))
  3463. return 0;
  3464. if (!elf_fetch_bits_backward (&pback2, pbackend2, &val2, &bits2))
  3465. return 0;
  3466. if (!elf_fetch_bits_backward (&pback3, pbackend3, &val3, &bits3))
  3467. return 0;
  3468. if (!elf_fetch_bits_backward (&pback4, pbackend4, &val4, &bits4))
  3469. return 0;
  3470. /* We can't subtract more than 11 bits at a time. */
  3471. do
  3472. {
  3473. t1 = huffman_table[(val1 >> (bits1 - huffman_table_bits))
  3474. & huffman_mask];
  3475. t2 = huffman_table[(val2 >> (bits2 - huffman_table_bits))
  3476. & huffman_mask];
  3477. t3 = huffman_table[(val3 >> (bits3 - huffman_table_bits))
  3478. & huffman_mask];
  3479. t4 = huffman_table[(val4 >> (bits4 - huffman_table_bits))
  3480. & huffman_mask];
  3481. *plit1 = t1 >> 8;
  3482. ++plit1;
  3483. bits1 -= t1 & 0xff;
  3484. *plit2 = t2 >> 8;
  3485. ++plit2;
  3486. bits2 -= t2 & 0xff;
  3487. *plit3 = t3 >> 8;
  3488. ++plit3;
  3489. bits3 -= t3 & 0xff;
  3490. *plit4 = t4 >> 8;
  3491. ++plit4;
  3492. bits4 -= t4 & 0xff;
  3493. ++i;
  3494. }
  3495. while (bits1 > 11 && bits2 > 11 && bits3 > 11 && bits4 > 11);
  3496. }
  3497. while (i < regenerated_stream_size)
  3498. {
  3499. int use4;
  3500. use4 = i < regenerated_stream_size4;
  3501. if (!elf_fetch_bits_backward (&pback1, pbackend1, &val1, &bits1))
  3502. return 0;
  3503. if (!elf_fetch_bits_backward (&pback2, pbackend2, &val2, &bits2))
  3504. return 0;
  3505. if (!elf_fetch_bits_backward (&pback3, pbackend3, &val3, &bits3))
  3506. return 0;
  3507. if (use4)
  3508. {
  3509. if (!elf_fetch_bits_backward (&pback4, pbackend4, &val4, &bits4))
  3510. return 0;
  3511. }
  3512. if (unlikely (bits1 < huffman_table_bits))
  3513. {
  3514. t1 = huffman_table[(val1 << (huffman_table_bits - bits1))
  3515. & huffman_mask];
  3516. if (unlikely (bits1 < (t1 & 0xff)))
  3517. {
  3518. elf_uncompress_failed ();
  3519. return 0;
  3520. }
  3521. }
  3522. else
  3523. t1 = huffman_table[(val1 >> (bits1 - huffman_table_bits))
  3524. & huffman_mask];
  3525. if (unlikely (bits2 < huffman_table_bits))
  3526. {
  3527. t2 = huffman_table[(val2 << (huffman_table_bits - bits2))
  3528. & huffman_mask];
  3529. if (unlikely (bits2 < (t2 & 0xff)))
  3530. {
  3531. elf_uncompress_failed ();
  3532. return 0;
  3533. }
  3534. }
  3535. else
  3536. t2 = huffman_table[(val2 >> (bits2 - huffman_table_bits))
  3537. & huffman_mask];
  3538. if (unlikely (bits3 < huffman_table_bits))
  3539. {
  3540. t3 = huffman_table[(val3 << (huffman_table_bits - bits3))
  3541. & huffman_mask];
  3542. if (unlikely (bits3 < (t3 & 0xff)))
  3543. {
  3544. elf_uncompress_failed ();
  3545. return 0;
  3546. }
  3547. }
  3548. else
  3549. t3 = huffman_table[(val3 >> (bits3 - huffman_table_bits))
  3550. & huffman_mask];
  3551. if (use4)
  3552. {
  3553. if (unlikely (bits4 < huffman_table_bits))
  3554. {
  3555. t4 = huffman_table[(val4 << (huffman_table_bits - bits4))
  3556. & huffman_mask];
  3557. if (unlikely (bits4 < (t4 & 0xff)))
  3558. {
  3559. elf_uncompress_failed ();
  3560. return 0;
  3561. }
  3562. }
  3563. else
  3564. t4 = huffman_table[(val4 >> (bits4 - huffman_table_bits))
  3565. & huffman_mask];
  3566. *plit4 = t4 >> 8;
  3567. ++plit4;
  3568. bits4 -= t4 & 0xff;
  3569. }
  3570. *plit1 = t1 >> 8;
  3571. ++plit1;
  3572. bits1 -= t1 & 0xff;
  3573. *plit2 = t2 >> 8;
  3574. ++plit2;
  3575. bits2 -= t2 & 0xff;
  3576. *plit3 = t3 >> 8;
  3577. ++plit3;
  3578. bits3 -= t3 & 0xff;
  3579. ++i;
  3580. }
  3581. }
  3582. return 1;
  3583. }
  3584. /* The information used to decompress a sequence code, which can be a literal
  3585. length, an offset, or a match length. */
  3586. struct elf_zstd_seq_decode
  3587. {
  3588. const struct elf_zstd_fse_baseline_entry *table;
  3589. int table_bits;
  3590. };
  3591. /* Unpack a sequence code compression mode. */
  3592. static int
  3593. elf_zstd_unpack_seq_decode (int mode,
  3594. const unsigned char **ppin,
  3595. const unsigned char *pinend,
  3596. const struct elf_zstd_fse_baseline_entry *predef,
  3597. int predef_bits,
  3598. uint16_t *scratch,
  3599. int maxidx,
  3600. struct elf_zstd_fse_baseline_entry *table,
  3601. int table_bits,
  3602. int (*conv)(const struct elf_zstd_fse_entry *,
  3603. int,
  3604. struct elf_zstd_fse_baseline_entry *),
  3605. struct elf_zstd_seq_decode *decode)
  3606. {
  3607. switch (mode)
  3608. {
  3609. case 0:
  3610. decode->table = predef;
  3611. decode->table_bits = predef_bits;
  3612. break;
  3613. case 1:
  3614. {
  3615. struct elf_zstd_fse_entry entry;
  3616. if (unlikely (*ppin >= pinend))
  3617. {
  3618. elf_uncompress_failed ();
  3619. return 0;
  3620. }
  3621. entry.symbol = **ppin;
  3622. ++*ppin;
  3623. entry.bits = 0;
  3624. entry.base = 0;
  3625. decode->table_bits = 0;
  3626. if (!conv (&entry, 0, table))
  3627. return 0;
  3628. }
  3629. break;
  3630. case 2:
  3631. {
  3632. struct elf_zstd_fse_entry *fse_table;
  3633. /* We use the same space for the simple FSE table and the baseline
  3634. table. */
  3635. fse_table = (struct elf_zstd_fse_entry *)table;
  3636. decode->table_bits = table_bits;
  3637. if (!elf_zstd_read_fse (ppin, pinend, scratch, maxidx, fse_table,
  3638. &decode->table_bits))
  3639. return 0;
  3640. if (!conv (fse_table, decode->table_bits, table))
  3641. return 0;
  3642. decode->table = table;
  3643. }
  3644. break;
  3645. case 3:
  3646. if (unlikely (decode->table_bits == -1))
  3647. {
  3648. elf_uncompress_failed ();
  3649. return 0;
  3650. }
  3651. break;
  3652. default:
  3653. elf_uncompress_failed ();
  3654. return 0;
  3655. }
  3656. return 1;
  3657. }
  3658. /* Decompress a zstd stream from PIN/SIN to POUT/SOUT. Code based on RFC 8878.
  3659. Return 1 on success, 0 on error. */
  3660. static int
  3661. elf_zstd_decompress (const unsigned char *pin, size_t sin,
  3662. unsigned char *zdebug_table, unsigned char *pout,
  3663. size_t sout)
  3664. {
  3665. const unsigned char *pinend;
  3666. unsigned char *poutstart;
  3667. unsigned char *poutend;
  3668. struct elf_zstd_seq_decode literal_decode;
  3669. struct elf_zstd_fse_baseline_entry *literal_fse_table;
  3670. struct elf_zstd_seq_decode match_decode;
  3671. struct elf_zstd_fse_baseline_entry *match_fse_table;
  3672. struct elf_zstd_seq_decode offset_decode;
  3673. struct elf_zstd_fse_baseline_entry *offset_fse_table;
  3674. uint16_t *huffman_table;
  3675. int huffman_table_bits;
  3676. uint32_t repeated_offset1;
  3677. uint32_t repeated_offset2;
  3678. uint32_t repeated_offset3;
  3679. uint16_t *scratch;
  3680. unsigned char hdr;
  3681. int has_checksum;
  3682. uint64_t content_size;
  3683. int last_block;
  3684. pinend = pin + sin;
  3685. poutstart = pout;
  3686. poutend = pout + sout;
  3687. literal_decode.table = NULL;
  3688. literal_decode.table_bits = -1;
  3689. literal_fse_table = ((struct elf_zstd_fse_baseline_entry *)
  3690. (zdebug_table + ZSTD_TABLE_LITERAL_FSE_OFFSET));
  3691. match_decode.table = NULL;
  3692. match_decode.table_bits = -1;
  3693. match_fse_table = ((struct elf_zstd_fse_baseline_entry *)
  3694. (zdebug_table + ZSTD_TABLE_MATCH_FSE_OFFSET));
  3695. offset_decode.table = NULL;
  3696. offset_decode.table_bits = -1;
  3697. offset_fse_table = ((struct elf_zstd_fse_baseline_entry *)
  3698. (zdebug_table + ZSTD_TABLE_OFFSET_FSE_OFFSET));
  3699. huffman_table = ((uint16_t *)
  3700. (zdebug_table + ZSTD_TABLE_HUFFMAN_OFFSET));
  3701. huffman_table_bits = 0;
  3702. scratch = ((uint16_t *)
  3703. (zdebug_table + ZSTD_TABLE_WORK_OFFSET));
  3704. repeated_offset1 = 1;
  3705. repeated_offset2 = 4;
  3706. repeated_offset3 = 8;
  3707. if (unlikely (sin < 4))
  3708. {
  3709. elf_uncompress_failed ();
  3710. return 0;
  3711. }
  3712. /* These values are the zstd magic number. */
  3713. if (unlikely (pin[0] != 0x28
  3714. || pin[1] != 0xb5
  3715. || pin[2] != 0x2f
  3716. || pin[3] != 0xfd))
  3717. {
  3718. elf_uncompress_failed ();
  3719. return 0;
  3720. }
  3721. pin += 4;
  3722. if (unlikely (pin >= pinend))
  3723. {
  3724. elf_uncompress_failed ();
  3725. return 0;
  3726. }
  3727. hdr = *pin++;
  3728. /* We expect a single frame. */
  3729. if (unlikely ((hdr & (1 << 5)) == 0))
  3730. {
  3731. elf_uncompress_failed ();
  3732. return 0;
  3733. }
  3734. /* Reserved bit must be zero. */
  3735. if (unlikely ((hdr & (1 << 3)) != 0))
  3736. {
  3737. elf_uncompress_failed ();
  3738. return 0;
  3739. }
  3740. /* We do not expect a dictionary. */
  3741. if (unlikely ((hdr & 3) != 0))
  3742. {
  3743. elf_uncompress_failed ();
  3744. return 0;
  3745. }
  3746. has_checksum = (hdr & (1 << 2)) != 0;
  3747. switch (hdr >> 6)
  3748. {
  3749. case 0:
  3750. if (unlikely (pin >= pinend))
  3751. {
  3752. elf_uncompress_failed ();
  3753. return 0;
  3754. }
  3755. content_size = (uint64_t) *pin++;
  3756. break;
  3757. case 1:
  3758. if (unlikely (pin + 1 >= pinend))
  3759. {
  3760. elf_uncompress_failed ();
  3761. return 0;
  3762. }
  3763. content_size = (((uint64_t) pin[0]) | (((uint64_t) pin[1]) << 8)) + 256;
  3764. pin += 2;
  3765. break;
  3766. case 2:
  3767. if (unlikely (pin + 3 >= pinend))
  3768. {
  3769. elf_uncompress_failed ();
  3770. return 0;
  3771. }
  3772. content_size = ((uint64_t) pin[0]
  3773. | (((uint64_t) pin[1]) << 8)
  3774. | (((uint64_t) pin[2]) << 16)
  3775. | (((uint64_t) pin[3]) << 24));
  3776. pin += 4;
  3777. break;
  3778. case 3:
  3779. if (unlikely (pin + 7 >= pinend))
  3780. {
  3781. elf_uncompress_failed ();
  3782. return 0;
  3783. }
  3784. content_size = ((uint64_t) pin[0]
  3785. | (((uint64_t) pin[1]) << 8)
  3786. | (((uint64_t) pin[2]) << 16)
  3787. | (((uint64_t) pin[3]) << 24)
  3788. | (((uint64_t) pin[4]) << 32)
  3789. | (((uint64_t) pin[5]) << 40)
  3790. | (((uint64_t) pin[6]) << 48)
  3791. | (((uint64_t) pin[7]) << 56));
  3792. pin += 8;
  3793. break;
  3794. default:
  3795. elf_uncompress_failed ();
  3796. return 0;
  3797. }
  3798. if (unlikely (content_size != (size_t) content_size
  3799. || (size_t) content_size != sout))
  3800. {
  3801. elf_uncompress_failed ();
  3802. return 0;
  3803. }
  3804. last_block = 0;
  3805. while (!last_block)
  3806. {
  3807. uint32_t block_hdr;
  3808. int block_type;
  3809. uint32_t block_size;
  3810. if (unlikely (pin + 2 >= pinend))
  3811. {
  3812. elf_uncompress_failed ();
  3813. return 0;
  3814. }
  3815. block_hdr = ((uint32_t) pin[0]
  3816. | (((uint32_t) pin[1]) << 8)
  3817. | (((uint32_t) pin[2]) << 16));
  3818. pin += 3;
  3819. last_block = block_hdr & 1;
  3820. block_type = (block_hdr >> 1) & 3;
  3821. block_size = block_hdr >> 3;
  3822. switch (block_type)
  3823. {
  3824. case 0:
  3825. /* Raw_Block */
  3826. if (unlikely ((size_t) block_size > (size_t) (pinend - pin)))
  3827. {
  3828. elf_uncompress_failed ();
  3829. return 0;
  3830. }
  3831. if (unlikely ((size_t) block_size > (size_t) (poutend - pout)))
  3832. {
  3833. elf_uncompress_failed ();
  3834. return 0;
  3835. }
  3836. memcpy (pout, pin, block_size);
  3837. pout += block_size;
  3838. pin += block_size;
  3839. break;
  3840. case 1:
  3841. /* RLE_Block */
  3842. if (unlikely (pin >= pinend))
  3843. {
  3844. elf_uncompress_failed ();
  3845. return 0;
  3846. }
  3847. if (unlikely ((size_t) block_size > (size_t) (poutend - pout)))
  3848. {
  3849. elf_uncompress_failed ();
  3850. return 0;
  3851. }
  3852. memset (pout, *pin, block_size);
  3853. pout += block_size;
  3854. pin++;
  3855. break;
  3856. case 2:
  3857. {
  3858. const unsigned char *pblockend;
  3859. unsigned char *plitstack;
  3860. unsigned char *plit;
  3861. uint32_t literal_count;
  3862. unsigned char seq_hdr;
  3863. size_t seq_count;
  3864. size_t seq;
  3865. const unsigned char *pback;
  3866. uint64_t val;
  3867. unsigned int bits;
  3868. unsigned int literal_state;
  3869. unsigned int offset_state;
  3870. unsigned int match_state;
  3871. /* Compressed_Block */
  3872. if (unlikely ((size_t) block_size > (size_t) (pinend - pin)))
  3873. {
  3874. elf_uncompress_failed ();
  3875. return 0;
  3876. }
  3877. pblockend = pin + block_size;
  3878. /* Read the literals into the end of the output space, and leave
  3879. PLIT pointing at them. */
  3880. if (!elf_zstd_read_literals (&pin, pblockend, pout, poutend,
  3881. scratch, huffman_table,
  3882. &huffman_table_bits,
  3883. &plitstack))
  3884. return 0;
  3885. plit = plitstack;
  3886. literal_count = poutend - plit;
  3887. seq_hdr = *pin;
  3888. pin++;
  3889. if (seq_hdr < 128)
  3890. seq_count = seq_hdr;
  3891. else if (seq_hdr < 255)
  3892. {
  3893. if (unlikely (pin >= pinend))
  3894. {
  3895. elf_uncompress_failed ();
  3896. return 0;
  3897. }
  3898. seq_count = ((seq_hdr - 128) << 8) + *pin;
  3899. pin++;
  3900. }
  3901. else
  3902. {
  3903. if (unlikely (pin + 1 >= pinend))
  3904. {
  3905. elf_uncompress_failed ();
  3906. return 0;
  3907. }
  3908. seq_count = *pin + (pin[1] << 8) + 0x7f00;
  3909. pin += 2;
  3910. }
  3911. if (seq_count > 0)
  3912. {
  3913. int (*pfn)(const struct elf_zstd_fse_entry *,
  3914. int, struct elf_zstd_fse_baseline_entry *);
  3915. if (unlikely (pin >= pinend))
  3916. {
  3917. elf_uncompress_failed ();
  3918. return 0;
  3919. }
  3920. seq_hdr = *pin;
  3921. ++pin;
  3922. pfn = elf_zstd_make_literal_baseline_fse;
  3923. if (!elf_zstd_unpack_seq_decode ((seq_hdr >> 6) & 3,
  3924. &pin, pinend,
  3925. &elf_zstd_lit_table[0], 6,
  3926. scratch, 35,
  3927. literal_fse_table, 9, pfn,
  3928. &literal_decode))
  3929. return 0;
  3930. pfn = elf_zstd_make_offset_baseline_fse;
  3931. if (!elf_zstd_unpack_seq_decode ((seq_hdr >> 4) & 3,
  3932. &pin, pinend,
  3933. &elf_zstd_offset_table[0], 5,
  3934. scratch, 31,
  3935. offset_fse_table, 8, pfn,
  3936. &offset_decode))
  3937. return 0;
  3938. pfn = elf_zstd_make_match_baseline_fse;
  3939. if (!elf_zstd_unpack_seq_decode ((seq_hdr >> 2) & 3,
  3940. &pin, pinend,
  3941. &elf_zstd_match_table[0], 6,
  3942. scratch, 52,
  3943. match_fse_table, 9, pfn,
  3944. &match_decode))
  3945. return 0;
  3946. }
  3947. pback = pblockend - 1;
  3948. if (!elf_fetch_backward_init (&pback, pin, &val, &bits))
  3949. return 0;
  3950. bits -= literal_decode.table_bits;
  3951. literal_state = ((val >> bits)
  3952. & ((1U << literal_decode.table_bits) - 1));
  3953. if (!elf_fetch_bits_backward (&pback, pin, &val, &bits))
  3954. return 0;
  3955. bits -= offset_decode.table_bits;
  3956. offset_state = ((val >> bits)
  3957. & ((1U << offset_decode.table_bits) - 1));
  3958. if (!elf_fetch_bits_backward (&pback, pin, &val, &bits))
  3959. return 0;
  3960. bits -= match_decode.table_bits;
  3961. match_state = ((val >> bits)
  3962. & ((1U << match_decode.table_bits) - 1));
  3963. seq = 0;
  3964. while (1)
  3965. {
  3966. const struct elf_zstd_fse_baseline_entry *pt;
  3967. uint32_t offset_basebits;
  3968. uint32_t offset_baseline;
  3969. uint32_t offset_bits;
  3970. uint32_t offset_base;
  3971. uint32_t offset;
  3972. uint32_t match_baseline;
  3973. uint32_t match_bits;
  3974. uint32_t match_base;
  3975. uint32_t match;
  3976. uint32_t literal_baseline;
  3977. uint32_t literal_bits;
  3978. uint32_t literal_base;
  3979. uint32_t literal;
  3980. uint32_t need;
  3981. uint32_t add;
  3982. pt = &offset_decode.table[offset_state];
  3983. offset_basebits = pt->basebits;
  3984. offset_baseline = pt->baseline;
  3985. offset_bits = pt->bits;
  3986. offset_base = pt->base;
  3987. /* This case can be more than 16 bits, which is all that
  3988. elf_fetch_bits_backward promises. */
  3989. need = offset_basebits;
  3990. add = 0;
  3991. if (unlikely (need > 16))
  3992. {
  3993. if (!elf_fetch_bits_backward (&pback, pin, &val, &bits))
  3994. return 0;
  3995. bits -= 16;
  3996. add = (val >> bits) & ((1U << 16) - 1);
  3997. need -= 16;
  3998. add <<= need;
  3999. }
  4000. if (need > 0)
  4001. {
  4002. if (!elf_fetch_bits_backward (&pback, pin, &val, &bits))
  4003. return 0;
  4004. bits -= need;
  4005. add += (val >> bits) & ((1U << need) - 1);
  4006. }
  4007. offset = offset_baseline + add;
  4008. pt = &match_decode.table[match_state];
  4009. need = pt->basebits;
  4010. match_baseline = pt->baseline;
  4011. match_bits = pt->bits;
  4012. match_base = pt->base;
  4013. add = 0;
  4014. if (need > 0)
  4015. {
  4016. if (!elf_fetch_bits_backward (&pback, pin, &val, &bits))
  4017. return 0;
  4018. bits -= need;
  4019. add = (val >> bits) & ((1U << need) - 1);
  4020. }
  4021. match = match_baseline + add;
  4022. pt = &literal_decode.table[literal_state];
  4023. need = pt->basebits;
  4024. literal_baseline = pt->baseline;
  4025. literal_bits = pt->bits;
  4026. literal_base = pt->base;
  4027. add = 0;
  4028. if (need > 0)
  4029. {
  4030. if (!elf_fetch_bits_backward (&pback, pin, &val, &bits))
  4031. return 0;
  4032. bits -= need;
  4033. add = (val >> bits) & ((1U << need) - 1);
  4034. }
  4035. literal = literal_baseline + add;
  4036. /* See the comment in elf_zstd_make_offset_baseline_fse. */
  4037. if (offset_basebits > 1)
  4038. {
  4039. repeated_offset3 = repeated_offset2;
  4040. repeated_offset2 = repeated_offset1;
  4041. repeated_offset1 = offset;
  4042. }
  4043. else
  4044. {
  4045. if (unlikely (literal == 0))
  4046. ++offset;
  4047. switch (offset)
  4048. {
  4049. case 1:
  4050. offset = repeated_offset1;
  4051. break;
  4052. case 2:
  4053. offset = repeated_offset2;
  4054. repeated_offset2 = repeated_offset1;
  4055. repeated_offset1 = offset;
  4056. break;
  4057. case 3:
  4058. offset = repeated_offset3;
  4059. repeated_offset3 = repeated_offset2;
  4060. repeated_offset2 = repeated_offset1;
  4061. repeated_offset1 = offset;
  4062. break;
  4063. case 4:
  4064. offset = repeated_offset1 - 1;
  4065. repeated_offset3 = repeated_offset2;
  4066. repeated_offset2 = repeated_offset1;
  4067. repeated_offset1 = offset;
  4068. break;
  4069. }
  4070. }
  4071. ++seq;
  4072. if (seq < seq_count)
  4073. {
  4074. uint32_t v;
  4075. /* Update the three states. */
  4076. if (!elf_fetch_bits_backward (&pback, pin, &val, &bits))
  4077. return 0;
  4078. need = literal_bits;
  4079. bits -= need;
  4080. v = (val >> bits) & (((uint32_t)1 << need) - 1);
  4081. literal_state = literal_base + v;
  4082. if (!elf_fetch_bits_backward (&pback, pin, &val, &bits))
  4083. return 0;
  4084. need = match_bits;
  4085. bits -= need;
  4086. v = (val >> bits) & (((uint32_t)1 << need) - 1);
  4087. match_state = match_base + v;
  4088. if (!elf_fetch_bits_backward (&pback, pin, &val, &bits))
  4089. return 0;
  4090. need = offset_bits;
  4091. bits -= need;
  4092. v = (val >> bits) & (((uint32_t)1 << need) - 1);
  4093. offset_state = offset_base + v;
  4094. }
  4095. /* The next sequence is now in LITERAL, OFFSET, MATCH. */
  4096. /* Copy LITERAL bytes from the literals. */
  4097. if (unlikely ((size_t)(poutend - pout) < literal))
  4098. {
  4099. elf_uncompress_failed ();
  4100. return 0;
  4101. }
  4102. if (unlikely (literal_count < literal))
  4103. {
  4104. elf_uncompress_failed ();
  4105. return 0;
  4106. }
  4107. literal_count -= literal;
  4108. /* Often LITERAL is small, so handle small cases quickly. */
  4109. switch (literal)
  4110. {
  4111. case 8:
  4112. *pout++ = *plit++;
  4113. ATTRIBUTE_FALLTHROUGH;
  4114. case 7:
  4115. *pout++ = *plit++;
  4116. ATTRIBUTE_FALLTHROUGH;
  4117. case 6:
  4118. *pout++ = *plit++;
  4119. ATTRIBUTE_FALLTHROUGH;
  4120. case 5:
  4121. *pout++ = *plit++;
  4122. ATTRIBUTE_FALLTHROUGH;
  4123. case 4:
  4124. *pout++ = *plit++;
  4125. ATTRIBUTE_FALLTHROUGH;
  4126. case 3:
  4127. *pout++ = *plit++;
  4128. ATTRIBUTE_FALLTHROUGH;
  4129. case 2:
  4130. *pout++ = *plit++;
  4131. ATTRIBUTE_FALLTHROUGH;
  4132. case 1:
  4133. *pout++ = *plit++;
  4134. break;
  4135. case 0:
  4136. break;
  4137. default:
  4138. if (unlikely ((size_t)(plit - pout) < literal))
  4139. {
  4140. uint32_t move;
  4141. move = plit - pout;
  4142. while (literal > move)
  4143. {
  4144. memcpy (pout, plit, move);
  4145. pout += move;
  4146. plit += move;
  4147. literal -= move;
  4148. }
  4149. }
  4150. memcpy (pout, plit, literal);
  4151. pout += literal;
  4152. plit += literal;
  4153. }
  4154. if (match > 0)
  4155. {
  4156. /* Copy MATCH bytes from the decoded output at OFFSET. */
  4157. if (unlikely ((size_t)(poutend - pout) < match))
  4158. {
  4159. elf_uncompress_failed ();
  4160. return 0;
  4161. }
  4162. if (unlikely ((size_t)(pout - poutstart) < offset))
  4163. {
  4164. elf_uncompress_failed ();
  4165. return 0;
  4166. }
  4167. if (offset >= match)
  4168. {
  4169. memcpy (pout, pout - offset, match);
  4170. pout += match;
  4171. }
  4172. else
  4173. {
  4174. while (match > 0)
  4175. {
  4176. uint32_t copy;
  4177. copy = match < offset ? match : offset;
  4178. memcpy (pout, pout - offset, copy);
  4179. match -= copy;
  4180. pout += copy;
  4181. }
  4182. }
  4183. }
  4184. if (unlikely (seq >= seq_count))
  4185. {
  4186. /* Copy remaining literals. */
  4187. if (literal_count > 0 && plit != pout)
  4188. {
  4189. if (unlikely ((size_t)(poutend - pout)
  4190. < literal_count))
  4191. {
  4192. elf_uncompress_failed ();
  4193. return 0;
  4194. }
  4195. if ((size_t)(plit - pout) < literal_count)
  4196. {
  4197. uint32_t move;
  4198. move = plit - pout;
  4199. while (literal_count > move)
  4200. {
  4201. memcpy (pout, plit, move);
  4202. pout += move;
  4203. plit += move;
  4204. literal_count -= move;
  4205. }
  4206. }
  4207. memcpy (pout, plit, literal_count);
  4208. }
  4209. pout += literal_count;
  4210. break;
  4211. }
  4212. }
  4213. pin = pblockend;
  4214. }
  4215. break;
  4216. case 3:
  4217. default:
  4218. elf_uncompress_failed ();
  4219. return 0;
  4220. }
  4221. }
  4222. if (has_checksum)
  4223. {
  4224. if (unlikely (pin + 4 > pinend))
  4225. {
  4226. elf_uncompress_failed ();
  4227. return 0;
  4228. }
  4229. /* We don't currently verify the checksum. Currently running GNU ld with
  4230. --compress-debug-sections=zstd does not seem to generate a
  4231. checksum. */
  4232. pin += 4;
  4233. }
  4234. if (pin != pinend)
  4235. {
  4236. elf_uncompress_failed ();
  4237. return 0;
  4238. }
  4239. return 1;
  4240. }
  4241. #define ZDEBUG_TABLE_SIZE \
  4242. (ZLIB_TABLE_SIZE > ZSTD_TABLE_SIZE ? ZLIB_TABLE_SIZE : ZSTD_TABLE_SIZE)
  4243. /* Uncompress the old compressed debug format, the one emitted by
  4244. --compress-debug-sections=zlib-gnu. The compressed data is in
  4245. COMPRESSED / COMPRESSED_SIZE, and the function writes to
  4246. *UNCOMPRESSED / *UNCOMPRESSED_SIZE. ZDEBUG_TABLE is work space to
  4247. hold Huffman tables. Returns 0 on error, 1 on successful
  4248. decompression or if something goes wrong. In general we try to
  4249. carry on, by returning 1, even if we can't decompress. */
  4250. static int
  4251. elf_uncompress_zdebug (struct backtrace_state *state,
  4252. const unsigned char *compressed, size_t compressed_size,
  4253. uint16_t *zdebug_table,
  4254. backtrace_error_callback error_callback, void *data,
  4255. unsigned char **uncompressed, size_t *uncompressed_size)
  4256. {
  4257. size_t sz;
  4258. size_t i;
  4259. unsigned char *po;
  4260. *uncompressed = NULL;
  4261. *uncompressed_size = 0;
  4262. /* The format starts with the four bytes ZLIB, followed by the 8
  4263. byte length of the uncompressed data in big-endian order,
  4264. followed by a zlib stream. */
  4265. if (compressed_size < 12 || memcmp (compressed, "ZLIB", 4) != 0)
  4266. return 1;
  4267. sz = 0;
  4268. for (i = 0; i < 8; i++)
  4269. sz = (sz << 8) | compressed[i + 4];
  4270. if (*uncompressed != NULL && *uncompressed_size >= sz)
  4271. po = *uncompressed;
  4272. else
  4273. {
  4274. po = (unsigned char *) backtrace_alloc (state, sz, error_callback, data);
  4275. if (po == NULL)
  4276. return 0;
  4277. }
  4278. if (!elf_zlib_inflate_and_verify (compressed + 12, compressed_size - 12,
  4279. zdebug_table, po, sz))
  4280. return 1;
  4281. *uncompressed = po;
  4282. *uncompressed_size = sz;
  4283. return 1;
  4284. }
  4285. /* Uncompress the new compressed debug format, the official standard
  4286. ELF approach emitted by --compress-debug-sections=zlib-gabi. The
  4287. compressed data is in COMPRESSED / COMPRESSED_SIZE, and the
  4288. function writes to *UNCOMPRESSED / *UNCOMPRESSED_SIZE.
  4289. ZDEBUG_TABLE is work space as for elf_uncompress_zdebug. Returns 0
  4290. on error, 1 on successful decompression or if something goes wrong.
  4291. In general we try to carry on, by returning 1, even if we can't
  4292. decompress. */
  4293. static int
  4294. elf_uncompress_chdr (struct backtrace_state *state,
  4295. const unsigned char *compressed, size_t compressed_size,
  4296. uint16_t *zdebug_table,
  4297. backtrace_error_callback error_callback, void *data,
  4298. unsigned char **uncompressed, size_t *uncompressed_size)
  4299. {
  4300. b_elf_chdr chdr;
  4301. char *alc;
  4302. size_t alc_len;
  4303. unsigned char *po;
  4304. *uncompressed = NULL;
  4305. *uncompressed_size = 0;
  4306. /* The format starts with an ELF compression header. */
  4307. if (compressed_size < sizeof (b_elf_chdr))
  4308. return 1;
  4309. /* The lld linker can misalign a compressed section, so we can't safely read
  4310. the fields directly as we can for other ELF sections. See
  4311. https://github.com/ianlancetaylor/libbacktrace/pull/120. */
  4312. memcpy (&chdr, compressed, sizeof (b_elf_chdr));
  4313. alc = NULL;
  4314. alc_len = 0;
  4315. if (*uncompressed != NULL && *uncompressed_size >= chdr.ch_size)
  4316. po = *uncompressed;
  4317. else
  4318. {
  4319. alc_len = chdr.ch_size;
  4320. alc = backtrace_alloc (state, alc_len, error_callback, data);
  4321. if (alc == NULL)
  4322. return 0;
  4323. po = (unsigned char *) alc;
  4324. }
  4325. switch (chdr.ch_type)
  4326. {
  4327. case ELFCOMPRESS_ZLIB:
  4328. if (!elf_zlib_inflate_and_verify (compressed + sizeof (b_elf_chdr),
  4329. compressed_size - sizeof (b_elf_chdr),
  4330. zdebug_table, po, chdr.ch_size))
  4331. goto skip;
  4332. break;
  4333. case ELFCOMPRESS_ZSTD:
  4334. if (!elf_zstd_decompress (compressed + sizeof (b_elf_chdr),
  4335. compressed_size - sizeof (b_elf_chdr),
  4336. (unsigned char *)zdebug_table, po,
  4337. chdr.ch_size))
  4338. goto skip;
  4339. break;
  4340. default:
  4341. /* Unsupported compression algorithm. */
  4342. goto skip;
  4343. }
  4344. *uncompressed = po;
  4345. *uncompressed_size = chdr.ch_size;
  4346. return 1;
  4347. skip:
  4348. if (alc != NULL && alc_len > 0)
  4349. backtrace_free (state, alc, alc_len, error_callback, data);
  4350. return 1;
  4351. }
  4352. /* This function is a hook for testing the zlib support. It is only
  4353. used by tests. */
  4354. int
  4355. backtrace_uncompress_zdebug (struct backtrace_state *state,
  4356. const unsigned char *compressed,
  4357. size_t compressed_size,
  4358. backtrace_error_callback error_callback,
  4359. void *data, unsigned char **uncompressed,
  4360. size_t *uncompressed_size)
  4361. {
  4362. uint16_t *zdebug_table;
  4363. int ret;
  4364. zdebug_table = ((uint16_t *) backtrace_alloc (state, ZDEBUG_TABLE_SIZE,
  4365. error_callback, data));
  4366. if (zdebug_table == NULL)
  4367. return 0;
  4368. ret = elf_uncompress_zdebug (state, compressed, compressed_size,
  4369. zdebug_table, error_callback, data,
  4370. uncompressed, uncompressed_size);
  4371. backtrace_free (state, zdebug_table, ZDEBUG_TABLE_SIZE,
  4372. error_callback, data);
  4373. return ret;
  4374. }
  4375. /* This function is a hook for testing the zstd support. It is only used by
  4376. tests. */
  4377. int
  4378. backtrace_uncompress_zstd (struct backtrace_state *state,
  4379. const unsigned char *compressed,
  4380. size_t compressed_size,
  4381. backtrace_error_callback error_callback,
  4382. void *data, unsigned char *uncompressed,
  4383. size_t uncompressed_size)
  4384. {
  4385. unsigned char *zdebug_table;
  4386. int ret;
  4387. zdebug_table = ((unsigned char *) backtrace_alloc (state, ZDEBUG_TABLE_SIZE,
  4388. error_callback, data));
  4389. if (zdebug_table == NULL)
  4390. return 0;
  4391. ret = elf_zstd_decompress (compressed, compressed_size,
  4392. zdebug_table, uncompressed, uncompressed_size);
  4393. backtrace_free (state, zdebug_table, ZDEBUG_TABLE_SIZE,
  4394. error_callback, data);
  4395. return ret;
  4396. }
  4397. /* Number of LZMA states. */
  4398. #define LZMA_STATES (12)
  4399. /* Number of LZMA position states. The pb value of the property byte
  4400. is the number of bits to include in these states, and the maximum
  4401. value of pb is 4. */
  4402. #define LZMA_POS_STATES (16)
  4403. /* Number of LZMA distance states. These are used match distances
  4404. with a short match length: up to 4 bytes. */
  4405. #define LZMA_DIST_STATES (4)
  4406. /* Number of LZMA distance slots. LZMA uses six bits to encode larger
  4407. match lengths, so 1 << 6 possible probabilities. */
  4408. #define LZMA_DIST_SLOTS (64)
  4409. /* LZMA distances 0 to 3 are encoded directly, larger values use a
  4410. probability model. */
  4411. #define LZMA_DIST_MODEL_START (4)
  4412. /* The LZMA probability model ends at 14. */
  4413. #define LZMA_DIST_MODEL_END (14)
  4414. /* LZMA distance slots for distances less than 127. */
  4415. #define LZMA_FULL_DISTANCES (128)
  4416. /* LZMA uses four alignment bits. */
  4417. #define LZMA_ALIGN_SIZE (16)
  4418. /* LZMA match length is encoded with 4, 5, or 10 bits, some of which
  4419. are already known. */
  4420. #define LZMA_LEN_LOW_SYMBOLS (8)
  4421. #define LZMA_LEN_MID_SYMBOLS (8)
  4422. #define LZMA_LEN_HIGH_SYMBOLS (256)
  4423. /* LZMA literal encoding. */
  4424. #define LZMA_LITERAL_CODERS_MAX (16)
  4425. #define LZMA_LITERAL_CODER_SIZE (0x300)
  4426. /* LZMA is based on a large set of probabilities, each managed
  4427. independently. Each probability is an 11 bit number that we store
  4428. in a uint16_t. We use a single large array of probabilities. */
  4429. /* Lengths of entries in the LZMA probabilities array. The names used
  4430. here are copied from the Linux kernel implementation. */
  4431. #define LZMA_PROB_IS_MATCH_LEN (LZMA_STATES * LZMA_POS_STATES)
  4432. #define LZMA_PROB_IS_REP_LEN LZMA_STATES
  4433. #define LZMA_PROB_IS_REP0_LEN LZMA_STATES
  4434. #define LZMA_PROB_IS_REP1_LEN LZMA_STATES
  4435. #define LZMA_PROB_IS_REP2_LEN LZMA_STATES
  4436. #define LZMA_PROB_IS_REP0_LONG_LEN (LZMA_STATES * LZMA_POS_STATES)
  4437. #define LZMA_PROB_DIST_SLOT_LEN (LZMA_DIST_STATES * LZMA_DIST_SLOTS)
  4438. #define LZMA_PROB_DIST_SPECIAL_LEN (LZMA_FULL_DISTANCES - LZMA_DIST_MODEL_END)
  4439. #define LZMA_PROB_DIST_ALIGN_LEN LZMA_ALIGN_SIZE
  4440. #define LZMA_PROB_MATCH_LEN_CHOICE_LEN 1
  4441. #define LZMA_PROB_MATCH_LEN_CHOICE2_LEN 1
  4442. #define LZMA_PROB_MATCH_LEN_LOW_LEN (LZMA_POS_STATES * LZMA_LEN_LOW_SYMBOLS)
  4443. #define LZMA_PROB_MATCH_LEN_MID_LEN (LZMA_POS_STATES * LZMA_LEN_MID_SYMBOLS)
  4444. #define LZMA_PROB_MATCH_LEN_HIGH_LEN LZMA_LEN_HIGH_SYMBOLS
  4445. #define LZMA_PROB_REP_LEN_CHOICE_LEN 1
  4446. #define LZMA_PROB_REP_LEN_CHOICE2_LEN 1
  4447. #define LZMA_PROB_REP_LEN_LOW_LEN (LZMA_POS_STATES * LZMA_LEN_LOW_SYMBOLS)
  4448. #define LZMA_PROB_REP_LEN_MID_LEN (LZMA_POS_STATES * LZMA_LEN_MID_SYMBOLS)
  4449. #define LZMA_PROB_REP_LEN_HIGH_LEN LZMA_LEN_HIGH_SYMBOLS
  4450. #define LZMA_PROB_LITERAL_LEN \
  4451. (LZMA_LITERAL_CODERS_MAX * LZMA_LITERAL_CODER_SIZE)
  4452. /* Offsets into the LZMA probabilities array. This is mechanically
  4453. generated from the above lengths. */
  4454. #define LZMA_PROB_IS_MATCH_OFFSET 0
  4455. #define LZMA_PROB_IS_REP_OFFSET \
  4456. (LZMA_PROB_IS_MATCH_OFFSET + LZMA_PROB_IS_MATCH_LEN)
  4457. #define LZMA_PROB_IS_REP0_OFFSET \
  4458. (LZMA_PROB_IS_REP_OFFSET + LZMA_PROB_IS_REP_LEN)
  4459. #define LZMA_PROB_IS_REP1_OFFSET \
  4460. (LZMA_PROB_IS_REP0_OFFSET + LZMA_PROB_IS_REP0_LEN)
  4461. #define LZMA_PROB_IS_REP2_OFFSET \
  4462. (LZMA_PROB_IS_REP1_OFFSET + LZMA_PROB_IS_REP1_LEN)
  4463. #define LZMA_PROB_IS_REP0_LONG_OFFSET \
  4464. (LZMA_PROB_IS_REP2_OFFSET + LZMA_PROB_IS_REP2_LEN)
  4465. #define LZMA_PROB_DIST_SLOT_OFFSET \
  4466. (LZMA_PROB_IS_REP0_LONG_OFFSET + LZMA_PROB_IS_REP0_LONG_LEN)
  4467. #define LZMA_PROB_DIST_SPECIAL_OFFSET \
  4468. (LZMA_PROB_DIST_SLOT_OFFSET + LZMA_PROB_DIST_SLOT_LEN)
  4469. #define LZMA_PROB_DIST_ALIGN_OFFSET \
  4470. (LZMA_PROB_DIST_SPECIAL_OFFSET + LZMA_PROB_DIST_SPECIAL_LEN)
  4471. #define LZMA_PROB_MATCH_LEN_CHOICE_OFFSET \
  4472. (LZMA_PROB_DIST_ALIGN_OFFSET + LZMA_PROB_DIST_ALIGN_LEN)
  4473. #define LZMA_PROB_MATCH_LEN_CHOICE2_OFFSET \
  4474. (LZMA_PROB_MATCH_LEN_CHOICE_OFFSET + LZMA_PROB_MATCH_LEN_CHOICE_LEN)
  4475. #define LZMA_PROB_MATCH_LEN_LOW_OFFSET \
  4476. (LZMA_PROB_MATCH_LEN_CHOICE2_OFFSET + LZMA_PROB_MATCH_LEN_CHOICE2_LEN)
  4477. #define LZMA_PROB_MATCH_LEN_MID_OFFSET \
  4478. (LZMA_PROB_MATCH_LEN_LOW_OFFSET + LZMA_PROB_MATCH_LEN_LOW_LEN)
  4479. #define LZMA_PROB_MATCH_LEN_HIGH_OFFSET \
  4480. (LZMA_PROB_MATCH_LEN_MID_OFFSET + LZMA_PROB_MATCH_LEN_MID_LEN)
  4481. #define LZMA_PROB_REP_LEN_CHOICE_OFFSET \
  4482. (LZMA_PROB_MATCH_LEN_HIGH_OFFSET + LZMA_PROB_MATCH_LEN_HIGH_LEN)
  4483. #define LZMA_PROB_REP_LEN_CHOICE2_OFFSET \
  4484. (LZMA_PROB_REP_LEN_CHOICE_OFFSET + LZMA_PROB_REP_LEN_CHOICE_LEN)
  4485. #define LZMA_PROB_REP_LEN_LOW_OFFSET \
  4486. (LZMA_PROB_REP_LEN_CHOICE2_OFFSET + LZMA_PROB_REP_LEN_CHOICE2_LEN)
  4487. #define LZMA_PROB_REP_LEN_MID_OFFSET \
  4488. (LZMA_PROB_REP_LEN_LOW_OFFSET + LZMA_PROB_REP_LEN_LOW_LEN)
  4489. #define LZMA_PROB_REP_LEN_HIGH_OFFSET \
  4490. (LZMA_PROB_REP_LEN_MID_OFFSET + LZMA_PROB_REP_LEN_MID_LEN)
  4491. #define LZMA_PROB_LITERAL_OFFSET \
  4492. (LZMA_PROB_REP_LEN_HIGH_OFFSET + LZMA_PROB_REP_LEN_HIGH_LEN)
  4493. #define LZMA_PROB_TOTAL_COUNT \
  4494. (LZMA_PROB_LITERAL_OFFSET + LZMA_PROB_LITERAL_LEN)
  4495. /* Check that the number of LZMA probabilities is the same as the
  4496. Linux kernel implementation. */
  4497. #if LZMA_PROB_TOTAL_COUNT != 1846 + (1 << 4) * 0x300
  4498. #error Wrong number of LZMA probabilities
  4499. #endif
  4500. /* Expressions for the offset in the LZMA probabilities array of a
  4501. specific probability. */
  4502. #define LZMA_IS_MATCH(state, pos) \
  4503. (LZMA_PROB_IS_MATCH_OFFSET + (state) * LZMA_POS_STATES + (pos))
  4504. #define LZMA_IS_REP(state) \
  4505. (LZMA_PROB_IS_REP_OFFSET + (state))
  4506. #define LZMA_IS_REP0(state) \
  4507. (LZMA_PROB_IS_REP0_OFFSET + (state))
  4508. #define LZMA_IS_REP1(state) \
  4509. (LZMA_PROB_IS_REP1_OFFSET + (state))
  4510. #define LZMA_IS_REP2(state) \
  4511. (LZMA_PROB_IS_REP2_OFFSET + (state))
  4512. #define LZMA_IS_REP0_LONG(state, pos) \
  4513. (LZMA_PROB_IS_REP0_LONG_OFFSET + (state) * LZMA_POS_STATES + (pos))
  4514. #define LZMA_DIST_SLOT(dist, slot) \
  4515. (LZMA_PROB_DIST_SLOT_OFFSET + (dist) * LZMA_DIST_SLOTS + (slot))
  4516. #define LZMA_DIST_SPECIAL(dist) \
  4517. (LZMA_PROB_DIST_SPECIAL_OFFSET + (dist))
  4518. #define LZMA_DIST_ALIGN(dist) \
  4519. (LZMA_PROB_DIST_ALIGN_OFFSET + (dist))
  4520. #define LZMA_MATCH_LEN_CHOICE \
  4521. LZMA_PROB_MATCH_LEN_CHOICE_OFFSET
  4522. #define LZMA_MATCH_LEN_CHOICE2 \
  4523. LZMA_PROB_MATCH_LEN_CHOICE2_OFFSET
  4524. #define LZMA_MATCH_LEN_LOW(pos, sym) \
  4525. (LZMA_PROB_MATCH_LEN_LOW_OFFSET + (pos) * LZMA_LEN_LOW_SYMBOLS + (sym))
  4526. #define LZMA_MATCH_LEN_MID(pos, sym) \
  4527. (LZMA_PROB_MATCH_LEN_MID_OFFSET + (pos) * LZMA_LEN_MID_SYMBOLS + (sym))
  4528. #define LZMA_MATCH_LEN_HIGH(sym) \
  4529. (LZMA_PROB_MATCH_LEN_HIGH_OFFSET + (sym))
  4530. #define LZMA_REP_LEN_CHOICE \
  4531. LZMA_PROB_REP_LEN_CHOICE_OFFSET
  4532. #define LZMA_REP_LEN_CHOICE2 \
  4533. LZMA_PROB_REP_LEN_CHOICE2_OFFSET
  4534. #define LZMA_REP_LEN_LOW(pos, sym) \
  4535. (LZMA_PROB_REP_LEN_LOW_OFFSET + (pos) * LZMA_LEN_LOW_SYMBOLS + (sym))
  4536. #define LZMA_REP_LEN_MID(pos, sym) \
  4537. (LZMA_PROB_REP_LEN_MID_OFFSET + (pos) * LZMA_LEN_MID_SYMBOLS + (sym))
  4538. #define LZMA_REP_LEN_HIGH(sym) \
  4539. (LZMA_PROB_REP_LEN_HIGH_OFFSET + (sym))
  4540. #define LZMA_LITERAL(code, size) \
  4541. (LZMA_PROB_LITERAL_OFFSET + (code) * LZMA_LITERAL_CODER_SIZE + (size))
  4542. /* Read an LZMA varint from BUF, reading and updating *POFFSET,
  4543. setting *VAL. Returns 0 on error, 1 on success. */
  4544. static int
  4545. elf_lzma_varint (const unsigned char *compressed, size_t compressed_size,
  4546. size_t *poffset, uint64_t *val)
  4547. {
  4548. size_t off;
  4549. int i;
  4550. uint64_t v;
  4551. unsigned char b;
  4552. off = *poffset;
  4553. i = 0;
  4554. v = 0;
  4555. while (1)
  4556. {
  4557. if (unlikely (off >= compressed_size))
  4558. {
  4559. elf_uncompress_failed ();
  4560. return 0;
  4561. }
  4562. b = compressed[off];
  4563. v |= (b & 0x7f) << (i * 7);
  4564. ++off;
  4565. if ((b & 0x80) == 0)
  4566. {
  4567. *poffset = off;
  4568. *val = v;
  4569. return 1;
  4570. }
  4571. ++i;
  4572. if (unlikely (i >= 9))
  4573. {
  4574. elf_uncompress_failed ();
  4575. return 0;
  4576. }
  4577. }
  4578. }
  4579. /* Normalize the LZMA range decoder, pulling in an extra input byte if
  4580. needed. */
  4581. static void
  4582. elf_lzma_range_normalize (const unsigned char *compressed,
  4583. size_t compressed_size, size_t *poffset,
  4584. uint32_t *prange, uint32_t *pcode)
  4585. {
  4586. if (*prange < (1U << 24))
  4587. {
  4588. if (unlikely (*poffset >= compressed_size))
  4589. {
  4590. /* We assume this will be caught elsewhere. */
  4591. elf_uncompress_failed ();
  4592. return;
  4593. }
  4594. *prange <<= 8;
  4595. *pcode <<= 8;
  4596. *pcode += compressed[*poffset];
  4597. ++*poffset;
  4598. }
  4599. }
  4600. /* Read and return a single bit from the LZMA stream, reading and
  4601. updating *PROB. Each bit comes from the range coder. */
  4602. static int
  4603. elf_lzma_bit (const unsigned char *compressed, size_t compressed_size,
  4604. uint16_t *prob, size_t *poffset, uint32_t *prange,
  4605. uint32_t *pcode)
  4606. {
  4607. uint32_t bound;
  4608. elf_lzma_range_normalize (compressed, compressed_size, poffset,
  4609. prange, pcode);
  4610. bound = (*prange >> 11) * (uint32_t) *prob;
  4611. if (*pcode < bound)
  4612. {
  4613. *prange = bound;
  4614. *prob += ((1U << 11) - *prob) >> 5;
  4615. return 0;
  4616. }
  4617. else
  4618. {
  4619. *prange -= bound;
  4620. *pcode -= bound;
  4621. *prob -= *prob >> 5;
  4622. return 1;
  4623. }
  4624. }
  4625. /* Read an integer of size BITS from the LZMA stream, most significant
  4626. bit first. The bits are predicted using PROBS. */
  4627. static uint32_t
  4628. elf_lzma_integer (const unsigned char *compressed, size_t compressed_size,
  4629. uint16_t *probs, uint32_t bits, size_t *poffset,
  4630. uint32_t *prange, uint32_t *pcode)
  4631. {
  4632. uint32_t sym;
  4633. uint32_t i;
  4634. sym = 1;
  4635. for (i = 0; i < bits; i++)
  4636. {
  4637. int bit;
  4638. bit = elf_lzma_bit (compressed, compressed_size, probs + sym, poffset,
  4639. prange, pcode);
  4640. sym <<= 1;
  4641. sym += bit;
  4642. }
  4643. return sym - (1 << bits);
  4644. }
  4645. /* Read an integer of size BITS from the LZMA stream, least
  4646. significant bit first. The bits are predicted using PROBS. */
  4647. static uint32_t
  4648. elf_lzma_reverse_integer (const unsigned char *compressed,
  4649. size_t compressed_size, uint16_t *probs,
  4650. uint32_t bits, size_t *poffset, uint32_t *prange,
  4651. uint32_t *pcode)
  4652. {
  4653. uint32_t sym;
  4654. uint32_t val;
  4655. uint32_t i;
  4656. sym = 1;
  4657. val = 0;
  4658. for (i = 0; i < bits; i++)
  4659. {
  4660. int bit;
  4661. bit = elf_lzma_bit (compressed, compressed_size, probs + sym, poffset,
  4662. prange, pcode);
  4663. sym <<= 1;
  4664. sym += bit;
  4665. val += bit << i;
  4666. }
  4667. return val;
  4668. }
  4669. /* Read a length from the LZMA stream. IS_REP picks either LZMA_MATCH
  4670. or LZMA_REP probabilities. */
  4671. static uint32_t
  4672. elf_lzma_len (const unsigned char *compressed, size_t compressed_size,
  4673. uint16_t *probs, int is_rep, unsigned int pos_state,
  4674. size_t *poffset, uint32_t *prange, uint32_t *pcode)
  4675. {
  4676. uint16_t *probs_choice;
  4677. uint16_t *probs_sym;
  4678. uint32_t bits;
  4679. uint32_t len;
  4680. probs_choice = probs + (is_rep
  4681. ? LZMA_REP_LEN_CHOICE
  4682. : LZMA_MATCH_LEN_CHOICE);
  4683. if (elf_lzma_bit (compressed, compressed_size, probs_choice, poffset,
  4684. prange, pcode))
  4685. {
  4686. probs_choice = probs + (is_rep
  4687. ? LZMA_REP_LEN_CHOICE2
  4688. : LZMA_MATCH_LEN_CHOICE2);
  4689. if (elf_lzma_bit (compressed, compressed_size, probs_choice,
  4690. poffset, prange, pcode))
  4691. {
  4692. probs_sym = probs + (is_rep
  4693. ? LZMA_REP_LEN_HIGH (0)
  4694. : LZMA_MATCH_LEN_HIGH (0));
  4695. bits = 8;
  4696. len = 2 + 8 + 8;
  4697. }
  4698. else
  4699. {
  4700. probs_sym = probs + (is_rep
  4701. ? LZMA_REP_LEN_MID (pos_state, 0)
  4702. : LZMA_MATCH_LEN_MID (pos_state, 0));
  4703. bits = 3;
  4704. len = 2 + 8;
  4705. }
  4706. }
  4707. else
  4708. {
  4709. probs_sym = probs + (is_rep
  4710. ? LZMA_REP_LEN_LOW (pos_state, 0)
  4711. : LZMA_MATCH_LEN_LOW (pos_state, 0));
  4712. bits = 3;
  4713. len = 2;
  4714. }
  4715. len += elf_lzma_integer (compressed, compressed_size, probs_sym, bits,
  4716. poffset, prange, pcode);
  4717. return len;
  4718. }
  4719. /* Uncompress one LZMA block from a minidebug file. The compressed
  4720. data is at COMPRESSED + *POFFSET. Update *POFFSET. Store the data
  4721. into the memory at UNCOMPRESSED, size UNCOMPRESSED_SIZE. CHECK is
  4722. the stream flag from the xz header. Return 1 on successful
  4723. decompression. */
  4724. static int
  4725. elf_uncompress_lzma_block (const unsigned char *compressed,
  4726. size_t compressed_size, unsigned char check,
  4727. uint16_t *probs, unsigned char *uncompressed,
  4728. size_t uncompressed_size, size_t *poffset)
  4729. {
  4730. size_t off;
  4731. size_t block_header_offset;
  4732. size_t block_header_size;
  4733. unsigned char block_flags;
  4734. uint64_t header_compressed_size;
  4735. uint64_t header_uncompressed_size;
  4736. unsigned char lzma2_properties;
  4737. size_t crc_offset;
  4738. uint32_t computed_crc;
  4739. uint32_t stream_crc;
  4740. size_t uncompressed_offset;
  4741. size_t dict_start_offset;
  4742. unsigned int lc;
  4743. unsigned int lp;
  4744. unsigned int pb;
  4745. uint32_t range;
  4746. uint32_t code;
  4747. uint32_t lstate;
  4748. uint32_t dist[4];
  4749. off = *poffset;
  4750. block_header_offset = off;
  4751. /* Block header size is a single byte. */
  4752. if (unlikely (off >= compressed_size))
  4753. {
  4754. elf_uncompress_failed ();
  4755. return 0;
  4756. }
  4757. block_header_size = (compressed[off] + 1) * 4;
  4758. if (unlikely (off + block_header_size > compressed_size))
  4759. {
  4760. elf_uncompress_failed ();
  4761. return 0;
  4762. }
  4763. /* Block flags. */
  4764. block_flags = compressed[off + 1];
  4765. if (unlikely ((block_flags & 0x3c) != 0))
  4766. {
  4767. elf_uncompress_failed ();
  4768. return 0;
  4769. }
  4770. off += 2;
  4771. /* Optional compressed size. */
  4772. header_compressed_size = 0;
  4773. if ((block_flags & 0x40) != 0)
  4774. {
  4775. *poffset = off;
  4776. if (!elf_lzma_varint (compressed, compressed_size, poffset,
  4777. &header_compressed_size))
  4778. return 0;
  4779. off = *poffset;
  4780. }
  4781. /* Optional uncompressed size. */
  4782. header_uncompressed_size = 0;
  4783. if ((block_flags & 0x80) != 0)
  4784. {
  4785. *poffset = off;
  4786. if (!elf_lzma_varint (compressed, compressed_size, poffset,
  4787. &header_uncompressed_size))
  4788. return 0;
  4789. off = *poffset;
  4790. }
  4791. /* The recipe for creating a minidebug file is to run the xz program
  4792. with no arguments, so we expect exactly one filter: lzma2. */
  4793. if (unlikely ((block_flags & 0x3) != 0))
  4794. {
  4795. elf_uncompress_failed ();
  4796. return 0;
  4797. }
  4798. if (unlikely (off + 2 >= block_header_offset + block_header_size))
  4799. {
  4800. elf_uncompress_failed ();
  4801. return 0;
  4802. }
  4803. /* The filter ID for LZMA2 is 0x21. */
  4804. if (unlikely (compressed[off] != 0x21))
  4805. {
  4806. elf_uncompress_failed ();
  4807. return 0;
  4808. }
  4809. ++off;
  4810. /* The size of the filter properties for LZMA2 is 1. */
  4811. if (unlikely (compressed[off] != 1))
  4812. {
  4813. elf_uncompress_failed ();
  4814. return 0;
  4815. }
  4816. ++off;
  4817. lzma2_properties = compressed[off];
  4818. ++off;
  4819. if (unlikely (lzma2_properties > 40))
  4820. {
  4821. elf_uncompress_failed ();
  4822. return 0;
  4823. }
  4824. /* The properties describe the dictionary size, but we don't care
  4825. what that is. */
  4826. /* Skip to just before CRC, verifying zero bytes in between. */
  4827. crc_offset = block_header_offset + block_header_size - 4;
  4828. if (unlikely (crc_offset + 4 > compressed_size))
  4829. {
  4830. elf_uncompress_failed ();
  4831. return 0;
  4832. }
  4833. for (; off < crc_offset; off++)
  4834. {
  4835. if (compressed[off] != 0)
  4836. {
  4837. elf_uncompress_failed ();
  4838. return 0;
  4839. }
  4840. }
  4841. /* Block header CRC. */
  4842. computed_crc = elf_crc32 (0, compressed + block_header_offset,
  4843. block_header_size - 4);
  4844. stream_crc = ((uint32_t)compressed[off]
  4845. | ((uint32_t)compressed[off + 1] << 8)
  4846. | ((uint32_t)compressed[off + 2] << 16)
  4847. | ((uint32_t)compressed[off + 3] << 24));
  4848. if (unlikely (computed_crc != stream_crc))
  4849. {
  4850. elf_uncompress_failed ();
  4851. return 0;
  4852. }
  4853. off += 4;
  4854. /* Read a sequence of LZMA2 packets. */
  4855. uncompressed_offset = 0;
  4856. dict_start_offset = 0;
  4857. lc = 0;
  4858. lp = 0;
  4859. pb = 0;
  4860. lstate = 0;
  4861. while (off < compressed_size)
  4862. {
  4863. unsigned char control;
  4864. range = 0xffffffff;
  4865. code = 0;
  4866. control = compressed[off];
  4867. ++off;
  4868. if (unlikely (control == 0))
  4869. {
  4870. /* End of packets. */
  4871. break;
  4872. }
  4873. if (control == 1 || control >= 0xe0)
  4874. {
  4875. /* Reset dictionary to empty. */
  4876. dict_start_offset = uncompressed_offset;
  4877. }
  4878. if (control < 0x80)
  4879. {
  4880. size_t chunk_size;
  4881. /* The only valid values here are 1 or 2. A 1 means to
  4882. reset the dictionary (done above). Then we see an
  4883. uncompressed chunk. */
  4884. if (unlikely (control > 2))
  4885. {
  4886. elf_uncompress_failed ();
  4887. return 0;
  4888. }
  4889. /* An uncompressed chunk is a two byte size followed by
  4890. data. */
  4891. if (unlikely (off + 2 > compressed_size))
  4892. {
  4893. elf_uncompress_failed ();
  4894. return 0;
  4895. }
  4896. chunk_size = compressed[off] << 8;
  4897. chunk_size += compressed[off + 1];
  4898. ++chunk_size;
  4899. off += 2;
  4900. if (unlikely (off + chunk_size > compressed_size))
  4901. {
  4902. elf_uncompress_failed ();
  4903. return 0;
  4904. }
  4905. if (unlikely (uncompressed_offset + chunk_size > uncompressed_size))
  4906. {
  4907. elf_uncompress_failed ();
  4908. return 0;
  4909. }
  4910. memcpy (uncompressed + uncompressed_offset, compressed + off,
  4911. chunk_size);
  4912. uncompressed_offset += chunk_size;
  4913. off += chunk_size;
  4914. }
  4915. else
  4916. {
  4917. size_t uncompressed_chunk_start;
  4918. size_t uncompressed_chunk_size;
  4919. size_t compressed_chunk_size;
  4920. size_t limit;
  4921. /* An LZMA chunk. This starts with an uncompressed size and
  4922. a compressed size. */
  4923. if (unlikely (off + 4 >= compressed_size))
  4924. {
  4925. elf_uncompress_failed ();
  4926. return 0;
  4927. }
  4928. uncompressed_chunk_start = uncompressed_offset;
  4929. uncompressed_chunk_size = (control & 0x1f) << 16;
  4930. uncompressed_chunk_size += compressed[off] << 8;
  4931. uncompressed_chunk_size += compressed[off + 1];
  4932. ++uncompressed_chunk_size;
  4933. compressed_chunk_size = compressed[off + 2] << 8;
  4934. compressed_chunk_size += compressed[off + 3];
  4935. ++compressed_chunk_size;
  4936. off += 4;
  4937. /* Bit 7 (0x80) is set.
  4938. Bits 6 and 5 (0x40 and 0x20) are as follows:
  4939. 0: don't reset anything
  4940. 1: reset state
  4941. 2: reset state, read properties
  4942. 3: reset state, read properties, reset dictionary (done above) */
  4943. if (control >= 0xc0)
  4944. {
  4945. unsigned char props;
  4946. /* Bit 6 is set, read properties. */
  4947. if (unlikely (off >= compressed_size))
  4948. {
  4949. elf_uncompress_failed ();
  4950. return 0;
  4951. }
  4952. props = compressed[off];
  4953. ++off;
  4954. if (unlikely (props > (4 * 5 + 4) * 9 + 8))
  4955. {
  4956. elf_uncompress_failed ();
  4957. return 0;
  4958. }
  4959. pb = 0;
  4960. while (props >= 9 * 5)
  4961. {
  4962. props -= 9 * 5;
  4963. ++pb;
  4964. }
  4965. lp = 0;
  4966. while (props > 9)
  4967. {
  4968. props -= 9;
  4969. ++lp;
  4970. }
  4971. lc = props;
  4972. if (unlikely (lc + lp > 4))
  4973. {
  4974. elf_uncompress_failed ();
  4975. return 0;
  4976. }
  4977. }
  4978. if (control >= 0xa0)
  4979. {
  4980. size_t i;
  4981. /* Bit 5 or 6 is set, reset LZMA state. */
  4982. lstate = 0;
  4983. memset (&dist, 0, sizeof dist);
  4984. for (i = 0; i < LZMA_PROB_TOTAL_COUNT; i++)
  4985. probs[i] = 1 << 10;
  4986. range = 0xffffffff;
  4987. code = 0;
  4988. }
  4989. /* Read the range code. */
  4990. if (unlikely (off + 5 > compressed_size))
  4991. {
  4992. elf_uncompress_failed ();
  4993. return 0;
  4994. }
  4995. /* The byte at compressed[off] is ignored for some
  4996. reason. */
  4997. code = ((compressed[off + 1] << 24)
  4998. + (compressed[off + 2] << 16)
  4999. + (compressed[off + 3] << 8)
  5000. + compressed[off + 4]);
  5001. off += 5;
  5002. /* This is the main LZMA decode loop. */
  5003. limit = off + compressed_chunk_size;
  5004. *poffset = off;
  5005. while (*poffset < limit)
  5006. {
  5007. unsigned int pos_state;
  5008. if (unlikely (uncompressed_offset
  5009. == (uncompressed_chunk_start
  5010. + uncompressed_chunk_size)))
  5011. {
  5012. /* We've decompressed all the expected bytes. */
  5013. break;
  5014. }
  5015. pos_state = ((uncompressed_offset - dict_start_offset)
  5016. & ((1 << pb) - 1));
  5017. if (elf_lzma_bit (compressed, compressed_size,
  5018. probs + LZMA_IS_MATCH (lstate, pos_state),
  5019. poffset, &range, &code))
  5020. {
  5021. uint32_t len;
  5022. if (elf_lzma_bit (compressed, compressed_size,
  5023. probs + LZMA_IS_REP (lstate),
  5024. poffset, &range, &code))
  5025. {
  5026. int short_rep;
  5027. uint32_t next_dist;
  5028. /* Repeated match. */
  5029. short_rep = 0;
  5030. if (elf_lzma_bit (compressed, compressed_size,
  5031. probs + LZMA_IS_REP0 (lstate),
  5032. poffset, &range, &code))
  5033. {
  5034. if (elf_lzma_bit (compressed, compressed_size,
  5035. probs + LZMA_IS_REP1 (lstate),
  5036. poffset, &range, &code))
  5037. {
  5038. if (elf_lzma_bit (compressed, compressed_size,
  5039. probs + LZMA_IS_REP2 (lstate),
  5040. poffset, &range, &code))
  5041. {
  5042. next_dist = dist[3];
  5043. dist[3] = dist[2];
  5044. }
  5045. else
  5046. {
  5047. next_dist = dist[2];
  5048. }
  5049. dist[2] = dist[1];
  5050. }
  5051. else
  5052. {
  5053. next_dist = dist[1];
  5054. }
  5055. dist[1] = dist[0];
  5056. dist[0] = next_dist;
  5057. }
  5058. else
  5059. {
  5060. if (!elf_lzma_bit (compressed, compressed_size,
  5061. (probs
  5062. + LZMA_IS_REP0_LONG (lstate,
  5063. pos_state)),
  5064. poffset, &range, &code))
  5065. short_rep = 1;
  5066. }
  5067. if (lstate < 7)
  5068. lstate = short_rep ? 9 : 8;
  5069. else
  5070. lstate = 11;
  5071. if (short_rep)
  5072. len = 1;
  5073. else
  5074. len = elf_lzma_len (compressed, compressed_size,
  5075. probs, 1, pos_state, poffset,
  5076. &range, &code);
  5077. }
  5078. else
  5079. {
  5080. uint32_t dist_state;
  5081. uint32_t dist_slot;
  5082. uint16_t *probs_dist;
  5083. /* Match. */
  5084. if (lstate < 7)
  5085. lstate = 7;
  5086. else
  5087. lstate = 10;
  5088. dist[3] = dist[2];
  5089. dist[2] = dist[1];
  5090. dist[1] = dist[0];
  5091. len = elf_lzma_len (compressed, compressed_size,
  5092. probs, 0, pos_state, poffset,
  5093. &range, &code);
  5094. if (len < 4 + 2)
  5095. dist_state = len - 2;
  5096. else
  5097. dist_state = 3;
  5098. probs_dist = probs + LZMA_DIST_SLOT (dist_state, 0);
  5099. dist_slot = elf_lzma_integer (compressed,
  5100. compressed_size,
  5101. probs_dist, 6,
  5102. poffset, &range,
  5103. &code);
  5104. if (dist_slot < LZMA_DIST_MODEL_START)
  5105. dist[0] = dist_slot;
  5106. else
  5107. {
  5108. uint32_t limit;
  5109. limit = (dist_slot >> 1) - 1;
  5110. dist[0] = 2 + (dist_slot & 1);
  5111. if (dist_slot < LZMA_DIST_MODEL_END)
  5112. {
  5113. dist[0] <<= limit;
  5114. probs_dist = (probs
  5115. + LZMA_DIST_SPECIAL(dist[0]
  5116. - dist_slot
  5117. - 1));
  5118. dist[0] +=
  5119. elf_lzma_reverse_integer (compressed,
  5120. compressed_size,
  5121. probs_dist,
  5122. limit, poffset,
  5123. &range, &code);
  5124. }
  5125. else
  5126. {
  5127. uint32_t dist0;
  5128. uint32_t i;
  5129. dist0 = dist[0];
  5130. for (i = 0; i < limit - 4; i++)
  5131. {
  5132. uint32_t mask;
  5133. elf_lzma_range_normalize (compressed,
  5134. compressed_size,
  5135. poffset,
  5136. &range, &code);
  5137. range >>= 1;
  5138. code -= range;
  5139. mask = -(code >> 31);
  5140. code += range & mask;
  5141. dist0 <<= 1;
  5142. dist0 += mask + 1;
  5143. }
  5144. dist0 <<= 4;
  5145. probs_dist = probs + LZMA_DIST_ALIGN (0);
  5146. dist0 +=
  5147. elf_lzma_reverse_integer (compressed,
  5148. compressed_size,
  5149. probs_dist, 4,
  5150. poffset,
  5151. &range, &code);
  5152. dist[0] = dist0;
  5153. }
  5154. }
  5155. }
  5156. if (unlikely (uncompressed_offset
  5157. - dict_start_offset < dist[0] + 1))
  5158. {
  5159. elf_uncompress_failed ();
  5160. return 0;
  5161. }
  5162. if (unlikely (uncompressed_offset + len > uncompressed_size))
  5163. {
  5164. elf_uncompress_failed ();
  5165. return 0;
  5166. }
  5167. if (dist[0] == 0)
  5168. {
  5169. /* A common case, meaning repeat the last
  5170. character LEN times. */
  5171. memset (uncompressed + uncompressed_offset,
  5172. uncompressed[uncompressed_offset - 1],
  5173. len);
  5174. uncompressed_offset += len;
  5175. }
  5176. else if (dist[0] + 1 >= len)
  5177. {
  5178. memcpy (uncompressed + uncompressed_offset,
  5179. uncompressed + uncompressed_offset - dist[0] - 1,
  5180. len);
  5181. uncompressed_offset += len;
  5182. }
  5183. else
  5184. {
  5185. while (len > 0)
  5186. {
  5187. uint32_t copy;
  5188. copy = len < dist[0] + 1 ? len : dist[0] + 1;
  5189. memcpy (uncompressed + uncompressed_offset,
  5190. (uncompressed + uncompressed_offset
  5191. - dist[0] - 1),
  5192. copy);
  5193. len -= copy;
  5194. uncompressed_offset += copy;
  5195. }
  5196. }
  5197. }
  5198. else
  5199. {
  5200. unsigned char prev;
  5201. unsigned char low;
  5202. size_t high;
  5203. uint16_t *lit_probs;
  5204. unsigned int sym;
  5205. /* Literal value. */
  5206. if (uncompressed_offset > 0)
  5207. prev = uncompressed[uncompressed_offset - 1];
  5208. else
  5209. prev = 0;
  5210. low = prev >> (8 - lc);
  5211. high = (((uncompressed_offset - dict_start_offset)
  5212. & ((1 << lp) - 1))
  5213. << lc);
  5214. lit_probs = probs + LZMA_LITERAL (low + high, 0);
  5215. if (lstate < 7)
  5216. sym = elf_lzma_integer (compressed, compressed_size,
  5217. lit_probs, 8, poffset, &range,
  5218. &code);
  5219. else
  5220. {
  5221. unsigned int match;
  5222. unsigned int bit;
  5223. unsigned int match_bit;
  5224. unsigned int idx;
  5225. sym = 1;
  5226. if (uncompressed_offset >= dist[0] + 1)
  5227. match = uncompressed[uncompressed_offset - dist[0] - 1];
  5228. else
  5229. match = 0;
  5230. match <<= 1;
  5231. bit = 0x100;
  5232. do
  5233. {
  5234. match_bit = match & bit;
  5235. match <<= 1;
  5236. idx = bit + match_bit + sym;
  5237. sym <<= 1;
  5238. if (elf_lzma_bit (compressed, compressed_size,
  5239. lit_probs + idx, poffset,
  5240. &range, &code))
  5241. {
  5242. ++sym;
  5243. bit &= match_bit;
  5244. }
  5245. else
  5246. {
  5247. bit &= ~ match_bit;
  5248. }
  5249. }
  5250. while (sym < 0x100);
  5251. }
  5252. if (unlikely (uncompressed_offset >= uncompressed_size))
  5253. {
  5254. elf_uncompress_failed ();
  5255. return 0;
  5256. }
  5257. uncompressed[uncompressed_offset] = (unsigned char) sym;
  5258. ++uncompressed_offset;
  5259. if (lstate <= 3)
  5260. lstate = 0;
  5261. else if (lstate <= 9)
  5262. lstate -= 3;
  5263. else
  5264. lstate -= 6;
  5265. }
  5266. }
  5267. elf_lzma_range_normalize (compressed, compressed_size, poffset,
  5268. &range, &code);
  5269. off = *poffset;
  5270. }
  5271. }
  5272. /* We have reached the end of the block. Pad to four byte
  5273. boundary. */
  5274. off = (off + 3) &~ (size_t) 3;
  5275. if (unlikely (off > compressed_size))
  5276. {
  5277. elf_uncompress_failed ();
  5278. return 0;
  5279. }
  5280. switch (check)
  5281. {
  5282. case 0:
  5283. /* No check. */
  5284. break;
  5285. case 1:
  5286. /* CRC32 */
  5287. if (unlikely (off + 4 > compressed_size))
  5288. {
  5289. elf_uncompress_failed ();
  5290. return 0;
  5291. }
  5292. computed_crc = elf_crc32 (0, uncompressed, uncompressed_offset);
  5293. stream_crc = (compressed[off]
  5294. | (compressed[off + 1] << 8)
  5295. | (compressed[off + 2] << 16)
  5296. | (compressed[off + 3] << 24));
  5297. if (computed_crc != stream_crc)
  5298. {
  5299. elf_uncompress_failed ();
  5300. return 0;
  5301. }
  5302. off += 4;
  5303. break;
  5304. case 4:
  5305. /* CRC64. We don't bother computing a CRC64 checksum. */
  5306. if (unlikely (off + 8 > compressed_size))
  5307. {
  5308. elf_uncompress_failed ();
  5309. return 0;
  5310. }
  5311. off += 8;
  5312. break;
  5313. case 10:
  5314. /* SHA. We don't bother computing a SHA checksum. */
  5315. if (unlikely (off + 32 > compressed_size))
  5316. {
  5317. elf_uncompress_failed ();
  5318. return 0;
  5319. }
  5320. off += 32;
  5321. break;
  5322. default:
  5323. elf_uncompress_failed ();
  5324. return 0;
  5325. }
  5326. *poffset = off;
  5327. return 1;
  5328. }
  5329. /* Uncompress LZMA data found in a minidebug file. The minidebug
  5330. format is described at
  5331. https://sourceware.org/gdb/current/onlinedocs/gdb/MiniDebugInfo.html.
  5332. Returns 0 on error, 1 on successful decompression. For this
  5333. function we return 0 on failure to decompress, as the calling code
  5334. will carry on in that case. */
  5335. static int
  5336. elf_uncompress_lzma (struct backtrace_state *state,
  5337. const unsigned char *compressed, size_t compressed_size,
  5338. backtrace_error_callback error_callback, void *data,
  5339. unsigned char **uncompressed, size_t *uncompressed_size)
  5340. {
  5341. size_t header_size;
  5342. size_t footer_size;
  5343. unsigned char check;
  5344. uint32_t computed_crc;
  5345. uint32_t stream_crc;
  5346. size_t offset;
  5347. size_t index_size;
  5348. size_t footer_offset;
  5349. size_t index_offset;
  5350. uint64_t index_compressed_size;
  5351. uint64_t index_uncompressed_size;
  5352. unsigned char *mem;
  5353. uint16_t *probs;
  5354. size_t compressed_block_size;
  5355. /* The format starts with a stream header and ends with a stream
  5356. footer. */
  5357. header_size = 12;
  5358. footer_size = 12;
  5359. if (unlikely (compressed_size < header_size + footer_size))
  5360. {
  5361. elf_uncompress_failed ();
  5362. return 0;
  5363. }
  5364. /* The stream header starts with a magic string. */
  5365. if (unlikely (memcmp (compressed, "\375" "7zXZ\0", 6) != 0))
  5366. {
  5367. elf_uncompress_failed ();
  5368. return 0;
  5369. }
  5370. /* Next come stream flags. The first byte is zero, the second byte
  5371. is the check. */
  5372. if (unlikely (compressed[6] != 0))
  5373. {
  5374. elf_uncompress_failed ();
  5375. return 0;
  5376. }
  5377. check = compressed[7];
  5378. if (unlikely ((check & 0xf8) != 0))
  5379. {
  5380. elf_uncompress_failed ();
  5381. return 0;
  5382. }
  5383. /* Next comes a CRC of the stream flags. */
  5384. computed_crc = elf_crc32 (0, compressed + 6, 2);
  5385. stream_crc = ((uint32_t)compressed[8]
  5386. | ((uint32_t)compressed[9] << 8)
  5387. | ((uint32_t)compressed[10] << 16)
  5388. | ((uint32_t)compressed[11] << 24));
  5389. if (unlikely (computed_crc != stream_crc))
  5390. {
  5391. elf_uncompress_failed ();
  5392. return 0;
  5393. }
  5394. /* Now that we've parsed the header, parse the footer, so that we
  5395. can get the uncompressed size. */
  5396. /* The footer ends with two magic bytes. */
  5397. offset = compressed_size;
  5398. if (unlikely (memcmp (compressed + offset - 2, "YZ", 2) != 0))
  5399. {
  5400. elf_uncompress_failed ();
  5401. return 0;
  5402. }
  5403. offset -= 2;
  5404. /* Before that are the stream flags, which should be the same as the
  5405. flags in the header. */
  5406. if (unlikely (compressed[offset - 2] != 0
  5407. || compressed[offset - 1] != check))
  5408. {
  5409. elf_uncompress_failed ();
  5410. return 0;
  5411. }
  5412. offset -= 2;
  5413. /* Before that is the size of the index field, which precedes the
  5414. footer. */
  5415. index_size = (compressed[offset - 4]
  5416. | (compressed[offset - 3] << 8)
  5417. | (compressed[offset - 2] << 16)
  5418. | (compressed[offset - 1] << 24));
  5419. index_size = (index_size + 1) * 4;
  5420. offset -= 4;
  5421. /* Before that is a footer CRC. */
  5422. computed_crc = elf_crc32 (0, compressed + offset, 6);
  5423. stream_crc = ((uint32_t)compressed[offset - 4]
  5424. | ((uint32_t)compressed[offset - 3] << 8)
  5425. | ((uint32_t)compressed[offset - 2] << 16)
  5426. | ((uint32_t)compressed[offset - 1] << 24));
  5427. if (unlikely (computed_crc != stream_crc))
  5428. {
  5429. elf_uncompress_failed ();
  5430. return 0;
  5431. }
  5432. offset -= 4;
  5433. /* The index comes just before the footer. */
  5434. if (unlikely (offset < index_size + header_size))
  5435. {
  5436. elf_uncompress_failed ();
  5437. return 0;
  5438. }
  5439. footer_offset = offset;
  5440. offset -= index_size;
  5441. index_offset = offset;
  5442. /* The index starts with a zero byte. */
  5443. if (unlikely (compressed[offset] != 0))
  5444. {
  5445. elf_uncompress_failed ();
  5446. return 0;
  5447. }
  5448. ++offset;
  5449. /* Next is the number of blocks. We expect zero blocks for an empty
  5450. stream, and otherwise a single block. */
  5451. if (unlikely (compressed[offset] == 0))
  5452. {
  5453. *uncompressed = NULL;
  5454. *uncompressed_size = 0;
  5455. return 1;
  5456. }
  5457. if (unlikely (compressed[offset] != 1))
  5458. {
  5459. elf_uncompress_failed ();
  5460. return 0;
  5461. }
  5462. ++offset;
  5463. /* Next is the compressed size and the uncompressed size. */
  5464. if (!elf_lzma_varint (compressed, compressed_size, &offset,
  5465. &index_compressed_size))
  5466. return 0;
  5467. if (!elf_lzma_varint (compressed, compressed_size, &offset,
  5468. &index_uncompressed_size))
  5469. return 0;
  5470. /* Pad to a four byte boundary. */
  5471. offset = (offset + 3) &~ (size_t) 3;
  5472. /* Next is a CRC of the index. */
  5473. computed_crc = elf_crc32 (0, compressed + index_offset,
  5474. offset - index_offset);
  5475. stream_crc = ((uint32_t)compressed[offset]
  5476. | ((uint32_t)compressed[offset + 1] << 8)
  5477. | ((uint32_t)compressed[offset + 2] << 16)
  5478. | ((uint32_t)compressed[offset + 3] << 24));
  5479. if (unlikely (computed_crc != stream_crc))
  5480. {
  5481. elf_uncompress_failed ();
  5482. return 0;
  5483. }
  5484. offset += 4;
  5485. /* We should now be back at the footer. */
  5486. if (unlikely (offset != footer_offset))
  5487. {
  5488. elf_uncompress_failed ();
  5489. return 0;
  5490. }
  5491. /* Allocate space to hold the uncompressed data. If we succeed in
  5492. uncompressing the LZMA data, we never free this memory. */
  5493. mem = (unsigned char *) backtrace_alloc (state, index_uncompressed_size,
  5494. error_callback, data);
  5495. if (unlikely (mem == NULL))
  5496. return 0;
  5497. *uncompressed = mem;
  5498. *uncompressed_size = index_uncompressed_size;
  5499. /* Allocate space for probabilities. */
  5500. probs = ((uint16_t *)
  5501. backtrace_alloc (state,
  5502. LZMA_PROB_TOTAL_COUNT * sizeof (uint16_t),
  5503. error_callback, data));
  5504. if (unlikely (probs == NULL))
  5505. {
  5506. backtrace_free (state, mem, index_uncompressed_size, error_callback,
  5507. data);
  5508. return 0;
  5509. }
  5510. /* Uncompress the block, which follows the header. */
  5511. offset = 12;
  5512. if (!elf_uncompress_lzma_block (compressed, compressed_size, check, probs,
  5513. mem, index_uncompressed_size, &offset))
  5514. {
  5515. backtrace_free (state, mem, index_uncompressed_size, error_callback,
  5516. data);
  5517. return 0;
  5518. }
  5519. compressed_block_size = offset - 12;
  5520. if (unlikely (compressed_block_size
  5521. != ((index_compressed_size + 3) &~ (size_t) 3)))
  5522. {
  5523. elf_uncompress_failed ();
  5524. backtrace_free (state, mem, index_uncompressed_size, error_callback,
  5525. data);
  5526. return 0;
  5527. }
  5528. offset = (offset + 3) &~ (size_t) 3;
  5529. if (unlikely (offset != index_offset))
  5530. {
  5531. elf_uncompress_failed ();
  5532. backtrace_free (state, mem, index_uncompressed_size, error_callback,
  5533. data);
  5534. return 0;
  5535. }
  5536. return 1;
  5537. }
  5538. /* This function is a hook for testing the LZMA support. It is only
  5539. used by tests. */
  5540. int
  5541. backtrace_uncompress_lzma (struct backtrace_state *state,
  5542. const unsigned char *compressed,
  5543. size_t compressed_size,
  5544. backtrace_error_callback error_callback,
  5545. void *data, unsigned char **uncompressed,
  5546. size_t *uncompressed_size)
  5547. {
  5548. return elf_uncompress_lzma (state, compressed, compressed_size,
  5549. error_callback, data, uncompressed,
  5550. uncompressed_size);
  5551. }
  5552. /* Add the backtrace data for one ELF file. Returns 1 on success,
  5553. 0 on failure (in both cases descriptor is closed) or -1 if exe
  5554. is non-zero and the ELF file is ET_DYN, which tells the caller that
  5555. elf_add will need to be called on the descriptor again after
  5556. base_address is determined. */
  5557. static int
  5558. elf_add (struct backtrace_state *state, const char *filename, int descriptor,
  5559. const unsigned char *memory, size_t memory_size,
  5560. struct libbacktrace_base_address base_address,
  5561. struct elf_ppc64_opd_data *caller_opd,
  5562. backtrace_error_callback error_callback, void *data,
  5563. fileline *fileline_fn, int *found_sym, int *found_dwarf,
  5564. struct dwarf_data **fileline_entry, int exe, int debuginfo,
  5565. const char *with_buildid_data, uint32_t with_buildid_size)
  5566. {
  5567. struct elf_view ehdr_view;
  5568. b_elf_ehdr ehdr;
  5569. off_t shoff;
  5570. unsigned int shnum;
  5571. unsigned int shstrndx;
  5572. struct elf_view shdrs_view;
  5573. int shdrs_view_valid;
  5574. const b_elf_shdr *shdrs;
  5575. const b_elf_shdr *shstrhdr;
  5576. size_t shstr_size;
  5577. off_t shstr_off;
  5578. struct elf_view names_view;
  5579. int names_view_valid;
  5580. const char *names;
  5581. unsigned int symtab_shndx;
  5582. unsigned int dynsym_shndx;
  5583. unsigned int i;
  5584. struct debug_section_info sections[DEBUG_MAX];
  5585. struct debug_section_info zsections[DEBUG_MAX];
  5586. struct elf_view symtab_view;
  5587. int symtab_view_valid;
  5588. struct elf_view strtab_view;
  5589. int strtab_view_valid;
  5590. struct elf_view buildid_view;
  5591. int buildid_view_valid;
  5592. const char *buildid_data;
  5593. uint32_t buildid_size;
  5594. struct elf_view debuglink_view;
  5595. int debuglink_view_valid;
  5596. const char *debuglink_name;
  5597. uint32_t debuglink_crc;
  5598. struct elf_view debugaltlink_view;
  5599. int debugaltlink_view_valid;
  5600. const char *debugaltlink_name;
  5601. const char *debugaltlink_buildid_data;
  5602. uint32_t debugaltlink_buildid_size;
  5603. struct elf_view gnu_debugdata_view;
  5604. int gnu_debugdata_view_valid;
  5605. size_t gnu_debugdata_size;
  5606. unsigned char *gnu_debugdata_uncompressed;
  5607. size_t gnu_debugdata_uncompressed_size;
  5608. off_t min_offset;
  5609. off_t max_offset;
  5610. off_t debug_size;
  5611. struct elf_view debug_view;
  5612. int debug_view_valid;
  5613. unsigned int using_debug_view;
  5614. uint16_t *zdebug_table;
  5615. struct elf_view split_debug_view[DEBUG_MAX];
  5616. unsigned char split_debug_view_valid[DEBUG_MAX];
  5617. struct elf_ppc64_opd_data opd_data, *opd;
  5618. int opd_view_valid;
  5619. struct dwarf_sections dwarf_sections;
  5620. if (!debuginfo)
  5621. {
  5622. *found_sym = 0;
  5623. *found_dwarf = 0;
  5624. }
  5625. shdrs_view_valid = 0;
  5626. names_view_valid = 0;
  5627. symtab_view_valid = 0;
  5628. strtab_view_valid = 0;
  5629. buildid_view_valid = 0;
  5630. buildid_data = NULL;
  5631. buildid_size = 0;
  5632. debuglink_view_valid = 0;
  5633. debuglink_name = NULL;
  5634. debuglink_crc = 0;
  5635. debugaltlink_view_valid = 0;
  5636. debugaltlink_name = NULL;
  5637. debugaltlink_buildid_data = NULL;
  5638. debugaltlink_buildid_size = 0;
  5639. gnu_debugdata_view_valid = 0;
  5640. gnu_debugdata_size = 0;
  5641. debug_view_valid = 0;
  5642. memset (&split_debug_view_valid[0], 0, sizeof split_debug_view_valid);
  5643. opd = NULL;
  5644. opd_view_valid = 0;
  5645. if (!elf_get_view (state, descriptor, memory, memory_size, 0, sizeof ehdr,
  5646. error_callback, data, &ehdr_view))
  5647. goto fail;
  5648. memcpy (&ehdr, ehdr_view.view.data, sizeof ehdr);
  5649. elf_release_view (state, &ehdr_view, error_callback, data);
  5650. if (ehdr.e_ident[EI_MAG0] != ELFMAG0
  5651. || ehdr.e_ident[EI_MAG1] != ELFMAG1
  5652. || ehdr.e_ident[EI_MAG2] != ELFMAG2
  5653. || ehdr.e_ident[EI_MAG3] != ELFMAG3)
  5654. {
  5655. error_callback (data, "executable file is not ELF", 0);
  5656. goto fail;
  5657. }
  5658. if (ehdr.e_ident[EI_VERSION] != EV_CURRENT)
  5659. {
  5660. error_callback (data, "executable file is unrecognized ELF version", 0);
  5661. goto fail;
  5662. }
  5663. #if BACKTRACE_ELF_SIZE == 32
  5664. #define BACKTRACE_ELFCLASS ELFCLASS32
  5665. #else
  5666. #define BACKTRACE_ELFCLASS ELFCLASS64
  5667. #endif
  5668. if (ehdr.e_ident[EI_CLASS] != BACKTRACE_ELFCLASS)
  5669. {
  5670. error_callback (data, "executable file is unexpected ELF class", 0);
  5671. goto fail;
  5672. }
  5673. if (ehdr.e_ident[EI_DATA] != ELFDATA2LSB
  5674. && ehdr.e_ident[EI_DATA] != ELFDATA2MSB)
  5675. {
  5676. error_callback (data, "executable file has unknown endianness", 0);
  5677. goto fail;
  5678. }
  5679. /* If the executable is ET_DYN, it is either a PIE, or we are running
  5680. directly a shared library with .interp. We need to wait for
  5681. dl_iterate_phdr in that case to determine the actual base_address. */
  5682. if (exe && ehdr.e_type == ET_DYN)
  5683. return -1;
  5684. shoff = ehdr.e_shoff;
  5685. shnum = ehdr.e_shnum;
  5686. shstrndx = ehdr.e_shstrndx;
  5687. if ((shnum == 0 || shstrndx == SHN_XINDEX)
  5688. && shoff != 0)
  5689. {
  5690. struct elf_view shdr_view;
  5691. const b_elf_shdr *shdr;
  5692. if (!elf_get_view (state, descriptor, memory, memory_size, shoff,
  5693. sizeof shdr, error_callback, data, &shdr_view))
  5694. goto fail;
  5695. shdr = (const b_elf_shdr *) shdr_view.view.data;
  5696. if (shnum == 0)
  5697. shnum = shdr->sh_size;
  5698. if (shstrndx == SHN_XINDEX)
  5699. {
  5700. shstrndx = shdr->sh_link;
  5701. /* Versions of the GNU binutils between 2.12 and 2.18 did
  5702. not handle objects with more than SHN_LORESERVE sections
  5703. correctly. All large section indexes were offset by
  5704. 0x100. There is more information at
  5705. http://sourceware.org/bugzilla/show_bug.cgi?id-5900 .
  5706. Fortunately these object files are easy to detect, as the
  5707. GNU binutils always put the section header string table
  5708. near the end of the list of sections. Thus if the
  5709. section header string table index is larger than the
  5710. number of sections, then we know we have to subtract
  5711. 0x100 to get the real section index. */
  5712. if (shstrndx >= shnum && shstrndx >= SHN_LORESERVE + 0x100)
  5713. shstrndx -= 0x100;
  5714. }
  5715. elf_release_view (state, &shdr_view, error_callback, data);
  5716. }
  5717. if (shnum == 0 || shstrndx == 0)
  5718. goto fail;
  5719. /* To translate PC to file/line when using DWARF, we need to find
  5720. the .debug_info and .debug_line sections. */
  5721. /* Read the section headers, skipping the first one. */
  5722. if (!elf_get_view (state, descriptor, memory, memory_size,
  5723. shoff + sizeof (b_elf_shdr),
  5724. (shnum - 1) * sizeof (b_elf_shdr),
  5725. error_callback, data, &shdrs_view))
  5726. goto fail;
  5727. shdrs_view_valid = 1;
  5728. shdrs = (const b_elf_shdr *) shdrs_view.view.data;
  5729. /* Read the section names. */
  5730. shstrhdr = &shdrs[shstrndx - 1];
  5731. shstr_size = shstrhdr->sh_size;
  5732. shstr_off = shstrhdr->sh_offset;
  5733. if (!elf_get_view (state, descriptor, memory, memory_size, shstr_off,
  5734. shstrhdr->sh_size, error_callback, data, &names_view))
  5735. goto fail;
  5736. names_view_valid = 1;
  5737. names = (const char *) names_view.view.data;
  5738. symtab_shndx = 0;
  5739. dynsym_shndx = 0;
  5740. memset (sections, 0, sizeof sections);
  5741. memset (zsections, 0, sizeof zsections);
  5742. /* Look for the symbol table. */
  5743. for (i = 1; i < shnum; ++i)
  5744. {
  5745. const b_elf_shdr *shdr;
  5746. unsigned int sh_name;
  5747. const char *name;
  5748. int j;
  5749. shdr = &shdrs[i - 1];
  5750. if (shdr->sh_type == SHT_SYMTAB)
  5751. symtab_shndx = i;
  5752. else if (shdr->sh_type == SHT_DYNSYM)
  5753. dynsym_shndx = i;
  5754. sh_name = shdr->sh_name;
  5755. if (sh_name >= shstr_size)
  5756. {
  5757. error_callback (data, "ELF section name out of range", 0);
  5758. goto fail;
  5759. }
  5760. name = names + sh_name;
  5761. for (j = 0; j < (int) DEBUG_MAX; ++j)
  5762. {
  5763. if (strcmp (name, dwarf_section_names[j]) == 0)
  5764. {
  5765. sections[j].offset = shdr->sh_offset;
  5766. sections[j].size = shdr->sh_size;
  5767. sections[j].compressed = (shdr->sh_flags & SHF_COMPRESSED) != 0;
  5768. break;
  5769. }
  5770. }
  5771. if (name[0] == '.' && name[1] == 'z')
  5772. {
  5773. for (j = 0; j < (int) DEBUG_MAX; ++j)
  5774. {
  5775. if (strcmp (name + 2, dwarf_section_names[j] + 1) == 0)
  5776. {
  5777. zsections[j].offset = shdr->sh_offset;
  5778. zsections[j].size = shdr->sh_size;
  5779. break;
  5780. }
  5781. }
  5782. }
  5783. /* Read the build ID if present. This could check for any
  5784. SHT_NOTE section with the right note name and type, but gdb
  5785. looks for a specific section name. */
  5786. if ((!debuginfo || with_buildid_data != NULL)
  5787. && !buildid_view_valid
  5788. && strcmp (name, ".note.gnu.build-id") == 0)
  5789. {
  5790. const b_elf_note *note;
  5791. if (!elf_get_view (state, descriptor, memory, memory_size,
  5792. shdr->sh_offset, shdr->sh_size, error_callback,
  5793. data, &buildid_view))
  5794. goto fail;
  5795. buildid_view_valid = 1;
  5796. note = (const b_elf_note *) buildid_view.view.data;
  5797. if (note->type == NT_GNU_BUILD_ID
  5798. && note->namesz == 4
  5799. && strncmp (note->name, "GNU", 4) == 0
  5800. && shdr->sh_size <= 12 + ((note->namesz + 3) & ~ 3) + note->descsz)
  5801. {
  5802. buildid_data = &note->name[0] + ((note->namesz + 3) & ~ 3);
  5803. buildid_size = note->descsz;
  5804. }
  5805. if (with_buildid_size != 0)
  5806. {
  5807. if (buildid_size != with_buildid_size)
  5808. goto fail;
  5809. if (memcmp (buildid_data, with_buildid_data, buildid_size) != 0)
  5810. goto fail;
  5811. }
  5812. }
  5813. /* Read the debuglink file if present. */
  5814. if (!debuginfo
  5815. && !debuglink_view_valid
  5816. && strcmp (name, ".gnu_debuglink") == 0)
  5817. {
  5818. const char *debuglink_data;
  5819. size_t crc_offset;
  5820. if (!elf_get_view (state, descriptor, memory, memory_size,
  5821. shdr->sh_offset, shdr->sh_size, error_callback,
  5822. data, &debuglink_view))
  5823. goto fail;
  5824. debuglink_view_valid = 1;
  5825. debuglink_data = (const char *) debuglink_view.view.data;
  5826. crc_offset = strnlen (debuglink_data, shdr->sh_size);
  5827. crc_offset = (crc_offset + 3) & ~3;
  5828. if (crc_offset + 4 <= shdr->sh_size)
  5829. {
  5830. debuglink_name = debuglink_data;
  5831. debuglink_crc = *(const uint32_t*)(debuglink_data + crc_offset);
  5832. }
  5833. }
  5834. if (!debugaltlink_view_valid
  5835. && strcmp (name, ".gnu_debugaltlink") == 0)
  5836. {
  5837. const char *debugaltlink_data;
  5838. size_t debugaltlink_name_len;
  5839. if (!elf_get_view (state, descriptor, memory, memory_size,
  5840. shdr->sh_offset, shdr->sh_size, error_callback,
  5841. data, &debugaltlink_view))
  5842. goto fail;
  5843. debugaltlink_view_valid = 1;
  5844. debugaltlink_data = (const char *) debugaltlink_view.view.data;
  5845. debugaltlink_name = debugaltlink_data;
  5846. debugaltlink_name_len = strnlen (debugaltlink_data, shdr->sh_size);
  5847. if (debugaltlink_name_len < shdr->sh_size)
  5848. {
  5849. /* Include terminating zero. */
  5850. debugaltlink_name_len += 1;
  5851. debugaltlink_buildid_data
  5852. = debugaltlink_data + debugaltlink_name_len;
  5853. debugaltlink_buildid_size = shdr->sh_size - debugaltlink_name_len;
  5854. }
  5855. }
  5856. if (!debuginfo
  5857. && !gnu_debugdata_view_valid
  5858. && strcmp (name, ".gnu_debugdata") == 0)
  5859. {
  5860. if (!elf_get_view (state, descriptor, memory, memory_size,
  5861. shdr->sh_offset, shdr->sh_size, error_callback,
  5862. data, &gnu_debugdata_view))
  5863. goto fail;
  5864. gnu_debugdata_size = shdr->sh_size;
  5865. gnu_debugdata_view_valid = 1;
  5866. }
  5867. /* Read the .opd section on PowerPC64 ELFv1. */
  5868. if (ehdr.e_machine == EM_PPC64
  5869. && (ehdr.e_flags & EF_PPC64_ABI) < 2
  5870. && shdr->sh_type == SHT_PROGBITS
  5871. && strcmp (name, ".opd") == 0)
  5872. {
  5873. if (!elf_get_view (state, descriptor, memory, memory_size,
  5874. shdr->sh_offset, shdr->sh_size, error_callback,
  5875. data, &opd_data.view))
  5876. goto fail;
  5877. opd = &opd_data;
  5878. opd->addr = shdr->sh_addr;
  5879. opd->data = (const char *) opd_data.view.view.data;
  5880. opd->size = shdr->sh_size;
  5881. opd_view_valid = 1;
  5882. }
  5883. }
  5884. /* A debuginfo file may not have a useful .opd section, but we can use the
  5885. one from the original executable. */
  5886. if (opd == NULL)
  5887. opd = caller_opd;
  5888. if (symtab_shndx == 0)
  5889. symtab_shndx = dynsym_shndx;
  5890. if (symtab_shndx != 0)
  5891. {
  5892. const b_elf_shdr *symtab_shdr;
  5893. unsigned int strtab_shndx;
  5894. const b_elf_shdr *strtab_shdr;
  5895. struct elf_syminfo_data *sdata;
  5896. symtab_shdr = &shdrs[symtab_shndx - 1];
  5897. strtab_shndx = symtab_shdr->sh_link;
  5898. if (strtab_shndx >= shnum)
  5899. {
  5900. error_callback (data,
  5901. "ELF symbol table strtab link out of range", 0);
  5902. goto fail;
  5903. }
  5904. strtab_shdr = &shdrs[strtab_shndx - 1];
  5905. if (!elf_get_view (state, descriptor, memory, memory_size,
  5906. symtab_shdr->sh_offset, symtab_shdr->sh_size,
  5907. error_callback, data, &symtab_view))
  5908. goto fail;
  5909. symtab_view_valid = 1;
  5910. if (!elf_get_view (state, descriptor, memory, memory_size,
  5911. strtab_shdr->sh_offset, strtab_shdr->sh_size,
  5912. error_callback, data, &strtab_view))
  5913. goto fail;
  5914. strtab_view_valid = 1;
  5915. sdata = ((struct elf_syminfo_data *)
  5916. backtrace_alloc (state, sizeof *sdata, error_callback, data));
  5917. if (sdata == NULL)
  5918. goto fail;
  5919. if (!elf_initialize_syminfo (state, base_address,
  5920. symtab_view.view.data, symtab_shdr->sh_size,
  5921. strtab_view.view.data, strtab_shdr->sh_size,
  5922. error_callback, data, sdata, opd))
  5923. {
  5924. backtrace_free (state, sdata, sizeof *sdata, error_callback, data);
  5925. goto fail;
  5926. }
  5927. /* We no longer need the symbol table, but we hold on to the
  5928. string table permanently. */
  5929. elf_release_view (state, &symtab_view, error_callback, data);
  5930. symtab_view_valid = 0;
  5931. strtab_view_valid = 0;
  5932. *found_sym = 1;
  5933. elf_add_syminfo_data (state, sdata);
  5934. }
  5935. elf_release_view (state, &shdrs_view, error_callback, data);
  5936. shdrs_view_valid = 0;
  5937. elf_release_view (state, &names_view, error_callback, data);
  5938. names_view_valid = 0;
  5939. /* If the debug info is in a separate file, read that one instead. */
  5940. if (buildid_data != NULL)
  5941. {
  5942. int d;
  5943. d = elf_open_debugfile_by_buildid (state, buildid_data, buildid_size,
  5944. error_callback, data);
  5945. if (d >= 0)
  5946. {
  5947. int ret;
  5948. elf_release_view (state, &buildid_view, error_callback, data);
  5949. if (debuglink_view_valid)
  5950. elf_release_view (state, &debuglink_view, error_callback, data);
  5951. if (debugaltlink_view_valid)
  5952. elf_release_view (state, &debugaltlink_view, error_callback, data);
  5953. ret = elf_add (state, "", d, NULL, 0, base_address, opd,
  5954. error_callback, data, fileline_fn, found_sym,
  5955. found_dwarf, NULL, 0, 1, NULL, 0);
  5956. if (ret < 0)
  5957. backtrace_close (d, error_callback, data);
  5958. else if (descriptor >= 0)
  5959. backtrace_close (descriptor, error_callback, data);
  5960. return ret;
  5961. }
  5962. }
  5963. if (buildid_view_valid)
  5964. {
  5965. elf_release_view (state, &buildid_view, error_callback, data);
  5966. buildid_view_valid = 0;
  5967. }
  5968. if (debuglink_name != NULL)
  5969. {
  5970. int d;
  5971. d = elf_open_debugfile_by_debuglink (state, filename, debuglink_name,
  5972. debuglink_crc, error_callback,
  5973. data);
  5974. if (d >= 0)
  5975. {
  5976. int ret;
  5977. elf_release_view (state, &debuglink_view, error_callback, data);
  5978. if (debugaltlink_view_valid)
  5979. elf_release_view (state, &debugaltlink_view, error_callback, data);
  5980. ret = elf_add (state, "", d, NULL, 0, base_address, opd,
  5981. error_callback, data, fileline_fn, found_sym,
  5982. found_dwarf, NULL, 0, 1, NULL, 0);
  5983. if (ret < 0)
  5984. backtrace_close (d, error_callback, data);
  5985. else if (descriptor >= 0)
  5986. backtrace_close(descriptor, error_callback, data);
  5987. return ret;
  5988. }
  5989. }
  5990. if (debuglink_view_valid)
  5991. {
  5992. elf_release_view (state, &debuglink_view, error_callback, data);
  5993. debuglink_view_valid = 0;
  5994. }
  5995. struct dwarf_data *fileline_altlink = NULL;
  5996. if (debugaltlink_name != NULL)
  5997. {
  5998. int d;
  5999. d = elf_open_debugfile_by_debuglink (state, filename, debugaltlink_name,
  6000. 0, error_callback, data);
  6001. if (d >= 0)
  6002. {
  6003. int ret;
  6004. ret = elf_add (state, filename, d, NULL, 0, base_address, opd,
  6005. error_callback, data, fileline_fn, found_sym,
  6006. found_dwarf, &fileline_altlink, 0, 1,
  6007. debugaltlink_buildid_data, debugaltlink_buildid_size);
  6008. elf_release_view (state, &debugaltlink_view, error_callback, data);
  6009. debugaltlink_view_valid = 0;
  6010. if (ret < 0)
  6011. {
  6012. backtrace_close (d, error_callback, data);
  6013. return ret;
  6014. }
  6015. }
  6016. }
  6017. if (debugaltlink_view_valid)
  6018. {
  6019. elf_release_view (state, &debugaltlink_view, error_callback, data);
  6020. debugaltlink_view_valid = 0;
  6021. }
  6022. if (gnu_debugdata_view_valid)
  6023. {
  6024. int ret;
  6025. ret = elf_uncompress_lzma (state,
  6026. ((const unsigned char *)
  6027. gnu_debugdata_view.view.data),
  6028. gnu_debugdata_size, error_callback, data,
  6029. &gnu_debugdata_uncompressed,
  6030. &gnu_debugdata_uncompressed_size);
  6031. elf_release_view (state, &gnu_debugdata_view, error_callback, data);
  6032. gnu_debugdata_view_valid = 0;
  6033. if (ret)
  6034. {
  6035. ret = elf_add (state, filename, -1, gnu_debugdata_uncompressed,
  6036. gnu_debugdata_uncompressed_size, base_address, opd,
  6037. error_callback, data, fileline_fn, found_sym,
  6038. found_dwarf, NULL, 0, 0, NULL, 0);
  6039. if (ret >= 0 && descriptor >= 0)
  6040. backtrace_close(descriptor, error_callback, data);
  6041. return ret;
  6042. }
  6043. }
  6044. if (opd_view_valid)
  6045. {
  6046. elf_release_view (state, &opd->view, error_callback, data);
  6047. opd_view_valid = 0;
  6048. opd = NULL;
  6049. }
  6050. /* Read all the debug sections in a single view, since they are
  6051. probably adjacent in the file. If any of sections are
  6052. uncompressed, we never release this view. */
  6053. min_offset = 0;
  6054. max_offset = 0;
  6055. debug_size = 0;
  6056. for (i = 0; i < (int) DEBUG_MAX; ++i)
  6057. {
  6058. off_t end;
  6059. if (sections[i].size != 0)
  6060. {
  6061. if (min_offset == 0 || sections[i].offset < min_offset)
  6062. min_offset = sections[i].offset;
  6063. end = sections[i].offset + sections[i].size;
  6064. if (end > max_offset)
  6065. max_offset = end;
  6066. debug_size += sections[i].size;
  6067. }
  6068. if (zsections[i].size != 0)
  6069. {
  6070. if (min_offset == 0 || zsections[i].offset < min_offset)
  6071. min_offset = zsections[i].offset;
  6072. end = zsections[i].offset + zsections[i].size;
  6073. if (end > max_offset)
  6074. max_offset = end;
  6075. debug_size += zsections[i].size;
  6076. }
  6077. }
  6078. if (min_offset == 0 || max_offset == 0)
  6079. {
  6080. if (descriptor >= 0)
  6081. {
  6082. if (!backtrace_close (descriptor, error_callback, data))
  6083. goto fail;
  6084. }
  6085. return 1;
  6086. }
  6087. /* If the total debug section size is large, assume that there are
  6088. gaps between the sections, and read them individually. */
  6089. if (max_offset - min_offset < 0x20000000
  6090. || max_offset - min_offset < debug_size + 0x10000)
  6091. {
  6092. if (!elf_get_view (state, descriptor, memory, memory_size, min_offset,
  6093. max_offset - min_offset, error_callback, data,
  6094. &debug_view))
  6095. goto fail;
  6096. debug_view_valid = 1;
  6097. }
  6098. else
  6099. {
  6100. memset (&split_debug_view[0], 0, sizeof split_debug_view);
  6101. for (i = 0; i < (int) DEBUG_MAX; ++i)
  6102. {
  6103. struct debug_section_info *dsec;
  6104. if (sections[i].size != 0)
  6105. dsec = &sections[i];
  6106. else if (zsections[i].size != 0)
  6107. dsec = &zsections[i];
  6108. else
  6109. continue;
  6110. if (!elf_get_view (state, descriptor, memory, memory_size,
  6111. dsec->offset, dsec->size, error_callback, data,
  6112. &split_debug_view[i]))
  6113. goto fail;
  6114. split_debug_view_valid[i] = 1;
  6115. if (sections[i].size != 0)
  6116. sections[i].data = ((const unsigned char *)
  6117. split_debug_view[i].view.data);
  6118. else
  6119. zsections[i].data = ((const unsigned char *)
  6120. split_debug_view[i].view.data);
  6121. }
  6122. }
  6123. /* We've read all we need from the executable. */
  6124. if (descriptor >= 0)
  6125. {
  6126. if (!backtrace_close (descriptor, error_callback, data))
  6127. goto fail;
  6128. descriptor = -1;
  6129. }
  6130. using_debug_view = 0;
  6131. if (debug_view_valid)
  6132. {
  6133. for (i = 0; i < (int) DEBUG_MAX; ++i)
  6134. {
  6135. if (sections[i].size == 0)
  6136. sections[i].data = NULL;
  6137. else
  6138. {
  6139. sections[i].data = ((const unsigned char *) debug_view.view.data
  6140. + (sections[i].offset - min_offset));
  6141. ++using_debug_view;
  6142. }
  6143. if (zsections[i].size == 0)
  6144. zsections[i].data = NULL;
  6145. else
  6146. zsections[i].data = ((const unsigned char *) debug_view.view.data
  6147. + (zsections[i].offset - min_offset));
  6148. }
  6149. }
  6150. /* Uncompress the old format (--compress-debug-sections=zlib-gnu). */
  6151. zdebug_table = NULL;
  6152. for (i = 0; i < (int) DEBUG_MAX; ++i)
  6153. {
  6154. if (sections[i].size == 0 && zsections[i].size > 0)
  6155. {
  6156. unsigned char *uncompressed_data;
  6157. size_t uncompressed_size;
  6158. if (zdebug_table == NULL)
  6159. {
  6160. zdebug_table = ((uint16_t *)
  6161. backtrace_alloc (state, ZLIB_TABLE_SIZE,
  6162. error_callback, data));
  6163. if (zdebug_table == NULL)
  6164. goto fail;
  6165. }
  6166. uncompressed_data = NULL;
  6167. uncompressed_size = 0;
  6168. if (!elf_uncompress_zdebug (state, zsections[i].data,
  6169. zsections[i].size, zdebug_table,
  6170. error_callback, data,
  6171. &uncompressed_data, &uncompressed_size))
  6172. goto fail;
  6173. sections[i].data = uncompressed_data;
  6174. sections[i].size = uncompressed_size;
  6175. sections[i].compressed = 0;
  6176. if (split_debug_view_valid[i])
  6177. {
  6178. elf_release_view (state, &split_debug_view[i],
  6179. error_callback, data);
  6180. split_debug_view_valid[i] = 0;
  6181. }
  6182. }
  6183. }
  6184. if (zdebug_table != NULL)
  6185. {
  6186. backtrace_free (state, zdebug_table, ZLIB_TABLE_SIZE,
  6187. error_callback, data);
  6188. zdebug_table = NULL;
  6189. }
  6190. /* Uncompress the official ELF format
  6191. (--compress-debug-sections=zlib-gabi, --compress-debug-sections=zstd). */
  6192. for (i = 0; i < (int) DEBUG_MAX; ++i)
  6193. {
  6194. unsigned char *uncompressed_data;
  6195. size_t uncompressed_size;
  6196. if (sections[i].size == 0 || !sections[i].compressed)
  6197. continue;
  6198. if (zdebug_table == NULL)
  6199. {
  6200. zdebug_table = ((uint16_t *)
  6201. backtrace_alloc (state, ZDEBUG_TABLE_SIZE,
  6202. error_callback, data));
  6203. if (zdebug_table == NULL)
  6204. goto fail;
  6205. }
  6206. uncompressed_data = NULL;
  6207. uncompressed_size = 0;
  6208. if (!elf_uncompress_chdr (state, sections[i].data, sections[i].size,
  6209. zdebug_table, error_callback, data,
  6210. &uncompressed_data, &uncompressed_size))
  6211. goto fail;
  6212. sections[i].data = uncompressed_data;
  6213. sections[i].size = uncompressed_size;
  6214. sections[i].compressed = 0;
  6215. if (debug_view_valid)
  6216. --using_debug_view;
  6217. else if (split_debug_view_valid[i])
  6218. {
  6219. elf_release_view (state, &split_debug_view[i], error_callback, data);
  6220. split_debug_view_valid[i] = 0;
  6221. }
  6222. }
  6223. if (zdebug_table != NULL)
  6224. backtrace_free (state, zdebug_table, ZDEBUG_TABLE_SIZE,
  6225. error_callback, data);
  6226. if (debug_view_valid && using_debug_view == 0)
  6227. {
  6228. elf_release_view (state, &debug_view, error_callback, data);
  6229. debug_view_valid = 0;
  6230. }
  6231. for (i = 0; i < (int) DEBUG_MAX; ++i)
  6232. {
  6233. dwarf_sections.data[i] = sections[i].data;
  6234. dwarf_sections.size[i] = sections[i].size;
  6235. }
  6236. if (!backtrace_dwarf_add (state, base_address, &dwarf_sections,
  6237. ehdr.e_ident[EI_DATA] == ELFDATA2MSB,
  6238. fileline_altlink,
  6239. error_callback, data, fileline_fn,
  6240. fileline_entry))
  6241. goto fail;
  6242. *found_dwarf = 1;
  6243. return 1;
  6244. fail:
  6245. if (shdrs_view_valid)
  6246. elf_release_view (state, &shdrs_view, error_callback, data);
  6247. if (names_view_valid)
  6248. elf_release_view (state, &names_view, error_callback, data);
  6249. if (symtab_view_valid)
  6250. elf_release_view (state, &symtab_view, error_callback, data);
  6251. if (strtab_view_valid)
  6252. elf_release_view (state, &strtab_view, error_callback, data);
  6253. if (debuglink_view_valid)
  6254. elf_release_view (state, &debuglink_view, error_callback, data);
  6255. if (debugaltlink_view_valid)
  6256. elf_release_view (state, &debugaltlink_view, error_callback, data);
  6257. if (gnu_debugdata_view_valid)
  6258. elf_release_view (state, &gnu_debugdata_view, error_callback, data);
  6259. if (buildid_view_valid)
  6260. elf_release_view (state, &buildid_view, error_callback, data);
  6261. if (debug_view_valid)
  6262. elf_release_view (state, &debug_view, error_callback, data);
  6263. for (i = 0; i < (int) DEBUG_MAX; ++i)
  6264. {
  6265. if (split_debug_view_valid[i])
  6266. elf_release_view (state, &split_debug_view[i], error_callback, data);
  6267. }
  6268. if (opd_view_valid)
  6269. elf_release_view (state, &opd->view, error_callback, data);
  6270. if (descriptor >= 0)
  6271. backtrace_close (descriptor, error_callback, data);
  6272. return 0;
  6273. }
  6274. /* Data passed to phdr_callback. */
  6275. struct phdr_data
  6276. {
  6277. struct backtrace_state *state;
  6278. backtrace_error_callback error_callback;
  6279. void *data;
  6280. fileline *fileline_fn;
  6281. int *found_sym;
  6282. int *found_dwarf;
  6283. const char *exe_filename;
  6284. int exe_descriptor;
  6285. };
  6286. /* Callback passed to dl_iterate_phdr. Load debug info from shared
  6287. libraries. */
  6288. static int
  6289. #ifdef __i386__
  6290. __attribute__ ((__force_align_arg_pointer__))
  6291. #endif
  6292. phdr_callback (struct dl_phdr_info *info, size_t size ATTRIBUTE_UNUSED,
  6293. void *pdata)
  6294. {
  6295. struct phdr_data *pd = (struct phdr_data *) pdata;
  6296. const char *filename;
  6297. int descriptor;
  6298. int does_not_exist;
  6299. struct libbacktrace_base_address base_address;
  6300. fileline elf_fileline_fn;
  6301. int found_dwarf;
  6302. /* There is not much we can do if we don't have the module name,
  6303. unless executable is ET_DYN, where we expect the very first
  6304. phdr_callback to be for the PIE. */
  6305. if (info->dlpi_name == NULL || info->dlpi_name[0] == '\0')
  6306. {
  6307. if (pd->exe_descriptor == -1)
  6308. return 0;
  6309. filename = pd->exe_filename;
  6310. descriptor = pd->exe_descriptor;
  6311. pd->exe_descriptor = -1;
  6312. }
  6313. else
  6314. {
  6315. if (pd->exe_descriptor != -1)
  6316. {
  6317. backtrace_close (pd->exe_descriptor, pd->error_callback, pd->data);
  6318. pd->exe_descriptor = -1;
  6319. }
  6320. filename = info->dlpi_name;
  6321. descriptor = backtrace_open (info->dlpi_name, pd->error_callback,
  6322. pd->data, &does_not_exist);
  6323. if (descriptor < 0)
  6324. return 0;
  6325. }
  6326. base_address.m = info->dlpi_addr;
  6327. if (elf_add (pd->state, filename, descriptor, NULL, 0, base_address, NULL,
  6328. pd->error_callback, pd->data, &elf_fileline_fn, pd->found_sym,
  6329. &found_dwarf, NULL, 0, 0, NULL, 0))
  6330. {
  6331. if (found_dwarf)
  6332. {
  6333. *pd->found_dwarf = 1;
  6334. *pd->fileline_fn = elf_fileline_fn;
  6335. }
  6336. }
  6337. return 0;
  6338. }
  6339. /* Initialize the backtrace data we need from an ELF executable. At
  6340. the ELF level, all we need to do is find the debug info
  6341. sections. */
  6342. int
  6343. backtrace_initialize (struct backtrace_state *state, const char *filename,
  6344. int descriptor, backtrace_error_callback error_callback,
  6345. void *data, fileline *fileline_fn)
  6346. {
  6347. int ret;
  6348. int found_sym;
  6349. int found_dwarf;
  6350. fileline elf_fileline_fn = elf_nodebug;
  6351. struct phdr_data pd;
  6352. /* When using fdpic we must use dl_iterate_phdr for all modules, including
  6353. the main executable, so that we can get the right base address
  6354. mapping. */
  6355. if (!libbacktrace_using_fdpic ())
  6356. {
  6357. struct libbacktrace_base_address zero_base_address;
  6358. memset (&zero_base_address, 0, sizeof zero_base_address);
  6359. ret = elf_add (state, filename, descriptor, NULL, 0, zero_base_address,
  6360. NULL, error_callback, data, &elf_fileline_fn, &found_sym,
  6361. &found_dwarf, NULL, 1, 0, NULL, 0);
  6362. if (!ret)
  6363. return 0;
  6364. }
  6365. pd.state = state;
  6366. pd.error_callback = error_callback;
  6367. pd.data = data;
  6368. pd.fileline_fn = &elf_fileline_fn;
  6369. pd.found_sym = &found_sym;
  6370. pd.found_dwarf = &found_dwarf;
  6371. pd.exe_filename = filename;
  6372. pd.exe_descriptor = ret < 0 ? descriptor : -1;
  6373. dl_iterate_phdr (phdr_callback, (void *) &pd);
  6374. if (!state->threaded)
  6375. {
  6376. if (found_sym)
  6377. state->syminfo_fn = elf_syminfo;
  6378. else if (state->syminfo_fn == NULL)
  6379. state->syminfo_fn = elf_nosyms;
  6380. }
  6381. else
  6382. {
  6383. if (found_sym)
  6384. backtrace_atomic_store_pointer (&state->syminfo_fn, elf_syminfo);
  6385. else
  6386. (void) __sync_bool_compare_and_swap (&state->syminfo_fn, NULL,
  6387. elf_nosyms);
  6388. }
  6389. if (!state->threaded)
  6390. *fileline_fn = state->fileline_fn;
  6391. else
  6392. *fileline_fn = backtrace_atomic_load_pointer (&state->fileline_fn);
  6393. if (*fileline_fn == NULL || *fileline_fn == elf_nodebug)
  6394. *fileline_fn = elf_fileline_fn;
  6395. return 1;
  6396. }