dbgdwarf.pas 179 KB

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