dbgdwarf.pas 182 KB

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