COFF.cpp 187 KB

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