dbgdwarf.pas 186 KB

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