dbgdwarf.pas 183 KB

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