dbgdwarf.pas 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892
  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,
  32. symbase,symtype,symdef,
  33. DbgBase;
  34. type
  35. { Tag names and codes. }
  36. tdwarf_tag = (DW_TAG_padding := $00,DW_TAG_array_type := $01,
  37. DW_TAG_class_type := $02,DW_TAG_entry_point := $03,
  38. DW_TAG_enumeration_type := $04,DW_TAG_formal_parameter := $05,
  39. DW_TAG_imported_declaration := $08,DW_TAG_label := $0a,
  40. DW_TAG_lexical_block := $0b,DW_TAG_member := $0d,
  41. DW_TAG_pointer_type := $0f,DW_TAG_reference_type := $10,
  42. DW_TAG_compile_unit := $11,DW_TAG_string_type := $12,
  43. DW_TAG_structure_type := $13,DW_TAG_subroutine_type := $15,
  44. DW_TAG_typedef := $16,DW_TAG_union_type := $17,
  45. DW_TAG_unspecified_parameters := $18,
  46. DW_TAG_variant := $19,DW_TAG_common_block := $1a,
  47. DW_TAG_common_inclusion := $1b,DW_TAG_inheritance := $1c,
  48. DW_TAG_inlined_subroutine := $1d,DW_TAG_module := $1e,
  49. DW_TAG_ptr_to_member_type := $1f,DW_TAG_set_type := $20,
  50. DW_TAG_subrange_type := $21,DW_TAG_with_stmt := $22,
  51. DW_TAG_access_declaration := $23,DW_TAG_base_type := $24,
  52. DW_TAG_catch_block := $25,DW_TAG_const_type := $26,
  53. DW_TAG_constant := $27,DW_TAG_enumerator := $28,
  54. DW_TAG_file_type := $29,DW_TAG_friend := $2a,
  55. DW_TAG_namelist := $2b,DW_TAG_namelist_item := $2c,
  56. DW_TAG_packed_type := $2d,DW_TAG_subprogram := $2e,
  57. DW_TAG_template_type_param := $2f,DW_TAG_template_value_param := $30,
  58. DW_TAG_thrown_type := $31,DW_TAG_try_block := $32,
  59. DW_TAG_variant_part := $33,DW_TAG_variable := $34,
  60. DW_TAG_volatile_type := $35,
  61. { DWARF 3. }
  62. DW_TAG_dwarf_procedure := $36,
  63. DW_TAG_restrict_type := $37,DW_TAG_interface_type := $38,
  64. DW_TAG_namespace := $39,DW_TAG_imported_module := $3a,
  65. DW_TAG_unspecified_type := $3b,DW_TAG_partial_unit := $3c,
  66. DW_TAG_imported_unit := $3d,
  67. { SGI/MIPS Extensions. }
  68. DW_TAG_MIPS_loop := $4081,
  69. { HP extensions. See: ftp://ftp.hp.com/pub/lang/tools/WDB/wdb-4.0.tar.gz . }
  70. DW_TAG_HP_array_descriptor := $4090,
  71. { GNU extensions. }
  72. { For FORTRAN 77 and Fortran 90. }
  73. DW_TAG_format_label := $4101,
  74. { For C++. }
  75. DW_TAG_function_template := $4102,DW_TAG_class_template := $4103,
  76. DW_TAG_GNU_BINCL := $4104,DW_TAG_GNU_EINCL := $4105,
  77. { Extensions for UPC. See: http://upc.gwu.edu/~upc. }
  78. DW_TAG_upc_shared_type := $8765,DW_TAG_upc_strict_type := $8766,
  79. DW_TAG_upc_relaxed_type := $8767,
  80. { PGI (STMicroelectronics) extensions. No documentation available. }
  81. DW_TAG_PGI_kanji_type := $A000,
  82. DW_TAG_PGI_interface_block := $A020);
  83. { Attribute names and codes. }
  84. tdwarf_attribute = (DW_AT_sibling := $01,DW_AT_location := $02,
  85. DW_AT_name := $03,DW_AT_ordering := $09,
  86. DW_AT_subscr_data := $0a,DW_AT_byte_size := $0b,
  87. DW_AT_bit_offset := $0c,DW_AT_bit_size := $0d,
  88. DW_AT_element_list := $0f,DW_AT_stmt_list := $10,
  89. DW_AT_low_pc := $11,DW_AT_high_pc := $12,
  90. DW_AT_language := $13,DW_AT_member := $14,
  91. DW_AT_discr := $15,DW_AT_discr_value := $16,
  92. DW_AT_visibility := $17,DW_AT_import := $18,
  93. DW_AT_string_length := $19,DW_AT_common_reference := $1a,
  94. DW_AT_comp_dir := $1b,DW_AT_const_value := $1c,
  95. DW_AT_containing_type := $1d,DW_AT_default_value := $1e,
  96. DW_AT_inline := $20,DW_AT_is_optional := $21,
  97. DW_AT_lower_bound := $22,DW_AT_producer := $25,
  98. DW_AT_prototyped := $27,DW_AT_return_addr := $2a,
  99. DW_AT_start_scope := $2c,DW_AT_stride_size := $2e,
  100. DW_AT_upper_bound := $2f,DW_AT_abstract_origin := $31,
  101. DW_AT_accessibility := $32,DW_AT_address_class := $33,
  102. DW_AT_artificial := $34,DW_AT_base_types := $35,
  103. DW_AT_calling_convention := $36,DW_AT_count := $37,
  104. DW_AT_data_member_location := $38,DW_AT_decl_column := $39,
  105. DW_AT_decl_file := $3a,DW_AT_decl_line := $3b,
  106. DW_AT_declaration := $3c,DW_AT_discr_list := $3d,
  107. DW_AT_encoding := $3e,DW_AT_external := $3f,
  108. DW_AT_frame_base := $40,DW_AT_friend := $41,
  109. DW_AT_identifier_case := $42,DW_AT_macro_info := $43,
  110. DW_AT_namelist_items := $44,DW_AT_priority := $45,
  111. DW_AT_segment := $46,DW_AT_specification := $47,
  112. DW_AT_static_link := $48,DW_AT_type := $49,
  113. DW_AT_use_location := $4a,DW_AT_variable_parameter := $4b,
  114. DW_AT_virtuality := $4c,DW_AT_vtable_elem_location := $4d,
  115. { DWARF 3 values. }
  116. DW_AT_allocated := $4e,DW_AT_associated := $4f,
  117. DW_AT_data_location := $50,DW_AT_stride := $51,
  118. DW_AT_entry_pc := $52,DW_AT_use_UTF8 := $53,
  119. DW_AT_extension := $54,DW_AT_ranges := $55,
  120. DW_AT_trampoline := $56,DW_AT_call_column := $57,
  121. DW_AT_call_file := $58,DW_AT_call_line := $59,
  122. { SGI/MIPS extensions. }
  123. DW_AT_MIPS_fde := $2001,DW_AT_MIPS_loop_begin := $2002,
  124. DW_AT_MIPS_tail_loop_begin := $2003,DW_AT_MIPS_epilog_begin := $2004,
  125. DW_AT_MIPS_loop_unroll_factor := $2005,
  126. DW_AT_MIPS_software_pipeline_depth := $2006,
  127. DW_AT_MIPS_linkage_name := $2007,DW_AT_MIPS_stride := $2008,
  128. DW_AT_MIPS_abstract_name := $2009,DW_AT_MIPS_clone_origin := $200a,
  129. DW_AT_MIPS_has_inlines := $200b,
  130. { HP extensions. }
  131. DW_AT_HP_block_index := $2000,
  132. DW_AT_HP_unmodifiable := $2001,DW_AT_HP_actuals_stmt_list := $2010,
  133. DW_AT_HP_proc_per_section := $2011,DW_AT_HP_raw_data_ptr := $2012,
  134. DW_AT_HP_pass_by_reference := $2013,DW_AT_HP_opt_level := $2014,
  135. DW_AT_HP_prof_version_id := $2015,DW_AT_HP_opt_flags := $2016,
  136. DW_AT_HP_cold_region_low_pc := $2017,DW_AT_HP_cold_region_high_pc := $2018,
  137. DW_AT_HP_all_variables_modifiable := $2019,
  138. DW_AT_HP_linkage_name := $201a,DW_AT_HP_prof_flags := $201b,
  139. { GNU extensions. }
  140. DW_AT_sf_names := $2101,DW_AT_src_info := $2102,
  141. DW_AT_mac_info := $2103,DW_AT_src_coords := $2104,
  142. DW_AT_body_begin := $2105,DW_AT_body_end := $2106,
  143. DW_AT_GNU_vector := $2107,
  144. { VMS extensions. }
  145. DW_AT_VMS_rtnbeg_pd_address := $2201,
  146. { UPC extension. }
  147. DW_AT_upc_threads_scaled := $3210,
  148. { PGI (STMicroelectronics) extensions. }
  149. DW_AT_PGI_lbase := $3a00,
  150. DW_AT_PGI_soffset := $3a01,DW_AT_PGI_lstride := $3a02
  151. );
  152. { Form names and codes. }
  153. Tdwarf_form = (DW_FORM_addr := $01,DW_FORM_block2 := $03,
  154. DW_FORM_block4 := $04,DW_FORM_data2 := $05,
  155. DW_FORM_data4 := $06,DW_FORM_data8 := $07,
  156. DW_FORM_string := $08,DW_FORM_block := $09,
  157. DW_FORM_block1 := $0a,DW_FORM_data1 := $0b,
  158. DW_FORM_flag := $0c,DW_FORM_sdata := $0d,
  159. DW_FORM_strp := $0e,DW_FORM_udata := $0f,
  160. DW_FORM_ref_addr := $10,DW_FORM_ref1 := $11,
  161. DW_FORM_ref2 := $12,DW_FORM_ref4 := $13,
  162. DW_FORM_ref8 := $14,DW_FORM_ref_udata := $15,
  163. DW_FORM_indirect := $16);
  164. TDebugInfoDwarf = class(TDebugInfo)
  165. private
  166. currfileidx : longint;
  167. currabbrevnumber : longint;
  168. writing_def_dwarf : boolean;
  169. { use this defs to create info for variants and file handles }
  170. vardatadef,
  171. filerecdef,
  172. textrecdef : tdef;
  173. function append_entry(tag : tdwarf_tag;has_children : boolean;data : array of const) : longint;
  174. procedure append_labelentry(attr : tdwarf_attribute;sym : tasmsymbol);
  175. procedure append_labelentry_ref(attr : tdwarf_attribute;sym : tasmsymbol);
  176. procedure append_labelentry_data(attr : tdwarf_attribute;sym : tasmsymbol);
  177. procedure finish_entry;
  178. procedure finish_children;
  179. procedure field_add_dwarftag(p:Tnamedindexitem;arg:pointer);
  180. procedure method_add_dwarftag(p:Tnamedindexitem;arg:pointer);
  181. procedure append_procdef(list:taasmoutput;pd:tprocdef);
  182. procedure append_dwarftag(list:taasmoutput;def:tdef);
  183. procedure insertsym(list:taasmoutput;sym:tsym);
  184. procedure write_symtable_syms(list:taasmoutput;st:tsymtable);
  185. function def_dwarf_lab(def:tdef) : tasmsymbol;
  186. public
  187. procedure insertdef(list:taasmoutput;def:tdef);override;
  188. procedure insertmoduleinfo;override;
  189. procedure inserttypeinfo;override;
  190. procedure referencesections(list:taasmoutput);override;
  191. procedure insertlineinfo(list:taasmoutput);override;
  192. procedure write_symtable_defs(list:taasmoutput;st:tsymtable);override;
  193. end;
  194. implementation
  195. uses
  196. version,
  197. cutils,
  198. globtype,
  199. globals,
  200. verbose,
  201. systems,
  202. cpubase,
  203. cgbase,
  204. finput,
  205. fmodule,
  206. defutil,
  207. symconst,symtable,symsym
  208. ;
  209. const
  210. DW_TAG_lo_user = $4080;
  211. DW_TAG_hi_user = $ffff;
  212. { Flag that tells whether entry has a child or not. }
  213. DW_children_no = 0;
  214. DW_children_yes = 1;
  215. const
  216. { Implementation-defined range start. }
  217. DW_AT_lo_user = $2000;
  218. { Implementation-defined range end. }
  219. DW_AT_hi_user = $3ff0;
  220. type
  221. { Source language names and codes. }
  222. tdwarf_source_language = (DW_LANG_C89 := $0001,DW_LANG_C := $0002,DW_LANG_Ada83 := $0003,
  223. DW_LANG_C_plus_plus := $0004,DW_LANG_Cobol74 := $0005,
  224. DW_LANG_Cobol85 := $0006,DW_LANG_Fortran77 := $0007,
  225. DW_LANG_Fortran90 := $0008,DW_LANG_Pascal83 := $0009,
  226. DW_LANG_Modula2 := $000a,DW_LANG_Java := $000b,
  227. { DWARF 3. }
  228. DW_LANG_C99 := $000c,DW_LANG_Ada95 := $000d,
  229. DW_LANG_Fortran95 := $000e,
  230. { MIPS. }
  231. DW_LANG_Mips_Assembler := $8001,
  232. { UPC. }
  233. DW_LANG_Upc := $8765
  234. );
  235. const
  236. { Implementation-defined range start. }
  237. DW_LANG_lo_user = $8000;
  238. { Implementation-defined range start. }
  239. DW_LANG_hi_user = $ffff;
  240. type
  241. { Names and codes for macro information. }
  242. tdwarf_macinfo_record_type = (DW_MACINFO_define := 1,DW_MACINFO_undef := 2,
  243. DW_MACINFO_start_file := 3,DW_MACINFO_end_file := 4,
  244. DW_MACINFO_vendor_ext := 255);
  245. type
  246. { Type encodings. }
  247. Tdwarf_type = (DW_ATE_void := $0,DW_ATE_address := $1,
  248. DW_ATE_boolean := $2,DW_ATE_complex_float := $3,
  249. DW_ATE_float := $4,DW_ATE_signed := $5,
  250. DW_ATE_signed_char := $6,DW_ATE_unsigned := $7,
  251. DW_ATE_unsigned_char := $8,DW_ATE_imaginary_float := $9,
  252. { HP extensions. }
  253. DW_ATE_HP_float80 := $80,DW_ATE_HP_complex_float80 := $81,
  254. DW_ATE_HP_float128 := $82,DW_ATE_HP_complex_float128 := $83,
  255. DW_ATE_HP_floathpintel := $84,DW_ATE_HP_imaginary_float80 := $85,
  256. DW_ATE_HP_imaginary_float128 := $86
  257. );
  258. const
  259. DW_ATE_lo_user = $80;
  260. DW_ATE_hi_user = $ff;
  261. type
  262. Tdwarf_array_dim_ordering = (DW_ORD_row_major := 0,DW_ORD_col_major := 1
  263. );
  264. { Access attribute. }
  265. Tdwarf_access_attribute = (DW_ACCESS_public := 1,DW_ACCESS_protected := 2,
  266. DW_ACCESS_private := 3);
  267. { Visibility. }
  268. Tdwarf_visibility_attribute = (DW_VIS_local := 1,DW_VIS_exported := 2,
  269. DW_VIS_qualified := 3);
  270. { Virtuality. }
  271. Tdwarf_virtuality_attribute = (DW_VIRTUALITY_none := 0,DW_VIRTUALITY_virtual := 1,
  272. DW_VIRTUALITY_pure_virtual := 2);
  273. { Case sensitivity. }
  274. Tdwarf_id_case = (DW_ID_case_sensitive := 0,DW_ID_up_case := 1,
  275. DW_ID_down_case := 2,DW_ID_case_insensitive := 3
  276. );
  277. { Calling convention. }
  278. Tdwarf_calling_convention = (DW_CC_normal := $1,DW_CC_program := $2,
  279. DW_CC_nocall := $3,DW_CC_GNU_renesas_sh := $40
  280. );
  281. { Location atom names and codes. }
  282. Tdwarf_location_atom = (DW_OP_addr := $03,DW_OP_deref := $06,DW_OP_const1u := $08,
  283. DW_OP_const1s := $09,DW_OP_const2u := $0a,
  284. DW_OP_const2s := $0b,DW_OP_const4u := $0c,
  285. DW_OP_const4s := $0d,DW_OP_const8u := $0e,
  286. DW_OP_const8s := $0f,DW_OP_constu := $10,
  287. DW_OP_consts := $11,DW_OP_dup := $12,DW_OP_drop := $13,
  288. DW_OP_over := $14,DW_OP_pick := $15,DW_OP_swap := $16,
  289. DW_OP_rot := $17,DW_OP_xderef := $18,DW_OP_abs := $19,
  290. DW_OP_and := $1a,DW_OP_div := $1b,DW_OP_minus := $1c,
  291. DW_OP_mod := $1d,DW_OP_mul := $1e,DW_OP_neg := $1f,
  292. DW_OP_not := $20,DW_OP_or := $21,DW_OP_plus := $22,
  293. DW_OP_plus_uconst := $23,DW_OP_shl := $24,
  294. DW_OP_shr := $25,DW_OP_shra := $26,DW_OP_xor := $27,
  295. DW_OP_bra := $28,DW_OP_eq := $29,DW_OP_ge := $2a,
  296. DW_OP_gt := $2b,DW_OP_le := $2c,DW_OP_lt := $2d,
  297. DW_OP_ne := $2e,DW_OP_skip := $2f,DW_OP_lit0 := $30,
  298. DW_OP_lit1 := $31,DW_OP_lit2 := $32,DW_OP_lit3 := $33,
  299. DW_OP_lit4 := $34,DW_OP_lit5 := $35,DW_OP_lit6 := $36,
  300. DW_OP_lit7 := $37,DW_OP_lit8 := $38,DW_OP_lit9 := $39,
  301. DW_OP_lit10 := $3a,DW_OP_lit11 := $3b,
  302. DW_OP_lit12 := $3c,DW_OP_lit13 := $3d,
  303. DW_OP_lit14 := $3e,DW_OP_lit15 := $3f,
  304. DW_OP_lit16 := $40,DW_OP_lit17 := $41,
  305. DW_OP_lit18 := $42,DW_OP_lit19 := $43,
  306. DW_OP_lit20 := $44,DW_OP_lit21 := $45,
  307. DW_OP_lit22 := $46,DW_OP_lit23 := $47,
  308. DW_OP_lit24 := $48,DW_OP_lit25 := $49,
  309. DW_OP_lit26 := $4a,DW_OP_lit27 := $4b,
  310. DW_OP_lit28 := $4c,DW_OP_lit29 := $4d,
  311. DW_OP_lit30 := $4e,DW_OP_lit31 := $4f,
  312. DW_OP_reg0 := $50,DW_OP_reg1 := $51,DW_OP_reg2 := $52,
  313. DW_OP_reg3 := $53,DW_OP_reg4 := $54,DW_OP_reg5 := $55,
  314. DW_OP_reg6 := $56,DW_OP_reg7 := $57,DW_OP_reg8 := $58,
  315. DW_OP_reg9 := $59,DW_OP_reg10 := $5a,DW_OP_reg11 := $5b,
  316. DW_OP_reg12 := $5c,DW_OP_reg13 := $5d,
  317. DW_OP_reg14 := $5e,DW_OP_reg15 := $5f,
  318. DW_OP_reg16 := $60,DW_OP_reg17 := $61,
  319. DW_OP_reg18 := $62,DW_OP_reg19 := $63,
  320. DW_OP_reg20 := $64,DW_OP_reg21 := $65,
  321. DW_OP_reg22 := $66,DW_OP_reg23 := $67,
  322. DW_OP_reg24 := $68,DW_OP_reg25 := $69,
  323. DW_OP_reg26 := $6a,DW_OP_reg27 := $6b,
  324. DW_OP_reg28 := $6c,DW_OP_reg29 := $6d,
  325. DW_OP_reg30 := $6e,DW_OP_reg31 := $6f,
  326. DW_OP_breg0 := $70,DW_OP_breg1 := $71,
  327. DW_OP_breg2 := $72,DW_OP_breg3 := $73,
  328. DW_OP_breg4 := $74,DW_OP_breg5 := $75,
  329. DW_OP_breg6 := $76,DW_OP_breg7 := $77,
  330. DW_OP_breg8 := $78,DW_OP_breg9 := $79,
  331. DW_OP_breg10 := $7a,DW_OP_breg11 := $7b,
  332. DW_OP_breg12 := $7c,DW_OP_breg13 := $7d,
  333. DW_OP_breg14 := $7e,DW_OP_breg15 := $7f,
  334. DW_OP_breg16 := $80,DW_OP_breg17 := $81,
  335. DW_OP_breg18 := $82,DW_OP_breg19 := $83,
  336. DW_OP_breg20 := $84,DW_OP_breg21 := $85,
  337. DW_OP_breg22 := $86,DW_OP_breg23 := $87,
  338. DW_OP_breg24 := $88,DW_OP_breg25 := $89,
  339. DW_OP_breg26 := $8a,DW_OP_breg27 := $8b,
  340. DW_OP_breg28 := $8c,DW_OP_breg29 := $8d,
  341. DW_OP_breg30 := $8e,DW_OP_breg31 := $8f,
  342. DW_OP_regx := $90,DW_OP_fbreg := $91,DW_OP_bregx := $92,
  343. DW_OP_piece := $93,DW_OP_deref_size := $94,
  344. DW_OP_xderef_size := $95,DW_OP_nop := $96,
  345. { DWARF 3 extensions. }
  346. DW_OP_push_object_address := $97,DW_OP_call2 := $98,
  347. DW_OP_call4 := $99,DW_OP_call_ref := $9a,
  348. { GNU extensions. }
  349. DW_OP_GNU_push_tls_address := $e0,
  350. { HP extensions. }
  351. DW_OP_HP_unknown := $e0,
  352. DW_OP_HP_is_value := $e1,DW_OP_HP_fltconst4 := $e2,
  353. DW_OP_HP_fltconst8 := $e3,DW_OP_HP_mod_range := $e4,
  354. DW_OP_HP_unmod_range := $e5,DW_OP_HP_tls := $e6
  355. );
  356. const
  357. { Implementation-defined range start. }
  358. DW_OP_lo_user = $e0;
  359. { Implementation-defined range end. }
  360. DW_OP_hi_user = $ff;
  361. function TDebugInfoDwarf.def_dwarf_lab(def:tdef) : tasmsymbol;
  362. begin
  363. { procdefs only need a number, mark them as already written
  364. so they won't be written implicitly }
  365. if (def.deftype=procdef) then
  366. def.dbg_state:=dbg_state_written;
  367. { dwarf must already be written, or we must be busy writing it }
  368. if writing_def_dwarf and
  369. not(def.dbg_state in [dbg_state_writing,dbg_state_written]) then
  370. internalerror(200601241);
  371. { Keep track of used dwarf entries, this info is only usefull for dwarf entries
  372. referenced by the symbols. Definitions will always include all
  373. required stabs }
  374. if def.dbg_state=dbg_state_unused then
  375. def.dbg_state:=dbg_state_used;
  376. { Need a new label? }
  377. if def.dwarf_lab=nil then
  378. begin
  379. objectlibrary.getdatalabel(def.dwarf_lab);
  380. end;
  381. result:=def.dwarf_lab;
  382. end;
  383. { writing the data through a few simply procedures allows to create easily extra information
  384. for debugging of debug info }
  385. function TDebugInfoDwarf.append_entry(tag : tdwarf_tag;has_children : boolean;data : array of const) : longint;
  386. var
  387. i : longint;
  388. begin
  389. inc(currabbrevnumber);
  390. asmlist[al_dwarf_abbrev].concat(tai_comment.Create(strpnew('Abbrev '+tostr(currabbrevnumber))));
  391. { abbrev number }
  392. asmlist[al_dwarf_info].concat(tai_const.create_uleb128bit(currabbrevnumber));
  393. asmlist[al_dwarf_abbrev].concat(tai_const.create_uleb128bit(currabbrevnumber));
  394. { tag }
  395. asmlist[al_dwarf_abbrev].concat(tai_const.create_uleb128bit(ord(tag)));
  396. { children? }
  397. asmlist[al_dwarf_abbrev].concat(tai_const.create_8bit(ord(has_children)));
  398. i:=0;
  399. while i<=high(data) do
  400. begin
  401. { attribute }
  402. if data[i].VType=vtInteger then
  403. begin
  404. asmlist[al_dwarf_abbrev].concat(tai_const.create_uleb128bit(data[i].VInteger));
  405. end
  406. else
  407. internalerror(200601261);
  408. inc(i);
  409. { form }
  410. if data[i].VType=vtInteger then
  411. begin
  412. asmlist[al_dwarf_abbrev].concat(tai_const.create_uleb128bit(data[i].VInteger));
  413. end
  414. else
  415. internalerror(200601262);
  416. inc(i);
  417. { info itself }
  418. case tdwarf_form(data[i-1].VInteger) of
  419. DW_FORM_string:
  420. case data[i].VType of
  421. vtChar:
  422. asmlist[al_dwarf_info].concat(tai_string.create(data[i].VChar));
  423. vtString:
  424. asmlist[al_dwarf_info].concat(tai_string.create(data[i].VString^));
  425. vtAnsistring:
  426. asmlist[al_dwarf_info].concat(tai_string.create(Ansistring(data[i].VAnsiString^)));
  427. else
  428. internalerror(200601264);
  429. end;
  430. DW_FORM_flag:
  431. asmlist[al_dwarf_info].concat(tai_const.create_8bit(byte(data[i].VBoolean)));
  432. DW_FORM_data1:
  433. asmlist[al_dwarf_info].concat(tai_const.create_8bit(data[i].VInteger));
  434. DW_FORM_data2:
  435. asmlist[al_dwarf_info].concat(tai_const.create_16bit(data[i].VInteger));
  436. DW_FORM_data4:
  437. asmlist[al_dwarf_info].concat(tai_const.create_32bit(data[i].VInteger));
  438. DW_FORM_data8:
  439. asmlist[al_dwarf_info].concat(tai_const.create_64bit(data[i].VInteger));
  440. DW_FORM_sdata:
  441. case data[i].VType of
  442. vtInteger:
  443. asmlist[al_dwarf_info].concat(tai_const.create_sleb128bit(data[i].VInteger));
  444. vtInt64:
  445. asmlist[al_dwarf_info].concat(tai_const.create_sleb128bit(data[i].VInt64^));
  446. vtQWord:
  447. asmlist[al_dwarf_info].concat(tai_const.create_sleb128bit(data[i].VQWord^));
  448. else
  449. internalerror(200601285);
  450. end;
  451. DW_FORM_udata:
  452. case data[i].VType of
  453. vtInteger:
  454. asmlist[al_dwarf_info].concat(tai_const.create_uleb128bit(data[i].VInteger));
  455. vtInt64:
  456. asmlist[al_dwarf_info].concat(tai_const.create_uleb128bit(data[i].VInt64^));
  457. vtQWord:
  458. asmlist[al_dwarf_info].concat(tai_const.create_uleb128bit(data[i].VQWord^));
  459. else
  460. internalerror(200601284);
  461. end;
  462. { block gets only the size, the rest is appended manually by the caller }
  463. DW_FORM_block1:
  464. asmlist[al_dwarf_info].concat(tai_const.create_8bit(data[i].VInteger));
  465. else
  466. internalerror(200601263);
  467. end;
  468. inc(i);
  469. end;
  470. end;
  471. procedure TDebugInfoDwarf.append_labelentry(attr : tdwarf_attribute;sym : tasmsymbol);
  472. begin
  473. asmlist[al_dwarf_abbrev].concat(tai_const.create_uleb128bit(ord(attr)));
  474. asmlist[al_dwarf_abbrev].concat(tai_const.create_uleb128bit(ord(DW_FORM_addr)));
  475. asmlist[al_dwarf_info].concat(tai_const.create_sym(sym));
  476. end;
  477. procedure TDebugInfoDwarf.append_labelentry_ref(attr : tdwarf_attribute;sym : tasmsymbol);
  478. begin
  479. asmlist[al_dwarf_abbrev].concat(tai_const.create_uleb128bit(ord(attr)));
  480. {$ifdef cpu64bit}
  481. asmlist[al_dwarf_abbrev].concat(tai_const.create_uleb128bit(ord(DW_FORM_ref8)));
  482. {$else cpu64bit}
  483. asmlist[al_dwarf_abbrev].concat(tai_const.create_uleb128bit(ord(DW_FORM_ref4)));
  484. {$endif cpu64bit}
  485. asmlist[al_dwarf_info].concat(tai_const.create_sym(sym));
  486. end;
  487. procedure TDebugInfoDwarf.append_labelentry_data(attr : tdwarf_attribute;sym : tasmsymbol);
  488. begin
  489. asmlist[al_dwarf_abbrev].concat(tai_const.create_uleb128bit(ord(attr)));
  490. {$ifdef cpu64bit}
  491. asmlist[al_dwarf_abbrev].concat(tai_const.create_uleb128bit(ord(DW_FORM_data8)));
  492. {$else cpu64bit}
  493. asmlist[al_dwarf_abbrev].concat(tai_const.create_uleb128bit(ord(DW_FORM_data4)));
  494. {$endif cpu64bit}
  495. asmlist[al_dwarf_info].concat(tai_const.create_sym(sym));
  496. end;
  497. procedure TDebugInfoDwarf.finish_entry;
  498. begin
  499. asmlist[al_dwarf_abbrev].concat(tai_const.create_8bit(0));
  500. asmlist[al_dwarf_abbrev].concat(tai_const.create_8bit(0));
  501. end;
  502. procedure TDebugInfoDwarf.finish_children;
  503. begin
  504. asmlist[al_dwarf_info].concat(tai_const.create_8bit(0));
  505. end;
  506. procedure TDebugInfoDwarf.field_add_dwarftag(p:Tnamedindexitem;arg:pointer);
  507. begin
  508. { static variables from objects are like global objects }
  509. if (tsym(p).typ=fieldvarsym) and
  510. not(sp_static in Tsym(p).symoptions) then
  511. begin
  512. append_entry(DW_TAG_member,false,[
  513. DW_AT_name,DW_FORM_string,tsym(p).name+#0,
  514. DW_AT_data_member_location,DW_FORM_block1,1+lengthuleb128(tfieldvarsym(p).fieldoffset)
  515. ]);
  516. asmlist[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_plus_uconst)));
  517. asmlist[al_dwarf_info].concat(tai_const.create_uleb128bit(tfieldvarsym(p).fieldoffset));
  518. append_labelentry_ref(DW_AT_type,def_dwarf_lab(tfieldvarsym(p).vartype.def));
  519. finish_entry;
  520. end;
  521. end;
  522. procedure TDebugInfoDwarf.method_add_dwarftag(p:Tnamedindexitem;arg:pointer);
  523. begin
  524. end;
  525. procedure TDebugInfoDwarf.append_dwarftag(list:taasmoutput;def:tdef);
  526. procedure append_dwarftag_orddef(def:torddef);
  527. begin
  528. case def.typ of
  529. s8bit,
  530. s16bit,
  531. s32bit :
  532. begin
  533. { we should generate a subrange type here }
  534. if assigned(def.typesym) then
  535. append_entry(DW_TAG_base_type,false,[
  536. DW_AT_name,DW_FORM_string,def.typesym.name+#0,
  537. DW_AT_encoding,DW_FORM_data1,DW_ATE_signed,
  538. DW_AT_byte_size,DW_FORM_data1,def.size
  539. ])
  540. else
  541. append_entry(DW_TAG_base_type,false,[
  542. DW_AT_encoding,DW_FORM_data1,DW_ATE_signed,
  543. DW_AT_byte_size,DW_FORM_data1,def.size
  544. ]);
  545. finish_entry;
  546. end;
  547. u8bit,
  548. u16bit,
  549. u32bit :
  550. begin
  551. { we should generate a subrange type here }
  552. if assigned(def.typesym) then
  553. append_entry(DW_TAG_base_type,false,[
  554. DW_AT_name,DW_FORM_string,def.typesym.name+#0,
  555. DW_AT_encoding,DW_FORM_data1,DW_ATE_unsigned,
  556. DW_AT_byte_size,DW_FORM_data1,def.size
  557. ])
  558. else
  559. append_entry(DW_TAG_base_type,false,[
  560. DW_AT_encoding,DW_FORM_data1,DW_ATE_unsigned,
  561. DW_AT_byte_size,DW_FORM_data1,def.size
  562. ]);
  563. finish_entry;
  564. end;
  565. uvoid :
  566. begin
  567. { gdb 6.4 doesn't support DW_TAG_unspecified_type so we
  568. replace it with a unsigned type with size 0 (FK)
  569. }
  570. append_entry(DW_TAG_base_type,false,[
  571. DW_AT_name,DW_FORM_string,'Void'#0,
  572. DW_AT_encoding,DW_FORM_data1,DW_ATE_unsigned,
  573. DW_AT_byte_size,DW_FORM_data1,0
  574. ]);
  575. finish_entry;
  576. end;
  577. uchar :
  578. begin
  579. append_entry(DW_TAG_base_type,false,[
  580. DW_AT_name,DW_FORM_string,'Char'#0,
  581. DW_AT_encoding,DW_FORM_data1,DW_ATE_unsigned_char,
  582. DW_AT_byte_size,DW_FORM_data1,1
  583. ]);
  584. finish_entry;
  585. end;
  586. uwidechar :
  587. begin
  588. append_entry(DW_TAG_base_type,false,[
  589. DW_AT_name,DW_FORM_string,'WideChar'#0,
  590. DW_AT_encoding,DW_FORM_data1,DW_ATE_unsigned_char,
  591. DW_AT_byte_size,DW_FORM_data1,2
  592. ]);
  593. finish_entry;
  594. end;
  595. bool8bit :
  596. begin
  597. append_entry(DW_TAG_base_type,false,[
  598. DW_AT_name,DW_FORM_string,'Boolean'#0,
  599. DW_AT_encoding,DW_FORM_data1,DW_ATE_unsigned_char,
  600. DW_AT_byte_size,DW_FORM_data1,1
  601. ]);
  602. finish_entry;
  603. end;
  604. bool16bit :
  605. begin
  606. append_entry(DW_TAG_base_type,false,[
  607. DW_AT_name,DW_FORM_string,'WordBool'#0,
  608. DW_AT_encoding,DW_FORM_data1,DW_ATE_boolean,
  609. DW_AT_byte_size,DW_FORM_data1,2
  610. ]);
  611. finish_entry;
  612. end;
  613. bool32bit :
  614. begin
  615. append_entry(DW_TAG_base_type,false,[
  616. DW_AT_name,DW_FORM_string,'LongBool'#0,
  617. DW_AT_encoding,DW_FORM_data1,DW_ATE_boolean,
  618. DW_AT_byte_size,DW_FORM_data1,4
  619. ]);
  620. finish_entry;
  621. end;
  622. u64bit :
  623. begin
  624. append_entry(DW_TAG_base_type,false,[
  625. DW_AT_name,DW_FORM_string,'QWord'#0,
  626. DW_AT_encoding,DW_FORM_data1,DW_ATE_unsigned,
  627. DW_AT_byte_size,DW_FORM_data1,8
  628. ]);
  629. finish_entry;
  630. end;
  631. s64bit :
  632. begin
  633. append_entry(DW_TAG_base_type,false,[
  634. DW_AT_name,DW_FORM_string,'Int64'#0,
  635. DW_AT_encoding,DW_FORM_data1,DW_ATE_signed,
  636. DW_AT_byte_size,DW_FORM_data1,8
  637. ]);
  638. finish_entry;
  639. end;
  640. else
  641. internalerror(200601287);
  642. end;
  643. end;
  644. procedure append_dwarftag_floatdef(def:tfloatdef);
  645. begin
  646. case def.typ of
  647. s32real,
  648. s64real,
  649. s80real:
  650. if assigned(def.typesym) then
  651. append_entry(DW_TAG_base_type,false,[
  652. DW_AT_name,DW_FORM_string,def.typesym.name+#0,
  653. DW_AT_encoding,DW_FORM_data1,DW_ATE_float,
  654. DW_AT_byte_size,DW_FORM_data1,def.size
  655. ])
  656. else
  657. append_entry(DW_TAG_base_type,false,[
  658. DW_AT_encoding,DW_FORM_data1,DW_ATE_float,
  659. DW_AT_byte_size,DW_FORM_data1,def.size
  660. ]);
  661. s64currency:
  662. { we should use DW_ATE_signed_fixed, however it isn't supported yet by GDB (FK) }
  663. if assigned(def.typesym) then
  664. append_entry(DW_TAG_base_type,false,[
  665. DW_AT_name,DW_FORM_string,def.typesym.name+#0,
  666. DW_AT_encoding,DW_FORM_data1,DW_ATE_signed,
  667. DW_AT_byte_size,DW_FORM_data1,8
  668. ])
  669. else
  670. append_entry(DW_TAG_base_type,false,[
  671. DW_AT_encoding,DW_FORM_data1,DW_ATE_signed,
  672. DW_AT_byte_size,DW_FORM_data1,8
  673. ]);
  674. s64comp:
  675. if assigned(def.typesym) then
  676. append_entry(DW_TAG_base_type,false,[
  677. DW_AT_name,DW_FORM_string,def.typesym.name+#0,
  678. DW_AT_encoding,DW_FORM_data1,DW_ATE_signed,
  679. DW_AT_byte_size,DW_FORM_data1,8
  680. ])
  681. else
  682. append_entry(DW_TAG_base_type,false,[
  683. DW_AT_encoding,DW_FORM_data1,DW_ATE_signed,
  684. DW_AT_byte_size,DW_FORM_data1,8
  685. ]);
  686. else
  687. internalerror(200601289);
  688. end;
  689. finish_entry;
  690. end;
  691. procedure append_dwarftag_enumdef(def:tenumdef);
  692. var
  693. hp : tenumsym;
  694. begin
  695. if assigned(def.typesym) then
  696. append_entry(DW_TAG_enumeration_type,true,[
  697. DW_AT_name,DW_FORM_string,def.typesym.name+#0,
  698. DW_AT_byte_size,DW_FORM_data1,def.size
  699. ])
  700. else
  701. append_entry(DW_TAG_enumeration_type,true,[
  702. DW_AT_byte_size,DW_FORM_data1,def.size
  703. ]);
  704. if assigned(def.basedef) then
  705. append_labelentry_ref(DW_AT_type,def_dwarf_lab(def.basedef));
  706. finish_entry;
  707. { write enum symbols }
  708. hp:=tenumsym(def.firstenum);
  709. while assigned(hp) do
  710. begin
  711. append_entry(DW_TAG_enumerator,false,[
  712. DW_AT_name,DW_FORM_string,hp.name+#0,
  713. DW_AT_const_value,DW_FORM_data4,hp.value
  714. ]);
  715. finish_entry;
  716. hp:=tenumsym(hp).nextenum;
  717. end;
  718. finish_children;
  719. end;
  720. procedure append_dwarftag_arraydef(def:tarraydef);
  721. begin
  722. if assigned(def.typesym) then
  723. append_entry(DW_TAG_array_type,true,[
  724. DW_AT_name,DW_FORM_string,def.typesym.name+#0,
  725. DW_AT_byte_size,DW_FORM_udata,def.size,
  726. DW_AT_stride_size,DW_FORM_udata,def.elesize*8
  727. ])
  728. else
  729. append_entry(DW_TAG_array_type,true,[
  730. DW_AT_byte_size,DW_FORM_udata,def.size,
  731. DW_AT_stride_size,DW_FORM_udata,def.elesize*8
  732. ]);
  733. append_labelentry_ref(DW_AT_type,def_dwarf_lab(def.elementtype.def));
  734. if def.IsDynamicArray then
  735. begin
  736. { !!! FIXME !!! }
  737. { gdb's dwarf implementation sucks, so we can't use DW_OP_push_object here (FK)
  738. { insert location attribute manually }
  739. asmlist[al_dwarf_abbrev].concat(tai_const.create_uleb128bit(DW_AT_data_location));
  740. asmlist[al_dwarf_abbrev].concat(tai_const.create_uleb128bit(DW_FORM_block1));
  741. asmlist[al_dwarf_info].concat(tai_const.create_8bit(1));
  742. asmlist[al_dwarf_info].concat(tai_const.create_8bit(DW_OP_push_object));
  743. asmlist[al_dwarf_info].concat(tai_const.create_8bit(DW_OP_deref));
  744. }
  745. finish_entry;
  746. { to simplify things, we don't write a multidimensional array here }
  747. append_entry(DW_TAG_subrange_type,false,[
  748. DW_AT_lower_bound,DW_FORM_udata,0,
  749. DW_AT_upper_bound,DW_FORM_udata,0
  750. ]);
  751. append_labelentry_ref(DW_AT_type,def_dwarf_lab(def.rangetype.def));
  752. finish_entry;
  753. end
  754. else
  755. begin
  756. finish_entry;
  757. { to simplify things, we don't write a multidimensional array here }
  758. append_entry(DW_TAG_subrange_type,false,[
  759. DW_AT_lower_bound,DW_FORM_udata,def.lowrange,
  760. DW_AT_upper_bound,DW_FORM_udata,def.highrange
  761. ]);
  762. append_labelentry_ref(DW_AT_type,def_dwarf_lab(def.rangetype.def));
  763. finish_entry;
  764. end;
  765. finish_children;
  766. end;
  767. procedure append_dwarftag_recorddef(def:trecorddef);
  768. begin
  769. if assigned(def.typesym) then
  770. append_entry(DW_TAG_structure_type,true,[
  771. DW_AT_name,DW_FORM_string,def.typesym.name+#0,
  772. DW_AT_byte_size,DW_FORM_udata,def.size
  773. ])
  774. else
  775. append_entry(DW_TAG_structure_type,true,[
  776. DW_AT_byte_size,DW_FORM_udata,def.size
  777. ]);
  778. finish_entry;
  779. def.symtable.foreach(@field_add_dwarftag,nil);
  780. finish_children;
  781. end;
  782. procedure append_dwarftag_objectdef(def:tobjectdef);
  783. procedure doappend;
  784. begin
  785. if assigned(def.objname) then
  786. append_entry(DW_TAG_structure_type,true,[
  787. DW_AT_name,DW_FORM_string,def.objname^+#0,
  788. DW_AT_byte_size,DW_FORM_udata,def.size
  789. ])
  790. else
  791. append_entry(DW_TAG_structure_type,true,[
  792. DW_AT_byte_size,DW_FORM_udata,def.size
  793. ]);
  794. finish_entry;
  795. if assigned(def.childof) then
  796. begin
  797. append_entry(DW_TAG_inheritance,false,[
  798. DW_AT_accessibility,DW_FORM_data1,DW_ACCESS_public,
  799. DW_AT_data_member_location,DW_FORM_block1,1+lengthuleb128(0)
  800. ]);
  801. asmlist[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_plus_uconst)));
  802. asmlist[al_dwarf_info].concat(tai_const.create_uleb128bit(0));
  803. append_labelentry_ref(DW_AT_type,def_dwarf_lab(def.childof));
  804. finish_entry;
  805. end;
  806. def.symtable.foreach(@field_add_dwarftag,nil);
  807. def.symtable.foreach(@method_add_dwarftag,nil);
  808. finish_children;
  809. end;
  810. var
  811. obj : tasmlabel;
  812. begin
  813. case def.objecttype of
  814. odt_cppclass,
  815. odt_object:
  816. doappend;
  817. odt_interfacecom,
  818. odt_interfacecorba,
  819. odt_dispinterface,
  820. odt_class:
  821. begin
  822. objectlibrary.getdatalabel(obj);
  823. { implicit pointer }
  824. append_entry(DW_TAG_pointer_type,false,[]);
  825. append_labelentry_ref(DW_AT_type,obj);
  826. finish_entry;
  827. asmlist[al_dwarf_info].concat(tai_symbol.create(obj,0));
  828. doappend;
  829. end;
  830. else
  831. internalerror(200602041);
  832. end;
  833. end;
  834. procedure append_dwarftag_pointerdef(def:tpointerdef);
  835. begin
  836. append_entry(DW_TAG_pointer_type,false,[]);
  837. if not(is_voidpointer(def)) then
  838. append_labelentry_ref(DW_AT_type,def_dwarf_lab(def.pointertype.def));
  839. finish_entry;
  840. end;
  841. procedure append_dwarftag_procvardef(def:tprocvardef);
  842. procedure doappend;
  843. var
  844. i : longint;
  845. begin
  846. if assigned(def.typesym) then
  847. append_entry(DW_TAG_subroutine_type,true,[
  848. DW_AT_name,DW_FORM_string,def.typesym.name+#0,
  849. DW_AT_prototyped,DW_FORM_flag,true
  850. ])
  851. else
  852. append_entry(DW_TAG_subroutine_type,true,[
  853. DW_AT_prototyped,DW_FORM_flag,true
  854. ]);
  855. if not(is_void(tprocvardef(def).rettype.def)) then
  856. append_labelentry_ref(DW_AT_type,def_dwarf_lab(tprocvardef(def).rettype.def));
  857. finish_entry;
  858. { write parameters }
  859. for i:=0 to def.paras.count-1 do
  860. begin
  861. append_entry(DW_TAG_formal_parameter,false,[
  862. DW_AT_name,DW_FORM_string,tparavarsym(def.paras[i]).name+#0
  863. ]);
  864. append_labelentry_ref(DW_AT_type,def_dwarf_lab(tparavarsym(def.paras[i]).vartype.def));
  865. finish_entry;
  866. end;
  867. finish_children;
  868. end;
  869. var
  870. proc : tasmlabel;
  871. begin
  872. if def.is_methodpointer then
  873. begin
  874. { create a structure with two elements }
  875. objectlibrary.getdatalabel(proc);
  876. append_entry(DW_TAG_structure_type,true,[
  877. DW_AT_byte_size,DW_FORM_data1,2*sizeof(aint)
  878. ]);
  879. finish_entry;
  880. { proc entry }
  881. append_entry(DW_TAG_member,false,[
  882. DW_AT_name,DW_FORM_string,'Proc'#0,
  883. DW_AT_data_member_location,DW_FORM_block1,1+lengthuleb128(0)
  884. ]);
  885. asmlist[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_plus_uconst)));
  886. asmlist[al_dwarf_info].concat(tai_const.create_uleb128bit(0));
  887. append_labelentry_ref(DW_AT_type,proc);
  888. finish_entry;
  889. { self entry }
  890. append_entry(DW_TAG_member,false,[
  891. DW_AT_name,DW_FORM_string,'Self'#0,
  892. DW_AT_data_member_location,DW_FORM_block1,1+lengthuleb128(sizeof(aint))
  893. ]);
  894. asmlist[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_plus_uconst)));
  895. asmlist[al_dwarf_info].concat(tai_const.create_uleb128bit(sizeof(aint)));
  896. append_labelentry_ref(DW_AT_type,def_dwarf_lab(class_tobject));
  897. finish_entry;
  898. finish_children;
  899. asmlist[al_dwarf_info].concat(tai_symbol.create(proc,0));
  900. doappend;
  901. end
  902. else
  903. doappend;
  904. end;
  905. begin
  906. list.concat(tai_symbol.create(def_dwarf_lab(def),0));
  907. case def.deftype of
  908. {
  909. stringdef :
  910. result:=stringdef_stabstr(tstringdef(def));
  911. }
  912. enumdef :
  913. append_dwarftag_enumdef(tenumdef(def));
  914. orddef :
  915. append_dwarftag_orddef(torddef(def));
  916. pointerdef :
  917. append_dwarftag_pointerdef(tpointerdef(def));
  918. floatdef :
  919. append_dwarftag_floatdef(tfloatdef(def));
  920. filedef :
  921. begin
  922. { gdb 6.4 doesn't support files so far so we use some fake recorddef
  923. { file recs. are less than 1k so using data2 is enough }
  924. if assigned(def.typesym) then
  925. append_entry(DW_TAG_file_type,false,[
  926. DW_AT_name,DW_FORM_string,def.typesym.name+#0,
  927. DW_AT_byte_size,DW_FORM_data2,def.size
  928. ])
  929. else
  930. append_entry(DW_TAG_file_type,false,[
  931. DW_AT_byte_size,DW_FORM_data2,def.size
  932. ]);
  933. if tfiledef(def).filetyp=ft_typed then
  934. append_labelentry_ref(DW_AT_type,def_dwarf_lab(tfiledef(def).typedfiletype.def));
  935. }
  936. if assigned(def.typesym) then
  937. append_entry(DW_TAG_structure_type,true,[
  938. DW_AT_name,DW_FORM_string,def.typesym.name+#0,
  939. DW_AT_byte_size,DW_FORM_udata,def.size
  940. ])
  941. else
  942. append_entry(DW_TAG_structure_type,true,[
  943. DW_AT_byte_size,DW_FORM_udata,def.size
  944. ]);
  945. finish_entry;
  946. end;
  947. recorddef :
  948. append_dwarftag_recorddef(trecorddef(def));
  949. variantdef :
  950. { variants aren't known to dwarf but writting tvardata should be enough }
  951. append_dwarftag_recorddef(trecorddef(vardatadef));
  952. classrefdef :
  953. append_dwarftag_pointerdef(tpointerdef(pvmttype.def));
  954. setdef :
  955. begin
  956. { at least gdb up to 6.4 doesn't support sets in dwarf, there is a patch available to fix this:
  957. http://sources.redhat.com/ml/gdb-patches/2005-05/msg00278.html (FK)
  958. if assigned(def.typesym) then
  959. append_entry(DW_TAG_set_type,false,[
  960. DW_AT_name,DW_FORM_string,def.typesym.name+#0,
  961. DW_AT_byte_size,DW_FORM_data2,def.size
  962. ])
  963. else
  964. append_entry(DW_TAG_set_type,false,[
  965. DW_AT_byte_size,DW_FORM_data2,def.size
  966. ]);
  967. append_labelentry_ref(DW_AT_type,def_dwarf_lab(tsetdef(def).elementtype.def));
  968. finish_entry;
  969. }
  970. if assigned(def.typesym) then
  971. append_entry(DW_TAG_base_type,false,[
  972. DW_AT_name,DW_FORM_string,def.typesym.name+#0,
  973. DW_AT_encoding,DW_FORM_data1,DW_ATE_unsigned,
  974. DW_AT_byte_size,DW_FORM_data2,def.size
  975. ])
  976. else
  977. append_entry(DW_TAG_base_type,false,[
  978. DW_AT_encoding,DW_FORM_data1,DW_ATE_unsigned,
  979. DW_AT_byte_size,DW_FORM_data2,def.size
  980. ]);
  981. finish_entry;
  982. end;
  983. formaldef :
  984. begin
  985. { gdb 6.4 doesn't support DW_TAG_unspecified_type so we
  986. replace it with a unsigned type with size 0 (FK)
  987. }
  988. append_entry(DW_TAG_base_type,false,[
  989. DW_AT_name,DW_FORM_string,'FormalDef'#0,
  990. DW_AT_encoding,DW_FORM_data1,DW_ATE_unsigned,
  991. DW_AT_byte_size,DW_FORM_data1,0
  992. ]);
  993. finish_entry;
  994. end;
  995. arraydef :
  996. append_dwarftag_arraydef(tarraydef(def));
  997. {
  998. procdef :
  999. result:=procdef_stabstr(tprocdef(def));
  1000. }
  1001. procvardef :
  1002. append_dwarftag_procvardef(tprocvardef(def));
  1003. objectdef :
  1004. append_dwarftag_objectdef(tobjectdef(def));
  1005. undefineddef :
  1006. begin
  1007. { gdb 6.4 doesn't support DW_TAG_unspecified_type so we
  1008. replace it with a unsigned type with size 0 (FK)
  1009. }
  1010. append_entry(DW_TAG_base_type,false,[
  1011. DW_AT_name,DW_FORM_string,'FormalDef'#0,
  1012. DW_AT_encoding,DW_FORM_data1,DW_ATE_unsigned,
  1013. DW_AT_byte_size,DW_FORM_data1,0
  1014. ]);
  1015. finish_entry;
  1016. end;
  1017. else
  1018. internalerror(200601281);
  1019. end;
  1020. end;
  1021. procedure TDebugInfoDwarf.insertdef(list:taasmoutput;def:tdef);
  1022. var
  1023. anc : tobjectdef;
  1024. oldtypesym : tsym;
  1025. i : longint;
  1026. begin
  1027. if (def.dbg_state in [dbg_state_writing,dbg_state_written]) then
  1028. exit;
  1029. { never write generic template defs }
  1030. if df_generic in def.defoptions then
  1031. begin
  1032. def.dbg_state:=dbg_state_written;
  1033. exit;
  1034. end;
  1035. { to avoid infinite loops }
  1036. def.dbg_state := dbg_state_writing;
  1037. { write dependencies first }
  1038. case def.deftype of
  1039. stringdef :
  1040. begin
  1041. if tstringdef(def).string_typ=st_widestring then
  1042. insertdef(list,cwidechartype.def)
  1043. else
  1044. begin
  1045. insertdef(list,cchartype.def);
  1046. insertdef(list,u8inttype.def);
  1047. end;
  1048. end;
  1049. floatdef :
  1050. insertdef(list,s32inttype.def);
  1051. filedef :
  1052. begin
  1053. insertdef(list,s32inttype.def);
  1054. {$ifdef cpu64bit}
  1055. insertdef(list,s64inttype.def);
  1056. {$endif cpu64bit}
  1057. insertdef(list,u8inttype.def);
  1058. insertdef(list,cchartype.def);
  1059. end;
  1060. classrefdef :
  1061. insertdef(list,pvmttype.def);
  1062. pointerdef :
  1063. insertdef(list,tpointerdef(def).pointertype.def);
  1064. setdef :
  1065. insertdef(list,tsetdef(def).elementtype.def);
  1066. procvardef:
  1067. begin
  1068. insertdef(list,tprocvardef(def).rettype.def);
  1069. if tprocvardef(def).is_methodpointer then
  1070. insertdef(list,class_tobject);
  1071. { parameters }
  1072. for i:=0 to tprocvardef(def).paras.count-1 do
  1073. insertdef(list,tparavarsym(tprocvardef(def).paras[i]).vartype.def);
  1074. end;
  1075. procdef :
  1076. insertdef(list,tprocdef(def).rettype.def);
  1077. arraydef :
  1078. begin
  1079. insertdef(list,tarraydef(def).rangetype.def);
  1080. insertdef(list,tarraydef(def).elementtype.def);
  1081. end;
  1082. recorddef :
  1083. trecorddef(def).symtable.foreach(@field_write_defs,list);
  1084. variantdef :
  1085. trecorddef(vardatadef).symtable.foreach(@field_write_defs,list);
  1086. objectdef :
  1087. begin
  1088. insertdef(list,vmtarraytype.def);
  1089. { first the parents }
  1090. anc:=tobjectdef(def);
  1091. while assigned(anc.childof) do
  1092. begin
  1093. anc:=anc.childof;
  1094. insertdef(list,anc);
  1095. end;
  1096. tobjectdef(def).symtable.foreach(@field_write_defs,list);
  1097. tobjectdef(def).symtable.foreach(@method_write_defs,list);
  1098. end;
  1099. end;
  1100. append_dwarftag(list,def);
  1101. def.dbg_state:=dbg_state_written;
  1102. end;
  1103. procedure TDebugInfoDwarf.write_symtable_defs(list:taasmoutput;st:tsymtable);
  1104. procedure dowritedwarf(list:taasmoutput;st:tsymtable);
  1105. var
  1106. p : tdef;
  1107. begin
  1108. p:=tdef(st.defindex.first);
  1109. while assigned(p) do
  1110. begin
  1111. if (p.dbg_state=dbg_state_used) then
  1112. insertdef(list,p);
  1113. p:=tdef(p.indexnext);
  1114. end;
  1115. end;
  1116. var
  1117. old_writing_def_dwarf : boolean;
  1118. begin
  1119. case st.symtabletype of
  1120. staticsymtable :
  1121. list.concat(tai_comment.Create(strpnew('Defs - Begin Staticsymtable')));
  1122. globalsymtable :
  1123. list.concat(tai_comment.Create(strpnew('Defs - Begin unit '+st.name^+' has index '+tostr(st.moduleid))));
  1124. end;
  1125. old_writing_def_dwarf:=writing_def_dwarf;
  1126. writing_def_dwarf:=true;
  1127. dowritedwarf(list,st);
  1128. writing_def_dwarf:=old_writing_def_dwarf;
  1129. case st.symtabletype of
  1130. staticsymtable :
  1131. list.concat(tai_comment.Create(strpnew('Defs - End Staticsymtable')));
  1132. globalsymtable :
  1133. list.concat(tai_comment.Create(strpnew('Defs - End unit '+st.name^+' has index '+tostr(st.moduleid))));
  1134. end;
  1135. end;
  1136. procedure TDebugInfoDwarf.append_procdef(list:taasmoutput;pd:tprocdef);
  1137. var
  1138. procendlabel : tasmlabel;
  1139. mangled_length : longint;
  1140. p : pchar;
  1141. hs : string;
  1142. begin
  1143. if assigned(pd.procstarttai) then
  1144. begin
  1145. append_entry(DW_TAG_subprogram,true,
  1146. [DW_AT_name,DW_FORM_string,pd.procsym.name+#0
  1147. { data continues below }
  1148. { problem: base reg isn't known here
  1149. DW_AT_frame_base,DW_FORM_block1,1
  1150. }
  1151. ]);
  1152. { append block data }
  1153. { asmlist[al_dwarf_info].concat(tai_const.create_8bit(dwarf_reg(pd.))); }
  1154. if not(is_void(tprocdef(pd).rettype.def)) then
  1155. append_labelentry_ref(DW_AT_type,def_dwarf_lab(tprocdef(pd).rettype.def));
  1156. { mark end of procedure }
  1157. objectlibrary.getlabel(procendlabel,alt_dbgtype);
  1158. asmlist[al_procedures].insertbefore(tai_label.create(procendlabel),pd.procendtai);
  1159. append_labelentry(DW_AT_low_pc,objectlibrary.newasmsymbol(pd.mangledname,AB_LOCAL,AT_DATA));
  1160. append_labelentry(DW_AT_high_pc,procendlabel);
  1161. {
  1162. if assigned(pd.funcretsym) and
  1163. (tabstractnormalvarsym(pd.funcretsym).refs>0) then
  1164. begin
  1165. if tabstractnormalvarsym(pd.funcretsym).localloc.loc=LOC_REFERENCE then
  1166. begin
  1167. {$warning Need to add gdb support for ret in param register calling}
  1168. if paramanager.ret_in_param(pd.rettype.def,pd.proccalloption) then
  1169. hs:='X*'
  1170. else
  1171. hs:='X';
  1172. templist.concat(Tai_stab.create(stab_stabs,strpnew(
  1173. '"'+pd.procsym.name+':'+hs+def_stab_number(pd.rettype.def)+'",'+
  1174. tostr(N_tsym)+',0,0,'+tostr(tabstractnormalvarsym(pd.funcretsym).localloc.reference.offset))));
  1175. if (m_result in aktmodeswitches) then
  1176. templist.concat(Tai_stab.create(stab_stabs,strpnew(
  1177. '"RESULT:'+hs+def_stab_number(pd.rettype.def)+'",'+
  1178. tostr(N_tsym)+',0,0,'+tostr(tabstractnormalvarsym(pd.funcretsym).localloc.reference.offset))));
  1179. end;
  1180. end;
  1181. }
  1182. finish_entry;
  1183. {
  1184. { para types }
  1185. write_def_stabstr(templist,pd);
  1186. }
  1187. if assigned(pd.parast) then
  1188. write_symtable_syms(list,pd.parast);
  1189. { local type defs and vars should not be written
  1190. inside the main proc stab }
  1191. if assigned(pd.localst) and
  1192. (pd.localst.symtabletype=localsymtable) then
  1193. write_symtable_syms(list,pd.localst);
  1194. finish_children;
  1195. end;
  1196. end;
  1197. procedure TDebugInfoDwarf.insertsym(list:taasmoutput;sym:tsym);
  1198. function fieldvarsym_stabstr(sym:tfieldvarsym):Pchar;
  1199. begin
  1200. {
  1201. result:=nil;
  1202. if (sym.owner.symtabletype=objectsymtable) and
  1203. (sp_static in sym.symoptions) then
  1204. result:=sym_stabstr_evaluate(sym,'"${ownername}__${name}:S$1",${N_LCSYM},0,${line},${mangledname}',
  1205. [def_stab_number(sym.vartype.def)]);
  1206. }
  1207. end;
  1208. procedure append_varsym(sym:tabstractnormalvarsym);
  1209. var
  1210. templist : taasmoutput;
  1211. blocksize : longint;
  1212. regidx : longint;
  1213. tag : tdwarf_tag;
  1214. begin
  1215. { external symbols can't be resolved at link time, so we
  1216. can't generate stabs for them
  1217. not sure if this applies to dwarf as well (FK)
  1218. }
  1219. if vo_is_external in sym.varoptions then
  1220. exit;
  1221. { There is no space allocated for not referenced locals }
  1222. if (sym.owner.symtabletype=localsymtable) and (sym.refs=0) then
  1223. exit;
  1224. templist:=taasmoutput.create;
  1225. case sym.localloc.loc of
  1226. LOC_REGISTER,
  1227. LOC_CREGISTER,
  1228. LOC_MMREGISTER,
  1229. LOC_CMMREGISTER,
  1230. LOC_FPUREGISTER,
  1231. LOC_CFPUREGISTER :
  1232. begin
  1233. regidx:=findreg_by_number(sym.localloc.register);
  1234. templist.concat(tai_const.create_8bit(ord(DW_OP_regx)));
  1235. templist.concat(tai_const.create_uleb128bit(regdwarf_table[regidx]));
  1236. blocksize:=Lengthuleb128(regdwarf_table[regidx])+1;
  1237. end;
  1238. else
  1239. begin
  1240. case sym.typ of
  1241. globalvarsym:
  1242. begin
  1243. if (vo_is_thread_var in sym.varoptions) then
  1244. begin
  1245. {$warning !!! FIXME: dwarf for thread vars !!!}
  1246. end
  1247. else
  1248. begin
  1249. templist.concat(tai_const.create_8bit(3));
  1250. templist.concat(tai_const.createname(sym.mangledname,AT_DATA,0));
  1251. blocksize:=1+sizeof(aword);
  1252. end;
  1253. end;
  1254. paravarsym,
  1255. localvarsym:
  1256. begin
  1257. regidx:=findreg_by_number(sym.localloc.reference.base);
  1258. templist.concat(tai_const.create_8bit(ord(DW_OP_breg0)+regdwarf_table[regidx]));
  1259. templist.concat(tai_const.create_sleb128bit(sym.localloc.reference.offset));
  1260. blocksize:=Lengthsleb128(sym.localloc.reference.offset)+1;
  1261. end
  1262. else
  1263. internalerror(200601288);
  1264. end;
  1265. end;
  1266. end;
  1267. if sym.typ=paravarsym then
  1268. tag:=DW_TAG_formal_parameter
  1269. else
  1270. tag:=DW_TAG_variable;
  1271. append_entry(tag,false,[
  1272. DW_AT_name,DW_FORM_string,sym.name+#0,
  1273. {
  1274. DW_AT_decl_file,DW_FORM_data1,0,
  1275. DW_AT_decl_line,DW_FORM_data1,
  1276. }
  1277. DW_AT_external,DW_FORM_flag,true,
  1278. { data continues below }
  1279. DW_AT_location,DW_FORM_block1,blocksize
  1280. ]);
  1281. { append block data }
  1282. asmlist[al_dwarf_info].concatlist(templist);
  1283. append_labelentry_ref(DW_AT_type,def_dwarf_lab(sym.vartype.def));
  1284. templist.free;
  1285. finish_entry;
  1286. end;
  1287. procedure paravarsym_stabstr(sym:tparavarsym);
  1288. begin
  1289. {
  1290. result:=nil;
  1291. { set loc to LOC_REFERENCE to get somewhat usable debugging info for -Or }
  1292. { while stabs aren't adapted for regvars yet }
  1293. if (vo_is_self in sym.varoptions) then
  1294. begin
  1295. case sym.localloc.loc of
  1296. LOC_REGISTER,
  1297. LOC_CREGISTER:
  1298. regidx:=findreg_by_number(sym.localloc.register);
  1299. LOC_REFERENCE: ;
  1300. else
  1301. internalerror(2003091815);
  1302. end;
  1303. if (po_classmethod in tabstractprocdef(sym.owner.defowner).procoptions) or
  1304. (po_staticmethod in tabstractprocdef(sym.owner.defowner).procoptions) then
  1305. begin
  1306. if (sym.localloc.loc=LOC_REFERENCE) then
  1307. result:=sym_stabstr_evaluate(sym,'"pvmt:p$1",${N_TSYM},0,0,$2',
  1308. [def_stab_number(pvmttype.def),tostr(sym.localloc.reference.offset)]);
  1309. (* else
  1310. result:=sym_stabstr_evaluate(sym,'"pvmt:r$1",${N_RSYM},0,0,$2',
  1311. [def_stab_number(pvmttype.def),tostr(regstabs_table[regidx])]) *)
  1312. end
  1313. else
  1314. begin
  1315. if not(is_class(tprocdef(sym.owner.defowner)._class)) then
  1316. c:='v'
  1317. else
  1318. c:='p';
  1319. if (sym.localloc.loc=LOC_REFERENCE) then
  1320. result:=sym_stabstr_evaluate(sym,'"$$t:$1",${N_TSYM},0,0,$2',
  1321. [c+def_stab_number(tprocdef(sym.owner.defowner)._class),tostr(sym.localloc.reference.offset)]);
  1322. (* else
  1323. result:=sym_stabstr_evaluate(sym,'"$$t:r$1",${N_RSYM},0,0,$2',
  1324. [c+def_stab_number(tprocdef(sym.owner.defowner)._class),tostr(regstabs_table[regidx])]); *)
  1325. end;
  1326. end
  1327. else
  1328. begin
  1329. st:=def_stab_number(sym.vartype.def);
  1330. if paramanager.push_addr_param(sym.varspez,sym.vartype.def,tprocdef(sym.owner.defowner).proccalloption) and
  1331. not(vo_has_local_copy in sym.varoptions) and
  1332. not is_open_string(sym.vartype.def) then
  1333. st := 'v'+st { should be 'i' but 'i' doesn't work }
  1334. else
  1335. st := 'p'+st;
  1336. case sym.localloc.loc of
  1337. LOC_REGISTER,
  1338. LOC_CREGISTER,
  1339. LOC_MMREGISTER,
  1340. LOC_CMMREGISTER,
  1341. LOC_FPUREGISTER,
  1342. LOC_CFPUREGISTER :
  1343. begin
  1344. regidx:=findreg_by_number(sym.localloc.register);
  1345. { "eax", "ecx", "edx", "ebx", "esp", "ebp", "esi", "edi", "eip", "ps", "cs", "ss", "ds", "es", "fs", "gs", }
  1346. { this is the register order for GDB}
  1347. if regidx<>0 then
  1348. result:=sym_stabstr_evaluate(sym,'"${name}:r$1",${N_RSYM},0,${line},$2',[st,tostr(longint(regstabs_table[regidx]))]);
  1349. end;
  1350. LOC_REFERENCE :
  1351. { offset to ebp => will not work if the framepointer is esp
  1352. so some optimizing will make things harder to debug }
  1353. result:=sym_stabstr_evaluate(sym,'"${name}:$1",${N_TSYM},0,${line},$2',[st,tostr(sym.localloc.reference.offset)])
  1354. else
  1355. internalerror(2003091814);
  1356. end;
  1357. end;
  1358. }
  1359. end;
  1360. procedure append_constsym(sym:tconstsym);
  1361. var
  1362. st : string;
  1363. begin
  1364. append_entry(DW_TAG_constant,false,[
  1365. DW_AT_name,DW_FORM_string,sym.name+#0
  1366. ]);
  1367. { for string constants, consttype isn't set because they have no real type }
  1368. if sym.consttyp<>conststring then
  1369. append_labelentry_ref(DW_AT_type,def_dwarf_lab(sym.consttype.def));
  1370. asmlist[al_dwarf_abbrev].concat(tai_const.create_uleb128bit(ord(DW_AT_const_value)));
  1371. case sym.consttyp of
  1372. conststring:
  1373. begin
  1374. asmlist[al_dwarf_abbrev].concat(tai_const.create_uleb128bit(ord(DW_FORM_string)));
  1375. asmlist[al_dwarf_info].concat(tai_string.create(strpas(pchar(sym.value.valueptr))));
  1376. asmlist[al_dwarf_info].concat(tai_const.create_8bit(0));
  1377. end;
  1378. constord:
  1379. begin
  1380. asmlist[al_dwarf_abbrev].concat(tai_const.create_uleb128bit(ord(DW_FORM_sdata)));
  1381. asmlist[al_dwarf_info].concat(tai_const.create_sleb128bit(sym.value.valueord));
  1382. end;
  1383. constpointer:
  1384. begin
  1385. {$ifdef cpu64bit}
  1386. asmlist[al_dwarf_abbrev].concat(tai_const.create_uleb128bit(ord(DW_FORM_data8)));
  1387. asmlist[al_dwarf_info].concat(tai_const.create_64bit(sym.value.valueordptr));
  1388. {$else cpu64bit}
  1389. asmlist[al_dwarf_abbrev].concat(tai_const.create_uleb128bit(ord(DW_FORM_data4)));
  1390. asmlist[al_dwarf_info].concat(tai_const.create_32bit(sym.value.valueordptr));
  1391. {$endif cpu64bit}
  1392. end;
  1393. constreal:
  1394. begin
  1395. asmlist[al_dwarf_abbrev].concat(tai_const.create_uleb128bit(ord(DW_FORM_block1)));
  1396. case tfloatdef(sym.consttype.def).typ of
  1397. s32real:
  1398. begin
  1399. asmlist[al_dwarf_info].concat(tai_const.create_8bit(4));
  1400. asmlist[al_dwarf_info].concat(tai_real_32bit.create(psingle(sym.value.valueptr)^));
  1401. end;
  1402. s64comp,
  1403. s64currency,
  1404. s64real:
  1405. begin
  1406. asmlist[al_dwarf_info].concat(tai_const.create_8bit(8));
  1407. asmlist[al_dwarf_info].concat(tai_real_64bit.create(pdouble(sym.value.valueptr)^));
  1408. end;
  1409. s80real:
  1410. begin
  1411. asmlist[al_dwarf_info].concat(tai_const.create_8bit(10));
  1412. asmlist[al_dwarf_info].concat(tai_real_80bit.create(pextended(sym.value.valueptr)^));
  1413. end;
  1414. else
  1415. internalerror(200601291);
  1416. end;
  1417. end;
  1418. else
  1419. internalerror(200601291);
  1420. end;
  1421. finish_entry;
  1422. end;
  1423. procedure append_procsym(sym:tprocsym);
  1424. var
  1425. i : longint;
  1426. begin
  1427. for i:=1 to sym.procdef_count do
  1428. append_procdef(list,sym.procdef[i]);
  1429. end;
  1430. begin
  1431. case sym.typ of
  1432. globalvarsym :
  1433. append_varsym(tglobalvarsym(sym));
  1434. unitsym:
  1435. { for now, we ignore unit symbols }
  1436. ;
  1437. procsym :
  1438. append_procsym(tprocsym(sym));
  1439. labelsym :
  1440. { ignore label syms for now, the problem is that a label sym
  1441. can have more than one label associated e.g. in case of
  1442. an inline procedure expansion }
  1443. ;
  1444. localvarsym :
  1445. append_varsym(tlocalvarsym(sym));
  1446. paravarsym :
  1447. append_varsym(tparavarsym(sym));
  1448. typedconstsym :
  1449. begin
  1450. append_entry(DW_TAG_variable,false,[
  1451. DW_AT_name,DW_FORM_string,sym.name+#0,
  1452. {
  1453. DW_AT_decl_file,DW_FORM_data1,0,
  1454. DW_AT_decl_line,DW_FORM_data1,
  1455. }
  1456. DW_AT_external,DW_FORM_flag,true,
  1457. { data continues below }
  1458. DW_AT_location,DW_FORM_block1,1+sizeof(aword)
  1459. ]);
  1460. { append block data }
  1461. asmlist[al_dwarf_info].concat(tai_const.create_8bit(3));
  1462. asmlist[al_dwarf_info].concat(tai_const.createname(sym.mangledname,AT_DATA,0));
  1463. append_labelentry_ref(DW_AT_type,def_dwarf_lab(ttypedconstsym(sym).typedconsttype.def));
  1464. finish_entry;
  1465. end;
  1466. constsym :
  1467. append_constsym(tconstsym(sym));
  1468. typesym :
  1469. begin
  1470. append_entry(DW_TAG_typedef,false,[
  1471. DW_AT_name,DW_FORM_string,sym.name+#0
  1472. ]);
  1473. append_labelentry_ref(DW_AT_type,def_dwarf_lab(ttypesym(sym).restype.def));
  1474. finish_entry;
  1475. end;
  1476. enumsym :
  1477. { ignore enum syms, they are written by the owner }
  1478. ;
  1479. rttisym :
  1480. { ignore rtti syms, they are only of internal use }
  1481. ;
  1482. else
  1483. internalerror(200601242);
  1484. end;
  1485. {
  1486. if stabstr<>nil then
  1487. list.concat(Tai_stab.create(stab_stabs,stabstr));
  1488. { For object types write also the symtable entries }
  1489. if (sym.typ=typesym) and (ttypesym(sym).restype.def.deftype=objectdef) then
  1490. write_symtable_syms(list,tobjectdef(ttypesym(sym).restype.def).symtable);
  1491. sym.isstabwritten:=true;
  1492. }
  1493. end;
  1494. procedure TDebugInfoDwarf.write_symtable_syms(list:taasmoutput;st:tsymtable);
  1495. var
  1496. p : tsym;
  1497. begin
  1498. case st.symtabletype of
  1499. staticsymtable :
  1500. list.concat(tai_comment.Create(strpnew('Syms - Begin Staticsymtable')));
  1501. globalsymtable :
  1502. list.concat(tai_comment.Create(strpnew('Syms - Begin unit '+st.name^+' has index '+tostr(st.moduleid))));
  1503. end;
  1504. p:=tsym(st.symindex.first);
  1505. while assigned(p) do
  1506. begin
  1507. if (not p.isstabwritten) then
  1508. insertsym(list,p);
  1509. p:=tsym(p.indexnext);
  1510. end;
  1511. case st.symtabletype of
  1512. staticsymtable :
  1513. list.concat(tai_comment.Create(strpnew('Syms - End Staticsymtable')));
  1514. globalsymtable :
  1515. list.concat(tai_comment.Create(strpnew('Syms - End unit '+st.name^+' has index '+tostr(st.moduleid))));
  1516. end;
  1517. end;
  1518. procedure TDebugInfoDwarf.insertmoduleinfo;
  1519. var
  1520. templist : taasmoutput;
  1521. begin
  1522. { insert .Ltext0 label }
  1523. templist:=taasmoutput.create;
  1524. new_section(templist,sec_code,'',0);
  1525. templist.concat(tai_symbol.createname('.Ltext0',AT_DATA,0));
  1526. asmlist[al_start].insertlist(templist);
  1527. templist.free;
  1528. { insert .Letext0 label }
  1529. templist:=taasmoutput.create;
  1530. new_section(templist,sec_code,'',0);
  1531. templist.concat(tai_symbol.createname('.Letext0',AT_DATA,0));
  1532. asmlist[al_end].insertlist(templist);
  1533. templist.free;
  1534. { insert .Ldebug_abbrev0 label }
  1535. templist:=taasmoutput.create;
  1536. new_section(templist,sec_debug_abbrev,'',0);
  1537. templist.concat(tai_symbol.createname('.Ldebug_abbrev0',AT_DATA,0));
  1538. asmlist[al_start].insertlist(templist);
  1539. templist.free;
  1540. { insert .Ldebug_line0 label }
  1541. templist:=taasmoutput.create;
  1542. new_section(templist,sec_debug_line,'',0);
  1543. templist.concat(tai_symbol.createname('.Ldebug_line0',AT_DATA,0));
  1544. asmlist[al_start].insertlist(templist);
  1545. templist.free;
  1546. end;
  1547. procedure TDebugInfoDwarf.inserttypeinfo;
  1548. function gettypedef(const s : string) : tdef;
  1549. var
  1550. srsym : tsym;
  1551. begin
  1552. srsym:=searchsymonlyin(systemunit,'TVARDATA');
  1553. if not(assigned(srsym)) then
  1554. internalerror(200602022);
  1555. result:=ttypesym(srsym).restype.def;
  1556. end;
  1557. var
  1558. storefilepos : tfileposinfo;
  1559. lenstartlabel : tasmlabel;
  1560. begin
  1561. storefilepos:=aktfilepos;
  1562. aktfilepos:=current_module.mainfilepos;
  1563. currabbrevnumber:=0;
  1564. writing_def_dwarf:=false;
  1565. vardatadef:=gettypedef('TVARDATA');
  1566. { not exported (FK)
  1567. filerecdef:=gettypedef('FILEREC');
  1568. textrecdef:=gettypedef('TEXTREC');
  1569. }
  1570. { write start labels }
  1571. asmlist[al_dwarf_info].concat(tai_section.create(sec_debug_info,'',0));
  1572. asmlist[al_dwarf_info].concat(tai_symbol.createname('.Ldebug_info0',AT_DATA,0));
  1573. { start abbrev section }
  1574. new_section(asmlist[al_dwarf_abbrev],sec_debug_abbrev,'',0);
  1575. { debug info header }
  1576. objectlibrary.getlabel(lenstartlabel,alt_dbgfile);
  1577. { size }
  1578. asmlist[al_dwarf_info].concat(tai_const.create_rel_sym(aitconst_ptr,
  1579. lenstartlabel,tasmsymbol.create('.Ledebug_info0',AB_COMMON,AT_DATA)));
  1580. asmlist[al_dwarf_info].concat(tai_label.create(lenstartlabel));
  1581. { version }
  1582. asmlist[al_dwarf_info].concat(tai_const.create_16bit(2));
  1583. { abbrev table }
  1584. asmlist[al_dwarf_info].concat(tai_const.createname('.Ldebug_abbrev0',AT_DATA,0));
  1585. { address size }
  1586. asmlist[al_dwarf_info].concat(tai_const.create_8bit(sizeof(aint)));
  1587. append_entry(DW_TAG_compile_unit,true,[
  1588. DW_AT_name,DW_FORM_string,FixFileName(current_module.sourcefiles.get_file(1).name^)+#0,
  1589. DW_AT_producer,DW_FORM_string,'Free Pascal '+full_version_string+' '+date_string+#0,
  1590. DW_AT_comp_dir,DW_FORM_string,BsToSlash(FixPath(current_module.sourcefiles.get_file(1).path^,false))+#0,
  1591. DW_AT_language,DW_FORM_data1,DW_LANG_Pascal83,
  1592. DW_AT_identifier_case,DW_FORM_data1,DW_ID_case_insensitive]);
  1593. { reference to line info section }
  1594. append_labelentry_data(DW_AT_stmt_list,objectlibrary.newasmsymbol('.Ldebug_line0',AB_LOCAL,AT_DATA));
  1595. append_labelentry(DW_AT_low_pc,objectlibrary.newasmsymbol('.Ltext0',AB_LOCAL,AT_DATA));
  1596. append_labelentry(DW_AT_high_pc,objectlibrary.newasmsymbol('.Letext0',AB_LOCAL,AT_DATA));
  1597. finish_entry;
  1598. { first write all global/local symbols. This will flag all required tdefs }
  1599. if assigned(current_module.globalsymtable) then
  1600. write_symtable_syms(asmlist[al_dwarf_info],current_module.globalsymtable);
  1601. if assigned(current_module.localsymtable) then
  1602. write_symtable_syms(asmlist[al_dwarf_info],current_module.localsymtable);
  1603. { reset unit type info flag }
  1604. reset_unit_type_info;
  1605. { write used types from the used units }
  1606. write_used_unit_type_info(asmlist[al_dwarf_info],current_module);
  1607. { last write the types from this unit }
  1608. if assigned(current_module.globalsymtable) then
  1609. write_symtable_defs(asmlist[al_dwarf_info],current_module.globalsymtable);
  1610. if assigned(current_module.localsymtable) then
  1611. write_symtable_defs(asmlist[al_dwarf_info],current_module.localsymtable);
  1612. { close compilation unit entry }
  1613. finish_children;
  1614. { end of debug info table }
  1615. asmlist[al_dwarf_info].concat(tai_const.create_8bit(0));
  1616. asmlist[al_dwarf_info].concat(tai_symbol.createname('.Ledebug_info0',AT_DATA,0));
  1617. aktfilepos:=storefilepos;
  1618. end;
  1619. procedure TDebugInfoDwarf.referencesections(list:taasmoutput);
  1620. begin
  1621. end;
  1622. procedure TDebugInfoDwarf.insertlineinfo(list:taasmoutput);
  1623. var
  1624. currfileinfo,
  1625. lastfileinfo : tfileposinfo;
  1626. currfuncname : pstring;
  1627. currsectype : tasmsectiontype;
  1628. hlabel : tasmlabel;
  1629. hp : tai;
  1630. infile : tinputfile;
  1631. begin
  1632. FillChar(lastfileinfo,sizeof(lastfileinfo),0);
  1633. currfuncname:=nil;
  1634. currsectype:=sec_code;
  1635. hp:=Tai(list.first);
  1636. while assigned(hp) do
  1637. begin
  1638. case hp.typ of
  1639. ait_section :
  1640. currsectype:=tai_section(hp).sectype;
  1641. ait_function_name :
  1642. currfuncname:=tai_function_name(hp).funcname;
  1643. ait_force_line :
  1644. lastfileinfo.line:=-1;
  1645. end;
  1646. if (currsectype=sec_code) and
  1647. (hp.typ=ait_instruction) then
  1648. begin
  1649. currfileinfo:=tailineinfo(hp).fileinfo;
  1650. { file changed ? (must be before line info) }
  1651. if (currfileinfo.fileindex<>0) and
  1652. (lastfileinfo.fileindex<>currfileinfo.fileindex) then
  1653. begin
  1654. infile:=current_module.sourcefiles.get_file(currfileinfo.fileindex);
  1655. if assigned(infile) then
  1656. begin
  1657. inc(currfileidx);
  1658. if (infile.path^<>'') then
  1659. list.insertbefore(tai_file.create(
  1660. BsToSlash(FixPath(infile.path^,false)+FixFileName(infile.name^)),currfileidx
  1661. ),hp)
  1662. else
  1663. list.insertbefore(tai_file.create(
  1664. FixFileName(infile.name^),currfileidx),hp);
  1665. { force new line info }
  1666. lastfileinfo.line:=-1;
  1667. end;
  1668. end;
  1669. { line changed ? }
  1670. if (lastfileinfo.line<>currfileinfo.line) and (currfileinfo.line<>0) then
  1671. list.insertbefore(tai_loc.create(
  1672. currfileidx,currfileinfo.line,currfileinfo.column),hp);
  1673. lastfileinfo:=currfileinfo;
  1674. end;
  1675. hp:=tai(hp.next);
  1676. end;
  1677. end;
  1678. const
  1679. dbg_dwarf_info : tdbginfo =
  1680. (
  1681. id : dbg_dwarf;
  1682. idtxt : 'DWARF';
  1683. );
  1684. initialization
  1685. RegisterDebugInfo(dbg_dwarf_info,TDebugInfoDwarf);
  1686. end.