dbgdwarf.pas 153 KB

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