dbgdwarf.pas 134 KB

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