2
0

COFF.cpp 195 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317
  1. #include "COFF.h"
  2. #include "BeefySysLib/FileStream.h"
  3. #include "BeefySysLib/MemStream.h"
  4. #include "codeview/cvinfo.h"
  5. #include "DebugTarget.h"
  6. #include "DebugManager.h"
  7. #include "DWARFInfo.h"
  8. #include "BeefySysLib/util/PerfTimer.h"
  9. #include "BeefySysLib/util/Dictionary.h"
  10. #include "BeefySysLib/util/BeefPerf.h"
  11. #include "BeefySysLib/util/BeefPerf.h"
  12. #include "BeefySysLib/util/ZipFile.h"
  13. #include "BeefySysLib/util/Hash.h"
  14. #include "BeefySysLib/platform/PlatformHelper.h"
  15. #include "WinDebugger.h"
  16. #include "MiniDumpDebugger.h"
  17. #include "Linker/BlHash.h"
  18. #include "Backend/BeLibManger.h"
  19. #include "Compiler/BfUtil.h"
  20. #include <shlobj.h>
  21. #include "BeefySysLib/util/AllocDebug.h"
  22. #define LF_CLASS_EX 0x1608
  23. #define LF_STRUCTURE_EX 0x1609
  24. USING_NS_BF_DBG;
  25. #define MSF_SIGNATURE_700 "Microsoft C/C++ MSF 7.00\r\n\032DS\0\0"
  26. static const int NUM_ROOT_DIRS = 73;
  27. #pragma warning(default:4800)
  28. #define ADDR_FLAG_ABS 0x8000000000000000L
  29. #define GET(T) *((T*)(data += sizeof(T)) - 1)
  30. #define GET_INTO(T, name) T name = GET(T)
  31. #define GET_FROM(ptr, T) *((T*)(ptr += sizeof(T)) - 1)
  32. #define PTR_ALIGN(ptr, origPtr, alignSize) ptr = ( (origPtr)+( ((ptr - origPtr) + (alignSize - 1)) & ~(alignSize - 1) ) )
  33. static const char* DataGetString(uint8*& data)
  34. {
  35. const char* prevVal = (const char*)data;
  36. while (*data != 0)
  37. data++;
  38. data++;
  39. return prevVal;
  40. }
  41. static const char* SafeDataGetString(uint8*& data, uint8* dataEnd)
  42. {
  43. if (data >= dataEnd)
  44. return NULL;
  45. const char* prevVal = (const char*)data;
  46. while (*data != 0)
  47. {
  48. data++;
  49. if (data >= dataEnd)
  50. return NULL;
  51. }
  52. data++;
  53. return prevVal;
  54. }
  55. #define CREATE_PRIMITIVE(pdbTypeCode, dwTypeCode, typeName, type) \
  56. BP_ALLOC_T(DbgType); \
  57. dbgType = mAlloc.Alloc<DbgType>(); \
  58. dbgType->mCompileUnit = mMasterCompileUnit; \
  59. dbgType->mTypeName = typeName; \
  60. dbgType->mName = typeName; \
  61. dbgType->mTypeCode = dwTypeCode; \
  62. dbgType->mSize = sizeof(type); \
  63. dbgType->mAlign = sizeof(type); \
  64. mCvSystemTypes[(int)pdbTypeCode] = dbgType; \
  65. \
  66. BP_ALLOC_T(DbgType); \
  67. ptrType = mAlloc.Alloc<DbgType>(); \
  68. ptrType->mCompileUnit = mMasterCompileUnit; \
  69. ptrType->mTypeCode = DbgType_Ptr; \
  70. ptrType->mSize = sizeof(addr_target); \
  71. ptrType->mAlign = sizeof(addr_target); \
  72. ptrType->mTypeParam = dbgType; \
  73. dbgType->mPtrType = ptrType; \
  74. mCvSystemTypes[(int)pdbTypeCode | ptrMask] = ptrType;
  75. #define REGISTER_PRIMITIVE() mTypeMap.Insert(dbgType)
  76. static const char* GetLastDoubleColon(const char* name)
  77. {
  78. int templateDepth = 0;
  79. int parenDepth = 0;
  80. const char* lastDblColon = NULL;
  81. for (const char* checkPtr = name; *checkPtr != '\0'; checkPtr++)
  82. {
  83. char c = checkPtr[0];
  84. if (c == '<')
  85. templateDepth++;
  86. else if (c == '>')
  87. templateDepth--;
  88. else if (c == '(')
  89. parenDepth++;
  90. else if (c == ')')
  91. parenDepth--;
  92. if ((templateDepth == 0) && (parenDepth == 0) && (c == ':') && (checkPtr[1] == ':'))
  93. lastDblColon = checkPtr;
  94. if ((templateDepth == 0) && (parenDepth == 0) && (c == ' '))
  95. {
  96. // This catches cases like "Beefy::BfMethodRef::operator Beefy::BfMethodInstance*", where we want to match the :: right before "operator"
  97. break;
  98. }
  99. }
  100. return lastDblColon;
  101. }
  102. static const char* GetNamespaceEnd(const char* name)
  103. {
  104. int templateDepth = 0;
  105. const char* lastDblColon = NULL;
  106. for (const char* checkPtr = name; *checkPtr != '\0'; checkPtr++)
  107. {
  108. char c = checkPtr[0];
  109. if ((c == '<') || (c == ' '))
  110. return NULL;
  111. if ((c == ':') && (checkPtr[1] == ':'))
  112. lastDblColon = checkPtr;
  113. }
  114. return lastDblColon;
  115. }
  116. // This version inserts namespaces that contain specialized methods, but since we don't (at the time of this comment) support
  117. // calling specialized methods, we don't bother doing this -- it increases the scan time
  118. #if 0
  119. static const char* GetNamespaceEnd(const char* name)
  120. {
  121. bool hadTemplate = false;
  122. int templateDepth = 0;
  123. const char* lastDblColon = NULL;
  124. for (const char* checkPtr = name; *checkPtr != '\0'; checkPtr++)
  125. {
  126. char c = checkPtr[0];
  127. if (c == '<')
  128. {
  129. hadTemplate = true;
  130. templateDepth++;
  131. }
  132. else if (c == '>')
  133. templateDepth--;
  134. if ((templateDepth == 0) && (c == ':') && (checkPtr[1] == ':'))
  135. {
  136. if (hadTemplate)
  137. {
  138. // This is to reject cases like NameSpace::TypeName<int>::sField - since we know that there's a typename attached and we just want 'loose' namespaces,
  139. // But this still allows NameSpace::Method<int>, where "NameSpace" is indeed a loose namespace we need to catch
  140. return NULL;
  141. }
  142. lastDblColon = checkPtr;
  143. }
  144. if ((templateDepth == 0) && (c == ' '))
  145. {
  146. // This catches cases like "Beefy::BfMethodRef::operator Beefy::BfMethodInstance*", where we want to match the :: right before "operator"
  147. // In these cases we do know this is an operator method name so it's not in a loose namespace
  148. return NULL;
  149. }
  150. }
  151. return lastDblColon;
  152. }
  153. #endif
  154. //////////////////////////////////////////////////////////////////////////
  155. uint8* CvStreamReader::GetTempPtr(int offset, int size, bool mayRecurse, bool* madeCopy)
  156. {
  157. int pageStart = offset >> mPageBits;
  158. int pageEnd = (offset + size - 1) >> mPageBits;
  159. if (pageStart >= pageEnd)
  160. return mStreamPtrs.mVals[pageStart] + (offset & ((1<<mPageBits) - 1));
  161. // Handle the relatively-rare case of spanning multiple pages
  162. if (madeCopy != NULL)
  163. *madeCopy = true;
  164. uint8* destPtr;
  165. if (mayRecurse)
  166. {
  167. if (mCOFF->mTempBufIdx >= mCOFF->mTempBufs.size())
  168. mCOFF->mTempBufs.push_back(Array<uint8>());
  169. auto& arr = mCOFF->mTempBufs[mCOFF->mTempBufIdx++];
  170. if (arr.size() < size)
  171. arr.Resize(size);
  172. destPtr = arr.mVals;
  173. }
  174. else
  175. {
  176. if (mTempData.size() < size)
  177. mTempData.Resize(size);
  178. destPtr = mTempData.mVals;
  179. }
  180. uint8* dest = destPtr;
  181. while (size > 0)
  182. {
  183. int copyPage = offset >> mPageBits;
  184. int pageOffset = offset & ((1 << mPageBits) - 1);
  185. int copyBytes = BF_MIN(mCOFF->mCvPageSize - pageOffset, size);
  186. memcpy(destPtr, mStreamPtrs.mVals[copyPage] + pageOffset, copyBytes);
  187. destPtr += copyBytes;
  188. offset += copyBytes;
  189. size -= copyBytes;
  190. }
  191. return dest;
  192. }
  193. uint8* CvStreamReader::GetPermanentPtr(int offset, int size, bool* madeCopy)
  194. {
  195. int pageStart = offset >> mPageBits;
  196. int pageEnd = (offset + size - 1) >> mPageBits;
  197. if (pageStart == pageEnd)
  198. return mStreamPtrs.mVals[pageStart] + (offset & ((1 << mPageBits) - 1));
  199. // Handle the relatively-rare case of spanning multiple pages
  200. BP_ALLOC("GetPermanentPtr", size);
  201. uint8* destData = mCOFF->mAlloc.AllocBytes(size, "GetPermanentPtr");
  202. if (madeCopy != NULL)
  203. *madeCopy = true;
  204. uint8* destPtr = destData;
  205. while (size > 0)
  206. {
  207. int copyPage = offset >> mPageBits;
  208. int pageOffset = offset & ((1 << mPageBits) - 1);
  209. int copyBytes = BF_MIN(mCOFF->mCvPageSize - pageOffset, size);
  210. memcpy(destPtr, mStreamPtrs.mVals[copyPage] + pageOffset, copyBytes);
  211. destPtr += copyBytes;
  212. offset += copyBytes;
  213. size -= copyBytes;
  214. }
  215. return destData;
  216. }
  217. //////////////////////////////////////////////////////////////////////////
  218. COFF::COFF(DebugTarget* debugTarget) : DbgModule(debugTarget)
  219. {
  220. mParseKind = ParseKind_Full;
  221. memset(mWantPDBGuid, 0, 16);
  222. memset(mPDBGuid, 0, 16);
  223. mWantAge = -1;
  224. mDebugAge = -1;
  225. mFileAge = -1;
  226. mCvMinTag = -1;
  227. mCvMaxTag = -1;
  228. mCvIPIMinTag = -1;
  229. mCvIPIMaxTag = -1;
  230. mMasterCompileUnit = NULL;
  231. mTempBufIdx = 0;
  232. mIs64Bit = false;
  233. mCvPageSize = 0;
  234. mCvPageBits = 31;
  235. mCvDataStream = NULL;
  236. mCvHeaderData = NULL;
  237. mCvStrTableData = NULL;
  238. mCvPublicSymbolData = NULL;
  239. mCvGlobalSymbolData = NULL;
  240. mNewFPOData = NULL;
  241. mCvGlobalSymbolInfoStream = 0;
  242. mCvPublicSymbolInfoStream = 0;
  243. mCvSymbolRecordStream = 0;
  244. mCvNewFPOStream = 0;
  245. mCvMappedFile = INVALID_HANDLE_VALUE;
  246. mCvMappedFileMapping = INVALID_HANDLE_VALUE;
  247. mCvMappedViewOfFile = NULL;
  248. mCvMappedFileSize = 0;
  249. //mParsedProcRecs = false;
  250. mGlobalsTargetType = NULL;
  251. mPrevScanName = NULL;
  252. mProcSymCount = 0;
  253. mCvSrcSrvStream = -1;
  254. mCvEmitStream = -1;
  255. mIsFastLink = false;
  256. mHotThunkCurAddr = 0;
  257. mHotThunkDataLeft = 0;
  258. mTriedSymSrv = false;
  259. mDbgSymRequest = NULL;
  260. mWantsAutoLoadDebugInfo = false;
  261. mPDBLoaded = false;
  262. mEmitSourceFile = NULL;
  263. }
  264. COFF::~COFF()
  265. {
  266. BF_ASSERT(mTempBufIdx == 0);
  267. ClosePDB();
  268. mDebugger->mDbgSymSrv.ReleaseRequest(mDbgSymRequest);
  269. }
  270. const char* COFF::CvCheckTargetMatch(const char* name, bool& wasBeef)
  271. {
  272. if (mGlobalsTargetType == NULL)
  273. return NULL;
  274. /*if (mGlobalsTargetType->IsGlobalsContainer())
  275. {
  276. wasBeef = true;
  277. }
  278. else*/ if (mGlobalsTargetType->mLanguage == DbgLanguage_Beef)
  279. {
  280. if (strncmp(name, "_bf::", 5) != 0)
  281. return NULL;
  282. name += 5;
  283. wasBeef = true;
  284. }
  285. else if (mGlobalsTargetType->mTypeCode == DbgType_Root)
  286. {
  287. if (strncmp(name, "_bf::", 5) == 0)
  288. {
  289. wasBeef = true;
  290. name += 5;
  291. }
  292. }
  293. if (mGlobalsTargetType->mName == NULL)
  294. {
  295. for (const char* cPtr = name; *cPtr != '\0'; cPtr++)
  296. {
  297. if (*cPtr == ':')
  298. return NULL;
  299. if (*cPtr == '<')
  300. return NULL;
  301. }
  302. return name;
  303. }
  304. const char* memberNamePtr = name;
  305. const char* typeName = mGlobalsTargetType->mName;
  306. while (true)
  307. {
  308. char cm = *(memberNamePtr++);
  309. char ct = *(typeName++);
  310. if (ct == 0)
  311. {
  312. if (cm == ':')
  313. {
  314. const char* memberName = memberNamePtr + 1;
  315. for (const char* cPtr = memberName; *cPtr != '\0'; cPtr++)
  316. {
  317. if (*cPtr == ':')
  318. return NULL;
  319. if (*cPtr == '<')
  320. return NULL;
  321. }
  322. return memberName;
  323. }
  324. return NULL;
  325. }
  326. if (cm != ct)
  327. {
  328. if ((cm == ':') && (ct == '.') && (memberNamePtr[0] == ':'))
  329. {
  330. memberNamePtr++;
  331. continue;
  332. }
  333. return NULL;
  334. }
  335. }
  336. }
  337. int COFF::CvGetStringHash(const char* str)
  338. {
  339. if (str == NULL)
  340. return 0;
  341. int curHash = 0;
  342. const char* curHashPtr = str;
  343. while (*curHashPtr != 0)
  344. {
  345. char c = *curHashPtr;
  346. curHash = ((curHash ^ *curHashPtr) << 5) - curHash;
  347. curHashPtr++;
  348. }
  349. return curHash & 0x3FFFFFFF;
  350. }
  351. // Remove "__ptr64"
  352. void COFF::CvFixupName(char* name)
  353. {
  354. char* cPtrOut = NULL;
  355. for (char* cPtr = name; *cPtr != '\0'; cPtr++)
  356. {
  357. if ((cPtr[0] == '_') && (cPtr[1] == '_'))
  358. {
  359. if (strncmp(cPtr + 2, "ptr64", 5) == 0)
  360. {
  361. if (cPtrOut == NULL)
  362. cPtrOut = cPtr;
  363. cPtr += 6;
  364. }
  365. }
  366. else if (cPtrOut != NULL)
  367. {
  368. *(cPtrOut++) = *cPtr;
  369. }
  370. }
  371. if (cPtrOut != NULL)
  372. *(cPtrOut++) = '\0';
  373. }
  374. void COFF::InitCvTypes()
  375. {
  376. if (mMasterCompileUnit != NULL)
  377. {
  378. BF_ASSERT(mCompileUnits.size() == 1);
  379. return;
  380. }
  381. mDbgFlavor = DbgFlavor_MS;
  382. mCvSystemTypes.Resize(0x700);
  383. DbgType* dbgType;
  384. DbgType* ptrType;
  385. #ifdef BF_DBG_32
  386. const int ptrMask = 0x0400; // T_32*
  387. #else
  388. const int ptrMask = 0x0600; // T_64*
  389. #endif
  390. mMasterCompileUnit = new DbgCompileUnit(this);
  391. mMasterCompileUnit->mDbgModule = this;
  392. mMasterCompileUnit->mIsMaster = true;
  393. mCompileUnits.push_back(mMasterCompileUnit);
  394. CREATE_PRIMITIVE(T_NOTTRANS, DbgType_Void, "void", void*);
  395. mCvSystemTypes[T_NOTTRANS]->mSize = 0;
  396. mCvSystemTypes[T_NOTTRANS]->mAlign = 0;
  397. CREATE_PRIMITIVE(T_NOTYPE, DbgType_Void, "void", void*);
  398. mCvSystemTypes[T_NOTYPE]->mSize = 0;
  399. mCvSystemTypes[T_NOTYPE]->mAlign = 0;
  400. CREATE_PRIMITIVE(T_VOID, DbgType_Void, "void", void*);
  401. mCvSystemTypes[T_VOID]->mSize = 0;
  402. mCvSystemTypes[T_VOID]->mAlign = 0;
  403. mCvSystemTypes[T_PVOID] = ptrType;
  404. #ifdef BF_DBG_32
  405. BP_ALLOC_T(DbgType);
  406. ptrType = mAlloc.Alloc<DbgType>();
  407. ptrType->mCompileUnit = mMasterCompileUnit;
  408. ptrType->mTypeCode = DbgType_Ptr;
  409. ptrType->mSize = 8;
  410. ptrType->mAlign = 8;
  411. ptrType->mTypeParam = dbgType;
  412. dbgType->mPtrType = ptrType;
  413. mCvSystemTypes[(int)T_VOID | 0x0600] = ptrType;
  414. #endif
  415. #ifdef BF_DBG_32
  416. CREATE_PRIMITIVE(T_HRESULT, DbgType_u32, "HRESULT", addr_target);
  417. #else
  418. CREATE_PRIMITIVE(T_HRESULT, DbgType_u64, "HRESULT", addr_target);
  419. #endif
  420. CREATE_PRIMITIVE(T_CHAR, DbgType_SChar, "char", char);
  421. CREATE_PRIMITIVE(T_RCHAR, DbgType_SChar, "char", char); REGISTER_PRIMITIVE();
  422. CREATE_PRIMITIVE(T_UCHAR, DbgType_UChar, "char8", uint8); REGISTER_PRIMITIVE();
  423. CREATE_PRIMITIVE(T_WCHAR, DbgType_SChar16, "wchar", uint16); REGISTER_PRIMITIVE();
  424. CREATE_PRIMITIVE(T_CHAR16, DbgType_UChar16, "char16", uint16);
  425. CREATE_PRIMITIVE(T_CHAR32, DbgType_UChar32, "char32", uint32);
  426. CREATE_PRIMITIVE(T_INT1, DbgType_i8, "sbyte", int8);
  427. CREATE_PRIMITIVE(T_UINT1, DbgType_u8, "byte", uint8);
  428. CREATE_PRIMITIVE(T_SHORT, DbgType_i16, "short", int16);
  429. CREATE_PRIMITIVE(T_INT2, DbgType_i16, "short", int16);
  430. CREATE_PRIMITIVE(T_USHORT, DbgType_u16, "ushort", uint16);
  431. CREATE_PRIMITIVE(T_UINT2, DbgType_u16, "ushort", uint16);
  432. CREATE_PRIMITIVE(T_LONG, DbgType_i32, "int", int32);
  433. CREATE_PRIMITIVE(T_INT4, DbgType_i32, "int", int32);
  434. CREATE_PRIMITIVE(T_ULONG, DbgType_i32, "uint", uint32);
  435. CREATE_PRIMITIVE(T_UINT4, DbgType_u32, "uint", uint32);
  436. CREATE_PRIMITIVE(T_QUAD, DbgType_i64, "int64", int64);
  437. CREATE_PRIMITIVE(T_INT8, DbgType_i64, "int64", int64); REGISTER_PRIMITIVE();
  438. CREATE_PRIMITIVE(T_UQUAD, DbgType_u64, "uint64", uint64);
  439. CREATE_PRIMITIVE(T_UINT8, DbgType_u64, "uint64", uint64); REGISTER_PRIMITIVE();
  440. CREATE_PRIMITIVE(T_OCT, DbgType_i128, "int128", uint128);
  441. CREATE_PRIMITIVE(T_INT16, DbgType_i128, "int128", uint128);
  442. CREATE_PRIMITIVE(T_UOCT, DbgType_u128, "uint128", uint128);
  443. CREATE_PRIMITIVE(T_UINT16, DbgType_u128, "uint128", uint128);
  444. CREATE_PRIMITIVE(T_BOOL08, DbgType_Bool, "bool", bool);
  445. CREATE_PRIMITIVE(T_BOOL32, DbgType_Bool, "bool", int32);
  446. CREATE_PRIMITIVE(T_REAL32, DbgType_Single, "float", float);
  447. CREATE_PRIMITIVE(T_REAL64, DbgType_Double, "double", double);
  448. CREATE_PRIMITIVE(T_REAL80, DbgType_Double, "double80", double); // This isn't correct
  449. }
  450. addr_target COFF::GetSectionAddr(uint16 section, uint32 offset)
  451. {
  452. if (section == 0)
  453. return offset;
  454. if ((section & 0x8000) != 0)
  455. {
  456. auto linkedModule = GetLinkedModule();
  457. addr_target hiBase = linkedModule->mSecRelEncodingVec[section & 0x7FFF];
  458. return hiBase + offset;
  459. }
  460. int rva = mSectionHeaders[section - 1].mVirtualAddress;
  461. if (rva == 0)
  462. return ADDR_FLAG_ABS + offset;
  463. return (addr_target)mImageBase + rva + offset;
  464. }
  465. DbgType* COFF::CvGetType(int typeId)
  466. {
  467. //TODO: How do we handle types that have the high bit set?
  468. if (typeId < 0)
  469. return NULL;
  470. /*if (typeId == 0)
  471. return NULL;*/
  472. if (typeId < 0x1000)
  473. {
  474. TYPE_ENUM_e typeEnum = (TYPE_ENUM_e)typeId;
  475. DbgType* type = mCvSystemTypes[typeId];
  476. return type;
  477. }
  478. DbgType* type = mCvTypeMap[typeId - mCvMinTag];
  479. if (type == NULL)
  480. type = CvParseType(typeId);
  481. /*if ((!allowNull) || (type != NULL))
  482. {
  483. BF_ASSERT(type->mCompileUnit->mDbgModule == this);
  484. }*/
  485. return type;
  486. }
  487. DbgType* COFF::CvGetTypeSafe(int typeId)
  488. {
  489. DbgType* type = CvGetType(typeId);
  490. if (type != NULL)
  491. return type;
  492. return CvGetType(T_VOID);
  493. }
  494. DbgType* COFF::CvGetType(int typeId, CvCompileUnit* compileUnit)
  495. {
  496. if ((typeId & 0x80000000) != 0)
  497. {
  498. return CvGetType(MapImport(compileUnit, typeId));
  499. }
  500. return CvGetType(typeId);
  501. }
  502. int COFF::CvGetTagStart(int tagIdx, bool ipi)
  503. {
  504. if (tagIdx == 0)
  505. return NULL;
  506. if (ipi)
  507. return mCvIPITagStartMap[tagIdx - mCvMinTag];
  508. else
  509. return mCvTagStartMap[tagIdx - mCvMinTag];
  510. }
  511. int COFF::CvGetTagSize(int tagIdx, bool ipi)
  512. {
  513. if (tagIdx == 0)
  514. return 0;
  515. if (ipi)
  516. return mCvIPITagStartMap[tagIdx - mCvMinTag + 1] - mCvIPITagStartMap[tagIdx - mCvMinTag];
  517. else
  518. return mCvTagStartMap[tagIdx - mCvMinTag + 1] - mCvTagStartMap[tagIdx - mCvMinTag];
  519. }
  520. uint8* COFF::CvGetTagData(int tagIdx, bool ipi, int* outDataSize)
  521. {
  522. if (tagIdx == 0)
  523. return NULL;
  524. if ((ipi) && (tagIdx < mCvIPIMinTag))
  525. return NULL;
  526. if ((!ipi) && (tagIdx < mCvMinTag))
  527. return NULL;
  528. auto& reader = ipi ? mCvIPIReader : mCvTypeSectionReader;
  529. int offset = ipi ? mCvIPITagStartMap[tagIdx - mCvIPIMinTag] : mCvTagStartMap[tagIdx - mCvMinTag];
  530. uint8* data = reader.GetTempPtr(offset, 4);
  531. uint16 trLength = *(uint16*)data;
  532. data = reader.GetTempPtr(offset + 2, trLength, true);
  533. if (outDataSize != NULL)
  534. *outDataSize = trLength;
  535. return data;
  536. }
  537. int64 COFF::CvParseConstant(uint16 constVal, uint8*& data)
  538. {
  539. if (constVal < LF_NUMERIC) // 0x8000
  540. return constVal;
  541. switch (constVal)
  542. {
  543. case LF_CHAR:
  544. return GET(int8);
  545. case LF_SHORT:
  546. return GET(int16);
  547. case LF_USHORT:
  548. return GET(uint16);
  549. case LF_LONG:
  550. return GET(int32);
  551. case LF_ULONG:
  552. return GET(uint32);
  553. case LF_QUADWORD:
  554. return GET(int64);
  555. case LF_UQUADWORD:
  556. return (int64)GET(uint64);
  557. default:
  558. BF_FATAL("Not handled");
  559. }
  560. return 0;
  561. }
  562. int64 COFF::CvParseConstant(uint8*& data)
  563. {
  564. uint16 val = GET(uint16);
  565. return CvParseConstant(val, data);
  566. }
  567. const char* COFF::CvParseString(uint8*& data)
  568. {
  569. const char* strStart = (const char*)data;
  570. int strLen = strlen((const char*)data);
  571. if (strLen == 0)
  572. return NULL;
  573. data += strLen + 1;
  574. return strStart;
  575. }
  576. const char* COFF::CvParseAndDupString(uint8*& data)
  577. {
  578. int strLen = strlen((const char*)data);
  579. if (strLen == 0)
  580. {
  581. data++;
  582. return NULL;
  583. }
  584. BP_ALLOC("CvParseAndDupString", strLen + 1);
  585. char* dupStr = (char*)mAlloc.AllocBytes(strLen + 1, "CvParseAndDupString");
  586. memcpy(dupStr, data, strLen);
  587. data += strLen + 1;
  588. return dupStr;
  589. }
  590. const char* COFF::CvDupString(const char* str, int strLen)
  591. {
  592. BP_ALLOC("CvDupString", strLen + 1);
  593. char* dupStr = (char*)mAlloc.AllocBytes(strLen + 1, "CvParseAndDupString");
  594. memcpy(dupStr, str, strLen);
  595. dupStr[strLen] = 0;
  596. return dupStr;
  597. }
  598. void COFF::CvParseArgList(DbgSubprogram* subprogram, int tagIdx, bool ipi)
  599. {
  600. uint8* data = CvGetTagData(tagIdx, ipi);
  601. CvAutoReleaseTempData releaseTempData(this, data);
  602. int16 trLeafType = GET(int16);
  603. BF_ASSERT(trLeafType == LF_ARGLIST);
  604. int argCount = GET(int32);
  605. for (int argIdx = 0; argIdx < argCount; argIdx++)
  606. {
  607. CV_typ_t argTypeId = GET(CV_typ_t);
  608. DbgType* argType = CvGetType(argTypeId);
  609. BP_ALLOC_T(DbgVariable);
  610. DbgVariable* arg = mAlloc.Alloc<DbgVariable>();
  611. arg->mType = argType;
  612. arg->mIsParam = true;
  613. subprogram->mParams.PushBack(arg);
  614. }
  615. ReleaseTempBuf(data);
  616. }
  617. DbgSubprogram* COFF::CvParseMethod(DbgType* parentType, const char* methodName, int tagIdx, bool ipi, DbgSubprogram* subprogram)
  618. {
  619. BP_ZONE("COFF::CvParseMethod");
  620. uint8* data = CvGetTagData(tagIdx, ipi);
  621. if (data == NULL)
  622. return NULL;
  623. CvAutoReleaseTempData releaseTempData(this, data);
  624. bool fromTypeSection = methodName != NULL;
  625. uint8* dataStart = data;
  626. int16 trLeafType = GET(int16);
  627. if (trLeafType == LF_FUNC_ID)
  628. {
  629. lfFuncId* funcData = (lfFuncId*)dataStart;
  630. subprogram = CvParseMethod(NULL, (const char*)funcData->name, funcData->type, false, subprogram);
  631. return subprogram;
  632. }
  633. if (trLeafType == LF_MFUNC_ID)
  634. {
  635. lfMFuncId* funcData = (lfMFuncId*)dataStart;
  636. auto parentType = CvGetType(funcData->parentType);
  637. //subprogram = CvParseMethod(parentType, (const char*)funcData->name, funcData->type, false, subprogram);
  638. // We shouldn't pass parentType in there, because that would be the declType and not the actual primary type (ie: definition type)
  639. subprogram = CvParseMethod(NULL, (const char*)funcData->name, funcData->type, false, subprogram);
  640. return subprogram;
  641. }
  642. //DbgSubprogram* subprogram = mAlloc.Alloc<DbgSubprogram>();
  643. if (subprogram == NULL)
  644. {
  645. BP_ALLOC_T(DbgSubprogram);
  646. subprogram = mAlloc.Alloc<DbgSubprogram>();
  647. }
  648. subprogram->mName = methodName;
  649. int argListId = 0;
  650. if (trLeafType == LF_MFUNCTION)
  651. {
  652. static int gMFuncIdx = 0;
  653. gMFuncIdx++;
  654. lfMFunc* funcData = (lfMFunc*)dataStart;
  655. argListId = funcData->arglist;
  656. subprogram->mReturnType = CvGetType(funcData->rvtype);
  657. //TODO:
  658. if (parentType == NULL)
  659. subprogram->mParentType = CvGetType(funcData->classtype);
  660. DbgType* thisType = CvGetType(funcData->thistype);
  661. if ((thisType != NULL) && (!thisType->IsVoid()))
  662. {
  663. BP_ALLOC_T(DbgVariable);
  664. DbgVariable* arg = mAlloc.Alloc<DbgVariable>();
  665. arg->mType = thisType;
  666. arg->mIsParam = true;
  667. arg->mTagIdx = gMFuncIdx;
  668. subprogram->mParams.PushBack(arg);
  669. //TODO: Why did we have this "fromTypeSection" check? It caused non-static methods from S_GPROC32_ID to look like static methods in the autocomplete list
  670. //if (fromTypeSection)
  671. {
  672. arg->mType = thisType;
  673. subprogram->mHasThis = true;
  674. arg->mName = "this";
  675. }
  676. }
  677. }
  678. else if (trLeafType == LF_METHODLIST)
  679. {
  680. int32 flags = GET(int32);
  681. int methodIndex = GET(int32);
  682. return CvParseMethod(parentType, methodName, methodIndex, ipi, subprogram);
  683. }
  684. else if (trLeafType == LF_PROCEDURE)
  685. {
  686. lfProc* funcData = (lfProc*)dataStart;
  687. subprogram->mReturnType = CvGetType(funcData->rvtype);
  688. argListId = funcData->arglist;
  689. }
  690. else if (trLeafType == LF_MFUNC_ID)
  691. {
  692. //
  693. }
  694. else
  695. {
  696. SoftFail(StrFormat("Unhandled func type at tagId %d ipi %d", tagIdx, ipi));
  697. }
  698. if ((parentType != NULL) && (!IsObjectFile()))
  699. {
  700. subprogram->mCompileUnit = parentType->mCompileUnit;
  701. parentType->mMethodList.PushBack(subprogram);
  702. }
  703. mSubprograms.push_back(subprogram);
  704. CvParseArgList(subprogram, argListId, ipi);
  705. return subprogram;
  706. }
  707. void COFF::CvParseMethodList(DbgType* parentType, const char* methodName, int tagIdx, bool ipi)
  708. {
  709. int dataSize = 0;
  710. uint8* data = CvGetTagData(tagIdx, ipi, &dataSize);
  711. CvAutoReleaseTempData releaseTempData(this, data);
  712. uint8* dataEnd = data + dataSize;
  713. int16 trLeafType = GET(int16);
  714. BF_ASSERT(trLeafType == LF_METHODLIST);
  715. while (data < dataEnd)
  716. {
  717. int32 flags = GET(int32);
  718. int32 methodIndex = GET(int32);
  719. CvParseMethod(parentType, methodName, methodIndex, ipi);
  720. }
  721. }
  722. void COFF::CvParseMembers(DbgType* parentType, int tagIdx, bool ipi)
  723. {
  724. if (tagIdx == NULL)
  725. return;
  726. auto& reader = ipi ? mCvIPIReader : mCvTypeSectionReader;
  727. int offset = ipi ? mCvIPITagStartMap[tagIdx - mCvIPIMinTag] : mCvTagStartMap[tagIdx - mCvMinTag];
  728. uint8* data = reader.GetTempPtr(offset, 4);
  729. uint16 trLength = *(uint16*)data;
  730. offset += 2;
  731. data = reader.GetTempPtr(offset, trLength, true);
  732. CvAutoReleaseTempData releaseTempData(this, data);
  733. uint8* dataStart = data;
  734. uint8* dataEnd = data + trLength;
  735. int16 trLeafType = GET(int16);
  736. bool strMadeCopy;
  737. auto _ParseString = [&]()
  738. {
  739. strMadeCopy = false;
  740. int nameLen = strlen((const char*)data);
  741. int nameOfs = data - dataStart;
  742. const char* name = (const char*)reader.GetPermanentPtr(offset + nameOfs, nameLen + 1, &strMadeCopy);
  743. data += nameLen + 1;
  744. return name;
  745. };
  746. if (trLeafType == 0)
  747. return;
  748. switch (trLeafType)
  749. {
  750. case LF_FIELDLIST:
  751. {
  752. uint8* sectionStart = data;
  753. while (data < dataEnd)
  754. {
  755. uint8* leafDataStart = data;
  756. int leafType = (int)GET(uint16);
  757. switch (leafType)
  758. {
  759. case LF_VFUNCTAB:
  760. {
  761. lfVFuncTab& vfuncTab = *(lfVFuncTab*)leafDataStart;
  762. DbgType* vtableType = CvGetType(vfuncTab.type);
  763. BP_ALLOC_T(DbgVariable);
  764. DbgVariable* vtableMember = mAlloc.Alloc<DbgVariable>();
  765. vtableMember->mType = vtableType;
  766. parentType->mMemberList.PushFront(vtableMember);
  767. data = (uint8*)(&vfuncTab + 1);
  768. }
  769. break;
  770. case LF_BCLASS:
  771. {
  772. lfBClass& baseClassInfo = *(lfBClass*)leafDataStart;
  773. BP_ALLOC_T(DbgBaseTypeEntry);
  774. DbgBaseTypeEntry* baseTypeEntry = mAlloc.Alloc<DbgBaseTypeEntry>();
  775. data = (uint8*)&baseClassInfo.offset;
  776. baseTypeEntry->mThisOffset = (int)CvParseConstant(data);
  777. if (baseClassInfo.index != 0)
  778. {
  779. baseTypeEntry->mBaseType = CvGetType(baseClassInfo.index);
  780. // if (parentType->mLanguage == DbgLanguage_Beef)
  781. // {
  782. // if (!parentType->mBaseTypes.IsEmpty())
  783. // parentType->mTypeParam = baseTypeEntry->mBaseType;
  784. // }
  785. parentType->mBaseTypes.PushBack(baseTypeEntry);
  786. parentType->mAlign = std::max(parentType->mAlign, baseTypeEntry->mBaseType->GetAlign());
  787. if (!parentType->mSizeCalculated)
  788. {
  789. if ((baseTypeEntry->mBaseType->GetByteCount() == 0) && (baseTypeEntry->mBaseType->IsBfObject()))
  790. {
  791. parentType->mExtType = DbgExtType_Interface;
  792. }
  793. parentType->mSize = BF_MAX(parentType->mSize, baseTypeEntry->mBaseType->GetByteCount());
  794. }
  795. }
  796. }
  797. break;
  798. case LF_VBCLASS:
  799. case LF_IVBCLASS:
  800. {
  801. lfVBClass& baseClassInfo = *(lfVBClass*)leafDataStart;
  802. BP_ALLOC_T(DbgBaseTypeEntry);
  803. DbgBaseTypeEntry* baseTypeEntry = mAlloc.Alloc<DbgBaseTypeEntry>();
  804. baseTypeEntry->mBaseType = CvGetType(baseClassInfo.index);
  805. data = (uint8*)&baseClassInfo.vbpoff;
  806. baseTypeEntry->mThisOffset = (int)CvParseConstant(data);
  807. baseTypeEntry->mVTableOffset = (int)CvParseConstant(data);
  808. parentType->mBaseTypes.PushBack(baseTypeEntry);
  809. }
  810. break;
  811. case LF_ENUMERATE:
  812. {
  813. CV_fldattr_t fieldAttr = GET(CV_fldattr_t);
  814. int64 fieldVal = CvParseConstant(data);
  815. const char* fieldName = _ParseString();
  816. BP_ALLOC_T(DbgVariable);
  817. DbgVariable* member = mAlloc.Alloc<DbgVariable>();
  818. member->mCompileUnit = parentType->mCompileUnit;
  819. member->mConstValue = fieldVal;
  820. member->mName = fieldName;
  821. member->mIsStatic = true;
  822. member->mIsConst = true;
  823. member->mType = parentType->mTypeParam;
  824. FixConstant(member);
  825. parentType->mMemberList.PushBack(member);
  826. }
  827. break;
  828. case LF_NESTTYPE:
  829. {
  830. // Resolve nested types at end through 'primary type'
  831. // Nested types also include nested TypeDefs, so the embedded type index may even be a primitive type here
  832. // Why did we have this check to avoid unnamed nested types?
  833. // it seems we added this and removed it a couple times.
  834. // Disallowing NULL nested types here breaks the "_BUF_SIZE" in String, which is
  835. // an anonymous enum
  836. /*int16 pad = GET(int16);
  837. int32 nestedTypeId = GET(int32);
  838. const char* typeName = CvParseAndDupString(data);
  839. if ((typeName != NULL) && (typeName[0] != '<'))
  840. {
  841. DbgType* nestedType = CvCreateType();
  842. nestedType->mTypeParam = CvGetType(nestedTypeId);
  843. nestedType->mTypeCode = DbgType_TypeDef;
  844. nestedType->mName = typeName;
  845. nestedType->mTypeName = typeName;
  846. nestedType->mParent = parentType;
  847. parentType->mSubTypeList.PushBack(nestedType);
  848. }*/
  849. int16 pad = GET(int16);
  850. int32 nestedTypeId = GET(int32);
  851. const char* typeName = _ParseString();
  852. DbgType* nestedType = CvCreateType();
  853. nestedType->mTypeParam = CvGetType(nestedTypeId);
  854. nestedType->mTypeCode = DbgType_TypeDef;
  855. if ((typeName != NULL) && (typeName[0] != '<'))
  856. {
  857. nestedType->mName = typeName;
  858. nestedType->mTypeName = typeName;
  859. }
  860. nestedType->mParent = parentType;
  861. parentType->mSubTypeList.PushBack(nestedType);
  862. }
  863. break;
  864. case LF_ONEMETHOD:
  865. {
  866. CV_fldattr_t attr = GET(CV_fldattr_t);
  867. CV_typ_t methodTypeId = GET(CV_typ_t);
  868. int virtOffset = -1;
  869. if ((attr.mprop == CV_MTintro) || (attr.mprop == CV_MTpureintro))
  870. virtOffset = GET(int32);
  871. const char* methodName = _ParseString();
  872. DbgSubprogram* subProgram = CvParseMethod(parentType, methodName, methodTypeId, ipi);
  873. subProgram->mVirtual = (attr.mprop == CV_MTintro) || (attr.mprop == CV_MTpureintro) || (attr.mprop == CV_MTvirtual);
  874. subProgram->mVTableLoc = virtOffset;
  875. }
  876. break;
  877. case LF_METHOD:
  878. {
  879. int count = (int)GET(uint16);
  880. int32 methodList = GET(int32);
  881. const char* methodName = _ParseString();
  882. uint8* listData = CvGetTagData(methodList, ipi);
  883. CvAutoReleaseTempData releaseTempData(this, listData);
  884. int16 trLeafType = GET_FROM(listData, int16);
  885. if (trLeafType != LF_METHODLIST)
  886. {
  887. SoftFail("Invalid LF_METHOD member");
  888. return;
  889. }
  890. BF_ASSERT(trLeafType == LF_METHODLIST);
  891. for (int methodIdx = 0; methodIdx < count; methodIdx++)
  892. {
  893. CV_fldattr_t attr = GET_FROM(listData, CV_fldattr_t);
  894. int16 unused = GET_FROM(listData, int16);
  895. CV_typ_t methodTypeId = GET_FROM(listData, CV_typ_t);
  896. int virtOffset = -1;
  897. if ((attr.mprop == CV_MTintro) || (attr.mprop == CV_MTpureintro))
  898. virtOffset = GET_FROM(listData, int32);
  899. DbgSubprogram* subProgram = CvParseMethod(parentType, methodName, methodTypeId, ipi);
  900. subProgram->mVirtual = (attr.mprop == CV_MTintro) || (attr.mprop == CV_MTpureintro) || (attr.mprop == CV_MTvirtual);
  901. subProgram->mVTableLoc = virtOffset;
  902. }
  903. }
  904. break;
  905. case LF_MEMBER:
  906. case LF_STMEMBER:
  907. {
  908. bool isStatic = leafType == LF_STMEMBER;
  909. bool isConst = false;
  910. CV_fldattr_t attr = GET(CV_fldattr_t);
  911. CV_typ_t fieldTypeId = GET(CV_typ_t);
  912. if (parentType->mTagIdx == 29184)
  913. {
  914. NOP;
  915. }
  916. int memberOffset = -1;
  917. if (isStatic)
  918. {
  919. //?
  920. }
  921. else
  922. {
  923. memberOffset = (int)CvParseConstant(data);
  924. }
  925. char* fieldName = (char*)_ParseString();
  926. if ((fieldName != NULL) && (parentType->mLanguage == DbgLanguage_Beef))
  927. {
  928. if (strcmp(fieldName, "$prim") == 0)
  929. {
  930. parentType->mTypeParam = CvGetType(fieldTypeId);
  931. parentType->mTypeParam = GetPrimitiveType(parentType->mTypeParam->mTypeCode, DbgLanguage_Beef);
  932. parentType->mSize = parentType->mTypeParam->mSize;
  933. parentType->mAlign = parentType->mTypeParam->mAlign;
  934. if ((parentType->mBaseTypes.mHead != NULL) && (strcmp(parentType->mBaseTypes.mHead->mBaseType->mName, "System.Enum") == 0))
  935. parentType->mTypeCode = DbgType_Enum;
  936. break;
  937. }
  938. if (strncmp(fieldName, "$using$", 7) == 0)
  939. {
  940. fieldName = NULL;
  941. }
  942. }
  943. int64 constVal = 0;
  944. if ((fieldName != NULL) && (parentType->mLanguage == DbgLanguage_Beef) && (isStatic))
  945. {
  946. for (char* cPtr = fieldName; true; cPtr++)
  947. {
  948. char c = *cPtr;
  949. if (c == 0)
  950. break;
  951. if (c == '$')
  952. {
  953. if (!strMadeCopy)
  954. {
  955. int ofs = cPtr - fieldName;
  956. fieldName = DbgDupString(fieldName, "CvParseMembers.LF_MEMBER");
  957. cPtr = fieldName + ofs;
  958. }
  959. isConst = true;
  960. if (cPtr[1] == '_')
  961. {
  962. cPtr[1] = '-';
  963. constVal = atoll(cPtr + 1);
  964. }
  965. else
  966. constVal = atoll(cPtr + 1);
  967. *cPtr = 0;
  968. break;
  969. }
  970. }
  971. }
  972. if ((isStatic) && (!isConst) && (IsObjectFile()))
  973. {
  974. // Already has statics filled in
  975. break;
  976. }
  977. BP_ALLOC_T(DbgVariable);
  978. DbgVariable* member = mAlloc.Alloc<DbgVariable>();
  979. member->mIsStatic = isStatic;
  980. DbgType* fieldType = CvGetType(fieldTypeId);
  981. // if (fieldType == NULL)
  982. // {
  983. // uint8* fieldTypeData = CvGetTagData(fieldTypeId, ipi);
  984. // CvAutoReleaseTempData releaseTempData(this, fieldTypeData);
  985. //
  986. // // It's a section data ptr
  987. // int16 memberLeafType = *((int16*)fieldTypeData);
  988. // switch (memberLeafType)
  989. // {
  990. // case LF_BITFIELD:
  991. // {
  992. // lfBitfield& bitfield = *(lfBitfield*)fieldTypeData;
  993. // fieldType = CvGetType(bitfield.type);
  994. //
  995. // // Bit offset is expressed in MSB form
  996. // member->mBitOffset = (fieldType->mSize * 8) - bitfield.position - bitfield.length;
  997. // member->mBitSize = bitfield.length;
  998. // }
  999. // break;
  1000. // default:
  1001. // BF_FATAL("Unhandled");
  1002. // }
  1003. // }
  1004. if (fieldType == NULL)
  1005. fieldType = CvGetType(fieldTypeId);
  1006. if ((fieldType->mTypeCode == DbgType_Enum) && (fieldType->GetByteCount() == 0))
  1007. fieldType = fieldType->GetPrimaryType();
  1008. // if (fieldType->mTypeCode == DbgType_Bitfield)
  1009. // {
  1010. // auto bitfieldType = (DbgBitfieldType*)fieldType;
  1011. // member->mBitOffset = bitfieldType->mPosition;
  1012. // member->mBitSize = bitfieldType->mLength;
  1013. // fieldType = fieldType->mTypeParam;
  1014. // }
  1015. if (isConst)
  1016. {
  1017. member->mConstValue = constVal;
  1018. member->mIsConst = true;
  1019. }
  1020. else if (isStatic)
  1021. {
  1022. // NOP
  1023. }
  1024. else
  1025. member->mMemberOffset = memberOffset;
  1026. member->mType = fieldType;
  1027. member->mCompileUnit = parentType->mCompileUnit;
  1028. member->mName = fieldName;
  1029. // Size should already be set, right? It gets set on 'dataSize' in the LF_STRUCT/LF_CLASS
  1030. //parentType->mSize = std::max(memberOffset + fieldType->mSize, parentType->mSize);
  1031. if ((!isStatic) && (member->mMemberOffset >= 0))
  1032. {
  1033. if (!parentType->mIsPacked)
  1034. parentType->mAlign = std::max(parentType->mAlign, fieldType->GetAlign());
  1035. if (!parentType->mSizeCalculated)
  1036. {
  1037. parentType->mSize = std::max(memberOffset + fieldType->GetByteCount(), parentType->mSize);
  1038. }
  1039. }
  1040. //if (parentType->mAlign > 1)
  1041. //parentType->mSize = (parentType->mSize + (parentType->mAlign - 1)) & ~(parentType->mAlign - 1);
  1042. //parentType->mSizeCalculated = true;
  1043. parentType->mMemberList.PushBack(member);
  1044. if (isStatic)
  1045. parentType->mNeedsGlobalsPopulated = true;
  1046. }
  1047. break;
  1048. case LF_INDEX:
  1049. {
  1050. int _pad = (int)GET(uint16);
  1051. int32 indexType = GET(int32);
  1052. CvParseMembers(parentType, indexType, ipi);
  1053. }
  1054. break;
  1055. default:
  1056. SoftFail(StrFormat("Unhandled leaf id 0x%X", leafType));
  1057. return;
  1058. }
  1059. PTR_ALIGN(data, sectionStart, 4);
  1060. }
  1061. }
  1062. break;
  1063. }
  1064. }
  1065. int COFF::CvConvRegNum(int regNum, int* outBits)
  1066. {
  1067. if ((regNum >= CV_AMD64_R8B) && (regNum <= CV_AMD64_R15B))
  1068. {
  1069. if (outBits != NULL)
  1070. *outBits = 8;
  1071. regNum -= CV_AMD64_R8B - CV_AMD64_R8;
  1072. }
  1073. else if ((regNum >= CV_AMD64_R8W) && (regNum <= CV_AMD64_R15W))
  1074. {
  1075. if (outBits != NULL)
  1076. *outBits = 16;
  1077. regNum -= CV_AMD64_R8W - CV_AMD64_R8;
  1078. }
  1079. else if ((regNum >= CV_AMD64_R8D) && (regNum <= CV_AMD64_R15D))
  1080. {
  1081. if (outBits != NULL)
  1082. *outBits = 32;
  1083. regNum -= CV_AMD64_R8D - CV_AMD64_R8;
  1084. }
  1085. switch (regNum)
  1086. {
  1087. #ifdef BF_DBG_64
  1088. case CV_AMD64_RAX: return X64Reg_RAX;
  1089. case CV_AMD64_RDX: return X64Reg_RDX;
  1090. case CV_AMD64_RCX: return X64Reg_RCX;
  1091. case CV_AMD64_RBX: return X64Reg_RBX;
  1092. case CV_AMD64_RSI: return X64Reg_RSI;
  1093. case CV_AMD64_RDI: return X64Reg_RDI;
  1094. case CV_AMD64_RBP: return X64Reg_RBP;
  1095. case CV_AMD64_RSP: return X64Reg_RSP;
  1096. case CV_AMD64_R8: return X64Reg_R8;
  1097. case CV_AMD64_R9: return X64Reg_R9;
  1098. case CV_AMD64_R10: return X64Reg_R10;
  1099. case CV_AMD64_R11: return X64Reg_R11;
  1100. case CV_AMD64_R12: return X64Reg_R12;
  1101. case CV_AMD64_R13: return X64Reg_R13;
  1102. case CV_AMD64_R14: return X64Reg_R14;
  1103. case CV_AMD64_R15: return X64Reg_R15;
  1104. case CV_AMD64_RIP: return X64Reg_RIP;
  1105. case CV_REG_AL: if (outBits != NULL) *outBits = 8; return X64Reg_RAX;
  1106. case CV_REG_AX: if (outBits != NULL) *outBits = 16; return X64Reg_RAX;
  1107. case CV_REG_EAX: if (outBits != NULL) *outBits = 32; return X64Reg_RAX;
  1108. case CV_REG_CL: if (outBits != NULL) *outBits = 8; return X64Reg_RCX;
  1109. case CV_REG_CX: if (outBits != NULL) *outBits = 16; return X64Reg_RCX;
  1110. case CV_REG_ECX: if (outBits != NULL) *outBits = 32; return X64Reg_RCX;
  1111. case CV_REG_DL: if (outBits != NULL) *outBits = 8; return X64Reg_RDX;
  1112. case CV_REG_DX: if (outBits != NULL) *outBits = 16; return X64Reg_RDX;
  1113. case CV_REG_EDX: if (outBits != NULL) *outBits = 32; return X64Reg_RDX;
  1114. case CV_REG_BL: if (outBits != NULL) *outBits = 8; return X64Reg_RBX;
  1115. case CV_REG_BX: if (outBits != NULL) *outBits = 16; return X64Reg_RBX;
  1116. case CV_REG_EBX: if (outBits != NULL) *outBits = 32; return X64Reg_RBX;
  1117. case CV_REG_ESP: if (outBits != NULL) *outBits = 32; return X64Reg_RSP;
  1118. case CV_REG_EBP: if (outBits != NULL) *outBits = 32; return X64Reg_RBP;
  1119. case CV_AMD64_SIL: if (outBits != NULL) *outBits = 8; return X64Reg_RSI;
  1120. case CV_REG_SI: if (outBits != NULL) *outBits = 16; return X64Reg_RSI;
  1121. case CV_REG_ESI: if (outBits != NULL) *outBits = 32; return X64Reg_RSI;
  1122. case CV_AMD64_DIL: if (outBits != NULL) *outBits = 8; return X64Reg_RDI;
  1123. case CV_REG_DI: if (outBits != NULL) *outBits = 16; return X64Reg_RDI;
  1124. case CV_REG_EDI: if (outBits != NULL) *outBits = 32; return X64Reg_RDI;
  1125. case CV_AMD64_R8B: *outBits = 8; return X64Reg_R8;
  1126. case CV_AMD64_R8W: *outBits = 16; return X64Reg_R8;
  1127. case CV_AMD64_R8D: *outBits = 32; return X64Reg_R8;
  1128. case CV_AMD64_R9B: *outBits = 8; return X64Reg_R9;
  1129. case CV_AMD64_R9W: *outBits = 16; return X64Reg_R9;
  1130. case CV_AMD64_R9D: *outBits = 32; return X64Reg_R9;
  1131. case CV_AMD64_R10B: *outBits = 8; return X64Reg_R10;
  1132. case CV_AMD64_R10W: *outBits = 16; return X64Reg_R10;
  1133. case CV_AMD64_R10D: *outBits = 32; return X64Reg_R10;
  1134. case CV_AMD64_R11B: *outBits = 8; return X64Reg_R11;
  1135. case CV_AMD64_R11W: *outBits = 16; return X64Reg_R11;
  1136. case CV_AMD64_R11D: *outBits = 32; return X64Reg_R11;
  1137. case CV_AMD64_R12B: *outBits = 8; return X64Reg_R12;
  1138. case CV_AMD64_R12W: *outBits = 16; return X64Reg_R12;
  1139. case CV_AMD64_R12D: *outBits = 32; return X64Reg_R12;
  1140. case CV_AMD64_R13B: *outBits = 8; return X64Reg_R13;
  1141. case CV_AMD64_R13W: *outBits = 16; return X64Reg_R13;
  1142. case CV_AMD64_R13D: *outBits = 32; return X64Reg_R13;
  1143. case CV_AMD64_R14B: *outBits = 8; return X64Reg_R14;
  1144. case CV_AMD64_R14W: *outBits = 16; return X64Reg_R14;
  1145. case CV_AMD64_R14D: *outBits = 32; return X64Reg_R14;
  1146. case CV_AMD64_R15B: *outBits = 8; return X64Reg_R15;
  1147. case CV_AMD64_R15W: *outBits = 16; return X64Reg_R15;
  1148. case CV_AMD64_R15D: *outBits = 32; return X64Reg_R15;
  1149. case CV_AMD64_XMM0_0: return X64Reg_XMM00;
  1150. case CV_AMD64_XMM0_1: return X64Reg_XMM01;
  1151. case CV_AMD64_XMM0_2: return X64Reg_XMM02;
  1152. case CV_AMD64_XMM0_3: return X64Reg_XMM03;
  1153. case CV_AMD64_XMM1_0: return X64Reg_XMM10;
  1154. case CV_AMD64_XMM1_1: return X64Reg_XMM11;
  1155. case CV_AMD64_XMM1_2: return X64Reg_XMM12;
  1156. case CV_AMD64_XMM1_3: return X64Reg_XMM13;
  1157. case CV_AMD64_XMM2_0: return X64Reg_XMM20;
  1158. case CV_AMD64_XMM2_1: return X64Reg_XMM21;
  1159. case CV_AMD64_XMM2_2: return X64Reg_XMM22;
  1160. case CV_AMD64_XMM2_3: return X64Reg_XMM23;
  1161. case CV_AMD64_XMM3_0: return X64Reg_XMM30;
  1162. case CV_AMD64_XMM3_1: return X64Reg_XMM31;
  1163. case CV_AMD64_XMM3_2: return X64Reg_XMM32;
  1164. case CV_AMD64_XMM3_3: return X64Reg_XMM33;
  1165. case CV_AMD64_XMM4_0: return X64Reg_XMM40;
  1166. case CV_AMD64_XMM4_1: return X64Reg_XMM41;
  1167. case CV_AMD64_XMM4_2: return X64Reg_XMM42;
  1168. case CV_AMD64_XMM4_3: return X64Reg_XMM43;
  1169. case CV_AMD64_XMM5_0: return X64Reg_XMM50;
  1170. case CV_AMD64_XMM5_1: return X64Reg_XMM51;
  1171. case CV_AMD64_XMM5_2: return X64Reg_XMM52;
  1172. case CV_AMD64_XMM5_3: return X64Reg_XMM53;
  1173. case CV_AMD64_XMM6_0: return X64Reg_XMM60;
  1174. case CV_AMD64_XMM6_1: return X64Reg_XMM61;
  1175. case CV_AMD64_XMM6_2: return X64Reg_XMM62;
  1176. case CV_AMD64_XMM6_3: return X64Reg_XMM63;
  1177. case CV_AMD64_XMM7_0: return X64Reg_XMM70;
  1178. case CV_AMD64_XMM7_1: return X64Reg_XMM71;
  1179. case CV_AMD64_XMM7_2: return X64Reg_XMM72;
  1180. case CV_AMD64_XMM7_3: return X64Reg_XMM73;
  1181. case CV_AMD64_XMM8_0: return X64Reg_XMM80;
  1182. case CV_AMD64_XMM8_1: return X64Reg_XMM81;
  1183. case CV_AMD64_XMM8_2: return X64Reg_XMM82;
  1184. case CV_AMD64_XMM8_3: return X64Reg_XMM83;
  1185. case CV_AMD64_XMM9_0: return X64Reg_XMM90;
  1186. case CV_AMD64_XMM9_1: return X64Reg_XMM91;
  1187. case CV_AMD64_XMM9_2: return X64Reg_XMM92;
  1188. case CV_AMD64_XMM9_3: return X64Reg_XMM93;
  1189. case CV_AMD64_XMM10_0: return X64Reg_XMM10_0;
  1190. case CV_AMD64_XMM10_1: return X64Reg_XMM10_1;
  1191. case CV_AMD64_XMM10_2: return X64Reg_XMM10_2;
  1192. case CV_AMD64_XMM10_3: return X64Reg_XMM10_3;
  1193. case CV_AMD64_XMM11_0: return X64Reg_XMM11_0;
  1194. case CV_AMD64_XMM11_1: return X64Reg_XMM11_1;
  1195. case CV_AMD64_XMM11_2: return X64Reg_XMM11_2;
  1196. case CV_AMD64_XMM11_3: return X64Reg_XMM11_3;
  1197. case CV_AMD64_XMM12_0: return X64Reg_XMM12_0;
  1198. case CV_AMD64_XMM12_1: return X64Reg_XMM12_1;
  1199. case CV_AMD64_XMM12_2: return X64Reg_XMM12_2;
  1200. case CV_AMD64_XMM12_3: return X64Reg_XMM12_3;
  1201. case CV_AMD64_XMM13_0: return X64Reg_XMM13_0;
  1202. case CV_AMD64_XMM13_1: return X64Reg_XMM13_1;
  1203. case CV_AMD64_XMM13_2: return X64Reg_XMM13_2;
  1204. case CV_AMD64_XMM13_3: return X64Reg_XMM13_3;
  1205. case CV_AMD64_XMM14_0: return X64Reg_XMM14_0;
  1206. case CV_AMD64_XMM14_1: return X64Reg_XMM14_1;
  1207. case CV_AMD64_XMM14_2: return X64Reg_XMM14_2;
  1208. case CV_AMD64_XMM14_3: return X64Reg_XMM14_3;
  1209. case CV_AMD64_XMM15_0: return X64Reg_XMM15_0;
  1210. case CV_AMD64_XMM15_1: return X64Reg_XMM15_1;
  1211. case CV_AMD64_XMM15_2: return X64Reg_XMM15_2;
  1212. case CV_AMD64_XMM15_3: return X64Reg_XMM15_3;
  1213. #else
  1214. case CV_REG_AL: return X86Reg_EAX;
  1215. case CV_REG_CL: return X86Reg_ECX;
  1216. case CV_REG_DL: return X86Reg_EDX;
  1217. case CV_REG_BL: return X86Reg_EBX;
  1218. case CV_REG_AH: return X86Reg_EAX;
  1219. case CV_REG_AX: return X86Reg_EAX;
  1220. case CV_REG_CX: return X86Reg_ECX;
  1221. case CV_REG_DX: return X86Reg_EDX;
  1222. case CV_REG_BX: return X86Reg_EBX;
  1223. case CV_REG_SP: return X86Reg_ESP;
  1224. case CV_REG_BP: return X86Reg_EBP;
  1225. case CV_REG_SI: return X86Reg_ESI;
  1226. case CV_REG_DI: return X86Reg_EDI;
  1227. case CV_REG_EAX: return X86Reg_EAX;
  1228. case CV_REG_ECX: return X86Reg_ECX;
  1229. case CV_REG_EDX: return X86Reg_EDX;
  1230. case CV_REG_EBX: return X86Reg_EBX;
  1231. case CV_REG_ESP: return X86Reg_ESP;
  1232. case CV_REG_EBP: return X86Reg_EBP;
  1233. case CV_REG_ESI: return X86Reg_ESI;
  1234. case CV_REG_EDI: return X86Reg_EDI;
  1235. case CV_REG_IP: return X86Reg_EIP;
  1236. // case CV_REG_XMM0: return X86Reg_XMM00;
  1237. // case CV_REG_XMM1: return X86Reg_XMM01;
  1238. // case CV_REG_XMM2: return X86Reg_XMM02;
  1239. // case CV_REG_XMM3: return X86Reg_XMM03;
  1240. // case CV_REG_XMM4: return X86Reg_XMM04;
  1241. // case CV_REG_XMM5: return X86Reg_XMM05;
  1242. // case CV_REG_XMM6: return X86Reg_XMM06;
  1243. // case CV_REG_XMM7: return X86Reg_XMM07;
  1244. #endif
  1245. }
  1246. return 0; // Nope
  1247. BF_FATAL("Invalid register");
  1248. return 0;
  1249. }
  1250. DbgType* COFF::CvCreateType()
  1251. {
  1252. DbgModule* linkedModule = GetLinkedModule();
  1253. BP_ALLOC_T(DbgType);
  1254. DbgType* dbgType = mAlloc.Alloc<DbgType>();
  1255. dbgType->mCompileUnit = mMasterCompileUnit;
  1256. dbgType->mTypeIdx = (int)linkedModule->mTypes.size();
  1257. linkedModule->mTypes.push_back(dbgType);
  1258. return dbgType;
  1259. }
  1260. DbgType* COFF::CvParseType(int tagIdx, bool ipi)
  1261. {
  1262. auto& reader = ipi ? mCvIPIReader : mCvTypeSectionReader;
  1263. int offset = ipi ? mCvIPITagStartMap[tagIdx - mCvIPIMinTag] : mCvTagStartMap[tagIdx - mCvMinTag];
  1264. uint8* data = reader.GetTempPtr(offset, 4);
  1265. uint16 trLength = *(uint16*)data;
  1266. offset += 2;
  1267. data = reader.GetTempPtr(offset, trLength, true);
  1268. CvAutoReleaseTempData releaseTempData(this, data);
  1269. uint8* dataStart = data;
  1270. uint8* dataEnd = data + trLength;
  1271. int16 trLeafType = GET(int16);
  1272. bool strMadeCopy;
  1273. auto _ParseString = [&]()
  1274. {
  1275. strMadeCopy = false;
  1276. int nameLen = strlen((const char*)data);
  1277. int nameOfs = data - dataStart;
  1278. const char* name = (const char*)reader.GetPermanentPtr(offset + nameOfs, nameLen + 1, &strMadeCopy);
  1279. data += nameLen + 1;
  1280. return name;
  1281. };
  1282. DbgType* dbgType = NULL;
  1283. switch (trLeafType)
  1284. {
  1285. case LF_ENUM:
  1286. {
  1287. int elemCount = (int)GET(uint16);
  1288. int prop = (int)GET(uint16);
  1289. int underlyingType = GET(int32);
  1290. int typeIndex = GET(int32);
  1291. const char* name = _ParseString();
  1292. dbgType = CvCreateType();
  1293. dbgType->mCompileUnit = mMasterCompileUnit;
  1294. dbgType->mName = name;
  1295. dbgType->mTypeName = name;
  1296. //SplitName(dbgType->mName, dbgType->mTypeName, dbgType->mTemplateParams);
  1297. dbgType->mTypeCode = DbgType_Enum;
  1298. dbgType->mTypeParam = CvGetTypeSafe(underlyingType);
  1299. dbgType->mIsIncomplete = true;
  1300. if (dbgType->mTypeParam->GetByteCount() == 0)
  1301. dbgType->mIsDeclaration = true;
  1302. dbgType->mSize = dbgType->mTypeParam->mSize;
  1303. dbgType->mAlign = dbgType->mTypeParam->mAlign;
  1304. /*if (dbgType->mTypeParam->GetByteCount() == 0)
  1305. {
  1306. // Appears to be a bug where byte-sized enums come out as void (sometimes?)
  1307. dbgType->mTypeParam = mBfPrimitiveTypes[DbgType_u8];
  1308. }*/
  1309. //dbgType->mIsDeclaration = typeIndex == 0;
  1310. //CvParseMembers(dbgType, typeIndex, sectionData);
  1311. }
  1312. break;
  1313. case LF_VTSHAPE:
  1314. {
  1315. lfVTShape& vtShape = *(lfVTShape*)dataStart;
  1316. dbgType = CvCreateType();
  1317. dbgType->mTypeCode = DbgType_VTable;
  1318. dbgType->mSize = vtShape.count * sizeof(addr_target);
  1319. dbgType->mSizeCalculated = true;
  1320. }
  1321. break;
  1322. case LF_BITFIELD:
  1323. {
  1324. //DbgType
  1325. lfBitfield& bitfield = *(lfBitfield*)dataStart;
  1326. //dbgType = CvCreateType();
  1327. DbgBitfieldType* bitfieldType = mAlloc.Alloc<DbgBitfieldType>();
  1328. dbgType = bitfieldType;
  1329. dbgType->mTypeParam = CvGetTypeSafe(bitfield.type);
  1330. dbgType->mTypeCode = DbgType_Bitfield;
  1331. //bitfieldType->mPosition = (dbgType->mTypeParam->mSize * 8) - bitfield.position - bitfield.length;
  1332. bitfieldType->mPosition = bitfield.position;
  1333. bitfieldType->mLength = bitfield.length;
  1334. bitfieldType->mSize = dbgType->mTypeParam->mSize;
  1335. bitfieldType->mAlign = dbgType->mTypeParam->mAlign;
  1336. dbgType->mSizeCalculated = true;
  1337. dbgType->mPriority = DbgTypePriority_Unique;
  1338. }
  1339. break;
  1340. case LF_CLASS:
  1341. case LF_STRUCTURE:
  1342. case LF_CLASS_EX:
  1343. case LF_STRUCTURE_EX:
  1344. {
  1345. unsigned short count; // count of number of elements in class
  1346. CV_prop_t property; // property attribute field (prop_t)
  1347. CV_typ_t field; // type index of LF_FIELD descriptor list
  1348. CV_typ_t derived; // type index of derived from list if not zero
  1349. CV_typ_t vshape; // type index of vshape table for this class
  1350. int dataSize;
  1351. int16 extra = 0;
  1352. if ((trLeafType == 0x1608) || (trLeafType == 0x1609))
  1353. {
  1354. property = GET(CV_prop_t);
  1355. extra = GET(int16);
  1356. field = GET(CV_typ_t);
  1357. derived = GET(CV_typ_t);
  1358. vshape = GET(CV_typ_t);
  1359. count = GET(unsigned short);
  1360. dataSize = (int)CvParseConstant(data);
  1361. }
  1362. else
  1363. {
  1364. count = GET(unsigned short);
  1365. property = GET(CV_prop_t);
  1366. field = GET(CV_typ_t);
  1367. derived = GET(CV_typ_t);
  1368. vshape = GET(CV_typ_t);
  1369. dataSize = (int)CvParseConstant(data);
  1370. }
  1371. const char* name = _ParseString();
  1372. // if (strstr(name, "TestStruct") != NULL)
  1373. // {
  1374. // NOP;
  1375. // }
  1376. // if ((strstr(name, "`") != NULL) || (strstr(name, "::__l") != NULL))
  1377. // {
  1378. // OutputDebugStrF("Local type: %s\n", name);
  1379. // }
  1380. // Remove "enum " from type names
  1381. bool isPartialDef = false;
  1382. for (int i = 0; true; i++)
  1383. {
  1384. char c = name[i];
  1385. if (c == 0)
  1386. break;
  1387. if (c == '$')
  1388. {
  1389. if ((i >= 5) && (strncmp(&name[i - 5], "$part$", 6) == 0))
  1390. {
  1391. if (!strMadeCopy)
  1392. name = DbgDupString(name, "CvParseType.LF_CLASS");
  1393. strcpy((char*)&name[i - 5], &name[i + 1]);
  1394. if (name[i - 5] == '\0')
  1395. {
  1396. isPartialDef = true;
  1397. break;
  1398. }
  1399. }
  1400. }
  1401. if ((c == ' ') && (i >= 5))
  1402. {
  1403. if ((name[i - 4] == 'e') &&
  1404. (name[i - 3] == 'n') &&
  1405. (name[i - 2] == 'u') &&
  1406. (name[i - 1] == 'm'))
  1407. {
  1408. char prevC = name[i - 5];
  1409. if ((!isalnum(prevC)) && (prevC != '_'))
  1410. {
  1411. if (!strMadeCopy)
  1412. name = DbgDupString(name, "CvParseType.LF_CLASS");
  1413. memmove((char*)name + i - 4, name + i + 1, strlen(name + i + 1) + 1);
  1414. i -= 5;
  1415. continue;
  1416. }
  1417. }
  1418. }
  1419. }
  1420. dbgType = CvCreateType();
  1421. dbgType->mCompileUnit = mMasterCompileUnit;
  1422. dbgType->mIsDeclaration = property.fwdref;
  1423. if (isPartialDef)
  1424. dbgType->mIsDeclaration = true;
  1425. dbgType->mName = name;
  1426. dbgType->mTypeName = name;
  1427. //SplitName(dbgType->mName, dbgType->mTypeName, dbgType->mTemplateParams);
  1428. if ((trLeafType == LF_CLASS) || (trLeafType == LF_CLASS_EX))
  1429. dbgType->mTypeCode = DbgType_Class;
  1430. else
  1431. dbgType->mTypeCode = DbgType_Struct;
  1432. DbgType* baseType = NULL;
  1433. if (derived != 0)
  1434. {
  1435. baseType = CvGetTypeSafe(derived);
  1436. BP_ALLOC_T(DbgBaseTypeEntry);
  1437. DbgBaseTypeEntry* baseTypeEntry = mAlloc.Alloc<DbgBaseTypeEntry>();
  1438. baseTypeEntry->mBaseType = baseType;
  1439. dbgType->mBaseTypes.PushBack(baseTypeEntry);
  1440. }
  1441. if (property.packed)
  1442. {
  1443. dbgType->mAlign = 1;
  1444. dbgType->mIsPacked = true;
  1445. }
  1446. if (!dbgType->mIsDeclaration)
  1447. {
  1448. dbgType->mSizeCalculated = true;
  1449. dbgType->mSize = dataSize;
  1450. if (strncmp(name, "_bf:", 4) == 0)
  1451. {
  1452. // Beef types have different strides from size. Note we MUST set size to zero here, because there are
  1453. // some BF_MAX calculations we perform on it
  1454. dbgType->mSizeCalculated = false;
  1455. dbgType->mSize = 0;
  1456. }
  1457. }
  1458. if (vshape != 0)
  1459. {
  1460. CvGetTypeSafe(vshape);
  1461. dbgType->mHasVTable = true;
  1462. }
  1463. dbgType->mIsIncomplete = true;
  1464. // if (classInfo.field != 0)
  1465. // dbgType->mDefinedMembersSize = CvGetTagSize(classInfo.field, ipi);
  1466. //CvParseMembers(dbgType, classInfo.field, sectionData);
  1467. }
  1468. break;
  1469. case LF_UNION:
  1470. {
  1471. lfUnion& classInfo = *(lfUnion*)dataStart;
  1472. data = (uint8*)&classInfo.data;
  1473. int dataSize = (int)CvParseConstant(data);
  1474. const char* name = _ParseString();
  1475. dbgType = CvCreateType();
  1476. dbgType->mCompileUnit = mMasterCompileUnit;
  1477. dbgType->mIsDeclaration = classInfo.property.fwdref;
  1478. dbgType->mName = name;
  1479. dbgType->mTypeName = name;
  1480. //SplitName(dbgType->mName, dbgType->mTypeName, dbgType->mTemplateParams);
  1481. dbgType->mTypeCode = DbgType_Union;
  1482. dbgType->mSize = dataSize;
  1483. dbgType->mAlign = 1;
  1484. dbgType->mSizeCalculated = !dbgType->mIsDeclaration;
  1485. dbgType->mIsIncomplete = true;
  1486. //CvParseMembers(dbgType, classInfo.field, sectionData);
  1487. }
  1488. break;
  1489. case LF_MODIFIER:
  1490. {
  1491. lfModifier& modifier = *(lfModifier*)dataStart;
  1492. DbgType* outerType = CvGetTypeSafe(modifier.type);
  1493. dbgType = outerType;
  1494. if (modifier.attr.MOD_const)
  1495. {
  1496. DbgType* innerType = dbgType;
  1497. dbgType = CvCreateType();
  1498. dbgType->mTypeParam = innerType;
  1499. dbgType->mTypeCode = DbgType_Const;
  1500. dbgType->mLanguage = innerType->mLanguage;
  1501. }
  1502. if (modifier.attr.MOD_volatile)
  1503. {
  1504. DbgType* innerType = dbgType;
  1505. dbgType = CvCreateType();
  1506. dbgType->mTypeParam = innerType;
  1507. dbgType->mTypeCode = DbgType_Volatile;
  1508. dbgType->mLanguage = innerType->mLanguage;
  1509. }
  1510. if (modifier.attr.MOD_unaligned)
  1511. {
  1512. DbgType* innerType = dbgType;
  1513. dbgType = CvCreateType();
  1514. dbgType->mTypeParam = innerType;
  1515. dbgType->mTypeCode = DbgType_Unaligned;
  1516. dbgType->mLanguage = innerType->mLanguage;
  1517. }
  1518. }
  1519. break;
  1520. case LF_POINTER:
  1521. {
  1522. lfPointer* pointerInfo = (lfPointer*)dataStart;
  1523. dbgType = CvCreateType();
  1524. dbgType->mTypeParam = CvGetTypeSafe(pointerInfo->utype);
  1525. if (pointerInfo->attr.ptrmode == CV_PTR_MODE_RVREF)
  1526. dbgType->mTypeCode = DbgType_RValueReference;
  1527. else if (pointerInfo->attr.ptrmode == CV_PTR_MODE_LVREF)
  1528. dbgType->mTypeCode = DbgType_Ref;
  1529. else
  1530. {
  1531. dbgType->mTypeCode = DbgType_Ptr;
  1532. if ((dbgType->mTypeParam != NULL) && (dbgType->mTypeParam->mPtrType == NULL))
  1533. dbgType->mTypeParam->mPtrType = dbgType;
  1534. }
  1535. //dbgType->mSize = pointerInfo->attr.size;
  1536. dbgType->mSize = sizeof(addr_target);
  1537. dbgType->mAlign = dbgType->mSize;
  1538. dbgType->mSizeCalculated = true;
  1539. dbgType->mLanguage = dbgType->mTypeParam->mLanguage;
  1540. /*if (prop.isconst)
  1541. {
  1542. DbgType* innerType = dbgType;
  1543. dbgType = CvCreateType();
  1544. dbgType->mTypeParam = innerType;
  1545. dbgType->mTypeCode = DbgType_Const;
  1546. dbgType->mSize = innerType->mSize;
  1547. }*/
  1548. //TODO: Handle const, volatile, ref, restrict, etc
  1549. }
  1550. break;
  1551. case LF_DIMARRAY:
  1552. {
  1553. }
  1554. break;
  1555. case LF_ARRAY:
  1556. {
  1557. lfArray* array = (lfArray*)dataStart;
  1558. DbgType* indexType = CvGetTypeSafe(array->idxtype);
  1559. dbgType = CvCreateType();
  1560. dbgType->mTypeParam = CvGetTypeSafe(array->elemtype);
  1561. dbgType->mTypeCode = DbgType_SizedArray;
  1562. dbgType->mLanguage = dbgType->mTypeParam->mLanguage;
  1563. data = (uint8*)&array->data;
  1564. int size = (int)CvParseConstant(data);
  1565. const char* name = _ParseString();
  1566. dbgType->mName = name;
  1567. dbgType->mSize = size;
  1568. dbgType->mAlign = dbgType->mTypeParam->GetAlign();
  1569. // Beef arrays do not necessarily have aligned sizes
  1570. dbgType->mSizeCalculated = false;
  1571. dbgType->mLanguage = dbgType->mTypeParam->mLanguage;
  1572. }
  1573. break;
  1574. case LF_PROCEDURE:
  1575. {
  1576. dbgType = CvCreateType();
  1577. //TODO: Calling convetion, etc
  1578. lfProc* proc = (lfProc*)dataStart;
  1579. dbgType->mTypeCode = DbgType_Subroutine;
  1580. dbgType->mTypeParam = CvGetTypeSafe(proc->rvtype);
  1581. BP_ALLOC_T(DbgBlock);
  1582. dbgType->mBlockParam = mAlloc.Alloc<DbgBlock>();
  1583. uint8* argData = CvGetTagData(proc->arglist, ipi);
  1584. CvAutoReleaseTempData releaseTempData(this, argData);
  1585. int16 argLeafType = GET_FROM(argData, int16);
  1586. BF_ASSERT(argLeafType == LF_ARGLIST);
  1587. int argCount = GET_FROM(argData, int32);
  1588. CV_typ_t* argTypes = (CV_typ_t*)argData;
  1589. for (int paramIdx = 0; paramIdx < proc->parmcount; paramIdx++)
  1590. {
  1591. BP_ALLOC_T(DbgVariable);
  1592. DbgVariable* arg = mAlloc.Alloc<DbgVariable>();
  1593. arg->mIsParam = true;
  1594. arg->mType = CvGetTypeSafe(argTypes[paramIdx]);
  1595. arg->mName = "$arg";
  1596. dbgType->mBlockParam->mVariables.PushBack(arg);
  1597. }
  1598. }
  1599. break;
  1600. case LF_MFUNCTION:
  1601. {
  1602. dbgType = CvCreateType();
  1603. //TODO: Calling convetion, etc
  1604. lfMFunc* proc = (lfMFunc*)dataStart;
  1605. dbgType->mTypeCode = DbgType_Subroutine;
  1606. dbgType->mTypeParam = CvGetTypeSafe(proc->rvtype);
  1607. BP_ALLOC_T(DbgBlock);
  1608. dbgType->mBlockParam = mAlloc.Alloc<DbgBlock>();
  1609. uint8* argData = CvGetTagData(proc->arglist, ipi);
  1610. CvAutoReleaseTempData releaseTempData(this, argData);
  1611. int16 argLeafType = GET_FROM(argData, int16);
  1612. BF_ASSERT(argLeafType == LF_ARGLIST);
  1613. int argCount = GET_FROM(argData, int32);
  1614. CV_typ_t* argTypes = (CV_typ_t*)argData;
  1615. for (int paramIdx = 0; paramIdx < proc->parmcount; paramIdx++)
  1616. {
  1617. BP_ALLOC_T(DbgVariable);
  1618. DbgVariable* arg = mAlloc.Alloc<DbgVariable>();
  1619. arg->mIsParam = true;
  1620. arg->mType = CvGetTypeSafe(argTypes[paramIdx]);
  1621. arg->mName = "$arg";
  1622. dbgType->mBlockParam->mVariables.PushBack(arg);
  1623. }
  1624. }
  1625. break;
  1626. // case 0x1609:
  1627. // NOP;
  1628. // break;
  1629. default:
  1630. NOP;
  1631. break;
  1632. }
  1633. if (dbgType != NULL)
  1634. {
  1635. if (tagIdx == 14709)
  1636. {
  1637. NOP;
  1638. }
  1639. dbgType->mTagIdx = tagIdx;
  1640. int wantIdx = tagIdx - mCvMinTag;
  1641. mCvTypeMap[wantIdx] = dbgType;
  1642. }
  1643. return dbgType;
  1644. }
  1645. void COFF::ParseTypeData(CvStreamReader& reader, int dataOffset)
  1646. {
  1647. BP_ZONE("COFF::ParseTypeData");
  1648. //uint8* sectionEnd = sectionData + sectionSize;
  1649. //uint8* data = sectionData;
  1650. if (mCvMinTag == -1)
  1651. mCvMinTag = 0x1000;
  1652. int offset = dataOffset;
  1653. bool isTagMapEmpty = mCvTagStartMap.empty();
  1654. for (int tagIdx = mCvMinTag; true; tagIdx++)
  1655. {
  1656. if (tagIdx == mCvMaxTag)
  1657. break;
  1658. if (offset >= reader.mSize)
  1659. break;
  1660. //OutputDebugStrF("%X %X\n", tagIdx, (int)(data - sectionData));
  1661. BF_ASSERT(((offset) & 3) == 0);
  1662. //PTR_ALIGN(data, sectionData, 4);
  1663. uint8* data = reader.GetTempPtr(offset, 4);
  1664. uint16 trLength = *(uint16*)data;
  1665. uint16 trLeafType = *(uint16*)(data + 2);
  1666. // uint16 trLength = GET(uint16);
  1667. // uint8* dataStart = data;
  1668. // uint16 trLeafType = GET(uint16);
  1669. // uint8* dataEnd = dataStart + trLength;
  1670. if (isTagMapEmpty)
  1671. {
  1672. mCvTagStartMap.push_back(offset);
  1673. mCvTypeMap.push_back(NULL);
  1674. }
  1675. else
  1676. mCvTagStartMap[tagIdx - mCvMinTag] = offset;
  1677. offset += trLength + 2;
  1678. DbgType* dbgType = NULL;
  1679. if (trLeafType == 0)
  1680. continue;
  1681. if ((trLeafType == LF_ENUM) || (trLeafType == LF_CLASS) || (trLeafType == LF_STRUCTURE) || (trLeafType == LF_UNION) ||
  1682. (trLeafType == LF_CLASS_EX) || (trLeafType == LF_STRUCTURE_EX))
  1683. {
  1684. CvParseType(tagIdx);
  1685. }
  1686. }
  1687. if (isTagMapEmpty)
  1688. mCvTagStartMap.Add(offset);
  1689. }
  1690. void COFF::ParseTypeData(int sectionNum, CvStreamReader& reader, int& sectionSize, int& dataOfs, int& hashStream, int& hashAdjOffset, int& hashAdjSize, int& minVal, int& maxVal)
  1691. {
  1692. BP_ZONE("COFF::ParseTypeData");
  1693. sectionSize = 0;
  1694. // sectionData = CvReadStream(sectionNum, &sectionSize);
  1695. // if (sectionData == NULL)
  1696. // return;
  1697. // uint8* data = sectionData;
  1698. CvReadStream(sectionNum, reader);
  1699. uint8* data = reader.GetTempPtr(0, 0);
  1700. uint8* sectionData = data;
  1701. int32 ver = GET(int32);
  1702. int32 headerSize = GET(int32);
  1703. minVal = GET(int32);
  1704. maxVal = GET(int32);
  1705. int32 followSize = GET(int32);
  1706. hashStream = GET(int16);
  1707. int16 hashStreamPadding = GET(int16); // -1
  1708. int32 hashKeySize = GET(int32); // 4
  1709. int32 hashBucketsSize = GET(int32); // 0x3ffff
  1710. int32 hashValsOffset = GET(int32); // 0
  1711. int32 hashValsSize = GET(int32);
  1712. int32 hashTypeIndexOffset = GET(int32);
  1713. int32 hashTypeIndexSize = GET(int32);
  1714. hashAdjOffset = GET(int32);
  1715. hashAdjSize = GET(int32);
  1716. dataOfs = 14 * 4;
  1717. uint8* typeDataHead = data;
  1718. bool validate = false;
  1719. // Validate hash info - not necessary, just for debugging
  1720. if (validate)
  1721. {
  1722. BF_FATAL("Fix validate stuff to work with new CvReader");
  1723. int hashStreamSize = 0;
  1724. uint8* hashDataHead = CvReadStream(hashStream, &hashStreamSize);
  1725. int tagId = minVal;
  1726. //////// Validate Type indices
  1727. int typeIndexCount = hashTypeIndexSize / 8;
  1728. // We expect a TypeIndexOffset for every 8k of data, plus the zero at the start
  1729. int expectedTypeIndices = (sectionSize - (int)(data - sectionData)) / 8192 + 1;
  1730. // Assert it's in some acceptable range...
  1731. BF_ASSERT((typeIndexCount > expectedTypeIndices / 2) && (typeIndexCount < expectedTypeIndices * 2));
  1732. for (int entryIdx = 0; entryIdx < typeIndexCount; entryIdx++)
  1733. {
  1734. data = hashDataHead + hashTypeIndexOffset + entryIdx * 8;
  1735. GET_INTO(int, typeIdx);
  1736. GET_INTO(int, ofs);
  1737. uint8* dataEnd;
  1738. int endTagId;
  1739. if (entryIdx < typeIndexCount - 1)
  1740. {
  1741. endTagId = GET(int);
  1742. int endOfs = GET(int);
  1743. dataEnd = typeDataHead + endOfs;
  1744. }
  1745. else
  1746. {
  1747. endTagId = maxVal;
  1748. dataEnd = sectionData + sectionSize;
  1749. }
  1750. data = typeDataHead + ofs;
  1751. while (data < dataEnd)
  1752. {
  1753. int tagSize = GET(uint16);
  1754. data += tagSize;
  1755. tagId++;
  1756. }
  1757. BF_ASSERT(data == dataEnd);
  1758. BF_ASSERT(tagId == endTagId);
  1759. }
  1760. BF_ASSERT(tagId == maxVal);
  1761. ////// Validate hashes
  1762. int hashCount = hashValsSize / 4;
  1763. BF_ASSERT(hashCount == maxVal - minVal);
  1764. tagId = minVal;
  1765. int32* hashVals = (int32*)(hashDataHead + hashValsOffset);
  1766. data = typeDataHead;
  1767. Array<int> tagStarts;
  1768. tagStarts.Reserve(maxVal - minVal);
  1769. for (int hashIdx = 0; hashIdx < hashCount; hashIdx++)
  1770. {
  1771. int32 expectHashVal = hashVals[hashIdx];
  1772. PTR_ALIGN(data, sectionData, 4);
  1773. tagStarts.push_back((int)(data - sectionData));
  1774. int32 hashVal = BlHash::GetTypeHash(data) % hashBucketsSize;
  1775. BF_ASSERT(hashVal == expectHashVal);
  1776. tagId++;
  1777. }
  1778. //////// Validate hash adjusters
  1779. if (hashAdjSize > 0)
  1780. {
  1781. data = hashDataHead + hashAdjOffset;
  1782. uint8* dataEnd = data + hashAdjSize;
  1783. GET_INTO(int32, adjustCount);
  1784. GET_INTO(int32, adjustCapacity);
  1785. GET_INTO(int32, usedLen);
  1786. uint32* usedBitsArr = (uint32*)data;
  1787. data += usedLen * sizeof(int32);
  1788. GET_INTO(int32, deletedLen);
  1789. uint32* deletedBitsArr = (uint32*)data;
  1790. data += deletedLen * sizeof(int32);
  1791. int curAdjIdx = 0;
  1792. for (int usedIdx = 0; usedIdx < usedLen; usedIdx++)
  1793. {
  1794. int usedBits = usedBitsArr[usedIdx];
  1795. for (int i = 0; i < 32; i++)
  1796. {
  1797. if ((usedBits & (1 << i)) != 0)
  1798. {
  1799. GET_INTO(int32, adjVal);
  1800. GET_INTO(CV_typ_t, typeId);
  1801. uint8* typeData = sectionData + tagStarts[typeId - minVal];
  1802. int32 hashVal = BlHash::GetTypeHash(typeData) % hashBucketsSize;
  1803. const char* strVal = mStringTable.mStrTable + adjVal;
  1804. int32 strHashVal = BlHash::HashStr_PdbV1(strVal) % hashBucketsSize;
  1805. BF_ASSERT(hashVal == strHashVal);
  1806. curAdjIdx++;
  1807. }
  1808. }
  1809. }
  1810. BF_ASSERT(curAdjIdx == adjustCount);
  1811. }
  1812. data = typeDataHead;
  1813. }
  1814. }
  1815. void COFF::ParseTypeData()
  1816. {
  1817. if (!mPDBLoaded)
  1818. return;
  1819. if (mParsedTypeData)
  1820. return;
  1821. mParsedTypeData = true;
  1822. auto linkedModule = GetLinkedModule();
  1823. int startingTypeIdx = (int)linkedModule->mTypes.size();
  1824. //uint8* sectionData;
  1825. int sectionSize = 0;
  1826. int hashAdjOffset = 0;
  1827. int32 hashStream = -1;
  1828. int32 hashAdjSize = 0;
  1829. int32 dataOffset = 0;
  1830. ParseTypeData(2, mCvTypeSectionReader, sectionSize, dataOffset, hashStream, hashAdjOffset, hashAdjSize, mCvMinTag, mCvMaxTag);
  1831. //mCvTypeSectionData = sectionData;
  1832. mCvTypeMap.Clear();
  1833. mCvTagStartMap.Clear();
  1834. mCvTypeMap.Resize(mCvMaxTag - mCvMinTag);
  1835. mCvTagStartMap.Resize(mCvMaxTag - mCvMinTag);
  1836. //DbgDataMap dataMap(minVal, maxVal);
  1837. //uint8* data = sectionData + dataOffset;
  1838. ParseTypeData(mCvTypeSectionReader, dataOffset);
  1839. if (hashAdjSize > 0)
  1840. {
  1841. int sectionSize = 0;
  1842. uint8* data = CvReadStream(hashStream, &sectionSize);
  1843. uint8* sectionData = data;
  1844. data = sectionData + hashAdjOffset;
  1845. GET_INTO(int32, adjustCount);
  1846. GET_INTO(int32, unk0);
  1847. GET_INTO(int32, unkCount);
  1848. for (int i = 0; i < unkCount; i++)
  1849. {
  1850. GET_INTO(int32, unk2);
  1851. }
  1852. GET_INTO(int32, unkCount2);
  1853. for (int i = 0; i < unkCount2; i++)
  1854. {
  1855. GET_INTO(int32, unk3);
  1856. }
  1857. // Types listed in the adjustment table are always primary types,
  1858. // they should override any "old types" with the same name
  1859. for (int adjIdx = 0; adjIdx < adjustCount; adjIdx++)
  1860. {
  1861. GET_INTO(int32, adjVal);
  1862. GET_INTO(CV_typ_t, typeId);
  1863. DbgType* dbgType = CvGetType(typeId);
  1864. if (dbgType != NULL)
  1865. {
  1866. dbgType->mPriority = DbgTypePriority_Primary_Explicit;
  1867. }
  1868. }
  1869. delete [] sectionData;
  1870. }
  1871. FixTypes(startingTypeIdx);
  1872. MapTypes(startingTypeIdx);
  1873. BF_ASSERT(mTempBufIdx == 0);
  1874. }
  1875. void COFF::FixConstant(DbgVariable* constVar)
  1876. {
  1877. if (constVar->mType->mTypeCode == DbgType_u8)
  1878. constVar->mConstValue &= 0xFF;
  1879. else if (constVar->mType->mTypeCode == DbgType_u16)
  1880. constVar->mConstValue &= 0xFFFF;
  1881. else if (constVar->mType->mTypeCode == DbgType_u32)
  1882. constVar->mConstValue &= 0xFFFFFFFF;
  1883. }
  1884. void COFF::MapRanges(DbgVariable* variable, CV_LVAR_ADDR_RANGE* range, CV_LVAR_ADDR_GAP* gaps)
  1885. {
  1886. auto rangeStart = GetSectionAddr(range->isectStart, range->offStart);
  1887. if (variable->mRangeStart == 0)
  1888. {
  1889. variable->mRangeStart = rangeStart;
  1890. variable->mRangeLen = (rangeStart + range->cbRange) - variable->mRangeStart;
  1891. }
  1892. else
  1893. {
  1894. addr_target maxEnd = BF_MAX(variable->mRangeStart + variable->mRangeLen, rangeStart + range->cbRange);
  1895. variable->mRangeStart = BF_MIN(variable->mRangeStart, rangeStart);
  1896. variable->mRangeLen = maxEnd - variable->mRangeStart;
  1897. }
  1898. }
  1899. void COFF::MakeThis(DbgSubprogram* curSubprogram, DbgVariable*& curParam)
  1900. {
  1901. if ((curParam != NULL) || (curSubprogram->mParams.mHead != NULL))
  1902. return;
  1903. BP_ALLOC_T(DbgVariable);
  1904. curParam = mAlloc.Alloc<DbgVariable>();
  1905. curParam->mIsParam = true;
  1906. curParam->mName = "this";
  1907. curSubprogram->mParams.mHead = curParam;
  1908. }
  1909. static int gSymCount = 0;
  1910. void COFF::ParseCompileUnit_Symbols(DbgCompileUnit* compileUnit, uint8* sectionData, uint8* data, uint8* symDataEnd, CvInlineInfoVec& inlineDataVec, bool wantDeferInternals, DbgSubprogram* useSubprogram)
  1911. {
  1912. bool isOptimized = false;
  1913. uint8* dataHead = data;
  1914. CvCompileUnit* cvCompileUnit = (CvCompileUnit*)compileUnit;
  1915. uint8* symDataStart = data;
  1916. DbgSubprogram* curSubprogram = NULL;
  1917. DbgVariable* curRegRelVariable = NULL;
  1918. DbgVariable* curParam = NULL;
  1919. DbgVariable* localVar = NULL;
  1920. bool inLocalVarRanged = false;
  1921. SizedArray<DbgBlock*, 16> blockStack;
  1922. blockStack.push_back(compileUnit->mGlobalBlock);
  1923. int deferBlockDepth = 0;
  1924. int deferInlineDepth = 0;
  1925. uint8* newLocationDataStart = NULL;
  1926. uint8* locationDataStart = NULL;
  1927. uint8* locationDataEnd = NULL;
  1928. int locationDataCount = 0;
  1929. struct _DeferredVariableLocation
  1930. {
  1931. DbgVariable* mVariable;
  1932. uint8* mRangedStart;
  1933. int mRangedLength;
  1934. _DeferredVariableLocation()
  1935. {
  1936. mVariable = NULL;
  1937. mRangedStart = NULL;
  1938. mRangedLength = 0;
  1939. }
  1940. };
  1941. SizedArray<_DeferredVariableLocation, 16> deferredVariableLocations;
  1942. int unrangedIdx = -1;
  1943. auto _NextUnrangedLocalVar = [&](const char* name, DbgType* varType)
  1944. {
  1945. inLocalVarRanged = false;
  1946. localVar = NULL;
  1947. unrangedIdx++;
  1948. while (unrangedIdx < deferredVariableLocations.size())
  1949. {
  1950. localVar = deferredVariableLocations[unrangedIdx].mVariable;
  1951. if (strcmp(localVar->mName, name) == 0)
  1952. break;
  1953. localVar = NULL;
  1954. unrangedIdx++;
  1955. }
  1956. bool isParam = false;
  1957. if (localVar == NULL)
  1958. {
  1959. if (curParam != NULL)
  1960. {
  1961. isParam = true;
  1962. if (curParam->mType != varType)
  1963. {
  1964. // Type was different, probably from a 'ref' being added when we are passing composites
  1965. curParam->mName = name;
  1966. curParam = curParam->mNext;
  1967. }
  1968. else
  1969. {
  1970. localVar = curParam;
  1971. curParam = curParam->mNext;
  1972. }
  1973. }
  1974. }
  1975. if (localVar == NULL)
  1976. {
  1977. BP_ALLOC_T(DbgVariable);
  1978. localVar = mAlloc.Alloc<DbgVariable>();
  1979. if (name != NULL)
  1980. blockStack.back()->mVariables.PushBack(localVar);
  1981. //return;
  1982. }
  1983. localVar->mName = name;
  1984. if (isParam)
  1985. localVar->mIsParam = true;
  1986. if (strcmp(name, "this") == 0)
  1987. {
  1988. MakeThis(curSubprogram, localVar);
  1989. //BF_ASSERT(varType->mTypeCode == DbgType_Ptr);
  1990. curSubprogram->mParentType = varType->mTypeParam;
  1991. curSubprogram->mHasThis = true;
  1992. }
  1993. };
  1994. auto _FinishLocationData = [&]()
  1995. {
  1996. if (locationDataStart == NULL)
  1997. return;
  1998. if (inLocalVarRanged)
  1999. {
  2000. auto& deferredVariableLocation = deferredVariableLocations.back();
  2001. BF_ASSERT(deferredVariableLocation.mRangedLength == 0);
  2002. deferredVariableLocation.mRangedStart = locationDataStart;
  2003. deferredVariableLocation.mRangedLength = (int)(locationDataEnd - locationDataStart);
  2004. }
  2005. else
  2006. {
  2007. if (localVar->mLocationData != NULL)
  2008. {
  2009. // Already handled
  2010. NOP;
  2011. }
  2012. else if (unrangedIdx >= deferredVariableLocations.size())
  2013. {
  2014. // There was no ranged data, commit just the unranged entry
  2015. BF_ASSERT(locationDataCount == localVar->mLocationLen);
  2016. BP_ALLOC("DeferredVarLoc0", locationDataEnd - locationDataStart);
  2017. localVar->mLocationData = mAlloc.AllocBytes(locationDataEnd - locationDataStart, "DeferredVarLoc0");
  2018. memcpy((uint8*)localVar->mLocationData, locationDataStart, locationDataEnd - locationDataStart);
  2019. }
  2020. else
  2021. {
  2022. // We have both ranged an unranged data. Merge them!
  2023. auto& deferredVariableLocation = deferredVariableLocations[unrangedIdx];
  2024. auto deferredVar = deferredVariableLocation.mVariable;
  2025. BF_ASSERT(deferredVar == localVar);
  2026. if (deferredVariableLocation.mRangedLength == 0)
  2027. BF_ASSERT(locationDataCount == localVar->mLocationLen);
  2028. else
  2029. BF_ASSERT(locationDataCount < localVar->mLocationLen);
  2030. BP_ALLOC("DeferredVarLoc1", deferredVariableLocation.mRangedLength + (locationDataEnd - locationDataStart));
  2031. localVar->mLocationData = mAlloc.AllocBytes(deferredVariableLocation.mRangedLength + (locationDataEnd - locationDataStart), "DeferredVarLoc1");
  2032. memcpy((uint8*)localVar->mLocationData, deferredVariableLocation.mRangedStart, deferredVariableLocation.mRangedLength);
  2033. memcpy((uint8*)localVar->mLocationData + deferredVariableLocation.mRangedLength, locationDataStart, locationDataEnd - locationDataStart);
  2034. deferredVariableLocation.mRangedLength = -1; // Mark as 'handled'
  2035. }
  2036. }
  2037. locationDataStart = NULL;
  2038. locationDataEnd = NULL;
  2039. locationDataCount = 0;
  2040. };
  2041. auto _FlushDeferredVariableLocations = [&]()
  2042. {
  2043. for (auto& deferredVariableLocation : deferredVariableLocations)
  2044. {
  2045. if (deferredVariableLocation.mRangedLength == -1)
  2046. continue;
  2047. auto deferredVar = deferredVariableLocation.mVariable;
  2048. if (deferredVar->mLocationData != NULL)
  2049. {
  2050. // Already handled
  2051. continue;
  2052. }
  2053. BP_ALLOC("DeferredVarLoc2", deferredVariableLocation.mRangedLength);
  2054. deferredVar->mLocationData = mAlloc.AllocBytes(deferredVariableLocation.mRangedLength, "DeferredVarLoc2");
  2055. memcpy((uint8*)deferredVar->mLocationData, deferredVariableLocation.mRangedStart, deferredVariableLocation.mRangedLength);
  2056. }
  2057. deferredVariableLocations.clear();
  2058. unrangedIdx = -1;
  2059. };
  2060. bool inlineDebugDump = false;
  2061. if (useSubprogram != NULL)
  2062. {
  2063. curSubprogram = useSubprogram;
  2064. blockStack.push_back(&curSubprogram->mBlock);
  2065. curParam = curSubprogram->mParams.mHead;
  2066. curRegRelVariable = curParam;
  2067. }
  2068. while (data < symDataEnd)
  2069. {
  2070. bool deferInternals = (wantDeferInternals) || (deferInlineDepth > 0);
  2071. gSymCount++;
  2072. uint8* dataStart = data;
  2073. GET_INTO(uint16, symLen);
  2074. uint8* dataEnd = data + symLen;
  2075. GET_INTO(uint16, symType);
  2076. if (!IsObjectFile())
  2077. BF_ASSERT(symLen % 4 == 2);
  2078. bool newLocalVarHasLocData = false;
  2079. DbgVariable* prevLocalVar = localVar;
  2080. switch (symType)
  2081. {
  2082. case S_LOCAL:
  2083. case S_BPREL32:
  2084. case S_REGISTER:
  2085. case S_REGREL32:
  2086. case S_GPROC32:
  2087. case S_LPROC32:
  2088. case S_GPROC32_ID:
  2089. case S_LPROC32_ID:
  2090. // Starting a new var or method
  2091. if ((!deferInternals) && (locationDataStart != NULL))
  2092. {
  2093. _FinishLocationData();
  2094. }
  2095. break;
  2096. }
  2097. /*if (handled)
  2098. {
  2099. data = dataEnd;
  2100. continue;
  2101. }*/
  2102. if (inlineDebugDump)
  2103. BfLogCv("SYMTYPE: %X\n", symType);
  2104. switch (symType)
  2105. {
  2106. case S_UDT:
  2107. {
  2108. UDTSYM& udtSym = *(UDTSYM*)dataStart;
  2109. DbgType* typeDefType = CvCreateType();
  2110. typeDefType->mTypeCode = DbgType_TypeDef;
  2111. typeDefType->mTypeParam = CvGetTypeSafe(udtSym.typind);
  2112. typeDefType->mName = DbgDupString((const char*)udtSym.name, "DbgDupString.S_UDT");
  2113. if (strncmp(typeDefType->mName, "_bf::", 5) == 0)
  2114. {
  2115. typeDefType->mLanguage = DbgLanguage_Beef;
  2116. typeDefType->mName += 5;
  2117. }
  2118. else
  2119. {
  2120. typeDefType->mLanguage = DbgLanguage_C;
  2121. }
  2122. typeDefType->mTypeName = typeDefType->mName;
  2123. }
  2124. break;
  2125. case S_OBJNAME:
  2126. {
  2127. GET_INTO(int32, objSig);
  2128. const char* objName = CvParseAndDupString(data);
  2129. }
  2130. break;
  2131. case S_CONSTANT:
  2132. {
  2133. CONSTSYM& constSym = *(CONSTSYM*)dataStart;
  2134. BP_ALLOC_T(DbgVariable);
  2135. DbgVariable* constVar = mAlloc.Alloc<DbgVariable>();
  2136. constVar->mName = DbgDupString((const char*)constSym.name, "DbgDupString.S_CONSTANT");
  2137. constVar->mType = CvGetTypeSafe(constSym.typind);
  2138. constVar->mIsConst = true;
  2139. constVar->mIsStatic = true;
  2140. constVar->mConstValue = constSym.value;
  2141. data = constSym.name + strlen((const char*)constSym.name) + 1;
  2142. constVar->mConstValue = CvParseConstant(constSym.value, data);
  2143. FixConstant(constVar);
  2144. if (constVar->mName != NULL)
  2145. blockStack.back()->mVariables.PushBack(constVar);
  2146. }
  2147. break;
  2148. case S_EXPORT:
  2149. {
  2150. GET_INTO(int16, ord);
  2151. struct ExpFlags
  2152. {
  2153. unsigned short fConstant : 1; // CONSTANT
  2154. unsigned short fData : 1; // DATA
  2155. unsigned short fPrivate : 1; // PRIVATE
  2156. unsigned short fNoName : 1; // NONAME
  2157. unsigned short fOrdinal : 1; // Ordinal was explicitly assigned
  2158. unsigned short fForwarder : 1; // This is a forwarder
  2159. unsigned short reserved : 10; // Reserved. Must be zero.
  2160. };
  2161. GET_INTO(ExpFlags, flags);
  2162. const char* symName = CvParseAndDupString(data);
  2163. }
  2164. break;
  2165. case S_GDATA32:
  2166. case S_LDATA32:
  2167. // In PDB reads we get this from the symbol stream
  2168. if ((IsObjectFile()) || (curSubprogram != NULL))
  2169. {
  2170. auto linkedModule = GetLinkedModule();
  2171. DATASYM32& dataSym = *(DATASYM32*)dataStart;
  2172. char* name = (char*)dataSym.name;
  2173. char* targetName = NULL;
  2174. DbgType* targetType = mMasterCompileUnit->mGlobalType;
  2175. bool overrideBeef = false;
  2176. char* lastDblColon = (char*)GetLastDoubleColon(name);
  2177. if (lastDblColon != NULL)
  2178. {
  2179. targetName = name;
  2180. name = lastDblColon + 2;
  2181. *lastDblColon = 0;
  2182. if (strcmp(targetName, "_bf") == 0)
  2183. {
  2184. overrideBeef = true;
  2185. }
  2186. else
  2187. {
  2188. targetType = CvGetTypeOrNamespace(targetName);
  2189. }
  2190. }
  2191. DbgType* dbgType = CvGetTypeSafe(dataSym.typind);
  2192. BP_ALLOC_T(DbgVariable);
  2193. DbgVariable* variable = mAlloc.Alloc<DbgVariable>();
  2194. variable->mType = dbgType;
  2195. variable->mLocationData = dataStart;
  2196. variable->mLocationLen = 1;
  2197. variable->mIsStatic = true;
  2198. variable->mCompileUnit = mMasterCompileUnit;
  2199. variable->mName = name;
  2200. if (targetType == mMasterCompileUnit->mGlobalType)
  2201. variable->mLinkName = name;
  2202. variable->mIsExtern = symType == S_GDATA32;
  2203. if (curSubprogram != NULL)
  2204. {
  2205. if (!IsObjectFile())
  2206. {
  2207. // Copy this, we free the source data
  2208. variable->mName = DbgDupString(name, "DbgDupString.S_GDATA32");
  2209. BP_ALLOC_T(DATASYM32)
  2210. auto newPtr = mAlloc.Alloc<DATASYM32>();
  2211. memcpy(newPtr, variable->mLocationData, sizeof(DATASYM32));
  2212. variable->mLocationData = (uint8*)newPtr;
  2213. }
  2214. if (variable->mName != NULL)
  2215. blockStack.back()->mVariables.PushBack(variable);
  2216. break;
  2217. }
  2218. //variable->mCompileUnit = m;
  2219. // Push front so we will find before the original static declaration (that has no memory associated with it)
  2220. if (targetType != NULL)
  2221. targetType->mMemberList.PushFront(variable);
  2222. if ((variable->mIsExtern) && (variable->mLinkName != NULL))
  2223. mStaticVariables.push_back(variable);
  2224. }
  2225. break;
  2226. case S_GTHREAD32:
  2227. case S_LTHREAD32:
  2228. {
  2229. if ((IsObjectFile()) || (curSubprogram != NULL))
  2230. {
  2231. auto linkedModule = GetLinkedModule();
  2232. THREADSYM32& dataSym = *(THREADSYM32*)dataStart;
  2233. char* name = (char*)dataSym.name;
  2234. char* targetName = NULL;
  2235. DbgType* targetType = mMasterCompileUnit->mGlobalType;
  2236. bool overrideBeef = false;
  2237. char* lastDblColon = (char*)GetLastDoubleColon(name);
  2238. if (lastDblColon != NULL)
  2239. {
  2240. targetName = name;
  2241. name = lastDblColon + 2;
  2242. *lastDblColon = 0;
  2243. if (strcmp(targetName, "_bf") == 0)
  2244. {
  2245. overrideBeef = true;
  2246. }
  2247. else
  2248. {
  2249. targetType = CvGetTypeOrNamespace(targetName);
  2250. }
  2251. }
  2252. DbgType* dbgType = CvGetTypeSafe(dataSym.typind);
  2253. BP_ALLOC_T(DbgVariable);
  2254. DbgVariable* variable = mAlloc.Alloc<DbgVariable>();
  2255. variable->mType = dbgType;
  2256. variable->mLocationData = dataStart;
  2257. variable->mLocationLen = 1;
  2258. variable->mIsStatic = true;
  2259. variable->mCompileUnit = mMasterCompileUnit;
  2260. variable->mName = name;
  2261. if (targetType == mMasterCompileUnit->mGlobalType)
  2262. variable->mLinkName = name;
  2263. variable->mIsExtern = symType == S_GTHREAD32;
  2264. if (curSubprogram != NULL)
  2265. {
  2266. if (!IsObjectFile())
  2267. {
  2268. // Copy this, we free the source data
  2269. variable->mName = DbgDupString(name, "DbgDupString.S_GTHREAD32");
  2270. BP_ALLOC_T(DATASYM32)
  2271. auto newPtr = mAlloc.Alloc<DATASYM32>();
  2272. memcpy(newPtr, variable->mLocationData, sizeof(DATASYM32));
  2273. variable->mLocationData = (uint8*)newPtr;
  2274. }
  2275. if (variable->mName != NULL)
  2276. blockStack.back()->mVariables.PushBack(variable);
  2277. break;
  2278. }
  2279. //variable->mCompileUnit = m;
  2280. // Push front so we will find before the original static declaration (that has no memory associated with it)
  2281. targetType->mMemberList.PushFront(variable);
  2282. if ((variable->mIsExtern) && (variable->mLinkName != NULL))
  2283. mStaticVariables.push_back(variable);
  2284. }
  2285. }
  2286. break;
  2287. case S_GPROC32:
  2288. case S_LPROC32:
  2289. case S_GPROC32_ID:
  2290. case S_LPROC32_ID:
  2291. {
  2292. BF_ASSERT(curSubprogram == NULL);
  2293. PROCSYM32* procSym = (PROCSYM32*)dataStart;
  2294. DbgType* parentType = NULL;
  2295. auto addr = GetSectionAddr(procSym->seg, procSym->off);
  2296. DbgSubprogram* subprogram = NULL;
  2297. if (procSym->typind != 0)
  2298. {
  2299. bool ipi = false;
  2300. if ((!IsObjectFile()) &&
  2301. ((symType == S_GPROC32_ID) || (symType == S_LPROC32_ID)))
  2302. {
  2303. if (!mCvIPIReader.IsSetup())
  2304. CvParseIPI();
  2305. ipi = true;
  2306. }
  2307. subprogram = CvParseMethod(parentType, NULL, procSym->typind, ipi);
  2308. }
  2309. if (subprogram == NULL)
  2310. {
  2311. BP_ALLOC_T(DbgSubprogram);
  2312. subprogram = mAlloc.Alloc<DbgSubprogram>();
  2313. }
  2314. subprogram->mTagIdx = (int)(dataEnd - sectionData); // Position for method data
  2315. subprogram->mIsOptimized = isOptimized;
  2316. subprogram->mCompileUnit = compileUnit;
  2317. char* name = DbgDupString((const char*)procSym->name, "DbgDupString.S_GPROC32");
  2318. if (procSym->flags.CV_PFLAG_OPTDBGINFO)
  2319. {
  2320. subprogram->mIsOptimized = true;
  2321. }
  2322. if ((name[0] == '_') && (name[1] == '_'))
  2323. {
  2324. // This matches things like "__chkstk" or other system (supposedly) functions
  2325. subprogram->mIsStepFilteredDefault = true;
  2326. }
  2327. if (strncmp(name, "_bf::", 5) == 0)
  2328. {
  2329. compileUnit->mLanguage = DbgLanguage_Beef;
  2330. }
  2331. localVar = NULL;
  2332. bool hasColon = false;
  2333. for (char* cPtr = name; *cPtr != 0; cPtr++)
  2334. {
  2335. char c = *cPtr;
  2336. hasColon |= c == ':';
  2337. if (c == '$')
  2338. {
  2339. if (strcmp(cPtr, "$CHK") == 0)
  2340. {
  2341. subprogram->mCheckedKind = BfCheckedKind_Checked;
  2342. *cPtr = NULL;
  2343. break;
  2344. }
  2345. else if (strcmp(cPtr, "$UCHK") == 0)
  2346. {
  2347. subprogram->mCheckedKind = BfCheckedKind_Unchecked;
  2348. *cPtr = NULL;
  2349. break;
  2350. }
  2351. }
  2352. }
  2353. subprogram->mPrologueSize = procSym->DbgStart;
  2354. subprogram->mBlock.mLowPC = addr;
  2355. subprogram->mBlock.mHighPC = subprogram->mBlock.mLowPC + (int32)procSym->len;
  2356. BF_ASSERT(procSym->len >= 0);
  2357. MapSubprogram(subprogram);
  2358. curSubprogram = subprogram;
  2359. curParam = subprogram->mParams.mHead;
  2360. curRegRelVariable = curParam;
  2361. blockStack.push_back(&subprogram->mBlock);
  2362. //OutputDebugStrF("Func: %s\n", subprogram->mName);
  2363. if (hasColon)
  2364. {
  2365. subprogram->mName = name;
  2366. subprogram->mHasQualifiedName = true;
  2367. compileUnit->mOrphanMethods.PushBack(curSubprogram);
  2368. }
  2369. else
  2370. {
  2371. subprogram->mName = name;
  2372. compileUnit->mGlobalType->mMethodList.PushBack(curSubprogram);
  2373. }
  2374. BF_ASSERT(unrangedIdx == -1);
  2375. }
  2376. break;
  2377. case S_THUNK32:
  2378. {
  2379. THUNKSYM32& thunkSym = *(THUNKSYM32*)dataStart;
  2380. DbgType* parentType = NULL;
  2381. DbgSubprogram* subprogram;
  2382. BP_ALLOC_T(DbgSubprogram);
  2383. subprogram = mAlloc.Alloc<DbgSubprogram>();
  2384. subprogram->mCompileUnit = compileUnit;
  2385. subprogram->mHasQualifiedName = true;
  2386. subprogram->mName = DbgDupString((const char*)thunkSym.name, "DbgDupString.S_THUNK32");
  2387. subprogram->mTagIdx = (int)(dataEnd - sectionData); // Position for method data
  2388. subprogram->mBlock.mLowPC = GetSectionAddr(thunkSym.seg, thunkSym.off);
  2389. subprogram->mBlock.mHighPC = subprogram->mBlock.mLowPC + thunkSym.len;
  2390. BF_ASSERT(thunkSym.len >= 0);
  2391. MapSubprogram(subprogram);
  2392. curSubprogram = subprogram;
  2393. curParam = subprogram->mParams.mHead;
  2394. blockStack.push_back(&subprogram->mBlock);
  2395. BF_ASSERT(unrangedIdx == -1);
  2396. //OutputDebugStrF("Func: %s\n", subprogram->mName);
  2397. }
  2398. break;
  2399. case S_BLOCK32:
  2400. {
  2401. if (deferInternals)
  2402. {
  2403. deferBlockDepth++;
  2404. break;
  2405. }
  2406. BP_ALLOC_T(DbgBlock);
  2407. DbgBlock* block = mAlloc.Alloc<DbgBlock>();
  2408. blockStack.back()->mSubBlocks.PushBack(block);
  2409. blockStack.push_back(block);
  2410. BLOCKSYM32* blockSym = (BLOCKSYM32*)dataStart;
  2411. block->mLowPC = GetSectionAddr(blockSym->seg, blockSym->off);
  2412. block->mHighPC = block->mLowPC + blockSym->len;
  2413. }
  2414. break;
  2415. case S_FRAMEPROC:
  2416. {
  2417. FRAMEPROCSYM* frameProc = (FRAMEPROCSYM*)dataStart;
  2418. if (curSubprogram != NULL)
  2419. {
  2420. // if ((curSubprogram->mName != NULL) && (strcmp(curSubprogram->mName, "_bf::IDETest::HotTester::TestFuncs") == 0))
  2421. // {
  2422. // NOP;
  2423. // }
  2424. curSubprogram->mFrameBaseLen = frameProc->cbFrame + frameProc->cbSaveRegs;
  2425. curSubprogram->mParamBaseReg = (DbgSubprogram::LocalBaseRegKind)(uint8)frameProc->flags.encodedParamBasePointer;
  2426. curSubprogram->mLocalBaseReg = (DbgSubprogram::LocalBaseRegKind)(uint8)frameProc->flags.encodedLocalBasePointer;
  2427. }
  2428. }
  2429. break;
  2430. case S_BPREL32:
  2431. {
  2432. if (deferInternals)
  2433. break;
  2434. BPRELSYM32* bpRel32 = (BPRELSYM32*)dataStart;
  2435. const char* name = DbgDupString((const char*)bpRel32->name, "DbgDupString.S_BPREL32");
  2436. DbgType* varType = CvGetTypeSafe(bpRel32->typind);
  2437. _NextUnrangedLocalVar(name, varType);
  2438. localVar->mName = name;
  2439. localVar->mCompileUnit = compileUnit;
  2440. localVar->mType = varType;
  2441. // This is location data now, not just a S_LOCAL opener
  2442. //prevLocalVar = localVar;
  2443. newLocalVarHasLocData = true;
  2444. }
  2445. break;
  2446. case S_REGISTER:
  2447. {
  2448. if (deferInternals)
  2449. break;
  2450. REGSYM* regSym = (REGSYM*)dataStart;
  2451. const char* name = DbgDupString((const char*)regSym->name);
  2452. DbgType* varType = CvGetTypeSafe(regSym->typind);
  2453. _NextUnrangedLocalVar(name, varType);
  2454. localVar->mName = name;
  2455. localVar->mCompileUnit = compileUnit;
  2456. localVar->mType = varType;
  2457. // This is location data now, not just a S_LOCAL opener
  2458. //prevLocalVar = localVar;
  2459. newLocalVarHasLocData = true;
  2460. }
  2461. break;
  2462. case S_REGREL32:
  2463. {
  2464. if (deferInternals)
  2465. break;
  2466. REGREL32* regRel32 = (REGREL32*)dataStart;
  2467. const char* name = DbgDupString((const char*)regRel32->name);
  2468. DbgType* varType = CvGetTypeSafe(regRel32->typind);
  2469. _NextUnrangedLocalVar(name, varType);
  2470. localVar->mName = name;
  2471. localVar->mCompileUnit = compileUnit;
  2472. if ((localVar->mType != NULL) && (!localVar->mType->IsPointer()) && (varType != NULL) && (varType->IsPointer()))
  2473. localVar->mSigNoPointer = true;
  2474. localVar->mType = varType;
  2475. // This is location data now, not just a S_LOCAL opener
  2476. //prevLocalVar = localVar;
  2477. newLocalVarHasLocData = true;
  2478. }
  2479. break;
  2480. case S_LOCAL:
  2481. {
  2482. if (deferInternals)
  2483. break;
  2484. inLocalVarRanged = true;
  2485. LOCALSYM& localSym = *(LOCALSYM*)dataStart;
  2486. char* name = DbgDupString((const char*)localSym.name);
  2487. bool isConst = false;
  2488. int64 constVal = 0;
  2489. if ((compileUnit->mLanguage == DbgLanguage_Beef) && (name != NULL) && (name[0] != '$'))
  2490. {
  2491. for (char* cPtr = name + 1; true; cPtr++)
  2492. {
  2493. char c = *cPtr;
  2494. if (c == 0)
  2495. break;
  2496. if (c == '$')
  2497. {
  2498. if (cPtr[1] == '_')
  2499. {
  2500. cPtr[1] = '-';
  2501. isConst = true;
  2502. constVal = atoll(cPtr + 1);
  2503. *cPtr = 0;
  2504. }
  2505. else if ((cPtr[1] >= '0') && (cPtr[1] <= '9'))
  2506. {
  2507. isConst = true;
  2508. constVal = atoll(cPtr + 1);
  2509. *cPtr = 0;
  2510. }
  2511. }
  2512. }
  2513. }
  2514. if ((name != NULL) && (name[0] == '#'))
  2515. {
  2516. if (strcmp(name + 1, "StepOver") == 0)
  2517. {
  2518. curSubprogram->mIsStepFilteredDefault = true;
  2519. }
  2520. localVar = NULL;
  2521. break;
  2522. }
  2523. DbgType* varType = CvGetType(localSym.typind, cvCompileUnit);
  2524. if (varType == NULL)
  2525. varType = CvGetType(T_VOID);
  2526. if (name != NULL)
  2527. {
  2528. if ((localSym.flags.fIsOptimizedOut) && (name[0] != '$'))
  2529. {
  2530. if (varType->mSize > 0)
  2531. curSubprogram->mIsOptimized = true;
  2532. }
  2533. if ((strcmp(name, "this") == 0) && (varType != NULL))
  2534. {
  2535. //BF_ASSERT(varType->mTypeCode == DbgType_Ptr);
  2536. MakeThis(curSubprogram, curParam);
  2537. curSubprogram->mParentType = varType->mTypeParam;
  2538. curSubprogram->mHasThis = true;
  2539. }
  2540. }
  2541. bool handledLocalVar = false;
  2542. if (curParam != NULL)
  2543. {
  2544. if ((name != NULL) && (name[0] == '$'))
  2545. {
  2546. int strLen = strlen(name);
  2547. // Splat head
  2548. const char* dollarPos = strchr(name + 1, '$');
  2549. const char* nameStr = name + 1;
  2550. int nameLen = dollarPos - name - 1;
  2551. if ((dollarPos != NULL) &&
  2552. ((localVar == NULL) || (strncmp(localVar->mName, name, nameLen + 1) != 0)))
  2553. {
  2554. BP_ALLOC("ParamName", nameLen + 1);
  2555. char* dupStr = (char*)mAlloc.AllocBytes(nameLen + 1, "ParamName");
  2556. memcpy(dupStr, nameStr, nameLen);
  2557. curParam->mName = dupStr;
  2558. curParam->mIsConst = true;
  2559. curParam = curParam->mNext;
  2560. if (strcmp(dupStr, "this") == 0)
  2561. curSubprogram->mHasThis = true;
  2562. }
  2563. }
  2564. else if ((curParam->mType != varType) && (varType->mTypeCode != DbgType_Void))
  2565. {
  2566. // Type was different, probably from a 'ref' being added when we are passing composites
  2567. curParam->mName = name;
  2568. curParam = curParam->mNext;
  2569. }
  2570. else
  2571. {
  2572. localVar = curParam;
  2573. curParam = curParam->mNext;
  2574. handledLocalVar = true;
  2575. }
  2576. }
  2577. if (!handledLocalVar)
  2578. {
  2579. BP_ALLOC_T(DbgVariable);
  2580. localVar = mAlloc.Alloc<DbgVariable>();
  2581. if (name != NULL)
  2582. blockStack.back()->mVariables.PushBack(localVar);
  2583. }
  2584. localVar->mName = name;
  2585. localVar->mCompileUnit = compileUnit;
  2586. localVar->mType = varType;
  2587. localVar->mIsConst = isConst;
  2588. localVar->mConstValue = constVal;
  2589. _DeferredVariableLocation deferredLocation;
  2590. deferredLocation.mVariable = localVar;
  2591. deferredVariableLocations.Add(deferredLocation);
  2592. }
  2593. break;
  2594. case S_DEFRANGE_REGISTER:
  2595. {
  2596. if (deferInternals)
  2597. break;
  2598. DEFRANGESYMREGISTER& defRangeReg = *(DEFRANGESYMREGISTER*)dataStart;
  2599. if (localVar != NULL)
  2600. MapRanges(localVar, &defRangeReg.range, defRangeReg.gaps);
  2601. }
  2602. break;
  2603. case S_DEFRANGE_FRAMEPOINTER_REL:
  2604. {
  2605. if (deferInternals)
  2606. break;
  2607. DEFRANGESYMFRAMEPOINTERREL& defRangeFPRel = *(DEFRANGESYMFRAMEPOINTERREL*)dataStart;
  2608. MapRanges(localVar, &defRangeFPRel.range, defRangeFPRel.gaps);
  2609. }
  2610. break;
  2611. case S_DEFRANGE_SUBFIELD_REGISTER:
  2612. {
  2613. if (deferInternals)
  2614. break;
  2615. DEFRANGESYMSUBFIELDREGISTER& defRangeSubFieldReg = *(DEFRANGESYMSUBFIELDREGISTER*)dataStart;
  2616. }
  2617. break;
  2618. case S_DEFRANGE_FRAMEPOINTER_REL_FULL_SCOPE:
  2619. {
  2620. if (deferInternals)
  2621. break;
  2622. DEFRANGESYMFRAMEPOINTERREL_FULL_SCOPE& defRangeFPRel = *(DEFRANGESYMFRAMEPOINTERREL_FULL_SCOPE*)dataStart;
  2623. }
  2624. break;
  2625. case S_DEFRANGE_REGISTER_REL:
  2626. {
  2627. if (deferInternals)
  2628. break;
  2629. DEFRANGESYMREGISTERREL& defRangeRegRel = *(DEFRANGESYMREGISTERREL*)dataStart;
  2630. if (localVar != NULL)
  2631. MapRanges(localVar, &defRangeRegRel.range, defRangeRegRel.gaps);
  2632. }
  2633. break;
  2634. case S_ENDARG:
  2635. if (deferInternals)
  2636. break;
  2637. BF_ASSERT(curParam == NULL);
  2638. break;
  2639. case S_END:
  2640. case S_PROC_ID_END:
  2641. if (deferBlockDepth > 0)
  2642. --deferBlockDepth;
  2643. else
  2644. blockStack.pop_back();
  2645. BF_ASSERT(blockStack.size() > 0);
  2646. if (blockStack.size() == 1)
  2647. {
  2648. _FinishLocationData();
  2649. _FlushDeferredVariableLocations();
  2650. // Done with forced subprogram?
  2651. if (useSubprogram != NULL)
  2652. {
  2653. return;
  2654. }
  2655. else if (deferInternals)
  2656. {
  2657. if (curSubprogram->mTagIdx != -1)
  2658. {
  2659. int endOffset = (int)(dataEnd - sectionData);
  2660. curSubprogram->mDeferredInternalsSize = endOffset - curSubprogram->mTagIdx;
  2661. }
  2662. }
  2663. inlineDebugDump = false;
  2664. curSubprogram = NULL;
  2665. curParam = NULL;
  2666. }
  2667. break;
  2668. case S_COMPILE2:
  2669. {
  2670. COMPILESYM* compileSym = (COMPILESYM*)dataStart;
  2671. }
  2672. break;
  2673. case S_COMPILE3:
  2674. {
  2675. COMPILESYM3* compileSym = (COMPILESYM3*)dataStart;
  2676. }
  2677. break;
  2678. case S_ENVBLOCK:
  2679. {
  2680. GET_INTO(uint8, flags);
  2681. while (true)
  2682. {
  2683. const char* envKey = CvParseAndDupString(data);
  2684. if (envKey == NULL)
  2685. break;
  2686. const char* envValue = CvParseAndDupString(data);
  2687. }
  2688. }
  2689. break;
  2690. case S_BUILDINFO:
  2691. {
  2692. CV_ItemId buildInfoId = GET(CV_ItemId);
  2693. }
  2694. break;
  2695. case S_TRAMPOLINE:
  2696. break;
  2697. case S_COFFGROUP:
  2698. break;
  2699. case S_SECTION:
  2700. break;
  2701. case S_FRAMECOOKIE:
  2702. {
  2703. FRAMECOOKIE& frameCookie = *(FRAMECOOKIE*)dataStart;
  2704. }
  2705. break;
  2706. case S_LABEL32:
  2707. {
  2708. LABELSYM32& labelSym = *(LABELSYM32*)dataStart;
  2709. }
  2710. break;
  2711. case S_CALLSITEINFO:
  2712. {
  2713. CALLSITEINFO& callSiteInfo = *(CALLSITEINFO*)dataStart;
  2714. }
  2715. break;
  2716. case S_HEAPALLOCSITE:
  2717. {
  2718. HEAPALLOCSITE& heapAllocSite = *(HEAPALLOCSITE*)dataStart;
  2719. }
  2720. break;
  2721. case S_FILESTATIC:
  2722. {
  2723. FILESTATICSYM& fileStaticSym = *(FILESTATICSYM*)dataStart;
  2724. }
  2725. break;
  2726. case S_CALLEES:
  2727. {
  2728. FUNCTIONLIST& calleeList = *(FUNCTIONLIST*)dataStart;
  2729. }
  2730. break;
  2731. case S_CALLERS:
  2732. {
  2733. FUNCTIONLIST& calleeList = *(FUNCTIONLIST*)dataStart;
  2734. }
  2735. break;
  2736. case S_POGODATA:
  2737. {
  2738. POGOINFO& pogoInfo = *(POGOINFO*)dataStart;
  2739. }
  2740. break;
  2741. case S_INLINESITE:
  2742. case S_INLINESITE2:
  2743. {
  2744. if (useSubprogram != NULL)
  2745. {
  2746. deferInlineDepth++;
  2747. break; // Already handled this
  2748. }
  2749. uint32 inlinee;
  2750. uint8* binaryAnnotations;
  2751. if (symType == S_INLINESITE)
  2752. {
  2753. INLINESITESYM& inlineSite = *(INLINESITESYM*)dataStart;
  2754. inlinee = inlineSite.inlinee;
  2755. binaryAnnotations = (uint8*)&inlineSite.binaryAnnotations;
  2756. }
  2757. else // S_INLINESITE2
  2758. {
  2759. INLINESITESYM2& inlineSite = *(INLINESITESYM2*)dataStart;
  2760. inlinee = inlineSite.inlinee;
  2761. binaryAnnotations = (uint8*)&inlineSite.binaryAnnotations;
  2762. }
  2763. DbgSubprogram* inlineParent = curSubprogram;
  2764. DbgSubprogram* subprogram = NULL;
  2765. if (IsObjectFile())
  2766. {
  2767. subprogram = CvParseMethod(NULL, NULL, inlinee, false);
  2768. subprogram->mCompileUnit = compileUnit;
  2769. curSubprogram = subprogram;
  2770. }
  2771. else if ((inlinee & 0x80000000) != 0)
  2772. {
  2773. BP_ALLOC_T(DbgSubprogram);
  2774. subprogram = mAlloc.Alloc<DbgSubprogram>(); // ??
  2775. subprogram->mCompileUnit = compileUnit;
  2776. if (!mCvIPIReader.IsSetup())
  2777. CvParseIPI();
  2778. curSubprogram = subprogram;
  2779. }
  2780. else
  2781. {
  2782. BP_ALLOC_T(DbgSubprogram);
  2783. subprogram = mAlloc.Alloc<DbgSubprogram>();
  2784. subprogram->mCompileUnit = compileUnit;
  2785. curSubprogram = subprogram;
  2786. FixupInlinee(curSubprogram, inlinee);
  2787. }
  2788. BF_ASSERT(subprogram == curSubprogram);
  2789. curSubprogram->mTagIdx = (int)(dataEnd - sectionData); // Position for method data
  2790. BP_ALLOC_T(DbgInlineeInfo);
  2791. curSubprogram->mInlineeInfo = mAlloc.Alloc<DbgInlineeInfo>();
  2792. curSubprogram->mInlineeInfo->mInlineParent = inlineParent;
  2793. if (inlineParent->mInlineeInfo != NULL)
  2794. curSubprogram->mInlineeInfo->mInlineDepth = inlineParent->mInlineeInfo->mInlineDepth + 1;
  2795. else
  2796. curSubprogram->mInlineeInfo->mInlineDepth = 1;
  2797. curSubprogram->mInlineeInfo->mRootInliner = inlineParent->GetRootInlineParent();
  2798. curSubprogram->mInlineeInfo->mInlineeId = 0;
  2799. curSubprogram->mFrameBaseData = inlineParent->mFrameBaseData;
  2800. curSubprogram->mFrameBaseLen = inlineParent->mFrameBaseLen;
  2801. curSubprogram->mLocalBaseReg = inlineParent->mLocalBaseReg;
  2802. if (curSubprogram->mName == NULL)
  2803. {
  2804. /*curSubprogram->mName = "<Inline>";
  2805. String name = StrFormat("<Inline>@%@", curSubprogram);
  2806. curSubprogram->mName = DbgDupString(name.c_str());*/
  2807. String name = StrFormat("<Inline>@%@", curSubprogram);
  2808. curSubprogram->mName = DbgDupString(name.c_str());
  2809. curSubprogram->mInlineeInfo->mInlineeId = inlinee;
  2810. }
  2811. if (inlineDebugDump)
  2812. {
  2813. int depth = curSubprogram->GetInlineDepth();
  2814. BfLogCv("S_INLINESITE Ofs:%d Depth:%d Inlinee:%X Subprogram:%@ %s\n Parent:%@ %s\n", (dataStart - symDataStart), depth, inlinee,
  2815. curSubprogram, curSubprogram->mName, curSubprogram->mInlineeInfo->mInlineParent, curSubprogram->mInlineeInfo->mInlineParent->mName);
  2816. }
  2817. CvInlineInfo inlineInfo;
  2818. inlineInfo.mNext = NULL;
  2819. inlineInfo.mTail = NULL;
  2820. inlineInfo.mInlinee = inlinee;
  2821. inlineInfo.mSubprogram = NULL;
  2822. inlineInfo.mSubprogram = curSubprogram;
  2823. inlineInfo.mData = binaryAnnotations;
  2824. inlineInfo.mDataLen = dataEnd - inlineInfo.mData;
  2825. inlineInfo.mDebugDump = inlineDebugDump;
  2826. inlineDataVec.push_back(inlineInfo);
  2827. blockStack.push_back(&curSubprogram->mBlock);
  2828. }
  2829. break;
  2830. case S_INLINESITE_END:
  2831. {
  2832. if (inlineDebugDump)
  2833. BfLogCv("S_INLINESITE_END\n");
  2834. if (deferInlineDepth > 0)
  2835. {
  2836. deferInlineDepth--;
  2837. }
  2838. else
  2839. {
  2840. if (deferInternals)
  2841. {
  2842. int endOffset = (int)(dataEnd - sectionData);
  2843. curSubprogram->mDeferredInternalsSize = endOffset - curSubprogram->mTagIdx;
  2844. }
  2845. curSubprogram = curSubprogram->mInlineeInfo->mInlineParent;
  2846. blockStack.pop_back();
  2847. }
  2848. }
  2849. break;
  2850. case S_SSEARCH:
  2851. {
  2852. SEARCHSYM* searchSym = (SEARCHSYM*)dataStart;
  2853. }
  2854. break;
  2855. case S_SEPCODE:
  2856. {
  2857. BP_ALLOC_T(DbgSubprogram);
  2858. curSubprogram = mAlloc.Alloc<DbgSubprogram>();
  2859. curSubprogram->mCompileUnit = compileUnit;
  2860. blockStack.push_back(&curSubprogram->mBlock);
  2861. SEPCODESYM* sepCodeSym = (SEPCODESYM*)dataStart;
  2862. auto addr = GetSectionAddr(sepCodeSym->sect, sepCodeSym->off);
  2863. auto parentAddr = GetSectionAddr(sepCodeSym->sectParent, sepCodeSym->offParent);
  2864. curSubprogram->mBlock.mLowPC = addr;
  2865. curSubprogram->mBlock.mHighPC = addr + sepCodeSym->length;
  2866. String name = StrFormat("SEPCODE@%p", parentAddr);
  2867. curSubprogram->mName = DbgDupString(name.c_str());
  2868. curSubprogram->mTagIdx = (int)(dataEnd - sectionData); // Position for method data
  2869. MapSubprogram(curSubprogram);
  2870. }
  2871. break;
  2872. case S_COMPILE:
  2873. break;
  2874. case S_ANNOTATION:
  2875. break;
  2876. case S_UNAMESPACE:
  2877. break;
  2878. case /*S_FASTLINK*/0x1167:
  2879. break;
  2880. case /*S_INLINEES*/0x1168:
  2881. break;
  2882. case 0x1176:
  2883. case 0x1178:
  2884. case 0x1179:
  2885. case 0x117A:
  2886. case 0x117B:
  2887. case 0x117C:
  2888. case 0x117D:
  2889. case 0x117E:
  2890. case 0x117F:
  2891. case 0x1180:
  2892. break;
  2893. case 7:
  2894. // Unknown
  2895. break;
  2896. default:
  2897. BF_DBG_FATAL("Unhandled");
  2898. break;
  2899. }
  2900. bool hasNewLocalVar = (localVar != prevLocalVar) && (localVar != NULL);
  2901. if (newLocalVarHasLocData)
  2902. {
  2903. prevLocalVar = localVar;
  2904. }
  2905. if (deferInlineDepth == 0)
  2906. {
  2907. switch (symType)
  2908. {
  2909. case S_BPREL32:
  2910. case S_REGISTER:
  2911. case S_REGREL32:
  2912. case S_DEFRANGE_REGISTER:
  2913. case S_DEFRANGE_FRAMEPOINTER_REL:
  2914. case S_DEFRANGE_SUBFIELD_REGISTER:
  2915. case S_DEFRANGE_FRAMEPOINTER_REL_FULL_SCOPE:
  2916. case S_DEFRANGE_REGISTER_REL:
  2917. {
  2918. if (prevLocalVar != NULL)
  2919. {
  2920. /*if (localVar->mLocationData == NULL)
  2921. localVar->mLocationData = dataStart;
  2922. localVar->mLocationLen++;*/
  2923. if (locationDataStart == NULL)
  2924. locationDataStart = dataStart;
  2925. locationDataEnd = dataEnd;
  2926. locationDataCount++;
  2927. if (localVar->mLocationData == NULL)
  2928. localVar->mLocationLen++;
  2929. }
  2930. }
  2931. break;
  2932. }
  2933. }
  2934. if (hasNewLocalVar)
  2935. {
  2936. if ((localVar->mName != NULL) && (localVar->mName[0] == '$'))
  2937. {
  2938. char* aliasPos = (char*)strstr(localVar->mName, "$alias$");
  2939. // This $alias$ hack is unfortunate, but LLVM gets confused when we attempt to tie multiple debug variables
  2940. // to the same memory location, which can happen during mixin injection. The result is that the mixin gets
  2941. // some premature instructions attributed to it from the variable declaration. This fixes that.
  2942. if ((aliasPos != NULL) && (aliasPos > localVar->mName))
  2943. {
  2944. String findName = String(aliasPos + 7);
  2945. localVar->mName = CvDupString(localVar->mName + 1, aliasPos - localVar->mName - 1);
  2946. auto curBlock = blockStack.back();
  2947. localVar->mLocationData = (uint8*)CvDupString(aliasPos + 7, strlen(aliasPos + 7));
  2948. localVar->mRangeStart = curBlock->mLowPC;
  2949. localVar->mRangeLen = curBlock->mHighPC - curBlock->mLowPC;
  2950. /*bool found = false;
  2951. auto _CheckBlock = [&](DbgBlock* block)
  2952. {
  2953. for (auto checkVar : block->mVariables)
  2954. {
  2955. if (checkVar->mName == NULL)
  2956. continue;
  2957. if (findName == checkVar->mName)
  2958. {
  2959. localVar->mConstValue = checkVar->mConstValue;
  2960. localVar->mType = checkVar->mType;
  2961. localVar->mLocationData = checkVar->mLocationData;
  2962. localVar->mLocationLen = checkVar->mLocationLen;
  2963. found = true;
  2964. return true;
  2965. }
  2966. }
  2967. return false;
  2968. };
  2969. for (int blockIdx = (int)blockStack.size() - 1; blockIdx >= 0; blockIdx--)
  2970. {
  2971. DbgBlock* block = blockStack[blockIdx];
  2972. if (_CheckBlock(block))
  2973. break;
  2974. }
  2975. if (!found)
  2976. {
  2977. addr_target checkAddr = 0;
  2978. DbgSubprogram* checkSubprogram = curSubprogram;
  2979. while (checkSubprogram != NULL)
  2980. {
  2981. auto checkAddr = checkSubprogram->mBlock.mLowPC;
  2982. if (checkSubprogram->mInlineeInfo == NULL)
  2983. break;
  2984. checkSubprogram = checkSubprogram->mInlineeInfo->mInlineParent;
  2985. std::function<void(DbgBlock*)> _RecurseBlock = [&](DbgBlock* block)
  2986. {
  2987. if ((checkAddr < block->mLowPC) || (checkAddr >= block->mHighPC))
  2988. return;
  2989. if (_CheckBlock(block))
  2990. return;
  2991. for (auto block : block->mSubBlocks)
  2992. _RecurseBlock(block);
  2993. };
  2994. _RecurseBlock(&checkSubprogram->mBlock);
  2995. }
  2996. }*/
  2997. }
  2998. }
  2999. if ((compileUnit->mLanguage != DbgLanguage_Beef) && (localVar->mName != NULL))
  3000. {
  3001. for (char* cPtr = (char*)localVar->mName; true; cPtr++)
  3002. {
  3003. char c = *cPtr;
  3004. if (c == 0)
  3005. break;
  3006. if ((c == '<') || (c == '>'))
  3007. *cPtr = '$';
  3008. }
  3009. }
  3010. }
  3011. /*if (prevLocalVar != NULL)
  3012. {
  3013. BF_ASSERT(locationDataCount == prevLocalVar->mLocationLen);
  3014. }*/
  3015. data = dataEnd;
  3016. //PTR_ALIGN(data, sectionData, 4);
  3017. }
  3018. if (localVar != NULL)
  3019. {
  3020. if (locationDataStart != NULL)
  3021. {
  3022. _FinishLocationData();
  3023. }
  3024. }
  3025. _FlushDeferredVariableLocations();
  3026. }
  3027. CvCompileUnit* COFF::ParseCompileUnit(CvModuleInfo* moduleInfo, CvCompileUnit* compileUnit, uint8* sectionData, int sectionSize)
  3028. {
  3029. BP_ZONE("COFF::ParseCompileUnit");
  3030. CvInlineInfoVec inlineDataVec;
  3031. Dictionary<uint32, CvInlineInfo*> inlineDataDict;
  3032. if (moduleInfo != NULL)
  3033. {
  3034. BfLogDbg("ParseCompileUnit %s %s\n", mPDBPath.c_str(), moduleInfo->mModuleName);
  3035. }
  3036. else
  3037. {
  3038. BfLogDbg("ParseCompileUnit %s NULL\n", mPDBPath.c_str());
  3039. }
  3040. int allocSizeStart = mAlloc.GetAllocSize();
  3041. if (compileUnit == NULL)
  3042. {
  3043. compileUnit = new CvCompileUnit(this);
  3044. mCompileUnits.push_back(compileUnit);
  3045. }
  3046. compileUnit->mDbgModule = this;
  3047. if (moduleInfo != NULL)
  3048. {
  3049. compileUnit->mModuleIdx = moduleInfo->mIdx;
  3050. compileUnit->mName = moduleInfo->mModuleName;
  3051. moduleInfo->mCompileUnit = compileUnit;
  3052. }
  3053. else
  3054. {
  3055. compileUnit->mModuleIdx = NULL;
  3056. compileUnit->mName = mFilePath.c_str();
  3057. }
  3058. uint8* data = sectionData;
  3059. uint8* dataEnd = NULL;
  3060. GET_INTO(uint32, infoType);
  3061. BF_ASSERT(infoType == CV_SIGNATURE_C13);
  3062. int taggedSize = sectionSize;
  3063. uint8* debugSubSectionsStart = data;
  3064. if (moduleInfo != NULL)
  3065. {
  3066. taggedSize = moduleInfo->mLinesBytes;
  3067. debugSubSectionsStart = data + moduleInfo->mSymbolBytes - sizeof(uint32);
  3068. }
  3069. else
  3070. {
  3071. taggedSize = sectionSize - sizeof(uint32);
  3072. }
  3073. if (mStringTable.mStrTable == NULL)
  3074. {
  3075. data = debugSubSectionsStart;
  3076. dataEnd = data + taggedSize;
  3077. while (data < dataEnd)
  3078. {
  3079. PTR_ALIGN(data, sectionData, 4);
  3080. GET_INTO(int32, lineInfoType);
  3081. GET_INTO(int32, lineInfoLength);
  3082. uint8* dataStart = data;
  3083. uint8* dataEnd = data + lineInfoLength;
  3084. if (lineInfoType == DEBUG_S_STRINGTABLE)
  3085. {
  3086. if (mStringTable.mStrTable == NULL)
  3087. mStringTable.mStrTable = (const char*)data;
  3088. }
  3089. data = dataEnd;
  3090. }
  3091. }
  3092. //
  3093. {
  3094. data = debugSubSectionsStart;
  3095. dataEnd = data + taggedSize;
  3096. while (data < dataEnd)
  3097. {
  3098. PTR_ALIGN(data, sectionData, 4);
  3099. GET_INTO(int32, lineInfoType);
  3100. GET_INTO(int32, lineInfoLength);
  3101. uint8* dataStart = data;
  3102. uint8* dataEnd = data + lineInfoLength;
  3103. if (lineInfoType == DEBUG_S_CROSSSCOPEIMPORTS)
  3104. {
  3105. while (data < dataEnd)
  3106. {
  3107. compileUnit->mImports.Add(CvCrossScopeImport());
  3108. auto& crossScopeImport = compileUnit->mImports.back();
  3109. crossScopeImport.mScopeCompileUnit = NULL;
  3110. GET_INTO(uint32, externalScope);
  3111. const char* fileName = mStringTable.mStrTable + externalScope;
  3112. BfLogCv("DEBUG_S_CROSSSCOPEIMPORTS Scope:%s ", fileName);
  3113. crossScopeImport.mScopeName = fileName;
  3114. //
  3115. /*for (int checkIdx = 0; checkIdx < (int)mCvModuleInfo.size(); checkIdx++)
  3116. {
  3117. auto checkModuleInfo = mCvModuleInfo[checkIdx];
  3118. if ((checkModuleInfo != NULL) && (checkModuleInfo->mModuleName != NULL) && (stricmp(checkModuleInfo->mModuleName, fileName) == 0))
  3119. {
  3120. ParseCompileUnit(checkIdx);
  3121. }
  3122. }*/
  3123. GET_INTO(int32, numRefs);
  3124. for (int i = 0; i < numRefs; i++)
  3125. {
  3126. GET_INTO(uint32, refId);
  3127. BfLogCv(" %X", refId);
  3128. crossScopeImport.mImports.Add(refId);
  3129. }
  3130. BfLogCv("\n");
  3131. }
  3132. }
  3133. else if (lineInfoType == DEBUG_S_CROSSSCOPEEXPORTS)
  3134. {
  3135. BfLogCv("DEBUG_S_CROSSSCOPEEXPORTS");
  3136. while (data < dataEnd)
  3137. {
  3138. GET_INTO(int32, localId);
  3139. GET_INTO(int32, globalId);
  3140. BfLogCv(" %X=%X", globalId, localId);
  3141. CvCrossScopeExportEntry entry;
  3142. entry.mLocalId = localId;
  3143. entry.mGlobalId = globalId;
  3144. compileUnit->mExports.Add(entry);
  3145. }
  3146. BfLogCv("\n");
  3147. }
  3148. else if (lineInfoType == DEBUG_S_FRAMEDATA)
  3149. {
  3150. uint8* dataPtr = data;
  3151. addr_target baseAddr = 0;
  3152. int relAddr = GET_FROM(dataPtr, int32);
  3153. ParseFrameDescriptors(dataPtr, dataEnd - dataPtr, GetLinkedModule()->mImageBase + relAddr);
  3154. }
  3155. data = dataEnd;
  3156. }
  3157. }
  3158. if (moduleInfo != NULL)
  3159. {
  3160. data = sectionData + 4;
  3161. dataEnd = data + moduleInfo->mSymbolBytes - sizeof(uint32);
  3162. bool wantsDeferInternals = true;
  3163. ParseCompileUnit_Symbols(compileUnit, sectionData, data, dataEnd, inlineDataVec, wantsDeferInternals, NULL);
  3164. //ParseCompileUnit_Symbols(compileUnit, sectionData, data, dataEnd, inlineDataVec, false, NULL);
  3165. data = dataEnd;
  3166. }
  3167. // Scan debug subsections to find file checksum table, and to load symbol data for hotloads
  3168. Dictionary<int, int> checksumFileRefs;
  3169. {
  3170. BP_ZONE("ParseCompileUnit_SrcFiles");
  3171. data = debugSubSectionsStart;
  3172. dataEnd = data + taggedSize;
  3173. while (true)
  3174. {
  3175. PTR_ALIGN(data, sectionData, 4);
  3176. if (data >= dataEnd)
  3177. break;
  3178. GET_INTO(int32, lineInfoType);
  3179. GET_INTO(int32, lineInfoLength);
  3180. uint8* dataStart = data;
  3181. uint8* dataEnd = data + lineInfoLength;
  3182. if (lineInfoType < 0)
  3183. {
  3184. // Ignore
  3185. }
  3186. else if (lineInfoType == DEBUG_S_SYMBOLS)
  3187. {
  3188. ParseCompileUnit_Symbols(compileUnit, sectionData, data, dataEnd, inlineDataVec, false, NULL);
  3189. }
  3190. else if (lineInfoType == DEBUG_S_FILECHKSMS)
  3191. {
  3192. BF_ASSERT(checksumFileRefs.size() == 0);
  3193. while (data < dataEnd)
  3194. {
  3195. int dataOfs = (int)(data - dataStart);
  3196. GET_INTO(uint, fileTableOfs);
  3197. const char* fileName = mStringTable.mStrTable + fileTableOfs;
  3198. if ((fileName[0] == '\\') && (fileName[1] == '$'))
  3199. fileName++;
  3200. DbgSrcFile* srcFile = NULL;
  3201. if (fileName[0] == '$')
  3202. {
  3203. srcFile = AddSrcFile(compileUnit, fileName);
  3204. }
  3205. else if ((fileName[0] == '/') || (fileName[0] == '\\') ||
  3206. ((fileName[0] != 0) && (fileName[1] == ':')))
  3207. {
  3208. srcFile = AddSrcFile(compileUnit, fileName);
  3209. }
  3210. else
  3211. {
  3212. String fullDir = GetFileDir(mFilePath);
  3213. fullDir.Append("\\");
  3214. fullDir.Append(fileName);
  3215. srcFile = AddSrcFile(compileUnit, fullDir);
  3216. }
  3217. if (srcFile->IsBeef())
  3218. compileUnit->mLanguage = DbgLanguage_Beef;
  3219. GET_INTO(uint8, hashLen);
  3220. GET_INTO(uint8, hashType);
  3221. if ((hashType == 1) && (hashLen == 16))
  3222. {
  3223. srcFile->mHashKind = DbgHashKind_MD5;
  3224. memcpy(srcFile->mHash, data, 16);
  3225. }
  3226. else if ((hashType == 3) && (hashLen == 32))
  3227. {
  3228. srcFile->mHashKind = DbgHashKind_SHA256;
  3229. memcpy(srcFile->mHash, data, 32);
  3230. }
  3231. data += hashLen;
  3232. checksumFileRefs.TryAdd(dataOfs, (int)compileUnit->mSrcFileRefs.size() - 1);
  3233. PTR_ALIGN(data, sectionData, 4);
  3234. }
  3235. }
  3236. else if (lineInfoType == 255)
  3237. {
  3238. // Ignore
  3239. }
  3240. else
  3241. {
  3242. BF_ASSERT((lineInfoType >= DEBUG_S_SYMBOLS) && (lineInfoType <= DEBUG_S_COFF_SYMBOL_RVA));
  3243. }
  3244. data = dataEnd;
  3245. }
  3246. }
  3247. if (!inlineDataVec.IsEmpty())
  3248. {
  3249. for (auto& inlineData : inlineDataVec)
  3250. {
  3251. uint32* keyPtr;
  3252. CvInlineInfo** valuePtr;
  3253. if (inlineDataDict.TryAdd(inlineData.mInlinee, &keyPtr, &valuePtr))
  3254. {
  3255. inlineData.mTail = &inlineData;
  3256. *valuePtr = &inlineData;
  3257. }
  3258. else
  3259. {
  3260. (*valuePtr)->mTail->mNext = &inlineData;
  3261. (*valuePtr)->mTail = &inlineData;
  3262. }
  3263. }
  3264. }
  3265. int inlineDataIdx = 0;
  3266. DbgLineDataBuilder lineBuilder(this);
  3267. //
  3268. {
  3269. BP_ZONE("ParseCompileUnit_LineInfo");
  3270. //int totalLineCount = 0;
  3271. //int targetLineCount = 0;
  3272. int inlineIdx = 0;
  3273. // struct _InlinerData
  3274. // {
  3275. // bool mWantSummaryDump;
  3276. // Array<DbgInlinee> mInlinees;
  3277. // Array<DbgInlineLineData> mInlineLineData;
  3278. //
  3279. // _InlinerData()
  3280. // {
  3281. // mWantSummaryDump = false;
  3282. // }
  3283. // };
  3284. //
  3285. // Dictionary<DbgSubprogram*, _InlinerData> deferredInlinerDatas;
  3286. // Line info
  3287. data = debugSubSectionsStart;
  3288. dataEnd = data + taggedSize;
  3289. while (data < dataEnd)
  3290. {
  3291. PTR_ALIGN(data, sectionData, 4);
  3292. GET_INTO(int32, lineInfoType);
  3293. GET_INTO(int32, lineInfoLength);
  3294. uint8* dataStart = data;
  3295. uint8* dataEnd = data + lineInfoLength;
  3296. if (lineInfoType == DEBUG_S_FILECHKSMS)
  3297. {
  3298. // Already handled
  3299. }
  3300. else if (lineInfoType == DEBUG_S_LINES)
  3301. {
  3302. CV_DebugSLinesHeader_t& lineSec = GET(CV_DebugSLinesHeader_t);
  3303. Array<DbgLineData> lineDataVec;
  3304. addr_target contribStartAddr = GetSectionAddr(lineSec.segCon, lineSec.offCon);
  3305. while (data < dataEnd)
  3306. {
  3307. lineDataVec.Clear();
  3308. CV_DebugSLinesFileBlockHeader_t& linesFileHeader = GET(CV_DebugSLinesFileBlockHeader_t);
  3309. DbgSrcFileReference* srcFileRef = &compileUnit->mSrcFileRefs[checksumFileRefs[linesFileHeader.offFile]];
  3310. bool isBeef = srcFileRef->mSrcFile->IsBeef();
  3311. int expectSize = (int)sizeof(CV_DebugSLinesFileBlockHeader_t) + (int)(linesFileHeader.nLines * sizeof(CV_Line_t));
  3312. if ((lineSec.flags & CV_LINES_HAVE_COLUMNS) != 0)
  3313. expectSize += (int)(linesFileHeader.nLines * sizeof(int16) * 2);
  3314. BF_ASSERT(expectSize == linesFileHeader.cbBlock);
  3315. lineDataVec.Resize(linesFileHeader.nLines);
  3316. for (int lineIdx = 0; lineIdx < linesFileHeader.nLines; lineIdx++)
  3317. {
  3318. CV_Line_t& srcLineData = GET(CV_Line_t);
  3319. DbgLineData& lineData = lineDataVec[lineIdx];
  3320. lineData.mRelAddress = (uint32)((contribStartAddr + srcLineData.offset) - mImageBase);
  3321. if (srcLineData.linenumStart == 0xf00f00) // Never step into
  3322. {
  3323. if (lineIdx > 0)
  3324. lineData.mLine = lineDataVec[lineIdx - 1].mLine;
  3325. lineData.mColumn = -2; // Never step into marker
  3326. }
  3327. else if (srcLineData.linenumStart == 0xfeefee) // Always step into
  3328. {
  3329. if (lineIdx > 0)
  3330. lineData.mLine = lineDataVec[lineIdx - 1].mLine;
  3331. lineData.mColumn = -1; // Always step into marker
  3332. }
  3333. else
  3334. lineData.mLine = srcLineData.linenumStart - 1;
  3335. // In Beef we always set the column, so a section without CV_LINES_HAVE_COLUMNS indicates a block of invalid
  3336. // positions that we want to skip over
  3337. if (isBeef)
  3338. lineData.mColumn = -1;
  3339. }
  3340. if ((lineSec.flags & CV_LINES_HAVE_COLUMNS) != 0)
  3341. {
  3342. for (int lineIdx = 0; lineIdx < linesFileHeader.nLines; lineIdx++)
  3343. {
  3344. CV_Column_t& srcColumnData = GET(CV_Column_t);
  3345. DbgLineData& lineData = lineDataVec[lineIdx];
  3346. lineData.mColumn = (int)srcColumnData.offColumnStart - 1;
  3347. }
  3348. }
  3349. DbgLineData* lastLineData = NULL;
  3350. for (int lineIdx = 0; lineIdx < linesFileHeader.nLines; lineIdx++)
  3351. {
  3352. DbgLineData& lineData = lineDataVec[lineIdx];
  3353. lastLineData = lineBuilder.Add(compileUnit, lineData, srcFileRef->mSrcFile, NULL);
  3354. }
  3355. }
  3356. }
  3357. else if (lineInfoType == DEBUG_S_INLINEELINES)
  3358. {
  3359. int linesType = GET(int);
  3360. while (data < dataEnd)
  3361. {
  3362. int inlinee = 0;
  3363. int srcFileIdx = -1;
  3364. int startLine;
  3365. DbgSrcFileReference* startSrcFileRef = NULL;
  3366. if (linesType == 0)
  3367. {
  3368. CodeViewInfo::InlineeSourceLine& lineInfo = GET(CodeViewInfo::InlineeSourceLine);
  3369. srcFileIdx = checksumFileRefs[lineInfo.fileId];
  3370. startSrcFileRef = &compileUnit->mSrcFileRefs[srcFileIdx];
  3371. startLine = lineInfo.sourceLineNum;
  3372. inlinee = lineInfo.inlinee;
  3373. }
  3374. else if (linesType == 1)
  3375. {
  3376. CodeViewInfo::InlineeSourceLineEx& lineInfo = *(CodeViewInfo::InlineeSourceLineEx*)data;
  3377. srcFileIdx = checksumFileRefs[lineInfo.fileId];
  3378. startSrcFileRef = &compileUnit->mSrcFileRefs[srcFileIdx];
  3379. startLine = lineInfo.sourceLineNum;
  3380. inlinee = lineInfo.inlinee;
  3381. data += sizeof(CodeViewInfo::InlineeSourceLineEx);
  3382. data += (int)sizeof(CV_off32_t) * lineInfo.countOfExtraFiles;
  3383. }
  3384. else
  3385. BF_FATAL("Invalid DEBUG_S_INLINEELINES lines type");
  3386. CvInlineInfo* inlineData = NULL;
  3387. inlineDataDict.TryGetValue(inlinee, &inlineData);
  3388. while (inlineData != NULL)
  3389. {
  3390. bool inlineDebugDump = inlineData->mDebugDump;
  3391. if (inlineData->mInlinee == -1)
  3392. {
  3393. if (inlineDebugDump)
  3394. BfLogCv("Duplicate data: %X\n", inlinee);
  3395. break;
  3396. }
  3397. // Mark as done
  3398. inlineData->mInlinee = -1;
  3399. int chunkNum = 0;
  3400. int curLine = startLine;
  3401. int curValidLine = curLine;
  3402. bool flushOnLineOffset = false;
  3403. addr_target lastLineAddr = 0;
  3404. DbgSrcFileReference* srcFileRef = startSrcFileRef;
  3405. DbgSubprogram* curSubprogram = inlineData->mSubprogram;
  3406. DbgSubprogram* inlineParent = inlineData->mSubprogram->GetRootInlineParent();
  3407. addr_target curAddr = inlineParent->mBlock.mLowPC;
  3408. lastLineAddr = 0;
  3409. if (inlineDebugDump)
  3410. {
  3411. //inlinerData->mWantSummaryDump = true;
  3412. BfLogCv("------------------------------------------------\nINLINE DATA:%X Idx:%d CurAddr:%@ Line:%d\n SubProgram:%@ %s\n File:%s\n", inlinee, inlineIdx, curAddr, curLine,
  3413. curSubprogram, curSubprogram->mName, srcFileRef->mSrcFile->mFilePath.c_str());
  3414. }
  3415. uint8* annData = inlineData->mData;
  3416. uint8* annDataEnd = inlineData->mData + inlineData->mDataLen;
  3417. DbgLineData* curLineData = NULL;
  3418. auto _AddLine = [&]()
  3419. {
  3420. if (chunkNum != 0)
  3421. {
  3422. curLineData = NULL;
  3423. return;
  3424. }
  3425. DbgLineData lineData;
  3426. lineData.mLine = curValidLine - 1;
  3427. lineData.mRelAddress = (uint32)(curAddr - mImageBase);
  3428. lineData.mColumn = 0;
  3429. if (curLine <= 0) // Negative lines mean "invalid position" for inlining
  3430. lineData.mColumn = -1;
  3431. lineData.mContribSize = 0;
  3432. if ((curSubprogram->mBlock.mLowPC == 0) && (curLineData != NULL))
  3433. curSubprogram->mBlock.mLowPC = mImageBase + curLineData->mRelAddress;
  3434. if (inlineDebugDump)
  3435. BfLogCv(" Adding Line:%d Addr:%@\n", lineData.mLine + 1, lineData.mRelAddress + mImageBase);
  3436. curLineData = lineBuilder.Add(compileUnit, lineData, srcFileRef->mSrcFile, curSubprogram);
  3437. };
  3438. int codeIdx = 0;
  3439. while (annData < annDataEnd)
  3440. {
  3441. auto annVal = CodeViewInfo::CVUncompressData(annData);
  3442. switch (annVal)
  3443. {
  3444. case CodeViewInfo::BA_OP_Invalid: // link time pdb contains PADDINGs
  3445. break;
  3446. case CodeViewInfo::BA_OP_CodeOffset: // param : start offset
  3447. {
  3448. int32 offset = (int32)CodeViewInfo::CVUncompressData(annData);
  3449. curAddr = inlineParent->mBlock.mLowPC + offset;
  3450. if (inlineDebugDump)
  3451. BfLogCv(" BA_OP_CodeOffset(%d) CurAddr:%@ Line:%d\n", offset, curAddr, curLine);
  3452. }
  3453. break;
  3454. case CodeViewInfo::BA_OP_ChangeCodeOffsetBase: // param : nth separated code chunk (main code chunk == 0)
  3455. {
  3456. chunkNum = (int32)CodeViewInfo::CVUncompressData(annData);
  3457. if (inlineDebugDump)
  3458. BfLogCv("xxxxxxxxx BA_OP_ChangeCodeOffsetBase(%d) CurAddr:%@ Line:%d\n", chunkNum, curAddr, curLine);
  3459. }
  3460. break;
  3461. case CodeViewInfo::BA_OP_ChangeCodeOffset: // param : delta of offset
  3462. {
  3463. int32 offset = (int32)CodeViewInfo::CVUncompressData(annData);
  3464. curAddr += offset;
  3465. if (inlineDebugDump)
  3466. BfLogCv(" BA_OP_ChangeCodeOffset(%d) CurAddr:%@ Line:%d\n", offset, curAddr, curLine);
  3467. if ((curLineData != NULL) && (curLineData->mContribSize == 0))
  3468. {
  3469. curLineData->mRelAddress = (uint32)(curAddr - mImageBase);
  3470. if (inlineDebugDump)
  3471. BfLogCv(" Setting addr on previous entry\n");
  3472. }
  3473. else
  3474. _AddLine();
  3475. flushOnLineOffset = true;
  3476. }
  3477. break;
  3478. case CodeViewInfo::BA_OP_ChangeCodeLength: // param : length of code, default next start
  3479. {
  3480. int newCodeLen = (int32)CodeViewInfo::CVUncompressData(annData);
  3481. curAddr += newCodeLen;
  3482. if (curLineData != NULL)
  3483. curLineData->mContribSize = newCodeLen;
  3484. if (inlineDebugDump)
  3485. BfLogCv(" BA_OP_ChangeCodeLength(%d) Addr:%@ EndAddr:%@\n", newCodeLen, curLineData->mRelAddress + mImageBase, curLineData->mRelAddress + mImageBase + curLineData->mContribSize);
  3486. }
  3487. break;
  3488. case CodeViewInfo::BA_OP_ChangeFile: // param : fileId
  3489. {
  3490. uint32 newFileId = CodeViewInfo::CVUncompressData(annData);
  3491. srcFileRef = &compileUnit->mSrcFileRefs[checksumFileRefs[newFileId]];
  3492. if (inlineDebugDump)
  3493. BfLogCv(" BA_OP_ChangeFile(%s) CurAddr:%@ Line:%d\n", srcFileRef->mSrcFile->mFilePath.c_str(), curAddr, curLine);
  3494. }
  3495. break;
  3496. case CodeViewInfo::BA_OP_ChangeLineOffset: // param : line offset (signed)
  3497. {
  3498. int32 lineOfs = (int32)CodeViewInfo::DecodeSignedInt32(CodeViewInfo::CVUncompressData(annData));
  3499. curLine += lineOfs;
  3500. if (curLine > 0)
  3501. curValidLine = curLine;
  3502. if (inlineDebugDump)
  3503. BfLogCv(" BA_OP_ChangeLineOffset(%d) CurAddr:%@ Line:%d\n", lineOfs, curAddr, curLine);
  3504. _AddLine();
  3505. }
  3506. break;
  3507. case CodeViewInfo::BA_OP_ChangeLineEndDelta: // param : how many lines, default 1
  3508. {
  3509. int numLines = (int32)CodeViewInfo::CVUncompressData(annData);
  3510. if (inlineDebugDump)
  3511. BfLogCv(" BA_OP_ChangeLineEndDelta(%d) CurAddr:%@ Line:%d\n", numLines, curAddr, curLine);
  3512. }
  3513. break;
  3514. case CodeViewInfo::BA_OP_ChangeRangeKind: // param : either 1 (default, for statement) or 0 (for expression)
  3515. {
  3516. int32 readKind = (int32)CodeViewInfo::CVUncompressData(annData);
  3517. if (inlineDebugDump)
  3518. BfLogCv(" BA_OP_ChangeRangeKind(%d) CurAddr:%@ Line:%d\n", readKind, curAddr, curLine);
  3519. }
  3520. break;
  3521. case CodeViewInfo::BA_OP_ChangeColumnStart: // param : start column number, 0 means no column info
  3522. {
  3523. int column = (int32)CodeViewInfo::CVUncompressData(annData);
  3524. if (curLineData != NULL)
  3525. curLineData->mColumn = column;
  3526. if (inlineDebugDump)
  3527. BfLogCv(" BA_OP_ChangeColumnStart(%d) CurAddr:%@ Line:%d\n", column, curAddr, curLine);
  3528. }
  3529. break;
  3530. case CodeViewInfo::BA_OP_ChangeColumnEndDelta: // param : end column number delta (signed)
  3531. {
  3532. int columnEndDelta = (int32)CodeViewInfo::DecodeSignedInt32(CodeViewInfo::CVUncompressData(annData));
  3533. if (inlineDebugDump)
  3534. BfLogCv(" BA_OP_ChangeColumnEndDelta(%d) CurAddr:%@ Line:%d\n", columnEndDelta, curAddr, curLine);
  3535. }
  3536. break;
  3537. // Combo opcodes for smaller encoding size.
  3538. case CodeViewInfo::BA_OP_ChangeCodeOffsetAndLineOffset: // param : ((sourceDelta << 4) | CodeDelta)
  3539. {
  3540. int offsetData = (int32)CodeViewInfo::CVUncompressData(annData);
  3541. int codeDelta = offsetData & 0xF;
  3542. int sourceDelta = CodeViewInfo::DecodeSignedInt32(offsetData >> 4);
  3543. curAddr += codeDelta;
  3544. curLine += sourceDelta;
  3545. if (curLine > 0)
  3546. curValidLine = curLine;
  3547. if (inlineDebugDump)
  3548. BfLogCv(" BA_OP_ChangeCodeOffsetAndLineOffset(%d, %d) CurAddr:%@ Line:%d\n", codeDelta, sourceDelta, curAddr, curLine);
  3549. _AddLine();
  3550. }
  3551. break;
  3552. case CodeViewInfo::BA_OP_ChangeCodeLengthAndCodeOffset: // param : codeLength, codeOffset
  3553. {
  3554. curValidLine = curLine;
  3555. int codeLen = (int32)CodeViewInfo::CVUncompressData(annData);
  3556. int codeOffset = (int32)CodeViewInfo::CVUncompressData(annData);
  3557. curAddr += codeOffset;
  3558. if (inlineDebugDump)
  3559. BfLogCv(" BA_OP_ChangeCodeLengthAndCodeOffset(%d, %d) CurAddr:%@ Line:%d\n", codeLen, codeOffset, curAddr, curLine);
  3560. _AddLine();
  3561. if (curLineData != NULL)
  3562. {
  3563. if (inlineDebugDump)
  3564. BfLogCv(" ContribSize:%d\n", codeLen);
  3565. curLineData->mContribSize = codeLen;
  3566. }
  3567. }
  3568. break;
  3569. case CodeViewInfo::BA_OP_ChangeColumnEnd: // param : end column number
  3570. {
  3571. int columnEnd = (int32)CodeViewInfo::CVUncompressData(annData);
  3572. if (inlineDebugDump)
  3573. BfLogCv(" BA_OP_ChangeColumnEnd(%d) CurAddr:%@ Line:%d\n", columnEnd, curAddr, curLine);
  3574. }
  3575. break;
  3576. }
  3577. codeIdx++;
  3578. }
  3579. if (curLineData != NULL)
  3580. {
  3581. if (curSubprogram->mBlock.mLowPC == 0)
  3582. curSubprogram->mBlock.mLowPC = mImageBase + curLineData->mRelAddress;
  3583. curSubprogram->mBlock.mHighPC = mImageBase + curLineData->mRelAddress + curLineData->mContribSize;
  3584. }
  3585. inlineData = inlineData->mNext;
  3586. }
  3587. }
  3588. }
  3589. data = dataEnd;
  3590. }
  3591. // for (auto& deferredInlinerDataKV : deferredInlinerDatas)
  3592. // {
  3593. // auto inlinerParent = deferredInlinerDataKV.mKey;
  3594. // auto deferredInlinerData = &deferredInlinerDataKV.mValue;
  3595. //
  3596. // inlinerParent->mInlinerData = mAlloc.Alloc<DbgInlinerData>();
  3597. // inlinerParent->mInlinerData->mInlinees.CopyFrom(deferredInlinerData->mInlinees.mVals, deferredInlinerData->mInlinees.mSize, mAlloc);
  3598. // inlinerParent->mInlinerData->mInlineLineData.CopyFrom(deferredInlinerData->mInlineLineData.mVals, deferredInlinerData->mInlineLineData.mSize, mAlloc);
  3599. // //deferredInlinerData->
  3600. //
  3601. // if (deferredInlinerData->mWantSummaryDump)
  3602. // {
  3603. // BfLogCv("------------------------------\nSUMMARY FOR: %s\n", inlinerParent->mName);
  3604. //
  3605. // for (auto& inlineLine : inlinerParent->mInlinerData->mInlineLineData)
  3606. // {
  3607. // BfLogCv("Addr:%@ EndAddr:%@ Line:%d Column:%d Inlinee:%d\n", inlineLine.mAddress, inlineLine.mAddress + inlineLine.mContribSize,
  3608. // inlineLine.mLine + 1, inlineLine.mColumn + 1, inlineLine.mInlineIdx);
  3609. // }
  3610. // }
  3611. // }
  3612. }
  3613. lineBuilder.Commit();
  3614. //OutputDebugStrF("Module loaded, AllocSize added: %d\n", (mAlloc.GetAllocSize() - allocSizeStart) / 1024);
  3615. return compileUnit;
  3616. }
  3617. CvCompileUnit* COFF::ParseCompileUnit(int compileUnitId)
  3618. {
  3619. CvModuleInfo* moduleInfo = mCvModuleInfo[compileUnitId];
  3620. if (moduleInfo->mCompileUnit != NULL)
  3621. return moduleInfo->mCompileUnit;
  3622. BP_ZONE("ParseCompileUnit");
  3623. ParseTypeData();
  3624. if (moduleInfo->mStream == -1)
  3625. return NULL;
  3626. int sectionSize = 0;
  3627. uint8* sectionData = CvReadStream(moduleInfo->mStream, &sectionSize);
  3628. if (sectionData == NULL)
  3629. return NULL;
  3630. ParseCompileUnit(moduleInfo, NULL, sectionData, sectionSize);
  3631. delete sectionData;
  3632. return moduleInfo->mCompileUnit;
  3633. }
  3634. void COFF::ParseCompileUnits()
  3635. {
  3636. for (int i = 0; i < (int)mCvModuleInfo.size(); i++)
  3637. ParseCompileUnit(i);
  3638. }
  3639. DbgType* COFF::CvGetTypeOrNamespace(char* name, DbgLanguage language)
  3640. {
  3641. if (language == DbgLanguage_Unknown)
  3642. {
  3643. if (strncmp(name, "_bf::", 5) == 0)
  3644. {
  3645. language = DbgLanguage_Beef;
  3646. name += 5;
  3647. }
  3648. else if ((name[0] == 'G') && (name[1] == '$'))
  3649. {
  3650. language = DbgLanguage_Beef;
  3651. }
  3652. else
  3653. {
  3654. language = DbgLanguage_C;
  3655. // Check for a primitive array type like 'double[]'
  3656. for (int i = 0; i < 10; i++)
  3657. {
  3658. char c = name[i];
  3659. if (c == 0)
  3660. break;
  3661. if (c == '[')
  3662. language = DbgLanguage_Beef;
  3663. }
  3664. }
  3665. }
  3666. auto linkedModule = GetLinkedModule();
  3667. auto dbgTypeEntry = linkedModule->mTypeMap.Find(name, language);
  3668. if (dbgTypeEntry != NULL)
  3669. {
  3670. auto dbgType = dbgTypeEntry->mValue;
  3671. if (dbgType->mHotNewType != NULL)
  3672. return dbgType->mHotNewType;
  3673. return dbgType;
  3674. }
  3675. // It's possible that we get a reference to a template name that isn't actually in our type database
  3676. // These may indicate VS compiler bugs or anonymous namespaces
  3677. bool isValidName = true;
  3678. for (const char* cPtr = name; *cPtr != '\0'; cPtr++)
  3679. if ((*cPtr == '<') || (*cPtr == '`') || (*cPtr == '$'))
  3680. isValidName = false;
  3681. if (!isValidName)
  3682. return NULL;
  3683. //OutputDebugStrF("CvGetTypeOrNamespace Creating: %s\n", name);
  3684. char* lastDblColon = (char*)GetLastDoubleColon(name);
  3685. DbgType* dbgType = CvCreateType();
  3686. DbgType* parentType = mMasterCompileUnit->mGlobalType;
  3687. if (lastDblColon != NULL)
  3688. {
  3689. *lastDblColon = 0;
  3690. parentType = CvGetTypeOrNamespace(name, language);
  3691. dbgType->mTypeName = DbgDupString(lastDblColon + 2, "DbgDupString.TypeOrNamespace0");
  3692. *lastDblColon = ':';
  3693. dbgType->mName = DbgDupString(name, "DbgDupString.TypeOrNamespace1");
  3694. }
  3695. else
  3696. {
  3697. dbgType->mTypeName = DbgDupString(name, "DbgDupString.TypeOrNamespace2");
  3698. dbgType->mName = dbgType->mTypeName;
  3699. }
  3700. parentType->mSubTypeList.PushBack(dbgType);
  3701. dbgType->mTypeCode = DbgType_Namespace;
  3702. dbgType->mLanguage = language;
  3703. linkedModule->mTypeMap.Insert(dbgType);
  3704. return dbgType;
  3705. }
  3706. void COFF::MapCompileUnitMethods(DbgCompileUnit* compileUnit)
  3707. {
  3708. bool addHotTypes = (IsObjectFile()) && (mHotPrimaryTypes.size() == 0);
  3709. DbgSubprogram* prevDbgMethod = NULL;
  3710. DbgSubprogram* dbgMethod = compileUnit->mOrphanMethods.mHead;
  3711. while (dbgMethod != NULL)
  3712. {
  3713. auto nextDbgMethod = dbgMethod->mNext;
  3714. bool movedMethod = false;
  3715. if (dbgMethod->mHasQualifiedName)
  3716. {
  3717. char* name = (char*)dbgMethod->mName;
  3718. char* lastDblColon = (char*)GetLastDoubleColon(name);
  3719. if (lastDblColon != NULL)
  3720. {
  3721. dbgMethod->mName = lastDblColon + 2;
  3722. dbgMethod->mHasQualifiedName = false;
  3723. const char* typeName = name;
  3724. *lastDblColon = 0;
  3725. DbgType* parentType = CvGetTypeOrNamespace(name);
  3726. if (parentType != NULL)
  3727. {
  3728. if (addHotTypes)
  3729. {
  3730. mHotPrimaryTypes.Add(parentType);
  3731. }
  3732. compileUnit->mOrphanMethods.Remove(dbgMethod, prevDbgMethod);
  3733. parentType->mMethodList.PushBack(dbgMethod);
  3734. dbgMethod->mParentType = parentType;
  3735. movedMethod = true;
  3736. }
  3737. }
  3738. }
  3739. if (!movedMethod)
  3740. prevDbgMethod = dbgMethod;
  3741. dbgMethod = nextDbgMethod;
  3742. }
  3743. }
  3744. void COFF::MapCompileUnitMethods(int compileUnitId)
  3745. {
  3746. CvModuleInfo* moduleInfo = mCvModuleInfo[compileUnitId];
  3747. if (moduleInfo->mHasMappedMethods)
  3748. return;
  3749. ParseCompileUnit(compileUnitId);
  3750. auto compileUnit = moduleInfo->mCompileUnit;
  3751. //for (auto dbgMethod : compileUnit->mGlobalType.mMethodList)
  3752. MapCompileUnitMethods(compileUnit);
  3753. moduleInfo->mHasMappedMethods = true;
  3754. }
  3755. void COFF::PopulateType(DbgType* dbgType)
  3756. {
  3757. uint8* data = CvGetTagData(dbgType->mTagIdx, false);
  3758. CvAutoReleaseTempData releaseTempData(this, data);
  3759. uint8* dataStart = data;
  3760. uint16 trLeafType = GET(uint16);
  3761. switch (trLeafType)
  3762. {
  3763. case LF_ENUM:
  3764. {
  3765. lfEnum& enumInfo = *(lfEnum*)dataStart;
  3766. CvParseMembers(dbgType, enumInfo.field, false);
  3767. }
  3768. break;
  3769. case LF_CLASS:
  3770. case LF_STRUCTURE:
  3771. {
  3772. lfClass& classInfo = *(lfClass*)dataStart;
  3773. CvParseMembers(dbgType, classInfo.field, false);
  3774. }
  3775. break;
  3776. case LF_CLASS_EX:
  3777. case LF_STRUCTURE_EX:
  3778. {
  3779. auto property = GET(CV_prop_t);
  3780. auto extra = GET(int16);
  3781. auto field = GET(CV_typ_t);
  3782. CvParseMembers(dbgType, field, false);
  3783. }
  3784. break;
  3785. case LF_UNION:
  3786. {
  3787. lfUnion& classInfo = *(lfUnion*)dataStart;
  3788. CvParseMembers(dbgType, classInfo.field, false);
  3789. }
  3790. break;
  3791. default:
  3792. BF_FATAL("Invalid type");
  3793. }
  3794. }
  3795. void COFF::PopulateTypeGlobals(DbgType* dbgType)
  3796. {
  3797. //gDbgPerfManager->StartRecording();
  3798. {
  3799. BP_ZONE_F("COFF::PopulateTypeGlobals %s", (dbgType->mName != NULL) ? dbgType->mName : "?");
  3800. ParseTypeData();
  3801. dbgType->mNeedsGlobalsPopulated = false;
  3802. dbgType->mHasGlobalsPopulated = true;
  3803. mGlobalsTargetType = dbgType;
  3804. ParseSymbolStream(CvSymStreamType_Globals_Targeted);
  3805. mGlobalsTargetType = NULL;
  3806. }
  3807. //gDbgPerfManager->StopRecording(true);
  3808. }
  3809. void COFF::PopulateSubprogram(DbgSubprogram* dbgSubprogram)
  3810. {
  3811. if (dbgSubprogram->mDeferredInternalsSize == 0)
  3812. return;
  3813. BP_ZONE("COFF::PopulateSubprogram");
  3814. auto compileUnit = (CvCompileUnit*)dbgSubprogram->mCompileUnit;
  3815. CvModuleInfo* moduleInfo = mCvModuleInfo[compileUnit->mModuleIdx];
  3816. int sectionSize = 0;
  3817. uint8* sectionData = CvReadStreamSegment(moduleInfo->mStream, dbgSubprogram->mTagIdx, dbgSubprogram->mDeferredInternalsSize);
  3818. CvInlineInfoVec inlineDataVec;
  3819. ParseCompileUnit_Symbols(compileUnit, NULL, sectionData, sectionData + dbgSubprogram->mDeferredInternalsSize, inlineDataVec, false, dbgSubprogram);
  3820. delete sectionData;
  3821. dbgSubprogram->mDeferredInternalsSize = 0;
  3822. }
  3823. void COFF::FixSubprogramName(DbgSubprogram* dbgSubprogram)
  3824. {
  3825. const char* name = dbgSubprogram->mName;
  3826. for (const char* cPtr = name; *cPtr != 0; cPtr++)
  3827. {
  3828. char c = *cPtr;
  3829. if (c == '$')
  3830. {
  3831. bool isChecked = strcmp(cPtr, "$CHK") == 0;
  3832. bool isUnchecked = strcmp(cPtr, "$UCHK") == 0;
  3833. if (isChecked || isUnchecked)
  3834. {
  3835. dbgSubprogram->mCheckedKind = isChecked ? BfCheckedKind_Checked : BfCheckedKind_Unchecked;
  3836. dbgSubprogram->mName = CvDupString(name, cPtr - name);
  3837. return;
  3838. }
  3839. }
  3840. }
  3841. }
  3842. void COFF::FixupInlinee(DbgSubprogram* dbgSubprogram, uint32 ipiTag)
  3843. {
  3844. if (!mCvIPIReader.IsSetup())
  3845. CvParseIPI();
  3846. //int idx = (ipiTag & 0x7FFFFFFF) - mCvIPIMinTag;
  3847. //uint8* dataStart = mCvIPIData + mCvIPITagStartMap[idx];
  3848. uint8* dataStart = CvGetTagData(ipiTag & 0x7FFFFFFF, true);
  3849. CvAutoReleaseTempData releaseTempData(this, dataStart);
  3850. lfEasy& funcLeaf = *(lfEasy*)dataStart;
  3851. switch (funcLeaf.leaf)
  3852. {
  3853. case LF_FUNC_ID:
  3854. {
  3855. lfFuncId* funcData = (lfFuncId*)dataStart;
  3856. CvParseMethod(NULL, NULL, funcData->type, false, dbgSubprogram);
  3857. dbgSubprogram->mName = (const char*)funcData->name;
  3858. bool doScope = true;
  3859. if ((funcData->scopeId != 0) && (doScope))
  3860. {
  3861. uint8* data = CvGetTagData(funcData->scopeId, true);
  3862. CvAutoReleaseTempData releaseTempData(this, data);
  3863. lfEasy& leaf = *(lfEasy*)data;
  3864. switch (leaf.leaf)
  3865. {
  3866. case LF_STRING_ID:
  3867. {
  3868. lfStringId& str = *(lfStringId*)data;
  3869. const char* parentStr = (const char*)str.name;
  3870. int allocLen = strlen(parentStr) + 2 + strlen(dbgSubprogram->mName) + 1;
  3871. BP_ALLOC("String", allocLen);
  3872. char* dupStr = (char*)mAlloc.AllocBytes(allocLen, "String");
  3873. strcpy(dupStr, parentStr);
  3874. strcat(dupStr, "::");
  3875. strcat(dupStr, dbgSubprogram->mName);
  3876. dbgSubprogram->mName = dupStr;
  3877. }
  3878. break;
  3879. case LF_UDT_MOD_SRC_LINE:
  3880. {
  3881. /*auto udtModSrcLine = *(lfUdtModSrcLine*)data;
  3882. auto parentType = CvGetType(udtModSrcLine.type);
  3883. curSubprogram->mParentType = parentType;*/
  3884. }
  3885. break;
  3886. }
  3887. }
  3888. break;
  3889. }
  3890. case LF_MFUNC_ID:
  3891. {
  3892. lfMFuncId* funcData = (lfMFuncId*)dataStart;
  3893. CvParseMethod(NULL, NULL, funcData->type, false, dbgSubprogram);
  3894. dbgSubprogram->mName = (const char*)funcData->name;
  3895. if (dbgSubprogram->mName != NULL)
  3896. {
  3897. for (const char* cPtr = dbgSubprogram->mName + 1; true; cPtr++)
  3898. {
  3899. char c = *cPtr;
  3900. if (c == 0)
  3901. break;
  3902. // For removing the mangled name from the mixins
  3903. if (c == '?')
  3904. {
  3905. int nameLen = cPtr - dbgSubprogram->mName;
  3906. char* dupStr = (char*)mAlloc.AllocBytes(nameLen + 1);
  3907. memcpy(dupStr, dbgSubprogram->mName, nameLen);
  3908. dupStr[nameLen] = 0;
  3909. dbgSubprogram->mName = dupStr;
  3910. break;
  3911. }
  3912. }
  3913. }
  3914. FixSubprogramName(dbgSubprogram);
  3915. dbgSubprogram->mParentType = CvGetType(funcData->parentType);
  3916. }
  3917. break;
  3918. }
  3919. }
  3920. int COFF::MapImport(CvCompileUnit* compileUnit, int id)
  3921. {
  3922. BF_ASSERT((id & 0x80000000) != 0);
  3923. int scopeIdx = (id & 0x7FFF0000) >> 20;
  3924. int itemId = (id & 0x0000FFFF);
  3925. if (mModuleNameSet.IsEmpty())
  3926. {
  3927. for (auto moduleInfo : mCvModuleInfo)
  3928. {
  3929. String name = moduleInfo->mModuleName;
  3930. name = ToUpper(name);
  3931. CvModuleInfoNameEntry entry;
  3932. entry.mModuleInfo = moduleInfo;
  3933. mModuleNameSet.Add(entry);
  3934. }
  3935. }
  3936. if (scopeIdx < (int)compileUnit->mImports.size())
  3937. {
  3938. auto& importSection = compileUnit->mImports[scopeIdx];
  3939. if (itemId < (int)importSection.mImports.size())
  3940. {
  3941. auto importId = importSection.mImports[itemId];
  3942. if (importSection.mScopeCompileUnit == NULL)
  3943. {
  3944. CvModuleInfo* moduleInfo = NULL;
  3945. size_t nameHash = CvModuleInfoNameEntry::GetHashCode(importSection.mScopeName);
  3946. for (auto& nameEntry : mModuleNameSet.SelectHashes(nameHash))
  3947. {
  3948. String checkModuleName = nameEntry.mModuleInfo->mModuleName;
  3949. checkModuleName.Replace("\\", "/");
  3950. checkModuleName = ToUpper(checkModuleName);
  3951. String checkScopeName = importSection.mScopeName;
  3952. checkScopeName.Replace("\\", "/");
  3953. checkScopeName = ToUpper(checkScopeName);
  3954. bool matches = false;
  3955. if (checkModuleName == checkScopeName)
  3956. matches = true;
  3957. else if (checkScopeName.EndsWith("/" + checkModuleName))
  3958. matches = true;
  3959. if (matches)
  3960. {
  3961. ParseCompileUnit(nameEntry.mModuleInfo->mIdx);
  3962. auto externCompileUnit = nameEntry.mModuleInfo->mCompileUnit;
  3963. if (externCompileUnit->mExportMap.IsEmpty())
  3964. {
  3965. for (auto& exportEntry : externCompileUnit->mExports)
  3966. externCompileUnit->mExportMap[exportEntry.mLocalId] = exportEntry.mGlobalId;
  3967. }
  3968. if (externCompileUnit->mExportMap.ContainsKey(importId))
  3969. {
  3970. importSection.mScopeCompileUnit = nameEntry.mModuleInfo->mCompileUnit;
  3971. break;
  3972. }
  3973. }
  3974. }
  3975. }
  3976. if (importSection.mScopeCompileUnit != NULL)
  3977. {
  3978. uint32 exportId;
  3979. if (importSection.mScopeCompileUnit->mExportMap.TryGetValue(importId, &exportId))
  3980. {
  3981. if ((exportId & 0x80000000) != 0)
  3982. {
  3983. // Does this mean a recursive lookup?
  3984. return exportId & 0x7FFFFFFF;
  3985. }
  3986. else
  3987. {
  3988. return exportId;
  3989. }
  3990. }
  3991. }
  3992. }
  3993. }
  3994. return 0;
  3995. }
  3996. void COFF::FixupInlinee(DbgSubprogram* dbgSubprogram)
  3997. {
  3998. BF_ASSERT(dbgSubprogram->mInlineeInfo != NULL);
  3999. BF_ASSERT((dbgSubprogram->mInlineeInfo->mInlineeId & 0x80000000) != 0);
  4000. int scopeIdx = (dbgSubprogram->mInlineeInfo->mInlineeId & 0x7FFF0000) >> 20;
  4001. int itemId = (dbgSubprogram->mInlineeInfo->mInlineeId & 0x0000FFFF);
  4002. if (mModuleNameMap.IsEmpty())
  4003. {
  4004. for (auto moduleInfo : mCvModuleInfo)
  4005. {
  4006. mModuleNameMap[CaseInsensitiveString(moduleInfo->mModuleName)] = moduleInfo;
  4007. }
  4008. }
  4009. auto compileUnit = (CvCompileUnit*)dbgSubprogram->mCompileUnit;
  4010. if (scopeIdx < (int)compileUnit->mImports.size())
  4011. {
  4012. auto& importSection = compileUnit->mImports[scopeIdx];
  4013. if (itemId < (int)importSection.mImports.size())
  4014. {
  4015. auto importId = importSection.mImports[itemId];
  4016. if (importSection.mScopeCompileUnit == NULL)
  4017. {
  4018. CvModuleInfo* moduleInfo = NULL;
  4019. mModuleNameMap.TryGetValue(CaseInsensitiveString(importSection.mScopeName), &moduleInfo);
  4020. if (moduleInfo != NULL)
  4021. {
  4022. ParseCompileUnit(moduleInfo->mIdx);
  4023. importSection.mScopeCompileUnit = moduleInfo->mCompileUnit;
  4024. }
  4025. if (importSection.mScopeCompileUnit != NULL)
  4026. {
  4027. for (auto& exportEntry : importSection.mScopeCompileUnit->mExports)
  4028. {
  4029. if (exportEntry.mLocalId == importId)
  4030. {
  4031. if (exportEntry.mGlobalId < mCvIPITagStartMap.size())
  4032. {
  4033. FixupInlinee(dbgSubprogram, exportEntry.mGlobalId);
  4034. }
  4035. }
  4036. }
  4037. }
  4038. }
  4039. }
  4040. }
  4041. dbgSubprogram->mInlineeInfo->mInlineeId = 0;
  4042. }
  4043. void COFF::PopulateStaticVariableMap()
  4044. {
  4045. if (mPopulatedStaticVariables)
  4046. return;
  4047. BP_ZONE("COFF::PopulateStaticVariableMap");
  4048. if (mMasterCompileUnit != NULL)
  4049. {
  4050. PopulateTypeGlobals(mMasterCompileUnit->mGlobalType);
  4051. for (auto variable : mMasterCompileUnit->mGlobalType->mMemberList)
  4052. {
  4053. if ((variable->mIsExtern) && (variable->mLinkName != NULL))
  4054. mStaticVariables.push_back(variable);
  4055. }
  4056. }
  4057. DbgModule::PopulateStaticVariableMap();
  4058. }
  4059. void COFF::ScanCompileUnit(int compileUnitId)
  4060. {
  4061. BP_ZONE("COFF::ScanCompileUnit");
  4062. CvModuleInfo* moduleInfo = mCvModuleInfo[compileUnitId];
  4063. if (moduleInfo->mStream < 0)
  4064. return;
  4065. BF_ASSERT(moduleInfo->mSymbolBytes % 4 == 0);
  4066. uint8* sectionData = CvReadStreamSegment(moduleInfo->mStream, moduleInfo->mSymbolBytes, moduleInfo->mLinesBytes);
  4067. uint8* data = sectionData;
  4068. uint8* dataEnd = sectionData + moduleInfo->mLinesBytes;
  4069. while (data < dataEnd)
  4070. {
  4071. GET_INTO(int32, lineInfoType);
  4072. GET_INTO(int32, lineInfoLength);
  4073. uint8* dataEnd = data + lineInfoLength;
  4074. if (lineInfoType == DEBUG_S_FILECHKSMS)
  4075. {
  4076. while (data < dataEnd)
  4077. {
  4078. GET_INTO(uint, fileTableOfs);
  4079. DbgSrcFile* srcFile;
  4080. DbgSrcFile** srcFilePtr = NULL;
  4081. if (!mCVSrcFileRefCache.TryAdd(fileTableOfs, NULL, &srcFilePtr))
  4082. {
  4083. srcFile = *srcFilePtr;
  4084. }
  4085. else
  4086. {
  4087. const char* fileName = mStringTable.mStrTable + fileTableOfs;
  4088. if ((fileName[0] == '\\') && (fileName[1] == '$'))
  4089. fileName++;
  4090. srcFile = AddSrcFile(NULL, fileName);
  4091. mSrcFileDeferredRefs.Add(srcFile);
  4092. *srcFilePtr = srcFile;
  4093. //mCVSrcFileRefCache[fileTableOfs] = srcFile;
  4094. }
  4095. DbgDeferredSrcFileReference dbgDeferredSrcFileReference;
  4096. dbgDeferredSrcFileReference.mDbgModule = this;
  4097. dbgDeferredSrcFileReference.mCompileUnitId = compileUnitId;
  4098. srcFile->mDeferredRefs.push_back(dbgDeferredSrcFileReference);
  4099. srcFile->mHadLineData = true;
  4100. GET_INTO(uint8, hashLen);
  4101. GET_INTO(uint8, hashType);
  4102. data += hashLen;
  4103. PTR_ALIGN(data, sectionData, 4);
  4104. }
  4105. break; // Stop once we handle the file checksums
  4106. }
  4107. data = dataEnd;
  4108. }
  4109. delete [] sectionData;
  4110. }
  4111. void COFF::FixTypes(int startingIdx)
  4112. {
  4113. BP_ZONE("COFF::FixTypes");
  4114. //std::unordered_map<String, DbgType*> namespaceMap;
  4115. auto linkedModule = GetLinkedModule();
  4116. String wholeNamespace;
  4117. for (int typeIdx = startingIdx; typeIdx < (int)linkedModule->mTypes.size(); typeIdx++)
  4118. {
  4119. DbgType* dbgType = linkedModule->mTypes[typeIdx];
  4120. DbgType* prevNamespaceType = NULL;
  4121. if (dbgType->mName == NULL)
  4122. {
  4123. // Internal typedef
  4124. continue;
  4125. }
  4126. if (dbgType->mLanguage == DbgLanguage_Unknown)
  4127. {
  4128. if (strncmp(dbgType->mName, "_bf::", 5) == 0)
  4129. {
  4130. dbgType->mLanguage = DbgLanguage_Beef;
  4131. dbgType->mName += 5;
  4132. dbgType->mTypeName += 5;
  4133. }
  4134. else if ((dbgType->mName[0] == 'G') && (dbgType->mName[1] == '$'))
  4135. {
  4136. dbgType->mLanguage = DbgLanguage_Beef;
  4137. }
  4138. else
  4139. {
  4140. dbgType->mLanguage = DbgLanguage_C;
  4141. // Check for a primitive array type like 'double[]'
  4142. for (int i = 0; i < 10; i++)
  4143. {
  4144. char c = dbgType->mName[i];
  4145. if (c == 0)
  4146. break;
  4147. if (c == '[')
  4148. dbgType->mLanguage = DbgLanguage_Beef;
  4149. }
  4150. }
  4151. }
  4152. if (dbgType->mIsDeclaration)
  4153. continue;
  4154. if (IsObjectFile())
  4155. {
  4156. auto entry = linkedModule->mTypeMap.Find(dbgType->mName, dbgType->mLanguage);
  4157. if (entry != NULL)
  4158. {
  4159. // Just do hot replacing unless this type is new
  4160. continue;
  4161. }
  4162. }
  4163. // This never happens anymore - nested types are always handled as "internal typedefs"
  4164. //TODO: Check the 'isnested' flag on the leaf instead of counting on parent to be specified?
  4165. bool hadParent = dbgType->mParent != NULL;
  4166. int chevronDepth = 0;
  4167. if ((dbgType->mTypeCode == DbgType_Class) || (dbgType->mTypeCode == DbgType_Struct) || (dbgType->mTypeCode == DbgType_Union) ||
  4168. (dbgType->mTypeCode == DbgType_Enum) || (dbgType->mTypeCode == DbgType_TypeDef))
  4169. {
  4170. int startIdx = -1;
  4171. const char* name = dbgType->mTypeName;
  4172. for (int i = 0; true; i++)
  4173. {
  4174. char c = name[i];
  4175. if (c == '<')
  4176. chevronDepth++;
  4177. else if (c == '>')
  4178. chevronDepth--;
  4179. if ((chevronDepth == 0) && ((c == ':') || (c == '.')))
  4180. {
  4181. if ((!hadParent) && (i - startIdx > 1))
  4182. {
  4183. //String wholeNamespace = String(name, name + i);
  4184. wholeNamespace.clear();
  4185. wholeNamespace.Insert(0, name, i);
  4186. auto entry = linkedModule->mTypeMap.Find(wholeNamespace.c_str(), dbgType->mLanguage);
  4187. if (entry == NULL)
  4188. {
  4189. DbgType* namespaceType = CvCreateType();
  4190. namespaceType->mTypeCode = DbgType_Namespace;
  4191. namespaceType->mName = DbgDupString(wholeNamespace.c_str(), "DbgDupString.FixTypes");
  4192. namespaceType->mTypeName = namespaceType->mName + startIdx + 1;
  4193. namespaceType->mPriority = DbgTypePriority_Primary_Implicit;
  4194. namespaceType->mLanguage = dbgType->mLanguage;
  4195. linkedModule->mTypeMap.Insert(namespaceType);
  4196. if (prevNamespaceType != NULL)
  4197. {
  4198. namespaceType->mParent = prevNamespaceType;
  4199. prevNamespaceType->mSubTypeList.PushBack(namespaceType);
  4200. }
  4201. prevNamespaceType = namespaceType;
  4202. }
  4203. else
  4204. {
  4205. prevNamespaceType = entry->mValue;
  4206. }
  4207. }
  4208. startIdx = i;
  4209. }
  4210. else if ((c == 0) /*|| (c == '<')*/ || (c == '('))
  4211. break;
  4212. }
  4213. if (prevNamespaceType != NULL)
  4214. {
  4215. dbgType->mParent = prevNamespaceType;
  4216. prevNamespaceType->mSubTypeList.PushBack(dbgType);
  4217. }
  4218. dbgType->mTypeName = dbgType->mTypeName + startIdx + 1; // Just take last part of name
  4219. }
  4220. if (dbgType->mParent == NULL)
  4221. dbgType->mCompileUnit->mGlobalType->mSubTypeList.PushBack(dbgType);
  4222. }
  4223. }
  4224. void COFF::CvReadStream(int streamIdx, CvStreamReader& streamReader)
  4225. {
  4226. BF_ASSERT(streamReader.mCOFF == NULL);
  4227. streamReader.mCOFF = this;
  4228. streamReader.mPageBits = mCvPageBits;
  4229. int streamSize = mCvStreamSizes[streamIdx];
  4230. int streamPageCount = (streamSize + mCvPageSize - 1) / mCvPageSize;
  4231. streamReader.mStreamPtrs.Resize(streamPageCount);
  4232. streamReader.mSize = streamSize;
  4233. int streamPtrIdx = mCvStreamPtrStartIdxs[streamIdx];
  4234. for (int streamPageIdx = 0; streamPageIdx < streamPageCount; streamPageIdx++)
  4235. {
  4236. streamReader.mStreamPtrs[streamPageIdx] = (uint8*)mCvMappedViewOfFile + mCvStreamPtrs[streamPtrIdx] * mCvPageSize;
  4237. streamPtrIdx++;
  4238. }
  4239. }
  4240. void COFF::CvInitStreamRaw(CvStreamReader & streamReader, uint8 * data, int size)
  4241. {
  4242. BF_ASSERT(streamReader.mCOFF == NULL);
  4243. streamReader.mCOFF = this;
  4244. streamReader.mPageBits = 31;
  4245. streamReader.mStreamPtrs.Add(data);
  4246. streamReader.mSize = size;
  4247. }
  4248. uint8* COFF::CvReadStream(int streamIdx, int* outSize)
  4249. {
  4250. BP_ZONE("ReadCvSection");
  4251. if (streamIdx >= mCvStreamSizes.size())
  4252. return NULL;
  4253. if ((streamIdx < 0) || (streamIdx >= mCvStreamSizes.mSize))
  4254. {
  4255. return NULL;
  4256. }
  4257. int streamSize = mCvStreamSizes[streamIdx];
  4258. if (outSize != NULL)
  4259. *outSize = streamSize;
  4260. if (streamSize <= 0)
  4261. return NULL;
  4262. int streamPageCount = (streamSize + mCvPageSize - 1) / mCvPageSize;
  4263. uint8* sectionData = new uint8[streamSize];
  4264. bool deferDeleteSectionData = false;
  4265. int streamPtrIdx = mCvStreamPtrStartIdxs[streamIdx];
  4266. for (int streamPageIdx = 0; streamPageIdx < streamPageCount; streamPageIdx++)
  4267. {
  4268. mCvDataStream->SetPos(mCvStreamPtrs[streamPtrIdx] * mCvPageSize);
  4269. mCvDataStream->Read(sectionData + streamPageIdx * mCvPageSize, std::min(streamSize - (streamPageIdx * mCvPageSize), mCvPageSize));
  4270. streamPtrIdx++;
  4271. }
  4272. return sectionData;
  4273. }
  4274. uint8* COFF::CvReadStreamSegment(int streamIdx, int offset, int size)
  4275. {
  4276. BP_ZONE("ReadCvSection");
  4277. //int streamSize = mCvStreamSizes[streamIdx];
  4278. //int streamPageCount = (streamSize + mCvPageSize - 1) / mCvPageSize;
  4279. uint8* sectionData = new uint8[size];
  4280. //delete [] sectionData;
  4281. bool deferDeleteSectionData = false;
  4282. uint8* data = sectionData;
  4283. int curOffset = offset;
  4284. int sizeLeft = size;
  4285. int streamPtrIdx = mCvStreamPtrStartIdxs[streamIdx];
  4286. streamPtrIdx += offset / mCvPageSize;
  4287. curOffset = offset - (offset / mCvPageSize) * mCvPageSize;
  4288. //for (int streamPageIdx = 0; streamPageIdx < streamPageCount; streamPageIdx++)
  4289. while (sizeLeft > 0)
  4290. {
  4291. mCvDataStream->SetPos(mCvStreamPtrs[streamPtrIdx] * mCvPageSize + curOffset);
  4292. int readSize = std::min(mCvPageSize - curOffset, sizeLeft);
  4293. mCvDataStream->Read(data, readSize);
  4294. data += readSize;
  4295. sizeLeft -= readSize;
  4296. curOffset = 0;
  4297. streamPtrIdx++;
  4298. }
  4299. return sectionData;
  4300. }
  4301. void COFF::ReleaseTempBuf(uint8* buf)
  4302. {
  4303. if ((mTempBufIdx > 0) && (mTempBufs.mVals[mTempBufIdx - 1].mVals == buf))
  4304. mTempBufIdx--;
  4305. }
  4306. bool COFF::CvParseHeader(uint8 wantGuid[16], int32 wantAge)
  4307. {
  4308. int streamSize = 0;
  4309. uint8* data = CvReadStream(1, &streamSize);
  4310. mCvHeaderData = data;
  4311. int32 pdbVersion = GET(int32);
  4312. int32 timestamp = GET(int32);
  4313. int32 pdbAge = GET(int32);
  4314. for (int i = 0; i < 16; i++)
  4315. mPDBGuid[i] = GET(int8);
  4316. if ((wantAge != -1) &&
  4317. (/*(pdbAge != wantAge) ||*/ (memcmp(mPDBGuid, wantGuid, 16) != 0)))
  4318. {
  4319. if (mDebugger != NULL)
  4320. {
  4321. String msg = "PDB GUID did not match requested version\n";
  4322. msg += StrFormat(" Module GUID: ");
  4323. for (int i = 0; i < 16; i++)
  4324. msg += StrFormat("%02X", (uint8)wantGuid[i]);
  4325. msg += "\n";
  4326. msg += StrFormat(" PDB GUID : ");
  4327. for (int i = 0; i < 16; i++)
  4328. msg += StrFormat("%02X", (uint8)mPDBGuid[i]);
  4329. Fail(msg);
  4330. }
  4331. return false;
  4332. }
  4333. if (mParseKind == ParseKind_Header)
  4334. return true;
  4335. int nameTableIdx = -1;
  4336. GET_INTO(int32, strTabLen);
  4337. const char* strTab = (const char*)data;
  4338. data += strTabLen;
  4339. GET_INTO(int32, numStrItems);
  4340. GET_INTO(int32, strItemMax);
  4341. GET_INTO(int32, usedLen);
  4342. data += usedLen * sizeof(int32);
  4343. GET_INTO(int32, deletedLen);
  4344. data += deletedLen * sizeof(int32);
  4345. for (int tableIdx = 0; tableIdx < numStrItems; tableIdx++)
  4346. {
  4347. GET_INTO(int32, strOfs);
  4348. GET_INTO(int32, streamNum);
  4349. const char* tableName = strTab + strOfs;
  4350. if (strcmp(tableName, "/names") == 0)
  4351. mStringTable.mStream = streamNum;
  4352. if (strcmp(tableName, "srcsrv") == 0)
  4353. mCvSrcSrvStream = streamNum;
  4354. if (strcmp(tableName, "emit") == 0)
  4355. mCvEmitStream = streamNum;
  4356. /*if (tableIdx == nameTableIdx)
  4357. {
  4358. nameStringTable.mStream = streamNum;
  4359. nameStringTable.mStreamOffset = strOfs;
  4360. }*/
  4361. }
  4362. return true;
  4363. }
  4364. bool COFF::CvParseDBI(int wantAge)
  4365. {
  4366. BP_ZONE("CvParseDBI");
  4367. uint8* data = CvReadStream(3);
  4368. if (data == NULL)
  4369. return false;
  4370. uint8* sectionData = data;
  4371. defer
  4372. (
  4373. delete sectionData;
  4374. );
  4375. // Header
  4376. GET_INTO(int32, signature);
  4377. GET_INTO(uint32, version);
  4378. GET_INTO(uint32, age);
  4379. mDebugAge = (int32)age;
  4380. if ((wantAge != -1) && (wantAge != age))
  4381. {
  4382. if (mDebugger != NULL)
  4383. {
  4384. String msg = "PDB age did not match requested age\n";
  4385. msg += StrFormat(" Module Age: %d PDB Age: %d", wantAge, age);
  4386. Fail(msg);
  4387. }
  4388. return false;
  4389. }
  4390. if (mParseKind == ParseKind_Header)
  4391. return true;
  4392. mCvGlobalSymbolInfoStream = GET(int16); // hash1_file, snGSSyms
  4393. GET_INTO(uint16, pdb_dll_version);
  4394. mCvPublicSymbolInfoStream = GET(int16); // hash2_file, snPSSyms
  4395. GET_INTO(uint16, pdb_dll_build_major);
  4396. mCvSymbolRecordStream = GET(int16); // gsym_file, snSymRecs
  4397. GET_INTO(uint16, pdb_dll_build_minor);
  4398. GET_INTO(uint32, gp_modi_size); //module_size
  4399. GET_INTO(uint32, section_contribution_size); //offset_size
  4400. GET_INTO(uint32, section_map_size); //hash_size
  4401. GET_INTO(uint32, file_info_size); //srcmodule_size
  4402. GET_INTO(uint32, ts_map_size); //pdb_import_size
  4403. GET_INTO(uint32, mfc_index); //resvd0
  4404. GET_INTO(uint32, dbg_header_size); // stream_index_size
  4405. GET_INTO(uint32, ec_info_size); //unknown_size
  4406. GET_INTO(uint16, flags); //resvd3
  4407. GET_INTO(uint16, machine);
  4408. GET_INTO(uint32, reserved); //resvd4
  4409. if (machine == 0x8664)
  4410. mIs64Bit = true;
  4411. else if (machine == 0x014C)
  4412. mIs64Bit = false;
  4413. else // Unknown machine
  4414. return false;
  4415. uint8* headerEnd = data;
  4416. // Skip to debug header
  4417. data += gp_modi_size;
  4418. data += section_contribution_size;
  4419. data += section_map_size;
  4420. data += file_info_size;
  4421. data += ts_map_size;
  4422. data += ec_info_size;
  4423. // Debug header
  4424. GET_INTO(int16, fpo);
  4425. GET_INTO(int16, exception);
  4426. GET_INTO(int16, fixup);
  4427. GET_INTO(int16, omap_to_src);
  4428. GET_INTO(int16, omap_from_src);
  4429. GET_INTO(int16, section_header); // 0x0a, all others are -1 (segments)
  4430. GET_INTO(int16, token_rid_map);
  4431. GET_INTO(int16, x_data);
  4432. GET_INTO(int16, p_data);
  4433. mCvNewFPOStream = GET(int16);
  4434. GET_INTO(int16, section_header_origin);
  4435. if (section_header != -1)
  4436. ParseSectionHeader(section_header);
  4437. // Module info
  4438. data = headerEnd;
  4439. uint8* dataEnd = data + gp_modi_size;
  4440. while (data < dataEnd)
  4441. {
  4442. BP_ALLOC_T(CvModuleInfo);
  4443. CvModuleInfo* moduleInfo = mAlloc.Alloc<CvModuleInfo>();
  4444. memcpy(moduleInfo, data, sizeof(CvModuleInfoBase));
  4445. data += sizeof(CvModuleInfoBase);
  4446. moduleInfo->mModuleName = CvParseAndDupString(data);//DataGetString(data);
  4447. moduleInfo->mObjectName = CvParseAndDupString(data);//DataGetString(data);
  4448. moduleInfo->mIdx = (int)mCvModuleInfo.size();
  4449. //BfLogCv("Module %d: %s Obj: %s\n", mCvModuleInfo.size(), moduleInfo->mModuleName, moduleInfo->mObjectName);
  4450. mCvModuleInfo.push_back(moduleInfo);
  4451. /*if (moduleInfo->mStream != -1)
  4452. {
  4453. if (moduleInfo->mStream >= mDeferredModuleInfo.size())
  4454. mDeferredModuleInfo.resize(moduleInfo->mStream + 1);
  4455. mDeferredModuleInfo[moduleInfo->mStream].push_back(moduleInfo);
  4456. }*/
  4457. PTR_ALIGN(data, sectionData, 4);
  4458. }
  4459. // Section contrib
  4460. {
  4461. BP_ZONE("CvParseDBI_Contrib");
  4462. dataEnd = data + section_contribution_size;
  4463. int32 sig = GET(int32);
  4464. BF_ASSERT((sig == 0xF12EBA2D) || (sig == 0xf13151e4));
  4465. bool isV2 = sig == 0xf13151e4;
  4466. if (isV2)
  4467. {
  4468. Fail("FastLink PDBs are not supported. Consider adding full debug info (/DEBUG:FULL).");
  4469. mIsFastLink = true;
  4470. }
  4471. while (data < dataEnd)
  4472. {
  4473. CvSectionContrib& contrib = GET(CvSectionContrib);
  4474. if (isV2)
  4475. {
  4476. uint32_t isectCoff = GET(uint32_t);
  4477. }
  4478. for (int contribOffset = 0; true; contribOffset += DBG_MAX_LOOKBACK)
  4479. {
  4480. int curSize = contrib.mSize - contribOffset;
  4481. if (curSize <= 0)
  4482. break;
  4483. BP_ALLOC_T(DbgCompileUnitContrib);
  4484. auto contribEntry = mAlloc.Alloc<DbgCompileUnitContrib>();
  4485. contribEntry->mAddress = GetSectionAddr(contrib.mSection, contrib.mOffset) + contribOffset;
  4486. if ((contribEntry->mAddress & 0xFFFF0000'00000000) != 0)
  4487. continue;
  4488. contribEntry->mLength = curSize;
  4489. contribEntry->mDbgModule = this;
  4490. contribEntry->mCompileUnitId = contrib.mModule;
  4491. contribEntry->mSection = contrib.mSection;
  4492. mDebugTarget->mContribMap.Insert(contribEntry);
  4493. }
  4494. }
  4495. }
  4496. // Dbi section map
  4497. dataEnd = data + section_map_size;
  4498. GET_INTO(int16, numSections);
  4499. GET_INTO(int16, numSectionsCopy);
  4500. while (data < dataEnd)
  4501. {
  4502. GET_INTO(uint8, flags);
  4503. GET_INTO(uint8, section_type);
  4504. // This field hasn't been deciphered but it is always 0x00000000 or 0xFFFFFFFF
  4505. // and modifying it doesn't seem to invalidate the Cv.
  4506. GET_INTO(int32, unknownData);
  4507. GET_INTO(uint16, section_number);
  4508. // Same thing as for unknown_data_1.
  4509. GET_INTO(int32, unknownData2);
  4510. // Value added to the address offset when calculating the RVA.
  4511. GET_INTO(uint32, rva_offset);
  4512. GET_INTO(uint32, section_length);
  4513. }
  4514. // Dbi File info
  4515. dataEnd = data + file_info_size;
  4516. GET_INTO(uint16, fileInfoNumModules);
  4517. GET_INTO(uint16, bad_fileInfoNumSourceFiles);
  4518. int fileInfoNumSourceFiles = 0;
  4519. uint16* modIndices = (uint16*)data;
  4520. data += fileInfoNumModules * sizeof(uint16);
  4521. uint16* modFileCounts = (uint16*)data;
  4522. data += fileInfoNumModules * sizeof(uint16);
  4523. for (int moduleIdx = 0; moduleIdx < fileInfoNumModules; moduleIdx++)
  4524. fileInfoNumSourceFiles += modFileCounts[moduleIdx];
  4525. int32* fileNameOffsets = (int32*)data;
  4526. data += fileInfoNumSourceFiles * sizeof(int32);
  4527. char* nameBuffer = (char*)data;
  4528. char* namePtr = nameBuffer;
  4529. int curFileOfs = 0;
  4530. for (int moduleIdx = 0; moduleIdx < fileInfoNumModules; moduleIdx++)
  4531. {
  4532. //TODO: LLD-LINK does not emit this properly
  4533. //BF_ASSERT((modIndices[moduleIdx] == curFileOfs) || (moduleIdx >= 32));
  4534. for (int fileIdx = 0; fileIdx < modFileCounts[moduleIdx]; fileIdx++)
  4535. {
  4536. char* fileName = namePtr + *fileNameOffsets;
  4537. curFileOfs++;
  4538. fileNameOffsets++;
  4539. }
  4540. //int blockStart = fileBlockTable[i];
  4541. //int blockLength = fileBlockTable[fileInfoNumModules + i];
  4542. //int offset = offsetTable[blockStart]; through blockLength...
  4543. }
  4544. // File name table
  4545. /*uint8* dataStart = data;
  4546. while (data < dataEnd)
  4547. {
  4548. int offset = (int)(data - dataStart);
  4549. const char* fileName = SafeDataGetString(data, dataEnd);
  4550. if (fileName == NULL)
  4551. break;
  4552. //DbgSrcFile* srcFile = AddSrcFile(mMasterCompileUnit, fileName);
  4553. //fileTable.insert(std::make_pair(offset, srcFile));
  4554. }*/
  4555. data = dataEnd;
  4556. BF_ASSERT(ts_map_size == 0);
  4557. // EC Info
  4558. dataEnd = data + ec_info_size;
  4559. GET_INTO(uint32, strTabSig);
  4560. GET_INTO(uint32, strTabVer);
  4561. BF_ASSERT(strTabSig == 0xEFFEEFFE);
  4562. GET_INTO(int32, strTabSize);
  4563. uint8* strTabData = data;
  4564. data += strTabSize;
  4565. int pdbNameIdx = -1;
  4566. for (auto moduleInfo : mCvModuleInfo)
  4567. {
  4568. if (moduleInfo->mCompilerNameCount > 0)
  4569. pdbNameIdx = moduleInfo->mCompilerNameCount;
  4570. }
  4571. if ((pdbNameIdx > 0) && (pdbNameIdx < strTabSize))
  4572. mOrigPDBPath = (const char*)strTabData + pdbNameIdx;
  4573. GET_INTO(int32, strTabEntryCount);
  4574. for (int entryIdx = 0; entryIdx < strTabEntryCount; entryIdx++)
  4575. {
  4576. GET_INTO(int32, strOffset);
  4577. const char* str = (const char*)strTabData + strOffset;
  4578. }
  4579. return true;
  4580. }
  4581. void COFF::ParseSectionHeader(int sectionIdx)
  4582. {
  4583. bool fakeRVAS = mSectionHeaders.empty();
  4584. int sectionSize = 0;
  4585. uint8* sectionData = CvReadStream(sectionIdx, &sectionSize);
  4586. uint8* data = sectionData;
  4587. uint8* dataEnd = data + sectionSize;
  4588. while (data < dataEnd)
  4589. {
  4590. auto& sectionHeader = GET(PESectionHeader);
  4591. if (fakeRVAS)
  4592. {
  4593. mSectionHeaders.push_back(sectionHeader);
  4594. }
  4595. }
  4596. if (fakeRVAS)
  4597. {
  4598. PESectionHeader sectionHeader = { 0 };
  4599. mSectionHeaders.push_back(sectionHeader);
  4600. }
  4601. delete sectionData;
  4602. }
  4603. void COFF::CvParseIPI()
  4604. {
  4605. BF_ASSERT(!mCvIPIReader.IsSetup());
  4606. //uint8* sectionData;
  4607. int sectionSize = 0;
  4608. int hashAdjOffset = 0;
  4609. int32 hashStream = -1;
  4610. int32 hashAdjSize = 0;
  4611. int32 dataOffset = 0;
  4612. ParseTypeData(4, mCvIPIReader, sectionSize, dataOffset, hashStream, hashAdjOffset, hashAdjSize, mCvIPIMinTag, mCvIPIMaxTag);
  4613. //mCvIPIData = sectionData;
  4614. int recordCount = mCvIPIMaxTag - mCvIPIMinTag;
  4615. mCvIPITagStartMap.Resize(recordCount + 1);
  4616. //uint8* data = sectionData + dataOffset;
  4617. int offset = dataOffset;
  4618. for (int idx = 0; idx < recordCount; idx++)
  4619. {
  4620. //BF_ASSERT(((offset) & 3) == 0);
  4621. uint8* data = mCvIPIReader.GetTempPtr(offset, 4);
  4622. uint16 trLength = GET(uint16);
  4623. int offsetStart = offset;
  4624. offset += 2;
  4625. data = mCvIPIReader.GetTempPtr(offset, trLength);
  4626. uint8* dataStart = data;
  4627. uint16 trLeafType = GET(uint16);
  4628. //uint8* dataEnd = dataStart + trLength;
  4629. offset += trLength;
  4630. /*
  4631. */
  4632. lfEasy& funcLeaf = *(lfEasy*)dataStart;
  4633. switch (funcLeaf.leaf)
  4634. {
  4635. case LF_FUNC_ID:
  4636. {
  4637. lfFuncId* funcData = (lfFuncId*)dataStart;
  4638. //subprogram->mName = (const char*)funcData->name;
  4639. bool doScope = true;
  4640. /*if (strcmp(curSubprogram->mName, "DispatchToMethod") == 0)
  4641. {
  4642. doScope = true;
  4643. }*/
  4644. if ((funcData->scopeId != 0) && (doScope))
  4645. {
  4646. uint8* data = CvGetTagData(funcData->scopeId, true);
  4647. CvAutoReleaseTempData releaseTempData(this, data);
  4648. lfEasy& leaf = *(lfEasy*)data;
  4649. switch (leaf.leaf)
  4650. {
  4651. case LF_STRING_ID:
  4652. {
  4653. lfStringId& str = *(lfStringId*)data;
  4654. const char* parentStr = (const char*)str.name;
  4655. }
  4656. break;
  4657. case LF_UDT_MOD_SRC_LINE:
  4658. {
  4659. /*auto udtModSrcLine = *(lfUdtModSrcLine*)data;
  4660. auto parentType = CvGetType(udtModSrcLine.type);
  4661. curSubprogram->mParentType = parentType;*/
  4662. }
  4663. break;
  4664. }
  4665. }
  4666. break;
  4667. }
  4668. case LF_MFUNC_ID:
  4669. {
  4670. lfMFuncId* funcData = (lfMFuncId*)dataStart;
  4671. auto parentType = CvGetType(funcData->parentType);
  4672. }
  4673. break;
  4674. }
  4675. mCvIPITagStartMap[idx] = offsetStart;
  4676. }
  4677. mCvIPITagStartMap[recordCount] = offset;
  4678. }
  4679. const char* COFF::CvParseSymbol(int offset, CvSymStreamType symStreamType, addr_target& outAddr)
  4680. {
  4681. const char* retName = NULL;
  4682. int offsetStart = offset;
  4683. uint8* data = mCvSymbolRecordReader.GetTempPtr(offset, 4);
  4684. uint16 symLen = *(uint16*)data;
  4685. bool madeCopy = false;
  4686. data = mCvSymbolRecordReader.GetTempPtr(offset, symLen + 2, false, &madeCopy);
  4687. uint8* dataStart = data;
  4688. uint16 symType = *(uint16*)(data + 2);
  4689. BF_ASSERT(symLen % 4 == 2);
  4690. BF_ASSERT((intptr)data % 4 == 0);
  4691. char* scanName = NULL;
  4692. switch (symType)
  4693. {
  4694. case S_UDT:
  4695. {
  4696. UDTSYM& udt = *(UDTSYM*)dataStart;
  4697. const char* name = (const char*)udt.name;
  4698. #ifdef _DEBUG
  4699. ParseTypeData();
  4700. auto type = CvGetType(udt.typind);
  4701. #endif
  4702. retName = name;
  4703. }
  4704. break;
  4705. case S_PUB32:
  4706. {
  4707. PUBSYM32& pubSym = *(PUBSYM32*)dataStart;
  4708. if (symStreamType != CvSymStreamType_Symbols)
  4709. break;
  4710. BP_ALLOC_T(DbgSymbol);
  4711. DbgSymbol* dbgSymbol = mAlloc.Alloc<DbgSymbol>();
  4712. const char* name = (const char*)mCvSymbolRecordReader.GetPermanentPtr(offsetStart + offsetof(PUBSYM32, name), symLen - offsetof(PUBSYM32, name) + 2); // pubSym.name;
  4713. //OutputDebugStrF("Sym: %s\n", name);
  4714. #ifdef BF_DBG_32
  4715. if ((name != NULL) && (name[0] == '_'))
  4716. name++;
  4717. #endif
  4718. dbgSymbol->mName = name;
  4719. dbgSymbol->mAddress = GetSectionAddr(pubSym.seg, pubSym.off);
  4720. dbgSymbol->mDbgModule = this;
  4721. if ((dbgSymbol->mAddress >= mImageBase) && (dbgSymbol->mAddress < mImageBase + mImageSize))
  4722. {
  4723. if ((dbgSymbol->mAddress & 0xFFFF'0000'0000'0000) == 0)
  4724. mDebugTarget->mSymbolMap.Insert(dbgSymbol);
  4725. }
  4726. else
  4727. {
  4728. NOP;
  4729. }
  4730. mSymbolNameMap.Insert(dbgSymbol);
  4731. outAddr = dbgSymbol->mAddress;
  4732. retName = name;
  4733. }
  4734. break;
  4735. case S_CONSTANT:
  4736. {
  4737. CONSTSYM& constSym = *(CONSTSYM*)dataStart;
  4738. }
  4739. break;
  4740. case S_GDATA32:
  4741. case S_LDATA32:
  4742. {
  4743. uint8* permDataPtr = madeCopy ? mCvSymbolRecordReader.GetPermanentPtr(offsetStart, symLen + 2) : dataStart;
  4744. DATASYM32& dataSym = *(DATASYM32*)permDataPtr;
  4745. if (strcmp((const char*)dataSym.name, "_tls_index") == 0)
  4746. {
  4747. mTLSIndexAddr = GetSectionAddr(dataSym.seg, dataSym.off);
  4748. }
  4749. char* name = (char*)dataSym.name;
  4750. retName = name;
  4751. if (symStreamType == CvSymStreamType_Globals_Scan)
  4752. {
  4753. scanName = (char*)dataSym.name;
  4754. break;
  4755. }
  4756. bool wasBeef = false;
  4757. const char* memberName = CvCheckTargetMatch(name, wasBeef);
  4758. if (memberName == NULL)
  4759. break;
  4760. DbgType* dbgType = CvGetType(dataSym.typind);
  4761. BP_ALLOC_T(DbgVariable);
  4762. DbgVariable* variable = mAlloc.Alloc<DbgVariable>();
  4763. variable->mType = dbgType;
  4764. variable->mLocationData = permDataPtr;
  4765. variable->mLocationLen = 1;
  4766. variable->mIsStatic = true;
  4767. variable->mCompileUnit = mMasterCompileUnit;
  4768. variable->mName = memberName;
  4769. variable->mLinkName = name;
  4770. if (strcmp((const char*)dataSym.name, "__ImageBase") == 0)
  4771. variable->mLinkName = NULL; // Avoid adding to map
  4772. variable->mIsExtern = symType == S_GDATA32;
  4773. variable->mCompileUnit = mGlobalsTargetType->mCompileUnit;
  4774. // Push front so we will find before the original static declaration (that has no memory associated with it)
  4775. mGlobalsTargetType->mMemberList.PushFront(variable);
  4776. if (IsObjectFile())
  4777. {
  4778. if ((variable->mIsExtern) && (variable->mLinkName != NULL))
  4779. mStaticVariables.push_back(variable);
  4780. }
  4781. }
  4782. break;
  4783. case S_GTHREAD32:
  4784. case S_LTHREAD32:
  4785. {
  4786. uint8* permDataPtr = madeCopy ? mCvSymbolRecordReader.GetPermanentPtr(offsetStart, symLen + 2) : dataStart;
  4787. THREADSYM32& threadSym = *(THREADSYM32*)permDataPtr;
  4788. if (symStreamType == CvSymStreamType_Globals_Scan)
  4789. {
  4790. scanName = (char*)threadSym.name;
  4791. break;
  4792. }
  4793. char* name = (char*)threadSym.name;
  4794. retName = name;
  4795. bool wasBeef = false;
  4796. const char* memberName = CvCheckTargetMatch(name, wasBeef);
  4797. if (memberName == NULL)
  4798. break;
  4799. addr_target addr = GetSectionAddr(threadSym.seg, threadSym.off);
  4800. DbgType* dbgType = CvGetType(threadSym.typind);
  4801. BP_ALLOC_T(DbgVariable);
  4802. DbgVariable* variable = mAlloc.Alloc<DbgVariable>();
  4803. variable->mType = dbgType;
  4804. variable->mLocationData = permDataPtr;
  4805. variable->mLocationLen = 1;
  4806. variable->mIsStatic = true;
  4807. variable->mCompileUnit = mMasterCompileUnit;
  4808. variable->mName = memberName;
  4809. variable->mLinkName = name;
  4810. variable->mIsExtern = symType == S_GTHREAD32;
  4811. variable->mCompileUnit = mGlobalsTargetType->mCompileUnit;
  4812. // Push front so we will find before the original static declaration (that has no memory associated with it)
  4813. mGlobalsTargetType->mMemberList.PushFront(variable);
  4814. }
  4815. break;
  4816. case S_PROCREF:
  4817. {
  4818. REFSYM2& refSym = *(REFSYM2*)dataStart;
  4819. if (refSym.imod == 0)
  4820. break;
  4821. char* name = (char*)refSym.name;
  4822. retName = name;
  4823. if (symStreamType == CvSymStreamType_Globals_Scan)
  4824. {
  4825. scanName = (char*)refSym.name;
  4826. break;
  4827. }
  4828. int moduleId = refSym.imod - 1;
  4829. bool wasBeef = false;
  4830. char* memberName = (char*)CvCheckTargetMatch(name, wasBeef);
  4831. if (memberName == NULL)
  4832. break;
  4833. bool wantsCopy = madeCopy;
  4834. bool isIllegal = false;
  4835. for (const char* cPtr = memberName; *cPtr != 0; cPtr++)
  4836. {
  4837. char c = *cPtr;
  4838. if (c == '$')
  4839. {
  4840. if ((strcmp(cPtr, "$CHK") == 0) || (strcmp(cPtr, "$UCHK") == 0))
  4841. {
  4842. auto prevName = memberName;
  4843. memberName = DbgDupString(prevName);
  4844. memberName[cPtr - prevName] = 0;
  4845. wantsCopy = false;
  4846. break;
  4847. }
  4848. else
  4849. isIllegal = true;
  4850. }
  4851. if ((c == '<') || (c == '`'))
  4852. isIllegal = true;
  4853. }
  4854. if (!isIllegal)
  4855. {
  4856. BP_ALLOC_T(DbgMethodNameEntry);
  4857. auto methodNameEntry = mAlloc.Alloc<DbgMethodNameEntry>();
  4858. methodNameEntry->mCompileUnitId = moduleId;
  4859. methodNameEntry->mName = wantsCopy ? DbgDupString(memberName) : memberName;
  4860. mGlobalsTargetType->mMethodNameList.PushBack(methodNameEntry);
  4861. }
  4862. }
  4863. break;
  4864. default:
  4865. //BF_FATAL("Unhandled");
  4866. break;
  4867. }
  4868. if (scanName != NULL)
  4869. {
  4870. //TODO: Remove
  4871. //String origName = name;
  4872. //CvFixupName(name);
  4873. char* lastDblColon = (char*)GetNamespaceEnd(scanName);
  4874. if (lastDblColon != NULL)
  4875. {
  4876. if (mPrevScanName != NULL)
  4877. {
  4878. int namespaceLen = lastDblColon - scanName;
  4879. if (strncmp(scanName, mPrevScanName, namespaceLen + 2) == 0) // +2 includes the ending '::'
  4880. {
  4881. // We've already inserted this namespace
  4882. return retName;
  4883. }
  4884. }
  4885. StringT<256> tempName;
  4886. tempName = String(scanName, lastDblColon - scanName);
  4887. DbgType* dbgType = CvGetTypeOrNamespace((char*)tempName.c_str());
  4888. // *lastDblColon = '\0';
  4889. // DbgType* dbgType = CvGetTypeOrNamespace(scanName);
  4890. // *lastDblColon = ':';
  4891. /*if (strcmp(lastDblColon, "::this") == 0)
  4892. {
  4893. dbgType->mLanguage = DbgLanguage_Beef;
  4894. }*/
  4895. if (dbgType != NULL)
  4896. dbgType->mNeedsGlobalsPopulated = true;
  4897. mPrevScanName = scanName;
  4898. /*const char* methodName = NULL;
  4899. DbgType* dbgParent = NULL;
  4900. if (lastDblColon != NULL)
  4901. {
  4902. *lastDblColon = 0;
  4903. const char* namespaceName = name;
  4904. dbgParent = CvGetTypeOrNamespace(namespaceName);
  4905. methodName = lastDblColon + 2;
  4906. }
  4907. else
  4908. {
  4909. dbgParent = &mMasterCompileUnit->mGlobalType;
  4910. //mStaticVariables.push_back(variable);
  4911. methodName = name;
  4912. }
  4913. if (dbgParent != NULL)
  4914. {
  4915. auto methodNameEntry = mAlloc.Alloc<DbgMethodNameEntry>();
  4916. methodNameEntry->mCompileUnitId = moduleId;
  4917. methodNameEntry->mName = methodName;
  4918. dbgParent->mMethodNameList.PushBack(methodNameEntry);
  4919. }*/
  4920. }
  4921. else
  4922. {
  4923. mMasterCompileUnit->mGlobalType->mNeedsGlobalsPopulated = true;
  4924. }
  4925. }
  4926. return retName;
  4927. }
  4928. uint8* COFF::HandleSymStreamEntries(CvSymStreamType symStreamType, uint8* data, uint8* addrMap)
  4929. {
  4930. uint32 tickStart = BFTickCount();
  4931. GET_INTO(uint32, verSig);
  4932. GET_INTO(uint32, verHdr);
  4933. GET_INTO(int32, sizeHr);
  4934. GET_INTO(int32, sizeBuckets);
  4935. BF_ASSERT(verSig == 0xffffffff);
  4936. BF_ASSERT(verHdr == 0xf12f091a);
  4937. uint8* dataEnd = data + sizeHr;
  4938. uint8* refDataHead = data;
  4939. int entryIdxMax = sizeHr / 8;
  4940. data = dataEnd;
  4941. dataEnd = data + sizeBuckets ;
  4942. int32* hashBucketHead = (int32*)(data + 0x81 * 4);
  4943. int32* hashBucketPtr = hashBucketHead;
  4944. int numRecordsRead = 0;
  4945. #ifdef _DEBUG
  4946. bool verify = false;
  4947. #endif
  4948. if (mIsFastLink)
  4949. return dataEnd; // Format changed
  4950. if (sizeBuckets == 0)
  4951. return dataEnd; // No hash
  4952. std::multimap<addr_target, int> checkAddrMap;
  4953. int bitCount = 0;
  4954. for (int blockIdx = 0; blockIdx < 0x81; blockIdx++)
  4955. {
  4956. GET_INTO(uint32, bits);
  4957. for (int i = 0; i < 32; i++)
  4958. {
  4959. if ((bits & (1 << i)) != 0)
  4960. {
  4961. bitCount++;
  4962. int32 entryStart = *(hashBucketPtr++) / 12;
  4963. int32 entryEnd;
  4964. if ((uint8*)hashBucketPtr != (uint8*)dataEnd)
  4965. entryEnd = *(hashBucketPtr) / 12;
  4966. else
  4967. entryEnd = entryIdxMax;
  4968. BF_ASSERT(entryStart <= entryEnd);
  4969. for (int entryIdx = entryStart; entryIdx < entryEnd; entryIdx++)
  4970. {
  4971. int32 symDataPtr = *(int32*)(refDataHead + entryIdx * 8);
  4972. if (symDataPtr == 0)
  4973. continue;
  4974. addr_target addr = 0;
  4975. const char* symName = CvParseSymbol(symDataPtr - 1, symStreamType, addr);
  4976. #ifdef _DEBUG
  4977. if ((verify) && (addrMap != NULL))
  4978. {
  4979. checkAddrMap.insert(std::make_pair(addr, symDataPtr - 1));
  4980. }
  4981. if ((verify) && (symName != NULL))
  4982. {
  4983. int hashIdx = (blockIdx * 32) + i;
  4984. int wantHash = BlHash::HashStr_PdbV1(symName) % 4096;
  4985. BF_ASSERT(hashIdx == wantHash);
  4986. }
  4987. #endif
  4988. numRecordsRead++;
  4989. }
  4990. }
  4991. }
  4992. }
  4993. #ifdef _DEBUG
  4994. if ((verify) && (addrMap != NULL))
  4995. {
  4996. auto itr = checkAddrMap.begin();
  4997. data = addrMap;
  4998. for (int entryIdx = 0; entryIdx < numRecordsRead; entryIdx++)
  4999. {
  5000. int verifyIdx = GET(int32);
  5001. int actualIdx = itr->second;
  5002. if (actualIdx != verifyIdx)
  5003. {
  5004. OutputDebugStrF("Mismatch #%X %X %X %08X\n", entryIdx, verifyIdx, actualIdx, itr->first);
  5005. }
  5006. ++itr;
  5007. }
  5008. }
  5009. #endif
  5010. BF_ASSERT(numRecordsRead == entryIdxMax);
  5011. //OutputDebugStrF("HandleSymStreamEntries Ticks: %d\n", BFTickCount() - tickStart);
  5012. return dataEnd;
  5013. }
  5014. void COFF::ParseSymbolStream(CvSymStreamType symStreamType)
  5015. {
  5016. if (mCvSymbolRecordStream == 0)
  5017. return;
  5018. BP_ZONE("COFF::ParseSymbolStream");
  5019. if (!mCvSymbolRecordReader.IsSetup())
  5020. CvReadStream(mCvSymbolRecordStream, mCvSymbolRecordReader);
  5021. if ((symStreamType == CvSymStreamType_Globals_Scan) || (symStreamType == CvSymStreamType_Globals_Targeted))
  5022. {
  5023. int streamSize = 0;
  5024. uint8* data;
  5025. if (mCvGlobalSymbolData == NULL)
  5026. mCvGlobalSymbolData = CvReadStream(mCvGlobalSymbolInfoStream, &streamSize);
  5027. data = mCvGlobalSymbolData;
  5028. HandleSymStreamEntries(symStreamType, data, NULL);
  5029. }
  5030. else
  5031. {
  5032. int streamSize = 0;
  5033. uint8* data;
  5034. if (mCvPublicSymbolData == NULL)
  5035. mCvPublicSymbolData = CvReadStream(mCvPublicSymbolInfoStream, &streamSize); // psgsi
  5036. data = mCvPublicSymbolData;
  5037. uint8* sectionData = data;
  5038. GET_INTO(int32, symHashSize);
  5039. GET_INTO(int32, addrMapSize);
  5040. GET_INTO(int32, thunkCount);
  5041. GET_INTO(int32, thunkSize);
  5042. GET_INTO(int32, thunkTableStream);
  5043. GET_INTO(int32, thunkTableOfs);
  5044. GET_INTO(int32, thunkSectCount);
  5045. data = HandleSymStreamEntries(symStreamType, data, data + symHashSize);
  5046. //TODO: Hm, not quite right?
  5047. /*int addrMapEntryCount = addrMapSize / 4;
  5048. for (int addrIdx = 0; addrIdx < addrMapEntryCount; addrIdx++)
  5049. {
  5050. GET_INTO(int32, addrVal);
  5051. }
  5052. for (int thunkIdx = 0; thunkIdx < thunkCount; thunkIdx++)
  5053. {
  5054. GET_INTO(int32, thunkVal);
  5055. }
  5056. for (int sectionIdx = 0; sectionIdx < thunkSectCount; sectionIdx++)
  5057. {
  5058. GET_INTO(int32, ofs);
  5059. GET_INTO(int32, sectNum);
  5060. }*/
  5061. }
  5062. }
  5063. void COFF::Fail(const StringImpl& error)
  5064. {
  5065. DbgModule::Fail(StrFormat("%s in %s", error.c_str(), mPDBPath.c_str()));
  5066. }
  5067. void COFF::SoftFail(const StringImpl& error)
  5068. {
  5069. DbgModule::SoftFail(StrFormat("%s in %s", error.c_str(), mPDBPath.c_str()));
  5070. }
  5071. void COFF::HardFail(const StringImpl& error)
  5072. {
  5073. DbgModule::HardFail(StrFormat("%s in %s", error.c_str(), mPDBPath.c_str()));
  5074. }
  5075. void COFF::ParseGlobalsData()
  5076. {
  5077. if (!mPDBLoaded)
  5078. return;
  5079. if (mParsedGlobalsData)
  5080. return;
  5081. ParseTypeData();
  5082. mParsedGlobalsData = true;
  5083. //gDbgPerfManager->StartRecording();
  5084. int startTypeIdx = (int)mTypes.size();
  5085. ParseSymbolStream(CvSymStreamType_Globals_Scan);
  5086. //gDbgPerfManager->StopRecording(true);
  5087. //int addedTypes = (int)mTypes.size() - startTypeIdx;
  5088. //OutputDebugStrF("Types Added: %d ProcSymCount: %d\n", addedTypes, mProcSymCount);
  5089. }
  5090. void COFF::ParseSymbolData()
  5091. {
  5092. if (!mPDBLoaded)
  5093. return;
  5094. if (mParsedSymbolData)
  5095. return;
  5096. ParseGlobalsData();
  5097. mParsedSymbolData = true;
  5098. ParseSymbolStream(CvSymStreamType_Symbols);
  5099. }
  5100. bool COFF::ParseCv(DataStream& pdbFS, uint8* rootDirData, int pageSize, uint8 wantGuid[16], int32 wantAge)
  5101. {
  5102. BP_ZONE("ParseCv");
  5103. mParsedGlobalsData = false;
  5104. mParsedSymbolData = false;
  5105. mParsedTypeData = false;
  5106. mPopulatedStaticVariables = false;
  5107. InitCvTypes();
  5108. int startingTypeIdx = mTypes.size();
  5109. uint8* data = rootDirData;
  5110. bool failed = false;
  5111. int numStreams = GET(int32);
  5112. if (numStreams == 0)
  5113. return true;
  5114. mCvStreamSizes.Resize(numStreams);
  5115. mCvStreamPtrStartIdxs.Resize(numStreams);
  5116. int streamPages = 0;
  5117. for (int i = 0; i < (int)mCvStreamSizes.size(); i++)
  5118. mCvStreamSizes[i] = GET(int32);
  5119. for (int streamIdx = 0; streamIdx < numStreams; streamIdx++)
  5120. {
  5121. mCvStreamPtrStartIdxs[streamIdx] = streamPages;
  5122. if (mCvStreamSizes[streamIdx] > 0)
  5123. streamPages += (mCvStreamSizes[streamIdx] + pageSize - 1) / pageSize;
  5124. }
  5125. mCvStreamPtrs.Resize(streamPages);
  5126. for (int i = 0; i < (int)mCvStreamPtrs.size(); i++)
  5127. mCvStreamPtrs[i] = GET(int32);
  5128. //////////////////////////////////////////////////////////////////////////
  5129. if (!CvParseHeader(wantGuid, wantAge))
  5130. return false;
  5131. if (!CvParseDBI(wantAge))
  5132. return false;
  5133. if (mParseKind == ParseKind_Header)
  5134. return true;
  5135. ///
  5136. {
  5137. BP_ZONE("COFF::ParseCv_ReadStrTable");
  5138. if (mStringTable.mStream != -1)
  5139. {
  5140. mCvStrTableData = CvReadStream(mStringTable.mStream);
  5141. mStringTable.mStrTable = (const char*)mCvStrTableData + 12;
  5142. }
  5143. }
  5144. for (int compileUnitId = 0; compileUnitId < (int)mCvModuleInfo.size(); compileUnitId++)
  5145. {
  5146. ScanCompileUnit(compileUnitId);
  5147. }
  5148. //////////////////////////////////////////////////////////////////////////
  5149. return true;
  5150. }
  5151. void COFF::ReportMemory(MemReporter* memReporter)
  5152. {
  5153. DbgModule::ReportMemory(memReporter);
  5154. memReporter->AddVec("mCvTypeMap", mCvTypeMap);
  5155. memReporter->AddVec("mCvTagStartMap", mCvTagStartMap);
  5156. memReporter->AddVec("mCvIPITagStartMap", mCvIPITagStartMap);
  5157. memReporter->AddVec(mCvStreamSizes);
  5158. memReporter->AddVec(mCvStreamPtrStartIdxs);
  5159. memReporter->AddVec(mCvStreamPtrs);
  5160. memReporter->AddVec(mCvModuleInfo);
  5161. memReporter->AddVec(mCVSrcFileRefCache);
  5162. if (mCvHeaderData != NULL)
  5163. memReporter->Add("mCvHeaderData", mCvStreamSizes[1]);
  5164. for (auto& entry : mCvTypeSectionData)
  5165. {
  5166. if (entry.mSize != -1)
  5167. memReporter->Add("mCvTypeSectionData", entry.mSize);
  5168. else
  5169. memReporter->Add("mCvTypeSectionData", mCvStreamSizes[2]);
  5170. }
  5171. if (mCvStrTableData != NULL)
  5172. memReporter->Add("mCvStrTableData", mCvStreamSizes[mStringTable.mStream]);
  5173. if (mCvPublicSymbolData != NULL)
  5174. memReporter->Add("mCvPublicSymbolData", mCvStreamSizes[mCvPublicSymbolInfoStream]);
  5175. if (mCvGlobalSymbolData != NULL)
  5176. memReporter->Add("mCvGlobalSymbolData", mCvStreamSizes[mCvGlobalSymbolInfoStream]);
  5177. //memReporter->AddVec(mCvNamespaceStaticEntries);
  5178. }
  5179. bool COFF::TryLoadPDB(const String& pdbPath, uint8 wantGuid[16], int32 wantAge)
  5180. {
  5181. bool isVerifyOnly = mDebugger == NULL;
  5182. if (!mPDBPath.IsEmpty())
  5183. return false; // Already have a PDB loaded
  5184. mCvMappedFile = CreateFileA(pdbPath.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, NULL, NULL);
  5185. if (mCvMappedFile == INVALID_HANDLE_VALUE)
  5186. {
  5187. return false;
  5188. }
  5189. BP_ZONE("COFF::TryLoadPDB");
  5190. if (!isVerifyOnly && !(gDebugManager->GetOutputFilterFlags() & BfOutputFilterFlags_SymbolLoadMessages))
  5191. mDebugger->OutputMessage(StrFormat("Loading PDB %s\n", pdbPath.c_str()));
  5192. DWORD highFileSize = 0;
  5193. int fileSize = (int)GetFileSize(mCvMappedFile, &highFileSize);
  5194. mCvMappedFileMapping = CreateFileMapping(mCvMappedFile, NULL, PAGE_READONLY, 0, fileSize, NULL);
  5195. mCvMappedViewOfFile = MapViewOfFile(mCvMappedFileMapping, FILE_MAP_READ, 0, 0, fileSize);
  5196. mCvMappedFileSize = fileSize;
  5197. //FileStream pdbFS;
  5198. //if (!pdbFS.Open(pdbPath, "rb"))
  5199. //return false;
  5200. mPDBPath = pdbPath;
  5201. mCvDataStream = new SafeMemStream(mCvMappedViewOfFile, fileSize, false);
  5202. //Cv_HEADER
  5203. //pdbFS.Read
  5204. char sig[32];
  5205. mCvDataStream->Read(sig, 32);
  5206. if (memcmp(sig, MSF_SIGNATURE_700, 32) != 0)
  5207. {
  5208. if (!isVerifyOnly)
  5209. Fail("PDB signature error");
  5210. return false;
  5211. }
  5212. BfLogDbg("Loading PDB %s\n", pdbPath.c_str());
  5213. BP_ZONE_F("LoadCv %s", pdbPath.c_str());
  5214. int pageSize = mCvDataStream->ReadInt32();
  5215. int fpmPageNum = mCvDataStream->ReadInt32();
  5216. int totalPageCount = mCvDataStream->ReadInt32();
  5217. int rootDirSize = mCvDataStream->ReadInt32();
  5218. int unknown = mCvDataStream->ReadInt32();
  5219. int rootDirPtrs[NUM_ROOT_DIRS];
  5220. mCvDataStream->ReadT(rootDirPtrs);
  5221. bool failed = false;
  5222. mCvPageSize = pageSize;
  5223. for (int i = 0; i < 31; i++)
  5224. {
  5225. if (mCvPageSize < (1 << i))
  5226. break;
  5227. mCvPageBits = i;
  5228. }
  5229. int rootPageCount = (rootDirSize + pageSize - 1) / pageSize;
  5230. int rootPointersPages = (rootPageCount*sizeof(int32) + pageSize - 1) / pageSize;
  5231. int rootPageIdx = 0;
  5232. Array<uint8> rootDirData;
  5233. rootDirData.Resize(rootPageCount * pageSize);
  5234. for (int rootDirIdx = 0; rootDirIdx < rootPointersPages; rootDirIdx++)
  5235. {
  5236. int rootDirPtr = rootDirPtrs[rootDirIdx];
  5237. if (rootDirPtr == 0)
  5238. continue;
  5239. mCvDataStream->SetPos(rootDirPtr * pageSize);
  5240. int32 rootPages[1024];
  5241. mCvDataStream->Read(rootPages, pageSize);
  5242. for (int subRootPageIdx = 0; subRootPageIdx < pageSize / 4; subRootPageIdx++, rootPageIdx++)
  5243. {
  5244. if (rootPageIdx >= rootPageCount)
  5245. break;
  5246. int rootPagePtr = rootPages[subRootPageIdx];
  5247. if (rootPagePtr == 0)
  5248. break;
  5249. mCvDataStream->SetPos(rootPagePtr * pageSize);
  5250. mCvDataStream->Read(&rootDirData[rootPageIdx * pageSize], pageSize);
  5251. }
  5252. }
  5253. int startingTypeIdx = mTypes.size();
  5254. if (!ParseCv(*mCvDataStream, &rootDirData[0], pageSize, wantGuid, wantAge))
  5255. {
  5256. //mDebugger->OutputMessage("Failed to parse PDB\n");
  5257. return false;
  5258. }
  5259. if (mCvDataStream->mFailed)
  5260. return false;
  5261. //OutputDebugStrF("COFF::TryLoadPDB %s\n", pdbPath.c_str());
  5262. if (!isVerifyOnly)
  5263. mDebugger->ModuleChanged(this);
  5264. mPDBLoaded = true;
  5265. return true;
  5266. }
  5267. void COFF::ClosePDB()
  5268. {
  5269. delete mCvDataStream;
  5270. mCvDataStream = NULL;
  5271. delete mCvHeaderData;
  5272. mCvHeaderData = NULL;
  5273. delete mCvStrTableData;
  5274. mCvStrTableData = NULL;
  5275. for (auto& entry : mCvTypeSectionData)
  5276. delete entry.mData;
  5277. mCvTypeSectionData.Clear();
  5278. for (auto& entry : mCvCompileUnitData)
  5279. delete entry.mData;
  5280. mCvCompileUnitData.Clear();
  5281. delete mCvPublicSymbolData;
  5282. mCvPublicSymbolData = NULL;
  5283. delete mCvGlobalSymbolData;
  5284. mCvGlobalSymbolData = NULL;
  5285. delete mNewFPOData;
  5286. mNewFPOData = NULL;
  5287. if (mCvMappedViewOfFile != NULL)
  5288. ::UnmapViewOfFile(mCvMappedViewOfFile);
  5289. mCvMappedViewOfFile = NULL;
  5290. if (mCvMappedFileMapping != INVALID_HANDLE_VALUE)
  5291. ::CloseHandle(mCvMappedFileMapping);
  5292. mCvMappedFileMapping = NULL;
  5293. if (mCvMappedFile != INVALID_HANDLE_VALUE)
  5294. ::CloseHandle(mCvMappedFile);
  5295. mCvMappedFile = NULL;
  5296. for (auto kv : mHotLibMap)
  5297. delete kv.mValue;
  5298. mHotLibMap.Clear();
  5299. mHotLibSymMap.Clear();
  5300. delete mEmitSourceFile;
  5301. mEmitSourceFile = NULL;
  5302. }
  5303. bool COFF::LoadPDB(const String& pdbPath, uint8 wantGuid[16], int32 wantAge)
  5304. {
  5305. if (mDebugTarget->mTargetBinary == this)
  5306. {
  5307. // If we don't have to load the debug symbols from a remote source or the cache
  5308. // then we assume we were locally built, meaning we don't attempt to auto-load
  5309. // any symbols since we assume we already have what we built for all referenced
  5310. // dlls
  5311. mDebugTarget->mWasLocallyBuilt = FileExists(pdbPath);
  5312. }
  5313. memcpy(mWantPDBGuid, wantGuid, 16);
  5314. mWantAge = wantAge;
  5315. mDbgSymRequest = mDebugger->mDbgSymSrv.CreateRequest(mFilePath, pdbPath, wantGuid, wantAge);
  5316. mDbgSymRequest->SearchLocal();
  5317. if (!mDbgSymRequest->mFinalPDBPath.IsEmpty())
  5318. {
  5319. TryLoadPDB(mDbgSymRequest->mFinalPDBPath, wantGuid, wantAge);
  5320. mDebugger->mDbgSymSrv.ReleaseRequest(mDbgSymRequest);
  5321. mDbgSymRequest = NULL;
  5322. }
  5323. else
  5324. mMayBeOld = true;
  5325. String fileName = GetFileName(mFilePath);
  5326. mWantsAutoLoadDebugInfo = !mDebugTarget->mWasLocallyBuilt;
  5327. if ((fileName.Equals("KERNEL32.DLL", String::CompareKind_OrdinalIgnoreCase)) ||
  5328. (fileName.Equals("KERNELBASE.DLL", String::CompareKind_OrdinalIgnoreCase)) ||
  5329. (fileName.Equals("USER32.DLL", String::CompareKind_OrdinalIgnoreCase)) ||
  5330. (fileName.Equals("SHELL32.DLL", String::CompareKind_OrdinalIgnoreCase)) ||
  5331. (fileName.Equals("GDI32.DLL", String::CompareKind_OrdinalIgnoreCase)) ||
  5332. (fileName.Equals("GDI32FULL.DLL", String::CompareKind_OrdinalIgnoreCase)) ||
  5333. (fileName.Equals("NTDLL.DLL", String::CompareKind_OrdinalIgnoreCase)) ||
  5334. (fileName.Equals("COMDLG32.DLL", String::CompareKind_OrdinalIgnoreCase)) ||
  5335. (fileName.Equals("MSVCRT.DLL", String::CompareKind_OrdinalIgnoreCase)) ||
  5336. (fileName.Equals("COMBASE.DLL", String::CompareKind_OrdinalIgnoreCase)) ||
  5337. (fileName.Equals("UCRTBASE.DLL", String::CompareKind_OrdinalIgnoreCase)) ||
  5338. (fileName.Equals("RPCRT4.DLL", String::CompareKind_OrdinalIgnoreCase)) ||
  5339. (fileName.Equals("BCRYPTPRIMITIVES.DLL", String::CompareKind_OrdinalIgnoreCase)) ||
  5340. (fileName.Equals("SHCORE.DLL", String::CompareKind_OrdinalIgnoreCase)) ||
  5341. (fileName.Equals("WIN32U.DLL", String::CompareKind_OrdinalIgnoreCase)) ||
  5342. (fileName.Equals("MSWSOCK.DLL", String::CompareKind_OrdinalIgnoreCase)))
  5343. {
  5344. mWantsAutoLoadDebugInfo = false;
  5345. }
  5346. /*if (pdbPath.IndexOf('\\') != -1) // Do we have an absolute path at all?
  5347. {
  5348. if (TryLoadPDB(pdbPath, wantGuid, wantAge))
  5349. return true;
  5350. ClosePDB();
  5351. }*/
  5352. //char exePath[MAX_PATH];
  5353. //GetModuleFileNameA(NULL, exePath, sizeof(exePath));
  5354. /*String checkPath = ::GetFileDir(mFilePath);
  5355. checkPath += "/";
  5356. checkPath += GetFileName(pdbPath);
  5357. if (!FileNameEquals(checkPath, pdbPath))
  5358. {
  5359. if (TryLoadPDB(checkPath, wantGuid, wantAge))
  5360. return true;
  5361. }*/
  5362. return false;
  5363. }
  5364. bool COFF::CheckSection(const char* name, uint8* sectionData, int sectionSize)
  5365. {
  5366. if (strcmp(name, ".debug$T") == 0)
  5367. {
  5368. mDbgFlavor = DbgFlavor_MS;
  5369. DbgSectionData entry;
  5370. entry.mData = sectionData;
  5371. entry.mSize = sectionSize;
  5372. mCvTypeSectionData.Add(entry);
  5373. return true;
  5374. }
  5375. if (strcmp(name, ".debug$S") == 0)
  5376. {
  5377. DbgSectionData entry;
  5378. entry.mData = sectionData;
  5379. entry.mSize = sectionSize;
  5380. mCvCompileUnitData.Add(entry);
  5381. return true;
  5382. }
  5383. return false;
  5384. }
  5385. void COFF::ProcessDebugInfo()
  5386. {
  5387. BP_ZONE("COFF::ProcessDebugInfo");
  5388. if ((!mCvTypeSectionData.IsEmpty()) && (!mCvCompileUnitData.IsEmpty()))
  5389. {
  5390. auto linkedModule = (COFF*)GetLinkedModule();
  5391. int startingTypeIdx = (int)linkedModule->mTypes.size();
  5392. InitCvTypes();
  5393. for (auto entry : mCvTypeSectionData)
  5394. {
  5395. uint8* data = entry.mData;
  5396. GET_INTO(uint32, infoType);
  5397. BF_ASSERT(infoType == CV_SIGNATURE_C13);
  5398. CvInitStreamRaw(mCvTypeSectionReader, entry.mData + 4, entry.mSize - 4);
  5399. ParseTypeData(mCvTypeSectionReader, 0);
  5400. }
  5401. FixTypes(startingTypeIdx);
  5402. linkedModule->MapTypes(startingTypeIdx);
  5403. CvCompileUnit* compileUnit = NULL;
  5404. for (auto entry : mCvCompileUnitData)
  5405. {
  5406. compileUnit = ParseCompileUnit(NULL, compileUnit, entry.mData, entry.mSize);
  5407. }
  5408. if (compileUnit != NULL)
  5409. {
  5410. compileUnit->mLanguage = DbgLanguage_Beef;
  5411. mMasterCompileUnit->mLanguage = DbgLanguage_Beef;
  5412. MapCompileUnitMethods(compileUnit);
  5413. mEndTypeIdx = (int)linkedModule->mTypes.size();
  5414. }
  5415. }
  5416. }
  5417. void COFF::FinishHotSwap()
  5418. {
  5419. DbgModule::FinishHotSwap();
  5420. mTypeMap.Clear();
  5421. }
  5422. intptr COFF::EvaluateLocation(DbgSubprogram* dwSubprogram, const uint8* locData, int locDataLen, WdStackFrame* stackFrame, DbgAddrType* outAddrType, DbgEvalLocFlags flags)
  5423. {
  5424. if (mDbgFlavor == DbgFlavor_GNU)
  5425. return DbgModule::EvaluateLocation(dwSubprogram, locData, locDataLen, stackFrame, outAddrType, flags);
  5426. if ((locDataLen == 0) && (locData != NULL))
  5427. {
  5428. // locData is actually a string, the aliased name
  5429. *outAddrType = DbgAddrType_Alias;
  5430. return (intptr)locData;
  5431. }
  5432. addr_target pc = 0;
  5433. if (stackFrame != NULL)
  5434. {
  5435. // Use 'GetSourcePC', which will offset the RSP when we're not at the top position of the call stack, since RSP will be the
  5436. // return address in those cases
  5437. pc = stackFrame->GetSourcePC();
  5438. }
  5439. int inlineDepth = 0;
  5440. uint8* data = (uint8*)locData;
  5441. for (int locIdx = 0; locIdx < locDataLen; locIdx++)
  5442. {
  5443. uint8* dataStart = data;
  5444. GET_INTO(uint16, symLen);
  5445. uint8* dataEnd = data + symLen;
  5446. GET_INTO(uint16, symType);
  5447. CV_LVAR_ADDR_RANGE* rangeInfo = NULL;
  5448. CV_LVAR_ADDR_GAP* gapsInfo = NULL;
  5449. addr_target result = 0;
  5450. if (inlineDepth > 0)
  5451. {
  5452. if (symType == S_INLINESITE)
  5453. inlineDepth++;
  5454. else if (symType == S_INLINESITE_END)
  5455. inlineDepth--;
  5456. data = dataEnd;
  5457. continue;
  5458. }
  5459. bool handled = false;
  5460. switch (symType)
  5461. {
  5462. case S_GDATA32:
  5463. case S_LDATA32:
  5464. {
  5465. DATASYM32& dataSym = *(DATASYM32*)dataStart;
  5466. *outAddrType = DbgAddrType_Target;
  5467. return GetSectionAddr(dataSym.seg, dataSym.off);
  5468. }
  5469. break;
  5470. case S_BPREL32:
  5471. {
  5472. BPRELSYM32* bpRel32 = (BPRELSYM32*)dataStart;
  5473. #if BF_DBG_32
  5474. *outAddrType = DbgAddrType_Target;
  5475. return stackFrame->mRegisters.mIntRegs.ebp + (int)bpRel32->off;
  5476. #else
  5477. *outAddrType = DbgAddrType_Target;
  5478. return stackFrame->mRegisters.mIntRegs.rbp + (int)bpRel32->off;
  5479. #endif
  5480. }
  5481. break;
  5482. case S_LTHREAD32:
  5483. case S_GTHREAD32:
  5484. {
  5485. THREADSYM32& threadSym = *(THREADSYM32*)dataStart;
  5486. int tlsIndex = mDebugger->ReadMemory<int>(mTLSIndexAddr);
  5487. addr_target tlsEntry = mDebugger->GetTLSOffset(tlsIndex);
  5488. *outAddrType = DbgAddrType_Target;
  5489. return threadSym.off + tlsEntry;
  5490. }
  5491. break;
  5492. case S_REGISTER:
  5493. {
  5494. REGSYM* regSym = (REGSYM*)dataStart;
  5495. *outAddrType = DbgAddrType_Register;
  5496. int regNum = CvConvRegNum(regSym->reg);
  5497. return regNum;
  5498. }
  5499. break;
  5500. case S_REGREL32:
  5501. {
  5502. REGREL32* regRel32 = (REGREL32*)dataStart;
  5503. *outAddrType = DbgAddrType_Target;
  5504. int regNum = CvConvRegNum(regRel32->reg);
  5505. return stackFrame->mRegisters.mIntRegsArray[regNum] + regRel32->off;
  5506. }
  5507. break;
  5508. case S_DEFRANGE_REGISTER:
  5509. {
  5510. DEFRANGESYMREGISTER& defRangeReg = *(DEFRANGESYMREGISTER*)dataStart;
  5511. *outAddrType = DbgAddrType_Register;
  5512. result = CvConvRegNum(defRangeReg.reg);
  5513. rangeInfo = &defRangeReg.range;
  5514. gapsInfo = &defRangeReg.gaps[0];
  5515. }
  5516. break;
  5517. case S_DEFRANGE_FRAMEPOINTER_REL:
  5518. {
  5519. DEFRANGESYMFRAMEPOINTERREL& defRangeFPRel = *(DEFRANGESYMFRAMEPOINTERREL*)dataStart;
  5520. DbgSubprogram::LocalBaseRegKind baseReg = ((flags & DbgEvalLocFlag_IsParam) != 0) ? dwSubprogram->mParamBaseReg : dwSubprogram->mLocalBaseReg;
  5521. *outAddrType = DbgAddrType_Target;
  5522. #ifdef BF_DBG_64
  5523. if (baseReg == DbgSubprogram::LocalBaseRegKind_RSP)
  5524. result = stackFrame->mRegisters.mIntRegsArray[X64Reg_RSP] + defRangeFPRel.offFramePointer;
  5525. else if (baseReg == DbgSubprogram::LocalBaseRegKind_R13)
  5526. result = stackFrame->mRegisters.mIntRegsArray[X64Reg_R13] + defRangeFPRel.offFramePointer;
  5527. else
  5528. result = stackFrame->mRegisters.mIntRegsArray[X64Reg_RBP] + defRangeFPRel.offFramePointer;
  5529. #else
  5530. if (baseReg == DbgSubprogram::LocalBaseRegKind_VFRAME)
  5531. result = stackFrame->mRegisters.mIntRegsArray[X86Reg_ESP] + dwSubprogram->mFrameBaseLen + defRangeFPRel.offFramePointer;
  5532. else if (baseReg == DbgSubprogram::LocalBaseRegKind_EBX)
  5533. result = stackFrame->mRegisters.mIntRegsArray[X86Reg_EBX] + defRangeFPRel.offFramePointer;
  5534. else
  5535. result = stackFrame->mRegisters.mIntRegsArray[X86Reg_EBP] + defRangeFPRel.offFramePointer;
  5536. #endif
  5537. rangeInfo = &defRangeFPRel.range;
  5538. gapsInfo = &defRangeFPRel.gaps[0];
  5539. }
  5540. break;
  5541. case S_DEFRANGE_SUBFIELD_REGISTER:
  5542. {
  5543. DEFRANGESYMSUBFIELDREGISTER& defRangeSubfieldReg = *(DEFRANGESYMSUBFIELDREGISTER*)dataStart;
  5544. *outAddrType = DbgAddrType_Target;
  5545. int regNum = CvConvRegNum(defRangeSubfieldReg.reg);
  5546. result = stackFrame->mRegisters.mIntRegsArray[regNum] + defRangeSubfieldReg.offParent;
  5547. rangeInfo = &defRangeSubfieldReg.range;
  5548. gapsInfo = &defRangeSubfieldReg.gaps[0];
  5549. }
  5550. break;
  5551. case S_DEFRANGE_FRAMEPOINTER_REL_FULL_SCOPE:
  5552. {
  5553. DEFRANGESYMFRAMEPOINTERREL_FULL_SCOPE& defFPRel = *(DEFRANGESYMFRAMEPOINTERREL_FULL_SCOPE*)dataStart;
  5554. #ifdef BF_DBG_64
  5555. int regNum = X64Reg_RSP;
  5556. #else
  5557. int regNum = X86Reg_ESP;
  5558. #endif
  5559. *outAddrType = DbgAddrType_Target;
  5560. result = stackFrame->mRegisters.mIntRegsArray[regNum] + defFPRel.offFramePointer;
  5561. return result;
  5562. }
  5563. break;
  5564. case S_DEFRANGE_REGISTER_REL:
  5565. {
  5566. DEFRANGESYMREGISTERREL& defRangeRegRel = *(DEFRANGESYMREGISTERREL*)dataStart;
  5567. *outAddrType = DbgAddrType_Target;
  5568. int regNum = CvConvRegNum(defRangeRegRel.baseReg);
  5569. result = stackFrame->mRegisters.mIntRegsArray[regNum] + defRangeRegRel.offBasePointer;
  5570. rangeInfo = &defRangeRegRel.range;
  5571. gapsInfo = &defRangeRegRel.gaps[0];
  5572. }
  5573. break;
  5574. case S_PROCREF:
  5575. {
  5576. // Not currently handled.
  5577. }
  5578. break;
  5579. case S_INLINESITE:
  5580. {
  5581. inlineDepth++;
  5582. }
  5583. break;
  5584. case S_FILESTATIC:
  5585. {
  5586. FILESTATICSYM& fileStaticSym = *(FILESTATICSYM*)dataStart;
  5587. }
  5588. break;
  5589. default:
  5590. if (!mFailed)
  5591. SoftFail(StrFormat("Unknown symbol type '0x%X' in EvaluateLocation", symType));
  5592. return 0;
  5593. }
  5594. if (rangeInfo != NULL)
  5595. {
  5596. auto rangeStart = GetSectionAddr(rangeInfo->isectStart, rangeInfo->offStart);
  5597. if ((pc >= rangeStart) && (pc < rangeStart + rangeInfo->cbRange))
  5598. {
  5599. bool inRange = true;
  5600. while ((uint8*)gapsInfo < (uint8*)dataEnd)
  5601. {
  5602. if ((pc >= rangeStart + gapsInfo->gapStartOffset) && (pc < rangeStart + gapsInfo->gapStartOffset + gapsInfo->cbRange))
  5603. {
  5604. inRange = false;
  5605. break;
  5606. }
  5607. gapsInfo++;
  5608. }
  5609. if (inRange)
  5610. return result;
  5611. }
  5612. }
  5613. data = dataEnd;
  5614. }
  5615. if ((dwSubprogram != NULL) && (dwSubprogram->mIsOptimized))
  5616. *outAddrType = DbgAddrType_OptimizedOut;
  5617. else
  5618. *outAddrType = DbgAddrType_NoValue;
  5619. return 0;
  5620. }
  5621. bool COFF::CanGetOldSource()
  5622. {
  5623. return mCvSrcSrvStream != -1;
  5624. }
  5625. String COFF::GetOldSourceCommand(const StringImpl& path)
  5626. {
  5627. if (mCvSrcSrvStream == -1)
  5628. return "";
  5629. int outSize;
  5630. uint8* data = CvReadStream(mCvSrcSrvStream, &outSize);
  5631. String cmdBlock = String((char*)data, outSize);
  5632. delete data;
  5633. bool inFileSection = false;
  5634. Dictionary<String, String> defs;
  5635. enum _SectType
  5636. {
  5637. _SectType_None,
  5638. _SectType_Ini,
  5639. _SectType_Variables,
  5640. _SectType_SourceFiles
  5641. };
  5642. _SectType sectType = _SectType_None;
  5643. //
  5644. {
  5645. AutoCrit autoCrit(gDebugManager->mCritSect);
  5646. defs["TARG"] = gDebugManager->mSymSrvOptions.mSourceServerCacheDir;
  5647. }
  5648. std::function<void (String&)> _Expand = [&](String& str)
  5649. {
  5650. for (int i = 0; i < str.length(); i++)
  5651. {
  5652. if (str[i] == '%')
  5653. {
  5654. int endIdx = str.IndexOf('%', i + 1);
  5655. if (endIdx != -1)
  5656. {
  5657. String varName = ToUpper(str.Substring(i + 1, endIdx - i - 1));
  5658. if (((endIdx < str.length() - 1) && (str[endIdx + 1] == '(')) &&
  5659. ((varName == "FNVAR") || (varName == "FNBKSL") || (varName == "FNFILE")))
  5660. {
  5661. int closePos = str.IndexOf(')', endIdx + 2);
  5662. if (closePos != -1)
  5663. {
  5664. String paramStr = str.Substring(endIdx + 2, closePos - endIdx - 2);
  5665. _Expand(paramStr);
  5666. if (varName == "FNVAR")
  5667. {
  5668. paramStr = defs[paramStr];
  5669. }
  5670. else
  5671. {
  5672. if (varName == "FNBKSL")
  5673. {
  5674. paramStr.Replace("/", "\\");
  5675. }
  5676. else if (varName == "FNFILE")
  5677. {
  5678. paramStr = GetFileName(paramStr);
  5679. }
  5680. }
  5681. str.Remove(i, closePos - i + 1);
  5682. str.Insert(i, paramStr);
  5683. i--;
  5684. }
  5685. }
  5686. else
  5687. {
  5688. auto replaceStr = defs[varName];
  5689. if (!replaceStr.IsEmpty())
  5690. {
  5691. str.Remove(i, endIdx - i + 1);
  5692. str.Insert(i, replaceStr);
  5693. i--;
  5694. }
  5695. }
  5696. }
  5697. }
  5698. }
  5699. };
  5700. int linePos = 0;
  5701. while (true)
  5702. {
  5703. int crPos = cmdBlock.IndexOf('\n', linePos);
  5704. if (crPos == -1)
  5705. break;
  5706. int lineEndPos = crPos;
  5707. if (cmdBlock[lineEndPos - 1] == '\r')
  5708. lineEndPos--;
  5709. String line = cmdBlock.Substring(linePos, lineEndPos - linePos);
  5710. if (line.StartsWith("SRCSRV: ini -"))
  5711. {
  5712. sectType = _SectType_Ini;
  5713. }
  5714. else if (line.StartsWith("SRCSRV: variables -"))
  5715. {
  5716. sectType = _SectType_Variables;
  5717. }
  5718. else if (line.StartsWith("SRCSRV: source files -"))
  5719. {
  5720. sectType = _SectType_SourceFiles;
  5721. }
  5722. else if (line.StartsWith("SRCSRV: end -"))
  5723. {
  5724. break;
  5725. }
  5726. else
  5727. {
  5728. switch (sectType)
  5729. {
  5730. case _SectType_Ini:
  5731. case _SectType_Variables:
  5732. {
  5733. int eqPos = line.IndexOf('=');
  5734. if (eqPos != -1)
  5735. {
  5736. defs[ToUpper(line.Substring(0, eqPos))] = line.Substring(eqPos + 1);
  5737. }
  5738. break;
  5739. }
  5740. case _SectType_SourceFiles:
  5741. {
  5742. int curStrIdx = 0;
  5743. int curStrPos = 0;
  5744. StringT<256> var;
  5745. bool matches = false;
  5746. while (curStrPos < (int)line.length())
  5747. {
  5748. int starPos = line.IndexOf('*', curStrPos);
  5749. if (starPos == -1)
  5750. starPos = line.length();
  5751. //var = line.Substring(curStrPos, starPos - curStrPos);
  5752. var.Clear();
  5753. var.Insert(0, line.c_str() + curStrPos, starPos - curStrPos);
  5754. if (curStrIdx == 0)
  5755. {
  5756. matches = FileNameEquals(var, path);
  5757. if (!matches)
  5758. break;
  5759. }
  5760. defs[StrFormat("VAR%d", curStrIdx + 1)] = var;
  5761. curStrIdx++;
  5762. curStrPos = starPos + 1;
  5763. }
  5764. if (!matches)
  5765. break;
  5766. String target = defs["SRCSRVTRG"];
  5767. String cmd = defs["SRCSRVCMD"];
  5768. String env = defs["SRCSRVENV"];
  5769. _Expand(target);
  5770. _Expand(cmd);
  5771. _Expand(env);
  5772. String retVal;
  5773. if ((cmd.IsEmpty()) && (target.StartsWith("HTTP", StringImpl::CompareKind_OrdinalIgnoreCase)))
  5774. {
  5775. String localFile;
  5776. BfpFileResult result;
  5777. BFP_GETSTR_HELPER(localFile, result, BfpFile_GetTempPath(__STR, __STRLEN, &result));
  5778. int dotPos = target.IndexOf("://");
  5779. if (dotPos != -1)
  5780. {
  5781. localFile.Append("SymbolCache\\src\\");
  5782. localFile.Append(StringView(target, dotPos + 3));
  5783. localFile.Replace("/", "\\");
  5784. }
  5785. retVal = localFile;
  5786. retVal += "\n";
  5787. retVal += target;
  5788. retVal += "\n";
  5789. retVal += env;
  5790. }
  5791. else if (!cmd.IsWhitespace())
  5792. {
  5793. retVal = target;
  5794. retVal += "\n";
  5795. retVal += cmd;
  5796. retVal += "\n";
  5797. retVal += env;
  5798. }
  5799. return retVal;
  5800. }
  5801. break;
  5802. }
  5803. }
  5804. linePos = crPos + 1;
  5805. }
  5806. return "";
  5807. }
  5808. bool COFF::GetEmitSource(const StringImpl& filePath, String& outText)
  5809. {
  5810. if (!filePath.StartsWith("$Emit"))
  5811. return false;
  5812. if (mEmitSourceFile == NULL)
  5813. {
  5814. mEmitSourceFile = new ZipFile();
  5815. String zipPath = mPDBPath;
  5816. int dotPos = zipPath.LastIndexOf('.');
  5817. zipPath.RemoveToEnd(dotPos);
  5818. zipPath.Append("__emit.zip");
  5819. if (!mEmitSourceFile->Open(zipPath))
  5820. {
  5821. if (mCvEmitStream == -1)
  5822. return "";
  5823. int outSize;
  5824. uint8* data = CvReadStream(mCvEmitStream, &outSize);
  5825. FileStream fileStream;
  5826. fileStream.Open(zipPath, "wb");
  5827. fileStream.Write(data, outSize);
  5828. fileStream.Close();
  5829. delete data;
  5830. mEmitSourceFile->Open(zipPath);
  5831. }
  5832. }
  5833. if (mEmitSourceFile->IsOpen())
  5834. {
  5835. String usePath = filePath;
  5836. if (usePath.StartsWith("$Emit"))
  5837. {
  5838. int dollarPos = usePath.IndexOf('$', 1);
  5839. usePath.Remove(0, dollarPos + 1);
  5840. }
  5841. usePath = EncodeFileName(usePath);
  5842. usePath.Append(".bf");
  5843. Array<uint8> data;
  5844. if (mEmitSourceFile->Get(usePath, data))
  5845. {
  5846. outText.Insert(outText.mLength, (char*)data.mVals, data.mSize);
  5847. return true;
  5848. }
  5849. }
  5850. return false;
  5851. }
  5852. bool COFF::HasPendingDebugInfo()
  5853. {
  5854. if (mDbgSymRequest == NULL)
  5855. return false;
  5856. return true;
  5857. }
  5858. void COFF::PreCacheImage()
  5859. {
  5860. if (!mDebugger->IsMiniDumpDebugger())
  5861. return;
  5862. if (mLoadState != DbgModuleLoadState_NotLoaded)
  5863. return;
  5864. auto miniDumpDebugger = (MiniDumpDebugger*)mDebugger;
  5865. if (mOrigImageData != NULL)
  5866. return;
  5867. mDebugger->mDbgSymSrv.PreCacheImage(mFilePath, mTimeStamp, mImageSize);
  5868. }
  5869. void COFF::PreCacheDebugInfo()
  5870. {
  5871. if (mDbgSymRequest == NULL)
  5872. return;
  5873. if (mDbgSymRequest->mInProcess)
  5874. return;
  5875. bool hasDebugInfo = false;
  5876. mDbgSymRequest->SearchCache();
  5877. if (mDbgSymRequest->mFinalPDBPath.IsEmpty())
  5878. {
  5879. if (GetCurrentThreadId() == mDebugger->mDebuggerThreadId)
  5880. {
  5881. mDbgSymRequest->mIsPreCache = true;
  5882. mDbgSymRequest->SearchSymSrv();
  5883. mDbgSymRequest->mIsPreCache = false;
  5884. mDbgSymRequest->mFailed = false;
  5885. }
  5886. }
  5887. }
  5888. bool COFF::LoadModuleImage(const StringImpl& imagePath)
  5889. {
  5890. if (!mDebugger->IsMiniDumpDebugger())
  5891. return false;
  5892. auto miniDumpDebugger = (MiniDumpDebugger*)mDebugger;
  5893. if (!imagePath.IsEmpty())
  5894. {
  5895. MappedFile* mappedFile = miniDumpDebugger->MapModule(this, imagePath);
  5896. mMappedImageFile = mappedFile;
  5897. if (mappedFile != NULL)
  5898. {
  5899. MemStream memStream(mappedFile->mData, mappedFile->mFileSize, false);
  5900. ReadCOFF(&memStream, DbgModuleKind_Module);
  5901. mOrigImageData = new DbgModuleMemoryCache(mImageBase, mImageSize);
  5902. }
  5903. }
  5904. if (mOrigImageData == NULL) // Failed?
  5905. {
  5906. mLoadState = DbgModuleLoadState_Failed;
  5907. return false;
  5908. }
  5909. return true;
  5910. }
  5911. bool COFF::RequestImage()
  5912. {
  5913. if (!mDebugger->IsMiniDumpDebugger())
  5914. return false;
  5915. if (mLoadState != DbgModuleLoadState_NotLoaded)
  5916. return false;
  5917. auto miniDumpDebugger = (MiniDumpDebugger*)mDebugger;
  5918. if (mOrigImageData != NULL)
  5919. return false;
  5920. if (GetCurrentThreadId() == mDebugger->mDebuggerThreadId)
  5921. {
  5922. auto prevRunState = mDebugger->mRunState;
  5923. mDebugger->mRunState = RunState_SearchingSymSrv;
  5924. mDebugger->mDebugManager->mOutMessages.push_back(StrFormat("symsrv Searching for image '%s'", mFilePath.c_str()));
  5925. auto dbgSymRequest = mDebugger->mDbgSymSrv.CreateRequest();
  5926. mDebugger->mActiveSymSrvRequest = dbgSymRequest;
  5927. BF_ASSERT(mDebugger->mDebugManager->mCritSect.mLockCount == 1);
  5928. mDebugger->mDebugManager->mCritSect.Unlock();
  5929. // We unlock to allow the IDE to continue updating while we search
  5930. String imagePath = dbgSymRequest->SearchForImage(mFilePath, mTimeStamp, mImageSize);
  5931. mDebugger->mDebugManager->mCritSect.Lock();
  5932. delete mDebugger->mActiveSymSrvRequest;
  5933. mDebugger->mActiveSymSrvRequest = NULL;
  5934. mDebugger->mRunState = prevRunState;
  5935. return LoadModuleImage(imagePath);
  5936. }
  5937. else
  5938. {
  5939. return false;
  5940. }
  5941. return false;
  5942. }
  5943. bool COFF::RequestDebugInfo(bool allowRemote)
  5944. {
  5945. if (mDbgSymRequest == NULL)
  5946. return false;
  5947. if (mDbgSymRequest->mInProcess)
  5948. return false;
  5949. bool hasDebugInfo = false;
  5950. mDbgSymRequest->SearchCache();
  5951. if (mDbgSymRequest->mFinalPDBPath.IsEmpty())
  5952. {
  5953. if (!allowRemote)
  5954. return false;
  5955. if (GetCurrentThreadId() == mDebugger->mDebuggerThreadId)
  5956. {
  5957. auto prevRunState = mDebugger->mRunState;
  5958. mDebugger->mRunState = RunState_SearchingSymSrv;
  5959. mDbgSymRequest->mInProcess = true;
  5960. mDebugger->mActiveSymSrvRequest = mDbgSymRequest;
  5961. BF_ASSERT(mDebugger->mDebugManager->mCritSect.mLockCount == 1);
  5962. mDebugger->mDebugManager->mCritSect.Unlock();
  5963. mDbgSymRequest->SearchSymSrv();
  5964. mDebugger->mDebugManager->mCritSect.Lock();
  5965. mDebugger->mActiveSymSrvRequest = NULL;
  5966. mDbgSymRequest->mInProcess = false;
  5967. mDebugger->mRunState = prevRunState;
  5968. }
  5969. else
  5970. {
  5971. return false;
  5972. }
  5973. }
  5974. if (!mDbgSymRequest->mFinalPDBPath.IsEmpty())
  5975. hasDebugInfo = TryLoadPDB(mDbgSymRequest->mFinalPDBPath, mDbgSymRequest->mWantGuid, mDbgSymRequest->mWantAge);
  5976. mDebugger->mDbgSymSrv.ReleaseRequest(mDbgSymRequest);
  5977. mDbgSymRequest = NULL;
  5978. return hasDebugInfo;
  5979. }
  5980. bool COFF::WantsAutoLoadDebugInfo()
  5981. {
  5982. return mWantsAutoLoadDebugInfo;
  5983. }
  5984. bool COFF::DbgIsStrMutable(const char* str)
  5985. {
  5986. if (IsObjectFile())
  5987. {
  5988. return GetLinkedModule()->DbgIsStrMutable(str);
  5989. }
  5990. if (mCvMappedViewOfFile == NULL)
  5991. return true;
  5992. if ((str >= (const char*)mCvMappedViewOfFile) && (str < (const char*)mCvMappedViewOfFile + mCvMappedFileSize))
  5993. return false;
  5994. return true;
  5995. }
  5996. // We don't need this for Beef linkining because of "FORCELINK_", but we DO need it for
  5997. // libraries we linked to, either in the DLL or in the LIB
  5998. addr_target COFF::LocateSymbol(const StringImpl& name)
  5999. {
  6000. BP_ZONE("COFF::LocateSymbol");
  6001. BfLogDbg("COFF:LocateSymbol '%s'\n", name.c_str());
  6002. if (mHotLibMap.IsEmpty())
  6003. {
  6004. for (auto moduleInfo : mCvModuleInfo)
  6005. {
  6006. if (moduleInfo->mObjectName == NULL)
  6007. continue;
  6008. String libName = moduleInfo->mObjectName;
  6009. if (!libName.EndsWith(".lib", StringImpl::CompareKind_OrdinalIgnoreCase))
  6010. continue;
  6011. CvLibInfo** libInfoPtr;
  6012. if (!mHotLibMap.TryAdd(libName, NULL, &libInfoPtr))
  6013. continue;
  6014. CvLibInfo* libInfo = new CvLibInfo();
  6015. *libInfoPtr = libInfo;
  6016. if (!libInfo->mLibFile.Init(libName, false))
  6017. continue;
  6018. for (auto kv : libInfo->mLibFile.mOldEntries)
  6019. {
  6020. auto libEntry = kv.mValue;
  6021. while (libEntry != NULL)
  6022. {
  6023. for (auto sym : libEntry->mSymbols)
  6024. {
  6025. #ifdef BF_DBG_32
  6026. if (sym.StartsWith('_'))
  6027. mHotLibSymMap[sym.Substring(1)] = libEntry;
  6028. else
  6029. // Fallthrough
  6030. #endif
  6031. mHotLibSymMap[sym] = libEntry;
  6032. }
  6033. libEntry = libEntry->mNextWithSameName;
  6034. }
  6035. }
  6036. }
  6037. }
  6038. BeLibEntry* libEntry;
  6039. if (!mHotLibSymMap.TryGetValue(name, &libEntry))
  6040. return 0;
  6041. if (libEntry->mLength == -1)
  6042. {
  6043. BfLogDbg("Already loaded obj '%s' in '%s'\n", libEntry->mName.c_str(), libEntry->mLibFile->mFilePath.c_str());
  6044. // We already tried to load this
  6045. return 0;
  6046. }
  6047. auto fileExt = GetFileExtension(libEntry->mName);
  6048. if (String::Equals(fileExt, ".dll", StringImpl::CompareKind_OrdinalIgnoreCase))
  6049. {
  6050. for (auto dbgModule : mDebugTarget->mDbgModules)
  6051. {
  6052. if (String::Equals(libEntry->mName, dbgModule->mDisplayName))
  6053. {
  6054. dbgModule->ParseSymbolData();
  6055. auto entry = dbgModule->mSymbolNameMap.Find(name.c_str());
  6056. if (entry == NULL)
  6057. continue;
  6058. addr_target callTarget;
  6059. #ifdef BF_DBG_32
  6060. callTarget = entry->mValue->mAddress;
  6061. #else
  6062. BfLogDbg("Setting up DLL thunk for '%s' in '%s'\n", name.c_str(), dbgModule->mFilePath.c_str());
  6063. int wantHotSize = 16;
  6064. if (mHotThunkDataLeft < wantHotSize)
  6065. {
  6066. int allocSize = 4096;
  6067. mDebugger->ReserveHotTargetMemory(allocSize);
  6068. int outAllocSize = 0;
  6069. mHotThunkCurAddr = mDebugger->AllocHotTargetMemory(allocSize, true, false, &outAllocSize);
  6070. mHotThunkDataLeft = outAllocSize;
  6071. }
  6072. int outAllocSize = 0;
  6073. addr_target hotAddr = mHotThunkCurAddr;
  6074. mHotThunkCurAddr += wantHotSize;
  6075. mHotThunkDataLeft -= wantHotSize;
  6076. #pragma pack(push, 1)
  6077. struct HotLongJmpOp
  6078. {
  6079. uint8 mOpCode0;
  6080. uint8 mOpCode1;
  6081. int32 mRIPRel;
  6082. uint64 mTarget;
  6083. };
  6084. #pragma pack(pop)
  6085. HotLongJmpOp jumpOp;
  6086. jumpOp.mOpCode0 = 0xFF;
  6087. jumpOp.mOpCode1 = 0x25;
  6088. jumpOp.mRIPRel = 0;
  6089. jumpOp.mTarget = entry->mValue->mAddress;
  6090. mDebugger->WriteMemory(hotAddr, jumpOp);
  6091. callTarget = hotAddr;
  6092. #endif
  6093. char* dupStr = (char*)mAlloc.AllocBytes(name.length() + 1);
  6094. memcpy(dupStr, name.c_str(), name.length());
  6095. DbgSymbol* dwSymbol = mAlloc.Alloc<DbgSymbol>();
  6096. dwSymbol->mDbgModule = this;
  6097. dwSymbol->mName = dupStr;
  6098. dwSymbol->mAddress = callTarget;
  6099. mSymbolNameMap.Insert(dwSymbol);
  6100. return callTarget;
  6101. }
  6102. }
  6103. return 0;
  6104. }
  6105. BfLogDbg("Loading obj '%s' in '%s'\n", libEntry->mName.c_str(), libEntry->mLibFile->mFilePath.c_str());
  6106. // #ifdef _DEBUG
  6107. // FILE* fpTest = fopen("c:\\temp\\locateSym.obj", "wb");
  6108. //
  6109. // uint8* data = new uint8[libEntry->mLength];
  6110. //
  6111. // fseek(libEntry->mLibFile->mOldFileStream.mFP, libEntry->mOldDataPos + sizeof(BeLibMemberHeader), SEEK_SET);
  6112. // fread(data, 1, libEntry->mLength, libEntry->mLibFile->mOldFileStream.mFP);
  6113. // fwrite(data, 1, libEntry->mLength, fpTest);
  6114. // fclose(fpTest);
  6115. // delete data;
  6116. // #endif
  6117. FileSubStream fileStream;
  6118. fileStream.mFP = libEntry->mLibFile->mOldFileStream.mFP;
  6119. fileStream.mOffset = libEntry->mOldDataPos + sizeof(BeLibMemberHeader);
  6120. fileStream.mSize = libEntry->mLength;
  6121. fileStream.SetPos(0);
  6122. DbgModule* dbgModule = new COFF(mDebugger->mDebugTarget);
  6123. dbgModule->mHotIdx = mDebugger->mActiveHotIdx;
  6124. dbgModule->mFilePath = libEntry->mName + "@" + libEntry->mLibFile->mFilePath;
  6125. bool success = dbgModule->ReadCOFF(&fileStream, DbgModuleKind_FromLocateSymbol);
  6126. fileStream.mFP = NULL;
  6127. // Mark as loaded
  6128. libEntry->mLength = -1;
  6129. if (!success)
  6130. {
  6131. BfLogDbg("Failed\n");
  6132. Fail(StrFormat("Debugger failed to read binary '%s' in '%s'", libEntry->mName.c_str(), libEntry->mLibFile->mFilePath.c_str()));
  6133. delete dbgModule;
  6134. return 0;
  6135. }
  6136. mDebugger->mDebugTarget->AddDbgModule(dbgModule);
  6137. auto symbolEntry = mSymbolNameMap.Find(name.c_str());
  6138. if (symbolEntry != NULL)
  6139. return symbolEntry->mValue->mAddress;
  6140. return 0;
  6141. }
  6142. void COFF::ParseFrameDescriptors(uint8* data, int size, addr_target baseAddr)
  6143. {
  6144. uint8* ptr = data;
  6145. uint8* endPtr = data + size;
  6146. Dictionary<int, COFFFrameProgram> programMap;
  6147. Array<COFFFrameProgram::Command> cmds;
  6148. while (ptr < endPtr)
  6149. {
  6150. COFFFrameDescriptor* frame = (COFFFrameDescriptor*)ptr;
  6151. ptr += sizeof(COFFFrameDescriptor);
  6152. COFFFrameDescriptorEntry entry;
  6153. entry.mFrameDescriptor = frame;
  6154. bool failed = false;
  6155. COFFFrameProgram* programPtr = NULL;
  6156. if (programMap.TryAdd(frame->mFrameFunc, NULL, &programPtr))
  6157. {
  6158. COFFFrameProgram program;
  6159. cmds.Clear();
  6160. String curCmd;
  6161. const char* ptr = mStringTable.mStrTable + frame->mFrameFunc;
  6162. while (true)
  6163. {
  6164. char c = *(ptr++);
  6165. if ((c == ' ') || (c == 0))
  6166. {
  6167. if (curCmd == "$eip")
  6168. cmds.Add(COFFFrameProgram::Command_EIP);
  6169. else if ((curCmd == "$esp") || (curCmd == "$21"))
  6170. cmds.Add(COFFFrameProgram::Command_ESP);
  6171. else if ((curCmd == "$ebp") || (curCmd == "$22"))
  6172. cmds.Add(COFFFrameProgram::Command_EBP);
  6173. else if ((curCmd == "$eax") || (curCmd == "$17"))
  6174. cmds.Add(COFFFrameProgram::Command_EAX);
  6175. else if ((curCmd == "$ebx") || (curCmd == "$20"))
  6176. cmds.Add(COFFFrameProgram::Command_EBX);
  6177. else if ((curCmd == "$ecx") || (curCmd == "$18"))
  6178. cmds.Add(COFFFrameProgram::Command_ECX);
  6179. else if ((curCmd == "$edx") || (curCmd == "$19"))
  6180. cmds.Add(COFFFrameProgram::Command_EDX);
  6181. else if ((curCmd == "$esi") || (curCmd == "$23"))
  6182. cmds.Add(COFFFrameProgram::Command_ESI);
  6183. else if ((curCmd == "$edi") || (curCmd == "$24"))
  6184. cmds.Add(COFFFrameProgram::Command_EDI);
  6185. else if (curCmd == "$T0")
  6186. cmds.Add(COFFFrameProgram::Command_T0);
  6187. else if (curCmd == "$T1")
  6188. cmds.Add(COFFFrameProgram::Command_T1);
  6189. else if (curCmd == "$T2")
  6190. cmds.Add(COFFFrameProgram::Command_T2);
  6191. else if (curCmd == "$T3")
  6192. cmds.Add(COFFFrameProgram::Command_T3);
  6193. else if ((curCmd == ".raSearch") || (curCmd == ".raSearchStart"))
  6194. cmds.Add(COFFFrameProgram::Command_RASearch);
  6195. else if (curCmd == "+")
  6196. cmds.Add(COFFFrameProgram::Command_Add);
  6197. else if (curCmd == "-")
  6198. cmds.Add(COFFFrameProgram::Command_Subtract);
  6199. else if (curCmd == "@")
  6200. cmds.Add(COFFFrameProgram::Command_Align);
  6201. else if (curCmd == "=")
  6202. cmds.Add(COFFFrameProgram::Command_Set);
  6203. else if (curCmd == "^")
  6204. cmds.Add(COFFFrameProgram::Command_Deref);
  6205. else if (!curCmd.IsEmpty())
  6206. {
  6207. if ((curCmd[0] >= '0') && (curCmd[0] <= '9'))
  6208. {
  6209. int val = atoi(curCmd.c_str());
  6210. if ((val >= 0) && (val <= 255))
  6211. {
  6212. cmds.Add(COFFFrameProgram::Command_Value8);
  6213. cmds.Add((COFFFrameProgram::Command)val);
  6214. }
  6215. else
  6216. {
  6217. cmds.Add(COFFFrameProgram::Command_Value);
  6218. cmds.Insert(cmds.size(), (COFFFrameProgram::Command*)&val, 4);
  6219. }
  6220. }
  6221. else
  6222. {
  6223. failed = true;
  6224. SoftFail(StrFormat("Invalid COFF frame program: %s", curCmd.c_str()));
  6225. }
  6226. }
  6227. if (c == 0)
  6228. break;
  6229. curCmd.Clear();
  6230. }
  6231. else
  6232. curCmd.Append(c);
  6233. }
  6234. if (failed)
  6235. cmds.Clear();
  6236. cmds.Add(COFFFrameProgram::Command_None);
  6237. program.mCommands = (COFFFrameProgram::Command*)mAlloc.AllocBytes(cmds.size());
  6238. memcpy(program.mCommands, &cmds[0], cmds.size());
  6239. *programPtr = program;
  6240. }
  6241. entry.mProgram = *programPtr;
  6242. mDebugTarget->mCOFFFrameDescriptorMap.insert(std::make_pair(baseAddr + frame->mRvaStart, entry));
  6243. }
  6244. mParsedFrameDescriptors = true;
  6245. }
  6246. void COFF::ParseFrameDescriptors()
  6247. {
  6248. if (mParsedFrameDescriptors)
  6249. return;
  6250. if (mCvNewFPOStream == 0)
  6251. return;
  6252. int streamSize = 0;
  6253. mNewFPOData = CvReadStream(mCvNewFPOStream, &streamSize);
  6254. ParseFrameDescriptors(mNewFPOData, streamSize, mImageBase);
  6255. }
  6256. NS_BF_DBG_BEGIN
  6257. // void TestCoff(void* tdata, int tdataSize, void* cuData, int cuDataSize)
  6258. // {
  6259. // DebugTarget* debugTarget = new DebugTarget(NULL);
  6260. // {
  6261. // COFF coff(debugTarget);
  6262. // coff.mCvTypeSectionData = (uint8*)tdata;
  6263. // coff.mCvTypeSectionDataSize = tdataSize;
  6264. //
  6265. // coff.mCvCompileUnitData = (uint8*)cuData;
  6266. // coff.mCvCompileUnitDataSize = cuDataSize;
  6267. //
  6268. // coff.ProcessDebugInfo();
  6269. // }
  6270. // delete debugTarget;
  6271. // }
  6272. void TestPDB(const StringImpl& fileName, WinDebugger* debugger)
  6273. {
  6274. DebugTarget* debugTarget = new DebugTarget(NULL);
  6275. COFF coff(debugTarget);
  6276. coff.mDebugger = debugger;
  6277. uint8 wantGuid[16];
  6278. coff.TryLoadPDB(fileName, wantGuid, -1);
  6279. coff.ParseTypeData();
  6280. coff.CvParseIPI();
  6281. coff.ParseGlobalsData();
  6282. coff.ParseSymbolData();
  6283. for (int compileUnitId = 0; compileUnitId < (int)coff.mCvModuleInfo.size(); compileUnitId++)
  6284. coff.ParseCompileUnit(compileUnitId);
  6285. }
  6286. NS_BF_DBG_END