dbgdwarf.pas 178 KB

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