DbgModule.cpp 200 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549
  1. #pragma warning(push)
  2. #pragma warning(disable:4146)
  3. #pragma warning(disable:4996)
  4. #pragma warning(disable:4800)
  5. #pragma warning(disable:4244)
  6. #include "DbgModule.h"
  7. #include "DWARFInfo.h"
  8. #include <windows.h>
  9. #include <stddef.h>
  10. #include <stdio.h>
  11. #include <string>
  12. #include <inttypes.h>
  13. #include <assert.h>
  14. #include <vector>
  15. #include "WinDebugger.h"
  16. #include "DebugManager.h"
  17. #include "DebugTarget.h"
  18. #include "COFFData.h"
  19. #include "Compiler/BfDemangler.h"
  20. #include "BeefySysLib/util/Hash.h"
  21. #include "BeefySysLib/util/BeefPerf.h"
  22. #include "DbgSymSrv.h"
  23. #include "MiniDumpDebugger.h"
  24. #pragma warning(pop)
  25. #pragma warning(disable:4996)
  26. #include "BeefySysLib/util/AllocDebug.h"
  27. USING_NS_BF_DBG;
  28. void SetBreakpoint(int64_t address);
  29. NS_BF_DBG_BEGIN
  30. #ifdef BF_DBG_32
  31. typedef PEOptionalHeader32 PEOptionalHeader;
  32. typedef PE_NTHeaders32 PE_NTHeaders;
  33. #else
  34. typedef PEOptionalHeader64 PEOptionalHeader;
  35. typedef PE_NTHeaders64 PE_NTHeaders;
  36. #endif
  37. #define GET(T) *((T*)(data += sizeof(T)) - 1)
  38. #define GET_FROM(ptr, T) *((T*)(ptr += sizeof(T)) - 1)
  39. //////////////////////////////////////////////////////////////////////////
  40. DbgCompileUnit::DbgCompileUnit(DbgModule* dbgModule)
  41. {
  42. mDbgModule = dbgModule;
  43. mLanguage = DbgLanguage_Unknown;
  44. mGlobalBlock = mDbgModule->mAlloc.Alloc<DbgBlock>();
  45. mGlobalType = mDbgModule->mAlloc.Alloc<DbgType>();
  46. mGlobalType->mTypeCode = DbgType_Root;
  47. mGlobalType->mPriority = DbgTypePriority_Primary_Explicit;
  48. mGlobalType->mCompileUnit = this;
  49. mLowPC = (addr_target)-1;
  50. mHighPC = 0;
  51. //mDoPrimaryRemapping = true;
  52. mNeedsLineDataFixup = true;
  53. mWasHotReplaced = false;
  54. mIsMaster = false;
  55. }
  56. //////////////////////////////////////////////////////////////////////////
  57. addr_target DbgLineDataEx::GetAddress()
  58. {
  59. return mSubprogram->GetLineAddr(*mLineData);
  60. }
  61. DbgSrcFile* DbgLineDataEx::GetSrcFile()
  62. {
  63. auto inlineRoot = mSubprogram->GetRootInlineParent();
  64. return inlineRoot->mLineInfo->mContexts[mLineData->mCtxIdx].mSrcFile;
  65. }
  66. addr_target DbgSubprogram::GetLineAddr(const DbgLineData& lineData)
  67. {
  68. return (addr_target)(lineData.mRelAddress + mCompileUnit->mDbgModule->mImageBase);
  69. }
  70. DbgSubprogram* DbgSubprogram::GetLineInlinee(const DbgLineData& lineData)
  71. {
  72. auto inlineRoot = GetRootInlineParent();
  73. return inlineRoot->mLineInfo->mContexts[lineData.mCtxIdx].mInlinee;
  74. }
  75. DbgSrcFile* DbgSubprogram::GetLineSrcFile(const DbgLineData& lineData)
  76. {
  77. auto inlineRoot = GetRootInlineParent();
  78. return inlineRoot->mLineInfo->mContexts[lineData.mCtxIdx].mSrcFile;
  79. }
  80. bool DbgSubprogram::HasValidLines()
  81. {
  82. auto inlineRoot = GetRootInlineParent();
  83. for (int lineIdx = 0; lineIdx < (int)inlineRoot->mLineInfo->mLines.size(); lineIdx++)
  84. {
  85. auto& lineInfo = inlineRoot->mLineInfo->mLines[lineIdx];
  86. if (lineInfo.mColumn >= 0)
  87. return true;
  88. }
  89. return false;
  90. }
  91. void DbgSubprogram::PopulateSubprogram()
  92. {
  93. if (mDeferredInternalsSize == 0)
  94. return;
  95. mCompileUnit->mDbgModule->PopulateSubprogram(this);
  96. }
  97. //////////////////////////////////////////////////////////////////////////
  98. DbgLineDataBuilder::DbgLineDataBuilder(DbgModule* dbgModule)
  99. {
  100. mDbgModule = dbgModule;
  101. mCurSubprogram = NULL;
  102. mCurRecord = NULL;
  103. }
  104. DbgLineData* DbgLineDataBuilder::Add(DbgCompileUnit* compileUnit, DbgLineData& lineData, DbgSrcFile* srcFile, DbgSubprogram* inlinee)
  105. {
  106. addr_target address = (addr_target)(lineData.mRelAddress + mDbgModule->mImageBase);
  107. if ((compileUnit->mLowPC != (addr_target)-1) && ((address < (addr_target)compileUnit->mLowPC) || (address >= (addr_target)compileUnit->mHighPC)))
  108. return NULL;
  109. if ((mCurSubprogram == NULL) || (address < mCurSubprogram->mBlock.mLowPC) || (address >= mCurSubprogram->mBlock.mHighPC))
  110. {
  111. DbgSubprogramMapEntry* mapEntry = mDbgModule->mDebugTarget->mSubprogramMap.Get(address, DBG_MAX_LOOKBACK);
  112. if (mapEntry != NULL)
  113. {
  114. mCurSubprogram = mapEntry->mEntry;
  115. if (address > mCurSubprogram->mBlock.mHighPC)
  116. mCurSubprogram = NULL;
  117. if (mCurSubprogram != NULL)
  118. {
  119. SubprogramRecord** recordPtr = NULL;
  120. if (mRecords.TryAdd(mCurSubprogram, NULL, &recordPtr))
  121. {
  122. // It's not too expensive to over-reserve here, because these are just temporary structures that get copied
  123. // exactly sized when we Commit
  124. mCurRecord = mAlloc.Alloc<SubprogramRecord>();
  125. *recordPtr = mCurRecord;
  126. mCurRecord->mContexts.mAlloc = &mAlloc;
  127. mCurRecord->mContexts.Reserve(16);
  128. mCurRecord->mLines.mAlloc = &mAlloc;
  129. mCurRecord->mLines.Reserve(128);
  130. mCurRecord->mCurContext = -1;
  131. mCurRecord->mHasInlinees = false;
  132. }
  133. else
  134. mCurRecord = *recordPtr;
  135. }
  136. else
  137. mCurRecord = NULL;
  138. }
  139. }
  140. if (mCurSubprogram == NULL)
  141. return NULL;
  142. bool needsNewCtx = false;
  143. if (mCurRecord->mCurContext == -1)
  144. {
  145. needsNewCtx = true;
  146. }
  147. else
  148. {
  149. auto& curContext = mCurRecord->mContexts[mCurRecord->mCurContext];
  150. if ((curContext.mInlinee != inlinee) || (curContext.mSrcFile != srcFile))
  151. {
  152. needsNewCtx = true;
  153. for (int ctxIdx = 0; ctxIdx < (int)mCurRecord->mContexts.size(); ctxIdx++)
  154. {
  155. auto& ctx = mCurRecord->mContexts[ctxIdx];
  156. if ((ctx.mInlinee == inlinee) && (ctx.mSrcFile == srcFile))
  157. {
  158. needsNewCtx = false;
  159. mCurRecord->mCurContext = ctxIdx;
  160. break;
  161. }
  162. }
  163. }
  164. }
  165. if (needsNewCtx)
  166. {
  167. DbgLineInfoCtx ctx;
  168. ctx.mInlinee = inlinee;
  169. ctx.mSrcFile = srcFile;
  170. if (inlinee != NULL)
  171. mCurRecord->mHasInlinees = true;
  172. mCurRecord->mContexts.Add(ctx);
  173. mCurRecord->mCurContext = (int)mCurRecord->mContexts.size() - 1;
  174. }
  175. lineData.mCtxIdx = mCurRecord->mCurContext;
  176. if ((mCurSubprogram->mPrologueSize > 0) && (mCurRecord->mLines.size() == 1) && (inlinee == NULL))
  177. {
  178. auto& firstLine = mCurRecord->mLines[0];
  179. auto dbgStartAddr = firstLine.mRelAddress + mCurSubprogram->mPrologueSize;
  180. if (lineData.mRelAddress != dbgStartAddr)
  181. {
  182. DbgLineData dbgStartLine = firstLine;
  183. dbgStartLine.mRelAddress = dbgStartAddr;
  184. mCurRecord->mLines.Add(dbgStartLine);
  185. }
  186. firstLine.mColumn = -2; // Marker for 'in prologue'
  187. }
  188. if (inlinee != NULL)
  189. {
  190. if (inlinee->mInlineeInfo->mFirstLineData.mRelAddress == 0)
  191. inlinee->mInlineeInfo->mFirstLineData = lineData;
  192. inlinee->mInlineeInfo->mLastLineData = lineData;
  193. }
  194. mCurRecord->mLines.Add(lineData);
  195. return &mCurRecord->mLines.back();
  196. }
  197. void DbgLineDataBuilder::Commit()
  198. {
  199. HashSet<DbgSrcFile*> usedSrcFiles;
  200. for (auto& recordKV : mRecords)
  201. {
  202. auto dbgSubprogram = recordKV.mKey;
  203. auto record = recordKV.mValue;
  204. usedSrcFiles.Clear();
  205. for (auto& ctx : record->mContexts)
  206. {
  207. if (usedSrcFiles.Add(ctx.mSrcFile))
  208. {
  209. ctx.mSrcFile->mLineDataRefs.Add(dbgSubprogram);
  210. }
  211. }
  212. for (int lineIdx = 0; lineIdx < (int)record->mLines.size() - 1; lineIdx++)
  213. {
  214. auto& lineData = record->mLines[lineIdx];
  215. auto& nextLineData = record->mLines[lineIdx + 1];
  216. if ((lineData.mContribSize == 0) && (lineData.mCtxIdx == nextLineData.mCtxIdx))
  217. {
  218. lineData.mContribSize = (uint32)(nextLineData.mRelAddress - lineData.mRelAddress);
  219. }
  220. bool sameInliner = lineData.mCtxIdx == nextLineData.mCtxIdx;
  221. if (!sameInliner)
  222. {
  223. auto ctx = record->mContexts[lineData.mCtxIdx];
  224. auto nextCtx = record->mContexts[lineData.mCtxIdx];
  225. sameInliner = ctx.mInlinee == nextCtx.mInlinee;
  226. }
  227. if ((sameInliner) && (lineData.mRelAddress + lineData.mContribSize < nextLineData.mRelAddress))
  228. {
  229. auto ctx = record->mContexts[lineData.mCtxIdx];
  230. if (ctx.mInlinee != NULL)
  231. ctx.mInlinee->mHasLineAddrGaps = true;
  232. }
  233. }
  234. DbgLineData* lastLine = NULL;
  235. for (int lineIdx = 0; lineIdx < (int)record->mLines.size(); lineIdx++)
  236. {
  237. auto& lineData = record->mLines[lineIdx];
  238. if (lineData.mContribSize == 0)
  239. {
  240. auto ctx = record->mContexts[lineData.mCtxIdx];
  241. if (ctx.mInlinee == NULL)
  242. lastLine = &lineData;
  243. }
  244. }
  245. if (lastLine != NULL)
  246. lastLine->mContribSize = (uint32)(dbgSubprogram->mBlock.mHighPC - (mDbgModule->mImageBase + lastLine->mRelAddress));
  247. BF_ASSERT(dbgSubprogram->mLineInfo == NULL);
  248. dbgSubprogram->mLineInfo = mDbgModule->mAlloc.Alloc<DbgLineInfo>();
  249. dbgSubprogram->mLineInfo->mLines.CopyFrom(&record->mLines[0], (int)record->mLines.size(), mDbgModule->mAlloc);
  250. BfSizedArray<DbgLineInfoCtx> contexts;
  251. contexts.CopyFrom(&record->mContexts[0], (int)record->mContexts.size(), mDbgModule->mAlloc);
  252. dbgSubprogram->mLineInfo->mContexts = contexts.mVals;
  253. dbgSubprogram->mLineInfo->mHasInlinees = record->mHasInlinees;
  254. }
  255. }
  256. //////////////////////////////////////////////////////////////////////////
  257. static const char* DataGetString(const uint8*& data)
  258. {
  259. const char* prevVal = (const char*)data;
  260. while (*data != 0)
  261. data++;
  262. data++;
  263. return prevVal;
  264. }
  265. struct AbstractOriginEntry
  266. {
  267. public:
  268. int mClassType;
  269. DbgDebugData* mDestination;
  270. DbgDebugData* mAbstractOrigin;
  271. private:
  272. AbstractOriginEntry()
  273. {
  274. }
  275. public:
  276. static AbstractOriginEntry Create(int classType, DbgDebugData* destination, DbgDebugData* abstractOrigin)
  277. {
  278. AbstractOriginEntry abstractOriginEntry;
  279. abstractOriginEntry.mClassType = classType;
  280. abstractOriginEntry.mDestination = destination;
  281. abstractOriginEntry.mAbstractOrigin = abstractOrigin;
  282. return abstractOriginEntry;
  283. }
  284. void Replace()
  285. {
  286. if (mClassType == DbgSubprogram::ClassType)
  287. {
  288. DbgSubprogram* destSubprogram = (DbgSubprogram*)mDestination;
  289. DbgSubprogram* originSubprogram = (DbgSubprogram*)mAbstractOrigin;
  290. if (destSubprogram->mName == NULL)
  291. {
  292. destSubprogram->mName = originSubprogram->mName;
  293. destSubprogram->mParentType = originSubprogram->mParentType;
  294. }
  295. destSubprogram->mHasThis = originSubprogram->mHasThis;
  296. if (destSubprogram->mFrameBaseData == NULL)
  297. {
  298. destSubprogram->mFrameBaseData = originSubprogram->mFrameBaseData;
  299. destSubprogram->mFrameBaseLen = originSubprogram->mFrameBaseLen;
  300. }
  301. destSubprogram->mReturnType = originSubprogram->mReturnType;
  302. auto originItr = originSubprogram->mParams.begin();
  303. for (auto destParam : destSubprogram->mParams)
  304. {
  305. DbgVariable* originParam = *originItr;
  306. if (originParam != NULL)
  307. {
  308. if (destParam->mName == NULL)
  309. destParam->mName = originParam->mName;
  310. if (destParam->mType == NULL)
  311. destParam->mType = originParam->mType;
  312. }
  313. ++originItr;
  314. }
  315. //BF_ASSERT(originItr == originSubprogram->mParams.end());
  316. }
  317. else if (mClassType == DbgVariable::ClassType)
  318. {
  319. DbgVariable* destVariable = (DbgVariable*)mDestination;
  320. DbgVariable* originVariable = (DbgVariable*)mAbstractOrigin;
  321. if (destVariable->mName == NULL)
  322. destVariable->mName = originVariable->mName;
  323. if (destVariable->mType == NULL)
  324. destVariable->mType = originVariable->mType;
  325. }
  326. else
  327. {
  328. BF_FATAL("Unhandled");
  329. }
  330. }
  331. };
  332. NS_BF_DBG_END
  333. //////////////////////////////////////////////////////////////////////////
  334. void DbgSubprogram::ToString(StringImpl& str, bool internalName)
  335. {
  336. if ((mInlineeInfo != NULL) && (mInlineeInfo->mInlineeId != 0))
  337. mCompileUnit->mDbgModule->FixupInlinee(this);
  338. PopulateSubprogram();
  339. if (mCheckedKind == BfCheckedKind_Checked)
  340. str += "[Checked] ";
  341. else if (mCheckedKind == BfCheckedKind_Unchecked)
  342. str += "[Unchecked] ";
  343. auto language = GetLanguage();
  344. if (mName == NULL)
  345. {
  346. if (mLinkName[0] == '<')
  347. {
  348. str += mLinkName;
  349. return;
  350. }
  351. str = BfDemangler::Demangle(StringImpl::MakeRef(mLinkName), language);
  352. // Strip off the params since we need to generate those ourselves
  353. int parenPos = (int)str.IndexOf('(');
  354. if (parenPos != -1)
  355. str = str.Substring(0, parenPos);
  356. }
  357. else if ((mHasQualifiedName) && (!internalName))
  358. {
  359. const char* cPtr = mName;
  360. if (strncmp(cPtr, "_bf::", 5) == 0)
  361. {
  362. cPtr += 5;
  363. for ( ; true; cPtr++)
  364. {
  365. char c = *cPtr;
  366. if (c == 0)
  367. break;
  368. if ((c == '_') && (cPtr[-1] == ':'))
  369. {
  370. if (strcmp(cPtr, "__BfCtor") == 0)
  371. {
  372. str += "this";
  373. break;
  374. }
  375. if (strcmp(cPtr, "__BfStaticCtor") == 0)
  376. {
  377. str += "this$static";
  378. break;
  379. }
  380. if (strcmp(cPtr, "__BfCtorClear") == 0)
  381. {
  382. str += "this$clear";
  383. break;
  384. }
  385. }
  386. if ((c == ':') && (cPtr[1] == ':'))
  387. {
  388. str.Append('.');
  389. cPtr++;
  390. }
  391. else
  392. str.Append(c);
  393. }
  394. }
  395. else
  396. str += mName;
  397. }
  398. else
  399. {
  400. if (mParentType != NULL)
  401. {
  402. mParentType->ToString(str, language, true, internalName);
  403. if (!str.empty())
  404. {
  405. if (language == DbgLanguage_Beef)
  406. str += ".";
  407. else
  408. str += "::";
  409. }
  410. }
  411. const char* name = mName;
  412. if (mHasQualifiedName)
  413. {
  414. const char* cPtr = name;
  415. for (; true; cPtr++)
  416. {
  417. char c = *cPtr;
  418. if (c == 0)
  419. break;
  420. if ((c == ':') && (cPtr[1] == ':'))
  421. {
  422. name = cPtr + 2;
  423. }
  424. }
  425. }
  426. if ((language == DbgLanguage_Beef) && (mParentType != NULL) && (mParentType->mTypeName != NULL) && (strcmp(name, mParentType->mTypeName) == 0))
  427. str += "this";
  428. else if ((language == DbgLanguage_Beef) && (name[0] == '~'))
  429. str += "~this";
  430. else if (strncmp(name, "_bf::", 5) == 0)
  431. str += name + 5;
  432. else
  433. {
  434. bool handled = false;
  435. if ((language == DbgLanguage_Beef) && (name[0] == '_'))
  436. {
  437. if (strcmp(name, "__BfCtor") == 0)
  438. {
  439. str += "this";
  440. handled = true;
  441. }
  442. else if (strcmp(name, "__BfStaticCtor") == 0)
  443. {
  444. str += "this";
  445. handled = true;
  446. }
  447. else if (strcmp(name, "__BfCtorClear") == 0)
  448. {
  449. str += "this$clear";
  450. handled = true;
  451. }
  452. }
  453. if (!handled)
  454. str += name;
  455. }
  456. }
  457. //if (mTemplateName != NULL)
  458. //str += mTemplateName;
  459. if (str.empty())
  460. str += "`anon";
  461. if ((str[str.length() - 1] == '!') || (str[0] == '<'))
  462. {
  463. if (language == DbgLanguage_Beef)
  464. {
  465. // It's a mixin - assert that there's no params
  466. //BF_ASSERT(mParams.Size() == 0);
  467. }
  468. //return str;
  469. }
  470. str += "(";
  471. bool showedParam = false;
  472. int i = 0;
  473. for (auto variable : mParams)
  474. {
  475. if ((variable->mName != NULL) && (strcmp(variable->mName, "this") == 0))
  476. continue;
  477. if (showedParam)
  478. str += ", ";
  479. if (variable->mType != NULL)
  480. {
  481. auto varType = variable->mType;
  482. if (varType->mTypeCode == DbgType_Const)
  483. varType = varType->mTypeParam;
  484. if (variable->mSigNoPointer)
  485. {
  486. BF_ASSERT(varType->IsPointer());
  487. varType = varType->mTypeParam;
  488. }
  489. varType->ToString(str, language, false, internalName);
  490. if (variable->mName != NULL)
  491. str += " ";
  492. }
  493. if (variable->mName != NULL)
  494. str += variable->mName;
  495. showedParam = true;
  496. i++;
  497. }
  498. str += ")";
  499. }
  500. String DbgSubprogram::ToString()
  501. {
  502. String str;
  503. ToString(str, false);
  504. return str;
  505. }
  506. // For inlined subprograms, the "root" inliner means the bottom-most non-inlined function. This subprogram contains
  507. // all the line data for it's own non-inlined instructions, PLUS line data for all inlined functions that it calls.
  508. // The inlined functions has empty mLineInfo structures.
  509. //
  510. // When we pass a non-NULL value into inlinedSubprogram, we are requesting to ONLY return lines that were emitted from
  511. // that subprogram (inlined or not).
  512. //
  513. // If we call FindClosestLine on an inlined subprogram, we only want results of functions that are inside or inlined by
  514. // the 'this' subprogram. Thus, we do a "get any line" call on the root inliner and then filter the results based
  515. // on whether they are relevant.
  516. DbgLineData* DbgSubprogram::FindClosestLine(addr_target addr, DbgSubprogram** inlinedSubprogram, DbgSrcFile** srcFile, int* outLineIdx)
  517. {
  518. if (mLineInfo == NULL)
  519. {
  520. if (mInlineeInfo == NULL)
  521. return NULL;
  522. if ((inlinedSubprogram != NULL) && (*inlinedSubprogram != NULL))
  523. {
  524. // Keep explicit inlinee requirement
  525. return mInlineeInfo->mRootInliner->FindClosestLine(addr, inlinedSubprogram, srcFile, outLineIdx);
  526. }
  527. else
  528. {
  529. DbgSubprogram* rootInlinedSubprogram = NULL;
  530. auto result = mInlineeInfo->mRootInliner->FindClosestLine(addr, &rootInlinedSubprogram, srcFile, outLineIdx);
  531. if (result == NULL)
  532. return NULL;
  533. if (rootInlinedSubprogram == NULL) // Do not allow root parent, as we cannot be a parent to the root parent (duh)
  534. return NULL;
  535. // We need to check to see if we are a parent of the found line
  536. auto checkSubprogram = rootInlinedSubprogram;
  537. while ((checkSubprogram != NULL) && (checkSubprogram->mInlineeInfo != NULL))
  538. {
  539. if (checkSubprogram == this)
  540. {
  541. if (inlinedSubprogram != NULL)
  542. *inlinedSubprogram = rootInlinedSubprogram;
  543. return result;
  544. }
  545. checkSubprogram = checkSubprogram->mInlineeInfo->mInlineParent;
  546. }
  547. return NULL;
  548. }
  549. }
  550. // Binary search - lineData is sorted
  551. int first = 0;
  552. int last = (int)mLineInfo->mLines.mSize - 1;
  553. int middle = (first + last) / 2;
  554. int useIdx = -1;
  555. while (first <= last)
  556. {
  557. addr_target midAddr = (addr_target)(mLineInfo->mLines.mVals[middle].mRelAddress + mCompileUnit->mDbgModule->mImageBase);
  558. if (midAddr < addr)
  559. first = middle + 1;
  560. else if (midAddr == addr)
  561. {
  562. useIdx = middle;
  563. break;
  564. }
  565. else
  566. last = middle - 1;
  567. middle = (first + last) / 2;
  568. }
  569. if (useIdx == -1)
  570. useIdx = last;
  571. if (last == -1)
  572. return NULL;
  573. // If we have lines with the same addr, take the more inner one
  574. while (true)
  575. {
  576. auto lineData = &mLineInfo->mLines.mVals[useIdx];
  577. if (useIdx + 1 < mLineInfo->mLines.mSize)
  578. {
  579. auto peekNext = &mLineInfo->mLines.mVals[useIdx + 1];
  580. if (lineData->mRelAddress != peekNext->mRelAddress)
  581. break;
  582. useIdx++;
  583. }
  584. else
  585. {
  586. break;
  587. }
  588. }
  589. while (true)
  590. {
  591. auto lineData = &mLineInfo->mLines.mVals[useIdx];
  592. if (addr < lineData->mRelAddress + lineData->mContribSize + mCompileUnit->mDbgModule->mImageBase)
  593. {
  594. auto& ctx = mLineInfo->mContexts[lineData->mCtxIdx];
  595. if (srcFile != NULL)
  596. *srcFile = ctx.mSrcFile;
  597. if (inlinedSubprogram != NULL)
  598. {
  599. auto subprogram = (ctx.mInlinee != NULL) ? ctx.mInlinee : this;
  600. if (*inlinedSubprogram != NULL)
  601. {
  602. // Strictness check
  603. if (subprogram == *inlinedSubprogram)
  604. {
  605. if (outLineIdx != NULL)
  606. *outLineIdx = useIdx;
  607. return lineData;
  608. }
  609. }
  610. else
  611. {
  612. *inlinedSubprogram = subprogram;
  613. if (outLineIdx != NULL)
  614. *outLineIdx = useIdx;
  615. return lineData;
  616. }
  617. }
  618. else
  619. {
  620. if (outLineIdx != NULL)
  621. *outLineIdx = useIdx;
  622. return lineData;
  623. }
  624. }
  625. // Hope we can find an earlier entry whose "contribution" is still valid
  626. if (--useIdx < 0)
  627. break;
  628. }
  629. return NULL;
  630. }
  631. DbgType* DbgSubprogram::GetParent()
  632. {
  633. if ((mParentType == NULL) && (mCompileUnit != NULL))
  634. mCompileUnit->mDbgModule->MapCompileUnitMethods(mCompileUnit);
  635. return mParentType;
  636. }
  637. DbgType* DbgSubprogram::GetTargetType()
  638. {
  639. if (!mHasThis)
  640. return mParentType;
  641. auto thisType = mParams.mHead->mType;
  642. if (thisType == NULL)
  643. return mParentType;
  644. if (thisType->IsPointer())
  645. return thisType->mTypeParam;
  646. return thisType;
  647. }
  648. DbgLanguage DbgSubprogram::GetLanguage()
  649. {
  650. if (mParentType != NULL)
  651. return mParentType->GetLanguage();
  652. if (mCompileUnit->mLanguage != DbgLanguage_Unknown)
  653. return mCompileUnit->mLanguage;
  654. return DbgLanguage_C; // Parent type would have been set for Beef, so it must be C
  655. }
  656. bool DbgSubprogram::Equals(DbgSubprogram* checkMethod, bool allowThisMismatch)
  657. {
  658. if ((mLinkName != NULL) && (checkMethod->mLinkName != NULL))
  659. {
  660. return strcmp(mLinkName, checkMethod->mLinkName) == 0;
  661. }
  662. if (strcmp(mName, checkMethod->mName) != 0)
  663. return false;
  664. if (mHasThis != checkMethod->mHasThis)
  665. return false;
  666. int paramIdx = 0;
  667. auto param = mParams.mHead;
  668. auto checkParam = checkMethod->mParams.mHead;
  669. while ((param != NULL) && (checkParam != NULL))
  670. {
  671. if ((paramIdx == 0) && (allowThisMismatch))
  672. {
  673. // Allow
  674. }
  675. else if ((param->mType != checkParam->mType) && (!param->mType->Equals(checkParam->mType)))
  676. return false;
  677. param = param->mNext;
  678. checkParam = checkParam->mNext;
  679. paramIdx++;
  680. }
  681. if ((param != NULL) || (checkParam != NULL))
  682. return false;
  683. if (!mReturnType->Equals(checkMethod->mReturnType))
  684. return false;
  685. return true;
  686. }
  687. int DbgSubprogram::GetParamCount()
  688. {
  689. int paramCount = mParams.Size();
  690. if (mHasThis)
  691. paramCount--;
  692. return paramCount;
  693. }
  694. String DbgSubprogram::GetParamName(int paramIdx)
  695. {
  696. auto param = mParams[paramIdx];
  697. if (param->mName != NULL)
  698. {
  699. String name = "'";
  700. name += param->mName;
  701. name += "'";
  702. return name;
  703. }
  704. return StrFormat("%d", paramIdx + 1);
  705. }
  706. bool DbgSubprogram::IsGenericMethod()
  707. {
  708. if (mName == NULL)
  709. return false;
  710. for (const char* cPtr = mName; true; cPtr++)
  711. {
  712. char c = *cPtr;
  713. if (c == '\0')
  714. break;
  715. if (c == '<')
  716. return true;
  717. }
  718. return false;
  719. }
  720. bool DbgSubprogram::ThisIsSplat()
  721. {
  722. if (mBlock.mVariables.mHead == NULL)
  723. return false;
  724. return strncmp(mBlock.mVariables.mHead->mName, "$this$", 6) == 0;
  725. }
  726. bool DbgSubprogram::IsLambda()
  727. {
  728. if (mName == NULL)
  729. return false;
  730. return StringView(mName).Contains('$');
  731. }
  732. //////////////////////////////////////////////////////////////////////////
  733. DbgSubprogram::~DbgSubprogram()
  734. {
  735. BfLogDbg("DbgSubprogram::~DbgSubprogram %p\n", this);
  736. }
  737. ////////////////////
  738. bool DbgSrcFile::IsBeef()
  739. {
  740. int dotPos = (int)mFilePath.LastIndexOf('.');
  741. if (dotPos == -1)
  742. return false;
  743. const char* ext = mFilePath.c_str() + dotPos;
  744. // The ".cs" is legacy. Remove that eventually.
  745. return (stricmp(ext, ".bf") == 0) || (stricmp(ext, ".cs") == 0);
  746. }
  747. DbgSrcFile::~DbgSrcFile()
  748. {
  749. for (auto replacedLineInfo : mHotReplacedDbgLineInfo)
  750. delete replacedLineInfo;
  751. }
  752. void DbgSrcFile::RemoveDeferredRefs(DbgModule* debugModule)
  753. {
  754. for (int deferredIdx = 0; deferredIdx < (int)mDeferredRefs.size(); )
  755. {
  756. if (mDeferredRefs[deferredIdx].mDbgModule == debugModule)
  757. {
  758. // Fast remove
  759. mDeferredRefs[deferredIdx] = mDeferredRefs.back();
  760. mDeferredRefs.pop_back();
  761. }
  762. else
  763. deferredIdx++;
  764. }
  765. }
  766. void DbgSrcFile::RemoveLines(DbgModule* debugModule)
  767. {
  768. if (!mHasLineDataFromMultipleModules)
  769. {
  770. // Fast-out case
  771. mLineDataRefs.Clear();
  772. return;
  773. }
  774. for (int idx = 0; idx < (int)mLineDataRefs.size(); idx++)
  775. {
  776. auto dbgSubprogram = mLineDataRefs[idx];
  777. if (dbgSubprogram->mCompileUnit->mDbgModule == debugModule)
  778. {
  779. mLineDataRefs.RemoveAtFast(idx);
  780. idx--;
  781. }
  782. }
  783. }
  784. void DbgSrcFile::RemoveLines(DbgModule* debugModule, DbgSubprogram* dbgSubprogram, bool isHotReplaced)
  785. {
  786. debugModule->mDebugTarget->mPendingSrcFileRehup.Add(this);
  787. if (isHotReplaced)
  788. {
  789. int vecIdx = dbgSubprogram->mCompileUnit->mDbgModule->mHotIdx;
  790. BF_ASSERT(vecIdx >= 0);
  791. while (vecIdx >= (int)mHotReplacedDbgLineInfo.size())
  792. mHotReplacedDbgLineInfo.push_back(new HotReplacedLineInfo());
  793. auto hotReplacedLineInfo = mHotReplacedDbgLineInfo[vecIdx];
  794. HotReplacedLineInfo::Entry entry;
  795. entry.mSubprogram = dbgSubprogram;
  796. entry.mLineInfo = dbgSubprogram->mLineInfo;
  797. hotReplacedLineInfo->mEntries.Add(entry);
  798. }
  799. }
  800. void DbgSrcFile::RehupLineData()
  801. {
  802. for (int idx = 0; idx < (int)mLineDataRefs.size(); idx++)
  803. {
  804. auto dbgSubprogram = mLineDataRefs[idx];
  805. if (dbgSubprogram->mHotReplaceKind != DbgSubprogram::HotReplaceKind_None)
  806. {
  807. mLineDataRefs.RemoveAtFast(idx);
  808. idx--;
  809. }
  810. }
  811. }
  812. void DbgSrcFile::VerifyPath()
  813. {
  814. if (mVerifiedPath)
  815. return;
  816. if (mLineDataRefs.IsEmpty())
  817. return;
  818. if (!::FileExists(mFilePath))
  819. {
  820. bool didReplace = false;
  821. for (auto& kv : gDebugManager->mSourcePathRemap)
  822. {
  823. if (mFilePath.StartsWith(kv.mKey, StringImpl::CompareKind_OrdinalIgnoreCase))
  824. {
  825. mFilePath.Remove(0, kv.mKey.mLength);
  826. mFilePath.Insert(0, kv.mValue);
  827. didReplace = true;
  828. }
  829. }
  830. if (!didReplace)
  831. {
  832. HashSet<DbgModule*> checkedModules;
  833. for (auto& lineDataRef : mLineDataRefs)
  834. {
  835. auto dbgModule = lineDataRef->mCompileUnit->mDbgModule;
  836. if (checkedModules.Add(dbgModule))
  837. {
  838. if (dbgModule->mDbgFlavor == DbgFlavor_MS)
  839. {
  840. COFF* coff = (COFF*)dbgModule;
  841. if ((!coff->mOrigPDBPath.IsEmpty()) && (!coff->mOrigPDBPath.Equals(coff->mPDBPath, StringImpl::CompareKind_OrdinalIgnoreCase)))
  842. {
  843. String relFilePath = GetRelativePath(mFilePath, coff->mOrigPDBPath);
  844. String checkActualFilePath = GetAbsPath(relFilePath, coff->mPDBPath);
  845. if (FileExists(checkActualFilePath))
  846. {
  847. mFilePath = checkActualFilePath;
  848. break;
  849. }
  850. }
  851. }
  852. }
  853. }
  854. }
  855. }
  856. mVerifiedPath = true;
  857. }
  858. const String& DbgSrcFile::GetLocalPath()
  859. {
  860. if (!mVerifiedPath)
  861. VerifyPath();
  862. return (!mLocalPath.IsEmpty()) ? mLocalPath : mFilePath;
  863. }
  864. void DbgSrcFile::GetHash(String& outStr)
  865. {
  866. if (mHashKind == DbgHashKind_MD5)
  867. {
  868. for (int i = 0; i < 16; i++)
  869. {
  870. outStr += StrFormat("%02X", mHash[i]);
  871. }
  872. }
  873. else if (mHashKind == DbgHashKind_SHA256)
  874. {
  875. for (int i = 0; i < 32; i++)
  876. {
  877. outStr += StrFormat("%02X", mHash[i]);
  878. }
  879. }
  880. }
  881. //////////////////////////////////////////////////////////////////////////
  882. DbgType::DbgType()
  883. {
  884. mTypeIdx = -1;
  885. mIsDeclaration = false;
  886. mParent = NULL;
  887. mTypeName = NULL;
  888. mTypeCode = DbgType_Null;
  889. mSize = 0;
  890. mPtrType = NULL;
  891. mTypeParam = NULL;
  892. mBlockParam = NULL;
  893. mNext = NULL;
  894. mPriority = DbgTypePriority_Normal;
  895. }
  896. DbgType::~DbgType()
  897. {
  898. BfLogDbg("DbgType::~DWType %p\n", this);
  899. }
  900. DbgType* DbgType::ResolveTypeDef()
  901. {
  902. if (mTypeCode == DbgType_TypeDef)
  903. return mTypeParam->ResolveTypeDef();
  904. return this;
  905. }
  906. bool DbgType::Equals(DbgType* dbgType)
  907. {
  908. if (dbgType == NULL)
  909. return false;
  910. if (mTypeCode != dbgType->mTypeCode)
  911. {
  912. if ((mTypeCode == DbgType_Enum) || (dbgType->mTypeCode == DbgType_Enum))
  913. {
  914. // These may change mTypeCode, so redo the check afterward
  915. GetPrimaryType();
  916. dbgType->GetPrimaryType();
  917. }
  918. if (mTypeCode != dbgType->mTypeCode)
  919. return false;
  920. }
  921. if ((mName == NULL) != (dbgType->mName == NULL))
  922. return false;
  923. if (mName != NULL)
  924. {
  925. if (dbgType->mFixedName)
  926. FixName();
  927. else if (mFixedName)
  928. dbgType->FixName();
  929. if (strcmp(mName, dbgType->mName) != 0)
  930. return false;
  931. }
  932. if ((mTypeParam != NULL) && (!mTypeParam->Equals(dbgType->mTypeParam)))
  933. return false;
  934. // Did mName already include the parent name?
  935. if (mCompileUnit->mDbgModule->mDbgFlavor == DbgFlavor_MS)
  936. return true;
  937. if ((mParent != NULL) != (dbgType->mParent != NULL))
  938. return false;
  939. if (mParent != NULL)
  940. return mParent->Equals(dbgType->mParent);
  941. return true;
  942. }
  943. bool DbgType::IsStruct()
  944. {
  945. return mTypeCode == DbgType_Struct;
  946. }
  947. bool DbgType::IsPrimitiveType()
  948. {
  949. return (mTypeCode >= DbgType_i8) && (mTypeCode <= DbgType_Bool);
  950. }
  951. bool DbgType::IsNull()
  952. {
  953. return mTypeCode == DbgType_Null;
  954. }
  955. bool DbgType::IsVoid()
  956. {
  957. return (mTypeCode == DbgType_Void);
  958. }
  959. bool DbgType::IsValuelessType()
  960. {
  961. return ((mTypeCode == DbgType_Struct) && (GetByteCount() == 0)) || (mTypeCode == DbgType_Void);
  962. }
  963. bool DbgType::IsValueType()
  964. {
  965. return (mTypeCode <= DbgType_DefinitionEnd);
  966. }
  967. bool DbgType::IsTypedPrimitive()
  968. {
  969. PopulateType();
  970. if (mTypeCode != DbgType_Struct)
  971. return false;
  972. if (mTypeParam != NULL)
  973. return true;
  974. auto baseType = GetBaseType();
  975. if (baseType == NULL)
  976. return false;
  977. if (!baseType->IsTypedPrimitive())
  978. return false;
  979. mTypeParam = baseType->mTypeParam;
  980. return true;
  981. }
  982. bool DbgType::IsBoolean()
  983. {
  984. return mTypeCode == DbgType_Bool;
  985. }
  986. bool DbgType::IsInteger()
  987. {
  988. return (mTypeCode >= DbgType_i8) && (mTypeCode <= DbgType_u64);
  989. }
  990. bool DbgType::IsIntegral()
  991. {
  992. return ((mTypeCode >= DbgType_i8) && (mTypeCode <= DbgType_u64)) ||
  993. ((mTypeCode >= DbgType_SChar) && (mTypeCode <= DbgType_UChar32));
  994. }
  995. bool DbgType::IsChar()
  996. {
  997. return (mTypeCode >= DbgType_SChar) && (mTypeCode <= DbgType_UChar32);
  998. }
  999. bool DbgType::IsChar(DbgLanguage language)
  1000. {
  1001. if (language == DbgLanguage_Beef)
  1002. return (mTypeCode >= DbgType_UChar) && (mTypeCode <= DbgType_UChar32);
  1003. return (mTypeCode >= DbgType_SChar) && (mTypeCode <= DbgType_SChar32);
  1004. }
  1005. bool DbgType::IsFloat()
  1006. {
  1007. return (mTypeCode == DbgType_Single) || (mTypeCode == DbgType_Double);
  1008. }
  1009. // "Struct" in this sense means that we do NOT have a pointer to this value, but it may or may not be a Beef Struct
  1010. bool DbgType::IsCompositeType()
  1011. {
  1012. if (((mTypeCode == DbgType_TypeDef) || (mTypeCode == DbgType_Const)) && (mTypeParam != NULL))
  1013. return mTypeParam->IsCompositeType();
  1014. return ((mTypeCode == DbgType_Struct) || (mTypeCode == DbgType_Class) || (mTypeCode == DbgType_SizedArray));
  1015. }
  1016. bool DbgType::WantsRefThis()
  1017. {
  1018. return (GetLanguage() == DbgLanguage_Beef) && (!IsBfObject());
  1019. }
  1020. bool DbgType::IsBfObjectPtr()
  1021. {
  1022. if ((mTypeCode == DbgType_Ptr) && (mTypeParam != NULL))
  1023. return mTypeParam->IsBfObject();
  1024. return false;
  1025. }
  1026. DbgExtType DbgType::CalcExtType()
  1027. {
  1028. auto language = GetLanguage();
  1029. if ((!mFixedName) && (language == DbgLanguage_Beef))
  1030. {
  1031. FixName();
  1032. }
  1033. auto primaryType = GetPrimaryType();
  1034. if (this != primaryType)
  1035. {
  1036. return primaryType->CalcExtType();
  1037. }
  1038. if (mCompileUnit == NULL)
  1039. return DbgExtType_Normal;
  1040. if (language != DbgLanguage_Beef)
  1041. return DbgExtType_Normal;
  1042. if ((mTypeCode != DbgType_Struct) && (mTypeCode != DbgType_Class))
  1043. return DbgExtType_Normal;
  1044. PopulateType();
  1045. if (mExtType != DbgExtType_Unknown)
  1046. return mExtType;
  1047. auto baseType = GetBaseType();
  1048. if (baseType == NULL)
  1049. {
  1050. if (mParent == NULL)
  1051. return DbgExtType_Normal;
  1052. if (mParent->mTypeCode != DbgType_Namespace)
  1053. return DbgExtType_Normal;
  1054. if (mParent->mParent != NULL)
  1055. return DbgExtType_Normal;
  1056. if (strcmp(mParent->mTypeName, "System") != 0)
  1057. return DbgExtType_Normal;
  1058. if (strcmp(mTypeName, "Object") != 0)
  1059. return DbgExtType_Normal;
  1060. return DbgExtType_BfObject;
  1061. }
  1062. else
  1063. {
  1064. if (strcmp(baseType->mTypeName, "Enum") == 0)
  1065. {
  1066. for (auto member : mMemberList)
  1067. {
  1068. if ((member->mName != NULL) && (strcmp(member->mName, "__bftag") == 0))
  1069. return DbgExtType_BfPayloadEnum;
  1070. }
  1071. return DbgExtType_Normal;
  1072. }
  1073. else if (strcmp(baseType->mTypeName, "ValueType") == 0)
  1074. {
  1075. for (auto member : mMemberList)
  1076. {
  1077. if ((member->mName != NULL) && (strcmp(member->mName, "$bfunion") == 0))
  1078. return DbgExtType_BfUnion;
  1079. }
  1080. }
  1081. }
  1082. auto baseExtType = baseType->CalcExtType();
  1083. if ((baseExtType == DbgExtType_BfObject) && (GetByteCount() == 0))
  1084. baseExtType = DbgExtType_Interface;
  1085. return baseExtType;
  1086. }
  1087. DbgLanguage DbgType::GetLanguage()
  1088. {
  1089. return mLanguage;
  1090. }
  1091. void DbgType::FixName()
  1092. {
  1093. if (mFixedName)
  1094. return;
  1095. int depthCount = 0;
  1096. auto dbgModule = mCompileUnit->mDbgModule;
  1097. if ((dbgModule->mDbgFlavor == DbgFlavor_MS) && (mName != NULL) && (strlen(mName) > 0))
  1098. {
  1099. bool modified = false;
  1100. if (!dbgModule->DbgIsStrMutable(mName))
  1101. mName = dbgModule->DbgDupString(mName);
  1102. const char* typeNamePtr = mTypeName;
  1103. char* nameP = (char*)mName;
  1104. // Fix the name
  1105. char* inPtr = nameP;
  1106. char* outPtr = nameP;
  1107. while (true)
  1108. {
  1109. char c = *(inPtr++);
  1110. if ((c == '<') || (c == '('))
  1111. depthCount++;
  1112. else if ((c == '>') || (c == ')'))
  1113. depthCount--;
  1114. if ((c == ':') && (inPtr[0] == ':'))
  1115. {
  1116. if (mLanguage == DbgLanguage_Beef)
  1117. {
  1118. modified = true;
  1119. inPtr++;
  1120. *(outPtr++) = '.';
  1121. if (depthCount == 0)
  1122. typeNamePtr = outPtr;
  1123. }
  1124. else if (depthCount == 0)
  1125. mTypeName = inPtr + 1;
  1126. }
  1127. else if (modified)
  1128. *(outPtr++) = c;
  1129. else
  1130. outPtr++;
  1131. if (c == 0)
  1132. break;
  1133. }
  1134. if ((modified) && (mName != mTypeName) && (typeNamePtr != NULL))
  1135. {
  1136. mTypeName = typeNamePtr;
  1137. }
  1138. }
  1139. mFixedName = true;
  1140. }
  1141. bool DbgType::IsBfObject()
  1142. {
  1143. if (mExtType == DbgExtType_Unknown)
  1144. mExtType = CalcExtType();
  1145. return (mExtType == DbgExtType_BfObject) || (mExtType == DbgExtType_Interface);
  1146. }
  1147. bool DbgType::IsBfPayloadEnum()
  1148. {
  1149. if (mExtType == DbgExtType_Unknown)
  1150. mExtType = CalcExtType();
  1151. return mExtType == DbgExtType_BfPayloadEnum;
  1152. }
  1153. bool DbgType::IsBfUnion()
  1154. {
  1155. if (mExtType == DbgExtType_Unknown)
  1156. mExtType = CalcExtType();
  1157. return mExtType == DbgExtType_BfUnion;
  1158. }
  1159. bool DbgType::IsBfEnum()
  1160. {
  1161. if (mTypeCode != DbgType_Struct)
  1162. return false;
  1163. auto baseType = GetBaseType();
  1164. if (baseType == NULL)
  1165. {
  1166. if (mParent == NULL)
  1167. return false;
  1168. if (mParent->mTypeCode != DbgType_Namespace)
  1169. return false;
  1170. if (mParent->mParent != NULL)
  1171. return false;
  1172. if (strcmp(mParent->mTypeName, "System") != 0)
  1173. return false;
  1174. return strcmp(mTypeName, "Enum") == 0;
  1175. }
  1176. return baseType->IsBfEnum();
  1177. }
  1178. bool DbgType::IsBfTuple()
  1179. {
  1180. if (mTypeCode != DbgType_Struct)
  1181. return false;
  1182. if (GetLanguage() != DbgLanguage_Beef)
  1183. return false;
  1184. if (mName == NULL)
  1185. return false;
  1186. return mName[0] == '(';
  1187. }
  1188. bool DbgType::HasCPPVTable()
  1189. {
  1190. if ((mTypeCode != DbgType_Struct) && (mTypeCode != DbgType_Class))
  1191. return false;
  1192. /*if (!mMemberList.IsEmpty())
  1193. {
  1194. //TODO: We commented this out at some point- why did we do that?
  1195. if ((mMemberList.mHead->mName != NULL) && (strncmp(mMemberList.mHead->mName, "_vptr$", 6) == 0))
  1196. return true;
  1197. }*/
  1198. if (mHasVTable)
  1199. return true;
  1200. if (GetLanguage() == DbgLanguage_Beef)
  1201. return false;
  1202. for (auto checkBaseType : mBaseTypes)
  1203. {
  1204. if (checkBaseType->mBaseType->HasCPPVTable())
  1205. return true;
  1206. }
  1207. return false;
  1208. }
  1209. bool DbgType::IsBaseBfObject()
  1210. {
  1211. auto baseType = GetBaseType();
  1212. return (baseType == NULL) && (IsBfObject());
  1213. }
  1214. bool DbgType::IsInterface()
  1215. {
  1216. if (mExtType == DbgExtType_Unknown)
  1217. mExtType = CalcExtType();
  1218. return mExtType == DbgExtType_Interface;
  1219. }
  1220. bool DbgType::IsNamespace()
  1221. {
  1222. return mTypeCode == DbgType_Namespace;
  1223. }
  1224. bool DbgType::IsEnum()
  1225. {
  1226. return (mTypeCode == DbgType_Enum);
  1227. }
  1228. bool DbgType::IsRoot()
  1229. {
  1230. return (mTypeCode == DbgType_Root);
  1231. }
  1232. bool DbgType::IsRef()
  1233. {
  1234. return
  1235. (mTypeCode == DbgType_Ref) ||
  1236. (mTypeCode == DbgType_RValueReference);
  1237. }
  1238. bool DbgType::IsSigned()
  1239. {
  1240. return
  1241. (mTypeCode == DbgType_i8) ||
  1242. (mTypeCode == DbgType_i16) ||
  1243. (mTypeCode == DbgType_i32) ||
  1244. (mTypeCode == DbgType_i64);
  1245. }
  1246. bool DbgType::IsConst()
  1247. {
  1248. if ((mTypeCode == DbgType_Ptr) || (mTypeCode == DbgType_Ref))
  1249. {
  1250. if (mTypeParam != NULL)
  1251. return mTypeParam->IsConst();
  1252. }
  1253. return mTypeCode == DbgType_Const;
  1254. }
  1255. bool DbgType::IsPointer(bool includeBfObjectPointer)
  1256. {
  1257. if (mTypeCode != DbgType_Ptr)
  1258. return false;
  1259. if ((!includeBfObjectPointer) && (mTypeParam != NULL) && (mTypeParam->IsBfObject()))
  1260. return false;
  1261. return true;
  1262. }
  1263. bool DbgType::HasPointer(bool includeBfObjectPointer)
  1264. {
  1265. if (((mTypeCode == DbgType_Const) || (mTypeCode == DbgType_Ref)) && (mTypeParam != NULL))
  1266. return mTypeParam->IsPointer(includeBfObjectPointer);
  1267. return IsPointer(includeBfObjectPointer);
  1268. }
  1269. bool DbgType::IsPointerOrRef(bool includeBfObjectPointer)
  1270. {
  1271. if ((mTypeCode != DbgType_Ptr) && (mTypeCode != DbgType_Ref) && (mTypeCode != DbgType_RValueReference))
  1272. return false;
  1273. if ((!includeBfObjectPointer) && (mTypeParam != NULL) && (mTypeParam->IsBfObject()))
  1274. return false;
  1275. return true;
  1276. }
  1277. bool DbgType::IsSizedArray()
  1278. {
  1279. return (mTypeCode == DbgType_SizedArray);
  1280. }
  1281. bool DbgType::IsAnonymous()
  1282. {
  1283. return (mTypeName == NULL) || (mTypeName[0] == '<');
  1284. }
  1285. bool DbgType::IsGlobalsContainer()
  1286. {
  1287. return (mTypeName != NULL) && (mTypeName[0] == 'G') && (mTypeName[1] == '$');
  1288. }
  1289. DbgType* DbgType::GetUnderlyingType()
  1290. {
  1291. return mTypeParam;
  1292. }
  1293. void DbgType::PopulateType()
  1294. {
  1295. if (mIsIncomplete)
  1296. {
  1297. mCompileUnit->mDbgModule->PopulateType(this);
  1298. mIsIncomplete = false;
  1299. }
  1300. }
  1301. DbgModule* DbgType::GetDbgModule()
  1302. {
  1303. if (mCompileUnit == NULL)
  1304. return NULL;
  1305. return mCompileUnit->mDbgModule;
  1306. }
  1307. DbgType* DbgType::GetPrimaryType()
  1308. {
  1309. if (mPrimaryType != NULL)
  1310. return mPrimaryType;
  1311. mPrimaryType = this;
  1312. if (mPriority <= DbgTypePriority_Normal)
  1313. {
  1314. if ((mCompileUnit != NULL) &&
  1315. ((mCompileUnit->mLanguage == DbgLanguage_Beef)|| (mLanguage == DbgLanguage_Beef) ||
  1316. (mTypeCode == DbgType_Namespace) || (mIsDeclaration)))
  1317. {
  1318. mPrimaryType = mCompileUnit->mDbgModule->GetPrimaryType(this);
  1319. mPrimaryType->PopulateType();
  1320. mTypeCode = mPrimaryType->mTypeCode;
  1321. mTypeParam = mPrimaryType->mTypeParam;
  1322. }
  1323. }
  1324. return mPrimaryType;
  1325. }
  1326. DbgType* DbgType::GetBaseType()
  1327. {
  1328. auto primaryType = GetPrimaryType();
  1329. if (primaryType != this)
  1330. return primaryType->GetBaseType();
  1331. PopulateType();
  1332. if (mBaseTypes.mHead == NULL)
  1333. return NULL;
  1334. if (GetLanguage() != DbgLanguage_Beef)
  1335. return NULL;
  1336. auto baseType = mBaseTypes.mHead->mBaseType;
  1337. BF_ASSERT(!baseType->IsInterface());
  1338. if ((baseType == NULL) || (baseType->mPriority > DbgTypePriority_Normal))
  1339. return baseType;
  1340. baseType = mCompileUnit->mDbgModule->GetPrimaryType(baseType);
  1341. mBaseTypes.mHead->mBaseType = baseType;
  1342. if (baseType->mIsDeclaration)
  1343. {
  1344. // That's no good, try to fix it up
  1345. if (baseType->GetLanguage() == DbgLanguage_Beef)
  1346. {
  1347. if (baseType->GetBaseType() == NULL)
  1348. {
  1349. if (baseType->ToString() == "System.Function")
  1350. {
  1351. DbgBaseTypeEntry* baseTypeEntry = mCompileUnit->mDbgModule->mAlloc.Alloc<DbgBaseTypeEntry>();
  1352. baseTypeEntry->mBaseType = mCompileUnit->mDbgModule->GetPrimitiveType(DbgType_IntPtr_Alias, DbgLanguage_Beef);
  1353. baseType->mBaseTypes.PushBack(baseTypeEntry);
  1354. }
  1355. }
  1356. }
  1357. }
  1358. return baseType;
  1359. }
  1360. DbgType* DbgType::GetRootBaseType()
  1361. {
  1362. auto baseType = GetBaseType();
  1363. if (baseType != NULL)
  1364. return baseType->GetRootBaseType();
  1365. return this;
  1366. }
  1367. DbgType* DbgType::RemoveModifiers(bool* hadRef)
  1368. {
  1369. DbgType* dbgType = this;
  1370. while (dbgType != NULL)
  1371. {
  1372. bool curHadRef = (dbgType->mTypeCode == DbgType_Ref) || (dbgType->mTypeCode == DbgType_RValueReference);
  1373. if ((curHadRef) && (hadRef != NULL))
  1374. *hadRef = true;
  1375. if ((dbgType->mTypeCode == DbgType_Const) || (dbgType->mTypeCode == DbgType_TypeDef) || (dbgType->mTypeCode == DbgType_Volatile) || (dbgType->mTypeCode == DbgType_Bitfield) ||
  1376. (dbgType->mTypeCode == DbgType_Unaligned) || (curHadRef))
  1377. {
  1378. if (dbgType->mTypeParam == NULL)
  1379. break;
  1380. dbgType = dbgType->mTypeParam;
  1381. }
  1382. else
  1383. break;
  1384. }
  1385. return dbgType;
  1386. }
  1387. String DbgType::ToStringRaw(DbgLanguage language)
  1388. {
  1389. if (mTypeIdx != -1)
  1390. return StrFormat("_T_%d_%d", mCompileUnit->mDbgModule->GetLinkedModule()->mId, mTypeIdx);
  1391. return ToString(language);
  1392. }
  1393. void DbgType::ToString(StringImpl& str, DbgLanguage language, bool allowDirectBfObject, bool internalName)
  1394. {
  1395. if (language == DbgLanguage_Unknown)
  1396. language = GetLanguage();
  1397. if (language == DbgLanguage_Beef)
  1398. {
  1399. switch (mTypeCode)
  1400. {
  1401. case DbgType_UChar:
  1402. str += "char8";
  1403. return;
  1404. case DbgType_UChar16:
  1405. str += "char16";
  1406. return;
  1407. case DbgType_UChar32:
  1408. str += "char32";
  1409. return;
  1410. case DbgType_i8:
  1411. str += "int8";
  1412. return;
  1413. case DbgType_u8:
  1414. str += "uint8";
  1415. return;
  1416. case DbgType_i16:
  1417. str += "int16";
  1418. return;
  1419. case DbgType_u16:
  1420. str += "uint16";
  1421. return;
  1422. case DbgType_i32:
  1423. str += "int32";
  1424. return;
  1425. case DbgType_u32:
  1426. str += "uint32";
  1427. return;
  1428. case DbgType_i64:
  1429. str += "int64";
  1430. return;
  1431. case DbgType_u64:
  1432. str += "uint64";
  1433. return;
  1434. }
  1435. }
  1436. else
  1437. {
  1438. switch (mTypeCode)
  1439. {
  1440. case DbgType_SChar:
  1441. str += "char";
  1442. return;
  1443. case DbgType_SChar16:
  1444. str += "wchar_t";
  1445. return;
  1446. case DbgType_SChar32:
  1447. str += "int32_t";
  1448. return;
  1449. case DbgType_UChar:
  1450. str += "uint8_t";
  1451. return;
  1452. case DbgType_UChar16:
  1453. str += "uint16_t";
  1454. return;
  1455. case DbgType_UChar32:
  1456. str += "uint32_t";
  1457. return;
  1458. case DbgType_i8:
  1459. str += "char";
  1460. return;
  1461. case DbgType_u8:
  1462. str += "uint8_t";
  1463. return;
  1464. case DbgType_i16:
  1465. str += "short";
  1466. return;
  1467. case DbgType_u16:
  1468. str += "uint16_t";
  1469. return;
  1470. case DbgType_i32:
  1471. str += "int";
  1472. return;
  1473. case DbgType_u32:
  1474. str += "uint32_t";
  1475. return;
  1476. case DbgType_i64:
  1477. str += "int64_t";
  1478. return;
  1479. case DbgType_u64:
  1480. str += "uint64_t";
  1481. return;
  1482. }
  1483. }
  1484. if (mTypeCode == DbgType_Namespace)
  1485. internalName = false;
  1486. auto parent = mParent;
  1487. if ((parent == NULL) && (internalName))
  1488. {
  1489. auto primaryType = GetPrimaryType();
  1490. parent = primaryType->mParent;
  1491. }
  1492. if (mTypeName != NULL)
  1493. {
  1494. if ((!allowDirectBfObject) && (IsBfObject()))
  1495. {
  1496. // Only use the '#' for testing
  1497. //return ToString(true) + "#";
  1498. ToString(str, DbgLanguage_Unknown, true, internalName);
  1499. return;
  1500. }
  1501. if (IsGlobalsContainer())
  1502. {
  1503. if (mParent != NULL)
  1504. {
  1505. mParent->ToString(str, language, false, internalName);
  1506. return;
  1507. }
  1508. return;
  1509. }
  1510. //String combName;
  1511. /*if (mTemplateParams != NULL)
  1512. {
  1513. combName = nameP;
  1514. combName += mTemplateParams;
  1515. nameP = combName.c_str();
  1516. }*/
  1517. if ((!mFixedName) /*&& (language == DbgLanguage_Beef)*/)
  1518. {
  1519. FixName();
  1520. }
  1521. char* nameP = (char*)mTypeName;
  1522. if (parent == NULL)
  1523. {
  1524. if (strncmp(nameP, "Box<", 4) == 0)
  1525. {
  1526. str += String(nameP + 4, nameP + strlen(nameP) - 1);
  1527. str += "^";
  1528. return;
  1529. }
  1530. // For declarations, may also include namespaces
  1531. str += mName;
  1532. return;
  1533. }
  1534. if (GetLanguage() == DbgLanguage_Beef)
  1535. {
  1536. parent->ToString(str, language, allowDirectBfObject, internalName);
  1537. if ((internalName) && (parent->mTypeCode != DbgType_Namespace))
  1538. str += "+";
  1539. else
  1540. str += ".";
  1541. str += nameP;
  1542. }
  1543. else
  1544. {
  1545. parent->ToString(str, language, allowDirectBfObject, internalName);
  1546. if ((internalName) && (parent->mTypeCode != DbgType_Namespace))
  1547. str += "+";
  1548. else
  1549. str += "::";
  1550. str += nameP;
  1551. }
  1552. return;
  1553. }
  1554. switch (mTypeCode)
  1555. {
  1556. case DbgType_Struct:
  1557. {
  1558. if ((mTypeName == NULL) && (parent != NULL))
  1559. {
  1560. parent->ToString(str, language, allowDirectBfObject, internalName);
  1561. return;
  1562. }
  1563. str += "@struct";
  1564. return;
  1565. }
  1566. case DbgType_Class:
  1567. {
  1568. str += "@class";
  1569. return;
  1570. }
  1571. case DbgType_TypeDef:
  1572. {
  1573. str += "@typedef";
  1574. return;
  1575. }
  1576. case DbgType_Const:
  1577. {
  1578. if (language == DbgLanguage_Beef)
  1579. {
  1580. str += "readonly";
  1581. if (mTypeParam != NULL)
  1582. {
  1583. str += " ";
  1584. mTypeParam->ToString(str, language, allowDirectBfObject, internalName);
  1585. }
  1586. return;
  1587. }
  1588. str += "const";
  1589. if (mTypeParam != NULL)
  1590. {
  1591. str += " ";
  1592. mTypeParam->ToString(str, language, allowDirectBfObject, internalName);
  1593. }
  1594. return;
  1595. }
  1596. case DbgType_Volatile:
  1597. {
  1598. str += "volatile";
  1599. if (mTypeParam != NULL)
  1600. {
  1601. str += " ";
  1602. mTypeParam->ToString(str, language, allowDirectBfObject, internalName);
  1603. }
  1604. return;
  1605. }
  1606. case DbgType_Unaligned:
  1607. {
  1608. str += "unaligned";
  1609. if (mTypeParam != NULL)
  1610. {
  1611. str += " ";
  1612. mTypeParam->ToString(str, language, allowDirectBfObject, internalName);
  1613. }
  1614. }
  1615. case DbgType_Restrict:
  1616. {
  1617. str += "restrict";
  1618. if (mTypeParam != NULL)
  1619. {
  1620. str += " ";
  1621. mTypeParam->ToString(str, language, allowDirectBfObject, internalName);
  1622. }
  1623. }
  1624. case DbgType_Ptr:
  1625. {
  1626. if (mTypeParam == NULL)
  1627. {
  1628. str += "void*";
  1629. return;
  1630. }
  1631. if (mTypeParam->IsBfObject())
  1632. {
  1633. mTypeParam->ToString(str, DbgLanguage_Unknown, true, internalName);
  1634. return;
  1635. }
  1636. // Don't put a "*" on the end of a function type, it's implicit
  1637. if (mTypeParam->mTypeCode == DbgType_Subroutine)
  1638. {
  1639. mTypeParam->ToString(str, language, allowDirectBfObject, internalName);
  1640. return;
  1641. }
  1642. mTypeParam->ToString(str, language, allowDirectBfObject, internalName);
  1643. str += "*";
  1644. return;
  1645. }
  1646. case DbgType_Ref:
  1647. {
  1648. if (language == DbgLanguage_Beef)
  1649. {
  1650. str += "ref";
  1651. if (mTypeParam != NULL)
  1652. {
  1653. str += " ";
  1654. mTypeParam->ToString(str, language, allowDirectBfObject, internalName);
  1655. }
  1656. return;
  1657. }
  1658. if (mTypeParam == NULL)
  1659. {
  1660. str += "&";
  1661. return;
  1662. }
  1663. mTypeParam->ToString(str, language, allowDirectBfObject, internalName);
  1664. str += "&";
  1665. return;
  1666. }
  1667. case DbgType_RValueReference:
  1668. {
  1669. if (language == DbgLanguage_Beef)
  1670. {
  1671. // Ignore this - this is used for passing structs when we're not using the 'byval' attribute
  1672. mTypeParam->ToString(str, language, allowDirectBfObject, internalName);
  1673. return;
  1674. }
  1675. if (mTypeParam == NULL)
  1676. {
  1677. str += "&&";
  1678. return;
  1679. }
  1680. mTypeParam->ToString(str, language, allowDirectBfObject, internalName);
  1681. str += "&&";
  1682. return;
  1683. }
  1684. case DbgType_Unspecified:
  1685. str += mTypeName;
  1686. return;
  1687. case DbgType_SizedArray:
  1688. {
  1689. StringT<128> name;
  1690. auto checkType = this;
  1691. while (checkType->mTypeCode == DbgType_SizedArray)
  1692. {
  1693. intptr innerSize = checkType->mTypeParam->GetStride();
  1694. intptr arrSize = 0;
  1695. if (innerSize > 0)
  1696. {
  1697. arrSize = checkType->GetStride() / innerSize;
  1698. }
  1699. name += StrFormat("[%lld]", arrSize);
  1700. checkType = checkType->mTypeParam;
  1701. }
  1702. checkType->ToString(str, language, allowDirectBfObject, internalName);
  1703. str += name;
  1704. return;
  1705. }
  1706. case DbgType_Union:
  1707. {
  1708. str += "union";
  1709. if (mTypeParam != NULL)
  1710. {
  1711. str += " ";
  1712. mTypeParam->ToString(str, language, allowDirectBfObject, internalName);
  1713. }
  1714. return;
  1715. }
  1716. case DbgType_Single:
  1717. str += "float";
  1718. return;
  1719. case DbgType_Double:
  1720. str += "double";
  1721. return;
  1722. case DbgType_Null:
  1723. str += "void";
  1724. return;
  1725. case DbgType_Subroutine:
  1726. {
  1727. mTypeParam->ToString(str, language, allowDirectBfObject, internalName);
  1728. str += " (";
  1729. int paramIdx = 0;
  1730. for (auto param : mBlockParam->mVariables)
  1731. {
  1732. if (paramIdx > 0)
  1733. str += ", ";
  1734. param->mType->ToString(str, language, allowDirectBfObject, internalName);
  1735. paramIdx++;
  1736. }
  1737. str += ")";
  1738. return;
  1739. }
  1740. case DbgType_VTable:
  1741. str += "@vtable";
  1742. return;
  1743. case DbgType_Enum:
  1744. str += "@enum";
  1745. return;
  1746. case DbgType_Namespace:
  1747. {
  1748. // Anonymous
  1749. str += "`anon`";
  1750. return;
  1751. }
  1752. case DbgType_PtrToMember:
  1753. str += "@ptrToMember";
  1754. return;
  1755. case DbgType_Bitfield:
  1756. {
  1757. auto dbgBitfieldType = (DbgBitfieldType*)this;
  1758. mTypeParam->ToString(str, language, allowDirectBfObject, internalName);
  1759. str += StrFormat("{%d:%d}", dbgBitfieldType->mPosition, dbgBitfieldType->mLength);
  1760. return;
  1761. }
  1762. default:
  1763. break;
  1764. }
  1765. BF_FATAL("Unhandled type");
  1766. str += "???";
  1767. }
  1768. String DbgType::ToString(DbgLanguage language, bool allowDirectBfObject)
  1769. {
  1770. String str;
  1771. ToString(str, language, allowDirectBfObject, false);
  1772. return str;
  1773. }
  1774. intptr DbgType::GetByteCount()
  1775. {
  1776. if (!mSizeCalculated)
  1777. {
  1778. PopulateType();
  1779. if ((mSize == 0) && (GetLanguage() == DbgLanguage_Beef))
  1780. CalcExtType();
  1781. if ((mTypeCode == DbgType_Struct) || (mTypeCode == DbgType_Class) || (mTypeCode == DbgType_Union))
  1782. {
  1783. if (mPriority <= DbgTypePriority_Normal)
  1784. {
  1785. auto primaryType = GetPrimaryType();
  1786. if (primaryType != this)
  1787. {
  1788. mSize = primaryType->GetByteCount();
  1789. mAlign = primaryType->mAlign;
  1790. }
  1791. }
  1792. }
  1793. else if ((mTypeCode == DbgType_Ref) || (mTypeCode == DbgType_Ptr) || (mTypeCode == DbgType_PtrToMember))
  1794. {
  1795. #ifdef BF_DBG_32
  1796. mSize = 4;
  1797. #else
  1798. mSize = 8;
  1799. #endif
  1800. }
  1801. else if (mTypeCode == DbgType_SizedArray)
  1802. {
  1803. auto language = GetLanguage();
  1804. if (language == DbgLanguage_Beef)
  1805. {
  1806. if (mTypeParam->mAlign == 0)
  1807. {
  1808. NOP;
  1809. }
  1810. auto primaryType = mTypeParam->GetPrimaryType();
  1811. if (primaryType->mAlign == 0)
  1812. {
  1813. NOP;
  1814. }
  1815. else
  1816. {
  1817. intptr elemCount = BF_ALIGN(mSize, primaryType->mAlign) / primaryType->GetStride();
  1818. if (elemCount > 0)
  1819. {
  1820. mSize = ((elemCount - 1) * primaryType->GetStride()) + primaryType->GetByteCount();
  1821. }
  1822. }
  1823. mAlign = primaryType->mAlign;
  1824. }
  1825. }
  1826. else if (mTypeParam != NULL) // typedef, const, volatile, restrict, etc
  1827. mSize = mTypeParam->GetByteCount();
  1828. mSizeCalculated = true;
  1829. }
  1830. return mSize;
  1831. }
  1832. intptr DbgType::GetStride()
  1833. {
  1834. return BF_ALIGN(GetByteCount(), GetAlign());
  1835. }
  1836. int DbgType::GetAlign()
  1837. {
  1838. if (mAlign == 0)
  1839. {
  1840. auto primaryType = GetPrimaryType();
  1841. if (primaryType != this)
  1842. return primaryType->GetAlign();
  1843. if (IsCompositeType())
  1844. {
  1845. PopulateType();
  1846. }
  1847. }
  1848. if (mAlign != 0)
  1849. return mAlign;
  1850. return 1;
  1851. }
  1852. void DbgType::EnsureMethodsMapped()
  1853. {
  1854. for (auto methodNameEntry : mMethodNameList)
  1855. {
  1856. if (methodNameEntry->mCompileUnitId != -1)
  1857. {
  1858. mCompileUnit->mDbgModule->MapCompileUnitMethods(methodNameEntry->mCompileUnitId);
  1859. methodNameEntry->mCompileUnitId = -1;
  1860. }
  1861. }
  1862. }
  1863. #define CREATE_PRIMITIVE_C(typeCode, cTypeName, type) \
  1864. dbgType = mAlloc.Alloc<DbgType>(); \
  1865. dbgType->mCompileUnit = &mDefaultCompileUnit; \
  1866. dbgType->mName = cTypeName; \
  1867. dbgType->mLanguage = DbgLanguage_C;\
  1868. dbgType->mTypeName = cTypeName; \
  1869. dbgType->mTypeCode = typeCode; \
  1870. dbgType->mSize = sizeof(type); \
  1871. dbgType->mAlign = sizeof(type); \
  1872. mCPrimitiveTypes[typeCode] = dbgType; \
  1873. mTypeMap.Insert(dbgType);
  1874. #define CREATE_PRIMITIVE(typeCode, cTypeName, bfTypeName, structName, type) \
  1875. dbgType = mAlloc.Alloc<DbgType>(); \
  1876. dbgType->mCompileUnit = &mDefaultCompileUnit; \
  1877. dbgType->mName = cTypeName; \
  1878. dbgType->mLanguage = DbgLanguage_C;\
  1879. dbgType->mTypeName = cTypeName; \
  1880. dbgType->mTypeCode = typeCode; \
  1881. dbgType->mSize = sizeof(type); \
  1882. dbgType->mAlign = sizeof(type); \
  1883. mCPrimitiveTypes[typeCode] = dbgType; \
  1884. mTypeMap.Insert(dbgType); \
  1885. dbgType = mAlloc.Alloc<DbgType>(); \
  1886. dbgType->mCompileUnit = &mDefaultCompileUnit; \
  1887. dbgType->mName = bfTypeName; \
  1888. dbgType->mLanguage = DbgLanguage_Beef;\
  1889. dbgType->mTypeName = bfTypeName; \
  1890. dbgType->mTypeCode = typeCode; \
  1891. dbgType->mSize = sizeof(type); \
  1892. dbgType->mAlign = sizeof(type); \
  1893. mBfPrimitiveTypes[typeCode] = dbgType; \
  1894. mPrimitiveStructNames[typeCode] = structName; \
  1895. mTypeMap.Insert(dbgType);
  1896. DbgModule::DbgModule(DebugTarget* debugTarget) : mDefaultCompileUnit(this)
  1897. {
  1898. mMemReporter = NULL;
  1899. mLoadState = DbgModuleLoadState_NotLoaded;
  1900. mMappedImageFile = NULL;
  1901. mEntryPoint = 0;
  1902. mFailMsgPtr = NULL;
  1903. mFailed = false;
  1904. for (int i = 0; i < DbgType_COUNT; i++)
  1905. {
  1906. mBfPrimitiveTypes[i] = NULL;
  1907. mCPrimitiveTypes[i] = NULL;
  1908. mPrimitiveStructNames[i] = NULL;
  1909. }
  1910. DbgType* dbgType;
  1911. mDefaultCompileUnit.mLanguage = DbgLanguage_Beef;
  1912. mDefaultCompileUnit.mDbgModule = this;
  1913. if (debugTarget != NULL)
  1914. {
  1915. // These are 'alias' definitions for C, but get overwritten by their official
  1916. // stdint.h versions (ie: int8_t)
  1917. CREATE_PRIMITIVE_C(DbgType_i8, "int8", int8);
  1918. CREATE_PRIMITIVE_C(DbgType_i16, "int16", int16);
  1919. CREATE_PRIMITIVE_C(DbgType_i32, "int32", int32);
  1920. CREATE_PRIMITIVE_C(DbgType_i64, "int64", int64);
  1921. CREATE_PRIMITIVE_C(DbgType_i8, "uint8", uint8);
  1922. CREATE_PRIMITIVE_C(DbgType_i16, "uint16", uint16);
  1923. CREATE_PRIMITIVE_C(DbgType_i32, "uint32", uint32);
  1924. CREATE_PRIMITIVE_C(DbgType_i64, "uint64", uint64);
  1925. CREATE_PRIMITIVE(DbgType_Void, "void", "void", "void", void*);
  1926. dbgType->mSize = 0;
  1927. dbgType->mAlign = 0;
  1928. CREATE_PRIMITIVE(DbgType_Null, "null", "null", "null", void*);
  1929. CREATE_PRIMITIVE(DbgType_IntPtr_Alias, "intptr_t", "int", "System.Int", intptr_target);
  1930. CREATE_PRIMITIVE(DbgType_UIntPtr_Alias, "uintptr_t", "uint", "System.UInt", addr_target);
  1931. CREATE_PRIMITIVE(DbgType_SChar, "char", "char", "System.Char", char);
  1932. CREATE_PRIMITIVE(DbgType_SChar16, "wchar_t", "wchar", "System.Char16", wchar_t);
  1933. CREATE_PRIMITIVE(DbgType_i8, "int8_t", "int8", "System.SByte", int8);
  1934. CREATE_PRIMITIVE(DbgType_i16, "short", "int16", "System.Int16", int16);
  1935. CREATE_PRIMITIVE(DbgType_i32, "int", "int32", "System.Int32", int32);
  1936. CREATE_PRIMITIVE(DbgType_i64, "int64_t", "int64", "System.Int64", int64);
  1937. CREATE_PRIMITIVE(DbgType_u8, "uint8_t", "uint8", "System.UInt8", uint8);
  1938. CREATE_PRIMITIVE(DbgType_u16, "uint16_t", "uint16", "System.UInt16", uint16);
  1939. CREATE_PRIMITIVE(DbgType_u32, "uint32_t", "uint32", "System.UInt32", uint32);
  1940. CREATE_PRIMITIVE(DbgType_u64, "uint64_t", "uint64", "System.UInt64", uint64);
  1941. CREATE_PRIMITIVE(DbgType_Single, "float", "float", "System.Single", float);
  1942. CREATE_PRIMITIVE(DbgType_Double, "double", "double", "System.Double", double);
  1943. CREATE_PRIMITIVE(DbgType_UChar, "char8", "char8", "System.Char", char);
  1944. CREATE_PRIMITIVE(DbgType_UChar16, "char16", "char16", "System.Char16", short);
  1945. CREATE_PRIMITIVE(DbgType_UChar32, "char32", "char32", "System.Char32", int);
  1946. CREATE_PRIMITIVE(DbgType_Bool, "bool", "bool", "System.Boolean", bool);
  1947. CREATE_PRIMITIVE(DbgType_Subroutine, "@Func", "@Func", "@Func", bool);
  1948. CREATE_PRIMITIVE(DbgType_RawText, "@RawText", "@RawText", "@RawText", bool);
  1949. CREATE_PRIMITIVE(DbgType_RegGroup, "@RegGroup", "@RegGroup", "@RegGroup", void*);
  1950. CREATE_PRIMITIVE_C(DbgType_i16, "int16_t", int16_t);
  1951. CREATE_PRIMITIVE_C(DbgType_i32, "int32_t", int32_t);
  1952. CREATE_PRIMITIVE_C(DbgType_i64, "__int64", int64);
  1953. CREATE_PRIMITIVE_C(DbgType_u64, "unsigned __int64", uint64);
  1954. CREATE_PRIMITIVE_C(DbgType_u8, "unsigned char", uint8);
  1955. CREATE_PRIMITIVE_C(DbgType_u16, "unsigned short", uint16);
  1956. CREATE_PRIMITIVE_C(DbgType_u32, "unsigned int", uint32);
  1957. CREATE_PRIMITIVE_C(DbgType_u32, "unsigned int32_t", uint32_t);
  1958. CREATE_PRIMITIVE_C(DbgType_u32, "unsigned long", uint32);
  1959. CREATE_PRIMITIVE_C(DbgType_u64, "unsigned int64_t", uint64);
  1960. }
  1961. mIsDwarf64 = false;
  1962. mDebugTarget = debugTarget;
  1963. if (debugTarget != NULL)
  1964. mDebugger = debugTarget->mDebugger;
  1965. else
  1966. mDebugger = NULL;
  1967. mDebugLineData = NULL;
  1968. mDebugInfoData = NULL;
  1969. mDebugPubNames = NULL;
  1970. mDebugFrameAddress = 0;
  1971. mDebugFrameData = NULL;
  1972. mDebugLocationData = NULL;
  1973. mDebugRangesData = NULL;
  1974. mDebugAbbrevData = NULL;
  1975. mDebugStrData = NULL;
  1976. mDebugAbbrevPtrData = NULL;
  1977. mEHFrameData = NULL;
  1978. mEHFrameAddress = 0;
  1979. mStringTable = NULL;
  1980. mSymbolData = NULL;
  1981. mCheckedBfObject = false;
  1982. mBfObjectHasFlags = false;
  1983. mModuleKind = DbgModuleKind_Module;
  1984. mStartTypeIdx = 0;
  1985. mEndTypeIdx = 0;
  1986. mHotIdx = 0;
  1987. mId = 0;
  1988. mStartSubprogramIdx = 0;
  1989. mEndSubprogramIdx = 0;
  1990. mCodeAddress = NULL;
  1991. mMayBeOld = false;
  1992. mTimeStamp = 0;
  1993. mExpectedFileSize = 0;
  1994. mBfTypeType = NULL;
  1995. mBfTypesInfoAddr = 0;
  1996. mImageBase = 0;
  1997. mPreferredImageBase = 0;
  1998. mImageSize = 0;
  1999. mOrigImageData = NULL;
  2000. mDeleting = false;
  2001. mAllocSizeData = 0;
  2002. mParsedSymbolData = false;
  2003. mParsedTypeData = false;
  2004. mParsedGlobalsData = false;
  2005. mPopulatedStaticVariables = false;
  2006. mParsedFrameDescriptors = false;
  2007. mTLSAddr = 0;
  2008. mTLSSize = 0;
  2009. mTLSExtraAddr = 0;
  2010. mTLSExtraSize = 0;
  2011. mTLSIndexAddr = 0;
  2012. mDbgFlavor = DbgFlavor_Unknown;
  2013. mMasterCompileUnit = NULL;
  2014. }
  2015. DbgModule::~DbgModule()
  2016. {
  2017. delete mMemReporter;
  2018. for (auto dwSrcFile : mEmptySrcFiles)
  2019. delete dwSrcFile;
  2020. for (auto dwCompileUnit : mCompileUnits)
  2021. delete dwCompileUnit;
  2022. delete mSymbolData;
  2023. delete mStringTable;
  2024. delete mDebugLineData;
  2025. delete mDebugInfoData;
  2026. delete mDebugPubNames;
  2027. delete mDebugFrameData;
  2028. delete mDebugLocationData;
  2029. delete mDebugRangesData;
  2030. delete mDebugAbbrevData;
  2031. delete mDebugAbbrevPtrData;
  2032. delete mDebugStrData;
  2033. for (auto entry : mExceptionDirectory)
  2034. delete entry.mData;
  2035. delete mEHFrameData;
  2036. delete mOrigImageData;
  2037. if ((IsObjectFile()) && (mImageBase != 0))
  2038. {
  2039. mDebugger->ReleaseHotTargetMemory((addr_target)mImageBase, (int)mImageSize);
  2040. }
  2041. for (auto data : mOwnedSectionData)
  2042. delete data;
  2043. }
  2044. DbgSubprogram* DbgModule::FindSubprogram(DbgType* dbgType, const char * methodName)
  2045. {
  2046. dbgType = dbgType->GetPrimaryType();
  2047. dbgType->PopulateType();
  2048. if (dbgType->mNeedsGlobalsPopulated)
  2049. PopulateTypeGlobals(dbgType);
  2050. for (auto methodNameEntry : dbgType->mMethodNameList)
  2051. {
  2052. if ((methodNameEntry->mCompileUnitId != -1) && (strcmp(methodNameEntry->mName, methodName) == 0))
  2053. {
  2054. // If we hot-replaced this type then we replaced and parsed all the methods too
  2055. if (!dbgType->mCompileUnit->mDbgModule->IsObjectFile())
  2056. dbgType->mCompileUnit->mDbgModule->MapCompileUnitMethods(methodNameEntry->mCompileUnitId);
  2057. methodNameEntry->mCompileUnitId = -1;
  2058. }
  2059. }
  2060. DbgSubprogram* result = NULL;
  2061. for (auto method : dbgType->mMethodList)
  2062. {
  2063. if (strcmp(method->mName, methodName) == 0)
  2064. {
  2065. method->PopulateSubprogram();
  2066. if ((result == NULL) || (method->mBlock.mLowPC != 0))
  2067. result = method;
  2068. }
  2069. }
  2070. return result;
  2071. }
  2072. void DbgModule::Fail(const StringImpl& error)
  2073. {
  2074. if (mFailMsgPtr != NULL)
  2075. {
  2076. if (mFailMsgPtr->IsEmpty())
  2077. *mFailMsgPtr = error;
  2078. }
  2079. String errorStr = "error ";
  2080. if (!mFilePath.IsEmpty())
  2081. {
  2082. errorStr += "Error in ";
  2083. errorStr += mFilePath;
  2084. errorStr += ": ";
  2085. }
  2086. errorStr += error;
  2087. errorStr += "\n";
  2088. mDebugger->OutputRawMessage(errorStr);
  2089. mFailed = true;
  2090. }
  2091. void DbgModule::SoftFail(const StringImpl& error)
  2092. {
  2093. if (mFailMsgPtr != NULL)
  2094. {
  2095. if (mFailMsgPtr->IsEmpty())
  2096. *mFailMsgPtr = error;
  2097. }
  2098. String errorStr = "errorsoft ";
  2099. if (!mFilePath.IsEmpty())
  2100. {
  2101. errorStr += "Error in ";
  2102. errorStr += mFilePath;
  2103. errorStr += ": ";
  2104. }
  2105. errorStr += error;
  2106. errorStr += "\n";
  2107. mDebugger->OutputRawMessage(errorStr);
  2108. mFailed = true;
  2109. }
  2110. void DbgModule::HardFail(const StringImpl& error)
  2111. {
  2112. if (mFailMsgPtr != NULL)
  2113. {
  2114. if (mFailMsgPtr->IsEmpty())
  2115. *mFailMsgPtr = error;
  2116. }
  2117. String errorStr;
  2118. if (!mFilePath.IsEmpty())
  2119. {
  2120. errorStr += "Error in ";
  2121. errorStr += mFilePath;
  2122. errorStr += ": ";
  2123. }
  2124. errorStr += error;
  2125. errorStr += "\n";
  2126. BF_FATAL(errorStr.c_str());
  2127. }
  2128. char* DbgModule::DbgDupString(const char* str, const char* allocName)
  2129. {
  2130. int strLen = (int)strlen(str);
  2131. if (strLen == 0)
  2132. return NULL;
  2133. char* dupStr = (char*)mAlloc.AllocBytes(strLen + 1, (allocName != NULL) ? allocName : "DbgDupString");
  2134. memcpy(dupStr, str, strLen);
  2135. return dupStr;
  2136. }
  2137. DbgModule* DbgModule::GetLinkedModule()
  2138. {
  2139. if (IsObjectFile())
  2140. return mDebugTarget->mTargetBinary;
  2141. return this;
  2142. }
  2143. addr_target DbgModule::GetTargetImageBase()
  2144. {
  2145. if (IsObjectFile())
  2146. return (addr_target)mDebugTarget->mTargetBinary->mImageBase;
  2147. return (addr_target)mImageBase;
  2148. }
  2149. void DbgModule::ParseGlobalsData()
  2150. {
  2151. mParsedGlobalsData = true;
  2152. }
  2153. void DbgModule::ParseSymbolData()
  2154. {
  2155. mParsedSymbolData = true;
  2156. }
  2157. void DbgModule::ParseTypeData()
  2158. {
  2159. mParsedTypeData = true;
  2160. }
  2161. DbgCompileUnit* DbgModule::ParseCompileUnit(int compileUnitId)
  2162. {
  2163. return NULL;
  2164. }
  2165. void DbgModule::MapCompileUnitMethods(DbgCompileUnit * compileUnit)
  2166. {
  2167. }
  2168. void DbgModule::MapCompileUnitMethods(int compileUnitId)
  2169. {
  2170. }
  2171. void DbgModule::PopulateType(DbgType* dbgType)
  2172. {
  2173. }
  2174. void DbgModule::PopulateTypeGlobals(DbgType* dbgType)
  2175. {
  2176. }
  2177. void DbgModule::PopulateStaticVariableMap()
  2178. {
  2179. if (mPopulatedStaticVariables)
  2180. return;
  2181. for (auto staticVariable : mStaticVariables)
  2182. {
  2183. mStaticVariableMap[staticVariable->GetMappedName()] = staticVariable;
  2184. }
  2185. mPopulatedStaticVariables = true;
  2186. }
  2187. void DbgModule::ProcessDebugInfo()
  2188. {
  2189. }
  2190. addr_target DbgModule::RemapAddr(addr_target addr)
  2191. {
  2192. if ((addr != 0) && (mPreferredImageBase != 0) && (mImageBase != 0))
  2193. return addr + (intptr_target)(mImageBase - mPreferredImageBase);
  2194. return addr;
  2195. }
  2196. void DbgModule::ParseAbbrevData(const uint8* data)
  2197. {
  2198. while (true)
  2199. {
  2200. int abbrevIdx = (int)DecodeULEB128(data);
  2201. mDebugAbbrevPtrData[abbrevIdx] = data;
  2202. if (abbrevIdx == 0)
  2203. break;
  2204. int entryTag = (int)DecodeULEB128(data);
  2205. bool hasChildren = GET(char) == DW_CHILDREN_yes;
  2206. while (true)
  2207. {
  2208. int attrName = (int)DecodeULEB128(data);
  2209. int form = (int)DecodeULEB128(data);
  2210. if ((attrName == 0) && (form == 0))
  2211. break;
  2212. }
  2213. }
  2214. }
  2215. void DbgModule::ParseExceptionData()
  2216. {
  2217. if (mExceptionDirectory.IsEmpty())
  2218. return;
  2219. BP_ZONE("DbgModule::ParseExceptionData");
  2220. for (auto entry : mExceptionDirectory)
  2221. {
  2222. const uint8* data = entry.mData;
  2223. const uint8* dataEnd = data + entry.mSize;
  2224. static int entryCount = 0;
  2225. addr_target imageBase = GetTargetImageBase();
  2226. while (data < dataEnd)
  2227. {
  2228. addr_target beginAddress = GET(uint32);
  2229. addr_target endAddress = GET(uint32);
  2230. uint32 unwindData = GET(uint32);
  2231. //TODO: Apparently unwindData can refer to another runtime entry in the .pdata if the LSB is set to 1?
  2232. beginAddress += (addr_target)imageBase;
  2233. endAddress += (addr_target)imageBase;
  2234. int exSize = (int)(endAddress - beginAddress);
  2235. for (int exOffset = 0; true; exOffset += DBG_MAX_LOOKBACK)
  2236. {
  2237. int curSize = exSize - exOffset;
  2238. if (curSize <= 0)
  2239. break;
  2240. BP_ALLOC_T(DbgExceptionDirectoryEntry);
  2241. DbgExceptionDirectoryEntry* exceptionDirectoryEntry = mAlloc.Alloc<DbgExceptionDirectoryEntry>();
  2242. exceptionDirectoryEntry->mAddress = beginAddress + exOffset;
  2243. exceptionDirectoryEntry->mOrigAddressOffset = exOffset;
  2244. exceptionDirectoryEntry->mAddressLength = curSize;
  2245. exceptionDirectoryEntry->mExceptionPos = (int)unwindData;
  2246. exceptionDirectoryEntry->mDbgModule = this;
  2247. mDebugTarget->mExceptionDirectoryMap.Insert(exceptionDirectoryEntry);
  2248. entryCount++;
  2249. }
  2250. }
  2251. }
  2252. }
  2253. static int gIdx = 0;
  2254. template <typename T> static bool IsTypeSigned() { return false; }
  2255. template <> bool IsTypeSigned<int8>() { return true; }
  2256. template <> bool IsTypeSigned<int16>() { return true; }
  2257. template <> bool IsTypeSigned<int32>() { return true; }
  2258. template <> bool IsTypeSigned<int64>() { return true; }
  2259. #pragma warning(push)
  2260. #pragma warning(disable:4302)
  2261. #pragma warning(disable:4311)
  2262. #pragma warning(disable:4312)
  2263. #pragma warning(disable:4800)
  2264. #pragma warning(disable:4800)
  2265. template <typename T>
  2266. T DbgModule::ReadValue(const uint8*& data, int form, int refOffset, const uint8** extraData, const uint8* startData)
  2267. {
  2268. gIdx++;
  2269. switch (form)
  2270. {
  2271. case DW_FORM_strp:
  2272. {
  2273. int strOffset = GET(int);
  2274. BF_ASSERT(mDebugStrData != NULL);
  2275. const char* str = (const char*)mDebugStrData + strOffset;
  2276. return (T)(intptr)str;
  2277. }
  2278. break;
  2279. case DW_FORM_data1:
  2280. {
  2281. if (IsTypeSigned<T>())
  2282. return (T)(intptr)GET(int8);
  2283. else
  2284. return (T)(uintptr)GET(uint8);
  2285. }
  2286. break;
  2287. case DW_FORM_data2:
  2288. {
  2289. if (IsTypeSigned<T>())
  2290. return (T)(intptr)GET(int16);
  2291. else
  2292. return (T)(uintptr)GET(uint16);
  2293. }
  2294. break;
  2295. case DW_FORM_data4:
  2296. {
  2297. if (IsTypeSigned<T>())
  2298. return (T)(intptr)GET(int32);
  2299. else
  2300. return (T)(uintptr)GET(uint32);
  2301. }
  2302. break;
  2303. case DW_FORM_data8:
  2304. {
  2305. if (IsTypeSigned<T>())
  2306. return (T)GET(int64);
  2307. else
  2308. return (T)GET(uint64);
  2309. }
  2310. break;
  2311. case DW_FORM_ref1:
  2312. {
  2313. return (T)(intptr)GET(int8) + refOffset;
  2314. }
  2315. break;
  2316. case DW_FORM_ref2:
  2317. {
  2318. return (T)(intptr)GET(int16) + refOffset;
  2319. }
  2320. break;
  2321. case DW_FORM_ref4:
  2322. {
  2323. return (T)(intptr)GET(int32) + refOffset;
  2324. }
  2325. break;
  2326. case DW_FORM_sec_offset:
  2327. {
  2328. intptr_target offset;
  2329. if (mIsDwarf64)
  2330. offset = (intptr_target)GET(int64);
  2331. else
  2332. offset = GET(int32);
  2333. if (extraData != NULL)
  2334. {
  2335. *extraData = mDebugLocationData + offset;
  2336. return 0;
  2337. }
  2338. return (T)offset;
  2339. }
  2340. break;
  2341. case DW_FORM_addr:
  2342. {
  2343. return (T)GET(addr_target);
  2344. }
  2345. break;
  2346. case DW_FORM_exprloc:
  2347. {
  2348. int64_t exprLen = DecodeULEB128(data);
  2349. const uint8* endData = data + exprLen;
  2350. if (extraData != NULL)
  2351. *extraData = data;
  2352. data = endData;
  2353. return (T)exprLen;
  2354. }
  2355. break;
  2356. case DW_FORM_flag_present:
  2357. {
  2358. //
  2359. return (T)1;
  2360. }
  2361. break;
  2362. case DW_FORM_flag:
  2363. {
  2364. //
  2365. return (T)(intptr)GET(char);
  2366. }
  2367. break;
  2368. case DW_FORM_sdata:
  2369. return (T)DecodeSLEB128(data);
  2370. case DW_FORM_udata:
  2371. return (T)DecodeULEB128(data);
  2372. case DW_FORM_string:
  2373. {
  2374. const char* str = (const char*)data;
  2375. while (true)
  2376. {
  2377. uint8 val = *data;
  2378. data++;
  2379. if (val == 0)
  2380. return (T)(intptr)str;
  2381. }
  2382. }
  2383. case DW_FORM_block:
  2384. {
  2385. int blockLen = (int)DecodeULEB128(data);
  2386. const uint8* retVal = data;
  2387. data += blockLen;
  2388. return (T)(intptr)retVal;
  2389. }
  2390. case DW_FORM_block1:
  2391. {
  2392. int blockLen = (int)*((uint8*)data);
  2393. data += sizeof(uint8);
  2394. const uint8* retVal = data;
  2395. data += blockLen;
  2396. return (T)(intptr)retVal;
  2397. }
  2398. default:
  2399. assert("Not covered!" == 0);
  2400. break;
  2401. }
  2402. return (T)0;
  2403. }
  2404. #pragma warning(pop)
  2405. static int gAbbrevNum = 0;
  2406. DbgType* DbgModule::GetOrCreateType(int typeIdx, DbgDataMap& dataMap)
  2407. {
  2408. if (typeIdx == 0)
  2409. return NULL;
  2410. DbgModule* linkedModule = GetLinkedModule();
  2411. DbgType* dbgType = dataMap.Get<DbgType*>(typeIdx);
  2412. if (dbgType != NULL)
  2413. return dbgType;
  2414. dbgType = mAlloc.Alloc<DbgType>();
  2415. dbgType->mTypeIdx = (int)linkedModule->mTypes.size();
  2416. linkedModule->mTypes.push_back(dbgType);
  2417. dataMap.Set(typeIdx, dbgType);
  2418. return dbgType;
  2419. }
  2420. typedef std::pair<DbgClassType, void*> DataPair;
  2421. typedef llvm::SmallVector<DataPair, 16> DataStack;
  2422. template <typename T>
  2423. T DbgModule::GetOrCreate(int idx, DbgDataMap& dataMap)
  2424. {
  2425. if (idx == 0)
  2426. return NULL;
  2427. T val = dataMap.Get<T>(idx);
  2428. if (val != NULL)
  2429. return val;
  2430. val = mAlloc.Alloc<typename RemoveTypePointer<T>::type >();
  2431. dataMap.Set(idx, val);
  2432. return val;
  2433. }
  2434. template <typename T>
  2435. static T GetStackTop(DataStack* dataStack)
  2436. {
  2437. auto dataPair = dataStack->back();
  2438. if (dataPair.first == RemoveTypePointer<T>::type::ClassType)
  2439. return (T)dataPair.second;
  2440. return NULL;
  2441. }
  2442. template <>
  2443. DbgBlock* GetStackTop<DbgBlock*>(DataStack* dataStack)
  2444. {
  2445. auto dataPair = dataStack->back();
  2446. if (dataPair.first == DbgBlock::ClassType)
  2447. return (DbgBlock*)dataPair.second;
  2448. if (dataPair.first == DbgSubprogram::ClassType)
  2449. return &((DbgSubprogram*)dataPair.second)->mBlock;
  2450. if (dataPair.first == DbgType::ClassType)
  2451. return ((DbgType*)dataPair.second)->mBlockParam;
  2452. return NULL;
  2453. }
  2454. template <typename T>
  2455. static bool StackHasType(DataStack* dataStack)
  2456. {
  2457. for (auto itr : *dataStack)
  2458. if (itr.first == RemoveTypePointer<T>::type::ClassType)
  2459. return true;
  2460. return false;
  2461. }
  2462. template <typename T>
  2463. static T GetStackLast(DataStack* dataStack)
  2464. {
  2465. for (int i = (int)dataStack->size() - 1; i >= 0; i--)
  2466. {
  2467. if ((*dataStack)[i].first == RemoveTypePointer<T>::type::ClassType)
  2468. return (T)(*dataStack)[i].second;
  2469. }
  2470. return NULL;
  2471. }
  2472. template <typename T>
  2473. static DataPair MakeDataPair(T* data)
  2474. {
  2475. return DataPair(T::ClassType, data);
  2476. }
  2477. void DbgModule::FixupInnerTypes(int startingTypeIdx)
  2478. {
  2479. BP_ZONE("DbgModule_FixupInnerTypes");
  2480. for (int typeIdx = startingTypeIdx; typeIdx < (int)mTypes.size(); typeIdx++)
  2481. {
  2482. DbgType* dbgType = mTypes[typeIdx];
  2483. if ((dbgType->mPriority == DbgTypePriority_Primary_Implicit) && (dbgType->mParent != NULL) && (dbgType->mParent->mTypeCode != DbgType_Namespace) &&
  2484. (dbgType->mParent->mPriority <= DbgTypePriority_Primary_Implicit))
  2485. {
  2486. auto primaryParent = dbgType->mParent->GetPrimaryType();
  2487. dbgType->mParent->mSubTypeList.Clear();
  2488. dbgType->mParent = primaryParent;
  2489. primaryParent->mSubTypeList.PushBack(dbgType);
  2490. }
  2491. }
  2492. }
  2493. void DbgModule::MapTypes(int startingTypeIdx)
  2494. {
  2495. BP_ZONE("DbgModule_MapTypes");
  2496. bool needsInnerFixups = false;
  2497. for (int typeIdx = startingTypeIdx; typeIdx < (int)mTypes.size(); typeIdx++)
  2498. {
  2499. DbgType* dbgType = mTypes[typeIdx];
  2500. BF_ASSERT(dbgType->mTypeCode != DbgType_Null);
  2501. if ((dbgType->mTypeCode == DbgType_Namespace) && (dbgType->mPriority < DbgTypePriority_Primary_Implicit))
  2502. continue;
  2503. //TODO: Always valid?
  2504. if (dbgType->mIsDeclaration)
  2505. continue;
  2506. // We were avoiding adding '<' names before, but that made it impossible to look up auto-named primary types ,
  2507. // like in-place unions like '<unnamed-type-u>'
  2508. if ((dbgType->mTypeName == NULL) || (dbgType->mName == NULL) /*|| (dbgType->mTypeName[0] == '<')*/)
  2509. continue;
  2510. if (dbgType->mTypeCode > DbgType_DefinitionEnd)
  2511. {
  2512. // Only add "definition types"
  2513. continue;
  2514. }
  2515. if (dbgType->mTypeCode == DbgType_Namespace)
  2516. {
  2517. bool isQualifiedNamespace = false;
  2518. for (const char* cPtr = dbgType->mTypeName; *cPtr != '\0'; cPtr++)
  2519. if (*cPtr == '.')
  2520. isQualifiedNamespace = true;
  2521. if (isQualifiedNamespace)
  2522. continue; // Don't add fully qualified namespaces (they come from the 'using' implementation)*
  2523. }
  2524. if (dbgType->mHasStaticMembers)
  2525. {
  2526. for (auto member : dbgType->mMemberList)
  2527. if ((member->mIsStatic) && (member->mLocationData != NULL))
  2528. dbgType->mDefinedMembersSize++;
  2529. }
  2530. if ((dbgType->mTypeName != NULL) && (strcmp(dbgType->mTypeName, "@") == 0))
  2531. {
  2532. // Globals type.
  2533. continue;
  2534. }
  2535. auto prevTypeEntry = FindType(dbgType->mName, dbgType->mLanguage);
  2536. // Only replace previous instance if its a declaration
  2537. if (prevTypeEntry != NULL)
  2538. {
  2539. auto prevType = prevTypeEntry->mValue;
  2540. if (dbgType->mCompileUnit->mDbgModule != prevType->mCompileUnit->mDbgModule)
  2541. {
  2542. // Don't replace original types with hot types -- those need to be inserted in the the hot alternates list
  2543. BF_ASSERT(dbgType->mCompileUnit->mDbgModule->IsObjectFile());
  2544. prevType->mHotNewType = dbgType;
  2545. continue;
  2546. }
  2547. // Never override explicit primaries
  2548. if (prevType->mPriority == DbgTypePriority_Primary_Explicit)
  2549. continue;
  2550. if (dbgType->mTypeCode == DbgType_TypeDef)
  2551. {
  2552. // Typedef can never override anything
  2553. continue;
  2554. }
  2555. if (prevType->mTypeCode == DbgType_TypeDef)
  2556. {
  2557. if (dbgType->mTypeCode != DbgType_TypeDef)
  2558. {
  2559. // Allow this to override
  2560. prevTypeEntry->mValue = dbgType;
  2561. }
  2562. continue;
  2563. }
  2564. // Don't replace a ptr to an BfObject with a BfObject
  2565. if ((prevType->mTypeCode == DbgType_Ptr) && (dbgType->mTypeCode == DbgType_Struct))
  2566. continue;
  2567. if ((prevType->mTypeCode == DbgType_Struct) && (dbgType->mTypeCode == DbgType_Ptr))
  2568. {
  2569. // Allow this to override
  2570. prevTypeEntry->mValue = dbgType;
  2571. continue;
  2572. }
  2573. if (prevType->mTypeCode == DbgType_Namespace)
  2574. {
  2575. if (dbgType->mTypeCode != DbgType_Namespace)
  2576. {
  2577. // Old type was namespace but new isn't? Replace old type.
  2578. while (!prevType->mSubTypeList.IsEmpty())
  2579. {
  2580. DbgType* subType = prevType->mSubTypeList.PopFront();
  2581. subType->mParent = dbgType;
  2582. dbgType->mSubTypeList.PushBack(subType);
  2583. }
  2584. prevType->mPriority = DbgTypePriority_Normal;
  2585. if (dbgType->mPriority < DbgTypePriority_Primary_Implicit)
  2586. dbgType->mPriority = DbgTypePriority_Primary_Implicit;
  2587. prevTypeEntry->mValue = dbgType;
  2588. continue;
  2589. }
  2590. // We definitely didn't want to do this for MS. For DWARF?
  2591. //prevType->mAlternates.PushFront(dbgType, &mAlloc);
  2592. continue;
  2593. }
  2594. else
  2595. {
  2596. // New type is namespace but old wasn't? Ignore new type.
  2597. if (dbgType->mTypeCode == DbgType_Namespace)
  2598. continue;
  2599. if (dbgType->mIsDeclaration)
  2600. continue;
  2601. if (!prevType->mIsDeclaration)
  2602. {
  2603. if ((prevType->mCompileUnit == NULL) || (dbgType->mLanguage < prevType->mLanguage))
  2604. {
  2605. // We always want 'Beef' types to supersede 'C' types, but don't override the built-in primitive types
  2606. continue;
  2607. }
  2608. if (prevType->mDefinedMembersSize > 0)
  2609. {
  2610. if (dbgType->mDefinedMembersSize > 0)
  2611. {
  2612. // We create an 'alternates' list for all types that define at least one static field
  2613. if (prevType->mHasStaticMembers)
  2614. prevType->mAlternates.PushFront(dbgType, &mAlloc);
  2615. }
  2616. continue;
  2617. }
  2618. // if (prevType->mDefinedMembersSize > dbgType->mDefinedMembersSize)
  2619. // {
  2620. // continue;
  2621. // }
  2622. if (prevType->mMethodsWithParamsCount > dbgType->mMethodsWithParamsCount)
  2623. {
  2624. // This handles a special case where methods without line data like <Enum>.HasFlags doesn't show containing
  2625. // params in cases where it gets inlined
  2626. continue;
  2627. }
  2628. // Types with method lists are preferred
  2629. if ((!prevType->mMethodList.IsEmpty()) && (dbgType->mMethodList.IsEmpty()))
  2630. continue;
  2631. if ((prevType->mTypeCode == DbgType_Ptr) && (prevType->mTypeParam != NULL) && (!prevType->mTypeParam->mMethodList.IsEmpty()))
  2632. continue;
  2633. }
  2634. // Replace type
  2635. if (!prevType->mSubTypeList.IsEmpty())
  2636. needsInnerFixups = true;
  2637. prevType->mPriority = DbgTypePriority_Normal;
  2638. if (dbgType->mPriority == DbgTypePriority_Normal)
  2639. dbgType->mPriority = DbgTypePriority_Primary_Implicit;
  2640. prevTypeEntry->mValue = dbgType;
  2641. continue;
  2642. }
  2643. }
  2644. if ((dbgType->mParent != NULL) && (dbgType->mParent->mTypeCode != DbgType_Namespace) && (dbgType->mParent->mPriority <= DbgTypePriority_Primary_Implicit))
  2645. needsInnerFixups = true;
  2646. if (dbgType->mPriority == DbgTypePriority_Normal)
  2647. dbgType->mPriority = DbgTypePriority_Primary_Implicit;
  2648. mTypeMap.Insert(dbgType);
  2649. }
  2650. if (needsInnerFixups)
  2651. FixupInnerTypes(startingTypeIdx);
  2652. }
  2653. void DbgModule::CreateNamespaces()
  2654. {
  2655. BP_ZONE("DbgModule::CreateNamespaces");
  2656. int startLength = (int)mTypes.size();
  2657. for (int typeIdx = 0; typeIdx < startLength; typeIdx++)
  2658. {
  2659. DbgType* dbgType = mTypes[typeIdx];
  2660. if (dbgType->mName == NULL)
  2661. continue;
  2662. if ((dbgType->mTypeCode == DbgType_Namespace) && (dbgType->mTagIdx != 0))
  2663. {
  2664. auto namespaceTypeEntry = FindType(dbgType->mName, dbgType->GetLanguage());
  2665. DbgType* namespaceType;
  2666. if (namespaceTypeEntry == NULL)
  2667. {
  2668. namespaceType = mAlloc.Alloc<DbgType>();
  2669. namespaceType->mTypeCode = DbgType_Namespace;
  2670. namespaceType->mLanguage = dbgType->mLanguage;
  2671. namespaceType->mCompileUnit = dbgType->mCompileUnit;
  2672. namespaceType->mTypeIdx = (int)mTypes.size();
  2673. namespaceType->mPriority = DbgTypePriority_Primary_Explicit;
  2674. namespaceType->mName = dbgType->mName;
  2675. namespaceType->mTypeName = dbgType->mTypeName;
  2676. if (dbgType->mParent != NULL)
  2677. {
  2678. namespaceType->mParent = dbgType->mParent->GetPrimaryType();
  2679. namespaceType->mParent->mSubTypeList.PushBack(namespaceType);
  2680. }
  2681. else
  2682. {
  2683. namespaceType->mCompileUnit->mGlobalType->mSubTypeList.PushBack(namespaceType);
  2684. }
  2685. mTypes.push_back(namespaceType);
  2686. mTypeMap.Insert(namespaceType);
  2687. }
  2688. else
  2689. namespaceType = namespaceTypeEntry->mValue;
  2690. while (!dbgType->mMemberList.IsEmpty())
  2691. {
  2692. DbgVariable* curVar = dbgType->mMemberList.PopFront();
  2693. namespaceType->mMemberList.PushBack(curVar);
  2694. }
  2695. DbgType* prevType = NULL;
  2696. DbgType* curType = dbgType->mSubTypeList.mHead;
  2697. while (curType != NULL)
  2698. {
  2699. DbgType* nextType = curType->mNext;
  2700. if (curType->mPriority >= DbgTypePriority_Primary_Implicit)
  2701. {
  2702. dbgType->mSubTypeList.Remove(curType, prevType);
  2703. namespaceType->mSubTypeList.PushBack(curType);
  2704. }
  2705. prevType = curType;
  2706. curType = nextType;
  2707. }
  2708. continue;
  2709. }
  2710. }
  2711. // If we didn't have a parent type for a namespace (ie: if System.Collections wasn't linked to System) then we wait
  2712. // until the end and move those from the global list to the parent list
  2713. for (int typeIdx = startLength; typeIdx < (int)mTypes.size(); typeIdx++)
  2714. {
  2715. DbgType* dbgType = mTypes[typeIdx];
  2716. if (dbgType->mParent != NULL)
  2717. continue;
  2718. char* typeName = (char*)dbgType->mTypeName;
  2719. int lastDotIdx = -1;
  2720. for (int i = 0; true; i++)
  2721. {
  2722. char c = typeName[i];
  2723. if (c == 0)
  2724. break;
  2725. if (c == '.')
  2726. lastDotIdx = i;
  2727. }
  2728. if (lastDotIdx == -1)
  2729. continue;
  2730. typeName[lastDotIdx] = 0;
  2731. dbgType->mTypeName = typeName + lastDotIdx + 1;
  2732. auto parentEntry = FindType(typeName, dbgType->GetLanguage());
  2733. typeName[lastDotIdx] = '.';
  2734. if (parentEntry == NULL)
  2735. continue;
  2736. auto parentType = parentEntry->mValue;
  2737. dbgType->mCompileUnit->mGlobalType->mSubTypeList.Remove(dbgType);
  2738. dbgType->mParent = parentType;
  2739. parentType->mSubTypeList.PushBack(dbgType);
  2740. }
  2741. }
  2742. void DbgModule::FindTemplateStr(const char*& name, int& templateNameIdx)
  2743. {
  2744. if (templateNameIdx == 0)
  2745. {
  2746. for (int i = 0; name[i] != 0; i++)
  2747. {
  2748. if (name[i] == '<')
  2749. {
  2750. templateNameIdx = i;
  2751. return;
  2752. }
  2753. }
  2754. templateNameIdx = -1;
  2755. }
  2756. }
  2757. void DbgModule::TempRemoveTemplateStr(const char*& name, int& templateNameIdx)
  2758. {
  2759. if (templateNameIdx == 0)
  2760. FindTemplateStr(name, templateNameIdx);
  2761. if (templateNameIdx == -1)
  2762. return;
  2763. if (!DbgIsStrMutable(name))
  2764. name = DbgDupString(name);
  2765. ((char*)name)[templateNameIdx] = 0;
  2766. }
  2767. void DbgModule::ReplaceTemplateStr(const char*& name, int& templateNameIdx)
  2768. {
  2769. if (templateNameIdx > 0)
  2770. ((char*)name)[templateNameIdx] = '<';
  2771. }
  2772. void DbgModule::MapSubprogram(DbgSubprogram* dbgSubprogram)
  2773. {
  2774. if (dbgSubprogram->mBlock.IsEmpty())
  2775. return;
  2776. int progSize = (int)(dbgSubprogram->mBlock.mHighPC - dbgSubprogram->mBlock.mLowPC);
  2777. for (int progOffset = 0; true; progOffset += DBG_MAX_LOOKBACK)
  2778. {
  2779. int curSize = progSize - progOffset;
  2780. if (curSize <= 0)
  2781. break;
  2782. BP_ALLOC_T(DbgSubprogramMapEntry);
  2783. DbgSubprogramMapEntry* subprogramMapEntry = mAlloc.Alloc<DbgSubprogramMapEntry>();
  2784. subprogramMapEntry->mAddress = dbgSubprogram->mBlock.mLowPC + progOffset;
  2785. subprogramMapEntry->mEntry = dbgSubprogram;
  2786. mDebugTarget->mSubprogramMap.Insert(subprogramMapEntry);
  2787. }
  2788. }
  2789. bool DbgModule::ParseDWARF(const uint8*& dataPtr)
  2790. {
  2791. BP_ZONE("ParseDWARF");
  2792. const uint8* data = dataPtr;
  2793. const uint8* startData = mDebugInfoData;
  2794. int dataOfs = (int)(data - mDebugInfoData);
  2795. intptr_target length = GET(int);
  2796. DbgModule* linkedModule = GetLinkedModule();
  2797. if (length == -1)
  2798. {
  2799. mIsDwarf64 = true;
  2800. length = (intptr_target)GET(int64);
  2801. }
  2802. else
  2803. mIsDwarf64 = false;
  2804. if (length == 0)
  2805. return false;
  2806. const uint8* dataEnd = data + length;
  2807. int version = GET(short);
  2808. int abbrevOffset = GET(int);
  2809. char pointerSize = GET(char);
  2810. ParseAbbrevData(mDebugAbbrevData + abbrevOffset);
  2811. DbgCompileUnit* compileUnit = new DbgCompileUnit(this);
  2812. mDbgFlavor = DbgFlavor_GNU;
  2813. compileUnit->mDbgModule = this;
  2814. mCompileUnits.push_back(compileUnit);
  2815. DbgSubprogram* subProgram = NULL;
  2816. //std::map<int, DbgType*> typeMap;
  2817. //std::map<int, DbgSubprogram*> subprogramMap;
  2818. int tagStart = (int)(data - startData);
  2819. int tagEnd = (int)(dataEnd - startData);
  2820. DbgDataMap dataMap(tagStart, tagEnd);
  2821. DataStack dataStack;
  2822. Array<AbstractOriginEntry> abstractOriginReplaceList;
  2823. Array<int> deferredArrayDims;
  2824. int startingTypeIdx = (int)linkedModule->mTypes.size();
  2825. while (data < dataEnd)
  2826. {
  2827. gAbbrevNum++;
  2828. const uint8* tagDataStart = data;
  2829. int tagIdx = (int)(tagDataStart - startData);
  2830. int abbrevIdx = (int)DecodeULEB128(data);
  2831. const uint8* abbrevData = mDebugAbbrevPtrData[abbrevIdx];
  2832. if (abbrevIdx == 0)
  2833. {
  2834. if (deferredArrayDims.size() > 0)
  2835. {
  2836. DbgType* arrType = GetStackTop<DbgType*>(&dataStack);
  2837. BF_ASSERT(arrType->mTypeCode == DbgType_SizedArray);
  2838. arrType->mSize = deferredArrayDims[0]; // Byte count still needs to be multiplied by the underlying type size
  2839. DbgType* rootArrType = arrType;
  2840. for (int dimIdx = 0; dimIdx < (int)deferredArrayDims.size() - 1; dimIdx++)
  2841. {
  2842. int dimSize = deferredArrayDims[dimIdx];
  2843. DbgType* subArrType = mAlloc.Alloc<DbgType>();
  2844. subArrType->mCompileUnit = compileUnit;
  2845. subArrType->mLanguage = compileUnit->mLanguage;
  2846. subArrType->mTypeIdx = (int)linkedModule->mTypes.size();
  2847. linkedModule->mTypes.push_back(subArrType);
  2848. subArrType->mTypeCode = DbgType_SizedArray;
  2849. subArrType->mTypeParam = arrType->mTypeParam;
  2850. subArrType->mSize = deferredArrayDims[dimIdx + 1];
  2851. arrType->mTypeParam = subArrType;
  2852. arrType = subArrType;
  2853. }
  2854. deferredArrayDims.Clear();
  2855. }
  2856. dataStack.pop_back();
  2857. continue;
  2858. }
  2859. int entryTag = (int) DecodeULEB128(abbrevData);
  2860. bool hasChildren = GET_FROM(abbrevData, char) == DW_CHILDREN_yes;
  2861. int64 atLowPC = 0;
  2862. int64 atHighPC = 0;
  2863. int64 atRanges = 0;
  2864. bool hasRanges = false;
  2865. const uint8* atFrameBase = NULL;
  2866. int64_t atFrameBaseLength = 0;
  2867. int64 atLocationLen = 0;
  2868. const uint8* atLocationData = 0;
  2869. const char* atProducer = NULL;
  2870. const char* atName = NULL;
  2871. const char* atCompDir = NULL;
  2872. const char* atLinkageName = NULL;
  2873. int64 atConstValue = 0;
  2874. int atDataMemberLocation = 0;
  2875. const uint8* atDataMemberData = NULL;
  2876. int atDeclFile = 0;
  2877. int atDeclLine = 0;
  2878. int atCallFile = 0;
  2879. int atCallLine = 0;
  2880. int atCount = 0;
  2881. int atType = 0;
  2882. int atImport = 0;
  2883. int atInline = 0;
  2884. int atArtificial = 0;
  2885. int atExternal = 0;
  2886. int atByteSize = -1;
  2887. int atEncoding = 0;
  2888. int atSpecification = 0;
  2889. int atObjectPointer = 0;
  2890. int atBitOffset = 0;
  2891. int atBitSize = 0;
  2892. int atAbstractOrigin = 0;
  2893. const uint8* atVirtualLocData = NULL;
  2894. bool atDeclaration = false;
  2895. bool atVirtual = false;
  2896. bool hadConstValue = false;
  2897. bool hadMemberLocation = false;
  2898. bool isOptimized = false;
  2899. DataPair newDataPair;
  2900. while (true)
  2901. {
  2902. int attrName = (int)DecodeULEB128(abbrevData);
  2903. int form = (int)DecodeULEB128(abbrevData);
  2904. if ((attrName == 0) && (form == 0))
  2905. break;
  2906. switch (attrName)
  2907. {
  2908. case DW_AT_sibling:
  2909. ReadValue<char>(data, form);
  2910. break;
  2911. case DW_AT_location:
  2912. atLocationLen = (int)ReadValue<uint>(data, form, dataOfs, &atLocationData, startData);
  2913. break;
  2914. case DW_AT_name:
  2915. atName = ReadValue<const char*>(data, form);
  2916. break;
  2917. case DW_AT_ordering:
  2918. /*TODO:*/ ReadValue<int>(data, form);
  2919. break;
  2920. case DW_AT_byte_size:
  2921. atByteSize = ReadValue<int>(data, form);
  2922. break;
  2923. case DW_AT_bit_offset:
  2924. atBitOffset = ReadValue<int>(data, form);
  2925. break;
  2926. case DW_AT_bit_size:
  2927. atBitSize = ReadValue<int>(data, form);
  2928. break;
  2929. case DW_AT_stmt_list:
  2930. ReadValue<int64_t>(data, form);
  2931. break;
  2932. case DW_AT_low_pc:
  2933. atLowPC = RemapAddr((addr_target)ReadValue<int64_t>(data, form));
  2934. break;
  2935. case DW_AT_high_pc:
  2936. atHighPC = ReadValue<int64_t>(data, form);
  2937. break;
  2938. case DW_AT_language:
  2939. /*TODO:*/ ReadValue<int>(data, form);
  2940. break;
  2941. case DW_AT_discr:
  2942. /*TODO:*/ ReadValue<int>(data, form);
  2943. break;
  2944. case DW_AT_discr_value:
  2945. /*TODO:*/ ReadValue<int>(data, form);
  2946. break;
  2947. case DW_AT_visibility:
  2948. /*TODO:*/ ReadValue<int>(data, form);
  2949. break;
  2950. case DW_AT_import:
  2951. atImport = ReadValue<int>(data, form) + dataOfs;
  2952. break;
  2953. case DW_AT_string_length:
  2954. /*TODO:*/ ReadValue<int>(data, form);
  2955. break;
  2956. case DW_AT_common_reference:
  2957. /*TODO:*/ ReadValue<int>(data, form);
  2958. break;
  2959. case DW_AT_comp_dir:
  2960. atCompDir = ReadValue<const char*>(data, form);
  2961. break;
  2962. case DW_AT_const_value:
  2963. atConstValue = ReadValue<int64>(data, form);
  2964. hadConstValue = true;
  2965. break;
  2966. case DW_AT_containing_type:
  2967. /*TODO:*/ ReadValue<int>(data, form);
  2968. break;
  2969. case DW_AT_default_value:
  2970. /*TODO:*/ ReadValue<int>(data, form);
  2971. break;
  2972. case DW_AT_inline:
  2973. atInline = ReadValue<int>(data, form);
  2974. break;
  2975. case DW_AT_is_optional:
  2976. /*TODO:*/ ReadValue<int>(data, form);
  2977. break;
  2978. case DW_AT_lower_bound:
  2979. /*TODO:*/ ReadValue<int>(data, form);
  2980. break;
  2981. case DW_AT_producer:
  2982. atProducer = ReadValue<const char*>(data, form);
  2983. break;
  2984. case DW_AT_prototyped:
  2985. /*TODO:*/ ReadValue<int>(data, form);
  2986. break;
  2987. case DW_AT_return_addr:
  2988. /*TODO:*/ ReadValue<int>(data, form);
  2989. break;
  2990. case DW_AT_start_scope:
  2991. /*TODO:*/ ReadValue<int>(data, form);
  2992. break;
  2993. case DW_AT_bit_stride:
  2994. /*TODO:*/ ReadValue<int>(data, form);
  2995. break;
  2996. case DW_AT_upper_bound:
  2997. // Lower bound not supported
  2998. atCount = ReadValue<int>(data, form);
  2999. break;
  3000. case DW_AT_abstract_origin:
  3001. atAbstractOrigin = ReadValue<int>(data, form, dataOfs);
  3002. break;
  3003. case DW_AT_accessibility:
  3004. /*TODO:*/ ReadValue<int>(data, form);
  3005. break;
  3006. case DW_AT_address_class:
  3007. /*TODO:*/ ReadValue<int>(data, form);
  3008. break;
  3009. case DW_AT_artificial:
  3010. atArtificial = ReadValue<int>(data, form);
  3011. break;
  3012. case DW_AT_base_types:
  3013. /*TODO:*/ ReadValue<int>(data, form);
  3014. break;
  3015. case DW_AT_calling_convention:
  3016. /*TODO:*/ ReadValue<int>(data, form);
  3017. break;
  3018. case DW_AT_count:
  3019. atCount = ReadValue<uint>(data, form);
  3020. break;
  3021. case DW_AT_data_member_location:
  3022. if (form == DW_FORM_exprloc)
  3023. {
  3024. atDataMemberLocation = (int)ReadValue<uint>(data, form, dataOfs, &atDataMemberData);
  3025. hadMemberLocation = true;
  3026. }
  3027. else
  3028. {
  3029. atDataMemberLocation = (int)ReadValue<uint>(data, form);
  3030. hadMemberLocation = true;
  3031. }
  3032. break;
  3033. case DW_AT_decl_column:
  3034. /*TODO:*/ ReadValue<uint32>(data, form);
  3035. break;
  3036. case DW_AT_decl_file:
  3037. atDeclFile = ReadValue<uint32>(data, form);
  3038. break;
  3039. case DW_AT_decl_line:
  3040. atDeclLine = ReadValue<uint32>(data, form);
  3041. break;
  3042. case DW_AT_declaration:
  3043. atDeclaration = ReadValue<bool>(data, form);
  3044. break;
  3045. case DW_AT_discr_list:
  3046. /*TODO:*/ ReadValue<int>(data, form);
  3047. break;
  3048. case DW_AT_encoding:
  3049. atEncoding = ReadValue<int>(data, form);
  3050. break;
  3051. case DW_AT_external:
  3052. atExternal = ReadValue<int>(data, form);
  3053. break;
  3054. case DW_AT_frame_base:
  3055. atFrameBaseLength = (int64_t)ReadValue<uint64_t>(data, form, dataOfs, &atFrameBase);
  3056. break;
  3057. case DW_AT_friend:
  3058. /*TODO:*/ ReadValue<int>(data, form);
  3059. break;
  3060. case DW_AT_identifier_case:
  3061. /*TODO:*/ ReadValue<int>(data, form);
  3062. break;
  3063. case DW_AT_macro_info:
  3064. /*TODO:*/ ReadValue<int>(data, form);
  3065. break;
  3066. case DW_AT_namelist_item:
  3067. /*TODO:*/ ReadValue<int>(data, form);
  3068. break;
  3069. case DW_AT_priority:
  3070. /*TODO:*/ ReadValue<int>(data, form);
  3071. break;
  3072. case DW_AT_segment:
  3073. /*TODO:*/ ReadValue<int>(data, form);
  3074. break;
  3075. case DW_AT_specification:
  3076. atSpecification = ReadValue<int>(data, form, dataOfs);
  3077. break;
  3078. case DW_AT_static_link:
  3079. /*TODO:*/ ReadValue<int>(data, form);
  3080. break;
  3081. case DW_AT_type:
  3082. atType = ReadValue<int>(data, form, dataOfs);
  3083. break;
  3084. case DW_AT_use_location:
  3085. /*TODO:*/ ReadValue<int>(data, form);
  3086. break;
  3087. case DW_AT_variable_parameter:
  3088. /*TODO:*/ ReadValue<int>(data, form);
  3089. break;
  3090. case DW_AT_virtuality:
  3091. atVirtual = ReadValue<int>(data, form) != 0;
  3092. break;
  3093. case DW_AT_vtable_elem_location:
  3094. ReadValue<uint64_t>(data, form, dataOfs, &atVirtualLocData);
  3095. break;
  3096. case DW_AT_allocated:
  3097. /*TODO:*/ ReadValue<int>(data, form);
  3098. break;
  3099. case DW_AT_associated:
  3100. /*TODO:*/ ReadValue<int>(data, form);
  3101. break;
  3102. case DW_AT_data_location:
  3103. /*TODO:*/ ReadValue<int>(data, form);
  3104. break;
  3105. case DW_AT_byte_stride:
  3106. /*TODO:*/ ReadValue<int>(data, form);
  3107. break;
  3108. case DW_AT_entry_pc:
  3109. /*TODO:*/ ReadValue<int>(data, form);
  3110. break;
  3111. case DW_AT_use_UTF8:
  3112. /*TODO:*/ ReadValue<int>(data, form);
  3113. break;
  3114. case DW_AT_extension:
  3115. /*TODO:*/ ReadValue<int>(data, form);
  3116. break;
  3117. case DW_AT_ranges:
  3118. atRanges = (int)ReadValue<uint>(data, form);
  3119. hasRanges = true;
  3120. break;
  3121. case DW_AT_trampoline:
  3122. /*TODO:*/ ReadValue<int>(data, form);
  3123. break;
  3124. case DW_AT_call_column:
  3125. /*TODO:*/ ReadValue<int>(data, form);
  3126. break;
  3127. case DW_AT_call_file:
  3128. atCallFile = ReadValue<uint32>(data, form);
  3129. break;
  3130. case DW_AT_call_line:
  3131. atCallLine = ReadValue<uint32>(data, form);
  3132. break;
  3133. case DW_AT_description:
  3134. /*TODO:*/ ReadValue<int>(data, form);
  3135. break;
  3136. case DW_AT_binary_scale:
  3137. /*TODO:*/ ReadValue<int>(data, form);
  3138. break;
  3139. case DW_AT_decimal_scale:
  3140. /*TODO:*/ ReadValue<int>(data, form);
  3141. break;
  3142. case DW_AT_small:
  3143. /*TODO:*/ ReadValue<int>(data, form);
  3144. break;
  3145. case DW_AT_decimal_sign:
  3146. /*TODO:*/ ReadValue<int>(data, form);
  3147. break;
  3148. case DW_AT_digit_count:
  3149. /*TODO:*/ ReadValue<int>(data, form);
  3150. break;
  3151. case DW_AT_picture_string:
  3152. /*TODO:*/ ReadValue<int>(data, form);
  3153. break;
  3154. case DW_AT_mutable:
  3155. /*TODO:*/ ReadValue<int>(data, form);
  3156. break;
  3157. case DW_AT_threads_scaled:
  3158. /*TODO:*/ ReadValue<int>(data, form);
  3159. break;
  3160. case DW_AT_explicit:
  3161. /*TODO:*/ ReadValue<int>(data, form);
  3162. break;
  3163. case DW_AT_object_pointer:
  3164. atObjectPointer = ReadValue<int>(data, form);
  3165. break;
  3166. case DW_AT_endianity:
  3167. /*TODO:*/ ReadValue<int>(data, form);
  3168. break;
  3169. case DW_AT_elemental:
  3170. /*TODO:*/ ReadValue<int>(data, form);
  3171. break;
  3172. case DW_AT_pure:
  3173. /*TODO:*/ ReadValue<int>(data, form);
  3174. break;
  3175. case DW_AT_recursive:
  3176. /*TODO:*/ ReadValue<int>(data, form);
  3177. break;
  3178. case DW_AT_signature:
  3179. /*TODO:*/ ReadValue<int>(data, form);
  3180. break;
  3181. case DW_AT_main_subprogram:
  3182. /*TODO:*/ ReadValue<int>(data, form);
  3183. break;
  3184. case DW_AT_data_bit_offset:
  3185. /*TODO:*/ ReadValue<int>(data, form);
  3186. break;
  3187. case DW_AT_const_expr:
  3188. /*TODO:*/ ReadValue<int>(data, form);
  3189. break;
  3190. case DW_AT_enum_class:
  3191. /*TODO:*/ ReadValue<int>(data, form);
  3192. break;
  3193. case DW_AT_linkage_name:
  3194. atLinkageName = ReadValue<const char*>(data, form);
  3195. break;
  3196. //
  3197. case DW_AT_MIPS_linkage_name:
  3198. atLinkageName = ReadValue<const char*>(data, form);
  3199. break;
  3200. case DW_AT_APPLE_optimized:
  3201. isOptimized = ReadValue<bool>(data, form);
  3202. break;
  3203. default:
  3204. ReadValue<int>(data, form);
  3205. break;
  3206. }
  3207. }
  3208. if ((hasRanges) && (atLowPC == 0))
  3209. {
  3210. addr_target* rangeData = (addr_target*)(mDebugRangesData + atRanges);
  3211. while (true)
  3212. {
  3213. addr_target lowPC = *(rangeData++);
  3214. if (lowPC == 0)
  3215. break;
  3216. addr_target highPC = *(rangeData++);
  3217. if (compileUnit->mLowPC != (addr_target)-1)
  3218. {
  3219. // These are sometimes relative to the compile unit and sometimes absolute
  3220. if (highPC + compileUnit->mLowPC <= compileUnit->mHighPC)
  3221. {
  3222. lowPC += compileUnit->mLowPC;
  3223. highPC += compileUnit->mLowPC;
  3224. }
  3225. }
  3226. highPC -= lowPC;
  3227. // Select the largest range. We have some cases where some hoisting and such will
  3228. // give us a small inlining aberration much earlier than expected so this ignores that
  3229. if ((int64)highPC > atHighPC)
  3230. {
  3231. atLowPC = lowPC;
  3232. atHighPC = highPC;
  3233. }
  3234. /*if ((atLowPC == 0) || (lowPC < (addr_target)atLowPC))
  3235. atLowPC = lowPC;
  3236. if (highPC > (addr_target)atHighPC)
  3237. atHighPC = highPC;*/
  3238. }
  3239. }
  3240. switch (entryTag)
  3241. {
  3242. case DW_TAG_compile_unit:
  3243. {
  3244. newDataPair = MakeDataPair(compileUnit);
  3245. compileUnit->mName = atName;
  3246. compileUnit->mProducer = atProducer;
  3247. if (atCompDir != NULL)
  3248. compileUnit->mCompileDir = atCompDir;
  3249. if (atLowPC != 0)
  3250. {
  3251. compileUnit->mLowPC = (addr_target)atLowPC;
  3252. compileUnit->mHighPC = (addr_target)(atLowPC + atHighPC);
  3253. }
  3254. if (compileUnit->mProducer.IndexOf("Beef") != -1)
  3255. {
  3256. compileUnit->mLanguage = DbgLanguage_Beef;
  3257. }
  3258. else
  3259. {
  3260. compileUnit->mLanguage = DbgLanguage_C;
  3261. }
  3262. compileUnit->mGlobalType->mLanguage = compileUnit->mLanguage;
  3263. }
  3264. break;
  3265. case DW_TAG_imported_module:
  3266. {
  3267. DbgType* parentType = GetStackTop<DbgType*>(&dataStack);
  3268. DbgType* importType = GetOrCreateType(atImport, dataMap);
  3269. if (parentType != NULL) // Parent type is NULL for Clang DbgModule info
  3270. parentType->mUsingNamespaces.PushFront(importType, &mAlloc);
  3271. }
  3272. break;
  3273. case DW_TAG_inlined_subroutine:
  3274. case DW_TAG_subprogram:
  3275. {
  3276. /*//TODO: This is a test. See if it breaks anything.
  3277. if ((atExternal != 0) && (atLowPC == 0))
  3278. break;*/
  3279. if (atSpecification == 0)
  3280. {
  3281. subProgram = GetOrCreate<DbgSubprogram*>(tagIdx, dataMap);
  3282. subProgram->mCompileUnit = compileUnit;
  3283. subProgram->mVirtual = atVirtual;
  3284. subProgram->mIsOptimized = isOptimized;
  3285. if (atVirtualLocData != NULL)
  3286. {
  3287. const uint8* opPtr = atVirtualLocData;
  3288. if (*(opPtr++) == DW_OP_constu)
  3289. {
  3290. subProgram->mVTableLoc = (int)DecodeSLEB128(opPtr) * sizeof(addr_target);
  3291. }
  3292. }
  3293. //subProgram->mVTableLoc = atVirtualLoc * sizeof(addr_target);
  3294. //SplitName(atName, subProgram->mName, subProgram->mTemplateName);
  3295. subProgram->mName = atName;
  3296. subProgram->mLinkName = atLinkageName;
  3297. if (atAbstractOrigin != NULL)
  3298. {
  3299. DbgSubprogram* originSubProgram = GetOrCreate<DbgSubprogram*>(atAbstractOrigin, dataMap);
  3300. auto abstractOriginEntry = AbstractOriginEntry::Create(DbgSubprogram::ClassType, subProgram, originSubProgram);
  3301. abstractOriginReplaceList.push_back(abstractOriginEntry);
  3302. }
  3303. subProgram->mParentType = GetStackTop<DbgType*>(&dataStack);
  3304. newDataPair = MakeDataPair(subProgram);
  3305. //if ((atLinkageName != NULL) && (subProgram->mParentType != NULL))
  3306. //subProgram->mParentType->mDefinedMembersCount++;
  3307. mSubprograms.push_back(subProgram);
  3308. if (subProgram->mParentType != NULL)
  3309. {
  3310. subProgram->mParentType->mMethodList.PushBack(subProgram);
  3311. }
  3312. else
  3313. {
  3314. compileUnit->mGlobalType->mMethodList.PushBack(subProgram);
  3315. }
  3316. }
  3317. else
  3318. {
  3319. subProgram = dataMap.Get<DbgSubprogram*>(atSpecification);
  3320. BF_ASSERT(subProgram != NULL);
  3321. // We remove params form the declaration and re-add the real ones here
  3322. subProgram->mParams.Clear();
  3323. }
  3324. newDataPair = MakeDataPair(subProgram);
  3325. DbgBlock* dwBlock = &subProgram->mBlock;
  3326. if (atType != 0)
  3327. subProgram->mReturnType = GetOrCreateType(atType, dataMap);
  3328. if (!atDeclaration)
  3329. {
  3330. dwBlock->mLowPC = (addr_target)atLowPC;
  3331. dwBlock->mHighPC = (addr_target)(atLowPC + atHighPC);
  3332. if (dwBlock->mLowPC != 0)
  3333. {
  3334. compileUnit->mLowPC = std::min(compileUnit->mLowPC, dwBlock->mLowPC);
  3335. compileUnit->mHighPC = std::max(compileUnit->mHighPC, dwBlock->mHighPC);
  3336. }
  3337. if (atObjectPointer != 0)
  3338. subProgram->mHasThis = true;
  3339. subProgram->mFrameBaseLen = (int)atFrameBaseLength;
  3340. subProgram->mFrameBaseData = atFrameBase;
  3341. if (atHighPC > 0)
  3342. {
  3343. MapSubprogram(subProgram);
  3344. }
  3345. }
  3346. if (entryTag == DW_TAG_inlined_subroutine)
  3347. {
  3348. DbgSubprogram* parentSubProgram = GetStackLast<DbgSubprogram*>(&dataStack);
  3349. subProgram->mInlineeInfo = mAlloc.Alloc<DbgInlineeInfo>();
  3350. subProgram->mInlineeInfo->mInlineParent = parentSubProgram;
  3351. subProgram->mInlineeInfo->mRootInliner = parentSubProgram->GetRootInlineParent();
  3352. subProgram->mFrameBaseData = parentSubProgram->mFrameBaseData;
  3353. subProgram->mFrameBaseLen = parentSubProgram->mFrameBaseLen;
  3354. }
  3355. //if (subProgram->mParentType != NULL)
  3356. //subProgram->mParentType->mDefinedMembersCount++;
  3357. }
  3358. break;
  3359. case DW_TAG_lexical_block:
  3360. {
  3361. DbgBlock* prevBlock = GetStackTop<DbgBlock*>(&dataStack);
  3362. DbgBlock* dwBlock = mAlloc.Alloc<DbgBlock>();
  3363. if (hasRanges)
  3364. {
  3365. dwBlock->mLowPC = -1;
  3366. dwBlock->mHighPC = (addr_target)atRanges;
  3367. }
  3368. else
  3369. {
  3370. dwBlock->mLowPC = (addr_target)atLowPC;
  3371. dwBlock->mHighPC = (addr_target)(atLowPC + atHighPC);
  3372. }
  3373. newDataPair = MakeDataPair(dwBlock);
  3374. prevBlock->mSubBlocks.PushBack(dwBlock);
  3375. }
  3376. break;
  3377. case DW_TAG_variable:
  3378. {
  3379. DbgBlock* dwBlock = GetStackTop<DbgBlock*>(&dataStack);
  3380. if (atName && !strncmp(atName, "__asmLines", 10))
  3381. {
  3382. const char* ptr = strchr(atName, '.');
  3383. if (!ptr)
  3384. break;
  3385. int declLine = atDeclLine;
  3386. Array<int> asmLines;
  3387. int curAsmLine = 0;
  3388. int curRunCount = 1; // initial value is starting line, with an assumed run count of 1
  3389. bool parity = true; // starting line is standalone; everything afterwards is in pairs
  3390. while (true)
  3391. {
  3392. ++ptr;
  3393. if (!*ptr)
  3394. break;
  3395. String s;
  3396. if (*ptr == '$')
  3397. {
  3398. ++ptr;
  3399. const char* dollarPtr = strchr(ptr, '$');
  3400. if (!dollarPtr)
  3401. break;
  3402. s = String(ptr, (int)(dollarPtr - ptr));
  3403. ptr = dollarPtr;
  3404. }
  3405. else
  3406. {
  3407. s += *ptr;
  3408. }
  3409. //int asmLine = atoi(s.c_str());
  3410. //asmLines.push_back(asmLine);
  3411. const char* sPtr = s.c_str();
  3412. int decodedValue = (int)DecodeULEB32(sPtr);
  3413. if (!parity)
  3414. {
  3415. curRunCount = decodedValue;
  3416. }
  3417. else
  3418. {
  3419. for (int iLine=0; iLine<curRunCount; ++iLine)
  3420. {
  3421. curAsmLine += decodedValue;
  3422. asmLines.push_back(curAsmLine);
  3423. }
  3424. }
  3425. parity = !parity;
  3426. }
  3427. BF_ASSERT(!parity);
  3428. if (dwBlock->mAsmDebugLineMap == NULL)
  3429. {
  3430. mAsmDebugLineMaps.resize(mAsmDebugLineMaps.size() + 1);
  3431. dwBlock->mAsmDebugLineMap = &mAsmDebugLineMaps.back();
  3432. }
  3433. auto mapIter = dwBlock->mAsmDebugLineMap->find(declLine);
  3434. if (mapIter != dwBlock->mAsmDebugLineMap->end())
  3435. {
  3436. auto& dstVec = mapIter->second;
  3437. dstVec.Reserve(dstVec.size() + asmLines.size());
  3438. //dstVec.insert(dstVec.end(), asmLines.begin(), asmLines.end());
  3439. if (!asmLines.IsEmpty())
  3440. dstVec.Insert(dstVec.size(), &asmLines[0], asmLines.size());
  3441. }
  3442. else
  3443. {
  3444. (*dwBlock->mAsmDebugLineMap)[declLine] = std::move(asmLines);
  3445. }
  3446. break;
  3447. }
  3448. bool addToGlobalVarMap = false;
  3449. bool isNewVariable = true;
  3450. DbgVariable* dbgVariable = NULL;
  3451. if (atSpecification != 0)
  3452. {
  3453. //dbgVariable = dataMap.Get<DbgVariable*>(atSpecification);
  3454. //BF_ASSERT(dbgVariable != NULL);
  3455. dbgVariable = GetOrCreate<DbgVariable*>(atSpecification, dataMap);
  3456. //dbgVariable = dataMap.Get<DbgVariable*>(atSpecification);
  3457. //BF_ASSERT(dbgVariable != NULL);
  3458. }
  3459. else if (dwBlock != NULL)
  3460. {
  3461. dbgVariable = GetOrCreate<DbgVariable*>(tagIdx, dataMap);
  3462. dwBlock->mVariables.PushBack(dbgVariable);
  3463. }
  3464. else
  3465. {
  3466. DbgType* dbgType = GetStackTop<DbgType*>(&dataStack);
  3467. bool wantGlobal = true;
  3468. if (compileUnit->mLanguage == DbgLanguage_Beef)
  3469. {
  3470. // Don't show certain global variables in Beef -- that includes things like VTable data
  3471. if (atName[0] == '_')
  3472. wantGlobal = false;
  3473. }
  3474. if ((dbgType == NULL) && (wantGlobal))
  3475. {
  3476. /*DbgCompileUnit* topCompileUnit = GetStackTop<DbgCompileUnit*>(&dataStack);
  3477. if (topCompileUnit != NULL)
  3478. dbgType = &topCompileUnit->mGlobalType;*/
  3479. dbgType = linkedModule->mMasterCompileUnit->mGlobalType;
  3480. auto foundEntry = mGlobalVarMap.Find(atName);
  3481. if (foundEntry != NULL)
  3482. {
  3483. isNewVariable = false;
  3484. dbgVariable = foundEntry->mValue;
  3485. }
  3486. else
  3487. {
  3488. addToGlobalVarMap = true;
  3489. }
  3490. }
  3491. if (dbgVariable == NULL)
  3492. dbgVariable = GetOrCreate<DbgVariable*>(tagIdx, dataMap);
  3493. dbgVariable->mIsStatic = true;
  3494. //TODO: dbgType can be NULL. This only (apparently?) happens for DW_TAG_inlined_subroutine, which we don't handle right now...
  3495. if (dbgType != NULL)
  3496. {
  3497. BF_ASSERT(dbgType->IsNamespace() || (dbgType->mTypeCode == DbgType_Root));
  3498. if (isNewVariable)
  3499. dbgType->mMemberList.PushBack(dbgVariable);
  3500. }
  3501. }
  3502. if (dbgVariable != NULL)
  3503. {
  3504. if (atSpecification == 0)
  3505. {
  3506. dbgVariable->mIsParam = false;
  3507. dbgVariable->mName = atName;
  3508. dbgVariable->mConstValue = atConstValue;
  3509. dbgVariable->mType = GetOrCreateType(atType, dataMap);
  3510. dbgVariable->mIsConst = hadConstValue;
  3511. dbgVariable->mIsStatic = !hadMemberLocation;
  3512. dbgVariable->mIsExtern = atExternal != 0;
  3513. }
  3514. if (atLinkageName != NULL)
  3515. dbgVariable->mLinkName = atLinkageName;
  3516. dbgVariable->mLocationLen = (int8)atLocationLen;
  3517. dbgVariable->mLocationData = atLocationData;
  3518. dbgVariable->mCompileUnit = compileUnit;
  3519. /*if (dbgVariable->mIsStatic && !dbgVariable->mIsConst && (dbgVariable->mLocationLen > 0) && (dbgVariable->mIsExtern))
  3520. {
  3521. DbgAddrType addrType = DbgAddrType_Value;
  3522. //
  3523. addr_target valAddr = mDebugTarget->EvaluateLocation(dbgVariable->mCompileUnit->mDbgModule, NULL, dbgVariable->mLocationData, dbgVariable->mLocationLen, NULL, &addrType);
  3524. if ((addrType == DbgAddrType_Target) && (valAddr != 0))
  3525. {
  3526. dbgVariable->mStaticCachedAddr = valAddr;
  3527. if (dbgVariable->mLinkName != NULL)
  3528. mStaticVariables.push_back(dbgVariable);
  3529. }
  3530. else
  3531. dbgVariable->mIsStatic = false;
  3532. }*/
  3533. // We had to remove the above for hot loading, calculate the mStaticCachedAddr later. Just put into mStaticVariables for now
  3534. mStaticVariables.push_back(dbgVariable);
  3535. if (atAbstractOrigin != NULL)
  3536. {
  3537. DbgVariable* originVariable = GetOrCreate<DbgVariable*>(atAbstractOrigin, dataMap);
  3538. auto abstractOriginEntry = AbstractOriginEntry::Create(DbgVariable::ClassType, dbgVariable, originVariable);
  3539. if (atAbstractOrigin < tagIdx)
  3540. abstractOriginEntry.Replace();
  3541. else
  3542. abstractOriginReplaceList.push_back(abstractOriginEntry);
  3543. }
  3544. else if (dbgVariable->mName == NULL)
  3545. dbgVariable->mName = "_unnamed";
  3546. if (addToGlobalVarMap)
  3547. mGlobalVarMap.Insert(dbgVariable);
  3548. newDataPair = MakeDataPair(dbgVariable);
  3549. }
  3550. }
  3551. break;
  3552. case DW_TAG_formal_parameter:
  3553. {
  3554. DbgSubprogram* dwSubprogram = GetStackTop<DbgSubprogram*>(&dataStack);
  3555. if (dwSubprogram == NULL)
  3556. {
  3557. if ((atName == NULL) && (atAbstractOrigin == 0))
  3558. {
  3559. DbgType* dbgType = GetStackTop<DbgType*>(&dataStack);
  3560. if ((dbgType == NULL) || (dbgType->mTypeCode != DbgType_Subroutine))
  3561. break;
  3562. //TODO: Add params to subroutine type
  3563. break;
  3564. }
  3565. break;
  3566. }
  3567. if ((dwSubprogram->mParams.IsEmpty()) && (dwSubprogram->mParentType != 0))
  3568. dwSubprogram->mParentType->mMethodsWithParamsCount++;
  3569. //DbgVariable* dbgVariable = mAlloc.Alloc<DbgVariable>();
  3570. DbgVariable* dwVariable = GetOrCreate<DbgVariable*>(tagIdx, dataMap);
  3571. dwSubprogram->mParams.PushBack(dwVariable);
  3572. if (atArtificial != 0)
  3573. {
  3574. dwSubprogram->mHasThis = true;
  3575. if (atName == NULL)
  3576. atName = "this";
  3577. }
  3578. dwVariable->mCompileUnit = compileUnit;
  3579. dwVariable->mIsParam = true;
  3580. dwVariable->mName = atName;
  3581. dwVariable->mLocationLen = (int)atLocationLen;
  3582. dwVariable->mLocationData = atLocationData;
  3583. dwVariable->mType = GetOrCreateType(atType, dataMap);
  3584. if (atAbstractOrigin != 0)
  3585. {
  3586. }
  3587. }
  3588. break;
  3589. case DW_TAG_enumerator:
  3590. {
  3591. DbgVariable* member = mAlloc.Alloc<DbgVariable>();
  3592. member->mCompileUnit = compileUnit;
  3593. member->mConstValue = atConstValue;
  3594. member->mName = atName;
  3595. member->mIsStatic = true;
  3596. member->mIsConst = true;
  3597. DbgType* parentType = GetStackTop<DbgType*>(&dataStack);
  3598. parentType->mMemberList.PushBack(member);
  3599. member->mMemberOffset = atDataMemberLocation;
  3600. //member->mType = parentType->mTypeParam;
  3601. member->mType = parentType;
  3602. // Insert into parent's namespace
  3603. auto prevTop = dataStack.back();
  3604. dataStack.pop_back();
  3605. DbgBlock* dwBlock = GetStackTop<DbgBlock*>(&dataStack);
  3606. dataStack.push_back(prevTop);
  3607. if (dwBlock != NULL)
  3608. {
  3609. DbgVariable* dwVariable = mAlloc.Alloc<DbgVariable>();
  3610. dwBlock->mVariables.PushBack(dwVariable);
  3611. if (atSpecification == 0)
  3612. {
  3613. dwVariable->mIsParam = false;
  3614. dwVariable->mName = atName;
  3615. dwVariable->mConstValue = atConstValue;
  3616. dwVariable->mType = parentType->mTypeParam;
  3617. dwVariable->mIsConst = hadConstValue;
  3618. dwVariable->mIsStatic = !hadMemberLocation;
  3619. }
  3620. dwVariable->mLocationLen = (int)atLocationLen;
  3621. dwVariable->mLocationData = atLocationData;
  3622. dwVariable->mCompileUnit = compileUnit;
  3623. BF_ASSERT(dwVariable->mName != 0);
  3624. newDataPair = MakeDataPair(dwVariable);
  3625. }
  3626. }
  3627. break;
  3628. /*case DW_TAG_subrange_type:
  3629. {
  3630. DbgType* parentType = GetStackTop<DbgType*>(&dataStack);
  3631. parentType->mArraySize = atUpperBound;
  3632. }
  3633. break;*/
  3634. case DW_TAG_inheritance:
  3635. {
  3636. DbgType* derivedType = GetStackTop<DbgType*>(&dataStack);
  3637. DbgBaseTypeEntry* baseTypeEntry = mAlloc.Alloc<DbgBaseTypeEntry>();
  3638. baseTypeEntry->mBaseType = GetOrCreateType(atType, dataMap);
  3639. if (atDataMemberData != NULL)
  3640. {
  3641. bool foundVirtOffset = false;
  3642. const uint8* opPtr = atDataMemberData;
  3643. if (*(opPtr++) == DW_OP_dup)
  3644. {
  3645. if (*(opPtr++) == DW_OP_deref)
  3646. {
  3647. if (*(opPtr++) == DW_OP_constu)
  3648. {
  3649. baseTypeEntry->mVTableOffset = (int)DecodeSLEB128(opPtr) / sizeof(int32);
  3650. foundVirtOffset = true;
  3651. if (*(opPtr++) == DW_OP_minus)
  3652. baseTypeEntry->mVTableOffset = -baseTypeEntry->mVTableOffset;
  3653. }
  3654. }
  3655. }
  3656. BF_ASSERT(foundVirtOffset);
  3657. }
  3658. else
  3659. baseTypeEntry->mThisOffset = atDataMemberLocation;
  3660. derivedType->mBaseTypes.PushBack(baseTypeEntry);
  3661. }
  3662. break;
  3663. case DW_TAG_member:
  3664. {
  3665. DbgType* parentType = GetStackTop<DbgType*>(&dataStack);
  3666. if ((atName != NULL) && (strncmp(atName, "_vptr$", 6) == 0))
  3667. {
  3668. parentType->mHasVTable = true;
  3669. break;
  3670. }
  3671. //DbgVariable* member = mAlloc.Alloc<DbgVariable>();
  3672. DbgVariable* member = GetOrCreate<DbgVariable*>(tagIdx, dataMap);
  3673. member->mIsMember = true;
  3674. member->mCompileUnit = compileUnit;
  3675. member->mName = atName;
  3676. member->mType = GetOrCreateType(atType, dataMap);
  3677. member->mConstValue = atConstValue;
  3678. member->mIsConst = hadConstValue;
  3679. member->mIsStatic = !hadMemberLocation;
  3680. member->mBitSize = atBitSize;
  3681. member->mBitOffset = atBitOffset;
  3682. member->mIsExtern = atExternal != 0;
  3683. parentType->mMemberList.PushBack(member);
  3684. member->mMemberOffset = atDataMemberLocation;
  3685. if ((member->mIsStatic) && (!member->mIsConst))
  3686. parentType->mHasStaticMembers = true;
  3687. /*if ((member->mIsStatic) && (!member->mIsConst))
  3688. mStaticVariables.push_back(member);*/
  3689. newDataPair = MakeDataPair(member);
  3690. //dataMap.Set(tagIdx, member);
  3691. }
  3692. break;
  3693. case DW_TAG_subrange_type:
  3694. {
  3695. int typeIdx = (int)(tagDataStart - startData);
  3696. DbgType* parentType = GetStackTop<DbgType*>(&dataStack);
  3697. int arrSize = atCount;
  3698. deferredArrayDims.push_back(arrSize);
  3699. }
  3700. break;
  3701. case DW_TAG_namespace:
  3702. case DW_TAG_const_type:
  3703. case DW_TAG_base_type:
  3704. case DW_TAG_pointer_type:
  3705. case DW_TAG_ptr_to_member_type:
  3706. case DW_TAG_array_type:
  3707. case DW_TAG_reference_type:
  3708. case DW_TAG_rvalue_reference_type:
  3709. case DW_TAG_unspecified_type:
  3710. case DW_TAG_class_type:
  3711. case DW_TAG_enumeration_type:
  3712. case DW_TAG_structure_type:
  3713. case DW_TAG_union_type:
  3714. case DW_TAG_typedef:
  3715. case DW_TAG_volatile_type:
  3716. case DW_TAG_subroutine_type:
  3717. //case DW_TAG_subrange_type:
  3718. case DW_TAG_restrict_type:
  3719. {
  3720. int typeIdx = (int)(tagDataStart - startData);
  3721. DbgType* dbgType = GetOrCreateType(typeIdx, dataMap);
  3722. const char* nameSep = (compileUnit->mLanguage == DbgLanguage_Beef) ? "." : "::";
  3723. if ((atName != NULL) &&
  3724. ((entryTag == DW_TAG_structure_type) || (entryTag == DW_TAG_class_type) ||
  3725. (entryTag == DW_TAG_typedef) || (entryTag == DW_TAG_union_type) || (entryTag == DW_TAG_enumeration_type) ||
  3726. (entryTag == DW_TAG_namespace)))
  3727. {
  3728. BF_ASSERT(dbgType->mTypeCode == DbgType_Null);
  3729. DbgType* parentType = GetStackTop<DbgType*>(&dataStack);
  3730. if (parentType != NULL)
  3731. {
  3732. dbgType->mParent = parentType;
  3733. dbgType->mParent->mSubTypeList.PushBack(dbgType);
  3734. /*if (dbgType->mParent->mName != NULL)
  3735. {
  3736. if (atName == NULL)
  3737. {
  3738. dbgType->mName = dbgType->mParent->mName; // Extend from name of parent if we're anonymous
  3739. }
  3740. else
  3741. {
  3742. int nameSepLen = strlen(nameSep);
  3743. int parentNameLen = strlen(dbgType->mParent->mName);
  3744. int nameLen = strlen(atName);
  3745. char* name = (char*)mAlloc.AllocBytes(parentNameLen + nameSepLen + nameLen + 1);
  3746. memcpy(name, dbgType->mParent->mName, parentNameLen);
  3747. memcpy(name + parentNameLen, nameSep, nameSepLen);
  3748. memcpy(name + parentNameLen + nameSepLen, atName, nameLen);
  3749. dbgType->mName = name;
  3750. }
  3751. }*/
  3752. }
  3753. else
  3754. {
  3755. // Add to global subtype list but don't set dbgType->mParent
  3756. compileUnit->mGlobalType->mSubTypeList.PushBack(dbgType);
  3757. }
  3758. }
  3759. const char* useName = atName;
  3760. /*if ((useName != NULL) && (strcmp(useName, "@") == 0))
  3761. useName = NULL;*/
  3762. dbgType->mCompileUnit = compileUnit;
  3763. dbgType->mLanguage = compileUnit->mLanguage;
  3764. //SplitName(atName, dbgType->mTypeName, dbgType->mTemplateParams);
  3765. dbgType->mName = useName;
  3766. if (dbgType->mTypeName == NULL)
  3767. dbgType->mTypeName = useName;
  3768. //if (dbgType->mName == NULL)
  3769. //dbgType->mName = atName;
  3770. int parentNameLen = ((dbgType->mParent != NULL) && (dbgType->mParent->mName != NULL)) ? (int)strlen(dbgType->mParent->mName) : 0;
  3771. int typeNameLen = (dbgType->mTypeName != NULL) ? (int)strlen(dbgType->mTypeName) : 0;
  3772. //int templateParamsLen = (dbgType->mTemplateParams != NULL) ? strlen(dbgType->mTemplateParams) : 0;
  3773. if ((parentNameLen != 0) /*&& (templateParamsLen == 0)*/)
  3774. {
  3775. int nameSepLen = (int)strlen(nameSep);
  3776. int nameLen = parentNameLen + typeNameLen /*+ templateParamsLen*/;
  3777. if ((parentNameLen > 0) && (nameLen > 0))
  3778. nameLen += nameSepLen;
  3779. char* namePtr = (char*)mAlloc.AllocBytes(nameLen + 1, "DWARF");
  3780. dbgType->mName = namePtr;
  3781. if (parentNameLen > 0)
  3782. {
  3783. memcpy(namePtr, dbgType->mParent->mName, parentNameLen);
  3784. namePtr += parentNameLen;
  3785. if (nameLen > 0)
  3786. {
  3787. memcpy(namePtr, nameSep, nameSepLen);
  3788. namePtr += nameSepLen;
  3789. }
  3790. }
  3791. if (nameLen > 0)
  3792. {
  3793. memcpy(namePtr, useName, typeNameLen);
  3794. namePtr += typeNameLen;
  3795. }
  3796. /*if (templateParamsLen > 0)
  3797. {
  3798. memcpy(namePtr, dbgType->mTemplateParams, templateParamsLen);
  3799. namePtr += templateParamsLen;
  3800. }*/
  3801. }
  3802. dbgType->mTypeCode = DbgType_Null;
  3803. dbgType->mIsDeclaration = atDeclaration;
  3804. if (atByteSize != -1)
  3805. {
  3806. dbgType->mSize = atByteSize;
  3807. dbgType->mSizeCalculated = true;
  3808. }
  3809. switch (entryTag)
  3810. {
  3811. case DW_TAG_base_type:
  3812. // Types that may do fallover to int/uints on size mismatch
  3813. switch (atEncoding)
  3814. {
  3815. case DW_ATE_UTF:
  3816. if (atByteSize == 1)
  3817. dbgType->mTypeCode = DbgType_Utf8;
  3818. else if (atByteSize == 2)
  3819. dbgType->mTypeCode = DbgType_Utf16;
  3820. else
  3821. dbgType->mTypeCode = DbgType_Utf32;
  3822. break;
  3823. case DW_ATE_signed_char:
  3824. if (atByteSize == 1)
  3825. dbgType->mTypeCode = DbgType_SChar;
  3826. else if (atByteSize == 2)
  3827. dbgType->mTypeCode = DbgType_SChar16;
  3828. else if (atByteSize == 4)
  3829. dbgType->mTypeCode = DbgType_SChar32;
  3830. else
  3831. atEncoding = DW_ATE_signed;
  3832. break;
  3833. case DW_ATE_unsigned_char:
  3834. if (atByteSize == 1)
  3835. dbgType->mTypeCode = DbgType_UChar;
  3836. else if (atByteSize == 2)
  3837. dbgType->mTypeCode = DbgType_UChar16;
  3838. else if (atByteSize == 4)
  3839. dbgType->mTypeCode = DbgType_UChar32;
  3840. atEncoding = DW_ATE_unsigned;
  3841. break;
  3842. case DW_ATE_boolean:
  3843. if (atByteSize == 1)
  3844. dbgType->mTypeCode = DbgType_Bool;
  3845. else
  3846. atEncoding = DW_ATE_unsigned;
  3847. break;
  3848. }
  3849. if (dbgType->mTypeCode == DbgType_Null)
  3850. {
  3851. switch (atEncoding)
  3852. {
  3853. case DW_ATE_address:
  3854. if (atByteSize == 0)
  3855. dbgType->mTypeCode = DbgType_Void;
  3856. break;
  3857. case DW_ATE_boolean:
  3858. if (atByteSize == 1)
  3859. {
  3860. dbgType->mTypeCode = DbgType_Bool;
  3861. break;
  3862. }
  3863. //Fall through
  3864. case DW_ATE_signed:
  3865. switch (atByteSize)
  3866. {
  3867. case 1:
  3868. dbgType->mTypeCode = DbgType_i8;
  3869. break;
  3870. case 2:
  3871. dbgType->mTypeCode = DbgType_i16;
  3872. break;
  3873. case 4:
  3874. dbgType->mTypeCode = DbgType_i32;
  3875. break;
  3876. case 8:
  3877. dbgType->mTypeCode = DbgType_i64;
  3878. break;
  3879. case 16:
  3880. dbgType->mTypeCode = DbgType_i128;
  3881. break;
  3882. }
  3883. break;
  3884. case DW_ATE_unsigned:
  3885. switch (atByteSize)
  3886. {
  3887. case 1:
  3888. dbgType->mTypeCode = DbgType_u8;
  3889. break;
  3890. case 2:
  3891. if ((atName != NULL) && (strcmp(atName, "wchar_t") == 0))
  3892. dbgType->mTypeCode = DbgType_UChar16;
  3893. else
  3894. dbgType->mTypeCode = DbgType_u16;
  3895. break;
  3896. case 4:
  3897. dbgType->mTypeCode = DbgType_u32;
  3898. break;
  3899. case 8:
  3900. dbgType->mTypeCode = DbgType_u64;
  3901. break;
  3902. case 16:
  3903. dbgType->mTypeCode = DbgType_u128;
  3904. break;
  3905. }
  3906. break;
  3907. case DW_ATE_float:
  3908. if (atByteSize == 4)
  3909. dbgType->mTypeCode = DbgType_Single;
  3910. else if (atByteSize == 8)
  3911. dbgType->mTypeCode = DbgType_Double;
  3912. else if (atByteSize == 12)
  3913. dbgType->mTypeCode = DbgType_Float96;
  3914. else if (atByteSize == 16)
  3915. dbgType->mTypeCode = DbgType_Float128;
  3916. break;
  3917. case DW_ATE_complex_float:
  3918. if (atByteSize == 8)
  3919. dbgType->mTypeCode = DbgType_ComplexFloat;
  3920. else if (atByteSize == 16)
  3921. dbgType->mTypeCode = DbgType_ComplexDouble;
  3922. else if (atByteSize == 24)
  3923. dbgType->mTypeCode = DbgType_ComplexDouble96;
  3924. else if (atByteSize == 32)
  3925. dbgType->mTypeCode = DbgType_ComplexDouble128;
  3926. break;
  3927. default:
  3928. BF_FATAL("Unknown DW_ATE type");
  3929. break;
  3930. }
  3931. }
  3932. break;
  3933. case DW_TAG_enumeration_type: //TODO: Handle these differently
  3934. dbgType->mTypeCode = DbgType_Enum;
  3935. dbgType->mTypeParam = mAlloc.Alloc<DbgType>();
  3936. if (atByteSize == 8)
  3937. dbgType->mTypeParam->mTypeCode = DbgType_i64;
  3938. else if (atByteSize == 4)
  3939. dbgType->mTypeParam->mTypeCode = DbgType_i32;
  3940. else if (atByteSize == 2)
  3941. dbgType->mTypeParam->mTypeCode = DbgType_i16;
  3942. else if (atByteSize == 1)
  3943. dbgType->mTypeParam->mTypeCode = DbgType_i8;
  3944. else
  3945. {
  3946. BF_DBG_FATAL("Invalid enum type");
  3947. }
  3948. break;
  3949. case DW_TAG_namespace:
  3950. dbgType->mTypeCode = DbgType_Namespace;
  3951. break;
  3952. case DW_TAG_const_type:
  3953. dbgType->mTypeCode = DbgType_Const;
  3954. dbgType->mTypeParam = GetOrCreateType(atType, dataMap);
  3955. break;
  3956. case DW_TAG_rvalue_reference_type:
  3957. dbgType->mTypeCode = DbgType_RValueReference;
  3958. dbgType->mTypeParam = GetOrCreateType(atType, dataMap);
  3959. break;
  3960. case DW_TAG_unspecified_type:
  3961. dbgType->mTypeCode = DbgType_Unspecified;
  3962. dbgType->mTypeName = atName;
  3963. break;
  3964. case DW_TAG_reference_type:
  3965. dbgType->mTypeCode = DbgType_Ref;
  3966. dbgType->mTypeParam = GetOrCreateType(atType, dataMap);
  3967. break;
  3968. case DW_TAG_pointer_type:
  3969. dbgType->mTypeCode = DbgType_Ptr;
  3970. dbgType->mSize = sizeof(addr_target);
  3971. dbgType->mSizeCalculated = true;
  3972. dbgType->mTypeParam = GetOrCreateType(atType, dataMap);
  3973. if (dbgType->mTypeParam != NULL)
  3974. dbgType->mTypeParam->mPtrType = dbgType;
  3975. break;
  3976. case DW_TAG_ptr_to_member_type:
  3977. dbgType->mTypeCode = DbgType_PtrToMember;
  3978. dbgType->mSize = sizeof(addr_target);
  3979. dbgType->mTypeParam = GetOrCreateType(atType, dataMap);
  3980. if (dbgType->mTypeParam != NULL)
  3981. dbgType->mTypeParam->mPtrType = dbgType;
  3982. break;
  3983. case DW_TAG_array_type:
  3984. dbgType->mTypeCode = DbgType_SizedArray;
  3985. dbgType->mTypeParam = GetOrCreateType(atType, dataMap);
  3986. break;
  3987. case DW_TAG_structure_type:
  3988. dbgType->mTypeCode = DbgType_Struct;
  3989. break;
  3990. case DW_TAG_class_type:
  3991. dbgType->mTypeCode = DbgType_Class;
  3992. break;
  3993. case DW_TAG_union_type:
  3994. dbgType->mTypeCode = DbgType_Union;
  3995. break;
  3996. case DW_TAG_typedef:
  3997. dbgType->mTypeCode = DbgType_TypeDef;
  3998. dbgType->mTypeParam = GetOrCreateType(atType, dataMap);
  3999. break;
  4000. case DW_TAG_volatile_type:
  4001. dbgType->mTypeCode = DbgType_Volatile;
  4002. dbgType->mTypeParam = GetOrCreateType(atType, dataMap);
  4003. break;
  4004. case DW_TAG_subroutine_type:
  4005. dbgType->mTypeCode = DbgType_Subroutine;
  4006. if (atType != 0) // Return value
  4007. dbgType->mTypeParam = GetOrCreateType(atType, dataMap);
  4008. dbgType->mBlockParam = mAlloc.Alloc<DbgBlock>();
  4009. break;
  4010. case DW_TAG_restrict_type:
  4011. dbgType->mTypeCode = DbgType_Restrict;
  4012. dbgType->mTypeParam = GetOrCreateType(atType, dataMap);
  4013. break;
  4014. }
  4015. newDataPair = MakeDataPair(dbgType);
  4016. }
  4017. break;
  4018. }
  4019. if (hasChildren)
  4020. dataStack.push_back(newDataPair);
  4021. }
  4022. for (auto& abstractOriginEntry : abstractOriginReplaceList)
  4023. abstractOriginEntry.Replace();
  4024. GetLinkedModule()->MapTypes(startingTypeIdx);
  4025. dataPtr = dataEnd;
  4026. return true;
  4027. }
  4028. void DbgModule::ParseDebugFrameData()
  4029. {
  4030. BP_ZONE("ParseDebugFrameData");
  4031. const uint8* data = mDebugFrameData;
  4032. if (data == NULL)
  4033. return;
  4034. mParsedFrameDescriptors = true;
  4035. Dictionary<addr_target, DwCommonFrameDescriptor*> commonFrameDescriptorMap;
  4036. while (true)
  4037. {
  4038. addr_target relSectionAddr = (addr_target)(data - mDebugFrameData);
  4039. int length = GET(int);
  4040. if (length == 0)
  4041. break;
  4042. const uint8* dataEnd = data + length;
  4043. int cieID = GET(int);
  4044. if (cieID < 0)
  4045. {
  4046. BP_ALLOC_T(DwCommonFrameDescriptor);
  4047. DwCommonFrameDescriptor* commonFrameDescriptor = mAlloc.Alloc<DwCommonFrameDescriptor>();
  4048. char version = GET(char);
  4049. commonFrameDescriptor->mDbgModule = this;
  4050. commonFrameDescriptor->mAugmentation = DataGetString(data);
  4051. if (version >= 4)
  4052. {
  4053. commonFrameDescriptor->mPointerSize = GET(int8);
  4054. commonFrameDescriptor->mSegmentSize = GET(int8);
  4055. }
  4056. commonFrameDescriptor->mCodeAlignmentFactor = (int)DecodeULEB128(data);
  4057. commonFrameDescriptor->mDataAlignmentFactor = (int)DecodeSLEB128(data);
  4058. commonFrameDescriptor->mReturnAddressColumn = (int)DecodeULEB128(data);
  4059. commonFrameDescriptor->mInstData = data;
  4060. commonFrameDescriptor->mInstLen = (int)(dataEnd - data);
  4061. mDebugTarget->mCommonFrameDescriptors.push_back(commonFrameDescriptor);
  4062. if (version < 3)
  4063. commonFrameDescriptorMap[relSectionAddr] = commonFrameDescriptor;
  4064. else
  4065. commonFrameDescriptorMap[mDebugFrameAddress + relSectionAddr] = commonFrameDescriptor;
  4066. }
  4067. else
  4068. {
  4069. addr_target lowPC = GET(addr_target);
  4070. addr_target highPC = lowPC + GET(addr_target);
  4071. DwCommonFrameDescriptor* commonFrameDescriptor = commonFrameDescriptorMap[(addr_target)cieID];
  4072. BF_ASSERT(commonFrameDescriptor != NULL);
  4073. typedef decltype(mDebugTarget->mDwFrameDescriptorMap) MapType;
  4074. auto resultPair = mDebugTarget->mDwFrameDescriptorMap.insert(MapType::value_type(lowPC, DwFrameDescriptor()));
  4075. auto frameDescriptor = &resultPair.first->second;
  4076. //frameDescriptor->
  4077. frameDescriptor->mLowPC = lowPC;
  4078. frameDescriptor->mHighPC = highPC;
  4079. frameDescriptor->mInstData = data;
  4080. frameDescriptor->mInstLen = (int)(dataEnd - data);
  4081. frameDescriptor->mCommonFrameDescriptor = commonFrameDescriptor;
  4082. }
  4083. data = dataEnd;
  4084. }
  4085. }
  4086. void DbgModule::ParseEHFrameData()
  4087. {
  4088. const uint8* data = mEHFrameData;
  4089. if (data == NULL)
  4090. return;
  4091. Dictionary<addr_target, DwCommonFrameDescriptor*> commonFrameDescriptorMap;
  4092. while (true)
  4093. {
  4094. addr_target sectionAddress = (addr_target)(data - mEHFrameData);
  4095. int length = GET(int);
  4096. if (length == 0)
  4097. break;
  4098. const uint8* dataEnd = data + length;
  4099. int cieID = GET(int);
  4100. if (cieID <= 0)
  4101. {
  4102. BP_ALLOC_T(DwCommonFrameDescriptor);
  4103. DwCommonFrameDescriptor* commonFrameDescriptor = mAlloc.Alloc<DwCommonFrameDescriptor>();
  4104. char version = GET(char);
  4105. const char* augmentation = DataGetString(data);
  4106. commonFrameDescriptor->mDbgModule = this;
  4107. commonFrameDescriptor->mCodeAlignmentFactor = (int)DecodeULEB128(data);
  4108. commonFrameDescriptor->mDataAlignmentFactor = (int)DecodeSLEB128(data);
  4109. commonFrameDescriptor->mReturnAddressColumn = (int)DecodeULEB128(data);
  4110. commonFrameDescriptor->mAugmentation = augmentation;
  4111. if (*augmentation == 'z')
  4112. {
  4113. ++augmentation;
  4114. int augLen = (int)DecodeULEB128(data);
  4115. commonFrameDescriptor->mAugmentationLength = augLen;
  4116. const uint8* augEnd = data + augLen;
  4117. while (*augmentation != '\0')
  4118. {
  4119. if (*augmentation == 'R')
  4120. commonFrameDescriptor->mAddressPointerEncoding = (int) GET(uint8);
  4121. else if (*augmentation == 'P')
  4122. {
  4123. int encodingType = GET(uint8);
  4124. BF_ASSERT(encodingType == 0);
  4125. commonFrameDescriptor->mLSDARoutine = GET(addr_target);
  4126. }
  4127. else if (*augmentation == 'L')
  4128. commonFrameDescriptor->mLSDAPointerEncodingFDE = GET(uint8);
  4129. else if (*augmentation == 'S')
  4130. {
  4131. // mIsSignalHandler - on return from stack frame, CFA is before next instruction rather than after it
  4132. }
  4133. else
  4134. BF_FATAL("Unknown CIE augmentation");
  4135. ++augmentation;
  4136. }
  4137. data = augEnd;
  4138. }
  4139. commonFrameDescriptor->mInstData = data;
  4140. commonFrameDescriptor->mInstLen = (int)(dataEnd - data);
  4141. mDebugTarget->mCommonFrameDescriptors.push_back(commonFrameDescriptor);
  4142. commonFrameDescriptorMap[sectionAddress] = commonFrameDescriptor;
  4143. }
  4144. else
  4145. {
  4146. int ciePos = (int)(sectionAddress - cieID) + 4;
  4147. DwCommonFrameDescriptor* commonFrameDescriptor = commonFrameDescriptorMap[(addr_target)ciePos];
  4148. addr_target lowPC;
  4149. addr_target highPC;
  4150. if (commonFrameDescriptor->mAddressPointerEncoding == (DW_EH_PE_pcrel | DW_EH_PE_sdata4))
  4151. {
  4152. lowPC = GET(int);
  4153. lowPC += mEHFrameAddress + sectionAddress + 8;
  4154. highPC = lowPC + GET(int);
  4155. }
  4156. else
  4157. {
  4158. lowPC = GET(int);
  4159. highPC = lowPC + GET(int);
  4160. }
  4161. typedef decltype(mDebugTarget->mDwFrameDescriptorMap) MapType;
  4162. auto resultPair = mDebugTarget->mDwFrameDescriptorMap.insert(MapType::value_type(lowPC, DwFrameDescriptor()));
  4163. auto frameDescriptor = &resultPair.first->second;
  4164. frameDescriptor->mLSDARoutine = commonFrameDescriptor->mLSDARoutine;
  4165. const char* augmentation = commonFrameDescriptor->mAugmentation;
  4166. if (*augmentation == 'z')
  4167. {
  4168. int augLen = GET(uint8);
  4169. const uint8* augEnd = data + augLen;
  4170. ++augmentation;
  4171. while (*augmentation != '\0')
  4172. {
  4173. if (*augmentation == 'R')
  4174. {
  4175. }
  4176. else if (*augmentation == 'P')
  4177. {
  4178. }
  4179. else if (*augmentation == 'L')
  4180. {
  4181. BF_ASSERT(commonFrameDescriptor->mLSDAPointerEncodingFDE == 0);
  4182. frameDescriptor->mLSDARoutine = GET(addr_target);
  4183. }
  4184. else if (*augmentation == 'S')
  4185. {
  4186. }
  4187. else
  4188. BF_FATAL("Unknown CIE augmentation");
  4189. augmentation++;
  4190. }
  4191. data = augEnd;
  4192. }
  4193. frameDescriptor->mLowPC = lowPC;
  4194. frameDescriptor->mHighPC = highPC;
  4195. frameDescriptor->mInstData = data;
  4196. frameDescriptor->mInstLen = (int)(dataEnd - data);
  4197. frameDescriptor->mCommonFrameDescriptor = commonFrameDescriptor;
  4198. }
  4199. data = dataEnd;
  4200. }
  4201. }
  4202. void DbgModule::FlushLineData(DbgSubprogram* curSubprogram, std::list<DbgLineData>& queuedLineData)
  4203. {
  4204. }
  4205. DbgSrcFile* DbgModule::AddSrcFile(DbgCompileUnit* compileUnit, const String& srcFilePath)
  4206. {
  4207. DbgSrcFile* dwSrcFile = mDebugTarget->AddSrcFile(srcFilePath);
  4208. if (compileUnit != NULL)
  4209. {
  4210. DbgSrcFileReference srcFileRef;
  4211. srcFileRef.mSrcFile = dwSrcFile;
  4212. srcFileRef.mCompileUnit = compileUnit;
  4213. compileUnit->mSrcFileRefs.push_back(srcFileRef);
  4214. }
  4215. return dwSrcFile;
  4216. }
  4217. bool DbgModule::ParseDebugLineInfo(const uint8*& dataPtr, int compileUnitIdx)
  4218. {
  4219. BP_ZONE("ParseDebugLineInfo");
  4220. const uint8* data = dataPtr;
  4221. const int startOffset = (int)(data - mDebugLineData);
  4222. int length = GET(int);
  4223. if (length == 0)
  4224. return false;
  4225. DbgCompileUnit* dwCompileUnit = mCompileUnits[compileUnitIdx];
  4226. const uint8* dataEnd = data + length;
  4227. short version = GET(short);
  4228. int headerLength = GET(int);
  4229. char minimumInstructionLength = GET(char);
  4230. int maximumOperationsPerInstruction = 1;
  4231. char defaultIsStmt = GET(char);
  4232. char lineBase = GET(char);
  4233. char lineRange = GET(char);
  4234. char opcodeBase = GET(char);
  4235. for (int i = 0; i < opcodeBase - 1; i++)
  4236. {
  4237. char standardOpcodeLengths = GET(char);
  4238. }
  4239. Array<const char*> directoryNames;
  4240. while (true)
  4241. {
  4242. const char* name = DataGetString(data);
  4243. if (name[0] == 0)
  4244. break;
  4245. directoryNames.push_back(name);
  4246. }
  4247. DbgSrcFileReference* dwSrcFileRef = NULL;
  4248. HashSet<String> foundPathSet;
  4249. int curFileIdx = 0;
  4250. DbgSubprogram* curSubprogram = NULL;
  4251. #define ADD_LINEDATA(lineData) \
  4252. lineBuilder.Add(dwCompileUnit, lineData, dwSrcFileRef->mSrcFile, NULL);
  4253. while (true)
  4254. {
  4255. const char* path = DataGetString(data);
  4256. if (path[0] == 0)
  4257. break;
  4258. int directoryIdx = (int)DecodeULEB128(data);
  4259. int lastModificationTime = (int)DecodeULEB128(data);
  4260. int fileLength = (int)DecodeULEB128(data);
  4261. String filePath;
  4262. if (directoryIdx > 0)
  4263. filePath = String(directoryNames[directoryIdx - 1]) + "/";
  4264. filePath += path;
  4265. filePath = GetAbsPath(filePath, dwCompileUnit->mCompileDir);
  4266. AddSrcFile(dwCompileUnit, filePath.c_str());
  4267. }
  4268. if (dwCompileUnit->mSrcFileRefs.size() > 0)
  4269. dwSrcFileRef = &dwCompileUnit->mSrcFileRefs.front();
  4270. DbgLineDataBuilder lineBuilder(this);
  4271. bool queuedPostPrologue = false;
  4272. DbgLineDataState dwLineData;
  4273. dwLineData.mLine = 0;
  4274. dwLineData.mRelAddress = 0;
  4275. dwLineData.mOpIndex = 0;
  4276. dwLineData.mBasicBlock = false;
  4277. dwLineData.mDiscriminator = 0;
  4278. dwLineData.mIsStmt = defaultIsStmt != 0;
  4279. dwLineData.mIsa = 0;
  4280. dwLineData.mColumn = -2;
  4281. while (data < dataEnd)
  4282. {
  4283. uint8_t opcode = GET(uint8_t);
  4284. switch (opcode)
  4285. {
  4286. case DW_LNS_extended_op:
  4287. {
  4288. int len = (int)DecodeULEB128(data);
  4289. uint8_t exOpcode = GET(uint8_t);
  4290. switch (exOpcode)
  4291. {
  4292. case DW_LNE_end_sequence:
  4293. {
  4294. ADD_LINEDATA(dwLineData);
  4295. dwSrcFileRef = &dwCompileUnit->mSrcFileRefs[0];
  4296. dwLineData.mLine = 0;
  4297. dwLineData.mRelAddress = 0;
  4298. dwLineData.mOpIndex = 0;
  4299. dwLineData.mBasicBlock = false;
  4300. dwLineData.mDiscriminator = 0;
  4301. dwLineData.mIsStmt = defaultIsStmt != 0;
  4302. dwLineData.mIsa = 0;
  4303. dwLineData.mColumn = -2;
  4304. }
  4305. break;
  4306. case DW_LNE_set_address:
  4307. dwLineData.mRelAddress = (uint32)(RemapAddr(GET(addr_target)) - mImageBase);
  4308. break;
  4309. case DW_LNE_define_file:
  4310. {
  4311. const char* path = DataGetString(data);
  4312. int directoryIdx = (int)DecodeULEB128(data);
  4313. int lastModificationTime = (int)DecodeULEB128(data);
  4314. int fileLength = (int)DecodeULEB128(data);
  4315. }
  4316. break;
  4317. case DW_LNE_set_discriminator:
  4318. dwLineData.mDiscriminator = (int)DecodeULEB128(data);
  4319. break;
  4320. }
  4321. }
  4322. break;
  4323. case DW_LNS_copy:
  4324. ADD_LINEDATA(dwLineData);
  4325. dwLineData.mDiscriminator = 0;
  4326. dwLineData.mBasicBlock = false;
  4327. break;
  4328. case DW_LNS_advance_pc:
  4329. {
  4330. int advance = (int)DecodeULEB128(data);
  4331. dwLineData.mRelAddress += advance;
  4332. // How to advance opCode addr?
  4333. }
  4334. break;
  4335. case DW_LNS_advance_line:
  4336. {
  4337. int advance = (int)DecodeSLEB128(data);
  4338. dwLineData.mLine += advance;
  4339. }
  4340. break;
  4341. case DW_LNS_set_file:
  4342. {
  4343. curFileIdx = (int)DecodeULEB128(data) - 1;
  4344. dwSrcFileRef = &dwCompileUnit->mSrcFileRefs[curFileIdx];
  4345. //dwLineData.mSrcFileRef = dwSrcFileRef;
  4346. }
  4347. break;
  4348. case DW_LNS_set_column:
  4349. {
  4350. dwLineData.mColumn = (int)DecodeULEB128(data) - 1;
  4351. }
  4352. break;
  4353. case DW_LNS_negate_stmt:
  4354. {
  4355. dwLineData.mIsStmt = !dwLineData.mIsStmt;
  4356. }
  4357. break;
  4358. case DW_LNS_set_basic_block:
  4359. {
  4360. dwLineData.mBasicBlock = true;
  4361. }
  4362. break;
  4363. case DW_LNS_const_add_pc:
  4364. {
  4365. int adjustedOpcode = 255 - opcodeBase;
  4366. int opAdvance = adjustedOpcode / lineRange;
  4367. uint32 newAddress = dwLineData.mRelAddress + minimumInstructionLength * ((dwLineData.mOpIndex + opAdvance) / maximumOperationsPerInstruction);
  4368. int newOpIndex = (dwLineData.mOpIndex + opAdvance) % maximumOperationsPerInstruction;
  4369. dwLineData.mRelAddress = newAddress;
  4370. dwLineData.mOpIndex = newOpIndex;
  4371. }
  4372. break;
  4373. case DW_LNS_fixed_advance_pc:
  4374. {
  4375. uint16_t advance = GET(uint16_t);
  4376. dwLineData.mRelAddress += advance;
  4377. dwLineData.mOpIndex = 0;
  4378. }
  4379. break;
  4380. case DW_LNS_set_prologue_end:
  4381. {
  4382. queuedPostPrologue = true;
  4383. }
  4384. break;
  4385. case DW_LNS_set_epilogue_begin:
  4386. {
  4387. dwLineData.mColumn = -2;
  4388. }
  4389. break;
  4390. case DW_LNS_set_isa:
  4391. {
  4392. dwLineData.mIsa = (int)DecodeULEB128(data);
  4393. }
  4394. break;
  4395. default:
  4396. {
  4397. // Special opcode
  4398. int adjustedOpcode = opcode - opcodeBase;
  4399. int opAdvance = adjustedOpcode / lineRange;
  4400. uint32 oldAddress = dwLineData.mRelAddress;
  4401. uint32 newAddress = dwLineData.mRelAddress + minimumInstructionLength * ((dwLineData.mOpIndex + opAdvance) / maximumOperationsPerInstruction);
  4402. int newOpIndex = (dwLineData.mOpIndex + opAdvance) % maximumOperationsPerInstruction;
  4403. int lineIncrement = lineBase + (adjustedOpcode % lineRange);
  4404. dwLineData.mLine += lineIncrement;
  4405. dwLineData.mRelAddress = newAddress;
  4406. dwLineData.mOpIndex = newOpIndex;
  4407. DbgLineData* lastLineData = NULL;
  4408. if ((newAddress == oldAddress) && (queuedPostPrologue) && (curSubprogram != NULL) && (curSubprogram->mBlock.mLowPC == newAddress))
  4409. {
  4410. // Adjust this line later
  4411. ADD_LINEDATA(dwLineData);
  4412. }
  4413. queuedPostPrologue = false;
  4414. }
  4415. break;
  4416. }
  4417. }
  4418. lineBuilder.Commit();
  4419. dataPtr = data;
  4420. return true;
  4421. }
  4422. addr_target DbgModule::GetHotTargetAddress(DbgHotTargetSection* hotTargetSection)
  4423. {
  4424. if ((hotTargetSection->mTargetSectionAddr == NULL) && (hotTargetSection->mDataSize > 0))
  4425. {
  4426. if (hotTargetSection->mNoTargetAlloc)
  4427. return 0;
  4428. BfLogDbg("DbgModule::GetHotTargetAddress %p %p\n", this, hotTargetSection);
  4429. hotTargetSection->mTargetSectionAddr = mDebugger->AllocHotTargetMemory(hotTargetSection->mDataSize, hotTargetSection->mCanExecute, hotTargetSection->mCanWrite, &hotTargetSection->mTargetSectionSize);
  4430. hotTargetSection->mImageOffset = (int)mImageSize;
  4431. if (mImageBase == NULL)
  4432. {
  4433. mImageBase = hotTargetSection->mTargetSectionAddr;
  4434. mOrigImageData->mAddr = mImageBase;
  4435. }
  4436. mImageSize += hotTargetSection->mTargetSectionSize;
  4437. /*if (mExceptionData == hotTargetSection->mData)
  4438. mExceptionDataRVA = (addr_target)(hotTargetSection->mTargetSectionAddr - mImageBase);*/
  4439. }
  4440. return hotTargetSection->mTargetSectionAddr;
  4441. }
  4442. uint8* DbgModule::GetHotTargetData(addr_target address)
  4443. {
  4444. for (int sectNum = 0; sectNum < (int)mHotTargetSections.size(); sectNum++)
  4445. {
  4446. if (mHotTargetSections[sectNum] != NULL)
  4447. {
  4448. DbgHotTargetSection* hotTargetSection = mHotTargetSections[sectNum];
  4449. if ((address >= hotTargetSection->mTargetSectionAddr) && (address < hotTargetSection->mTargetSectionAddr + hotTargetSection->mTargetSectionSize))
  4450. {
  4451. return hotTargetSection->mData + (address - hotTargetSection->mTargetSectionAddr);
  4452. }
  4453. }
  4454. }
  4455. return NULL;
  4456. }
  4457. void DbgModule::DoReloc(DbgHotTargetSection* hotTargetSection, COFFRelocation& coffReloc, addr_target resolvedSymbolAddr, PE_SymInfo* symInfo)
  4458. {
  4459. #ifdef BF_DBG_32
  4460. if (coffReloc.mType == IMAGE_REL_I386_DIR32)
  4461. {
  4462. *(uint32*)(hotTargetSection->mData + coffReloc.mVirtualAddress) += resolvedSymbolAddr;
  4463. }
  4464. else if (coffReloc.mType == IMAGE_REL_I386_DIR32NB)
  4465. {
  4466. GetHotTargetAddress(hotTargetSection); // Just to make sure we have mImageBase
  4467. // We were previously using mImageBase instead of mDebugTarget->mTargetBinary->mImageBase. Was there a reason for that?
  4468. // It was causing hot-loaded jump tables to have invalid addresses since the need to be relative to __ImageBase
  4469. *(uint32*)(hotTargetSection->mData + coffReloc.mVirtualAddress) += (uint32)(resolvedSymbolAddr - GetTargetImageBase());
  4470. }
  4471. else if (coffReloc.mType == IMAGE_REL_I386_REL32)
  4472. {
  4473. addr_target myAddr = GetHotTargetAddress(hotTargetSection) + coffReloc.mVirtualAddress;
  4474. *(uint32*)(hotTargetSection->mData + coffReloc.mVirtualAddress) += resolvedSymbolAddr - myAddr - sizeof(int32);
  4475. }
  4476. else if (coffReloc.mType == IMAGE_REL_I386_SECTION)
  4477. {
  4478. // auto linkedModule = GetLinkedModule();
  4479. // addr_target mappedAddr = resolvedSymbolAddr & ~0x7FFFFFF;
  4480. // int* encodingPtr = NULL;
  4481. // if (linkedModule->mSecRelEncodingMap.TryAdd(mappedAddr, NULL, &encodingPtr))
  4482. // {
  4483. // *encodingPtr = (int)linkedModule->mSecRelEncodingVec.size();
  4484. // linkedModule->mSecRelEncodingVec.push_back(mappedAddr);
  4485. // }
  4486. // *(uint16*)(hotTargetSection->mData + coffReloc.mVirtualAddress) = 0x8000 | *encodingPtr;
  4487. *(uint16*)(hotTargetSection->mData + coffReloc.mVirtualAddress) = 0;
  4488. }
  4489. else if (coffReloc.mType == IMAGE_REL_I386_SECREL)
  4490. {
  4491. //*(uint32*)(hotTargetSection->mData + coffReloc.mVirtualAddress) += symInfo->mValue;
  4492. *(uint32*)(hotTargetSection->mData + coffReloc.mVirtualAddress) += resolvedSymbolAddr;
  4493. }
  4494. else
  4495. {
  4496. BF_ASSERT(0=="Invalid COFF reloc type");
  4497. }
  4498. #else
  4499. // CodeView uses SECTION:SECREL locations, and we just want to find a mapping such that
  4500. // COFF::GetSectionAddr can map it to the 64-bit address. We do this by encoding the
  4501. // lower 31 bits in the SECREL (allowing a 31-bit offset at the destination as well)
  4502. // and then we use a 15-bit key to map the upper bits
  4503. if (coffReloc.mType == IMAGE_REL_AMD64_REL32)
  4504. {
  4505. addr_target myAddr = GetHotTargetAddress(hotTargetSection) + coffReloc.mVirtualAddress;
  4506. intptr_target addrOffset = resolvedSymbolAddr - myAddr - sizeof(int32);
  4507. BF_ASSERT((int64)(int32)addrOffset == addrOffset);
  4508. *(uint32*)(hotTargetSection->mData + coffReloc.mVirtualAddress) += (int32)addrOffset;
  4509. }
  4510. else if (coffReloc.mType == IMAGE_REL_AMD64_SECTION)
  4511. {
  4512. /*if (symInfo != NULL)
  4513. {
  4514. *(uint16*)(hotTargetSection->mData + coffReloc.mVirtualAddress) = symInfo->mSectionNum;
  4515. }
  4516. else*/
  4517. {
  4518. auto linkedModule = GetLinkedModule();
  4519. addr_target mappedAddr = resolvedSymbolAddr & ~0x7FFFFFF;
  4520. /*auto pair = linkedModule->mSecRelEncodingMap.insert(std::make_pair(mappedAddr, (int)linkedModule->mSecRelEncodingMap.size()));
  4521. if (pair.second)
  4522. linkedModule->mSecRelEncodingVec.push_back(mappedAddr);*/
  4523. int* encodingPtr = NULL;
  4524. if (linkedModule->mSecRelEncodingMap.TryAdd(mappedAddr, NULL, &encodingPtr))
  4525. {
  4526. *encodingPtr = (int)linkedModule->mSecRelEncodingVec.size();
  4527. linkedModule->mSecRelEncodingVec.push_back(mappedAddr);
  4528. }
  4529. //*(uint16*)(hotTargetSection->mData + coffReloc.mVirtualAddress) = 0x8000 | pair.first->second;
  4530. *(uint16*)(hotTargetSection->mData + coffReloc.mVirtualAddress) = 0x8000 | *encodingPtr;
  4531. }
  4532. }
  4533. else if (coffReloc.mType == IMAGE_REL_AMD64_SECREL)
  4534. {
  4535. auto linkedModule = GetLinkedModule();
  4536. if ((resolvedSymbolAddr >= linkedModule->mTLSAddr) && (resolvedSymbolAddr < linkedModule->mTLSAddr + linkedModule->mTLSSize))
  4537. {
  4538. // Make relative to actual TLS data
  4539. resolvedSymbolAddr -= linkedModule->mTLSAddr;
  4540. }
  4541. *(uint32*)(hotTargetSection->mData + coffReloc.mVirtualAddress) += (uint32)(resolvedSymbolAddr & 0x7FFFFFF);
  4542. }
  4543. else if (coffReloc.mType == IMAGE_REL_AMD64_ADDR64)
  4544. {
  4545. *(uint64*)(hotTargetSection->mData + coffReloc.mVirtualAddress) += resolvedSymbolAddr;
  4546. }
  4547. else if (coffReloc.mType == IMAGE_REL_AMD64_ADDR32NB)
  4548. {
  4549. GetHotTargetAddress(hotTargetSection); // Just to make sure we have mImageBase
  4550. // We were previously using mImageBase instead of mDebugTarget->mTargetBinary->mImageBase. Was there a reason for that?
  4551. // It was causing hot-loaded jump tables to have invalid addresses since the need to be relative to __ImageBase
  4552. *(uint32*)(hotTargetSection->mData + coffReloc.mVirtualAddress) += (uint32)(resolvedSymbolAddr - GetTargetImageBase());
  4553. //*(int32*)(hotTargetSection->mData + coffReloc.mVirtualAddress) += secRelAddr;
  4554. }
  4555. else
  4556. {
  4557. BF_ASSERT(0=="Invalid COFF reloc type");
  4558. }
  4559. #endif
  4560. }
  4561. bool DbgModule::IsHotSwapPreserve(const String& name)
  4562. {
  4563. // We have different rules for overwriting symbols in DWARF vs CodeView
  4564. // Since MS mangling includes return types, we know that a type change of a static
  4565. // member will mangle to a new name whereas with DWARF we DO want a new
  4566. // address if the type changes but we can't tell that based on the mangle alone,
  4567. // thus the reliance on the side table of mStaticVariables. We still do need
  4568. // to determine whether the symbol is data (and thus we do preserve) or a method
  4569. // (in which case we don't)
  4570. if ((mDbgFlavor == DbgFlavor_MS) && (BfDemangler::IsData(name)))
  4571. {
  4572. if ((!name.StartsWith("?")) && (name.Contains("sBfTypeData"))) // We DO need to replace the fields/methods/etc but not the base sBfTypeData
  4573. return false;
  4574. if (name.StartsWith("?bf_hs_replace_"))
  4575. return false;
  4576. return true;
  4577. }
  4578. const char* prefix = "bf_hs_preserve@";
  4579. return strncmp(name.c_str(), prefix, strlen(prefix)) == 0;
  4580. }
  4581. void DbgModule::ParseHotTargetSections(DataStream* stream, addr_target* resolvedSymbolAddrs)
  4582. {
  4583. auto mainModule = mDebugTarget->mTargetBinary;
  4584. mainModule->ParseSymbolData();
  4585. String name;
  4586. for (int sectNum = 0; sectNum < (int)mHotTargetSections.size(); sectNum++)
  4587. {
  4588. if (mHotTargetSections[sectNum] != NULL)
  4589. {
  4590. DbgHotTargetSection* hotTargetSection = mHotTargetSections[sectNum];
  4591. stream->SetPos(hotTargetSection->mPointerToRelocations);
  4592. for (int relocIdx = 0; relocIdx < hotTargetSection->mNumberOfRelocations; relocIdx++)
  4593. {
  4594. COFFRelocation coffReloc;
  4595. stream->Read(&coffReloc, sizeof(COFFRelocation));
  4596. PE_SymInfo* symInfo = (PE_SymInfo*)&mSymbolData[coffReloc.mSymbolTableIndex * 18];
  4597. //const char* symName = mSymbolData[coffReloc.mSymbolTableIndex];
  4598. bool isStaticSymbol = symInfo->mStorageClass == COFF_SYM_CLASS_STATIC;
  4599. if (symInfo->mNameOfs[0] != 0)
  4600. {
  4601. if (symInfo->mName[7] != 0)
  4602. {
  4603. // Name is exactly 8 chars, not null terminated yet
  4604. name = String(symInfo->mName, symInfo->mName + 8);
  4605. }
  4606. else
  4607. name = symInfo->mName;
  4608. }
  4609. else
  4610. name = mStringTable + symInfo->mNameOfs[1];
  4611. bool didNameMatch = false;
  4612. addr_target resolvedSymbolAddr = resolvedSymbolAddrs[coffReloc.mSymbolTableIndex];
  4613. #ifdef BF_DBG_32
  4614. bool needsSymbolAddr = (coffReloc.mType == IMAGE_REL_I386_DIR32) || (coffReloc.mType == IMAGE_REL_I386_REL32) || (coffReloc.mType == IMAGE_REL_I386_SECREL) || (coffReloc.mType == IMAGE_REL_I386_SECTION);
  4615. if (name[0] == '_')
  4616. name.Remove(0, 1);
  4617. #else
  4618. bool needsSymbolAddr = (coffReloc.mType == IMAGE_REL_AMD64_ADDR64) || (coffReloc.mType == IMAGE_REL_AMD64_ADDR32) || (coffReloc.mType == IMAGE_REL_AMD64_ADDR32NB) ||
  4619. ((coffReloc.mType >= IMAGE_REL_AMD64_REL32) || (coffReloc.mType <= IMAGE_REL_AMD64_REL32_5));
  4620. #endif
  4621. bool isHsPrev = false;
  4622. if (name.StartsWith("bf_hs_prev@"))
  4623. {
  4624. isHsPrev = true;
  4625. name.Remove(0, 11);
  4626. }
  4627. bool deferResolve = false;
  4628. if ((resolvedSymbolAddr == 0) && (needsSymbolAddr))
  4629. {
  4630. bool isHotSwapPreserve = IsHotSwapPreserve(name);
  4631. if ((symInfo->mSectionNum == 0) || (isHotSwapPreserve) || (isHsPrev))
  4632. {
  4633. auto origSymbolEntry = mainModule->mSymbolNameMap.Find(name.c_str());
  4634. if (origSymbolEntry != NULL)
  4635. {
  4636. resolvedSymbolAddr = origSymbolEntry->mValue->mAddress;
  4637. }
  4638. else
  4639. {
  4640. //BF_FATAL("Symbol lookup error");
  4641. deferResolve = true;
  4642. }
  4643. }
  4644. if ((symInfo->mSectionNum != 0) && (resolvedSymbolAddr == NULL))
  4645. {
  4646. DbgHotTargetSection* refHotTargetSection = mHotTargetSections[symInfo->mSectionNum - 1];
  4647. resolvedSymbolAddr = GetHotTargetAddress(refHotTargetSection) + symInfo->mValue;
  4648. // Using the !hotTargetSection->mNoTargetAlloc check down here caused us to not properly remap reloaded
  4649. // static members in the debug info. Even though we parse the debug info before we apply the deferred
  4650. // resolves, the mLocData points into the original data so we still get it remapped when we use that
  4651. // mLocData
  4652. if (/*(!hotTargetSection->mNoTargetAlloc) &&*/ ((refHotTargetSection->mData == NULL) || (refHotTargetSection->mNoTargetAlloc)) &&
  4653. (!isStaticSymbol))
  4654. deferResolve = true;
  4655. else
  4656. deferResolve = false;
  4657. }
  4658. }
  4659. if (deferResolve)
  4660. {
  4661. // It's a static field, defer resolution, but don't bother replacing for debug info sections
  4662. DbgDeferredHotResolve* deferredResolve = mDeferredHotResolveList.Alloc();
  4663. deferredResolve->mHotTargetSection = hotTargetSection;
  4664. deferredResolve->mName = name;
  4665. deferredResolve->mNewAddr = resolvedSymbolAddr;
  4666. deferredResolve->mReloc = coffReloc;
  4667. continue;
  4668. }
  4669. else
  4670. {
  4671. resolvedSymbolAddrs[coffReloc.mSymbolTableIndex] = resolvedSymbolAddr;
  4672. DoReloc(hotTargetSection, coffReloc, resolvedSymbolAddr, symInfo);
  4673. }
  4674. }
  4675. }
  4676. }
  4677. }
  4678. void DbgModule::CommitHotTargetSections()
  4679. {
  4680. BfLogDbg("DbgModule::CommitHotTargetSections %p\n", this);
  4681. for (int sectNum = 0; sectNum < (int)mHotTargetSections.size(); sectNum++)
  4682. {
  4683. if (mHotTargetSections[sectNum] != NULL)
  4684. {
  4685. DbgHotTargetSection* hotTargetSection = mHotTargetSections[sectNum];
  4686. addr_target hotAddr = GetHotTargetAddress(hotTargetSection);
  4687. if (hotAddr != 0)
  4688. {
  4689. // void* imageDestPtr = mOrigImageData->mBlocks[0] + hotTargetSection->mImageOffset;
  4690. // if (hotTargetSection->mData != NULL)
  4691. // memcpy(imageDestPtr, hotTargetSection->mData, hotTargetSection->mDataSize);
  4692. // else
  4693. // memset(imageDestPtr, 0, hotTargetSection->mDataSize);
  4694. BF_ASSERT(mOrigImageData->mAddr != 0);
  4695. void* imageDestPtr = hotTargetSection->mData;
  4696. bool isTemp = false;
  4697. if (imageDestPtr == NULL)
  4698. {
  4699. imageDestPtr = new uint8[hotTargetSection->mDataSize];
  4700. memset(imageDestPtr, 0, hotTargetSection->mDataSize);
  4701. isTemp = true;
  4702. }
  4703. if (hotTargetSection->mCanExecute)
  4704. {
  4705. bool success = mDebugger->WriteInstructions(hotAddr, imageDestPtr, hotTargetSection->mDataSize);
  4706. BF_ASSERT(success);
  4707. }
  4708. else
  4709. {
  4710. bool success = mDebugger->WriteMemory(hotAddr, imageDestPtr, hotTargetSection->mDataSize);
  4711. BF_ASSERT(success);
  4712. }
  4713. if (isTemp)
  4714. delete imageDestPtr;
  4715. }
  4716. }
  4717. }
  4718. }
  4719. void DbgModule::HotReplaceType(DbgType* newType)
  4720. {
  4721. auto linkedModule = GetLinkedModule();
  4722. newType->PopulateType();
  4723. DbgType* primaryType = linkedModule->GetPrimaryType(newType);
  4724. if (primaryType == newType)
  4725. {
  4726. // There was no previous type
  4727. BF_ASSERT(primaryType->mHotNewType == NULL);
  4728. return;
  4729. }
  4730. if (primaryType->mHotNewType != newType)
  4731. {
  4732. // We have already pulled in the new data from a previous new type
  4733. BF_ASSERT(primaryType->mHotNewType == NULL);
  4734. return;
  4735. }
  4736. primaryType->mHotNewType = NULL;
  4737. primaryType->PopulateType();
  4738. linkedModule->ParseGlobalsData();
  4739. linkedModule->ParseSymbolData();
  4740. if (primaryType->mNeedsGlobalsPopulated)
  4741. {
  4742. // These aren't proper TPI types so we don't have any method declarations until we PopulateTypeGlobals
  4743. linkedModule->PopulateTypeGlobals(primaryType);
  4744. }
  4745. for (auto methodNameEntry : primaryType->mMethodNameList)
  4746. {
  4747. if (methodNameEntry->mCompileUnitId != -1)
  4748. {
  4749. linkedModule->MapCompileUnitMethods(methodNameEntry->mCompileUnitId);
  4750. methodNameEntry->mCompileUnitId = -1;
  4751. }
  4752. }
  4753. // Now actually remove the linedata from the defining module
  4754. HashSet<DbgSrcFile*> checkedFiles;
  4755. for (auto method : primaryType->mMethodList)
  4756. {
  4757. //method->mWasModuleHotReplaced = true;
  4758. method->mHotReplaceKind = DbgSubprogram::HotReplaceKind_Orphaned; // May be temporarily orphaned
  4759. if (method->mLineInfo == NULL)
  4760. continue;
  4761. //FIXME: Hot replacing lines
  4762. DbgSrcFile* lastSrcFile = NULL;
  4763. checkedFiles.Clear();
  4764. int prevCtx = -1;
  4765. auto inlineRoot = method->GetRootInlineParent();
  4766. for (int lineIdx = 0; lineIdx < method->mLineInfo->mLines.mSize; lineIdx++)
  4767. {
  4768. auto& lineData = method->mLineInfo->mLines[lineIdx];
  4769. if (lineData.mCtxIdx != prevCtx)
  4770. {
  4771. auto ctxInfo = inlineRoot->mLineInfo->mContexts[lineData.mCtxIdx];
  4772. auto srcFile = ctxInfo.mSrcFile;
  4773. prevCtx = lineData.mCtxIdx;
  4774. if (srcFile != lastSrcFile)
  4775. {
  4776. if (checkedFiles.Add(srcFile))
  4777. {
  4778. // Remove linedata for old type
  4779. // These go into a hot-replaced list so we can still bind to them -- that is necessary because
  4780. // we may still have old versions of this method running (and may forever, if its in a loop on some thread)
  4781. // since we only patch entry points
  4782. //srcFile->RemoveLines(primaryType->mCompileUnit->mDbgModule, primaryType->mCompileUnit, true);
  4783. //srcFile->RemoveLines(primaryType->mCompileUnit->mDbgModule, method, true);
  4784. srcFile->RemoveLines(method->mCompileUnit->mDbgModule, method, true);
  4785. }
  4786. lastSrcFile = srcFile;
  4787. }
  4788. }
  4789. }
  4790. }
  4791. //DbgType* primaryType = newType->GetPrimaryType();
  4792. // We need to keep a persistent list of hot replaced methods so we can set hot jumps
  4793. // in old methods that may still be on the callstack. These entries get removed when
  4794. // we unload unused hot files in
  4795. while (!primaryType->mMethodList.IsEmpty())
  4796. {
  4797. auto method = primaryType->mMethodList.PopFront();
  4798. method->PopulateSubprogram();
  4799. primaryType->mHotReplacedMethodList.PushFront(method);
  4800. mHotPrimaryTypes.Add(primaryType);
  4801. }
  4802. Dictionary<StringView, DbgSubprogram*> oldProgramMap;
  4803. for (auto oldMethod : primaryType->mHotReplacedMethodList)
  4804. {
  4805. oldMethod->PopulateSubprogram();
  4806. if (oldMethod->mBlock.IsEmpty())
  4807. continue;
  4808. auto symInfo = mDebugTarget->mSymbolMap.Get(oldMethod->mBlock.mLowPC);
  4809. if (symInfo != NULL)
  4810. {
  4811. oldProgramMap.TryAdd(symInfo->mName, oldMethod);
  4812. }
  4813. }
  4814. bool setHotJumpFailed = false;
  4815. while (!newType->mMethodList.IsEmpty())
  4816. {
  4817. DbgSubprogram* newMethod = newType->mMethodList.PopFront();
  4818. if (!newMethod->mBlock.IsEmpty())
  4819. {
  4820. BfLogDbg("Hot added new method %p %s Address:%p\n", newMethod, newMethod->mName, newMethod->mBlock.mLowPC);
  4821. newMethod->PopulateSubprogram();
  4822. auto symInfo = mDebugTarget->mSymbolMap.Get(newMethod->mBlock.mLowPC);
  4823. if (symInfo != NULL)
  4824. {
  4825. DbgSubprogram* oldMethod = NULL;
  4826. if (oldProgramMap.TryGetValue(symInfo->mName, &oldMethod))
  4827. {
  4828. bool doHotJump = false;
  4829. if (oldMethod->Equals(newMethod))
  4830. {
  4831. doHotJump = true;
  4832. }
  4833. else
  4834. {
  4835. // When mangles match but the actual signatures don't match, that can mean that the call signature was changed
  4836. // and thus it's actually a different method and shouldn't hot jump OR it could be lambda whose captures changed.
  4837. // When the lambda captures change, the user didn't actually enter a different signature so we want to do a hard
  4838. // fail if the old code gets called to avoid confusion of "why aren't my changes working?"
  4839. // If we removed captures then we can still do the hot jump. Otherwise we have to fail...
  4840. doHotJump = false;
  4841. if ((oldMethod->IsLambda()) && (oldMethod->Equals(newMethod, true)) &&
  4842. (oldMethod->mHasThis) && (newMethod->mHasThis))
  4843. {
  4844. auto oldParam = oldMethod->mParams.front();
  4845. auto newParam = newMethod->mParams.front();
  4846. if ((oldParam->mType->IsPointer()) && (newParam->mType->IsPointer()))
  4847. {
  4848. auto oldType = oldParam->mType->mTypeParam->GetPrimaryType();
  4849. oldType->PopulateType();
  4850. auto newType = newParam->mType->mTypeParam->GetPrimaryType();
  4851. newType->PopulateType();
  4852. if ((oldType->IsStruct()) && (newType->IsStruct()))
  4853. {
  4854. bool wasMatch = true;
  4855. auto oldMember = oldType->mMemberList.front();
  4856. auto newMember = newType->mMemberList.front();
  4857. while (newMember != NULL)
  4858. {
  4859. if (oldMember == NULL)
  4860. {
  4861. wasMatch = false;
  4862. break;
  4863. }
  4864. if ((oldMember->mName == NULL) || (newMember->mName == NULL))
  4865. {
  4866. wasMatch = false;
  4867. break;
  4868. }
  4869. if (strcmp(oldMember->mName, newMember->mName) != 0)
  4870. {
  4871. wasMatch = false;
  4872. break;
  4873. }
  4874. if (!oldMember->mType->Equals(newMember->mType))
  4875. {
  4876. wasMatch = false;
  4877. break;
  4878. }
  4879. oldMember = oldMember->mNext;
  4880. newMember = newMember->mNext;
  4881. }
  4882. if (wasMatch)
  4883. doHotJump = true;
  4884. }
  4885. }
  4886. if (!doHotJump)
  4887. {
  4888. mDebugTarget->mDebugger->PhysSetBreakpoint(oldMethod->mBlock.mLowPC);
  4889. oldMethod->mHotReplaceKind = DbgSubprogram::HotReplaceKind_Invalid;
  4890. }
  4891. }
  4892. }
  4893. if (doHotJump)
  4894. {
  4895. if (!setHotJumpFailed)
  4896. {
  4897. if (!mDebugger->SetHotJump(oldMethod, newMethod->mBlock.mLowPC, (int)(newMethod->mBlock.mHighPC - newMethod->mBlock.mLowPC)))
  4898. setHotJumpFailed = true;
  4899. }
  4900. oldMethod->mHotReplaceKind = DbgSubprogram::HotReplaceKind_Replaced;
  4901. }
  4902. }
  4903. }
  4904. }
  4905. newMethod->mParentType = primaryType;
  4906. primaryType->mMethodList.PushBack(newMethod);
  4907. }
  4908. //mDebugTarget->mSymbolMap.Get()
  4909. // bool setHotJumpFailed = false;
  4910. // while (!newType->mMethodList.IsEmpty())
  4911. // {
  4912. // DbgSubprogram* newMethod = newType->mMethodList.PopFront();
  4913. // if (!newMethod->mBlock.IsEmpty())
  4914. // {
  4915. // newMethod->PopulateSubprogram();
  4916. //
  4917. // bool found = false;
  4918. // for (auto oldMethod : primaryType->mHotReplacedMethodList)
  4919. // {
  4920. // if (oldMethod->mBlock.IsEmpty())
  4921. // continue;
  4922. // if (oldMethod->Equals(newMethod))
  4923. // {
  4924. // if (!setHotJumpFailed)
  4925. // {
  4926. // if (!mDebugger->SetHotJump(oldMethod, newMethod))
  4927. // setHotJumpFailed = true;
  4928. // oldMethod->mWasHotReplaced = true;
  4929. // }
  4930. // }
  4931. // }
  4932. // }
  4933. // newMethod->mParentType = primaryType;
  4934. // primaryType->mMethodList.PushBack(newMethod);
  4935. // }
  4936. primaryType->mCompileUnit->mWasHotReplaced = true;
  4937. primaryType->mNeedsGlobalsPopulated = newType->mNeedsGlobalsPopulated;
  4938. primaryType->mUsingNamespaces = newType->mUsingNamespaces;
  4939. primaryType->mMemberList = newType->mMemberList;
  4940. primaryType->mCompileUnit = newType->mCompileUnit;
  4941. }
  4942. bool DbgModule::CanRead(DataStream* stream, DebuggerResult* outResult)
  4943. {
  4944. PEHeader hdr;
  4945. memset(&hdr, 0, sizeof(hdr));
  4946. PE_NTHeaders ntHdr;
  4947. memset(&ntHdr, 0, sizeof(ntHdr));
  4948. stream->Read(&hdr, sizeof(PEHeader));
  4949. stream->SetPos(hdr.e_lfanew);
  4950. stream->Read(&ntHdr, sizeof(PE_NTHeaders));
  4951. if ((hdr.e_magic != PE_DOS_SIGNATURE) || (ntHdr.mSignature != PE_NT_SIGNATURE))
  4952. {
  4953. *outResult = DebuggerResult_UnknownError;
  4954. return false;
  4955. }
  4956. #ifdef BF_DBG_32
  4957. if (ntHdr.mFileHeader.mMachine != PE_MACHINE_X86)
  4958. {
  4959. if (ntHdr.mFileHeader.mMachine == PE_MACHINE_X64)
  4960. *outResult = DebuggerResult_WrongBitSize;
  4961. else
  4962. *outResult = DebuggerResult_UnknownError;
  4963. return false;
  4964. }
  4965. #else
  4966. if (ntHdr.mFileHeader.mMachine != PE_MACHINE_X64)
  4967. {
  4968. if (ntHdr.mFileHeader.mMachine == PE_MACHINE_X86)
  4969. *outResult = DebuggerResult_WrongBitSize;
  4970. else
  4971. *outResult = DebuggerResult_UnknownError;
  4972. return false;
  4973. }
  4974. #endif
  4975. return true;
  4976. }
  4977. const char* DbgModule::GetStringTable(DataStream* stream, int stringTablePos)
  4978. {
  4979. if (mStringTable == NULL)
  4980. {
  4981. int prevPos = stream->GetPos();
  4982. stream->SetPos(stringTablePos);
  4983. int strTableSize = 0;
  4984. stream->Read(&strTableSize, 4);
  4985. if (strTableSize != 0)
  4986. {
  4987. strTableSize -= 4;
  4988. char* strTableData = new char[strTableSize + 4];
  4989. memcpy(strTableData, &strTableSize, 4);
  4990. stream->Read(strTableData + 4, strTableSize);
  4991. mStringTable = strTableData;
  4992. }
  4993. stream->SetPos(prevPos);
  4994. }
  4995. return mStringTable;
  4996. }
  4997. bool DbgModule::ReadCOFF(DataStream* stream, DbgModuleKind moduleKind)
  4998. {
  4999. BP_ZONE("DbgModule::ReadCOFF");
  5000. //if (this == mDebugTarget->mTargetBinary)
  5001. //mMemReporter = new MemReporter();
  5002. BfLogDbg("DbgModule::ReadCOFF %p %s\n", this, mFilePath.c_str());
  5003. if (mMemReporter != NULL)
  5004. {
  5005. mMemReporter->BeginSection(StrFormat("Module: %s", mFilePath.c_str()));
  5006. mMemReporter->Add(mImageSize);
  5007. }
  5008. defer
  5009. (
  5010. if (mMemReporter != NULL)
  5011. mMemReporter->EndSection();
  5012. );
  5013. DbgModule* mainModule = mDebugTarget->mTargetBinary;
  5014. MiniDumpDebugger* miniDumpDebugger = NULL;
  5015. if (mDebugger->IsMiniDumpDebugger())
  5016. {
  5017. miniDumpDebugger = (MiniDumpDebugger*)mDebugger;
  5018. }
  5019. mModuleKind = moduleKind;
  5020. bool isHotSwap = mModuleKind == DbgModuleKind_HotObject;
  5021. bool isObjectFile = mModuleKind != DbgModuleKind_Module;
  5022. auto linkedModule = GetLinkedModule();
  5023. if (isObjectFile)
  5024. linkedModule->PopulateStaticVariableMap();
  5025. mStartTypeIdx = (int)linkedModule->mTypes.size();
  5026. int startSrcFile = (int)mDebugTarget->mSrcFiles.size();
  5027. mStartSubprogramIdx = (int)mSubprograms.size();
  5028. PEHeader hdr;
  5029. memset(&hdr, 0, sizeof(hdr));
  5030. PE_NTHeaders ntHdr;
  5031. memset(&ntHdr, 0, sizeof(ntHdr));
  5032. if (!isObjectFile)
  5033. {
  5034. stream->Read(&hdr, sizeof(PEHeader));
  5035. stream->SetPos(hdr.e_lfanew);
  5036. stream->Read(&ntHdr, sizeof(PE_NTHeaders));
  5037. mPreferredImageBase = ntHdr.mOptionalHeader.mImageBase;
  5038. if (mImageBase == 0)
  5039. {
  5040. BF_ASSERT(this == mainModule);
  5041. mImageBase = mPreferredImageBase;
  5042. }
  5043. if ((hdr.e_magic != PE_DOS_SIGNATURE) || (ntHdr.mSignature != PE_NT_SIGNATURE))
  5044. {
  5045. mLoadState = DbgModuleLoadState_Failed;
  5046. return false;
  5047. }
  5048. #ifdef BF_DBG_32
  5049. if (ntHdr.mFileHeader.mMachine != PE_MACHINE_X86)
  5050. return false;
  5051. #else
  5052. if (ntHdr.mFileHeader.mMachine != PE_MACHINE_X64)
  5053. {
  5054. mLoadState = DbgModuleLoadState_Failed;
  5055. return false;
  5056. }
  5057. #endif
  5058. int pos = hdr.e_lfanew + FIELD_OFFSET(PE_NTHeaders, mOptionalHeader) + ntHdr.mFileHeader.mSizeOfOptionalHeader;
  5059. stream->SetPos(pos);
  5060. }
  5061. else
  5062. {
  5063. stream->Read(&ntHdr.mFileHeader, sizeof(PEFileHeader));
  5064. if (mMemReporter != NULL)
  5065. mMemReporter->Add("PEFileHeader", sizeof(PEFileHeader));
  5066. #ifdef BF_DBG_32
  5067. if (ntHdr.mFileHeader.mMachine != PE_MACHINE_X86)
  5068. return false;
  5069. #else
  5070. if (ntHdr.mFileHeader.mMachine != PE_MACHINE_X64)
  5071. {
  5072. mLoadState = DbgModuleLoadState_Failed;
  5073. return false;
  5074. }
  5075. #endif
  5076. }
  5077. int sectionStartPos = stream->GetPos();
  5078. int sectionDataEndPos = 0;
  5079. if (miniDumpDebugger != NULL)
  5080. {
  5081. // Map header
  5082. miniDumpDebugger->MapMemory((addr_target)mImageBase, (uint8*)mMappedImageFile->mData, 0x1000);
  5083. }
  5084. bool wantStringTable = isObjectFile;
  5085. stream->SetPos(sectionStartPos);
  5086. for (int dirNum = 0; dirNum < (int) ntHdr.mFileHeader.mNumberOfSections; dirNum++)
  5087. {
  5088. PESectionHeader sectHdr;
  5089. char* name = sectHdr.mName;
  5090. stream->Read(&sectHdr, sizeof(PESectionHeader));
  5091. if (sectHdr.mSizeOfRawData > 0)
  5092. sectionDataEndPos = BF_MAX(sectionDataEndPos, (int)(sectHdr.mPointerToRawData + sectHdr.mSizeOfRawData));
  5093. if (sectHdr.mNumberOfRelocations > 0)
  5094. sectionDataEndPos = BF_MAX(sectionDataEndPos, (int)(sectHdr.mPointerToRelocations + sectHdr.mNumberOfRelocations * sizeof(COFFRelocation)));
  5095. if (miniDumpDebugger != NULL)
  5096. {
  5097. miniDumpDebugger->MapMemory((addr_target)(mImageBase + sectHdr.mVirtualAddress), (uint8*)mMappedImageFile->mData + sectHdr.mPointerToRawData, sectHdr.mSizeOfRawData);
  5098. }
  5099. }
  5100. //fseek(fp, sectionDataEndPos + ntHdr.mFileHeader.mNumberOfSymbols * 18, SEEK_SET);
  5101. stream->SetPos(sectionDataEndPos);
  5102. uint8* symbolData = new uint8[ntHdr.mFileHeader.mNumberOfSymbols * 18];
  5103. mAllocSizeData += ntHdr.mFileHeader.mNumberOfSymbols * 18;
  5104. mSymbolData = symbolData;
  5105. stream->Read(symbolData, ntHdr.mFileHeader.mNumberOfSymbols * 18);
  5106. int curPos = stream->GetPos();
  5107. int stringTablePos = curPos;
  5108. if (isObjectFile)
  5109. GetStringTable(stream, stringTablePos);
  5110. int mDebugFrameDataLen = 0;
  5111. stream->SetPos(sectionStartPos);
  5112. PEDataDirectory* exportDataDir = &ntHdr.mOptionalHeader.mDataDirectory[0];
  5113. mHotTargetSections.Resize(ntHdr.mFileHeader.mNumberOfSections);
  5114. Array<PESectionHeader> sectionHeaders;
  5115. sectionHeaders.Resize(ntHdr.mFileHeader.mNumberOfSections);
  5116. mSectionRVAs.Resize(sectionHeaders.size() + 1);
  5117. Array<String> sectionNames;
  5118. sectionNames.Resize(ntHdr.mFileHeader.mNumberOfSections);
  5119. stream->Read(&sectionHeaders[0], sizeof(PESectionHeader) * ntHdr.mFileHeader.mNumberOfSections);
  5120. for (int sectNum = 0; sectNum < ntHdr.mFileHeader.mNumberOfSections; sectNum++)
  5121. {
  5122. mSectionRVAs[sectNum] = sectionHeaders[sectNum].mVirtualAddress;
  5123. }
  5124. int tlsSection = -1;
  5125. for (int sectNum = 0; sectNum < ntHdr.mFileHeader.mNumberOfSections; sectNum++)
  5126. {
  5127. //PEDataDirectory* dataDir = &ntHdr.mOptionalHeader.mDataDirectory[dirNum];
  5128. PESectionHeader& sectHdr = sectionHeaders[sectNum];
  5129. //stream->Read(&sectHdr, sizeof(PESectionHeader));
  5130. const char* name = sectHdr.mName;
  5131. if (name[0] == '/')
  5132. {
  5133. int strIdx = atoi(name + 1);
  5134. name = &GetStringTable(stream, stringTablePos)[strIdx];
  5135. }
  5136. sectionNames[sectNum] = name;
  5137. DbgHotTargetSection* targetSection = NULL;
  5138. if (IsObjectFile())
  5139. {
  5140. targetSection = new DbgHotTargetSection();
  5141. targetSection->mDataSize = sectHdr.mSizeOfRawData;
  5142. targetSection->mPointerToRelocations = sectHdr.mPointerToRelocations;
  5143. targetSection->mNumberOfRelocations = sectHdr.mNumberOfRelocations;
  5144. targetSection->mTargetSectionAddr = 0; // TODO: Allocate!
  5145. targetSection->mCanExecute = (sectHdr.mCharacteristics & IMAGE_SCN_MEM_EXECUTE) != 0;
  5146. targetSection->mCanWrite = (sectHdr.mCharacteristics & IMAGE_SCN_MEM_WRITE) != 0;
  5147. targetSection->mNoTargetAlloc = (sectHdr.mCharacteristics & IMAGE_SCN_MEM_DISCARDABLE) != 0;
  5148. mHotTargetSections[sectNum] = targetSection;
  5149. }
  5150. DbgSection dwSection;
  5151. dwSection.mIsExecutable = (sectHdr.mCharacteristics & IMAGE_SCN_MEM_EXECUTE) != 0;
  5152. dwSection.mAddrStart = sectHdr.mVirtualAddress;
  5153. dwSection.mAddrLength = BF_MAX(sectHdr.mSizeOfRawData, sectHdr.mVirtualSize);
  5154. mSections.push_back(dwSection);
  5155. if (sectHdr.mPointerToRawData == 0)
  5156. continue;
  5157. if (strcmp(name, ".tls") == 0)
  5158. mTLSAddr = (addr_target)(sectHdr.mVirtualAddress + mImageBase);
  5159. if ((IsObjectFile()) && (strcmp(name, ".tls$") == 0))
  5160. {
  5161. tlsSection = sectNum;
  5162. mTLSSize = sectHdr.mSizeOfRawData;
  5163. targetSection->mNoTargetAlloc = true;
  5164. }
  5165. bool isExportDataDir = ((exportDataDir->mVirtualAddress != 0) && (exportDataDir->mVirtualAddress >= sectHdr.mVirtualAddress) && (exportDataDir->mVirtualAddress < sectHdr.mVirtualAddress + sectHdr.mSizeOfRawData));
  5166. if ((!IsObjectFile()) && (!isExportDataDir))
  5167. {
  5168. if (((strcmp(name, ".text")) == 0) ||
  5169. ((strcmp(name, ".textbss")) == 0) ||
  5170. ((strcmp(name, ".reloc")) == 0)/* ||
  5171. ((strcmp(name, ".data")) == 0)*/)
  5172. {
  5173. // Big unneeded sections
  5174. continue;
  5175. }
  5176. }
  5177. stream->SetPos(sectHdr.mPointerToRawData);
  5178. int dataSize = sectHdr.mSizeOfRawData + 8;
  5179. mAllocSizeData += dataSize;
  5180. uint8* data = new uint8[dataSize];
  5181. {
  5182. BP_ZONE("DbgModule::ReadCOFF_ReadSectionData");
  5183. stream->Read(data, sectHdr.mSizeOfRawData);
  5184. }
  5185. BfLogDbg("Read section data %s %p\n", name, data);
  5186. memset(data + sectHdr.mSizeOfRawData, 0, 8);
  5187. if (IsObjectFile())
  5188. targetSection->mData = data;
  5189. addr_target addrOffset = sectHdr.mVirtualAddress;
  5190. if (isExportDataDir)
  5191. {
  5192. BP_ZONE("DbgModule::ReadCOFF_SymbolMap");
  5193. IMAGE_EXPORT_DIRECTORY* imageExportDir = (IMAGE_EXPORT_DIRECTORY*)(data + (exportDataDir->mVirtualAddress - addrOffset));
  5194. for (int funcIdx = 0; funcIdx < (int)imageExportDir->NumberOfNames; funcIdx++)
  5195. {
  5196. //addr_target strAddr = *(addr_target*)(data + (imageExportDir->AddressOfNames - addrOffset) + funcIdx * sizeof(addr_target));
  5197. int32 strAddr = *(int32*)(data + (imageExportDir->AddressOfNames - addrOffset) + funcIdx * sizeof(int32));
  5198. const char* name = (const char*)(data + (strAddr - addrOffset));
  5199. #ifdef BF_DBG_32
  5200. if (name[0] == '_')
  5201. name++;
  5202. #endif
  5203. int funcOrd = *(uint16*)(data + (imageExportDir->AddressOfNameOrdinals - addrOffset) + funcIdx * sizeof(uint16));
  5204. addr_target funcAddr = *(uint32*)(data + (imageExportDir->AddressOfFunctions - addrOffset) + funcOrd * sizeof(int32));
  5205. int strLen = (int)strlen(name);
  5206. BP_ALLOC("ReadCOFF_SymbolMap", strLen + 1);
  5207. char* allocStr = (char*)mAlloc.AllocBytes(strLen + 1, "ReadCOFF_SymbolMap");
  5208. memcpy(allocStr, name, strLen);
  5209. BP_ALLOC_T(DbgSymbol);
  5210. DbgSymbol* dwSymbol = mAlloc.Alloc<DbgSymbol>();
  5211. dwSymbol->mDbgModule = this;
  5212. dwSymbol->mName = allocStr;
  5213. dwSymbol->mAddress = funcAddr;
  5214. if (strcmp(name, "_tls_index") == 0)
  5215. {
  5216. mTLSIndexAddr = funcAddr;
  5217. }
  5218. //TODO:
  5219. //mDeferredSymbols.PushFront(dwSymbol);
  5220. dwSymbol->mAddress = (addr_target)(dwSymbol->mAddress + mImageBase);
  5221. mDebugTarget->mSymbolMap.Insert(dwSymbol);
  5222. linkedModule->mSymbolNameMap.Insert(dwSymbol);
  5223. }
  5224. }
  5225. if ((IsObjectFile()) && (sectHdr.mNumberOfRelocations > 0))
  5226. {
  5227. //mDebugger->AllocTargetMemory(sectHdr.mSizeOfRawData, true, true);
  5228. }
  5229. if (strcmp(name, ".text") == 0)
  5230. {
  5231. if (!IsObjectFile())
  5232. mCodeAddress = ntHdr.mOptionalHeader.mImageBase + sectHdr.mVirtualAddress;
  5233. }
  5234. //if (strcmp(name, ".rdata") == 0)
  5235. {
  5236. PEDataDirectory& debugDirEntry = ntHdr.mOptionalHeader.mDataDirectory[IMAGE_DIRECTORY_ENTRY_DEBUG];
  5237. if (debugDirEntry.mSize > 0)
  5238. {
  5239. if (mMemReporter != NULL)
  5240. mMemReporter->Add("DataDirectory", debugDirEntry.mSize);
  5241. if ((debugDirEntry.mVirtualAddress >= sectHdr.mVirtualAddress) && (debugDirEntry.mVirtualAddress < sectHdr.mVirtualAddress + sectHdr.mSizeOfRawData))
  5242. {
  5243. int count = debugDirEntry.mSize / sizeof(IMAGE_DEBUG_DIRECTORY);
  5244. for (int dirIdx = 0; dirIdx < count; dirIdx++)
  5245. {
  5246. IMAGE_DEBUG_DIRECTORY* debugDirectory = (IMAGE_DEBUG_DIRECTORY*)(data + debugDirEntry.mVirtualAddress - sectHdr.mVirtualAddress) + dirIdx;
  5247. if (debugDirectory->Type == IMAGE_DEBUG_TYPE_CODEVIEW)
  5248. {
  5249. struct _CodeViewEntry
  5250. {
  5251. public:
  5252. int32 mSig;
  5253. uint8 mGUID[16];
  5254. int32 mAge;
  5255. const char mPDBPath[1];
  5256. };
  5257. if (debugDirectory->AddressOfRawData != 0)
  5258. {
  5259. _CodeViewEntry* codeViewEntry = (_CodeViewEntry*)(data + debugDirectory->AddressOfRawData - sectHdr.mVirtualAddress);
  5260. if (codeViewEntry->mSig == 'SDSR')
  5261. {
  5262. LoadPDB(codeViewEntry->mPDBPath, codeViewEntry->mGUID, codeViewEntry->mAge);
  5263. }
  5264. }
  5265. }
  5266. }
  5267. }
  5268. //stream->SetPos(debugDirEntry.mVirtualAddress);
  5269. }
  5270. }
  5271. //
  5272. {
  5273. PEDataDirectory& tlsDirEntry = ntHdr.mOptionalHeader.mDataDirectory[IMAGE_DIRECTORY_ENTRY_TLS];
  5274. if (tlsDirEntry.mSize > 0)
  5275. {
  5276. if ((tlsDirEntry.mVirtualAddress >= sectHdr.mVirtualAddress) && (tlsDirEntry.mVirtualAddress < sectHdr.mVirtualAddress + sectHdr.mSizeOfRawData))
  5277. {
  5278. uint8* relPtr = data + tlsDirEntry.mVirtualAddress - sectHdr.mVirtualAddress;
  5279. uint8* endPtr = relPtr + tlsDirEntry.mSize;
  5280. addr_target tlsDataStart = GET_FROM(relPtr, addr_target) - ntHdr.mOptionalHeader.mImageBase;
  5281. addr_target tlsDataEnd = GET_FROM(relPtr, addr_target) - ntHdr.mOptionalHeader.mImageBase;
  5282. mTLSAddr = (addr_target)(tlsDataStart + mImageBase);
  5283. mTLSSize = (int)(tlsDataEnd - tlsDataStart);
  5284. }
  5285. }
  5286. }
  5287. //
  5288. {
  5289. PEDataDirectory& debugDirEntry = ntHdr.mOptionalHeader.mDataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE];
  5290. if (debugDirEntry.mSize > 0)
  5291. {
  5292. if ((debugDirEntry.mVirtualAddress >= sectHdr.mVirtualAddress) && (debugDirEntry.mVirtualAddress < sectHdr.mVirtualAddress + sectHdr.mSizeOfRawData))
  5293. {
  5294. uint8* relPtr = data + debugDirEntry.mVirtualAddress - sectHdr.mVirtualAddress;
  5295. uint8* endPtr = relPtr + debugDirEntry.mSize;
  5296. IMAGE_RESOURCE_DIRECTORY* typeDir = (IMAGE_RESOURCE_DIRECTORY*)(relPtr);
  5297. // Skip named entries
  5298. for (int typeIdx = 0; typeIdx < typeDir->NumberOfIdEntries; typeIdx++)
  5299. {
  5300. IMAGE_RESOURCE_DIRECTORY_ENTRY* typeEntry = (IMAGE_RESOURCE_DIRECTORY_ENTRY*)((uint8*)typeDir + sizeof(IMAGE_RESOURCE_DIRECTORY) +
  5301. (typeDir->NumberOfNamedEntries + typeIdx)*sizeof(IMAGE_RESOURCE_DIRECTORY_ENTRY));
  5302. if (typeEntry->Id == 0x10) //VERSION
  5303. {
  5304. IMAGE_RESOURCE_DIRECTORY* idDir = (IMAGE_RESOURCE_DIRECTORY*)(relPtr + (typeEntry->OffsetToData & 0x7FFFFFFF));
  5305. if (idDir->NumberOfIdEntries < 1)
  5306. break;
  5307. IMAGE_RESOURCE_DIRECTORY_ENTRY* idEntry = (IMAGE_RESOURCE_DIRECTORY_ENTRY*)((uint8*)idDir + sizeof(IMAGE_RESOURCE_DIRECTORY) +
  5308. (idDir->NumberOfNamedEntries + 0) * sizeof(IMAGE_RESOURCE_DIRECTORY_ENTRY));
  5309. IMAGE_RESOURCE_DIRECTORY* langDir = (IMAGE_RESOURCE_DIRECTORY*)(relPtr + (idEntry->OffsetToData & 0x7FFFFFFF));
  5310. if (langDir->NumberOfIdEntries < 1)
  5311. break;
  5312. IMAGE_RESOURCE_DIRECTORY_ENTRY* langEntry = (IMAGE_RESOURCE_DIRECTORY_ENTRY*)((uint8*)langDir + sizeof(IMAGE_RESOURCE_DIRECTORY) +
  5313. (langDir->NumberOfNamedEntries + 0) * sizeof(IMAGE_RESOURCE_DIRECTORY_ENTRY));
  5314. IMAGE_RESOURCE_DATA_ENTRY* dataEntry = (IMAGE_RESOURCE_DATA_ENTRY*)(relPtr + (langEntry->OffsetToData & 0x7FFFFFFF));
  5315. uint8* versionData = data + dataEntry->OffsetToData - sectHdr.mVirtualAddress;
  5316. uint8* vPtr = versionData;
  5317. auto vSize = GET_FROM(vPtr, uint16);
  5318. auto verEnd = vPtr + vSize;
  5319. auto vLength = GET_FROM(vPtr, uint16);
  5320. vPtr += 36; // "VS_VERSION_INFO"
  5321. auto fixedFileInfo = GET_FROM(vPtr, VS_FIXEDFILEINFO);
  5322. auto _GetString = [&]()
  5323. {
  5324. wchar_t* cPtr = (wchar_t*)vPtr;
  5325. int len = (int)wcslen(cPtr);
  5326. vPtr += (len + 1) * 2;
  5327. if (((intptr)vPtr & 3) != 0)
  5328. vPtr += 2;
  5329. UTF16String str16(cPtr, len);
  5330. return UTF8Encode(str16);
  5331. };
  5332. while (vPtr < verEnd)
  5333. {
  5334. auto size = GET_FROM(vPtr, uint16);
  5335. auto childEnd = vPtr + size;
  5336. auto valueLength = GET_FROM(vPtr, uint16);
  5337. auto type = GET_FROM(vPtr, uint16);
  5338. String infoType = _GetString();
  5339. if (infoType == "StringFileInfo")
  5340. {
  5341. while (vPtr < childEnd)
  5342. {
  5343. auto strsSize = GET_FROM(vPtr, uint16);
  5344. auto strsEnd = vPtr + strsSize;
  5345. auto strsLength = GET_FROM(vPtr, uint16);
  5346. auto strsType = GET_FROM(vPtr, uint16);
  5347. String hexNum = _GetString();
  5348. while (vPtr < strsEnd)
  5349. {
  5350. auto strSize = GET_FROM(vPtr, uint16);
  5351. auto strEnd = vPtr + strSize;
  5352. auto strLength = GET_FROM(vPtr, uint16);
  5353. auto strType = GET_FROM(vPtr, uint16);
  5354. String key = _GetString();
  5355. String value = _GetString();
  5356. if (key == "FileVersion")
  5357. mVersion = value;
  5358. }
  5359. }
  5360. }
  5361. vPtr = childEnd;
  5362. }
  5363. }
  5364. }
  5365. }
  5366. //stream->SetPos(debugDirEntry.mVirtualAddress);
  5367. }
  5368. }
  5369. bool usedData = true;
  5370. /*if (isUnwindSection)
  5371. {
  5372. mExceptionData = data;
  5373. mExceptionDataRVA = sectHdr.mVirtualAddress;
  5374. }*/
  5375. if (strcmp(name, ".pdata") == 0)
  5376. {
  5377. DbgSectionData entry;
  5378. entry.mData = data;
  5379. entry.mSize = sectHdr.mSizeOfRawData;
  5380. mExceptionDirectory.Add(entry);
  5381. }
  5382. // Old, unsupported DWARF debug info
  5383. /*
  5384. else if (strcmp(name, ".debug_info") == 0)
  5385. {
  5386. mDebugInfoData = data;
  5387. }
  5388. else if (strcmp(name, ".debug_line") == 0)
  5389. {
  5390. mDebugLineData = data;
  5391. }
  5392. else if (strcmp(name, ".debug_str") == 0)
  5393. {
  5394. mDebugStrData = data;
  5395. }
  5396. else if (strcmp(name, ".debug_frame") == 0)
  5397. {
  5398. mDebugFrameAddress = ntHdr.mOptionalHeader.mImageBase + sectHdr.mVirtualAddress;
  5399. mDebugFrameData = data;
  5400. mDebugFrameDataLen = sectHdr.mSizeOfRawData;
  5401. }
  5402. else if (strcmp(name, ".eh_frame") == 0)
  5403. {
  5404. mEHFrameAddress = ntHdr.mOptionalHeader.mImageBase + sectHdr.mVirtualAddress;
  5405. mEHFrameData = data;
  5406. }
  5407. else if (strcmp(name, ".debug_abbrev") == 0)
  5408. {
  5409. mDebugAbbrevData = data;
  5410. mDebugAbbrevPtrData = new const uint8*[sectHdr.mSizeOfRawData];
  5411. }
  5412. else if (strcmp(name, ".debug_loc") == 0)
  5413. {
  5414. mDebugLocationData = data;
  5415. }
  5416. else if (strcmp(name, ".debug_ranges") == 0)
  5417. {
  5418. mDebugRangesData = data;
  5419. }
  5420. */
  5421. // else if (strcmp(name, ".rsrc") == 0)
  5422. // {
  5423. // //IMAGE_DIRECTORY_ENTRY_RESOURCE
  5424. // }
  5425. else if (CheckSection(name, data, sectHdr.mSizeOfRawData))
  5426. {
  5427. // Was used
  5428. }
  5429. else
  5430. {
  5431. /*if (isUnwindSection)
  5432. mOwnsExceptionData = true;
  5433. else*/
  5434. usedData = false;
  5435. }
  5436. if (!usedData)
  5437. {
  5438. if (IsObjectFile())
  5439. {
  5440. mOwnedSectionData.push_back(data);
  5441. }
  5442. else
  5443. {
  5444. mAllocSizeData -= dataSize;
  5445. delete [] data;
  5446. }
  5447. }
  5448. //stream->SetPos(prevPos);
  5449. }
  5450. int needHotTargetMemory = 0;
  5451. if (isObjectFile)
  5452. {
  5453. for (int sectNum = 0; sectNum < ntHdr.mFileHeader.mNumberOfSections; sectNum++)
  5454. {
  5455. auto targetSection = mHotTargetSections[sectNum];
  5456. if (!targetSection->mNoTargetAlloc)
  5457. needHotTargetMemory += (targetSection->mDataSize + (mDebugger->mPageSize - 1)) & ~(mDebugger->mPageSize - 1);
  5458. }
  5459. mDebugger->ReserveHotTargetMemory(needHotTargetMemory);
  5460. // '0' address is temporary
  5461. //mOrigImageData = new DbgModuleMemoryCache(0, NULL, needHotTargetMemory, true);
  5462. mOrigImageData = new DbgModuleMemoryCache(0, needHotTargetMemory);
  5463. }
  5464. int numSections = ntHdr.mFileHeader.mNumberOfSections;
  5465. if (isObjectFile)
  5466. {
  5467. addr_target* resolvedSymbolAddrs = new addr_target[ntHdr.mFileHeader.mNumberOfSymbols];
  5468. memset(resolvedSymbolAddrs, 0, ntHdr.mFileHeader.mNumberOfSymbols * sizeof(addr_target));
  5469. ParseHotTargetSections(stream, resolvedSymbolAddrs);
  5470. delete [] resolvedSymbolAddrs;
  5471. }
  5472. ProcessDebugInfo();
  5473. if (mDebugInfoData != NULL)
  5474. {
  5475. mDbgFlavor = DbgFlavor_GNU;
  5476. mMasterCompileUnit = new DbgCompileUnit(this);
  5477. mMasterCompileUnit->mDbgModule = this;
  5478. mMasterCompileUnit->mIsMaster = true;
  5479. const uint8* data = mDebugInfoData;
  5480. while (ParseDWARF(data)) {}
  5481. CreateNamespaces();
  5482. // Must be added last so module reference indices still map correctly
  5483. mCompileUnits.push_back(mMasterCompileUnit);
  5484. }
  5485. ParseDebugFrameData();
  5486. ParseEHFrameData();
  5487. mEndTypeIdx = (int)linkedModule->mTypes.size();
  5488. mEndSubprogramIdx = (int)mSubprograms.size();
  5489. if (mDebugLineData != NULL)
  5490. {
  5491. const uint8* data = mDebugLineData;
  5492. for (int compileUnitIdx = 0; true; compileUnitIdx++)
  5493. if (!ParseDebugLineInfo(data, compileUnitIdx))
  5494. break;
  5495. }
  5496. {
  5497. BP_ZONE("ReadPE_ReadSymbols");
  5498. //linkedModule->mSymbolNameMap.reserve(linkedModule->mSymbolNameMap.size() + ntHdr.mFileHeader.mNumberOfSymbols * 2);
  5499. bool tlsFailed = false;
  5500. addr_target tlsMappingAddr = 0;
  5501. for (int symNum = 0; symNum < (int)ntHdr.mFileHeader.mNumberOfSymbols; symNum++)
  5502. {
  5503. PE_SymInfo* symInfo = (PE_SymInfo*)&mSymbolData[symNum * 18];
  5504. char* name = symInfo->mName;
  5505. if (symInfo->mNameOfs[0] != 0)
  5506. {
  5507. if (name[7] != 0)
  5508. {
  5509. // Name is exactly 8 chars, not null terminated yet
  5510. name = (char*)mAlloc.AllocBytes(9, "PESymbol");
  5511. memcpy(name, symInfo->mName, 8);
  5512. name[8] = 0;
  5513. }
  5514. }
  5515. else
  5516. name = (char*)GetStringTable(stream, stringTablePos) + symInfo->mNameOfs[1];
  5517. if ((symInfo->mStorageClass == COFF_SYM_CLASS_EXTERNAL) ||
  5518. (symInfo->mStorageClass == COFF_SYM_CLASS_STATIC))
  5519. {
  5520. // 'static' in the C sense.
  5521. // It means local to the compile unit, so may have multiple copies of the same symbol name.
  5522. bool isStaticSymbol = symInfo->mStorageClass == COFF_SYM_CLASS_STATIC;
  5523. if (symInfo->mSectionNum == 0xFFFF)
  5524. continue;
  5525. if (symInfo->mSectionNum > 0)
  5526. {
  5527. bool isTLS = false;
  5528. addr_target targetAddr = 0;
  5529. if (isObjectFile)
  5530. {
  5531. if (symInfo->mSectionNum - 1 == tlsSection)
  5532. {
  5533. isTLS = true;
  5534. }
  5535. else
  5536. {
  5537. auto hotTargetSection = mHotTargetSections[symInfo->mSectionNum - 1];
  5538. if (hotTargetSection != NULL)
  5539. targetAddr = GetHotTargetAddress(hotTargetSection) + symInfo->mValue;
  5540. }
  5541. }
  5542. else
  5543. targetAddr = mSectionRVAs[symInfo->mSectionNum - 1] + symInfo->mValue;
  5544. if (((targetAddr != 0) || (isTLS)) &&
  5545. (name[0] != '.'))
  5546. {
  5547. const char* symbolName = name;
  5548. #ifdef BF_DBG_32
  5549. if (symbolName[0] == '_')
  5550. symbolName++;
  5551. #endif
  5552. if (strcmp(symbolName, "_tls_index") == 0)
  5553. {
  5554. mTLSIndexAddr = (addr_target)(targetAddr + mImageBase);
  5555. }
  5556. if ((isStaticSymbol) && (IsHotSwapPreserve(symbolName)))
  5557. isStaticSymbol = false;
  5558. if ((isObjectFile) && (!isStaticSymbol))
  5559. {
  5560. DbgSymbol* dwSymbol = NULL;
  5561. linkedModule->ParseSymbolData() ;
  5562. BP_ALLOC_T(DbgSymbol);
  5563. dwSymbol = mAlloc.Alloc<DbgSymbol>();
  5564. dwSymbol->mDbgModule = this;
  5565. dwSymbol->mName = symbolName;
  5566. dwSymbol->mAddress = targetAddr;
  5567. if (dwSymbol != NULL)
  5568. {
  5569. bool isHotSwapPreserve = IsHotSwapPreserve(dwSymbol->mName);
  5570. bool insertIntoNameMap = true;
  5571. bool oldFound = false;
  5572. auto nameMapEntry = linkedModule->mSymbolNameMap.Find(dwSymbol->mName);
  5573. if (nameMapEntry != NULL)
  5574. {
  5575. oldFound = true;
  5576. if (!isHotSwapPreserve)
  5577. {
  5578. nameMapEntry->mValue = dwSymbol;
  5579. }
  5580. else if (mDbgFlavor == DbgFlavor_MS)
  5581. {
  5582. // Store in our own map - this is needed for storing address of the new vdata
  5583. // so the new values can be copied in
  5584. mSymbolNameMap.Insert(dwSymbol);
  5585. }
  5586. }
  5587. else
  5588. {
  5589. if (isTLS)
  5590. {
  5591. if (mainModule->mTLSExtraAddr == 0)
  5592. {
  5593. auto extraSym = mainModule->mSymbolNameMap.Find("__BFTLS_EXTRA");
  5594. if (extraSym != NULL)
  5595. {
  5596. mainModule->ParseGlobalsData();
  5597. auto itr = mainModule->mStaticVariableMap.find("__BFTLS_EXTRA");
  5598. if (itr != mainModule->mStaticVariableMap.end())
  5599. {
  5600. auto staticVar = itr->second;
  5601. mainModule->mTLSExtraAddr = extraSym->mValue->mAddress;
  5602. mainModule->mTLSExtraSize = (int)staticVar->mType->GetByteCount();
  5603. }
  5604. }
  5605. }
  5606. if ((mainModule->mTLSExtraAddr != 0) && (tlsMappingAddr == 0))
  5607. {
  5608. // Take a chunk out of __BFTLS_EXTRA
  5609. if (mTLSSize <= mainModule->mTLSExtraSize)
  5610. {
  5611. tlsMappingAddr = mainModule->mTLSExtraAddr;
  5612. mainModule->mTLSExtraAddr += mTLSSize;
  5613. mainModule->mTLSExtraSize -= mTLSSize;
  5614. }
  5615. }
  5616. if (tlsMappingAddr != 0)
  5617. {
  5618. BF_ASSERT(symInfo->mValue < mTLSSize);
  5619. dwSymbol->mAddress = tlsMappingAddr + symInfo->mValue;
  5620. }
  5621. if (dwSymbol->mAddress == 0)
  5622. {
  5623. if (!tlsFailed)
  5624. {
  5625. Fail(StrFormat("Hot swapping failed to allocate TLS address for '%s'. Program restart required.", name));
  5626. }
  5627. dwSymbol->mAddress = (addr_target)0xCDCDCDCD;
  5628. tlsFailed = true;
  5629. }
  5630. }
  5631. }
  5632. if (dwSymbol->mAddress != 0)
  5633. {
  5634. if (!oldFound)
  5635. linkedModule->mSymbolNameMap.Insert(dwSymbol);
  5636. mDebugTarget->mSymbolMap.Insert(dwSymbol);
  5637. }
  5638. }
  5639. }
  5640. else
  5641. {
  5642. //TODO: We don't need to defer symbols anymore... we can just do a Fixup on their addr
  5643. //mDeferredSymbols.PushFront(dwSymbol);
  5644. BP_ALLOC_T(DbgSymbol);
  5645. DbgSymbol* dwSymbol = mAlloc.Alloc<DbgSymbol>();
  5646. dwSymbol->mDbgModule = this;
  5647. dwSymbol->mName = symbolName;
  5648. dwSymbol->mAddress = targetAddr;
  5649. if (!IsObjectFile())
  5650. dwSymbol->mAddress += (addr_target)mImageBase;
  5651. if (IsObjectFile())
  5652. BF_ASSERT((dwSymbol->mAddress >= mImageBase) && (dwSymbol->mAddress < mImageBase + mImageSize));
  5653. mDebugTarget->mSymbolMap.Insert(dwSymbol);
  5654. if (!isStaticSymbol)
  5655. linkedModule->mSymbolNameMap.Insert(dwSymbol);
  5656. }
  5657. }
  5658. }
  5659. }
  5660. if (symInfo->mStorageClass == COFF_SYM_CLASS_FILE)
  5661. {
  5662. const char* fileName = (const char*)&mSymbolData[(symNum + 1) * 18];
  5663. }
  5664. symNum += symInfo->mNumOfAuxSymbols;
  5665. }
  5666. }
  5667. int subProgramSizes = 0;
  5668. for (int subProgramIdx = mStartSubprogramIdx; subProgramIdx < mEndSubprogramIdx; subProgramIdx++)
  5669. {
  5670. auto dwSubprogram = mSubprograms[subProgramIdx];
  5671. subProgramSizes += (int)(dwSubprogram->mBlock.mHighPC - dwSubprogram->mBlock.mLowPC);
  5672. /*for (int i = 0; i < dwSubprogram->mLineDataArray.mSize; i++)
  5673. {
  5674. auto lineData = dwSubprogram->mLineDataArray.mData[i];
  5675. auto srcFile = lineData->mSrcFileRef->mSrcFile;
  5676. srcFile->mLineData.push_back(lineData);
  5677. srcFile->mHadLineData = true;
  5678. if ((srcFile->mFirstLineDataDbgModule == NULL) || (srcFile->mFirstLineDataDbgModule == this))
  5679. srcFile->mFirstLineDataDbgModule = this;
  5680. else
  5681. srcFile->mHasLineDataFromMultipleModules = true;
  5682. }*/
  5683. }
  5684. // Delete srcFiles without line data
  5685. int lineDataCount = 0;
  5686. /*for (int srcFileIdx = startSrcFile; srcFileIdx < (int)mDebugTarget->mSrcFiles.size(); srcFileIdx++)
  5687. {
  5688. if (!mDebugTarget->mSrcFiles[srcFileIdx]->mHadLineData)
  5689. {
  5690. mEmptySrcFiles.push_back(mDebugTarget->mSrcFiles[srcFileIdx]);
  5691. mDebugTarget->mSrcFiles.erase(mDebugTarget->mSrcFiles.begin() + srcFileIdx);
  5692. }
  5693. else
  5694. lineDataCount += (int)mDebugTarget->mSrcFiles[srcFileIdx]->mLineData.size();
  5695. }*/
  5696. auto srcFilesItr = mDebugTarget->mSrcFiles.begin();
  5697. while (srcFilesItr != mDebugTarget->mSrcFiles.end())
  5698. {
  5699. DbgSrcFile* srcFile = srcFilesItr->mValue;
  5700. if ((!srcFile->mHadLineData) && (srcFile->mLocalPath.IsEmpty()))
  5701. {
  5702. mEmptySrcFiles.push_back(srcFile);
  5703. srcFilesItr = mDebugTarget->mSrcFiles.Remove(srcFilesItr);
  5704. }
  5705. else
  5706. {
  5707. ++srcFilesItr;
  5708. }
  5709. }
  5710. if (!isObjectFile)
  5711. {
  5712. mImageSize = ntHdr.mOptionalHeader.mSizeOfImage;
  5713. mEntryPoint = ntHdr.mOptionalHeader.mAddressOfEntryPoint;
  5714. }
  5715. /*OutputDebugStrF("%s:\n CompileUnits:%d DebugLines: %d Types: %d (%d in map) SubPrograms: %d (%dk) AllocSize:%dk\n", mFilePath.c_str(), mCompileUnits.size(),
  5716. lineDataCount, mEndTypeIdx - mStartTypeIdx, (int)linkedModule->mTypes.size() - mStartTypeIdx, mEndSubprogramIdx - mStartSubprogramIdx, subProgramSizes / 1024, mAlloc.GetAllocSize() / 1024);*/
  5717. if (isHotSwap)
  5718. {
  5719. // In COFF, we don't necessarily add an actual primary type during MapCompileUnitMethods, so this fixes that
  5720. while (true)
  5721. {
  5722. bool didReplaceType = false;
  5723. for (auto itr = mHotPrimaryTypes.begin(); itr != mHotPrimaryTypes.end(); ++itr)
  5724. {
  5725. auto dbgType = *itr;
  5726. auto primaryType = dbgType->GetPrimaryType();
  5727. if (primaryType != dbgType)
  5728. {
  5729. mHotPrimaryTypes.Remove(itr);
  5730. mHotPrimaryTypes.Add(primaryType);
  5731. didReplaceType = true;
  5732. break;
  5733. }
  5734. }
  5735. if (!didReplaceType)
  5736. break;
  5737. }
  5738. BF_ASSERT(mTypes.size() == 0);
  5739. for (int typeIdx = mStartTypeIdx; typeIdx < (int)linkedModule->mTypes.size(); typeIdx++)
  5740. {
  5741. DbgType* newType = linkedModule->mTypes[typeIdx];
  5742. //if (!newType->mMethodList.IsEmpty())
  5743. if (!newType->mIsDeclaration)
  5744. HotReplaceType(newType);
  5745. }
  5746. }
  5747. if (needHotTargetMemory != 0)
  5748. {
  5749. BF_ASSERT(needHotTargetMemory >= (int)mImageSize);
  5750. }
  5751. //BF_ASSERT(mEndTypeIdx == (int)linkedModule->mTypes.size());
  5752. //BF_ASSERT(mEndSubprogramIdx == (int)mSubprograms.size());
  5753. ParseExceptionData();
  5754. mLoadState = DbgModuleLoadState_Loaded;
  5755. if (mMemReporter != NULL)
  5756. {
  5757. mMemReporter->BeginSection("Sections");
  5758. ParseSymbolData();
  5759. Array<DbgSymbol*> orderedSyms;
  5760. for (auto sym : mSymbolNameMap)
  5761. {
  5762. auto dbgSym = sym->mValue;
  5763. orderedSyms.Add(dbgSym);
  5764. }
  5765. orderedSyms.Sort([](DbgSymbol* lhs, DbgSymbol* rhs) { return lhs->mAddress < rhs->mAddress; });
  5766. for (int sectNum = 0; sectNum < ntHdr.mFileHeader.mNumberOfSections; sectNum++)
  5767. {
  5768. PESectionHeader& sectHdr = sectionHeaders[sectNum];
  5769. mMemReporter->BeginSection(sectionNames[sectNum]);
  5770. DbgSymbol* lastSym = NULL;
  5771. for (auto dbgSym : orderedSyms)
  5772. {
  5773. if (dbgSym->mAddress < mImageBase + sectHdr.mVirtualAddress)
  5774. continue;
  5775. if (dbgSym->mAddress >= mImageBase + sectHdr.mVirtualAddress + sectHdr.mSizeOfRawData)
  5776. break;
  5777. if (lastSym != NULL)
  5778. {
  5779. mMemReporter->Add(lastSym->mName, (int)(dbgSym->mAddress - lastSym->mAddress));
  5780. }
  5781. else
  5782. {
  5783. int startingOffset = (int)(dbgSym->mAddress - (mImageBase + sectHdr.mVirtualAddress + sectHdr.mSizeOfRawData));
  5784. if (startingOffset > 0)
  5785. mMemReporter->Add("<StartData>", startingOffset);
  5786. }
  5787. lastSym = dbgSym;
  5788. }
  5789. if (lastSym != NULL)
  5790. mMemReporter->Add(lastSym->mName, (int)((mImageBase + sectHdr.mVirtualAddress + sectHdr.mSizeOfRawData) - lastSym->mAddress));
  5791. else
  5792. {
  5793. mMemReporter->Add("<Unaccounted>", (int)(sectHdr.mSizeOfRawData));
  5794. }
  5795. mMemReporter->EndSection();
  5796. }
  5797. mMemReporter->EndSection();
  5798. mMemReporter->mShowInKB = false;
  5799. mMemReporter->Report();
  5800. }
  5801. return true;
  5802. }
  5803. void DbgModule::FinishHotSwap()
  5804. {
  5805. BF_ASSERT(IsObjectFile());
  5806. auto linkedModule = GetLinkedModule();
  5807. auto mainModule = mDebugTarget->mTargetBinary;
  5808. HashSet<String> failSet;
  5809. String findName;
  5810. for (auto deferredHotResolve : mDeferredHotResolveList)
  5811. {
  5812. addr_target resolveTargetAddr = deferredHotResolve->mNewAddr;
  5813. findName = deferredHotResolve->mName;
  5814. if (mDbgFlavor == DbgFlavor_MS)
  5815. {
  5816. // ... why do we need to find these variables in the variable map instead of the symbol name map?
  5817. }
  5818. auto itr = mainModule->mStaticVariableMap.find(findName.c_str());
  5819. if (itr != mainModule->mStaticVariableMap.end())
  5820. {
  5821. DbgVariable* variable = itr->second;
  5822. resolveTargetAddr = mDebugTarget->GetStaticAddress(variable);
  5823. }
  5824. else
  5825. {
  5826. auto symbolEntry = mainModule->mSymbolNameMap.Find(findName.c_str());
  5827. if (symbolEntry != NULL)
  5828. {
  5829. resolveTargetAddr = symbolEntry->mValue->mAddress;
  5830. }
  5831. else
  5832. {
  5833. if (deferredHotResolve->mName == "__ImageBase")
  5834. {
  5835. resolveTargetAddr = (addr_target)mainModule->mImageBase;
  5836. }
  5837. else
  5838. {
  5839. resolveTargetAddr = mainModule->LocateSymbol(deferredHotResolve->mName);
  5840. if (resolveTargetAddr == 0)
  5841. {
  5842. failSet.Add(deferredHotResolve->mName);
  5843. continue;
  5844. }
  5845. }
  5846. }
  5847. }
  5848. DoReloc(deferredHotResolve->mHotTargetSection, deferredHotResolve->mReloc, resolveTargetAddr, NULL);
  5849. }
  5850. mDeferredHotResolveList.Clear();
  5851. if (!failSet.IsEmpty())
  5852. {
  5853. bool handled = false;
  5854. if (!mDebugger->mDebugManager->mOutMessages.empty())
  5855. {
  5856. auto& str = mDebugger->mDebugManager->mOutMessages.back();
  5857. if (str.Contains("failed to resolve"))
  5858. {
  5859. for (auto& sym : failSet)
  5860. {
  5861. str += ", ";
  5862. str += sym;
  5863. }
  5864. handled = true;
  5865. }
  5866. }
  5867. if (!handled)
  5868. {
  5869. int symIdx = 0;
  5870. String str;
  5871. if (failSet.size() == 1)
  5872. str = "Hot swapping failed to resolve symbol: ";
  5873. else
  5874. str = "Hot swapping failed to resolve symbols: ";
  5875. for (auto& sym : failSet)
  5876. {
  5877. if (symIdx != 0)
  5878. str += ", ";
  5879. str += sym;
  5880. symIdx++;
  5881. }
  5882. mDebugger->Fail(str);
  5883. }
  5884. }
  5885. CommitHotTargetSections();
  5886. // We need this here because vdata gets loaded first, so we need to wait until we have the addrs for the new methods (from other modules)
  5887. // before we can finalize the class vdata.
  5888. ProcessHotSwapVariables();
  5889. for (auto hotTargetSection : mHotTargetSections)
  5890. delete hotTargetSection;
  5891. mHotTargetSections.Clear();
  5892. mSymbolNameMap.Clear();
  5893. }
  5894. addr_target DbgModule::ExecuteOps(DbgSubprogram* dwSubprogram, const uint8* locData, int locDataLen, WdStackFrame* stackFrame, CPURegisters* registers, DbgAddrType* outAddrType, DbgEvalLocFlags flags, addr_target* pushValue)
  5895. {
  5896. bool allowReg = (flags & DbgEvalLocFlag_IsParam) == 0;
  5897. const uint8* locDataEnd = locData + locDataLen;
  5898. int regNum = -1;
  5899. addr_target stackFrameData[256];
  5900. int stackIdx = 0;
  5901. if (pushValue != NULL)
  5902. stackFrameData[stackIdx++] = *pushValue;
  5903. while (locData < locDataEnd)
  5904. {
  5905. uint8 opCode = GET_FROM(locData, uint8);
  5906. switch (opCode)
  5907. {
  5908. case DW_OP_piece:
  5909. {
  5910. if (*outAddrType == DbgAddrType_Register)
  5911. *outAddrType = DbgAddrType_Value;
  5912. addr_target val = stackFrameData[--stackIdx];
  5913. int pieceSize = (int)DecodeULEB128(locData);
  5914. if (pieceSize == 4)
  5915. val &= 0xFFFFFFFF;
  5916. else if (pieceSize == 2)
  5917. val &= 0xFFFF;
  5918. else if (pieceSize == 1)
  5919. val &= 0xFF;
  5920. stackFrameData[stackIdx++] = val;
  5921. }
  5922. break;
  5923. case DW_OP_consts:
  5924. {
  5925. int64 val = DecodeSLEB128(locData);
  5926. stackFrameData[stackIdx++] = (addr_target)val;
  5927. }
  5928. break;
  5929. case DW_OP_stack_value:
  5930. {
  5931. *outAddrType = DbgAddrType_Value;
  5932. }
  5933. break;
  5934. case DW_OP_addr_noRemap:
  5935. {
  5936. addr_target addr = GET_FROM(locData, addr_target);
  5937. stackFrameData[stackIdx++] = addr;
  5938. //*outIsAddr = true;
  5939. *outAddrType = DbgAddrType_Target;
  5940. }
  5941. break;
  5942. case DW_OP_addr:
  5943. {
  5944. addr_target addr = GET_FROM(locData, addr_target);
  5945. //if (dwarf != NULL)
  5946. addr = RemapAddr(addr);
  5947. stackFrameData[stackIdx++] = addr;
  5948. //*outIsAddr = true;
  5949. *outAddrType = DbgAddrType_Target;
  5950. }
  5951. break;
  5952. case DW_OP_deref:
  5953. {
  5954. addr_target addr = stackFrameData[--stackIdx];
  5955. addr_target value = mDebugger->ReadMemory<addr_target>(addr);
  5956. stackFrameData[stackIdx++] = value;
  5957. }
  5958. break;
  5959. case DW_OP_fbreg:
  5960. {
  5961. if (registers == NULL)
  5962. return 0;
  5963. BF_ASSERT(dwSubprogram != NULL);
  5964. DbgSubprogram* nonInlinedSubProgram = dwSubprogram->GetRootInlineParent();
  5965. if (nonInlinedSubProgram->mFrameBaseData == NULL)
  5966. {
  5967. *outAddrType = DbgAddrType_Target; //TODO: why?
  5968. return 0;
  5969. }
  5970. BF_ASSERT(nonInlinedSubProgram->mFrameBaseData != NULL);
  5971. intptr loc = EvaluateLocation(nonInlinedSubProgram, nonInlinedSubProgram->mFrameBaseData, nonInlinedSubProgram->mFrameBaseLen, stackFrame, outAddrType, DbgEvalLocFlag_DisallowReg);
  5972. int64 offset = DecodeSLEB128(locData);
  5973. loc += offset;
  5974. //loc = BfDebuggerReadMemory(loc);
  5975. //*outIsAddr = true;
  5976. *outAddrType = DbgAddrType_Target;
  5977. stackFrameData[stackIdx++] = (addr_target)loc;
  5978. }
  5979. break;
  5980. case DW_OP_reg0:
  5981. case DW_OP_reg1:
  5982. case DW_OP_reg2:
  5983. case DW_OP_reg3:
  5984. case DW_OP_reg4:
  5985. case DW_OP_reg5:
  5986. case DW_OP_reg6:
  5987. case DW_OP_reg7:
  5988. case DW_OP_reg8:
  5989. case DW_OP_reg9:
  5990. case DW_OP_reg10:
  5991. case DW_OP_reg11:
  5992. case DW_OP_reg12:
  5993. case DW_OP_reg13:
  5994. case DW_OP_reg14:
  5995. case DW_OP_reg15:
  5996. if (registers == NULL)
  5997. return 0;
  5998. BF_ASSERT((opCode - DW_OP_reg0) < CPURegisters::kNumIntRegs);
  5999. regNum = opCode - DW_OP_reg0;
  6000. stackFrameData[stackIdx++] = registers->mIntRegsArray[regNum];
  6001. *outAddrType = DbgAddrType_Register;
  6002. break;
  6003. case DW_OP_reg21: //XMM0
  6004. BF_FATAL("XMM registers not supported yet");
  6005. break;
  6006. case DW_OP_breg0:
  6007. case DW_OP_breg1:
  6008. case DW_OP_breg2:
  6009. case DW_OP_breg3:
  6010. case DW_OP_breg4:
  6011. case DW_OP_breg5:
  6012. case DW_OP_breg6:
  6013. case DW_OP_breg7:
  6014. case DW_OP_breg8:
  6015. case DW_OP_breg9:
  6016. case DW_OP_breg10:
  6017. case DW_OP_breg11:
  6018. case DW_OP_breg12:
  6019. case DW_OP_breg13:
  6020. case DW_OP_breg14:
  6021. case DW_OP_breg15:
  6022. {
  6023. if (registers == NULL)
  6024. return 0;
  6025. int64 offset = DecodeSLEB128(locData);
  6026. BF_ASSERT((opCode - DW_OP_breg0) < CPURegisters::kNumIntRegs);
  6027. auto loc = registers->mIntRegsArray[opCode - DW_OP_breg0] + offset;
  6028. //loc = BfDebuggerReadMemory(loc);
  6029. //*outIsAddr = true;
  6030. *outAddrType = DbgAddrType_Target;
  6031. stackFrameData[stackIdx++] = (addr_target)loc;
  6032. }
  6033. break;
  6034. case DW_OP_bregx:
  6035. {
  6036. if (registers == NULL)
  6037. return 0;
  6038. int regNum = (int)DecodeULEB128(locData);
  6039. int64 offset = DecodeSLEB128(locData);
  6040. BF_ASSERT(regNum < CPURegisters::kNumIntRegs);
  6041. auto loc = registers->mIntRegsArray[regNum] + offset;
  6042. //loc = BfDebuggerReadMemory(loc);
  6043. //*outIsAddr = true;
  6044. *outAddrType = DbgAddrType_Target;
  6045. stackFrameData[stackIdx++] = (addr_target)loc;
  6046. }
  6047. break;
  6048. case DW_OP_const4u:
  6049. {
  6050. uint32 val = GET_FROM(locData, uint32);
  6051. stackFrameData[stackIdx++] = val;
  6052. }
  6053. break;
  6054. case DW_OP_const8u:
  6055. {
  6056. uint64 val = GET_FROM(locData, uint64);
  6057. stackFrameData[stackIdx++] = (addr_target)val;
  6058. }
  6059. break;
  6060. case DW_OP_GNU_push_tls_address:
  6061. {
  6062. if ((mTLSAddr == 0) || (mTLSIndexAddr == 0))
  6063. return 0;
  6064. int tlsIndex = mDebugger->ReadMemory<int>(mTLSIndexAddr);
  6065. addr_target tlsEntry = mDebugger->GetTLSOffset(tlsIndex);
  6066. intptr_target tlsValueIndex = stackFrameData[--stackIdx];
  6067. stackFrameData[stackIdx++] = (tlsValueIndex - mTLSAddr) + tlsEntry;
  6068. *outAddrType = DbgAddrType_Target;
  6069. }
  6070. break;
  6071. case DW_OP_nop:
  6072. break;
  6073. default:
  6074. BF_FATAL("Unknown DW_OP");
  6075. break;
  6076. }
  6077. }
  6078. if (*outAddrType == DbgAddrType_Register)
  6079. {
  6080. if (allowReg)
  6081. return regNum;
  6082. *outAddrType = DbgAddrType_Value;
  6083. }
  6084. //BF_ASSERT(stackIdx == 1);
  6085. return stackFrameData[--stackIdx];
  6086. }
  6087. intptr DbgModule::EvaluateLocation(DbgSubprogram* dwSubprogram, const uint8* locData, int locDataLen, WdStackFrame* stackFrame, DbgAddrType* outAddrType, DbgEvalLocFlags flags)
  6088. {
  6089. BP_ZONE("DebugTarget::EvaluateLocation");
  6090. auto dbgModule = this;
  6091. if (locDataLen == DbgLocationLenKind_SegPlusOffset)
  6092. {
  6093. BF_ASSERT(dbgModule->mDbgFlavor == DbgFlavor_MS);
  6094. if (dbgModule->mDbgFlavor == DbgFlavor_MS)
  6095. {
  6096. COFF* coff = (COFF*)dbgModule;
  6097. struct SegOfsData
  6098. {
  6099. uint32 mOfs;
  6100. uint16 mSeg;
  6101. };
  6102. SegOfsData* segOfsData = (SegOfsData*)locData;
  6103. *outAddrType = DbgAddrType_Target;
  6104. return coff->GetSectionAddr(segOfsData->mSeg, segOfsData->mOfs);
  6105. }
  6106. else
  6107. {
  6108. *outAddrType = DbgAddrType_Target;
  6109. return 0;
  6110. }
  6111. }
  6112. CPURegisters* registers = NULL;
  6113. if (stackFrame != NULL)
  6114. registers = &stackFrame->mRegisters;
  6115. if (locDataLen < 0)
  6116. {
  6117. if (registers == NULL)
  6118. return 0;
  6119. int64 ipAddr = stackFrame->GetSourcePC();
  6120. const uint8* checkLocData = locData;
  6121. int64 startLoc = (int64)GET_FROM(checkLocData, addr_target);
  6122. int64 endLoc = startLoc + GET_FROM(checkLocData, uint16);
  6123. BF_ASSERT(dwSubprogram != NULL);
  6124. startLoc += dwSubprogram->mCompileUnit->mLowPC;
  6125. endLoc += dwSubprogram->mCompileUnit->mLowPC;
  6126. if ((ipAddr >= startLoc) && (ipAddr < endLoc))
  6127. {
  6128. locDataLen = -locDataLen - sizeof(addr_target) - sizeof(uint16);
  6129. locData = checkLocData;
  6130. }
  6131. else
  6132. {
  6133. *outAddrType = DbgAddrType_OptimizedOut;
  6134. return 0;
  6135. }
  6136. }
  6137. else if (locDataLen == 0)
  6138. {
  6139. if (registers == NULL)
  6140. return 0;
  6141. int64 ipAddr = stackFrame->GetSourcePC();
  6142. const uint8* checkLocData = locData;
  6143. while (true)
  6144. {
  6145. int64 startLoc = (int64)GET_FROM(checkLocData, addr_target);
  6146. int64 endLoc = (int64)GET_FROM(checkLocData, addr_target);
  6147. if ((startLoc == 0) && (endLoc == 0))
  6148. {
  6149. *outAddrType = DbgAddrType_OptimizedOut;
  6150. return 0;
  6151. }
  6152. BF_ASSERT(dwSubprogram != NULL);
  6153. startLoc += dwSubprogram->mCompileUnit->mLowPC;
  6154. endLoc += dwSubprogram->mCompileUnit->mLowPC;
  6155. if ((ipAddr >= startLoc) && (ipAddr < endLoc))
  6156. {
  6157. locDataLen = GET_FROM(checkLocData, int16);
  6158. locData = checkLocData;
  6159. break;
  6160. }
  6161. else
  6162. {
  6163. int len = GET_FROM(checkLocData, int16);;
  6164. checkLocData += len;
  6165. }
  6166. }
  6167. }
  6168. return ExecuteOps(dwSubprogram, locData, locDataLen, stackFrame, registers, outAddrType, flags);
  6169. }
  6170. void DbgModule::ProcessHotSwapVariables()
  6171. {
  6172. BP_ZONE("DbgModule::ProcessHotSwapVariables");
  6173. auto linkedModule = GetLinkedModule();
  6174. for (auto staticVariable : mStaticVariables)
  6175. {
  6176. bool replaceVariable = false;
  6177. const char* findName = staticVariable->GetMappedName();
  6178. auto itr = linkedModule->mStaticVariableMap.find(findName);
  6179. if (itr != linkedModule->mStaticVariableMap.end())
  6180. {
  6181. DbgVariable* oldVariable = itr->second;
  6182. // If the old static field has the same type as the new static field then we keep the same
  6183. // address, otherwise we use the new (zeroed-out) allocated space
  6184. auto _GetNewAddress = [&]()
  6185. {
  6186. addr_target newAddress = 0;
  6187. if (mDbgFlavor == DbgFlavor_GNU)
  6188. {
  6189. newAddress = mDebugTarget->GetStaticAddress(staticVariable);
  6190. }
  6191. else
  6192. {
  6193. // In CodeView, the newVariable ends up pointing to the old address, so we need to store
  6194. // the location in our own mSymbolNameMap
  6195. auto entry = mSymbolNameMap.Find(oldVariable->mLinkName);
  6196. if (entry != NULL)
  6197. newAddress = entry->mValue->mAddress;
  6198. }
  6199. return newAddress;
  6200. };
  6201. if (oldVariable->mType->IsSizedArray())
  6202. {
  6203. mDebugTarget->GetCompilerSettings();
  6204. bool doMerge = strstr(oldVariable->mName, "sBfClassVData") != NULL;
  6205. bool keepInPlace = (doMerge) && (strstr(oldVariable->mName, ".vext") == NULL);
  6206. if (doMerge)
  6207. {
  6208. addr_target oldAddress = mDebugTarget->GetStaticAddress(oldVariable);
  6209. addr_target newAddress = _GetNewAddress();
  6210. if (newAddress == 0)
  6211. continue;
  6212. uint8* newData = GetHotTargetData(newAddress);
  6213. int newArraySize = (int)staticVariable->mType->GetByteCount();
  6214. int oldArraySize = (int)oldVariable->mType->GetByteCount();
  6215. int copySize = std::min(newArraySize, oldArraySize);
  6216. BF_ASSERT((oldArraySize & (sizeof(addr_target) - 1)) == 0);
  6217. DbgModule* defModule = oldVariable->mType->mCompileUnit->mDbgModule;
  6218. defModule->EnableWriting(oldAddress);
  6219. uint8* mergedData = new uint8[copySize];
  6220. mDebugger->ReadMemory(oldAddress, copySize, mergedData);
  6221. // The new vtable may have 0's in it when virtual methods are removed. Keep the old virtual addresses in those.
  6222. addr_target* newDataPtr = (addr_target*)newData;
  6223. addr_target* mergedPtr = (addr_target*)mergedData;
  6224. while (mergedPtr < (addr_target*)(mergedData + copySize))
  6225. {
  6226. if (*newDataPtr != 0)
  6227. *mergedPtr = *newDataPtr;
  6228. mergedPtr++;
  6229. newDataPtr++;
  6230. }
  6231. bool success;
  6232. success = mDebugger->WriteMemory(oldAddress, mergedData, copySize);
  6233. BF_ASSERT(success);
  6234. memcpy(newData, mergedData, copySize);
  6235. delete mergedData;
  6236. }
  6237. else if (strstr(oldVariable->mName, "sStringLiterals") != NULL)
  6238. {
  6239. addr_target oldAddress = mDebugTarget->GetStaticAddress(oldVariable);
  6240. addr_target newAddress = NULL;
  6241. if (mDbgFlavor == DbgFlavor_GNU)
  6242. {
  6243. newAddress = mDebugTarget->GetStaticAddress(staticVariable);
  6244. }
  6245. else
  6246. {
  6247. // In CodeView, the newVariable ends up pointing to the old address, so we need to store
  6248. // the location in our own mSymbolNameMap
  6249. auto entry = mSymbolNameMap.Find(oldVariable->mLinkName);
  6250. if (entry == NULL)
  6251. continue;
  6252. newAddress = entry->mValue->mAddress;
  6253. }
  6254. // Make sure newAddress doesn't have anything linked to it
  6255. addr_target val = 0;
  6256. bool success = mDebugger->ReadMemory((intptr)newAddress, sizeof(addr_target), &val);
  6257. BF_ASSERT(success);
  6258. BF_ASSERT(val == 0);
  6259. // Link the new table to the old extended table
  6260. addr_target prevLinkage = 0;
  6261. success = mDebugger->ReadMemory((intptr)oldAddress, sizeof(addr_target), &prevLinkage);
  6262. BF_ASSERT(success);
  6263. success = mDebugger->WriteMemory((intptr)newAddress, &prevLinkage, sizeof(addr_target));
  6264. BF_ASSERT(success);
  6265. mDebugger->EnableWriting((intptr)oldAddress, sizeof(addr_target));
  6266. success = mDebugger->WriteMemory((intptr)oldAddress, &newAddress, sizeof(addr_target));
  6267. BF_ASSERT(success);
  6268. keepInPlace = true;
  6269. }
  6270. if (keepInPlace)
  6271. {
  6272. // We have to maintain the OLD size because we can't overwrite the original bounds
  6273. staticVariable->mType = oldVariable->mType;
  6274. staticVariable->mLocationLen = oldVariable->mLocationLen;
  6275. staticVariable->mLocationData = oldVariable->mLocationData;
  6276. staticVariable->mCompileUnit = oldVariable->mCompileUnit;
  6277. }
  6278. }
  6279. else if (oldVariable->mType->Equals(staticVariable->mType))
  6280. {
  6281. if (oldVariable->mType->IsStruct())
  6282. {
  6283. if ((strncmp(oldVariable->mName, "?sBfTypeData@", 13) == 0) || (strncmp(oldVariable->mName, "sBfTypeData.", 12) == 0))
  6284. {
  6285. int size = (int)staticVariable->mType->GetByteCount();
  6286. addr_target oldAddress = mDebugTarget->GetStaticAddress(oldVariable);
  6287. addr_target newAddress = _GetNewAddress();
  6288. if (newAddress == 0)
  6289. continue;
  6290. uint8* data = new uint8[size];
  6291. bool success = mDebugger->ReadMemory(newAddress, size, data);
  6292. if (success)
  6293. {
  6294. mDebugger->EnableWriting((intptr)oldAddress, size);
  6295. success = mDebugger->WriteMemory(oldAddress, data, size);
  6296. }
  6297. delete data;
  6298. BF_ASSERT(success);
  6299. staticVariable->mLocationLen = oldVariable->mLocationLen;
  6300. staticVariable->mLocationData = oldVariable->mLocationData;
  6301. }
  6302. }
  6303. //staticVariable->mLocationLen = oldVariable->mLocationLen;
  6304. //staticVariable->mLocationData = oldVariable->mLocationData;
  6305. replaceVariable = false;
  6306. }
  6307. else
  6308. {
  6309. BF_ASSERT(!oldVariable->mType->IsSizedArray());
  6310. }
  6311. if (!replaceVariable)
  6312. {
  6313. auto symbolVal = linkedModule->mSymbolNameMap.Find(staticVariable->GetMappedName());
  6314. if (symbolVal != NULL)
  6315. {
  6316. addr_target oldAddress = mDebugTarget->GetStaticAddress(oldVariable);
  6317. DbgSymbol* oldSymbol = mDebugTarget->mSymbolMap.Get(oldAddress);
  6318. if (oldSymbol != NULL)
  6319. symbolVal->mValue = oldSymbol;
  6320. }
  6321. }
  6322. }
  6323. else // Not found - new variable
  6324. replaceVariable = true;
  6325. if (replaceVariable)
  6326. {
  6327. linkedModule->mStaticVariableMap[staticVariable->GetMappedName()] = staticVariable;
  6328. }
  6329. }
  6330. }
  6331. int64 DbgModule::GetImageSize()
  6332. {
  6333. return mImageSize;
  6334. }
  6335. /*const uint8* DbgModule::GetOrigImageData(addr_target address)
  6336. {
  6337. return mOrigImageData + (address - mImageBase);
  6338. }*/
  6339. DbgFileExistKind DbgModule::CheckSourceFileExist(const StringImpl& path)
  6340. {
  6341. DbgFileExistKind existsKind = DbgFileExistKind_NotFound;
  6342. if (path.StartsWith("$Emit"))
  6343. return DbgFileExistKind_Found;
  6344. if (FileExists(path))
  6345. existsKind = DbgFileExistKind_Found;
  6346. String oldSourceCommand = GetOldSourceCommand(path);
  6347. if (!oldSourceCommand.IsEmpty())
  6348. {
  6349. int crPos = (int)oldSourceCommand.IndexOf('\n');
  6350. if (crPos != -1)
  6351. {
  6352. String targetPath = oldSourceCommand.Substring(0, crPos);
  6353. if (FileExists(targetPath))
  6354. existsKind = DbgFileExistKind_Found;
  6355. else
  6356. existsKind = DbgFileExistKind_HasOldSourceCommand;
  6357. }
  6358. }
  6359. return existsKind;
  6360. }
  6361. void DbgModule::EnableWriting(addr_target address)
  6362. {
  6363. for (int sectionIdx = 0; sectionIdx < (int)mSections.size(); sectionIdx++)
  6364. {
  6365. DbgSection* section = &mSections[sectionIdx];
  6366. if ((address >= mImageBase + section->mAddrStart) && (address < mImageBase + section->mAddrStart + section->mAddrLength))
  6367. {
  6368. if (!section->mWritingEnabled)
  6369. {
  6370. section->mOldProt = mDebugger->EnableWriting(mImageBase + section->mAddrStart, (int32)section->mAddrLength);
  6371. section->mWritingEnabled = true;
  6372. }
  6373. }
  6374. }
  6375. }
  6376. void DbgModule::RevertWritingEnable()
  6377. {
  6378. for (int sectionIdx = 0; sectionIdx < (int)mSections.size(); sectionIdx++)
  6379. {
  6380. DbgSection* section = &mSections[sectionIdx];
  6381. if (section->mWritingEnabled)
  6382. {
  6383. mDebugger->SetProtection(mImageBase + section->mAddrStart, (int32)section->mAddrLength, section->mOldProt);
  6384. section->mWritingEnabled = false;
  6385. }
  6386. }
  6387. }
  6388. template <typename TRadixMap>
  6389. static void RemoveInvalidRange(TRadixMap& radixMap, addr_target startAddr, int addrLength)
  6390. {
  6391. radixMap.RemoveRange(startAddr, addrLength);
  6392. }
  6393. template <typename TMap>
  6394. static void RemoveInvalidMapRange(TMap& map, addr_target startAddr, int addrLength)
  6395. {
  6396. auto itr = map.lower_bound(startAddr);
  6397. while (itr != map.end())
  6398. {
  6399. auto val = itr->first;
  6400. if (val >= startAddr + addrLength)
  6401. return;
  6402. itr = map.erase(itr);
  6403. }
  6404. }
  6405. void DbgModule::RemoveTargetData()
  6406. {
  6407. BP_ZONE("DbgModule::RemoveTargetData");
  6408. for (auto srcFileRef : mSrcFileDeferredRefs)
  6409. srcFileRef->RemoveDeferredRefs(this);
  6410. HashSet<DbgSrcFile*> visitedFiles;
  6411. for (auto compileUnit : mCompileUnits)
  6412. {
  6413. for (auto& fileRef : compileUnit->mSrcFileRefs)
  6414. {
  6415. if (visitedFiles.Add(fileRef.mSrcFile))
  6416. {
  6417. fileRef.mSrcFile->RemoveLines(this);
  6418. }
  6419. }
  6420. }
  6421. RemoveInvalidRange(mDebugTarget->mSymbolMap, (addr_target)mImageBase, (int32)mImageSize);
  6422. RemoveInvalidRange(mDebugTarget->mSubprogramMap, (addr_target)mImageBase, (int32)mImageSize);
  6423. RemoveInvalidRange(mDebugTarget->mExceptionDirectoryMap, (addr_target)mImageBase, (int32)mImageSize);
  6424. RemoveInvalidRange(mDebugTarget->mContribMap, (addr_target)mImageBase, (int32)mImageSize);
  6425. RemoveInvalidMapRange(mDebugTarget->mDwFrameDescriptorMap, (addr_target)mImageBase, (int32)mImageSize);
  6426. RemoveInvalidMapRange(mDebugTarget->mCOFFFrameDescriptorMap, (addr_target)mImageBase, (int32)mImageSize);
  6427. //mDebugTarget->mDwFrameDescriptorMap.erase()
  6428. // Remove any of our entries from the mHotReplacedMethodList from 'primary modules' that are not going away
  6429. for (auto dbgType : mHotPrimaryTypes)
  6430. {
  6431. DbgSubprogram** nextSrc = &dbgType->mHotReplacedMethodList.mHead;
  6432. while (*nextSrc != NULL)
  6433. {
  6434. auto* subprogram = *nextSrc;
  6435. if (subprogram->mCompileUnit->mDbgModule == this)
  6436. *nextSrc = subprogram->mNext;
  6437. else
  6438. nextSrc = &(*nextSrc)->mNext;;
  6439. }
  6440. }
  6441. }
  6442. void DbgModule::ReportMemory(MemReporter* memReporter)
  6443. {
  6444. //memReporter->Add("BumpAlloc_Used", mAlloc.GetAllocSize());
  6445. //memReporter->Add("BumpAlloc_Unused", mAlloc.GetTotalAllocSize() - mAlloc.GetAllocSize());
  6446. memReporter->AddBumpAlloc("BumpAlloc", mAlloc);
  6447. memReporter->AddVec(mTypes);
  6448. memReporter->AddVec(mSubprograms);
  6449. //memReporter->Add("TypeMap", mTypeMap.mAlloc.GetTotalAllocSize() + sizeof(StrHashMap<DbgType*>));
  6450. memReporter->AddHashSet("TypeMap", mTypeMap.mMap);
  6451. memReporter->Add("SymbolNameMap", mSymbolNameMap.mAlloc.GetTotalAllocSize() + sizeof(StrHashMap<DbgType*>));
  6452. if (mOrigImageData != NULL)
  6453. {
  6454. memReporter->BeginSection("OrigImageData");
  6455. mOrigImageData->ReportMemory(memReporter);
  6456. memReporter->EndSection();
  6457. }
  6458. }
  6459. DbgType* DbgModule::GetPointerType(DbgType* innerType)
  6460. {
  6461. auto linkedModule = GetLinkedModule();
  6462. BF_ASSERT(innerType->GetDbgModule()->GetLinkedModule() == linkedModule);
  6463. if (innerType->mPtrType == NULL)
  6464. {
  6465. BP_ALLOC_T(DbgType);
  6466. auto ptrType = mAlloc.Alloc<DbgType>();
  6467. ptrType->mCompileUnit = innerType->mCompileUnit;
  6468. ptrType->mLanguage = innerType->mLanguage;
  6469. ptrType->mTypeCode = DbgType_Ptr;
  6470. ptrType->mTypeParam = innerType;
  6471. ptrType->mSize = sizeof(addr_target);
  6472. ptrType->mAlign = (int)ptrType->mSize;
  6473. ptrType->mTypeIdx = (int32)linkedModule->mTypes.size();
  6474. linkedModule->mTypes.push_back(ptrType);
  6475. innerType->mPtrType = ptrType;
  6476. }
  6477. return innerType->mPtrType;
  6478. }
  6479. DbgType* DbgModule::GetConstType(DbgType* innerType)
  6480. {
  6481. auto linkedModule = GetLinkedModule();
  6482. BF_ASSERT(innerType->GetDbgModule()->GetLinkedModule() == linkedModule);
  6483. /*auto itr = linkedModule->mConstTypes.find(innerType);
  6484. if (itr != linkedModule->mConstTypes.end())
  6485. return itr->second;*/
  6486. DbgType* constType = NULL;
  6487. if (linkedModule->mConstTypes.TryGetValue(innerType, &constType))
  6488. return constType;
  6489. BP_ALLOC_T(DbgType);
  6490. constType = mAlloc.Alloc<DbgType>();
  6491. constType->mCompileUnit = innerType->mCompileUnit;
  6492. constType->mLanguage = innerType->mLanguage;
  6493. constType->mTypeCode = DbgType_Const;
  6494. constType->mTypeParam = innerType;
  6495. constType->mSize = sizeof(addr_target);
  6496. constType->mTypeIdx = (int32)linkedModule->mTypes.size();
  6497. linkedModule->mTypes.push_back(constType);
  6498. linkedModule->mConstTypes[innerType] = constType;
  6499. return constType;
  6500. }
  6501. DbgType* DbgModule::GetPrimaryType(DbgType* dbgType)
  6502. {
  6503. if (dbgType->mPriority <= DbgTypePriority_Normal)
  6504. {
  6505. if ((dbgType->mLanguage == DbgLanguage_Beef) && (dbgType->mName != NULL))
  6506. {
  6507. auto newTypeEntry = FindType(dbgType->mName, dbgType->mLanguage);
  6508. if (newTypeEntry != NULL)
  6509. {
  6510. DbgType* newType = newTypeEntry->mValue;
  6511. if ((newType->mTypeCode == DbgType_Ptr) && (newType->IsBfObjectPtr()))
  6512. newType = newType->mTypeParam;
  6513. newType->mPriority = DbgTypePriority_Primary_Implicit;
  6514. return newType;
  6515. }
  6516. }
  6517. else if (dbgType->mName != NULL)
  6518. {
  6519. auto newTypeEntry = FindType(dbgType->mName, dbgType->mLanguage);
  6520. if (newTypeEntry != NULL)
  6521. {
  6522. DbgType* newType = newTypeEntry->mValue;
  6523. newType = newType->RemoveModifiers();
  6524. if (newType != dbgType)
  6525. newType = GetPrimaryType(newType);
  6526. newType->mPriority = DbgTypePriority_Primary_Implicit;
  6527. return newType;
  6528. }
  6529. }
  6530. }
  6531. return dbgType;
  6532. }
  6533. DbgType* DbgModule::GetInnerTypeOrVoid(DbgType* dbgType)
  6534. {
  6535. if (dbgType->mTypeParam != NULL)
  6536. return dbgType->mTypeParam;
  6537. return GetPrimitiveType(DbgType_Void, dbgType->mLanguage);
  6538. }
  6539. DbgType* DbgModule::FindTypeHelper(const String& typeName, DbgType* checkType)
  6540. {
  6541. for (auto subType : checkType->mSubTypeList)
  6542. {
  6543. if (strcmp(subType->mTypeName, typeName.c_str()) == 0)
  6544. return subType;
  6545. }
  6546. for (auto baseType : checkType->mBaseTypes)
  6547. {
  6548. auto retType = FindTypeHelper(typeName, baseType->mBaseType);
  6549. if (retType != NULL)
  6550. return retType;
  6551. }
  6552. return NULL;
  6553. }
  6554. DbgType* DbgModule::FindType(const String& typeName, DbgType* contextType, DbgLanguage language, bool bfObjectPtr)
  6555. {
  6556. if ((language == DbgLanguage_Unknown) && (contextType != NULL))
  6557. language = contextType->mLanguage;
  6558. if (typeName.length() > 0)
  6559. {
  6560. if (typeName[typeName.length() - 1] == '*')
  6561. {
  6562. DbgType* dbgType = FindType(typeName.Substring(0, typeName.length() - 1), contextType, language, bfObjectPtr);
  6563. if (dbgType == NULL)
  6564. return NULL;
  6565. return GetPointerType(dbgType);
  6566. }
  6567. }
  6568. auto entry = GetLinkedModule()->mTypeMap.Find(typeName.c_str(), language);
  6569. if (entry != NULL)
  6570. {
  6571. if ((bfObjectPtr) && (entry->mValue->IsBfObject()))
  6572. return GetPointerType(entry->mValue);
  6573. return entry->mValue;
  6574. }
  6575. if (contextType != NULL)
  6576. {
  6577. DbgType* checkType = contextType;
  6578. if (checkType->IsPointer())
  6579. checkType = checkType->mTypeParam;
  6580. return FindTypeHelper(typeName, checkType);
  6581. }
  6582. return NULL;
  6583. }
  6584. DbgTypeMap::Entry* DbgModule::FindType(const char* typeName, DbgLanguage language)
  6585. {
  6586. return GetLinkedModule()->mTypeMap.Find(typeName, language);
  6587. /*auto& typeMap = GetLinkedModule()->mTypeMap;
  6588. auto dbgTypeEntry = typeMap.Find(typeName);
  6589. if (dbgTypeEntry == NULL)
  6590. return NULL;
  6591. if (dbgTypeEntry->mValue->mLanguage == language)
  6592. return dbgTypeEntry;
  6593. while (dbgTypeEntry != NULL)
  6594. {
  6595. DbgType* dbgType = dbgTypeEntry->mValue;
  6596. if ((dbgType->GetLanguage() == language) && (typeMap.StrEqual(dbgType->mName, typeName)))
  6597. return dbgTypeEntry;
  6598. dbgTypeEntry = dbgTypeEntry->mNext;
  6599. }*/
  6600. //return NULL;
  6601. }
  6602. DbgType* DbgModule::GetPrimitiveType(DbgTypeCode typeCode, DbgLanguage language)
  6603. {
  6604. if (language == DbgLanguage_Beef)
  6605. return mBfPrimitiveTypes[(int)typeCode];
  6606. else
  6607. return mCPrimitiveTypes[(int)typeCode];
  6608. }
  6609. DbgType* DbgModule::GetPrimitiveStructType(DbgTypeCode typeCode)
  6610. {
  6611. const char* name = mPrimitiveStructNames[typeCode];
  6612. if (name == NULL)
  6613. return NULL;
  6614. return FindType(name, NULL, DbgLanguage_Beef);
  6615. }
  6616. DbgType* DbgModule::GetSizedArrayType(DbgType * elementType, int count)
  6617. {
  6618. auto linkedModule = GetLinkedModule();
  6619. if ((linkedModule != NULL) && (linkedModule != this))
  6620. {
  6621. return linkedModule->GetSizedArrayType(elementType, count);
  6622. }
  6623. DbgType** sizedArrayTypePtr;
  6624. DbgSizedArrayEntry entry;
  6625. entry.mElementType = elementType;
  6626. entry.mCount = count;
  6627. if (mSizedArrayTypes.TryAdd(entry, NULL, &sizedArrayTypePtr))
  6628. {
  6629. BP_ALLOC_T(DbgType);
  6630. auto sizedArrayType = mAlloc.Alloc<DbgType>();
  6631. sizedArrayType->mCompileUnit = elementType->mCompileUnit;
  6632. sizedArrayType->mLanguage = elementType->mLanguage;
  6633. sizedArrayType->mTypeCode = DbgType_SizedArray;
  6634. sizedArrayType->mTypeParam = elementType;
  6635. sizedArrayType->mSize = count * elementType->GetStride();
  6636. sizedArrayType->mAlign = elementType->GetAlign();
  6637. sizedArrayType->mSizeCalculated = true;
  6638. sizedArrayType->mTypeIdx = (int32)mTypes.size();
  6639. linkedModule->mTypes.push_back(sizedArrayType);
  6640. *sizedArrayTypePtr = sizedArrayType;
  6641. }
  6642. return *sizedArrayTypePtr;
  6643. }