dwarf.cpp 113 KB

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