dbgdwarf.pas 157 KB

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