arm-tdep.c 398 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959
  1. /* Common target dependent code for GDB on ARM systems.
  2. Copyright (C) 1988-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #include "defs.h"
  15. #include <ctype.h> /* XXX for isupper (). */
  16. #include "frame.h"
  17. #include "inferior.h"
  18. #include "infrun.h"
  19. #include "gdbcmd.h"
  20. #include "gdbcore.h"
  21. #include "dis-asm.h" /* For register styles. */
  22. #include "disasm.h"
  23. #include "regcache.h"
  24. #include "reggroups.h"
  25. #include "target-float.h"
  26. #include "value.h"
  27. #include "arch-utils.h"
  28. #include "osabi.h"
  29. #include "frame-unwind.h"
  30. #include "frame-base.h"
  31. #include "trad-frame.h"
  32. #include "objfiles.h"
  33. #include "dwarf2.h"
  34. #include "dwarf2/frame.h"
  35. #include "gdbtypes.h"
  36. #include "prologue-value.h"
  37. #include "remote.h"
  38. #include "target-descriptions.h"
  39. #include "user-regs.h"
  40. #include "observable.h"
  41. #include "count-one-bits.h"
  42. #include "arch/arm.h"
  43. #include "arch/arm-get-next-pcs.h"
  44. #include "arm-tdep.h"
  45. #include "gdb/sim-arm.h"
  46. #include "elf-bfd.h"
  47. #include "coff/internal.h"
  48. #include "elf/arm.h"
  49. #include "record.h"
  50. #include "record-full.h"
  51. #include <algorithm>
  52. #include "producer.h"
  53. #if GDB_SELF_TEST
  54. #include "gdbsupport/selftest.h"
  55. #endif
  56. static bool arm_debug;
  57. /* Print an "arm" debug statement. */
  58. #define arm_debug_printf(fmt, ...) \
  59. debug_prefixed_printf_cond (arm_debug, "arm", fmt, ##__VA_ARGS__)
  60. /* Macros for setting and testing a bit in a minimal symbol that marks
  61. it as Thumb function. The MSB of the minimal symbol's "info" field
  62. is used for this purpose.
  63. MSYMBOL_SET_SPECIAL Actually sets the "special" bit.
  64. MSYMBOL_IS_SPECIAL Tests the "special" bit in a minimal symbol. */
  65. #define MSYMBOL_SET_SPECIAL(msym) \
  66. MSYMBOL_TARGET_FLAG_1 (msym) = 1
  67. #define MSYMBOL_IS_SPECIAL(msym) \
  68. MSYMBOL_TARGET_FLAG_1 (msym)
  69. struct arm_mapping_symbol
  70. {
  71. CORE_ADDR value;
  72. char type;
  73. bool operator< (const arm_mapping_symbol &other) const
  74. { return this->value < other.value; }
  75. };
  76. typedef std::vector<arm_mapping_symbol> arm_mapping_symbol_vec;
  77. struct arm_per_bfd
  78. {
  79. explicit arm_per_bfd (size_t num_sections)
  80. : section_maps (new arm_mapping_symbol_vec[num_sections]),
  81. section_maps_sorted (new bool[num_sections] ())
  82. {}
  83. DISABLE_COPY_AND_ASSIGN (arm_per_bfd);
  84. /* Information about mapping symbols ($a, $d, $t) in the objfile.
  85. The format is an array of vectors of arm_mapping_symbols, there is one
  86. vector for each section of the objfile (the array is index by BFD section
  87. index).
  88. For each section, the vector of arm_mapping_symbol is sorted by
  89. symbol value (address). */
  90. std::unique_ptr<arm_mapping_symbol_vec[]> section_maps;
  91. /* For each corresponding element of section_maps above, is this vector
  92. sorted. */
  93. std::unique_ptr<bool[]> section_maps_sorted;
  94. };
  95. /* Per-bfd data used for mapping symbols. */
  96. static bfd_key<arm_per_bfd> arm_bfd_data_key;
  97. /* The list of available "set arm ..." and "show arm ..." commands. */
  98. static struct cmd_list_element *setarmcmdlist = NULL;
  99. static struct cmd_list_element *showarmcmdlist = NULL;
  100. /* The type of floating-point to use. Keep this in sync with enum
  101. arm_float_model, and the help string in _initialize_arm_tdep. */
  102. static const char *const fp_model_strings[] =
  103. {
  104. "auto",
  105. "softfpa",
  106. "fpa",
  107. "softvfp",
  108. "vfp",
  109. NULL
  110. };
  111. /* A variable that can be configured by the user. */
  112. static enum arm_float_model arm_fp_model = ARM_FLOAT_AUTO;
  113. static const char *current_fp_model = "auto";
  114. /* The ABI to use. Keep this in sync with arm_abi_kind. */
  115. static const char *const arm_abi_strings[] =
  116. {
  117. "auto",
  118. "APCS",
  119. "AAPCS",
  120. NULL
  121. };
  122. /* A variable that can be configured by the user. */
  123. static enum arm_abi_kind arm_abi_global = ARM_ABI_AUTO;
  124. static const char *arm_abi_string = "auto";
  125. /* The execution mode to assume. */
  126. static const char *const arm_mode_strings[] =
  127. {
  128. "auto",
  129. "arm",
  130. "thumb",
  131. NULL
  132. };
  133. static const char *arm_fallback_mode_string = "auto";
  134. static const char *arm_force_mode_string = "auto";
  135. /* The standard register names, and all the valid aliases for them. Note
  136. that `fp', `sp' and `pc' are not added in this alias list, because they
  137. have been added as builtin user registers in
  138. std-regs.c:_initialize_frame_reg. */
  139. static const struct
  140. {
  141. const char *name;
  142. int regnum;
  143. } arm_register_aliases[] = {
  144. /* Basic register numbers. */
  145. { "r0", 0 },
  146. { "r1", 1 },
  147. { "r2", 2 },
  148. { "r3", 3 },
  149. { "r4", 4 },
  150. { "r5", 5 },
  151. { "r6", 6 },
  152. { "r7", 7 },
  153. { "r8", 8 },
  154. { "r9", 9 },
  155. { "r10", 10 },
  156. { "r11", 11 },
  157. { "r12", 12 },
  158. { "r13", 13 },
  159. { "r14", 14 },
  160. { "r15", 15 },
  161. /* Synonyms (argument and variable registers). */
  162. { "a1", 0 },
  163. { "a2", 1 },
  164. { "a3", 2 },
  165. { "a4", 3 },
  166. { "v1", 4 },
  167. { "v2", 5 },
  168. { "v3", 6 },
  169. { "v4", 7 },
  170. { "v5", 8 },
  171. { "v6", 9 },
  172. { "v7", 10 },
  173. { "v8", 11 },
  174. /* Other platform-specific names for r9. */
  175. { "sb", 9 },
  176. { "tr", 9 },
  177. /* Special names. */
  178. { "ip", 12 },
  179. { "lr", 14 },
  180. /* Names used by GCC (not listed in the ARM EABI). */
  181. { "sl", 10 },
  182. /* A special name from the older ATPCS. */
  183. { "wr", 7 },
  184. };
  185. static const char *const arm_register_names[] =
  186. {"r0", "r1", "r2", "r3", /* 0 1 2 3 */
  187. "r4", "r5", "r6", "r7", /* 4 5 6 7 */
  188. "r8", "r9", "r10", "r11", /* 8 9 10 11 */
  189. "r12", "sp", "lr", "pc", /* 12 13 14 15 */
  190. "f0", "f1", "f2", "f3", /* 16 17 18 19 */
  191. "f4", "f5", "f6", "f7", /* 20 21 22 23 */
  192. "fps", "cpsr" }; /* 24 25 */
  193. /* Holds the current set of options to be passed to the disassembler. */
  194. static char *arm_disassembler_options;
  195. /* Valid register name styles. */
  196. static const char **valid_disassembly_styles;
  197. /* Disassembly style to use. Default to "std" register names. */
  198. static const char *disassembly_style;
  199. /* All possible arm target descriptors. */
  200. static struct target_desc *tdesc_arm_list[ARM_FP_TYPE_INVALID];
  201. static struct target_desc *tdesc_arm_mprofile_list[ARM_M_TYPE_INVALID];
  202. /* This is used to keep the bfd arch_info in sync with the disassembly
  203. style. */
  204. static void set_disassembly_style_sfunc (const char *, int,
  205. struct cmd_list_element *);
  206. static void show_disassembly_style_sfunc (struct ui_file *, int,
  207. struct cmd_list_element *,
  208. const char *);
  209. static enum register_status arm_neon_quad_read (struct gdbarch *gdbarch,
  210. readable_regcache *regcache,
  211. int regnum, gdb_byte *buf);
  212. static void arm_neon_quad_write (struct gdbarch *gdbarch,
  213. struct regcache *regcache,
  214. int regnum, const gdb_byte *buf);
  215. static CORE_ADDR
  216. arm_get_next_pcs_syscall_next_pc (struct arm_get_next_pcs *self);
  217. /* get_next_pcs operations. */
  218. static struct arm_get_next_pcs_ops arm_get_next_pcs_ops = {
  219. arm_get_next_pcs_read_memory_unsigned_integer,
  220. arm_get_next_pcs_syscall_next_pc,
  221. arm_get_next_pcs_addr_bits_remove,
  222. arm_get_next_pcs_is_thumb,
  223. NULL,
  224. };
  225. struct arm_prologue_cache
  226. {
  227. /* The stack pointer at the time this frame was created; i.e. the
  228. caller's stack pointer when this function was called. It is used
  229. to identify this frame. */
  230. CORE_ADDR prev_sp;
  231. /* The frame base for this frame is just prev_sp - frame size.
  232. FRAMESIZE is the distance from the frame pointer to the
  233. initial stack pointer. */
  234. int framesize;
  235. /* The register used to hold the frame pointer for this frame. */
  236. int framereg;
  237. /* True if the return address is signed, false otherwise. */
  238. gdb::optional<bool> ra_signed_state;
  239. /* Saved register offsets. */
  240. trad_frame_saved_reg *saved_regs;
  241. };
  242. namespace {
  243. /* Abstract class to read ARM instructions from memory. */
  244. class arm_instruction_reader
  245. {
  246. public:
  247. /* Read a 4 bytes instruction from memory using the BYTE_ORDER endianness. */
  248. virtual uint32_t read (CORE_ADDR memaddr, bfd_endian byte_order) const = 0;
  249. };
  250. /* Read instructions from target memory. */
  251. class target_arm_instruction_reader : public arm_instruction_reader
  252. {
  253. public:
  254. uint32_t read (CORE_ADDR memaddr, bfd_endian byte_order) const override
  255. {
  256. return read_code_unsigned_integer (memaddr, 4, byte_order);
  257. }
  258. };
  259. } /* namespace */
  260. static CORE_ADDR arm_analyze_prologue
  261. (struct gdbarch *gdbarch, CORE_ADDR prologue_start, CORE_ADDR prologue_end,
  262. struct arm_prologue_cache *cache, const arm_instruction_reader &insn_reader);
  263. /* Architecture version for displaced stepping. This effects the behaviour of
  264. certain instructions, and really should not be hard-wired. */
  265. #define DISPLACED_STEPPING_ARCH_VERSION 5
  266. /* See arm-tdep.h. */
  267. bool arm_apcs_32 = true;
  268. /* Return the bit mask in ARM_PS_REGNUM that indicates Thumb mode. */
  269. int
  270. arm_psr_thumb_bit (struct gdbarch *gdbarch)
  271. {
  272. arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  273. if (tdep->is_m)
  274. return XPSR_T;
  275. else
  276. return CPSR_T;
  277. }
  278. /* Determine if the processor is currently executing in Thumb mode. */
  279. int
  280. arm_is_thumb (struct regcache *regcache)
  281. {
  282. ULONGEST cpsr;
  283. ULONGEST t_bit = arm_psr_thumb_bit (regcache->arch ());
  284. cpsr = regcache_raw_get_unsigned (regcache, ARM_PS_REGNUM);
  285. return (cpsr & t_bit) != 0;
  286. }
  287. /* Determine if FRAME is executing in Thumb mode. */
  288. int
  289. arm_frame_is_thumb (struct frame_info *frame)
  290. {
  291. CORE_ADDR cpsr;
  292. ULONGEST t_bit = arm_psr_thumb_bit (get_frame_arch (frame));
  293. /* Every ARM frame unwinder can unwind the T bit of the CPSR, either
  294. directly (from a signal frame or dummy frame) or by interpreting
  295. the saved LR (from a prologue or DWARF frame). So consult it and
  296. trust the unwinders. */
  297. cpsr = get_frame_register_unsigned (frame, ARM_PS_REGNUM);
  298. return (cpsr & t_bit) != 0;
  299. }
  300. /* Search for the mapping symbol covering MEMADDR. If one is found,
  301. return its type. Otherwise, return 0. If START is non-NULL,
  302. set *START to the location of the mapping symbol. */
  303. static char
  304. arm_find_mapping_symbol (CORE_ADDR memaddr, CORE_ADDR *start)
  305. {
  306. struct obj_section *sec;
  307. /* If there are mapping symbols, consult them. */
  308. sec = find_pc_section (memaddr);
  309. if (sec != NULL)
  310. {
  311. arm_per_bfd *data = arm_bfd_data_key.get (sec->objfile->obfd);
  312. if (data != NULL)
  313. {
  314. unsigned int section_idx = sec->the_bfd_section->index;
  315. arm_mapping_symbol_vec &map
  316. = data->section_maps[section_idx];
  317. /* Sort the vector on first use. */
  318. if (!data->section_maps_sorted[section_idx])
  319. {
  320. std::sort (map.begin (), map.end ());
  321. data->section_maps_sorted[section_idx] = true;
  322. }
  323. arm_mapping_symbol map_key = { memaddr - sec->addr (), 0 };
  324. arm_mapping_symbol_vec::const_iterator it
  325. = std::lower_bound (map.begin (), map.end (), map_key);
  326. /* std::lower_bound finds the earliest ordered insertion
  327. point. If the symbol at this position starts at this exact
  328. address, we use that; otherwise, the preceding
  329. mapping symbol covers this address. */
  330. if (it < map.end ())
  331. {
  332. if (it->value == map_key.value)
  333. {
  334. if (start)
  335. *start = it->value + sec->addr ();
  336. return it->type;
  337. }
  338. }
  339. if (it > map.begin ())
  340. {
  341. arm_mapping_symbol_vec::const_iterator prev_it
  342. = it - 1;
  343. if (start)
  344. *start = prev_it->value + sec->addr ();
  345. return prev_it->type;
  346. }
  347. }
  348. }
  349. return 0;
  350. }
  351. /* Determine if the program counter specified in MEMADDR is in a Thumb
  352. function. This function should be called for addresses unrelated to
  353. any executing frame; otherwise, prefer arm_frame_is_thumb. */
  354. int
  355. arm_pc_is_thumb (struct gdbarch *gdbarch, CORE_ADDR memaddr)
  356. {
  357. struct bound_minimal_symbol sym;
  358. char type;
  359. arm_displaced_step_copy_insn_closure *dsc = nullptr;
  360. arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  361. if (gdbarch_displaced_step_copy_insn_closure_by_addr_p (gdbarch))
  362. dsc = ((arm_displaced_step_copy_insn_closure * )
  363. gdbarch_displaced_step_copy_insn_closure_by_addr
  364. (gdbarch, current_inferior (), memaddr));
  365. /* If checking the mode of displaced instruction in copy area, the mode
  366. should be determined by instruction on the original address. */
  367. if (dsc)
  368. {
  369. displaced_debug_printf ("check mode of %.8lx instead of %.8lx",
  370. (unsigned long) dsc->insn_addr,
  371. (unsigned long) memaddr);
  372. memaddr = dsc->insn_addr;
  373. }
  374. /* If bit 0 of the address is set, assume this is a Thumb address. */
  375. if (IS_THUMB_ADDR (memaddr))
  376. return 1;
  377. /* If the user wants to override the symbol table, let him. */
  378. if (strcmp (arm_force_mode_string, "arm") == 0)
  379. return 0;
  380. if (strcmp (arm_force_mode_string, "thumb") == 0)
  381. return 1;
  382. /* ARM v6-M and v7-M are always in Thumb mode. */
  383. if (tdep->is_m)
  384. return 1;
  385. /* If there are mapping symbols, consult them. */
  386. type = arm_find_mapping_symbol (memaddr, NULL);
  387. if (type)
  388. return type == 't';
  389. /* Thumb functions have a "special" bit set in minimal symbols. */
  390. sym = lookup_minimal_symbol_by_pc (memaddr);
  391. if (sym.minsym)
  392. return (MSYMBOL_IS_SPECIAL (sym.minsym));
  393. /* If the user wants to override the fallback mode, let them. */
  394. if (strcmp (arm_fallback_mode_string, "arm") == 0)
  395. return 0;
  396. if (strcmp (arm_fallback_mode_string, "thumb") == 0)
  397. return 1;
  398. /* If we couldn't find any symbol, but we're talking to a running
  399. target, then trust the current value of $cpsr. This lets
  400. "display/i $pc" always show the correct mode (though if there is
  401. a symbol table we will not reach here, so it still may not be
  402. displayed in the mode it will be executed). */
  403. if (target_has_registers ())
  404. return arm_frame_is_thumb (get_current_frame ());
  405. /* Otherwise we're out of luck; we assume ARM. */
  406. return 0;
  407. }
  408. /* Determine if the address specified equals any of these magic return
  409. values, called EXC_RETURN, defined by the ARM v6-M, v7-M and v8-M
  410. architectures.
  411. From ARMv6-M Reference Manual B1.5.8
  412. Table B1-5 Exception return behavior
  413. EXC_RETURN Return To Return Stack
  414. 0xFFFFFFF1 Handler mode Main
  415. 0xFFFFFFF9 Thread mode Main
  416. 0xFFFFFFFD Thread mode Process
  417. From ARMv7-M Reference Manual B1.5.8
  418. Table B1-8 EXC_RETURN definition of exception return behavior, no FP
  419. EXC_RETURN Return To Return Stack
  420. 0xFFFFFFF1 Handler mode Main
  421. 0xFFFFFFF9 Thread mode Main
  422. 0xFFFFFFFD Thread mode Process
  423. Table B1-9 EXC_RETURN definition of exception return behavior, with
  424. FP
  425. EXC_RETURN Return To Return Stack Frame Type
  426. 0xFFFFFFE1 Handler mode Main Extended
  427. 0xFFFFFFE9 Thread mode Main Extended
  428. 0xFFFFFFED Thread mode Process Extended
  429. 0xFFFFFFF1 Handler mode Main Basic
  430. 0xFFFFFFF9 Thread mode Main Basic
  431. 0xFFFFFFFD Thread mode Process Basic
  432. For more details see "B1.5.8 Exception return behavior"
  433. in both ARMv6-M and ARMv7-M Architecture Reference Manuals.
  434. In the ARMv8-M Architecture Technical Reference also adds
  435. for implementations without the Security Extension:
  436. EXC_RETURN Condition
  437. 0xFFFFFFB0 Return to Handler mode.
  438. 0xFFFFFFB8 Return to Thread mode using the main stack.
  439. 0xFFFFFFBC Return to Thread mode using the process stack. */
  440. static int
  441. arm_m_addr_is_magic (CORE_ADDR addr)
  442. {
  443. switch (addr)
  444. {
  445. /* Values from ARMv8-M Architecture Technical Reference. */
  446. case 0xffffffb0:
  447. case 0xffffffb8:
  448. case 0xffffffbc:
  449. /* Values from Tables in B1.5.8 the EXC_RETURN definitions of
  450. the exception return behavior. */
  451. case 0xffffffe1:
  452. case 0xffffffe9:
  453. case 0xffffffed:
  454. case 0xfffffff1:
  455. case 0xfffffff9:
  456. case 0xfffffffd:
  457. /* Address is magic. */
  458. return 1;
  459. default:
  460. /* Address is not magic. */
  461. return 0;
  462. }
  463. }
  464. /* Remove useless bits from addresses in a running program. */
  465. static CORE_ADDR
  466. arm_addr_bits_remove (struct gdbarch *gdbarch, CORE_ADDR val)
  467. {
  468. arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  469. /* On M-profile devices, do not strip the low bit from EXC_RETURN
  470. (the magic exception return address). */
  471. if (tdep->is_m && arm_m_addr_is_magic (val))
  472. return val;
  473. if (arm_apcs_32)
  474. return UNMAKE_THUMB_ADDR (val);
  475. else
  476. return (val & 0x03fffffc);
  477. }
  478. /* Return 1 if PC is the start of a compiler helper function which
  479. can be safely ignored during prologue skipping. IS_THUMB is true
  480. if the function is known to be a Thumb function due to the way it
  481. is being called. */
  482. static int
  483. skip_prologue_function (struct gdbarch *gdbarch, CORE_ADDR pc, int is_thumb)
  484. {
  485. enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
  486. struct bound_minimal_symbol msym;
  487. msym = lookup_minimal_symbol_by_pc (pc);
  488. if (msym.minsym != NULL
  489. && BMSYMBOL_VALUE_ADDRESS (msym) == pc
  490. && msym.minsym->linkage_name () != NULL)
  491. {
  492. const char *name = msym.minsym->linkage_name ();
  493. /* The GNU linker's Thumb call stub to foo is named
  494. __foo_from_thumb. */
  495. if (strstr (name, "_from_thumb") != NULL)
  496. name += 2;
  497. /* On soft-float targets, __truncdfsf2 is called to convert promoted
  498. arguments to their argument types in non-prototyped
  499. functions. */
  500. if (startswith (name, "__truncdfsf2"))
  501. return 1;
  502. if (startswith (name, "__aeabi_d2f"))
  503. return 1;
  504. /* Internal functions related to thread-local storage. */
  505. if (startswith (name, "__tls_get_addr"))
  506. return 1;
  507. if (startswith (name, "__aeabi_read_tp"))
  508. return 1;
  509. }
  510. else
  511. {
  512. /* If we run against a stripped glibc, we may be unable to identify
  513. special functions by name. Check for one important case,
  514. __aeabi_read_tp, by comparing the *code* against the default
  515. implementation (this is hand-written ARM assembler in glibc). */
  516. if (!is_thumb
  517. && read_code_unsigned_integer (pc, 4, byte_order_for_code)
  518. == 0xe3e00a0f /* mov r0, #0xffff0fff */
  519. && read_code_unsigned_integer (pc + 4, 4, byte_order_for_code)
  520. == 0xe240f01f) /* sub pc, r0, #31 */
  521. return 1;
  522. }
  523. return 0;
  524. }
  525. /* Extract the immediate from instruction movw/movt of encoding T. INSN1 is
  526. the first 16-bit of instruction, and INSN2 is the second 16-bit of
  527. instruction. */
  528. #define EXTRACT_MOVW_MOVT_IMM_T(insn1, insn2) \
  529. ((bits ((insn1), 0, 3) << 12) \
  530. | (bits ((insn1), 10, 10) << 11) \
  531. | (bits ((insn2), 12, 14) << 8) \
  532. | bits ((insn2), 0, 7))
  533. /* Extract the immediate from instruction movw/movt of encoding A. INSN is
  534. the 32-bit instruction. */
  535. #define EXTRACT_MOVW_MOVT_IMM_A(insn) \
  536. ((bits ((insn), 16, 19) << 12) \
  537. | bits ((insn), 0, 11))
  538. /* Decode immediate value; implements ThumbExpandImmediate pseudo-op. */
  539. static unsigned int
  540. thumb_expand_immediate (unsigned int imm)
  541. {
  542. unsigned int count = imm >> 7;
  543. if (count < 8)
  544. switch (count / 2)
  545. {
  546. case 0:
  547. return imm & 0xff;
  548. case 1:
  549. return (imm & 0xff) | ((imm & 0xff) << 16);
  550. case 2:
  551. return ((imm & 0xff) << 8) | ((imm & 0xff) << 24);
  552. case 3:
  553. return (imm & 0xff) | ((imm & 0xff) << 8)
  554. | ((imm & 0xff) << 16) | ((imm & 0xff) << 24);
  555. }
  556. return (0x80 | (imm & 0x7f)) << (32 - count);
  557. }
  558. /* Return 1 if the 16-bit Thumb instruction INSN restores SP in
  559. epilogue, 0 otherwise. */
  560. static int
  561. thumb_instruction_restores_sp (unsigned short insn)
  562. {
  563. return (insn == 0x46bd /* mov sp, r7 */
  564. || (insn & 0xff80) == 0xb000 /* add sp, imm */
  565. || (insn & 0xfe00) == 0xbc00); /* pop <registers> */
  566. }
  567. /* Analyze a Thumb prologue, looking for a recognizable stack frame
  568. and frame pointer. Scan until we encounter a store that could
  569. clobber the stack frame unexpectedly, or an unknown instruction.
  570. Return the last address which is definitely safe to skip for an
  571. initial breakpoint. */
  572. static CORE_ADDR
  573. thumb_analyze_prologue (struct gdbarch *gdbarch,
  574. CORE_ADDR start, CORE_ADDR limit,
  575. struct arm_prologue_cache *cache)
  576. {
  577. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  578. enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
  579. int i;
  580. pv_t regs[16];
  581. CORE_ADDR offset;
  582. CORE_ADDR unrecognized_pc = 0;
  583. for (i = 0; i < 16; i++)
  584. regs[i] = pv_register (i, 0);
  585. pv_area stack (ARM_SP_REGNUM, gdbarch_addr_bit (gdbarch));
  586. while (start < limit)
  587. {
  588. unsigned short insn;
  589. gdb::optional<bool> ra_signed_state;
  590. insn = read_code_unsigned_integer (start, 2, byte_order_for_code);
  591. if ((insn & 0xfe00) == 0xb400) /* push { rlist } */
  592. {
  593. int regno;
  594. int mask;
  595. if (stack.store_would_trash (regs[ARM_SP_REGNUM]))
  596. break;
  597. /* Bits 0-7 contain a mask for registers R0-R7. Bit 8 says
  598. whether to save LR (R14). */
  599. mask = (insn & 0xff) | ((insn & 0x100) << 6);
  600. /* Calculate offsets of saved R0-R7 and LR. */
  601. for (regno = ARM_LR_REGNUM; regno >= 0; regno--)
  602. if (mask & (1 << regno))
  603. {
  604. regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM],
  605. -4);
  606. stack.store (regs[ARM_SP_REGNUM], 4, regs[regno]);
  607. }
  608. }
  609. else if ((insn & 0xff80) == 0xb080) /* sub sp, #imm */
  610. {
  611. offset = (insn & 0x7f) << 2; /* get scaled offset */
  612. regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM],
  613. -offset);
  614. }
  615. else if (thumb_instruction_restores_sp (insn))
  616. {
  617. /* Don't scan past the epilogue. */
  618. break;
  619. }
  620. else if ((insn & 0xf800) == 0xa800) /* add Rd, sp, #imm */
  621. regs[bits (insn, 8, 10)] = pv_add_constant (regs[ARM_SP_REGNUM],
  622. (insn & 0xff) << 2);
  623. else if ((insn & 0xfe00) == 0x1c00 /* add Rd, Rn, #imm */
  624. && pv_is_register (regs[bits (insn, 3, 5)], ARM_SP_REGNUM))
  625. regs[bits (insn, 0, 2)] = pv_add_constant (regs[bits (insn, 3, 5)],
  626. bits (insn, 6, 8));
  627. else if ((insn & 0xf800) == 0x3000 /* add Rd, #imm */
  628. && pv_is_register (regs[bits (insn, 8, 10)], ARM_SP_REGNUM))
  629. regs[bits (insn, 8, 10)] = pv_add_constant (regs[bits (insn, 8, 10)],
  630. bits (insn, 0, 7));
  631. else if ((insn & 0xfe00) == 0x1800 /* add Rd, Rn, Rm */
  632. && pv_is_register (regs[bits (insn, 6, 8)], ARM_SP_REGNUM)
  633. && pv_is_constant (regs[bits (insn, 3, 5)]))
  634. regs[bits (insn, 0, 2)] = pv_add (regs[bits (insn, 3, 5)],
  635. regs[bits (insn, 6, 8)]);
  636. else if ((insn & 0xff00) == 0x4400 /* add Rd, Rm */
  637. && pv_is_constant (regs[bits (insn, 3, 6)]))
  638. {
  639. int rd = (bit (insn, 7) << 3) + bits (insn, 0, 2);
  640. int rm = bits (insn, 3, 6);
  641. regs[rd] = pv_add (regs[rd], regs[rm]);
  642. }
  643. else if ((insn & 0xff00) == 0x4600) /* mov hi, lo or mov lo, hi */
  644. {
  645. int dst_reg = (insn & 0x7) + ((insn & 0x80) >> 4);
  646. int src_reg = (insn & 0x78) >> 3;
  647. regs[dst_reg] = regs[src_reg];
  648. }
  649. else if ((insn & 0xf800) == 0x9000) /* str rd, [sp, #off] */
  650. {
  651. /* Handle stores to the stack. Normally pushes are used,
  652. but with GCC -mtpcs-frame, there may be other stores
  653. in the prologue to create the frame. */
  654. int regno = (insn >> 8) & 0x7;
  655. pv_t addr;
  656. offset = (insn & 0xff) << 2;
  657. addr = pv_add_constant (regs[ARM_SP_REGNUM], offset);
  658. if (stack.store_would_trash (addr))
  659. break;
  660. stack.store (addr, 4, regs[regno]);
  661. }
  662. else if ((insn & 0xf800) == 0x6000) /* str rd, [rn, #off] */
  663. {
  664. int rd = bits (insn, 0, 2);
  665. int rn = bits (insn, 3, 5);
  666. pv_t addr;
  667. offset = bits (insn, 6, 10) << 2;
  668. addr = pv_add_constant (regs[rn], offset);
  669. if (stack.store_would_trash (addr))
  670. break;
  671. stack.store (addr, 4, regs[rd]);
  672. }
  673. else if (((insn & 0xf800) == 0x7000 /* strb Rd, [Rn, #off] */
  674. || (insn & 0xf800) == 0x8000) /* strh Rd, [Rn, #off] */
  675. && pv_is_register (regs[bits (insn, 3, 5)], ARM_SP_REGNUM))
  676. /* Ignore stores of argument registers to the stack. */
  677. ;
  678. else if ((insn & 0xf800) == 0xc800 /* ldmia Rn!, { registers } */
  679. && pv_is_register (regs[bits (insn, 8, 10)], ARM_SP_REGNUM))
  680. /* Ignore block loads from the stack, potentially copying
  681. parameters from memory. */
  682. ;
  683. else if ((insn & 0xf800) == 0x9800 /* ldr Rd, [Rn, #immed] */
  684. || ((insn & 0xf800) == 0x6800 /* ldr Rd, [sp, #immed] */
  685. && pv_is_register (regs[bits (insn, 3, 5)], ARM_SP_REGNUM)))
  686. /* Similarly ignore single loads from the stack. */
  687. ;
  688. else if ((insn & 0xffc0) == 0x0000 /* lsls Rd, Rm, #0 */
  689. || (insn & 0xffc0) == 0x1c00) /* add Rd, Rn, #0 */
  690. /* Skip register copies, i.e. saves to another register
  691. instead of the stack. */
  692. ;
  693. else if ((insn & 0xf800) == 0x2000) /* movs Rd, #imm */
  694. /* Recognize constant loads; even with small stacks these are necessary
  695. on Thumb. */
  696. regs[bits (insn, 8, 10)] = pv_constant (bits (insn, 0, 7));
  697. else if ((insn & 0xf800) == 0x4800) /* ldr Rd, [pc, #imm] */
  698. {
  699. /* Constant pool loads, for the same reason. */
  700. unsigned int constant;
  701. CORE_ADDR loc;
  702. loc = start + 4 + bits (insn, 0, 7) * 4;
  703. constant = read_memory_unsigned_integer (loc, 4, byte_order);
  704. regs[bits (insn, 8, 10)] = pv_constant (constant);
  705. }
  706. else if (thumb_insn_size (insn) == 4) /* 32-bit Thumb-2 instructions. */
  707. {
  708. unsigned short inst2;
  709. inst2 = read_code_unsigned_integer (start + 2, 2,
  710. byte_order_for_code);
  711. uint32_t whole_insn = (insn << 16) | inst2;
  712. if ((insn & 0xf800) == 0xf000 && (inst2 & 0xe800) == 0xe800)
  713. {
  714. /* BL, BLX. Allow some special function calls when
  715. skipping the prologue; GCC generates these before
  716. storing arguments to the stack. */
  717. CORE_ADDR nextpc;
  718. int j1, j2, imm1, imm2;
  719. imm1 = sbits (insn, 0, 10);
  720. imm2 = bits (inst2, 0, 10);
  721. j1 = bit (inst2, 13);
  722. j2 = bit (inst2, 11);
  723. offset = ((imm1 << 12) + (imm2 << 1));
  724. offset ^= ((!j2) << 22) | ((!j1) << 23);
  725. nextpc = start + 4 + offset;
  726. /* For BLX make sure to clear the low bits. */
  727. if (bit (inst2, 12) == 0)
  728. nextpc = nextpc & 0xfffffffc;
  729. if (!skip_prologue_function (gdbarch, nextpc,
  730. bit (inst2, 12) != 0))
  731. break;
  732. }
  733. else if ((insn & 0xffd0) == 0xe900 /* stmdb Rn{!},
  734. { registers } */
  735. && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
  736. {
  737. pv_t addr = regs[bits (insn, 0, 3)];
  738. int regno;
  739. if (stack.store_would_trash (addr))
  740. break;
  741. /* Calculate offsets of saved registers. */
  742. for (regno = ARM_LR_REGNUM; regno >= 0; regno--)
  743. if (inst2 & (1 << regno))
  744. {
  745. addr = pv_add_constant (addr, -4);
  746. stack.store (addr, 4, regs[regno]);
  747. }
  748. if (insn & 0x0020)
  749. regs[bits (insn, 0, 3)] = addr;
  750. }
  751. else if ((insn & 0xff50) == 0xe940 /* strd Rt, Rt2,
  752. [Rn, #+/-imm]{!} */
  753. && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
  754. {
  755. int regno1 = bits (inst2, 12, 15);
  756. int regno2 = bits (inst2, 8, 11);
  757. pv_t addr = regs[bits (insn, 0, 3)];
  758. offset = inst2 & 0xff;
  759. if (insn & 0x0080)
  760. addr = pv_add_constant (addr, offset);
  761. else
  762. addr = pv_add_constant (addr, -offset);
  763. if (stack.store_would_trash (addr))
  764. break;
  765. stack.store (addr, 4, regs[regno1]);
  766. stack.store (pv_add_constant (addr, 4),
  767. 4, regs[regno2]);
  768. if (insn & 0x0020)
  769. regs[bits (insn, 0, 3)] = addr;
  770. }
  771. else if ((insn & 0xfff0) == 0xf8c0 /* str Rt,[Rn,+/-#imm]{!} */
  772. && (inst2 & 0x0c00) == 0x0c00
  773. && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
  774. {
  775. int regno = bits (inst2, 12, 15);
  776. pv_t addr = regs[bits (insn, 0, 3)];
  777. offset = inst2 & 0xff;
  778. if (inst2 & 0x0200)
  779. addr = pv_add_constant (addr, offset);
  780. else
  781. addr = pv_add_constant (addr, -offset);
  782. if (stack.store_would_trash (addr))
  783. break;
  784. stack.store (addr, 4, regs[regno]);
  785. if (inst2 & 0x0100)
  786. regs[bits (insn, 0, 3)] = addr;
  787. }
  788. else if ((insn & 0xfff0) == 0xf8c0 /* str.w Rt,[Rn,#imm] */
  789. && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
  790. {
  791. int regno = bits (inst2, 12, 15);
  792. pv_t addr;
  793. offset = inst2 & 0xfff;
  794. addr = pv_add_constant (regs[bits (insn, 0, 3)], offset);
  795. if (stack.store_would_trash (addr))
  796. break;
  797. stack.store (addr, 4, regs[regno]);
  798. }
  799. else if ((insn & 0xffd0) == 0xf880 /* str{bh}.w Rt,[Rn,#imm] */
  800. && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
  801. /* Ignore stores of argument registers to the stack. */
  802. ;
  803. else if ((insn & 0xffd0) == 0xf800 /* str{bh} Rt,[Rn,#+/-imm] */
  804. && (inst2 & 0x0d00) == 0x0c00
  805. && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
  806. /* Ignore stores of argument registers to the stack. */
  807. ;
  808. else if ((insn & 0xffd0) == 0xe890 /* ldmia Rn[!],
  809. { registers } */
  810. && (inst2 & 0x8000) == 0x0000
  811. && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
  812. /* Ignore block loads from the stack, potentially copying
  813. parameters from memory. */
  814. ;
  815. else if ((insn & 0xff70) == 0xe950 /* ldrd Rt, Rt2,
  816. [Rn, #+/-imm] */
  817. && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
  818. /* Similarly ignore dual loads from the stack. */
  819. ;
  820. else if ((insn & 0xfff0) == 0xf850 /* ldr Rt,[Rn,#+/-imm] */
  821. && (inst2 & 0x0d00) == 0x0c00
  822. && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
  823. /* Similarly ignore single loads from the stack. */
  824. ;
  825. else if ((insn & 0xfff0) == 0xf8d0 /* ldr.w Rt,[Rn,#imm] */
  826. && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
  827. /* Similarly ignore single loads from the stack. */
  828. ;
  829. else if ((insn & 0xfbf0) == 0xf100 /* add.w Rd, Rn, #imm */
  830. && (inst2 & 0x8000) == 0x0000)
  831. {
  832. unsigned int imm = ((bits (insn, 10, 10) << 11)
  833. | (bits (inst2, 12, 14) << 8)
  834. | bits (inst2, 0, 7));
  835. regs[bits (inst2, 8, 11)]
  836. = pv_add_constant (regs[bits (insn, 0, 3)],
  837. thumb_expand_immediate (imm));
  838. }
  839. else if ((insn & 0xfbf0) == 0xf200 /* addw Rd, Rn, #imm */
  840. && (inst2 & 0x8000) == 0x0000)
  841. {
  842. unsigned int imm = ((bits (insn, 10, 10) << 11)
  843. | (bits (inst2, 12, 14) << 8)
  844. | bits (inst2, 0, 7));
  845. regs[bits (inst2, 8, 11)]
  846. = pv_add_constant (regs[bits (insn, 0, 3)], imm);
  847. }
  848. else if ((insn & 0xfbf0) == 0xf1a0 /* sub.w Rd, Rn, #imm */
  849. && (inst2 & 0x8000) == 0x0000)
  850. {
  851. unsigned int imm = ((bits (insn, 10, 10) << 11)
  852. | (bits (inst2, 12, 14) << 8)
  853. | bits (inst2, 0, 7));
  854. regs[bits (inst2, 8, 11)]
  855. = pv_add_constant (regs[bits (insn, 0, 3)],
  856. - (CORE_ADDR) thumb_expand_immediate (imm));
  857. }
  858. else if ((insn & 0xfbf0) == 0xf2a0 /* subw Rd, Rn, #imm */
  859. && (inst2 & 0x8000) == 0x0000)
  860. {
  861. unsigned int imm = ((bits (insn, 10, 10) << 11)
  862. | (bits (inst2, 12, 14) << 8)
  863. | bits (inst2, 0, 7));
  864. regs[bits (inst2, 8, 11)]
  865. = pv_add_constant (regs[bits (insn, 0, 3)], - (CORE_ADDR) imm);
  866. }
  867. else if ((insn & 0xfbff) == 0xf04f) /* mov.w Rd, #const */
  868. {
  869. unsigned int imm = ((bits (insn, 10, 10) << 11)
  870. | (bits (inst2, 12, 14) << 8)
  871. | bits (inst2, 0, 7));
  872. regs[bits (inst2, 8, 11)]
  873. = pv_constant (thumb_expand_immediate (imm));
  874. }
  875. else if ((insn & 0xfbf0) == 0xf240) /* movw Rd, #const */
  876. {
  877. unsigned int imm
  878. = EXTRACT_MOVW_MOVT_IMM_T (insn, inst2);
  879. regs[bits (inst2, 8, 11)] = pv_constant (imm);
  880. }
  881. else if (insn == 0xea5f /* mov.w Rd,Rm */
  882. && (inst2 & 0xf0f0) == 0)
  883. {
  884. int dst_reg = (inst2 & 0x0f00) >> 8;
  885. int src_reg = inst2 & 0xf;
  886. regs[dst_reg] = regs[src_reg];
  887. }
  888. else if ((insn & 0xff7f) == 0xf85f) /* ldr.w Rt,<label> */
  889. {
  890. /* Constant pool loads. */
  891. unsigned int constant;
  892. CORE_ADDR loc;
  893. offset = bits (inst2, 0, 11);
  894. if (insn & 0x0080)
  895. loc = start + 4 + offset;
  896. else
  897. loc = start + 4 - offset;
  898. constant = read_memory_unsigned_integer (loc, 4, byte_order);
  899. regs[bits (inst2, 12, 15)] = pv_constant (constant);
  900. }
  901. else if ((insn & 0xff7f) == 0xe95f) /* ldrd Rt,Rt2,<label> */
  902. {
  903. /* Constant pool loads. */
  904. unsigned int constant;
  905. CORE_ADDR loc;
  906. offset = bits (inst2, 0, 7) << 2;
  907. if (insn & 0x0080)
  908. loc = start + 4 + offset;
  909. else
  910. loc = start + 4 - offset;
  911. constant = read_memory_unsigned_integer (loc, 4, byte_order);
  912. regs[bits (inst2, 12, 15)] = pv_constant (constant);
  913. constant = read_memory_unsigned_integer (loc + 4, 4, byte_order);
  914. regs[bits (inst2, 8, 11)] = pv_constant (constant);
  915. }
  916. /* Start of ARMv8.1-m PACBTI extension instructions. */
  917. else if (IS_PAC (whole_insn))
  918. {
  919. /* LR and SP are input registers. PAC is in R12. LR is
  920. signed from this point onwards. NOP space. */
  921. ra_signed_state = true;
  922. }
  923. else if (IS_PACBTI (whole_insn))
  924. {
  925. /* LR and SP are input registers. PAC is in R12 and PC is a
  926. valid BTI landing pad. LR is signed from this point onwards.
  927. NOP space. */
  928. ra_signed_state = true;
  929. }
  930. else if (IS_BTI (whole_insn))
  931. {
  932. /* Valid BTI landing pad. NOP space. */
  933. }
  934. else if (IS_PACG (whole_insn))
  935. {
  936. /* Sign Rn using Rm and store the PAC in Rd. Rd is signed from
  937. this point onwards. */
  938. ra_signed_state = true;
  939. }
  940. else if (IS_AUT (whole_insn) || IS_AUTG (whole_insn))
  941. {
  942. /* These instructions appear close to the epilogue, when signed
  943. pointers are getting authenticated. */
  944. ra_signed_state = false;
  945. }
  946. /* End of ARMv8.1-m PACBTI extension instructions */
  947. else if (thumb2_instruction_changes_pc (insn, inst2))
  948. {
  949. /* Don't scan past anything that might change control flow. */
  950. break;
  951. }
  952. else
  953. {
  954. /* The optimizer might shove anything into the prologue,
  955. so we just skip what we don't recognize. */
  956. unrecognized_pc = start;
  957. }
  958. arm_gdbarch_tdep *tdep
  959. = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  960. /* Make sure we are dealing with a target that supports ARMv8.1-m
  961. PACBTI. */
  962. if (cache != nullptr && tdep->have_pacbti
  963. && ra_signed_state.has_value ())
  964. {
  965. arm_debug_printf ("Found pacbti instruction at %s",
  966. paddress (gdbarch, start));
  967. arm_debug_printf ("RA is %s",
  968. *ra_signed_state? "signed" : "not signed");
  969. cache->ra_signed_state = ra_signed_state;
  970. }
  971. start += 2;
  972. }
  973. else if (thumb_instruction_changes_pc (insn))
  974. {
  975. /* Don't scan past anything that might change control flow. */
  976. break;
  977. }
  978. else
  979. {
  980. /* The optimizer might shove anything into the prologue,
  981. so we just skip what we don't recognize. */
  982. unrecognized_pc = start;
  983. }
  984. start += 2;
  985. }
  986. arm_debug_printf ("Prologue scan stopped at %s",
  987. paddress (gdbarch, start));
  988. if (unrecognized_pc == 0)
  989. unrecognized_pc = start;
  990. if (cache == NULL)
  991. return unrecognized_pc;
  992. if (pv_is_register (regs[ARM_FP_REGNUM], ARM_SP_REGNUM))
  993. {
  994. /* Frame pointer is fp. Frame size is constant. */
  995. cache->framereg = ARM_FP_REGNUM;
  996. cache->framesize = -regs[ARM_FP_REGNUM].k;
  997. }
  998. else if (pv_is_register (regs[THUMB_FP_REGNUM], ARM_SP_REGNUM))
  999. {
  1000. /* Frame pointer is r7. Frame size is constant. */
  1001. cache->framereg = THUMB_FP_REGNUM;
  1002. cache->framesize = -regs[THUMB_FP_REGNUM].k;
  1003. }
  1004. else
  1005. {
  1006. /* Try the stack pointer... this is a bit desperate. */
  1007. cache->framereg = ARM_SP_REGNUM;
  1008. cache->framesize = -regs[ARM_SP_REGNUM].k;
  1009. }
  1010. for (i = 0; i < 16; i++)
  1011. if (stack.find_reg (gdbarch, i, &offset))
  1012. cache->saved_regs[i].set_addr (offset);
  1013. return unrecognized_pc;
  1014. }
  1015. /* Try to analyze the instructions starting from PC, which load symbol
  1016. __stack_chk_guard. Return the address of instruction after loading this
  1017. symbol, set the dest register number to *BASEREG, and set the size of
  1018. instructions for loading symbol in OFFSET. Return 0 if instructions are
  1019. not recognized. */
  1020. static CORE_ADDR
  1021. arm_analyze_load_stack_chk_guard(CORE_ADDR pc, struct gdbarch *gdbarch,
  1022. unsigned int *destreg, int *offset)
  1023. {
  1024. enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
  1025. int is_thumb = arm_pc_is_thumb (gdbarch, pc);
  1026. unsigned int low, high, address;
  1027. address = 0;
  1028. if (is_thumb)
  1029. {
  1030. unsigned short insn1
  1031. = read_code_unsigned_integer (pc, 2, byte_order_for_code);
  1032. if ((insn1 & 0xf800) == 0x4800) /* ldr Rd, #immed */
  1033. {
  1034. *destreg = bits (insn1, 8, 10);
  1035. *offset = 2;
  1036. address = (pc & 0xfffffffc) + 4 + (bits (insn1, 0, 7) << 2);
  1037. address = read_memory_unsigned_integer (address, 4,
  1038. byte_order_for_code);
  1039. }
  1040. else if ((insn1 & 0xfbf0) == 0xf240) /* movw Rd, #const */
  1041. {
  1042. unsigned short insn2
  1043. = read_code_unsigned_integer (pc + 2, 2, byte_order_for_code);
  1044. low = EXTRACT_MOVW_MOVT_IMM_T (insn1, insn2);
  1045. insn1
  1046. = read_code_unsigned_integer (pc + 4, 2, byte_order_for_code);
  1047. insn2
  1048. = read_code_unsigned_integer (pc + 6, 2, byte_order_for_code);
  1049. /* movt Rd, #const */
  1050. if ((insn1 & 0xfbc0) == 0xf2c0)
  1051. {
  1052. high = EXTRACT_MOVW_MOVT_IMM_T (insn1, insn2);
  1053. *destreg = bits (insn2, 8, 11);
  1054. *offset = 8;
  1055. address = (high << 16 | low);
  1056. }
  1057. }
  1058. }
  1059. else
  1060. {
  1061. unsigned int insn
  1062. = read_code_unsigned_integer (pc, 4, byte_order_for_code);
  1063. if ((insn & 0x0e5f0000) == 0x041f0000) /* ldr Rd, [PC, #immed] */
  1064. {
  1065. address = bits (insn, 0, 11) + pc + 8;
  1066. address = read_memory_unsigned_integer (address, 4,
  1067. byte_order_for_code);
  1068. *destreg = bits (insn, 12, 15);
  1069. *offset = 4;
  1070. }
  1071. else if ((insn & 0x0ff00000) == 0x03000000) /* movw Rd, #const */
  1072. {
  1073. low = EXTRACT_MOVW_MOVT_IMM_A (insn);
  1074. insn
  1075. = read_code_unsigned_integer (pc + 4, 4, byte_order_for_code);
  1076. if ((insn & 0x0ff00000) == 0x03400000) /* movt Rd, #const */
  1077. {
  1078. high = EXTRACT_MOVW_MOVT_IMM_A (insn);
  1079. *destreg = bits (insn, 12, 15);
  1080. *offset = 8;
  1081. address = (high << 16 | low);
  1082. }
  1083. }
  1084. }
  1085. return address;
  1086. }
  1087. /* Try to skip a sequence of instructions used for stack protector. If PC
  1088. points to the first instruction of this sequence, return the address of
  1089. first instruction after this sequence, otherwise, return original PC.
  1090. On arm, this sequence of instructions is composed of mainly three steps,
  1091. Step 1: load symbol __stack_chk_guard,
  1092. Step 2: load from address of __stack_chk_guard,
  1093. Step 3: store it to somewhere else.
  1094. Usually, instructions on step 2 and step 3 are the same on various ARM
  1095. architectures. On step 2, it is one instruction 'ldr Rx, [Rn, #0]', and
  1096. on step 3, it is also one instruction 'str Rx, [r7, #immd]'. However,
  1097. instructions in step 1 vary from different ARM architectures. On ARMv7,
  1098. they are,
  1099. movw Rn, #:lower16:__stack_chk_guard
  1100. movt Rn, #:upper16:__stack_chk_guard
  1101. On ARMv5t, it is,
  1102. ldr Rn, .Label
  1103. ....
  1104. .Lable:
  1105. .word __stack_chk_guard
  1106. Since ldr/str is a very popular instruction, we can't use them as
  1107. 'fingerprint' or 'signature' of stack protector sequence. Here we choose
  1108. sequence {movw/movt, ldr}/ldr/str plus symbol __stack_chk_guard, if not
  1109. stripped, as the 'fingerprint' of a stack protector cdoe sequence. */
  1110. static CORE_ADDR
  1111. arm_skip_stack_protector(CORE_ADDR pc, struct gdbarch *gdbarch)
  1112. {
  1113. enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
  1114. unsigned int basereg;
  1115. struct bound_minimal_symbol stack_chk_guard;
  1116. int offset;
  1117. int is_thumb = arm_pc_is_thumb (gdbarch, pc);
  1118. CORE_ADDR addr;
  1119. /* Try to parse the instructions in Step 1. */
  1120. addr = arm_analyze_load_stack_chk_guard (pc, gdbarch,
  1121. &basereg, &offset);
  1122. if (!addr)
  1123. return pc;
  1124. stack_chk_guard = lookup_minimal_symbol_by_pc (addr);
  1125. /* ADDR must correspond to a symbol whose name is __stack_chk_guard.
  1126. Otherwise, this sequence cannot be for stack protector. */
  1127. if (stack_chk_guard.minsym == NULL
  1128. || !startswith (stack_chk_guard.minsym->linkage_name (), "__stack_chk_guard"))
  1129. return pc;
  1130. if (is_thumb)
  1131. {
  1132. unsigned int destreg;
  1133. unsigned short insn
  1134. = read_code_unsigned_integer (pc + offset, 2, byte_order_for_code);
  1135. /* Step 2: ldr Rd, [Rn, #immed], encoding T1. */
  1136. if ((insn & 0xf800) != 0x6800)
  1137. return pc;
  1138. if (bits (insn, 3, 5) != basereg)
  1139. return pc;
  1140. destreg = bits (insn, 0, 2);
  1141. insn = read_code_unsigned_integer (pc + offset + 2, 2,
  1142. byte_order_for_code);
  1143. /* Step 3: str Rd, [Rn, #immed], encoding T1. */
  1144. if ((insn & 0xf800) != 0x6000)
  1145. return pc;
  1146. if (destreg != bits (insn, 0, 2))
  1147. return pc;
  1148. }
  1149. else
  1150. {
  1151. unsigned int destreg;
  1152. unsigned int insn
  1153. = read_code_unsigned_integer (pc + offset, 4, byte_order_for_code);
  1154. /* Step 2: ldr Rd, [Rn, #immed], encoding A1. */
  1155. if ((insn & 0x0e500000) != 0x04100000)
  1156. return pc;
  1157. if (bits (insn, 16, 19) != basereg)
  1158. return pc;
  1159. destreg = bits (insn, 12, 15);
  1160. /* Step 3: str Rd, [Rn, #immed], encoding A1. */
  1161. insn = read_code_unsigned_integer (pc + offset + 4,
  1162. 4, byte_order_for_code);
  1163. if ((insn & 0x0e500000) != 0x04000000)
  1164. return pc;
  1165. if (bits (insn, 12, 15) != destreg)
  1166. return pc;
  1167. }
  1168. /* The size of total two instructions ldr/str is 4 on Thumb-2, while 8
  1169. on arm. */
  1170. if (is_thumb)
  1171. return pc + offset + 4;
  1172. else
  1173. return pc + offset + 8;
  1174. }
  1175. /* Advance the PC across any function entry prologue instructions to
  1176. reach some "real" code.
  1177. The APCS (ARM Procedure Call Standard) defines the following
  1178. prologue:
  1179. mov ip, sp
  1180. [stmfd sp!, {a1,a2,a3,a4}]
  1181. stmfd sp!, {...,fp,ip,lr,pc}
  1182. [stfe f7, [sp, #-12]!]
  1183. [stfe f6, [sp, #-12]!]
  1184. [stfe f5, [sp, #-12]!]
  1185. [stfe f4, [sp, #-12]!]
  1186. sub fp, ip, #nn @@ nn == 20 or 4 depending on second insn. */
  1187. static CORE_ADDR
  1188. arm_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
  1189. {
  1190. CORE_ADDR func_addr, limit_pc;
  1191. /* See if we can determine the end of the prologue via the symbol table.
  1192. If so, then return either PC, or the PC after the prologue, whichever
  1193. is greater. */
  1194. if (find_pc_partial_function (pc, NULL, &func_addr, NULL))
  1195. {
  1196. CORE_ADDR post_prologue_pc
  1197. = skip_prologue_using_sal (gdbarch, func_addr);
  1198. struct compunit_symtab *cust = find_pc_compunit_symtab (func_addr);
  1199. if (post_prologue_pc)
  1200. post_prologue_pc
  1201. = arm_skip_stack_protector (post_prologue_pc, gdbarch);
  1202. /* GCC always emits a line note before the prologue and another
  1203. one after, even if the two are at the same address or on the
  1204. same line. Take advantage of this so that we do not need to
  1205. know every instruction that might appear in the prologue. We
  1206. will have producer information for most binaries; if it is
  1207. missing (e.g. for -gstabs), assuming the GNU tools. */
  1208. if (post_prologue_pc
  1209. && (cust == NULL
  1210. || cust->producer () == NULL
  1211. || startswith (cust->producer (), "GNU ")
  1212. || producer_is_llvm (cust->producer ())))
  1213. return post_prologue_pc;
  1214. if (post_prologue_pc != 0)
  1215. {
  1216. CORE_ADDR analyzed_limit;
  1217. /* For non-GCC compilers, make sure the entire line is an
  1218. acceptable prologue; GDB will round this function's
  1219. return value up to the end of the following line so we
  1220. can not skip just part of a line (and we do not want to).
  1221. RealView does not treat the prologue specially, but does
  1222. associate prologue code with the opening brace; so this
  1223. lets us skip the first line if we think it is the opening
  1224. brace. */
  1225. if (arm_pc_is_thumb (gdbarch, func_addr))
  1226. analyzed_limit = thumb_analyze_prologue (gdbarch, func_addr,
  1227. post_prologue_pc, NULL);
  1228. else
  1229. analyzed_limit
  1230. = arm_analyze_prologue (gdbarch, func_addr, post_prologue_pc,
  1231. NULL, target_arm_instruction_reader ());
  1232. if (analyzed_limit != post_prologue_pc)
  1233. return func_addr;
  1234. return post_prologue_pc;
  1235. }
  1236. }
  1237. /* Can't determine prologue from the symbol table, need to examine
  1238. instructions. */
  1239. /* Find an upper limit on the function prologue using the debug
  1240. information. If the debug information could not be used to provide
  1241. that bound, then use an arbitrary large number as the upper bound. */
  1242. /* Like arm_scan_prologue, stop no later than pc + 64. */
  1243. limit_pc = skip_prologue_using_sal (gdbarch, pc);
  1244. if (limit_pc == 0)
  1245. limit_pc = pc + 64; /* Magic. */
  1246. /* Check if this is Thumb code. */
  1247. if (arm_pc_is_thumb (gdbarch, pc))
  1248. return thumb_analyze_prologue (gdbarch, pc, limit_pc, NULL);
  1249. else
  1250. return arm_analyze_prologue (gdbarch, pc, limit_pc, NULL,
  1251. target_arm_instruction_reader ());
  1252. }
  1253. /* *INDENT-OFF* */
  1254. /* Function: thumb_scan_prologue (helper function for arm_scan_prologue)
  1255. This function decodes a Thumb function prologue to determine:
  1256. 1) the size of the stack frame
  1257. 2) which registers are saved on it
  1258. 3) the offsets of saved regs
  1259. 4) the offset from the stack pointer to the frame pointer
  1260. A typical Thumb function prologue would create this stack frame
  1261. (offsets relative to FP)
  1262. old SP -> 24 stack parameters
  1263. 20 LR
  1264. 16 R7
  1265. R7 -> 0 local variables (16 bytes)
  1266. SP -> -12 additional stack space (12 bytes)
  1267. The frame size would thus be 36 bytes, and the frame offset would be
  1268. 12 bytes. The frame register is R7.
  1269. The comments for thumb_skip_prolog() describe the algorithm we use
  1270. to detect the end of the prolog. */
  1271. /* *INDENT-ON* */
  1272. static void
  1273. thumb_scan_prologue (struct gdbarch *gdbarch, CORE_ADDR prev_pc,
  1274. CORE_ADDR block_addr, struct arm_prologue_cache *cache)
  1275. {
  1276. CORE_ADDR prologue_start;
  1277. CORE_ADDR prologue_end;
  1278. if (find_pc_partial_function (block_addr, NULL, &prologue_start,
  1279. &prologue_end))
  1280. {
  1281. /* See comment in arm_scan_prologue for an explanation of
  1282. this heuristics. */
  1283. if (prologue_end > prologue_start + 64)
  1284. {
  1285. prologue_end = prologue_start + 64;
  1286. }
  1287. }
  1288. else
  1289. /* We're in the boondocks: we have no idea where the start of the
  1290. function is. */
  1291. return;
  1292. prologue_end = std::min (prologue_end, prev_pc);
  1293. thumb_analyze_prologue (gdbarch, prologue_start, prologue_end, cache);
  1294. }
  1295. /* Return 1 if the ARM instruction INSN restores SP in epilogue, 0
  1296. otherwise. */
  1297. static int
  1298. arm_instruction_restores_sp (unsigned int insn)
  1299. {
  1300. if (bits (insn, 28, 31) != INST_NV)
  1301. {
  1302. if ((insn & 0x0df0f000) == 0x0080d000
  1303. /* ADD SP (register or immediate). */
  1304. || (insn & 0x0df0f000) == 0x0040d000
  1305. /* SUB SP (register or immediate). */
  1306. || (insn & 0x0ffffff0) == 0x01a0d000
  1307. /* MOV SP. */
  1308. || (insn & 0x0fff0000) == 0x08bd0000
  1309. /* POP (LDMIA). */
  1310. || (insn & 0x0fff0000) == 0x049d0000)
  1311. /* POP of a single register. */
  1312. return 1;
  1313. }
  1314. return 0;
  1315. }
  1316. /* Implement immediate value decoding, as described in section A5.2.4
  1317. (Modified immediate constants in ARM instructions) of the ARM Architecture
  1318. Reference Manual (ARMv7-A and ARMv7-R edition). */
  1319. static uint32_t
  1320. arm_expand_immediate (uint32_t imm)
  1321. {
  1322. /* Immediate values are 12 bits long. */
  1323. gdb_assert ((imm & 0xfffff000) == 0);
  1324. uint32_t unrotated_value = imm & 0xff;
  1325. uint32_t rotate_amount = (imm & 0xf00) >> 7;
  1326. if (rotate_amount == 0)
  1327. return unrotated_value;
  1328. return ((unrotated_value >> rotate_amount)
  1329. | (unrotated_value << (32 - rotate_amount)));
  1330. }
  1331. /* Analyze an ARM mode prologue starting at PROLOGUE_START and
  1332. continuing no further than PROLOGUE_END. If CACHE is non-NULL,
  1333. fill it in. Return the first address not recognized as a prologue
  1334. instruction.
  1335. We recognize all the instructions typically found in ARM prologues,
  1336. plus harmless instructions which can be skipped (either for analysis
  1337. purposes, or a more restrictive set that can be skipped when finding
  1338. the end of the prologue). */
  1339. static CORE_ADDR
  1340. arm_analyze_prologue (struct gdbarch *gdbarch,
  1341. CORE_ADDR prologue_start, CORE_ADDR prologue_end,
  1342. struct arm_prologue_cache *cache,
  1343. const arm_instruction_reader &insn_reader)
  1344. {
  1345. enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
  1346. int regno;
  1347. CORE_ADDR offset, current_pc;
  1348. pv_t regs[ARM_FPS_REGNUM];
  1349. CORE_ADDR unrecognized_pc = 0;
  1350. arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  1351. /* Search the prologue looking for instructions that set up the
  1352. frame pointer, adjust the stack pointer, and save registers.
  1353. Be careful, however, and if it doesn't look like a prologue,
  1354. don't try to scan it. If, for instance, a frameless function
  1355. begins with stmfd sp!, then we will tell ourselves there is
  1356. a frame, which will confuse stack traceback, as well as "finish"
  1357. and other operations that rely on a knowledge of the stack
  1358. traceback. */
  1359. for (regno = 0; regno < ARM_FPS_REGNUM; regno++)
  1360. regs[regno] = pv_register (regno, 0);
  1361. pv_area stack (ARM_SP_REGNUM, gdbarch_addr_bit (gdbarch));
  1362. for (current_pc = prologue_start;
  1363. current_pc < prologue_end;
  1364. current_pc += 4)
  1365. {
  1366. uint32_t insn = insn_reader.read (current_pc, byte_order_for_code);
  1367. if (insn == 0xe1a0c00d) /* mov ip, sp */
  1368. {
  1369. regs[ARM_IP_REGNUM] = regs[ARM_SP_REGNUM];
  1370. continue;
  1371. }
  1372. else if ((insn & 0xfff00000) == 0xe2800000 /* add Rd, Rn, #n */
  1373. && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
  1374. {
  1375. uint32_t imm = arm_expand_immediate (insn & 0xfff);
  1376. int rd = bits (insn, 12, 15);
  1377. regs[rd] = pv_add_constant (regs[bits (insn, 16, 19)], imm);
  1378. continue;
  1379. }
  1380. else if ((insn & 0xfff00000) == 0xe2400000 /* sub Rd, Rn, #n */
  1381. && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
  1382. {
  1383. uint32_t imm = arm_expand_immediate (insn & 0xfff);
  1384. int rd = bits (insn, 12, 15);
  1385. regs[rd] = pv_add_constant (regs[bits (insn, 16, 19)], -imm);
  1386. continue;
  1387. }
  1388. else if ((insn & 0xffff0fff) == 0xe52d0004) /* str Rd,
  1389. [sp, #-4]! */
  1390. {
  1391. if (stack.store_would_trash (regs[ARM_SP_REGNUM]))
  1392. break;
  1393. regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -4);
  1394. stack.store (regs[ARM_SP_REGNUM], 4,
  1395. regs[bits (insn, 12, 15)]);
  1396. continue;
  1397. }
  1398. else if ((insn & 0xffff0000) == 0xe92d0000)
  1399. /* stmfd sp!, {..., fp, ip, lr, pc}
  1400. or
  1401. stmfd sp!, {a1, a2, a3, a4} */
  1402. {
  1403. int mask = insn & 0xffff;
  1404. if (stack.store_would_trash (regs[ARM_SP_REGNUM]))
  1405. break;
  1406. /* Calculate offsets of saved registers. */
  1407. for (regno = ARM_PC_REGNUM; regno >= 0; regno--)
  1408. if (mask & (1 << regno))
  1409. {
  1410. regs[ARM_SP_REGNUM]
  1411. = pv_add_constant (regs[ARM_SP_REGNUM], -4);
  1412. stack.store (regs[ARM_SP_REGNUM], 4, regs[regno]);
  1413. }
  1414. }
  1415. else if ((insn & 0xffff0000) == 0xe54b0000 /* strb rx,[r11,#-n] */
  1416. || (insn & 0xffff00f0) == 0xe14b00b0 /* strh rx,[r11,#-n] */
  1417. || (insn & 0xffffc000) == 0xe50b0000) /* str rx,[r11,#-n] */
  1418. {
  1419. /* No need to add this to saved_regs -- it's just an arg reg. */
  1420. continue;
  1421. }
  1422. else if ((insn & 0xffff0000) == 0xe5cd0000 /* strb rx,[sp,#n] */
  1423. || (insn & 0xffff00f0) == 0xe1cd00b0 /* strh rx,[sp,#n] */
  1424. || (insn & 0xffffc000) == 0xe58d0000) /* str rx,[sp,#n] */
  1425. {
  1426. /* No need to add this to saved_regs -- it's just an arg reg. */
  1427. continue;
  1428. }
  1429. else if ((insn & 0xfff00000) == 0xe8800000 /* stm Rn,
  1430. { registers } */
  1431. && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
  1432. {
  1433. /* No need to add this to saved_regs -- it's just arg regs. */
  1434. continue;
  1435. }
  1436. else if ((insn & 0xfffff000) == 0xe24cb000) /* sub fp, ip #n */
  1437. {
  1438. uint32_t imm = arm_expand_immediate (insn & 0xfff);
  1439. regs[ARM_FP_REGNUM] = pv_add_constant (regs[ARM_IP_REGNUM], -imm);
  1440. }
  1441. else if ((insn & 0xfffff000) == 0xe24dd000) /* sub sp, sp #n */
  1442. {
  1443. uint32_t imm = arm_expand_immediate(insn & 0xfff);
  1444. regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -imm);
  1445. }
  1446. else if ((insn & 0xffff7fff) == 0xed6d0103 /* stfe f?,
  1447. [sp, -#c]! */
  1448. && tdep->have_fpa_registers)
  1449. {
  1450. if (stack.store_would_trash (regs[ARM_SP_REGNUM]))
  1451. break;
  1452. regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -12);
  1453. regno = ARM_F0_REGNUM + ((insn >> 12) & 0x07);
  1454. stack.store (regs[ARM_SP_REGNUM], 12, regs[regno]);
  1455. }
  1456. else if ((insn & 0xffbf0fff) == 0xec2d0200 /* sfmfd f0, 4,
  1457. [sp!] */
  1458. && tdep->have_fpa_registers)
  1459. {
  1460. int n_saved_fp_regs;
  1461. unsigned int fp_start_reg, fp_bound_reg;
  1462. if (stack.store_would_trash (regs[ARM_SP_REGNUM]))
  1463. break;
  1464. if ((insn & 0x800) == 0x800) /* N0 is set */
  1465. {
  1466. if ((insn & 0x40000) == 0x40000) /* N1 is set */
  1467. n_saved_fp_regs = 3;
  1468. else
  1469. n_saved_fp_regs = 1;
  1470. }
  1471. else
  1472. {
  1473. if ((insn & 0x40000) == 0x40000) /* N1 is set */
  1474. n_saved_fp_regs = 2;
  1475. else
  1476. n_saved_fp_regs = 4;
  1477. }
  1478. fp_start_reg = ARM_F0_REGNUM + ((insn >> 12) & 0x7);
  1479. fp_bound_reg = fp_start_reg + n_saved_fp_regs;
  1480. for (; fp_start_reg < fp_bound_reg; fp_start_reg++)
  1481. {
  1482. regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -12);
  1483. stack.store (regs[ARM_SP_REGNUM], 12,
  1484. regs[fp_start_reg++]);
  1485. }
  1486. }
  1487. else if ((insn & 0xff000000) == 0xeb000000 && cache == NULL) /* bl */
  1488. {
  1489. /* Allow some special function calls when skipping the
  1490. prologue; GCC generates these before storing arguments to
  1491. the stack. */
  1492. CORE_ADDR dest = BranchDest (current_pc, insn);
  1493. if (skip_prologue_function (gdbarch, dest, 0))
  1494. continue;
  1495. else
  1496. break;
  1497. }
  1498. else if ((insn & 0xf0000000) != 0xe0000000)
  1499. break; /* Condition not true, exit early. */
  1500. else if (arm_instruction_changes_pc (insn))
  1501. /* Don't scan past anything that might change control flow. */
  1502. break;
  1503. else if (arm_instruction_restores_sp (insn))
  1504. {
  1505. /* Don't scan past the epilogue. */
  1506. break;
  1507. }
  1508. else if ((insn & 0xfe500000) == 0xe8100000 /* ldm */
  1509. && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
  1510. /* Ignore block loads from the stack, potentially copying
  1511. parameters from memory. */
  1512. continue;
  1513. else if ((insn & 0xfc500000) == 0xe4100000
  1514. && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
  1515. /* Similarly ignore single loads from the stack. */
  1516. continue;
  1517. else if ((insn & 0xffff0ff0) == 0xe1a00000)
  1518. /* MOV Rd, Rm. Skip register copies, i.e. saves to another
  1519. register instead of the stack. */
  1520. continue;
  1521. else
  1522. {
  1523. /* The optimizer might shove anything into the prologue, if
  1524. we build up cache (cache != NULL) from scanning prologue,
  1525. we just skip what we don't recognize and scan further to
  1526. make cache as complete as possible. However, if we skip
  1527. prologue, we'll stop immediately on unrecognized
  1528. instruction. */
  1529. unrecognized_pc = current_pc;
  1530. if (cache != NULL)
  1531. continue;
  1532. else
  1533. break;
  1534. }
  1535. }
  1536. if (unrecognized_pc == 0)
  1537. unrecognized_pc = current_pc;
  1538. if (cache)
  1539. {
  1540. int framereg, framesize;
  1541. /* The frame size is just the distance from the frame register
  1542. to the original stack pointer. */
  1543. if (pv_is_register (regs[ARM_FP_REGNUM], ARM_SP_REGNUM))
  1544. {
  1545. /* Frame pointer is fp. */
  1546. framereg = ARM_FP_REGNUM;
  1547. framesize = -regs[ARM_FP_REGNUM].k;
  1548. }
  1549. else
  1550. {
  1551. /* Try the stack pointer... this is a bit desperate. */
  1552. framereg = ARM_SP_REGNUM;
  1553. framesize = -regs[ARM_SP_REGNUM].k;
  1554. }
  1555. cache->framereg = framereg;
  1556. cache->framesize = framesize;
  1557. for (regno = 0; regno < ARM_FPS_REGNUM; regno++)
  1558. if (stack.find_reg (gdbarch, regno, &offset))
  1559. cache->saved_regs[regno].set_addr (offset);
  1560. }
  1561. arm_debug_printf ("Prologue scan stopped at %s",
  1562. paddress (gdbarch, unrecognized_pc));
  1563. return unrecognized_pc;
  1564. }
  1565. static void
  1566. arm_scan_prologue (struct frame_info *this_frame,
  1567. struct arm_prologue_cache *cache)
  1568. {
  1569. struct gdbarch *gdbarch = get_frame_arch (this_frame);
  1570. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1571. CORE_ADDR prologue_start, prologue_end;
  1572. CORE_ADDR prev_pc = get_frame_pc (this_frame);
  1573. CORE_ADDR block_addr = get_frame_address_in_block (this_frame);
  1574. arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  1575. /* Assume there is no frame until proven otherwise. */
  1576. cache->framereg = ARM_SP_REGNUM;
  1577. cache->framesize = 0;
  1578. /* Check for Thumb prologue. */
  1579. if (arm_frame_is_thumb (this_frame))
  1580. {
  1581. thumb_scan_prologue (gdbarch, prev_pc, block_addr, cache);
  1582. return;
  1583. }
  1584. /* Find the function prologue. If we can't find the function in
  1585. the symbol table, peek in the stack frame to find the PC. */
  1586. if (find_pc_partial_function (block_addr, NULL, &prologue_start,
  1587. &prologue_end))
  1588. {
  1589. /* One way to find the end of the prologue (which works well
  1590. for unoptimized code) is to do the following:
  1591. struct symtab_and_line sal = find_pc_line (prologue_start, 0);
  1592. if (sal.line == 0)
  1593. prologue_end = prev_pc;
  1594. else if (sal.end < prologue_end)
  1595. prologue_end = sal.end;
  1596. This mechanism is very accurate so long as the optimizer
  1597. doesn't move any instructions from the function body into the
  1598. prologue. If this happens, sal.end will be the last
  1599. instruction in the first hunk of prologue code just before
  1600. the first instruction that the scheduler has moved from
  1601. the body to the prologue.
  1602. In order to make sure that we scan all of the prologue
  1603. instructions, we use a slightly less accurate mechanism which
  1604. may scan more than necessary. To help compensate for this
  1605. lack of accuracy, the prologue scanning loop below contains
  1606. several clauses which'll cause the loop to terminate early if
  1607. an implausible prologue instruction is encountered.
  1608. The expression
  1609. prologue_start + 64
  1610. is a suitable endpoint since it accounts for the largest
  1611. possible prologue plus up to five instructions inserted by
  1612. the scheduler. */
  1613. if (prologue_end > prologue_start + 64)
  1614. {
  1615. prologue_end = prologue_start + 64; /* See above. */
  1616. }
  1617. }
  1618. else
  1619. {
  1620. /* We have no symbol information. Our only option is to assume this
  1621. function has a standard stack frame and the normal frame register.
  1622. Then, we can find the value of our frame pointer on entrance to
  1623. the callee (or at the present moment if this is the innermost frame).
  1624. The value stored there should be the address of the stmfd + 8. */
  1625. CORE_ADDR frame_loc;
  1626. ULONGEST return_value;
  1627. /* AAPCS does not use a frame register, so we can abort here. */
  1628. if (tdep->arm_abi == ARM_ABI_AAPCS)
  1629. return;
  1630. frame_loc = get_frame_register_unsigned (this_frame, ARM_FP_REGNUM);
  1631. if (!safe_read_memory_unsigned_integer (frame_loc, 4, byte_order,
  1632. &return_value))
  1633. return;
  1634. else
  1635. {
  1636. prologue_start = gdbarch_addr_bits_remove
  1637. (gdbarch, return_value) - 8;
  1638. prologue_end = prologue_start + 64; /* See above. */
  1639. }
  1640. }
  1641. if (prev_pc < prologue_end)
  1642. prologue_end = prev_pc;
  1643. arm_analyze_prologue (gdbarch, prologue_start, prologue_end, cache,
  1644. target_arm_instruction_reader ());
  1645. }
  1646. static struct arm_prologue_cache *
  1647. arm_make_prologue_cache (struct frame_info *this_frame)
  1648. {
  1649. int reg;
  1650. struct arm_prologue_cache *cache;
  1651. CORE_ADDR unwound_fp;
  1652. cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
  1653. cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
  1654. arm_scan_prologue (this_frame, cache);
  1655. unwound_fp = get_frame_register_unsigned (this_frame, cache->framereg);
  1656. if (unwound_fp == 0)
  1657. return cache;
  1658. cache->prev_sp = unwound_fp + cache->framesize;
  1659. /* Calculate actual addresses of saved registers using offsets
  1660. determined by arm_scan_prologue. */
  1661. for (reg = 0; reg < gdbarch_num_regs (get_frame_arch (this_frame)); reg++)
  1662. if (cache->saved_regs[reg].is_addr ())
  1663. cache->saved_regs[reg].set_addr (cache->saved_regs[reg].addr ()
  1664. + cache->prev_sp);
  1665. return cache;
  1666. }
  1667. /* Implementation of the stop_reason hook for arm_prologue frames. */
  1668. static enum unwind_stop_reason
  1669. arm_prologue_unwind_stop_reason (struct frame_info *this_frame,
  1670. void **this_cache)
  1671. {
  1672. struct arm_prologue_cache *cache;
  1673. CORE_ADDR pc;
  1674. if (*this_cache == NULL)
  1675. *this_cache = arm_make_prologue_cache (this_frame);
  1676. cache = (struct arm_prologue_cache *) *this_cache;
  1677. /* This is meant to halt the backtrace at "_start". */
  1678. pc = get_frame_pc (this_frame);
  1679. gdbarch *arch = get_frame_arch (this_frame);
  1680. arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (arch);
  1681. if (pc <= tdep->lowest_pc)
  1682. return UNWIND_OUTERMOST;
  1683. /* If we've hit a wall, stop. */
  1684. if (cache->prev_sp == 0)
  1685. return UNWIND_OUTERMOST;
  1686. return UNWIND_NO_REASON;
  1687. }
  1688. /* Our frame ID for a normal frame is the current function's starting PC
  1689. and the caller's SP when we were called. */
  1690. static void
  1691. arm_prologue_this_id (struct frame_info *this_frame,
  1692. void **this_cache,
  1693. struct frame_id *this_id)
  1694. {
  1695. struct arm_prologue_cache *cache;
  1696. struct frame_id id;
  1697. CORE_ADDR pc, func;
  1698. if (*this_cache == NULL)
  1699. *this_cache = arm_make_prologue_cache (this_frame);
  1700. cache = (struct arm_prologue_cache *) *this_cache;
  1701. /* Use function start address as part of the frame ID. If we cannot
  1702. identify the start address (due to missing symbol information),
  1703. fall back to just using the current PC. */
  1704. pc = get_frame_pc (this_frame);
  1705. func = get_frame_func (this_frame);
  1706. if (!func)
  1707. func = pc;
  1708. id = frame_id_build (cache->prev_sp, func);
  1709. *this_id = id;
  1710. }
  1711. static struct value *
  1712. arm_prologue_prev_register (struct frame_info *this_frame,
  1713. void **this_cache,
  1714. int prev_regnum)
  1715. {
  1716. struct gdbarch *gdbarch = get_frame_arch (this_frame);
  1717. struct arm_prologue_cache *cache;
  1718. if (*this_cache == NULL)
  1719. *this_cache = arm_make_prologue_cache (this_frame);
  1720. cache = (struct arm_prologue_cache *) *this_cache;
  1721. arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  1722. /* If this frame has signed the return address, mark it as so. */
  1723. if (tdep->have_pacbti && cache->ra_signed_state.has_value ()
  1724. && *cache->ra_signed_state)
  1725. set_frame_previous_pc_masked (this_frame);
  1726. /* If we are asked to unwind the PC, then we need to return the LR
  1727. instead. The prologue may save PC, but it will point into this
  1728. frame's prologue, not the next frame's resume location. Also
  1729. strip the saved T bit. A valid LR may have the low bit set, but
  1730. a valid PC never does. */
  1731. if (prev_regnum == ARM_PC_REGNUM)
  1732. {
  1733. CORE_ADDR lr;
  1734. lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
  1735. return frame_unwind_got_constant (this_frame, prev_regnum,
  1736. arm_addr_bits_remove (gdbarch, lr));
  1737. }
  1738. /* SP is generally not saved to the stack, but this frame is
  1739. identified by the next frame's stack pointer at the time of the call.
  1740. The value was already reconstructed into PREV_SP. */
  1741. if (prev_regnum == ARM_SP_REGNUM)
  1742. return frame_unwind_got_constant (this_frame, prev_regnum, cache->prev_sp);
  1743. /* The CPSR may have been changed by the call instruction and by the
  1744. called function. The only bit we can reconstruct is the T bit,
  1745. by checking the low bit of LR as of the call. This is a reliable
  1746. indicator of Thumb-ness except for some ARM v4T pre-interworking
  1747. Thumb code, which could get away with a clear low bit as long as
  1748. the called function did not use bx. Guess that all other
  1749. bits are unchanged; the condition flags are presumably lost,
  1750. but the processor status is likely valid. */
  1751. if (prev_regnum == ARM_PS_REGNUM)
  1752. {
  1753. CORE_ADDR lr, cpsr;
  1754. ULONGEST t_bit = arm_psr_thumb_bit (gdbarch);
  1755. cpsr = get_frame_register_unsigned (this_frame, prev_regnum);
  1756. lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
  1757. if (IS_THUMB_ADDR (lr))
  1758. cpsr |= t_bit;
  1759. else
  1760. cpsr &= ~t_bit;
  1761. return frame_unwind_got_constant (this_frame, prev_regnum, cpsr);
  1762. }
  1763. return trad_frame_get_prev_register (this_frame, cache->saved_regs,
  1764. prev_regnum);
  1765. }
  1766. static frame_unwind arm_prologue_unwind = {
  1767. "arm prologue",
  1768. NORMAL_FRAME,
  1769. arm_prologue_unwind_stop_reason,
  1770. arm_prologue_this_id,
  1771. arm_prologue_prev_register,
  1772. NULL,
  1773. default_frame_sniffer
  1774. };
  1775. /* Maintain a list of ARM exception table entries per objfile, similar to the
  1776. list of mapping symbols. We only cache entries for standard ARM-defined
  1777. personality routines; the cache will contain only the frame unwinding
  1778. instructions associated with the entry (not the descriptors). */
  1779. struct arm_exidx_entry
  1780. {
  1781. CORE_ADDR addr;
  1782. gdb_byte *entry;
  1783. bool operator< (const arm_exidx_entry &other) const
  1784. {
  1785. return addr < other.addr;
  1786. }
  1787. };
  1788. struct arm_exidx_data
  1789. {
  1790. std::vector<std::vector<arm_exidx_entry>> section_maps;
  1791. };
  1792. /* Per-BFD key to store exception handling information. */
  1793. static const struct bfd_key<arm_exidx_data> arm_exidx_data_key;
  1794. static struct obj_section *
  1795. arm_obj_section_from_vma (struct objfile *objfile, bfd_vma vma)
  1796. {
  1797. struct obj_section *osect;
  1798. ALL_OBJFILE_OSECTIONS (objfile, osect)
  1799. if (bfd_section_flags (osect->the_bfd_section) & SEC_ALLOC)
  1800. {
  1801. bfd_vma start, size;
  1802. start = bfd_section_vma (osect->the_bfd_section);
  1803. size = bfd_section_size (osect->the_bfd_section);
  1804. if (start <= vma && vma < start + size)
  1805. return osect;
  1806. }
  1807. return NULL;
  1808. }
  1809. /* Parse contents of exception table and exception index sections
  1810. of OBJFILE, and fill in the exception table entry cache.
  1811. For each entry that refers to a standard ARM-defined personality
  1812. routine, extract the frame unwinding instructions (from either
  1813. the index or the table section). The unwinding instructions
  1814. are normalized by:
  1815. - extracting them from the rest of the table data
  1816. - converting to host endianness
  1817. - appending the implicit 0xb0 ("Finish") code
  1818. The extracted and normalized instructions are stored for later
  1819. retrieval by the arm_find_exidx_entry routine. */
  1820. static void
  1821. arm_exidx_new_objfile (struct objfile *objfile)
  1822. {
  1823. struct arm_exidx_data *data;
  1824. asection *exidx, *extab;
  1825. bfd_vma exidx_vma = 0, extab_vma = 0;
  1826. LONGEST i;
  1827. /* If we've already touched this file, do nothing. */
  1828. if (!objfile || arm_exidx_data_key.get (objfile->obfd) != NULL)
  1829. return;
  1830. /* Read contents of exception table and index. */
  1831. exidx = bfd_get_section_by_name (objfile->obfd, ELF_STRING_ARM_unwind);
  1832. gdb::byte_vector exidx_data;
  1833. if (exidx)
  1834. {
  1835. exidx_vma = bfd_section_vma (exidx);
  1836. exidx_data.resize (bfd_section_size (exidx));
  1837. if (!bfd_get_section_contents (objfile->obfd, exidx,
  1838. exidx_data.data (), 0,
  1839. exidx_data.size ()))
  1840. return;
  1841. }
  1842. extab = bfd_get_section_by_name (objfile->obfd, ".ARM.extab");
  1843. gdb::byte_vector extab_data;
  1844. if (extab)
  1845. {
  1846. extab_vma = bfd_section_vma (extab);
  1847. extab_data.resize (bfd_section_size (extab));
  1848. if (!bfd_get_section_contents (objfile->obfd, extab,
  1849. extab_data.data (), 0,
  1850. extab_data.size ()))
  1851. return;
  1852. }
  1853. /* Allocate exception table data structure. */
  1854. data = arm_exidx_data_key.emplace (objfile->obfd);
  1855. data->section_maps.resize (objfile->obfd->section_count);
  1856. /* Fill in exception table. */
  1857. for (i = 0; i < exidx_data.size () / 8; i++)
  1858. {
  1859. struct arm_exidx_entry new_exidx_entry;
  1860. bfd_vma idx = bfd_h_get_32 (objfile->obfd, exidx_data.data () + i * 8);
  1861. bfd_vma val = bfd_h_get_32 (objfile->obfd,
  1862. exidx_data.data () + i * 8 + 4);
  1863. bfd_vma addr = 0, word = 0;
  1864. int n_bytes = 0, n_words = 0;
  1865. struct obj_section *sec;
  1866. gdb_byte *entry = NULL;
  1867. /* Extract address of start of function. */
  1868. idx = ((idx & 0x7fffffff) ^ 0x40000000) - 0x40000000;
  1869. idx += exidx_vma + i * 8;
  1870. /* Find section containing function and compute section offset. */
  1871. sec = arm_obj_section_from_vma (objfile, idx);
  1872. if (sec == NULL)
  1873. continue;
  1874. idx -= bfd_section_vma (sec->the_bfd_section);
  1875. /* Determine address of exception table entry. */
  1876. if (val == 1)
  1877. {
  1878. /* EXIDX_CANTUNWIND -- no exception table entry present. */
  1879. }
  1880. else if ((val & 0xff000000) == 0x80000000)
  1881. {
  1882. /* Exception table entry embedded in .ARM.exidx
  1883. -- must be short form. */
  1884. word = val;
  1885. n_bytes = 3;
  1886. }
  1887. else if (!(val & 0x80000000))
  1888. {
  1889. /* Exception table entry in .ARM.extab. */
  1890. addr = ((val & 0x7fffffff) ^ 0x40000000) - 0x40000000;
  1891. addr += exidx_vma + i * 8 + 4;
  1892. if (addr >= extab_vma && addr + 4 <= extab_vma + extab_data.size ())
  1893. {
  1894. word = bfd_h_get_32 (objfile->obfd,
  1895. extab_data.data () + addr - extab_vma);
  1896. addr += 4;
  1897. if ((word & 0xff000000) == 0x80000000)
  1898. {
  1899. /* Short form. */
  1900. n_bytes = 3;
  1901. }
  1902. else if ((word & 0xff000000) == 0x81000000
  1903. || (word & 0xff000000) == 0x82000000)
  1904. {
  1905. /* Long form. */
  1906. n_bytes = 2;
  1907. n_words = ((word >> 16) & 0xff);
  1908. }
  1909. else if (!(word & 0x80000000))
  1910. {
  1911. bfd_vma pers;
  1912. struct obj_section *pers_sec;
  1913. int gnu_personality = 0;
  1914. /* Custom personality routine. */
  1915. pers = ((word & 0x7fffffff) ^ 0x40000000) - 0x40000000;
  1916. pers = UNMAKE_THUMB_ADDR (pers + addr - 4);
  1917. /* Check whether we've got one of the variants of the
  1918. GNU personality routines. */
  1919. pers_sec = arm_obj_section_from_vma (objfile, pers);
  1920. if (pers_sec)
  1921. {
  1922. static const char *personality[] =
  1923. {
  1924. "__gcc_personality_v0",
  1925. "__gxx_personality_v0",
  1926. "__gcj_personality_v0",
  1927. "__gnu_objc_personality_v0",
  1928. NULL
  1929. };
  1930. CORE_ADDR pc = pers + pers_sec->offset ();
  1931. int k;
  1932. for (k = 0; personality[k]; k++)
  1933. if (lookup_minimal_symbol_by_pc_name
  1934. (pc, personality[k], objfile))
  1935. {
  1936. gnu_personality = 1;
  1937. break;
  1938. }
  1939. }
  1940. /* If so, the next word contains a word count in the high
  1941. byte, followed by the same unwind instructions as the
  1942. pre-defined forms. */
  1943. if (gnu_personality
  1944. && addr + 4 <= extab_vma + extab_data.size ())
  1945. {
  1946. word = bfd_h_get_32 (objfile->obfd,
  1947. (extab_data.data ()
  1948. + addr - extab_vma));
  1949. addr += 4;
  1950. n_bytes = 3;
  1951. n_words = ((word >> 24) & 0xff);
  1952. }
  1953. }
  1954. }
  1955. }
  1956. /* Sanity check address. */
  1957. if (n_words)
  1958. if (addr < extab_vma
  1959. || addr + 4 * n_words > extab_vma + extab_data.size ())
  1960. n_words = n_bytes = 0;
  1961. /* The unwind instructions reside in WORD (only the N_BYTES least
  1962. significant bytes are valid), followed by N_WORDS words in the
  1963. extab section starting at ADDR. */
  1964. if (n_bytes || n_words)
  1965. {
  1966. gdb_byte *p = entry
  1967. = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack,
  1968. n_bytes + n_words * 4 + 1);
  1969. while (n_bytes--)
  1970. *p++ = (gdb_byte) ((word >> (8 * n_bytes)) & 0xff);
  1971. while (n_words--)
  1972. {
  1973. word = bfd_h_get_32 (objfile->obfd,
  1974. extab_data.data () + addr - extab_vma);
  1975. addr += 4;
  1976. *p++ = (gdb_byte) ((word >> 24) & 0xff);
  1977. *p++ = (gdb_byte) ((word >> 16) & 0xff);
  1978. *p++ = (gdb_byte) ((word >> 8) & 0xff);
  1979. *p++ = (gdb_byte) (word & 0xff);
  1980. }
  1981. /* Implied "Finish" to terminate the list. */
  1982. *p++ = 0xb0;
  1983. }
  1984. /* Push entry onto vector. They are guaranteed to always
  1985. appear in order of increasing addresses. */
  1986. new_exidx_entry.addr = idx;
  1987. new_exidx_entry.entry = entry;
  1988. data->section_maps[sec->the_bfd_section->index].push_back
  1989. (new_exidx_entry);
  1990. }
  1991. }
  1992. /* Search for the exception table entry covering MEMADDR. If one is found,
  1993. return a pointer to its data. Otherwise, return 0. If START is non-NULL,
  1994. set *START to the start of the region covered by this entry. */
  1995. static gdb_byte *
  1996. arm_find_exidx_entry (CORE_ADDR memaddr, CORE_ADDR *start)
  1997. {
  1998. struct obj_section *sec;
  1999. sec = find_pc_section (memaddr);
  2000. if (sec != NULL)
  2001. {
  2002. struct arm_exidx_data *data;
  2003. struct arm_exidx_entry map_key = { memaddr - sec->addr (), 0 };
  2004. data = arm_exidx_data_key.get (sec->objfile->obfd);
  2005. if (data != NULL)
  2006. {
  2007. std::vector<arm_exidx_entry> &map
  2008. = data->section_maps[sec->the_bfd_section->index];
  2009. if (!map.empty ())
  2010. {
  2011. auto idx = std::lower_bound (map.begin (), map.end (), map_key);
  2012. /* std::lower_bound finds the earliest ordered insertion
  2013. point. If the following symbol starts at this exact
  2014. address, we use that; otherwise, the preceding
  2015. exception table entry covers this address. */
  2016. if (idx < map.end ())
  2017. {
  2018. if (idx->addr == map_key.addr)
  2019. {
  2020. if (start)
  2021. *start = idx->addr + sec->addr ();
  2022. return idx->entry;
  2023. }
  2024. }
  2025. if (idx > map.begin ())
  2026. {
  2027. idx = idx - 1;
  2028. if (start)
  2029. *start = idx->addr + sec->addr ();
  2030. return idx->entry;
  2031. }
  2032. }
  2033. }
  2034. }
  2035. return NULL;
  2036. }
  2037. /* Given the current frame THIS_FRAME, and its associated frame unwinding
  2038. instruction list from the ARM exception table entry ENTRY, allocate and
  2039. return a prologue cache structure describing how to unwind this frame.
  2040. Return NULL if the unwinding instruction list contains a "spare",
  2041. "reserved" or "refuse to unwind" instruction as defined in section
  2042. "9.3 Frame unwinding instructions" of the "Exception Handling ABI
  2043. for the ARM Architecture" document. */
  2044. static struct arm_prologue_cache *
  2045. arm_exidx_fill_cache (struct frame_info *this_frame, gdb_byte *entry)
  2046. {
  2047. CORE_ADDR vsp = 0;
  2048. int vsp_valid = 0;
  2049. struct arm_prologue_cache *cache;
  2050. cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
  2051. cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
  2052. for (;;)
  2053. {
  2054. gdb_byte insn;
  2055. /* Whenever we reload SP, we actually have to retrieve its
  2056. actual value in the current frame. */
  2057. if (!vsp_valid)
  2058. {
  2059. if (cache->saved_regs[ARM_SP_REGNUM].is_realreg ())
  2060. {
  2061. int reg = cache->saved_regs[ARM_SP_REGNUM].realreg ();
  2062. vsp = get_frame_register_unsigned (this_frame, reg);
  2063. }
  2064. else
  2065. {
  2066. CORE_ADDR addr = cache->saved_regs[ARM_SP_REGNUM].addr ();
  2067. vsp = get_frame_memory_unsigned (this_frame, addr, 4);
  2068. }
  2069. vsp_valid = 1;
  2070. }
  2071. /* Decode next unwind instruction. */
  2072. insn = *entry++;
  2073. if ((insn & 0xc0) == 0)
  2074. {
  2075. int offset = insn & 0x3f;
  2076. vsp += (offset << 2) + 4;
  2077. }
  2078. else if ((insn & 0xc0) == 0x40)
  2079. {
  2080. int offset = insn & 0x3f;
  2081. vsp -= (offset << 2) + 4;
  2082. }
  2083. else if ((insn & 0xf0) == 0x80)
  2084. {
  2085. int mask = ((insn & 0xf) << 8) | *entry++;
  2086. int i;
  2087. /* The special case of an all-zero mask identifies
  2088. "Refuse to unwind". We return NULL to fall back
  2089. to the prologue analyzer. */
  2090. if (mask == 0)
  2091. return NULL;
  2092. /* Pop registers r4..r15 under mask. */
  2093. for (i = 0; i < 12; i++)
  2094. if (mask & (1 << i))
  2095. {
  2096. cache->saved_regs[4 + i].set_addr (vsp);
  2097. vsp += 4;
  2098. }
  2099. /* Special-case popping SP -- we need to reload vsp. */
  2100. if (mask & (1 << (ARM_SP_REGNUM - 4)))
  2101. vsp_valid = 0;
  2102. }
  2103. else if ((insn & 0xf0) == 0x90)
  2104. {
  2105. int reg = insn & 0xf;
  2106. /* Reserved cases. */
  2107. if (reg == ARM_SP_REGNUM || reg == ARM_PC_REGNUM)
  2108. return NULL;
  2109. /* Set SP from another register and mark VSP for reload. */
  2110. cache->saved_regs[ARM_SP_REGNUM] = cache->saved_regs[reg];
  2111. vsp_valid = 0;
  2112. }
  2113. else if ((insn & 0xf0) == 0xa0)
  2114. {
  2115. int count = insn & 0x7;
  2116. int pop_lr = (insn & 0x8) != 0;
  2117. int i;
  2118. /* Pop r4..r[4+count]. */
  2119. for (i = 0; i <= count; i++)
  2120. {
  2121. cache->saved_regs[4 + i].set_addr (vsp);
  2122. vsp += 4;
  2123. }
  2124. /* If indicated by flag, pop LR as well. */
  2125. if (pop_lr)
  2126. {
  2127. cache->saved_regs[ARM_LR_REGNUM].set_addr (vsp);
  2128. vsp += 4;
  2129. }
  2130. }
  2131. else if (insn == 0xb0)
  2132. {
  2133. /* We could only have updated PC by popping into it; if so, it
  2134. will show up as address. Otherwise, copy LR into PC. */
  2135. if (!cache->saved_regs[ARM_PC_REGNUM].is_addr ())
  2136. cache->saved_regs[ARM_PC_REGNUM]
  2137. = cache->saved_regs[ARM_LR_REGNUM];
  2138. /* We're done. */
  2139. break;
  2140. }
  2141. else if (insn == 0xb1)
  2142. {
  2143. int mask = *entry++;
  2144. int i;
  2145. /* All-zero mask and mask >= 16 is "spare". */
  2146. if (mask == 0 || mask >= 16)
  2147. return NULL;
  2148. /* Pop r0..r3 under mask. */
  2149. for (i = 0; i < 4; i++)
  2150. if (mask & (1 << i))
  2151. {
  2152. cache->saved_regs[i].set_addr (vsp);
  2153. vsp += 4;
  2154. }
  2155. }
  2156. else if (insn == 0xb2)
  2157. {
  2158. ULONGEST offset = 0;
  2159. unsigned shift = 0;
  2160. do
  2161. {
  2162. offset |= (*entry & 0x7f) << shift;
  2163. shift += 7;
  2164. }
  2165. while (*entry++ & 0x80);
  2166. vsp += 0x204 + (offset << 2);
  2167. }
  2168. else if (insn == 0xb3)
  2169. {
  2170. int start = *entry >> 4;
  2171. int count = (*entry++) & 0xf;
  2172. int i;
  2173. /* Only registers D0..D15 are valid here. */
  2174. if (start + count >= 16)
  2175. return NULL;
  2176. /* Pop VFP double-precision registers D[start]..D[start+count]. */
  2177. for (i = 0; i <= count; i++)
  2178. {
  2179. cache->saved_regs[ARM_D0_REGNUM + start + i].set_addr (vsp);
  2180. vsp += 8;
  2181. }
  2182. /* Add an extra 4 bytes for FSTMFDX-style stack. */
  2183. vsp += 4;
  2184. }
  2185. else if ((insn & 0xf8) == 0xb8)
  2186. {
  2187. int count = insn & 0x7;
  2188. int i;
  2189. /* Pop VFP double-precision registers D[8]..D[8+count]. */
  2190. for (i = 0; i <= count; i++)
  2191. {
  2192. cache->saved_regs[ARM_D0_REGNUM + 8 + i].set_addr (vsp);
  2193. vsp += 8;
  2194. }
  2195. /* Add an extra 4 bytes for FSTMFDX-style stack. */
  2196. vsp += 4;
  2197. }
  2198. else if (insn == 0xc6)
  2199. {
  2200. int start = *entry >> 4;
  2201. int count = (*entry++) & 0xf;
  2202. int i;
  2203. /* Only registers WR0..WR15 are valid. */
  2204. if (start + count >= 16)
  2205. return NULL;
  2206. /* Pop iwmmx registers WR[start]..WR[start+count]. */
  2207. for (i = 0; i <= count; i++)
  2208. {
  2209. cache->saved_regs[ARM_WR0_REGNUM + start + i].set_addr (vsp);
  2210. vsp += 8;
  2211. }
  2212. }
  2213. else if (insn == 0xc7)
  2214. {
  2215. int mask = *entry++;
  2216. int i;
  2217. /* All-zero mask and mask >= 16 is "spare". */
  2218. if (mask == 0 || mask >= 16)
  2219. return NULL;
  2220. /* Pop iwmmx general-purpose registers WCGR0..WCGR3 under mask. */
  2221. for (i = 0; i < 4; i++)
  2222. if (mask & (1 << i))
  2223. {
  2224. cache->saved_regs[ARM_WCGR0_REGNUM + i].set_addr (vsp);
  2225. vsp += 4;
  2226. }
  2227. }
  2228. else if ((insn & 0xf8) == 0xc0)
  2229. {
  2230. int count = insn & 0x7;
  2231. int i;
  2232. /* Pop iwmmx registers WR[10]..WR[10+count]. */
  2233. for (i = 0; i <= count; i++)
  2234. {
  2235. cache->saved_regs[ARM_WR0_REGNUM + 10 + i].set_addr (vsp);
  2236. vsp += 8;
  2237. }
  2238. }
  2239. else if (insn == 0xc8)
  2240. {
  2241. int start = *entry >> 4;
  2242. int count = (*entry++) & 0xf;
  2243. int i;
  2244. /* Only registers D0..D31 are valid. */
  2245. if (start + count >= 16)
  2246. return NULL;
  2247. /* Pop VFP double-precision registers
  2248. D[16+start]..D[16+start+count]. */
  2249. for (i = 0; i <= count; i++)
  2250. {
  2251. cache->saved_regs[ARM_D0_REGNUM + 16 + start + i].set_addr (vsp);
  2252. vsp += 8;
  2253. }
  2254. }
  2255. else if (insn == 0xc9)
  2256. {
  2257. int start = *entry >> 4;
  2258. int count = (*entry++) & 0xf;
  2259. int i;
  2260. /* Pop VFP double-precision registers D[start]..D[start+count]. */
  2261. for (i = 0; i <= count; i++)
  2262. {
  2263. cache->saved_regs[ARM_D0_REGNUM + start + i].set_addr (vsp);
  2264. vsp += 8;
  2265. }
  2266. }
  2267. else if ((insn & 0xf8) == 0xd0)
  2268. {
  2269. int count = insn & 0x7;
  2270. int i;
  2271. /* Pop VFP double-precision registers D[8]..D[8+count]. */
  2272. for (i = 0; i <= count; i++)
  2273. {
  2274. cache->saved_regs[ARM_D0_REGNUM + 8 + i].set_addr (vsp);
  2275. vsp += 8;
  2276. }
  2277. }
  2278. else
  2279. {
  2280. /* Everything else is "spare". */
  2281. return NULL;
  2282. }
  2283. }
  2284. /* If we restore SP from a register, assume this was the frame register.
  2285. Otherwise just fall back to SP as frame register. */
  2286. if (cache->saved_regs[ARM_SP_REGNUM].is_realreg ())
  2287. cache->framereg = cache->saved_regs[ARM_SP_REGNUM].realreg ();
  2288. else
  2289. cache->framereg = ARM_SP_REGNUM;
  2290. /* Determine offset to previous frame. */
  2291. cache->framesize
  2292. = vsp - get_frame_register_unsigned (this_frame, cache->framereg);
  2293. /* We already got the previous SP. */
  2294. cache->prev_sp = vsp;
  2295. return cache;
  2296. }
  2297. /* Unwinding via ARM exception table entries. Note that the sniffer
  2298. already computes a filled-in prologue cache, which is then used
  2299. with the same arm_prologue_this_id and arm_prologue_prev_register
  2300. routines also used for prologue-parsing based unwinding. */
  2301. static int
  2302. arm_exidx_unwind_sniffer (const struct frame_unwind *self,
  2303. struct frame_info *this_frame,
  2304. void **this_prologue_cache)
  2305. {
  2306. struct gdbarch *gdbarch = get_frame_arch (this_frame);
  2307. enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
  2308. CORE_ADDR addr_in_block, exidx_region, func_start;
  2309. struct arm_prologue_cache *cache;
  2310. gdb_byte *entry;
  2311. /* See if we have an ARM exception table entry covering this address. */
  2312. addr_in_block = get_frame_address_in_block (this_frame);
  2313. entry = arm_find_exidx_entry (addr_in_block, &exidx_region);
  2314. if (!entry)
  2315. return 0;
  2316. /* The ARM exception table does not describe unwind information
  2317. for arbitrary PC values, but is guaranteed to be correct only
  2318. at call sites. We have to decide here whether we want to use
  2319. ARM exception table information for this frame, or fall back
  2320. to using prologue parsing. (Note that if we have DWARF CFI,
  2321. this sniffer isn't even called -- CFI is always preferred.)
  2322. Before we make this decision, however, we check whether we
  2323. actually have *symbol* information for the current frame.
  2324. If not, prologue parsing would not work anyway, so we might
  2325. as well use the exception table and hope for the best. */
  2326. if (find_pc_partial_function (addr_in_block, NULL, &func_start, NULL))
  2327. {
  2328. int exc_valid = 0;
  2329. /* If the next frame is "normal", we are at a call site in this
  2330. frame, so exception information is guaranteed to be valid. */
  2331. if (get_next_frame (this_frame)
  2332. && get_frame_type (get_next_frame (this_frame)) == NORMAL_FRAME)
  2333. exc_valid = 1;
  2334. /* We also assume exception information is valid if we're currently
  2335. blocked in a system call. The system library is supposed to
  2336. ensure this, so that e.g. pthread cancellation works. */
  2337. if (arm_frame_is_thumb (this_frame))
  2338. {
  2339. ULONGEST insn;
  2340. if (safe_read_memory_unsigned_integer (get_frame_pc (this_frame) - 2,
  2341. 2, byte_order_for_code, &insn)
  2342. && (insn & 0xff00) == 0xdf00 /* svc */)
  2343. exc_valid = 1;
  2344. }
  2345. else
  2346. {
  2347. ULONGEST insn;
  2348. if (safe_read_memory_unsigned_integer (get_frame_pc (this_frame) - 4,
  2349. 4, byte_order_for_code, &insn)
  2350. && (insn & 0x0f000000) == 0x0f000000 /* svc */)
  2351. exc_valid = 1;
  2352. }
  2353. /* Bail out if we don't know that exception information is valid. */
  2354. if (!exc_valid)
  2355. return 0;
  2356. /* The ARM exception index does not mark the *end* of the region
  2357. covered by the entry, and some functions will not have any entry.
  2358. To correctly recognize the end of the covered region, the linker
  2359. should have inserted dummy records with a CANTUNWIND marker.
  2360. Unfortunately, current versions of GNU ld do not reliably do
  2361. this, and thus we may have found an incorrect entry above.
  2362. As a (temporary) sanity check, we only use the entry if it
  2363. lies *within* the bounds of the function. Note that this check
  2364. might reject perfectly valid entries that just happen to cover
  2365. multiple functions; therefore this check ought to be removed
  2366. once the linker is fixed. */
  2367. if (func_start > exidx_region)
  2368. return 0;
  2369. }
  2370. /* Decode the list of unwinding instructions into a prologue cache.
  2371. Note that this may fail due to e.g. a "refuse to unwind" code. */
  2372. cache = arm_exidx_fill_cache (this_frame, entry);
  2373. if (!cache)
  2374. return 0;
  2375. *this_prologue_cache = cache;
  2376. return 1;
  2377. }
  2378. struct frame_unwind arm_exidx_unwind = {
  2379. "arm exidx",
  2380. NORMAL_FRAME,
  2381. default_frame_unwind_stop_reason,
  2382. arm_prologue_this_id,
  2383. arm_prologue_prev_register,
  2384. NULL,
  2385. arm_exidx_unwind_sniffer
  2386. };
  2387. static struct arm_prologue_cache *
  2388. arm_make_epilogue_frame_cache (struct frame_info *this_frame)
  2389. {
  2390. struct arm_prologue_cache *cache;
  2391. int reg;
  2392. cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
  2393. cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
  2394. /* Still rely on the offset calculated from prologue. */
  2395. arm_scan_prologue (this_frame, cache);
  2396. /* Since we are in epilogue, the SP has been restored. */
  2397. cache->prev_sp = get_frame_register_unsigned (this_frame, ARM_SP_REGNUM);
  2398. /* Calculate actual addresses of saved registers using offsets
  2399. determined by arm_scan_prologue. */
  2400. for (reg = 0; reg < gdbarch_num_regs (get_frame_arch (this_frame)); reg++)
  2401. if (cache->saved_regs[reg].is_addr ())
  2402. cache->saved_regs[reg].set_addr (cache->saved_regs[reg].addr ()
  2403. + cache->prev_sp);
  2404. return cache;
  2405. }
  2406. /* Implementation of function hook 'this_id' in
  2407. 'struct frame_uwnind' for epilogue unwinder. */
  2408. static void
  2409. arm_epilogue_frame_this_id (struct frame_info *this_frame,
  2410. void **this_cache,
  2411. struct frame_id *this_id)
  2412. {
  2413. struct arm_prologue_cache *cache;
  2414. CORE_ADDR pc, func;
  2415. if (*this_cache == NULL)
  2416. *this_cache = arm_make_epilogue_frame_cache (this_frame);
  2417. cache = (struct arm_prologue_cache *) *this_cache;
  2418. /* Use function start address as part of the frame ID. If we cannot
  2419. identify the start address (due to missing symbol information),
  2420. fall back to just using the current PC. */
  2421. pc = get_frame_pc (this_frame);
  2422. func = get_frame_func (this_frame);
  2423. if (func == 0)
  2424. func = pc;
  2425. (*this_id) = frame_id_build (cache->prev_sp, pc);
  2426. }
  2427. /* Implementation of function hook 'prev_register' in
  2428. 'struct frame_uwnind' for epilogue unwinder. */
  2429. static struct value *
  2430. arm_epilogue_frame_prev_register (struct frame_info *this_frame,
  2431. void **this_cache, int regnum)
  2432. {
  2433. if (*this_cache == NULL)
  2434. *this_cache = arm_make_epilogue_frame_cache (this_frame);
  2435. return arm_prologue_prev_register (this_frame, this_cache, regnum);
  2436. }
  2437. static int arm_stack_frame_destroyed_p_1 (struct gdbarch *gdbarch,
  2438. CORE_ADDR pc);
  2439. static int thumb_stack_frame_destroyed_p (struct gdbarch *gdbarch,
  2440. CORE_ADDR pc);
  2441. /* Implementation of function hook 'sniffer' in
  2442. 'struct frame_uwnind' for epilogue unwinder. */
  2443. static int
  2444. arm_epilogue_frame_sniffer (const struct frame_unwind *self,
  2445. struct frame_info *this_frame,
  2446. void **this_prologue_cache)
  2447. {
  2448. if (frame_relative_level (this_frame) == 0)
  2449. {
  2450. struct gdbarch *gdbarch = get_frame_arch (this_frame);
  2451. CORE_ADDR pc = get_frame_pc (this_frame);
  2452. if (arm_frame_is_thumb (this_frame))
  2453. return thumb_stack_frame_destroyed_p (gdbarch, pc);
  2454. else
  2455. return arm_stack_frame_destroyed_p_1 (gdbarch, pc);
  2456. }
  2457. else
  2458. return 0;
  2459. }
  2460. /* Frame unwinder from epilogue. */
  2461. static const struct frame_unwind arm_epilogue_frame_unwind =
  2462. {
  2463. "arm epilogue",
  2464. NORMAL_FRAME,
  2465. default_frame_unwind_stop_reason,
  2466. arm_epilogue_frame_this_id,
  2467. arm_epilogue_frame_prev_register,
  2468. NULL,
  2469. arm_epilogue_frame_sniffer,
  2470. };
  2471. /* Recognize GCC's trampoline for thumb call-indirect. If we are in a
  2472. trampoline, return the target PC. Otherwise return 0.
  2473. void call0a (char c, short s, int i, long l) {}
  2474. int main (void)
  2475. {
  2476. (*pointer_to_call0a) (c, s, i, l);
  2477. }
  2478. Instead of calling a stub library function _call_via_xx (xx is
  2479. the register name), GCC may inline the trampoline in the object
  2480. file as below (register r2 has the address of call0a).
  2481. .global main
  2482. .type main, %function
  2483. ...
  2484. bl .L1
  2485. ...
  2486. .size main, .-main
  2487. .L1:
  2488. bx r2
  2489. The trampoline 'bx r2' doesn't belong to main. */
  2490. static CORE_ADDR
  2491. arm_skip_bx_reg (struct frame_info *frame, CORE_ADDR pc)
  2492. {
  2493. /* The heuristics of recognizing such trampoline is that FRAME is
  2494. executing in Thumb mode and the instruction on PC is 'bx Rm'. */
  2495. if (arm_frame_is_thumb (frame))
  2496. {
  2497. gdb_byte buf[2];
  2498. if (target_read_memory (pc, buf, 2) == 0)
  2499. {
  2500. struct gdbarch *gdbarch = get_frame_arch (frame);
  2501. enum bfd_endian byte_order_for_code
  2502. = gdbarch_byte_order_for_code (gdbarch);
  2503. uint16_t insn
  2504. = extract_unsigned_integer (buf, 2, byte_order_for_code);
  2505. if ((insn & 0xff80) == 0x4700) /* bx <Rm> */
  2506. {
  2507. CORE_ADDR dest
  2508. = get_frame_register_unsigned (frame, bits (insn, 3, 6));
  2509. /* Clear the LSB so that gdb core sets step-resume
  2510. breakpoint at the right address. */
  2511. return UNMAKE_THUMB_ADDR (dest);
  2512. }
  2513. }
  2514. }
  2515. return 0;
  2516. }
  2517. static struct arm_prologue_cache *
  2518. arm_make_stub_cache (struct frame_info *this_frame)
  2519. {
  2520. struct arm_prologue_cache *cache;
  2521. cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
  2522. cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
  2523. cache->prev_sp = get_frame_register_unsigned (this_frame, ARM_SP_REGNUM);
  2524. return cache;
  2525. }
  2526. /* Our frame ID for a stub frame is the current SP and LR. */
  2527. static void
  2528. arm_stub_this_id (struct frame_info *this_frame,
  2529. void **this_cache,
  2530. struct frame_id *this_id)
  2531. {
  2532. struct arm_prologue_cache *cache;
  2533. if (*this_cache == NULL)
  2534. *this_cache = arm_make_stub_cache (this_frame);
  2535. cache = (struct arm_prologue_cache *) *this_cache;
  2536. *this_id = frame_id_build (cache->prev_sp, get_frame_pc (this_frame));
  2537. }
  2538. static int
  2539. arm_stub_unwind_sniffer (const struct frame_unwind *self,
  2540. struct frame_info *this_frame,
  2541. void **this_prologue_cache)
  2542. {
  2543. CORE_ADDR addr_in_block;
  2544. gdb_byte dummy[4];
  2545. CORE_ADDR pc, start_addr;
  2546. const char *name;
  2547. addr_in_block = get_frame_address_in_block (this_frame);
  2548. pc = get_frame_pc (this_frame);
  2549. if (in_plt_section (addr_in_block)
  2550. /* We also use the stub winder if the target memory is unreadable
  2551. to avoid having the prologue unwinder trying to read it. */
  2552. || target_read_memory (pc, dummy, 4) != 0)
  2553. return 1;
  2554. if (find_pc_partial_function (pc, &name, &start_addr, NULL) == 0
  2555. && arm_skip_bx_reg (this_frame, pc) != 0)
  2556. return 1;
  2557. return 0;
  2558. }
  2559. struct frame_unwind arm_stub_unwind = {
  2560. "arm stub",
  2561. NORMAL_FRAME,
  2562. default_frame_unwind_stop_reason,
  2563. arm_stub_this_id,
  2564. arm_prologue_prev_register,
  2565. NULL,
  2566. arm_stub_unwind_sniffer
  2567. };
  2568. /* Put here the code to store, into CACHE->saved_regs, the addresses
  2569. of the saved registers of frame described by THIS_FRAME. CACHE is
  2570. returned. */
  2571. static struct arm_prologue_cache *
  2572. arm_m_exception_cache (struct frame_info *this_frame)
  2573. {
  2574. struct gdbarch *gdbarch = get_frame_arch (this_frame);
  2575. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  2576. struct arm_prologue_cache *cache;
  2577. CORE_ADDR lr;
  2578. CORE_ADDR sp;
  2579. CORE_ADDR unwound_sp;
  2580. LONGEST xpsr;
  2581. uint32_t exc_return;
  2582. uint32_t process_stack_used;
  2583. uint32_t extended_frame_used;
  2584. uint32_t secure_stack_used;
  2585. cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
  2586. cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
  2587. /* ARMv7-M Architecture Reference "B1.5.6 Exception entry behavior"
  2588. describes which bits in LR that define which stack was used prior
  2589. to the exception and if FPU is used (causing extended stack frame). */
  2590. lr = get_frame_register_unsigned (this_frame, ARM_LR_REGNUM);
  2591. sp = get_frame_register_unsigned (this_frame, ARM_SP_REGNUM);
  2592. /* Check EXC_RETURN indicator bits. */
  2593. exc_return = (((lr >> 28) & 0xf) == 0xf);
  2594. /* Check EXC_RETURN bit SPSEL if Main or Thread (process) stack used. */
  2595. process_stack_used = ((lr & (1 << 2)) != 0);
  2596. if (exc_return && process_stack_used)
  2597. {
  2598. /* Thread (process) stack used.
  2599. Potentially this could be other register defined by target, but PSP
  2600. can be considered a standard name for the "Process Stack Pointer".
  2601. To be fully aware of system registers like MSP and PSP, these could
  2602. be added to a separate XML arm-m-system-profile that is valid for
  2603. ARMv6-M and ARMv7-M architectures. Also to be able to debug eg a
  2604. corefile off-line, then these registers must be defined by GDB,
  2605. and also be included in the corefile regsets. */
  2606. int psp_regnum = user_reg_map_name_to_regnum (gdbarch, "psp", -1);
  2607. if (psp_regnum == -1)
  2608. {
  2609. /* Thread (process) stack could not be fetched,
  2610. give warning and exit. */
  2611. warning (_("no PSP thread stack unwinding supported."));
  2612. /* Terminate any further stack unwinding by refer to self. */
  2613. cache->prev_sp = sp;
  2614. return cache;
  2615. }
  2616. else
  2617. {
  2618. /* Thread (process) stack used, use PSP as SP. */
  2619. unwound_sp = get_frame_register_unsigned (this_frame, psp_regnum);
  2620. }
  2621. }
  2622. else
  2623. {
  2624. /* Main stack used, use MSP as SP. */
  2625. unwound_sp = sp;
  2626. }
  2627. /* The hardware saves eight 32-bit words, comprising xPSR,
  2628. ReturnAddress, LR (R14), R12, R3, R2, R1, R0. See details in
  2629. "B1.5.6 Exception entry behavior" in
  2630. "ARMv7-M Architecture Reference Manual". */
  2631. cache->saved_regs[0].set_addr (unwound_sp);
  2632. cache->saved_regs[1].set_addr (unwound_sp + 4);
  2633. cache->saved_regs[2].set_addr (unwound_sp + 8);
  2634. cache->saved_regs[3].set_addr (unwound_sp + 12);
  2635. cache->saved_regs[ARM_IP_REGNUM].set_addr (unwound_sp + 16);
  2636. cache->saved_regs[ARM_LR_REGNUM].set_addr (unwound_sp + 20);
  2637. cache->saved_regs[ARM_PC_REGNUM].set_addr (unwound_sp + 24);
  2638. cache->saved_regs[ARM_PS_REGNUM].set_addr (unwound_sp + 28);
  2639. /* Check EXC_RETURN bit FTYPE if extended stack frame (FPU regs stored)
  2640. type used. */
  2641. extended_frame_used = ((lr & (1 << 4)) == 0);
  2642. if (exc_return && extended_frame_used)
  2643. {
  2644. int i;
  2645. int fpu_regs_stack_offset;
  2646. /* This code does not take into account the lazy stacking, see "Lazy
  2647. context save of FP state", in B1.5.7, also ARM AN298, supported
  2648. by Cortex-M4F architecture.
  2649. To fully handle this the FPCCR register (Floating-point Context
  2650. Control Register) needs to be read out and the bits ASPEN and LSPEN
  2651. could be checked to setup correct lazy stacked FP registers.
  2652. This register is located at address 0xE000EF34. */
  2653. /* Extended stack frame type used. */
  2654. fpu_regs_stack_offset = unwound_sp + 0x20;
  2655. for (i = 0; i < 16; i++)
  2656. {
  2657. cache->saved_regs[ARM_D0_REGNUM + i].set_addr (fpu_regs_stack_offset);
  2658. fpu_regs_stack_offset += 4;
  2659. }
  2660. cache->saved_regs[ARM_FPSCR_REGNUM].set_addr (unwound_sp + 0x60);
  2661. /* Offset 0x64 is reserved. */
  2662. cache->prev_sp = unwound_sp + 0x68;
  2663. }
  2664. else
  2665. {
  2666. /* Standard stack frame type used. */
  2667. cache->prev_sp = unwound_sp + 0x20;
  2668. }
  2669. /* Check EXC_RETURN bit S if Secure or Non-secure stack used. */
  2670. secure_stack_used = ((lr & (1 << 6)) != 0);
  2671. if (exc_return && secure_stack_used)
  2672. {
  2673. /* ARMv8-M Exception and interrupt handling is not considered here.
  2674. In the ARMv8-M architecture also EXC_RETURN bit S is controlling if
  2675. the Secure or Non-secure stack was used. To separate Secure and
  2676. Non-secure stacks, processors that are based on the ARMv8-M
  2677. architecture support 4 stack pointers: MSP_S, PSP_S, MSP_NS, PSP_NS.
  2678. In addition, a stack limit feature is provided using stack limit
  2679. registers (accessible using MSR and MRS instructions) in Privileged
  2680. level. */
  2681. }
  2682. /* If bit 9 of the saved xPSR is set, then there is a four-byte
  2683. aligner between the top of the 32-byte stack frame and the
  2684. previous context's stack pointer. */
  2685. if (safe_read_memory_integer (unwound_sp + 28, 4, byte_order, &xpsr)
  2686. && (xpsr & (1 << 9)) != 0)
  2687. cache->prev_sp += 4;
  2688. return cache;
  2689. }
  2690. /* Implementation of function hook 'this_id' in
  2691. 'struct frame_uwnind'. */
  2692. static void
  2693. arm_m_exception_this_id (struct frame_info *this_frame,
  2694. void **this_cache,
  2695. struct frame_id *this_id)
  2696. {
  2697. struct arm_prologue_cache *cache;
  2698. if (*this_cache == NULL)
  2699. *this_cache = arm_m_exception_cache (this_frame);
  2700. cache = (struct arm_prologue_cache *) *this_cache;
  2701. /* Our frame ID for a stub frame is the current SP and LR. */
  2702. *this_id = frame_id_build (cache->prev_sp,
  2703. get_frame_pc (this_frame));
  2704. }
  2705. /* Implementation of function hook 'prev_register' in
  2706. 'struct frame_uwnind'. */
  2707. static struct value *
  2708. arm_m_exception_prev_register (struct frame_info *this_frame,
  2709. void **this_cache,
  2710. int prev_regnum)
  2711. {
  2712. struct arm_prologue_cache *cache;
  2713. if (*this_cache == NULL)
  2714. *this_cache = arm_m_exception_cache (this_frame);
  2715. cache = (struct arm_prologue_cache *) *this_cache;
  2716. /* The value was already reconstructed into PREV_SP. */
  2717. if (prev_regnum == ARM_SP_REGNUM)
  2718. return frame_unwind_got_constant (this_frame, prev_regnum,
  2719. cache->prev_sp);
  2720. return trad_frame_get_prev_register (this_frame, cache->saved_regs,
  2721. prev_regnum);
  2722. }
  2723. /* Implementation of function hook 'sniffer' in
  2724. 'struct frame_uwnind'. */
  2725. static int
  2726. arm_m_exception_unwind_sniffer (const struct frame_unwind *self,
  2727. struct frame_info *this_frame,
  2728. void **this_prologue_cache)
  2729. {
  2730. CORE_ADDR this_pc = get_frame_pc (this_frame);
  2731. /* No need to check is_m; this sniffer is only registered for
  2732. M-profile architectures. */
  2733. /* Check if exception frame returns to a magic PC value. */
  2734. return arm_m_addr_is_magic (this_pc);
  2735. }
  2736. /* Frame unwinder for M-profile exceptions. */
  2737. struct frame_unwind arm_m_exception_unwind =
  2738. {
  2739. "arm m exception",
  2740. SIGTRAMP_FRAME,
  2741. default_frame_unwind_stop_reason,
  2742. arm_m_exception_this_id,
  2743. arm_m_exception_prev_register,
  2744. NULL,
  2745. arm_m_exception_unwind_sniffer
  2746. };
  2747. static CORE_ADDR
  2748. arm_normal_frame_base (struct frame_info *this_frame, void **this_cache)
  2749. {
  2750. struct arm_prologue_cache *cache;
  2751. if (*this_cache == NULL)
  2752. *this_cache = arm_make_prologue_cache (this_frame);
  2753. cache = (struct arm_prologue_cache *) *this_cache;
  2754. return cache->prev_sp - cache->framesize;
  2755. }
  2756. struct frame_base arm_normal_base = {
  2757. &arm_prologue_unwind,
  2758. arm_normal_frame_base,
  2759. arm_normal_frame_base,
  2760. arm_normal_frame_base
  2761. };
  2762. static struct value *
  2763. arm_dwarf2_prev_register (struct frame_info *this_frame, void **this_cache,
  2764. int regnum)
  2765. {
  2766. struct gdbarch * gdbarch = get_frame_arch (this_frame);
  2767. arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  2768. CORE_ADDR lr, cpsr;
  2769. ULONGEST t_bit = arm_psr_thumb_bit (gdbarch);
  2770. switch (regnum)
  2771. {
  2772. case ARM_PC_REGNUM:
  2773. /* The PC is normally copied from the return column, which
  2774. describes saves of LR. However, that version may have an
  2775. extra bit set to indicate Thumb state. The bit is not
  2776. part of the PC. */
  2777. /* Record in the frame whether the return address was signed. */
  2778. if (tdep->have_pacbti)
  2779. {
  2780. CORE_ADDR ra_auth_code
  2781. = frame_unwind_register_unsigned (this_frame,
  2782. tdep->pacbti_pseudo_base);
  2783. if (ra_auth_code != 0)
  2784. set_frame_previous_pc_masked (this_frame);
  2785. }
  2786. lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
  2787. return frame_unwind_got_constant (this_frame, regnum,
  2788. arm_addr_bits_remove (gdbarch, lr));
  2789. case ARM_PS_REGNUM:
  2790. /* Reconstruct the T bit; see arm_prologue_prev_register for details. */
  2791. cpsr = get_frame_register_unsigned (this_frame, regnum);
  2792. lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
  2793. if (IS_THUMB_ADDR (lr))
  2794. cpsr |= t_bit;
  2795. else
  2796. cpsr &= ~t_bit;
  2797. return frame_unwind_got_constant (this_frame, regnum, cpsr);
  2798. default:
  2799. internal_error (__FILE__, __LINE__,
  2800. _("Unexpected register %d"), regnum);
  2801. }
  2802. }
  2803. /* Implement the stack_frame_destroyed_p gdbarch method. */
  2804. static int
  2805. thumb_stack_frame_destroyed_p (struct gdbarch *gdbarch, CORE_ADDR pc)
  2806. {
  2807. enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
  2808. unsigned int insn, insn2;
  2809. int found_return = 0, found_stack_adjust = 0;
  2810. CORE_ADDR func_start, func_end;
  2811. CORE_ADDR scan_pc;
  2812. gdb_byte buf[4];
  2813. if (!find_pc_partial_function (pc, NULL, &func_start, &func_end))
  2814. return 0;
  2815. /* The epilogue is a sequence of instructions along the following lines:
  2816. - add stack frame size to SP or FP
  2817. - [if frame pointer used] restore SP from FP
  2818. - restore registers from SP [may include PC]
  2819. - a return-type instruction [if PC wasn't already restored]
  2820. In a first pass, we scan forward from the current PC and verify the
  2821. instructions we find as compatible with this sequence, ending in a
  2822. return instruction.
  2823. However, this is not sufficient to distinguish indirect function calls
  2824. within a function from indirect tail calls in the epilogue in some cases.
  2825. Therefore, if we didn't already find any SP-changing instruction during
  2826. forward scan, we add a backward scanning heuristic to ensure we actually
  2827. are in the epilogue. */
  2828. scan_pc = pc;
  2829. while (scan_pc < func_end && !found_return)
  2830. {
  2831. if (target_read_memory (scan_pc, buf, 2))
  2832. break;
  2833. scan_pc += 2;
  2834. insn = extract_unsigned_integer (buf, 2, byte_order_for_code);
  2835. if ((insn & 0xff80) == 0x4700) /* bx <Rm> */
  2836. found_return = 1;
  2837. else if (insn == 0x46f7) /* mov pc, lr */
  2838. found_return = 1;
  2839. else if (thumb_instruction_restores_sp (insn))
  2840. {
  2841. if ((insn & 0xff00) == 0xbd00) /* pop <registers, PC> */
  2842. found_return = 1;
  2843. }
  2844. else if (thumb_insn_size (insn) == 4) /* 32-bit Thumb-2 instruction */
  2845. {
  2846. if (target_read_memory (scan_pc, buf, 2))
  2847. break;
  2848. scan_pc += 2;
  2849. insn2 = extract_unsigned_integer (buf, 2, byte_order_for_code);
  2850. if (insn == 0xe8bd) /* ldm.w sp!, <registers> */
  2851. {
  2852. if (insn2 & 0x8000) /* <registers> include PC. */
  2853. found_return = 1;
  2854. }
  2855. else if (insn == 0xf85d /* ldr.w <Rt>, [sp], #4 */
  2856. && (insn2 & 0x0fff) == 0x0b04)
  2857. {
  2858. if ((insn2 & 0xf000) == 0xf000) /* <Rt> is PC. */
  2859. found_return = 1;
  2860. }
  2861. else if ((insn & 0xffbf) == 0xecbd /* vldm sp!, <list> */
  2862. && (insn2 & 0x0e00) == 0x0a00)
  2863. ;
  2864. else
  2865. break;
  2866. }
  2867. else
  2868. break;
  2869. }
  2870. if (!found_return)
  2871. return 0;
  2872. /* Since any instruction in the epilogue sequence, with the possible
  2873. exception of return itself, updates the stack pointer, we need to
  2874. scan backwards for at most one instruction. Try either a 16-bit or
  2875. a 32-bit instruction. This is just a heuristic, so we do not worry
  2876. too much about false positives. */
  2877. if (pc - 4 < func_start)
  2878. return 0;
  2879. if (target_read_memory (pc - 4, buf, 4))
  2880. return 0;
  2881. insn = extract_unsigned_integer (buf, 2, byte_order_for_code);
  2882. insn2 = extract_unsigned_integer (buf + 2, 2, byte_order_for_code);
  2883. if (thumb_instruction_restores_sp (insn2))
  2884. found_stack_adjust = 1;
  2885. else if (insn == 0xe8bd) /* ldm.w sp!, <registers> */
  2886. found_stack_adjust = 1;
  2887. else if (insn == 0xf85d /* ldr.w <Rt>, [sp], #4 */
  2888. && (insn2 & 0x0fff) == 0x0b04)
  2889. found_stack_adjust = 1;
  2890. else if ((insn & 0xffbf) == 0xecbd /* vldm sp!, <list> */
  2891. && (insn2 & 0x0e00) == 0x0a00)
  2892. found_stack_adjust = 1;
  2893. return found_stack_adjust;
  2894. }
  2895. static int
  2896. arm_stack_frame_destroyed_p_1 (struct gdbarch *gdbarch, CORE_ADDR pc)
  2897. {
  2898. enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
  2899. unsigned int insn;
  2900. int found_return;
  2901. CORE_ADDR func_start, func_end;
  2902. if (!find_pc_partial_function (pc, NULL, &func_start, &func_end))
  2903. return 0;
  2904. /* We are in the epilogue if the previous instruction was a stack
  2905. adjustment and the next instruction is a possible return (bx, mov
  2906. pc, or pop). We could have to scan backwards to find the stack
  2907. adjustment, or forwards to find the return, but this is a decent
  2908. approximation. First scan forwards. */
  2909. found_return = 0;
  2910. insn = read_memory_unsigned_integer (pc, 4, byte_order_for_code);
  2911. if (bits (insn, 28, 31) != INST_NV)
  2912. {
  2913. if ((insn & 0x0ffffff0) == 0x012fff10)
  2914. /* BX. */
  2915. found_return = 1;
  2916. else if ((insn & 0x0ffffff0) == 0x01a0f000)
  2917. /* MOV PC. */
  2918. found_return = 1;
  2919. else if ((insn & 0x0fff0000) == 0x08bd0000
  2920. && (insn & 0x0000c000) != 0)
  2921. /* POP (LDMIA), including PC or LR. */
  2922. found_return = 1;
  2923. }
  2924. if (!found_return)
  2925. return 0;
  2926. /* Scan backwards. This is just a heuristic, so do not worry about
  2927. false positives from mode changes. */
  2928. if (pc < func_start + 4)
  2929. return 0;
  2930. insn = read_memory_unsigned_integer (pc - 4, 4, byte_order_for_code);
  2931. if (arm_instruction_restores_sp (insn))
  2932. return 1;
  2933. return 0;
  2934. }
  2935. /* Implement the stack_frame_destroyed_p gdbarch method. */
  2936. static int
  2937. arm_stack_frame_destroyed_p (struct gdbarch *gdbarch, CORE_ADDR pc)
  2938. {
  2939. if (arm_pc_is_thumb (gdbarch, pc))
  2940. return thumb_stack_frame_destroyed_p (gdbarch, pc);
  2941. else
  2942. return arm_stack_frame_destroyed_p_1 (gdbarch, pc);
  2943. }
  2944. /* When arguments must be pushed onto the stack, they go on in reverse
  2945. order. The code below implements a FILO (stack) to do this. */
  2946. struct stack_item
  2947. {
  2948. int len;
  2949. struct stack_item *prev;
  2950. gdb_byte *data;
  2951. };
  2952. static struct stack_item *
  2953. push_stack_item (struct stack_item *prev, const gdb_byte *contents, int len)
  2954. {
  2955. struct stack_item *si;
  2956. si = XNEW (struct stack_item);
  2957. si->data = (gdb_byte *) xmalloc (len);
  2958. si->len = len;
  2959. si->prev = prev;
  2960. memcpy (si->data, contents, len);
  2961. return si;
  2962. }
  2963. static struct stack_item *
  2964. pop_stack_item (struct stack_item *si)
  2965. {
  2966. struct stack_item *dead = si;
  2967. si = si->prev;
  2968. xfree (dead->data);
  2969. xfree (dead);
  2970. return si;
  2971. }
  2972. /* Implement the gdbarch type alignment method, overrides the generic
  2973. alignment algorithm for anything that is arm specific. */
  2974. static ULONGEST
  2975. arm_type_align (gdbarch *gdbarch, struct type *t)
  2976. {
  2977. t = check_typedef (t);
  2978. if (t->code () == TYPE_CODE_ARRAY && t->is_vector ())
  2979. {
  2980. /* Use the natural alignment for vector types (the same for
  2981. scalar type), but the maximum alignment is 64-bit. */
  2982. if (TYPE_LENGTH (t) > 8)
  2983. return 8;
  2984. else
  2985. return TYPE_LENGTH (t);
  2986. }
  2987. /* Allow the common code to calculate the alignment. */
  2988. return 0;
  2989. }
  2990. /* Possible base types for a candidate for passing and returning in
  2991. VFP registers. */
  2992. enum arm_vfp_cprc_base_type
  2993. {
  2994. VFP_CPRC_UNKNOWN,
  2995. VFP_CPRC_SINGLE,
  2996. VFP_CPRC_DOUBLE,
  2997. VFP_CPRC_VEC64,
  2998. VFP_CPRC_VEC128
  2999. };
  3000. /* The length of one element of base type B. */
  3001. static unsigned
  3002. arm_vfp_cprc_unit_length (enum arm_vfp_cprc_base_type b)
  3003. {
  3004. switch (b)
  3005. {
  3006. case VFP_CPRC_SINGLE:
  3007. return 4;
  3008. case VFP_CPRC_DOUBLE:
  3009. return 8;
  3010. case VFP_CPRC_VEC64:
  3011. return 8;
  3012. case VFP_CPRC_VEC128:
  3013. return 16;
  3014. default:
  3015. internal_error (__FILE__, __LINE__, _("Invalid VFP CPRC type: %d."),
  3016. (int) b);
  3017. }
  3018. }
  3019. /* The character ('s', 'd' or 'q') for the type of VFP register used
  3020. for passing base type B. */
  3021. static int
  3022. arm_vfp_cprc_reg_char (enum arm_vfp_cprc_base_type b)
  3023. {
  3024. switch (b)
  3025. {
  3026. case VFP_CPRC_SINGLE:
  3027. return 's';
  3028. case VFP_CPRC_DOUBLE:
  3029. return 'd';
  3030. case VFP_CPRC_VEC64:
  3031. return 'd';
  3032. case VFP_CPRC_VEC128:
  3033. return 'q';
  3034. default:
  3035. internal_error (__FILE__, __LINE__, _("Invalid VFP CPRC type: %d."),
  3036. (int) b);
  3037. }
  3038. }
  3039. /* Determine whether T may be part of a candidate for passing and
  3040. returning in VFP registers, ignoring the limit on the total number
  3041. of components. If *BASE_TYPE is VFP_CPRC_UNKNOWN, set it to the
  3042. classification of the first valid component found; if it is not
  3043. VFP_CPRC_UNKNOWN, all components must have the same classification
  3044. as *BASE_TYPE. If it is found that T contains a type not permitted
  3045. for passing and returning in VFP registers, a type differently
  3046. classified from *BASE_TYPE, or two types differently classified
  3047. from each other, return -1, otherwise return the total number of
  3048. base-type elements found (possibly 0 in an empty structure or
  3049. array). Vector types are not currently supported, matching the
  3050. generic AAPCS support. */
  3051. static int
  3052. arm_vfp_cprc_sub_candidate (struct type *t,
  3053. enum arm_vfp_cprc_base_type *base_type)
  3054. {
  3055. t = check_typedef (t);
  3056. switch (t->code ())
  3057. {
  3058. case TYPE_CODE_FLT:
  3059. switch (TYPE_LENGTH (t))
  3060. {
  3061. case 4:
  3062. if (*base_type == VFP_CPRC_UNKNOWN)
  3063. *base_type = VFP_CPRC_SINGLE;
  3064. else if (*base_type != VFP_CPRC_SINGLE)
  3065. return -1;
  3066. return 1;
  3067. case 8:
  3068. if (*base_type == VFP_CPRC_UNKNOWN)
  3069. *base_type = VFP_CPRC_DOUBLE;
  3070. else if (*base_type != VFP_CPRC_DOUBLE)
  3071. return -1;
  3072. return 1;
  3073. default:
  3074. return -1;
  3075. }
  3076. break;
  3077. case TYPE_CODE_COMPLEX:
  3078. /* Arguments of complex T where T is one of the types float or
  3079. double get treated as if they are implemented as:
  3080. struct complexT
  3081. {
  3082. T real;
  3083. T imag;
  3084. };
  3085. */
  3086. switch (TYPE_LENGTH (t))
  3087. {
  3088. case 8:
  3089. if (*base_type == VFP_CPRC_UNKNOWN)
  3090. *base_type = VFP_CPRC_SINGLE;
  3091. else if (*base_type != VFP_CPRC_SINGLE)
  3092. return -1;
  3093. return 2;
  3094. case 16:
  3095. if (*base_type == VFP_CPRC_UNKNOWN)
  3096. *base_type = VFP_CPRC_DOUBLE;
  3097. else if (*base_type != VFP_CPRC_DOUBLE)
  3098. return -1;
  3099. return 2;
  3100. default:
  3101. return -1;
  3102. }
  3103. break;
  3104. case TYPE_CODE_ARRAY:
  3105. {
  3106. if (t->is_vector ())
  3107. {
  3108. /* A 64-bit or 128-bit containerized vector type are VFP
  3109. CPRCs. */
  3110. switch (TYPE_LENGTH (t))
  3111. {
  3112. case 8:
  3113. if (*base_type == VFP_CPRC_UNKNOWN)
  3114. *base_type = VFP_CPRC_VEC64;
  3115. return 1;
  3116. case 16:
  3117. if (*base_type == VFP_CPRC_UNKNOWN)
  3118. *base_type = VFP_CPRC_VEC128;
  3119. return 1;
  3120. default:
  3121. return -1;
  3122. }
  3123. }
  3124. else
  3125. {
  3126. int count;
  3127. unsigned unitlen;
  3128. count = arm_vfp_cprc_sub_candidate (TYPE_TARGET_TYPE (t),
  3129. base_type);
  3130. if (count == -1)
  3131. return -1;
  3132. if (TYPE_LENGTH (t) == 0)
  3133. {
  3134. gdb_assert (count == 0);
  3135. return 0;
  3136. }
  3137. else if (count == 0)
  3138. return -1;
  3139. unitlen = arm_vfp_cprc_unit_length (*base_type);
  3140. gdb_assert ((TYPE_LENGTH (t) % unitlen) == 0);
  3141. return TYPE_LENGTH (t) / unitlen;
  3142. }
  3143. }
  3144. break;
  3145. case TYPE_CODE_STRUCT:
  3146. {
  3147. int count = 0;
  3148. unsigned unitlen;
  3149. int i;
  3150. for (i = 0; i < t->num_fields (); i++)
  3151. {
  3152. int sub_count = 0;
  3153. if (!field_is_static (&t->field (i)))
  3154. sub_count = arm_vfp_cprc_sub_candidate (t->field (i).type (),
  3155. base_type);
  3156. if (sub_count == -1)
  3157. return -1;
  3158. count += sub_count;
  3159. }
  3160. if (TYPE_LENGTH (t) == 0)
  3161. {
  3162. gdb_assert (count == 0);
  3163. return 0;
  3164. }
  3165. else if (count == 0)
  3166. return -1;
  3167. unitlen = arm_vfp_cprc_unit_length (*base_type);
  3168. if (TYPE_LENGTH (t) != unitlen * count)
  3169. return -1;
  3170. return count;
  3171. }
  3172. case TYPE_CODE_UNION:
  3173. {
  3174. int count = 0;
  3175. unsigned unitlen;
  3176. int i;
  3177. for (i = 0; i < t->num_fields (); i++)
  3178. {
  3179. int sub_count = arm_vfp_cprc_sub_candidate (t->field (i).type (),
  3180. base_type);
  3181. if (sub_count == -1)
  3182. return -1;
  3183. count = (count > sub_count ? count : sub_count);
  3184. }
  3185. if (TYPE_LENGTH (t) == 0)
  3186. {
  3187. gdb_assert (count == 0);
  3188. return 0;
  3189. }
  3190. else if (count == 0)
  3191. return -1;
  3192. unitlen = arm_vfp_cprc_unit_length (*base_type);
  3193. if (TYPE_LENGTH (t) != unitlen * count)
  3194. return -1;
  3195. return count;
  3196. }
  3197. default:
  3198. break;
  3199. }
  3200. return -1;
  3201. }
  3202. /* Determine whether T is a VFP co-processor register candidate (CPRC)
  3203. if passed to or returned from a non-variadic function with the VFP
  3204. ABI in effect. Return 1 if it is, 0 otherwise. If it is, set
  3205. *BASE_TYPE to the base type for T and *COUNT to the number of
  3206. elements of that base type before returning. */
  3207. static int
  3208. arm_vfp_call_candidate (struct type *t, enum arm_vfp_cprc_base_type *base_type,
  3209. int *count)
  3210. {
  3211. enum arm_vfp_cprc_base_type b = VFP_CPRC_UNKNOWN;
  3212. int c = arm_vfp_cprc_sub_candidate (t, &b);
  3213. if (c <= 0 || c > 4)
  3214. return 0;
  3215. *base_type = b;
  3216. *count = c;
  3217. return 1;
  3218. }
  3219. /* Return 1 if the VFP ABI should be used for passing arguments to and
  3220. returning values from a function of type FUNC_TYPE, 0
  3221. otherwise. */
  3222. static int
  3223. arm_vfp_abi_for_function (struct gdbarch *gdbarch, struct type *func_type)
  3224. {
  3225. arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  3226. /* Variadic functions always use the base ABI. Assume that functions
  3227. without debug info are not variadic. */
  3228. if (func_type && check_typedef (func_type)->has_varargs ())
  3229. return 0;
  3230. /* The VFP ABI is only supported as a variant of AAPCS. */
  3231. if (tdep->arm_abi != ARM_ABI_AAPCS)
  3232. return 0;
  3233. return tdep->fp_model == ARM_FLOAT_VFP;
  3234. }
  3235. /* We currently only support passing parameters in integer registers, which
  3236. conforms with GCC's default model, and VFP argument passing following
  3237. the VFP variant of AAPCS. Several other variants exist and
  3238. we should probably support some of them based on the selected ABI. */
  3239. static CORE_ADDR
  3240. arm_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
  3241. struct regcache *regcache, CORE_ADDR bp_addr, int nargs,
  3242. struct value **args, CORE_ADDR sp,
  3243. function_call_return_method return_method,
  3244. CORE_ADDR struct_addr)
  3245. {
  3246. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  3247. int argnum;
  3248. int argreg;
  3249. int nstack;
  3250. struct stack_item *si = NULL;
  3251. int use_vfp_abi;
  3252. struct type *ftype;
  3253. unsigned vfp_regs_free = (1 << 16) - 1;
  3254. arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  3255. /* Determine the type of this function and whether the VFP ABI
  3256. applies. */
  3257. ftype = check_typedef (value_type (function));
  3258. if (ftype->code () == TYPE_CODE_PTR)
  3259. ftype = check_typedef (TYPE_TARGET_TYPE (ftype));
  3260. use_vfp_abi = arm_vfp_abi_for_function (gdbarch, ftype);
  3261. /* Set the return address. For the ARM, the return breakpoint is
  3262. always at BP_ADDR. */
  3263. if (arm_pc_is_thumb (gdbarch, bp_addr))
  3264. bp_addr |= 1;
  3265. regcache_cooked_write_unsigned (regcache, ARM_LR_REGNUM, bp_addr);
  3266. /* Walk through the list of args and determine how large a temporary
  3267. stack is required. Need to take care here as structs may be
  3268. passed on the stack, and we have to push them. */
  3269. nstack = 0;
  3270. argreg = ARM_A1_REGNUM;
  3271. nstack = 0;
  3272. /* The struct_return pointer occupies the first parameter
  3273. passing register. */
  3274. if (return_method == return_method_struct)
  3275. {
  3276. arm_debug_printf ("struct return in %s = %s",
  3277. gdbarch_register_name (gdbarch, argreg),
  3278. paddress (gdbarch, struct_addr));
  3279. regcache_cooked_write_unsigned (regcache, argreg, struct_addr);
  3280. argreg++;
  3281. }
  3282. for (argnum = 0; argnum < nargs; argnum++)
  3283. {
  3284. int len;
  3285. struct type *arg_type;
  3286. struct type *target_type;
  3287. enum type_code typecode;
  3288. const bfd_byte *val;
  3289. int align;
  3290. enum arm_vfp_cprc_base_type vfp_base_type;
  3291. int vfp_base_count;
  3292. int may_use_core_reg = 1;
  3293. arg_type = check_typedef (value_type (args[argnum]));
  3294. len = TYPE_LENGTH (arg_type);
  3295. target_type = TYPE_TARGET_TYPE (arg_type);
  3296. typecode = arg_type->code ();
  3297. val = value_contents (args[argnum]).data ();
  3298. align = type_align (arg_type);
  3299. /* Round alignment up to a whole number of words. */
  3300. align = (align + ARM_INT_REGISTER_SIZE - 1)
  3301. & ~(ARM_INT_REGISTER_SIZE - 1);
  3302. /* Different ABIs have different maximum alignments. */
  3303. if (tdep->arm_abi == ARM_ABI_APCS)
  3304. {
  3305. /* The APCS ABI only requires word alignment. */
  3306. align = ARM_INT_REGISTER_SIZE;
  3307. }
  3308. else
  3309. {
  3310. /* The AAPCS requires at most doubleword alignment. */
  3311. if (align > ARM_INT_REGISTER_SIZE * 2)
  3312. align = ARM_INT_REGISTER_SIZE * 2;
  3313. }
  3314. if (use_vfp_abi
  3315. && arm_vfp_call_candidate (arg_type, &vfp_base_type,
  3316. &vfp_base_count))
  3317. {
  3318. int regno;
  3319. int unit_length;
  3320. int shift;
  3321. unsigned mask;
  3322. /* Because this is a CPRC it cannot go in a core register or
  3323. cause a core register to be skipped for alignment.
  3324. Either it goes in VFP registers and the rest of this loop
  3325. iteration is skipped for this argument, or it goes on the
  3326. stack (and the stack alignment code is correct for this
  3327. case). */
  3328. may_use_core_reg = 0;
  3329. unit_length = arm_vfp_cprc_unit_length (vfp_base_type);
  3330. shift = unit_length / 4;
  3331. mask = (1 << (shift * vfp_base_count)) - 1;
  3332. for (regno = 0; regno < 16; regno += shift)
  3333. if (((vfp_regs_free >> regno) & mask) == mask)
  3334. break;
  3335. if (regno < 16)
  3336. {
  3337. int reg_char;
  3338. int reg_scaled;
  3339. int i;
  3340. vfp_regs_free &= ~(mask << regno);
  3341. reg_scaled = regno / shift;
  3342. reg_char = arm_vfp_cprc_reg_char (vfp_base_type);
  3343. for (i = 0; i < vfp_base_count; i++)
  3344. {
  3345. char name_buf[4];
  3346. int regnum;
  3347. if (reg_char == 'q')
  3348. arm_neon_quad_write (gdbarch, regcache, reg_scaled + i,
  3349. val + i * unit_length);
  3350. else
  3351. {
  3352. xsnprintf (name_buf, sizeof (name_buf), "%c%d",
  3353. reg_char, reg_scaled + i);
  3354. regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
  3355. strlen (name_buf));
  3356. regcache->cooked_write (regnum, val + i * unit_length);
  3357. }
  3358. }
  3359. continue;
  3360. }
  3361. else
  3362. {
  3363. /* This CPRC could not go in VFP registers, so all VFP
  3364. registers are now marked as used. */
  3365. vfp_regs_free = 0;
  3366. }
  3367. }
  3368. /* Push stack padding for doubleword alignment. */
  3369. if (nstack & (align - 1))
  3370. {
  3371. si = push_stack_item (si, val, ARM_INT_REGISTER_SIZE);
  3372. nstack += ARM_INT_REGISTER_SIZE;
  3373. }
  3374. /* Doubleword aligned quantities must go in even register pairs. */
  3375. if (may_use_core_reg
  3376. && argreg <= ARM_LAST_ARG_REGNUM
  3377. && align > ARM_INT_REGISTER_SIZE
  3378. && argreg & 1)
  3379. argreg++;
  3380. /* If the argument is a pointer to a function, and it is a
  3381. Thumb function, create a LOCAL copy of the value and set
  3382. the THUMB bit in it. */
  3383. if (TYPE_CODE_PTR == typecode
  3384. && target_type != NULL
  3385. && TYPE_CODE_FUNC == check_typedef (target_type)->code ())
  3386. {
  3387. CORE_ADDR regval = extract_unsigned_integer (val, len, byte_order);
  3388. if (arm_pc_is_thumb (gdbarch, regval))
  3389. {
  3390. bfd_byte *copy = (bfd_byte *) alloca (len);
  3391. store_unsigned_integer (copy, len, byte_order,
  3392. MAKE_THUMB_ADDR (regval));
  3393. val = copy;
  3394. }
  3395. }
  3396. /* Copy the argument to general registers or the stack in
  3397. register-sized pieces. Large arguments are split between
  3398. registers and stack. */
  3399. while (len > 0)
  3400. {
  3401. int partial_len = len < ARM_INT_REGISTER_SIZE
  3402. ? len : ARM_INT_REGISTER_SIZE;
  3403. CORE_ADDR regval
  3404. = extract_unsigned_integer (val, partial_len, byte_order);
  3405. if (may_use_core_reg && argreg <= ARM_LAST_ARG_REGNUM)
  3406. {
  3407. /* The argument is being passed in a general purpose
  3408. register. */
  3409. if (byte_order == BFD_ENDIAN_BIG)
  3410. regval <<= (ARM_INT_REGISTER_SIZE - partial_len) * 8;
  3411. arm_debug_printf ("arg %d in %s = 0x%s", argnum,
  3412. gdbarch_register_name (gdbarch, argreg),
  3413. phex (regval, ARM_INT_REGISTER_SIZE));
  3414. regcache_cooked_write_unsigned (regcache, argreg, regval);
  3415. argreg++;
  3416. }
  3417. else
  3418. {
  3419. gdb_byte buf[ARM_INT_REGISTER_SIZE];
  3420. memset (buf, 0, sizeof (buf));
  3421. store_unsigned_integer (buf, partial_len, byte_order, regval);
  3422. /* Push the arguments onto the stack. */
  3423. arm_debug_printf ("arg %d @ sp + %d", argnum, nstack);
  3424. si = push_stack_item (si, buf, ARM_INT_REGISTER_SIZE);
  3425. nstack += ARM_INT_REGISTER_SIZE;
  3426. }
  3427. len -= partial_len;
  3428. val += partial_len;
  3429. }
  3430. }
  3431. /* If we have an odd number of words to push, then decrement the stack
  3432. by one word now, so first stack argument will be dword aligned. */
  3433. if (nstack & 4)
  3434. sp -= 4;
  3435. while (si)
  3436. {
  3437. sp -= si->len;
  3438. write_memory (sp, si->data, si->len);
  3439. si = pop_stack_item (si);
  3440. }
  3441. /* Finally, update teh SP register. */
  3442. regcache_cooked_write_unsigned (regcache, ARM_SP_REGNUM, sp);
  3443. return sp;
  3444. }
  3445. /* Always align the frame to an 8-byte boundary. This is required on
  3446. some platforms and harmless on the rest. */
  3447. static CORE_ADDR
  3448. arm_frame_align (struct gdbarch *gdbarch, CORE_ADDR sp)
  3449. {
  3450. /* Align the stack to eight bytes. */
  3451. return sp & ~ (CORE_ADDR) 7;
  3452. }
  3453. static void
  3454. print_fpu_flags (struct ui_file *file, int flags)
  3455. {
  3456. if (flags & (1 << 0))
  3457. gdb_puts ("IVO ", file);
  3458. if (flags & (1 << 1))
  3459. gdb_puts ("DVZ ", file);
  3460. if (flags & (1 << 2))
  3461. gdb_puts ("OFL ", file);
  3462. if (flags & (1 << 3))
  3463. gdb_puts ("UFL ", file);
  3464. if (flags & (1 << 4))
  3465. gdb_puts ("INX ", file);
  3466. gdb_putc ('\n', file);
  3467. }
  3468. /* Print interesting information about the floating point processor
  3469. (if present) or emulator. */
  3470. static void
  3471. arm_print_float_info (struct gdbarch *gdbarch, struct ui_file *file,
  3472. struct frame_info *frame, const char *args)
  3473. {
  3474. unsigned long status = get_frame_register_unsigned (frame, ARM_FPS_REGNUM);
  3475. int type;
  3476. type = (status >> 24) & 127;
  3477. if (status & (1 << 31))
  3478. gdb_printf (file, _("Hardware FPU type %d\n"), type);
  3479. else
  3480. gdb_printf (file, _("Software FPU type %d\n"), type);
  3481. /* i18n: [floating point unit] mask */
  3482. gdb_puts (_("mask: "), file);
  3483. print_fpu_flags (file, status >> 16);
  3484. /* i18n: [floating point unit] flags */
  3485. gdb_puts (_("flags: "), file);
  3486. print_fpu_flags (file, status);
  3487. }
  3488. /* Construct the ARM extended floating point type. */
  3489. static struct type *
  3490. arm_ext_type (struct gdbarch *gdbarch)
  3491. {
  3492. arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  3493. if (!tdep->arm_ext_type)
  3494. tdep->arm_ext_type
  3495. = arch_float_type (gdbarch, -1, "builtin_type_arm_ext",
  3496. floatformats_arm_ext);
  3497. return tdep->arm_ext_type;
  3498. }
  3499. static struct type *
  3500. arm_neon_double_type (struct gdbarch *gdbarch)
  3501. {
  3502. arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  3503. if (tdep->neon_double_type == NULL)
  3504. {
  3505. struct type *t, *elem;
  3506. t = arch_composite_type (gdbarch, "__gdb_builtin_type_neon_d",
  3507. TYPE_CODE_UNION);
  3508. elem = builtin_type (gdbarch)->builtin_uint8;
  3509. append_composite_type_field (t, "u8", init_vector_type (elem, 8));
  3510. elem = builtin_type (gdbarch)->builtin_uint16;
  3511. append_composite_type_field (t, "u16", init_vector_type (elem, 4));
  3512. elem = builtin_type (gdbarch)->builtin_uint32;
  3513. append_composite_type_field (t, "u32", init_vector_type (elem, 2));
  3514. elem = builtin_type (gdbarch)->builtin_uint64;
  3515. append_composite_type_field (t, "u64", elem);
  3516. elem = builtin_type (gdbarch)->builtin_float;
  3517. append_composite_type_field (t, "f32", init_vector_type (elem, 2));
  3518. elem = builtin_type (gdbarch)->builtin_double;
  3519. append_composite_type_field (t, "f64", elem);
  3520. t->set_is_vector (true);
  3521. t->set_name ("neon_d");
  3522. tdep->neon_double_type = t;
  3523. }
  3524. return tdep->neon_double_type;
  3525. }
  3526. /* FIXME: The vector types are not correctly ordered on big-endian
  3527. targets. Just as s0 is the low bits of d0, d0[0] is also the low
  3528. bits of d0 - regardless of what unit size is being held in d0. So
  3529. the offset of the first uint8 in d0 is 7, but the offset of the
  3530. first float is 4. This code works as-is for little-endian
  3531. targets. */
  3532. static struct type *
  3533. arm_neon_quad_type (struct gdbarch *gdbarch)
  3534. {
  3535. arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  3536. if (tdep->neon_quad_type == NULL)
  3537. {
  3538. struct type *t, *elem;
  3539. t = arch_composite_type (gdbarch, "__gdb_builtin_type_neon_q",
  3540. TYPE_CODE_UNION);
  3541. elem = builtin_type (gdbarch)->builtin_uint8;
  3542. append_composite_type_field (t, "u8", init_vector_type (elem, 16));
  3543. elem = builtin_type (gdbarch)->builtin_uint16;
  3544. append_composite_type_field (t, "u16", init_vector_type (elem, 8));
  3545. elem = builtin_type (gdbarch)->builtin_uint32;
  3546. append_composite_type_field (t, "u32", init_vector_type (elem, 4));
  3547. elem = builtin_type (gdbarch)->builtin_uint64;
  3548. append_composite_type_field (t, "u64", init_vector_type (elem, 2));
  3549. elem = builtin_type (gdbarch)->builtin_float;
  3550. append_composite_type_field (t, "f32", init_vector_type (elem, 4));
  3551. elem = builtin_type (gdbarch)->builtin_double;
  3552. append_composite_type_field (t, "f64", init_vector_type (elem, 2));
  3553. t->set_is_vector (true);
  3554. t->set_name ("neon_q");
  3555. tdep->neon_quad_type = t;
  3556. }
  3557. return tdep->neon_quad_type;
  3558. }
  3559. /* Return true if REGNUM is a Q pseudo register. Return false
  3560. otherwise.
  3561. REGNUM is the raw register number and not a pseudo-relative register
  3562. number. */
  3563. static bool
  3564. is_q_pseudo (struct gdbarch *gdbarch, int regnum)
  3565. {
  3566. arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  3567. /* Q pseudo registers are available for both NEON (Q0~Q15) and
  3568. MVE (Q0~Q7) features. */
  3569. if (tdep->have_q_pseudos
  3570. && regnum >= tdep->q_pseudo_base
  3571. && regnum < (tdep->q_pseudo_base + tdep->q_pseudo_count))
  3572. return true;
  3573. return false;
  3574. }
  3575. /* Return true if REGNUM is a VFP S pseudo register. Return false
  3576. otherwise.
  3577. REGNUM is the raw register number and not a pseudo-relative register
  3578. number. */
  3579. static bool
  3580. is_s_pseudo (struct gdbarch *gdbarch, int regnum)
  3581. {
  3582. arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  3583. if (tdep->have_s_pseudos
  3584. && regnum >= tdep->s_pseudo_base
  3585. && regnum < (tdep->s_pseudo_base + tdep->s_pseudo_count))
  3586. return true;
  3587. return false;
  3588. }
  3589. /* Return true if REGNUM is a MVE pseudo register (P0). Return false
  3590. otherwise.
  3591. REGNUM is the raw register number and not a pseudo-relative register
  3592. number. */
  3593. static bool
  3594. is_mve_pseudo (struct gdbarch *gdbarch, int regnum)
  3595. {
  3596. arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  3597. if (tdep->have_mve
  3598. && regnum >= tdep->mve_pseudo_base
  3599. && regnum < tdep->mve_pseudo_base + tdep->mve_pseudo_count)
  3600. return true;
  3601. return false;
  3602. }
  3603. /* Return true if REGNUM is a PACBTI pseudo register (ra_auth_code). Return
  3604. false otherwise.
  3605. REGNUM is the raw register number and not a pseudo-relative register
  3606. number. */
  3607. static bool
  3608. is_pacbti_pseudo (struct gdbarch *gdbarch, int regnum)
  3609. {
  3610. arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  3611. if (tdep->have_pacbti
  3612. && regnum >= tdep->pacbti_pseudo_base
  3613. && regnum < tdep->pacbti_pseudo_base + tdep->pacbti_pseudo_count)
  3614. return true;
  3615. return false;
  3616. }
  3617. /* Return the GDB type object for the "standard" data type of data in
  3618. register N. */
  3619. static struct type *
  3620. arm_register_type (struct gdbarch *gdbarch, int regnum)
  3621. {
  3622. arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  3623. if (is_s_pseudo (gdbarch, regnum))
  3624. return builtin_type (gdbarch)->builtin_float;
  3625. if (is_q_pseudo (gdbarch, regnum))
  3626. return arm_neon_quad_type (gdbarch);
  3627. if (is_mve_pseudo (gdbarch, regnum))
  3628. return builtin_type (gdbarch)->builtin_int16;
  3629. if (is_pacbti_pseudo (gdbarch, regnum))
  3630. return builtin_type (gdbarch)->builtin_uint32;
  3631. /* If the target description has register information, we are only
  3632. in this function so that we can override the types of
  3633. double-precision registers for NEON. */
  3634. if (tdesc_has_registers (gdbarch_target_desc (gdbarch)))
  3635. {
  3636. struct type *t = tdesc_register_type (gdbarch, regnum);
  3637. if (regnum >= ARM_D0_REGNUM && regnum < ARM_D0_REGNUM + 32
  3638. && t->code () == TYPE_CODE_FLT
  3639. && tdep->have_neon)
  3640. return arm_neon_double_type (gdbarch);
  3641. else
  3642. return t;
  3643. }
  3644. if (regnum >= ARM_F0_REGNUM && regnum < ARM_F0_REGNUM + NUM_FREGS)
  3645. {
  3646. if (!tdep->have_fpa_registers)
  3647. return builtin_type (gdbarch)->builtin_void;
  3648. return arm_ext_type (gdbarch);
  3649. }
  3650. else if (regnum == ARM_SP_REGNUM)
  3651. return builtin_type (gdbarch)->builtin_data_ptr;
  3652. else if (regnum == ARM_PC_REGNUM)
  3653. return builtin_type (gdbarch)->builtin_func_ptr;
  3654. else if (regnum >= ARRAY_SIZE (arm_register_names))
  3655. /* These registers are only supported on targets which supply
  3656. an XML description. */
  3657. return builtin_type (gdbarch)->builtin_int0;
  3658. else
  3659. return builtin_type (gdbarch)->builtin_uint32;
  3660. }
  3661. /* Map a DWARF register REGNUM onto the appropriate GDB register
  3662. number. */
  3663. static int
  3664. arm_dwarf_reg_to_regnum (struct gdbarch *gdbarch, int reg)
  3665. {
  3666. /* Core integer regs. */
  3667. if (reg >= 0 && reg <= 15)
  3668. return reg;
  3669. /* Legacy FPA encoding. These were once used in a way which
  3670. overlapped with VFP register numbering, so their use is
  3671. discouraged, but GDB doesn't support the ARM toolchain
  3672. which used them for VFP. */
  3673. if (reg >= 16 && reg <= 23)
  3674. return ARM_F0_REGNUM + reg - 16;
  3675. /* New assignments for the FPA registers. */
  3676. if (reg >= 96 && reg <= 103)
  3677. return ARM_F0_REGNUM + reg - 96;
  3678. /* WMMX register assignments. */
  3679. if (reg >= 104 && reg <= 111)
  3680. return ARM_WCGR0_REGNUM + reg - 104;
  3681. if (reg >= 112 && reg <= 127)
  3682. return ARM_WR0_REGNUM + reg - 112;
  3683. /* PACBTI register containing the Pointer Authentication Code. */
  3684. if (reg == ARM_DWARF_RA_AUTH_CODE)
  3685. {
  3686. arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  3687. if (tdep->have_pacbti)
  3688. return tdep->pacbti_pseudo_base;
  3689. return -1;
  3690. }
  3691. if (reg >= 192 && reg <= 199)
  3692. return ARM_WC0_REGNUM + reg - 192;
  3693. /* VFP v2 registers. A double precision value is actually
  3694. in d1 rather than s2, but the ABI only defines numbering
  3695. for the single precision registers. This will "just work"
  3696. in GDB for little endian targets (we'll read eight bytes,
  3697. starting in s0 and then progressing to s1), but will be
  3698. reversed on big endian targets with VFP. This won't
  3699. be a problem for the new Neon quad registers; you're supposed
  3700. to use DW_OP_piece for those. */
  3701. if (reg >= 64 && reg <= 95)
  3702. {
  3703. char name_buf[4];
  3704. xsnprintf (name_buf, sizeof (name_buf), "s%d", reg - 64);
  3705. return user_reg_map_name_to_regnum (gdbarch, name_buf,
  3706. strlen (name_buf));
  3707. }
  3708. /* VFP v3 / Neon registers. This range is also used for VFP v2
  3709. registers, except that it now describes d0 instead of s0. */
  3710. if (reg >= 256 && reg <= 287)
  3711. {
  3712. char name_buf[4];
  3713. xsnprintf (name_buf, sizeof (name_buf), "d%d", reg - 256);
  3714. return user_reg_map_name_to_regnum (gdbarch, name_buf,
  3715. strlen (name_buf));
  3716. }
  3717. return -1;
  3718. }
  3719. /* Map GDB internal REGNUM onto the Arm simulator register numbers. */
  3720. static int
  3721. arm_register_sim_regno (struct gdbarch *gdbarch, int regnum)
  3722. {
  3723. int reg = regnum;
  3724. gdb_assert (reg >= 0 && reg < gdbarch_num_regs (gdbarch));
  3725. if (regnum >= ARM_WR0_REGNUM && regnum <= ARM_WR15_REGNUM)
  3726. return regnum - ARM_WR0_REGNUM + SIM_ARM_IWMMXT_COP0R0_REGNUM;
  3727. if (regnum >= ARM_WC0_REGNUM && regnum <= ARM_WC7_REGNUM)
  3728. return regnum - ARM_WC0_REGNUM + SIM_ARM_IWMMXT_COP1R0_REGNUM;
  3729. if (regnum >= ARM_WCGR0_REGNUM && regnum <= ARM_WCGR7_REGNUM)
  3730. return regnum - ARM_WCGR0_REGNUM + SIM_ARM_IWMMXT_COP1R8_REGNUM;
  3731. if (reg < NUM_GREGS)
  3732. return SIM_ARM_R0_REGNUM + reg;
  3733. reg -= NUM_GREGS;
  3734. if (reg < NUM_FREGS)
  3735. return SIM_ARM_FP0_REGNUM + reg;
  3736. reg -= NUM_FREGS;
  3737. if (reg < NUM_SREGS)
  3738. return SIM_ARM_FPS_REGNUM + reg;
  3739. reg -= NUM_SREGS;
  3740. internal_error (__FILE__, __LINE__, _("Bad REGNUM %d"), regnum);
  3741. }
  3742. static const unsigned char op_lit0 = DW_OP_lit0;
  3743. static void
  3744. arm_dwarf2_frame_init_reg (struct gdbarch *gdbarch, int regnum,
  3745. struct dwarf2_frame_state_reg *reg,
  3746. struct frame_info *this_frame)
  3747. {
  3748. if (is_pacbti_pseudo (gdbarch, regnum))
  3749. {
  3750. /* Initialize RA_AUTH_CODE to zero. */
  3751. reg->how = DWARF2_FRAME_REG_SAVED_VAL_EXP;
  3752. reg->loc.exp.start = &op_lit0;
  3753. reg->loc.exp.len = 1;
  3754. return;
  3755. }
  3756. switch (regnum)
  3757. {
  3758. case ARM_PC_REGNUM:
  3759. case ARM_PS_REGNUM:
  3760. reg->how = DWARF2_FRAME_REG_FN;
  3761. reg->loc.fn = arm_dwarf2_prev_register;
  3762. break;
  3763. case ARM_SP_REGNUM:
  3764. reg->how = DWARF2_FRAME_REG_CFA;
  3765. break;
  3766. }
  3767. }
  3768. /* Given BUF, which is OLD_LEN bytes ending at ENDADDR, expand
  3769. the buffer to be NEW_LEN bytes ending at ENDADDR. Return
  3770. NULL if an error occurs. BUF is freed. */
  3771. static gdb_byte *
  3772. extend_buffer_earlier (gdb_byte *buf, CORE_ADDR endaddr,
  3773. int old_len, int new_len)
  3774. {
  3775. gdb_byte *new_buf;
  3776. int bytes_to_read = new_len - old_len;
  3777. new_buf = (gdb_byte *) xmalloc (new_len);
  3778. memcpy (new_buf + bytes_to_read, buf, old_len);
  3779. xfree (buf);
  3780. if (target_read_code (endaddr - new_len, new_buf, bytes_to_read) != 0)
  3781. {
  3782. xfree (new_buf);
  3783. return NULL;
  3784. }
  3785. return new_buf;
  3786. }
  3787. /* An IT block is at most the 2-byte IT instruction followed by
  3788. four 4-byte instructions. The furthest back we must search to
  3789. find an IT block that affects the current instruction is thus
  3790. 2 + 3 * 4 == 14 bytes. */
  3791. #define MAX_IT_BLOCK_PREFIX 14
  3792. /* Use a quick scan if there are more than this many bytes of
  3793. code. */
  3794. #define IT_SCAN_THRESHOLD 32
  3795. /* Adjust a breakpoint's address to move breakpoints out of IT blocks.
  3796. A breakpoint in an IT block may not be hit, depending on the
  3797. condition flags. */
  3798. static CORE_ADDR
  3799. arm_adjust_breakpoint_address (struct gdbarch *gdbarch, CORE_ADDR bpaddr)
  3800. {
  3801. gdb_byte *buf;
  3802. char map_type;
  3803. CORE_ADDR boundary, func_start;
  3804. int buf_len;
  3805. enum bfd_endian order = gdbarch_byte_order_for_code (gdbarch);
  3806. int i, any, last_it, last_it_count;
  3807. arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  3808. /* If we are using BKPT breakpoints, none of this is necessary. */
  3809. if (tdep->thumb2_breakpoint == NULL)
  3810. return bpaddr;
  3811. /* ARM mode does not have this problem. */
  3812. if (!arm_pc_is_thumb (gdbarch, bpaddr))
  3813. return bpaddr;
  3814. /* We are setting a breakpoint in Thumb code that could potentially
  3815. contain an IT block. The first step is to find how much Thumb
  3816. code there is; we do not need to read outside of known Thumb
  3817. sequences. */
  3818. map_type = arm_find_mapping_symbol (bpaddr, &boundary);
  3819. if (map_type == 0)
  3820. /* Thumb-2 code must have mapping symbols to have a chance. */
  3821. return bpaddr;
  3822. bpaddr = gdbarch_addr_bits_remove (gdbarch, bpaddr);
  3823. if (find_pc_partial_function (bpaddr, NULL, &func_start, NULL)
  3824. && func_start > boundary)
  3825. boundary = func_start;
  3826. /* Search for a candidate IT instruction. We have to do some fancy
  3827. footwork to distinguish a real IT instruction from the second
  3828. half of a 32-bit instruction, but there is no need for that if
  3829. there's no candidate. */
  3830. buf_len = std::min (bpaddr - boundary, (CORE_ADDR) MAX_IT_BLOCK_PREFIX);
  3831. if (buf_len == 0)
  3832. /* No room for an IT instruction. */
  3833. return bpaddr;
  3834. buf = (gdb_byte *) xmalloc (buf_len);
  3835. if (target_read_code (bpaddr - buf_len, buf, buf_len) != 0)
  3836. return bpaddr;
  3837. any = 0;
  3838. for (i = 0; i < buf_len; i += 2)
  3839. {
  3840. unsigned short inst1 = extract_unsigned_integer (&buf[i], 2, order);
  3841. if ((inst1 & 0xff00) == 0xbf00 && (inst1 & 0x000f) != 0)
  3842. {
  3843. any = 1;
  3844. break;
  3845. }
  3846. }
  3847. if (any == 0)
  3848. {
  3849. xfree (buf);
  3850. return bpaddr;
  3851. }
  3852. /* OK, the code bytes before this instruction contain at least one
  3853. halfword which resembles an IT instruction. We know that it's
  3854. Thumb code, but there are still two possibilities. Either the
  3855. halfword really is an IT instruction, or it is the second half of
  3856. a 32-bit Thumb instruction. The only way we can tell is to
  3857. scan forwards from a known instruction boundary. */
  3858. if (bpaddr - boundary > IT_SCAN_THRESHOLD)
  3859. {
  3860. int definite;
  3861. /* There's a lot of code before this instruction. Start with an
  3862. optimistic search; it's easy to recognize halfwords that can
  3863. not be the start of a 32-bit instruction, and use that to
  3864. lock on to the instruction boundaries. */
  3865. buf = extend_buffer_earlier (buf, bpaddr, buf_len, IT_SCAN_THRESHOLD);
  3866. if (buf == NULL)
  3867. return bpaddr;
  3868. buf_len = IT_SCAN_THRESHOLD;
  3869. definite = 0;
  3870. for (i = 0; i < buf_len - sizeof (buf) && ! definite; i += 2)
  3871. {
  3872. unsigned short inst1 = extract_unsigned_integer (&buf[i], 2, order);
  3873. if (thumb_insn_size (inst1) == 2)
  3874. {
  3875. definite = 1;
  3876. break;
  3877. }
  3878. }
  3879. /* At this point, if DEFINITE, BUF[I] is the first place we
  3880. are sure that we know the instruction boundaries, and it is far
  3881. enough from BPADDR that we could not miss an IT instruction
  3882. affecting BPADDR. If ! DEFINITE, give up - start from a
  3883. known boundary. */
  3884. if (! definite)
  3885. {
  3886. buf = extend_buffer_earlier (buf, bpaddr, buf_len,
  3887. bpaddr - boundary);
  3888. if (buf == NULL)
  3889. return bpaddr;
  3890. buf_len = bpaddr - boundary;
  3891. i = 0;
  3892. }
  3893. }
  3894. else
  3895. {
  3896. buf = extend_buffer_earlier (buf, bpaddr, buf_len, bpaddr - boundary);
  3897. if (buf == NULL)
  3898. return bpaddr;
  3899. buf_len = bpaddr - boundary;
  3900. i = 0;
  3901. }
  3902. /* Scan forwards. Find the last IT instruction before BPADDR. */
  3903. last_it = -1;
  3904. last_it_count = 0;
  3905. while (i < buf_len)
  3906. {
  3907. unsigned short inst1 = extract_unsigned_integer (&buf[i], 2, order);
  3908. last_it_count--;
  3909. if ((inst1 & 0xff00) == 0xbf00 && (inst1 & 0x000f) != 0)
  3910. {
  3911. last_it = i;
  3912. if (inst1 & 0x0001)
  3913. last_it_count = 4;
  3914. else if (inst1 & 0x0002)
  3915. last_it_count = 3;
  3916. else if (inst1 & 0x0004)
  3917. last_it_count = 2;
  3918. else
  3919. last_it_count = 1;
  3920. }
  3921. i += thumb_insn_size (inst1);
  3922. }
  3923. xfree (buf);
  3924. if (last_it == -1)
  3925. /* There wasn't really an IT instruction after all. */
  3926. return bpaddr;
  3927. if (last_it_count < 1)
  3928. /* It was too far away. */
  3929. return bpaddr;
  3930. /* This really is a trouble spot. Move the breakpoint to the IT
  3931. instruction. */
  3932. return bpaddr - buf_len + last_it;
  3933. }
  3934. /* ARM displaced stepping support.
  3935. Generally ARM displaced stepping works as follows:
  3936. 1. When an instruction is to be single-stepped, it is first decoded by
  3937. arm_process_displaced_insn. Depending on the type of instruction, it is
  3938. then copied to a scratch location, possibly in a modified form. The
  3939. copy_* set of functions performs such modification, as necessary. A
  3940. breakpoint is placed after the modified instruction in the scratch space
  3941. to return control to GDB. Note in particular that instructions which
  3942. modify the PC will no longer do so after modification.
  3943. 2. The instruction is single-stepped, by setting the PC to the scratch
  3944. location address, and resuming. Control returns to GDB when the
  3945. breakpoint is hit.
  3946. 3. A cleanup function (cleanup_*) is called corresponding to the copy_*
  3947. function used for the current instruction. This function's job is to
  3948. put the CPU/memory state back to what it would have been if the
  3949. instruction had been executed unmodified in its original location. */
  3950. /* NOP instruction (mov r0, r0). */
  3951. #define ARM_NOP 0xe1a00000
  3952. #define THUMB_NOP 0x4600
  3953. /* Helper for register reads for displaced stepping. In particular, this
  3954. returns the PC as it would be seen by the instruction at its original
  3955. location. */
  3956. ULONGEST
  3957. displaced_read_reg (regcache *regs, arm_displaced_step_copy_insn_closure *dsc,
  3958. int regno)
  3959. {
  3960. ULONGEST ret;
  3961. CORE_ADDR from = dsc->insn_addr;
  3962. if (regno == ARM_PC_REGNUM)
  3963. {
  3964. /* Compute pipeline offset:
  3965. - When executing an ARM instruction, PC reads as the address of the
  3966. current instruction plus 8.
  3967. - When executing a Thumb instruction, PC reads as the address of the
  3968. current instruction plus 4. */
  3969. if (!dsc->is_thumb)
  3970. from += 8;
  3971. else
  3972. from += 4;
  3973. displaced_debug_printf ("read pc value %.8lx",
  3974. (unsigned long) from);
  3975. return (ULONGEST) from;
  3976. }
  3977. else
  3978. {
  3979. regcache_cooked_read_unsigned (regs, regno, &ret);
  3980. displaced_debug_printf ("read r%d value %.8lx",
  3981. regno, (unsigned long) ret);
  3982. return ret;
  3983. }
  3984. }
  3985. static int
  3986. displaced_in_arm_mode (struct regcache *regs)
  3987. {
  3988. ULONGEST ps;
  3989. ULONGEST t_bit = arm_psr_thumb_bit (regs->arch ());
  3990. regcache_cooked_read_unsigned (regs, ARM_PS_REGNUM, &ps);
  3991. return (ps & t_bit) == 0;
  3992. }
  3993. /* Write to the PC as from a branch instruction. */
  3994. static void
  3995. branch_write_pc (regcache *regs, arm_displaced_step_copy_insn_closure *dsc,
  3996. ULONGEST val)
  3997. {
  3998. if (!dsc->is_thumb)
  3999. /* Note: If bits 0/1 are set, this branch would be unpredictable for
  4000. architecture versions < 6. */
  4001. regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM,
  4002. val & ~(ULONGEST) 0x3);
  4003. else
  4004. regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM,
  4005. val & ~(ULONGEST) 0x1);
  4006. }
  4007. /* Write to the PC as from a branch-exchange instruction. */
  4008. static void
  4009. bx_write_pc (struct regcache *regs, ULONGEST val)
  4010. {
  4011. ULONGEST ps;
  4012. ULONGEST t_bit = arm_psr_thumb_bit (regs->arch ());
  4013. regcache_cooked_read_unsigned (regs, ARM_PS_REGNUM, &ps);
  4014. if ((val & 1) == 1)
  4015. {
  4016. regcache_cooked_write_unsigned (regs, ARM_PS_REGNUM, ps | t_bit);
  4017. regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM, val & 0xfffffffe);
  4018. }
  4019. else if ((val & 2) == 0)
  4020. {
  4021. regcache_cooked_write_unsigned (regs, ARM_PS_REGNUM, ps & ~t_bit);
  4022. regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM, val);
  4023. }
  4024. else
  4025. {
  4026. /* Unpredictable behaviour. Try to do something sensible (switch to ARM
  4027. mode, align dest to 4 bytes). */
  4028. warning (_("Single-stepping BX to non-word-aligned ARM instruction."));
  4029. regcache_cooked_write_unsigned (regs, ARM_PS_REGNUM, ps & ~t_bit);
  4030. regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM, val & 0xfffffffc);
  4031. }
  4032. }
  4033. /* Write to the PC as if from a load instruction. */
  4034. static void
  4035. load_write_pc (regcache *regs, arm_displaced_step_copy_insn_closure *dsc,
  4036. ULONGEST val)
  4037. {
  4038. if (DISPLACED_STEPPING_ARCH_VERSION >= 5)
  4039. bx_write_pc (regs, val);
  4040. else
  4041. branch_write_pc (regs, dsc, val);
  4042. }
  4043. /* Write to the PC as if from an ALU instruction. */
  4044. static void
  4045. alu_write_pc (regcache *regs, arm_displaced_step_copy_insn_closure *dsc,
  4046. ULONGEST val)
  4047. {
  4048. if (DISPLACED_STEPPING_ARCH_VERSION >= 7 && !dsc->is_thumb)
  4049. bx_write_pc (regs, val);
  4050. else
  4051. branch_write_pc (regs, dsc, val);
  4052. }
  4053. /* Helper for writing to registers for displaced stepping. Writing to the PC
  4054. has a varying effects depending on the instruction which does the write:
  4055. this is controlled by the WRITE_PC argument. */
  4056. void
  4057. displaced_write_reg (regcache *regs, arm_displaced_step_copy_insn_closure *dsc,
  4058. int regno, ULONGEST val, enum pc_write_style write_pc)
  4059. {
  4060. if (regno == ARM_PC_REGNUM)
  4061. {
  4062. displaced_debug_printf ("writing pc %.8lx", (unsigned long) val);
  4063. switch (write_pc)
  4064. {
  4065. case BRANCH_WRITE_PC:
  4066. branch_write_pc (regs, dsc, val);
  4067. break;
  4068. case BX_WRITE_PC:
  4069. bx_write_pc (regs, val);
  4070. break;
  4071. case LOAD_WRITE_PC:
  4072. load_write_pc (regs, dsc, val);
  4073. break;
  4074. case ALU_WRITE_PC:
  4075. alu_write_pc (regs, dsc, val);
  4076. break;
  4077. case CANNOT_WRITE_PC:
  4078. warning (_("Instruction wrote to PC in an unexpected way when "
  4079. "single-stepping"));
  4080. break;
  4081. default:
  4082. internal_error (__FILE__, __LINE__,
  4083. _("Invalid argument to displaced_write_reg"));
  4084. }
  4085. dsc->wrote_to_pc = 1;
  4086. }
  4087. else
  4088. {
  4089. displaced_debug_printf ("writing r%d value %.8lx",
  4090. regno, (unsigned long) val);
  4091. regcache_cooked_write_unsigned (regs, regno, val);
  4092. }
  4093. }
  4094. /* This function is used to concisely determine if an instruction INSN
  4095. references PC. Register fields of interest in INSN should have the
  4096. corresponding fields of BITMASK set to 0b1111. The function
  4097. returns return 1 if any of these fields in INSN reference the PC
  4098. (also 0b1111, r15), else it returns 0. */
  4099. static int
  4100. insn_references_pc (uint32_t insn, uint32_t bitmask)
  4101. {
  4102. uint32_t lowbit = 1;
  4103. while (bitmask != 0)
  4104. {
  4105. uint32_t mask;
  4106. for (; lowbit && (bitmask & lowbit) == 0; lowbit <<= 1)
  4107. ;
  4108. if (!lowbit)
  4109. break;
  4110. mask = lowbit * 0xf;
  4111. if ((insn & mask) == mask)
  4112. return 1;
  4113. bitmask &= ~mask;
  4114. }
  4115. return 0;
  4116. }
  4117. /* The simplest copy function. Many instructions have the same effect no
  4118. matter what address they are executed at: in those cases, use this. */
  4119. static int
  4120. arm_copy_unmodified (struct gdbarch *gdbarch, uint32_t insn, const char *iname,
  4121. arm_displaced_step_copy_insn_closure *dsc)
  4122. {
  4123. displaced_debug_printf ("copying insn %.8lx, opcode/class '%s' unmodified",
  4124. (unsigned long) insn, iname);
  4125. dsc->modinsn[0] = insn;
  4126. return 0;
  4127. }
  4128. static int
  4129. thumb_copy_unmodified_32bit (struct gdbarch *gdbarch, uint16_t insn1,
  4130. uint16_t insn2, const char *iname,
  4131. arm_displaced_step_copy_insn_closure *dsc)
  4132. {
  4133. displaced_debug_printf ("copying insn %.4x %.4x, opcode/class '%s' "
  4134. "unmodified", insn1, insn2, iname);
  4135. dsc->modinsn[0] = insn1;
  4136. dsc->modinsn[1] = insn2;
  4137. dsc->numinsns = 2;
  4138. return 0;
  4139. }
  4140. /* Copy 16-bit Thumb(Thumb and 16-bit Thumb-2) instruction without any
  4141. modification. */
  4142. static int
  4143. thumb_copy_unmodified_16bit (struct gdbarch *gdbarch, uint16_t insn,
  4144. const char *iname,
  4145. arm_displaced_step_copy_insn_closure *dsc)
  4146. {
  4147. displaced_debug_printf ("copying insn %.4x, opcode/class '%s' unmodified",
  4148. insn, iname);
  4149. dsc->modinsn[0] = insn;
  4150. return 0;
  4151. }
  4152. /* Preload instructions with immediate offset. */
  4153. static void
  4154. cleanup_preload (struct gdbarch *gdbarch, regcache *regs,
  4155. arm_displaced_step_copy_insn_closure *dsc)
  4156. {
  4157. displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
  4158. if (!dsc->u.preload.immed)
  4159. displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
  4160. }
  4161. static void
  4162. install_preload (struct gdbarch *gdbarch, struct regcache *regs,
  4163. arm_displaced_step_copy_insn_closure *dsc, unsigned int rn)
  4164. {
  4165. ULONGEST rn_val;
  4166. /* Preload instructions:
  4167. {pli/pld} [rn, #+/-imm]
  4168. ->
  4169. {pli/pld} [r0, #+/-imm]. */
  4170. dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
  4171. rn_val = displaced_read_reg (regs, dsc, rn);
  4172. displaced_write_reg (regs, dsc, 0, rn_val, CANNOT_WRITE_PC);
  4173. dsc->u.preload.immed = 1;
  4174. dsc->cleanup = &cleanup_preload;
  4175. }
  4176. static int
  4177. arm_copy_preload (struct gdbarch *gdbarch, uint32_t insn, struct regcache *regs,
  4178. arm_displaced_step_copy_insn_closure *dsc)
  4179. {
  4180. unsigned int rn = bits (insn, 16, 19);
  4181. if (!insn_references_pc (insn, 0x000f0000ul))
  4182. return arm_copy_unmodified (gdbarch, insn, "preload", dsc);
  4183. displaced_debug_printf ("copying preload insn %.8lx", (unsigned long) insn);
  4184. dsc->modinsn[0] = insn & 0xfff0ffff;
  4185. install_preload (gdbarch, regs, dsc, rn);
  4186. return 0;
  4187. }
  4188. static int
  4189. thumb2_copy_preload (struct gdbarch *gdbarch, uint16_t insn1, uint16_t insn2,
  4190. regcache *regs, arm_displaced_step_copy_insn_closure *dsc)
  4191. {
  4192. unsigned int rn = bits (insn1, 0, 3);
  4193. unsigned int u_bit = bit (insn1, 7);
  4194. int imm12 = bits (insn2, 0, 11);
  4195. ULONGEST pc_val;
  4196. if (rn != ARM_PC_REGNUM)
  4197. return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "preload", dsc);
  4198. /* PC is only allowed to use in PLI (immediate,literal) Encoding T3, and
  4199. PLD (literal) Encoding T1. */
  4200. displaced_debug_printf ("copying pld/pli pc (0x%x) %c imm12 %.4x",
  4201. (unsigned int) dsc->insn_addr, u_bit ? '+' : '-',
  4202. imm12);
  4203. if (!u_bit)
  4204. imm12 = -1 * imm12;
  4205. /* Rewrite instruction {pli/pld} PC imm12 into:
  4206. Prepare: tmp[0] <- r0, tmp[1] <- r1, r0 <- pc, r1 <- imm12
  4207. {pli/pld} [r0, r1]
  4208. Cleanup: r0 <- tmp[0], r1 <- tmp[1]. */
  4209. dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
  4210. dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
  4211. pc_val = displaced_read_reg (regs, dsc, ARM_PC_REGNUM);
  4212. displaced_write_reg (regs, dsc, 0, pc_val, CANNOT_WRITE_PC);
  4213. displaced_write_reg (regs, dsc, 1, imm12, CANNOT_WRITE_PC);
  4214. dsc->u.preload.immed = 0;
  4215. /* {pli/pld} [r0, r1] */
  4216. dsc->modinsn[0] = insn1 & 0xfff0;
  4217. dsc->modinsn[1] = 0xf001;
  4218. dsc->numinsns = 2;
  4219. dsc->cleanup = &cleanup_preload;
  4220. return 0;
  4221. }
  4222. /* Preload instructions with register offset. */
  4223. static void
  4224. install_preload_reg(struct gdbarch *gdbarch, struct regcache *regs,
  4225. arm_displaced_step_copy_insn_closure *dsc, unsigned int rn,
  4226. unsigned int rm)
  4227. {
  4228. ULONGEST rn_val, rm_val;
  4229. /* Preload register-offset instructions:
  4230. {pli/pld} [rn, rm {, shift}]
  4231. ->
  4232. {pli/pld} [r0, r1 {, shift}]. */
  4233. dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
  4234. dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
  4235. rn_val = displaced_read_reg (regs, dsc, rn);
  4236. rm_val = displaced_read_reg (regs, dsc, rm);
  4237. displaced_write_reg (regs, dsc, 0, rn_val, CANNOT_WRITE_PC);
  4238. displaced_write_reg (regs, dsc, 1, rm_val, CANNOT_WRITE_PC);
  4239. dsc->u.preload.immed = 0;
  4240. dsc->cleanup = &cleanup_preload;
  4241. }
  4242. static int
  4243. arm_copy_preload_reg (struct gdbarch *gdbarch, uint32_t insn,
  4244. struct regcache *regs,
  4245. arm_displaced_step_copy_insn_closure *dsc)
  4246. {
  4247. unsigned int rn = bits (insn, 16, 19);
  4248. unsigned int rm = bits (insn, 0, 3);
  4249. if (!insn_references_pc (insn, 0x000f000ful))
  4250. return arm_copy_unmodified (gdbarch, insn, "preload reg", dsc);
  4251. displaced_debug_printf ("copying preload insn %.8lx",
  4252. (unsigned long) insn);
  4253. dsc->modinsn[0] = (insn & 0xfff0fff0) | 0x1;
  4254. install_preload_reg (gdbarch, regs, dsc, rn, rm);
  4255. return 0;
  4256. }
  4257. /* Copy/cleanup coprocessor load and store instructions. */
  4258. static void
  4259. cleanup_copro_load_store (struct gdbarch *gdbarch,
  4260. struct regcache *regs,
  4261. arm_displaced_step_copy_insn_closure *dsc)
  4262. {
  4263. ULONGEST rn_val = displaced_read_reg (regs, dsc, 0);
  4264. displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
  4265. if (dsc->u.ldst.writeback)
  4266. displaced_write_reg (regs, dsc, dsc->u.ldst.rn, rn_val, LOAD_WRITE_PC);
  4267. }
  4268. static void
  4269. install_copro_load_store (struct gdbarch *gdbarch, struct regcache *regs,
  4270. arm_displaced_step_copy_insn_closure *dsc,
  4271. int writeback, unsigned int rn)
  4272. {
  4273. ULONGEST rn_val;
  4274. /* Coprocessor load/store instructions:
  4275. {stc/stc2} [<Rn>, #+/-imm] (and other immediate addressing modes)
  4276. ->
  4277. {stc/stc2} [r0, #+/-imm].
  4278. ldc/ldc2 are handled identically. */
  4279. dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
  4280. rn_val = displaced_read_reg (regs, dsc, rn);
  4281. /* PC should be 4-byte aligned. */
  4282. rn_val = rn_val & 0xfffffffc;
  4283. displaced_write_reg (regs, dsc, 0, rn_val, CANNOT_WRITE_PC);
  4284. dsc->u.ldst.writeback = writeback;
  4285. dsc->u.ldst.rn = rn;
  4286. dsc->cleanup = &cleanup_copro_load_store;
  4287. }
  4288. static int
  4289. arm_copy_copro_load_store (struct gdbarch *gdbarch, uint32_t insn,
  4290. struct regcache *regs,
  4291. arm_displaced_step_copy_insn_closure *dsc)
  4292. {
  4293. unsigned int rn = bits (insn, 16, 19);
  4294. if (!insn_references_pc (insn, 0x000f0000ul))
  4295. return arm_copy_unmodified (gdbarch, insn, "copro load/store", dsc);
  4296. displaced_debug_printf ("copying coprocessor load/store insn %.8lx",
  4297. (unsigned long) insn);
  4298. dsc->modinsn[0] = insn & 0xfff0ffff;
  4299. install_copro_load_store (gdbarch, regs, dsc, bit (insn, 25), rn);
  4300. return 0;
  4301. }
  4302. static int
  4303. thumb2_copy_copro_load_store (struct gdbarch *gdbarch, uint16_t insn1,
  4304. uint16_t insn2, struct regcache *regs,
  4305. arm_displaced_step_copy_insn_closure *dsc)
  4306. {
  4307. unsigned int rn = bits (insn1, 0, 3);
  4308. if (rn != ARM_PC_REGNUM)
  4309. return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
  4310. "copro load/store", dsc);
  4311. displaced_debug_printf ("copying coprocessor load/store insn %.4x%.4x",
  4312. insn1, insn2);
  4313. dsc->modinsn[0] = insn1 & 0xfff0;
  4314. dsc->modinsn[1] = insn2;
  4315. dsc->numinsns = 2;
  4316. /* This function is called for copying instruction LDC/LDC2/VLDR, which
  4317. doesn't support writeback, so pass 0. */
  4318. install_copro_load_store (gdbarch, regs, dsc, 0, rn);
  4319. return 0;
  4320. }
  4321. /* Clean up branch instructions (actually perform the branch, by setting
  4322. PC). */
  4323. static void
  4324. cleanup_branch (struct gdbarch *gdbarch, struct regcache *regs,
  4325. arm_displaced_step_copy_insn_closure *dsc)
  4326. {
  4327. uint32_t status = displaced_read_reg (regs, dsc, ARM_PS_REGNUM);
  4328. int branch_taken = condition_true (dsc->u.branch.cond, status);
  4329. enum pc_write_style write_pc = dsc->u.branch.exchange
  4330. ? BX_WRITE_PC : BRANCH_WRITE_PC;
  4331. if (!branch_taken)
  4332. return;
  4333. if (dsc->u.branch.link)
  4334. {
  4335. /* The value of LR should be the next insn of current one. In order
  4336. not to confuse logic handling later insn `bx lr', if current insn mode
  4337. is Thumb, the bit 0 of LR value should be set to 1. */
  4338. ULONGEST next_insn_addr = dsc->insn_addr + dsc->insn_size;
  4339. if (dsc->is_thumb)
  4340. next_insn_addr |= 0x1;
  4341. displaced_write_reg (regs, dsc, ARM_LR_REGNUM, next_insn_addr,
  4342. CANNOT_WRITE_PC);
  4343. }
  4344. displaced_write_reg (regs, dsc, ARM_PC_REGNUM, dsc->u.branch.dest, write_pc);
  4345. }
  4346. /* Copy B/BL/BLX instructions with immediate destinations. */
  4347. static void
  4348. install_b_bl_blx (struct gdbarch *gdbarch, struct regcache *regs,
  4349. arm_displaced_step_copy_insn_closure *dsc,
  4350. unsigned int cond, int exchange, int link, long offset)
  4351. {
  4352. /* Implement "BL<cond> <label>" as:
  4353. Preparation: cond <- instruction condition
  4354. Insn: mov r0, r0 (nop)
  4355. Cleanup: if (condition true) { r14 <- pc; pc <- label }.
  4356. B<cond> similar, but don't set r14 in cleanup. */
  4357. dsc->u.branch.cond = cond;
  4358. dsc->u.branch.link = link;
  4359. dsc->u.branch.exchange = exchange;
  4360. dsc->u.branch.dest = dsc->insn_addr;
  4361. if (link && exchange)
  4362. /* For BLX, offset is computed from the Align (PC, 4). */
  4363. dsc->u.branch.dest = dsc->u.branch.dest & 0xfffffffc;
  4364. if (dsc->is_thumb)
  4365. dsc->u.branch.dest += 4 + offset;
  4366. else
  4367. dsc->u.branch.dest += 8 + offset;
  4368. dsc->cleanup = &cleanup_branch;
  4369. }
  4370. static int
  4371. arm_copy_b_bl_blx (struct gdbarch *gdbarch, uint32_t insn,
  4372. regcache *regs, arm_displaced_step_copy_insn_closure *dsc)
  4373. {
  4374. unsigned int cond = bits (insn, 28, 31);
  4375. int exchange = (cond == 0xf);
  4376. int link = exchange || bit (insn, 24);
  4377. long offset;
  4378. displaced_debug_printf ("copying %s immediate insn %.8lx",
  4379. (exchange) ? "blx" : (link) ? "bl" : "b",
  4380. (unsigned long) insn);
  4381. if (exchange)
  4382. /* For BLX, set bit 0 of the destination. The cleanup_branch function will
  4383. then arrange the switch into Thumb mode. */
  4384. offset = (bits (insn, 0, 23) << 2) | (bit (insn, 24) << 1) | 1;
  4385. else
  4386. offset = bits (insn, 0, 23) << 2;
  4387. if (bit (offset, 25))
  4388. offset = offset | ~0x3ffffff;
  4389. dsc->modinsn[0] = ARM_NOP;
  4390. install_b_bl_blx (gdbarch, regs, dsc, cond, exchange, link, offset);
  4391. return 0;
  4392. }
  4393. static int
  4394. thumb2_copy_b_bl_blx (struct gdbarch *gdbarch, uint16_t insn1,
  4395. uint16_t insn2, struct regcache *regs,
  4396. arm_displaced_step_copy_insn_closure *dsc)
  4397. {
  4398. int link = bit (insn2, 14);
  4399. int exchange = link && !bit (insn2, 12);
  4400. int cond = INST_AL;
  4401. long offset = 0;
  4402. int j1 = bit (insn2, 13);
  4403. int j2 = bit (insn2, 11);
  4404. int s = sbits (insn1, 10, 10);
  4405. int i1 = !(j1 ^ bit (insn1, 10));
  4406. int i2 = !(j2 ^ bit (insn1, 10));
  4407. if (!link && !exchange) /* B */
  4408. {
  4409. offset = (bits (insn2, 0, 10) << 1);
  4410. if (bit (insn2, 12)) /* Encoding T4 */
  4411. {
  4412. offset |= (bits (insn1, 0, 9) << 12)
  4413. | (i2 << 22)
  4414. | (i1 << 23)
  4415. | (s << 24);
  4416. cond = INST_AL;
  4417. }
  4418. else /* Encoding T3 */
  4419. {
  4420. offset |= (bits (insn1, 0, 5) << 12)
  4421. | (j1 << 18)
  4422. | (j2 << 19)
  4423. | (s << 20);
  4424. cond = bits (insn1, 6, 9);
  4425. }
  4426. }
  4427. else
  4428. {
  4429. offset = (bits (insn1, 0, 9) << 12);
  4430. offset |= ((i2 << 22) | (i1 << 23) | (s << 24));
  4431. offset |= exchange ?
  4432. (bits (insn2, 1, 10) << 2) : (bits (insn2, 0, 10) << 1);
  4433. }
  4434. displaced_debug_printf ("copying %s insn %.4x %.4x with offset %.8lx",
  4435. link ? (exchange) ? "blx" : "bl" : "b",
  4436. insn1, insn2, offset);
  4437. dsc->modinsn[0] = THUMB_NOP;
  4438. install_b_bl_blx (gdbarch, regs, dsc, cond, exchange, link, offset);
  4439. return 0;
  4440. }
  4441. /* Copy B Thumb instructions. */
  4442. static int
  4443. thumb_copy_b (struct gdbarch *gdbarch, uint16_t insn,
  4444. arm_displaced_step_copy_insn_closure *dsc)
  4445. {
  4446. unsigned int cond = 0;
  4447. int offset = 0;
  4448. unsigned short bit_12_15 = bits (insn, 12, 15);
  4449. CORE_ADDR from = dsc->insn_addr;
  4450. if (bit_12_15 == 0xd)
  4451. {
  4452. /* offset = SignExtend (imm8:0, 32) */
  4453. offset = sbits ((insn << 1), 0, 8);
  4454. cond = bits (insn, 8, 11);
  4455. }
  4456. else if (bit_12_15 == 0xe) /* Encoding T2 */
  4457. {
  4458. offset = sbits ((insn << 1), 0, 11);
  4459. cond = INST_AL;
  4460. }
  4461. displaced_debug_printf ("copying b immediate insn %.4x with offset %d",
  4462. insn, offset);
  4463. dsc->u.branch.cond = cond;
  4464. dsc->u.branch.link = 0;
  4465. dsc->u.branch.exchange = 0;
  4466. dsc->u.branch.dest = from + 4 + offset;
  4467. dsc->modinsn[0] = THUMB_NOP;
  4468. dsc->cleanup = &cleanup_branch;
  4469. return 0;
  4470. }
  4471. /* Copy BX/BLX with register-specified destinations. */
  4472. static void
  4473. install_bx_blx_reg (struct gdbarch *gdbarch, struct regcache *regs,
  4474. arm_displaced_step_copy_insn_closure *dsc, int link,
  4475. unsigned int cond, unsigned int rm)
  4476. {
  4477. /* Implement {BX,BLX}<cond> <reg>" as:
  4478. Preparation: cond <- instruction condition
  4479. Insn: mov r0, r0 (nop)
  4480. Cleanup: if (condition true) { r14 <- pc; pc <- dest; }.
  4481. Don't set r14 in cleanup for BX. */
  4482. dsc->u.branch.dest = displaced_read_reg (regs, dsc, rm);
  4483. dsc->u.branch.cond = cond;
  4484. dsc->u.branch.link = link;
  4485. dsc->u.branch.exchange = 1;
  4486. dsc->cleanup = &cleanup_branch;
  4487. }
  4488. static int
  4489. arm_copy_bx_blx_reg (struct gdbarch *gdbarch, uint32_t insn,
  4490. regcache *regs, arm_displaced_step_copy_insn_closure *dsc)
  4491. {
  4492. unsigned int cond = bits (insn, 28, 31);
  4493. /* BX: x12xxx1x
  4494. BLX: x12xxx3x. */
  4495. int link = bit (insn, 5);
  4496. unsigned int rm = bits (insn, 0, 3);
  4497. displaced_debug_printf ("copying insn %.8lx", (unsigned long) insn);
  4498. dsc->modinsn[0] = ARM_NOP;
  4499. install_bx_blx_reg (gdbarch, regs, dsc, link, cond, rm);
  4500. return 0;
  4501. }
  4502. static int
  4503. thumb_copy_bx_blx_reg (struct gdbarch *gdbarch, uint16_t insn,
  4504. struct regcache *regs,
  4505. arm_displaced_step_copy_insn_closure *dsc)
  4506. {
  4507. int link = bit (insn, 7);
  4508. unsigned int rm = bits (insn, 3, 6);
  4509. displaced_debug_printf ("copying insn %.4x", (unsigned short) insn);
  4510. dsc->modinsn[0] = THUMB_NOP;
  4511. install_bx_blx_reg (gdbarch, regs, dsc, link, INST_AL, rm);
  4512. return 0;
  4513. }
  4514. /* Copy/cleanup arithmetic/logic instruction with immediate RHS. */
  4515. static void
  4516. cleanup_alu_imm (struct gdbarch *gdbarch,
  4517. regcache *regs, arm_displaced_step_copy_insn_closure *dsc)
  4518. {
  4519. ULONGEST rd_val = displaced_read_reg (regs, dsc, 0);
  4520. displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
  4521. displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
  4522. displaced_write_reg (regs, dsc, dsc->rd, rd_val, ALU_WRITE_PC);
  4523. }
  4524. static int
  4525. arm_copy_alu_imm (struct gdbarch *gdbarch, uint32_t insn, struct regcache *regs,
  4526. arm_displaced_step_copy_insn_closure *dsc)
  4527. {
  4528. unsigned int rn = bits (insn, 16, 19);
  4529. unsigned int rd = bits (insn, 12, 15);
  4530. unsigned int op = bits (insn, 21, 24);
  4531. int is_mov = (op == 0xd);
  4532. ULONGEST rd_val, rn_val;
  4533. if (!insn_references_pc (insn, 0x000ff000ul))
  4534. return arm_copy_unmodified (gdbarch, insn, "ALU immediate", dsc);
  4535. displaced_debug_printf ("copying immediate %s insn %.8lx",
  4536. is_mov ? "move" : "ALU",
  4537. (unsigned long) insn);
  4538. /* Instruction is of form:
  4539. <op><cond> rd, [rn,] #imm
  4540. Rewrite as:
  4541. Preparation: tmp1, tmp2 <- r0, r1;
  4542. r0, r1 <- rd, rn
  4543. Insn: <op><cond> r0, r1, #imm
  4544. Cleanup: rd <- r0; r0 <- tmp1; r1 <- tmp2
  4545. */
  4546. dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
  4547. dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
  4548. rn_val = displaced_read_reg (regs, dsc, rn);
  4549. rd_val = displaced_read_reg (regs, dsc, rd);
  4550. displaced_write_reg (regs, dsc, 0, rd_val, CANNOT_WRITE_PC);
  4551. displaced_write_reg (regs, dsc, 1, rn_val, CANNOT_WRITE_PC);
  4552. dsc->rd = rd;
  4553. if (is_mov)
  4554. dsc->modinsn[0] = insn & 0xfff00fff;
  4555. else
  4556. dsc->modinsn[0] = (insn & 0xfff00fff) | 0x10000;
  4557. dsc->cleanup = &cleanup_alu_imm;
  4558. return 0;
  4559. }
  4560. static int
  4561. thumb2_copy_alu_imm (struct gdbarch *gdbarch, uint16_t insn1,
  4562. uint16_t insn2, struct regcache *regs,
  4563. arm_displaced_step_copy_insn_closure *dsc)
  4564. {
  4565. unsigned int op = bits (insn1, 5, 8);
  4566. unsigned int rn, rm, rd;
  4567. ULONGEST rd_val, rn_val;
  4568. rn = bits (insn1, 0, 3); /* Rn */
  4569. rm = bits (insn2, 0, 3); /* Rm */
  4570. rd = bits (insn2, 8, 11); /* Rd */
  4571. /* This routine is only called for instruction MOV. */
  4572. gdb_assert (op == 0x2 && rn == 0xf);
  4573. if (rm != ARM_PC_REGNUM && rd != ARM_PC_REGNUM)
  4574. return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "ALU imm", dsc);
  4575. displaced_debug_printf ("copying reg %s insn %.4x%.4x", "ALU", insn1, insn2);
  4576. /* Instruction is of form:
  4577. <op><cond> rd, [rn,] #imm
  4578. Rewrite as:
  4579. Preparation: tmp1, tmp2 <- r0, r1;
  4580. r0, r1 <- rd, rn
  4581. Insn: <op><cond> r0, r1, #imm
  4582. Cleanup: rd <- r0; r0 <- tmp1; r1 <- tmp2
  4583. */
  4584. dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
  4585. dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
  4586. rn_val = displaced_read_reg (regs, dsc, rn);
  4587. rd_val = displaced_read_reg (regs, dsc, rd);
  4588. displaced_write_reg (regs, dsc, 0, rd_val, CANNOT_WRITE_PC);
  4589. displaced_write_reg (regs, dsc, 1, rn_val, CANNOT_WRITE_PC);
  4590. dsc->rd = rd;
  4591. dsc->modinsn[0] = insn1;
  4592. dsc->modinsn[1] = ((insn2 & 0xf0f0) | 0x1);
  4593. dsc->numinsns = 2;
  4594. dsc->cleanup = &cleanup_alu_imm;
  4595. return 0;
  4596. }
  4597. /* Copy/cleanup arithmetic/logic insns with register RHS. */
  4598. static void
  4599. cleanup_alu_reg (struct gdbarch *gdbarch,
  4600. regcache *regs, arm_displaced_step_copy_insn_closure *dsc)
  4601. {
  4602. ULONGEST rd_val;
  4603. int i;
  4604. rd_val = displaced_read_reg (regs, dsc, 0);
  4605. for (i = 0; i < 3; i++)
  4606. displaced_write_reg (regs, dsc, i, dsc->tmp[i], CANNOT_WRITE_PC);
  4607. displaced_write_reg (regs, dsc, dsc->rd, rd_val, ALU_WRITE_PC);
  4608. }
  4609. static void
  4610. install_alu_reg (struct gdbarch *gdbarch, struct regcache *regs,
  4611. arm_displaced_step_copy_insn_closure *dsc,
  4612. unsigned int rd, unsigned int rn, unsigned int rm)
  4613. {
  4614. ULONGEST rd_val, rn_val, rm_val;
  4615. /* Instruction is of form:
  4616. <op><cond> rd, [rn,] rm [, <shift>]
  4617. Rewrite as:
  4618. Preparation: tmp1, tmp2, tmp3 <- r0, r1, r2;
  4619. r0, r1, r2 <- rd, rn, rm
  4620. Insn: <op><cond> r0, [r1,] r2 [, <shift>]
  4621. Cleanup: rd <- r0; r0, r1, r2 <- tmp1, tmp2, tmp3
  4622. */
  4623. dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
  4624. dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
  4625. dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
  4626. rd_val = displaced_read_reg (regs, dsc, rd);
  4627. rn_val = displaced_read_reg (regs, dsc, rn);
  4628. rm_val = displaced_read_reg (regs, dsc, rm);
  4629. displaced_write_reg (regs, dsc, 0, rd_val, CANNOT_WRITE_PC);
  4630. displaced_write_reg (regs, dsc, 1, rn_val, CANNOT_WRITE_PC);
  4631. displaced_write_reg (regs, dsc, 2, rm_val, CANNOT_WRITE_PC);
  4632. dsc->rd = rd;
  4633. dsc->cleanup = &cleanup_alu_reg;
  4634. }
  4635. static int
  4636. arm_copy_alu_reg (struct gdbarch *gdbarch, uint32_t insn, struct regcache *regs,
  4637. arm_displaced_step_copy_insn_closure *dsc)
  4638. {
  4639. unsigned int op = bits (insn, 21, 24);
  4640. int is_mov = (op == 0xd);
  4641. if (!insn_references_pc (insn, 0x000ff00ful))
  4642. return arm_copy_unmodified (gdbarch, insn, "ALU reg", dsc);
  4643. displaced_debug_printf ("copying reg %s insn %.8lx",
  4644. is_mov ? "move" : "ALU", (unsigned long) insn);
  4645. if (is_mov)
  4646. dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x2;
  4647. else
  4648. dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x10002;
  4649. install_alu_reg (gdbarch, regs, dsc, bits (insn, 12, 15), bits (insn, 16, 19),
  4650. bits (insn, 0, 3));
  4651. return 0;
  4652. }
  4653. static int
  4654. thumb_copy_alu_reg (struct gdbarch *gdbarch, uint16_t insn,
  4655. struct regcache *regs,
  4656. arm_displaced_step_copy_insn_closure *dsc)
  4657. {
  4658. unsigned rm, rd;
  4659. rm = bits (insn, 3, 6);
  4660. rd = (bit (insn, 7) << 3) | bits (insn, 0, 2);
  4661. if (rd != ARM_PC_REGNUM && rm != ARM_PC_REGNUM)
  4662. return thumb_copy_unmodified_16bit (gdbarch, insn, "ALU reg", dsc);
  4663. displaced_debug_printf ("copying ALU reg insn %.4x", (unsigned short) insn);
  4664. dsc->modinsn[0] = ((insn & 0xff00) | 0x10);
  4665. install_alu_reg (gdbarch, regs, dsc, rd, rd, rm);
  4666. return 0;
  4667. }
  4668. /* Cleanup/copy arithmetic/logic insns with shifted register RHS. */
  4669. static void
  4670. cleanup_alu_shifted_reg (struct gdbarch *gdbarch,
  4671. struct regcache *regs,
  4672. arm_displaced_step_copy_insn_closure *dsc)
  4673. {
  4674. ULONGEST rd_val = displaced_read_reg (regs, dsc, 0);
  4675. int i;
  4676. for (i = 0; i < 4; i++)
  4677. displaced_write_reg (regs, dsc, i, dsc->tmp[i], CANNOT_WRITE_PC);
  4678. displaced_write_reg (regs, dsc, dsc->rd, rd_val, ALU_WRITE_PC);
  4679. }
  4680. static void
  4681. install_alu_shifted_reg (struct gdbarch *gdbarch, struct regcache *regs,
  4682. arm_displaced_step_copy_insn_closure *dsc,
  4683. unsigned int rd, unsigned int rn, unsigned int rm,
  4684. unsigned rs)
  4685. {
  4686. int i;
  4687. ULONGEST rd_val, rn_val, rm_val, rs_val;
  4688. /* Instruction is of form:
  4689. <op><cond> rd, [rn,] rm, <shift> rs
  4690. Rewrite as:
  4691. Preparation: tmp1, tmp2, tmp3, tmp4 <- r0, r1, r2, r3
  4692. r0, r1, r2, r3 <- rd, rn, rm, rs
  4693. Insn: <op><cond> r0, r1, r2, <shift> r3
  4694. Cleanup: tmp5 <- r0
  4695. r0, r1, r2, r3 <- tmp1, tmp2, tmp3, tmp4
  4696. rd <- tmp5
  4697. */
  4698. for (i = 0; i < 4; i++)
  4699. dsc->tmp[i] = displaced_read_reg (regs, dsc, i);
  4700. rd_val = displaced_read_reg (regs, dsc, rd);
  4701. rn_val = displaced_read_reg (regs, dsc, rn);
  4702. rm_val = displaced_read_reg (regs, dsc, rm);
  4703. rs_val = displaced_read_reg (regs, dsc, rs);
  4704. displaced_write_reg (regs, dsc, 0, rd_val, CANNOT_WRITE_PC);
  4705. displaced_write_reg (regs, dsc, 1, rn_val, CANNOT_WRITE_PC);
  4706. displaced_write_reg (regs, dsc, 2, rm_val, CANNOT_WRITE_PC);
  4707. displaced_write_reg (regs, dsc, 3, rs_val, CANNOT_WRITE_PC);
  4708. dsc->rd = rd;
  4709. dsc->cleanup = &cleanup_alu_shifted_reg;
  4710. }
  4711. static int
  4712. arm_copy_alu_shifted_reg (struct gdbarch *gdbarch, uint32_t insn,
  4713. struct regcache *regs,
  4714. arm_displaced_step_copy_insn_closure *dsc)
  4715. {
  4716. unsigned int op = bits (insn, 21, 24);
  4717. int is_mov = (op == 0xd);
  4718. unsigned int rd, rn, rm, rs;
  4719. if (!insn_references_pc (insn, 0x000fff0ful))
  4720. return arm_copy_unmodified (gdbarch, insn, "ALU shifted reg", dsc);
  4721. displaced_debug_printf ("copying shifted reg %s insn %.8lx",
  4722. is_mov ? "move" : "ALU",
  4723. (unsigned long) insn);
  4724. rn = bits (insn, 16, 19);
  4725. rm = bits (insn, 0, 3);
  4726. rs = bits (insn, 8, 11);
  4727. rd = bits (insn, 12, 15);
  4728. if (is_mov)
  4729. dsc->modinsn[0] = (insn & 0xfff000f0) | 0x302;
  4730. else
  4731. dsc->modinsn[0] = (insn & 0xfff000f0) | 0x10302;
  4732. install_alu_shifted_reg (gdbarch, regs, dsc, rd, rn, rm, rs);
  4733. return 0;
  4734. }
  4735. /* Clean up load instructions. */
  4736. static void
  4737. cleanup_load (struct gdbarch *gdbarch, struct regcache *regs,
  4738. arm_displaced_step_copy_insn_closure *dsc)
  4739. {
  4740. ULONGEST rt_val, rt_val2 = 0, rn_val;
  4741. rt_val = displaced_read_reg (regs, dsc, 0);
  4742. if (dsc->u.ldst.xfersize == 8)
  4743. rt_val2 = displaced_read_reg (regs, dsc, 1);
  4744. rn_val = displaced_read_reg (regs, dsc, 2);
  4745. displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
  4746. if (dsc->u.ldst.xfersize > 4)
  4747. displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
  4748. displaced_write_reg (regs, dsc, 2, dsc->tmp[2], CANNOT_WRITE_PC);
  4749. if (!dsc->u.ldst.immed)
  4750. displaced_write_reg (regs, dsc, 3, dsc->tmp[3], CANNOT_WRITE_PC);
  4751. /* Handle register writeback. */
  4752. if (dsc->u.ldst.writeback)
  4753. displaced_write_reg (regs, dsc, dsc->u.ldst.rn, rn_val, CANNOT_WRITE_PC);
  4754. /* Put result in right place. */
  4755. displaced_write_reg (regs, dsc, dsc->rd, rt_val, LOAD_WRITE_PC);
  4756. if (dsc->u.ldst.xfersize == 8)
  4757. displaced_write_reg (regs, dsc, dsc->rd + 1, rt_val2, LOAD_WRITE_PC);
  4758. }
  4759. /* Clean up store instructions. */
  4760. static void
  4761. cleanup_store (struct gdbarch *gdbarch, struct regcache *regs,
  4762. arm_displaced_step_copy_insn_closure *dsc)
  4763. {
  4764. ULONGEST rn_val = displaced_read_reg (regs, dsc, 2);
  4765. displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
  4766. if (dsc->u.ldst.xfersize > 4)
  4767. displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
  4768. displaced_write_reg (regs, dsc, 2, dsc->tmp[2], CANNOT_WRITE_PC);
  4769. if (!dsc->u.ldst.immed)
  4770. displaced_write_reg (regs, dsc, 3, dsc->tmp[3], CANNOT_WRITE_PC);
  4771. if (!dsc->u.ldst.restore_r4)
  4772. displaced_write_reg (regs, dsc, 4, dsc->tmp[4], CANNOT_WRITE_PC);
  4773. /* Writeback. */
  4774. if (dsc->u.ldst.writeback)
  4775. displaced_write_reg (regs, dsc, dsc->u.ldst.rn, rn_val, CANNOT_WRITE_PC);
  4776. }
  4777. /* Copy "extra" load/store instructions. These are halfword/doubleword
  4778. transfers, which have a different encoding to byte/word transfers. */
  4779. static int
  4780. arm_copy_extra_ld_st (struct gdbarch *gdbarch, uint32_t insn, int unprivileged,
  4781. regcache *regs, arm_displaced_step_copy_insn_closure *dsc)
  4782. {
  4783. unsigned int op1 = bits (insn, 20, 24);
  4784. unsigned int op2 = bits (insn, 5, 6);
  4785. unsigned int rt = bits (insn, 12, 15);
  4786. unsigned int rn = bits (insn, 16, 19);
  4787. unsigned int rm = bits (insn, 0, 3);
  4788. char load[12] = {0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1};
  4789. char bytesize[12] = {2, 2, 2, 2, 8, 1, 8, 1, 8, 2, 8, 2};
  4790. int immed = (op1 & 0x4) != 0;
  4791. int opcode;
  4792. ULONGEST rt_val, rt_val2 = 0, rn_val, rm_val = 0;
  4793. if (!insn_references_pc (insn, 0x000ff00ful))
  4794. return arm_copy_unmodified (gdbarch, insn, "extra load/store", dsc);
  4795. displaced_debug_printf ("copying %sextra load/store insn %.8lx",
  4796. unprivileged ? "unprivileged " : "",
  4797. (unsigned long) insn);
  4798. opcode = ((op2 << 2) | (op1 & 0x1) | ((op1 & 0x4) >> 1)) - 4;
  4799. if (opcode < 0)
  4800. internal_error (__FILE__, __LINE__,
  4801. _("copy_extra_ld_st: instruction decode error"));
  4802. dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
  4803. dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
  4804. dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
  4805. if (!immed)
  4806. dsc->tmp[3] = displaced_read_reg (regs, dsc, 3);
  4807. rt_val = displaced_read_reg (regs, dsc, rt);
  4808. if (bytesize[opcode] == 8)
  4809. rt_val2 = displaced_read_reg (regs, dsc, rt + 1);
  4810. rn_val = displaced_read_reg (regs, dsc, rn);
  4811. if (!immed)
  4812. rm_val = displaced_read_reg (regs, dsc, rm);
  4813. displaced_write_reg (regs, dsc, 0, rt_val, CANNOT_WRITE_PC);
  4814. if (bytesize[opcode] == 8)
  4815. displaced_write_reg (regs, dsc, 1, rt_val2, CANNOT_WRITE_PC);
  4816. displaced_write_reg (regs, dsc, 2, rn_val, CANNOT_WRITE_PC);
  4817. if (!immed)
  4818. displaced_write_reg (regs, dsc, 3, rm_val, CANNOT_WRITE_PC);
  4819. dsc->rd = rt;
  4820. dsc->u.ldst.xfersize = bytesize[opcode];
  4821. dsc->u.ldst.rn = rn;
  4822. dsc->u.ldst.immed = immed;
  4823. dsc->u.ldst.writeback = bit (insn, 24) == 0 || bit (insn, 21) != 0;
  4824. dsc->u.ldst.restore_r4 = 0;
  4825. if (immed)
  4826. /* {ldr,str}<width><cond> rt, [rt2,] [rn, #imm]
  4827. ->
  4828. {ldr,str}<width><cond> r0, [r1,] [r2, #imm]. */
  4829. dsc->modinsn[0] = (insn & 0xfff00fff) | 0x20000;
  4830. else
  4831. /* {ldr,str}<width><cond> rt, [rt2,] [rn, +/-rm]
  4832. ->
  4833. {ldr,str}<width><cond> r0, [r1,] [r2, +/-r3]. */
  4834. dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x20003;
  4835. dsc->cleanup = load[opcode] ? &cleanup_load : &cleanup_store;
  4836. return 0;
  4837. }
  4838. /* Copy byte/half word/word loads and stores. */
  4839. static void
  4840. install_load_store (struct gdbarch *gdbarch, struct regcache *regs,
  4841. arm_displaced_step_copy_insn_closure *dsc, int load,
  4842. int immed, int writeback, int size, int usermode,
  4843. int rt, int rm, int rn)
  4844. {
  4845. ULONGEST rt_val, rn_val, rm_val = 0;
  4846. dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
  4847. dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
  4848. if (!immed)
  4849. dsc->tmp[3] = displaced_read_reg (regs, dsc, 3);
  4850. if (!load)
  4851. dsc->tmp[4] = displaced_read_reg (regs, dsc, 4);
  4852. rt_val = displaced_read_reg (regs, dsc, rt);
  4853. rn_val = displaced_read_reg (regs, dsc, rn);
  4854. if (!immed)
  4855. rm_val = displaced_read_reg (regs, dsc, rm);
  4856. displaced_write_reg (regs, dsc, 0, rt_val, CANNOT_WRITE_PC);
  4857. displaced_write_reg (regs, dsc, 2, rn_val, CANNOT_WRITE_PC);
  4858. if (!immed)
  4859. displaced_write_reg (regs, dsc, 3, rm_val, CANNOT_WRITE_PC);
  4860. dsc->rd = rt;
  4861. dsc->u.ldst.xfersize = size;
  4862. dsc->u.ldst.rn = rn;
  4863. dsc->u.ldst.immed = immed;
  4864. dsc->u.ldst.writeback = writeback;
  4865. /* To write PC we can do:
  4866. Before this sequence of instructions:
  4867. r0 is the PC value got from displaced_read_reg, so r0 = from + 8;
  4868. r2 is the Rn value got from displaced_read_reg.
  4869. Insn1: push {pc} Write address of STR instruction + offset on stack
  4870. Insn2: pop {r4} Read it back from stack, r4 = addr(Insn1) + offset
  4871. Insn3: sub r4, r4, pc r4 = addr(Insn1) + offset - pc
  4872. = addr(Insn1) + offset - addr(Insn3) - 8
  4873. = offset - 16
  4874. Insn4: add r4, r4, #8 r4 = offset - 8
  4875. Insn5: add r0, r0, r4 r0 = from + 8 + offset - 8
  4876. = from + offset
  4877. Insn6: str r0, [r2, #imm] (or str r0, [r2, r3])
  4878. Otherwise we don't know what value to write for PC, since the offset is
  4879. architecture-dependent (sometimes PC+8, sometimes PC+12). More details
  4880. of this can be found in Section "Saving from r15" in
  4881. http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0204g/Cihbjifh.html */
  4882. dsc->cleanup = load ? &cleanup_load : &cleanup_store;
  4883. }
  4884. static int
  4885. thumb2_copy_load_literal (struct gdbarch *gdbarch, uint16_t insn1,
  4886. uint16_t insn2, struct regcache *regs,
  4887. arm_displaced_step_copy_insn_closure *dsc, int size)
  4888. {
  4889. unsigned int u_bit = bit (insn1, 7);
  4890. unsigned int rt = bits (insn2, 12, 15);
  4891. int imm12 = bits (insn2, 0, 11);
  4892. ULONGEST pc_val;
  4893. displaced_debug_printf ("copying ldr pc (0x%x) R%d %c imm12 %.4x",
  4894. (unsigned int) dsc->insn_addr, rt, u_bit ? '+' : '-',
  4895. imm12);
  4896. if (!u_bit)
  4897. imm12 = -1 * imm12;
  4898. /* Rewrite instruction LDR Rt imm12 into:
  4899. Prepare: tmp[0] <- r0, tmp[1] <- r2, tmp[2] <- r3, r2 <- pc, r3 <- imm12
  4900. LDR R0, R2, R3,
  4901. Cleanup: rt <- r0, r0 <- tmp[0], r2 <- tmp[1], r3 <- tmp[2]. */
  4902. dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
  4903. dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
  4904. dsc->tmp[3] = displaced_read_reg (regs, dsc, 3);
  4905. pc_val = displaced_read_reg (regs, dsc, ARM_PC_REGNUM);
  4906. pc_val = pc_val & 0xfffffffc;
  4907. displaced_write_reg (regs, dsc, 2, pc_val, CANNOT_WRITE_PC);
  4908. displaced_write_reg (regs, dsc, 3, imm12, CANNOT_WRITE_PC);
  4909. dsc->rd = rt;
  4910. dsc->u.ldst.xfersize = size;
  4911. dsc->u.ldst.immed = 0;
  4912. dsc->u.ldst.writeback = 0;
  4913. dsc->u.ldst.restore_r4 = 0;
  4914. /* LDR R0, R2, R3 */
  4915. dsc->modinsn[0] = 0xf852;
  4916. dsc->modinsn[1] = 0x3;
  4917. dsc->numinsns = 2;
  4918. dsc->cleanup = &cleanup_load;
  4919. return 0;
  4920. }
  4921. static int
  4922. thumb2_copy_load_reg_imm (struct gdbarch *gdbarch, uint16_t insn1,
  4923. uint16_t insn2, struct regcache *regs,
  4924. arm_displaced_step_copy_insn_closure *dsc,
  4925. int writeback, int immed)
  4926. {
  4927. unsigned int rt = bits (insn2, 12, 15);
  4928. unsigned int rn = bits (insn1, 0, 3);
  4929. unsigned int rm = bits (insn2, 0, 3); /* Only valid if !immed. */
  4930. /* In LDR (register), there is also a register Rm, which is not allowed to
  4931. be PC, so we don't have to check it. */
  4932. if (rt != ARM_PC_REGNUM && rn != ARM_PC_REGNUM)
  4933. return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "load",
  4934. dsc);
  4935. displaced_debug_printf ("copying ldr r%d [r%d] insn %.4x%.4x",
  4936. rt, rn, insn1, insn2);
  4937. install_load_store (gdbarch, regs, dsc, 1, immed, writeback, 4,
  4938. 0, rt, rm, rn);
  4939. dsc->u.ldst.restore_r4 = 0;
  4940. if (immed)
  4941. /* ldr[b]<cond> rt, [rn, #imm], etc.
  4942. ->
  4943. ldr[b]<cond> r0, [r2, #imm]. */
  4944. {
  4945. dsc->modinsn[0] = (insn1 & 0xfff0) | 0x2;
  4946. dsc->modinsn[1] = insn2 & 0x0fff;
  4947. }
  4948. else
  4949. /* ldr[b]<cond> rt, [rn, rm], etc.
  4950. ->
  4951. ldr[b]<cond> r0, [r2, r3]. */
  4952. {
  4953. dsc->modinsn[0] = (insn1 & 0xfff0) | 0x2;
  4954. dsc->modinsn[1] = (insn2 & 0x0ff0) | 0x3;
  4955. }
  4956. dsc->numinsns = 2;
  4957. return 0;
  4958. }
  4959. static int
  4960. arm_copy_ldr_str_ldrb_strb (struct gdbarch *gdbarch, uint32_t insn,
  4961. struct regcache *regs,
  4962. arm_displaced_step_copy_insn_closure *dsc,
  4963. int load, int size, int usermode)
  4964. {
  4965. int immed = !bit (insn, 25);
  4966. int writeback = (bit (insn, 24) == 0 || bit (insn, 21) != 0);
  4967. unsigned int rt = bits (insn, 12, 15);
  4968. unsigned int rn = bits (insn, 16, 19);
  4969. unsigned int rm = bits (insn, 0, 3); /* Only valid if !immed. */
  4970. if (!insn_references_pc (insn, 0x000ff00ful))
  4971. return arm_copy_unmodified (gdbarch, insn, "load/store", dsc);
  4972. displaced_debug_printf ("copying %s%s r%d [r%d] insn %.8lx",
  4973. load ? (size == 1 ? "ldrb" : "ldr")
  4974. : (size == 1 ? "strb" : "str"),
  4975. usermode ? "t" : "",
  4976. rt, rn,
  4977. (unsigned long) insn);
  4978. install_load_store (gdbarch, regs, dsc, load, immed, writeback, size,
  4979. usermode, rt, rm, rn);
  4980. if (load || rt != ARM_PC_REGNUM)
  4981. {
  4982. dsc->u.ldst.restore_r4 = 0;
  4983. if (immed)
  4984. /* {ldr,str}[b]<cond> rt, [rn, #imm], etc.
  4985. ->
  4986. {ldr,str}[b]<cond> r0, [r2, #imm]. */
  4987. dsc->modinsn[0] = (insn & 0xfff00fff) | 0x20000;
  4988. else
  4989. /* {ldr,str}[b]<cond> rt, [rn, rm], etc.
  4990. ->
  4991. {ldr,str}[b]<cond> r0, [r2, r3]. */
  4992. dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x20003;
  4993. }
  4994. else
  4995. {
  4996. /* We need to use r4 as scratch. Make sure it's restored afterwards. */
  4997. dsc->u.ldst.restore_r4 = 1;
  4998. dsc->modinsn[0] = 0xe92d8000; /* push {pc} */
  4999. dsc->modinsn[1] = 0xe8bd0010; /* pop {r4} */
  5000. dsc->modinsn[2] = 0xe044400f; /* sub r4, r4, pc. */
  5001. dsc->modinsn[3] = 0xe2844008; /* add r4, r4, #8. */
  5002. dsc->modinsn[4] = 0xe0800004; /* add r0, r0, r4. */
  5003. /* As above. */
  5004. if (immed)
  5005. dsc->modinsn[5] = (insn & 0xfff00fff) | 0x20000;
  5006. else
  5007. dsc->modinsn[5] = (insn & 0xfff00ff0) | 0x20003;
  5008. dsc->numinsns = 6;
  5009. }
  5010. dsc->cleanup = load ? &cleanup_load : &cleanup_store;
  5011. return 0;
  5012. }
  5013. /* Cleanup LDM instructions with fully-populated register list. This is an
  5014. unfortunate corner case: it's impossible to implement correctly by modifying
  5015. the instruction. The issue is as follows: we have an instruction,
  5016. ldm rN, {r0-r15}
  5017. which we must rewrite to avoid loading PC. A possible solution would be to
  5018. do the load in two halves, something like (with suitable cleanup
  5019. afterwards):
  5020. mov r8, rN
  5021. ldm[id][ab] r8!, {r0-r7}
  5022. str r7, <temp>
  5023. ldm[id][ab] r8, {r7-r14}
  5024. <bkpt>
  5025. but at present there's no suitable place for <temp>, since the scratch space
  5026. is overwritten before the cleanup routine is called. For now, we simply
  5027. emulate the instruction. */
  5028. static void
  5029. cleanup_block_load_all (struct gdbarch *gdbarch, struct regcache *regs,
  5030. arm_displaced_step_copy_insn_closure *dsc)
  5031. {
  5032. int inc = dsc->u.block.increment;
  5033. int bump_before = dsc->u.block.before ? (inc ? 4 : -4) : 0;
  5034. int bump_after = dsc->u.block.before ? 0 : (inc ? 4 : -4);
  5035. uint32_t regmask = dsc->u.block.regmask;
  5036. int regno = inc ? 0 : 15;
  5037. CORE_ADDR xfer_addr = dsc->u.block.xfer_addr;
  5038. int exception_return = dsc->u.block.load && dsc->u.block.user
  5039. && (regmask & 0x8000) != 0;
  5040. uint32_t status = displaced_read_reg (regs, dsc, ARM_PS_REGNUM);
  5041. int do_transfer = condition_true (dsc->u.block.cond, status);
  5042. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  5043. if (!do_transfer)
  5044. return;
  5045. /* If the instruction is ldm rN, {...pc}^, I don't think there's anything
  5046. sensible we can do here. Complain loudly. */
  5047. if (exception_return)
  5048. error (_("Cannot single-step exception return"));
  5049. /* We don't handle any stores here for now. */
  5050. gdb_assert (dsc->u.block.load != 0);
  5051. displaced_debug_printf ("emulating block transfer: %s %s %s",
  5052. dsc->u.block.load ? "ldm" : "stm",
  5053. dsc->u.block.increment ? "inc" : "dec",
  5054. dsc->u.block.before ? "before" : "after");
  5055. while (regmask)
  5056. {
  5057. uint32_t memword;
  5058. if (inc)
  5059. while (regno <= ARM_PC_REGNUM && (regmask & (1 << regno)) == 0)
  5060. regno++;
  5061. else
  5062. while (regno >= 0 && (regmask & (1 << regno)) == 0)
  5063. regno--;
  5064. xfer_addr += bump_before;
  5065. memword = read_memory_unsigned_integer (xfer_addr, 4, byte_order);
  5066. displaced_write_reg (regs, dsc, regno, memword, LOAD_WRITE_PC);
  5067. xfer_addr += bump_after;
  5068. regmask &= ~(1 << regno);
  5069. }
  5070. if (dsc->u.block.writeback)
  5071. displaced_write_reg (regs, dsc, dsc->u.block.rn, xfer_addr,
  5072. CANNOT_WRITE_PC);
  5073. }
  5074. /* Clean up an STM which included the PC in the register list. */
  5075. static void
  5076. cleanup_block_store_pc (struct gdbarch *gdbarch, struct regcache *regs,
  5077. arm_displaced_step_copy_insn_closure *dsc)
  5078. {
  5079. uint32_t status = displaced_read_reg (regs, dsc, ARM_PS_REGNUM);
  5080. int store_executed = condition_true (dsc->u.block.cond, status);
  5081. CORE_ADDR pc_stored_at, transferred_regs
  5082. = count_one_bits (dsc->u.block.regmask);
  5083. CORE_ADDR stm_insn_addr;
  5084. uint32_t pc_val;
  5085. long offset;
  5086. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  5087. /* If condition code fails, there's nothing else to do. */
  5088. if (!store_executed)
  5089. return;
  5090. if (dsc->u.block.increment)
  5091. {
  5092. pc_stored_at = dsc->u.block.xfer_addr + 4 * transferred_regs;
  5093. if (dsc->u.block.before)
  5094. pc_stored_at += 4;
  5095. }
  5096. else
  5097. {
  5098. pc_stored_at = dsc->u.block.xfer_addr;
  5099. if (dsc->u.block.before)
  5100. pc_stored_at -= 4;
  5101. }
  5102. pc_val = read_memory_unsigned_integer (pc_stored_at, 4, byte_order);
  5103. stm_insn_addr = dsc->scratch_base;
  5104. offset = pc_val - stm_insn_addr;
  5105. displaced_debug_printf ("detected PC offset %.8lx for STM instruction",
  5106. offset);
  5107. /* Rewrite the stored PC to the proper value for the non-displaced original
  5108. instruction. */
  5109. write_memory_unsigned_integer (pc_stored_at, 4, byte_order,
  5110. dsc->insn_addr + offset);
  5111. }
  5112. /* Clean up an LDM which includes the PC in the register list. We clumped all
  5113. the registers in the transferred list into a contiguous range r0...rX (to
  5114. avoid loading PC directly and losing control of the debugged program), so we
  5115. must undo that here. */
  5116. static void
  5117. cleanup_block_load_pc (struct gdbarch *gdbarch,
  5118. struct regcache *regs,
  5119. arm_displaced_step_copy_insn_closure *dsc)
  5120. {
  5121. uint32_t status = displaced_read_reg (regs, dsc, ARM_PS_REGNUM);
  5122. int load_executed = condition_true (dsc->u.block.cond, status);
  5123. unsigned int mask = dsc->u.block.regmask, write_reg = ARM_PC_REGNUM;
  5124. unsigned int regs_loaded = count_one_bits (mask);
  5125. unsigned int num_to_shuffle = regs_loaded, clobbered;
  5126. /* The method employed here will fail if the register list is fully populated
  5127. (we need to avoid loading PC directly). */
  5128. gdb_assert (num_to_shuffle < 16);
  5129. if (!load_executed)
  5130. return;
  5131. clobbered = (1 << num_to_shuffle) - 1;
  5132. while (num_to_shuffle > 0)
  5133. {
  5134. if ((mask & (1 << write_reg)) != 0)
  5135. {
  5136. unsigned int read_reg = num_to_shuffle - 1;
  5137. if (read_reg != write_reg)
  5138. {
  5139. ULONGEST rval = displaced_read_reg (regs, dsc, read_reg);
  5140. displaced_write_reg (regs, dsc, write_reg, rval, LOAD_WRITE_PC);
  5141. displaced_debug_printf ("LDM: move loaded register r%d to r%d",
  5142. read_reg, write_reg);
  5143. }
  5144. else
  5145. displaced_debug_printf ("LDM: register r%d already in the right "
  5146. "place", write_reg);
  5147. clobbered &= ~(1 << write_reg);
  5148. num_to_shuffle--;
  5149. }
  5150. write_reg--;
  5151. }
  5152. /* Restore any registers we scribbled over. */
  5153. for (write_reg = 0; clobbered != 0; write_reg++)
  5154. {
  5155. if ((clobbered & (1 << write_reg)) != 0)
  5156. {
  5157. displaced_write_reg (regs, dsc, write_reg, dsc->tmp[write_reg],
  5158. CANNOT_WRITE_PC);
  5159. displaced_debug_printf ("LDM: restored clobbered register r%d",
  5160. write_reg);
  5161. clobbered &= ~(1 << write_reg);
  5162. }
  5163. }
  5164. /* Perform register writeback manually. */
  5165. if (dsc->u.block.writeback)
  5166. {
  5167. ULONGEST new_rn_val = dsc->u.block.xfer_addr;
  5168. if (dsc->u.block.increment)
  5169. new_rn_val += regs_loaded * 4;
  5170. else
  5171. new_rn_val -= regs_loaded * 4;
  5172. displaced_write_reg (regs, dsc, dsc->u.block.rn, new_rn_val,
  5173. CANNOT_WRITE_PC);
  5174. }
  5175. }
  5176. /* Handle ldm/stm, apart from some tricky cases which are unlikely to occur
  5177. in user-level code (in particular exception return, ldm rn, {...pc}^). */
  5178. static int
  5179. arm_copy_block_xfer (struct gdbarch *gdbarch, uint32_t insn,
  5180. struct regcache *regs,
  5181. arm_displaced_step_copy_insn_closure *dsc)
  5182. {
  5183. int load = bit (insn, 20);
  5184. int user = bit (insn, 22);
  5185. int increment = bit (insn, 23);
  5186. int before = bit (insn, 24);
  5187. int writeback = bit (insn, 21);
  5188. int rn = bits (insn, 16, 19);
  5189. /* Block transfers which don't mention PC can be run directly
  5190. out-of-line. */
  5191. if (rn != ARM_PC_REGNUM && (insn & 0x8000) == 0)
  5192. return arm_copy_unmodified (gdbarch, insn, "ldm/stm", dsc);
  5193. if (rn == ARM_PC_REGNUM)
  5194. {
  5195. warning (_("displaced: Unpredictable LDM or STM with "
  5196. "base register r15"));
  5197. return arm_copy_unmodified (gdbarch, insn, "unpredictable ldm/stm", dsc);
  5198. }
  5199. displaced_debug_printf ("copying block transfer insn %.8lx",
  5200. (unsigned long) insn);
  5201. dsc->u.block.xfer_addr = displaced_read_reg (regs, dsc, rn);
  5202. dsc->u.block.rn = rn;
  5203. dsc->u.block.load = load;
  5204. dsc->u.block.user = user;
  5205. dsc->u.block.increment = increment;
  5206. dsc->u.block.before = before;
  5207. dsc->u.block.writeback = writeback;
  5208. dsc->u.block.cond = bits (insn, 28, 31);
  5209. dsc->u.block.regmask = insn & 0xffff;
  5210. if (load)
  5211. {
  5212. if ((insn & 0xffff) == 0xffff)
  5213. {
  5214. /* LDM with a fully-populated register list. This case is
  5215. particularly tricky. Implement for now by fully emulating the
  5216. instruction (which might not behave perfectly in all cases, but
  5217. these instructions should be rare enough for that not to matter
  5218. too much). */
  5219. dsc->modinsn[0] = ARM_NOP;
  5220. dsc->cleanup = &cleanup_block_load_all;
  5221. }
  5222. else
  5223. {
  5224. /* LDM of a list of registers which includes PC. Implement by
  5225. rewriting the list of registers to be transferred into a
  5226. contiguous chunk r0...rX before doing the transfer, then shuffling
  5227. registers into the correct places in the cleanup routine. */
  5228. unsigned int regmask = insn & 0xffff;
  5229. unsigned int num_in_list = count_one_bits (regmask), new_regmask;
  5230. unsigned int i;
  5231. for (i = 0; i < num_in_list; i++)
  5232. dsc->tmp[i] = displaced_read_reg (regs, dsc, i);
  5233. /* Writeback makes things complicated. We need to avoid clobbering
  5234. the base register with one of the registers in our modified
  5235. register list, but just using a different register can't work in
  5236. all cases, e.g.:
  5237. ldm r14!, {r0-r13,pc}
  5238. which would need to be rewritten as:
  5239. ldm rN!, {r0-r14}
  5240. but that can't work, because there's no free register for N.
  5241. Solve this by turning off the writeback bit, and emulating
  5242. writeback manually in the cleanup routine. */
  5243. if (writeback)
  5244. insn &= ~(1 << 21);
  5245. new_regmask = (1 << num_in_list) - 1;
  5246. displaced_debug_printf ("LDM r%d%s, {..., pc}: original reg list "
  5247. "%.4x, modified list %.4x",
  5248. rn, writeback ? "!" : "",
  5249. (int) insn & 0xffff, new_regmask);
  5250. dsc->modinsn[0] = (insn & ~0xffff) | (new_regmask & 0xffff);
  5251. dsc->cleanup = &cleanup_block_load_pc;
  5252. }
  5253. }
  5254. else
  5255. {
  5256. /* STM of a list of registers which includes PC. Run the instruction
  5257. as-is, but out of line: this will store the wrong value for the PC,
  5258. so we must manually fix up the memory in the cleanup routine.
  5259. Doing things this way has the advantage that we can auto-detect
  5260. the offset of the PC write (which is architecture-dependent) in
  5261. the cleanup routine. */
  5262. dsc->modinsn[0] = insn;
  5263. dsc->cleanup = &cleanup_block_store_pc;
  5264. }
  5265. return 0;
  5266. }
  5267. static int
  5268. thumb2_copy_block_xfer (struct gdbarch *gdbarch, uint16_t insn1, uint16_t insn2,
  5269. struct regcache *regs,
  5270. arm_displaced_step_copy_insn_closure *dsc)
  5271. {
  5272. int rn = bits (insn1, 0, 3);
  5273. int load = bit (insn1, 4);
  5274. int writeback = bit (insn1, 5);
  5275. /* Block transfers which don't mention PC can be run directly
  5276. out-of-line. */
  5277. if (rn != ARM_PC_REGNUM && (insn2 & 0x8000) == 0)
  5278. return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "ldm/stm", dsc);
  5279. if (rn == ARM_PC_REGNUM)
  5280. {
  5281. warning (_("displaced: Unpredictable LDM or STM with "
  5282. "base register r15"));
  5283. return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
  5284. "unpredictable ldm/stm", dsc);
  5285. }
  5286. displaced_debug_printf ("copying block transfer insn %.4x%.4x",
  5287. insn1, insn2);
  5288. /* Clear bit 13, since it should be always zero. */
  5289. dsc->u.block.regmask = (insn2 & 0xdfff);
  5290. dsc->u.block.rn = rn;
  5291. dsc->u.block.load = load;
  5292. dsc->u.block.user = 0;
  5293. dsc->u.block.increment = bit (insn1, 7);
  5294. dsc->u.block.before = bit (insn1, 8);
  5295. dsc->u.block.writeback = writeback;
  5296. dsc->u.block.cond = INST_AL;
  5297. dsc->u.block.xfer_addr = displaced_read_reg (regs, dsc, rn);
  5298. if (load)
  5299. {
  5300. if (dsc->u.block.regmask == 0xffff)
  5301. {
  5302. /* This branch is impossible to happen. */
  5303. gdb_assert (0);
  5304. }
  5305. else
  5306. {
  5307. unsigned int regmask = dsc->u.block.regmask;
  5308. unsigned int num_in_list = count_one_bits (regmask), new_regmask;
  5309. unsigned int i;
  5310. for (i = 0; i < num_in_list; i++)
  5311. dsc->tmp[i] = displaced_read_reg (regs, dsc, i);
  5312. if (writeback)
  5313. insn1 &= ~(1 << 5);
  5314. new_regmask = (1 << num_in_list) - 1;
  5315. displaced_debug_printf ("LDM r%d%s, {..., pc}: original reg list "
  5316. "%.4x, modified list %.4x",
  5317. rn, writeback ? "!" : "",
  5318. (int) dsc->u.block.regmask, new_regmask);
  5319. dsc->modinsn[0] = insn1;
  5320. dsc->modinsn[1] = (new_regmask & 0xffff);
  5321. dsc->numinsns = 2;
  5322. dsc->cleanup = &cleanup_block_load_pc;
  5323. }
  5324. }
  5325. else
  5326. {
  5327. dsc->modinsn[0] = insn1;
  5328. dsc->modinsn[1] = insn2;
  5329. dsc->numinsns = 2;
  5330. dsc->cleanup = &cleanup_block_store_pc;
  5331. }
  5332. return 0;
  5333. }
  5334. /* Wrapper over read_memory_unsigned_integer for use in arm_get_next_pcs.
  5335. This is used to avoid a dependency on BFD's bfd_endian enum. */
  5336. ULONGEST
  5337. arm_get_next_pcs_read_memory_unsigned_integer (CORE_ADDR memaddr, int len,
  5338. int byte_order)
  5339. {
  5340. return read_memory_unsigned_integer (memaddr, len,
  5341. (enum bfd_endian) byte_order);
  5342. }
  5343. /* Wrapper over gdbarch_addr_bits_remove for use in arm_get_next_pcs. */
  5344. CORE_ADDR
  5345. arm_get_next_pcs_addr_bits_remove (struct arm_get_next_pcs *self,
  5346. CORE_ADDR val)
  5347. {
  5348. return gdbarch_addr_bits_remove (self->regcache->arch (), val);
  5349. }
  5350. /* Wrapper over syscall_next_pc for use in get_next_pcs. */
  5351. static CORE_ADDR
  5352. arm_get_next_pcs_syscall_next_pc (struct arm_get_next_pcs *self)
  5353. {
  5354. return 0;
  5355. }
  5356. /* Wrapper over arm_is_thumb for use in arm_get_next_pcs. */
  5357. int
  5358. arm_get_next_pcs_is_thumb (struct arm_get_next_pcs *self)
  5359. {
  5360. return arm_is_thumb (self->regcache);
  5361. }
  5362. /* single_step() is called just before we want to resume the inferior,
  5363. if we want to single-step it but there is no hardware or kernel
  5364. single-step support. We find the target of the coming instructions
  5365. and breakpoint them. */
  5366. std::vector<CORE_ADDR>
  5367. arm_software_single_step (struct regcache *regcache)
  5368. {
  5369. struct gdbarch *gdbarch = regcache->arch ();
  5370. struct arm_get_next_pcs next_pcs_ctx;
  5371. arm_get_next_pcs_ctor (&next_pcs_ctx,
  5372. &arm_get_next_pcs_ops,
  5373. gdbarch_byte_order (gdbarch),
  5374. gdbarch_byte_order_for_code (gdbarch),
  5375. 0,
  5376. regcache);
  5377. std::vector<CORE_ADDR> next_pcs = arm_get_next_pcs (&next_pcs_ctx);
  5378. for (CORE_ADDR &pc_ref : next_pcs)
  5379. pc_ref = gdbarch_addr_bits_remove (gdbarch, pc_ref);
  5380. return next_pcs;
  5381. }
  5382. /* Cleanup/copy SVC (SWI) instructions. These two functions are overridden
  5383. for Linux, where some SVC instructions must be treated specially. */
  5384. static void
  5385. cleanup_svc (struct gdbarch *gdbarch, struct regcache *regs,
  5386. arm_displaced_step_copy_insn_closure *dsc)
  5387. {
  5388. CORE_ADDR resume_addr = dsc->insn_addr + dsc->insn_size;
  5389. displaced_debug_printf ("cleanup for svc, resume at %.8lx",
  5390. (unsigned long) resume_addr);
  5391. displaced_write_reg (regs, dsc, ARM_PC_REGNUM, resume_addr, BRANCH_WRITE_PC);
  5392. }
  5393. /* Common copy routine for svc instruction. */
  5394. static int
  5395. install_svc (struct gdbarch *gdbarch, struct regcache *regs,
  5396. arm_displaced_step_copy_insn_closure *dsc)
  5397. {
  5398. /* Preparation: none.
  5399. Insn: unmodified svc.
  5400. Cleanup: pc <- insn_addr + insn_size. */
  5401. /* Pretend we wrote to the PC, so cleanup doesn't set PC to the next
  5402. instruction. */
  5403. dsc->wrote_to_pc = 1;
  5404. /* Allow OS-specific code to override SVC handling. */
  5405. if (dsc->u.svc.copy_svc_os)
  5406. return dsc->u.svc.copy_svc_os (gdbarch, regs, dsc);
  5407. else
  5408. {
  5409. dsc->cleanup = &cleanup_svc;
  5410. return 0;
  5411. }
  5412. }
  5413. static int
  5414. arm_copy_svc (struct gdbarch *gdbarch, uint32_t insn,
  5415. regcache *regs, arm_displaced_step_copy_insn_closure *dsc)
  5416. {
  5417. displaced_debug_printf ("copying svc insn %.8lx",
  5418. (unsigned long) insn);
  5419. dsc->modinsn[0] = insn;
  5420. return install_svc (gdbarch, regs, dsc);
  5421. }
  5422. static int
  5423. thumb_copy_svc (struct gdbarch *gdbarch, uint16_t insn,
  5424. regcache *regs, arm_displaced_step_copy_insn_closure *dsc)
  5425. {
  5426. displaced_debug_printf ("copying svc insn %.4x", insn);
  5427. dsc->modinsn[0] = insn;
  5428. return install_svc (gdbarch, regs, dsc);
  5429. }
  5430. /* Copy undefined instructions. */
  5431. static int
  5432. arm_copy_undef (struct gdbarch *gdbarch, uint32_t insn,
  5433. arm_displaced_step_copy_insn_closure *dsc)
  5434. {
  5435. displaced_debug_printf ("copying undefined insn %.8lx",
  5436. (unsigned long) insn);
  5437. dsc->modinsn[0] = insn;
  5438. return 0;
  5439. }
  5440. static int
  5441. thumb_32bit_copy_undef (struct gdbarch *gdbarch, uint16_t insn1, uint16_t insn2,
  5442. arm_displaced_step_copy_insn_closure *dsc)
  5443. {
  5444. displaced_debug_printf ("copying undefined insn %.4x %.4x",
  5445. (unsigned short) insn1, (unsigned short) insn2);
  5446. dsc->modinsn[0] = insn1;
  5447. dsc->modinsn[1] = insn2;
  5448. dsc->numinsns = 2;
  5449. return 0;
  5450. }
  5451. /* Copy unpredictable instructions. */
  5452. static int
  5453. arm_copy_unpred (struct gdbarch *gdbarch, uint32_t insn,
  5454. arm_displaced_step_copy_insn_closure *dsc)
  5455. {
  5456. displaced_debug_printf ("copying unpredictable insn %.8lx",
  5457. (unsigned long) insn);
  5458. dsc->modinsn[0] = insn;
  5459. return 0;
  5460. }
  5461. /* The decode_* functions are instruction decoding helpers. They mostly follow
  5462. the presentation in the ARM ARM. */
  5463. static int
  5464. arm_decode_misc_memhint_neon (struct gdbarch *gdbarch, uint32_t insn,
  5465. struct regcache *regs,
  5466. arm_displaced_step_copy_insn_closure *dsc)
  5467. {
  5468. unsigned int op1 = bits (insn, 20, 26), op2 = bits (insn, 4, 7);
  5469. unsigned int rn = bits (insn, 16, 19);
  5470. if (op1 == 0x10 && (op2 & 0x2) == 0x0 && (rn & 0x1) == 0x0)
  5471. return arm_copy_unmodified (gdbarch, insn, "cps", dsc);
  5472. else if (op1 == 0x10 && op2 == 0x0 && (rn & 0x1) == 0x1)
  5473. return arm_copy_unmodified (gdbarch, insn, "setend", dsc);
  5474. else if ((op1 & 0x60) == 0x20)
  5475. return arm_copy_unmodified (gdbarch, insn, "neon dataproc", dsc);
  5476. else if ((op1 & 0x71) == 0x40)
  5477. return arm_copy_unmodified (gdbarch, insn, "neon elt/struct load/store",
  5478. dsc);
  5479. else if ((op1 & 0x77) == 0x41)
  5480. return arm_copy_unmodified (gdbarch, insn, "unallocated mem hint", dsc);
  5481. else if ((op1 & 0x77) == 0x45)
  5482. return arm_copy_preload (gdbarch, insn, regs, dsc); /* pli. */
  5483. else if ((op1 & 0x77) == 0x51)
  5484. {
  5485. if (rn != 0xf)
  5486. return arm_copy_preload (gdbarch, insn, regs, dsc); /* pld/pldw. */
  5487. else
  5488. return arm_copy_unpred (gdbarch, insn, dsc);
  5489. }
  5490. else if ((op1 & 0x77) == 0x55)
  5491. return arm_copy_preload (gdbarch, insn, regs, dsc); /* pld/pldw. */
  5492. else if (op1 == 0x57)
  5493. switch (op2)
  5494. {
  5495. case 0x1: return arm_copy_unmodified (gdbarch, insn, "clrex", dsc);
  5496. case 0x4: return arm_copy_unmodified (gdbarch, insn, "dsb", dsc);
  5497. case 0x5: return arm_copy_unmodified (gdbarch, insn, "dmb", dsc);
  5498. case 0x6: return arm_copy_unmodified (gdbarch, insn, "isb", dsc);
  5499. default: return arm_copy_unpred (gdbarch, insn, dsc);
  5500. }
  5501. else if ((op1 & 0x63) == 0x43)
  5502. return arm_copy_unpred (gdbarch, insn, dsc);
  5503. else if ((op2 & 0x1) == 0x0)
  5504. switch (op1 & ~0x80)
  5505. {
  5506. case 0x61:
  5507. return arm_copy_unmodified (gdbarch, insn, "unallocated mem hint", dsc);
  5508. case 0x65:
  5509. return arm_copy_preload_reg (gdbarch, insn, regs, dsc); /* pli reg. */
  5510. case 0x71: case 0x75:
  5511. /* pld/pldw reg. */
  5512. return arm_copy_preload_reg (gdbarch, insn, regs, dsc);
  5513. case 0x63: case 0x67: case 0x73: case 0x77:
  5514. return arm_copy_unpred (gdbarch, insn, dsc);
  5515. default:
  5516. return arm_copy_undef (gdbarch, insn, dsc);
  5517. }
  5518. else
  5519. return arm_copy_undef (gdbarch, insn, dsc); /* Probably unreachable. */
  5520. }
  5521. static int
  5522. arm_decode_unconditional (struct gdbarch *gdbarch, uint32_t insn,
  5523. struct regcache *regs,
  5524. arm_displaced_step_copy_insn_closure *dsc)
  5525. {
  5526. if (bit (insn, 27) == 0)
  5527. return arm_decode_misc_memhint_neon (gdbarch, insn, regs, dsc);
  5528. /* Switch on bits: 0bxxxxx321xxx0xxxxxxxxxxxxxxxxxxxx. */
  5529. else switch (((insn & 0x7000000) >> 23) | ((insn & 0x100000) >> 20))
  5530. {
  5531. case 0x0: case 0x2:
  5532. return arm_copy_unmodified (gdbarch, insn, "srs", dsc);
  5533. case 0x1: case 0x3:
  5534. return arm_copy_unmodified (gdbarch, insn, "rfe", dsc);
  5535. case 0x4: case 0x5: case 0x6: case 0x7:
  5536. return arm_copy_b_bl_blx (gdbarch, insn, regs, dsc);
  5537. case 0x8:
  5538. switch ((insn & 0xe00000) >> 21)
  5539. {
  5540. case 0x1: case 0x3: case 0x4: case 0x5: case 0x6: case 0x7:
  5541. /* stc/stc2. */
  5542. return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
  5543. case 0x2:
  5544. return arm_copy_unmodified (gdbarch, insn, "mcrr/mcrr2", dsc);
  5545. default:
  5546. return arm_copy_undef (gdbarch, insn, dsc);
  5547. }
  5548. case 0x9:
  5549. {
  5550. int rn_f = (bits (insn, 16, 19) == 0xf);
  5551. switch ((insn & 0xe00000) >> 21)
  5552. {
  5553. case 0x1: case 0x3:
  5554. /* ldc/ldc2 imm (undefined for rn == pc). */
  5555. return rn_f ? arm_copy_undef (gdbarch, insn, dsc)
  5556. : arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
  5557. case 0x2:
  5558. return arm_copy_unmodified (gdbarch, insn, "mrrc/mrrc2", dsc);
  5559. case 0x4: case 0x5: case 0x6: case 0x7:
  5560. /* ldc/ldc2 lit (undefined for rn != pc). */
  5561. return rn_f ? arm_copy_copro_load_store (gdbarch, insn, regs, dsc)
  5562. : arm_copy_undef (gdbarch, insn, dsc);
  5563. default:
  5564. return arm_copy_undef (gdbarch, insn, dsc);
  5565. }
  5566. }
  5567. case 0xa:
  5568. return arm_copy_unmodified (gdbarch, insn, "stc/stc2", dsc);
  5569. case 0xb:
  5570. if (bits (insn, 16, 19) == 0xf)
  5571. /* ldc/ldc2 lit. */
  5572. return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
  5573. else
  5574. return arm_copy_undef (gdbarch, insn, dsc);
  5575. case 0xc:
  5576. if (bit (insn, 4))
  5577. return arm_copy_unmodified (gdbarch, insn, "mcr/mcr2", dsc);
  5578. else
  5579. return arm_copy_unmodified (gdbarch, insn, "cdp/cdp2", dsc);
  5580. case 0xd:
  5581. if (bit (insn, 4))
  5582. return arm_copy_unmodified (gdbarch, insn, "mrc/mrc2", dsc);
  5583. else
  5584. return arm_copy_unmodified (gdbarch, insn, "cdp/cdp2", dsc);
  5585. default:
  5586. return arm_copy_undef (gdbarch, insn, dsc);
  5587. }
  5588. }
  5589. /* Decode miscellaneous instructions in dp/misc encoding space. */
  5590. static int
  5591. arm_decode_miscellaneous (struct gdbarch *gdbarch, uint32_t insn,
  5592. struct regcache *regs,
  5593. arm_displaced_step_copy_insn_closure *dsc)
  5594. {
  5595. unsigned int op2 = bits (insn, 4, 6);
  5596. unsigned int op = bits (insn, 21, 22);
  5597. switch (op2)
  5598. {
  5599. case 0x0:
  5600. return arm_copy_unmodified (gdbarch, insn, "mrs/msr", dsc);
  5601. case 0x1:
  5602. if (op == 0x1) /* bx. */
  5603. return arm_copy_bx_blx_reg (gdbarch, insn, regs, dsc);
  5604. else if (op == 0x3)
  5605. return arm_copy_unmodified (gdbarch, insn, "clz", dsc);
  5606. else
  5607. return arm_copy_undef (gdbarch, insn, dsc);
  5608. case 0x2:
  5609. if (op == 0x1)
  5610. /* Not really supported. */
  5611. return arm_copy_unmodified (gdbarch, insn, "bxj", dsc);
  5612. else
  5613. return arm_copy_undef (gdbarch, insn, dsc);
  5614. case 0x3:
  5615. if (op == 0x1)
  5616. return arm_copy_bx_blx_reg (gdbarch, insn,
  5617. regs, dsc); /* blx register. */
  5618. else
  5619. return arm_copy_undef (gdbarch, insn, dsc);
  5620. case 0x5:
  5621. return arm_copy_unmodified (gdbarch, insn, "saturating add/sub", dsc);
  5622. case 0x7:
  5623. if (op == 0x1)
  5624. return arm_copy_unmodified (gdbarch, insn, "bkpt", dsc);
  5625. else if (op == 0x3)
  5626. /* Not really supported. */
  5627. return arm_copy_unmodified (gdbarch, insn, "smc", dsc);
  5628. /* Fall through. */
  5629. default:
  5630. return arm_copy_undef (gdbarch, insn, dsc);
  5631. }
  5632. }
  5633. static int
  5634. arm_decode_dp_misc (struct gdbarch *gdbarch, uint32_t insn,
  5635. struct regcache *regs,
  5636. arm_displaced_step_copy_insn_closure *dsc)
  5637. {
  5638. if (bit (insn, 25))
  5639. switch (bits (insn, 20, 24))
  5640. {
  5641. case 0x10:
  5642. return arm_copy_unmodified (gdbarch, insn, "movw", dsc);
  5643. case 0x14:
  5644. return arm_copy_unmodified (gdbarch, insn, "movt", dsc);
  5645. case 0x12: case 0x16:
  5646. return arm_copy_unmodified (gdbarch, insn, "msr imm", dsc);
  5647. default:
  5648. return arm_copy_alu_imm (gdbarch, insn, regs, dsc);
  5649. }
  5650. else
  5651. {
  5652. uint32_t op1 = bits (insn, 20, 24), op2 = bits (insn, 4, 7);
  5653. if ((op1 & 0x19) != 0x10 && (op2 & 0x1) == 0x0)
  5654. return arm_copy_alu_reg (gdbarch, insn, regs, dsc);
  5655. else if ((op1 & 0x19) != 0x10 && (op2 & 0x9) == 0x1)
  5656. return arm_copy_alu_shifted_reg (gdbarch, insn, regs, dsc);
  5657. else if ((op1 & 0x19) == 0x10 && (op2 & 0x8) == 0x0)
  5658. return arm_decode_miscellaneous (gdbarch, insn, regs, dsc);
  5659. else if ((op1 & 0x19) == 0x10 && (op2 & 0x9) == 0x8)
  5660. return arm_copy_unmodified (gdbarch, insn, "halfword mul/mla", dsc);
  5661. else if ((op1 & 0x10) == 0x00 && op2 == 0x9)
  5662. return arm_copy_unmodified (gdbarch, insn, "mul/mla", dsc);
  5663. else if ((op1 & 0x10) == 0x10 && op2 == 0x9)
  5664. return arm_copy_unmodified (gdbarch, insn, "synch", dsc);
  5665. else if (op2 == 0xb || (op2 & 0xd) == 0xd)
  5666. /* 2nd arg means "unprivileged". */
  5667. return arm_copy_extra_ld_st (gdbarch, insn, (op1 & 0x12) == 0x02, regs,
  5668. dsc);
  5669. }
  5670. /* Should be unreachable. */
  5671. return 1;
  5672. }
  5673. static int
  5674. arm_decode_ld_st_word_ubyte (struct gdbarch *gdbarch, uint32_t insn,
  5675. struct regcache *regs,
  5676. arm_displaced_step_copy_insn_closure *dsc)
  5677. {
  5678. int a = bit (insn, 25), b = bit (insn, 4);
  5679. uint32_t op1 = bits (insn, 20, 24);
  5680. if ((!a && (op1 & 0x05) == 0x00 && (op1 & 0x17) != 0x02)
  5681. || (a && (op1 & 0x05) == 0x00 && (op1 & 0x17) != 0x02 && !b))
  5682. return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 0, 4, 0);
  5683. else if ((!a && (op1 & 0x17) == 0x02)
  5684. || (a && (op1 & 0x17) == 0x02 && !b))
  5685. return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 0, 4, 1);
  5686. else if ((!a && (op1 & 0x05) == 0x01 && (op1 & 0x17) != 0x03)
  5687. || (a && (op1 & 0x05) == 0x01 && (op1 & 0x17) != 0x03 && !b))
  5688. return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 1, 4, 0);
  5689. else if ((!a && (op1 & 0x17) == 0x03)
  5690. || (a && (op1 & 0x17) == 0x03 && !b))
  5691. return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 1, 4, 1);
  5692. else if ((!a && (op1 & 0x05) == 0x04 && (op1 & 0x17) != 0x06)
  5693. || (a && (op1 & 0x05) == 0x04 && (op1 & 0x17) != 0x06 && !b))
  5694. return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 0, 1, 0);
  5695. else if ((!a && (op1 & 0x17) == 0x06)
  5696. || (a && (op1 & 0x17) == 0x06 && !b))
  5697. return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 0, 1, 1);
  5698. else if ((!a && (op1 & 0x05) == 0x05 && (op1 & 0x17) != 0x07)
  5699. || (a && (op1 & 0x05) == 0x05 && (op1 & 0x17) != 0x07 && !b))
  5700. return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 1, 1, 0);
  5701. else if ((!a && (op1 & 0x17) == 0x07)
  5702. || (a && (op1 & 0x17) == 0x07 && !b))
  5703. return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 1, 1, 1);
  5704. /* Should be unreachable. */
  5705. return 1;
  5706. }
  5707. static int
  5708. arm_decode_media (struct gdbarch *gdbarch, uint32_t insn,
  5709. arm_displaced_step_copy_insn_closure *dsc)
  5710. {
  5711. switch (bits (insn, 20, 24))
  5712. {
  5713. case 0x00: case 0x01: case 0x02: case 0x03:
  5714. return arm_copy_unmodified (gdbarch, insn, "parallel add/sub signed", dsc);
  5715. case 0x04: case 0x05: case 0x06: case 0x07:
  5716. return arm_copy_unmodified (gdbarch, insn, "parallel add/sub unsigned", dsc);
  5717. case 0x08: case 0x09: case 0x0a: case 0x0b:
  5718. case 0x0c: case 0x0d: case 0x0e: case 0x0f:
  5719. return arm_copy_unmodified (gdbarch, insn,
  5720. "decode/pack/unpack/saturate/reverse", dsc);
  5721. case 0x18:
  5722. if (bits (insn, 5, 7) == 0) /* op2. */
  5723. {
  5724. if (bits (insn, 12, 15) == 0xf)
  5725. return arm_copy_unmodified (gdbarch, insn, "usad8", dsc);
  5726. else
  5727. return arm_copy_unmodified (gdbarch, insn, "usada8", dsc);
  5728. }
  5729. else
  5730. return arm_copy_undef (gdbarch, insn, dsc);
  5731. case 0x1a: case 0x1b:
  5732. if (bits (insn, 5, 6) == 0x2) /* op2[1:0]. */
  5733. return arm_copy_unmodified (gdbarch, insn, "sbfx", dsc);
  5734. else
  5735. return arm_copy_undef (gdbarch, insn, dsc);
  5736. case 0x1c: case 0x1d:
  5737. if (bits (insn, 5, 6) == 0x0) /* op2[1:0]. */
  5738. {
  5739. if (bits (insn, 0, 3) == 0xf)
  5740. return arm_copy_unmodified (gdbarch, insn, "bfc", dsc);
  5741. else
  5742. return arm_copy_unmodified (gdbarch, insn, "bfi", dsc);
  5743. }
  5744. else
  5745. return arm_copy_undef (gdbarch, insn, dsc);
  5746. case 0x1e: case 0x1f:
  5747. if (bits (insn, 5, 6) == 0x2) /* op2[1:0]. */
  5748. return arm_copy_unmodified (gdbarch, insn, "ubfx", dsc);
  5749. else
  5750. return arm_copy_undef (gdbarch, insn, dsc);
  5751. }
  5752. /* Should be unreachable. */
  5753. return 1;
  5754. }
  5755. static int
  5756. arm_decode_b_bl_ldmstm (struct gdbarch *gdbarch, uint32_t insn,
  5757. struct regcache *regs,
  5758. arm_displaced_step_copy_insn_closure *dsc)
  5759. {
  5760. if (bit (insn, 25))
  5761. return arm_copy_b_bl_blx (gdbarch, insn, regs, dsc);
  5762. else
  5763. return arm_copy_block_xfer (gdbarch, insn, regs, dsc);
  5764. }
  5765. static int
  5766. arm_decode_ext_reg_ld_st (struct gdbarch *gdbarch, uint32_t insn,
  5767. struct regcache *regs,
  5768. arm_displaced_step_copy_insn_closure *dsc)
  5769. {
  5770. unsigned int opcode = bits (insn, 20, 24);
  5771. switch (opcode)
  5772. {
  5773. case 0x04: case 0x05: /* VFP/Neon mrrc/mcrr. */
  5774. return arm_copy_unmodified (gdbarch, insn, "vfp/neon mrrc/mcrr", dsc);
  5775. case 0x08: case 0x0a: case 0x0c: case 0x0e:
  5776. case 0x12: case 0x16:
  5777. return arm_copy_unmodified (gdbarch, insn, "vfp/neon vstm/vpush", dsc);
  5778. case 0x09: case 0x0b: case 0x0d: case 0x0f:
  5779. case 0x13: case 0x17:
  5780. return arm_copy_unmodified (gdbarch, insn, "vfp/neon vldm/vpop", dsc);
  5781. case 0x10: case 0x14: case 0x18: case 0x1c: /* vstr. */
  5782. case 0x11: case 0x15: case 0x19: case 0x1d: /* vldr. */
  5783. /* Note: no writeback for these instructions. Bit 25 will always be
  5784. zero though (via caller), so the following works OK. */
  5785. return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
  5786. }
  5787. /* Should be unreachable. */
  5788. return 1;
  5789. }
  5790. /* Decode shifted register instructions. */
  5791. static int
  5792. thumb2_decode_dp_shift_reg (struct gdbarch *gdbarch, uint16_t insn1,
  5793. uint16_t insn2, struct regcache *regs,
  5794. arm_displaced_step_copy_insn_closure *dsc)
  5795. {
  5796. /* PC is only allowed to be used in instruction MOV. */
  5797. unsigned int op = bits (insn1, 5, 8);
  5798. unsigned int rn = bits (insn1, 0, 3);
  5799. if (op == 0x2 && rn == 0xf) /* MOV */
  5800. return thumb2_copy_alu_imm (gdbarch, insn1, insn2, regs, dsc);
  5801. else
  5802. return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
  5803. "dp (shift reg)", dsc);
  5804. }
  5805. /* Decode extension register load/store. Exactly the same as
  5806. arm_decode_ext_reg_ld_st. */
  5807. static int
  5808. thumb2_decode_ext_reg_ld_st (struct gdbarch *gdbarch, uint16_t insn1,
  5809. uint16_t insn2, struct regcache *regs,
  5810. arm_displaced_step_copy_insn_closure *dsc)
  5811. {
  5812. unsigned int opcode = bits (insn1, 4, 8);
  5813. switch (opcode)
  5814. {
  5815. case 0x04: case 0x05:
  5816. return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
  5817. "vfp/neon vmov", dsc);
  5818. case 0x08: case 0x0c: /* 01x00 */
  5819. case 0x0a: case 0x0e: /* 01x10 */
  5820. case 0x12: case 0x16: /* 10x10 */
  5821. return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
  5822. "vfp/neon vstm/vpush", dsc);
  5823. case 0x09: case 0x0d: /* 01x01 */
  5824. case 0x0b: case 0x0f: /* 01x11 */
  5825. case 0x13: case 0x17: /* 10x11 */
  5826. return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
  5827. "vfp/neon vldm/vpop", dsc);
  5828. case 0x10: case 0x14: case 0x18: case 0x1c: /* vstr. */
  5829. return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
  5830. "vstr", dsc);
  5831. case 0x11: case 0x15: case 0x19: case 0x1d: /* vldr. */
  5832. return thumb2_copy_copro_load_store (gdbarch, insn1, insn2, regs, dsc);
  5833. }
  5834. /* Should be unreachable. */
  5835. return 1;
  5836. }
  5837. static int
  5838. arm_decode_svc_copro (struct gdbarch *gdbarch, uint32_t insn,
  5839. regcache *regs, arm_displaced_step_copy_insn_closure *dsc)
  5840. {
  5841. unsigned int op1 = bits (insn, 20, 25);
  5842. int op = bit (insn, 4);
  5843. unsigned int coproc = bits (insn, 8, 11);
  5844. if ((op1 & 0x20) == 0x00 && (op1 & 0x3a) != 0x00 && (coproc & 0xe) == 0xa)
  5845. return arm_decode_ext_reg_ld_st (gdbarch, insn, regs, dsc);
  5846. else if ((op1 & 0x21) == 0x00 && (op1 & 0x3a) != 0x00
  5847. && (coproc & 0xe) != 0xa)
  5848. /* stc/stc2. */
  5849. return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
  5850. else if ((op1 & 0x21) == 0x01 && (op1 & 0x3a) != 0x00
  5851. && (coproc & 0xe) != 0xa)
  5852. /* ldc/ldc2 imm/lit. */
  5853. return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
  5854. else if ((op1 & 0x3e) == 0x00)
  5855. return arm_copy_undef (gdbarch, insn, dsc);
  5856. else if ((op1 & 0x3e) == 0x04 && (coproc & 0xe) == 0xa)
  5857. return arm_copy_unmodified (gdbarch, insn, "neon 64bit xfer", dsc);
  5858. else if (op1 == 0x04 && (coproc & 0xe) != 0xa)
  5859. return arm_copy_unmodified (gdbarch, insn, "mcrr/mcrr2", dsc);
  5860. else if (op1 == 0x05 && (coproc & 0xe) != 0xa)
  5861. return arm_copy_unmodified (gdbarch, insn, "mrrc/mrrc2", dsc);
  5862. else if ((op1 & 0x30) == 0x20 && !op)
  5863. {
  5864. if ((coproc & 0xe) == 0xa)
  5865. return arm_copy_unmodified (gdbarch, insn, "vfp dataproc", dsc);
  5866. else
  5867. return arm_copy_unmodified (gdbarch, insn, "cdp/cdp2", dsc);
  5868. }
  5869. else if ((op1 & 0x30) == 0x20 && op)
  5870. return arm_copy_unmodified (gdbarch, insn, "neon 8/16/32 bit xfer", dsc);
  5871. else if ((op1 & 0x31) == 0x20 && op && (coproc & 0xe) != 0xa)
  5872. return arm_copy_unmodified (gdbarch, insn, "mcr/mcr2", dsc);
  5873. else if ((op1 & 0x31) == 0x21 && op && (coproc & 0xe) != 0xa)
  5874. return arm_copy_unmodified (gdbarch, insn, "mrc/mrc2", dsc);
  5875. else if ((op1 & 0x30) == 0x30)
  5876. return arm_copy_svc (gdbarch, insn, regs, dsc);
  5877. else
  5878. return arm_copy_undef (gdbarch, insn, dsc); /* Possibly unreachable. */
  5879. }
  5880. static int
  5881. thumb2_decode_svc_copro (struct gdbarch *gdbarch, uint16_t insn1,
  5882. uint16_t insn2, struct regcache *regs,
  5883. arm_displaced_step_copy_insn_closure *dsc)
  5884. {
  5885. unsigned int coproc = bits (insn2, 8, 11);
  5886. unsigned int bit_5_8 = bits (insn1, 5, 8);
  5887. unsigned int bit_9 = bit (insn1, 9);
  5888. unsigned int bit_4 = bit (insn1, 4);
  5889. if (bit_9 == 0)
  5890. {
  5891. if (bit_5_8 == 2)
  5892. return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
  5893. "neon 64bit xfer/mrrc/mrrc2/mcrr/mcrr2",
  5894. dsc);
  5895. else if (bit_5_8 == 0) /* UNDEFINED. */
  5896. return thumb_32bit_copy_undef (gdbarch, insn1, insn2, dsc);
  5897. else
  5898. {
  5899. /*coproc is 101x. SIMD/VFP, ext registers load/store. */
  5900. if ((coproc & 0xe) == 0xa)
  5901. return thumb2_decode_ext_reg_ld_st (gdbarch, insn1, insn2, regs,
  5902. dsc);
  5903. else /* coproc is not 101x. */
  5904. {
  5905. if (bit_4 == 0) /* STC/STC2. */
  5906. return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
  5907. "stc/stc2", dsc);
  5908. else /* LDC/LDC2 {literal, immediate}. */
  5909. return thumb2_copy_copro_load_store (gdbarch, insn1, insn2,
  5910. regs, dsc);
  5911. }
  5912. }
  5913. }
  5914. else
  5915. return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "coproc", dsc);
  5916. return 0;
  5917. }
  5918. static void
  5919. install_pc_relative (struct gdbarch *gdbarch, struct regcache *regs,
  5920. arm_displaced_step_copy_insn_closure *dsc, int rd)
  5921. {
  5922. /* ADR Rd, #imm
  5923. Rewrite as:
  5924. Preparation: Rd <- PC
  5925. Insn: ADD Rd, #imm
  5926. Cleanup: Null.
  5927. */
  5928. /* Rd <- PC */
  5929. int val = displaced_read_reg (regs, dsc, ARM_PC_REGNUM);
  5930. displaced_write_reg (regs, dsc, rd, val, CANNOT_WRITE_PC);
  5931. }
  5932. static int
  5933. thumb_copy_pc_relative_16bit (struct gdbarch *gdbarch, struct regcache *regs,
  5934. arm_displaced_step_copy_insn_closure *dsc,
  5935. int rd, unsigned int imm)
  5936. {
  5937. /* Encoding T2: ADDS Rd, #imm */
  5938. dsc->modinsn[0] = (0x3000 | (rd << 8) | imm);
  5939. install_pc_relative (gdbarch, regs, dsc, rd);
  5940. return 0;
  5941. }
  5942. static int
  5943. thumb_decode_pc_relative_16bit (struct gdbarch *gdbarch, uint16_t insn,
  5944. struct regcache *regs,
  5945. arm_displaced_step_copy_insn_closure *dsc)
  5946. {
  5947. unsigned int rd = bits (insn, 8, 10);
  5948. unsigned int imm8 = bits (insn, 0, 7);
  5949. displaced_debug_printf ("copying thumb adr r%d, #%d insn %.4x",
  5950. rd, imm8, insn);
  5951. return thumb_copy_pc_relative_16bit (gdbarch, regs, dsc, rd, imm8);
  5952. }
  5953. static int
  5954. thumb_copy_pc_relative_32bit (struct gdbarch *gdbarch, uint16_t insn1,
  5955. uint16_t insn2, struct regcache *regs,
  5956. arm_displaced_step_copy_insn_closure *dsc)
  5957. {
  5958. unsigned int rd = bits (insn2, 8, 11);
  5959. /* Since immediate has the same encoding in ADR ADD and SUB, so we simply
  5960. extract raw immediate encoding rather than computing immediate. When
  5961. generating ADD or SUB instruction, we can simply perform OR operation to
  5962. set immediate into ADD. */
  5963. unsigned int imm_3_8 = insn2 & 0x70ff;
  5964. unsigned int imm_i = insn1 & 0x0400; /* Clear all bits except bit 10. */
  5965. displaced_debug_printf ("copying thumb adr r%d, #%d:%d insn %.4x%.4x",
  5966. rd, imm_i, imm_3_8, insn1, insn2);
  5967. if (bit (insn1, 7)) /* Encoding T2 */
  5968. {
  5969. /* Encoding T3: SUB Rd, Rd, #imm */
  5970. dsc->modinsn[0] = (0xf1a0 | rd | imm_i);
  5971. dsc->modinsn[1] = ((rd << 8) | imm_3_8);
  5972. }
  5973. else /* Encoding T3 */
  5974. {
  5975. /* Encoding T3: ADD Rd, Rd, #imm */
  5976. dsc->modinsn[0] = (0xf100 | rd | imm_i);
  5977. dsc->modinsn[1] = ((rd << 8) | imm_3_8);
  5978. }
  5979. dsc->numinsns = 2;
  5980. install_pc_relative (gdbarch, regs, dsc, rd);
  5981. return 0;
  5982. }
  5983. static int
  5984. thumb_copy_16bit_ldr_literal (struct gdbarch *gdbarch, uint16_t insn1,
  5985. struct regcache *regs,
  5986. arm_displaced_step_copy_insn_closure *dsc)
  5987. {
  5988. unsigned int rt = bits (insn1, 8, 10);
  5989. unsigned int pc;
  5990. int imm8 = (bits (insn1, 0, 7) << 2);
  5991. /* LDR Rd, #imm8
  5992. Rwrite as:
  5993. Preparation: tmp0 <- R0, tmp2 <- R2, tmp3 <- R3, R2 <- PC, R3 <- #imm8;
  5994. Insn: LDR R0, [R2, R3];
  5995. Cleanup: R2 <- tmp2, R3 <- tmp3, Rd <- R0, R0 <- tmp0 */
  5996. displaced_debug_printf ("copying thumb ldr r%d [pc #%d]", rt, imm8);
  5997. dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
  5998. dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
  5999. dsc->tmp[3] = displaced_read_reg (regs, dsc, 3);
  6000. pc = displaced_read_reg (regs, dsc, ARM_PC_REGNUM);
  6001. /* The assembler calculates the required value of the offset from the
  6002. Align(PC,4) value of this instruction to the label. */
  6003. pc = pc & 0xfffffffc;
  6004. displaced_write_reg (regs, dsc, 2, pc, CANNOT_WRITE_PC);
  6005. displaced_write_reg (regs, dsc, 3, imm8, CANNOT_WRITE_PC);
  6006. dsc->rd = rt;
  6007. dsc->u.ldst.xfersize = 4;
  6008. dsc->u.ldst.rn = 0;
  6009. dsc->u.ldst.immed = 0;
  6010. dsc->u.ldst.writeback = 0;
  6011. dsc->u.ldst.restore_r4 = 0;
  6012. dsc->modinsn[0] = 0x58d0; /* ldr r0, [r2, r3]*/
  6013. dsc->cleanup = &cleanup_load;
  6014. return 0;
  6015. }
  6016. /* Copy Thumb cbnz/cbz instruction. */
  6017. static int
  6018. thumb_copy_cbnz_cbz (struct gdbarch *gdbarch, uint16_t insn1,
  6019. struct regcache *regs,
  6020. arm_displaced_step_copy_insn_closure *dsc)
  6021. {
  6022. int non_zero = bit (insn1, 11);
  6023. unsigned int imm5 = (bit (insn1, 9) << 6) | (bits (insn1, 3, 7) << 1);
  6024. CORE_ADDR from = dsc->insn_addr;
  6025. int rn = bits (insn1, 0, 2);
  6026. int rn_val = displaced_read_reg (regs, dsc, rn);
  6027. dsc->u.branch.cond = (rn_val && non_zero) || (!rn_val && !non_zero);
  6028. /* CBNZ and CBZ do not affect the condition flags. If condition is true,
  6029. set it INST_AL, so cleanup_branch will know branch is taken, otherwise,
  6030. condition is false, let it be, cleanup_branch will do nothing. */
  6031. if (dsc->u.branch.cond)
  6032. {
  6033. dsc->u.branch.cond = INST_AL;
  6034. dsc->u.branch.dest = from + 4 + imm5;
  6035. }
  6036. else
  6037. dsc->u.branch.dest = from + 2;
  6038. dsc->u.branch.link = 0;
  6039. dsc->u.branch.exchange = 0;
  6040. displaced_debug_printf ("copying %s [r%d = 0x%x] insn %.4x to %.8lx",
  6041. non_zero ? "cbnz" : "cbz",
  6042. rn, rn_val, insn1, dsc->u.branch.dest);
  6043. dsc->modinsn[0] = THUMB_NOP;
  6044. dsc->cleanup = &cleanup_branch;
  6045. return 0;
  6046. }
  6047. /* Copy Table Branch Byte/Halfword */
  6048. static int
  6049. thumb2_copy_table_branch (struct gdbarch *gdbarch, uint16_t insn1,
  6050. uint16_t insn2, struct regcache *regs,
  6051. arm_displaced_step_copy_insn_closure *dsc)
  6052. {
  6053. ULONGEST rn_val, rm_val;
  6054. int is_tbh = bit (insn2, 4);
  6055. CORE_ADDR halfwords = 0;
  6056. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  6057. rn_val = displaced_read_reg (regs, dsc, bits (insn1, 0, 3));
  6058. rm_val = displaced_read_reg (regs, dsc, bits (insn2, 0, 3));
  6059. if (is_tbh)
  6060. {
  6061. gdb_byte buf[2];
  6062. target_read_memory (rn_val + 2 * rm_val, buf, 2);
  6063. halfwords = extract_unsigned_integer (buf, 2, byte_order);
  6064. }
  6065. else
  6066. {
  6067. gdb_byte buf[1];
  6068. target_read_memory (rn_val + rm_val, buf, 1);
  6069. halfwords = extract_unsigned_integer (buf, 1, byte_order);
  6070. }
  6071. displaced_debug_printf ("%s base 0x%x offset 0x%x offset 0x%x",
  6072. is_tbh ? "tbh" : "tbb",
  6073. (unsigned int) rn_val, (unsigned int) rm_val,
  6074. (unsigned int) halfwords);
  6075. dsc->u.branch.cond = INST_AL;
  6076. dsc->u.branch.link = 0;
  6077. dsc->u.branch.exchange = 0;
  6078. dsc->u.branch.dest = dsc->insn_addr + 4 + 2 * halfwords;
  6079. dsc->cleanup = &cleanup_branch;
  6080. return 0;
  6081. }
  6082. static void
  6083. cleanup_pop_pc_16bit_all (struct gdbarch *gdbarch, struct regcache *regs,
  6084. arm_displaced_step_copy_insn_closure *dsc)
  6085. {
  6086. /* PC <- r7 */
  6087. int val = displaced_read_reg (regs, dsc, 7);
  6088. displaced_write_reg (regs, dsc, ARM_PC_REGNUM, val, BX_WRITE_PC);
  6089. /* r7 <- r8 */
  6090. val = displaced_read_reg (regs, dsc, 8);
  6091. displaced_write_reg (regs, dsc, 7, val, CANNOT_WRITE_PC);
  6092. /* r8 <- tmp[0] */
  6093. displaced_write_reg (regs, dsc, 8, dsc->tmp[0], CANNOT_WRITE_PC);
  6094. }
  6095. static int
  6096. thumb_copy_pop_pc_16bit (struct gdbarch *gdbarch, uint16_t insn1,
  6097. struct regcache *regs,
  6098. arm_displaced_step_copy_insn_closure *dsc)
  6099. {
  6100. dsc->u.block.regmask = insn1 & 0x00ff;
  6101. /* Rewrite instruction: POP {rX, rY, ...,rZ, PC}
  6102. to :
  6103. (1) register list is full, that is, r0-r7 are used.
  6104. Prepare: tmp[0] <- r8
  6105. POP {r0, r1, ...., r6, r7}; remove PC from reglist
  6106. MOV r8, r7; Move value of r7 to r8;
  6107. POP {r7}; Store PC value into r7.
  6108. Cleanup: PC <- r7, r7 <- r8, r8 <-tmp[0]
  6109. (2) register list is not full, supposing there are N registers in
  6110. register list (except PC, 0 <= N <= 7).
  6111. Prepare: for each i, 0 - N, tmp[i] <- ri.
  6112. POP {r0, r1, ...., rN};
  6113. Cleanup: Set registers in original reglist from r0 - rN. Restore r0 - rN
  6114. from tmp[] properly.
  6115. */
  6116. displaced_debug_printf ("copying thumb pop {%.8x, pc} insn %.4x",
  6117. dsc->u.block.regmask, insn1);
  6118. if (dsc->u.block.regmask == 0xff)
  6119. {
  6120. dsc->tmp[0] = displaced_read_reg (regs, dsc, 8);
  6121. dsc->modinsn[0] = (insn1 & 0xfeff); /* POP {r0,r1,...,r6, r7} */
  6122. dsc->modinsn[1] = 0x46b8; /* MOV r8, r7 */
  6123. dsc->modinsn[2] = 0xbc80; /* POP {r7} */
  6124. dsc->numinsns = 3;
  6125. dsc->cleanup = &cleanup_pop_pc_16bit_all;
  6126. }
  6127. else
  6128. {
  6129. unsigned int num_in_list = count_one_bits (dsc->u.block.regmask);
  6130. unsigned int i;
  6131. unsigned int new_regmask;
  6132. for (i = 0; i < num_in_list + 1; i++)
  6133. dsc->tmp[i] = displaced_read_reg (regs, dsc, i);
  6134. new_regmask = (1 << (num_in_list + 1)) - 1;
  6135. displaced_debug_printf ("POP {..., pc}: original reg list %.4x, "
  6136. "modified list %.4x",
  6137. (int) dsc->u.block.regmask, new_regmask);
  6138. dsc->u.block.regmask |= 0x8000;
  6139. dsc->u.block.writeback = 0;
  6140. dsc->u.block.cond = INST_AL;
  6141. dsc->modinsn[0] = (insn1 & ~0x1ff) | (new_regmask & 0xff);
  6142. dsc->cleanup = &cleanup_block_load_pc;
  6143. }
  6144. return 0;
  6145. }
  6146. static void
  6147. thumb_process_displaced_16bit_insn (struct gdbarch *gdbarch, uint16_t insn1,
  6148. struct regcache *regs,
  6149. arm_displaced_step_copy_insn_closure *dsc)
  6150. {
  6151. unsigned short op_bit_12_15 = bits (insn1, 12, 15);
  6152. unsigned short op_bit_10_11 = bits (insn1, 10, 11);
  6153. int err = 0;
  6154. /* 16-bit thumb instructions. */
  6155. switch (op_bit_12_15)
  6156. {
  6157. /* Shift (imme), add, subtract, move and compare. */
  6158. case 0: case 1: case 2: case 3:
  6159. err = thumb_copy_unmodified_16bit (gdbarch, insn1,
  6160. "shift/add/sub/mov/cmp",
  6161. dsc);
  6162. break;
  6163. case 4:
  6164. switch (op_bit_10_11)
  6165. {
  6166. case 0: /* Data-processing */
  6167. err = thumb_copy_unmodified_16bit (gdbarch, insn1,
  6168. "data-processing",
  6169. dsc);
  6170. break;
  6171. case 1: /* Special data instructions and branch and exchange. */
  6172. {
  6173. unsigned short op = bits (insn1, 7, 9);
  6174. if (op == 6 || op == 7) /* BX or BLX */
  6175. err = thumb_copy_bx_blx_reg (gdbarch, insn1, regs, dsc);
  6176. else if (bits (insn1, 6, 7) != 0) /* ADD/MOV/CMP high registers. */
  6177. err = thumb_copy_alu_reg (gdbarch, insn1, regs, dsc);
  6178. else
  6179. err = thumb_copy_unmodified_16bit (gdbarch, insn1, "special data",
  6180. dsc);
  6181. }
  6182. break;
  6183. default: /* LDR (literal) */
  6184. err = thumb_copy_16bit_ldr_literal (gdbarch, insn1, regs, dsc);
  6185. }
  6186. break;
  6187. case 5: case 6: case 7: case 8: case 9: /* Load/Store single data item */
  6188. err = thumb_copy_unmodified_16bit (gdbarch, insn1, "ldr/str", dsc);
  6189. break;
  6190. case 10:
  6191. if (op_bit_10_11 < 2) /* Generate PC-relative address */
  6192. err = thumb_decode_pc_relative_16bit (gdbarch, insn1, regs, dsc);
  6193. else /* Generate SP-relative address */
  6194. err = thumb_copy_unmodified_16bit (gdbarch, insn1, "sp-relative", dsc);
  6195. break;
  6196. case 11: /* Misc 16-bit instructions */
  6197. {
  6198. switch (bits (insn1, 8, 11))
  6199. {
  6200. case 1: case 3: case 9: case 11: /* CBNZ, CBZ */
  6201. err = thumb_copy_cbnz_cbz (gdbarch, insn1, regs, dsc);
  6202. break;
  6203. case 12: case 13: /* POP */
  6204. if (bit (insn1, 8)) /* PC is in register list. */
  6205. err = thumb_copy_pop_pc_16bit (gdbarch, insn1, regs, dsc);
  6206. else
  6207. err = thumb_copy_unmodified_16bit (gdbarch, insn1, "pop", dsc);
  6208. break;
  6209. case 15: /* If-Then, and hints */
  6210. if (bits (insn1, 0, 3))
  6211. /* If-Then makes up to four following instructions conditional.
  6212. IT instruction itself is not conditional, so handle it as a
  6213. common unmodified instruction. */
  6214. err = thumb_copy_unmodified_16bit (gdbarch, insn1, "If-Then",
  6215. dsc);
  6216. else
  6217. err = thumb_copy_unmodified_16bit (gdbarch, insn1, "hints", dsc);
  6218. break;
  6219. default:
  6220. err = thumb_copy_unmodified_16bit (gdbarch, insn1, "misc", dsc);
  6221. }
  6222. }
  6223. break;
  6224. case 12:
  6225. if (op_bit_10_11 < 2) /* Store multiple registers */
  6226. err = thumb_copy_unmodified_16bit (gdbarch, insn1, "stm", dsc);
  6227. else /* Load multiple registers */
  6228. err = thumb_copy_unmodified_16bit (gdbarch, insn1, "ldm", dsc);
  6229. break;
  6230. case 13: /* Conditional branch and supervisor call */
  6231. if (bits (insn1, 9, 11) != 7) /* conditional branch */
  6232. err = thumb_copy_b (gdbarch, insn1, dsc);
  6233. else
  6234. err = thumb_copy_svc (gdbarch, insn1, regs, dsc);
  6235. break;
  6236. case 14: /* Unconditional branch */
  6237. err = thumb_copy_b (gdbarch, insn1, dsc);
  6238. break;
  6239. default:
  6240. err = 1;
  6241. }
  6242. if (err)
  6243. internal_error (__FILE__, __LINE__,
  6244. _("thumb_process_displaced_16bit_insn: Instruction decode error"));
  6245. }
  6246. static int
  6247. decode_thumb_32bit_ld_mem_hints (struct gdbarch *gdbarch,
  6248. uint16_t insn1, uint16_t insn2,
  6249. struct regcache *regs,
  6250. arm_displaced_step_copy_insn_closure *dsc)
  6251. {
  6252. int rt = bits (insn2, 12, 15);
  6253. int rn = bits (insn1, 0, 3);
  6254. int op1 = bits (insn1, 7, 8);
  6255. switch (bits (insn1, 5, 6))
  6256. {
  6257. case 0: /* Load byte and memory hints */
  6258. if (rt == 0xf) /* PLD/PLI */
  6259. {
  6260. if (rn == 0xf)
  6261. /* PLD literal or Encoding T3 of PLI(immediate, literal). */
  6262. return thumb2_copy_preload (gdbarch, insn1, insn2, regs, dsc);
  6263. else
  6264. return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
  6265. "pli/pld", dsc);
  6266. }
  6267. else
  6268. {
  6269. if (rn == 0xf) /* LDRB/LDRSB (literal) */
  6270. return thumb2_copy_load_literal (gdbarch, insn1, insn2, regs, dsc,
  6271. 1);
  6272. else
  6273. return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
  6274. "ldrb{reg, immediate}/ldrbt",
  6275. dsc);
  6276. }
  6277. break;
  6278. case 1: /* Load halfword and memory hints. */
  6279. if (rt == 0xf) /* PLD{W} and Unalloc memory hint. */
  6280. return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
  6281. "pld/unalloc memhint", dsc);
  6282. else
  6283. {
  6284. if (rn == 0xf)
  6285. return thumb2_copy_load_literal (gdbarch, insn1, insn2, regs, dsc,
  6286. 2);
  6287. else
  6288. return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
  6289. "ldrh/ldrht", dsc);
  6290. }
  6291. break;
  6292. case 2: /* Load word */
  6293. {
  6294. int insn2_bit_8_11 = bits (insn2, 8, 11);
  6295. if (rn == 0xf)
  6296. return thumb2_copy_load_literal (gdbarch, insn1, insn2, regs, dsc, 4);
  6297. else if (op1 == 0x1) /* Encoding T3 */
  6298. return thumb2_copy_load_reg_imm (gdbarch, insn1, insn2, regs, dsc,
  6299. 0, 1);
  6300. else /* op1 == 0x0 */
  6301. {
  6302. if (insn2_bit_8_11 == 0xc || (insn2_bit_8_11 & 0x9) == 0x9)
  6303. /* LDR (immediate) */
  6304. return thumb2_copy_load_reg_imm (gdbarch, insn1, insn2, regs,
  6305. dsc, bit (insn2, 8), 1);
  6306. else if (insn2_bit_8_11 == 0xe) /* LDRT */
  6307. return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
  6308. "ldrt", dsc);
  6309. else
  6310. /* LDR (register) */
  6311. return thumb2_copy_load_reg_imm (gdbarch, insn1, insn2, regs,
  6312. dsc, 0, 0);
  6313. }
  6314. break;
  6315. }
  6316. default:
  6317. return thumb_32bit_copy_undef (gdbarch, insn1, insn2, dsc);
  6318. break;
  6319. }
  6320. return 0;
  6321. }
  6322. static void
  6323. thumb_process_displaced_32bit_insn (struct gdbarch *gdbarch, uint16_t insn1,
  6324. uint16_t insn2, struct regcache *regs,
  6325. arm_displaced_step_copy_insn_closure *dsc)
  6326. {
  6327. int err = 0;
  6328. unsigned short op = bit (insn2, 15);
  6329. unsigned int op1 = bits (insn1, 11, 12);
  6330. switch (op1)
  6331. {
  6332. case 1:
  6333. {
  6334. switch (bits (insn1, 9, 10))
  6335. {
  6336. case 0:
  6337. if (bit (insn1, 6))
  6338. {
  6339. /* Load/store {dual, exclusive}, table branch. */
  6340. if (bits (insn1, 7, 8) == 1 && bits (insn1, 4, 5) == 1
  6341. && bits (insn2, 5, 7) == 0)
  6342. err = thumb2_copy_table_branch (gdbarch, insn1, insn2, regs,
  6343. dsc);
  6344. else
  6345. /* PC is not allowed to use in load/store {dual, exclusive}
  6346. instructions. */
  6347. err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
  6348. "load/store dual/ex", dsc);
  6349. }
  6350. else /* load/store multiple */
  6351. {
  6352. switch (bits (insn1, 7, 8))
  6353. {
  6354. case 0: case 3: /* SRS, RFE */
  6355. err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
  6356. "srs/rfe", dsc);
  6357. break;
  6358. case 1: case 2: /* LDM/STM/PUSH/POP */
  6359. err = thumb2_copy_block_xfer (gdbarch, insn1, insn2, regs, dsc);
  6360. break;
  6361. }
  6362. }
  6363. break;
  6364. case 1:
  6365. /* Data-processing (shift register). */
  6366. err = thumb2_decode_dp_shift_reg (gdbarch, insn1, insn2, regs,
  6367. dsc);
  6368. break;
  6369. default: /* Coprocessor instructions. */
  6370. err = thumb2_decode_svc_copro (gdbarch, insn1, insn2, regs, dsc);
  6371. break;
  6372. }
  6373. break;
  6374. }
  6375. case 2: /* op1 = 2 */
  6376. if (op) /* Branch and misc control. */
  6377. {
  6378. if (bit (insn2, 14) /* BLX/BL */
  6379. || bit (insn2, 12) /* Unconditional branch */
  6380. || (bits (insn1, 7, 9) != 0x7)) /* Conditional branch */
  6381. err = thumb2_copy_b_bl_blx (gdbarch, insn1, insn2, regs, dsc);
  6382. else
  6383. err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
  6384. "misc ctrl", dsc);
  6385. }
  6386. else
  6387. {
  6388. if (bit (insn1, 9)) /* Data processing (plain binary imm). */
  6389. {
  6390. int dp_op = bits (insn1, 4, 8);
  6391. int rn = bits (insn1, 0, 3);
  6392. if ((dp_op == 0 || dp_op == 0xa) && rn == 0xf)
  6393. err = thumb_copy_pc_relative_32bit (gdbarch, insn1, insn2,
  6394. regs, dsc);
  6395. else
  6396. err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
  6397. "dp/pb", dsc);
  6398. }
  6399. else /* Data processing (modified immediate) */
  6400. err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
  6401. "dp/mi", dsc);
  6402. }
  6403. break;
  6404. case 3: /* op1 = 3 */
  6405. switch (bits (insn1, 9, 10))
  6406. {
  6407. case 0:
  6408. if (bit (insn1, 4))
  6409. err = decode_thumb_32bit_ld_mem_hints (gdbarch, insn1, insn2,
  6410. regs, dsc);
  6411. else /* NEON Load/Store and Store single data item */
  6412. err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
  6413. "neon elt/struct load/store",
  6414. dsc);
  6415. break;
  6416. case 1: /* op1 = 3, bits (9, 10) == 1 */
  6417. switch (bits (insn1, 7, 8))
  6418. {
  6419. case 0: case 1: /* Data processing (register) */
  6420. err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
  6421. "dp(reg)", dsc);
  6422. break;
  6423. case 2: /* Multiply and absolute difference */
  6424. err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
  6425. "mul/mua/diff", dsc);
  6426. break;
  6427. case 3: /* Long multiply and divide */
  6428. err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
  6429. "lmul/lmua", dsc);
  6430. break;
  6431. }
  6432. break;
  6433. default: /* Coprocessor instructions */
  6434. err = thumb2_decode_svc_copro (gdbarch, insn1, insn2, regs, dsc);
  6435. break;
  6436. }
  6437. break;
  6438. default:
  6439. err = 1;
  6440. }
  6441. if (err)
  6442. internal_error (__FILE__, __LINE__,
  6443. _("thumb_process_displaced_32bit_insn: Instruction decode error"));
  6444. }
  6445. static void
  6446. thumb_process_displaced_insn (struct gdbarch *gdbarch, CORE_ADDR from,
  6447. struct regcache *regs,
  6448. arm_displaced_step_copy_insn_closure *dsc)
  6449. {
  6450. enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
  6451. uint16_t insn1
  6452. = read_memory_unsigned_integer (from, 2, byte_order_for_code);
  6453. displaced_debug_printf ("process thumb insn %.4x at %.8lx",
  6454. insn1, (unsigned long) from);
  6455. dsc->is_thumb = 1;
  6456. dsc->insn_size = thumb_insn_size (insn1);
  6457. if (thumb_insn_size (insn1) == 4)
  6458. {
  6459. uint16_t insn2
  6460. = read_memory_unsigned_integer (from + 2, 2, byte_order_for_code);
  6461. thumb_process_displaced_32bit_insn (gdbarch, insn1, insn2, regs, dsc);
  6462. }
  6463. else
  6464. thumb_process_displaced_16bit_insn (gdbarch, insn1, regs, dsc);
  6465. }
  6466. void
  6467. arm_process_displaced_insn (struct gdbarch *gdbarch, CORE_ADDR from,
  6468. CORE_ADDR to, struct regcache *regs,
  6469. arm_displaced_step_copy_insn_closure *dsc)
  6470. {
  6471. int err = 0;
  6472. enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
  6473. uint32_t insn;
  6474. /* Most displaced instructions use a 1-instruction scratch space, so set this
  6475. here and override below if/when necessary. */
  6476. dsc->numinsns = 1;
  6477. dsc->insn_addr = from;
  6478. dsc->scratch_base = to;
  6479. dsc->cleanup = NULL;
  6480. dsc->wrote_to_pc = 0;
  6481. if (!displaced_in_arm_mode (regs))
  6482. return thumb_process_displaced_insn (gdbarch, from, regs, dsc);
  6483. dsc->is_thumb = 0;
  6484. dsc->insn_size = 4;
  6485. insn = read_memory_unsigned_integer (from, 4, byte_order_for_code);
  6486. displaced_debug_printf ("stepping insn %.8lx at %.8lx",
  6487. (unsigned long) insn, (unsigned long) from);
  6488. if ((insn & 0xf0000000) == 0xf0000000)
  6489. err = arm_decode_unconditional (gdbarch, insn, regs, dsc);
  6490. else switch (((insn & 0x10) >> 4) | ((insn & 0xe000000) >> 24))
  6491. {
  6492. case 0x0: case 0x1: case 0x2: case 0x3:
  6493. err = arm_decode_dp_misc (gdbarch, insn, regs, dsc);
  6494. break;
  6495. case 0x4: case 0x5: case 0x6:
  6496. err = arm_decode_ld_st_word_ubyte (gdbarch, insn, regs, dsc);
  6497. break;
  6498. case 0x7:
  6499. err = arm_decode_media (gdbarch, insn, dsc);
  6500. break;
  6501. case 0x8: case 0x9: case 0xa: case 0xb:
  6502. err = arm_decode_b_bl_ldmstm (gdbarch, insn, regs, dsc);
  6503. break;
  6504. case 0xc: case 0xd: case 0xe: case 0xf:
  6505. err = arm_decode_svc_copro (gdbarch, insn, regs, dsc);
  6506. break;
  6507. }
  6508. if (err)
  6509. internal_error (__FILE__, __LINE__,
  6510. _("arm_process_displaced_insn: Instruction decode error"));
  6511. }
  6512. /* Actually set up the scratch space for a displaced instruction. */
  6513. void
  6514. arm_displaced_init_closure (struct gdbarch *gdbarch, CORE_ADDR from,
  6515. CORE_ADDR to,
  6516. arm_displaced_step_copy_insn_closure *dsc)
  6517. {
  6518. arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  6519. unsigned int i, len, offset;
  6520. enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
  6521. int size = dsc->is_thumb? 2 : 4;
  6522. const gdb_byte *bkp_insn;
  6523. offset = 0;
  6524. /* Poke modified instruction(s). */
  6525. for (i = 0; i < dsc->numinsns; i++)
  6526. {
  6527. if (size == 4)
  6528. displaced_debug_printf ("writing insn %.8lx at %.8lx",
  6529. dsc->modinsn[i], (unsigned long) to + offset);
  6530. else if (size == 2)
  6531. displaced_debug_printf ("writing insn %.4x at %.8lx",
  6532. (unsigned short) dsc->modinsn[i],
  6533. (unsigned long) to + offset);
  6534. write_memory_unsigned_integer (to + offset, size,
  6535. byte_order_for_code,
  6536. dsc->modinsn[i]);
  6537. offset += size;
  6538. }
  6539. /* Choose the correct breakpoint instruction. */
  6540. if (dsc->is_thumb)
  6541. {
  6542. bkp_insn = tdep->thumb_breakpoint;
  6543. len = tdep->thumb_breakpoint_size;
  6544. }
  6545. else
  6546. {
  6547. bkp_insn = tdep->arm_breakpoint;
  6548. len = tdep->arm_breakpoint_size;
  6549. }
  6550. /* Put breakpoint afterwards. */
  6551. write_memory (to + offset, bkp_insn, len);
  6552. displaced_debug_printf ("copy %s->%s", paddress (gdbarch, from),
  6553. paddress (gdbarch, to));
  6554. }
  6555. /* Entry point for cleaning things up after a displaced instruction has been
  6556. single-stepped. */
  6557. void
  6558. arm_displaced_step_fixup (struct gdbarch *gdbarch,
  6559. struct displaced_step_copy_insn_closure *dsc_,
  6560. CORE_ADDR from, CORE_ADDR to,
  6561. struct regcache *regs)
  6562. {
  6563. arm_displaced_step_copy_insn_closure *dsc
  6564. = (arm_displaced_step_copy_insn_closure *) dsc_;
  6565. if (dsc->cleanup)
  6566. dsc->cleanup (gdbarch, regs, dsc);
  6567. if (!dsc->wrote_to_pc)
  6568. regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM,
  6569. dsc->insn_addr + dsc->insn_size);
  6570. }
  6571. #include "bfd-in2.h"
  6572. #include "libcoff.h"
  6573. static int
  6574. gdb_print_insn_arm (bfd_vma memaddr, disassemble_info *info)
  6575. {
  6576. gdb_disassembler *di
  6577. = static_cast<gdb_disassembler *>(info->application_data);
  6578. struct gdbarch *gdbarch = di->arch ();
  6579. if (arm_pc_is_thumb (gdbarch, memaddr))
  6580. {
  6581. static asymbol *asym;
  6582. static combined_entry_type ce;
  6583. static struct coff_symbol_struct csym;
  6584. static struct bfd fake_bfd;
  6585. static bfd_target fake_target;
  6586. if (csym.native == NULL)
  6587. {
  6588. /* Create a fake symbol vector containing a Thumb symbol.
  6589. This is solely so that the code in print_insn_little_arm()
  6590. and print_insn_big_arm() in opcodes/arm-dis.c will detect
  6591. the presence of a Thumb symbol and switch to decoding
  6592. Thumb instructions. */
  6593. fake_target.flavour = bfd_target_coff_flavour;
  6594. fake_bfd.xvec = &fake_target;
  6595. ce.u.syment.n_sclass = C_THUMBEXTFUNC;
  6596. csym.native = &ce;
  6597. csym.symbol.the_bfd = &fake_bfd;
  6598. csym.symbol.name = "fake";
  6599. asym = (asymbol *) & csym;
  6600. }
  6601. memaddr = UNMAKE_THUMB_ADDR (memaddr);
  6602. info->symbols = &asym;
  6603. }
  6604. else
  6605. info->symbols = NULL;
  6606. /* GDB is able to get bfd_mach from the exe_bfd, info->mach is
  6607. accurate, so mark USER_SPECIFIED_MACHINE_TYPE bit. Otherwise,
  6608. opcodes/arm-dis.c:print_insn reset info->mach, and it will trigger
  6609. the assert on the mismatch of info->mach and
  6610. bfd_get_mach (current_program_space->exec_bfd ()) in
  6611. default_print_insn. */
  6612. if (current_program_space->exec_bfd () != NULL
  6613. && (current_program_space->exec_bfd ()->arch_info
  6614. == gdbarch_bfd_arch_info (gdbarch)))
  6615. info->flags |= USER_SPECIFIED_MACHINE_TYPE;
  6616. return default_print_insn (memaddr, info);
  6617. }
  6618. /* The following define instruction sequences that will cause ARM
  6619. cpu's to take an undefined instruction trap. These are used to
  6620. signal a breakpoint to GDB.
  6621. The newer ARMv4T cpu's are capable of operating in ARM or Thumb
  6622. modes. A different instruction is required for each mode. The ARM
  6623. cpu's can also be big or little endian. Thus four different
  6624. instructions are needed to support all cases.
  6625. Note: ARMv4 defines several new instructions that will take the
  6626. undefined instruction trap. ARM7TDMI is nominally ARMv4T, but does
  6627. not in fact add the new instructions. The new undefined
  6628. instructions in ARMv4 are all instructions that had no defined
  6629. behaviour in earlier chips. There is no guarantee that they will
  6630. raise an exception, but may be treated as NOP's. In practice, it
  6631. may only safe to rely on instructions matching:
  6632. 3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1
  6633. 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
  6634. C C C C 0 1 1 x x x x x x x x x x x x x x x x x x x x 1 x x x x
  6635. Even this may only true if the condition predicate is true. The
  6636. following use a condition predicate of ALWAYS so it is always TRUE.
  6637. There are other ways of forcing a breakpoint. GNU/Linux, RISC iX,
  6638. and NetBSD all use a software interrupt rather than an undefined
  6639. instruction to force a trap. This can be handled by by the
  6640. abi-specific code during establishment of the gdbarch vector. */
  6641. #define ARM_LE_BREAKPOINT {0xFE,0xDE,0xFF,0xE7}
  6642. #define ARM_BE_BREAKPOINT {0xE7,0xFF,0xDE,0xFE}
  6643. #define THUMB_LE_BREAKPOINT {0xbe,0xbe}
  6644. #define THUMB_BE_BREAKPOINT {0xbe,0xbe}
  6645. static const gdb_byte arm_default_arm_le_breakpoint[] = ARM_LE_BREAKPOINT;
  6646. static const gdb_byte arm_default_arm_be_breakpoint[] = ARM_BE_BREAKPOINT;
  6647. static const gdb_byte arm_default_thumb_le_breakpoint[] = THUMB_LE_BREAKPOINT;
  6648. static const gdb_byte arm_default_thumb_be_breakpoint[] = THUMB_BE_BREAKPOINT;
  6649. /* Implement the breakpoint_kind_from_pc gdbarch method. */
  6650. static int
  6651. arm_breakpoint_kind_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr)
  6652. {
  6653. arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  6654. enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
  6655. if (arm_pc_is_thumb (gdbarch, *pcptr))
  6656. {
  6657. *pcptr = UNMAKE_THUMB_ADDR (*pcptr);
  6658. /* If we have a separate 32-bit breakpoint instruction for Thumb-2,
  6659. check whether we are replacing a 32-bit instruction. */
  6660. if (tdep->thumb2_breakpoint != NULL)
  6661. {
  6662. gdb_byte buf[2];
  6663. if (target_read_memory (*pcptr, buf, 2) == 0)
  6664. {
  6665. unsigned short inst1;
  6666. inst1 = extract_unsigned_integer (buf, 2, byte_order_for_code);
  6667. if (thumb_insn_size (inst1) == 4)
  6668. return ARM_BP_KIND_THUMB2;
  6669. }
  6670. }
  6671. return ARM_BP_KIND_THUMB;
  6672. }
  6673. else
  6674. return ARM_BP_KIND_ARM;
  6675. }
  6676. /* Implement the sw_breakpoint_from_kind gdbarch method. */
  6677. static const gdb_byte *
  6678. arm_sw_breakpoint_from_kind (struct gdbarch *gdbarch, int kind, int *size)
  6679. {
  6680. arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  6681. switch (kind)
  6682. {
  6683. case ARM_BP_KIND_ARM:
  6684. *size = tdep->arm_breakpoint_size;
  6685. return tdep->arm_breakpoint;
  6686. case ARM_BP_KIND_THUMB:
  6687. *size = tdep->thumb_breakpoint_size;
  6688. return tdep->thumb_breakpoint;
  6689. case ARM_BP_KIND_THUMB2:
  6690. *size = tdep->thumb2_breakpoint_size;
  6691. return tdep->thumb2_breakpoint;
  6692. default:
  6693. gdb_assert_not_reached ("unexpected arm breakpoint kind");
  6694. }
  6695. }
  6696. /* Implement the breakpoint_kind_from_current_state gdbarch method. */
  6697. static int
  6698. arm_breakpoint_kind_from_current_state (struct gdbarch *gdbarch,
  6699. struct regcache *regcache,
  6700. CORE_ADDR *pcptr)
  6701. {
  6702. gdb_byte buf[4];
  6703. /* Check the memory pointed by PC is readable. */
  6704. if (target_read_memory (regcache_read_pc (regcache), buf, 4) == 0)
  6705. {
  6706. struct arm_get_next_pcs next_pcs_ctx;
  6707. arm_get_next_pcs_ctor (&next_pcs_ctx,
  6708. &arm_get_next_pcs_ops,
  6709. gdbarch_byte_order (gdbarch),
  6710. gdbarch_byte_order_for_code (gdbarch),
  6711. 0,
  6712. regcache);
  6713. std::vector<CORE_ADDR> next_pcs = arm_get_next_pcs (&next_pcs_ctx);
  6714. /* If MEMADDR is the next instruction of current pc, do the
  6715. software single step computation, and get the thumb mode by
  6716. the destination address. */
  6717. for (CORE_ADDR pc : next_pcs)
  6718. {
  6719. if (UNMAKE_THUMB_ADDR (pc) == *pcptr)
  6720. {
  6721. if (IS_THUMB_ADDR (pc))
  6722. {
  6723. *pcptr = MAKE_THUMB_ADDR (*pcptr);
  6724. return arm_breakpoint_kind_from_pc (gdbarch, pcptr);
  6725. }
  6726. else
  6727. return ARM_BP_KIND_ARM;
  6728. }
  6729. }
  6730. }
  6731. return arm_breakpoint_kind_from_pc (gdbarch, pcptr);
  6732. }
  6733. /* Extract from an array REGBUF containing the (raw) register state a
  6734. function return value of type TYPE, and copy that, in virtual
  6735. format, into VALBUF. */
  6736. static void
  6737. arm_extract_return_value (struct type *type, struct regcache *regs,
  6738. gdb_byte *valbuf)
  6739. {
  6740. struct gdbarch *gdbarch = regs->arch ();
  6741. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  6742. arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  6743. if (TYPE_CODE_FLT == type->code ())
  6744. {
  6745. switch (tdep->fp_model)
  6746. {
  6747. case ARM_FLOAT_FPA:
  6748. {
  6749. /* The value is in register F0 in internal format. We need to
  6750. extract the raw value and then convert it to the desired
  6751. internal type. */
  6752. bfd_byte tmpbuf[ARM_FP_REGISTER_SIZE];
  6753. regs->cooked_read (ARM_F0_REGNUM, tmpbuf);
  6754. target_float_convert (tmpbuf, arm_ext_type (gdbarch),
  6755. valbuf, type);
  6756. }
  6757. break;
  6758. case ARM_FLOAT_SOFT_FPA:
  6759. case ARM_FLOAT_SOFT_VFP:
  6760. /* ARM_FLOAT_VFP can arise if this is a variadic function so
  6761. not using the VFP ABI code. */
  6762. case ARM_FLOAT_VFP:
  6763. regs->cooked_read (ARM_A1_REGNUM, valbuf);
  6764. if (TYPE_LENGTH (type) > 4)
  6765. regs->cooked_read (ARM_A1_REGNUM + 1,
  6766. valbuf + ARM_INT_REGISTER_SIZE);
  6767. break;
  6768. default:
  6769. internal_error (__FILE__, __LINE__,
  6770. _("arm_extract_return_value: "
  6771. "Floating point model not supported"));
  6772. break;
  6773. }
  6774. }
  6775. else if (type->code () == TYPE_CODE_INT
  6776. || type->code () == TYPE_CODE_CHAR
  6777. || type->code () == TYPE_CODE_BOOL
  6778. || type->code () == TYPE_CODE_PTR
  6779. || TYPE_IS_REFERENCE (type)
  6780. || type->code () == TYPE_CODE_ENUM
  6781. || is_fixed_point_type (type))
  6782. {
  6783. /* If the type is a plain integer, then the access is
  6784. straight-forward. Otherwise we have to play around a bit
  6785. more. */
  6786. int len = TYPE_LENGTH (type);
  6787. int regno = ARM_A1_REGNUM;
  6788. ULONGEST tmp;
  6789. while (len > 0)
  6790. {
  6791. /* By using store_unsigned_integer we avoid having to do
  6792. anything special for small big-endian values. */
  6793. regcache_cooked_read_unsigned (regs, regno++, &tmp);
  6794. store_unsigned_integer (valbuf,
  6795. (len > ARM_INT_REGISTER_SIZE
  6796. ? ARM_INT_REGISTER_SIZE : len),
  6797. byte_order, tmp);
  6798. len -= ARM_INT_REGISTER_SIZE;
  6799. valbuf += ARM_INT_REGISTER_SIZE;
  6800. }
  6801. }
  6802. else
  6803. {
  6804. /* For a structure or union the behaviour is as if the value had
  6805. been stored to word-aligned memory and then loaded into
  6806. registers with 32-bit load instruction(s). */
  6807. int len = TYPE_LENGTH (type);
  6808. int regno = ARM_A1_REGNUM;
  6809. bfd_byte tmpbuf[ARM_INT_REGISTER_SIZE];
  6810. while (len > 0)
  6811. {
  6812. regs->cooked_read (regno++, tmpbuf);
  6813. memcpy (valbuf, tmpbuf,
  6814. len > ARM_INT_REGISTER_SIZE ? ARM_INT_REGISTER_SIZE : len);
  6815. len -= ARM_INT_REGISTER_SIZE;
  6816. valbuf += ARM_INT_REGISTER_SIZE;
  6817. }
  6818. }
  6819. }
  6820. /* Will a function return an aggregate type in memory or in a
  6821. register? Return 0 if an aggregate type can be returned in a
  6822. register, 1 if it must be returned in memory. */
  6823. static int
  6824. arm_return_in_memory (struct gdbarch *gdbarch, struct type *type)
  6825. {
  6826. enum type_code code;
  6827. type = check_typedef (type);
  6828. /* Simple, non-aggregate types (ie not including vectors and
  6829. complex) are always returned in a register (or registers). */
  6830. code = type->code ();
  6831. if (TYPE_CODE_STRUCT != code && TYPE_CODE_UNION != code
  6832. && TYPE_CODE_ARRAY != code && TYPE_CODE_COMPLEX != code)
  6833. return 0;
  6834. if (TYPE_CODE_ARRAY == code && type->is_vector ())
  6835. {
  6836. /* Vector values should be returned using ARM registers if they
  6837. are not over 16 bytes. */
  6838. return (TYPE_LENGTH (type) > 16);
  6839. }
  6840. arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  6841. if (tdep->arm_abi != ARM_ABI_APCS)
  6842. {
  6843. /* The AAPCS says all aggregates not larger than a word are returned
  6844. in a register. */
  6845. if (TYPE_LENGTH (type) <= ARM_INT_REGISTER_SIZE
  6846. && language_pass_by_reference (type).trivially_copyable)
  6847. return 0;
  6848. return 1;
  6849. }
  6850. else
  6851. {
  6852. int nRc;
  6853. /* All aggregate types that won't fit in a register must be returned
  6854. in memory. */
  6855. if (TYPE_LENGTH (type) > ARM_INT_REGISTER_SIZE
  6856. || !language_pass_by_reference (type).trivially_copyable)
  6857. return 1;
  6858. /* In the ARM ABI, "integer" like aggregate types are returned in
  6859. registers. For an aggregate type to be integer like, its size
  6860. must be less than or equal to ARM_INT_REGISTER_SIZE and the
  6861. offset of each addressable subfield must be zero. Note that bit
  6862. fields are not addressable, and all addressable subfields of
  6863. unions always start at offset zero.
  6864. This function is based on the behaviour of GCC 2.95.1.
  6865. See: gcc/arm.c: arm_return_in_memory() for details.
  6866. Note: All versions of GCC before GCC 2.95.2 do not set up the
  6867. parameters correctly for a function returning the following
  6868. structure: struct { float f;}; This should be returned in memory,
  6869. not a register. Richard Earnshaw sent me a patch, but I do not
  6870. know of any way to detect if a function like the above has been
  6871. compiled with the correct calling convention. */
  6872. /* Assume all other aggregate types can be returned in a register.
  6873. Run a check for structures, unions and arrays. */
  6874. nRc = 0;
  6875. if ((TYPE_CODE_STRUCT == code) || (TYPE_CODE_UNION == code))
  6876. {
  6877. int i;
  6878. /* Need to check if this struct/union is "integer" like. For
  6879. this to be true, its size must be less than or equal to
  6880. ARM_INT_REGISTER_SIZE and the offset of each addressable
  6881. subfield must be zero. Note that bit fields are not
  6882. addressable, and unions always start at offset zero. If any
  6883. of the subfields is a floating point type, the struct/union
  6884. cannot be an integer type. */
  6885. /* For each field in the object, check:
  6886. 1) Is it FP? --> yes, nRc = 1;
  6887. 2) Is it addressable (bitpos != 0) and
  6888. not packed (bitsize == 0)?
  6889. --> yes, nRc = 1
  6890. */
  6891. for (i = 0; i < type->num_fields (); i++)
  6892. {
  6893. enum type_code field_type_code;
  6894. field_type_code
  6895. = check_typedef (type->field (i).type ())->code ();
  6896. /* Is it a floating point type field? */
  6897. if (field_type_code == TYPE_CODE_FLT)
  6898. {
  6899. nRc = 1;
  6900. break;
  6901. }
  6902. /* If bitpos != 0, then we have to care about it. */
  6903. if (type->field (i).loc_bitpos () != 0)
  6904. {
  6905. /* Bitfields are not addressable. If the field bitsize is
  6906. zero, then the field is not packed. Hence it cannot be
  6907. a bitfield or any other packed type. */
  6908. if (TYPE_FIELD_BITSIZE (type, i) == 0)
  6909. {
  6910. nRc = 1;
  6911. break;
  6912. }
  6913. }
  6914. }
  6915. }
  6916. return nRc;
  6917. }
  6918. }
  6919. /* Write into appropriate registers a function return value of type
  6920. TYPE, given in virtual format. */
  6921. static void
  6922. arm_store_return_value (struct type *type, struct regcache *regs,
  6923. const gdb_byte *valbuf)
  6924. {
  6925. struct gdbarch *gdbarch = regs->arch ();
  6926. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  6927. if (type->code () == TYPE_CODE_FLT)
  6928. {
  6929. gdb_byte buf[ARM_FP_REGISTER_SIZE];
  6930. arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  6931. switch (tdep->fp_model)
  6932. {
  6933. case ARM_FLOAT_FPA:
  6934. target_float_convert (valbuf, type, buf, arm_ext_type (gdbarch));
  6935. regs->cooked_write (ARM_F0_REGNUM, buf);
  6936. break;
  6937. case ARM_FLOAT_SOFT_FPA:
  6938. case ARM_FLOAT_SOFT_VFP:
  6939. /* ARM_FLOAT_VFP can arise if this is a variadic function so
  6940. not using the VFP ABI code. */
  6941. case ARM_FLOAT_VFP:
  6942. regs->cooked_write (ARM_A1_REGNUM, valbuf);
  6943. if (TYPE_LENGTH (type) > 4)
  6944. regs->cooked_write (ARM_A1_REGNUM + 1,
  6945. valbuf + ARM_INT_REGISTER_SIZE);
  6946. break;
  6947. default:
  6948. internal_error (__FILE__, __LINE__,
  6949. _("arm_store_return_value: Floating "
  6950. "point model not supported"));
  6951. break;
  6952. }
  6953. }
  6954. else if (type->code () == TYPE_CODE_INT
  6955. || type->code () == TYPE_CODE_CHAR
  6956. || type->code () == TYPE_CODE_BOOL
  6957. || type->code () == TYPE_CODE_PTR
  6958. || TYPE_IS_REFERENCE (type)
  6959. || type->code () == TYPE_CODE_ENUM)
  6960. {
  6961. if (TYPE_LENGTH (type) <= 4)
  6962. {
  6963. /* Values of one word or less are zero/sign-extended and
  6964. returned in r0. */
  6965. bfd_byte tmpbuf[ARM_INT_REGISTER_SIZE];
  6966. LONGEST val = unpack_long (type, valbuf);
  6967. store_signed_integer (tmpbuf, ARM_INT_REGISTER_SIZE, byte_order, val);
  6968. regs->cooked_write (ARM_A1_REGNUM, tmpbuf);
  6969. }
  6970. else
  6971. {
  6972. /* Integral values greater than one word are stored in consecutive
  6973. registers starting with r0. This will always be a multiple of
  6974. the regiser size. */
  6975. int len = TYPE_LENGTH (type);
  6976. int regno = ARM_A1_REGNUM;
  6977. while (len > 0)
  6978. {
  6979. regs->cooked_write (regno++, valbuf);
  6980. len -= ARM_INT_REGISTER_SIZE;
  6981. valbuf += ARM_INT_REGISTER_SIZE;
  6982. }
  6983. }
  6984. }
  6985. else
  6986. {
  6987. /* For a structure or union the behaviour is as if the value had
  6988. been stored to word-aligned memory and then loaded into
  6989. registers with 32-bit load instruction(s). */
  6990. int len = TYPE_LENGTH (type);
  6991. int regno = ARM_A1_REGNUM;
  6992. bfd_byte tmpbuf[ARM_INT_REGISTER_SIZE];
  6993. while (len > 0)
  6994. {
  6995. memcpy (tmpbuf, valbuf,
  6996. len > ARM_INT_REGISTER_SIZE ? ARM_INT_REGISTER_SIZE : len);
  6997. regs->cooked_write (regno++, tmpbuf);
  6998. len -= ARM_INT_REGISTER_SIZE;
  6999. valbuf += ARM_INT_REGISTER_SIZE;
  7000. }
  7001. }
  7002. }
  7003. /* Handle function return values. */
  7004. static enum return_value_convention
  7005. arm_return_value (struct gdbarch *gdbarch, struct value *function,
  7006. struct type *valtype, struct regcache *regcache,
  7007. gdb_byte *readbuf, const gdb_byte *writebuf)
  7008. {
  7009. arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  7010. struct type *func_type = function ? value_type (function) : NULL;
  7011. enum arm_vfp_cprc_base_type vfp_base_type;
  7012. int vfp_base_count;
  7013. if (arm_vfp_abi_for_function (gdbarch, func_type)
  7014. && arm_vfp_call_candidate (valtype, &vfp_base_type, &vfp_base_count))
  7015. {
  7016. int reg_char = arm_vfp_cprc_reg_char (vfp_base_type);
  7017. int unit_length = arm_vfp_cprc_unit_length (vfp_base_type);
  7018. int i;
  7019. for (i = 0; i < vfp_base_count; i++)
  7020. {
  7021. if (reg_char == 'q')
  7022. {
  7023. if (writebuf)
  7024. arm_neon_quad_write (gdbarch, regcache, i,
  7025. writebuf + i * unit_length);
  7026. if (readbuf)
  7027. arm_neon_quad_read (gdbarch, regcache, i,
  7028. readbuf + i * unit_length);
  7029. }
  7030. else
  7031. {
  7032. char name_buf[4];
  7033. int regnum;
  7034. xsnprintf (name_buf, sizeof (name_buf), "%c%d", reg_char, i);
  7035. regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
  7036. strlen (name_buf));
  7037. if (writebuf)
  7038. regcache->cooked_write (regnum, writebuf + i * unit_length);
  7039. if (readbuf)
  7040. regcache->cooked_read (regnum, readbuf + i * unit_length);
  7041. }
  7042. }
  7043. return RETURN_VALUE_REGISTER_CONVENTION;
  7044. }
  7045. if (valtype->code () == TYPE_CODE_STRUCT
  7046. || valtype->code () == TYPE_CODE_UNION
  7047. || valtype->code () == TYPE_CODE_ARRAY)
  7048. {
  7049. /* From the AAPCS document:
  7050. Result return:
  7051. A Composite Type larger than 4 bytes, or whose size cannot be
  7052. determined statically by both caller and callee, is stored in memory
  7053. at an address passed as an extra argument when the function was
  7054. called (Parameter Passing, rule A.4). The memory to be used for the
  7055. result may be modified at any point during the function call.
  7056. Parameter Passing:
  7057. A.4: If the subroutine is a function that returns a result in memory,
  7058. then the address for the result is placed in r0 and the NCRN is set
  7059. to r1. */
  7060. if (tdep->struct_return == pcc_struct_return
  7061. || arm_return_in_memory (gdbarch, valtype))
  7062. {
  7063. if (readbuf)
  7064. {
  7065. CORE_ADDR addr;
  7066. regcache->cooked_read (ARM_A1_REGNUM, &addr);
  7067. read_memory (addr, readbuf, TYPE_LENGTH (valtype));
  7068. }
  7069. return RETURN_VALUE_ABI_RETURNS_ADDRESS;
  7070. }
  7071. }
  7072. else if (valtype->code () == TYPE_CODE_COMPLEX)
  7073. {
  7074. if (arm_return_in_memory (gdbarch, valtype))
  7075. return RETURN_VALUE_STRUCT_CONVENTION;
  7076. }
  7077. if (writebuf)
  7078. arm_store_return_value (valtype, regcache, writebuf);
  7079. if (readbuf)
  7080. arm_extract_return_value (valtype, regcache, readbuf);
  7081. return RETURN_VALUE_REGISTER_CONVENTION;
  7082. }
  7083. static int
  7084. arm_get_longjmp_target (struct frame_info *frame, CORE_ADDR *pc)
  7085. {
  7086. struct gdbarch *gdbarch = get_frame_arch (frame);
  7087. arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  7088. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  7089. CORE_ADDR jb_addr;
  7090. gdb_byte buf[ARM_INT_REGISTER_SIZE];
  7091. jb_addr = get_frame_register_unsigned (frame, ARM_A1_REGNUM);
  7092. if (target_read_memory (jb_addr + tdep->jb_pc * tdep->jb_elt_size, buf,
  7093. ARM_INT_REGISTER_SIZE))
  7094. return 0;
  7095. *pc = extract_unsigned_integer (buf, ARM_INT_REGISTER_SIZE, byte_order);
  7096. return 1;
  7097. }
  7098. /* A call to cmse secure entry function "foo" at "a" is modified by
  7099. GNU ld as "b".
  7100. a) bl xxxx <foo>
  7101. <foo>
  7102. xxxx:
  7103. b) bl yyyy <__acle_se_foo>
  7104. section .gnu.sgstubs:
  7105. <foo>
  7106. yyyy: sg // secure gateway
  7107. b.w xxxx <__acle_se_foo> // original_branch_dest
  7108. <__acle_se_foo>
  7109. xxxx:
  7110. When the control at "b", the pc contains "yyyy" (sg address) which is a
  7111. trampoline and does not exist in source code. This function returns the
  7112. target pc "xxxx". For more details please refer to section 5.4
  7113. (Entry functions) and section 3.4.4 (C level development flow of secure code)
  7114. of "armv8-m-security-extensions-requirements-on-development-tools-engineering-specification"
  7115. document on www.developer.arm.com. */
  7116. static CORE_ADDR
  7117. arm_skip_cmse_entry (CORE_ADDR pc, const char *name, struct objfile *objfile)
  7118. {
  7119. int target_len = strlen (name) + strlen ("__acle_se_") + 1;
  7120. char *target_name = (char *) alloca (target_len);
  7121. xsnprintf (target_name, target_len, "%s%s", "__acle_se_", name);
  7122. struct bound_minimal_symbol minsym
  7123. = lookup_minimal_symbol (target_name, NULL, objfile);
  7124. if (minsym.minsym != nullptr)
  7125. return BMSYMBOL_VALUE_ADDRESS (minsym);
  7126. return 0;
  7127. }
  7128. /* Return true when SEC points to ".gnu.sgstubs" section. */
  7129. static bool
  7130. arm_is_sgstubs_section (struct obj_section *sec)
  7131. {
  7132. return (sec != nullptr
  7133. && sec->the_bfd_section != nullptr
  7134. && sec->the_bfd_section->name != nullptr
  7135. && streq (sec->the_bfd_section->name, ".gnu.sgstubs"));
  7136. }
  7137. /* Recognize GCC and GNU ld's trampolines. If we are in a trampoline,
  7138. return the target PC. Otherwise return 0. */
  7139. CORE_ADDR
  7140. arm_skip_stub (struct frame_info *frame, CORE_ADDR pc)
  7141. {
  7142. const char *name;
  7143. int namelen;
  7144. CORE_ADDR start_addr;
  7145. /* Find the starting address and name of the function containing the PC. */
  7146. if (find_pc_partial_function (pc, &name, &start_addr, NULL) == 0)
  7147. {
  7148. /* Trampoline 'bx reg' doesn't belong to any functions. Do the
  7149. check here. */
  7150. start_addr = arm_skip_bx_reg (frame, pc);
  7151. if (start_addr != 0)
  7152. return start_addr;
  7153. return 0;
  7154. }
  7155. /* If PC is in a Thumb call or return stub, return the address of the
  7156. target PC, which is in a register. The thunk functions are called
  7157. _call_via_xx, where x is the register name. The possible names
  7158. are r0-r9, sl, fp, ip, sp, and lr. ARM RealView has similar
  7159. functions, named __ARM_call_via_r[0-7]. */
  7160. if (startswith (name, "_call_via_")
  7161. || startswith (name, "__ARM_call_via_"))
  7162. {
  7163. /* Use the name suffix to determine which register contains the
  7164. target PC. */
  7165. static const char *table[15] =
  7166. {"r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
  7167. "r8", "r9", "sl", "fp", "ip", "sp", "lr"
  7168. };
  7169. int regno;
  7170. int offset = strlen (name) - 2;
  7171. for (regno = 0; regno <= 14; regno++)
  7172. if (strcmp (&name[offset], table[regno]) == 0)
  7173. return get_frame_register_unsigned (frame, regno);
  7174. }
  7175. /* GNU ld generates __foo_from_arm or __foo_from_thumb for
  7176. non-interworking calls to foo. We could decode the stubs
  7177. to find the target but it's easier to use the symbol table. */
  7178. namelen = strlen (name);
  7179. if (name[0] == '_' && name[1] == '_'
  7180. && ((namelen > 2 + strlen ("_from_thumb")
  7181. && startswith (name + namelen - strlen ("_from_thumb"), "_from_thumb"))
  7182. || (namelen > 2 + strlen ("_from_arm")
  7183. && startswith (name + namelen - strlen ("_from_arm"), "_from_arm"))))
  7184. {
  7185. char *target_name;
  7186. int target_len = namelen - 2;
  7187. struct bound_minimal_symbol minsym;
  7188. struct objfile *objfile;
  7189. struct obj_section *sec;
  7190. if (name[namelen - 1] == 'b')
  7191. target_len -= strlen ("_from_thumb");
  7192. else
  7193. target_len -= strlen ("_from_arm");
  7194. target_name = (char *) alloca (target_len + 1);
  7195. memcpy (target_name, name + 2, target_len);
  7196. target_name[target_len] = '\0';
  7197. sec = find_pc_section (pc);
  7198. objfile = (sec == NULL) ? NULL : sec->objfile;
  7199. minsym = lookup_minimal_symbol (target_name, NULL, objfile);
  7200. if (minsym.minsym != NULL)
  7201. return BMSYMBOL_VALUE_ADDRESS (minsym);
  7202. else
  7203. return 0;
  7204. }
  7205. struct obj_section *section = find_pc_section (pc);
  7206. /* Check whether SECTION points to the ".gnu.sgstubs" section. */
  7207. if (arm_is_sgstubs_section (section))
  7208. return arm_skip_cmse_entry (pc, name, section->objfile);
  7209. return 0; /* not a stub */
  7210. }
  7211. static void
  7212. arm_update_current_architecture (void)
  7213. {
  7214. /* If the current architecture is not ARM, we have nothing to do. */
  7215. if (gdbarch_bfd_arch_info (target_gdbarch ())->arch != bfd_arch_arm)
  7216. return;
  7217. /* Update the architecture. */
  7218. gdbarch_info info;
  7219. if (!gdbarch_update_p (info))
  7220. internal_error (__FILE__, __LINE__, _("could not update architecture"));
  7221. }
  7222. static void
  7223. set_fp_model_sfunc (const char *args, int from_tty,
  7224. struct cmd_list_element *c)
  7225. {
  7226. int fp_model;
  7227. for (fp_model = ARM_FLOAT_AUTO; fp_model != ARM_FLOAT_LAST; fp_model++)
  7228. if (strcmp (current_fp_model, fp_model_strings[fp_model]) == 0)
  7229. {
  7230. arm_fp_model = (enum arm_float_model) fp_model;
  7231. break;
  7232. }
  7233. if (fp_model == ARM_FLOAT_LAST)
  7234. internal_error (__FILE__, __LINE__, _("Invalid fp model accepted: %s."),
  7235. current_fp_model);
  7236. arm_update_current_architecture ();
  7237. }
  7238. static void
  7239. show_fp_model (struct ui_file *file, int from_tty,
  7240. struct cmd_list_element *c, const char *value)
  7241. {
  7242. arm_gdbarch_tdep *tdep
  7243. = (arm_gdbarch_tdep *) gdbarch_tdep (target_gdbarch ());
  7244. if (arm_fp_model == ARM_FLOAT_AUTO
  7245. && gdbarch_bfd_arch_info (target_gdbarch ())->arch == bfd_arch_arm)
  7246. gdb_printf (file, _("\
  7247. The current ARM floating point model is \"auto\" (currently \"%s\").\n"),
  7248. fp_model_strings[tdep->fp_model]);
  7249. else
  7250. gdb_printf (file, _("\
  7251. The current ARM floating point model is \"%s\".\n"),
  7252. fp_model_strings[arm_fp_model]);
  7253. }
  7254. static void
  7255. arm_set_abi (const char *args, int from_tty,
  7256. struct cmd_list_element *c)
  7257. {
  7258. int arm_abi;
  7259. for (arm_abi = ARM_ABI_AUTO; arm_abi != ARM_ABI_LAST; arm_abi++)
  7260. if (strcmp (arm_abi_string, arm_abi_strings[arm_abi]) == 0)
  7261. {
  7262. arm_abi_global = (enum arm_abi_kind) arm_abi;
  7263. break;
  7264. }
  7265. if (arm_abi == ARM_ABI_LAST)
  7266. internal_error (__FILE__, __LINE__, _("Invalid ABI accepted: %s."),
  7267. arm_abi_string);
  7268. arm_update_current_architecture ();
  7269. }
  7270. static void
  7271. arm_show_abi (struct ui_file *file, int from_tty,
  7272. struct cmd_list_element *c, const char *value)
  7273. {
  7274. arm_gdbarch_tdep *tdep
  7275. = (arm_gdbarch_tdep *) gdbarch_tdep (target_gdbarch ());
  7276. if (arm_abi_global == ARM_ABI_AUTO
  7277. && gdbarch_bfd_arch_info (target_gdbarch ())->arch == bfd_arch_arm)
  7278. gdb_printf (file, _("\
  7279. The current ARM ABI is \"auto\" (currently \"%s\").\n"),
  7280. arm_abi_strings[tdep->arm_abi]);
  7281. else
  7282. gdb_printf (file, _("The current ARM ABI is \"%s\".\n"),
  7283. arm_abi_string);
  7284. }
  7285. static void
  7286. arm_show_fallback_mode (struct ui_file *file, int from_tty,
  7287. struct cmd_list_element *c, const char *value)
  7288. {
  7289. gdb_printf (file,
  7290. _("The current execution mode assumed "
  7291. "(when symbols are unavailable) is \"%s\".\n"),
  7292. arm_fallback_mode_string);
  7293. }
  7294. static void
  7295. arm_show_force_mode (struct ui_file *file, int from_tty,
  7296. struct cmd_list_element *c, const char *value)
  7297. {
  7298. gdb_printf (file,
  7299. _("The current execution mode assumed "
  7300. "(even when symbols are available) is \"%s\".\n"),
  7301. arm_force_mode_string);
  7302. }
  7303. /* If the user changes the register disassembly style used for info
  7304. register and other commands, we have to also switch the style used
  7305. in opcodes for disassembly output. This function is run in the "set
  7306. arm disassembly" command, and does that. */
  7307. static void
  7308. set_disassembly_style_sfunc (const char *args, int from_tty,
  7309. struct cmd_list_element *c)
  7310. {
  7311. /* Convert the short style name into the long style name (eg, reg-names-*)
  7312. before calling the generic set_disassembler_options() function. */
  7313. std::string long_name = std::string ("reg-names-") + disassembly_style;
  7314. set_disassembler_options (&long_name[0]);
  7315. }
  7316. static void
  7317. show_disassembly_style_sfunc (struct ui_file *file, int from_tty,
  7318. struct cmd_list_element *c, const char *value)
  7319. {
  7320. struct gdbarch *gdbarch = get_current_arch ();
  7321. char *options = get_disassembler_options (gdbarch);
  7322. const char *style = "";
  7323. int len = 0;
  7324. const char *opt;
  7325. FOR_EACH_DISASSEMBLER_OPTION (opt, options)
  7326. if (startswith (opt, "reg-names-"))
  7327. {
  7328. style = &opt[strlen ("reg-names-")];
  7329. len = strcspn (style, ",");
  7330. }
  7331. gdb_printf (file, "The disassembly style is \"%.*s\".\n", len, style);
  7332. }
  7333. /* Return the ARM register name corresponding to register I. */
  7334. static const char *
  7335. arm_register_name (struct gdbarch *gdbarch, int i)
  7336. {
  7337. arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  7338. if (is_s_pseudo (gdbarch, i))
  7339. {
  7340. static const char *const s_pseudo_names[] = {
  7341. "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7",
  7342. "s8", "s9", "s10", "s11", "s12", "s13", "s14", "s15",
  7343. "s16", "s17", "s18", "s19", "s20", "s21", "s22", "s23",
  7344. "s24", "s25", "s26", "s27", "s28", "s29", "s30", "s31",
  7345. };
  7346. return s_pseudo_names[i - tdep->s_pseudo_base];
  7347. }
  7348. if (is_q_pseudo (gdbarch, i))
  7349. {
  7350. static const char *const q_pseudo_names[] = {
  7351. "q0", "q1", "q2", "q3", "q4", "q5", "q6", "q7",
  7352. "q8", "q9", "q10", "q11", "q12", "q13", "q14", "q15",
  7353. };
  7354. return q_pseudo_names[i - tdep->q_pseudo_base];
  7355. }
  7356. if (is_mve_pseudo (gdbarch, i))
  7357. return "p0";
  7358. /* RA_AUTH_CODE is used for unwinding only. Do not assign it a name. */
  7359. if (is_pacbti_pseudo (gdbarch, i))
  7360. return "";
  7361. if (i >= ARRAY_SIZE (arm_register_names))
  7362. /* These registers are only supported on targets which supply
  7363. an XML description. */
  7364. return "";
  7365. /* Non-pseudo registers. */
  7366. return arm_register_names[i];
  7367. }
  7368. /* Test whether the coff symbol specific value corresponds to a Thumb
  7369. function. */
  7370. static int
  7371. coff_sym_is_thumb (int val)
  7372. {
  7373. return (val == C_THUMBEXT
  7374. || val == C_THUMBSTAT
  7375. || val == C_THUMBEXTFUNC
  7376. || val == C_THUMBSTATFUNC
  7377. || val == C_THUMBLABEL);
  7378. }
  7379. /* arm_coff_make_msymbol_special()
  7380. arm_elf_make_msymbol_special()
  7381. These functions test whether the COFF or ELF symbol corresponds to
  7382. an address in thumb code, and set a "special" bit in a minimal
  7383. symbol to indicate that it does. */
  7384. static void
  7385. arm_elf_make_msymbol_special(asymbol *sym, struct minimal_symbol *msym)
  7386. {
  7387. elf_symbol_type *elfsym = (elf_symbol_type *) sym;
  7388. if (ARM_GET_SYM_BRANCH_TYPE (elfsym->internal_elf_sym.st_target_internal)
  7389. == ST_BRANCH_TO_THUMB)
  7390. MSYMBOL_SET_SPECIAL (msym);
  7391. }
  7392. static void
  7393. arm_coff_make_msymbol_special(int val, struct minimal_symbol *msym)
  7394. {
  7395. if (coff_sym_is_thumb (val))
  7396. MSYMBOL_SET_SPECIAL (msym);
  7397. }
  7398. static void
  7399. arm_record_special_symbol (struct gdbarch *gdbarch, struct objfile *objfile,
  7400. asymbol *sym)
  7401. {
  7402. const char *name = bfd_asymbol_name (sym);
  7403. struct arm_per_bfd *data;
  7404. struct arm_mapping_symbol new_map_sym;
  7405. gdb_assert (name[0] == '$');
  7406. if (name[1] != 'a' && name[1] != 't' && name[1] != 'd')
  7407. return;
  7408. data = arm_bfd_data_key.get (objfile->obfd);
  7409. if (data == NULL)
  7410. data = arm_bfd_data_key.emplace (objfile->obfd,
  7411. objfile->obfd->section_count);
  7412. arm_mapping_symbol_vec &map
  7413. = data->section_maps[bfd_asymbol_section (sym)->index];
  7414. new_map_sym.value = sym->value;
  7415. new_map_sym.type = name[1];
  7416. /* Insert at the end, the vector will be sorted on first use. */
  7417. map.push_back (new_map_sym);
  7418. }
  7419. static void
  7420. arm_write_pc (struct regcache *regcache, CORE_ADDR pc)
  7421. {
  7422. struct gdbarch *gdbarch = regcache->arch ();
  7423. regcache_cooked_write_unsigned (regcache, ARM_PC_REGNUM, pc);
  7424. /* If necessary, set the T bit. */
  7425. if (arm_apcs_32)
  7426. {
  7427. ULONGEST val, t_bit;
  7428. regcache_cooked_read_unsigned (regcache, ARM_PS_REGNUM, &val);
  7429. t_bit = arm_psr_thumb_bit (gdbarch);
  7430. if (arm_pc_is_thumb (gdbarch, pc))
  7431. regcache_cooked_write_unsigned (regcache, ARM_PS_REGNUM,
  7432. val | t_bit);
  7433. else
  7434. regcache_cooked_write_unsigned (regcache, ARM_PS_REGNUM,
  7435. val & ~t_bit);
  7436. }
  7437. }
  7438. /* Read the contents of a NEON quad register, by reading from two
  7439. double registers. This is used to implement the quad pseudo
  7440. registers, and for argument passing in case the quad registers are
  7441. missing; vectors are passed in quad registers when using the VFP
  7442. ABI, even if a NEON unit is not present. REGNUM is the index of
  7443. the quad register, in [0, 15]. */
  7444. static enum register_status
  7445. arm_neon_quad_read (struct gdbarch *gdbarch, readable_regcache *regcache,
  7446. int regnum, gdb_byte *buf)
  7447. {
  7448. char name_buf[4];
  7449. gdb_byte reg_buf[8];
  7450. int offset, double_regnum;
  7451. enum register_status status;
  7452. xsnprintf (name_buf, sizeof (name_buf), "d%d", regnum << 1);
  7453. double_regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
  7454. strlen (name_buf));
  7455. /* d0 is always the least significant half of q0. */
  7456. if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
  7457. offset = 8;
  7458. else
  7459. offset = 0;
  7460. status = regcache->raw_read (double_regnum, reg_buf);
  7461. if (status != REG_VALID)
  7462. return status;
  7463. memcpy (buf + offset, reg_buf, 8);
  7464. offset = 8 - offset;
  7465. status = regcache->raw_read (double_regnum + 1, reg_buf);
  7466. if (status != REG_VALID)
  7467. return status;
  7468. memcpy (buf + offset, reg_buf, 8);
  7469. return REG_VALID;
  7470. }
  7471. /* Read the contents of the MVE pseudo register REGNUM and store it
  7472. in BUF. */
  7473. static enum register_status
  7474. arm_mve_pseudo_read (struct gdbarch *gdbarch, readable_regcache *regcache,
  7475. int regnum, gdb_byte *buf)
  7476. {
  7477. arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  7478. /* P0 is the first 16 bits of VPR. */
  7479. return regcache->raw_read_part (tdep->mve_vpr_regnum, 0, 2, buf);
  7480. }
  7481. static enum register_status
  7482. arm_pseudo_read (struct gdbarch *gdbarch, readable_regcache *regcache,
  7483. int regnum, gdb_byte *buf)
  7484. {
  7485. const int num_regs = gdbarch_num_regs (gdbarch);
  7486. char name_buf[4];
  7487. gdb_byte reg_buf[8];
  7488. int offset, double_regnum;
  7489. arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  7490. gdb_assert (regnum >= num_regs);
  7491. if (is_q_pseudo (gdbarch, regnum))
  7492. {
  7493. /* Quad-precision register. */
  7494. return arm_neon_quad_read (gdbarch, regcache,
  7495. regnum - tdep->q_pseudo_base, buf);
  7496. }
  7497. else if (is_mve_pseudo (gdbarch, regnum))
  7498. return arm_mve_pseudo_read (gdbarch, regcache, regnum, buf);
  7499. else
  7500. {
  7501. enum register_status status;
  7502. regnum -= tdep->s_pseudo_base;
  7503. /* Single-precision register. */
  7504. gdb_assert (regnum < 32);
  7505. /* s0 is always the least significant half of d0. */
  7506. if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
  7507. offset = (regnum & 1) ? 0 : 4;
  7508. else
  7509. offset = (regnum & 1) ? 4 : 0;
  7510. xsnprintf (name_buf, sizeof (name_buf), "d%d", regnum >> 1);
  7511. double_regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
  7512. strlen (name_buf));
  7513. status = regcache->raw_read (double_regnum, reg_buf);
  7514. if (status == REG_VALID)
  7515. memcpy (buf, reg_buf + offset, 4);
  7516. return status;
  7517. }
  7518. }
  7519. /* Store the contents of BUF to a NEON quad register, by writing to
  7520. two double registers. This is used to implement the quad pseudo
  7521. registers, and for argument passing in case the quad registers are
  7522. missing; vectors are passed in quad registers when using the VFP
  7523. ABI, even if a NEON unit is not present. REGNUM is the index
  7524. of the quad register, in [0, 15]. */
  7525. static void
  7526. arm_neon_quad_write (struct gdbarch *gdbarch, struct regcache *regcache,
  7527. int regnum, const gdb_byte *buf)
  7528. {
  7529. char name_buf[4];
  7530. int offset, double_regnum;
  7531. xsnprintf (name_buf, sizeof (name_buf), "d%d", regnum << 1);
  7532. double_regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
  7533. strlen (name_buf));
  7534. /* d0 is always the least significant half of q0. */
  7535. if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
  7536. offset = 8;
  7537. else
  7538. offset = 0;
  7539. regcache->raw_write (double_regnum, buf + offset);
  7540. offset = 8 - offset;
  7541. regcache->raw_write (double_regnum + 1, buf + offset);
  7542. }
  7543. /* Store the contents of BUF to the MVE pseudo register REGNUM. */
  7544. static void
  7545. arm_mve_pseudo_write (struct gdbarch *gdbarch, struct regcache *regcache,
  7546. int regnum, const gdb_byte *buf)
  7547. {
  7548. arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  7549. /* P0 is the first 16 bits of VPR. */
  7550. regcache->raw_write_part (tdep->mve_vpr_regnum, 0, 2, buf);
  7551. }
  7552. static void
  7553. arm_pseudo_write (struct gdbarch *gdbarch, struct regcache *regcache,
  7554. int regnum, const gdb_byte *buf)
  7555. {
  7556. const int num_regs = gdbarch_num_regs (gdbarch);
  7557. char name_buf[4];
  7558. gdb_byte reg_buf[8];
  7559. int offset, double_regnum;
  7560. arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  7561. gdb_assert (regnum >= num_regs);
  7562. if (is_q_pseudo (gdbarch, regnum))
  7563. {
  7564. /* Quad-precision register. */
  7565. arm_neon_quad_write (gdbarch, regcache,
  7566. regnum - tdep->q_pseudo_base, buf);
  7567. }
  7568. else if (is_mve_pseudo (gdbarch, regnum))
  7569. arm_mve_pseudo_write (gdbarch, regcache, regnum, buf);
  7570. else
  7571. {
  7572. regnum -= tdep->s_pseudo_base;
  7573. /* Single-precision register. */
  7574. gdb_assert (regnum < 32);
  7575. /* s0 is always the least significant half of d0. */
  7576. if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
  7577. offset = (regnum & 1) ? 0 : 4;
  7578. else
  7579. offset = (regnum & 1) ? 4 : 0;
  7580. xsnprintf (name_buf, sizeof (name_buf), "d%d", regnum >> 1);
  7581. double_regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
  7582. strlen (name_buf));
  7583. regcache->raw_read (double_regnum, reg_buf);
  7584. memcpy (reg_buf + offset, buf, 4);
  7585. regcache->raw_write (double_regnum, reg_buf);
  7586. }
  7587. }
  7588. static struct value *
  7589. value_of_arm_user_reg (struct frame_info *frame, const void *baton)
  7590. {
  7591. const int *reg_p = (const int *) baton;
  7592. return value_of_register (*reg_p, frame);
  7593. }
  7594. static enum gdb_osabi
  7595. arm_elf_osabi_sniffer (bfd *abfd)
  7596. {
  7597. unsigned int elfosabi;
  7598. enum gdb_osabi osabi = GDB_OSABI_UNKNOWN;
  7599. elfosabi = elf_elfheader (abfd)->e_ident[EI_OSABI];
  7600. if (elfosabi == ELFOSABI_ARM)
  7601. /* GNU tools use this value. Check note sections in this case,
  7602. as well. */
  7603. {
  7604. for (asection *sect : gdb_bfd_sections (abfd))
  7605. generic_elf_osabi_sniff_abi_tag_sections (abfd, sect, &osabi);
  7606. }
  7607. /* Anything else will be handled by the generic ELF sniffer. */
  7608. return osabi;
  7609. }
  7610. static int
  7611. arm_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
  7612. const struct reggroup *group)
  7613. {
  7614. /* FPS register's type is INT, but belongs to float_reggroup. Beside
  7615. this, FPS register belongs to save_regroup, restore_reggroup, and
  7616. all_reggroup, of course. */
  7617. if (regnum == ARM_FPS_REGNUM)
  7618. return (group == float_reggroup
  7619. || group == save_reggroup
  7620. || group == restore_reggroup
  7621. || group == all_reggroup);
  7622. else
  7623. return default_register_reggroup_p (gdbarch, regnum, group);
  7624. }
  7625. /* For backward-compatibility we allow two 'g' packet lengths with
  7626. the remote protocol depending on whether FPA registers are
  7627. supplied. M-profile targets do not have FPA registers, but some
  7628. stubs already exist in the wild which use a 'g' packet which
  7629. supplies them albeit with dummy values. The packet format which
  7630. includes FPA registers should be considered deprecated for
  7631. M-profile targets. */
  7632. static void
  7633. arm_register_g_packet_guesses (struct gdbarch *gdbarch)
  7634. {
  7635. arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  7636. if (tdep->is_m)
  7637. {
  7638. const target_desc *tdesc;
  7639. /* If we know from the executable this is an M-profile target,
  7640. cater for remote targets whose register set layout is the
  7641. same as the FPA layout. */
  7642. tdesc = arm_read_mprofile_description (ARM_M_TYPE_WITH_FPA);
  7643. register_remote_g_packet_guess (gdbarch,
  7644. ARM_CORE_REGS_SIZE + ARM_FP_REGS_SIZE,
  7645. tdesc);
  7646. /* The regular M-profile layout. */
  7647. tdesc = arm_read_mprofile_description (ARM_M_TYPE_M_PROFILE);
  7648. register_remote_g_packet_guess (gdbarch, ARM_CORE_REGS_SIZE,
  7649. tdesc);
  7650. /* M-profile plus M4F VFP. */
  7651. tdesc = arm_read_mprofile_description (ARM_M_TYPE_VFP_D16);
  7652. register_remote_g_packet_guess (gdbarch,
  7653. ARM_CORE_REGS_SIZE + ARM_VFP2_REGS_SIZE,
  7654. tdesc);
  7655. /* M-profile plus MVE. */
  7656. tdesc = arm_read_mprofile_description (ARM_M_TYPE_MVE);
  7657. register_remote_g_packet_guess (gdbarch, ARM_CORE_REGS_SIZE
  7658. + ARM_VFP2_REGS_SIZE
  7659. + ARM_INT_REGISTER_SIZE, tdesc);
  7660. }
  7661. /* Otherwise we don't have a useful guess. */
  7662. }
  7663. /* Implement the code_of_frame_writable gdbarch method. */
  7664. static int
  7665. arm_code_of_frame_writable (struct gdbarch *gdbarch, struct frame_info *frame)
  7666. {
  7667. arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  7668. if (tdep->is_m && get_frame_type (frame) == SIGTRAMP_FRAME)
  7669. {
  7670. /* M-profile exception frames return to some magic PCs, where
  7671. isn't writable at all. */
  7672. return 0;
  7673. }
  7674. else
  7675. return 1;
  7676. }
  7677. /* Implement gdbarch_gnu_triplet_regexp. If the arch name is arm then allow it
  7678. to be postfixed by a version (eg armv7hl). */
  7679. static const char *
  7680. arm_gnu_triplet_regexp (struct gdbarch *gdbarch)
  7681. {
  7682. if (strcmp (gdbarch_bfd_arch_info (gdbarch)->arch_name, "arm") == 0)
  7683. return "arm(v[^- ]*)?";
  7684. return gdbarch_bfd_arch_info (gdbarch)->arch_name;
  7685. }
  7686. /* Implement the "get_pc_address_flags" gdbarch method. */
  7687. static std::string
  7688. arm_get_pc_address_flags (frame_info *frame, CORE_ADDR pc)
  7689. {
  7690. if (get_frame_pc_masked (frame))
  7691. return "PAC";
  7692. return "";
  7693. }
  7694. /* Initialize the current architecture based on INFO. If possible,
  7695. re-use an architecture from ARCHES, which is a list of
  7696. architectures already created during this debugging session.
  7697. Called e.g. at program startup, when reading a core file, and when
  7698. reading a binary file. */
  7699. static struct gdbarch *
  7700. arm_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
  7701. {
  7702. struct gdbarch *gdbarch;
  7703. struct gdbarch_list *best_arch;
  7704. enum arm_abi_kind arm_abi = arm_abi_global;
  7705. enum arm_float_model fp_model = arm_fp_model;
  7706. tdesc_arch_data_up tdesc_data;
  7707. int i;
  7708. bool is_m = false;
  7709. int vfp_register_count = 0;
  7710. bool have_s_pseudos = false, have_q_pseudos = false;
  7711. bool have_wmmx_registers = false;
  7712. bool have_neon = false;
  7713. bool have_fpa_registers = true;
  7714. const struct target_desc *tdesc = info.target_desc;
  7715. bool have_vfp = false;
  7716. bool have_mve = false;
  7717. bool have_pacbti = false;
  7718. int mve_vpr_regnum = -1;
  7719. int register_count = ARM_NUM_REGS;
  7720. /* If we have an object to base this architecture on, try to determine
  7721. its ABI. */
  7722. if (arm_abi == ARM_ABI_AUTO && info.abfd != NULL)
  7723. {
  7724. int ei_osabi, e_flags;
  7725. switch (bfd_get_flavour (info.abfd))
  7726. {
  7727. case bfd_target_coff_flavour:
  7728. /* Assume it's an old APCS-style ABI. */
  7729. /* XXX WinCE? */
  7730. arm_abi = ARM_ABI_APCS;
  7731. break;
  7732. case bfd_target_elf_flavour:
  7733. ei_osabi = elf_elfheader (info.abfd)->e_ident[EI_OSABI];
  7734. e_flags = elf_elfheader (info.abfd)->e_flags;
  7735. if (ei_osabi == ELFOSABI_ARM)
  7736. {
  7737. /* GNU tools used to use this value, but do not for EABI
  7738. objects. There's nowhere to tag an EABI version
  7739. anyway, so assume APCS. */
  7740. arm_abi = ARM_ABI_APCS;
  7741. }
  7742. else if (ei_osabi == ELFOSABI_NONE || ei_osabi == ELFOSABI_GNU)
  7743. {
  7744. int eabi_ver = EF_ARM_EABI_VERSION (e_flags);
  7745. switch (eabi_ver)
  7746. {
  7747. case EF_ARM_EABI_UNKNOWN:
  7748. /* Assume GNU tools. */
  7749. arm_abi = ARM_ABI_APCS;
  7750. break;
  7751. case EF_ARM_EABI_VER4:
  7752. case EF_ARM_EABI_VER5:
  7753. arm_abi = ARM_ABI_AAPCS;
  7754. /* EABI binaries default to VFP float ordering.
  7755. They may also contain build attributes that can
  7756. be used to identify if the VFP argument-passing
  7757. ABI is in use. */
  7758. if (fp_model == ARM_FLOAT_AUTO)
  7759. {
  7760. #ifdef HAVE_ELF
  7761. switch (bfd_elf_get_obj_attr_int (info.abfd,
  7762. OBJ_ATTR_PROC,
  7763. Tag_ABI_VFP_args))
  7764. {
  7765. case AEABI_VFP_args_base:
  7766. /* "The user intended FP parameter/result
  7767. passing to conform to AAPCS, base
  7768. variant". */
  7769. fp_model = ARM_FLOAT_SOFT_VFP;
  7770. break;
  7771. case AEABI_VFP_args_vfp:
  7772. /* "The user intended FP parameter/result
  7773. passing to conform to AAPCS, VFP
  7774. variant". */
  7775. fp_model = ARM_FLOAT_VFP;
  7776. break;
  7777. case AEABI_VFP_args_toolchain:
  7778. /* "The user intended FP parameter/result
  7779. passing to conform to tool chain-specific
  7780. conventions" - we don't know any such
  7781. conventions, so leave it as "auto". */
  7782. break;
  7783. case AEABI_VFP_args_compatible:
  7784. /* "Code is compatible with both the base
  7785. and VFP variants; the user did not permit
  7786. non-variadic functions to pass FP
  7787. parameters/results" - leave it as
  7788. "auto". */
  7789. break;
  7790. default:
  7791. /* Attribute value not mentioned in the
  7792. November 2012 ABI, so leave it as
  7793. "auto". */
  7794. break;
  7795. }
  7796. #else
  7797. fp_model = ARM_FLOAT_SOFT_VFP;
  7798. #endif
  7799. }
  7800. break;
  7801. default:
  7802. /* Leave it as "auto". */
  7803. warning (_("unknown ARM EABI version 0x%x"), eabi_ver);
  7804. break;
  7805. }
  7806. #ifdef HAVE_ELF
  7807. /* Detect M-profile programs. This only works if the
  7808. executable file includes build attributes; GCC does
  7809. copy them to the executable, but e.g. RealView does
  7810. not. */
  7811. int attr_arch
  7812. = bfd_elf_get_obj_attr_int (info.abfd, OBJ_ATTR_PROC,
  7813. Tag_CPU_arch);
  7814. int attr_profile
  7815. = bfd_elf_get_obj_attr_int (info.abfd, OBJ_ATTR_PROC,
  7816. Tag_CPU_arch_profile);
  7817. /* GCC specifies the profile for v6-M; RealView only
  7818. specifies the profile for architectures starting with
  7819. V7 (as opposed to architectures with a tag
  7820. numerically greater than TAG_CPU_ARCH_V7). */
  7821. if (!tdesc_has_registers (tdesc)
  7822. && (attr_arch == TAG_CPU_ARCH_V6_M
  7823. || attr_arch == TAG_CPU_ARCH_V6S_M
  7824. || attr_arch == TAG_CPU_ARCH_V7E_M
  7825. || attr_arch == TAG_CPU_ARCH_V8M_BASE
  7826. || attr_arch == TAG_CPU_ARCH_V8M_MAIN
  7827. || attr_arch == TAG_CPU_ARCH_V8_1M_MAIN
  7828. || attr_profile == 'M'))
  7829. is_m = true;
  7830. /* Look for attributes that indicate support for ARMv8.1-m
  7831. PACBTI. */
  7832. if (!tdesc_has_registers (tdesc) && is_m)
  7833. {
  7834. int attr_pac_extension
  7835. = bfd_elf_get_obj_attr_int (info.abfd, OBJ_ATTR_PROC,
  7836. Tag_PAC_extension);
  7837. int attr_bti_extension
  7838. = bfd_elf_get_obj_attr_int (info.abfd, OBJ_ATTR_PROC,
  7839. Tag_BTI_extension);
  7840. int attr_pacret_use
  7841. = bfd_elf_get_obj_attr_int (info.abfd, OBJ_ATTR_PROC,
  7842. Tag_PACRET_use);
  7843. int attr_bti_use
  7844. = bfd_elf_get_obj_attr_int (info.abfd, OBJ_ATTR_PROC,
  7845. Tag_BTI_use);
  7846. if (attr_pac_extension != 0 || attr_bti_extension != 0
  7847. || attr_pacret_use != 0 || attr_bti_use != 0)
  7848. have_pacbti = true;
  7849. }
  7850. #endif
  7851. }
  7852. if (fp_model == ARM_FLOAT_AUTO)
  7853. {
  7854. switch (e_flags & (EF_ARM_SOFT_FLOAT | EF_ARM_VFP_FLOAT))
  7855. {
  7856. case 0:
  7857. /* Leave it as "auto". Strictly speaking this case
  7858. means FPA, but almost nobody uses that now, and
  7859. many toolchains fail to set the appropriate bits
  7860. for the floating-point model they use. */
  7861. break;
  7862. case EF_ARM_SOFT_FLOAT:
  7863. fp_model = ARM_FLOAT_SOFT_FPA;
  7864. break;
  7865. case EF_ARM_VFP_FLOAT:
  7866. fp_model = ARM_FLOAT_VFP;
  7867. break;
  7868. case EF_ARM_SOFT_FLOAT | EF_ARM_VFP_FLOAT:
  7869. fp_model = ARM_FLOAT_SOFT_VFP;
  7870. break;
  7871. }
  7872. }
  7873. if (e_flags & EF_ARM_BE8)
  7874. info.byte_order_for_code = BFD_ENDIAN_LITTLE;
  7875. break;
  7876. default:
  7877. /* Leave it as "auto". */
  7878. break;
  7879. }
  7880. }
  7881. /* Check any target description for validity. */
  7882. if (tdesc_has_registers (tdesc))
  7883. {
  7884. /* For most registers we require GDB's default names; but also allow
  7885. the numeric names for sp / lr / pc, as a convenience. */
  7886. static const char *const arm_sp_names[] = { "r13", "sp", NULL };
  7887. static const char *const arm_lr_names[] = { "r14", "lr", NULL };
  7888. static const char *const arm_pc_names[] = { "r15", "pc", NULL };
  7889. const struct tdesc_feature *feature;
  7890. int valid_p;
  7891. feature = tdesc_find_feature (tdesc,
  7892. "org.gnu.gdb.arm.core");
  7893. if (feature == NULL)
  7894. {
  7895. feature = tdesc_find_feature (tdesc,
  7896. "org.gnu.gdb.arm.m-profile");
  7897. if (feature == NULL)
  7898. return NULL;
  7899. else
  7900. is_m = true;
  7901. }
  7902. tdesc_data = tdesc_data_alloc ();
  7903. valid_p = 1;
  7904. for (i = 0; i < ARM_SP_REGNUM; i++)
  7905. valid_p &= tdesc_numbered_register (feature, tdesc_data.get (), i,
  7906. arm_register_names[i]);
  7907. valid_p &= tdesc_numbered_register_choices (feature, tdesc_data.get (),
  7908. ARM_SP_REGNUM,
  7909. arm_sp_names);
  7910. valid_p &= tdesc_numbered_register_choices (feature, tdesc_data.get (),
  7911. ARM_LR_REGNUM,
  7912. arm_lr_names);
  7913. valid_p &= tdesc_numbered_register_choices (feature, tdesc_data.get (),
  7914. ARM_PC_REGNUM,
  7915. arm_pc_names);
  7916. if (is_m)
  7917. valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
  7918. ARM_PS_REGNUM, "xpsr");
  7919. else
  7920. valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
  7921. ARM_PS_REGNUM, "cpsr");
  7922. if (!valid_p)
  7923. return NULL;
  7924. feature = tdesc_find_feature (tdesc,
  7925. "org.gnu.gdb.arm.fpa");
  7926. if (feature != NULL)
  7927. {
  7928. valid_p = 1;
  7929. for (i = ARM_F0_REGNUM; i <= ARM_FPS_REGNUM; i++)
  7930. valid_p &= tdesc_numbered_register (feature, tdesc_data.get (), i,
  7931. arm_register_names[i]);
  7932. if (!valid_p)
  7933. return NULL;
  7934. }
  7935. else
  7936. have_fpa_registers = false;
  7937. feature = tdesc_find_feature (tdesc,
  7938. "org.gnu.gdb.xscale.iwmmxt");
  7939. if (feature != NULL)
  7940. {
  7941. static const char *const iwmmxt_names[] = {
  7942. "wR0", "wR1", "wR2", "wR3", "wR4", "wR5", "wR6", "wR7",
  7943. "wR8", "wR9", "wR10", "wR11", "wR12", "wR13", "wR14", "wR15",
  7944. "wCID", "wCon", "wCSSF", "wCASF", "", "", "", "",
  7945. "wCGR0", "wCGR1", "wCGR2", "wCGR3", "", "", "", "",
  7946. };
  7947. valid_p = 1;
  7948. for (i = ARM_WR0_REGNUM; i <= ARM_WR15_REGNUM; i++)
  7949. valid_p
  7950. &= tdesc_numbered_register (feature, tdesc_data.get (), i,
  7951. iwmmxt_names[i - ARM_WR0_REGNUM]);
  7952. /* Check for the control registers, but do not fail if they
  7953. are missing. */
  7954. for (i = ARM_WC0_REGNUM; i <= ARM_WCASF_REGNUM; i++)
  7955. tdesc_numbered_register (feature, tdesc_data.get (), i,
  7956. iwmmxt_names[i - ARM_WR0_REGNUM]);
  7957. for (i = ARM_WCGR0_REGNUM; i <= ARM_WCGR3_REGNUM; i++)
  7958. valid_p
  7959. &= tdesc_numbered_register (feature, tdesc_data.get (), i,
  7960. iwmmxt_names[i - ARM_WR0_REGNUM]);
  7961. if (!valid_p)
  7962. return NULL;
  7963. have_wmmx_registers = true;
  7964. }
  7965. /* If we have a VFP unit, check whether the single precision registers
  7966. are present. If not, then we will synthesize them as pseudo
  7967. registers. */
  7968. feature = tdesc_find_feature (tdesc,
  7969. "org.gnu.gdb.arm.vfp");
  7970. if (feature != NULL)
  7971. {
  7972. static const char *const vfp_double_names[] = {
  7973. "d0", "d1", "d2", "d3", "d4", "d5", "d6", "d7",
  7974. "d8", "d9", "d10", "d11", "d12", "d13", "d14", "d15",
  7975. "d16", "d17", "d18", "d19", "d20", "d21", "d22", "d23",
  7976. "d24", "d25", "d26", "d27", "d28", "d29", "d30", "d31",
  7977. };
  7978. /* Require the double precision registers. There must be either
  7979. 16 or 32. */
  7980. valid_p = 1;
  7981. for (i = 0; i < 32; i++)
  7982. {
  7983. valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
  7984. ARM_D0_REGNUM + i,
  7985. vfp_double_names[i]);
  7986. if (!valid_p)
  7987. break;
  7988. }
  7989. if (!valid_p && i == 16)
  7990. valid_p = 1;
  7991. /* Also require FPSCR. */
  7992. valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
  7993. ARM_FPSCR_REGNUM, "fpscr");
  7994. if (!valid_p)
  7995. return NULL;
  7996. have_vfp = true;
  7997. if (tdesc_unnumbered_register (feature, "s0") == 0)
  7998. have_s_pseudos = true;
  7999. vfp_register_count = i;
  8000. /* If we have VFP, also check for NEON. The architecture allows
  8001. NEON without VFP (integer vector operations only), but GDB
  8002. does not support that. */
  8003. feature = tdesc_find_feature (tdesc,
  8004. "org.gnu.gdb.arm.neon");
  8005. if (feature != NULL)
  8006. {
  8007. /* NEON requires 32 double-precision registers. */
  8008. if (i != 32)
  8009. return NULL;
  8010. /* If there are quad registers defined by the stub, use
  8011. their type; otherwise (normally) provide them with
  8012. the default type. */
  8013. if (tdesc_unnumbered_register (feature, "q0") == 0)
  8014. have_q_pseudos = true;
  8015. }
  8016. }
  8017. /* Check for MVE after all the checks for GPR's, VFP and Neon.
  8018. MVE (Helium) is an M-profile extension. */
  8019. if (is_m)
  8020. {
  8021. /* Do we have the MVE feature? */
  8022. feature = tdesc_find_feature (tdesc,"org.gnu.gdb.arm.m-profile-mve");
  8023. if (feature != nullptr)
  8024. {
  8025. /* If we have MVE, we must always have the VPR register. */
  8026. valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
  8027. register_count, "vpr");
  8028. if (!valid_p)
  8029. {
  8030. warning (_("MVE feature is missing required register vpr."));
  8031. return nullptr;
  8032. }
  8033. have_mve = true;
  8034. mve_vpr_regnum = register_count;
  8035. register_count++;
  8036. /* We can't have Q pseudo registers available here, as that
  8037. would mean we have NEON features, and that is only available
  8038. on A and R profiles. */
  8039. gdb_assert (!have_q_pseudos);
  8040. /* Given we have a M-profile target description, if MVE is
  8041. enabled and there are VFP registers, we should have Q
  8042. pseudo registers (Q0 ~ Q7). */
  8043. if (have_vfp)
  8044. have_q_pseudos = true;
  8045. }
  8046. /* Do we have the ARMv8.1-m PACBTI feature? */
  8047. feature = tdesc_find_feature (tdesc,
  8048. "org.gnu.gdb.arm.m-profile-pacbti");
  8049. if (feature != nullptr)
  8050. {
  8051. /* By advertising this feature, the target acknowledges the
  8052. presence of the ARMv8.1-m PACBTI extensions.
  8053. We don't care for any particular registers in this group, so
  8054. the target is free to include whatever it deems appropriate.
  8055. The expectation is for this feature to include the PAC
  8056. keys. */
  8057. have_pacbti = true;
  8058. }
  8059. }
  8060. }
  8061. /* If there is already a candidate, use it. */
  8062. for (best_arch = gdbarch_list_lookup_by_info (arches, &info);
  8063. best_arch != NULL;
  8064. best_arch = gdbarch_list_lookup_by_info (best_arch->next, &info))
  8065. {
  8066. arm_gdbarch_tdep *tdep
  8067. = (arm_gdbarch_tdep *) gdbarch_tdep (best_arch->gdbarch);
  8068. if (arm_abi != ARM_ABI_AUTO && arm_abi != tdep->arm_abi)
  8069. continue;
  8070. if (fp_model != ARM_FLOAT_AUTO && fp_model != tdep->fp_model)
  8071. continue;
  8072. /* There are various other properties in tdep that we do not
  8073. need to check here: those derived from a target description,
  8074. since gdbarches with a different target description are
  8075. automatically disqualified. */
  8076. /* Do check is_m, though, since it might come from the binary. */
  8077. if (is_m != tdep->is_m)
  8078. continue;
  8079. /* Also check for ARMv8.1-m PACBTI support, since it might come from
  8080. the binary. */
  8081. if (have_pacbti != tdep->have_pacbti)
  8082. continue;
  8083. /* Found a match. */
  8084. break;
  8085. }
  8086. if (best_arch != NULL)
  8087. return best_arch->gdbarch;
  8088. arm_gdbarch_tdep *tdep = new arm_gdbarch_tdep;
  8089. gdbarch = gdbarch_alloc (&info, tdep);
  8090. /* Record additional information about the architecture we are defining.
  8091. These are gdbarch discriminators, like the OSABI. */
  8092. tdep->arm_abi = arm_abi;
  8093. tdep->fp_model = fp_model;
  8094. tdep->is_m = is_m;
  8095. tdep->have_fpa_registers = have_fpa_registers;
  8096. tdep->have_wmmx_registers = have_wmmx_registers;
  8097. gdb_assert (vfp_register_count == 0
  8098. || vfp_register_count == 16
  8099. || vfp_register_count == 32);
  8100. tdep->vfp_register_count = vfp_register_count;
  8101. tdep->have_s_pseudos = have_s_pseudos;
  8102. tdep->have_q_pseudos = have_q_pseudos;
  8103. tdep->have_neon = have_neon;
  8104. /* Adjust the MVE feature settings. */
  8105. if (have_mve)
  8106. {
  8107. tdep->have_mve = true;
  8108. tdep->mve_vpr_regnum = mve_vpr_regnum;
  8109. }
  8110. /* Adjust the PACBTI feature settings. */
  8111. tdep->have_pacbti = have_pacbti;
  8112. arm_register_g_packet_guesses (gdbarch);
  8113. /* Breakpoints. */
  8114. switch (info.byte_order_for_code)
  8115. {
  8116. case BFD_ENDIAN_BIG:
  8117. tdep->arm_breakpoint = arm_default_arm_be_breakpoint;
  8118. tdep->arm_breakpoint_size = sizeof (arm_default_arm_be_breakpoint);
  8119. tdep->thumb_breakpoint = arm_default_thumb_be_breakpoint;
  8120. tdep->thumb_breakpoint_size = sizeof (arm_default_thumb_be_breakpoint);
  8121. break;
  8122. case BFD_ENDIAN_LITTLE:
  8123. tdep->arm_breakpoint = arm_default_arm_le_breakpoint;
  8124. tdep->arm_breakpoint_size = sizeof (arm_default_arm_le_breakpoint);
  8125. tdep->thumb_breakpoint = arm_default_thumb_le_breakpoint;
  8126. tdep->thumb_breakpoint_size = sizeof (arm_default_thumb_le_breakpoint);
  8127. break;
  8128. default:
  8129. internal_error (__FILE__, __LINE__,
  8130. _("arm_gdbarch_init: bad byte order for float format"));
  8131. }
  8132. /* On ARM targets char defaults to unsigned. */
  8133. set_gdbarch_char_signed (gdbarch, 0);
  8134. /* wchar_t is unsigned under the AAPCS. */
  8135. if (tdep->arm_abi == ARM_ABI_AAPCS)
  8136. set_gdbarch_wchar_signed (gdbarch, 0);
  8137. else
  8138. set_gdbarch_wchar_signed (gdbarch, 1);
  8139. /* Compute type alignment. */
  8140. set_gdbarch_type_align (gdbarch, arm_type_align);
  8141. /* Note: for displaced stepping, this includes the breakpoint, and one word
  8142. of additional scratch space. This setting isn't used for anything beside
  8143. displaced stepping at present. */
  8144. set_gdbarch_max_insn_length (gdbarch, 4 * ARM_DISPLACED_MODIFIED_INSNS);
  8145. /* This should be low enough for everything. */
  8146. tdep->lowest_pc = 0x20;
  8147. tdep->jb_pc = -1; /* Longjump support not enabled by default. */
  8148. /* The default, for both APCS and AAPCS, is to return small
  8149. structures in registers. */
  8150. tdep->struct_return = reg_struct_return;
  8151. set_gdbarch_push_dummy_call (gdbarch, arm_push_dummy_call);
  8152. set_gdbarch_frame_align (gdbarch, arm_frame_align);
  8153. if (is_m)
  8154. set_gdbarch_code_of_frame_writable (gdbarch, arm_code_of_frame_writable);
  8155. set_gdbarch_write_pc (gdbarch, arm_write_pc);
  8156. frame_base_set_default (gdbarch, &arm_normal_base);
  8157. /* Address manipulation. */
  8158. set_gdbarch_addr_bits_remove (gdbarch, arm_addr_bits_remove);
  8159. /* Advance PC across function entry code. */
  8160. set_gdbarch_skip_prologue (gdbarch, arm_skip_prologue);
  8161. /* Detect whether PC is at a point where the stack has been destroyed. */
  8162. set_gdbarch_stack_frame_destroyed_p (gdbarch, arm_stack_frame_destroyed_p);
  8163. /* Skip trampolines. */
  8164. set_gdbarch_skip_trampoline_code (gdbarch, arm_skip_stub);
  8165. /* The stack grows downward. */
  8166. set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
  8167. /* Breakpoint manipulation. */
  8168. set_gdbarch_breakpoint_kind_from_pc (gdbarch, arm_breakpoint_kind_from_pc);
  8169. set_gdbarch_sw_breakpoint_from_kind (gdbarch, arm_sw_breakpoint_from_kind);
  8170. set_gdbarch_breakpoint_kind_from_current_state (gdbarch,
  8171. arm_breakpoint_kind_from_current_state);
  8172. /* Information about registers, etc. */
  8173. set_gdbarch_sp_regnum (gdbarch, ARM_SP_REGNUM);
  8174. set_gdbarch_pc_regnum (gdbarch, ARM_PC_REGNUM);
  8175. set_gdbarch_num_regs (gdbarch, register_count);
  8176. set_gdbarch_register_type (gdbarch, arm_register_type);
  8177. set_gdbarch_register_reggroup_p (gdbarch, arm_register_reggroup_p);
  8178. /* This "info float" is FPA-specific. Use the generic version if we
  8179. do not have FPA. */
  8180. if (tdep->have_fpa_registers)
  8181. set_gdbarch_print_float_info (gdbarch, arm_print_float_info);
  8182. /* Internal <-> external register number maps. */
  8183. set_gdbarch_dwarf2_reg_to_regnum (gdbarch, arm_dwarf_reg_to_regnum);
  8184. set_gdbarch_register_sim_regno (gdbarch, arm_register_sim_regno);
  8185. set_gdbarch_register_name (gdbarch, arm_register_name);
  8186. /* Returning results. */
  8187. set_gdbarch_return_value (gdbarch, arm_return_value);
  8188. /* Disassembly. */
  8189. set_gdbarch_print_insn (gdbarch, gdb_print_insn_arm);
  8190. /* Minsymbol frobbing. */
  8191. set_gdbarch_elf_make_msymbol_special (gdbarch, arm_elf_make_msymbol_special);
  8192. set_gdbarch_coff_make_msymbol_special (gdbarch,
  8193. arm_coff_make_msymbol_special);
  8194. set_gdbarch_record_special_symbol (gdbarch, arm_record_special_symbol);
  8195. /* Thumb-2 IT block support. */
  8196. set_gdbarch_adjust_breakpoint_address (gdbarch,
  8197. arm_adjust_breakpoint_address);
  8198. /* Virtual tables. */
  8199. set_gdbarch_vbit_in_delta (gdbarch, 1);
  8200. /* Hook in the ABI-specific overrides, if they have been registered. */
  8201. gdbarch_init_osabi (info, gdbarch);
  8202. dwarf2_frame_set_init_reg (gdbarch, arm_dwarf2_frame_init_reg);
  8203. /* Add some default predicates. */
  8204. if (is_m)
  8205. frame_unwind_append_unwinder (gdbarch, &arm_m_exception_unwind);
  8206. frame_unwind_append_unwinder (gdbarch, &arm_stub_unwind);
  8207. dwarf2_append_unwinders (gdbarch);
  8208. frame_unwind_append_unwinder (gdbarch, &arm_exidx_unwind);
  8209. frame_unwind_append_unwinder (gdbarch, &arm_epilogue_frame_unwind);
  8210. frame_unwind_append_unwinder (gdbarch, &arm_prologue_unwind);
  8211. /* Now we have tuned the configuration, set a few final things,
  8212. based on what the OS ABI has told us. */
  8213. /* If the ABI is not otherwise marked, assume the old GNU APCS. EABI
  8214. binaries are always marked. */
  8215. if (tdep->arm_abi == ARM_ABI_AUTO)
  8216. tdep->arm_abi = ARM_ABI_APCS;
  8217. /* Watchpoints are not steppable. */
  8218. set_gdbarch_have_nonsteppable_watchpoint (gdbarch, 1);
  8219. /* We used to default to FPA for generic ARM, but almost nobody
  8220. uses that now, and we now provide a way for the user to force
  8221. the model. So default to the most useful variant. */
  8222. if (tdep->fp_model == ARM_FLOAT_AUTO)
  8223. tdep->fp_model = ARM_FLOAT_SOFT_FPA;
  8224. if (tdep->jb_pc >= 0)
  8225. set_gdbarch_get_longjmp_target (gdbarch, arm_get_longjmp_target);
  8226. /* Floating point sizes and format. */
  8227. set_gdbarch_float_format (gdbarch, floatformats_ieee_single);
  8228. if (tdep->fp_model == ARM_FLOAT_SOFT_FPA || tdep->fp_model == ARM_FLOAT_FPA)
  8229. {
  8230. set_gdbarch_double_format
  8231. (gdbarch, floatformats_ieee_double_littlebyte_bigword);
  8232. set_gdbarch_long_double_format
  8233. (gdbarch, floatformats_ieee_double_littlebyte_bigword);
  8234. }
  8235. else
  8236. {
  8237. set_gdbarch_double_format (gdbarch, floatformats_ieee_double);
  8238. set_gdbarch_long_double_format (gdbarch, floatformats_ieee_double);
  8239. }
  8240. /* Hook used to decorate frames with signed return addresses, only available
  8241. for ARMv8.1-m PACBTI. */
  8242. if (is_m && have_pacbti)
  8243. set_gdbarch_get_pc_address_flags (gdbarch, arm_get_pc_address_flags);
  8244. if (tdesc_data != nullptr)
  8245. {
  8246. set_tdesc_pseudo_register_name (gdbarch, arm_register_name);
  8247. tdesc_use_registers (gdbarch, tdesc, std::move (tdesc_data));
  8248. register_count = gdbarch_num_regs (gdbarch);
  8249. /* Override tdesc_register_type to adjust the types of VFP
  8250. registers for NEON. */
  8251. set_gdbarch_register_type (gdbarch, arm_register_type);
  8252. }
  8253. /* Initialize the pseudo register data. */
  8254. int num_pseudos = 0;
  8255. if (tdep->have_s_pseudos)
  8256. {
  8257. /* VFP single precision pseudo registers (S0~S31). */
  8258. tdep->s_pseudo_base = register_count;
  8259. tdep->s_pseudo_count = 32;
  8260. num_pseudos += tdep->s_pseudo_count;
  8261. if (tdep->have_q_pseudos)
  8262. {
  8263. /* NEON quad precision pseudo registers (Q0~Q15). */
  8264. tdep->q_pseudo_base = register_count + num_pseudos;
  8265. if (have_neon)
  8266. tdep->q_pseudo_count = 16;
  8267. else if (have_mve)
  8268. tdep->q_pseudo_count = ARM_MVE_NUM_Q_REGS;
  8269. num_pseudos += tdep->q_pseudo_count;
  8270. }
  8271. }
  8272. /* Do we have any MVE pseudo registers? */
  8273. if (have_mve)
  8274. {
  8275. tdep->mve_pseudo_base = register_count + num_pseudos;
  8276. tdep->mve_pseudo_count = 1;
  8277. num_pseudos += tdep->mve_pseudo_count;
  8278. }
  8279. /* Do we have any ARMv8.1-m PACBTI pseudo registers. */
  8280. if (have_pacbti)
  8281. {
  8282. tdep->pacbti_pseudo_base = register_count + num_pseudos;
  8283. tdep->pacbti_pseudo_count = 1;
  8284. num_pseudos += tdep->pacbti_pseudo_count;
  8285. }
  8286. /* Set some pseudo register hooks, if we have pseudo registers. */
  8287. if (tdep->have_s_pseudos || have_mve || have_pacbti)
  8288. {
  8289. set_gdbarch_num_pseudo_regs (gdbarch, num_pseudos);
  8290. set_gdbarch_pseudo_register_read (gdbarch, arm_pseudo_read);
  8291. set_gdbarch_pseudo_register_write (gdbarch, arm_pseudo_write);
  8292. }
  8293. /* Add standard register aliases. We add aliases even for those
  8294. names which are used by the current architecture - it's simpler,
  8295. and does no harm, since nothing ever lists user registers. */
  8296. for (i = 0; i < ARRAY_SIZE (arm_register_aliases); i++)
  8297. user_reg_add (gdbarch, arm_register_aliases[i].name,
  8298. value_of_arm_user_reg, &arm_register_aliases[i].regnum);
  8299. set_gdbarch_disassembler_options (gdbarch, &arm_disassembler_options);
  8300. set_gdbarch_valid_disassembler_options (gdbarch, disassembler_options_arm ());
  8301. set_gdbarch_gnu_triplet_regexp (gdbarch, arm_gnu_triplet_regexp);
  8302. return gdbarch;
  8303. }
  8304. static void
  8305. arm_dump_tdep (struct gdbarch *gdbarch, struct ui_file *file)
  8306. {
  8307. arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  8308. if (tdep == NULL)
  8309. return;
  8310. gdb_printf (file, _("arm_dump_tdep: fp_model = %i\n"),
  8311. (int) tdep->fp_model);
  8312. gdb_printf (file, _("arm_dump_tdep: have_fpa_registers = %i\n"),
  8313. (int) tdep->have_fpa_registers);
  8314. gdb_printf (file, _("arm_dump_tdep: have_wmmx_registers = %i\n"),
  8315. (int) tdep->have_wmmx_registers);
  8316. gdb_printf (file, _("arm_dump_tdep: vfp_register_count = %i\n"),
  8317. (int) tdep->vfp_register_count);
  8318. gdb_printf (file, _("arm_dump_tdep: have_s_pseudos = %s\n"),
  8319. tdep->have_s_pseudos? "true" : "false");
  8320. gdb_printf (file, _("arm_dump_tdep: s_pseudo_base = %i\n"),
  8321. (int) tdep->s_pseudo_base);
  8322. gdb_printf (file, _("arm_dump_tdep: s_pseudo_count = %i\n"),
  8323. (int) tdep->s_pseudo_count);
  8324. gdb_printf (file, _("arm_dump_tdep: have_q_pseudos = %s\n"),
  8325. tdep->have_q_pseudos? "true" : "false");
  8326. gdb_printf (file, _("arm_dump_tdep: q_pseudo_base = %i\n"),
  8327. (int) tdep->q_pseudo_base);
  8328. gdb_printf (file, _("arm_dump_tdep: q_pseudo_count = %i\n"),
  8329. (int) tdep->q_pseudo_count);
  8330. gdb_printf (file, _("arm_dump_tdep: have_neon = %i\n"),
  8331. (int) tdep->have_neon);
  8332. gdb_printf (file, _("arm_dump_tdep: have_mve = %s\n"),
  8333. tdep->have_mve? "yes" : "no");
  8334. gdb_printf (file, _("arm_dump_tdep: mve_vpr_regnum = %i\n"),
  8335. tdep->mve_vpr_regnum);
  8336. gdb_printf (file, _("arm_dump_tdep: mve_pseudo_base = %i\n"),
  8337. tdep->mve_pseudo_base);
  8338. gdb_printf (file, _("arm_dump_tdep: mve_pseudo_count = %i\n"),
  8339. tdep->mve_pseudo_count);
  8340. gdb_printf (file, _("arm_dump_tdep: Lowest pc = 0x%lx\n"),
  8341. (unsigned long) tdep->lowest_pc);
  8342. gdb_printf (file, _("arm_dump_tdep: have_pacbti = %s\n"),
  8343. tdep->have_pacbti? "yes" : "no");
  8344. gdb_printf (file, _("arm_dump_tdep: pacbti_pseudo_base = %i\n"),
  8345. tdep->pacbti_pseudo_base);
  8346. gdb_printf (file, _("arm_dump_tdep: pacbti_pseudo_count = %i\n"),
  8347. tdep->pacbti_pseudo_count);
  8348. gdb_printf (file, _("arm_dump_tdep: is_m = %s\n"),
  8349. tdep->is_m? "yes" : "no");
  8350. }
  8351. #if GDB_SELF_TEST
  8352. namespace selftests
  8353. {
  8354. static void arm_record_test (void);
  8355. static void arm_analyze_prologue_test ();
  8356. }
  8357. #endif
  8358. void _initialize_arm_tdep ();
  8359. void
  8360. _initialize_arm_tdep ()
  8361. {
  8362. long length;
  8363. int i, j;
  8364. char regdesc[1024], *rdptr = regdesc;
  8365. size_t rest = sizeof (regdesc);
  8366. gdbarch_register (bfd_arch_arm, arm_gdbarch_init, arm_dump_tdep);
  8367. /* Add ourselves to objfile event chain. */
  8368. gdb::observers::new_objfile.attach (arm_exidx_new_objfile, "arm-tdep");
  8369. /* Register an ELF OS ABI sniffer for ARM binaries. */
  8370. gdbarch_register_osabi_sniffer (bfd_arch_arm,
  8371. bfd_target_elf_flavour,
  8372. arm_elf_osabi_sniffer);
  8373. /* Add root prefix command for all "set arm"/"show arm" commands. */
  8374. add_setshow_prefix_cmd ("arm", no_class,
  8375. _("Various ARM-specific commands."),
  8376. _("Various ARM-specific commands."),
  8377. &setarmcmdlist, &showarmcmdlist,
  8378. &setlist, &showlist);
  8379. arm_disassembler_options = xstrdup ("reg-names-std");
  8380. const disasm_options_t *disasm_options
  8381. = &disassembler_options_arm ()->options;
  8382. int num_disassembly_styles = 0;
  8383. for (i = 0; disasm_options->name[i] != NULL; i++)
  8384. if (startswith (disasm_options->name[i], "reg-names-"))
  8385. num_disassembly_styles++;
  8386. /* Initialize the array that will be passed to add_setshow_enum_cmd(). */
  8387. valid_disassembly_styles = XNEWVEC (const char *,
  8388. num_disassembly_styles + 1);
  8389. for (i = j = 0; disasm_options->name[i] != NULL; i++)
  8390. if (startswith (disasm_options->name[i], "reg-names-"))
  8391. {
  8392. size_t offset = strlen ("reg-names-");
  8393. const char *style = disasm_options->name[i];
  8394. valid_disassembly_styles[j++] = &style[offset];
  8395. if (strcmp (&style[offset], "std") == 0)
  8396. disassembly_style = &style[offset];
  8397. length = snprintf (rdptr, rest, "%s - %s\n", &style[offset],
  8398. disasm_options->description[i]);
  8399. rdptr += length;
  8400. rest -= length;
  8401. }
  8402. /* Mark the end of valid options. */
  8403. valid_disassembly_styles[num_disassembly_styles] = NULL;
  8404. /* Create the help text. */
  8405. std::string helptext = string_printf ("%s%s%s",
  8406. _("The valid values are:\n"),
  8407. regdesc,
  8408. _("The default is \"std\"."));
  8409. add_setshow_enum_cmd("disassembler", no_class,
  8410. valid_disassembly_styles, &disassembly_style,
  8411. _("Set the disassembly style."),
  8412. _("Show the disassembly style."),
  8413. helptext.c_str (),
  8414. set_disassembly_style_sfunc,
  8415. show_disassembly_style_sfunc,
  8416. &setarmcmdlist, &showarmcmdlist);
  8417. add_setshow_boolean_cmd ("apcs32", no_class, &arm_apcs_32,
  8418. _("Set usage of ARM 32-bit mode."),
  8419. _("Show usage of ARM 32-bit mode."),
  8420. _("When off, a 26-bit PC will be used."),
  8421. NULL,
  8422. NULL, /* FIXME: i18n: Usage of ARM 32-bit
  8423. mode is %s. */
  8424. &setarmcmdlist, &showarmcmdlist);
  8425. /* Add a command to allow the user to force the FPU model. */
  8426. add_setshow_enum_cmd ("fpu", no_class, fp_model_strings, &current_fp_model,
  8427. _("Set the floating point type."),
  8428. _("Show the floating point type."),
  8429. _("auto - Determine the FP typefrom the OS-ABI.\n\
  8430. softfpa - Software FP, mixed-endian doubles on little-endian ARMs.\n\
  8431. fpa - FPA co-processor (GCC compiled).\n\
  8432. softvfp - Software FP with pure-endian doubles.\n\
  8433. vfp - VFP co-processor."),
  8434. set_fp_model_sfunc, show_fp_model,
  8435. &setarmcmdlist, &showarmcmdlist);
  8436. /* Add a command to allow the user to force the ABI. */
  8437. add_setshow_enum_cmd ("abi", class_support, arm_abi_strings, &arm_abi_string,
  8438. _("Set the ABI."),
  8439. _("Show the ABI."),
  8440. NULL, arm_set_abi, arm_show_abi,
  8441. &setarmcmdlist, &showarmcmdlist);
  8442. /* Add two commands to allow the user to force the assumed
  8443. execution mode. */
  8444. add_setshow_enum_cmd ("fallback-mode", class_support,
  8445. arm_mode_strings, &arm_fallback_mode_string,
  8446. _("Set the mode assumed when symbols are unavailable."),
  8447. _("Show the mode assumed when symbols are unavailable."),
  8448. NULL, NULL, arm_show_fallback_mode,
  8449. &setarmcmdlist, &showarmcmdlist);
  8450. add_setshow_enum_cmd ("force-mode", class_support,
  8451. arm_mode_strings, &arm_force_mode_string,
  8452. _("Set the mode assumed even when symbols are available."),
  8453. _("Show the mode assumed even when symbols are available."),
  8454. NULL, NULL, arm_show_force_mode,
  8455. &setarmcmdlist, &showarmcmdlist);
  8456. /* Debugging flag. */
  8457. add_setshow_boolean_cmd ("arm", class_maintenance, &arm_debug,
  8458. _("Set ARM debugging."),
  8459. _("Show ARM debugging."),
  8460. _("When on, arm-specific debugging is enabled."),
  8461. NULL,
  8462. NULL, /* FIXME: i18n: "ARM debugging is %s. */
  8463. &setdebuglist, &showdebuglist);
  8464. #if GDB_SELF_TEST
  8465. selftests::register_test ("arm-record", selftests::arm_record_test);
  8466. selftests::register_test ("arm_analyze_prologue", selftests::arm_analyze_prologue_test);
  8467. #endif
  8468. }
  8469. /* ARM-reversible process record data structures. */
  8470. #define ARM_INSN_SIZE_BYTES 4
  8471. #define THUMB_INSN_SIZE_BYTES 2
  8472. #define THUMB2_INSN_SIZE_BYTES 4
  8473. /* Position of the bit within a 32-bit ARM instruction
  8474. that defines whether the instruction is a load or store. */
  8475. #define INSN_S_L_BIT_NUM 20
  8476. #define REG_ALLOC(REGS, LENGTH, RECORD_BUF) \
  8477. do \
  8478. { \
  8479. unsigned int reg_len = LENGTH; \
  8480. if (reg_len) \
  8481. { \
  8482. REGS = XNEWVEC (uint32_t, reg_len); \
  8483. memcpy(&REGS[0], &RECORD_BUF[0], sizeof(uint32_t)*LENGTH); \
  8484. } \
  8485. } \
  8486. while (0)
  8487. #define MEM_ALLOC(MEMS, LENGTH, RECORD_BUF) \
  8488. do \
  8489. { \
  8490. unsigned int mem_len = LENGTH; \
  8491. if (mem_len) \
  8492. { \
  8493. MEMS = XNEWVEC (struct arm_mem_r, mem_len); \
  8494. memcpy(&MEMS->len, &RECORD_BUF[0], \
  8495. sizeof(struct arm_mem_r) * LENGTH); \
  8496. } \
  8497. } \
  8498. while (0)
  8499. /* Checks whether insn is already recorded or yet to be decoded. (boolean expression). */
  8500. #define INSN_RECORDED(ARM_RECORD) \
  8501. (0 != (ARM_RECORD)->reg_rec_count || 0 != (ARM_RECORD)->mem_rec_count)
  8502. /* ARM memory record structure. */
  8503. struct arm_mem_r
  8504. {
  8505. uint32_t len; /* Record length. */
  8506. uint32_t addr; /* Memory address. */
  8507. };
  8508. /* ARM instruction record contains opcode of current insn
  8509. and execution state (before entry to decode_insn()),
  8510. contains list of to-be-modified registers and
  8511. memory blocks (on return from decode_insn()). */
  8512. typedef struct insn_decode_record_t
  8513. {
  8514. struct gdbarch *gdbarch;
  8515. struct regcache *regcache;
  8516. CORE_ADDR this_addr; /* Address of the insn being decoded. */
  8517. uint32_t arm_insn; /* Should accommodate thumb. */
  8518. uint32_t cond; /* Condition code. */
  8519. uint32_t opcode; /* Insn opcode. */
  8520. uint32_t decode; /* Insn decode bits. */
  8521. uint32_t mem_rec_count; /* No of mem records. */
  8522. uint32_t reg_rec_count; /* No of reg records. */
  8523. uint32_t *arm_regs; /* Registers to be saved for this record. */
  8524. struct arm_mem_r *arm_mems; /* Memory to be saved for this record. */
  8525. } insn_decode_record;
  8526. /* Checks ARM SBZ and SBO mandatory fields. */
  8527. static int
  8528. sbo_sbz (uint32_t insn, uint32_t bit_num, uint32_t len, uint32_t sbo)
  8529. {
  8530. uint32_t ones = bits (insn, bit_num - 1, (bit_num -1) + (len - 1));
  8531. if (!len)
  8532. return 1;
  8533. if (!sbo)
  8534. ones = ~ones;
  8535. while (ones)
  8536. {
  8537. if (!(ones & sbo))
  8538. {
  8539. return 0;
  8540. }
  8541. ones = ones >> 1;
  8542. }
  8543. return 1;
  8544. }
  8545. enum arm_record_result
  8546. {
  8547. ARM_RECORD_SUCCESS = 0,
  8548. ARM_RECORD_FAILURE = 1
  8549. };
  8550. typedef enum
  8551. {
  8552. ARM_RECORD_STRH=1,
  8553. ARM_RECORD_STRD
  8554. } arm_record_strx_t;
  8555. typedef enum
  8556. {
  8557. ARM_RECORD=1,
  8558. THUMB_RECORD,
  8559. THUMB2_RECORD
  8560. } record_type_t;
  8561. static int
  8562. arm_record_strx (insn_decode_record *arm_insn_r, uint32_t *record_buf,
  8563. uint32_t *record_buf_mem, arm_record_strx_t str_type)
  8564. {
  8565. struct regcache *reg_cache = arm_insn_r->regcache;
  8566. ULONGEST u_regval[2]= {0};
  8567. uint32_t reg_src1 = 0, reg_src2 = 0;
  8568. uint32_t immed_high = 0, immed_low = 0,offset_8 = 0, tgt_mem_addr = 0;
  8569. arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 21, 24);
  8570. arm_insn_r->decode = bits (arm_insn_r->arm_insn, 4, 7);
  8571. if (14 == arm_insn_r->opcode || 10 == arm_insn_r->opcode)
  8572. {
  8573. /* 1) Handle misc store, immediate offset. */
  8574. immed_low = bits (arm_insn_r->arm_insn, 0, 3);
  8575. immed_high = bits (arm_insn_r->arm_insn, 8, 11);
  8576. reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
  8577. regcache_raw_read_unsigned (reg_cache, reg_src1,
  8578. &u_regval[0]);
  8579. if (ARM_PC_REGNUM == reg_src1)
  8580. {
  8581. /* If R15 was used as Rn, hence current PC+8. */
  8582. u_regval[0] = u_regval[0] + 8;
  8583. }
  8584. offset_8 = (immed_high << 4) | immed_low;
  8585. /* Calculate target store address. */
  8586. if (14 == arm_insn_r->opcode)
  8587. {
  8588. tgt_mem_addr = u_regval[0] + offset_8;
  8589. }
  8590. else
  8591. {
  8592. tgt_mem_addr = u_regval[0] - offset_8;
  8593. }
  8594. if (ARM_RECORD_STRH == str_type)
  8595. {
  8596. record_buf_mem[0] = 2;
  8597. record_buf_mem[1] = tgt_mem_addr;
  8598. arm_insn_r->mem_rec_count = 1;
  8599. }
  8600. else if (ARM_RECORD_STRD == str_type)
  8601. {
  8602. record_buf_mem[0] = 4;
  8603. record_buf_mem[1] = tgt_mem_addr;
  8604. record_buf_mem[2] = 4;
  8605. record_buf_mem[3] = tgt_mem_addr + 4;
  8606. arm_insn_r->mem_rec_count = 2;
  8607. }
  8608. }
  8609. else if (12 == arm_insn_r->opcode || 8 == arm_insn_r->opcode)
  8610. {
  8611. /* 2) Store, register offset. */
  8612. /* Get Rm. */
  8613. reg_src1 = bits (arm_insn_r->arm_insn, 0, 3);
  8614. /* Get Rn. */
  8615. reg_src2 = bits (arm_insn_r->arm_insn, 16, 19);
  8616. regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
  8617. regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
  8618. if (15 == reg_src2)
  8619. {
  8620. /* If R15 was used as Rn, hence current PC+8. */
  8621. u_regval[0] = u_regval[0] + 8;
  8622. }
  8623. /* Calculate target store address, Rn +/- Rm, register offset. */
  8624. if (12 == arm_insn_r->opcode)
  8625. {
  8626. tgt_mem_addr = u_regval[0] + u_regval[1];
  8627. }
  8628. else
  8629. {
  8630. tgt_mem_addr = u_regval[1] - u_regval[0];
  8631. }
  8632. if (ARM_RECORD_STRH == str_type)
  8633. {
  8634. record_buf_mem[0] = 2;
  8635. record_buf_mem[1] = tgt_mem_addr;
  8636. arm_insn_r->mem_rec_count = 1;
  8637. }
  8638. else if (ARM_RECORD_STRD == str_type)
  8639. {
  8640. record_buf_mem[0] = 4;
  8641. record_buf_mem[1] = tgt_mem_addr;
  8642. record_buf_mem[2] = 4;
  8643. record_buf_mem[3] = tgt_mem_addr + 4;
  8644. arm_insn_r->mem_rec_count = 2;
  8645. }
  8646. }
  8647. else if (11 == arm_insn_r->opcode || 15 == arm_insn_r->opcode
  8648. || 2 == arm_insn_r->opcode || 6 == arm_insn_r->opcode)
  8649. {
  8650. /* 3) Store, immediate pre-indexed. */
  8651. /* 5) Store, immediate post-indexed. */
  8652. immed_low = bits (arm_insn_r->arm_insn, 0, 3);
  8653. immed_high = bits (arm_insn_r->arm_insn, 8, 11);
  8654. offset_8 = (immed_high << 4) | immed_low;
  8655. reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
  8656. regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
  8657. /* Calculate target store address, Rn +/- Rm, register offset. */
  8658. if (15 == arm_insn_r->opcode || 6 == arm_insn_r->opcode)
  8659. {
  8660. tgt_mem_addr = u_regval[0] + offset_8;
  8661. }
  8662. else
  8663. {
  8664. tgt_mem_addr = u_regval[0] - offset_8;
  8665. }
  8666. if (ARM_RECORD_STRH == str_type)
  8667. {
  8668. record_buf_mem[0] = 2;
  8669. record_buf_mem[1] = tgt_mem_addr;
  8670. arm_insn_r->mem_rec_count = 1;
  8671. }
  8672. else if (ARM_RECORD_STRD == str_type)
  8673. {
  8674. record_buf_mem[0] = 4;
  8675. record_buf_mem[1] = tgt_mem_addr;
  8676. record_buf_mem[2] = 4;
  8677. record_buf_mem[3] = tgt_mem_addr + 4;
  8678. arm_insn_r->mem_rec_count = 2;
  8679. }
  8680. /* Record Rn also as it changes. */
  8681. *(record_buf) = bits (arm_insn_r->arm_insn, 16, 19);
  8682. arm_insn_r->reg_rec_count = 1;
  8683. }
  8684. else if (9 == arm_insn_r->opcode || 13 == arm_insn_r->opcode
  8685. || 0 == arm_insn_r->opcode || 4 == arm_insn_r->opcode)
  8686. {
  8687. /* 4) Store, register pre-indexed. */
  8688. /* 6) Store, register post -indexed. */
  8689. reg_src1 = bits (arm_insn_r->arm_insn, 0, 3);
  8690. reg_src2 = bits (arm_insn_r->arm_insn, 16, 19);
  8691. regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
  8692. regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
  8693. /* Calculate target store address, Rn +/- Rm, register offset. */
  8694. if (13 == arm_insn_r->opcode || 4 == arm_insn_r->opcode)
  8695. {
  8696. tgt_mem_addr = u_regval[0] + u_regval[1];
  8697. }
  8698. else
  8699. {
  8700. tgt_mem_addr = u_regval[1] - u_regval[0];
  8701. }
  8702. if (ARM_RECORD_STRH == str_type)
  8703. {
  8704. record_buf_mem[0] = 2;
  8705. record_buf_mem[1] = tgt_mem_addr;
  8706. arm_insn_r->mem_rec_count = 1;
  8707. }
  8708. else if (ARM_RECORD_STRD == str_type)
  8709. {
  8710. record_buf_mem[0] = 4;
  8711. record_buf_mem[1] = tgt_mem_addr;
  8712. record_buf_mem[2] = 4;
  8713. record_buf_mem[3] = tgt_mem_addr + 4;
  8714. arm_insn_r->mem_rec_count = 2;
  8715. }
  8716. /* Record Rn also as it changes. */
  8717. *(record_buf) = bits (arm_insn_r->arm_insn, 16, 19);
  8718. arm_insn_r->reg_rec_count = 1;
  8719. }
  8720. return 0;
  8721. }
  8722. /* Handling ARM extension space insns. */
  8723. static int
  8724. arm_record_extension_space (insn_decode_record *arm_insn_r)
  8725. {
  8726. int ret = 0; /* Return value: -1:record failure ; 0:success */
  8727. uint32_t opcode1 = 0, opcode2 = 0, insn_op1 = 0;
  8728. uint32_t record_buf[8], record_buf_mem[8];
  8729. uint32_t reg_src1 = 0;
  8730. struct regcache *reg_cache = arm_insn_r->regcache;
  8731. ULONGEST u_regval = 0;
  8732. gdb_assert (!INSN_RECORDED(arm_insn_r));
  8733. /* Handle unconditional insn extension space. */
  8734. opcode1 = bits (arm_insn_r->arm_insn, 20, 27);
  8735. opcode2 = bits (arm_insn_r->arm_insn, 4, 7);
  8736. if (arm_insn_r->cond)
  8737. {
  8738. /* PLD has no affect on architectural state, it just affects
  8739. the caches. */
  8740. if (5 == ((opcode1 & 0xE0) >> 5))
  8741. {
  8742. /* BLX(1) */
  8743. record_buf[0] = ARM_PS_REGNUM;
  8744. record_buf[1] = ARM_LR_REGNUM;
  8745. arm_insn_r->reg_rec_count = 2;
  8746. }
  8747. /* STC2, LDC2, MCR2, MRC2, CDP2: <TBD>, co-processor insn. */
  8748. }
  8749. opcode1 = bits (arm_insn_r->arm_insn, 25, 27);
  8750. if (3 == opcode1 && bit (arm_insn_r->arm_insn, 4))
  8751. {
  8752. ret = -1;
  8753. /* Undefined instruction on ARM V5; need to handle if later
  8754. versions define it. */
  8755. }
  8756. opcode1 = bits (arm_insn_r->arm_insn, 24, 27);
  8757. opcode2 = bits (arm_insn_r->arm_insn, 4, 7);
  8758. insn_op1 = bits (arm_insn_r->arm_insn, 20, 23);
  8759. /* Handle arithmetic insn extension space. */
  8760. if (!opcode1 && 9 == opcode2 && 1 != arm_insn_r->cond
  8761. && !INSN_RECORDED(arm_insn_r))
  8762. {
  8763. /* Handle MLA(S) and MUL(S). */
  8764. if (in_inclusive_range (insn_op1, 0U, 3U))
  8765. {
  8766. record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
  8767. record_buf[1] = ARM_PS_REGNUM;
  8768. arm_insn_r->reg_rec_count = 2;
  8769. }
  8770. else if (in_inclusive_range (insn_op1, 4U, 15U))
  8771. {
  8772. /* Handle SMLAL(S), SMULL(S), UMLAL(S), UMULL(S). */
  8773. record_buf[0] = bits (arm_insn_r->arm_insn, 16, 19);
  8774. record_buf[1] = bits (arm_insn_r->arm_insn, 12, 15);
  8775. record_buf[2] = ARM_PS_REGNUM;
  8776. arm_insn_r->reg_rec_count = 3;
  8777. }
  8778. }
  8779. opcode1 = bits (arm_insn_r->arm_insn, 26, 27);
  8780. opcode2 = bits (arm_insn_r->arm_insn, 23, 24);
  8781. insn_op1 = bits (arm_insn_r->arm_insn, 21, 22);
  8782. /* Handle control insn extension space. */
  8783. if (!opcode1 && 2 == opcode2 && !bit (arm_insn_r->arm_insn, 20)
  8784. && 1 != arm_insn_r->cond && !INSN_RECORDED(arm_insn_r))
  8785. {
  8786. if (!bit (arm_insn_r->arm_insn,25))
  8787. {
  8788. if (!bits (arm_insn_r->arm_insn, 4, 7))
  8789. {
  8790. if ((0 == insn_op1) || (2 == insn_op1))
  8791. {
  8792. /* MRS. */
  8793. record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
  8794. arm_insn_r->reg_rec_count = 1;
  8795. }
  8796. else if (1 == insn_op1)
  8797. {
  8798. /* CSPR is going to be changed. */
  8799. record_buf[0] = ARM_PS_REGNUM;
  8800. arm_insn_r->reg_rec_count = 1;
  8801. }
  8802. else if (3 == insn_op1)
  8803. {
  8804. /* SPSR is going to be changed. */
  8805. /* We need to get SPSR value, which is yet to be done. */
  8806. return -1;
  8807. }
  8808. }
  8809. else if (1 == bits (arm_insn_r->arm_insn, 4, 7))
  8810. {
  8811. if (1 == insn_op1)
  8812. {
  8813. /* BX. */
  8814. record_buf[0] = ARM_PS_REGNUM;
  8815. arm_insn_r->reg_rec_count = 1;
  8816. }
  8817. else if (3 == insn_op1)
  8818. {
  8819. /* CLZ. */
  8820. record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
  8821. arm_insn_r->reg_rec_count = 1;
  8822. }
  8823. }
  8824. else if (3 == bits (arm_insn_r->arm_insn, 4, 7))
  8825. {
  8826. /* BLX. */
  8827. record_buf[0] = ARM_PS_REGNUM;
  8828. record_buf[1] = ARM_LR_REGNUM;
  8829. arm_insn_r->reg_rec_count = 2;
  8830. }
  8831. else if (5 == bits (arm_insn_r->arm_insn, 4, 7))
  8832. {
  8833. /* QADD, QSUB, QDADD, QDSUB */
  8834. record_buf[0] = ARM_PS_REGNUM;
  8835. record_buf[1] = bits (arm_insn_r->arm_insn, 12, 15);
  8836. arm_insn_r->reg_rec_count = 2;
  8837. }
  8838. else if (7 == bits (arm_insn_r->arm_insn, 4, 7))
  8839. {
  8840. /* BKPT. */
  8841. record_buf[0] = ARM_PS_REGNUM;
  8842. record_buf[1] = ARM_LR_REGNUM;
  8843. arm_insn_r->reg_rec_count = 2;
  8844. /* Save SPSR also;how? */
  8845. return -1;
  8846. }
  8847. else if(8 == bits (arm_insn_r->arm_insn, 4, 7)
  8848. || 10 == bits (arm_insn_r->arm_insn, 4, 7)
  8849. || 12 == bits (arm_insn_r->arm_insn, 4, 7)
  8850. || 14 == bits (arm_insn_r->arm_insn, 4, 7)
  8851. )
  8852. {
  8853. if (0 == insn_op1 || 1 == insn_op1)
  8854. {
  8855. /* SMLA<x><y>, SMLAW<y>, SMULW<y>. */
  8856. /* We dont do optimization for SMULW<y> where we
  8857. need only Rd. */
  8858. record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
  8859. record_buf[1] = ARM_PS_REGNUM;
  8860. arm_insn_r->reg_rec_count = 2;
  8861. }
  8862. else if (2 == insn_op1)
  8863. {
  8864. /* SMLAL<x><y>. */
  8865. record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
  8866. record_buf[1] = bits (arm_insn_r->arm_insn, 16, 19);
  8867. arm_insn_r->reg_rec_count = 2;
  8868. }
  8869. else if (3 == insn_op1)
  8870. {
  8871. /* SMUL<x><y>. */
  8872. record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
  8873. arm_insn_r->reg_rec_count = 1;
  8874. }
  8875. }
  8876. }
  8877. else
  8878. {
  8879. /* MSR : immediate form. */
  8880. if (1 == insn_op1)
  8881. {
  8882. /* CSPR is going to be changed. */
  8883. record_buf[0] = ARM_PS_REGNUM;
  8884. arm_insn_r->reg_rec_count = 1;
  8885. }
  8886. else if (3 == insn_op1)
  8887. {
  8888. /* SPSR is going to be changed. */
  8889. /* we need to get SPSR value, which is yet to be done */
  8890. return -1;
  8891. }
  8892. }
  8893. }
  8894. opcode1 = bits (arm_insn_r->arm_insn, 25, 27);
  8895. opcode2 = bits (arm_insn_r->arm_insn, 20, 24);
  8896. insn_op1 = bits (arm_insn_r->arm_insn, 5, 6);
  8897. /* Handle load/store insn extension space. */
  8898. if (!opcode1 && bit (arm_insn_r->arm_insn, 7)
  8899. && bit (arm_insn_r->arm_insn, 4) && 1 != arm_insn_r->cond
  8900. && !INSN_RECORDED(arm_insn_r))
  8901. {
  8902. /* SWP/SWPB. */
  8903. if (0 == insn_op1)
  8904. {
  8905. /* These insn, changes register and memory as well. */
  8906. /* SWP or SWPB insn. */
  8907. /* Get memory address given by Rn. */
  8908. reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
  8909. regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval);
  8910. /* SWP insn ?, swaps word. */
  8911. if (8 == arm_insn_r->opcode)
  8912. {
  8913. record_buf_mem[0] = 4;
  8914. }
  8915. else
  8916. {
  8917. /* SWPB insn, swaps only byte. */
  8918. record_buf_mem[0] = 1;
  8919. }
  8920. record_buf_mem[1] = u_regval;
  8921. arm_insn_r->mem_rec_count = 1;
  8922. record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
  8923. arm_insn_r->reg_rec_count = 1;
  8924. }
  8925. else if (1 == insn_op1 && !bit (arm_insn_r->arm_insn, 20))
  8926. {
  8927. /* STRH. */
  8928. arm_record_strx(arm_insn_r, &record_buf[0], &record_buf_mem[0],
  8929. ARM_RECORD_STRH);
  8930. }
  8931. else if (2 == insn_op1 && !bit (arm_insn_r->arm_insn, 20))
  8932. {
  8933. /* LDRD. */
  8934. record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
  8935. record_buf[1] = record_buf[0] + 1;
  8936. arm_insn_r->reg_rec_count = 2;
  8937. }
  8938. else if (3 == insn_op1 && !bit (arm_insn_r->arm_insn, 20))
  8939. {
  8940. /* STRD. */
  8941. arm_record_strx(arm_insn_r, &record_buf[0], &record_buf_mem[0],
  8942. ARM_RECORD_STRD);
  8943. }
  8944. else if (bit (arm_insn_r->arm_insn, 20) && insn_op1 <= 3)
  8945. {
  8946. /* LDRH, LDRSB, LDRSH. */
  8947. record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
  8948. arm_insn_r->reg_rec_count = 1;
  8949. }
  8950. }
  8951. opcode1 = bits (arm_insn_r->arm_insn, 23, 27);
  8952. if (24 == opcode1 && bit (arm_insn_r->arm_insn, 21)
  8953. && !INSN_RECORDED(arm_insn_r))
  8954. {
  8955. ret = -1;
  8956. /* Handle coprocessor insn extension space. */
  8957. }
  8958. /* To be done for ARMv5 and later; as of now we return -1. */
  8959. if (-1 == ret)
  8960. return ret;
  8961. REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
  8962. MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
  8963. return ret;
  8964. }
  8965. /* Handling opcode 000 insns. */
  8966. static int
  8967. arm_record_data_proc_misc_ld_str (insn_decode_record *arm_insn_r)
  8968. {
  8969. struct regcache *reg_cache = arm_insn_r->regcache;
  8970. uint32_t record_buf[8], record_buf_mem[8];
  8971. ULONGEST u_regval[2] = {0};
  8972. uint32_t reg_src1 = 0;
  8973. uint32_t opcode1 = 0;
  8974. arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 21, 24);
  8975. arm_insn_r->decode = bits (arm_insn_r->arm_insn, 4, 7);
  8976. opcode1 = bits (arm_insn_r->arm_insn, 20, 24);
  8977. if (!((opcode1 & 0x19) == 0x10))
  8978. {
  8979. /* Data-processing (register) and Data-processing (register-shifted
  8980. register */
  8981. /* Out of 11 shifter operands mode, all the insn modifies destination
  8982. register, which is specified by 13-16 decode. */
  8983. record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
  8984. record_buf[1] = ARM_PS_REGNUM;
  8985. arm_insn_r->reg_rec_count = 2;
  8986. }
  8987. else if ((arm_insn_r->decode < 8) && ((opcode1 & 0x19) == 0x10))
  8988. {
  8989. /* Miscellaneous instructions */
  8990. if (3 == arm_insn_r->decode && 0x12 == opcode1
  8991. && sbo_sbz (arm_insn_r->arm_insn, 9, 12, 1))
  8992. {
  8993. /* Handle BLX, branch and link/exchange. */
  8994. if (9 == arm_insn_r->opcode)
  8995. {
  8996. /* Branch is chosen by setting T bit of CSPR, bitp[0] of Rm,
  8997. and R14 stores the return address. */
  8998. record_buf[0] = ARM_PS_REGNUM;
  8999. record_buf[1] = ARM_LR_REGNUM;
  9000. arm_insn_r->reg_rec_count = 2;
  9001. }
  9002. }
  9003. else if (7 == arm_insn_r->decode && 0x12 == opcode1)
  9004. {
  9005. /* Handle enhanced software breakpoint insn, BKPT. */
  9006. /* CPSR is changed to be executed in ARM state, disabling normal
  9007. interrupts, entering abort mode. */
  9008. /* According to high vector configuration PC is set. */
  9009. /* user hit breakpoint and type reverse, in
  9010. that case, we need to go back with previous CPSR and
  9011. Program Counter. */
  9012. record_buf[0] = ARM_PS_REGNUM;
  9013. record_buf[1] = ARM_LR_REGNUM;
  9014. arm_insn_r->reg_rec_count = 2;
  9015. /* Save SPSR also; how? */
  9016. return -1;
  9017. }
  9018. else if (1 == arm_insn_r->decode && 0x12 == opcode1
  9019. && sbo_sbz (arm_insn_r->arm_insn, 9, 12, 1))
  9020. {
  9021. /* Handle BX, branch and link/exchange. */
  9022. /* Branch is chosen by setting T bit of CSPR, bitp[0] of Rm. */
  9023. record_buf[0] = ARM_PS_REGNUM;
  9024. arm_insn_r->reg_rec_count = 1;
  9025. }
  9026. else if (1 == arm_insn_r->decode && 0x16 == opcode1
  9027. && sbo_sbz (arm_insn_r->arm_insn, 9, 4, 1)
  9028. && sbo_sbz (arm_insn_r->arm_insn, 17, 4, 1))
  9029. {
  9030. /* Count leading zeros: CLZ. */
  9031. record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
  9032. arm_insn_r->reg_rec_count = 1;
  9033. }
  9034. else if (!bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM)
  9035. && (8 == arm_insn_r->opcode || 10 == arm_insn_r->opcode)
  9036. && sbo_sbz (arm_insn_r->arm_insn, 17, 4, 1)
  9037. && sbo_sbz (arm_insn_r->arm_insn, 1, 12, 0))
  9038. {
  9039. /* Handle MRS insn. */
  9040. record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
  9041. arm_insn_r->reg_rec_count = 1;
  9042. }
  9043. }
  9044. else if (9 == arm_insn_r->decode && opcode1 < 0x10)
  9045. {
  9046. /* Multiply and multiply-accumulate */
  9047. /* Handle multiply instructions. */
  9048. /* MLA, MUL, SMLAL, SMULL, UMLAL, UMULL. */
  9049. if (0 == arm_insn_r->opcode || 1 == arm_insn_r->opcode)
  9050. {
  9051. /* Handle MLA and MUL. */
  9052. record_buf[0] = bits (arm_insn_r->arm_insn, 16, 19);
  9053. record_buf[1] = ARM_PS_REGNUM;
  9054. arm_insn_r->reg_rec_count = 2;
  9055. }
  9056. else if (4 <= arm_insn_r->opcode && 7 >= arm_insn_r->opcode)
  9057. {
  9058. /* Handle SMLAL, SMULL, UMLAL, UMULL. */
  9059. record_buf[0] = bits (arm_insn_r->arm_insn, 16, 19);
  9060. record_buf[1] = bits (arm_insn_r->arm_insn, 12, 15);
  9061. record_buf[2] = ARM_PS_REGNUM;
  9062. arm_insn_r->reg_rec_count = 3;
  9063. }
  9064. }
  9065. else if (9 == arm_insn_r->decode && opcode1 > 0x10)
  9066. {
  9067. /* Synchronization primitives */
  9068. /* Handling SWP, SWPB. */
  9069. /* These insn, changes register and memory as well. */
  9070. /* SWP or SWPB insn. */
  9071. reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
  9072. regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
  9073. /* SWP insn ?, swaps word. */
  9074. if (8 == arm_insn_r->opcode)
  9075. {
  9076. record_buf_mem[0] = 4;
  9077. }
  9078. else
  9079. {
  9080. /* SWPB insn, swaps only byte. */
  9081. record_buf_mem[0] = 1;
  9082. }
  9083. record_buf_mem[1] = u_regval[0];
  9084. arm_insn_r->mem_rec_count = 1;
  9085. record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
  9086. arm_insn_r->reg_rec_count = 1;
  9087. }
  9088. else if (11 == arm_insn_r->decode || 13 == arm_insn_r->decode
  9089. || 15 == arm_insn_r->decode)
  9090. {
  9091. if ((opcode1 & 0x12) == 2)
  9092. {
  9093. /* Extra load/store (unprivileged) */
  9094. return -1;
  9095. }
  9096. else
  9097. {
  9098. /* Extra load/store */
  9099. switch (bits (arm_insn_r->arm_insn, 5, 6))
  9100. {
  9101. case 1:
  9102. if ((opcode1 & 0x05) == 0x0 || (opcode1 & 0x05) == 0x4)
  9103. {
  9104. /* STRH (register), STRH (immediate) */
  9105. arm_record_strx (arm_insn_r, &record_buf[0],
  9106. &record_buf_mem[0], ARM_RECORD_STRH);
  9107. }
  9108. else if ((opcode1 & 0x05) == 0x1)
  9109. {
  9110. /* LDRH (register) */
  9111. record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
  9112. arm_insn_r->reg_rec_count = 1;
  9113. if (bit (arm_insn_r->arm_insn, 21))
  9114. {
  9115. /* Write back to Rn. */
  9116. record_buf[arm_insn_r->reg_rec_count++]
  9117. = bits (arm_insn_r->arm_insn, 16, 19);
  9118. }
  9119. }
  9120. else if ((opcode1 & 0x05) == 0x5)
  9121. {
  9122. /* LDRH (immediate), LDRH (literal) */
  9123. int rn = bits (arm_insn_r->arm_insn, 16, 19);
  9124. record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
  9125. arm_insn_r->reg_rec_count = 1;
  9126. if (rn != 15)
  9127. {
  9128. /*LDRH (immediate) */
  9129. if (bit (arm_insn_r->arm_insn, 21))
  9130. {
  9131. /* Write back to Rn. */
  9132. record_buf[arm_insn_r->reg_rec_count++] = rn;
  9133. }
  9134. }
  9135. }
  9136. else
  9137. return -1;
  9138. break;
  9139. case 2:
  9140. if ((opcode1 & 0x05) == 0x0)
  9141. {
  9142. /* LDRD (register) */
  9143. record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
  9144. record_buf[1] = record_buf[0] + 1;
  9145. arm_insn_r->reg_rec_count = 2;
  9146. if (bit (arm_insn_r->arm_insn, 21))
  9147. {
  9148. /* Write back to Rn. */
  9149. record_buf[arm_insn_r->reg_rec_count++]
  9150. = bits (arm_insn_r->arm_insn, 16, 19);
  9151. }
  9152. }
  9153. else if ((opcode1 & 0x05) == 0x1)
  9154. {
  9155. /* LDRSB (register) */
  9156. record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
  9157. arm_insn_r->reg_rec_count = 1;
  9158. if (bit (arm_insn_r->arm_insn, 21))
  9159. {
  9160. /* Write back to Rn. */
  9161. record_buf[arm_insn_r->reg_rec_count++]
  9162. = bits (arm_insn_r->arm_insn, 16, 19);
  9163. }
  9164. }
  9165. else if ((opcode1 & 0x05) == 0x4 || (opcode1 & 0x05) == 0x5)
  9166. {
  9167. /* LDRD (immediate), LDRD (literal), LDRSB (immediate),
  9168. LDRSB (literal) */
  9169. int rn = bits (arm_insn_r->arm_insn, 16, 19);
  9170. record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
  9171. arm_insn_r->reg_rec_count = 1;
  9172. if (rn != 15)
  9173. {
  9174. /*LDRD (immediate), LDRSB (immediate) */
  9175. if (bit (arm_insn_r->arm_insn, 21))
  9176. {
  9177. /* Write back to Rn. */
  9178. record_buf[arm_insn_r->reg_rec_count++] = rn;
  9179. }
  9180. }
  9181. }
  9182. else
  9183. return -1;
  9184. break;
  9185. case 3:
  9186. if ((opcode1 & 0x05) == 0x0)
  9187. {
  9188. /* STRD (register) */
  9189. arm_record_strx (arm_insn_r, &record_buf[0],
  9190. &record_buf_mem[0], ARM_RECORD_STRD);
  9191. }
  9192. else if ((opcode1 & 0x05) == 0x1)
  9193. {
  9194. /* LDRSH (register) */
  9195. record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
  9196. arm_insn_r->reg_rec_count = 1;
  9197. if (bit (arm_insn_r->arm_insn, 21))
  9198. {
  9199. /* Write back to Rn. */
  9200. record_buf[arm_insn_r->reg_rec_count++]
  9201. = bits (arm_insn_r->arm_insn, 16, 19);
  9202. }
  9203. }
  9204. else if ((opcode1 & 0x05) == 0x4)
  9205. {
  9206. /* STRD (immediate) */
  9207. arm_record_strx (arm_insn_r, &record_buf[0],
  9208. &record_buf_mem[0], ARM_RECORD_STRD);
  9209. }
  9210. else if ((opcode1 & 0x05) == 0x5)
  9211. {
  9212. /* LDRSH (immediate), LDRSH (literal) */
  9213. record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
  9214. arm_insn_r->reg_rec_count = 1;
  9215. if (bit (arm_insn_r->arm_insn, 21))
  9216. {
  9217. /* Write back to Rn. */
  9218. record_buf[arm_insn_r->reg_rec_count++]
  9219. = bits (arm_insn_r->arm_insn, 16, 19);
  9220. }
  9221. }
  9222. else
  9223. return -1;
  9224. break;
  9225. default:
  9226. return -1;
  9227. }
  9228. }
  9229. }
  9230. else
  9231. {
  9232. return -1;
  9233. }
  9234. REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
  9235. MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
  9236. return 0;
  9237. }
  9238. /* Handling opcode 001 insns. */
  9239. static int
  9240. arm_record_data_proc_imm (insn_decode_record *arm_insn_r)
  9241. {
  9242. uint32_t record_buf[8], record_buf_mem[8];
  9243. arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 21, 24);
  9244. arm_insn_r->decode = bits (arm_insn_r->arm_insn, 4, 7);
  9245. if ((9 == arm_insn_r->opcode || 11 == arm_insn_r->opcode)
  9246. && 2 == bits (arm_insn_r->arm_insn, 20, 21)
  9247. && sbo_sbz (arm_insn_r->arm_insn, 13, 4, 1)
  9248. )
  9249. {
  9250. /* Handle MSR insn. */
  9251. if (9 == arm_insn_r->opcode)
  9252. {
  9253. /* CSPR is going to be changed. */
  9254. record_buf[0] = ARM_PS_REGNUM;
  9255. arm_insn_r->reg_rec_count = 1;
  9256. }
  9257. else
  9258. {
  9259. /* SPSR is going to be changed. */
  9260. }
  9261. }
  9262. else if (arm_insn_r->opcode <= 15)
  9263. {
  9264. /* Normal data processing insns. */
  9265. /* Out of 11 shifter operands mode, all the insn modifies destination
  9266. register, which is specified by 13-16 decode. */
  9267. record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
  9268. record_buf[1] = ARM_PS_REGNUM;
  9269. arm_insn_r->reg_rec_count = 2;
  9270. }
  9271. else
  9272. {
  9273. return -1;
  9274. }
  9275. REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
  9276. MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
  9277. return 0;
  9278. }
  9279. static int
  9280. arm_record_media (insn_decode_record *arm_insn_r)
  9281. {
  9282. uint32_t record_buf[8];
  9283. switch (bits (arm_insn_r->arm_insn, 22, 24))
  9284. {
  9285. case 0:
  9286. /* Parallel addition and subtraction, signed */
  9287. case 1:
  9288. /* Parallel addition and subtraction, unsigned */
  9289. case 2:
  9290. case 3:
  9291. /* Packing, unpacking, saturation and reversal */
  9292. {
  9293. int rd = bits (arm_insn_r->arm_insn, 12, 15);
  9294. record_buf[arm_insn_r->reg_rec_count++] = rd;
  9295. }
  9296. break;
  9297. case 4:
  9298. case 5:
  9299. /* Signed multiplies */
  9300. {
  9301. int rd = bits (arm_insn_r->arm_insn, 16, 19);
  9302. unsigned int op1 = bits (arm_insn_r->arm_insn, 20, 22);
  9303. record_buf[arm_insn_r->reg_rec_count++] = rd;
  9304. if (op1 == 0x0)
  9305. record_buf[arm_insn_r->reg_rec_count++] = ARM_PS_REGNUM;
  9306. else if (op1 == 0x4)
  9307. record_buf[arm_insn_r->reg_rec_count++]
  9308. = bits (arm_insn_r->arm_insn, 12, 15);
  9309. }
  9310. break;
  9311. case 6:
  9312. {
  9313. if (bit (arm_insn_r->arm_insn, 21)
  9314. && bits (arm_insn_r->arm_insn, 5, 6) == 0x2)
  9315. {
  9316. /* SBFX */
  9317. record_buf[arm_insn_r->reg_rec_count++]
  9318. = bits (arm_insn_r->arm_insn, 12, 15);
  9319. }
  9320. else if (bits (arm_insn_r->arm_insn, 20, 21) == 0x0
  9321. && bits (arm_insn_r->arm_insn, 5, 7) == 0x0)
  9322. {
  9323. /* USAD8 and USADA8 */
  9324. record_buf[arm_insn_r->reg_rec_count++]
  9325. = bits (arm_insn_r->arm_insn, 16, 19);
  9326. }
  9327. }
  9328. break;
  9329. case 7:
  9330. {
  9331. if (bits (arm_insn_r->arm_insn, 20, 21) == 0x3
  9332. && bits (arm_insn_r->arm_insn, 5, 7) == 0x7)
  9333. {
  9334. /* Permanently UNDEFINED */
  9335. return -1;
  9336. }
  9337. else
  9338. {
  9339. /* BFC, BFI and UBFX */
  9340. record_buf[arm_insn_r->reg_rec_count++]
  9341. = bits (arm_insn_r->arm_insn, 12, 15);
  9342. }
  9343. }
  9344. break;
  9345. default:
  9346. return -1;
  9347. }
  9348. REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
  9349. return 0;
  9350. }
  9351. /* Handle ARM mode instructions with opcode 010. */
  9352. static int
  9353. arm_record_ld_st_imm_offset (insn_decode_record *arm_insn_r)
  9354. {
  9355. struct regcache *reg_cache = arm_insn_r->regcache;
  9356. uint32_t reg_base , reg_dest;
  9357. uint32_t offset_12, tgt_mem_addr;
  9358. uint32_t record_buf[8], record_buf_mem[8];
  9359. unsigned char wback;
  9360. ULONGEST u_regval;
  9361. /* Calculate wback. */
  9362. wback = (bit (arm_insn_r->arm_insn, 24) == 0)
  9363. || (bit (arm_insn_r->arm_insn, 21) == 1);
  9364. arm_insn_r->reg_rec_count = 0;
  9365. reg_base = bits (arm_insn_r->arm_insn, 16, 19);
  9366. if (bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM))
  9367. {
  9368. /* LDR (immediate), LDR (literal), LDRB (immediate), LDRB (literal), LDRBT
  9369. and LDRT. */
  9370. reg_dest = bits (arm_insn_r->arm_insn, 12, 15);
  9371. record_buf[arm_insn_r->reg_rec_count++] = reg_dest;
  9372. /* The LDR instruction is capable of doing branching. If MOV LR, PC
  9373. preceeds a LDR instruction having R15 as reg_base, it
  9374. emulates a branch and link instruction, and hence we need to save
  9375. CPSR and PC as well. */
  9376. if (ARM_PC_REGNUM == reg_dest)
  9377. record_buf[arm_insn_r->reg_rec_count++] = ARM_PS_REGNUM;
  9378. /* If wback is true, also save the base register, which is going to be
  9379. written to. */
  9380. if (wback)
  9381. record_buf[arm_insn_r->reg_rec_count++] = reg_base;
  9382. }
  9383. else
  9384. {
  9385. /* STR (immediate), STRB (immediate), STRBT and STRT. */
  9386. offset_12 = bits (arm_insn_r->arm_insn, 0, 11);
  9387. regcache_raw_read_unsigned (reg_cache, reg_base, &u_regval);
  9388. /* Handle bit U. */
  9389. if (bit (arm_insn_r->arm_insn, 23))
  9390. {
  9391. /* U == 1: Add the offset. */
  9392. tgt_mem_addr = (uint32_t) u_regval + offset_12;
  9393. }
  9394. else
  9395. {
  9396. /* U == 0: subtract the offset. */
  9397. tgt_mem_addr = (uint32_t) u_regval - offset_12;
  9398. }
  9399. /* Bit 22 tells us whether the store instruction writes 1 byte or 4
  9400. bytes. */
  9401. if (bit (arm_insn_r->arm_insn, 22))
  9402. {
  9403. /* STRB and STRBT: 1 byte. */
  9404. record_buf_mem[0] = 1;
  9405. }
  9406. else
  9407. {
  9408. /* STR and STRT: 4 bytes. */
  9409. record_buf_mem[0] = 4;
  9410. }
  9411. /* Handle bit P. */
  9412. if (bit (arm_insn_r->arm_insn, 24))
  9413. record_buf_mem[1] = tgt_mem_addr;
  9414. else
  9415. record_buf_mem[1] = (uint32_t) u_regval;
  9416. arm_insn_r->mem_rec_count = 1;
  9417. /* If wback is true, also save the base register, which is going to be
  9418. written to. */
  9419. if (wback)
  9420. record_buf[arm_insn_r->reg_rec_count++] = reg_base;
  9421. }
  9422. REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
  9423. MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
  9424. return 0;
  9425. }
  9426. /* Handling opcode 011 insns. */
  9427. static int
  9428. arm_record_ld_st_reg_offset (insn_decode_record *arm_insn_r)
  9429. {
  9430. struct regcache *reg_cache = arm_insn_r->regcache;
  9431. uint32_t shift_imm = 0;
  9432. uint32_t reg_src1 = 0, reg_src2 = 0, reg_dest = 0;
  9433. uint32_t offset_12 = 0, tgt_mem_addr = 0;
  9434. uint32_t record_buf[8], record_buf_mem[8];
  9435. LONGEST s_word;
  9436. ULONGEST u_regval[2];
  9437. if (bit (arm_insn_r->arm_insn, 4))
  9438. return arm_record_media (arm_insn_r);
  9439. arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 21, 24);
  9440. arm_insn_r->decode = bits (arm_insn_r->arm_insn, 4, 7);
  9441. /* Handle enhanced store insns and LDRD DSP insn,
  9442. order begins according to addressing modes for store insns
  9443. STRH insn. */
  9444. /* LDR or STR? */
  9445. if (bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM))
  9446. {
  9447. reg_dest = bits (arm_insn_r->arm_insn, 12, 15);
  9448. /* LDR insn has a capability to do branching, if
  9449. MOV LR, PC is preceded by LDR insn having Rn as R15
  9450. in that case, it emulates branch and link insn, and hence we
  9451. need to save CSPR and PC as well. */
  9452. if (15 != reg_dest)
  9453. {
  9454. record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
  9455. arm_insn_r->reg_rec_count = 1;
  9456. }
  9457. else
  9458. {
  9459. record_buf[0] = reg_dest;
  9460. record_buf[1] = ARM_PS_REGNUM;
  9461. arm_insn_r->reg_rec_count = 2;
  9462. }
  9463. }
  9464. else
  9465. {
  9466. if (! bits (arm_insn_r->arm_insn, 4, 11))
  9467. {
  9468. /* Store insn, register offset and register pre-indexed,
  9469. register post-indexed. */
  9470. /* Get Rm. */
  9471. reg_src1 = bits (arm_insn_r->arm_insn, 0, 3);
  9472. /* Get Rn. */
  9473. reg_src2 = bits (arm_insn_r->arm_insn, 16, 19);
  9474. regcache_raw_read_unsigned (reg_cache, reg_src1
  9475. , &u_regval[0]);
  9476. regcache_raw_read_unsigned (reg_cache, reg_src2
  9477. , &u_regval[1]);
  9478. if (15 == reg_src2)
  9479. {
  9480. /* If R15 was used as Rn, hence current PC+8. */
  9481. /* Pre-indexed mode doesnt reach here ; illegal insn. */
  9482. u_regval[0] = u_regval[0] + 8;
  9483. }
  9484. /* Calculate target store address, Rn +/- Rm, register offset. */
  9485. /* U == 1. */
  9486. if (bit (arm_insn_r->arm_insn, 23))
  9487. {
  9488. tgt_mem_addr = u_regval[0] + u_regval[1];
  9489. }
  9490. else
  9491. {
  9492. tgt_mem_addr = u_regval[1] - u_regval[0];
  9493. }
  9494. switch (arm_insn_r->opcode)
  9495. {
  9496. /* STR. */
  9497. case 8:
  9498. case 12:
  9499. /* STR. */
  9500. case 9:
  9501. case 13:
  9502. /* STRT. */
  9503. case 1:
  9504. case 5:
  9505. /* STR. */
  9506. case 0:
  9507. case 4:
  9508. record_buf_mem[0] = 4;
  9509. break;
  9510. /* STRB. */
  9511. case 10:
  9512. case 14:
  9513. /* STRB. */
  9514. case 11:
  9515. case 15:
  9516. /* STRBT. */
  9517. case 3:
  9518. case 7:
  9519. /* STRB. */
  9520. case 2:
  9521. case 6:
  9522. record_buf_mem[0] = 1;
  9523. break;
  9524. default:
  9525. gdb_assert_not_reached ("no decoding pattern found");
  9526. break;
  9527. }
  9528. record_buf_mem[1] = tgt_mem_addr;
  9529. arm_insn_r->mem_rec_count = 1;
  9530. if (9 == arm_insn_r->opcode || 11 == arm_insn_r->opcode
  9531. || 13 == arm_insn_r->opcode || 15 == arm_insn_r->opcode
  9532. || 0 == arm_insn_r->opcode || 2 == arm_insn_r->opcode
  9533. || 4 == arm_insn_r->opcode || 6 == arm_insn_r->opcode
  9534. || 1 == arm_insn_r->opcode || 3 == arm_insn_r->opcode
  9535. || 5 == arm_insn_r->opcode || 7 == arm_insn_r->opcode
  9536. )
  9537. {
  9538. /* Rn is going to be changed in pre-indexed mode and
  9539. post-indexed mode as well. */
  9540. record_buf[0] = reg_src2;
  9541. arm_insn_r->reg_rec_count = 1;
  9542. }
  9543. }
  9544. else
  9545. {
  9546. /* Store insn, scaled register offset; scaled pre-indexed. */
  9547. offset_12 = bits (arm_insn_r->arm_insn, 5, 6);
  9548. /* Get Rm. */
  9549. reg_src1 = bits (arm_insn_r->arm_insn, 0, 3);
  9550. /* Get Rn. */
  9551. reg_src2 = bits (arm_insn_r->arm_insn, 16, 19);
  9552. /* Get shift_imm. */
  9553. shift_imm = bits (arm_insn_r->arm_insn, 7, 11);
  9554. regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
  9555. regcache_raw_read_signed (reg_cache, reg_src1, &s_word);
  9556. regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
  9557. /* Offset_12 used as shift. */
  9558. switch (offset_12)
  9559. {
  9560. case 0:
  9561. /* Offset_12 used as index. */
  9562. offset_12 = u_regval[0] << shift_imm;
  9563. break;
  9564. case 1:
  9565. offset_12 = (!shift_imm)?0:u_regval[0] >> shift_imm;
  9566. break;
  9567. case 2:
  9568. if (!shift_imm)
  9569. {
  9570. if (bit (u_regval[0], 31))
  9571. {
  9572. offset_12 = 0xFFFFFFFF;
  9573. }
  9574. else
  9575. {
  9576. offset_12 = 0;
  9577. }
  9578. }
  9579. else
  9580. {
  9581. /* This is arithmetic shift. */
  9582. offset_12 = s_word >> shift_imm;
  9583. }
  9584. break;
  9585. case 3:
  9586. if (!shift_imm)
  9587. {
  9588. regcache_raw_read_unsigned (reg_cache, ARM_PS_REGNUM,
  9589. &u_regval[1]);
  9590. /* Get C flag value and shift it by 31. */
  9591. offset_12 = (((bit (u_regval[1], 29)) << 31) \
  9592. | (u_regval[0]) >> 1);
  9593. }
  9594. else
  9595. {
  9596. offset_12 = (u_regval[0] >> shift_imm) \
  9597. | (u_regval[0] <<
  9598. (sizeof(uint32_t) - shift_imm));
  9599. }
  9600. break;
  9601. default:
  9602. gdb_assert_not_reached ("no decoding pattern found");
  9603. break;
  9604. }
  9605. regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
  9606. /* bit U set. */
  9607. if (bit (arm_insn_r->arm_insn, 23))
  9608. {
  9609. tgt_mem_addr = u_regval[1] + offset_12;
  9610. }
  9611. else
  9612. {
  9613. tgt_mem_addr = u_regval[1] - offset_12;
  9614. }
  9615. switch (arm_insn_r->opcode)
  9616. {
  9617. /* STR. */
  9618. case 8:
  9619. case 12:
  9620. /* STR. */
  9621. case 9:
  9622. case 13:
  9623. /* STRT. */
  9624. case 1:
  9625. case 5:
  9626. /* STR. */
  9627. case 0:
  9628. case 4:
  9629. record_buf_mem[0] = 4;
  9630. break;
  9631. /* STRB. */
  9632. case 10:
  9633. case 14:
  9634. /* STRB. */
  9635. case 11:
  9636. case 15:
  9637. /* STRBT. */
  9638. case 3:
  9639. case 7:
  9640. /* STRB. */
  9641. case 2:
  9642. case 6:
  9643. record_buf_mem[0] = 1;
  9644. break;
  9645. default:
  9646. gdb_assert_not_reached ("no decoding pattern found");
  9647. break;
  9648. }
  9649. record_buf_mem[1] = tgt_mem_addr;
  9650. arm_insn_r->mem_rec_count = 1;
  9651. if (9 == arm_insn_r->opcode || 11 == arm_insn_r->opcode
  9652. || 13 == arm_insn_r->opcode || 15 == arm_insn_r->opcode
  9653. || 0 == arm_insn_r->opcode || 2 == arm_insn_r->opcode
  9654. || 4 == arm_insn_r->opcode || 6 == arm_insn_r->opcode
  9655. || 1 == arm_insn_r->opcode || 3 == arm_insn_r->opcode
  9656. || 5 == arm_insn_r->opcode || 7 == arm_insn_r->opcode
  9657. )
  9658. {
  9659. /* Rn is going to be changed in register scaled pre-indexed
  9660. mode,and scaled post indexed mode. */
  9661. record_buf[0] = reg_src2;
  9662. arm_insn_r->reg_rec_count = 1;
  9663. }
  9664. }
  9665. }
  9666. REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
  9667. MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
  9668. return 0;
  9669. }
  9670. /* Handle ARM mode instructions with opcode 100. */
  9671. static int
  9672. arm_record_ld_st_multiple (insn_decode_record *arm_insn_r)
  9673. {
  9674. struct regcache *reg_cache = arm_insn_r->regcache;
  9675. uint32_t register_count = 0, register_bits;
  9676. uint32_t reg_base, addr_mode;
  9677. uint32_t record_buf[24], record_buf_mem[48];
  9678. uint32_t wback;
  9679. ULONGEST u_regval;
  9680. /* Fetch the list of registers. */
  9681. register_bits = bits (arm_insn_r->arm_insn, 0, 15);
  9682. arm_insn_r->reg_rec_count = 0;
  9683. /* Fetch the base register that contains the address we are loading data
  9684. to. */
  9685. reg_base = bits (arm_insn_r->arm_insn, 16, 19);
  9686. /* Calculate wback. */
  9687. wback = (bit (arm_insn_r->arm_insn, 21) == 1);
  9688. if (bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM))
  9689. {
  9690. /* LDM/LDMIA/LDMFD, LDMDA/LDMFA, LDMDB and LDMIB. */
  9691. /* Find out which registers are going to be loaded from memory. */
  9692. while (register_bits)
  9693. {
  9694. if (register_bits & 0x00000001)
  9695. record_buf[arm_insn_r->reg_rec_count++] = register_count;
  9696. register_bits = register_bits >> 1;
  9697. register_count++;
  9698. }
  9699. /* If wback is true, also save the base register, which is going to be
  9700. written to. */
  9701. if (wback)
  9702. record_buf[arm_insn_r->reg_rec_count++] = reg_base;
  9703. /* Save the CPSR register. */
  9704. record_buf[arm_insn_r->reg_rec_count++] = ARM_PS_REGNUM;
  9705. }
  9706. else
  9707. {
  9708. /* STM (STMIA, STMEA), STMDA (STMED), STMDB (STMFD) and STMIB (STMFA). */
  9709. addr_mode = bits (arm_insn_r->arm_insn, 23, 24);
  9710. regcache_raw_read_unsigned (reg_cache, reg_base, &u_regval);
  9711. /* Find out how many registers are going to be stored to memory. */
  9712. while (register_bits)
  9713. {
  9714. if (register_bits & 0x00000001)
  9715. register_count++;
  9716. register_bits = register_bits >> 1;
  9717. }
  9718. switch (addr_mode)
  9719. {
  9720. /* STMDA (STMED): Decrement after. */
  9721. case 0:
  9722. record_buf_mem[1] = (uint32_t) u_regval
  9723. - register_count * ARM_INT_REGISTER_SIZE + 4;
  9724. break;
  9725. /* STM (STMIA, STMEA): Increment after. */
  9726. case 1:
  9727. record_buf_mem[1] = (uint32_t) u_regval;
  9728. break;
  9729. /* STMDB (STMFD): Decrement before. */
  9730. case 2:
  9731. record_buf_mem[1] = (uint32_t) u_regval
  9732. - register_count * ARM_INT_REGISTER_SIZE;
  9733. break;
  9734. /* STMIB (STMFA): Increment before. */
  9735. case 3:
  9736. record_buf_mem[1] = (uint32_t) u_regval + ARM_INT_REGISTER_SIZE;
  9737. break;
  9738. default:
  9739. gdb_assert_not_reached ("no decoding pattern found");
  9740. break;
  9741. }
  9742. record_buf_mem[0] = register_count * ARM_INT_REGISTER_SIZE;
  9743. arm_insn_r->mem_rec_count = 1;
  9744. /* If wback is true, also save the base register, which is going to be
  9745. written to. */
  9746. if (wback)
  9747. record_buf[arm_insn_r->reg_rec_count++] = reg_base;
  9748. }
  9749. REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
  9750. MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
  9751. return 0;
  9752. }
  9753. /* Handling opcode 101 insns. */
  9754. static int
  9755. arm_record_b_bl (insn_decode_record *arm_insn_r)
  9756. {
  9757. uint32_t record_buf[8];
  9758. /* Handle B, BL, BLX(1) insns. */
  9759. /* B simply branches so we do nothing here. */
  9760. /* Note: BLX(1) doesnt fall here but instead it falls into
  9761. extension space. */
  9762. if (bit (arm_insn_r->arm_insn, 24))
  9763. {
  9764. record_buf[0] = ARM_LR_REGNUM;
  9765. arm_insn_r->reg_rec_count = 1;
  9766. }
  9767. REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
  9768. return 0;
  9769. }
  9770. static int
  9771. arm_record_unsupported_insn (insn_decode_record *arm_insn_r)
  9772. {
  9773. gdb_printf (gdb_stderr,
  9774. _("Process record does not support instruction "
  9775. "0x%0x at address %s.\n"),arm_insn_r->arm_insn,
  9776. paddress (arm_insn_r->gdbarch, arm_insn_r->this_addr));
  9777. return -1;
  9778. }
  9779. /* Record handler for vector data transfer instructions. */
  9780. static int
  9781. arm_record_vdata_transfer_insn (insn_decode_record *arm_insn_r)
  9782. {
  9783. uint32_t bits_a, bit_c, bit_l, reg_t, reg_v;
  9784. uint32_t record_buf[4];
  9785. reg_t = bits (arm_insn_r->arm_insn, 12, 15);
  9786. reg_v = bits (arm_insn_r->arm_insn, 21, 23);
  9787. bits_a = bits (arm_insn_r->arm_insn, 21, 23);
  9788. bit_l = bit (arm_insn_r->arm_insn, 20);
  9789. bit_c = bit (arm_insn_r->arm_insn, 8);
  9790. /* Handle VMOV instruction. */
  9791. if (bit_l && bit_c)
  9792. {
  9793. record_buf[0] = reg_t;
  9794. arm_insn_r->reg_rec_count = 1;
  9795. }
  9796. else if (bit_l && !bit_c)
  9797. {
  9798. /* Handle VMOV instruction. */
  9799. if (bits_a == 0x00)
  9800. {
  9801. record_buf[0] = reg_t;
  9802. arm_insn_r->reg_rec_count = 1;
  9803. }
  9804. /* Handle VMRS instruction. */
  9805. else if (bits_a == 0x07)
  9806. {
  9807. if (reg_t == 15)
  9808. reg_t = ARM_PS_REGNUM;
  9809. record_buf[0] = reg_t;
  9810. arm_insn_r->reg_rec_count = 1;
  9811. }
  9812. }
  9813. else if (!bit_l && !bit_c)
  9814. {
  9815. /* Handle VMOV instruction. */
  9816. if (bits_a == 0x00)
  9817. {
  9818. record_buf[0] = ARM_D0_REGNUM + reg_v;
  9819. arm_insn_r->reg_rec_count = 1;
  9820. }
  9821. /* Handle VMSR instruction. */
  9822. else if (bits_a == 0x07)
  9823. {
  9824. record_buf[0] = ARM_FPSCR_REGNUM;
  9825. arm_insn_r->reg_rec_count = 1;
  9826. }
  9827. }
  9828. else if (!bit_l && bit_c)
  9829. {
  9830. /* Handle VMOV instruction. */
  9831. if (!(bits_a & 0x04))
  9832. {
  9833. record_buf[0] = (reg_v | (bit (arm_insn_r->arm_insn, 7) << 4))
  9834. + ARM_D0_REGNUM;
  9835. arm_insn_r->reg_rec_count = 1;
  9836. }
  9837. /* Handle VDUP instruction. */
  9838. else
  9839. {
  9840. if (bit (arm_insn_r->arm_insn, 21))
  9841. {
  9842. reg_v = reg_v | (bit (arm_insn_r->arm_insn, 7) << 4);
  9843. record_buf[0] = reg_v + ARM_D0_REGNUM;
  9844. record_buf[1] = reg_v + ARM_D0_REGNUM + 1;
  9845. arm_insn_r->reg_rec_count = 2;
  9846. }
  9847. else
  9848. {
  9849. reg_v = reg_v | (bit (arm_insn_r->arm_insn, 7) << 4);
  9850. record_buf[0] = reg_v + ARM_D0_REGNUM;
  9851. arm_insn_r->reg_rec_count = 1;
  9852. }
  9853. }
  9854. }
  9855. REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
  9856. return 0;
  9857. }
  9858. /* Record handler for extension register load/store instructions. */
  9859. static int
  9860. arm_record_exreg_ld_st_insn (insn_decode_record *arm_insn_r)
  9861. {
  9862. uint32_t opcode, single_reg;
  9863. uint8_t op_vldm_vstm;
  9864. uint32_t record_buf[8], record_buf_mem[128];
  9865. ULONGEST u_regval = 0;
  9866. struct regcache *reg_cache = arm_insn_r->regcache;
  9867. opcode = bits (arm_insn_r->arm_insn, 20, 24);
  9868. single_reg = !bit (arm_insn_r->arm_insn, 8);
  9869. op_vldm_vstm = opcode & 0x1b;
  9870. /* Handle VMOV instructions. */
  9871. if ((opcode & 0x1e) == 0x04)
  9872. {
  9873. if (bit (arm_insn_r->arm_insn, 20)) /* to_arm_registers bit 20? */
  9874. {
  9875. record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
  9876. record_buf[1] = bits (arm_insn_r->arm_insn, 16, 19);
  9877. arm_insn_r->reg_rec_count = 2;
  9878. }
  9879. else
  9880. {
  9881. uint8_t reg_m = bits (arm_insn_r->arm_insn, 0, 3);
  9882. uint8_t bit_m = bit (arm_insn_r->arm_insn, 5);
  9883. if (single_reg)
  9884. {
  9885. /* The first S register number m is REG_M:M (M is bit 5),
  9886. the corresponding D register number is REG_M:M / 2, which
  9887. is REG_M. */
  9888. record_buf[arm_insn_r->reg_rec_count++] = ARM_D0_REGNUM + reg_m;
  9889. /* The second S register number is REG_M:M + 1, the
  9890. corresponding D register number is (REG_M:M + 1) / 2.
  9891. IOW, if bit M is 1, the first and second S registers
  9892. are mapped to different D registers, otherwise, they are
  9893. in the same D register. */
  9894. if (bit_m)
  9895. {
  9896. record_buf[arm_insn_r->reg_rec_count++]
  9897. = ARM_D0_REGNUM + reg_m + 1;
  9898. }
  9899. }
  9900. else
  9901. {
  9902. record_buf[0] = ((bit_m << 4) + reg_m + ARM_D0_REGNUM);
  9903. arm_insn_r->reg_rec_count = 1;
  9904. }
  9905. }
  9906. }
  9907. /* Handle VSTM and VPUSH instructions. */
  9908. else if (op_vldm_vstm == 0x08 || op_vldm_vstm == 0x0a
  9909. || op_vldm_vstm == 0x12)
  9910. {
  9911. uint32_t start_address, reg_rn, imm_off32, imm_off8, memory_count;
  9912. uint32_t memory_index = 0;
  9913. reg_rn = bits (arm_insn_r->arm_insn, 16, 19);
  9914. regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval);
  9915. imm_off8 = bits (arm_insn_r->arm_insn, 0, 7);
  9916. imm_off32 = imm_off8 << 2;
  9917. memory_count = imm_off8;
  9918. if (bit (arm_insn_r->arm_insn, 23))
  9919. start_address = u_regval;
  9920. else
  9921. start_address = u_regval - imm_off32;
  9922. if (bit (arm_insn_r->arm_insn, 21))
  9923. {
  9924. record_buf[0] = reg_rn;
  9925. arm_insn_r->reg_rec_count = 1;
  9926. }
  9927. while (memory_count > 0)
  9928. {
  9929. if (single_reg)
  9930. {
  9931. record_buf_mem[memory_index] = 4;
  9932. record_buf_mem[memory_index + 1] = start_address;
  9933. start_address = start_address + 4;
  9934. memory_index = memory_index + 2;
  9935. }
  9936. else
  9937. {
  9938. record_buf_mem[memory_index] = 4;
  9939. record_buf_mem[memory_index + 1] = start_address;
  9940. record_buf_mem[memory_index + 2] = 4;
  9941. record_buf_mem[memory_index + 3] = start_address + 4;
  9942. start_address = start_address + 8;
  9943. memory_index = memory_index + 4;
  9944. }
  9945. memory_count--;
  9946. }
  9947. arm_insn_r->mem_rec_count = (memory_index >> 1);
  9948. }
  9949. /* Handle VLDM instructions. */
  9950. else if (op_vldm_vstm == 0x09 || op_vldm_vstm == 0x0b
  9951. || op_vldm_vstm == 0x13)
  9952. {
  9953. uint32_t reg_count, reg_vd;
  9954. uint32_t reg_index = 0;
  9955. uint32_t bit_d = bit (arm_insn_r->arm_insn, 22);
  9956. reg_vd = bits (arm_insn_r->arm_insn, 12, 15);
  9957. reg_count = bits (arm_insn_r->arm_insn, 0, 7);
  9958. /* REG_VD is the first D register number. If the instruction
  9959. loads memory to S registers (SINGLE_REG is TRUE), the register
  9960. number is (REG_VD << 1 | bit D), so the corresponding D
  9961. register number is (REG_VD << 1 | bit D) / 2 = REG_VD. */
  9962. if (!single_reg)
  9963. reg_vd = reg_vd | (bit_d << 4);
  9964. if (bit (arm_insn_r->arm_insn, 21) /* write back */)
  9965. record_buf[reg_index++] = bits (arm_insn_r->arm_insn, 16, 19);
  9966. /* If the instruction loads memory to D register, REG_COUNT should
  9967. be divided by 2, according to the ARM Architecture Reference
  9968. Manual. If the instruction loads memory to S register, divide by
  9969. 2 as well because two S registers are mapped to D register. */
  9970. reg_count = reg_count / 2;
  9971. if (single_reg && bit_d)
  9972. {
  9973. /* Increase the register count if S register list starts from
  9974. an odd number (bit d is one). */
  9975. reg_count++;
  9976. }
  9977. while (reg_count > 0)
  9978. {
  9979. record_buf[reg_index++] = ARM_D0_REGNUM + reg_vd + reg_count - 1;
  9980. reg_count--;
  9981. }
  9982. arm_insn_r->reg_rec_count = reg_index;
  9983. }
  9984. /* VSTR Vector store register. */
  9985. else if ((opcode & 0x13) == 0x10)
  9986. {
  9987. uint32_t start_address, reg_rn, imm_off32, imm_off8;
  9988. uint32_t memory_index = 0;
  9989. reg_rn = bits (arm_insn_r->arm_insn, 16, 19);
  9990. regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval);
  9991. imm_off8 = bits (arm_insn_r->arm_insn, 0, 7);
  9992. imm_off32 = imm_off8 << 2;
  9993. if (bit (arm_insn_r->arm_insn, 23))
  9994. start_address = u_regval + imm_off32;
  9995. else
  9996. start_address = u_regval - imm_off32;
  9997. if (single_reg)
  9998. {
  9999. record_buf_mem[memory_index] = 4;
  10000. record_buf_mem[memory_index + 1] = start_address;
  10001. arm_insn_r->mem_rec_count = 1;
  10002. }
  10003. else
  10004. {
  10005. record_buf_mem[memory_index] = 4;
  10006. record_buf_mem[memory_index + 1] = start_address;
  10007. record_buf_mem[memory_index + 2] = 4;
  10008. record_buf_mem[memory_index + 3] = start_address + 4;
  10009. arm_insn_r->mem_rec_count = 2;
  10010. }
  10011. }
  10012. /* VLDR Vector load register. */
  10013. else if ((opcode & 0x13) == 0x11)
  10014. {
  10015. uint32_t reg_vd = bits (arm_insn_r->arm_insn, 12, 15);
  10016. if (!single_reg)
  10017. {
  10018. reg_vd = reg_vd | (bit (arm_insn_r->arm_insn, 22) << 4);
  10019. record_buf[0] = ARM_D0_REGNUM + reg_vd;
  10020. }
  10021. else
  10022. {
  10023. reg_vd = (reg_vd << 1) | bit (arm_insn_r->arm_insn, 22);
  10024. /* Record register D rather than pseudo register S. */
  10025. record_buf[0] = ARM_D0_REGNUM + reg_vd / 2;
  10026. }
  10027. arm_insn_r->reg_rec_count = 1;
  10028. }
  10029. REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
  10030. MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
  10031. return 0;
  10032. }
  10033. /* Record handler for arm/thumb mode VFP data processing instructions. */
  10034. static int
  10035. arm_record_vfp_data_proc_insn (insn_decode_record *arm_insn_r)
  10036. {
  10037. uint32_t opc1, opc2, opc3, dp_op_sz, bit_d, reg_vd;
  10038. uint32_t record_buf[4];
  10039. enum insn_types {INSN_T0, INSN_T1, INSN_T2, INSN_T3, INSN_INV};
  10040. enum insn_types curr_insn_type = INSN_INV;
  10041. reg_vd = bits (arm_insn_r->arm_insn, 12, 15);
  10042. opc1 = bits (arm_insn_r->arm_insn, 20, 23);
  10043. opc2 = bits (arm_insn_r->arm_insn, 16, 19);
  10044. opc3 = bits (arm_insn_r->arm_insn, 6, 7);
  10045. dp_op_sz = bit (arm_insn_r->arm_insn, 8);
  10046. bit_d = bit (arm_insn_r->arm_insn, 22);
  10047. /* Mask off the "D" bit. */
  10048. opc1 = opc1 & ~0x04;
  10049. /* Handle VMLA, VMLS. */
  10050. if (opc1 == 0x00)
  10051. {
  10052. if (bit (arm_insn_r->arm_insn, 10))
  10053. {
  10054. if (bit (arm_insn_r->arm_insn, 6))
  10055. curr_insn_type = INSN_T0;
  10056. else
  10057. curr_insn_type = INSN_T1;
  10058. }
  10059. else
  10060. {
  10061. if (dp_op_sz)
  10062. curr_insn_type = INSN_T1;
  10063. else
  10064. curr_insn_type = INSN_T2;
  10065. }
  10066. }
  10067. /* Handle VNMLA, VNMLS, VNMUL. */
  10068. else if (opc1 == 0x01)
  10069. {
  10070. if (dp_op_sz)
  10071. curr_insn_type = INSN_T1;
  10072. else
  10073. curr_insn_type = INSN_T2;
  10074. }
  10075. /* Handle VMUL. */
  10076. else if (opc1 == 0x02 && !(opc3 & 0x01))
  10077. {
  10078. if (bit (arm_insn_r->arm_insn, 10))
  10079. {
  10080. if (bit (arm_insn_r->arm_insn, 6))
  10081. curr_insn_type = INSN_T0;
  10082. else
  10083. curr_insn_type = INSN_T1;
  10084. }
  10085. else
  10086. {
  10087. if (dp_op_sz)
  10088. curr_insn_type = INSN_T1;
  10089. else
  10090. curr_insn_type = INSN_T2;
  10091. }
  10092. }
  10093. /* Handle VADD, VSUB. */
  10094. else if (opc1 == 0x03)
  10095. {
  10096. if (!bit (arm_insn_r->arm_insn, 9))
  10097. {
  10098. if (bit (arm_insn_r->arm_insn, 6))
  10099. curr_insn_type = INSN_T0;
  10100. else
  10101. curr_insn_type = INSN_T1;
  10102. }
  10103. else
  10104. {
  10105. if (dp_op_sz)
  10106. curr_insn_type = INSN_T1;
  10107. else
  10108. curr_insn_type = INSN_T2;
  10109. }
  10110. }
  10111. /* Handle VDIV. */
  10112. else if (opc1 == 0x08)
  10113. {
  10114. if (dp_op_sz)
  10115. curr_insn_type = INSN_T1;
  10116. else
  10117. curr_insn_type = INSN_T2;
  10118. }
  10119. /* Handle all other vfp data processing instructions. */
  10120. else if (opc1 == 0x0b)
  10121. {
  10122. /* Handle VMOV. */
  10123. if (!(opc3 & 0x01) || (opc2 == 0x00 && opc3 == 0x01))
  10124. {
  10125. if (bit (arm_insn_r->arm_insn, 4))
  10126. {
  10127. if (bit (arm_insn_r->arm_insn, 6))
  10128. curr_insn_type = INSN_T0;
  10129. else
  10130. curr_insn_type = INSN_T1;
  10131. }
  10132. else
  10133. {
  10134. if (dp_op_sz)
  10135. curr_insn_type = INSN_T1;
  10136. else
  10137. curr_insn_type = INSN_T2;
  10138. }
  10139. }
  10140. /* Handle VNEG and VABS. */
  10141. else if ((opc2 == 0x01 && opc3 == 0x01)
  10142. || (opc2 == 0x00 && opc3 == 0x03))
  10143. {
  10144. if (!bit (arm_insn_r->arm_insn, 11))
  10145. {
  10146. if (bit (arm_insn_r->arm_insn, 6))
  10147. curr_insn_type = INSN_T0;
  10148. else
  10149. curr_insn_type = INSN_T1;
  10150. }
  10151. else
  10152. {
  10153. if (dp_op_sz)
  10154. curr_insn_type = INSN_T1;
  10155. else
  10156. curr_insn_type = INSN_T2;
  10157. }
  10158. }
  10159. /* Handle VSQRT. */
  10160. else if (opc2 == 0x01 && opc3 == 0x03)
  10161. {
  10162. if (dp_op_sz)
  10163. curr_insn_type = INSN_T1;
  10164. else
  10165. curr_insn_type = INSN_T2;
  10166. }
  10167. /* Handle VCVT. */
  10168. else if (opc2 == 0x07 && opc3 == 0x03)
  10169. {
  10170. if (!dp_op_sz)
  10171. curr_insn_type = INSN_T1;
  10172. else
  10173. curr_insn_type = INSN_T2;
  10174. }
  10175. else if (opc3 & 0x01)
  10176. {
  10177. /* Handle VCVT. */
  10178. if ((opc2 == 0x08) || (opc2 & 0x0e) == 0x0c)
  10179. {
  10180. if (!bit (arm_insn_r->arm_insn, 18))
  10181. curr_insn_type = INSN_T2;
  10182. else
  10183. {
  10184. if (dp_op_sz)
  10185. curr_insn_type = INSN_T1;
  10186. else
  10187. curr_insn_type = INSN_T2;
  10188. }
  10189. }
  10190. /* Handle VCVT. */
  10191. else if ((opc2 & 0x0e) == 0x0a || (opc2 & 0x0e) == 0x0e)
  10192. {
  10193. if (dp_op_sz)
  10194. curr_insn_type = INSN_T1;
  10195. else
  10196. curr_insn_type = INSN_T2;
  10197. }
  10198. /* Handle VCVTB, VCVTT. */
  10199. else if ((opc2 & 0x0e) == 0x02)
  10200. curr_insn_type = INSN_T2;
  10201. /* Handle VCMP, VCMPE. */
  10202. else if ((opc2 & 0x0e) == 0x04)
  10203. curr_insn_type = INSN_T3;
  10204. }
  10205. }
  10206. switch (curr_insn_type)
  10207. {
  10208. case INSN_T0:
  10209. reg_vd = reg_vd | (bit_d << 4);
  10210. record_buf[0] = reg_vd + ARM_D0_REGNUM;
  10211. record_buf[1] = reg_vd + ARM_D0_REGNUM + 1;
  10212. arm_insn_r->reg_rec_count = 2;
  10213. break;
  10214. case INSN_T1:
  10215. reg_vd = reg_vd | (bit_d << 4);
  10216. record_buf[0] = reg_vd + ARM_D0_REGNUM;
  10217. arm_insn_r->reg_rec_count = 1;
  10218. break;
  10219. case INSN_T2:
  10220. reg_vd = (reg_vd << 1) | bit_d;
  10221. record_buf[0] = reg_vd + ARM_D0_REGNUM;
  10222. arm_insn_r->reg_rec_count = 1;
  10223. break;
  10224. case INSN_T3:
  10225. record_buf[0] = ARM_FPSCR_REGNUM;
  10226. arm_insn_r->reg_rec_count = 1;
  10227. break;
  10228. default:
  10229. gdb_assert_not_reached ("no decoding pattern found");
  10230. break;
  10231. }
  10232. REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
  10233. return 0;
  10234. }
  10235. /* Handling opcode 110 insns. */
  10236. static int
  10237. arm_record_asimd_vfp_coproc (insn_decode_record *arm_insn_r)
  10238. {
  10239. uint32_t op1, op1_ebit, coproc;
  10240. coproc = bits (arm_insn_r->arm_insn, 8, 11);
  10241. op1 = bits (arm_insn_r->arm_insn, 20, 25);
  10242. op1_ebit = bit (arm_insn_r->arm_insn, 20);
  10243. if ((coproc & 0x0e) == 0x0a)
  10244. {
  10245. /* Handle extension register ld/st instructions. */
  10246. if (!(op1 & 0x20))
  10247. return arm_record_exreg_ld_st_insn (arm_insn_r);
  10248. /* 64-bit transfers between arm core and extension registers. */
  10249. if ((op1 & 0x3e) == 0x04)
  10250. return arm_record_exreg_ld_st_insn (arm_insn_r);
  10251. }
  10252. else
  10253. {
  10254. /* Handle coprocessor ld/st instructions. */
  10255. if (!(op1 & 0x3a))
  10256. {
  10257. /* Store. */
  10258. if (!op1_ebit)
  10259. return arm_record_unsupported_insn (arm_insn_r);
  10260. else
  10261. /* Load. */
  10262. return arm_record_unsupported_insn (arm_insn_r);
  10263. }
  10264. /* Move to coprocessor from two arm core registers. */
  10265. if (op1 == 0x4)
  10266. return arm_record_unsupported_insn (arm_insn_r);
  10267. /* Move to two arm core registers from coprocessor. */
  10268. if (op1 == 0x5)
  10269. {
  10270. uint32_t reg_t[2];
  10271. reg_t[0] = bits (arm_insn_r->arm_insn, 12, 15);
  10272. reg_t[1] = bits (arm_insn_r->arm_insn, 16, 19);
  10273. arm_insn_r->reg_rec_count = 2;
  10274. REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, reg_t);
  10275. return 0;
  10276. }
  10277. }
  10278. return arm_record_unsupported_insn (arm_insn_r);
  10279. }
  10280. /* Handling opcode 111 insns. */
  10281. static int
  10282. arm_record_coproc_data_proc (insn_decode_record *arm_insn_r)
  10283. {
  10284. uint32_t op, op1_ebit, coproc, bits_24_25;
  10285. arm_gdbarch_tdep *tdep
  10286. = (arm_gdbarch_tdep *) gdbarch_tdep (arm_insn_r->gdbarch);
  10287. struct regcache *reg_cache = arm_insn_r->regcache;
  10288. arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 24, 27);
  10289. coproc = bits (arm_insn_r->arm_insn, 8, 11);
  10290. op1_ebit = bit (arm_insn_r->arm_insn, 20);
  10291. op = bit (arm_insn_r->arm_insn, 4);
  10292. bits_24_25 = bits (arm_insn_r->arm_insn, 24, 25);
  10293. /* Handle arm SWI/SVC system call instructions. */
  10294. if (bits_24_25 == 0x3)
  10295. {
  10296. if (tdep->arm_syscall_record != NULL)
  10297. {
  10298. ULONGEST svc_operand, svc_number;
  10299. svc_operand = (0x00ffffff & arm_insn_r->arm_insn);
  10300. if (svc_operand) /* OABI. */
  10301. svc_number = svc_operand - 0x900000;
  10302. else /* EABI. */
  10303. regcache_raw_read_unsigned (reg_cache, 7, &svc_number);
  10304. return tdep->arm_syscall_record (reg_cache, svc_number);
  10305. }
  10306. else
  10307. {
  10308. gdb_printf (gdb_stderr, _("no syscall record support\n"));
  10309. return -1;
  10310. }
  10311. }
  10312. else if (bits_24_25 == 0x02)
  10313. {
  10314. if (op)
  10315. {
  10316. if ((coproc & 0x0e) == 0x0a)
  10317. {
  10318. /* 8, 16, and 32-bit transfer */
  10319. return arm_record_vdata_transfer_insn (arm_insn_r);
  10320. }
  10321. else
  10322. {
  10323. if (op1_ebit)
  10324. {
  10325. /* MRC, MRC2 */
  10326. uint32_t record_buf[1];
  10327. record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
  10328. if (record_buf[0] == 15)
  10329. record_buf[0] = ARM_PS_REGNUM;
  10330. arm_insn_r->reg_rec_count = 1;
  10331. REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count,
  10332. record_buf);
  10333. return 0;
  10334. }
  10335. else
  10336. {
  10337. /* MCR, MCR2 */
  10338. return -1;
  10339. }
  10340. }
  10341. }
  10342. else
  10343. {
  10344. if ((coproc & 0x0e) == 0x0a)
  10345. {
  10346. /* VFP data-processing instructions. */
  10347. return arm_record_vfp_data_proc_insn (arm_insn_r);
  10348. }
  10349. else
  10350. {
  10351. /* CDP, CDP2 */
  10352. return -1;
  10353. }
  10354. }
  10355. }
  10356. else
  10357. {
  10358. unsigned int op1 = bits (arm_insn_r->arm_insn, 20, 25);
  10359. if (op1 == 5)
  10360. {
  10361. if ((coproc & 0x0e) != 0x0a)
  10362. {
  10363. /* MRRC, MRRC2 */
  10364. return -1;
  10365. }
  10366. }
  10367. else if (op1 == 4 || op1 == 5)
  10368. {
  10369. if ((coproc & 0x0e) == 0x0a)
  10370. {
  10371. /* 64-bit transfers between ARM core and extension */
  10372. return -1;
  10373. }
  10374. else if (op1 == 4)
  10375. {
  10376. /* MCRR, MCRR2 */
  10377. return -1;
  10378. }
  10379. }
  10380. else if (op1 == 0 || op1 == 1)
  10381. {
  10382. /* UNDEFINED */
  10383. return -1;
  10384. }
  10385. else
  10386. {
  10387. if ((coproc & 0x0e) == 0x0a)
  10388. {
  10389. /* Extension register load/store */
  10390. }
  10391. else
  10392. {
  10393. /* STC, STC2, LDC, LDC2 */
  10394. }
  10395. return -1;
  10396. }
  10397. }
  10398. return -1;
  10399. }
  10400. /* Handling opcode 000 insns. */
  10401. static int
  10402. thumb_record_shift_add_sub (insn_decode_record *thumb_insn_r)
  10403. {
  10404. uint32_t record_buf[8];
  10405. uint32_t reg_src1 = 0;
  10406. reg_src1 = bits (thumb_insn_r->arm_insn, 0, 2);
  10407. record_buf[0] = ARM_PS_REGNUM;
  10408. record_buf[1] = reg_src1;
  10409. thumb_insn_r->reg_rec_count = 2;
  10410. REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
  10411. return 0;
  10412. }
  10413. /* Handling opcode 001 insns. */
  10414. static int
  10415. thumb_record_add_sub_cmp_mov (insn_decode_record *thumb_insn_r)
  10416. {
  10417. uint32_t record_buf[8];
  10418. uint32_t reg_src1 = 0;
  10419. reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
  10420. record_buf[0] = ARM_PS_REGNUM;
  10421. record_buf[1] = reg_src1;
  10422. thumb_insn_r->reg_rec_count = 2;
  10423. REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
  10424. return 0;
  10425. }
  10426. /* Handling opcode 010 insns. */
  10427. static int
  10428. thumb_record_ld_st_reg_offset (insn_decode_record *thumb_insn_r)
  10429. {
  10430. struct regcache *reg_cache = thumb_insn_r->regcache;
  10431. uint32_t record_buf[8], record_buf_mem[8];
  10432. uint32_t reg_src1 = 0, reg_src2 = 0;
  10433. uint32_t opcode1 = 0, opcode2 = 0, opcode3 = 0;
  10434. ULONGEST u_regval[2] = {0};
  10435. opcode1 = bits (thumb_insn_r->arm_insn, 10, 12);
  10436. if (bit (thumb_insn_r->arm_insn, 12))
  10437. {
  10438. /* Handle load/store register offset. */
  10439. uint32_t opB = bits (thumb_insn_r->arm_insn, 9, 11);
  10440. if (in_inclusive_range (opB, 4U, 7U))
  10441. {
  10442. /* LDR(2), LDRB(2) , LDRH(2), LDRSB, LDRSH. */
  10443. reg_src1 = bits (thumb_insn_r->arm_insn,0, 2);
  10444. record_buf[0] = reg_src1;
  10445. thumb_insn_r->reg_rec_count = 1;
  10446. }
  10447. else if (in_inclusive_range (opB, 0U, 2U))
  10448. {
  10449. /* STR(2), STRB(2), STRH(2) . */
  10450. reg_src1 = bits (thumb_insn_r->arm_insn, 3, 5);
  10451. reg_src2 = bits (thumb_insn_r->arm_insn, 6, 8);
  10452. regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
  10453. regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
  10454. if (0 == opB)
  10455. record_buf_mem[0] = 4; /* STR (2). */
  10456. else if (2 == opB)
  10457. record_buf_mem[0] = 1; /* STRB (2). */
  10458. else if (1 == opB)
  10459. record_buf_mem[0] = 2; /* STRH (2). */
  10460. record_buf_mem[1] = u_regval[0] + u_regval[1];
  10461. thumb_insn_r->mem_rec_count = 1;
  10462. }
  10463. }
  10464. else if (bit (thumb_insn_r->arm_insn, 11))
  10465. {
  10466. /* Handle load from literal pool. */
  10467. /* LDR(3). */
  10468. reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
  10469. record_buf[0] = reg_src1;
  10470. thumb_insn_r->reg_rec_count = 1;
  10471. }
  10472. else if (opcode1)
  10473. {
  10474. /* Special data instructions and branch and exchange */
  10475. opcode2 = bits (thumb_insn_r->arm_insn, 8, 9);
  10476. opcode3 = bits (thumb_insn_r->arm_insn, 0, 2);
  10477. if ((3 == opcode2) && (!opcode3))
  10478. {
  10479. /* Branch with exchange. */
  10480. record_buf[0] = ARM_PS_REGNUM;
  10481. thumb_insn_r->reg_rec_count = 1;
  10482. }
  10483. else
  10484. {
  10485. /* Format 8; special data processing insns. */
  10486. record_buf[0] = ARM_PS_REGNUM;
  10487. record_buf[1] = (bit (thumb_insn_r->arm_insn, 7) << 3
  10488. | bits (thumb_insn_r->arm_insn, 0, 2));
  10489. thumb_insn_r->reg_rec_count = 2;
  10490. }
  10491. }
  10492. else
  10493. {
  10494. /* Format 5; data processing insns. */
  10495. reg_src1 = bits (thumb_insn_r->arm_insn, 0, 2);
  10496. if (bit (thumb_insn_r->arm_insn, 7))
  10497. {
  10498. reg_src1 = reg_src1 + 8;
  10499. }
  10500. record_buf[0] = ARM_PS_REGNUM;
  10501. record_buf[1] = reg_src1;
  10502. thumb_insn_r->reg_rec_count = 2;
  10503. }
  10504. REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
  10505. MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
  10506. record_buf_mem);
  10507. return 0;
  10508. }
  10509. /* Handling opcode 001 insns. */
  10510. static int
  10511. thumb_record_ld_st_imm_offset (insn_decode_record *thumb_insn_r)
  10512. {
  10513. struct regcache *reg_cache = thumb_insn_r->regcache;
  10514. uint32_t record_buf[8], record_buf_mem[8];
  10515. uint32_t reg_src1 = 0;
  10516. uint32_t opcode = 0, immed_5 = 0;
  10517. ULONGEST u_regval = 0;
  10518. opcode = bits (thumb_insn_r->arm_insn, 11, 12);
  10519. if (opcode)
  10520. {
  10521. /* LDR(1). */
  10522. reg_src1 = bits (thumb_insn_r->arm_insn, 0, 2);
  10523. record_buf[0] = reg_src1;
  10524. thumb_insn_r->reg_rec_count = 1;
  10525. }
  10526. else
  10527. {
  10528. /* STR(1). */
  10529. reg_src1 = bits (thumb_insn_r->arm_insn, 3, 5);
  10530. immed_5 = bits (thumb_insn_r->arm_insn, 6, 10);
  10531. regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval);
  10532. record_buf_mem[0] = 4;
  10533. record_buf_mem[1] = u_regval + (immed_5 * 4);
  10534. thumb_insn_r->mem_rec_count = 1;
  10535. }
  10536. REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
  10537. MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
  10538. record_buf_mem);
  10539. return 0;
  10540. }
  10541. /* Handling opcode 100 insns. */
  10542. static int
  10543. thumb_record_ld_st_stack (insn_decode_record *thumb_insn_r)
  10544. {
  10545. struct regcache *reg_cache = thumb_insn_r->regcache;
  10546. uint32_t record_buf[8], record_buf_mem[8];
  10547. uint32_t reg_src1 = 0;
  10548. uint32_t opcode = 0, immed_8 = 0, immed_5 = 0;
  10549. ULONGEST u_regval = 0;
  10550. opcode = bits (thumb_insn_r->arm_insn, 11, 12);
  10551. if (3 == opcode)
  10552. {
  10553. /* LDR(4). */
  10554. reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
  10555. record_buf[0] = reg_src1;
  10556. thumb_insn_r->reg_rec_count = 1;
  10557. }
  10558. else if (1 == opcode)
  10559. {
  10560. /* LDRH(1). */
  10561. reg_src1 = bits (thumb_insn_r->arm_insn, 0, 2);
  10562. record_buf[0] = reg_src1;
  10563. thumb_insn_r->reg_rec_count = 1;
  10564. }
  10565. else if (2 == opcode)
  10566. {
  10567. /* STR(3). */
  10568. immed_8 = bits (thumb_insn_r->arm_insn, 0, 7);
  10569. regcache_raw_read_unsigned (reg_cache, ARM_SP_REGNUM, &u_regval);
  10570. record_buf_mem[0] = 4;
  10571. record_buf_mem[1] = u_regval + (immed_8 * 4);
  10572. thumb_insn_r->mem_rec_count = 1;
  10573. }
  10574. else if (0 == opcode)
  10575. {
  10576. /* STRH(1). */
  10577. immed_5 = bits (thumb_insn_r->arm_insn, 6, 10);
  10578. reg_src1 = bits (thumb_insn_r->arm_insn, 3, 5);
  10579. regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval);
  10580. record_buf_mem[0] = 2;
  10581. record_buf_mem[1] = u_regval + (immed_5 * 2);
  10582. thumb_insn_r->mem_rec_count = 1;
  10583. }
  10584. REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
  10585. MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
  10586. record_buf_mem);
  10587. return 0;
  10588. }
  10589. /* Handling opcode 101 insns. */
  10590. static int
  10591. thumb_record_misc (insn_decode_record *thumb_insn_r)
  10592. {
  10593. struct regcache *reg_cache = thumb_insn_r->regcache;
  10594. uint32_t opcode = 0;
  10595. uint32_t register_bits = 0, register_count = 0;
  10596. uint32_t index = 0, start_address = 0;
  10597. uint32_t record_buf[24], record_buf_mem[48];
  10598. uint32_t reg_src1;
  10599. ULONGEST u_regval = 0;
  10600. opcode = bits (thumb_insn_r->arm_insn, 11, 12);
  10601. if (opcode == 0 || opcode == 1)
  10602. {
  10603. /* ADR and ADD (SP plus immediate) */
  10604. reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
  10605. record_buf[0] = reg_src1;
  10606. thumb_insn_r->reg_rec_count = 1;
  10607. }
  10608. else
  10609. {
  10610. /* Miscellaneous 16-bit instructions */
  10611. uint32_t opcode2 = bits (thumb_insn_r->arm_insn, 8, 11);
  10612. switch (opcode2)
  10613. {
  10614. case 6:
  10615. /* SETEND and CPS */
  10616. break;
  10617. case 0:
  10618. /* ADD/SUB (SP plus immediate) */
  10619. reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
  10620. record_buf[0] = ARM_SP_REGNUM;
  10621. thumb_insn_r->reg_rec_count = 1;
  10622. break;
  10623. case 1: /* fall through */
  10624. case 3: /* fall through */
  10625. case 9: /* fall through */
  10626. case 11:
  10627. /* CBNZ, CBZ */
  10628. break;
  10629. case 2:
  10630. /* SXTH, SXTB, UXTH, UXTB */
  10631. record_buf[0] = bits (thumb_insn_r->arm_insn, 0, 2);
  10632. thumb_insn_r->reg_rec_count = 1;
  10633. break;
  10634. case 4: /* fall through */
  10635. case 5:
  10636. /* PUSH. */
  10637. register_bits = bits (thumb_insn_r->arm_insn, 0, 7);
  10638. regcache_raw_read_unsigned (reg_cache, ARM_SP_REGNUM, &u_regval);
  10639. while (register_bits)
  10640. {
  10641. if (register_bits & 0x00000001)
  10642. register_count++;
  10643. register_bits = register_bits >> 1;
  10644. }
  10645. start_address = u_regval - \
  10646. (4 * (bit (thumb_insn_r->arm_insn, 8) + register_count));
  10647. thumb_insn_r->mem_rec_count = register_count;
  10648. while (register_count)
  10649. {
  10650. record_buf_mem[(register_count * 2) - 1] = start_address;
  10651. record_buf_mem[(register_count * 2) - 2] = 4;
  10652. start_address = start_address + 4;
  10653. register_count--;
  10654. }
  10655. record_buf[0] = ARM_SP_REGNUM;
  10656. thumb_insn_r->reg_rec_count = 1;
  10657. break;
  10658. case 10:
  10659. /* REV, REV16, REVSH */
  10660. record_buf[0] = bits (thumb_insn_r->arm_insn, 0, 2);
  10661. thumb_insn_r->reg_rec_count = 1;
  10662. break;
  10663. case 12: /* fall through */
  10664. case 13:
  10665. /* POP. */
  10666. register_bits = bits (thumb_insn_r->arm_insn, 0, 7);
  10667. while (register_bits)
  10668. {
  10669. if (register_bits & 0x00000001)
  10670. record_buf[index++] = register_count;
  10671. register_bits = register_bits >> 1;
  10672. register_count++;
  10673. }
  10674. record_buf[index++] = ARM_PS_REGNUM;
  10675. record_buf[index++] = ARM_SP_REGNUM;
  10676. thumb_insn_r->reg_rec_count = index;
  10677. break;
  10678. case 0xe:
  10679. /* BKPT insn. */
  10680. /* Handle enhanced software breakpoint insn, BKPT. */
  10681. /* CPSR is changed to be executed in ARM state, disabling normal
  10682. interrupts, entering abort mode. */
  10683. /* According to high vector configuration PC is set. */
  10684. /* User hits breakpoint and type reverse, in that case, we need to go back with
  10685. previous CPSR and Program Counter. */
  10686. record_buf[0] = ARM_PS_REGNUM;
  10687. record_buf[1] = ARM_LR_REGNUM;
  10688. thumb_insn_r->reg_rec_count = 2;
  10689. /* We need to save SPSR value, which is not yet done. */
  10690. gdb_printf (gdb_stderr,
  10691. _("Process record does not support instruction "
  10692. "0x%0x at address %s.\n"),
  10693. thumb_insn_r->arm_insn,
  10694. paddress (thumb_insn_r->gdbarch,
  10695. thumb_insn_r->this_addr));
  10696. return -1;
  10697. case 0xf:
  10698. /* If-Then, and hints */
  10699. break;
  10700. default:
  10701. return -1;
  10702. };
  10703. }
  10704. REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
  10705. MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
  10706. record_buf_mem);
  10707. return 0;
  10708. }
  10709. /* Handling opcode 110 insns. */
  10710. static int
  10711. thumb_record_ldm_stm_swi (insn_decode_record *thumb_insn_r)
  10712. {
  10713. arm_gdbarch_tdep *tdep
  10714. = (arm_gdbarch_tdep *) gdbarch_tdep (thumb_insn_r->gdbarch);
  10715. struct regcache *reg_cache = thumb_insn_r->regcache;
  10716. uint32_t ret = 0; /* function return value: -1:record failure ; 0:success */
  10717. uint32_t reg_src1 = 0;
  10718. uint32_t opcode1 = 0, opcode2 = 0, register_bits = 0, register_count = 0;
  10719. uint32_t index = 0, start_address = 0;
  10720. uint32_t record_buf[24], record_buf_mem[48];
  10721. ULONGEST u_regval = 0;
  10722. opcode1 = bits (thumb_insn_r->arm_insn, 8, 12);
  10723. opcode2 = bits (thumb_insn_r->arm_insn, 11, 12);
  10724. if (1 == opcode2)
  10725. {
  10726. /* LDMIA. */
  10727. register_bits = bits (thumb_insn_r->arm_insn, 0, 7);
  10728. /* Get Rn. */
  10729. reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
  10730. while (register_bits)
  10731. {
  10732. if (register_bits & 0x00000001)
  10733. record_buf[index++] = register_count;
  10734. register_bits = register_bits >> 1;
  10735. register_count++;
  10736. }
  10737. record_buf[index++] = reg_src1;
  10738. thumb_insn_r->reg_rec_count = index;
  10739. }
  10740. else if (0 == opcode2)
  10741. {
  10742. /* It handles both STMIA. */
  10743. register_bits = bits (thumb_insn_r->arm_insn, 0, 7);
  10744. /* Get Rn. */
  10745. reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
  10746. regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval);
  10747. while (register_bits)
  10748. {
  10749. if (register_bits & 0x00000001)
  10750. register_count++;
  10751. register_bits = register_bits >> 1;
  10752. }
  10753. start_address = u_regval;
  10754. thumb_insn_r->mem_rec_count = register_count;
  10755. while (register_count)
  10756. {
  10757. record_buf_mem[(register_count * 2) - 1] = start_address;
  10758. record_buf_mem[(register_count * 2) - 2] = 4;
  10759. start_address = start_address + 4;
  10760. register_count--;
  10761. }
  10762. }
  10763. else if (0x1F == opcode1)
  10764. {
  10765. /* Handle arm syscall insn. */
  10766. if (tdep->arm_syscall_record != NULL)
  10767. {
  10768. regcache_raw_read_unsigned (reg_cache, 7, &u_regval);
  10769. ret = tdep->arm_syscall_record (reg_cache, u_regval);
  10770. }
  10771. else
  10772. {
  10773. gdb_printf (gdb_stderr, _("no syscall record support\n"));
  10774. return -1;
  10775. }
  10776. }
  10777. /* B (1), conditional branch is automatically taken care in process_record,
  10778. as PC is saved there. */
  10779. REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
  10780. MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
  10781. record_buf_mem);
  10782. return ret;
  10783. }
  10784. /* Handling opcode 111 insns. */
  10785. static int
  10786. thumb_record_branch (insn_decode_record *thumb_insn_r)
  10787. {
  10788. uint32_t record_buf[8];
  10789. uint32_t bits_h = 0;
  10790. bits_h = bits (thumb_insn_r->arm_insn, 11, 12);
  10791. if (2 == bits_h || 3 == bits_h)
  10792. {
  10793. /* BL */
  10794. record_buf[0] = ARM_LR_REGNUM;
  10795. thumb_insn_r->reg_rec_count = 1;
  10796. }
  10797. else if (1 == bits_h)
  10798. {
  10799. /* BLX(1). */
  10800. record_buf[0] = ARM_PS_REGNUM;
  10801. record_buf[1] = ARM_LR_REGNUM;
  10802. thumb_insn_r->reg_rec_count = 2;
  10803. }
  10804. /* B(2) is automatically taken care in process_record, as PC is
  10805. saved there. */
  10806. REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
  10807. return 0;
  10808. }
  10809. /* Handler for thumb2 load/store multiple instructions. */
  10810. static int
  10811. thumb2_record_ld_st_multiple (insn_decode_record *thumb2_insn_r)
  10812. {
  10813. struct regcache *reg_cache = thumb2_insn_r->regcache;
  10814. uint32_t reg_rn, op;
  10815. uint32_t register_bits = 0, register_count = 0;
  10816. uint32_t index = 0, start_address = 0;
  10817. uint32_t record_buf[24], record_buf_mem[48];
  10818. ULONGEST u_regval = 0;
  10819. reg_rn = bits (thumb2_insn_r->arm_insn, 16, 19);
  10820. op = bits (thumb2_insn_r->arm_insn, 23, 24);
  10821. if (0 == op || 3 == op)
  10822. {
  10823. if (bit (thumb2_insn_r->arm_insn, INSN_S_L_BIT_NUM))
  10824. {
  10825. /* Handle RFE instruction. */
  10826. record_buf[0] = ARM_PS_REGNUM;
  10827. thumb2_insn_r->reg_rec_count = 1;
  10828. }
  10829. else
  10830. {
  10831. /* Handle SRS instruction after reading banked SP. */
  10832. return arm_record_unsupported_insn (thumb2_insn_r);
  10833. }
  10834. }
  10835. else if (1 == op || 2 == op)
  10836. {
  10837. if (bit (thumb2_insn_r->arm_insn, INSN_S_L_BIT_NUM))
  10838. {
  10839. /* Handle LDM/LDMIA/LDMFD and LDMDB/LDMEA instructions. */
  10840. register_bits = bits (thumb2_insn_r->arm_insn, 0, 15);
  10841. while (register_bits)
  10842. {
  10843. if (register_bits & 0x00000001)
  10844. record_buf[index++] = register_count;
  10845. register_count++;
  10846. register_bits = register_bits >> 1;
  10847. }
  10848. record_buf[index++] = reg_rn;
  10849. record_buf[index++] = ARM_PS_REGNUM;
  10850. thumb2_insn_r->reg_rec_count = index;
  10851. }
  10852. else
  10853. {
  10854. /* Handle STM/STMIA/STMEA and STMDB/STMFD. */
  10855. register_bits = bits (thumb2_insn_r->arm_insn, 0, 15);
  10856. regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval);
  10857. while (register_bits)
  10858. {
  10859. if (register_bits & 0x00000001)
  10860. register_count++;
  10861. register_bits = register_bits >> 1;
  10862. }
  10863. if (1 == op)
  10864. {
  10865. /* Start address calculation for LDMDB/LDMEA. */
  10866. start_address = u_regval;
  10867. }
  10868. else if (2 == op)
  10869. {
  10870. /* Start address calculation for LDMDB/LDMEA. */
  10871. start_address = u_regval - register_count * 4;
  10872. }
  10873. thumb2_insn_r->mem_rec_count = register_count;
  10874. while (register_count)
  10875. {
  10876. record_buf_mem[register_count * 2 - 1] = start_address;
  10877. record_buf_mem[register_count * 2 - 2] = 4;
  10878. start_address = start_address + 4;
  10879. register_count--;
  10880. }
  10881. record_buf[0] = reg_rn;
  10882. record_buf[1] = ARM_PS_REGNUM;
  10883. thumb2_insn_r->reg_rec_count = 2;
  10884. }
  10885. }
  10886. MEM_ALLOC (thumb2_insn_r->arm_mems, thumb2_insn_r->mem_rec_count,
  10887. record_buf_mem);
  10888. REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
  10889. record_buf);
  10890. return ARM_RECORD_SUCCESS;
  10891. }
  10892. /* Handler for thumb2 load/store (dual/exclusive) and table branch
  10893. instructions. */
  10894. static int
  10895. thumb2_record_ld_st_dual_ex_tbb (insn_decode_record *thumb2_insn_r)
  10896. {
  10897. struct regcache *reg_cache = thumb2_insn_r->regcache;
  10898. uint32_t reg_rd, reg_rn, offset_imm;
  10899. uint32_t reg_dest1, reg_dest2;
  10900. uint32_t address, offset_addr;
  10901. uint32_t record_buf[8], record_buf_mem[8];
  10902. uint32_t op1, op2, op3;
  10903. ULONGEST u_regval[2];
  10904. op1 = bits (thumb2_insn_r->arm_insn, 23, 24);
  10905. op2 = bits (thumb2_insn_r->arm_insn, 20, 21);
  10906. op3 = bits (thumb2_insn_r->arm_insn, 4, 7);
  10907. if (bit (thumb2_insn_r->arm_insn, INSN_S_L_BIT_NUM))
  10908. {
  10909. if(!(1 == op1 && 1 == op2 && (0 == op3 || 1 == op3)))
  10910. {
  10911. reg_dest1 = bits (thumb2_insn_r->arm_insn, 12, 15);
  10912. record_buf[0] = reg_dest1;
  10913. record_buf[1] = ARM_PS_REGNUM;
  10914. thumb2_insn_r->reg_rec_count = 2;
  10915. }
  10916. if (3 == op2 || (op1 & 2) || (1 == op1 && 1 == op2 && 7 == op3))
  10917. {
  10918. reg_dest2 = bits (thumb2_insn_r->arm_insn, 8, 11);
  10919. record_buf[2] = reg_dest2;
  10920. thumb2_insn_r->reg_rec_count = 3;
  10921. }
  10922. }
  10923. else
  10924. {
  10925. reg_rn = bits (thumb2_insn_r->arm_insn, 16, 19);
  10926. regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval[0]);
  10927. if (0 == op1 && 0 == op2)
  10928. {
  10929. /* Handle STREX. */
  10930. offset_imm = bits (thumb2_insn_r->arm_insn, 0, 7);
  10931. address = u_regval[0] + (offset_imm * 4);
  10932. record_buf_mem[0] = 4;
  10933. record_buf_mem[1] = address;
  10934. thumb2_insn_r->mem_rec_count = 1;
  10935. reg_rd = bits (thumb2_insn_r->arm_insn, 0, 3);
  10936. record_buf[0] = reg_rd;
  10937. thumb2_insn_r->reg_rec_count = 1;
  10938. }
  10939. else if (1 == op1 && 0 == op2)
  10940. {
  10941. reg_rd = bits (thumb2_insn_r->arm_insn, 0, 3);
  10942. record_buf[0] = reg_rd;
  10943. thumb2_insn_r->reg_rec_count = 1;
  10944. address = u_regval[0];
  10945. record_buf_mem[1] = address;
  10946. if (4 == op3)
  10947. {
  10948. /* Handle STREXB. */
  10949. record_buf_mem[0] = 1;
  10950. thumb2_insn_r->mem_rec_count = 1;
  10951. }
  10952. else if (5 == op3)
  10953. {
  10954. /* Handle STREXH. */
  10955. record_buf_mem[0] = 2 ;
  10956. thumb2_insn_r->mem_rec_count = 1;
  10957. }
  10958. else if (7 == op3)
  10959. {
  10960. /* Handle STREXD. */
  10961. address = u_regval[0];
  10962. record_buf_mem[0] = 4;
  10963. record_buf_mem[2] = 4;
  10964. record_buf_mem[3] = address + 4;
  10965. thumb2_insn_r->mem_rec_count = 2;
  10966. }
  10967. }
  10968. else
  10969. {
  10970. offset_imm = bits (thumb2_insn_r->arm_insn, 0, 7);
  10971. if (bit (thumb2_insn_r->arm_insn, 24))
  10972. {
  10973. if (bit (thumb2_insn_r->arm_insn, 23))
  10974. offset_addr = u_regval[0] + (offset_imm * 4);
  10975. else
  10976. offset_addr = u_regval[0] - (offset_imm * 4);
  10977. address = offset_addr;
  10978. }
  10979. else
  10980. address = u_regval[0];
  10981. record_buf_mem[0] = 4;
  10982. record_buf_mem[1] = address;
  10983. record_buf_mem[2] = 4;
  10984. record_buf_mem[3] = address + 4;
  10985. thumb2_insn_r->mem_rec_count = 2;
  10986. record_buf[0] = reg_rn;
  10987. thumb2_insn_r->reg_rec_count = 1;
  10988. }
  10989. }
  10990. REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
  10991. record_buf);
  10992. MEM_ALLOC (thumb2_insn_r->arm_mems, thumb2_insn_r->mem_rec_count,
  10993. record_buf_mem);
  10994. return ARM_RECORD_SUCCESS;
  10995. }
  10996. /* Handler for thumb2 data processing (shift register and modified immediate)
  10997. instructions. */
  10998. static int
  10999. thumb2_record_data_proc_sreg_mimm (insn_decode_record *thumb2_insn_r)
  11000. {
  11001. uint32_t reg_rd, op;
  11002. uint32_t record_buf[8];
  11003. op = bits (thumb2_insn_r->arm_insn, 21, 24);
  11004. reg_rd = bits (thumb2_insn_r->arm_insn, 8, 11);
  11005. if ((0 == op || 4 == op || 8 == op || 13 == op) && 15 == reg_rd)
  11006. {
  11007. record_buf[0] = ARM_PS_REGNUM;
  11008. thumb2_insn_r->reg_rec_count = 1;
  11009. }
  11010. else
  11011. {
  11012. record_buf[0] = reg_rd;
  11013. record_buf[1] = ARM_PS_REGNUM;
  11014. thumb2_insn_r->reg_rec_count = 2;
  11015. }
  11016. REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
  11017. record_buf);
  11018. return ARM_RECORD_SUCCESS;
  11019. }
  11020. /* Generic handler for thumb2 instructions which effect destination and PS
  11021. registers. */
  11022. static int
  11023. thumb2_record_ps_dest_generic (insn_decode_record *thumb2_insn_r)
  11024. {
  11025. uint32_t reg_rd;
  11026. uint32_t record_buf[8];
  11027. reg_rd = bits (thumb2_insn_r->arm_insn, 8, 11);
  11028. record_buf[0] = reg_rd;
  11029. record_buf[1] = ARM_PS_REGNUM;
  11030. thumb2_insn_r->reg_rec_count = 2;
  11031. REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
  11032. record_buf);
  11033. return ARM_RECORD_SUCCESS;
  11034. }
  11035. /* Handler for thumb2 branch and miscellaneous control instructions. */
  11036. static int
  11037. thumb2_record_branch_misc_cntrl (insn_decode_record *thumb2_insn_r)
  11038. {
  11039. uint32_t op, op1, op2;
  11040. uint32_t record_buf[8];
  11041. op = bits (thumb2_insn_r->arm_insn, 20, 26);
  11042. op1 = bits (thumb2_insn_r->arm_insn, 12, 14);
  11043. op2 = bits (thumb2_insn_r->arm_insn, 8, 11);
  11044. /* Handle MSR insn. */
  11045. if (!(op1 & 0x2) && 0x38 == op)
  11046. {
  11047. if (!(op2 & 0x3))
  11048. {
  11049. /* CPSR is going to be changed. */
  11050. record_buf[0] = ARM_PS_REGNUM;
  11051. thumb2_insn_r->reg_rec_count = 1;
  11052. }
  11053. else
  11054. {
  11055. arm_record_unsupported_insn(thumb2_insn_r);
  11056. return -1;
  11057. }
  11058. }
  11059. else if (4 == (op1 & 0x5) || 5 == (op1 & 0x5))
  11060. {
  11061. /* BLX. */
  11062. record_buf[0] = ARM_PS_REGNUM;
  11063. record_buf[1] = ARM_LR_REGNUM;
  11064. thumb2_insn_r->reg_rec_count = 2;
  11065. }
  11066. REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
  11067. record_buf);
  11068. return ARM_RECORD_SUCCESS;
  11069. }
  11070. /* Handler for thumb2 store single data item instructions. */
  11071. static int
  11072. thumb2_record_str_single_data (insn_decode_record *thumb2_insn_r)
  11073. {
  11074. struct regcache *reg_cache = thumb2_insn_r->regcache;
  11075. uint32_t reg_rn, reg_rm, offset_imm, shift_imm;
  11076. uint32_t address, offset_addr;
  11077. uint32_t record_buf[8], record_buf_mem[8];
  11078. uint32_t op1, op2;
  11079. ULONGEST u_regval[2];
  11080. op1 = bits (thumb2_insn_r->arm_insn, 21, 23);
  11081. op2 = bits (thumb2_insn_r->arm_insn, 6, 11);
  11082. reg_rn = bits (thumb2_insn_r->arm_insn, 16, 19);
  11083. regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval[0]);
  11084. if (bit (thumb2_insn_r->arm_insn, 23))
  11085. {
  11086. /* T2 encoding. */
  11087. offset_imm = bits (thumb2_insn_r->arm_insn, 0, 11);
  11088. offset_addr = u_regval[0] + offset_imm;
  11089. address = offset_addr;
  11090. }
  11091. else
  11092. {
  11093. /* T3 encoding. */
  11094. if ((0 == op1 || 1 == op1 || 2 == op1) && !(op2 & 0x20))
  11095. {
  11096. /* Handle STRB (register). */
  11097. reg_rm = bits (thumb2_insn_r->arm_insn, 0, 3);
  11098. regcache_raw_read_unsigned (reg_cache, reg_rm, &u_regval[1]);
  11099. shift_imm = bits (thumb2_insn_r->arm_insn, 4, 5);
  11100. offset_addr = u_regval[1] << shift_imm;
  11101. address = u_regval[0] + offset_addr;
  11102. }
  11103. else
  11104. {
  11105. offset_imm = bits (thumb2_insn_r->arm_insn, 0, 7);
  11106. if (bit (thumb2_insn_r->arm_insn, 10))
  11107. {
  11108. if (bit (thumb2_insn_r->arm_insn, 9))
  11109. offset_addr = u_regval[0] + offset_imm;
  11110. else
  11111. offset_addr = u_regval[0] - offset_imm;
  11112. address = offset_addr;
  11113. }
  11114. else
  11115. address = u_regval[0];
  11116. }
  11117. }
  11118. switch (op1)
  11119. {
  11120. /* Store byte instructions. */
  11121. case 4:
  11122. case 0:
  11123. record_buf_mem[0] = 1;
  11124. break;
  11125. /* Store half word instructions. */
  11126. case 1:
  11127. case 5:
  11128. record_buf_mem[0] = 2;
  11129. break;
  11130. /* Store word instructions. */
  11131. case 2:
  11132. case 6:
  11133. record_buf_mem[0] = 4;
  11134. break;
  11135. default:
  11136. gdb_assert_not_reached ("no decoding pattern found");
  11137. break;
  11138. }
  11139. record_buf_mem[1] = address;
  11140. thumb2_insn_r->mem_rec_count = 1;
  11141. record_buf[0] = reg_rn;
  11142. thumb2_insn_r->reg_rec_count = 1;
  11143. REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
  11144. record_buf);
  11145. MEM_ALLOC (thumb2_insn_r->arm_mems, thumb2_insn_r->mem_rec_count,
  11146. record_buf_mem);
  11147. return ARM_RECORD_SUCCESS;
  11148. }
  11149. /* Handler for thumb2 load memory hints instructions. */
  11150. static int
  11151. thumb2_record_ld_mem_hints (insn_decode_record *thumb2_insn_r)
  11152. {
  11153. uint32_t record_buf[8];
  11154. uint32_t reg_rt, reg_rn;
  11155. reg_rt = bits (thumb2_insn_r->arm_insn, 12, 15);
  11156. reg_rn = bits (thumb2_insn_r->arm_insn, 16, 19);
  11157. if (ARM_PC_REGNUM != reg_rt)
  11158. {
  11159. record_buf[0] = reg_rt;
  11160. record_buf[1] = reg_rn;
  11161. record_buf[2] = ARM_PS_REGNUM;
  11162. thumb2_insn_r->reg_rec_count = 3;
  11163. REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
  11164. record_buf);
  11165. return ARM_RECORD_SUCCESS;
  11166. }
  11167. return ARM_RECORD_FAILURE;
  11168. }
  11169. /* Handler for thumb2 load word instructions. */
  11170. static int
  11171. thumb2_record_ld_word (insn_decode_record *thumb2_insn_r)
  11172. {
  11173. uint32_t record_buf[8];
  11174. record_buf[0] = bits (thumb2_insn_r->arm_insn, 12, 15);
  11175. record_buf[1] = ARM_PS_REGNUM;
  11176. thumb2_insn_r->reg_rec_count = 2;
  11177. REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
  11178. record_buf);
  11179. return ARM_RECORD_SUCCESS;
  11180. }
  11181. /* Handler for thumb2 long multiply, long multiply accumulate, and
  11182. divide instructions. */
  11183. static int
  11184. thumb2_record_lmul_lmla_div (insn_decode_record *thumb2_insn_r)
  11185. {
  11186. uint32_t opcode1 = 0, opcode2 = 0;
  11187. uint32_t record_buf[8];
  11188. opcode1 = bits (thumb2_insn_r->arm_insn, 20, 22);
  11189. opcode2 = bits (thumb2_insn_r->arm_insn, 4, 7);
  11190. if (0 == opcode1 || 2 == opcode1 || (opcode1 >= 4 && opcode1 <= 6))
  11191. {
  11192. /* Handle SMULL, UMULL, SMULAL. */
  11193. /* Handle SMLAL(S), SMULL(S), UMLAL(S), UMULL(S). */
  11194. record_buf[0] = bits (thumb2_insn_r->arm_insn, 16, 19);
  11195. record_buf[1] = bits (thumb2_insn_r->arm_insn, 12, 15);
  11196. record_buf[2] = ARM_PS_REGNUM;
  11197. thumb2_insn_r->reg_rec_count = 3;
  11198. }
  11199. else if (1 == opcode1 || 3 == opcode2)
  11200. {
  11201. /* Handle SDIV and UDIV. */
  11202. record_buf[0] = bits (thumb2_insn_r->arm_insn, 16, 19);
  11203. record_buf[1] = bits (thumb2_insn_r->arm_insn, 12, 15);
  11204. record_buf[2] = ARM_PS_REGNUM;
  11205. thumb2_insn_r->reg_rec_count = 3;
  11206. }
  11207. else
  11208. return ARM_RECORD_FAILURE;
  11209. REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
  11210. record_buf);
  11211. return ARM_RECORD_SUCCESS;
  11212. }
  11213. /* Record handler for thumb32 coprocessor instructions. */
  11214. static int
  11215. thumb2_record_coproc_insn (insn_decode_record *thumb2_insn_r)
  11216. {
  11217. if (bit (thumb2_insn_r->arm_insn, 25))
  11218. return arm_record_coproc_data_proc (thumb2_insn_r);
  11219. else
  11220. return arm_record_asimd_vfp_coproc (thumb2_insn_r);
  11221. }
  11222. /* Record handler for advance SIMD structure load/store instructions. */
  11223. static int
  11224. thumb2_record_asimd_struct_ld_st (insn_decode_record *thumb2_insn_r)
  11225. {
  11226. struct regcache *reg_cache = thumb2_insn_r->regcache;
  11227. uint32_t l_bit, a_bit, b_bits;
  11228. uint32_t record_buf[128], record_buf_mem[128];
  11229. uint32_t reg_rn, reg_vd, address, f_elem;
  11230. uint32_t index_r = 0, index_e = 0, bf_regs = 0, index_m = 0, loop_t = 0;
  11231. uint8_t f_ebytes;
  11232. l_bit = bit (thumb2_insn_r->arm_insn, 21);
  11233. a_bit = bit (thumb2_insn_r->arm_insn, 23);
  11234. b_bits = bits (thumb2_insn_r->arm_insn, 8, 11);
  11235. reg_rn = bits (thumb2_insn_r->arm_insn, 16, 19);
  11236. reg_vd = bits (thumb2_insn_r->arm_insn, 12, 15);
  11237. reg_vd = (bit (thumb2_insn_r->arm_insn, 22) << 4) | reg_vd;
  11238. f_ebytes = (1 << bits (thumb2_insn_r->arm_insn, 6, 7));
  11239. f_elem = 8 / f_ebytes;
  11240. if (!l_bit)
  11241. {
  11242. ULONGEST u_regval = 0;
  11243. regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval);
  11244. address = u_regval;
  11245. if (!a_bit)
  11246. {
  11247. /* Handle VST1. */
  11248. if (b_bits == 0x02 || b_bits == 0x0a || (b_bits & 0x0e) == 0x06)
  11249. {
  11250. if (b_bits == 0x07)
  11251. bf_regs = 1;
  11252. else if (b_bits == 0x0a)
  11253. bf_regs = 2;
  11254. else if (b_bits == 0x06)
  11255. bf_regs = 3;
  11256. else if (b_bits == 0x02)
  11257. bf_regs = 4;
  11258. else
  11259. bf_regs = 0;
  11260. for (index_r = 0; index_r < bf_regs; index_r++)
  11261. {
  11262. for (index_e = 0; index_e < f_elem; index_e++)
  11263. {
  11264. record_buf_mem[index_m++] = f_ebytes;
  11265. record_buf_mem[index_m++] = address;
  11266. address = address + f_ebytes;
  11267. thumb2_insn_r->mem_rec_count += 1;
  11268. }
  11269. }
  11270. }
  11271. /* Handle VST2. */
  11272. else if (b_bits == 0x03 || (b_bits & 0x0e) == 0x08)
  11273. {
  11274. if (b_bits == 0x09 || b_bits == 0x08)
  11275. bf_regs = 1;
  11276. else if (b_bits == 0x03)
  11277. bf_regs = 2;
  11278. else
  11279. bf_regs = 0;
  11280. for (index_r = 0; index_r < bf_regs; index_r++)
  11281. for (index_e = 0; index_e < f_elem; index_e++)
  11282. {
  11283. for (loop_t = 0; loop_t < 2; loop_t++)
  11284. {
  11285. record_buf_mem[index_m++] = f_ebytes;
  11286. record_buf_mem[index_m++] = address + (loop_t * f_ebytes);
  11287. thumb2_insn_r->mem_rec_count += 1;
  11288. }
  11289. address = address + (2 * f_ebytes);
  11290. }
  11291. }
  11292. /* Handle VST3. */
  11293. else if ((b_bits & 0x0e) == 0x04)
  11294. {
  11295. for (index_e = 0; index_e < f_elem; index_e++)
  11296. {
  11297. for (loop_t = 0; loop_t < 3; loop_t++)
  11298. {
  11299. record_buf_mem[index_m++] = f_ebytes;
  11300. record_buf_mem[index_m++] = address + (loop_t * f_ebytes);
  11301. thumb2_insn_r->mem_rec_count += 1;
  11302. }
  11303. address = address + (3 * f_ebytes);
  11304. }
  11305. }
  11306. /* Handle VST4. */
  11307. else if (!(b_bits & 0x0e))
  11308. {
  11309. for (index_e = 0; index_e < f_elem; index_e++)
  11310. {
  11311. for (loop_t = 0; loop_t < 4; loop_t++)
  11312. {
  11313. record_buf_mem[index_m++] = f_ebytes;
  11314. record_buf_mem[index_m++] = address + (loop_t * f_ebytes);
  11315. thumb2_insn_r->mem_rec_count += 1;
  11316. }
  11317. address = address + (4 * f_ebytes);
  11318. }
  11319. }
  11320. }
  11321. else
  11322. {
  11323. uint8_t bft_size = bits (thumb2_insn_r->arm_insn, 10, 11);
  11324. if (bft_size == 0x00)
  11325. f_ebytes = 1;
  11326. else if (bft_size == 0x01)
  11327. f_ebytes = 2;
  11328. else if (bft_size == 0x02)
  11329. f_ebytes = 4;
  11330. else
  11331. f_ebytes = 0;
  11332. /* Handle VST1. */
  11333. if (!(b_bits & 0x0b) || b_bits == 0x08)
  11334. thumb2_insn_r->mem_rec_count = 1;
  11335. /* Handle VST2. */
  11336. else if ((b_bits & 0x0b) == 0x01 || b_bits == 0x09)
  11337. thumb2_insn_r->mem_rec_count = 2;
  11338. /* Handle VST3. */
  11339. else if ((b_bits & 0x0b) == 0x02 || b_bits == 0x0a)
  11340. thumb2_insn_r->mem_rec_count = 3;
  11341. /* Handle VST4. */
  11342. else if ((b_bits & 0x0b) == 0x03 || b_bits == 0x0b)
  11343. thumb2_insn_r->mem_rec_count = 4;
  11344. for (index_m = 0; index_m < thumb2_insn_r->mem_rec_count; index_m++)
  11345. {
  11346. record_buf_mem[index_m] = f_ebytes;
  11347. record_buf_mem[index_m] = address + (index_m * f_ebytes);
  11348. }
  11349. }
  11350. }
  11351. else
  11352. {
  11353. if (!a_bit)
  11354. {
  11355. /* Handle VLD1. */
  11356. if (b_bits == 0x02 || b_bits == 0x0a || (b_bits & 0x0e) == 0x06)
  11357. thumb2_insn_r->reg_rec_count = 1;
  11358. /* Handle VLD2. */
  11359. else if (b_bits == 0x03 || (b_bits & 0x0e) == 0x08)
  11360. thumb2_insn_r->reg_rec_count = 2;
  11361. /* Handle VLD3. */
  11362. else if ((b_bits & 0x0e) == 0x04)
  11363. thumb2_insn_r->reg_rec_count = 3;
  11364. /* Handle VLD4. */
  11365. else if (!(b_bits & 0x0e))
  11366. thumb2_insn_r->reg_rec_count = 4;
  11367. }
  11368. else
  11369. {
  11370. /* Handle VLD1. */
  11371. if (!(b_bits & 0x0b) || b_bits == 0x08 || b_bits == 0x0c)
  11372. thumb2_insn_r->reg_rec_count = 1;
  11373. /* Handle VLD2. */
  11374. else if ((b_bits & 0x0b) == 0x01 || b_bits == 0x09 || b_bits == 0x0d)
  11375. thumb2_insn_r->reg_rec_count = 2;
  11376. /* Handle VLD3. */
  11377. else if ((b_bits & 0x0b) == 0x02 || b_bits == 0x0a || b_bits == 0x0e)
  11378. thumb2_insn_r->reg_rec_count = 3;
  11379. /* Handle VLD4. */
  11380. else if ((b_bits & 0x0b) == 0x03 || b_bits == 0x0b || b_bits == 0x0f)
  11381. thumb2_insn_r->reg_rec_count = 4;
  11382. for (index_r = 0; index_r < thumb2_insn_r->reg_rec_count; index_r++)
  11383. record_buf[index_r] = reg_vd + ARM_D0_REGNUM + index_r;
  11384. }
  11385. }
  11386. if (bits (thumb2_insn_r->arm_insn, 0, 3) != 15)
  11387. {
  11388. record_buf[index_r] = reg_rn;
  11389. thumb2_insn_r->reg_rec_count += 1;
  11390. }
  11391. REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
  11392. record_buf);
  11393. MEM_ALLOC (thumb2_insn_r->arm_mems, thumb2_insn_r->mem_rec_count,
  11394. record_buf_mem);
  11395. return 0;
  11396. }
  11397. /* Decodes thumb2 instruction type and invokes its record handler. */
  11398. static unsigned int
  11399. thumb2_record_decode_insn_handler (insn_decode_record *thumb2_insn_r)
  11400. {
  11401. uint32_t op, op1, op2;
  11402. op = bit (thumb2_insn_r->arm_insn, 15);
  11403. op1 = bits (thumb2_insn_r->arm_insn, 27, 28);
  11404. op2 = bits (thumb2_insn_r->arm_insn, 20, 26);
  11405. if (op1 == 0x01)
  11406. {
  11407. if (!(op2 & 0x64 ))
  11408. {
  11409. /* Load/store multiple instruction. */
  11410. return thumb2_record_ld_st_multiple (thumb2_insn_r);
  11411. }
  11412. else if ((op2 & 0x64) == 0x4)
  11413. {
  11414. /* Load/store (dual/exclusive) and table branch instruction. */
  11415. return thumb2_record_ld_st_dual_ex_tbb (thumb2_insn_r);
  11416. }
  11417. else if ((op2 & 0x60) == 0x20)
  11418. {
  11419. /* Data-processing (shifted register). */
  11420. return thumb2_record_data_proc_sreg_mimm (thumb2_insn_r);
  11421. }
  11422. else if (op2 & 0x40)
  11423. {
  11424. /* Co-processor instructions. */
  11425. return thumb2_record_coproc_insn (thumb2_insn_r);
  11426. }
  11427. }
  11428. else if (op1 == 0x02)
  11429. {
  11430. if (op)
  11431. {
  11432. /* Branches and miscellaneous control instructions. */
  11433. return thumb2_record_branch_misc_cntrl (thumb2_insn_r);
  11434. }
  11435. else if (op2 & 0x20)
  11436. {
  11437. /* Data-processing (plain binary immediate) instruction. */
  11438. return thumb2_record_ps_dest_generic (thumb2_insn_r);
  11439. }
  11440. else
  11441. {
  11442. /* Data-processing (modified immediate). */
  11443. return thumb2_record_data_proc_sreg_mimm (thumb2_insn_r);
  11444. }
  11445. }
  11446. else if (op1 == 0x03)
  11447. {
  11448. if (!(op2 & 0x71 ))
  11449. {
  11450. /* Store single data item. */
  11451. return thumb2_record_str_single_data (thumb2_insn_r);
  11452. }
  11453. else if (!((op2 & 0x71) ^ 0x10))
  11454. {
  11455. /* Advanced SIMD or structure load/store instructions. */
  11456. return thumb2_record_asimd_struct_ld_st (thumb2_insn_r);
  11457. }
  11458. else if (!((op2 & 0x67) ^ 0x01))
  11459. {
  11460. /* Load byte, memory hints instruction. */
  11461. return thumb2_record_ld_mem_hints (thumb2_insn_r);
  11462. }
  11463. else if (!((op2 & 0x67) ^ 0x03))
  11464. {
  11465. /* Load halfword, memory hints instruction. */
  11466. return thumb2_record_ld_mem_hints (thumb2_insn_r);
  11467. }
  11468. else if (!((op2 & 0x67) ^ 0x05))
  11469. {
  11470. /* Load word instruction. */
  11471. return thumb2_record_ld_word (thumb2_insn_r);
  11472. }
  11473. else if (!((op2 & 0x70) ^ 0x20))
  11474. {
  11475. /* Data-processing (register) instruction. */
  11476. return thumb2_record_ps_dest_generic (thumb2_insn_r);
  11477. }
  11478. else if (!((op2 & 0x78) ^ 0x30))
  11479. {
  11480. /* Multiply, multiply accumulate, abs diff instruction. */
  11481. return thumb2_record_ps_dest_generic (thumb2_insn_r);
  11482. }
  11483. else if (!((op2 & 0x78) ^ 0x38))
  11484. {
  11485. /* Long multiply, long multiply accumulate, and divide. */
  11486. return thumb2_record_lmul_lmla_div (thumb2_insn_r);
  11487. }
  11488. else if (op2 & 0x40)
  11489. {
  11490. /* Co-processor instructions. */
  11491. return thumb2_record_coproc_insn (thumb2_insn_r);
  11492. }
  11493. }
  11494. return -1;
  11495. }
  11496. namespace {
  11497. /* Abstract memory reader. */
  11498. class abstract_memory_reader
  11499. {
  11500. public:
  11501. /* Read LEN bytes of target memory at address MEMADDR, placing the
  11502. results in GDB's memory at BUF. Return true on success. */
  11503. virtual bool read (CORE_ADDR memaddr, gdb_byte *buf, const size_t len) = 0;
  11504. };
  11505. /* Instruction reader from real target. */
  11506. class instruction_reader : public abstract_memory_reader
  11507. {
  11508. public:
  11509. bool read (CORE_ADDR memaddr, gdb_byte *buf, const size_t len) override
  11510. {
  11511. if (target_read_memory (memaddr, buf, len))
  11512. return false;
  11513. else
  11514. return true;
  11515. }
  11516. };
  11517. } // namespace
  11518. /* Extracts arm/thumb/thumb2 insn depending on the size, and returns 0 on success
  11519. and positive val on failure. */
  11520. static int
  11521. extract_arm_insn (abstract_memory_reader& reader,
  11522. insn_decode_record *insn_record, uint32_t insn_size)
  11523. {
  11524. gdb_byte buf[insn_size];
  11525. memset (&buf[0], 0, insn_size);
  11526. if (!reader.read (insn_record->this_addr, buf, insn_size))
  11527. return 1;
  11528. insn_record->arm_insn = (uint32_t) extract_unsigned_integer (&buf[0],
  11529. insn_size,
  11530. gdbarch_byte_order_for_code (insn_record->gdbarch));
  11531. return 0;
  11532. }
  11533. typedef int (*sti_arm_hdl_fp_t) (insn_decode_record*);
  11534. /* Decode arm/thumb insn depending on condition cods and opcodes; and
  11535. dispatch it. */
  11536. static int
  11537. decode_insn (abstract_memory_reader &reader, insn_decode_record *arm_record,
  11538. record_type_t record_type, uint32_t insn_size)
  11539. {
  11540. /* (Starting from numerical 0); bits 25, 26, 27 decodes type of arm
  11541. instruction. */
  11542. static const sti_arm_hdl_fp_t arm_handle_insn[8] =
  11543. {
  11544. arm_record_data_proc_misc_ld_str, /* 000. */
  11545. arm_record_data_proc_imm, /* 001. */
  11546. arm_record_ld_st_imm_offset, /* 010. */
  11547. arm_record_ld_st_reg_offset, /* 011. */
  11548. arm_record_ld_st_multiple, /* 100. */
  11549. arm_record_b_bl, /* 101. */
  11550. arm_record_asimd_vfp_coproc, /* 110. */
  11551. arm_record_coproc_data_proc /* 111. */
  11552. };
  11553. /* (Starting from numerical 0); bits 13,14,15 decodes type of thumb
  11554. instruction. */
  11555. static const sti_arm_hdl_fp_t thumb_handle_insn[8] =
  11556. { \
  11557. thumb_record_shift_add_sub, /* 000. */
  11558. thumb_record_add_sub_cmp_mov, /* 001. */
  11559. thumb_record_ld_st_reg_offset, /* 010. */
  11560. thumb_record_ld_st_imm_offset, /* 011. */
  11561. thumb_record_ld_st_stack, /* 100. */
  11562. thumb_record_misc, /* 101. */
  11563. thumb_record_ldm_stm_swi, /* 110. */
  11564. thumb_record_branch /* 111. */
  11565. };
  11566. uint32_t ret = 0; /* return value: negative:failure 0:success. */
  11567. uint32_t insn_id = 0;
  11568. if (extract_arm_insn (reader, arm_record, insn_size))
  11569. {
  11570. if (record_debug)
  11571. {
  11572. gdb_printf (gdb_stdlog,
  11573. _("Process record: error reading memory at "
  11574. "addr %s len = %d.\n"),
  11575. paddress (arm_record->gdbarch,
  11576. arm_record->this_addr), insn_size);
  11577. }
  11578. return -1;
  11579. }
  11580. else if (ARM_RECORD == record_type)
  11581. {
  11582. arm_record->cond = bits (arm_record->arm_insn, 28, 31);
  11583. insn_id = bits (arm_record->arm_insn, 25, 27);
  11584. if (arm_record->cond == 0xf)
  11585. ret = arm_record_extension_space (arm_record);
  11586. else
  11587. {
  11588. /* If this insn has fallen into extension space
  11589. then we need not decode it anymore. */
  11590. ret = arm_handle_insn[insn_id] (arm_record);
  11591. }
  11592. if (ret != ARM_RECORD_SUCCESS)
  11593. {
  11594. arm_record_unsupported_insn (arm_record);
  11595. ret = -1;
  11596. }
  11597. }
  11598. else if (THUMB_RECORD == record_type)
  11599. {
  11600. /* As thumb does not have condition codes, we set negative. */
  11601. arm_record->cond = -1;
  11602. insn_id = bits (arm_record->arm_insn, 13, 15);
  11603. ret = thumb_handle_insn[insn_id] (arm_record);
  11604. if (ret != ARM_RECORD_SUCCESS)
  11605. {
  11606. arm_record_unsupported_insn (arm_record);
  11607. ret = -1;
  11608. }
  11609. }
  11610. else if (THUMB2_RECORD == record_type)
  11611. {
  11612. /* As thumb does not have condition codes, we set negative. */
  11613. arm_record->cond = -1;
  11614. /* Swap first half of 32bit thumb instruction with second half. */
  11615. arm_record->arm_insn
  11616. = (arm_record->arm_insn >> 16) | (arm_record->arm_insn << 16);
  11617. ret = thumb2_record_decode_insn_handler (arm_record);
  11618. if (ret != ARM_RECORD_SUCCESS)
  11619. {
  11620. arm_record_unsupported_insn (arm_record);
  11621. ret = -1;
  11622. }
  11623. }
  11624. else
  11625. {
  11626. /* Throw assertion. */
  11627. gdb_assert_not_reached ("not a valid instruction, could not decode");
  11628. }
  11629. return ret;
  11630. }
  11631. #if GDB_SELF_TEST
  11632. namespace selftests {
  11633. /* Provide both 16-bit and 32-bit thumb instructions. */
  11634. class instruction_reader_thumb : public abstract_memory_reader
  11635. {
  11636. public:
  11637. template<size_t SIZE>
  11638. instruction_reader_thumb (enum bfd_endian endian,
  11639. const uint16_t (&insns)[SIZE])
  11640. : m_endian (endian), m_insns (insns), m_insns_size (SIZE)
  11641. {}
  11642. bool read (CORE_ADDR memaddr, gdb_byte *buf, const size_t len) override
  11643. {
  11644. SELF_CHECK (len == 4 || len == 2);
  11645. SELF_CHECK (memaddr % 2 == 0);
  11646. SELF_CHECK ((memaddr / 2) < m_insns_size);
  11647. store_unsigned_integer (buf, 2, m_endian, m_insns[memaddr / 2]);
  11648. if (len == 4)
  11649. {
  11650. store_unsigned_integer (&buf[2], 2, m_endian,
  11651. m_insns[memaddr / 2 + 1]);
  11652. }
  11653. return true;
  11654. }
  11655. private:
  11656. enum bfd_endian m_endian;
  11657. const uint16_t *m_insns;
  11658. size_t m_insns_size;
  11659. };
  11660. static void
  11661. arm_record_test (void)
  11662. {
  11663. struct gdbarch_info info;
  11664. info.bfd_arch_info = bfd_scan_arch ("arm");
  11665. struct gdbarch *gdbarch = gdbarch_find_by_info (info);
  11666. SELF_CHECK (gdbarch != NULL);
  11667. /* 16-bit Thumb instructions. */
  11668. {
  11669. insn_decode_record arm_record;
  11670. memset (&arm_record, 0, sizeof (insn_decode_record));
  11671. arm_record.gdbarch = gdbarch;
  11672. static const uint16_t insns[] = {
  11673. /* db b2 uxtb r3, r3 */
  11674. 0xb2db,
  11675. /* cd 58 ldr r5, [r1, r3] */
  11676. 0x58cd,
  11677. };
  11678. enum bfd_endian endian = gdbarch_byte_order_for_code (arm_record.gdbarch);
  11679. instruction_reader_thumb reader (endian, insns);
  11680. int ret = decode_insn (reader, &arm_record, THUMB_RECORD,
  11681. THUMB_INSN_SIZE_BYTES);
  11682. SELF_CHECK (ret == 0);
  11683. SELF_CHECK (arm_record.mem_rec_count == 0);
  11684. SELF_CHECK (arm_record.reg_rec_count == 1);
  11685. SELF_CHECK (arm_record.arm_regs[0] == 3);
  11686. arm_record.this_addr += 2;
  11687. ret = decode_insn (reader, &arm_record, THUMB_RECORD,
  11688. THUMB_INSN_SIZE_BYTES);
  11689. SELF_CHECK (ret == 0);
  11690. SELF_CHECK (arm_record.mem_rec_count == 0);
  11691. SELF_CHECK (arm_record.reg_rec_count == 1);
  11692. SELF_CHECK (arm_record.arm_regs[0] == 5);
  11693. }
  11694. /* 32-bit Thumb-2 instructions. */
  11695. {
  11696. insn_decode_record arm_record;
  11697. memset (&arm_record, 0, sizeof (insn_decode_record));
  11698. arm_record.gdbarch = gdbarch;
  11699. static const uint16_t insns[] = {
  11700. /* 1d ee 70 7f mrc 15, 0, r7, cr13, cr0, {3} */
  11701. 0xee1d, 0x7f70,
  11702. };
  11703. enum bfd_endian endian = gdbarch_byte_order_for_code (arm_record.gdbarch);
  11704. instruction_reader_thumb reader (endian, insns);
  11705. int ret = decode_insn (reader, &arm_record, THUMB2_RECORD,
  11706. THUMB2_INSN_SIZE_BYTES);
  11707. SELF_CHECK (ret == 0);
  11708. SELF_CHECK (arm_record.mem_rec_count == 0);
  11709. SELF_CHECK (arm_record.reg_rec_count == 1);
  11710. SELF_CHECK (arm_record.arm_regs[0] == 7);
  11711. }
  11712. }
  11713. /* Instruction reader from manually cooked instruction sequences. */
  11714. class test_arm_instruction_reader : public arm_instruction_reader
  11715. {
  11716. public:
  11717. explicit test_arm_instruction_reader (gdb::array_view<const uint32_t> insns)
  11718. : m_insns (insns)
  11719. {}
  11720. uint32_t read (CORE_ADDR memaddr, enum bfd_endian byte_order) const override
  11721. {
  11722. SELF_CHECK (memaddr % 4 == 0);
  11723. SELF_CHECK (memaddr / 4 < m_insns.size ());
  11724. return m_insns[memaddr / 4];
  11725. }
  11726. private:
  11727. const gdb::array_view<const uint32_t> m_insns;
  11728. };
  11729. static void
  11730. arm_analyze_prologue_test ()
  11731. {
  11732. for (bfd_endian endianness : {BFD_ENDIAN_LITTLE, BFD_ENDIAN_BIG})
  11733. {
  11734. struct gdbarch_info info;
  11735. info.byte_order = endianness;
  11736. info.byte_order_for_code = endianness;
  11737. info.bfd_arch_info = bfd_scan_arch ("arm");
  11738. struct gdbarch *gdbarch = gdbarch_find_by_info (info);
  11739. SELF_CHECK (gdbarch != NULL);
  11740. /* The "sub" instruction contains an immediate value rotate count of 0,
  11741. which resulted in a 32-bit shift of a 32-bit value, caught by
  11742. UBSan. */
  11743. const uint32_t insns[] = {
  11744. 0xe92d4ff0, /* push {r4, r5, r6, r7, r8, r9, sl, fp, lr} */
  11745. 0xe1a05000, /* mov r5, r0 */
  11746. 0xe5903020, /* ldr r3, [r0, #32] */
  11747. 0xe24dd044, /* sub sp, sp, #68 ; 0x44 */
  11748. };
  11749. test_arm_instruction_reader mem_reader (insns);
  11750. arm_prologue_cache cache;
  11751. cache.saved_regs = trad_frame_alloc_saved_regs (gdbarch);
  11752. arm_analyze_prologue (gdbarch, 0, sizeof (insns) - 1, &cache, mem_reader);
  11753. }
  11754. }
  11755. } // namespace selftests
  11756. #endif /* GDB_SELF_TEST */
  11757. /* Cleans up local record registers and memory allocations. */
  11758. static void
  11759. deallocate_reg_mem (insn_decode_record *record)
  11760. {
  11761. xfree (record->arm_regs);
  11762. xfree (record->arm_mems);
  11763. }
  11764. /* Parse the current instruction and record the values of the registers and
  11765. memory that will be changed in current instruction to record_arch_list".
  11766. Return -1 if something is wrong. */
  11767. int
  11768. arm_process_record (struct gdbarch *gdbarch, struct regcache *regcache,
  11769. CORE_ADDR insn_addr)
  11770. {
  11771. uint32_t no_of_rec = 0;
  11772. uint32_t ret = 0; /* return value: -1:record failure ; 0:success */
  11773. ULONGEST t_bit = 0, insn_id = 0;
  11774. ULONGEST u_regval = 0;
  11775. insn_decode_record arm_record;
  11776. memset (&arm_record, 0, sizeof (insn_decode_record));
  11777. arm_record.regcache = regcache;
  11778. arm_record.this_addr = insn_addr;
  11779. arm_record.gdbarch = gdbarch;
  11780. if (record_debug > 1)
  11781. {
  11782. gdb_printf (gdb_stdlog, "Process record: arm_process_record "
  11783. "addr = %s\n",
  11784. paddress (gdbarch, arm_record.this_addr));
  11785. }
  11786. instruction_reader reader;
  11787. if (extract_arm_insn (reader, &arm_record, 2))
  11788. {
  11789. if (record_debug)
  11790. {
  11791. gdb_printf (gdb_stdlog,
  11792. _("Process record: error reading memory at "
  11793. "addr %s len = %d.\n"),
  11794. paddress (arm_record.gdbarch,
  11795. arm_record.this_addr), 2);
  11796. }
  11797. return -1;
  11798. }
  11799. /* Check the insn, whether it is thumb or arm one. */
  11800. t_bit = arm_psr_thumb_bit (arm_record.gdbarch);
  11801. regcache_raw_read_unsigned (arm_record.regcache, ARM_PS_REGNUM, &u_regval);
  11802. if (!(u_regval & t_bit))
  11803. {
  11804. /* We are decoding arm insn. */
  11805. ret = decode_insn (reader, &arm_record, ARM_RECORD, ARM_INSN_SIZE_BYTES);
  11806. }
  11807. else
  11808. {
  11809. insn_id = bits (arm_record.arm_insn, 11, 15);
  11810. /* is it thumb2 insn? */
  11811. if ((0x1D == insn_id) || (0x1E == insn_id) || (0x1F == insn_id))
  11812. {
  11813. ret = decode_insn (reader, &arm_record, THUMB2_RECORD,
  11814. THUMB2_INSN_SIZE_BYTES);
  11815. }
  11816. else
  11817. {
  11818. /* We are decoding thumb insn. */
  11819. ret = decode_insn (reader, &arm_record, THUMB_RECORD,
  11820. THUMB_INSN_SIZE_BYTES);
  11821. }
  11822. }
  11823. if (0 == ret)
  11824. {
  11825. /* Record registers. */
  11826. record_full_arch_list_add_reg (arm_record.regcache, ARM_PC_REGNUM);
  11827. if (arm_record.arm_regs)
  11828. {
  11829. for (no_of_rec = 0; no_of_rec < arm_record.reg_rec_count; no_of_rec++)
  11830. {
  11831. if (record_full_arch_list_add_reg
  11832. (arm_record.regcache , arm_record.arm_regs[no_of_rec]))
  11833. ret = -1;
  11834. }
  11835. }
  11836. /* Record memories. */
  11837. if (arm_record.arm_mems)
  11838. {
  11839. for (no_of_rec = 0; no_of_rec < arm_record.mem_rec_count; no_of_rec++)
  11840. {
  11841. if (record_full_arch_list_add_mem
  11842. ((CORE_ADDR)arm_record.arm_mems[no_of_rec].addr,
  11843. arm_record.arm_mems[no_of_rec].len))
  11844. ret = -1;
  11845. }
  11846. }
  11847. if (record_full_arch_list_add_end ())
  11848. ret = -1;
  11849. }
  11850. deallocate_reg_mem (&arm_record);
  11851. return ret;
  11852. }
  11853. /* See arm-tdep.h. */
  11854. const target_desc *
  11855. arm_read_description (arm_fp_type fp_type)
  11856. {
  11857. struct target_desc *tdesc = tdesc_arm_list[fp_type];
  11858. if (tdesc == nullptr)
  11859. {
  11860. tdesc = arm_create_target_description (fp_type);
  11861. tdesc_arm_list[fp_type] = tdesc;
  11862. }
  11863. return tdesc;
  11864. }
  11865. /* See arm-tdep.h. */
  11866. const target_desc *
  11867. arm_read_mprofile_description (arm_m_profile_type m_type)
  11868. {
  11869. struct target_desc *tdesc = tdesc_arm_mprofile_list[m_type];
  11870. if (tdesc == nullptr)
  11871. {
  11872. tdesc = arm_create_mprofile_target_description (m_type);
  11873. tdesc_arm_mprofile_list[m_type] = tdesc;
  11874. }
  11875. return tdesc;
  11876. }