dbgdwarf.pas 181 KB

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