dwarf.c 114 KB

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