dbgdwarf.pas 181 KB

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