dbgdwarf.pas 117 KB

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