dwarf.c 119 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593
  1. /* dwarf.c -- Get file/line information from DWARF 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 "filenames.h"
  33. #include "backtrace.h"
  34. #include "internal.h"
  35. /* DWARF constants. */
  36. enum dwarf_tag {
  37. DW_TAG_entry_point = 0x3,
  38. DW_TAG_compile_unit = 0x11,
  39. DW_TAG_inlined_subroutine = 0x1d,
  40. DW_TAG_subprogram = 0x2e,
  41. DW_TAG_skeleton_unit = 0x4a,
  42. };
  43. enum dwarf_form {
  44. DW_FORM_addr = 0x01,
  45. DW_FORM_block2 = 0x03,
  46. DW_FORM_block4 = 0x04,
  47. DW_FORM_data2 = 0x05,
  48. DW_FORM_data4 = 0x06,
  49. DW_FORM_data8 = 0x07,
  50. DW_FORM_string = 0x08,
  51. DW_FORM_block = 0x09,
  52. DW_FORM_block1 = 0x0a,
  53. DW_FORM_data1 = 0x0b,
  54. DW_FORM_flag = 0x0c,
  55. DW_FORM_sdata = 0x0d,
  56. DW_FORM_strp = 0x0e,
  57. DW_FORM_udata = 0x0f,
  58. DW_FORM_ref_addr = 0x10,
  59. DW_FORM_ref1 = 0x11,
  60. DW_FORM_ref2 = 0x12,
  61. DW_FORM_ref4 = 0x13,
  62. DW_FORM_ref8 = 0x14,
  63. DW_FORM_ref_udata = 0x15,
  64. DW_FORM_indirect = 0x16,
  65. DW_FORM_sec_offset = 0x17,
  66. DW_FORM_exprloc = 0x18,
  67. DW_FORM_flag_present = 0x19,
  68. DW_FORM_ref_sig8 = 0x20,
  69. DW_FORM_strx = 0x1a,
  70. DW_FORM_addrx = 0x1b,
  71. DW_FORM_ref_sup4 = 0x1c,
  72. DW_FORM_strp_sup = 0x1d,
  73. DW_FORM_data16 = 0x1e,
  74. DW_FORM_line_strp = 0x1f,
  75. DW_FORM_implicit_const = 0x21,
  76. DW_FORM_loclistx = 0x22,
  77. DW_FORM_rnglistx = 0x23,
  78. DW_FORM_ref_sup8 = 0x24,
  79. DW_FORM_strx1 = 0x25,
  80. DW_FORM_strx2 = 0x26,
  81. DW_FORM_strx3 = 0x27,
  82. DW_FORM_strx4 = 0x28,
  83. DW_FORM_addrx1 = 0x29,
  84. DW_FORM_addrx2 = 0x2a,
  85. DW_FORM_addrx3 = 0x2b,
  86. DW_FORM_addrx4 = 0x2c,
  87. DW_FORM_GNU_addr_index = 0x1f01,
  88. DW_FORM_GNU_str_index = 0x1f02,
  89. DW_FORM_GNU_ref_alt = 0x1f20,
  90. DW_FORM_GNU_strp_alt = 0x1f21
  91. };
  92. enum dwarf_attribute {
  93. DW_AT_sibling = 0x01,
  94. DW_AT_location = 0x02,
  95. DW_AT_name = 0x03,
  96. DW_AT_ordering = 0x09,
  97. DW_AT_subscr_data = 0x0a,
  98. DW_AT_byte_size = 0x0b,
  99. DW_AT_bit_offset = 0x0c,
  100. DW_AT_bit_size = 0x0d,
  101. DW_AT_element_list = 0x0f,
  102. DW_AT_stmt_list = 0x10,
  103. DW_AT_low_pc = 0x11,
  104. DW_AT_high_pc = 0x12,
  105. DW_AT_language = 0x13,
  106. DW_AT_member = 0x14,
  107. DW_AT_discr = 0x15,
  108. DW_AT_discr_value = 0x16,
  109. DW_AT_visibility = 0x17,
  110. DW_AT_import = 0x18,
  111. DW_AT_string_length = 0x19,
  112. DW_AT_common_reference = 0x1a,
  113. DW_AT_comp_dir = 0x1b,
  114. DW_AT_const_value = 0x1c,
  115. DW_AT_containing_type = 0x1d,
  116. DW_AT_default_value = 0x1e,
  117. DW_AT_inline = 0x20,
  118. DW_AT_is_optional = 0x21,
  119. DW_AT_lower_bound = 0x22,
  120. DW_AT_producer = 0x25,
  121. DW_AT_prototyped = 0x27,
  122. DW_AT_return_addr = 0x2a,
  123. DW_AT_start_scope = 0x2c,
  124. DW_AT_bit_stride = 0x2e,
  125. DW_AT_upper_bound = 0x2f,
  126. DW_AT_abstract_origin = 0x31,
  127. DW_AT_accessibility = 0x32,
  128. DW_AT_address_class = 0x33,
  129. DW_AT_artificial = 0x34,
  130. DW_AT_base_types = 0x35,
  131. DW_AT_calling_convention = 0x36,
  132. DW_AT_count = 0x37,
  133. DW_AT_data_member_location = 0x38,
  134. DW_AT_decl_column = 0x39,
  135. DW_AT_decl_file = 0x3a,
  136. DW_AT_decl_line = 0x3b,
  137. DW_AT_declaration = 0x3c,
  138. DW_AT_discr_list = 0x3d,
  139. DW_AT_encoding = 0x3e,
  140. DW_AT_external = 0x3f,
  141. DW_AT_frame_base = 0x40,
  142. DW_AT_friend = 0x41,
  143. DW_AT_identifier_case = 0x42,
  144. DW_AT_macro_info = 0x43,
  145. DW_AT_namelist_items = 0x44,
  146. DW_AT_priority = 0x45,
  147. DW_AT_segment = 0x46,
  148. DW_AT_specification = 0x47,
  149. DW_AT_static_link = 0x48,
  150. DW_AT_type = 0x49,
  151. DW_AT_use_location = 0x4a,
  152. DW_AT_variable_parameter = 0x4b,
  153. DW_AT_virtuality = 0x4c,
  154. DW_AT_vtable_elem_location = 0x4d,
  155. DW_AT_allocated = 0x4e,
  156. DW_AT_associated = 0x4f,
  157. DW_AT_data_location = 0x50,
  158. DW_AT_byte_stride = 0x51,
  159. DW_AT_entry_pc = 0x52,
  160. DW_AT_use_UTF8 = 0x53,
  161. DW_AT_extension = 0x54,
  162. DW_AT_ranges = 0x55,
  163. DW_AT_trampoline = 0x56,
  164. DW_AT_call_column = 0x57,
  165. DW_AT_call_file = 0x58,
  166. DW_AT_call_line = 0x59,
  167. DW_AT_description = 0x5a,
  168. DW_AT_binary_scale = 0x5b,
  169. DW_AT_decimal_scale = 0x5c,
  170. DW_AT_small = 0x5d,
  171. DW_AT_decimal_sign = 0x5e,
  172. DW_AT_digit_count = 0x5f,
  173. DW_AT_picture_string = 0x60,
  174. DW_AT_mutable = 0x61,
  175. DW_AT_threads_scaled = 0x62,
  176. DW_AT_explicit = 0x63,
  177. DW_AT_object_pointer = 0x64,
  178. DW_AT_endianity = 0x65,
  179. DW_AT_elemental = 0x66,
  180. DW_AT_pure = 0x67,
  181. DW_AT_recursive = 0x68,
  182. DW_AT_signature = 0x69,
  183. DW_AT_main_subprogram = 0x6a,
  184. DW_AT_data_bit_offset = 0x6b,
  185. DW_AT_const_expr = 0x6c,
  186. DW_AT_enum_class = 0x6d,
  187. DW_AT_linkage_name = 0x6e,
  188. DW_AT_string_length_bit_size = 0x6f,
  189. DW_AT_string_length_byte_size = 0x70,
  190. DW_AT_rank = 0x71,
  191. DW_AT_str_offsets_base = 0x72,
  192. DW_AT_addr_base = 0x73,
  193. DW_AT_rnglists_base = 0x74,
  194. DW_AT_dwo_name = 0x76,
  195. DW_AT_reference = 0x77,
  196. DW_AT_rvalue_reference = 0x78,
  197. DW_AT_macros = 0x79,
  198. DW_AT_call_all_calls = 0x7a,
  199. DW_AT_call_all_source_calls = 0x7b,
  200. DW_AT_call_all_tail_calls = 0x7c,
  201. DW_AT_call_return_pc = 0x7d,
  202. DW_AT_call_value = 0x7e,
  203. DW_AT_call_origin = 0x7f,
  204. DW_AT_call_parameter = 0x80,
  205. DW_AT_call_pc = 0x81,
  206. DW_AT_call_tail_call = 0x82,
  207. DW_AT_call_target = 0x83,
  208. DW_AT_call_target_clobbered = 0x84,
  209. DW_AT_call_data_location = 0x85,
  210. DW_AT_call_data_value = 0x86,
  211. DW_AT_noreturn = 0x87,
  212. DW_AT_alignment = 0x88,
  213. DW_AT_export_symbols = 0x89,
  214. DW_AT_deleted = 0x8a,
  215. DW_AT_defaulted = 0x8b,
  216. DW_AT_loclists_base = 0x8c,
  217. DW_AT_lo_user = 0x2000,
  218. DW_AT_hi_user = 0x3fff,
  219. DW_AT_MIPS_fde = 0x2001,
  220. DW_AT_MIPS_loop_begin = 0x2002,
  221. DW_AT_MIPS_tail_loop_begin = 0x2003,
  222. DW_AT_MIPS_epilog_begin = 0x2004,
  223. DW_AT_MIPS_loop_unroll_factor = 0x2005,
  224. DW_AT_MIPS_software_pipeline_depth = 0x2006,
  225. DW_AT_MIPS_linkage_name = 0x2007,
  226. DW_AT_MIPS_stride = 0x2008,
  227. DW_AT_MIPS_abstract_name = 0x2009,
  228. DW_AT_MIPS_clone_origin = 0x200a,
  229. DW_AT_MIPS_has_inlines = 0x200b,
  230. DW_AT_HP_block_index = 0x2000,
  231. DW_AT_HP_unmodifiable = 0x2001,
  232. DW_AT_HP_prologue = 0x2005,
  233. DW_AT_HP_epilogue = 0x2008,
  234. DW_AT_HP_actuals_stmt_list = 0x2010,
  235. DW_AT_HP_proc_per_section = 0x2011,
  236. DW_AT_HP_raw_data_ptr = 0x2012,
  237. DW_AT_HP_pass_by_reference = 0x2013,
  238. DW_AT_HP_opt_level = 0x2014,
  239. DW_AT_HP_prof_version_id = 0x2015,
  240. DW_AT_HP_opt_flags = 0x2016,
  241. DW_AT_HP_cold_region_low_pc = 0x2017,
  242. DW_AT_HP_cold_region_high_pc = 0x2018,
  243. DW_AT_HP_all_variables_modifiable = 0x2019,
  244. DW_AT_HP_linkage_name = 0x201a,
  245. DW_AT_HP_prof_flags = 0x201b,
  246. DW_AT_HP_unit_name = 0x201f,
  247. DW_AT_HP_unit_size = 0x2020,
  248. DW_AT_HP_widened_byte_size = 0x2021,
  249. DW_AT_HP_definition_points = 0x2022,
  250. DW_AT_HP_default_location = 0x2023,
  251. DW_AT_HP_is_result_param = 0x2029,
  252. DW_AT_sf_names = 0x2101,
  253. DW_AT_src_info = 0x2102,
  254. DW_AT_mac_info = 0x2103,
  255. DW_AT_src_coords = 0x2104,
  256. DW_AT_body_begin = 0x2105,
  257. DW_AT_body_end = 0x2106,
  258. DW_AT_GNU_vector = 0x2107,
  259. DW_AT_GNU_guarded_by = 0x2108,
  260. DW_AT_GNU_pt_guarded_by = 0x2109,
  261. DW_AT_GNU_guarded = 0x210a,
  262. DW_AT_GNU_pt_guarded = 0x210b,
  263. DW_AT_GNU_locks_excluded = 0x210c,
  264. DW_AT_GNU_exclusive_locks_required = 0x210d,
  265. DW_AT_GNU_shared_locks_required = 0x210e,
  266. DW_AT_GNU_odr_signature = 0x210f,
  267. DW_AT_GNU_template_name = 0x2110,
  268. DW_AT_GNU_call_site_value = 0x2111,
  269. DW_AT_GNU_call_site_data_value = 0x2112,
  270. DW_AT_GNU_call_site_target = 0x2113,
  271. DW_AT_GNU_call_site_target_clobbered = 0x2114,
  272. DW_AT_GNU_tail_call = 0x2115,
  273. DW_AT_GNU_all_tail_call_sites = 0x2116,
  274. DW_AT_GNU_all_call_sites = 0x2117,
  275. DW_AT_GNU_all_source_call_sites = 0x2118,
  276. DW_AT_GNU_macros = 0x2119,
  277. DW_AT_GNU_deleted = 0x211a,
  278. DW_AT_GNU_dwo_name = 0x2130,
  279. DW_AT_GNU_dwo_id = 0x2131,
  280. DW_AT_GNU_ranges_base = 0x2132,
  281. DW_AT_GNU_addr_base = 0x2133,
  282. DW_AT_GNU_pubnames = 0x2134,
  283. DW_AT_GNU_pubtypes = 0x2135,
  284. DW_AT_GNU_discriminator = 0x2136,
  285. DW_AT_GNU_locviews = 0x2137,
  286. DW_AT_GNU_entry_view = 0x2138,
  287. DW_AT_VMS_rtnbeg_pd_address = 0x2201,
  288. DW_AT_use_GNAT_descriptive_type = 0x2301,
  289. DW_AT_GNAT_descriptive_type = 0x2302,
  290. DW_AT_GNU_numerator = 0x2303,
  291. DW_AT_GNU_denominator = 0x2304,
  292. DW_AT_GNU_bias = 0x2305,
  293. DW_AT_upc_threads_scaled = 0x3210,
  294. DW_AT_PGI_lbase = 0x3a00,
  295. DW_AT_PGI_soffset = 0x3a01,
  296. DW_AT_PGI_lstride = 0x3a02,
  297. DW_AT_APPLE_optimized = 0x3fe1,
  298. DW_AT_APPLE_flags = 0x3fe2,
  299. DW_AT_APPLE_isa = 0x3fe3,
  300. DW_AT_APPLE_block = 0x3fe4,
  301. DW_AT_APPLE_major_runtime_vers = 0x3fe5,
  302. DW_AT_APPLE_runtime_class = 0x3fe6,
  303. DW_AT_APPLE_omit_frame_ptr = 0x3fe7,
  304. DW_AT_APPLE_property_name = 0x3fe8,
  305. DW_AT_APPLE_property_getter = 0x3fe9,
  306. DW_AT_APPLE_property_setter = 0x3fea,
  307. DW_AT_APPLE_property_attribute = 0x3feb,
  308. DW_AT_APPLE_objc_complete_type = 0x3fec,
  309. DW_AT_APPLE_property = 0x3fed
  310. };
  311. enum dwarf_line_number_op {
  312. DW_LNS_extended_op = 0x0,
  313. DW_LNS_copy = 0x1,
  314. DW_LNS_advance_pc = 0x2,
  315. DW_LNS_advance_line = 0x3,
  316. DW_LNS_set_file = 0x4,
  317. DW_LNS_set_column = 0x5,
  318. DW_LNS_negate_stmt = 0x6,
  319. DW_LNS_set_basic_block = 0x7,
  320. DW_LNS_const_add_pc = 0x8,
  321. DW_LNS_fixed_advance_pc = 0x9,
  322. DW_LNS_set_prologue_end = 0xa,
  323. DW_LNS_set_epilogue_begin = 0xb,
  324. DW_LNS_set_isa = 0xc,
  325. };
  326. enum dwarf_extended_line_number_op {
  327. DW_LNE_end_sequence = 0x1,
  328. DW_LNE_set_address = 0x2,
  329. DW_LNE_define_file = 0x3,
  330. DW_LNE_set_discriminator = 0x4,
  331. };
  332. enum dwarf_line_number_content_type {
  333. DW_LNCT_path = 0x1,
  334. DW_LNCT_directory_index = 0x2,
  335. DW_LNCT_timestamp = 0x3,
  336. DW_LNCT_size = 0x4,
  337. DW_LNCT_MD5 = 0x5,
  338. DW_LNCT_lo_user = 0x2000,
  339. DW_LNCT_hi_user = 0x3fff
  340. };
  341. enum dwarf_range_list_entry {
  342. DW_RLE_end_of_list = 0x00,
  343. DW_RLE_base_addressx = 0x01,
  344. DW_RLE_startx_endx = 0x02,
  345. DW_RLE_startx_length = 0x03,
  346. DW_RLE_offset_pair = 0x04,
  347. DW_RLE_base_address = 0x05,
  348. DW_RLE_start_end = 0x06,
  349. DW_RLE_start_length = 0x07
  350. };
  351. enum dwarf_unit_type {
  352. DW_UT_compile = 0x01,
  353. DW_UT_type = 0x02,
  354. DW_UT_partial = 0x03,
  355. DW_UT_skeleton = 0x04,
  356. DW_UT_split_compile = 0x05,
  357. DW_UT_split_type = 0x06,
  358. DW_UT_lo_user = 0x80,
  359. DW_UT_hi_user = 0xff
  360. };
  361. #if !defined(HAVE_DECL_STRNLEN) || !HAVE_DECL_STRNLEN
  362. /* If strnlen is not declared, provide our own version. */
  363. static size_t
  364. xstrnlen (const char *s, size_t maxlen)
  365. {
  366. size_t i;
  367. for (i = 0; i < maxlen; ++i)
  368. if (s[i] == '\0')
  369. break;
  370. return i;
  371. }
  372. #define strnlen xstrnlen
  373. #endif
  374. /* A buffer to read DWARF info. */
  375. struct dwarf_buf
  376. {
  377. /* Buffer name for error messages. */
  378. const char *name;
  379. /* Start of the buffer. */
  380. const unsigned char *start;
  381. /* Next byte to read. */
  382. const unsigned char *buf;
  383. /* The number of bytes remaining. */
  384. size_t left;
  385. /* Whether the data is big-endian. */
  386. int is_bigendian;
  387. /* Error callback routine. */
  388. backtrace_error_callback error_callback;
  389. /* Data for error_callback. */
  390. void *data;
  391. /* Non-zero if we've reported an underflow error. */
  392. int reported_underflow;
  393. };
  394. /* A single attribute in a DWARF abbreviation. */
  395. struct attr
  396. {
  397. /* The attribute name. */
  398. enum dwarf_attribute name;
  399. /* The attribute form. */
  400. enum dwarf_form form;
  401. /* The attribute value, for DW_FORM_implicit_const. */
  402. int64_t val;
  403. };
  404. /* A single DWARF abbreviation. */
  405. struct abbrev
  406. {
  407. /* The abbrev code--the number used to refer to the abbrev. */
  408. uint64_t code;
  409. /* The entry tag. */
  410. enum dwarf_tag tag;
  411. /* Non-zero if this abbrev has child entries. */
  412. int has_children;
  413. /* The number of attributes. */
  414. size_t num_attrs;
  415. /* The attributes. */
  416. struct attr *attrs;
  417. };
  418. /* The DWARF abbreviations for a compilation unit. This structure
  419. only exists while reading the compilation unit. Most DWARF readers
  420. seem to a hash table to map abbrev ID's to abbrev entries.
  421. However, we primarily care about GCC, and GCC simply issues ID's in
  422. numerical order starting at 1. So we simply keep a sorted vector,
  423. and try to just look up the code. */
  424. struct abbrevs
  425. {
  426. /* The number of abbrevs in the vector. */
  427. size_t num_abbrevs;
  428. /* The abbrevs, sorted by the code field. */
  429. struct abbrev *abbrevs;
  430. };
  431. /* The different kinds of attribute values. */
  432. enum attr_val_encoding
  433. {
  434. /* No attribute value. */
  435. ATTR_VAL_NONE,
  436. /* An address. */
  437. ATTR_VAL_ADDRESS,
  438. /* An index into the .debug_addr section, whose value is relative to
  439. the DW_AT_addr_base attribute of the compilation unit. */
  440. ATTR_VAL_ADDRESS_INDEX,
  441. /* A unsigned integer. */
  442. ATTR_VAL_UINT,
  443. /* A sigd integer. */
  444. ATTR_VAL_SINT,
  445. /* A string. */
  446. ATTR_VAL_STRING,
  447. /* An index into the .debug_str_offsets section. */
  448. ATTR_VAL_STRING_INDEX,
  449. /* An offset to other data in the containing unit. */
  450. ATTR_VAL_REF_UNIT,
  451. /* An offset to other data within the .debug_info section. */
  452. ATTR_VAL_REF_INFO,
  453. /* An offset to other data within the alt .debug_info section. */
  454. ATTR_VAL_REF_ALT_INFO,
  455. /* An offset to data in some other section. */
  456. ATTR_VAL_REF_SECTION,
  457. /* A type signature. */
  458. ATTR_VAL_REF_TYPE,
  459. /* An index into the .debug_rnglists section. */
  460. ATTR_VAL_RNGLISTS_INDEX,
  461. /* A block of data (not represented). */
  462. ATTR_VAL_BLOCK,
  463. /* An expression (not represented). */
  464. ATTR_VAL_EXPR,
  465. };
  466. /* An attribute value. */
  467. struct attr_val
  468. {
  469. /* How the value is stored in the field u. */
  470. enum attr_val_encoding encoding;
  471. union
  472. {
  473. /* ATTR_VAL_ADDRESS*, ATTR_VAL_UINT, ATTR_VAL_REF*. */
  474. uint64_t uint;
  475. /* ATTR_VAL_SINT. */
  476. int64_t sint;
  477. /* ATTR_VAL_STRING. */
  478. const char *string;
  479. /* ATTR_VAL_BLOCK not stored. */
  480. } u;
  481. };
  482. /* The line number program header. */
  483. struct line_header
  484. {
  485. /* The version of the line number information. */
  486. int version;
  487. /* Address size. */
  488. int addrsize;
  489. /* The minimum instruction length. */
  490. unsigned int min_insn_len;
  491. /* The maximum number of ops per instruction. */
  492. unsigned int max_ops_per_insn;
  493. /* The line base for special opcodes. */
  494. int line_base;
  495. /* The line range for special opcodes. */
  496. unsigned int line_range;
  497. /* The opcode base--the first special opcode. */
  498. unsigned int opcode_base;
  499. /* Opcode lengths, indexed by opcode - 1. */
  500. const unsigned char *opcode_lengths;
  501. /* The number of directory entries. */
  502. size_t dirs_count;
  503. /* The directory entries. */
  504. const char **dirs;
  505. /* The number of filenames. */
  506. size_t filenames_count;
  507. /* The filenames. */
  508. const char **filenames;
  509. };
  510. /* A format description from a line header. */
  511. struct line_header_format
  512. {
  513. int lnct; /* LNCT code. */
  514. enum dwarf_form form; /* Form of entry data. */
  515. };
  516. /* Map a single PC value to a file/line. We will keep a vector of
  517. these sorted by PC value. Each file/line will be correct from the
  518. PC up to the PC of the next entry if there is one. We allocate one
  519. extra entry at the end so that we can use bsearch. */
  520. struct line
  521. {
  522. /* PC. */
  523. uintptr_t pc;
  524. /* File name. Many entries in the array are expected to point to
  525. the same file name. */
  526. const char *filename;
  527. /* Line number. */
  528. int lineno;
  529. /* Index of the object in the original array read from the DWARF
  530. section, before it has been sorted. The index makes it possible
  531. to use Quicksort and maintain stability. */
  532. int idx;
  533. };
  534. /* A growable vector of line number information. This is used while
  535. reading the line numbers. */
  536. struct line_vector
  537. {
  538. /* Memory. This is an array of struct line. */
  539. struct backtrace_vector vec;
  540. /* Number of valid mappings. */
  541. size_t count;
  542. };
  543. /* A function described in the debug info. */
  544. struct function
  545. {
  546. /* The name of the function. */
  547. const char *name;
  548. /* If this is an inlined function, the filename of the call
  549. site. */
  550. const char *caller_filename;
  551. /* If this is an inlined function, the line number of the call
  552. site. */
  553. int caller_lineno;
  554. /* Map PC ranges to inlined functions. */
  555. struct function_addrs *function_addrs;
  556. size_t function_addrs_count;
  557. };
  558. /* An address range for a function. This maps a PC value to a
  559. specific function. */
  560. struct function_addrs
  561. {
  562. /* Range is LOW <= PC < HIGH. */
  563. uintptr_t low;
  564. uintptr_t high;
  565. /* Function for this address range. */
  566. struct function *function;
  567. };
  568. /* A growable vector of function address ranges. */
  569. struct function_vector
  570. {
  571. /* Memory. This is an array of struct function_addrs. */
  572. struct backtrace_vector vec;
  573. /* Number of address ranges present. */
  574. size_t count;
  575. };
  576. /* A DWARF compilation unit. This only holds the information we need
  577. to map a PC to a file and line. */
  578. struct unit
  579. {
  580. /* The first entry for this compilation unit. */
  581. const unsigned char *unit_data;
  582. /* The length of the data for this compilation unit. */
  583. size_t unit_data_len;
  584. /* The offset of UNIT_DATA from the start of the information for
  585. this compilation unit. */
  586. size_t unit_data_offset;
  587. /* Offset of the start of the compilation unit from the start of the
  588. .debug_info section. */
  589. size_t low_offset;
  590. /* Offset of the end of the compilation unit from the start of the
  591. .debug_info section. */
  592. size_t high_offset;
  593. /* DWARF version. */
  594. int version;
  595. /* Whether unit is DWARF64. */
  596. int is_dwarf64;
  597. /* Address size. */
  598. int addrsize;
  599. /* Offset into line number information. */
  600. off_t lineoff;
  601. /* Offset of compilation unit in .debug_str_offsets. */
  602. uint64_t str_offsets_base;
  603. /* Offset of compilation unit in .debug_addr. */
  604. uint64_t addr_base;
  605. /* Offset of compilation unit in .debug_rnglists. */
  606. uint64_t rnglists_base;
  607. /* Primary source file. */
  608. const char *filename;
  609. /* Compilation command working directory. */
  610. const char *comp_dir;
  611. /* Absolute file name, only set if needed. */
  612. const char *abs_filename;
  613. /* The abbreviations for this unit. */
  614. struct abbrevs abbrevs;
  615. /* The fields above this point are read in during initialization and
  616. may be accessed freely. The fields below this point are read in
  617. as needed, and therefore require care, as different threads may
  618. try to initialize them simultaneously. */
  619. /* PC to line number mapping. This is NULL if the values have not
  620. been read. This is (struct line *) -1 if there was an error
  621. reading the values. */
  622. struct line *lines;
  623. /* Number of entries in lines. */
  624. size_t lines_count;
  625. /* PC ranges to function. */
  626. struct function_addrs *function_addrs;
  627. size_t function_addrs_count;
  628. };
  629. /* An address range for a compilation unit. This maps a PC value to a
  630. specific compilation unit. Note that we invert the representation
  631. in DWARF: instead of listing the units and attaching a list of
  632. ranges, we list the ranges and have each one point to the unit.
  633. This lets us do a binary search to find the unit. */
  634. struct unit_addrs
  635. {
  636. /* Range is LOW <= PC < HIGH. */
  637. uintptr_t low;
  638. uintptr_t high;
  639. /* Compilation unit for this address range. */
  640. struct unit *u;
  641. };
  642. /* A growable vector of compilation unit address ranges. */
  643. struct unit_addrs_vector
  644. {
  645. /* Memory. This is an array of struct unit_addrs. */
  646. struct backtrace_vector vec;
  647. /* Number of address ranges present. */
  648. size_t count;
  649. };
  650. /* A growable vector of compilation unit pointer. */
  651. struct unit_vector
  652. {
  653. struct backtrace_vector vec;
  654. size_t count;
  655. };
  656. /* The information we need to map a PC to a file and line. */
  657. struct dwarf_data
  658. {
  659. /* The data for the next file we know about. */
  660. struct dwarf_data *next;
  661. /* The data for .gnu_debugaltlink. */
  662. struct dwarf_data *altlink;
  663. /* The base address mapping for this file. */
  664. struct libbacktrace_base_address base_address;
  665. /* A sorted list of address ranges. */
  666. struct unit_addrs *addrs;
  667. /* Number of address ranges in list. */
  668. size_t addrs_count;
  669. /* A sorted list of units. */
  670. struct unit **units;
  671. /* Number of units in the list. */
  672. size_t units_count;
  673. /* The unparsed DWARF debug data. */
  674. struct dwarf_sections dwarf_sections;
  675. /* Whether the data is big-endian or not. */
  676. int is_bigendian;
  677. /* A vector used for function addresses. We keep this here so that
  678. we can grow the vector as we read more functions. */
  679. struct function_vector fvec;
  680. };
  681. /* Report an error for a DWARF buffer. */
  682. static void
  683. dwarf_buf_error (struct dwarf_buf *buf, const char *msg, int errnum)
  684. {
  685. char b[200];
  686. snprintf (b, sizeof b, "%s in %s at %d",
  687. msg, buf->name, (int) (buf->buf - buf->start));
  688. buf->error_callback (buf->data, b, errnum);
  689. }
  690. /* Require at least COUNT bytes in BUF. Return 1 if all is well, 0 on
  691. error. */
  692. static int
  693. require (struct dwarf_buf *buf, size_t count)
  694. {
  695. if (buf->left >= count)
  696. return 1;
  697. if (!buf->reported_underflow)
  698. {
  699. dwarf_buf_error (buf, "DWARF underflow", 0);
  700. buf->reported_underflow = 1;
  701. }
  702. return 0;
  703. }
  704. /* Advance COUNT bytes in BUF. Return 1 if all is well, 0 on
  705. error. */
  706. static int
  707. advance (struct dwarf_buf *buf, size_t count)
  708. {
  709. if (!require (buf, count))
  710. return 0;
  711. buf->buf += count;
  712. buf->left -= count;
  713. return 1;
  714. }
  715. /* Read one zero-terminated string from BUF and advance past the string. */
  716. static const char *
  717. read_string (struct dwarf_buf *buf)
  718. {
  719. const char *p = (const char *)buf->buf;
  720. size_t len = strnlen (p, buf->left);
  721. /* - If len == left, we ran out of buffer before finding the zero terminator.
  722. Generate an error by advancing len + 1.
  723. - If len < left, advance by len + 1 to skip past the zero terminator. */
  724. size_t count = len + 1;
  725. if (!advance (buf, count))
  726. return NULL;
  727. return p;
  728. }
  729. /* Read one byte from BUF and advance 1 byte. */
  730. static unsigned char
  731. read_byte (struct dwarf_buf *buf)
  732. {
  733. const unsigned char *p = buf->buf;
  734. if (!advance (buf, 1))
  735. return 0;
  736. return p[0];
  737. }
  738. /* Read a signed char from BUF and advance 1 byte. */
  739. static signed char
  740. read_sbyte (struct dwarf_buf *buf)
  741. {
  742. const unsigned char *p = buf->buf;
  743. if (!advance (buf, 1))
  744. return 0;
  745. return (*p ^ 0x80) - 0x80;
  746. }
  747. /* Read a uint16 from BUF and advance 2 bytes. */
  748. static uint16_t
  749. read_uint16 (struct dwarf_buf *buf)
  750. {
  751. const unsigned char *p = buf->buf;
  752. if (!advance (buf, 2))
  753. return 0;
  754. if (buf->is_bigendian)
  755. return ((uint16_t) p[0] << 8) | (uint16_t) p[1];
  756. else
  757. return ((uint16_t) p[1] << 8) | (uint16_t) p[0];
  758. }
  759. /* Read a 24 bit value from BUF and advance 3 bytes. */
  760. static uint32_t
  761. read_uint24 (struct dwarf_buf *buf)
  762. {
  763. const unsigned char *p = buf->buf;
  764. if (!advance (buf, 3))
  765. return 0;
  766. if (buf->is_bigendian)
  767. return (((uint32_t) p[0] << 16) | ((uint32_t) p[1] << 8)
  768. | (uint32_t) p[2]);
  769. else
  770. return (((uint32_t) p[2] << 16) | ((uint32_t) p[1] << 8)
  771. | (uint32_t) p[0]);
  772. }
  773. /* Read a uint32 from BUF and advance 4 bytes. */
  774. static uint32_t
  775. read_uint32 (struct dwarf_buf *buf)
  776. {
  777. const unsigned char *p = buf->buf;
  778. if (!advance (buf, 4))
  779. return 0;
  780. if (buf->is_bigendian)
  781. return (((uint32_t) p[0] << 24) | ((uint32_t) p[1] << 16)
  782. | ((uint32_t) p[2] << 8) | (uint32_t) p[3]);
  783. else
  784. return (((uint32_t) p[3] << 24) | ((uint32_t) p[2] << 16)
  785. | ((uint32_t) p[1] << 8) | (uint32_t) p[0]);
  786. }
  787. /* Read a uint64 from BUF and advance 8 bytes. */
  788. static uint64_t
  789. read_uint64 (struct dwarf_buf *buf)
  790. {
  791. const unsigned char *p = buf->buf;
  792. if (!advance (buf, 8))
  793. return 0;
  794. if (buf->is_bigendian)
  795. return (((uint64_t) p[0] << 56) | ((uint64_t) p[1] << 48)
  796. | ((uint64_t) p[2] << 40) | ((uint64_t) p[3] << 32)
  797. | ((uint64_t) p[4] << 24) | ((uint64_t) p[5] << 16)
  798. | ((uint64_t) p[6] << 8) | (uint64_t) p[7]);
  799. else
  800. return (((uint64_t) p[7] << 56) | ((uint64_t) p[6] << 48)
  801. | ((uint64_t) p[5] << 40) | ((uint64_t) p[4] << 32)
  802. | ((uint64_t) p[3] << 24) | ((uint64_t) p[2] << 16)
  803. | ((uint64_t) p[1] << 8) | (uint64_t) p[0]);
  804. }
  805. /* Read an offset from BUF and advance the appropriate number of
  806. bytes. */
  807. static uint64_t
  808. read_offset (struct dwarf_buf *buf, int is_dwarf64)
  809. {
  810. if (is_dwarf64)
  811. return read_uint64 (buf);
  812. else
  813. return read_uint32 (buf);
  814. }
  815. /* Read an address from BUF and advance the appropriate number of
  816. bytes. */
  817. static uint64_t
  818. read_address (struct dwarf_buf *buf, int addrsize)
  819. {
  820. switch (addrsize)
  821. {
  822. case 1:
  823. return read_byte (buf);
  824. case 2:
  825. return read_uint16 (buf);
  826. case 4:
  827. return read_uint32 (buf);
  828. case 8:
  829. return read_uint64 (buf);
  830. default:
  831. dwarf_buf_error (buf, "unrecognized address size", 0);
  832. return 0;
  833. }
  834. }
  835. /* Return whether a value is the highest possible address, given the
  836. address size. */
  837. static int
  838. is_highest_address (uint64_t address, int addrsize)
  839. {
  840. switch (addrsize)
  841. {
  842. case 1:
  843. return address == (unsigned char) -1;
  844. case 2:
  845. return address == (uint16_t) -1;
  846. case 4:
  847. return address == (uint32_t) -1;
  848. case 8:
  849. return address == (uint64_t) -1;
  850. default:
  851. return 0;
  852. }
  853. }
  854. /* Read an unsigned LEB128 number. */
  855. static uint64_t
  856. read_uleb128 (struct dwarf_buf *buf)
  857. {
  858. uint64_t ret;
  859. unsigned int shift;
  860. int overflow;
  861. unsigned char b;
  862. ret = 0;
  863. shift = 0;
  864. overflow = 0;
  865. do
  866. {
  867. const unsigned char *p;
  868. p = buf->buf;
  869. if (!advance (buf, 1))
  870. return 0;
  871. b = *p;
  872. if (shift < 64)
  873. ret |= ((uint64_t) (b & 0x7f)) << shift;
  874. else if (!overflow)
  875. {
  876. dwarf_buf_error (buf, "LEB128 overflows uint64_t", 0);
  877. overflow = 1;
  878. }
  879. shift += 7;
  880. }
  881. while ((b & 0x80) != 0);
  882. return ret;
  883. }
  884. /* Read a signed LEB128 number. */
  885. static int64_t
  886. read_sleb128 (struct dwarf_buf *buf)
  887. {
  888. uint64_t val;
  889. unsigned int shift;
  890. int overflow;
  891. unsigned char b;
  892. val = 0;
  893. shift = 0;
  894. overflow = 0;
  895. do
  896. {
  897. const unsigned char *p;
  898. p = buf->buf;
  899. if (!advance (buf, 1))
  900. return 0;
  901. b = *p;
  902. if (shift < 64)
  903. val |= ((uint64_t) (b & 0x7f)) << shift;
  904. else if (!overflow)
  905. {
  906. dwarf_buf_error (buf, "signed LEB128 overflows uint64_t", 0);
  907. overflow = 1;
  908. }
  909. shift += 7;
  910. }
  911. while ((b & 0x80) != 0);
  912. if ((b & 0x40) != 0 && shift < 64)
  913. val |= ((uint64_t) -1) << shift;
  914. return (int64_t) val;
  915. }
  916. /* Return the length of an LEB128 number. */
  917. static size_t
  918. leb128_len (const unsigned char *p)
  919. {
  920. size_t ret;
  921. ret = 1;
  922. while ((*p & 0x80) != 0)
  923. {
  924. ++p;
  925. ++ret;
  926. }
  927. return ret;
  928. }
  929. /* Read initial_length from BUF and advance the appropriate number of bytes. */
  930. static uint64_t
  931. read_initial_length (struct dwarf_buf *buf, int *is_dwarf64)
  932. {
  933. uint64_t len;
  934. len = read_uint32 (buf);
  935. if (len == 0xffffffff)
  936. {
  937. len = read_uint64 (buf);
  938. *is_dwarf64 = 1;
  939. }
  940. else
  941. *is_dwarf64 = 0;
  942. return len;
  943. }
  944. /* Free an abbreviations structure. */
  945. static void
  946. free_abbrevs (struct backtrace_state *state, struct abbrevs *abbrevs,
  947. backtrace_error_callback error_callback, void *data)
  948. {
  949. size_t i;
  950. for (i = 0; i < abbrevs->num_abbrevs; ++i)
  951. backtrace_free (state, abbrevs->abbrevs[i].attrs,
  952. abbrevs->abbrevs[i].num_attrs * sizeof (struct attr),
  953. error_callback, data);
  954. backtrace_free (state, abbrevs->abbrevs,
  955. abbrevs->num_abbrevs * sizeof (struct abbrev),
  956. error_callback, data);
  957. abbrevs->num_abbrevs = 0;
  958. abbrevs->abbrevs = NULL;
  959. }
  960. /* Read an attribute value. Returns 1 on success, 0 on failure. If
  961. the value can be represented as a uint64_t, sets *VAL and sets
  962. *IS_VALID to 1. We don't try to store the value of other attribute
  963. forms, because we don't care about them. */
  964. static int
  965. read_attribute (enum dwarf_form form, uint64_t implicit_val,
  966. struct dwarf_buf *buf, int is_dwarf64, int version,
  967. int addrsize, const struct dwarf_sections *dwarf_sections,
  968. struct dwarf_data *altlink, struct attr_val *val)
  969. {
  970. /* Avoid warnings about val.u.FIELD may be used uninitialized if
  971. this function is inlined. The warnings aren't valid but can
  972. occur because the different fields are set and used
  973. conditionally. */
  974. memset (val, 0, sizeof *val);
  975. switch (form)
  976. {
  977. case DW_FORM_addr:
  978. val->encoding = ATTR_VAL_ADDRESS;
  979. val->u.uint = read_address (buf, addrsize);
  980. return 1;
  981. case DW_FORM_block2:
  982. val->encoding = ATTR_VAL_BLOCK;
  983. return advance (buf, read_uint16 (buf));
  984. case DW_FORM_block4:
  985. val->encoding = ATTR_VAL_BLOCK;
  986. return advance (buf, read_uint32 (buf));
  987. case DW_FORM_data2:
  988. val->encoding = ATTR_VAL_UINT;
  989. val->u.uint = read_uint16 (buf);
  990. return 1;
  991. case DW_FORM_data4:
  992. val->encoding = ATTR_VAL_UINT;
  993. val->u.uint = read_uint32 (buf);
  994. return 1;
  995. case DW_FORM_data8:
  996. val->encoding = ATTR_VAL_UINT;
  997. val->u.uint = read_uint64 (buf);
  998. return 1;
  999. case DW_FORM_data16:
  1000. val->encoding = ATTR_VAL_BLOCK;
  1001. return advance (buf, 16);
  1002. case DW_FORM_string:
  1003. val->encoding = ATTR_VAL_STRING;
  1004. val->u.string = read_string (buf);
  1005. return val->u.string == NULL ? 0 : 1;
  1006. case DW_FORM_block:
  1007. val->encoding = ATTR_VAL_BLOCK;
  1008. return advance (buf, read_uleb128 (buf));
  1009. case DW_FORM_block1:
  1010. val->encoding = ATTR_VAL_BLOCK;
  1011. return advance (buf, read_byte (buf));
  1012. case DW_FORM_data1:
  1013. val->encoding = ATTR_VAL_UINT;
  1014. val->u.uint = read_byte (buf);
  1015. return 1;
  1016. case DW_FORM_flag:
  1017. val->encoding = ATTR_VAL_UINT;
  1018. val->u.uint = read_byte (buf);
  1019. return 1;
  1020. case DW_FORM_sdata:
  1021. val->encoding = ATTR_VAL_SINT;
  1022. val->u.sint = read_sleb128 (buf);
  1023. return 1;
  1024. case DW_FORM_strp:
  1025. {
  1026. uint64_t offset;
  1027. offset = read_offset (buf, is_dwarf64);
  1028. if (offset >= dwarf_sections->size[DEBUG_STR])
  1029. {
  1030. dwarf_buf_error (buf, "DW_FORM_strp out of range", 0);
  1031. return 0;
  1032. }
  1033. val->encoding = ATTR_VAL_STRING;
  1034. val->u.string =
  1035. (const char *) dwarf_sections->data[DEBUG_STR] + offset;
  1036. return 1;
  1037. }
  1038. case DW_FORM_line_strp:
  1039. {
  1040. uint64_t offset;
  1041. offset = read_offset (buf, is_dwarf64);
  1042. if (offset >= dwarf_sections->size[DEBUG_LINE_STR])
  1043. {
  1044. dwarf_buf_error (buf, "DW_FORM_line_strp out of range", 0);
  1045. return 0;
  1046. }
  1047. val->encoding = ATTR_VAL_STRING;
  1048. val->u.string =
  1049. (const char *) dwarf_sections->data[DEBUG_LINE_STR] + offset;
  1050. return 1;
  1051. }
  1052. case DW_FORM_udata:
  1053. val->encoding = ATTR_VAL_UINT;
  1054. val->u.uint = read_uleb128 (buf);
  1055. return 1;
  1056. case DW_FORM_ref_addr:
  1057. val->encoding = ATTR_VAL_REF_INFO;
  1058. if (version == 2)
  1059. val->u.uint = read_address (buf, addrsize);
  1060. else
  1061. val->u.uint = read_offset (buf, is_dwarf64);
  1062. return 1;
  1063. case DW_FORM_ref1:
  1064. val->encoding = ATTR_VAL_REF_UNIT;
  1065. val->u.uint = read_byte (buf);
  1066. return 1;
  1067. case DW_FORM_ref2:
  1068. val->encoding = ATTR_VAL_REF_UNIT;
  1069. val->u.uint = read_uint16 (buf);
  1070. return 1;
  1071. case DW_FORM_ref4:
  1072. val->encoding = ATTR_VAL_REF_UNIT;
  1073. val->u.uint = read_uint32 (buf);
  1074. return 1;
  1075. case DW_FORM_ref8:
  1076. val->encoding = ATTR_VAL_REF_UNIT;
  1077. val->u.uint = read_uint64 (buf);
  1078. return 1;
  1079. case DW_FORM_ref_udata:
  1080. val->encoding = ATTR_VAL_REF_UNIT;
  1081. val->u.uint = read_uleb128 (buf);
  1082. return 1;
  1083. case DW_FORM_indirect:
  1084. {
  1085. uint64_t form;
  1086. form = read_uleb128 (buf);
  1087. if (form == DW_FORM_implicit_const)
  1088. {
  1089. dwarf_buf_error (buf,
  1090. "DW_FORM_indirect to DW_FORM_implicit_const",
  1091. 0);
  1092. return 0;
  1093. }
  1094. return read_attribute ((enum dwarf_form) form, 0, buf, is_dwarf64,
  1095. version, addrsize, dwarf_sections, altlink,
  1096. val);
  1097. }
  1098. case DW_FORM_sec_offset:
  1099. val->encoding = ATTR_VAL_REF_SECTION;
  1100. val->u.uint = read_offset (buf, is_dwarf64);
  1101. return 1;
  1102. case DW_FORM_exprloc:
  1103. val->encoding = ATTR_VAL_EXPR;
  1104. return advance (buf, read_uleb128 (buf));
  1105. case DW_FORM_flag_present:
  1106. val->encoding = ATTR_VAL_UINT;
  1107. val->u.uint = 1;
  1108. return 1;
  1109. case DW_FORM_ref_sig8:
  1110. val->encoding = ATTR_VAL_REF_TYPE;
  1111. val->u.uint = read_uint64 (buf);
  1112. return 1;
  1113. case DW_FORM_strx: case DW_FORM_strx1: case DW_FORM_strx2:
  1114. case DW_FORM_strx3: case DW_FORM_strx4:
  1115. {
  1116. uint64_t offset;
  1117. switch (form)
  1118. {
  1119. case DW_FORM_strx:
  1120. offset = read_uleb128 (buf);
  1121. break;
  1122. case DW_FORM_strx1:
  1123. offset = read_byte (buf);
  1124. break;
  1125. case DW_FORM_strx2:
  1126. offset = read_uint16 (buf);
  1127. break;
  1128. case DW_FORM_strx3:
  1129. offset = read_uint24 (buf);
  1130. break;
  1131. case DW_FORM_strx4:
  1132. offset = read_uint32 (buf);
  1133. break;
  1134. default:
  1135. /* This case can't happen. */
  1136. return 0;
  1137. }
  1138. val->encoding = ATTR_VAL_STRING_INDEX;
  1139. val->u.uint = offset;
  1140. return 1;
  1141. }
  1142. case DW_FORM_addrx: case DW_FORM_addrx1: case DW_FORM_addrx2:
  1143. case DW_FORM_addrx3: case DW_FORM_addrx4:
  1144. {
  1145. uint64_t offset;
  1146. switch (form)
  1147. {
  1148. case DW_FORM_addrx:
  1149. offset = read_uleb128 (buf);
  1150. break;
  1151. case DW_FORM_addrx1:
  1152. offset = read_byte (buf);
  1153. break;
  1154. case DW_FORM_addrx2:
  1155. offset = read_uint16 (buf);
  1156. break;
  1157. case DW_FORM_addrx3:
  1158. offset = read_uint24 (buf);
  1159. break;
  1160. case DW_FORM_addrx4:
  1161. offset = read_uint32 (buf);
  1162. break;
  1163. default:
  1164. /* This case can't happen. */
  1165. return 0;
  1166. }
  1167. val->encoding = ATTR_VAL_ADDRESS_INDEX;
  1168. val->u.uint = offset;
  1169. return 1;
  1170. }
  1171. case DW_FORM_ref_sup4:
  1172. val->encoding = ATTR_VAL_REF_SECTION;
  1173. val->u.uint = read_uint32 (buf);
  1174. return 1;
  1175. case DW_FORM_ref_sup8:
  1176. val->encoding = ATTR_VAL_REF_SECTION;
  1177. val->u.uint = read_uint64 (buf);
  1178. return 1;
  1179. case DW_FORM_implicit_const:
  1180. val->encoding = ATTR_VAL_UINT;
  1181. val->u.uint = implicit_val;
  1182. return 1;
  1183. case DW_FORM_loclistx:
  1184. /* We don't distinguish this from DW_FORM_sec_offset. It
  1185. * shouldn't matter since we don't care about loclists. */
  1186. val->encoding = ATTR_VAL_REF_SECTION;
  1187. val->u.uint = read_uleb128 (buf);
  1188. return 1;
  1189. case DW_FORM_rnglistx:
  1190. val->encoding = ATTR_VAL_RNGLISTS_INDEX;
  1191. val->u.uint = read_uleb128 (buf);
  1192. return 1;
  1193. case DW_FORM_GNU_addr_index:
  1194. val->encoding = ATTR_VAL_REF_SECTION;
  1195. val->u.uint = read_uleb128 (buf);
  1196. return 1;
  1197. case DW_FORM_GNU_str_index:
  1198. val->encoding = ATTR_VAL_REF_SECTION;
  1199. val->u.uint = read_uleb128 (buf);
  1200. return 1;
  1201. case DW_FORM_GNU_ref_alt:
  1202. val->u.uint = read_offset (buf, is_dwarf64);
  1203. if (altlink == NULL)
  1204. {
  1205. val->encoding = ATTR_VAL_NONE;
  1206. return 1;
  1207. }
  1208. val->encoding = ATTR_VAL_REF_ALT_INFO;
  1209. return 1;
  1210. case DW_FORM_strp_sup: case DW_FORM_GNU_strp_alt:
  1211. {
  1212. uint64_t offset;
  1213. offset = read_offset (buf, is_dwarf64);
  1214. if (altlink == NULL)
  1215. {
  1216. val->encoding = ATTR_VAL_NONE;
  1217. return 1;
  1218. }
  1219. if (offset >= altlink->dwarf_sections.size[DEBUG_STR])
  1220. {
  1221. dwarf_buf_error (buf, "DW_FORM_strp_sup out of range", 0);
  1222. return 0;
  1223. }
  1224. val->encoding = ATTR_VAL_STRING;
  1225. val->u.string =
  1226. (const char *) altlink->dwarf_sections.data[DEBUG_STR] + offset;
  1227. return 1;
  1228. }
  1229. default:
  1230. dwarf_buf_error (buf, "unrecognized DWARF form", -1);
  1231. return 0;
  1232. }
  1233. }
  1234. /* If we can determine the value of a string attribute, set *STRING to
  1235. point to the string. Return 1 on success, 0 on error. If we don't
  1236. know the value, we consider that a success, and we don't change
  1237. *STRING. An error is only reported for some sort of out of range
  1238. offset. */
  1239. static int
  1240. resolve_string (const struct dwarf_sections *dwarf_sections, int is_dwarf64,
  1241. int is_bigendian, uint64_t str_offsets_base,
  1242. const struct attr_val *val,
  1243. backtrace_error_callback error_callback, void *data,
  1244. const char **string)
  1245. {
  1246. switch (val->encoding)
  1247. {
  1248. case ATTR_VAL_STRING:
  1249. *string = val->u.string;
  1250. return 1;
  1251. case ATTR_VAL_STRING_INDEX:
  1252. {
  1253. uint64_t offset;
  1254. struct dwarf_buf offset_buf;
  1255. offset = val->u.uint * (is_dwarf64 ? 8 : 4) + str_offsets_base;
  1256. if (offset + (is_dwarf64 ? 8 : 4)
  1257. > dwarf_sections->size[DEBUG_STR_OFFSETS])
  1258. {
  1259. error_callback (data, "DW_FORM_strx value out of range", 0);
  1260. return 0;
  1261. }
  1262. offset_buf.name = ".debug_str_offsets";
  1263. offset_buf.start = dwarf_sections->data[DEBUG_STR_OFFSETS];
  1264. offset_buf.buf = dwarf_sections->data[DEBUG_STR_OFFSETS] + offset;
  1265. offset_buf.left = dwarf_sections->size[DEBUG_STR_OFFSETS] - offset;
  1266. offset_buf.is_bigendian = is_bigendian;
  1267. offset_buf.error_callback = error_callback;
  1268. offset_buf.data = data;
  1269. offset_buf.reported_underflow = 0;
  1270. offset = read_offset (&offset_buf, is_dwarf64);
  1271. if (offset >= dwarf_sections->size[DEBUG_STR])
  1272. {
  1273. dwarf_buf_error (&offset_buf,
  1274. "DW_FORM_strx offset out of range",
  1275. 0);
  1276. return 0;
  1277. }
  1278. *string = (const char *) dwarf_sections->data[DEBUG_STR] + offset;
  1279. return 1;
  1280. }
  1281. default:
  1282. return 1;
  1283. }
  1284. }
  1285. /* Set *ADDRESS to the real address for a ATTR_VAL_ADDRESS_INDEX.
  1286. Return 1 on success, 0 on error. */
  1287. static int
  1288. resolve_addr_index (const struct dwarf_sections *dwarf_sections,
  1289. uint64_t addr_base, int addrsize, int is_bigendian,
  1290. uint64_t addr_index,
  1291. backtrace_error_callback error_callback, void *data,
  1292. uintptr_t *address)
  1293. {
  1294. uint64_t offset;
  1295. struct dwarf_buf addr_buf;
  1296. offset = addr_index * addrsize + addr_base;
  1297. if (offset + addrsize > dwarf_sections->size[DEBUG_ADDR])
  1298. {
  1299. error_callback (data, "DW_FORM_addrx value out of range", 0);
  1300. return 0;
  1301. }
  1302. addr_buf.name = ".debug_addr";
  1303. addr_buf.start = dwarf_sections->data[DEBUG_ADDR];
  1304. addr_buf.buf = dwarf_sections->data[DEBUG_ADDR] + offset;
  1305. addr_buf.left = dwarf_sections->size[DEBUG_ADDR] - offset;
  1306. addr_buf.is_bigendian = is_bigendian;
  1307. addr_buf.error_callback = error_callback;
  1308. addr_buf.data = data;
  1309. addr_buf.reported_underflow = 0;
  1310. *address = (uintptr_t) read_address (&addr_buf, addrsize);
  1311. return 1;
  1312. }
  1313. /* Compare a unit offset against a unit for bsearch. */
  1314. static int
  1315. units_search (const void *vkey, const void *ventry)
  1316. {
  1317. const size_t *key = (const size_t *) vkey;
  1318. const struct unit *entry = *((const struct unit *const *) ventry);
  1319. size_t offset;
  1320. offset = *key;
  1321. if (offset < entry->low_offset)
  1322. return -1;
  1323. else if (offset >= entry->high_offset)
  1324. return 1;
  1325. else
  1326. return 0;
  1327. }
  1328. /* Find a unit in PU containing OFFSET. */
  1329. static struct unit *
  1330. find_unit (struct unit **pu, size_t units_count, size_t offset)
  1331. {
  1332. struct unit **u;
  1333. u = bsearch (&offset, pu, units_count, sizeof (struct unit *), units_search);
  1334. return u == NULL ? NULL : *u;
  1335. }
  1336. /* Compare function_addrs for qsort. When ranges are nested, make the
  1337. smallest one sort last. */
  1338. static int
  1339. function_addrs_compare (const void *v1, const void *v2)
  1340. {
  1341. const struct function_addrs *a1 = (const struct function_addrs *) v1;
  1342. const struct function_addrs *a2 = (const struct function_addrs *) v2;
  1343. if (a1->low < a2->low)
  1344. return -1;
  1345. if (a1->low > a2->low)
  1346. return 1;
  1347. if (a1->high < a2->high)
  1348. return 1;
  1349. if (a1->high > a2->high)
  1350. return -1;
  1351. return strcmp (a1->function->name, a2->function->name);
  1352. }
  1353. /* Compare a PC against a function_addrs for bsearch. We always
  1354. allocate an entra entry at the end of the vector, so that this
  1355. routine can safely look at the next entry. Note that if there are
  1356. multiple ranges containing PC, which one will be returned is
  1357. unpredictable. We compensate for that in dwarf_fileline. */
  1358. static int
  1359. function_addrs_search (const void *vkey, const void *ventry)
  1360. {
  1361. const uintptr_t *key = (const uintptr_t *) vkey;
  1362. const struct function_addrs *entry = (const struct function_addrs *) ventry;
  1363. uintptr_t pc;
  1364. pc = *key;
  1365. if (pc < entry->low)
  1366. return -1;
  1367. else if (pc > (entry + 1)->low)
  1368. return 1;
  1369. else
  1370. return 0;
  1371. }
  1372. /* Add a new compilation unit address range to a vector. This is
  1373. called via add_ranges. Returns 1 on success, 0 on failure. */
  1374. static int
  1375. add_unit_addr (struct backtrace_state *state, void *rdata,
  1376. uintptr_t lowpc, uintptr_t highpc,
  1377. backtrace_error_callback error_callback, void *data,
  1378. void *pvec)
  1379. {
  1380. struct unit *u = (struct unit *) rdata;
  1381. struct unit_addrs_vector *vec = (struct unit_addrs_vector *) pvec;
  1382. struct unit_addrs *p;
  1383. /* Try to merge with the last entry. */
  1384. if (vec->count > 0)
  1385. {
  1386. p = (struct unit_addrs *) vec->vec.base + (vec->count - 1);
  1387. if ((lowpc == p->high || lowpc == p->high + 1)
  1388. && u == p->u)
  1389. {
  1390. if (highpc > p->high)
  1391. p->high = highpc;
  1392. return 1;
  1393. }
  1394. }
  1395. p = ((struct unit_addrs *)
  1396. backtrace_vector_grow (state, sizeof (struct unit_addrs),
  1397. error_callback, data, &vec->vec));
  1398. if (p == NULL)
  1399. return 0;
  1400. p->low = lowpc;
  1401. p->high = highpc;
  1402. p->u = u;
  1403. ++vec->count;
  1404. return 1;
  1405. }
  1406. /* Compare unit_addrs for qsort. When ranges are nested, make the
  1407. smallest one sort last. */
  1408. static int
  1409. unit_addrs_compare (const void *v1, const void *v2)
  1410. {
  1411. const struct unit_addrs *a1 = (const struct unit_addrs *) v1;
  1412. const struct unit_addrs *a2 = (const struct unit_addrs *) v2;
  1413. if (a1->low < a2->low)
  1414. return -1;
  1415. if (a1->low > a2->low)
  1416. return 1;
  1417. if (a1->high < a2->high)
  1418. return 1;
  1419. if (a1->high > a2->high)
  1420. return -1;
  1421. if (a1->u->lineoff < a2->u->lineoff)
  1422. return -1;
  1423. if (a1->u->lineoff > a2->u->lineoff)
  1424. return 1;
  1425. return 0;
  1426. }
  1427. /* Compare a PC against a unit_addrs for bsearch. We always allocate
  1428. an entry entry at the end of the vector, so that this routine can
  1429. safely look at the next entry. Note that if there are multiple
  1430. ranges containing PC, which one will be returned is unpredictable.
  1431. We compensate for that in dwarf_fileline. */
  1432. static int
  1433. unit_addrs_search (const void *vkey, const void *ventry)
  1434. {
  1435. const uintptr_t *key = (const uintptr_t *) vkey;
  1436. const struct unit_addrs *entry = (const struct unit_addrs *) ventry;
  1437. uintptr_t pc;
  1438. pc = *key;
  1439. if (pc < entry->low)
  1440. return -1;
  1441. else if (pc > (entry + 1)->low)
  1442. return 1;
  1443. else
  1444. return 0;
  1445. }
  1446. /* Fill in overlapping ranges as needed. This is a subroutine of
  1447. resolve_unit_addrs_overlap. */
  1448. static int
  1449. resolve_unit_addrs_overlap_walk (struct backtrace_state *state,
  1450. size_t *pfrom, size_t *pto,
  1451. struct unit_addrs *enclosing,
  1452. struct unit_addrs_vector *old_vec,
  1453. backtrace_error_callback error_callback,
  1454. void *data,
  1455. struct unit_addrs_vector *new_vec)
  1456. {
  1457. struct unit_addrs *old_addrs;
  1458. size_t old_count;
  1459. struct unit_addrs *new_addrs;
  1460. size_t from;
  1461. size_t to;
  1462. old_addrs = (struct unit_addrs *) old_vec->vec.base;
  1463. old_count = old_vec->count;
  1464. new_addrs = (struct unit_addrs *) new_vec->vec.base;
  1465. for (from = *pfrom, to = *pto; from < old_count; from++, to++)
  1466. {
  1467. /* If we are in the scope of a larger range that can no longer
  1468. cover any further ranges, return back to the caller. */
  1469. if (enclosing != NULL
  1470. && enclosing->high <= old_addrs[from].low)
  1471. {
  1472. *pfrom = from;
  1473. *pto = to;
  1474. return 1;
  1475. }
  1476. new_addrs[to] = old_addrs[from];
  1477. /* If we are in scope of a larger range, fill in any gaps
  1478. between this entry and the next one.
  1479. There is an extra entry at the end of the vector, so it's
  1480. always OK to refer to from + 1. */
  1481. if (enclosing != NULL
  1482. && enclosing->high > old_addrs[from].high
  1483. && old_addrs[from].high < old_addrs[from + 1].low)
  1484. {
  1485. void *grew;
  1486. size_t new_high;
  1487. grew = backtrace_vector_grow (state, sizeof (struct unit_addrs),
  1488. error_callback, data, &new_vec->vec);
  1489. if (grew == NULL)
  1490. return 0;
  1491. new_addrs = (struct unit_addrs *) new_vec->vec.base;
  1492. to++;
  1493. new_addrs[to].low = old_addrs[from].high;
  1494. new_high = old_addrs[from + 1].low;
  1495. if (enclosing->high < new_high)
  1496. new_high = enclosing->high;
  1497. new_addrs[to].high = new_high;
  1498. new_addrs[to].u = enclosing->u;
  1499. }
  1500. /* If this range has a larger scope than the next one, use it to
  1501. fill in any gaps. */
  1502. if (old_addrs[from].high > old_addrs[from + 1].high)
  1503. {
  1504. *pfrom = from + 1;
  1505. *pto = to + 1;
  1506. if (!resolve_unit_addrs_overlap_walk (state, pfrom, pto,
  1507. &old_addrs[from], old_vec,
  1508. error_callback, data, new_vec))
  1509. return 0;
  1510. from = *pfrom;
  1511. to = *pto;
  1512. /* Undo the increment the loop is about to do. */
  1513. from--;
  1514. to--;
  1515. }
  1516. }
  1517. if (enclosing == NULL)
  1518. {
  1519. struct unit_addrs *pa;
  1520. /* Add trailing entry. */
  1521. pa = ((struct unit_addrs *)
  1522. backtrace_vector_grow (state, sizeof (struct unit_addrs),
  1523. error_callback, data, &new_vec->vec));
  1524. if (pa == NULL)
  1525. return 0;
  1526. pa->low = 0;
  1527. --pa->low;
  1528. pa->high = pa->low;
  1529. pa->u = NULL;
  1530. new_vec->count = to;
  1531. }
  1532. return 1;
  1533. }
  1534. /* It is possible for the unit_addrs list to contain overlaps, as in
  1535. 10: low == 10, high == 20, unit 1
  1536. 11: low == 12, high == 15, unit 2
  1537. 12: low == 20, high == 30, unit 1
  1538. In such a case, for pc == 17, a search using units_addr_search will
  1539. return entry 11. However, pc == 17 doesn't fit in that range. We
  1540. actually want range 10.
  1541. It seems that in general we might have an arbitrary number of
  1542. ranges in between 10 and 12.
  1543. To handle this we look for cases where range R1 is followed by
  1544. range R2 such that R2 is a strict subset of R1. In such cases we
  1545. insert a new range R3 following R2 that fills in the remainder of
  1546. the address space covered by R1. That lets a relatively simple
  1547. search find the correct range.
  1548. These overlaps can occur because of the range merging we do in
  1549. add_unit_addr. When the linker de-duplicates functions, it can
  1550. leave behind an address range that refers to the address range of
  1551. the retained duplicate. If the retained duplicate address range is
  1552. merged with others, then after sorting we can see overlapping
  1553. address ranges.
  1554. See https://github.com/ianlancetaylor/libbacktrace/issues/137. */
  1555. static int
  1556. resolve_unit_addrs_overlap (struct backtrace_state *state,
  1557. backtrace_error_callback error_callback,
  1558. void *data, struct unit_addrs_vector *addrs_vec)
  1559. {
  1560. struct unit_addrs *addrs;
  1561. size_t count;
  1562. int found;
  1563. struct unit_addrs *entry;
  1564. size_t i;
  1565. struct unit_addrs_vector new_vec;
  1566. void *grew;
  1567. size_t from;
  1568. size_t to;
  1569. addrs = (struct unit_addrs *) addrs_vec->vec.base;
  1570. count = addrs_vec->count;
  1571. if (count == 0)
  1572. return 1;
  1573. /* Optimistically assume that overlaps are rare. */
  1574. found = 0;
  1575. entry = addrs;
  1576. for (i = 0; i < count - 1; i++)
  1577. {
  1578. if (entry->low < (entry + 1)->low
  1579. && entry->high > (entry + 1)->high)
  1580. {
  1581. found = 1;
  1582. break;
  1583. }
  1584. entry++;
  1585. }
  1586. if (!found)
  1587. return 1;
  1588. memset (&new_vec, 0, sizeof new_vec);
  1589. grew = backtrace_vector_grow (state,
  1590. count * sizeof (struct unit_addrs),
  1591. error_callback, data, &new_vec.vec);
  1592. if (grew == NULL)
  1593. return 0;
  1594. from = 0;
  1595. to = 0;
  1596. resolve_unit_addrs_overlap_walk (state, &from, &to, NULL, addrs_vec,
  1597. error_callback, data, &new_vec);
  1598. backtrace_vector_free (state, &addrs_vec->vec, error_callback, data);
  1599. *addrs_vec = new_vec;
  1600. return 1;
  1601. }
  1602. /* Sort the line vector by PC. We want a stable sort here to maintain
  1603. the order of lines for the same PC values. Since the sequence is
  1604. being sorted in place, their addresses cannot be relied on to
  1605. maintain stability. That is the purpose of the index member. */
  1606. static int
  1607. line_compare (const void *v1, const void *v2)
  1608. {
  1609. const struct line *ln1 = (const struct line *) v1;
  1610. const struct line *ln2 = (const struct line *) v2;
  1611. if (ln1->pc < ln2->pc)
  1612. return -1;
  1613. else if (ln1->pc > ln2->pc)
  1614. return 1;
  1615. else if (ln1->idx < ln2->idx)
  1616. return -1;
  1617. else if (ln1->idx > ln2->idx)
  1618. return 1;
  1619. else
  1620. return 0;
  1621. }
  1622. /* Find a PC in a line vector. We always allocate an extra entry at
  1623. the end of the lines vector, so that this routine can safely look
  1624. at the next entry. Note that when there are multiple mappings for
  1625. the same PC value, this will return the last one. */
  1626. static int
  1627. line_search (const void *vkey, const void *ventry)
  1628. {
  1629. const uintptr_t *key = (const uintptr_t *) vkey;
  1630. const struct line *entry = (const struct line *) ventry;
  1631. uintptr_t pc;
  1632. pc = *key;
  1633. if (pc < entry->pc)
  1634. return -1;
  1635. else if (pc >= (entry + 1)->pc)
  1636. return 1;
  1637. else
  1638. return 0;
  1639. }
  1640. /* Sort the abbrevs by the abbrev code. This function is passed to
  1641. both qsort and bsearch. */
  1642. static int
  1643. abbrev_compare (const void *v1, const void *v2)
  1644. {
  1645. const struct abbrev *a1 = (const struct abbrev *) v1;
  1646. const struct abbrev *a2 = (const struct abbrev *) v2;
  1647. if (a1->code < a2->code)
  1648. return -1;
  1649. else if (a1->code > a2->code)
  1650. return 1;
  1651. else
  1652. {
  1653. /* This really shouldn't happen. It means there are two
  1654. different abbrevs with the same code, and that means we don't
  1655. know which one lookup_abbrev should return. */
  1656. return 0;
  1657. }
  1658. }
  1659. /* Read the abbreviation table for a compilation unit. Returns 1 on
  1660. success, 0 on failure. */
  1661. static int
  1662. read_abbrevs (struct backtrace_state *state, uint64_t abbrev_offset,
  1663. const unsigned char *dwarf_abbrev, size_t dwarf_abbrev_size,
  1664. int is_bigendian, backtrace_error_callback error_callback,
  1665. void *data, struct abbrevs *abbrevs)
  1666. {
  1667. struct dwarf_buf abbrev_buf;
  1668. struct dwarf_buf count_buf;
  1669. size_t num_abbrevs;
  1670. abbrevs->num_abbrevs = 0;
  1671. abbrevs->abbrevs = NULL;
  1672. if (abbrev_offset >= dwarf_abbrev_size)
  1673. {
  1674. error_callback (data, "abbrev offset out of range", 0);
  1675. return 0;
  1676. }
  1677. abbrev_buf.name = ".debug_abbrev";
  1678. abbrev_buf.start = dwarf_abbrev;
  1679. abbrev_buf.buf = dwarf_abbrev + abbrev_offset;
  1680. abbrev_buf.left = dwarf_abbrev_size - abbrev_offset;
  1681. abbrev_buf.is_bigendian = is_bigendian;
  1682. abbrev_buf.error_callback = error_callback;
  1683. abbrev_buf.data = data;
  1684. abbrev_buf.reported_underflow = 0;
  1685. /* Count the number of abbrevs in this list. */
  1686. count_buf = abbrev_buf;
  1687. num_abbrevs = 0;
  1688. while (read_uleb128 (&count_buf) != 0)
  1689. {
  1690. if (count_buf.reported_underflow)
  1691. return 0;
  1692. ++num_abbrevs;
  1693. // Skip tag.
  1694. read_uleb128 (&count_buf);
  1695. // Skip has_children.
  1696. read_byte (&count_buf);
  1697. // Skip attributes.
  1698. while (read_uleb128 (&count_buf) != 0)
  1699. {
  1700. uint64_t form;
  1701. form = read_uleb128 (&count_buf);
  1702. if ((enum dwarf_form) form == DW_FORM_implicit_const)
  1703. read_sleb128 (&count_buf);
  1704. }
  1705. // Skip form of last attribute.
  1706. read_uleb128 (&count_buf);
  1707. }
  1708. if (count_buf.reported_underflow)
  1709. return 0;
  1710. if (num_abbrevs == 0)
  1711. return 1;
  1712. abbrevs->abbrevs = ((struct abbrev *)
  1713. backtrace_alloc (state,
  1714. num_abbrevs * sizeof (struct abbrev),
  1715. error_callback, data));
  1716. if (abbrevs->abbrevs == NULL)
  1717. return 0;
  1718. abbrevs->num_abbrevs = num_abbrevs;
  1719. memset (abbrevs->abbrevs, 0, num_abbrevs * sizeof (struct abbrev));
  1720. num_abbrevs = 0;
  1721. while (1)
  1722. {
  1723. uint64_t code;
  1724. struct abbrev a;
  1725. size_t num_attrs;
  1726. struct attr *attrs;
  1727. if (abbrev_buf.reported_underflow)
  1728. goto fail;
  1729. code = read_uleb128 (&abbrev_buf);
  1730. if (code == 0)
  1731. break;
  1732. a.code = code;
  1733. a.tag = (enum dwarf_tag) read_uleb128 (&abbrev_buf);
  1734. a.has_children = read_byte (&abbrev_buf);
  1735. count_buf = abbrev_buf;
  1736. num_attrs = 0;
  1737. while (read_uleb128 (&count_buf) != 0)
  1738. {
  1739. uint64_t form;
  1740. ++num_attrs;
  1741. form = read_uleb128 (&count_buf);
  1742. if ((enum dwarf_form) form == DW_FORM_implicit_const)
  1743. read_sleb128 (&count_buf);
  1744. }
  1745. if (num_attrs == 0)
  1746. {
  1747. attrs = NULL;
  1748. read_uleb128 (&abbrev_buf);
  1749. read_uleb128 (&abbrev_buf);
  1750. }
  1751. else
  1752. {
  1753. attrs = ((struct attr *)
  1754. backtrace_alloc (state, num_attrs * sizeof *attrs,
  1755. error_callback, data));
  1756. if (attrs == NULL)
  1757. goto fail;
  1758. num_attrs = 0;
  1759. while (1)
  1760. {
  1761. uint64_t name;
  1762. uint64_t form;
  1763. name = read_uleb128 (&abbrev_buf);
  1764. form = read_uleb128 (&abbrev_buf);
  1765. if (name == 0)
  1766. break;
  1767. attrs[num_attrs].name = (enum dwarf_attribute) name;
  1768. attrs[num_attrs].form = (enum dwarf_form) form;
  1769. if ((enum dwarf_form) form == DW_FORM_implicit_const)
  1770. attrs[num_attrs].val = read_sleb128 (&abbrev_buf);
  1771. else
  1772. attrs[num_attrs].val = 0;
  1773. ++num_attrs;
  1774. }
  1775. }
  1776. a.num_attrs = num_attrs;
  1777. a.attrs = attrs;
  1778. abbrevs->abbrevs[num_abbrevs] = a;
  1779. ++num_abbrevs;
  1780. }
  1781. backtrace_qsort (abbrevs->abbrevs, abbrevs->num_abbrevs,
  1782. sizeof (struct abbrev), abbrev_compare);
  1783. return 1;
  1784. fail:
  1785. free_abbrevs (state, abbrevs, error_callback, data);
  1786. return 0;
  1787. }
  1788. /* Return the abbrev information for an abbrev code. */
  1789. static const struct abbrev *
  1790. lookup_abbrev (struct abbrevs *abbrevs, uint64_t code,
  1791. backtrace_error_callback error_callback, void *data)
  1792. {
  1793. struct abbrev key;
  1794. void *p;
  1795. /* With GCC, where abbrevs are simply numbered in order, we should
  1796. be able to just look up the entry. */
  1797. if (code - 1 < abbrevs->num_abbrevs
  1798. && abbrevs->abbrevs[code - 1].code == code)
  1799. return &abbrevs->abbrevs[code - 1];
  1800. /* Otherwise we have to search. */
  1801. memset (&key, 0, sizeof key);
  1802. key.code = code;
  1803. p = bsearch (&key, abbrevs->abbrevs, abbrevs->num_abbrevs,
  1804. sizeof (struct abbrev), abbrev_compare);
  1805. if (p == NULL)
  1806. {
  1807. error_callback (data, "invalid abbreviation code", 0);
  1808. return NULL;
  1809. }
  1810. return (const struct abbrev *) p;
  1811. }
  1812. /* This struct is used to gather address range information while
  1813. reading attributes. We use this while building a mapping from
  1814. address ranges to compilation units and then again while mapping
  1815. from address ranges to function entries. Normally either
  1816. lowpc/highpc is set or ranges is set. */
  1817. struct pcrange {
  1818. uintptr_t lowpc; /* The low PC value. */
  1819. int have_lowpc; /* Whether a low PC value was found. */
  1820. int lowpc_is_addr_index; /* Whether lowpc is in .debug_addr. */
  1821. uintptr_t highpc; /* The high PC value. */
  1822. int have_highpc; /* Whether a high PC value was found. */
  1823. int highpc_is_relative; /* Whether highpc is relative to lowpc. */
  1824. int highpc_is_addr_index; /* Whether highpc is in .debug_addr. */
  1825. uint64_t ranges; /* Offset in ranges section. */
  1826. int have_ranges; /* Whether ranges is valid. */
  1827. int ranges_is_index; /* Whether ranges is DW_FORM_rnglistx. */
  1828. };
  1829. /* Update PCRANGE from an attribute value. */
  1830. static void
  1831. update_pcrange (const struct attr* attr, const struct attr_val* val,
  1832. struct pcrange *pcrange)
  1833. {
  1834. switch (attr->name)
  1835. {
  1836. case DW_AT_low_pc:
  1837. if (val->encoding == ATTR_VAL_ADDRESS)
  1838. {
  1839. pcrange->lowpc = (uintptr_t) val->u.uint;
  1840. pcrange->have_lowpc = 1;
  1841. }
  1842. else if (val->encoding == ATTR_VAL_ADDRESS_INDEX)
  1843. {
  1844. pcrange->lowpc = (uintptr_t) val->u.uint;
  1845. pcrange->have_lowpc = 1;
  1846. pcrange->lowpc_is_addr_index = 1;
  1847. }
  1848. break;
  1849. case DW_AT_high_pc:
  1850. if (val->encoding == ATTR_VAL_ADDRESS)
  1851. {
  1852. pcrange->highpc = (uintptr_t) val->u.uint;
  1853. pcrange->have_highpc = 1;
  1854. }
  1855. else if (val->encoding == ATTR_VAL_UINT)
  1856. {
  1857. pcrange->highpc = (uintptr_t) val->u.uint;
  1858. pcrange->have_highpc = 1;
  1859. pcrange->highpc_is_relative = 1;
  1860. }
  1861. else if (val->encoding == ATTR_VAL_ADDRESS_INDEX)
  1862. {
  1863. pcrange->highpc = (uintptr_t) val->u.uint;
  1864. pcrange->have_highpc = 1;
  1865. pcrange->highpc_is_addr_index = 1;
  1866. }
  1867. break;
  1868. case DW_AT_ranges:
  1869. if (val->encoding == ATTR_VAL_UINT
  1870. || val->encoding == ATTR_VAL_REF_SECTION)
  1871. {
  1872. pcrange->ranges = val->u.uint;
  1873. pcrange->have_ranges = 1;
  1874. }
  1875. else if (val->encoding == ATTR_VAL_RNGLISTS_INDEX)
  1876. {
  1877. pcrange->ranges = val->u.uint;
  1878. pcrange->have_ranges = 1;
  1879. pcrange->ranges_is_index = 1;
  1880. }
  1881. break;
  1882. default:
  1883. break;
  1884. }
  1885. }
  1886. /* Call ADD_RANGE for a low/high PC pair. Returns 1 on success, 0 on
  1887. error. */
  1888. static int
  1889. add_low_high_range (struct backtrace_state *state,
  1890. const struct dwarf_sections *dwarf_sections,
  1891. struct libbacktrace_base_address base_address,
  1892. int is_bigendian, struct unit *u,
  1893. const struct pcrange *pcrange,
  1894. int (*add_range) (struct backtrace_state *state,
  1895. void *rdata, uintptr_t lowpc,
  1896. uintptr_t highpc,
  1897. backtrace_error_callback error_callback,
  1898. void *data, void *vec),
  1899. void *rdata,
  1900. backtrace_error_callback error_callback, void *data,
  1901. void *vec)
  1902. {
  1903. uintptr_t lowpc;
  1904. uintptr_t highpc;
  1905. lowpc = pcrange->lowpc;
  1906. if (pcrange->lowpc_is_addr_index)
  1907. {
  1908. if (!resolve_addr_index (dwarf_sections, u->addr_base, u->addrsize,
  1909. is_bigendian, lowpc, error_callback, data,
  1910. &lowpc))
  1911. return 0;
  1912. }
  1913. highpc = pcrange->highpc;
  1914. if (pcrange->highpc_is_addr_index)
  1915. {
  1916. if (!resolve_addr_index (dwarf_sections, u->addr_base, u->addrsize,
  1917. is_bigendian, highpc, error_callback, data,
  1918. &highpc))
  1919. return 0;
  1920. }
  1921. if (pcrange->highpc_is_relative)
  1922. highpc += lowpc;
  1923. /* Add in the base address of the module when recording PC values,
  1924. so that we can look up the PC directly. */
  1925. lowpc = libbacktrace_add_base (lowpc, base_address);
  1926. highpc = libbacktrace_add_base (highpc, base_address);
  1927. return add_range (state, rdata, lowpc, highpc, error_callback, data, vec);
  1928. }
  1929. /* Call ADD_RANGE for each range read from .debug_ranges, as used in
  1930. DWARF versions 2 through 4. */
  1931. static int
  1932. add_ranges_from_ranges (
  1933. struct backtrace_state *state,
  1934. const struct dwarf_sections *dwarf_sections,
  1935. struct libbacktrace_base_address base_address, int is_bigendian,
  1936. struct unit *u, uintptr_t base,
  1937. const struct pcrange *pcrange,
  1938. int (*add_range) (struct backtrace_state *state, void *rdata,
  1939. uintptr_t lowpc, uintptr_t highpc,
  1940. backtrace_error_callback error_callback, void *data,
  1941. void *vec),
  1942. void *rdata,
  1943. backtrace_error_callback error_callback, void *data,
  1944. void *vec)
  1945. {
  1946. struct dwarf_buf ranges_buf;
  1947. if (pcrange->ranges >= dwarf_sections->size[DEBUG_RANGES])
  1948. {
  1949. error_callback (data, "ranges offset out of range", 0);
  1950. return 0;
  1951. }
  1952. ranges_buf.name = ".debug_ranges";
  1953. ranges_buf.start = dwarf_sections->data[DEBUG_RANGES];
  1954. ranges_buf.buf = dwarf_sections->data[DEBUG_RANGES] + pcrange->ranges;
  1955. ranges_buf.left = dwarf_sections->size[DEBUG_RANGES] - pcrange->ranges;
  1956. ranges_buf.is_bigendian = is_bigendian;
  1957. ranges_buf.error_callback = error_callback;
  1958. ranges_buf.data = data;
  1959. ranges_buf.reported_underflow = 0;
  1960. while (1)
  1961. {
  1962. uint64_t low;
  1963. uint64_t high;
  1964. if (ranges_buf.reported_underflow)
  1965. return 0;
  1966. low = read_address (&ranges_buf, u->addrsize);
  1967. high = read_address (&ranges_buf, u->addrsize);
  1968. if (low == 0 && high == 0)
  1969. break;
  1970. if (is_highest_address (low, u->addrsize))
  1971. base = (uintptr_t) high;
  1972. else
  1973. {
  1974. uintptr_t rl, rh;
  1975. rl = libbacktrace_add_base ((uintptr_t) low + base, base_address);
  1976. rh = libbacktrace_add_base ((uintptr_t) high + base, base_address);
  1977. if (!add_range (state, rdata, rl, rh, error_callback, data, vec))
  1978. return 0;
  1979. }
  1980. }
  1981. if (ranges_buf.reported_underflow)
  1982. return 0;
  1983. return 1;
  1984. }
  1985. /* Call ADD_RANGE for each range read from .debug_rnglists, as used in
  1986. DWARF version 5. */
  1987. static int
  1988. add_ranges_from_rnglists (
  1989. struct backtrace_state *state,
  1990. const struct dwarf_sections *dwarf_sections,
  1991. struct libbacktrace_base_address base_address, int is_bigendian,
  1992. struct unit *u, uintptr_t base,
  1993. const struct pcrange *pcrange,
  1994. int (*add_range) (struct backtrace_state *state, void *rdata,
  1995. uintptr_t lowpc, uintptr_t highpc,
  1996. backtrace_error_callback error_callback, void *data,
  1997. void *vec),
  1998. void *rdata,
  1999. backtrace_error_callback error_callback, void *data,
  2000. void *vec)
  2001. {
  2002. uint64_t offset;
  2003. struct dwarf_buf rnglists_buf;
  2004. if (!pcrange->ranges_is_index)
  2005. offset = pcrange->ranges;
  2006. else
  2007. offset = u->rnglists_base + pcrange->ranges * (u->is_dwarf64 ? 8 : 4);
  2008. if (offset >= dwarf_sections->size[DEBUG_RNGLISTS])
  2009. {
  2010. error_callback (data, "rnglists offset out of range", 0);
  2011. return 0;
  2012. }
  2013. rnglists_buf.name = ".debug_rnglists";
  2014. rnglists_buf.start = dwarf_sections->data[DEBUG_RNGLISTS];
  2015. rnglists_buf.buf = dwarf_sections->data[DEBUG_RNGLISTS] + offset;
  2016. rnglists_buf.left = dwarf_sections->size[DEBUG_RNGLISTS] - offset;
  2017. rnglists_buf.is_bigendian = is_bigendian;
  2018. rnglists_buf.error_callback = error_callback;
  2019. rnglists_buf.data = data;
  2020. rnglists_buf.reported_underflow = 0;
  2021. if (pcrange->ranges_is_index)
  2022. {
  2023. offset = read_offset (&rnglists_buf, u->is_dwarf64);
  2024. offset += u->rnglists_base;
  2025. if (offset >= dwarf_sections->size[DEBUG_RNGLISTS])
  2026. {
  2027. error_callback (data, "rnglists index offset out of range", 0);
  2028. return 0;
  2029. }
  2030. rnglists_buf.buf = dwarf_sections->data[DEBUG_RNGLISTS] + offset;
  2031. rnglists_buf.left = dwarf_sections->size[DEBUG_RNGLISTS] - offset;
  2032. }
  2033. while (1)
  2034. {
  2035. unsigned char rle;
  2036. rle = read_byte (&rnglists_buf);
  2037. if (rle == DW_RLE_end_of_list)
  2038. break;
  2039. switch (rle)
  2040. {
  2041. case DW_RLE_base_addressx:
  2042. {
  2043. uint64_t index;
  2044. index = read_uleb128 (&rnglists_buf);
  2045. if (!resolve_addr_index (dwarf_sections, u->addr_base,
  2046. u->addrsize, is_bigendian, index,
  2047. error_callback, data, &base))
  2048. return 0;
  2049. }
  2050. break;
  2051. case DW_RLE_startx_endx:
  2052. {
  2053. uint64_t index;
  2054. uintptr_t low;
  2055. uintptr_t high;
  2056. index = read_uleb128 (&rnglists_buf);
  2057. if (!resolve_addr_index (dwarf_sections, u->addr_base,
  2058. u->addrsize, is_bigendian, index,
  2059. error_callback, data, &low))
  2060. return 0;
  2061. index = read_uleb128 (&rnglists_buf);
  2062. if (!resolve_addr_index (dwarf_sections, u->addr_base,
  2063. u->addrsize, is_bigendian, index,
  2064. error_callback, data, &high))
  2065. return 0;
  2066. if (!add_range (state, rdata,
  2067. libbacktrace_add_base (low, base_address),
  2068. libbacktrace_add_base (high, base_address),
  2069. error_callback, data, vec))
  2070. return 0;
  2071. }
  2072. break;
  2073. case DW_RLE_startx_length:
  2074. {
  2075. uint64_t index;
  2076. uintptr_t low;
  2077. uintptr_t length;
  2078. index = read_uleb128 (&rnglists_buf);
  2079. if (!resolve_addr_index (dwarf_sections, u->addr_base,
  2080. u->addrsize, is_bigendian, index,
  2081. error_callback, data, &low))
  2082. return 0;
  2083. length = read_uleb128 (&rnglists_buf);
  2084. low = libbacktrace_add_base (low, base_address);
  2085. if (!add_range (state, rdata, low, low + length,
  2086. error_callback, data, vec))
  2087. return 0;
  2088. }
  2089. break;
  2090. case DW_RLE_offset_pair:
  2091. {
  2092. uint64_t low;
  2093. uint64_t high;
  2094. low = read_uleb128 (&rnglists_buf);
  2095. high = read_uleb128 (&rnglists_buf);
  2096. if (!add_range (state, rdata,
  2097. libbacktrace_add_base (low + base, base_address),
  2098. libbacktrace_add_base (high + base, base_address),
  2099. error_callback, data, vec))
  2100. return 0;
  2101. }
  2102. break;
  2103. case DW_RLE_base_address:
  2104. base = (uintptr_t) read_address (&rnglists_buf, u->addrsize);
  2105. break;
  2106. case DW_RLE_start_end:
  2107. {
  2108. uintptr_t low;
  2109. uintptr_t high;
  2110. low = (uintptr_t) read_address (&rnglists_buf, u->addrsize);
  2111. high = (uintptr_t) read_address (&rnglists_buf, u->addrsize);
  2112. if (!add_range (state, rdata,
  2113. libbacktrace_add_base (low, base_address),
  2114. libbacktrace_add_base (high, base_address),
  2115. error_callback, data, vec))
  2116. return 0;
  2117. }
  2118. break;
  2119. case DW_RLE_start_length:
  2120. {
  2121. uintptr_t low;
  2122. uintptr_t length;
  2123. low = (uintptr_t) read_address (&rnglists_buf, u->addrsize);
  2124. length = (uintptr_t) read_uleb128 (&rnglists_buf);
  2125. low = libbacktrace_add_base (low, base_address);
  2126. if (!add_range (state, rdata, low, low + length,
  2127. error_callback, data, vec))
  2128. return 0;
  2129. }
  2130. break;
  2131. default:
  2132. dwarf_buf_error (&rnglists_buf, "unrecognized DW_RLE value", -1);
  2133. return 0;
  2134. }
  2135. }
  2136. if (rnglists_buf.reported_underflow)
  2137. return 0;
  2138. return 1;
  2139. }
  2140. /* Call ADD_RANGE for each lowpc/highpc pair in PCRANGE. RDATA is
  2141. passed to ADD_RANGE, and is either a struct unit * or a struct
  2142. function *. VEC is the vector we are adding ranges to, and is
  2143. either a struct unit_addrs_vector * or a struct function_vector *.
  2144. Returns 1 on success, 0 on error. */
  2145. static int
  2146. add_ranges (struct backtrace_state *state,
  2147. const struct dwarf_sections *dwarf_sections,
  2148. struct libbacktrace_base_address base_address, int is_bigendian,
  2149. struct unit *u, uintptr_t base, const struct pcrange *pcrange,
  2150. int (*add_range) (struct backtrace_state *state, void *rdata,
  2151. uintptr_t lowpc, uintptr_t highpc,
  2152. backtrace_error_callback error_callback,
  2153. void *data, void *vec),
  2154. void *rdata,
  2155. backtrace_error_callback error_callback, void *data,
  2156. void *vec)
  2157. {
  2158. if (pcrange->have_lowpc && pcrange->have_highpc)
  2159. return add_low_high_range (state, dwarf_sections, base_address,
  2160. is_bigendian, u, pcrange, add_range, rdata,
  2161. error_callback, data, vec);
  2162. if (!pcrange->have_ranges)
  2163. {
  2164. /* Did not find any address ranges to add. */
  2165. return 1;
  2166. }
  2167. if (u->version < 5)
  2168. return add_ranges_from_ranges (state, dwarf_sections, base_address,
  2169. is_bigendian, u, base, pcrange, add_range,
  2170. rdata, error_callback, data, vec);
  2171. else
  2172. return add_ranges_from_rnglists (state, dwarf_sections, base_address,
  2173. is_bigendian, u, base, pcrange, add_range,
  2174. rdata, error_callback, data, vec);
  2175. }
  2176. /* Find the address range covered by a compilation unit, reading from
  2177. UNIT_BUF and adding values to U. Returns 1 if all data could be
  2178. read, 0 if there is some error. */
  2179. static int
  2180. find_address_ranges (struct backtrace_state *state,
  2181. struct libbacktrace_base_address base_address,
  2182. struct dwarf_buf *unit_buf,
  2183. const struct dwarf_sections *dwarf_sections,
  2184. int is_bigendian, struct dwarf_data *altlink,
  2185. backtrace_error_callback error_callback, void *data,
  2186. struct unit *u, struct unit_addrs_vector *addrs,
  2187. enum dwarf_tag *unit_tag)
  2188. {
  2189. while (unit_buf->left > 0)
  2190. {
  2191. uint64_t code;
  2192. const struct abbrev *abbrev;
  2193. struct pcrange pcrange;
  2194. struct attr_val name_val;
  2195. int have_name_val;
  2196. struct attr_val comp_dir_val;
  2197. int have_comp_dir_val;
  2198. size_t i;
  2199. code = read_uleb128 (unit_buf);
  2200. if (code == 0)
  2201. return 1;
  2202. abbrev = lookup_abbrev (&u->abbrevs, code, error_callback, data);
  2203. if (abbrev == NULL)
  2204. return 0;
  2205. if (unit_tag != NULL)
  2206. *unit_tag = abbrev->tag;
  2207. memset (&pcrange, 0, sizeof pcrange);
  2208. memset (&name_val, 0, sizeof name_val);
  2209. have_name_val = 0;
  2210. memset (&comp_dir_val, 0, sizeof comp_dir_val);
  2211. have_comp_dir_val = 0;
  2212. for (i = 0; i < abbrev->num_attrs; ++i)
  2213. {
  2214. struct attr_val val;
  2215. if (!read_attribute (abbrev->attrs[i].form, abbrev->attrs[i].val,
  2216. unit_buf, u->is_dwarf64, u->version,
  2217. u->addrsize, dwarf_sections, altlink, &val))
  2218. return 0;
  2219. switch (abbrev->attrs[i].name)
  2220. {
  2221. case DW_AT_low_pc: case DW_AT_high_pc: case DW_AT_ranges:
  2222. update_pcrange (&abbrev->attrs[i], &val, &pcrange);
  2223. break;
  2224. case DW_AT_stmt_list:
  2225. if ((abbrev->tag == DW_TAG_compile_unit
  2226. || abbrev->tag == DW_TAG_skeleton_unit)
  2227. && (val.encoding == ATTR_VAL_UINT
  2228. || val.encoding == ATTR_VAL_REF_SECTION))
  2229. u->lineoff = val.u.uint;
  2230. break;
  2231. case DW_AT_name:
  2232. if (abbrev->tag == DW_TAG_compile_unit
  2233. || abbrev->tag == DW_TAG_skeleton_unit)
  2234. {
  2235. name_val = val;
  2236. have_name_val = 1;
  2237. }
  2238. break;
  2239. case DW_AT_comp_dir:
  2240. if (abbrev->tag == DW_TAG_compile_unit
  2241. || abbrev->tag == DW_TAG_skeleton_unit)
  2242. {
  2243. comp_dir_val = val;
  2244. have_comp_dir_val = 1;
  2245. }
  2246. break;
  2247. case DW_AT_str_offsets_base:
  2248. if ((abbrev->tag == DW_TAG_compile_unit
  2249. || abbrev->tag == DW_TAG_skeleton_unit)
  2250. && val.encoding == ATTR_VAL_REF_SECTION)
  2251. u->str_offsets_base = val.u.uint;
  2252. break;
  2253. case DW_AT_addr_base:
  2254. if ((abbrev->tag == DW_TAG_compile_unit
  2255. || abbrev->tag == DW_TAG_skeleton_unit)
  2256. && val.encoding == ATTR_VAL_REF_SECTION)
  2257. u->addr_base = val.u.uint;
  2258. break;
  2259. case DW_AT_rnglists_base:
  2260. if ((abbrev->tag == DW_TAG_compile_unit
  2261. || abbrev->tag == DW_TAG_skeleton_unit)
  2262. && val.encoding == ATTR_VAL_REF_SECTION)
  2263. u->rnglists_base = val.u.uint;
  2264. break;
  2265. default:
  2266. break;
  2267. }
  2268. }
  2269. // Resolve strings after we're sure that we have seen
  2270. // DW_AT_str_offsets_base.
  2271. if (have_name_val)
  2272. {
  2273. if (!resolve_string (dwarf_sections, u->is_dwarf64, is_bigendian,
  2274. u->str_offsets_base, &name_val,
  2275. error_callback, data, &u->filename))
  2276. return 0;
  2277. }
  2278. if (have_comp_dir_val)
  2279. {
  2280. if (!resolve_string (dwarf_sections, u->is_dwarf64, is_bigendian,
  2281. u->str_offsets_base, &comp_dir_val,
  2282. error_callback, data, &u->comp_dir))
  2283. return 0;
  2284. }
  2285. if (abbrev->tag == DW_TAG_compile_unit
  2286. || abbrev->tag == DW_TAG_subprogram
  2287. || abbrev->tag == DW_TAG_skeleton_unit)
  2288. {
  2289. if (!add_ranges (state, dwarf_sections, base_address,
  2290. is_bigendian, u, pcrange.lowpc, &pcrange,
  2291. add_unit_addr, (void *) u, error_callback, data,
  2292. (void *) addrs))
  2293. return 0;
  2294. /* If we found the PC range in the DW_TAG_compile_unit or
  2295. DW_TAG_skeleton_unit, we can stop now. */
  2296. if ((abbrev->tag == DW_TAG_compile_unit
  2297. || abbrev->tag == DW_TAG_skeleton_unit)
  2298. && (pcrange.have_ranges
  2299. || (pcrange.have_lowpc && pcrange.have_highpc)))
  2300. return 1;
  2301. }
  2302. if (abbrev->has_children)
  2303. {
  2304. if (!find_address_ranges (state, base_address, unit_buf,
  2305. dwarf_sections, is_bigendian, altlink,
  2306. error_callback, data, u, addrs, NULL))
  2307. return 0;
  2308. }
  2309. }
  2310. return 1;
  2311. }
  2312. /* Build a mapping from address ranges to the compilation units where
  2313. the line number information for that range can be found. Returns 1
  2314. on success, 0 on failure. */
  2315. static int
  2316. build_address_map (struct backtrace_state *state,
  2317. struct libbacktrace_base_address base_address,
  2318. const struct dwarf_sections *dwarf_sections,
  2319. int is_bigendian, struct dwarf_data *altlink,
  2320. backtrace_error_callback error_callback, void *data,
  2321. struct unit_addrs_vector *addrs,
  2322. struct unit_vector *unit_vec)
  2323. {
  2324. struct dwarf_buf info;
  2325. struct backtrace_vector units;
  2326. size_t units_count;
  2327. size_t i;
  2328. struct unit **pu;
  2329. size_t unit_offset = 0;
  2330. struct unit_addrs *pa;
  2331. memset (&addrs->vec, 0, sizeof addrs->vec);
  2332. memset (&unit_vec->vec, 0, sizeof unit_vec->vec);
  2333. addrs->count = 0;
  2334. unit_vec->count = 0;
  2335. /* Read through the .debug_info section. FIXME: Should we use the
  2336. .debug_aranges section? gdb and addr2line don't use it, but I'm
  2337. not sure why. */
  2338. info.name = ".debug_info";
  2339. info.start = dwarf_sections->data[DEBUG_INFO];
  2340. info.buf = info.start;
  2341. info.left = dwarf_sections->size[DEBUG_INFO];
  2342. info.is_bigendian = is_bigendian;
  2343. info.error_callback = error_callback;
  2344. info.data = data;
  2345. info.reported_underflow = 0;
  2346. memset (&units, 0, sizeof units);
  2347. units_count = 0;
  2348. while (info.left > 0)
  2349. {
  2350. const unsigned char *unit_data_start;
  2351. uint64_t len;
  2352. int is_dwarf64;
  2353. struct dwarf_buf unit_buf;
  2354. int version;
  2355. int unit_type;
  2356. uint64_t abbrev_offset;
  2357. int addrsize;
  2358. struct unit *u;
  2359. enum dwarf_tag unit_tag;
  2360. if (info.reported_underflow)
  2361. goto fail;
  2362. unit_data_start = info.buf;
  2363. len = read_initial_length (&info, &is_dwarf64);
  2364. unit_buf = info;
  2365. unit_buf.left = len;
  2366. if (!advance (&info, len))
  2367. goto fail;
  2368. version = read_uint16 (&unit_buf);
  2369. if (version < 2 || version > 5)
  2370. {
  2371. dwarf_buf_error (&unit_buf, "unrecognized DWARF version", -1);
  2372. goto fail;
  2373. }
  2374. if (version < 5)
  2375. unit_type = 0;
  2376. else
  2377. {
  2378. unit_type = read_byte (&unit_buf);
  2379. if (unit_type == DW_UT_type || unit_type == DW_UT_split_type)
  2380. {
  2381. /* This unit doesn't have anything we need. */
  2382. continue;
  2383. }
  2384. }
  2385. pu = ((struct unit **)
  2386. backtrace_vector_grow (state, sizeof (struct unit *),
  2387. error_callback, data, &units));
  2388. if (pu == NULL)
  2389. goto fail;
  2390. u = ((struct unit *)
  2391. backtrace_alloc (state, sizeof *u, error_callback, data));
  2392. if (u == NULL)
  2393. goto fail;
  2394. *pu = u;
  2395. ++units_count;
  2396. if (version < 5)
  2397. addrsize = 0; /* Set below. */
  2398. else
  2399. addrsize = read_byte (&unit_buf);
  2400. memset (&u->abbrevs, 0, sizeof u->abbrevs);
  2401. abbrev_offset = read_offset (&unit_buf, is_dwarf64);
  2402. if (!read_abbrevs (state, abbrev_offset,
  2403. dwarf_sections->data[DEBUG_ABBREV],
  2404. dwarf_sections->size[DEBUG_ABBREV],
  2405. is_bigendian, error_callback, data, &u->abbrevs))
  2406. goto fail;
  2407. if (version < 5)
  2408. addrsize = read_byte (&unit_buf);
  2409. switch (unit_type)
  2410. {
  2411. case 0:
  2412. break;
  2413. case DW_UT_compile: case DW_UT_partial:
  2414. break;
  2415. case DW_UT_skeleton: case DW_UT_split_compile:
  2416. read_uint64 (&unit_buf); /* dwo_id */
  2417. break;
  2418. default:
  2419. break;
  2420. }
  2421. u->low_offset = unit_offset;
  2422. unit_offset += len + (is_dwarf64 ? 12 : 4);
  2423. u->high_offset = unit_offset;
  2424. u->unit_data = unit_buf.buf;
  2425. u->unit_data_len = unit_buf.left;
  2426. u->unit_data_offset = unit_buf.buf - unit_data_start;
  2427. u->version = version;
  2428. u->is_dwarf64 = is_dwarf64;
  2429. u->addrsize = addrsize;
  2430. u->filename = NULL;
  2431. u->comp_dir = NULL;
  2432. u->abs_filename = NULL;
  2433. u->lineoff = 0;
  2434. u->str_offsets_base = 0;
  2435. u->addr_base = 0;
  2436. u->rnglists_base = 0;
  2437. /* The actual line number mappings will be read as needed. */
  2438. u->lines = NULL;
  2439. u->lines_count = 0;
  2440. u->function_addrs = NULL;
  2441. u->function_addrs_count = 0;
  2442. if (!find_address_ranges (state, base_address, &unit_buf, dwarf_sections,
  2443. is_bigendian, altlink, error_callback, data,
  2444. u, addrs, &unit_tag))
  2445. goto fail;
  2446. if (unit_buf.reported_underflow)
  2447. goto fail;
  2448. }
  2449. if (info.reported_underflow)
  2450. goto fail;
  2451. /* Add a trailing addrs entry, but don't include it in addrs->count. */
  2452. pa = ((struct unit_addrs *)
  2453. backtrace_vector_grow (state, sizeof (struct unit_addrs),
  2454. error_callback, data, &addrs->vec));
  2455. if (pa == NULL)
  2456. goto fail;
  2457. pa->low = 0;
  2458. --pa->low;
  2459. pa->high = pa->low;
  2460. pa->u = NULL;
  2461. unit_vec->vec = units;
  2462. unit_vec->count = units_count;
  2463. return 1;
  2464. fail:
  2465. if (units_count > 0)
  2466. {
  2467. pu = (struct unit **) units.base;
  2468. for (i = 0; i < units_count; i++)
  2469. {
  2470. free_abbrevs (state, &pu[i]->abbrevs, error_callback, data);
  2471. backtrace_free (state, pu[i], sizeof **pu, error_callback, data);
  2472. }
  2473. backtrace_vector_free (state, &units, error_callback, data);
  2474. }
  2475. if (addrs->count > 0)
  2476. {
  2477. backtrace_vector_free (state, &addrs->vec, error_callback, data);
  2478. addrs->count = 0;
  2479. }
  2480. return 0;
  2481. }
  2482. /* Add a new mapping to the vector of line mappings that we are
  2483. building. Returns 1 on success, 0 on failure. */
  2484. static int
  2485. add_line (struct backtrace_state *state, struct dwarf_data *ddata,
  2486. uintptr_t pc, const char *filename, int lineno,
  2487. backtrace_error_callback error_callback, void *data,
  2488. struct line_vector *vec)
  2489. {
  2490. struct line *ln;
  2491. /* If we are adding the same mapping, ignore it. This can happen
  2492. when using discriminators. */
  2493. if (vec->count > 0)
  2494. {
  2495. ln = (struct line *) vec->vec.base + (vec->count - 1);
  2496. if (pc == ln->pc && filename == ln->filename && lineno == ln->lineno)
  2497. return 1;
  2498. }
  2499. ln = ((struct line *)
  2500. backtrace_vector_grow (state, sizeof (struct line), error_callback,
  2501. data, &vec->vec));
  2502. if (ln == NULL)
  2503. return 0;
  2504. /* Add in the base address here, so that we can look up the PC
  2505. directly. */
  2506. ln->pc = libbacktrace_add_base (pc, ddata->base_address);
  2507. ln->filename = filename;
  2508. ln->lineno = lineno;
  2509. ln->idx = vec->count;
  2510. ++vec->count;
  2511. return 1;
  2512. }
  2513. /* Free the line header information. */
  2514. static void
  2515. free_line_header (struct backtrace_state *state, struct line_header *hdr,
  2516. backtrace_error_callback error_callback, void *data)
  2517. {
  2518. if (hdr->dirs_count != 0)
  2519. backtrace_free (state, hdr->dirs, hdr->dirs_count * sizeof (const char *),
  2520. error_callback, data);
  2521. backtrace_free (state, hdr->filenames,
  2522. hdr->filenames_count * sizeof (char *),
  2523. error_callback, data);
  2524. }
  2525. /* Read the directories and file names for a line header for version
  2526. 2, setting fields in HDR. Return 1 on success, 0 on failure. */
  2527. static int
  2528. read_v2_paths (struct backtrace_state *state, struct unit *u,
  2529. struct dwarf_buf *hdr_buf, struct line_header *hdr)
  2530. {
  2531. const unsigned char *p;
  2532. const unsigned char *pend;
  2533. size_t i;
  2534. /* Count the number of directory entries. */
  2535. hdr->dirs_count = 0;
  2536. p = hdr_buf->buf;
  2537. pend = p + hdr_buf->left;
  2538. while (p < pend && *p != '\0')
  2539. {
  2540. p += strnlen((const char *) p, pend - p) + 1;
  2541. ++hdr->dirs_count;
  2542. }
  2543. /* The index of the first entry in the list of directories is 1. Index 0 is
  2544. used for the current directory of the compilation. To simplify index
  2545. handling, we set entry 0 to the compilation unit directory. */
  2546. ++hdr->dirs_count;
  2547. hdr->dirs = ((const char **)
  2548. backtrace_alloc (state,
  2549. hdr->dirs_count * sizeof (const char *),
  2550. hdr_buf->error_callback,
  2551. hdr_buf->data));
  2552. if (hdr->dirs == NULL)
  2553. return 0;
  2554. hdr->dirs[0] = u->comp_dir;
  2555. i = 1;
  2556. while (*hdr_buf->buf != '\0')
  2557. {
  2558. if (hdr_buf->reported_underflow)
  2559. return 0;
  2560. hdr->dirs[i] = read_string (hdr_buf);
  2561. if (hdr->dirs[i] == NULL)
  2562. return 0;
  2563. ++i;
  2564. }
  2565. if (!advance (hdr_buf, 1))
  2566. return 0;
  2567. /* Count the number of file entries. */
  2568. hdr->filenames_count = 0;
  2569. p = hdr_buf->buf;
  2570. pend = p + hdr_buf->left;
  2571. while (p < pend && *p != '\0')
  2572. {
  2573. p += strnlen ((const char *) p, pend - p) + 1;
  2574. p += leb128_len (p);
  2575. p += leb128_len (p);
  2576. p += leb128_len (p);
  2577. ++hdr->filenames_count;
  2578. }
  2579. /* The index of the first entry in the list of file names is 1. Index 0 is
  2580. used for the DW_AT_name of the compilation unit. To simplify index
  2581. handling, we set entry 0 to the compilation unit file name. */
  2582. ++hdr->filenames_count;
  2583. hdr->filenames = ((const char **)
  2584. backtrace_alloc (state,
  2585. hdr->filenames_count * sizeof (char *),
  2586. hdr_buf->error_callback,
  2587. hdr_buf->data));
  2588. if (hdr->filenames == NULL)
  2589. return 0;
  2590. hdr->filenames[0] = u->filename;
  2591. i = 1;
  2592. while (*hdr_buf->buf != '\0')
  2593. {
  2594. const char *filename;
  2595. uint64_t dir_index;
  2596. if (hdr_buf->reported_underflow)
  2597. return 0;
  2598. filename = read_string (hdr_buf);
  2599. if (filename == NULL)
  2600. return 0;
  2601. dir_index = read_uleb128 (hdr_buf);
  2602. if (IS_ABSOLUTE_PATH (filename)
  2603. || (dir_index < hdr->dirs_count && hdr->dirs[dir_index] == NULL))
  2604. hdr->filenames[i] = filename;
  2605. else
  2606. {
  2607. const char *dir;
  2608. size_t dir_len;
  2609. size_t filename_len;
  2610. char *s;
  2611. if (dir_index < hdr->dirs_count)
  2612. dir = hdr->dirs[dir_index];
  2613. else
  2614. {
  2615. dwarf_buf_error (hdr_buf,
  2616. ("invalid directory index in "
  2617. "line number program header"),
  2618. 0);
  2619. return 0;
  2620. }
  2621. dir_len = strlen (dir);
  2622. filename_len = strlen (filename);
  2623. s = ((char *) backtrace_alloc (state, dir_len + filename_len + 2,
  2624. hdr_buf->error_callback,
  2625. hdr_buf->data));
  2626. if (s == NULL)
  2627. return 0;
  2628. memcpy (s, dir, dir_len);
  2629. /* FIXME: If we are on a DOS-based file system, and the
  2630. directory or the file name use backslashes, then we
  2631. should use a backslash here. */
  2632. s[dir_len] = '/';
  2633. memcpy (s + dir_len + 1, filename, filename_len + 1);
  2634. hdr->filenames[i] = s;
  2635. }
  2636. /* Ignore the modification time and size. */
  2637. read_uleb128 (hdr_buf);
  2638. read_uleb128 (hdr_buf);
  2639. ++i;
  2640. }
  2641. return 1;
  2642. }
  2643. /* Read a single version 5 LNCT entry for a directory or file name in a
  2644. line header. Sets *STRING to the resulting name, ignoring other
  2645. data. Return 1 on success, 0 on failure. */
  2646. static int
  2647. read_lnct (struct backtrace_state *state, struct dwarf_data *ddata,
  2648. struct unit *u, struct dwarf_buf *hdr_buf,
  2649. const struct line_header *hdr, size_t formats_count,
  2650. const struct line_header_format *formats, const char **string)
  2651. {
  2652. size_t i;
  2653. const char *dir;
  2654. const char *path;
  2655. dir = NULL;
  2656. path = NULL;
  2657. for (i = 0; i < formats_count; i++)
  2658. {
  2659. struct attr_val val;
  2660. if (!read_attribute (formats[i].form, 0, hdr_buf, u->is_dwarf64,
  2661. u->version, hdr->addrsize, &ddata->dwarf_sections,
  2662. ddata->altlink, &val))
  2663. return 0;
  2664. switch (formats[i].lnct)
  2665. {
  2666. case DW_LNCT_path:
  2667. if (!resolve_string (&ddata->dwarf_sections, u->is_dwarf64,
  2668. ddata->is_bigendian, u->str_offsets_base,
  2669. &val, hdr_buf->error_callback, hdr_buf->data,
  2670. &path))
  2671. return 0;
  2672. break;
  2673. case DW_LNCT_directory_index:
  2674. if (val.encoding == ATTR_VAL_UINT)
  2675. {
  2676. if (val.u.uint >= hdr->dirs_count)
  2677. {
  2678. dwarf_buf_error (hdr_buf,
  2679. ("invalid directory index in "
  2680. "line number program header"),
  2681. 0);
  2682. return 0;
  2683. }
  2684. dir = hdr->dirs[val.u.uint];
  2685. }
  2686. break;
  2687. default:
  2688. /* We don't care about timestamps or sizes or hashes. */
  2689. break;
  2690. }
  2691. }
  2692. if (path == NULL)
  2693. {
  2694. dwarf_buf_error (hdr_buf,
  2695. "missing file name in line number program header",
  2696. 0);
  2697. return 0;
  2698. }
  2699. if (dir == NULL)
  2700. *string = path;
  2701. else
  2702. {
  2703. size_t dir_len;
  2704. size_t path_len;
  2705. char *s;
  2706. dir_len = strlen (dir);
  2707. path_len = strlen (path);
  2708. s = (char *) backtrace_alloc (state, dir_len + path_len + 2,
  2709. hdr_buf->error_callback, hdr_buf->data);
  2710. if (s == NULL)
  2711. return 0;
  2712. memcpy (s, dir, dir_len);
  2713. /* FIXME: If we are on a DOS-based file system, and the
  2714. directory or the path name use backslashes, then we should
  2715. use a backslash here. */
  2716. s[dir_len] = '/';
  2717. memcpy (s + dir_len + 1, path, path_len + 1);
  2718. *string = s;
  2719. }
  2720. return 1;
  2721. }
  2722. /* Read a set of DWARF 5 line header format entries, setting *PCOUNT
  2723. and *PPATHS. Return 1 on success, 0 on failure. */
  2724. static int
  2725. read_line_header_format_entries (struct backtrace_state *state,
  2726. struct dwarf_data *ddata,
  2727. struct unit *u,
  2728. struct dwarf_buf *hdr_buf,
  2729. struct line_header *hdr,
  2730. size_t *pcount,
  2731. const char ***ppaths)
  2732. {
  2733. size_t formats_count;
  2734. struct line_header_format *formats;
  2735. size_t paths_count;
  2736. const char **paths;
  2737. size_t i;
  2738. int ret;
  2739. formats_count = read_byte (hdr_buf);
  2740. if (formats_count == 0)
  2741. formats = NULL;
  2742. else
  2743. {
  2744. formats = ((struct line_header_format *)
  2745. backtrace_alloc (state,
  2746. (formats_count
  2747. * sizeof (struct line_header_format)),
  2748. hdr_buf->error_callback,
  2749. hdr_buf->data));
  2750. if (formats == NULL)
  2751. return 0;
  2752. for (i = 0; i < formats_count; i++)
  2753. {
  2754. formats[i].lnct = (int) read_uleb128(hdr_buf);
  2755. formats[i].form = (enum dwarf_form) read_uleb128 (hdr_buf);
  2756. }
  2757. }
  2758. paths_count = read_uleb128 (hdr_buf);
  2759. if (paths_count == 0)
  2760. {
  2761. *pcount = 0;
  2762. *ppaths = NULL;
  2763. ret = 1;
  2764. goto exit;
  2765. }
  2766. paths = ((const char **)
  2767. backtrace_alloc (state, paths_count * sizeof (const char *),
  2768. hdr_buf->error_callback, hdr_buf->data));
  2769. if (paths == NULL)
  2770. {
  2771. ret = 0;
  2772. goto exit;
  2773. }
  2774. for (i = 0; i < paths_count; i++)
  2775. {
  2776. if (!read_lnct (state, ddata, u, hdr_buf, hdr, formats_count,
  2777. formats, &paths[i]))
  2778. {
  2779. backtrace_free (state, paths,
  2780. paths_count * sizeof (const char *),
  2781. hdr_buf->error_callback, hdr_buf->data);
  2782. ret = 0;
  2783. goto exit;
  2784. }
  2785. }
  2786. *pcount = paths_count;
  2787. *ppaths = paths;
  2788. ret = 1;
  2789. exit:
  2790. if (formats != NULL)
  2791. backtrace_free (state, formats,
  2792. formats_count * sizeof (struct line_header_format),
  2793. hdr_buf->error_callback, hdr_buf->data);
  2794. return ret;
  2795. }
  2796. /* Read the line header. Return 1 on success, 0 on failure. */
  2797. static int
  2798. read_line_header (struct backtrace_state *state, struct dwarf_data *ddata,
  2799. struct unit *u, int is_dwarf64, struct dwarf_buf *line_buf,
  2800. struct line_header *hdr)
  2801. {
  2802. uint64_t hdrlen;
  2803. struct dwarf_buf hdr_buf;
  2804. hdr->version = read_uint16 (line_buf);
  2805. if (hdr->version < 2 || hdr->version > 5)
  2806. {
  2807. dwarf_buf_error (line_buf, "unsupported line number version", -1);
  2808. return 0;
  2809. }
  2810. if (hdr->version < 5)
  2811. hdr->addrsize = u->addrsize;
  2812. else
  2813. {
  2814. hdr->addrsize = read_byte (line_buf);
  2815. /* We could support a non-zero segment_selector_size but I doubt
  2816. we'll ever see it. */
  2817. if (read_byte (line_buf) != 0)
  2818. {
  2819. dwarf_buf_error (line_buf,
  2820. "non-zero segment_selector_size not supported",
  2821. -1);
  2822. return 0;
  2823. }
  2824. }
  2825. hdrlen = read_offset (line_buf, is_dwarf64);
  2826. hdr_buf = *line_buf;
  2827. hdr_buf.left = hdrlen;
  2828. if (!advance (line_buf, hdrlen))
  2829. return 0;
  2830. hdr->min_insn_len = read_byte (&hdr_buf);
  2831. if (hdr->version < 4)
  2832. hdr->max_ops_per_insn = 1;
  2833. else
  2834. hdr->max_ops_per_insn = read_byte (&hdr_buf);
  2835. /* We don't care about default_is_stmt. */
  2836. read_byte (&hdr_buf);
  2837. hdr->line_base = read_sbyte (&hdr_buf);
  2838. hdr->line_range = read_byte (&hdr_buf);
  2839. hdr->opcode_base = read_byte (&hdr_buf);
  2840. hdr->opcode_lengths = hdr_buf.buf;
  2841. if (!advance (&hdr_buf, hdr->opcode_base - 1))
  2842. return 0;
  2843. if (hdr->version < 5)
  2844. {
  2845. if (!read_v2_paths (state, u, &hdr_buf, hdr))
  2846. return 0;
  2847. }
  2848. else
  2849. {
  2850. if (!read_line_header_format_entries (state, ddata, u, &hdr_buf, hdr,
  2851. &hdr->dirs_count,
  2852. &hdr->dirs))
  2853. return 0;
  2854. if (!read_line_header_format_entries (state, ddata, u, &hdr_buf, hdr,
  2855. &hdr->filenames_count,
  2856. &hdr->filenames))
  2857. return 0;
  2858. }
  2859. if (hdr_buf.reported_underflow)
  2860. return 0;
  2861. return 1;
  2862. }
  2863. /* Read the line program, adding line mappings to VEC. Return 1 on
  2864. success, 0 on failure. */
  2865. static int
  2866. read_line_program (struct backtrace_state *state, struct dwarf_data *ddata,
  2867. const struct line_header *hdr, struct dwarf_buf *line_buf,
  2868. struct line_vector *vec)
  2869. {
  2870. uint64_t address;
  2871. unsigned int op_index;
  2872. const char *reset_filename;
  2873. const char *filename;
  2874. int lineno;
  2875. address = 0;
  2876. op_index = 0;
  2877. if (hdr->filenames_count > 1)
  2878. reset_filename = hdr->filenames[1];
  2879. else
  2880. reset_filename = "";
  2881. filename = reset_filename;
  2882. lineno = 1;
  2883. while (line_buf->left > 0)
  2884. {
  2885. unsigned int op;
  2886. op = read_byte (line_buf);
  2887. if (op >= hdr->opcode_base)
  2888. {
  2889. unsigned int advance;
  2890. /* Special opcode. */
  2891. op -= hdr->opcode_base;
  2892. advance = op / hdr->line_range;
  2893. address += (hdr->min_insn_len * (op_index + advance)
  2894. / hdr->max_ops_per_insn);
  2895. op_index = (op_index + advance) % hdr->max_ops_per_insn;
  2896. lineno += hdr->line_base + (int) (op % hdr->line_range);
  2897. add_line (state, ddata, address, filename, lineno,
  2898. line_buf->error_callback, line_buf->data, vec);
  2899. }
  2900. else if (op == DW_LNS_extended_op)
  2901. {
  2902. uint64_t len;
  2903. len = read_uleb128 (line_buf);
  2904. op = read_byte (line_buf);
  2905. switch (op)
  2906. {
  2907. case DW_LNE_end_sequence:
  2908. /* FIXME: Should we mark the high PC here? It seems
  2909. that we already have that information from the
  2910. compilation unit. */
  2911. address = 0;
  2912. op_index = 0;
  2913. filename = reset_filename;
  2914. lineno = 1;
  2915. break;
  2916. case DW_LNE_set_address:
  2917. address = read_address (line_buf, hdr->addrsize);
  2918. break;
  2919. case DW_LNE_define_file:
  2920. {
  2921. const char *f;
  2922. unsigned int dir_index;
  2923. f = read_string (line_buf);
  2924. if (f == NULL)
  2925. return 0;
  2926. dir_index = read_uleb128 (line_buf);
  2927. /* Ignore that time and length. */
  2928. read_uleb128 (line_buf);
  2929. read_uleb128 (line_buf);
  2930. if (IS_ABSOLUTE_PATH (f))
  2931. filename = f;
  2932. else
  2933. {
  2934. const char *dir;
  2935. size_t dir_len;
  2936. size_t f_len;
  2937. char *p;
  2938. if (dir_index < hdr->dirs_count)
  2939. dir = hdr->dirs[dir_index];
  2940. else
  2941. {
  2942. dwarf_buf_error (line_buf,
  2943. ("invalid directory index "
  2944. "in line number program"),
  2945. 0);
  2946. return 0;
  2947. }
  2948. dir_len = strlen (dir);
  2949. f_len = strlen (f);
  2950. p = ((char *)
  2951. backtrace_alloc (state, dir_len + f_len + 2,
  2952. line_buf->error_callback,
  2953. line_buf->data));
  2954. if (p == NULL)
  2955. return 0;
  2956. memcpy (p, dir, dir_len);
  2957. /* FIXME: If we are on a DOS-based file system,
  2958. and the directory or the file name use
  2959. backslashes, then we should use a backslash
  2960. here. */
  2961. p[dir_len] = '/';
  2962. memcpy (p + dir_len + 1, f, f_len + 1);
  2963. filename = p;
  2964. }
  2965. }
  2966. break;
  2967. case DW_LNE_set_discriminator:
  2968. /* We don't care about discriminators. */
  2969. read_uleb128 (line_buf);
  2970. break;
  2971. default:
  2972. if (!advance (line_buf, len - 1))
  2973. return 0;
  2974. break;
  2975. }
  2976. }
  2977. else
  2978. {
  2979. switch (op)
  2980. {
  2981. case DW_LNS_copy:
  2982. add_line (state, ddata, address, filename, lineno,
  2983. line_buf->error_callback, line_buf->data, vec);
  2984. break;
  2985. case DW_LNS_advance_pc:
  2986. {
  2987. uint64_t advance;
  2988. advance = read_uleb128 (line_buf);
  2989. address += (hdr->min_insn_len * (op_index + advance)
  2990. / hdr->max_ops_per_insn);
  2991. op_index = (op_index + advance) % hdr->max_ops_per_insn;
  2992. }
  2993. break;
  2994. case DW_LNS_advance_line:
  2995. lineno += (int) read_sleb128 (line_buf);
  2996. break;
  2997. case DW_LNS_set_file:
  2998. {
  2999. uint64_t fileno;
  3000. fileno = read_uleb128 (line_buf);
  3001. if (fileno >= hdr->filenames_count)
  3002. {
  3003. dwarf_buf_error (line_buf,
  3004. ("invalid file number in "
  3005. "line number program"),
  3006. 0);
  3007. return 0;
  3008. }
  3009. filename = hdr->filenames[fileno];
  3010. }
  3011. break;
  3012. case DW_LNS_set_column:
  3013. read_uleb128 (line_buf);
  3014. break;
  3015. case DW_LNS_negate_stmt:
  3016. break;
  3017. case DW_LNS_set_basic_block:
  3018. break;
  3019. case DW_LNS_const_add_pc:
  3020. {
  3021. unsigned int advance;
  3022. op = 255 - hdr->opcode_base;
  3023. advance = op / hdr->line_range;
  3024. address += (hdr->min_insn_len * (op_index + advance)
  3025. / hdr->max_ops_per_insn);
  3026. op_index = (op_index + advance) % hdr->max_ops_per_insn;
  3027. }
  3028. break;
  3029. case DW_LNS_fixed_advance_pc:
  3030. address += read_uint16 (line_buf);
  3031. op_index = 0;
  3032. break;
  3033. case DW_LNS_set_prologue_end:
  3034. break;
  3035. case DW_LNS_set_epilogue_begin:
  3036. break;
  3037. case DW_LNS_set_isa:
  3038. read_uleb128 (line_buf);
  3039. break;
  3040. default:
  3041. {
  3042. unsigned int i;
  3043. for (i = hdr->opcode_lengths[op - 1]; i > 0; --i)
  3044. read_uleb128 (line_buf);
  3045. }
  3046. break;
  3047. }
  3048. }
  3049. }
  3050. return 1;
  3051. }
  3052. /* Read the line number information for a compilation unit. Returns 1
  3053. on success, 0 on failure. */
  3054. static int
  3055. read_line_info (struct backtrace_state *state, struct dwarf_data *ddata,
  3056. backtrace_error_callback error_callback, void *data,
  3057. struct unit *u, struct line_header *hdr, struct line **lines,
  3058. size_t *lines_count)
  3059. {
  3060. struct line_vector vec;
  3061. struct dwarf_buf line_buf;
  3062. uint64_t len;
  3063. int is_dwarf64;
  3064. struct line *ln;
  3065. memset (&vec.vec, 0, sizeof vec.vec);
  3066. vec.count = 0;
  3067. memset (hdr, 0, sizeof *hdr);
  3068. if (u->lineoff != (off_t) (size_t) u->lineoff
  3069. || (size_t) u->lineoff >= ddata->dwarf_sections.size[DEBUG_LINE])
  3070. {
  3071. error_callback (data, "unit line offset out of range", 0);
  3072. goto fail;
  3073. }
  3074. line_buf.name = ".debug_line";
  3075. line_buf.start = ddata->dwarf_sections.data[DEBUG_LINE];
  3076. line_buf.buf = ddata->dwarf_sections.data[DEBUG_LINE] + u->lineoff;
  3077. line_buf.left = ddata->dwarf_sections.size[DEBUG_LINE] - u->lineoff;
  3078. line_buf.is_bigendian = ddata->is_bigendian;
  3079. line_buf.error_callback = error_callback;
  3080. line_buf.data = data;
  3081. line_buf.reported_underflow = 0;
  3082. len = read_initial_length (&line_buf, &is_dwarf64);
  3083. line_buf.left = len;
  3084. if (!read_line_header (state, ddata, u, is_dwarf64, &line_buf, hdr))
  3085. goto fail;
  3086. if (!read_line_program (state, ddata, hdr, &line_buf, &vec))
  3087. goto fail;
  3088. if (line_buf.reported_underflow)
  3089. goto fail;
  3090. if (vec.count == 0)
  3091. {
  3092. /* This is not a failure in the sense of generating an error,
  3093. but it is a failure in that sense that we have no useful
  3094. information. */
  3095. goto fail;
  3096. }
  3097. /* Allocate one extra entry at the end. */
  3098. ln = ((struct line *)
  3099. backtrace_vector_grow (state, sizeof (struct line), error_callback,
  3100. data, &vec.vec));
  3101. if (ln == NULL)
  3102. goto fail;
  3103. ln->pc = (uintptr_t) -1;
  3104. ln->filename = NULL;
  3105. ln->lineno = 0;
  3106. ln->idx = 0;
  3107. if (!backtrace_vector_release (state, &vec.vec, error_callback, data))
  3108. goto fail;
  3109. ln = (struct line *) vec.vec.base;
  3110. backtrace_qsort (ln, vec.count, sizeof (struct line), line_compare);
  3111. *lines = ln;
  3112. *lines_count = vec.count;
  3113. return 1;
  3114. fail:
  3115. backtrace_vector_free (state, &vec.vec, error_callback, data);
  3116. free_line_header (state, hdr, error_callback, data);
  3117. *lines = (struct line *) (uintptr_t) -1;
  3118. *lines_count = 0;
  3119. return 0;
  3120. }
  3121. static const char *read_referenced_name (struct dwarf_data *, struct unit *,
  3122. uint64_t, backtrace_error_callback,
  3123. void *);
  3124. /* Read the name of a function from a DIE referenced by ATTR with VAL. */
  3125. static const char *
  3126. read_referenced_name_from_attr (struct dwarf_data *ddata, struct unit *u,
  3127. struct attr *attr, struct attr_val *val,
  3128. backtrace_error_callback error_callback,
  3129. void *data)
  3130. {
  3131. switch (attr->name)
  3132. {
  3133. case DW_AT_abstract_origin:
  3134. case DW_AT_specification:
  3135. break;
  3136. default:
  3137. return NULL;
  3138. }
  3139. if (attr->form == DW_FORM_ref_sig8)
  3140. return NULL;
  3141. if (val->encoding == ATTR_VAL_REF_INFO)
  3142. {
  3143. struct unit *unit
  3144. = find_unit (ddata->units, ddata->units_count,
  3145. val->u.uint);
  3146. if (unit == NULL)
  3147. return NULL;
  3148. uint64_t offset = val->u.uint - unit->low_offset;
  3149. return read_referenced_name (ddata, unit, offset, error_callback, data);
  3150. }
  3151. if (val->encoding == ATTR_VAL_UINT
  3152. || val->encoding == ATTR_VAL_REF_UNIT)
  3153. return read_referenced_name (ddata, u, val->u.uint, error_callback, data);
  3154. if (val->encoding == ATTR_VAL_REF_ALT_INFO)
  3155. {
  3156. struct unit *alt_unit
  3157. = find_unit (ddata->altlink->units, ddata->altlink->units_count,
  3158. val->u.uint);
  3159. if (alt_unit == NULL)
  3160. return NULL;
  3161. uint64_t offset = val->u.uint - alt_unit->low_offset;
  3162. return read_referenced_name (ddata->altlink, alt_unit, offset,
  3163. error_callback, data);
  3164. }
  3165. return NULL;
  3166. }
  3167. /* Read the name of a function from a DIE referenced by a
  3168. DW_AT_abstract_origin or DW_AT_specification tag. OFFSET is within
  3169. the same compilation unit. */
  3170. static const char *
  3171. read_referenced_name (struct dwarf_data *ddata, struct unit *u,
  3172. uint64_t offset, backtrace_error_callback error_callback,
  3173. void *data)
  3174. {
  3175. struct dwarf_buf unit_buf;
  3176. uint64_t code;
  3177. const struct abbrev *abbrev;
  3178. const char *ret;
  3179. size_t i;
  3180. /* OFFSET is from the start of the data for this compilation unit.
  3181. U->unit_data is the data, but it starts U->unit_data_offset bytes
  3182. from the beginning. */
  3183. if (offset < u->unit_data_offset
  3184. || offset - u->unit_data_offset >= u->unit_data_len)
  3185. {
  3186. error_callback (data,
  3187. "abstract origin or specification out of range",
  3188. 0);
  3189. return NULL;
  3190. }
  3191. offset -= u->unit_data_offset;
  3192. unit_buf.name = ".debug_info";
  3193. unit_buf.start = ddata->dwarf_sections.data[DEBUG_INFO];
  3194. unit_buf.buf = u->unit_data + offset;
  3195. unit_buf.left = u->unit_data_len - offset;
  3196. unit_buf.is_bigendian = ddata->is_bigendian;
  3197. unit_buf.error_callback = error_callback;
  3198. unit_buf.data = data;
  3199. unit_buf.reported_underflow = 0;
  3200. code = read_uleb128 (&unit_buf);
  3201. if (code == 0)
  3202. {
  3203. dwarf_buf_error (&unit_buf,
  3204. "invalid abstract origin or specification",
  3205. 0);
  3206. return NULL;
  3207. }
  3208. abbrev = lookup_abbrev (&u->abbrevs, code, error_callback, data);
  3209. if (abbrev == NULL)
  3210. return NULL;
  3211. ret = NULL;
  3212. for (i = 0; i < abbrev->num_attrs; ++i)
  3213. {
  3214. struct attr_val val;
  3215. if (!read_attribute (abbrev->attrs[i].form, abbrev->attrs[i].val,
  3216. &unit_buf, u->is_dwarf64, u->version, u->addrsize,
  3217. &ddata->dwarf_sections, ddata->altlink, &val))
  3218. return NULL;
  3219. switch (abbrev->attrs[i].name)
  3220. {
  3221. case DW_AT_name:
  3222. /* Third name preference: don't override. A name we found in some
  3223. other way, will normally be more useful -- e.g., this name is
  3224. normally not mangled. */
  3225. if (ret != NULL)
  3226. break;
  3227. if (!resolve_string (&ddata->dwarf_sections, u->is_dwarf64,
  3228. ddata->is_bigendian, u->str_offsets_base,
  3229. &val, error_callback, data, &ret))
  3230. return NULL;
  3231. break;
  3232. case DW_AT_linkage_name:
  3233. case DW_AT_MIPS_linkage_name:
  3234. /* First name preference: override all. */
  3235. {
  3236. const char *s;
  3237. s = NULL;
  3238. if (!resolve_string (&ddata->dwarf_sections, u->is_dwarf64,
  3239. ddata->is_bigendian, u->str_offsets_base,
  3240. &val, error_callback, data, &s))
  3241. return NULL;
  3242. if (s != NULL)
  3243. return s;
  3244. }
  3245. break;
  3246. case DW_AT_specification:
  3247. /* Second name preference: override DW_AT_name, don't override
  3248. DW_AT_linkage_name. */
  3249. {
  3250. const char *name;
  3251. name = read_referenced_name_from_attr (ddata, u, &abbrev->attrs[i],
  3252. &val, error_callback, data);
  3253. if (name != NULL)
  3254. ret = name;
  3255. }
  3256. break;
  3257. default:
  3258. break;
  3259. }
  3260. }
  3261. return ret;
  3262. }
  3263. /* Add a range to a unit that maps to a function. This is called via
  3264. add_ranges. Returns 1 on success, 0 on error. */
  3265. static int
  3266. add_function_range (struct backtrace_state *state, void *rdata,
  3267. uintptr_t lowpc, uintptr_t highpc,
  3268. backtrace_error_callback error_callback, void *data,
  3269. void *pvec)
  3270. {
  3271. struct function *function = (struct function *) rdata;
  3272. struct function_vector *vec = (struct function_vector *) pvec;
  3273. struct function_addrs *p;
  3274. if (vec->count > 0)
  3275. {
  3276. p = (struct function_addrs *) vec->vec.base + (vec->count - 1);
  3277. if ((lowpc == p->high || lowpc == p->high + 1)
  3278. && function == p->function)
  3279. {
  3280. if (highpc > p->high)
  3281. p->high = highpc;
  3282. return 1;
  3283. }
  3284. }
  3285. p = ((struct function_addrs *)
  3286. backtrace_vector_grow (state, sizeof (struct function_addrs),
  3287. error_callback, data, &vec->vec));
  3288. if (p == NULL)
  3289. return 0;
  3290. p->low = lowpc;
  3291. p->high = highpc;
  3292. p->function = function;
  3293. ++vec->count;
  3294. return 1;
  3295. }
  3296. /* Read one entry plus all its children. Add function addresses to
  3297. VEC. Returns 1 on success, 0 on error. */
  3298. static int
  3299. read_function_entry (struct backtrace_state *state, struct dwarf_data *ddata,
  3300. struct unit *u, uintptr_t base, struct dwarf_buf *unit_buf,
  3301. const struct line_header *lhdr,
  3302. backtrace_error_callback error_callback, void *data,
  3303. struct function_vector *vec_function,
  3304. struct function_vector *vec_inlined)
  3305. {
  3306. while (unit_buf->left > 0)
  3307. {
  3308. uint64_t code;
  3309. const struct abbrev *abbrev;
  3310. int is_function;
  3311. struct function *function;
  3312. struct function_vector *vec;
  3313. size_t i;
  3314. struct pcrange pcrange;
  3315. int have_linkage_name;
  3316. code = read_uleb128 (unit_buf);
  3317. if (code == 0)
  3318. return 1;
  3319. abbrev = lookup_abbrev (&u->abbrevs, code, error_callback, data);
  3320. if (abbrev == NULL)
  3321. return 0;
  3322. is_function = (abbrev->tag == DW_TAG_subprogram
  3323. || abbrev->tag == DW_TAG_entry_point
  3324. || abbrev->tag == DW_TAG_inlined_subroutine);
  3325. if (abbrev->tag == DW_TAG_inlined_subroutine)
  3326. vec = vec_inlined;
  3327. else
  3328. vec = vec_function;
  3329. function = NULL;
  3330. if (is_function)
  3331. {
  3332. function = ((struct function *)
  3333. backtrace_alloc (state, sizeof *function,
  3334. error_callback, data));
  3335. if (function == NULL)
  3336. return 0;
  3337. memset (function, 0, sizeof *function);
  3338. }
  3339. memset (&pcrange, 0, sizeof pcrange);
  3340. have_linkage_name = 0;
  3341. for (i = 0; i < abbrev->num_attrs; ++i)
  3342. {
  3343. struct attr_val val;
  3344. if (!read_attribute (abbrev->attrs[i].form, abbrev->attrs[i].val,
  3345. unit_buf, u->is_dwarf64, u->version,
  3346. u->addrsize, &ddata->dwarf_sections,
  3347. ddata->altlink, &val))
  3348. return 0;
  3349. /* The compile unit sets the base address for any address
  3350. ranges in the function entries. */
  3351. if ((abbrev->tag == DW_TAG_compile_unit
  3352. || abbrev->tag == DW_TAG_skeleton_unit)
  3353. && abbrev->attrs[i].name == DW_AT_low_pc)
  3354. {
  3355. if (val.encoding == ATTR_VAL_ADDRESS)
  3356. base = (uintptr_t) val.u.uint;
  3357. else if (val.encoding == ATTR_VAL_ADDRESS_INDEX)
  3358. {
  3359. if (!resolve_addr_index (&ddata->dwarf_sections,
  3360. u->addr_base, u->addrsize,
  3361. ddata->is_bigendian, val.u.uint,
  3362. error_callback, data, &base))
  3363. return 0;
  3364. }
  3365. }
  3366. if (is_function)
  3367. {
  3368. switch (abbrev->attrs[i].name)
  3369. {
  3370. case DW_AT_call_file:
  3371. if (val.encoding == ATTR_VAL_UINT)
  3372. {
  3373. if (val.u.uint >= lhdr->filenames_count)
  3374. {
  3375. dwarf_buf_error (unit_buf,
  3376. ("invalid file number in "
  3377. "DW_AT_call_file attribute"),
  3378. 0);
  3379. return 0;
  3380. }
  3381. function->caller_filename = lhdr->filenames[val.u.uint];
  3382. }
  3383. break;
  3384. case DW_AT_call_line:
  3385. if (val.encoding == ATTR_VAL_UINT)
  3386. function->caller_lineno = val.u.uint;
  3387. break;
  3388. case DW_AT_abstract_origin:
  3389. case DW_AT_specification:
  3390. /* Second name preference: override DW_AT_name, don't override
  3391. DW_AT_linkage_name. */
  3392. if (have_linkage_name)
  3393. break;
  3394. {
  3395. const char *name;
  3396. name
  3397. = read_referenced_name_from_attr (ddata, u,
  3398. &abbrev->attrs[i], &val,
  3399. error_callback, data);
  3400. if (name != NULL)
  3401. function->name = name;
  3402. }
  3403. break;
  3404. case DW_AT_name:
  3405. /* Third name preference: don't override. */
  3406. if (function->name != NULL)
  3407. break;
  3408. if (!resolve_string (&ddata->dwarf_sections, u->is_dwarf64,
  3409. ddata->is_bigendian,
  3410. u->str_offsets_base, &val,
  3411. error_callback, data, &function->name))
  3412. return 0;
  3413. break;
  3414. case DW_AT_linkage_name:
  3415. case DW_AT_MIPS_linkage_name:
  3416. /* First name preference: override all. */
  3417. {
  3418. const char *s;
  3419. s = NULL;
  3420. if (!resolve_string (&ddata->dwarf_sections, u->is_dwarf64,
  3421. ddata->is_bigendian,
  3422. u->str_offsets_base, &val,
  3423. error_callback, data, &s))
  3424. return 0;
  3425. if (s != NULL)
  3426. {
  3427. function->name = s;
  3428. have_linkage_name = 1;
  3429. }
  3430. }
  3431. break;
  3432. case DW_AT_low_pc: case DW_AT_high_pc: case DW_AT_ranges:
  3433. update_pcrange (&abbrev->attrs[i], &val, &pcrange);
  3434. break;
  3435. default:
  3436. break;
  3437. }
  3438. }
  3439. }
  3440. /* If we couldn't find a name for the function, we have no use
  3441. for it. */
  3442. if (is_function && function->name == NULL)
  3443. {
  3444. backtrace_free (state, function, sizeof *function,
  3445. error_callback, data);
  3446. is_function = 0;
  3447. }
  3448. if (is_function)
  3449. {
  3450. if (pcrange.have_ranges
  3451. || (pcrange.have_lowpc && pcrange.have_highpc))
  3452. {
  3453. if (!add_ranges (state, &ddata->dwarf_sections,
  3454. ddata->base_address, ddata->is_bigendian,
  3455. u, base, &pcrange, add_function_range,
  3456. (void *) function, error_callback, data,
  3457. (void *) vec))
  3458. return 0;
  3459. }
  3460. else
  3461. {
  3462. backtrace_free (state, function, sizeof *function,
  3463. error_callback, data);
  3464. is_function = 0;
  3465. }
  3466. }
  3467. if (abbrev->has_children)
  3468. {
  3469. if (!is_function)
  3470. {
  3471. if (!read_function_entry (state, ddata, u, base, unit_buf, lhdr,
  3472. error_callback, data, vec_function,
  3473. vec_inlined))
  3474. return 0;
  3475. }
  3476. else
  3477. {
  3478. struct function_vector fvec;
  3479. /* Gather any information for inlined functions in
  3480. FVEC. */
  3481. memset (&fvec, 0, sizeof fvec);
  3482. if (!read_function_entry (state, ddata, u, base, unit_buf, lhdr,
  3483. error_callback, data, vec_function,
  3484. &fvec))
  3485. return 0;
  3486. if (fvec.count > 0)
  3487. {
  3488. struct function_addrs *p;
  3489. struct function_addrs *faddrs;
  3490. /* Allocate a trailing entry, but don't include it
  3491. in fvec.count. */
  3492. p = ((struct function_addrs *)
  3493. backtrace_vector_grow (state,
  3494. sizeof (struct function_addrs),
  3495. error_callback, data,
  3496. &fvec.vec));
  3497. if (p == NULL)
  3498. return 0;
  3499. p->low = 0;
  3500. --p->low;
  3501. p->high = p->low;
  3502. p->function = NULL;
  3503. if (!backtrace_vector_release (state, &fvec.vec,
  3504. error_callback, data))
  3505. return 0;
  3506. faddrs = (struct function_addrs *) fvec.vec.base;
  3507. backtrace_qsort (faddrs, fvec.count,
  3508. sizeof (struct function_addrs),
  3509. function_addrs_compare);
  3510. function->function_addrs = faddrs;
  3511. function->function_addrs_count = fvec.count;
  3512. }
  3513. }
  3514. }
  3515. }
  3516. return 1;
  3517. }
  3518. /* Read function name information for a compilation unit. We look
  3519. through the whole unit looking for function tags. */
  3520. static void
  3521. read_function_info (struct backtrace_state *state, struct dwarf_data *ddata,
  3522. const struct line_header *lhdr,
  3523. backtrace_error_callback error_callback, void *data,
  3524. struct unit *u, struct function_vector *fvec,
  3525. struct function_addrs **ret_addrs,
  3526. size_t *ret_addrs_count)
  3527. {
  3528. struct function_vector lvec;
  3529. struct function_vector *pfvec;
  3530. struct dwarf_buf unit_buf;
  3531. struct function_addrs *p;
  3532. struct function_addrs *addrs;
  3533. size_t addrs_count;
  3534. /* Use FVEC if it is not NULL. Otherwise use our own vector. */
  3535. if (fvec != NULL)
  3536. pfvec = fvec;
  3537. else
  3538. {
  3539. memset (&lvec, 0, sizeof lvec);
  3540. pfvec = &lvec;
  3541. }
  3542. unit_buf.name = ".debug_info";
  3543. unit_buf.start = ddata->dwarf_sections.data[DEBUG_INFO];
  3544. unit_buf.buf = u->unit_data;
  3545. unit_buf.left = u->unit_data_len;
  3546. unit_buf.is_bigendian = ddata->is_bigendian;
  3547. unit_buf.error_callback = error_callback;
  3548. unit_buf.data = data;
  3549. unit_buf.reported_underflow = 0;
  3550. while (unit_buf.left > 0)
  3551. {
  3552. if (!read_function_entry (state, ddata, u, 0, &unit_buf, lhdr,
  3553. error_callback, data, pfvec, pfvec))
  3554. return;
  3555. }
  3556. if (pfvec->count == 0)
  3557. return;
  3558. /* Allocate a trailing entry, but don't include it in
  3559. pfvec->count. */
  3560. p = ((struct function_addrs *)
  3561. backtrace_vector_grow (state, sizeof (struct function_addrs),
  3562. error_callback, data, &pfvec->vec));
  3563. if (p == NULL)
  3564. return;
  3565. p->low = 0;
  3566. --p->low;
  3567. p->high = p->low;
  3568. p->function = NULL;
  3569. addrs_count = pfvec->count;
  3570. if (fvec == NULL)
  3571. {
  3572. if (!backtrace_vector_release (state, &lvec.vec, error_callback, data))
  3573. return;
  3574. addrs = (struct function_addrs *) pfvec->vec.base;
  3575. }
  3576. else
  3577. {
  3578. /* Finish this list of addresses, but leave the remaining space in
  3579. the vector available for the next function unit. */
  3580. addrs = ((struct function_addrs *)
  3581. backtrace_vector_finish (state, &fvec->vec,
  3582. error_callback, data));
  3583. if (addrs == NULL)
  3584. return;
  3585. fvec->count = 0;
  3586. }
  3587. backtrace_qsort (addrs, addrs_count, sizeof (struct function_addrs),
  3588. function_addrs_compare);
  3589. *ret_addrs = addrs;
  3590. *ret_addrs_count = addrs_count;
  3591. }
  3592. /* See if PC is inlined in FUNCTION. If it is, print out the inlined
  3593. information, and update FILENAME and LINENO for the caller.
  3594. Returns whatever CALLBACK returns, or 0 to keep going. */
  3595. static int
  3596. report_inlined_functions (uintptr_t pc, struct function *function,
  3597. backtrace_full_callback callback, void *data,
  3598. const char **filename, int *lineno)
  3599. {
  3600. struct function_addrs *p;
  3601. struct function_addrs *match;
  3602. struct function *inlined;
  3603. int ret;
  3604. if (function->function_addrs_count == 0)
  3605. return 0;
  3606. /* Our search isn't safe if pc == -1, as that is the sentinel
  3607. value. */
  3608. if (pc + 1 == 0)
  3609. return 0;
  3610. p = ((struct function_addrs *)
  3611. bsearch (&pc, function->function_addrs,
  3612. function->function_addrs_count,
  3613. sizeof (struct function_addrs),
  3614. function_addrs_search));
  3615. if (p == NULL)
  3616. return 0;
  3617. /* Here pc >= p->low && pc < (p + 1)->low. The function_addrs are
  3618. sorted by low, so if pc > p->low we are at the end of a range of
  3619. function_addrs with the same low value. If pc == p->low walk
  3620. forward to the end of the range with that low value. Then walk
  3621. backward and use the first range that includes pc. */
  3622. while (pc == (p + 1)->low)
  3623. ++p;
  3624. match = NULL;
  3625. while (1)
  3626. {
  3627. if (pc < p->high)
  3628. {
  3629. match = p;
  3630. break;
  3631. }
  3632. if (p == function->function_addrs)
  3633. break;
  3634. if ((p - 1)->low < p->low)
  3635. break;
  3636. --p;
  3637. }
  3638. if (match == NULL)
  3639. return 0;
  3640. /* We found an inlined call. */
  3641. inlined = match->function;
  3642. /* Report any calls inlined into this one. */
  3643. ret = report_inlined_functions (pc, inlined, callback, data,
  3644. filename, lineno);
  3645. if (ret != 0)
  3646. return ret;
  3647. /* Report this inlined call. */
  3648. ret = callback (data, pc, *filename, *lineno, inlined->name);
  3649. if (ret != 0)
  3650. return ret;
  3651. /* Our caller will report the caller of the inlined function; tell
  3652. it the appropriate filename and line number. */
  3653. *filename = inlined->caller_filename;
  3654. *lineno = inlined->caller_lineno;
  3655. return 0;
  3656. }
  3657. /* Look for a PC in the DWARF mapping for one module. On success,
  3658. call CALLBACK and return whatever it returns. On error, call
  3659. ERROR_CALLBACK and return 0. Sets *FOUND to 1 if the PC is found,
  3660. 0 if not. */
  3661. static int
  3662. dwarf_lookup_pc (struct backtrace_state *state, struct dwarf_data *ddata,
  3663. uintptr_t pc, backtrace_full_callback callback,
  3664. backtrace_error_callback error_callback, void *data,
  3665. int *found)
  3666. {
  3667. struct unit_addrs *entry;
  3668. int found_entry;
  3669. struct unit *u;
  3670. int new_data;
  3671. struct line *lines;
  3672. struct line *ln;
  3673. struct function_addrs *p;
  3674. struct function_addrs *fmatch;
  3675. struct function *function;
  3676. const char *filename;
  3677. int lineno;
  3678. int ret;
  3679. *found = 1;
  3680. /* Find an address range that includes PC. Our search isn't safe if
  3681. PC == -1, as we use that as a sentinel value, so skip the search
  3682. in that case. */
  3683. entry = (ddata->addrs_count == 0 || pc + 1 == 0
  3684. ? NULL
  3685. : bsearch (&pc, ddata->addrs, ddata->addrs_count,
  3686. sizeof (struct unit_addrs), unit_addrs_search));
  3687. if (entry == NULL)
  3688. {
  3689. *found = 0;
  3690. return 0;
  3691. }
  3692. /* Here pc >= entry->low && pc < (entry + 1)->low. The unit_addrs
  3693. are sorted by low, so if pc > p->low we are at the end of a range
  3694. of unit_addrs with the same low value. If pc == p->low walk
  3695. forward to the end of the range with that low value. Then walk
  3696. backward and use the first range that includes pc. */
  3697. while (pc == (entry + 1)->low)
  3698. ++entry;
  3699. found_entry = 0;
  3700. while (1)
  3701. {
  3702. if (pc < entry->high)
  3703. {
  3704. found_entry = 1;
  3705. break;
  3706. }
  3707. if (entry == ddata->addrs)
  3708. break;
  3709. if ((entry - 1)->low < entry->low)
  3710. break;
  3711. --entry;
  3712. }
  3713. if (!found_entry)
  3714. {
  3715. *found = 0;
  3716. return 0;
  3717. }
  3718. /* We need the lines, lines_count, function_addrs,
  3719. function_addrs_count fields of u. If they are not set, we need
  3720. to set them. When running in threaded mode, we need to allow for
  3721. the possibility that some other thread is setting them
  3722. simultaneously. */
  3723. u = entry->u;
  3724. lines = u->lines;
  3725. /* Skip units with no useful line number information by walking
  3726. backward. Useless line number information is marked by setting
  3727. lines == -1. */
  3728. while (entry > ddata->addrs
  3729. && pc >= (entry - 1)->low
  3730. && pc < (entry - 1)->high)
  3731. {
  3732. if (state->threaded)
  3733. lines = (struct line *) backtrace_atomic_load_pointer (&u->lines);
  3734. if (lines != (struct line *) (uintptr_t) -1)
  3735. break;
  3736. --entry;
  3737. u = entry->u;
  3738. lines = u->lines;
  3739. }
  3740. if (state->threaded)
  3741. lines = backtrace_atomic_load_pointer (&u->lines);
  3742. new_data = 0;
  3743. if (lines == NULL)
  3744. {
  3745. struct function_addrs *function_addrs;
  3746. size_t function_addrs_count;
  3747. struct line_header lhdr;
  3748. size_t count;
  3749. /* We have never read the line information for this unit. Read
  3750. it now. */
  3751. function_addrs = NULL;
  3752. function_addrs_count = 0;
  3753. if (read_line_info (state, ddata, error_callback, data, entry->u, &lhdr,
  3754. &lines, &count))
  3755. {
  3756. struct function_vector *pfvec;
  3757. /* If not threaded, reuse DDATA->FVEC for better memory
  3758. consumption. */
  3759. if (state->threaded)
  3760. pfvec = NULL;
  3761. else
  3762. pfvec = &ddata->fvec;
  3763. read_function_info (state, ddata, &lhdr, error_callback, data,
  3764. entry->u, pfvec, &function_addrs,
  3765. &function_addrs_count);
  3766. free_line_header (state, &lhdr, error_callback, data);
  3767. new_data = 1;
  3768. }
  3769. /* Atomically store the information we just read into the unit.
  3770. If another thread is simultaneously writing, it presumably
  3771. read the same information, and we don't care which one we
  3772. wind up with; we just leak the other one. We do have to
  3773. write the lines field last, so that the acquire-loads above
  3774. ensure that the other fields are set. */
  3775. if (!state->threaded)
  3776. {
  3777. u->lines_count = count;
  3778. u->function_addrs = function_addrs;
  3779. u->function_addrs_count = function_addrs_count;
  3780. u->lines = lines;
  3781. }
  3782. else
  3783. {
  3784. backtrace_atomic_store_size_t (&u->lines_count, count);
  3785. backtrace_atomic_store_pointer (&u->function_addrs, function_addrs);
  3786. backtrace_atomic_store_size_t (&u->function_addrs_count,
  3787. function_addrs_count);
  3788. backtrace_atomic_store_pointer (&u->lines, lines);
  3789. }
  3790. }
  3791. /* Now all fields of U have been initialized. */
  3792. if (lines == (struct line *) (uintptr_t) -1)
  3793. {
  3794. /* If reading the line number information failed in some way,
  3795. try again to see if there is a better compilation unit for
  3796. this PC. */
  3797. if (new_data)
  3798. return dwarf_lookup_pc (state, ddata, pc, callback, error_callback,
  3799. data, found);
  3800. return callback (data, pc, NULL, 0, NULL);
  3801. }
  3802. /* Search for PC within this unit. */
  3803. ln = (struct line *) bsearch (&pc, lines, entry->u->lines_count,
  3804. sizeof (struct line), line_search);
  3805. if (ln == NULL)
  3806. {
  3807. /* The PC is between the low_pc and high_pc attributes of the
  3808. compilation unit, but no entry in the line table covers it.
  3809. This implies that the start of the compilation unit has no
  3810. line number information. */
  3811. if (entry->u->abs_filename == NULL)
  3812. {
  3813. const char *filename;
  3814. filename = entry->u->filename;
  3815. if (filename != NULL
  3816. && !IS_ABSOLUTE_PATH (filename)
  3817. && entry->u->comp_dir != NULL)
  3818. {
  3819. size_t filename_len;
  3820. const char *dir;
  3821. size_t dir_len;
  3822. char *s;
  3823. filename_len = strlen (filename);
  3824. dir = entry->u->comp_dir;
  3825. dir_len = strlen (dir);
  3826. s = (char *) backtrace_alloc (state, dir_len + filename_len + 2,
  3827. error_callback, data);
  3828. if (s == NULL)
  3829. {
  3830. *found = 0;
  3831. return 0;
  3832. }
  3833. memcpy (s, dir, dir_len);
  3834. /* FIXME: Should use backslash if DOS file system. */
  3835. s[dir_len] = '/';
  3836. memcpy (s + dir_len + 1, filename, filename_len + 1);
  3837. filename = s;
  3838. }
  3839. entry->u->abs_filename = filename;
  3840. }
  3841. return callback (data, pc, entry->u->abs_filename, 0, NULL);
  3842. }
  3843. /* Search for function name within this unit. */
  3844. if (entry->u->function_addrs_count == 0)
  3845. return callback (data, pc, ln->filename, ln->lineno, NULL);
  3846. p = ((struct function_addrs *)
  3847. bsearch (&pc, entry->u->function_addrs,
  3848. entry->u->function_addrs_count,
  3849. sizeof (struct function_addrs),
  3850. function_addrs_search));
  3851. if (p == NULL)
  3852. return callback (data, pc, ln->filename, ln->lineno, NULL);
  3853. /* Here pc >= p->low && pc < (p + 1)->low. The function_addrs are
  3854. sorted by low, so if pc > p->low we are at the end of a range of
  3855. function_addrs with the same low value. If pc == p->low walk
  3856. forward to the end of the range with that low value. Then walk
  3857. backward and use the first range that includes pc. */
  3858. while (pc == (p + 1)->low)
  3859. ++p;
  3860. fmatch = NULL;
  3861. while (1)
  3862. {
  3863. if (pc < p->high)
  3864. {
  3865. fmatch = p;
  3866. break;
  3867. }
  3868. if (p == entry->u->function_addrs)
  3869. break;
  3870. if ((p - 1)->low < p->low)
  3871. break;
  3872. --p;
  3873. }
  3874. if (fmatch == NULL)
  3875. return callback (data, pc, ln->filename, ln->lineno, NULL);
  3876. function = fmatch->function;
  3877. filename = ln->filename;
  3878. lineno = ln->lineno;
  3879. ret = report_inlined_functions (pc, function, callback, data,
  3880. &filename, &lineno);
  3881. if (ret != 0)
  3882. return ret;
  3883. return callback (data, pc, filename, lineno, function->name);
  3884. }
  3885. /* Return the file/line information for a PC using the DWARF mapping
  3886. we built earlier. */
  3887. static int
  3888. dwarf_fileline (struct backtrace_state *state, uintptr_t pc,
  3889. backtrace_full_callback callback,
  3890. backtrace_error_callback error_callback, void *data)
  3891. {
  3892. struct dwarf_data *ddata;
  3893. int found;
  3894. int ret;
  3895. if (!state->threaded)
  3896. {
  3897. for (ddata = (struct dwarf_data *) state->fileline_data;
  3898. ddata != NULL;
  3899. ddata = ddata->next)
  3900. {
  3901. ret = dwarf_lookup_pc (state, ddata, pc, callback, error_callback,
  3902. data, &found);
  3903. if (ret != 0 || found)
  3904. return ret;
  3905. }
  3906. }
  3907. else
  3908. {
  3909. struct dwarf_data **pp;
  3910. pp = (struct dwarf_data **) (void *) &state->fileline_data;
  3911. while (1)
  3912. {
  3913. ddata = backtrace_atomic_load_pointer (pp);
  3914. if (ddata == NULL)
  3915. break;
  3916. ret = dwarf_lookup_pc (state, ddata, pc, callback, error_callback,
  3917. data, &found);
  3918. if (ret != 0 || found)
  3919. return ret;
  3920. pp = &ddata->next;
  3921. }
  3922. }
  3923. /* FIXME: See if any libraries have been dlopen'ed. */
  3924. return callback (data, pc, NULL, 0, NULL);
  3925. }
  3926. /* Initialize our data structures from the DWARF debug info for a
  3927. file. Return NULL on failure. */
  3928. static struct dwarf_data *
  3929. build_dwarf_data (struct backtrace_state *state,
  3930. struct libbacktrace_base_address base_address,
  3931. const struct dwarf_sections *dwarf_sections,
  3932. int is_bigendian,
  3933. struct dwarf_data *altlink,
  3934. backtrace_error_callback error_callback,
  3935. void *data)
  3936. {
  3937. struct unit_addrs_vector addrs_vec;
  3938. struct unit_vector units_vec;
  3939. struct dwarf_data *fdata;
  3940. if (!build_address_map (state, base_address, dwarf_sections, is_bigendian,
  3941. altlink, error_callback, data, &addrs_vec,
  3942. &units_vec))
  3943. return NULL;
  3944. if (!backtrace_vector_release (state, &addrs_vec.vec, error_callback, data))
  3945. return NULL;
  3946. if (!backtrace_vector_release (state, &units_vec.vec, error_callback, data))
  3947. return NULL;
  3948. backtrace_qsort ((struct unit_addrs *) addrs_vec.vec.base, addrs_vec.count,
  3949. sizeof (struct unit_addrs), unit_addrs_compare);
  3950. if (!resolve_unit_addrs_overlap (state, error_callback, data, &addrs_vec))
  3951. return NULL;
  3952. /* No qsort for units required, already sorted. */
  3953. fdata = ((struct dwarf_data *)
  3954. backtrace_alloc (state, sizeof (struct dwarf_data),
  3955. error_callback, data));
  3956. if (fdata == NULL)
  3957. return NULL;
  3958. fdata->next = NULL;
  3959. fdata->altlink = altlink;
  3960. fdata->base_address = base_address;
  3961. fdata->addrs = (struct unit_addrs *) addrs_vec.vec.base;
  3962. fdata->addrs_count = addrs_vec.count;
  3963. fdata->units = (struct unit **) units_vec.vec.base;
  3964. fdata->units_count = units_vec.count;
  3965. fdata->dwarf_sections = *dwarf_sections;
  3966. fdata->is_bigendian = is_bigendian;
  3967. memset (&fdata->fvec, 0, sizeof fdata->fvec);
  3968. return fdata;
  3969. }
  3970. /* Build our data structures from the DWARF sections for a module.
  3971. Set FILELINE_FN and STATE->FILELINE_DATA. Return 1 on success, 0
  3972. on failure. */
  3973. int
  3974. backtrace_dwarf_add (struct backtrace_state *state,
  3975. struct libbacktrace_base_address base_address,
  3976. const struct dwarf_sections *dwarf_sections,
  3977. int is_bigendian,
  3978. struct dwarf_data *fileline_altlink,
  3979. backtrace_error_callback error_callback,
  3980. void *data, fileline *fileline_fn,
  3981. struct dwarf_data **fileline_entry)
  3982. {
  3983. struct dwarf_data *fdata;
  3984. fdata = build_dwarf_data (state, base_address, dwarf_sections, is_bigendian,
  3985. fileline_altlink, error_callback, data);
  3986. if (fdata == NULL)
  3987. return 0;
  3988. if (fileline_entry != NULL)
  3989. *fileline_entry = fdata;
  3990. if (!state->threaded)
  3991. {
  3992. struct dwarf_data **pp;
  3993. for (pp = (struct dwarf_data **) (void *) &state->fileline_data;
  3994. *pp != NULL;
  3995. pp = &(*pp)->next)
  3996. ;
  3997. *pp = fdata;
  3998. }
  3999. else
  4000. {
  4001. while (1)
  4002. {
  4003. struct dwarf_data **pp;
  4004. pp = (struct dwarf_data **) (void *) &state->fileline_data;
  4005. while (1)
  4006. {
  4007. struct dwarf_data *p;
  4008. p = backtrace_atomic_load_pointer (pp);
  4009. if (p == NULL)
  4010. break;
  4011. pp = &p->next;
  4012. }
  4013. if (__sync_bool_compare_and_swap (pp, NULL, fdata))
  4014. break;
  4015. }
  4016. }
  4017. *fileline_fn = dwarf_fileline;
  4018. return 1;
  4019. }