dbgdwarf.pas 171 KB

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