dbgdwarf.pas 180 KB

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