dbgdwarf.pas 178 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438
  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. pocall_register:
  1709. result:=DW_CC_GNU_borland_fastcall_i386;
  1710. pocall_cdecl,
  1711. pocall_stdcall,
  1712. pocall_cppdecl,
  1713. pocall_mwpascal:
  1714. result:=DW_CC_normal;
  1715. else
  1716. result:=DW_CC_nocall;
  1717. end
  1718. end;
  1719. var
  1720. procendlabel : tasmlabel;
  1721. procentry,s : string;
  1722. cc : Tdwarf_calling_convention;
  1723. st : tsymtable;
  1724. vmtoffset : pint;
  1725. in_currentunit : boolean;
  1726. begin
  1727. { only write debug info for procedures defined in the current module,
  1728. except in case of methods (gcc-compatible)
  1729. }
  1730. in_currentunit:=def.in_currentunit;
  1731. if not in_currentunit and
  1732. not (def.owner.symtabletype in [objectsymtable,recordsymtable]) then
  1733. exit;
  1734. { happens for init procdef of units without init section }
  1735. if in_currentunit and
  1736. not assigned(def.procstarttai) then
  1737. exit;
  1738. if df_generic in def.defoptions then
  1739. exit;
  1740. { Procdefs are not handled by the regular def writing code, so
  1741. dbg_state is not set/checked for them. Do it here. }
  1742. if (def.dbg_state in [dbg_state_writing,dbg_state_written]) then
  1743. exit;
  1744. defnumberlist.Add(def);
  1745. { Write methods and only in the scope of their parent objectdefs. }
  1746. if (def.owner.symtabletype in [objectsymtable,recordsymtable]) then
  1747. begin
  1748. { this code can also work for nested procdefs, but is not yet
  1749. activated for those because there is no clear advantage yet to
  1750. limiting the scope of nested procedures to that of their parent,
  1751. and it makes it impossible to set breakpoints in them by
  1752. referring to their name. }
  1753. st:=def.owner;
  1754. while assigned(st.defowner) and
  1755. (tdef(st.defowner).typ = procdef) do
  1756. st:=tprocdef(st.defowner).owner;
  1757. if assigned(st) and
  1758. (tdef(st.defowner).dbg_state<>dbg_state_writing) then
  1759. exit;
  1760. end;
  1761. def.dbg_state:=dbg_state_writing;
  1762. current_asmdata.asmlists[al_dwarf_info].concat(tai_comment.Create(strpnew('Procdef '+def.fullprocname(true))));
  1763. if not is_objc_class_or_protocol(def.struct) then
  1764. append_entry(DW_TAG_subprogram,true,
  1765. [DW_AT_name,DW_FORM_string,symname(def.procsym, false)+#0])
  1766. else
  1767. append_entry(DW_TAG_subprogram,true,
  1768. [DW_AT_name,DW_FORM_string,def.mangledname+#0]);
  1769. if (ds_dwarf_cpp in current_settings.debugswitches) and (def.owner.symtabletype in [objectsymtable,recordsymtable]) then
  1770. begin
  1771. { If C++ emulation is enabled, add DW_AT_linkage_name attribute for methods.
  1772. LLDB uses it to display fully qualified method names.
  1773. Add a simple C++ mangled name without params to achieve at least "Class::Method()"
  1774. instead of just "Method" in LLDB. }
  1775. s:=tabstractrecorddef(def.owner.defowner).objrealname^;
  1776. procentry:=Format('_ZN%d%s', [Length(s), s]);
  1777. s:=symname(def.procsym, false);
  1778. procentry:=Format('%s%d%sEv'#0, [procentry, Length(s), s]);
  1779. append_attribute(DW_AT_linkage_name,DW_FORM_string, [procentry]);
  1780. end;
  1781. append_proc_frame_base(list,def);
  1782. { Append optional flags. }
  1783. { All Pascal procedures are prototyped }
  1784. append_attribute(DW_AT_prototyped,DW_FORM_flag,[true]);
  1785. { Calling convention. }
  1786. cc:=dwarf_calling_convention(def);
  1787. if (cc<>DW_CC_normal) then
  1788. append_attribute(DW_AT_calling_convention,DW_FORM_data1,[ord(cc)]);
  1789. {$ifdef i8086}
  1790. { Call model (near or far). Open Watcom compatible. }
  1791. if tcpuprocdef(def).is_far then
  1792. append_attribute(DW_AT_address_class,DW_FORM_data1,[DW_ADDR_far16])
  1793. else
  1794. append_attribute(DW_AT_address_class,DW_FORM_data1,[DW_ADDR_none]);
  1795. {$endif i8086}
  1796. { Externally visible. }
  1797. if (po_global in def.procoptions) and
  1798. (def.parast.symtablelevel<=normal_function_level) then
  1799. append_attribute(DW_AT_external,DW_FORM_flag,[true]);
  1800. { Abstract or virtual/overriding method. }
  1801. if (([po_abstractmethod, po_virtualmethod, po_overridingmethod] * def.procoptions) <> []) and
  1802. not is_objc_class_or_protocol(def.struct) and
  1803. not is_objectpascal_helper(def.struct) then
  1804. begin
  1805. if not(po_abstractmethod in def.procoptions) then
  1806. append_attribute(DW_AT_virtuality,DW_FORM_data1,[ord(DW_VIRTUALITY_virtual)])
  1807. else
  1808. append_attribute(DW_AT_virtuality,DW_FORM_data1,[ord(DW_VIRTUALITY_pure_virtual)]);
  1809. { Element number in the vmt (needs to skip stuff coming before the
  1810. actual method addresses in the vmt, so we use vmtmethodoffset()
  1811. and then divide by sizeof(pint)). }
  1812. vmtoffset:=tobjectdef(def.owner.defowner).vmtmethodoffset(def.extnumber);
  1813. append_attribute(DW_AT_vtable_elem_location,DW_FORM_block1,[3+LengthUleb128(vmtoffset)]);
  1814. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_deref)));
  1815. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_constu)));
  1816. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.Create_uleb128bit(vmtoffset));
  1817. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_plus)));
  1818. end;
  1819. { accessibility: public/private/protected }
  1820. if (def.owner.symtabletype in [objectsymtable,recordsymtable]) then
  1821. append_visibility(def.visibility);
  1822. { Return type. }
  1823. if not(is_void(tprocdef(def).returndef)) then
  1824. append_labelentry_ref(DW_AT_type,def_dwarf_lab(tprocdef(def).returndef));
  1825. { we can only write the start/end if this procedure is implemented in
  1826. this module
  1827. }
  1828. if in_currentunit then
  1829. begin
  1830. { mark end of procedure }
  1831. current_asmdata.getlabel(procendlabel,alt_dbgtype);
  1832. current_asmdata.asmlists[al_procedures].insertbefore(tai_label.create(procendlabel),def.procendtai);
  1833. if use_dotted_functions then
  1834. procentry := '.' + def.mangledname
  1835. else
  1836. procentry := def.mangledname;
  1837. {$ifdef i8086}
  1838. append_seg_name(procentry);
  1839. {$endif i8086}
  1840. append_labelentry(DW_AT_low_pc,current_asmdata.RefAsmSymbol(procentry,AT_FUNCTION));
  1841. append_labelentry(DW_AT_high_pc,procendlabel);
  1842. if not(target_info.system in systems_darwin) then
  1843. begin
  1844. current_asmdata.asmlists[al_dwarf_aranges].Concat(
  1845. tai_const.create_type_sym(aitconst_ptr_unaligned,current_asmdata.RefAsmSymbol(procentry,AT_FUNCTION)));
  1846. {$ifdef i8086}
  1847. { bits 16..31 of the offset }
  1848. current_asmdata.asmlists[al_dwarf_aranges].concat(tai_const.Create_16bit_unaligned(0));
  1849. { segment }
  1850. current_asmdata.asmlists[al_dwarf_aranges].concat(tai_const.Create_seg_name(procentry));
  1851. {$endif i8086}
  1852. current_asmdata.asmlists[al_dwarf_aranges].Concat(
  1853. tai_const.Create_rel_sym(aitconst_ptr_unaligned,current_asmdata.RefAsmSymbol(procentry,AT_FUNCTION),procendlabel));
  1854. {$ifdef i8086}
  1855. { bits 16..31 of length }
  1856. current_asmdata.asmlists[al_dwarf_aranges].concat(tai_const.Create_16bit_unaligned(0));
  1857. {$endif i8086}
  1858. end;
  1859. end;
  1860. { Don't write the funcretsym explicitly, it's also in the
  1861. localsymtable and/or parasymtable.
  1862. }
  1863. finish_entry;
  1864. if assigned(def.parast) then
  1865. begin
  1866. { First insert self, because gdb uses the fact whether or not the
  1867. first parameter of a method is artificial to distinguish static
  1868. from regular methods. }
  1869. { fortunately, self is the always the first parameter in the
  1870. paralist, since it has the lowest paranr. Note that this is not
  1871. true for Objective-C, but those methods are detected in
  1872. another way (by reading the ObjC run time information) }
  1873. write_symtable_parasyms(current_asmdata.asmlists[al_dwarf_info],def.paras);
  1874. end;
  1875. { local type defs and vars should not be written
  1876. inside the main proc }
  1877. if in_currentunit and
  1878. assigned(def.localst) and
  1879. (def.localst.symtabletype=localsymtable) then
  1880. write_symtable_syms(current_asmdata.asmlists[al_dwarf_info],def.localst);
  1881. { last write the types from this procdef }
  1882. if assigned(def.parast) then
  1883. write_symtable_defs(current_asmdata.asmlists[al_dwarf_info],def.parast);
  1884. { only try to write the localst if the routine is implemented here }
  1885. if in_currentunit and
  1886. assigned(def.localst) and
  1887. (def.localst.symtabletype=localsymtable) then
  1888. begin
  1889. write_symtable_defs(current_asmdata.asmlists[al_dwarf_info],def.localst);
  1890. { Write nested procedures -- disabled, see scope check at the
  1891. beginning; currently, these are still written in the global
  1892. scope. }
  1893. // write_symtable_procdefs(current_asmdata.asmlists[al_dwarf_info],def.localst);
  1894. end;
  1895. finish_children;
  1896. end;
  1897. function TDebugInfoDwarf.get_symlist_sym_offset(symlist: ppropaccesslistitem; out sym: tabstractvarsym; out offset: pint): boolean;
  1898. var
  1899. elesize : pint;
  1900. currdef : tdef;
  1901. indirection: boolean;
  1902. begin
  1903. result:=false;
  1904. if not assigned(symlist) then
  1905. exit;
  1906. sym:=nil;
  1907. offset:=0;
  1908. currdef:=nil;
  1909. indirection:=false;
  1910. repeat
  1911. case symlist^.sltype of
  1912. sl_load:
  1913. begin
  1914. if assigned(sym) then
  1915. internalerror(2009031203);
  1916. if not(symlist^.sym.typ in [paravarsym,localvarsym,staticvarsym,fieldvarsym]) then
  1917. { can't handle... }
  1918. exit;
  1919. sym:=tabstractvarsym(symlist^.sym);
  1920. currdef:=tabstractvarsym(sym).vardef;
  1921. if ((sym.typ=paravarsym) and
  1922. paramanager.push_addr_param(tparavarsym(sym).varspez,sym.vardef,tprocdef(sym.owner.defowner).proccalloption)) then
  1923. indirection:=true;
  1924. end;
  1925. sl_subscript:
  1926. begin
  1927. if not assigned(currdef) then
  1928. internalerror(2009031301);
  1929. if (symlist^.sym.typ<>fieldvarsym) then
  1930. internalerror(2009031202);
  1931. { can't handle offsets with indirections yet }
  1932. if indirection then
  1933. exit;
  1934. if is_packed_record_or_object(currdef) then
  1935. begin
  1936. { can't calculate the address of a non-byte aligned field }
  1937. if (tfieldvarsym(symlist^.sym).fieldoffset mod 8) <> 0 then
  1938. exit;
  1939. inc(offset,tfieldvarsym(symlist^.sym).fieldoffset div 8)
  1940. end
  1941. else
  1942. inc(offset,tfieldvarsym(symlist^.sym).fieldoffset);
  1943. currdef:=tfieldvarsym(symlist^.sym).vardef;
  1944. end;
  1945. sl_absolutetype,
  1946. sl_typeconv:
  1947. begin
  1948. currdef:=symlist^.def;
  1949. { ignore, these don't change the address }
  1950. end;
  1951. sl_vec:
  1952. begin
  1953. if not assigned(currdef) then
  1954. internalerror(2009031201);
  1955. { can't handle offsets with indirections yet }
  1956. if indirection then
  1957. exit;
  1958. case currdef.typ of
  1959. arraydef:
  1960. begin
  1961. if not is_packed_array(currdef) then
  1962. elesize:=tarraydef(currdef).elesize
  1963. else
  1964. begin
  1965. elesize:=tarraydef(currdef).elepackedbitsize;
  1966. { can't calculate the address of a non-byte aligned element }
  1967. if (elesize mod 8)<>0 then
  1968. exit;
  1969. elesize:=elesize div 8;
  1970. end;
  1971. inc(offset,(symlist^.value.svalue-tarraydef(currdef).lowrange)*elesize);
  1972. currdef:=tarraydef(currdef).elementdef;
  1973. end;
  1974. stringdef:
  1975. begin
  1976. case tstringdef(currdef).stringtype of
  1977. st_widestring,st_unicodestring:
  1978. begin
  1979. inc(offset,(symlist^.value.svalue-1)*2);
  1980. currdef:=cwidechartype;
  1981. end;
  1982. st_shortstring:
  1983. begin
  1984. inc(offset,symlist^.value.svalue);
  1985. currdef:=cansichartype;
  1986. end;
  1987. st_ansistring:
  1988. begin
  1989. inc(offset,symlist^.value.svalue-1);
  1990. currdef:=cansichartype;
  1991. end;
  1992. else
  1993. Internalerror(2022070502);
  1994. end;
  1995. end;
  1996. else
  1997. internalerror(2022070501);
  1998. end;
  1999. end;
  2000. else
  2001. internalerror(2009031403);
  2002. end;
  2003. symlist:=symlist^.next;
  2004. until not assigned(symlist);
  2005. if not assigned(sym) then
  2006. internalerror(2009031205);
  2007. result:=true;
  2008. end;
  2009. procedure TDebugInfoDwarf.appendsym_var(list:TAsmList;sym:tabstractnormalvarsym);
  2010. begin
  2011. appendsym_var_with_name_type_offset(list,sym,symname(sym, false),sym.vardef,0,[]);
  2012. end;
  2013. procedure TDebugInfoDwarf.appendsym_var_with_name_type_offset(list:TAsmList; sym:tabstractnormalvarsym; const name: string; def: tdef; offset: pint; const flags: tdwarfvarsymflags);
  2014. var
  2015. templist : TAsmList;
  2016. blocksize,size_of_int : longint;
  2017. tag : tdwarf_tag;
  2018. has_high_reg : boolean;
  2019. dreg,dreghigh : shortint;
  2020. {$ifdef i8086}
  2021. has_segment_sym_name : boolean=false;
  2022. segment_sym_name : TSymStr='';
  2023. segment_reg: TRegister=NR_NO;
  2024. {$endif i8086}
  2025. begin
  2026. blocksize:=0;
  2027. dreghigh:=0;
  2028. { external symbols can't be resolved at link time, so we
  2029. can't generate stabs for them
  2030. not sure if this applies to dwarf as well (FK)
  2031. }
  2032. if vo_is_external in sym.varoptions then
  2033. exit;
  2034. { There is no space allocated for not referenced locals }
  2035. if (sym.owner.symtabletype=localsymtable) and (sym.refs=0) then
  2036. exit;
  2037. templist:=TAsmList.create;
  2038. case sym.localloc.loc of
  2039. LOC_REGISTER,
  2040. LOC_CREGISTER,
  2041. LOC_MMREGISTER,
  2042. LOC_CMMREGISTER,
  2043. LOC_FPUREGISTER,
  2044. LOC_CFPUREGISTER :
  2045. begin
  2046. { dwarf_reg_no_error might return -1
  2047. in case the register variable has been optimized out }
  2048. dreg:=dwarf_reg_no_error(sym.localloc.register);
  2049. has_high_reg:=(sym.localloc.loc in [LOC_REGISTER,LOC_CREGISTER]) and (sym.localloc.registerhi<>NR_NO);
  2050. if has_high_reg then
  2051. dreghigh:=dwarf_reg_no_error(sym.localloc.registerhi);
  2052. if dreghigh=-1 then
  2053. has_high_reg:=false;
  2054. if (sym.localloc.loc in [LOC_REGISTER,LOC_CREGISTER]) and
  2055. (sym.typ=paravarsym) and
  2056. paramanager.push_addr_param(sym.varspez,sym.vardef,tprocdef(sym.owner.defowner).proccalloption) and
  2057. not(vo_has_local_copy in sym.varoptions) and
  2058. not is_open_string(sym.vardef) and (dreg>=0) then
  2059. begin
  2060. templist.concat(tai_const.create_8bit(ord(DW_OP_bregx)));
  2061. templist.concat(tai_const.create_uleb128bit(dreg));
  2062. templist.concat(tai_const.create_sleb128bit(0));
  2063. blocksize:=1+Lengthuleb128(dreg)+LengthSleb128(0);
  2064. end
  2065. else
  2066. begin
  2067. if has_high_reg then
  2068. begin
  2069. templist.concat(tai_comment.create(strpnew('high:low reg pair variable')));
  2070. size_of_int:=sizeof(aint);
  2071. templist.concat(tai_const.create_8bit(ord(DW_OP_regx)));
  2072. templist.concat(tai_const.create_uleb128bit(dreg));
  2073. blocksize:=1+Lengthuleb128(dreg);
  2074. templist.concat(tai_const.create_8bit(ord(DW_OP_piece)));
  2075. templist.concat(tai_const.create_uleb128bit(size_of_int));
  2076. blocksize:=blocksize+1+Lengthuleb128(size_of_int);
  2077. templist.concat(tai_const.create_8bit(ord(DW_OP_regx)));
  2078. templist.concat(tai_const.create_uleb128bit(dreghigh));
  2079. blocksize:=blocksize+1+Lengthuleb128(dreghigh);
  2080. templist.concat(tai_const.create_8bit(ord(DW_OP_piece)));
  2081. templist.concat(tai_const.create_uleb128bit(size_of_int));
  2082. blocksize:=blocksize+1+Lengthuleb128(size_of_int);
  2083. end
  2084. else if (dreg>=0) then
  2085. begin
  2086. templist.concat(tai_const.create_8bit(ord(DW_OP_regx)));
  2087. templist.concat(tai_const.create_uleb128bit(dreg));
  2088. blocksize:=1+Lengthuleb128(dreg);
  2089. end;
  2090. end;
  2091. end;
  2092. else
  2093. begin
  2094. case sym.typ of
  2095. staticvarsym:
  2096. begin
  2097. if vo_is_thread_var in sym.varoptions then
  2098. begin
  2099. if tf_section_threadvars in target_info.flags then
  2100. begin
  2101. case sizeof(puint) of
  2102. 2:
  2103. templist.concat(tai_const.create_8bit(ord(DW_OP_const2u)));
  2104. 4:
  2105. templist.concat(tai_const.create_8bit(ord(DW_OP_const4u)));
  2106. 8:
  2107. templist.concat(tai_const.create_8bit(ord(DW_OP_const8u)));
  2108. else
  2109. Internalerror(2019100501);
  2110. end;
  2111. {$push}
  2112. {$warn 6018 off} { Unreachable code due to compile time evaluation }
  2113. templist.concat(tai_const.Create_type_name(aitconst_dtpoff,sym.mangledname,0));
  2114. { so far, aitconst_dtpoff is solely 32 bit }
  2115. if (sizeof(puint)=8) and (target_info.endian=endian_little) then
  2116. templist.concat(tai_const.create_32bit(0));
  2117. templist.concat(tai_const.create_8bit(ord(DW_OP_GNU_push_tls_address)));
  2118. if (sizeof(puint)=8) and (target_info.endian=endian_big) then
  2119. templist.concat(tai_const.create_32bit(0));
  2120. {$pop}
  2121. blocksize:=2+sizeof(puint);
  2122. end
  2123. else
  2124. begin
  2125. { TODO: !!! FIXME: dwarf for thread vars !!!}
  2126. { This is only a minimal change to at least be able to get a value
  2127. in only one thread is present PM 2014-11-21, like for stabs format }
  2128. templist.concat(tai_const.create_8bit(ord(DW_OP_addr)));
  2129. templist.concat(tai_const.Create_type_name(aitconst_ptr_unaligned,sym.mangledname,
  2130. offset+sizeof(pint)));
  2131. blocksize:=1+sizeof(puint);
  2132. end;
  2133. end
  2134. else
  2135. begin
  2136. templist.concat(tai_const.create_8bit(ord(DW_OP_addr)));
  2137. templist.concat(tai_const.Create_type_name(aitconst_ptr_unaligned,sym.mangledname,offset));
  2138. blocksize:=1+sizeof(puint);
  2139. {$ifdef i8086}
  2140. segment_sym_name:=sym.mangledname;
  2141. has_segment_sym_name:=true;
  2142. {$endif i8086}
  2143. end;
  2144. end;
  2145. paravarsym,
  2146. localvarsym:
  2147. begin
  2148. { Happens when writing debug info for paras of procdefs not
  2149. implemented in the current module. Can't add a general check
  2150. for LOC_INVALID above, because staticvarsyms may also have it.
  2151. }
  2152. if sym.localloc.loc<> LOC_INVALID then
  2153. begin
  2154. if is_fbreg(sym.localloc.reference.base) then
  2155. begin
  2156. templist.concat(tai_const.create_8bit(ord(DW_OP_fbreg)));
  2157. templist.concat(tai_const.create_sleb128bit(sym.localloc.reference.offset+offset));
  2158. blocksize:=1+Lengthsleb128(sym.localloc.reference.offset+offset);
  2159. end
  2160. else
  2161. begin
  2162. dreg:=dwarf_reg(sym.localloc.reference.base);
  2163. if dreg<=31 then
  2164. begin
  2165. templist.concat(tai_const.create_8bit(ord(DW_OP_breg0)+dreg));
  2166. templist.concat(tai_const.create_sleb128bit(sym.localloc.reference.offset+offset));
  2167. blocksize:=1+Lengthsleb128(sym.localloc.reference.offset+offset);
  2168. end
  2169. else
  2170. begin
  2171. templist.concat(tai_const.create_8bit(ord(DW_OP_bregx)));
  2172. templist.concat(tai_const.create_uleb128bit(dreg));
  2173. templist.concat(tai_const.create_sleb128bit(sym.localloc.reference.offset+offset));
  2174. blocksize:=1+Lengthuleb128(dreg)+LengthSleb128(sym.localloc.reference.offset+offset);
  2175. end;
  2176. end;
  2177. {$ifdef i8086}
  2178. segment_reg:=sym.localloc.reference.segment;
  2179. {$endif i8086}
  2180. {$ifndef gdb_supports_DW_AT_variable_parameter}
  2181. { Parameters which are passed by reference. (var and the like)
  2182. Hide the reference-pointer and dereference the pointer
  2183. in the DW_AT_location block.
  2184. }
  2185. if (sym.typ=paravarsym) and
  2186. paramanager.push_addr_param(sym.varspez,sym.vardef,tprocdef(sym.owner.defowner).proccalloption) and
  2187. not(vo_has_local_copy in sym.varoptions) and
  2188. not is_open_string(sym.vardef) then
  2189. begin
  2190. templist.concat(tai_const.create_8bit(ord(DW_OP_deref)));
  2191. inc(blocksize);
  2192. end
  2193. {$endif not gdb_supports_DW_AT_variable_parameter}
  2194. end;
  2195. end
  2196. else
  2197. internalerror(200601288);
  2198. end;
  2199. end;
  2200. end;
  2201. { function results must not be added to the parameter list,
  2202. as they are not part of the signature of the function
  2203. (gdb automatically adds them according to the ABI specifications
  2204. when calling the function)
  2205. }
  2206. if (sym.typ=paravarsym) and
  2207. not(dvf_force_local_var in flags) and
  2208. not(vo_is_funcret in sym.varoptions) then
  2209. tag:=DW_TAG_formal_parameter
  2210. else
  2211. tag:=DW_TAG_variable;
  2212. { must be parasym of externally implemented procdef, but
  2213. the parasymtable can con also contain e.g. absolutevarsyms
  2214. -> check symtabletype}
  2215. if (sym.owner.symtabletype=parasymtable) and
  2216. (sym.localloc.loc=LOC_INVALID) then
  2217. begin
  2218. if (sym.owner.symtabletype<>parasymtable) then
  2219. internalerror(2009101001);
  2220. append_entry(tag,false,[
  2221. DW_AT_name,DW_FORM_string,name+#0
  2222. {
  2223. DW_AT_decl_file,DW_FORM_data1,0,
  2224. DW_AT_decl_line,DW_FORM_data1,
  2225. }
  2226. ])
  2227. end
  2228. else if not(sym.localloc.loc in [LOC_REGISTER,LOC_CREGISTER,LOC_MMREGISTER,
  2229. LOC_CMMREGISTER,LOC_FPUREGISTER,LOC_CFPUREGISTER]) and
  2230. ((sym.owner.symtabletype = globalsymtable) or
  2231. (sp_static in sym.symoptions) or
  2232. (vo_is_public in sym.varoptions)) then
  2233. append_entry(tag,false,[
  2234. DW_AT_name,DW_FORM_string,name+#0,
  2235. {
  2236. DW_AT_decl_file,DW_FORM_data1,0,
  2237. DW_AT_decl_line,DW_FORM_data1,
  2238. }
  2239. DW_AT_external,DW_FORM_flag,true,
  2240. { data continues below }
  2241. DW_AT_location,DW_FORM_block1,blocksize
  2242. ])
  2243. {$ifdef gdb_supports_DW_AT_variable_parameter}
  2244. else if (sym.typ=paravarsym) and
  2245. paramanager.push_addr_param(sym.varspez,sym.vardef,tprocdef(sym.owner.defowner).proccalloption) and
  2246. not(vo_has_local_copy in sym.varoptions) and
  2247. not is_open_string(sym.vardef) then
  2248. append_entry(tag,false,[
  2249. DW_AT_name,DW_FORM_string,name+#0,
  2250. DW_AT_variable_parameter,DW_FORM_flag,true,
  2251. {
  2252. DW_AT_decl_file,DW_FORM_data1,0,
  2253. DW_AT_decl_line,DW_FORM_data1,
  2254. }
  2255. { data continues below }
  2256. DW_AT_location,DW_FORM_block1,blocksize
  2257. ])
  2258. {$endif gdb_supports_DW_AT_variable_parameter}
  2259. else
  2260. append_entry(tag,false,[
  2261. DW_AT_name,DW_FORM_string,name+#0,
  2262. {
  2263. DW_AT_decl_file,DW_FORM_data1,0,
  2264. DW_AT_decl_line,DW_FORM_data1,
  2265. }
  2266. { data continues below }
  2267. DW_AT_location,DW_FORM_block1,blocksize
  2268. ]);
  2269. { append block data }
  2270. current_asmdata.asmlists[al_dwarf_info].concatlist(templist);
  2271. { Mark self as artificial for methods, because gdb uses the fact
  2272. whether or not the first parameter of a method is artificial to
  2273. distinguish regular from static methods (since there are no
  2274. no vo_is_self parameters for static methods, we don't have to check
  2275. that). }
  2276. if (vo_is_self in sym.varoptions) then
  2277. append_attribute(DW_AT_artificial,DW_FORM_flag,[true]);
  2278. append_labelentry_ref(DW_AT_type,def_dwarf_lab(def));
  2279. {$ifdef i8086}
  2280. if has_segment_sym_name then
  2281. append_seg_name(segment_sym_name)
  2282. else if segment_reg<>NR_NO then
  2283. append_seg_reg(segment_reg);
  2284. {$endif i8086}
  2285. templist.free;
  2286. finish_entry;
  2287. end;
  2288. procedure TDebugInfoDwarf.appendsym_staticvar(list:TAsmList;sym:tstaticvarsym);
  2289. begin
  2290. appendsym_var(list,sym);
  2291. end;
  2292. procedure TDebugInfoDwarf.appendsym_localvar(list:TAsmList;sym:tlocalvarsym);
  2293. begin
  2294. appendsym_var(list,sym);
  2295. end;
  2296. procedure TDebugInfoDwarf.appendsym_paravar(list:TAsmList;sym:tparavarsym);
  2297. begin
  2298. appendsym_var(list,sym);
  2299. end;
  2300. procedure TDebugInfoDwarf.appendsym_fieldvar(list:TAsmList;sym: tfieldvarsym);
  2301. begin
  2302. appendsym_fieldvar_with_name_offset(list,sym,symname(sym, false),sym.vardef,0);
  2303. end;
  2304. procedure TDebugInfoDwarf.appendsym_fieldvar_with_name_offset(list:TAsmList;sym: tfieldvarsym;const name: string; def: tdef; offset: pint);
  2305. var
  2306. bitoffset,
  2307. fieldoffset,
  2308. fieldnatsize: asizeint;
  2309. begin
  2310. if (sp_static in sym.symoptions) or
  2311. (sym.visibility=vis_hidden) then
  2312. exit;
  2313. if (tabstractrecordsymtable(sym.owner).usefieldalignment<>bit_alignment) or
  2314. { only ordinals are bitpacked }
  2315. not is_ordinal(sym.vardef) then
  2316. begin
  2317. { other kinds of fields can however also appear in a bitpacked }
  2318. { record, and then their offset is also specified in bits rather }
  2319. { than in bytes }
  2320. if (tabstractrecordsymtable(sym.owner).usefieldalignment<>bit_alignment) then
  2321. fieldoffset:=sym.fieldoffset
  2322. else
  2323. fieldoffset:=sym.fieldoffset div 8;
  2324. inc(fieldoffset,offset);
  2325. append_entry(DW_TAG_member,false,[
  2326. DW_AT_name,DW_FORM_string,name+#0,
  2327. DW_AT_data_member_location,DW_FORM_block1,1+lengthuleb128(fieldoffset)
  2328. ]);
  2329. end
  2330. else
  2331. begin
  2332. if (sym.vardef.packedbitsize > 255) then
  2333. internalerror(2007061201);
  2334. { we don't bitpack according to the ABI, but as close as }
  2335. { possible, i.e., equivalent to gcc's }
  2336. { __attribute__((__packed__)), which is also what gpc }
  2337. { does. }
  2338. fieldnatsize:=max(sizeof(pint),sym.vardef.size);
  2339. fieldoffset:=(sym.fieldoffset div (fieldnatsize*8)) * fieldnatsize;
  2340. inc(fieldoffset,offset);
  2341. bitoffset:=sym.fieldoffset mod (fieldnatsize*8);
  2342. if (target_info.endian=endian_little) then
  2343. bitoffset:=(fieldnatsize*8)-bitoffset-sym.vardef.packedbitsize;
  2344. append_entry(DW_TAG_member,false,[
  2345. DW_AT_name,DW_FORM_string,symname(sym, false)+#0,
  2346. { gcc also generates both a bit and byte size attribute }
  2347. { we don't support ordinals >= 256 bits }
  2348. DW_AT_byte_size,DW_FORM_data1,fieldnatsize,
  2349. { nor >= 256 bits (not yet, anyway, see IE above) }
  2350. DW_AT_bit_size,DW_FORM_data1,sym.vardef.packedbitsize,
  2351. { data1 and data2 are unsigned, bitoffset can also be negative }
  2352. DW_AT_bit_offset,DW_FORM_data4,bitoffset,
  2353. DW_AT_data_member_location,DW_FORM_block1,1+lengthuleb128(fieldoffset)
  2354. ]);
  2355. end;
  2356. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_plus_uconst)));
  2357. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_uleb128bit(fieldoffset));
  2358. if (sym.owner.symtabletype in [objectsymtable,recordsymtable]) then
  2359. append_visibility(sym.visibility);
  2360. append_labelentry_ref(DW_AT_type,def_dwarf_lab(def));
  2361. finish_entry;
  2362. end;
  2363. procedure TDebugInfoDwarf.appendsym_const(list:TAsmList;sym:tconstsym);
  2364. begin
  2365. appendsym_const_member(list,sym,false);
  2366. end;
  2367. procedure TDebugInfoDwarf.appendsym_const_member(list:TAsmList;sym:tconstsym;ismember:boolean);
  2368. var
  2369. i,
  2370. size: aint;
  2371. usedef: tdef;
  2372. begin
  2373. { These are default values of parameters. These should be encoded
  2374. via DW_AT_default_value, not as a separate sym. Moreover, their
  2375. type is not available when writing the debug info for external
  2376. procedures.
  2377. }
  2378. if (sym.owner.symtabletype=parasymtable) then
  2379. exit;
  2380. if ismember then
  2381. append_entry(DW_TAG_member,false,[
  2382. DW_AT_name,DW_FORM_string,symname(sym, false)+#0,
  2383. { The DW_AT_declaration tag is invalid according to the DWARF specifications.
  2384. But gcc adds this to static const members and gdb checks
  2385. for this flag. So we have to set it also.
  2386. }
  2387. DW_AT_declaration,DW_FORM_flag,true,
  2388. DW_AT_external,DW_FORM_flag,true
  2389. ])
  2390. else
  2391. append_entry(DW_TAG_variable,false,[
  2392. DW_AT_name,DW_FORM_string,symname(sym, false)+#0
  2393. ]);
  2394. { for string constants, constdef isn't set because they have no real type }
  2395. case sym.consttyp of
  2396. conststring:
  2397. begin
  2398. { if DW_FORM_string is used below one day, this usedef should
  2399. probably become nil }
  2400. { note: < 255 instead of <= 255 because we have to store the
  2401. entire length of the string as well, and 256 does not fit in
  2402. a byte }
  2403. if (sym.value.len<255) then
  2404. usedef:=cshortstringtype
  2405. else
  2406. usedef:=clongstringtype;
  2407. end;
  2408. constresourcestring,
  2409. constwstring:
  2410. usedef:=nil;
  2411. else
  2412. usedef:=sym.constdef;
  2413. end;
  2414. if assigned(usedef) then
  2415. append_labelentry_ref(DW_AT_type,def_dwarf_lab(usedef));
  2416. AddConstToAbbrev(ord(DW_AT_const_value));
  2417. case sym.consttyp of
  2418. conststring:
  2419. begin
  2420. { DW_FORM_string isn't supported yet by the Pascal value printer
  2421. -> create a string using raw bytes }
  2422. if (sym.value.len<255) then
  2423. begin
  2424. AddConstToAbbrev(ord(DW_FORM_block1));
  2425. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(sym.value.len+1));
  2426. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(sym.value.len));
  2427. end
  2428. else
  2429. begin
  2430. AddConstToAbbrev(ord(DW_FORM_block));
  2431. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_uleb128bit(sym.value.len+sizesinttype.size));
  2432. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.Create_sizeint_unaligned(sym.value.len));
  2433. end;
  2434. i:=0;
  2435. size:=sym.value.len;
  2436. while(i<size) do
  2437. begin
  2438. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit((pbyte(sym.value.valueptr+i)^)));
  2439. inc(i);
  2440. end;
  2441. end;
  2442. constguid,
  2443. constset:
  2444. begin
  2445. AddConstToAbbrev(ord(DW_FORM_block1));
  2446. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(usedef.size));
  2447. i:=0;
  2448. size:=sym.constdef.size;
  2449. while (i<size) do
  2450. begin
  2451. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit((pbyte(sym.value.valueptr+i)^)));
  2452. inc(i);
  2453. end;
  2454. end;
  2455. constwstring,
  2456. constresourcestring:
  2457. begin
  2458. { write dummy for now }
  2459. AddConstToAbbrev(ord(DW_FORM_string));
  2460. current_asmdata.asmlists[al_dwarf_info].concat(tai_string.create(''));
  2461. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(0));
  2462. end;
  2463. constord:
  2464. begin
  2465. if (sym.value.valueord<0) then
  2466. begin
  2467. AddConstToAbbrev(ord(DW_FORM_sdata));
  2468. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_sleb128bit(sym.value.valueord.svalue));
  2469. end
  2470. else
  2471. begin
  2472. AddConstToAbbrev(ord(DW_FORM_udata));
  2473. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_uleb128bit(sym.value.valueord.uvalue));
  2474. end;
  2475. end;
  2476. constnil:
  2477. begin
  2478. {$ifdef cpu64bitaddr}
  2479. AddConstToAbbrev(ord(DW_FORM_data8));
  2480. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_64bit_unaligned(0));
  2481. {$else cpu64bitaddr}
  2482. AddConstToAbbrev(ord(DW_FORM_data4));
  2483. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_32bit_unaligned(0));
  2484. {$endif cpu64bitaddr}
  2485. end;
  2486. constpointer:
  2487. begin
  2488. {$ifdef cpu64bitaddr}
  2489. AddConstToAbbrev(ord(DW_FORM_data8));
  2490. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_64bit_unaligned(int64(sym.value.valueordptr)));
  2491. {$else cpu64bitaddr}
  2492. AddConstToAbbrev(ord(DW_FORM_data4));
  2493. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_32bit_unaligned(longint(sym.value.valueordptr)));
  2494. {$endif cpu64bitaddr}
  2495. end;
  2496. constreal:
  2497. begin
  2498. AddConstToAbbrev(ord(DW_FORM_block1));
  2499. case tfloatdef(sym.constdef).floattype of
  2500. s32real:
  2501. begin
  2502. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(4));
  2503. current_asmdata.asmlists[al_dwarf_info].concat(tai_realconst.create_s32real(pbestreal(sym.value.valueptr)^));
  2504. end;
  2505. s64real:
  2506. begin
  2507. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(8));
  2508. current_asmdata.asmlists[al_dwarf_info].concat(tai_realconst.create_s64real(pbestreal(sym.value.valueptr)^));
  2509. end;
  2510. s64comp,
  2511. s64currency:
  2512. begin
  2513. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(8));
  2514. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_64bit_unaligned(trunc(pbestreal(sym.value.valueptr)^)));
  2515. end;
  2516. s80real,
  2517. sc80real:
  2518. begin
  2519. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(sym.constdef.size));
  2520. current_asmdata.asmlists[al_dwarf_info].concat(tai_realconst.create_s80real(pextended(sym.value.valueptr)^,sym.constdef.size));
  2521. end;
  2522. else
  2523. internalerror(200601291);
  2524. end;
  2525. end;
  2526. else
  2527. internalerror(200601292);
  2528. end;
  2529. finish_entry;
  2530. end;
  2531. procedure TDebugInfoDwarf.appendsym_label(list:TAsmList;sym: tlabelsym);
  2532. begin
  2533. { ignore label syms for now, the problem is that a label sym
  2534. can have more than one label associated e.g. in case of
  2535. an inline procedure expansion }
  2536. end;
  2537. procedure TDebugInfoDwarf.appendsym_property(list:TAsmList;sym: tpropertysym);
  2538. var
  2539. symlist: ppropaccesslistitem;
  2540. tosym: tabstractvarsym;
  2541. offset: pint;
  2542. begin
  2543. if assigned(sym.propaccesslist[palt_read]) and
  2544. not assigned(sym.propaccesslist[palt_read].procdef) then
  2545. symlist:=sym.propaccesslist[palt_read].firstsym
  2546. else
  2547. { can't handle }
  2548. exit;
  2549. if not get_symlist_sym_offset(symlist,tosym,offset) then
  2550. exit;
  2551. if not (tosym.owner.symtabletype in [objectsymtable,recordsymtable]) then
  2552. begin
  2553. if (tosym.typ=fieldvarsym) then
  2554. internalerror(2009031404);
  2555. appendsym_var_with_name_type_offset(list,tabstractnormalvarsym(tosym),symname(sym, false),sym.propdef,offset,[])
  2556. end
  2557. else
  2558. appendsym_fieldvar_with_name_offset(list,tfieldvarsym(tosym),symname(sym, false),sym.propdef,offset)
  2559. end;
  2560. function TDebugInfoDwarf.symdebugname(sym: tsym): String;
  2561. begin
  2562. result := sym.name;
  2563. end;
  2564. procedure TDebugInfoDwarf.appendsym_type(list:TAsmList;sym: ttypesym);
  2565. begin
  2566. { just queue the def if needed, beforeappenddef will
  2567. emit the typedef if necessary }
  2568. def_dwarf_lab(sym.typedef);
  2569. end;
  2570. procedure TDebugInfoDwarf.appendsym_absolute(list:TAsmList;sym:tabsolutevarsym);
  2571. var
  2572. templist : TAsmList;
  2573. blocksize : longint;
  2574. symlist : ppropaccesslistitem;
  2575. tosym: tabstractvarsym;
  2576. offset: pint;
  2577. flags: tdwarfvarsymflags;
  2578. begin
  2579. templist:=TAsmList.create;
  2580. case tabsolutevarsym(sym).abstyp of
  2581. toaddr :
  2582. begin
  2583. { MWE: replaced ifdef i368 }
  2584. (*
  2585. if target_cpu = cpu_i386 then
  2586. begin
  2587. { in theory, we could write a DW_AT_segment entry here for sym.absseg,
  2588. however I doubt that gdb supports this (FK) }
  2589. end;
  2590. *)
  2591. templist.concat(tai_const.create_8bit(3));
  2592. {$ifdef avr}
  2593. // Add $800000 to indicate that the address is in memory space
  2594. templist.concat(tai_const.create_int_dataptr_unaligned(sym.addroffset + $800000, aitconst_ptr_unaligned));
  2595. {$else}
  2596. templist.concat(tai_const.create_int_dataptr_unaligned(sym.addroffset));
  2597. {$endif}
  2598. blocksize:=1+sizeof(puint);
  2599. end;
  2600. toasm :
  2601. begin
  2602. templist.concat(tai_const.create_8bit(3));
  2603. templist.concat(tai_const.create_type_name(aitconst_ptr_unaligned,sym.mangledname,0));
  2604. blocksize:=1+sizeof(puint);
  2605. end;
  2606. tovar:
  2607. begin
  2608. symlist:=tabsolutevarsym(sym).ref.firstsym;
  2609. if get_symlist_sym_offset(symlist,tosym,offset) then
  2610. begin
  2611. if (tosym.typ=fieldvarsym) then
  2612. internalerror(2009031402);
  2613. flags:=[];
  2614. if (sym.owner.symtabletype=localsymtable) then
  2615. include(flags,dvf_force_local_var);
  2616. appendsym_var_with_name_type_offset(list,tabstractnormalvarsym(tosym),symname(sym, false),tabstractvarsym(sym).vardef,offset,flags);
  2617. end;
  2618. templist.free;
  2619. exit;
  2620. end;
  2621. end;
  2622. append_entry(DW_TAG_variable,false,[
  2623. DW_AT_name,DW_FORM_string,symname(sym, false)+#0,
  2624. {
  2625. DW_AT_decl_file,DW_FORM_data1,0,
  2626. DW_AT_decl_line,DW_FORM_data1,
  2627. }
  2628. DW_AT_external,DW_FORM_flag,true,
  2629. { data continues below }
  2630. DW_AT_location,DW_FORM_block1,blocksize
  2631. ]);
  2632. { append block data }
  2633. current_asmdata.asmlists[al_dwarf_info].concatlist(templist);
  2634. append_labelentry_ref(DW_AT_type,def_dwarf_lab(sym.vardef));
  2635. templist.free;
  2636. finish_entry;
  2637. end;
  2638. procedure TDebugInfoDwarf.beforeappendsym(list:TAsmList;sym:tsym);
  2639. begin
  2640. current_asmdata.asmlists[al_dwarf_info].concat(tai_comment.Create(strpnew('Symbol '+symname(sym, true))));
  2641. end;
  2642. procedure TDebugInfoDwarf.insertmoduleinfo;
  2643. var
  2644. templist: TAsmList;
  2645. linelist: TAsmList;
  2646. lbl : tasmlabel;
  2647. n,m : Integer;
  2648. ditem : TDirIndexItem;
  2649. fitem : TFileIndexItem;
  2650. flist : TFPList;
  2651. dbgname : TSymStr;
  2652. begin
  2653. if not (target_info.system in systems_wasm) then
  2654. begin
  2655. { insert DEBUGSTART and DEBUGEND labels }
  2656. dbgname:=make_mangledname('DEBUGSTART',current_module.localsymtable,'');
  2657. { Darwin's linker does not like two global labels both pointing to the
  2658. end of a section, which can happen in case of units without code ->
  2659. make them local; we don't need the debugtable stuff there either,
  2660. so it doesn't matter that they are not global.
  2661. }
  2662. if (target_info.system in systems_darwin) then
  2663. dbgname:='L'+dbgname;
  2664. new_section(current_asmdata.asmlists[al_start],sec_code,dbgname,0,secorder_begin);
  2665. if not(target_info.system in systems_darwin) then
  2666. current_asmdata.asmlists[al_start].concat(tai_symbol.Createname_global(dbgname,AT_METADATA,0,voidpointertype))
  2667. else
  2668. current_asmdata.asmlists[al_start].concat(tai_symbol.Createname(dbgname,AT_METADATA,0,voidpointertype));
  2669. dbgname:=make_mangledname('DEBUGEND',current_module.localsymtable,'');
  2670. { See above. }
  2671. if (target_info.system in systems_darwin) then
  2672. dbgname:='L'+dbgname;
  2673. new_section(current_asmdata.asmlists[al_end],sec_code,dbgname,0,secorder_end);
  2674. if not(target_info.system in systems_darwin) then
  2675. current_asmdata.asmlists[al_end].concat(tai_symbol.Createname_global(dbgname,AT_METADATA,0,voidpointertype))
  2676. else
  2677. current_asmdata.asmlists[al_end].concat(tai_symbol.Createname(dbgname,AT_METADATA,0,voidpointertype));
  2678. end;
  2679. { insert .Ldebug_abbrev0 label }
  2680. templist:=TAsmList.create;
  2681. new_section(templist,sec_debug_abbrev,'',0);
  2682. templist.concat(tai_symbol.createname(target_asm.labelprefix+'debug_abbrevsection0',AT_METADATA,0,voidpointertype));
  2683. { add any extra stuff which needs to be in the abbrev section, but before }
  2684. { the actual abbreviations, in between the symbol above and below, i.e. here }
  2685. templist.concat(tai_symbol.createname(target_asm.labelprefix+'debug_abbrev0',AT_METADATA,0,voidpointertype));
  2686. current_asmdata.asmlists[al_start].insertlist(templist);
  2687. templist.free;
  2688. { insert .Ldebug_line0 label }
  2689. templist:=TAsmList.create;
  2690. new_section(templist,sec_debug_line,'',0);
  2691. templist.concat(tai_symbol.createname(target_asm.labelprefix+'debug_linesection0',AT_METADATA,0,voidpointertype));
  2692. { add any extra stuff which needs to be in the line section, but before }
  2693. { the actual line info, in between the symbol above and below, i.e. here }
  2694. templist.concat(tai_symbol.createname(target_asm.labelprefix+'debug_line0',AT_METADATA,0,voidpointertype));
  2695. current_asmdata.asmlists[al_start].insertlist(templist);
  2696. templist.free;
  2697. { finalize line info if the unit doesn't contain any function/ }
  2698. { procedure/init/final code }
  2699. finish_lineinfo;
  2700. { debug line header }
  2701. linelist := current_asmdata.asmlists[al_dwarf_line];
  2702. new_section(linelist,sec_debug_line,'',0);
  2703. linelist.concat(tai_comment.Create(strpnew('=== header start ===')));
  2704. { size }
  2705. current_asmdata.getlabel(lbl,alt_dbgfile);
  2706. if use_64bit_headers then
  2707. linelist.concat(tai_const.create_32bit_unaligned(longint($FFFFFFFF)));
  2708. linelist.concat(tai_const.create_rel_sym(offsetreltype,
  2709. lbl,current_asmdata.DefineAsmSymbol(target_asm.labelprefix+'edebug_line0',AB_LOCAL,AT_METADATA,voidpointertype)));
  2710. linelist.concat(tai_label.create(lbl));
  2711. { version }
  2712. linelist.concat(tai_const.create_16bit_unaligned(dwarf_version));
  2713. { header length }
  2714. current_asmdata.getlabel(lbl,alt_dbgfile);
  2715. linelist.concat(tai_const.create_rel_sym(offsetreltype,
  2716. lbl,current_asmdata.DefineAsmSymbol(target_asm.labelprefix+'ehdebug_line0',AB_LOCAL,AT_METADATA,voidpointertype)));
  2717. linelist.concat(tai_label.create(lbl));
  2718. { minimum_instruction_length }
  2719. linelist.concat(tai_const.create_8bit(1));
  2720. { default_is_stmt }
  2721. linelist.concat(tai_const.create_8bit(1));
  2722. { line_base }
  2723. linelist.concat(tai_const.create_8bit(LINE_BASE));
  2724. { line_range }
  2725. { only line increase, no adress }
  2726. linelist.concat(tai_const.create_8bit(255));
  2727. { opcode_base }
  2728. linelist.concat(tai_const.create_8bit(OPCODE_BASE));
  2729. { standard_opcode_lengths }
  2730. { MWE: sigh... why adding the default lengths (and make those sizes sense with LEB encoding) }
  2731. { DW_LNS_copy }
  2732. linelist.concat(tai_const.create_8bit(0));
  2733. { DW_LNS_advance_pc }
  2734. linelist.concat(tai_const.create_8bit(1));
  2735. { DW_LNS_advance_line }
  2736. linelist.concat(tai_const.create_8bit(1));
  2737. { DW_LNS_set_file }
  2738. linelist.concat(tai_const.create_8bit(1));
  2739. { DW_LNS_set_column }
  2740. linelist.concat(tai_const.create_8bit(1));
  2741. { DW_LNS_negate_stmt }
  2742. linelist.concat(tai_const.create_8bit(0));
  2743. { DW_LNS_set_basic_block }
  2744. linelist.concat(tai_const.create_8bit(0));
  2745. { DW_LNS_const_add_pc }
  2746. linelist.concat(tai_const.create_8bit(0));
  2747. { DW_LNS_fixed_advance_pc }
  2748. linelist.concat(tai_const.create_8bit(1));
  2749. { DW_LNS_set_prologue_end }
  2750. linelist.concat(tai_const.create_8bit(0));
  2751. { DW_LNS_set_epilogue_begin }
  2752. linelist.concat(tai_const.create_8bit(0));
  2753. { DW_LNS_set_isa }
  2754. linelist.concat(tai_const.create_8bit(1));
  2755. { Create single list of filenames sorted in IndexNr }
  2756. flist:=TFPList.Create;
  2757. for n := 0 to dirlist.Count - 1 do
  2758. begin
  2759. ditem := TDirIndexItem(dirlist[n]);
  2760. for m := 0 to ditem.Files.Count - 1 do
  2761. flist.Add(ditem.Files[m]);
  2762. end;
  2763. flist.Sort(@FileListSortCompare);
  2764. { include_directories }
  2765. linelist.concat(tai_comment.Create(strpnew('include_directories')));
  2766. for n := 0 to dirlist.Count - 1 do
  2767. begin
  2768. ditem := TDirIndexItem(dirlist[n]);
  2769. if ditem.Name = '.' then
  2770. Continue;
  2771. { Write without trailing path delimiter and also don't prefix with ./ for current dir (already done while adding to dirlist }
  2772. linelist.concat(tai_string.create(ditem.Name+#0));
  2773. end;
  2774. linelist.concat(tai_const.create_8bit(0));
  2775. { file_names }
  2776. linelist.concat(tai_comment.Create(strpnew('file_names')));
  2777. for n := 0 to flist.Count - 1 do
  2778. begin
  2779. fitem := TFileIndexItem(flist[n]);
  2780. { file name }
  2781. linelist.concat(tai_string.create(fitem.Name+#0));
  2782. { directory index }
  2783. linelist.concat(tai_const.create_uleb128bit(fitem.DirIndex));
  2784. { last modification }
  2785. linelist.concat(tai_const.create_uleb128bit(0));
  2786. { file length }
  2787. linelist.concat(tai_const.create_uleb128bit(0));
  2788. end;
  2789. linelist.concat(tai_const.create_8bit(0));
  2790. { end of debug line header }
  2791. linelist.concat(tai_symbol.createname(target_asm.labelprefix+'ehdebug_line0',AT_METADATA,0,voidpointertype));
  2792. linelist.concat(tai_comment.Create(strpnew('=== header end ===')));
  2793. { add line program }
  2794. linelist.concatList(asmline);
  2795. { end of debug line table }
  2796. linelist.concat(tai_symbol.createname(target_asm.labelprefix+'edebug_line0',AT_METADATA,0,voidpointertype));
  2797. flist.free;
  2798. end;
  2799. procedure TDebugInfoDwarf.inserttypeinfo;
  2800. var
  2801. storefilepos : tfileposinfo;
  2802. lenstartlabel,arangestartlabel: tasmlabel;
  2803. i : longint;
  2804. def: tdef;
  2805. dbgname: string;
  2806. vardatatype: ttypesym;
  2807. bind: tasmsymbind;
  2808. lang: tdwarf_source_language;
  2809. begin
  2810. include(current_module.moduleflags,mf_has_dwarf_debuginfo);
  2811. storefilepos:=current_filepos;
  2812. current_filepos:=current_module.mainfilepos;
  2813. if assigned(dwarflabels) then
  2814. internalerror(2015100301);
  2815. { one item per def, plus some extra space in case of nested types,
  2816. externally used types etc (it will grow further if necessary) }
  2817. i:=current_module.localsymtable.DefList.count*4;
  2818. if assigned(current_module.globalsymtable) then
  2819. inc(i,current_module.globalsymtable.DefList.count*2);
  2820. dwarflabels:=TDwarfLabHashSet.Create(i,true,false);
  2821. currabbrevnumber:=0;
  2822. defnumberlist:=TFPObjectList.create(false);
  2823. deftowritelist:=TFPObjectList.create(false);
  2824. { not exported (FK)
  2825. FILEREC
  2826. TEXTREC
  2827. }
  2828. vardatatype:=try_search_system_type('TVARDATA');
  2829. if assigned(vardatatype) then
  2830. vardatadef:=trecorddef(vardatatype.typedef);
  2831. { write start labels }
  2832. new_section(current_asmdata.asmlists[al_dwarf_info],sec_debug_info,'',0);
  2833. current_asmdata.asmlists[al_dwarf_info].concat(tai_symbol.createname(target_asm.labelprefix+'debug_info0',AT_METADATA,0,voidpointertype));
  2834. { start abbrev section }
  2835. new_section(current_asmdata.asmlists[al_dwarf_abbrev],sec_debug_abbrev,'',0);
  2836. if not(target_info.system in systems_darwin) then
  2837. begin
  2838. { start aranges section }
  2839. new_section(current_asmdata.asmlists[al_dwarf_aranges],sec_debug_aranges,'',0);
  2840. current_asmdata.getlabel(arangestartlabel,alt_dbgfile);
  2841. if use_64bit_headers then
  2842. current_asmdata.asmlists[al_dwarf_aranges].concat(tai_const.create_32bit_unaligned(longint($FFFFFFFF)));
  2843. current_asmdata.asmlists[al_dwarf_aranges].concat(tai_const.create_rel_sym(offsetreltype,
  2844. arangestartlabel,current_asmdata.DefineAsmSymbol(target_asm.labelprefix+'earanges0',AB_LOCAL,AT_METADATA,voidpointertype)));
  2845. current_asmdata.asmlists[al_dwarf_aranges].concat(tai_label.create(arangestartlabel));
  2846. current_asmdata.asmlists[al_dwarf_aranges].concat(tai_const.create_16bit_unaligned(2));
  2847. if not(tf_dwarf_relative_addresses in target_info.flags) then
  2848. current_asmdata.asmlists[al_dwarf_aranges].concat(tai_const.create_type_sym(offsetabstype,
  2849. current_asmdata.DefineAsmSymbol(target_asm.labelprefix+'debug_info0',AB_LOCAL,AT_METADATA,voidpointertype)))
  2850. else
  2851. current_asmdata.asmlists[al_dwarf_aranges].concat(tai_const.create_rel_sym(offsetreltype,
  2852. current_asmdata.DefineAsmSymbol(target_asm.labelprefix+'debug_infosection0',AB_LOCAL,AT_METADATA,voidpointertype),
  2853. current_asmdata.DefineAsmSymbol(target_asm.labelprefix+'debug_info0',AB_LOCAL,AT_METADATA,voidpointertype)));
  2854. {$ifdef i8086}
  2855. { address_size }
  2856. current_asmdata.asmlists[al_dwarf_aranges].concat(tai_const.create_8bit(4));
  2857. { segment_size }
  2858. current_asmdata.asmlists[al_dwarf_aranges].concat(tai_const.create_8bit(2));
  2859. { no alignment/padding bytes on i8086 for Open Watcom compatibility }
  2860. {$else i8086}
  2861. { address_size }
  2862. current_asmdata.asmlists[al_dwarf_aranges].concat(tai_const.create_8bit(sizeof(pint)));
  2863. { segment_size }
  2864. current_asmdata.asmlists[al_dwarf_aranges].concat(tai_const.create_8bit(0));
  2865. { alignment }
  2866. current_asmdata.asmlists[al_dwarf_aranges].concat(tai_const.create_32bit_unaligned(0));
  2867. {$endif i8086}
  2868. { start ranges section }
  2869. new_section(current_asmdata.asmlists[al_dwarf_ranges],sec_debug_ranges,'',0);
  2870. end;
  2871. { debug info header }
  2872. current_asmdata.getlabel(lenstartlabel,alt_dbgfile);
  2873. { size }
  2874. if use_64bit_headers then
  2875. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_32bit_unaligned(longint($FFFFFFFF)));
  2876. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_rel_sym(offsetreltype,
  2877. lenstartlabel,current_asmdata.DefineAsmSymbol(target_asm.labelprefix+'edebug_info0',AB_LOCAL,AT_METADATA,voidpointertype)));
  2878. current_asmdata.asmlists[al_dwarf_info].concat(tai_label.create(lenstartlabel));
  2879. { version }
  2880. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_16bit_unaligned(dwarf_version));
  2881. { abbrev table (=relative from section start)}
  2882. if not(tf_dwarf_relative_addresses in target_info.flags) then
  2883. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_type_sym(offsetabstype,
  2884. current_asmdata.DefineAsmSymbol(target_asm.labelprefix+'debug_abbrev0',AB_LOCAL,AT_METADATA,voidpointertype)))
  2885. else
  2886. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_rel_sym(offsetreltype,
  2887. current_asmdata.DefineAsmSymbol(target_asm.labelprefix+'debug_abbrevsection0',AB_LOCAL,AT_METADATA,voidpointertype),
  2888. current_asmdata.DefineAsmSymbol(target_asm.labelprefix+'debug_abbrev0',AB_LOCAL,AT_METADATA,voidpointertype)));
  2889. { address size }
  2890. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(sizeof(pint)));
  2891. if (ds_dwarf_cpp in current_settings.debugswitches) then
  2892. lang:=DW_LANG_C_plus_plus
  2893. else
  2894. lang:=DW_LANG_Pascal83;
  2895. { first manadatory compilation unit TAG }
  2896. append_entry(DW_TAG_compile_unit,true,[
  2897. DW_AT_name,DW_FORM_string,relative_dwarf_path(current_module.sourcefiles.get_file(1).path+current_module.sourcefiles.get_file(1).name)+#0,
  2898. DW_AT_producer,DW_FORM_string,'Free Pascal '+full_version_string+' '+date_string+#0,
  2899. DW_AT_comp_dir,DW_FORM_string,BSToSlash(FixPath(GetCurrentDir,false))+#0,
  2900. DW_AT_language,DW_FORM_data1,lang,
  2901. DW_AT_identifier_case,DW_FORM_data1,DW_ID_case_insensitive]);
  2902. {$ifdef i8086}
  2903. case current_settings.x86memorymodel of
  2904. mm_tiny,
  2905. mm_small:
  2906. append_attribute(DW_AT_WATCOM_memory_model,DW_FORM_data1,[DW_WATCOM_MEMORY_MODEL_small]);
  2907. mm_medium:
  2908. append_attribute(DW_AT_WATCOM_memory_model,DW_FORM_data1,[DW_WATCOM_MEMORY_MODEL_medium]);
  2909. mm_compact:
  2910. append_attribute(DW_AT_WATCOM_memory_model,DW_FORM_data1,[DW_WATCOM_MEMORY_MODEL_compact]);
  2911. mm_large:
  2912. append_attribute(DW_AT_WATCOM_memory_model,DW_FORM_data1,[DW_WATCOM_MEMORY_MODEL_large]);
  2913. mm_huge:
  2914. append_attribute(DW_AT_WATCOM_memory_model,DW_FORM_data1,[DW_WATCOM_MEMORY_MODEL_huge]);
  2915. end;
  2916. {$endif i8086}
  2917. { reference to line info section }
  2918. if not(tf_dwarf_relative_addresses in target_info.flags) then
  2919. append_labelentry_dataptr_abs(DW_AT_stmt_list,current_asmdata.DefineAsmSymbol(target_asm.labelprefix+'debug_line0',AB_LOCAL,AT_METADATA,voidpointertype))
  2920. else
  2921. append_labelentry_dataptr_rel(DW_AT_stmt_list,
  2922. current_asmdata.DefineAsmSymbol(target_asm.labelprefix+'debug_linesection0',AB_LOCAL,AT_METADATA,voidpointertype),
  2923. current_asmdata.DefineAsmSymbol(target_asm.labelprefix+'debug_line0',AB_LOCAL,AT_METADATA,voidpointertype));
  2924. if (m_objectivec1 in current_settings.modeswitches) then
  2925. append_attribute(DW_AT_APPLE_major_runtime_vers,DW_FORM_data1,[1]);
  2926. if target_info.system in systems_wasm then
  2927. begin
  2928. append_attribute(DW_AT_low_pc,DW_FORM_data4,[0]);
  2929. { todo: append DW_AT_ranges }
  2930. end
  2931. else
  2932. begin
  2933. dbgname:=make_mangledname('DEBUGSTART',current_module.localsymtable,'');
  2934. if (target_info.system in systems_darwin) then
  2935. begin
  2936. bind:=AB_LOCAL;
  2937. dbgname:='L'+dbgname;
  2938. end
  2939. else
  2940. bind:=AB_GLOBAL;
  2941. append_labelentry(DW_AT_low_pc,current_asmdata.DefineAsmSymbol(dbgname,bind,AT_METADATA,voidpointertype));
  2942. dbgname:=make_mangledname('DEBUGEND',current_module.localsymtable,'');
  2943. if (target_info.system in systems_darwin) then
  2944. dbgname:='L'+dbgname;
  2945. append_labelentry(DW_AT_high_pc,current_asmdata.DefineAsmSymbol(dbgname,bind,AT_METADATA,voidpointertype));
  2946. end;
  2947. finish_entry;
  2948. { write all global/local variables. This will flag all required tdefs }
  2949. if assigned(current_module.globalsymtable) then
  2950. write_symtable_syms(current_asmdata.asmlists[al_dwarf_info],current_module.globalsymtable);
  2951. if assigned(current_module.localsymtable) then
  2952. write_symtable_syms(current_asmdata.asmlists[al_dwarf_info],current_module.localsymtable);
  2953. { write all procedures and methods. This will flag all required tdefs }
  2954. if assigned(current_module.globalsymtable) then
  2955. write_symtable_procdefs(current_asmdata.asmlists[al_dwarf_info],current_module.globalsymtable);
  2956. if assigned(current_module.localsymtable) then
  2957. write_symtable_procdefs(current_asmdata.asmlists[al_dwarf_info],current_module.localsymtable);
  2958. { reset unit type info flag }
  2959. reset_unit_type_info;
  2960. { write used types from the used units }
  2961. write_used_unit_type_info(current_asmdata.asmlists[al_dwarf_info],current_module);
  2962. { last write the types from this unit }
  2963. if assigned(current_module.globalsymtable) then
  2964. write_symtable_defs(current_asmdata.asmlists[al_dwarf_info],current_module.globalsymtable);
  2965. if assigned(current_module.localsymtable) then
  2966. write_symtable_defs(current_asmdata.asmlists[al_dwarf_info],current_module.localsymtable);
  2967. { write defs not written yet }
  2968. write_remaining_defs_to_write(current_asmdata.asmlists[al_dwarf_info]);
  2969. { close compilation unit entry }
  2970. finish_children;
  2971. { end of debug info table }
  2972. current_asmdata.asmlists[al_dwarf_info].concat(tai_symbol.createname(target_asm.labelprefix+'edebug_info0',AT_METADATA,0,voidpointertype));
  2973. { end of abbrev table }
  2974. current_asmdata.asmlists[al_dwarf_abbrev].concat(tai_const.create_8bit(0));
  2975. if not(target_info.system in systems_darwin) then
  2976. begin
  2977. { end of aranges table }
  2978. {$ifdef i8086}
  2979. { 32-bit offset }
  2980. current_asmdata.asmlists[al_dwarf_aranges].concat(tai_const.Create_32bit_unaligned(0));
  2981. { 16-bit segment }
  2982. current_asmdata.asmlists[al_dwarf_aranges].concat(tai_const.Create_16bit_unaligned(0));
  2983. { 32-bit length }
  2984. current_asmdata.asmlists[al_dwarf_aranges].concat(tai_const.Create_32bit_unaligned(0));
  2985. {$else i8086}
  2986. { offset }
  2987. current_asmdata.asmlists[al_dwarf_aranges].concat(tai_const.Create_aint(0));
  2988. { length }
  2989. current_asmdata.asmlists[al_dwarf_aranges].concat(tai_const.Create_aint(0));
  2990. {$endif i8086}
  2991. current_asmdata.asmlists[al_dwarf_aranges].concat(tai_symbol.createname(target_asm.labelprefix+'earanges0',AT_METADATA,0,voidpointertype));
  2992. end;
  2993. { reset all def debug states }
  2994. for i:=0 to defnumberlist.count-1 do
  2995. begin
  2996. def := tdef(defnumberlist[i]);
  2997. if assigned(def) then
  2998. def.dbg_state:=dbg_state_unused;
  2999. end;
  3000. dwarflabels.free;
  3001. dwarflabels:=nil;
  3002. defnumberlist.free;
  3003. defnumberlist:=nil;
  3004. deftowritelist.free;
  3005. deftowritelist:=nil;
  3006. current_filepos:=storefilepos;
  3007. end;
  3008. procedure TDebugInfoDwarf.referencesections(list:TAsmList);
  3009. var
  3010. hp : tmodule;
  3011. begin
  3012. { Reference all DEBUGINFO sections from the main .fpc section }
  3013. { to prevent eliminating them by smartlinking }
  3014. if (target_info.system in ([system_powerpc_macosclassic]+systems_darwin+systems_wasm)) then
  3015. exit;
  3016. new_section(list,sec_fpc,'links',0);
  3017. { include reference to all debuginfo sections of used units }
  3018. hp:=tmodule(loaded_units.first);
  3019. while assigned(hp) do
  3020. begin
  3021. If (mf_has_dwarf_debuginfo in hp.moduleflags) and not assigned(hp.package) then
  3022. begin
  3023. list.concat(Tai_const.Createname(make_mangledname('DEBUGSTART',hp.localsymtable,''),0));
  3024. list.concat(Tai_const.Createname(make_mangledname('DEBUGEND',hp.localsymtable,''),0));
  3025. end;
  3026. hp:=tmodule(hp.next);
  3027. end;
  3028. end;
  3029. function TDebugInfoDwarf.symname(sym: tsym; manglename: boolean): String;
  3030. begin
  3031. if (sym.typ=paravarsym) and
  3032. (vo_is_self in tparavarsym(sym).varoptions) then
  3033. { We use 'this' for regular methods because that's what gdb triggers
  3034. on to automatically search fields. Don't do this for class methods,
  3035. because search class fields is not supported, and gdb 7.0+ fails
  3036. in this case because "this" is not a record in that case (it's a
  3037. pointer to a vmt) }
  3038. if not is_objc_class_or_protocol(tdef(sym.owner.defowner.owner.defowner)) and
  3039. not(po_classmethod in tabstractprocdef(sym.owner.defowner).procoptions) then
  3040. result:='this'
  3041. else
  3042. result:='self'
  3043. else if (sym.typ=typesym) and
  3044. is_objc_class_or_protocol(ttypesym(sym).typedef) then
  3045. result:=tobjectdef(ttypesym(sym).typedef).objextname^
  3046. else if (ds_dwarf_method_class_prefix in current_settings.debugswitches) and
  3047. (sym.typ=procsym) and
  3048. (tprocsym(sym).owner.symtabletype in [objectsymtable,recordsymtable]) then
  3049. begin
  3050. result:=tprocsym(sym).owner.name^+'__';
  3051. if manglename then
  3052. result := result + sym.name
  3053. else
  3054. result := result + symdebugname(sym);
  3055. end
  3056. else
  3057. begin
  3058. if manglename then
  3059. result := sym.name
  3060. else
  3061. result := symdebugname(sym);
  3062. end;
  3063. end;
  3064. procedure TDebugInfoDwarf.append_visibility(vis: tvisibility);
  3065. begin
  3066. case vis of
  3067. vis_hidden,
  3068. vis_private,
  3069. vis_strictprivate:
  3070. append_attribute(DW_AT_accessibility,DW_FORM_data1,[ord(DW_ACCESS_private)]);
  3071. vis_protected,
  3072. vis_strictprotected:
  3073. append_attribute(DW_AT_accessibility,DW_FORM_data1,[ord(DW_ACCESS_protected)]);
  3074. vis_published,
  3075. vis_public:
  3076. { default };
  3077. vis_none:
  3078. internalerror(2019050720);
  3079. end;
  3080. end;
  3081. procedure TDebugInfoDwarf.insertlineinfo(list:TAsmList);
  3082. var
  3083. currfileinfo,
  3084. lastfileinfo : tfileposinfo;
  3085. currfuncname : pshortstring;
  3086. currstatement: boolean;
  3087. currsectype : TAsmSectiontype;
  3088. hp, hpend : tai;
  3089. infile : tinputfile;
  3090. prevcolumn,
  3091. diffline,
  3092. prevline,
  3093. prevfileidx,
  3094. currfileidx,
  3095. nolineinfolevel : Integer;
  3096. prevlabel,
  3097. currlabel : tasmlabel;
  3098. begin
  3099. {$ifdef OMFOBJSUPPORT}
  3100. if ds_dwarf_omf_linnum in current_settings.debugswitches then
  3101. dbgcodeview.InsertLineInfo_OMF_LINNUM_MsLink(list);
  3102. {$endif OMFOBJSUPPORT}
  3103. { this function will always terminate the lineinfo block }
  3104. generated_lineinfo := true;
  3105. { if this unit only contains code without debug info (implicit init
  3106. or final etc), make sure the file table contains at least one entry
  3107. (the main source of the unit), because normally this table gets
  3108. populated via calls to get_file_index and that won't happen in this
  3109. case }
  3110. get_file_index(current_module.sourcefiles.get_file(1));
  3111. FillChar(lastfileinfo,sizeof(lastfileinfo),0);
  3112. currfuncname:=nil;
  3113. currsectype:=sec_code;
  3114. hp:=Tai(list.first);
  3115. currstatement:=true;
  3116. prevcolumn := 0;
  3117. prevline := 1;
  3118. prevfileidx := 1;
  3119. prevlabel := nil;
  3120. nolineinfolevel:=0;
  3121. while assigned(hp) do
  3122. begin
  3123. case hp.typ of
  3124. ait_section :
  3125. currsectype:=tai_section(hp).sectype;
  3126. ait_function_name :
  3127. begin
  3128. currfuncname:=tai_function_name(hp).funcname;
  3129. asmline.concat(tai_comment.Create(strpnew('function: '+currfuncname^)));
  3130. end;
  3131. ait_force_line :
  3132. begin
  3133. lastfileinfo.line:=-1;
  3134. end;
  3135. ait_marker :
  3136. begin
  3137. case tai_marker(hp).kind of
  3138. mark_NoLineInfoStart:
  3139. inc(nolineinfolevel);
  3140. mark_NoLineInfoEnd:
  3141. dec(nolineinfolevel);
  3142. else
  3143. ;
  3144. end;
  3145. end;
  3146. else
  3147. ;
  3148. end;
  3149. if (currsectype=sec_code) and
  3150. (hp.typ=ait_instruction) then
  3151. begin
  3152. currfileinfo:=tailineinfo(hp).fileinfo;
  3153. { file changed ? (must be before line info) }
  3154. if (currfileinfo.fileindex<>0) and
  3155. ((lastfileinfo.fileindex<>currfileinfo.fileindex) or
  3156. (lastfileinfo.moduleindex<>currfileinfo.moduleindex)) then
  3157. begin
  3158. infile:=get_module(currfileinfo.moduleindex).sourcefiles.get_file(currfileinfo.fileindex);
  3159. if assigned(infile) then
  3160. begin
  3161. currfileidx := get_file_index(infile);
  3162. if prevfileidx <> currfileidx then
  3163. begin
  3164. list.insertbefore(tai_comment.Create(strpnew('path: '+infile.path)), hp);
  3165. list.insertbefore(tai_comment.Create(strpnew('file: '+infile.name)), hp);
  3166. list.insertbefore(tai_comment.Create(strpnew('indx: '+tostr(currfileidx))), hp);
  3167. { set file }
  3168. asmline.concat(tai_comment.Create(strpnew('path: '+infile.path)));
  3169. asmline.concat(tai_comment.Create(strpnew('file: '+infile.name)));
  3170. asmline.concat(tai_const.create_8bit(DW_LNS_set_file));
  3171. asmline.concat(tai_const.create_uleb128bit(currfileidx));
  3172. prevfileidx := currfileidx;
  3173. end;
  3174. { force new line info }
  3175. lastfileinfo.line:=-1;
  3176. end;
  3177. end;
  3178. { Set the line-nr to 0 if the code does not corresponds to a particular line }
  3179. if nolineinfolevel>0 then
  3180. currfileinfo.line := 0;
  3181. { line changed ? }
  3182. if (lastfileinfo.line<>currfileinfo.line) and ((currfileinfo.line<>0) or (nolineinfolevel>0)) then
  3183. begin
  3184. { set address }
  3185. current_asmdata.getlabel(currlabel, alt_dbgline);
  3186. list.insertbefore(tai_label.create(currlabel), hp);
  3187. asmline.concat(tai_comment.Create(strpnew('['+tostr(currfileinfo.line)+':'+tostr(currfileinfo.column)+']')));
  3188. if (prevlabel = nil) or
  3189. { darwin's assembler cannot create an uleb128 of the difference
  3190. between to symbols
  3191. same goes for Solaris native assembler
  3192. ... and riscv }
  3193. (target_info.system in systems_darwin+[system_riscv32_linux,system_riscv64_linux,
  3194. system_riscv32_embedded,system_riscv64_embedded]) or
  3195. (target_asm.id=as_solaris_as) then
  3196. begin
  3197. asmline.concat(tai_const.create_8bit(DW_LNS_extended_op));
  3198. asmline.concat(tai_const.create_uleb128bit(1+sizeof(pint)));
  3199. asmline.concat(tai_const.create_8bit(DW_LNE_set_address));
  3200. asmline.concat(tai_const.create_type_sym(aitconst_ptr_unaligned,currlabel));
  3201. {$ifdef i8086}
  3202. { on i8086 we also emit an Open Watcom-specific 'set segment' op }
  3203. asmline.concat(tai_const.create_8bit(DW_LNS_extended_op));
  3204. asmline.concat(tai_const.create_uleb128bit(3));
  3205. asmline.concat(tai_const.create_8bit(DW_LNE_set_segment));
  3206. asmline.concat(tai_const.Create_seg_name(currlabel.Name));
  3207. {$endif i8086}
  3208. end
  3209. else
  3210. begin
  3211. asmline.concat(tai_const.create_8bit(DW_LNS_advance_pc));
  3212. asmline.concat(tai_const.create_rel_sym(aitconst_uleb128bit, prevlabel, currlabel));
  3213. end;
  3214. prevlabel := currlabel;
  3215. { set column }
  3216. if prevcolumn <> currfileinfo.column then
  3217. begin
  3218. asmline.concat(tai_const.create_8bit(DW_LNS_set_column));
  3219. asmline.concat(tai_const.create_uleb128bit(currfileinfo.column));
  3220. prevcolumn := currfileinfo.column;
  3221. end;
  3222. { set statement }
  3223. if (currfileinfo.line=0) and currstatement then
  3224. begin
  3225. currstatement := false;
  3226. asmline.concat(tai_const.create_8bit(DW_LNS_negate_stmt));
  3227. end;
  3228. if not currstatement and (currfileinfo.line>0) then
  3229. begin
  3230. currstatement := true;
  3231. asmline.concat(tai_const.create_8bit(DW_LNS_negate_stmt));
  3232. end;
  3233. { set line }
  3234. diffline := currfileinfo.line - prevline;
  3235. if (diffline >= LINE_BASE) and (OPCODE_BASE + diffline - LINE_BASE <= 255) then
  3236. begin
  3237. { use special opcode, this also adds a row }
  3238. asmline.concat(tai_const.create_8bit(OPCODE_BASE + diffline - LINE_BASE));
  3239. end
  3240. else
  3241. begin
  3242. if diffline <> 0 then
  3243. begin
  3244. asmline.concat(tai_const.create_8bit(DW_LNS_advance_line));
  3245. asmline.concat(tai_const.create_sleb128bit(diffline));
  3246. end;
  3247. { no row added yet, do it manually }
  3248. asmline.concat(tai_const.create_8bit(DW_LNS_copy));
  3249. end;
  3250. prevline := currfileinfo.line;
  3251. end;
  3252. lastfileinfo:=currfileinfo;
  3253. end;
  3254. hpend:=hp;
  3255. hp:=tai(hp.next);
  3256. end;
  3257. if assigned(hpend) then
  3258. begin
  3259. { set address for end (see appendix 3 of dwarf 2 specs) }
  3260. current_asmdata.getlabel(currlabel, alt_dbgline);
  3261. list.insertafter(tai_label.create(currlabel), hpend);
  3262. asmline.concat(tai_const.create_8bit(DW_LNS_extended_op));
  3263. asmline.concat(tai_const.create_uleb128bit(1+sizeof(pint)));
  3264. asmline.concat(tai_const.create_8bit(DW_LNE_set_address));
  3265. asmline.concat(tai_const.create_type_sym(aitconst_ptr_unaligned,currlabel));
  3266. end;
  3267. { end sequence }
  3268. asmline.concat(tai_const.Create_8bit(DW_LNS_extended_op));
  3269. asmline.concat(tai_const.Create_8bit(1));
  3270. asmline.concat(tai_const.Create_8bit(DW_LNE_end_sequence));
  3271. asmline.concat(tai_comment.Create(strpnew('###################')));
  3272. end;
  3273. procedure TDebugInfoDwarf.finish_lineinfo;
  3274. var
  3275. infile: tinputfile;
  3276. begin
  3277. { only needed if no line info at all has been generated }
  3278. if generated_lineinfo then
  3279. begin
  3280. { reset for next module compilation }
  3281. generated_lineinfo:=false;
  3282. exit;
  3283. end;
  3284. { at least the Darwin linker is annoyed if you do not }
  3285. { finish the lineinfo section, or if it doesn't }
  3286. { contain at least one file name and set_address }
  3287. infile:=current_module.sourcefiles.get_file(1);
  3288. if not assigned(infile) then
  3289. internalerror(2006020211);
  3290. asmline.concat(tai_const.create_8bit(DW_LNS_set_file));
  3291. asmline.concat(tai_const.create_uleb128bit(get_file_index(infile)));
  3292. asmline.concat(tai_const.create_8bit(DW_LNS_extended_op));
  3293. asmline.concat(tai_const.create_uleb128bit(1+sizeof(pint)));
  3294. asmline.concat(tai_const.create_8bit(DW_LNE_set_address));
  3295. asmline.concat(tai_const.create_type_sym(aitconst_ptr_unaligned,nil));
  3296. asmline.concat(tai_const.create_8bit(DW_LNS_extended_op));
  3297. asmline.concat(tai_const.Create_8bit(1));
  3298. asmline.concat(tai_const.Create_8bit(DW_LNE_end_sequence));
  3299. asmline.concat(tai_comment.Create(strpnew('###################')));
  3300. end;
  3301. {****************************************************************************
  3302. TDebugInfoDwarf2
  3303. ****************************************************************************}
  3304. procedure TDebugInfoDwarf2.appenddef_file(list:TAsmList;def: tfiledef);
  3305. begin
  3306. { gdb 6.4 doesn't support files so far so we use some fake recorddef
  3307. file recs. are less than 1k so using data2 is enough }
  3308. if assigned(def.typesym) then
  3309. append_entry(DW_TAG_structure_type,false,[
  3310. DW_AT_name,DW_FORM_string,symname(def.typesym, false)+#0,
  3311. DW_AT_byte_size,DW_FORM_udata,def.size
  3312. ])
  3313. else
  3314. append_entry(DW_TAG_structure_type,false,[
  3315. DW_AT_byte_size,DW_FORM_udata,def.size
  3316. ]);
  3317. finish_entry;
  3318. end;
  3319. procedure TDebugInfoDwarf2.appenddef_formal(list:TAsmList;def: tformaldef);
  3320. begin
  3321. { gdb 6.4 doesn't support DW_TAG_unspecified_type so we
  3322. replace it with a unsigned type with size 0 (FK)
  3323. }
  3324. append_entry(DW_TAG_base_type,false,[
  3325. DW_AT_name,DW_FORM_string,'FormalDef'#0,
  3326. DW_AT_encoding,DW_FORM_data1,DW_ATE_unsigned,
  3327. DW_AT_byte_size,DW_FORM_data1,0
  3328. ]);
  3329. finish_entry;
  3330. end;
  3331. procedure TDebugInfoDwarf2.append_object_struct(def: tobjectdef; const createlabel: boolean; const objectname: PShortString);
  3332. begin
  3333. if createlabel then
  3334. begin
  3335. if not(tf_dwarf_only_local_labels in target_info.flags) then
  3336. current_asmdata.asmlists[al_dwarf_info].concat(tai_symbol.create_global(def_dwarf_class_struct_lab(def),0))
  3337. else
  3338. current_asmdata.asmlists[al_dwarf_info].concat(tai_symbol.create(def_dwarf_class_struct_lab(def),0));
  3339. end;
  3340. if assigned(objectname) then
  3341. append_entry(DW_TAG_class_type,true,[
  3342. DW_AT_name,DW_FORM_string,objectname^+#0,
  3343. DW_AT_byte_size,DW_FORM_udata,tobjectsymtable(def.symtable).datasize
  3344. ])
  3345. else
  3346. append_entry(DW_TAG_class_type,true,[
  3347. DW_AT_byte_size,DW_FORM_udata,tobjectsymtable(def.symtable).datasize
  3348. ]);
  3349. { Apple-specific tag that identifies it as an Objective-C class }
  3350. if (def.objecttype=odt_objcclass) then
  3351. append_attribute(DW_AT_APPLE_runtime_class,DW_FORM_data1,[DW_LANG_ObjC]);
  3352. finish_entry;
  3353. if assigned(def.childof) then
  3354. begin
  3355. append_entry(DW_TAG_inheritance,false,[
  3356. DW_AT_accessibility,DW_FORM_data1,DW_ACCESS_public,
  3357. DW_AT_data_member_location,DW_FORM_block1,1+lengthuleb128(0)
  3358. ]);
  3359. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_plus_uconst)));
  3360. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_uleb128bit(0));
  3361. if (def.childof.dbg_state=dbg_state_unused) then
  3362. def.childof.dbg_state:=dbg_state_used;
  3363. if is_implicit_pointer_object_type(def) then
  3364. append_labelentry_ref(DW_AT_type,def_dwarf_class_struct_lab(def.childof))
  3365. else
  3366. append_labelentry_ref(DW_AT_type,def_dwarf_lab(def.childof));
  3367. finish_entry;
  3368. end;
  3369. if (oo_has_vmt in def.objectoptions) and
  3370. (not assigned(def.childof) or
  3371. not(oo_has_vmt in def.childof.objectoptions)) then
  3372. begin
  3373. { vmt field }
  3374. append_entry(DW_TAG_member,false,[
  3375. DW_AT_artificial,DW_FORM_flag,true,
  3376. DW_AT_name,DW_FORM_string,'_vptr$'+def.objname^+#0,
  3377. DW_AT_data_member_location,DW_FORM_block1,1+lengthuleb128(def.vmt_offset)
  3378. ]);
  3379. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_plus_uconst)));
  3380. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_uleb128bit(def.vmt_offset));
  3381. { should be changed into a pointer to a function returning an }
  3382. { int and with TAG_unspecified_parameters }
  3383. if (voidpointertype.dbg_state=dbg_state_unused) then
  3384. voidpointertype.dbg_state:=dbg_state_used;
  3385. append_labelentry_ref(DW_AT_type,def_dwarf_lab(voidpointertype));
  3386. finish_entry;
  3387. end;
  3388. def.symtable.symList.ForEachCall(@enum_membersyms_callback,nil);
  3389. { Write the methods in the scope of the class/object, except for Objective-C. }
  3390. if is_objc_class_or_protocol(def) then
  3391. finish_children;
  3392. { don't write procdefs of externally defined classes, gcc doesn't
  3393. either (info is probably gotten from ObjC runtime) }
  3394. if not(oo_is_external in def.objectoptions) then
  3395. write_symtable_procdefs(current_asmdata.asmlists[al_dwarf_info],def.symtable);
  3396. if not is_objc_class_or_protocol(def) then
  3397. finish_children;
  3398. end;
  3399. procedure TDebugInfoDwarf2.appenddef_object(list:TAsmList;def: tobjectdef);
  3400. begin
  3401. case def.objecttype of
  3402. odt_cppclass,
  3403. odt_object:
  3404. append_object_struct(def,false,def.objname);
  3405. odt_interfacecom,
  3406. odt_interfacecorba,
  3407. odt_dispinterface,
  3408. odt_helper,
  3409. odt_class:
  3410. begin
  3411. { implicit pointer }
  3412. append_entry(DW_TAG_pointer_type,false,[]);
  3413. append_labelentry_ref(DW_AT_type,def_dwarf_class_struct_lab(def));
  3414. finish_entry;
  3415. append_object_struct(def,true,def.objname);
  3416. end;
  3417. odt_objcclass:
  3418. { Objective-C class: same as regular class, except for
  3419. a) Apple-specific tag that identifies it as an Objective-C class
  3420. b) use extname^ instead of objname
  3421. }
  3422. append_object_struct(def,true,def.objextname);
  3423. odt_objcprotocol:
  3424. begin
  3425. append_entry(DW_TAG_pointer_type,false,[]);
  3426. append_labelentry_ref(DW_AT_type,def_dwarf_lab(voidpointertype));
  3427. finish_entry;
  3428. end;
  3429. else
  3430. internalerror(200602041);
  3431. end;
  3432. end;
  3433. procedure TDebugInfoDwarf2.appenddef_set_intern(list:TAsmList;def: tsetdef; force_tag_set: boolean);
  3434. var
  3435. lab: tasmlabel;
  3436. begin
  3437. if force_tag_set or
  3438. (ds_dwarf_sets in current_settings.debugswitches) then
  3439. begin
  3440. { current (20070704 -- patch was committed on 20060513) gdb cvs supports set types }
  3441. if assigned(def.typesym) then
  3442. append_entry(DW_TAG_set_type,false,[
  3443. DW_AT_name,DW_FORM_string,symname(def.typesym, false)+#0,
  3444. DW_AT_byte_size,DW_FORM_data2,def.size
  3445. ])
  3446. else
  3447. append_entry(DW_TAG_set_type,false,[
  3448. DW_AT_byte_size,DW_FORM_data2,def.size
  3449. ]);
  3450. if assigned(def.elementdef) then
  3451. begin
  3452. if not(tf_dwarf_only_local_labels in target_info.flags) then
  3453. current_asmdata.getglobaldatalabel(lab)
  3454. else
  3455. current_asmdata.getaddrlabel(lab);
  3456. append_labelentry_ref(DW_AT_type,lab);
  3457. finish_entry;
  3458. if lab.bind=AB_GLOBAL then
  3459. current_asmdata.asmlists[al_dwarf_info].concat(tai_symbol.create_global(lab,0))
  3460. else
  3461. current_asmdata.asmlists[al_dwarf_info].concat(tai_symbol.create(lab,0));
  3462. { Sets of e.g. [1..5] are actually stored as a set of [0..7],
  3463. so write the exact boundaries of the set here. Let's hope no
  3464. debugger ever rejects this because this "subrange" type can
  3465. actually have a larger range than the original one. }
  3466. append_entry(DW_TAG_subrange_type,false,[
  3467. DW_AT_lower_bound,DW_FORM_sdata,def.setbase,
  3468. DW_AT_upper_bound,DW_FORM_sdata,get_max_value(def.elementdef).svalue
  3469. ]);
  3470. append_labelentry_ref(DW_AT_type,def_dwarf_lab(def.elementdef))
  3471. end
  3472. end
  3473. else
  3474. begin
  3475. { gdb versions which don't support sets refuse to load the debug }
  3476. { info of modules that contain set tags }
  3477. if assigned(def.typesym) then
  3478. append_entry(DW_TAG_base_type,false,[
  3479. DW_AT_name,DW_FORM_string,symname(def.typesym, false)+#0,
  3480. DW_AT_encoding,DW_FORM_data1,DW_ATE_unsigned,
  3481. DW_AT_byte_size,DW_FORM_data2,def.size
  3482. ])
  3483. else
  3484. append_entry(DW_TAG_base_type,false,[
  3485. DW_AT_encoding,DW_FORM_data1,DW_ATE_unsigned,
  3486. DW_AT_byte_size,DW_FORM_data2,def.size
  3487. ]);
  3488. end;
  3489. finish_entry;
  3490. end;
  3491. procedure TDebugInfoDwarf2.appenddef_set(list:TAsmList;def: tsetdef);
  3492. begin
  3493. appenddef_set_intern(list,def,false);
  3494. end;
  3495. procedure TDebugInfoDwarf2.appenddef_undefined(list:TAsmList;def: tundefineddef);
  3496. begin
  3497. { gdb 6.4 doesn't support DW_TAG_unspecified_type so we
  3498. replace it with a unsigned type with size 0 (FK)
  3499. }
  3500. append_entry(DW_TAG_base_type,false,[
  3501. DW_AT_name,DW_FORM_string,'FormalDef'#0,
  3502. DW_AT_encoding,DW_FORM_data1,DW_ATE_unsigned,
  3503. DW_AT_byte_size,DW_FORM_data1,0
  3504. ]);
  3505. finish_entry;
  3506. end;
  3507. procedure TDebugInfoDwarf2.appenddef_variant(list:TAsmList;def: tvariantdef);
  3508. begin
  3509. { variants aren't known to dwarf2 but writting tvardata should be enough }
  3510. if assigned(vardatadef) then
  3511. appenddef_record_named(list,trecorddef(vardatadef),'Variant');
  3512. end;
  3513. function TDebugInfoDwarf2.dwarf_version: Word;
  3514. begin
  3515. Result:=2;
  3516. end;
  3517. {****************************************************************************
  3518. TDebugInfoDwarf3
  3519. ****************************************************************************}
  3520. procedure TDebugInfoDwarf3.append_labelentry_addr_ref(sym : tasmsymbol);
  3521. begin
  3522. AddConstToAbbrev(ord(DW_FORM_ref_addr));
  3523. { Since Dwarf 3 the length of a DW_FORM_ref_addr entry is not dependent on the pointer size of the
  3524. target platform, but on the used Dwarf-format (32 bit or 64 bit) for the current compilation section. }
  3525. if use_64bit_headers then
  3526. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.Create_type_sym(aitconst_64bit_unaligned,sym))
  3527. else
  3528. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.Create_type_sym(aitconst_32bit_unaligned,sym));
  3529. end;
  3530. procedure tdebuginfodwarf3.appenddef_array(list: tasmlist; def: tarraydef);
  3531. begin
  3532. if not is_dynamic_array(def) then
  3533. begin
  3534. inherited appenddef_array(list,def);
  3535. exit;
  3536. end;
  3537. if assigned(def.typesym) then
  3538. append_entry(DW_TAG_array_type,true,[
  3539. DW_AT_name,DW_FORM_string,symname(def.typesym, false)+#0,
  3540. DW_AT_byte_stride,DW_FORM_udata,def.elesize,
  3541. DW_AT_data_location,DW_FORM_block1,2
  3542. ])
  3543. else
  3544. append_entry(DW_TAG_array_type,true,[
  3545. DW_AT_byte_stride,DW_FORM_udata,def.elesize,
  3546. DW_AT_data_location,DW_FORM_block1,2
  3547. ]);
  3548. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_push_object_address)));
  3549. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_deref)));
  3550. append_labelentry_ref(DW_AT_type,def_dwarf_lab(def.elementdef));
  3551. finish_entry;
  3552. { to simplify things, we don't write a multidimensional array here }
  3553. append_entry(DW_TAG_subrange_type,false,[
  3554. DW_AT_lower_bound,DW_FORM_udata,0,
  3555. DW_AT_upper_bound,DW_FORM_block1,15
  3556. ]);
  3557. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_push_object_address)));
  3558. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_deref)));
  3559. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_dup)));
  3560. { pointer = nil? }
  3561. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_bra)));
  3562. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_16bit_unaligned(5));
  3563. { yes -> length = 0 }
  3564. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_const1s)));
  3565. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(byte(-1)));
  3566. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_skip)));
  3567. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_16bit_unaligned(3));
  3568. { no -> load length }
  3569. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_lit0)+sizesinttype.size));
  3570. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_minus)));
  3571. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_deref)));
  3572. { skip to past end is not allowed, thus use a nop here }
  3573. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_nop)));
  3574. append_labelentry_ref(DW_AT_type,def_dwarf_lab(def.rangedef));
  3575. finish_entry;
  3576. finish_children;
  3577. end;
  3578. procedure tdebuginfodwarf3.appenddef_string(list: tasmlist; def: tstringdef);
  3579. procedure addstringdef(const name: shortstring; chardef: tdef; deref: boolean; lensize: aint);
  3580. var
  3581. upperopcodes: longint;
  3582. begin
  3583. { deref=true -> ansi/unicde/widestring; deref = false -> short/longstring }
  3584. if assigned(def.typesym) then
  3585. append_entry(DW_TAG_array_type,true,[
  3586. DW_AT_name,DW_FORM_string,name+#0,
  3587. DW_AT_data_location,DW_FORM_block1,2+ord(not(deref))
  3588. ])
  3589. else
  3590. append_entry(DW_TAG_array_type,true,[
  3591. DW_AT_data_location,DW_FORM_block1,2+ord(not(deref))
  3592. ]);
  3593. { in all cases we start with the address of the string }
  3594. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_push_object_address)));
  3595. if deref then
  3596. begin
  3597. { ansi/unicode/widestring -> dereference the address of the string, and then
  3598. we point to address of the string
  3599. }
  3600. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_deref)));
  3601. end
  3602. else
  3603. begin
  3604. { shortstring characters begin at string[1], so add one to the string's address }
  3605. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_lit0)+lensize));
  3606. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_plus)))
  3607. end;
  3608. { reference to the element type of the string }
  3609. append_labelentry_ref(DW_AT_type,def_dwarf_lab(chardef));
  3610. finish_entry;
  3611. { now the information about the length of the string }
  3612. if deref then
  3613. begin
  3614. if not (is_widestring(def) and (tf_winlikewidestring in target_info.flags)) then
  3615. upperopcodes:=14
  3616. else
  3617. upperopcodes:=17;
  3618. { lower bound is always 1, upper bound (length) needs to be calculated }
  3619. append_entry(DW_TAG_subrange_type,false,[
  3620. DW_AT_lower_bound,DW_FORM_udata,1,
  3621. DW_AT_upper_bound,DW_FORM_block1,upperopcodes
  3622. ]);
  3623. { high(string) is stored sizeof(sizeint) bytes before the string data }
  3624. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_push_object_address)));
  3625. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_deref)));
  3626. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_dup)));
  3627. { pointer = nil? }
  3628. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_bra)));
  3629. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_16bit_unaligned(4));
  3630. { yes -> length = 0 }
  3631. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_lit0)));
  3632. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_skip)));
  3633. if upperopcodes=17 then
  3634. { skip the extra deref_size argument and the division by two of the length }
  3635. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_16bit_unaligned(6))
  3636. else
  3637. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_16bit_unaligned(3));
  3638. { no -> load length }
  3639. if upperopcodes=17 then
  3640. { for Windows WideString the size is always a DWORD }
  3641. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_lit4)))
  3642. else
  3643. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_lit0)+sizesinttype.size));
  3644. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_minus)));
  3645. if upperopcodes=17 then
  3646. begin
  3647. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_deref_size)));
  3648. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(4));
  3649. end
  3650. else
  3651. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_deref)));
  3652. { for widestrings, the length is specified in bytes, so divide by two }
  3653. if (upperopcodes=17) then
  3654. begin
  3655. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_lit1)));
  3656. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_shr)));
  3657. end;
  3658. { skip to past end is not allowed, thus use a nop here }
  3659. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_nop)));
  3660. end
  3661. else
  3662. begin
  3663. append_entry(DW_TAG_subrange_type,false,[
  3664. DW_AT_lower_bound,DW_FORM_udata,1,
  3665. DW_AT_upper_bound,DW_FORM_block1,3
  3666. ]);
  3667. { for shortstrings, the length is the first byte of the string }
  3668. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_push_object_address)));
  3669. { load 1 byte }
  3670. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_deref_size)));
  3671. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(lensize));
  3672. end;
  3673. finish_entry;
  3674. finish_children;
  3675. end;
  3676. begin
  3677. if (ds_dwarf_cpp in current_settings.debugswitches) then
  3678. begin
  3679. // At least LLDB 6.0.0 does not like this implementation of string types.
  3680. // Call the inherited DWARF 2 implementation, which works fine.
  3681. inherited;
  3682. exit;
  3683. end;
  3684. case def.stringtype of
  3685. st_shortstring:
  3686. begin
  3687. addstringdef('ShortString',cansichartype,false,1);
  3688. end;
  3689. st_longstring:
  3690. begin
  3691. {$ifdef cpu64bitaddr}
  3692. addstringdef('LongString',cansichartype,false,8);
  3693. {$else cpu64bitaddr}
  3694. addstringdef('LongString',cansichartype,false,4);
  3695. {$endif cpu64bitaddr}
  3696. end;
  3697. st_ansistring:
  3698. begin
  3699. addstringdef('AnsiString',cansichartype,true,-1);
  3700. end;
  3701. st_unicodestring:
  3702. begin
  3703. addstringdef('UnicodeString',cwidechartype,true,-1);
  3704. end;
  3705. st_widestring:
  3706. begin
  3707. addstringdef('WideString',cwidechartype,true,-1)
  3708. end;
  3709. end;
  3710. end;
  3711. procedure TDebugInfoDwarf3.appenddef_file(list:TAsmList;def: tfiledef);
  3712. begin
  3713. if assigned(def.typesym) then
  3714. append_entry(DW_TAG_file_type,false,[
  3715. DW_AT_name,DW_FORM_string,symname(def.typesym, false)+#0,
  3716. DW_AT_byte_size,DW_FORM_data2,def.size
  3717. ])
  3718. else
  3719. append_entry(DW_TAG_file_type,false,[
  3720. DW_AT_byte_size,DW_FORM_data2,def.size
  3721. ]);
  3722. if tfiledef(def).filetyp=ft_typed then
  3723. append_labelentry_ref(DW_AT_type,def_dwarf_lab(tfiledef(def).typedfiledef));
  3724. finish_entry;
  3725. end;
  3726. procedure TDebugInfoDwarf3.appenddef_formal(list:TAsmList;def: tformaldef);
  3727. begin
  3728. if (ds_dwarf_cpp in current_settings.debugswitches) then
  3729. begin
  3730. // Do not use DW_TAG_unspecified_type for C++ simulation.
  3731. // At least LLDB 3.9.0 crashes in such case.
  3732. // Call the inherited DWARF 2 implementation, which works fine.
  3733. inherited;
  3734. exit;
  3735. end;
  3736. append_entry(DW_TAG_unspecified_type,false,[]);
  3737. finish_entry;
  3738. end;
  3739. procedure TDebugInfoDwarf3.appenddef_object(list:TAsmList;def: tobjectdef);
  3740. procedure dostruct(tag: tdwarf_tag);
  3741. begin
  3742. if assigned(def.objname) then
  3743. append_entry(tag,true,[
  3744. DW_AT_name,DW_FORM_string,def.objrealname^+#0
  3745. ])
  3746. else
  3747. append_entry(DW_TAG_structure_type,true,[]);
  3748. append_attribute(DW_AT_byte_size,DW_FORM_udata,[tobjectsymtable(def.symtable).datasize]);
  3749. { 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,
  3750. see issue #36017 }
  3751. if not(is_object(def) or is_cppclass(def)) then
  3752. begin
  3753. { The pointer to the class-structure is hidden. The debug-information
  3754. does not contain an implicit pointer, but the data-adress is dereferenced here.
  3755. In case of a nil-pointer, report the class as being unallocated.
  3756. }
  3757. append_block1(DW_AT_allocated,2);
  3758. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_push_object_address)));
  3759. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_deref)));
  3760. append_block1(DW_AT_data_location,2);
  3761. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_push_object_address)));
  3762. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_deref)));
  3763. end;
  3764. finish_entry;
  3765. end;
  3766. procedure doimplicitpointer;
  3767. var
  3768. obj : tasmlabel;
  3769. begin
  3770. if not(tf_dwarf_only_local_labels in target_info.flags) then
  3771. current_asmdata.getglobaldatalabel(obj)
  3772. else
  3773. current_asmdata.getaddrlabel(obj);
  3774. { implicit pointer }
  3775. append_entry(DW_TAG_pointer_type,false,[]);
  3776. append_labelentry_ref(DW_AT_type,obj);
  3777. finish_entry;
  3778. current_asmdata.asmlists[al_dwarf_info].concat(tai_symbol.create(obj,0));
  3779. end;
  3780. procedure doparent(isinterface: boolean);
  3781. begin
  3782. if not assigned(def.childof) then
  3783. exit;
  3784. if isinterface then
  3785. begin
  3786. append_entry(DW_TAG_inheritance,false,[]);
  3787. end
  3788. else
  3789. begin
  3790. append_entry(DW_TAG_inheritance,false,[
  3791. DW_AT_accessibility,DW_FORM_data1,DW_ACCESS_public,
  3792. DW_AT_data_member_location,DW_FORM_block1,1+lengthuleb128(0)
  3793. ]);
  3794. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_plus_uconst)));
  3795. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_uleb128bit(0));
  3796. end;
  3797. append_labelentry_ref(DW_AT_type,def_dwarf_lab(def.childof));
  3798. finish_entry;
  3799. end;
  3800. var
  3801. n: integer;
  3802. begin
  3803. case def.objecttype of
  3804. odt_objcclass,
  3805. odt_objcprotocol:
  3806. begin
  3807. inherited;
  3808. exit
  3809. end;
  3810. odt_cppclass,
  3811. odt_object:
  3812. begin
  3813. append_object_struct(def,false,def.objname);
  3814. exit;
  3815. end;
  3816. odt_interfacecom,
  3817. odt_interfacecorba,
  3818. odt_dispinterface:
  3819. begin
  3820. dostruct(DW_TAG_interface_type);
  3821. doparent(true);
  3822. end;
  3823. odt_helper,
  3824. odt_class:
  3825. begin
  3826. append_entry(DW_TAG_pointer_type,false,[]);
  3827. append_labelentry_ref(DW_AT_type,def_dwarf_class_struct_lab(def));
  3828. finish_entry;
  3829. append_object_struct(def,true,def.objrealname);
  3830. Exit;
  3831. end;
  3832. else
  3833. internalerror(200609171);
  3834. end;
  3835. { add implemented interfaces }
  3836. if assigned(def.ImplementedInterfaces) then
  3837. for n := 0 to def.ImplementedInterfaces.count-1 do
  3838. begin
  3839. append_entry(DW_TAG_inheritance,false,[]);
  3840. append_labelentry_ref(DW_AT_type,def_dwarf_lab(TImplementedInterface(def.ImplementedInterfaces[n]).IntfDef));
  3841. finish_entry;
  3842. end;
  3843. { add members }
  3844. def.symtable.symList.ForEachCall(@enum_membersyms_callback,nil);
  3845. finish_children;
  3846. end;
  3847. procedure TDebugInfoDwarf3.appenddef_set(list:TAsmList;def: tsetdef);
  3848. begin
  3849. appenddef_set_intern(list,def,true);
  3850. end;
  3851. procedure TDebugInfoDwarf3.appenddef_undefined(list:TAsmList;def: tundefineddef);
  3852. begin
  3853. { ??? can a undefined def have a typename ? }
  3854. if assigned(def.typesym) then
  3855. append_entry(DW_TAG_unspecified_type,false,[
  3856. DW_AT_name,DW_FORM_string,symname(def.typesym, false)+#0
  3857. ])
  3858. else
  3859. append_entry(DW_TAG_unspecified_type,false,[
  3860. ]);
  3861. finish_entry;
  3862. end;
  3863. procedure TDebugInfoDwarf3.appenddef_variant(list:TAsmList;def: tvariantdef);
  3864. const
  3865. VARIANTS: array[1..27] of record
  3866. Value: Word;
  3867. Name: String;
  3868. { some fields are only supported by some features }
  3869. features : tfeatures
  3870. end = (
  3871. (value:0; name:'';features: []),
  3872. (value:1; name:'';features: []),
  3873. (value:2; name:'VSMALLINT';features: []),
  3874. (value:3; name:'VINTEGER';features: []),
  3875. (value:4; name:'VSINGLE';features: [f_softfpu]),
  3876. (value:5; name:'VDOUBLE';features: [f_softfpu]),
  3877. (value:6; name:'VCURRENCY';features: [f_softfpu]),
  3878. (value:7; name:'VDATE';features: [f_softfpu]),
  3879. (value:8; name:'VOLESTR';features: []),
  3880. (value:9; name:'VDISPATCH';features: []),
  3881. (value:10; name:'VERROR';features: []),
  3882. (value:11; name:'VBOOLEAN';features: []),
  3883. (value:12; name:'';features: []),
  3884. (value:13; name:'VUNKNOWN';features: []),
  3885. (value:14; name:'';features: []),
  3886. (value:16; name:'VSHORTINT';features: []),
  3887. (value:17; name:'VBYTE';features: []),
  3888. (value:18; name:'VWORD';features: []),
  3889. (value:19; name:'VLONGWORD';features: []),
  3890. (value:20; name:'VINT64';features: []),
  3891. (value:21; name:'VQWORD';features: []),
  3892. (value:36; name:'VRECORD';features: []),
  3893. (value:$48; name:'';features: []),
  3894. (value:$100; name:'VSTRING';features: []),
  3895. (value:$101; name:'VANY';features: []),
  3896. (value:$2000; name:'VARRAY';features: []),
  3897. (value:$4000; name:'VPOINTER';features: [])
  3898. );
  3899. var
  3900. fs: tfieldvarsym;
  3901. lbl: tasmlabel;
  3902. idx: integer;
  3903. begin
  3904. { it could be done with DW_TAG_variant for the union part (if that info was available)
  3905. now we do it manually for variants (MWE) }
  3906. { struct }
  3907. append_entry(DW_TAG_structure_type,true,[
  3908. DW_AT_name,DW_FORM_string,'Variant'#0,
  3909. DW_AT_byte_size,DW_FORM_udata,vardatadef.size
  3910. ]);
  3911. finish_entry;
  3912. append_entry(DW_TAG_variant_part,true,[
  3913. ]);
  3914. current_asmdata.getaddrlabel(lbl);
  3915. append_labelentry_ref(DW_AT_discr,lbl);
  3916. finish_entry;
  3917. { discriminant }
  3918. fs := tfieldvarsym(vardatadef.symtable.Find('VTYPE'));
  3919. if (fs = nil) or (fs.typ <> fieldvarsym) then
  3920. internalerror(200609271);
  3921. current_asmdata.asmlists[al_dwarf_info].concat(tai_symbol.create(lbl,0));
  3922. appendsym_fieldvar(list,fs);
  3923. { variants }
  3924. for idx := Low(VARIANTS) to High(VARIANTS) do
  3925. begin
  3926. if (features*VARIANTS[idx].features)=VARIANTS[idx].features then
  3927. begin
  3928. append_entry(DW_TAG_variant,true,[
  3929. DW_AT_discr_value,DW_FORM_udata,VARIANTS[idx].value
  3930. ]);
  3931. finish_entry;
  3932. if VARIANTS[idx].name <> '' then
  3933. begin
  3934. fs := tfieldvarsym(vardatadef.symtable.Find(VARIANTS[idx].name));
  3935. if (fs = nil) or (fs.typ <> fieldvarsym) then
  3936. internalerror(2006092702+idx);
  3937. appendsym_fieldvar(list,fs);
  3938. end;
  3939. finish_children; { variant }
  3940. end;
  3941. end;
  3942. finish_children; { variant part }
  3943. finish_children; { struct }
  3944. end;
  3945. function TDebugInfoDwarf3.dwarf_version: Word;
  3946. begin
  3947. Result:=3;
  3948. end;
  3949. function TDebugInfoDwarf3.symdebugname(sym: tsym): String;
  3950. begin
  3951. Result:=sym.realname;
  3952. end;
  3953. { TDebugInfoDwarf4 }
  3954. function TDebugInfoDwarf4.dwarf_version: Word;
  3955. begin
  3956. Result:=4;
  3957. end;
  3958. {****************************************************************************
  3959. ****************************************************************************}
  3960. const
  3961. dbg_dwarf2_info : tdbginfo =
  3962. (
  3963. id : dbg_dwarf2;
  3964. idtxt : 'DWARF2';
  3965. );
  3966. dbg_dwarf3_info : tdbginfo =
  3967. (
  3968. id : dbg_dwarf3;
  3969. idtxt : 'DWARF3';
  3970. );
  3971. dbg_dwarf4_info : tdbginfo =
  3972. (
  3973. id : dbg_dwarf4;
  3974. idtxt : 'DWARF4';
  3975. );
  3976. initialization
  3977. RegisterDebugInfo(dbg_dwarf2_info,TDebugInfoDwarf2);
  3978. RegisterDebugInfo(dbg_dwarf3_info,TDebugInfoDwarf3);
  3979. RegisterDebugInfo(dbg_dwarf4_info,TDebugInfoDwarf4);
  3980. end.