dbgdwarf.pas 146 KB

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