dbgdwarf.pas 187 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656
  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,s : 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. if (ds_dwarf_cpp in current_settings.debugswitches) and (def.owner.symtabletype in [objectsymtable,recordsymtable]) then
  2049. begin
  2050. { If C++ emulation is enabled, add DW_AT_linkage_name attribute for methods.
  2051. LLDB uses it to display fully qualified method names.
  2052. Add a simple C++ mangled name without params to achieve at least "Class::Method()"
  2053. instead of just "Method" in LLDB. }
  2054. s:=tabstractrecorddef(def.owner.defowner).objrealname^;
  2055. procentry:=Format('_ZN%d%s', [Length(s), s]);
  2056. s:=symname(def.procsym, false);
  2057. procentry:=Format('%s%d%sEv'#0, [procentry, Length(s), s]);
  2058. append_attribute(DW_AT_linkage_name,DW_FORM_string, [procentry]);
  2059. end;
  2060. append_proc_frame_base(list,def);
  2061. { Append optional flags. }
  2062. { All Pascal procedures are prototyped }
  2063. append_attribute(DW_AT_prototyped,DW_FORM_flag,[true]);
  2064. { Calling convention. }
  2065. cc:=dwarf_calling_convention(def);
  2066. if (cc<>DW_CC_normal) then
  2067. append_attribute(DW_AT_calling_convention,DW_FORM_data1,[ord(cc)]);
  2068. {$ifdef i8086}
  2069. { Call model (near or far). Open Watcom compatible. }
  2070. if tcpuprocdef(def).is_far then
  2071. append_attribute(DW_AT_address_class,DW_FORM_data1,[DW_ADDR_far16])
  2072. else
  2073. append_attribute(DW_AT_address_class,DW_FORM_data1,[DW_ADDR_none]);
  2074. {$endif i8086}
  2075. { Externally visible. }
  2076. if (po_global in def.procoptions) and
  2077. (def.parast.symtablelevel<=normal_function_level) then
  2078. append_attribute(DW_AT_external,DW_FORM_flag,[true]);
  2079. { Abstract or virtual/overriding method. }
  2080. if (([po_abstractmethod, po_virtualmethod, po_overridingmethod] * def.procoptions) <> []) and
  2081. not is_objc_class_or_protocol(def.struct) and
  2082. not is_objectpascal_helper(def.struct) then
  2083. begin
  2084. if not(po_abstractmethod in def.procoptions) then
  2085. append_attribute(DW_AT_virtuality,DW_FORM_data1,[ord(DW_VIRTUALITY_virtual)])
  2086. else
  2087. append_attribute(DW_AT_virtuality,DW_FORM_data1,[ord(DW_VIRTUALITY_pure_virtual)]);
  2088. { Element number in the vmt (needs to skip stuff coming before the
  2089. actual method addresses in the vmt, so we use vmtmethodoffset()
  2090. and then divide by sizeof(pint)). }
  2091. vmtoffset:=tobjectdef(def.owner.defowner).vmtmethodoffset(def.extnumber);
  2092. append_attribute(DW_AT_vtable_elem_location,DW_FORM_block1,[3+LengthUleb128(vmtoffset)]);
  2093. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_deref)));
  2094. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_constu)));
  2095. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.Create_uleb128bit(vmtoffset));
  2096. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_plus)));
  2097. end;
  2098. { accessibility: public/private/protected }
  2099. if (def.owner.symtabletype in [objectsymtable,recordsymtable]) then
  2100. append_visibility(def.visibility);
  2101. { Return type. }
  2102. if not(is_void(tprocdef(def).returndef)) then
  2103. append_labelentry_ref(DW_AT_type,def_dwarf_lab(tprocdef(def).returndef));
  2104. { we can only write the start/end if this procedure is implemented in
  2105. this module
  2106. }
  2107. if in_currentunit then
  2108. begin
  2109. { mark end of procedure }
  2110. current_asmdata.getlabel(procendlabel,alt_dbgtype);
  2111. current_asmdata.asmlists[al_procedures].insertbefore(tai_label.create(procendlabel),def.procendtai);
  2112. if use_dotted_functions then
  2113. procentry := '.' + def.mangledname
  2114. else
  2115. procentry := def.mangledname;
  2116. {$ifdef i8086}
  2117. append_seg_name(procentry);
  2118. {$endif i8086}
  2119. append_labelentry(DW_AT_low_pc,current_asmdata.RefAsmSymbol(procentry,AT_FUNCTION));
  2120. append_labelentry(DW_AT_high_pc,procendlabel);
  2121. if not(target_info.system in systems_darwin) then
  2122. begin
  2123. current_asmdata.asmlists[al_dwarf_aranges].Concat(
  2124. tai_const.create_type_sym(aitconst_ptr_unaligned,current_asmdata.RefAsmSymbol(procentry,AT_FUNCTION)));
  2125. {$ifdef i8086}
  2126. { bits 16..31 of the offset }
  2127. current_asmdata.asmlists[al_dwarf_aranges].concat(tai_const.Create_16bit_unaligned(0));
  2128. { segment }
  2129. current_asmdata.asmlists[al_dwarf_aranges].concat(tai_const.Create_seg_name(procentry));
  2130. {$endif i8086}
  2131. current_asmdata.asmlists[al_dwarf_aranges].Concat(
  2132. tai_const.Create_rel_sym(aitconst_ptr_unaligned,current_asmdata.RefAsmSymbol(procentry,AT_FUNCTION),procendlabel));
  2133. {$ifdef i8086}
  2134. { bits 16..31 of length }
  2135. current_asmdata.asmlists[al_dwarf_aranges].concat(tai_const.Create_16bit_unaligned(0));
  2136. {$endif i8086}
  2137. end;
  2138. end;
  2139. { Don't write the funcretsym explicitly, it's also in the
  2140. localsymtable and/or parasymtable.
  2141. }
  2142. finish_entry;
  2143. if assigned(def.parast) then
  2144. begin
  2145. { First insert self, because gdb uses the fact whether or not the
  2146. first parameter of a method is artificial to distinguish static
  2147. from regular methods. }
  2148. { fortunately, self is the always the first parameter in the
  2149. paralist, since it has the lowest paranr. Note that this is not
  2150. true for Objective-C, but those methods are detected in
  2151. another way (by reading the ObjC run time information) }
  2152. write_symtable_parasyms(current_asmdata.asmlists[al_dwarf_info],def.paras);
  2153. end;
  2154. { local type defs and vars should not be written
  2155. inside the main proc }
  2156. if in_currentunit and
  2157. assigned(def.localst) and
  2158. (def.localst.symtabletype=localsymtable) then
  2159. write_symtable_syms(current_asmdata.asmlists[al_dwarf_info],def.localst);
  2160. { last write the types from this procdef }
  2161. if assigned(def.parast) then
  2162. write_symtable_defs(current_asmdata.asmlists[al_dwarf_info],def.parast);
  2163. { only try to write the localst if the routine is implemented here }
  2164. if in_currentunit and
  2165. assigned(def.localst) and
  2166. (def.localst.symtabletype=localsymtable) then
  2167. begin
  2168. write_symtable_defs(current_asmdata.asmlists[al_dwarf_info],def.localst);
  2169. { Write nested procedures -- disabled, see scope check at the
  2170. beginning; currently, these are still written in the global
  2171. scope. }
  2172. // write_symtable_procdefs(current_asmdata.asmlists[al_dwarf_info],def.localst);
  2173. end;
  2174. finish_children;
  2175. end;
  2176. function TDebugInfoDwarf.get_symlist_sym_offset(symlist: ppropaccesslistitem; out sym: tabstractvarsym; out offset: pint): boolean;
  2177. var
  2178. elesize : pint;
  2179. currdef : tdef;
  2180. indirection: boolean;
  2181. begin
  2182. result:=false;
  2183. if not assigned(symlist) then
  2184. exit;
  2185. sym:=nil;
  2186. offset:=0;
  2187. currdef:=nil;
  2188. indirection:=false;
  2189. repeat
  2190. case symlist^.sltype of
  2191. sl_load:
  2192. begin
  2193. if assigned(sym) then
  2194. internalerror(2009031203);
  2195. if not(symlist^.sym.typ in [paravarsym,localvarsym,staticvarsym,fieldvarsym]) then
  2196. { can't handle... }
  2197. exit;
  2198. sym:=tabstractvarsym(symlist^.sym);
  2199. currdef:=tabstractvarsym(sym).vardef;
  2200. if ((sym.typ=paravarsym) and
  2201. paramanager.push_addr_param(tparavarsym(sym).varspez,sym.vardef,tprocdef(sym.owner.defowner).proccalloption)) then
  2202. indirection:=true;
  2203. end;
  2204. sl_subscript:
  2205. begin
  2206. if not assigned(currdef) then
  2207. internalerror(2009031301);
  2208. if (symlist^.sym.typ<>fieldvarsym) then
  2209. internalerror(2009031202);
  2210. { can't handle offsets with indirections yet }
  2211. if indirection then
  2212. exit;
  2213. if is_packed_record_or_object(currdef) then
  2214. begin
  2215. { can't calculate the address of a non-byte aligned field }
  2216. if (tfieldvarsym(symlist^.sym).fieldoffset mod 8) <> 0 then
  2217. exit;
  2218. inc(offset,tfieldvarsym(symlist^.sym).fieldoffset div 8)
  2219. end
  2220. else
  2221. inc(offset,tfieldvarsym(symlist^.sym).fieldoffset);
  2222. currdef:=tfieldvarsym(symlist^.sym).vardef;
  2223. end;
  2224. sl_absolutetype,
  2225. sl_typeconv:
  2226. begin
  2227. currdef:=symlist^.def;
  2228. { ignore, these don't change the address }
  2229. end;
  2230. sl_vec:
  2231. begin
  2232. if not assigned(currdef) or
  2233. (currdef.typ<>arraydef) then
  2234. internalerror(2009031201);
  2235. { can't handle offsets with indirections yet }
  2236. if indirection then
  2237. exit;
  2238. if not is_packed_array(currdef) then
  2239. elesize:=tarraydef(currdef).elesize
  2240. else
  2241. begin
  2242. elesize:=tarraydef(currdef).elepackedbitsize;
  2243. { can't calculate the address of a non-byte aligned element }
  2244. if (elesize mod 8)<>0 then
  2245. exit;
  2246. elesize:=elesize div 8;
  2247. end;
  2248. inc(offset,(symlist^.value.svalue-tarraydef(currdef).lowrange)*elesize);
  2249. currdef:=tarraydef(currdef).elementdef;
  2250. end;
  2251. else
  2252. internalerror(2009031401);
  2253. end;
  2254. symlist:=symlist^.next;
  2255. until not assigned(symlist);
  2256. if not assigned(sym) then
  2257. internalerror(2009031205);
  2258. result:=true;
  2259. end;
  2260. procedure TDebugInfoDwarf.appendsym_var(list:TAsmList;sym:tabstractnormalvarsym);
  2261. begin
  2262. appendsym_var_with_name_type_offset(list,sym,symname(sym, false),sym.vardef,0,[]);
  2263. end;
  2264. procedure TDebugInfoDwarf.appendsym_var_with_name_type_offset(list:TAsmList; sym:tabstractnormalvarsym; const name: string; def: tdef; offset: pint; const flags: tdwarfvarsymflags);
  2265. var
  2266. templist : TAsmList;
  2267. blocksize,size_of_int : longint;
  2268. tag : tdwarf_tag;
  2269. has_high_reg : boolean;
  2270. dreg,dreghigh : shortint;
  2271. {$ifdef i8086}
  2272. has_segment_sym_name : boolean=false;
  2273. segment_sym_name : TSymStr='';
  2274. segment_reg: TRegister=NR_NO;
  2275. {$endif i8086}
  2276. begin
  2277. blocksize:=0;
  2278. dreghigh:=0;
  2279. { external symbols can't be resolved at link time, so we
  2280. can't generate stabs for them
  2281. not sure if this applies to dwarf as well (FK)
  2282. }
  2283. if vo_is_external in sym.varoptions then
  2284. exit;
  2285. { There is no space allocated for not referenced locals }
  2286. if (sym.owner.symtabletype=localsymtable) and (sym.refs=0) then
  2287. exit;
  2288. templist:=TAsmList.create;
  2289. case sym.localloc.loc of
  2290. LOC_REGISTER,
  2291. LOC_CREGISTER,
  2292. LOC_MMREGISTER,
  2293. LOC_CMMREGISTER,
  2294. LOC_FPUREGISTER,
  2295. LOC_CFPUREGISTER :
  2296. begin
  2297. { dwarf_reg_no_error might return -1
  2298. in case the register variable has been optimized out }
  2299. dreg:=dwarf_reg_no_error(sym.localloc.register);
  2300. has_high_reg:=(sym.localloc.loc in [LOC_REGISTER,LOC_CREGISTER]) and (sym.localloc.registerhi<>NR_NO);
  2301. if has_high_reg then
  2302. dreghigh:=dwarf_reg_no_error(sym.localloc.registerhi);
  2303. if dreghigh=-1 then
  2304. has_high_reg:=false;
  2305. if (sym.localloc.loc in [LOC_REGISTER,LOC_CREGISTER]) and
  2306. (sym.typ=paravarsym) and
  2307. paramanager.push_addr_param(sym.varspez,sym.vardef,tprocdef(sym.owner.defowner).proccalloption) and
  2308. not(vo_has_local_copy in sym.varoptions) and
  2309. not is_open_string(sym.vardef) and (dreg>=0) then
  2310. begin
  2311. templist.concat(tai_const.create_8bit(ord(DW_OP_bregx)));
  2312. templist.concat(tai_const.create_uleb128bit(dreg));
  2313. templist.concat(tai_const.create_sleb128bit(0));
  2314. blocksize:=1+Lengthuleb128(dreg)+LengthSleb128(0);
  2315. end
  2316. else
  2317. begin
  2318. if has_high_reg then
  2319. begin
  2320. templist.concat(tai_comment.create(strpnew('high:low reg pair variable')));
  2321. size_of_int:=sizeof(aint);
  2322. templist.concat(tai_const.create_8bit(ord(DW_OP_regx)));
  2323. templist.concat(tai_const.create_uleb128bit(dreg));
  2324. blocksize:=1+Lengthuleb128(dreg);
  2325. templist.concat(tai_const.create_8bit(ord(DW_OP_piece)));
  2326. templist.concat(tai_const.create_uleb128bit(size_of_int));
  2327. blocksize:=blocksize+1+Lengthuleb128(size_of_int);
  2328. templist.concat(tai_const.create_8bit(ord(DW_OP_regx)));
  2329. templist.concat(tai_const.create_uleb128bit(dreghigh));
  2330. blocksize:=blocksize+1+Lengthuleb128(dreghigh);
  2331. templist.concat(tai_const.create_8bit(ord(DW_OP_piece)));
  2332. templist.concat(tai_const.create_uleb128bit(size_of_int));
  2333. blocksize:=blocksize+1+Lengthuleb128(size_of_int);
  2334. end
  2335. else if (dreg>=0) then
  2336. begin
  2337. templist.concat(tai_const.create_8bit(ord(DW_OP_regx)));
  2338. templist.concat(tai_const.create_uleb128bit(dreg));
  2339. blocksize:=1+Lengthuleb128(dreg);
  2340. end;
  2341. end;
  2342. end;
  2343. else
  2344. begin
  2345. case sym.typ of
  2346. staticvarsym:
  2347. begin
  2348. if (vo_is_thread_var in sym.varoptions) then
  2349. begin
  2350. { TODO: !!! FIXME: dwarf for thread vars !!!}
  2351. { This is only a minimal change to at least be able to get a value
  2352. in only one thread is present PM 2014-11-21, like for stabs format }
  2353. templist.concat(tai_const.create_8bit(ord(DW_OP_addr)));
  2354. templist.concat(tai_const.Create_type_name(aitconst_ptr_unaligned,sym.mangledname,
  2355. offset+sizeof(pint)));
  2356. blocksize:=1+sizeof(puint);
  2357. end
  2358. else
  2359. begin
  2360. templist.concat(tai_const.create_8bit(ord(DW_OP_addr)));
  2361. templist.concat(tai_const.Create_type_name(aitconst_ptr_unaligned,sym.mangledname,offset));
  2362. blocksize:=1+sizeof(puint);
  2363. {$ifdef i8086}
  2364. segment_sym_name:=sym.mangledname;
  2365. has_segment_sym_name:=true;
  2366. {$endif i8086}
  2367. end;
  2368. end;
  2369. paravarsym,
  2370. localvarsym:
  2371. begin
  2372. { Happens when writing debug info for paras of procdefs not
  2373. implemented in the current module. Can't add a general check
  2374. for LOC_INVALID above, because staticvarsyms may also have it.
  2375. }
  2376. if sym.localloc.loc<> LOC_INVALID then
  2377. begin
  2378. if is_fbreg(sym.localloc.reference.base) then
  2379. begin
  2380. templist.concat(tai_const.create_8bit(ord(DW_OP_fbreg)));
  2381. templist.concat(tai_const.create_sleb128bit(sym.localloc.reference.offset+offset));
  2382. blocksize:=1+Lengthsleb128(sym.localloc.reference.offset+offset);
  2383. end
  2384. else
  2385. begin
  2386. dreg:=dwarf_reg(sym.localloc.reference.base);
  2387. if dreg<=31 then
  2388. begin
  2389. templist.concat(tai_const.create_8bit(ord(DW_OP_breg0)+dreg));
  2390. templist.concat(tai_const.create_sleb128bit(sym.localloc.reference.offset+offset));
  2391. blocksize:=1+Lengthsleb128(sym.localloc.reference.offset+offset);
  2392. end
  2393. else
  2394. begin
  2395. templist.concat(tai_const.create_8bit(ord(DW_OP_bregx)));
  2396. templist.concat(tai_const.create_uleb128bit(dreg));
  2397. templist.concat(tai_const.create_sleb128bit(sym.localloc.reference.offset+offset));
  2398. blocksize:=1+Lengthuleb128(dreg)+LengthSleb128(sym.localloc.reference.offset+offset);
  2399. end;
  2400. end;
  2401. {$ifdef i8086}
  2402. segment_reg:=sym.localloc.reference.segment;
  2403. {$endif i8086}
  2404. {$ifndef gdb_supports_DW_AT_variable_parameter}
  2405. { Parameters which are passed by reference. (var and the like)
  2406. Hide the reference-pointer and dereference the pointer
  2407. in the DW_AT_location block.
  2408. }
  2409. if (sym.typ=paravarsym) and
  2410. paramanager.push_addr_param(sym.varspez,sym.vardef,tprocdef(sym.owner.defowner).proccalloption) and
  2411. not(vo_has_local_copy in sym.varoptions) and
  2412. not is_open_string(sym.vardef) then
  2413. begin
  2414. templist.concat(tai_const.create_8bit(ord(DW_OP_deref)));
  2415. inc(blocksize);
  2416. end
  2417. {$endif not gdb_supports_DW_AT_variable_parameter}
  2418. end;
  2419. end
  2420. else
  2421. internalerror(200601288);
  2422. end;
  2423. end;
  2424. end;
  2425. { function results must not be added to the parameter list,
  2426. as they are not part of the signature of the function
  2427. (gdb automatically adds them according to the ABI specifications
  2428. when calling the function)
  2429. }
  2430. if (sym.typ=paravarsym) and
  2431. not(dvf_force_local_var in flags) and
  2432. not(vo_is_funcret in sym.varoptions) then
  2433. tag:=DW_TAG_formal_parameter
  2434. else
  2435. tag:=DW_TAG_variable;
  2436. { must be parasym of externally implemented procdef, but
  2437. the parasymtable can con also contain e.g. absolutevarsyms
  2438. -> check symtabletype}
  2439. if (sym.owner.symtabletype=parasymtable) and
  2440. (sym.localloc.loc=LOC_INVALID) then
  2441. begin
  2442. if (sym.owner.symtabletype<>parasymtable) then
  2443. internalerror(2009101001);
  2444. append_entry(tag,false,[
  2445. DW_AT_name,DW_FORM_string,name+#0
  2446. {
  2447. DW_AT_decl_file,DW_FORM_data1,0,
  2448. DW_AT_decl_line,DW_FORM_data1,
  2449. }
  2450. ])
  2451. end
  2452. else if not(sym.localloc.loc in [LOC_REGISTER,LOC_CREGISTER,LOC_MMREGISTER,
  2453. LOC_CMMREGISTER,LOC_FPUREGISTER,LOC_CFPUREGISTER]) and
  2454. ((sym.owner.symtabletype = globalsymtable) or
  2455. (sp_static in sym.symoptions) or
  2456. (vo_is_public in sym.varoptions)) then
  2457. append_entry(tag,false,[
  2458. DW_AT_name,DW_FORM_string,name+#0,
  2459. {
  2460. DW_AT_decl_file,DW_FORM_data1,0,
  2461. DW_AT_decl_line,DW_FORM_data1,
  2462. }
  2463. DW_AT_external,DW_FORM_flag,true,
  2464. { data continues below }
  2465. DW_AT_location,DW_FORM_block1,blocksize
  2466. ])
  2467. {$ifdef gdb_supports_DW_AT_variable_parameter}
  2468. else if (sym.typ=paravarsym) and
  2469. paramanager.push_addr_param(sym.varspez,sym.vardef,tprocdef(sym.owner.defowner).proccalloption) and
  2470. not(vo_has_local_copy in sym.varoptions) and
  2471. not is_open_string(sym.vardef) then
  2472. append_entry(tag,false,[
  2473. DW_AT_name,DW_FORM_string,name+#0,
  2474. DW_AT_variable_parameter,DW_FORM_flag,true,
  2475. {
  2476. DW_AT_decl_file,DW_FORM_data1,0,
  2477. DW_AT_decl_line,DW_FORM_data1,
  2478. }
  2479. { data continues below }
  2480. DW_AT_location,DW_FORM_block1,blocksize
  2481. ])
  2482. {$endif gdb_supports_DW_AT_variable_parameter}
  2483. else
  2484. append_entry(tag,false,[
  2485. DW_AT_name,DW_FORM_string,name+#0,
  2486. {
  2487. DW_AT_decl_file,DW_FORM_data1,0,
  2488. DW_AT_decl_line,DW_FORM_data1,
  2489. }
  2490. { data continues below }
  2491. DW_AT_location,DW_FORM_block1,blocksize
  2492. ]);
  2493. { append block data }
  2494. current_asmdata.asmlists[al_dwarf_info].concatlist(templist);
  2495. { Mark self as artificial for methods, because gdb uses the fact
  2496. whether or not the first parameter of a method is artificial to
  2497. distinguish regular from static methods (since there are no
  2498. no vo_is_self parameters for static methods, we don't have to check
  2499. that). }
  2500. if (vo_is_self in sym.varoptions) then
  2501. append_attribute(DW_AT_artificial,DW_FORM_flag,[true]);
  2502. append_labelentry_ref(DW_AT_type,def_dwarf_lab(def));
  2503. {$ifdef i8086}
  2504. if has_segment_sym_name then
  2505. append_seg_name(segment_sym_name)
  2506. else if segment_reg<>NR_NO then
  2507. append_seg_reg(segment_reg);
  2508. {$endif i8086}
  2509. templist.free;
  2510. finish_entry;
  2511. end;
  2512. procedure TDebugInfoDwarf.appendsym_staticvar(list:TAsmList;sym:tstaticvarsym);
  2513. begin
  2514. appendsym_var(list,sym);
  2515. end;
  2516. procedure TDebugInfoDwarf.appendsym_localvar(list:TAsmList;sym:tlocalvarsym);
  2517. begin
  2518. appendsym_var(list,sym);
  2519. end;
  2520. procedure TDebugInfoDwarf.appendsym_paravar(list:TAsmList;sym:tparavarsym);
  2521. begin
  2522. appendsym_var(list,sym);
  2523. end;
  2524. procedure TDebugInfoDwarf.appendsym_fieldvar(list:TAsmList;sym: tfieldvarsym);
  2525. begin
  2526. appendsym_fieldvar_with_name_offset(list,sym,symname(sym, false),sym.vardef,0);
  2527. end;
  2528. procedure TDebugInfoDwarf.appendsym_fieldvar_with_name_offset(list:TAsmList;sym: tfieldvarsym;const name: string; def: tdef; offset: pint);
  2529. var
  2530. bitoffset,
  2531. fieldoffset,
  2532. fieldnatsize: asizeint;
  2533. begin
  2534. if (sp_static in sym.symoptions) or
  2535. (sym.visibility=vis_hidden) then
  2536. exit;
  2537. if (tabstractrecordsymtable(sym.owner).usefieldalignment<>bit_alignment) or
  2538. { only ordinals are bitpacked }
  2539. not is_ordinal(sym.vardef) then
  2540. begin
  2541. { other kinds of fields can however also appear in a bitpacked }
  2542. { record, and then their offset is also specified in bits rather }
  2543. { than in bytes }
  2544. if (tabstractrecordsymtable(sym.owner).usefieldalignment<>bit_alignment) then
  2545. fieldoffset:=sym.fieldoffset
  2546. else
  2547. fieldoffset:=sym.fieldoffset div 8;
  2548. inc(fieldoffset,offset);
  2549. append_entry(DW_TAG_member,false,[
  2550. DW_AT_name,DW_FORM_string,name+#0,
  2551. DW_AT_data_member_location,DW_FORM_block1,1+lengthuleb128(fieldoffset)
  2552. ]);
  2553. end
  2554. else
  2555. begin
  2556. if (sym.vardef.packedbitsize > 255) then
  2557. internalerror(2007061201);
  2558. { we don't bitpack according to the ABI, but as close as }
  2559. { possible, i.e., equivalent to gcc's }
  2560. { __attribute__((__packed__)), which is also what gpc }
  2561. { does. }
  2562. fieldnatsize:=max(sizeof(pint),sym.vardef.size);
  2563. fieldoffset:=(sym.fieldoffset div (fieldnatsize*8)) * fieldnatsize;
  2564. inc(fieldoffset,offset);
  2565. bitoffset:=sym.fieldoffset mod (fieldnatsize*8);
  2566. if (target_info.endian=endian_little) then
  2567. bitoffset:=(fieldnatsize*8)-bitoffset-sym.vardef.packedbitsize;
  2568. append_entry(DW_TAG_member,false,[
  2569. DW_AT_name,DW_FORM_string,symname(sym, false)+#0,
  2570. { gcc also generates both a bit and byte size attribute }
  2571. { we don't support ordinals >= 256 bits }
  2572. DW_AT_byte_size,DW_FORM_data1,fieldnatsize,
  2573. { nor >= 256 bits (not yet, anyway, see IE above) }
  2574. DW_AT_bit_size,DW_FORM_data1,sym.vardef.packedbitsize,
  2575. { data1 and data2 are unsigned, bitoffset can also be negative }
  2576. DW_AT_bit_offset,DW_FORM_data4,bitoffset,
  2577. DW_AT_data_member_location,DW_FORM_block1,1+lengthuleb128(fieldoffset)
  2578. ]);
  2579. end;
  2580. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_plus_uconst)));
  2581. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_uleb128bit(fieldoffset));
  2582. if (sym.owner.symtabletype in [objectsymtable,recordsymtable]) then
  2583. append_visibility(sym.visibility);
  2584. append_labelentry_ref(DW_AT_type,def_dwarf_lab(def));
  2585. finish_entry;
  2586. end;
  2587. procedure TDebugInfoDwarf.appendsym_const(list:TAsmList;sym:tconstsym);
  2588. begin
  2589. appendsym_const_member(list,sym,false);
  2590. end;
  2591. procedure TDebugInfoDwarf.appendsym_const_member(list:TAsmList;sym:tconstsym;ismember:boolean);
  2592. var
  2593. i,
  2594. size: aint;
  2595. usedef: tdef;
  2596. begin
  2597. { These are default values of parameters. These should be encoded
  2598. via DW_AT_default_value, not as a separate sym. Moreover, their
  2599. type is not available when writing the debug info for external
  2600. procedures.
  2601. }
  2602. if (sym.owner.symtabletype=parasymtable) then
  2603. exit;
  2604. if ismember then
  2605. append_entry(DW_TAG_member,false,[
  2606. DW_AT_name,DW_FORM_string,symname(sym, false)+#0,
  2607. { The DW_AT_declaration tag is invalid according to the DWARF specifications.
  2608. But gcc adds this to static const members and gdb checks
  2609. for this flag. So we have to set it also.
  2610. }
  2611. DW_AT_declaration,DW_FORM_flag,true,
  2612. DW_AT_external,DW_FORM_flag,true
  2613. ])
  2614. else
  2615. append_entry(DW_TAG_variable,false,[
  2616. DW_AT_name,DW_FORM_string,symname(sym, false)+#0
  2617. ]);
  2618. { for string constants, constdef isn't set because they have no real type }
  2619. case sym.consttyp of
  2620. conststring:
  2621. begin
  2622. { if DW_FORM_string is used below one day, this usedef should
  2623. probably become nil }
  2624. { note: < 255 instead of <= 255 because we have to store the
  2625. entire length of the string as well, and 256 does not fit in
  2626. a byte }
  2627. if (sym.value.len<255) then
  2628. usedef:=cshortstringtype
  2629. else
  2630. usedef:=clongstringtype;
  2631. end;
  2632. constresourcestring,
  2633. constwstring:
  2634. usedef:=nil;
  2635. else
  2636. usedef:=sym.constdef;
  2637. end;
  2638. if assigned(usedef) then
  2639. append_labelentry_ref(DW_AT_type,def_dwarf_lab(usedef));
  2640. AddConstToAbbrev(ord(DW_AT_const_value));
  2641. case sym.consttyp of
  2642. conststring:
  2643. begin
  2644. { DW_FORM_string isn't supported yet by the Pascal value printer
  2645. -> create a string using raw bytes }
  2646. if (sym.value.len<255) then
  2647. begin
  2648. AddConstToAbbrev(ord(DW_FORM_block1));
  2649. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(sym.value.len+1));
  2650. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(sym.value.len));
  2651. end
  2652. else
  2653. begin
  2654. AddConstToAbbrev(ord(DW_FORM_block));
  2655. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_uleb128bit(sym.value.len+sizesinttype.size));
  2656. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.Create_sizeint_unaligned(sym.value.len));
  2657. end;
  2658. i:=0;
  2659. size:=sym.value.len;
  2660. while(i<size) do
  2661. begin
  2662. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit((pbyte(sym.value.valueptr+i)^)));
  2663. inc(i);
  2664. end;
  2665. end;
  2666. constguid,
  2667. constset:
  2668. begin
  2669. AddConstToAbbrev(ord(DW_FORM_block1));
  2670. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(usedef.size));
  2671. i:=0;
  2672. size:=sym.constdef.size;
  2673. while (i<size) do
  2674. begin
  2675. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit((pbyte(sym.value.valueptr+i)^)));
  2676. inc(i);
  2677. end;
  2678. end;
  2679. constwstring,
  2680. constresourcestring:
  2681. begin
  2682. { write dummy for now }
  2683. AddConstToAbbrev(ord(DW_FORM_string));
  2684. current_asmdata.asmlists[al_dwarf_info].concat(tai_string.create(''));
  2685. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(0));
  2686. end;
  2687. constord:
  2688. begin
  2689. if (sym.value.valueord<0) then
  2690. begin
  2691. AddConstToAbbrev(ord(DW_FORM_sdata));
  2692. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_sleb128bit(sym.value.valueord.svalue));
  2693. end
  2694. else
  2695. begin
  2696. AddConstToAbbrev(ord(DW_FORM_udata));
  2697. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_uleb128bit(sym.value.valueord.uvalue));
  2698. end;
  2699. end;
  2700. constnil:
  2701. begin
  2702. {$ifdef cpu64bitaddr}
  2703. AddConstToAbbrev(ord(DW_FORM_data8));
  2704. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_64bit_unaligned(0));
  2705. {$else cpu64bitaddr}
  2706. AddConstToAbbrev(ord(DW_FORM_data4));
  2707. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_32bit_unaligned(0));
  2708. {$endif cpu64bitaddr}
  2709. end;
  2710. constpointer:
  2711. begin
  2712. {$ifdef cpu64bitaddr}
  2713. AddConstToAbbrev(ord(DW_FORM_data8));
  2714. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_64bit_unaligned(int64(sym.value.valueordptr)));
  2715. {$else cpu64bitaddr}
  2716. AddConstToAbbrev(ord(DW_FORM_data4));
  2717. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_32bit_unaligned(longint(sym.value.valueordptr)));
  2718. {$endif cpu64bitaddr}
  2719. end;
  2720. constreal:
  2721. begin
  2722. AddConstToAbbrev(ord(DW_FORM_block1));
  2723. case tfloatdef(sym.constdef).floattype of
  2724. s32real:
  2725. begin
  2726. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(4));
  2727. current_asmdata.asmlists[al_dwarf_info].concat(tai_realconst.create_s32real(pbestreal(sym.value.valueptr)^));
  2728. end;
  2729. s64real:
  2730. begin
  2731. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(8));
  2732. current_asmdata.asmlists[al_dwarf_info].concat(tai_realconst.create_s64real(pbestreal(sym.value.valueptr)^));
  2733. end;
  2734. s64comp,
  2735. s64currency:
  2736. begin
  2737. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(8));
  2738. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_64bit_unaligned(trunc(pbestreal(sym.value.valueptr)^)));
  2739. end;
  2740. s80real,
  2741. sc80real:
  2742. begin
  2743. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(sym.constdef.size));
  2744. current_asmdata.asmlists[al_dwarf_info].concat(tai_realconst.create_s80real(pextended(sym.value.valueptr)^,sym.constdef.size));
  2745. end;
  2746. else
  2747. internalerror(200601291);
  2748. end;
  2749. end;
  2750. else
  2751. internalerror(200601292);
  2752. end;
  2753. finish_entry;
  2754. end;
  2755. procedure TDebugInfoDwarf.appendsym_label(list:TAsmList;sym: tlabelsym);
  2756. begin
  2757. { ignore label syms for now, the problem is that a label sym
  2758. can have more than one label associated e.g. in case of
  2759. an inline procedure expansion }
  2760. end;
  2761. procedure TDebugInfoDwarf.appendsym_property(list:TAsmList;sym: tpropertysym);
  2762. var
  2763. symlist: ppropaccesslistitem;
  2764. tosym: tabstractvarsym;
  2765. offset: pint;
  2766. begin
  2767. if assigned(sym.propaccesslist[palt_read]) and
  2768. not assigned(sym.propaccesslist[palt_read].procdef) then
  2769. symlist:=sym.propaccesslist[palt_read].firstsym
  2770. else
  2771. { can't handle }
  2772. exit;
  2773. if not get_symlist_sym_offset(symlist,tosym,offset) then
  2774. exit;
  2775. if not (tosym.owner.symtabletype in [objectsymtable,recordsymtable]) then
  2776. begin
  2777. if (tosym.typ=fieldvarsym) then
  2778. internalerror(2009031404);
  2779. appendsym_var_with_name_type_offset(list,tabstractnormalvarsym(tosym),symname(sym, false),sym.propdef,offset,[])
  2780. end
  2781. else
  2782. appendsym_fieldvar_with_name_offset(list,tfieldvarsym(tosym),symname(sym, false),sym.propdef,offset)
  2783. end;
  2784. function TDebugInfoDwarf.symdebugname(sym: tsym): String;
  2785. begin
  2786. result := sym.name;
  2787. end;
  2788. procedure TDebugInfoDwarf.appendsym_type(list:TAsmList;sym: ttypesym);
  2789. begin
  2790. { just queue the def if needed, beforeappenddef will
  2791. emit the typedef if necessary }
  2792. def_dwarf_lab(sym.typedef);
  2793. end;
  2794. procedure TDebugInfoDwarf.appendsym_absolute(list:TAsmList;sym:tabsolutevarsym);
  2795. var
  2796. templist : TAsmList;
  2797. blocksize : longint;
  2798. symlist : ppropaccesslistitem;
  2799. tosym: tabstractvarsym;
  2800. offset: pint;
  2801. flags: tdwarfvarsymflags;
  2802. begin
  2803. templist:=TAsmList.create;
  2804. case tabsolutevarsym(sym).abstyp of
  2805. toaddr :
  2806. begin
  2807. { MWE: replaced ifdef i368 }
  2808. (*
  2809. if target_cpu = cpu_i386 then
  2810. begin
  2811. { in theory, we could write a DW_AT_segment entry here for sym.absseg,
  2812. however I doubt that gdb supports this (FK) }
  2813. end;
  2814. *)
  2815. templist.concat(tai_const.create_8bit(3));
  2816. templist.concat(tai_const.create_int_dataptr_unaligned(sym.addroffset));
  2817. blocksize:=1+sizeof(puint);
  2818. end;
  2819. toasm :
  2820. begin
  2821. templist.concat(tai_const.create_8bit(3));
  2822. templist.concat(tai_const.create_type_name(aitconst_ptr_unaligned,sym.mangledname,0));
  2823. blocksize:=1+sizeof(puint);
  2824. end;
  2825. tovar:
  2826. begin
  2827. symlist:=tabsolutevarsym(sym).ref.firstsym;
  2828. if get_symlist_sym_offset(symlist,tosym,offset) then
  2829. begin
  2830. if (tosym.typ=fieldvarsym) then
  2831. internalerror(2009031402);
  2832. flags:=[];
  2833. if (sym.owner.symtabletype=localsymtable) then
  2834. include(flags,dvf_force_local_var);
  2835. appendsym_var_with_name_type_offset(list,tabstractnormalvarsym(tosym),symname(sym, false),tabstractvarsym(sym).vardef,offset,flags);
  2836. end;
  2837. templist.free;
  2838. exit;
  2839. end;
  2840. else
  2841. internalerror(2013120111);
  2842. end;
  2843. append_entry(DW_TAG_variable,false,[
  2844. DW_AT_name,DW_FORM_string,symname(sym, false)+#0,
  2845. {
  2846. DW_AT_decl_file,DW_FORM_data1,0,
  2847. DW_AT_decl_line,DW_FORM_data1,
  2848. }
  2849. DW_AT_external,DW_FORM_flag,true,
  2850. { data continues below }
  2851. DW_AT_location,DW_FORM_block1,blocksize
  2852. ]);
  2853. { append block data }
  2854. current_asmdata.asmlists[al_dwarf_info].concatlist(templist);
  2855. append_labelentry_ref(DW_AT_type,def_dwarf_lab(sym.vardef));
  2856. templist.free;
  2857. finish_entry;
  2858. end;
  2859. procedure TDebugInfoDwarf.beforeappendsym(list:TAsmList;sym:tsym);
  2860. begin
  2861. current_asmdata.asmlists[al_dwarf_info].concat(tai_comment.Create(strpnew('Symbol '+symname(sym, true))));
  2862. end;
  2863. procedure TDebugInfoDwarf.insertmoduleinfo;
  2864. var
  2865. templist: TAsmList;
  2866. linelist: TAsmList;
  2867. lbl : tasmlabel;
  2868. n,m : Integer;
  2869. ditem : TDirIndexItem;
  2870. fitem : TFileIndexItem;
  2871. flist : TFPList;
  2872. dbgname : String;
  2873. begin
  2874. { insert DEBUGSTART and DEBUGEND labels }
  2875. dbgname:=make_mangledname('DEBUGSTART',current_module.localsymtable,'');
  2876. { Darwin's linker does not like two global labels both pointing to the
  2877. end of a section, which can happen in case of units without code ->
  2878. make them local; we don't need the debugtable stuff there either,
  2879. so it doesn't matter that they are not global.
  2880. }
  2881. if (target_info.system in systems_darwin) then
  2882. dbgname:='L'+dbgname;
  2883. new_section(current_asmdata.asmlists[al_start],sec_code,dbgname,0,secorder_begin);
  2884. if not(target_info.system in systems_darwin) then
  2885. current_asmdata.asmlists[al_start].concat(tai_symbol.Createname_global(dbgname,AT_METADATA,0,voidpointertype))
  2886. else
  2887. current_asmdata.asmlists[al_start].concat(tai_symbol.Createname(dbgname,AT_METADATA,0,voidpointertype));
  2888. dbgname:=make_mangledname('DEBUGEND',current_module.localsymtable,'');
  2889. { See above. }
  2890. if (target_info.system in systems_darwin) then
  2891. dbgname:='L'+dbgname;
  2892. new_section(current_asmdata.asmlists[al_end],sec_code,dbgname,0,secorder_end);
  2893. if not(target_info.system in systems_darwin) then
  2894. current_asmdata.asmlists[al_end].concat(tai_symbol.Createname_global(dbgname,AT_METADATA,0,voidpointertype))
  2895. else
  2896. current_asmdata.asmlists[al_end].concat(tai_symbol.Createname(dbgname,AT_METADATA,0,voidpointertype));
  2897. { insert .Ldebug_abbrev0 label }
  2898. templist:=TAsmList.create;
  2899. new_section(templist,sec_debug_abbrev,'',0);
  2900. templist.concat(tai_symbol.createname(target_asm.labelprefix+'debug_abbrevsection0',AT_METADATA,0,voidpointertype));
  2901. { add any extra stuff which needs to be in the abbrev section, but before }
  2902. { the actual abbreviations, in between the symbol above and below, i.e. here }
  2903. templist.concat(tai_symbol.createname(target_asm.labelprefix+'debug_abbrev0',AT_METADATA,0,voidpointertype));
  2904. current_asmdata.asmlists[al_start].insertlist(templist);
  2905. templist.free;
  2906. { insert .Ldebug_line0 label }
  2907. templist:=TAsmList.create;
  2908. new_section(templist,sec_debug_line,'',0);
  2909. templist.concat(tai_symbol.createname(target_asm.labelprefix+'debug_linesection0',AT_METADATA,0,voidpointertype));
  2910. { add any extra stuff which needs to be in the line section, but before }
  2911. { the actual line info, in between the symbol above and below, i.e. here }
  2912. templist.concat(tai_symbol.createname(target_asm.labelprefix+'debug_line0',AT_METADATA,0,voidpointertype));
  2913. current_asmdata.asmlists[al_start].insertlist(templist);
  2914. templist.free;
  2915. { finalize line info if the unit doesn't contain any function/ }
  2916. { procedure/init/final code }
  2917. finish_lineinfo;
  2918. { debug line header }
  2919. linelist := current_asmdata.asmlists[al_dwarf_line];
  2920. new_section(linelist,sec_debug_line,'',0);
  2921. linelist.concat(tai_comment.Create(strpnew('=== header start ===')));
  2922. { size }
  2923. current_asmdata.getlabel(lbl,alt_dbgfile);
  2924. if use_64bit_headers then
  2925. linelist.concat(tai_const.create_32bit_unaligned(longint($FFFFFFFF)));
  2926. linelist.concat(tai_const.create_rel_sym(offsetreltype,
  2927. lbl,current_asmdata.DefineAsmSymbol(target_asm.labelprefix+'edebug_line0',AB_LOCAL,AT_METADATA,voidpointertype)));
  2928. linelist.concat(tai_label.create(lbl));
  2929. { version }
  2930. linelist.concat(tai_const.create_16bit_unaligned(dwarf_version));
  2931. { header length }
  2932. current_asmdata.getlabel(lbl,alt_dbgfile);
  2933. linelist.concat(tai_const.create_rel_sym(offsetreltype,
  2934. lbl,current_asmdata.DefineAsmSymbol(target_asm.labelprefix+'ehdebug_line0',AB_LOCAL,AT_METADATA,voidpointertype)));
  2935. linelist.concat(tai_label.create(lbl));
  2936. { minimum_instruction_length }
  2937. linelist.concat(tai_const.create_8bit(1));
  2938. { default_is_stmt }
  2939. linelist.concat(tai_const.create_8bit(1));
  2940. { line_base }
  2941. linelist.concat(tai_const.create_8bit(LINE_BASE));
  2942. { line_range }
  2943. { only line increase, no adress }
  2944. linelist.concat(tai_const.create_8bit(255));
  2945. { opcode_base }
  2946. linelist.concat(tai_const.create_8bit(OPCODE_BASE));
  2947. { standard_opcode_lengths }
  2948. { MWE: sigh... why adding the default lengths (and make those sizes sense with LEB encoding) }
  2949. { DW_LNS_copy }
  2950. linelist.concat(tai_const.create_8bit(0));
  2951. { DW_LNS_advance_pc }
  2952. linelist.concat(tai_const.create_8bit(1));
  2953. { DW_LNS_advance_line }
  2954. linelist.concat(tai_const.create_8bit(1));
  2955. { DW_LNS_set_file }
  2956. linelist.concat(tai_const.create_8bit(1));
  2957. { DW_LNS_set_column }
  2958. linelist.concat(tai_const.create_8bit(1));
  2959. { DW_LNS_negate_stmt }
  2960. linelist.concat(tai_const.create_8bit(0));
  2961. { DW_LNS_set_basic_block }
  2962. linelist.concat(tai_const.create_8bit(0));
  2963. { DW_LNS_const_add_pc }
  2964. linelist.concat(tai_const.create_8bit(0));
  2965. { DW_LNS_fixed_advance_pc }
  2966. linelist.concat(tai_const.create_8bit(1));
  2967. { DW_LNS_set_prologue_end }
  2968. linelist.concat(tai_const.create_8bit(0));
  2969. { DW_LNS_set_epilogue_begin }
  2970. linelist.concat(tai_const.create_8bit(0));
  2971. { DW_LNS_set_isa }
  2972. linelist.concat(tai_const.create_8bit(1));
  2973. { Create single list of filenames sorted in IndexNr }
  2974. flist:=TFPList.Create;
  2975. for n := 0 to dirlist.Count - 1 do
  2976. begin
  2977. ditem := TDirIndexItem(dirlist[n]);
  2978. for m := 0 to ditem.Files.Count - 1 do
  2979. flist.Add(ditem.Files[m]);
  2980. end;
  2981. flist.Sort(@FileListSortCompare);
  2982. { include_directories }
  2983. linelist.concat(tai_comment.Create(strpnew('include_directories')));
  2984. for n := 0 to dirlist.Count - 1 do
  2985. begin
  2986. ditem := TDirIndexItem(dirlist[n]);
  2987. if ditem.Name = '.' then
  2988. Continue;
  2989. { Write without trailing path delimiter and also don't prefix with ./ for current dir (already done while adding to dirlist }
  2990. linelist.concat(tai_string.create(ditem.Name+#0));
  2991. end;
  2992. linelist.concat(tai_const.create_8bit(0));
  2993. { file_names }
  2994. linelist.concat(tai_comment.Create(strpnew('file_names')));
  2995. for n := 0 to flist.Count - 1 do
  2996. begin
  2997. fitem := TFileIndexItem(flist[n]);
  2998. { file name }
  2999. linelist.concat(tai_string.create(fitem.Name+#0));
  3000. { directory index }
  3001. linelist.concat(tai_const.create_uleb128bit(fitem.DirIndex));
  3002. { last modification }
  3003. linelist.concat(tai_const.create_uleb128bit(0));
  3004. { file length }
  3005. linelist.concat(tai_const.create_uleb128bit(0));
  3006. end;
  3007. linelist.concat(tai_const.create_8bit(0));
  3008. { end of debug line header }
  3009. linelist.concat(tai_symbol.createname(target_asm.labelprefix+'ehdebug_line0',AT_METADATA,0,voidpointertype));
  3010. linelist.concat(tai_comment.Create(strpnew('=== header end ===')));
  3011. { add line program }
  3012. linelist.concatList(asmline);
  3013. { end of debug line table }
  3014. linelist.concat(tai_symbol.createname(target_asm.labelprefix+'edebug_line0',AT_METADATA,0,voidpointertype));
  3015. flist.free;
  3016. end;
  3017. procedure TDebugInfoDwarf.inserttypeinfo;
  3018. var
  3019. storefilepos : tfileposinfo;
  3020. lenstartlabel,arangestartlabel: tasmlabel;
  3021. i : longint;
  3022. def: tdef;
  3023. dbgname: string;
  3024. vardatatype: ttypesym;
  3025. bind: tasmsymbind;
  3026. lang: tdwarf_source_language;
  3027. begin
  3028. current_module.flags:=current_module.flags or uf_has_dwarf_debuginfo;
  3029. storefilepos:=current_filepos;
  3030. current_filepos:=current_module.mainfilepos;
  3031. if assigned(dwarflabels) then
  3032. internalerror(2015100301);
  3033. { one item per def, plus some extra space in case of nested types,
  3034. externally used types etc (it will grow further if necessary) }
  3035. i:=current_module.localsymtable.DefList.count*4;
  3036. if assigned(current_module.globalsymtable) then
  3037. inc(i,current_module.globalsymtable.DefList.count*2);
  3038. dwarflabels:=TDwarfLabHashSet.Create(i,true,false);
  3039. currabbrevnumber:=0;
  3040. defnumberlist:=TFPObjectList.create(false);
  3041. deftowritelist:=TFPObjectList.create(false);
  3042. { not exported (FK)
  3043. FILEREC
  3044. TEXTREC
  3045. }
  3046. vardatatype:=try_search_system_type('TVARDATA');
  3047. if assigned(vardatatype) then
  3048. vardatadef:=trecorddef(vardatatype.typedef);
  3049. { write start labels }
  3050. new_section(current_asmdata.asmlists[al_dwarf_info],sec_debug_info,'',0);
  3051. current_asmdata.asmlists[al_dwarf_info].concat(tai_symbol.createname(target_asm.labelprefix+'debug_info0',AT_METADATA,0,voidpointertype));
  3052. { start abbrev section }
  3053. new_section(current_asmdata.asmlists[al_dwarf_abbrev],sec_debug_abbrev,'',0);
  3054. if not(target_info.system in systems_darwin) then
  3055. begin
  3056. { start aranges section }
  3057. new_section(current_asmdata.asmlists[al_dwarf_aranges],sec_debug_aranges,'',0);
  3058. current_asmdata.getlabel(arangestartlabel,alt_dbgfile);
  3059. if use_64bit_headers then
  3060. current_asmdata.asmlists[al_dwarf_aranges].concat(tai_const.create_32bit_unaligned(longint($FFFFFFFF)));
  3061. current_asmdata.asmlists[al_dwarf_aranges].concat(tai_const.create_rel_sym(offsetreltype,
  3062. arangestartlabel,current_asmdata.DefineAsmSymbol(target_asm.labelprefix+'earanges0',AB_LOCAL,AT_METADATA,voidpointertype)));
  3063. current_asmdata.asmlists[al_dwarf_aranges].concat(tai_label.create(arangestartlabel));
  3064. current_asmdata.asmlists[al_dwarf_aranges].concat(tai_const.create_16bit_unaligned(2));
  3065. if not(tf_dwarf_relative_addresses in target_info.flags) then
  3066. current_asmdata.asmlists[al_dwarf_aranges].concat(tai_const.create_type_sym(offsetabstype,
  3067. current_asmdata.DefineAsmSymbol(target_asm.labelprefix+'debug_info0',AB_LOCAL,AT_METADATA,voidpointertype)))
  3068. else
  3069. current_asmdata.asmlists[al_dwarf_aranges].concat(tai_const.create_rel_sym(offsetreltype,
  3070. current_asmdata.DefineAsmSymbol(target_asm.labelprefix+'debug_infosection0',AB_LOCAL,AT_METADATA,voidpointertype),
  3071. current_asmdata.DefineAsmSymbol(target_asm.labelprefix+'debug_info0',AB_LOCAL,AT_METADATA,voidpointertype)));
  3072. {$ifdef i8086}
  3073. { address_size }
  3074. current_asmdata.asmlists[al_dwarf_aranges].concat(tai_const.create_8bit(4));
  3075. { segment_size }
  3076. current_asmdata.asmlists[al_dwarf_aranges].concat(tai_const.create_8bit(2));
  3077. { no alignment/padding bytes on i8086 for Open Watcom compatibility }
  3078. {$else i8086}
  3079. { address_size }
  3080. current_asmdata.asmlists[al_dwarf_aranges].concat(tai_const.create_8bit(sizeof(pint)));
  3081. { segment_size }
  3082. current_asmdata.asmlists[al_dwarf_aranges].concat(tai_const.create_8bit(0));
  3083. { alignment }
  3084. current_asmdata.asmlists[al_dwarf_aranges].concat(tai_const.create_32bit_unaligned(0));
  3085. {$endif i8086}
  3086. { start ranges section }
  3087. new_section(current_asmdata.asmlists[al_dwarf_ranges],sec_debug_ranges,'',0);
  3088. end;
  3089. { debug info header }
  3090. current_asmdata.getlabel(lenstartlabel,alt_dbgfile);
  3091. { size }
  3092. if use_64bit_headers then
  3093. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_32bit_unaligned(longint($FFFFFFFF)));
  3094. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_rel_sym(offsetreltype,
  3095. lenstartlabel,current_asmdata.DefineAsmSymbol(target_asm.labelprefix+'edebug_info0',AB_LOCAL,AT_METADATA,voidpointertype)));
  3096. current_asmdata.asmlists[al_dwarf_info].concat(tai_label.create(lenstartlabel));
  3097. { version }
  3098. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_16bit_unaligned(dwarf_version));
  3099. { abbrev table (=relative from section start)}
  3100. if not(tf_dwarf_relative_addresses in target_info.flags) then
  3101. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_type_sym(offsetabstype,
  3102. current_asmdata.DefineAsmSymbol(target_asm.labelprefix+'debug_abbrev0',AB_LOCAL,AT_METADATA,voidpointertype)))
  3103. else
  3104. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_rel_sym(offsetreltype,
  3105. current_asmdata.DefineAsmSymbol(target_asm.labelprefix+'debug_abbrevsection0',AB_LOCAL,AT_METADATA,voidpointertype),
  3106. current_asmdata.DefineAsmSymbol(target_asm.labelprefix+'debug_abbrev0',AB_LOCAL,AT_METADATA,voidpointertype)));
  3107. { address size }
  3108. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(sizeof(pint)));
  3109. if (ds_dwarf_cpp in current_settings.debugswitches) then
  3110. lang:=DW_LANG_C_plus_plus
  3111. else
  3112. lang:=DW_LANG_Pascal83;
  3113. { first manadatory compilation unit TAG }
  3114. append_entry(DW_TAG_compile_unit,true,[
  3115. DW_AT_name,DW_FORM_string,relative_dwarf_path(current_module.sourcefiles.get_file(1).path+current_module.sourcefiles.get_file(1).name)+#0,
  3116. DW_AT_producer,DW_FORM_string,'Free Pascal '+full_version_string+' '+date_string+#0,
  3117. DW_AT_comp_dir,DW_FORM_string,BSToSlash(FixPath(GetCurrentDir,false))+#0,
  3118. DW_AT_language,DW_FORM_data1,lang,
  3119. DW_AT_identifier_case,DW_FORM_data1,DW_ID_case_insensitive]);
  3120. {$ifdef i8086}
  3121. case current_settings.x86memorymodel of
  3122. mm_tiny,
  3123. mm_small:
  3124. append_attribute(DW_AT_WATCOM_memory_model,DW_FORM_data1,[DW_WATCOM_MEMORY_MODEL_small]);
  3125. mm_medium:
  3126. append_attribute(DW_AT_WATCOM_memory_model,DW_FORM_data1,[DW_WATCOM_MEMORY_MODEL_medium]);
  3127. mm_compact:
  3128. append_attribute(DW_AT_WATCOM_memory_model,DW_FORM_data1,[DW_WATCOM_MEMORY_MODEL_compact]);
  3129. mm_large:
  3130. append_attribute(DW_AT_WATCOM_memory_model,DW_FORM_data1,[DW_WATCOM_MEMORY_MODEL_large]);
  3131. mm_huge:
  3132. append_attribute(DW_AT_WATCOM_memory_model,DW_FORM_data1,[DW_WATCOM_MEMORY_MODEL_huge]);
  3133. else
  3134. internalerror(2018052402);
  3135. end;
  3136. {$endif i8086}
  3137. { reference to line info section }
  3138. if not(tf_dwarf_relative_addresses in target_info.flags) then
  3139. append_labelentry_dataptr_abs(DW_AT_stmt_list,current_asmdata.DefineAsmSymbol(target_asm.labelprefix+'debug_line0',AB_LOCAL,AT_METADATA,voidpointertype))
  3140. else
  3141. append_labelentry_dataptr_rel(DW_AT_stmt_list,
  3142. current_asmdata.DefineAsmSymbol(target_asm.labelprefix+'debug_linesection0',AB_LOCAL,AT_METADATA,voidpointertype),
  3143. current_asmdata.DefineAsmSymbol(target_asm.labelprefix+'debug_line0',AB_LOCAL,AT_METADATA,voidpointertype));
  3144. if (m_objectivec1 in current_settings.modeswitches) then
  3145. append_attribute(DW_AT_APPLE_major_runtime_vers,DW_FORM_data1,[1]);
  3146. dbgname:=make_mangledname('DEBUGSTART',current_module.localsymtable,'');
  3147. if (target_info.system in systems_darwin) then
  3148. begin
  3149. bind:=AB_LOCAL;
  3150. dbgname:='L'+dbgname;
  3151. end
  3152. else
  3153. bind:=AB_GLOBAL;
  3154. append_labelentry(DW_AT_low_pc,current_asmdata.DefineAsmSymbol(dbgname,bind,AT_METADATA,voidpointertype));
  3155. dbgname:=make_mangledname('DEBUGEND',current_module.localsymtable,'');
  3156. if (target_info.system in systems_darwin) then
  3157. dbgname:='L'+dbgname;
  3158. append_labelentry(DW_AT_high_pc,current_asmdata.DefineAsmSymbol(dbgname,bind,AT_METADATA,voidpointertype));
  3159. finish_entry;
  3160. { write all global/local variables. This will flag all required tdefs }
  3161. if assigned(current_module.globalsymtable) then
  3162. write_symtable_syms(current_asmdata.asmlists[al_dwarf_info],current_module.globalsymtable);
  3163. if assigned(current_module.localsymtable) then
  3164. write_symtable_syms(current_asmdata.asmlists[al_dwarf_info],current_module.localsymtable);
  3165. { write all procedures and methods. This will flag all required tdefs }
  3166. if assigned(current_module.globalsymtable) then
  3167. write_symtable_procdefs(current_asmdata.asmlists[al_dwarf_info],current_module.globalsymtable);
  3168. if assigned(current_module.localsymtable) then
  3169. write_symtable_procdefs(current_asmdata.asmlists[al_dwarf_info],current_module.localsymtable);
  3170. { reset unit type info flag }
  3171. reset_unit_type_info;
  3172. { write used types from the used units }
  3173. write_used_unit_type_info(current_asmdata.asmlists[al_dwarf_info],current_module);
  3174. { last write the types from this unit }
  3175. if assigned(current_module.globalsymtable) then
  3176. write_symtable_defs(current_asmdata.asmlists[al_dwarf_info],current_module.globalsymtable);
  3177. if assigned(current_module.localsymtable) then
  3178. write_symtable_defs(current_asmdata.asmlists[al_dwarf_info],current_module.localsymtable);
  3179. { write defs not written yet }
  3180. write_remaining_defs_to_write(current_asmdata.asmlists[al_dwarf_info]);
  3181. { close compilation unit entry }
  3182. finish_children;
  3183. { end of debug info table }
  3184. current_asmdata.asmlists[al_dwarf_info].concat(tai_symbol.createname(target_asm.labelprefix+'edebug_info0',AT_METADATA,0,voidpointertype));
  3185. { end of abbrev table }
  3186. current_asmdata.asmlists[al_dwarf_abbrev].concat(tai_const.create_8bit(0));
  3187. if not(target_info.system in systems_darwin) then
  3188. begin
  3189. { end of aranges table }
  3190. {$ifdef i8086}
  3191. { 32-bit offset }
  3192. current_asmdata.asmlists[al_dwarf_aranges].concat(tai_const.Create_32bit_unaligned(0));
  3193. { 16-bit segment }
  3194. current_asmdata.asmlists[al_dwarf_aranges].concat(tai_const.Create_16bit_unaligned(0));
  3195. { 32-bit length }
  3196. current_asmdata.asmlists[al_dwarf_aranges].concat(tai_const.Create_32bit_unaligned(0));
  3197. {$else i8086}
  3198. { offset }
  3199. current_asmdata.asmlists[al_dwarf_aranges].concat(tai_const.Create_aint(0));
  3200. { length }
  3201. current_asmdata.asmlists[al_dwarf_aranges].concat(tai_const.Create_aint(0));
  3202. {$endif i8086}
  3203. current_asmdata.asmlists[al_dwarf_aranges].concat(tai_symbol.createname(target_asm.labelprefix+'earanges0',AT_METADATA,0,voidpointertype));
  3204. end;
  3205. { reset all def debug states }
  3206. for i:=0 to defnumberlist.count-1 do
  3207. begin
  3208. def := tdef(defnumberlist[i]);
  3209. if assigned(def) then
  3210. def.dbg_state:=dbg_state_unused;
  3211. end;
  3212. dwarflabels.free;
  3213. dwarflabels:=nil;
  3214. defnumberlist.free;
  3215. defnumberlist:=nil;
  3216. deftowritelist.free;
  3217. deftowritelist:=nil;
  3218. current_filepos:=storefilepos;
  3219. end;
  3220. procedure TDebugInfoDwarf.referencesections(list:TAsmList);
  3221. var
  3222. hp : tmodule;
  3223. begin
  3224. { Reference all DEBUGINFO sections from the main .fpc section }
  3225. { to prevent eliminating them by smartlinking }
  3226. if (target_info.system in ([system_powerpc_macosclassic]+systems_darwin)) then
  3227. exit;
  3228. new_section(list,sec_fpc,'links',0);
  3229. { include reference to all debuginfo sections of used units }
  3230. hp:=tmodule(loaded_units.first);
  3231. while assigned(hp) do
  3232. begin
  3233. If ((hp.flags and uf_has_dwarf_debuginfo)=uf_has_dwarf_debuginfo) and not assigned(hp.package) then
  3234. begin
  3235. list.concat(Tai_const.Createname(make_mangledname('DEBUGSTART',hp.localsymtable,''),0));
  3236. list.concat(Tai_const.Createname(make_mangledname('DEBUGEND',hp.localsymtable,''),0));
  3237. end;
  3238. hp:=tmodule(hp.next);
  3239. end;
  3240. end;
  3241. function TDebugInfoDwarf.symname(sym: tsym; manglename: boolean): String;
  3242. begin
  3243. if (sym.typ=paravarsym) and
  3244. (vo_is_self in tparavarsym(sym).varoptions) then
  3245. { We use 'this' for regular methods because that's what gdb triggers
  3246. on to automatically search fields. Don't do this for class methods,
  3247. because search class fields is not supported, and gdb 7.0+ fails
  3248. in this case because "this" is not a record in that case (it's a
  3249. pointer to a vmt) }
  3250. if not is_objc_class_or_protocol(tdef(sym.owner.defowner.owner.defowner)) and
  3251. not(po_classmethod in tabstractprocdef(sym.owner.defowner).procoptions) then
  3252. result:='this'
  3253. else
  3254. result:='self'
  3255. else if (sym.typ=typesym) and
  3256. is_objc_class_or_protocol(ttypesym(sym).typedef) then
  3257. result:=tobjectdef(ttypesym(sym).typedef).objextname^
  3258. else if (ds_dwarf_method_class_prefix in current_settings.debugswitches) and
  3259. (sym.typ=procsym) and
  3260. (tprocsym(sym).owner.symtabletype in [objectsymtable,recordsymtable]) then
  3261. begin
  3262. result:=tprocsym(sym).owner.name^+'__';
  3263. if manglename then
  3264. result := result + sym.name
  3265. else
  3266. result := result + symdebugname(sym);
  3267. end
  3268. else
  3269. begin
  3270. if manglename then
  3271. result := sym.name
  3272. else
  3273. result := symdebugname(sym);
  3274. end;
  3275. end;
  3276. procedure TDebugInfoDwarf.append_visibility(vis: tvisibility);
  3277. begin
  3278. case vis of
  3279. vis_private,
  3280. vis_strictprivate:
  3281. append_attribute(DW_AT_accessibility,DW_FORM_data1,[ord(DW_ACCESS_private)]);
  3282. vis_protected,
  3283. vis_strictprotected:
  3284. append_attribute(DW_AT_accessibility,DW_FORM_data1,[ord(DW_ACCESS_protected)]);
  3285. vis_public:
  3286. { default };
  3287. end;
  3288. end;
  3289. procedure TDebugInfoDwarf.insertlineinfo(list:TAsmList);
  3290. var
  3291. currfileinfo,
  3292. lastfileinfo : tfileposinfo;
  3293. currfuncname : pshortstring;
  3294. currstatement: boolean;
  3295. currsectype : TAsmSectiontype;
  3296. hp, hpend : tai;
  3297. infile : tinputfile;
  3298. prevcolumn,
  3299. diffline,
  3300. prevline,
  3301. prevfileidx,
  3302. currfileidx,
  3303. nolineinfolevel : Integer;
  3304. prevlabel,
  3305. currlabel : tasmlabel;
  3306. begin
  3307. {$ifdef OMFOBJSUPPORT}
  3308. if ds_dwarf_omf_linnum in current_settings.debugswitches then
  3309. dbgcodeview.InsertLineInfo_OMF_LINNUM_MsLink(list);
  3310. {$endif OMFOBJSUPPORT}
  3311. { this function will always terminate the lineinfo block }
  3312. generated_lineinfo := true;
  3313. { if this unit only contains code without debug info (implicit init
  3314. or final etc), make sure the file table contains at least one entry
  3315. (the main source of the unit), because normally this table gets
  3316. populated via calls to get_file_index and that won't happen in this
  3317. case }
  3318. get_file_index(current_module.sourcefiles.get_file(1));
  3319. FillChar(lastfileinfo,sizeof(lastfileinfo),0);
  3320. currfuncname:=nil;
  3321. currsectype:=sec_code;
  3322. hp:=Tai(list.first);
  3323. currstatement:=true;
  3324. prevcolumn := 0;
  3325. prevline := 1;
  3326. prevfileidx := 1;
  3327. prevlabel := nil;
  3328. nolineinfolevel:=0;
  3329. while assigned(hp) do
  3330. begin
  3331. case hp.typ of
  3332. ait_section :
  3333. currsectype:=tai_section(hp).sectype;
  3334. ait_function_name :
  3335. begin
  3336. currfuncname:=tai_function_name(hp).funcname;
  3337. asmline.concat(tai_comment.Create(strpnew('function: '+currfuncname^)));
  3338. end;
  3339. ait_force_line :
  3340. begin
  3341. lastfileinfo.line:=-1;
  3342. end;
  3343. ait_marker :
  3344. begin
  3345. case tai_marker(hp).kind of
  3346. mark_NoLineInfoStart:
  3347. inc(nolineinfolevel);
  3348. mark_NoLineInfoEnd:
  3349. dec(nolineinfolevel);
  3350. end;
  3351. end;
  3352. end;
  3353. if (currsectype=sec_code) and
  3354. (hp.typ=ait_instruction) then
  3355. begin
  3356. currfileinfo:=tailineinfo(hp).fileinfo;
  3357. { file changed ? (must be before line info) }
  3358. if (currfileinfo.fileindex<>0) and
  3359. ((lastfileinfo.fileindex<>currfileinfo.fileindex) or
  3360. (lastfileinfo.moduleindex<>currfileinfo.moduleindex)) then
  3361. begin
  3362. infile:=get_module(currfileinfo.moduleindex).sourcefiles.get_file(currfileinfo.fileindex);
  3363. if assigned(infile) then
  3364. begin
  3365. currfileidx := get_file_index(infile);
  3366. if prevfileidx <> currfileidx then
  3367. begin
  3368. list.insertbefore(tai_comment.Create(strpnew('path: '+infile.path)), hp);
  3369. list.insertbefore(tai_comment.Create(strpnew('file: '+infile.name)), hp);
  3370. list.insertbefore(tai_comment.Create(strpnew('indx: '+tostr(currfileidx))), hp);
  3371. { set file }
  3372. asmline.concat(tai_comment.Create(strpnew('path: '+infile.path)));
  3373. asmline.concat(tai_comment.Create(strpnew('file: '+infile.name)));
  3374. asmline.concat(tai_const.create_8bit(DW_LNS_set_file));
  3375. asmline.concat(tai_const.create_uleb128bit(currfileidx));
  3376. prevfileidx := currfileidx;
  3377. end;
  3378. { force new line info }
  3379. lastfileinfo.line:=-1;
  3380. end;
  3381. end;
  3382. { Set the line-nr to 0 if the code does not corresponds to a particular line }
  3383. if nolineinfolevel>0 then
  3384. currfileinfo.line := 0;
  3385. { line changed ? }
  3386. if (lastfileinfo.line<>currfileinfo.line) and ((currfileinfo.line<>0) or (nolineinfolevel>0)) then
  3387. begin
  3388. { set address }
  3389. current_asmdata.getlabel(currlabel, alt_dbgline);
  3390. list.insertbefore(tai_label.create(currlabel), hp);
  3391. asmline.concat(tai_comment.Create(strpnew('['+tostr(currfileinfo.line)+':'+tostr(currfileinfo.column)+']')));
  3392. if (prevlabel = nil) or
  3393. { darwin's assembler cannot create an uleb128 of the difference }
  3394. { between to symbols }
  3395. { same goes for Solaris native assembler }
  3396. (target_info.system in systems_darwin) or
  3397. (target_asm.id=as_solaris_as) then
  3398. begin
  3399. asmline.concat(tai_const.create_8bit(DW_LNS_extended_op));
  3400. asmline.concat(tai_const.create_uleb128bit(1+sizeof(pint)));
  3401. asmline.concat(tai_const.create_8bit(DW_LNE_set_address));
  3402. asmline.concat(tai_const.create_type_sym(aitconst_ptr_unaligned,currlabel));
  3403. {$ifdef i8086}
  3404. { on i8086 we also emit an Open Watcom-specific 'set segment' op }
  3405. asmline.concat(tai_const.create_8bit(DW_LNS_extended_op));
  3406. asmline.concat(tai_const.create_uleb128bit(3));
  3407. asmline.concat(tai_const.create_8bit(DW_LNE_set_segment));
  3408. asmline.concat(tai_const.Create_seg_name(currlabel.Name));
  3409. {$endif i8086}
  3410. end
  3411. else
  3412. begin
  3413. asmline.concat(tai_const.create_8bit(DW_LNS_advance_pc));
  3414. asmline.concat(tai_const.create_rel_sym(aitconst_uleb128bit, prevlabel, currlabel));
  3415. end;
  3416. prevlabel := currlabel;
  3417. { set column }
  3418. if prevcolumn <> currfileinfo.column then
  3419. begin
  3420. asmline.concat(tai_const.create_8bit(DW_LNS_set_column));
  3421. asmline.concat(tai_const.create_uleb128bit(currfileinfo.column));
  3422. prevcolumn := currfileinfo.column;
  3423. end;
  3424. { set statement }
  3425. if (currfileinfo.line=0) and currstatement then
  3426. begin
  3427. currstatement := false;
  3428. asmline.concat(tai_const.create_8bit(DW_LNS_negate_stmt));
  3429. end;
  3430. if not currstatement and (currfileinfo.line>0) then
  3431. begin
  3432. currstatement := true;
  3433. asmline.concat(tai_const.create_8bit(DW_LNS_negate_stmt));
  3434. end;
  3435. { set line }
  3436. diffline := currfileinfo.line - prevline;
  3437. if (diffline >= LINE_BASE) and (OPCODE_BASE + diffline - LINE_BASE <= 255) then
  3438. begin
  3439. { use special opcode, this also adds a row }
  3440. asmline.concat(tai_const.create_8bit(OPCODE_BASE + diffline - LINE_BASE));
  3441. end
  3442. else
  3443. begin
  3444. if diffline <> 0 then
  3445. begin
  3446. asmline.concat(tai_const.create_8bit(DW_LNS_advance_line));
  3447. asmline.concat(tai_const.create_sleb128bit(diffline));
  3448. end;
  3449. { no row added yet, do it manually }
  3450. asmline.concat(tai_const.create_8bit(DW_LNS_copy));
  3451. end;
  3452. prevline := currfileinfo.line;
  3453. end;
  3454. lastfileinfo:=currfileinfo;
  3455. end;
  3456. hpend:=hp;
  3457. hp:=tai(hp.next);
  3458. end;
  3459. if assigned(hpend) then
  3460. begin
  3461. { set address for end (see appendix 3 of dwarf 2 specs) }
  3462. current_asmdata.getlabel(currlabel, alt_dbgline);
  3463. list.insertafter(tai_label.create(currlabel), hpend);
  3464. asmline.concat(tai_const.create_8bit(DW_LNS_extended_op));
  3465. asmline.concat(tai_const.create_uleb128bit(1+sizeof(pint)));
  3466. asmline.concat(tai_const.create_8bit(DW_LNE_set_address));
  3467. asmline.concat(tai_const.create_type_sym(aitconst_ptr_unaligned,currlabel));
  3468. end;
  3469. { end sequence }
  3470. asmline.concat(tai_const.Create_8bit(DW_LNS_extended_op));
  3471. asmline.concat(tai_const.Create_8bit(1));
  3472. asmline.concat(tai_const.Create_8bit(DW_LNE_end_sequence));
  3473. asmline.concat(tai_comment.Create(strpnew('###################')));
  3474. end;
  3475. procedure TDebugInfoDwarf.finish_lineinfo;
  3476. var
  3477. infile: tinputfile;
  3478. begin
  3479. { only needed if no line info at all has been generated }
  3480. if generated_lineinfo then
  3481. begin
  3482. { reset for next module compilation }
  3483. generated_lineinfo:=false;
  3484. exit;
  3485. end;
  3486. { at least the Darwin linker is annoyed if you do not }
  3487. { finish the lineinfo section, or if it doesn't }
  3488. { contain at least one file name and set_address }
  3489. infile:=current_module.sourcefiles.get_file(1);
  3490. if not assigned(infile) then
  3491. internalerror(2006020211);
  3492. asmline.concat(tai_const.create_8bit(DW_LNS_set_file));
  3493. asmline.concat(tai_const.create_uleb128bit(get_file_index(infile)));
  3494. asmline.concat(tai_const.create_8bit(DW_LNS_extended_op));
  3495. asmline.concat(tai_const.create_uleb128bit(1+sizeof(pint)));
  3496. asmline.concat(tai_const.create_8bit(DW_LNE_set_address));
  3497. asmline.concat(tai_const.create_type_sym(aitconst_ptr_unaligned,nil));
  3498. asmline.concat(tai_const.create_8bit(DW_LNS_extended_op));
  3499. asmline.concat(tai_const.Create_8bit(1));
  3500. asmline.concat(tai_const.Create_8bit(DW_LNE_end_sequence));
  3501. asmline.concat(tai_comment.Create(strpnew('###################')));
  3502. end;
  3503. {****************************************************************************
  3504. TDebugInfoDwarf2
  3505. ****************************************************************************}
  3506. procedure TDebugInfoDwarf2.appenddef_file(list:TAsmList;def: tfiledef);
  3507. begin
  3508. { gdb 6.4 doesn't support files so far so we use some fake recorddef
  3509. file recs. are less than 1k so using data2 is enough }
  3510. if assigned(def.typesym) then
  3511. append_entry(DW_TAG_structure_type,false,[
  3512. DW_AT_name,DW_FORM_string,symname(def.typesym, false)+#0,
  3513. DW_AT_byte_size,DW_FORM_udata,def.size
  3514. ])
  3515. else
  3516. append_entry(DW_TAG_structure_type,false,[
  3517. DW_AT_byte_size,DW_FORM_udata,def.size
  3518. ]);
  3519. finish_entry;
  3520. end;
  3521. procedure TDebugInfoDwarf2.appenddef_formal(list:TAsmList;def: tformaldef);
  3522. begin
  3523. { gdb 6.4 doesn't support DW_TAG_unspecified_type so we
  3524. replace it with a unsigned type with size 0 (FK)
  3525. }
  3526. append_entry(DW_TAG_base_type,false,[
  3527. DW_AT_name,DW_FORM_string,'FormalDef'#0,
  3528. DW_AT_encoding,DW_FORM_data1,DW_ATE_unsigned,
  3529. DW_AT_byte_size,DW_FORM_data1,0
  3530. ]);
  3531. finish_entry;
  3532. end;
  3533. procedure TDebugInfoDwarf2.append_object_struct(def: tobjectdef; const createlabel: boolean; const objectname: PShortString);
  3534. begin
  3535. if createlabel then
  3536. begin
  3537. if not(tf_dwarf_only_local_labels in target_info.flags) then
  3538. current_asmdata.asmlists[al_dwarf_info].concat(tai_symbol.create_global(def_dwarf_class_struct_lab(def),0))
  3539. else
  3540. current_asmdata.asmlists[al_dwarf_info].concat(tai_symbol.create(def_dwarf_class_struct_lab(def),0));
  3541. end;
  3542. if assigned(objectname) then
  3543. append_entry(DW_TAG_class_type,true,[
  3544. DW_AT_name,DW_FORM_string,objectname^+#0,
  3545. DW_AT_byte_size,DW_FORM_udata,tobjectsymtable(def.symtable).datasize
  3546. ])
  3547. else
  3548. append_entry(DW_TAG_class_type,true,[
  3549. DW_AT_byte_size,DW_FORM_udata,tobjectsymtable(def.symtable).datasize
  3550. ]);
  3551. { Apple-specific tag that identifies it as an Objective-C class }
  3552. if (def.objecttype=odt_objcclass) then
  3553. append_attribute(DW_AT_APPLE_runtime_class,DW_FORM_data1,[DW_LANG_ObjC]);
  3554. finish_entry;
  3555. if assigned(def.childof) then
  3556. begin
  3557. append_entry(DW_TAG_inheritance,false,[
  3558. DW_AT_accessibility,DW_FORM_data1,DW_ACCESS_public,
  3559. DW_AT_data_member_location,DW_FORM_block1,1+lengthuleb128(0)
  3560. ]);
  3561. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_plus_uconst)));
  3562. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_uleb128bit(0));
  3563. if (def.childof.dbg_state=dbg_state_unused) then
  3564. def.childof.dbg_state:=dbg_state_used;
  3565. if is_implicit_pointer_object_type(def) then
  3566. append_labelentry_ref(DW_AT_type,def_dwarf_class_struct_lab(def.childof))
  3567. else
  3568. append_labelentry_ref(DW_AT_type,def_dwarf_lab(def.childof));
  3569. finish_entry;
  3570. end;
  3571. if (oo_has_vmt in def.objectoptions) and
  3572. (not assigned(def.childof) or
  3573. not(oo_has_vmt in def.childof.objectoptions)) then
  3574. begin
  3575. { vmt field }
  3576. append_entry(DW_TAG_member,false,[
  3577. DW_AT_artificial,DW_FORM_flag,true,
  3578. DW_AT_name,DW_FORM_string,'_vptr$'+def.objname^+#0,
  3579. DW_AT_data_member_location,DW_FORM_block1,1+lengthuleb128(def.vmt_offset)
  3580. ]);
  3581. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_plus_uconst)));
  3582. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_uleb128bit(def.vmt_offset));
  3583. { should be changed into a pointer to a function returning an }
  3584. { int and with TAG_unspecified_parameters }
  3585. if (voidpointertype.dbg_state=dbg_state_unused) then
  3586. voidpointertype.dbg_state:=dbg_state_used;
  3587. append_labelentry_ref(DW_AT_type,def_dwarf_lab(voidpointertype));
  3588. finish_entry;
  3589. end;
  3590. def.symtable.symList.ForEachCall(@enum_membersyms_callback,nil);
  3591. { Write the methods in the scope of the class/object, except for Objective-C. }
  3592. if is_objc_class_or_protocol(def) then
  3593. finish_children;
  3594. { don't write procdefs of externally defined classes, gcc doesn't
  3595. either (info is probably gotten from ObjC runtime) }
  3596. if not(oo_is_external in def.objectoptions) then
  3597. write_symtable_procdefs(current_asmdata.asmlists[al_dwarf_info],def.symtable);
  3598. if not is_objc_class_or_protocol(def) then
  3599. finish_children;
  3600. end;
  3601. procedure TDebugInfoDwarf2.appenddef_object(list:TAsmList;def: tobjectdef);
  3602. begin
  3603. case def.objecttype of
  3604. odt_cppclass,
  3605. odt_object:
  3606. append_object_struct(def,false,def.objname);
  3607. odt_interfacecom,
  3608. odt_interfacecorba,
  3609. odt_dispinterface,
  3610. odt_helper,
  3611. odt_class:
  3612. begin
  3613. { implicit pointer }
  3614. append_entry(DW_TAG_pointer_type,false,[]);
  3615. append_labelentry_ref(DW_AT_type,def_dwarf_class_struct_lab(def));
  3616. finish_entry;
  3617. append_object_struct(def,true,def.objname);
  3618. end;
  3619. odt_objcclass:
  3620. { Objective-C class: same as regular class, except for
  3621. a) Apple-specific tag that identifies it as an Objective-C class
  3622. b) use extname^ instead of objname
  3623. }
  3624. append_object_struct(def,true,def.objextname);
  3625. odt_objcprotocol:
  3626. begin
  3627. append_entry(DW_TAG_pointer_type,false,[]);
  3628. append_labelentry_ref(DW_AT_type,def_dwarf_lab(voidpointertype));
  3629. finish_entry;
  3630. end;
  3631. else
  3632. internalerror(200602041);
  3633. end;
  3634. end;
  3635. procedure TDebugInfoDwarf2.appenddef_set_intern(list:TAsmList;def: tsetdef; force_tag_set: boolean);
  3636. var
  3637. lab: tasmlabel;
  3638. begin
  3639. if force_tag_set or
  3640. (ds_dwarf_sets in current_settings.debugswitches) then
  3641. begin
  3642. { current (20070704 -- patch was committed on 20060513) gdb cvs supports set types }
  3643. if assigned(def.typesym) then
  3644. append_entry(DW_TAG_set_type,false,[
  3645. DW_AT_name,DW_FORM_string,symname(def.typesym, false)+#0,
  3646. DW_AT_byte_size,DW_FORM_data2,def.size
  3647. ])
  3648. else
  3649. append_entry(DW_TAG_set_type,false,[
  3650. DW_AT_byte_size,DW_FORM_data2,def.size
  3651. ]);
  3652. if assigned(def.elementdef) then
  3653. begin
  3654. if not(tf_dwarf_only_local_labels in target_info.flags) then
  3655. current_asmdata.getglobaldatalabel(lab)
  3656. else
  3657. current_asmdata.getaddrlabel(lab);
  3658. append_labelentry_ref(DW_AT_type,lab);
  3659. finish_entry;
  3660. if lab.bind=AB_GLOBAL then
  3661. current_asmdata.asmlists[al_dwarf_info].concat(tai_symbol.create_global(lab,0))
  3662. else
  3663. current_asmdata.asmlists[al_dwarf_info].concat(tai_symbol.create(lab,0));
  3664. { Sets of e.g. [1..5] are actually stored as a set of [0..7],
  3665. so write the exact boundaries of the set here. Let's hope no
  3666. debugger ever rejects this because this "subrange" type can
  3667. actually have a larger range than the original one. }
  3668. append_entry(DW_TAG_subrange_type,false,[
  3669. DW_AT_lower_bound,DW_FORM_sdata,def.setbase,
  3670. DW_AT_upper_bound,DW_FORM_sdata,get_max_value(def.elementdef).svalue
  3671. ]);
  3672. append_labelentry_ref(DW_AT_type,def_dwarf_lab(def.elementdef))
  3673. end
  3674. end
  3675. else
  3676. begin
  3677. { gdb versions which don't support sets refuse to load the debug }
  3678. { info of modules that contain set tags }
  3679. if assigned(def.typesym) then
  3680. append_entry(DW_TAG_base_type,false,[
  3681. DW_AT_name,DW_FORM_string,symname(def.typesym, false)+#0,
  3682. DW_AT_encoding,DW_FORM_data1,DW_ATE_unsigned,
  3683. DW_AT_byte_size,DW_FORM_data2,def.size
  3684. ])
  3685. else
  3686. append_entry(DW_TAG_base_type,false,[
  3687. DW_AT_encoding,DW_FORM_data1,DW_ATE_unsigned,
  3688. DW_AT_byte_size,DW_FORM_data2,def.size
  3689. ]);
  3690. end;
  3691. finish_entry;
  3692. end;
  3693. procedure TDebugInfoDwarf2.appenddef_set(list:TAsmList;def: tsetdef);
  3694. begin
  3695. appenddef_set_intern(list,def,false);
  3696. end;
  3697. procedure TDebugInfoDwarf2.appenddef_undefined(list:TAsmList;def: tundefineddef);
  3698. begin
  3699. { gdb 6.4 doesn't support DW_TAG_unspecified_type so we
  3700. replace it with a unsigned type with size 0 (FK)
  3701. }
  3702. append_entry(DW_TAG_base_type,false,[
  3703. DW_AT_name,DW_FORM_string,'FormalDef'#0,
  3704. DW_AT_encoding,DW_FORM_data1,DW_ATE_unsigned,
  3705. DW_AT_byte_size,DW_FORM_data1,0
  3706. ]);
  3707. finish_entry;
  3708. end;
  3709. procedure TDebugInfoDwarf2.appenddef_variant(list:TAsmList;def: tvariantdef);
  3710. begin
  3711. { variants aren't known to dwarf2 but writting tvardata should be enough }
  3712. if assigned(vardatadef) then
  3713. appenddef_record_named(list,trecorddef(vardatadef),'Variant');
  3714. end;
  3715. function TDebugInfoDwarf2.dwarf_version: Word;
  3716. begin
  3717. Result:=2;
  3718. end;
  3719. {****************************************************************************
  3720. TDebugInfoDwarf3
  3721. ****************************************************************************}
  3722. procedure TDebugInfoDwarf3.append_labelentry_addr_ref(attr : tdwarf_attribute;sym : tasmsymbol);
  3723. begin
  3724. AddConstToAbbrev(ord(DW_FORM_ref_addr));
  3725. { Since Dwarf 3 the length of a DW_FORM_ref_addr entry is not dependent on the pointer size of the
  3726. target platform, but on the used Dwarf-format (32 bit or 64 bit) for the current compilation section. }
  3727. if use_64bit_headers then
  3728. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.Create_type_sym(aitconst_64bit_unaligned,sym))
  3729. else
  3730. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.Create_type_sym(aitconst_32bit_unaligned,sym));
  3731. end;
  3732. procedure tdebuginfodwarf3.appenddef_array(list: tasmlist; def: tarraydef);
  3733. begin
  3734. if not is_dynamic_array(def) then
  3735. begin
  3736. inherited appenddef_array(list,def);
  3737. exit;
  3738. end;
  3739. if assigned(def.typesym) then
  3740. append_entry(DW_TAG_array_type,true,[
  3741. DW_AT_name,DW_FORM_string,symname(def.typesym, false)+#0,
  3742. DW_AT_data_location,DW_FORM_block1,2
  3743. ])
  3744. else
  3745. append_entry(DW_TAG_array_type,true,[
  3746. DW_AT_data_location,DW_FORM_block1,2
  3747. ]);
  3748. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_push_object_address)));
  3749. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_deref)));
  3750. append_labelentry_ref(DW_AT_type,def_dwarf_lab(def.elementdef));
  3751. finish_entry;
  3752. { to simplify things, we don't write a multidimensional array here }
  3753. append_entry(DW_TAG_subrange_type,false,[
  3754. DW_AT_byte_stride,DW_FORM_udata,def.elesize,
  3755. DW_AT_lower_bound,DW_FORM_udata,0,
  3756. DW_AT_upper_bound,DW_FORM_block1,14
  3757. ]);
  3758. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_push_object_address)));
  3759. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_deref)));
  3760. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_dup)));
  3761. { pointer = nil? }
  3762. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_bra)));
  3763. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_16bit_unaligned(5));
  3764. { yes -> length = 0 }
  3765. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_const1s)));
  3766. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(byte(-1)));
  3767. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_skip)));
  3768. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_16bit_unaligned(3));
  3769. { no -> load length }
  3770. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_lit0)+sizesinttype.size));
  3771. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_minus)));
  3772. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_deref)));
  3773. append_labelentry_ref(DW_AT_type,def_dwarf_lab(def.rangedef));
  3774. finish_entry;
  3775. finish_children;
  3776. end;
  3777. procedure tdebuginfodwarf3.appenddef_string(list: tasmlist; def: tstringdef);
  3778. procedure addstringdef(const name: shortstring; chardef: tdef; deref: boolean; lensize: aint);
  3779. var
  3780. upperopcodes: longint;
  3781. begin
  3782. { deref=true -> ansi/unicde/widestring; deref = false -> short/longstring }
  3783. if assigned(def.typesym) then
  3784. append_entry(DW_TAG_array_type,true,[
  3785. DW_AT_name,DW_FORM_string,name+#0,
  3786. DW_AT_data_location,DW_FORM_block1,2+ord(not(deref))
  3787. ])
  3788. else
  3789. append_entry(DW_TAG_array_type,true,[
  3790. DW_AT_data_location,DW_FORM_block1,2+ord(not(deref))
  3791. ]);
  3792. { in all cases we start with the address of the string }
  3793. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_push_object_address)));
  3794. if deref then
  3795. begin
  3796. { ansi/unicode/widestring -> dereference the address of the string, and then
  3797. we point to address of the string
  3798. }
  3799. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_deref)));
  3800. end
  3801. else
  3802. begin
  3803. { shortstring characters begin at string[1], so add one to the string's address }
  3804. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_lit0)+lensize));
  3805. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_plus)))
  3806. end;
  3807. { reference to the element type of the string }
  3808. append_labelentry_ref(DW_AT_type,def_dwarf_lab(chardef));
  3809. finish_entry;
  3810. { now the information about the length of the string }
  3811. if deref then
  3812. begin
  3813. if not (is_widestring(def) and (tf_winlikewidestring in target_info.flags)) then
  3814. upperopcodes:=13
  3815. else
  3816. upperopcodes:=16;
  3817. { lower bound is always 1, upper bound (length) needs to be calculated }
  3818. append_entry(DW_TAG_subrange_type,false,[
  3819. DW_AT_lower_bound,DW_FORM_udata,1,
  3820. DW_AT_upper_bound,DW_FORM_block1,upperopcodes
  3821. ]);
  3822. { high(string) is stored sizeof(sizeint) bytes before the string data }
  3823. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_push_object_address)));
  3824. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_deref)));
  3825. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_dup)));
  3826. { pointer = nil? }
  3827. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_bra)));
  3828. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_16bit_unaligned(4));
  3829. { yes -> length = 0 }
  3830. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_lit0)));
  3831. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_skip)));
  3832. if upperopcodes=16 then
  3833. { skip the extra deref_size argument and the division by two of the length }
  3834. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_16bit_unaligned(6))
  3835. else
  3836. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_16bit_unaligned(3));
  3837. { no -> load length }
  3838. if upperopcodes=16 then
  3839. { for Windows WideString the size is always a DWORD }
  3840. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_lit4)))
  3841. else
  3842. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_lit0)+sizesinttype.size));
  3843. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_minus)));
  3844. if upperopcodes=16 then
  3845. begin
  3846. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_deref_size)));
  3847. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(4));
  3848. end
  3849. else
  3850. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_deref)));
  3851. { for widestrings, the length is specified in bytes, so divide by two }
  3852. if (upperopcodes=16) then
  3853. begin
  3854. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_lit1)));
  3855. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_shr)));
  3856. end;
  3857. end
  3858. else
  3859. begin
  3860. append_entry(DW_TAG_subrange_type,false,[
  3861. DW_AT_lower_bound,DW_FORM_udata,1,
  3862. DW_AT_upper_bound,DW_FORM_block1,3
  3863. ]);
  3864. { for shortstrings, the length is the first byte of the string }
  3865. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_push_object_address)));
  3866. { load 1 byte }
  3867. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_deref_size)));
  3868. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(lensize));
  3869. end;
  3870. finish_entry;
  3871. finish_children;
  3872. end;
  3873. begin
  3874. if (ds_dwarf_cpp in current_settings.debugswitches) then
  3875. begin
  3876. // At least LLDB 6.0.0 does not like this implementation of string types.
  3877. // Call the inherited DWARF 2 implementation, which works fine.
  3878. inherited;
  3879. exit;
  3880. end;
  3881. case def.stringtype of
  3882. st_shortstring:
  3883. begin
  3884. addstringdef('ShortString',cansichartype,false,1);
  3885. end;
  3886. st_longstring:
  3887. begin
  3888. {$ifdef cpu64bitaddr}
  3889. addstringdef('LongString',cansichartype,false,8);
  3890. {$else cpu64bitaddr}
  3891. addstringdef('LongString',cansichartype,false,4);
  3892. {$endif cpu64bitaddr}
  3893. end;
  3894. st_ansistring:
  3895. begin
  3896. addstringdef('AnsiString',cansichartype,true,-1);
  3897. end;
  3898. st_unicodestring:
  3899. begin
  3900. addstringdef('UnicodeString',cwidechartype,true,-1);
  3901. end;
  3902. st_widestring:
  3903. begin
  3904. addstringdef('WideString',cwidechartype,true,-1)
  3905. end;
  3906. end;
  3907. end;
  3908. procedure TDebugInfoDwarf3.appenddef_file(list:TAsmList;def: tfiledef);
  3909. begin
  3910. if assigned(def.typesym) then
  3911. append_entry(DW_TAG_file_type,false,[
  3912. DW_AT_name,DW_FORM_string,symname(def.typesym, false)+#0,
  3913. DW_AT_byte_size,DW_FORM_data2,def.size
  3914. ])
  3915. else
  3916. append_entry(DW_TAG_file_type,false,[
  3917. DW_AT_byte_size,DW_FORM_data2,def.size
  3918. ]);
  3919. if tfiledef(def).filetyp=ft_typed then
  3920. append_labelentry_ref(DW_AT_type,def_dwarf_lab(tfiledef(def).typedfiledef));
  3921. finish_entry;
  3922. end;
  3923. procedure TDebugInfoDwarf3.appenddef_formal(list:TAsmList;def: tformaldef);
  3924. begin
  3925. if (ds_dwarf_cpp in current_settings.debugswitches) then
  3926. begin
  3927. // Do not use DW_TAG_unspecified_type for C++ simulation.
  3928. // At least LLDB 3.9.0 crashes in such case.
  3929. // Call the inherited DWARF 2 implementation, which works fine.
  3930. inherited;
  3931. exit;
  3932. end;
  3933. append_entry(DW_TAG_unspecified_type,false,[]);
  3934. finish_entry;
  3935. end;
  3936. procedure TDebugInfoDwarf3.appenddef_object(list:TAsmList;def: tobjectdef);
  3937. procedure dostruct(tag: tdwarf_tag);
  3938. begin
  3939. if assigned(def.objname) then
  3940. append_entry(tag,true,[
  3941. DW_AT_name,DW_FORM_string,def.objrealname^+#0
  3942. ])
  3943. else
  3944. append_entry(DW_TAG_structure_type,true,[]);
  3945. append_attribute(DW_AT_byte_size,DW_FORM_udata,[tobjectsymtable(def.symtable).datasize]);
  3946. { an old style object and a cpp class are accessed directly, so we do not need DW_AT_allocated and DW_AT_data_location tags,
  3947. see issue #36017 }
  3948. if not(is_object(def) or is_cppclass(def)) then
  3949. begin
  3950. { The pointer to the class-structure is hidden. The debug-information
  3951. does not contain an implicit pointer, but the data-adress is dereferenced here.
  3952. In case of a nil-pointer, report the class as being unallocated.
  3953. }
  3954. append_block1(DW_AT_allocated,2);
  3955. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_push_object_address)));
  3956. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_deref)));
  3957. append_block1(DW_AT_data_location,2);
  3958. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_push_object_address)));
  3959. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_deref)));
  3960. end;
  3961. finish_entry;
  3962. end;
  3963. procedure doimplicitpointer;
  3964. var
  3965. obj : tasmlabel;
  3966. begin
  3967. if not(tf_dwarf_only_local_labels in target_info.flags) then
  3968. current_asmdata.getglobaldatalabel(obj)
  3969. else
  3970. current_asmdata.getaddrlabel(obj);
  3971. { implicit pointer }
  3972. append_entry(DW_TAG_pointer_type,false,[]);
  3973. append_labelentry_ref(DW_AT_type,obj);
  3974. finish_entry;
  3975. current_asmdata.asmlists[al_dwarf_info].concat(tai_symbol.create(obj,0));
  3976. end;
  3977. procedure doparent(isinterface: boolean);
  3978. begin
  3979. if not assigned(def.childof) then
  3980. exit;
  3981. if isinterface then
  3982. begin
  3983. append_entry(DW_TAG_inheritance,false,[]);
  3984. end
  3985. else
  3986. begin
  3987. append_entry(DW_TAG_inheritance,false,[
  3988. DW_AT_accessibility,DW_FORM_data1,DW_ACCESS_public,
  3989. DW_AT_data_member_location,DW_FORM_block1,1+lengthuleb128(0)
  3990. ]);
  3991. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_plus_uconst)));
  3992. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_uleb128bit(0));
  3993. end;
  3994. append_labelentry_ref(DW_AT_type,def_dwarf_lab(def.childof));
  3995. finish_entry;
  3996. end;
  3997. var
  3998. n: integer;
  3999. begin
  4000. case def.objecttype of
  4001. odt_cppclass,
  4002. odt_object:
  4003. begin
  4004. dostruct(DW_TAG_structure_type);
  4005. doparent(false);
  4006. end;
  4007. odt_interfacecom,
  4008. odt_interfacecorba,
  4009. odt_dispinterface:
  4010. begin
  4011. dostruct(DW_TAG_interface_type);
  4012. doparent(true);
  4013. end;
  4014. odt_helper,
  4015. odt_class:
  4016. begin
  4017. //dostruct(DW_TAG_class_type);
  4018. //doparent(false);
  4019. append_entry(DW_TAG_pointer_type,false,[]);
  4020. append_labelentry_ref(DW_AT_type,def_dwarf_class_struct_lab(def));
  4021. finish_entry;
  4022. append_object_struct(def,true,def.objrealname);
  4023. Exit;
  4024. end;
  4025. else
  4026. internalerror(200609171);
  4027. end;
  4028. { add implemented interfaces }
  4029. if assigned(def.ImplementedInterfaces) then
  4030. for n := 0 to def.ImplementedInterfaces.count-1 do
  4031. begin
  4032. append_entry(DW_TAG_inheritance,false,[]);
  4033. append_labelentry_ref(DW_AT_type,def_dwarf_lab(TImplementedInterface(def.ImplementedInterfaces[n]).IntfDef));
  4034. finish_entry;
  4035. end;
  4036. { add members }
  4037. def.symtable.symList.ForEachCall(@enum_membersyms_callback,nil);
  4038. finish_children;
  4039. end;
  4040. procedure TDebugInfoDwarf3.appenddef_set(list:TAsmList;def: tsetdef);
  4041. begin
  4042. appenddef_set_intern(list,def,true);
  4043. end;
  4044. procedure TDebugInfoDwarf3.appenddef_undefined(list:TAsmList;def: tundefineddef);
  4045. begin
  4046. { ??? can a undefined def have a typename ? }
  4047. if assigned(def.typesym) then
  4048. append_entry(DW_TAG_unspecified_type,false,[
  4049. DW_AT_name,DW_FORM_string,symname(def.typesym, false)+#0
  4050. ])
  4051. else
  4052. append_entry(DW_TAG_unspecified_type,false,[
  4053. ]);
  4054. finish_entry;
  4055. end;
  4056. procedure TDebugInfoDwarf3.appenddef_variant(list:TAsmList;def: tvariantdef);
  4057. const
  4058. VARIANTS: array[1..27] of record Value: Word; Name: String end = (
  4059. (value:0; name:''),
  4060. (value:1; name:''),
  4061. (value:2; name:'VSMALLINT'),
  4062. (value:3; name:'VINTEGER'),
  4063. (value:4; name:'VSINGLE'),
  4064. (value:5; name:'VDOUBLE'),
  4065. (value:6; name:'VCURRENCY'),
  4066. (value:7; name:'VDATE'),
  4067. (value:8; name:'VOLESTR'),
  4068. (value:9; name:'VDISPATCH'),
  4069. (value:10; name:'VERROR'),
  4070. (value:11; name:'VBOOLEAN'),
  4071. (value:12; name:''),
  4072. (value:13; name:'VUNKNOWN'),
  4073. (value:14; name:''),
  4074. (value:16; name:'VSHORTINT'),
  4075. (value:17; name:'VBYTE'),
  4076. (value:18; name:'VWORD'),
  4077. (value:19; name:'VLONGWORD'),
  4078. (value:20; name:'VINT64'),
  4079. (value:21; name:'VQWORD'),
  4080. (value:36; name:'VRECORD'),
  4081. (value:$48; name:''),
  4082. (value:$100; name:'VSTRING'),
  4083. (value:$101; name:'VANY'),
  4084. (value:$2000; name:'VARRAY'),
  4085. (value:$4000; name:'VPOINTER')
  4086. );
  4087. var
  4088. fs: tfieldvarsym;
  4089. lbl: tasmlabel;
  4090. idx: integer;
  4091. begin
  4092. { it could be done with DW_TAG_variant for the union part (if that info was available)
  4093. now we do it manually for variants (MWE) }
  4094. { struct }
  4095. append_entry(DW_TAG_structure_type,true,[
  4096. DW_AT_name,DW_FORM_string,'Variant'#0,
  4097. DW_AT_byte_size,DW_FORM_udata,vardatadef.size
  4098. ]);
  4099. finish_entry;
  4100. append_entry(DW_TAG_variant_part,true,[
  4101. ]);
  4102. current_asmdata.getaddrlabel(lbl);
  4103. append_labelentry_ref(DW_AT_discr,lbl);
  4104. finish_entry;
  4105. { discriminant }
  4106. fs := tfieldvarsym(vardatadef.symtable.Find('VTYPE'));
  4107. if (fs = nil) or (fs.typ <> fieldvarsym) then
  4108. internalerror(200609271);
  4109. current_asmdata.asmlists[al_dwarf_info].concat(tai_symbol.create(lbl,0));
  4110. appendsym_fieldvar(list,fs);
  4111. { variants }
  4112. for idx := Low(VARIANTS) to High(VARIANTS) do
  4113. begin
  4114. append_entry(DW_TAG_variant,true,[
  4115. DW_AT_discr_value,DW_FORM_udata,VARIANTS[idx].value
  4116. ]);
  4117. finish_entry;
  4118. if VARIANTS[idx].name <> '' then
  4119. begin
  4120. fs := tfieldvarsym(vardatadef.symtable.Find(VARIANTS[idx].name));
  4121. if (fs = nil) or (fs.typ <> fieldvarsym) then
  4122. internalerror(20060927200+idx);
  4123. appendsym_fieldvar(list,fs);
  4124. end;
  4125. finish_children; { variant }
  4126. end;
  4127. finish_children; { variant part }
  4128. finish_children; { struct }
  4129. end;
  4130. function TDebugInfoDwarf3.dwarf_version: Word;
  4131. begin
  4132. Result:=3;
  4133. end;
  4134. function TDebugInfoDwarf3.symdebugname(sym: tsym): String;
  4135. begin
  4136. Result:=sym.realname;
  4137. end;
  4138. { TDebugInfoDwarf4 }
  4139. function TDebugInfoDwarf4.dwarf_version: Word;
  4140. begin
  4141. Result:=4;
  4142. end;
  4143. {****************************************************************************
  4144. ****************************************************************************}
  4145. const
  4146. dbg_dwarf2_info : tdbginfo =
  4147. (
  4148. id : dbg_dwarf2;
  4149. idtxt : 'DWARF2';
  4150. );
  4151. dbg_dwarf3_info : tdbginfo =
  4152. (
  4153. id : dbg_dwarf3;
  4154. idtxt : 'DWARF3';
  4155. );
  4156. dbg_dwarf4_info : tdbginfo =
  4157. (
  4158. id : dbg_dwarf4;
  4159. idtxt : 'DWARF4';
  4160. );
  4161. initialization
  4162. RegisterDebugInfo(dbg_dwarf2_info,TDebugInfoDwarf2);
  4163. RegisterDebugInfo(dbg_dwarf3_info,TDebugInfoDwarf3);
  4164. RegisterDebugInfo(dbg_dwarf4_info,TDebugInfoDwarf4);
  4165. end.