dbgdwarf.pas 118 KB

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