dbgdwarf.pas 153 KB

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