dbgdwarf.pas 184 KB

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