dbgdwarf.pas 146 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661
  1. {
  2. Copyright (c) 2003-2006 by Peter Vreman and Florian Klaempfl
  3. This units contains support for DWARF debug info generation
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. {
  18. This units contains support for DWARF debug info generation.
  19. Currently a lot of code looks like being mergable with dbgstabs. This might
  20. change however when improved dwarf info is generated, so the stuff shouldn't be
  21. merged yet. (FK)
  22. The easiest way to debug dwarf debug info generation is the usage of
  23. readelf --debug-dump <executable>
  24. This works only with elf targets though.
  25. There is a similar utility called dwarfdump which is not elf-specific and
  26. which has been ported to most systems.
  27. }
  28. unit dbgdwarf;
  29. {$i fpcdefs.inc}
  30. interface
  31. uses
  32. cclasses,globtype,
  33. aasmbase,aasmtai,aasmdata,
  34. symbase,symtype,symdef,symsym,
  35. finput,
  36. DbgBase;
  37. type
  38. { Tag names and codes. }
  39. tdwarf_tag = (DW_TAG_padding := $00,DW_TAG_array_type := $01,
  40. DW_TAG_class_type := $02,DW_TAG_entry_point := $03,
  41. DW_TAG_enumeration_type := $04,DW_TAG_formal_parameter := $05,
  42. DW_TAG_imported_declaration := $08,DW_TAG_label := $0a,
  43. DW_TAG_lexical_block := $0b,DW_TAG_member := $0d,
  44. DW_TAG_pointer_type := $0f,DW_TAG_reference_type := $10,
  45. DW_TAG_compile_unit := $11,DW_TAG_stringtypee := $12,
  46. DW_TAG_structure_type := $13,DW_TAG_subroutine_type := $15,
  47. DW_TAG_typedef := $16,DW_TAG_union_type := $17,
  48. DW_TAG_unspecified_parameters := $18,
  49. DW_TAG_variant := $19,DW_TAG_common_block := $1a,
  50. DW_TAG_common_inclusion := $1b,DW_TAG_inheritance := $1c,
  51. DW_TAG_inlined_subroutine := $1d,DW_TAG_module := $1e,
  52. DW_TAG_ptr_to_member_type := $1f,DW_TAG_set_type := $20,
  53. DW_TAG_subrange_type := $21,DW_TAG_with_stmt := $22,
  54. DW_TAG_access_declaration := $23,DW_TAG_base_type := $24,
  55. DW_TAG_catch_block := $25,DW_TAG_const_type := $26,
  56. DW_TAG_constant := $27,DW_TAG_enumerator := $28,
  57. DW_TAG_file_type := $29,DW_TAG_friend := $2a,
  58. DW_TAG_namelist := $2b,DW_TAG_namelist_item := $2c,
  59. DW_TAG_packed_type := $2d,DW_TAG_subprogram := $2e,
  60. DW_TAG_template_type_param := $2f,DW_TAG_template_value_param := $30,
  61. DW_TAG_thrown_type := $31,DW_TAG_try_block := $32,
  62. DW_TAG_variant_part := $33,DW_TAG_variable := $34,
  63. DW_TAG_volatile_type := $35,
  64. { DWARF 3. }
  65. DW_TAG_dwarf_procedure := $36,
  66. DW_TAG_restrict_type := $37,DW_TAG_interface_type := $38,
  67. DW_TAG_namespace := $39,DW_TAG_imported_module := $3a,
  68. DW_TAG_unspecified_type := $3b,DW_TAG_partial_unit := $3c,
  69. DW_TAG_imported_unit := $3d,
  70. { SGI/MIPS Extensions. }
  71. DW_TAG_MIPS_loop := $4081,
  72. { HP extensions. See: ftp://ftp.hp.com/pub/lang/tools/WDB/wdb-4.0.tar.gz . }
  73. DW_TAG_HP_array_descriptor := $4090,
  74. { GNU extensions. }
  75. { For FORTRAN 77 and Fortran 90. }
  76. DW_TAG_format_label := $4101,
  77. { For C++. }
  78. DW_TAG_function_template := $4102,DW_TAG_class_template := $4103,
  79. DW_TAG_GNU_BINCL := $4104,DW_TAG_GNU_EINCL := $4105,
  80. { Extensions for UPC. See: http://upc.gwu.edu/~upc. }
  81. DW_TAG_upc_shared_type := $8765,DW_TAG_upc_strict_type := $8766,
  82. DW_TAG_upc_relaxed_type := $8767,
  83. { PGI (STMicroelectronics) extensions. No documentation available. }
  84. DW_TAG_PGI_kanji_type := $A000,
  85. DW_TAG_PGI_interface_block := $A020);
  86. {$notes off}
  87. { Attribute names and codes. }
  88. tdwarf_attribute = (DW_AT_sibling := $01,DW_AT_location := $02,
  89. DW_AT_name := $03,DW_AT_ordering := $09,
  90. DW_AT_subscr_data := $0a,DW_AT_byte_size := $0b,
  91. DW_AT_bit_offset := $0c,DW_AT_bit_size := $0d,
  92. DW_AT_element_list := $0f,DW_AT_stmt_list := $10,
  93. DW_AT_low_pc := $11,DW_AT_high_pc := $12,
  94. DW_AT_language := $13,DW_AT_member := $14,
  95. DW_AT_discr := $15,DW_AT_discr_value := $16,
  96. DW_AT_visibility := $17,DW_AT_import := $18,
  97. DW_AT_string_length := $19,DW_AT_common_reference := $1a,
  98. DW_AT_comp_dir := $1b,DW_AT_const_value := $1c,
  99. DW_AT_containing_type := $1d,DW_AT_default_value := $1e,
  100. DW_AT_inline := $20,DW_AT_is_optional := $21,
  101. DW_AT_lower_bound := $22,DW_AT_producer := $25,
  102. DW_AT_prototyped := $27,DW_AT_return_addr := $2a,
  103. DW_AT_start_scope := $2c,DW_AT_stride_size := $2e,
  104. DW_AT_upper_bound := $2f,DW_AT_abstract_origin := $31,
  105. DW_AT_accessibility := $32,DW_AT_address_class := $33,
  106. DW_AT_artificial := $34,DW_AT_base_types := $35,
  107. DW_AT_calling_convention := $36,DW_AT_count := $37,
  108. DW_AT_data_member_location := $38,DW_AT_decl_column := $39,
  109. DW_AT_decl_file := $3a,DW_AT_decl_line := $3b,
  110. DW_AT_declaration := $3c,DW_AT_discr_list := $3d,
  111. DW_AT_encoding := $3e,DW_AT_external := $3f,
  112. DW_AT_frame_base := $40,DW_AT_friend := $41,
  113. DW_AT_identifier_case := $42,DW_AT_macro_info := $43,
  114. DW_AT_namelist_items := $44,DW_AT_priority := $45,
  115. DW_AT_segment := $46,DW_AT_specification := $47,
  116. DW_AT_static_link := $48,DW_AT_type := $49,
  117. DW_AT_use_location := $4a,DW_AT_variable_parameter := $4b,
  118. DW_AT_virtuality := $4c,DW_AT_vtable_elem_location := $4d,
  119. { DWARF 3 values. }
  120. DW_AT_allocated := $4e,DW_AT_associated := $4f,
  121. DW_AT_data_location := $50,DW_AT_byte_stride := $51,
  122. DW_AT_entry_pc := $52,DW_AT_use_UTF8 := $53,
  123. DW_AT_extension := $54,DW_AT_ranges := $55,
  124. DW_AT_trampoline := $56,DW_AT_call_column := $57,
  125. DW_AT_call_file := $58,DW_AT_call_line := $59,
  126. { SGI/MIPS extensions. }
  127. DW_AT_MIPS_fde := $2001,DW_AT_MIPS_loop_begin := $2002,
  128. DW_AT_MIPS_tail_loop_begin := $2003,DW_AT_MIPS_epilog_begin := $2004,
  129. DW_AT_MIPS_loop_unroll_factor := $2005,
  130. DW_AT_MIPS_software_pipeline_depth := $2006,
  131. DW_AT_MIPS_linkage_name := $2007,DW_AT_MIPS_stride := $2008,
  132. DW_AT_MIPS_abstract_name := $2009,DW_AT_MIPS_clone_origin := $200a,
  133. DW_AT_MIPS_has_inlines := $200b,
  134. { HP extensions. }
  135. DW_AT_HP_block_index := $2000,
  136. DW_AT_HP_unmodifiable := $2001,DW_AT_HP_actuals_stmt_list := $2010,
  137. DW_AT_HP_proc_per_section := $2011,DW_AT_HP_raw_data_ptr := $2012,
  138. DW_AT_HP_pass_by_reference := $2013,DW_AT_HP_opt_level := $2014,
  139. DW_AT_HP_prof_version_id := $2015,DW_AT_HP_opt_flags := $2016,
  140. DW_AT_HP_cold_region_low_pc := $2017,DW_AT_HP_cold_region_high_pc := $2018,
  141. DW_AT_HP_all_variables_modifiable := $2019,
  142. DW_AT_HP_linkage_name := $201a,DW_AT_HP_prof_flags := $201b,
  143. { GNU extensions. }
  144. DW_AT_sf_names := $2101,DW_AT_src_info := $2102,
  145. DW_AT_mac_info := $2103,DW_AT_src_coords := $2104,
  146. DW_AT_body_begin := $2105,DW_AT_body_end := $2106,
  147. DW_AT_GNU_vector := $2107,
  148. { VMS extensions. }
  149. DW_AT_VMS_rtnbeg_pd_address := $2201,
  150. { UPC extension. }
  151. DW_AT_upc_threads_scaled := $3210,
  152. { PGI (STMicroelectronics) extensions. }
  153. DW_AT_PGI_lbase := $3a00,
  154. DW_AT_PGI_soffset := $3a01,DW_AT_PGI_lstride := $3a02
  155. );
  156. {$notes on}
  157. { Form names and codes. }
  158. Tdwarf_form = (DW_FORM_addr := $01,DW_FORM_block2 := $03,
  159. DW_FORM_block4 := $04,DW_FORM_data2 := $05,
  160. DW_FORM_data4 := $06,DW_FORM_data8 := $07,
  161. DW_FORM_string := $08,DW_FORM_block := $09,
  162. DW_FORM_block1 := $0a,DW_FORM_data1 := $0b,
  163. DW_FORM_flag := $0c,DW_FORM_sdata := $0d,
  164. DW_FORM_strp := $0e,DW_FORM_udata := $0f,
  165. DW_FORM_ref_addr := $10,DW_FORM_ref1 := $11,
  166. DW_FORM_ref2 := $12,DW_FORM_ref4 := $13,
  167. DW_FORM_ref8 := $14,DW_FORM_ref_udata := $15,
  168. DW_FORM_indirect := $16);
  169. TDwarfFile = record
  170. Index: integer;
  171. Name: PChar;
  172. end;
  173. { TDebugInfoDwarf }
  174. TDebugInfoDwarf = class(TDebugInfo)
  175. private
  176. currabbrevnumber : longint;
  177. { collect all defs in one list so we can reset them easily }
  178. defnumberlist,
  179. deftowritelist : TFPObjectList;
  180. { use this defs to create info for variants and file handles }
  181. { unused (MWE)
  182. filerecdef,
  183. textrecdef : tdef;
  184. }
  185. dirlist: TFPHashObjectList;
  186. filesequence: Integer;
  187. loclist: tdynamicarray;
  188. asmline: TAsmList;
  189. function def_dwarf_lab(def:tdef) : tasmsymbol;
  190. function def_dwarf_ref_lab(def:tdef) : tasmsymbol;
  191. function def_dwarf_class_struct_lab(def:tobjectdef) : tasmsymbol;
  192. function get_file_index(afile: tinputfile): Integer;
  193. function relative_dwarf_path(const s:tcmdstr):tcmdstr;
  194. protected
  195. // set if we should use 64bit headers (dwarf3 and up)
  196. _use_64bit_headers: Boolean;
  197. // set to ait_const32bit if use_64bit_headers is false, otherwise
  198. // to ait_const64bit
  199. offsetreltype,
  200. offsetabstype : taiconst_type;
  201. // set if we generated any lineinfo at all. If not, we have to terminate
  202. // when insertmoduleinfo is called.
  203. generated_lineinfo: boolean;
  204. vardatadef: trecorddef;
  205. procedure set_use_64bit_headers(state: boolean);
  206. property use_64bit_headers: Boolean read _use_64bit_headers write set_use_64bit_headers;
  207. procedure set_def_dwarf_labs(def:tdef);
  208. { Convenience version of the method below, so the compiler creates the
  209. tvarrec for us (must only pass one element in the last parameter). }
  210. procedure append_attribute(attr: tdwarf_attribute; form: tdwarf_form; const values: array of const);
  211. procedure append_attribute(attr: tdwarf_attribute; form: tdwarf_form; const value: tvarrec);
  212. procedure append_entry(tag : tdwarf_tag;has_children : boolean;data : array of const);
  213. procedure append_block1(attr: tdwarf_attribute; size: aint);
  214. procedure append_labelentry(attr : tdwarf_attribute;sym : tasmsymbol);
  215. procedure append_labelentry_addr_ref(attr : tdwarf_attribute;sym : tasmsymbol); virtual;
  216. procedure append_labelentry_ref(attr : tdwarf_attribute;sym : tasmsymbol);
  217. procedure append_labelentry_dataptr_abs(attr : tdwarf_attribute;sym : tasmsymbol);
  218. procedure append_labelentry_dataptr_rel(attr : tdwarf_attribute;sym,endsym : tasmsymbol);
  219. procedure append_labelentry_dataptr_common(attr : tdwarf_attribute);
  220. procedure beforeappenddef(list:TAsmList;def:tdef);override;
  221. procedure afterappenddef(list:TAsmList;def:tdef);override;
  222. procedure appenddef_ord(list:TAsmList;def:torddef);override;
  223. procedure appenddef_float(list:TAsmList;def:tfloatdef);override;
  224. procedure appenddef_enum(list:TAsmList;def:tenumdef);override;
  225. procedure appenddef_array(list:TAsmList;def:tarraydef);override;
  226. procedure appenddef_record_named(list:TAsmList;def:trecorddef;const name: shortstring);
  227. procedure appenddef_record(list:TAsmList;def:trecorddef);override;
  228. procedure appenddef_pointer(list:TAsmList;def:tpointerdef);override;
  229. procedure appenddef_string(list:TAsmList;def:tstringdef);override;
  230. procedure appenddef_procvar(list:TAsmList;def:tprocvardef);override;
  231. procedure appendprocdef(list:TAsmList;def:tprocdef);override;
  232. function get_symlist_sym_offset(symlist: ppropaccesslistitem; out sym: tabstractvarsym; out offset: pint): boolean;
  233. procedure appendsym_var(list:TAsmList;sym:tabstractnormalvarsym);
  234. { used for global/static variables, local variables, parameters and
  235. absolute variables
  236. }
  237. procedure appendsym_var_with_name_type_offset(list:TAsmList; sym:tabstractnormalvarsym; const name: string; def: tdef; offset: pint; do_self: boolean);
  238. { used for fields and properties mapped to fields }
  239. procedure appendsym_fieldvar_with_name_offset(list:TAsmList;sym: tfieldvarsym;const name: string; def: tdef; offset: pint);
  240. procedure beforeappendsym(list:TAsmList;sym:tsym);override;
  241. procedure appendsym_staticvar(list:TAsmList;sym:tstaticvarsym);override;
  242. procedure appendsym_paravar(list:TAsmList;sym:tparavarsym);override;
  243. procedure appendsym_localvar(list:TAsmList;sym:tlocalvarsym);override;
  244. procedure appendsym_fieldvar(list:TAsmList;sym:tfieldvarsym);override;
  245. procedure appendsym_const(list:TAsmList;sym:tconstsym);override;
  246. procedure appendsym_type(list:TAsmList;sym:ttypesym);override;
  247. procedure appendsym_label(list:TAsmList;sym:tlabelsym);override;
  248. procedure appendsym_absolute(list:TAsmList;sym:tabsolutevarsym);override;
  249. procedure appendsym_property(list:TAsmList;sym:tpropertysym);override;
  250. function symname(sym:tsym): String; virtual;
  251. procedure enum_membersyms_callback(p:TObject;arg:pointer);
  252. procedure finish_children;
  253. procedure finish_entry;
  254. procedure finish_lineinfo;
  255. public
  256. constructor Create;override;
  257. destructor Destroy;override;
  258. procedure insertmoduleinfo;override;
  259. procedure inserttypeinfo;override;
  260. procedure referencesections(list:TAsmList);override;
  261. procedure insertlineinfo(list:TAsmList);override;
  262. function dwarf_version: Word; virtual; abstract;
  263. end;
  264. { TDebugInfoDwarf2 }
  265. TDebugInfoDwarf2 = class(TDebugInfoDwarf)
  266. private
  267. protected
  268. procedure appenddef_set_intern(list:TAsmList;def:tsetdef; force_tag_set: boolean);
  269. procedure appenddef_file(list:TAsmList;def:tfiledef); override;
  270. procedure appenddef_formal(list:TAsmList;def:tformaldef); override;
  271. procedure appenddef_object(list:TAsmList;def:tobjectdef); override;
  272. procedure appenddef_set(list:TAsmList;def:tsetdef); override;
  273. procedure appenddef_undefined(list:TAsmList;def:tundefineddef); override;
  274. procedure appenddef_variant(list:TAsmList;def:tvariantdef); override;
  275. public
  276. function dwarf_version: Word; override;
  277. end;
  278. { TDebugInfoDwarf3 }
  279. TDebugInfoDwarf3 = class(TDebugInfoDwarf2)
  280. private
  281. protected
  282. procedure append_labelentry_addr_ref(attr : tdwarf_attribute;sym : tasmsymbol); override;
  283. procedure appenddef_array(list:TAsmList;def:tarraydef); override;
  284. procedure appenddef_string(list:TAsmList;def:tstringdef);override;
  285. procedure appenddef_file(list:TAsmList;def:tfiledef); override;
  286. procedure appenddef_formal(list:TAsmList;def:tformaldef); override;
  287. procedure appenddef_object(list:TAsmList;def:tobjectdef); override;
  288. procedure appenddef_set(list:TAsmList;def: tsetdef); override;
  289. procedure appenddef_undefined(list:TAsmList;def:tundefineddef); override;
  290. procedure appenddef_variant(list:TAsmList;def:tvariantdef); override;
  291. function symname(sym:tsym): String; override;
  292. public
  293. function dwarf_version: Word; override;
  294. end;
  295. implementation
  296. uses
  297. sysutils,cutils,cfileutl,constexp,
  298. version,globals,verbose,systems,
  299. cpubase,cgbase,paramgr,
  300. fmodule,
  301. defutil,symconst,symtable,ppu
  302. ;
  303. const
  304. LINE_BASE = 1;
  305. OPCODE_BASE = 13;
  306. const
  307. DW_TAG_lo_user = $4080;
  308. DW_TAG_hi_user = $ffff;
  309. { Flag that tells whether entry has a child or not. }
  310. DW_children_no = 0;
  311. DW_children_yes = 1;
  312. const
  313. { Implementation-defined range start. }
  314. DW_AT_lo_user = $2000;
  315. { Implementation-defined range end. }
  316. DW_AT_hi_user = $3ff0;
  317. type
  318. { Source language names and codes. }
  319. tdwarf_source_language = (DW_LANG_C89 := $0001,DW_LANG_C := $0002,DW_LANG_Ada83 := $0003,
  320. DW_LANG_C_plus_plus := $0004,DW_LANG_Cobol74 := $0005,
  321. DW_LANG_Cobol85 := $0006,DW_LANG_Fortran77 := $0007,
  322. DW_LANG_Fortran90 := $0008,DW_LANG_Pascal83 := $0009,
  323. DW_LANG_Modula2 := $000a,DW_LANG_Java := $000b,
  324. { DWARF 3. }
  325. DW_LANG_C99 := $000c,DW_LANG_Ada95 := $000d,
  326. DW_LANG_Fortran95 := $000e,
  327. { MIPS. }
  328. DW_LANG_Mips_Assembler := $8001,
  329. { UPC. }
  330. DW_LANG_Upc := $8765
  331. );
  332. const
  333. { Implementation-defined range start. }
  334. DW_LANG_lo_user = $8000;
  335. { Implementation-defined range start. }
  336. DW_LANG_hi_user = $ffff;
  337. type
  338. { Names and codes for macro information. }
  339. tdwarf_macinfo_record_type = (DW_MACINFO_define := 1,DW_MACINFO_undef := 2,
  340. DW_MACINFO_start_file := 3,DW_MACINFO_end_file := 4,
  341. DW_MACINFO_vendor_ext := 255);
  342. type
  343. { Type encodings. }
  344. Tdwarf_type = (DW_ATE_void := $0,DW_ATE_address := $1,
  345. DW_ATE_boolean := $2,DW_ATE_complex_float := $3,
  346. DW_ATE_float := $4,DW_ATE_signed := $5,
  347. DW_ATE_signed_char := $6,DW_ATE_unsigned := $7,
  348. DW_ATE_unsigned_char := $8,DW_ATE_imaginary_float := $9,
  349. { HP extensions. }
  350. DW_ATE_HP_float80 := $80,DW_ATE_HP_complex_float80 := $81,
  351. DW_ATE_HP_float128 := $82,DW_ATE_HP_complex_float128 := $83,
  352. DW_ATE_HP_floathpintel := $84,DW_ATE_HP_imaginary_float80 := $85,
  353. DW_ATE_HP_imaginary_float128 := $86
  354. );
  355. const
  356. DW_ATE_lo_user = $80;
  357. DW_ATE_hi_user = $ff;
  358. type
  359. Tdwarf_array_dim_ordering = (DW_ORD_row_major := 0,DW_ORD_col_major := 1
  360. );
  361. { Access attribute. }
  362. Tdwarf_access_attribute = (DW_ACCESS_public := 1,DW_ACCESS_protected := 2,
  363. DW_ACCESS_private := 3);
  364. { Visibility. }
  365. Tdwarf_visibility_attribute = (DW_VIS_local := 1,DW_VIS_exported := 2,
  366. DW_VIS_qualified := 3);
  367. { Virtuality. }
  368. Tdwarf_virtuality_attribute = (DW_VIRTUALITY_none := 0,DW_VIRTUALITY_virtual := 1,
  369. DW_VIRTUALITY_pure_virtual := 2);
  370. { Case sensitivity. }
  371. Tdwarf_id_case = (DW_ID_case_sensitive := 0,DW_ID_up_case := 1,
  372. DW_ID_down_case := 2,DW_ID_case_insensitive := 3
  373. );
  374. { Calling convention. }
  375. Tdwarf_calling_convention = (DW_CC_normal := $1,DW_CC_program := $2,
  376. DW_CC_nocall := $3,DW_CC_GNU_renesas_sh := $40, DW_CC_GNU_borland_fastcall_i386 := $41
  377. );
  378. {$notes off}
  379. { Location atom names and codes. }
  380. Tdwarf_location_atom = (DW_OP_addr := $03,DW_OP_deref := $06,DW_OP_const1u := $08,
  381. DW_OP_const1s := $09,DW_OP_const2u := $0a,
  382. DW_OP_const2s := $0b,DW_OP_const4u := $0c,
  383. DW_OP_const4s := $0d,DW_OP_const8u := $0e,
  384. DW_OP_const8s := $0f,DW_OP_constu := $10,
  385. DW_OP_consts := $11,DW_OP_dup := $12,DW_OP_drop := $13,
  386. DW_OP_over := $14,DW_OP_pick := $15,DW_OP_swap := $16,
  387. DW_OP_rot := $17,DW_OP_xderef := $18,DW_OP_abs := $19,
  388. DW_OP_and := $1a,DW_OP_div := $1b,DW_OP_minus := $1c,
  389. DW_OP_mod := $1d,DW_OP_mul := $1e,DW_OP_neg := $1f,
  390. DW_OP_not := $20,DW_OP_or := $21,DW_OP_plus := $22,
  391. DW_OP_plus_uconst := $23,DW_OP_shl := $24,
  392. DW_OP_shr := $25,DW_OP_shra := $26,DW_OP_xor := $27,
  393. DW_OP_bra := $28,DW_OP_eq := $29,DW_OP_ge := $2a,
  394. DW_OP_gt := $2b,DW_OP_le := $2c,DW_OP_lt := $2d,
  395. DW_OP_ne := $2e,DW_OP_skip := $2f,DW_OP_lit0 := $30,
  396. DW_OP_lit1 := $31,DW_OP_lit2 := $32,DW_OP_lit3 := $33,
  397. DW_OP_lit4 := $34,DW_OP_lit5 := $35,DW_OP_lit6 := $36,
  398. DW_OP_lit7 := $37,DW_OP_lit8 := $38,DW_OP_lit9 := $39,
  399. DW_OP_lit10 := $3a,DW_OP_lit11 := $3b,
  400. DW_OP_lit12 := $3c,DW_OP_lit13 := $3d,
  401. DW_OP_lit14 := $3e,DW_OP_lit15 := $3f,
  402. DW_OP_lit16 := $40,DW_OP_lit17 := $41,
  403. DW_OP_lit18 := $42,DW_OP_lit19 := $43,
  404. DW_OP_lit20 := $44,DW_OP_lit21 := $45,
  405. DW_OP_lit22 := $46,DW_OP_lit23 := $47,
  406. DW_OP_lit24 := $48,DW_OP_lit25 := $49,
  407. DW_OP_lit26 := $4a,DW_OP_lit27 := $4b,
  408. DW_OP_lit28 := $4c,DW_OP_lit29 := $4d,
  409. DW_OP_lit30 := $4e,DW_OP_lit31 := $4f,
  410. DW_OP_reg0 := $50,DW_OP_reg1 := $51,DW_OP_reg2 := $52,
  411. DW_OP_reg3 := $53,DW_OP_reg4 := $54,DW_OP_reg5 := $55,
  412. DW_OP_reg6 := $56,DW_OP_reg7 := $57,DW_OP_reg8 := $58,
  413. DW_OP_reg9 := $59,DW_OP_reg10 := $5a,DW_OP_reg11 := $5b,
  414. DW_OP_reg12 := $5c,DW_OP_reg13 := $5d,
  415. DW_OP_reg14 := $5e,DW_OP_reg15 := $5f,
  416. DW_OP_reg16 := $60,DW_OP_reg17 := $61,
  417. DW_OP_reg18 := $62,DW_OP_reg19 := $63,
  418. DW_OP_reg20 := $64,DW_OP_reg21 := $65,
  419. DW_OP_reg22 := $66,DW_OP_reg23 := $67,
  420. DW_OP_reg24 := $68,DW_OP_reg25 := $69,
  421. DW_OP_reg26 := $6a,DW_OP_reg27 := $6b,
  422. DW_OP_reg28 := $6c,DW_OP_reg29 := $6d,
  423. DW_OP_reg30 := $6e,DW_OP_reg31 := $6f,
  424. DW_OP_breg0 := $70,DW_OP_breg1 := $71,
  425. DW_OP_breg2 := $72,DW_OP_breg3 := $73,
  426. DW_OP_breg4 := $74,DW_OP_breg5 := $75,
  427. DW_OP_breg6 := $76,DW_OP_breg7 := $77,
  428. DW_OP_breg8 := $78,DW_OP_breg9 := $79,
  429. DW_OP_breg10 := $7a,DW_OP_breg11 := $7b,
  430. DW_OP_breg12 := $7c,DW_OP_breg13 := $7d,
  431. DW_OP_breg14 := $7e,DW_OP_breg15 := $7f,
  432. DW_OP_breg16 := $80,DW_OP_breg17 := $81,
  433. DW_OP_breg18 := $82,DW_OP_breg19 := $83,
  434. DW_OP_breg20 := $84,DW_OP_breg21 := $85,
  435. DW_OP_breg22 := $86,DW_OP_breg23 := $87,
  436. DW_OP_breg24 := $88,DW_OP_breg25 := $89,
  437. DW_OP_breg26 := $8a,DW_OP_breg27 := $8b,
  438. DW_OP_breg28 := $8c,DW_OP_breg29 := $8d,
  439. DW_OP_breg30 := $8e,DW_OP_breg31 := $8f,
  440. DW_OP_regx := $90,DW_OP_fbreg := $91,DW_OP_bregx := $92,
  441. DW_OP_piece := $93,DW_OP_deref_size := $94,
  442. DW_OP_xderef_size := $95,DW_OP_nop := $96,
  443. { DWARF 3 extensions. }
  444. DW_OP_push_object_address := $97,DW_OP_call2 := $98,
  445. DW_OP_call4 := $99,DW_OP_call_ref := $9a,
  446. { GNU extensions. }
  447. DW_OP_GNU_push_tls_address := $e0,
  448. { HP extensions. }
  449. DW_OP_HP_unknown := $e0,
  450. DW_OP_HP_is_value := $e1,DW_OP_HP_fltconst4 := $e2,
  451. DW_OP_HP_fltconst8 := $e3,DW_OP_HP_mod_range := $e4,
  452. DW_OP_HP_unmod_range := $e5,DW_OP_HP_tls := $e6
  453. );
  454. {$notes on}
  455. const
  456. { Implementation-defined range start. }
  457. DW_OP_lo_user = $e0;
  458. { Implementation-defined range end. }
  459. DW_OP_hi_user = $ff;
  460. const
  461. DW_LNS_extended_op = $00;
  462. { next copied from cfidwarf, need to go to something shared }
  463. DW_LNS_copy = $01;
  464. DW_LNS_advance_pc = $02;
  465. DW_LNS_advance_line = $03;
  466. DW_LNS_set_file = $04;
  467. DW_LNS_set_column = $05;
  468. DW_LNS_negate_stmt = $06;
  469. DW_LNS_set_basic_block = $07;
  470. DW_LNS_const_add_pc = $08;
  471. DW_LNS_fixed_advance_pc = $09;
  472. DW_LNS_set_prologue_end = $0a;
  473. DW_LNS_set_epilogue_begin = $0b;
  474. DW_LNS_set_isa = $0c;
  475. DW_LNE_end_sequence = $01;
  476. DW_LNE_set_address = $02;
  477. DW_LNE_define_file = $03;
  478. DW_LNE_lo_user = $80;
  479. DW_LNE_hi_user = $ff;
  480. type
  481. { TDirIndexItem }
  482. TDirIndexItem = class(TFPHashObject)
  483. private
  484. FFiles: TFPHashObjectList;
  485. public
  486. IndexNr : Integer;
  487. constructor Create(AList:TFPHashObjectList;const AName: String; AIndex: Integer);
  488. destructor Destroy;override;
  489. property Files: TFPHashObjectList read FFiles;
  490. end;
  491. { TFileIndexItem }
  492. TFileIndexItem = class(TFPHashObject)
  493. private
  494. FDirIndex: Integer;
  495. public
  496. IndexNr : Integer;
  497. constructor Create(AList:TFPHashObjectList;const AName: String; ADirIndex, AIndex: Integer);
  498. property DirIndex: Integer read FDirIndex;
  499. end;
  500. {****************************************************************************
  501. procs
  502. ****************************************************************************}
  503. function DirListSortCompare(AItem1, AItem2: Pointer): Integer;
  504. begin
  505. Result := TDirIndexItem(AItem1).IndexNr - TDirIndexItem(AItem2).IndexNr;
  506. end;
  507. function FileListSortCompare(AItem1, AItem2: Pointer): Integer;
  508. begin
  509. Result := TFileIndexItem(AItem1).IndexNr - TFileIndexItem(AItem2).IndexNr;
  510. end;
  511. {****************************************************************************
  512. TDirIndexItem
  513. ****************************************************************************}
  514. constructor TDirIndexItem.Create(AList:TFPHashObjectList;const AName: String; AIndex: Integer);
  515. begin
  516. inherited Create(AList,AName);
  517. FFiles := TFPHashObjectList.Create;
  518. IndexNr := AIndex;
  519. end;
  520. destructor TDirIndexItem.Destroy;
  521. begin
  522. FFiles.Free;
  523. inherited Destroy;
  524. end;
  525. {****************************************************************************
  526. TFileIndexItem
  527. ****************************************************************************}
  528. constructor TFileIndexItem.Create(AList:TFPHashObjectList;const AName: String; ADirIndex, AIndex: Integer);
  529. begin
  530. inherited Create(AList,Aname);
  531. FDirIndex := ADirIndex;
  532. IndexNr := AIndex;
  533. end;
  534. {****************************************************************************
  535. TDebugInfoDwarf
  536. ****************************************************************************}
  537. function TDebugInfoDwarf.relative_dwarf_path(const s:tcmdstr):tcmdstr;
  538. begin
  539. { Make a clean path for gdb. Remove trailing / and ./ prefixes and
  540. use always a / }
  541. result:=BsToSlash(ExcludeTrailingPathDelimiter(ExtractRelativePath(GetCurrentDir,FixFileName(ExpandFileName(s)))));
  542. end;
  543. procedure TDebugInfoDwarf.set_use_64bit_headers(state: boolean);
  544. begin
  545. _use_64bit_headers:=state;
  546. if not(state) then
  547. begin
  548. if (target_info.system in system_windows+system_wince) then
  549. offsetabstype:=aitconst_secrel32_symbol
  550. else
  551. offsetabstype:=aitconst_32bit;
  552. if (target_info.system in systems_darwin) then
  553. offsetreltype:=aitconst_darwin_dwarf_delta32
  554. else
  555. offsetreltype:=aitconst_32bit;
  556. end
  557. else
  558. begin
  559. if (target_info.system in systems_darwin) then
  560. offsetreltype:=aitconst_darwin_dwarf_delta64
  561. else
  562. offsetreltype:=aitconst_64bit;
  563. offsetabstype:=aitconst_64bit;
  564. end;
  565. end;
  566. procedure TDebugInfoDwarf.set_def_dwarf_labs(def:tdef);
  567. begin
  568. { Keep track of used dwarf entries, this info is only useful for dwarf entries
  569. referenced by the symbols. Definitions will always include all
  570. required stabs }
  571. if def.dbg_state=dbg_state_unused then
  572. def.dbg_state:=dbg_state_used;
  573. { Need a new label? }
  574. if not assigned(def.dwarf_lab) then
  575. begin
  576. if not(tf_dwarf_only_local_labels in target_info.flags) then
  577. begin
  578. if (ds_dwarf_dbg_info_written in def.defstates) then
  579. begin
  580. if not assigned(def.typesym) then
  581. internalerror(200610011);
  582. def.dwarf_lab:=current_asmdata.RefAsmSymbol(make_mangledname('DBG',def.owner,symname(def.typesym)));
  583. def.dwarf_ref_lab:=current_asmdata.RefAsmSymbol(make_mangledname('DBGREF',def.owner,symname(def.typesym)));
  584. if is_class_or_interface_or_dispinterface(def) then
  585. tobjectdef(def).dwarf_struct_lab:=current_asmdata.RefAsmSymbol(make_mangledname('DBG2',def.owner,symname(def.typesym)));
  586. def.dbg_state:=dbg_state_written;
  587. end
  588. else
  589. begin
  590. { Create an exported DBG symbol if we are generating a def defined in the
  591. globalsymtable of the current unit }
  592. if assigned(def.typesym) and
  593. (def.owner.symtabletype=globalsymtable) and
  594. (def.owner.iscurrentunit) then
  595. begin
  596. def.dwarf_lab:=current_asmdata.DefineAsmSymbol(make_mangledname('DBG',def.owner,symname(def.typesym)),AB_GLOBAL,AT_DATA);
  597. def.dwarf_ref_lab:=current_asmdata.DefineAsmSymbol(make_mangledname('DBGREF',def.owner,symname(def.typesym)),AB_GLOBAL,AT_DATA);
  598. if is_class_or_interface_or_dispinterface(def) then
  599. tobjectdef(def).dwarf_struct_lab:=current_asmdata.DefineAsmSymbol(make_mangledname('DBG2',def.owner,symname(def.typesym)),AB_GLOBAL,AT_DATA);
  600. include(def.defstates,ds_dwarf_dbg_info_written);
  601. end
  602. else
  603. begin
  604. { The pointer typecast is needed to prevent a problem with range checking
  605. on when the typecast is changed to 'as' }
  606. current_asmdata.getdatalabel(TAsmLabel(pointer(def.dwarf_lab)));
  607. current_asmdata.getdatalabel(TAsmLabel(pointer(def.dwarf_ref_lab)));
  608. if is_class_or_interface_or_dispinterface(def) then
  609. current_asmdata.getdatalabel(TAsmLabel(pointer(tobjectdef(def).dwarf_struct_lab)));
  610. end;
  611. end;
  612. end
  613. else
  614. begin
  615. { The pointer typecast is needed to prevent a problem with range checking
  616. on when the typecast is changed to 'as' }
  617. { addrlabel instead of datalabel because it must be a local one }
  618. current_asmdata.getaddrlabel(TAsmLabel(pointer(def.dwarf_lab)));
  619. current_asmdata.getaddrlabel(TAsmLabel(pointer(def.dwarf_ref_lab)));
  620. if is_class_or_interface_or_dispinterface(def) then
  621. current_asmdata.getaddrlabel(TAsmLabel(pointer(tobjectdef(def).dwarf_struct_lab)));
  622. end;
  623. if def.dbg_state=dbg_state_used then
  624. deftowritelist.Add(def);
  625. defnumberlist.Add(def);
  626. end;
  627. end;
  628. function TDebugInfoDwarf.def_dwarf_lab(def: tdef): tasmsymbol;
  629. begin
  630. set_def_dwarf_labs(def);
  631. result:=def.dwarf_lab;
  632. end;
  633. function TDebugInfoDwarf.def_dwarf_class_struct_lab(def: tobjectdef): tasmsymbol;
  634. begin
  635. set_def_dwarf_labs(def);
  636. result:=def.dwarf_struct_lab;
  637. end;
  638. function TDebugInfoDwarf.def_dwarf_ref_lab(def: tdef): tasmsymbol;
  639. begin
  640. set_def_dwarf_labs(def);
  641. result:=def.dwarf_ref_lab;
  642. end;
  643. constructor TDebugInfoDwarf.Create;
  644. begin
  645. inherited Create;
  646. { 64bit headers are only supported for dwarf3 and up, so default off }
  647. use_64bit_headers := false;
  648. { we haven't generated any lineinfo yet }
  649. generated_lineinfo := false;
  650. dirlist := TFPHashObjectList.Create;
  651. { add current dir as first item (index=0) }
  652. TDirIndexItem.Create(dirlist,'.', 0);
  653. asmline := TAsmList.create;
  654. loclist := tdynamicarray.Create(4096);
  655. end;
  656. destructor TDebugInfoDwarf.Destroy;
  657. begin
  658. dirlist.Free;
  659. dirlist := nil;
  660. asmline.free;
  661. asmline:=nil;
  662. loclist.Free;
  663. loclist := nil;
  664. inherited Destroy;
  665. end;
  666. procedure TDebugInfoDwarf.enum_membersyms_callback(p:TObject; arg: pointer);
  667. begin
  668. case tsym(p).typ of
  669. fieldvarsym:
  670. appendsym_fieldvar(TAsmList(arg),tfieldvarsym(p));
  671. propertysym:
  672. appendsym_property(TAsmList(arg),tpropertysym(p));
  673. end;
  674. end;
  675. function TDebugInfoDwarf.get_file_index(afile: tinputfile): Integer;
  676. var
  677. dirname: String;
  678. diritem: TDirIndexItem;
  679. diridx: Integer;
  680. fileitem: TFileIndexItem;
  681. begin
  682. if afile.path^ = '' then
  683. dirname := '.'
  684. else
  685. begin
  686. { add the canonical form here already to avoid problems with }
  687. { paths such as './' etc }
  688. dirname := relative_dwarf_path(afile.path^);
  689. if dirname = '' then
  690. dirname := '.';
  691. end;
  692. diritem := TDirIndexItem(dirlist.Find(dirname));
  693. if diritem = nil then
  694. diritem := TDirIndexItem.Create(dirlist,dirname, dirlist.Count);
  695. diridx := diritem.IndexNr;
  696. fileitem := TFileIndexItem(diritem.files.Find(afile.name^));
  697. if fileitem = nil then
  698. begin
  699. Inc(filesequence);
  700. fileitem := TFileIndexItem.Create(diritem.files,afile.name^, diridx, filesequence);
  701. end;
  702. Result := fileitem.IndexNr;
  703. end;
  704. procedure TDebugInfoDwarf.append_attribute(attr: tdwarf_attribute; form: tdwarf_form; const values: array of const);
  705. begin
  706. if length(values)<>1 then
  707. internalerror(2009040402);
  708. append_attribute(attr,form,values[0]);
  709. end;
  710. procedure TDebugInfoDwarf.append_attribute(attr: tdwarf_attribute; form: tdwarf_form; const value: tvarrec);
  711. begin
  712. { attribute }
  713. current_asmdata.asmlists[al_dwarf_abbrev].concat(tai_const.create_uleb128bit(cardinal(attr)));
  714. { form }
  715. current_asmdata.asmlists[al_dwarf_abbrev].concat(tai_const.create_uleb128bit(cardinal(form)));
  716. { info itself }
  717. case form of
  718. DW_FORM_string:
  719. case value.VType of
  720. vtChar:
  721. current_asmdata.asmlists[al_dwarf_info].concat(tai_string.create(value.VChar));
  722. vtString:
  723. current_asmdata.asmlists[al_dwarf_info].concat(tai_string.create(value.VString^));
  724. vtAnsistring:
  725. current_asmdata.asmlists[al_dwarf_info].concat(tai_string.create(Ansistring(value.VAnsiString)));
  726. else
  727. internalerror(200601264);
  728. end;
  729. DW_FORM_flag:
  730. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(byte(value.VBoolean)));
  731. DW_FORM_data1:
  732. case value.VType of
  733. vtInteger:
  734. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(value.VInteger));
  735. vtInt64:
  736. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(value.VInt64^));
  737. vtQWord:
  738. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(value.VQWord^));
  739. else
  740. internalerror(200602143);
  741. end;
  742. DW_FORM_data2:
  743. case value.VType of
  744. vtInteger:
  745. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_16bit(value.VInteger));
  746. vtInt64:
  747. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_16bit(value.VInt64^));
  748. vtQWord:
  749. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_16bit(value.VQWord^));
  750. else
  751. internalerror(200602144);
  752. end;
  753. DW_FORM_data4:
  754. case value.VType of
  755. vtInteger:
  756. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_32bit(value.VInteger));
  757. vtInt64:
  758. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_32bit(value.VInt64^));
  759. vtQWord:
  760. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_32bit(value.VQWord^));
  761. else
  762. internalerror(200602145);
  763. end;
  764. DW_FORM_data8:
  765. case value.VType of
  766. vtInteger:
  767. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_64bit(value.VInteger));
  768. vtInt64:
  769. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_64bit(value.VInt64^));
  770. vtQWord:
  771. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_64bit(value.VQWord^));
  772. else
  773. internalerror(200602146);
  774. end;
  775. DW_FORM_sdata:
  776. case value.VType of
  777. vtInteger:
  778. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_sleb128bit(value.VInteger));
  779. vtInt64:
  780. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_sleb128bit(value.VInt64^));
  781. vtQWord:
  782. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_sleb128bit(value.VQWord^));
  783. else
  784. internalerror(200601285);
  785. end;
  786. DW_FORM_udata:
  787. case value.VType of
  788. vtInteger:
  789. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_uleb128bit(value.VInteger));
  790. vtInt64:
  791. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_uleb128bit(value.VInt64^));
  792. vtQWord:
  793. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_uleb128bit(value.VQWord^));
  794. else
  795. internalerror(200601284);
  796. end;
  797. { block gets only the size, the rest is appended manually by the caller }
  798. DW_FORM_block1:
  799. case value.VType of
  800. vtInteger:
  801. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(value.VInteger));
  802. vtInt64:
  803. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(value.VInt64^));
  804. vtQWord:
  805. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(value.VQWord^));
  806. else
  807. internalerror(200602141);
  808. end;
  809. else
  810. internalerror(200601263);
  811. end;
  812. end;
  813. { writing the data through a few simply procedures allows to create easily extra information
  814. for debugging of debug info }
  815. procedure TDebugInfoDwarf.append_entry(tag : tdwarf_tag;has_children : boolean;data : array of const);
  816. var
  817. i : longint;
  818. begin
  819. { todo: store defined abbrevs, so you have to define tehm only once (for this unit) (MWE) }
  820. inc(currabbrevnumber);
  821. current_asmdata.asmlists[al_dwarf_abbrev].concat(tai_comment.Create(strpnew('Abbrev '+tostr(currabbrevnumber))));
  822. { abbrev number }
  823. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_uleb128bit(currabbrevnumber));
  824. current_asmdata.asmlists[al_dwarf_abbrev].concat(tai_const.create_uleb128bit(currabbrevnumber));
  825. { tag }
  826. current_asmdata.asmlists[al_dwarf_abbrev].concat(tai_const.create_uleb128bit(ord(tag)));
  827. { children? }
  828. current_asmdata.asmlists[al_dwarf_abbrev].concat(tai_const.create_8bit(ord(has_children)));
  829. i:=0;
  830. while i<=high(data) do
  831. begin
  832. if (i+2 > high(data)) then
  833. internalerror(2009040401);
  834. if data[i].VType<>vtInteger then
  835. internalerror(200601261);
  836. if data[i+1].VType<>vtInteger then
  837. internalerror(200601261);
  838. append_attribute(tdwarf_attribute(data[i].VInteger),tdwarf_form(data[i+1].VInteger),data[i+2]);
  839. inc(i,3);
  840. end;
  841. end;
  842. procedure TDebugInfoDwarf.append_block1(attr: tdwarf_attribute; size: aint);
  843. begin
  844. current_asmdata.asmlists[al_dwarf_abbrev].concat(tai_const.create_uleb128bit(ord(attr)));
  845. current_asmdata.asmlists[al_dwarf_abbrev].concat(tai_const.create_uleb128bit(ord(DW_FORM_block1)));
  846. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(size));
  847. end;
  848. procedure TDebugInfoDwarf.append_labelentry(attr : tdwarf_attribute;sym : tasmsymbol);
  849. begin
  850. current_asmdata.asmlists[al_dwarf_abbrev].concat(tai_const.create_uleb128bit(ord(attr)));
  851. current_asmdata.asmlists[al_dwarf_abbrev].concat(tai_const.create_uleb128bit(ord(DW_FORM_addr)));
  852. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_sym(sym));
  853. end;
  854. procedure TDebugInfoDwarf.append_labelentry_addr_ref(attr : tdwarf_attribute;sym : tasmsymbol);
  855. begin
  856. current_asmdata.asmlists[al_dwarf_abbrev].concat(tai_const.create_uleb128bit(ord(DW_FORM_ref_addr)));
  857. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_sym(sym))
  858. end;
  859. procedure TDebugInfoDwarf.append_labelentry_ref(attr : tdwarf_attribute;sym : tasmsymbol);
  860. begin
  861. current_asmdata.asmlists[al_dwarf_abbrev].concat(tai_const.create_uleb128bit(ord(attr)));
  862. if not(tf_dwarf_only_local_labels in target_info.flags) then
  863. append_labelentry_addr_ref(attr, sym)
  864. else
  865. begin
  866. current_asmdata.asmlists[al_dwarf_abbrev].concat(tai_const.create_uleb128bit(ord(DW_FORM_ref4)));
  867. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_rel_sym(offsetreltype,current_asmdata.RefAsmSymbol(target_asm.labelprefix+'debug_info0'),sym));
  868. end;
  869. end;
  870. procedure TDebugInfoDwarf.append_labelentry_dataptr_common(attr : tdwarf_attribute);
  871. begin
  872. current_asmdata.asmlists[al_dwarf_abbrev].concat(tai_const.create_uleb128bit(ord(attr)));
  873. if use_64bit_headers then
  874. current_asmdata.asmlists[al_dwarf_abbrev].concat(tai_const.create_uleb128bit(ord(DW_FORM_data8)))
  875. else
  876. current_asmdata.asmlists[al_dwarf_abbrev].concat(tai_const.create_uleb128bit(ord(DW_FORM_data4)));
  877. end;
  878. procedure TDebugInfoDwarf.append_labelentry_dataptr_abs(attr : tdwarf_attribute;sym : tasmsymbol);
  879. begin
  880. {
  881. used for writing dwarf lineptr, loclistptr, macptr and rangelistptr classes as FORM_dataN
  882. The size of these depend on the header format
  883. Must be relative to another symbol on tf_dwarf_relative_addresses
  884. targets
  885. }
  886. if (tf_dwarf_relative_addresses in target_info.flags) then
  887. { use append_labelentry_dataptr_rel instead }
  888. internalerror(2007020210);
  889. append_labelentry_dataptr_common(attr);
  890. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_type_sym(offsetabstype,sym))
  891. end;
  892. procedure TDebugInfoDwarf.append_labelentry_dataptr_rel(attr : tdwarf_attribute;sym,endsym : tasmsymbol);
  893. begin
  894. {
  895. used for writing dwarf lineptr, loclistptr, macptr and rangelistptr classes as FORM_dataN
  896. The size of these depend on the header format
  897. Must be relative to another symbol on tf_dwarf_relative_addresses
  898. targets
  899. }
  900. append_labelentry_dataptr_common(attr);
  901. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_rel_sym(offsetreltype,sym,endsym));
  902. end;
  903. procedure TDebugInfoDwarf.finish_entry;
  904. begin
  905. current_asmdata.asmlists[al_dwarf_abbrev].concat(tai_const.create_8bit(0));
  906. current_asmdata.asmlists[al_dwarf_abbrev].concat(tai_const.create_8bit(0));
  907. end;
  908. procedure TDebugInfoDwarf.finish_children;
  909. begin
  910. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(0));
  911. end;
  912. procedure TDebugInfoDwarf.appenddef_ord(list:TAsmList;def:torddef);
  913. var
  914. basedef : tdef;
  915. sign : tdwarf_type;
  916. signform : tdwarf_form;
  917. fullbytesize : byte;
  918. begin
  919. case def.ordtype of
  920. s8bit,
  921. s16bit,
  922. s32bit,
  923. u8bit,
  924. u16bit,
  925. u32bit :
  926. begin
  927. { generate proper signed/unsigned info for types like 0..3 }
  928. { these are s8bit, but should be identified as unsigned }
  929. { because otherwise they are interpreted wrongly when used }
  930. { in a bitpacked record }
  931. if (def.low<0) then
  932. begin
  933. sign:=DW_ATE_signed;
  934. signform:=DW_FORM_sdata
  935. end
  936. else
  937. begin
  938. sign:=DW_ATE_unsigned;
  939. signform:=DW_FORM_udata
  940. end;
  941. fullbytesize:=def.size;
  942. case fullbytesize of
  943. 1:
  944. if (sign=DW_ATE_signed) then
  945. basedef:=s8inttype
  946. else
  947. basedef:=u8inttype;
  948. 2:
  949. if (sign=DW_ATE_signed) then
  950. basedef:=s16inttype
  951. else
  952. basedef:=u16inttype;
  953. 4:
  954. if (sign=DW_ATE_signed) then
  955. basedef:=s32inttype
  956. else
  957. basedef:=u32inttype;
  958. else
  959. internalerror(2008032201);
  960. end;
  961. if (def.low=torddef(basedef).low) and
  962. (def.high=torddef(basedef).high) then
  963. { base type such as byte/shortint/word/... }
  964. if assigned(def.typesym) then
  965. append_entry(DW_TAG_base_type,false,[
  966. DW_AT_name,DW_FORM_string,symname(def.typesym)+#0,
  967. DW_AT_encoding,DW_FORM_data1,sign,
  968. DW_AT_byte_size,DW_FORM_data1,fullbytesize])
  969. else
  970. append_entry(DW_TAG_base_type,false,[
  971. DW_AT_encoding,DW_FORM_data1,sign,
  972. DW_AT_byte_size,DW_FORM_data1,fullbytesize])
  973. else
  974. begin
  975. { subrange type }
  976. { note: don't do this 64 bit int types, they appear }
  977. { to be always clipped to s32bit for some reason }
  978. if assigned(def.typesym) then
  979. append_entry(DW_TAG_subrange_type,false,[
  980. DW_AT_name,DW_FORM_string,symname(def.typesym)+#0,
  981. DW_AT_lower_bound,signform,int64(def.low),
  982. DW_AT_upper_bound,signform,int64(def.high)
  983. ])
  984. else
  985. append_entry(DW_TAG_subrange_type,false,[
  986. DW_AT_lower_bound,signform,int64(def.low),
  987. DW_AT_upper_bound,signform,int64(def.high)
  988. ]);
  989. append_labelentry_ref(DW_AT_type,def_dwarf_lab(basedef));
  990. end;
  991. finish_entry;
  992. end;
  993. uvoid :
  994. begin
  995. { gdb 6.4 doesn't support DW_TAG_unspecified_type so we
  996. replace it with a unsigned type with size 0 (FK)
  997. }
  998. append_entry(DW_TAG_base_type,false,[
  999. DW_AT_name,DW_FORM_string,'Void'#0,
  1000. DW_AT_encoding,DW_FORM_data1,DW_ATE_unsigned,
  1001. DW_AT_byte_size,DW_FORM_data1,0
  1002. ]);
  1003. finish_entry;
  1004. end;
  1005. uchar :
  1006. begin
  1007. append_entry(DW_TAG_base_type,false,[
  1008. DW_AT_name,DW_FORM_string,'Char'#0,
  1009. DW_AT_encoding,DW_FORM_data1,DW_ATE_unsigned_char,
  1010. DW_AT_byte_size,DW_FORM_data1,1
  1011. ]);
  1012. finish_entry;
  1013. end;
  1014. uwidechar :
  1015. begin
  1016. append_entry(DW_TAG_base_type,false,[
  1017. DW_AT_name,DW_FORM_string,'WideChar'#0,
  1018. DW_AT_encoding,DW_FORM_data1,DW_ATE_unsigned_char,
  1019. DW_AT_byte_size,DW_FORM_data1,2
  1020. ]);
  1021. finish_entry;
  1022. end;
  1023. pasbool,
  1024. bool8bit :
  1025. begin
  1026. append_entry(DW_TAG_base_type,false,[
  1027. DW_AT_name,DW_FORM_string,'Boolean'#0,
  1028. DW_AT_encoding,DW_FORM_data1,DW_ATE_boolean,
  1029. DW_AT_byte_size,DW_FORM_data1,1
  1030. ]);
  1031. finish_entry;
  1032. end;
  1033. bool16bit :
  1034. begin
  1035. append_entry(DW_TAG_base_type,false,[
  1036. DW_AT_name,DW_FORM_string,'WordBool'#0,
  1037. DW_AT_encoding,DW_FORM_data1,DW_ATE_boolean,
  1038. DW_AT_byte_size,DW_FORM_data1,2
  1039. ]);
  1040. finish_entry;
  1041. end;
  1042. bool32bit :
  1043. begin
  1044. append_entry(DW_TAG_base_type,false,[
  1045. DW_AT_name,DW_FORM_string,'LongBool'#0,
  1046. DW_AT_encoding,DW_FORM_data1,DW_ATE_boolean,
  1047. DW_AT_byte_size,DW_FORM_data1,4
  1048. ]);
  1049. finish_entry;
  1050. end;
  1051. bool64bit :
  1052. begin
  1053. append_entry(DW_TAG_base_type,false,[
  1054. DW_AT_name,DW_FORM_string,'QWordBool'#0,
  1055. DW_AT_encoding,DW_FORM_data1,DW_ATE_boolean,
  1056. DW_AT_byte_size,DW_FORM_data1,8
  1057. ]);
  1058. finish_entry;
  1059. end;
  1060. u64bit :
  1061. begin
  1062. append_entry(DW_TAG_base_type,false,[
  1063. DW_AT_name,DW_FORM_string,'QWord'#0,
  1064. DW_AT_encoding,DW_FORM_data1,DW_ATE_unsigned,
  1065. DW_AT_byte_size,DW_FORM_data1,8
  1066. ]);
  1067. finish_entry;
  1068. end;
  1069. scurrency :
  1070. begin
  1071. { we should use DW_ATE_signed_fixed, however it isn't supported yet by GDB (FK) }
  1072. append_entry(DW_TAG_base_type,false,[
  1073. DW_AT_name,DW_FORM_string,'Currency'#0,
  1074. DW_AT_encoding,DW_FORM_data1,DW_ATE_signed,
  1075. DW_AT_byte_size,DW_FORM_data1,8
  1076. ]);
  1077. finish_entry;
  1078. end;
  1079. s64bit :
  1080. begin
  1081. append_entry(DW_TAG_base_type,false,[
  1082. DW_AT_name,DW_FORM_string,'Int64'#0,
  1083. DW_AT_encoding,DW_FORM_data1,DW_ATE_signed,
  1084. DW_AT_byte_size,DW_FORM_data1,8
  1085. ]);
  1086. finish_entry;
  1087. end;
  1088. else
  1089. internalerror(200601287);
  1090. end;
  1091. end;
  1092. procedure TDebugInfoDwarf.appenddef_float(list:TAsmList;def:tfloatdef);
  1093. begin
  1094. case def.floattype of
  1095. s32real,
  1096. s64real,
  1097. s80real:
  1098. if assigned(def.typesym) then
  1099. append_entry(DW_TAG_base_type,false,[
  1100. DW_AT_name,DW_FORM_string,symname(def.typesym)+#0,
  1101. DW_AT_encoding,DW_FORM_data1,DW_ATE_float,
  1102. DW_AT_byte_size,DW_FORM_data1,def.size
  1103. ])
  1104. else
  1105. append_entry(DW_TAG_base_type,false,[
  1106. DW_AT_encoding,DW_FORM_data1,DW_ATE_float,
  1107. DW_AT_byte_size,DW_FORM_data1,def.size
  1108. ]);
  1109. s64currency:
  1110. { we should use DW_ATE_signed_fixed, however it isn't supported yet by GDB (FK) }
  1111. if assigned(def.typesym) then
  1112. append_entry(DW_TAG_base_type,false,[
  1113. DW_AT_name,DW_FORM_string,symname(def.typesym)+#0,
  1114. DW_AT_encoding,DW_FORM_data1,DW_ATE_signed,
  1115. DW_AT_byte_size,DW_FORM_data1,8
  1116. ])
  1117. else
  1118. append_entry(DW_TAG_base_type,false,[
  1119. DW_AT_encoding,DW_FORM_data1,DW_ATE_signed,
  1120. DW_AT_byte_size,DW_FORM_data1,8
  1121. ]);
  1122. s64comp:
  1123. if assigned(def.typesym) then
  1124. append_entry(DW_TAG_base_type,false,[
  1125. DW_AT_name,DW_FORM_string,symname(def.typesym)+#0,
  1126. DW_AT_encoding,DW_FORM_data1,DW_ATE_signed,
  1127. DW_AT_byte_size,DW_FORM_data1,8
  1128. ])
  1129. else
  1130. append_entry(DW_TAG_base_type,false,[
  1131. DW_AT_encoding,DW_FORM_data1,DW_ATE_signed,
  1132. DW_AT_byte_size,DW_FORM_data1,8
  1133. ]);
  1134. else
  1135. internalerror(200601289);
  1136. end;
  1137. finish_entry;
  1138. end;
  1139. procedure TDebugInfoDwarf.appenddef_enum(list:TAsmList;def:tenumdef);
  1140. var
  1141. hp : tenumsym;
  1142. begin
  1143. if assigned(def.typesym) then
  1144. append_entry(DW_TAG_enumeration_type,true,[
  1145. DW_AT_name,DW_FORM_string,symname(def.typesym)+#0,
  1146. DW_AT_byte_size,DW_FORM_data1,def.size
  1147. ])
  1148. else
  1149. append_entry(DW_TAG_enumeration_type,true,[
  1150. DW_AT_byte_size,DW_FORM_data1,def.size
  1151. ]);
  1152. if assigned(def.basedef) then
  1153. append_labelentry_ref(DW_AT_type,def_dwarf_lab(def.basedef));
  1154. finish_entry;
  1155. { write enum symbols }
  1156. hp:=tenumsym(def.firstenum);
  1157. while assigned(hp) do
  1158. begin
  1159. append_entry(DW_TAG_enumerator,false,[
  1160. DW_AT_name,DW_FORM_string,symname(hp)+#0,
  1161. DW_AT_const_value,DW_FORM_data4,hp.value
  1162. ]);
  1163. finish_entry;
  1164. hp:=tenumsym(hp).nextenum;
  1165. end;
  1166. finish_children;
  1167. end;
  1168. procedure TDebugInfoDwarf.appenddef_array(list:TAsmList;def:tarraydef);
  1169. var
  1170. size : aint;
  1171. elesize : aint;
  1172. elestrideattr : tdwarf_attribute;
  1173. labsym: tasmlabel;
  1174. begin
  1175. if is_dynamic_array(def) then
  1176. begin
  1177. { It's a pointer to the actual array }
  1178. current_asmdata.getaddrlabel(labsym);
  1179. append_entry(DW_TAG_pointer_type,false,[]);
  1180. append_labelentry_ref(DW_AT_type,labsym);
  1181. finish_entry;
  1182. current_asmdata.asmlists[al_dwarf_info].concat(tai_symbol.create(labsym,0));
  1183. end;
  1184. if not is_packed_array(def) then
  1185. begin
  1186. elestrideattr := DW_AT_byte_stride;
  1187. elesize := def.elesize;
  1188. end
  1189. else
  1190. begin
  1191. elestrideattr := DW_AT_stride_size;
  1192. elesize := def.elepackedbitsize;
  1193. end;
  1194. if is_special_array(def) then
  1195. begin
  1196. { no known size, no known upper bound }
  1197. if assigned(def.typesym) then
  1198. append_entry(DW_TAG_array_type,true,[
  1199. DW_AT_name,DW_FORM_string,symname(def.typesym)+#0
  1200. ])
  1201. else
  1202. append_entry(DW_TAG_array_type,true,[]);
  1203. append_labelentry_ref(DW_AT_type,def_dwarf_lab(def.elementdef));
  1204. finish_entry;
  1205. { a missing upper bound means "unknown"/default }
  1206. append_entry(DW_TAG_subrange_type,false,[
  1207. DW_AT_lower_bound,DW_FORM_sdata,def.lowrange,
  1208. elestrideattr,DW_FORM_udata,elesize
  1209. ]);
  1210. end
  1211. else
  1212. begin
  1213. size:=def.size;
  1214. if assigned(def.typesym) then
  1215. append_entry(DW_TAG_array_type,true,[
  1216. DW_AT_name,DW_FORM_string,symname(def.typesym)+#0,
  1217. DW_AT_byte_size,DW_FORM_udata,size
  1218. ])
  1219. else
  1220. append_entry(DW_TAG_array_type,true,[
  1221. DW_AT_byte_size,DW_FORM_udata,size
  1222. ]);
  1223. append_labelentry_ref(DW_AT_type,def_dwarf_lab(def.elementdef));
  1224. finish_entry;
  1225. { to simplify things, we don't write a multidimensional array here }
  1226. append_entry(DW_TAG_subrange_type,false,[
  1227. DW_AT_lower_bound,DW_FORM_sdata,def.lowrange,
  1228. DW_AT_upper_bound,DW_FORM_sdata,def.highrange,
  1229. elestrideattr,DW_FORM_udata,elesize
  1230. ]);
  1231. end;
  1232. append_labelentry_ref(DW_AT_type,def_dwarf_lab(def.rangedef));
  1233. finish_entry;
  1234. finish_children;
  1235. end;
  1236. procedure TDebugInfoDwarf.appenddef_record(list:TAsmList;def:trecorddef);
  1237. begin
  1238. if assigned(def.typesym) then
  1239. appenddef_record_named(list,def,symname(def.typesym))
  1240. else
  1241. appenddef_record_named(list,def,'');
  1242. end;
  1243. procedure TDebugInfoDwarf.appenddef_record_named(list:TAsmList;def:trecorddef;const name: shortstring);
  1244. begin
  1245. if (name<>'') then
  1246. append_entry(DW_TAG_structure_type,true,[
  1247. DW_AT_name,DW_FORM_string,name+#0,
  1248. DW_AT_byte_size,DW_FORM_udata,def.size
  1249. ])
  1250. else
  1251. append_entry(DW_TAG_structure_type,true,[
  1252. DW_AT_byte_size,DW_FORM_udata,def.size
  1253. ]);
  1254. finish_entry;
  1255. def.symtable.symList.ForEachCall(@enum_membersyms_callback,nil);
  1256. finish_children;
  1257. end;
  1258. procedure TDebugInfoDwarf.appenddef_pointer(list:TAsmList;def:tpointerdef);
  1259. begin
  1260. append_entry(DW_TAG_pointer_type,false,[]);
  1261. if not(is_voidpointer(def)) then
  1262. append_labelentry_ref(DW_AT_type,def_dwarf_lab(def.pointeddef));
  1263. finish_entry;
  1264. end;
  1265. procedure TDebugInfoDwarf.appenddef_string(list:TAsmList;def:tstringdef);
  1266. procedure addnormalstringdef(const name: shortstring; lendef: tdef; maxlen: aword);
  1267. var
  1268. { maxlen can be > high(int64) }
  1269. slen : aword;
  1270. arr : tasmlabel;
  1271. begin
  1272. { fix length of openshortstring }
  1273. slen:=aword(def.len);
  1274. if (slen=0) or
  1275. (slen>maxlen) then
  1276. slen:=maxlen;
  1277. { create a structure with two elements }
  1278. if not(tf_dwarf_only_local_labels in target_info.flags) then
  1279. current_asmdata.getdatalabel(arr)
  1280. else
  1281. current_asmdata.getaddrlabel(arr);
  1282. append_entry(DW_TAG_structure_type,true,[
  1283. DW_AT_name,DW_FORM_string,name+#0,
  1284. DW_AT_byte_size,DW_FORM_udata,qword(lendef.size)+slen
  1285. ]);
  1286. finish_entry;
  1287. { length entry }
  1288. append_entry(DW_TAG_member,false,[
  1289. DW_AT_name,DW_FORM_string,'length'#0,
  1290. DW_AT_data_member_location,DW_FORM_block1,1+lengthuleb128(0)
  1291. ]);
  1292. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_plus_uconst)));
  1293. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_uleb128bit(0));
  1294. append_labelentry_ref(DW_AT_type,def_dwarf_lab(lendef));
  1295. finish_entry;
  1296. { string data entry }
  1297. append_entry(DW_TAG_member,false,[
  1298. DW_AT_name,DW_FORM_string,'st'#0,
  1299. DW_AT_data_member_location,DW_FORM_block1,1+lengthuleb128(1)
  1300. ]);
  1301. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_plus_uconst)));
  1302. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_uleb128bit(lendef.size));
  1303. append_labelentry_ref(DW_AT_type,arr);
  1304. finish_entry;
  1305. finish_children;
  1306. { now the data array }
  1307. current_asmdata.asmlists[al_dwarf_info].concat(tai_symbol.create(arr,0));
  1308. append_entry(DW_TAG_array_type,true,[
  1309. DW_AT_byte_size,DW_FORM_udata,def.size,
  1310. DW_AT_byte_stride,DW_FORM_udata,1
  1311. ]);
  1312. append_labelentry_ref(DW_AT_type,def_dwarf_lab(cchartype));
  1313. finish_entry;
  1314. append_entry(DW_TAG_subrange_type,false,[
  1315. DW_AT_lower_bound,DW_FORM_udata,0,
  1316. DW_AT_upper_bound,DW_FORM_udata,qword(slen)
  1317. ]);
  1318. append_labelentry_ref(DW_AT_type,def_dwarf_lab(lendef));
  1319. finish_entry;
  1320. finish_children;
  1321. end;
  1322. begin
  1323. case def.stringtype of
  1324. st_shortstring:
  1325. begin
  1326. addnormalstringdef('ShortString',u8inttype,255);
  1327. end;
  1328. st_longstring:
  1329. begin
  1330. { a) we don't actually support variables of this type currently
  1331. b) this type is only used as the type for constant strings
  1332. > 255 characters
  1333. c) in such a case, gdb will allocate and initialise enough
  1334. memory to hold the maximum size for such a string
  1335. -> don't use high(qword)/high(cardinal) as maximum, since that
  1336. will cause exhausting the VM space, but some "reasonably high"
  1337. number that should be enough for most constant strings
  1338. }
  1339. {$ifdef cpu64bitaddr}
  1340. addnormalstringdef('LongString',u64inttype,qword(1024*1024));
  1341. {$else cpu64bitaddr}
  1342. addnormalstringdef('LongString',u32inttype,cardinal(1024*1024));
  1343. {$endif cpu64bitaddr}
  1344. end;
  1345. st_ansistring:
  1346. begin
  1347. { looks like a pchar }
  1348. append_entry(DW_TAG_pointer_type,false,[]);
  1349. append_labelentry_ref(DW_AT_type,def_dwarf_lab(cchartype));
  1350. finish_entry;
  1351. end;
  1352. st_unicodestring,
  1353. st_widestring:
  1354. begin
  1355. { looks like a pwidechar }
  1356. append_entry(DW_TAG_pointer_type,false,[]);
  1357. append_labelentry_ref(DW_AT_type,def_dwarf_lab(cwidechartype));
  1358. finish_entry;
  1359. end;
  1360. end;
  1361. end;
  1362. procedure TDebugInfoDwarf.appenddef_procvar(list:TAsmList;def:tprocvardef);
  1363. procedure doappend;
  1364. var
  1365. i : longint;
  1366. begin
  1367. if assigned(def.typesym) then
  1368. append_entry(DW_TAG_subroutine_type,true,[
  1369. DW_AT_name,DW_FORM_string,symname(def.typesym)+#0,
  1370. DW_AT_prototyped,DW_FORM_flag,true
  1371. ])
  1372. else
  1373. append_entry(DW_TAG_subroutine_type,true,[
  1374. DW_AT_prototyped,DW_FORM_flag,true
  1375. ]);
  1376. if not(is_void(tprocvardef(def).returndef)) then
  1377. append_labelentry_ref(DW_AT_type,def_dwarf_lab(tprocvardef(def).returndef));
  1378. finish_entry;
  1379. { write parameters }
  1380. for i:=0 to def.paras.count-1 do
  1381. begin
  1382. append_entry(DW_TAG_formal_parameter,false,[
  1383. DW_AT_name,DW_FORM_string,symname(tsym(def.paras[i]))+#0
  1384. ]);
  1385. append_labelentry_ref(DW_AT_type,def_dwarf_lab(tparavarsym(def.paras[i]).vardef));
  1386. finish_entry;
  1387. end;
  1388. finish_children;
  1389. end;
  1390. var
  1391. proc : tasmlabel;
  1392. begin
  1393. if def.is_methodpointer then
  1394. begin
  1395. { create a structure with two elements }
  1396. if not(tf_dwarf_only_local_labels in target_info.flags) then
  1397. current_asmdata.getdatalabel(proc)
  1398. else
  1399. current_asmdata.getaddrlabel(proc);
  1400. append_entry(DW_TAG_structure_type,true,[
  1401. DW_AT_byte_size,DW_FORM_data1,2*sizeof(pint)
  1402. ]);
  1403. finish_entry;
  1404. { proc entry }
  1405. append_entry(DW_TAG_member,false,[
  1406. DW_AT_name,DW_FORM_string,'Proc'#0,
  1407. DW_AT_data_member_location,DW_FORM_block1,1+lengthuleb128(0)
  1408. ]);
  1409. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_plus_uconst)));
  1410. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_uleb128bit(0));
  1411. append_labelentry_ref(DW_AT_type,proc);
  1412. finish_entry;
  1413. { self entry }
  1414. append_entry(DW_TAG_member,false,[
  1415. DW_AT_name,DW_FORM_string,'Self'#0,
  1416. DW_AT_data_member_location,DW_FORM_block1,1+lengthuleb128(sizeof(pint))
  1417. ]);
  1418. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_plus_uconst)));
  1419. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_uleb128bit(sizeof(pint)));
  1420. append_labelentry_ref(DW_AT_type,def_dwarf_lab(class_tobject));
  1421. finish_entry;
  1422. finish_children;
  1423. current_asmdata.asmlists[al_dwarf_info].concat(tai_symbol.create(proc,0));
  1424. doappend;
  1425. end
  1426. else
  1427. doappend;
  1428. end;
  1429. procedure TDebugInfoDwarf.beforeappenddef(list:TAsmList;def:tdef);
  1430. var
  1431. labsym : tasmsymbol;
  1432. begin
  1433. current_asmdata.asmlists[al_dwarf_info].concat(tai_comment.Create(strpnew('Definition '+def.typename)));
  1434. labsym:=def_dwarf_lab(def);
  1435. if ds_dwarf_dbg_info_written in def.defstates then
  1436. current_asmdata.asmlists[al_dwarf_info].concat(tai_symbol.create_global(labsym,0))
  1437. else
  1438. current_asmdata.asmlists[al_dwarf_info].concat(tai_symbol.create(labsym,0));
  1439. { On Darwin, dwarf info is not linked in the final binary,
  1440. but kept in the individual object files. This allows for
  1441. faster linking, but means that you have to keep the object
  1442. files for debugging and also that gdb only loads in the
  1443. debug info of a particular object file once you step into
  1444. or over a procedure in it.
  1445. To solve this, there is a tool called dsymutil which can
  1446. extract all the dwarf info from a program's object files.
  1447. This utility however performs "smart linking" on the dwarf
  1448. info and throws away all unreferenced dwarf entries. Since
  1449. variables' types always point to the dwarfino for a tdef
  1450. and never to that for a typesym, this means all debug
  1451. entries generated for typesyms are thrown away.
  1452. The problem with that is that we translate typesyms into
  1453. DW_TAG_typedef, and gdb's dwarf-2 reader only makes types
  1454. globally visibly if they are defined using a DW_TAG_typedef.
  1455. So as a result, before running dsymutil types only become
  1456. available once you stepped into/over a function in the object
  1457. file where they are declared, and after running dsymutil they
  1458. are all gone (printng variables still works because the
  1459. tdef dwarf info is still available, but you cannot typecast
  1460. anything outside the declaring units because the type names
  1461. are not known there).
  1462. The solution: if a tdef has an associated typesym, let the
  1463. debug label for the tdef point to a DW_TAG_typedef instead
  1464. of directly to the tdef itself. And don't write anything
  1465. special for the typesym itself.
  1466. Update: we now also do this for other platforms, because
  1467. otherwise if you compile unit A without debug info and
  1468. use one of its types in unit B, then no typedef will be
  1469. generated and hence gdb will not be able to give a definition
  1470. of the type.
  1471. }
  1472. if assigned(def.typesym) and
  1473. not(df_generic in def.defoptions) then
  1474. begin
  1475. current_asmdata.getaddrlabel(TAsmLabel(pointer(labsym)));
  1476. append_entry(DW_TAG_typedef,false,[
  1477. DW_AT_name,DW_FORM_string,symname(def.typesym)+#0
  1478. ]);
  1479. append_labelentry_ref(DW_AT_type,labsym);
  1480. finish_entry;
  1481. current_asmdata.asmlists[al_dwarf_info].concat(tai_symbol.create(labsym,0));
  1482. end;
  1483. end;
  1484. procedure TDebugInfoDwarf.afterappenddef(list:TAsmList;def:tdef);
  1485. var
  1486. labsym : tasmsymbol;
  1487. begin
  1488. { create a derived reference type for pass-by-reference parameters }
  1489. { (gdb doesn't support DW_AT_variable_parameter yet) }
  1490. labsym:=def_dwarf_ref_lab(def);
  1491. if ds_dwarf_dbg_info_written in def.defstates then
  1492. current_asmdata.asmlists[al_dwarf_info].concat(tai_symbol.create_global(labsym,0))
  1493. else
  1494. current_asmdata.asmlists[al_dwarf_info].concat(tai_symbol.create(labsym,0));
  1495. append_entry(DW_TAG_reference_type,false,[]);
  1496. append_labelentry_ref(DW_AT_type,def_dwarf_lab(def));
  1497. finish_entry;
  1498. end;
  1499. procedure TDebugInfoDwarf.appendprocdef(list:TAsmList; def:tprocdef);
  1500. function dwarf_calling_convention(def: tprocdef): Tdwarf_calling_convention;
  1501. begin
  1502. case def.proccalloption of
  1503. pocall_register:
  1504. result:=DW_CC_GNU_borland_fastcall_i386;
  1505. pocall_cdecl,
  1506. pocall_stdcall,
  1507. pocall_cppdecl,
  1508. pocall_mwpascal:
  1509. result:=DW_CC_normal;
  1510. else
  1511. result:=DW_CC_nocall;
  1512. end
  1513. end;
  1514. var
  1515. procendlabel : tasmlabel;
  1516. procentry : string;
  1517. cc : Tdwarf_calling_convention;
  1518. st : tsymtable;
  1519. i : longint;
  1520. vmtindexnr : pint;
  1521. begin
  1522. if not assigned(def.procstarttai) then
  1523. exit;
  1524. { Procdefs are not handled by the regular def writing code, so
  1525. dbg_state is not set/checked for them. Do it here. }
  1526. if (def.dbg_state in [dbg_state_writing,dbg_state_written]) then
  1527. exit;
  1528. { Write methods and only in the scope of their parent objectdefs. }
  1529. if (def.owner.symtabletype=objectsymtable) then
  1530. begin
  1531. { this code can also work for nested procdefs, but is not yet
  1532. activated for those because there is no clear advantage yet to
  1533. limiting the scope of nested procedures to that of their parent,
  1534. and it makes it impossible to set breakpoints in them by
  1535. referring to their name. }
  1536. st:=def.owner;
  1537. while assigned(st.defowner) and
  1538. (tdef(st.defowner).typ = procdef) do
  1539. st:=tprocdef(st.defowner).owner;
  1540. if assigned(st) and
  1541. (tdef(st.defowner).dbg_state<>dbg_state_writing) then
  1542. exit;
  1543. end;
  1544. def.dbg_state:=dbg_state_writing;
  1545. current_asmdata.asmlists[al_dwarf_info].concat(tai_comment.Create(strpnew('Procdef '+def.fullprocname(true))));
  1546. append_entry(DW_TAG_subprogram,true,
  1547. [DW_AT_name,DW_FORM_string,symname(def.procsym)+#0
  1548. { data continues below }
  1549. { problem: base reg isn't known here
  1550. DW_AT_frame_base,DW_FORM_block1,1
  1551. }
  1552. ]);
  1553. { Append optional flags. }
  1554. { Calling convention. }
  1555. cc:=dwarf_calling_convention(def);
  1556. if (cc<>DW_CC_normal) then
  1557. append_attribute(DW_AT_calling_convention,DW_FORM_data1,[ord(cc)]);
  1558. { Externally visible. }
  1559. if (po_global in def.procoptions) and
  1560. (def.parast.symtablelevel<=normal_function_level) then
  1561. append_attribute(DW_AT_external,DW_FORM_flag,[true]);
  1562. { Abstract or virtual/overriding method. }
  1563. if (([po_abstractmethod, po_virtualmethod, po_overridingmethod] * def.procoptions) <> []) then
  1564. begin
  1565. if not(po_abstractmethod in def.procoptions) then
  1566. append_attribute(DW_AT_virtuality,DW_FORM_data1,[ord(DW_VIRTUALITY_virtual)])
  1567. else
  1568. append_attribute(DW_AT_virtuality,DW_FORM_data1,[ord(DW_VIRTUALITY_pure_virtual)]);
  1569. { Element number in the vmt (needs to skip stuff coming before the
  1570. actual method addresses in the vmt, so we use vmtmethodoffset()
  1571. and then divide by sizeof(pint)). }
  1572. vmtindexnr:=tobjectdef(def.owner.defowner).vmtmethodoffset(def.extnumber) div sizeof(pint);
  1573. append_attribute(DW_AT_vtable_elem_location,DW_FORM_block1,[1+LengthUleb128(vmtindexnr)]);
  1574. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_constu)));
  1575. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.Create_uleb128bit(vmtindexnr));
  1576. end;
  1577. { Return type. }
  1578. if not(is_void(tprocdef(def).returndef)) then
  1579. append_labelentry_ref(DW_AT_type,def_dwarf_lab(tprocdef(def).returndef));
  1580. { mark end of procedure }
  1581. current_asmdata.getlabel(procendlabel,alt_dbgtype);
  1582. current_asmdata.asmlists[al_procedures].insertbefore(tai_label.create(procendlabel),def.procendtai);
  1583. if (target_info.system = system_powerpc64_linux) then
  1584. procentry := '.' + def.mangledname
  1585. else
  1586. procentry := def.mangledname;
  1587. append_labelentry(DW_AT_low_pc,current_asmdata.RefAsmSymbol(procentry));
  1588. append_labelentry(DW_AT_high_pc,procendlabel);
  1589. { Don't write the funcretsym explicitly, it's also in the
  1590. localsymtable and/or parasymtable.
  1591. }
  1592. finish_entry;
  1593. if assigned(def.parast) then
  1594. begin
  1595. { First insert self, because gdb uses the fact whether or not the
  1596. first parameter of a method is artificial to distinguish static
  1597. from regular methods. }
  1598. { Find the self parameter (it's usually last in the list). }
  1599. for i:=def.parast.symlist.count-1 downto 0 do
  1600. if (tsym(def.parast.symlist[i]).typ = paravarsym) and
  1601. (vo_is_self in tparavarsym(def.parast.symlist[i]).varoptions) then
  1602. { insert it as the first one }
  1603. appendsym_var_with_name_type_offset(list,
  1604. tparavarsym(def.parast.symlist[i]),
  1605. symname(tsym(def.parast.symlist[i])),
  1606. tparavarsym(def.parast.symlist[i]).vardef,0,true);
  1607. { Now insert the rest (this will skip the self parameter). }
  1608. write_symtable_syms(current_asmdata.asmlists[al_dwarf_info],def.parast);
  1609. end;
  1610. { local type defs and vars should not be written
  1611. inside the main proc }
  1612. if assigned(def.localst) and
  1613. (def.localst.symtabletype=localsymtable) then
  1614. write_symtable_syms(current_asmdata.asmlists[al_dwarf_info],def.localst);
  1615. { last write the types from this procdef }
  1616. if assigned(def.parast) then
  1617. write_symtable_defs(current_asmdata.asmlists[al_dwarf_info],def.parast);
  1618. if assigned(def.localst) and
  1619. (def.localst.symtabletype=localsymtable) then
  1620. begin
  1621. write_symtable_defs(current_asmdata.asmlists[al_dwarf_info],def.localst);
  1622. { Write nested procedures -- disabled, see scope check at the
  1623. beginning; currently, these are still written in the global
  1624. scope. }
  1625. // write_symtable_procdefs(current_asmdata.asmlists[al_dwarf_info],def.localst);
  1626. end;
  1627. finish_children;
  1628. end;
  1629. function TDebugInfoDwarf.get_symlist_sym_offset(symlist: ppropaccesslistitem; out sym: tabstractvarsym; out offset: pint): boolean;
  1630. var
  1631. elesize : pint;
  1632. currdef : tdef;
  1633. indirection: boolean;
  1634. begin
  1635. result:=false;
  1636. if not assigned(symlist) then
  1637. exit;
  1638. sym:=nil;
  1639. offset:=0;
  1640. currdef:=nil;
  1641. indirection:=false;
  1642. repeat
  1643. case symlist^.sltype of
  1644. sl_load:
  1645. begin
  1646. if assigned(sym) then
  1647. internalerror(2009031203);
  1648. if not(symlist^.sym.typ in [paravarsym,localvarsym,staticvarsym,fieldvarsym]) then
  1649. { can't handle... }
  1650. exit;
  1651. sym:=tabstractvarsym(symlist^.sym);
  1652. currdef:=tabstractvarsym(sym).vardef;
  1653. if ((sym.typ=paravarsym) and
  1654. paramanager.push_addr_param(tparavarsym(sym).varspez,sym.vardef,tprocdef(sym.owner.defowner).proccalloption)) then
  1655. indirection:=true;
  1656. end;
  1657. sl_subscript:
  1658. begin
  1659. if not assigned(currdef) then
  1660. internalerror(2009031301);
  1661. if (symlist^.sym.typ<>fieldvarsym) then
  1662. internalerror(2009031202);
  1663. { can't handle offsets with indirections yet }
  1664. if indirection then
  1665. exit;
  1666. if is_packed_record_or_object(currdef) then
  1667. begin
  1668. { can't calculate the address of a non-byte aligned field }
  1669. if (tfieldvarsym(symlist^.sym).fieldoffset mod 8) <> 0 then
  1670. exit;
  1671. inc(offset,tfieldvarsym(symlist^.sym).fieldoffset div 8)
  1672. end
  1673. else
  1674. inc(offset,tfieldvarsym(symlist^.sym).fieldoffset);
  1675. currdef:=tfieldvarsym(symlist^.sym).vardef;
  1676. end;
  1677. sl_absolutetype,
  1678. sl_typeconv:
  1679. begin
  1680. currdef:=tfieldvarsym(symlist^.sym).vardef;
  1681. { ignore, these don't change the address }
  1682. end;
  1683. sl_vec:
  1684. begin
  1685. if not assigned(currdef) or
  1686. (currdef.typ<>arraydef) then
  1687. internalerror(2009031201);
  1688. { can't handle offsets with indirections yet }
  1689. if indirection then
  1690. exit;
  1691. if not is_packed_array(currdef) then
  1692. elesize:=tarraydef(currdef).elesize
  1693. else
  1694. begin
  1695. elesize:=tarraydef(currdef).elepackedbitsize;
  1696. { can't calculate the address of a non-byte aligned element }
  1697. if (elesize mod 8)<>0 then
  1698. exit;
  1699. elesize:=elesize div 8;
  1700. end;
  1701. inc(offset,(symlist^.value.svalue-tarraydef(currdef).lowrange)*elesize);
  1702. currdef:=tarraydef(currdef).elementdef;
  1703. end;
  1704. else
  1705. internalerror(2009031401);
  1706. end;
  1707. symlist:=symlist^.next;
  1708. until not assigned(symlist);
  1709. if not assigned(sym) then
  1710. internalerror(2009031205);
  1711. result:=true;
  1712. end;
  1713. procedure TDebugInfoDwarf.appendsym_var(list:TAsmList;sym:tabstractnormalvarsym);
  1714. begin
  1715. appendsym_var_with_name_type_offset(list,sym,symname(sym),sym.vardef,0,false);
  1716. end;
  1717. procedure TDebugInfoDwarf.appendsym_var_with_name_type_offset(list:TAsmList; sym:tabstractnormalvarsym; const name: string; def: tdef; offset: pint; do_self: boolean);
  1718. var
  1719. templist : TAsmList;
  1720. blocksize : longint;
  1721. tag : tdwarf_tag;
  1722. dreg : byte;
  1723. begin
  1724. { external symbols can't be resolved at link time, so we
  1725. can't generate stabs for them
  1726. not sure if this applies to dwarf as well (FK)
  1727. }
  1728. if vo_is_external in sym.varoptions then
  1729. exit;
  1730. { Self must be the first inserted parameter, see
  1731. appendprocdef(). }
  1732. if not(do_self) and
  1733. (vo_is_self in sym.varoptions) then
  1734. exit;
  1735. { There is no space allocated for not referenced locals }
  1736. if (sym.owner.symtabletype=localsymtable) and (sym.refs=0) then
  1737. exit;
  1738. templist:=TAsmList.create;
  1739. case sym.localloc.loc of
  1740. LOC_REGISTER,
  1741. LOC_CREGISTER,
  1742. LOC_MMREGISTER,
  1743. LOC_CMMREGISTER,
  1744. LOC_FPUREGISTER,
  1745. LOC_CFPUREGISTER :
  1746. begin
  1747. templist.concat(tai_const.create_8bit(ord(DW_OP_regx)));
  1748. dreg:=dwarf_reg(sym.localloc.register);
  1749. templist.concat(tai_const.create_uleb128bit(dreg));
  1750. blocksize:=1+Lengthuleb128(dreg);
  1751. end;
  1752. else
  1753. begin
  1754. case sym.typ of
  1755. staticvarsym:
  1756. begin
  1757. if (vo_is_thread_var in sym.varoptions) then
  1758. begin
  1759. { TODO: !!! FIXME: dwarf for thread vars !!!}
  1760. blocksize:=0;
  1761. end
  1762. else
  1763. begin
  1764. templist.concat(tai_const.create_8bit(ord(DW_OP_addr)));
  1765. templist.concat(tai_const.createname(sym.mangledname,offset));
  1766. blocksize:=1+sizeof(puint);
  1767. end;
  1768. end;
  1769. paravarsym,
  1770. localvarsym:
  1771. begin
  1772. dreg:=dwarf_reg(sym.localloc.reference.base);
  1773. templist.concat(tai_const.create_8bit(ord(DW_OP_breg0)+dreg));
  1774. templist.concat(tai_const.create_sleb128bit(sym.localloc.reference.offset+offset));
  1775. blocksize:=1+Lengthsleb128(sym.localloc.reference.offset);
  1776. end
  1777. else
  1778. internalerror(200601288);
  1779. end;
  1780. end;
  1781. end;
  1782. { function results must not be added to the parameter list,
  1783. as they are not part of the signature of the function
  1784. (gdb automatically adds them according to the ABI specifications
  1785. when calling the function)
  1786. }
  1787. if (sym.typ=paravarsym) and
  1788. not(vo_is_funcret in sym.varoptions) then
  1789. tag:=DW_TAG_formal_parameter
  1790. else
  1791. tag:=DW_TAG_variable;
  1792. if not(sym.localloc.loc in [LOC_REGISTER,LOC_CREGISTER,LOC_MMREGISTER,
  1793. LOC_CMMREGISTER,LOC_FPUREGISTER,LOC_CFPUREGISTER]) and
  1794. ((sym.owner.symtabletype = globalsymtable) or
  1795. (sp_static in sym.symoptions) or
  1796. (vo_is_public in sym.varoptions)) then
  1797. append_entry(tag,false,[
  1798. DW_AT_name,DW_FORM_string,name+#0,
  1799. {
  1800. DW_AT_decl_file,DW_FORM_data1,0,
  1801. DW_AT_decl_line,DW_FORM_data1,
  1802. }
  1803. DW_AT_external,DW_FORM_flag,true,
  1804. { data continues below }
  1805. DW_AT_location,DW_FORM_block1,blocksize
  1806. ])
  1807. {$ifdef gdb_supports_DW_AT_variable_parameter}
  1808. else if (sym.typ=paravarsym) and
  1809. paramanager.push_addr_param(sym.varspez,sym.vardef,tprocdef(sym.owner.defowner).proccalloption) and
  1810. not(vo_has_local_copy in sym.varoptions) and
  1811. not is_open_string(sym.vardef) then
  1812. append_entry(tag,false,[
  1813. DW_AT_name,DW_FORM_string,name+#0,
  1814. DW_AT_variable_parameter,DW_FORM_flag,true,
  1815. {
  1816. DW_AT_decl_file,DW_FORM_data1,0,
  1817. DW_AT_decl_line,DW_FORM_data1,
  1818. }
  1819. { data continues below }
  1820. DW_AT_location,DW_FORM_block1,blocksize
  1821. ])
  1822. {$endif gdb_supports_DW_AT_variable_parameter}
  1823. else
  1824. append_entry(tag,false,[
  1825. DW_AT_name,DW_FORM_string,name+#0,
  1826. {
  1827. DW_AT_decl_file,DW_FORM_data1,0,
  1828. DW_AT_decl_line,DW_FORM_data1,
  1829. }
  1830. { data continues below }
  1831. DW_AT_location,DW_FORM_block1,blocksize
  1832. ]);
  1833. { append block data }
  1834. current_asmdata.asmlists[al_dwarf_info].concatlist(templist);
  1835. { Mark self as artificial for methods, because gdb uses the fact
  1836. whether or not the first parameter of a method is artificial to
  1837. distinguish regular from static methods (since there are no
  1838. no vo_is_self parameters for static methods, we don't have to check
  1839. that). }
  1840. if (vo_is_self in sym.varoptions) then
  1841. append_attribute(DW_AT_artificial,DW_FORM_flag,[true]);
  1842. {$ifndef gdb_supports_DW_AT_variable_parameter}
  1843. if (sym.typ=paravarsym) and
  1844. paramanager.push_addr_param(sym.varspez,sym.vardef,tprocdef(sym.owner.defowner).proccalloption) and
  1845. not(vo_has_local_copy in sym.varoptions) and
  1846. not is_open_string(sym.vardef) then
  1847. append_labelentry_ref(DW_AT_type,def_dwarf_ref_lab(def))
  1848. else
  1849. {$endif not gdb_supports_DW_AT_variable_parameter}
  1850. append_labelentry_ref(DW_AT_type,def_dwarf_lab(def));
  1851. templist.free;
  1852. finish_entry;
  1853. end;
  1854. procedure TDebugInfoDwarf.appendsym_staticvar(list:TAsmList;sym:tstaticvarsym);
  1855. begin
  1856. appendsym_var(list,sym);
  1857. end;
  1858. procedure TDebugInfoDwarf.appendsym_localvar(list:TAsmList;sym:tlocalvarsym);
  1859. begin
  1860. appendsym_var(list,sym);
  1861. end;
  1862. procedure TDebugInfoDwarf.appendsym_paravar(list:TAsmList;sym:tparavarsym);
  1863. begin
  1864. appendsym_var(list,sym);
  1865. end;
  1866. procedure TDebugInfoDwarf.appendsym_fieldvar(list:TAsmList;sym: tfieldvarsym);
  1867. begin
  1868. appendsym_fieldvar_with_name_offset(list,sym,symname(sym),sym.vardef,0);
  1869. end;
  1870. procedure TDebugInfoDwarf.appendsym_fieldvar_with_name_offset(list:TAsmList;sym: tfieldvarsym;const name: string; def: tdef; offset: pint);
  1871. var
  1872. bitoffset,
  1873. fieldoffset,
  1874. fieldnatsize: aint;
  1875. begin
  1876. if (sp_static in sym.symoptions) or
  1877. (sym.visibility=vis_hidden) then
  1878. exit;
  1879. if (tabstractrecordsymtable(sym.owner).usefieldalignment<>bit_alignment) or
  1880. { only ordinals are bitpacked }
  1881. not is_ordinal(sym.vardef) then
  1882. begin
  1883. { other kinds of fields can however also appear in a bitpacked }
  1884. { record, and then their offset is also specified in bits rather }
  1885. { than in bytes }
  1886. if (tabstractrecordsymtable(sym.owner).usefieldalignment<>bit_alignment) then
  1887. fieldoffset:=sym.fieldoffset
  1888. else
  1889. fieldoffset:=sym.fieldoffset div 8;
  1890. inc(fieldoffset,offset);
  1891. append_entry(DW_TAG_member,false,[
  1892. DW_AT_name,DW_FORM_string,name+#0,
  1893. DW_AT_data_member_location,DW_FORM_block1,1+lengthuleb128(fieldoffset)
  1894. ]);
  1895. end
  1896. else
  1897. begin
  1898. if (sym.vardef.packedbitsize > 255) then
  1899. internalerror(2007061201);
  1900. { we don't bitpack according to the ABI, but as close as }
  1901. { possible, i.e., equivalent to gcc's }
  1902. { __attribute__((__packed__)), which is also what gpc }
  1903. { does. }
  1904. fieldnatsize:=max(sizeof(pint),sym.vardef.size);
  1905. fieldoffset:=(sym.fieldoffset div (fieldnatsize*8)) * fieldnatsize;
  1906. inc(fieldoffset,offset);
  1907. bitoffset:=sym.fieldoffset mod (fieldnatsize*8);
  1908. if (target_info.endian=endian_little) then
  1909. bitoffset:=(fieldnatsize*8)-bitoffset-sym.vardef.packedbitsize;
  1910. append_entry(DW_TAG_member,false,[
  1911. DW_AT_name,DW_FORM_string,symname(sym)+#0,
  1912. { gcc also generates both a bit and byte size attribute }
  1913. { we don't support ordinals >= 256 bits }
  1914. DW_AT_byte_size,DW_FORM_data1,fieldnatsize,
  1915. { nor >= 256 bits (not yet, anyway, see IE above) }
  1916. DW_AT_bit_size,DW_FORM_data1,sym.vardef.packedbitsize,
  1917. { data1 and data2 are unsigned, bitoffset can also be negative }
  1918. DW_AT_bit_offset,DW_FORM_data4,bitoffset,
  1919. DW_AT_data_member_location,DW_FORM_block1,1+lengthuleb128(fieldoffset)
  1920. ]);
  1921. end;
  1922. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_plus_uconst)));
  1923. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_uleb128bit(fieldoffset));
  1924. append_labelentry_ref(DW_AT_type,def_dwarf_lab(def));
  1925. finish_entry;
  1926. end;
  1927. procedure TDebugInfoDwarf.appendsym_const(list:TAsmList;sym:tconstsym);
  1928. var
  1929. i,
  1930. size: aint;
  1931. usedef: tdef;
  1932. begin
  1933. append_entry(DW_TAG_variable,false,[
  1934. DW_AT_name,DW_FORM_string,symname(sym)+#0
  1935. ]);
  1936. { for string constants, constdef isn't set because they have no real type }
  1937. case sym.consttyp of
  1938. conststring:
  1939. begin
  1940. { if DW_FORM_string is used below one day, this usedef should
  1941. probably become 0 }
  1942. if (sym.value.len<=255) then
  1943. usedef:=cshortstringtype
  1944. else
  1945. usedef:=clongstringtype;
  1946. end;
  1947. constresourcestring,
  1948. constwstring:
  1949. usedef:=nil;
  1950. else
  1951. usedef:=sym.constdef;
  1952. end;
  1953. if assigned(usedef) then
  1954. append_labelentry_ref(DW_AT_type,def_dwarf_lab(usedef));
  1955. current_asmdata.asmlists[al_dwarf_abbrev].concat(tai_const.create_uleb128bit(ord(DW_AT_const_value)));
  1956. case sym.consttyp of
  1957. conststring:
  1958. begin
  1959. { DW_FORM_string isn't supported yet by the Pascal value printer
  1960. -> create a string using raw bytes }
  1961. if (sym.value.len<=255) then
  1962. begin
  1963. current_asmdata.asmlists[al_dwarf_abbrev].concat(tai_const.create_uleb128bit(ord(DW_FORM_block1)));
  1964. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(sym.value.len+1));
  1965. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(sym.value.len));
  1966. end
  1967. else
  1968. begin
  1969. current_asmdata.asmlists[al_dwarf_abbrev].concat(tai_const.create_uleb128bit(ord(DW_FORM_block)));
  1970. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_uleb128bit(sym.value.len+sizeof(pint)));
  1971. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_pint(sym.value.len));
  1972. end;
  1973. i:=0;
  1974. size:=sym.value.len;
  1975. while(i<size) do
  1976. begin
  1977. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit((pbyte(sym.value.valueptr+i)^)));
  1978. inc(i);
  1979. end;
  1980. end;
  1981. constguid,
  1982. constset:
  1983. begin
  1984. current_asmdata.asmlists[al_dwarf_abbrev].concat(tai_const.create_uleb128bit(ord(DW_FORM_block1)));
  1985. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(usedef.size));
  1986. i:=0;
  1987. size:=sym.constdef.size;
  1988. while (i<size) do
  1989. begin
  1990. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit((pbyte(sym.value.valueptr+i)^)));
  1991. inc(i);
  1992. end;
  1993. end;
  1994. constwstring,
  1995. constresourcestring:
  1996. begin
  1997. { write dummy for now }
  1998. current_asmdata.asmlists[al_dwarf_abbrev].concat(tai_const.create_uleb128bit(ord(DW_FORM_string)));
  1999. current_asmdata.asmlists[al_dwarf_info].concat(tai_string.create(''));
  2000. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(0));
  2001. end;
  2002. constord:
  2003. begin
  2004. if (sym.value.valueord<0) then
  2005. begin
  2006. current_asmdata.asmlists[al_dwarf_abbrev].concat(tai_const.create_uleb128bit(ord(DW_FORM_sdata)));
  2007. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_sleb128bit(sym.value.valueord.svalue));
  2008. end
  2009. else
  2010. begin
  2011. current_asmdata.asmlists[al_dwarf_abbrev].concat(tai_const.create_uleb128bit(ord(DW_FORM_udata)));
  2012. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_uleb128bit(sym.value.valueord.uvalue));
  2013. end;
  2014. end;
  2015. constnil:
  2016. begin
  2017. {$ifdef cpu64bitaddr}
  2018. current_asmdata.asmlists[al_dwarf_abbrev].concat(tai_const.create_uleb128bit(ord(DW_FORM_data8)));
  2019. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_64bit(0));
  2020. {$else cpu64bitaddr}
  2021. current_asmdata.asmlists[al_dwarf_abbrev].concat(tai_const.create_uleb128bit(ord(DW_FORM_data4)));
  2022. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_32bit(0));
  2023. {$endif cpu64bitaddr}
  2024. end;
  2025. constpointer:
  2026. begin
  2027. {$ifdef cpu64bitaddr}
  2028. current_asmdata.asmlists[al_dwarf_abbrev].concat(tai_const.create_uleb128bit(ord(DW_FORM_data8)));
  2029. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_64bit(int64(sym.value.valueordptr)));
  2030. {$else cpu64bitaddr}
  2031. current_asmdata.asmlists[al_dwarf_abbrev].concat(tai_const.create_uleb128bit(ord(DW_FORM_data4)));
  2032. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_32bit(longint(sym.value.valueordptr)));
  2033. {$endif cpu64bitaddr}
  2034. end;
  2035. constreal:
  2036. begin
  2037. current_asmdata.asmlists[al_dwarf_abbrev].concat(tai_const.create_uleb128bit(ord(DW_FORM_block1)));
  2038. case tfloatdef(sym.constdef).floattype of
  2039. s32real:
  2040. begin
  2041. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(4));
  2042. current_asmdata.asmlists[al_dwarf_info].concat(tai_real_32bit.create(psingle(sym.value.valueptr)^));
  2043. end;
  2044. s64comp,
  2045. s64currency,
  2046. s64real:
  2047. begin
  2048. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(8));
  2049. current_asmdata.asmlists[al_dwarf_info].concat(tai_real_64bit.create(pdouble(sym.value.valueptr)^));
  2050. end;
  2051. s80real:
  2052. begin
  2053. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(10));
  2054. current_asmdata.asmlists[al_dwarf_info].concat(tai_real_80bit.create(pextended(sym.value.valueptr)^));
  2055. end;
  2056. else
  2057. internalerror(200601291);
  2058. end;
  2059. end;
  2060. else
  2061. internalerror(200601292);
  2062. end;
  2063. finish_entry;
  2064. end;
  2065. procedure TDebugInfoDwarf.appendsym_label(list:TAsmList;sym: tlabelsym);
  2066. begin
  2067. { ignore label syms for now, the problem is that a label sym
  2068. can have more than one label associated e.g. in case of
  2069. an inline procedure expansion }
  2070. end;
  2071. procedure TDebugInfoDwarf.appendsym_property(list:TAsmList;sym: tpropertysym);
  2072. var
  2073. symlist: ppropaccesslistitem;
  2074. tosym: tabstractvarsym;
  2075. offset: pint;
  2076. begin
  2077. if assigned(sym.propaccesslist[palt_read]) and
  2078. not assigned(sym.propaccesslist[palt_read].procdef) then
  2079. symlist:=sym.propaccesslist[palt_read].firstsym
  2080. else
  2081. { can't handle }
  2082. exit;
  2083. if not get_symlist_sym_offset(symlist,tosym,offset) then
  2084. exit;
  2085. if (tosym.owner.symtabletype<>objectsymtable) then
  2086. begin
  2087. if (tosym.typ=fieldvarsym) then
  2088. internalerror(2009031404);
  2089. appendsym_var_with_name_type_offset(list,tabstractnormalvarsym(tosym),symname(sym),sym.propdef,offset,false)
  2090. end
  2091. else
  2092. appendsym_fieldvar_with_name_offset(list,tfieldvarsym(tosym),symname(sym),sym.propdef,offset)
  2093. end;
  2094. procedure TDebugInfoDwarf.appendsym_type(list:TAsmList;sym: ttypesym);
  2095. begin
  2096. { just queue the def if needed, beforeappenddef will
  2097. emit the typedef if necessary }
  2098. def_dwarf_lab(sym.typedef);
  2099. end;
  2100. procedure TDebugInfoDwarf.appendsym_absolute(list:TAsmList;sym:tabsolutevarsym);
  2101. var
  2102. templist : TAsmList;
  2103. blocksize : longint;
  2104. symlist : ppropaccesslistitem;
  2105. tosym: tabstractvarsym;
  2106. offset: pint;
  2107. begin
  2108. templist:=TAsmList.create;
  2109. case tabsolutevarsym(sym).abstyp of
  2110. toaddr :
  2111. begin
  2112. { MWE: replaced ifdef i368 }
  2113. (*
  2114. if target_cpu = cpu_i386 then
  2115. begin
  2116. { in theory, we could write a DW_AT_segment entry here for sym.absseg,
  2117. however I doubt that gdb supports this (FK) }
  2118. end;
  2119. *)
  2120. templist.concat(tai_const.create_8bit(3));
  2121. templist.concat(tai_const.create_pint(sym.addroffset));
  2122. blocksize:=1+sizeof(puint);
  2123. end;
  2124. toasm :
  2125. begin
  2126. templist.concat(tai_const.create_8bit(3));
  2127. templist.concat(tai_const.createname(sym.mangledname,0));
  2128. blocksize:=1+sizeof(puint);
  2129. end;
  2130. tovar:
  2131. begin
  2132. symlist:=tabsolutevarsym(sym).ref.firstsym;
  2133. if get_symlist_sym_offset(symlist,tosym,offset) then
  2134. begin
  2135. if (tosym.typ=fieldvarsym) then
  2136. internalerror(2009031402);
  2137. appendsym_var_with_name_type_offset(list,tabstractnormalvarsym(tosym),symname(sym),tabstractvarsym(sym).vardef,offset,false);
  2138. end;
  2139. templist.free;
  2140. exit;
  2141. end;
  2142. end;
  2143. append_entry(DW_TAG_variable,false,[
  2144. DW_AT_name,DW_FORM_string,symname(sym)+#0,
  2145. {
  2146. DW_AT_decl_file,DW_FORM_data1,0,
  2147. DW_AT_decl_line,DW_FORM_data1,
  2148. }
  2149. DW_AT_external,DW_FORM_flag,true,
  2150. { data continues below }
  2151. DW_AT_location,DW_FORM_block1,blocksize
  2152. ]);
  2153. { append block data }
  2154. current_asmdata.asmlists[al_dwarf_info].concatlist(templist);
  2155. append_labelentry_ref(DW_AT_type,def_dwarf_lab(sym.vardef));
  2156. templist.free;
  2157. finish_entry;
  2158. end;
  2159. procedure TDebugInfoDwarf.beforeappendsym(list:TAsmList;sym:tsym);
  2160. begin
  2161. current_asmdata.asmlists[al_dwarf_info].concat(tai_comment.Create(strpnew('Symbol '+symname(sym))));
  2162. end;
  2163. procedure TDebugInfoDwarf.insertmoduleinfo;
  2164. var
  2165. templist: TAsmList;
  2166. linelist: TAsmList;
  2167. lbl : tasmlabel;
  2168. n,m : Integer;
  2169. ditem : TDirIndexItem;
  2170. fitem : TFileIndexItem;
  2171. flist : TFPList;
  2172. dbgname : String;
  2173. begin
  2174. { insert DEBUGSTART and DEBUGEND labels }
  2175. dbgname:=make_mangledname('DEBUGSTART',current_module.localsymtable,'');
  2176. { Darwin's linker does not like two global labels both pointing to the
  2177. end of a section, which can happen in case of units without code ->
  2178. make them local; we don't need the debugtable stuff there either,
  2179. so it doesn't matter that they are not global.
  2180. }
  2181. if (target_info.system in systems_darwin) then
  2182. dbgname:='L'+dbgname;
  2183. new_section(current_asmdata.asmlists[al_start],sec_code,dbgname,0,secorder_begin);
  2184. if not(target_info.system in systems_darwin) then
  2185. current_asmdata.asmlists[al_start].concat(tai_symbol.Createname_global(dbgname,AT_DATA,0))
  2186. else
  2187. current_asmdata.asmlists[al_start].concat(tai_symbol.Createname(dbgname,AT_DATA,0));
  2188. dbgname:=make_mangledname('DEBUGEND',current_module.localsymtable,'');
  2189. { See above. }
  2190. if (target_info.system in systems_darwin) then
  2191. dbgname:='L'+dbgname;
  2192. new_section(current_asmdata.asmlists[al_end],sec_code,dbgname,0,secorder_end);
  2193. if not(target_info.system in systems_darwin) then
  2194. current_asmdata.asmlists[al_end].concat(tai_symbol.Createname_global(dbgname,AT_DATA,0))
  2195. else
  2196. current_asmdata.asmlists[al_end].concat(tai_symbol.Createname(dbgname,AT_DATA,0));
  2197. { insert .Ldebug_abbrev0 label }
  2198. templist:=TAsmList.create;
  2199. new_section(templist,sec_debug_abbrev,'',0);
  2200. templist.concat(tai_symbol.createname(target_asm.labelprefix+'debug_abbrevsection0',AT_DATA,0));
  2201. { add any extra stuff which needs to be in the abbrev section, but before }
  2202. { the actual abbreviations, in between the symbol above and below, i.e. here }
  2203. templist.concat(tai_symbol.createname(target_asm.labelprefix+'debug_abbrev0',AT_DATA,0));
  2204. current_asmdata.asmlists[al_start].insertlist(templist);
  2205. templist.free;
  2206. { insert .Ldebug_line0 label }
  2207. templist:=TAsmList.create;
  2208. new_section(templist,sec_debug_line,'',0);
  2209. templist.concat(tai_symbol.createname(target_asm.labelprefix+'debug_linesection0',AT_DATA,0));
  2210. { add any extra stuff which needs to be in the line section, but before }
  2211. { the actual line info, in between the symbol above and below, i.e. here }
  2212. templist.concat(tai_symbol.createname(target_asm.labelprefix+'debug_line0',AT_DATA,0));
  2213. current_asmdata.asmlists[al_start].insertlist(templist);
  2214. templist.free;
  2215. { finalize line info if the unit doesn't contain any function/ }
  2216. { procedure/init/final code }
  2217. finish_lineinfo;
  2218. { debug line header }
  2219. linelist := current_asmdata.asmlists[al_dwarf_line];
  2220. new_section(linelist,sec_debug_line,'',0);
  2221. linelist.concat(tai_comment.Create(strpnew('=== header start ===')));
  2222. { size }
  2223. current_asmdata.getlabel(lbl,alt_dbgfile);
  2224. if use_64bit_headers then
  2225. linelist.concat(tai_const.create_32bit(longint($FFFFFFFF)));
  2226. linelist.concat(tai_const.create_rel_sym(offsetreltype,
  2227. lbl,current_asmdata.RefAsmSymbol(target_asm.labelprefix+'edebug_line0')));
  2228. linelist.concat(tai_label.create(lbl));
  2229. { version }
  2230. linelist.concat(tai_const.create_16bit(dwarf_version));
  2231. { header length }
  2232. current_asmdata.getlabel(lbl,alt_dbgfile);
  2233. linelist.concat(tai_const.create_rel_sym(offsetreltype,
  2234. lbl,current_asmdata.RefAsmSymbol(target_asm.labelprefix+'ehdebug_line0')));
  2235. linelist.concat(tai_label.create(lbl));
  2236. { minimum_instruction_length }
  2237. linelist.concat(tai_const.create_8bit(1));
  2238. { default_is_stmt }
  2239. linelist.concat(tai_const.create_8bit(1));
  2240. { line_base }
  2241. linelist.concat(tai_const.create_8bit(LINE_BASE));
  2242. { line_range }
  2243. { only line increase, no adress }
  2244. linelist.concat(tai_const.create_8bit(255));
  2245. { opcode_base }
  2246. linelist.concat(tai_const.create_8bit(OPCODE_BASE));
  2247. { standard_opcode_lengths }
  2248. { MWE: sigh... why adding the default lengths (and make those sizes sense with LEB encoding) }
  2249. { DW_LNS_copy }
  2250. linelist.concat(tai_const.create_8bit(0));
  2251. { DW_LNS_advance_pc }
  2252. linelist.concat(tai_const.create_8bit(1));
  2253. { DW_LNS_advance_line }
  2254. linelist.concat(tai_const.create_8bit(1));
  2255. { DW_LNS_set_file }
  2256. linelist.concat(tai_const.create_8bit(1));
  2257. { DW_LNS_set_column }
  2258. linelist.concat(tai_const.create_8bit(1));
  2259. { DW_LNS_negate_stmt }
  2260. linelist.concat(tai_const.create_8bit(0));
  2261. { DW_LNS_set_basic_block }
  2262. linelist.concat(tai_const.create_8bit(0));
  2263. { DW_LNS_const_add_pc }
  2264. linelist.concat(tai_const.create_8bit(0));
  2265. { DW_LNS_fixed_advance_pc }
  2266. linelist.concat(tai_const.create_8bit(1));
  2267. { DW_LNS_set_prologue_end }
  2268. linelist.concat(tai_const.create_8bit(0));
  2269. { DW_LNS_set_epilogue_begin }
  2270. linelist.concat(tai_const.create_8bit(0));
  2271. { DW_LNS_set_isa }
  2272. linelist.concat(tai_const.create_8bit(1));
  2273. { Create single list of filenames sorted in IndexNr }
  2274. flist:=TFPList.Create;
  2275. for n := 0 to dirlist.Count - 1 do
  2276. begin
  2277. ditem := TDirIndexItem(dirlist[n]);
  2278. for m := 0 to ditem.Files.Count - 1 do
  2279. flist.Add(ditem.Files[m]);
  2280. end;
  2281. flist.Sort(@FileListSortCompare);
  2282. { include_directories }
  2283. linelist.concat(tai_comment.Create(strpnew('include_directories')));
  2284. for n := 0 to dirlist.Count - 1 do
  2285. begin
  2286. ditem := TDirIndexItem(dirlist[n]);
  2287. if ditem.Name = '.' then
  2288. Continue;
  2289. { Write without trailing path delimiter and also don't prefix with ./ for current dir (already done while adding to dirlist }
  2290. linelist.concat(tai_string.create(ditem.Name+#0));
  2291. end;
  2292. linelist.concat(tai_const.create_8bit(0));
  2293. { file_names }
  2294. linelist.concat(tai_comment.Create(strpnew('file_names')));
  2295. for n := 0 to flist.Count - 1 do
  2296. begin
  2297. fitem := TFileIndexItem(flist[n]);
  2298. { file name }
  2299. linelist.concat(tai_string.create(fitem.Name+#0));
  2300. { directory index }
  2301. linelist.concat(tai_const.create_uleb128bit(fitem.DirIndex));
  2302. { last modification }
  2303. linelist.concat(tai_const.create_uleb128bit(0));
  2304. { file length }
  2305. linelist.concat(tai_const.create_uleb128bit(0));
  2306. end;
  2307. linelist.concat(tai_const.create_8bit(0));
  2308. { end of debug line header }
  2309. linelist.concat(tai_symbol.createname(target_asm.labelprefix+'ehdebug_line0',AT_DATA,0));
  2310. linelist.concat(tai_comment.Create(strpnew('=== header end ===')));
  2311. { add line program }
  2312. linelist.concatList(asmline);
  2313. { end of debug line table }
  2314. linelist.concat(tai_symbol.createname(target_asm.labelprefix+'edebug_line0',AT_DATA,0));
  2315. flist.free;
  2316. end;
  2317. procedure TDebugInfoDwarf.inserttypeinfo;
  2318. procedure write_defs_to_write;
  2319. var
  2320. n : integer;
  2321. looplist,
  2322. templist: TFPObjectList;
  2323. def : tdef;
  2324. begin
  2325. templist := TFPObjectList.Create(False);
  2326. looplist := deftowritelist;
  2327. while looplist.count > 0 do
  2328. begin
  2329. deftowritelist := templist;
  2330. for n := 0 to looplist.count - 1 do
  2331. begin
  2332. def := tdef(looplist[n]);
  2333. case def.dbg_state of
  2334. dbg_state_written:
  2335. continue;
  2336. dbg_state_writing:
  2337. internalerror(200610052);
  2338. dbg_state_unused:
  2339. internalerror(200610053);
  2340. dbg_state_used:
  2341. appenddef(nil,def)
  2342. else
  2343. internalerror(200610054);
  2344. end;
  2345. end;
  2346. looplist.clear;
  2347. templist := looplist;
  2348. looplist := deftowritelist;
  2349. end;
  2350. templist.free;
  2351. end;
  2352. var
  2353. storefilepos : tfileposinfo;
  2354. lenstartlabel : tasmlabel;
  2355. i : longint;
  2356. def: tdef;
  2357. dbgname: string;
  2358. begin
  2359. current_module.flags:=current_module.flags or uf_has_dwarf_debuginfo;
  2360. storefilepos:=current_filepos;
  2361. current_filepos:=current_module.mainfilepos;
  2362. currabbrevnumber:=0;
  2363. defnumberlist:=TFPObjectList.create(false);
  2364. deftowritelist:=TFPObjectList.create(false);
  2365. { not exported (FK)
  2366. FILEREC
  2367. TEXTREC
  2368. }
  2369. vardatadef:=trecorddef(search_system_type('TVARDATA').typedef);
  2370. { write start labels }
  2371. current_asmdata.asmlists[al_dwarf_info].concat(tai_section.create(sec_debug_info,'',0));
  2372. current_asmdata.asmlists[al_dwarf_info].concat(tai_symbol.createname(target_asm.labelprefix+'debug_info0',AT_DATA,0));
  2373. { start abbrev section }
  2374. new_section(current_asmdata.asmlists[al_dwarf_abbrev],sec_debug_abbrev,'',0);
  2375. { debug info header }
  2376. current_asmdata.getlabel(lenstartlabel,alt_dbgfile);
  2377. { size }
  2378. if use_64bit_headers then
  2379. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_32bit(longint($FFFFFFFF)));
  2380. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_rel_sym(offsetreltype,
  2381. lenstartlabel,current_asmdata.RefAsmSymbol(target_asm.labelprefix+'edebug_info0')));
  2382. current_asmdata.asmlists[al_dwarf_info].concat(tai_label.create(lenstartlabel));
  2383. { version }
  2384. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_16bit(dwarf_version));
  2385. { abbrev table (=relative from section start)}
  2386. if not(tf_dwarf_relative_addresses in target_info.flags) then
  2387. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_type_sym(offsetabstype,
  2388. current_asmdata.RefAsmSymbol(target_asm.labelprefix+'debug_abbrev0')))
  2389. else
  2390. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_rel_sym(offsetreltype,
  2391. current_asmdata.RefAsmSymbol(target_asm.labelprefix+'debug_abbrevsection0'),
  2392. current_asmdata.RefAsmSymbol(target_asm.labelprefix+'debug_abbrev0')));
  2393. { address size }
  2394. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(sizeof(pint)));
  2395. { first manadatory compilation unit TAG }
  2396. append_entry(DW_TAG_compile_unit,true,[
  2397. DW_AT_name,DW_FORM_string,relative_dwarf_path(current_module.sourcefiles.get_file(1).path^+current_module.sourcefiles.get_file(1).name^)+#0,
  2398. DW_AT_producer,DW_FORM_string,'Free Pascal '+full_version_string+' '+date_string+#0,
  2399. DW_AT_comp_dir,DW_FORM_string,BSToSlash(FixPath(GetCurrentDir,false))+#0,
  2400. DW_AT_language,DW_FORM_data1,DW_LANG_Pascal83,
  2401. DW_AT_identifier_case,DW_FORM_data1,DW_ID_case_insensitive]);
  2402. { reference to line info section }
  2403. if not(tf_dwarf_relative_addresses in target_info.flags) then
  2404. append_labelentry_dataptr_abs(DW_AT_stmt_list,current_asmdata.RefAsmSymbol(target_asm.labelprefix+'debug_line0'))
  2405. else
  2406. append_labelentry_dataptr_rel(DW_AT_stmt_list,
  2407. current_asmdata.RefAsmSymbol(target_asm.labelprefix+'debug_linesection0'),
  2408. current_asmdata.RefAsmSymbol(target_asm.labelprefix+'debug_line0'));
  2409. dbgname:=make_mangledname('DEBUGSTART',current_module.localsymtable,'');
  2410. if (target_info.system in systems_darwin) then
  2411. dbgname:='L'+dbgname;
  2412. append_labelentry(DW_AT_low_pc,current_asmdata.RefAsmSymbol(dbgname));
  2413. dbgname:=make_mangledname('DEBUGEND',current_module.localsymtable,'');
  2414. if (target_info.system in systems_darwin) then
  2415. dbgname:='L'+dbgname;
  2416. append_labelentry(DW_AT_high_pc,current_asmdata.RefAsmSymbol(dbgname));
  2417. finish_entry;
  2418. { write all global/local variables. This will flag all required tdefs }
  2419. if assigned(current_module.globalsymtable) then
  2420. write_symtable_syms(current_asmdata.asmlists[al_dwarf_info],current_module.globalsymtable);
  2421. if assigned(current_module.localsymtable) then
  2422. write_symtable_syms(current_asmdata.asmlists[al_dwarf_info],current_module.localsymtable);
  2423. { write all procedures and methods. This will flag all required tdefs }
  2424. if assigned(current_module.globalsymtable) then
  2425. write_symtable_procdefs(current_asmdata.asmlists[al_dwarf_info],current_module.globalsymtable);
  2426. if assigned(current_module.localsymtable) then
  2427. write_symtable_procdefs(current_asmdata.asmlists[al_dwarf_info],current_module.localsymtable);
  2428. { reset unit type info flag }
  2429. reset_unit_type_info;
  2430. { write used types from the used units }
  2431. write_used_unit_type_info(current_asmdata.asmlists[al_dwarf_info],current_module);
  2432. { last write the types from this unit }
  2433. if assigned(current_module.globalsymtable) then
  2434. write_symtable_defs(current_asmdata.asmlists[al_dwarf_info],current_module.globalsymtable);
  2435. if assigned(current_module.localsymtable) then
  2436. write_symtable_defs(current_asmdata.asmlists[al_dwarf_info],current_module.localsymtable);
  2437. { write defs not written yet }
  2438. write_defs_to_write;
  2439. { close compilation unit entry }
  2440. finish_children;
  2441. { end of debug info table }
  2442. current_asmdata.asmlists[al_dwarf_info].concat(tai_symbol.createname(target_asm.labelprefix+'edebug_info0',AT_DATA,0));
  2443. { end of abbrev table }
  2444. current_asmdata.asmlists[al_dwarf_abbrev].concat(tai_const.create_8bit(0));
  2445. { reset all def labels }
  2446. for i:=0 to defnumberlist.count-1 do
  2447. begin
  2448. def := tdef(defnumberlist[i]);
  2449. if assigned(def) then
  2450. begin
  2451. def.dwarf_lab:=nil;
  2452. def.dbg_state:=dbg_state_unused;
  2453. end;
  2454. end;
  2455. defnumberlist.free;
  2456. defnumberlist:=nil;
  2457. deftowritelist.free;
  2458. deftowritelist:=nil;
  2459. current_filepos:=storefilepos;
  2460. end;
  2461. procedure TDebugInfoDwarf.referencesections(list:TAsmList);
  2462. var
  2463. hp : tmodule;
  2464. begin
  2465. { Reference all DEBUGINFO sections from the main .fpc section }
  2466. { to prevent eliminating them by smartlinking }
  2467. if (target_info.system in ([system_powerpc_macos]+systems_darwin)) then
  2468. exit;
  2469. list.concat(Tai_section.create(sec_fpc,'links',0));
  2470. { include reference to all debuginfo sections of used units }
  2471. hp:=tmodule(loaded_units.first);
  2472. while assigned(hp) do
  2473. begin
  2474. If (hp.flags and uf_has_dwarf_debuginfo)=uf_has_dwarf_debuginfo then
  2475. begin
  2476. list.concat(Tai_const.Createname(make_mangledname('DEBUGSTART',hp.localsymtable,''),0));
  2477. list.concat(Tai_const.Createname(make_mangledname('DEBUGEND',hp.localsymtable,''),0));
  2478. end;
  2479. hp:=tmodule(hp.next);
  2480. end;
  2481. end;
  2482. function TDebugInfoDwarf.symname(sym: tsym): String;
  2483. begin
  2484. if (sym.typ=paravarsym) and
  2485. (vo_is_self in tparavarsym(sym).varoptions) then
  2486. { We use 'this' for regular methods because that's what gdb triggers
  2487. on to automatically search fields. Don't do this for class methods,
  2488. because search class fields is not supported, and gdb 7.0+ fails
  2489. in this case because "this" is not a record in that case (it's a
  2490. pointer to a vmt) }
  2491. if not(po_classmethod in tabstractprocdef(sym.owner.defowner).procoptions) then
  2492. result:='this'
  2493. else
  2494. result:='self'
  2495. else if (ds_dwarf_method_class_prefix in current_settings.debugswitches) and
  2496. (sym.typ=procsym) and
  2497. (tprocsym(sym).owner.symtabletype=objectsymtable) then
  2498. result:=tprocsym(sym).owner.name^+'__'+sym.name
  2499. else
  2500. result := sym.Name;
  2501. end;
  2502. procedure TDebugInfoDwarf.insertlineinfo(list:TAsmList);
  2503. var
  2504. currfileinfo,
  2505. lastfileinfo : tfileposinfo;
  2506. currfuncname : pshortstring;
  2507. currsectype : TAsmSectiontype;
  2508. hp, hpend : tai;
  2509. infile : tinputfile;
  2510. prevcolumn,
  2511. diffline,
  2512. prevline,
  2513. prevfileidx,
  2514. currfileidx,
  2515. nolineinfolevel : Integer;
  2516. prevlabel,
  2517. currlabel : tasmlabel;
  2518. begin
  2519. { this function will always terminate the lineinfo block }
  2520. generated_lineinfo := true;
  2521. FillChar(lastfileinfo,sizeof(lastfileinfo),0);
  2522. currfuncname:=nil;
  2523. currsectype:=sec_code;
  2524. hp:=Tai(list.first);
  2525. prevcolumn := 0;
  2526. prevline := 1;
  2527. prevfileidx := 1;
  2528. prevlabel := nil;
  2529. nolineinfolevel:=0;
  2530. while assigned(hp) do
  2531. begin
  2532. case hp.typ of
  2533. ait_section :
  2534. currsectype:=tai_section(hp).sectype;
  2535. ait_function_name :
  2536. begin
  2537. currfuncname:=tai_function_name(hp).funcname;
  2538. asmline.concat(tai_comment.Create(strpnew('function: '+currfuncname^)));
  2539. end;
  2540. ait_force_line :
  2541. begin
  2542. lastfileinfo.line:=-1;
  2543. end;
  2544. ait_marker :
  2545. begin
  2546. case tai_marker(hp).kind of
  2547. mark_NoLineInfoStart:
  2548. inc(nolineinfolevel);
  2549. mark_NoLineInfoEnd:
  2550. dec(nolineinfolevel);
  2551. end;
  2552. end;
  2553. end;
  2554. if (currsectype=sec_code) and
  2555. (hp.typ=ait_instruction) and
  2556. (nolineinfolevel=0) then
  2557. begin
  2558. currfileinfo:=tailineinfo(hp).fileinfo;
  2559. { file changed ? (must be before line info) }
  2560. if (currfileinfo.fileindex<>0) and
  2561. ((lastfileinfo.fileindex<>currfileinfo.fileindex) or
  2562. (lastfileinfo.moduleindex<>currfileinfo.moduleindex)) then
  2563. begin
  2564. infile:=get_module(currfileinfo.moduleindex).sourcefiles.get_file(currfileinfo.fileindex);
  2565. if assigned(infile) then
  2566. begin
  2567. currfileidx := get_file_index(infile);
  2568. if prevfileidx <> currfileidx then
  2569. begin
  2570. list.insertbefore(tai_comment.Create(strpnew('path: '+infile.path^)), hp);
  2571. list.insertbefore(tai_comment.Create(strpnew('file: '+infile.name^)), hp);
  2572. list.insertbefore(tai_comment.Create(strpnew('indx: '+tostr(currfileidx))), hp);
  2573. { set file }
  2574. asmline.concat(tai_comment.Create(strpnew('path: '+infile.path^)));
  2575. asmline.concat(tai_comment.Create(strpnew('file: '+infile.name^)));
  2576. asmline.concat(tai_const.create_8bit(DW_LNS_set_file));
  2577. asmline.concat(tai_const.create_uleb128bit(currfileidx));
  2578. prevfileidx := currfileidx;
  2579. end;
  2580. { force new line info }
  2581. lastfileinfo.line:=-1;
  2582. end;
  2583. end;
  2584. { line changed ? }
  2585. if (lastfileinfo.line<>currfileinfo.line) and (currfileinfo.line<>0) then
  2586. begin
  2587. { set address }
  2588. current_asmdata.getlabel(currlabel, alt_dbgline);
  2589. list.insertbefore(tai_label.create(currlabel), hp);
  2590. asmline.concat(tai_comment.Create(strpnew('['+tostr(currfileinfo.line)+':'+tostr(currfileinfo.column)+']')));
  2591. if (prevlabel = nil) or
  2592. { darwin's assembler cannot create an uleb128 of the difference }
  2593. { between to symbols }
  2594. (target_info.system in systems_darwin) then
  2595. begin
  2596. asmline.concat(tai_const.create_8bit(DW_LNS_extended_op));
  2597. asmline.concat(tai_const.create_uleb128bit(1+sizeof(pint)));
  2598. asmline.concat(tai_const.create_8bit(DW_LNE_set_address));
  2599. asmline.concat(tai_const.create_sym(currlabel));
  2600. end
  2601. else
  2602. begin
  2603. asmline.concat(tai_const.create_8bit(DW_LNS_advance_pc));
  2604. asmline.concat(tai_const.create_rel_sym(aitconst_uleb128bit, prevlabel, currlabel));
  2605. end;
  2606. prevlabel := currlabel;
  2607. { set column }
  2608. if prevcolumn <> currfileinfo.column then
  2609. begin
  2610. asmline.concat(tai_const.create_8bit(DW_LNS_set_column));
  2611. asmline.concat(tai_const.create_uleb128bit(currfileinfo.column));
  2612. prevcolumn := currfileinfo.column;
  2613. end;
  2614. { set line }
  2615. diffline := currfileinfo.line - prevline;
  2616. if (diffline >= LINE_BASE) and (OPCODE_BASE + diffline - LINE_BASE <= 255) then
  2617. begin
  2618. { use special opcode, this also adds a row }
  2619. asmline.concat(tai_const.create_8bit(OPCODE_BASE + diffline - LINE_BASE));
  2620. end
  2621. else
  2622. begin
  2623. if diffline <> 0 then
  2624. begin
  2625. asmline.concat(tai_const.create_8bit(DW_LNS_advance_line));
  2626. asmline.concat(tai_const.create_sleb128bit(diffline));
  2627. end;
  2628. { no row added yet, do it manually }
  2629. asmline.concat(tai_const.create_8bit(DW_LNS_copy));
  2630. end;
  2631. prevline := currfileinfo.line;
  2632. end;
  2633. lastfileinfo:=currfileinfo;
  2634. end;
  2635. hpend:=hp;
  2636. hp:=tai(hp.next);
  2637. end;
  2638. if assigned(hpend) then
  2639. begin
  2640. { set address for end (see appendix 3 of dwarf 2 specs) }
  2641. current_asmdata.getlabel(currlabel, alt_dbgline);
  2642. list.insertafter(tai_label.create(currlabel), hpend);
  2643. asmline.concat(tai_const.create_8bit(DW_LNS_extended_op));
  2644. asmline.concat(tai_const.create_uleb128bit(1+sizeof(pint)));
  2645. asmline.concat(tai_const.create_8bit(DW_LNE_set_address));
  2646. asmline.concat(tai_const.create_sym(currlabel));
  2647. end;
  2648. { end sequence }
  2649. asmline.concat(tai_const.Create_8bit(DW_LNS_extended_op));
  2650. asmline.concat(tai_const.Create_8bit(1));
  2651. asmline.concat(tai_const.Create_8bit(DW_LNE_end_sequence));
  2652. asmline.concat(tai_comment.Create(strpnew('###################')));
  2653. end;
  2654. procedure TDebugInfoDwarf.finish_lineinfo;
  2655. var
  2656. infile: tinputfile;
  2657. begin
  2658. { only needed if no line info at all has been generated }
  2659. if generated_lineinfo then
  2660. begin
  2661. { reset for next module compilation }
  2662. generated_lineinfo:=false;
  2663. exit;
  2664. end;
  2665. { at least the Darwin linker is annoyed if you do not }
  2666. { finish the lineinfo section, or if it doesn't }
  2667. { contain at least one file name and set_address }
  2668. infile:=current_module.sourcefiles.get_file(1);
  2669. if not assigned(infile) then
  2670. internalerror(2006020211);
  2671. asmline.concat(tai_const.create_8bit(DW_LNS_set_file));
  2672. asmline.concat(tai_const.create_uleb128bit(get_file_index(infile)));
  2673. asmline.concat(tai_const.create_8bit(DW_LNS_extended_op));
  2674. asmline.concat(tai_const.create_uleb128bit(1+sizeof(pint)));
  2675. asmline.concat(tai_const.create_8bit(DW_LNE_set_address));
  2676. asmline.concat(tai_const.create_sym(nil));
  2677. asmline.concat(tai_const.create_8bit(DW_LNS_extended_op));
  2678. asmline.concat(tai_const.Create_8bit(1));
  2679. asmline.concat(tai_const.Create_8bit(DW_LNE_end_sequence));
  2680. asmline.concat(tai_comment.Create(strpnew('###################')));
  2681. end;
  2682. {****************************************************************************
  2683. TDebugInfoDwarf2
  2684. ****************************************************************************}
  2685. procedure TDebugInfoDwarf2.appenddef_file(list:TAsmList;def: tfiledef);
  2686. begin
  2687. { gdb 6.4 doesn't support files so far so we use some fake recorddef
  2688. file recs. are less than 1k so using data2 is enough }
  2689. if assigned(def.typesym) then
  2690. append_entry(DW_TAG_structure_type,false,[
  2691. DW_AT_name,DW_FORM_string,symname(def.typesym)+#0,
  2692. DW_AT_byte_size,DW_FORM_udata,def.size
  2693. ])
  2694. else
  2695. append_entry(DW_TAG_structure_type,false,[
  2696. DW_AT_byte_size,DW_FORM_udata,def.size
  2697. ]);
  2698. finish_entry;
  2699. end;
  2700. procedure TDebugInfoDwarf2.appenddef_formal(list:TAsmList;def: tformaldef);
  2701. begin
  2702. { gdb 6.4 doesn't support DW_TAG_unspecified_type so we
  2703. replace it with a unsigned type with size 0 (FK)
  2704. }
  2705. append_entry(DW_TAG_base_type,false,[
  2706. DW_AT_name,DW_FORM_string,'FormalDef'#0,
  2707. DW_AT_encoding,DW_FORM_data1,DW_ATE_unsigned,
  2708. DW_AT_byte_size,DW_FORM_data1,0
  2709. ]);
  2710. finish_entry;
  2711. end;
  2712. procedure TDebugInfoDwarf2.appenddef_object(list:TAsmList;def: tobjectdef);
  2713. procedure doappend;
  2714. begin
  2715. if assigned(def.objname) then
  2716. append_entry(DW_TAG_structure_type,true,[
  2717. DW_AT_name,DW_FORM_string,def.objname^+#0,
  2718. DW_AT_byte_size,DW_FORM_udata,tobjectsymtable(def.symtable).datasize
  2719. ])
  2720. else
  2721. append_entry(DW_TAG_structure_type,true,[
  2722. DW_AT_byte_size,DW_FORM_udata,tobjectsymtable(def.symtable).datasize
  2723. ]);
  2724. finish_entry;
  2725. if assigned(def.childof) then
  2726. begin
  2727. append_entry(DW_TAG_inheritance,false,[
  2728. DW_AT_accessibility,DW_FORM_data1,DW_ACCESS_public,
  2729. DW_AT_data_member_location,DW_FORM_block1,1+lengthuleb128(0)
  2730. ]);
  2731. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_plus_uconst)));
  2732. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_uleb128bit(0));
  2733. if (def.childof.dbg_state=dbg_state_unused) then
  2734. def.childof.dbg_state:=dbg_state_used;
  2735. if is_class_or_interface_or_dispinterface(def) then
  2736. append_labelentry_ref(DW_AT_type,def_dwarf_class_struct_lab(def.childof))
  2737. else
  2738. append_labelentry_ref(DW_AT_type,def_dwarf_lab(def.childof));
  2739. finish_entry;
  2740. end;
  2741. if (oo_has_vmt in def.objectoptions) and
  2742. (not assigned(def.childof) or
  2743. not(oo_has_vmt in def.childof.objectoptions)) then
  2744. begin
  2745. { vmt field }
  2746. append_entry(DW_TAG_member,false,[
  2747. DW_AT_artificial,DW_FORM_flag,true,
  2748. DW_AT_name,DW_FORM_string,'_vptr$'+def.objname^+#0,
  2749. DW_AT_data_member_location,DW_FORM_block1,1+lengthuleb128(def.vmt_offset)
  2750. ]);
  2751. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_plus_uconst)));
  2752. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_uleb128bit(def.vmt_offset));
  2753. { should be changed into a pointer to a function returning an }
  2754. { int and with TAG_unspecified_parameters }
  2755. if (voidpointertype.dbg_state=dbg_state_unused) then
  2756. voidpointertype.dbg_state:=dbg_state_used;
  2757. append_labelentry_ref(DW_AT_type,def_dwarf_lab(voidpointertype));
  2758. finish_entry;
  2759. end;
  2760. def.symtable.symList.ForEachCall(@enum_membersyms_callback,nil);
  2761. { Write the methods in the scope of the class/object. }
  2762. write_symtable_procdefs(current_asmdata.asmlists[al_dwarf_info],def.symtable);
  2763. finish_children;
  2764. end;
  2765. begin
  2766. case def.objecttype of
  2767. odt_cppclass,
  2768. odt_object:
  2769. doappend;
  2770. odt_interfacecom,
  2771. odt_interfacecorba,
  2772. odt_dispinterface,
  2773. odt_class:
  2774. begin
  2775. { implicit pointer }
  2776. append_entry(DW_TAG_pointer_type,false,[]);
  2777. append_labelentry_ref(DW_AT_type,def_dwarf_class_struct_lab(def));
  2778. finish_entry;
  2779. if not(tf_dwarf_only_local_labels in target_info.flags) then
  2780. current_asmdata.asmlists[al_dwarf_info].concat(tai_symbol.create_global(def_dwarf_class_struct_lab(def),0))
  2781. else
  2782. current_asmdata.asmlists[al_dwarf_info].concat(tai_symbol.create(def_dwarf_class_struct_lab(def),0));
  2783. doappend;
  2784. end;
  2785. else
  2786. internalerror(200602041);
  2787. end;
  2788. end;
  2789. procedure TDebugInfoDwarf2.appenddef_set_intern(list:TAsmList;def: tsetdef; force_tag_set: boolean);
  2790. var
  2791. lab: tasmlabel;
  2792. begin
  2793. if force_tag_set or
  2794. (ds_dwarf_sets in current_settings.debugswitches) then
  2795. begin
  2796. { current (20070704 -- patch was committed on 20060513) gdb cvs supports set types }
  2797. if assigned(def.typesym) then
  2798. append_entry(DW_TAG_set_type,false,[
  2799. DW_AT_name,DW_FORM_string,symname(def.typesym)+#0,
  2800. DW_AT_byte_size,DW_FORM_data2,def.size
  2801. ])
  2802. else
  2803. append_entry(DW_TAG_set_type,false,[
  2804. DW_AT_byte_size,DW_FORM_data2,def.size
  2805. ]);
  2806. if assigned(def.elementdef) then
  2807. begin
  2808. if not(tf_dwarf_only_local_labels in target_info.flags) then
  2809. current_asmdata.getdatalabel(lab)
  2810. else
  2811. current_asmdata.getaddrlabel(lab);
  2812. append_labelentry_ref(DW_AT_type,lab);
  2813. finish_entry;
  2814. current_asmdata.asmlists[al_dwarf_info].concat(tai_symbol.create(lab,0));
  2815. { Sets of e.g. [1..5] are actually stored as a set of [0..7],
  2816. so write the exact boundaries of the set here. Let's hope no
  2817. debugger ever rejects this because this "subrange" type can
  2818. actually have a larger range than the original one. }
  2819. append_entry(DW_TAG_subrange_type,false,[
  2820. DW_AT_lower_bound,DW_FORM_sdata,def.setbase,
  2821. DW_AT_upper_bound,DW_FORM_sdata,get_max_value(def.elementdef).svalue
  2822. ]);
  2823. append_labelentry_ref(DW_AT_type,def_dwarf_lab(def.elementdef))
  2824. end
  2825. end
  2826. else
  2827. begin
  2828. { gdb versions which don't support sets refuse to load the debug }
  2829. { info of modules that contain set tags }
  2830. if assigned(def.typesym) then
  2831. append_entry(DW_TAG_base_type,false,[
  2832. DW_AT_name,DW_FORM_string,symname(def.typesym)+#0,
  2833. DW_AT_encoding,DW_FORM_data1,DW_ATE_unsigned,
  2834. DW_AT_byte_size,DW_FORM_data2,def.size
  2835. ])
  2836. else
  2837. append_entry(DW_TAG_base_type,false,[
  2838. DW_AT_encoding,DW_FORM_data1,DW_ATE_unsigned,
  2839. DW_AT_byte_size,DW_FORM_data2,def.size
  2840. ]);
  2841. end;
  2842. finish_entry;
  2843. end;
  2844. procedure TDebugInfoDwarf2.appenddef_set(list:TAsmList;def: tsetdef);
  2845. begin
  2846. appenddef_set_intern(list,def,false);
  2847. end;
  2848. procedure TDebugInfoDwarf2.appenddef_undefined(list:TAsmList;def: tundefineddef);
  2849. begin
  2850. { gdb 6.4 doesn't support DW_TAG_unspecified_type so we
  2851. replace it with a unsigned type with size 0 (FK)
  2852. }
  2853. append_entry(DW_TAG_base_type,false,[
  2854. DW_AT_name,DW_FORM_string,'FormalDef'#0,
  2855. DW_AT_encoding,DW_FORM_data1,DW_ATE_unsigned,
  2856. DW_AT_byte_size,DW_FORM_data1,0
  2857. ]);
  2858. finish_entry;
  2859. end;
  2860. procedure TDebugInfoDwarf2.appenddef_variant(list:TAsmList;def: tvariantdef);
  2861. begin
  2862. { variants aren't known to dwarf2 but writting tvardata should be enough }
  2863. appenddef_record_named(list,trecorddef(vardatadef),'Variant');
  2864. end;
  2865. function TDebugInfoDwarf2.dwarf_version: Word;
  2866. begin
  2867. Result:=2;
  2868. end;
  2869. {****************************************************************************
  2870. TDebugInfoDwarf3
  2871. ****************************************************************************}
  2872. procedure TDebugInfoDwarf3.append_labelentry_addr_ref(attr : tdwarf_attribute;sym : tasmsymbol);
  2873. begin
  2874. current_asmdata.asmlists[al_dwarf_abbrev].concat(tai_const.create_uleb128bit(ord(DW_FORM_ref_addr)));
  2875. { Since Dwarf 3 the length of a DW_FORM_ref_addr entry is not dependent on the pointer size of the
  2876. target platform, but on the used Dwarf-format (32 bit or 64 bit) for the current compilation section. }
  2877. if use_64bit_headers then
  2878. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.Create_type_sym(aitconst_64bit,sym))
  2879. else
  2880. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.Create_type_sym(aitconst_32bit,sym));
  2881. end;
  2882. procedure tdebuginfodwarf3.appenddef_array(list: tasmlist; def: tarraydef);
  2883. begin
  2884. if not is_dynamic_array(def) then
  2885. begin
  2886. inherited appenddef_array(list,def);
  2887. exit;
  2888. end;
  2889. if assigned(def.typesym) then
  2890. append_entry(DW_TAG_array_type,true,[
  2891. DW_AT_name,DW_FORM_string,symname(def.typesym)+#0,
  2892. DW_AT_data_location,DW_FORM_block1,2
  2893. ])
  2894. else
  2895. append_entry(DW_TAG_array_type,true,[
  2896. DW_AT_data_location,DW_FORM_block1,2
  2897. ]);
  2898. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_push_object_address)));
  2899. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_deref)));
  2900. append_block1(DW_AT_allocated,2);
  2901. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_push_object_address)));
  2902. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_deref)));
  2903. append_labelentry_ref(DW_AT_type,def_dwarf_lab(def.elementdef));
  2904. finish_entry;
  2905. { to simplify things, we don't write a multidimensional array here }
  2906. append_entry(DW_TAG_subrange_type,false,[
  2907. DW_AT_byte_stride,DW_FORM_udata,def.elesize,
  2908. DW_AT_lower_bound,DW_FORM_udata,0,
  2909. DW_AT_upper_bound,DW_FORM_block1,5
  2910. ]);
  2911. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_push_object_address)));
  2912. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_deref)));
  2913. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_lit0)+sizeof(ptrint)));
  2914. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_minus)));
  2915. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_deref)));
  2916. append_labelentry_ref(DW_AT_type,def_dwarf_lab(def.rangedef));
  2917. finish_entry;
  2918. finish_children;
  2919. end;
  2920. procedure tdebuginfodwarf3.appenddef_string(list: tasmlist; def: tstringdef);
  2921. procedure addstringdef(const name: shortstring; chardef: tdef; deref: boolean; lensize: aint);
  2922. var
  2923. upperopcodes: longint;
  2924. begin
  2925. { deref=true -> ansi/unicde/widestring; deref = false -> short/longstring }
  2926. if assigned(def.typesym) then
  2927. append_entry(DW_TAG_array_type,true,[
  2928. DW_AT_name,DW_FORM_string,name+#0,
  2929. DW_AT_data_location,DW_FORM_block1,2+ord(not(deref))
  2930. ])
  2931. else
  2932. append_entry(DW_TAG_array_type,true,[
  2933. DW_AT_data_location,DW_FORM_block1,2+ord(not(deref))
  2934. ]);
  2935. { in all cases we start with the address of the string }
  2936. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_push_object_address)));
  2937. if deref then
  2938. begin
  2939. { ansi/unicode/widestring -> dereference the address of the string, and then
  2940. we point to address of the string
  2941. }
  2942. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_deref)));
  2943. { also add how to detect whether or not the string is allocated: if the pointer is 0
  2944. then it isn't, otherwise it is
  2945. }
  2946. append_block1(DW_AT_allocated,2);
  2947. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_push_object_address)));
  2948. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_deref)));
  2949. end
  2950. else
  2951. begin
  2952. { shortstring characters begin at string[1], so add one to the string's address }
  2953. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_lit0)+lensize));
  2954. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_plus)))
  2955. end;
  2956. { reference to the element type of the string }
  2957. append_labelentry_ref(DW_AT_type,def_dwarf_lab(chardef));
  2958. finish_entry;
  2959. { now the information about the length of the string }
  2960. if deref then
  2961. begin
  2962. if (chardef.size=1) then
  2963. upperopcodes:=5
  2964. else
  2965. upperopcodes:=7;
  2966. { lower bound is always 1, upper bound (length) needs to be calculated }
  2967. append_entry(DW_TAG_subrange_type,false,[
  2968. DW_AT_lower_bound,DW_FORM_udata,1,
  2969. DW_AT_upper_bound,DW_FORM_block1,upperopcodes
  2970. ]);
  2971. { high(string) is stored sizeof(ptrint) bytes before the string data }
  2972. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_push_object_address)));
  2973. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_deref)));
  2974. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_lit0)+sizeof(ptrint)));
  2975. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_minus)));
  2976. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_deref)));
  2977. { for widestrings, the length is specified in bytes, so divide by two }
  2978. if (upperopcodes=7) then
  2979. begin
  2980. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_lit1)));
  2981. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_shra)));
  2982. end;
  2983. end
  2984. else
  2985. begin
  2986. append_entry(DW_TAG_subrange_type,false,[
  2987. DW_AT_lower_bound,DW_FORM_udata,1,
  2988. DW_AT_upper_bound,DW_FORM_block1,3
  2989. ]);
  2990. { for shortstrings, the length is the first byte of the string }
  2991. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_push_object_address)));
  2992. { load 1 byte }
  2993. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_deref_size)));
  2994. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(lensize));
  2995. end;
  2996. finish_entry;
  2997. finish_children;
  2998. end;
  2999. begin
  3000. case def.stringtype of
  3001. st_shortstring:
  3002. begin
  3003. addstringdef('ShortString',cchartype,false,1);
  3004. end;
  3005. st_longstring:
  3006. begin
  3007. {$ifdef cpu64bitaddr}
  3008. addstringdef('LongString',cchartype,false,8);
  3009. {$else cpu64bitaddr}
  3010. addstringdef('LongString',cchartype,false,4);
  3011. {$endif cpu64bitaddr}
  3012. end;
  3013. st_ansistring:
  3014. begin
  3015. addstringdef('AnsiString',cchartype,true,-1);
  3016. end;
  3017. st_unicodestring:
  3018. begin
  3019. addstringdef('UnicodeString',cwidechartype,true,-1);
  3020. end;
  3021. st_widestring:
  3022. begin
  3023. if not(tf_winlikewidestring in target_info.flags) then
  3024. addstringdef('WideString',cwidechartype,true,-1)
  3025. else
  3026. begin
  3027. { looks like a pwidechar (no idea about length location) }
  3028. append_entry(DW_TAG_pointer_type,false,[]);
  3029. append_labelentry_ref(DW_AT_type,def_dwarf_lab(cwidechartype));
  3030. finish_entry;
  3031. end;
  3032. end;
  3033. end;
  3034. end;
  3035. procedure TDebugInfoDwarf3.appenddef_file(list:TAsmList;def: tfiledef);
  3036. begin
  3037. if assigned(def.typesym) then
  3038. append_entry(DW_TAG_file_type,false,[
  3039. DW_AT_name,DW_FORM_string,symname(def.typesym)+#0,
  3040. DW_AT_byte_size,DW_FORM_data2,def.size
  3041. ])
  3042. else
  3043. append_entry(DW_TAG_file_type,false,[
  3044. DW_AT_byte_size,DW_FORM_data2,def.size
  3045. ]);
  3046. if tfiledef(def).filetyp=ft_typed then
  3047. append_labelentry_ref(DW_AT_type,def_dwarf_lab(tfiledef(def).typedfiledef));
  3048. finish_entry;
  3049. end;
  3050. procedure TDebugInfoDwarf3.appenddef_formal(list:TAsmList;def: tformaldef);
  3051. begin
  3052. append_entry(DW_TAG_unspecified_type,false,[
  3053. ]);
  3054. finish_entry;
  3055. end;
  3056. procedure TDebugInfoDwarf3.appenddef_object(list:TAsmList;def: tobjectdef);
  3057. procedure dostruct(tag: tdwarf_tag);
  3058. begin
  3059. if assigned(def.objname) then
  3060. append_entry(tag,true,[
  3061. DW_AT_name,DW_FORM_string,def.objrealname^+#0,
  3062. DW_AT_byte_size,DW_FORM_udata,def.size
  3063. ])
  3064. else
  3065. append_entry(DW_TAG_structure_type,true,[
  3066. DW_AT_byte_size,DW_FORM_udata,def.size
  3067. ]);
  3068. finish_entry;
  3069. end;
  3070. procedure doimplicitpointer;
  3071. var
  3072. obj : tasmlabel;
  3073. begin
  3074. if not(tf_dwarf_only_local_labels in target_info.flags) then
  3075. current_asmdata.getdatalabel(obj)
  3076. else
  3077. current_asmdata.getaddrlabel(obj);
  3078. { implicit pointer }
  3079. append_entry(DW_TAG_pointer_type,false,[]);
  3080. append_labelentry_ref(DW_AT_type,obj);
  3081. finish_entry;
  3082. current_asmdata.asmlists[al_dwarf_info].concat(tai_symbol.create(obj,0));
  3083. end;
  3084. procedure doparent(isinterface: boolean);
  3085. begin
  3086. if not assigned(def.childof) then
  3087. exit;
  3088. if isinterface then
  3089. begin
  3090. append_entry(DW_TAG_inheritance,false,[]);
  3091. end
  3092. else
  3093. begin
  3094. append_entry(DW_TAG_inheritance,false,[
  3095. DW_AT_accessibility,DW_FORM_data1,DW_ACCESS_public,
  3096. DW_AT_data_member_location,DW_FORM_block1,1+lengthuleb128(0)
  3097. ]);
  3098. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_plus_uconst)));
  3099. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_uleb128bit(0));
  3100. end;
  3101. append_labelentry_ref(DW_AT_type,def_dwarf_lab(def.childof));
  3102. finish_entry;
  3103. end;
  3104. var
  3105. n: integer;
  3106. begin
  3107. case def.objecttype of
  3108. odt_cppclass,
  3109. odt_object:
  3110. begin
  3111. dostruct(DW_TAG_structure_type);
  3112. doparent(false);
  3113. end;
  3114. odt_interfacecom,
  3115. odt_interfacecorba,
  3116. odt_dispinterface:
  3117. begin
  3118. dostruct(DW_TAG_interface_type);
  3119. doparent(true);
  3120. end;
  3121. odt_class:
  3122. begin
  3123. { not sure if the implicit pointer is needed for tag_class (MWE)}
  3124. {
  3125. doimplicitpointer;
  3126. }
  3127. dostruct(DW_TAG_class_type);
  3128. doparent(false);
  3129. end;
  3130. else
  3131. internalerror(200609171);
  3132. end;
  3133. { add implemented interfaces }
  3134. if assigned(def.ImplementedInterfaces) then
  3135. for n := 0 to def.ImplementedInterfaces.count-1 do
  3136. begin
  3137. append_entry(DW_TAG_inheritance,false,[]);
  3138. append_labelentry_ref(DW_AT_type,def_dwarf_lab(TImplementedInterface(def.ImplementedInterfaces[n]).IntfDef));
  3139. finish_entry;
  3140. end;
  3141. { add members }
  3142. def.symtable.symList.ForEachCall(@enum_membersyms_callback,nil);
  3143. finish_children;
  3144. end;
  3145. procedure TDebugInfoDwarf3.appenddef_set(list:TAsmList;def: tsetdef);
  3146. begin
  3147. appenddef_set_intern(list,def,true);
  3148. end;
  3149. procedure TDebugInfoDwarf3.appenddef_undefined(list:TAsmList;def: tundefineddef);
  3150. begin
  3151. { ??? can a undefined def have a typename ? }
  3152. if assigned(def.typesym) then
  3153. append_entry(DW_TAG_unspecified_type,false,[
  3154. DW_AT_name,DW_FORM_string,symname(def.typesym)+#0
  3155. ])
  3156. else
  3157. append_entry(DW_TAG_unspecified_type,false,[
  3158. ]);
  3159. finish_entry;
  3160. end;
  3161. procedure TDebugInfoDwarf3.appenddef_variant(list:TAsmList;def: tvariantdef);
  3162. const
  3163. VARIANTS: array[1..27] of record Value: Word; Name: String end = (
  3164. (value:0; name:''),
  3165. (value:1; name:''),
  3166. (value:2; name:'VSMALLINT'),
  3167. (value:3; name:'VINTEGER'),
  3168. (value:4; name:'VSINGLE'),
  3169. (value:5; name:'VDOUBLE'),
  3170. (value:6; name:'VCURRENCY'),
  3171. (value:7; name:'VDATE'),
  3172. (value:8; name:'VOLESTR'),
  3173. (value:9; name:'VDISPATCH'),
  3174. (value:10; name:'VERROR'),
  3175. (value:11; name:'VBOOLEAN'),
  3176. (value:12; name:''),
  3177. (value:13; name:'VUNKNOWN'),
  3178. (value:14; name:''),
  3179. (value:16; name:'VSHORTINT'),
  3180. (value:17; name:'VBYTE'),
  3181. (value:18; name:'VWORD'),
  3182. (value:19; name:'VLONGWORD'),
  3183. (value:20; name:'VINT64'),
  3184. (value:21; name:'VQWORD'),
  3185. (value:36; name:'VRECORD'),
  3186. (value:$48; name:''),
  3187. (value:$100; name:'VSTRING'),
  3188. (value:$101; name:'VANY'),
  3189. (value:$2000; name:'VARRAY'),
  3190. (value:$4000; name:'VPOINTER')
  3191. );
  3192. var
  3193. fs: tfieldvarsym;
  3194. lbl: tasmlabel;
  3195. idx: integer;
  3196. begin
  3197. { it could be done with DW_TAG_variant for the union part (if that info was available)
  3198. now we do it manually for variants (MWE) }
  3199. { struct }
  3200. append_entry(DW_TAG_structure_type,true,[
  3201. DW_AT_name,DW_FORM_string,'Variant'#0,
  3202. DW_AT_byte_size,DW_FORM_udata,vardatadef.size
  3203. ]);
  3204. finish_entry;
  3205. append_entry(DW_TAG_variant_part,true,[
  3206. ]);
  3207. current_asmdata.getaddrlabel(lbl);
  3208. append_labelentry_ref(DW_AT_discr,lbl);
  3209. finish_entry;
  3210. { discriminant }
  3211. fs := tfieldvarsym(vardatadef.symtable.Find('VTYPE'));
  3212. if (fs = nil) or (fs.typ <> fieldvarsym) then
  3213. internalerror(200609271);
  3214. current_asmdata.asmlists[al_dwarf_info].concat(tai_symbol.create(lbl,0));
  3215. appendsym_fieldvar(list,fs);
  3216. { variants }
  3217. for idx := Low(VARIANTS) to High(VARIANTS) do
  3218. begin
  3219. append_entry(DW_TAG_variant,true,[
  3220. DW_AT_discr_value,DW_FORM_udata,VARIANTS[idx].value
  3221. ]);
  3222. finish_entry;
  3223. if VARIANTS[idx].name <> '' then
  3224. begin
  3225. fs := tfieldvarsym(vardatadef.symtable.Find(VARIANTS[idx].name));
  3226. if (fs = nil) or (fs.typ <> fieldvarsym) then
  3227. internalerror(20060927200+idx);
  3228. appendsym_fieldvar(list,fs);
  3229. end;
  3230. finish_children; { variant }
  3231. end;
  3232. finish_children; { variant part }
  3233. finish_children; { struct }
  3234. end;
  3235. function TDebugInfoDwarf3.dwarf_version: Word;
  3236. begin
  3237. Result:=3;
  3238. end;
  3239. function TDebugInfoDwarf3.symname(sym: tsym): String;
  3240. begin
  3241. Result:=sym.realname;
  3242. end;
  3243. {****************************************************************************
  3244. ****************************************************************************}
  3245. const
  3246. dbg_dwarf2_info : tdbginfo =
  3247. (
  3248. id : dbg_dwarf2;
  3249. idtxt : 'DWARF2';
  3250. );
  3251. dbg_dwarf3_info : tdbginfo =
  3252. (
  3253. id : dbg_dwarf3;
  3254. idtxt : 'DWARF3';
  3255. );
  3256. initialization
  3257. RegisterDebugInfo(dbg_dwarf2_info,TDebugInfoDwarf2);
  3258. RegisterDebugInfo(dbg_dwarf3_info,TDebugInfoDwarf3);
  3259. end.