dbgdwarf.pas 157 KB

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