dbgdwarf.pas 150 KB

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