dbgdwarf.pas 176 KB

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