dbgdwarf.pas 191 KB

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