dbgdwarf.pas 179 KB

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