2
0

dbgdwarf.pas 181 KB

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