dbgdwarf.pas 159 KB

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