2
0

dbgdwarf.pas 190 KB

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