dbgdwarf.pas 118 KB

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