dbgdwarf.pas 176 KB

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