dbgdwarf.pas 143 KB

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