dbgdwarf.pas 192 KB

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