dbgdwarf.pas 182 KB

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