dbgdwarf.pas 189 KB

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