dbgdwarf.pas 143 KB

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