dbgdwarf.pas 179 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446
  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. else
  2167. begin
  2168. dreg:=dwarf_reg(sym.localloc.reference.base);
  2169. if dreg<=31 then
  2170. begin
  2171. templist.concat(tai_const.create_8bit(ord(DW_OP_breg0)+dreg));
  2172. templist.concat(tai_const.create_sleb128bit(sym.localloc.reference.offset+offset));
  2173. blocksize:=1+Lengthsleb128(sym.localloc.reference.offset+offset);
  2174. end
  2175. else
  2176. begin
  2177. templist.concat(tai_const.create_8bit(ord(DW_OP_bregx)));
  2178. templist.concat(tai_const.create_uleb128bit(dreg));
  2179. templist.concat(tai_const.create_sleb128bit(sym.localloc.reference.offset+offset));
  2180. blocksize:=1+Lengthuleb128(dreg)+LengthSleb128(sym.localloc.reference.offset+offset);
  2181. end;
  2182. end;
  2183. {$ifdef i8086}
  2184. segment_reg:=sym.localloc.reference.segment;
  2185. {$endif i8086}
  2186. {$ifndef gdb_supports_DW_AT_variable_parameter}
  2187. { Parameters which are passed by reference. (var and the like)
  2188. Hide the reference-pointer and dereference the pointer
  2189. in the DW_AT_location block.
  2190. }
  2191. if (sym.typ=paravarsym) and
  2192. paramanager.push_addr_param(sym.varspez,sym.vardef,tprocdef(sym.owner.defowner).proccalloption) and
  2193. not(vo_has_local_copy in sym.varoptions) and
  2194. not is_open_string(sym.vardef) then
  2195. begin
  2196. templist.concat(tai_const.create_8bit(ord(DW_OP_deref)));
  2197. inc(blocksize);
  2198. end
  2199. {$endif not gdb_supports_DW_AT_variable_parameter}
  2200. end;
  2201. end
  2202. else
  2203. internalerror(200601288);
  2204. end;
  2205. end;
  2206. end;
  2207. { function results must not be added to the parameter list,
  2208. as they are not part of the signature of the function
  2209. (gdb automatically adds them according to the ABI specifications
  2210. when calling the function)
  2211. }
  2212. if (sym.typ=paravarsym) and
  2213. not(dvf_force_local_var in flags) and
  2214. not(vo_is_funcret in sym.varoptions) then
  2215. tag:=DW_TAG_formal_parameter
  2216. else
  2217. tag:=DW_TAG_variable;
  2218. { must be parasym of externally implemented procdef, but
  2219. the parasymtable can con also contain e.g. absolutevarsyms
  2220. -> check symtabletype}
  2221. if (sym.owner.symtabletype=parasymtable) and
  2222. (sym.localloc.loc=LOC_INVALID) then
  2223. begin
  2224. if (sym.owner.symtabletype<>parasymtable) then
  2225. internalerror(2009101001);
  2226. append_entry(tag,false,[
  2227. DW_AT_name,DW_FORM_string,name+#0
  2228. {
  2229. DW_AT_decl_file,DW_FORM_data1,0,
  2230. DW_AT_decl_line,DW_FORM_data1,
  2231. }
  2232. ])
  2233. end
  2234. else if not(sym.localloc.loc in [LOC_REGISTER,LOC_CREGISTER,LOC_MMREGISTER,
  2235. LOC_CMMREGISTER,LOC_FPUREGISTER,LOC_CFPUREGISTER]) and
  2236. ((sym.owner.symtabletype = globalsymtable) or
  2237. (sp_static in sym.symoptions) or
  2238. (vo_is_public in sym.varoptions)) then
  2239. append_entry(tag,false,[
  2240. DW_AT_name,DW_FORM_string,name+#0,
  2241. {
  2242. DW_AT_decl_file,DW_FORM_data1,0,
  2243. DW_AT_decl_line,DW_FORM_data1,
  2244. }
  2245. DW_AT_external,DW_FORM_flag,true,
  2246. { data continues below }
  2247. DW_AT_location,DW_FORM_block1,blocksize
  2248. ])
  2249. {$ifdef gdb_supports_DW_AT_variable_parameter}
  2250. else if (sym.typ=paravarsym) and
  2251. paramanager.push_addr_param(sym.varspez,sym.vardef,tprocdef(sym.owner.defowner).proccalloption) and
  2252. not(vo_has_local_copy in sym.varoptions) and
  2253. not is_open_string(sym.vardef) then
  2254. append_entry(tag,false,[
  2255. DW_AT_name,DW_FORM_string,name+#0,
  2256. DW_AT_variable_parameter,DW_FORM_flag,true,
  2257. {
  2258. DW_AT_decl_file,DW_FORM_data1,0,
  2259. DW_AT_decl_line,DW_FORM_data1,
  2260. }
  2261. { data continues below }
  2262. DW_AT_location,DW_FORM_block1,blocksize
  2263. ])
  2264. {$endif gdb_supports_DW_AT_variable_parameter}
  2265. else
  2266. append_entry(tag,false,[
  2267. DW_AT_name,DW_FORM_string,name+#0,
  2268. {
  2269. DW_AT_decl_file,DW_FORM_data1,0,
  2270. DW_AT_decl_line,DW_FORM_data1,
  2271. }
  2272. { data continues below }
  2273. DW_AT_location,DW_FORM_block1,blocksize
  2274. ]);
  2275. { append block data }
  2276. current_asmdata.asmlists[al_dwarf_info].concatlist(templist);
  2277. { Mark self as artificial for methods, because gdb uses the fact
  2278. whether or not the first parameter of a method is artificial to
  2279. distinguish regular from static methods (since there are no
  2280. no vo_is_self parameters for static methods, we don't have to check
  2281. that). }
  2282. if (vo_is_self in sym.varoptions) then
  2283. append_attribute(DW_AT_artificial,DW_FORM_flag,[true]);
  2284. append_labelentry_ref(DW_AT_type,def_dwarf_lab(def));
  2285. {$ifdef i8086}
  2286. if has_segment_sym_name then
  2287. append_seg_name(segment_sym_name)
  2288. else if segment_reg<>NR_NO then
  2289. append_seg_reg(segment_reg);
  2290. {$endif i8086}
  2291. templist.free;
  2292. finish_entry;
  2293. end;
  2294. procedure TDebugInfoDwarf.appendsym_staticvar(list:TAsmList;sym:tstaticvarsym);
  2295. begin
  2296. appendsym_var(list,sym);
  2297. end;
  2298. procedure TDebugInfoDwarf.appendsym_localvar(list:TAsmList;sym:tlocalvarsym);
  2299. begin
  2300. appendsym_var(list,sym);
  2301. end;
  2302. procedure TDebugInfoDwarf.appendsym_paravar(list:TAsmList;sym:tparavarsym);
  2303. begin
  2304. appendsym_var(list,sym);
  2305. end;
  2306. procedure TDebugInfoDwarf.appendsym_fieldvar(list:TAsmList;sym: tfieldvarsym);
  2307. begin
  2308. appendsym_fieldvar_with_name_offset(list,sym,symname(sym, false),sym.vardef,0);
  2309. end;
  2310. procedure TDebugInfoDwarf.appendsym_fieldvar_with_name_offset(list:TAsmList;sym: tfieldvarsym;const name: string; def: tdef; offset: pint);
  2311. var
  2312. bitoffset,
  2313. fieldoffset,
  2314. fieldnatsize: asizeint;
  2315. begin
  2316. if (sp_static in sym.symoptions) or
  2317. (sym.visibility=vis_hidden) then
  2318. exit;
  2319. if (tabstractrecordsymtable(sym.owner).usefieldalignment<>bit_alignment) or
  2320. { only ordinals are bitpacked }
  2321. not is_ordinal(sym.vardef) then
  2322. begin
  2323. { other kinds of fields can however also appear in a bitpacked }
  2324. { record, and then their offset is also specified in bits rather }
  2325. { than in bytes }
  2326. if (tabstractrecordsymtable(sym.owner).usefieldalignment<>bit_alignment) then
  2327. fieldoffset:=sym.fieldoffset
  2328. else
  2329. fieldoffset:=sym.fieldoffset div 8;
  2330. inc(fieldoffset,offset);
  2331. append_entry(DW_TAG_member,false,[
  2332. DW_AT_name,DW_FORM_string,name+#0,
  2333. DW_AT_data_member_location,DW_FORM_block1,1+lengthuleb128(fieldoffset)
  2334. ]);
  2335. end
  2336. else
  2337. begin
  2338. if (sym.vardef.packedbitsize > 255) then
  2339. internalerror(2007061201);
  2340. { we don't bitpack according to the ABI, but as close as }
  2341. { possible, i.e., equivalent to gcc's }
  2342. { __attribute__((__packed__)), which is also what gpc }
  2343. { does. }
  2344. fieldnatsize:=max(sizeof(pint),sym.vardef.size);
  2345. fieldoffset:=(sym.fieldoffset div (fieldnatsize*8)) * fieldnatsize;
  2346. inc(fieldoffset,offset);
  2347. bitoffset:=sym.fieldoffset mod (fieldnatsize*8);
  2348. if (target_info.endian=endian_little) then
  2349. bitoffset:=(fieldnatsize*8)-bitoffset-sym.vardef.packedbitsize;
  2350. append_entry(DW_TAG_member,false,[
  2351. DW_AT_name,DW_FORM_string,symname(sym, false)+#0,
  2352. { gcc also generates both a bit and byte size attribute }
  2353. { we don't support ordinals >= 256 bits }
  2354. DW_AT_byte_size,DW_FORM_data1,fieldnatsize,
  2355. { nor >= 256 bits (not yet, anyway, see IE above) }
  2356. DW_AT_bit_size,DW_FORM_data1,sym.vardef.packedbitsize,
  2357. { data1 and data2 are unsigned, bitoffset can also be negative }
  2358. DW_AT_bit_offset,DW_FORM_data4,bitoffset,
  2359. DW_AT_data_member_location,DW_FORM_block1,1+lengthuleb128(fieldoffset)
  2360. ]);
  2361. end;
  2362. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_plus_uconst)));
  2363. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_uleb128bit(fieldoffset));
  2364. if (sym.owner.symtabletype in [objectsymtable,recordsymtable]) then
  2365. append_visibility(sym.visibility);
  2366. append_labelentry_ref(DW_AT_type,def_dwarf_lab(def));
  2367. finish_entry;
  2368. end;
  2369. procedure TDebugInfoDwarf.appendsym_const(list:TAsmList;sym:tconstsym);
  2370. begin
  2371. appendsym_const_member(list,sym,false);
  2372. end;
  2373. procedure TDebugInfoDwarf.appendsym_const_member(list:TAsmList;sym:tconstsym;ismember:boolean);
  2374. var
  2375. i,
  2376. size: aint;
  2377. usedef: tdef;
  2378. begin
  2379. { These are default values of parameters. These should be encoded
  2380. via DW_AT_default_value, not as a separate sym. Moreover, their
  2381. type is not available when writing the debug info for external
  2382. procedures.
  2383. }
  2384. if (sym.owner.symtabletype=parasymtable) then
  2385. exit;
  2386. if ismember then
  2387. append_entry(DW_TAG_member,false,[
  2388. DW_AT_name,DW_FORM_string,symname(sym, false)+#0,
  2389. { The DW_AT_declaration tag is invalid according to the DWARF specifications.
  2390. But gcc adds this to static const members and gdb checks
  2391. for this flag. So we have to set it also.
  2392. }
  2393. DW_AT_declaration,DW_FORM_flag,true,
  2394. DW_AT_external,DW_FORM_flag,true
  2395. ])
  2396. else
  2397. append_entry(DW_TAG_variable,false,[
  2398. DW_AT_name,DW_FORM_string,symname(sym, false)+#0
  2399. ]);
  2400. { for string constants, constdef isn't set because they have no real type }
  2401. case sym.consttyp of
  2402. conststring:
  2403. begin
  2404. { if DW_FORM_string is used below one day, this usedef should
  2405. probably become nil }
  2406. { note: < 255 instead of <= 255 because we have to store the
  2407. entire length of the string as well, and 256 does not fit in
  2408. a byte }
  2409. if (sym.value.len<255) then
  2410. usedef:=cshortstringtype
  2411. else
  2412. usedef:=clongstringtype;
  2413. end;
  2414. constresourcestring,
  2415. constwstring:
  2416. usedef:=nil;
  2417. else
  2418. usedef:=sym.constdef;
  2419. end;
  2420. if assigned(usedef) then
  2421. append_labelentry_ref(DW_AT_type,def_dwarf_lab(usedef));
  2422. AddConstToAbbrev(ord(DW_AT_const_value));
  2423. case sym.consttyp of
  2424. conststring:
  2425. begin
  2426. { DW_FORM_string isn't supported yet by the Pascal value printer
  2427. -> create a string using raw bytes }
  2428. if (sym.value.len<255) then
  2429. begin
  2430. AddConstToAbbrev(ord(DW_FORM_block1));
  2431. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(sym.value.len+1));
  2432. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(sym.value.len));
  2433. end
  2434. else
  2435. begin
  2436. AddConstToAbbrev(ord(DW_FORM_block));
  2437. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_uleb128bit(sym.value.len+sizesinttype.size));
  2438. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.Create_sizeint_unaligned(sym.value.len));
  2439. end;
  2440. i:=0;
  2441. size:=sym.value.len;
  2442. while(i<size) do
  2443. begin
  2444. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit((pbyte(sym.value.valueptr+i)^)));
  2445. inc(i);
  2446. end;
  2447. end;
  2448. constguid,
  2449. constset:
  2450. begin
  2451. AddConstToAbbrev(ord(DW_FORM_block1));
  2452. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(usedef.size));
  2453. i:=0;
  2454. size:=sym.constdef.size;
  2455. while (i<size) do
  2456. begin
  2457. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit((pbyte(sym.value.valueptr+i)^)));
  2458. inc(i);
  2459. end;
  2460. end;
  2461. constwstring,
  2462. constresourcestring:
  2463. begin
  2464. { write dummy for now }
  2465. AddConstToAbbrev(ord(DW_FORM_string));
  2466. current_asmdata.asmlists[al_dwarf_info].concat(tai_string.create(''));
  2467. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(0));
  2468. end;
  2469. constord:
  2470. begin
  2471. if (sym.value.valueord<0) then
  2472. begin
  2473. AddConstToAbbrev(ord(DW_FORM_sdata));
  2474. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_sleb128bit(sym.value.valueord.svalue));
  2475. end
  2476. else
  2477. begin
  2478. AddConstToAbbrev(ord(DW_FORM_udata));
  2479. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_uleb128bit(sym.value.valueord.uvalue));
  2480. end;
  2481. end;
  2482. constnil:
  2483. begin
  2484. {$ifdef cpu64bitaddr}
  2485. AddConstToAbbrev(ord(DW_FORM_data8));
  2486. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_64bit_unaligned(0));
  2487. {$else cpu64bitaddr}
  2488. AddConstToAbbrev(ord(DW_FORM_data4));
  2489. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_32bit_unaligned(0));
  2490. {$endif cpu64bitaddr}
  2491. end;
  2492. constpointer:
  2493. begin
  2494. {$ifdef cpu64bitaddr}
  2495. AddConstToAbbrev(ord(DW_FORM_data8));
  2496. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_64bit_unaligned(int64(sym.value.valueordptr)));
  2497. {$else cpu64bitaddr}
  2498. AddConstToAbbrev(ord(DW_FORM_data4));
  2499. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_32bit_unaligned(longint(sym.value.valueordptr)));
  2500. {$endif cpu64bitaddr}
  2501. end;
  2502. constreal:
  2503. begin
  2504. AddConstToAbbrev(ord(DW_FORM_block1));
  2505. case tfloatdef(sym.constdef).floattype of
  2506. s32real:
  2507. begin
  2508. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(4));
  2509. current_asmdata.asmlists[al_dwarf_info].concat(tai_realconst.create_s32real(pbestreal(sym.value.valueptr)^));
  2510. end;
  2511. s64real:
  2512. begin
  2513. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(8));
  2514. current_asmdata.asmlists[al_dwarf_info].concat(tai_realconst.create_s64real(pbestreal(sym.value.valueptr)^));
  2515. end;
  2516. s64comp,
  2517. s64currency:
  2518. begin
  2519. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(8));
  2520. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_64bit_unaligned(trunc(pbestreal(sym.value.valueptr)^)));
  2521. end;
  2522. s80real,
  2523. sc80real:
  2524. begin
  2525. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(sym.constdef.size));
  2526. current_asmdata.asmlists[al_dwarf_info].concat(tai_realconst.create_s80real(pextended(sym.value.valueptr)^,sym.constdef.size));
  2527. end;
  2528. else
  2529. internalerror(200601291);
  2530. end;
  2531. end;
  2532. else
  2533. internalerror(200601292);
  2534. end;
  2535. finish_entry;
  2536. end;
  2537. procedure TDebugInfoDwarf.appendsym_label(list:TAsmList;sym: tlabelsym);
  2538. begin
  2539. { ignore label syms for now, the problem is that a label sym
  2540. can have more than one label associated e.g. in case of
  2541. an inline procedure expansion }
  2542. end;
  2543. procedure TDebugInfoDwarf.appendsym_property(list:TAsmList;sym: tpropertysym);
  2544. var
  2545. symlist: ppropaccesslistitem;
  2546. tosym: tabstractvarsym;
  2547. offset: pint;
  2548. begin
  2549. if assigned(sym.propaccesslist[palt_read]) and
  2550. not assigned(sym.propaccesslist[palt_read].procdef) then
  2551. symlist:=sym.propaccesslist[palt_read].firstsym
  2552. else
  2553. { can't handle }
  2554. exit;
  2555. if not get_symlist_sym_offset(symlist,tosym,offset) then
  2556. exit;
  2557. if not (tosym.owner.symtabletype in [objectsymtable,recordsymtable]) then
  2558. begin
  2559. if (tosym.typ=fieldvarsym) then
  2560. internalerror(2009031404);
  2561. appendsym_var_with_name_type_offset(list,tabstractnormalvarsym(tosym),symname(sym, false),sym.propdef,offset,[])
  2562. end
  2563. else
  2564. appendsym_fieldvar_with_name_offset(list,tfieldvarsym(tosym),symname(sym, false),sym.propdef,offset)
  2565. end;
  2566. function TDebugInfoDwarf.symdebugname(sym: tsym): String;
  2567. begin
  2568. result := sym.name;
  2569. end;
  2570. procedure TDebugInfoDwarf.appendsym_type(list:TAsmList;sym: ttypesym);
  2571. begin
  2572. { just queue the def if needed, beforeappenddef will
  2573. emit the typedef if necessary }
  2574. def_dwarf_lab(sym.typedef);
  2575. end;
  2576. procedure TDebugInfoDwarf.appendsym_absolute(list:TAsmList;sym:tabsolutevarsym);
  2577. var
  2578. templist : TAsmList;
  2579. blocksize : longint;
  2580. symlist : ppropaccesslistitem;
  2581. tosym: tabstractvarsym;
  2582. offset: pint;
  2583. flags: tdwarfvarsymflags;
  2584. begin
  2585. templist:=TAsmList.create;
  2586. case tabsolutevarsym(sym).abstyp of
  2587. toaddr :
  2588. begin
  2589. { MWE: replaced ifdef i368 }
  2590. (*
  2591. if target_cpu = cpu_i386 then
  2592. begin
  2593. { in theory, we could write a DW_AT_segment entry here for sym.absseg,
  2594. however I doubt that gdb supports this (FK) }
  2595. end;
  2596. *)
  2597. templist.concat(tai_const.create_8bit(3));
  2598. {$ifdef avr}
  2599. // Add $800000 to indicate that the address is in memory space
  2600. templist.concat(tai_const.create_int_dataptr_unaligned(sym.addroffset + $800000, aitconst_ptr_unaligned));
  2601. {$else}
  2602. templist.concat(tai_const.create_int_dataptr_unaligned(sym.addroffset));
  2603. {$endif}
  2604. blocksize:=1+sizeof(puint);
  2605. end;
  2606. toasm :
  2607. begin
  2608. templist.concat(tai_const.create_8bit(3));
  2609. templist.concat(tai_const.create_type_name(aitconst_ptr_unaligned,sym.mangledname,0));
  2610. blocksize:=1+sizeof(puint);
  2611. end;
  2612. tovar:
  2613. begin
  2614. symlist:=tabsolutevarsym(sym).ref.firstsym;
  2615. if get_symlist_sym_offset(symlist,tosym,offset) then
  2616. begin
  2617. if (tosym.typ=fieldvarsym) then
  2618. internalerror(2009031402);
  2619. flags:=[];
  2620. if (sym.owner.symtabletype=localsymtable) then
  2621. include(flags,dvf_force_local_var);
  2622. appendsym_var_with_name_type_offset(list,tabstractnormalvarsym(tosym),symname(sym, false),tabstractvarsym(sym).vardef,offset,flags);
  2623. end;
  2624. templist.free;
  2625. exit;
  2626. end;
  2627. end;
  2628. append_entry(DW_TAG_variable,false,[
  2629. DW_AT_name,DW_FORM_string,symname(sym, false)+#0,
  2630. {
  2631. DW_AT_decl_file,DW_FORM_data1,0,
  2632. DW_AT_decl_line,DW_FORM_data1,
  2633. }
  2634. DW_AT_external,DW_FORM_flag,true,
  2635. { data continues below }
  2636. DW_AT_location,DW_FORM_block1,blocksize
  2637. ]);
  2638. { append block data }
  2639. current_asmdata.asmlists[al_dwarf_info].concatlist(templist);
  2640. append_labelentry_ref(DW_AT_type,def_dwarf_lab(sym.vardef));
  2641. templist.free;
  2642. finish_entry;
  2643. end;
  2644. procedure TDebugInfoDwarf.beforeappendsym(list:TAsmList;sym:tsym);
  2645. begin
  2646. current_asmdata.asmlists[al_dwarf_info].concat(tai_comment.Create(strpnew('Symbol '+symname(sym, true))));
  2647. end;
  2648. procedure TDebugInfoDwarf.insertmoduleinfo;
  2649. var
  2650. templist: TAsmList;
  2651. linelist: TAsmList;
  2652. lbl : tasmlabel;
  2653. n,m : Integer;
  2654. ditem : TDirIndexItem;
  2655. fitem : TFileIndexItem;
  2656. flist : TFPList;
  2657. dbgname : TSymStr;
  2658. begin
  2659. if not (target_info.system in systems_wasm) then
  2660. begin
  2661. { insert DEBUGSTART and DEBUGEND labels }
  2662. dbgname:=make_mangledname('DEBUGSTART',current_module.localsymtable,'');
  2663. { Darwin's linker does not like two global labels both pointing to the
  2664. end of a section, which can happen in case of units without code ->
  2665. make them local; we don't need the debugtable stuff there either,
  2666. so it doesn't matter that they are not global.
  2667. }
  2668. if (target_info.system in systems_darwin) then
  2669. dbgname:='L'+dbgname;
  2670. new_section(current_asmdata.asmlists[al_start],sec_code,dbgname,0,secorder_begin);
  2671. if not(target_info.system in systems_darwin) then
  2672. current_asmdata.asmlists[al_start].concat(tai_symbol.Createname_global(dbgname,AT_METADATA,0,voidpointertype))
  2673. else
  2674. current_asmdata.asmlists[al_start].concat(tai_symbol.Createname(dbgname,AT_METADATA,0,voidpointertype));
  2675. dbgname:=make_mangledname('DEBUGEND',current_module.localsymtable,'');
  2676. { See above. }
  2677. if (target_info.system in systems_darwin) then
  2678. dbgname:='L'+dbgname;
  2679. new_section(current_asmdata.asmlists[al_end],sec_code,dbgname,0,secorder_end);
  2680. if not(target_info.system in systems_darwin) then
  2681. current_asmdata.asmlists[al_end].concat(tai_symbol.Createname_global(dbgname,AT_METADATA,0,voidpointertype))
  2682. else
  2683. current_asmdata.asmlists[al_end].concat(tai_symbol.Createname(dbgname,AT_METADATA,0,voidpointertype));
  2684. end;
  2685. { insert .Ldebug_abbrev0 label }
  2686. templist:=TAsmList.create;
  2687. new_section(templist,sec_debug_abbrev,'',0);
  2688. templist.concat(tai_symbol.createname(target_asm.labelprefix+'debug_abbrevsection0',AT_METADATA,0,voidpointertype));
  2689. { add any extra stuff which needs to be in the abbrev section, but before }
  2690. { the actual abbreviations, in between the symbol above and below, i.e. here }
  2691. templist.concat(tai_symbol.createname(target_asm.labelprefix+'debug_abbrev0',AT_METADATA,0,voidpointertype));
  2692. current_asmdata.asmlists[al_start].insertlist(templist);
  2693. templist.free;
  2694. { insert .Ldebug_line0 label }
  2695. templist:=TAsmList.create;
  2696. new_section(templist,sec_debug_line,'',0);
  2697. templist.concat(tai_symbol.createname(target_asm.labelprefix+'debug_linesection0',AT_METADATA,0,voidpointertype));
  2698. { add any extra stuff which needs to be in the line section, but before }
  2699. { the actual line info, in between the symbol above and below, i.e. here }
  2700. templist.concat(tai_symbol.createname(target_asm.labelprefix+'debug_line0',AT_METADATA,0,voidpointertype));
  2701. current_asmdata.asmlists[al_start].insertlist(templist);
  2702. templist.free;
  2703. { finalize line info if the unit doesn't contain any function/ }
  2704. { procedure/init/final code }
  2705. finish_lineinfo;
  2706. { debug line header }
  2707. linelist := current_asmdata.asmlists[al_dwarf_line];
  2708. new_section(linelist,sec_debug_line,'',0);
  2709. linelist.concat(tai_comment.Create(strpnew('=== header start ===')));
  2710. { size }
  2711. current_asmdata.getlabel(lbl,alt_dbgfile);
  2712. if use_64bit_headers then
  2713. linelist.concat(tai_const.create_32bit_unaligned(longint($FFFFFFFF)));
  2714. linelist.concat(tai_const.create_rel_sym(offsetreltype,
  2715. lbl,current_asmdata.DefineAsmSymbol(target_asm.labelprefix+'edebug_line0',AB_LOCAL,AT_METADATA,voidpointertype)));
  2716. linelist.concat(tai_label.create(lbl));
  2717. { version }
  2718. linelist.concat(tai_const.create_16bit_unaligned(dwarf_version));
  2719. { header length }
  2720. current_asmdata.getlabel(lbl,alt_dbgfile);
  2721. linelist.concat(tai_const.create_rel_sym(offsetreltype,
  2722. lbl,current_asmdata.DefineAsmSymbol(target_asm.labelprefix+'ehdebug_line0',AB_LOCAL,AT_METADATA,voidpointertype)));
  2723. linelist.concat(tai_label.create(lbl));
  2724. { minimum_instruction_length }
  2725. linelist.concat(tai_const.create_8bit(1));
  2726. { default_is_stmt }
  2727. linelist.concat(tai_const.create_8bit(1));
  2728. { line_base }
  2729. linelist.concat(tai_const.create_8bit(LINE_BASE));
  2730. { line_range }
  2731. { only line increase, no adress }
  2732. linelist.concat(tai_const.create_8bit(255));
  2733. { opcode_base }
  2734. linelist.concat(tai_const.create_8bit(OPCODE_BASE));
  2735. { standard_opcode_lengths }
  2736. { MWE: sigh... why adding the default lengths (and make those sizes sense with LEB encoding) }
  2737. { DW_LNS_copy }
  2738. linelist.concat(tai_const.create_8bit(0));
  2739. { DW_LNS_advance_pc }
  2740. linelist.concat(tai_const.create_8bit(1));
  2741. { DW_LNS_advance_line }
  2742. linelist.concat(tai_const.create_8bit(1));
  2743. { DW_LNS_set_file }
  2744. linelist.concat(tai_const.create_8bit(1));
  2745. { DW_LNS_set_column }
  2746. linelist.concat(tai_const.create_8bit(1));
  2747. { DW_LNS_negate_stmt }
  2748. linelist.concat(tai_const.create_8bit(0));
  2749. { DW_LNS_set_basic_block }
  2750. linelist.concat(tai_const.create_8bit(0));
  2751. { DW_LNS_const_add_pc }
  2752. linelist.concat(tai_const.create_8bit(0));
  2753. { DW_LNS_fixed_advance_pc }
  2754. linelist.concat(tai_const.create_8bit(1));
  2755. { DW_LNS_set_prologue_end }
  2756. linelist.concat(tai_const.create_8bit(0));
  2757. { DW_LNS_set_epilogue_begin }
  2758. linelist.concat(tai_const.create_8bit(0));
  2759. { DW_LNS_set_isa }
  2760. linelist.concat(tai_const.create_8bit(1));
  2761. { Create single list of filenames sorted in IndexNr }
  2762. flist:=TFPList.Create;
  2763. for n := 0 to dirlist.Count - 1 do
  2764. begin
  2765. ditem := TDirIndexItem(dirlist[n]);
  2766. for m := 0 to ditem.Files.Count - 1 do
  2767. flist.Add(ditem.Files[m]);
  2768. end;
  2769. flist.Sort(@FileListSortCompare);
  2770. { include_directories }
  2771. linelist.concat(tai_comment.Create(strpnew('include_directories')));
  2772. for n := 0 to dirlist.Count - 1 do
  2773. begin
  2774. ditem := TDirIndexItem(dirlist[n]);
  2775. if ditem.Name = '.' then
  2776. Continue;
  2777. { Write without trailing path delimiter and also don't prefix with ./ for current dir (already done while adding to dirlist }
  2778. linelist.concat(tai_string.create(ditem.Name+#0));
  2779. end;
  2780. linelist.concat(tai_const.create_8bit(0));
  2781. { file_names }
  2782. linelist.concat(tai_comment.Create(strpnew('file_names')));
  2783. for n := 0 to flist.Count - 1 do
  2784. begin
  2785. fitem := TFileIndexItem(flist[n]);
  2786. { file name }
  2787. linelist.concat(tai_string.create(fitem.Name+#0));
  2788. { directory index }
  2789. linelist.concat(tai_const.create_uleb128bit(fitem.DirIndex));
  2790. { last modification }
  2791. linelist.concat(tai_const.create_uleb128bit(0));
  2792. { file length }
  2793. linelist.concat(tai_const.create_uleb128bit(0));
  2794. end;
  2795. linelist.concat(tai_const.create_8bit(0));
  2796. { end of debug line header }
  2797. linelist.concat(tai_symbol.createname(target_asm.labelprefix+'ehdebug_line0',AT_METADATA,0,voidpointertype));
  2798. linelist.concat(tai_comment.Create(strpnew('=== header end ===')));
  2799. { add line program }
  2800. linelist.concatList(asmline);
  2801. { end of debug line table }
  2802. linelist.concat(tai_symbol.createname(target_asm.labelprefix+'edebug_line0',AT_METADATA,0,voidpointertype));
  2803. flist.free;
  2804. end;
  2805. procedure TDebugInfoDwarf.inserttypeinfo;
  2806. var
  2807. storefilepos : tfileposinfo;
  2808. lenstartlabel,arangestartlabel: tasmlabel;
  2809. i : longint;
  2810. def: tdef;
  2811. dbgname: string;
  2812. vardatatype: ttypesym;
  2813. bind: tasmsymbind;
  2814. lang: tdwarf_source_language;
  2815. begin
  2816. include(current_module.moduleflags,mf_has_dwarf_debuginfo);
  2817. storefilepos:=current_filepos;
  2818. current_filepos:=current_module.mainfilepos;
  2819. if assigned(dwarflabels) then
  2820. internalerror(2015100301);
  2821. { one item per def, plus some extra space in case of nested types,
  2822. externally used types etc (it will grow further if necessary) }
  2823. i:=current_module.localsymtable.DefList.count*4;
  2824. if assigned(current_module.globalsymtable) then
  2825. inc(i,current_module.globalsymtable.DefList.count*2);
  2826. dwarflabels:=TDwarfLabHashSet.Create(i,true,false);
  2827. currabbrevnumber:=0;
  2828. defnumberlist:=TFPObjectList.create(false);
  2829. deftowritelist:=TFPObjectList.create(false);
  2830. { not exported (FK)
  2831. FILEREC
  2832. TEXTREC
  2833. }
  2834. vardatatype:=try_search_system_type('TVARDATA');
  2835. if assigned(vardatatype) then
  2836. vardatadef:=trecorddef(vardatatype.typedef);
  2837. { write start labels }
  2838. new_section(current_asmdata.asmlists[al_dwarf_info],sec_debug_info,'',0);
  2839. current_asmdata.asmlists[al_dwarf_info].concat(tai_symbol.createname(target_asm.labelprefix+'debug_info0',AT_METADATA,0,voidpointertype));
  2840. { start abbrev section }
  2841. new_section(current_asmdata.asmlists[al_dwarf_abbrev],sec_debug_abbrev,'',0);
  2842. if not(target_info.system in systems_darwin) then
  2843. begin
  2844. { start aranges section }
  2845. new_section(current_asmdata.asmlists[al_dwarf_aranges],sec_debug_aranges,'',0);
  2846. current_asmdata.getlabel(arangestartlabel,alt_dbgfile);
  2847. if use_64bit_headers then
  2848. current_asmdata.asmlists[al_dwarf_aranges].concat(tai_const.create_32bit_unaligned(longint($FFFFFFFF)));
  2849. current_asmdata.asmlists[al_dwarf_aranges].concat(tai_const.create_rel_sym(offsetreltype,
  2850. arangestartlabel,current_asmdata.DefineAsmSymbol(target_asm.labelprefix+'earanges0',AB_LOCAL,AT_METADATA,voidpointertype)));
  2851. current_asmdata.asmlists[al_dwarf_aranges].concat(tai_label.create(arangestartlabel));
  2852. current_asmdata.asmlists[al_dwarf_aranges].concat(tai_const.create_16bit_unaligned(2));
  2853. if not(tf_dwarf_relative_addresses in target_info.flags) then
  2854. current_asmdata.asmlists[al_dwarf_aranges].concat(tai_const.create_type_sym(offsetabstype,
  2855. current_asmdata.DefineAsmSymbol(target_asm.labelprefix+'debug_info0',AB_LOCAL,AT_METADATA,voidpointertype)))
  2856. else
  2857. current_asmdata.asmlists[al_dwarf_aranges].concat(tai_const.create_rel_sym(offsetreltype,
  2858. current_asmdata.DefineAsmSymbol(target_asm.labelprefix+'debug_infosection0',AB_LOCAL,AT_METADATA,voidpointertype),
  2859. current_asmdata.DefineAsmSymbol(target_asm.labelprefix+'debug_info0',AB_LOCAL,AT_METADATA,voidpointertype)));
  2860. {$ifdef i8086}
  2861. { address_size }
  2862. current_asmdata.asmlists[al_dwarf_aranges].concat(tai_const.create_8bit(4));
  2863. { segment_size }
  2864. current_asmdata.asmlists[al_dwarf_aranges].concat(tai_const.create_8bit(2));
  2865. { no alignment/padding bytes on i8086 for Open Watcom compatibility }
  2866. {$else i8086}
  2867. { address_size }
  2868. current_asmdata.asmlists[al_dwarf_aranges].concat(tai_const.create_8bit(sizeof(pint)));
  2869. { segment_size }
  2870. current_asmdata.asmlists[al_dwarf_aranges].concat(tai_const.create_8bit(0));
  2871. { alignment }
  2872. current_asmdata.asmlists[al_dwarf_aranges].concat(tai_const.create_32bit_unaligned(0));
  2873. {$endif i8086}
  2874. { start ranges section }
  2875. new_section(current_asmdata.asmlists[al_dwarf_ranges],sec_debug_ranges,'',0);
  2876. end;
  2877. { debug info header }
  2878. current_asmdata.getlabel(lenstartlabel,alt_dbgfile);
  2879. { size }
  2880. if use_64bit_headers then
  2881. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_32bit_unaligned(longint($FFFFFFFF)));
  2882. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_rel_sym(offsetreltype,
  2883. lenstartlabel,current_asmdata.DefineAsmSymbol(target_asm.labelprefix+'edebug_info0',AB_LOCAL,AT_METADATA,voidpointertype)));
  2884. current_asmdata.asmlists[al_dwarf_info].concat(tai_label.create(lenstartlabel));
  2885. { version }
  2886. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_16bit_unaligned(dwarf_version));
  2887. { abbrev table (=relative from section start)}
  2888. if not(tf_dwarf_relative_addresses in target_info.flags) then
  2889. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_type_sym(offsetabstype,
  2890. current_asmdata.DefineAsmSymbol(target_asm.labelprefix+'debug_abbrev0',AB_LOCAL,AT_METADATA,voidpointertype)))
  2891. else
  2892. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_rel_sym(offsetreltype,
  2893. current_asmdata.DefineAsmSymbol(target_asm.labelprefix+'debug_abbrevsection0',AB_LOCAL,AT_METADATA,voidpointertype),
  2894. current_asmdata.DefineAsmSymbol(target_asm.labelprefix+'debug_abbrev0',AB_LOCAL,AT_METADATA,voidpointertype)));
  2895. { address size }
  2896. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(sizeof(pint)));
  2897. if (ds_dwarf_cpp in current_settings.debugswitches) then
  2898. lang:=DW_LANG_C_plus_plus
  2899. else
  2900. lang:=DW_LANG_Pascal83;
  2901. { first manadatory compilation unit TAG }
  2902. append_entry(DW_TAG_compile_unit,true,[
  2903. DW_AT_name,DW_FORM_string,relative_dwarf_path(current_module.sourcefiles.get_file(1).path+current_module.sourcefiles.get_file(1).name)+#0,
  2904. DW_AT_producer,DW_FORM_string,'Free Pascal '+full_version_string+' '+date_string+#0,
  2905. DW_AT_comp_dir,DW_FORM_string,BSToSlash(FixPath(GetCurrentDir,false))+#0,
  2906. DW_AT_language,DW_FORM_data1,lang,
  2907. DW_AT_identifier_case,DW_FORM_data1,DW_ID_case_insensitive]);
  2908. {$ifdef i8086}
  2909. case current_settings.x86memorymodel of
  2910. mm_tiny,
  2911. mm_small:
  2912. append_attribute(DW_AT_WATCOM_memory_model,DW_FORM_data1,[DW_WATCOM_MEMORY_MODEL_small]);
  2913. mm_medium:
  2914. append_attribute(DW_AT_WATCOM_memory_model,DW_FORM_data1,[DW_WATCOM_MEMORY_MODEL_medium]);
  2915. mm_compact:
  2916. append_attribute(DW_AT_WATCOM_memory_model,DW_FORM_data1,[DW_WATCOM_MEMORY_MODEL_compact]);
  2917. mm_large:
  2918. append_attribute(DW_AT_WATCOM_memory_model,DW_FORM_data1,[DW_WATCOM_MEMORY_MODEL_large]);
  2919. mm_huge:
  2920. append_attribute(DW_AT_WATCOM_memory_model,DW_FORM_data1,[DW_WATCOM_MEMORY_MODEL_huge]);
  2921. end;
  2922. {$endif i8086}
  2923. { reference to line info section }
  2924. if not(tf_dwarf_relative_addresses in target_info.flags) then
  2925. append_labelentry_dataptr_abs(DW_AT_stmt_list,current_asmdata.DefineAsmSymbol(target_asm.labelprefix+'debug_line0',AB_LOCAL,AT_METADATA,voidpointertype))
  2926. else
  2927. append_labelentry_dataptr_rel(DW_AT_stmt_list,
  2928. current_asmdata.DefineAsmSymbol(target_asm.labelprefix+'debug_linesection0',AB_LOCAL,AT_METADATA,voidpointertype),
  2929. current_asmdata.DefineAsmSymbol(target_asm.labelprefix+'debug_line0',AB_LOCAL,AT_METADATA,voidpointertype));
  2930. if (m_objectivec1 in current_settings.modeswitches) then
  2931. append_attribute(DW_AT_APPLE_major_runtime_vers,DW_FORM_data1,[1]);
  2932. if target_info.system in systems_wasm then
  2933. begin
  2934. append_attribute(DW_AT_low_pc,DW_FORM_data4,[0]);
  2935. { todo: append DW_AT_ranges }
  2936. end
  2937. else
  2938. begin
  2939. dbgname:=make_mangledname('DEBUGSTART',current_module.localsymtable,'');
  2940. if (target_info.system in systems_darwin) then
  2941. begin
  2942. bind:=AB_LOCAL;
  2943. dbgname:='L'+dbgname;
  2944. end
  2945. else
  2946. bind:=AB_GLOBAL;
  2947. append_labelentry(DW_AT_low_pc,current_asmdata.DefineAsmSymbol(dbgname,bind,AT_METADATA,voidpointertype));
  2948. dbgname:=make_mangledname('DEBUGEND',current_module.localsymtable,'');
  2949. if (target_info.system in systems_darwin) then
  2950. dbgname:='L'+dbgname;
  2951. append_labelentry(DW_AT_high_pc,current_asmdata.DefineAsmSymbol(dbgname,bind,AT_METADATA,voidpointertype));
  2952. end;
  2953. finish_entry;
  2954. { write all global/local variables. This will flag all required tdefs }
  2955. if assigned(current_module.globalsymtable) then
  2956. write_symtable_syms(current_asmdata.asmlists[al_dwarf_info],current_module.globalsymtable);
  2957. if assigned(current_module.localsymtable) then
  2958. write_symtable_syms(current_asmdata.asmlists[al_dwarf_info],current_module.localsymtable);
  2959. { write all procedures and methods. This will flag all required tdefs }
  2960. if assigned(current_module.globalsymtable) then
  2961. write_symtable_procdefs(current_asmdata.asmlists[al_dwarf_info],current_module.globalsymtable);
  2962. if assigned(current_module.localsymtable) then
  2963. write_symtable_procdefs(current_asmdata.asmlists[al_dwarf_info],current_module.localsymtable);
  2964. { reset unit type info flag }
  2965. reset_unit_type_info;
  2966. { write used types from the used units }
  2967. write_used_unit_type_info(current_asmdata.asmlists[al_dwarf_info],current_module);
  2968. { last write the types from this unit }
  2969. if assigned(current_module.globalsymtable) then
  2970. write_symtable_defs(current_asmdata.asmlists[al_dwarf_info],current_module.globalsymtable);
  2971. if assigned(current_module.localsymtable) then
  2972. write_symtable_defs(current_asmdata.asmlists[al_dwarf_info],current_module.localsymtable);
  2973. { write defs not written yet }
  2974. write_remaining_defs_to_write(current_asmdata.asmlists[al_dwarf_info]);
  2975. { close compilation unit entry }
  2976. finish_children;
  2977. { end of debug info table }
  2978. current_asmdata.asmlists[al_dwarf_info].concat(tai_symbol.createname(target_asm.labelprefix+'edebug_info0',AT_METADATA,0,voidpointertype));
  2979. { end of abbrev table }
  2980. current_asmdata.asmlists[al_dwarf_abbrev].concat(tai_const.create_8bit(0));
  2981. if not(target_info.system in systems_darwin) then
  2982. begin
  2983. { end of aranges table }
  2984. {$ifdef i8086}
  2985. { 32-bit offset }
  2986. current_asmdata.asmlists[al_dwarf_aranges].concat(tai_const.Create_32bit_unaligned(0));
  2987. { 16-bit segment }
  2988. current_asmdata.asmlists[al_dwarf_aranges].concat(tai_const.Create_16bit_unaligned(0));
  2989. { 32-bit length }
  2990. current_asmdata.asmlists[al_dwarf_aranges].concat(tai_const.Create_32bit_unaligned(0));
  2991. {$else i8086}
  2992. { offset }
  2993. current_asmdata.asmlists[al_dwarf_aranges].concat(tai_const.Create_aint(0));
  2994. { length }
  2995. current_asmdata.asmlists[al_dwarf_aranges].concat(tai_const.Create_aint(0));
  2996. {$endif i8086}
  2997. current_asmdata.asmlists[al_dwarf_aranges].concat(tai_symbol.createname(target_asm.labelprefix+'earanges0',AT_METADATA,0,voidpointertype));
  2998. end;
  2999. { reset all def debug states }
  3000. for i:=0 to defnumberlist.count-1 do
  3001. begin
  3002. def := tdef(defnumberlist[i]);
  3003. if assigned(def) then
  3004. def.dbg_state:=dbg_state_unused;
  3005. end;
  3006. dwarflabels.free;
  3007. dwarflabels:=nil;
  3008. defnumberlist.free;
  3009. defnumberlist:=nil;
  3010. deftowritelist.free;
  3011. deftowritelist:=nil;
  3012. current_filepos:=storefilepos;
  3013. end;
  3014. procedure TDebugInfoDwarf.referencesections(list:TAsmList);
  3015. var
  3016. hp : tmodule;
  3017. begin
  3018. { Reference all DEBUGINFO sections from the main .fpc section }
  3019. { to prevent eliminating them by smartlinking }
  3020. if (target_info.system in ([system_powerpc_macosclassic]+systems_darwin+systems_wasm)) then
  3021. exit;
  3022. new_section(list,sec_fpc,'links',0);
  3023. { include reference to all debuginfo sections of used units }
  3024. hp:=tmodule(loaded_units.first);
  3025. while assigned(hp) do
  3026. begin
  3027. If (mf_has_dwarf_debuginfo in hp.moduleflags) and not assigned(hp.package) then
  3028. begin
  3029. list.concat(Tai_const.Createname(make_mangledname('DEBUGSTART',hp.localsymtable,''),0));
  3030. list.concat(Tai_const.Createname(make_mangledname('DEBUGEND',hp.localsymtable,''),0));
  3031. end;
  3032. hp:=tmodule(hp.next);
  3033. end;
  3034. end;
  3035. function TDebugInfoDwarf.symname(sym: tsym; manglename: boolean): String;
  3036. begin
  3037. if (sym.typ=paravarsym) and
  3038. (vo_is_self in tparavarsym(sym).varoptions) then
  3039. { We use 'this' for regular methods because that's what gdb triggers
  3040. on to automatically search fields. Don't do this for class methods,
  3041. because search class fields is not supported, and gdb 7.0+ fails
  3042. in this case because "this" is not a record in that case (it's a
  3043. pointer to a vmt) }
  3044. if not is_objc_class_or_protocol(tdef(sym.owner.defowner.owner.defowner)) and
  3045. not(po_classmethod in tabstractprocdef(sym.owner.defowner).procoptions) then
  3046. result:='this'
  3047. else
  3048. result:='self'
  3049. else if (sym.typ=typesym) and
  3050. is_objc_class_or_protocol(ttypesym(sym).typedef) then
  3051. result:=tobjectdef(ttypesym(sym).typedef).objextname^
  3052. else if (ds_dwarf_method_class_prefix in current_settings.debugswitches) and
  3053. (sym.typ=procsym) and
  3054. (tprocsym(sym).owner.symtabletype in [objectsymtable,recordsymtable]) then
  3055. begin
  3056. result:=tprocsym(sym).owner.name^+'__';
  3057. if manglename then
  3058. result := result + sym.name
  3059. else
  3060. result := result + symdebugname(sym);
  3061. end
  3062. else
  3063. begin
  3064. if manglename then
  3065. result := sym.name
  3066. else
  3067. result := symdebugname(sym);
  3068. end;
  3069. end;
  3070. procedure TDebugInfoDwarf.append_visibility(vis: tvisibility);
  3071. begin
  3072. case vis of
  3073. vis_hidden,
  3074. vis_private,
  3075. vis_strictprivate:
  3076. append_attribute(DW_AT_accessibility,DW_FORM_data1,[ord(DW_ACCESS_private)]);
  3077. vis_protected,
  3078. vis_strictprotected:
  3079. append_attribute(DW_AT_accessibility,DW_FORM_data1,[ord(DW_ACCESS_protected)]);
  3080. vis_published,
  3081. vis_public:
  3082. { default };
  3083. vis_none:
  3084. internalerror(2019050720);
  3085. end;
  3086. end;
  3087. procedure TDebugInfoDwarf.insertlineinfo(list:TAsmList);
  3088. var
  3089. currfileinfo,
  3090. lastfileinfo : tfileposinfo;
  3091. currfuncname : pshortstring;
  3092. currstatement: boolean;
  3093. currsectype : TAsmSectiontype;
  3094. hp, hpend : tai;
  3095. infile : tinputfile;
  3096. prevcolumn,
  3097. diffline,
  3098. prevline,
  3099. prevfileidx,
  3100. currfileidx,
  3101. nolineinfolevel : Integer;
  3102. prevlabel,
  3103. currlabel : tasmlabel;
  3104. begin
  3105. {$ifdef OMFOBJSUPPORT}
  3106. if ds_dwarf_omf_linnum in current_settings.debugswitches then
  3107. dbgcodeview.InsertLineInfo_OMF_LINNUM_MsLink(list);
  3108. {$endif OMFOBJSUPPORT}
  3109. { this function will always terminate the lineinfo block }
  3110. generated_lineinfo := true;
  3111. { if this unit only contains code without debug info (implicit init
  3112. or final etc), make sure the file table contains at least one entry
  3113. (the main source of the unit), because normally this table gets
  3114. populated via calls to get_file_index and that won't happen in this
  3115. case }
  3116. get_file_index(current_module.sourcefiles.get_file(1));
  3117. FillChar(lastfileinfo,sizeof(lastfileinfo),0);
  3118. currfuncname:=nil;
  3119. currsectype:=sec_code;
  3120. hp:=Tai(list.first);
  3121. currstatement:=true;
  3122. prevcolumn := 0;
  3123. prevline := 1;
  3124. prevfileidx := 1;
  3125. prevlabel := nil;
  3126. nolineinfolevel := 0;
  3127. hpend := nil;
  3128. while assigned(hp) do
  3129. begin
  3130. case hp.typ of
  3131. ait_section :
  3132. currsectype:=tai_section(hp).sectype;
  3133. ait_function_name :
  3134. begin
  3135. currfuncname:=tai_function_name(hp).funcname;
  3136. asmline.concat(tai_comment.Create(strpnew('function: '+currfuncname^)));
  3137. end;
  3138. ait_force_line :
  3139. begin
  3140. lastfileinfo.line:=-1;
  3141. end;
  3142. ait_marker :
  3143. begin
  3144. case tai_marker(hp).kind of
  3145. mark_NoLineInfoStart:
  3146. inc(nolineinfolevel);
  3147. mark_NoLineInfoEnd:
  3148. dec(nolineinfolevel);
  3149. else
  3150. ;
  3151. end;
  3152. end;
  3153. else
  3154. ;
  3155. end;
  3156. if (currsectype=sec_code) and
  3157. (hp.typ=ait_instruction) then
  3158. begin
  3159. currfileinfo:=tailineinfo(hp).fileinfo;
  3160. { file changed ? (must be before line info) }
  3161. if (currfileinfo.fileindex<>0) and
  3162. ((lastfileinfo.fileindex<>currfileinfo.fileindex) or
  3163. (lastfileinfo.moduleindex<>currfileinfo.moduleindex)) then
  3164. begin
  3165. infile:=get_module(currfileinfo.moduleindex).sourcefiles.get_file(currfileinfo.fileindex);
  3166. if assigned(infile) then
  3167. begin
  3168. currfileidx := get_file_index(infile);
  3169. if prevfileidx <> currfileidx then
  3170. begin
  3171. list.insertbefore(tai_comment.Create(strpnew('path: '+infile.path)), hp);
  3172. list.insertbefore(tai_comment.Create(strpnew('file: '+infile.name)), hp);
  3173. list.insertbefore(tai_comment.Create(strpnew('indx: '+tostr(currfileidx))), hp);
  3174. { set file }
  3175. asmline.concat(tai_comment.Create(strpnew('path: '+infile.path)));
  3176. asmline.concat(tai_comment.Create(strpnew('file: '+infile.name)));
  3177. asmline.concat(tai_const.create_8bit(DW_LNS_set_file));
  3178. asmline.concat(tai_const.create_uleb128bit(currfileidx));
  3179. prevfileidx := currfileidx;
  3180. end;
  3181. { force new line info }
  3182. lastfileinfo.line:=-1;
  3183. end;
  3184. end;
  3185. { Set the line-nr to 0 if the code does not corresponds to a particular line }
  3186. if nolineinfolevel>0 then
  3187. currfileinfo.line := 0;
  3188. { line changed ? }
  3189. if (lastfileinfo.line<>currfileinfo.line) and ((currfileinfo.line<>0) or (nolineinfolevel>0)) then
  3190. begin
  3191. { set address }
  3192. current_asmdata.getlabel(currlabel, alt_dbgline);
  3193. list.insertbefore(tai_label.create(currlabel), hp);
  3194. asmline.concat(tai_comment.Create(strpnew('['+tostr(currfileinfo.line)+':'+tostr(currfileinfo.column)+']')));
  3195. if (prevlabel = nil) or
  3196. { darwin's assembler cannot create an uleb128 of the difference
  3197. between to symbols
  3198. same goes for Solaris native assembler
  3199. ... and riscv }
  3200. (target_info.system in systems_darwin+[system_riscv32_linux,system_riscv64_linux,
  3201. system_riscv32_embedded,system_riscv64_embedded,
  3202. system_riscv32_freertos]) or
  3203. (target_asm.id=as_solaris_as) then
  3204. begin
  3205. asmline.concat(tai_const.create_8bit(DW_LNS_extended_op));
  3206. asmline.concat(tai_const.create_uleb128bit(1+sizeof(pint)));
  3207. asmline.concat(tai_const.create_8bit(DW_LNE_set_address));
  3208. asmline.concat(tai_const.create_type_sym(aitconst_ptr_unaligned,currlabel));
  3209. {$ifdef i8086}
  3210. { on i8086 we also emit an Open Watcom-specific 'set segment' op }
  3211. asmline.concat(tai_const.create_8bit(DW_LNS_extended_op));
  3212. asmline.concat(tai_const.create_uleb128bit(3));
  3213. asmline.concat(tai_const.create_8bit(DW_LNE_set_segment));
  3214. asmline.concat(tai_const.Create_seg_name(currlabel.Name));
  3215. {$endif i8086}
  3216. end
  3217. else
  3218. begin
  3219. asmline.concat(tai_const.create_8bit(DW_LNS_advance_pc));
  3220. asmline.concat(tai_const.create_rel_sym(aitconst_uleb128bit, prevlabel, currlabel));
  3221. end;
  3222. prevlabel := currlabel;
  3223. { set column }
  3224. if prevcolumn <> currfileinfo.column then
  3225. begin
  3226. asmline.concat(tai_const.create_8bit(DW_LNS_set_column));
  3227. asmline.concat(tai_const.create_uleb128bit(currfileinfo.column));
  3228. prevcolumn := currfileinfo.column;
  3229. end;
  3230. { set statement }
  3231. if (currfileinfo.line=0) and currstatement then
  3232. begin
  3233. currstatement := false;
  3234. asmline.concat(tai_const.create_8bit(DW_LNS_negate_stmt));
  3235. end;
  3236. if not currstatement and (currfileinfo.line>0) then
  3237. begin
  3238. currstatement := true;
  3239. asmline.concat(tai_const.create_8bit(DW_LNS_negate_stmt));
  3240. end;
  3241. { set line }
  3242. diffline := currfileinfo.line - prevline;
  3243. if (diffline >= LINE_BASE) and (OPCODE_BASE + diffline - LINE_BASE <= 255) then
  3244. begin
  3245. { use special opcode, this also adds a row }
  3246. asmline.concat(tai_const.create_8bit(OPCODE_BASE + diffline - LINE_BASE));
  3247. end
  3248. else
  3249. begin
  3250. if diffline <> 0 then
  3251. begin
  3252. asmline.concat(tai_const.create_8bit(DW_LNS_advance_line));
  3253. asmline.concat(tai_const.create_sleb128bit(diffline));
  3254. end;
  3255. { no row added yet, do it manually }
  3256. asmline.concat(tai_const.create_8bit(DW_LNS_copy));
  3257. end;
  3258. prevline := currfileinfo.line;
  3259. end;
  3260. lastfileinfo:=currfileinfo;
  3261. end;
  3262. hpend:=hp;
  3263. hp:=tai(hp.next);
  3264. end;
  3265. if assigned(hpend) then
  3266. begin
  3267. { set address for end (see appendix 3 of dwarf 2 specs) }
  3268. current_asmdata.getlabel(currlabel, alt_dbgline);
  3269. list.insertafter(tai_label.create(currlabel), hpend);
  3270. asmline.concat(tai_const.create_8bit(DW_LNS_extended_op));
  3271. asmline.concat(tai_const.create_uleb128bit(1+sizeof(pint)));
  3272. asmline.concat(tai_const.create_8bit(DW_LNE_set_address));
  3273. asmline.concat(tai_const.create_type_sym(aitconst_ptr_unaligned,currlabel));
  3274. end;
  3275. { end sequence }
  3276. asmline.concat(tai_const.Create_8bit(DW_LNS_extended_op));
  3277. asmline.concat(tai_const.Create_8bit(1));
  3278. asmline.concat(tai_const.Create_8bit(DW_LNE_end_sequence));
  3279. asmline.concat(tai_comment.Create(strpnew('###################')));
  3280. end;
  3281. procedure TDebugInfoDwarf.finish_lineinfo;
  3282. var
  3283. infile: tinputfile;
  3284. begin
  3285. { only needed if no line info at all has been generated }
  3286. if generated_lineinfo then
  3287. begin
  3288. { reset for next module compilation }
  3289. generated_lineinfo:=false;
  3290. exit;
  3291. end;
  3292. { at least the Darwin linker is annoyed if you do not }
  3293. { finish the lineinfo section, or if it doesn't }
  3294. { contain at least one file name and set_address }
  3295. infile:=current_module.sourcefiles.get_file(1);
  3296. if not assigned(infile) then
  3297. internalerror(2006020211);
  3298. asmline.concat(tai_const.create_8bit(DW_LNS_set_file));
  3299. asmline.concat(tai_const.create_uleb128bit(get_file_index(infile)));
  3300. asmline.concat(tai_const.create_8bit(DW_LNS_extended_op));
  3301. asmline.concat(tai_const.create_uleb128bit(1+sizeof(pint)));
  3302. asmline.concat(tai_const.create_8bit(DW_LNE_set_address));
  3303. asmline.concat(tai_const.create_type_sym(aitconst_ptr_unaligned,nil));
  3304. asmline.concat(tai_const.create_8bit(DW_LNS_extended_op));
  3305. asmline.concat(tai_const.Create_8bit(1));
  3306. asmline.concat(tai_const.Create_8bit(DW_LNE_end_sequence));
  3307. asmline.concat(tai_comment.Create(strpnew('###################')));
  3308. end;
  3309. {****************************************************************************
  3310. TDebugInfoDwarf2
  3311. ****************************************************************************}
  3312. procedure TDebugInfoDwarf2.appenddef_file(list:TAsmList;def: tfiledef);
  3313. begin
  3314. { gdb 6.4 doesn't support files so far so we use some fake recorddef
  3315. file recs. are less than 1k so using data2 is enough }
  3316. if assigned(def.typesym) then
  3317. append_entry(DW_TAG_structure_type,false,[
  3318. DW_AT_name,DW_FORM_string,symname(def.typesym, false)+#0,
  3319. DW_AT_byte_size,DW_FORM_udata,def.size
  3320. ])
  3321. else
  3322. append_entry(DW_TAG_structure_type,false,[
  3323. DW_AT_byte_size,DW_FORM_udata,def.size
  3324. ]);
  3325. finish_entry;
  3326. end;
  3327. procedure TDebugInfoDwarf2.appenddef_formal(list:TAsmList;def: tformaldef);
  3328. begin
  3329. { gdb 6.4 doesn't support DW_TAG_unspecified_type so we
  3330. replace it with a unsigned type with size 0 (FK)
  3331. }
  3332. append_entry(DW_TAG_base_type,false,[
  3333. DW_AT_name,DW_FORM_string,'FormalDef'#0,
  3334. DW_AT_encoding,DW_FORM_data1,DW_ATE_unsigned,
  3335. DW_AT_byte_size,DW_FORM_data1,0
  3336. ]);
  3337. finish_entry;
  3338. end;
  3339. procedure TDebugInfoDwarf2.append_object_struct(def: tobjectdef; const createlabel: boolean; const objectname: PShortString);
  3340. begin
  3341. if createlabel then
  3342. begin
  3343. if not(tf_dwarf_only_local_labels in target_info.flags) then
  3344. current_asmdata.asmlists[al_dwarf_info].concat(tai_symbol.create_global(def_dwarf_class_struct_lab(def),0))
  3345. else
  3346. current_asmdata.asmlists[al_dwarf_info].concat(tai_symbol.create(def_dwarf_class_struct_lab(def),0));
  3347. end;
  3348. if assigned(objectname) then
  3349. append_entry(DW_TAG_class_type,true,[
  3350. DW_AT_name,DW_FORM_string,objectname^+#0,
  3351. DW_AT_byte_size,DW_FORM_udata,tobjectsymtable(def.symtable).datasize
  3352. ])
  3353. else
  3354. append_entry(DW_TAG_class_type,true,[
  3355. DW_AT_byte_size,DW_FORM_udata,tobjectsymtable(def.symtable).datasize
  3356. ]);
  3357. { Apple-specific tag that identifies it as an Objective-C class }
  3358. if (def.objecttype=odt_objcclass) then
  3359. append_attribute(DW_AT_APPLE_runtime_class,DW_FORM_data1,[DW_LANG_ObjC]);
  3360. finish_entry;
  3361. if assigned(def.childof) then
  3362. begin
  3363. append_entry(DW_TAG_inheritance,false,[
  3364. DW_AT_accessibility,DW_FORM_data1,DW_ACCESS_public,
  3365. DW_AT_data_member_location,DW_FORM_block1,1+lengthuleb128(0)
  3366. ]);
  3367. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_plus_uconst)));
  3368. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_uleb128bit(0));
  3369. if (def.childof.dbg_state=dbg_state_unused) then
  3370. def.childof.dbg_state:=dbg_state_used;
  3371. if is_implicit_pointer_object_type(def) then
  3372. append_labelentry_ref(DW_AT_type,def_dwarf_class_struct_lab(def.childof))
  3373. else
  3374. append_labelentry_ref(DW_AT_type,def_dwarf_lab(def.childof));
  3375. finish_entry;
  3376. end;
  3377. if (oo_has_vmt in def.objectoptions) and
  3378. (not assigned(def.childof) or
  3379. not(oo_has_vmt in def.childof.objectoptions)) then
  3380. begin
  3381. { vmt field }
  3382. append_entry(DW_TAG_member,false,[
  3383. DW_AT_artificial,DW_FORM_flag,true,
  3384. DW_AT_name,DW_FORM_string,'_vptr$'+def.objname^+#0,
  3385. DW_AT_data_member_location,DW_FORM_block1,1+lengthuleb128(def.vmt_offset)
  3386. ]);
  3387. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_plus_uconst)));
  3388. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_uleb128bit(def.vmt_offset));
  3389. { should be changed into a pointer to a function returning an }
  3390. { int and with TAG_unspecified_parameters }
  3391. if (voidpointertype.dbg_state=dbg_state_unused) then
  3392. voidpointertype.dbg_state:=dbg_state_used;
  3393. append_labelentry_ref(DW_AT_type,def_dwarf_lab(voidpointertype));
  3394. finish_entry;
  3395. end;
  3396. def.symtable.symList.ForEachCall(@enum_membersyms_callback,nil);
  3397. { Write the methods in the scope of the class/object, except for Objective-C. }
  3398. if is_objc_class_or_protocol(def) then
  3399. finish_children;
  3400. { don't write procdefs of externally defined classes, gcc doesn't
  3401. either (info is probably gotten from ObjC runtime) }
  3402. if not(oo_is_external in def.objectoptions) then
  3403. write_symtable_procdefs(current_asmdata.asmlists[al_dwarf_info],def.symtable);
  3404. if not is_objc_class_or_protocol(def) then
  3405. finish_children;
  3406. end;
  3407. procedure TDebugInfoDwarf2.appenddef_object(list:TAsmList;def: tobjectdef);
  3408. begin
  3409. case def.objecttype of
  3410. odt_cppclass,
  3411. odt_object:
  3412. append_object_struct(def,false,def.objname);
  3413. odt_interfacecom,
  3414. odt_interfacecorba,
  3415. odt_dispinterface,
  3416. odt_helper,
  3417. odt_class:
  3418. begin
  3419. { implicit pointer }
  3420. append_entry(DW_TAG_pointer_type,false,[]);
  3421. append_labelentry_ref(DW_AT_type,def_dwarf_class_struct_lab(def));
  3422. finish_entry;
  3423. append_object_struct(def,true,def.objname);
  3424. end;
  3425. odt_objcclass:
  3426. { Objective-C class: same as regular class, except for
  3427. a) Apple-specific tag that identifies it as an Objective-C class
  3428. b) use extname^ instead of objname
  3429. }
  3430. append_object_struct(def,true,def.objextname);
  3431. odt_objcprotocol:
  3432. begin
  3433. append_entry(DW_TAG_pointer_type,false,[]);
  3434. append_labelentry_ref(DW_AT_type,def_dwarf_lab(voidpointertype));
  3435. finish_entry;
  3436. end;
  3437. else
  3438. internalerror(200602041);
  3439. end;
  3440. end;
  3441. procedure TDebugInfoDwarf2.appenddef_set_intern(list:TAsmList;def: tsetdef; force_tag_set: boolean);
  3442. var
  3443. lab: tasmlabel;
  3444. begin
  3445. if force_tag_set or
  3446. (ds_dwarf_sets in current_settings.debugswitches) then
  3447. begin
  3448. { current (20070704 -- patch was committed on 20060513) gdb cvs supports set types }
  3449. if assigned(def.typesym) then
  3450. append_entry(DW_TAG_set_type,false,[
  3451. DW_AT_name,DW_FORM_string,symname(def.typesym, false)+#0,
  3452. DW_AT_byte_size,DW_FORM_data2,def.size
  3453. ])
  3454. else
  3455. append_entry(DW_TAG_set_type,false,[
  3456. DW_AT_byte_size,DW_FORM_data2,def.size
  3457. ]);
  3458. if assigned(def.elementdef) then
  3459. begin
  3460. if not(tf_dwarf_only_local_labels in target_info.flags) then
  3461. current_asmdata.getglobaldatalabel(lab)
  3462. else
  3463. current_asmdata.getaddrlabel(lab);
  3464. append_labelentry_ref(DW_AT_type,lab);
  3465. finish_entry;
  3466. if lab.bind=AB_GLOBAL then
  3467. current_asmdata.asmlists[al_dwarf_info].concat(tai_symbol.create_global(lab,0))
  3468. else
  3469. current_asmdata.asmlists[al_dwarf_info].concat(tai_symbol.create(lab,0));
  3470. { Sets of e.g. [1..5] are actually stored as a set of [0..7],
  3471. so write the exact boundaries of the set here. Let's hope no
  3472. debugger ever rejects this because this "subrange" type can
  3473. actually have a larger range than the original one. }
  3474. append_entry(DW_TAG_subrange_type,false,[
  3475. DW_AT_lower_bound,DW_FORM_sdata,def.setbase,
  3476. DW_AT_upper_bound,DW_FORM_sdata,get_max_value(def.elementdef).svalue
  3477. ]);
  3478. append_labelentry_ref(DW_AT_type,def_dwarf_lab(def.elementdef))
  3479. end
  3480. end
  3481. else
  3482. begin
  3483. { gdb versions which don't support sets refuse to load the debug }
  3484. { info of modules that contain set tags }
  3485. if assigned(def.typesym) then
  3486. append_entry(DW_TAG_base_type,false,[
  3487. DW_AT_name,DW_FORM_string,symname(def.typesym, false)+#0,
  3488. DW_AT_encoding,DW_FORM_data1,DW_ATE_unsigned,
  3489. DW_AT_byte_size,DW_FORM_data2,def.size
  3490. ])
  3491. else
  3492. append_entry(DW_TAG_base_type,false,[
  3493. DW_AT_encoding,DW_FORM_data1,DW_ATE_unsigned,
  3494. DW_AT_byte_size,DW_FORM_data2,def.size
  3495. ]);
  3496. end;
  3497. finish_entry;
  3498. end;
  3499. procedure TDebugInfoDwarf2.appenddef_set(list:TAsmList;def: tsetdef);
  3500. begin
  3501. appenddef_set_intern(list,def,false);
  3502. end;
  3503. procedure TDebugInfoDwarf2.appenddef_undefined(list:TAsmList;def: tundefineddef);
  3504. begin
  3505. { gdb 6.4 doesn't support DW_TAG_unspecified_type so we
  3506. replace it with a unsigned type with size 0 (FK)
  3507. }
  3508. append_entry(DW_TAG_base_type,false,[
  3509. DW_AT_name,DW_FORM_string,'FormalDef'#0,
  3510. DW_AT_encoding,DW_FORM_data1,DW_ATE_unsigned,
  3511. DW_AT_byte_size,DW_FORM_data1,0
  3512. ]);
  3513. finish_entry;
  3514. end;
  3515. procedure TDebugInfoDwarf2.appenddef_variant(list:TAsmList;def: tvariantdef);
  3516. begin
  3517. { variants aren't known to dwarf2 but writting tvardata should be enough }
  3518. if assigned(vardatadef) then
  3519. appenddef_record_named(list,trecorddef(vardatadef),'Variant');
  3520. end;
  3521. function TDebugInfoDwarf2.dwarf_version: Word;
  3522. begin
  3523. Result:=2;
  3524. end;
  3525. {****************************************************************************
  3526. TDebugInfoDwarf3
  3527. ****************************************************************************}
  3528. procedure TDebugInfoDwarf3.append_labelentry_addr_ref(sym : tasmsymbol);
  3529. begin
  3530. AddConstToAbbrev(ord(DW_FORM_ref_addr));
  3531. { Since Dwarf 3 the length of a DW_FORM_ref_addr entry is not dependent on the pointer size of the
  3532. target platform, but on the used Dwarf-format (32 bit or 64 bit) for the current compilation section. }
  3533. if use_64bit_headers then
  3534. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.Create_type_sym(aitconst_64bit_unaligned,sym))
  3535. else
  3536. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.Create_type_sym(aitconst_32bit_unaligned,sym));
  3537. end;
  3538. procedure tdebuginfodwarf3.appenddef_array(list: tasmlist; def: tarraydef);
  3539. begin
  3540. if not is_dynamic_array(def) then
  3541. begin
  3542. inherited appenddef_array(list,def);
  3543. exit;
  3544. end;
  3545. if assigned(def.typesym) then
  3546. append_entry(DW_TAG_array_type,true,[
  3547. DW_AT_name,DW_FORM_string,symname(def.typesym, false)+#0,
  3548. DW_AT_byte_stride,DW_FORM_udata,def.elesize,
  3549. DW_AT_data_location,DW_FORM_block1,2
  3550. ])
  3551. else
  3552. append_entry(DW_TAG_array_type,true,[
  3553. DW_AT_byte_stride,DW_FORM_udata,def.elesize,
  3554. DW_AT_data_location,DW_FORM_block1,2
  3555. ]);
  3556. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_push_object_address)));
  3557. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_deref)));
  3558. append_labelentry_ref(DW_AT_type,def_dwarf_lab(def.elementdef));
  3559. finish_entry;
  3560. { to simplify things, we don't write a multidimensional array here }
  3561. append_entry(DW_TAG_subrange_type,false,[
  3562. DW_AT_lower_bound,DW_FORM_udata,0,
  3563. DW_AT_upper_bound,DW_FORM_block1,15
  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. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_dup)));
  3568. { pointer = nil? }
  3569. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_bra)));
  3570. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_16bit_unaligned(5));
  3571. { yes -> length = 0 }
  3572. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_const1s)));
  3573. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(byte(-1)));
  3574. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_skip)));
  3575. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_16bit_unaligned(3));
  3576. { no -> load length }
  3577. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_lit0)+sizesinttype.size));
  3578. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_minus)));
  3579. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_deref)));
  3580. { skip to past end is not allowed, thus use a nop here }
  3581. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_nop)));
  3582. append_labelentry_ref(DW_AT_type,def_dwarf_lab(def.rangedef));
  3583. finish_entry;
  3584. finish_children;
  3585. end;
  3586. procedure tdebuginfodwarf3.appenddef_string(list: tasmlist; def: tstringdef);
  3587. procedure addstringdef(const name: shortstring; chardef: tdef; deref: boolean; lensize: aint);
  3588. var
  3589. upperopcodes: longint;
  3590. begin
  3591. { deref=true -> ansi/unicde/widestring; deref = false -> short/longstring }
  3592. if assigned(def.typesym) then
  3593. append_entry(DW_TAG_array_type,true,[
  3594. DW_AT_name,DW_FORM_string,name+#0,
  3595. DW_AT_data_location,DW_FORM_block1,2+ord(not(deref))
  3596. ])
  3597. else
  3598. append_entry(DW_TAG_array_type,true,[
  3599. DW_AT_data_location,DW_FORM_block1,2+ord(not(deref))
  3600. ]);
  3601. { in all cases we start with the address of the string }
  3602. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_push_object_address)));
  3603. if deref then
  3604. begin
  3605. { ansi/unicode/widestring -> dereference the address of the string, and then
  3606. we point to address of the string
  3607. }
  3608. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_deref)));
  3609. end
  3610. else
  3611. begin
  3612. { shortstring characters begin at string[1], so add one to the string's address }
  3613. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_lit0)+lensize));
  3614. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_plus)))
  3615. end;
  3616. { reference to the element type of the string }
  3617. append_labelentry_ref(DW_AT_type,def_dwarf_lab(chardef));
  3618. finish_entry;
  3619. { now the information about the length of the string }
  3620. if deref then
  3621. begin
  3622. if not (is_widestring(def) and (tf_winlikewidestring in target_info.flags)) then
  3623. upperopcodes:=14
  3624. else
  3625. upperopcodes:=17;
  3626. { lower bound is always 1, upper bound (length) needs to be calculated }
  3627. append_entry(DW_TAG_subrange_type,false,[
  3628. DW_AT_lower_bound,DW_FORM_udata,1,
  3629. DW_AT_upper_bound,DW_FORM_block1,upperopcodes
  3630. ]);
  3631. { high(string) is stored sizeof(sizeint) bytes before the string data }
  3632. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_push_object_address)));
  3633. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_deref)));
  3634. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_dup)));
  3635. { pointer = nil? }
  3636. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_bra)));
  3637. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_16bit_unaligned(4));
  3638. { yes -> length = 0 }
  3639. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_lit0)));
  3640. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_skip)));
  3641. if upperopcodes=17 then
  3642. { skip the extra deref_size argument and the division by two of the length }
  3643. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_16bit_unaligned(6))
  3644. else
  3645. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_16bit_unaligned(3));
  3646. { no -> load length }
  3647. if upperopcodes=17 then
  3648. { for Windows WideString the size is always a DWORD }
  3649. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_lit4)))
  3650. else
  3651. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_lit0)+sizesinttype.size));
  3652. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_minus)));
  3653. if upperopcodes=17 then
  3654. begin
  3655. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_deref_size)));
  3656. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(4));
  3657. end
  3658. else
  3659. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_deref)));
  3660. { for widestrings, the length is specified in bytes, so divide by two }
  3661. if (upperopcodes=17) then
  3662. begin
  3663. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_lit1)));
  3664. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_shr)));
  3665. end;
  3666. { skip to past end is not allowed, thus use a nop here }
  3667. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_nop)));
  3668. end
  3669. else
  3670. begin
  3671. append_entry(DW_TAG_subrange_type,false,[
  3672. DW_AT_lower_bound,DW_FORM_udata,1,
  3673. DW_AT_upper_bound,DW_FORM_block1,3
  3674. ]);
  3675. { for shortstrings, the length is the first byte of the string }
  3676. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_push_object_address)));
  3677. { load 1 byte }
  3678. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_deref_size)));
  3679. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(lensize));
  3680. end;
  3681. finish_entry;
  3682. finish_children;
  3683. end;
  3684. begin
  3685. if (ds_dwarf_cpp in current_settings.debugswitches) then
  3686. begin
  3687. // At least LLDB 6.0.0 does not like this implementation of string types.
  3688. // Call the inherited DWARF 2 implementation, which works fine.
  3689. inherited;
  3690. exit;
  3691. end;
  3692. case def.stringtype of
  3693. st_shortstring:
  3694. begin
  3695. addstringdef('ShortString',cansichartype,false,1);
  3696. end;
  3697. st_longstring:
  3698. begin
  3699. {$ifdef cpu64bitaddr}
  3700. addstringdef('LongString',cansichartype,false,8);
  3701. {$else cpu64bitaddr}
  3702. addstringdef('LongString',cansichartype,false,4);
  3703. {$endif cpu64bitaddr}
  3704. end;
  3705. st_ansistring:
  3706. begin
  3707. addstringdef('AnsiString',cansichartype,true,-1);
  3708. end;
  3709. st_unicodestring:
  3710. begin
  3711. addstringdef('UnicodeString',cwidechartype,true,-1);
  3712. end;
  3713. st_widestring:
  3714. begin
  3715. addstringdef('WideString',cwidechartype,true,-1)
  3716. end;
  3717. end;
  3718. end;
  3719. procedure TDebugInfoDwarf3.appenddef_file(list:TAsmList;def: tfiledef);
  3720. begin
  3721. if assigned(def.typesym) then
  3722. append_entry(DW_TAG_file_type,false,[
  3723. DW_AT_name,DW_FORM_string,symname(def.typesym, false)+#0,
  3724. DW_AT_byte_size,DW_FORM_data2,def.size
  3725. ])
  3726. else
  3727. append_entry(DW_TAG_file_type,false,[
  3728. DW_AT_byte_size,DW_FORM_data2,def.size
  3729. ]);
  3730. if tfiledef(def).filetyp=ft_typed then
  3731. append_labelentry_ref(DW_AT_type,def_dwarf_lab(tfiledef(def).typedfiledef));
  3732. finish_entry;
  3733. end;
  3734. procedure TDebugInfoDwarf3.appenddef_formal(list:TAsmList;def: tformaldef);
  3735. begin
  3736. if (ds_dwarf_cpp in current_settings.debugswitches) then
  3737. begin
  3738. // Do not use DW_TAG_unspecified_type for C++ simulation.
  3739. // At least LLDB 3.9.0 crashes in such case.
  3740. // Call the inherited DWARF 2 implementation, which works fine.
  3741. inherited;
  3742. exit;
  3743. end;
  3744. append_entry(DW_TAG_unspecified_type,false,[]);
  3745. finish_entry;
  3746. end;
  3747. procedure TDebugInfoDwarf3.appenddef_object(list:TAsmList;def: tobjectdef);
  3748. procedure dostruct(tag: tdwarf_tag);
  3749. begin
  3750. if assigned(def.objname) then
  3751. append_entry(tag,true,[
  3752. DW_AT_name,DW_FORM_string,def.objrealname^+#0
  3753. ])
  3754. else
  3755. append_entry(DW_TAG_structure_type,true,[]);
  3756. append_attribute(DW_AT_byte_size,DW_FORM_udata,[tobjectsymtable(def.symtable).datasize]);
  3757. { 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,
  3758. see issue #36017 }
  3759. if not(is_object(def) or is_cppclass(def)) then
  3760. begin
  3761. { The pointer to the class-structure is hidden. The debug-information
  3762. does not contain an implicit pointer, but the data-adress is dereferenced here.
  3763. In case of a nil-pointer, report the class as being unallocated.
  3764. }
  3765. append_block1(DW_AT_allocated,2);
  3766. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_push_object_address)));
  3767. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_deref)));
  3768. append_block1(DW_AT_data_location,2);
  3769. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_push_object_address)));
  3770. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_deref)));
  3771. end;
  3772. finish_entry;
  3773. end;
  3774. procedure doimplicitpointer;
  3775. var
  3776. obj : tasmlabel;
  3777. begin
  3778. if not(tf_dwarf_only_local_labels in target_info.flags) then
  3779. current_asmdata.getglobaldatalabel(obj)
  3780. else
  3781. current_asmdata.getaddrlabel(obj);
  3782. { implicit pointer }
  3783. append_entry(DW_TAG_pointer_type,false,[]);
  3784. append_labelentry_ref(DW_AT_type,obj);
  3785. finish_entry;
  3786. current_asmdata.asmlists[al_dwarf_info].concat(tai_symbol.create(obj,0));
  3787. end;
  3788. procedure doparent(isinterface: boolean);
  3789. begin
  3790. if not assigned(def.childof) then
  3791. exit;
  3792. if isinterface then
  3793. begin
  3794. append_entry(DW_TAG_inheritance,false,[]);
  3795. end
  3796. else
  3797. begin
  3798. append_entry(DW_TAG_inheritance,false,[
  3799. DW_AT_accessibility,DW_FORM_data1,DW_ACCESS_public,
  3800. DW_AT_data_member_location,DW_FORM_block1,1+lengthuleb128(0)
  3801. ]);
  3802. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_plus_uconst)));
  3803. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_uleb128bit(0));
  3804. end;
  3805. append_labelentry_ref(DW_AT_type,def_dwarf_lab(def.childof));
  3806. finish_entry;
  3807. end;
  3808. var
  3809. n: integer;
  3810. begin
  3811. case def.objecttype of
  3812. odt_objcclass,
  3813. odt_objcprotocol:
  3814. begin
  3815. inherited;
  3816. exit
  3817. end;
  3818. odt_cppclass,
  3819. odt_object:
  3820. begin
  3821. append_object_struct(def,false,def.objname);
  3822. exit;
  3823. end;
  3824. odt_interfacecom,
  3825. odt_interfacecorba,
  3826. odt_dispinterface:
  3827. begin
  3828. dostruct(DW_TAG_interface_type);
  3829. doparent(true);
  3830. end;
  3831. odt_helper,
  3832. odt_class:
  3833. begin
  3834. append_entry(DW_TAG_pointer_type,false,[]);
  3835. append_labelentry_ref(DW_AT_type,def_dwarf_class_struct_lab(def));
  3836. finish_entry;
  3837. append_object_struct(def,true,def.objrealname);
  3838. Exit;
  3839. end;
  3840. else
  3841. internalerror(200609171);
  3842. end;
  3843. { add implemented interfaces }
  3844. if assigned(def.ImplementedInterfaces) then
  3845. for n := 0 to def.ImplementedInterfaces.count-1 do
  3846. begin
  3847. append_entry(DW_TAG_inheritance,false,[]);
  3848. append_labelentry_ref(DW_AT_type,def_dwarf_lab(TImplementedInterface(def.ImplementedInterfaces[n]).IntfDef));
  3849. finish_entry;
  3850. end;
  3851. { add members }
  3852. def.symtable.symList.ForEachCall(@enum_membersyms_callback,nil);
  3853. finish_children;
  3854. end;
  3855. procedure TDebugInfoDwarf3.appenddef_set(list:TAsmList;def: tsetdef);
  3856. begin
  3857. appenddef_set_intern(list,def,true);
  3858. end;
  3859. procedure TDebugInfoDwarf3.appenddef_undefined(list:TAsmList;def: tundefineddef);
  3860. begin
  3861. { ??? can a undefined def have a typename ? }
  3862. if assigned(def.typesym) then
  3863. append_entry(DW_TAG_unspecified_type,false,[
  3864. DW_AT_name,DW_FORM_string,symname(def.typesym, false)+#0
  3865. ])
  3866. else
  3867. append_entry(DW_TAG_unspecified_type,false,[
  3868. ]);
  3869. finish_entry;
  3870. end;
  3871. procedure TDebugInfoDwarf3.appenddef_variant(list:TAsmList;def: tvariantdef);
  3872. const
  3873. VARIANTS: array[1..27] of record
  3874. Value: Word;
  3875. Name: String;
  3876. { some fields are only supported by some features }
  3877. features : tfeatures
  3878. end = (
  3879. (value:0; name:'';features: []),
  3880. (value:1; name:'';features: []),
  3881. (value:2; name:'VSMALLINT';features: []),
  3882. (value:3; name:'VINTEGER';features: []),
  3883. (value:4; name:'VSINGLE';features: [f_softfpu]),
  3884. (value:5; name:'VDOUBLE';features: [f_softfpu]),
  3885. (value:6; name:'VCURRENCY';features: [f_softfpu]),
  3886. (value:7; name:'VDATE';features: [f_softfpu]),
  3887. (value:8; name:'VOLESTR';features: []),
  3888. (value:9; name:'VDISPATCH';features: []),
  3889. (value:10; name:'VERROR';features: []),
  3890. (value:11; name:'VBOOLEAN';features: []),
  3891. (value:12; name:'';features: []),
  3892. (value:13; name:'VUNKNOWN';features: []),
  3893. (value:14; name:'';features: []),
  3894. (value:16; name:'VSHORTINT';features: []),
  3895. (value:17; name:'VBYTE';features: []),
  3896. (value:18; name:'VWORD';features: []),
  3897. (value:19; name:'VLONGWORD';features: []),
  3898. (value:20; name:'VINT64';features: []),
  3899. (value:21; name:'VQWORD';features: []),
  3900. (value:36; name:'VRECORD';features: []),
  3901. (value:$48; name:'';features: []),
  3902. (value:$100; name:'VSTRING';features: []),
  3903. (value:$101; name:'VANY';features: []),
  3904. (value:$2000; name:'VARRAY';features: []),
  3905. (value:$4000; name:'VPOINTER';features: [])
  3906. );
  3907. var
  3908. fs: tfieldvarsym;
  3909. lbl: tasmlabel;
  3910. idx: integer;
  3911. begin
  3912. { it could be done with DW_TAG_variant for the union part (if that info was available)
  3913. now we do it manually for variants (MWE) }
  3914. { struct }
  3915. append_entry(DW_TAG_structure_type,true,[
  3916. DW_AT_name,DW_FORM_string,'Variant'#0,
  3917. DW_AT_byte_size,DW_FORM_udata,vardatadef.size
  3918. ]);
  3919. finish_entry;
  3920. append_entry(DW_TAG_variant_part,true,[
  3921. ]);
  3922. current_asmdata.getaddrlabel(lbl);
  3923. append_labelentry_ref(DW_AT_discr,lbl);
  3924. finish_entry;
  3925. { discriminant }
  3926. fs := tfieldvarsym(vardatadef.symtable.Find('VTYPE'));
  3927. if (fs = nil) or (fs.typ <> fieldvarsym) then
  3928. internalerror(200609271);
  3929. current_asmdata.asmlists[al_dwarf_info].concat(tai_symbol.create(lbl,0));
  3930. appendsym_fieldvar(list,fs);
  3931. { variants }
  3932. for idx := Low(VARIANTS) to High(VARIANTS) do
  3933. begin
  3934. if (features*VARIANTS[idx].features)=VARIANTS[idx].features then
  3935. begin
  3936. append_entry(DW_TAG_variant,true,[
  3937. DW_AT_discr_value,DW_FORM_udata,VARIANTS[idx].value
  3938. ]);
  3939. finish_entry;
  3940. if VARIANTS[idx].name <> '' then
  3941. begin
  3942. fs := tfieldvarsym(vardatadef.symtable.Find(VARIANTS[idx].name));
  3943. if (fs = nil) or (fs.typ <> fieldvarsym) then
  3944. internalerror(2006092702+idx);
  3945. appendsym_fieldvar(list,fs);
  3946. end;
  3947. finish_children; { variant }
  3948. end;
  3949. end;
  3950. finish_children; { variant part }
  3951. finish_children; { struct }
  3952. end;
  3953. function TDebugInfoDwarf3.dwarf_version: Word;
  3954. begin
  3955. Result:=3;
  3956. end;
  3957. function TDebugInfoDwarf3.symdebugname(sym: tsym): String;
  3958. begin
  3959. Result:=sym.realname;
  3960. end;
  3961. { TDebugInfoDwarf4 }
  3962. function TDebugInfoDwarf4.dwarf_version: Word;
  3963. begin
  3964. Result:=4;
  3965. end;
  3966. {****************************************************************************
  3967. ****************************************************************************}
  3968. const
  3969. dbg_dwarf2_info : tdbginfo =
  3970. (
  3971. id : dbg_dwarf2;
  3972. idtxt : 'DWARF2';
  3973. );
  3974. dbg_dwarf3_info : tdbginfo =
  3975. (
  3976. id : dbg_dwarf3;
  3977. idtxt : 'DWARF3';
  3978. );
  3979. dbg_dwarf4_info : tdbginfo =
  3980. (
  3981. id : dbg_dwarf4;
  3982. idtxt : 'DWARF4';
  3983. );
  3984. initialization
  3985. RegisterDebugInfo(dbg_dwarf2_info,TDebugInfoDwarf2);
  3986. RegisterDebugInfo(dbg_dwarf3_info,TDebugInfoDwarf3);
  3987. RegisterDebugInfo(dbg_dwarf4_info,TDebugInfoDwarf4);
  3988. end.