dbgdwarf.pas 175 KB

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