dbgdwarf.pas 180 KB

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